2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "c-family/c-pragma.h"
34 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
41 #include "tree-pretty-print.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 } 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 */
179 static cp_lexer *cp_lexer_new_main
181 static cp_lexer *cp_lexer_new_from_tokens
182 (cp_token_cache *tokens);
183 static void cp_lexer_destroy
185 static int cp_lexer_saving_tokens
187 static cp_token *cp_lexer_token_at
188 (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190 (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
193 static cp_token *cp_lexer_peek_nth_token
194 (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196 (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198 (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200 (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
203 static void cp_lexer_purge_token
205 static void cp_lexer_purge_tokens_after
206 (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
209 static void cp_lexer_commit_tokens
211 static void cp_lexer_rollback_tokens
213 static void cp_lexer_print_token
214 (FILE *, cp_token *);
215 static inline bool cp_lexer_debugging_p
217 static void cp_lexer_start_debugging
218 (cp_lexer *) ATTRIBUTE_UNUSED;
219 static void cp_lexer_stop_debugging
220 (cp_lexer *) ATTRIBUTE_UNUSED;
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
228 static tree cp_literal_operator_id
231 /* Manifest constants. */
232 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
233 #define CP_SAVED_TOKEN_STACK 5
237 /* The stream to which debugging output should be written. */
238 static FILE *cp_lexer_debug_stream;
240 /* Nonzero if we are parsing an unevaluated operand: an operand to
241 sizeof, typeof, or alignof. */
242 int cp_unevaluated_operand;
244 /* Dump up to NUM tokens in BUFFER to FILE starting with token
245 START_TOKEN. If START_TOKEN is NULL, the dump starts with the
246 first token in BUFFER. If NUM is 0, dump all the tokens. If
247 CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
248 highlighted by surrounding it in [[ ]]. */
251 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
252 cp_token *start_token, unsigned num,
253 cp_token *curr_token)
255 unsigned i, nprinted;
259 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265 num = VEC_length (cp_token, buffer);
267 if (start_token == NULL)
268 start_token = VEC_address (cp_token, buffer);
270 if (start_token > VEC_address (cp_token, buffer))
272 cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
273 fprintf (file, " ... ");
278 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
280 if (token == start_token)
287 if (token == curr_token)
288 fprintf (file, "[[");
290 cp_lexer_print_token (file, token);
292 if (token == curr_token)
293 fprintf (file, "]]");
299 case CPP_CLOSE_BRACE:
309 if (i == num && i < VEC_length (cp_token, buffer))
311 fprintf (file, " ... ");
312 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
313 VEC_length (cp_token, buffer) - 1));
316 fprintf (file, "\n");
320 /* Dump all tokens in BUFFER to stderr. */
323 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
325 cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
329 /* Dump the cp_parser tree field T to FILE if T is non-NULL. DESC is the
330 description for T. */
333 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
337 fprintf (file, "%s: ", desc);
338 print_node_brief (file, "", t, 0);
343 /* Dump parser context C to FILE. */
346 cp_debug_print_context (FILE *file, cp_parser_context *c)
348 const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
349 fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
350 print_node_brief (file, "", c->object_type, 0);
351 fprintf (file, "}\n");
355 /* Print the stack of parsing contexts to FILE starting with FIRST. */
358 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
361 cp_parser_context *c;
363 fprintf (file, "Parsing context stack:\n");
364 for (i = 0, c = first; c; c = c->next, i++)
366 fprintf (file, "\t#%u: ", i);
367 cp_debug_print_context (file, c);
372 /* Print the value of FLAG to FILE. DESC is a string describing the flag. */
375 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
378 fprintf (file, "%s: true\n", desc);
382 /* Print an unparsed function entry UF to FILE. */
385 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
388 cp_default_arg_entry *default_arg_fn;
391 fprintf (file, "\tFunctions with default args:\n");
393 VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
397 fprintf (file, "\t\tClass type: ");
398 print_node_brief (file, "", default_arg_fn->class_type, 0);
399 fprintf (file, "\t\tDeclaration: ");
400 print_node_brief (file, "", default_arg_fn->decl, 0);
401 fprintf (file, "\n");
404 fprintf (file, "\n\tFunctions with definitions that require "
405 "post-processing\n\t\t");
406 for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
408 print_node_brief (file, "", fn, 0);
411 fprintf (file, "\n");
413 fprintf (file, "\n\tNon-static data members with initializers that require "
414 "post-processing\n\t\t");
415 for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
417 print_node_brief (file, "", fn, 0);
420 fprintf (file, "\n");
424 /* Print the stack of unparsed member functions S to FILE. */
427 cp_debug_print_unparsed_queues (FILE *file,
428 VEC(cp_unparsed_functions_entry, gc) *s)
431 cp_unparsed_functions_entry *uf;
433 fprintf (file, "Unparsed functions\n");
434 for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
436 fprintf (file, "#%u:\n", i);
437 cp_debug_print_unparsed_function (file, uf);
442 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
443 the given PARSER. If FILE is NULL, the output is printed on stderr. */
446 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
448 cp_token *next_token, *first_token, *start_token;
453 next_token = parser->lexer->next_token;
454 first_token = VEC_address (cp_token, parser->lexer->buffer);
455 start_token = (next_token > first_token + window_size / 2)
456 ? next_token - window_size / 2
458 cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
463 /* Dump debugging information for the given PARSER. If FILE is NULL,
464 the output is printed on stderr. */
467 cp_debug_parser (FILE *file, cp_parser *parser)
469 const size_t window_size = 20;
471 expanded_location eloc;
476 fprintf (file, "Parser state\n\n");
477 fprintf (file, "Number of tokens: %u\n",
478 VEC_length (cp_token, parser->lexer->buffer));
479 cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
480 cp_debug_print_tree_if_set (file, "Object scope",
481 parser->object_scope);
482 cp_debug_print_tree_if_set (file, "Qualifying scope",
483 parser->qualifying_scope);
484 cp_debug_print_context_stack (file, parser->context);
485 cp_debug_print_flag (file, "Allow GNU extensions",
486 parser->allow_gnu_extensions_p);
487 cp_debug_print_flag (file, "'>' token is greater-than",
488 parser->greater_than_is_operator_p);
489 cp_debug_print_flag (file, "Default args allowed in current "
490 "parameter list", parser->default_arg_ok_p);
491 cp_debug_print_flag (file, "Parsing integral constant-expression",
492 parser->integral_constant_expression_p);
493 cp_debug_print_flag (file, "Allow non-constant expression in current "
494 "constant-expression",
495 parser->allow_non_integral_constant_expression_p);
496 cp_debug_print_flag (file, "Seen non-constant expression",
497 parser->non_integral_constant_expression_p);
498 cp_debug_print_flag (file, "Local names and 'this' forbidden in "
500 parser->local_variables_forbidden_p);
501 cp_debug_print_flag (file, "In unbraced linkage specification",
502 parser->in_unbraced_linkage_specification_p);
503 cp_debug_print_flag (file, "Parsing a declarator",
504 parser->in_declarator_p);
505 cp_debug_print_flag (file, "In template argument list",
506 parser->in_template_argument_list_p);
507 cp_debug_print_flag (file, "Parsing an iteration statement",
508 parser->in_statement & IN_ITERATION_STMT);
509 cp_debug_print_flag (file, "Parsing a switch statement",
510 parser->in_statement & IN_SWITCH_STMT);
511 cp_debug_print_flag (file, "Parsing a structured OpenMP block",
512 parser->in_statement & IN_OMP_BLOCK);
513 cp_debug_print_flag (file, "Parsing a an OpenMP loop",
514 parser->in_statement & IN_OMP_FOR);
515 cp_debug_print_flag (file, "Parsing an if statement",
516 parser->in_statement & IN_IF_STMT);
517 cp_debug_print_flag (file, "Parsing a type-id in an expression "
518 "context", parser->in_type_id_in_expr_p);
519 cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
520 parser->implicit_extern_c);
521 cp_debug_print_flag (file, "String expressions should be translated "
522 "to execution character set",
523 parser->translate_strings_p);
524 cp_debug_print_flag (file, "Parsing function body outside of a "
525 "local class", parser->in_function_body);
526 cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
527 parser->colon_corrects_to_scope_p);
528 if (parser->type_definition_forbidden_message)
529 fprintf (file, "Error message for forbidden type definitions: %s\n",
530 parser->type_definition_forbidden_message);
531 cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
532 fprintf (file, "Number of class definitions in progress: %u\n",
533 parser->num_classes_being_defined);
534 fprintf (file, "Number of template parameter lists for the current "
535 "declaration: %u\n", parser->num_template_parameter_lists);
536 cp_debug_parser_tokens (file, parser, window_size);
537 token = parser->lexer->next_token;
538 fprintf (file, "Next token to parse:\n");
539 fprintf (file, "\tToken: ");
540 cp_lexer_print_token (file, token);
541 eloc = expand_location (token->location);
542 fprintf (file, "\n\tFile: %s\n", eloc.file);
543 fprintf (file, "\tLine: %d\n", eloc.line);
544 fprintf (file, "\tColumn: %d\n", eloc.column);
548 /* Allocate memory for a new lexer object and return it. */
551 cp_lexer_alloc (void)
555 c_common_no_more_pch ();
557 /* Allocate the memory. */
558 lexer = ggc_alloc_cleared_cp_lexer ();
560 /* Initially we are not debugging. */
561 lexer->debugging_p = false;
563 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
564 CP_SAVED_TOKEN_STACK);
566 /* Create the buffer. */
567 lexer->buffer = VEC_alloc (cp_token, gc, 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 VEC_quick_push (cp_token, lexer->buffer, &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 (cp_token, gc, lexer->buffer, &token);
599 lexer->last_token = VEC_address (cp_token, lexer->buffer)
600 + VEC_length (cp_token, lexer->buffer)
602 lexer->next_token = VEC_length (cp_token, lexer->buffer)
603 ? VEC_address (cp_token, lexer->buffer)
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 = VEC_alloc (cp_token_position, heap,
630 CP_SAVED_TOKEN_STACK);
632 /* Initially we are not debugging. */
633 lexer->debugging_p = false;
635 gcc_assert (!lexer->next_token->purged_p);
639 /* Frees all resources associated with LEXER. */
642 cp_lexer_destroy (cp_lexer *lexer)
644 VEC_free (cp_token, gc, lexer->buffer);
645 VEC_free (cp_token_position, heap, lexer->saved_tokens);
649 /* Returns nonzero if debugging information should be output. */
652 cp_lexer_debugging_p (cp_lexer *lexer)
654 return lexer->debugging_p;
658 static inline cp_token_position
659 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
661 gcc_assert (!previous_p || lexer->next_token != &eof_token);
663 return lexer->next_token - previous_p;
666 static inline cp_token *
667 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
675 lexer->next_token = cp_lexer_token_at (lexer, pos);
678 static inline cp_token_position
679 cp_lexer_previous_token_position (cp_lexer *lexer)
681 if (lexer->next_token == &eof_token)
682 return lexer->last_token - 1;
684 return cp_lexer_token_position (lexer, true);
687 static inline cp_token *
688 cp_lexer_previous_token (cp_lexer *lexer)
690 cp_token_position tp = cp_lexer_previous_token_position (lexer);
692 return cp_lexer_token_at (lexer, tp);
695 /* nonzero if we are presently saving tokens. */
698 cp_lexer_saving_tokens (const cp_lexer* lexer)
700 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
703 /* Store the next token from the preprocessor in *TOKEN. Return true
704 if we reach EOF. If LEXER is NULL, assume we are handling an
705 initial #pragma pch_preprocess, and thus want the lexer to return
706 processed strings. */
709 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
711 static int is_extern_c = 0;
713 /* Get a new token from the preprocessor. */
715 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
716 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
717 token->keyword = RID_MAX;
718 token->pragma_kind = PRAGMA_NONE;
719 token->purged_p = false;
721 /* On some systems, some header files are surrounded by an
722 implicit extern "C" block. Set a flag in the token if it
723 comes from such a header. */
724 is_extern_c += pending_lang_change;
725 pending_lang_change = 0;
726 token->implicit_extern_c = is_extern_c > 0;
728 /* Check to see if this token is a keyword. */
729 if (token->type == CPP_NAME)
731 if (C_IS_RESERVED_WORD (token->u.value))
733 /* Mark this token as a keyword. */
734 token->type = CPP_KEYWORD;
735 /* Record which keyword. */
736 token->keyword = C_RID_CODE (token->u.value);
740 if (warn_cxx0x_compat
741 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
742 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
744 /* Warn about the C++0x keyword (but still treat it as
746 warning (OPT_Wc__0x_compat,
747 "identifier %qE will become a keyword in C++0x",
750 /* Clear out the C_RID_CODE so we don't warn about this
751 particular identifier-turned-keyword again. */
752 C_SET_RID_CODE (token->u.value, RID_MAX);
755 token->ambiguous_p = false;
756 token->keyword = RID_MAX;
759 else if (token->type == CPP_AT_NAME)
761 /* This only happens in Objective-C++; it must be a keyword. */
762 token->type = CPP_KEYWORD;
763 switch (C_RID_CODE (token->u.value))
765 /* Replace 'class' with '@class', 'private' with '@private',
766 etc. This prevents confusion with the C++ keyword
767 'class', and makes the tokens consistent with other
768 Objective-C 'AT' keywords. For example '@class' is
769 reported as RID_AT_CLASS which is consistent with
770 '@synchronized', which is reported as
773 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
774 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
775 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
776 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
777 case RID_THROW: token->keyword = RID_AT_THROW; break;
778 case RID_TRY: token->keyword = RID_AT_TRY; break;
779 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
780 default: token->keyword = C_RID_CODE (token->u.value);
783 else if (token->type == CPP_PRAGMA)
785 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
786 token->pragma_kind = ((enum pragma_kind)
787 TREE_INT_CST_LOW (token->u.value));
788 token->u.value = NULL_TREE;
792 /* Update the globals input_location and the input file stack from TOKEN. */
794 cp_lexer_set_source_position_from_token (cp_token *token)
796 if (token->type != CPP_EOF)
798 input_location = token->location;
802 /* Return a pointer to the next token in the token stream, but do not
805 static inline cp_token *
806 cp_lexer_peek_token (cp_lexer *lexer)
808 if (cp_lexer_debugging_p (lexer))
810 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
811 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
812 putc ('\n', cp_lexer_debug_stream);
814 return lexer->next_token;
817 /* Return true if the next token has the indicated TYPE. */
820 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
822 return cp_lexer_peek_token (lexer)->type == type;
825 /* Return true if the next token does not have the indicated TYPE. */
828 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
830 return !cp_lexer_next_token_is (lexer, type);
833 /* Return true if the next token is the indicated KEYWORD. */
836 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
838 return cp_lexer_peek_token (lexer)->keyword == keyword;
841 /* Return true if the next token is not the indicated KEYWORD. */
844 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
846 return cp_lexer_peek_token (lexer)->keyword != keyword;
849 /* Return true if the next token is a keyword for a decl-specifier. */
852 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
856 token = cp_lexer_peek_token (lexer);
857 switch (token->keyword)
859 /* auto specifier: storage-class-specifier in C++,
860 simple-type-specifier in C++0x. */
862 /* Storage classes. */
868 /* Elaborated type specifiers. */
874 /* Simple type specifiers. */
889 /* GNU extensions. */
892 /* C++0x extensions. */
894 case RID_UNDERLYING_TYPE:
902 /* Returns TRUE iff the token T begins a decltype type. */
905 token_is_decltype (cp_token *t)
907 return (t->keyword == RID_DECLTYPE
908 || t->type == CPP_DECLTYPE);
911 /* Returns TRUE iff the next token begins a decltype type. */
914 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
916 cp_token *t = cp_lexer_peek_token (lexer);
917 return token_is_decltype (t);
920 /* Return a pointer to the Nth token in the token stream. If N is 1,
921 then this is precisely equivalent to cp_lexer_peek_token (except
922 that it is not inline). One would like to disallow that case, but
923 there is one case (cp_parser_nth_token_starts_template_id) where
924 the caller passes a variable for N and it might be 1. */
927 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
931 /* N is 1-based, not zero-based. */
934 if (cp_lexer_debugging_p (lexer))
935 fprintf (cp_lexer_debug_stream,
936 "cp_lexer: peeking ahead %ld at token: ", (long)n);
939 token = lexer->next_token;
940 gcc_assert (!n || token != &eof_token);
944 if (token == lexer->last_token)
950 if (!token->purged_p)
954 if (cp_lexer_debugging_p (lexer))
956 cp_lexer_print_token (cp_lexer_debug_stream, token);
957 putc ('\n', cp_lexer_debug_stream);
963 /* Return the next token, and advance the lexer's next_token pointer
964 to point to the next non-purged token. */
967 cp_lexer_consume_token (cp_lexer* lexer)
969 cp_token *token = lexer->next_token;
971 gcc_assert (token != &eof_token);
972 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
977 if (lexer->next_token == lexer->last_token)
979 lexer->next_token = &eof_token;
984 while (lexer->next_token->purged_p);
986 cp_lexer_set_source_position_from_token (token);
988 /* Provide debugging output. */
989 if (cp_lexer_debugging_p (lexer))
991 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
992 cp_lexer_print_token (cp_lexer_debug_stream, token);
993 putc ('\n', cp_lexer_debug_stream);
999 /* Permanently remove the next token from the token stream, and
1000 advance the next_token pointer to refer to the next non-purged
1004 cp_lexer_purge_token (cp_lexer *lexer)
1006 cp_token *tok = lexer->next_token;
1008 gcc_assert (tok != &eof_token);
1009 tok->purged_p = true;
1010 tok->location = UNKNOWN_LOCATION;
1011 tok->u.value = NULL_TREE;
1012 tok->keyword = RID_MAX;
1017 if (tok == lexer->last_token)
1023 while (tok->purged_p);
1024 lexer->next_token = tok;
1027 /* Permanently remove all tokens after TOK, up to, but not
1028 including, the token that will be returned next by
1029 cp_lexer_peek_token. */
1032 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1034 cp_token *peek = lexer->next_token;
1036 if (peek == &eof_token)
1037 peek = lexer->last_token;
1039 gcc_assert (tok < peek);
1041 for ( tok += 1; tok != peek; tok += 1)
1043 tok->purged_p = true;
1044 tok->location = UNKNOWN_LOCATION;
1045 tok->u.value = NULL_TREE;
1046 tok->keyword = RID_MAX;
1050 /* Begin saving tokens. All tokens consumed after this point will be
1054 cp_lexer_save_tokens (cp_lexer* lexer)
1056 /* Provide debugging output. */
1057 if (cp_lexer_debugging_p (lexer))
1058 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1060 VEC_safe_push (cp_token_position, heap,
1061 lexer->saved_tokens, lexer->next_token);
1064 /* Commit to the portion of the token stream most recently saved. */
1067 cp_lexer_commit_tokens (cp_lexer* lexer)
1069 /* Provide debugging output. */
1070 if (cp_lexer_debugging_p (lexer))
1071 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1073 VEC_pop (cp_token_position, lexer->saved_tokens);
1076 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1077 to the token stream. Stop saving tokens. */
1080 cp_lexer_rollback_tokens (cp_lexer* lexer)
1082 /* Provide debugging output. */
1083 if (cp_lexer_debugging_p (lexer))
1084 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1086 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1089 /* Print a representation of the TOKEN on the STREAM. */
1092 cp_lexer_print_token (FILE * stream, cp_token *token)
1094 /* We don't use cpp_type2name here because the parser defines
1095 a few tokens of its own. */
1096 static const char *const token_names[] = {
1097 /* cpplib-defined token types */
1098 #define OP(e, s) #e,
1099 #define TK(e, s) #e,
1103 /* C++ parser token types - see "Manifest constants", above. */
1106 "NESTED_NAME_SPECIFIER",
1109 /* For some tokens, print the associated data. */
1110 switch (token->type)
1113 /* Some keywords have a value that is not an IDENTIFIER_NODE.
1114 For example, `struct' is mapped to an INTEGER_CST. */
1115 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1117 /* else fall through */
1119 fputs (IDENTIFIER_POINTER (token->u.value), stream);
1126 case CPP_UTF8STRING:
1127 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1131 print_generic_expr (stream, token->u.value, 0);
1135 /* If we have a name for the token, print it out. Otherwise, we
1136 simply give the numeric code. */
1137 if (token->type < ARRAY_SIZE(token_names))
1138 fputs (token_names[token->type], stream);
1140 fprintf (stream, "[%d]", token->type);
1145 /* Start emitting debugging information. */
1148 cp_lexer_start_debugging (cp_lexer* lexer)
1150 lexer->debugging_p = true;
1151 cp_lexer_debug_stream = stderr;
1154 /* Stop emitting debugging information. */
1157 cp_lexer_stop_debugging (cp_lexer* lexer)
1159 lexer->debugging_p = false;
1160 cp_lexer_debug_stream = NULL;
1163 /* Create a new cp_token_cache, representing a range of tokens. */
1165 static cp_token_cache *
1166 cp_token_cache_new (cp_token *first, cp_token *last)
1168 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1169 cache->first = first;
1175 /* Decl-specifiers. */
1177 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
1180 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1182 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1187 /* Nothing other than the parser should be creating declarators;
1188 declarators are a semi-syntactic representation of C++ entities.
1189 Other parts of the front end that need to create entities (like
1190 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
1192 static cp_declarator *make_call_declarator
1193 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1194 static cp_declarator *make_array_declarator
1195 (cp_declarator *, tree);
1196 static cp_declarator *make_pointer_declarator
1197 (cp_cv_quals, cp_declarator *);
1198 static cp_declarator *make_reference_declarator
1199 (cp_cv_quals, cp_declarator *, bool);
1200 static cp_parameter_declarator *make_parameter_declarator
1201 (cp_decl_specifier_seq *, cp_declarator *, tree);
1202 static cp_declarator *make_ptrmem_declarator
1203 (cp_cv_quals, tree, cp_declarator *);
1205 /* An erroneous declarator. */
1206 static cp_declarator *cp_error_declarator;
1208 /* The obstack on which declarators and related data structures are
1210 static struct obstack declarator_obstack;
1212 /* Alloc BYTES from the declarator memory pool. */
1214 static inline void *
1215 alloc_declarator (size_t bytes)
1217 return obstack_alloc (&declarator_obstack, bytes);
1220 /* Allocate a declarator of the indicated KIND. Clear fields that are
1221 common to all declarators. */
1223 static cp_declarator *
1224 make_declarator (cp_declarator_kind kind)
1226 cp_declarator *declarator;
1228 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1229 declarator->kind = kind;
1230 declarator->attributes = NULL_TREE;
1231 declarator->declarator = NULL;
1232 declarator->parameter_pack_p = false;
1233 declarator->id_loc = UNKNOWN_LOCATION;
1238 /* Make a declarator for a generalized identifier. If
1239 QUALIFYING_SCOPE is non-NULL, the identifier is
1240 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1241 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1244 static cp_declarator *
1245 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1246 special_function_kind sfk)
1248 cp_declarator *declarator;
1250 /* It is valid to write:
1252 class C { void f(); };
1256 The standard is not clear about whether `typedef const C D' is
1257 legal; as of 2002-09-15 the committee is considering that
1258 question. EDG 3.0 allows that syntax. Therefore, we do as
1260 if (qualifying_scope && TYPE_P (qualifying_scope))
1261 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1263 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1264 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1265 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1267 declarator = make_declarator (cdk_id);
1268 declarator->u.id.qualifying_scope = qualifying_scope;
1269 declarator->u.id.unqualified_name = unqualified_name;
1270 declarator->u.id.sfk = sfk;
1275 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1276 of modifiers such as const or volatile to apply to the pointer
1277 type, represented as identifiers. */
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1282 cp_declarator *declarator;
1284 declarator = make_declarator (cdk_pointer);
1285 declarator->declarator = target;
1286 declarator->u.pointer.qualifiers = cv_qualifiers;
1287 declarator->u.pointer.class_type = NULL_TREE;
1290 declarator->id_loc = target->id_loc;
1291 declarator->parameter_pack_p = target->parameter_pack_p;
1292 target->parameter_pack_p = false;
1295 declarator->parameter_pack_p = false;
1300 /* Like make_pointer_declarator -- but for references. */
1303 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1306 cp_declarator *declarator;
1308 declarator = make_declarator (cdk_reference);
1309 declarator->declarator = target;
1310 declarator->u.reference.qualifiers = cv_qualifiers;
1311 declarator->u.reference.rvalue_ref = rvalue_ref;
1314 declarator->id_loc = target->id_loc;
1315 declarator->parameter_pack_p = target->parameter_pack_p;
1316 target->parameter_pack_p = false;
1319 declarator->parameter_pack_p = false;
1324 /* Like make_pointer_declarator -- but for a pointer to a non-static
1325 member of CLASS_TYPE. */
1328 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1329 cp_declarator *pointee)
1331 cp_declarator *declarator;
1333 declarator = make_declarator (cdk_ptrmem);
1334 declarator->declarator = pointee;
1335 declarator->u.pointer.qualifiers = cv_qualifiers;
1336 declarator->u.pointer.class_type = class_type;
1340 declarator->parameter_pack_p = pointee->parameter_pack_p;
1341 pointee->parameter_pack_p = false;
1344 declarator->parameter_pack_p = false;
1349 /* Make a declarator for the function given by TARGET, with the
1350 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1351 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1352 indicates what exceptions can be thrown. */
1355 make_call_declarator (cp_declarator *target,
1357 cp_cv_quals cv_qualifiers,
1358 cp_virt_specifiers virt_specifiers,
1359 tree exception_specification,
1360 tree late_return_type)
1362 cp_declarator *declarator;
1364 declarator = make_declarator (cdk_function);
1365 declarator->declarator = target;
1366 declarator->u.function.parameters = parms;
1367 declarator->u.function.qualifiers = cv_qualifiers;
1368 declarator->u.function.virt_specifiers = virt_specifiers;
1369 declarator->u.function.exception_specification = exception_specification;
1370 declarator->u.function.late_return_type = late_return_type;
1373 declarator->id_loc = target->id_loc;
1374 declarator->parameter_pack_p = target->parameter_pack_p;
1375 target->parameter_pack_p = false;
1378 declarator->parameter_pack_p = false;
1383 /* Make a declarator for an array of BOUNDS elements, each of which is
1384 defined by ELEMENT. */
1387 make_array_declarator (cp_declarator *element, tree bounds)
1389 cp_declarator *declarator;
1391 declarator = make_declarator (cdk_array);
1392 declarator->declarator = element;
1393 declarator->u.array.bounds = bounds;
1396 declarator->id_loc = element->id_loc;
1397 declarator->parameter_pack_p = element->parameter_pack_p;
1398 element->parameter_pack_p = false;
1401 declarator->parameter_pack_p = false;
1406 /* Determine whether the declarator we've seen so far can be a
1407 parameter pack, when followed by an ellipsis. */
1409 declarator_can_be_parameter_pack (cp_declarator *declarator)
1411 /* Search for a declarator name, or any other declarator that goes
1412 after the point where the ellipsis could appear in a parameter
1413 pack. If we find any of these, then this declarator can not be
1414 made into a parameter pack. */
1416 while (declarator && !found)
1418 switch ((int)declarator->kind)
1429 declarator = declarator->declarator;
1437 cp_parameter_declarator *no_parameters;
1439 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1440 DECLARATOR and DEFAULT_ARGUMENT. */
1442 cp_parameter_declarator *
1443 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1444 cp_declarator *declarator,
1445 tree default_argument)
1447 cp_parameter_declarator *parameter;
1449 parameter = ((cp_parameter_declarator *)
1450 alloc_declarator (sizeof (cp_parameter_declarator)));
1451 parameter->next = NULL;
1452 if (decl_specifiers)
1453 parameter->decl_specifiers = *decl_specifiers;
1455 clear_decl_specs (¶meter->decl_specifiers);
1456 parameter->declarator = declarator;
1457 parameter->default_argument = default_argument;
1458 parameter->ellipsis_p = false;
1463 /* Returns true iff DECLARATOR is a declaration for a function. */
1466 function_declarator_p (const cp_declarator *declarator)
1470 if (declarator->kind == cdk_function
1471 && declarator->declarator->kind == cdk_id)
1473 if (declarator->kind == cdk_id
1474 || declarator->kind == cdk_error)
1476 declarator = declarator->declarator;
1486 A cp_parser parses the token stream as specified by the C++
1487 grammar. Its job is purely parsing, not semantic analysis. For
1488 example, the parser breaks the token stream into declarators,
1489 expressions, statements, and other similar syntactic constructs.
1490 It does not check that the types of the expressions on either side
1491 of an assignment-statement are compatible, or that a function is
1492 not declared with a parameter of type `void'.
1494 The parser invokes routines elsewhere in the compiler to perform
1495 semantic analysis and to build up the abstract syntax tree for the
1498 The parser (and the template instantiation code, which is, in a
1499 way, a close relative of parsing) are the only parts of the
1500 compiler that should be calling push_scope and pop_scope, or
1501 related functions. The parser (and template instantiation code)
1502 keeps track of what scope is presently active; everything else
1503 should simply honor that. (The code that generates static
1504 initializers may also need to set the scope, in order to check
1505 access control correctly when emitting the initializers.)
1510 The parser is of the standard recursive-descent variety. Upcoming
1511 tokens in the token stream are examined in order to determine which
1512 production to use when parsing a non-terminal. Some C++ constructs
1513 require arbitrary look ahead to disambiguate. For example, it is
1514 impossible, in the general case, to tell whether a statement is an
1515 expression or declaration without scanning the entire statement.
1516 Therefore, the parser is capable of "parsing tentatively." When the
1517 parser is not sure what construct comes next, it enters this mode.
1518 Then, while we attempt to parse the construct, the parser queues up
1519 error messages, rather than issuing them immediately, and saves the
1520 tokens it consumes. If the construct is parsed successfully, the
1521 parser "commits", i.e., it issues any queued error messages and
1522 the tokens that were being preserved are permanently discarded.
1523 If, however, the construct is not parsed successfully, the parser
1524 rolls back its state completely so that it can resume parsing using
1525 a different alternative.
1530 The performance of the parser could probably be improved substantially.
1531 We could often eliminate the need to parse tentatively by looking ahead
1532 a little bit. In some places, this approach might not entirely eliminate
1533 the need to parse tentatively, but it might still speed up the average
1536 /* Flags that are passed to some parsing functions. These values can
1537 be bitwise-ored together. */
1542 CP_PARSER_FLAGS_NONE = 0x0,
1543 /* The construct is optional. If it is not present, then no error
1544 should be issued. */
1545 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1546 /* When parsing a type-specifier, treat user-defined type-names
1547 as non-type identifiers. */
1548 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1549 /* When parsing a type-specifier, do not try to parse a class-specifier
1550 or enum-specifier. */
1551 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1552 /* When parsing a decl-specifier-seq, only allow type-specifier or
1554 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1557 /* This type is used for parameters and variables which hold
1558 combinations of the above flags. */
1559 typedef int cp_parser_flags;
1561 /* The different kinds of declarators we want to parse. */
1563 typedef enum cp_parser_declarator_kind
1565 /* We want an abstract declarator. */
1566 CP_PARSER_DECLARATOR_ABSTRACT,
1567 /* We want a named declarator. */
1568 CP_PARSER_DECLARATOR_NAMED,
1569 /* We don't mind, but the name must be an unqualified-id. */
1570 CP_PARSER_DECLARATOR_EITHER
1571 } cp_parser_declarator_kind;
1573 /* The precedence values used to parse binary expressions. The minimum value
1574 of PREC must be 1, because zero is reserved to quickly discriminate
1575 binary operators from other tokens. */
1580 PREC_LOGICAL_OR_EXPRESSION,
1581 PREC_LOGICAL_AND_EXPRESSION,
1582 PREC_INCLUSIVE_OR_EXPRESSION,
1583 PREC_EXCLUSIVE_OR_EXPRESSION,
1584 PREC_AND_EXPRESSION,
1585 PREC_EQUALITY_EXPRESSION,
1586 PREC_RELATIONAL_EXPRESSION,
1587 PREC_SHIFT_EXPRESSION,
1588 PREC_ADDITIVE_EXPRESSION,
1589 PREC_MULTIPLICATIVE_EXPRESSION,
1591 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1594 /* A mapping from a token type to a corresponding tree node type, with a
1595 precedence value. */
1597 typedef struct cp_parser_binary_operations_map_node
1599 /* The token type. */
1600 enum cpp_ttype token_type;
1601 /* The corresponding tree code. */
1602 enum tree_code tree_type;
1603 /* The precedence of this operator. */
1604 enum cp_parser_prec prec;
1605 } cp_parser_binary_operations_map_node;
1607 typedef struct cp_parser_expression_stack_entry
1609 /* Left hand side of the binary operation we are currently
1612 /* Original tree code for left hand side, if it was a binary
1613 expression itself (used for -Wparentheses). */
1614 enum tree_code lhs_type;
1615 /* Tree code for the binary operation we are parsing. */
1616 enum tree_code tree_type;
1617 /* Precedence of the binary operation we are parsing. */
1618 enum cp_parser_prec prec;
1619 } cp_parser_expression_stack_entry;
1621 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1622 entries because precedence levels on the stack are monotonically
1624 typedef struct cp_parser_expression_stack_entry
1625 cp_parser_expression_stack[NUM_PREC_VALUES];
1629 /* Constructors and destructors. */
1631 static cp_parser_context *cp_parser_context_new
1632 (cp_parser_context *);
1634 /* Class variables. */
1636 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1638 /* The operator-precedence table used by cp_parser_binary_expression.
1639 Transformed into an associative array (binops_by_token) by
1642 static const cp_parser_binary_operations_map_node binops[] = {
1643 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1644 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1646 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1647 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1648 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1650 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1651 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1653 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1654 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1656 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1657 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1658 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1659 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1661 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1662 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1664 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1666 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1668 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1670 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1672 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1675 /* The same as binops, but initialized by cp_parser_new so that
1676 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1678 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1680 /* Constructors and destructors. */
1682 /* Construct a new context. The context below this one on the stack
1683 is given by NEXT. */
1685 static cp_parser_context *
1686 cp_parser_context_new (cp_parser_context* next)
1688 cp_parser_context *context;
1690 /* Allocate the storage. */
1691 if (cp_parser_context_free_list != NULL)
1693 /* Pull the first entry from the free list. */
1694 context = cp_parser_context_free_list;
1695 cp_parser_context_free_list = context->next;
1696 memset (context, 0, sizeof (*context));
1699 context = ggc_alloc_cleared_cp_parser_context ();
1701 /* No errors have occurred yet in this context. */
1702 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1703 /* If this is not the bottommost context, copy information that we
1704 need from the previous context. */
1707 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1708 expression, then we are parsing one in this context, too. */
1709 context->object_type = next->object_type;
1710 /* Thread the stack. */
1711 context->next = next;
1717 /* Managing the unparsed function queues. */
1719 #define unparsed_funs_with_default_args \
1720 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1721 #define unparsed_funs_with_definitions \
1722 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1723 #define unparsed_nsdmis \
1724 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1727 push_unparsed_function_queues (cp_parser *parser)
1729 VEC_safe_push (cp_unparsed_functions_entry, gc,
1730 parser->unparsed_queues, NULL);
1731 unparsed_funs_with_default_args = NULL;
1732 unparsed_funs_with_definitions = make_tree_vector ();
1733 unparsed_nsdmis = NULL;
1737 pop_unparsed_function_queues (cp_parser *parser)
1739 release_tree_vector (unparsed_funs_with_definitions);
1740 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1745 /* Constructors and destructors. */
1747 static cp_parser *cp_parser_new
1750 /* Routines to parse various constructs.
1752 Those that return `tree' will return the error_mark_node (rather
1753 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1754 Sometimes, they will return an ordinary node if error-recovery was
1755 attempted, even though a parse error occurred. So, to check
1756 whether or not a parse error occurred, you should always use
1757 cp_parser_error_occurred. If the construct is optional (indicated
1758 either by an `_opt' in the name of the function that does the
1759 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1760 the construct is not present. */
1762 /* Lexical conventions [gram.lex] */
1764 static tree cp_parser_identifier
1766 static tree cp_parser_string_literal
1767 (cp_parser *, bool, bool);
1768 static tree cp_parser_userdef_char_literal
1770 static tree cp_parser_userdef_string_literal
1772 static tree cp_parser_userdef_numeric_literal
1775 /* Basic concepts [gram.basic] */
1777 static bool cp_parser_translation_unit
1780 /* Expressions [gram.expr] */
1782 static tree cp_parser_primary_expression
1783 (cp_parser *, bool, bool, bool, cp_id_kind *);
1784 static tree cp_parser_id_expression
1785 (cp_parser *, bool, bool, bool *, bool, bool);
1786 static tree cp_parser_unqualified_id
1787 (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_nested_name_specifier_opt
1789 (cp_parser *, bool, bool, bool, bool);
1790 static tree cp_parser_nested_name_specifier
1791 (cp_parser *, bool, bool, bool, bool);
1792 static tree cp_parser_qualifying_entity
1793 (cp_parser *, bool, bool, bool, bool, bool);
1794 static tree cp_parser_postfix_expression
1795 (cp_parser *, bool, bool, bool, cp_id_kind *);
1796 static tree cp_parser_postfix_open_square_expression
1797 (cp_parser *, tree, bool);
1798 static tree cp_parser_postfix_dot_deref_expression
1799 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1800 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1801 (cp_parser *, int, bool, bool, bool *);
1802 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1803 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1804 static void cp_parser_pseudo_destructor_name
1805 (cp_parser *, tree *, tree *);
1806 static tree cp_parser_unary_expression
1807 (cp_parser *, bool, bool, cp_id_kind *);
1808 static enum tree_code cp_parser_unary_operator
1810 static tree cp_parser_new_expression
1812 static VEC(tree,gc) *cp_parser_new_placement
1814 static tree cp_parser_new_type_id
1815 (cp_parser *, tree *);
1816 static cp_declarator *cp_parser_new_declarator_opt
1818 static cp_declarator *cp_parser_direct_new_declarator
1820 static VEC(tree,gc) *cp_parser_new_initializer
1822 static tree cp_parser_delete_expression
1824 static tree cp_parser_cast_expression
1825 (cp_parser *, bool, bool, cp_id_kind *);
1826 static tree cp_parser_binary_expression
1827 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1828 static tree cp_parser_question_colon_clause
1829 (cp_parser *, tree);
1830 static tree cp_parser_assignment_expression
1831 (cp_parser *, bool, cp_id_kind *);
1832 static enum tree_code cp_parser_assignment_operator_opt
1834 static tree cp_parser_expression
1835 (cp_parser *, bool, cp_id_kind *);
1836 static tree cp_parser_constant_expression
1837 (cp_parser *, bool, bool *);
1838 static tree cp_parser_builtin_offsetof
1840 static tree cp_parser_lambda_expression
1842 static void cp_parser_lambda_introducer
1843 (cp_parser *, tree);
1844 static bool cp_parser_lambda_declarator_opt
1845 (cp_parser *, tree);
1846 static void cp_parser_lambda_body
1847 (cp_parser *, tree);
1849 /* Statements [gram.stmt.stmt] */
1851 static void cp_parser_statement
1852 (cp_parser *, tree, bool, bool *);
1853 static void cp_parser_label_for_labeled_statement
1855 static tree cp_parser_expression_statement
1856 (cp_parser *, tree);
1857 static tree cp_parser_compound_statement
1858 (cp_parser *, tree, bool, bool);
1859 static void cp_parser_statement_seq_opt
1860 (cp_parser *, tree);
1861 static tree cp_parser_selection_statement
1862 (cp_parser *, bool *);
1863 static tree cp_parser_condition
1865 static tree cp_parser_iteration_statement
1867 static bool cp_parser_for_init_statement
1868 (cp_parser *, tree *decl);
1869 static tree cp_parser_for
1871 static tree cp_parser_c_for
1872 (cp_parser *, tree, tree);
1873 static tree cp_parser_range_for
1874 (cp_parser *, tree, tree, tree);
1875 static void do_range_for_auto_deduction
1877 static tree cp_parser_perform_range_for_lookup
1878 (tree, tree *, tree *);
1879 static tree cp_parser_range_for_member_function
1881 static tree cp_parser_jump_statement
1883 static void cp_parser_declaration_statement
1886 static tree cp_parser_implicitly_scoped_statement
1887 (cp_parser *, bool *);
1888 static void cp_parser_already_scoped_statement
1891 /* Declarations [gram.dcl.dcl] */
1893 static void cp_parser_declaration_seq_opt
1895 static void cp_parser_declaration
1897 static void cp_parser_block_declaration
1898 (cp_parser *, bool);
1899 static void cp_parser_simple_declaration
1900 (cp_parser *, bool, tree *);
1901 static void cp_parser_decl_specifier_seq
1902 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1903 static tree cp_parser_storage_class_specifier_opt
1905 static tree cp_parser_function_specifier_opt
1906 (cp_parser *, cp_decl_specifier_seq *);
1907 static tree cp_parser_type_specifier
1908 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1910 static tree cp_parser_simple_type_specifier
1911 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1912 static tree cp_parser_type_name
1914 static tree cp_parser_nonclass_name
1915 (cp_parser* parser);
1916 static tree cp_parser_elaborated_type_specifier
1917 (cp_parser *, bool, bool);
1918 static tree cp_parser_enum_specifier
1920 static void cp_parser_enumerator_list
1921 (cp_parser *, tree);
1922 static void cp_parser_enumerator_definition
1923 (cp_parser *, tree);
1924 static tree cp_parser_namespace_name
1926 static void cp_parser_namespace_definition
1928 static void cp_parser_namespace_body
1930 static tree cp_parser_qualified_namespace_specifier
1932 static void cp_parser_namespace_alias_definition
1934 static bool cp_parser_using_declaration
1935 (cp_parser *, bool);
1936 static void cp_parser_using_directive
1938 static void cp_parser_asm_definition
1940 static void cp_parser_linkage_specification
1942 static void cp_parser_static_assert
1943 (cp_parser *, bool);
1944 static tree cp_parser_decltype
1947 /* Declarators [gram.dcl.decl] */
1949 static tree cp_parser_init_declarator
1950 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1951 static cp_declarator *cp_parser_declarator
1952 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1953 static cp_declarator *cp_parser_direct_declarator
1954 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1955 static enum tree_code cp_parser_ptr_operator
1956 (cp_parser *, tree *, cp_cv_quals *);
1957 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1959 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1961 static tree cp_parser_late_return_type_opt
1962 (cp_parser *, cp_cv_quals);
1963 static tree cp_parser_declarator_id
1964 (cp_parser *, bool);
1965 static tree cp_parser_type_id
1967 static tree cp_parser_template_type_arg
1969 static tree cp_parser_trailing_type_id (cp_parser *);
1970 static tree cp_parser_type_id_1
1971 (cp_parser *, bool, bool);
1972 static void cp_parser_type_specifier_seq
1973 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1974 static tree cp_parser_parameter_declaration_clause
1976 static tree cp_parser_parameter_declaration_list
1977 (cp_parser *, bool *);
1978 static cp_parameter_declarator *cp_parser_parameter_declaration
1979 (cp_parser *, bool, bool *);
1980 static tree cp_parser_default_argument
1981 (cp_parser *, bool);
1982 static void cp_parser_function_body
1984 static tree cp_parser_initializer
1985 (cp_parser *, bool *, bool *);
1986 static tree cp_parser_initializer_clause
1987 (cp_parser *, bool *);
1988 static tree cp_parser_braced_list
1989 (cp_parser*, bool*);
1990 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1991 (cp_parser *, bool *);
1993 static bool cp_parser_ctor_initializer_opt_and_function_body
1996 /* Classes [gram.class] */
1998 static tree cp_parser_class_name
1999 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2000 static tree cp_parser_class_specifier
2002 static tree cp_parser_class_head
2003 (cp_parser *, bool *, tree *, tree *);
2004 static enum tag_types cp_parser_class_key
2006 static void cp_parser_member_specification_opt
2008 static void cp_parser_member_declaration
2010 static tree cp_parser_pure_specifier
2012 static tree cp_parser_constant_initializer
2015 /* Derived classes [gram.class.derived] */
2017 static tree cp_parser_base_clause
2019 static tree cp_parser_base_specifier
2022 /* Special member functions [gram.special] */
2024 static tree cp_parser_conversion_function_id
2026 static tree cp_parser_conversion_type_id
2028 static cp_declarator *cp_parser_conversion_declarator_opt
2030 static bool cp_parser_ctor_initializer_opt
2032 static void cp_parser_mem_initializer_list
2034 static tree cp_parser_mem_initializer
2036 static tree cp_parser_mem_initializer_id
2039 /* Overloading [gram.over] */
2041 static tree cp_parser_operator_function_id
2043 static tree cp_parser_operator
2046 /* Templates [gram.temp] */
2048 static void cp_parser_template_declaration
2049 (cp_parser *, bool);
2050 static tree cp_parser_template_parameter_list
2052 static tree cp_parser_template_parameter
2053 (cp_parser *, bool *, bool *);
2054 static tree cp_parser_type_parameter
2055 (cp_parser *, bool *);
2056 static tree cp_parser_template_id
2057 (cp_parser *, bool, bool, bool);
2058 static tree cp_parser_template_name
2059 (cp_parser *, bool, bool, bool, bool *);
2060 static tree cp_parser_template_argument_list
2062 static tree cp_parser_template_argument
2064 static void cp_parser_explicit_instantiation
2066 static void cp_parser_explicit_specialization
2069 /* Exception handling [gram.exception] */
2071 static tree cp_parser_try_block
2073 static bool cp_parser_function_try_block
2075 static void cp_parser_handler_seq
2077 static void cp_parser_handler
2079 static tree cp_parser_exception_declaration
2081 static tree cp_parser_throw_expression
2083 static tree cp_parser_exception_specification_opt
2085 static tree cp_parser_type_id_list
2088 /* GNU Extensions */
2090 static tree cp_parser_asm_specification_opt
2092 static tree cp_parser_asm_operand_list
2094 static tree cp_parser_asm_clobber_list
2096 static tree cp_parser_asm_label_list
2098 static tree cp_parser_attributes_opt
2100 static tree cp_parser_attribute_list
2102 static bool cp_parser_extension_opt
2103 (cp_parser *, int *);
2104 static void cp_parser_label_declaration
2107 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2108 static bool cp_parser_pragma
2109 (cp_parser *, enum pragma_context);
2111 /* Objective-C++ Productions */
2113 static tree cp_parser_objc_message_receiver
2115 static tree cp_parser_objc_message_args
2117 static tree cp_parser_objc_message_expression
2119 static tree cp_parser_objc_encode_expression
2121 static tree cp_parser_objc_defs_expression
2123 static tree cp_parser_objc_protocol_expression
2125 static tree cp_parser_objc_selector_expression
2127 static tree cp_parser_objc_expression
2129 static bool cp_parser_objc_selector_p
2131 static tree cp_parser_objc_selector
2133 static tree cp_parser_objc_protocol_refs_opt
2135 static void cp_parser_objc_declaration
2136 (cp_parser *, tree);
2137 static tree cp_parser_objc_statement
2139 static bool cp_parser_objc_valid_prefix_attributes
2140 (cp_parser *, tree *);
2141 static void cp_parser_objc_at_property_declaration
2143 static void cp_parser_objc_at_synthesize_declaration
2145 static void cp_parser_objc_at_dynamic_declaration
2147 static tree cp_parser_objc_struct_declaration
2150 /* Utility Routines */
2152 static tree cp_parser_lookup_name
2153 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2154 static tree cp_parser_lookup_name_simple
2155 (cp_parser *, tree, location_t);
2156 static tree cp_parser_maybe_treat_template_as_class
2158 static bool cp_parser_check_declarator_template_parameters
2159 (cp_parser *, cp_declarator *, location_t);
2160 static bool cp_parser_check_template_parameters
2161 (cp_parser *, unsigned, location_t, cp_declarator *);
2162 static tree cp_parser_simple_cast_expression
2164 static tree cp_parser_global_scope_opt
2165 (cp_parser *, bool);
2166 static bool cp_parser_constructor_declarator_p
2167 (cp_parser *, bool);
2168 static tree cp_parser_function_definition_from_specifiers_and_declarator
2169 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2170 static tree cp_parser_function_definition_after_declarator
2171 (cp_parser *, bool);
2172 static void cp_parser_template_declaration_after_export
2173 (cp_parser *, bool);
2174 static void cp_parser_perform_template_parameter_access_checks
2175 (VEC (deferred_access_check,gc)*);
2176 static tree cp_parser_single_declaration
2177 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2178 static tree cp_parser_functional_cast
2179 (cp_parser *, tree);
2180 static tree cp_parser_save_member_function_body
2181 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2182 static tree cp_parser_save_nsdmi
2184 static tree cp_parser_enclosed_template_argument_list
2186 static void cp_parser_save_default_args
2187 (cp_parser *, tree);
2188 static void cp_parser_late_parsing_for_member
2189 (cp_parser *, tree);
2190 static tree cp_parser_late_parse_one_default_arg
2191 (cp_parser *, tree, tree, tree);
2192 static void cp_parser_late_parsing_nsdmi
2193 (cp_parser *, tree);
2194 static void cp_parser_late_parsing_default_args
2195 (cp_parser *, tree);
2196 static tree cp_parser_sizeof_operand
2197 (cp_parser *, enum rid);
2198 static tree cp_parser_trait_expr
2199 (cp_parser *, enum rid);
2200 static bool cp_parser_declares_only_class_p
2202 static void cp_parser_set_storage_class
2203 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2204 static void cp_parser_set_decl_spec_type
2205 (cp_decl_specifier_seq *, tree, location_t, bool);
2206 static bool cp_parser_friend_p
2207 (const cp_decl_specifier_seq *);
2208 static void cp_parser_required_error
2209 (cp_parser *, required_token, bool);
2210 static cp_token *cp_parser_require
2211 (cp_parser *, enum cpp_ttype, required_token);
2212 static cp_token *cp_parser_require_keyword
2213 (cp_parser *, enum rid, required_token);
2214 static bool cp_parser_token_starts_function_definition_p
2216 static bool cp_parser_next_token_starts_class_definition_p
2218 static bool cp_parser_next_token_ends_template_argument_p
2220 static bool cp_parser_nth_token_starts_template_argument_list_p
2221 (cp_parser *, size_t);
2222 static enum tag_types cp_parser_token_is_class_key
2224 static void cp_parser_check_class_key
2225 (enum tag_types, tree type);
2226 static void cp_parser_check_access_in_redeclaration
2227 (tree type, location_t location);
2228 static bool cp_parser_optional_template_keyword
2230 static void cp_parser_pre_parsed_nested_name_specifier
2232 static bool cp_parser_cache_group
2233 (cp_parser *, enum cpp_ttype, unsigned);
2234 static void cp_parser_parse_tentatively
2236 static void cp_parser_commit_to_tentative_parse
2238 static void cp_parser_abort_tentative_parse
2240 static bool cp_parser_parse_definitely
2242 static inline bool cp_parser_parsing_tentatively
2244 static bool cp_parser_uncommitted_to_tentative_parse_p
2246 static void cp_parser_error
2247 (cp_parser *, const char *);
2248 static void cp_parser_name_lookup_error
2249 (cp_parser *, tree, tree, name_lookup_error, location_t);
2250 static bool cp_parser_simulate_error
2252 static bool cp_parser_check_type_definition
2254 static void cp_parser_check_for_definition_in_return_type
2255 (cp_declarator *, tree, location_t type_location);
2256 static void cp_parser_check_for_invalid_template_id
2257 (cp_parser *, tree, location_t location);
2258 static bool cp_parser_non_integral_constant_expression
2259 (cp_parser *, non_integral_constant);
2260 static void cp_parser_diagnose_invalid_type_name
2261 (cp_parser *, tree, tree, location_t);
2262 static bool cp_parser_parse_and_diagnose_invalid_type_name
2264 static int cp_parser_skip_to_closing_parenthesis
2265 (cp_parser *, bool, bool, bool);
2266 static void cp_parser_skip_to_end_of_statement
2268 static void cp_parser_consume_semicolon_at_end_of_statement
2270 static void cp_parser_skip_to_end_of_block_or_statement
2272 static bool cp_parser_skip_to_closing_brace
2274 static void cp_parser_skip_to_end_of_template_parameter_list
2276 static void cp_parser_skip_to_pragma_eol
2277 (cp_parser*, cp_token *);
2278 static bool cp_parser_error_occurred
2280 static bool cp_parser_allow_gnu_extensions_p
2282 static bool cp_parser_is_pure_string_literal
2284 static bool cp_parser_is_string_literal
2286 static bool cp_parser_is_keyword
2287 (cp_token *, enum rid);
2288 static tree cp_parser_make_typename_type
2289 (cp_parser *, tree, tree, location_t location);
2290 static cp_declarator * cp_parser_make_indirect_declarator
2291 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2293 /* Returns nonzero if we are parsing tentatively. */
2296 cp_parser_parsing_tentatively (cp_parser* parser)
2298 return parser->context->next != NULL;
2301 /* Returns nonzero if TOKEN is a string literal. */
2304 cp_parser_is_pure_string_literal (cp_token* token)
2306 return (token->type == CPP_STRING ||
2307 token->type == CPP_STRING16 ||
2308 token->type == CPP_STRING32 ||
2309 token->type == CPP_WSTRING ||
2310 token->type == CPP_UTF8STRING);
2313 /* Returns nonzero if TOKEN is a string literal
2314 of a user-defined string literal. */
2317 cp_parser_is_string_literal (cp_token* token)
2319 return (cp_parser_is_pure_string_literal (token) ||
2320 token->type == CPP_STRING_USERDEF ||
2321 token->type == CPP_STRING16_USERDEF ||
2322 token->type == CPP_STRING32_USERDEF ||
2323 token->type == CPP_WSTRING_USERDEF ||
2324 token->type == CPP_UTF8STRING_USERDEF);
2327 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2330 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2332 return token->keyword == keyword;
2335 /* If not parsing tentatively, issue a diagnostic of the form
2336 FILE:LINE: MESSAGE before TOKEN
2337 where TOKEN is the next token in the input stream. MESSAGE
2338 (specified by the caller) is usually of the form "expected
2342 cp_parser_error (cp_parser* parser, const char* gmsgid)
2344 if (!cp_parser_simulate_error (parser))
2346 cp_token *token = cp_lexer_peek_token (parser->lexer);
2347 /* This diagnostic makes more sense if it is tagged to the line
2348 of the token we just peeked at. */
2349 cp_lexer_set_source_position_from_token (token);
2351 if (token->type == CPP_PRAGMA)
2353 error_at (token->location,
2354 "%<#pragma%> is not allowed here");
2355 cp_parser_skip_to_pragma_eol (parser, token);
2359 c_parse_error (gmsgid,
2360 /* Because c_parser_error does not understand
2361 CPP_KEYWORD, keywords are treated like
2363 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2364 token->u.value, token->flags);
2368 /* Issue an error about name-lookup failing. NAME is the
2369 IDENTIFIER_NODE DECL is the result of
2370 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2371 the thing that we hoped to find. */
2374 cp_parser_name_lookup_error (cp_parser* parser,
2377 name_lookup_error desired,
2378 location_t location)
2380 /* If name lookup completely failed, tell the user that NAME was not
2382 if (decl == error_mark_node)
2384 if (parser->scope && parser->scope != global_namespace)
2385 error_at (location, "%<%E::%E%> has not been declared",
2386 parser->scope, name);
2387 else if (parser->scope == global_namespace)
2388 error_at (location, "%<::%E%> has not been declared", name);
2389 else if (parser->object_scope
2390 && !CLASS_TYPE_P (parser->object_scope))
2391 error_at (location, "request for member %qE in non-class type %qT",
2392 name, parser->object_scope);
2393 else if (parser->object_scope)
2394 error_at (location, "%<%T::%E%> has not been declared",
2395 parser->object_scope, name);
2397 error_at (location, "%qE has not been declared", name);
2399 else if (parser->scope && parser->scope != global_namespace)
2404 error_at (location, "%<%E::%E%> is not a type",
2405 parser->scope, name);
2408 error_at (location, "%<%E::%E%> is not a class or namespace",
2409 parser->scope, name);
2413 "%<%E::%E%> is not a class, namespace, or enumeration",
2414 parser->scope, name);
2421 else if (parser->scope == global_namespace)
2426 error_at (location, "%<::%E%> is not a type", name);
2429 error_at (location, "%<::%E%> is not a class or namespace", name);
2433 "%<::%E%> is not a class, namespace, or enumeration",
2445 error_at (location, "%qE is not a type", name);
2448 error_at (location, "%qE is not a class or namespace", name);
2452 "%qE is not a class, namespace, or enumeration", name);
2460 /* If we are parsing tentatively, remember that an error has occurred
2461 during this tentative parse. Returns true if the error was
2462 simulated; false if a message should be issued by the caller. */
2465 cp_parser_simulate_error (cp_parser* parser)
2467 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2469 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2475 /* Check for repeated decl-specifiers. */
2478 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2479 location_t location)
2483 for (ds = ds_first; ds != ds_last; ++ds)
2485 unsigned count = decl_specs->specs[ds];
2488 /* The "long" specifier is a special case because of "long long". */
2492 error_at (location, "%<long long long%> is too long for GCC");
2494 pedwarn_cxx98 (location, OPT_Wlong_long,
2495 "ISO C++ 1998 does not support %<long long%>");
2499 static const char *const decl_spec_names[] = {
2516 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2521 /* This function is called when a type is defined. If type
2522 definitions are forbidden at this point, an error message is
2526 cp_parser_check_type_definition (cp_parser* parser)
2528 /* If types are forbidden here, issue a message. */
2529 if (parser->type_definition_forbidden_message)
2531 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2532 in the message need to be interpreted. */
2533 error (parser->type_definition_forbidden_message);
2539 /* This function is called when the DECLARATOR is processed. The TYPE
2540 was a type defined in the decl-specifiers. If it is invalid to
2541 define a type in the decl-specifiers for DECLARATOR, an error is
2542 issued. TYPE_LOCATION is the location of TYPE and is used
2543 for error reporting. */
2546 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2547 tree type, location_t type_location)
2549 /* [dcl.fct] forbids type definitions in return types.
2550 Unfortunately, it's not easy to know whether or not we are
2551 processing a return type until after the fact. */
2553 && (declarator->kind == cdk_pointer
2554 || declarator->kind == cdk_reference
2555 || declarator->kind == cdk_ptrmem))
2556 declarator = declarator->declarator;
2558 && declarator->kind == cdk_function)
2560 error_at (type_location,
2561 "new types may not be defined in a return type");
2562 inform (type_location,
2563 "(perhaps a semicolon is missing after the definition of %qT)",
2568 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2569 "<" in any valid C++ program. If the next token is indeed "<",
2570 issue a message warning the user about what appears to be an
2571 invalid attempt to form a template-id. LOCATION is the location
2572 of the type-specifier (TYPE) */
2575 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2576 tree type, location_t location)
2578 cp_token_position start = 0;
2580 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2583 error_at (location, "%qT is not a template", type);
2584 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2585 error_at (location, "%qE is not a template", type);
2587 error_at (location, "invalid template-id");
2588 /* Remember the location of the invalid "<". */
2589 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2590 start = cp_lexer_token_position (parser->lexer, true);
2591 /* Consume the "<". */
2592 cp_lexer_consume_token (parser->lexer);
2593 /* Parse the template arguments. */
2594 cp_parser_enclosed_template_argument_list (parser);
2595 /* Permanently remove the invalid template arguments so that
2596 this error message is not issued again. */
2598 cp_lexer_purge_tokens_after (parser->lexer, start);
2602 /* If parsing an integral constant-expression, issue an error message
2603 about the fact that THING appeared and return true. Otherwise,
2604 return false. In either case, set
2605 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2608 cp_parser_non_integral_constant_expression (cp_parser *parser,
2609 non_integral_constant thing)
2611 parser->non_integral_constant_expression_p = true;
2612 if (parser->integral_constant_expression_p)
2614 if (!parser->allow_non_integral_constant_expression_p)
2616 const char *msg = NULL;
2620 error ("floating-point literal "
2621 "cannot appear in a constant-expression");
2624 error ("a cast to a type other than an integral or "
2625 "enumeration type cannot appear in a "
2626 "constant-expression");
2629 error ("%<typeid%> operator "
2630 "cannot appear in a constant-expression");
2633 error ("non-constant compound literals "
2634 "cannot appear in a constant-expression");
2637 error ("a function call "
2638 "cannot appear in a constant-expression");
2641 error ("an increment "
2642 "cannot appear in a constant-expression");
2645 error ("an decrement "
2646 "cannot appear in a constant-expression");
2649 error ("an array reference "
2650 "cannot appear in a constant-expression");
2652 case NIC_ADDR_LABEL:
2653 error ("the address of a label "
2654 "cannot appear in a constant-expression");
2656 case NIC_OVERLOADED:
2657 error ("calls to overloaded operators "
2658 "cannot appear in a constant-expression");
2660 case NIC_ASSIGNMENT:
2661 error ("an assignment cannot appear in a constant-expression");
2664 error ("a comma operator "
2665 "cannot appear in a constant-expression");
2667 case NIC_CONSTRUCTOR:
2668 error ("a call to a constructor "
2669 "cannot appear in a constant-expression");
2675 msg = "__FUNCTION__";
2677 case NIC_PRETTY_FUNC:
2678 msg = "__PRETTY_FUNCTION__";
2698 case NIC_PREINCREMENT:
2701 case NIC_PREDECREMENT:
2714 error ("%qs cannot appear in a constant-expression", msg);
2721 /* Emit a diagnostic for an invalid type name. SCOPE is the
2722 qualifying scope (or NULL, if none) for ID. This function commits
2723 to the current active tentative parse, if any. (Otherwise, the
2724 problematic construct might be encountered again later, resulting
2725 in duplicate error messages.) LOCATION is the location of ID. */
2728 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2729 tree scope, tree id,
2730 location_t location)
2732 tree decl, old_scope;
2733 cp_parser_commit_to_tentative_parse (parser);
2734 /* Try to lookup the identifier. */
2735 old_scope = parser->scope;
2736 parser->scope = scope;
2737 decl = cp_parser_lookup_name_simple (parser, id, location);
2738 parser->scope = old_scope;
2739 /* If the lookup found a template-name, it means that the user forgot
2740 to specify an argument list. Emit a useful error message. */
2741 if (TREE_CODE (decl) == TEMPLATE_DECL)
2743 "invalid use of template-name %qE without an argument list",
2745 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2746 error_at (location, "invalid use of destructor %qD as a type", id);
2747 else if (TREE_CODE (decl) == TYPE_DECL)
2748 /* Something like 'unsigned A a;' */
2749 error_at (location, "invalid combination of multiple type-specifiers");
2750 else if (!parser->scope)
2752 /* Issue an error message. */
2753 error_at (location, "%qE does not name a type", id);
2754 /* If we're in a template class, it's possible that the user was
2755 referring to a type from a base class. For example:
2757 template <typename T> struct A { typedef T X; };
2758 template <typename T> struct B : public A<T> { X x; };
2760 The user should have said "typename A<T>::X". */
2761 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2762 inform (location, "C++0x %<constexpr%> only available with "
2763 "-std=c++0x or -std=gnu++0x");
2764 else if (processing_template_decl && current_class_type
2765 && TYPE_BINFO (current_class_type))
2769 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2773 tree base_type = BINFO_TYPE (b);
2774 if (CLASS_TYPE_P (base_type)
2775 && dependent_type_p (base_type))
2778 /* Go from a particular instantiation of the
2779 template (which will have an empty TYPE_FIELDs),
2780 to the main version. */
2781 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2782 for (field = TYPE_FIELDS (base_type);
2784 field = DECL_CHAIN (field))
2785 if (TREE_CODE (field) == TYPE_DECL
2786 && DECL_NAME (field) == id)
2789 "(perhaps %<typename %T::%E%> was intended)",
2790 BINFO_TYPE (b), id);
2799 /* Here we diagnose qualified-ids where the scope is actually correct,
2800 but the identifier does not resolve to a valid type name. */
2801 else if (parser->scope != error_mark_node)
2803 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2804 error_at (location, "%qE in namespace %qE does not name a type",
2806 else if (CLASS_TYPE_P (parser->scope)
2807 && constructor_name_p (id, parser->scope))
2810 error_at (location, "%<%T::%E%> names the constructor, not"
2811 " the type", parser->scope, id);
2812 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2813 error_at (location, "and %qT has no template constructors",
2816 else if (TYPE_P (parser->scope)
2817 && dependent_scope_p (parser->scope))
2818 error_at (location, "need %<typename%> before %<%T::%E%> because "
2819 "%qT is a dependent scope",
2820 parser->scope, id, parser->scope);
2821 else if (TYPE_P (parser->scope))
2822 error_at (location, "%qE in %q#T does not name a type",
2829 /* Check for a common situation where a type-name should be present,
2830 but is not, and issue a sensible error message. Returns true if an
2831 invalid type-name was detected.
2833 The situation handled by this function are variable declarations of the
2834 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2835 Usually, `ID' should name a type, but if we got here it means that it
2836 does not. We try to emit the best possible error message depending on
2837 how exactly the id-expression looks like. */
2840 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2843 cp_token *token = cp_lexer_peek_token (parser->lexer);
2845 /* Avoid duplicate error about ambiguous lookup. */
2846 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2848 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2849 if (next->type == CPP_NAME && next->ambiguous_p)
2853 cp_parser_parse_tentatively (parser);
2854 id = cp_parser_id_expression (parser,
2855 /*template_keyword_p=*/false,
2856 /*check_dependency_p=*/true,
2857 /*template_p=*/NULL,
2858 /*declarator_p=*/true,
2859 /*optional_p=*/false);
2860 /* If the next token is a (, this is a function with no explicit return
2861 type, i.e. constructor, destructor or conversion op. */
2862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2863 || TREE_CODE (id) == TYPE_DECL)
2865 cp_parser_abort_tentative_parse (parser);
2868 if (!cp_parser_parse_definitely (parser))
2871 /* Emit a diagnostic for the invalid type. */
2872 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2873 id, token->location);
2875 /* If we aren't in the middle of a declarator (i.e. in a
2876 parameter-declaration-clause), skip to the end of the declaration;
2877 there's no point in trying to process it. */
2878 if (!parser->in_declarator_p)
2879 cp_parser_skip_to_end_of_block_or_statement (parser);
2883 /* Consume tokens up to, and including, the next non-nested closing `)'.
2884 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2885 are doing error recovery. Returns -1 if OR_COMMA is true and we
2886 found an unnested comma. */
2889 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2894 unsigned paren_depth = 0;
2895 unsigned brace_depth = 0;
2896 unsigned square_depth = 0;
2898 if (recovering && !or_comma
2899 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2904 cp_token * token = cp_lexer_peek_token (parser->lexer);
2906 switch (token->type)
2909 case CPP_PRAGMA_EOL:
2910 /* If we've run out of tokens, then there is no closing `)'. */
2913 /* This is good for lambda expression capture-lists. */
2914 case CPP_OPEN_SQUARE:
2917 case CPP_CLOSE_SQUARE:
2918 if (!square_depth--)
2923 /* This matches the processing in skip_to_end_of_statement. */
2928 case CPP_OPEN_BRACE:
2931 case CPP_CLOSE_BRACE:
2937 if (recovering && or_comma && !brace_depth && !paren_depth
2942 case CPP_OPEN_PAREN:
2947 case CPP_CLOSE_PAREN:
2948 if (!brace_depth && !paren_depth--)
2951 cp_lexer_consume_token (parser->lexer);
2960 /* Consume the token. */
2961 cp_lexer_consume_token (parser->lexer);
2965 /* Consume tokens until we reach the end of the current statement.
2966 Normally, that will be just before consuming a `;'. However, if a
2967 non-nested `}' comes first, then we stop before consuming that. */
2970 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2972 unsigned nesting_depth = 0;
2976 cp_token *token = cp_lexer_peek_token (parser->lexer);
2978 switch (token->type)
2981 case CPP_PRAGMA_EOL:
2982 /* If we've run out of tokens, stop. */
2986 /* If the next token is a `;', we have reached the end of the
2992 case CPP_CLOSE_BRACE:
2993 /* If this is a non-nested '}', stop before consuming it.
2994 That way, when confronted with something like:
2998 we stop before consuming the closing '}', even though we
2999 have not yet reached a `;'. */
3000 if (nesting_depth == 0)
3003 /* If it is the closing '}' for a block that we have
3004 scanned, stop -- but only after consuming the token.
3010 we will stop after the body of the erroneously declared
3011 function, but before consuming the following `typedef'
3013 if (--nesting_depth == 0)
3015 cp_lexer_consume_token (parser->lexer);
3019 case CPP_OPEN_BRACE:
3027 /* Consume the token. */
3028 cp_lexer_consume_token (parser->lexer);
3032 /* This function is called at the end of a statement or declaration.
3033 If the next token is a semicolon, it is consumed; otherwise, error
3034 recovery is attempted. */
3037 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3039 /* Look for the trailing `;'. */
3040 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3042 /* If there is additional (erroneous) input, skip to the end of
3044 cp_parser_skip_to_end_of_statement (parser);
3045 /* If the next token is now a `;', consume it. */
3046 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3047 cp_lexer_consume_token (parser->lexer);
3051 /* Skip tokens until we have consumed an entire block, or until we
3052 have consumed a non-nested `;'. */
3055 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3057 int nesting_depth = 0;
3059 while (nesting_depth >= 0)
3061 cp_token *token = cp_lexer_peek_token (parser->lexer);
3063 switch (token->type)
3066 case CPP_PRAGMA_EOL:
3067 /* If we've run out of tokens, stop. */
3071 /* Stop if this is an unnested ';'. */
3076 case CPP_CLOSE_BRACE:
3077 /* Stop if this is an unnested '}', or closes the outermost
3080 if (nesting_depth < 0)
3086 case CPP_OPEN_BRACE:
3095 /* Consume the token. */
3096 cp_lexer_consume_token (parser->lexer);
3100 /* Skip tokens until a non-nested closing curly brace is the next
3101 token, or there are no more tokens. Return true in the first case,
3105 cp_parser_skip_to_closing_brace (cp_parser *parser)
3107 unsigned nesting_depth = 0;
3111 cp_token *token = cp_lexer_peek_token (parser->lexer);
3113 switch (token->type)
3116 case CPP_PRAGMA_EOL:
3117 /* If we've run out of tokens, stop. */
3120 case CPP_CLOSE_BRACE:
3121 /* If the next token is a non-nested `}', then we have reached
3122 the end of the current block. */
3123 if (nesting_depth-- == 0)
3127 case CPP_OPEN_BRACE:
3128 /* If it the next token is a `{', then we are entering a new
3129 block. Consume the entire block. */
3137 /* Consume the token. */
3138 cp_lexer_consume_token (parser->lexer);
3142 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3143 parameter is the PRAGMA token, allowing us to purge the entire pragma
3147 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3151 parser->lexer->in_pragma = false;
3154 token = cp_lexer_consume_token (parser->lexer);
3155 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3157 /* Ensure that the pragma is not parsed again. */
3158 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3161 /* Require pragma end of line, resyncing with it as necessary. The
3162 arguments are as for cp_parser_skip_to_pragma_eol. */
3165 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3167 parser->lexer->in_pragma = false;
3168 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3169 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3172 /* This is a simple wrapper around make_typename_type. When the id is
3173 an unresolved identifier node, we can provide a superior diagnostic
3174 using cp_parser_diagnose_invalid_type_name. */
3177 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3178 tree id, location_t id_location)
3181 if (TREE_CODE (id) == IDENTIFIER_NODE)
3183 result = make_typename_type (scope, id, typename_type,
3184 /*complain=*/tf_none);
3185 if (result == error_mark_node)
3186 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3189 return make_typename_type (scope, id, typename_type, tf_error);
3192 /* This is a wrapper around the
3193 make_{pointer,ptrmem,reference}_declarator functions that decides
3194 which one to call based on the CODE and CLASS_TYPE arguments. The
3195 CODE argument should be one of the values returned by
3196 cp_parser_ptr_operator. */
3197 static cp_declarator *
3198 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3199 cp_cv_quals cv_qualifiers,
3200 cp_declarator *target)
3202 if (code == ERROR_MARK)
3203 return cp_error_declarator;
3205 if (code == INDIRECT_REF)
3206 if (class_type == NULL_TREE)
3207 return make_pointer_declarator (cv_qualifiers, target);
3209 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3210 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3211 return make_reference_declarator (cv_qualifiers, target, false);
3212 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3213 return make_reference_declarator (cv_qualifiers, target, true);
3217 /* Create a new C++ parser. */
3220 cp_parser_new (void)
3226 /* cp_lexer_new_main is called before doing GC allocation because
3227 cp_lexer_new_main might load a PCH file. */
3228 lexer = cp_lexer_new_main ();
3230 /* Initialize the binops_by_token so that we can get the tree
3231 directly from the token. */
3232 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3233 binops_by_token[binops[i].token_type] = binops[i];
3235 parser = ggc_alloc_cleared_cp_parser ();
3236 parser->lexer = lexer;
3237 parser->context = cp_parser_context_new (NULL);
3239 /* For now, we always accept GNU extensions. */
3240 parser->allow_gnu_extensions_p = 1;
3242 /* The `>' token is a greater-than operator, not the end of a
3244 parser->greater_than_is_operator_p = true;
3246 parser->default_arg_ok_p = true;
3248 /* We are not parsing a constant-expression. */
3249 parser->integral_constant_expression_p = false;
3250 parser->allow_non_integral_constant_expression_p = false;
3251 parser->non_integral_constant_expression_p = false;
3253 /* Local variable names are not forbidden. */
3254 parser->local_variables_forbidden_p = false;
3256 /* We are not processing an `extern "C"' declaration. */
3257 parser->in_unbraced_linkage_specification_p = false;
3259 /* We are not processing a declarator. */
3260 parser->in_declarator_p = false;
3262 /* We are not processing a template-argument-list. */
3263 parser->in_template_argument_list_p = false;
3265 /* We are not in an iteration statement. */
3266 parser->in_statement = 0;
3268 /* We are not in a switch statement. */
3269 parser->in_switch_statement_p = false;
3271 /* We are not parsing a type-id inside an expression. */
3272 parser->in_type_id_in_expr_p = false;
3274 /* Declarations aren't implicitly extern "C". */
3275 parser->implicit_extern_c = false;
3277 /* String literals should be translated to the execution character set. */
3278 parser->translate_strings_p = true;
3280 /* We are not parsing a function body. */
3281 parser->in_function_body = false;
3283 /* We can correct until told otherwise. */
3284 parser->colon_corrects_to_scope_p = true;
3286 /* The unparsed function queue is empty. */
3287 push_unparsed_function_queues (parser);
3289 /* There are no classes being defined. */
3290 parser->num_classes_being_defined = 0;
3292 /* No template parameters apply. */
3293 parser->num_template_parameter_lists = 0;
3298 /* Create a cp_lexer structure which will emit the tokens in CACHE
3299 and push it onto the parser's lexer stack. This is used for delayed
3300 parsing of in-class method bodies and default arguments, and should
3301 not be confused with tentative parsing. */
3303 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3305 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3306 lexer->next = parser->lexer;
3307 parser->lexer = lexer;
3309 /* Move the current source position to that of the first token in the
3311 cp_lexer_set_source_position_from_token (lexer->next_token);
3314 /* Pop the top lexer off the parser stack. This is never used for the
3315 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3317 cp_parser_pop_lexer (cp_parser *parser)
3319 cp_lexer *lexer = parser->lexer;
3320 parser->lexer = lexer->next;
3321 cp_lexer_destroy (lexer);
3323 /* Put the current source position back where it was before this
3324 lexer was pushed. */
3325 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3328 /* Lexical conventions [gram.lex] */
3330 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3334 cp_parser_identifier (cp_parser* parser)
3338 /* Look for the identifier. */
3339 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3340 /* Return the value. */
3341 return token ? token->u.value : error_mark_node;
3344 /* Parse a sequence of adjacent string constants. Returns a
3345 TREE_STRING representing the combined, nul-terminated string
3346 constant. If TRANSLATE is true, translate the string to the
3347 execution character set. If WIDE_OK is true, a wide string is
3350 C++98 [lex.string] says that if a narrow string literal token is
3351 adjacent to a wide string literal token, the behavior is undefined.
3352 However, C99 6.4.5p4 says that this results in a wide string literal.
3353 We follow C99 here, for consistency with the C front end.
3355 This code is largely lifted from lex_string() in c-lex.c.
3357 FUTURE: ObjC++ will need to handle @-strings here. */
3359 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3363 struct obstack str_ob;
3364 cpp_string str, istr, *strs;
3366 enum cpp_ttype type, curr_type;
3367 int have_suffix_p = 0;
3369 tree suffix_id = NULL_TREE;
3370 bool curr_tok_is_userdef_p = false;
3372 tok = cp_lexer_peek_token (parser->lexer);
3373 if (!cp_parser_is_string_literal (tok))
3375 cp_parser_error (parser, "expected string-literal");
3376 return error_mark_node;
3379 if (cpp_userdef_string_p (tok->type))
3381 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3382 curr_type = cpp_userdef_string_remove_type (tok->type);
3383 curr_tok_is_userdef_p = true;
3387 string_tree = tok->u.value;
3388 curr_type = tok->type;
3392 /* Try to avoid the overhead of creating and destroying an obstack
3393 for the common case of just one string. */
3394 if (!cp_parser_is_string_literal
3395 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3397 cp_lexer_consume_token (parser->lexer);
3399 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3400 str.len = TREE_STRING_LENGTH (string_tree);
3403 if (curr_tok_is_userdef_p)
3405 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3407 curr_type = cpp_userdef_string_remove_type (tok->type);
3410 curr_type = tok->type;
3416 gcc_obstack_init (&str_ob);
3421 cp_lexer_consume_token (parser->lexer);
3423 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3424 str.len = TREE_STRING_LENGTH (string_tree);
3426 if (curr_tok_is_userdef_p)
3428 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3429 if (have_suffix_p == 0)
3431 suffix_id = curr_suffix_id;
3434 else if (have_suffix_p == 1
3435 && curr_suffix_id != suffix_id)
3437 error ("inconsistent user-defined literal suffixes"
3438 " %qD and %qD in string literal",
3439 suffix_id, curr_suffix_id);
3442 curr_type = cpp_userdef_string_remove_type (tok->type);
3445 curr_type = tok->type;
3447 if (type != curr_type)
3449 if (type == CPP_STRING)
3451 else if (curr_type != CPP_STRING)
3452 error_at (tok->location,
3453 "unsupported non-standard concatenation "
3454 "of string literals");
3457 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3459 tok = cp_lexer_peek_token (parser->lexer);
3460 if (cpp_userdef_string_p (tok->type))
3462 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3463 curr_type = cpp_userdef_string_remove_type (tok->type);
3464 curr_tok_is_userdef_p = true;
3468 string_tree = tok->u.value;
3469 curr_type = tok->type;
3470 curr_tok_is_userdef_p = false;
3473 while (cp_parser_is_string_literal (tok));
3475 strs = (cpp_string *) obstack_finish (&str_ob);
3478 if (type != CPP_STRING && !wide_ok)
3480 cp_parser_error (parser, "a wide string is invalid in this context");
3484 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3485 (parse_in, strs, count, &istr, type))
3487 value = build_string (istr.len, (const char *)istr.text);
3488 free (CONST_CAST (unsigned char *, istr.text));
3494 case CPP_UTF8STRING:
3495 TREE_TYPE (value) = char_array_type_node;
3498 TREE_TYPE (value) = char16_array_type_node;
3501 TREE_TYPE (value) = char32_array_type_node;
3504 TREE_TYPE (value) = wchar_array_type_node;
3508 value = fix_string_type (value);
3512 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3513 tok->u.value = literal;
3514 return cp_parser_userdef_string_literal (tok);
3518 /* cpp_interpret_string has issued an error. */
3519 value = error_mark_node;
3522 obstack_free (&str_ob, 0);
3527 /* Parse a user-defined char constant. Returns a call to a user-defined
3528 literal operator taking the character as an argument. */
3531 cp_parser_userdef_char_literal (cp_parser *parser)
3533 cp_token *token = NULL;
3534 tree literal, suffix_id, value;
3539 token = cp_lexer_consume_token (parser->lexer);
3540 literal = token->u.value;
3541 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3542 value = USERDEF_LITERAL_VALUE (literal);
3543 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3545 /* Build up a call to the user-defined operator */
3546 /* Lookup the name we got back from the id-expression. */
3547 vec = make_tree_vector ();
3548 VEC_safe_push (tree, gc, vec, value);
3549 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3550 if (!decl || decl == error_mark_node)
3552 error ("unable to find user-defined character literal operator %qD",
3554 release_tree_vector (vec);
3555 return error_mark_node;
3557 result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3558 release_tree_vector (vec);
3563 /* A subroutine of cp_parser_userdef_numeric_literal to
3564 create a char... template parameter pack from a string node. */
3567 make_char_string_pack (tree value)
3570 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3571 const char *str = TREE_STRING_POINTER (value);
3572 int i, len = TREE_STRING_LENGTH (value) - 1;
3573 tree argvec = make_tree_vec (1);
3575 /* Fill in CHARVEC with all of the parameters. */
3576 charvec = make_tree_vec (len);
3577 for (i = 0; i < len; ++i)
3578 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3580 /* Build the argument packs. */
3581 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3582 TREE_TYPE (argpack) = char_type_node;
3584 TREE_VEC_ELT (argvec, 0) = argpack;
3589 /* Parse a user-defined numeric constant. returns a call to a user-defined
3590 literal operator. */
3593 cp_parser_userdef_numeric_literal (cp_parser *parser)
3595 cp_token *token = NULL;
3596 tree literal, suffix_id, value, num_string;
3598 tree result = error_mark_node;
3601 token = cp_lexer_consume_token (parser->lexer);
3602 literal = token->u.value;
3603 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3604 value = USERDEF_LITERAL_VALUE (literal);
3605 num_string = USERDEF_LITERAL_NUM_STRING (literal);
3606 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3608 /* Build up a call to the user-defined operator */
3609 /* Lookup the name we got back from the id-expression. */
3610 /* Try to find the literal operator by finishing the call expression
3611 with the numeric argument. */
3612 args = make_tree_vector ();
3613 VEC_safe_push (tree, gc, args, value);
3614 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3615 if (decl && decl != error_mark_node)
3617 result = finish_call_expr (decl, &args, false, true, tf_none);
3618 if (result != error_mark_node)
3620 release_tree_vector (args);
3624 release_tree_vector (args);
3626 /* If the numeric argument didn't work, look for a raw literal
3627 operator taking a const char* argument consisting of the number
3628 in string format. */
3629 args = make_tree_vector ();
3630 VEC_safe_push (tree, gc, args, num_string);
3631 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3632 if (decl && decl != error_mark_node)
3634 result = finish_call_expr (decl, &args, false, true, tf_none);
3635 if (result != error_mark_node)
3637 release_tree_vector (args);
3641 release_tree_vector (args);
3643 /* If the raw literal didn't work, look for a non-type template
3644 function with parameter pack char.... Call the function with
3645 template parameter characters representing the number. */
3646 args = make_tree_vector ();
3647 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3648 if (decl && decl != error_mark_node)
3650 tree tmpl_args = make_char_string_pack (num_string);
3651 decl = lookup_template_function (decl, tmpl_args);
3652 result = finish_call_expr (decl, &args, false, true, tf_none);
3653 if (result != error_mark_node)
3655 release_tree_vector (args);
3659 release_tree_vector (args);
3661 if (result == error_mark_node)
3662 error ("unable to find user-defined numeric literal operator %qD", name);
3667 /* Parse a user-defined string constant. Returns a call to a user-defined
3668 literal operator taking a character pointer and the length of the string
3671 cp_parser_userdef_string_literal (cp_token *token)
3673 tree literal, suffix_id, value;
3679 literal = token->u.value;
3680 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3681 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3682 value = USERDEF_LITERAL_VALUE (literal);
3683 len = TREE_STRING_LENGTH (value) - 1;
3685 /* Build up a call to the user-defined operator */
3686 /* Lookup the name we got back from the id-expression. */
3687 vec = make_tree_vector ();
3688 VEC_safe_push (tree, gc, vec, value);
3689 VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3690 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3691 if (!decl || decl == error_mark_node)
3693 error ("unable to find user-defined string literal operator %qD", name);
3694 release_tree_vector (vec);
3695 return error_mark_node;
3697 result = finish_call_expr (decl, &vec, false, true, tf_none);
3698 if (result == error_mark_node)
3699 error ("unable to find valid user-defined string literal operator %qD."
3700 " Possible missing length argument in string literal operator.",
3702 release_tree_vector (vec);
3708 /* Basic concepts [gram.basic] */
3710 /* Parse a translation-unit.
3713 declaration-seq [opt]
3715 Returns TRUE if all went well. */
3718 cp_parser_translation_unit (cp_parser* parser)
3720 /* The address of the first non-permanent object on the declarator
3722 static void *declarator_obstack_base;
3726 /* Create the declarator obstack, if necessary. */
3727 if (!cp_error_declarator)
3729 gcc_obstack_init (&declarator_obstack);
3730 /* Create the error declarator. */
3731 cp_error_declarator = make_declarator (cdk_error);
3732 /* Create the empty parameter list. */
3733 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3734 /* Remember where the base of the declarator obstack lies. */
3735 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3738 cp_parser_declaration_seq_opt (parser);
3740 /* If there are no tokens left then all went well. */
3741 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3743 /* Get rid of the token array; we don't need it any more. */
3744 cp_lexer_destroy (parser->lexer);
3745 parser->lexer = NULL;
3747 /* This file might have been a context that's implicitly extern
3748 "C". If so, pop the lang context. (Only relevant for PCH.) */
3749 if (parser->implicit_extern_c)
3751 pop_lang_context ();
3752 parser->implicit_extern_c = false;
3756 finish_translation_unit ();
3762 cp_parser_error (parser, "expected declaration");
3766 /* Make sure the declarator obstack was fully cleaned up. */
3767 gcc_assert (obstack_next_free (&declarator_obstack)
3768 == declarator_obstack_base);
3770 /* All went well. */
3774 /* Expressions [gram.expr] */
3776 /* Parse a primary-expression.
3787 ( compound-statement )
3788 __builtin_va_arg ( assignment-expression , type-id )
3789 __builtin_offsetof ( type-id , offsetof-expression )
3792 __has_nothrow_assign ( type-id )
3793 __has_nothrow_constructor ( type-id )
3794 __has_nothrow_copy ( type-id )
3795 __has_trivial_assign ( type-id )
3796 __has_trivial_constructor ( type-id )
3797 __has_trivial_copy ( type-id )
3798 __has_trivial_destructor ( type-id )
3799 __has_virtual_destructor ( type-id )
3800 __is_abstract ( type-id )
3801 __is_base_of ( type-id , type-id )
3802 __is_class ( type-id )
3803 __is_convertible_to ( type-id , type-id )
3804 __is_empty ( type-id )
3805 __is_enum ( type-id )
3806 __is_literal_type ( type-id )
3807 __is_pod ( type-id )
3808 __is_polymorphic ( type-id )
3809 __is_std_layout ( type-id )
3810 __is_trivial ( type-id )
3811 __is_union ( type-id )
3813 Objective-C++ Extension:
3821 ADDRESS_P is true iff this expression was immediately preceded by
3822 "&" and therefore might denote a pointer-to-member. CAST_P is true
3823 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3824 true iff this expression is a template argument.
3826 Returns a representation of the expression. Upon return, *IDK
3827 indicates what kind of id-expression (if any) was present. */
3830 cp_parser_primary_expression (cp_parser *parser,
3833 bool template_arg_p,
3836 cp_token *token = NULL;
3838 /* Assume the primary expression is not an id-expression. */
3839 *idk = CP_ID_KIND_NONE;
3841 /* Peek at the next token. */
3842 token = cp_lexer_peek_token (parser->lexer);
3843 switch (token->type)
3852 user-defined-literal */
3858 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3859 return cp_parser_userdef_numeric_literal (parser);
3860 token = cp_lexer_consume_token (parser->lexer);
3861 if (TREE_CODE (token->u.value) == FIXED_CST)
3863 error_at (token->location,
3864 "fixed-point types not supported in C++");
3865 return error_mark_node;
3867 /* Floating-point literals are only allowed in an integral
3868 constant expression if they are cast to an integral or
3869 enumeration type. */
3870 if (TREE_CODE (token->u.value) == REAL_CST
3871 && parser->integral_constant_expression_p
3874 /* CAST_P will be set even in invalid code like "int(2.7 +
3875 ...)". Therefore, we have to check that the next token
3876 is sure to end the cast. */
3879 cp_token *next_token;
3881 next_token = cp_lexer_peek_token (parser->lexer);
3882 if (/* The comma at the end of an
3883 enumerator-definition. */
3884 next_token->type != CPP_COMMA
3885 /* The curly brace at the end of an enum-specifier. */
3886 && next_token->type != CPP_CLOSE_BRACE
3887 /* The end of a statement. */
3888 && next_token->type != CPP_SEMICOLON
3889 /* The end of the cast-expression. */
3890 && next_token->type != CPP_CLOSE_PAREN
3891 /* The end of an array bound. */
3892 && next_token->type != CPP_CLOSE_SQUARE
3893 /* The closing ">" in a template-argument-list. */
3894 && (next_token->type != CPP_GREATER
3895 || parser->greater_than_is_operator_p)
3896 /* C++0x only: A ">>" treated like two ">" tokens,
3897 in a template-argument-list. */
3898 && (next_token->type != CPP_RSHIFT
3899 || (cxx_dialect == cxx98)
3900 || parser->greater_than_is_operator_p))
3904 /* If we are within a cast, then the constraint that the
3905 cast is to an integral or enumeration type will be
3906 checked at that point. If we are not within a cast, then
3907 this code is invalid. */
3909 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3911 return token->u.value;
3913 case CPP_CHAR_USERDEF:
3914 case CPP_CHAR16_USERDEF:
3915 case CPP_CHAR32_USERDEF:
3916 case CPP_WCHAR_USERDEF:
3917 return cp_parser_userdef_char_literal (parser);
3923 case CPP_UTF8STRING:
3924 case CPP_STRING_USERDEF:
3925 case CPP_STRING16_USERDEF:
3926 case CPP_STRING32_USERDEF:
3927 case CPP_WSTRING_USERDEF:
3928 case CPP_UTF8STRING_USERDEF:
3929 /* ??? Should wide strings be allowed when parser->translate_strings_p
3930 is false (i.e. in attributes)? If not, we can kill the third
3931 argument to cp_parser_string_literal. */
3932 return cp_parser_string_literal (parser,
3933 parser->translate_strings_p,
3936 case CPP_OPEN_PAREN:
3939 bool saved_greater_than_is_operator_p;
3941 /* Consume the `('. */
3942 cp_lexer_consume_token (parser->lexer);
3943 /* Within a parenthesized expression, a `>' token is always
3944 the greater-than operator. */
3945 saved_greater_than_is_operator_p
3946 = parser->greater_than_is_operator_p;
3947 parser->greater_than_is_operator_p = true;
3948 /* If we see `( { ' then we are looking at the beginning of
3949 a GNU statement-expression. */
3950 if (cp_parser_allow_gnu_extensions_p (parser)
3951 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3953 /* Statement-expressions are not allowed by the standard. */
3954 pedwarn (token->location, OPT_pedantic,
3955 "ISO C++ forbids braced-groups within expressions");
3957 /* And they're not allowed outside of a function-body; you
3958 cannot, for example, write:
3960 int i = ({ int j = 3; j + 1; });
3962 at class or namespace scope. */
3963 if (!parser->in_function_body
3964 || parser->in_template_argument_list_p)
3966 error_at (token->location,
3967 "statement-expressions are not allowed outside "
3968 "functions nor in template-argument lists");
3969 cp_parser_skip_to_end_of_block_or_statement (parser);
3970 expr = error_mark_node;
3974 /* Start the statement-expression. */
3975 expr = begin_stmt_expr ();
3976 /* Parse the compound-statement. */
3977 cp_parser_compound_statement (parser, expr, false, false);
3979 expr = finish_stmt_expr (expr, false);
3984 /* Parse the parenthesized expression. */
3985 expr = cp_parser_expression (parser, cast_p, idk);
3986 /* Let the front end know that this expression was
3987 enclosed in parentheses. This matters in case, for
3988 example, the expression is of the form `A::B', since
3989 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3991 finish_parenthesized_expr (expr);
3992 /* DR 705: Wrapping an unqualified name in parentheses
3993 suppresses arg-dependent lookup. We want to pass back
3994 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3995 (c++/37862), but none of the others. */
3996 if (*idk != CP_ID_KIND_QUALIFIED)
3997 *idk = CP_ID_KIND_NONE;
3999 /* The `>' token might be the end of a template-id or
4000 template-parameter-list now. */
4001 parser->greater_than_is_operator_p
4002 = saved_greater_than_is_operator_p;
4003 /* Consume the `)'. */
4004 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4005 cp_parser_skip_to_end_of_statement (parser);
4010 case CPP_OPEN_SQUARE:
4011 if (c_dialect_objc ())
4012 /* We have an Objective-C++ message. */
4013 return cp_parser_objc_expression (parser);
4015 tree lam = cp_parser_lambda_expression (parser);
4016 /* Don't warn about a failed tentative parse. */
4017 if (cp_parser_error_occurred (parser))
4018 return error_mark_node;
4019 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4023 case CPP_OBJC_STRING:
4024 if (c_dialect_objc ())
4025 /* We have an Objective-C++ string literal. */
4026 return cp_parser_objc_expression (parser);
4027 cp_parser_error (parser, "expected primary-expression");
4028 return error_mark_node;
4031 switch (token->keyword)
4033 /* These two are the boolean literals. */
4035 cp_lexer_consume_token (parser->lexer);
4036 return boolean_true_node;
4038 cp_lexer_consume_token (parser->lexer);
4039 return boolean_false_node;
4041 /* The `__null' literal. */
4043 cp_lexer_consume_token (parser->lexer);
4046 /* The `nullptr' literal. */
4048 cp_lexer_consume_token (parser->lexer);
4049 return nullptr_node;
4051 /* Recognize the `this' keyword. */
4053 cp_lexer_consume_token (parser->lexer);
4054 if (parser->local_variables_forbidden_p)
4056 error_at (token->location,
4057 "%<this%> may not be used in this context");
4058 return error_mark_node;
4060 /* Pointers cannot appear in constant-expressions. */
4061 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4062 return error_mark_node;
4063 return finish_this_expr ();
4065 /* The `operator' keyword can be the beginning of an
4070 case RID_FUNCTION_NAME:
4071 case RID_PRETTY_FUNCTION_NAME:
4072 case RID_C99_FUNCTION_NAME:
4074 non_integral_constant name;
4076 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4077 __func__ are the names of variables -- but they are
4078 treated specially. Therefore, they are handled here,
4079 rather than relying on the generic id-expression logic
4080 below. Grammatically, these names are id-expressions.
4082 Consume the token. */
4083 token = cp_lexer_consume_token (parser->lexer);
4085 switch (token->keyword)
4087 case RID_FUNCTION_NAME:
4088 name = NIC_FUNC_NAME;
4090 case RID_PRETTY_FUNCTION_NAME:
4091 name = NIC_PRETTY_FUNC;
4093 case RID_C99_FUNCTION_NAME:
4094 name = NIC_C99_FUNC;
4100 if (cp_parser_non_integral_constant_expression (parser, name))
4101 return error_mark_node;
4103 /* Look up the name. */
4104 return finish_fname (token->u.value);
4112 /* The `__builtin_va_arg' construct is used to handle
4113 `va_arg'. Consume the `__builtin_va_arg' token. */
4114 cp_lexer_consume_token (parser->lexer);
4115 /* Look for the opening `('. */
4116 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4117 /* Now, parse the assignment-expression. */
4118 expression = cp_parser_assignment_expression (parser,
4119 /*cast_p=*/false, NULL);
4120 /* Look for the `,'. */
4121 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4122 /* Parse the type-id. */
4123 type = cp_parser_type_id (parser);
4124 /* Look for the closing `)'. */
4125 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4126 /* Using `va_arg' in a constant-expression is not
4128 if (cp_parser_non_integral_constant_expression (parser,
4130 return error_mark_node;
4131 return build_x_va_arg (expression, type);
4135 return cp_parser_builtin_offsetof (parser);
4137 case RID_HAS_NOTHROW_ASSIGN:
4138 case RID_HAS_NOTHROW_CONSTRUCTOR:
4139 case RID_HAS_NOTHROW_COPY:
4140 case RID_HAS_TRIVIAL_ASSIGN:
4141 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4142 case RID_HAS_TRIVIAL_COPY:
4143 case RID_HAS_TRIVIAL_DESTRUCTOR:
4144 case RID_HAS_VIRTUAL_DESTRUCTOR:
4145 case RID_IS_ABSTRACT:
4146 case RID_IS_BASE_OF:
4148 case RID_IS_CONVERTIBLE_TO:
4151 case RID_IS_LITERAL_TYPE:
4153 case RID_IS_POLYMORPHIC:
4154 case RID_IS_STD_LAYOUT:
4155 case RID_IS_TRIVIAL:
4157 return cp_parser_trait_expr (parser, token->keyword);
4159 /* Objective-C++ expressions. */
4161 case RID_AT_PROTOCOL:
4162 case RID_AT_SELECTOR:
4163 return cp_parser_objc_expression (parser);
4166 if (parser->in_function_body
4167 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4170 error_at (token->location,
4171 "a template declaration cannot appear at block scope");
4172 cp_parser_skip_to_end_of_block_or_statement (parser);
4173 return error_mark_node;
4176 cp_parser_error (parser, "expected primary-expression");
4177 return error_mark_node;
4180 /* An id-expression can start with either an identifier, a
4181 `::' as the beginning of a qualified-id, or the "operator"
4185 case CPP_TEMPLATE_ID:
4186 case CPP_NESTED_NAME_SPECIFIER:
4190 const char *error_msg;
4193 cp_token *id_expr_token;
4196 /* Parse the id-expression. */
4198 = cp_parser_id_expression (parser,
4199 /*template_keyword_p=*/false,
4200 /*check_dependency_p=*/true,
4202 /*declarator_p=*/false,
4203 /*optional_p=*/false);
4204 if (id_expression == error_mark_node)
4205 return error_mark_node;
4206 id_expr_token = token;
4207 token = cp_lexer_peek_token (parser->lexer);
4208 done = (token->type != CPP_OPEN_SQUARE
4209 && token->type != CPP_OPEN_PAREN
4210 && token->type != CPP_DOT
4211 && token->type != CPP_DEREF
4212 && token->type != CPP_PLUS_PLUS
4213 && token->type != CPP_MINUS_MINUS);
4214 /* If we have a template-id, then no further lookup is
4215 required. If the template-id was for a template-class, we
4216 will sometimes have a TYPE_DECL at this point. */
4217 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4218 || TREE_CODE (id_expression) == TYPE_DECL)
4219 decl = id_expression;
4220 /* Look up the name. */
4223 tree ambiguous_decls;
4225 /* If we already know that this lookup is ambiguous, then
4226 we've already issued an error message; there's no reason
4228 if (id_expr_token->type == CPP_NAME
4229 && id_expr_token->ambiguous_p)
4231 cp_parser_simulate_error (parser);
4232 return error_mark_node;
4235 decl = cp_parser_lookup_name (parser, id_expression,
4238 /*is_namespace=*/false,
4239 /*check_dependency=*/true,
4241 id_expr_token->location);
4242 /* If the lookup was ambiguous, an error will already have
4244 if (ambiguous_decls)
4245 return error_mark_node;
4247 /* In Objective-C++, we may have an Objective-C 2.0
4248 dot-syntax for classes here. */
4249 if (c_dialect_objc ()
4250 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4251 && TREE_CODE (decl) == TYPE_DECL
4252 && objc_is_class_name (decl))
4255 cp_lexer_consume_token (parser->lexer);
4256 component = cp_parser_identifier (parser);
4257 if (component == error_mark_node)
4258 return error_mark_node;
4260 return objc_build_class_component_ref (id_expression, component);
4263 /* In Objective-C++, an instance variable (ivar) may be preferred
4264 to whatever cp_parser_lookup_name() found. */
4265 decl = objc_lookup_ivar (decl, id_expression);
4267 /* If name lookup gives us a SCOPE_REF, then the
4268 qualifying scope was dependent. */
4269 if (TREE_CODE (decl) == SCOPE_REF)
4271 /* At this point, we do not know if DECL is a valid
4272 integral constant expression. We assume that it is
4273 in fact such an expression, so that code like:
4275 template <int N> struct A {
4279 is accepted. At template-instantiation time, we
4280 will check that B<N>::i is actually a constant. */
4283 /* Check to see if DECL is a local variable in a context
4284 where that is forbidden. */
4285 if (parser->local_variables_forbidden_p
4286 && local_variable_p (decl))
4288 /* It might be that we only found DECL because we are
4289 trying to be generous with pre-ISO scoping rules.
4290 For example, consider:
4294 for (int i = 0; i < 10; ++i) {}
4295 extern void f(int j = i);
4298 Here, name look up will originally find the out
4299 of scope `i'. We need to issue a warning message,
4300 but then use the global `i'. */
4301 decl = check_for_out_of_scope_variable (decl);
4302 if (local_variable_p (decl))
4304 error_at (id_expr_token->location,
4305 "local variable %qD may not appear in this context",
4307 return error_mark_node;
4312 decl = (finish_id_expression
4313 (id_expression, decl, parser->scope,
4315 parser->integral_constant_expression_p,
4316 parser->allow_non_integral_constant_expression_p,
4317 &parser->non_integral_constant_expression_p,
4318 template_p, done, address_p,
4321 id_expr_token->location));
4323 cp_parser_error (parser, error_msg);
4327 /* Anything else is an error. */
4329 cp_parser_error (parser, "expected primary-expression");
4330 return error_mark_node;
4334 /* Parse an id-expression.
4341 :: [opt] nested-name-specifier template [opt] unqualified-id
4343 :: operator-function-id
4346 Return a representation of the unqualified portion of the
4347 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4348 a `::' or nested-name-specifier.
4350 Often, if the id-expression was a qualified-id, the caller will
4351 want to make a SCOPE_REF to represent the qualified-id. This
4352 function does not do this in order to avoid wastefully creating
4353 SCOPE_REFs when they are not required.
4355 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4358 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4359 uninstantiated templates.
4361 If *TEMPLATE_P is non-NULL, it is set to true iff the
4362 `template' keyword is used to explicitly indicate that the entity
4363 named is a template.
4365 If DECLARATOR_P is true, the id-expression is appearing as part of
4366 a declarator, rather than as part of an expression. */
4369 cp_parser_id_expression (cp_parser *parser,
4370 bool template_keyword_p,
4371 bool check_dependency_p,
4376 bool global_scope_p;
4377 bool nested_name_specifier_p;
4379 /* Assume the `template' keyword was not used. */
4381 *template_p = template_keyword_p;
4383 /* Look for the optional `::' operator. */
4385 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4387 /* Look for the optional nested-name-specifier. */
4388 nested_name_specifier_p
4389 = (cp_parser_nested_name_specifier_opt (parser,
4390 /*typename_keyword_p=*/false,
4395 /* If there is a nested-name-specifier, then we are looking at
4396 the first qualified-id production. */
4397 if (nested_name_specifier_p)
4400 tree saved_object_scope;
4401 tree saved_qualifying_scope;
4402 tree unqualified_id;
4405 /* See if the next token is the `template' keyword. */
4407 template_p = &is_template;
4408 *template_p = cp_parser_optional_template_keyword (parser);
4409 /* Name lookup we do during the processing of the
4410 unqualified-id might obliterate SCOPE. */
4411 saved_scope = parser->scope;
4412 saved_object_scope = parser->object_scope;
4413 saved_qualifying_scope = parser->qualifying_scope;
4414 /* Process the final unqualified-id. */
4415 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4418 /*optional_p=*/false);
4419 /* Restore the SAVED_SCOPE for our caller. */
4420 parser->scope = saved_scope;
4421 parser->object_scope = saved_object_scope;
4422 parser->qualifying_scope = saved_qualifying_scope;
4424 return unqualified_id;
4426 /* Otherwise, if we are in global scope, then we are looking at one
4427 of the other qualified-id productions. */
4428 else if (global_scope_p)
4433 /* Peek at the next token. */
4434 token = cp_lexer_peek_token (parser->lexer);
4436 /* If it's an identifier, and the next token is not a "<", then
4437 we can avoid the template-id case. This is an optimization
4438 for this common case. */
4439 if (token->type == CPP_NAME
4440 && !cp_parser_nth_token_starts_template_argument_list_p
4442 return cp_parser_identifier (parser);
4444 cp_parser_parse_tentatively (parser);
4445 /* Try a template-id. */
4446 id = cp_parser_template_id (parser,
4447 /*template_keyword_p=*/false,
4448 /*check_dependency_p=*/true,
4450 /* If that worked, we're done. */
4451 if (cp_parser_parse_definitely (parser))
4454 /* Peek at the next token. (Changes in the token buffer may
4455 have invalidated the pointer obtained above.) */
4456 token = cp_lexer_peek_token (parser->lexer);
4458 switch (token->type)
4461 return cp_parser_identifier (parser);
4464 if (token->keyword == RID_OPERATOR)
4465 return cp_parser_operator_function_id (parser);
4469 cp_parser_error (parser, "expected id-expression");
4470 return error_mark_node;
4474 return cp_parser_unqualified_id (parser, template_keyword_p,
4475 /*check_dependency_p=*/true,
4480 /* Parse an unqualified-id.
4484 operator-function-id
4485 conversion-function-id
4489 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4490 keyword, in a construct like `A::template ...'.
4492 Returns a representation of unqualified-id. For the `identifier'
4493 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4494 production a BIT_NOT_EXPR is returned; the operand of the
4495 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4496 other productions, see the documentation accompanying the
4497 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4498 names are looked up in uninstantiated templates. If DECLARATOR_P
4499 is true, the unqualified-id is appearing as part of a declarator,
4500 rather than as part of an expression. */
4503 cp_parser_unqualified_id (cp_parser* parser,
4504 bool template_keyword_p,
4505 bool check_dependency_p,
4511 /* Peek at the next token. */
4512 token = cp_lexer_peek_token (parser->lexer);
4514 switch (token->type)
4520 /* We don't know yet whether or not this will be a
4522 cp_parser_parse_tentatively (parser);
4523 /* Try a template-id. */
4524 id = cp_parser_template_id (parser, template_keyword_p,
4527 /* If it worked, we're done. */
4528 if (cp_parser_parse_definitely (parser))
4530 /* Otherwise, it's an ordinary identifier. */
4531 return cp_parser_identifier (parser);
4534 case CPP_TEMPLATE_ID:
4535 return cp_parser_template_id (parser, template_keyword_p,
4542 tree qualifying_scope;
4547 /* Consume the `~' token. */
4548 cp_lexer_consume_token (parser->lexer);
4549 /* Parse the class-name. The standard, as written, seems to
4552 template <typename T> struct S { ~S (); };
4553 template <typename T> S<T>::~S() {}
4555 is invalid, since `~' must be followed by a class-name, but
4556 `S<T>' is dependent, and so not known to be a class.
4557 That's not right; we need to look in uninstantiated
4558 templates. A further complication arises from:
4560 template <typename T> void f(T t) {
4564 Here, it is not possible to look up `T' in the scope of `T'
4565 itself. We must look in both the current scope, and the
4566 scope of the containing complete expression.
4568 Yet another issue is:
4577 The standard does not seem to say that the `S' in `~S'
4578 should refer to the type `S' and not the data member
4581 /* DR 244 says that we look up the name after the "~" in the
4582 same scope as we looked up the qualifying name. That idea
4583 isn't fully worked out; it's more complicated than that. */
4584 scope = parser->scope;
4585 object_scope = parser->object_scope;
4586 qualifying_scope = parser->qualifying_scope;
4588 /* Check for invalid scopes. */
4589 if (scope == error_mark_node)
4591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4592 cp_lexer_consume_token (parser->lexer);
4593 return error_mark_node;
4595 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4597 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4598 error_at (token->location,
4599 "scope %qT before %<~%> is not a class-name",
4601 cp_parser_simulate_error (parser);
4602 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4603 cp_lexer_consume_token (parser->lexer);
4604 return error_mark_node;
4606 gcc_assert (!scope || TYPE_P (scope));
4608 /* If the name is of the form "X::~X" it's OK even if X is a
4610 token = cp_lexer_peek_token (parser->lexer);
4612 && token->type == CPP_NAME
4613 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4615 && (token->u.value == TYPE_IDENTIFIER (scope)
4616 || (CLASS_TYPE_P (scope)
4617 && constructor_name_p (token->u.value, scope))))
4619 cp_lexer_consume_token (parser->lexer);
4620 return build_nt (BIT_NOT_EXPR, scope);
4623 /* If there was an explicit qualification (S::~T), first look
4624 in the scope given by the qualification (i.e., S).
4626 Note: in the calls to cp_parser_class_name below we pass
4627 typename_type so that lookup finds the injected-class-name
4628 rather than the constructor. */
4630 type_decl = NULL_TREE;
4633 cp_parser_parse_tentatively (parser);
4634 type_decl = cp_parser_class_name (parser,
4635 /*typename_keyword_p=*/false,
4636 /*template_keyword_p=*/false,
4638 /*check_dependency=*/false,
4639 /*class_head_p=*/false,
4641 if (cp_parser_parse_definitely (parser))
4644 /* In "N::S::~S", look in "N" as well. */
4645 if (!done && scope && qualifying_scope)
4647 cp_parser_parse_tentatively (parser);
4648 parser->scope = qualifying_scope;
4649 parser->object_scope = NULL_TREE;
4650 parser->qualifying_scope = NULL_TREE;
4652 = cp_parser_class_name (parser,
4653 /*typename_keyword_p=*/false,
4654 /*template_keyword_p=*/false,
4656 /*check_dependency=*/false,
4657 /*class_head_p=*/false,
4659 if (cp_parser_parse_definitely (parser))
4662 /* In "p->S::~T", look in the scope given by "*p" as well. */
4663 else if (!done && object_scope)
4665 cp_parser_parse_tentatively (parser);
4666 parser->scope = object_scope;
4667 parser->object_scope = NULL_TREE;
4668 parser->qualifying_scope = NULL_TREE;
4670 = cp_parser_class_name (parser,
4671 /*typename_keyword_p=*/false,
4672 /*template_keyword_p=*/false,
4674 /*check_dependency=*/false,
4675 /*class_head_p=*/false,
4677 if (cp_parser_parse_definitely (parser))
4680 /* Look in the surrounding context. */
4683 parser->scope = NULL_TREE;
4684 parser->object_scope = NULL_TREE;
4685 parser->qualifying_scope = NULL_TREE;
4686 if (processing_template_decl)
4687 cp_parser_parse_tentatively (parser);
4689 = cp_parser_class_name (parser,
4690 /*typename_keyword_p=*/false,
4691 /*template_keyword_p=*/false,
4693 /*check_dependency=*/false,
4694 /*class_head_p=*/false,
4696 if (processing_template_decl
4697 && ! cp_parser_parse_definitely (parser))
4699 /* We couldn't find a type with this name, so just accept
4700 it and check for a match at instantiation time. */
4701 type_decl = cp_parser_identifier (parser);
4702 if (type_decl != error_mark_node)
4703 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4707 /* If an error occurred, assume that the name of the
4708 destructor is the same as the name of the qualifying
4709 class. That allows us to keep parsing after running
4710 into ill-formed destructor names. */
4711 if (type_decl == error_mark_node && scope)
4712 return build_nt (BIT_NOT_EXPR, scope);
4713 else if (type_decl == error_mark_node)
4714 return error_mark_node;
4716 /* Check that destructor name and scope match. */
4717 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4719 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4720 error_at (token->location,
4721 "declaration of %<~%T%> as member of %qT",
4723 cp_parser_simulate_error (parser);
4724 return error_mark_node;
4729 A typedef-name that names a class shall not be used as the
4730 identifier in the declarator for a destructor declaration. */
4732 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4733 && !DECL_SELF_REFERENCE_P (type_decl)
4734 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4735 error_at (token->location,
4736 "typedef-name %qD used as destructor declarator",
4739 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4743 if (token->keyword == RID_OPERATOR)
4747 /* This could be a template-id, so we try that first. */
4748 cp_parser_parse_tentatively (parser);
4749 /* Try a template-id. */
4750 id = cp_parser_template_id (parser, template_keyword_p,
4751 /*check_dependency_p=*/true,
4753 /* If that worked, we're done. */
4754 if (cp_parser_parse_definitely (parser))
4756 /* We still don't know whether we're looking at an
4757 operator-function-id or a conversion-function-id. */
4758 cp_parser_parse_tentatively (parser);
4759 /* Try an operator-function-id. */
4760 id = cp_parser_operator_function_id (parser);
4761 /* If that didn't work, try a conversion-function-id. */
4762 if (!cp_parser_parse_definitely (parser))
4763 id = cp_parser_conversion_function_id (parser);
4764 else if (UDLIT_OPER_P (id))
4767 const char *name = UDLIT_OP_SUFFIX (id);
4768 if (name[0] != '_' && !in_system_header)
4769 warning (0, "literal operator suffixes not preceded by %<_%>"
4770 " are reserved for future standardization");
4780 cp_parser_error (parser, "expected unqualified-id");
4781 return error_mark_node;
4785 /* Parse an (optional) nested-name-specifier.
4787 nested-name-specifier: [C++98]
4788 class-or-namespace-name :: nested-name-specifier [opt]
4789 class-or-namespace-name :: template nested-name-specifier [opt]
4791 nested-name-specifier: [C++0x]
4794 nested-name-specifier identifier ::
4795 nested-name-specifier template [opt] simple-template-id ::
4797 PARSER->SCOPE should be set appropriately before this function is
4798 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4799 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4802 Sets PARSER->SCOPE to the class (TYPE) or namespace
4803 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4804 it unchanged if there is no nested-name-specifier. Returns the new
4805 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4807 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4808 part of a declaration and/or decl-specifier. */
4811 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4812 bool typename_keyword_p,
4813 bool check_dependency_p,
4815 bool is_declaration)
4817 bool success = false;
4818 cp_token_position start = 0;
4821 /* Remember where the nested-name-specifier starts. */
4822 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4824 start = cp_lexer_token_position (parser->lexer, false);
4825 push_deferring_access_checks (dk_deferred);
4832 tree saved_qualifying_scope;
4833 bool template_keyword_p;
4835 /* Spot cases that cannot be the beginning of a
4836 nested-name-specifier. */
4837 token = cp_lexer_peek_token (parser->lexer);
4839 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4840 the already parsed nested-name-specifier. */
4841 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4843 /* Grab the nested-name-specifier and continue the loop. */
4844 cp_parser_pre_parsed_nested_name_specifier (parser);
4845 /* If we originally encountered this nested-name-specifier
4846 with IS_DECLARATION set to false, we will not have
4847 resolved TYPENAME_TYPEs, so we must do so here. */
4849 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4851 new_scope = resolve_typename_type (parser->scope,
4852 /*only_current_p=*/false);
4853 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4854 parser->scope = new_scope;
4860 /* Spot cases that cannot be the beginning of a
4861 nested-name-specifier. On the second and subsequent times
4862 through the loop, we look for the `template' keyword. */
4863 if (success && token->keyword == RID_TEMPLATE)
4865 /* A template-id can start a nested-name-specifier. */
4866 else if (token->type == CPP_TEMPLATE_ID)
4868 /* DR 743: decltype can be used in a nested-name-specifier. */
4869 else if (token_is_decltype (token))
4873 /* If the next token is not an identifier, then it is
4874 definitely not a type-name or namespace-name. */
4875 if (token->type != CPP_NAME)
4877 /* If the following token is neither a `<' (to begin a
4878 template-id), nor a `::', then we are not looking at a
4879 nested-name-specifier. */
4880 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4882 if (token->type == CPP_COLON
4883 && parser->colon_corrects_to_scope_p
4884 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4886 error_at (token->location,
4887 "found %<:%> in nested-name-specifier, expected %<::%>");
4888 token->type = CPP_SCOPE;
4891 if (token->type != CPP_SCOPE
4892 && !cp_parser_nth_token_starts_template_argument_list_p
4897 /* The nested-name-specifier is optional, so we parse
4899 cp_parser_parse_tentatively (parser);
4901 /* Look for the optional `template' keyword, if this isn't the
4902 first time through the loop. */
4904 template_keyword_p = cp_parser_optional_template_keyword (parser);
4906 template_keyword_p = false;
4908 /* Save the old scope since the name lookup we are about to do
4909 might destroy it. */
4910 old_scope = parser->scope;
4911 saved_qualifying_scope = parser->qualifying_scope;
4912 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4913 look up names in "X<T>::I" in order to determine that "Y" is
4914 a template. So, if we have a typename at this point, we make
4915 an effort to look through it. */
4917 && !typename_keyword_p
4919 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4920 parser->scope = resolve_typename_type (parser->scope,
4921 /*only_current_p=*/false);
4922 /* Parse the qualifying entity. */
4924 = cp_parser_qualifying_entity (parser,
4930 /* Look for the `::' token. */
4931 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4933 /* If we found what we wanted, we keep going; otherwise, we're
4935 if (!cp_parser_parse_definitely (parser))
4937 bool error_p = false;
4939 /* Restore the OLD_SCOPE since it was valid before the
4940 failed attempt at finding the last
4941 class-or-namespace-name. */
4942 parser->scope = old_scope;
4943 parser->qualifying_scope = saved_qualifying_scope;
4945 /* If the next token is a decltype, and the one after that is a
4946 `::', then the decltype has failed to resolve to a class or
4947 enumeration type. Give this error even when parsing
4948 tentatively since it can't possibly be valid--and we're going
4949 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4950 won't get another chance.*/
4951 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4952 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4955 token = cp_lexer_consume_token (parser->lexer);
4956 error_at (token->location, "decltype evaluates to %qT, "
4957 "which is not a class or enumeration type",
4959 parser->scope = error_mark_node;
4963 cp_lexer_consume_token (parser->lexer);
4966 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4968 /* If the next token is an identifier, and the one after
4969 that is a `::', then any valid interpretation would have
4970 found a class-or-namespace-name. */
4971 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4972 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4974 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4977 token = cp_lexer_consume_token (parser->lexer);
4980 if (!token->ambiguous_p)
4983 tree ambiguous_decls;
4985 decl = cp_parser_lookup_name (parser, token->u.value,
4987 /*is_template=*/false,
4988 /*is_namespace=*/false,
4989 /*check_dependency=*/true,
4992 if (TREE_CODE (decl) == TEMPLATE_DECL)
4993 error_at (token->location,
4994 "%qD used without template parameters",
4996 else if (ambiguous_decls)
4998 error_at (token->location,
4999 "reference to %qD is ambiguous",
5001 print_candidates (ambiguous_decls);
5002 decl = error_mark_node;
5006 if (cxx_dialect != cxx98)
5007 cp_parser_name_lookup_error
5008 (parser, token->u.value, decl, NLE_NOT_CXX98,
5011 cp_parser_name_lookup_error
5012 (parser, token->u.value, decl, NLE_CXX98,
5016 parser->scope = error_mark_node;
5018 /* Treat this as a successful nested-name-specifier
5023 If the name found is not a class-name (clause
5024 _class_) or namespace-name (_namespace.def_), the
5025 program is ill-formed. */
5028 cp_lexer_consume_token (parser->lexer);
5032 /* We've found one valid nested-name-specifier. */
5034 /* Name lookup always gives us a DECL. */
5035 if (TREE_CODE (new_scope) == TYPE_DECL)
5036 new_scope = TREE_TYPE (new_scope);
5037 /* Uses of "template" must be followed by actual templates. */
5038 if (template_keyword_p
5039 && !(CLASS_TYPE_P (new_scope)
5040 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5041 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5042 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5043 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5044 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5045 == TEMPLATE_ID_EXPR)))
5046 permerror (input_location, TYPE_P (new_scope)
5047 ? G_("%qT is not a template")
5048 : G_("%qD is not a template"),
5050 /* If it is a class scope, try to complete it; we are about to
5051 be looking up names inside the class. */
5052 if (TYPE_P (new_scope)
5053 /* Since checking types for dependency can be expensive,
5054 avoid doing it if the type is already complete. */
5055 && !COMPLETE_TYPE_P (new_scope)
5056 /* Do not try to complete dependent types. */
5057 && !dependent_type_p (new_scope))
5059 new_scope = complete_type (new_scope);
5060 /* If it is a typedef to current class, use the current
5061 class instead, as the typedef won't have any names inside
5063 if (!COMPLETE_TYPE_P (new_scope)
5064 && currently_open_class (new_scope))
5065 new_scope = TYPE_MAIN_VARIANT (new_scope);
5067 /* Make sure we look in the right scope the next time through
5069 parser->scope = new_scope;
5072 /* If parsing tentatively, replace the sequence of tokens that makes
5073 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5074 token. That way, should we re-parse the token stream, we will
5075 not have to repeat the effort required to do the parse, nor will
5076 we issue duplicate error messages. */
5077 if (success && start)
5081 token = cp_lexer_token_at (parser->lexer, start);
5082 /* Reset the contents of the START token. */
5083 token->type = CPP_NESTED_NAME_SPECIFIER;
5084 /* Retrieve any deferred checks. Do not pop this access checks yet
5085 so the memory will not be reclaimed during token replacing below. */
5086 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5087 token->u.tree_check_value->value = parser->scope;
5088 token->u.tree_check_value->checks = get_deferred_access_checks ();
5089 token->u.tree_check_value->qualifying_scope =
5090 parser->qualifying_scope;
5091 token->keyword = RID_MAX;
5093 /* Purge all subsequent tokens. */
5094 cp_lexer_purge_tokens_after (parser->lexer, start);
5098 pop_to_parent_deferring_access_checks ();
5100 return success ? parser->scope : NULL_TREE;
5103 /* Parse a nested-name-specifier. See
5104 cp_parser_nested_name_specifier_opt for details. This function
5105 behaves identically, except that it will an issue an error if no
5106 nested-name-specifier is present. */
5109 cp_parser_nested_name_specifier (cp_parser *parser,
5110 bool typename_keyword_p,
5111 bool check_dependency_p,
5113 bool is_declaration)
5117 /* Look for the nested-name-specifier. */
5118 scope = cp_parser_nested_name_specifier_opt (parser,
5123 /* If it was not present, issue an error message. */
5126 cp_parser_error (parser, "expected nested-name-specifier");
5127 parser->scope = NULL_TREE;
5133 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5134 this is either a class-name or a namespace-name (which corresponds
5135 to the class-or-namespace-name production in the grammar). For
5136 C++0x, it can also be a type-name that refers to an enumeration
5139 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5140 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5141 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5142 TYPE_P is TRUE iff the next name should be taken as a class-name,
5143 even the same name is declared to be another entity in the same
5146 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5147 specified by the class-or-namespace-name. If neither is found the
5148 ERROR_MARK_NODE is returned. */
5151 cp_parser_qualifying_entity (cp_parser *parser,
5152 bool typename_keyword_p,
5153 bool template_keyword_p,
5154 bool check_dependency_p,
5156 bool is_declaration)
5159 tree saved_qualifying_scope;
5160 tree saved_object_scope;
5163 bool successful_parse_p;
5165 /* DR 743: decltype can appear in a nested-name-specifier. */
5166 if (cp_lexer_next_token_is_decltype (parser->lexer))
5168 scope = cp_parser_decltype (parser);
5169 if (TREE_CODE (scope) != ENUMERAL_TYPE
5170 && !MAYBE_CLASS_TYPE_P (scope))
5172 cp_parser_simulate_error (parser);
5173 return error_mark_node;
5175 if (TYPE_NAME (scope))
5176 scope = TYPE_NAME (scope);
5180 /* Before we try to parse the class-name, we must save away the
5181 current PARSER->SCOPE since cp_parser_class_name will destroy
5183 saved_scope = parser->scope;
5184 saved_qualifying_scope = parser->qualifying_scope;
5185 saved_object_scope = parser->object_scope;
5186 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5187 there is no need to look for a namespace-name. */
5188 only_class_p = template_keyword_p
5189 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5191 cp_parser_parse_tentatively (parser);
5192 scope = cp_parser_class_name (parser,
5195 type_p ? class_type : none_type,
5197 /*class_head_p=*/false,
5199 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5200 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5202 && cxx_dialect != cxx98
5203 && !successful_parse_p)
5205 /* Restore the saved scope. */
5206 parser->scope = saved_scope;
5207 parser->qualifying_scope = saved_qualifying_scope;
5208 parser->object_scope = saved_object_scope;
5210 /* Parse tentatively. */
5211 cp_parser_parse_tentatively (parser);
5213 /* Parse a typedef-name or enum-name. */
5214 scope = cp_parser_nonclass_name (parser);
5216 /* "If the name found does not designate a namespace or a class,
5217 enumeration, or dependent type, the program is ill-formed."
5219 We cover classes and dependent types above and namespaces below,
5220 so this code is only looking for enums. */
5221 if (!scope || TREE_CODE (scope) != TYPE_DECL
5222 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5223 cp_parser_simulate_error (parser);
5225 successful_parse_p = cp_parser_parse_definitely (parser);
5227 /* If that didn't work, try for a namespace-name. */
5228 if (!only_class_p && !successful_parse_p)
5230 /* Restore the saved scope. */
5231 parser->scope = saved_scope;
5232 parser->qualifying_scope = saved_qualifying_scope;
5233 parser->object_scope = saved_object_scope;
5234 /* If we are not looking at an identifier followed by the scope
5235 resolution operator, then this is not part of a
5236 nested-name-specifier. (Note that this function is only used
5237 to parse the components of a nested-name-specifier.) */
5238 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5239 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5240 return error_mark_node;
5241 scope = cp_parser_namespace_name (parser);
5247 /* Parse a postfix-expression.
5251 postfix-expression [ expression ]
5252 postfix-expression ( expression-list [opt] )
5253 simple-type-specifier ( expression-list [opt] )
5254 typename :: [opt] nested-name-specifier identifier
5255 ( expression-list [opt] )
5256 typename :: [opt] nested-name-specifier template [opt] template-id
5257 ( expression-list [opt] )
5258 postfix-expression . template [opt] id-expression
5259 postfix-expression -> template [opt] id-expression
5260 postfix-expression . pseudo-destructor-name
5261 postfix-expression -> pseudo-destructor-name
5262 postfix-expression ++
5263 postfix-expression --
5264 dynamic_cast < type-id > ( expression )
5265 static_cast < type-id > ( expression )
5266 reinterpret_cast < type-id > ( expression )
5267 const_cast < type-id > ( expression )
5268 typeid ( expression )
5274 ( type-id ) { initializer-list , [opt] }
5276 This extension is a GNU version of the C99 compound-literal
5277 construct. (The C99 grammar uses `type-name' instead of `type-id',
5278 but they are essentially the same concept.)
5280 If ADDRESS_P is true, the postfix expression is the operand of the
5281 `&' operator. CAST_P is true if this expression is the target of a
5284 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5285 class member access expressions [expr.ref].
5287 Returns a representation of the expression. */
5290 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5291 bool member_access_only_p,
5292 cp_id_kind * pidk_return)
5296 cp_id_kind idk = CP_ID_KIND_NONE;
5297 tree postfix_expression = NULL_TREE;
5298 bool is_member_access = false;
5300 /* Peek at the next token. */
5301 token = cp_lexer_peek_token (parser->lexer);
5302 /* Some of the productions are determined by keywords. */
5303 keyword = token->keyword;
5313 const char *saved_message;
5315 /* All of these can be handled in the same way from the point
5316 of view of parsing. Begin by consuming the token
5317 identifying the cast. */
5318 cp_lexer_consume_token (parser->lexer);
5320 /* New types cannot be defined in the cast. */
5321 saved_message = parser->type_definition_forbidden_message;
5322 parser->type_definition_forbidden_message
5323 = G_("types may not be defined in casts");
5325 /* Look for the opening `<'. */
5326 cp_parser_require (parser, CPP_LESS, RT_LESS);
5327 /* Parse the type to which we are casting. */
5328 type = cp_parser_type_id (parser);
5329 /* Look for the closing `>'. */
5330 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5331 /* Restore the old message. */
5332 parser->type_definition_forbidden_message = saved_message;
5334 /* And the expression which is being cast. */
5335 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5336 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5337 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5339 /* Only type conversions to integral or enumeration types
5340 can be used in constant-expressions. */
5341 if (!cast_valid_in_integral_constant_expression_p (type)
5342 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5343 return error_mark_node;
5349 = build_dynamic_cast (type, expression, tf_warning_or_error);
5353 = build_static_cast (type, expression, tf_warning_or_error);
5357 = build_reinterpret_cast (type, expression,
5358 tf_warning_or_error);
5362 = build_const_cast (type, expression, tf_warning_or_error);
5373 const char *saved_message;
5374 bool saved_in_type_id_in_expr_p;
5376 /* Consume the `typeid' token. */
5377 cp_lexer_consume_token (parser->lexer);
5378 /* Look for the `(' token. */
5379 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5380 /* Types cannot be defined in a `typeid' expression. */
5381 saved_message = parser->type_definition_forbidden_message;
5382 parser->type_definition_forbidden_message
5383 = G_("types may not be defined in a %<typeid%> expression");
5384 /* We can't be sure yet whether we're looking at a type-id or an
5386 cp_parser_parse_tentatively (parser);
5387 /* Try a type-id first. */
5388 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5389 parser->in_type_id_in_expr_p = true;
5390 type = cp_parser_type_id (parser);
5391 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5392 /* Look for the `)' token. Otherwise, we can't be sure that
5393 we're not looking at an expression: consider `typeid (int
5394 (3))', for example. */
5395 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5396 /* If all went well, simply lookup the type-id. */
5397 if (cp_parser_parse_definitely (parser))
5398 postfix_expression = get_typeid (type);
5399 /* Otherwise, fall back to the expression variant. */
5404 /* Look for an expression. */
5405 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5406 /* Compute its typeid. */
5407 postfix_expression = build_typeid (expression);
5408 /* Look for the `)' token. */
5409 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5411 /* Restore the saved message. */
5412 parser->type_definition_forbidden_message = saved_message;
5413 /* `typeid' may not appear in an integral constant expression. */
5414 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5415 return error_mark_node;
5422 /* The syntax permitted here is the same permitted for an
5423 elaborated-type-specifier. */
5424 type = cp_parser_elaborated_type_specifier (parser,
5425 /*is_friend=*/false,
5426 /*is_declaration=*/false);
5427 postfix_expression = cp_parser_functional_cast (parser, type);
5435 /* If the next thing is a simple-type-specifier, we may be
5436 looking at a functional cast. We could also be looking at
5437 an id-expression. So, we try the functional cast, and if
5438 that doesn't work we fall back to the primary-expression. */
5439 cp_parser_parse_tentatively (parser);
5440 /* Look for the simple-type-specifier. */
5441 type = cp_parser_simple_type_specifier (parser,
5442 /*decl_specs=*/NULL,
5443 CP_PARSER_FLAGS_NONE);
5444 /* Parse the cast itself. */
5445 if (!cp_parser_error_occurred (parser))
5447 = cp_parser_functional_cast (parser, type);
5448 /* If that worked, we're done. */
5449 if (cp_parser_parse_definitely (parser))
5452 /* If the functional-cast didn't work out, try a
5453 compound-literal. */
5454 if (cp_parser_allow_gnu_extensions_p (parser)
5455 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5457 VEC(constructor_elt,gc) *initializer_list = NULL;
5458 bool saved_in_type_id_in_expr_p;
5460 cp_parser_parse_tentatively (parser);
5461 /* Consume the `('. */
5462 cp_lexer_consume_token (parser->lexer);
5463 /* Parse the type. */
5464 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5465 parser->in_type_id_in_expr_p = true;
5466 type = cp_parser_type_id (parser);
5467 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5468 /* Look for the `)'. */
5469 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5470 /* Look for the `{'. */
5471 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5472 /* If things aren't going well, there's no need to
5474 if (!cp_parser_error_occurred (parser))
5476 bool non_constant_p;
5477 /* Parse the initializer-list. */
5479 = cp_parser_initializer_list (parser, &non_constant_p);
5480 /* Allow a trailing `,'. */
5481 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5482 cp_lexer_consume_token (parser->lexer);
5483 /* Look for the final `}'. */
5484 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5486 /* If that worked, we're definitely looking at a
5487 compound-literal expression. */
5488 if (cp_parser_parse_definitely (parser))
5490 /* Warn the user that a compound literal is not
5491 allowed in standard C++. */
5492 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5493 /* For simplicity, we disallow compound literals in
5494 constant-expressions. We could
5495 allow compound literals of integer type, whose
5496 initializer was a constant, in constant
5497 expressions. Permitting that usage, as a further
5498 extension, would not change the meaning of any
5499 currently accepted programs. (Of course, as
5500 compound literals are not part of ISO C++, the
5501 standard has nothing to say.) */
5502 if (cp_parser_non_integral_constant_expression (parser,
5505 postfix_expression = error_mark_node;
5508 /* Form the representation of the compound-literal. */
5510 = (finish_compound_literal
5511 (type, build_constructor (init_list_type_node,
5513 tf_warning_or_error));
5518 /* It must be a primary-expression. */
5520 = cp_parser_primary_expression (parser, address_p, cast_p,
5521 /*template_arg_p=*/false,
5527 /* Keep looping until the postfix-expression is complete. */
5530 if (idk == CP_ID_KIND_UNQUALIFIED
5531 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5532 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5533 /* It is not a Koenig lookup function call. */
5535 = unqualified_name_lookup_error (postfix_expression);
5537 /* Peek at the next token. */
5538 token = cp_lexer_peek_token (parser->lexer);
5540 switch (token->type)
5542 case CPP_OPEN_SQUARE:
5544 = cp_parser_postfix_open_square_expression (parser,
5547 idk = CP_ID_KIND_NONE;
5548 is_member_access = false;
5551 case CPP_OPEN_PAREN:
5552 /* postfix-expression ( expression-list [opt] ) */
5555 bool is_builtin_constant_p;
5556 bool saved_integral_constant_expression_p = false;
5557 bool saved_non_integral_constant_expression_p = false;
5560 is_member_access = false;
5562 is_builtin_constant_p
5563 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5564 if (is_builtin_constant_p)
5566 /* The whole point of __builtin_constant_p is to allow
5567 non-constant expressions to appear as arguments. */
5568 saved_integral_constant_expression_p
5569 = parser->integral_constant_expression_p;
5570 saved_non_integral_constant_expression_p
5571 = parser->non_integral_constant_expression_p;
5572 parser->integral_constant_expression_p = false;
5574 args = (cp_parser_parenthesized_expression_list
5576 /*cast_p=*/false, /*allow_expansion_p=*/true,
5577 /*non_constant_p=*/NULL));
5578 if (is_builtin_constant_p)
5580 parser->integral_constant_expression_p
5581 = saved_integral_constant_expression_p;
5582 parser->non_integral_constant_expression_p
5583 = saved_non_integral_constant_expression_p;
5588 postfix_expression = error_mark_node;
5592 /* Function calls are not permitted in
5593 constant-expressions. */
5594 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5595 && cp_parser_non_integral_constant_expression (parser,
5598 postfix_expression = error_mark_node;
5599 release_tree_vector (args);
5604 if (idk == CP_ID_KIND_UNQUALIFIED
5605 || idk == CP_ID_KIND_TEMPLATE_ID)
5607 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5609 if (!VEC_empty (tree, args))
5612 if (!any_type_dependent_arguments_p (args))
5614 = perform_koenig_lookup (postfix_expression, args,
5615 /*include_std=*/false,
5616 tf_warning_or_error);
5620 = unqualified_fn_lookup_error (postfix_expression);
5622 /* We do not perform argument-dependent lookup if
5623 normal lookup finds a non-function, in accordance
5624 with the expected resolution of DR 218. */
5625 else if (!VEC_empty (tree, args)
5626 && is_overloaded_fn (postfix_expression))
5628 tree fn = get_first_fn (postfix_expression);
5629 fn = STRIP_TEMPLATE (fn);
5631 /* Do not do argument dependent lookup if regular
5632 lookup finds a member function or a block-scope
5633 function declaration. [basic.lookup.argdep]/3 */
5634 if (!DECL_FUNCTION_MEMBER_P (fn)
5635 && !DECL_LOCAL_FUNCTION_P (fn))
5638 if (!any_type_dependent_arguments_p (args))
5640 = perform_koenig_lookup (postfix_expression, args,
5641 /*include_std=*/false,
5642 tf_warning_or_error);
5647 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5649 tree instance = TREE_OPERAND (postfix_expression, 0);
5650 tree fn = TREE_OPERAND (postfix_expression, 1);
5652 if (processing_template_decl
5653 && (type_dependent_expression_p (instance)
5654 || (!BASELINK_P (fn)
5655 && TREE_CODE (fn) != FIELD_DECL)
5656 || type_dependent_expression_p (fn)
5657 || any_type_dependent_arguments_p (args)))
5660 = build_nt_call_vec (postfix_expression, args);
5661 release_tree_vector (args);
5665 if (BASELINK_P (fn))
5668 = (build_new_method_call
5669 (instance, fn, &args, NULL_TREE,
5670 (idk == CP_ID_KIND_QUALIFIED
5671 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5674 tf_warning_or_error));
5678 = finish_call_expr (postfix_expression, &args,
5679 /*disallow_virtual=*/false,
5681 tf_warning_or_error);
5683 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5684 || TREE_CODE (postfix_expression) == MEMBER_REF
5685 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5686 postfix_expression = (build_offset_ref_call_from_tree
5687 (postfix_expression, &args));
5688 else if (idk == CP_ID_KIND_QUALIFIED)
5689 /* A call to a static class member, or a namespace-scope
5692 = finish_call_expr (postfix_expression, &args,
5693 /*disallow_virtual=*/true,
5695 tf_warning_or_error);
5697 /* All other function calls. */
5699 = finish_call_expr (postfix_expression, &args,
5700 /*disallow_virtual=*/false,
5702 tf_warning_or_error);
5704 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5705 idk = CP_ID_KIND_NONE;
5707 release_tree_vector (args);
5713 /* postfix-expression . template [opt] id-expression
5714 postfix-expression . pseudo-destructor-name
5715 postfix-expression -> template [opt] id-expression
5716 postfix-expression -> pseudo-destructor-name */
5718 /* Consume the `.' or `->' operator. */
5719 cp_lexer_consume_token (parser->lexer);
5722 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5727 is_member_access = true;
5731 /* postfix-expression ++ */
5732 /* Consume the `++' token. */
5733 cp_lexer_consume_token (parser->lexer);
5734 /* Generate a representation for the complete expression. */
5736 = finish_increment_expr (postfix_expression,
5737 POSTINCREMENT_EXPR);
5738 /* Increments may not appear in constant-expressions. */
5739 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5740 postfix_expression = error_mark_node;
5741 idk = CP_ID_KIND_NONE;
5742 is_member_access = false;
5745 case CPP_MINUS_MINUS:
5746 /* postfix-expression -- */
5747 /* Consume the `--' token. */
5748 cp_lexer_consume_token (parser->lexer);
5749 /* Generate a representation for the complete expression. */
5751 = finish_increment_expr (postfix_expression,
5752 POSTDECREMENT_EXPR);
5753 /* Decrements may not appear in constant-expressions. */
5754 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5755 postfix_expression = error_mark_node;
5756 idk = CP_ID_KIND_NONE;
5757 is_member_access = false;
5761 if (pidk_return != NULL)
5762 * pidk_return = idk;
5763 if (member_access_only_p)
5764 return is_member_access? postfix_expression : error_mark_node;
5766 return postfix_expression;
5770 /* We should never get here. */
5772 return error_mark_node;
5775 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5776 by cp_parser_builtin_offsetof. We're looking for
5778 postfix-expression [ expression ]
5780 FOR_OFFSETOF is set if we're being called in that context, which
5781 changes how we deal with integer constant expressions. */
5784 cp_parser_postfix_open_square_expression (cp_parser *parser,
5785 tree postfix_expression,
5790 /* Consume the `[' token. */
5791 cp_lexer_consume_token (parser->lexer);
5793 /* Parse the index expression. */
5794 /* ??? For offsetof, there is a question of what to allow here. If
5795 offsetof is not being used in an integral constant expression context,
5796 then we *could* get the right answer by computing the value at runtime.
5797 If we are in an integral constant expression context, then we might
5798 could accept any constant expression; hard to say without analysis.
5799 Rather than open the barn door too wide right away, allow only integer
5800 constant expressions here. */
5802 index = cp_parser_constant_expression (parser, false, NULL);
5804 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5806 /* Look for the closing `]'. */
5807 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5809 /* Build the ARRAY_REF. */
5810 postfix_expression = grok_array_decl (postfix_expression, index);
5812 /* When not doing offsetof, array references are not permitted in
5813 constant-expressions. */
5815 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5816 postfix_expression = error_mark_node;
5818 return postfix_expression;
5821 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5822 by cp_parser_builtin_offsetof. We're looking for
5824 postfix-expression . template [opt] id-expression
5825 postfix-expression . pseudo-destructor-name
5826 postfix-expression -> template [opt] id-expression
5827 postfix-expression -> pseudo-destructor-name
5829 FOR_OFFSETOF is set if we're being called in that context. That sorta
5830 limits what of the above we'll actually accept, but nevermind.
5831 TOKEN_TYPE is the "." or "->" token, which will already have been
5832 removed from the stream. */
5835 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5836 enum cpp_ttype token_type,
5837 tree postfix_expression,
5838 bool for_offsetof, cp_id_kind *idk,
5839 location_t location)
5843 bool pseudo_destructor_p;
5844 tree scope = NULL_TREE;
5846 /* If this is a `->' operator, dereference the pointer. */
5847 if (token_type == CPP_DEREF)
5848 postfix_expression = build_x_arrow (postfix_expression);
5849 /* Check to see whether or not the expression is type-dependent. */
5850 dependent_p = type_dependent_expression_p (postfix_expression);
5851 /* The identifier following the `->' or `.' is not qualified. */
5852 parser->scope = NULL_TREE;
5853 parser->qualifying_scope = NULL_TREE;
5854 parser->object_scope = NULL_TREE;
5855 *idk = CP_ID_KIND_NONE;
5857 /* Enter the scope corresponding to the type of the object
5858 given by the POSTFIX_EXPRESSION. */
5859 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5861 scope = TREE_TYPE (postfix_expression);
5862 /* According to the standard, no expression should ever have
5863 reference type. Unfortunately, we do not currently match
5864 the standard in this respect in that our internal representation
5865 of an expression may have reference type even when the standard
5866 says it does not. Therefore, we have to manually obtain the
5867 underlying type here. */
5868 scope = non_reference (scope);
5869 /* The type of the POSTFIX_EXPRESSION must be complete. */
5870 if (scope == unknown_type_node)
5872 error_at (location, "%qE does not have class type",
5873 postfix_expression);
5876 /* Unlike the object expression in other contexts, *this is not
5877 required to be of complete type for purposes of class member
5878 access (5.2.5) outside the member function body. */
5879 else if (scope != current_class_ref
5880 && !(processing_template_decl && scope == current_class_type))
5881 scope = complete_type_or_else (scope, NULL_TREE);
5882 /* Let the name lookup machinery know that we are processing a
5883 class member access expression. */
5884 parser->context->object_type = scope;
5885 /* If something went wrong, we want to be able to discern that case,
5886 as opposed to the case where there was no SCOPE due to the type
5887 of expression being dependent. */
5889 scope = error_mark_node;
5890 /* If the SCOPE was erroneous, make the various semantic analysis
5891 functions exit quickly -- and without issuing additional error
5893 if (scope == error_mark_node)
5894 postfix_expression = error_mark_node;
5897 /* Assume this expression is not a pseudo-destructor access. */
5898 pseudo_destructor_p = false;
5900 /* If the SCOPE is a scalar type, then, if this is a valid program,
5901 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5902 is type dependent, it can be pseudo-destructor-name or something else.
5903 Try to parse it as pseudo-destructor-name first. */
5904 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5909 cp_parser_parse_tentatively (parser);
5910 /* Parse the pseudo-destructor-name. */
5912 cp_parser_pseudo_destructor_name (parser, &s, &type);
5914 && (cp_parser_error_occurred (parser)
5915 || TREE_CODE (type) != TYPE_DECL
5916 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5917 cp_parser_abort_tentative_parse (parser);
5918 else if (cp_parser_parse_definitely (parser))
5920 pseudo_destructor_p = true;
5922 = finish_pseudo_destructor_expr (postfix_expression,
5923 s, TREE_TYPE (type));
5927 if (!pseudo_destructor_p)
5929 /* If the SCOPE is not a scalar type, we are looking at an
5930 ordinary class member access expression, rather than a
5931 pseudo-destructor-name. */
5933 cp_token *token = cp_lexer_peek_token (parser->lexer);
5934 /* Parse the id-expression. */
5935 name = (cp_parser_id_expression
5937 cp_parser_optional_template_keyword (parser),
5938 /*check_dependency_p=*/true,
5940 /*declarator_p=*/false,
5941 /*optional_p=*/false));
5942 /* In general, build a SCOPE_REF if the member name is qualified.
5943 However, if the name was not dependent and has already been
5944 resolved; there is no need to build the SCOPE_REF. For example;
5946 struct X { void f(); };
5947 template <typename T> void f(T* t) { t->X::f(); }
5949 Even though "t" is dependent, "X::f" is not and has been resolved
5950 to a BASELINK; there is no need to include scope information. */
5952 /* But we do need to remember that there was an explicit scope for
5953 virtual function calls. */
5955 *idk = CP_ID_KIND_QUALIFIED;
5957 /* If the name is a template-id that names a type, we will get a
5958 TYPE_DECL here. That is invalid code. */
5959 if (TREE_CODE (name) == TYPE_DECL)
5961 error_at (token->location, "invalid use of %qD", name);
5962 postfix_expression = error_mark_node;
5966 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5968 name = build_qualified_name (/*type=*/NULL_TREE,
5972 parser->scope = NULL_TREE;
5973 parser->qualifying_scope = NULL_TREE;
5974 parser->object_scope = NULL_TREE;
5976 if (scope && name && BASELINK_P (name))
5977 adjust_result_of_qualified_name_lookup
5978 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5980 = finish_class_member_access_expr (postfix_expression, name,
5982 tf_warning_or_error);
5986 /* We no longer need to look up names in the scope of the object on
5987 the left-hand side of the `.' or `->' operator. */
5988 parser->context->object_type = NULL_TREE;
5990 /* Outside of offsetof, these operators may not appear in
5991 constant-expressions. */
5993 && (cp_parser_non_integral_constant_expression
5994 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5995 postfix_expression = error_mark_node;
5997 return postfix_expression;
6000 /* Parse a parenthesized expression-list.
6003 assignment-expression
6004 expression-list, assignment-expression
6009 identifier, expression-list
6011 CAST_P is true if this expression is the target of a cast.
6013 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6016 Returns a vector of trees. Each element is a representation of an
6017 assignment-expression. NULL is returned if the ( and or ) are
6018 missing. An empty, but allocated, vector is returned on no
6019 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6020 if we are parsing an attribute list for an attribute that wants a
6021 plain identifier argument, normal_attr for an attribute that wants
6022 an expression, or non_attr if we aren't parsing an attribute list. If
6023 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6024 not all of the expressions in the list were constant. */
6026 static VEC(tree,gc) *
6027 cp_parser_parenthesized_expression_list (cp_parser* parser,
6028 int is_attribute_list,
6030 bool allow_expansion_p,
6031 bool *non_constant_p)
6033 VEC(tree,gc) *expression_list;
6034 bool fold_expr_p = is_attribute_list != non_attr;
6035 tree identifier = NULL_TREE;
6036 bool saved_greater_than_is_operator_p;
6038 /* Assume all the expressions will be constant. */
6040 *non_constant_p = false;
6042 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6045 expression_list = make_tree_vector ();
6047 /* Within a parenthesized expression, a `>' token is always
6048 the greater-than operator. */
6049 saved_greater_than_is_operator_p
6050 = parser->greater_than_is_operator_p;
6051 parser->greater_than_is_operator_p = true;
6053 /* Consume expressions until there are no more. */
6054 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6059 /* At the beginning of attribute lists, check to see if the
6060 next token is an identifier. */
6061 if (is_attribute_list == id_attr
6062 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6066 /* Consume the identifier. */
6067 token = cp_lexer_consume_token (parser->lexer);
6068 /* Save the identifier. */
6069 identifier = token->u.value;
6073 bool expr_non_constant_p;
6075 /* Parse the next assignment-expression. */
6076 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6078 /* A braced-init-list. */
6079 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6080 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6081 if (non_constant_p && expr_non_constant_p)
6082 *non_constant_p = true;
6084 else if (non_constant_p)
6086 expr = (cp_parser_constant_expression
6087 (parser, /*allow_non_constant_p=*/true,
6088 &expr_non_constant_p));
6089 if (expr_non_constant_p)
6090 *non_constant_p = true;
6093 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6096 expr = fold_non_dependent_expr (expr);
6098 /* If we have an ellipsis, then this is an expression
6100 if (allow_expansion_p
6101 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6103 /* Consume the `...'. */
6104 cp_lexer_consume_token (parser->lexer);
6106 /* Build the argument pack. */
6107 expr = make_pack_expansion (expr);
6110 /* Add it to the list. We add error_mark_node
6111 expressions to the list, so that we can still tell if
6112 the correct form for a parenthesized expression-list
6113 is found. That gives better errors. */
6114 VEC_safe_push (tree, gc, expression_list, expr);
6116 if (expr == error_mark_node)
6120 /* After the first item, attribute lists look the same as
6121 expression lists. */
6122 is_attribute_list = non_attr;
6125 /* If the next token isn't a `,', then we are done. */
6126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6129 /* Otherwise, consume the `,' and keep going. */
6130 cp_lexer_consume_token (parser->lexer);
6133 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6138 /* We try and resync to an unnested comma, as that will give the
6139 user better diagnostics. */
6140 ending = cp_parser_skip_to_closing_parenthesis (parser,
6141 /*recovering=*/true,
6143 /*consume_paren=*/true);
6148 parser->greater_than_is_operator_p
6149 = saved_greater_than_is_operator_p;
6154 parser->greater_than_is_operator_p
6155 = saved_greater_than_is_operator_p;
6158 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6160 return expression_list;
6163 /* Parse a pseudo-destructor-name.
6165 pseudo-destructor-name:
6166 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6167 :: [opt] nested-name-specifier template template-id :: ~ type-name
6168 :: [opt] nested-name-specifier [opt] ~ type-name
6170 If either of the first two productions is used, sets *SCOPE to the
6171 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6172 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6173 or ERROR_MARK_NODE if the parse fails. */
6176 cp_parser_pseudo_destructor_name (cp_parser* parser,
6180 bool nested_name_specifier_p;
6182 /* Assume that things will not work out. */
6183 *type = error_mark_node;
6185 /* Look for the optional `::' operator. */
6186 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6187 /* Look for the optional nested-name-specifier. */
6188 nested_name_specifier_p
6189 = (cp_parser_nested_name_specifier_opt (parser,
6190 /*typename_keyword_p=*/false,
6191 /*check_dependency_p=*/true,
6193 /*is_declaration=*/false)
6195 /* Now, if we saw a nested-name-specifier, we might be doing the
6196 second production. */
6197 if (nested_name_specifier_p
6198 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6200 /* Consume the `template' keyword. */
6201 cp_lexer_consume_token (parser->lexer);
6202 /* Parse the template-id. */
6203 cp_parser_template_id (parser,
6204 /*template_keyword_p=*/true,
6205 /*check_dependency_p=*/false,
6206 /*is_declaration=*/true);
6207 /* Look for the `::' token. */
6208 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6210 /* If the next token is not a `~', then there might be some
6211 additional qualification. */
6212 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6214 /* At this point, we're looking for "type-name :: ~". The type-name
6215 must not be a class-name, since this is a pseudo-destructor. So,
6216 it must be either an enum-name, or a typedef-name -- both of which
6217 are just identifiers. So, we peek ahead to check that the "::"
6218 and "~" tokens are present; if they are not, then we can avoid
6219 calling type_name. */
6220 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6221 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6222 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6224 cp_parser_error (parser, "non-scalar type");
6228 /* Look for the type-name. */
6229 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6230 if (*scope == error_mark_node)
6233 /* Look for the `::' token. */
6234 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6239 /* Look for the `~'. */
6240 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6242 /* Once we see the ~, this has to be a pseudo-destructor. */
6243 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6244 cp_parser_commit_to_tentative_parse (parser);
6246 /* Look for the type-name again. We are not responsible for
6247 checking that it matches the first type-name. */
6248 *type = cp_parser_nonclass_name (parser);
6251 /* Parse a unary-expression.
6257 unary-operator cast-expression
6258 sizeof unary-expression
6260 alignof ( type-id ) [C++0x]
6267 __extension__ cast-expression
6268 __alignof__ unary-expression
6269 __alignof__ ( type-id )
6270 alignof unary-expression [C++0x]
6271 __real__ cast-expression
6272 __imag__ cast-expression
6275 ADDRESS_P is true iff the unary-expression is appearing as the
6276 operand of the `&' operator. CAST_P is true if this expression is
6277 the target of a cast.
6279 Returns a representation of the expression. */
6282 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6286 enum tree_code unary_operator;
6288 /* Peek at the next token. */
6289 token = cp_lexer_peek_token (parser->lexer);
6290 /* Some keywords give away the kind of expression. */
6291 if (token->type == CPP_KEYWORD)
6293 enum rid keyword = token->keyword;
6303 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6304 /* Consume the token. */
6305 cp_lexer_consume_token (parser->lexer);
6306 /* Parse the operand. */
6307 operand = cp_parser_sizeof_operand (parser, keyword);
6309 if (TYPE_P (operand))
6310 return cxx_sizeof_or_alignof_type (operand, op, true);
6313 /* ISO C++ defines alignof only with types, not with
6314 expressions. So pedwarn if alignof is used with a non-
6315 type expression. However, __alignof__ is ok. */
6316 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6317 pedwarn (token->location, OPT_pedantic,
6318 "ISO C++ does not allow %<alignof%> "
6321 return cxx_sizeof_or_alignof_expr (operand, op, true);
6326 return cp_parser_new_expression (parser);
6329 return cp_parser_delete_expression (parser);
6333 /* The saved value of the PEDANTIC flag. */
6337 /* Save away the PEDANTIC flag. */
6338 cp_parser_extension_opt (parser, &saved_pedantic);
6339 /* Parse the cast-expression. */
6340 expr = cp_parser_simple_cast_expression (parser);
6341 /* Restore the PEDANTIC flag. */
6342 pedantic = saved_pedantic;
6352 /* Consume the `__real__' or `__imag__' token. */
6353 cp_lexer_consume_token (parser->lexer);
6354 /* Parse the cast-expression. */
6355 expression = cp_parser_simple_cast_expression (parser);
6356 /* Create the complete representation. */
6357 return build_x_unary_op ((keyword == RID_REALPART
6358 ? REALPART_EXPR : IMAGPART_EXPR),
6360 tf_warning_or_error);
6367 const char *saved_message;
6368 bool saved_integral_constant_expression_p;
6369 bool saved_non_integral_constant_expression_p;
6370 bool saved_greater_than_is_operator_p;
6372 cp_lexer_consume_token (parser->lexer);
6373 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6375 saved_message = parser->type_definition_forbidden_message;
6376 parser->type_definition_forbidden_message
6377 = G_("types may not be defined in %<noexcept%> expressions");
6379 saved_integral_constant_expression_p
6380 = parser->integral_constant_expression_p;
6381 saved_non_integral_constant_expression_p
6382 = parser->non_integral_constant_expression_p;
6383 parser->integral_constant_expression_p = false;
6385 saved_greater_than_is_operator_p
6386 = parser->greater_than_is_operator_p;
6387 parser->greater_than_is_operator_p = true;
6389 ++cp_unevaluated_operand;
6390 ++c_inhibit_evaluation_warnings;
6391 expr = cp_parser_expression (parser, false, NULL);
6392 --c_inhibit_evaluation_warnings;
6393 --cp_unevaluated_operand;
6395 parser->greater_than_is_operator_p
6396 = saved_greater_than_is_operator_p;
6398 parser->integral_constant_expression_p
6399 = saved_integral_constant_expression_p;
6400 parser->non_integral_constant_expression_p
6401 = saved_non_integral_constant_expression_p;
6403 parser->type_definition_forbidden_message = saved_message;
6405 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6406 return finish_noexcept_expr (expr, tf_warning_or_error);
6414 /* Look for the `:: new' and `:: delete', which also signal the
6415 beginning of a new-expression, or delete-expression,
6416 respectively. If the next token is `::', then it might be one of
6418 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6422 /* See if the token after the `::' is one of the keywords in
6423 which we're interested. */
6424 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6425 /* If it's `new', we have a new-expression. */
6426 if (keyword == RID_NEW)
6427 return cp_parser_new_expression (parser);
6428 /* Similarly, for `delete'. */
6429 else if (keyword == RID_DELETE)
6430 return cp_parser_delete_expression (parser);
6433 /* Look for a unary operator. */
6434 unary_operator = cp_parser_unary_operator (token);
6435 /* The `++' and `--' operators can be handled similarly, even though
6436 they are not technically unary-operators in the grammar. */
6437 if (unary_operator == ERROR_MARK)
6439 if (token->type == CPP_PLUS_PLUS)
6440 unary_operator = PREINCREMENT_EXPR;
6441 else if (token->type == CPP_MINUS_MINUS)
6442 unary_operator = PREDECREMENT_EXPR;
6443 /* Handle the GNU address-of-label extension. */
6444 else if (cp_parser_allow_gnu_extensions_p (parser)
6445 && token->type == CPP_AND_AND)
6449 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6451 /* Consume the '&&' token. */
6452 cp_lexer_consume_token (parser->lexer);
6453 /* Look for the identifier. */
6454 identifier = cp_parser_identifier (parser);
6455 /* Create an expression representing the address. */
6456 expression = finish_label_address_expr (identifier, loc);
6457 if (cp_parser_non_integral_constant_expression (parser,
6459 expression = error_mark_node;
6463 if (unary_operator != ERROR_MARK)
6465 tree cast_expression;
6466 tree expression = error_mark_node;
6467 non_integral_constant non_constant_p = NIC_NONE;
6469 /* Consume the operator token. */
6470 token = cp_lexer_consume_token (parser->lexer);
6471 /* Parse the cast-expression. */
6473 = cp_parser_cast_expression (parser,
6474 unary_operator == ADDR_EXPR,
6475 /*cast_p=*/false, pidk);
6476 /* Now, build an appropriate representation. */
6477 switch (unary_operator)
6480 non_constant_p = NIC_STAR;
6481 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6482 tf_warning_or_error);
6486 non_constant_p = NIC_ADDR;
6489 expression = build_x_unary_op (unary_operator, cast_expression,
6490 tf_warning_or_error);
6493 case PREINCREMENT_EXPR:
6494 case PREDECREMENT_EXPR:
6495 non_constant_p = unary_operator == PREINCREMENT_EXPR
6496 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6498 case UNARY_PLUS_EXPR:
6500 case TRUTH_NOT_EXPR:
6501 expression = finish_unary_op_expr (unary_operator, cast_expression);
6508 if (non_constant_p != NIC_NONE
6509 && cp_parser_non_integral_constant_expression (parser,
6511 expression = error_mark_node;
6516 return cp_parser_postfix_expression (parser, address_p, cast_p,
6517 /*member_access_only_p=*/false,
6521 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6522 unary-operator, the corresponding tree code is returned. */
6524 static enum tree_code
6525 cp_parser_unary_operator (cp_token* token)
6527 switch (token->type)
6530 return INDIRECT_REF;
6536 return UNARY_PLUS_EXPR;
6542 return TRUTH_NOT_EXPR;
6545 return BIT_NOT_EXPR;
6552 /* Parse a new-expression.
6555 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6556 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6558 Returns a representation of the expression. */
6561 cp_parser_new_expression (cp_parser* parser)
6563 bool global_scope_p;
6564 VEC(tree,gc) *placement;
6566 VEC(tree,gc) *initializer;
6570 /* Look for the optional `::' operator. */
6572 = (cp_parser_global_scope_opt (parser,
6573 /*current_scope_valid_p=*/false)
6575 /* Look for the `new' operator. */
6576 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6577 /* There's no easy way to tell a new-placement from the
6578 `( type-id )' construct. */
6579 cp_parser_parse_tentatively (parser);
6580 /* Look for a new-placement. */
6581 placement = cp_parser_new_placement (parser);
6582 /* If that didn't work out, there's no new-placement. */
6583 if (!cp_parser_parse_definitely (parser))
6585 if (placement != NULL)
6586 release_tree_vector (placement);
6590 /* If the next token is a `(', then we have a parenthesized
6592 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6595 /* Consume the `('. */
6596 cp_lexer_consume_token (parser->lexer);
6597 /* Parse the type-id. */
6598 type = cp_parser_type_id (parser);
6599 /* Look for the closing `)'. */
6600 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6601 token = cp_lexer_peek_token (parser->lexer);
6602 /* There should not be a direct-new-declarator in this production,
6603 but GCC used to allowed this, so we check and emit a sensible error
6604 message for this case. */
6605 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6607 error_at (token->location,
6608 "array bound forbidden after parenthesized type-id");
6609 inform (token->location,
6610 "try removing the parentheses around the type-id");
6611 cp_parser_direct_new_declarator (parser);
6615 /* Otherwise, there must be a new-type-id. */
6617 type = cp_parser_new_type_id (parser, &nelts);
6619 /* If the next token is a `(' or '{', then we have a new-initializer. */
6620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6621 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6622 initializer = cp_parser_new_initializer (parser);
6626 /* A new-expression may not appear in an integral constant
6628 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6629 ret = error_mark_node;
6632 /* Create a representation of the new-expression. */
6633 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6634 tf_warning_or_error);
6637 if (placement != NULL)
6638 release_tree_vector (placement);
6639 if (initializer != NULL)
6640 release_tree_vector (initializer);
6645 /* Parse a new-placement.
6650 Returns the same representation as for an expression-list. */
6652 static VEC(tree,gc) *
6653 cp_parser_new_placement (cp_parser* parser)
6655 VEC(tree,gc) *expression_list;
6657 /* Parse the expression-list. */
6658 expression_list = (cp_parser_parenthesized_expression_list
6659 (parser, non_attr, /*cast_p=*/false,
6660 /*allow_expansion_p=*/true,
6661 /*non_constant_p=*/NULL));
6663 return expression_list;
6666 /* Parse a new-type-id.
6669 type-specifier-seq new-declarator [opt]
6671 Returns the TYPE allocated. If the new-type-id indicates an array
6672 type, *NELTS is set to the number of elements in the last array
6673 bound; the TYPE will not include the last array bound. */
6676 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6678 cp_decl_specifier_seq type_specifier_seq;
6679 cp_declarator *new_declarator;
6680 cp_declarator *declarator;
6681 cp_declarator *outer_declarator;
6682 const char *saved_message;
6685 /* The type-specifier sequence must not contain type definitions.
6686 (It cannot contain declarations of new types either, but if they
6687 are not definitions we will catch that because they are not
6689 saved_message = parser->type_definition_forbidden_message;
6690 parser->type_definition_forbidden_message
6691 = G_("types may not be defined in a new-type-id");
6692 /* Parse the type-specifier-seq. */
6693 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6694 /*is_trailing_return=*/false,
6695 &type_specifier_seq);
6696 /* Restore the old message. */
6697 parser->type_definition_forbidden_message = saved_message;
6698 /* Parse the new-declarator. */
6699 new_declarator = cp_parser_new_declarator_opt (parser);
6701 /* Determine the number of elements in the last array dimension, if
6704 /* Skip down to the last array dimension. */
6705 declarator = new_declarator;
6706 outer_declarator = NULL;
6707 while (declarator && (declarator->kind == cdk_pointer
6708 || declarator->kind == cdk_ptrmem))
6710 outer_declarator = declarator;
6711 declarator = declarator->declarator;
6714 && declarator->kind == cdk_array
6715 && declarator->declarator
6716 && declarator->declarator->kind == cdk_array)
6718 outer_declarator = declarator;
6719 declarator = declarator->declarator;
6722 if (declarator && declarator->kind == cdk_array)
6724 *nelts = declarator->u.array.bounds;
6725 if (*nelts == error_mark_node)
6726 *nelts = integer_one_node;
6728 if (outer_declarator)
6729 outer_declarator->declarator = declarator->declarator;
6731 new_declarator = NULL;
6734 type = groktypename (&type_specifier_seq, new_declarator, false);
6738 /* Parse an (optional) new-declarator.
6741 ptr-operator new-declarator [opt]
6742 direct-new-declarator
6744 Returns the declarator. */
6746 static cp_declarator *
6747 cp_parser_new_declarator_opt (cp_parser* parser)
6749 enum tree_code code;
6751 cp_cv_quals cv_quals;
6753 /* We don't know if there's a ptr-operator next, or not. */
6754 cp_parser_parse_tentatively (parser);
6755 /* Look for a ptr-operator. */
6756 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6757 /* If that worked, look for more new-declarators. */
6758 if (cp_parser_parse_definitely (parser))
6760 cp_declarator *declarator;
6762 /* Parse another optional declarator. */
6763 declarator = cp_parser_new_declarator_opt (parser);
6765 return cp_parser_make_indirect_declarator
6766 (code, type, cv_quals, declarator);
6769 /* If the next token is a `[', there is a direct-new-declarator. */
6770 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6771 return cp_parser_direct_new_declarator (parser);
6776 /* Parse a direct-new-declarator.
6778 direct-new-declarator:
6780 direct-new-declarator [constant-expression]
6784 static cp_declarator *
6785 cp_parser_direct_new_declarator (cp_parser* parser)
6787 cp_declarator *declarator = NULL;
6793 /* Look for the opening `['. */
6794 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6795 /* The first expression is not required to be constant. */
6798 cp_token *token = cp_lexer_peek_token (parser->lexer);
6799 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6800 /* The standard requires that the expression have integral
6801 type. DR 74 adds enumeration types. We believe that the
6802 real intent is that these expressions be handled like the
6803 expression in a `switch' condition, which also allows
6804 classes with a single conversion to integral or
6805 enumeration type. */
6806 if (!processing_template_decl)
6809 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6814 error_at (token->location,
6815 "expression in new-declarator must have integral "
6816 "or enumeration type");
6817 expression = error_mark_node;
6821 /* But all the other expressions must be. */
6824 = cp_parser_constant_expression (parser,
6825 /*allow_non_constant=*/false,
6827 /* Look for the closing `]'. */
6828 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6830 /* Add this bound to the declarator. */
6831 declarator = make_array_declarator (declarator, expression);
6833 /* If the next token is not a `[', then there are no more
6835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6842 /* Parse a new-initializer.
6845 ( expression-list [opt] )
6848 Returns a representation of the expression-list. */
6850 static VEC(tree,gc) *
6851 cp_parser_new_initializer (cp_parser* parser)
6853 VEC(tree,gc) *expression_list;
6855 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6858 bool expr_non_constant_p;
6859 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6860 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6861 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6862 expression_list = make_tree_vector_single (t);
6865 expression_list = (cp_parser_parenthesized_expression_list
6866 (parser, non_attr, /*cast_p=*/false,
6867 /*allow_expansion_p=*/true,
6868 /*non_constant_p=*/NULL));
6870 return expression_list;
6873 /* Parse a delete-expression.
6876 :: [opt] delete cast-expression
6877 :: [opt] delete [ ] cast-expression
6879 Returns a representation of the expression. */
6882 cp_parser_delete_expression (cp_parser* parser)
6884 bool global_scope_p;
6888 /* Look for the optional `::' operator. */
6890 = (cp_parser_global_scope_opt (parser,
6891 /*current_scope_valid_p=*/false)
6893 /* Look for the `delete' keyword. */
6894 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6895 /* See if the array syntax is in use. */
6896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6898 /* Consume the `[' token. */
6899 cp_lexer_consume_token (parser->lexer);
6900 /* Look for the `]' token. */
6901 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6902 /* Remember that this is the `[]' construct. */
6908 /* Parse the cast-expression. */
6909 expression = cp_parser_simple_cast_expression (parser);
6911 /* A delete-expression may not appear in an integral constant
6913 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6914 return error_mark_node;
6916 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6917 tf_warning_or_error);
6920 /* Returns true if TOKEN may start a cast-expression and false
6924 cp_parser_token_starts_cast_expression (cp_token *token)
6926 switch (token->type)
6932 case CPP_CLOSE_SQUARE:
6933 case CPP_CLOSE_PAREN:
6934 case CPP_CLOSE_BRACE:
6938 case CPP_DEREF_STAR:
6946 case CPP_GREATER_EQ:
6966 /* '[' may start a primary-expression in obj-c++. */
6967 case CPP_OPEN_SQUARE:
6968 return c_dialect_objc ();
6975 /* Parse a cast-expression.
6979 ( type-id ) cast-expression
6981 ADDRESS_P is true iff the unary-expression is appearing as the
6982 operand of the `&' operator. CAST_P is true if this expression is
6983 the target of a cast.
6985 Returns a representation of the expression. */
6988 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6991 /* If it's a `(', then we might be looking at a cast. */
6992 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6994 tree type = NULL_TREE;
6995 tree expr = NULL_TREE;
6996 bool compound_literal_p;
6997 const char *saved_message;
6999 /* There's no way to know yet whether or not this is a cast.
7000 For example, `(int (3))' is a unary-expression, while `(int)
7001 3' is a cast. So, we resort to parsing tentatively. */
7002 cp_parser_parse_tentatively (parser);
7003 /* Types may not be defined in a cast. */
7004 saved_message = parser->type_definition_forbidden_message;
7005 parser->type_definition_forbidden_message
7006 = G_("types may not be defined in casts");
7007 /* Consume the `('. */
7008 cp_lexer_consume_token (parser->lexer);
7009 /* A very tricky bit is that `(struct S) { 3 }' is a
7010 compound-literal (which we permit in C++ as an extension).
7011 But, that construct is not a cast-expression -- it is a
7012 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7013 is legal; if the compound-literal were a cast-expression,
7014 you'd need an extra set of parentheses.) But, if we parse
7015 the type-id, and it happens to be a class-specifier, then we
7016 will commit to the parse at that point, because we cannot
7017 undo the action that is done when creating a new class. So,
7018 then we cannot back up and do a postfix-expression.
7020 Therefore, we scan ahead to the closing `)', and check to see
7021 if the token after the `)' is a `{'. If so, we are not
7022 looking at a cast-expression.
7024 Save tokens so that we can put them back. */
7025 cp_lexer_save_tokens (parser->lexer);
7026 /* Skip tokens until the next token is a closing parenthesis.
7027 If we find the closing `)', and the next token is a `{', then
7028 we are looking at a compound-literal. */
7030 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7031 /*consume_paren=*/true)
7032 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7033 /* Roll back the tokens we skipped. */
7034 cp_lexer_rollback_tokens (parser->lexer);
7035 /* If we were looking at a compound-literal, simulate an error
7036 so that the call to cp_parser_parse_definitely below will
7038 if (compound_literal_p)
7039 cp_parser_simulate_error (parser);
7042 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7043 parser->in_type_id_in_expr_p = true;
7044 /* Look for the type-id. */
7045 type = cp_parser_type_id (parser);
7046 /* Look for the closing `)'. */
7047 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7048 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7051 /* Restore the saved message. */
7052 parser->type_definition_forbidden_message = saved_message;
7054 /* At this point this can only be either a cast or a
7055 parenthesized ctor such as `(T ())' that looks like a cast to
7056 function returning T. */
7057 if (!cp_parser_error_occurred (parser)
7058 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7061 cp_parser_parse_definitely (parser);
7062 expr = cp_parser_cast_expression (parser,
7063 /*address_p=*/false,
7064 /*cast_p=*/true, pidk);
7066 /* Warn about old-style casts, if so requested. */
7067 if (warn_old_style_cast
7068 && !in_system_header
7069 && !VOID_TYPE_P (type)
7070 && current_lang_name != lang_name_c)
7071 warning (OPT_Wold_style_cast, "use of old-style cast");
7073 /* Only type conversions to integral or enumeration types
7074 can be used in constant-expressions. */
7075 if (!cast_valid_in_integral_constant_expression_p (type)
7076 && cp_parser_non_integral_constant_expression (parser,
7078 return error_mark_node;
7080 /* Perform the cast. */
7081 expr = build_c_cast (input_location, type, expr);
7085 cp_parser_abort_tentative_parse (parser);
7088 /* If we get here, then it's not a cast, so it must be a
7089 unary-expression. */
7090 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7093 /* Parse a binary expression of the general form:
7097 pm-expression .* cast-expression
7098 pm-expression ->* cast-expression
7100 multiplicative-expression:
7102 multiplicative-expression * pm-expression
7103 multiplicative-expression / pm-expression
7104 multiplicative-expression % pm-expression
7106 additive-expression:
7107 multiplicative-expression
7108 additive-expression + multiplicative-expression
7109 additive-expression - multiplicative-expression
7113 shift-expression << additive-expression
7114 shift-expression >> additive-expression
7116 relational-expression:
7118 relational-expression < shift-expression
7119 relational-expression > shift-expression
7120 relational-expression <= shift-expression
7121 relational-expression >= shift-expression
7125 relational-expression:
7126 relational-expression <? shift-expression
7127 relational-expression >? shift-expression
7129 equality-expression:
7130 relational-expression
7131 equality-expression == relational-expression
7132 equality-expression != relational-expression
7136 and-expression & equality-expression
7138 exclusive-or-expression:
7140 exclusive-or-expression ^ and-expression
7142 inclusive-or-expression:
7143 exclusive-or-expression
7144 inclusive-or-expression | exclusive-or-expression
7146 logical-and-expression:
7147 inclusive-or-expression
7148 logical-and-expression && inclusive-or-expression
7150 logical-or-expression:
7151 logical-and-expression
7152 logical-or-expression || logical-and-expression
7154 All these are implemented with a single function like:
7157 simple-cast-expression
7158 binary-expression <token> binary-expression
7160 CAST_P is true if this expression is the target of a cast.
7162 The binops_by_token map is used to get the tree codes for each <token> type.
7163 binary-expressions are associated according to a precedence table. */
7165 #define TOKEN_PRECEDENCE(token) \
7166 (((token->type == CPP_GREATER \
7167 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7168 && !parser->greater_than_is_operator_p) \
7169 ? PREC_NOT_OPERATOR \
7170 : binops_by_token[token->type].prec)
7173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7174 bool no_toplevel_fold_p,
7175 enum cp_parser_prec prec,
7178 cp_parser_expression_stack stack;
7179 cp_parser_expression_stack_entry *sp = &stack[0];
7182 enum tree_code tree_type, lhs_type, rhs_type;
7183 enum cp_parser_prec new_prec, lookahead_prec;
7186 /* Parse the first expression. */
7187 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7188 lhs_type = ERROR_MARK;
7192 /* Get an operator token. */
7193 token = cp_lexer_peek_token (parser->lexer);
7195 if (warn_cxx0x_compat
7196 && token->type == CPP_RSHIFT
7197 && !parser->greater_than_is_operator_p)
7199 if (warning_at (token->location, OPT_Wc__0x_compat,
7200 "%<>>%> operator will be treated as"
7201 " two right angle brackets in C++0x"))
7202 inform (token->location,
7203 "suggest parentheses around %<>>%> expression");
7206 new_prec = TOKEN_PRECEDENCE (token);
7208 /* Popping an entry off the stack means we completed a subexpression:
7209 - either we found a token which is not an operator (`>' where it is not
7210 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7211 will happen repeatedly;
7212 - or, we found an operator which has lower priority. This is the case
7213 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7215 if (new_prec <= prec)
7224 tree_type = binops_by_token[token->type].tree_type;
7226 /* We used the operator token. */
7227 cp_lexer_consume_token (parser->lexer);
7229 /* For "false && x" or "true || x", x will never be executed;
7230 disable warnings while evaluating it. */
7231 if (tree_type == TRUTH_ANDIF_EXPR)
7232 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7233 else if (tree_type == TRUTH_ORIF_EXPR)
7234 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7236 /* Extract another operand. It may be the RHS of this expression
7237 or the LHS of a new, higher priority expression. */
7238 rhs = cp_parser_simple_cast_expression (parser);
7239 rhs_type = ERROR_MARK;
7241 /* Get another operator token. Look up its precedence to avoid
7242 building a useless (immediately popped) stack entry for common
7243 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7244 token = cp_lexer_peek_token (parser->lexer);
7245 lookahead_prec = TOKEN_PRECEDENCE (token);
7246 if (lookahead_prec > new_prec)
7248 /* ... and prepare to parse the RHS of the new, higher priority
7249 expression. Since precedence levels on the stack are
7250 monotonically increasing, we do not have to care about
7253 sp->tree_type = tree_type;
7255 sp->lhs_type = lhs_type;
7258 lhs_type = rhs_type;
7260 new_prec = lookahead_prec;
7264 lookahead_prec = new_prec;
7265 /* If the stack is not empty, we have parsed into LHS the right side
7266 (`4' in the example above) of an expression we had suspended.
7267 We can use the information on the stack to recover the LHS (`3')
7268 from the stack together with the tree code (`MULT_EXPR'), and
7269 the precedence of the higher level subexpression
7270 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7271 which will be used to actually build the additive expression. */
7274 tree_type = sp->tree_type;
7276 rhs_type = lhs_type;
7278 lhs_type = sp->lhs_type;
7281 /* Undo the disabling of warnings done above. */
7282 if (tree_type == TRUTH_ANDIF_EXPR)
7283 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7284 else if (tree_type == TRUTH_ORIF_EXPR)
7285 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7288 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7289 ERROR_MARK for everything that is not a binary expression.
7290 This makes warn_about_parentheses miss some warnings that
7291 involve unary operators. For unary expressions we should
7292 pass the correct tree_code unless the unary expression was
7293 surrounded by parentheses.
7295 if (no_toplevel_fold_p
7296 && lookahead_prec <= prec
7298 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7299 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7301 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7302 &overload, tf_warning_or_error);
7303 lhs_type = tree_type;
7305 /* If the binary operator required the use of an overloaded operator,
7306 then this expression cannot be an integral constant-expression.
7307 An overloaded operator can be used even if both operands are
7308 otherwise permissible in an integral constant-expression if at
7309 least one of the operands is of enumeration type. */
7312 && cp_parser_non_integral_constant_expression (parser,
7314 return error_mark_node;
7321 /* Parse the `? expression : assignment-expression' part of a
7322 conditional-expression. The LOGICAL_OR_EXPR is the
7323 logical-or-expression that started the conditional-expression.
7324 Returns a representation of the entire conditional-expression.
7326 This routine is used by cp_parser_assignment_expression.
7328 ? expression : assignment-expression
7332 ? : assignment-expression */
7335 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7338 tree assignment_expr;
7339 struct cp_token *token;
7341 /* Consume the `?' token. */
7342 cp_lexer_consume_token (parser->lexer);
7343 token = cp_lexer_peek_token (parser->lexer);
7344 if (cp_parser_allow_gnu_extensions_p (parser)
7345 && token->type == CPP_COLON)
7347 pedwarn (token->location, OPT_pedantic,
7348 "ISO C++ does not allow ?: with omitted middle operand");
7349 /* Implicit true clause. */
7351 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7352 warn_for_omitted_condop (token->location, logical_or_expr);
7356 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7357 parser->colon_corrects_to_scope_p = false;
7358 /* Parse the expression. */
7359 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7360 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7361 c_inhibit_evaluation_warnings +=
7362 ((logical_or_expr == truthvalue_true_node)
7363 - (logical_or_expr == truthvalue_false_node));
7364 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7367 /* The next token should be a `:'. */
7368 cp_parser_require (parser, CPP_COLON, RT_COLON);
7369 /* Parse the assignment-expression. */
7370 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7371 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7373 /* Build the conditional-expression. */
7374 return build_x_conditional_expr (logical_or_expr,
7377 tf_warning_or_error);
7380 /* Parse an assignment-expression.
7382 assignment-expression:
7383 conditional-expression
7384 logical-or-expression assignment-operator assignment_expression
7387 CAST_P is true if this expression is the target of a cast.
7389 Returns a representation for the expression. */
7392 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7397 /* If the next token is the `throw' keyword, then we're looking at
7398 a throw-expression. */
7399 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7400 expr = cp_parser_throw_expression (parser);
7401 /* Otherwise, it must be that we are looking at a
7402 logical-or-expression. */
7405 /* Parse the binary expressions (logical-or-expression). */
7406 expr = cp_parser_binary_expression (parser, cast_p, false,
7407 PREC_NOT_OPERATOR, pidk);
7408 /* If the next token is a `?' then we're actually looking at a
7409 conditional-expression. */
7410 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7411 return cp_parser_question_colon_clause (parser, expr);
7414 enum tree_code assignment_operator;
7416 /* If it's an assignment-operator, we're using the second
7419 = cp_parser_assignment_operator_opt (parser);
7420 if (assignment_operator != ERROR_MARK)
7422 bool non_constant_p;
7424 /* Parse the right-hand side of the assignment. */
7425 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7427 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7430 /* An assignment may not appear in a
7431 constant-expression. */
7432 if (cp_parser_non_integral_constant_expression (parser,
7434 return error_mark_node;
7435 /* Build the assignment expression. */
7436 expr = build_x_modify_expr (expr,
7437 assignment_operator,
7439 tf_warning_or_error);
7447 /* Parse an (optional) assignment-operator.
7449 assignment-operator: one of
7450 = *= /= %= += -= >>= <<= &= ^= |=
7454 assignment-operator: one of
7457 If the next token is an assignment operator, the corresponding tree
7458 code is returned, and the token is consumed. For example, for
7459 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7460 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7461 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7462 operator, ERROR_MARK is returned. */
7464 static enum tree_code
7465 cp_parser_assignment_operator_opt (cp_parser* parser)
7470 /* Peek at the next token. */
7471 token = cp_lexer_peek_token (parser->lexer);
7473 switch (token->type)
7484 op = TRUNC_DIV_EXPR;
7488 op = TRUNC_MOD_EXPR;
7520 /* Nothing else is an assignment operator. */
7524 /* If it was an assignment operator, consume it. */
7525 if (op != ERROR_MARK)
7526 cp_lexer_consume_token (parser->lexer);
7531 /* Parse an expression.
7534 assignment-expression
7535 expression , assignment-expression
7537 CAST_P is true if this expression is the target of a cast.
7539 Returns a representation of the expression. */
7542 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7544 tree expression = NULL_TREE;
7548 tree assignment_expression;
7550 /* Parse the next assignment-expression. */
7551 assignment_expression
7552 = cp_parser_assignment_expression (parser, cast_p, pidk);
7553 /* If this is the first assignment-expression, we can just
7556 expression = assignment_expression;
7558 expression = build_x_compound_expr (expression,
7559 assignment_expression,
7560 tf_warning_or_error);
7561 /* If the next token is not a comma, then we are done with the
7563 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7565 /* Consume the `,'. */
7566 cp_lexer_consume_token (parser->lexer);
7567 /* A comma operator cannot appear in a constant-expression. */
7568 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7569 expression = error_mark_node;
7575 /* Parse a constant-expression.
7577 constant-expression:
7578 conditional-expression
7580 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7581 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7582 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7583 is false, NON_CONSTANT_P should be NULL. */
7586 cp_parser_constant_expression (cp_parser* parser,
7587 bool allow_non_constant_p,
7588 bool *non_constant_p)
7590 bool saved_integral_constant_expression_p;
7591 bool saved_allow_non_integral_constant_expression_p;
7592 bool saved_non_integral_constant_expression_p;
7595 /* It might seem that we could simply parse the
7596 conditional-expression, and then check to see if it were
7597 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7598 one that the compiler can figure out is constant, possibly after
7599 doing some simplifications or optimizations. The standard has a
7600 precise definition of constant-expression, and we must honor
7601 that, even though it is somewhat more restrictive.
7607 is not a legal declaration, because `(2, 3)' is not a
7608 constant-expression. The `,' operator is forbidden in a
7609 constant-expression. However, GCC's constant-folding machinery
7610 will fold this operation to an INTEGER_CST for `3'. */
7612 /* Save the old settings. */
7613 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7614 saved_allow_non_integral_constant_expression_p
7615 = parser->allow_non_integral_constant_expression_p;
7616 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7617 /* We are now parsing a constant-expression. */
7618 parser->integral_constant_expression_p = true;
7619 parser->allow_non_integral_constant_expression_p
7620 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7621 parser->non_integral_constant_expression_p = false;
7622 /* Although the grammar says "conditional-expression", we parse an
7623 "assignment-expression", which also permits "throw-expression"
7624 and the use of assignment operators. In the case that
7625 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7626 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7627 actually essential that we look for an assignment-expression.
7628 For example, cp_parser_initializer_clauses uses this function to
7629 determine whether a particular assignment-expression is in fact
7631 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7632 /* Restore the old settings. */
7633 parser->integral_constant_expression_p
7634 = saved_integral_constant_expression_p;
7635 parser->allow_non_integral_constant_expression_p
7636 = saved_allow_non_integral_constant_expression_p;
7637 if (cxx_dialect >= cxx0x)
7639 /* Require an rvalue constant expression here; that's what our
7640 callers expect. Reference constant expressions are handled
7641 separately in e.g. cp_parser_template_argument. */
7642 bool is_const = potential_rvalue_constant_expression (expression);
7643 parser->non_integral_constant_expression_p = !is_const;
7644 if (!is_const && !allow_non_constant_p)
7645 require_potential_rvalue_constant_expression (expression);
7647 if (allow_non_constant_p)
7648 *non_constant_p = parser->non_integral_constant_expression_p;
7649 parser->non_integral_constant_expression_p
7650 = saved_non_integral_constant_expression_p;
7655 /* Parse __builtin_offsetof.
7657 offsetof-expression:
7658 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7660 offsetof-member-designator:
7662 | offsetof-member-designator "." id-expression
7663 | offsetof-member-designator "[" expression "]"
7664 | offsetof-member-designator "->" id-expression */
7667 cp_parser_builtin_offsetof (cp_parser *parser)
7669 int save_ice_p, save_non_ice_p;
7674 /* We're about to accept non-integral-constant things, but will
7675 definitely yield an integral constant expression. Save and
7676 restore these values around our local parsing. */
7677 save_ice_p = parser->integral_constant_expression_p;
7678 save_non_ice_p = parser->non_integral_constant_expression_p;
7680 /* Consume the "__builtin_offsetof" token. */
7681 cp_lexer_consume_token (parser->lexer);
7682 /* Consume the opening `('. */
7683 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7684 /* Parse the type-id. */
7685 type = cp_parser_type_id (parser);
7686 /* Look for the `,'. */
7687 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7688 token = cp_lexer_peek_token (parser->lexer);
7690 /* Build the (type *)null that begins the traditional offsetof macro. */
7691 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7692 tf_warning_or_error);
7694 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7695 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7696 true, &dummy, token->location);
7699 token = cp_lexer_peek_token (parser->lexer);
7700 switch (token->type)
7702 case CPP_OPEN_SQUARE:
7703 /* offsetof-member-designator "[" expression "]" */
7704 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7708 /* offsetof-member-designator "->" identifier */
7709 expr = grok_array_decl (expr, integer_zero_node);
7713 /* offsetof-member-designator "." identifier */
7714 cp_lexer_consume_token (parser->lexer);
7715 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7720 case CPP_CLOSE_PAREN:
7721 /* Consume the ")" token. */
7722 cp_lexer_consume_token (parser->lexer);
7726 /* Error. We know the following require will fail, but
7727 that gives the proper error message. */
7728 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7729 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7730 expr = error_mark_node;
7736 /* If we're processing a template, we can't finish the semantics yet.
7737 Otherwise we can fold the entire expression now. */
7738 if (processing_template_decl)
7739 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7741 expr = finish_offsetof (expr);
7744 parser->integral_constant_expression_p = save_ice_p;
7745 parser->non_integral_constant_expression_p = save_non_ice_p;
7750 /* Parse a trait expression.
7752 Returns a representation of the expression, the underlying type
7753 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7756 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7759 tree type1, type2 = NULL_TREE;
7760 bool binary = false;
7761 cp_decl_specifier_seq decl_specs;
7765 case RID_HAS_NOTHROW_ASSIGN:
7766 kind = CPTK_HAS_NOTHROW_ASSIGN;
7768 case RID_HAS_NOTHROW_CONSTRUCTOR:
7769 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7771 case RID_HAS_NOTHROW_COPY:
7772 kind = CPTK_HAS_NOTHROW_COPY;
7774 case RID_HAS_TRIVIAL_ASSIGN:
7775 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7777 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7778 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7780 case RID_HAS_TRIVIAL_COPY:
7781 kind = CPTK_HAS_TRIVIAL_COPY;
7783 case RID_HAS_TRIVIAL_DESTRUCTOR:
7784 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7786 case RID_HAS_VIRTUAL_DESTRUCTOR:
7787 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7789 case RID_IS_ABSTRACT:
7790 kind = CPTK_IS_ABSTRACT;
7792 case RID_IS_BASE_OF:
7793 kind = CPTK_IS_BASE_OF;
7797 kind = CPTK_IS_CLASS;
7799 case RID_IS_CONVERTIBLE_TO:
7800 kind = CPTK_IS_CONVERTIBLE_TO;
7804 kind = CPTK_IS_EMPTY;
7807 kind = CPTK_IS_ENUM;
7809 case RID_IS_LITERAL_TYPE:
7810 kind = CPTK_IS_LITERAL_TYPE;
7815 case RID_IS_POLYMORPHIC:
7816 kind = CPTK_IS_POLYMORPHIC;
7818 case RID_IS_STD_LAYOUT:
7819 kind = CPTK_IS_STD_LAYOUT;
7821 case RID_IS_TRIVIAL:
7822 kind = CPTK_IS_TRIVIAL;
7825 kind = CPTK_IS_UNION;
7827 case RID_UNDERLYING_TYPE:
7828 kind = CPTK_UNDERLYING_TYPE;
7833 case RID_DIRECT_BASES:
7834 kind = CPTK_DIRECT_BASES;
7840 /* Consume the token. */
7841 cp_lexer_consume_token (parser->lexer);
7843 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7845 type1 = cp_parser_type_id (parser);
7847 if (type1 == error_mark_node)
7848 return error_mark_node;
7850 /* Build a trivial decl-specifier-seq. */
7851 clear_decl_specs (&decl_specs);
7852 decl_specs.type = type1;
7854 /* Call grokdeclarator to figure out what type this is. */
7855 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7856 /*initialized=*/0, /*attrlist=*/NULL);
7860 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7862 type2 = cp_parser_type_id (parser);
7864 if (type2 == error_mark_node)
7865 return error_mark_node;
7867 /* Build a trivial decl-specifier-seq. */
7868 clear_decl_specs (&decl_specs);
7869 decl_specs.type = type2;
7871 /* Call grokdeclarator to figure out what type this is. */
7872 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7873 /*initialized=*/0, /*attrlist=*/NULL);
7876 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7878 /* Complete the trait expression, which may mean either processing
7879 the trait expr now or saving it for template instantiation. */
7882 case CPTK_UNDERLYING_TYPE:
7883 return finish_underlying_type (type1);
7885 return finish_bases (type1, false);
7886 case CPTK_DIRECT_BASES:
7887 return finish_bases (type1, true);
7889 return finish_trait_expr (kind, type1, type2);
7893 /* Lambdas that appear in variable initializer or default argument scope
7894 get that in their mangling, so we need to record it. We might as well
7895 use the count for function and namespace scopes as well. */
7896 static GTY(()) tree lambda_scope;
7897 static GTY(()) int lambda_count;
7898 typedef struct GTY(()) tree_int
7903 DEF_VEC_O(tree_int);
7904 DEF_VEC_ALLOC_O(tree_int,gc);
7905 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7908 start_lambda_scope (tree decl)
7912 /* Once we're inside a function, we ignore other scopes and just push
7913 the function again so that popping works properly. */
7914 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7915 decl = current_function_decl;
7916 ti.t = lambda_scope;
7917 ti.i = lambda_count;
7918 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7919 if (lambda_scope != decl)
7921 /* Don't reset the count if we're still in the same function. */
7922 lambda_scope = decl;
7928 record_lambda_scope (tree lambda)
7930 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7931 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7935 finish_lambda_scope (void)
7937 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7938 if (lambda_scope != p->t)
7940 lambda_scope = p->t;
7941 lambda_count = p->i;
7943 VEC_pop (tree_int, lambda_scope_stack);
7946 /* Parse a lambda expression.
7949 lambda-introducer lambda-declarator [opt] compound-statement
7951 Returns a representation of the expression. */
7954 cp_parser_lambda_expression (cp_parser* parser)
7956 tree lambda_expr = build_lambda_expr ();
7960 LAMBDA_EXPR_LOCATION (lambda_expr)
7961 = cp_lexer_peek_token (parser->lexer)->location;
7963 if (cp_unevaluated_operand)
7964 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7965 "lambda-expression in unevaluated context");
7967 /* We may be in the middle of deferred access check. Disable
7969 push_deferring_access_checks (dk_no_deferred);
7971 cp_parser_lambda_introducer (parser, lambda_expr);
7973 type = begin_lambda_type (lambda_expr);
7975 record_lambda_scope (lambda_expr);
7977 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7978 determine_visibility (TYPE_NAME (type));
7980 /* Now that we've started the type, add the capture fields for any
7981 explicit captures. */
7982 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7985 /* Inside the class, surrounding template-parameter-lists do not apply. */
7986 unsigned int saved_num_template_parameter_lists
7987 = parser->num_template_parameter_lists;
7988 unsigned char in_statement = parser->in_statement;
7989 bool in_switch_statement_p = parser->in_switch_statement_p;
7991 parser->num_template_parameter_lists = 0;
7992 parser->in_statement = 0;
7993 parser->in_switch_statement_p = false;
7995 /* By virtue of defining a local class, a lambda expression has access to
7996 the private variables of enclosing classes. */
7998 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8001 cp_parser_lambda_body (parser, lambda_expr);
8002 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8003 cp_parser_skip_to_end_of_block_or_statement (parser);
8005 /* The capture list was built up in reverse order; fix that now. */
8007 tree newlist = NULL_TREE;
8010 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8013 next = TREE_CHAIN (elt);
8014 TREE_CHAIN (elt) = newlist;
8017 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8021 maybe_add_lambda_conv_op (type);
8023 type = finish_struct (type, /*attributes=*/NULL_TREE);
8025 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8026 parser->in_statement = in_statement;
8027 parser->in_switch_statement_p = in_switch_statement_p;
8030 pop_deferring_access_checks ();
8032 /* This field is only used during parsing of the lambda. */
8033 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8035 /* This lambda shouldn't have any proxies left at this point. */
8036 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8037 /* And now that we're done, push proxies for an enclosing lambda. */
8038 insert_pending_capture_proxies ();
8041 return build_lambda_object (lambda_expr);
8043 return error_mark_node;
8046 /* Parse the beginning of a lambda expression.
8049 [ lambda-capture [opt] ]
8051 LAMBDA_EXPR is the current representation of the lambda expression. */
8054 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8056 /* Need commas after the first capture. */
8059 /* Eat the leading `['. */
8060 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8062 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8063 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8064 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8065 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8066 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8067 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8069 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8071 cp_lexer_consume_token (parser->lexer);
8075 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8077 cp_token* capture_token;
8079 tree capture_init_expr;
8080 cp_id_kind idk = CP_ID_KIND_NONE;
8081 bool explicit_init_p = false;
8083 enum capture_kind_type
8088 enum capture_kind_type capture_kind = BY_COPY;
8090 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8092 error ("expected end of capture-list");
8099 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8101 /* Possibly capture `this'. */
8102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8104 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8105 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8106 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8107 "with by-copy capture default");
8108 cp_lexer_consume_token (parser->lexer);
8109 add_capture (lambda_expr,
8110 /*id=*/this_identifier,
8111 /*initializer=*/finish_this_expr(),
8112 /*by_reference_p=*/false,
8117 /* Remember whether we want to capture as a reference or not. */
8118 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8120 capture_kind = BY_REFERENCE;
8121 cp_lexer_consume_token (parser->lexer);
8124 /* Get the identifier. */
8125 capture_token = cp_lexer_peek_token (parser->lexer);
8126 capture_id = cp_parser_identifier (parser);
8128 if (capture_id == error_mark_node)
8129 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8130 delimiters, but I modified this to stop on unnested ']' as well. It
8131 was already changed to stop on unnested '}', so the
8132 "closing_parenthesis" name is no more misleading with my change. */
8134 cp_parser_skip_to_closing_parenthesis (parser,
8135 /*recovering=*/true,
8137 /*consume_paren=*/true);
8141 /* Find the initializer for this capture. */
8142 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8144 /* An explicit expression exists. */
8145 cp_lexer_consume_token (parser->lexer);
8146 pedwarn (input_location, OPT_pedantic,
8147 "ISO C++ does not allow initializers "
8148 "in lambda expression capture lists");
8149 capture_init_expr = cp_parser_assignment_expression (parser,
8152 explicit_init_p = true;
8156 const char* error_msg;
8158 /* Turn the identifier into an id-expression. */
8160 = cp_parser_lookup_name
8164 /*is_template=*/false,
8165 /*is_namespace=*/false,
8166 /*check_dependency=*/true,
8167 /*ambiguous_decls=*/NULL,
8168 capture_token->location);
8170 if (capture_init_expr == error_mark_node)
8172 unqualified_name_lookup_error (capture_id);
8175 else if (DECL_P (capture_init_expr)
8176 && (TREE_CODE (capture_init_expr) != VAR_DECL
8177 && TREE_CODE (capture_init_expr) != PARM_DECL))
8179 error_at (capture_token->location,
8180 "capture of non-variable %qD ",
8182 inform (0, "%q+#D declared here", capture_init_expr);
8185 if (TREE_CODE (capture_init_expr) == VAR_DECL
8186 && decl_storage_duration (capture_init_expr) != dk_auto)
8188 pedwarn (capture_token->location, 0, "capture of variable "
8189 "%qD with non-automatic storage duration",
8191 inform (0, "%q+#D declared here", capture_init_expr);
8196 = finish_id_expression
8201 /*integral_constant_expression_p=*/false,
8202 /*allow_non_integral_constant_expression_p=*/false,
8203 /*non_integral_constant_expression_p=*/NULL,
8204 /*template_p=*/false,
8206 /*address_p=*/false,
8207 /*template_arg_p=*/false,
8209 capture_token->location);
8212 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8213 && !explicit_init_p)
8215 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8216 && capture_kind == BY_COPY)
8217 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8218 "of %qD redundant with by-copy capture default",
8220 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8221 && capture_kind == BY_REFERENCE)
8222 pedwarn (capture_token->location, 0, "explicit by-reference "
8223 "capture of %qD redundant with by-reference capture "
8224 "default", capture_id);
8227 add_capture (lambda_expr,
8230 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8234 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8237 /* Parse the (optional) middle of a lambda expression.
8240 ( parameter-declaration-clause [opt] )
8241 attribute-specifier [opt]
8243 exception-specification [opt]
8244 lambda-return-type-clause [opt]
8246 LAMBDA_EXPR is the current representation of the lambda expression. */
8249 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8251 /* 5.1.1.4 of the standard says:
8252 If a lambda-expression does not include a lambda-declarator, it is as if
8253 the lambda-declarator were ().
8254 This means an empty parameter list, no attributes, and no exception
8256 tree param_list = void_list_node;
8257 tree attributes = NULL_TREE;
8258 tree exception_spec = NULL_TREE;
8261 /* The lambda-declarator is optional, but must begin with an opening
8262 parenthesis if present. */
8263 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8265 cp_lexer_consume_token (parser->lexer);
8267 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8269 /* Parse parameters. */
8270 param_list = cp_parser_parameter_declaration_clause (parser);
8272 /* Default arguments shall not be specified in the
8273 parameter-declaration-clause of a lambda-declarator. */
8274 for (t = param_list; t; t = TREE_CHAIN (t))
8275 if (TREE_PURPOSE (t))
8276 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8277 "default argument specified for lambda parameter");
8279 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8281 attributes = cp_parser_attributes_opt (parser);
8283 /* Parse optional `mutable' keyword. */
8284 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8286 cp_lexer_consume_token (parser->lexer);
8287 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8290 /* Parse optional exception specification. */
8291 exception_spec = cp_parser_exception_specification_opt (parser);
8293 /* Parse optional trailing return type. */
8294 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8296 cp_lexer_consume_token (parser->lexer);
8297 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8300 /* The function parameters must be in scope all the way until after the
8301 trailing-return-type in case of decltype. */
8302 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8303 pop_binding (DECL_NAME (t), t);
8308 /* Create the function call operator.
8310 Messing with declarators like this is no uglier than building up the
8311 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8314 cp_decl_specifier_seq return_type_specs;
8315 cp_declarator* declarator;
8320 clear_decl_specs (&return_type_specs);
8321 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8322 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8324 /* Maybe we will deduce the return type later, but we can use void
8325 as a placeholder return type anyways. */
8326 return_type_specs.type = void_type_node;
8328 p = obstack_alloc (&declarator_obstack, 0);
8330 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8333 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8334 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8335 declarator = make_call_declarator (declarator, param_list, quals,
8336 VIRT_SPEC_UNSPECIFIED,
8338 /*late_return_type=*/NULL_TREE);
8339 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8341 fco = grokmethod (&return_type_specs,
8344 if (fco != error_mark_node)
8346 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8347 DECL_ARTIFICIAL (fco) = 1;
8348 /* Give the object parameter a different name. */
8349 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8352 finish_member_declaration (fco);
8354 obstack_free (&declarator_obstack, p);
8356 return (fco != error_mark_node);
8360 /* Parse the body of a lambda expression, which is simply
8364 but which requires special handling.
8365 LAMBDA_EXPR is the current representation of the lambda expression. */
8368 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8370 bool nested = (current_function_decl != NULL_TREE);
8371 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8373 push_function_context ();
8375 /* Still increment function_depth so that we don't GC in the
8376 middle of an expression. */
8378 /* Clear this in case we're in the middle of a default argument. */
8379 parser->local_variables_forbidden_p = false;
8381 /* Finish the function call operator
8383 + late_parsing_for_member
8384 + function_definition_after_declarator
8385 + ctor_initializer_opt_and_function_body */
8387 tree fco = lambda_function (lambda_expr);
8393 /* Let the front end know that we are going to be defining this
8395 start_preparsed_function (fco,
8397 SF_PRE_PARSED | SF_INCLASS_INLINE);
8399 start_lambda_scope (fco);
8400 body = begin_function_body ();
8402 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8405 /* Push the proxies for any explicit captures. */
8406 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8407 cap = TREE_CHAIN (cap))
8408 build_capture_proxy (TREE_PURPOSE (cap));
8410 compound_stmt = begin_compound_stmt (0);
8412 /* 5.1.1.4 of the standard says:
8413 If a lambda-expression does not include a trailing-return-type, it
8414 is as if the trailing-return-type denotes the following type:
8415 * if the compound-statement is of the form
8416 { return attribute-specifier [opt] expression ; }
8417 the type of the returned expression after lvalue-to-rvalue
8418 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8419 (_conv.array_ 4.2), and function-to-pointer conversion
8421 * otherwise, void. */
8423 /* In a lambda that has neither a lambda-return-type-clause
8424 nor a deducible form, errors should be reported for return statements
8425 in the body. Since we used void as the placeholder return type, parsing
8426 the body as usual will give such desired behavior. */
8427 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8428 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8429 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8431 tree expr = NULL_TREE;
8432 cp_id_kind idk = CP_ID_KIND_NONE;
8434 /* Parse tentatively in case there's more after the initial return
8436 cp_parser_parse_tentatively (parser);
8438 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8440 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8442 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8443 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8445 if (cp_parser_parse_definitely (parser))
8447 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8449 /* Will get error here if type not deduced yet. */
8450 finish_return_stmt (expr);
8458 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8459 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8460 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8461 cp_parser_label_declaration (parser);
8462 cp_parser_statement_seq_opt (parser, NULL_TREE);
8463 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8464 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8467 finish_compound_stmt (compound_stmt);
8470 finish_function_body (body);
8471 finish_lambda_scope ();
8473 /* Finish the function and generate code for it if necessary. */
8474 expand_or_defer_fn (finish_function (/*inline*/2));
8477 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8479 pop_function_context();
8484 /* Statements [gram.stmt.stmt] */
8486 /* Parse a statement.
8490 expression-statement
8495 declaration-statement
8498 IN_COMPOUND is true when the statement is nested inside a
8499 cp_parser_compound_statement; this matters for certain pragmas.
8501 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8502 is a (possibly labeled) if statement which is not enclosed in braces
8503 and has an else clause. This is used to implement -Wparentheses. */
8506 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8507 bool in_compound, bool *if_p)
8511 location_t statement_location;
8516 /* There is no statement yet. */
8517 statement = NULL_TREE;
8518 /* Peek at the next token. */
8519 token = cp_lexer_peek_token (parser->lexer);
8520 /* Remember the location of the first token in the statement. */
8521 statement_location = token->location;
8522 /* If this is a keyword, then that will often determine what kind of
8523 statement we have. */
8524 if (token->type == CPP_KEYWORD)
8526 enum rid keyword = token->keyword;
8532 /* Looks like a labeled-statement with a case label.
8533 Parse the label, and then use tail recursion to parse
8535 cp_parser_label_for_labeled_statement (parser);
8540 statement = cp_parser_selection_statement (parser, if_p);
8546 statement = cp_parser_iteration_statement (parser);
8553 statement = cp_parser_jump_statement (parser);
8556 /* Objective-C++ exception-handling constructs. */
8559 case RID_AT_FINALLY:
8560 case RID_AT_SYNCHRONIZED:
8562 statement = cp_parser_objc_statement (parser);
8566 statement = cp_parser_try_block (parser);
8570 /* This must be a namespace alias definition. */
8571 cp_parser_declaration_statement (parser);
8575 /* It might be a keyword like `int' that can start a
8576 declaration-statement. */
8580 else if (token->type == CPP_NAME)
8582 /* If the next token is a `:', then we are looking at a
8583 labeled-statement. */
8584 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8585 if (token->type == CPP_COLON)
8587 /* Looks like a labeled-statement with an ordinary label.
8588 Parse the label, and then use tail recursion to parse
8590 cp_parser_label_for_labeled_statement (parser);
8594 /* Anything that starts with a `{' must be a compound-statement. */
8595 else if (token->type == CPP_OPEN_BRACE)
8596 statement = cp_parser_compound_statement (parser, NULL, false, false);
8597 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8598 a statement all its own. */
8599 else if (token->type == CPP_PRAGMA)
8601 /* Only certain OpenMP pragmas are attached to statements, and thus
8602 are considered statements themselves. All others are not. In
8603 the context of a compound, accept the pragma as a "statement" and
8604 return so that we can check for a close brace. Otherwise we
8605 require a real statement and must go back and read one. */
8607 cp_parser_pragma (parser, pragma_compound);
8608 else if (!cp_parser_pragma (parser, pragma_stmt))
8612 else if (token->type == CPP_EOF)
8614 cp_parser_error (parser, "expected statement");
8618 /* Everything else must be a declaration-statement or an
8619 expression-statement. Try for the declaration-statement
8620 first, unless we are looking at a `;', in which case we know that
8621 we have an expression-statement. */
8624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8626 cp_parser_parse_tentatively (parser);
8627 /* Try to parse the declaration-statement. */
8628 cp_parser_declaration_statement (parser);
8629 /* If that worked, we're done. */
8630 if (cp_parser_parse_definitely (parser))
8633 /* Look for an expression-statement instead. */
8634 statement = cp_parser_expression_statement (parser, in_statement_expr);
8637 /* Set the line number for the statement. */
8638 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8639 SET_EXPR_LOCATION (statement, statement_location);
8642 /* Parse the label for a labeled-statement, i.e.
8645 case constant-expression :
8649 case constant-expression ... constant-expression : statement
8651 When a label is parsed without errors, the label is added to the
8652 parse tree by the finish_* functions, so this function doesn't
8653 have to return the label. */
8656 cp_parser_label_for_labeled_statement (cp_parser* parser)
8659 tree label = NULL_TREE;
8660 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8662 /* The next token should be an identifier. */
8663 token = cp_lexer_peek_token (parser->lexer);
8664 if (token->type != CPP_NAME
8665 && token->type != CPP_KEYWORD)
8667 cp_parser_error (parser, "expected labeled-statement");
8671 parser->colon_corrects_to_scope_p = false;
8672 switch (token->keyword)
8679 /* Consume the `case' token. */
8680 cp_lexer_consume_token (parser->lexer);
8681 /* Parse the constant-expression. */
8682 expr = cp_parser_constant_expression (parser,
8683 /*allow_non_constant_p=*/false,
8686 ellipsis = cp_lexer_peek_token (parser->lexer);
8687 if (ellipsis->type == CPP_ELLIPSIS)
8689 /* Consume the `...' token. */
8690 cp_lexer_consume_token (parser->lexer);
8692 cp_parser_constant_expression (parser,
8693 /*allow_non_constant_p=*/false,
8695 /* We don't need to emit warnings here, as the common code
8696 will do this for us. */
8699 expr_hi = NULL_TREE;
8701 if (parser->in_switch_statement_p)
8702 finish_case_label (token->location, expr, expr_hi);
8704 error_at (token->location,
8705 "case label %qE not within a switch statement",
8711 /* Consume the `default' token. */
8712 cp_lexer_consume_token (parser->lexer);
8714 if (parser->in_switch_statement_p)
8715 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8717 error_at (token->location, "case label not within a switch statement");
8721 /* Anything else must be an ordinary label. */
8722 label = finish_label_stmt (cp_parser_identifier (parser));
8726 /* Require the `:' token. */
8727 cp_parser_require (parser, CPP_COLON, RT_COLON);
8729 /* An ordinary label may optionally be followed by attributes.
8730 However, this is only permitted if the attributes are then
8731 followed by a semicolon. This is because, for backward
8732 compatibility, when parsing
8733 lab: __attribute__ ((unused)) int i;
8734 we want the attribute to attach to "i", not "lab". */
8735 if (label != NULL_TREE
8736 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8740 cp_parser_parse_tentatively (parser);
8741 attrs = cp_parser_attributes_opt (parser);
8742 if (attrs == NULL_TREE
8743 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8744 cp_parser_abort_tentative_parse (parser);
8745 else if (!cp_parser_parse_definitely (parser))
8748 cplus_decl_attributes (&label, attrs, 0);
8751 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8754 /* Parse an expression-statement.
8756 expression-statement:
8759 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8760 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8761 indicates whether this expression-statement is part of an
8762 expression statement. */
8765 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8767 tree statement = NULL_TREE;
8768 cp_token *token = cp_lexer_peek_token (parser->lexer);
8770 /* If the next token is a ';', then there is no expression
8772 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8773 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8775 /* Give a helpful message for "A<T>::type t;" and the like. */
8776 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8777 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8779 if (TREE_CODE (statement) == SCOPE_REF)
8780 error_at (token->location, "need %<typename%> before %qE because "
8781 "%qT is a dependent scope",
8782 statement, TREE_OPERAND (statement, 0));
8783 else if (is_overloaded_fn (statement)
8784 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8787 tree fn = get_first_fn (statement);
8788 error_at (token->location,
8789 "%<%T::%D%> names the constructor, not the type",
8790 DECL_CONTEXT (fn), DECL_NAME (fn));
8794 /* Consume the final `;'. */
8795 cp_parser_consume_semicolon_at_end_of_statement (parser);
8797 if (in_statement_expr
8798 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8799 /* This is the final expression statement of a statement
8801 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8803 statement = finish_expr_stmt (statement);
8810 /* Parse a compound-statement.
8813 { statement-seq [opt] }
8818 { label-declaration-seq [opt] statement-seq [opt] }
8820 label-declaration-seq:
8822 label-declaration-seq label-declaration
8824 Returns a tree representing the statement. */
8827 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8828 bool in_try, bool function_body)
8832 /* Consume the `{'. */
8833 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8834 return error_mark_node;
8835 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8837 pedwarn (input_location, OPT_pedantic,
8838 "compound-statement in constexpr function");
8839 /* Begin the compound-statement. */
8840 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8841 /* If the next keyword is `__label__' we have a label declaration. */
8842 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8843 cp_parser_label_declaration (parser);
8844 /* Parse an (optional) statement-seq. */
8845 cp_parser_statement_seq_opt (parser, in_statement_expr);
8846 /* Finish the compound-statement. */
8847 finish_compound_stmt (compound_stmt);
8848 /* Consume the `}'. */
8849 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8851 return compound_stmt;
8854 /* Parse an (optional) statement-seq.
8858 statement-seq [opt] statement */
8861 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8863 /* Scan statements until there aren't any more. */
8866 cp_token *token = cp_lexer_peek_token (parser->lexer);
8868 /* If we are looking at a `}', then we have run out of
8869 statements; the same is true if we have reached the end
8870 of file, or have stumbled upon a stray '@end'. */
8871 if (token->type == CPP_CLOSE_BRACE
8872 || token->type == CPP_EOF
8873 || token->type == CPP_PRAGMA_EOL
8874 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8877 /* If we are in a compound statement and find 'else' then
8878 something went wrong. */
8879 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8881 if (parser->in_statement & IN_IF_STMT)
8885 token = cp_lexer_consume_token (parser->lexer);
8886 error_at (token->location, "%<else%> without a previous %<if%>");
8890 /* Parse the statement. */
8891 cp_parser_statement (parser, in_statement_expr, true, NULL);
8895 /* Parse a selection-statement.
8897 selection-statement:
8898 if ( condition ) statement
8899 if ( condition ) statement else statement
8900 switch ( condition ) statement
8902 Returns the new IF_STMT or SWITCH_STMT.
8904 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8905 is a (possibly labeled) if statement which is not enclosed in
8906 braces and has an else clause. This is used to implement
8910 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8918 /* Peek at the next token. */
8919 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8921 /* See what kind of keyword it is. */
8922 keyword = token->keyword;
8931 /* Look for the `('. */
8932 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8934 cp_parser_skip_to_end_of_statement (parser);
8935 return error_mark_node;
8938 /* Begin the selection-statement. */
8939 if (keyword == RID_IF)
8940 statement = begin_if_stmt ();
8942 statement = begin_switch_stmt ();
8944 /* Parse the condition. */
8945 condition = cp_parser_condition (parser);
8946 /* Look for the `)'. */
8947 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8948 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8949 /*consume_paren=*/true);
8951 if (keyword == RID_IF)
8954 unsigned char in_statement;
8956 /* Add the condition. */
8957 finish_if_stmt_cond (condition, statement);
8959 /* Parse the then-clause. */
8960 in_statement = parser->in_statement;
8961 parser->in_statement |= IN_IF_STMT;
8962 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8964 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8965 add_stmt (build_empty_stmt (loc));
8966 cp_lexer_consume_token (parser->lexer);
8967 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8968 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8969 "empty body in an %<if%> statement");
8973 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8974 parser->in_statement = in_statement;
8976 finish_then_clause (statement);
8978 /* If the next token is `else', parse the else-clause. */
8979 if (cp_lexer_next_token_is_keyword (parser->lexer,
8982 /* Consume the `else' keyword. */
8983 cp_lexer_consume_token (parser->lexer);
8984 begin_else_clause (statement);
8985 /* Parse the else-clause. */
8986 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8989 loc = cp_lexer_peek_token (parser->lexer)->location;
8991 OPT_Wempty_body, "suggest braces around "
8992 "empty body in an %<else%> statement");
8993 add_stmt (build_empty_stmt (loc));
8994 cp_lexer_consume_token (parser->lexer);
8997 cp_parser_implicitly_scoped_statement (parser, NULL);
8999 finish_else_clause (statement);
9001 /* If we are currently parsing a then-clause, then
9002 IF_P will not be NULL. We set it to true to
9003 indicate that this if statement has an else clause.
9004 This may trigger the Wparentheses warning below
9005 when we get back up to the parent if statement. */
9011 /* This if statement does not have an else clause. If
9012 NESTED_IF is true, then the then-clause is an if
9013 statement which does have an else clause. We warn
9014 about the potential ambiguity. */
9016 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9017 "suggest explicit braces to avoid ambiguous"
9021 /* Now we're all done with the if-statement. */
9022 finish_if_stmt (statement);
9026 bool in_switch_statement_p;
9027 unsigned char in_statement;
9029 /* Add the condition. */
9030 finish_switch_cond (condition, statement);
9032 /* Parse the body of the switch-statement. */
9033 in_switch_statement_p = parser->in_switch_statement_p;
9034 in_statement = parser->in_statement;
9035 parser->in_switch_statement_p = true;
9036 parser->in_statement |= IN_SWITCH_STMT;
9037 cp_parser_implicitly_scoped_statement (parser, NULL);
9038 parser->in_switch_statement_p = in_switch_statement_p;
9039 parser->in_statement = in_statement;
9041 /* Now we're all done with the switch-statement. */
9042 finish_switch_stmt (statement);
9050 cp_parser_error (parser, "expected selection-statement");
9051 return error_mark_node;
9055 /* Parse a condition.
9059 type-specifier-seq declarator = initializer-clause
9060 type-specifier-seq declarator braced-init-list
9065 type-specifier-seq declarator asm-specification [opt]
9066 attributes [opt] = assignment-expression
9068 Returns the expression that should be tested. */
9071 cp_parser_condition (cp_parser* parser)
9073 cp_decl_specifier_seq type_specifiers;
9074 const char *saved_message;
9075 int declares_class_or_enum;
9077 /* Try the declaration first. */
9078 cp_parser_parse_tentatively (parser);
9079 /* New types are not allowed in the type-specifier-seq for a
9081 saved_message = parser->type_definition_forbidden_message;
9082 parser->type_definition_forbidden_message
9083 = G_("types may not be defined in conditions");
9084 /* Parse the type-specifier-seq. */
9085 cp_parser_decl_specifier_seq (parser,
9086 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9088 &declares_class_or_enum);
9089 /* Restore the saved message. */
9090 parser->type_definition_forbidden_message = saved_message;
9091 /* If all is well, we might be looking at a declaration. */
9092 if (!cp_parser_error_occurred (parser))
9095 tree asm_specification;
9097 cp_declarator *declarator;
9098 tree initializer = NULL_TREE;
9100 /* Parse the declarator. */
9101 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9102 /*ctor_dtor_or_conv_p=*/NULL,
9103 /*parenthesized_p=*/NULL,
9104 /*member_p=*/false);
9105 /* Parse the attributes. */
9106 attributes = cp_parser_attributes_opt (parser);
9107 /* Parse the asm-specification. */
9108 asm_specification = cp_parser_asm_specification_opt (parser);
9109 /* If the next token is not an `=' or '{', then we might still be
9110 looking at an expression. For example:
9114 looks like a decl-specifier-seq and a declarator -- but then
9115 there is no `=', so this is an expression. */
9116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9117 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9118 cp_parser_simulate_error (parser);
9120 /* If we did see an `=' or '{', then we are looking at a declaration
9122 if (cp_parser_parse_definitely (parser))
9125 bool non_constant_p;
9126 bool flags = LOOKUP_ONLYCONVERTING;
9128 /* Create the declaration. */
9129 decl = start_decl (declarator, &type_specifiers,
9130 /*initialized_p=*/true,
9131 attributes, /*prefix_attributes=*/NULL_TREE,
9134 /* Parse the initializer. */
9135 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9137 initializer = cp_parser_braced_list (parser, &non_constant_p);
9138 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9143 /* Consume the `='. */
9144 cp_parser_require (parser, CPP_EQ, RT_EQ);
9145 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9147 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9148 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9150 /* Process the initializer. */
9151 cp_finish_decl (decl,
9152 initializer, !non_constant_p,
9157 pop_scope (pushed_scope);
9159 return convert_from_reference (decl);
9162 /* If we didn't even get past the declarator successfully, we are
9163 definitely not looking at a declaration. */
9165 cp_parser_abort_tentative_parse (parser);
9167 /* Otherwise, we are looking at an expression. */
9168 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9171 /* Parses a for-statement or range-for-statement until the closing ')',
9175 cp_parser_for (cp_parser *parser)
9177 tree init, scope, decl;
9180 /* Begin the for-statement. */
9181 scope = begin_for_scope (&init);
9183 /* Parse the initialization. */
9184 is_range_for = cp_parser_for_init_statement (parser, &decl);
9187 return cp_parser_range_for (parser, scope, init, decl);
9189 return cp_parser_c_for (parser, scope, init);
9193 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9195 /* Normal for loop */
9196 tree condition = NULL_TREE;
9197 tree expression = NULL_TREE;
9200 stmt = begin_for_stmt (scope, init);
9201 /* The for-init-statement has already been parsed in
9202 cp_parser_for_init_statement, so no work is needed here. */
9203 finish_for_init_stmt (stmt);
9205 /* If there's a condition, process it. */
9206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9207 condition = cp_parser_condition (parser);
9208 finish_for_cond (condition, stmt);
9209 /* Look for the `;'. */
9210 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9212 /* If there's an expression, process it. */
9213 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9214 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9215 finish_for_expr (expression, stmt);
9220 /* Tries to parse a range-based for-statement:
9223 decl-specifier-seq declarator : expression
9225 The decl-specifier-seq declarator and the `:' are already parsed by
9226 cp_parser_for_init_statement. If processing_template_decl it returns a
9227 newly created RANGE_FOR_STMT; if not, it is converted to a
9228 regular FOR_STMT. */
9231 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9233 tree stmt, range_expr;
9235 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9237 bool expr_non_constant_p;
9238 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9241 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9243 /* If in template, STMT is converted to a normal for-statement
9244 at instantiation. If not, it is done just ahead. */
9245 if (processing_template_decl)
9247 stmt = begin_range_for_stmt (scope, init);
9248 finish_range_for_decl (stmt, range_decl, range_expr);
9249 if (!type_dependent_expression_p (range_expr)
9250 /* do_auto_deduction doesn't mess with template init-lists. */
9251 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9252 do_range_for_auto_deduction (range_decl, range_expr);
9256 stmt = begin_for_stmt (scope, init);
9257 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9262 /* Subroutine of cp_convert_range_for: given the initializer expression,
9263 builds up the range temporary. */
9266 build_range_temp (tree range_expr)
9268 tree range_type, range_temp;
9270 /* Find out the type deduced by the declaration
9271 `auto &&__range = range_expr'. */
9272 range_type = cp_build_reference_type (make_auto (), true);
9273 range_type = do_auto_deduction (range_type, range_expr,
9274 type_uses_auto (range_type));
9276 /* Create the __range variable. */
9277 range_temp = build_decl (input_location, VAR_DECL,
9278 get_identifier ("__for_range"), range_type);
9279 TREE_USED (range_temp) = 1;
9280 DECL_ARTIFICIAL (range_temp) = 1;
9285 /* Used by cp_parser_range_for in template context: we aren't going to
9286 do a full conversion yet, but we still need to resolve auto in the
9287 type of the for-range-declaration if present. This is basically
9288 a shortcut version of cp_convert_range_for. */
9291 do_range_for_auto_deduction (tree decl, tree range_expr)
9293 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9296 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9297 range_temp = convert_from_reference (build_range_temp (range_expr));
9298 iter_type = (cp_parser_perform_range_for_lookup
9299 (range_temp, &begin_dummy, &end_dummy));
9300 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9301 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9302 tf_warning_or_error);
9303 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9304 iter_decl, auto_node);
9308 /* Converts a range-based for-statement into a normal
9309 for-statement, as per the definition.
9311 for (RANGE_DECL : RANGE_EXPR)
9314 should be equivalent to:
9317 auto &&__range = RANGE_EXPR;
9318 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9322 RANGE_DECL = *__begin;
9327 If RANGE_EXPR is an array:
9328 BEGIN_EXPR = __range
9329 END_EXPR = __range + ARRAY_SIZE(__range)
9330 Else if RANGE_EXPR has a member 'begin' or 'end':
9331 BEGIN_EXPR = __range.begin()
9332 END_EXPR = __range.end()
9334 BEGIN_EXPR = begin(__range)
9335 END_EXPR = end(__range);
9337 If __range has a member 'begin' but not 'end', or vice versa, we must
9338 still use the second alternative (it will surely fail, however).
9339 When calling begin()/end() in the third alternative we must use
9340 argument dependent lookup, but always considering 'std' as an associated
9344 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9347 tree iter_type, begin_expr, end_expr;
9348 tree condition, expression;
9350 if (range_decl == error_mark_node || range_expr == error_mark_node)
9351 /* If an error happened previously do nothing or else a lot of
9352 unhelpful errors would be issued. */
9353 begin_expr = end_expr = iter_type = error_mark_node;
9356 tree range_temp = build_range_temp (range_expr);
9357 pushdecl (range_temp);
9358 cp_finish_decl (range_temp, range_expr,
9359 /*is_constant_init*/false, NULL_TREE,
9360 LOOKUP_ONLYCONVERTING);
9362 range_temp = convert_from_reference (range_temp);
9363 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9364 &begin_expr, &end_expr);
9367 /* The new for initialization statement. */
9368 begin = build_decl (input_location, VAR_DECL,
9369 get_identifier ("__for_begin"), iter_type);
9370 TREE_USED (begin) = 1;
9371 DECL_ARTIFICIAL (begin) = 1;
9373 cp_finish_decl (begin, begin_expr,
9374 /*is_constant_init*/false, NULL_TREE,
9375 LOOKUP_ONLYCONVERTING);
9377 end = build_decl (input_location, VAR_DECL,
9378 get_identifier ("__for_end"), iter_type);
9379 TREE_USED (end) = 1;
9380 DECL_ARTIFICIAL (end) = 1;
9382 cp_finish_decl (end, end_expr,
9383 /*is_constant_init*/false, NULL_TREE,
9384 LOOKUP_ONLYCONVERTING);
9386 finish_for_init_stmt (statement);
9388 /* The new for condition. */
9389 condition = build_x_binary_op (NE_EXPR,
9392 NULL, tf_warning_or_error);
9393 finish_for_cond (condition, statement);
9395 /* The new increment expression. */
9396 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9397 finish_for_expr (expression, statement);
9399 /* The declaration is initialized with *__begin inside the loop body. */
9400 cp_finish_decl (range_decl,
9401 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9402 /*is_constant_init*/false, NULL_TREE,
9403 LOOKUP_ONLYCONVERTING);
9408 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9409 We need to solve both at the same time because the method used
9410 depends on the existence of members begin or end.
9411 Returns the type deduced for the iterator expression. */
9414 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9416 if (error_operand_p (range))
9418 *begin = *end = error_mark_node;
9419 return error_mark_node;
9422 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9424 error ("range-based %<for%> expression of type %qT "
9425 "has incomplete type", TREE_TYPE (range));
9426 *begin = *end = error_mark_node;
9427 return error_mark_node;
9429 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9431 /* If RANGE is an array, we will use pointer arithmetic. */
9433 *end = build_binary_op (input_location, PLUS_EXPR,
9435 array_type_nelts_top (TREE_TYPE (range)),
9437 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9441 /* If it is not an array, we must do a bit of magic. */
9442 tree id_begin, id_end;
9443 tree member_begin, member_end;
9445 *begin = *end = error_mark_node;
9447 id_begin = get_identifier ("begin");
9448 id_end = get_identifier ("end");
9449 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9450 /*protect=*/2, /*want_type=*/false);
9451 member_end = lookup_member (TREE_TYPE (range), id_end,
9452 /*protect=*/2, /*want_type=*/false);
9454 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9456 /* Use the member functions. */
9457 if (member_begin != NULL_TREE)
9458 *begin = cp_parser_range_for_member_function (range, id_begin);
9460 error ("range-based %<for%> expression of type %qT has an "
9461 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9463 if (member_end != NULL_TREE)
9464 *end = cp_parser_range_for_member_function (range, id_end);
9466 error ("range-based %<for%> expression of type %qT has a "
9467 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9471 /* Use global functions with ADL. */
9473 vec = make_tree_vector ();
9475 VEC_safe_push (tree, gc, vec, range);
9477 member_begin = perform_koenig_lookup (id_begin, vec,
9478 /*include_std=*/true,
9479 tf_warning_or_error);
9480 *begin = finish_call_expr (member_begin, &vec, false, true,
9481 tf_warning_or_error);
9482 member_end = perform_koenig_lookup (id_end, vec,
9483 /*include_std=*/true,
9484 tf_warning_or_error);
9485 *end = finish_call_expr (member_end, &vec, false, true,
9486 tf_warning_or_error);
9488 release_tree_vector (vec);
9491 /* Last common checks. */
9492 if (*begin == error_mark_node || *end == error_mark_node)
9494 /* If one of the expressions is an error do no more checks. */
9495 *begin = *end = error_mark_node;
9496 return error_mark_node;
9500 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9501 /* The unqualified type of the __begin and __end temporaries should
9502 be the same, as required by the multiple auto declaration. */
9503 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9504 error ("inconsistent begin/end types in range-based %<for%> "
9505 "statement: %qT and %qT",
9506 TREE_TYPE (*begin), TREE_TYPE (*end));
9512 /* Helper function for cp_parser_perform_range_for_lookup.
9513 Builds a tree for RANGE.IDENTIFIER(). */
9516 cp_parser_range_for_member_function (tree range, tree identifier)
9521 member = finish_class_member_access_expr (range, identifier,
9522 false, tf_warning_or_error);
9523 if (member == error_mark_node)
9524 return error_mark_node;
9526 vec = make_tree_vector ();
9527 res = finish_call_expr (member, &vec,
9528 /*disallow_virtual=*/false,
9530 tf_warning_or_error);
9531 release_tree_vector (vec);
9535 /* Parse an iteration-statement.
9537 iteration-statement:
9538 while ( condition ) statement
9539 do statement while ( expression ) ;
9540 for ( for-init-statement condition [opt] ; expression [opt] )
9543 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9546 cp_parser_iteration_statement (cp_parser* parser)
9551 unsigned char in_statement;
9553 /* Peek at the next token. */
9554 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9556 return error_mark_node;
9558 /* Remember whether or not we are already within an iteration
9560 in_statement = parser->in_statement;
9562 /* See what kind of keyword it is. */
9563 keyword = token->keyword;
9570 /* Begin the while-statement. */
9571 statement = begin_while_stmt ();
9572 /* Look for the `('. */
9573 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9574 /* Parse the condition. */
9575 condition = cp_parser_condition (parser);
9576 finish_while_stmt_cond (condition, statement);
9577 /* Look for the `)'. */
9578 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9579 /* Parse the dependent statement. */
9580 parser->in_statement = IN_ITERATION_STMT;
9581 cp_parser_already_scoped_statement (parser);
9582 parser->in_statement = in_statement;
9583 /* We're done with the while-statement. */
9584 finish_while_stmt (statement);
9592 /* Begin the do-statement. */
9593 statement = begin_do_stmt ();
9594 /* Parse the body of the do-statement. */
9595 parser->in_statement = IN_ITERATION_STMT;
9596 cp_parser_implicitly_scoped_statement (parser, NULL);
9597 parser->in_statement = in_statement;
9598 finish_do_body (statement);
9599 /* Look for the `while' keyword. */
9600 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9601 /* Look for the `('. */
9602 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9603 /* Parse the expression. */
9604 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9605 /* We're done with the do-statement. */
9606 finish_do_stmt (expression, statement);
9607 /* Look for the `)'. */
9608 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9609 /* Look for the `;'. */
9610 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9616 /* Look for the `('. */
9617 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9619 statement = cp_parser_for (parser);
9621 /* Look for the `)'. */
9622 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9624 /* Parse the body of the for-statement. */
9625 parser->in_statement = IN_ITERATION_STMT;
9626 cp_parser_already_scoped_statement (parser);
9627 parser->in_statement = in_statement;
9629 /* We're done with the for-statement. */
9630 finish_for_stmt (statement);
9635 cp_parser_error (parser, "expected iteration-statement");
9636 statement = error_mark_node;
9643 /* Parse a for-init-statement or the declarator of a range-based-for.
9644 Returns true if a range-based-for declaration is seen.
9647 expression-statement
9648 simple-declaration */
9651 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9653 /* If the next token is a `;', then we have an empty
9654 expression-statement. Grammatically, this is also a
9655 simple-declaration, but an invalid one, because it does not
9656 declare anything. Therefore, if we did not handle this case
9657 specially, we would issue an error message about an invalid
9659 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9661 bool is_range_for = false;
9662 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9664 parser->colon_corrects_to_scope_p = false;
9666 /* We're going to speculatively look for a declaration, falling back
9667 to an expression, if necessary. */
9668 cp_parser_parse_tentatively (parser);
9669 /* Parse the declaration. */
9670 cp_parser_simple_declaration (parser,
9671 /*function_definition_allowed_p=*/false,
9673 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9674 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9676 /* It is a range-for, consume the ':' */
9677 cp_lexer_consume_token (parser->lexer);
9678 is_range_for = true;
9679 if (cxx_dialect < cxx0x)
9681 error_at (cp_lexer_peek_token (parser->lexer)->location,
9682 "range-based %<for%> loops are not allowed "
9684 *decl = error_mark_node;
9688 /* The ';' is not consumed yet because we told
9689 cp_parser_simple_declaration not to. */
9690 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9692 if (cp_parser_parse_definitely (parser))
9693 return is_range_for;
9694 /* If the tentative parse failed, then we shall need to look for an
9695 expression-statement. */
9697 /* If we are here, it is an expression-statement. */
9698 cp_parser_expression_statement (parser, NULL_TREE);
9702 /* Parse a jump-statement.
9707 return expression [opt] ;
9708 return braced-init-list ;
9716 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9719 cp_parser_jump_statement (cp_parser* parser)
9721 tree statement = error_mark_node;
9724 unsigned char in_statement;
9726 /* Peek at the next token. */
9727 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9729 return error_mark_node;
9731 /* See what kind of keyword it is. */
9732 keyword = token->keyword;
9736 in_statement = parser->in_statement & ~IN_IF_STMT;
9737 switch (in_statement)
9740 error_at (token->location, "break statement not within loop or switch");
9743 gcc_assert ((in_statement & IN_SWITCH_STMT)
9744 || in_statement == IN_ITERATION_STMT);
9745 statement = finish_break_stmt ();
9748 error_at (token->location, "invalid exit from OpenMP structured block");
9751 error_at (token->location, "break statement used with OpenMP for loop");
9754 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9758 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9761 error_at (token->location, "continue statement not within a loop");
9763 case IN_ITERATION_STMT:
9765 statement = finish_continue_stmt ();
9768 error_at (token->location, "invalid exit from OpenMP structured block");
9773 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9779 bool expr_non_constant_p;
9781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9783 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9784 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9786 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9787 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9789 /* If the next token is a `;', then there is no
9792 /* Build the return-statement. */
9793 statement = finish_return_stmt (expr);
9794 /* Look for the final `;'. */
9795 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9800 /* Create the goto-statement. */
9801 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9803 /* Issue a warning about this use of a GNU extension. */
9804 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9805 /* Consume the '*' token. */
9806 cp_lexer_consume_token (parser->lexer);
9807 /* Parse the dependent expression. */
9808 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9811 finish_goto_stmt (cp_parser_identifier (parser));
9812 /* Look for the final `;'. */
9813 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9817 cp_parser_error (parser, "expected jump-statement");
9824 /* Parse a declaration-statement.
9826 declaration-statement:
9827 block-declaration */
9830 cp_parser_declaration_statement (cp_parser* parser)
9834 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9835 p = obstack_alloc (&declarator_obstack, 0);
9837 /* Parse the block-declaration. */
9838 cp_parser_block_declaration (parser, /*statement_p=*/true);
9840 /* Free any declarators allocated. */
9841 obstack_free (&declarator_obstack, p);
9843 /* Finish off the statement. */
9847 /* Some dependent statements (like `if (cond) statement'), are
9848 implicitly in their own scope. In other words, if the statement is
9849 a single statement (as opposed to a compound-statement), it is
9850 none-the-less treated as if it were enclosed in braces. Any
9851 declarations appearing in the dependent statement are out of scope
9852 after control passes that point. This function parses a statement,
9853 but ensures that is in its own scope, even if it is not a
9856 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9857 is a (possibly labeled) if statement which is not enclosed in
9858 braces and has an else clause. This is used to implement
9861 Returns the new statement. */
9864 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9871 /* Mark if () ; with a special NOP_EXPR. */
9872 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9874 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9875 cp_lexer_consume_token (parser->lexer);
9876 statement = add_stmt (build_empty_stmt (loc));
9878 /* if a compound is opened, we simply parse the statement directly. */
9879 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9880 statement = cp_parser_compound_statement (parser, NULL, false, false);
9881 /* If the token is not a `{', then we must take special action. */
9884 /* Create a compound-statement. */
9885 statement = begin_compound_stmt (0);
9886 /* Parse the dependent-statement. */
9887 cp_parser_statement (parser, NULL_TREE, false, if_p);
9888 /* Finish the dummy compound-statement. */
9889 finish_compound_stmt (statement);
9892 /* Return the statement. */
9896 /* For some dependent statements (like `while (cond) statement'), we
9897 have already created a scope. Therefore, even if the dependent
9898 statement is a compound-statement, we do not want to create another
9902 cp_parser_already_scoped_statement (cp_parser* parser)
9904 /* If the token is a `{', then we must take special action. */
9905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9906 cp_parser_statement (parser, NULL_TREE, false, NULL);
9909 /* Avoid calling cp_parser_compound_statement, so that we
9910 don't create a new scope. Do everything else by hand. */
9911 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9912 /* If the next keyword is `__label__' we have a label declaration. */
9913 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9914 cp_parser_label_declaration (parser);
9915 /* Parse an (optional) statement-seq. */
9916 cp_parser_statement_seq_opt (parser, NULL_TREE);
9917 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9921 /* Declarations [gram.dcl.dcl] */
9923 /* Parse an optional declaration-sequence.
9927 declaration-seq declaration */
9930 cp_parser_declaration_seq_opt (cp_parser* parser)
9936 token = cp_lexer_peek_token (parser->lexer);
9938 if (token->type == CPP_CLOSE_BRACE
9939 || token->type == CPP_EOF
9940 || token->type == CPP_PRAGMA_EOL)
9943 if (token->type == CPP_SEMICOLON)
9945 /* A declaration consisting of a single semicolon is
9946 invalid. Allow it unless we're being pedantic. */
9947 cp_lexer_consume_token (parser->lexer);
9948 if (!in_system_header)
9949 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9953 /* If we're entering or exiting a region that's implicitly
9954 extern "C", modify the lang context appropriately. */
9955 if (!parser->implicit_extern_c && token->implicit_extern_c)
9957 push_lang_context (lang_name_c);
9958 parser->implicit_extern_c = true;
9960 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9962 pop_lang_context ();
9963 parser->implicit_extern_c = false;
9966 if (token->type == CPP_PRAGMA)
9968 /* A top-level declaration can consist solely of a #pragma.
9969 A nested declaration cannot, so this is done here and not
9970 in cp_parser_declaration. (A #pragma at block scope is
9971 handled in cp_parser_statement.) */
9972 cp_parser_pragma (parser, pragma_external);
9976 /* Parse the declaration itself. */
9977 cp_parser_declaration (parser);
9981 /* Parse a declaration.
9986 template-declaration
9987 explicit-instantiation
9988 explicit-specialization
9989 linkage-specification
9990 namespace-definition
9995 __extension__ declaration */
9998 cp_parser_declaration (cp_parser* parser)
10002 int saved_pedantic;
10004 tree attributes = NULL_TREE;
10006 /* Check for the `__extension__' keyword. */
10007 if (cp_parser_extension_opt (parser, &saved_pedantic))
10009 /* Parse the qualified declaration. */
10010 cp_parser_declaration (parser);
10011 /* Restore the PEDANTIC flag. */
10012 pedantic = saved_pedantic;
10017 /* Try to figure out what kind of declaration is present. */
10018 token1 = *cp_lexer_peek_token (parser->lexer);
10020 if (token1.type != CPP_EOF)
10021 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10024 token2.type = CPP_EOF;
10025 token2.keyword = RID_MAX;
10028 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10029 p = obstack_alloc (&declarator_obstack, 0);
10031 /* If the next token is `extern' and the following token is a string
10032 literal, then we have a linkage specification. */
10033 if (token1.keyword == RID_EXTERN
10034 && cp_parser_is_pure_string_literal (&token2))
10035 cp_parser_linkage_specification (parser);
10036 /* If the next token is `template', then we have either a template
10037 declaration, an explicit instantiation, or an explicit
10039 else if (token1.keyword == RID_TEMPLATE)
10041 /* `template <>' indicates a template specialization. */
10042 if (token2.type == CPP_LESS
10043 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10044 cp_parser_explicit_specialization (parser);
10045 /* `template <' indicates a template declaration. */
10046 else if (token2.type == CPP_LESS)
10047 cp_parser_template_declaration (parser, /*member_p=*/false);
10048 /* Anything else must be an explicit instantiation. */
10050 cp_parser_explicit_instantiation (parser);
10052 /* If the next token is `export', then we have a template
10054 else if (token1.keyword == RID_EXPORT)
10055 cp_parser_template_declaration (parser, /*member_p=*/false);
10056 /* If the next token is `extern', 'static' or 'inline' and the one
10057 after that is `template', we have a GNU extended explicit
10058 instantiation directive. */
10059 else if (cp_parser_allow_gnu_extensions_p (parser)
10060 && (token1.keyword == RID_EXTERN
10061 || token1.keyword == RID_STATIC
10062 || token1.keyword == RID_INLINE)
10063 && token2.keyword == RID_TEMPLATE)
10064 cp_parser_explicit_instantiation (parser);
10065 /* If the next token is `namespace', check for a named or unnamed
10066 namespace definition. */
10067 else if (token1.keyword == RID_NAMESPACE
10068 && (/* A named namespace definition. */
10069 (token2.type == CPP_NAME
10070 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10072 /* An unnamed namespace definition. */
10073 || token2.type == CPP_OPEN_BRACE
10074 || token2.keyword == RID_ATTRIBUTE))
10075 cp_parser_namespace_definition (parser);
10076 /* An inline (associated) namespace definition. */
10077 else if (token1.keyword == RID_INLINE
10078 && token2.keyword == RID_NAMESPACE)
10079 cp_parser_namespace_definition (parser);
10080 /* Objective-C++ declaration/definition. */
10081 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10082 cp_parser_objc_declaration (parser, NULL_TREE);
10083 else if (c_dialect_objc ()
10084 && token1.keyword == RID_ATTRIBUTE
10085 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10086 cp_parser_objc_declaration (parser, attributes);
10087 /* We must have either a block declaration or a function
10090 /* Try to parse a block-declaration, or a function-definition. */
10091 cp_parser_block_declaration (parser, /*statement_p=*/false);
10093 /* Free any declarators allocated. */
10094 obstack_free (&declarator_obstack, p);
10097 /* Parse a block-declaration.
10102 namespace-alias-definition
10109 __extension__ block-declaration
10114 static_assert-declaration
10116 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10117 part of a declaration-statement. */
10120 cp_parser_block_declaration (cp_parser *parser,
10124 int saved_pedantic;
10126 /* Check for the `__extension__' keyword. */
10127 if (cp_parser_extension_opt (parser, &saved_pedantic))
10129 /* Parse the qualified declaration. */
10130 cp_parser_block_declaration (parser, statement_p);
10131 /* Restore the PEDANTIC flag. */
10132 pedantic = saved_pedantic;
10137 /* Peek at the next token to figure out which kind of declaration is
10139 token1 = cp_lexer_peek_token (parser->lexer);
10141 /* If the next keyword is `asm', we have an asm-definition. */
10142 if (token1->keyword == RID_ASM)
10145 cp_parser_commit_to_tentative_parse (parser);
10146 cp_parser_asm_definition (parser);
10148 /* If the next keyword is `namespace', we have a
10149 namespace-alias-definition. */
10150 else if (token1->keyword == RID_NAMESPACE)
10151 cp_parser_namespace_alias_definition (parser);
10152 /* If the next keyword is `using', we have either a
10153 using-declaration or a using-directive. */
10154 else if (token1->keyword == RID_USING)
10159 cp_parser_commit_to_tentative_parse (parser);
10160 /* If the token after `using' is `namespace', then we have a
10161 using-directive. */
10162 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10163 if (token2->keyword == RID_NAMESPACE)
10164 cp_parser_using_directive (parser);
10165 /* Otherwise, it's a using-declaration. */
10167 cp_parser_using_declaration (parser,
10168 /*access_declaration_p=*/false);
10170 /* If the next keyword is `__label__' we have a misplaced label
10172 else if (token1->keyword == RID_LABEL)
10174 cp_lexer_consume_token (parser->lexer);
10175 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10176 cp_parser_skip_to_end_of_statement (parser);
10177 /* If the next token is now a `;', consume it. */
10178 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10179 cp_lexer_consume_token (parser->lexer);
10181 /* If the next token is `static_assert' we have a static assertion. */
10182 else if (token1->keyword == RID_STATIC_ASSERT)
10183 cp_parser_static_assert (parser, /*member_p=*/false);
10184 /* Anything else must be a simple-declaration. */
10186 cp_parser_simple_declaration (parser, !statement_p,
10187 /*maybe_range_for_decl*/NULL);
10190 /* Parse a simple-declaration.
10192 simple-declaration:
10193 decl-specifier-seq [opt] init-declarator-list [opt] ;
10195 init-declarator-list:
10197 init-declarator-list , init-declarator
10199 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10200 function-definition as a simple-declaration.
10202 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10203 parsed declaration if it is an uninitialized single declarator not followed
10204 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10205 if present, will not be consumed. */
10208 cp_parser_simple_declaration (cp_parser* parser,
10209 bool function_definition_allowed_p,
10210 tree *maybe_range_for_decl)
10212 cp_decl_specifier_seq decl_specifiers;
10213 int declares_class_or_enum;
10214 bool saw_declarator;
10216 if (maybe_range_for_decl)
10217 *maybe_range_for_decl = NULL_TREE;
10219 /* Defer access checks until we know what is being declared; the
10220 checks for names appearing in the decl-specifier-seq should be
10221 done as if we were in the scope of the thing being declared. */
10222 push_deferring_access_checks (dk_deferred);
10224 /* Parse the decl-specifier-seq. We have to keep track of whether
10225 or not the decl-specifier-seq declares a named class or
10226 enumeration type, since that is the only case in which the
10227 init-declarator-list is allowed to be empty.
10231 In a simple-declaration, the optional init-declarator-list can be
10232 omitted only when declaring a class or enumeration, that is when
10233 the decl-specifier-seq contains either a class-specifier, an
10234 elaborated-type-specifier, or an enum-specifier. */
10235 cp_parser_decl_specifier_seq (parser,
10236 CP_PARSER_FLAGS_OPTIONAL,
10238 &declares_class_or_enum);
10239 /* We no longer need to defer access checks. */
10240 stop_deferring_access_checks ();
10242 /* In a block scope, a valid declaration must always have a
10243 decl-specifier-seq. By not trying to parse declarators, we can
10244 resolve the declaration/expression ambiguity more quickly. */
10245 if (!function_definition_allowed_p
10246 && !decl_specifiers.any_specifiers_p)
10248 cp_parser_error (parser, "expected declaration");
10252 /* If the next two tokens are both identifiers, the code is
10253 erroneous. The usual cause of this situation is code like:
10257 where "T" should name a type -- but does not. */
10258 if (!decl_specifiers.any_type_specifiers_p
10259 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10261 /* If parsing tentatively, we should commit; we really are
10262 looking at a declaration. */
10263 cp_parser_commit_to_tentative_parse (parser);
10268 /* If we have seen at least one decl-specifier, and the next token
10269 is not a parenthesis, then we must be looking at a declaration.
10270 (After "int (" we might be looking at a functional cast.) */
10271 if (decl_specifiers.any_specifiers_p
10272 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10273 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10274 && !cp_parser_error_occurred (parser))
10275 cp_parser_commit_to_tentative_parse (parser);
10277 /* Keep going until we hit the `;' at the end of the simple
10279 saw_declarator = false;
10280 while (cp_lexer_next_token_is_not (parser->lexer,
10284 bool function_definition_p;
10287 if (saw_declarator)
10289 /* If we are processing next declarator, coma is expected */
10290 token = cp_lexer_peek_token (parser->lexer);
10291 gcc_assert (token->type == CPP_COMMA);
10292 cp_lexer_consume_token (parser->lexer);
10293 if (maybe_range_for_decl)
10294 *maybe_range_for_decl = error_mark_node;
10297 saw_declarator = true;
10299 /* Parse the init-declarator. */
10300 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10302 function_definition_allowed_p,
10303 /*member_p=*/false,
10304 declares_class_or_enum,
10305 &function_definition_p,
10306 maybe_range_for_decl);
10307 /* If an error occurred while parsing tentatively, exit quickly.
10308 (That usually happens when in the body of a function; each
10309 statement is treated as a declaration-statement until proven
10311 if (cp_parser_error_occurred (parser))
10313 /* Handle function definitions specially. */
10314 if (function_definition_p)
10316 /* If the next token is a `,', then we are probably
10317 processing something like:
10321 which is erroneous. */
10322 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10324 cp_token *token = cp_lexer_peek_token (parser->lexer);
10325 error_at (token->location,
10327 " declarations and function-definitions is forbidden");
10329 /* Otherwise, we're done with the list of declarators. */
10332 pop_deferring_access_checks ();
10336 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10337 *maybe_range_for_decl = decl;
10338 /* The next token should be either a `,' or a `;'. */
10339 token = cp_lexer_peek_token (parser->lexer);
10340 /* If it's a `,', there are more declarators to come. */
10341 if (token->type == CPP_COMMA)
10342 /* will be consumed next time around */;
10343 /* If it's a `;', we are done. */
10344 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10346 /* Anything else is an error. */
10349 /* If we have already issued an error message we don't need
10350 to issue another one. */
10351 if (decl != error_mark_node
10352 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10353 cp_parser_error (parser, "expected %<,%> or %<;%>");
10354 /* Skip tokens until we reach the end of the statement. */
10355 cp_parser_skip_to_end_of_statement (parser);
10356 /* If the next token is now a `;', consume it. */
10357 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10358 cp_lexer_consume_token (parser->lexer);
10361 /* After the first time around, a function-definition is not
10362 allowed -- even if it was OK at first. For example:
10367 function_definition_allowed_p = false;
10370 /* Issue an error message if no declarators are present, and the
10371 decl-specifier-seq does not itself declare a class or
10373 if (!saw_declarator)
10375 if (cp_parser_declares_only_class_p (parser))
10376 shadow_tag (&decl_specifiers);
10377 /* Perform any deferred access checks. */
10378 perform_deferred_access_checks ();
10381 /* Consume the `;'. */
10382 if (!maybe_range_for_decl)
10383 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10386 pop_deferring_access_checks ();
10389 /* Parse a decl-specifier-seq.
10391 decl-specifier-seq:
10392 decl-specifier-seq [opt] decl-specifier
10395 storage-class-specifier
10406 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10408 The parser flags FLAGS is used to control type-specifier parsing.
10410 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10413 1: one of the decl-specifiers is an elaborated-type-specifier
10414 (i.e., a type declaration)
10415 2: one of the decl-specifiers is an enum-specifier or a
10416 class-specifier (i.e., a type definition)
10421 cp_parser_decl_specifier_seq (cp_parser* parser,
10422 cp_parser_flags flags,
10423 cp_decl_specifier_seq *decl_specs,
10424 int* declares_class_or_enum)
10426 bool constructor_possible_p = !parser->in_declarator_p;
10427 cp_token *start_token = NULL;
10429 /* Clear DECL_SPECS. */
10430 clear_decl_specs (decl_specs);
10432 /* Assume no class or enumeration type is declared. */
10433 *declares_class_or_enum = 0;
10435 /* Keep reading specifiers until there are no more to read. */
10438 bool constructor_p;
10439 bool found_decl_spec;
10442 /* Peek at the next token. */
10443 token = cp_lexer_peek_token (parser->lexer);
10445 /* Save the first token of the decl spec list for error
10448 start_token = token;
10449 /* Handle attributes. */
10450 if (token->keyword == RID_ATTRIBUTE)
10452 /* Parse the attributes. */
10453 decl_specs->attributes
10454 = chainon (decl_specs->attributes,
10455 cp_parser_attributes_opt (parser));
10458 /* Assume we will find a decl-specifier keyword. */
10459 found_decl_spec = true;
10460 /* If the next token is an appropriate keyword, we can simply
10461 add it to the list. */
10462 switch (token->keyword)
10468 if (!at_class_scope_p ())
10470 error_at (token->location, "%<friend%> used outside of class");
10471 cp_lexer_purge_token (parser->lexer);
10475 ++decl_specs->specs[(int) ds_friend];
10476 /* Consume the token. */
10477 cp_lexer_consume_token (parser->lexer);
10481 case RID_CONSTEXPR:
10482 ++decl_specs->specs[(int) ds_constexpr];
10483 cp_lexer_consume_token (parser->lexer);
10486 /* function-specifier:
10493 cp_parser_function_specifier_opt (parser, decl_specs);
10499 ++decl_specs->specs[(int) ds_typedef];
10500 /* Consume the token. */
10501 cp_lexer_consume_token (parser->lexer);
10502 /* A constructor declarator cannot appear in a typedef. */
10503 constructor_possible_p = false;
10504 /* The "typedef" keyword can only occur in a declaration; we
10505 may as well commit at this point. */
10506 cp_parser_commit_to_tentative_parse (parser);
10508 if (decl_specs->storage_class != sc_none)
10509 decl_specs->conflicting_specifiers_p = true;
10512 /* storage-class-specifier:
10522 if (cxx_dialect == cxx98)
10524 /* Consume the token. */
10525 cp_lexer_consume_token (parser->lexer);
10527 /* Complain about `auto' as a storage specifier, if
10528 we're complaining about C++0x compatibility. */
10529 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10530 " will change meaning in C++0x; please remove it");
10532 /* Set the storage class anyway. */
10533 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10537 /* C++0x auto type-specifier. */
10538 found_decl_spec = false;
10545 /* Consume the token. */
10546 cp_lexer_consume_token (parser->lexer);
10547 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10551 /* Consume the token. */
10552 cp_lexer_consume_token (parser->lexer);
10553 ++decl_specs->specs[(int) ds_thread];
10557 /* We did not yet find a decl-specifier yet. */
10558 found_decl_spec = false;
10562 if (found_decl_spec
10563 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10564 && token->keyword != RID_CONSTEXPR)
10565 error ("decl-specifier invalid in condition");
10567 /* Constructors are a special case. The `S' in `S()' is not a
10568 decl-specifier; it is the beginning of the declarator. */
10570 = (!found_decl_spec
10571 && constructor_possible_p
10572 && (cp_parser_constructor_declarator_p
10573 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10575 /* If we don't have a DECL_SPEC yet, then we must be looking at
10576 a type-specifier. */
10577 if (!found_decl_spec && !constructor_p)
10579 int decl_spec_declares_class_or_enum;
10580 bool is_cv_qualifier;
10584 = cp_parser_type_specifier (parser, flags,
10586 /*is_declaration=*/true,
10587 &decl_spec_declares_class_or_enum,
10589 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10591 /* If this type-specifier referenced a user-defined type
10592 (a typedef, class-name, etc.), then we can't allow any
10593 more such type-specifiers henceforth.
10597 The longest sequence of decl-specifiers that could
10598 possibly be a type name is taken as the
10599 decl-specifier-seq of a declaration. The sequence shall
10600 be self-consistent as described below.
10604 As a general rule, at most one type-specifier is allowed
10605 in the complete decl-specifier-seq of a declaration. The
10606 only exceptions are the following:
10608 -- const or volatile can be combined with any other
10611 -- signed or unsigned can be combined with char, long,
10619 void g (const int Pc);
10621 Here, Pc is *not* part of the decl-specifier seq; it's
10622 the declarator. Therefore, once we see a type-specifier
10623 (other than a cv-qualifier), we forbid any additional
10624 user-defined types. We *do* still allow things like `int
10625 int' to be considered a decl-specifier-seq, and issue the
10626 error message later. */
10627 if (type_spec && !is_cv_qualifier)
10628 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10629 /* A constructor declarator cannot follow a type-specifier. */
10632 constructor_possible_p = false;
10633 found_decl_spec = true;
10634 if (!is_cv_qualifier)
10635 decl_specs->any_type_specifiers_p = true;
10639 /* If we still do not have a DECL_SPEC, then there are no more
10640 decl-specifiers. */
10641 if (!found_decl_spec)
10644 decl_specs->any_specifiers_p = true;
10645 /* After we see one decl-specifier, further decl-specifiers are
10646 always optional. */
10647 flags |= CP_PARSER_FLAGS_OPTIONAL;
10650 cp_parser_check_decl_spec (decl_specs, start_token->location);
10652 /* Don't allow a friend specifier with a class definition. */
10653 if (decl_specs->specs[(int) ds_friend] != 0
10654 && (*declares_class_or_enum & 2))
10655 error_at (start_token->location,
10656 "class definition may not be declared a friend");
10659 /* Parse an (optional) storage-class-specifier.
10661 storage-class-specifier:
10670 storage-class-specifier:
10673 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10676 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10678 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10681 if (cxx_dialect != cxx98)
10683 /* Fall through for C++98. */
10690 /* Consume the token. */
10691 return cp_lexer_consume_token (parser->lexer)->u.value;
10698 /* Parse an (optional) function-specifier.
10700 function-specifier:
10705 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10706 Updates DECL_SPECS, if it is non-NULL. */
10709 cp_parser_function_specifier_opt (cp_parser* parser,
10710 cp_decl_specifier_seq *decl_specs)
10712 cp_token *token = cp_lexer_peek_token (parser->lexer);
10713 switch (token->keyword)
10717 ++decl_specs->specs[(int) ds_inline];
10721 /* 14.5.2.3 [temp.mem]
10723 A member function template shall not be virtual. */
10724 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10725 error_at (token->location, "templates may not be %<virtual%>");
10726 else if (decl_specs)
10727 ++decl_specs->specs[(int) ds_virtual];
10732 ++decl_specs->specs[(int) ds_explicit];
10739 /* Consume the token. */
10740 return cp_lexer_consume_token (parser->lexer)->u.value;
10743 /* Parse a linkage-specification.
10745 linkage-specification:
10746 extern string-literal { declaration-seq [opt] }
10747 extern string-literal declaration */
10750 cp_parser_linkage_specification (cp_parser* parser)
10754 /* Look for the `extern' keyword. */
10755 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10757 /* Look for the string-literal. */
10758 linkage = cp_parser_string_literal (parser, false, false);
10760 /* Transform the literal into an identifier. If the literal is a
10761 wide-character string, or contains embedded NULs, then we can't
10762 handle it as the user wants. */
10763 if (strlen (TREE_STRING_POINTER (linkage))
10764 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10766 cp_parser_error (parser, "invalid linkage-specification");
10767 /* Assume C++ linkage. */
10768 linkage = lang_name_cplusplus;
10771 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10773 /* We're now using the new linkage. */
10774 push_lang_context (linkage);
10776 /* If the next token is a `{', then we're using the first
10778 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10780 /* Consume the `{' token. */
10781 cp_lexer_consume_token (parser->lexer);
10782 /* Parse the declarations. */
10783 cp_parser_declaration_seq_opt (parser);
10784 /* Look for the closing `}'. */
10785 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10787 /* Otherwise, there's just one declaration. */
10790 bool saved_in_unbraced_linkage_specification_p;
10792 saved_in_unbraced_linkage_specification_p
10793 = parser->in_unbraced_linkage_specification_p;
10794 parser->in_unbraced_linkage_specification_p = true;
10795 cp_parser_declaration (parser);
10796 parser->in_unbraced_linkage_specification_p
10797 = saved_in_unbraced_linkage_specification_p;
10800 /* We're done with the linkage-specification. */
10801 pop_lang_context ();
10804 /* Parse a static_assert-declaration.
10806 static_assert-declaration:
10807 static_assert ( constant-expression , string-literal ) ;
10809 If MEMBER_P, this static_assert is a class member. */
10812 cp_parser_static_assert(cp_parser *parser, bool member_p)
10817 location_t saved_loc;
10820 /* Peek at the `static_assert' token so we can keep track of exactly
10821 where the static assertion started. */
10822 token = cp_lexer_peek_token (parser->lexer);
10823 saved_loc = token->location;
10825 /* Look for the `static_assert' keyword. */
10826 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10830 /* We know we are in a static assertion; commit to any tentative
10832 if (cp_parser_parsing_tentatively (parser))
10833 cp_parser_commit_to_tentative_parse (parser);
10835 /* Parse the `(' starting the static assertion condition. */
10836 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10838 /* Parse the constant-expression. Allow a non-constant expression
10839 here in order to give better diagnostics in finish_static_assert. */
10841 cp_parser_constant_expression (parser,
10842 /*allow_non_constant_p=*/true,
10843 /*non_constant_p=*/&dummy);
10845 /* Parse the separating `,'. */
10846 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10848 /* Parse the string-literal message. */
10849 message = cp_parser_string_literal (parser,
10850 /*translate=*/false,
10853 /* A `)' completes the static assertion. */
10854 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10855 cp_parser_skip_to_closing_parenthesis (parser,
10856 /*recovering=*/true,
10857 /*or_comma=*/false,
10858 /*consume_paren=*/true);
10860 /* A semicolon terminates the declaration. */
10861 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10863 /* Complete the static assertion, which may mean either processing
10864 the static assert now or saving it for template instantiation. */
10865 finish_static_assert (condition, message, saved_loc, member_p);
10868 /* Parse a `decltype' type. Returns the type.
10870 simple-type-specifier:
10871 decltype ( expression ) */
10874 cp_parser_decltype (cp_parser *parser)
10877 bool id_expression_or_member_access_p = false;
10878 const char *saved_message;
10879 bool saved_integral_constant_expression_p;
10880 bool saved_non_integral_constant_expression_p;
10881 cp_token *id_expr_start_token;
10882 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10884 if (start_token->type == CPP_DECLTYPE)
10886 /* Already parsed. */
10887 cp_lexer_consume_token (parser->lexer);
10888 return start_token->u.value;
10891 /* Look for the `decltype' token. */
10892 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10893 return error_mark_node;
10895 /* Types cannot be defined in a `decltype' expression. Save away the
10897 saved_message = parser->type_definition_forbidden_message;
10899 /* And create the new one. */
10900 parser->type_definition_forbidden_message
10901 = G_("types may not be defined in %<decltype%> expressions");
10903 /* The restrictions on constant-expressions do not apply inside
10904 decltype expressions. */
10905 saved_integral_constant_expression_p
10906 = parser->integral_constant_expression_p;
10907 saved_non_integral_constant_expression_p
10908 = parser->non_integral_constant_expression_p;
10909 parser->integral_constant_expression_p = false;
10911 /* Do not actually evaluate the expression. */
10912 ++cp_unevaluated_operand;
10914 /* Do not warn about problems with the expression. */
10915 ++c_inhibit_evaluation_warnings;
10917 /* Parse the opening `('. */
10918 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10919 return error_mark_node;
10921 /* First, try parsing an id-expression. */
10922 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10923 cp_parser_parse_tentatively (parser);
10924 expr = cp_parser_id_expression (parser,
10925 /*template_keyword_p=*/false,
10926 /*check_dependency_p=*/true,
10927 /*template_p=*/NULL,
10928 /*declarator_p=*/false,
10929 /*optional_p=*/false);
10931 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10933 bool non_integral_constant_expression_p = false;
10934 tree id_expression = expr;
10936 const char *error_msg;
10938 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10939 /* Lookup the name we got back from the id-expression. */
10940 expr = cp_parser_lookup_name (parser, expr,
10942 /*is_template=*/false,
10943 /*is_namespace=*/false,
10944 /*check_dependency=*/true,
10945 /*ambiguous_decls=*/NULL,
10946 id_expr_start_token->location);
10949 && expr != error_mark_node
10950 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10951 && TREE_CODE (expr) != TYPE_DECL
10952 && (TREE_CODE (expr) != BIT_NOT_EXPR
10953 || !TYPE_P (TREE_OPERAND (expr, 0)))
10954 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10956 /* Complete lookup of the id-expression. */
10957 expr = (finish_id_expression
10958 (id_expression, expr, parser->scope, &idk,
10959 /*integral_constant_expression_p=*/false,
10960 /*allow_non_integral_constant_expression_p=*/true,
10961 &non_integral_constant_expression_p,
10962 /*template_p=*/false,
10964 /*address_p=*/false,
10965 /*template_arg_p=*/false,
10967 id_expr_start_token->location));
10969 if (expr == error_mark_node)
10970 /* We found an id-expression, but it was something that we
10971 should not have found. This is an error, not something
10972 we can recover from, so note that we found an
10973 id-expression and we'll recover as gracefully as
10975 id_expression_or_member_access_p = true;
10979 && expr != error_mark_node
10980 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10981 /* We have an id-expression. */
10982 id_expression_or_member_access_p = true;
10985 if (!id_expression_or_member_access_p)
10987 /* Abort the id-expression parse. */
10988 cp_parser_abort_tentative_parse (parser);
10990 /* Parsing tentatively, again. */
10991 cp_parser_parse_tentatively (parser);
10993 /* Parse a class member access. */
10994 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10996 /*member_access_only_p=*/true, NULL);
10999 && expr != error_mark_node
11000 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11001 /* We have an id-expression. */
11002 id_expression_or_member_access_p = true;
11005 if (id_expression_or_member_access_p)
11006 /* We have parsed the complete id-expression or member access. */
11007 cp_parser_parse_definitely (parser);
11010 bool saved_greater_than_is_operator_p;
11012 /* Abort our attempt to parse an id-expression or member access
11014 cp_parser_abort_tentative_parse (parser);
11016 /* Within a parenthesized expression, a `>' token is always
11017 the greater-than operator. */
11018 saved_greater_than_is_operator_p
11019 = parser->greater_than_is_operator_p;
11020 parser->greater_than_is_operator_p = true;
11022 /* Parse a full expression. */
11023 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11025 /* The `>' token might be the end of a template-id or
11026 template-parameter-list now. */
11027 parser->greater_than_is_operator_p
11028 = saved_greater_than_is_operator_p;
11031 /* Go back to evaluating expressions. */
11032 --cp_unevaluated_operand;
11033 --c_inhibit_evaluation_warnings;
11035 /* Restore the old message and the integral constant expression
11037 parser->type_definition_forbidden_message = saved_message;
11038 parser->integral_constant_expression_p
11039 = saved_integral_constant_expression_p;
11040 parser->non_integral_constant_expression_p
11041 = saved_non_integral_constant_expression_p;
11043 /* Parse to the closing `)'. */
11044 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11046 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11047 /*consume_paren=*/true);
11048 return error_mark_node;
11051 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11052 tf_warning_or_error);
11054 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11056 start_token->type = CPP_DECLTYPE;
11057 start_token->u.value = expr;
11058 start_token->keyword = RID_MAX;
11059 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11064 /* Special member functions [gram.special] */
11066 /* Parse a conversion-function-id.
11068 conversion-function-id:
11069 operator conversion-type-id
11071 Returns an IDENTIFIER_NODE representing the operator. */
11074 cp_parser_conversion_function_id (cp_parser* parser)
11078 tree saved_qualifying_scope;
11079 tree saved_object_scope;
11080 tree pushed_scope = NULL_TREE;
11082 /* Look for the `operator' token. */
11083 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11084 return error_mark_node;
11085 /* When we parse the conversion-type-id, the current scope will be
11086 reset. However, we need that information in able to look up the
11087 conversion function later, so we save it here. */
11088 saved_scope = parser->scope;
11089 saved_qualifying_scope = parser->qualifying_scope;
11090 saved_object_scope = parser->object_scope;
11091 /* We must enter the scope of the class so that the names of
11092 entities declared within the class are available in the
11093 conversion-type-id. For example, consider:
11100 S::operator I() { ... }
11102 In order to see that `I' is a type-name in the definition, we
11103 must be in the scope of `S'. */
11105 pushed_scope = push_scope (saved_scope);
11106 /* Parse the conversion-type-id. */
11107 type = cp_parser_conversion_type_id (parser);
11108 /* Leave the scope of the class, if any. */
11110 pop_scope (pushed_scope);
11111 /* Restore the saved scope. */
11112 parser->scope = saved_scope;
11113 parser->qualifying_scope = saved_qualifying_scope;
11114 parser->object_scope = saved_object_scope;
11115 /* If the TYPE is invalid, indicate failure. */
11116 if (type == error_mark_node)
11117 return error_mark_node;
11118 return mangle_conv_op_name_for_type (type);
11121 /* Parse a conversion-type-id:
11123 conversion-type-id:
11124 type-specifier-seq conversion-declarator [opt]
11126 Returns the TYPE specified. */
11129 cp_parser_conversion_type_id (cp_parser* parser)
11132 cp_decl_specifier_seq type_specifiers;
11133 cp_declarator *declarator;
11134 tree type_specified;
11136 /* Parse the attributes. */
11137 attributes = cp_parser_attributes_opt (parser);
11138 /* Parse the type-specifiers. */
11139 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11140 /*is_trailing_return=*/false,
11142 /* If that didn't work, stop. */
11143 if (type_specifiers.type == error_mark_node)
11144 return error_mark_node;
11145 /* Parse the conversion-declarator. */
11146 declarator = cp_parser_conversion_declarator_opt (parser);
11148 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11149 /*initialized=*/0, &attributes);
11151 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11153 /* Don't give this error when parsing tentatively. This happens to
11154 work because we always parse this definitively once. */
11155 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11156 && type_uses_auto (type_specified))
11158 error ("invalid use of %<auto%> in conversion operator");
11159 return error_mark_node;
11162 return type_specified;
11165 /* Parse an (optional) conversion-declarator.
11167 conversion-declarator:
11168 ptr-operator conversion-declarator [opt]
11172 static cp_declarator *
11173 cp_parser_conversion_declarator_opt (cp_parser* parser)
11175 enum tree_code code;
11177 cp_cv_quals cv_quals;
11179 /* We don't know if there's a ptr-operator next, or not. */
11180 cp_parser_parse_tentatively (parser);
11181 /* Try the ptr-operator. */
11182 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11183 /* If it worked, look for more conversion-declarators. */
11184 if (cp_parser_parse_definitely (parser))
11186 cp_declarator *declarator;
11188 /* Parse another optional declarator. */
11189 declarator = cp_parser_conversion_declarator_opt (parser);
11191 return cp_parser_make_indirect_declarator
11192 (code, class_type, cv_quals, declarator);
11198 /* Parse an (optional) ctor-initializer.
11201 : mem-initializer-list
11203 Returns TRUE iff the ctor-initializer was actually present. */
11206 cp_parser_ctor_initializer_opt (cp_parser* parser)
11208 /* If the next token is not a `:', then there is no
11209 ctor-initializer. */
11210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11212 /* Do default initialization of any bases and members. */
11213 if (DECL_CONSTRUCTOR_P (current_function_decl))
11214 finish_mem_initializers (NULL_TREE);
11219 /* Consume the `:' token. */
11220 cp_lexer_consume_token (parser->lexer);
11221 /* And the mem-initializer-list. */
11222 cp_parser_mem_initializer_list (parser);
11227 /* Parse a mem-initializer-list.
11229 mem-initializer-list:
11230 mem-initializer ... [opt]
11231 mem-initializer ... [opt] , mem-initializer-list */
11234 cp_parser_mem_initializer_list (cp_parser* parser)
11236 tree mem_initializer_list = NULL_TREE;
11237 cp_token *token = cp_lexer_peek_token (parser->lexer);
11239 /* Let the semantic analysis code know that we are starting the
11240 mem-initializer-list. */
11241 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11242 error_at (token->location,
11243 "only constructors take member initializers");
11245 /* Loop through the list. */
11248 tree mem_initializer;
11250 token = cp_lexer_peek_token (parser->lexer);
11251 /* Parse the mem-initializer. */
11252 mem_initializer = cp_parser_mem_initializer (parser);
11253 /* If the next token is a `...', we're expanding member initializers. */
11254 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11256 /* Consume the `...'. */
11257 cp_lexer_consume_token (parser->lexer);
11259 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11260 can be expanded but members cannot. */
11261 if (mem_initializer != error_mark_node
11262 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11264 error_at (token->location,
11265 "cannot expand initializer for member %<%D%>",
11266 TREE_PURPOSE (mem_initializer));
11267 mem_initializer = error_mark_node;
11270 /* Construct the pack expansion type. */
11271 if (mem_initializer != error_mark_node)
11272 mem_initializer = make_pack_expansion (mem_initializer);
11274 /* Add it to the list, unless it was erroneous. */
11275 if (mem_initializer != error_mark_node)
11277 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11278 mem_initializer_list = mem_initializer;
11280 /* If the next token is not a `,', we're done. */
11281 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11283 /* Consume the `,' token. */
11284 cp_lexer_consume_token (parser->lexer);
11287 /* Perform semantic analysis. */
11288 if (DECL_CONSTRUCTOR_P (current_function_decl))
11289 finish_mem_initializers (mem_initializer_list);
11292 /* Parse a mem-initializer.
11295 mem-initializer-id ( expression-list [opt] )
11296 mem-initializer-id braced-init-list
11301 ( expression-list [opt] )
11303 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11304 class) or FIELD_DECL (for a non-static data member) to initialize;
11305 the TREE_VALUE is the expression-list. An empty initialization
11306 list is represented by void_list_node. */
11309 cp_parser_mem_initializer (cp_parser* parser)
11311 tree mem_initializer_id;
11312 tree expression_list;
11314 cp_token *token = cp_lexer_peek_token (parser->lexer);
11316 /* Find out what is being initialized. */
11317 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11319 permerror (token->location,
11320 "anachronistic old-style base class initializer");
11321 mem_initializer_id = NULL_TREE;
11325 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11326 if (mem_initializer_id == error_mark_node)
11327 return mem_initializer_id;
11329 member = expand_member_init (mem_initializer_id);
11330 if (member && !DECL_P (member))
11331 in_base_initializer = 1;
11333 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11335 bool expr_non_constant_p;
11336 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11337 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11338 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11339 expression_list = build_tree_list (NULL_TREE, expression_list);
11344 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11346 /*allow_expansion_p=*/true,
11347 /*non_constant_p=*/NULL);
11349 return error_mark_node;
11350 expression_list = build_tree_list_vec (vec);
11351 release_tree_vector (vec);
11354 if (expression_list == error_mark_node)
11355 return error_mark_node;
11356 if (!expression_list)
11357 expression_list = void_type_node;
11359 in_base_initializer = 0;
11361 return member ? build_tree_list (member, expression_list) : error_mark_node;
11364 /* Parse a mem-initializer-id.
11366 mem-initializer-id:
11367 :: [opt] nested-name-specifier [opt] class-name
11370 Returns a TYPE indicating the class to be initializer for the first
11371 production. Returns an IDENTIFIER_NODE indicating the data member
11372 to be initialized for the second production. */
11375 cp_parser_mem_initializer_id (cp_parser* parser)
11377 bool global_scope_p;
11378 bool nested_name_specifier_p;
11379 bool template_p = false;
11382 cp_token *token = cp_lexer_peek_token (parser->lexer);
11384 /* `typename' is not allowed in this context ([temp.res]). */
11385 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11387 error_at (token->location,
11388 "keyword %<typename%> not allowed in this context (a qualified "
11389 "member initializer is implicitly a type)");
11390 cp_lexer_consume_token (parser->lexer);
11392 /* Look for the optional `::' operator. */
11394 = (cp_parser_global_scope_opt (parser,
11395 /*current_scope_valid_p=*/false)
11397 /* Look for the optional nested-name-specifier. The simplest way to
11402 The keyword `typename' is not permitted in a base-specifier or
11403 mem-initializer; in these contexts a qualified name that
11404 depends on a template-parameter is implicitly assumed to be a
11407 is to assume that we have seen the `typename' keyword at this
11409 nested_name_specifier_p
11410 = (cp_parser_nested_name_specifier_opt (parser,
11411 /*typename_keyword_p=*/true,
11412 /*check_dependency_p=*/true,
11414 /*is_declaration=*/true)
11416 if (nested_name_specifier_p)
11417 template_p = cp_parser_optional_template_keyword (parser);
11418 /* If there is a `::' operator or a nested-name-specifier, then we
11419 are definitely looking for a class-name. */
11420 if (global_scope_p || nested_name_specifier_p)
11421 return cp_parser_class_name (parser,
11422 /*typename_keyword_p=*/true,
11423 /*template_keyword_p=*/template_p,
11425 /*check_dependency_p=*/true,
11426 /*class_head_p=*/false,
11427 /*is_declaration=*/true);
11428 /* Otherwise, we could also be looking for an ordinary identifier. */
11429 cp_parser_parse_tentatively (parser);
11430 /* Try a class-name. */
11431 id = cp_parser_class_name (parser,
11432 /*typename_keyword_p=*/true,
11433 /*template_keyword_p=*/false,
11435 /*check_dependency_p=*/true,
11436 /*class_head_p=*/false,
11437 /*is_declaration=*/true);
11438 /* If we found one, we're done. */
11439 if (cp_parser_parse_definitely (parser))
11441 /* Otherwise, look for an ordinary identifier. */
11442 return cp_parser_identifier (parser);
11445 /* Overloading [gram.over] */
11447 /* Parse an operator-function-id.
11449 operator-function-id:
11452 Returns an IDENTIFIER_NODE for the operator which is a
11453 human-readable spelling of the identifier, e.g., `operator +'. */
11456 cp_parser_operator_function_id (cp_parser* parser)
11458 /* Look for the `operator' keyword. */
11459 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11460 return error_mark_node;
11461 /* And then the name of the operator itself. */
11462 return cp_parser_operator (parser);
11465 /* Return an identifier node for a user-defined literal operator.
11466 The suffix identifier is chained to the operator name identifier. */
11469 cp_literal_operator_id (const char* name)
11472 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11473 + strlen (name) + 10);
11474 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11475 identifier = get_identifier (buffer);
11476 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11481 /* Parse an operator.
11484 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11485 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11486 || ++ -- , ->* -> () []
11493 Returns an IDENTIFIER_NODE for the operator which is a
11494 human-readable spelling of the identifier, e.g., `operator +'. */
11497 cp_parser_operator (cp_parser* parser)
11499 tree id = NULL_TREE;
11502 /* Peek at the next token. */
11503 token = cp_lexer_peek_token (parser->lexer);
11504 /* Figure out which operator we have. */
11505 switch (token->type)
11511 /* The keyword should be either `new' or `delete'. */
11512 if (token->keyword == RID_NEW)
11514 else if (token->keyword == RID_DELETE)
11519 /* Consume the `new' or `delete' token. */
11520 cp_lexer_consume_token (parser->lexer);
11522 /* Peek at the next token. */
11523 token = cp_lexer_peek_token (parser->lexer);
11524 /* If it's a `[' token then this is the array variant of the
11526 if (token->type == CPP_OPEN_SQUARE)
11528 /* Consume the `[' token. */
11529 cp_lexer_consume_token (parser->lexer);
11530 /* Look for the `]' token. */
11531 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11532 id = ansi_opname (op == NEW_EXPR
11533 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11535 /* Otherwise, we have the non-array variant. */
11537 id = ansi_opname (op);
11543 id = ansi_opname (PLUS_EXPR);
11547 id = ansi_opname (MINUS_EXPR);
11551 id = ansi_opname (MULT_EXPR);
11555 id = ansi_opname (TRUNC_DIV_EXPR);
11559 id = ansi_opname (TRUNC_MOD_EXPR);
11563 id = ansi_opname (BIT_XOR_EXPR);
11567 id = ansi_opname (BIT_AND_EXPR);
11571 id = ansi_opname (BIT_IOR_EXPR);
11575 id = ansi_opname (BIT_NOT_EXPR);
11579 id = ansi_opname (TRUTH_NOT_EXPR);
11583 id = ansi_assopname (NOP_EXPR);
11587 id = ansi_opname (LT_EXPR);
11591 id = ansi_opname (GT_EXPR);
11595 id = ansi_assopname (PLUS_EXPR);
11599 id = ansi_assopname (MINUS_EXPR);
11603 id = ansi_assopname (MULT_EXPR);
11607 id = ansi_assopname (TRUNC_DIV_EXPR);
11611 id = ansi_assopname (TRUNC_MOD_EXPR);
11615 id = ansi_assopname (BIT_XOR_EXPR);
11619 id = ansi_assopname (BIT_AND_EXPR);
11623 id = ansi_assopname (BIT_IOR_EXPR);
11627 id = ansi_opname (LSHIFT_EXPR);
11631 id = ansi_opname (RSHIFT_EXPR);
11634 case CPP_LSHIFT_EQ:
11635 id = ansi_assopname (LSHIFT_EXPR);
11638 case CPP_RSHIFT_EQ:
11639 id = ansi_assopname (RSHIFT_EXPR);
11643 id = ansi_opname (EQ_EXPR);
11647 id = ansi_opname (NE_EXPR);
11651 id = ansi_opname (LE_EXPR);
11654 case CPP_GREATER_EQ:
11655 id = ansi_opname (GE_EXPR);
11659 id = ansi_opname (TRUTH_ANDIF_EXPR);
11663 id = ansi_opname (TRUTH_ORIF_EXPR);
11666 case CPP_PLUS_PLUS:
11667 id = ansi_opname (POSTINCREMENT_EXPR);
11670 case CPP_MINUS_MINUS:
11671 id = ansi_opname (PREDECREMENT_EXPR);
11675 id = ansi_opname (COMPOUND_EXPR);
11678 case CPP_DEREF_STAR:
11679 id = ansi_opname (MEMBER_REF);
11683 id = ansi_opname (COMPONENT_REF);
11686 case CPP_OPEN_PAREN:
11687 /* Consume the `('. */
11688 cp_lexer_consume_token (parser->lexer);
11689 /* Look for the matching `)'. */
11690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11691 return ansi_opname (CALL_EXPR);
11693 case CPP_OPEN_SQUARE:
11694 /* Consume the `['. */
11695 cp_lexer_consume_token (parser->lexer);
11696 /* Look for the matching `]'. */
11697 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11698 return ansi_opname (ARRAY_REF);
11701 if (cxx_dialect == cxx98)
11702 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11703 if (TREE_STRING_LENGTH (token->u.value) > 2)
11705 error ("expected empty string after %<operator%> keyword");
11706 return error_mark_node;
11708 /* Consume the string. */
11709 cp_lexer_consume_token (parser->lexer);
11710 /* Look for the suffix identifier. */
11711 token = cp_lexer_peek_token (parser->lexer);
11712 if (token->type == CPP_NAME)
11714 id = cp_parser_identifier (parser);
11715 if (id != error_mark_node)
11717 const char *name = IDENTIFIER_POINTER (id);
11718 return cp_literal_operator_id (name);
11723 error ("expected suffix identifier");
11724 return error_mark_node;
11727 case CPP_STRING_USERDEF:
11728 error ("missing space between %<\"\"%> and suffix identifier");
11729 return error_mark_node;
11732 /* Anything else is an error. */
11736 /* If we have selected an identifier, we need to consume the
11739 cp_lexer_consume_token (parser->lexer);
11740 /* Otherwise, no valid operator name was present. */
11743 cp_parser_error (parser, "expected operator");
11744 id = error_mark_node;
11750 /* Parse a template-declaration.
11752 template-declaration:
11753 export [opt] template < template-parameter-list > declaration
11755 If MEMBER_P is TRUE, this template-declaration occurs within a
11758 The grammar rule given by the standard isn't correct. What
11759 is really meant is:
11761 template-declaration:
11762 export [opt] template-parameter-list-seq
11763 decl-specifier-seq [opt] init-declarator [opt] ;
11764 export [opt] template-parameter-list-seq
11765 function-definition
11767 template-parameter-list-seq:
11768 template-parameter-list-seq [opt]
11769 template < template-parameter-list > */
11772 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11774 /* Check for `export'. */
11775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11777 /* Consume the `export' token. */
11778 cp_lexer_consume_token (parser->lexer);
11779 /* Warn that we do not support `export'. */
11780 warning (0, "keyword %<export%> not implemented, and will be ignored");
11783 cp_parser_template_declaration_after_export (parser, member_p);
11786 /* Parse a template-parameter-list.
11788 template-parameter-list:
11790 template-parameter-list , template-parameter
11792 Returns a TREE_LIST. Each node represents a template parameter.
11793 The nodes are connected via their TREE_CHAINs. */
11796 cp_parser_template_parameter_list (cp_parser* parser)
11798 tree parameter_list = NULL_TREE;
11800 begin_template_parm_list ();
11802 /* The loop below parses the template parms. We first need to know
11803 the total number of template parms to be able to compute proper
11804 canonical types of each dependent type. So after the loop, when
11805 we know the total number of template parms,
11806 end_template_parm_list computes the proper canonical types and
11807 fixes up the dependent types accordingly. */
11812 bool is_parameter_pack;
11813 location_t parm_loc;
11815 /* Parse the template-parameter. */
11816 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11817 parameter = cp_parser_template_parameter (parser,
11819 &is_parameter_pack);
11820 /* Add it to the list. */
11821 if (parameter != error_mark_node)
11822 parameter_list = process_template_parm (parameter_list,
11830 tree err_parm = build_tree_list (parameter, parameter);
11831 parameter_list = chainon (parameter_list, err_parm);
11834 /* If the next token is not a `,', we're done. */
11835 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11837 /* Otherwise, consume the `,' token. */
11838 cp_lexer_consume_token (parser->lexer);
11841 return end_template_parm_list (parameter_list);
11844 /* Parse a template-parameter.
11846 template-parameter:
11848 parameter-declaration
11850 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11851 the parameter. The TREE_PURPOSE is the default value, if any.
11852 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11853 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11854 set to true iff this parameter is a parameter pack. */
11857 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11858 bool *is_parameter_pack)
11861 cp_parameter_declarator *parameter_declarator;
11862 cp_declarator *id_declarator;
11865 /* Assume it is a type parameter or a template parameter. */
11866 *is_non_type = false;
11867 /* Assume it not a parameter pack. */
11868 *is_parameter_pack = false;
11869 /* Peek at the next token. */
11870 token = cp_lexer_peek_token (parser->lexer);
11871 /* If it is `class' or `template', we have a type-parameter. */
11872 if (token->keyword == RID_TEMPLATE)
11873 return cp_parser_type_parameter (parser, is_parameter_pack);
11874 /* If it is `class' or `typename' we do not know yet whether it is a
11875 type parameter or a non-type parameter. Consider:
11877 template <typename T, typename T::X X> ...
11881 template <class C, class D*> ...
11883 Here, the first parameter is a type parameter, and the second is
11884 a non-type parameter. We can tell by looking at the token after
11885 the identifier -- if it is a `,', `=', or `>' then we have a type
11887 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11889 /* Peek at the token after `class' or `typename'. */
11890 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11891 /* If it's an ellipsis, we have a template type parameter
11893 if (token->type == CPP_ELLIPSIS)
11894 return cp_parser_type_parameter (parser, is_parameter_pack);
11895 /* If it's an identifier, skip it. */
11896 if (token->type == CPP_NAME)
11897 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11898 /* Now, see if the token looks like the end of a template
11900 if (token->type == CPP_COMMA
11901 || token->type == CPP_EQ
11902 || token->type == CPP_GREATER)
11903 return cp_parser_type_parameter (parser, is_parameter_pack);
11906 /* Otherwise, it is a non-type parameter.
11910 When parsing a default template-argument for a non-type
11911 template-parameter, the first non-nested `>' is taken as the end
11912 of the template parameter-list rather than a greater-than
11914 *is_non_type = true;
11915 parameter_declarator
11916 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11917 /*parenthesized_p=*/NULL);
11919 /* If the parameter declaration is marked as a parameter pack, set
11920 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11921 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11923 if (parameter_declarator
11924 && parameter_declarator->declarator
11925 && parameter_declarator->declarator->parameter_pack_p)
11927 *is_parameter_pack = true;
11928 parameter_declarator->declarator->parameter_pack_p = false;
11931 /* If the next token is an ellipsis, and we don't already have it
11932 marked as a parameter pack, then we have a parameter pack (that
11933 has no declarator). */
11934 if (!*is_parameter_pack
11935 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11936 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11938 /* Consume the `...'. */
11939 cp_lexer_consume_token (parser->lexer);
11940 maybe_warn_variadic_templates ();
11942 *is_parameter_pack = true;
11944 /* We might end up with a pack expansion as the type of the non-type
11945 template parameter, in which case this is a non-type template
11947 else if (parameter_declarator
11948 && parameter_declarator->decl_specifiers.type
11949 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11951 *is_parameter_pack = true;
11952 parameter_declarator->decl_specifiers.type =
11953 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11956 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11958 /* Parameter packs cannot have default arguments. However, a
11959 user may try to do so, so we'll parse them and give an
11960 appropriate diagnostic here. */
11962 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11964 /* Find the name of the parameter pack. */
11965 id_declarator = parameter_declarator->declarator;
11966 while (id_declarator && id_declarator->kind != cdk_id)
11967 id_declarator = id_declarator->declarator;
11969 if (id_declarator && id_declarator->kind == cdk_id)
11970 error_at (start_token->location,
11971 "template parameter pack %qD cannot have a default argument",
11972 id_declarator->u.id.unqualified_name);
11974 error_at (start_token->location,
11975 "template parameter pack cannot have a default argument");
11977 /* Parse the default argument, but throw away the result. */
11978 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11981 parm = grokdeclarator (parameter_declarator->declarator,
11982 ¶meter_declarator->decl_specifiers,
11983 TPARM, /*initialized=*/0,
11984 /*attrlist=*/NULL);
11985 if (parm == error_mark_node)
11986 return error_mark_node;
11988 return build_tree_list (parameter_declarator->default_argument, parm);
11991 /* Parse a type-parameter.
11994 class identifier [opt]
11995 class identifier [opt] = type-id
11996 typename identifier [opt]
11997 typename identifier [opt] = type-id
11998 template < template-parameter-list > class identifier [opt]
11999 template < template-parameter-list > class identifier [opt]
12002 GNU Extension (variadic templates):
12005 class ... identifier [opt]
12006 typename ... identifier [opt]
12008 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12009 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12010 the declaration of the parameter.
12012 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12015 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12020 /* Look for a keyword to tell us what kind of parameter this is. */
12021 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12023 return error_mark_node;
12025 switch (token->keyword)
12031 tree default_argument;
12033 /* If the next token is an ellipsis, we have a template
12035 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12037 /* Consume the `...' token. */
12038 cp_lexer_consume_token (parser->lexer);
12039 maybe_warn_variadic_templates ();
12041 *is_parameter_pack = true;
12044 /* If the next token is an identifier, then it names the
12046 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12047 identifier = cp_parser_identifier (parser);
12049 identifier = NULL_TREE;
12051 /* Create the parameter. */
12052 parameter = finish_template_type_parm (class_type_node, identifier);
12054 /* If the next token is an `=', we have a default argument. */
12055 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12057 /* Consume the `=' token. */
12058 cp_lexer_consume_token (parser->lexer);
12059 /* Parse the default-argument. */
12060 push_deferring_access_checks (dk_no_deferred);
12061 default_argument = cp_parser_type_id (parser);
12063 /* Template parameter packs cannot have default
12065 if (*is_parameter_pack)
12068 error_at (token->location,
12069 "template parameter pack %qD cannot have a "
12070 "default argument", identifier);
12072 error_at (token->location,
12073 "template parameter packs cannot have "
12074 "default arguments");
12075 default_argument = NULL_TREE;
12077 pop_deferring_access_checks ();
12080 default_argument = NULL_TREE;
12082 /* Create the combined representation of the parameter and the
12083 default argument. */
12084 parameter = build_tree_list (default_argument, parameter);
12091 tree default_argument;
12093 /* Look for the `<'. */
12094 cp_parser_require (parser, CPP_LESS, RT_LESS);
12095 /* Parse the template-parameter-list. */
12096 cp_parser_template_parameter_list (parser);
12097 /* Look for the `>'. */
12098 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12099 /* Look for the `class' keyword. */
12100 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12101 /* If the next token is an ellipsis, we have a template
12103 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12105 /* Consume the `...' token. */
12106 cp_lexer_consume_token (parser->lexer);
12107 maybe_warn_variadic_templates ();
12109 *is_parameter_pack = true;
12111 /* If the next token is an `=', then there is a
12112 default-argument. If the next token is a `>', we are at
12113 the end of the parameter-list. If the next token is a `,',
12114 then we are at the end of this parameter. */
12115 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12116 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12117 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12119 identifier = cp_parser_identifier (parser);
12120 /* Treat invalid names as if the parameter were nameless. */
12121 if (identifier == error_mark_node)
12122 identifier = NULL_TREE;
12125 identifier = NULL_TREE;
12127 /* Create the template parameter. */
12128 parameter = finish_template_template_parm (class_type_node,
12131 /* If the next token is an `=', then there is a
12132 default-argument. */
12133 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12137 /* Consume the `='. */
12138 cp_lexer_consume_token (parser->lexer);
12139 /* Parse the id-expression. */
12140 push_deferring_access_checks (dk_no_deferred);
12141 /* save token before parsing the id-expression, for error
12143 token = cp_lexer_peek_token (parser->lexer);
12145 = cp_parser_id_expression (parser,
12146 /*template_keyword_p=*/false,
12147 /*check_dependency_p=*/true,
12148 /*template_p=*/&is_template,
12149 /*declarator_p=*/false,
12150 /*optional_p=*/false);
12151 if (TREE_CODE (default_argument) == TYPE_DECL)
12152 /* If the id-expression was a template-id that refers to
12153 a template-class, we already have the declaration here,
12154 so no further lookup is needed. */
12157 /* Look up the name. */
12159 = cp_parser_lookup_name (parser, default_argument,
12161 /*is_template=*/is_template,
12162 /*is_namespace=*/false,
12163 /*check_dependency=*/true,
12164 /*ambiguous_decls=*/NULL,
12166 /* See if the default argument is valid. */
12168 = check_template_template_default_arg (default_argument);
12170 /* Template parameter packs cannot have default
12172 if (*is_parameter_pack)
12175 error_at (token->location,
12176 "template parameter pack %qD cannot "
12177 "have a default argument",
12180 error_at (token->location, "template parameter packs cannot "
12181 "have default arguments");
12182 default_argument = NULL_TREE;
12184 pop_deferring_access_checks ();
12187 default_argument = NULL_TREE;
12189 /* Create the combined representation of the parameter and the
12190 default argument. */
12191 parameter = build_tree_list (default_argument, parameter);
12196 gcc_unreachable ();
12203 /* Parse a template-id.
12206 template-name < template-argument-list [opt] >
12208 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12209 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12210 returned. Otherwise, if the template-name names a function, or set
12211 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12212 names a class, returns a TYPE_DECL for the specialization.
12214 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12215 uninstantiated templates. */
12218 cp_parser_template_id (cp_parser *parser,
12219 bool template_keyword_p,
12220 bool check_dependency_p,
12221 bool is_declaration)
12227 cp_token_position start_of_id = 0;
12228 deferred_access_check *chk;
12229 VEC (deferred_access_check,gc) *access_check;
12230 cp_token *next_token = NULL, *next_token_2 = NULL;
12231 bool is_identifier;
12233 /* If the next token corresponds to a template-id, there is no need
12235 next_token = cp_lexer_peek_token (parser->lexer);
12236 if (next_token->type == CPP_TEMPLATE_ID)
12238 struct tree_check *check_value;
12240 /* Get the stored value. */
12241 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12242 /* Perform any access checks that were deferred. */
12243 access_check = check_value->checks;
12246 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12247 perform_or_defer_access_check (chk->binfo,
12251 /* Return the stored value. */
12252 return check_value->value;
12255 /* Avoid performing name lookup if there is no possibility of
12256 finding a template-id. */
12257 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12258 || (next_token->type == CPP_NAME
12259 && !cp_parser_nth_token_starts_template_argument_list_p
12262 cp_parser_error (parser, "expected template-id");
12263 return error_mark_node;
12266 /* Remember where the template-id starts. */
12267 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12268 start_of_id = cp_lexer_token_position (parser->lexer, false);
12270 push_deferring_access_checks (dk_deferred);
12272 /* Parse the template-name. */
12273 is_identifier = false;
12274 templ = cp_parser_template_name (parser, template_keyword_p,
12275 check_dependency_p,
12278 if (templ == error_mark_node || is_identifier)
12280 pop_deferring_access_checks ();
12284 /* If we find the sequence `[:' after a template-name, it's probably
12285 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12286 parse correctly the argument list. */
12287 next_token = cp_lexer_peek_token (parser->lexer);
12288 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12289 if (next_token->type == CPP_OPEN_SQUARE
12290 && next_token->flags & DIGRAPH
12291 && next_token_2->type == CPP_COLON
12292 && !(next_token_2->flags & PREV_WHITE))
12294 cp_parser_parse_tentatively (parser);
12295 /* Change `:' into `::'. */
12296 next_token_2->type = CPP_SCOPE;
12297 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12299 cp_lexer_consume_token (parser->lexer);
12301 /* Parse the arguments. */
12302 arguments = cp_parser_enclosed_template_argument_list (parser);
12303 if (!cp_parser_parse_definitely (parser))
12305 /* If we couldn't parse an argument list, then we revert our changes
12306 and return simply an error. Maybe this is not a template-id
12308 next_token_2->type = CPP_COLON;
12309 cp_parser_error (parser, "expected %<<%>");
12310 pop_deferring_access_checks ();
12311 return error_mark_node;
12313 /* Otherwise, emit an error about the invalid digraph, but continue
12314 parsing because we got our argument list. */
12315 if (permerror (next_token->location,
12316 "%<<::%> cannot begin a template-argument list"))
12318 static bool hint = false;
12319 inform (next_token->location,
12320 "%<<:%> is an alternate spelling for %<[%>."
12321 " Insert whitespace between %<<%> and %<::%>");
12322 if (!hint && !flag_permissive)
12324 inform (next_token->location, "(if you use %<-fpermissive%>"
12325 " G++ will accept your code)");
12332 /* Look for the `<' that starts the template-argument-list. */
12333 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12335 pop_deferring_access_checks ();
12336 return error_mark_node;
12338 /* Parse the arguments. */
12339 arguments = cp_parser_enclosed_template_argument_list (parser);
12342 /* Build a representation of the specialization. */
12343 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12344 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12345 else if (DECL_CLASS_TEMPLATE_P (templ)
12346 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12348 bool entering_scope;
12349 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12350 template (rather than some instantiation thereof) only if
12351 is not nested within some other construct. For example, in
12352 "template <typename T> void f(T) { A<T>::", A<T> is just an
12353 instantiation of A. */
12354 entering_scope = (template_parm_scope_p ()
12355 && cp_lexer_next_token_is (parser->lexer,
12358 = finish_template_type (templ, arguments, entering_scope);
12362 /* If it's not a class-template or a template-template, it should be
12363 a function-template. */
12364 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12365 || TREE_CODE (templ) == OVERLOAD
12366 || BASELINK_P (templ)));
12368 template_id = lookup_template_function (templ, arguments);
12371 /* If parsing tentatively, replace the sequence of tokens that makes
12372 up the template-id with a CPP_TEMPLATE_ID token. That way,
12373 should we re-parse the token stream, we will not have to repeat
12374 the effort required to do the parse, nor will we issue duplicate
12375 error messages about problems during instantiation of the
12379 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12381 /* Reset the contents of the START_OF_ID token. */
12382 token->type = CPP_TEMPLATE_ID;
12383 /* Retrieve any deferred checks. Do not pop this access checks yet
12384 so the memory will not be reclaimed during token replacing below. */
12385 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12386 token->u.tree_check_value->value = template_id;
12387 token->u.tree_check_value->checks = get_deferred_access_checks ();
12388 token->keyword = RID_MAX;
12390 /* Purge all subsequent tokens. */
12391 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12393 /* ??? Can we actually assume that, if template_id ==
12394 error_mark_node, we will have issued a diagnostic to the
12395 user, as opposed to simply marking the tentative parse as
12397 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12398 error_at (token->location, "parse error in template argument list");
12401 pop_deferring_access_checks ();
12402 return template_id;
12405 /* Parse a template-name.
12410 The standard should actually say:
12414 operator-function-id
12416 A defect report has been filed about this issue.
12418 A conversion-function-id cannot be a template name because they cannot
12419 be part of a template-id. In fact, looking at this code:
12421 a.operator K<int>()
12423 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12424 It is impossible to call a templated conversion-function-id with an
12425 explicit argument list, since the only allowed template parameter is
12426 the type to which it is converting.
12428 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12429 `template' keyword, in a construction like:
12433 In that case `f' is taken to be a template-name, even though there
12434 is no way of knowing for sure.
12436 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12437 name refers to a set of overloaded functions, at least one of which
12438 is a template, or an IDENTIFIER_NODE with the name of the template,
12439 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12440 names are looked up inside uninstantiated templates. */
12443 cp_parser_template_name (cp_parser* parser,
12444 bool template_keyword_p,
12445 bool check_dependency_p,
12446 bool is_declaration,
12447 bool *is_identifier)
12452 cp_token *token = cp_lexer_peek_token (parser->lexer);
12454 /* If the next token is `operator', then we have either an
12455 operator-function-id or a conversion-function-id. */
12456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12458 /* We don't know whether we're looking at an
12459 operator-function-id or a conversion-function-id. */
12460 cp_parser_parse_tentatively (parser);
12461 /* Try an operator-function-id. */
12462 identifier = cp_parser_operator_function_id (parser);
12463 /* If that didn't work, try a conversion-function-id. */
12464 if (!cp_parser_parse_definitely (parser))
12466 cp_parser_error (parser, "expected template-name");
12467 return error_mark_node;
12470 /* Look for the identifier. */
12472 identifier = cp_parser_identifier (parser);
12474 /* If we didn't find an identifier, we don't have a template-id. */
12475 if (identifier == error_mark_node)
12476 return error_mark_node;
12478 /* If the name immediately followed the `template' keyword, then it
12479 is a template-name. However, if the next token is not `<', then
12480 we do not treat it as a template-name, since it is not being used
12481 as part of a template-id. This enables us to handle constructs
12484 template <typename T> struct S { S(); };
12485 template <typename T> S<T>::S();
12487 correctly. We would treat `S' as a template -- if it were `S<T>'
12488 -- but we do not if there is no `<'. */
12490 if (processing_template_decl
12491 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12493 /* In a declaration, in a dependent context, we pretend that the
12494 "template" keyword was present in order to improve error
12495 recovery. For example, given:
12497 template <typename T> void f(T::X<int>);
12499 we want to treat "X<int>" as a template-id. */
12501 && !template_keyword_p
12502 && parser->scope && TYPE_P (parser->scope)
12503 && check_dependency_p
12504 && dependent_scope_p (parser->scope)
12505 /* Do not do this for dtors (or ctors), since they never
12506 need the template keyword before their name. */
12507 && !constructor_name_p (identifier, parser->scope))
12509 cp_token_position start = 0;
12511 /* Explain what went wrong. */
12512 error_at (token->location, "non-template %qD used as template",
12514 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12515 parser->scope, identifier);
12516 /* If parsing tentatively, find the location of the "<" token. */
12517 if (cp_parser_simulate_error (parser))
12518 start = cp_lexer_token_position (parser->lexer, true);
12519 /* Parse the template arguments so that we can issue error
12520 messages about them. */
12521 cp_lexer_consume_token (parser->lexer);
12522 cp_parser_enclosed_template_argument_list (parser);
12523 /* Skip tokens until we find a good place from which to
12524 continue parsing. */
12525 cp_parser_skip_to_closing_parenthesis (parser,
12526 /*recovering=*/true,
12528 /*consume_paren=*/false);
12529 /* If parsing tentatively, permanently remove the
12530 template argument list. That will prevent duplicate
12531 error messages from being issued about the missing
12532 "template" keyword. */
12534 cp_lexer_purge_tokens_after (parser->lexer, start);
12536 *is_identifier = true;
12540 /* If the "template" keyword is present, then there is generally
12541 no point in doing name-lookup, so we just return IDENTIFIER.
12542 But, if the qualifying scope is non-dependent then we can
12543 (and must) do name-lookup normally. */
12544 if (template_keyword_p
12546 || (TYPE_P (parser->scope)
12547 && dependent_type_p (parser->scope))))
12551 /* Look up the name. */
12552 decl = cp_parser_lookup_name (parser, identifier,
12554 /*is_template=*/true,
12555 /*is_namespace=*/false,
12556 check_dependency_p,
12557 /*ambiguous_decls=*/NULL,
12560 /* If DECL is a template, then the name was a template-name. */
12561 if (TREE_CODE (decl) == TEMPLATE_DECL)
12565 tree fn = NULL_TREE;
12567 /* The standard does not explicitly indicate whether a name that
12568 names a set of overloaded declarations, some of which are
12569 templates, is a template-name. However, such a name should
12570 be a template-name; otherwise, there is no way to form a
12571 template-id for the overloaded templates. */
12572 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12573 if (TREE_CODE (fns) == OVERLOAD)
12574 for (fn = fns; fn; fn = OVL_NEXT (fn))
12575 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12580 /* The name does not name a template. */
12581 cp_parser_error (parser, "expected template-name");
12582 return error_mark_node;
12586 /* If DECL is dependent, and refers to a function, then just return
12587 its name; we will look it up again during template instantiation. */
12588 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12590 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12591 if (TYPE_P (scope) && dependent_type_p (scope))
12598 /* Parse a template-argument-list.
12600 template-argument-list:
12601 template-argument ... [opt]
12602 template-argument-list , template-argument ... [opt]
12604 Returns a TREE_VEC containing the arguments. */
12607 cp_parser_template_argument_list (cp_parser* parser)
12609 tree fixed_args[10];
12610 unsigned n_args = 0;
12611 unsigned alloced = 10;
12612 tree *arg_ary = fixed_args;
12614 bool saved_in_template_argument_list_p;
12616 bool saved_non_ice_p;
12618 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12619 parser->in_template_argument_list_p = true;
12620 /* Even if the template-id appears in an integral
12621 constant-expression, the contents of the argument list do
12623 saved_ice_p = parser->integral_constant_expression_p;
12624 parser->integral_constant_expression_p = false;
12625 saved_non_ice_p = parser->non_integral_constant_expression_p;
12626 parser->non_integral_constant_expression_p = false;
12628 /* Parse the arguments. */
12634 /* Consume the comma. */
12635 cp_lexer_consume_token (parser->lexer);
12637 /* Parse the template-argument. */
12638 argument = cp_parser_template_argument (parser);
12640 /* If the next token is an ellipsis, we're expanding a template
12642 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12644 if (argument == error_mark_node)
12646 cp_token *token = cp_lexer_peek_token (parser->lexer);
12647 error_at (token->location,
12648 "expected parameter pack before %<...%>");
12650 /* Consume the `...' token. */
12651 cp_lexer_consume_token (parser->lexer);
12653 /* Make the argument into a TYPE_PACK_EXPANSION or
12654 EXPR_PACK_EXPANSION. */
12655 argument = make_pack_expansion (argument);
12658 if (n_args == alloced)
12662 if (arg_ary == fixed_args)
12664 arg_ary = XNEWVEC (tree, alloced);
12665 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12668 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12670 arg_ary[n_args++] = argument;
12672 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12674 vec = make_tree_vec (n_args);
12677 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12679 if (arg_ary != fixed_args)
12681 parser->non_integral_constant_expression_p = saved_non_ice_p;
12682 parser->integral_constant_expression_p = saved_ice_p;
12683 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12684 #ifdef ENABLE_CHECKING
12685 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12690 /* Parse a template-argument.
12693 assignment-expression
12697 The representation is that of an assignment-expression, type-id, or
12698 id-expression -- except that the qualified id-expression is
12699 evaluated, so that the value returned is either a DECL or an
12702 Although the standard says "assignment-expression", it forbids
12703 throw-expressions or assignments in the template argument.
12704 Therefore, we use "conditional-expression" instead. */
12707 cp_parser_template_argument (cp_parser* parser)
12712 bool maybe_type_id = false;
12713 cp_token *token = NULL, *argument_start_token = NULL;
12716 /* There's really no way to know what we're looking at, so we just
12717 try each alternative in order.
12721 In a template-argument, an ambiguity between a type-id and an
12722 expression is resolved to a type-id, regardless of the form of
12723 the corresponding template-parameter.
12725 Therefore, we try a type-id first. */
12726 cp_parser_parse_tentatively (parser);
12727 argument = cp_parser_template_type_arg (parser);
12728 /* If there was no error parsing the type-id but the next token is a
12729 '>>', our behavior depends on which dialect of C++ we're
12730 parsing. In C++98, we probably found a typo for '> >'. But there
12731 are type-id which are also valid expressions. For instance:
12733 struct X { int operator >> (int); };
12734 template <int V> struct Foo {};
12737 Here 'X()' is a valid type-id of a function type, but the user just
12738 wanted to write the expression "X() >> 5". Thus, we remember that we
12739 found a valid type-id, but we still try to parse the argument as an
12740 expression to see what happens.
12742 In C++0x, the '>>' will be considered two separate '>'
12744 if (!cp_parser_error_occurred (parser)
12745 && cxx_dialect == cxx98
12746 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12748 maybe_type_id = true;
12749 cp_parser_abort_tentative_parse (parser);
12753 /* If the next token isn't a `,' or a `>', then this argument wasn't
12754 really finished. This means that the argument is not a valid
12756 if (!cp_parser_next_token_ends_template_argument_p (parser))
12757 cp_parser_error (parser, "expected template-argument");
12758 /* If that worked, we're done. */
12759 if (cp_parser_parse_definitely (parser))
12762 /* We're still not sure what the argument will be. */
12763 cp_parser_parse_tentatively (parser);
12764 /* Try a template. */
12765 argument_start_token = cp_lexer_peek_token (parser->lexer);
12766 argument = cp_parser_id_expression (parser,
12767 /*template_keyword_p=*/false,
12768 /*check_dependency_p=*/true,
12770 /*declarator_p=*/false,
12771 /*optional_p=*/false);
12772 /* If the next token isn't a `,' or a `>', then this argument wasn't
12773 really finished. */
12774 if (!cp_parser_next_token_ends_template_argument_p (parser))
12775 cp_parser_error (parser, "expected template-argument");
12776 if (!cp_parser_error_occurred (parser))
12778 /* Figure out what is being referred to. If the id-expression
12779 was for a class template specialization, then we will have a
12780 TYPE_DECL at this point. There is no need to do name lookup
12781 at this point in that case. */
12782 if (TREE_CODE (argument) != TYPE_DECL)
12783 argument = cp_parser_lookup_name (parser, argument,
12785 /*is_template=*/template_p,
12786 /*is_namespace=*/false,
12787 /*check_dependency=*/true,
12788 /*ambiguous_decls=*/NULL,
12789 argument_start_token->location);
12790 if (TREE_CODE (argument) != TEMPLATE_DECL
12791 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12792 cp_parser_error (parser, "expected template-name");
12794 if (cp_parser_parse_definitely (parser))
12796 /* It must be a non-type argument. There permitted cases are given
12797 in [temp.arg.nontype]:
12799 -- an integral constant-expression of integral or enumeration
12802 -- the name of a non-type template-parameter; or
12804 -- the name of an object or function with external linkage...
12806 -- the address of an object or function with external linkage...
12808 -- a pointer to member... */
12809 /* Look for a non-type template parameter. */
12810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12812 cp_parser_parse_tentatively (parser);
12813 argument = cp_parser_primary_expression (parser,
12814 /*address_p=*/false,
12816 /*template_arg_p=*/true,
12818 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12819 || !cp_parser_next_token_ends_template_argument_p (parser))
12820 cp_parser_simulate_error (parser);
12821 if (cp_parser_parse_definitely (parser))
12825 /* If the next token is "&", the argument must be the address of an
12826 object or function with external linkage. */
12827 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12829 cp_lexer_consume_token (parser->lexer);
12830 /* See if we might have an id-expression. */
12831 token = cp_lexer_peek_token (parser->lexer);
12832 if (token->type == CPP_NAME
12833 || token->keyword == RID_OPERATOR
12834 || token->type == CPP_SCOPE
12835 || token->type == CPP_TEMPLATE_ID
12836 || token->type == CPP_NESTED_NAME_SPECIFIER)
12838 cp_parser_parse_tentatively (parser);
12839 argument = cp_parser_primary_expression (parser,
12842 /*template_arg_p=*/true,
12844 if (cp_parser_error_occurred (parser)
12845 || !cp_parser_next_token_ends_template_argument_p (parser))
12846 cp_parser_abort_tentative_parse (parser);
12851 if (TREE_CODE (argument) == INDIRECT_REF)
12853 gcc_assert (REFERENCE_REF_P (argument));
12854 argument = TREE_OPERAND (argument, 0);
12857 /* If we're in a template, we represent a qualified-id referring
12858 to a static data member as a SCOPE_REF even if the scope isn't
12859 dependent so that we can check access control later. */
12861 if (TREE_CODE (probe) == SCOPE_REF)
12862 probe = TREE_OPERAND (probe, 1);
12863 if (TREE_CODE (probe) == VAR_DECL)
12865 /* A variable without external linkage might still be a
12866 valid constant-expression, so no error is issued here
12867 if the external-linkage check fails. */
12868 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12869 cp_parser_simulate_error (parser);
12871 else if (is_overloaded_fn (argument))
12872 /* All overloaded functions are allowed; if the external
12873 linkage test does not pass, an error will be issued
12877 && (TREE_CODE (argument) == OFFSET_REF
12878 || TREE_CODE (argument) == SCOPE_REF))
12879 /* A pointer-to-member. */
12881 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12884 cp_parser_simulate_error (parser);
12886 if (cp_parser_parse_definitely (parser))
12889 argument = build_x_unary_op (ADDR_EXPR, argument,
12890 tf_warning_or_error);
12895 /* If the argument started with "&", there are no other valid
12896 alternatives at this point. */
12899 cp_parser_error (parser, "invalid non-type template argument");
12900 return error_mark_node;
12903 /* If the argument wasn't successfully parsed as a type-id followed
12904 by '>>', the argument can only be a constant expression now.
12905 Otherwise, we try parsing the constant-expression tentatively,
12906 because the argument could really be a type-id. */
12908 cp_parser_parse_tentatively (parser);
12909 argument = cp_parser_constant_expression (parser,
12910 /*allow_non_constant_p=*/false,
12911 /*non_constant_p=*/NULL);
12912 argument = fold_non_dependent_expr (argument);
12913 if (!maybe_type_id)
12915 if (!cp_parser_next_token_ends_template_argument_p (parser))
12916 cp_parser_error (parser, "expected template-argument");
12917 if (cp_parser_parse_definitely (parser))
12919 /* We did our best to parse the argument as a non type-id, but that
12920 was the only alternative that matched (albeit with a '>' after
12921 it). We can assume it's just a typo from the user, and a
12922 diagnostic will then be issued. */
12923 return cp_parser_template_type_arg (parser);
12926 /* Parse an explicit-instantiation.
12928 explicit-instantiation:
12929 template declaration
12931 Although the standard says `declaration', what it really means is:
12933 explicit-instantiation:
12934 template decl-specifier-seq [opt] declarator [opt] ;
12936 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12937 supposed to be allowed. A defect report has been filed about this
12942 explicit-instantiation:
12943 storage-class-specifier template
12944 decl-specifier-seq [opt] declarator [opt] ;
12945 function-specifier template
12946 decl-specifier-seq [opt] declarator [opt] ; */
12949 cp_parser_explicit_instantiation (cp_parser* parser)
12951 int declares_class_or_enum;
12952 cp_decl_specifier_seq decl_specifiers;
12953 tree extension_specifier = NULL_TREE;
12955 timevar_push (TV_TEMPLATE_INST);
12957 /* Look for an (optional) storage-class-specifier or
12958 function-specifier. */
12959 if (cp_parser_allow_gnu_extensions_p (parser))
12961 extension_specifier
12962 = cp_parser_storage_class_specifier_opt (parser);
12963 if (!extension_specifier)
12964 extension_specifier
12965 = cp_parser_function_specifier_opt (parser,
12966 /*decl_specs=*/NULL);
12969 /* Look for the `template' keyword. */
12970 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12971 /* Let the front end know that we are processing an explicit
12973 begin_explicit_instantiation ();
12974 /* [temp.explicit] says that we are supposed to ignore access
12975 control while processing explicit instantiation directives. */
12976 push_deferring_access_checks (dk_no_check);
12977 /* Parse a decl-specifier-seq. */
12978 cp_parser_decl_specifier_seq (parser,
12979 CP_PARSER_FLAGS_OPTIONAL,
12981 &declares_class_or_enum);
12982 /* If there was exactly one decl-specifier, and it declared a class,
12983 and there's no declarator, then we have an explicit type
12985 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12989 type = check_tag_decl (&decl_specifiers);
12990 /* Turn access control back on for names used during
12991 template instantiation. */
12992 pop_deferring_access_checks ();
12994 do_type_instantiation (type, extension_specifier,
12995 /*complain=*/tf_error);
12999 cp_declarator *declarator;
13002 /* Parse the declarator. */
13004 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13005 /*ctor_dtor_or_conv_p=*/NULL,
13006 /*parenthesized_p=*/NULL,
13007 /*member_p=*/false);
13008 if (declares_class_or_enum & 2)
13009 cp_parser_check_for_definition_in_return_type (declarator,
13010 decl_specifiers.type,
13011 decl_specifiers.type_location);
13012 if (declarator != cp_error_declarator)
13014 if (decl_specifiers.specs[(int)ds_inline])
13015 permerror (input_location, "explicit instantiation shall not use"
13016 " %<inline%> specifier");
13017 if (decl_specifiers.specs[(int)ds_constexpr])
13018 permerror (input_location, "explicit instantiation shall not use"
13019 " %<constexpr%> specifier");
13021 decl = grokdeclarator (declarator, &decl_specifiers,
13022 NORMAL, 0, &decl_specifiers.attributes);
13023 /* Turn access control back on for names used during
13024 template instantiation. */
13025 pop_deferring_access_checks ();
13026 /* Do the explicit instantiation. */
13027 do_decl_instantiation (decl, extension_specifier);
13031 pop_deferring_access_checks ();
13032 /* Skip the body of the explicit instantiation. */
13033 cp_parser_skip_to_end_of_statement (parser);
13036 /* We're done with the instantiation. */
13037 end_explicit_instantiation ();
13039 cp_parser_consume_semicolon_at_end_of_statement (parser);
13041 timevar_pop (TV_TEMPLATE_INST);
13044 /* Parse an explicit-specialization.
13046 explicit-specialization:
13047 template < > declaration
13049 Although the standard says `declaration', what it really means is:
13051 explicit-specialization:
13052 template <> decl-specifier [opt] init-declarator [opt] ;
13053 template <> function-definition
13054 template <> explicit-specialization
13055 template <> template-declaration */
13058 cp_parser_explicit_specialization (cp_parser* parser)
13060 bool need_lang_pop;
13061 cp_token *token = cp_lexer_peek_token (parser->lexer);
13063 /* Look for the `template' keyword. */
13064 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13065 /* Look for the `<'. */
13066 cp_parser_require (parser, CPP_LESS, RT_LESS);
13067 /* Look for the `>'. */
13068 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13069 /* We have processed another parameter list. */
13070 ++parser->num_template_parameter_lists;
13073 A template ... explicit specialization ... shall not have C
13075 if (current_lang_name == lang_name_c)
13077 error_at (token->location, "template specialization with C linkage");
13078 /* Give it C++ linkage to avoid confusing other parts of the
13080 push_lang_context (lang_name_cplusplus);
13081 need_lang_pop = true;
13084 need_lang_pop = false;
13085 /* Let the front end know that we are beginning a specialization. */
13086 if (!begin_specialization ())
13088 end_specialization ();
13092 /* If the next keyword is `template', we need to figure out whether
13093 or not we're looking a template-declaration. */
13094 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13096 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13097 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13098 cp_parser_template_declaration_after_export (parser,
13099 /*member_p=*/false);
13101 cp_parser_explicit_specialization (parser);
13104 /* Parse the dependent declaration. */
13105 cp_parser_single_declaration (parser,
13107 /*member_p=*/false,
13108 /*explicit_specialization_p=*/true,
13109 /*friend_p=*/NULL);
13110 /* We're done with the specialization. */
13111 end_specialization ();
13112 /* For the erroneous case of a template with C linkage, we pushed an
13113 implicit C++ linkage scope; exit that scope now. */
13115 pop_lang_context ();
13116 /* We're done with this parameter list. */
13117 --parser->num_template_parameter_lists;
13120 /* Parse a type-specifier.
13123 simple-type-specifier
13126 elaborated-type-specifier
13134 Returns a representation of the type-specifier. For a
13135 class-specifier, enum-specifier, or elaborated-type-specifier, a
13136 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13138 The parser flags FLAGS is used to control type-specifier parsing.
13140 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13141 in a decl-specifier-seq.
13143 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13144 class-specifier, enum-specifier, or elaborated-type-specifier, then
13145 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13146 if a type is declared; 2 if it is defined. Otherwise, it is set to
13149 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13150 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13151 is set to FALSE. */
13154 cp_parser_type_specifier (cp_parser* parser,
13155 cp_parser_flags flags,
13156 cp_decl_specifier_seq *decl_specs,
13157 bool is_declaration,
13158 int* declares_class_or_enum,
13159 bool* is_cv_qualifier)
13161 tree type_spec = NULL_TREE;
13164 cp_decl_spec ds = ds_last;
13166 /* Assume this type-specifier does not declare a new type. */
13167 if (declares_class_or_enum)
13168 *declares_class_or_enum = 0;
13169 /* And that it does not specify a cv-qualifier. */
13170 if (is_cv_qualifier)
13171 *is_cv_qualifier = false;
13172 /* Peek at the next token. */
13173 token = cp_lexer_peek_token (parser->lexer);
13175 /* If we're looking at a keyword, we can use that to guide the
13176 production we choose. */
13177 keyword = token->keyword;
13181 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13182 goto elaborated_type_specifier;
13184 /* Look for the enum-specifier. */
13185 type_spec = cp_parser_enum_specifier (parser);
13186 /* If that worked, we're done. */
13189 if (declares_class_or_enum)
13190 *declares_class_or_enum = 2;
13192 cp_parser_set_decl_spec_type (decl_specs,
13195 /*type_definition_p=*/true);
13199 goto elaborated_type_specifier;
13201 /* Any of these indicate either a class-specifier, or an
13202 elaborated-type-specifier. */
13206 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13207 goto elaborated_type_specifier;
13209 /* Parse tentatively so that we can back up if we don't find a
13210 class-specifier. */
13211 cp_parser_parse_tentatively (parser);
13212 /* Look for the class-specifier. */
13213 type_spec = cp_parser_class_specifier (parser);
13214 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13215 /* If that worked, we're done. */
13216 if (cp_parser_parse_definitely (parser))
13218 if (declares_class_or_enum)
13219 *declares_class_or_enum = 2;
13221 cp_parser_set_decl_spec_type (decl_specs,
13224 /*type_definition_p=*/true);
13228 /* Fall through. */
13229 elaborated_type_specifier:
13230 /* We're declaring (not defining) a class or enum. */
13231 if (declares_class_or_enum)
13232 *declares_class_or_enum = 1;
13234 /* Fall through. */
13236 /* Look for an elaborated-type-specifier. */
13238 = (cp_parser_elaborated_type_specifier
13240 decl_specs && decl_specs->specs[(int) ds_friend],
13243 cp_parser_set_decl_spec_type (decl_specs,
13246 /*type_definition_p=*/false);
13251 if (is_cv_qualifier)
13252 *is_cv_qualifier = true;
13257 if (is_cv_qualifier)
13258 *is_cv_qualifier = true;
13263 if (is_cv_qualifier)
13264 *is_cv_qualifier = true;
13268 /* The `__complex__' keyword is a GNU extension. */
13276 /* Handle simple keywords. */
13281 ++decl_specs->specs[(int)ds];
13282 decl_specs->any_specifiers_p = true;
13284 return cp_lexer_consume_token (parser->lexer)->u.value;
13287 /* If we do not already have a type-specifier, assume we are looking
13288 at a simple-type-specifier. */
13289 type_spec = cp_parser_simple_type_specifier (parser,
13293 /* If we didn't find a type-specifier, and a type-specifier was not
13294 optional in this context, issue an error message. */
13295 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13297 cp_parser_error (parser, "expected type specifier");
13298 return error_mark_node;
13304 /* Parse a simple-type-specifier.
13306 simple-type-specifier:
13307 :: [opt] nested-name-specifier [opt] type-name
13308 :: [opt] nested-name-specifier template template-id
13323 simple-type-specifier:
13325 decltype ( expression )
13328 __underlying_type ( type-id )
13332 simple-type-specifier:
13334 __typeof__ unary-expression
13335 __typeof__ ( type-id )
13337 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13338 appropriately updated. */
13341 cp_parser_simple_type_specifier (cp_parser* parser,
13342 cp_decl_specifier_seq *decl_specs,
13343 cp_parser_flags flags)
13345 tree type = NULL_TREE;
13348 /* Peek at the next token. */
13349 token = cp_lexer_peek_token (parser->lexer);
13351 /* If we're looking at a keyword, things are easy. */
13352 switch (token->keyword)
13356 decl_specs->explicit_char_p = true;
13357 type = char_type_node;
13360 type = char16_type_node;
13363 type = char32_type_node;
13366 type = wchar_type_node;
13369 type = boolean_type_node;
13373 ++decl_specs->specs[(int) ds_short];
13374 type = short_integer_type_node;
13378 decl_specs->explicit_int_p = true;
13379 type = integer_type_node;
13382 if (!int128_integer_type_node)
13385 decl_specs->explicit_int128_p = true;
13386 type = int128_integer_type_node;
13390 ++decl_specs->specs[(int) ds_long];
13391 type = long_integer_type_node;
13395 ++decl_specs->specs[(int) ds_signed];
13396 type = integer_type_node;
13400 ++decl_specs->specs[(int) ds_unsigned];
13401 type = unsigned_type_node;
13404 type = float_type_node;
13407 type = double_type_node;
13410 type = void_type_node;
13414 maybe_warn_cpp0x (CPP0X_AUTO);
13415 type = make_auto ();
13419 /* Since DR 743, decltype can either be a simple-type-specifier by
13420 itself or begin a nested-name-specifier. Parsing it will replace
13421 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13422 handling below decide what to do. */
13423 cp_parser_decltype (parser);
13424 cp_lexer_set_token_position (parser->lexer, token);
13428 /* Consume the `typeof' token. */
13429 cp_lexer_consume_token (parser->lexer);
13430 /* Parse the operand to `typeof'. */
13431 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13432 /* If it is not already a TYPE, take its type. */
13433 if (!TYPE_P (type))
13434 type = finish_typeof (type);
13437 cp_parser_set_decl_spec_type (decl_specs, type,
13439 /*type_definition_p=*/false);
13443 case RID_UNDERLYING_TYPE:
13444 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13446 cp_parser_set_decl_spec_type (decl_specs, type,
13448 /*type_definition_p=*/false);
13453 case RID_DIRECT_BASES:
13454 type = cp_parser_trait_expr (parser, token->keyword);
13456 cp_parser_set_decl_spec_type (decl_specs, type,
13458 /*type_definition_p=*/false);
13464 /* If token is an already-parsed decltype not followed by ::,
13465 it's a simple-type-specifier. */
13466 if (token->type == CPP_DECLTYPE
13467 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13469 type = token->u.value;
13471 cp_parser_set_decl_spec_type (decl_specs, type,
13473 /*type_definition_p=*/false);
13474 cp_lexer_consume_token (parser->lexer);
13478 /* If the type-specifier was for a built-in type, we're done. */
13481 /* Record the type. */
13483 && (token->keyword != RID_SIGNED
13484 && token->keyword != RID_UNSIGNED
13485 && token->keyword != RID_SHORT
13486 && token->keyword != RID_LONG))
13487 cp_parser_set_decl_spec_type (decl_specs,
13490 /*type_definition_p=*/false);
13492 decl_specs->any_specifiers_p = true;
13494 /* Consume the token. */
13495 cp_lexer_consume_token (parser->lexer);
13497 /* There is no valid C++ program where a non-template type is
13498 followed by a "<". That usually indicates that the user thought
13499 that the type was a template. */
13500 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13502 return TYPE_NAME (type);
13505 /* The type-specifier must be a user-defined type. */
13506 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13511 /* Don't gobble tokens or issue error messages if this is an
13512 optional type-specifier. */
13513 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13514 cp_parser_parse_tentatively (parser);
13516 /* Look for the optional `::' operator. */
13518 = (cp_parser_global_scope_opt (parser,
13519 /*current_scope_valid_p=*/false)
13521 /* Look for the nested-name specifier. */
13523 = (cp_parser_nested_name_specifier_opt (parser,
13524 /*typename_keyword_p=*/false,
13525 /*check_dependency_p=*/true,
13527 /*is_declaration=*/false)
13529 token = cp_lexer_peek_token (parser->lexer);
13530 /* If we have seen a nested-name-specifier, and the next token
13531 is `template', then we are using the template-id production. */
13533 && cp_parser_optional_template_keyword (parser))
13535 /* Look for the template-id. */
13536 type = cp_parser_template_id (parser,
13537 /*template_keyword_p=*/true,
13538 /*check_dependency_p=*/true,
13539 /*is_declaration=*/false);
13540 /* If the template-id did not name a type, we are out of
13542 if (TREE_CODE (type) != TYPE_DECL)
13544 cp_parser_error (parser, "expected template-id for type");
13548 /* Otherwise, look for a type-name. */
13550 type = cp_parser_type_name (parser);
13551 /* Keep track of all name-lookups performed in class scopes. */
13555 && TREE_CODE (type) == TYPE_DECL
13556 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13557 maybe_note_name_used_in_class (DECL_NAME (type), type);
13558 /* If it didn't work out, we don't have a TYPE. */
13559 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13560 && !cp_parser_parse_definitely (parser))
13562 if (type && decl_specs)
13563 cp_parser_set_decl_spec_type (decl_specs, type,
13565 /*type_definition_p=*/false);
13568 /* If we didn't get a type-name, issue an error message. */
13569 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13571 cp_parser_error (parser, "expected type-name");
13572 return error_mark_node;
13575 if (type && type != error_mark_node)
13577 /* See if TYPE is an Objective-C type, and if so, parse and
13578 accept any protocol references following it. Do this before
13579 the cp_parser_check_for_invalid_template_id() call, because
13580 Objective-C types can be followed by '<...>' which would
13581 enclose protocol names rather than template arguments, and so
13582 everything is fine. */
13583 if (c_dialect_objc () && !parser->scope
13584 && (objc_is_id (type) || objc_is_class_name (type)))
13586 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13587 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13589 /* Clobber the "unqualified" type previously entered into
13590 DECL_SPECS with the new, improved protocol-qualified version. */
13592 decl_specs->type = qual_type;
13597 /* There is no valid C++ program where a non-template type is
13598 followed by a "<". That usually indicates that the user
13599 thought that the type was a template. */
13600 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13607 /* Parse a type-name.
13620 Returns a TYPE_DECL for the type. */
13623 cp_parser_type_name (cp_parser* parser)
13627 /* We can't know yet whether it is a class-name or not. */
13628 cp_parser_parse_tentatively (parser);
13629 /* Try a class-name. */
13630 type_decl = cp_parser_class_name (parser,
13631 /*typename_keyword_p=*/false,
13632 /*template_keyword_p=*/false,
13634 /*check_dependency_p=*/true,
13635 /*class_head_p=*/false,
13636 /*is_declaration=*/false);
13637 /* If it's not a class-name, keep looking. */
13638 if (!cp_parser_parse_definitely (parser))
13640 /* It must be a typedef-name or an enum-name. */
13641 return cp_parser_nonclass_name (parser);
13647 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13655 Returns a TYPE_DECL for the type. */
13658 cp_parser_nonclass_name (cp_parser* parser)
13663 cp_token *token = cp_lexer_peek_token (parser->lexer);
13664 identifier = cp_parser_identifier (parser);
13665 if (identifier == error_mark_node)
13666 return error_mark_node;
13668 /* Look up the type-name. */
13669 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13671 if (TREE_CODE (type_decl) != TYPE_DECL
13672 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13674 /* See if this is an Objective-C type. */
13675 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13676 tree type = objc_get_protocol_qualified_type (identifier, protos);
13678 type_decl = TYPE_NAME (type);
13681 /* Issue an error if we did not find a type-name. */
13682 if (TREE_CODE (type_decl) != TYPE_DECL
13683 /* In Objective-C, we have the complication that class names are
13684 normally type names and start declarations (eg, the
13685 "NSObject" in "NSObject *object;"), but can be used in an
13686 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13687 is an expression. So, a classname followed by a dot is not a
13688 valid type-name. */
13689 || (objc_is_class_name (TREE_TYPE (type_decl))
13690 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13692 if (!cp_parser_simulate_error (parser))
13693 cp_parser_name_lookup_error (parser, identifier, type_decl,
13694 NLE_TYPE, token->location);
13695 return error_mark_node;
13697 /* Remember that the name was used in the definition of the
13698 current class so that we can check later to see if the
13699 meaning would have been different after the class was
13700 entirely defined. */
13701 else if (type_decl != error_mark_node
13703 maybe_note_name_used_in_class (identifier, type_decl);
13708 /* Parse an elaborated-type-specifier. Note that the grammar given
13709 here incorporates the resolution to DR68.
13711 elaborated-type-specifier:
13712 class-key :: [opt] nested-name-specifier [opt] identifier
13713 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13714 enum-key :: [opt] nested-name-specifier [opt] identifier
13715 typename :: [opt] nested-name-specifier identifier
13716 typename :: [opt] nested-name-specifier template [opt]
13721 elaborated-type-specifier:
13722 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13723 class-key attributes :: [opt] nested-name-specifier [opt]
13724 template [opt] template-id
13725 enum attributes :: [opt] nested-name-specifier [opt] identifier
13727 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13728 declared `friend'. If IS_DECLARATION is TRUE, then this
13729 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13730 something is being declared.
13732 Returns the TYPE specified. */
13735 cp_parser_elaborated_type_specifier (cp_parser* parser,
13737 bool is_declaration)
13739 enum tag_types tag_type;
13741 tree type = NULL_TREE;
13742 tree attributes = NULL_TREE;
13744 cp_token *token = NULL;
13746 /* See if we're looking at the `enum' keyword. */
13747 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13749 /* Consume the `enum' token. */
13750 cp_lexer_consume_token (parser->lexer);
13751 /* Remember that it's an enumeration type. */
13752 tag_type = enum_type;
13753 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13754 enums) is used here. */
13755 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13756 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13758 pedwarn (input_location, 0, "elaborated-type-specifier "
13759 "for a scoped enum must not use the %<%D%> keyword",
13760 cp_lexer_peek_token (parser->lexer)->u.value);
13761 /* Consume the `struct' or `class' and parse it anyway. */
13762 cp_lexer_consume_token (parser->lexer);
13764 /* Parse the attributes. */
13765 attributes = cp_parser_attributes_opt (parser);
13767 /* Or, it might be `typename'. */
13768 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13771 /* Consume the `typename' token. */
13772 cp_lexer_consume_token (parser->lexer);
13773 /* Remember that it's a `typename' type. */
13774 tag_type = typename_type;
13776 /* Otherwise it must be a class-key. */
13779 tag_type = cp_parser_class_key (parser);
13780 if (tag_type == none_type)
13781 return error_mark_node;
13782 /* Parse the attributes. */
13783 attributes = cp_parser_attributes_opt (parser);
13786 /* Look for the `::' operator. */
13787 globalscope = cp_parser_global_scope_opt (parser,
13788 /*current_scope_valid_p=*/false);
13789 /* Look for the nested-name-specifier. */
13790 if (tag_type == typename_type && !globalscope)
13792 if (!cp_parser_nested_name_specifier (parser,
13793 /*typename_keyword_p=*/true,
13794 /*check_dependency_p=*/true,
13797 return error_mark_node;
13800 /* Even though `typename' is not present, the proposed resolution
13801 to Core Issue 180 says that in `class A<T>::B', `B' should be
13802 considered a type-name, even if `A<T>' is dependent. */
13803 cp_parser_nested_name_specifier_opt (parser,
13804 /*typename_keyword_p=*/true,
13805 /*check_dependency_p=*/true,
13808 /* For everything but enumeration types, consider a template-id.
13809 For an enumeration type, consider only a plain identifier. */
13810 if (tag_type != enum_type)
13812 bool template_p = false;
13815 /* Allow the `template' keyword. */
13816 template_p = cp_parser_optional_template_keyword (parser);
13817 /* If we didn't see `template', we don't know if there's a
13818 template-id or not. */
13820 cp_parser_parse_tentatively (parser);
13821 /* Parse the template-id. */
13822 token = cp_lexer_peek_token (parser->lexer);
13823 decl = cp_parser_template_id (parser, template_p,
13824 /*check_dependency_p=*/true,
13826 /* If we didn't find a template-id, look for an ordinary
13828 if (!template_p && !cp_parser_parse_definitely (parser))
13830 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13831 in effect, then we must assume that, upon instantiation, the
13832 template will correspond to a class. */
13833 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13834 && tag_type == typename_type)
13835 type = make_typename_type (parser->scope, decl,
13837 /*complain=*/tf_error);
13838 /* If the `typename' keyword is in effect and DECL is not a type
13839 decl. Then type is non existant. */
13840 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13843 type = TREE_TYPE (decl);
13848 token = cp_lexer_peek_token (parser->lexer);
13849 identifier = cp_parser_identifier (parser);
13851 if (identifier == error_mark_node)
13853 parser->scope = NULL_TREE;
13854 return error_mark_node;
13857 /* For a `typename', we needn't call xref_tag. */
13858 if (tag_type == typename_type
13859 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13860 return cp_parser_make_typename_type (parser, parser->scope,
13863 /* Look up a qualified name in the usual way. */
13867 tree ambiguous_decls;
13869 decl = cp_parser_lookup_name (parser, identifier,
13871 /*is_template=*/false,
13872 /*is_namespace=*/false,
13873 /*check_dependency=*/true,
13877 /* If the lookup was ambiguous, an error will already have been
13879 if (ambiguous_decls)
13880 return error_mark_node;
13882 /* If we are parsing friend declaration, DECL may be a
13883 TEMPLATE_DECL tree node here. However, we need to check
13884 whether this TEMPLATE_DECL results in valid code. Consider
13885 the following example:
13888 template <class T> class C {};
13891 template <class T> friend class N::C; // #1, valid code
13893 template <class T> class Y {
13894 friend class N::C; // #2, invalid code
13897 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13898 name lookup of `N::C'. We see that friend declaration must
13899 be template for the code to be valid. Note that
13900 processing_template_decl does not work here since it is
13901 always 1 for the above two cases. */
13903 decl = (cp_parser_maybe_treat_template_as_class
13904 (decl, /*tag_name_p=*/is_friend
13905 && parser->num_template_parameter_lists));
13907 if (TREE_CODE (decl) != TYPE_DECL)
13909 cp_parser_diagnose_invalid_type_name (parser,
13913 return error_mark_node;
13916 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13918 bool allow_template = (parser->num_template_parameter_lists
13919 || DECL_SELF_REFERENCE_P (decl));
13920 type = check_elaborated_type_specifier (tag_type, decl,
13923 if (type == error_mark_node)
13924 return error_mark_node;
13927 /* Forward declarations of nested types, such as
13932 are invalid unless all components preceding the final '::'
13933 are complete. If all enclosing types are complete, these
13934 declarations become merely pointless.
13936 Invalid forward declarations of nested types are errors
13937 caught elsewhere in parsing. Those that are pointless arrive
13940 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13941 && !is_friend && !processing_explicit_instantiation)
13942 warning (0, "declaration %qD does not declare anything", decl);
13944 type = TREE_TYPE (decl);
13948 /* An elaborated-type-specifier sometimes introduces a new type and
13949 sometimes names an existing type. Normally, the rule is that it
13950 introduces a new type only if there is not an existing type of
13951 the same name already in scope. For example, given:
13954 void f() { struct S s; }
13956 the `struct S' in the body of `f' is the same `struct S' as in
13957 the global scope; the existing definition is used. However, if
13958 there were no global declaration, this would introduce a new
13959 local class named `S'.
13961 An exception to this rule applies to the following code:
13963 namespace N { struct S; }
13965 Here, the elaborated-type-specifier names a new type
13966 unconditionally; even if there is already an `S' in the
13967 containing scope this declaration names a new type.
13968 This exception only applies if the elaborated-type-specifier
13969 forms the complete declaration:
13973 A declaration consisting solely of `class-key identifier ;' is
13974 either a redeclaration of the name in the current scope or a
13975 forward declaration of the identifier as a class name. It
13976 introduces the name into the current scope.
13978 We are in this situation precisely when the next token is a `;'.
13980 An exception to the exception is that a `friend' declaration does
13981 *not* name a new type; i.e., given:
13983 struct S { friend struct T; };
13985 `T' is not a new type in the scope of `S'.
13987 Also, `new struct S' or `sizeof (struct S)' never results in the
13988 definition of a new type; a new type can only be declared in a
13989 declaration context. */
13995 /* Friends have special name lookup rules. */
13996 ts = ts_within_enclosing_non_class;
13997 else if (is_declaration
13998 && cp_lexer_next_token_is (parser->lexer,
14000 /* This is a `class-key identifier ;' */
14006 (parser->num_template_parameter_lists
14007 && (cp_parser_next_token_starts_class_definition_p (parser)
14008 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14009 /* An unqualified name was used to reference this type, so
14010 there were no qualifying templates. */
14011 if (!cp_parser_check_template_parameters (parser,
14012 /*num_templates=*/0,
14014 /*declarator=*/NULL))
14015 return error_mark_node;
14016 type = xref_tag (tag_type, identifier, ts, template_p);
14020 if (type == error_mark_node)
14021 return error_mark_node;
14023 /* Allow attributes on forward declarations of classes. */
14026 if (TREE_CODE (type) == TYPENAME_TYPE)
14027 warning (OPT_Wattributes,
14028 "attributes ignored on uninstantiated type");
14029 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14030 && ! processing_explicit_instantiation)
14031 warning (OPT_Wattributes,
14032 "attributes ignored on template instantiation");
14033 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14034 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14036 warning (OPT_Wattributes,
14037 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14040 if (tag_type != enum_type)
14042 /* Indicate whether this class was declared as a `class' or as a
14044 if (TREE_CODE (type) == RECORD_TYPE)
14045 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14046 cp_parser_check_class_key (tag_type, type);
14049 /* A "<" cannot follow an elaborated type specifier. If that
14050 happens, the user was probably trying to form a template-id. */
14051 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14056 /* Parse an enum-specifier.
14059 enum-head { enumerator-list [opt] }
14062 enum-key identifier [opt] enum-base [opt]
14063 enum-key nested-name-specifier identifier enum-base [opt]
14068 enum struct [C++0x]
14071 : type-specifier-seq
14073 opaque-enum-specifier:
14074 enum-key identifier enum-base [opt] ;
14077 enum-key attributes[opt] identifier [opt] enum-base [opt]
14078 { enumerator-list [opt] }attributes[opt]
14080 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14081 if the token stream isn't an enum-specifier after all. */
14084 cp_parser_enum_specifier (cp_parser* parser)
14087 tree type = NULL_TREE;
14089 tree nested_name_specifier = NULL_TREE;
14091 bool scoped_enum_p = false;
14092 bool has_underlying_type = false;
14093 bool nested_being_defined = false;
14094 bool new_value_list = false;
14095 bool is_new_type = false;
14096 bool is_anonymous = false;
14097 tree underlying_type = NULL_TREE;
14098 cp_token *type_start_token = NULL;
14099 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14101 parser->colon_corrects_to_scope_p = false;
14103 /* Parse tentatively so that we can back up if we don't find a
14105 cp_parser_parse_tentatively (parser);
14107 /* Caller guarantees that the current token is 'enum', an identifier
14108 possibly follows, and the token after that is an opening brace.
14109 If we don't have an identifier, fabricate an anonymous name for
14110 the enumeration being defined. */
14111 cp_lexer_consume_token (parser->lexer);
14113 /* Parse the "class" or "struct", which indicates a scoped
14114 enumeration type in C++0x. */
14115 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14116 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14118 if (cxx_dialect < cxx0x)
14119 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14121 /* Consume the `struct' or `class' token. */
14122 cp_lexer_consume_token (parser->lexer);
14124 scoped_enum_p = true;
14127 attributes = cp_parser_attributes_opt (parser);
14129 /* Clear the qualification. */
14130 parser->scope = NULL_TREE;
14131 parser->qualifying_scope = NULL_TREE;
14132 parser->object_scope = NULL_TREE;
14134 /* Figure out in what scope the declaration is being placed. */
14135 prev_scope = current_scope ();
14137 type_start_token = cp_lexer_peek_token (parser->lexer);
14139 push_deferring_access_checks (dk_no_check);
14140 nested_name_specifier
14141 = cp_parser_nested_name_specifier_opt (parser,
14142 /*typename_keyword_p=*/true,
14143 /*check_dependency_p=*/false,
14145 /*is_declaration=*/false);
14147 if (nested_name_specifier)
14151 identifier = cp_parser_identifier (parser);
14152 name = cp_parser_lookup_name (parser, identifier,
14154 /*is_template=*/false,
14155 /*is_namespace=*/false,
14156 /*check_dependency=*/true,
14157 /*ambiguous_decls=*/NULL,
14161 type = TREE_TYPE (name);
14162 if (TREE_CODE (type) == TYPENAME_TYPE)
14164 /* Are template enums allowed in ISO? */
14165 if (template_parm_scope_p ())
14166 pedwarn (type_start_token->location, OPT_pedantic,
14167 "%qD is an enumeration template", name);
14168 /* ignore a typename reference, for it will be solved by name
14174 error_at (type_start_token->location,
14175 "%qD is not an enumerator-name", identifier);
14179 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14180 identifier = cp_parser_identifier (parser);
14183 identifier = make_anon_name ();
14184 is_anonymous = true;
14187 pop_deferring_access_checks ();
14189 /* Check for the `:' that denotes a specified underlying type in C++0x.
14190 Note that a ':' could also indicate a bitfield width, however. */
14191 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14193 cp_decl_specifier_seq type_specifiers;
14195 /* Consume the `:'. */
14196 cp_lexer_consume_token (parser->lexer);
14198 /* Parse the type-specifier-seq. */
14199 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14200 /*is_trailing_return=*/false,
14203 /* At this point this is surely not elaborated type specifier. */
14204 if (!cp_parser_parse_definitely (parser))
14207 if (cxx_dialect < cxx0x)
14208 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14210 has_underlying_type = true;
14212 /* If that didn't work, stop. */
14213 if (type_specifiers.type != error_mark_node)
14215 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14216 /*initialized=*/0, NULL);
14217 if (underlying_type == error_mark_node)
14218 underlying_type = NULL_TREE;
14222 /* Look for the `{' but don't consume it yet. */
14223 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14225 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14227 cp_parser_error (parser, "expected %<{%>");
14228 if (has_underlying_type)
14234 /* An opaque-enum-specifier must have a ';' here. */
14235 if ((scoped_enum_p || underlying_type)
14236 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14238 cp_parser_error (parser, "expected %<;%> or %<{%>");
14239 if (has_underlying_type)
14247 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14250 if (nested_name_specifier)
14252 if (CLASS_TYPE_P (nested_name_specifier))
14254 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14255 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14256 push_scope (nested_name_specifier);
14258 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14260 push_nested_namespace (nested_name_specifier);
14264 /* Issue an error message if type-definitions are forbidden here. */
14265 if (!cp_parser_check_type_definition (parser))
14266 type = error_mark_node;
14268 /* Create the new type. We do this before consuming the opening
14269 brace so the enum will be recorded as being on the line of its
14270 tag (or the 'enum' keyword, if there is no tag). */
14271 type = start_enum (identifier, type, underlying_type,
14272 scoped_enum_p, &is_new_type);
14274 /* If the next token is not '{' it is an opaque-enum-specifier or an
14275 elaborated-type-specifier. */
14276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14278 timevar_push (TV_PARSE_ENUM);
14279 if (nested_name_specifier)
14281 /* The following catches invalid code such as:
14282 enum class S<int>::E { A, B, C }; */
14283 if (!processing_specialization
14284 && CLASS_TYPE_P (nested_name_specifier)
14285 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14286 error_at (type_start_token->location, "cannot add an enumerator "
14287 "list to a template instantiation");
14289 /* If that scope does not contain the scope in which the
14290 class was originally declared, the program is invalid. */
14291 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14293 if (at_namespace_scope_p ())
14294 error_at (type_start_token->location,
14295 "declaration of %qD in namespace %qD which does not "
14297 type, prev_scope, nested_name_specifier);
14299 error_at (type_start_token->location,
14300 "declaration of %qD in %qD which does not enclose %qD",
14301 type, prev_scope, nested_name_specifier);
14302 type = error_mark_node;
14307 begin_scope (sk_scoped_enum, type);
14309 /* Consume the opening brace. */
14310 cp_lexer_consume_token (parser->lexer);
14312 if (type == error_mark_node)
14313 ; /* Nothing to add */
14314 else if (OPAQUE_ENUM_P (type)
14315 || (cxx_dialect > cxx98 && processing_specialization))
14317 new_value_list = true;
14318 SET_OPAQUE_ENUM_P (type, false);
14319 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14323 error_at (type_start_token->location, "multiple definition of %q#T", type);
14324 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14325 "previous definition here");
14326 type = error_mark_node;
14329 if (type == error_mark_node)
14330 cp_parser_skip_to_end_of_block_or_statement (parser);
14331 /* If the next token is not '}', then there are some enumerators. */
14332 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14333 cp_parser_enumerator_list (parser, type);
14335 /* Consume the final '}'. */
14336 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14340 timevar_pop (TV_PARSE_ENUM);
14344 /* If a ';' follows, then it is an opaque-enum-specifier
14345 and additional restrictions apply. */
14346 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14349 error_at (type_start_token->location,
14350 "opaque-enum-specifier without name");
14351 else if (nested_name_specifier)
14352 error_at (type_start_token->location,
14353 "opaque-enum-specifier must use a simple identifier");
14357 /* Look for trailing attributes to apply to this enumeration, and
14358 apply them if appropriate. */
14359 if (cp_parser_allow_gnu_extensions_p (parser))
14361 tree trailing_attr = cp_parser_attributes_opt (parser);
14362 trailing_attr = chainon (trailing_attr, attributes);
14363 cplus_decl_attributes (&type,
14365 (int) ATTR_FLAG_TYPE_IN_PLACE);
14368 /* Finish up the enumeration. */
14369 if (type != error_mark_node)
14371 if (new_value_list)
14372 finish_enum_value_list (type);
14374 finish_enum (type);
14377 if (nested_name_specifier)
14379 if (CLASS_TYPE_P (nested_name_specifier))
14381 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14382 pop_scope (nested_name_specifier);
14384 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14386 pop_nested_namespace (nested_name_specifier);
14390 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14394 /* Parse an enumerator-list. The enumerators all have the indicated
14398 enumerator-definition
14399 enumerator-list , enumerator-definition */
14402 cp_parser_enumerator_list (cp_parser* parser, tree type)
14406 /* Parse an enumerator-definition. */
14407 cp_parser_enumerator_definition (parser, type);
14409 /* If the next token is not a ',', we've reached the end of
14411 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14413 /* Otherwise, consume the `,' and keep going. */
14414 cp_lexer_consume_token (parser->lexer);
14415 /* If the next token is a `}', there is a trailing comma. */
14416 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14418 if (!in_system_header)
14419 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
14425 /* Parse an enumerator-definition. The enumerator has the indicated
14428 enumerator-definition:
14430 enumerator = constant-expression
14436 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14442 /* Save the input location because we are interested in the location
14443 of the identifier and not the location of the explicit value. */
14444 loc = cp_lexer_peek_token (parser->lexer)->location;
14446 /* Look for the identifier. */
14447 identifier = cp_parser_identifier (parser);
14448 if (identifier == error_mark_node)
14451 /* If the next token is an '=', then there is an explicit value. */
14452 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14454 /* Consume the `=' token. */
14455 cp_lexer_consume_token (parser->lexer);
14456 /* Parse the value. */
14457 value = cp_parser_constant_expression (parser,
14458 /*allow_non_constant_p=*/false,
14464 /* If we are processing a template, make sure the initializer of the
14465 enumerator doesn't contain any bare template parameter pack. */
14466 if (check_for_bare_parameter_packs (value))
14467 value = error_mark_node;
14469 /* integral_constant_value will pull out this expression, so make sure
14470 it's folded as appropriate. */
14471 value = fold_non_dependent_expr (value);
14473 /* Create the enumerator. */
14474 build_enumerator (identifier, value, type, loc);
14477 /* Parse a namespace-name.
14480 original-namespace-name
14483 Returns the NAMESPACE_DECL for the namespace. */
14486 cp_parser_namespace_name (cp_parser* parser)
14489 tree namespace_decl;
14491 cp_token *token = cp_lexer_peek_token (parser->lexer);
14493 /* Get the name of the namespace. */
14494 identifier = cp_parser_identifier (parser);
14495 if (identifier == error_mark_node)
14496 return error_mark_node;
14498 /* Look up the identifier in the currently active scope. Look only
14499 for namespaces, due to:
14501 [basic.lookup.udir]
14503 When looking up a namespace-name in a using-directive or alias
14504 definition, only namespace names are considered.
14508 [basic.lookup.qual]
14510 During the lookup of a name preceding the :: scope resolution
14511 operator, object, function, and enumerator names are ignored.
14513 (Note that cp_parser_qualifying_entity only calls this
14514 function if the token after the name is the scope resolution
14516 namespace_decl = cp_parser_lookup_name (parser, identifier,
14518 /*is_template=*/false,
14519 /*is_namespace=*/true,
14520 /*check_dependency=*/true,
14521 /*ambiguous_decls=*/NULL,
14523 /* If it's not a namespace, issue an error. */
14524 if (namespace_decl == error_mark_node
14525 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14527 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14528 error_at (token->location, "%qD is not a namespace-name", identifier);
14529 cp_parser_error (parser, "expected namespace-name");
14530 namespace_decl = error_mark_node;
14533 return namespace_decl;
14536 /* Parse a namespace-definition.
14538 namespace-definition:
14539 named-namespace-definition
14540 unnamed-namespace-definition
14542 named-namespace-definition:
14543 original-namespace-definition
14544 extension-namespace-definition
14546 original-namespace-definition:
14547 namespace identifier { namespace-body }
14549 extension-namespace-definition:
14550 namespace original-namespace-name { namespace-body }
14552 unnamed-namespace-definition:
14553 namespace { namespace-body } */
14556 cp_parser_namespace_definition (cp_parser* parser)
14558 tree identifier, attribs;
14559 bool has_visibility;
14562 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14564 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14566 cp_lexer_consume_token (parser->lexer);
14571 /* Look for the `namespace' keyword. */
14572 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14574 /* Get the name of the namespace. We do not attempt to distinguish
14575 between an original-namespace-definition and an
14576 extension-namespace-definition at this point. The semantic
14577 analysis routines are responsible for that. */
14578 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14579 identifier = cp_parser_identifier (parser);
14581 identifier = NULL_TREE;
14583 /* Parse any specified attributes. */
14584 attribs = cp_parser_attributes_opt (parser);
14586 /* Look for the `{' to start the namespace. */
14587 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14588 /* Start the namespace. */
14589 push_namespace (identifier);
14591 /* "inline namespace" is equivalent to a stub namespace definition
14592 followed by a strong using directive. */
14595 tree name_space = current_namespace;
14596 /* Set up namespace association. */
14597 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14598 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14599 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14600 /* Import the contents of the inline namespace. */
14602 do_using_directive (name_space);
14603 push_namespace (identifier);
14606 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14608 /* Parse the body of the namespace. */
14609 cp_parser_namespace_body (parser);
14611 if (has_visibility)
14612 pop_visibility (1);
14614 /* Finish the namespace. */
14616 /* Look for the final `}'. */
14617 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14620 /* Parse a namespace-body.
14623 declaration-seq [opt] */
14626 cp_parser_namespace_body (cp_parser* parser)
14628 cp_parser_declaration_seq_opt (parser);
14631 /* Parse a namespace-alias-definition.
14633 namespace-alias-definition:
14634 namespace identifier = qualified-namespace-specifier ; */
14637 cp_parser_namespace_alias_definition (cp_parser* parser)
14640 tree namespace_specifier;
14642 cp_token *token = cp_lexer_peek_token (parser->lexer);
14644 /* Look for the `namespace' keyword. */
14645 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14646 /* Look for the identifier. */
14647 identifier = cp_parser_identifier (parser);
14648 if (identifier == error_mark_node)
14650 /* Look for the `=' token. */
14651 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14652 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14654 error_at (token->location, "%<namespace%> definition is not allowed here");
14655 /* Skip the definition. */
14656 cp_lexer_consume_token (parser->lexer);
14657 if (cp_parser_skip_to_closing_brace (parser))
14658 cp_lexer_consume_token (parser->lexer);
14661 cp_parser_require (parser, CPP_EQ, RT_EQ);
14662 /* Look for the qualified-namespace-specifier. */
14663 namespace_specifier
14664 = cp_parser_qualified_namespace_specifier (parser);
14665 /* Look for the `;' token. */
14666 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14668 /* Register the alias in the symbol table. */
14669 do_namespace_alias (identifier, namespace_specifier);
14672 /* Parse a qualified-namespace-specifier.
14674 qualified-namespace-specifier:
14675 :: [opt] nested-name-specifier [opt] namespace-name
14677 Returns a NAMESPACE_DECL corresponding to the specified
14681 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14683 /* Look for the optional `::'. */
14684 cp_parser_global_scope_opt (parser,
14685 /*current_scope_valid_p=*/false);
14687 /* Look for the optional nested-name-specifier. */
14688 cp_parser_nested_name_specifier_opt (parser,
14689 /*typename_keyword_p=*/false,
14690 /*check_dependency_p=*/true,
14692 /*is_declaration=*/true);
14694 return cp_parser_namespace_name (parser);
14697 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14698 access declaration.
14701 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14702 using :: unqualified-id ;
14704 access-declaration:
14710 cp_parser_using_declaration (cp_parser* parser,
14711 bool access_declaration_p)
14714 bool typename_p = false;
14715 bool global_scope_p;
14720 if (access_declaration_p)
14721 cp_parser_parse_tentatively (parser);
14724 /* Look for the `using' keyword. */
14725 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14727 /* Peek at the next token. */
14728 token = cp_lexer_peek_token (parser->lexer);
14729 /* See if it's `typename'. */
14730 if (token->keyword == RID_TYPENAME)
14732 /* Remember that we've seen it. */
14734 /* Consume the `typename' token. */
14735 cp_lexer_consume_token (parser->lexer);
14739 /* Look for the optional global scope qualification. */
14741 = (cp_parser_global_scope_opt (parser,
14742 /*current_scope_valid_p=*/false)
14745 /* If we saw `typename', or didn't see `::', then there must be a
14746 nested-name-specifier present. */
14747 if (typename_p || !global_scope_p)
14748 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14749 /*check_dependency_p=*/true,
14751 /*is_declaration=*/true);
14752 /* Otherwise, we could be in either of the two productions. In that
14753 case, treat the nested-name-specifier as optional. */
14755 qscope = cp_parser_nested_name_specifier_opt (parser,
14756 /*typename_keyword_p=*/false,
14757 /*check_dependency_p=*/true,
14759 /*is_declaration=*/true);
14761 qscope = global_namespace;
14763 if (access_declaration_p && cp_parser_error_occurred (parser))
14764 /* Something has already gone wrong; there's no need to parse
14765 further. Since an error has occurred, the return value of
14766 cp_parser_parse_definitely will be false, as required. */
14767 return cp_parser_parse_definitely (parser);
14769 token = cp_lexer_peek_token (parser->lexer);
14770 /* Parse the unqualified-id. */
14771 identifier = cp_parser_unqualified_id (parser,
14772 /*template_keyword_p=*/false,
14773 /*check_dependency_p=*/true,
14774 /*declarator_p=*/true,
14775 /*optional_p=*/false);
14777 if (access_declaration_p)
14779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14780 cp_parser_simulate_error (parser);
14781 if (!cp_parser_parse_definitely (parser))
14785 /* The function we call to handle a using-declaration is different
14786 depending on what scope we are in. */
14787 if (qscope == error_mark_node || identifier == error_mark_node)
14789 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14790 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14791 /* [namespace.udecl]
14793 A using declaration shall not name a template-id. */
14794 error_at (token->location,
14795 "a template-id may not appear in a using-declaration");
14798 if (at_class_scope_p ())
14800 /* Create the USING_DECL. */
14801 decl = do_class_using_decl (parser->scope, identifier);
14803 if (check_for_bare_parameter_packs (decl))
14806 /* Add it to the list of members in this class. */
14807 finish_member_declaration (decl);
14811 decl = cp_parser_lookup_name_simple (parser,
14814 if (decl == error_mark_node)
14815 cp_parser_name_lookup_error (parser, identifier,
14818 else if (check_for_bare_parameter_packs (decl))
14820 else if (!at_namespace_scope_p ())
14821 do_local_using_decl (decl, qscope, identifier);
14823 do_toplevel_using_decl (decl, qscope, identifier);
14827 /* Look for the final `;'. */
14828 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14833 /* Parse a using-directive.
14836 using namespace :: [opt] nested-name-specifier [opt]
14837 namespace-name ; */
14840 cp_parser_using_directive (cp_parser* parser)
14842 tree namespace_decl;
14845 /* Look for the `using' keyword. */
14846 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14847 /* And the `namespace' keyword. */
14848 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14849 /* Look for the optional `::' operator. */
14850 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14851 /* And the optional nested-name-specifier. */
14852 cp_parser_nested_name_specifier_opt (parser,
14853 /*typename_keyword_p=*/false,
14854 /*check_dependency_p=*/true,
14856 /*is_declaration=*/true);
14857 /* Get the namespace being used. */
14858 namespace_decl = cp_parser_namespace_name (parser);
14859 /* And any specified attributes. */
14860 attribs = cp_parser_attributes_opt (parser);
14861 /* Update the symbol table. */
14862 parse_using_directive (namespace_decl, attribs);
14863 /* Look for the final `;'. */
14864 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14867 /* Parse an asm-definition.
14870 asm ( string-literal ) ;
14875 asm volatile [opt] ( string-literal ) ;
14876 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14877 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14878 : asm-operand-list [opt] ) ;
14879 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14880 : asm-operand-list [opt]
14881 : asm-clobber-list [opt] ) ;
14882 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14883 : asm-clobber-list [opt]
14884 : asm-goto-list ) ; */
14887 cp_parser_asm_definition (cp_parser* parser)
14890 tree outputs = NULL_TREE;
14891 tree inputs = NULL_TREE;
14892 tree clobbers = NULL_TREE;
14893 tree labels = NULL_TREE;
14895 bool volatile_p = false;
14896 bool extended_p = false;
14897 bool invalid_inputs_p = false;
14898 bool invalid_outputs_p = false;
14899 bool goto_p = false;
14900 required_token missing = RT_NONE;
14902 /* Look for the `asm' keyword. */
14903 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14904 /* See if the next token is `volatile'. */
14905 if (cp_parser_allow_gnu_extensions_p (parser)
14906 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14908 /* Remember that we saw the `volatile' keyword. */
14910 /* Consume the token. */
14911 cp_lexer_consume_token (parser->lexer);
14913 if (cp_parser_allow_gnu_extensions_p (parser)
14914 && parser->in_function_body
14915 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14917 /* Remember that we saw the `goto' keyword. */
14919 /* Consume the token. */
14920 cp_lexer_consume_token (parser->lexer);
14922 /* Look for the opening `('. */
14923 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14925 /* Look for the string. */
14926 string = cp_parser_string_literal (parser, false, false);
14927 if (string == error_mark_node)
14929 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14930 /*consume_paren=*/true);
14934 /* If we're allowing GNU extensions, check for the extended assembly
14935 syntax. Unfortunately, the `:' tokens need not be separated by
14936 a space in C, and so, for compatibility, we tolerate that here
14937 too. Doing that means that we have to treat the `::' operator as
14939 if (cp_parser_allow_gnu_extensions_p (parser)
14940 && parser->in_function_body
14941 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14942 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14944 bool inputs_p = false;
14945 bool clobbers_p = false;
14946 bool labels_p = false;
14948 /* The extended syntax was used. */
14951 /* Look for outputs. */
14952 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14954 /* Consume the `:'. */
14955 cp_lexer_consume_token (parser->lexer);
14956 /* Parse the output-operands. */
14957 if (cp_lexer_next_token_is_not (parser->lexer,
14959 && cp_lexer_next_token_is_not (parser->lexer,
14961 && cp_lexer_next_token_is_not (parser->lexer,
14964 outputs = cp_parser_asm_operand_list (parser);
14966 if (outputs == error_mark_node)
14967 invalid_outputs_p = true;
14969 /* If the next token is `::', there are no outputs, and the
14970 next token is the beginning of the inputs. */
14971 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14972 /* The inputs are coming next. */
14975 /* Look for inputs. */
14977 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14979 /* Consume the `:' or `::'. */
14980 cp_lexer_consume_token (parser->lexer);
14981 /* Parse the output-operands. */
14982 if (cp_lexer_next_token_is_not (parser->lexer,
14984 && cp_lexer_next_token_is_not (parser->lexer,
14986 && cp_lexer_next_token_is_not (parser->lexer,
14988 inputs = cp_parser_asm_operand_list (parser);
14990 if (inputs == error_mark_node)
14991 invalid_inputs_p = true;
14993 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14994 /* The clobbers are coming next. */
14997 /* Look for clobbers. */
14999 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15002 /* Consume the `:' or `::'. */
15003 cp_lexer_consume_token (parser->lexer);
15004 /* Parse the clobbers. */
15005 if (cp_lexer_next_token_is_not (parser->lexer,
15007 && cp_lexer_next_token_is_not (parser->lexer,
15009 clobbers = cp_parser_asm_clobber_list (parser);
15012 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15013 /* The labels are coming next. */
15016 /* Look for labels. */
15018 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15021 /* Consume the `:' or `::'. */
15022 cp_lexer_consume_token (parser->lexer);
15023 /* Parse the labels. */
15024 labels = cp_parser_asm_label_list (parser);
15027 if (goto_p && !labels_p)
15028 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15031 missing = RT_COLON_SCOPE;
15033 /* Look for the closing `)'. */
15034 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15035 missing ? missing : RT_CLOSE_PAREN))
15036 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15037 /*consume_paren=*/true);
15038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15040 if (!invalid_inputs_p && !invalid_outputs_p)
15042 /* Create the ASM_EXPR. */
15043 if (parser->in_function_body)
15045 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15046 inputs, clobbers, labels);
15047 /* If the extended syntax was not used, mark the ASM_EXPR. */
15050 tree temp = asm_stmt;
15051 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15052 temp = TREE_OPERAND (temp, 0);
15054 ASM_INPUT_P (temp) = 1;
15058 cgraph_add_asm_node (string);
15062 /* Declarators [gram.dcl.decl] */
15064 /* Parse an init-declarator.
15067 declarator initializer [opt]
15072 declarator asm-specification [opt] attributes [opt] initializer [opt]
15074 function-definition:
15075 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15077 decl-specifier-seq [opt] declarator function-try-block
15081 function-definition:
15082 __extension__ function-definition
15084 The DECL_SPECIFIERS apply to this declarator. Returns a
15085 representation of the entity declared. If MEMBER_P is TRUE, then
15086 this declarator appears in a class scope. The new DECL created by
15087 this declarator is returned.
15089 The CHECKS are access checks that should be performed once we know
15090 what entity is being declared (and, therefore, what classes have
15093 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15094 for a function-definition here as well. If the declarator is a
15095 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15096 be TRUE upon return. By that point, the function-definition will
15097 have been completely parsed.
15099 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15102 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15103 parsed declaration if it is an uninitialized single declarator not followed
15104 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15105 if present, will not be consumed. If returned, this declarator will be
15106 created with SD_INITIALIZED but will not call cp_finish_decl. */
15109 cp_parser_init_declarator (cp_parser* parser,
15110 cp_decl_specifier_seq *decl_specifiers,
15111 VEC (deferred_access_check,gc)* checks,
15112 bool function_definition_allowed_p,
15114 int declares_class_or_enum,
15115 bool* function_definition_p,
15116 tree* maybe_range_for_decl)
15118 cp_token *token = NULL, *asm_spec_start_token = NULL,
15119 *attributes_start_token = NULL;
15120 cp_declarator *declarator;
15121 tree prefix_attributes;
15123 tree asm_specification;
15125 tree decl = NULL_TREE;
15127 int is_initialized;
15128 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15129 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15131 enum cpp_ttype initialization_kind;
15132 bool is_direct_init = false;
15133 bool is_non_constant_init;
15134 int ctor_dtor_or_conv_p;
15136 tree pushed_scope = NULL_TREE;
15137 bool range_for_decl_p = false;
15139 /* Gather the attributes that were provided with the
15140 decl-specifiers. */
15141 prefix_attributes = decl_specifiers->attributes;
15143 /* Assume that this is not the declarator for a function
15145 if (function_definition_p)
15146 *function_definition_p = false;
15148 /* Defer access checks while parsing the declarator; we cannot know
15149 what names are accessible until we know what is being
15151 resume_deferring_access_checks ();
15153 /* Parse the declarator. */
15154 token = cp_lexer_peek_token (parser->lexer);
15156 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15157 &ctor_dtor_or_conv_p,
15158 /*parenthesized_p=*/NULL,
15160 /* Gather up the deferred checks. */
15161 stop_deferring_access_checks ();
15163 /* If the DECLARATOR was erroneous, there's no need to go
15165 if (declarator == cp_error_declarator)
15166 return error_mark_node;
15168 /* Check that the number of template-parameter-lists is OK. */
15169 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15171 return error_mark_node;
15173 if (declares_class_or_enum & 2)
15174 cp_parser_check_for_definition_in_return_type (declarator,
15175 decl_specifiers->type,
15176 decl_specifiers->type_location);
15178 /* Figure out what scope the entity declared by the DECLARATOR is
15179 located in. `grokdeclarator' sometimes changes the scope, so
15180 we compute it now. */
15181 scope = get_scope_of_declarator (declarator);
15183 /* Perform any lookups in the declared type which were thought to be
15184 dependent, but are not in the scope of the declarator. */
15185 decl_specifiers->type
15186 = maybe_update_decl_type (decl_specifiers->type, scope);
15188 /* If we're allowing GNU extensions, look for an asm-specification
15190 if (cp_parser_allow_gnu_extensions_p (parser))
15192 /* Look for an asm-specification. */
15193 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15194 asm_specification = cp_parser_asm_specification_opt (parser);
15195 /* And attributes. */
15196 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15197 attributes = cp_parser_attributes_opt (parser);
15201 asm_specification = NULL_TREE;
15202 attributes = NULL_TREE;
15205 /* Peek at the next token. */
15206 token = cp_lexer_peek_token (parser->lexer);
15207 /* Check to see if the token indicates the start of a
15208 function-definition. */
15209 if (function_declarator_p (declarator)
15210 && cp_parser_token_starts_function_definition_p (token))
15212 if (!function_definition_allowed_p)
15214 /* If a function-definition should not appear here, issue an
15216 cp_parser_error (parser,
15217 "a function-definition is not allowed here");
15218 return error_mark_node;
15222 location_t func_brace_location
15223 = cp_lexer_peek_token (parser->lexer)->location;
15225 /* Neither attributes nor an asm-specification are allowed
15226 on a function-definition. */
15227 if (asm_specification)
15228 error_at (asm_spec_start_token->location,
15229 "an asm-specification is not allowed "
15230 "on a function-definition");
15232 error_at (attributes_start_token->location,
15233 "attributes are not allowed on a function-definition");
15234 /* This is a function-definition. */
15235 *function_definition_p = true;
15237 /* Parse the function definition. */
15239 decl = cp_parser_save_member_function_body (parser,
15242 prefix_attributes);
15245 = (cp_parser_function_definition_from_specifiers_and_declarator
15246 (parser, decl_specifiers, prefix_attributes, declarator));
15248 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15250 /* This is where the prologue starts... */
15251 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15252 = func_brace_location;
15261 Only in function declarations for constructors, destructors, and
15262 type conversions can the decl-specifier-seq be omitted.
15264 We explicitly postpone this check past the point where we handle
15265 function-definitions because we tolerate function-definitions
15266 that are missing their return types in some modes. */
15267 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15269 cp_parser_error (parser,
15270 "expected constructor, destructor, or type conversion");
15271 return error_mark_node;
15274 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15275 if (token->type == CPP_EQ
15276 || token->type == CPP_OPEN_PAREN
15277 || token->type == CPP_OPEN_BRACE)
15279 is_initialized = SD_INITIALIZED;
15280 initialization_kind = token->type;
15281 if (maybe_range_for_decl)
15282 *maybe_range_for_decl = error_mark_node;
15284 if (token->type == CPP_EQ
15285 && function_declarator_p (declarator))
15287 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15288 if (t2->keyword == RID_DEFAULT)
15289 is_initialized = SD_DEFAULTED;
15290 else if (t2->keyword == RID_DELETE)
15291 is_initialized = SD_DELETED;
15296 /* If the init-declarator isn't initialized and isn't followed by a
15297 `,' or `;', it's not a valid init-declarator. */
15298 if (token->type != CPP_COMMA
15299 && token->type != CPP_SEMICOLON)
15301 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15302 range_for_decl_p = true;
15305 cp_parser_error (parser, "expected initializer");
15306 return error_mark_node;
15309 is_initialized = SD_UNINITIALIZED;
15310 initialization_kind = CPP_EOF;
15313 /* Because start_decl has side-effects, we should only call it if we
15314 know we're going ahead. By this point, we know that we cannot
15315 possibly be looking at any other construct. */
15316 cp_parser_commit_to_tentative_parse (parser);
15318 /* If the decl specifiers were bad, issue an error now that we're
15319 sure this was intended to be a declarator. Then continue
15320 declaring the variable(s), as int, to try to cut down on further
15322 if (decl_specifiers->any_specifiers_p
15323 && decl_specifiers->type == error_mark_node)
15325 cp_parser_error (parser, "invalid type in declaration");
15326 decl_specifiers->type = integer_type_node;
15329 /* Check to see whether or not this declaration is a friend. */
15330 friend_p = cp_parser_friend_p (decl_specifiers);
15332 /* Enter the newly declared entry in the symbol table. If we're
15333 processing a declaration in a class-specifier, we wait until
15334 after processing the initializer. */
15337 if (parser->in_unbraced_linkage_specification_p)
15338 decl_specifiers->storage_class = sc_extern;
15339 decl = start_decl (declarator, decl_specifiers,
15340 range_for_decl_p? SD_INITIALIZED : is_initialized,
15341 attributes, prefix_attributes,
15343 /* Adjust location of decl if declarator->id_loc is more appropriate:
15344 set, and decl wasn't merged with another decl, in which case its
15345 location would be different from input_location, and more accurate. */
15347 && declarator->id_loc != UNKNOWN_LOCATION
15348 && DECL_SOURCE_LOCATION (decl) == input_location)
15349 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15352 /* Enter the SCOPE. That way unqualified names appearing in the
15353 initializer will be looked up in SCOPE. */
15354 pushed_scope = push_scope (scope);
15356 /* Perform deferred access control checks, now that we know in which
15357 SCOPE the declared entity resides. */
15358 if (!member_p && decl)
15360 tree saved_current_function_decl = NULL_TREE;
15362 /* If the entity being declared is a function, pretend that we
15363 are in its scope. If it is a `friend', it may have access to
15364 things that would not otherwise be accessible. */
15365 if (TREE_CODE (decl) == FUNCTION_DECL)
15367 saved_current_function_decl = current_function_decl;
15368 current_function_decl = decl;
15371 /* Perform access checks for template parameters. */
15372 cp_parser_perform_template_parameter_access_checks (checks);
15374 /* Perform the access control checks for the declarator and the
15375 decl-specifiers. */
15376 perform_deferred_access_checks ();
15378 /* Restore the saved value. */
15379 if (TREE_CODE (decl) == FUNCTION_DECL)
15380 current_function_decl = saved_current_function_decl;
15383 /* Parse the initializer. */
15384 initializer = NULL_TREE;
15385 is_direct_init = false;
15386 is_non_constant_init = true;
15387 if (is_initialized)
15389 if (function_declarator_p (declarator))
15391 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15392 if (initialization_kind == CPP_EQ)
15393 initializer = cp_parser_pure_specifier (parser);
15396 /* If the declaration was erroneous, we don't really
15397 know what the user intended, so just silently
15398 consume the initializer. */
15399 if (decl != error_mark_node)
15400 error_at (initializer_start_token->location,
15401 "initializer provided for function");
15402 cp_parser_skip_to_closing_parenthesis (parser,
15403 /*recovering=*/true,
15404 /*or_comma=*/false,
15405 /*consume_paren=*/true);
15410 /* We want to record the extra mangling scope for in-class
15411 initializers of class members and initializers of static data
15412 member templates. The former is a C++0x feature which isn't
15413 implemented yet, and I expect it will involve deferring
15414 parsing of the initializer until end of class as with default
15415 arguments. So right here we only handle the latter. */
15416 if (!member_p && processing_template_decl)
15417 start_lambda_scope (decl);
15418 initializer = cp_parser_initializer (parser,
15420 &is_non_constant_init);
15421 if (!member_p && processing_template_decl)
15422 finish_lambda_scope ();
15426 /* The old parser allows attributes to appear after a parenthesized
15427 initializer. Mark Mitchell proposed removing this functionality
15428 on the GCC mailing lists on 2002-08-13. This parser accepts the
15429 attributes -- but ignores them. */
15430 if (cp_parser_allow_gnu_extensions_p (parser)
15431 && initialization_kind == CPP_OPEN_PAREN)
15432 if (cp_parser_attributes_opt (parser))
15433 warning (OPT_Wattributes,
15434 "attributes after parenthesized initializer ignored");
15436 /* For an in-class declaration, use `grokfield' to create the
15442 pop_scope (pushed_scope);
15443 pushed_scope = NULL_TREE;
15445 decl = grokfield (declarator, decl_specifiers,
15446 initializer, !is_non_constant_init,
15447 /*asmspec=*/NULL_TREE,
15448 prefix_attributes);
15449 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15450 cp_parser_save_default_args (parser, decl);
15453 /* Finish processing the declaration. But, skip member
15455 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15457 cp_finish_decl (decl,
15458 initializer, !is_non_constant_init,
15460 /* If the initializer is in parentheses, then this is
15461 a direct-initialization, which means that an
15462 `explicit' constructor is OK. Otherwise, an
15463 `explicit' constructor cannot be used. */
15464 ((is_direct_init || !is_initialized)
15465 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15467 else if ((cxx_dialect != cxx98) && friend_p
15468 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15469 /* Core issue #226 (C++0x only): A default template-argument
15470 shall not be specified in a friend class template
15472 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15473 /*is_partial=*/0, /*is_friend_decl=*/1);
15475 if (!friend_p && pushed_scope)
15476 pop_scope (pushed_scope);
15481 /* Parse a declarator.
15485 ptr-operator declarator
15487 abstract-declarator:
15488 ptr-operator abstract-declarator [opt]
15489 direct-abstract-declarator
15494 attributes [opt] direct-declarator
15495 attributes [opt] ptr-operator declarator
15497 abstract-declarator:
15498 attributes [opt] ptr-operator abstract-declarator [opt]
15499 attributes [opt] direct-abstract-declarator
15501 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15502 detect constructor, destructor or conversion operators. It is set
15503 to -1 if the declarator is a name, and +1 if it is a
15504 function. Otherwise it is set to zero. Usually you just want to
15505 test for >0, but internally the negative value is used.
15507 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15508 a decl-specifier-seq unless it declares a constructor, destructor,
15509 or conversion. It might seem that we could check this condition in
15510 semantic analysis, rather than parsing, but that makes it difficult
15511 to handle something like `f()'. We want to notice that there are
15512 no decl-specifiers, and therefore realize that this is an
15513 expression, not a declaration.)
15515 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15516 the declarator is a direct-declarator of the form "(...)".
15518 MEMBER_P is true iff this declarator is a member-declarator. */
15520 static cp_declarator *
15521 cp_parser_declarator (cp_parser* parser,
15522 cp_parser_declarator_kind dcl_kind,
15523 int* ctor_dtor_or_conv_p,
15524 bool* parenthesized_p,
15527 cp_declarator *declarator;
15528 enum tree_code code;
15529 cp_cv_quals cv_quals;
15531 tree attributes = NULL_TREE;
15533 /* Assume this is not a constructor, destructor, or type-conversion
15535 if (ctor_dtor_or_conv_p)
15536 *ctor_dtor_or_conv_p = 0;
15538 if (cp_parser_allow_gnu_extensions_p (parser))
15539 attributes = cp_parser_attributes_opt (parser);
15541 /* Check for the ptr-operator production. */
15542 cp_parser_parse_tentatively (parser);
15543 /* Parse the ptr-operator. */
15544 code = cp_parser_ptr_operator (parser,
15547 /* If that worked, then we have a ptr-operator. */
15548 if (cp_parser_parse_definitely (parser))
15550 /* If a ptr-operator was found, then this declarator was not
15552 if (parenthesized_p)
15553 *parenthesized_p = true;
15554 /* The dependent declarator is optional if we are parsing an
15555 abstract-declarator. */
15556 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15557 cp_parser_parse_tentatively (parser);
15559 /* Parse the dependent declarator. */
15560 declarator = cp_parser_declarator (parser, dcl_kind,
15561 /*ctor_dtor_or_conv_p=*/NULL,
15562 /*parenthesized_p=*/NULL,
15563 /*member_p=*/false);
15565 /* If we are parsing an abstract-declarator, we must handle the
15566 case where the dependent declarator is absent. */
15567 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15568 && !cp_parser_parse_definitely (parser))
15571 declarator = cp_parser_make_indirect_declarator
15572 (code, class_type, cv_quals, declarator);
15574 /* Everything else is a direct-declarator. */
15577 if (parenthesized_p)
15578 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15580 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15581 ctor_dtor_or_conv_p,
15585 if (attributes && declarator && declarator != cp_error_declarator)
15586 declarator->attributes = attributes;
15591 /* Parse a direct-declarator or direct-abstract-declarator.
15595 direct-declarator ( parameter-declaration-clause )
15596 cv-qualifier-seq [opt]
15597 exception-specification [opt]
15598 direct-declarator [ constant-expression [opt] ]
15601 direct-abstract-declarator:
15602 direct-abstract-declarator [opt]
15603 ( parameter-declaration-clause )
15604 cv-qualifier-seq [opt]
15605 exception-specification [opt]
15606 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15607 ( abstract-declarator )
15609 Returns a representation of the declarator. DCL_KIND is
15610 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15611 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15612 we are parsing a direct-declarator. It is
15613 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15614 of ambiguity we prefer an abstract declarator, as per
15615 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15616 cp_parser_declarator. */
15618 static cp_declarator *
15619 cp_parser_direct_declarator (cp_parser* parser,
15620 cp_parser_declarator_kind dcl_kind,
15621 int* ctor_dtor_or_conv_p,
15625 cp_declarator *declarator = NULL;
15626 tree scope = NULL_TREE;
15627 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15628 bool saved_in_declarator_p = parser->in_declarator_p;
15630 tree pushed_scope = NULL_TREE;
15634 /* Peek at the next token. */
15635 token = cp_lexer_peek_token (parser->lexer);
15636 if (token->type == CPP_OPEN_PAREN)
15638 /* This is either a parameter-declaration-clause, or a
15639 parenthesized declarator. When we know we are parsing a
15640 named declarator, it must be a parenthesized declarator
15641 if FIRST is true. For instance, `(int)' is a
15642 parameter-declaration-clause, with an omitted
15643 direct-abstract-declarator. But `((*))', is a
15644 parenthesized abstract declarator. Finally, when T is a
15645 template parameter `(T)' is a
15646 parameter-declaration-clause, and not a parenthesized
15649 We first try and parse a parameter-declaration-clause,
15650 and then try a nested declarator (if FIRST is true).
15652 It is not an error for it not to be a
15653 parameter-declaration-clause, even when FIRST is
15659 The first is the declaration of a function while the
15660 second is the definition of a variable, including its
15663 Having seen only the parenthesis, we cannot know which of
15664 these two alternatives should be selected. Even more
15665 complex are examples like:
15670 The former is a function-declaration; the latter is a
15671 variable initialization.
15673 Thus again, we try a parameter-declaration-clause, and if
15674 that fails, we back out and return. */
15676 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15679 unsigned saved_num_template_parameter_lists;
15680 bool is_declarator = false;
15683 /* In a member-declarator, the only valid interpretation
15684 of a parenthesis is the start of a
15685 parameter-declaration-clause. (It is invalid to
15686 initialize a static data member with a parenthesized
15687 initializer; only the "=" form of initialization is
15690 cp_parser_parse_tentatively (parser);
15692 /* Consume the `('. */
15693 cp_lexer_consume_token (parser->lexer);
15696 /* If this is going to be an abstract declarator, we're
15697 in a declarator and we can't have default args. */
15698 parser->default_arg_ok_p = false;
15699 parser->in_declarator_p = true;
15702 /* Inside the function parameter list, surrounding
15703 template-parameter-lists do not apply. */
15704 saved_num_template_parameter_lists
15705 = parser->num_template_parameter_lists;
15706 parser->num_template_parameter_lists = 0;
15708 begin_scope (sk_function_parms, NULL_TREE);
15710 /* Parse the parameter-declaration-clause. */
15711 params = cp_parser_parameter_declaration_clause (parser);
15713 parser->num_template_parameter_lists
15714 = saved_num_template_parameter_lists;
15716 /* Consume the `)'. */
15717 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15719 /* If all went well, parse the cv-qualifier-seq and the
15720 exception-specification. */
15721 if (member_p || cp_parser_parse_definitely (parser))
15723 cp_cv_quals cv_quals;
15724 cp_virt_specifiers virt_specifiers;
15725 tree exception_specification;
15728 is_declarator = true;
15730 if (ctor_dtor_or_conv_p)
15731 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15734 /* Parse the cv-qualifier-seq. */
15735 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15736 /* And the exception-specification. */
15737 exception_specification
15738 = cp_parser_exception_specification_opt (parser);
15739 /* Parse the virt-specifier-seq. */
15740 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15742 late_return = (cp_parser_late_return_type_opt
15743 (parser, member_p ? cv_quals : -1));
15745 /* Create the function-declarator. */
15746 declarator = make_call_declarator (declarator,
15750 exception_specification,
15752 /* Any subsequent parameter lists are to do with
15753 return type, so are not those of the declared
15755 parser->default_arg_ok_p = false;
15758 /* Remove the function parms from scope. */
15759 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15760 pop_binding (DECL_NAME (t), t);
15764 /* Repeat the main loop. */
15768 /* If this is the first, we can try a parenthesized
15772 bool saved_in_type_id_in_expr_p;
15774 parser->default_arg_ok_p = saved_default_arg_ok_p;
15775 parser->in_declarator_p = saved_in_declarator_p;
15777 /* Consume the `('. */
15778 cp_lexer_consume_token (parser->lexer);
15779 /* Parse the nested declarator. */
15780 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15781 parser->in_type_id_in_expr_p = true;
15783 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15784 /*parenthesized_p=*/NULL,
15786 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15788 /* Expect a `)'. */
15789 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15790 declarator = cp_error_declarator;
15791 if (declarator == cp_error_declarator)
15794 goto handle_declarator;
15796 /* Otherwise, we must be done. */
15800 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15801 && token->type == CPP_OPEN_SQUARE)
15803 /* Parse an array-declarator. */
15806 if (ctor_dtor_or_conv_p)
15807 *ctor_dtor_or_conv_p = 0;
15810 parser->default_arg_ok_p = false;
15811 parser->in_declarator_p = true;
15812 /* Consume the `['. */
15813 cp_lexer_consume_token (parser->lexer);
15814 /* Peek at the next token. */
15815 token = cp_lexer_peek_token (parser->lexer);
15816 /* If the next token is `]', then there is no
15817 constant-expression. */
15818 if (token->type != CPP_CLOSE_SQUARE)
15820 bool non_constant_p;
15823 = cp_parser_constant_expression (parser,
15824 /*allow_non_constant=*/true,
15826 if (!non_constant_p)
15828 /* Normally, the array bound must be an integral constant
15829 expression. However, as an extension, we allow VLAs
15830 in function scopes as long as they aren't part of a
15831 parameter declaration. */
15832 else if (!parser->in_function_body
15833 || current_binding_level->kind == sk_function_parms)
15835 cp_parser_error (parser,
15836 "array bound is not an integer constant");
15837 bounds = error_mark_node;
15839 else if (processing_template_decl && !error_operand_p (bounds))
15841 /* Remember this wasn't a constant-expression. */
15842 bounds = build_nop (TREE_TYPE (bounds), bounds);
15843 TREE_SIDE_EFFECTS (bounds) = 1;
15847 bounds = NULL_TREE;
15848 /* Look for the closing `]'. */
15849 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15851 declarator = cp_error_declarator;
15855 declarator = make_array_declarator (declarator, bounds);
15857 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15860 tree qualifying_scope;
15861 tree unqualified_name;
15862 special_function_kind sfk;
15864 bool pack_expansion_p = false;
15865 cp_token *declarator_id_start_token;
15867 /* Parse a declarator-id */
15868 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15871 cp_parser_parse_tentatively (parser);
15873 /* If we see an ellipsis, we should be looking at a
15875 if (token->type == CPP_ELLIPSIS)
15877 /* Consume the `...' */
15878 cp_lexer_consume_token (parser->lexer);
15880 pack_expansion_p = true;
15884 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15886 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15887 qualifying_scope = parser->scope;
15892 if (!unqualified_name && pack_expansion_p)
15894 /* Check whether an error occurred. */
15895 okay = !cp_parser_error_occurred (parser);
15897 /* We already consumed the ellipsis to mark a
15898 parameter pack, but we have no way to report it,
15899 so abort the tentative parse. We will be exiting
15900 immediately anyway. */
15901 cp_parser_abort_tentative_parse (parser);
15904 okay = cp_parser_parse_definitely (parser);
15907 unqualified_name = error_mark_node;
15908 else if (unqualified_name
15909 && (qualifying_scope
15910 || (TREE_CODE (unqualified_name)
15911 != IDENTIFIER_NODE)))
15913 cp_parser_error (parser, "expected unqualified-id");
15914 unqualified_name = error_mark_node;
15918 if (!unqualified_name)
15920 if (unqualified_name == error_mark_node)
15922 declarator = cp_error_declarator;
15923 pack_expansion_p = false;
15924 declarator->parameter_pack_p = false;
15928 if (qualifying_scope && at_namespace_scope_p ()
15929 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15931 /* In the declaration of a member of a template class
15932 outside of the class itself, the SCOPE will sometimes
15933 be a TYPENAME_TYPE. For example, given:
15935 template <typename T>
15936 int S<T>::R::i = 3;
15938 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15939 this context, we must resolve S<T>::R to an ordinary
15940 type, rather than a typename type.
15942 The reason we normally avoid resolving TYPENAME_TYPEs
15943 is that a specialization of `S' might render
15944 `S<T>::R' not a type. However, if `S' is
15945 specialized, then this `i' will not be used, so there
15946 is no harm in resolving the types here. */
15949 /* Resolve the TYPENAME_TYPE. */
15950 type = resolve_typename_type (qualifying_scope,
15951 /*only_current_p=*/false);
15952 /* If that failed, the declarator is invalid. */
15953 if (TREE_CODE (type) == TYPENAME_TYPE)
15955 if (typedef_variant_p (type))
15956 error_at (declarator_id_start_token->location,
15957 "cannot define member of dependent typedef "
15960 error_at (declarator_id_start_token->location,
15961 "%<%T::%E%> is not a type",
15962 TYPE_CONTEXT (qualifying_scope),
15963 TYPE_IDENTIFIER (qualifying_scope));
15965 qualifying_scope = type;
15970 if (unqualified_name)
15974 if (qualifying_scope
15975 && CLASS_TYPE_P (qualifying_scope))
15976 class_type = qualifying_scope;
15978 class_type = current_class_type;
15980 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15982 tree name_type = TREE_TYPE (unqualified_name);
15983 if (class_type && same_type_p (name_type, class_type))
15985 if (qualifying_scope
15986 && CLASSTYPE_USE_TEMPLATE (name_type))
15988 error_at (declarator_id_start_token->location,
15989 "invalid use of constructor as a template");
15990 inform (declarator_id_start_token->location,
15991 "use %<%T::%D%> instead of %<%T::%D%> to "
15992 "name the constructor in a qualified name",
15994 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15995 class_type, name_type);
15996 declarator = cp_error_declarator;
16000 unqualified_name = constructor_name (class_type);
16004 /* We do not attempt to print the declarator
16005 here because we do not have enough
16006 information about its original syntactic
16008 cp_parser_error (parser, "invalid declarator");
16009 declarator = cp_error_declarator;
16016 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16017 sfk = sfk_destructor;
16018 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16019 sfk = sfk_conversion;
16020 else if (/* There's no way to declare a constructor
16021 for an anonymous type, even if the type
16022 got a name for linkage purposes. */
16023 !TYPE_WAS_ANONYMOUS (class_type)
16024 && constructor_name_p (unqualified_name,
16027 unqualified_name = constructor_name (class_type);
16028 sfk = sfk_constructor;
16030 else if (is_overloaded_fn (unqualified_name)
16031 && DECL_CONSTRUCTOR_P (get_first_fn
16032 (unqualified_name)))
16033 sfk = sfk_constructor;
16035 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16036 *ctor_dtor_or_conv_p = -1;
16039 declarator = make_id_declarator (qualifying_scope,
16042 declarator->id_loc = token->location;
16043 declarator->parameter_pack_p = pack_expansion_p;
16045 if (pack_expansion_p)
16046 maybe_warn_variadic_templates ();
16049 handle_declarator:;
16050 scope = get_scope_of_declarator (declarator);
16052 /* Any names that appear after the declarator-id for a
16053 member are looked up in the containing scope. */
16054 pushed_scope = push_scope (scope);
16055 parser->in_declarator_p = true;
16056 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16057 || (declarator && declarator->kind == cdk_id))
16058 /* Default args are only allowed on function
16060 parser->default_arg_ok_p = saved_default_arg_ok_p;
16062 parser->default_arg_ok_p = false;
16071 /* For an abstract declarator, we might wind up with nothing at this
16072 point. That's an error; the declarator is not optional. */
16074 cp_parser_error (parser, "expected declarator");
16076 /* If we entered a scope, we must exit it now. */
16078 pop_scope (pushed_scope);
16080 parser->default_arg_ok_p = saved_default_arg_ok_p;
16081 parser->in_declarator_p = saved_in_declarator_p;
16086 /* Parse a ptr-operator.
16089 * cv-qualifier-seq [opt]
16091 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16096 & cv-qualifier-seq [opt]
16098 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16099 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16100 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16101 filled in with the TYPE containing the member. *CV_QUALS is
16102 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16103 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16104 Note that the tree codes returned by this function have nothing
16105 to do with the types of trees that will be eventually be created
16106 to represent the pointer or reference type being parsed. They are
16107 just constants with suggestive names. */
16108 static enum tree_code
16109 cp_parser_ptr_operator (cp_parser* parser,
16111 cp_cv_quals *cv_quals)
16113 enum tree_code code = ERROR_MARK;
16116 /* Assume that it's not a pointer-to-member. */
16118 /* And that there are no cv-qualifiers. */
16119 *cv_quals = TYPE_UNQUALIFIED;
16121 /* Peek at the next token. */
16122 token = cp_lexer_peek_token (parser->lexer);
16124 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16125 if (token->type == CPP_MULT)
16126 code = INDIRECT_REF;
16127 else if (token->type == CPP_AND)
16129 else if ((cxx_dialect != cxx98) &&
16130 token->type == CPP_AND_AND) /* C++0x only */
16131 code = NON_LVALUE_EXPR;
16133 if (code != ERROR_MARK)
16135 /* Consume the `*', `&' or `&&'. */
16136 cp_lexer_consume_token (parser->lexer);
16138 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16139 `&', if we are allowing GNU extensions. (The only qualifier
16140 that can legally appear after `&' is `restrict', but that is
16141 enforced during semantic analysis. */
16142 if (code == INDIRECT_REF
16143 || cp_parser_allow_gnu_extensions_p (parser))
16144 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16148 /* Try the pointer-to-member case. */
16149 cp_parser_parse_tentatively (parser);
16150 /* Look for the optional `::' operator. */
16151 cp_parser_global_scope_opt (parser,
16152 /*current_scope_valid_p=*/false);
16153 /* Look for the nested-name specifier. */
16154 token = cp_lexer_peek_token (parser->lexer);
16155 cp_parser_nested_name_specifier (parser,
16156 /*typename_keyword_p=*/false,
16157 /*check_dependency_p=*/true,
16159 /*is_declaration=*/false);
16160 /* If we found it, and the next token is a `*', then we are
16161 indeed looking at a pointer-to-member operator. */
16162 if (!cp_parser_error_occurred (parser)
16163 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16165 /* Indicate that the `*' operator was used. */
16166 code = INDIRECT_REF;
16168 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16169 error_at (token->location, "%qD is a namespace", parser->scope);
16172 /* The type of which the member is a member is given by the
16174 *type = parser->scope;
16175 /* The next name will not be qualified. */
16176 parser->scope = NULL_TREE;
16177 parser->qualifying_scope = NULL_TREE;
16178 parser->object_scope = NULL_TREE;
16179 /* Look for the optional cv-qualifier-seq. */
16180 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16183 /* If that didn't work we don't have a ptr-operator. */
16184 if (!cp_parser_parse_definitely (parser))
16185 cp_parser_error (parser, "expected ptr-operator");
16191 /* Parse an (optional) cv-qualifier-seq.
16194 cv-qualifier cv-qualifier-seq [opt]
16205 Returns a bitmask representing the cv-qualifiers. */
16208 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16210 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16215 cp_cv_quals cv_qualifier;
16217 /* Peek at the next token. */
16218 token = cp_lexer_peek_token (parser->lexer);
16219 /* See if it's a cv-qualifier. */
16220 switch (token->keyword)
16223 cv_qualifier = TYPE_QUAL_CONST;
16227 cv_qualifier = TYPE_QUAL_VOLATILE;
16231 cv_qualifier = TYPE_QUAL_RESTRICT;
16235 cv_qualifier = TYPE_UNQUALIFIED;
16242 if (cv_quals & cv_qualifier)
16244 error_at (token->location, "duplicate cv-qualifier");
16245 cp_lexer_purge_token (parser->lexer);
16249 cp_lexer_consume_token (parser->lexer);
16250 cv_quals |= cv_qualifier;
16257 /* Parse an (optional) virt-specifier-seq.
16259 virt-specifier-seq:
16260 virt-specifier virt-specifier-seq [opt]
16266 Returns a bitmask representing the virt-specifiers. */
16268 static cp_virt_specifiers
16269 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16271 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16276 cp_virt_specifiers virt_specifier;
16278 /* Peek at the next token. */
16279 token = cp_lexer_peek_token (parser->lexer);
16280 /* See if it's a virt-specifier-qualifier. */
16281 if (token->type != CPP_NAME)
16283 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16285 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16286 virt_specifier = VIRT_SPEC_OVERRIDE;
16288 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16290 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16291 virt_specifier = VIRT_SPEC_FINAL;
16293 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16295 virt_specifier = VIRT_SPEC_FINAL;
16300 if (virt_specifiers & virt_specifier)
16302 error_at (token->location, "duplicate virt-specifier");
16303 cp_lexer_purge_token (parser->lexer);
16307 cp_lexer_consume_token (parser->lexer);
16308 virt_specifiers |= virt_specifier;
16311 return virt_specifiers;
16314 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16315 is in scope even though it isn't real. */
16318 inject_this_parameter (tree ctype, cp_cv_quals quals)
16322 if (current_class_ptr)
16324 /* We don't clear this between NSDMIs. Is it already what we want? */
16325 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16326 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16327 && cp_type_quals (type) == quals)
16331 this_parm = build_this_parm (ctype, quals);
16332 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16333 current_class_ptr = NULL_TREE;
16335 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16336 current_class_ptr = this_parm;
16339 /* Parse a late-specified return type, if any. This is not a separate
16340 non-terminal, but part of a function declarator, which looks like
16342 -> trailing-type-specifier-seq abstract-declarator(opt)
16344 Returns the type indicated by the type-id.
16346 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16350 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16355 /* Peek at the next token. */
16356 token = cp_lexer_peek_token (parser->lexer);
16357 /* A late-specified return type is indicated by an initial '->'. */
16358 if (token->type != CPP_DEREF)
16361 /* Consume the ->. */
16362 cp_lexer_consume_token (parser->lexer);
16366 /* DR 1207: 'this' is in scope in the trailing return type. */
16367 gcc_assert (current_class_ptr == NULL_TREE);
16368 inject_this_parameter (current_class_type, quals);
16371 type = cp_parser_trailing_type_id (parser);
16374 current_class_ptr = current_class_ref = NULL_TREE;
16379 /* Parse a declarator-id.
16383 :: [opt] nested-name-specifier [opt] type-name
16385 In the `id-expression' case, the value returned is as for
16386 cp_parser_id_expression if the id-expression was an unqualified-id.
16387 If the id-expression was a qualified-id, then a SCOPE_REF is
16388 returned. The first operand is the scope (either a NAMESPACE_DECL
16389 or TREE_TYPE), but the second is still just a representation of an
16393 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16396 /* The expression must be an id-expression. Assume that qualified
16397 names are the names of types so that:
16400 int S<T>::R::i = 3;
16402 will work; we must treat `S<T>::R' as the name of a type.
16403 Similarly, assume that qualified names are templates, where
16407 int S<T>::R<T>::i = 3;
16410 id = cp_parser_id_expression (parser,
16411 /*template_keyword_p=*/false,
16412 /*check_dependency_p=*/false,
16413 /*template_p=*/NULL,
16414 /*declarator_p=*/true,
16416 if (id && BASELINK_P (id))
16417 id = BASELINK_FUNCTIONS (id);
16421 /* Parse a type-id.
16424 type-specifier-seq abstract-declarator [opt]
16426 Returns the TYPE specified. */
16429 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16430 bool is_trailing_return)
16432 cp_decl_specifier_seq type_specifier_seq;
16433 cp_declarator *abstract_declarator;
16435 /* Parse the type-specifier-seq. */
16436 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16437 is_trailing_return,
16438 &type_specifier_seq);
16439 if (type_specifier_seq.type == error_mark_node)
16440 return error_mark_node;
16442 /* There might or might not be an abstract declarator. */
16443 cp_parser_parse_tentatively (parser);
16444 /* Look for the declarator. */
16445 abstract_declarator
16446 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16447 /*parenthesized_p=*/NULL,
16448 /*member_p=*/false);
16449 /* Check to see if there really was a declarator. */
16450 if (!cp_parser_parse_definitely (parser))
16451 abstract_declarator = NULL;
16453 if (type_specifier_seq.type
16454 && type_uses_auto (type_specifier_seq.type))
16456 /* A type-id with type 'auto' is only ok if the abstract declarator
16457 is a function declarator with a late-specified return type. */
16458 if (abstract_declarator
16459 && abstract_declarator->kind == cdk_function
16460 && abstract_declarator->u.function.late_return_type)
16464 error ("invalid use of %<auto%>");
16465 return error_mark_node;
16469 return groktypename (&type_specifier_seq, abstract_declarator,
16473 static tree cp_parser_type_id (cp_parser *parser)
16475 return cp_parser_type_id_1 (parser, false, false);
16478 static tree cp_parser_template_type_arg (cp_parser *parser)
16481 const char *saved_message = parser->type_definition_forbidden_message;
16482 parser->type_definition_forbidden_message
16483 = G_("types may not be defined in template arguments");
16484 r = cp_parser_type_id_1 (parser, true, false);
16485 parser->type_definition_forbidden_message = saved_message;
16489 static tree cp_parser_trailing_type_id (cp_parser *parser)
16491 return cp_parser_type_id_1 (parser, false, true);
16494 /* Parse a type-specifier-seq.
16496 type-specifier-seq:
16497 type-specifier type-specifier-seq [opt]
16501 type-specifier-seq:
16502 attributes type-specifier-seq [opt]
16504 If IS_DECLARATION is true, we are at the start of a "condition" or
16505 exception-declaration, so we might be followed by a declarator-id.
16507 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16508 i.e. we've just seen "->".
16510 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16513 cp_parser_type_specifier_seq (cp_parser* parser,
16514 bool is_declaration,
16515 bool is_trailing_return,
16516 cp_decl_specifier_seq *type_specifier_seq)
16518 bool seen_type_specifier = false;
16519 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16520 cp_token *start_token = NULL;
16522 /* Clear the TYPE_SPECIFIER_SEQ. */
16523 clear_decl_specs (type_specifier_seq);
16525 /* In the context of a trailing return type, enum E { } is an
16526 elaborated-type-specifier followed by a function-body, not an
16528 if (is_trailing_return)
16529 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16531 /* Parse the type-specifiers and attributes. */
16534 tree type_specifier;
16535 bool is_cv_qualifier;
16537 /* Check for attributes first. */
16538 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16540 type_specifier_seq->attributes =
16541 chainon (type_specifier_seq->attributes,
16542 cp_parser_attributes_opt (parser));
16546 /* record the token of the beginning of the type specifier seq,
16547 for error reporting purposes*/
16549 start_token = cp_lexer_peek_token (parser->lexer);
16551 /* Look for the type-specifier. */
16552 type_specifier = cp_parser_type_specifier (parser,
16554 type_specifier_seq,
16555 /*is_declaration=*/false,
16558 if (!type_specifier)
16560 /* If the first type-specifier could not be found, this is not a
16561 type-specifier-seq at all. */
16562 if (!seen_type_specifier)
16564 cp_parser_error (parser, "expected type-specifier");
16565 type_specifier_seq->type = error_mark_node;
16568 /* If subsequent type-specifiers could not be found, the
16569 type-specifier-seq is complete. */
16573 seen_type_specifier = true;
16574 /* The standard says that a condition can be:
16576 type-specifier-seq declarator = assignment-expression
16583 we should treat the "S" as a declarator, not as a
16584 type-specifier. The standard doesn't say that explicitly for
16585 type-specifier-seq, but it does say that for
16586 decl-specifier-seq in an ordinary declaration. Perhaps it
16587 would be clearer just to allow a decl-specifier-seq here, and
16588 then add a semantic restriction that if any decl-specifiers
16589 that are not type-specifiers appear, the program is invalid. */
16590 if (is_declaration && !is_cv_qualifier)
16591 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16594 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16597 /* Parse a parameter-declaration-clause.
16599 parameter-declaration-clause:
16600 parameter-declaration-list [opt] ... [opt]
16601 parameter-declaration-list , ...
16603 Returns a representation for the parameter declarations. A return
16604 value of NULL indicates a parameter-declaration-clause consisting
16605 only of an ellipsis. */
16608 cp_parser_parameter_declaration_clause (cp_parser* parser)
16615 /* Peek at the next token. */
16616 token = cp_lexer_peek_token (parser->lexer);
16617 /* Check for trivial parameter-declaration-clauses. */
16618 if (token->type == CPP_ELLIPSIS)
16620 /* Consume the `...' token. */
16621 cp_lexer_consume_token (parser->lexer);
16624 else if (token->type == CPP_CLOSE_PAREN)
16625 /* There are no parameters. */
16627 #ifndef NO_IMPLICIT_EXTERN_C
16628 if (in_system_header && current_class_type == NULL
16629 && current_lang_name == lang_name_c)
16633 return void_list_node;
16635 /* Check for `(void)', too, which is a special case. */
16636 else if (token->keyword == RID_VOID
16637 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16638 == CPP_CLOSE_PAREN))
16640 /* Consume the `void' token. */
16641 cp_lexer_consume_token (parser->lexer);
16642 /* There are no parameters. */
16643 return void_list_node;
16646 /* Parse the parameter-declaration-list. */
16647 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16648 /* If a parse error occurred while parsing the
16649 parameter-declaration-list, then the entire
16650 parameter-declaration-clause is erroneous. */
16654 /* Peek at the next token. */
16655 token = cp_lexer_peek_token (parser->lexer);
16656 /* If it's a `,', the clause should terminate with an ellipsis. */
16657 if (token->type == CPP_COMMA)
16659 /* Consume the `,'. */
16660 cp_lexer_consume_token (parser->lexer);
16661 /* Expect an ellipsis. */
16663 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16665 /* It might also be `...' if the optional trailing `,' was
16667 else if (token->type == CPP_ELLIPSIS)
16669 /* Consume the `...' token. */
16670 cp_lexer_consume_token (parser->lexer);
16671 /* And remember that we saw it. */
16675 ellipsis_p = false;
16677 /* Finish the parameter list. */
16679 parameters = chainon (parameters, void_list_node);
16684 /* Parse a parameter-declaration-list.
16686 parameter-declaration-list:
16687 parameter-declaration
16688 parameter-declaration-list , parameter-declaration
16690 Returns a representation of the parameter-declaration-list, as for
16691 cp_parser_parameter_declaration_clause. However, the
16692 `void_list_node' is never appended to the list. Upon return,
16693 *IS_ERROR will be true iff an error occurred. */
16696 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16698 tree parameters = NULL_TREE;
16699 tree *tail = ¶meters;
16700 bool saved_in_unbraced_linkage_specification_p;
16703 /* Assume all will go well. */
16705 /* The special considerations that apply to a function within an
16706 unbraced linkage specifications do not apply to the parameters
16707 to the function. */
16708 saved_in_unbraced_linkage_specification_p
16709 = parser->in_unbraced_linkage_specification_p;
16710 parser->in_unbraced_linkage_specification_p = false;
16712 /* Look for more parameters. */
16715 cp_parameter_declarator *parameter;
16716 tree decl = error_mark_node;
16717 bool parenthesized_p = false;
16718 /* Parse the parameter. */
16720 = cp_parser_parameter_declaration (parser,
16721 /*template_parm_p=*/false,
16724 /* We don't know yet if the enclosing context is deprecated, so wait
16725 and warn in grokparms if appropriate. */
16726 deprecated_state = DEPRECATED_SUPPRESS;
16729 decl = grokdeclarator (parameter->declarator,
16730 ¶meter->decl_specifiers,
16732 parameter->default_argument != NULL_TREE,
16733 ¶meter->decl_specifiers.attributes);
16735 deprecated_state = DEPRECATED_NORMAL;
16737 /* If a parse error occurred parsing the parameter declaration,
16738 then the entire parameter-declaration-list is erroneous. */
16739 if (decl == error_mark_node)
16742 parameters = error_mark_node;
16746 if (parameter->decl_specifiers.attributes)
16747 cplus_decl_attributes (&decl,
16748 parameter->decl_specifiers.attributes,
16750 if (DECL_NAME (decl))
16751 decl = pushdecl (decl);
16753 if (decl != error_mark_node)
16755 retrofit_lang_decl (decl);
16756 DECL_PARM_INDEX (decl) = ++index;
16757 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16760 /* Add the new parameter to the list. */
16761 *tail = build_tree_list (parameter->default_argument, decl);
16762 tail = &TREE_CHAIN (*tail);
16764 /* Peek at the next token. */
16765 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16766 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16767 /* These are for Objective-C++ */
16768 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16769 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16770 /* The parameter-declaration-list is complete. */
16772 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16776 /* Peek at the next token. */
16777 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16778 /* If it's an ellipsis, then the list is complete. */
16779 if (token->type == CPP_ELLIPSIS)
16781 /* Otherwise, there must be more parameters. Consume the
16783 cp_lexer_consume_token (parser->lexer);
16784 /* When parsing something like:
16786 int i(float f, double d)
16788 we can tell after seeing the declaration for "f" that we
16789 are not looking at an initialization of a variable "i",
16790 but rather at the declaration of a function "i".
16792 Due to the fact that the parsing of template arguments
16793 (as specified to a template-id) requires backtracking we
16794 cannot use this technique when inside a template argument
16796 if (!parser->in_template_argument_list_p
16797 && !parser->in_type_id_in_expr_p
16798 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16799 /* However, a parameter-declaration of the form
16800 "foat(f)" (which is a valid declaration of a
16801 parameter "f") can also be interpreted as an
16802 expression (the conversion of "f" to "float"). */
16803 && !parenthesized_p)
16804 cp_parser_commit_to_tentative_parse (parser);
16808 cp_parser_error (parser, "expected %<,%> or %<...%>");
16809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16810 cp_parser_skip_to_closing_parenthesis (parser,
16811 /*recovering=*/true,
16812 /*or_comma=*/false,
16813 /*consume_paren=*/false);
16818 parser->in_unbraced_linkage_specification_p
16819 = saved_in_unbraced_linkage_specification_p;
16824 /* Parse a parameter declaration.
16826 parameter-declaration:
16827 decl-specifier-seq ... [opt] declarator
16828 decl-specifier-seq declarator = assignment-expression
16829 decl-specifier-seq ... [opt] abstract-declarator [opt]
16830 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16832 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16833 declares a template parameter. (In that case, a non-nested `>'
16834 token encountered during the parsing of the assignment-expression
16835 is not interpreted as a greater-than operator.)
16837 Returns a representation of the parameter, or NULL if an error
16838 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16839 true iff the declarator is of the form "(p)". */
16841 static cp_parameter_declarator *
16842 cp_parser_parameter_declaration (cp_parser *parser,
16843 bool template_parm_p,
16844 bool *parenthesized_p)
16846 int declares_class_or_enum;
16847 cp_decl_specifier_seq decl_specifiers;
16848 cp_declarator *declarator;
16849 tree default_argument;
16850 cp_token *token = NULL, *declarator_token_start = NULL;
16851 const char *saved_message;
16853 /* In a template parameter, `>' is not an operator.
16857 When parsing a default template-argument for a non-type
16858 template-parameter, the first non-nested `>' is taken as the end
16859 of the template parameter-list rather than a greater-than
16862 /* Type definitions may not appear in parameter types. */
16863 saved_message = parser->type_definition_forbidden_message;
16864 parser->type_definition_forbidden_message
16865 = G_("types may not be defined in parameter types");
16867 /* Parse the declaration-specifiers. */
16868 cp_parser_decl_specifier_seq (parser,
16869 CP_PARSER_FLAGS_NONE,
16871 &declares_class_or_enum);
16873 /* Complain about missing 'typename' or other invalid type names. */
16874 if (!decl_specifiers.any_type_specifiers_p)
16875 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16877 /* If an error occurred, there's no reason to attempt to parse the
16878 rest of the declaration. */
16879 if (cp_parser_error_occurred (parser))
16881 parser->type_definition_forbidden_message = saved_message;
16885 /* Peek at the next token. */
16886 token = cp_lexer_peek_token (parser->lexer);
16888 /* If the next token is a `)', `,', `=', `>', or `...', then there
16889 is no declarator. However, when variadic templates are enabled,
16890 there may be a declarator following `...'. */
16891 if (token->type == CPP_CLOSE_PAREN
16892 || token->type == CPP_COMMA
16893 || token->type == CPP_EQ
16894 || token->type == CPP_GREATER)
16897 if (parenthesized_p)
16898 *parenthesized_p = false;
16900 /* Otherwise, there should be a declarator. */
16903 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16904 parser->default_arg_ok_p = false;
16906 /* After seeing a decl-specifier-seq, if the next token is not a
16907 "(", there is no possibility that the code is a valid
16908 expression. Therefore, if parsing tentatively, we commit at
16910 if (!parser->in_template_argument_list_p
16911 /* In an expression context, having seen:
16915 we cannot be sure whether we are looking at a
16916 function-type (taking a "char" as a parameter) or a cast
16917 of some object of type "char" to "int". */
16918 && !parser->in_type_id_in_expr_p
16919 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16920 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16921 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16922 cp_parser_commit_to_tentative_parse (parser);
16923 /* Parse the declarator. */
16924 declarator_token_start = token;
16925 declarator = cp_parser_declarator (parser,
16926 CP_PARSER_DECLARATOR_EITHER,
16927 /*ctor_dtor_or_conv_p=*/NULL,
16929 /*member_p=*/false);
16930 parser->default_arg_ok_p = saved_default_arg_ok_p;
16931 /* After the declarator, allow more attributes. */
16932 decl_specifiers.attributes
16933 = chainon (decl_specifiers.attributes,
16934 cp_parser_attributes_opt (parser));
16937 /* If the next token is an ellipsis, and we have not seen a
16938 declarator name, and the type of the declarator contains parameter
16939 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16940 a parameter pack expansion expression. Otherwise, leave the
16941 ellipsis for a C-style variadic function. */
16942 token = cp_lexer_peek_token (parser->lexer);
16943 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16945 tree type = decl_specifiers.type;
16947 if (type && DECL_P (type))
16948 type = TREE_TYPE (type);
16951 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16952 && declarator_can_be_parameter_pack (declarator)
16953 && (!declarator || !declarator->parameter_pack_p)
16954 && uses_parameter_packs (type))
16956 /* Consume the `...'. */
16957 cp_lexer_consume_token (parser->lexer);
16958 maybe_warn_variadic_templates ();
16960 /* Build a pack expansion type */
16962 declarator->parameter_pack_p = true;
16964 decl_specifiers.type = make_pack_expansion (type);
16968 /* The restriction on defining new types applies only to the type
16969 of the parameter, not to the default argument. */
16970 parser->type_definition_forbidden_message = saved_message;
16972 /* If the next token is `=', then process a default argument. */
16973 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16975 /* If we are defining a class, then the tokens that make up the
16976 default argument must be saved and processed later. */
16977 if (!template_parm_p && at_class_scope_p ()
16978 && TYPE_BEING_DEFINED (current_class_type)
16979 && !LAMBDA_TYPE_P (current_class_type))
16981 unsigned depth = 0;
16982 int maybe_template_id = 0;
16983 cp_token *first_token;
16986 /* Add tokens until we have processed the entire default
16987 argument. We add the range [first_token, token). */
16988 first_token = cp_lexer_peek_token (parser->lexer);
16993 /* Peek at the next token. */
16994 token = cp_lexer_peek_token (parser->lexer);
16995 /* What we do depends on what token we have. */
16996 switch (token->type)
16998 /* In valid code, a default argument must be
16999 immediately followed by a `,' `)', or `...'. */
17001 if (depth == 0 && maybe_template_id)
17003 /* If we've seen a '<', we might be in a
17004 template-argument-list. Until Core issue 325 is
17005 resolved, we don't know how this situation ought
17006 to be handled, so try to DTRT. We check whether
17007 what comes after the comma is a valid parameter
17008 declaration list. If it is, then the comma ends
17009 the default argument; otherwise the default
17010 argument continues. */
17011 bool error = false;
17014 /* Set ITALP so cp_parser_parameter_declaration_list
17015 doesn't decide to commit to this parse. */
17016 bool saved_italp = parser->in_template_argument_list_p;
17017 parser->in_template_argument_list_p = true;
17019 cp_parser_parse_tentatively (parser);
17020 cp_lexer_consume_token (parser->lexer);
17021 begin_scope (sk_function_parms, NULL_TREE);
17022 cp_parser_parameter_declaration_list (parser, &error);
17023 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17024 pop_binding (DECL_NAME (t), t);
17026 if (!cp_parser_error_occurred (parser) && !error)
17028 cp_parser_abort_tentative_parse (parser);
17030 parser->in_template_argument_list_p = saved_italp;
17033 case CPP_CLOSE_PAREN:
17035 /* If we run into a non-nested `;', `}', or `]',
17036 then the code is invalid -- but the default
17037 argument is certainly over. */
17038 case CPP_SEMICOLON:
17039 case CPP_CLOSE_BRACE:
17040 case CPP_CLOSE_SQUARE:
17043 /* Update DEPTH, if necessary. */
17044 else if (token->type == CPP_CLOSE_PAREN
17045 || token->type == CPP_CLOSE_BRACE
17046 || token->type == CPP_CLOSE_SQUARE)
17050 case CPP_OPEN_PAREN:
17051 case CPP_OPEN_SQUARE:
17052 case CPP_OPEN_BRACE:
17058 /* This might be the comparison operator, or it might
17059 start a template argument list. */
17060 ++maybe_template_id;
17064 if (cxx_dialect == cxx98)
17066 /* Fall through for C++0x, which treats the `>>'
17067 operator like two `>' tokens in certain
17073 /* This might be an operator, or it might close a
17074 template argument list. But if a previous '<'
17075 started a template argument list, this will have
17076 closed it, so we can't be in one anymore. */
17077 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17078 if (maybe_template_id < 0)
17079 maybe_template_id = 0;
17083 /* If we run out of tokens, issue an error message. */
17085 case CPP_PRAGMA_EOL:
17086 error_at (token->location, "file ends in default argument");
17092 /* In these cases, we should look for template-ids.
17093 For example, if the default argument is
17094 `X<int, double>()', we need to do name lookup to
17095 figure out whether or not `X' is a template; if
17096 so, the `,' does not end the default argument.
17098 That is not yet done. */
17105 /* If we've reached the end, stop. */
17109 /* Add the token to the token block. */
17110 token = cp_lexer_consume_token (parser->lexer);
17113 /* Create a DEFAULT_ARG to represent the unparsed default
17115 default_argument = make_node (DEFAULT_ARG);
17116 DEFARG_TOKENS (default_argument)
17117 = cp_token_cache_new (first_token, token);
17118 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17120 /* Outside of a class definition, we can just parse the
17121 assignment-expression. */
17124 token = cp_lexer_peek_token (parser->lexer);
17126 = cp_parser_default_argument (parser, template_parm_p);
17129 if (!parser->default_arg_ok_p)
17131 if (flag_permissive)
17132 warning (0, "deprecated use of default argument for parameter of non-function");
17135 error_at (token->location,
17136 "default arguments are only "
17137 "permitted for function parameters");
17138 default_argument = NULL_TREE;
17141 else if ((declarator && declarator->parameter_pack_p)
17142 || (decl_specifiers.type
17143 && PACK_EXPANSION_P (decl_specifiers.type)))
17145 /* Find the name of the parameter pack. */
17146 cp_declarator *id_declarator = declarator;
17147 while (id_declarator && id_declarator->kind != cdk_id)
17148 id_declarator = id_declarator->declarator;
17150 if (id_declarator && id_declarator->kind == cdk_id)
17151 error_at (declarator_token_start->location,
17153 ? G_("template parameter pack %qD "
17154 "cannot have a default argument")
17155 : G_("parameter pack %qD cannot have "
17156 "a default argument"),
17157 id_declarator->u.id.unqualified_name);
17159 error_at (declarator_token_start->location,
17161 ? G_("template parameter pack cannot have "
17162 "a default argument")
17163 : G_("parameter pack cannot have a "
17164 "default argument"));
17166 default_argument = NULL_TREE;
17170 default_argument = NULL_TREE;
17172 return make_parameter_declarator (&decl_specifiers,
17177 /* Parse a default argument and return it.
17179 TEMPLATE_PARM_P is true if this is a default argument for a
17180 non-type template parameter. */
17182 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17184 tree default_argument = NULL_TREE;
17185 bool saved_greater_than_is_operator_p;
17186 bool saved_local_variables_forbidden_p;
17187 bool non_constant_p, is_direct_init;
17189 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17191 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17192 parser->greater_than_is_operator_p = !template_parm_p;
17193 /* Local variable names (and the `this' keyword) may not
17194 appear in a default argument. */
17195 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17196 parser->local_variables_forbidden_p = true;
17197 /* Parse the assignment-expression. */
17198 if (template_parm_p)
17199 push_deferring_access_checks (dk_no_deferred);
17201 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17202 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17203 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17204 if (template_parm_p)
17205 pop_deferring_access_checks ();
17206 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17207 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17209 return default_argument;
17212 /* Parse a function-body.
17215 compound_statement */
17218 cp_parser_function_body (cp_parser *parser)
17220 cp_parser_compound_statement (parser, NULL, false, true);
17223 /* Parse a ctor-initializer-opt followed by a function-body. Return
17224 true if a ctor-initializer was present. */
17227 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17230 bool ctor_initializer_p;
17231 const bool check_body_p =
17232 DECL_CONSTRUCTOR_P (current_function_decl)
17233 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17236 /* Begin the function body. */
17237 body = begin_function_body ();
17238 /* Parse the optional ctor-initializer. */
17239 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17241 /* If we're parsing a constexpr constructor definition, we need
17242 to check that the constructor body is indeed empty. However,
17243 before we get to cp_parser_function_body lot of junk has been
17244 generated, so we can't just check that we have an empty block.
17245 Rather we take a snapshot of the outermost block, and check whether
17246 cp_parser_function_body changed its state. */
17250 if (TREE_CODE (list) == BIND_EXPR)
17251 list = BIND_EXPR_BODY (list);
17252 if (TREE_CODE (list) == STATEMENT_LIST
17253 && STATEMENT_LIST_TAIL (list) != NULL)
17254 last = STATEMENT_LIST_TAIL (list)->stmt;
17256 /* Parse the function-body. */
17257 cp_parser_function_body (parser);
17259 check_constexpr_ctor_body (last, list);
17260 /* Finish the function body. */
17261 finish_function_body (body);
17263 return ctor_initializer_p;
17266 /* Parse an initializer.
17269 = initializer-clause
17270 ( expression-list )
17272 Returns an expression representing the initializer. If no
17273 initializer is present, NULL_TREE is returned.
17275 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17276 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17277 set to TRUE if there is no initializer present. If there is an
17278 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17279 is set to true; otherwise it is set to false. */
17282 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17283 bool* non_constant_p)
17288 /* Peek at the next token. */
17289 token = cp_lexer_peek_token (parser->lexer);
17291 /* Let our caller know whether or not this initializer was
17293 *is_direct_init = (token->type != CPP_EQ);
17294 /* Assume that the initializer is constant. */
17295 *non_constant_p = false;
17297 if (token->type == CPP_EQ)
17299 /* Consume the `='. */
17300 cp_lexer_consume_token (parser->lexer);
17301 /* Parse the initializer-clause. */
17302 init = cp_parser_initializer_clause (parser, non_constant_p);
17304 else if (token->type == CPP_OPEN_PAREN)
17307 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17309 /*allow_expansion_p=*/true,
17312 return error_mark_node;
17313 init = build_tree_list_vec (vec);
17314 release_tree_vector (vec);
17316 else if (token->type == CPP_OPEN_BRACE)
17318 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17319 init = cp_parser_braced_list (parser, non_constant_p);
17320 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17324 /* Anything else is an error. */
17325 cp_parser_error (parser, "expected initializer");
17326 init = error_mark_node;
17332 /* Parse an initializer-clause.
17334 initializer-clause:
17335 assignment-expression
17338 Returns an expression representing the initializer.
17340 If the `assignment-expression' production is used the value
17341 returned is simply a representation for the expression.
17343 Otherwise, calls cp_parser_braced_list. */
17346 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17350 /* Assume the expression is constant. */
17351 *non_constant_p = false;
17353 /* If it is not a `{', then we are looking at an
17354 assignment-expression. */
17355 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17358 = cp_parser_constant_expression (parser,
17359 /*allow_non_constant_p=*/true,
17363 initializer = cp_parser_braced_list (parser, non_constant_p);
17365 return initializer;
17368 /* Parse a brace-enclosed initializer list.
17371 { initializer-list , [opt] }
17374 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17375 the elements of the initializer-list (or NULL, if the last
17376 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17377 NULL_TREE. There is no way to detect whether or not the optional
17378 trailing `,' was provided. NON_CONSTANT_P is as for
17379 cp_parser_initializer. */
17382 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17386 /* Consume the `{' token. */
17387 cp_lexer_consume_token (parser->lexer);
17388 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17389 initializer = make_node (CONSTRUCTOR);
17390 /* If it's not a `}', then there is a non-trivial initializer. */
17391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17393 /* Parse the initializer list. */
17394 CONSTRUCTOR_ELTS (initializer)
17395 = cp_parser_initializer_list (parser, non_constant_p);
17396 /* A trailing `,' token is allowed. */
17397 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17398 cp_lexer_consume_token (parser->lexer);
17400 /* Now, there should be a trailing `}'. */
17401 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17402 TREE_TYPE (initializer) = init_list_type_node;
17403 return initializer;
17406 /* Parse an initializer-list.
17409 initializer-clause ... [opt]
17410 initializer-list , initializer-clause ... [opt]
17415 designation initializer-clause ...[opt]
17416 initializer-list , designation initializer-clause ...[opt]
17421 [ constant-expression ] =
17423 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17424 for the initializer. If the INDEX of the elt is non-NULL, it is the
17425 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17426 as for cp_parser_initializer. */
17428 static VEC(constructor_elt,gc) *
17429 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17431 VEC(constructor_elt,gc) *v = NULL;
17433 /* Assume all of the expressions are constant. */
17434 *non_constant_p = false;
17436 /* Parse the rest of the list. */
17442 bool clause_non_constant_p;
17444 /* If the next token is an identifier and the following one is a
17445 colon, we are looking at the GNU designated-initializer
17447 if (cp_parser_allow_gnu_extensions_p (parser)
17448 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17449 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17451 /* Warn the user that they are using an extension. */
17452 pedwarn (input_location, OPT_pedantic,
17453 "ISO C++ does not allow designated initializers");
17454 /* Consume the identifier. */
17455 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17456 /* Consume the `:'. */
17457 cp_lexer_consume_token (parser->lexer);
17459 /* Also handle the C99 syntax, '. id ='. */
17460 else if (cp_parser_allow_gnu_extensions_p (parser)
17461 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17462 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17463 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17465 /* Warn the user that they are using an extension. */
17466 pedwarn (input_location, OPT_pedantic,
17467 "ISO C++ does not allow C99 designated initializers");
17468 /* Consume the `.'. */
17469 cp_lexer_consume_token (parser->lexer);
17470 /* Consume the identifier. */
17471 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17472 /* Consume the `='. */
17473 cp_lexer_consume_token (parser->lexer);
17475 /* Also handle C99 array designators, '[ const ] ='. */
17476 else if (cp_parser_allow_gnu_extensions_p (parser)
17477 && !c_dialect_objc ()
17478 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17480 cp_lexer_consume_token (parser->lexer);
17481 designator = cp_parser_constant_expression (parser, false, NULL);
17482 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17483 cp_parser_require (parser, CPP_EQ, RT_EQ);
17486 designator = NULL_TREE;
17488 /* Parse the initializer. */
17489 initializer = cp_parser_initializer_clause (parser,
17490 &clause_non_constant_p);
17491 /* If any clause is non-constant, so is the entire initializer. */
17492 if (clause_non_constant_p)
17493 *non_constant_p = true;
17495 /* If we have an ellipsis, this is an initializer pack
17497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17499 /* Consume the `...'. */
17500 cp_lexer_consume_token (parser->lexer);
17502 /* Turn the initializer into an initializer expansion. */
17503 initializer = make_pack_expansion (initializer);
17506 /* Add it to the vector. */
17507 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17509 /* If the next token is not a comma, we have reached the end of
17511 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17514 /* Peek at the next token. */
17515 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17516 /* If the next token is a `}', then we're still done. An
17517 initializer-clause can have a trailing `,' after the
17518 initializer-list and before the closing `}'. */
17519 if (token->type == CPP_CLOSE_BRACE)
17522 /* Consume the `,' token. */
17523 cp_lexer_consume_token (parser->lexer);
17529 /* Classes [gram.class] */
17531 /* Parse a class-name.
17537 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17538 to indicate that names looked up in dependent types should be
17539 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17540 keyword has been used to indicate that the name that appears next
17541 is a template. TAG_TYPE indicates the explicit tag given before
17542 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17543 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17544 is the class being defined in a class-head.
17546 Returns the TYPE_DECL representing the class. */
17549 cp_parser_class_name (cp_parser *parser,
17550 bool typename_keyword_p,
17551 bool template_keyword_p,
17552 enum tag_types tag_type,
17553 bool check_dependency_p,
17555 bool is_declaration)
17561 tree identifier = NULL_TREE;
17563 /* All class-names start with an identifier. */
17564 token = cp_lexer_peek_token (parser->lexer);
17565 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17567 cp_parser_error (parser, "expected class-name");
17568 return error_mark_node;
17571 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17572 to a template-id, so we save it here. */
17573 scope = parser->scope;
17574 if (scope == error_mark_node)
17575 return error_mark_node;
17577 /* Any name names a type if we're following the `typename' keyword
17578 in a qualified name where the enclosing scope is type-dependent. */
17579 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17580 && dependent_type_p (scope));
17581 /* Handle the common case (an identifier, but not a template-id)
17583 if (token->type == CPP_NAME
17584 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17586 cp_token *identifier_token;
17589 /* Look for the identifier. */
17590 identifier_token = cp_lexer_peek_token (parser->lexer);
17591 ambiguous_p = identifier_token->ambiguous_p;
17592 identifier = cp_parser_identifier (parser);
17593 /* If the next token isn't an identifier, we are certainly not
17594 looking at a class-name. */
17595 if (identifier == error_mark_node)
17596 decl = error_mark_node;
17597 /* If we know this is a type-name, there's no need to look it
17599 else if (typename_p)
17603 tree ambiguous_decls;
17604 /* If we already know that this lookup is ambiguous, then
17605 we've already issued an error message; there's no reason
17609 cp_parser_simulate_error (parser);
17610 return error_mark_node;
17612 /* If the next token is a `::', then the name must be a type
17615 [basic.lookup.qual]
17617 During the lookup for a name preceding the :: scope
17618 resolution operator, object, function, and enumerator
17619 names are ignored. */
17620 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17621 tag_type = typename_type;
17622 /* Look up the name. */
17623 decl = cp_parser_lookup_name (parser, identifier,
17625 /*is_template=*/false,
17626 /*is_namespace=*/false,
17627 check_dependency_p,
17629 identifier_token->location);
17630 if (ambiguous_decls)
17632 if (cp_parser_parsing_tentatively (parser))
17633 cp_parser_simulate_error (parser);
17634 return error_mark_node;
17640 /* Try a template-id. */
17641 decl = cp_parser_template_id (parser, template_keyword_p,
17642 check_dependency_p,
17644 if (decl == error_mark_node)
17645 return error_mark_node;
17648 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17650 /* If this is a typename, create a TYPENAME_TYPE. */
17651 if (typename_p && decl != error_mark_node)
17653 decl = make_typename_type (scope, decl, typename_type,
17654 /*complain=*/tf_error);
17655 if (decl != error_mark_node)
17656 decl = TYPE_NAME (decl);
17659 /* Check to see that it is really the name of a class. */
17660 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17661 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17662 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17663 /* Situations like this:
17665 template <typename T> struct A {
17666 typename T::template X<int>::I i;
17669 are problematic. Is `T::template X<int>' a class-name? The
17670 standard does not seem to be definitive, but there is no other
17671 valid interpretation of the following `::'. Therefore, those
17672 names are considered class-names. */
17674 decl = make_typename_type (scope, decl, tag_type, tf_error);
17675 if (decl != error_mark_node)
17676 decl = TYPE_NAME (decl);
17678 else if (TREE_CODE (decl) != TYPE_DECL
17679 || TREE_TYPE (decl) == error_mark_node
17680 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17681 /* In Objective-C 2.0, a classname followed by '.' starts a
17682 dot-syntax expression, and it's not a type-name. */
17683 || (c_dialect_objc ()
17684 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17685 && objc_is_class_name (decl)))
17686 decl = error_mark_node;
17688 if (decl == error_mark_node)
17689 cp_parser_error (parser, "expected class-name");
17690 else if (identifier && !parser->scope)
17691 maybe_note_name_used_in_class (identifier, decl);
17696 /* Parse a class-specifier.
17699 class-head { member-specification [opt] }
17701 Returns the TREE_TYPE representing the class. */
17704 cp_parser_class_specifier_1 (cp_parser* parser)
17707 tree attributes = NULL_TREE;
17708 bool nested_name_specifier_p;
17709 unsigned saved_num_template_parameter_lists;
17710 bool saved_in_function_body;
17711 unsigned char in_statement;
17712 bool in_switch_statement_p;
17713 bool saved_in_unbraced_linkage_specification_p;
17714 tree old_scope = NULL_TREE;
17715 tree scope = NULL_TREE;
17717 cp_token *closing_brace;
17719 push_deferring_access_checks (dk_no_deferred);
17721 /* Parse the class-head. */
17722 type = cp_parser_class_head (parser,
17723 &nested_name_specifier_p,
17726 /* If the class-head was a semantic disaster, skip the entire body
17730 cp_parser_skip_to_end_of_block_or_statement (parser);
17731 pop_deferring_access_checks ();
17732 return error_mark_node;
17735 /* Look for the `{'. */
17736 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17738 pop_deferring_access_checks ();
17739 return error_mark_node;
17742 /* Process the base classes. If they're invalid, skip the
17743 entire class body. */
17744 if (!xref_basetypes (type, bases))
17746 /* Consuming the closing brace yields better error messages
17748 if (cp_parser_skip_to_closing_brace (parser))
17749 cp_lexer_consume_token (parser->lexer);
17750 pop_deferring_access_checks ();
17751 return error_mark_node;
17754 /* Issue an error message if type-definitions are forbidden here. */
17755 cp_parser_check_type_definition (parser);
17756 /* Remember that we are defining one more class. */
17757 ++parser->num_classes_being_defined;
17758 /* Inside the class, surrounding template-parameter-lists do not
17760 saved_num_template_parameter_lists
17761 = parser->num_template_parameter_lists;
17762 parser->num_template_parameter_lists = 0;
17763 /* We are not in a function body. */
17764 saved_in_function_body = parser->in_function_body;
17765 parser->in_function_body = false;
17766 /* Or in a loop. */
17767 in_statement = parser->in_statement;
17768 parser->in_statement = 0;
17769 /* Or in a switch. */
17770 in_switch_statement_p = parser->in_switch_statement_p;
17771 parser->in_switch_statement_p = false;
17772 /* We are not immediately inside an extern "lang" block. */
17773 saved_in_unbraced_linkage_specification_p
17774 = parser->in_unbraced_linkage_specification_p;
17775 parser->in_unbraced_linkage_specification_p = false;
17777 /* Start the class. */
17778 if (nested_name_specifier_p)
17780 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17781 old_scope = push_inner_scope (scope);
17783 type = begin_class_definition (type, attributes);
17785 if (type == error_mark_node)
17786 /* If the type is erroneous, skip the entire body of the class. */
17787 cp_parser_skip_to_closing_brace (parser);
17789 /* Parse the member-specification. */
17790 cp_parser_member_specification_opt (parser);
17792 /* Look for the trailing `}'. */
17793 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17794 /* Look for trailing attributes to apply to this class. */
17795 if (cp_parser_allow_gnu_extensions_p (parser))
17796 attributes = cp_parser_attributes_opt (parser);
17797 if (type != error_mark_node)
17798 type = finish_struct (type, attributes);
17799 if (nested_name_specifier_p)
17800 pop_inner_scope (old_scope, scope);
17802 /* We've finished a type definition. Check for the common syntax
17803 error of forgetting a semicolon after the definition. We need to
17804 be careful, as we can't just check for not-a-semicolon and be done
17805 with it; the user might have typed:
17807 class X { } c = ...;
17808 class X { } *p = ...;
17810 and so forth. Instead, enumerate all the possible tokens that
17811 might follow this production; if we don't see one of them, then
17812 complain and silently insert the semicolon. */
17814 cp_token *token = cp_lexer_peek_token (parser->lexer);
17815 bool want_semicolon = true;
17817 switch (token->type)
17820 case CPP_SEMICOLON:
17823 case CPP_OPEN_PAREN:
17824 case CPP_CLOSE_PAREN:
17826 want_semicolon = false;
17829 /* While it's legal for type qualifiers and storage class
17830 specifiers to follow type definitions in the grammar, only
17831 compiler testsuites contain code like that. Assume that if
17832 we see such code, then what we're really seeing is a case
17836 const <type> var = ...;
17841 static <type> func (...) ...
17843 i.e. the qualifier or specifier applies to the next
17844 declaration. To do so, however, we need to look ahead one
17845 more token to see if *that* token is a type specifier.
17847 This code could be improved to handle:
17850 static const <type> var = ...; */
17852 if (keyword_is_decl_specifier (token->keyword))
17854 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17856 /* Handling user-defined types here would be nice, but very
17859 = (lookahead->type == CPP_KEYWORD
17860 && keyword_begins_type_specifier (lookahead->keyword));
17867 /* If we don't have a type, then something is very wrong and we
17868 shouldn't try to do anything clever. Likewise for not seeing the
17870 if (closing_brace && TYPE_P (type) && want_semicolon)
17872 cp_token_position prev
17873 = cp_lexer_previous_token_position (parser->lexer);
17874 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17875 location_t loc = prev_token->location;
17877 if (CLASSTYPE_DECLARED_CLASS (type))
17878 error_at (loc, "expected %<;%> after class definition");
17879 else if (TREE_CODE (type) == RECORD_TYPE)
17880 error_at (loc, "expected %<;%> after struct definition");
17881 else if (TREE_CODE (type) == UNION_TYPE)
17882 error_at (loc, "expected %<;%> after union definition");
17884 gcc_unreachable ();
17886 /* Unget one token and smash it to look as though we encountered
17887 a semicolon in the input stream. */
17888 cp_lexer_set_token_position (parser->lexer, prev);
17889 token = cp_lexer_peek_token (parser->lexer);
17890 token->type = CPP_SEMICOLON;
17891 token->keyword = RID_MAX;
17895 /* If this class is not itself within the scope of another class,
17896 then we need to parse the bodies of all of the queued function
17897 definitions. Note that the queued functions defined in a class
17898 are not always processed immediately following the
17899 class-specifier for that class. Consider:
17902 struct B { void f() { sizeof (A); } };
17905 If `f' were processed before the processing of `A' were
17906 completed, there would be no way to compute the size of `A'.
17907 Note that the nesting we are interested in here is lexical --
17908 not the semantic nesting given by TYPE_CONTEXT. In particular,
17911 struct A { struct B; };
17912 struct A::B { void f() { } };
17914 there is no need to delay the parsing of `A::B::f'. */
17915 if (--parser->num_classes_being_defined == 0)
17918 tree class_type = NULL_TREE;
17919 tree pushed_scope = NULL_TREE;
17921 cp_default_arg_entry *e;
17922 tree save_ccp, save_ccr;
17924 /* In a first pass, parse default arguments to the functions.
17925 Then, in a second pass, parse the bodies of the functions.
17926 This two-phased approach handles cases like:
17934 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17938 /* If there are default arguments that have not yet been processed,
17939 take care of them now. */
17940 if (class_type != e->class_type)
17943 pop_scope (pushed_scope);
17944 class_type = e->class_type;
17945 pushed_scope = push_scope (class_type);
17947 /* Make sure that any template parameters are in scope. */
17948 maybe_begin_member_template_processing (decl);
17949 /* Parse the default argument expressions. */
17950 cp_parser_late_parsing_default_args (parser, decl);
17951 /* Remove any template parameters from the symbol table. */
17952 maybe_end_member_template_processing ();
17954 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17955 /* Now parse any NSDMIs. */
17956 save_ccp = current_class_ptr;
17957 save_ccr = current_class_ref;
17958 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
17960 if (class_type != DECL_CONTEXT (decl))
17963 pop_scope (pushed_scope);
17964 class_type = DECL_CONTEXT (decl);
17965 pushed_scope = push_scope (class_type);
17967 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
17968 cp_parser_late_parsing_nsdmi (parser, decl);
17970 VEC_truncate (tree, unparsed_nsdmis, 0);
17971 current_class_ptr = save_ccp;
17972 current_class_ref = save_ccr;
17974 pop_scope (pushed_scope);
17975 /* Now parse the body of the functions. */
17976 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
17977 cp_parser_late_parsing_for_member (parser, decl);
17978 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17981 /* Put back any saved access checks. */
17982 pop_deferring_access_checks ();
17984 /* Restore saved state. */
17985 parser->in_switch_statement_p = in_switch_statement_p;
17986 parser->in_statement = in_statement;
17987 parser->in_function_body = saved_in_function_body;
17988 parser->num_template_parameter_lists
17989 = saved_num_template_parameter_lists;
17990 parser->in_unbraced_linkage_specification_p
17991 = saved_in_unbraced_linkage_specification_p;
17997 cp_parser_class_specifier (cp_parser* parser)
18000 timevar_push (TV_PARSE_STRUCT);
18001 ret = cp_parser_class_specifier_1 (parser);
18002 timevar_pop (TV_PARSE_STRUCT);
18006 /* Parse a class-head.
18009 class-key identifier [opt] base-clause [opt]
18010 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18011 class-key nested-name-specifier [opt] template-id
18014 class-virt-specifier:
18018 class-key attributes identifier [opt] base-clause [opt]
18019 class-key attributes nested-name-specifier identifier base-clause [opt]
18020 class-key attributes nested-name-specifier [opt] template-id
18023 Upon return BASES is initialized to the list of base classes (or
18024 NULL, if there are none) in the same form returned by
18025 cp_parser_base_clause.
18027 Returns the TYPE of the indicated class. Sets
18028 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18029 involving a nested-name-specifier was used, and FALSE otherwise.
18031 Returns error_mark_node if this is not a class-head.
18033 Returns NULL_TREE if the class-head is syntactically valid, but
18034 semantically invalid in a way that means we should skip the entire
18035 body of the class. */
18038 cp_parser_class_head (cp_parser* parser,
18039 bool* nested_name_specifier_p,
18040 tree *attributes_p,
18043 tree nested_name_specifier;
18044 enum tag_types class_key;
18045 tree id = NULL_TREE;
18046 tree type = NULL_TREE;
18048 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18049 bool template_id_p = false;
18050 bool qualified_p = false;
18051 bool invalid_nested_name_p = false;
18052 bool invalid_explicit_specialization_p = false;
18053 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18054 tree pushed_scope = NULL_TREE;
18055 unsigned num_templates;
18056 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18057 /* Assume no nested-name-specifier will be present. */
18058 *nested_name_specifier_p = false;
18059 /* Assume no template parameter lists will be used in defining the
18062 parser->colon_corrects_to_scope_p = false;
18064 *bases = NULL_TREE;
18066 /* Look for the class-key. */
18067 class_key = cp_parser_class_key (parser);
18068 if (class_key == none_type)
18069 return error_mark_node;
18071 /* Parse the attributes. */
18072 attributes = cp_parser_attributes_opt (parser);
18074 /* If the next token is `::', that is invalid -- but sometimes
18075 people do try to write:
18079 Handle this gracefully by accepting the extra qualifier, and then
18080 issuing an error about it later if this really is a
18081 class-head. If it turns out just to be an elaborated type
18082 specifier, remain silent. */
18083 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18084 qualified_p = true;
18086 push_deferring_access_checks (dk_no_check);
18088 /* Determine the name of the class. Begin by looking for an
18089 optional nested-name-specifier. */
18090 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18091 nested_name_specifier
18092 = cp_parser_nested_name_specifier_opt (parser,
18093 /*typename_keyword_p=*/false,
18094 /*check_dependency_p=*/false,
18096 /*is_declaration=*/false);
18097 /* If there was a nested-name-specifier, then there *must* be an
18099 if (nested_name_specifier)
18101 type_start_token = cp_lexer_peek_token (parser->lexer);
18102 /* Although the grammar says `identifier', it really means
18103 `class-name' or `template-name'. You are only allowed to
18104 define a class that has already been declared with this
18107 The proposed resolution for Core Issue 180 says that wherever
18108 you see `class T::X' you should treat `X' as a type-name.
18110 It is OK to define an inaccessible class; for example:
18112 class A { class B; };
18115 We do not know if we will see a class-name, or a
18116 template-name. We look for a class-name first, in case the
18117 class-name is a template-id; if we looked for the
18118 template-name first we would stop after the template-name. */
18119 cp_parser_parse_tentatively (parser);
18120 type = cp_parser_class_name (parser,
18121 /*typename_keyword_p=*/false,
18122 /*template_keyword_p=*/false,
18124 /*check_dependency_p=*/false,
18125 /*class_head_p=*/true,
18126 /*is_declaration=*/false);
18127 /* If that didn't work, ignore the nested-name-specifier. */
18128 if (!cp_parser_parse_definitely (parser))
18130 invalid_nested_name_p = true;
18131 type_start_token = cp_lexer_peek_token (parser->lexer);
18132 id = cp_parser_identifier (parser);
18133 if (id == error_mark_node)
18136 /* If we could not find a corresponding TYPE, treat this
18137 declaration like an unqualified declaration. */
18138 if (type == error_mark_node)
18139 nested_name_specifier = NULL_TREE;
18140 /* Otherwise, count the number of templates used in TYPE and its
18141 containing scopes. */
18146 for (scope = TREE_TYPE (type);
18147 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18148 scope = (TYPE_P (scope)
18149 ? TYPE_CONTEXT (scope)
18150 : DECL_CONTEXT (scope)))
18152 && CLASS_TYPE_P (scope)
18153 && CLASSTYPE_TEMPLATE_INFO (scope)
18154 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18155 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18159 /* Otherwise, the identifier is optional. */
18162 /* We don't know whether what comes next is a template-id,
18163 an identifier, or nothing at all. */
18164 cp_parser_parse_tentatively (parser);
18165 /* Check for a template-id. */
18166 type_start_token = cp_lexer_peek_token (parser->lexer);
18167 id = cp_parser_template_id (parser,
18168 /*template_keyword_p=*/false,
18169 /*check_dependency_p=*/true,
18170 /*is_declaration=*/true);
18171 /* If that didn't work, it could still be an identifier. */
18172 if (!cp_parser_parse_definitely (parser))
18174 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18176 type_start_token = cp_lexer_peek_token (parser->lexer);
18177 id = cp_parser_identifier (parser);
18184 template_id_p = true;
18189 pop_deferring_access_checks ();
18193 cp_parser_check_for_invalid_template_id (parser, id,
18194 type_start_token->location);
18196 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18198 /* If it's not a `:' or a `{' then we can't really be looking at a
18199 class-head, since a class-head only appears as part of a
18200 class-specifier. We have to detect this situation before calling
18201 xref_tag, since that has irreversible side-effects. */
18202 if (!cp_parser_next_token_starts_class_definition_p (parser))
18204 cp_parser_error (parser, "expected %<{%> or %<:%>");
18205 type = error_mark_node;
18209 /* At this point, we're going ahead with the class-specifier, even
18210 if some other problem occurs. */
18211 cp_parser_commit_to_tentative_parse (parser);
18212 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18214 cp_parser_error (parser,
18215 "cannot specify %<override%> for a class");
18216 type = error_mark_node;
18219 /* Issue the error about the overly-qualified name now. */
18222 cp_parser_error (parser,
18223 "global qualification of class name is invalid");
18224 type = error_mark_node;
18227 else if (invalid_nested_name_p)
18229 cp_parser_error (parser,
18230 "qualified name does not name a class");
18231 type = error_mark_node;
18234 else if (nested_name_specifier)
18238 /* Reject typedef-names in class heads. */
18239 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18241 error_at (type_start_token->location,
18242 "invalid class name in declaration of %qD",
18248 /* Figure out in what scope the declaration is being placed. */
18249 scope = current_scope ();
18250 /* If that scope does not contain the scope in which the
18251 class was originally declared, the program is invalid. */
18252 if (scope && !is_ancestor (scope, nested_name_specifier))
18254 if (at_namespace_scope_p ())
18255 error_at (type_start_token->location,
18256 "declaration of %qD in namespace %qD which does not "
18258 type, scope, nested_name_specifier);
18260 error_at (type_start_token->location,
18261 "declaration of %qD in %qD which does not enclose %qD",
18262 type, scope, nested_name_specifier);
18268 A declarator-id shall not be qualified except for the
18269 definition of a ... nested class outside of its class
18270 ... [or] the definition or explicit instantiation of a
18271 class member of a namespace outside of its namespace. */
18272 if (scope == nested_name_specifier)
18274 permerror (nested_name_specifier_token_start->location,
18275 "extra qualification not allowed");
18276 nested_name_specifier = NULL_TREE;
18280 /* An explicit-specialization must be preceded by "template <>". If
18281 it is not, try to recover gracefully. */
18282 if (at_namespace_scope_p ()
18283 && parser->num_template_parameter_lists == 0
18286 error_at (type_start_token->location,
18287 "an explicit specialization must be preceded by %<template <>%>");
18288 invalid_explicit_specialization_p = true;
18289 /* Take the same action that would have been taken by
18290 cp_parser_explicit_specialization. */
18291 ++parser->num_template_parameter_lists;
18292 begin_specialization ();
18294 /* There must be no "return" statements between this point and the
18295 end of this function; set "type "to the correct return value and
18296 use "goto done;" to return. */
18297 /* Make sure that the right number of template parameters were
18299 if (!cp_parser_check_template_parameters (parser, num_templates,
18300 type_start_token->location,
18301 /*declarator=*/NULL))
18303 /* If something went wrong, there is no point in even trying to
18304 process the class-definition. */
18309 /* Look up the type. */
18312 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18313 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18314 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18316 error_at (type_start_token->location,
18317 "function template %qD redeclared as a class template", id);
18318 type = error_mark_node;
18322 type = TREE_TYPE (id);
18323 type = maybe_process_partial_specialization (type);
18325 if (nested_name_specifier)
18326 pushed_scope = push_scope (nested_name_specifier);
18328 else if (nested_name_specifier)
18334 template <typename T> struct S { struct T };
18335 template <typename T> struct S<T>::T { };
18337 we will get a TYPENAME_TYPE when processing the definition of
18338 `S::T'. We need to resolve it to the actual type before we
18339 try to define it. */
18340 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18342 class_type = resolve_typename_type (TREE_TYPE (type),
18343 /*only_current_p=*/false);
18344 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18345 type = TYPE_NAME (class_type);
18348 cp_parser_error (parser, "could not resolve typename type");
18349 type = error_mark_node;
18353 if (maybe_process_partial_specialization (TREE_TYPE (type))
18354 == error_mark_node)
18360 class_type = current_class_type;
18361 /* Enter the scope indicated by the nested-name-specifier. */
18362 pushed_scope = push_scope (nested_name_specifier);
18363 /* Get the canonical version of this type. */
18364 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18365 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18366 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18368 type = push_template_decl (type);
18369 if (type == error_mark_node)
18376 type = TREE_TYPE (type);
18377 *nested_name_specifier_p = true;
18379 else /* The name is not a nested name. */
18381 /* If the class was unnamed, create a dummy name. */
18383 id = make_anon_name ();
18384 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18385 parser->num_template_parameter_lists);
18388 /* Indicate whether this class was declared as a `class' or as a
18390 if (TREE_CODE (type) == RECORD_TYPE)
18391 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18392 cp_parser_check_class_key (class_key, type);
18394 /* If this type was already complete, and we see another definition,
18395 that's an error. */
18396 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18398 error_at (type_start_token->location, "redefinition of %q#T",
18400 error_at (type_start_token->location, "previous definition of %q+#T",
18405 else if (type == error_mark_node)
18408 /* We will have entered the scope containing the class; the names of
18409 base classes should be looked up in that context. For example:
18411 struct A { struct B {}; struct C; };
18412 struct A::C : B {};
18416 /* Get the list of base-classes, if there is one. */
18417 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18418 *bases = cp_parser_base_clause (parser);
18421 /* Leave the scope given by the nested-name-specifier. We will
18422 enter the class scope itself while processing the members. */
18424 pop_scope (pushed_scope);
18426 if (invalid_explicit_specialization_p)
18428 end_specialization ();
18429 --parser->num_template_parameter_lists;
18433 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18434 *attributes_p = attributes;
18435 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18436 CLASSTYPE_FINAL (type) = 1;
18438 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18442 /* Parse a class-key.
18449 Returns the kind of class-key specified, or none_type to indicate
18452 static enum tag_types
18453 cp_parser_class_key (cp_parser* parser)
18456 enum tag_types tag_type;
18458 /* Look for the class-key. */
18459 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18463 /* Check to see if the TOKEN is a class-key. */
18464 tag_type = cp_parser_token_is_class_key (token);
18466 cp_parser_error (parser, "expected class-key");
18470 /* Parse an (optional) member-specification.
18472 member-specification:
18473 member-declaration member-specification [opt]
18474 access-specifier : member-specification [opt] */
18477 cp_parser_member_specification_opt (cp_parser* parser)
18484 /* Peek at the next token. */
18485 token = cp_lexer_peek_token (parser->lexer);
18486 /* If it's a `}', or EOF then we've seen all the members. */
18487 if (token->type == CPP_CLOSE_BRACE
18488 || token->type == CPP_EOF
18489 || token->type == CPP_PRAGMA_EOL)
18492 /* See if this token is a keyword. */
18493 keyword = token->keyword;
18497 case RID_PROTECTED:
18499 /* Consume the access-specifier. */
18500 cp_lexer_consume_token (parser->lexer);
18501 /* Remember which access-specifier is active. */
18502 current_access_specifier = token->u.value;
18503 /* Look for the `:'. */
18504 cp_parser_require (parser, CPP_COLON, RT_COLON);
18508 /* Accept #pragmas at class scope. */
18509 if (token->type == CPP_PRAGMA)
18511 cp_parser_pragma (parser, pragma_external);
18515 /* Otherwise, the next construction must be a
18516 member-declaration. */
18517 cp_parser_member_declaration (parser);
18522 /* Parse a member-declaration.
18524 member-declaration:
18525 decl-specifier-seq [opt] member-declarator-list [opt] ;
18526 function-definition ; [opt]
18527 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18529 template-declaration
18531 member-declarator-list:
18533 member-declarator-list , member-declarator
18536 declarator pure-specifier [opt]
18537 declarator constant-initializer [opt]
18538 identifier [opt] : constant-expression
18542 member-declaration:
18543 __extension__ member-declaration
18546 declarator attributes [opt] pure-specifier [opt]
18547 declarator attributes [opt] constant-initializer [opt]
18548 identifier [opt] attributes [opt] : constant-expression
18552 member-declaration:
18553 static_assert-declaration */
18556 cp_parser_member_declaration (cp_parser* parser)
18558 cp_decl_specifier_seq decl_specifiers;
18559 tree prefix_attributes;
18561 int declares_class_or_enum;
18563 cp_token *token = NULL;
18564 cp_token *decl_spec_token_start = NULL;
18565 cp_token *initializer_token_start = NULL;
18566 int saved_pedantic;
18567 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18569 /* Check for the `__extension__' keyword. */
18570 if (cp_parser_extension_opt (parser, &saved_pedantic))
18573 cp_parser_member_declaration (parser);
18574 /* Restore the old value of the PEDANTIC flag. */
18575 pedantic = saved_pedantic;
18580 /* Check for a template-declaration. */
18581 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18583 /* An explicit specialization here is an error condition, and we
18584 expect the specialization handler to detect and report this. */
18585 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18586 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18587 cp_parser_explicit_specialization (parser);
18589 cp_parser_template_declaration (parser, /*member_p=*/true);
18594 /* Check for a using-declaration. */
18595 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18597 /* Parse the using-declaration. */
18598 cp_parser_using_declaration (parser,
18599 /*access_declaration_p=*/false);
18603 /* Check for @defs. */
18604 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18607 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18608 ivar = ivar_chains;
18612 ivar = TREE_CHAIN (member);
18613 TREE_CHAIN (member) = NULL_TREE;
18614 finish_member_declaration (member);
18619 /* If the next token is `static_assert' we have a static assertion. */
18620 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18622 cp_parser_static_assert (parser, /*member_p=*/true);
18626 parser->colon_corrects_to_scope_p = false;
18628 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18631 /* Parse the decl-specifier-seq. */
18632 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18633 cp_parser_decl_specifier_seq (parser,
18634 CP_PARSER_FLAGS_OPTIONAL,
18636 &declares_class_or_enum);
18637 prefix_attributes = decl_specifiers.attributes;
18638 decl_specifiers.attributes = NULL_TREE;
18639 /* Check for an invalid type-name. */
18640 if (!decl_specifiers.any_type_specifiers_p
18641 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18643 /* If there is no declarator, then the decl-specifier-seq should
18645 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18647 /* If there was no decl-specifier-seq, and the next token is a
18648 `;', then we have something like:
18654 Each member-declaration shall declare at least one member
18655 name of the class. */
18656 if (!decl_specifiers.any_specifiers_p)
18658 cp_token *token = cp_lexer_peek_token (parser->lexer);
18659 if (!in_system_header_at (token->location))
18660 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18666 /* See if this declaration is a friend. */
18667 friend_p = cp_parser_friend_p (&decl_specifiers);
18668 /* If there were decl-specifiers, check to see if there was
18669 a class-declaration. */
18670 type = check_tag_decl (&decl_specifiers);
18671 /* Nested classes have already been added to the class, but
18672 a `friend' needs to be explicitly registered. */
18675 /* If the `friend' keyword was present, the friend must
18676 be introduced with a class-key. */
18677 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18678 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18679 "in C++03 a class-key must be used "
18680 "when declaring a friend");
18683 template <typename T> struct A {
18684 friend struct A<T>::B;
18687 A<T>::B will be represented by a TYPENAME_TYPE, and
18688 therefore not recognized by check_tag_decl. */
18691 type = decl_specifiers.type;
18692 if (type && TREE_CODE (type) == TYPE_DECL)
18693 type = TREE_TYPE (type);
18695 if (!type || !TYPE_P (type))
18696 error_at (decl_spec_token_start->location,
18697 "friend declaration does not name a class or "
18700 make_friend_class (current_class_type, type,
18701 /*complain=*/true);
18703 /* If there is no TYPE, an error message will already have
18705 else if (!type || type == error_mark_node)
18707 /* An anonymous aggregate has to be handled specially; such
18708 a declaration really declares a data member (with a
18709 particular type), as opposed to a nested class. */
18710 else if (ANON_AGGR_TYPE_P (type))
18712 /* Remove constructors and such from TYPE, now that we
18713 know it is an anonymous aggregate. */
18714 fixup_anonymous_aggr (type);
18715 /* And make the corresponding data member. */
18716 decl = build_decl (decl_spec_token_start->location,
18717 FIELD_DECL, NULL_TREE, type);
18718 /* Add it to the class. */
18719 finish_member_declaration (decl);
18722 cp_parser_check_access_in_redeclaration
18724 decl_spec_token_start->location);
18729 bool assume_semicolon = false;
18731 /* See if these declarations will be friends. */
18732 friend_p = cp_parser_friend_p (&decl_specifiers);
18734 /* Keep going until we hit the `;' at the end of the
18736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18738 tree attributes = NULL_TREE;
18739 tree first_attribute;
18741 /* Peek at the next token. */
18742 token = cp_lexer_peek_token (parser->lexer);
18744 /* Check for a bitfield declaration. */
18745 if (token->type == CPP_COLON
18746 || (token->type == CPP_NAME
18747 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18753 /* Get the name of the bitfield. Note that we cannot just
18754 check TOKEN here because it may have been invalidated by
18755 the call to cp_lexer_peek_nth_token above. */
18756 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18757 identifier = cp_parser_identifier (parser);
18759 identifier = NULL_TREE;
18761 /* Consume the `:' token. */
18762 cp_lexer_consume_token (parser->lexer);
18763 /* Get the width of the bitfield. */
18765 = cp_parser_constant_expression (parser,
18766 /*allow_non_constant=*/false,
18769 /* Look for attributes that apply to the bitfield. */
18770 attributes = cp_parser_attributes_opt (parser);
18771 /* Remember which attributes are prefix attributes and
18773 first_attribute = attributes;
18774 /* Combine the attributes. */
18775 attributes = chainon (prefix_attributes, attributes);
18777 /* Create the bitfield declaration. */
18778 decl = grokbitfield (identifier
18779 ? make_id_declarator (NULL_TREE,
18789 cp_declarator *declarator;
18791 tree asm_specification;
18792 int ctor_dtor_or_conv_p;
18794 /* Parse the declarator. */
18796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18797 &ctor_dtor_or_conv_p,
18798 /*parenthesized_p=*/NULL,
18799 /*member_p=*/true);
18801 /* If something went wrong parsing the declarator, make sure
18802 that we at least consume some tokens. */
18803 if (declarator == cp_error_declarator)
18805 /* Skip to the end of the statement. */
18806 cp_parser_skip_to_end_of_statement (parser);
18807 /* If the next token is not a semicolon, that is
18808 probably because we just skipped over the body of
18809 a function. So, we consume a semicolon if
18810 present, but do not issue an error message if it
18812 if (cp_lexer_next_token_is (parser->lexer,
18814 cp_lexer_consume_token (parser->lexer);
18818 if (declares_class_or_enum & 2)
18819 cp_parser_check_for_definition_in_return_type
18820 (declarator, decl_specifiers.type,
18821 decl_specifiers.type_location);
18823 /* Look for an asm-specification. */
18824 asm_specification = cp_parser_asm_specification_opt (parser);
18825 /* Look for attributes that apply to the declaration. */
18826 attributes = cp_parser_attributes_opt (parser);
18827 /* Remember which attributes are prefix attributes and
18829 first_attribute = attributes;
18830 /* Combine the attributes. */
18831 attributes = chainon (prefix_attributes, attributes);
18833 /* If it's an `=', then we have a constant-initializer or a
18834 pure-specifier. It is not correct to parse the
18835 initializer before registering the member declaration
18836 since the member declaration should be in scope while
18837 its initializer is processed. However, the rest of the
18838 front end does not yet provide an interface that allows
18839 us to handle this correctly. */
18840 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18844 A pure-specifier shall be used only in the declaration of
18845 a virtual function.
18847 A member-declarator can contain a constant-initializer
18848 only if it declares a static member of integral or
18851 Therefore, if the DECLARATOR is for a function, we look
18852 for a pure-specifier; otherwise, we look for a
18853 constant-initializer. When we call `grokfield', it will
18854 perform more stringent semantics checks. */
18855 initializer_token_start = cp_lexer_peek_token (parser->lexer);
18856 if (function_declarator_p (declarator)
18857 || (decl_specifiers.type
18858 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18859 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18860 == FUNCTION_TYPE)))
18861 initializer = cp_parser_pure_specifier (parser);
18862 else if (decl_specifiers.storage_class != sc_static)
18863 initializer = cp_parser_save_nsdmi (parser);
18864 else if (cxx_dialect >= cxx0x)
18867 /* Don't require a constant rvalue in C++11, since we
18868 might want a reference constant. We'll enforce
18869 constancy later. */
18870 cp_lexer_consume_token (parser->lexer);
18871 /* Parse the initializer. */
18872 initializer = cp_parser_initializer_clause (parser,
18876 /* Parse the initializer. */
18877 initializer = cp_parser_constant_initializer (parser);
18879 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18880 && !function_declarator_p (declarator))
18883 if (decl_specifiers.storage_class != sc_static)
18884 initializer = cp_parser_save_nsdmi (parser);
18886 initializer = cp_parser_initializer (parser, &x, &x);
18888 /* Otherwise, there is no initializer. */
18890 initializer = NULL_TREE;
18892 /* See if we are probably looking at a function
18893 definition. We are certainly not looking at a
18894 member-declarator. Calling `grokfield' has
18895 side-effects, so we must not do it unless we are sure
18896 that we are looking at a member-declarator. */
18897 if (cp_parser_token_starts_function_definition_p
18898 (cp_lexer_peek_token (parser->lexer)))
18900 /* The grammar does not allow a pure-specifier to be
18901 used when a member function is defined. (It is
18902 possible that this fact is an oversight in the
18903 standard, since a pure function may be defined
18904 outside of the class-specifier. */
18906 error_at (initializer_token_start->location,
18907 "pure-specifier on function-definition");
18908 decl = cp_parser_save_member_function_body (parser,
18912 /* If the member was not a friend, declare it here. */
18914 finish_member_declaration (decl);
18915 /* Peek at the next token. */
18916 token = cp_lexer_peek_token (parser->lexer);
18917 /* If the next token is a semicolon, consume it. */
18918 if (token->type == CPP_SEMICOLON)
18919 cp_lexer_consume_token (parser->lexer);
18923 if (declarator->kind == cdk_function)
18924 declarator->id_loc = token->location;
18925 /* Create the declaration. */
18926 decl = grokfield (declarator, &decl_specifiers,
18927 initializer, /*init_const_expr_p=*/true,
18932 /* Reset PREFIX_ATTRIBUTES. */
18933 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18934 attributes = TREE_CHAIN (attributes);
18936 TREE_CHAIN (attributes) = NULL_TREE;
18938 /* If there is any qualification still in effect, clear it
18939 now; we will be starting fresh with the next declarator. */
18940 parser->scope = NULL_TREE;
18941 parser->qualifying_scope = NULL_TREE;
18942 parser->object_scope = NULL_TREE;
18943 /* If it's a `,', then there are more declarators. */
18944 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18945 cp_lexer_consume_token (parser->lexer);
18946 /* If the next token isn't a `;', then we have a parse error. */
18947 else if (cp_lexer_next_token_is_not (parser->lexer,
18950 /* The next token might be a ways away from where the
18951 actual semicolon is missing. Find the previous token
18952 and use that for our error position. */
18953 cp_token *token = cp_lexer_previous_token (parser->lexer);
18954 error_at (token->location,
18955 "expected %<;%> at end of member declaration");
18957 /* Assume that the user meant to provide a semicolon. If
18958 we were to cp_parser_skip_to_end_of_statement, we might
18959 skip to a semicolon inside a member function definition
18960 and issue nonsensical error messages. */
18961 assume_semicolon = true;
18966 /* Add DECL to the list of members. */
18968 finish_member_declaration (decl);
18970 if (TREE_CODE (decl) == FUNCTION_DECL)
18971 cp_parser_save_default_args (parser, decl);
18972 else if (TREE_CODE (decl) == FIELD_DECL
18973 && !DECL_C_BIT_FIELD (decl)
18974 && DECL_INITIAL (decl))
18975 /* Add DECL to the queue of NSDMI to be parsed later. */
18976 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
18979 if (assume_semicolon)
18984 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18986 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18989 /* Parse a pure-specifier.
18994 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18995 Otherwise, ERROR_MARK_NODE is returned. */
18998 cp_parser_pure_specifier (cp_parser* parser)
19002 /* Look for the `=' token. */
19003 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19004 return error_mark_node;
19005 /* Look for the `0' token. */
19006 token = cp_lexer_peek_token (parser->lexer);
19008 if (token->type == CPP_EOF
19009 || token->type == CPP_PRAGMA_EOL)
19010 return error_mark_node;
19012 cp_lexer_consume_token (parser->lexer);
19014 /* Accept = default or = delete in c++0x mode. */
19015 if (token->keyword == RID_DEFAULT
19016 || token->keyword == RID_DELETE)
19018 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19019 return token->u.value;
19022 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19023 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19025 cp_parser_error (parser,
19026 "invalid pure specifier (only %<= 0%> is allowed)");
19027 cp_parser_skip_to_end_of_statement (parser);
19028 return error_mark_node;
19030 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19032 error_at (token->location, "templates may not be %<virtual%>");
19033 return error_mark_node;
19036 return integer_zero_node;
19039 /* Parse a constant-initializer.
19041 constant-initializer:
19042 = constant-expression
19044 Returns a representation of the constant-expression. */
19047 cp_parser_constant_initializer (cp_parser* parser)
19049 /* Look for the `=' token. */
19050 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19051 return error_mark_node;
19053 /* It is invalid to write:
19055 struct S { static const int i = { 7 }; };
19058 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19060 cp_parser_error (parser,
19061 "a brace-enclosed initializer is not allowed here");
19062 /* Consume the opening brace. */
19063 cp_lexer_consume_token (parser->lexer);
19064 /* Skip the initializer. */
19065 cp_parser_skip_to_closing_brace (parser);
19066 /* Look for the trailing `}'. */
19067 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19069 return error_mark_node;
19072 return cp_parser_constant_expression (parser,
19073 /*allow_non_constant=*/false,
19077 /* Derived classes [gram.class.derived] */
19079 /* Parse a base-clause.
19082 : base-specifier-list
19084 base-specifier-list:
19085 base-specifier ... [opt]
19086 base-specifier-list , base-specifier ... [opt]
19088 Returns a TREE_LIST representing the base-classes, in the order in
19089 which they were declared. The representation of each node is as
19090 described by cp_parser_base_specifier.
19092 In the case that no bases are specified, this function will return
19093 NULL_TREE, not ERROR_MARK_NODE. */
19096 cp_parser_base_clause (cp_parser* parser)
19098 tree bases = NULL_TREE;
19100 /* Look for the `:' that begins the list. */
19101 cp_parser_require (parser, CPP_COLON, RT_COLON);
19103 /* Scan the base-specifier-list. */
19108 bool pack_expansion_p = false;
19110 /* Look for the base-specifier. */
19111 base = cp_parser_base_specifier (parser);
19112 /* Look for the (optional) ellipsis. */
19113 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19115 /* Consume the `...'. */
19116 cp_lexer_consume_token (parser->lexer);
19118 pack_expansion_p = true;
19121 /* Add BASE to the front of the list. */
19122 if (base && base != error_mark_node)
19124 if (pack_expansion_p)
19125 /* Make this a pack expansion type. */
19126 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19128 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19130 TREE_CHAIN (base) = bases;
19134 /* Peek at the next token. */
19135 token = cp_lexer_peek_token (parser->lexer);
19136 /* If it's not a comma, then the list is complete. */
19137 if (token->type != CPP_COMMA)
19139 /* Consume the `,'. */
19140 cp_lexer_consume_token (parser->lexer);
19143 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19144 base class had a qualified name. However, the next name that
19145 appears is certainly not qualified. */
19146 parser->scope = NULL_TREE;
19147 parser->qualifying_scope = NULL_TREE;
19148 parser->object_scope = NULL_TREE;
19150 return nreverse (bases);
19153 /* Parse a base-specifier.
19156 :: [opt] nested-name-specifier [opt] class-name
19157 virtual access-specifier [opt] :: [opt] nested-name-specifier
19159 access-specifier virtual [opt] :: [opt] nested-name-specifier
19162 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19163 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19164 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19165 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19168 cp_parser_base_specifier (cp_parser* parser)
19172 bool virtual_p = false;
19173 bool duplicate_virtual_error_issued_p = false;
19174 bool duplicate_access_error_issued_p = false;
19175 bool class_scope_p, template_p;
19176 tree access = access_default_node;
19179 /* Process the optional `virtual' and `access-specifier'. */
19182 /* Peek at the next token. */
19183 token = cp_lexer_peek_token (parser->lexer);
19184 /* Process `virtual'. */
19185 switch (token->keyword)
19188 /* If `virtual' appears more than once, issue an error. */
19189 if (virtual_p && !duplicate_virtual_error_issued_p)
19191 cp_parser_error (parser,
19192 "%<virtual%> specified more than once in base-specified");
19193 duplicate_virtual_error_issued_p = true;
19198 /* Consume the `virtual' token. */
19199 cp_lexer_consume_token (parser->lexer);
19204 case RID_PROTECTED:
19206 /* If more than one access specifier appears, issue an
19208 if (access != access_default_node
19209 && !duplicate_access_error_issued_p)
19211 cp_parser_error (parser,
19212 "more than one access specifier in base-specified");
19213 duplicate_access_error_issued_p = true;
19216 access = ridpointers[(int) token->keyword];
19218 /* Consume the access-specifier. */
19219 cp_lexer_consume_token (parser->lexer);
19228 /* It is not uncommon to see programs mechanically, erroneously, use
19229 the 'typename' keyword to denote (dependent) qualified types
19230 as base classes. */
19231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19233 token = cp_lexer_peek_token (parser->lexer);
19234 if (!processing_template_decl)
19235 error_at (token->location,
19236 "keyword %<typename%> not allowed outside of templates");
19238 error_at (token->location,
19239 "keyword %<typename%> not allowed in this context "
19240 "(the base class is implicitly a type)");
19241 cp_lexer_consume_token (parser->lexer);
19244 /* Look for the optional `::' operator. */
19245 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19246 /* Look for the nested-name-specifier. The simplest way to
19251 The keyword `typename' is not permitted in a base-specifier or
19252 mem-initializer; in these contexts a qualified name that
19253 depends on a template-parameter is implicitly assumed to be a
19256 is to pretend that we have seen the `typename' keyword at this
19258 cp_parser_nested_name_specifier_opt (parser,
19259 /*typename_keyword_p=*/true,
19260 /*check_dependency_p=*/true,
19262 /*is_declaration=*/true);
19263 /* If the base class is given by a qualified name, assume that names
19264 we see are type names or templates, as appropriate. */
19265 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19266 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19269 && cp_lexer_next_token_is_decltype (parser->lexer))
19270 /* DR 950 allows decltype as a base-specifier. */
19271 type = cp_parser_decltype (parser);
19274 /* Otherwise, look for the class-name. */
19275 type = cp_parser_class_name (parser,
19279 /*check_dependency_p=*/true,
19280 /*class_head_p=*/false,
19281 /*is_declaration=*/true);
19282 type = TREE_TYPE (type);
19285 if (type == error_mark_node)
19286 return error_mark_node;
19288 return finish_base_specifier (type, access, virtual_p);
19291 /* Exception handling [gram.exception] */
19293 /* Parse an (optional) exception-specification.
19295 exception-specification:
19296 throw ( type-id-list [opt] )
19298 Returns a TREE_LIST representing the exception-specification. The
19299 TREE_VALUE of each node is a type. */
19302 cp_parser_exception_specification_opt (cp_parser* parser)
19306 const char *saved_message;
19308 /* Peek at the next token. */
19309 token = cp_lexer_peek_token (parser->lexer);
19311 /* Is it a noexcept-specification? */
19312 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19315 cp_lexer_consume_token (parser->lexer);
19317 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19319 cp_lexer_consume_token (parser->lexer);
19321 /* Types may not be defined in an exception-specification. */
19322 saved_message = parser->type_definition_forbidden_message;
19323 parser->type_definition_forbidden_message
19324 = G_("types may not be defined in an exception-specification");
19326 expr = cp_parser_constant_expression (parser, false, NULL);
19328 /* Restore the saved message. */
19329 parser->type_definition_forbidden_message = saved_message;
19331 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19334 expr = boolean_true_node;
19336 return build_noexcept_spec (expr, tf_warning_or_error);
19339 /* If it's not `throw', then there's no exception-specification. */
19340 if (!cp_parser_is_keyword (token, RID_THROW))
19344 /* Enable this once a lot of code has transitioned to noexcept? */
19345 if (cxx_dialect == cxx0x && !in_system_header)
19346 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19347 "deprecated in C++0x; use %<noexcept%> instead");
19350 /* Consume the `throw'. */
19351 cp_lexer_consume_token (parser->lexer);
19353 /* Look for the `('. */
19354 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19356 /* Peek at the next token. */
19357 token = cp_lexer_peek_token (parser->lexer);
19358 /* If it's not a `)', then there is a type-id-list. */
19359 if (token->type != CPP_CLOSE_PAREN)
19361 /* Types may not be defined in an exception-specification. */
19362 saved_message = parser->type_definition_forbidden_message;
19363 parser->type_definition_forbidden_message
19364 = G_("types may not be defined in an exception-specification");
19365 /* Parse the type-id-list. */
19366 type_id_list = cp_parser_type_id_list (parser);
19367 /* Restore the saved message. */
19368 parser->type_definition_forbidden_message = saved_message;
19371 type_id_list = empty_except_spec;
19373 /* Look for the `)'. */
19374 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19376 return type_id_list;
19379 /* Parse an (optional) type-id-list.
19383 type-id-list , type-id ... [opt]
19385 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19386 in the order that the types were presented. */
19389 cp_parser_type_id_list (cp_parser* parser)
19391 tree types = NULL_TREE;
19398 /* Get the next type-id. */
19399 type = cp_parser_type_id (parser);
19400 /* Parse the optional ellipsis. */
19401 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19403 /* Consume the `...'. */
19404 cp_lexer_consume_token (parser->lexer);
19406 /* Turn the type into a pack expansion expression. */
19407 type = make_pack_expansion (type);
19409 /* Add it to the list. */
19410 types = add_exception_specifier (types, type, /*complain=*/1);
19411 /* Peek at the next token. */
19412 token = cp_lexer_peek_token (parser->lexer);
19413 /* If it is not a `,', we are done. */
19414 if (token->type != CPP_COMMA)
19416 /* Consume the `,'. */
19417 cp_lexer_consume_token (parser->lexer);
19420 return nreverse (types);
19423 /* Parse a try-block.
19426 try compound-statement handler-seq */
19429 cp_parser_try_block (cp_parser* parser)
19433 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19434 try_block = begin_try_block ();
19435 cp_parser_compound_statement (parser, NULL, true, false);
19436 finish_try_block (try_block);
19437 cp_parser_handler_seq (parser);
19438 finish_handler_sequence (try_block);
19443 /* Parse a function-try-block.
19445 function-try-block:
19446 try ctor-initializer [opt] function-body handler-seq */
19449 cp_parser_function_try_block (cp_parser* parser)
19451 tree compound_stmt;
19453 bool ctor_initializer_p;
19455 /* Look for the `try' keyword. */
19456 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19458 /* Let the rest of the front end know where we are. */
19459 try_block = begin_function_try_block (&compound_stmt);
19460 /* Parse the function-body. */
19462 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19463 /* We're done with the `try' part. */
19464 finish_function_try_block (try_block);
19465 /* Parse the handlers. */
19466 cp_parser_handler_seq (parser);
19467 /* We're done with the handlers. */
19468 finish_function_handler_sequence (try_block, compound_stmt);
19470 return ctor_initializer_p;
19473 /* Parse a handler-seq.
19476 handler handler-seq [opt] */
19479 cp_parser_handler_seq (cp_parser* parser)
19485 /* Parse the handler. */
19486 cp_parser_handler (parser);
19487 /* Peek at the next token. */
19488 token = cp_lexer_peek_token (parser->lexer);
19489 /* If it's not `catch' then there are no more handlers. */
19490 if (!cp_parser_is_keyword (token, RID_CATCH))
19495 /* Parse a handler.
19498 catch ( exception-declaration ) compound-statement */
19501 cp_parser_handler (cp_parser* parser)
19506 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19507 handler = begin_handler ();
19508 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19509 declaration = cp_parser_exception_declaration (parser);
19510 finish_handler_parms (declaration, handler);
19511 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19512 cp_parser_compound_statement (parser, NULL, false, false);
19513 finish_handler (handler);
19516 /* Parse an exception-declaration.
19518 exception-declaration:
19519 type-specifier-seq declarator
19520 type-specifier-seq abstract-declarator
19524 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19525 ellipsis variant is used. */
19528 cp_parser_exception_declaration (cp_parser* parser)
19530 cp_decl_specifier_seq type_specifiers;
19531 cp_declarator *declarator;
19532 const char *saved_message;
19534 /* If it's an ellipsis, it's easy to handle. */
19535 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19537 /* Consume the `...' token. */
19538 cp_lexer_consume_token (parser->lexer);
19542 /* Types may not be defined in exception-declarations. */
19543 saved_message = parser->type_definition_forbidden_message;
19544 parser->type_definition_forbidden_message
19545 = G_("types may not be defined in exception-declarations");
19547 /* Parse the type-specifier-seq. */
19548 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19549 /*is_trailing_return=*/false,
19551 /* If it's a `)', then there is no declarator. */
19552 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19555 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19556 /*ctor_dtor_or_conv_p=*/NULL,
19557 /*parenthesized_p=*/NULL,
19558 /*member_p=*/false);
19560 /* Restore the saved message. */
19561 parser->type_definition_forbidden_message = saved_message;
19563 if (!type_specifiers.any_specifiers_p)
19564 return error_mark_node;
19566 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19569 /* Parse a throw-expression.
19572 throw assignment-expression [opt]
19574 Returns a THROW_EXPR representing the throw-expression. */
19577 cp_parser_throw_expression (cp_parser* parser)
19582 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19583 token = cp_lexer_peek_token (parser->lexer);
19584 /* Figure out whether or not there is an assignment-expression
19585 following the "throw" keyword. */
19586 if (token->type == CPP_COMMA
19587 || token->type == CPP_SEMICOLON
19588 || token->type == CPP_CLOSE_PAREN
19589 || token->type == CPP_CLOSE_SQUARE
19590 || token->type == CPP_CLOSE_BRACE
19591 || token->type == CPP_COLON)
19592 expression = NULL_TREE;
19594 expression = cp_parser_assignment_expression (parser,
19595 /*cast_p=*/false, NULL);
19597 return build_throw (expression);
19600 /* GNU Extensions */
19602 /* Parse an (optional) asm-specification.
19605 asm ( string-literal )
19607 If the asm-specification is present, returns a STRING_CST
19608 corresponding to the string-literal. Otherwise, returns
19612 cp_parser_asm_specification_opt (cp_parser* parser)
19615 tree asm_specification;
19617 /* Peek at the next token. */
19618 token = cp_lexer_peek_token (parser->lexer);
19619 /* If the next token isn't the `asm' keyword, then there's no
19620 asm-specification. */
19621 if (!cp_parser_is_keyword (token, RID_ASM))
19624 /* Consume the `asm' token. */
19625 cp_lexer_consume_token (parser->lexer);
19626 /* Look for the `('. */
19627 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19629 /* Look for the string-literal. */
19630 asm_specification = cp_parser_string_literal (parser, false, false);
19632 /* Look for the `)'. */
19633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19635 return asm_specification;
19638 /* Parse an asm-operand-list.
19642 asm-operand-list , asm-operand
19645 string-literal ( expression )
19646 [ string-literal ] string-literal ( expression )
19648 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19649 each node is the expression. The TREE_PURPOSE is itself a
19650 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19651 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19652 is a STRING_CST for the string literal before the parenthesis. Returns
19653 ERROR_MARK_NODE if any of the operands are invalid. */
19656 cp_parser_asm_operand_list (cp_parser* parser)
19658 tree asm_operands = NULL_TREE;
19659 bool invalid_operands = false;
19663 tree string_literal;
19667 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19669 /* Consume the `[' token. */
19670 cp_lexer_consume_token (parser->lexer);
19671 /* Read the operand name. */
19672 name = cp_parser_identifier (parser);
19673 if (name != error_mark_node)
19674 name = build_string (IDENTIFIER_LENGTH (name),
19675 IDENTIFIER_POINTER (name));
19676 /* Look for the closing `]'. */
19677 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19681 /* Look for the string-literal. */
19682 string_literal = cp_parser_string_literal (parser, false, false);
19684 /* Look for the `('. */
19685 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19686 /* Parse the expression. */
19687 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19688 /* Look for the `)'. */
19689 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19691 if (name == error_mark_node
19692 || string_literal == error_mark_node
19693 || expression == error_mark_node)
19694 invalid_operands = true;
19696 /* Add this operand to the list. */
19697 asm_operands = tree_cons (build_tree_list (name, string_literal),
19700 /* If the next token is not a `,', there are no more
19702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19704 /* Consume the `,'. */
19705 cp_lexer_consume_token (parser->lexer);
19708 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19711 /* Parse an asm-clobber-list.
19715 asm-clobber-list , string-literal
19717 Returns a TREE_LIST, indicating the clobbers in the order that they
19718 appeared. The TREE_VALUE of each node is a STRING_CST. */
19721 cp_parser_asm_clobber_list (cp_parser* parser)
19723 tree clobbers = NULL_TREE;
19727 tree string_literal;
19729 /* Look for the string literal. */
19730 string_literal = cp_parser_string_literal (parser, false, false);
19731 /* Add it to the list. */
19732 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19733 /* If the next token is not a `,', then the list is
19735 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19737 /* Consume the `,' token. */
19738 cp_lexer_consume_token (parser->lexer);
19744 /* Parse an asm-label-list.
19748 asm-label-list , identifier
19750 Returns a TREE_LIST, indicating the labels in the order that they
19751 appeared. The TREE_VALUE of each node is a label. */
19754 cp_parser_asm_label_list (cp_parser* parser)
19756 tree labels = NULL_TREE;
19760 tree identifier, label, name;
19762 /* Look for the identifier. */
19763 identifier = cp_parser_identifier (parser);
19764 if (!error_operand_p (identifier))
19766 label = lookup_label (identifier);
19767 if (TREE_CODE (label) == LABEL_DECL)
19769 TREE_USED (label) = 1;
19770 check_goto (label);
19771 name = build_string (IDENTIFIER_LENGTH (identifier),
19772 IDENTIFIER_POINTER (identifier));
19773 labels = tree_cons (name, label, labels);
19776 /* If the next token is not a `,', then the list is
19778 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19780 /* Consume the `,' token. */
19781 cp_lexer_consume_token (parser->lexer);
19784 return nreverse (labels);
19787 /* Parse an (optional) series of attributes.
19790 attributes attribute
19793 __attribute__ (( attribute-list [opt] ))
19795 The return value is as for cp_parser_attribute_list. */
19798 cp_parser_attributes_opt (cp_parser* parser)
19800 tree attributes = NULL_TREE;
19805 tree attribute_list;
19807 /* Peek at the next token. */
19808 token = cp_lexer_peek_token (parser->lexer);
19809 /* If it's not `__attribute__', then we're done. */
19810 if (token->keyword != RID_ATTRIBUTE)
19813 /* Consume the `__attribute__' keyword. */
19814 cp_lexer_consume_token (parser->lexer);
19815 /* Look for the two `(' tokens. */
19816 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19819 /* Peek at the next token. */
19820 token = cp_lexer_peek_token (parser->lexer);
19821 if (token->type != CPP_CLOSE_PAREN)
19822 /* Parse the attribute-list. */
19823 attribute_list = cp_parser_attribute_list (parser);
19825 /* If the next token is a `)', then there is no attribute
19827 attribute_list = NULL;
19829 /* Look for the two `)' tokens. */
19830 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19833 /* Add these new attributes to the list. */
19834 attributes = chainon (attributes, attribute_list);
19840 /* Parse an attribute-list.
19844 attribute-list , attribute
19848 identifier ( identifier )
19849 identifier ( identifier , expression-list )
19850 identifier ( expression-list )
19852 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
19853 to an attribute. The TREE_PURPOSE of each node is the identifier
19854 indicating which attribute is in use. The TREE_VALUE represents
19855 the arguments, if any. */
19858 cp_parser_attribute_list (cp_parser* parser)
19860 tree attribute_list = NULL_TREE;
19861 bool save_translate_strings_p = parser->translate_strings_p;
19863 parser->translate_strings_p = false;
19870 /* Look for the identifier. We also allow keywords here; for
19871 example `__attribute__ ((const))' is legal. */
19872 token = cp_lexer_peek_token (parser->lexer);
19873 if (token->type == CPP_NAME
19874 || token->type == CPP_KEYWORD)
19876 tree arguments = NULL_TREE;
19878 /* Consume the token. */
19879 token = cp_lexer_consume_token (parser->lexer);
19881 /* Save away the identifier that indicates which attribute
19883 identifier = (token->type == CPP_KEYWORD)
19884 /* For keywords, use the canonical spelling, not the
19885 parsed identifier. */
19886 ? ridpointers[(int) token->keyword]
19889 attribute = build_tree_list (identifier, NULL_TREE);
19891 /* Peek at the next token. */
19892 token = cp_lexer_peek_token (parser->lexer);
19893 /* If it's an `(', then parse the attribute arguments. */
19894 if (token->type == CPP_OPEN_PAREN)
19897 int attr_flag = (attribute_takes_identifier_p (identifier)
19898 ? id_attr : normal_attr);
19899 vec = cp_parser_parenthesized_expression_list
19900 (parser, attr_flag, /*cast_p=*/false,
19901 /*allow_expansion_p=*/false,
19902 /*non_constant_p=*/NULL);
19904 arguments = error_mark_node;
19907 arguments = build_tree_list_vec (vec);
19908 release_tree_vector (vec);
19910 /* Save the arguments away. */
19911 TREE_VALUE (attribute) = arguments;
19914 if (arguments != error_mark_node)
19916 /* Add this attribute to the list. */
19917 TREE_CHAIN (attribute) = attribute_list;
19918 attribute_list = attribute;
19921 token = cp_lexer_peek_token (parser->lexer);
19923 /* Now, look for more attributes. If the next token isn't a
19924 `,', we're done. */
19925 if (token->type != CPP_COMMA)
19928 /* Consume the comma and keep going. */
19929 cp_lexer_consume_token (parser->lexer);
19931 parser->translate_strings_p = save_translate_strings_p;
19933 /* We built up the list in reverse order. */
19934 return nreverse (attribute_list);
19937 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
19938 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
19939 current value of the PEDANTIC flag, regardless of whether or not
19940 the `__extension__' keyword is present. The caller is responsible
19941 for restoring the value of the PEDANTIC flag. */
19944 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19946 /* Save the old value of the PEDANTIC flag. */
19947 *saved_pedantic = pedantic;
19949 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19951 /* Consume the `__extension__' token. */
19952 cp_lexer_consume_token (parser->lexer);
19953 /* We're not being pedantic while the `__extension__' keyword is
19963 /* Parse a label declaration.
19966 __label__ label-declarator-seq ;
19968 label-declarator-seq:
19969 identifier , label-declarator-seq
19973 cp_parser_label_declaration (cp_parser* parser)
19975 /* Look for the `__label__' keyword. */
19976 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19982 /* Look for an identifier. */
19983 identifier = cp_parser_identifier (parser);
19984 /* If we failed, stop. */
19985 if (identifier == error_mark_node)
19987 /* Declare it as a label. */
19988 finish_label_decl (identifier);
19989 /* If the next token is a `;', stop. */
19990 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19992 /* Look for the `,' separating the label declarations. */
19993 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19996 /* Look for the final `;'. */
19997 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20000 /* Support Functions */
20002 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20003 NAME should have one of the representations used for an
20004 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20005 is returned. If PARSER->SCOPE is a dependent type, then a
20006 SCOPE_REF is returned.
20008 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20009 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20010 was formed. Abstractly, such entities should not be passed to this
20011 function, because they do not need to be looked up, but it is
20012 simpler to check for this special case here, rather than at the
20015 In cases not explicitly covered above, this function returns a
20016 DECL, OVERLOAD, or baselink representing the result of the lookup.
20017 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20020 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20021 (e.g., "struct") that was used. In that case bindings that do not
20022 refer to types are ignored.
20024 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20027 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20030 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20033 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20034 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20035 NULL_TREE otherwise. */
20038 cp_parser_lookup_name (cp_parser *parser, tree name,
20039 enum tag_types tag_type,
20042 bool check_dependency,
20043 tree *ambiguous_decls,
20044 location_t name_location)
20048 tree object_type = parser->context->object_type;
20050 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20051 flags |= LOOKUP_COMPLAIN;
20053 /* Assume that the lookup will be unambiguous. */
20054 if (ambiguous_decls)
20055 *ambiguous_decls = NULL_TREE;
20057 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20058 no longer valid. Note that if we are parsing tentatively, and
20059 the parse fails, OBJECT_TYPE will be automatically restored. */
20060 parser->context->object_type = NULL_TREE;
20062 if (name == error_mark_node)
20063 return error_mark_node;
20065 /* A template-id has already been resolved; there is no lookup to
20067 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20069 if (BASELINK_P (name))
20071 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20072 == TEMPLATE_ID_EXPR);
20076 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20077 it should already have been checked to make sure that the name
20078 used matches the type being destroyed. */
20079 if (TREE_CODE (name) == BIT_NOT_EXPR)
20083 /* Figure out to which type this destructor applies. */
20085 type = parser->scope;
20086 else if (object_type)
20087 type = object_type;
20089 type = current_class_type;
20090 /* If that's not a class type, there is no destructor. */
20091 if (!type || !CLASS_TYPE_P (type))
20092 return error_mark_node;
20093 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20094 lazily_declare_fn (sfk_destructor, type);
20095 if (!CLASSTYPE_DESTRUCTORS (type))
20096 return error_mark_node;
20097 /* If it was a class type, return the destructor. */
20098 return CLASSTYPE_DESTRUCTORS (type);
20101 /* By this point, the NAME should be an ordinary identifier. If
20102 the id-expression was a qualified name, the qualifying scope is
20103 stored in PARSER->SCOPE at this point. */
20104 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20106 /* Perform the lookup. */
20111 if (parser->scope == error_mark_node)
20112 return error_mark_node;
20114 /* If the SCOPE is dependent, the lookup must be deferred until
20115 the template is instantiated -- unless we are explicitly
20116 looking up names in uninstantiated templates. Even then, we
20117 cannot look up the name if the scope is not a class type; it
20118 might, for example, be a template type parameter. */
20119 dependent_p = (TYPE_P (parser->scope)
20120 && dependent_scope_p (parser->scope));
20121 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20123 /* Defer lookup. */
20124 decl = error_mark_node;
20127 tree pushed_scope = NULL_TREE;
20129 /* If PARSER->SCOPE is a dependent type, then it must be a
20130 class type, and we must not be checking dependencies;
20131 otherwise, we would have processed this lookup above. So
20132 that PARSER->SCOPE is not considered a dependent base by
20133 lookup_member, we must enter the scope here. */
20135 pushed_scope = push_scope (parser->scope);
20137 /* If the PARSER->SCOPE is a template specialization, it
20138 may be instantiated during name lookup. In that case,
20139 errors may be issued. Even if we rollback the current
20140 tentative parse, those errors are valid. */
20141 decl = lookup_qualified_name (parser->scope, name,
20142 tag_type != none_type,
20143 /*complain=*/true);
20145 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20146 lookup result and the nested-name-specifier nominates a class C:
20147 * if the name specified after the nested-name-specifier, when
20148 looked up in C, is the injected-class-name of C (Clause 9), or
20149 * if the name specified after the nested-name-specifier is the
20150 same as the identifier or the simple-template-id's template-
20151 name in the last component of the nested-name-specifier,
20152 the name is instead considered to name the constructor of
20153 class C. [ Note: for example, the constructor is not an
20154 acceptable lookup result in an elaborated-type-specifier so
20155 the constructor would not be used in place of the
20156 injected-class-name. --end note ] Such a constructor name
20157 shall be used only in the declarator-id of a declaration that
20158 names a constructor or in a using-declaration. */
20159 if (tag_type == none_type
20160 && DECL_SELF_REFERENCE_P (decl)
20161 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20162 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20163 tag_type != none_type,
20164 /*complain=*/true);
20166 /* If we have a single function from a using decl, pull it out. */
20167 if (TREE_CODE (decl) == OVERLOAD
20168 && !really_overloaded_fn (decl))
20169 decl = OVL_FUNCTION (decl);
20172 pop_scope (pushed_scope);
20175 /* If the scope is a dependent type and either we deferred lookup or
20176 we did lookup but didn't find the name, rememeber the name. */
20177 if (decl == error_mark_node && TYPE_P (parser->scope)
20178 && dependent_type_p (parser->scope))
20184 /* The resolution to Core Issue 180 says that `struct
20185 A::B' should be considered a type-name, even if `A'
20187 type = make_typename_type (parser->scope, name, tag_type,
20188 /*complain=*/tf_error);
20189 decl = TYPE_NAME (type);
20191 else if (is_template
20192 && (cp_parser_next_token_ends_template_argument_p (parser)
20193 || cp_lexer_next_token_is (parser->lexer,
20195 decl = make_unbound_class_template (parser->scope,
20197 /*complain=*/tf_error);
20199 decl = build_qualified_name (/*type=*/NULL_TREE,
20200 parser->scope, name,
20203 parser->qualifying_scope = parser->scope;
20204 parser->object_scope = NULL_TREE;
20206 else if (object_type)
20208 tree object_decl = NULL_TREE;
20209 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20210 OBJECT_TYPE is not a class. */
20211 if (CLASS_TYPE_P (object_type))
20212 /* If the OBJECT_TYPE is a template specialization, it may
20213 be instantiated during name lookup. In that case, errors
20214 may be issued. Even if we rollback the current tentative
20215 parse, those errors are valid. */
20216 object_decl = lookup_member (object_type,
20219 tag_type != none_type);
20220 /* Look it up in the enclosing context, too. */
20221 decl = lookup_name_real (name, tag_type != none_type,
20223 /*block_p=*/true, is_namespace, flags);
20224 parser->object_scope = object_type;
20225 parser->qualifying_scope = NULL_TREE;
20227 decl = object_decl;
20231 decl = lookup_name_real (name, tag_type != none_type,
20233 /*block_p=*/true, is_namespace, flags);
20234 parser->qualifying_scope = NULL_TREE;
20235 parser->object_scope = NULL_TREE;
20238 /* If the lookup failed, let our caller know. */
20239 if (!decl || decl == error_mark_node)
20240 return error_mark_node;
20242 /* Pull out the template from an injected-class-name (or multiple). */
20244 decl = maybe_get_template_decl_from_type_decl (decl);
20246 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20247 if (TREE_CODE (decl) == TREE_LIST)
20249 if (ambiguous_decls)
20250 *ambiguous_decls = decl;
20251 /* The error message we have to print is too complicated for
20252 cp_parser_error, so we incorporate its actions directly. */
20253 if (!cp_parser_simulate_error (parser))
20255 error_at (name_location, "reference to %qD is ambiguous",
20257 print_candidates (decl);
20259 return error_mark_node;
20262 gcc_assert (DECL_P (decl)
20263 || TREE_CODE (decl) == OVERLOAD
20264 || TREE_CODE (decl) == SCOPE_REF
20265 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20266 || BASELINK_P (decl));
20268 /* If we have resolved the name of a member declaration, check to
20269 see if the declaration is accessible. When the name resolves to
20270 set of overloaded functions, accessibility is checked when
20271 overload resolution is done.
20273 During an explicit instantiation, access is not checked at all,
20274 as per [temp.explicit]. */
20276 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20278 maybe_record_typedef_use (decl);
20283 /* Like cp_parser_lookup_name, but for use in the typical case where
20284 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20285 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20288 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20290 return cp_parser_lookup_name (parser, name,
20292 /*is_template=*/false,
20293 /*is_namespace=*/false,
20294 /*check_dependency=*/true,
20295 /*ambiguous_decls=*/NULL,
20299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20300 the current context, return the TYPE_DECL. If TAG_NAME_P is
20301 true, the DECL indicates the class being defined in a class-head,
20302 or declared in an elaborated-type-specifier.
20304 Otherwise, return DECL. */
20307 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20309 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20310 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20313 template <typename T> struct B;
20316 template <typename T> struct A::B {};
20318 Similarly, in an elaborated-type-specifier:
20320 namespace N { struct X{}; }
20323 template <typename T> friend struct N::X;
20326 However, if the DECL refers to a class type, and we are in
20327 the scope of the class, then the name lookup automatically
20328 finds the TYPE_DECL created by build_self_reference rather
20329 than a TEMPLATE_DECL. For example, in:
20331 template <class T> struct S {
20335 there is no need to handle such case. */
20337 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20338 return DECL_TEMPLATE_RESULT (decl);
20343 /* If too many, or too few, template-parameter lists apply to the
20344 declarator, issue an error message. Returns TRUE if all went well,
20345 and FALSE otherwise. */
20348 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20349 cp_declarator *declarator,
20350 location_t declarator_location)
20352 unsigned num_templates;
20354 /* We haven't seen any classes that involve template parameters yet. */
20357 switch (declarator->kind)
20360 if (declarator->u.id.qualifying_scope)
20364 scope = declarator->u.id.qualifying_scope;
20366 while (scope && CLASS_TYPE_P (scope))
20368 /* You're supposed to have one `template <...>'
20369 for every template class, but you don't need one
20370 for a full specialization. For example:
20372 template <class T> struct S{};
20373 template <> struct S<int> { void f(); };
20374 void S<int>::f () {}
20376 is correct; there shouldn't be a `template <>' for
20377 the definition of `S<int>::f'. */
20378 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20379 /* If SCOPE does not have template information of any
20380 kind, then it is not a template, nor is it nested
20381 within a template. */
20383 if (explicit_class_specialization_p (scope))
20385 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20388 scope = TYPE_CONTEXT (scope);
20391 else if (TREE_CODE (declarator->u.id.unqualified_name)
20392 == TEMPLATE_ID_EXPR)
20393 /* If the DECLARATOR has the form `X<y>' then it uses one
20394 additional level of template parameters. */
20397 return cp_parser_check_template_parameters
20398 (parser, num_templates, declarator_location, declarator);
20404 case cdk_reference:
20406 return (cp_parser_check_declarator_template_parameters
20407 (parser, declarator->declarator, declarator_location));
20413 gcc_unreachable ();
20418 /* NUM_TEMPLATES were used in the current declaration. If that is
20419 invalid, return FALSE and issue an error messages. Otherwise,
20420 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20421 declarator and we can print more accurate diagnostics. */
20424 cp_parser_check_template_parameters (cp_parser* parser,
20425 unsigned num_templates,
20426 location_t location,
20427 cp_declarator *declarator)
20429 /* If there are the same number of template classes and parameter
20430 lists, that's OK. */
20431 if (parser->num_template_parameter_lists == num_templates)
20433 /* If there are more, but only one more, then we are referring to a
20434 member template. That's OK too. */
20435 if (parser->num_template_parameter_lists == num_templates + 1)
20437 /* If there are more template classes than parameter lists, we have
20440 template <class T> void S<T>::R<T>::f (); */
20441 if (parser->num_template_parameter_lists < num_templates)
20443 if (declarator && !current_function_decl)
20444 error_at (location, "specializing member %<%T::%E%> "
20445 "requires %<template<>%> syntax",
20446 declarator->u.id.qualifying_scope,
20447 declarator->u.id.unqualified_name);
20448 else if (declarator)
20449 error_at (location, "invalid declaration of %<%T::%E%>",
20450 declarator->u.id.qualifying_scope,
20451 declarator->u.id.unqualified_name);
20453 error_at (location, "too few template-parameter-lists");
20456 /* Otherwise, there are too many template parameter lists. We have
20459 template <class T> template <class U> void S::f(); */
20460 error_at (location, "too many template-parameter-lists");
20464 /* Parse an optional `::' token indicating that the following name is
20465 from the global namespace. If so, PARSER->SCOPE is set to the
20466 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20467 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20468 Returns the new value of PARSER->SCOPE, if the `::' token is
20469 present, and NULL_TREE otherwise. */
20472 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20476 /* Peek at the next token. */
20477 token = cp_lexer_peek_token (parser->lexer);
20478 /* If we're looking at a `::' token then we're starting from the
20479 global namespace, not our current location. */
20480 if (token->type == CPP_SCOPE)
20482 /* Consume the `::' token. */
20483 cp_lexer_consume_token (parser->lexer);
20484 /* Set the SCOPE so that we know where to start the lookup. */
20485 parser->scope = global_namespace;
20486 parser->qualifying_scope = global_namespace;
20487 parser->object_scope = NULL_TREE;
20489 return parser->scope;
20491 else if (!current_scope_valid_p)
20493 parser->scope = NULL_TREE;
20494 parser->qualifying_scope = NULL_TREE;
20495 parser->object_scope = NULL_TREE;
20501 /* Returns TRUE if the upcoming token sequence is the start of a
20502 constructor declarator. If FRIEND_P is true, the declarator is
20503 preceded by the `friend' specifier. */
20506 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20508 bool constructor_p;
20509 tree nested_name_specifier;
20510 cp_token *next_token;
20512 /* The common case is that this is not a constructor declarator, so
20513 try to avoid doing lots of work if at all possible. It's not
20514 valid declare a constructor at function scope. */
20515 if (parser->in_function_body)
20517 /* And only certain tokens can begin a constructor declarator. */
20518 next_token = cp_lexer_peek_token (parser->lexer);
20519 if (next_token->type != CPP_NAME
20520 && next_token->type != CPP_SCOPE
20521 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20522 && next_token->type != CPP_TEMPLATE_ID)
20525 /* Parse tentatively; we are going to roll back all of the tokens
20527 cp_parser_parse_tentatively (parser);
20528 /* Assume that we are looking at a constructor declarator. */
20529 constructor_p = true;
20531 /* Look for the optional `::' operator. */
20532 cp_parser_global_scope_opt (parser,
20533 /*current_scope_valid_p=*/false);
20534 /* Look for the nested-name-specifier. */
20535 nested_name_specifier
20536 = (cp_parser_nested_name_specifier_opt (parser,
20537 /*typename_keyword_p=*/false,
20538 /*check_dependency_p=*/false,
20540 /*is_declaration=*/false));
20541 /* Outside of a class-specifier, there must be a
20542 nested-name-specifier. */
20543 if (!nested_name_specifier &&
20544 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20546 constructor_p = false;
20547 else if (nested_name_specifier == error_mark_node)
20548 constructor_p = false;
20550 /* If we have a class scope, this is easy; DR 147 says that S::S always
20551 names the constructor, and no other qualified name could. */
20552 if (constructor_p && nested_name_specifier
20553 && CLASS_TYPE_P (nested_name_specifier))
20555 tree id = cp_parser_unqualified_id (parser,
20556 /*template_keyword_p=*/false,
20557 /*check_dependency_p=*/false,
20558 /*declarator_p=*/true,
20559 /*optional_p=*/false);
20560 if (is_overloaded_fn (id))
20561 id = DECL_NAME (get_first_fn (id));
20562 if (!constructor_name_p (id, nested_name_specifier))
20563 constructor_p = false;
20565 /* If we still think that this might be a constructor-declarator,
20566 look for a class-name. */
20567 else if (constructor_p)
20571 template <typename T> struct S {
20575 we must recognize that the nested `S' names a class. */
20577 type_decl = cp_parser_class_name (parser,
20578 /*typename_keyword_p=*/false,
20579 /*template_keyword_p=*/false,
20581 /*check_dependency_p=*/false,
20582 /*class_head_p=*/false,
20583 /*is_declaration=*/false);
20584 /* If there was no class-name, then this is not a constructor. */
20585 constructor_p = !cp_parser_error_occurred (parser);
20587 /* If we're still considering a constructor, we have to see a `(',
20588 to begin the parameter-declaration-clause, followed by either a
20589 `)', an `...', or a decl-specifier. We need to check for a
20590 type-specifier to avoid being fooled into thinking that:
20594 is a constructor. (It is actually a function named `f' that
20595 takes one parameter (of type `int') and returns a value of type
20598 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20599 constructor_p = false;
20602 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20603 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20604 /* A parameter declaration begins with a decl-specifier,
20605 which is either the "attribute" keyword, a storage class
20606 specifier, or (usually) a type-specifier. */
20607 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20610 tree pushed_scope = NULL_TREE;
20611 unsigned saved_num_template_parameter_lists;
20613 /* Names appearing in the type-specifier should be looked up
20614 in the scope of the class. */
20615 if (current_class_type)
20619 type = TREE_TYPE (type_decl);
20620 if (TREE_CODE (type) == TYPENAME_TYPE)
20622 type = resolve_typename_type (type,
20623 /*only_current_p=*/false);
20624 if (TREE_CODE (type) == TYPENAME_TYPE)
20626 cp_parser_abort_tentative_parse (parser);
20630 pushed_scope = push_scope (type);
20633 /* Inside the constructor parameter list, surrounding
20634 template-parameter-lists do not apply. */
20635 saved_num_template_parameter_lists
20636 = parser->num_template_parameter_lists;
20637 parser->num_template_parameter_lists = 0;
20639 /* Look for the type-specifier. */
20640 cp_parser_type_specifier (parser,
20641 CP_PARSER_FLAGS_NONE,
20642 /*decl_specs=*/NULL,
20643 /*is_declarator=*/true,
20644 /*declares_class_or_enum=*/NULL,
20645 /*is_cv_qualifier=*/NULL);
20647 parser->num_template_parameter_lists
20648 = saved_num_template_parameter_lists;
20650 /* Leave the scope of the class. */
20652 pop_scope (pushed_scope);
20654 constructor_p = !cp_parser_error_occurred (parser);
20658 /* We did not really want to consume any tokens. */
20659 cp_parser_abort_tentative_parse (parser);
20661 return constructor_p;
20664 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20665 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20666 they must be performed once we are in the scope of the function.
20668 Returns the function defined. */
20671 cp_parser_function_definition_from_specifiers_and_declarator
20672 (cp_parser* parser,
20673 cp_decl_specifier_seq *decl_specifiers,
20675 const cp_declarator *declarator)
20680 /* Begin the function-definition. */
20681 success_p = start_function (decl_specifiers, declarator, attributes);
20683 /* The things we're about to see are not directly qualified by any
20684 template headers we've seen thus far. */
20685 reset_specialization ();
20687 /* If there were names looked up in the decl-specifier-seq that we
20688 did not check, check them now. We must wait until we are in the
20689 scope of the function to perform the checks, since the function
20690 might be a friend. */
20691 perform_deferred_access_checks ();
20695 /* Skip the entire function. */
20696 cp_parser_skip_to_end_of_block_or_statement (parser);
20697 fn = error_mark_node;
20699 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20701 /* Seen already, skip it. An error message has already been output. */
20702 cp_parser_skip_to_end_of_block_or_statement (parser);
20703 fn = current_function_decl;
20704 current_function_decl = NULL_TREE;
20705 /* If this is a function from a class, pop the nested class. */
20706 if (current_class_name)
20707 pop_nested_class ();
20712 if (DECL_DECLARED_INLINE_P (current_function_decl))
20713 tv = TV_PARSE_INLINE;
20715 tv = TV_PARSE_FUNC;
20717 fn = cp_parser_function_definition_after_declarator (parser,
20718 /*inline_p=*/false);
20725 /* Parse the part of a function-definition that follows the
20726 declarator. INLINE_P is TRUE iff this function is an inline
20727 function defined within a class-specifier.
20729 Returns the function defined. */
20732 cp_parser_function_definition_after_declarator (cp_parser* parser,
20736 bool ctor_initializer_p = false;
20737 bool saved_in_unbraced_linkage_specification_p;
20738 bool saved_in_function_body;
20739 unsigned saved_num_template_parameter_lists;
20742 saved_in_function_body = parser->in_function_body;
20743 parser->in_function_body = true;
20744 /* If the next token is `return', then the code may be trying to
20745 make use of the "named return value" extension that G++ used to
20747 token = cp_lexer_peek_token (parser->lexer);
20748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20750 /* Consume the `return' keyword. */
20751 cp_lexer_consume_token (parser->lexer);
20752 /* Look for the identifier that indicates what value is to be
20754 cp_parser_identifier (parser);
20755 /* Issue an error message. */
20756 error_at (token->location,
20757 "named return values are no longer supported");
20758 /* Skip tokens until we reach the start of the function body. */
20761 cp_token *token = cp_lexer_peek_token (parser->lexer);
20762 if (token->type == CPP_OPEN_BRACE
20763 || token->type == CPP_EOF
20764 || token->type == CPP_PRAGMA_EOL)
20766 cp_lexer_consume_token (parser->lexer);
20769 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20770 anything declared inside `f'. */
20771 saved_in_unbraced_linkage_specification_p
20772 = parser->in_unbraced_linkage_specification_p;
20773 parser->in_unbraced_linkage_specification_p = false;
20774 /* Inside the function, surrounding template-parameter-lists do not
20776 saved_num_template_parameter_lists
20777 = parser->num_template_parameter_lists;
20778 parser->num_template_parameter_lists = 0;
20780 start_lambda_scope (current_function_decl);
20782 /* If the next token is `try', then we are looking at a
20783 function-try-block. */
20784 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20785 ctor_initializer_p = cp_parser_function_try_block (parser);
20786 /* A function-try-block includes the function-body, so we only do
20787 this next part if we're not processing a function-try-block. */
20790 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20792 finish_lambda_scope ();
20794 /* Finish the function. */
20795 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20796 (inline_p ? 2 : 0));
20797 /* Generate code for it, if necessary. */
20798 expand_or_defer_fn (fn);
20799 /* Restore the saved values. */
20800 parser->in_unbraced_linkage_specification_p
20801 = saved_in_unbraced_linkage_specification_p;
20802 parser->num_template_parameter_lists
20803 = saved_num_template_parameter_lists;
20804 parser->in_function_body = saved_in_function_body;
20809 /* Parse a template-declaration, assuming that the `export' (and
20810 `extern') keywords, if present, has already been scanned. MEMBER_P
20811 is as for cp_parser_template_declaration. */
20814 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20816 tree decl = NULL_TREE;
20817 VEC (deferred_access_check,gc) *checks;
20818 tree parameter_list;
20819 bool friend_p = false;
20820 bool need_lang_pop;
20823 /* Look for the `template' keyword. */
20824 token = cp_lexer_peek_token (parser->lexer);
20825 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20829 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20831 if (at_class_scope_p () && current_function_decl)
20833 /* 14.5.2.2 [temp.mem]
20835 A local class shall not have member templates. */
20836 error_at (token->location,
20837 "invalid declaration of member template in local class");
20838 cp_parser_skip_to_end_of_block_or_statement (parser);
20843 A template ... shall not have C linkage. */
20844 if (current_lang_name == lang_name_c)
20846 error_at (token->location, "template with C linkage");
20847 /* Give it C++ linkage to avoid confusing other parts of the
20849 push_lang_context (lang_name_cplusplus);
20850 need_lang_pop = true;
20853 need_lang_pop = false;
20855 /* We cannot perform access checks on the template parameter
20856 declarations until we know what is being declared, just as we
20857 cannot check the decl-specifier list. */
20858 push_deferring_access_checks (dk_deferred);
20860 /* If the next token is `>', then we have an invalid
20861 specialization. Rather than complain about an invalid template
20862 parameter, issue an error message here. */
20863 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20865 cp_parser_error (parser, "invalid explicit specialization");
20866 begin_specialization ();
20867 parameter_list = NULL_TREE;
20871 /* Parse the template parameters. */
20872 parameter_list = cp_parser_template_parameter_list (parser);
20873 fixup_template_parms ();
20876 /* Get the deferred access checks from the parameter list. These
20877 will be checked once we know what is being declared, as for a
20878 member template the checks must be performed in the scope of the
20879 class containing the member. */
20880 checks = get_deferred_access_checks ();
20882 /* Look for the `>'. */
20883 cp_parser_skip_to_end_of_template_parameter_list (parser);
20884 /* We just processed one more parameter list. */
20885 ++parser->num_template_parameter_lists;
20886 /* If the next token is `template', there are more template
20888 if (cp_lexer_next_token_is_keyword (parser->lexer,
20890 cp_parser_template_declaration_after_export (parser, member_p);
20893 /* There are no access checks when parsing a template, as we do not
20894 know if a specialization will be a friend. */
20895 push_deferring_access_checks (dk_no_check);
20896 token = cp_lexer_peek_token (parser->lexer);
20897 decl = cp_parser_single_declaration (parser,
20900 /*explicit_specialization_p=*/false,
20902 pop_deferring_access_checks ();
20904 /* If this is a member template declaration, let the front
20906 if (member_p && !friend_p && decl)
20908 if (TREE_CODE (decl) == TYPE_DECL)
20909 cp_parser_check_access_in_redeclaration (decl, token->location);
20911 decl = finish_member_template_decl (decl);
20913 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20914 make_friend_class (current_class_type, TREE_TYPE (decl),
20915 /*complain=*/true);
20917 /* We are done with the current parameter list. */
20918 --parser->num_template_parameter_lists;
20920 pop_deferring_access_checks ();
20923 finish_template_decl (parameter_list);
20925 /* Check the template arguments for a literal operator template. */
20927 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
20928 && UDLIT_OPER_P (DECL_NAME (decl)))
20931 if (parameter_list == NULL_TREE)
20935 int num_parms = TREE_VEC_LENGTH (parameter_list);
20936 if (num_parms != 1)
20940 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
20941 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
20942 if (TREE_TYPE (parm) != char_type_node
20943 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
20948 error ("literal operator template %qD has invalid parameter list."
20949 " Expected non-type template argument pack <char...>",
20952 /* Register member declarations. */
20953 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20954 finish_member_declaration (decl);
20955 /* For the erroneous case of a template with C linkage, we pushed an
20956 implicit C++ linkage scope; exit that scope now. */
20958 pop_lang_context ();
20959 /* If DECL is a function template, we must return to parse it later.
20960 (Even though there is no definition, there might be default
20961 arguments that need handling.) */
20962 if (member_p && decl
20963 && (TREE_CODE (decl) == FUNCTION_DECL
20964 || DECL_FUNCTION_TEMPLATE_P (decl)))
20965 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20968 /* Perform the deferred access checks from a template-parameter-list.
20969 CHECKS is a TREE_LIST of access checks, as returned by
20970 get_deferred_access_checks. */
20973 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20975 ++processing_template_parmlist;
20976 perform_access_checks (checks);
20977 --processing_template_parmlist;
20980 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20981 `function-definition' sequence. MEMBER_P is true, this declaration
20982 appears in a class scope.
20984 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
20985 *FRIEND_P is set to TRUE iff the declaration is a friend. */
20988 cp_parser_single_declaration (cp_parser* parser,
20989 VEC (deferred_access_check,gc)* checks,
20991 bool explicit_specialization_p,
20994 int declares_class_or_enum;
20995 tree decl = NULL_TREE;
20996 cp_decl_specifier_seq decl_specifiers;
20997 bool function_definition_p = false;
20998 cp_token *decl_spec_token_start;
21000 /* This function is only used when processing a template
21002 gcc_assert (innermost_scope_kind () == sk_template_parms
21003 || innermost_scope_kind () == sk_template_spec);
21005 /* Defer access checks until we know what is being declared. */
21006 push_deferring_access_checks (dk_deferred);
21008 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21010 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21011 cp_parser_decl_specifier_seq (parser,
21012 CP_PARSER_FLAGS_OPTIONAL,
21014 &declares_class_or_enum);
21016 *friend_p = cp_parser_friend_p (&decl_specifiers);
21018 /* There are no template typedefs. */
21019 if (decl_specifiers.specs[(int) ds_typedef])
21021 error_at (decl_spec_token_start->location,
21022 "template declaration of %<typedef%>");
21023 decl = error_mark_node;
21026 /* Gather up the access checks that occurred the
21027 decl-specifier-seq. */
21028 stop_deferring_access_checks ();
21030 /* Check for the declaration of a template class. */
21031 if (declares_class_or_enum)
21033 if (cp_parser_declares_only_class_p (parser))
21035 decl = shadow_tag (&decl_specifiers);
21040 friend template <typename T> struct A<T>::B;
21043 A<T>::B will be represented by a TYPENAME_TYPE, and
21044 therefore not recognized by shadow_tag. */
21045 if (friend_p && *friend_p
21047 && decl_specifiers.type
21048 && TYPE_P (decl_specifiers.type))
21049 decl = decl_specifiers.type;
21051 if (decl && decl != error_mark_node)
21052 decl = TYPE_NAME (decl);
21054 decl = error_mark_node;
21056 /* Perform access checks for template parameters. */
21057 cp_parser_perform_template_parameter_access_checks (checks);
21061 /* Complain about missing 'typename' or other invalid type names. */
21062 if (!decl_specifiers.any_type_specifiers_p
21063 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21065 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21066 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21067 the rest of this declaration. */
21068 decl = error_mark_node;
21072 /* If it's not a template class, try for a template function. If
21073 the next token is a `;', then this declaration does not declare
21074 anything. But, if there were errors in the decl-specifiers, then
21075 the error might well have come from an attempted class-specifier.
21076 In that case, there's no need to warn about a missing declarator. */
21078 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21079 || decl_specifiers.type != error_mark_node))
21081 decl = cp_parser_init_declarator (parser,
21084 /*function_definition_allowed_p=*/true,
21086 declares_class_or_enum,
21087 &function_definition_p,
21090 /* 7.1.1-1 [dcl.stc]
21092 A storage-class-specifier shall not be specified in an explicit
21093 specialization... */
21095 && explicit_specialization_p
21096 && decl_specifiers.storage_class != sc_none)
21098 error_at (decl_spec_token_start->location,
21099 "explicit template specialization cannot have a storage class");
21100 decl = error_mark_node;
21104 /* Look for a trailing `;' after the declaration. */
21105 if (!function_definition_p
21106 && (decl == error_mark_node
21107 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21108 cp_parser_skip_to_end_of_block_or_statement (parser);
21111 pop_deferring_access_checks ();
21113 /* Clear any current qualification; whatever comes next is the start
21114 of something new. */
21115 parser->scope = NULL_TREE;
21116 parser->qualifying_scope = NULL_TREE;
21117 parser->object_scope = NULL_TREE;
21122 /* Parse a cast-expression that is not the operand of a unary "&". */
21125 cp_parser_simple_cast_expression (cp_parser *parser)
21127 return cp_parser_cast_expression (parser, /*address_p=*/false,
21128 /*cast_p=*/false, NULL);
21131 /* Parse a functional cast to TYPE. Returns an expression
21132 representing the cast. */
21135 cp_parser_functional_cast (cp_parser* parser, tree type)
21138 tree expression_list;
21142 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21144 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21145 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21146 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21147 if (TREE_CODE (type) == TYPE_DECL)
21148 type = TREE_TYPE (type);
21149 return finish_compound_literal (type, expression_list,
21150 tf_warning_or_error);
21154 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21156 /*allow_expansion_p=*/true,
21157 /*non_constant_p=*/NULL);
21159 expression_list = error_mark_node;
21162 expression_list = build_tree_list_vec (vec);
21163 release_tree_vector (vec);
21166 cast = build_functional_cast (type, expression_list,
21167 tf_warning_or_error);
21168 /* [expr.const]/1: In an integral constant expression "only type
21169 conversions to integral or enumeration type can be used". */
21170 if (TREE_CODE (type) == TYPE_DECL)
21171 type = TREE_TYPE (type);
21172 if (cast != error_mark_node
21173 && !cast_valid_in_integral_constant_expression_p (type)
21174 && cp_parser_non_integral_constant_expression (parser,
21176 return error_mark_node;
21180 /* Save the tokens that make up the body of a member function defined
21181 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21182 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21183 specifiers applied to the declaration. Returns the FUNCTION_DECL
21184 for the member function. */
21187 cp_parser_save_member_function_body (cp_parser* parser,
21188 cp_decl_specifier_seq *decl_specifiers,
21189 cp_declarator *declarator,
21196 /* Create the FUNCTION_DECL. */
21197 fn = grokmethod (decl_specifiers, declarator, attributes);
21198 /* If something went badly wrong, bail out now. */
21199 if (fn == error_mark_node)
21201 /* If there's a function-body, skip it. */
21202 if (cp_parser_token_starts_function_definition_p
21203 (cp_lexer_peek_token (parser->lexer)))
21204 cp_parser_skip_to_end_of_block_or_statement (parser);
21205 return error_mark_node;
21208 /* Remember it, if there default args to post process. */
21209 cp_parser_save_default_args (parser, fn);
21211 /* Save away the tokens that make up the body of the
21213 first = parser->lexer->next_token;
21214 /* We can have braced-init-list mem-initializers before the fn body. */
21215 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21217 cp_lexer_consume_token (parser->lexer);
21218 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21219 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21221 /* cache_group will stop after an un-nested { } pair, too. */
21222 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21225 /* variadic mem-inits have ... after the ')'. */
21226 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21227 cp_lexer_consume_token (parser->lexer);
21230 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21231 /* Handle function try blocks. */
21232 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21233 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21234 last = parser->lexer->next_token;
21236 /* Save away the inline definition; we will process it when the
21237 class is complete. */
21238 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21239 DECL_PENDING_INLINE_P (fn) = 1;
21241 /* We need to know that this was defined in the class, so that
21242 friend templates are handled correctly. */
21243 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21245 /* Add FN to the queue of functions to be parsed later. */
21246 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21251 /* Save the tokens that make up the in-class initializer for a non-static
21252 data member. Returns a DEFAULT_ARG. */
21255 cp_parser_save_nsdmi (cp_parser* parser)
21257 /* Save away the tokens that make up the body of the
21259 cp_token *first = parser->lexer->next_token;
21263 /* Save tokens until the next comma or semicolon. */
21264 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21266 last = parser->lexer->next_token;
21268 node = make_node (DEFAULT_ARG);
21269 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21270 DEFARG_INSTANTIATIONS (node) = NULL;
21276 /* Parse a template-argument-list, as well as the trailing ">" (but
21277 not the opening "<"). See cp_parser_template_argument_list for the
21281 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21285 tree saved_qualifying_scope;
21286 tree saved_object_scope;
21287 bool saved_greater_than_is_operator_p;
21288 int saved_unevaluated_operand;
21289 int saved_inhibit_evaluation_warnings;
21293 When parsing a template-id, the first non-nested `>' is taken as
21294 the end of the template-argument-list rather than a greater-than
21296 saved_greater_than_is_operator_p
21297 = parser->greater_than_is_operator_p;
21298 parser->greater_than_is_operator_p = false;
21299 /* Parsing the argument list may modify SCOPE, so we save it
21301 saved_scope = parser->scope;
21302 saved_qualifying_scope = parser->qualifying_scope;
21303 saved_object_scope = parser->object_scope;
21304 /* We need to evaluate the template arguments, even though this
21305 template-id may be nested within a "sizeof". */
21306 saved_unevaluated_operand = cp_unevaluated_operand;
21307 cp_unevaluated_operand = 0;
21308 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21309 c_inhibit_evaluation_warnings = 0;
21310 /* Parse the template-argument-list itself. */
21311 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21312 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21313 arguments = NULL_TREE;
21315 arguments = cp_parser_template_argument_list (parser);
21316 /* Look for the `>' that ends the template-argument-list. If we find
21317 a '>>' instead, it's probably just a typo. */
21318 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21320 if (cxx_dialect != cxx98)
21322 /* In C++0x, a `>>' in a template argument list or cast
21323 expression is considered to be two separate `>'
21324 tokens. So, change the current token to a `>', but don't
21325 consume it: it will be consumed later when the outer
21326 template argument list (or cast expression) is parsed.
21327 Note that this replacement of `>' for `>>' is necessary
21328 even if we are parsing tentatively: in the tentative
21329 case, after calling
21330 cp_parser_enclosed_template_argument_list we will always
21331 throw away all of the template arguments and the first
21332 closing `>', either because the template argument list
21333 was erroneous or because we are replacing those tokens
21334 with a CPP_TEMPLATE_ID token. The second `>' (which will
21335 not have been thrown away) is needed either to close an
21336 outer template argument list or to complete a new-style
21338 cp_token *token = cp_lexer_peek_token (parser->lexer);
21339 token->type = CPP_GREATER;
21341 else if (!saved_greater_than_is_operator_p)
21343 /* If we're in a nested template argument list, the '>>' has
21344 to be a typo for '> >'. We emit the error message, but we
21345 continue parsing and we push a '>' as next token, so that
21346 the argument list will be parsed correctly. Note that the
21347 global source location is still on the token before the
21348 '>>', so we need to say explicitly where we want it. */
21349 cp_token *token = cp_lexer_peek_token (parser->lexer);
21350 error_at (token->location, "%<>>%> should be %<> >%> "
21351 "within a nested template argument list");
21353 token->type = CPP_GREATER;
21357 /* If this is not a nested template argument list, the '>>'
21358 is a typo for '>'. Emit an error message and continue.
21359 Same deal about the token location, but here we can get it
21360 right by consuming the '>>' before issuing the diagnostic. */
21361 cp_token *token = cp_lexer_consume_token (parser->lexer);
21362 error_at (token->location,
21363 "spurious %<>>%>, use %<>%> to terminate "
21364 "a template argument list");
21368 cp_parser_skip_to_end_of_template_parameter_list (parser);
21369 /* The `>' token might be a greater-than operator again now. */
21370 parser->greater_than_is_operator_p
21371 = saved_greater_than_is_operator_p;
21372 /* Restore the SAVED_SCOPE. */
21373 parser->scope = saved_scope;
21374 parser->qualifying_scope = saved_qualifying_scope;
21375 parser->object_scope = saved_object_scope;
21376 cp_unevaluated_operand = saved_unevaluated_operand;
21377 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21382 /* MEMBER_FUNCTION is a member function, or a friend. If default
21383 arguments, or the body of the function have not yet been parsed,
21387 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21389 timevar_push (TV_PARSE_INMETH);
21390 /* If this member is a template, get the underlying
21392 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21393 member_function = DECL_TEMPLATE_RESULT (member_function);
21395 /* There should not be any class definitions in progress at this
21396 point; the bodies of members are only parsed outside of all class
21398 gcc_assert (parser->num_classes_being_defined == 0);
21399 /* While we're parsing the member functions we might encounter more
21400 classes. We want to handle them right away, but we don't want
21401 them getting mixed up with functions that are currently in the
21403 push_unparsed_function_queues (parser);
21405 /* Make sure that any template parameters are in scope. */
21406 maybe_begin_member_template_processing (member_function);
21408 /* If the body of the function has not yet been parsed, parse it
21410 if (DECL_PENDING_INLINE_P (member_function))
21412 tree function_scope;
21413 cp_token_cache *tokens;
21415 /* The function is no longer pending; we are processing it. */
21416 tokens = DECL_PENDING_INLINE_INFO (member_function);
21417 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21418 DECL_PENDING_INLINE_P (member_function) = 0;
21420 /* If this is a local class, enter the scope of the containing
21422 function_scope = current_function_decl;
21423 if (function_scope)
21424 push_function_context ();
21426 /* Push the body of the function onto the lexer stack. */
21427 cp_parser_push_lexer_for_tokens (parser, tokens);
21429 /* Let the front end know that we going to be defining this
21431 start_preparsed_function (member_function, NULL_TREE,
21432 SF_PRE_PARSED | SF_INCLASS_INLINE);
21434 /* Don't do access checking if it is a templated function. */
21435 if (processing_template_decl)
21436 push_deferring_access_checks (dk_no_check);
21438 /* Now, parse the body of the function. */
21439 cp_parser_function_definition_after_declarator (parser,
21440 /*inline_p=*/true);
21442 if (processing_template_decl)
21443 pop_deferring_access_checks ();
21445 /* Leave the scope of the containing function. */
21446 if (function_scope)
21447 pop_function_context ();
21448 cp_parser_pop_lexer (parser);
21451 /* Remove any template parameters from the symbol table. */
21452 maybe_end_member_template_processing ();
21454 /* Restore the queue. */
21455 pop_unparsed_function_queues (parser);
21456 timevar_pop (TV_PARSE_INMETH);
21459 /* If DECL contains any default args, remember it on the unparsed
21460 functions queue. */
21463 cp_parser_save_default_args (cp_parser* parser, tree decl)
21467 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21469 probe = TREE_CHAIN (probe))
21470 if (TREE_PURPOSE (probe))
21472 cp_default_arg_entry *entry
21473 = VEC_safe_push (cp_default_arg_entry, gc,
21474 unparsed_funs_with_default_args, NULL);
21475 entry->class_type = current_class_type;
21476 entry->decl = decl;
21481 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21482 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21483 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21484 from the parameter-type-list. */
21487 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21488 tree default_arg, tree parmtype)
21490 cp_token_cache *tokens;
21494 /* Push the saved tokens for the default argument onto the parser's
21496 tokens = DEFARG_TOKENS (default_arg);
21497 cp_parser_push_lexer_for_tokens (parser, tokens);
21499 start_lambda_scope (decl);
21501 /* Parse the default argument. */
21502 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21503 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21504 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21506 finish_lambda_scope ();
21508 if (!processing_template_decl)
21510 /* In a non-template class, check conversions now. In a template,
21511 we'll wait and instantiate these as needed. */
21512 if (TREE_CODE (decl) == PARM_DECL)
21513 parsed_arg = check_default_argument (parmtype, parsed_arg);
21516 int flags = LOOKUP_IMPLICIT;
21517 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21518 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21519 flags = LOOKUP_NORMAL;
21520 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21524 /* If the token stream has not been completely used up, then
21525 there was extra junk after the end of the default
21527 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21529 if (TREE_CODE (decl) == PARM_DECL)
21530 cp_parser_error (parser, "expected %<,%>");
21532 cp_parser_error (parser, "expected %<;%>");
21535 /* Revert to the main lexer. */
21536 cp_parser_pop_lexer (parser);
21541 /* FIELD is a non-static data member with an initializer which we saved for
21542 later; parse it now. */
21545 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21549 push_unparsed_function_queues (parser);
21550 def = cp_parser_late_parse_one_default_arg (parser, field,
21551 DECL_INITIAL (field),
21553 pop_unparsed_function_queues (parser);
21555 DECL_INITIAL (field) = def;
21558 /* FN is a FUNCTION_DECL which may contains a parameter with an
21559 unparsed DEFAULT_ARG. Parse the default args now. This function
21560 assumes that the current scope is the scope in which the default
21561 argument should be processed. */
21564 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21566 bool saved_local_variables_forbidden_p;
21567 tree parm, parmdecl;
21569 /* While we're parsing the default args, we might (due to the
21570 statement expression extension) encounter more classes. We want
21571 to handle them right away, but we don't want them getting mixed
21572 up with default args that are currently in the queue. */
21573 push_unparsed_function_queues (parser);
21575 /* Local variable names (and the `this' keyword) may not appear
21576 in a default argument. */
21577 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21578 parser->local_variables_forbidden_p = true;
21580 push_defarg_context (fn);
21582 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21583 parmdecl = DECL_ARGUMENTS (fn);
21584 parm && parm != void_list_node;
21585 parm = TREE_CHAIN (parm),
21586 parmdecl = DECL_CHAIN (parmdecl))
21588 tree default_arg = TREE_PURPOSE (parm);
21590 VEC(tree,gc) *insts;
21597 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21598 /* This can happen for a friend declaration for a function
21599 already declared with default arguments. */
21603 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21605 TREE_VALUE (parm));
21606 if (parsed_arg == error_mark_node)
21611 TREE_PURPOSE (parm) = parsed_arg;
21613 /* Update any instantiations we've already created. */
21614 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21615 VEC_iterate (tree, insts, ix, copy); ix++)
21616 TREE_PURPOSE (copy) = parsed_arg;
21619 pop_defarg_context ();
21621 /* Make sure no default arg is missing. */
21622 check_default_args (fn);
21624 /* Restore the state of local_variables_forbidden_p. */
21625 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21627 /* Restore the queue. */
21628 pop_unparsed_function_queues (parser);
21631 /* Parse the operand of `sizeof' (or a similar operator). Returns
21632 either a TYPE or an expression, depending on the form of the
21633 input. The KEYWORD indicates which kind of expression we have
21637 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21639 tree expr = NULL_TREE;
21640 const char *saved_message;
21642 bool saved_integral_constant_expression_p;
21643 bool saved_non_integral_constant_expression_p;
21644 bool pack_expansion_p = false;
21646 /* Types cannot be defined in a `sizeof' expression. Save away the
21648 saved_message = parser->type_definition_forbidden_message;
21649 /* And create the new one. */
21650 tmp = concat ("types may not be defined in %<",
21651 IDENTIFIER_POINTER (ridpointers[keyword]),
21652 "%> expressions", NULL);
21653 parser->type_definition_forbidden_message = tmp;
21655 /* The restrictions on constant-expressions do not apply inside
21656 sizeof expressions. */
21657 saved_integral_constant_expression_p
21658 = parser->integral_constant_expression_p;
21659 saved_non_integral_constant_expression_p
21660 = parser->non_integral_constant_expression_p;
21661 parser->integral_constant_expression_p = false;
21663 /* If it's a `...', then we are computing the length of a parameter
21665 if (keyword == RID_SIZEOF
21666 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21668 /* Consume the `...'. */
21669 cp_lexer_consume_token (parser->lexer);
21670 maybe_warn_variadic_templates ();
21672 /* Note that this is an expansion. */
21673 pack_expansion_p = true;
21676 /* Do not actually evaluate the expression. */
21677 ++cp_unevaluated_operand;
21678 ++c_inhibit_evaluation_warnings;
21679 /* If it's a `(', then we might be looking at the type-id
21681 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21684 bool saved_in_type_id_in_expr_p;
21686 /* We can't be sure yet whether we're looking at a type-id or an
21688 cp_parser_parse_tentatively (parser);
21689 /* Consume the `('. */
21690 cp_lexer_consume_token (parser->lexer);
21691 /* Parse the type-id. */
21692 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21693 parser->in_type_id_in_expr_p = true;
21694 type = cp_parser_type_id (parser);
21695 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21696 /* Now, look for the trailing `)'. */
21697 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21698 /* If all went well, then we're done. */
21699 if (cp_parser_parse_definitely (parser))
21701 cp_decl_specifier_seq decl_specs;
21703 /* Build a trivial decl-specifier-seq. */
21704 clear_decl_specs (&decl_specs);
21705 decl_specs.type = type;
21707 /* Call grokdeclarator to figure out what type this is. */
21708 expr = grokdeclarator (NULL,
21712 /*attrlist=*/NULL);
21716 /* If the type-id production did not work out, then we must be
21717 looking at the unary-expression production. */
21719 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21720 /*cast_p=*/false, NULL);
21722 if (pack_expansion_p)
21723 /* Build a pack expansion. */
21724 expr = make_pack_expansion (expr);
21726 /* Go back to evaluating expressions. */
21727 --cp_unevaluated_operand;
21728 --c_inhibit_evaluation_warnings;
21730 /* Free the message we created. */
21732 /* And restore the old one. */
21733 parser->type_definition_forbidden_message = saved_message;
21734 parser->integral_constant_expression_p
21735 = saved_integral_constant_expression_p;
21736 parser->non_integral_constant_expression_p
21737 = saved_non_integral_constant_expression_p;
21742 /* If the current declaration has no declarator, return true. */
21745 cp_parser_declares_only_class_p (cp_parser *parser)
21747 /* If the next token is a `;' or a `,' then there is no
21749 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21750 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21753 /* Update the DECL_SPECS to reflect the storage class indicated by
21757 cp_parser_set_storage_class (cp_parser *parser,
21758 cp_decl_specifier_seq *decl_specs,
21760 location_t location)
21762 cp_storage_class storage_class;
21764 if (parser->in_unbraced_linkage_specification_p)
21766 error_at (location, "invalid use of %qD in linkage specification",
21767 ridpointers[keyword]);
21770 else if (decl_specs->storage_class != sc_none)
21772 decl_specs->conflicting_specifiers_p = true;
21776 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21777 && decl_specs->specs[(int) ds_thread])
21779 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21780 decl_specs->specs[(int) ds_thread] = 0;
21786 storage_class = sc_auto;
21789 storage_class = sc_register;
21792 storage_class = sc_static;
21795 storage_class = sc_extern;
21798 storage_class = sc_mutable;
21801 gcc_unreachable ();
21803 decl_specs->storage_class = storage_class;
21805 /* A storage class specifier cannot be applied alongside a typedef
21806 specifier. If there is a typedef specifier present then set
21807 conflicting_specifiers_p which will trigger an error later
21808 on in grokdeclarator. */
21809 if (decl_specs->specs[(int)ds_typedef])
21810 decl_specs->conflicting_specifiers_p = true;
21813 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
21814 is true, the type is a class or enum definition. */
21817 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21819 location_t location,
21820 bool type_definition_p)
21822 decl_specs->any_specifiers_p = true;
21824 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21825 (with, for example, in "typedef int wchar_t;") we remember that
21826 this is what happened. In system headers, we ignore these
21827 declarations so that G++ can work with system headers that are not
21829 if (decl_specs->specs[(int) ds_typedef]
21830 && !type_definition_p
21831 && (type_spec == boolean_type_node
21832 || type_spec == char16_type_node
21833 || type_spec == char32_type_node
21834 || type_spec == wchar_type_node)
21835 && (decl_specs->type
21836 || decl_specs->specs[(int) ds_long]
21837 || decl_specs->specs[(int) ds_short]
21838 || decl_specs->specs[(int) ds_unsigned]
21839 || decl_specs->specs[(int) ds_signed]))
21841 decl_specs->redefined_builtin_type = type_spec;
21842 if (!decl_specs->type)
21844 decl_specs->type = type_spec;
21845 decl_specs->type_definition_p = false;
21846 decl_specs->type_location = location;
21849 else if (decl_specs->type)
21850 decl_specs->multiple_types_p = true;
21853 decl_specs->type = type_spec;
21854 decl_specs->type_definition_p = type_definition_p;
21855 decl_specs->redefined_builtin_type = NULL_TREE;
21856 decl_specs->type_location = location;
21860 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21861 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
21864 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21866 return decl_specifiers->specs[(int) ds_friend] != 0;
21869 /* Issue an error message indicating that TOKEN_DESC was expected.
21870 If KEYWORD is true, it indicated this function is called by
21871 cp_parser_require_keword and the required token can only be
21872 a indicated keyword. */
21875 cp_parser_required_error (cp_parser *parser,
21876 required_token token_desc,
21879 switch (token_desc)
21882 cp_parser_error (parser, "expected %<new%>");
21885 cp_parser_error (parser, "expected %<delete%>");
21888 cp_parser_error (parser, "expected %<return%>");
21891 cp_parser_error (parser, "expected %<while%>");
21894 cp_parser_error (parser, "expected %<extern%>");
21896 case RT_STATIC_ASSERT:
21897 cp_parser_error (parser, "expected %<static_assert%>");
21900 cp_parser_error (parser, "expected %<decltype%>");
21903 cp_parser_error (parser, "expected %<operator%>");
21906 cp_parser_error (parser, "expected %<class%>");
21909 cp_parser_error (parser, "expected %<template%>");
21912 cp_parser_error (parser, "expected %<namespace%>");
21915 cp_parser_error (parser, "expected %<using%>");
21918 cp_parser_error (parser, "expected %<asm%>");
21921 cp_parser_error (parser, "expected %<try%>");
21924 cp_parser_error (parser, "expected %<catch%>");
21927 cp_parser_error (parser, "expected %<throw%>");
21930 cp_parser_error (parser, "expected %<__label__%>");
21933 cp_parser_error (parser, "expected %<@try%>");
21935 case RT_AT_SYNCHRONIZED:
21936 cp_parser_error (parser, "expected %<@synchronized%>");
21939 cp_parser_error (parser, "expected %<@throw%>");
21946 switch (token_desc)
21949 cp_parser_error (parser, "expected %<;%>");
21951 case RT_OPEN_PAREN:
21952 cp_parser_error (parser, "expected %<(%>");
21954 case RT_CLOSE_BRACE:
21955 cp_parser_error (parser, "expected %<}%>");
21957 case RT_OPEN_BRACE:
21958 cp_parser_error (parser, "expected %<{%>");
21960 case RT_CLOSE_SQUARE:
21961 cp_parser_error (parser, "expected %<]%>");
21963 case RT_OPEN_SQUARE:
21964 cp_parser_error (parser, "expected %<[%>");
21967 cp_parser_error (parser, "expected %<,%>");
21970 cp_parser_error (parser, "expected %<::%>");
21973 cp_parser_error (parser, "expected %<<%>");
21976 cp_parser_error (parser, "expected %<>%>");
21979 cp_parser_error (parser, "expected %<=%>");
21982 cp_parser_error (parser, "expected %<...%>");
21985 cp_parser_error (parser, "expected %<*%>");
21988 cp_parser_error (parser, "expected %<~%>");
21991 cp_parser_error (parser, "expected %<:%>");
21993 case RT_COLON_SCOPE:
21994 cp_parser_error (parser, "expected %<:%> or %<::%>");
21996 case RT_CLOSE_PAREN:
21997 cp_parser_error (parser, "expected %<)%>");
21999 case RT_COMMA_CLOSE_PAREN:
22000 cp_parser_error (parser, "expected %<,%> or %<)%>");
22002 case RT_PRAGMA_EOL:
22003 cp_parser_error (parser, "expected end of line");
22006 cp_parser_error (parser, "expected identifier");
22009 cp_parser_error (parser, "expected selection-statement");
22011 case RT_INTERATION:
22012 cp_parser_error (parser, "expected iteration-statement");
22015 cp_parser_error (parser, "expected jump-statement");
22018 cp_parser_error (parser, "expected class-key");
22020 case RT_CLASS_TYPENAME_TEMPLATE:
22021 cp_parser_error (parser,
22022 "expected %<class%>, %<typename%>, or %<template%>");
22025 gcc_unreachable ();
22029 gcc_unreachable ();
22034 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22035 issue an error message indicating that TOKEN_DESC was expected.
22037 Returns the token consumed, if the token had the appropriate type.
22038 Otherwise, returns NULL. */
22041 cp_parser_require (cp_parser* parser,
22042 enum cpp_ttype type,
22043 required_token token_desc)
22045 if (cp_lexer_next_token_is (parser->lexer, type))
22046 return cp_lexer_consume_token (parser->lexer);
22049 /* Output the MESSAGE -- unless we're parsing tentatively. */
22050 if (!cp_parser_simulate_error (parser))
22051 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22056 /* An error message is produced if the next token is not '>'.
22057 All further tokens are skipped until the desired token is
22058 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22061 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22063 /* Current level of '< ... >'. */
22064 unsigned level = 0;
22065 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22066 unsigned nesting_depth = 0;
22068 /* Are we ready, yet? If not, issue error message. */
22069 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22072 /* Skip tokens until the desired token is found. */
22075 /* Peek at the next token. */
22076 switch (cp_lexer_peek_token (parser->lexer)->type)
22079 if (!nesting_depth)
22084 if (cxx_dialect == cxx98)
22085 /* C++0x views the `>>' operator as two `>' tokens, but
22088 else if (!nesting_depth && level-- == 0)
22090 /* We've hit a `>>' where the first `>' closes the
22091 template argument list, and the second `>' is
22092 spurious. Just consume the `>>' and stop; we've
22093 already produced at least one error. */
22094 cp_lexer_consume_token (parser->lexer);
22097 /* Fall through for C++0x, so we handle the second `>' in
22101 if (!nesting_depth && level-- == 0)
22103 /* We've reached the token we want, consume it and stop. */
22104 cp_lexer_consume_token (parser->lexer);
22109 case CPP_OPEN_PAREN:
22110 case CPP_OPEN_SQUARE:
22114 case CPP_CLOSE_PAREN:
22115 case CPP_CLOSE_SQUARE:
22116 if (nesting_depth-- == 0)
22121 case CPP_PRAGMA_EOL:
22122 case CPP_SEMICOLON:
22123 case CPP_OPEN_BRACE:
22124 case CPP_CLOSE_BRACE:
22125 /* The '>' was probably forgotten, don't look further. */
22132 /* Consume this token. */
22133 cp_lexer_consume_token (parser->lexer);
22137 /* If the next token is the indicated keyword, consume it. Otherwise,
22138 issue an error message indicating that TOKEN_DESC was expected.
22140 Returns the token consumed, if the token had the appropriate type.
22141 Otherwise, returns NULL. */
22144 cp_parser_require_keyword (cp_parser* parser,
22146 required_token token_desc)
22148 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22150 if (token && token->keyword != keyword)
22152 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22159 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22160 function-definition. */
22163 cp_parser_token_starts_function_definition_p (cp_token* token)
22165 return (/* An ordinary function-body begins with an `{'. */
22166 token->type == CPP_OPEN_BRACE
22167 /* A ctor-initializer begins with a `:'. */
22168 || token->type == CPP_COLON
22169 /* A function-try-block begins with `try'. */
22170 || token->keyword == RID_TRY
22171 /* The named return value extension begins with `return'. */
22172 || token->keyword == RID_RETURN);
22175 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22179 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22183 token = cp_lexer_peek_token (parser->lexer);
22184 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22187 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22188 C++0x) ending a template-argument. */
22191 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22195 token = cp_lexer_peek_token (parser->lexer);
22196 return (token->type == CPP_COMMA
22197 || token->type == CPP_GREATER
22198 || token->type == CPP_ELLIPSIS
22199 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22202 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22203 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22206 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22211 token = cp_lexer_peek_nth_token (parser->lexer, n);
22212 if (token->type == CPP_LESS)
22214 /* Check for the sequence `<::' in the original code. It would be lexed as
22215 `[:', where `[' is a digraph, and there is no whitespace before
22217 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22220 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22221 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22227 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22228 or none_type otherwise. */
22230 static enum tag_types
22231 cp_parser_token_is_class_key (cp_token* token)
22233 switch (token->keyword)
22238 return record_type;
22247 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22250 cp_parser_check_class_key (enum tag_types class_key, tree type)
22252 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22253 permerror (input_location, "%qs tag used in naming %q#T",
22254 class_key == union_type ? "union"
22255 : class_key == record_type ? "struct" : "class",
22259 /* Issue an error message if DECL is redeclared with different
22260 access than its original declaration [class.access.spec/3].
22261 This applies to nested classes and nested class templates.
22265 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22267 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22270 if ((TREE_PRIVATE (decl)
22271 != (current_access_specifier == access_private_node))
22272 || (TREE_PROTECTED (decl)
22273 != (current_access_specifier == access_protected_node)))
22274 error_at (location, "%qD redeclared with different access", decl);
22277 /* Look for the `template' keyword, as a syntactic disambiguator.
22278 Return TRUE iff it is present, in which case it will be
22282 cp_parser_optional_template_keyword (cp_parser *parser)
22284 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22286 /* The `template' keyword can only be used within templates;
22287 outside templates the parser can always figure out what is a
22288 template and what is not. */
22289 if (!processing_template_decl)
22291 cp_token *token = cp_lexer_peek_token (parser->lexer);
22292 error_at (token->location,
22293 "%<template%> (as a disambiguator) is only allowed "
22294 "within templates");
22295 /* If this part of the token stream is rescanned, the same
22296 error message would be generated. So, we purge the token
22297 from the stream. */
22298 cp_lexer_purge_token (parser->lexer);
22303 /* Consume the `template' keyword. */
22304 cp_lexer_consume_token (parser->lexer);
22312 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22313 set PARSER->SCOPE, and perform other related actions. */
22316 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22319 struct tree_check *check_value;
22320 deferred_access_check *chk;
22321 VEC (deferred_access_check,gc) *checks;
22323 /* Get the stored value. */
22324 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22325 /* Perform any access checks that were deferred. */
22326 checks = check_value->checks;
22329 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22330 perform_or_defer_access_check (chk->binfo,
22334 /* Set the scope from the stored value. */
22335 parser->scope = check_value->value;
22336 parser->qualifying_scope = check_value->qualifying_scope;
22337 parser->object_scope = NULL_TREE;
22340 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22341 encounter the end of a block before what we were looking for. */
22344 cp_parser_cache_group (cp_parser *parser,
22345 enum cpp_ttype end,
22350 cp_token *token = cp_lexer_peek_token (parser->lexer);
22352 /* Abort a parenthesized expression if we encounter a semicolon. */
22353 if ((end == CPP_CLOSE_PAREN || depth == 0)
22354 && token->type == CPP_SEMICOLON)
22356 /* If we've reached the end of the file, stop. */
22357 if (token->type == CPP_EOF
22358 || (end != CPP_PRAGMA_EOL
22359 && token->type == CPP_PRAGMA_EOL))
22361 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22362 /* We've hit the end of an enclosing block, so there's been some
22363 kind of syntax error. */
22366 /* If we're caching something finished by a comma (or semicolon),
22367 such as an NSDMI, don't consume the comma. */
22368 if (end == CPP_COMMA
22369 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22372 /* Consume the token. */
22373 cp_lexer_consume_token (parser->lexer);
22374 /* See if it starts a new group. */
22375 if (token->type == CPP_OPEN_BRACE)
22377 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22378 /* In theory this should probably check end == '}', but
22379 cp_parser_save_member_function_body needs it to exit
22380 after either '}' or ')' when called with ')'. */
22384 else if (token->type == CPP_OPEN_PAREN)
22386 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22387 if (depth == 0 && end == CPP_CLOSE_PAREN)
22390 else if (token->type == CPP_PRAGMA)
22391 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22392 else if (token->type == end)
22397 /* Begin parsing tentatively. We always save tokens while parsing
22398 tentatively so that if the tentative parsing fails we can restore the
22402 cp_parser_parse_tentatively (cp_parser* parser)
22404 /* Enter a new parsing context. */
22405 parser->context = cp_parser_context_new (parser->context);
22406 /* Begin saving tokens. */
22407 cp_lexer_save_tokens (parser->lexer);
22408 /* In order to avoid repetitive access control error messages,
22409 access checks are queued up until we are no longer parsing
22411 push_deferring_access_checks (dk_deferred);
22414 /* Commit to the currently active tentative parse. */
22417 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22419 cp_parser_context *context;
22422 /* Mark all of the levels as committed. */
22423 lexer = parser->lexer;
22424 for (context = parser->context; context->next; context = context->next)
22426 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22428 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22429 while (!cp_lexer_saving_tokens (lexer))
22430 lexer = lexer->next;
22431 cp_lexer_commit_tokens (lexer);
22435 /* Abort the currently active tentative parse. All consumed tokens
22436 will be rolled back, and no diagnostics will be issued. */
22439 cp_parser_abort_tentative_parse (cp_parser* parser)
22441 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22442 || errorcount > 0);
22443 cp_parser_simulate_error (parser);
22444 /* Now, pretend that we want to see if the construct was
22445 successfully parsed. */
22446 cp_parser_parse_definitely (parser);
22449 /* Stop parsing tentatively. If a parse error has occurred, restore the
22450 token stream. Otherwise, commit to the tokens we have consumed.
22451 Returns true if no error occurred; false otherwise. */
22454 cp_parser_parse_definitely (cp_parser* parser)
22456 bool error_occurred;
22457 cp_parser_context *context;
22459 /* Remember whether or not an error occurred, since we are about to
22460 destroy that information. */
22461 error_occurred = cp_parser_error_occurred (parser);
22462 /* Remove the topmost context from the stack. */
22463 context = parser->context;
22464 parser->context = context->next;
22465 /* If no parse errors occurred, commit to the tentative parse. */
22466 if (!error_occurred)
22468 /* Commit to the tokens read tentatively, unless that was
22470 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22471 cp_lexer_commit_tokens (parser->lexer);
22473 pop_to_parent_deferring_access_checks ();
22475 /* Otherwise, if errors occurred, roll back our state so that things
22476 are just as they were before we began the tentative parse. */
22479 cp_lexer_rollback_tokens (parser->lexer);
22480 pop_deferring_access_checks ();
22482 /* Add the context to the front of the free list. */
22483 context->next = cp_parser_context_free_list;
22484 cp_parser_context_free_list = context;
22486 return !error_occurred;
22489 /* Returns true if we are parsing tentatively and are not committed to
22490 this tentative parse. */
22493 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22495 return (cp_parser_parsing_tentatively (parser)
22496 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22499 /* Returns nonzero iff an error has occurred during the most recent
22500 tentative parse. */
22503 cp_parser_error_occurred (cp_parser* parser)
22505 return (cp_parser_parsing_tentatively (parser)
22506 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22509 /* Returns nonzero if GNU extensions are allowed. */
22512 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22514 return parser->allow_gnu_extensions_p;
22517 /* Objective-C++ Productions */
22520 /* Parse an Objective-C expression, which feeds into a primary-expression
22524 objc-message-expression
22525 objc-string-literal
22526 objc-encode-expression
22527 objc-protocol-expression
22528 objc-selector-expression
22530 Returns a tree representation of the expression. */
22533 cp_parser_objc_expression (cp_parser* parser)
22535 /* Try to figure out what kind of declaration is present. */
22536 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22540 case CPP_OPEN_SQUARE:
22541 return cp_parser_objc_message_expression (parser);
22543 case CPP_OBJC_STRING:
22544 kwd = cp_lexer_consume_token (parser->lexer);
22545 return objc_build_string_object (kwd->u.value);
22548 switch (kwd->keyword)
22550 case RID_AT_ENCODE:
22551 return cp_parser_objc_encode_expression (parser);
22553 case RID_AT_PROTOCOL:
22554 return cp_parser_objc_protocol_expression (parser);
22556 case RID_AT_SELECTOR:
22557 return cp_parser_objc_selector_expression (parser);
22563 error_at (kwd->location,
22564 "misplaced %<@%D%> Objective-C++ construct",
22566 cp_parser_skip_to_end_of_block_or_statement (parser);
22569 return error_mark_node;
22572 /* Parse an Objective-C message expression.
22574 objc-message-expression:
22575 [ objc-message-receiver objc-message-args ]
22577 Returns a representation of an Objective-C message. */
22580 cp_parser_objc_message_expression (cp_parser* parser)
22582 tree receiver, messageargs;
22584 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22585 receiver = cp_parser_objc_message_receiver (parser);
22586 messageargs = cp_parser_objc_message_args (parser);
22587 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22589 return objc_build_message_expr (receiver, messageargs);
22592 /* Parse an objc-message-receiver.
22594 objc-message-receiver:
22596 simple-type-specifier
22598 Returns a representation of the type or expression. */
22601 cp_parser_objc_message_receiver (cp_parser* parser)
22605 /* An Objective-C message receiver may be either (1) a type
22606 or (2) an expression. */
22607 cp_parser_parse_tentatively (parser);
22608 rcv = cp_parser_expression (parser, false, NULL);
22610 if (cp_parser_parse_definitely (parser))
22613 rcv = cp_parser_simple_type_specifier (parser,
22614 /*decl_specs=*/NULL,
22615 CP_PARSER_FLAGS_NONE);
22617 return objc_get_class_reference (rcv);
22620 /* Parse the arguments and selectors comprising an Objective-C message.
22625 objc-selector-args , objc-comma-args
22627 objc-selector-args:
22628 objc-selector [opt] : assignment-expression
22629 objc-selector-args objc-selector [opt] : assignment-expression
22632 assignment-expression
22633 objc-comma-args , assignment-expression
22635 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22636 selector arguments and TREE_VALUE containing a list of comma
22640 cp_parser_objc_message_args (cp_parser* parser)
22642 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22643 bool maybe_unary_selector_p = true;
22644 cp_token *token = cp_lexer_peek_token (parser->lexer);
22646 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22648 tree selector = NULL_TREE, arg;
22650 if (token->type != CPP_COLON)
22651 selector = cp_parser_objc_selector (parser);
22653 /* Detect if we have a unary selector. */
22654 if (maybe_unary_selector_p
22655 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22656 return build_tree_list (selector, NULL_TREE);
22658 maybe_unary_selector_p = false;
22659 cp_parser_require (parser, CPP_COLON, RT_COLON);
22660 arg = cp_parser_assignment_expression (parser, false, NULL);
22663 = chainon (sel_args,
22664 build_tree_list (selector, arg));
22666 token = cp_lexer_peek_token (parser->lexer);
22669 /* Handle non-selector arguments, if any. */
22670 while (token->type == CPP_COMMA)
22674 cp_lexer_consume_token (parser->lexer);
22675 arg = cp_parser_assignment_expression (parser, false, NULL);
22678 = chainon (addl_args,
22679 build_tree_list (NULL_TREE, arg));
22681 token = cp_lexer_peek_token (parser->lexer);
22684 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22686 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22687 return build_tree_list (error_mark_node, error_mark_node);
22690 return build_tree_list (sel_args, addl_args);
22693 /* Parse an Objective-C encode expression.
22695 objc-encode-expression:
22696 @encode objc-typename
22698 Returns an encoded representation of the type argument. */
22701 cp_parser_objc_encode_expression (cp_parser* parser)
22706 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22707 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22708 token = cp_lexer_peek_token (parser->lexer);
22709 type = complete_type (cp_parser_type_id (parser));
22710 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22714 error_at (token->location,
22715 "%<@encode%> must specify a type as an argument");
22716 return error_mark_node;
22719 /* This happens if we find @encode(T) (where T is a template
22720 typename or something dependent on a template typename) when
22721 parsing a template. In that case, we can't compile it
22722 immediately, but we rather create an AT_ENCODE_EXPR which will
22723 need to be instantiated when the template is used.
22725 if (dependent_type_p (type))
22727 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22728 TREE_READONLY (value) = 1;
22732 return objc_build_encode_expr (type);
22735 /* Parse an Objective-C @defs expression. */
22738 cp_parser_objc_defs_expression (cp_parser *parser)
22742 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22744 name = cp_parser_identifier (parser);
22745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22747 return objc_get_class_ivars (name);
22750 /* Parse an Objective-C protocol expression.
22752 objc-protocol-expression:
22753 @protocol ( identifier )
22755 Returns a representation of the protocol expression. */
22758 cp_parser_objc_protocol_expression (cp_parser* parser)
22762 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22763 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22764 proto = cp_parser_identifier (parser);
22765 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22767 return objc_build_protocol_expr (proto);
22770 /* Parse an Objective-C selector expression.
22772 objc-selector-expression:
22773 @selector ( objc-method-signature )
22775 objc-method-signature:
22781 objc-selector-seq objc-selector :
22783 Returns a representation of the method selector. */
22786 cp_parser_objc_selector_expression (cp_parser* parser)
22788 tree sel_seq = NULL_TREE;
22789 bool maybe_unary_selector_p = true;
22791 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22793 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
22794 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22795 token = cp_lexer_peek_token (parser->lexer);
22797 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22798 || token->type == CPP_SCOPE)
22800 tree selector = NULL_TREE;
22802 if (token->type != CPP_COLON
22803 || token->type == CPP_SCOPE)
22804 selector = cp_parser_objc_selector (parser);
22806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22807 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22809 /* Detect if we have a unary selector. */
22810 if (maybe_unary_selector_p)
22812 sel_seq = selector;
22813 goto finish_selector;
22817 cp_parser_error (parser, "expected %<:%>");
22820 maybe_unary_selector_p = false;
22821 token = cp_lexer_consume_token (parser->lexer);
22823 if (token->type == CPP_SCOPE)
22826 = chainon (sel_seq,
22827 build_tree_list (selector, NULL_TREE));
22829 = chainon (sel_seq,
22830 build_tree_list (NULL_TREE, NULL_TREE));
22834 = chainon (sel_seq,
22835 build_tree_list (selector, NULL_TREE));
22837 token = cp_lexer_peek_token (parser->lexer);
22841 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22843 return objc_build_selector_expr (loc, sel_seq);
22846 /* Parse a list of identifiers.
22848 objc-identifier-list:
22850 objc-identifier-list , identifier
22852 Returns a TREE_LIST of identifier nodes. */
22855 cp_parser_objc_identifier_list (cp_parser* parser)
22861 identifier = cp_parser_identifier (parser);
22862 if (identifier == error_mark_node)
22863 return error_mark_node;
22865 list = build_tree_list (NULL_TREE, identifier);
22866 sep = cp_lexer_peek_token (parser->lexer);
22868 while (sep->type == CPP_COMMA)
22870 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22871 identifier = cp_parser_identifier (parser);
22872 if (identifier == error_mark_node)
22875 list = chainon (list, build_tree_list (NULL_TREE,
22877 sep = cp_lexer_peek_token (parser->lexer);
22883 /* Parse an Objective-C alias declaration.
22885 objc-alias-declaration:
22886 @compatibility_alias identifier identifier ;
22888 This function registers the alias mapping with the Objective-C front end.
22889 It returns nothing. */
22892 cp_parser_objc_alias_declaration (cp_parser* parser)
22896 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
22897 alias = cp_parser_identifier (parser);
22898 orig = cp_parser_identifier (parser);
22899 objc_declare_alias (alias, orig);
22900 cp_parser_consume_semicolon_at_end_of_statement (parser);
22903 /* Parse an Objective-C class forward-declaration.
22905 objc-class-declaration:
22906 @class objc-identifier-list ;
22908 The function registers the forward declarations with the Objective-C
22909 front end. It returns nothing. */
22912 cp_parser_objc_class_declaration (cp_parser* parser)
22914 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
22919 id = cp_parser_identifier (parser);
22920 if (id == error_mark_node)
22923 objc_declare_class (id);
22925 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22926 cp_lexer_consume_token (parser->lexer);
22930 cp_parser_consume_semicolon_at_end_of_statement (parser);
22933 /* Parse a list of Objective-C protocol references.
22935 objc-protocol-refs-opt:
22936 objc-protocol-refs [opt]
22938 objc-protocol-refs:
22939 < objc-identifier-list >
22941 Returns a TREE_LIST of identifiers, if any. */
22944 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22946 tree protorefs = NULL_TREE;
22948 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22950 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
22951 protorefs = cp_parser_objc_identifier_list (parser);
22952 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22958 /* Parse a Objective-C visibility specification. */
22961 cp_parser_objc_visibility_spec (cp_parser* parser)
22963 cp_token *vis = cp_lexer_peek_token (parser->lexer);
22965 switch (vis->keyword)
22967 case RID_AT_PRIVATE:
22968 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22970 case RID_AT_PROTECTED:
22971 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22973 case RID_AT_PUBLIC:
22974 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22976 case RID_AT_PACKAGE:
22977 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22983 /* Eat '@private'/'@protected'/'@public'. */
22984 cp_lexer_consume_token (parser->lexer);
22987 /* Parse an Objective-C method type. Return 'true' if it is a class
22988 (+) method, and 'false' if it is an instance (-) method. */
22991 cp_parser_objc_method_type (cp_parser* parser)
22993 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22999 /* Parse an Objective-C protocol qualifier. */
23002 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23004 tree quals = NULL_TREE, node;
23005 cp_token *token = cp_lexer_peek_token (parser->lexer);
23007 node = token->u.value;
23009 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23010 && (node == ridpointers [(int) RID_IN]
23011 || node == ridpointers [(int) RID_OUT]
23012 || node == ridpointers [(int) RID_INOUT]
23013 || node == ridpointers [(int) RID_BYCOPY]
23014 || node == ridpointers [(int) RID_BYREF]
23015 || node == ridpointers [(int) RID_ONEWAY]))
23017 quals = tree_cons (NULL_TREE, node, quals);
23018 cp_lexer_consume_token (parser->lexer);
23019 token = cp_lexer_peek_token (parser->lexer);
23020 node = token->u.value;
23026 /* Parse an Objective-C typename. */
23029 cp_parser_objc_typename (cp_parser* parser)
23031 tree type_name = NULL_TREE;
23033 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23035 tree proto_quals, cp_type = NULL_TREE;
23037 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23038 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23040 /* An ObjC type name may consist of just protocol qualifiers, in which
23041 case the type shall default to 'id'. */
23042 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23044 cp_type = cp_parser_type_id (parser);
23046 /* If the type could not be parsed, an error has already
23047 been produced. For error recovery, behave as if it had
23048 not been specified, which will use the default type
23050 if (cp_type == error_mark_node)
23052 cp_type = NULL_TREE;
23053 /* We need to skip to the closing parenthesis as
23054 cp_parser_type_id() does not seem to do it for
23056 cp_parser_skip_to_closing_parenthesis (parser,
23057 /*recovering=*/true,
23058 /*or_comma=*/false,
23059 /*consume_paren=*/false);
23063 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23064 type_name = build_tree_list (proto_quals, cp_type);
23070 /* Check to see if TYPE refers to an Objective-C selector name. */
23073 cp_parser_objc_selector_p (enum cpp_ttype type)
23075 return (type == CPP_NAME || type == CPP_KEYWORD
23076 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23077 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23078 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23079 || type == CPP_XOR || type == CPP_XOR_EQ);
23082 /* Parse an Objective-C selector. */
23085 cp_parser_objc_selector (cp_parser* parser)
23087 cp_token *token = cp_lexer_consume_token (parser->lexer);
23089 if (!cp_parser_objc_selector_p (token->type))
23091 error_at (token->location, "invalid Objective-C++ selector name");
23092 return error_mark_node;
23095 /* C++ operator names are allowed to appear in ObjC selectors. */
23096 switch (token->type)
23098 case CPP_AND_AND: return get_identifier ("and");
23099 case CPP_AND_EQ: return get_identifier ("and_eq");
23100 case CPP_AND: return get_identifier ("bitand");
23101 case CPP_OR: return get_identifier ("bitor");
23102 case CPP_COMPL: return get_identifier ("compl");
23103 case CPP_NOT: return get_identifier ("not");
23104 case CPP_NOT_EQ: return get_identifier ("not_eq");
23105 case CPP_OR_OR: return get_identifier ("or");
23106 case CPP_OR_EQ: return get_identifier ("or_eq");
23107 case CPP_XOR: return get_identifier ("xor");
23108 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23109 default: return token->u.value;
23113 /* Parse an Objective-C params list. */
23116 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23118 tree params = NULL_TREE;
23119 bool maybe_unary_selector_p = true;
23120 cp_token *token = cp_lexer_peek_token (parser->lexer);
23122 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23124 tree selector = NULL_TREE, type_name, identifier;
23125 tree parm_attr = NULL_TREE;
23127 if (token->keyword == RID_ATTRIBUTE)
23130 if (token->type != CPP_COLON)
23131 selector = cp_parser_objc_selector (parser);
23133 /* Detect if we have a unary selector. */
23134 if (maybe_unary_selector_p
23135 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23137 params = selector; /* Might be followed by attributes. */
23141 maybe_unary_selector_p = false;
23142 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23144 /* Something went quite wrong. There should be a colon
23145 here, but there is not. Stop parsing parameters. */
23148 type_name = cp_parser_objc_typename (parser);
23149 /* New ObjC allows attributes on parameters too. */
23150 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23151 parm_attr = cp_parser_attributes_opt (parser);
23152 identifier = cp_parser_identifier (parser);
23156 objc_build_keyword_decl (selector,
23161 token = cp_lexer_peek_token (parser->lexer);
23164 if (params == NULL_TREE)
23166 cp_parser_error (parser, "objective-c++ method declaration is expected");
23167 return error_mark_node;
23170 /* We allow tail attributes for the method. */
23171 if (token->keyword == RID_ATTRIBUTE)
23173 *attributes = cp_parser_attributes_opt (parser);
23174 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23175 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23177 cp_parser_error (parser,
23178 "method attributes must be specified at the end");
23179 return error_mark_node;
23182 if (params == NULL_TREE)
23184 cp_parser_error (parser, "objective-c++ method declaration is expected");
23185 return error_mark_node;
23190 /* Parse the non-keyword Objective-C params. */
23193 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23196 tree params = make_node (TREE_LIST);
23197 cp_token *token = cp_lexer_peek_token (parser->lexer);
23198 *ellipsisp = false; /* Initially, assume no ellipsis. */
23200 while (token->type == CPP_COMMA)
23202 cp_parameter_declarator *parmdecl;
23205 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23206 token = cp_lexer_peek_token (parser->lexer);
23208 if (token->type == CPP_ELLIPSIS)
23210 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23212 token = cp_lexer_peek_token (parser->lexer);
23216 /* TODO: parse attributes for tail parameters. */
23217 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23218 parm = grokdeclarator (parmdecl->declarator,
23219 &parmdecl->decl_specifiers,
23220 PARM, /*initialized=*/0,
23221 /*attrlist=*/NULL);
23223 chainon (params, build_tree_list (NULL_TREE, parm));
23224 token = cp_lexer_peek_token (parser->lexer);
23227 /* We allow tail attributes for the method. */
23228 if (token->keyword == RID_ATTRIBUTE)
23230 if (*attributes == NULL_TREE)
23232 *attributes = cp_parser_attributes_opt (parser);
23233 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23234 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23238 /* We have an error, but parse the attributes, so that we can
23240 *attributes = cp_parser_attributes_opt (parser);
23242 cp_parser_error (parser,
23243 "method attributes must be specified at the end");
23244 return error_mark_node;
23250 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23253 cp_parser_objc_interstitial_code (cp_parser* parser)
23255 cp_token *token = cp_lexer_peek_token (parser->lexer);
23257 /* If the next token is `extern' and the following token is a string
23258 literal, then we have a linkage specification. */
23259 if (token->keyword == RID_EXTERN
23260 && cp_parser_is_pure_string_literal
23261 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23262 cp_parser_linkage_specification (parser);
23263 /* Handle #pragma, if any. */
23264 else if (token->type == CPP_PRAGMA)
23265 cp_parser_pragma (parser, pragma_external);
23266 /* Allow stray semicolons. */
23267 else if (token->type == CPP_SEMICOLON)
23268 cp_lexer_consume_token (parser->lexer);
23269 /* Mark methods as optional or required, when building protocols. */
23270 else if (token->keyword == RID_AT_OPTIONAL)
23272 cp_lexer_consume_token (parser->lexer);
23273 objc_set_method_opt (true);
23275 else if (token->keyword == RID_AT_REQUIRED)
23277 cp_lexer_consume_token (parser->lexer);
23278 objc_set_method_opt (false);
23280 else if (token->keyword == RID_NAMESPACE)
23281 cp_parser_namespace_definition (parser);
23282 /* Other stray characters must generate errors. */
23283 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23285 cp_lexer_consume_token (parser->lexer);
23286 error ("stray %qs between Objective-C++ methods",
23287 token->type == CPP_OPEN_BRACE ? "{" : "}");
23289 /* Finally, try to parse a block-declaration, or a function-definition. */
23291 cp_parser_block_declaration (parser, /*statement_p=*/false);
23294 /* Parse a method signature. */
23297 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23299 tree rettype, kwdparms, optparms;
23300 bool ellipsis = false;
23301 bool is_class_method;
23303 is_class_method = cp_parser_objc_method_type (parser);
23304 rettype = cp_parser_objc_typename (parser);
23305 *attributes = NULL_TREE;
23306 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23307 if (kwdparms == error_mark_node)
23308 return error_mark_node;
23309 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23310 if (optparms == error_mark_node)
23311 return error_mark_node;
23313 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23317 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23320 cp_lexer_save_tokens (parser->lexer);
23321 tattr = cp_parser_attributes_opt (parser);
23322 gcc_assert (tattr) ;
23324 /* If the attributes are followed by a method introducer, this is not allowed.
23325 Dump the attributes and flag the situation. */
23326 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23327 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23330 /* Otherwise, the attributes introduce some interstitial code, possibly so
23331 rewind to allow that check. */
23332 cp_lexer_rollback_tokens (parser->lexer);
23336 /* Parse an Objective-C method prototype list. */
23339 cp_parser_objc_method_prototype_list (cp_parser* parser)
23341 cp_token *token = cp_lexer_peek_token (parser->lexer);
23343 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23345 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23347 tree attributes, sig;
23348 bool is_class_method;
23349 if (token->type == CPP_PLUS)
23350 is_class_method = true;
23352 is_class_method = false;
23353 sig = cp_parser_objc_method_signature (parser, &attributes);
23354 if (sig == error_mark_node)
23356 cp_parser_skip_to_end_of_block_or_statement (parser);
23357 token = cp_lexer_peek_token (parser->lexer);
23360 objc_add_method_declaration (is_class_method, sig, attributes);
23361 cp_parser_consume_semicolon_at_end_of_statement (parser);
23363 else if (token->keyword == RID_AT_PROPERTY)
23364 cp_parser_objc_at_property_declaration (parser);
23365 else if (token->keyword == RID_ATTRIBUTE
23366 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23367 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23369 "prefix attributes are ignored for methods");
23371 /* Allow for interspersed non-ObjC++ code. */
23372 cp_parser_objc_interstitial_code (parser);
23374 token = cp_lexer_peek_token (parser->lexer);
23377 if (token->type != CPP_EOF)
23378 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23380 cp_parser_error (parser, "expected %<@end%>");
23382 objc_finish_interface ();
23385 /* Parse an Objective-C method definition list. */
23388 cp_parser_objc_method_definition_list (cp_parser* parser)
23390 cp_token *token = cp_lexer_peek_token (parser->lexer);
23392 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23396 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23399 tree sig, attribute;
23400 bool is_class_method;
23401 if (token->type == CPP_PLUS)
23402 is_class_method = true;
23404 is_class_method = false;
23405 push_deferring_access_checks (dk_deferred);
23406 sig = cp_parser_objc_method_signature (parser, &attribute);
23407 if (sig == error_mark_node)
23409 cp_parser_skip_to_end_of_block_or_statement (parser);
23410 token = cp_lexer_peek_token (parser->lexer);
23413 objc_start_method_definition (is_class_method, sig, attribute,
23416 /* For historical reasons, we accept an optional semicolon. */
23417 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23418 cp_lexer_consume_token (parser->lexer);
23420 ptk = cp_lexer_peek_token (parser->lexer);
23421 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23422 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23424 perform_deferred_access_checks ();
23425 stop_deferring_access_checks ();
23426 meth = cp_parser_function_definition_after_declarator (parser,
23428 pop_deferring_access_checks ();
23429 objc_finish_method_definition (meth);
23432 /* The following case will be removed once @synthesize is
23433 completely implemented. */
23434 else if (token->keyword == RID_AT_PROPERTY)
23435 cp_parser_objc_at_property_declaration (parser);
23436 else if (token->keyword == RID_AT_SYNTHESIZE)
23437 cp_parser_objc_at_synthesize_declaration (parser);
23438 else if (token->keyword == RID_AT_DYNAMIC)
23439 cp_parser_objc_at_dynamic_declaration (parser);
23440 else if (token->keyword == RID_ATTRIBUTE
23441 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23442 warning_at (token->location, OPT_Wattributes,
23443 "prefix attributes are ignored for methods");
23445 /* Allow for interspersed non-ObjC++ code. */
23446 cp_parser_objc_interstitial_code (parser);
23448 token = cp_lexer_peek_token (parser->lexer);
23451 if (token->type != CPP_EOF)
23452 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23454 cp_parser_error (parser, "expected %<@end%>");
23456 objc_finish_implementation ();
23459 /* Parse Objective-C ivars. */
23462 cp_parser_objc_class_ivars (cp_parser* parser)
23464 cp_token *token = cp_lexer_peek_token (parser->lexer);
23466 if (token->type != CPP_OPEN_BRACE)
23467 return; /* No ivars specified. */
23469 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23470 token = cp_lexer_peek_token (parser->lexer);
23472 while (token->type != CPP_CLOSE_BRACE
23473 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23475 cp_decl_specifier_seq declspecs;
23476 int decl_class_or_enum_p;
23477 tree prefix_attributes;
23479 cp_parser_objc_visibility_spec (parser);
23481 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23484 cp_parser_decl_specifier_seq (parser,
23485 CP_PARSER_FLAGS_OPTIONAL,
23487 &decl_class_or_enum_p);
23489 /* auto, register, static, extern, mutable. */
23490 if (declspecs.storage_class != sc_none)
23492 cp_parser_error (parser, "invalid type for instance variable");
23493 declspecs.storage_class = sc_none;
23497 if (declspecs.specs[(int) ds_thread])
23499 cp_parser_error (parser, "invalid type for instance variable");
23500 declspecs.specs[(int) ds_thread] = 0;
23504 if (declspecs.specs[(int) ds_typedef])
23506 cp_parser_error (parser, "invalid type for instance variable");
23507 declspecs.specs[(int) ds_typedef] = 0;
23510 prefix_attributes = declspecs.attributes;
23511 declspecs.attributes = NULL_TREE;
23513 /* Keep going until we hit the `;' at the end of the
23515 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23517 tree width = NULL_TREE, attributes, first_attribute, decl;
23518 cp_declarator *declarator = NULL;
23519 int ctor_dtor_or_conv_p;
23521 /* Check for a (possibly unnamed) bitfield declaration. */
23522 token = cp_lexer_peek_token (parser->lexer);
23523 if (token->type == CPP_COLON)
23526 if (token->type == CPP_NAME
23527 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23530 /* Get the name of the bitfield. */
23531 declarator = make_id_declarator (NULL_TREE,
23532 cp_parser_identifier (parser),
23536 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23537 /* Get the width of the bitfield. */
23539 = cp_parser_constant_expression (parser,
23540 /*allow_non_constant=*/false,
23545 /* Parse the declarator. */
23547 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23548 &ctor_dtor_or_conv_p,
23549 /*parenthesized_p=*/NULL,
23550 /*member_p=*/false);
23553 /* Look for attributes that apply to the ivar. */
23554 attributes = cp_parser_attributes_opt (parser);
23555 /* Remember which attributes are prefix attributes and
23557 first_attribute = attributes;
23558 /* Combine the attributes. */
23559 attributes = chainon (prefix_attributes, attributes);
23562 /* Create the bitfield declaration. */
23563 decl = grokbitfield (declarator, &declspecs,
23567 decl = grokfield (declarator, &declspecs,
23568 NULL_TREE, /*init_const_expr_p=*/false,
23569 NULL_TREE, attributes);
23571 /* Add the instance variable. */
23572 if (decl != error_mark_node && decl != NULL_TREE)
23573 objc_add_instance_variable (decl);
23575 /* Reset PREFIX_ATTRIBUTES. */
23576 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23577 attributes = TREE_CHAIN (attributes);
23579 TREE_CHAIN (attributes) = NULL_TREE;
23581 token = cp_lexer_peek_token (parser->lexer);
23583 if (token->type == CPP_COMMA)
23585 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23591 cp_parser_consume_semicolon_at_end_of_statement (parser);
23592 token = cp_lexer_peek_token (parser->lexer);
23595 if (token->keyword == RID_AT_END)
23596 cp_parser_error (parser, "expected %<}%>");
23598 /* Do not consume the RID_AT_END, so it will be read again as terminating
23599 the @interface of @implementation. */
23600 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23601 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23603 /* For historical reasons, we accept an optional semicolon. */
23604 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23605 cp_lexer_consume_token (parser->lexer);
23608 /* Parse an Objective-C protocol declaration. */
23611 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23613 tree proto, protorefs;
23616 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23617 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23619 tok = cp_lexer_peek_token (parser->lexer);
23620 error_at (tok->location, "identifier expected after %<@protocol%>");
23621 cp_parser_consume_semicolon_at_end_of_statement (parser);
23625 /* See if we have a forward declaration or a definition. */
23626 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23628 /* Try a forward declaration first. */
23629 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23635 id = cp_parser_identifier (parser);
23636 if (id == error_mark_node)
23639 objc_declare_protocol (id, attributes);
23641 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23642 cp_lexer_consume_token (parser->lexer);
23646 cp_parser_consume_semicolon_at_end_of_statement (parser);
23649 /* Ok, we got a full-fledged definition (or at least should). */
23652 proto = cp_parser_identifier (parser);
23653 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23654 objc_start_protocol (proto, protorefs, attributes);
23655 cp_parser_objc_method_prototype_list (parser);
23659 /* Parse an Objective-C superclass or category. */
23662 cp_parser_objc_superclass_or_category (cp_parser *parser,
23665 tree *categ, bool *is_class_extension)
23667 cp_token *next = cp_lexer_peek_token (parser->lexer);
23669 *super = *categ = NULL_TREE;
23670 *is_class_extension = false;
23671 if (next->type == CPP_COLON)
23673 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23674 *super = cp_parser_identifier (parser);
23676 else if (next->type == CPP_OPEN_PAREN)
23678 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23680 /* If there is no category name, and this is an @interface, we
23681 have a class extension. */
23682 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23684 *categ = NULL_TREE;
23685 *is_class_extension = true;
23688 *categ = cp_parser_identifier (parser);
23690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23694 /* Parse an Objective-C class interface. */
23697 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23699 tree name, super, categ, protos;
23700 bool is_class_extension;
23702 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23703 name = cp_parser_identifier (parser);
23704 if (name == error_mark_node)
23706 /* It's hard to recover because even if valid @interface stuff
23707 is to follow, we can't compile it (or validate it) if we
23708 don't even know which class it refers to. Let's assume this
23709 was a stray '@interface' token in the stream and skip it.
23713 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23714 &is_class_extension);
23715 protos = cp_parser_objc_protocol_refs_opt (parser);
23717 /* We have either a class or a category on our hands. */
23718 if (categ || is_class_extension)
23719 objc_start_category_interface (name, categ, protos, attributes);
23722 objc_start_class_interface (name, super, protos, attributes);
23723 /* Handle instance variable declarations, if any. */
23724 cp_parser_objc_class_ivars (parser);
23725 objc_continue_interface ();
23728 cp_parser_objc_method_prototype_list (parser);
23731 /* Parse an Objective-C class implementation. */
23734 cp_parser_objc_class_implementation (cp_parser* parser)
23736 tree name, super, categ;
23737 bool is_class_extension;
23739 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23740 name = cp_parser_identifier (parser);
23741 if (name == error_mark_node)
23743 /* It's hard to recover because even if valid @implementation
23744 stuff is to follow, we can't compile it (or validate it) if
23745 we don't even know which class it refers to. Let's assume
23746 this was a stray '@implementation' token in the stream and
23751 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23752 &is_class_extension);
23754 /* We have either a class or a category on our hands. */
23756 objc_start_category_implementation (name, categ);
23759 objc_start_class_implementation (name, super);
23760 /* Handle instance variable declarations, if any. */
23761 cp_parser_objc_class_ivars (parser);
23762 objc_continue_implementation ();
23765 cp_parser_objc_method_definition_list (parser);
23768 /* Consume the @end token and finish off the implementation. */
23771 cp_parser_objc_end_implementation (cp_parser* parser)
23773 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23774 objc_finish_implementation ();
23777 /* Parse an Objective-C declaration. */
23780 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23782 /* Try to figure out what kind of declaration is present. */
23783 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23786 switch (kwd->keyword)
23791 error_at (kwd->location, "attributes may not be specified before"
23792 " the %<@%D%> Objective-C++ keyword",
23796 case RID_AT_IMPLEMENTATION:
23797 warning_at (kwd->location, OPT_Wattributes,
23798 "prefix attributes are ignored before %<@%D%>",
23805 switch (kwd->keyword)
23808 cp_parser_objc_alias_declaration (parser);
23811 cp_parser_objc_class_declaration (parser);
23813 case RID_AT_PROTOCOL:
23814 cp_parser_objc_protocol_declaration (parser, attributes);
23816 case RID_AT_INTERFACE:
23817 cp_parser_objc_class_interface (parser, attributes);
23819 case RID_AT_IMPLEMENTATION:
23820 cp_parser_objc_class_implementation (parser);
23823 cp_parser_objc_end_implementation (parser);
23826 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23828 cp_parser_skip_to_end_of_block_or_statement (parser);
23832 /* Parse an Objective-C try-catch-finally statement.
23834 objc-try-catch-finally-stmt:
23835 @try compound-statement objc-catch-clause-seq [opt]
23836 objc-finally-clause [opt]
23838 objc-catch-clause-seq:
23839 objc-catch-clause objc-catch-clause-seq [opt]
23842 @catch ( objc-exception-declaration ) compound-statement
23844 objc-finally-clause:
23845 @finally compound-statement
23847 objc-exception-declaration:
23848 parameter-declaration
23851 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23855 PS: This function is identical to c_parser_objc_try_catch_finally_statement
23856 for C. Keep them in sync. */
23859 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23861 location_t location;
23864 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23865 location = cp_lexer_peek_token (parser->lexer)->location;
23866 objc_maybe_warn_exceptions (location);
23867 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23868 node, lest it get absorbed into the surrounding block. */
23869 stmt = push_stmt_list ();
23870 cp_parser_compound_statement (parser, NULL, false, false);
23871 objc_begin_try_stmt (location, pop_stmt_list (stmt));
23873 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23875 cp_parameter_declarator *parm;
23876 tree parameter_declaration = error_mark_node;
23877 bool seen_open_paren = false;
23879 cp_lexer_consume_token (parser->lexer);
23880 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23881 seen_open_paren = true;
23882 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23884 /* We have "@catch (...)" (where the '...' are literally
23885 what is in the code). Skip the '...'.
23886 parameter_declaration is set to NULL_TREE, and
23887 objc_being_catch_clauses() knows that that means
23889 cp_lexer_consume_token (parser->lexer);
23890 parameter_declaration = NULL_TREE;
23894 /* We have "@catch (NSException *exception)" or something
23895 like that. Parse the parameter declaration. */
23896 parm = cp_parser_parameter_declaration (parser, false, NULL);
23898 parameter_declaration = error_mark_node;
23900 parameter_declaration = grokdeclarator (parm->declarator,
23901 &parm->decl_specifiers,
23902 PARM, /*initialized=*/0,
23903 /*attrlist=*/NULL);
23905 if (seen_open_paren)
23906 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23909 /* If there was no open parenthesis, we are recovering from
23910 an error, and we are trying to figure out what mistake
23911 the user has made. */
23913 /* If there is an immediate closing parenthesis, the user
23914 probably forgot the opening one (ie, they typed "@catch
23915 NSException *e)". Parse the closing parenthesis and keep
23917 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23918 cp_lexer_consume_token (parser->lexer);
23920 /* If these is no immediate closing parenthesis, the user
23921 probably doesn't know that parenthesis are required at
23922 all (ie, they typed "@catch NSException *e"). So, just
23923 forget about the closing parenthesis and keep going. */
23925 objc_begin_catch_clause (parameter_declaration);
23926 cp_parser_compound_statement (parser, NULL, false, false);
23927 objc_finish_catch_clause ();
23929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23931 cp_lexer_consume_token (parser->lexer);
23932 location = cp_lexer_peek_token (parser->lexer)->location;
23933 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23934 node, lest it get absorbed into the surrounding block. */
23935 stmt = push_stmt_list ();
23936 cp_parser_compound_statement (parser, NULL, false, false);
23937 objc_build_finally_clause (location, pop_stmt_list (stmt));
23940 return objc_finish_try_stmt ();
23943 /* Parse an Objective-C synchronized statement.
23945 objc-synchronized-stmt:
23946 @synchronized ( expression ) compound-statement
23948 Returns NULL_TREE. */
23951 cp_parser_objc_synchronized_statement (cp_parser *parser)
23953 location_t location;
23956 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23958 location = cp_lexer_peek_token (parser->lexer)->location;
23959 objc_maybe_warn_exceptions (location);
23960 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23961 lock = cp_parser_expression (parser, false, NULL);
23962 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23964 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23965 node, lest it get absorbed into the surrounding block. */
23966 stmt = push_stmt_list ();
23967 cp_parser_compound_statement (parser, NULL, false, false);
23969 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23972 /* Parse an Objective-C throw statement.
23975 @throw assignment-expression [opt] ;
23977 Returns a constructed '@throw' statement. */
23980 cp_parser_objc_throw_statement (cp_parser *parser)
23982 tree expr = NULL_TREE;
23983 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23985 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23987 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23988 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23990 cp_parser_consume_semicolon_at_end_of_statement (parser);
23992 return objc_build_throw_stmt (loc, expr);
23995 /* Parse an Objective-C statement. */
23998 cp_parser_objc_statement (cp_parser * parser)
24000 /* Try to figure out what kind of declaration is present. */
24001 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24003 switch (kwd->keyword)
24006 return cp_parser_objc_try_catch_finally_statement (parser);
24007 case RID_AT_SYNCHRONIZED:
24008 return cp_parser_objc_synchronized_statement (parser);
24010 return cp_parser_objc_throw_statement (parser);
24012 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24014 cp_parser_skip_to_end_of_block_or_statement (parser);
24017 return error_mark_node;
24020 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24021 look ahead to see if an objc keyword follows the attributes. This
24022 is to detect the use of prefix attributes on ObjC @interface and
24026 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24028 cp_lexer_save_tokens (parser->lexer);
24029 *attrib = cp_parser_attributes_opt (parser);
24030 gcc_assert (*attrib);
24031 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24033 cp_lexer_commit_tokens (parser->lexer);
24036 cp_lexer_rollback_tokens (parser->lexer);
24040 /* This routine is a minimal replacement for
24041 c_parser_struct_declaration () used when parsing the list of
24042 types/names or ObjC++ properties. For example, when parsing the
24045 @property (readonly) int a, b, c;
24047 this function is responsible for parsing "int a, int b, int c" and
24048 returning the declarations as CHAIN of DECLs.
24050 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24051 similar parsing. */
24053 cp_parser_objc_struct_declaration (cp_parser *parser)
24055 tree decls = NULL_TREE;
24056 cp_decl_specifier_seq declspecs;
24057 int decl_class_or_enum_p;
24058 tree prefix_attributes;
24060 cp_parser_decl_specifier_seq (parser,
24061 CP_PARSER_FLAGS_NONE,
24063 &decl_class_or_enum_p);
24065 if (declspecs.type == error_mark_node)
24066 return error_mark_node;
24068 /* auto, register, static, extern, mutable. */
24069 if (declspecs.storage_class != sc_none)
24071 cp_parser_error (parser, "invalid type for property");
24072 declspecs.storage_class = sc_none;
24076 if (declspecs.specs[(int) ds_thread])
24078 cp_parser_error (parser, "invalid type for property");
24079 declspecs.specs[(int) ds_thread] = 0;
24083 if (declspecs.specs[(int) ds_typedef])
24085 cp_parser_error (parser, "invalid type for property");
24086 declspecs.specs[(int) ds_typedef] = 0;
24089 prefix_attributes = declspecs.attributes;
24090 declspecs.attributes = NULL_TREE;
24092 /* Keep going until we hit the `;' at the end of the declaration. */
24093 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24095 tree attributes, first_attribute, decl;
24096 cp_declarator *declarator;
24099 /* Parse the declarator. */
24100 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24101 NULL, NULL, false);
24103 /* Look for attributes that apply to the ivar. */
24104 attributes = cp_parser_attributes_opt (parser);
24105 /* Remember which attributes are prefix attributes and
24107 first_attribute = attributes;
24108 /* Combine the attributes. */
24109 attributes = chainon (prefix_attributes, attributes);
24111 decl = grokfield (declarator, &declspecs,
24112 NULL_TREE, /*init_const_expr_p=*/false,
24113 NULL_TREE, attributes);
24115 if (decl == error_mark_node || decl == NULL_TREE)
24116 return error_mark_node;
24118 /* Reset PREFIX_ATTRIBUTES. */
24119 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24120 attributes = TREE_CHAIN (attributes);
24122 TREE_CHAIN (attributes) = NULL_TREE;
24124 DECL_CHAIN (decl) = decls;
24127 token = cp_lexer_peek_token (parser->lexer);
24128 if (token->type == CPP_COMMA)
24130 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24139 /* Parse an Objective-C @property declaration. The syntax is:
24141 objc-property-declaration:
24142 '@property' objc-property-attributes[opt] struct-declaration ;
24144 objc-property-attributes:
24145 '(' objc-property-attribute-list ')'
24147 objc-property-attribute-list:
24148 objc-property-attribute
24149 objc-property-attribute-list, objc-property-attribute
24151 objc-property-attribute
24152 'getter' = identifier
24153 'setter' = identifier
24162 @property NSString *name;
24163 @property (readonly) id object;
24164 @property (retain, nonatomic, getter=getTheName) id name;
24165 @property int a, b, c;
24167 PS: This function is identical to
24168 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24170 cp_parser_objc_at_property_declaration (cp_parser *parser)
24172 /* The following variables hold the attributes of the properties as
24173 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24174 seen. When we see an attribute, we set them to 'true' (if they
24175 are boolean properties) or to the identifier (if they have an
24176 argument, ie, for getter and setter). Note that here we only
24177 parse the list of attributes, check the syntax and accumulate the
24178 attributes that we find. objc_add_property_declaration() will
24179 then process the information. */
24180 bool property_assign = false;
24181 bool property_copy = false;
24182 tree property_getter_ident = NULL_TREE;
24183 bool property_nonatomic = false;
24184 bool property_readonly = false;
24185 bool property_readwrite = false;
24186 bool property_retain = false;
24187 tree property_setter_ident = NULL_TREE;
24189 /* 'properties' is the list of properties that we read. Usually a
24190 single one, but maybe more (eg, in "@property int a, b, c;" there
24195 loc = cp_lexer_peek_token (parser->lexer)->location;
24197 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24199 /* Parse the optional attribute list... */
24200 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24203 cp_lexer_consume_token (parser->lexer);
24207 bool syntax_error = false;
24208 cp_token *token = cp_lexer_peek_token (parser->lexer);
24211 if (token->type != CPP_NAME)
24213 cp_parser_error (parser, "expected identifier");
24216 keyword = C_RID_CODE (token->u.value);
24217 cp_lexer_consume_token (parser->lexer);
24220 case RID_ASSIGN: property_assign = true; break;
24221 case RID_COPY: property_copy = true; break;
24222 case RID_NONATOMIC: property_nonatomic = true; break;
24223 case RID_READONLY: property_readonly = true; break;
24224 case RID_READWRITE: property_readwrite = true; break;
24225 case RID_RETAIN: property_retain = true; break;
24229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24231 if (keyword == RID_GETTER)
24232 cp_parser_error (parser,
24233 "missing %<=%> (after %<getter%> attribute)");
24235 cp_parser_error (parser,
24236 "missing %<=%> (after %<setter%> attribute)");
24237 syntax_error = true;
24240 cp_lexer_consume_token (parser->lexer); /* eat the = */
24241 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24243 cp_parser_error (parser, "expected identifier");
24244 syntax_error = true;
24247 if (keyword == RID_SETTER)
24249 if (property_setter_ident != NULL_TREE)
24251 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24252 cp_lexer_consume_token (parser->lexer);
24255 property_setter_ident = cp_parser_objc_selector (parser);
24256 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24257 cp_parser_error (parser, "setter name must terminate with %<:%>");
24259 cp_lexer_consume_token (parser->lexer);
24263 if (property_getter_ident != NULL_TREE)
24265 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24266 cp_lexer_consume_token (parser->lexer);
24269 property_getter_ident = cp_parser_objc_selector (parser);
24273 cp_parser_error (parser, "unknown property attribute");
24274 syntax_error = true;
24281 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24282 cp_lexer_consume_token (parser->lexer);
24287 /* FIXME: "@property (setter, assign);" will generate a spurious
24288 "error: expected ‘)’ before ‘,’ token". This is because
24289 cp_parser_require, unlike the C counterpart, will produce an
24290 error even if we are in error recovery. */
24291 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24293 cp_parser_skip_to_closing_parenthesis (parser,
24294 /*recovering=*/true,
24295 /*or_comma=*/false,
24296 /*consume_paren=*/true);
24300 /* ... and the property declaration(s). */
24301 properties = cp_parser_objc_struct_declaration (parser);
24303 if (properties == error_mark_node)
24305 cp_parser_skip_to_end_of_statement (parser);
24306 /* If the next token is now a `;', consume it. */
24307 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24308 cp_lexer_consume_token (parser->lexer);
24312 if (properties == NULL_TREE)
24313 cp_parser_error (parser, "expected identifier");
24316 /* Comma-separated properties are chained together in
24317 reverse order; add them one by one. */
24318 properties = nreverse (properties);
24320 for (; properties; properties = TREE_CHAIN (properties))
24321 objc_add_property_declaration (loc, copy_node (properties),
24322 property_readonly, property_readwrite,
24323 property_assign, property_retain,
24324 property_copy, property_nonatomic,
24325 property_getter_ident, property_setter_ident);
24328 cp_parser_consume_semicolon_at_end_of_statement (parser);
24331 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24333 objc-synthesize-declaration:
24334 @synthesize objc-synthesize-identifier-list ;
24336 objc-synthesize-identifier-list:
24337 objc-synthesize-identifier
24338 objc-synthesize-identifier-list, objc-synthesize-identifier
24340 objc-synthesize-identifier
24342 identifier = identifier
24345 @synthesize MyProperty;
24346 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24348 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24349 for C. Keep them in sync.
24352 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24354 tree list = NULL_TREE;
24356 loc = cp_lexer_peek_token (parser->lexer)->location;
24358 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24361 tree property, ivar;
24362 property = cp_parser_identifier (parser);
24363 if (property == error_mark_node)
24365 cp_parser_consume_semicolon_at_end_of_statement (parser);
24368 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24370 cp_lexer_consume_token (parser->lexer);
24371 ivar = cp_parser_identifier (parser);
24372 if (ivar == error_mark_node)
24374 cp_parser_consume_semicolon_at_end_of_statement (parser);
24380 list = chainon (list, build_tree_list (ivar, property));
24381 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24382 cp_lexer_consume_token (parser->lexer);
24386 cp_parser_consume_semicolon_at_end_of_statement (parser);
24387 objc_add_synthesize_declaration (loc, list);
24390 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24392 objc-dynamic-declaration:
24393 @dynamic identifier-list ;
24396 @dynamic MyProperty;
24397 @dynamic MyProperty, AnotherProperty;
24399 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24400 for C. Keep them in sync.
24403 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24405 tree list = NULL_TREE;
24407 loc = cp_lexer_peek_token (parser->lexer)->location;
24409 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24413 property = cp_parser_identifier (parser);
24414 if (property == error_mark_node)
24416 cp_parser_consume_semicolon_at_end_of_statement (parser);
24419 list = chainon (list, build_tree_list (NULL, property));
24420 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24421 cp_lexer_consume_token (parser->lexer);
24425 cp_parser_consume_semicolon_at_end_of_statement (parser);
24426 objc_add_dynamic_declaration (loc, list);
24430 /* OpenMP 2.5 parsing routines. */
24432 /* Returns name of the next clause.
24433 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24434 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24435 returned and the token is consumed. */
24437 static pragma_omp_clause
24438 cp_parser_omp_clause_name (cp_parser *parser)
24440 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24442 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24443 result = PRAGMA_OMP_CLAUSE_IF;
24444 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24445 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24446 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24447 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24448 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24450 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24451 const char *p = IDENTIFIER_POINTER (id);
24456 if (!strcmp ("collapse", p))
24457 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24458 else if (!strcmp ("copyin", p))
24459 result = PRAGMA_OMP_CLAUSE_COPYIN;
24460 else if (!strcmp ("copyprivate", p))
24461 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24464 if (!strcmp ("final", p))
24465 result = PRAGMA_OMP_CLAUSE_FINAL;
24466 else if (!strcmp ("firstprivate", p))
24467 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24470 if (!strcmp ("lastprivate", p))
24471 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24474 if (!strcmp ("mergeable", p))
24475 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24478 if (!strcmp ("nowait", p))
24479 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24480 else if (!strcmp ("num_threads", p))
24481 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24484 if (!strcmp ("ordered", p))
24485 result = PRAGMA_OMP_CLAUSE_ORDERED;
24488 if (!strcmp ("reduction", p))
24489 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24492 if (!strcmp ("schedule", p))
24493 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24494 else if (!strcmp ("shared", p))
24495 result = PRAGMA_OMP_CLAUSE_SHARED;
24498 if (!strcmp ("untied", p))
24499 result = PRAGMA_OMP_CLAUSE_UNTIED;
24504 if (result != PRAGMA_OMP_CLAUSE_NONE)
24505 cp_lexer_consume_token (parser->lexer);
24510 /* Validate that a clause of the given type does not already exist. */
24513 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24514 const char *name, location_t location)
24518 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24519 if (OMP_CLAUSE_CODE (c) == code)
24521 error_at (location, "too many %qs clauses", name);
24529 variable-list , identifier
24531 In addition, we match a closing parenthesis. An opening parenthesis
24532 will have been consumed by the caller.
24534 If KIND is nonzero, create the appropriate node and install the decl
24535 in OMP_CLAUSE_DECL and add the node to the head of the list.
24537 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24538 return the list created. */
24541 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24549 token = cp_lexer_peek_token (parser->lexer);
24550 name = cp_parser_id_expression (parser, /*template_p=*/false,
24551 /*check_dependency_p=*/true,
24552 /*template_p=*/NULL,
24553 /*declarator_p=*/false,
24554 /*optional_p=*/false);
24555 if (name == error_mark_node)
24558 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24559 if (decl == error_mark_node)
24560 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24562 else if (kind != 0)
24564 tree u = build_omp_clause (token->location, kind);
24565 OMP_CLAUSE_DECL (u) = decl;
24566 OMP_CLAUSE_CHAIN (u) = list;
24570 list = tree_cons (decl, NULL_TREE, list);
24573 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24575 cp_lexer_consume_token (parser->lexer);
24578 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24582 /* Try to resync to an unnested comma. Copied from
24583 cp_parser_parenthesized_expression_list. */
24585 ending = cp_parser_skip_to_closing_parenthesis (parser,
24586 /*recovering=*/true,
24588 /*consume_paren=*/true);
24596 /* Similarly, but expect leading and trailing parenthesis. This is a very
24597 common case for omp clauses. */
24600 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24602 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24603 return cp_parser_omp_var_list_no_open (parser, kind, list);
24608 collapse ( constant-expression ) */
24611 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24617 loc = cp_lexer_peek_token (parser->lexer)->location;
24618 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24621 num = cp_parser_constant_expression (parser, false, NULL);
24623 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24624 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24625 /*or_comma=*/false,
24626 /*consume_paren=*/true);
24628 if (num == error_mark_node)
24630 num = fold_non_dependent_expr (num);
24631 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24632 || !host_integerp (num, 0)
24633 || (n = tree_low_cst (num, 0)) <= 0
24636 error_at (loc, "collapse argument needs positive constant integer expression");
24640 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24641 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24642 OMP_CLAUSE_CHAIN (c) = list;
24643 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24649 default ( shared | none ) */
24652 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24654 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24657 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24659 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24661 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24662 const char *p = IDENTIFIER_POINTER (id);
24667 if (strcmp ("none", p) != 0)
24669 kind = OMP_CLAUSE_DEFAULT_NONE;
24673 if (strcmp ("shared", p) != 0)
24675 kind = OMP_CLAUSE_DEFAULT_SHARED;
24682 cp_lexer_consume_token (parser->lexer);
24687 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24690 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24691 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24692 /*or_comma=*/false,
24693 /*consume_paren=*/true);
24695 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24698 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24699 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24700 OMP_CLAUSE_CHAIN (c) = list;
24701 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24707 final ( expression ) */
24710 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24714 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24717 t = cp_parser_condition (parser);
24719 if (t == error_mark_node
24720 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24721 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24722 /*or_comma=*/false,
24723 /*consume_paren=*/true);
24725 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24727 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24728 OMP_CLAUSE_FINAL_EXPR (c) = t;
24729 OMP_CLAUSE_CHAIN (c) = list;
24735 if ( expression ) */
24738 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24742 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24745 t = cp_parser_condition (parser);
24747 if (t == error_mark_node
24748 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24749 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24750 /*or_comma=*/false,
24751 /*consume_paren=*/true);
24753 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24755 c = build_omp_clause (location, OMP_CLAUSE_IF);
24756 OMP_CLAUSE_IF_EXPR (c) = t;
24757 OMP_CLAUSE_CHAIN (c) = list;
24766 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24767 tree list, location_t location)
24771 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24774 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24775 OMP_CLAUSE_CHAIN (c) = list;
24783 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24784 tree list, location_t location)
24788 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24790 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24791 OMP_CLAUSE_CHAIN (c) = list;
24796 num_threads ( expression ) */
24799 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24800 location_t location)
24804 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24807 t = cp_parser_expression (parser, false, NULL);
24809 if (t == error_mark_node
24810 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24811 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24812 /*or_comma=*/false,
24813 /*consume_paren=*/true);
24815 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24816 "num_threads", location);
24818 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24819 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24820 OMP_CLAUSE_CHAIN (c) = list;
24829 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24830 tree list, location_t location)
24834 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24835 "ordered", location);
24837 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24838 OMP_CLAUSE_CHAIN (c) = list;
24843 reduction ( reduction-operator : variable-list )
24845 reduction-operator:
24846 One of: + * - & ^ | && ||
24850 reduction-operator:
24851 One of: + * - & ^ | && || min max */
24854 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24856 enum tree_code code;
24859 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24862 switch (cp_lexer_peek_token (parser->lexer)->type)
24874 code = BIT_AND_EXPR;
24877 code = BIT_XOR_EXPR;
24880 code = BIT_IOR_EXPR;
24883 code = TRUTH_ANDIF_EXPR;
24886 code = TRUTH_ORIF_EXPR;
24890 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24891 const char *p = IDENTIFIER_POINTER (id);
24893 if (strcmp (p, "min") == 0)
24898 if (strcmp (p, "max") == 0)
24906 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24907 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24909 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24910 /*or_comma=*/false,
24911 /*consume_paren=*/true);
24914 cp_lexer_consume_token (parser->lexer);
24916 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24919 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24920 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24921 OMP_CLAUSE_REDUCTION_CODE (c) = code;
24927 schedule ( schedule-kind )
24928 schedule ( schedule-kind , expression )
24931 static | dynamic | guided | runtime | auto */
24934 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24938 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24941 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24943 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24945 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24946 const char *p = IDENTIFIER_POINTER (id);
24951 if (strcmp ("dynamic", p) != 0)
24953 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24957 if (strcmp ("guided", p) != 0)
24959 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24963 if (strcmp ("runtime", p) != 0)
24965 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24972 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24973 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24974 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24975 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24978 cp_lexer_consume_token (parser->lexer);
24980 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24983 cp_lexer_consume_token (parser->lexer);
24985 token = cp_lexer_peek_token (parser->lexer);
24986 t = cp_parser_assignment_expression (parser, false, NULL);
24988 if (t == error_mark_node)
24990 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24991 error_at (token->location, "schedule %<runtime%> does not take "
24992 "a %<chunk_size%> parameter");
24993 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24994 error_at (token->location, "schedule %<auto%> does not take "
24995 "a %<chunk_size%> parameter");
24997 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24999 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25002 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25005 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25006 OMP_CLAUSE_CHAIN (c) = list;
25010 cp_parser_error (parser, "invalid schedule kind");
25012 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25013 /*or_comma=*/false,
25014 /*consume_paren=*/true);
25022 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25023 tree list, location_t location)
25027 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25029 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25030 OMP_CLAUSE_CHAIN (c) = list;
25034 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25035 is a bitmask in MASK. Return the list of clauses found; the result
25036 of clause default goes in *pdefault. */
25039 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25040 const char *where, cp_token *pragma_tok)
25042 tree clauses = NULL;
25044 cp_token *token = NULL;
25046 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25048 pragma_omp_clause c_kind;
25049 const char *c_name;
25050 tree prev = clauses;
25052 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25053 cp_lexer_consume_token (parser->lexer);
25055 token = cp_lexer_peek_token (parser->lexer);
25056 c_kind = cp_parser_omp_clause_name (parser);
25061 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25062 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25064 c_name = "collapse";
25066 case PRAGMA_OMP_CLAUSE_COPYIN:
25067 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25070 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25071 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25073 c_name = "copyprivate";
25075 case PRAGMA_OMP_CLAUSE_DEFAULT:
25076 clauses = cp_parser_omp_clause_default (parser, clauses,
25078 c_name = "default";
25080 case PRAGMA_OMP_CLAUSE_FINAL:
25081 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25084 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25085 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25087 c_name = "firstprivate";
25089 case PRAGMA_OMP_CLAUSE_IF:
25090 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25093 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25094 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25096 c_name = "lastprivate";
25098 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25099 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25101 c_name = "mergeable";
25103 case PRAGMA_OMP_CLAUSE_NOWAIT:
25104 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25107 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25108 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25110 c_name = "num_threads";
25112 case PRAGMA_OMP_CLAUSE_ORDERED:
25113 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25115 c_name = "ordered";
25117 case PRAGMA_OMP_CLAUSE_PRIVATE:
25118 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25120 c_name = "private";
25122 case PRAGMA_OMP_CLAUSE_REDUCTION:
25123 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25124 c_name = "reduction";
25126 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25127 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25129 c_name = "schedule";
25131 case PRAGMA_OMP_CLAUSE_SHARED:
25132 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25136 case PRAGMA_OMP_CLAUSE_UNTIED:
25137 clauses = cp_parser_omp_clause_untied (parser, clauses,
25142 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25146 if (((mask >> c_kind) & 1) == 0)
25148 /* Remove the invalid clause(s) from the list to avoid
25149 confusing the rest of the compiler. */
25151 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25155 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25156 return finish_omp_clauses (clauses);
25163 In practice, we're also interested in adding the statement to an
25164 outer node. So it is convenient if we work around the fact that
25165 cp_parser_statement calls add_stmt. */
25168 cp_parser_begin_omp_structured_block (cp_parser *parser)
25170 unsigned save = parser->in_statement;
25172 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25173 This preserves the "not within loop or switch" style error messages
25174 for nonsense cases like
25180 if (parser->in_statement)
25181 parser->in_statement = IN_OMP_BLOCK;
25187 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25189 parser->in_statement = save;
25193 cp_parser_omp_structured_block (cp_parser *parser)
25195 tree stmt = begin_omp_structured_block ();
25196 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25198 cp_parser_statement (parser, NULL_TREE, false, NULL);
25200 cp_parser_end_omp_structured_block (parser, save);
25201 return finish_omp_structured_block (stmt);
25205 # pragma omp atomic new-line
25209 x binop= expr | x++ | ++x | x-- | --x
25211 +, *, -, /, &, ^, |, <<, >>
25213 where x is an lvalue expression with scalar type.
25216 # pragma omp atomic new-line
25219 # pragma omp atomic read new-line
25222 # pragma omp atomic write new-line
25225 # pragma omp atomic update new-line
25228 # pragma omp atomic capture new-line
25231 # pragma omp atomic capture new-line
25239 expression-stmt | x = x binop expr
25241 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25243 { v = x; update-stmt; } | { update-stmt; v = x; }
25245 where x and v are lvalue expressions with scalar type. */
25248 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25250 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25251 tree rhs1 = NULL_TREE, orig_lhs;
25252 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25253 bool structured_block = false;
25255 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25257 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25258 const char *p = IDENTIFIER_POINTER (id);
25260 if (!strcmp (p, "read"))
25261 code = OMP_ATOMIC_READ;
25262 else if (!strcmp (p, "write"))
25264 else if (!strcmp (p, "update"))
25266 else if (!strcmp (p, "capture"))
25267 code = OMP_ATOMIC_CAPTURE_NEW;
25271 cp_lexer_consume_token (parser->lexer);
25273 cp_parser_require_pragma_eol (parser, pragma_tok);
25277 case OMP_ATOMIC_READ:
25278 case NOP_EXPR: /* atomic write */
25279 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25280 /*cast_p=*/false, NULL);
25281 if (v == error_mark_node)
25283 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25285 if (code == NOP_EXPR)
25286 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25288 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25289 /*cast_p=*/false, NULL);
25290 if (lhs == error_mark_node)
25292 if (code == NOP_EXPR)
25294 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25302 case OMP_ATOMIC_CAPTURE_NEW:
25303 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25305 cp_lexer_consume_token (parser->lexer);
25306 structured_block = true;
25310 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25311 /*cast_p=*/false, NULL);
25312 if (v == error_mark_node)
25314 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25322 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25323 /*cast_p=*/false, NULL);
25325 switch (TREE_CODE (lhs))
25330 case POSTINCREMENT_EXPR:
25331 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25332 code = OMP_ATOMIC_CAPTURE_OLD;
25334 case PREINCREMENT_EXPR:
25335 lhs = TREE_OPERAND (lhs, 0);
25336 opcode = PLUS_EXPR;
25337 rhs = integer_one_node;
25340 case POSTDECREMENT_EXPR:
25341 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25342 code = OMP_ATOMIC_CAPTURE_OLD;
25344 case PREDECREMENT_EXPR:
25345 lhs = TREE_OPERAND (lhs, 0);
25346 opcode = MINUS_EXPR;
25347 rhs = integer_one_node;
25350 case COMPOUND_EXPR:
25351 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25352 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25353 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25354 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25355 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25356 (TREE_OPERAND (lhs, 1), 0), 0)))
25358 /* Undo effects of boolean_increment for post {in,de}crement. */
25359 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25362 if (TREE_CODE (lhs) == MODIFY_EXPR
25363 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25365 /* Undo effects of boolean_increment. */
25366 if (integer_onep (TREE_OPERAND (lhs, 1)))
25368 /* This is pre or post increment. */
25369 rhs = TREE_OPERAND (lhs, 1);
25370 lhs = TREE_OPERAND (lhs, 0);
25372 if (code == OMP_ATOMIC_CAPTURE_NEW
25373 && !structured_block
25374 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25375 code = OMP_ATOMIC_CAPTURE_OLD;
25381 switch (cp_lexer_peek_token (parser->lexer)->type)
25384 opcode = MULT_EXPR;
25387 opcode = TRUNC_DIV_EXPR;
25390 opcode = PLUS_EXPR;
25393 opcode = MINUS_EXPR;
25395 case CPP_LSHIFT_EQ:
25396 opcode = LSHIFT_EXPR;
25398 case CPP_RSHIFT_EQ:
25399 opcode = RSHIFT_EXPR;
25402 opcode = BIT_AND_EXPR;
25405 opcode = BIT_IOR_EXPR;
25408 opcode = BIT_XOR_EXPR;
25411 if (structured_block || code == OMP_ATOMIC)
25413 enum cp_parser_prec oprec;
25415 cp_lexer_consume_token (parser->lexer);
25416 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25417 /*cast_p=*/false, NULL);
25418 if (rhs1 == error_mark_node)
25420 token = cp_lexer_peek_token (parser->lexer);
25421 switch (token->type)
25423 case CPP_SEMICOLON:
25424 if (code == OMP_ATOMIC_CAPTURE_NEW)
25426 code = OMP_ATOMIC_CAPTURE_OLD;
25431 cp_lexer_consume_token (parser->lexer);
25434 cp_parser_error (parser,
25435 "invalid form of %<#pragma omp atomic%>");
25438 opcode = MULT_EXPR;
25441 opcode = TRUNC_DIV_EXPR;
25444 opcode = PLUS_EXPR;
25447 opcode = MINUS_EXPR;
25450 opcode = LSHIFT_EXPR;
25453 opcode = RSHIFT_EXPR;
25456 opcode = BIT_AND_EXPR;
25459 opcode = BIT_IOR_EXPR;
25462 opcode = BIT_XOR_EXPR;
25465 cp_parser_error (parser,
25466 "invalid operator for %<#pragma omp atomic%>");
25469 oprec = TOKEN_PRECEDENCE (token);
25470 gcc_assert (oprec != PREC_NOT_OPERATOR);
25471 if (commutative_tree_code (opcode))
25472 oprec = (enum cp_parser_prec) (oprec - 1);
25473 cp_lexer_consume_token (parser->lexer);
25474 rhs = cp_parser_binary_expression (parser, false, false,
25476 if (rhs == error_mark_node)
25482 cp_parser_error (parser,
25483 "invalid operator for %<#pragma omp atomic%>");
25486 cp_lexer_consume_token (parser->lexer);
25488 rhs = cp_parser_expression (parser, false, NULL);
25489 if (rhs == error_mark_node)
25494 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25496 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25498 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25499 /*cast_p=*/false, NULL);
25500 if (v == error_mark_node)
25502 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25504 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25505 /*cast_p=*/false, NULL);
25506 if (lhs1 == error_mark_node)
25509 if (structured_block)
25511 cp_parser_consume_semicolon_at_end_of_statement (parser);
25512 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25515 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25516 if (!structured_block)
25517 cp_parser_consume_semicolon_at_end_of_statement (parser);
25521 cp_parser_skip_to_end_of_block_or_statement (parser);
25522 if (structured_block)
25524 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25525 cp_lexer_consume_token (parser->lexer);
25526 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25528 cp_parser_skip_to_end_of_block_or_statement (parser);
25529 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25530 cp_lexer_consume_token (parser->lexer);
25537 # pragma omp barrier new-line */
25540 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25542 cp_parser_require_pragma_eol (parser, pragma_tok);
25543 finish_omp_barrier ();
25547 # pragma omp critical [(name)] new-line
25548 structured-block */
25551 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25553 tree stmt, name = NULL;
25555 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25557 cp_lexer_consume_token (parser->lexer);
25559 name = cp_parser_identifier (parser);
25561 if (name == error_mark_node
25562 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25563 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25564 /*or_comma=*/false,
25565 /*consume_paren=*/true);
25566 if (name == error_mark_node)
25569 cp_parser_require_pragma_eol (parser, pragma_tok);
25571 stmt = cp_parser_omp_structured_block (parser);
25572 return c_finish_omp_critical (input_location, stmt, name);
25576 # pragma omp flush flush-vars[opt] new-line
25579 ( variable-list ) */
25582 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25585 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25586 cp_parser_require_pragma_eol (parser, pragma_tok);
25588 finish_omp_flush ();
25591 /* Helper function, to parse omp for increment expression. */
25594 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25596 tree cond = cp_parser_binary_expression (parser, false, true,
25597 PREC_NOT_OPERATOR, NULL);
25598 if (cond == error_mark_node
25599 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25601 cp_parser_skip_to_end_of_statement (parser);
25602 return error_mark_node;
25605 switch (TREE_CODE (cond))
25613 return error_mark_node;
25616 /* If decl is an iterator, preserve LHS and RHS of the relational
25617 expr until finish_omp_for. */
25619 && (type_dependent_expression_p (decl)
25620 || CLASS_TYPE_P (TREE_TYPE (decl))))
25623 return build_x_binary_op (TREE_CODE (cond),
25624 TREE_OPERAND (cond, 0), ERROR_MARK,
25625 TREE_OPERAND (cond, 1), ERROR_MARK,
25626 /*overload=*/NULL, tf_warning_or_error);
25629 /* Helper function, to parse omp for increment expression. */
25632 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25634 cp_token *token = cp_lexer_peek_token (parser->lexer);
25640 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25642 op = (token->type == CPP_PLUS_PLUS
25643 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25644 cp_lexer_consume_token (parser->lexer);
25645 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25647 return error_mark_node;
25648 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25651 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25653 return error_mark_node;
25655 token = cp_lexer_peek_token (parser->lexer);
25656 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25658 op = (token->type == CPP_PLUS_PLUS
25659 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25660 cp_lexer_consume_token (parser->lexer);
25661 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25664 op = cp_parser_assignment_operator_opt (parser);
25665 if (op == ERROR_MARK)
25666 return error_mark_node;
25668 if (op != NOP_EXPR)
25670 rhs = cp_parser_assignment_expression (parser, false, NULL);
25671 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25672 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25675 lhs = cp_parser_binary_expression (parser, false, false,
25676 PREC_ADDITIVE_EXPRESSION, NULL);
25677 token = cp_lexer_peek_token (parser->lexer);
25678 decl_first = lhs == decl;
25681 if (token->type != CPP_PLUS
25682 && token->type != CPP_MINUS)
25683 return error_mark_node;
25687 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25688 cp_lexer_consume_token (parser->lexer);
25689 rhs = cp_parser_binary_expression (parser, false, false,
25690 PREC_ADDITIVE_EXPRESSION, NULL);
25691 token = cp_lexer_peek_token (parser->lexer);
25692 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25694 if (lhs == NULL_TREE)
25696 if (op == PLUS_EXPR)
25699 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25702 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25703 NULL, tf_warning_or_error);
25706 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25710 if (rhs != decl || op == MINUS_EXPR)
25711 return error_mark_node;
25712 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25715 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25717 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25720 /* Parse the restricted form of the for statement allowed by OpenMP. */
25723 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25725 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25726 tree real_decl, initv, condv, incrv, declv;
25727 tree this_pre_body, cl;
25728 location_t loc_first;
25729 bool collapse_err = false;
25730 int i, collapse = 1, nbraces = 0;
25731 VEC(tree,gc) *for_block = make_tree_vector ();
25733 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25734 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25735 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25737 gcc_assert (collapse >= 1);
25739 declv = make_tree_vec (collapse);
25740 initv = make_tree_vec (collapse);
25741 condv = make_tree_vec (collapse);
25742 incrv = make_tree_vec (collapse);
25744 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25746 for (i = 0; i < collapse; i++)
25748 int bracecount = 0;
25749 bool add_private_clause = false;
25752 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25754 cp_parser_error (parser, "for statement expected");
25757 loc = cp_lexer_consume_token (parser->lexer)->location;
25759 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25762 init = decl = real_decl = NULL;
25763 this_pre_body = push_stmt_list ();
25764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25766 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25770 integer-type var = lb
25771 random-access-iterator-type var = lb
25772 pointer-type var = lb
25774 cp_decl_specifier_seq type_specifiers;
25776 /* First, try to parse as an initialized declaration. See
25777 cp_parser_condition, from whence the bulk of this is copied. */
25779 cp_parser_parse_tentatively (parser);
25780 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25781 /*is_trailing_return=*/false,
25783 if (cp_parser_parse_definitely (parser))
25785 /* If parsing a type specifier seq succeeded, then this
25786 MUST be a initialized declaration. */
25787 tree asm_specification, attributes;
25788 cp_declarator *declarator;
25790 declarator = cp_parser_declarator (parser,
25791 CP_PARSER_DECLARATOR_NAMED,
25792 /*ctor_dtor_or_conv_p=*/NULL,
25793 /*parenthesized_p=*/NULL,
25794 /*member_p=*/false);
25795 attributes = cp_parser_attributes_opt (parser);
25796 asm_specification = cp_parser_asm_specification_opt (parser);
25798 if (declarator == cp_error_declarator)
25799 cp_parser_skip_to_end_of_statement (parser);
25803 tree pushed_scope, auto_node;
25805 decl = start_decl (declarator, &type_specifiers,
25806 SD_INITIALIZED, attributes,
25807 /*prefix_attributes=*/NULL_TREE,
25810 auto_node = type_uses_auto (TREE_TYPE (decl));
25811 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25813 if (cp_lexer_next_token_is (parser->lexer,
25815 error ("parenthesized initialization is not allowed in "
25816 "OpenMP %<for%> loop");
25818 /* Trigger an error. */
25819 cp_parser_require (parser, CPP_EQ, RT_EQ);
25821 init = error_mark_node;
25822 cp_parser_skip_to_end_of_statement (parser);
25824 else if (CLASS_TYPE_P (TREE_TYPE (decl))
25825 || type_dependent_expression_p (decl)
25828 bool is_direct_init, is_non_constant_init;
25830 init = cp_parser_initializer (parser,
25832 &is_non_constant_init);
25837 = do_auto_deduction (TREE_TYPE (decl), init,
25840 if (!CLASS_TYPE_P (TREE_TYPE (decl))
25841 && !type_dependent_expression_p (decl))
25845 cp_finish_decl (decl, init, !is_non_constant_init,
25847 LOOKUP_ONLYCONVERTING);
25848 if (CLASS_TYPE_P (TREE_TYPE (decl)))
25850 VEC_safe_push (tree, gc, for_block, this_pre_body);
25854 init = pop_stmt_list (this_pre_body);
25855 this_pre_body = NULL_TREE;
25860 cp_lexer_consume_token (parser->lexer);
25861 init = cp_parser_assignment_expression (parser, false, NULL);
25864 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25865 init = error_mark_node;
25867 cp_finish_decl (decl, NULL_TREE,
25868 /*init_const_expr_p=*/false,
25870 LOOKUP_ONLYCONVERTING);
25874 pop_scope (pushed_scope);
25880 /* If parsing a type specifier sequence failed, then
25881 this MUST be a simple expression. */
25882 cp_parser_parse_tentatively (parser);
25883 decl = cp_parser_primary_expression (parser, false, false,
25885 if (!cp_parser_error_occurred (parser)
25888 && CLASS_TYPE_P (TREE_TYPE (decl)))
25892 cp_parser_parse_definitely (parser);
25893 cp_parser_require (parser, CPP_EQ, RT_EQ);
25894 rhs = cp_parser_assignment_expression (parser, false, NULL);
25895 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25897 tf_warning_or_error));
25898 add_private_clause = true;
25903 cp_parser_abort_tentative_parse (parser);
25904 init = cp_parser_expression (parser, false, NULL);
25907 if (TREE_CODE (init) == MODIFY_EXPR
25908 || TREE_CODE (init) == MODOP_EXPR)
25909 real_decl = TREE_OPERAND (init, 0);
25914 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25917 this_pre_body = pop_stmt_list (this_pre_body);
25921 pre_body = push_stmt_list ();
25923 add_stmt (this_pre_body);
25924 pre_body = pop_stmt_list (pre_body);
25927 pre_body = this_pre_body;
25932 if (par_clauses != NULL && real_decl != NULL_TREE)
25935 for (c = par_clauses; *c ; )
25936 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25937 && OMP_CLAUSE_DECL (*c) == real_decl)
25939 error_at (loc, "iteration variable %qD"
25940 " should not be firstprivate", real_decl);
25941 *c = OMP_CLAUSE_CHAIN (*c);
25943 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25944 && OMP_CLAUSE_DECL (*c) == real_decl)
25946 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25947 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
25948 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25949 OMP_CLAUSE_DECL (l) = real_decl;
25950 OMP_CLAUSE_CHAIN (l) = clauses;
25951 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25953 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25954 CP_OMP_CLAUSE_INFO (*c) = NULL;
25955 add_private_clause = false;
25959 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25960 && OMP_CLAUSE_DECL (*c) == real_decl)
25961 add_private_clause = false;
25962 c = &OMP_CLAUSE_CHAIN (*c);
25966 if (add_private_clause)
25969 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25971 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25972 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25973 && OMP_CLAUSE_DECL (c) == decl)
25975 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25976 && OMP_CLAUSE_DECL (c) == decl)
25977 error_at (loc, "iteration variable %qD "
25978 "should not be firstprivate",
25980 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25981 && OMP_CLAUSE_DECL (c) == decl)
25982 error_at (loc, "iteration variable %qD should not be reduction",
25987 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25988 OMP_CLAUSE_DECL (c) = decl;
25989 c = finish_omp_clauses (c);
25992 OMP_CLAUSE_CHAIN (c) = clauses;
25999 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26000 cond = cp_parser_omp_for_cond (parser, decl);
26001 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26004 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26006 /* If decl is an iterator, preserve the operator on decl
26007 until finish_omp_for. */
26009 && ((type_dependent_expression_p (decl)
26010 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26011 || CLASS_TYPE_P (TREE_TYPE (decl))))
26012 incr = cp_parser_omp_for_incr (parser, decl);
26014 incr = cp_parser_expression (parser, false, NULL);
26017 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26018 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26019 /*or_comma=*/false,
26020 /*consume_paren=*/true);
26022 TREE_VEC_ELT (declv, i) = decl;
26023 TREE_VEC_ELT (initv, i) = init;
26024 TREE_VEC_ELT (condv, i) = cond;
26025 TREE_VEC_ELT (incrv, i) = incr;
26027 if (i == collapse - 1)
26030 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26031 in between the collapsed for loops to be still considered perfectly
26032 nested. Hopefully the final version clarifies this.
26033 For now handle (multiple) {'s and empty statements. */
26034 cp_parser_parse_tentatively (parser);
26037 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26039 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26041 cp_lexer_consume_token (parser->lexer);
26044 else if (bracecount
26045 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26046 cp_lexer_consume_token (parser->lexer);
26049 loc = cp_lexer_peek_token (parser->lexer)->location;
26050 error_at (loc, "not enough collapsed for loops");
26051 collapse_err = true;
26052 cp_parser_abort_tentative_parse (parser);
26061 cp_parser_parse_definitely (parser);
26062 nbraces += bracecount;
26066 /* Note that we saved the original contents of this flag when we entered
26067 the structured block, and so we don't need to re-save it here. */
26068 parser->in_statement = IN_OMP_FOR;
26070 /* Note that the grammar doesn't call for a structured block here,
26071 though the loop as a whole is a structured block. */
26072 body = push_stmt_list ();
26073 cp_parser_statement (parser, NULL_TREE, false, NULL);
26074 body = pop_stmt_list (body);
26076 if (declv == NULL_TREE)
26079 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26080 pre_body, clauses);
26084 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26086 cp_lexer_consume_token (parser->lexer);
26089 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26090 cp_lexer_consume_token (parser->lexer);
26095 error_at (cp_lexer_peek_token (parser->lexer)->location,
26096 "collapsed loops not perfectly nested");
26098 collapse_err = true;
26099 cp_parser_statement_seq_opt (parser, NULL);
26100 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26105 while (!VEC_empty (tree, for_block))
26106 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26107 release_tree_vector (for_block);
26113 #pragma omp for for-clause[optseq] new-line
26116 #define OMP_FOR_CLAUSE_MASK \
26117 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26118 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26119 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26120 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26121 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26122 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26123 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26124 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26127 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26129 tree clauses, sb, ret;
26132 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26133 "#pragma omp for", pragma_tok);
26135 sb = begin_omp_structured_block ();
26136 save = cp_parser_begin_omp_structured_block (parser);
26138 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26140 cp_parser_end_omp_structured_block (parser, save);
26141 add_stmt (finish_omp_structured_block (sb));
26147 # pragma omp master new-line
26148 structured-block */
26151 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26153 cp_parser_require_pragma_eol (parser, pragma_tok);
26154 return c_finish_omp_master (input_location,
26155 cp_parser_omp_structured_block (parser));
26159 # pragma omp ordered new-line
26160 structured-block */
26163 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26165 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26166 cp_parser_require_pragma_eol (parser, pragma_tok);
26167 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26173 { section-sequence }
26176 section-directive[opt] structured-block
26177 section-sequence section-directive structured-block */
26180 cp_parser_omp_sections_scope (cp_parser *parser)
26182 tree stmt, substmt;
26183 bool error_suppress = false;
26186 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26189 stmt = push_stmt_list ();
26191 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26195 substmt = begin_omp_structured_block ();
26196 save = cp_parser_begin_omp_structured_block (parser);
26200 cp_parser_statement (parser, NULL_TREE, false, NULL);
26202 tok = cp_lexer_peek_token (parser->lexer);
26203 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26205 if (tok->type == CPP_CLOSE_BRACE)
26207 if (tok->type == CPP_EOF)
26211 cp_parser_end_omp_structured_block (parser, save);
26212 substmt = finish_omp_structured_block (substmt);
26213 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26214 add_stmt (substmt);
26219 tok = cp_lexer_peek_token (parser->lexer);
26220 if (tok->type == CPP_CLOSE_BRACE)
26222 if (tok->type == CPP_EOF)
26225 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26227 cp_lexer_consume_token (parser->lexer);
26228 cp_parser_require_pragma_eol (parser, tok);
26229 error_suppress = false;
26231 else if (!error_suppress)
26233 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26234 error_suppress = true;
26237 substmt = cp_parser_omp_structured_block (parser);
26238 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26239 add_stmt (substmt);
26241 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26243 substmt = pop_stmt_list (stmt);
26245 stmt = make_node (OMP_SECTIONS);
26246 TREE_TYPE (stmt) = void_type_node;
26247 OMP_SECTIONS_BODY (stmt) = substmt;
26254 # pragma omp sections sections-clause[optseq] newline
26257 #define OMP_SECTIONS_CLAUSE_MASK \
26258 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26259 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26260 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26261 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26262 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26265 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26269 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26270 "#pragma omp sections", pragma_tok);
26272 ret = cp_parser_omp_sections_scope (parser);
26274 OMP_SECTIONS_CLAUSES (ret) = clauses;
26280 # pragma parallel parallel-clause new-line
26281 # pragma parallel for parallel-for-clause new-line
26282 # pragma parallel sections parallel-sections-clause new-line */
26284 #define OMP_PARALLEL_CLAUSE_MASK \
26285 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26286 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26287 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26288 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26289 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26290 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26291 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26292 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26295 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26297 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26298 const char *p_name = "#pragma omp parallel";
26299 tree stmt, clauses, par_clause, ws_clause, block;
26300 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26304 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26306 cp_lexer_consume_token (parser->lexer);
26307 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26308 p_name = "#pragma omp parallel for";
26309 mask |= OMP_FOR_CLAUSE_MASK;
26310 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26312 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26314 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26315 const char *p = IDENTIFIER_POINTER (id);
26316 if (strcmp (p, "sections") == 0)
26318 cp_lexer_consume_token (parser->lexer);
26319 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26320 p_name = "#pragma omp parallel sections";
26321 mask |= OMP_SECTIONS_CLAUSE_MASK;
26322 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26326 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26327 block = begin_omp_parallel ();
26328 save = cp_parser_begin_omp_structured_block (parser);
26332 case PRAGMA_OMP_PARALLEL:
26333 cp_parser_statement (parser, NULL_TREE, false, NULL);
26334 par_clause = clauses;
26337 case PRAGMA_OMP_PARALLEL_FOR:
26338 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26339 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26342 case PRAGMA_OMP_PARALLEL_SECTIONS:
26343 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26344 stmt = cp_parser_omp_sections_scope (parser);
26346 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26350 gcc_unreachable ();
26353 cp_parser_end_omp_structured_block (parser, save);
26354 stmt = finish_omp_parallel (par_clause, block);
26355 if (p_kind != PRAGMA_OMP_PARALLEL)
26356 OMP_PARALLEL_COMBINED (stmt) = 1;
26361 # pragma omp single single-clause[optseq] new-line
26362 structured-block */
26364 #define OMP_SINGLE_CLAUSE_MASK \
26365 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26366 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26367 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26368 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26371 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26373 tree stmt = make_node (OMP_SINGLE);
26374 TREE_TYPE (stmt) = void_type_node;
26376 OMP_SINGLE_CLAUSES (stmt)
26377 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26378 "#pragma omp single", pragma_tok);
26379 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26381 return add_stmt (stmt);
26385 # pragma omp task task-clause[optseq] new-line
26386 structured-block */
26388 #define OMP_TASK_CLAUSE_MASK \
26389 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26390 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26391 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26392 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26393 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26394 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26395 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26396 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26399 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26401 tree clauses, block;
26404 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26405 "#pragma omp task", pragma_tok);
26406 block = begin_omp_task ();
26407 save = cp_parser_begin_omp_structured_block (parser);
26408 cp_parser_statement (parser, NULL_TREE, false, NULL);
26409 cp_parser_end_omp_structured_block (parser, save);
26410 return finish_omp_task (clauses, block);
26414 # pragma omp taskwait new-line */
26417 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26419 cp_parser_require_pragma_eol (parser, pragma_tok);
26420 finish_omp_taskwait ();
26424 # pragma omp taskyield new-line */
26427 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26429 cp_parser_require_pragma_eol (parser, pragma_tok);
26430 finish_omp_taskyield ();
26434 # pragma omp threadprivate (variable-list) */
26437 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26441 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26442 cp_parser_require_pragma_eol (parser, pragma_tok);
26444 finish_omp_threadprivate (vars);
26447 /* Main entry point to OpenMP statement pragmas. */
26450 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26454 switch (pragma_tok->pragma_kind)
26456 case PRAGMA_OMP_ATOMIC:
26457 cp_parser_omp_atomic (parser, pragma_tok);
26459 case PRAGMA_OMP_CRITICAL:
26460 stmt = cp_parser_omp_critical (parser, pragma_tok);
26462 case PRAGMA_OMP_FOR:
26463 stmt = cp_parser_omp_for (parser, pragma_tok);
26465 case PRAGMA_OMP_MASTER:
26466 stmt = cp_parser_omp_master (parser, pragma_tok);
26468 case PRAGMA_OMP_ORDERED:
26469 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26471 case PRAGMA_OMP_PARALLEL:
26472 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26474 case PRAGMA_OMP_SECTIONS:
26475 stmt = cp_parser_omp_sections (parser, pragma_tok);
26477 case PRAGMA_OMP_SINGLE:
26478 stmt = cp_parser_omp_single (parser, pragma_tok);
26480 case PRAGMA_OMP_TASK:
26481 stmt = cp_parser_omp_task (parser, pragma_tok);
26484 gcc_unreachable ();
26488 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26493 static GTY (()) cp_parser *the_parser;
26496 /* Special handling for the first token or line in the file. The first
26497 thing in the file might be #pragma GCC pch_preprocess, which loads a
26498 PCH file, which is a GC collection point. So we need to handle this
26499 first pragma without benefit of an existing lexer structure.
26501 Always returns one token to the caller in *FIRST_TOKEN. This is
26502 either the true first token of the file, or the first token after
26503 the initial pragma. */
26506 cp_parser_initial_pragma (cp_token *first_token)
26510 cp_lexer_get_preprocessor_token (NULL, first_token);
26511 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26514 cp_lexer_get_preprocessor_token (NULL, first_token);
26515 if (first_token->type == CPP_STRING)
26517 name = first_token->u.value;
26519 cp_lexer_get_preprocessor_token (NULL, first_token);
26520 if (first_token->type != CPP_PRAGMA_EOL)
26521 error_at (first_token->location,
26522 "junk at end of %<#pragma GCC pch_preprocess%>");
26525 error_at (first_token->location, "expected string literal");
26527 /* Skip to the end of the pragma. */
26528 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26529 cp_lexer_get_preprocessor_token (NULL, first_token);
26531 /* Now actually load the PCH file. */
26533 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
26535 /* Read one more token to return to our caller. We have to do this
26536 after reading the PCH file in, since its pointers have to be
26538 cp_lexer_get_preprocessor_token (NULL, first_token);
26541 /* Normal parsing of a pragma token. Here we can (and must) use the
26545 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
26547 cp_token *pragma_tok;
26550 pragma_tok = cp_lexer_consume_token (parser->lexer);
26551 gcc_assert (pragma_tok->type == CPP_PRAGMA);
26552 parser->lexer->in_pragma = true;
26554 id = pragma_tok->pragma_kind;
26557 case PRAGMA_GCC_PCH_PREPROCESS:
26558 error_at (pragma_tok->location,
26559 "%<#pragma GCC pch_preprocess%> must be first");
26562 case PRAGMA_OMP_BARRIER:
26565 case pragma_compound:
26566 cp_parser_omp_barrier (parser, pragma_tok);
26569 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
26570 "used in compound statements");
26577 case PRAGMA_OMP_FLUSH:
26580 case pragma_compound:
26581 cp_parser_omp_flush (parser, pragma_tok);
26584 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
26585 "used in compound statements");
26592 case PRAGMA_OMP_TASKWAIT:
26595 case pragma_compound:
26596 cp_parser_omp_taskwait (parser, pragma_tok);
26599 error_at (pragma_tok->location,
26600 "%<#pragma omp taskwait%> may only be "
26601 "used in compound statements");
26608 case PRAGMA_OMP_TASKYIELD:
26611 case pragma_compound:
26612 cp_parser_omp_taskyield (parser, pragma_tok);
26615 error_at (pragma_tok->location,
26616 "%<#pragma omp taskyield%> may only be "
26617 "used in compound statements");
26624 case PRAGMA_OMP_THREADPRIVATE:
26625 cp_parser_omp_threadprivate (parser, pragma_tok);
26628 case PRAGMA_OMP_ATOMIC:
26629 case PRAGMA_OMP_CRITICAL:
26630 case PRAGMA_OMP_FOR:
26631 case PRAGMA_OMP_MASTER:
26632 case PRAGMA_OMP_ORDERED:
26633 case PRAGMA_OMP_PARALLEL:
26634 case PRAGMA_OMP_SECTIONS:
26635 case PRAGMA_OMP_SINGLE:
26636 case PRAGMA_OMP_TASK:
26637 if (context == pragma_external)
26639 cp_parser_omp_construct (parser, pragma_tok);
26642 case PRAGMA_OMP_SECTION:
26643 error_at (pragma_tok->location,
26644 "%<#pragma omp section%> may only be used in "
26645 "%<#pragma omp sections%> construct");
26649 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
26650 c_invoke_pragma_handler (id);
26654 cp_parser_error (parser, "expected declaration specifiers");
26658 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26662 /* The interface the pragma parsers have to the lexer. */
26665 pragma_lex (tree *value)
26668 enum cpp_ttype ret;
26670 tok = cp_lexer_peek_token (the_parser->lexer);
26673 *value = tok->u.value;
26675 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26677 else if (ret == CPP_STRING)
26678 *value = cp_parser_string_literal (the_parser, false, false);
26681 cp_lexer_consume_token (the_parser->lexer);
26682 if (ret == CPP_KEYWORD)
26690 /* External interface. */
26692 /* Parse one entire translation unit. */
26695 c_parse_file (void)
26697 static bool already_called = false;
26699 if (already_called)
26701 sorry ("inter-module optimizations not implemented for C++");
26704 already_called = true;
26706 the_parser = cp_parser_new ();
26707 push_deferring_access_checks (flag_access_control
26708 ? dk_no_deferred : dk_no_check);
26709 cp_parser_translation_unit (the_parser);
26713 #include "gt-cp-parser.h"