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
3672 cp_parser_userdef_string_literal (cp_token *token)
3674 tree literal, suffix_id, value;
3680 literal = token->u.value;
3681 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3682 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3683 value = USERDEF_LITERAL_VALUE (literal);
3684 len = TREE_STRING_LENGTH (value) - 1;
3686 /* Build up a call to the user-defined operator */
3687 /* Lookup the name we got back from the id-expression. */
3688 vec = make_tree_vector ();
3689 VEC_safe_push (tree, gc, vec, value);
3690 VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3691 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3692 if (!decl || decl == error_mark_node)
3694 error ("unable to find user-defined string literal operator %qD", name);
3695 release_tree_vector (vec);
3696 return error_mark_node;
3698 result = finish_call_expr (decl, &vec, false, true, tf_none);
3699 if (result == error_mark_node)
3700 error ("unable to find valid user-defined string literal operator %qD."
3701 " Possible missing length argument in string literal operator.",
3703 release_tree_vector (vec);
3709 /* Basic concepts [gram.basic] */
3711 /* Parse a translation-unit.
3714 declaration-seq [opt]
3716 Returns TRUE if all went well. */
3719 cp_parser_translation_unit (cp_parser* parser)
3721 /* The address of the first non-permanent object on the declarator
3723 static void *declarator_obstack_base;
3727 /* Create the declarator obstack, if necessary. */
3728 if (!cp_error_declarator)
3730 gcc_obstack_init (&declarator_obstack);
3731 /* Create the error declarator. */
3732 cp_error_declarator = make_declarator (cdk_error);
3733 /* Create the empty parameter list. */
3734 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3735 /* Remember where the base of the declarator obstack lies. */
3736 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3739 cp_parser_declaration_seq_opt (parser);
3741 /* If there are no tokens left then all went well. */
3742 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3744 /* Get rid of the token array; we don't need it any more. */
3745 cp_lexer_destroy (parser->lexer);
3746 parser->lexer = NULL;
3748 /* This file might have been a context that's implicitly extern
3749 "C". If so, pop the lang context. (Only relevant for PCH.) */
3750 if (parser->implicit_extern_c)
3752 pop_lang_context ();
3753 parser->implicit_extern_c = false;
3757 finish_translation_unit ();
3763 cp_parser_error (parser, "expected declaration");
3767 /* Make sure the declarator obstack was fully cleaned up. */
3768 gcc_assert (obstack_next_free (&declarator_obstack)
3769 == declarator_obstack_base);
3771 /* All went well. */
3775 /* Expressions [gram.expr] */
3777 /* Parse a primary-expression.
3788 ( compound-statement )
3789 __builtin_va_arg ( assignment-expression , type-id )
3790 __builtin_offsetof ( type-id , offsetof-expression )
3793 __has_nothrow_assign ( type-id )
3794 __has_nothrow_constructor ( type-id )
3795 __has_nothrow_copy ( type-id )
3796 __has_trivial_assign ( type-id )
3797 __has_trivial_constructor ( type-id )
3798 __has_trivial_copy ( type-id )
3799 __has_trivial_destructor ( type-id )
3800 __has_virtual_destructor ( type-id )
3801 __is_abstract ( type-id )
3802 __is_base_of ( type-id , type-id )
3803 __is_class ( type-id )
3804 __is_convertible_to ( type-id , type-id )
3805 __is_empty ( type-id )
3806 __is_enum ( type-id )
3807 __is_literal_type ( type-id )
3808 __is_pod ( type-id )
3809 __is_polymorphic ( type-id )
3810 __is_std_layout ( type-id )
3811 __is_trivial ( type-id )
3812 __is_union ( type-id )
3814 Objective-C++ Extension:
3822 ADDRESS_P is true iff this expression was immediately preceded by
3823 "&" and therefore might denote a pointer-to-member. CAST_P is true
3824 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3825 true iff this expression is a template argument.
3827 Returns a representation of the expression. Upon return, *IDK
3828 indicates what kind of id-expression (if any) was present. */
3831 cp_parser_primary_expression (cp_parser *parser,
3834 bool template_arg_p,
3837 cp_token *token = NULL;
3839 /* Assume the primary expression is not an id-expression. */
3840 *idk = CP_ID_KIND_NONE;
3842 /* Peek at the next token. */
3843 token = cp_lexer_peek_token (parser->lexer);
3844 switch (token->type)
3853 user-defined-literal */
3859 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3860 return cp_parser_userdef_numeric_literal (parser);
3861 token = cp_lexer_consume_token (parser->lexer);
3862 if (TREE_CODE (token->u.value) == FIXED_CST)
3864 error_at (token->location,
3865 "fixed-point types not supported in C++");
3866 return error_mark_node;
3868 /* Floating-point literals are only allowed in an integral
3869 constant expression if they are cast to an integral or
3870 enumeration type. */
3871 if (TREE_CODE (token->u.value) == REAL_CST
3872 && parser->integral_constant_expression_p
3875 /* CAST_P will be set even in invalid code like "int(2.7 +
3876 ...)". Therefore, we have to check that the next token
3877 is sure to end the cast. */
3880 cp_token *next_token;
3882 next_token = cp_lexer_peek_token (parser->lexer);
3883 if (/* The comma at the end of an
3884 enumerator-definition. */
3885 next_token->type != CPP_COMMA
3886 /* The curly brace at the end of an enum-specifier. */
3887 && next_token->type != CPP_CLOSE_BRACE
3888 /* The end of a statement. */
3889 && next_token->type != CPP_SEMICOLON
3890 /* The end of the cast-expression. */
3891 && next_token->type != CPP_CLOSE_PAREN
3892 /* The end of an array bound. */
3893 && next_token->type != CPP_CLOSE_SQUARE
3894 /* The closing ">" in a template-argument-list. */
3895 && (next_token->type != CPP_GREATER
3896 || parser->greater_than_is_operator_p)
3897 /* C++0x only: A ">>" treated like two ">" tokens,
3898 in a template-argument-list. */
3899 && (next_token->type != CPP_RSHIFT
3900 || (cxx_dialect == cxx98)
3901 || parser->greater_than_is_operator_p))
3905 /* If we are within a cast, then the constraint that the
3906 cast is to an integral or enumeration type will be
3907 checked at that point. If we are not within a cast, then
3908 this code is invalid. */
3910 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3912 return token->u.value;
3914 case CPP_CHAR_USERDEF:
3915 case CPP_CHAR16_USERDEF:
3916 case CPP_CHAR32_USERDEF:
3917 case CPP_WCHAR_USERDEF:
3918 return cp_parser_userdef_char_literal (parser);
3924 case CPP_UTF8STRING:
3925 case CPP_STRING_USERDEF:
3926 case CPP_STRING16_USERDEF:
3927 case CPP_STRING32_USERDEF:
3928 case CPP_WSTRING_USERDEF:
3929 case CPP_UTF8STRING_USERDEF:
3930 /* ??? Should wide strings be allowed when parser->translate_strings_p
3931 is false (i.e. in attributes)? If not, we can kill the third
3932 argument to cp_parser_string_literal. */
3933 return cp_parser_string_literal (parser,
3934 parser->translate_strings_p,
3937 case CPP_OPEN_PAREN:
3940 bool saved_greater_than_is_operator_p;
3942 /* Consume the `('. */
3943 cp_lexer_consume_token (parser->lexer);
3944 /* Within a parenthesized expression, a `>' token is always
3945 the greater-than operator. */
3946 saved_greater_than_is_operator_p
3947 = parser->greater_than_is_operator_p;
3948 parser->greater_than_is_operator_p = true;
3949 /* If we see `( { ' then we are looking at the beginning of
3950 a GNU statement-expression. */
3951 if (cp_parser_allow_gnu_extensions_p (parser)
3952 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3954 /* Statement-expressions are not allowed by the standard. */
3955 pedwarn (token->location, OPT_pedantic,
3956 "ISO C++ forbids braced-groups within expressions");
3958 /* And they're not allowed outside of a function-body; you
3959 cannot, for example, write:
3961 int i = ({ int j = 3; j + 1; });
3963 at class or namespace scope. */
3964 if (!parser->in_function_body
3965 || parser->in_template_argument_list_p)
3967 error_at (token->location,
3968 "statement-expressions are not allowed outside "
3969 "functions nor in template-argument lists");
3970 cp_parser_skip_to_end_of_block_or_statement (parser);
3971 expr = error_mark_node;
3975 /* Start the statement-expression. */
3976 expr = begin_stmt_expr ();
3977 /* Parse the compound-statement. */
3978 cp_parser_compound_statement (parser, expr, false, false);
3980 expr = finish_stmt_expr (expr, false);
3985 /* Parse the parenthesized expression. */
3986 expr = cp_parser_expression (parser, cast_p, idk);
3987 /* Let the front end know that this expression was
3988 enclosed in parentheses. This matters in case, for
3989 example, the expression is of the form `A::B', since
3990 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3992 finish_parenthesized_expr (expr);
3993 /* DR 705: Wrapping an unqualified name in parentheses
3994 suppresses arg-dependent lookup. We want to pass back
3995 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3996 (c++/37862), but none of the others. */
3997 if (*idk != CP_ID_KIND_QUALIFIED)
3998 *idk = CP_ID_KIND_NONE;
4000 /* The `>' token might be the end of a template-id or
4001 template-parameter-list now. */
4002 parser->greater_than_is_operator_p
4003 = saved_greater_than_is_operator_p;
4004 /* Consume the `)'. */
4005 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4006 cp_parser_skip_to_end_of_statement (parser);
4011 case CPP_OPEN_SQUARE:
4012 if (c_dialect_objc ())
4013 /* We have an Objective-C++ message. */
4014 return cp_parser_objc_expression (parser);
4016 tree lam = cp_parser_lambda_expression (parser);
4017 /* Don't warn about a failed tentative parse. */
4018 if (cp_parser_error_occurred (parser))
4019 return error_mark_node;
4020 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4024 case CPP_OBJC_STRING:
4025 if (c_dialect_objc ())
4026 /* We have an Objective-C++ string literal. */
4027 return cp_parser_objc_expression (parser);
4028 cp_parser_error (parser, "expected primary-expression");
4029 return error_mark_node;
4032 switch (token->keyword)
4034 /* These two are the boolean literals. */
4036 cp_lexer_consume_token (parser->lexer);
4037 return boolean_true_node;
4039 cp_lexer_consume_token (parser->lexer);
4040 return boolean_false_node;
4042 /* The `__null' literal. */
4044 cp_lexer_consume_token (parser->lexer);
4047 /* The `nullptr' literal. */
4049 cp_lexer_consume_token (parser->lexer);
4050 return nullptr_node;
4052 /* Recognize the `this' keyword. */
4054 cp_lexer_consume_token (parser->lexer);
4055 if (parser->local_variables_forbidden_p)
4057 error_at (token->location,
4058 "%<this%> may not be used in this context");
4059 return error_mark_node;
4061 /* Pointers cannot appear in constant-expressions. */
4062 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4063 return error_mark_node;
4064 return finish_this_expr ();
4066 /* The `operator' keyword can be the beginning of an
4071 case RID_FUNCTION_NAME:
4072 case RID_PRETTY_FUNCTION_NAME:
4073 case RID_C99_FUNCTION_NAME:
4075 non_integral_constant name;
4077 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4078 __func__ are the names of variables -- but they are
4079 treated specially. Therefore, they are handled here,
4080 rather than relying on the generic id-expression logic
4081 below. Grammatically, these names are id-expressions.
4083 Consume the token. */
4084 token = cp_lexer_consume_token (parser->lexer);
4086 switch (token->keyword)
4088 case RID_FUNCTION_NAME:
4089 name = NIC_FUNC_NAME;
4091 case RID_PRETTY_FUNCTION_NAME:
4092 name = NIC_PRETTY_FUNC;
4094 case RID_C99_FUNCTION_NAME:
4095 name = NIC_C99_FUNC;
4101 if (cp_parser_non_integral_constant_expression (parser, name))
4102 return error_mark_node;
4104 /* Look up the name. */
4105 return finish_fname (token->u.value);
4113 /* The `__builtin_va_arg' construct is used to handle
4114 `va_arg'. Consume the `__builtin_va_arg' token. */
4115 cp_lexer_consume_token (parser->lexer);
4116 /* Look for the opening `('. */
4117 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4118 /* Now, parse the assignment-expression. */
4119 expression = cp_parser_assignment_expression (parser,
4120 /*cast_p=*/false, NULL);
4121 /* Look for the `,'. */
4122 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4123 /* Parse the type-id. */
4124 type = cp_parser_type_id (parser);
4125 /* Look for the closing `)'. */
4126 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4127 /* Using `va_arg' in a constant-expression is not
4129 if (cp_parser_non_integral_constant_expression (parser,
4131 return error_mark_node;
4132 return build_x_va_arg (expression, type);
4136 return cp_parser_builtin_offsetof (parser);
4138 case RID_HAS_NOTHROW_ASSIGN:
4139 case RID_HAS_NOTHROW_CONSTRUCTOR:
4140 case RID_HAS_NOTHROW_COPY:
4141 case RID_HAS_TRIVIAL_ASSIGN:
4142 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4143 case RID_HAS_TRIVIAL_COPY:
4144 case RID_HAS_TRIVIAL_DESTRUCTOR:
4145 case RID_HAS_VIRTUAL_DESTRUCTOR:
4146 case RID_IS_ABSTRACT:
4147 case RID_IS_BASE_OF:
4149 case RID_IS_CONVERTIBLE_TO:
4152 case RID_IS_LITERAL_TYPE:
4154 case RID_IS_POLYMORPHIC:
4155 case RID_IS_STD_LAYOUT:
4156 case RID_IS_TRIVIAL:
4158 return cp_parser_trait_expr (parser, token->keyword);
4160 /* Objective-C++ expressions. */
4162 case RID_AT_PROTOCOL:
4163 case RID_AT_SELECTOR:
4164 return cp_parser_objc_expression (parser);
4167 if (parser->in_function_body
4168 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4171 error_at (token->location,
4172 "a template declaration cannot appear at block scope");
4173 cp_parser_skip_to_end_of_block_or_statement (parser);
4174 return error_mark_node;
4177 cp_parser_error (parser, "expected primary-expression");
4178 return error_mark_node;
4181 /* An id-expression can start with either an identifier, a
4182 `::' as the beginning of a qualified-id, or the "operator"
4186 case CPP_TEMPLATE_ID:
4187 case CPP_NESTED_NAME_SPECIFIER:
4191 const char *error_msg;
4194 cp_token *id_expr_token;
4197 /* Parse the id-expression. */
4199 = cp_parser_id_expression (parser,
4200 /*template_keyword_p=*/false,
4201 /*check_dependency_p=*/true,
4203 /*declarator_p=*/false,
4204 /*optional_p=*/false);
4205 if (id_expression == error_mark_node)
4206 return error_mark_node;
4207 id_expr_token = token;
4208 token = cp_lexer_peek_token (parser->lexer);
4209 done = (token->type != CPP_OPEN_SQUARE
4210 && token->type != CPP_OPEN_PAREN
4211 && token->type != CPP_DOT
4212 && token->type != CPP_DEREF
4213 && token->type != CPP_PLUS_PLUS
4214 && token->type != CPP_MINUS_MINUS);
4215 /* If we have a template-id, then no further lookup is
4216 required. If the template-id was for a template-class, we
4217 will sometimes have a TYPE_DECL at this point. */
4218 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4219 || TREE_CODE (id_expression) == TYPE_DECL)
4220 decl = id_expression;
4221 /* Look up the name. */
4224 tree ambiguous_decls;
4226 /* If we already know that this lookup is ambiguous, then
4227 we've already issued an error message; there's no reason
4229 if (id_expr_token->type == CPP_NAME
4230 && id_expr_token->ambiguous_p)
4232 cp_parser_simulate_error (parser);
4233 return error_mark_node;
4236 decl = cp_parser_lookup_name (parser, id_expression,
4239 /*is_namespace=*/false,
4240 /*check_dependency=*/true,
4242 id_expr_token->location);
4243 /* If the lookup was ambiguous, an error will already have
4245 if (ambiguous_decls)
4246 return error_mark_node;
4248 /* In Objective-C++, we may have an Objective-C 2.0
4249 dot-syntax for classes here. */
4250 if (c_dialect_objc ()
4251 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4252 && TREE_CODE (decl) == TYPE_DECL
4253 && objc_is_class_name (decl))
4256 cp_lexer_consume_token (parser->lexer);
4257 component = cp_parser_identifier (parser);
4258 if (component == error_mark_node)
4259 return error_mark_node;
4261 return objc_build_class_component_ref (id_expression, component);
4264 /* In Objective-C++, an instance variable (ivar) may be preferred
4265 to whatever cp_parser_lookup_name() found. */
4266 decl = objc_lookup_ivar (decl, id_expression);
4268 /* If name lookup gives us a SCOPE_REF, then the
4269 qualifying scope was dependent. */
4270 if (TREE_CODE (decl) == SCOPE_REF)
4272 /* At this point, we do not know if DECL is a valid
4273 integral constant expression. We assume that it is
4274 in fact such an expression, so that code like:
4276 template <int N> struct A {
4280 is accepted. At template-instantiation time, we
4281 will check that B<N>::i is actually a constant. */
4284 /* Check to see if DECL is a local variable in a context
4285 where that is forbidden. */
4286 if (parser->local_variables_forbidden_p
4287 && local_variable_p (decl))
4289 /* It might be that we only found DECL because we are
4290 trying to be generous with pre-ISO scoping rules.
4291 For example, consider:
4295 for (int i = 0; i < 10; ++i) {}
4296 extern void f(int j = i);
4299 Here, name look up will originally find the out
4300 of scope `i'. We need to issue a warning message,
4301 but then use the global `i'. */
4302 decl = check_for_out_of_scope_variable (decl);
4303 if (local_variable_p (decl))
4305 error_at (id_expr_token->location,
4306 "local variable %qD may not appear in this context",
4308 return error_mark_node;
4313 decl = (finish_id_expression
4314 (id_expression, decl, parser->scope,
4316 parser->integral_constant_expression_p,
4317 parser->allow_non_integral_constant_expression_p,
4318 &parser->non_integral_constant_expression_p,
4319 template_p, done, address_p,
4322 id_expr_token->location));
4324 cp_parser_error (parser, error_msg);
4328 /* Anything else is an error. */
4330 cp_parser_error (parser, "expected primary-expression");
4331 return error_mark_node;
4335 /* Parse an id-expression.
4342 :: [opt] nested-name-specifier template [opt] unqualified-id
4344 :: operator-function-id
4347 Return a representation of the unqualified portion of the
4348 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4349 a `::' or nested-name-specifier.
4351 Often, if the id-expression was a qualified-id, the caller will
4352 want to make a SCOPE_REF to represent the qualified-id. This
4353 function does not do this in order to avoid wastefully creating
4354 SCOPE_REFs when they are not required.
4356 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4359 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4360 uninstantiated templates.
4362 If *TEMPLATE_P is non-NULL, it is set to true iff the
4363 `template' keyword is used to explicitly indicate that the entity
4364 named is a template.
4366 If DECLARATOR_P is true, the id-expression is appearing as part of
4367 a declarator, rather than as part of an expression. */
4370 cp_parser_id_expression (cp_parser *parser,
4371 bool template_keyword_p,
4372 bool check_dependency_p,
4377 bool global_scope_p;
4378 bool nested_name_specifier_p;
4380 /* Assume the `template' keyword was not used. */
4382 *template_p = template_keyword_p;
4384 /* Look for the optional `::' operator. */
4386 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4388 /* Look for the optional nested-name-specifier. */
4389 nested_name_specifier_p
4390 = (cp_parser_nested_name_specifier_opt (parser,
4391 /*typename_keyword_p=*/false,
4396 /* If there is a nested-name-specifier, then we are looking at
4397 the first qualified-id production. */
4398 if (nested_name_specifier_p)
4401 tree saved_object_scope;
4402 tree saved_qualifying_scope;
4403 tree unqualified_id;
4406 /* See if the next token is the `template' keyword. */
4408 template_p = &is_template;
4409 *template_p = cp_parser_optional_template_keyword (parser);
4410 /* Name lookup we do during the processing of the
4411 unqualified-id might obliterate SCOPE. */
4412 saved_scope = parser->scope;
4413 saved_object_scope = parser->object_scope;
4414 saved_qualifying_scope = parser->qualifying_scope;
4415 /* Process the final unqualified-id. */
4416 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4419 /*optional_p=*/false);
4420 /* Restore the SAVED_SCOPE for our caller. */
4421 parser->scope = saved_scope;
4422 parser->object_scope = saved_object_scope;
4423 parser->qualifying_scope = saved_qualifying_scope;
4425 return unqualified_id;
4427 /* Otherwise, if we are in global scope, then we are looking at one
4428 of the other qualified-id productions. */
4429 else if (global_scope_p)
4434 /* Peek at the next token. */
4435 token = cp_lexer_peek_token (parser->lexer);
4437 /* If it's an identifier, and the next token is not a "<", then
4438 we can avoid the template-id case. This is an optimization
4439 for this common case. */
4440 if (token->type == CPP_NAME
4441 && !cp_parser_nth_token_starts_template_argument_list_p
4443 return cp_parser_identifier (parser);
4445 cp_parser_parse_tentatively (parser);
4446 /* Try a template-id. */
4447 id = cp_parser_template_id (parser,
4448 /*template_keyword_p=*/false,
4449 /*check_dependency_p=*/true,
4451 /* If that worked, we're done. */
4452 if (cp_parser_parse_definitely (parser))
4455 /* Peek at the next token. (Changes in the token buffer may
4456 have invalidated the pointer obtained above.) */
4457 token = cp_lexer_peek_token (parser->lexer);
4459 switch (token->type)
4462 return cp_parser_identifier (parser);
4465 if (token->keyword == RID_OPERATOR)
4466 return cp_parser_operator_function_id (parser);
4470 cp_parser_error (parser, "expected id-expression");
4471 return error_mark_node;
4475 return cp_parser_unqualified_id (parser, template_keyword_p,
4476 /*check_dependency_p=*/true,
4481 /* Parse an unqualified-id.
4485 operator-function-id
4486 conversion-function-id
4490 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4491 keyword, in a construct like `A::template ...'.
4493 Returns a representation of unqualified-id. For the `identifier'
4494 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4495 production a BIT_NOT_EXPR is returned; the operand of the
4496 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4497 other productions, see the documentation accompanying the
4498 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4499 names are looked up in uninstantiated templates. If DECLARATOR_P
4500 is true, the unqualified-id is appearing as part of a declarator,
4501 rather than as part of an expression. */
4504 cp_parser_unqualified_id (cp_parser* parser,
4505 bool template_keyword_p,
4506 bool check_dependency_p,
4512 /* Peek at the next token. */
4513 token = cp_lexer_peek_token (parser->lexer);
4515 switch (token->type)
4521 /* We don't know yet whether or not this will be a
4523 cp_parser_parse_tentatively (parser);
4524 /* Try a template-id. */
4525 id = cp_parser_template_id (parser, template_keyword_p,
4528 /* If it worked, we're done. */
4529 if (cp_parser_parse_definitely (parser))
4531 /* Otherwise, it's an ordinary identifier. */
4532 return cp_parser_identifier (parser);
4535 case CPP_TEMPLATE_ID:
4536 return cp_parser_template_id (parser, template_keyword_p,
4543 tree qualifying_scope;
4548 /* Consume the `~' token. */
4549 cp_lexer_consume_token (parser->lexer);
4550 /* Parse the class-name. The standard, as written, seems to
4553 template <typename T> struct S { ~S (); };
4554 template <typename T> S<T>::~S() {}
4556 is invalid, since `~' must be followed by a class-name, but
4557 `S<T>' is dependent, and so not known to be a class.
4558 That's not right; we need to look in uninstantiated
4559 templates. A further complication arises from:
4561 template <typename T> void f(T t) {
4565 Here, it is not possible to look up `T' in the scope of `T'
4566 itself. We must look in both the current scope, and the
4567 scope of the containing complete expression.
4569 Yet another issue is:
4578 The standard does not seem to say that the `S' in `~S'
4579 should refer to the type `S' and not the data member
4582 /* DR 244 says that we look up the name after the "~" in the
4583 same scope as we looked up the qualifying name. That idea
4584 isn't fully worked out; it's more complicated than that. */
4585 scope = parser->scope;
4586 object_scope = parser->object_scope;
4587 qualifying_scope = parser->qualifying_scope;
4589 /* Check for invalid scopes. */
4590 if (scope == error_mark_node)
4592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4593 cp_lexer_consume_token (parser->lexer);
4594 return error_mark_node;
4596 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4598 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4599 error_at (token->location,
4600 "scope %qT before %<~%> is not a class-name",
4602 cp_parser_simulate_error (parser);
4603 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4604 cp_lexer_consume_token (parser->lexer);
4605 return error_mark_node;
4607 gcc_assert (!scope || TYPE_P (scope));
4609 /* If the name is of the form "X::~X" it's OK even if X is a
4611 token = cp_lexer_peek_token (parser->lexer);
4613 && token->type == CPP_NAME
4614 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4616 && (token->u.value == TYPE_IDENTIFIER (scope)
4617 || (CLASS_TYPE_P (scope)
4618 && constructor_name_p (token->u.value, scope))))
4620 cp_lexer_consume_token (parser->lexer);
4621 return build_nt (BIT_NOT_EXPR, scope);
4624 /* If there was an explicit qualification (S::~T), first look
4625 in the scope given by the qualification (i.e., S).
4627 Note: in the calls to cp_parser_class_name below we pass
4628 typename_type so that lookup finds the injected-class-name
4629 rather than the constructor. */
4631 type_decl = NULL_TREE;
4634 cp_parser_parse_tentatively (parser);
4635 type_decl = cp_parser_class_name (parser,
4636 /*typename_keyword_p=*/false,
4637 /*template_keyword_p=*/false,
4639 /*check_dependency=*/false,
4640 /*class_head_p=*/false,
4642 if (cp_parser_parse_definitely (parser))
4645 /* In "N::S::~S", look in "N" as well. */
4646 if (!done && scope && qualifying_scope)
4648 cp_parser_parse_tentatively (parser);
4649 parser->scope = qualifying_scope;
4650 parser->object_scope = NULL_TREE;
4651 parser->qualifying_scope = NULL_TREE;
4653 = cp_parser_class_name (parser,
4654 /*typename_keyword_p=*/false,
4655 /*template_keyword_p=*/false,
4657 /*check_dependency=*/false,
4658 /*class_head_p=*/false,
4660 if (cp_parser_parse_definitely (parser))
4663 /* In "p->S::~T", look in the scope given by "*p" as well. */
4664 else if (!done && object_scope)
4666 cp_parser_parse_tentatively (parser);
4667 parser->scope = object_scope;
4668 parser->object_scope = NULL_TREE;
4669 parser->qualifying_scope = NULL_TREE;
4671 = cp_parser_class_name (parser,
4672 /*typename_keyword_p=*/false,
4673 /*template_keyword_p=*/false,
4675 /*check_dependency=*/false,
4676 /*class_head_p=*/false,
4678 if (cp_parser_parse_definitely (parser))
4681 /* Look in the surrounding context. */
4684 parser->scope = NULL_TREE;
4685 parser->object_scope = NULL_TREE;
4686 parser->qualifying_scope = NULL_TREE;
4687 if (processing_template_decl)
4688 cp_parser_parse_tentatively (parser);
4690 = cp_parser_class_name (parser,
4691 /*typename_keyword_p=*/false,
4692 /*template_keyword_p=*/false,
4694 /*check_dependency=*/false,
4695 /*class_head_p=*/false,
4697 if (processing_template_decl
4698 && ! cp_parser_parse_definitely (parser))
4700 /* We couldn't find a type with this name, so just accept
4701 it and check for a match at instantiation time. */
4702 type_decl = cp_parser_identifier (parser);
4703 if (type_decl != error_mark_node)
4704 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4708 /* If an error occurred, assume that the name of the
4709 destructor is the same as the name of the qualifying
4710 class. That allows us to keep parsing after running
4711 into ill-formed destructor names. */
4712 if (type_decl == error_mark_node && scope)
4713 return build_nt (BIT_NOT_EXPR, scope);
4714 else if (type_decl == error_mark_node)
4715 return error_mark_node;
4717 /* Check that destructor name and scope match. */
4718 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4720 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4721 error_at (token->location,
4722 "declaration of %<~%T%> as member of %qT",
4724 cp_parser_simulate_error (parser);
4725 return error_mark_node;
4730 A typedef-name that names a class shall not be used as the
4731 identifier in the declarator for a destructor declaration. */
4733 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4734 && !DECL_SELF_REFERENCE_P (type_decl)
4735 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4736 error_at (token->location,
4737 "typedef-name %qD used as destructor declarator",
4740 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4744 if (token->keyword == RID_OPERATOR)
4748 /* This could be a template-id, so we try that first. */
4749 cp_parser_parse_tentatively (parser);
4750 /* Try a template-id. */
4751 id = cp_parser_template_id (parser, template_keyword_p,
4752 /*check_dependency_p=*/true,
4754 /* If that worked, we're done. */
4755 if (cp_parser_parse_definitely (parser))
4757 /* We still don't know whether we're looking at an
4758 operator-function-id or a conversion-function-id. */
4759 cp_parser_parse_tentatively (parser);
4760 /* Try an operator-function-id. */
4761 id = cp_parser_operator_function_id (parser);
4762 /* If that didn't work, try a conversion-function-id. */
4763 if (!cp_parser_parse_definitely (parser))
4764 id = cp_parser_conversion_function_id (parser);
4765 else if (UDLIT_OPER_P (id))
4768 const char *name = UDLIT_OP_SUFFIX (id);
4769 if (name[0] != '_' && !in_system_header)
4770 warning (0, "literal operator suffixes not preceded by %<_%>"
4771 " are reserved for future standardization");
4781 cp_parser_error (parser, "expected unqualified-id");
4782 return error_mark_node;
4786 /* Parse an (optional) nested-name-specifier.
4788 nested-name-specifier: [C++98]
4789 class-or-namespace-name :: nested-name-specifier [opt]
4790 class-or-namespace-name :: template nested-name-specifier [opt]
4792 nested-name-specifier: [C++0x]
4795 nested-name-specifier identifier ::
4796 nested-name-specifier template [opt] simple-template-id ::
4798 PARSER->SCOPE should be set appropriately before this function is
4799 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4800 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4803 Sets PARSER->SCOPE to the class (TYPE) or namespace
4804 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4805 it unchanged if there is no nested-name-specifier. Returns the new
4806 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4808 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4809 part of a declaration and/or decl-specifier. */
4812 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4813 bool typename_keyword_p,
4814 bool check_dependency_p,
4816 bool is_declaration)
4818 bool success = false;
4819 cp_token_position start = 0;
4822 /* Remember where the nested-name-specifier starts. */
4823 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4825 start = cp_lexer_token_position (parser->lexer, false);
4826 push_deferring_access_checks (dk_deferred);
4833 tree saved_qualifying_scope;
4834 bool template_keyword_p;
4836 /* Spot cases that cannot be the beginning of a
4837 nested-name-specifier. */
4838 token = cp_lexer_peek_token (parser->lexer);
4840 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4841 the already parsed nested-name-specifier. */
4842 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4844 /* Grab the nested-name-specifier and continue the loop. */
4845 cp_parser_pre_parsed_nested_name_specifier (parser);
4846 /* If we originally encountered this nested-name-specifier
4847 with IS_DECLARATION set to false, we will not have
4848 resolved TYPENAME_TYPEs, so we must do so here. */
4850 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4852 new_scope = resolve_typename_type (parser->scope,
4853 /*only_current_p=*/false);
4854 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4855 parser->scope = new_scope;
4861 /* Spot cases that cannot be the beginning of a
4862 nested-name-specifier. On the second and subsequent times
4863 through the loop, we look for the `template' keyword. */
4864 if (success && token->keyword == RID_TEMPLATE)
4866 /* A template-id can start a nested-name-specifier. */
4867 else if (token->type == CPP_TEMPLATE_ID)
4869 /* DR 743: decltype can be used in a nested-name-specifier. */
4870 else if (token_is_decltype (token))
4874 /* If the next token is not an identifier, then it is
4875 definitely not a type-name or namespace-name. */
4876 if (token->type != CPP_NAME)
4878 /* If the following token is neither a `<' (to begin a
4879 template-id), nor a `::', then we are not looking at a
4880 nested-name-specifier. */
4881 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4883 if (token->type == CPP_COLON
4884 && parser->colon_corrects_to_scope_p
4885 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4887 error_at (token->location,
4888 "found %<:%> in nested-name-specifier, expected %<::%>");
4889 token->type = CPP_SCOPE;
4892 if (token->type != CPP_SCOPE
4893 && !cp_parser_nth_token_starts_template_argument_list_p
4898 /* The nested-name-specifier is optional, so we parse
4900 cp_parser_parse_tentatively (parser);
4902 /* Look for the optional `template' keyword, if this isn't the
4903 first time through the loop. */
4905 template_keyword_p = cp_parser_optional_template_keyword (parser);
4907 template_keyword_p = false;
4909 /* Save the old scope since the name lookup we are about to do
4910 might destroy it. */
4911 old_scope = parser->scope;
4912 saved_qualifying_scope = parser->qualifying_scope;
4913 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4914 look up names in "X<T>::I" in order to determine that "Y" is
4915 a template. So, if we have a typename at this point, we make
4916 an effort to look through it. */
4918 && !typename_keyword_p
4920 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4921 parser->scope = resolve_typename_type (parser->scope,
4922 /*only_current_p=*/false);
4923 /* Parse the qualifying entity. */
4925 = cp_parser_qualifying_entity (parser,
4931 /* Look for the `::' token. */
4932 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4934 /* If we found what we wanted, we keep going; otherwise, we're
4936 if (!cp_parser_parse_definitely (parser))
4938 bool error_p = false;
4940 /* Restore the OLD_SCOPE since it was valid before the
4941 failed attempt at finding the last
4942 class-or-namespace-name. */
4943 parser->scope = old_scope;
4944 parser->qualifying_scope = saved_qualifying_scope;
4946 /* If the next token is a decltype, and the one after that is a
4947 `::', then the decltype has failed to resolve to a class or
4948 enumeration type. Give this error even when parsing
4949 tentatively since it can't possibly be valid--and we're going
4950 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4951 won't get another chance.*/
4952 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4953 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4956 token = cp_lexer_consume_token (parser->lexer);
4957 error_at (token->location, "decltype evaluates to %qT, "
4958 "which is not a class or enumeration type",
4960 parser->scope = error_mark_node;
4964 cp_lexer_consume_token (parser->lexer);
4967 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4969 /* If the next token is an identifier, and the one after
4970 that is a `::', then any valid interpretation would have
4971 found a class-or-namespace-name. */
4972 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4973 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4975 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4978 token = cp_lexer_consume_token (parser->lexer);
4981 if (!token->ambiguous_p)
4984 tree ambiguous_decls;
4986 decl = cp_parser_lookup_name (parser, token->u.value,
4988 /*is_template=*/false,
4989 /*is_namespace=*/false,
4990 /*check_dependency=*/true,
4993 if (TREE_CODE (decl) == TEMPLATE_DECL)
4994 error_at (token->location,
4995 "%qD used without template parameters",
4997 else if (ambiguous_decls)
4999 error_at (token->location,
5000 "reference to %qD is ambiguous",
5002 print_candidates (ambiguous_decls);
5003 decl = error_mark_node;
5007 if (cxx_dialect != cxx98)
5008 cp_parser_name_lookup_error
5009 (parser, token->u.value, decl, NLE_NOT_CXX98,
5012 cp_parser_name_lookup_error
5013 (parser, token->u.value, decl, NLE_CXX98,
5017 parser->scope = error_mark_node;
5019 /* Treat this as a successful nested-name-specifier
5024 If the name found is not a class-name (clause
5025 _class_) or namespace-name (_namespace.def_), the
5026 program is ill-formed. */
5029 cp_lexer_consume_token (parser->lexer);
5033 /* We've found one valid nested-name-specifier. */
5035 /* Name lookup always gives us a DECL. */
5036 if (TREE_CODE (new_scope) == TYPE_DECL)
5037 new_scope = TREE_TYPE (new_scope);
5038 /* Uses of "template" must be followed by actual templates. */
5039 if (template_keyword_p
5040 && !(CLASS_TYPE_P (new_scope)
5041 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5042 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5043 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5044 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5045 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5046 == TEMPLATE_ID_EXPR)))
5047 permerror (input_location, TYPE_P (new_scope)
5048 ? G_("%qT is not a template")
5049 : G_("%qD is not a template"),
5051 /* If it is a class scope, try to complete it; we are about to
5052 be looking up names inside the class. */
5053 if (TYPE_P (new_scope)
5054 /* Since checking types for dependency can be expensive,
5055 avoid doing it if the type is already complete. */
5056 && !COMPLETE_TYPE_P (new_scope)
5057 /* Do not try to complete dependent types. */
5058 && !dependent_type_p (new_scope))
5060 new_scope = complete_type (new_scope);
5061 /* If it is a typedef to current class, use the current
5062 class instead, as the typedef won't have any names inside
5064 if (!COMPLETE_TYPE_P (new_scope)
5065 && currently_open_class (new_scope))
5066 new_scope = TYPE_MAIN_VARIANT (new_scope);
5068 /* Make sure we look in the right scope the next time through
5070 parser->scope = new_scope;
5073 /* If parsing tentatively, replace the sequence of tokens that makes
5074 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5075 token. That way, should we re-parse the token stream, we will
5076 not have to repeat the effort required to do the parse, nor will
5077 we issue duplicate error messages. */
5078 if (success && start)
5082 token = cp_lexer_token_at (parser->lexer, start);
5083 /* Reset the contents of the START token. */
5084 token->type = CPP_NESTED_NAME_SPECIFIER;
5085 /* Retrieve any deferred checks. Do not pop this access checks yet
5086 so the memory will not be reclaimed during token replacing below. */
5087 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5088 token->u.tree_check_value->value = parser->scope;
5089 token->u.tree_check_value->checks = get_deferred_access_checks ();
5090 token->u.tree_check_value->qualifying_scope =
5091 parser->qualifying_scope;
5092 token->keyword = RID_MAX;
5094 /* Purge all subsequent tokens. */
5095 cp_lexer_purge_tokens_after (parser->lexer, start);
5099 pop_to_parent_deferring_access_checks ();
5101 return success ? parser->scope : NULL_TREE;
5104 /* Parse a nested-name-specifier. See
5105 cp_parser_nested_name_specifier_opt for details. This function
5106 behaves identically, except that it will an issue an error if no
5107 nested-name-specifier is present. */
5110 cp_parser_nested_name_specifier (cp_parser *parser,
5111 bool typename_keyword_p,
5112 bool check_dependency_p,
5114 bool is_declaration)
5118 /* Look for the nested-name-specifier. */
5119 scope = cp_parser_nested_name_specifier_opt (parser,
5124 /* If it was not present, issue an error message. */
5127 cp_parser_error (parser, "expected nested-name-specifier");
5128 parser->scope = NULL_TREE;
5134 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5135 this is either a class-name or a namespace-name (which corresponds
5136 to the class-or-namespace-name production in the grammar). For
5137 C++0x, it can also be a type-name that refers to an enumeration
5140 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5141 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5142 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5143 TYPE_P is TRUE iff the next name should be taken as a class-name,
5144 even the same name is declared to be another entity in the same
5147 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5148 specified by the class-or-namespace-name. If neither is found the
5149 ERROR_MARK_NODE is returned. */
5152 cp_parser_qualifying_entity (cp_parser *parser,
5153 bool typename_keyword_p,
5154 bool template_keyword_p,
5155 bool check_dependency_p,
5157 bool is_declaration)
5160 tree saved_qualifying_scope;
5161 tree saved_object_scope;
5164 bool successful_parse_p;
5166 /* DR 743: decltype can appear in a nested-name-specifier. */
5167 if (cp_lexer_next_token_is_decltype (parser->lexer))
5169 scope = cp_parser_decltype (parser);
5170 if (TREE_CODE (scope) != ENUMERAL_TYPE
5171 && !MAYBE_CLASS_TYPE_P (scope))
5173 cp_parser_simulate_error (parser);
5174 return error_mark_node;
5176 if (TYPE_NAME (scope))
5177 scope = TYPE_NAME (scope);
5181 /* Before we try to parse the class-name, we must save away the
5182 current PARSER->SCOPE since cp_parser_class_name will destroy
5184 saved_scope = parser->scope;
5185 saved_qualifying_scope = parser->qualifying_scope;
5186 saved_object_scope = parser->object_scope;
5187 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5188 there is no need to look for a namespace-name. */
5189 only_class_p = template_keyword_p
5190 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5192 cp_parser_parse_tentatively (parser);
5193 scope = cp_parser_class_name (parser,
5196 type_p ? class_type : none_type,
5198 /*class_head_p=*/false,
5200 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5201 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5203 && cxx_dialect != cxx98
5204 && !successful_parse_p)
5206 /* Restore the saved scope. */
5207 parser->scope = saved_scope;
5208 parser->qualifying_scope = saved_qualifying_scope;
5209 parser->object_scope = saved_object_scope;
5211 /* Parse tentatively. */
5212 cp_parser_parse_tentatively (parser);
5214 /* Parse a typedef-name or enum-name. */
5215 scope = cp_parser_nonclass_name (parser);
5217 /* "If the name found does not designate a namespace or a class,
5218 enumeration, or dependent type, the program is ill-formed."
5220 We cover classes and dependent types above and namespaces below,
5221 so this code is only looking for enums. */
5222 if (!scope || TREE_CODE (scope) != TYPE_DECL
5223 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5224 cp_parser_simulate_error (parser);
5226 successful_parse_p = cp_parser_parse_definitely (parser);
5228 /* If that didn't work, try for a namespace-name. */
5229 if (!only_class_p && !successful_parse_p)
5231 /* Restore the saved scope. */
5232 parser->scope = saved_scope;
5233 parser->qualifying_scope = saved_qualifying_scope;
5234 parser->object_scope = saved_object_scope;
5235 /* If we are not looking at an identifier followed by the scope
5236 resolution operator, then this is not part of a
5237 nested-name-specifier. (Note that this function is only used
5238 to parse the components of a nested-name-specifier.) */
5239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5240 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5241 return error_mark_node;
5242 scope = cp_parser_namespace_name (parser);
5248 /* Parse a postfix-expression.
5252 postfix-expression [ expression ]
5253 postfix-expression ( expression-list [opt] )
5254 simple-type-specifier ( expression-list [opt] )
5255 typename :: [opt] nested-name-specifier identifier
5256 ( expression-list [opt] )
5257 typename :: [opt] nested-name-specifier template [opt] template-id
5258 ( expression-list [opt] )
5259 postfix-expression . template [opt] id-expression
5260 postfix-expression -> template [opt] id-expression
5261 postfix-expression . pseudo-destructor-name
5262 postfix-expression -> pseudo-destructor-name
5263 postfix-expression ++
5264 postfix-expression --
5265 dynamic_cast < type-id > ( expression )
5266 static_cast < type-id > ( expression )
5267 reinterpret_cast < type-id > ( expression )
5268 const_cast < type-id > ( expression )
5269 typeid ( expression )
5275 ( type-id ) { initializer-list , [opt] }
5277 This extension is a GNU version of the C99 compound-literal
5278 construct. (The C99 grammar uses `type-name' instead of `type-id',
5279 but they are essentially the same concept.)
5281 If ADDRESS_P is true, the postfix expression is the operand of the
5282 `&' operator. CAST_P is true if this expression is the target of a
5285 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5286 class member access expressions [expr.ref].
5288 Returns a representation of the expression. */
5291 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5292 bool member_access_only_p,
5293 cp_id_kind * pidk_return)
5297 cp_id_kind idk = CP_ID_KIND_NONE;
5298 tree postfix_expression = NULL_TREE;
5299 bool is_member_access = false;
5301 /* Peek at the next token. */
5302 token = cp_lexer_peek_token (parser->lexer);
5303 /* Some of the productions are determined by keywords. */
5304 keyword = token->keyword;
5314 const char *saved_message;
5316 /* All of these can be handled in the same way from the point
5317 of view of parsing. Begin by consuming the token
5318 identifying the cast. */
5319 cp_lexer_consume_token (parser->lexer);
5321 /* New types cannot be defined in the cast. */
5322 saved_message = parser->type_definition_forbidden_message;
5323 parser->type_definition_forbidden_message
5324 = G_("types may not be defined in casts");
5326 /* Look for the opening `<'. */
5327 cp_parser_require (parser, CPP_LESS, RT_LESS);
5328 /* Parse the type to which we are casting. */
5329 type = cp_parser_type_id (parser);
5330 /* Look for the closing `>'. */
5331 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5332 /* Restore the old message. */
5333 parser->type_definition_forbidden_message = saved_message;
5335 /* And the expression which is being cast. */
5336 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5337 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5338 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5340 /* Only type conversions to integral or enumeration types
5341 can be used in constant-expressions. */
5342 if (!cast_valid_in_integral_constant_expression_p (type)
5343 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5344 return error_mark_node;
5350 = build_dynamic_cast (type, expression, tf_warning_or_error);
5354 = build_static_cast (type, expression, tf_warning_or_error);
5358 = build_reinterpret_cast (type, expression,
5359 tf_warning_or_error);
5363 = build_const_cast (type, expression, tf_warning_or_error);
5374 const char *saved_message;
5375 bool saved_in_type_id_in_expr_p;
5377 /* Consume the `typeid' token. */
5378 cp_lexer_consume_token (parser->lexer);
5379 /* Look for the `(' token. */
5380 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5381 /* Types cannot be defined in a `typeid' expression. */
5382 saved_message = parser->type_definition_forbidden_message;
5383 parser->type_definition_forbidden_message
5384 = G_("types may not be defined in a %<typeid%> expression");
5385 /* We can't be sure yet whether we're looking at a type-id or an
5387 cp_parser_parse_tentatively (parser);
5388 /* Try a type-id first. */
5389 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5390 parser->in_type_id_in_expr_p = true;
5391 type = cp_parser_type_id (parser);
5392 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5393 /* Look for the `)' token. Otherwise, we can't be sure that
5394 we're not looking at an expression: consider `typeid (int
5395 (3))', for example. */
5396 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5397 /* If all went well, simply lookup the type-id. */
5398 if (cp_parser_parse_definitely (parser))
5399 postfix_expression = get_typeid (type);
5400 /* Otherwise, fall back to the expression variant. */
5405 /* Look for an expression. */
5406 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5407 /* Compute its typeid. */
5408 postfix_expression = build_typeid (expression);
5409 /* Look for the `)' token. */
5410 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5412 /* Restore the saved message. */
5413 parser->type_definition_forbidden_message = saved_message;
5414 /* `typeid' may not appear in an integral constant expression. */
5415 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5416 return error_mark_node;
5423 /* The syntax permitted here is the same permitted for an
5424 elaborated-type-specifier. */
5425 type = cp_parser_elaborated_type_specifier (parser,
5426 /*is_friend=*/false,
5427 /*is_declaration=*/false);
5428 postfix_expression = cp_parser_functional_cast (parser, type);
5436 /* If the next thing is a simple-type-specifier, we may be
5437 looking at a functional cast. We could also be looking at
5438 an id-expression. So, we try the functional cast, and if
5439 that doesn't work we fall back to the primary-expression. */
5440 cp_parser_parse_tentatively (parser);
5441 /* Look for the simple-type-specifier. */
5442 type = cp_parser_simple_type_specifier (parser,
5443 /*decl_specs=*/NULL,
5444 CP_PARSER_FLAGS_NONE);
5445 /* Parse the cast itself. */
5446 if (!cp_parser_error_occurred (parser))
5448 = cp_parser_functional_cast (parser, type);
5449 /* If that worked, we're done. */
5450 if (cp_parser_parse_definitely (parser))
5453 /* If the functional-cast didn't work out, try a
5454 compound-literal. */
5455 if (cp_parser_allow_gnu_extensions_p (parser)
5456 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5458 VEC(constructor_elt,gc) *initializer_list = NULL;
5459 bool saved_in_type_id_in_expr_p;
5461 cp_parser_parse_tentatively (parser);
5462 /* Consume the `('. */
5463 cp_lexer_consume_token (parser->lexer);
5464 /* Parse the type. */
5465 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5466 parser->in_type_id_in_expr_p = true;
5467 type = cp_parser_type_id (parser);
5468 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5469 /* Look for the `)'. */
5470 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5471 /* Look for the `{'. */
5472 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5473 /* If things aren't going well, there's no need to
5475 if (!cp_parser_error_occurred (parser))
5477 bool non_constant_p;
5478 /* Parse the initializer-list. */
5480 = cp_parser_initializer_list (parser, &non_constant_p);
5481 /* Allow a trailing `,'. */
5482 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5483 cp_lexer_consume_token (parser->lexer);
5484 /* Look for the final `}'. */
5485 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5487 /* If that worked, we're definitely looking at a
5488 compound-literal expression. */
5489 if (cp_parser_parse_definitely (parser))
5491 /* Warn the user that a compound literal is not
5492 allowed in standard C++. */
5493 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5494 /* For simplicity, we disallow compound literals in
5495 constant-expressions. We could
5496 allow compound literals of integer type, whose
5497 initializer was a constant, in constant
5498 expressions. Permitting that usage, as a further
5499 extension, would not change the meaning of any
5500 currently accepted programs. (Of course, as
5501 compound literals are not part of ISO C++, the
5502 standard has nothing to say.) */
5503 if (cp_parser_non_integral_constant_expression (parser,
5506 postfix_expression = error_mark_node;
5509 /* Form the representation of the compound-literal. */
5511 = (finish_compound_literal
5512 (type, build_constructor (init_list_type_node,
5514 tf_warning_or_error));
5519 /* It must be a primary-expression. */
5521 = cp_parser_primary_expression (parser, address_p, cast_p,
5522 /*template_arg_p=*/false,
5528 /* Keep looping until the postfix-expression is complete. */
5531 if (idk == CP_ID_KIND_UNQUALIFIED
5532 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5533 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5534 /* It is not a Koenig lookup function call. */
5536 = unqualified_name_lookup_error (postfix_expression);
5538 /* Peek at the next token. */
5539 token = cp_lexer_peek_token (parser->lexer);
5541 switch (token->type)
5543 case CPP_OPEN_SQUARE:
5545 = cp_parser_postfix_open_square_expression (parser,
5548 idk = CP_ID_KIND_NONE;
5549 is_member_access = false;
5552 case CPP_OPEN_PAREN:
5553 /* postfix-expression ( expression-list [opt] ) */
5556 bool is_builtin_constant_p;
5557 bool saved_integral_constant_expression_p = false;
5558 bool saved_non_integral_constant_expression_p = false;
5561 is_member_access = false;
5563 is_builtin_constant_p
5564 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5565 if (is_builtin_constant_p)
5567 /* The whole point of __builtin_constant_p is to allow
5568 non-constant expressions to appear as arguments. */
5569 saved_integral_constant_expression_p
5570 = parser->integral_constant_expression_p;
5571 saved_non_integral_constant_expression_p
5572 = parser->non_integral_constant_expression_p;
5573 parser->integral_constant_expression_p = false;
5575 args = (cp_parser_parenthesized_expression_list
5577 /*cast_p=*/false, /*allow_expansion_p=*/true,
5578 /*non_constant_p=*/NULL));
5579 if (is_builtin_constant_p)
5581 parser->integral_constant_expression_p
5582 = saved_integral_constant_expression_p;
5583 parser->non_integral_constant_expression_p
5584 = saved_non_integral_constant_expression_p;
5589 postfix_expression = error_mark_node;
5593 /* Function calls are not permitted in
5594 constant-expressions. */
5595 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5596 && cp_parser_non_integral_constant_expression (parser,
5599 postfix_expression = error_mark_node;
5600 release_tree_vector (args);
5605 if (idk == CP_ID_KIND_UNQUALIFIED
5606 || idk == CP_ID_KIND_TEMPLATE_ID)
5608 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5610 if (!VEC_empty (tree, args))
5613 if (!any_type_dependent_arguments_p (args))
5615 = perform_koenig_lookup (postfix_expression, args,
5616 /*include_std=*/false,
5617 tf_warning_or_error);
5621 = unqualified_fn_lookup_error (postfix_expression);
5623 /* We do not perform argument-dependent lookup if
5624 normal lookup finds a non-function, in accordance
5625 with the expected resolution of DR 218. */
5626 else if (!VEC_empty (tree, args)
5627 && is_overloaded_fn (postfix_expression))
5629 tree fn = get_first_fn (postfix_expression);
5630 fn = STRIP_TEMPLATE (fn);
5632 /* Do not do argument dependent lookup if regular
5633 lookup finds a member function or a block-scope
5634 function declaration. [basic.lookup.argdep]/3 */
5635 if (!DECL_FUNCTION_MEMBER_P (fn)
5636 && !DECL_LOCAL_FUNCTION_P (fn))
5639 if (!any_type_dependent_arguments_p (args))
5641 = perform_koenig_lookup (postfix_expression, args,
5642 /*include_std=*/false,
5643 tf_warning_or_error);
5648 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5650 tree instance = TREE_OPERAND (postfix_expression, 0);
5651 tree fn = TREE_OPERAND (postfix_expression, 1);
5653 if (processing_template_decl
5654 && (type_dependent_expression_p (instance)
5655 || (!BASELINK_P (fn)
5656 && TREE_CODE (fn) != FIELD_DECL)
5657 || type_dependent_expression_p (fn)
5658 || any_type_dependent_arguments_p (args)))
5661 = build_nt_call_vec (postfix_expression, args);
5662 release_tree_vector (args);
5666 if (BASELINK_P (fn))
5669 = (build_new_method_call
5670 (instance, fn, &args, NULL_TREE,
5671 (idk == CP_ID_KIND_QUALIFIED
5672 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5675 tf_warning_or_error));
5679 = finish_call_expr (postfix_expression, &args,
5680 /*disallow_virtual=*/false,
5682 tf_warning_or_error);
5684 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5685 || TREE_CODE (postfix_expression) == MEMBER_REF
5686 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5687 postfix_expression = (build_offset_ref_call_from_tree
5688 (postfix_expression, &args));
5689 else if (idk == CP_ID_KIND_QUALIFIED)
5690 /* A call to a static class member, or a namespace-scope
5693 = finish_call_expr (postfix_expression, &args,
5694 /*disallow_virtual=*/true,
5696 tf_warning_or_error);
5698 /* All other function calls. */
5700 = finish_call_expr (postfix_expression, &args,
5701 /*disallow_virtual=*/false,
5703 tf_warning_or_error);
5705 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5706 idk = CP_ID_KIND_NONE;
5708 release_tree_vector (args);
5714 /* postfix-expression . template [opt] id-expression
5715 postfix-expression . pseudo-destructor-name
5716 postfix-expression -> template [opt] id-expression
5717 postfix-expression -> pseudo-destructor-name */
5719 /* Consume the `.' or `->' operator. */
5720 cp_lexer_consume_token (parser->lexer);
5723 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5728 is_member_access = true;
5732 /* postfix-expression ++ */
5733 /* Consume the `++' token. */
5734 cp_lexer_consume_token (parser->lexer);
5735 /* Generate a representation for the complete expression. */
5737 = finish_increment_expr (postfix_expression,
5738 POSTINCREMENT_EXPR);
5739 /* Increments may not appear in constant-expressions. */
5740 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5741 postfix_expression = error_mark_node;
5742 idk = CP_ID_KIND_NONE;
5743 is_member_access = false;
5746 case CPP_MINUS_MINUS:
5747 /* postfix-expression -- */
5748 /* Consume the `--' token. */
5749 cp_lexer_consume_token (parser->lexer);
5750 /* Generate a representation for the complete expression. */
5752 = finish_increment_expr (postfix_expression,
5753 POSTDECREMENT_EXPR);
5754 /* Decrements may not appear in constant-expressions. */
5755 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5756 postfix_expression = error_mark_node;
5757 idk = CP_ID_KIND_NONE;
5758 is_member_access = false;
5762 if (pidk_return != NULL)
5763 * pidk_return = idk;
5764 if (member_access_only_p)
5765 return is_member_access? postfix_expression : error_mark_node;
5767 return postfix_expression;
5771 /* We should never get here. */
5773 return error_mark_node;
5776 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5777 by cp_parser_builtin_offsetof. We're looking for
5779 postfix-expression [ expression ]
5781 FOR_OFFSETOF is set if we're being called in that context, which
5782 changes how we deal with integer constant expressions. */
5785 cp_parser_postfix_open_square_expression (cp_parser *parser,
5786 tree postfix_expression,
5791 /* Consume the `[' token. */
5792 cp_lexer_consume_token (parser->lexer);
5794 /* Parse the index expression. */
5795 /* ??? For offsetof, there is a question of what to allow here. If
5796 offsetof is not being used in an integral constant expression context,
5797 then we *could* get the right answer by computing the value at runtime.
5798 If we are in an integral constant expression context, then we might
5799 could accept any constant expression; hard to say without analysis.
5800 Rather than open the barn door too wide right away, allow only integer
5801 constant expressions here. */
5803 index = cp_parser_constant_expression (parser, false, NULL);
5805 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5807 /* Look for the closing `]'. */
5808 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5810 /* Build the ARRAY_REF. */
5811 postfix_expression = grok_array_decl (postfix_expression, index);
5813 /* When not doing offsetof, array references are not permitted in
5814 constant-expressions. */
5816 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5817 postfix_expression = error_mark_node;
5819 return postfix_expression;
5822 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5823 by cp_parser_builtin_offsetof. We're looking for
5825 postfix-expression . template [opt] id-expression
5826 postfix-expression . pseudo-destructor-name
5827 postfix-expression -> template [opt] id-expression
5828 postfix-expression -> pseudo-destructor-name
5830 FOR_OFFSETOF is set if we're being called in that context. That sorta
5831 limits what of the above we'll actually accept, but nevermind.
5832 TOKEN_TYPE is the "." or "->" token, which will already have been
5833 removed from the stream. */
5836 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5837 enum cpp_ttype token_type,
5838 tree postfix_expression,
5839 bool for_offsetof, cp_id_kind *idk,
5840 location_t location)
5844 bool pseudo_destructor_p;
5845 tree scope = NULL_TREE;
5847 /* If this is a `->' operator, dereference the pointer. */
5848 if (token_type == CPP_DEREF)
5849 postfix_expression = build_x_arrow (postfix_expression);
5850 /* Check to see whether or not the expression is type-dependent. */
5851 dependent_p = type_dependent_expression_p (postfix_expression);
5852 /* The identifier following the `->' or `.' is not qualified. */
5853 parser->scope = NULL_TREE;
5854 parser->qualifying_scope = NULL_TREE;
5855 parser->object_scope = NULL_TREE;
5856 *idk = CP_ID_KIND_NONE;
5858 /* Enter the scope corresponding to the type of the object
5859 given by the POSTFIX_EXPRESSION. */
5860 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5862 scope = TREE_TYPE (postfix_expression);
5863 /* According to the standard, no expression should ever have
5864 reference type. Unfortunately, we do not currently match
5865 the standard in this respect in that our internal representation
5866 of an expression may have reference type even when the standard
5867 says it does not. Therefore, we have to manually obtain the
5868 underlying type here. */
5869 scope = non_reference (scope);
5870 /* The type of the POSTFIX_EXPRESSION must be complete. */
5871 if (scope == unknown_type_node)
5873 error_at (location, "%qE does not have class type",
5874 postfix_expression);
5877 /* Unlike the object expression in other contexts, *this is not
5878 required to be of complete type for purposes of class member
5879 access (5.2.5) outside the member function body. */
5880 else if (scope != current_class_ref
5881 && !(processing_template_decl && scope == current_class_type))
5882 scope = complete_type_or_else (scope, NULL_TREE);
5883 /* Let the name lookup machinery know that we are processing a
5884 class member access expression. */
5885 parser->context->object_type = scope;
5886 /* If something went wrong, we want to be able to discern that case,
5887 as opposed to the case where there was no SCOPE due to the type
5888 of expression being dependent. */
5890 scope = error_mark_node;
5891 /* If the SCOPE was erroneous, make the various semantic analysis
5892 functions exit quickly -- and without issuing additional error
5894 if (scope == error_mark_node)
5895 postfix_expression = error_mark_node;
5898 /* Assume this expression is not a pseudo-destructor access. */
5899 pseudo_destructor_p = false;
5901 /* If the SCOPE is a scalar type, then, if this is a valid program,
5902 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5903 is type dependent, it can be pseudo-destructor-name or something else.
5904 Try to parse it as pseudo-destructor-name first. */
5905 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5910 cp_parser_parse_tentatively (parser);
5911 /* Parse the pseudo-destructor-name. */
5913 cp_parser_pseudo_destructor_name (parser, &s, &type);
5915 && (cp_parser_error_occurred (parser)
5916 || TREE_CODE (type) != TYPE_DECL
5917 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5918 cp_parser_abort_tentative_parse (parser);
5919 else if (cp_parser_parse_definitely (parser))
5921 pseudo_destructor_p = true;
5923 = finish_pseudo_destructor_expr (postfix_expression,
5924 s, TREE_TYPE (type));
5928 if (!pseudo_destructor_p)
5930 /* If the SCOPE is not a scalar type, we are looking at an
5931 ordinary class member access expression, rather than a
5932 pseudo-destructor-name. */
5934 cp_token *token = cp_lexer_peek_token (parser->lexer);
5935 /* Parse the id-expression. */
5936 name = (cp_parser_id_expression
5938 cp_parser_optional_template_keyword (parser),
5939 /*check_dependency_p=*/true,
5941 /*declarator_p=*/false,
5942 /*optional_p=*/false));
5943 /* In general, build a SCOPE_REF if the member name is qualified.
5944 However, if the name was not dependent and has already been
5945 resolved; there is no need to build the SCOPE_REF. For example;
5947 struct X { void f(); };
5948 template <typename T> void f(T* t) { t->X::f(); }
5950 Even though "t" is dependent, "X::f" is not and has been resolved
5951 to a BASELINK; there is no need to include scope information. */
5953 /* But we do need to remember that there was an explicit scope for
5954 virtual function calls. */
5956 *idk = CP_ID_KIND_QUALIFIED;
5958 /* If the name is a template-id that names a type, we will get a
5959 TYPE_DECL here. That is invalid code. */
5960 if (TREE_CODE (name) == TYPE_DECL)
5962 error_at (token->location, "invalid use of %qD", name);
5963 postfix_expression = error_mark_node;
5967 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5969 name = build_qualified_name (/*type=*/NULL_TREE,
5973 parser->scope = NULL_TREE;
5974 parser->qualifying_scope = NULL_TREE;
5975 parser->object_scope = NULL_TREE;
5977 if (scope && name && BASELINK_P (name))
5978 adjust_result_of_qualified_name_lookup
5979 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5981 = finish_class_member_access_expr (postfix_expression, name,
5983 tf_warning_or_error);
5987 /* We no longer need to look up names in the scope of the object on
5988 the left-hand side of the `.' or `->' operator. */
5989 parser->context->object_type = NULL_TREE;
5991 /* Outside of offsetof, these operators may not appear in
5992 constant-expressions. */
5994 && (cp_parser_non_integral_constant_expression
5995 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5996 postfix_expression = error_mark_node;
5998 return postfix_expression;
6001 /* Parse a parenthesized expression-list.
6004 assignment-expression
6005 expression-list, assignment-expression
6010 identifier, expression-list
6012 CAST_P is true if this expression is the target of a cast.
6014 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6017 Returns a vector of trees. Each element is a representation of an
6018 assignment-expression. NULL is returned if the ( and or ) are
6019 missing. An empty, but allocated, vector is returned on no
6020 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6021 if we are parsing an attribute list for an attribute that wants a
6022 plain identifier argument, normal_attr for an attribute that wants
6023 an expression, or non_attr if we aren't parsing an attribute list. If
6024 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6025 not all of the expressions in the list were constant. */
6027 static VEC(tree,gc) *
6028 cp_parser_parenthesized_expression_list (cp_parser* parser,
6029 int is_attribute_list,
6031 bool allow_expansion_p,
6032 bool *non_constant_p)
6034 VEC(tree,gc) *expression_list;
6035 bool fold_expr_p = is_attribute_list != non_attr;
6036 tree identifier = NULL_TREE;
6037 bool saved_greater_than_is_operator_p;
6039 /* Assume all the expressions will be constant. */
6041 *non_constant_p = false;
6043 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6046 expression_list = make_tree_vector ();
6048 /* Within a parenthesized expression, a `>' token is always
6049 the greater-than operator. */
6050 saved_greater_than_is_operator_p
6051 = parser->greater_than_is_operator_p;
6052 parser->greater_than_is_operator_p = true;
6054 /* Consume expressions until there are no more. */
6055 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6060 /* At the beginning of attribute lists, check to see if the
6061 next token is an identifier. */
6062 if (is_attribute_list == id_attr
6063 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6067 /* Consume the identifier. */
6068 token = cp_lexer_consume_token (parser->lexer);
6069 /* Save the identifier. */
6070 identifier = token->u.value;
6074 bool expr_non_constant_p;
6076 /* Parse the next assignment-expression. */
6077 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6079 /* A braced-init-list. */
6080 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6081 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6082 if (non_constant_p && expr_non_constant_p)
6083 *non_constant_p = true;
6085 else if (non_constant_p)
6087 expr = (cp_parser_constant_expression
6088 (parser, /*allow_non_constant_p=*/true,
6089 &expr_non_constant_p));
6090 if (expr_non_constant_p)
6091 *non_constant_p = true;
6094 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6097 expr = fold_non_dependent_expr (expr);
6099 /* If we have an ellipsis, then this is an expression
6101 if (allow_expansion_p
6102 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6104 /* Consume the `...'. */
6105 cp_lexer_consume_token (parser->lexer);
6107 /* Build the argument pack. */
6108 expr = make_pack_expansion (expr);
6111 /* Add it to the list. We add error_mark_node
6112 expressions to the list, so that we can still tell if
6113 the correct form for a parenthesized expression-list
6114 is found. That gives better errors. */
6115 VEC_safe_push (tree, gc, expression_list, expr);
6117 if (expr == error_mark_node)
6121 /* After the first item, attribute lists look the same as
6122 expression lists. */
6123 is_attribute_list = non_attr;
6126 /* If the next token isn't a `,', then we are done. */
6127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6130 /* Otherwise, consume the `,' and keep going. */
6131 cp_lexer_consume_token (parser->lexer);
6134 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6139 /* We try and resync to an unnested comma, as that will give the
6140 user better diagnostics. */
6141 ending = cp_parser_skip_to_closing_parenthesis (parser,
6142 /*recovering=*/true,
6144 /*consume_paren=*/true);
6149 parser->greater_than_is_operator_p
6150 = saved_greater_than_is_operator_p;
6155 parser->greater_than_is_operator_p
6156 = saved_greater_than_is_operator_p;
6159 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6161 return expression_list;
6164 /* Parse a pseudo-destructor-name.
6166 pseudo-destructor-name:
6167 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6168 :: [opt] nested-name-specifier template template-id :: ~ type-name
6169 :: [opt] nested-name-specifier [opt] ~ type-name
6171 If either of the first two productions is used, sets *SCOPE to the
6172 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6173 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6174 or ERROR_MARK_NODE if the parse fails. */
6177 cp_parser_pseudo_destructor_name (cp_parser* parser,
6181 bool nested_name_specifier_p;
6183 /* Assume that things will not work out. */
6184 *type = error_mark_node;
6186 /* Look for the optional `::' operator. */
6187 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6188 /* Look for the optional nested-name-specifier. */
6189 nested_name_specifier_p
6190 = (cp_parser_nested_name_specifier_opt (parser,
6191 /*typename_keyword_p=*/false,
6192 /*check_dependency_p=*/true,
6194 /*is_declaration=*/false)
6196 /* Now, if we saw a nested-name-specifier, we might be doing the
6197 second production. */
6198 if (nested_name_specifier_p
6199 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6201 /* Consume the `template' keyword. */
6202 cp_lexer_consume_token (parser->lexer);
6203 /* Parse the template-id. */
6204 cp_parser_template_id (parser,
6205 /*template_keyword_p=*/true,
6206 /*check_dependency_p=*/false,
6207 /*is_declaration=*/true);
6208 /* Look for the `::' token. */
6209 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6211 /* If the next token is not a `~', then there might be some
6212 additional qualification. */
6213 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6215 /* At this point, we're looking for "type-name :: ~". The type-name
6216 must not be a class-name, since this is a pseudo-destructor. So,
6217 it must be either an enum-name, or a typedef-name -- both of which
6218 are just identifiers. So, we peek ahead to check that the "::"
6219 and "~" tokens are present; if they are not, then we can avoid
6220 calling type_name. */
6221 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6222 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6223 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6225 cp_parser_error (parser, "non-scalar type");
6229 /* Look for the type-name. */
6230 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6231 if (*scope == error_mark_node)
6234 /* Look for the `::' token. */
6235 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6240 /* Look for the `~'. */
6241 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6243 /* Once we see the ~, this has to be a pseudo-destructor. */
6244 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6245 cp_parser_commit_to_tentative_parse (parser);
6247 /* Look for the type-name again. We are not responsible for
6248 checking that it matches the first type-name. */
6249 *type = cp_parser_nonclass_name (parser);
6252 /* Parse a unary-expression.
6258 unary-operator cast-expression
6259 sizeof unary-expression
6261 alignof ( type-id ) [C++0x]
6268 __extension__ cast-expression
6269 __alignof__ unary-expression
6270 __alignof__ ( type-id )
6271 alignof unary-expression [C++0x]
6272 __real__ cast-expression
6273 __imag__ cast-expression
6276 ADDRESS_P is true iff the unary-expression is appearing as the
6277 operand of the `&' operator. CAST_P is true if this expression is
6278 the target of a cast.
6280 Returns a representation of the expression. */
6283 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6287 enum tree_code unary_operator;
6289 /* Peek at the next token. */
6290 token = cp_lexer_peek_token (parser->lexer);
6291 /* Some keywords give away the kind of expression. */
6292 if (token->type == CPP_KEYWORD)
6294 enum rid keyword = token->keyword;
6304 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6305 /* Consume the token. */
6306 cp_lexer_consume_token (parser->lexer);
6307 /* Parse the operand. */
6308 operand = cp_parser_sizeof_operand (parser, keyword);
6310 if (TYPE_P (operand))
6311 return cxx_sizeof_or_alignof_type (operand, op, true);
6314 /* ISO C++ defines alignof only with types, not with
6315 expressions. So pedwarn if alignof is used with a non-
6316 type expression. However, __alignof__ is ok. */
6317 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6318 pedwarn (token->location, OPT_pedantic,
6319 "ISO C++ does not allow %<alignof%> "
6322 return cxx_sizeof_or_alignof_expr (operand, op, true);
6327 return cp_parser_new_expression (parser);
6330 return cp_parser_delete_expression (parser);
6334 /* The saved value of the PEDANTIC flag. */
6338 /* Save away the PEDANTIC flag. */
6339 cp_parser_extension_opt (parser, &saved_pedantic);
6340 /* Parse the cast-expression. */
6341 expr = cp_parser_simple_cast_expression (parser);
6342 /* Restore the PEDANTIC flag. */
6343 pedantic = saved_pedantic;
6353 /* Consume the `__real__' or `__imag__' token. */
6354 cp_lexer_consume_token (parser->lexer);
6355 /* Parse the cast-expression. */
6356 expression = cp_parser_simple_cast_expression (parser);
6357 /* Create the complete representation. */
6358 return build_x_unary_op ((keyword == RID_REALPART
6359 ? REALPART_EXPR : IMAGPART_EXPR),
6361 tf_warning_or_error);
6368 const char *saved_message;
6369 bool saved_integral_constant_expression_p;
6370 bool saved_non_integral_constant_expression_p;
6371 bool saved_greater_than_is_operator_p;
6373 cp_lexer_consume_token (parser->lexer);
6374 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6376 saved_message = parser->type_definition_forbidden_message;
6377 parser->type_definition_forbidden_message
6378 = G_("types may not be defined in %<noexcept%> expressions");
6380 saved_integral_constant_expression_p
6381 = parser->integral_constant_expression_p;
6382 saved_non_integral_constant_expression_p
6383 = parser->non_integral_constant_expression_p;
6384 parser->integral_constant_expression_p = false;
6386 saved_greater_than_is_operator_p
6387 = parser->greater_than_is_operator_p;
6388 parser->greater_than_is_operator_p = true;
6390 ++cp_unevaluated_operand;
6391 ++c_inhibit_evaluation_warnings;
6392 expr = cp_parser_expression (parser, false, NULL);
6393 --c_inhibit_evaluation_warnings;
6394 --cp_unevaluated_operand;
6396 parser->greater_than_is_operator_p
6397 = saved_greater_than_is_operator_p;
6399 parser->integral_constant_expression_p
6400 = saved_integral_constant_expression_p;
6401 parser->non_integral_constant_expression_p
6402 = saved_non_integral_constant_expression_p;
6404 parser->type_definition_forbidden_message = saved_message;
6406 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6407 return finish_noexcept_expr (expr, tf_warning_or_error);
6415 /* Look for the `:: new' and `:: delete', which also signal the
6416 beginning of a new-expression, or delete-expression,
6417 respectively. If the next token is `::', then it might be one of
6419 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6423 /* See if the token after the `::' is one of the keywords in
6424 which we're interested. */
6425 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6426 /* If it's `new', we have a new-expression. */
6427 if (keyword == RID_NEW)
6428 return cp_parser_new_expression (parser);
6429 /* Similarly, for `delete'. */
6430 else if (keyword == RID_DELETE)
6431 return cp_parser_delete_expression (parser);
6434 /* Look for a unary operator. */
6435 unary_operator = cp_parser_unary_operator (token);
6436 /* The `++' and `--' operators can be handled similarly, even though
6437 they are not technically unary-operators in the grammar. */
6438 if (unary_operator == ERROR_MARK)
6440 if (token->type == CPP_PLUS_PLUS)
6441 unary_operator = PREINCREMENT_EXPR;
6442 else if (token->type == CPP_MINUS_MINUS)
6443 unary_operator = PREDECREMENT_EXPR;
6444 /* Handle the GNU address-of-label extension. */
6445 else if (cp_parser_allow_gnu_extensions_p (parser)
6446 && token->type == CPP_AND_AND)
6450 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6452 /* Consume the '&&' token. */
6453 cp_lexer_consume_token (parser->lexer);
6454 /* Look for the identifier. */
6455 identifier = cp_parser_identifier (parser);
6456 /* Create an expression representing the address. */
6457 expression = finish_label_address_expr (identifier, loc);
6458 if (cp_parser_non_integral_constant_expression (parser,
6460 expression = error_mark_node;
6464 if (unary_operator != ERROR_MARK)
6466 tree cast_expression;
6467 tree expression = error_mark_node;
6468 non_integral_constant non_constant_p = NIC_NONE;
6470 /* Consume the operator token. */
6471 token = cp_lexer_consume_token (parser->lexer);
6472 /* Parse the cast-expression. */
6474 = cp_parser_cast_expression (parser,
6475 unary_operator == ADDR_EXPR,
6476 /*cast_p=*/false, pidk);
6477 /* Now, build an appropriate representation. */
6478 switch (unary_operator)
6481 non_constant_p = NIC_STAR;
6482 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6483 tf_warning_or_error);
6487 non_constant_p = NIC_ADDR;
6490 expression = build_x_unary_op (unary_operator, cast_expression,
6491 tf_warning_or_error);
6494 case PREINCREMENT_EXPR:
6495 case PREDECREMENT_EXPR:
6496 non_constant_p = unary_operator == PREINCREMENT_EXPR
6497 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6499 case UNARY_PLUS_EXPR:
6501 case TRUTH_NOT_EXPR:
6502 expression = finish_unary_op_expr (unary_operator, cast_expression);
6509 if (non_constant_p != NIC_NONE
6510 && cp_parser_non_integral_constant_expression (parser,
6512 expression = error_mark_node;
6517 return cp_parser_postfix_expression (parser, address_p, cast_p,
6518 /*member_access_only_p=*/false,
6522 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6523 unary-operator, the corresponding tree code is returned. */
6525 static enum tree_code
6526 cp_parser_unary_operator (cp_token* token)
6528 switch (token->type)
6531 return INDIRECT_REF;
6537 return UNARY_PLUS_EXPR;
6543 return TRUTH_NOT_EXPR;
6546 return BIT_NOT_EXPR;
6553 /* Parse a new-expression.
6556 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6557 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6559 Returns a representation of the expression. */
6562 cp_parser_new_expression (cp_parser* parser)
6564 bool global_scope_p;
6565 VEC(tree,gc) *placement;
6567 VEC(tree,gc) *initializer;
6571 /* Look for the optional `::' operator. */
6573 = (cp_parser_global_scope_opt (parser,
6574 /*current_scope_valid_p=*/false)
6576 /* Look for the `new' operator. */
6577 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6578 /* There's no easy way to tell a new-placement from the
6579 `( type-id )' construct. */
6580 cp_parser_parse_tentatively (parser);
6581 /* Look for a new-placement. */
6582 placement = cp_parser_new_placement (parser);
6583 /* If that didn't work out, there's no new-placement. */
6584 if (!cp_parser_parse_definitely (parser))
6586 if (placement != NULL)
6587 release_tree_vector (placement);
6591 /* If the next token is a `(', then we have a parenthesized
6593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6596 /* Consume the `('. */
6597 cp_lexer_consume_token (parser->lexer);
6598 /* Parse the type-id. */
6599 type = cp_parser_type_id (parser);
6600 /* Look for the closing `)'. */
6601 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6602 token = cp_lexer_peek_token (parser->lexer);
6603 /* There should not be a direct-new-declarator in this production,
6604 but GCC used to allowed this, so we check and emit a sensible error
6605 message for this case. */
6606 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6608 error_at (token->location,
6609 "array bound forbidden after parenthesized type-id");
6610 inform (token->location,
6611 "try removing the parentheses around the type-id");
6612 cp_parser_direct_new_declarator (parser);
6616 /* Otherwise, there must be a new-type-id. */
6618 type = cp_parser_new_type_id (parser, &nelts);
6620 /* If the next token is a `(' or '{', then we have a new-initializer. */
6621 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6622 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6623 initializer = cp_parser_new_initializer (parser);
6627 /* A new-expression may not appear in an integral constant
6629 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6630 ret = error_mark_node;
6633 /* Create a representation of the new-expression. */
6634 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6635 tf_warning_or_error);
6638 if (placement != NULL)
6639 release_tree_vector (placement);
6640 if (initializer != NULL)
6641 release_tree_vector (initializer);
6646 /* Parse a new-placement.
6651 Returns the same representation as for an expression-list. */
6653 static VEC(tree,gc) *
6654 cp_parser_new_placement (cp_parser* parser)
6656 VEC(tree,gc) *expression_list;
6658 /* Parse the expression-list. */
6659 expression_list = (cp_parser_parenthesized_expression_list
6660 (parser, non_attr, /*cast_p=*/false,
6661 /*allow_expansion_p=*/true,
6662 /*non_constant_p=*/NULL));
6664 return expression_list;
6667 /* Parse a new-type-id.
6670 type-specifier-seq new-declarator [opt]
6672 Returns the TYPE allocated. If the new-type-id indicates an array
6673 type, *NELTS is set to the number of elements in the last array
6674 bound; the TYPE will not include the last array bound. */
6677 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6679 cp_decl_specifier_seq type_specifier_seq;
6680 cp_declarator *new_declarator;
6681 cp_declarator *declarator;
6682 cp_declarator *outer_declarator;
6683 const char *saved_message;
6686 /* The type-specifier sequence must not contain type definitions.
6687 (It cannot contain declarations of new types either, but if they
6688 are not definitions we will catch that because they are not
6690 saved_message = parser->type_definition_forbidden_message;
6691 parser->type_definition_forbidden_message
6692 = G_("types may not be defined in a new-type-id");
6693 /* Parse the type-specifier-seq. */
6694 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6695 /*is_trailing_return=*/false,
6696 &type_specifier_seq);
6697 /* Restore the old message. */
6698 parser->type_definition_forbidden_message = saved_message;
6699 /* Parse the new-declarator. */
6700 new_declarator = cp_parser_new_declarator_opt (parser);
6702 /* Determine the number of elements in the last array dimension, if
6705 /* Skip down to the last array dimension. */
6706 declarator = new_declarator;
6707 outer_declarator = NULL;
6708 while (declarator && (declarator->kind == cdk_pointer
6709 || declarator->kind == cdk_ptrmem))
6711 outer_declarator = declarator;
6712 declarator = declarator->declarator;
6715 && declarator->kind == cdk_array
6716 && declarator->declarator
6717 && declarator->declarator->kind == cdk_array)
6719 outer_declarator = declarator;
6720 declarator = declarator->declarator;
6723 if (declarator && declarator->kind == cdk_array)
6725 *nelts = declarator->u.array.bounds;
6726 if (*nelts == error_mark_node)
6727 *nelts = integer_one_node;
6729 if (outer_declarator)
6730 outer_declarator->declarator = declarator->declarator;
6732 new_declarator = NULL;
6735 type = groktypename (&type_specifier_seq, new_declarator, false);
6739 /* Parse an (optional) new-declarator.
6742 ptr-operator new-declarator [opt]
6743 direct-new-declarator
6745 Returns the declarator. */
6747 static cp_declarator *
6748 cp_parser_new_declarator_opt (cp_parser* parser)
6750 enum tree_code code;
6752 cp_cv_quals cv_quals;
6754 /* We don't know if there's a ptr-operator next, or not. */
6755 cp_parser_parse_tentatively (parser);
6756 /* Look for a ptr-operator. */
6757 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6758 /* If that worked, look for more new-declarators. */
6759 if (cp_parser_parse_definitely (parser))
6761 cp_declarator *declarator;
6763 /* Parse another optional declarator. */
6764 declarator = cp_parser_new_declarator_opt (parser);
6766 return cp_parser_make_indirect_declarator
6767 (code, type, cv_quals, declarator);
6770 /* If the next token is a `[', there is a direct-new-declarator. */
6771 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6772 return cp_parser_direct_new_declarator (parser);
6777 /* Parse a direct-new-declarator.
6779 direct-new-declarator:
6781 direct-new-declarator [constant-expression]
6785 static cp_declarator *
6786 cp_parser_direct_new_declarator (cp_parser* parser)
6788 cp_declarator *declarator = NULL;
6794 /* Look for the opening `['. */
6795 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6796 /* The first expression is not required to be constant. */
6799 cp_token *token = cp_lexer_peek_token (parser->lexer);
6800 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6801 /* The standard requires that the expression have integral
6802 type. DR 74 adds enumeration types. We believe that the
6803 real intent is that these expressions be handled like the
6804 expression in a `switch' condition, which also allows
6805 classes with a single conversion to integral or
6806 enumeration type. */
6807 if (!processing_template_decl)
6810 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6815 error_at (token->location,
6816 "expression in new-declarator must have integral "
6817 "or enumeration type");
6818 expression = error_mark_node;
6822 /* But all the other expressions must be. */
6825 = cp_parser_constant_expression (parser,
6826 /*allow_non_constant=*/false,
6828 /* Look for the closing `]'. */
6829 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6831 /* Add this bound to the declarator. */
6832 declarator = make_array_declarator (declarator, expression);
6834 /* If the next token is not a `[', then there are no more
6836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6843 /* Parse a new-initializer.
6846 ( expression-list [opt] )
6849 Returns a representation of the expression-list. */
6851 static VEC(tree,gc) *
6852 cp_parser_new_initializer (cp_parser* parser)
6854 VEC(tree,gc) *expression_list;
6856 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6859 bool expr_non_constant_p;
6860 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6861 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6862 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6863 expression_list = make_tree_vector_single (t);
6866 expression_list = (cp_parser_parenthesized_expression_list
6867 (parser, non_attr, /*cast_p=*/false,
6868 /*allow_expansion_p=*/true,
6869 /*non_constant_p=*/NULL));
6871 return expression_list;
6874 /* Parse a delete-expression.
6877 :: [opt] delete cast-expression
6878 :: [opt] delete [ ] cast-expression
6880 Returns a representation of the expression. */
6883 cp_parser_delete_expression (cp_parser* parser)
6885 bool global_scope_p;
6889 /* Look for the optional `::' operator. */
6891 = (cp_parser_global_scope_opt (parser,
6892 /*current_scope_valid_p=*/false)
6894 /* Look for the `delete' keyword. */
6895 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6896 /* See if the array syntax is in use. */
6897 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6899 /* Consume the `[' token. */
6900 cp_lexer_consume_token (parser->lexer);
6901 /* Look for the `]' token. */
6902 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6903 /* Remember that this is the `[]' construct. */
6909 /* Parse the cast-expression. */
6910 expression = cp_parser_simple_cast_expression (parser);
6912 /* A delete-expression may not appear in an integral constant
6914 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6915 return error_mark_node;
6917 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6918 tf_warning_or_error);
6921 /* Returns true if TOKEN may start a cast-expression and false
6925 cp_parser_token_starts_cast_expression (cp_token *token)
6927 switch (token->type)
6933 case CPP_CLOSE_SQUARE:
6934 case CPP_CLOSE_PAREN:
6935 case CPP_CLOSE_BRACE:
6939 case CPP_DEREF_STAR:
6947 case CPP_GREATER_EQ:
6967 /* '[' may start a primary-expression in obj-c++. */
6968 case CPP_OPEN_SQUARE:
6969 return c_dialect_objc ();
6976 /* Parse a cast-expression.
6980 ( type-id ) cast-expression
6982 ADDRESS_P is true iff the unary-expression is appearing as the
6983 operand of the `&' operator. CAST_P is true if this expression is
6984 the target of a cast.
6986 Returns a representation of the expression. */
6989 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6992 /* If it's a `(', then we might be looking at a cast. */
6993 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6995 tree type = NULL_TREE;
6996 tree expr = NULL_TREE;
6997 bool compound_literal_p;
6998 const char *saved_message;
7000 /* There's no way to know yet whether or not this is a cast.
7001 For example, `(int (3))' is a unary-expression, while `(int)
7002 3' is a cast. So, we resort to parsing tentatively. */
7003 cp_parser_parse_tentatively (parser);
7004 /* Types may not be defined in a cast. */
7005 saved_message = parser->type_definition_forbidden_message;
7006 parser->type_definition_forbidden_message
7007 = G_("types may not be defined in casts");
7008 /* Consume the `('. */
7009 cp_lexer_consume_token (parser->lexer);
7010 /* A very tricky bit is that `(struct S) { 3 }' is a
7011 compound-literal (which we permit in C++ as an extension).
7012 But, that construct is not a cast-expression -- it is a
7013 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7014 is legal; if the compound-literal were a cast-expression,
7015 you'd need an extra set of parentheses.) But, if we parse
7016 the type-id, and it happens to be a class-specifier, then we
7017 will commit to the parse at that point, because we cannot
7018 undo the action that is done when creating a new class. So,
7019 then we cannot back up and do a postfix-expression.
7021 Therefore, we scan ahead to the closing `)', and check to see
7022 if the token after the `)' is a `{'. If so, we are not
7023 looking at a cast-expression.
7025 Save tokens so that we can put them back. */
7026 cp_lexer_save_tokens (parser->lexer);
7027 /* Skip tokens until the next token is a closing parenthesis.
7028 If we find the closing `)', and the next token is a `{', then
7029 we are looking at a compound-literal. */
7031 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7032 /*consume_paren=*/true)
7033 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7034 /* Roll back the tokens we skipped. */
7035 cp_lexer_rollback_tokens (parser->lexer);
7036 /* If we were looking at a compound-literal, simulate an error
7037 so that the call to cp_parser_parse_definitely below will
7039 if (compound_literal_p)
7040 cp_parser_simulate_error (parser);
7043 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7044 parser->in_type_id_in_expr_p = true;
7045 /* Look for the type-id. */
7046 type = cp_parser_type_id (parser);
7047 /* Look for the closing `)'. */
7048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7049 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7052 /* Restore the saved message. */
7053 parser->type_definition_forbidden_message = saved_message;
7055 /* At this point this can only be either a cast or a
7056 parenthesized ctor such as `(T ())' that looks like a cast to
7057 function returning T. */
7058 if (!cp_parser_error_occurred (parser)
7059 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7062 cp_parser_parse_definitely (parser);
7063 expr = cp_parser_cast_expression (parser,
7064 /*address_p=*/false,
7065 /*cast_p=*/true, pidk);
7067 /* Warn about old-style casts, if so requested. */
7068 if (warn_old_style_cast
7069 && !in_system_header
7070 && !VOID_TYPE_P (type)
7071 && current_lang_name != lang_name_c)
7072 warning (OPT_Wold_style_cast, "use of old-style cast");
7074 /* Only type conversions to integral or enumeration types
7075 can be used in constant-expressions. */
7076 if (!cast_valid_in_integral_constant_expression_p (type)
7077 && cp_parser_non_integral_constant_expression (parser,
7079 return error_mark_node;
7081 /* Perform the cast. */
7082 expr = build_c_cast (input_location, type, expr);
7086 cp_parser_abort_tentative_parse (parser);
7089 /* If we get here, then it's not a cast, so it must be a
7090 unary-expression. */
7091 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7094 /* Parse a binary expression of the general form:
7098 pm-expression .* cast-expression
7099 pm-expression ->* cast-expression
7101 multiplicative-expression:
7103 multiplicative-expression * pm-expression
7104 multiplicative-expression / pm-expression
7105 multiplicative-expression % pm-expression
7107 additive-expression:
7108 multiplicative-expression
7109 additive-expression + multiplicative-expression
7110 additive-expression - multiplicative-expression
7114 shift-expression << additive-expression
7115 shift-expression >> additive-expression
7117 relational-expression:
7119 relational-expression < shift-expression
7120 relational-expression > shift-expression
7121 relational-expression <= shift-expression
7122 relational-expression >= shift-expression
7126 relational-expression:
7127 relational-expression <? shift-expression
7128 relational-expression >? shift-expression
7130 equality-expression:
7131 relational-expression
7132 equality-expression == relational-expression
7133 equality-expression != relational-expression
7137 and-expression & equality-expression
7139 exclusive-or-expression:
7141 exclusive-or-expression ^ and-expression
7143 inclusive-or-expression:
7144 exclusive-or-expression
7145 inclusive-or-expression | exclusive-or-expression
7147 logical-and-expression:
7148 inclusive-or-expression
7149 logical-and-expression && inclusive-or-expression
7151 logical-or-expression:
7152 logical-and-expression
7153 logical-or-expression || logical-and-expression
7155 All these are implemented with a single function like:
7158 simple-cast-expression
7159 binary-expression <token> binary-expression
7161 CAST_P is true if this expression is the target of a cast.
7163 The binops_by_token map is used to get the tree codes for each <token> type.
7164 binary-expressions are associated according to a precedence table. */
7166 #define TOKEN_PRECEDENCE(token) \
7167 (((token->type == CPP_GREATER \
7168 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7169 && !parser->greater_than_is_operator_p) \
7170 ? PREC_NOT_OPERATOR \
7171 : binops_by_token[token->type].prec)
7174 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7175 bool no_toplevel_fold_p,
7176 enum cp_parser_prec prec,
7179 cp_parser_expression_stack stack;
7180 cp_parser_expression_stack_entry *sp = &stack[0];
7183 enum tree_code tree_type, lhs_type, rhs_type;
7184 enum cp_parser_prec new_prec, lookahead_prec;
7187 /* Parse the first expression. */
7188 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7189 lhs_type = ERROR_MARK;
7193 /* Get an operator token. */
7194 token = cp_lexer_peek_token (parser->lexer);
7196 if (warn_cxx0x_compat
7197 && token->type == CPP_RSHIFT
7198 && !parser->greater_than_is_operator_p)
7200 if (warning_at (token->location, OPT_Wc__0x_compat,
7201 "%<>>%> operator will be treated as"
7202 " two right angle brackets in C++0x"))
7203 inform (token->location,
7204 "suggest parentheses around %<>>%> expression");
7207 new_prec = TOKEN_PRECEDENCE (token);
7209 /* Popping an entry off the stack means we completed a subexpression:
7210 - either we found a token which is not an operator (`>' where it is not
7211 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7212 will happen repeatedly;
7213 - or, we found an operator which has lower priority. This is the case
7214 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7216 if (new_prec <= prec)
7225 tree_type = binops_by_token[token->type].tree_type;
7227 /* We used the operator token. */
7228 cp_lexer_consume_token (parser->lexer);
7230 /* For "false && x" or "true || x", x will never be executed;
7231 disable warnings while evaluating it. */
7232 if (tree_type == TRUTH_ANDIF_EXPR)
7233 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7234 else if (tree_type == TRUTH_ORIF_EXPR)
7235 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7237 /* Extract another operand. It may be the RHS of this expression
7238 or the LHS of a new, higher priority expression. */
7239 rhs = cp_parser_simple_cast_expression (parser);
7240 rhs_type = ERROR_MARK;
7242 /* Get another operator token. Look up its precedence to avoid
7243 building a useless (immediately popped) stack entry for common
7244 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7245 token = cp_lexer_peek_token (parser->lexer);
7246 lookahead_prec = TOKEN_PRECEDENCE (token);
7247 if (lookahead_prec > new_prec)
7249 /* ... and prepare to parse the RHS of the new, higher priority
7250 expression. Since precedence levels on the stack are
7251 monotonically increasing, we do not have to care about
7254 sp->tree_type = tree_type;
7256 sp->lhs_type = lhs_type;
7259 lhs_type = rhs_type;
7261 new_prec = lookahead_prec;
7265 lookahead_prec = new_prec;
7266 /* If the stack is not empty, we have parsed into LHS the right side
7267 (`4' in the example above) of an expression we had suspended.
7268 We can use the information on the stack to recover the LHS (`3')
7269 from the stack together with the tree code (`MULT_EXPR'), and
7270 the precedence of the higher level subexpression
7271 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7272 which will be used to actually build the additive expression. */
7275 tree_type = sp->tree_type;
7277 rhs_type = lhs_type;
7279 lhs_type = sp->lhs_type;
7282 /* Undo the disabling of warnings done above. */
7283 if (tree_type == TRUTH_ANDIF_EXPR)
7284 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7285 else if (tree_type == TRUTH_ORIF_EXPR)
7286 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7289 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7290 ERROR_MARK for everything that is not a binary expression.
7291 This makes warn_about_parentheses miss some warnings that
7292 involve unary operators. For unary expressions we should
7293 pass the correct tree_code unless the unary expression was
7294 surrounded by parentheses.
7296 if (no_toplevel_fold_p
7297 && lookahead_prec <= prec
7299 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7300 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7302 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7303 &overload, tf_warning_or_error);
7304 lhs_type = tree_type;
7306 /* If the binary operator required the use of an overloaded operator,
7307 then this expression cannot be an integral constant-expression.
7308 An overloaded operator can be used even if both operands are
7309 otherwise permissible in an integral constant-expression if at
7310 least one of the operands is of enumeration type. */
7313 && cp_parser_non_integral_constant_expression (parser,
7315 return error_mark_node;
7322 /* Parse the `? expression : assignment-expression' part of a
7323 conditional-expression. The LOGICAL_OR_EXPR is the
7324 logical-or-expression that started the conditional-expression.
7325 Returns a representation of the entire conditional-expression.
7327 This routine is used by cp_parser_assignment_expression.
7329 ? expression : assignment-expression
7333 ? : assignment-expression */
7336 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7339 tree assignment_expr;
7340 struct cp_token *token;
7342 /* Consume the `?' token. */
7343 cp_lexer_consume_token (parser->lexer);
7344 token = cp_lexer_peek_token (parser->lexer);
7345 if (cp_parser_allow_gnu_extensions_p (parser)
7346 && token->type == CPP_COLON)
7348 pedwarn (token->location, OPT_pedantic,
7349 "ISO C++ does not allow ?: with omitted middle operand");
7350 /* Implicit true clause. */
7352 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7353 warn_for_omitted_condop (token->location, logical_or_expr);
7357 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7358 parser->colon_corrects_to_scope_p = false;
7359 /* Parse the expression. */
7360 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7361 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7362 c_inhibit_evaluation_warnings +=
7363 ((logical_or_expr == truthvalue_true_node)
7364 - (logical_or_expr == truthvalue_false_node));
7365 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7368 /* The next token should be a `:'. */
7369 cp_parser_require (parser, CPP_COLON, RT_COLON);
7370 /* Parse the assignment-expression. */
7371 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7372 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7374 /* Build the conditional-expression. */
7375 return build_x_conditional_expr (logical_or_expr,
7378 tf_warning_or_error);
7381 /* Parse an assignment-expression.
7383 assignment-expression:
7384 conditional-expression
7385 logical-or-expression assignment-operator assignment_expression
7388 CAST_P is true if this expression is the target of a cast.
7390 Returns a representation for the expression. */
7393 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7398 /* If the next token is the `throw' keyword, then we're looking at
7399 a throw-expression. */
7400 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7401 expr = cp_parser_throw_expression (parser);
7402 /* Otherwise, it must be that we are looking at a
7403 logical-or-expression. */
7406 /* Parse the binary expressions (logical-or-expression). */
7407 expr = cp_parser_binary_expression (parser, cast_p, false,
7408 PREC_NOT_OPERATOR, pidk);
7409 /* If the next token is a `?' then we're actually looking at a
7410 conditional-expression. */
7411 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7412 return cp_parser_question_colon_clause (parser, expr);
7415 enum tree_code assignment_operator;
7417 /* If it's an assignment-operator, we're using the second
7420 = cp_parser_assignment_operator_opt (parser);
7421 if (assignment_operator != ERROR_MARK)
7423 bool non_constant_p;
7425 /* Parse the right-hand side of the assignment. */
7426 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7428 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7429 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7431 /* An assignment may not appear in a
7432 constant-expression. */
7433 if (cp_parser_non_integral_constant_expression (parser,
7435 return error_mark_node;
7436 /* Build the assignment expression. */
7437 expr = build_x_modify_expr (expr,
7438 assignment_operator,
7440 tf_warning_or_error);
7448 /* Parse an (optional) assignment-operator.
7450 assignment-operator: one of
7451 = *= /= %= += -= >>= <<= &= ^= |=
7455 assignment-operator: one of
7458 If the next token is an assignment operator, the corresponding tree
7459 code is returned, and the token is consumed. For example, for
7460 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7461 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7462 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7463 operator, ERROR_MARK is returned. */
7465 static enum tree_code
7466 cp_parser_assignment_operator_opt (cp_parser* parser)
7471 /* Peek at the next token. */
7472 token = cp_lexer_peek_token (parser->lexer);
7474 switch (token->type)
7485 op = TRUNC_DIV_EXPR;
7489 op = TRUNC_MOD_EXPR;
7521 /* Nothing else is an assignment operator. */
7525 /* If it was an assignment operator, consume it. */
7526 if (op != ERROR_MARK)
7527 cp_lexer_consume_token (parser->lexer);
7532 /* Parse an expression.
7535 assignment-expression
7536 expression , assignment-expression
7538 CAST_P is true if this expression is the target of a cast.
7540 Returns a representation of the expression. */
7543 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7545 tree expression = NULL_TREE;
7549 tree assignment_expression;
7551 /* Parse the next assignment-expression. */
7552 assignment_expression
7553 = cp_parser_assignment_expression (parser, cast_p, pidk);
7554 /* If this is the first assignment-expression, we can just
7557 expression = assignment_expression;
7559 expression = build_x_compound_expr (expression,
7560 assignment_expression,
7561 tf_warning_or_error);
7562 /* If the next token is not a comma, then we are done with the
7564 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7566 /* Consume the `,'. */
7567 cp_lexer_consume_token (parser->lexer);
7568 /* A comma operator cannot appear in a constant-expression. */
7569 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7570 expression = error_mark_node;
7576 /* Parse a constant-expression.
7578 constant-expression:
7579 conditional-expression
7581 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7582 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7583 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7584 is false, NON_CONSTANT_P should be NULL. */
7587 cp_parser_constant_expression (cp_parser* parser,
7588 bool allow_non_constant_p,
7589 bool *non_constant_p)
7591 bool saved_integral_constant_expression_p;
7592 bool saved_allow_non_integral_constant_expression_p;
7593 bool saved_non_integral_constant_expression_p;
7596 /* It might seem that we could simply parse the
7597 conditional-expression, and then check to see if it were
7598 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7599 one that the compiler can figure out is constant, possibly after
7600 doing some simplifications or optimizations. The standard has a
7601 precise definition of constant-expression, and we must honor
7602 that, even though it is somewhat more restrictive.
7608 is not a legal declaration, because `(2, 3)' is not a
7609 constant-expression. The `,' operator is forbidden in a
7610 constant-expression. However, GCC's constant-folding machinery
7611 will fold this operation to an INTEGER_CST for `3'. */
7613 /* Save the old settings. */
7614 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7615 saved_allow_non_integral_constant_expression_p
7616 = parser->allow_non_integral_constant_expression_p;
7617 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7618 /* We are now parsing a constant-expression. */
7619 parser->integral_constant_expression_p = true;
7620 parser->allow_non_integral_constant_expression_p
7621 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7622 parser->non_integral_constant_expression_p = false;
7623 /* Although the grammar says "conditional-expression", we parse an
7624 "assignment-expression", which also permits "throw-expression"
7625 and the use of assignment operators. In the case that
7626 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7627 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7628 actually essential that we look for an assignment-expression.
7629 For example, cp_parser_initializer_clauses uses this function to
7630 determine whether a particular assignment-expression is in fact
7632 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7633 /* Restore the old settings. */
7634 parser->integral_constant_expression_p
7635 = saved_integral_constant_expression_p;
7636 parser->allow_non_integral_constant_expression_p
7637 = saved_allow_non_integral_constant_expression_p;
7638 if (cxx_dialect >= cxx0x)
7640 /* Require an rvalue constant expression here; that's what our
7641 callers expect. Reference constant expressions are handled
7642 separately in e.g. cp_parser_template_argument. */
7643 bool is_const = potential_rvalue_constant_expression (expression);
7644 parser->non_integral_constant_expression_p = !is_const;
7645 if (!is_const && !allow_non_constant_p)
7646 require_potential_rvalue_constant_expression (expression);
7648 if (allow_non_constant_p)
7649 *non_constant_p = parser->non_integral_constant_expression_p;
7650 parser->non_integral_constant_expression_p
7651 = saved_non_integral_constant_expression_p;
7656 /* Parse __builtin_offsetof.
7658 offsetof-expression:
7659 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7661 offsetof-member-designator:
7663 | offsetof-member-designator "." id-expression
7664 | offsetof-member-designator "[" expression "]"
7665 | offsetof-member-designator "->" id-expression */
7668 cp_parser_builtin_offsetof (cp_parser *parser)
7670 int save_ice_p, save_non_ice_p;
7675 /* We're about to accept non-integral-constant things, but will
7676 definitely yield an integral constant expression. Save and
7677 restore these values around our local parsing. */
7678 save_ice_p = parser->integral_constant_expression_p;
7679 save_non_ice_p = parser->non_integral_constant_expression_p;
7681 /* Consume the "__builtin_offsetof" token. */
7682 cp_lexer_consume_token (parser->lexer);
7683 /* Consume the opening `('. */
7684 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7685 /* Parse the type-id. */
7686 type = cp_parser_type_id (parser);
7687 /* Look for the `,'. */
7688 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7689 token = cp_lexer_peek_token (parser->lexer);
7691 /* Build the (type *)null that begins the traditional offsetof macro. */
7692 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7693 tf_warning_or_error);
7695 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7696 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7697 true, &dummy, token->location);
7700 token = cp_lexer_peek_token (parser->lexer);
7701 switch (token->type)
7703 case CPP_OPEN_SQUARE:
7704 /* offsetof-member-designator "[" expression "]" */
7705 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7709 /* offsetof-member-designator "->" identifier */
7710 expr = grok_array_decl (expr, integer_zero_node);
7714 /* offsetof-member-designator "." identifier */
7715 cp_lexer_consume_token (parser->lexer);
7716 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7721 case CPP_CLOSE_PAREN:
7722 /* Consume the ")" token. */
7723 cp_lexer_consume_token (parser->lexer);
7727 /* Error. We know the following require will fail, but
7728 that gives the proper error message. */
7729 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7730 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7731 expr = error_mark_node;
7737 /* If we're processing a template, we can't finish the semantics yet.
7738 Otherwise we can fold the entire expression now. */
7739 if (processing_template_decl)
7740 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7742 expr = finish_offsetof (expr);
7745 parser->integral_constant_expression_p = save_ice_p;
7746 parser->non_integral_constant_expression_p = save_non_ice_p;
7751 /* Parse a trait expression.
7753 Returns a representation of the expression, the underlying type
7754 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7757 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7760 tree type1, type2 = NULL_TREE;
7761 bool binary = false;
7762 cp_decl_specifier_seq decl_specs;
7766 case RID_HAS_NOTHROW_ASSIGN:
7767 kind = CPTK_HAS_NOTHROW_ASSIGN;
7769 case RID_HAS_NOTHROW_CONSTRUCTOR:
7770 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7772 case RID_HAS_NOTHROW_COPY:
7773 kind = CPTK_HAS_NOTHROW_COPY;
7775 case RID_HAS_TRIVIAL_ASSIGN:
7776 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7778 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7779 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7781 case RID_HAS_TRIVIAL_COPY:
7782 kind = CPTK_HAS_TRIVIAL_COPY;
7784 case RID_HAS_TRIVIAL_DESTRUCTOR:
7785 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7787 case RID_HAS_VIRTUAL_DESTRUCTOR:
7788 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7790 case RID_IS_ABSTRACT:
7791 kind = CPTK_IS_ABSTRACT;
7793 case RID_IS_BASE_OF:
7794 kind = CPTK_IS_BASE_OF;
7798 kind = CPTK_IS_CLASS;
7800 case RID_IS_CONVERTIBLE_TO:
7801 kind = CPTK_IS_CONVERTIBLE_TO;
7805 kind = CPTK_IS_EMPTY;
7808 kind = CPTK_IS_ENUM;
7810 case RID_IS_LITERAL_TYPE:
7811 kind = CPTK_IS_LITERAL_TYPE;
7816 case RID_IS_POLYMORPHIC:
7817 kind = CPTK_IS_POLYMORPHIC;
7819 case RID_IS_STD_LAYOUT:
7820 kind = CPTK_IS_STD_LAYOUT;
7822 case RID_IS_TRIVIAL:
7823 kind = CPTK_IS_TRIVIAL;
7826 kind = CPTK_IS_UNION;
7828 case RID_UNDERLYING_TYPE:
7829 kind = CPTK_UNDERLYING_TYPE;
7834 case RID_DIRECT_BASES:
7835 kind = CPTK_DIRECT_BASES;
7841 /* Consume the token. */
7842 cp_lexer_consume_token (parser->lexer);
7844 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7846 type1 = cp_parser_type_id (parser);
7848 if (type1 == error_mark_node)
7849 return error_mark_node;
7851 /* Build a trivial decl-specifier-seq. */
7852 clear_decl_specs (&decl_specs);
7853 decl_specs.type = type1;
7855 /* Call grokdeclarator to figure out what type this is. */
7856 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7857 /*initialized=*/0, /*attrlist=*/NULL);
7861 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7863 type2 = cp_parser_type_id (parser);
7865 if (type2 == error_mark_node)
7866 return error_mark_node;
7868 /* Build a trivial decl-specifier-seq. */
7869 clear_decl_specs (&decl_specs);
7870 decl_specs.type = type2;
7872 /* Call grokdeclarator to figure out what type this is. */
7873 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7874 /*initialized=*/0, /*attrlist=*/NULL);
7877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7879 /* Complete the trait expression, which may mean either processing
7880 the trait expr now or saving it for template instantiation. */
7883 case CPTK_UNDERLYING_TYPE:
7884 return finish_underlying_type (type1);
7886 return finish_bases (type1, false);
7887 case CPTK_DIRECT_BASES:
7888 return finish_bases (type1, true);
7890 return finish_trait_expr (kind, type1, type2);
7894 /* Lambdas that appear in variable initializer or default argument scope
7895 get that in their mangling, so we need to record it. We might as well
7896 use the count for function and namespace scopes as well. */
7897 static GTY(()) tree lambda_scope;
7898 static GTY(()) int lambda_count;
7899 typedef struct GTY(()) tree_int
7904 DEF_VEC_O(tree_int);
7905 DEF_VEC_ALLOC_O(tree_int,gc);
7906 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7909 start_lambda_scope (tree decl)
7913 /* Once we're inside a function, we ignore other scopes and just push
7914 the function again so that popping works properly. */
7915 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7916 decl = current_function_decl;
7917 ti.t = lambda_scope;
7918 ti.i = lambda_count;
7919 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7920 if (lambda_scope != decl)
7922 /* Don't reset the count if we're still in the same function. */
7923 lambda_scope = decl;
7929 record_lambda_scope (tree lambda)
7931 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7932 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7936 finish_lambda_scope (void)
7938 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7939 if (lambda_scope != p->t)
7941 lambda_scope = p->t;
7942 lambda_count = p->i;
7944 VEC_pop (tree_int, lambda_scope_stack);
7947 /* Parse a lambda expression.
7950 lambda-introducer lambda-declarator [opt] compound-statement
7952 Returns a representation of the expression. */
7955 cp_parser_lambda_expression (cp_parser* parser)
7957 tree lambda_expr = build_lambda_expr ();
7961 LAMBDA_EXPR_LOCATION (lambda_expr)
7962 = cp_lexer_peek_token (parser->lexer)->location;
7964 if (cp_unevaluated_operand)
7965 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7966 "lambda-expression in unevaluated context");
7968 /* We may be in the middle of deferred access check. Disable
7970 push_deferring_access_checks (dk_no_deferred);
7972 cp_parser_lambda_introducer (parser, lambda_expr);
7974 type = begin_lambda_type (lambda_expr);
7976 record_lambda_scope (lambda_expr);
7978 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7979 determine_visibility (TYPE_NAME (type));
7981 /* Now that we've started the type, add the capture fields for any
7982 explicit captures. */
7983 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7986 /* Inside the class, surrounding template-parameter-lists do not apply. */
7987 unsigned int saved_num_template_parameter_lists
7988 = parser->num_template_parameter_lists;
7989 unsigned char in_statement = parser->in_statement;
7990 bool in_switch_statement_p = parser->in_switch_statement_p;
7992 parser->num_template_parameter_lists = 0;
7993 parser->in_statement = 0;
7994 parser->in_switch_statement_p = false;
7996 /* By virtue of defining a local class, a lambda expression has access to
7997 the private variables of enclosing classes. */
7999 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8002 cp_parser_lambda_body (parser, lambda_expr);
8003 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8004 cp_parser_skip_to_end_of_block_or_statement (parser);
8006 /* The capture list was built up in reverse order; fix that now. */
8008 tree newlist = NULL_TREE;
8011 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8014 next = TREE_CHAIN (elt);
8015 TREE_CHAIN (elt) = newlist;
8018 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8022 maybe_add_lambda_conv_op (type);
8024 type = finish_struct (type, /*attributes=*/NULL_TREE);
8026 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8027 parser->in_statement = in_statement;
8028 parser->in_switch_statement_p = in_switch_statement_p;
8031 pop_deferring_access_checks ();
8033 /* This field is only used during parsing of the lambda. */
8034 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8036 /* This lambda shouldn't have any proxies left at this point. */
8037 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8038 /* And now that we're done, push proxies for an enclosing lambda. */
8039 insert_pending_capture_proxies ();
8042 return build_lambda_object (lambda_expr);
8044 return error_mark_node;
8047 /* Parse the beginning of a lambda expression.
8050 [ lambda-capture [opt] ]
8052 LAMBDA_EXPR is the current representation of the lambda expression. */
8055 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8057 /* Need commas after the first capture. */
8060 /* Eat the leading `['. */
8061 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8063 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8064 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8065 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8066 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8067 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8068 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8070 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8072 cp_lexer_consume_token (parser->lexer);
8076 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8078 cp_token* capture_token;
8080 tree capture_init_expr;
8081 cp_id_kind idk = CP_ID_KIND_NONE;
8082 bool explicit_init_p = false;
8084 enum capture_kind_type
8089 enum capture_kind_type capture_kind = BY_COPY;
8091 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8093 error ("expected end of capture-list");
8100 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8102 /* Possibly capture `this'. */
8103 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8105 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8106 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8107 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8108 "with by-copy capture default");
8109 cp_lexer_consume_token (parser->lexer);
8110 add_capture (lambda_expr,
8111 /*id=*/this_identifier,
8112 /*initializer=*/finish_this_expr(),
8113 /*by_reference_p=*/false,
8118 /* Remember whether we want to capture as a reference or not. */
8119 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8121 capture_kind = BY_REFERENCE;
8122 cp_lexer_consume_token (parser->lexer);
8125 /* Get the identifier. */
8126 capture_token = cp_lexer_peek_token (parser->lexer);
8127 capture_id = cp_parser_identifier (parser);
8129 if (capture_id == error_mark_node)
8130 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8131 delimiters, but I modified this to stop on unnested ']' as well. It
8132 was already changed to stop on unnested '}', so the
8133 "closing_parenthesis" name is no more misleading with my change. */
8135 cp_parser_skip_to_closing_parenthesis (parser,
8136 /*recovering=*/true,
8138 /*consume_paren=*/true);
8142 /* Find the initializer for this capture. */
8143 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8145 /* An explicit expression exists. */
8146 cp_lexer_consume_token (parser->lexer);
8147 pedwarn (input_location, OPT_pedantic,
8148 "ISO C++ does not allow initializers "
8149 "in lambda expression capture lists");
8150 capture_init_expr = cp_parser_assignment_expression (parser,
8153 explicit_init_p = true;
8157 const char* error_msg;
8159 /* Turn the identifier into an id-expression. */
8161 = cp_parser_lookup_name
8165 /*is_template=*/false,
8166 /*is_namespace=*/false,
8167 /*check_dependency=*/true,
8168 /*ambiguous_decls=*/NULL,
8169 capture_token->location);
8171 if (capture_init_expr == error_mark_node)
8173 unqualified_name_lookup_error (capture_id);
8176 else if (DECL_P (capture_init_expr)
8177 && (TREE_CODE (capture_init_expr) != VAR_DECL
8178 && TREE_CODE (capture_init_expr) != PARM_DECL))
8180 error_at (capture_token->location,
8181 "capture of non-variable %qD ",
8183 inform (0, "%q+#D declared here", capture_init_expr);
8186 if (TREE_CODE (capture_init_expr) == VAR_DECL
8187 && decl_storage_duration (capture_init_expr) != dk_auto)
8189 pedwarn (capture_token->location, 0, "capture of variable "
8190 "%qD with non-automatic storage duration",
8192 inform (0, "%q+#D declared here", capture_init_expr);
8197 = finish_id_expression
8202 /*integral_constant_expression_p=*/false,
8203 /*allow_non_integral_constant_expression_p=*/false,
8204 /*non_integral_constant_expression_p=*/NULL,
8205 /*template_p=*/false,
8207 /*address_p=*/false,
8208 /*template_arg_p=*/false,
8210 capture_token->location);
8213 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8214 && !explicit_init_p)
8216 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8217 && capture_kind == BY_COPY)
8218 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8219 "of %qD redundant with by-copy capture default",
8221 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8222 && capture_kind == BY_REFERENCE)
8223 pedwarn (capture_token->location, 0, "explicit by-reference "
8224 "capture of %qD redundant with by-reference capture "
8225 "default", capture_id);
8228 add_capture (lambda_expr,
8231 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8235 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8238 /* Parse the (optional) middle of a lambda expression.
8241 ( parameter-declaration-clause [opt] )
8242 attribute-specifier [opt]
8244 exception-specification [opt]
8245 lambda-return-type-clause [opt]
8247 LAMBDA_EXPR is the current representation of the lambda expression. */
8250 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8252 /* 5.1.1.4 of the standard says:
8253 If a lambda-expression does not include a lambda-declarator, it is as if
8254 the lambda-declarator were ().
8255 This means an empty parameter list, no attributes, and no exception
8257 tree param_list = void_list_node;
8258 tree attributes = NULL_TREE;
8259 tree exception_spec = NULL_TREE;
8262 /* The lambda-declarator is optional, but must begin with an opening
8263 parenthesis if present. */
8264 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8266 cp_lexer_consume_token (parser->lexer);
8268 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8270 /* Parse parameters. */
8271 param_list = cp_parser_parameter_declaration_clause (parser);
8273 /* Default arguments shall not be specified in the
8274 parameter-declaration-clause of a lambda-declarator. */
8275 for (t = param_list; t; t = TREE_CHAIN (t))
8276 if (TREE_PURPOSE (t))
8277 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8278 "default argument specified for lambda parameter");
8280 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8282 attributes = cp_parser_attributes_opt (parser);
8284 /* Parse optional `mutable' keyword. */
8285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8287 cp_lexer_consume_token (parser->lexer);
8288 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8291 /* Parse optional exception specification. */
8292 exception_spec = cp_parser_exception_specification_opt (parser);
8294 /* Parse optional trailing return type. */
8295 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8297 cp_lexer_consume_token (parser->lexer);
8298 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8301 /* The function parameters must be in scope all the way until after the
8302 trailing-return-type in case of decltype. */
8303 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8304 pop_binding (DECL_NAME (t), t);
8309 /* Create the function call operator.
8311 Messing with declarators like this is no uglier than building up the
8312 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8315 cp_decl_specifier_seq return_type_specs;
8316 cp_declarator* declarator;
8321 clear_decl_specs (&return_type_specs);
8322 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8323 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8325 /* Maybe we will deduce the return type later, but we can use void
8326 as a placeholder return type anyways. */
8327 return_type_specs.type = void_type_node;
8329 p = obstack_alloc (&declarator_obstack, 0);
8331 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8334 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8335 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8336 declarator = make_call_declarator (declarator, param_list, quals,
8337 VIRT_SPEC_UNSPECIFIED,
8339 /*late_return_type=*/NULL_TREE);
8340 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8342 fco = grokmethod (&return_type_specs,
8345 if (fco != error_mark_node)
8347 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8348 DECL_ARTIFICIAL (fco) = 1;
8349 /* Give the object parameter a different name. */
8350 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8353 finish_member_declaration (fco);
8355 obstack_free (&declarator_obstack, p);
8357 return (fco != error_mark_node);
8361 /* Parse the body of a lambda expression, which is simply
8365 but which requires special handling.
8366 LAMBDA_EXPR is the current representation of the lambda expression. */
8369 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8371 bool nested = (current_function_decl != NULL_TREE);
8372 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8374 push_function_context ();
8376 /* Still increment function_depth so that we don't GC in the
8377 middle of an expression. */
8379 /* Clear this in case we're in the middle of a default argument. */
8380 parser->local_variables_forbidden_p = false;
8382 /* Finish the function call operator
8384 + late_parsing_for_member
8385 + function_definition_after_declarator
8386 + ctor_initializer_opt_and_function_body */
8388 tree fco = lambda_function (lambda_expr);
8394 /* Let the front end know that we are going to be defining this
8396 start_preparsed_function (fco,
8398 SF_PRE_PARSED | SF_INCLASS_INLINE);
8400 start_lambda_scope (fco);
8401 body = begin_function_body ();
8403 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8406 /* Push the proxies for any explicit captures. */
8407 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8408 cap = TREE_CHAIN (cap))
8409 build_capture_proxy (TREE_PURPOSE (cap));
8411 compound_stmt = begin_compound_stmt (0);
8413 /* 5.1.1.4 of the standard says:
8414 If a lambda-expression does not include a trailing-return-type, it
8415 is as if the trailing-return-type denotes the following type:
8416 * if the compound-statement is of the form
8417 { return attribute-specifier [opt] expression ; }
8418 the type of the returned expression after lvalue-to-rvalue
8419 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8420 (_conv.array_ 4.2), and function-to-pointer conversion
8422 * otherwise, void. */
8424 /* In a lambda that has neither a lambda-return-type-clause
8425 nor a deducible form, errors should be reported for return statements
8426 in the body. Since we used void as the placeholder return type, parsing
8427 the body as usual will give such desired behavior. */
8428 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8429 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8430 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8432 tree expr = NULL_TREE;
8433 cp_id_kind idk = CP_ID_KIND_NONE;
8435 /* Parse tentatively in case there's more after the initial return
8437 cp_parser_parse_tentatively (parser);
8439 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8441 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8443 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8444 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8446 if (cp_parser_parse_definitely (parser))
8448 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8450 /* Will get error here if type not deduced yet. */
8451 finish_return_stmt (expr);
8459 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8460 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8461 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8462 cp_parser_label_declaration (parser);
8463 cp_parser_statement_seq_opt (parser, NULL_TREE);
8464 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8465 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8468 finish_compound_stmt (compound_stmt);
8471 finish_function_body (body);
8472 finish_lambda_scope ();
8474 /* Finish the function and generate code for it if necessary. */
8475 expand_or_defer_fn (finish_function (/*inline*/2));
8478 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8480 pop_function_context();
8485 /* Statements [gram.stmt.stmt] */
8487 /* Parse a statement.
8491 expression-statement
8496 declaration-statement
8499 IN_COMPOUND is true when the statement is nested inside a
8500 cp_parser_compound_statement; this matters for certain pragmas.
8502 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8503 is a (possibly labeled) if statement which is not enclosed in braces
8504 and has an else clause. This is used to implement -Wparentheses. */
8507 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8508 bool in_compound, bool *if_p)
8512 location_t statement_location;
8517 /* There is no statement yet. */
8518 statement = NULL_TREE;
8519 /* Peek at the next token. */
8520 token = cp_lexer_peek_token (parser->lexer);
8521 /* Remember the location of the first token in the statement. */
8522 statement_location = token->location;
8523 /* If this is a keyword, then that will often determine what kind of
8524 statement we have. */
8525 if (token->type == CPP_KEYWORD)
8527 enum rid keyword = token->keyword;
8533 /* Looks like a labeled-statement with a case label.
8534 Parse the label, and then use tail recursion to parse
8536 cp_parser_label_for_labeled_statement (parser);
8541 statement = cp_parser_selection_statement (parser, if_p);
8547 statement = cp_parser_iteration_statement (parser);
8554 statement = cp_parser_jump_statement (parser);
8557 /* Objective-C++ exception-handling constructs. */
8560 case RID_AT_FINALLY:
8561 case RID_AT_SYNCHRONIZED:
8563 statement = cp_parser_objc_statement (parser);
8567 statement = cp_parser_try_block (parser);
8571 /* This must be a namespace alias definition. */
8572 cp_parser_declaration_statement (parser);
8576 /* It might be a keyword like `int' that can start a
8577 declaration-statement. */
8581 else if (token->type == CPP_NAME)
8583 /* If the next token is a `:', then we are looking at a
8584 labeled-statement. */
8585 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8586 if (token->type == CPP_COLON)
8588 /* Looks like a labeled-statement with an ordinary label.
8589 Parse the label, and then use tail recursion to parse
8591 cp_parser_label_for_labeled_statement (parser);
8595 /* Anything that starts with a `{' must be a compound-statement. */
8596 else if (token->type == CPP_OPEN_BRACE)
8597 statement = cp_parser_compound_statement (parser, NULL, false, false);
8598 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8599 a statement all its own. */
8600 else if (token->type == CPP_PRAGMA)
8602 /* Only certain OpenMP pragmas are attached to statements, and thus
8603 are considered statements themselves. All others are not. In
8604 the context of a compound, accept the pragma as a "statement" and
8605 return so that we can check for a close brace. Otherwise we
8606 require a real statement and must go back and read one. */
8608 cp_parser_pragma (parser, pragma_compound);
8609 else if (!cp_parser_pragma (parser, pragma_stmt))
8613 else if (token->type == CPP_EOF)
8615 cp_parser_error (parser, "expected statement");
8619 /* Everything else must be a declaration-statement or an
8620 expression-statement. Try for the declaration-statement
8621 first, unless we are looking at a `;', in which case we know that
8622 we have an expression-statement. */
8625 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8627 cp_parser_parse_tentatively (parser);
8628 /* Try to parse the declaration-statement. */
8629 cp_parser_declaration_statement (parser);
8630 /* If that worked, we're done. */
8631 if (cp_parser_parse_definitely (parser))
8634 /* Look for an expression-statement instead. */
8635 statement = cp_parser_expression_statement (parser, in_statement_expr);
8638 /* Set the line number for the statement. */
8639 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8640 SET_EXPR_LOCATION (statement, statement_location);
8643 /* Parse the label for a labeled-statement, i.e.
8646 case constant-expression :
8650 case constant-expression ... constant-expression : statement
8652 When a label is parsed without errors, the label is added to the
8653 parse tree by the finish_* functions, so this function doesn't
8654 have to return the label. */
8657 cp_parser_label_for_labeled_statement (cp_parser* parser)
8660 tree label = NULL_TREE;
8661 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8663 /* The next token should be an identifier. */
8664 token = cp_lexer_peek_token (parser->lexer);
8665 if (token->type != CPP_NAME
8666 && token->type != CPP_KEYWORD)
8668 cp_parser_error (parser, "expected labeled-statement");
8672 parser->colon_corrects_to_scope_p = false;
8673 switch (token->keyword)
8680 /* Consume the `case' token. */
8681 cp_lexer_consume_token (parser->lexer);
8682 /* Parse the constant-expression. */
8683 expr = cp_parser_constant_expression (parser,
8684 /*allow_non_constant_p=*/false,
8687 ellipsis = cp_lexer_peek_token (parser->lexer);
8688 if (ellipsis->type == CPP_ELLIPSIS)
8690 /* Consume the `...' token. */
8691 cp_lexer_consume_token (parser->lexer);
8693 cp_parser_constant_expression (parser,
8694 /*allow_non_constant_p=*/false,
8696 /* We don't need to emit warnings here, as the common code
8697 will do this for us. */
8700 expr_hi = NULL_TREE;
8702 if (parser->in_switch_statement_p)
8703 finish_case_label (token->location, expr, expr_hi);
8705 error_at (token->location,
8706 "case label %qE not within a switch statement",
8712 /* Consume the `default' token. */
8713 cp_lexer_consume_token (parser->lexer);
8715 if (parser->in_switch_statement_p)
8716 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8718 error_at (token->location, "case label not within a switch statement");
8722 /* Anything else must be an ordinary label. */
8723 label = finish_label_stmt (cp_parser_identifier (parser));
8727 /* Require the `:' token. */
8728 cp_parser_require (parser, CPP_COLON, RT_COLON);
8730 /* An ordinary label may optionally be followed by attributes.
8731 However, this is only permitted if the attributes are then
8732 followed by a semicolon. This is because, for backward
8733 compatibility, when parsing
8734 lab: __attribute__ ((unused)) int i;
8735 we want the attribute to attach to "i", not "lab". */
8736 if (label != NULL_TREE
8737 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8741 cp_parser_parse_tentatively (parser);
8742 attrs = cp_parser_attributes_opt (parser);
8743 if (attrs == NULL_TREE
8744 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8745 cp_parser_abort_tentative_parse (parser);
8746 else if (!cp_parser_parse_definitely (parser))
8749 cplus_decl_attributes (&label, attrs, 0);
8752 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8755 /* Parse an expression-statement.
8757 expression-statement:
8760 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8761 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8762 indicates whether this expression-statement is part of an
8763 expression statement. */
8766 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8768 tree statement = NULL_TREE;
8769 cp_token *token = cp_lexer_peek_token (parser->lexer);
8771 /* If the next token is a ';', then there is no expression
8773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8774 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8776 /* Give a helpful message for "A<T>::type t;" and the like. */
8777 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8778 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8780 if (TREE_CODE (statement) == SCOPE_REF)
8781 error_at (token->location, "need %<typename%> before %qE because "
8782 "%qT is a dependent scope",
8783 statement, TREE_OPERAND (statement, 0));
8784 else if (is_overloaded_fn (statement)
8785 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8788 tree fn = get_first_fn (statement);
8789 error_at (token->location,
8790 "%<%T::%D%> names the constructor, not the type",
8791 DECL_CONTEXT (fn), DECL_NAME (fn));
8795 /* Consume the final `;'. */
8796 cp_parser_consume_semicolon_at_end_of_statement (parser);
8798 if (in_statement_expr
8799 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8800 /* This is the final expression statement of a statement
8802 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8804 statement = finish_expr_stmt (statement);
8811 /* Parse a compound-statement.
8814 { statement-seq [opt] }
8819 { label-declaration-seq [opt] statement-seq [opt] }
8821 label-declaration-seq:
8823 label-declaration-seq label-declaration
8825 Returns a tree representing the statement. */
8828 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8829 bool in_try, bool function_body)
8833 /* Consume the `{'. */
8834 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8835 return error_mark_node;
8836 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8838 pedwarn (input_location, OPT_pedantic,
8839 "compound-statement in constexpr function");
8840 /* Begin the compound-statement. */
8841 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8842 /* If the next keyword is `__label__' we have a label declaration. */
8843 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8844 cp_parser_label_declaration (parser);
8845 /* Parse an (optional) statement-seq. */
8846 cp_parser_statement_seq_opt (parser, in_statement_expr);
8847 /* Finish the compound-statement. */
8848 finish_compound_stmt (compound_stmt);
8849 /* Consume the `}'. */
8850 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8852 return compound_stmt;
8855 /* Parse an (optional) statement-seq.
8859 statement-seq [opt] statement */
8862 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8864 /* Scan statements until there aren't any more. */
8867 cp_token *token = cp_lexer_peek_token (parser->lexer);
8869 /* If we are looking at a `}', then we have run out of
8870 statements; the same is true if we have reached the end
8871 of file, or have stumbled upon a stray '@end'. */
8872 if (token->type == CPP_CLOSE_BRACE
8873 || token->type == CPP_EOF
8874 || token->type == CPP_PRAGMA_EOL
8875 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8878 /* If we are in a compound statement and find 'else' then
8879 something went wrong. */
8880 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8882 if (parser->in_statement & IN_IF_STMT)
8886 token = cp_lexer_consume_token (parser->lexer);
8887 error_at (token->location, "%<else%> without a previous %<if%>");
8891 /* Parse the statement. */
8892 cp_parser_statement (parser, in_statement_expr, true, NULL);
8896 /* Parse a selection-statement.
8898 selection-statement:
8899 if ( condition ) statement
8900 if ( condition ) statement else statement
8901 switch ( condition ) statement
8903 Returns the new IF_STMT or SWITCH_STMT.
8905 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8906 is a (possibly labeled) if statement which is not enclosed in
8907 braces and has an else clause. This is used to implement
8911 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8919 /* Peek at the next token. */
8920 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8922 /* See what kind of keyword it is. */
8923 keyword = token->keyword;
8932 /* Look for the `('. */
8933 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8935 cp_parser_skip_to_end_of_statement (parser);
8936 return error_mark_node;
8939 /* Begin the selection-statement. */
8940 if (keyword == RID_IF)
8941 statement = begin_if_stmt ();
8943 statement = begin_switch_stmt ();
8945 /* Parse the condition. */
8946 condition = cp_parser_condition (parser);
8947 /* Look for the `)'. */
8948 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8949 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8950 /*consume_paren=*/true);
8952 if (keyword == RID_IF)
8955 unsigned char in_statement;
8957 /* Add the condition. */
8958 finish_if_stmt_cond (condition, statement);
8960 /* Parse the then-clause. */
8961 in_statement = parser->in_statement;
8962 parser->in_statement |= IN_IF_STMT;
8963 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8965 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8966 add_stmt (build_empty_stmt (loc));
8967 cp_lexer_consume_token (parser->lexer);
8968 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8969 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8970 "empty body in an %<if%> statement");
8974 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8975 parser->in_statement = in_statement;
8977 finish_then_clause (statement);
8979 /* If the next token is `else', parse the else-clause. */
8980 if (cp_lexer_next_token_is_keyword (parser->lexer,
8983 /* Consume the `else' keyword. */
8984 cp_lexer_consume_token (parser->lexer);
8985 begin_else_clause (statement);
8986 /* Parse the else-clause. */
8987 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8990 loc = cp_lexer_peek_token (parser->lexer)->location;
8992 OPT_Wempty_body, "suggest braces around "
8993 "empty body in an %<else%> statement");
8994 add_stmt (build_empty_stmt (loc));
8995 cp_lexer_consume_token (parser->lexer);
8998 cp_parser_implicitly_scoped_statement (parser, NULL);
9000 finish_else_clause (statement);
9002 /* If we are currently parsing a then-clause, then
9003 IF_P will not be NULL. We set it to true to
9004 indicate that this if statement has an else clause.
9005 This may trigger the Wparentheses warning below
9006 when we get back up to the parent if statement. */
9012 /* This if statement does not have an else clause. If
9013 NESTED_IF is true, then the then-clause is an if
9014 statement which does have an else clause. We warn
9015 about the potential ambiguity. */
9017 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9018 "suggest explicit braces to avoid ambiguous"
9022 /* Now we're all done with the if-statement. */
9023 finish_if_stmt (statement);
9027 bool in_switch_statement_p;
9028 unsigned char in_statement;
9030 /* Add the condition. */
9031 finish_switch_cond (condition, statement);
9033 /* Parse the body of the switch-statement. */
9034 in_switch_statement_p = parser->in_switch_statement_p;
9035 in_statement = parser->in_statement;
9036 parser->in_switch_statement_p = true;
9037 parser->in_statement |= IN_SWITCH_STMT;
9038 cp_parser_implicitly_scoped_statement (parser, NULL);
9039 parser->in_switch_statement_p = in_switch_statement_p;
9040 parser->in_statement = in_statement;
9042 /* Now we're all done with the switch-statement. */
9043 finish_switch_stmt (statement);
9051 cp_parser_error (parser, "expected selection-statement");
9052 return error_mark_node;
9056 /* Parse a condition.
9060 type-specifier-seq declarator = initializer-clause
9061 type-specifier-seq declarator braced-init-list
9066 type-specifier-seq declarator asm-specification [opt]
9067 attributes [opt] = assignment-expression
9069 Returns the expression that should be tested. */
9072 cp_parser_condition (cp_parser* parser)
9074 cp_decl_specifier_seq type_specifiers;
9075 const char *saved_message;
9076 int declares_class_or_enum;
9078 /* Try the declaration first. */
9079 cp_parser_parse_tentatively (parser);
9080 /* New types are not allowed in the type-specifier-seq for a
9082 saved_message = parser->type_definition_forbidden_message;
9083 parser->type_definition_forbidden_message
9084 = G_("types may not be defined in conditions");
9085 /* Parse the type-specifier-seq. */
9086 cp_parser_decl_specifier_seq (parser,
9087 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9089 &declares_class_or_enum);
9090 /* Restore the saved message. */
9091 parser->type_definition_forbidden_message = saved_message;
9092 /* If all is well, we might be looking at a declaration. */
9093 if (!cp_parser_error_occurred (parser))
9096 tree asm_specification;
9098 cp_declarator *declarator;
9099 tree initializer = NULL_TREE;
9101 /* Parse the declarator. */
9102 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9103 /*ctor_dtor_or_conv_p=*/NULL,
9104 /*parenthesized_p=*/NULL,
9105 /*member_p=*/false);
9106 /* Parse the attributes. */
9107 attributes = cp_parser_attributes_opt (parser);
9108 /* Parse the asm-specification. */
9109 asm_specification = cp_parser_asm_specification_opt (parser);
9110 /* If the next token is not an `=' or '{', then we might still be
9111 looking at an expression. For example:
9115 looks like a decl-specifier-seq and a declarator -- but then
9116 there is no `=', so this is an expression. */
9117 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9118 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9119 cp_parser_simulate_error (parser);
9121 /* If we did see an `=' or '{', then we are looking at a declaration
9123 if (cp_parser_parse_definitely (parser))
9126 bool non_constant_p;
9127 bool flags = LOOKUP_ONLYCONVERTING;
9129 /* Create the declaration. */
9130 decl = start_decl (declarator, &type_specifiers,
9131 /*initialized_p=*/true,
9132 attributes, /*prefix_attributes=*/NULL_TREE,
9135 /* Parse the initializer. */
9136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9138 initializer = cp_parser_braced_list (parser, &non_constant_p);
9139 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9144 /* Consume the `='. */
9145 cp_parser_require (parser, CPP_EQ, RT_EQ);
9146 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9148 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9149 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9151 /* Process the initializer. */
9152 cp_finish_decl (decl,
9153 initializer, !non_constant_p,
9158 pop_scope (pushed_scope);
9160 return convert_from_reference (decl);
9163 /* If we didn't even get past the declarator successfully, we are
9164 definitely not looking at a declaration. */
9166 cp_parser_abort_tentative_parse (parser);
9168 /* Otherwise, we are looking at an expression. */
9169 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9172 /* Parses a for-statement or range-for-statement until the closing ')',
9176 cp_parser_for (cp_parser *parser)
9178 tree init, scope, decl;
9181 /* Begin the for-statement. */
9182 scope = begin_for_scope (&init);
9184 /* Parse the initialization. */
9185 is_range_for = cp_parser_for_init_statement (parser, &decl);
9188 return cp_parser_range_for (parser, scope, init, decl);
9190 return cp_parser_c_for (parser, scope, init);
9194 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9196 /* Normal for loop */
9197 tree condition = NULL_TREE;
9198 tree expression = NULL_TREE;
9201 stmt = begin_for_stmt (scope, init);
9202 /* The for-init-statement has already been parsed in
9203 cp_parser_for_init_statement, so no work is needed here. */
9204 finish_for_init_stmt (stmt);
9206 /* If there's a condition, process it. */
9207 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9208 condition = cp_parser_condition (parser);
9209 finish_for_cond (condition, stmt);
9210 /* Look for the `;'. */
9211 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9213 /* If there's an expression, process it. */
9214 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9215 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9216 finish_for_expr (expression, stmt);
9221 /* Tries to parse a range-based for-statement:
9224 decl-specifier-seq declarator : expression
9226 The decl-specifier-seq declarator and the `:' are already parsed by
9227 cp_parser_for_init_statement. If processing_template_decl it returns a
9228 newly created RANGE_FOR_STMT; if not, it is converted to a
9229 regular FOR_STMT. */
9232 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9234 tree stmt, range_expr;
9236 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9238 bool expr_non_constant_p;
9239 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9242 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9244 /* If in template, STMT is converted to a normal for-statement
9245 at instantiation. If not, it is done just ahead. */
9246 if (processing_template_decl)
9248 stmt = begin_range_for_stmt (scope, init);
9249 finish_range_for_decl (stmt, range_decl, range_expr);
9250 if (!type_dependent_expression_p (range_expr)
9251 /* do_auto_deduction doesn't mess with template init-lists. */
9252 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9253 do_range_for_auto_deduction (range_decl, range_expr);
9257 stmt = begin_for_stmt (scope, init);
9258 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9263 /* Subroutine of cp_convert_range_for: given the initializer expression,
9264 builds up the range temporary. */
9267 build_range_temp (tree range_expr)
9269 tree range_type, range_temp;
9271 /* Find out the type deduced by the declaration
9272 `auto &&__range = range_expr'. */
9273 range_type = cp_build_reference_type (make_auto (), true);
9274 range_type = do_auto_deduction (range_type, range_expr,
9275 type_uses_auto (range_type));
9277 /* Create the __range variable. */
9278 range_temp = build_decl (input_location, VAR_DECL,
9279 get_identifier ("__for_range"), range_type);
9280 TREE_USED (range_temp) = 1;
9281 DECL_ARTIFICIAL (range_temp) = 1;
9286 /* Used by cp_parser_range_for in template context: we aren't going to
9287 do a full conversion yet, but we still need to resolve auto in the
9288 type of the for-range-declaration if present. This is basically
9289 a shortcut version of cp_convert_range_for. */
9292 do_range_for_auto_deduction (tree decl, tree range_expr)
9294 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9297 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9298 range_temp = convert_from_reference (build_range_temp (range_expr));
9299 iter_type = (cp_parser_perform_range_for_lookup
9300 (range_temp, &begin_dummy, &end_dummy));
9301 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9302 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9303 tf_warning_or_error);
9304 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9305 iter_decl, auto_node);
9309 /* Converts a range-based for-statement into a normal
9310 for-statement, as per the definition.
9312 for (RANGE_DECL : RANGE_EXPR)
9315 should be equivalent to:
9318 auto &&__range = RANGE_EXPR;
9319 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9323 RANGE_DECL = *__begin;
9328 If RANGE_EXPR is an array:
9329 BEGIN_EXPR = __range
9330 END_EXPR = __range + ARRAY_SIZE(__range)
9331 Else if RANGE_EXPR has a member 'begin' or 'end':
9332 BEGIN_EXPR = __range.begin()
9333 END_EXPR = __range.end()
9335 BEGIN_EXPR = begin(__range)
9336 END_EXPR = end(__range);
9338 If __range has a member 'begin' but not 'end', or vice versa, we must
9339 still use the second alternative (it will surely fail, however).
9340 When calling begin()/end() in the third alternative we must use
9341 argument dependent lookup, but always considering 'std' as an associated
9345 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9348 tree iter_type, begin_expr, end_expr;
9349 tree condition, expression;
9351 if (range_decl == error_mark_node || range_expr == error_mark_node)
9352 /* If an error happened previously do nothing or else a lot of
9353 unhelpful errors would be issued. */
9354 begin_expr = end_expr = iter_type = error_mark_node;
9357 tree range_temp = build_range_temp (range_expr);
9358 pushdecl (range_temp);
9359 cp_finish_decl (range_temp, range_expr,
9360 /*is_constant_init*/false, NULL_TREE,
9361 LOOKUP_ONLYCONVERTING);
9363 range_temp = convert_from_reference (range_temp);
9364 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9365 &begin_expr, &end_expr);
9368 /* The new for initialization statement. */
9369 begin = build_decl (input_location, VAR_DECL,
9370 get_identifier ("__for_begin"), iter_type);
9371 TREE_USED (begin) = 1;
9372 DECL_ARTIFICIAL (begin) = 1;
9374 cp_finish_decl (begin, begin_expr,
9375 /*is_constant_init*/false, NULL_TREE,
9376 LOOKUP_ONLYCONVERTING);
9378 end = build_decl (input_location, VAR_DECL,
9379 get_identifier ("__for_end"), iter_type);
9380 TREE_USED (end) = 1;
9381 DECL_ARTIFICIAL (end) = 1;
9383 cp_finish_decl (end, end_expr,
9384 /*is_constant_init*/false, NULL_TREE,
9385 LOOKUP_ONLYCONVERTING);
9387 finish_for_init_stmt (statement);
9389 /* The new for condition. */
9390 condition = build_x_binary_op (NE_EXPR,
9393 NULL, tf_warning_or_error);
9394 finish_for_cond (condition, statement);
9396 /* The new increment expression. */
9397 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9398 finish_for_expr (expression, statement);
9400 /* The declaration is initialized with *__begin inside the loop body. */
9401 cp_finish_decl (range_decl,
9402 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9403 /*is_constant_init*/false, NULL_TREE,
9404 LOOKUP_ONLYCONVERTING);
9409 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9410 We need to solve both at the same time because the method used
9411 depends on the existence of members begin or end.
9412 Returns the type deduced for the iterator expression. */
9415 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9417 if (error_operand_p (range))
9419 *begin = *end = error_mark_node;
9420 return error_mark_node;
9423 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9425 error ("range-based %<for%> expression of type %qT "
9426 "has incomplete type", TREE_TYPE (range));
9427 *begin = *end = error_mark_node;
9428 return error_mark_node;
9430 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9432 /* If RANGE is an array, we will use pointer arithmetic. */
9434 *end = build_binary_op (input_location, PLUS_EXPR,
9436 array_type_nelts_top (TREE_TYPE (range)),
9438 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9442 /* If it is not an array, we must do a bit of magic. */
9443 tree id_begin, id_end;
9444 tree member_begin, member_end;
9446 *begin = *end = error_mark_node;
9448 id_begin = get_identifier ("begin");
9449 id_end = get_identifier ("end");
9450 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9451 /*protect=*/2, /*want_type=*/false);
9452 member_end = lookup_member (TREE_TYPE (range), id_end,
9453 /*protect=*/2, /*want_type=*/false);
9455 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9457 /* Use the member functions. */
9458 if (member_begin != NULL_TREE)
9459 *begin = cp_parser_range_for_member_function (range, id_begin);
9461 error ("range-based %<for%> expression of type %qT has an "
9462 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9464 if (member_end != NULL_TREE)
9465 *end = cp_parser_range_for_member_function (range, id_end);
9467 error ("range-based %<for%> expression of type %qT has a "
9468 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9472 /* Use global functions with ADL. */
9474 vec = make_tree_vector ();
9476 VEC_safe_push (tree, gc, vec, range);
9478 member_begin = perform_koenig_lookup (id_begin, vec,
9479 /*include_std=*/true,
9480 tf_warning_or_error);
9481 *begin = finish_call_expr (member_begin, &vec, false, true,
9482 tf_warning_or_error);
9483 member_end = perform_koenig_lookup (id_end, vec,
9484 /*include_std=*/true,
9485 tf_warning_or_error);
9486 *end = finish_call_expr (member_end, &vec, false, true,
9487 tf_warning_or_error);
9489 release_tree_vector (vec);
9492 /* Last common checks. */
9493 if (*begin == error_mark_node || *end == error_mark_node)
9495 /* If one of the expressions is an error do no more checks. */
9496 *begin = *end = error_mark_node;
9497 return error_mark_node;
9501 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9502 /* The unqualified type of the __begin and __end temporaries should
9503 be the same, as required by the multiple auto declaration. */
9504 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9505 error ("inconsistent begin/end types in range-based %<for%> "
9506 "statement: %qT and %qT",
9507 TREE_TYPE (*begin), TREE_TYPE (*end));
9513 /* Helper function for cp_parser_perform_range_for_lookup.
9514 Builds a tree for RANGE.IDENTIFIER(). */
9517 cp_parser_range_for_member_function (tree range, tree identifier)
9522 member = finish_class_member_access_expr (range, identifier,
9523 false, tf_warning_or_error);
9524 if (member == error_mark_node)
9525 return error_mark_node;
9527 vec = make_tree_vector ();
9528 res = finish_call_expr (member, &vec,
9529 /*disallow_virtual=*/false,
9531 tf_warning_or_error);
9532 release_tree_vector (vec);
9536 /* Parse an iteration-statement.
9538 iteration-statement:
9539 while ( condition ) statement
9540 do statement while ( expression ) ;
9541 for ( for-init-statement condition [opt] ; expression [opt] )
9544 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9547 cp_parser_iteration_statement (cp_parser* parser)
9552 unsigned char in_statement;
9554 /* Peek at the next token. */
9555 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9557 return error_mark_node;
9559 /* Remember whether or not we are already within an iteration
9561 in_statement = parser->in_statement;
9563 /* See what kind of keyword it is. */
9564 keyword = token->keyword;
9571 /* Begin the while-statement. */
9572 statement = begin_while_stmt ();
9573 /* Look for the `('. */
9574 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9575 /* Parse the condition. */
9576 condition = cp_parser_condition (parser);
9577 finish_while_stmt_cond (condition, statement);
9578 /* Look for the `)'. */
9579 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9580 /* Parse the dependent statement. */
9581 parser->in_statement = IN_ITERATION_STMT;
9582 cp_parser_already_scoped_statement (parser);
9583 parser->in_statement = in_statement;
9584 /* We're done with the while-statement. */
9585 finish_while_stmt (statement);
9593 /* Begin the do-statement. */
9594 statement = begin_do_stmt ();
9595 /* Parse the body of the do-statement. */
9596 parser->in_statement = IN_ITERATION_STMT;
9597 cp_parser_implicitly_scoped_statement (parser, NULL);
9598 parser->in_statement = in_statement;
9599 finish_do_body (statement);
9600 /* Look for the `while' keyword. */
9601 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9602 /* Look for the `('. */
9603 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9604 /* Parse the expression. */
9605 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9606 /* We're done with the do-statement. */
9607 finish_do_stmt (expression, statement);
9608 /* Look for the `)'. */
9609 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9610 /* Look for the `;'. */
9611 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9617 /* Look for the `('. */
9618 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9620 statement = cp_parser_for (parser);
9622 /* Look for the `)'. */
9623 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9625 /* Parse the body of the for-statement. */
9626 parser->in_statement = IN_ITERATION_STMT;
9627 cp_parser_already_scoped_statement (parser);
9628 parser->in_statement = in_statement;
9630 /* We're done with the for-statement. */
9631 finish_for_stmt (statement);
9636 cp_parser_error (parser, "expected iteration-statement");
9637 statement = error_mark_node;
9644 /* Parse a for-init-statement or the declarator of a range-based-for.
9645 Returns true if a range-based-for declaration is seen.
9648 expression-statement
9649 simple-declaration */
9652 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9654 /* If the next token is a `;', then we have an empty
9655 expression-statement. Grammatically, this is also a
9656 simple-declaration, but an invalid one, because it does not
9657 declare anything. Therefore, if we did not handle this case
9658 specially, we would issue an error message about an invalid
9660 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9662 bool is_range_for = false;
9663 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9665 parser->colon_corrects_to_scope_p = false;
9667 /* We're going to speculatively look for a declaration, falling back
9668 to an expression, if necessary. */
9669 cp_parser_parse_tentatively (parser);
9670 /* Parse the declaration. */
9671 cp_parser_simple_declaration (parser,
9672 /*function_definition_allowed_p=*/false,
9674 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9675 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9677 /* It is a range-for, consume the ':' */
9678 cp_lexer_consume_token (parser->lexer);
9679 is_range_for = true;
9680 if (cxx_dialect < cxx0x)
9682 error_at (cp_lexer_peek_token (parser->lexer)->location,
9683 "range-based %<for%> loops are not allowed "
9685 *decl = error_mark_node;
9689 /* The ';' is not consumed yet because we told
9690 cp_parser_simple_declaration not to. */
9691 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9693 if (cp_parser_parse_definitely (parser))
9694 return is_range_for;
9695 /* If the tentative parse failed, then we shall need to look for an
9696 expression-statement. */
9698 /* If we are here, it is an expression-statement. */
9699 cp_parser_expression_statement (parser, NULL_TREE);
9703 /* Parse a jump-statement.
9708 return expression [opt] ;
9709 return braced-init-list ;
9717 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9720 cp_parser_jump_statement (cp_parser* parser)
9722 tree statement = error_mark_node;
9725 unsigned char in_statement;
9727 /* Peek at the next token. */
9728 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9730 return error_mark_node;
9732 /* See what kind of keyword it is. */
9733 keyword = token->keyword;
9737 in_statement = parser->in_statement & ~IN_IF_STMT;
9738 switch (in_statement)
9741 error_at (token->location, "break statement not within loop or switch");
9744 gcc_assert ((in_statement & IN_SWITCH_STMT)
9745 || in_statement == IN_ITERATION_STMT);
9746 statement = finish_break_stmt ();
9749 error_at (token->location, "invalid exit from OpenMP structured block");
9752 error_at (token->location, "break statement used with OpenMP for loop");
9755 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9759 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9762 error_at (token->location, "continue statement not within a loop");
9764 case IN_ITERATION_STMT:
9766 statement = finish_continue_stmt ();
9769 error_at (token->location, "invalid exit from OpenMP structured block");
9774 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9780 bool expr_non_constant_p;
9782 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9784 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9785 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9787 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9788 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9790 /* If the next token is a `;', then there is no
9793 /* Build the return-statement. */
9794 statement = finish_return_stmt (expr);
9795 /* Look for the final `;'. */
9796 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9801 /* Create the goto-statement. */
9802 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9804 /* Issue a warning about this use of a GNU extension. */
9805 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9806 /* Consume the '*' token. */
9807 cp_lexer_consume_token (parser->lexer);
9808 /* Parse the dependent expression. */
9809 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9812 finish_goto_stmt (cp_parser_identifier (parser));
9813 /* Look for the final `;'. */
9814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9818 cp_parser_error (parser, "expected jump-statement");
9825 /* Parse a declaration-statement.
9827 declaration-statement:
9828 block-declaration */
9831 cp_parser_declaration_statement (cp_parser* parser)
9835 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9836 p = obstack_alloc (&declarator_obstack, 0);
9838 /* Parse the block-declaration. */
9839 cp_parser_block_declaration (parser, /*statement_p=*/true);
9841 /* Free any declarators allocated. */
9842 obstack_free (&declarator_obstack, p);
9844 /* Finish off the statement. */
9848 /* Some dependent statements (like `if (cond) statement'), are
9849 implicitly in their own scope. In other words, if the statement is
9850 a single statement (as opposed to a compound-statement), it is
9851 none-the-less treated as if it were enclosed in braces. Any
9852 declarations appearing in the dependent statement are out of scope
9853 after control passes that point. This function parses a statement,
9854 but ensures that is in its own scope, even if it is not a
9857 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9858 is a (possibly labeled) if statement which is not enclosed in
9859 braces and has an else clause. This is used to implement
9862 Returns the new statement. */
9865 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9872 /* Mark if () ; with a special NOP_EXPR. */
9873 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9875 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9876 cp_lexer_consume_token (parser->lexer);
9877 statement = add_stmt (build_empty_stmt (loc));
9879 /* if a compound is opened, we simply parse the statement directly. */
9880 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9881 statement = cp_parser_compound_statement (parser, NULL, false, false);
9882 /* If the token is not a `{', then we must take special action. */
9885 /* Create a compound-statement. */
9886 statement = begin_compound_stmt (0);
9887 /* Parse the dependent-statement. */
9888 cp_parser_statement (parser, NULL_TREE, false, if_p);
9889 /* Finish the dummy compound-statement. */
9890 finish_compound_stmt (statement);
9893 /* Return the statement. */
9897 /* For some dependent statements (like `while (cond) statement'), we
9898 have already created a scope. Therefore, even if the dependent
9899 statement is a compound-statement, we do not want to create another
9903 cp_parser_already_scoped_statement (cp_parser* parser)
9905 /* If the token is a `{', then we must take special action. */
9906 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9907 cp_parser_statement (parser, NULL_TREE, false, NULL);
9910 /* Avoid calling cp_parser_compound_statement, so that we
9911 don't create a new scope. Do everything else by hand. */
9912 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9913 /* If the next keyword is `__label__' we have a label declaration. */
9914 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9915 cp_parser_label_declaration (parser);
9916 /* Parse an (optional) statement-seq. */
9917 cp_parser_statement_seq_opt (parser, NULL_TREE);
9918 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9922 /* Declarations [gram.dcl.dcl] */
9924 /* Parse an optional declaration-sequence.
9928 declaration-seq declaration */
9931 cp_parser_declaration_seq_opt (cp_parser* parser)
9937 token = cp_lexer_peek_token (parser->lexer);
9939 if (token->type == CPP_CLOSE_BRACE
9940 || token->type == CPP_EOF
9941 || token->type == CPP_PRAGMA_EOL)
9944 if (token->type == CPP_SEMICOLON)
9946 /* A declaration consisting of a single semicolon is
9947 invalid. Allow it unless we're being pedantic. */
9948 cp_lexer_consume_token (parser->lexer);
9949 if (!in_system_header)
9950 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9954 /* If we're entering or exiting a region that's implicitly
9955 extern "C", modify the lang context appropriately. */
9956 if (!parser->implicit_extern_c && token->implicit_extern_c)
9958 push_lang_context (lang_name_c);
9959 parser->implicit_extern_c = true;
9961 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9963 pop_lang_context ();
9964 parser->implicit_extern_c = false;
9967 if (token->type == CPP_PRAGMA)
9969 /* A top-level declaration can consist solely of a #pragma.
9970 A nested declaration cannot, so this is done here and not
9971 in cp_parser_declaration. (A #pragma at block scope is
9972 handled in cp_parser_statement.) */
9973 cp_parser_pragma (parser, pragma_external);
9977 /* Parse the declaration itself. */
9978 cp_parser_declaration (parser);
9982 /* Parse a declaration.
9987 template-declaration
9988 explicit-instantiation
9989 explicit-specialization
9990 linkage-specification
9991 namespace-definition
9996 __extension__ declaration */
9999 cp_parser_declaration (cp_parser* parser)
10003 int saved_pedantic;
10005 tree attributes = NULL_TREE;
10007 /* Check for the `__extension__' keyword. */
10008 if (cp_parser_extension_opt (parser, &saved_pedantic))
10010 /* Parse the qualified declaration. */
10011 cp_parser_declaration (parser);
10012 /* Restore the PEDANTIC flag. */
10013 pedantic = saved_pedantic;
10018 /* Try to figure out what kind of declaration is present. */
10019 token1 = *cp_lexer_peek_token (parser->lexer);
10021 if (token1.type != CPP_EOF)
10022 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10025 token2.type = CPP_EOF;
10026 token2.keyword = RID_MAX;
10029 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10030 p = obstack_alloc (&declarator_obstack, 0);
10032 /* If the next token is `extern' and the following token is a string
10033 literal, then we have a linkage specification. */
10034 if (token1.keyword == RID_EXTERN
10035 && cp_parser_is_pure_string_literal (&token2))
10036 cp_parser_linkage_specification (parser);
10037 /* If the next token is `template', then we have either a template
10038 declaration, an explicit instantiation, or an explicit
10040 else if (token1.keyword == RID_TEMPLATE)
10042 /* `template <>' indicates a template specialization. */
10043 if (token2.type == CPP_LESS
10044 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10045 cp_parser_explicit_specialization (parser);
10046 /* `template <' indicates a template declaration. */
10047 else if (token2.type == CPP_LESS)
10048 cp_parser_template_declaration (parser, /*member_p=*/false);
10049 /* Anything else must be an explicit instantiation. */
10051 cp_parser_explicit_instantiation (parser);
10053 /* If the next token is `export', then we have a template
10055 else if (token1.keyword == RID_EXPORT)
10056 cp_parser_template_declaration (parser, /*member_p=*/false);
10057 /* If the next token is `extern', 'static' or 'inline' and the one
10058 after that is `template', we have a GNU extended explicit
10059 instantiation directive. */
10060 else if (cp_parser_allow_gnu_extensions_p (parser)
10061 && (token1.keyword == RID_EXTERN
10062 || token1.keyword == RID_STATIC
10063 || token1.keyword == RID_INLINE)
10064 && token2.keyword == RID_TEMPLATE)
10065 cp_parser_explicit_instantiation (parser);
10066 /* If the next token is `namespace', check for a named or unnamed
10067 namespace definition. */
10068 else if (token1.keyword == RID_NAMESPACE
10069 && (/* A named namespace definition. */
10070 (token2.type == CPP_NAME
10071 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10073 /* An unnamed namespace definition. */
10074 || token2.type == CPP_OPEN_BRACE
10075 || token2.keyword == RID_ATTRIBUTE))
10076 cp_parser_namespace_definition (parser);
10077 /* An inline (associated) namespace definition. */
10078 else if (token1.keyword == RID_INLINE
10079 && token2.keyword == RID_NAMESPACE)
10080 cp_parser_namespace_definition (parser);
10081 /* Objective-C++ declaration/definition. */
10082 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10083 cp_parser_objc_declaration (parser, NULL_TREE);
10084 else if (c_dialect_objc ()
10085 && token1.keyword == RID_ATTRIBUTE
10086 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10087 cp_parser_objc_declaration (parser, attributes);
10088 /* We must have either a block declaration or a function
10091 /* Try to parse a block-declaration, or a function-definition. */
10092 cp_parser_block_declaration (parser, /*statement_p=*/false);
10094 /* Free any declarators allocated. */
10095 obstack_free (&declarator_obstack, p);
10098 /* Parse a block-declaration.
10103 namespace-alias-definition
10110 __extension__ block-declaration
10115 static_assert-declaration
10117 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10118 part of a declaration-statement. */
10121 cp_parser_block_declaration (cp_parser *parser,
10125 int saved_pedantic;
10127 /* Check for the `__extension__' keyword. */
10128 if (cp_parser_extension_opt (parser, &saved_pedantic))
10130 /* Parse the qualified declaration. */
10131 cp_parser_block_declaration (parser, statement_p);
10132 /* Restore the PEDANTIC flag. */
10133 pedantic = saved_pedantic;
10138 /* Peek at the next token to figure out which kind of declaration is
10140 token1 = cp_lexer_peek_token (parser->lexer);
10142 /* If the next keyword is `asm', we have an asm-definition. */
10143 if (token1->keyword == RID_ASM)
10146 cp_parser_commit_to_tentative_parse (parser);
10147 cp_parser_asm_definition (parser);
10149 /* If the next keyword is `namespace', we have a
10150 namespace-alias-definition. */
10151 else if (token1->keyword == RID_NAMESPACE)
10152 cp_parser_namespace_alias_definition (parser);
10153 /* If the next keyword is `using', we have either a
10154 using-declaration or a using-directive. */
10155 else if (token1->keyword == RID_USING)
10160 cp_parser_commit_to_tentative_parse (parser);
10161 /* If the token after `using' is `namespace', then we have a
10162 using-directive. */
10163 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10164 if (token2->keyword == RID_NAMESPACE)
10165 cp_parser_using_directive (parser);
10166 /* Otherwise, it's a using-declaration. */
10168 cp_parser_using_declaration (parser,
10169 /*access_declaration_p=*/false);
10171 /* If the next keyword is `__label__' we have a misplaced label
10173 else if (token1->keyword == RID_LABEL)
10175 cp_lexer_consume_token (parser->lexer);
10176 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10177 cp_parser_skip_to_end_of_statement (parser);
10178 /* If the next token is now a `;', consume it. */
10179 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10180 cp_lexer_consume_token (parser->lexer);
10182 /* If the next token is `static_assert' we have a static assertion. */
10183 else if (token1->keyword == RID_STATIC_ASSERT)
10184 cp_parser_static_assert (parser, /*member_p=*/false);
10185 /* Anything else must be a simple-declaration. */
10187 cp_parser_simple_declaration (parser, !statement_p,
10188 /*maybe_range_for_decl*/NULL);
10191 /* Parse a simple-declaration.
10193 simple-declaration:
10194 decl-specifier-seq [opt] init-declarator-list [opt] ;
10196 init-declarator-list:
10198 init-declarator-list , init-declarator
10200 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10201 function-definition as a simple-declaration.
10203 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10204 parsed declaration if it is an uninitialized single declarator not followed
10205 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10206 if present, will not be consumed. */
10209 cp_parser_simple_declaration (cp_parser* parser,
10210 bool function_definition_allowed_p,
10211 tree *maybe_range_for_decl)
10213 cp_decl_specifier_seq decl_specifiers;
10214 int declares_class_or_enum;
10215 bool saw_declarator;
10217 if (maybe_range_for_decl)
10218 *maybe_range_for_decl = NULL_TREE;
10220 /* Defer access checks until we know what is being declared; the
10221 checks for names appearing in the decl-specifier-seq should be
10222 done as if we were in the scope of the thing being declared. */
10223 push_deferring_access_checks (dk_deferred);
10225 /* Parse the decl-specifier-seq. We have to keep track of whether
10226 or not the decl-specifier-seq declares a named class or
10227 enumeration type, since that is the only case in which the
10228 init-declarator-list is allowed to be empty.
10232 In a simple-declaration, the optional init-declarator-list can be
10233 omitted only when declaring a class or enumeration, that is when
10234 the decl-specifier-seq contains either a class-specifier, an
10235 elaborated-type-specifier, or an enum-specifier. */
10236 cp_parser_decl_specifier_seq (parser,
10237 CP_PARSER_FLAGS_OPTIONAL,
10239 &declares_class_or_enum);
10240 /* We no longer need to defer access checks. */
10241 stop_deferring_access_checks ();
10243 /* In a block scope, a valid declaration must always have a
10244 decl-specifier-seq. By not trying to parse declarators, we can
10245 resolve the declaration/expression ambiguity more quickly. */
10246 if (!function_definition_allowed_p
10247 && !decl_specifiers.any_specifiers_p)
10249 cp_parser_error (parser, "expected declaration");
10253 /* If the next two tokens are both identifiers, the code is
10254 erroneous. The usual cause of this situation is code like:
10258 where "T" should name a type -- but does not. */
10259 if (!decl_specifiers.any_type_specifiers_p
10260 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10262 /* If parsing tentatively, we should commit; we really are
10263 looking at a declaration. */
10264 cp_parser_commit_to_tentative_parse (parser);
10269 /* If we have seen at least one decl-specifier, and the next token
10270 is not a parenthesis, then we must be looking at a declaration.
10271 (After "int (" we might be looking at a functional cast.) */
10272 if (decl_specifiers.any_specifiers_p
10273 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10274 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10275 && !cp_parser_error_occurred (parser))
10276 cp_parser_commit_to_tentative_parse (parser);
10278 /* Keep going until we hit the `;' at the end of the simple
10280 saw_declarator = false;
10281 while (cp_lexer_next_token_is_not (parser->lexer,
10285 bool function_definition_p;
10288 if (saw_declarator)
10290 /* If we are processing next declarator, coma is expected */
10291 token = cp_lexer_peek_token (parser->lexer);
10292 gcc_assert (token->type == CPP_COMMA);
10293 cp_lexer_consume_token (parser->lexer);
10294 if (maybe_range_for_decl)
10295 *maybe_range_for_decl = error_mark_node;
10298 saw_declarator = true;
10300 /* Parse the init-declarator. */
10301 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10303 function_definition_allowed_p,
10304 /*member_p=*/false,
10305 declares_class_or_enum,
10306 &function_definition_p,
10307 maybe_range_for_decl);
10308 /* If an error occurred while parsing tentatively, exit quickly.
10309 (That usually happens when in the body of a function; each
10310 statement is treated as a declaration-statement until proven
10312 if (cp_parser_error_occurred (parser))
10314 /* Handle function definitions specially. */
10315 if (function_definition_p)
10317 /* If the next token is a `,', then we are probably
10318 processing something like:
10322 which is erroneous. */
10323 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10325 cp_token *token = cp_lexer_peek_token (parser->lexer);
10326 error_at (token->location,
10328 " declarations and function-definitions is forbidden");
10330 /* Otherwise, we're done with the list of declarators. */
10333 pop_deferring_access_checks ();
10337 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10338 *maybe_range_for_decl = decl;
10339 /* The next token should be either a `,' or a `;'. */
10340 token = cp_lexer_peek_token (parser->lexer);
10341 /* If it's a `,', there are more declarators to come. */
10342 if (token->type == CPP_COMMA)
10343 /* will be consumed next time around */;
10344 /* If it's a `;', we are done. */
10345 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10347 /* Anything else is an error. */
10350 /* If we have already issued an error message we don't need
10351 to issue another one. */
10352 if (decl != error_mark_node
10353 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10354 cp_parser_error (parser, "expected %<,%> or %<;%>");
10355 /* Skip tokens until we reach the end of the statement. */
10356 cp_parser_skip_to_end_of_statement (parser);
10357 /* If the next token is now a `;', consume it. */
10358 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10359 cp_lexer_consume_token (parser->lexer);
10362 /* After the first time around, a function-definition is not
10363 allowed -- even if it was OK at first. For example:
10368 function_definition_allowed_p = false;
10371 /* Issue an error message if no declarators are present, and the
10372 decl-specifier-seq does not itself declare a class or
10374 if (!saw_declarator)
10376 if (cp_parser_declares_only_class_p (parser))
10377 shadow_tag (&decl_specifiers);
10378 /* Perform any deferred access checks. */
10379 perform_deferred_access_checks ();
10382 /* Consume the `;'. */
10383 if (!maybe_range_for_decl)
10384 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10387 pop_deferring_access_checks ();
10390 /* Parse a decl-specifier-seq.
10392 decl-specifier-seq:
10393 decl-specifier-seq [opt] decl-specifier
10396 storage-class-specifier
10407 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10409 The parser flags FLAGS is used to control type-specifier parsing.
10411 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10414 1: one of the decl-specifiers is an elaborated-type-specifier
10415 (i.e., a type declaration)
10416 2: one of the decl-specifiers is an enum-specifier or a
10417 class-specifier (i.e., a type definition)
10422 cp_parser_decl_specifier_seq (cp_parser* parser,
10423 cp_parser_flags flags,
10424 cp_decl_specifier_seq *decl_specs,
10425 int* declares_class_or_enum)
10427 bool constructor_possible_p = !parser->in_declarator_p;
10428 cp_token *start_token = NULL;
10430 /* Clear DECL_SPECS. */
10431 clear_decl_specs (decl_specs);
10433 /* Assume no class or enumeration type is declared. */
10434 *declares_class_or_enum = 0;
10436 /* Keep reading specifiers until there are no more to read. */
10439 bool constructor_p;
10440 bool found_decl_spec;
10443 /* Peek at the next token. */
10444 token = cp_lexer_peek_token (parser->lexer);
10446 /* Save the first token of the decl spec list for error
10449 start_token = token;
10450 /* Handle attributes. */
10451 if (token->keyword == RID_ATTRIBUTE)
10453 /* Parse the attributes. */
10454 decl_specs->attributes
10455 = chainon (decl_specs->attributes,
10456 cp_parser_attributes_opt (parser));
10459 /* Assume we will find a decl-specifier keyword. */
10460 found_decl_spec = true;
10461 /* If the next token is an appropriate keyword, we can simply
10462 add it to the list. */
10463 switch (token->keyword)
10469 if (!at_class_scope_p ())
10471 error_at (token->location, "%<friend%> used outside of class");
10472 cp_lexer_purge_token (parser->lexer);
10476 ++decl_specs->specs[(int) ds_friend];
10477 /* Consume the token. */
10478 cp_lexer_consume_token (parser->lexer);
10482 case RID_CONSTEXPR:
10483 ++decl_specs->specs[(int) ds_constexpr];
10484 cp_lexer_consume_token (parser->lexer);
10487 /* function-specifier:
10494 cp_parser_function_specifier_opt (parser, decl_specs);
10500 ++decl_specs->specs[(int) ds_typedef];
10501 /* Consume the token. */
10502 cp_lexer_consume_token (parser->lexer);
10503 /* A constructor declarator cannot appear in a typedef. */
10504 constructor_possible_p = false;
10505 /* The "typedef" keyword can only occur in a declaration; we
10506 may as well commit at this point. */
10507 cp_parser_commit_to_tentative_parse (parser);
10509 if (decl_specs->storage_class != sc_none)
10510 decl_specs->conflicting_specifiers_p = true;
10513 /* storage-class-specifier:
10523 if (cxx_dialect == cxx98)
10525 /* Consume the token. */
10526 cp_lexer_consume_token (parser->lexer);
10528 /* Complain about `auto' as a storage specifier, if
10529 we're complaining about C++0x compatibility. */
10530 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10531 " will change meaning in C++0x; please remove it");
10533 /* Set the storage class anyway. */
10534 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10538 /* C++0x auto type-specifier. */
10539 found_decl_spec = false;
10546 /* Consume the token. */
10547 cp_lexer_consume_token (parser->lexer);
10548 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10552 /* Consume the token. */
10553 cp_lexer_consume_token (parser->lexer);
10554 ++decl_specs->specs[(int) ds_thread];
10558 /* We did not yet find a decl-specifier yet. */
10559 found_decl_spec = false;
10563 if (found_decl_spec
10564 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10565 && token->keyword != RID_CONSTEXPR)
10566 error ("decl-specifier invalid in condition");
10568 /* Constructors are a special case. The `S' in `S()' is not a
10569 decl-specifier; it is the beginning of the declarator. */
10571 = (!found_decl_spec
10572 && constructor_possible_p
10573 && (cp_parser_constructor_declarator_p
10574 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10576 /* If we don't have a DECL_SPEC yet, then we must be looking at
10577 a type-specifier. */
10578 if (!found_decl_spec && !constructor_p)
10580 int decl_spec_declares_class_or_enum;
10581 bool is_cv_qualifier;
10585 = cp_parser_type_specifier (parser, flags,
10587 /*is_declaration=*/true,
10588 &decl_spec_declares_class_or_enum,
10590 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10592 /* If this type-specifier referenced a user-defined type
10593 (a typedef, class-name, etc.), then we can't allow any
10594 more such type-specifiers henceforth.
10598 The longest sequence of decl-specifiers that could
10599 possibly be a type name is taken as the
10600 decl-specifier-seq of a declaration. The sequence shall
10601 be self-consistent as described below.
10605 As a general rule, at most one type-specifier is allowed
10606 in the complete decl-specifier-seq of a declaration. The
10607 only exceptions are the following:
10609 -- const or volatile can be combined with any other
10612 -- signed or unsigned can be combined with char, long,
10620 void g (const int Pc);
10622 Here, Pc is *not* part of the decl-specifier seq; it's
10623 the declarator. Therefore, once we see a type-specifier
10624 (other than a cv-qualifier), we forbid any additional
10625 user-defined types. We *do* still allow things like `int
10626 int' to be considered a decl-specifier-seq, and issue the
10627 error message later. */
10628 if (type_spec && !is_cv_qualifier)
10629 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10630 /* A constructor declarator cannot follow a type-specifier. */
10633 constructor_possible_p = false;
10634 found_decl_spec = true;
10635 if (!is_cv_qualifier)
10636 decl_specs->any_type_specifiers_p = true;
10640 /* If we still do not have a DECL_SPEC, then there are no more
10641 decl-specifiers. */
10642 if (!found_decl_spec)
10645 decl_specs->any_specifiers_p = true;
10646 /* After we see one decl-specifier, further decl-specifiers are
10647 always optional. */
10648 flags |= CP_PARSER_FLAGS_OPTIONAL;
10651 cp_parser_check_decl_spec (decl_specs, start_token->location);
10653 /* Don't allow a friend specifier with a class definition. */
10654 if (decl_specs->specs[(int) ds_friend] != 0
10655 && (*declares_class_or_enum & 2))
10656 error_at (start_token->location,
10657 "class definition may not be declared a friend");
10660 /* Parse an (optional) storage-class-specifier.
10662 storage-class-specifier:
10671 storage-class-specifier:
10674 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10677 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10679 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10682 if (cxx_dialect != cxx98)
10684 /* Fall through for C++98. */
10691 /* Consume the token. */
10692 return cp_lexer_consume_token (parser->lexer)->u.value;
10699 /* Parse an (optional) function-specifier.
10701 function-specifier:
10706 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10707 Updates DECL_SPECS, if it is non-NULL. */
10710 cp_parser_function_specifier_opt (cp_parser* parser,
10711 cp_decl_specifier_seq *decl_specs)
10713 cp_token *token = cp_lexer_peek_token (parser->lexer);
10714 switch (token->keyword)
10718 ++decl_specs->specs[(int) ds_inline];
10722 /* 14.5.2.3 [temp.mem]
10724 A member function template shall not be virtual. */
10725 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10726 error_at (token->location, "templates may not be %<virtual%>");
10727 else if (decl_specs)
10728 ++decl_specs->specs[(int) ds_virtual];
10733 ++decl_specs->specs[(int) ds_explicit];
10740 /* Consume the token. */
10741 return cp_lexer_consume_token (parser->lexer)->u.value;
10744 /* Parse a linkage-specification.
10746 linkage-specification:
10747 extern string-literal { declaration-seq [opt] }
10748 extern string-literal declaration */
10751 cp_parser_linkage_specification (cp_parser* parser)
10755 /* Look for the `extern' keyword. */
10756 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10758 /* Look for the string-literal. */
10759 linkage = cp_parser_string_literal (parser, false, false);
10761 /* Transform the literal into an identifier. If the literal is a
10762 wide-character string, or contains embedded NULs, then we can't
10763 handle it as the user wants. */
10764 if (strlen (TREE_STRING_POINTER (linkage))
10765 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10767 cp_parser_error (parser, "invalid linkage-specification");
10768 /* Assume C++ linkage. */
10769 linkage = lang_name_cplusplus;
10772 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10774 /* We're now using the new linkage. */
10775 push_lang_context (linkage);
10777 /* If the next token is a `{', then we're using the first
10779 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10781 /* Consume the `{' token. */
10782 cp_lexer_consume_token (parser->lexer);
10783 /* Parse the declarations. */
10784 cp_parser_declaration_seq_opt (parser);
10785 /* Look for the closing `}'. */
10786 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10788 /* Otherwise, there's just one declaration. */
10791 bool saved_in_unbraced_linkage_specification_p;
10793 saved_in_unbraced_linkage_specification_p
10794 = parser->in_unbraced_linkage_specification_p;
10795 parser->in_unbraced_linkage_specification_p = true;
10796 cp_parser_declaration (parser);
10797 parser->in_unbraced_linkage_specification_p
10798 = saved_in_unbraced_linkage_specification_p;
10801 /* We're done with the linkage-specification. */
10802 pop_lang_context ();
10805 /* Parse a static_assert-declaration.
10807 static_assert-declaration:
10808 static_assert ( constant-expression , string-literal ) ;
10810 If MEMBER_P, this static_assert is a class member. */
10813 cp_parser_static_assert(cp_parser *parser, bool member_p)
10818 location_t saved_loc;
10821 /* Peek at the `static_assert' token so we can keep track of exactly
10822 where the static assertion started. */
10823 token = cp_lexer_peek_token (parser->lexer);
10824 saved_loc = token->location;
10826 /* Look for the `static_assert' keyword. */
10827 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10831 /* We know we are in a static assertion; commit to any tentative
10833 if (cp_parser_parsing_tentatively (parser))
10834 cp_parser_commit_to_tentative_parse (parser);
10836 /* Parse the `(' starting the static assertion condition. */
10837 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10839 /* Parse the constant-expression. Allow a non-constant expression
10840 here in order to give better diagnostics in finish_static_assert. */
10842 cp_parser_constant_expression (parser,
10843 /*allow_non_constant_p=*/true,
10844 /*non_constant_p=*/&dummy);
10846 /* Parse the separating `,'. */
10847 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10849 /* Parse the string-literal message. */
10850 message = cp_parser_string_literal (parser,
10851 /*translate=*/false,
10854 /* A `)' completes the static assertion. */
10855 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10856 cp_parser_skip_to_closing_parenthesis (parser,
10857 /*recovering=*/true,
10858 /*or_comma=*/false,
10859 /*consume_paren=*/true);
10861 /* A semicolon terminates the declaration. */
10862 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10864 /* Complete the static assertion, which may mean either processing
10865 the static assert now or saving it for template instantiation. */
10866 finish_static_assert (condition, message, saved_loc, member_p);
10869 /* Parse a `decltype' type. Returns the type.
10871 simple-type-specifier:
10872 decltype ( expression ) */
10875 cp_parser_decltype (cp_parser *parser)
10878 bool id_expression_or_member_access_p = false;
10879 const char *saved_message;
10880 bool saved_integral_constant_expression_p;
10881 bool saved_non_integral_constant_expression_p;
10882 cp_token *id_expr_start_token;
10883 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10885 if (start_token->type == CPP_DECLTYPE)
10887 /* Already parsed. */
10888 cp_lexer_consume_token (parser->lexer);
10889 return start_token->u.value;
10892 /* Look for the `decltype' token. */
10893 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10894 return error_mark_node;
10896 /* Types cannot be defined in a `decltype' expression. Save away the
10898 saved_message = parser->type_definition_forbidden_message;
10900 /* And create the new one. */
10901 parser->type_definition_forbidden_message
10902 = G_("types may not be defined in %<decltype%> expressions");
10904 /* The restrictions on constant-expressions do not apply inside
10905 decltype expressions. */
10906 saved_integral_constant_expression_p
10907 = parser->integral_constant_expression_p;
10908 saved_non_integral_constant_expression_p
10909 = parser->non_integral_constant_expression_p;
10910 parser->integral_constant_expression_p = false;
10912 /* Do not actually evaluate the expression. */
10913 ++cp_unevaluated_operand;
10915 /* Do not warn about problems with the expression. */
10916 ++c_inhibit_evaluation_warnings;
10918 /* Parse the opening `('. */
10919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10920 return error_mark_node;
10922 /* First, try parsing an id-expression. */
10923 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10924 cp_parser_parse_tentatively (parser);
10925 expr = cp_parser_id_expression (parser,
10926 /*template_keyword_p=*/false,
10927 /*check_dependency_p=*/true,
10928 /*template_p=*/NULL,
10929 /*declarator_p=*/false,
10930 /*optional_p=*/false);
10932 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10934 bool non_integral_constant_expression_p = false;
10935 tree id_expression = expr;
10937 const char *error_msg;
10939 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10940 /* Lookup the name we got back from the id-expression. */
10941 expr = cp_parser_lookup_name (parser, expr,
10943 /*is_template=*/false,
10944 /*is_namespace=*/false,
10945 /*check_dependency=*/true,
10946 /*ambiguous_decls=*/NULL,
10947 id_expr_start_token->location);
10950 && expr != error_mark_node
10951 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10952 && TREE_CODE (expr) != TYPE_DECL
10953 && (TREE_CODE (expr) != BIT_NOT_EXPR
10954 || !TYPE_P (TREE_OPERAND (expr, 0)))
10955 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10957 /* Complete lookup of the id-expression. */
10958 expr = (finish_id_expression
10959 (id_expression, expr, parser->scope, &idk,
10960 /*integral_constant_expression_p=*/false,
10961 /*allow_non_integral_constant_expression_p=*/true,
10962 &non_integral_constant_expression_p,
10963 /*template_p=*/false,
10965 /*address_p=*/false,
10966 /*template_arg_p=*/false,
10968 id_expr_start_token->location));
10970 if (expr == error_mark_node)
10971 /* We found an id-expression, but it was something that we
10972 should not have found. This is an error, not something
10973 we can recover from, so note that we found an
10974 id-expression and we'll recover as gracefully as
10976 id_expression_or_member_access_p = true;
10980 && expr != error_mark_node
10981 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10982 /* We have an id-expression. */
10983 id_expression_or_member_access_p = true;
10986 if (!id_expression_or_member_access_p)
10988 /* Abort the id-expression parse. */
10989 cp_parser_abort_tentative_parse (parser);
10991 /* Parsing tentatively, again. */
10992 cp_parser_parse_tentatively (parser);
10994 /* Parse a class member access. */
10995 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10997 /*member_access_only_p=*/true, NULL);
11000 && expr != error_mark_node
11001 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11002 /* We have an id-expression. */
11003 id_expression_or_member_access_p = true;
11006 if (id_expression_or_member_access_p)
11007 /* We have parsed the complete id-expression or member access. */
11008 cp_parser_parse_definitely (parser);
11011 bool saved_greater_than_is_operator_p;
11013 /* Abort our attempt to parse an id-expression or member access
11015 cp_parser_abort_tentative_parse (parser);
11017 /* Within a parenthesized expression, a `>' token is always
11018 the greater-than operator. */
11019 saved_greater_than_is_operator_p
11020 = parser->greater_than_is_operator_p;
11021 parser->greater_than_is_operator_p = true;
11023 /* Parse a full expression. */
11024 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11026 /* The `>' token might be the end of a template-id or
11027 template-parameter-list now. */
11028 parser->greater_than_is_operator_p
11029 = saved_greater_than_is_operator_p;
11032 /* Go back to evaluating expressions. */
11033 --cp_unevaluated_operand;
11034 --c_inhibit_evaluation_warnings;
11036 /* Restore the old message and the integral constant expression
11038 parser->type_definition_forbidden_message = saved_message;
11039 parser->integral_constant_expression_p
11040 = saved_integral_constant_expression_p;
11041 parser->non_integral_constant_expression_p
11042 = saved_non_integral_constant_expression_p;
11044 /* Parse to the closing `)'. */
11045 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11047 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11048 /*consume_paren=*/true);
11049 return error_mark_node;
11052 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11053 tf_warning_or_error);
11055 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11057 start_token->type = CPP_DECLTYPE;
11058 start_token->u.value = expr;
11059 start_token->keyword = RID_MAX;
11060 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11065 /* Special member functions [gram.special] */
11067 /* Parse a conversion-function-id.
11069 conversion-function-id:
11070 operator conversion-type-id
11072 Returns an IDENTIFIER_NODE representing the operator. */
11075 cp_parser_conversion_function_id (cp_parser* parser)
11079 tree saved_qualifying_scope;
11080 tree saved_object_scope;
11081 tree pushed_scope = NULL_TREE;
11083 /* Look for the `operator' token. */
11084 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11085 return error_mark_node;
11086 /* When we parse the conversion-type-id, the current scope will be
11087 reset. However, we need that information in able to look up the
11088 conversion function later, so we save it here. */
11089 saved_scope = parser->scope;
11090 saved_qualifying_scope = parser->qualifying_scope;
11091 saved_object_scope = parser->object_scope;
11092 /* We must enter the scope of the class so that the names of
11093 entities declared within the class are available in the
11094 conversion-type-id. For example, consider:
11101 S::operator I() { ... }
11103 In order to see that `I' is a type-name in the definition, we
11104 must be in the scope of `S'. */
11106 pushed_scope = push_scope (saved_scope);
11107 /* Parse the conversion-type-id. */
11108 type = cp_parser_conversion_type_id (parser);
11109 /* Leave the scope of the class, if any. */
11111 pop_scope (pushed_scope);
11112 /* Restore the saved scope. */
11113 parser->scope = saved_scope;
11114 parser->qualifying_scope = saved_qualifying_scope;
11115 parser->object_scope = saved_object_scope;
11116 /* If the TYPE is invalid, indicate failure. */
11117 if (type == error_mark_node)
11118 return error_mark_node;
11119 return mangle_conv_op_name_for_type (type);
11122 /* Parse a conversion-type-id:
11124 conversion-type-id:
11125 type-specifier-seq conversion-declarator [opt]
11127 Returns the TYPE specified. */
11130 cp_parser_conversion_type_id (cp_parser* parser)
11133 cp_decl_specifier_seq type_specifiers;
11134 cp_declarator *declarator;
11135 tree type_specified;
11137 /* Parse the attributes. */
11138 attributes = cp_parser_attributes_opt (parser);
11139 /* Parse the type-specifiers. */
11140 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11141 /*is_trailing_return=*/false,
11143 /* If that didn't work, stop. */
11144 if (type_specifiers.type == error_mark_node)
11145 return error_mark_node;
11146 /* Parse the conversion-declarator. */
11147 declarator = cp_parser_conversion_declarator_opt (parser);
11149 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11150 /*initialized=*/0, &attributes);
11152 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11154 /* Don't give this error when parsing tentatively. This happens to
11155 work because we always parse this definitively once. */
11156 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11157 && type_uses_auto (type_specified))
11159 error ("invalid use of %<auto%> in conversion operator");
11160 return error_mark_node;
11163 return type_specified;
11166 /* Parse an (optional) conversion-declarator.
11168 conversion-declarator:
11169 ptr-operator conversion-declarator [opt]
11173 static cp_declarator *
11174 cp_parser_conversion_declarator_opt (cp_parser* parser)
11176 enum tree_code code;
11178 cp_cv_quals cv_quals;
11180 /* We don't know if there's a ptr-operator next, or not. */
11181 cp_parser_parse_tentatively (parser);
11182 /* Try the ptr-operator. */
11183 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11184 /* If it worked, look for more conversion-declarators. */
11185 if (cp_parser_parse_definitely (parser))
11187 cp_declarator *declarator;
11189 /* Parse another optional declarator. */
11190 declarator = cp_parser_conversion_declarator_opt (parser);
11192 return cp_parser_make_indirect_declarator
11193 (code, class_type, cv_quals, declarator);
11199 /* Parse an (optional) ctor-initializer.
11202 : mem-initializer-list
11204 Returns TRUE iff the ctor-initializer was actually present. */
11207 cp_parser_ctor_initializer_opt (cp_parser* parser)
11209 /* If the next token is not a `:', then there is no
11210 ctor-initializer. */
11211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11213 /* Do default initialization of any bases and members. */
11214 if (DECL_CONSTRUCTOR_P (current_function_decl))
11215 finish_mem_initializers (NULL_TREE);
11220 /* Consume the `:' token. */
11221 cp_lexer_consume_token (parser->lexer);
11222 /* And the mem-initializer-list. */
11223 cp_parser_mem_initializer_list (parser);
11228 /* Parse a mem-initializer-list.
11230 mem-initializer-list:
11231 mem-initializer ... [opt]
11232 mem-initializer ... [opt] , mem-initializer-list */
11235 cp_parser_mem_initializer_list (cp_parser* parser)
11237 tree mem_initializer_list = NULL_TREE;
11238 cp_token *token = cp_lexer_peek_token (parser->lexer);
11240 /* Let the semantic analysis code know that we are starting the
11241 mem-initializer-list. */
11242 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11243 error_at (token->location,
11244 "only constructors take member initializers");
11246 /* Loop through the list. */
11249 tree mem_initializer;
11251 token = cp_lexer_peek_token (parser->lexer);
11252 /* Parse the mem-initializer. */
11253 mem_initializer = cp_parser_mem_initializer (parser);
11254 /* If the next token is a `...', we're expanding member initializers. */
11255 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11257 /* Consume the `...'. */
11258 cp_lexer_consume_token (parser->lexer);
11260 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11261 can be expanded but members cannot. */
11262 if (mem_initializer != error_mark_node
11263 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11265 error_at (token->location,
11266 "cannot expand initializer for member %<%D%>",
11267 TREE_PURPOSE (mem_initializer));
11268 mem_initializer = error_mark_node;
11271 /* Construct the pack expansion type. */
11272 if (mem_initializer != error_mark_node)
11273 mem_initializer = make_pack_expansion (mem_initializer);
11275 /* Add it to the list, unless it was erroneous. */
11276 if (mem_initializer != error_mark_node)
11278 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11279 mem_initializer_list = mem_initializer;
11281 /* If the next token is not a `,', we're done. */
11282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11284 /* Consume the `,' token. */
11285 cp_lexer_consume_token (parser->lexer);
11288 /* Perform semantic analysis. */
11289 if (DECL_CONSTRUCTOR_P (current_function_decl))
11290 finish_mem_initializers (mem_initializer_list);
11293 /* Parse a mem-initializer.
11296 mem-initializer-id ( expression-list [opt] )
11297 mem-initializer-id braced-init-list
11302 ( expression-list [opt] )
11304 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11305 class) or FIELD_DECL (for a non-static data member) to initialize;
11306 the TREE_VALUE is the expression-list. An empty initialization
11307 list is represented by void_list_node. */
11310 cp_parser_mem_initializer (cp_parser* parser)
11312 tree mem_initializer_id;
11313 tree expression_list;
11315 cp_token *token = cp_lexer_peek_token (parser->lexer);
11317 /* Find out what is being initialized. */
11318 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11320 permerror (token->location,
11321 "anachronistic old-style base class initializer");
11322 mem_initializer_id = NULL_TREE;
11326 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11327 if (mem_initializer_id == error_mark_node)
11328 return mem_initializer_id;
11330 member = expand_member_init (mem_initializer_id);
11331 if (member && !DECL_P (member))
11332 in_base_initializer = 1;
11334 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11336 bool expr_non_constant_p;
11337 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11338 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11339 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11340 expression_list = build_tree_list (NULL_TREE, expression_list);
11345 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11347 /*allow_expansion_p=*/true,
11348 /*non_constant_p=*/NULL);
11350 return error_mark_node;
11351 expression_list = build_tree_list_vec (vec);
11352 release_tree_vector (vec);
11355 if (expression_list == error_mark_node)
11356 return error_mark_node;
11357 if (!expression_list)
11358 expression_list = void_type_node;
11360 in_base_initializer = 0;
11362 return member ? build_tree_list (member, expression_list) : error_mark_node;
11365 /* Parse a mem-initializer-id.
11367 mem-initializer-id:
11368 :: [opt] nested-name-specifier [opt] class-name
11371 Returns a TYPE indicating the class to be initializer for the first
11372 production. Returns an IDENTIFIER_NODE indicating the data member
11373 to be initialized for the second production. */
11376 cp_parser_mem_initializer_id (cp_parser* parser)
11378 bool global_scope_p;
11379 bool nested_name_specifier_p;
11380 bool template_p = false;
11383 cp_token *token = cp_lexer_peek_token (parser->lexer);
11385 /* `typename' is not allowed in this context ([temp.res]). */
11386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11388 error_at (token->location,
11389 "keyword %<typename%> not allowed in this context (a qualified "
11390 "member initializer is implicitly a type)");
11391 cp_lexer_consume_token (parser->lexer);
11393 /* Look for the optional `::' operator. */
11395 = (cp_parser_global_scope_opt (parser,
11396 /*current_scope_valid_p=*/false)
11398 /* Look for the optional nested-name-specifier. The simplest way to
11403 The keyword `typename' is not permitted in a base-specifier or
11404 mem-initializer; in these contexts a qualified name that
11405 depends on a template-parameter is implicitly assumed to be a
11408 is to assume that we have seen the `typename' keyword at this
11410 nested_name_specifier_p
11411 = (cp_parser_nested_name_specifier_opt (parser,
11412 /*typename_keyword_p=*/true,
11413 /*check_dependency_p=*/true,
11415 /*is_declaration=*/true)
11417 if (nested_name_specifier_p)
11418 template_p = cp_parser_optional_template_keyword (parser);
11419 /* If there is a `::' operator or a nested-name-specifier, then we
11420 are definitely looking for a class-name. */
11421 if (global_scope_p || nested_name_specifier_p)
11422 return cp_parser_class_name (parser,
11423 /*typename_keyword_p=*/true,
11424 /*template_keyword_p=*/template_p,
11426 /*check_dependency_p=*/true,
11427 /*class_head_p=*/false,
11428 /*is_declaration=*/true);
11429 /* Otherwise, we could also be looking for an ordinary identifier. */
11430 cp_parser_parse_tentatively (parser);
11431 /* Try a class-name. */
11432 id = cp_parser_class_name (parser,
11433 /*typename_keyword_p=*/true,
11434 /*template_keyword_p=*/false,
11436 /*check_dependency_p=*/true,
11437 /*class_head_p=*/false,
11438 /*is_declaration=*/true);
11439 /* If we found one, we're done. */
11440 if (cp_parser_parse_definitely (parser))
11442 /* Otherwise, look for an ordinary identifier. */
11443 return cp_parser_identifier (parser);
11446 /* Overloading [gram.over] */
11448 /* Parse an operator-function-id.
11450 operator-function-id:
11453 Returns an IDENTIFIER_NODE for the operator which is a
11454 human-readable spelling of the identifier, e.g., `operator +'. */
11457 cp_parser_operator_function_id (cp_parser* parser)
11459 /* Look for the `operator' keyword. */
11460 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11461 return error_mark_node;
11462 /* And then the name of the operator itself. */
11463 return cp_parser_operator (parser);
11466 /* Return an identifier node for a user-defined literal operator.
11467 The suffix identifier is chained to the operator name identifier. */
11470 cp_literal_operator_id (const char* name)
11473 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11474 + strlen (name) + 10);
11475 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11476 identifier = get_identifier (buffer);
11477 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11482 /* Parse an operator.
11485 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11486 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11487 || ++ -- , ->* -> () []
11494 Returns an IDENTIFIER_NODE for the operator which is a
11495 human-readable spelling of the identifier, e.g., `operator +'. */
11498 cp_parser_operator (cp_parser* parser)
11500 tree id = NULL_TREE;
11503 /* Peek at the next token. */
11504 token = cp_lexer_peek_token (parser->lexer);
11505 /* Figure out which operator we have. */
11506 switch (token->type)
11512 /* The keyword should be either `new' or `delete'. */
11513 if (token->keyword == RID_NEW)
11515 else if (token->keyword == RID_DELETE)
11520 /* Consume the `new' or `delete' token. */
11521 cp_lexer_consume_token (parser->lexer);
11523 /* Peek at the next token. */
11524 token = cp_lexer_peek_token (parser->lexer);
11525 /* If it's a `[' token then this is the array variant of the
11527 if (token->type == CPP_OPEN_SQUARE)
11529 /* Consume the `[' token. */
11530 cp_lexer_consume_token (parser->lexer);
11531 /* Look for the `]' token. */
11532 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11533 id = ansi_opname (op == NEW_EXPR
11534 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11536 /* Otherwise, we have the non-array variant. */
11538 id = ansi_opname (op);
11544 id = ansi_opname (PLUS_EXPR);
11548 id = ansi_opname (MINUS_EXPR);
11552 id = ansi_opname (MULT_EXPR);
11556 id = ansi_opname (TRUNC_DIV_EXPR);
11560 id = ansi_opname (TRUNC_MOD_EXPR);
11564 id = ansi_opname (BIT_XOR_EXPR);
11568 id = ansi_opname (BIT_AND_EXPR);
11572 id = ansi_opname (BIT_IOR_EXPR);
11576 id = ansi_opname (BIT_NOT_EXPR);
11580 id = ansi_opname (TRUTH_NOT_EXPR);
11584 id = ansi_assopname (NOP_EXPR);
11588 id = ansi_opname (LT_EXPR);
11592 id = ansi_opname (GT_EXPR);
11596 id = ansi_assopname (PLUS_EXPR);
11600 id = ansi_assopname (MINUS_EXPR);
11604 id = ansi_assopname (MULT_EXPR);
11608 id = ansi_assopname (TRUNC_DIV_EXPR);
11612 id = ansi_assopname (TRUNC_MOD_EXPR);
11616 id = ansi_assopname (BIT_XOR_EXPR);
11620 id = ansi_assopname (BIT_AND_EXPR);
11624 id = ansi_assopname (BIT_IOR_EXPR);
11628 id = ansi_opname (LSHIFT_EXPR);
11632 id = ansi_opname (RSHIFT_EXPR);
11635 case CPP_LSHIFT_EQ:
11636 id = ansi_assopname (LSHIFT_EXPR);
11639 case CPP_RSHIFT_EQ:
11640 id = ansi_assopname (RSHIFT_EXPR);
11644 id = ansi_opname (EQ_EXPR);
11648 id = ansi_opname (NE_EXPR);
11652 id = ansi_opname (LE_EXPR);
11655 case CPP_GREATER_EQ:
11656 id = ansi_opname (GE_EXPR);
11660 id = ansi_opname (TRUTH_ANDIF_EXPR);
11664 id = ansi_opname (TRUTH_ORIF_EXPR);
11667 case CPP_PLUS_PLUS:
11668 id = ansi_opname (POSTINCREMENT_EXPR);
11671 case CPP_MINUS_MINUS:
11672 id = ansi_opname (PREDECREMENT_EXPR);
11676 id = ansi_opname (COMPOUND_EXPR);
11679 case CPP_DEREF_STAR:
11680 id = ansi_opname (MEMBER_REF);
11684 id = ansi_opname (COMPONENT_REF);
11687 case CPP_OPEN_PAREN:
11688 /* Consume the `('. */
11689 cp_lexer_consume_token (parser->lexer);
11690 /* Look for the matching `)'. */
11691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11692 return ansi_opname (CALL_EXPR);
11694 case CPP_OPEN_SQUARE:
11695 /* Consume the `['. */
11696 cp_lexer_consume_token (parser->lexer);
11697 /* Look for the matching `]'. */
11698 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11699 return ansi_opname (ARRAY_REF);
11702 if (cxx_dialect == cxx98)
11703 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11704 if (TREE_STRING_LENGTH (token->u.value) > 2)
11706 error ("expected empty string after %<operator%> keyword");
11707 return error_mark_node;
11709 /* Consume the string. */
11710 cp_lexer_consume_token (parser->lexer);
11711 /* Look for the suffix identifier. */
11712 token = cp_lexer_peek_token (parser->lexer);
11713 if (token->type == CPP_NAME)
11715 id = cp_parser_identifier (parser);
11716 if (id != error_mark_node)
11718 const char *name = IDENTIFIER_POINTER (id);
11719 return cp_literal_operator_id (name);
11724 error ("expected suffix identifier");
11725 return error_mark_node;
11728 case CPP_STRING_USERDEF:
11729 error ("missing space between %<\"\"%> and suffix identifier");
11730 return error_mark_node;
11733 /* Anything else is an error. */
11737 /* If we have selected an identifier, we need to consume the
11740 cp_lexer_consume_token (parser->lexer);
11741 /* Otherwise, no valid operator name was present. */
11744 cp_parser_error (parser, "expected operator");
11745 id = error_mark_node;
11751 /* Parse a template-declaration.
11753 template-declaration:
11754 export [opt] template < template-parameter-list > declaration
11756 If MEMBER_P is TRUE, this template-declaration occurs within a
11759 The grammar rule given by the standard isn't correct. What
11760 is really meant is:
11762 template-declaration:
11763 export [opt] template-parameter-list-seq
11764 decl-specifier-seq [opt] init-declarator [opt] ;
11765 export [opt] template-parameter-list-seq
11766 function-definition
11768 template-parameter-list-seq:
11769 template-parameter-list-seq [opt]
11770 template < template-parameter-list > */
11773 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11775 /* Check for `export'. */
11776 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11778 /* Consume the `export' token. */
11779 cp_lexer_consume_token (parser->lexer);
11780 /* Warn that we do not support `export'. */
11781 warning (0, "keyword %<export%> not implemented, and will be ignored");
11784 cp_parser_template_declaration_after_export (parser, member_p);
11787 /* Parse a template-parameter-list.
11789 template-parameter-list:
11791 template-parameter-list , template-parameter
11793 Returns a TREE_LIST. Each node represents a template parameter.
11794 The nodes are connected via their TREE_CHAINs. */
11797 cp_parser_template_parameter_list (cp_parser* parser)
11799 tree parameter_list = NULL_TREE;
11801 begin_template_parm_list ();
11803 /* The loop below parses the template parms. We first need to know
11804 the total number of template parms to be able to compute proper
11805 canonical types of each dependent type. So after the loop, when
11806 we know the total number of template parms,
11807 end_template_parm_list computes the proper canonical types and
11808 fixes up the dependent types accordingly. */
11813 bool is_parameter_pack;
11814 location_t parm_loc;
11816 /* Parse the template-parameter. */
11817 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11818 parameter = cp_parser_template_parameter (parser,
11820 &is_parameter_pack);
11821 /* Add it to the list. */
11822 if (parameter != error_mark_node)
11823 parameter_list = process_template_parm (parameter_list,
11831 tree err_parm = build_tree_list (parameter, parameter);
11832 parameter_list = chainon (parameter_list, err_parm);
11835 /* If the next token is not a `,', we're done. */
11836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11838 /* Otherwise, consume the `,' token. */
11839 cp_lexer_consume_token (parser->lexer);
11842 return end_template_parm_list (parameter_list);
11845 /* Parse a template-parameter.
11847 template-parameter:
11849 parameter-declaration
11851 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11852 the parameter. The TREE_PURPOSE is the default value, if any.
11853 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11854 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11855 set to true iff this parameter is a parameter pack. */
11858 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11859 bool *is_parameter_pack)
11862 cp_parameter_declarator *parameter_declarator;
11863 cp_declarator *id_declarator;
11866 /* Assume it is a type parameter or a template parameter. */
11867 *is_non_type = false;
11868 /* Assume it not a parameter pack. */
11869 *is_parameter_pack = false;
11870 /* Peek at the next token. */
11871 token = cp_lexer_peek_token (parser->lexer);
11872 /* If it is `class' or `template', we have a type-parameter. */
11873 if (token->keyword == RID_TEMPLATE)
11874 return cp_parser_type_parameter (parser, is_parameter_pack);
11875 /* If it is `class' or `typename' we do not know yet whether it is a
11876 type parameter or a non-type parameter. Consider:
11878 template <typename T, typename T::X X> ...
11882 template <class C, class D*> ...
11884 Here, the first parameter is a type parameter, and the second is
11885 a non-type parameter. We can tell by looking at the token after
11886 the identifier -- if it is a `,', `=', or `>' then we have a type
11888 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11890 /* Peek at the token after `class' or `typename'. */
11891 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11892 /* If it's an ellipsis, we have a template type parameter
11894 if (token->type == CPP_ELLIPSIS)
11895 return cp_parser_type_parameter (parser, is_parameter_pack);
11896 /* If it's an identifier, skip it. */
11897 if (token->type == CPP_NAME)
11898 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11899 /* Now, see if the token looks like the end of a template
11901 if (token->type == CPP_COMMA
11902 || token->type == CPP_EQ
11903 || token->type == CPP_GREATER)
11904 return cp_parser_type_parameter (parser, is_parameter_pack);
11907 /* Otherwise, it is a non-type parameter.
11911 When parsing a default template-argument for a non-type
11912 template-parameter, the first non-nested `>' is taken as the end
11913 of the template parameter-list rather than a greater-than
11915 *is_non_type = true;
11916 parameter_declarator
11917 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11918 /*parenthesized_p=*/NULL);
11920 /* If the parameter declaration is marked as a parameter pack, set
11921 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11922 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11924 if (parameter_declarator
11925 && parameter_declarator->declarator
11926 && parameter_declarator->declarator->parameter_pack_p)
11928 *is_parameter_pack = true;
11929 parameter_declarator->declarator->parameter_pack_p = false;
11932 /* If the next token is an ellipsis, and we don't already have it
11933 marked as a parameter pack, then we have a parameter pack (that
11934 has no declarator). */
11935 if (!*is_parameter_pack
11936 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11937 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11939 /* Consume the `...'. */
11940 cp_lexer_consume_token (parser->lexer);
11941 maybe_warn_variadic_templates ();
11943 *is_parameter_pack = true;
11945 /* We might end up with a pack expansion as the type of the non-type
11946 template parameter, in which case this is a non-type template
11948 else if (parameter_declarator
11949 && parameter_declarator->decl_specifiers.type
11950 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11952 *is_parameter_pack = true;
11953 parameter_declarator->decl_specifiers.type =
11954 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11957 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11959 /* Parameter packs cannot have default arguments. However, a
11960 user may try to do so, so we'll parse them and give an
11961 appropriate diagnostic here. */
11963 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11965 /* Find the name of the parameter pack. */
11966 id_declarator = parameter_declarator->declarator;
11967 while (id_declarator && id_declarator->kind != cdk_id)
11968 id_declarator = id_declarator->declarator;
11970 if (id_declarator && id_declarator->kind == cdk_id)
11971 error_at (start_token->location,
11972 "template parameter pack %qD cannot have a default argument",
11973 id_declarator->u.id.unqualified_name);
11975 error_at (start_token->location,
11976 "template parameter pack cannot have a default argument");
11978 /* Parse the default argument, but throw away the result. */
11979 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11982 parm = grokdeclarator (parameter_declarator->declarator,
11983 ¶meter_declarator->decl_specifiers,
11984 TPARM, /*initialized=*/0,
11985 /*attrlist=*/NULL);
11986 if (parm == error_mark_node)
11987 return error_mark_node;
11989 return build_tree_list (parameter_declarator->default_argument, parm);
11992 /* Parse a type-parameter.
11995 class identifier [opt]
11996 class identifier [opt] = type-id
11997 typename identifier [opt]
11998 typename identifier [opt] = type-id
11999 template < template-parameter-list > class identifier [opt]
12000 template < template-parameter-list > class identifier [opt]
12003 GNU Extension (variadic templates):
12006 class ... identifier [opt]
12007 typename ... identifier [opt]
12009 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12010 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12011 the declaration of the parameter.
12013 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12016 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12021 /* Look for a keyword to tell us what kind of parameter this is. */
12022 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12024 return error_mark_node;
12026 switch (token->keyword)
12032 tree default_argument;
12034 /* If the next token is an ellipsis, we have a template
12036 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12038 /* Consume the `...' token. */
12039 cp_lexer_consume_token (parser->lexer);
12040 maybe_warn_variadic_templates ();
12042 *is_parameter_pack = true;
12045 /* If the next token is an identifier, then it names the
12047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12048 identifier = cp_parser_identifier (parser);
12050 identifier = NULL_TREE;
12052 /* Create the parameter. */
12053 parameter = finish_template_type_parm (class_type_node, identifier);
12055 /* If the next token is an `=', we have a default argument. */
12056 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12058 /* Consume the `=' token. */
12059 cp_lexer_consume_token (parser->lexer);
12060 /* Parse the default-argument. */
12061 push_deferring_access_checks (dk_no_deferred);
12062 default_argument = cp_parser_type_id (parser);
12064 /* Template parameter packs cannot have default
12066 if (*is_parameter_pack)
12069 error_at (token->location,
12070 "template parameter pack %qD cannot have a "
12071 "default argument", identifier);
12073 error_at (token->location,
12074 "template parameter packs cannot have "
12075 "default arguments");
12076 default_argument = NULL_TREE;
12078 pop_deferring_access_checks ();
12081 default_argument = NULL_TREE;
12083 /* Create the combined representation of the parameter and the
12084 default argument. */
12085 parameter = build_tree_list (default_argument, parameter);
12092 tree default_argument;
12094 /* Look for the `<'. */
12095 cp_parser_require (parser, CPP_LESS, RT_LESS);
12096 /* Parse the template-parameter-list. */
12097 cp_parser_template_parameter_list (parser);
12098 /* Look for the `>'. */
12099 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12100 /* Look for the `class' keyword. */
12101 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12102 /* If the next token is an ellipsis, we have a template
12104 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12106 /* Consume the `...' token. */
12107 cp_lexer_consume_token (parser->lexer);
12108 maybe_warn_variadic_templates ();
12110 *is_parameter_pack = true;
12112 /* If the next token is an `=', then there is a
12113 default-argument. If the next token is a `>', we are at
12114 the end of the parameter-list. If the next token is a `,',
12115 then we are at the end of this parameter. */
12116 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12117 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12118 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12120 identifier = cp_parser_identifier (parser);
12121 /* Treat invalid names as if the parameter were nameless. */
12122 if (identifier == error_mark_node)
12123 identifier = NULL_TREE;
12126 identifier = NULL_TREE;
12128 /* Create the template parameter. */
12129 parameter = finish_template_template_parm (class_type_node,
12132 /* If the next token is an `=', then there is a
12133 default-argument. */
12134 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12138 /* Consume the `='. */
12139 cp_lexer_consume_token (parser->lexer);
12140 /* Parse the id-expression. */
12141 push_deferring_access_checks (dk_no_deferred);
12142 /* save token before parsing the id-expression, for error
12144 token = cp_lexer_peek_token (parser->lexer);
12146 = cp_parser_id_expression (parser,
12147 /*template_keyword_p=*/false,
12148 /*check_dependency_p=*/true,
12149 /*template_p=*/&is_template,
12150 /*declarator_p=*/false,
12151 /*optional_p=*/false);
12152 if (TREE_CODE (default_argument) == TYPE_DECL)
12153 /* If the id-expression was a template-id that refers to
12154 a template-class, we already have the declaration here,
12155 so no further lookup is needed. */
12158 /* Look up the name. */
12160 = cp_parser_lookup_name (parser, default_argument,
12162 /*is_template=*/is_template,
12163 /*is_namespace=*/false,
12164 /*check_dependency=*/true,
12165 /*ambiguous_decls=*/NULL,
12167 /* See if the default argument is valid. */
12169 = check_template_template_default_arg (default_argument);
12171 /* Template parameter packs cannot have default
12173 if (*is_parameter_pack)
12176 error_at (token->location,
12177 "template parameter pack %qD cannot "
12178 "have a default argument",
12181 error_at (token->location, "template parameter packs cannot "
12182 "have default arguments");
12183 default_argument = NULL_TREE;
12185 pop_deferring_access_checks ();
12188 default_argument = NULL_TREE;
12190 /* Create the combined representation of the parameter and the
12191 default argument. */
12192 parameter = build_tree_list (default_argument, parameter);
12197 gcc_unreachable ();
12204 /* Parse a template-id.
12207 template-name < template-argument-list [opt] >
12209 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12210 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12211 returned. Otherwise, if the template-name names a function, or set
12212 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12213 names a class, returns a TYPE_DECL for the specialization.
12215 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12216 uninstantiated templates. */
12219 cp_parser_template_id (cp_parser *parser,
12220 bool template_keyword_p,
12221 bool check_dependency_p,
12222 bool is_declaration)
12228 cp_token_position start_of_id = 0;
12229 deferred_access_check *chk;
12230 VEC (deferred_access_check,gc) *access_check;
12231 cp_token *next_token = NULL, *next_token_2 = NULL;
12232 bool is_identifier;
12234 /* If the next token corresponds to a template-id, there is no need
12236 next_token = cp_lexer_peek_token (parser->lexer);
12237 if (next_token->type == CPP_TEMPLATE_ID)
12239 struct tree_check *check_value;
12241 /* Get the stored value. */
12242 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12243 /* Perform any access checks that were deferred. */
12244 access_check = check_value->checks;
12247 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12248 perform_or_defer_access_check (chk->binfo,
12252 /* Return the stored value. */
12253 return check_value->value;
12256 /* Avoid performing name lookup if there is no possibility of
12257 finding a template-id. */
12258 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12259 || (next_token->type == CPP_NAME
12260 && !cp_parser_nth_token_starts_template_argument_list_p
12263 cp_parser_error (parser, "expected template-id");
12264 return error_mark_node;
12267 /* Remember where the template-id starts. */
12268 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12269 start_of_id = cp_lexer_token_position (parser->lexer, false);
12271 push_deferring_access_checks (dk_deferred);
12273 /* Parse the template-name. */
12274 is_identifier = false;
12275 templ = cp_parser_template_name (parser, template_keyword_p,
12276 check_dependency_p,
12279 if (templ == error_mark_node || is_identifier)
12281 pop_deferring_access_checks ();
12285 /* If we find the sequence `[:' after a template-name, it's probably
12286 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12287 parse correctly the argument list. */
12288 next_token = cp_lexer_peek_token (parser->lexer);
12289 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12290 if (next_token->type == CPP_OPEN_SQUARE
12291 && next_token->flags & DIGRAPH
12292 && next_token_2->type == CPP_COLON
12293 && !(next_token_2->flags & PREV_WHITE))
12295 cp_parser_parse_tentatively (parser);
12296 /* Change `:' into `::'. */
12297 next_token_2->type = CPP_SCOPE;
12298 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12300 cp_lexer_consume_token (parser->lexer);
12302 /* Parse the arguments. */
12303 arguments = cp_parser_enclosed_template_argument_list (parser);
12304 if (!cp_parser_parse_definitely (parser))
12306 /* If we couldn't parse an argument list, then we revert our changes
12307 and return simply an error. Maybe this is not a template-id
12309 next_token_2->type = CPP_COLON;
12310 cp_parser_error (parser, "expected %<<%>");
12311 pop_deferring_access_checks ();
12312 return error_mark_node;
12314 /* Otherwise, emit an error about the invalid digraph, but continue
12315 parsing because we got our argument list. */
12316 if (permerror (next_token->location,
12317 "%<<::%> cannot begin a template-argument list"))
12319 static bool hint = false;
12320 inform (next_token->location,
12321 "%<<:%> is an alternate spelling for %<[%>."
12322 " Insert whitespace between %<<%> and %<::%>");
12323 if (!hint && !flag_permissive)
12325 inform (next_token->location, "(if you use %<-fpermissive%>"
12326 " G++ will accept your code)");
12333 /* Look for the `<' that starts the template-argument-list. */
12334 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12336 pop_deferring_access_checks ();
12337 return error_mark_node;
12339 /* Parse the arguments. */
12340 arguments = cp_parser_enclosed_template_argument_list (parser);
12343 /* Build a representation of the specialization. */
12344 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12345 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12346 else if (DECL_CLASS_TEMPLATE_P (templ)
12347 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12349 bool entering_scope;
12350 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12351 template (rather than some instantiation thereof) only if
12352 is not nested within some other construct. For example, in
12353 "template <typename T> void f(T) { A<T>::", A<T> is just an
12354 instantiation of A. */
12355 entering_scope = (template_parm_scope_p ()
12356 && cp_lexer_next_token_is (parser->lexer,
12359 = finish_template_type (templ, arguments, entering_scope);
12363 /* If it's not a class-template or a template-template, it should be
12364 a function-template. */
12365 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12366 || TREE_CODE (templ) == OVERLOAD
12367 || BASELINK_P (templ)));
12369 template_id = lookup_template_function (templ, arguments);
12372 /* If parsing tentatively, replace the sequence of tokens that makes
12373 up the template-id with a CPP_TEMPLATE_ID token. That way,
12374 should we re-parse the token stream, we will not have to repeat
12375 the effort required to do the parse, nor will we issue duplicate
12376 error messages about problems during instantiation of the
12380 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12382 /* Reset the contents of the START_OF_ID token. */
12383 token->type = CPP_TEMPLATE_ID;
12384 /* Retrieve any deferred checks. Do not pop this access checks yet
12385 so the memory will not be reclaimed during token replacing below. */
12386 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12387 token->u.tree_check_value->value = template_id;
12388 token->u.tree_check_value->checks = get_deferred_access_checks ();
12389 token->keyword = RID_MAX;
12391 /* Purge all subsequent tokens. */
12392 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12394 /* ??? Can we actually assume that, if template_id ==
12395 error_mark_node, we will have issued a diagnostic to the
12396 user, as opposed to simply marking the tentative parse as
12398 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12399 error_at (token->location, "parse error in template argument list");
12402 pop_deferring_access_checks ();
12403 return template_id;
12406 /* Parse a template-name.
12411 The standard should actually say:
12415 operator-function-id
12417 A defect report has been filed about this issue.
12419 A conversion-function-id cannot be a template name because they cannot
12420 be part of a template-id. In fact, looking at this code:
12422 a.operator K<int>()
12424 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12425 It is impossible to call a templated conversion-function-id with an
12426 explicit argument list, since the only allowed template parameter is
12427 the type to which it is converting.
12429 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12430 `template' keyword, in a construction like:
12434 In that case `f' is taken to be a template-name, even though there
12435 is no way of knowing for sure.
12437 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12438 name refers to a set of overloaded functions, at least one of which
12439 is a template, or an IDENTIFIER_NODE with the name of the template,
12440 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12441 names are looked up inside uninstantiated templates. */
12444 cp_parser_template_name (cp_parser* parser,
12445 bool template_keyword_p,
12446 bool check_dependency_p,
12447 bool is_declaration,
12448 bool *is_identifier)
12453 cp_token *token = cp_lexer_peek_token (parser->lexer);
12455 /* If the next token is `operator', then we have either an
12456 operator-function-id or a conversion-function-id. */
12457 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12459 /* We don't know whether we're looking at an
12460 operator-function-id or a conversion-function-id. */
12461 cp_parser_parse_tentatively (parser);
12462 /* Try an operator-function-id. */
12463 identifier = cp_parser_operator_function_id (parser);
12464 /* If that didn't work, try a conversion-function-id. */
12465 if (!cp_parser_parse_definitely (parser))
12467 cp_parser_error (parser, "expected template-name");
12468 return error_mark_node;
12471 /* Look for the identifier. */
12473 identifier = cp_parser_identifier (parser);
12475 /* If we didn't find an identifier, we don't have a template-id. */
12476 if (identifier == error_mark_node)
12477 return error_mark_node;
12479 /* If the name immediately followed the `template' keyword, then it
12480 is a template-name. However, if the next token is not `<', then
12481 we do not treat it as a template-name, since it is not being used
12482 as part of a template-id. This enables us to handle constructs
12485 template <typename T> struct S { S(); };
12486 template <typename T> S<T>::S();
12488 correctly. We would treat `S' as a template -- if it were `S<T>'
12489 -- but we do not if there is no `<'. */
12491 if (processing_template_decl
12492 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12494 /* In a declaration, in a dependent context, we pretend that the
12495 "template" keyword was present in order to improve error
12496 recovery. For example, given:
12498 template <typename T> void f(T::X<int>);
12500 we want to treat "X<int>" as a template-id. */
12502 && !template_keyword_p
12503 && parser->scope && TYPE_P (parser->scope)
12504 && check_dependency_p
12505 && dependent_scope_p (parser->scope)
12506 /* Do not do this for dtors (or ctors), since they never
12507 need the template keyword before their name. */
12508 && !constructor_name_p (identifier, parser->scope))
12510 cp_token_position start = 0;
12512 /* Explain what went wrong. */
12513 error_at (token->location, "non-template %qD used as template",
12515 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12516 parser->scope, identifier);
12517 /* If parsing tentatively, find the location of the "<" token. */
12518 if (cp_parser_simulate_error (parser))
12519 start = cp_lexer_token_position (parser->lexer, true);
12520 /* Parse the template arguments so that we can issue error
12521 messages about them. */
12522 cp_lexer_consume_token (parser->lexer);
12523 cp_parser_enclosed_template_argument_list (parser);
12524 /* Skip tokens until we find a good place from which to
12525 continue parsing. */
12526 cp_parser_skip_to_closing_parenthesis (parser,
12527 /*recovering=*/true,
12529 /*consume_paren=*/false);
12530 /* If parsing tentatively, permanently remove the
12531 template argument list. That will prevent duplicate
12532 error messages from being issued about the missing
12533 "template" keyword. */
12535 cp_lexer_purge_tokens_after (parser->lexer, start);
12537 *is_identifier = true;
12541 /* If the "template" keyword is present, then there is generally
12542 no point in doing name-lookup, so we just return IDENTIFIER.
12543 But, if the qualifying scope is non-dependent then we can
12544 (and must) do name-lookup normally. */
12545 if (template_keyword_p
12547 || (TYPE_P (parser->scope)
12548 && dependent_type_p (parser->scope))))
12552 /* Look up the name. */
12553 decl = cp_parser_lookup_name (parser, identifier,
12555 /*is_template=*/true,
12556 /*is_namespace=*/false,
12557 check_dependency_p,
12558 /*ambiguous_decls=*/NULL,
12561 /* If DECL is a template, then the name was a template-name. */
12562 if (TREE_CODE (decl) == TEMPLATE_DECL)
12566 tree fn = NULL_TREE;
12568 /* The standard does not explicitly indicate whether a name that
12569 names a set of overloaded declarations, some of which are
12570 templates, is a template-name. However, such a name should
12571 be a template-name; otherwise, there is no way to form a
12572 template-id for the overloaded templates. */
12573 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12574 if (TREE_CODE (fns) == OVERLOAD)
12575 for (fn = fns; fn; fn = OVL_NEXT (fn))
12576 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12581 /* The name does not name a template. */
12582 cp_parser_error (parser, "expected template-name");
12583 return error_mark_node;
12587 /* If DECL is dependent, and refers to a function, then just return
12588 its name; we will look it up again during template instantiation. */
12589 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12591 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12592 if (TYPE_P (scope) && dependent_type_p (scope))
12599 /* Parse a template-argument-list.
12601 template-argument-list:
12602 template-argument ... [opt]
12603 template-argument-list , template-argument ... [opt]
12605 Returns a TREE_VEC containing the arguments. */
12608 cp_parser_template_argument_list (cp_parser* parser)
12610 tree fixed_args[10];
12611 unsigned n_args = 0;
12612 unsigned alloced = 10;
12613 tree *arg_ary = fixed_args;
12615 bool saved_in_template_argument_list_p;
12617 bool saved_non_ice_p;
12619 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12620 parser->in_template_argument_list_p = true;
12621 /* Even if the template-id appears in an integral
12622 constant-expression, the contents of the argument list do
12624 saved_ice_p = parser->integral_constant_expression_p;
12625 parser->integral_constant_expression_p = false;
12626 saved_non_ice_p = parser->non_integral_constant_expression_p;
12627 parser->non_integral_constant_expression_p = false;
12629 /* Parse the arguments. */
12635 /* Consume the comma. */
12636 cp_lexer_consume_token (parser->lexer);
12638 /* Parse the template-argument. */
12639 argument = cp_parser_template_argument (parser);
12641 /* If the next token is an ellipsis, we're expanding a template
12643 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12645 if (argument == error_mark_node)
12647 cp_token *token = cp_lexer_peek_token (parser->lexer);
12648 error_at (token->location,
12649 "expected parameter pack before %<...%>");
12651 /* Consume the `...' token. */
12652 cp_lexer_consume_token (parser->lexer);
12654 /* Make the argument into a TYPE_PACK_EXPANSION or
12655 EXPR_PACK_EXPANSION. */
12656 argument = make_pack_expansion (argument);
12659 if (n_args == alloced)
12663 if (arg_ary == fixed_args)
12665 arg_ary = XNEWVEC (tree, alloced);
12666 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12669 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12671 arg_ary[n_args++] = argument;
12673 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12675 vec = make_tree_vec (n_args);
12678 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12680 if (arg_ary != fixed_args)
12682 parser->non_integral_constant_expression_p = saved_non_ice_p;
12683 parser->integral_constant_expression_p = saved_ice_p;
12684 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12685 #ifdef ENABLE_CHECKING
12686 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12691 /* Parse a template-argument.
12694 assignment-expression
12698 The representation is that of an assignment-expression, type-id, or
12699 id-expression -- except that the qualified id-expression is
12700 evaluated, so that the value returned is either a DECL or an
12703 Although the standard says "assignment-expression", it forbids
12704 throw-expressions or assignments in the template argument.
12705 Therefore, we use "conditional-expression" instead. */
12708 cp_parser_template_argument (cp_parser* parser)
12713 bool maybe_type_id = false;
12714 cp_token *token = NULL, *argument_start_token = NULL;
12717 /* There's really no way to know what we're looking at, so we just
12718 try each alternative in order.
12722 In a template-argument, an ambiguity between a type-id and an
12723 expression is resolved to a type-id, regardless of the form of
12724 the corresponding template-parameter.
12726 Therefore, we try a type-id first. */
12727 cp_parser_parse_tentatively (parser);
12728 argument = cp_parser_template_type_arg (parser);
12729 /* If there was no error parsing the type-id but the next token is a
12730 '>>', our behavior depends on which dialect of C++ we're
12731 parsing. In C++98, we probably found a typo for '> >'. But there
12732 are type-id which are also valid expressions. For instance:
12734 struct X { int operator >> (int); };
12735 template <int V> struct Foo {};
12738 Here 'X()' is a valid type-id of a function type, but the user just
12739 wanted to write the expression "X() >> 5". Thus, we remember that we
12740 found a valid type-id, but we still try to parse the argument as an
12741 expression to see what happens.
12743 In C++0x, the '>>' will be considered two separate '>'
12745 if (!cp_parser_error_occurred (parser)
12746 && cxx_dialect == cxx98
12747 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12749 maybe_type_id = true;
12750 cp_parser_abort_tentative_parse (parser);
12754 /* If the next token isn't a `,' or a `>', then this argument wasn't
12755 really finished. This means that the argument is not a valid
12757 if (!cp_parser_next_token_ends_template_argument_p (parser))
12758 cp_parser_error (parser, "expected template-argument");
12759 /* If that worked, we're done. */
12760 if (cp_parser_parse_definitely (parser))
12763 /* We're still not sure what the argument will be. */
12764 cp_parser_parse_tentatively (parser);
12765 /* Try a template. */
12766 argument_start_token = cp_lexer_peek_token (parser->lexer);
12767 argument = cp_parser_id_expression (parser,
12768 /*template_keyword_p=*/false,
12769 /*check_dependency_p=*/true,
12771 /*declarator_p=*/false,
12772 /*optional_p=*/false);
12773 /* If the next token isn't a `,' or a `>', then this argument wasn't
12774 really finished. */
12775 if (!cp_parser_next_token_ends_template_argument_p (parser))
12776 cp_parser_error (parser, "expected template-argument");
12777 if (!cp_parser_error_occurred (parser))
12779 /* Figure out what is being referred to. If the id-expression
12780 was for a class template specialization, then we will have a
12781 TYPE_DECL at this point. There is no need to do name lookup
12782 at this point in that case. */
12783 if (TREE_CODE (argument) != TYPE_DECL)
12784 argument = cp_parser_lookup_name (parser, argument,
12786 /*is_template=*/template_p,
12787 /*is_namespace=*/false,
12788 /*check_dependency=*/true,
12789 /*ambiguous_decls=*/NULL,
12790 argument_start_token->location);
12791 if (TREE_CODE (argument) != TEMPLATE_DECL
12792 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12793 cp_parser_error (parser, "expected template-name");
12795 if (cp_parser_parse_definitely (parser))
12797 /* It must be a non-type argument. There permitted cases are given
12798 in [temp.arg.nontype]:
12800 -- an integral constant-expression of integral or enumeration
12803 -- the name of a non-type template-parameter; or
12805 -- the name of an object or function with external linkage...
12807 -- the address of an object or function with external linkage...
12809 -- a pointer to member... */
12810 /* Look for a non-type template parameter. */
12811 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12813 cp_parser_parse_tentatively (parser);
12814 argument = cp_parser_primary_expression (parser,
12815 /*address_p=*/false,
12817 /*template_arg_p=*/true,
12819 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12820 || !cp_parser_next_token_ends_template_argument_p (parser))
12821 cp_parser_simulate_error (parser);
12822 if (cp_parser_parse_definitely (parser))
12826 /* If the next token is "&", the argument must be the address of an
12827 object or function with external linkage. */
12828 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12830 cp_lexer_consume_token (parser->lexer);
12831 /* See if we might have an id-expression. */
12832 token = cp_lexer_peek_token (parser->lexer);
12833 if (token->type == CPP_NAME
12834 || token->keyword == RID_OPERATOR
12835 || token->type == CPP_SCOPE
12836 || token->type == CPP_TEMPLATE_ID
12837 || token->type == CPP_NESTED_NAME_SPECIFIER)
12839 cp_parser_parse_tentatively (parser);
12840 argument = cp_parser_primary_expression (parser,
12843 /*template_arg_p=*/true,
12845 if (cp_parser_error_occurred (parser)
12846 || !cp_parser_next_token_ends_template_argument_p (parser))
12847 cp_parser_abort_tentative_parse (parser);
12852 if (TREE_CODE (argument) == INDIRECT_REF)
12854 gcc_assert (REFERENCE_REF_P (argument));
12855 argument = TREE_OPERAND (argument, 0);
12858 /* If we're in a template, we represent a qualified-id referring
12859 to a static data member as a SCOPE_REF even if the scope isn't
12860 dependent so that we can check access control later. */
12862 if (TREE_CODE (probe) == SCOPE_REF)
12863 probe = TREE_OPERAND (probe, 1);
12864 if (TREE_CODE (probe) == VAR_DECL)
12866 /* A variable without external linkage might still be a
12867 valid constant-expression, so no error is issued here
12868 if the external-linkage check fails. */
12869 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12870 cp_parser_simulate_error (parser);
12872 else if (is_overloaded_fn (argument))
12873 /* All overloaded functions are allowed; if the external
12874 linkage test does not pass, an error will be issued
12878 && (TREE_CODE (argument) == OFFSET_REF
12879 || TREE_CODE (argument) == SCOPE_REF))
12880 /* A pointer-to-member. */
12882 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12885 cp_parser_simulate_error (parser);
12887 if (cp_parser_parse_definitely (parser))
12890 argument = build_x_unary_op (ADDR_EXPR, argument,
12891 tf_warning_or_error);
12896 /* If the argument started with "&", there are no other valid
12897 alternatives at this point. */
12900 cp_parser_error (parser, "invalid non-type template argument");
12901 return error_mark_node;
12904 /* If the argument wasn't successfully parsed as a type-id followed
12905 by '>>', the argument can only be a constant expression now.
12906 Otherwise, we try parsing the constant-expression tentatively,
12907 because the argument could really be a type-id. */
12909 cp_parser_parse_tentatively (parser);
12910 argument = cp_parser_constant_expression (parser,
12911 /*allow_non_constant_p=*/false,
12912 /*non_constant_p=*/NULL);
12913 argument = fold_non_dependent_expr (argument);
12914 if (!maybe_type_id)
12916 if (!cp_parser_next_token_ends_template_argument_p (parser))
12917 cp_parser_error (parser, "expected template-argument");
12918 if (cp_parser_parse_definitely (parser))
12920 /* We did our best to parse the argument as a non type-id, but that
12921 was the only alternative that matched (albeit with a '>' after
12922 it). We can assume it's just a typo from the user, and a
12923 diagnostic will then be issued. */
12924 return cp_parser_template_type_arg (parser);
12927 /* Parse an explicit-instantiation.
12929 explicit-instantiation:
12930 template declaration
12932 Although the standard says `declaration', what it really means is:
12934 explicit-instantiation:
12935 template decl-specifier-seq [opt] declarator [opt] ;
12937 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12938 supposed to be allowed. A defect report has been filed about this
12943 explicit-instantiation:
12944 storage-class-specifier template
12945 decl-specifier-seq [opt] declarator [opt] ;
12946 function-specifier template
12947 decl-specifier-seq [opt] declarator [opt] ; */
12950 cp_parser_explicit_instantiation (cp_parser* parser)
12952 int declares_class_or_enum;
12953 cp_decl_specifier_seq decl_specifiers;
12954 tree extension_specifier = NULL_TREE;
12956 timevar_push (TV_TEMPLATE_INST);
12958 /* Look for an (optional) storage-class-specifier or
12959 function-specifier. */
12960 if (cp_parser_allow_gnu_extensions_p (parser))
12962 extension_specifier
12963 = cp_parser_storage_class_specifier_opt (parser);
12964 if (!extension_specifier)
12965 extension_specifier
12966 = cp_parser_function_specifier_opt (parser,
12967 /*decl_specs=*/NULL);
12970 /* Look for the `template' keyword. */
12971 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12972 /* Let the front end know that we are processing an explicit
12974 begin_explicit_instantiation ();
12975 /* [temp.explicit] says that we are supposed to ignore access
12976 control while processing explicit instantiation directives. */
12977 push_deferring_access_checks (dk_no_check);
12978 /* Parse a decl-specifier-seq. */
12979 cp_parser_decl_specifier_seq (parser,
12980 CP_PARSER_FLAGS_OPTIONAL,
12982 &declares_class_or_enum);
12983 /* If there was exactly one decl-specifier, and it declared a class,
12984 and there's no declarator, then we have an explicit type
12986 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12990 type = check_tag_decl (&decl_specifiers);
12991 /* Turn access control back on for names used during
12992 template instantiation. */
12993 pop_deferring_access_checks ();
12995 do_type_instantiation (type, extension_specifier,
12996 /*complain=*/tf_error);
13000 cp_declarator *declarator;
13003 /* Parse the declarator. */
13005 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13006 /*ctor_dtor_or_conv_p=*/NULL,
13007 /*parenthesized_p=*/NULL,
13008 /*member_p=*/false);
13009 if (declares_class_or_enum & 2)
13010 cp_parser_check_for_definition_in_return_type (declarator,
13011 decl_specifiers.type,
13012 decl_specifiers.type_location);
13013 if (declarator != cp_error_declarator)
13015 if (decl_specifiers.specs[(int)ds_inline])
13016 permerror (input_location, "explicit instantiation shall not use"
13017 " %<inline%> specifier");
13018 if (decl_specifiers.specs[(int)ds_constexpr])
13019 permerror (input_location, "explicit instantiation shall not use"
13020 " %<constexpr%> specifier");
13022 decl = grokdeclarator (declarator, &decl_specifiers,
13023 NORMAL, 0, &decl_specifiers.attributes);
13024 /* Turn access control back on for names used during
13025 template instantiation. */
13026 pop_deferring_access_checks ();
13027 /* Do the explicit instantiation. */
13028 do_decl_instantiation (decl, extension_specifier);
13032 pop_deferring_access_checks ();
13033 /* Skip the body of the explicit instantiation. */
13034 cp_parser_skip_to_end_of_statement (parser);
13037 /* We're done with the instantiation. */
13038 end_explicit_instantiation ();
13040 cp_parser_consume_semicolon_at_end_of_statement (parser);
13042 timevar_pop (TV_TEMPLATE_INST);
13045 /* Parse an explicit-specialization.
13047 explicit-specialization:
13048 template < > declaration
13050 Although the standard says `declaration', what it really means is:
13052 explicit-specialization:
13053 template <> decl-specifier [opt] init-declarator [opt] ;
13054 template <> function-definition
13055 template <> explicit-specialization
13056 template <> template-declaration */
13059 cp_parser_explicit_specialization (cp_parser* parser)
13061 bool need_lang_pop;
13062 cp_token *token = cp_lexer_peek_token (parser->lexer);
13064 /* Look for the `template' keyword. */
13065 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13066 /* Look for the `<'. */
13067 cp_parser_require (parser, CPP_LESS, RT_LESS);
13068 /* Look for the `>'. */
13069 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13070 /* We have processed another parameter list. */
13071 ++parser->num_template_parameter_lists;
13074 A template ... explicit specialization ... shall not have C
13076 if (current_lang_name == lang_name_c)
13078 error_at (token->location, "template specialization with C linkage");
13079 /* Give it C++ linkage to avoid confusing other parts of the
13081 push_lang_context (lang_name_cplusplus);
13082 need_lang_pop = true;
13085 need_lang_pop = false;
13086 /* Let the front end know that we are beginning a specialization. */
13087 if (!begin_specialization ())
13089 end_specialization ();
13093 /* If the next keyword is `template', we need to figure out whether
13094 or not we're looking a template-declaration. */
13095 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13097 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13098 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13099 cp_parser_template_declaration_after_export (parser,
13100 /*member_p=*/false);
13102 cp_parser_explicit_specialization (parser);
13105 /* Parse the dependent declaration. */
13106 cp_parser_single_declaration (parser,
13108 /*member_p=*/false,
13109 /*explicit_specialization_p=*/true,
13110 /*friend_p=*/NULL);
13111 /* We're done with the specialization. */
13112 end_specialization ();
13113 /* For the erroneous case of a template with C linkage, we pushed an
13114 implicit C++ linkage scope; exit that scope now. */
13116 pop_lang_context ();
13117 /* We're done with this parameter list. */
13118 --parser->num_template_parameter_lists;
13121 /* Parse a type-specifier.
13124 simple-type-specifier
13127 elaborated-type-specifier
13135 Returns a representation of the type-specifier. For a
13136 class-specifier, enum-specifier, or elaborated-type-specifier, a
13137 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13139 The parser flags FLAGS is used to control type-specifier parsing.
13141 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13142 in a decl-specifier-seq.
13144 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13145 class-specifier, enum-specifier, or elaborated-type-specifier, then
13146 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13147 if a type is declared; 2 if it is defined. Otherwise, it is set to
13150 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13151 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13152 is set to FALSE. */
13155 cp_parser_type_specifier (cp_parser* parser,
13156 cp_parser_flags flags,
13157 cp_decl_specifier_seq *decl_specs,
13158 bool is_declaration,
13159 int* declares_class_or_enum,
13160 bool* is_cv_qualifier)
13162 tree type_spec = NULL_TREE;
13165 cp_decl_spec ds = ds_last;
13167 /* Assume this type-specifier does not declare a new type. */
13168 if (declares_class_or_enum)
13169 *declares_class_or_enum = 0;
13170 /* And that it does not specify a cv-qualifier. */
13171 if (is_cv_qualifier)
13172 *is_cv_qualifier = false;
13173 /* Peek at the next token. */
13174 token = cp_lexer_peek_token (parser->lexer);
13176 /* If we're looking at a keyword, we can use that to guide the
13177 production we choose. */
13178 keyword = token->keyword;
13182 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13183 goto elaborated_type_specifier;
13185 /* Look for the enum-specifier. */
13186 type_spec = cp_parser_enum_specifier (parser);
13187 /* If that worked, we're done. */
13190 if (declares_class_or_enum)
13191 *declares_class_or_enum = 2;
13193 cp_parser_set_decl_spec_type (decl_specs,
13196 /*type_definition_p=*/true);
13200 goto elaborated_type_specifier;
13202 /* Any of these indicate either a class-specifier, or an
13203 elaborated-type-specifier. */
13207 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13208 goto elaborated_type_specifier;
13210 /* Parse tentatively so that we can back up if we don't find a
13211 class-specifier. */
13212 cp_parser_parse_tentatively (parser);
13213 /* Look for the class-specifier. */
13214 type_spec = cp_parser_class_specifier (parser);
13215 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13216 /* If that worked, we're done. */
13217 if (cp_parser_parse_definitely (parser))
13219 if (declares_class_or_enum)
13220 *declares_class_or_enum = 2;
13222 cp_parser_set_decl_spec_type (decl_specs,
13225 /*type_definition_p=*/true);
13229 /* Fall through. */
13230 elaborated_type_specifier:
13231 /* We're declaring (not defining) a class or enum. */
13232 if (declares_class_or_enum)
13233 *declares_class_or_enum = 1;
13235 /* Fall through. */
13237 /* Look for an elaborated-type-specifier. */
13239 = (cp_parser_elaborated_type_specifier
13241 decl_specs && decl_specs->specs[(int) ds_friend],
13244 cp_parser_set_decl_spec_type (decl_specs,
13247 /*type_definition_p=*/false);
13252 if (is_cv_qualifier)
13253 *is_cv_qualifier = true;
13258 if (is_cv_qualifier)
13259 *is_cv_qualifier = true;
13264 if (is_cv_qualifier)
13265 *is_cv_qualifier = true;
13269 /* The `__complex__' keyword is a GNU extension. */
13277 /* Handle simple keywords. */
13282 ++decl_specs->specs[(int)ds];
13283 decl_specs->any_specifiers_p = true;
13285 return cp_lexer_consume_token (parser->lexer)->u.value;
13288 /* If we do not already have a type-specifier, assume we are looking
13289 at a simple-type-specifier. */
13290 type_spec = cp_parser_simple_type_specifier (parser,
13294 /* If we didn't find a type-specifier, and a type-specifier was not
13295 optional in this context, issue an error message. */
13296 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13298 cp_parser_error (parser, "expected type specifier");
13299 return error_mark_node;
13305 /* Parse a simple-type-specifier.
13307 simple-type-specifier:
13308 :: [opt] nested-name-specifier [opt] type-name
13309 :: [opt] nested-name-specifier template template-id
13324 simple-type-specifier:
13326 decltype ( expression )
13329 __underlying_type ( type-id )
13333 simple-type-specifier:
13335 __typeof__ unary-expression
13336 __typeof__ ( type-id )
13338 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13339 appropriately updated. */
13342 cp_parser_simple_type_specifier (cp_parser* parser,
13343 cp_decl_specifier_seq *decl_specs,
13344 cp_parser_flags flags)
13346 tree type = NULL_TREE;
13349 /* Peek at the next token. */
13350 token = cp_lexer_peek_token (parser->lexer);
13352 /* If we're looking at a keyword, things are easy. */
13353 switch (token->keyword)
13357 decl_specs->explicit_char_p = true;
13358 type = char_type_node;
13361 type = char16_type_node;
13364 type = char32_type_node;
13367 type = wchar_type_node;
13370 type = boolean_type_node;
13374 ++decl_specs->specs[(int) ds_short];
13375 type = short_integer_type_node;
13379 decl_specs->explicit_int_p = true;
13380 type = integer_type_node;
13383 if (!int128_integer_type_node)
13386 decl_specs->explicit_int128_p = true;
13387 type = int128_integer_type_node;
13391 ++decl_specs->specs[(int) ds_long];
13392 type = long_integer_type_node;
13396 ++decl_specs->specs[(int) ds_signed];
13397 type = integer_type_node;
13401 ++decl_specs->specs[(int) ds_unsigned];
13402 type = unsigned_type_node;
13405 type = float_type_node;
13408 type = double_type_node;
13411 type = void_type_node;
13415 maybe_warn_cpp0x (CPP0X_AUTO);
13416 type = make_auto ();
13420 /* Since DR 743, decltype can either be a simple-type-specifier by
13421 itself or begin a nested-name-specifier. Parsing it will replace
13422 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13423 handling below decide what to do. */
13424 cp_parser_decltype (parser);
13425 cp_lexer_set_token_position (parser->lexer, token);
13429 /* Consume the `typeof' token. */
13430 cp_lexer_consume_token (parser->lexer);
13431 /* Parse the operand to `typeof'. */
13432 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13433 /* If it is not already a TYPE, take its type. */
13434 if (!TYPE_P (type))
13435 type = finish_typeof (type);
13438 cp_parser_set_decl_spec_type (decl_specs, type,
13440 /*type_definition_p=*/false);
13444 case RID_UNDERLYING_TYPE:
13445 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13447 cp_parser_set_decl_spec_type (decl_specs, type,
13449 /*type_definition_p=*/false);
13454 case RID_DIRECT_BASES:
13455 type = cp_parser_trait_expr (parser, token->keyword);
13457 cp_parser_set_decl_spec_type (decl_specs, type,
13459 /*type_definition_p=*/false);
13465 /* If token is an already-parsed decltype not followed by ::,
13466 it's a simple-type-specifier. */
13467 if (token->type == CPP_DECLTYPE
13468 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13470 type = token->u.value;
13472 cp_parser_set_decl_spec_type (decl_specs, type,
13474 /*type_definition_p=*/false);
13475 cp_lexer_consume_token (parser->lexer);
13479 /* If the type-specifier was for a built-in type, we're done. */
13482 /* Record the type. */
13484 && (token->keyword != RID_SIGNED
13485 && token->keyword != RID_UNSIGNED
13486 && token->keyword != RID_SHORT
13487 && token->keyword != RID_LONG))
13488 cp_parser_set_decl_spec_type (decl_specs,
13491 /*type_definition_p=*/false);
13493 decl_specs->any_specifiers_p = true;
13495 /* Consume the token. */
13496 cp_lexer_consume_token (parser->lexer);
13498 /* There is no valid C++ program where a non-template type is
13499 followed by a "<". That usually indicates that the user thought
13500 that the type was a template. */
13501 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13503 return TYPE_NAME (type);
13506 /* The type-specifier must be a user-defined type. */
13507 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13512 /* Don't gobble tokens or issue error messages if this is an
13513 optional type-specifier. */
13514 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13515 cp_parser_parse_tentatively (parser);
13517 /* Look for the optional `::' operator. */
13519 = (cp_parser_global_scope_opt (parser,
13520 /*current_scope_valid_p=*/false)
13522 /* Look for the nested-name specifier. */
13524 = (cp_parser_nested_name_specifier_opt (parser,
13525 /*typename_keyword_p=*/false,
13526 /*check_dependency_p=*/true,
13528 /*is_declaration=*/false)
13530 token = cp_lexer_peek_token (parser->lexer);
13531 /* If we have seen a nested-name-specifier, and the next token
13532 is `template', then we are using the template-id production. */
13534 && cp_parser_optional_template_keyword (parser))
13536 /* Look for the template-id. */
13537 type = cp_parser_template_id (parser,
13538 /*template_keyword_p=*/true,
13539 /*check_dependency_p=*/true,
13540 /*is_declaration=*/false);
13541 /* If the template-id did not name a type, we are out of
13543 if (TREE_CODE (type) != TYPE_DECL)
13545 cp_parser_error (parser, "expected template-id for type");
13549 /* Otherwise, look for a type-name. */
13551 type = cp_parser_type_name (parser);
13552 /* Keep track of all name-lookups performed in class scopes. */
13556 && TREE_CODE (type) == TYPE_DECL
13557 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13558 maybe_note_name_used_in_class (DECL_NAME (type), type);
13559 /* If it didn't work out, we don't have a TYPE. */
13560 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13561 && !cp_parser_parse_definitely (parser))
13563 if (type && decl_specs)
13564 cp_parser_set_decl_spec_type (decl_specs, type,
13566 /*type_definition_p=*/false);
13569 /* If we didn't get a type-name, issue an error message. */
13570 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13572 cp_parser_error (parser, "expected type-name");
13573 return error_mark_node;
13576 if (type && type != error_mark_node)
13578 /* See if TYPE is an Objective-C type, and if so, parse and
13579 accept any protocol references following it. Do this before
13580 the cp_parser_check_for_invalid_template_id() call, because
13581 Objective-C types can be followed by '<...>' which would
13582 enclose protocol names rather than template arguments, and so
13583 everything is fine. */
13584 if (c_dialect_objc () && !parser->scope
13585 && (objc_is_id (type) || objc_is_class_name (type)))
13587 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13588 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13590 /* Clobber the "unqualified" type previously entered into
13591 DECL_SPECS with the new, improved protocol-qualified version. */
13593 decl_specs->type = qual_type;
13598 /* There is no valid C++ program where a non-template type is
13599 followed by a "<". That usually indicates that the user
13600 thought that the type was a template. */
13601 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13608 /* Parse a type-name.
13621 Returns a TYPE_DECL for the type. */
13624 cp_parser_type_name (cp_parser* parser)
13628 /* We can't know yet whether it is a class-name or not. */
13629 cp_parser_parse_tentatively (parser);
13630 /* Try a class-name. */
13631 type_decl = cp_parser_class_name (parser,
13632 /*typename_keyword_p=*/false,
13633 /*template_keyword_p=*/false,
13635 /*check_dependency_p=*/true,
13636 /*class_head_p=*/false,
13637 /*is_declaration=*/false);
13638 /* If it's not a class-name, keep looking. */
13639 if (!cp_parser_parse_definitely (parser))
13641 /* It must be a typedef-name or an enum-name. */
13642 return cp_parser_nonclass_name (parser);
13648 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13656 Returns a TYPE_DECL for the type. */
13659 cp_parser_nonclass_name (cp_parser* parser)
13664 cp_token *token = cp_lexer_peek_token (parser->lexer);
13665 identifier = cp_parser_identifier (parser);
13666 if (identifier == error_mark_node)
13667 return error_mark_node;
13669 /* Look up the type-name. */
13670 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13672 if (TREE_CODE (type_decl) != TYPE_DECL
13673 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13675 /* See if this is an Objective-C type. */
13676 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13677 tree type = objc_get_protocol_qualified_type (identifier, protos);
13679 type_decl = TYPE_NAME (type);
13682 /* Issue an error if we did not find a type-name. */
13683 if (TREE_CODE (type_decl) != TYPE_DECL
13684 /* In Objective-C, we have the complication that class names are
13685 normally type names and start declarations (eg, the
13686 "NSObject" in "NSObject *object;"), but can be used in an
13687 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13688 is an expression. So, a classname followed by a dot is not a
13689 valid type-name. */
13690 || (objc_is_class_name (TREE_TYPE (type_decl))
13691 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13693 if (!cp_parser_simulate_error (parser))
13694 cp_parser_name_lookup_error (parser, identifier, type_decl,
13695 NLE_TYPE, token->location);
13696 return error_mark_node;
13698 /* Remember that the name was used in the definition of the
13699 current class so that we can check later to see if the
13700 meaning would have been different after the class was
13701 entirely defined. */
13702 else if (type_decl != error_mark_node
13704 maybe_note_name_used_in_class (identifier, type_decl);
13709 /* Parse an elaborated-type-specifier. Note that the grammar given
13710 here incorporates the resolution to DR68.
13712 elaborated-type-specifier:
13713 class-key :: [opt] nested-name-specifier [opt] identifier
13714 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13715 enum-key :: [opt] nested-name-specifier [opt] identifier
13716 typename :: [opt] nested-name-specifier identifier
13717 typename :: [opt] nested-name-specifier template [opt]
13722 elaborated-type-specifier:
13723 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13724 class-key attributes :: [opt] nested-name-specifier [opt]
13725 template [opt] template-id
13726 enum attributes :: [opt] nested-name-specifier [opt] identifier
13728 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13729 declared `friend'. If IS_DECLARATION is TRUE, then this
13730 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13731 something is being declared.
13733 Returns the TYPE specified. */
13736 cp_parser_elaborated_type_specifier (cp_parser* parser,
13738 bool is_declaration)
13740 enum tag_types tag_type;
13742 tree type = NULL_TREE;
13743 tree attributes = NULL_TREE;
13745 cp_token *token = NULL;
13747 /* See if we're looking at the `enum' keyword. */
13748 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13750 /* Consume the `enum' token. */
13751 cp_lexer_consume_token (parser->lexer);
13752 /* Remember that it's an enumeration type. */
13753 tag_type = enum_type;
13754 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13755 enums) is used here. */
13756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13757 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13759 pedwarn (input_location, 0, "elaborated-type-specifier "
13760 "for a scoped enum must not use the %<%D%> keyword",
13761 cp_lexer_peek_token (parser->lexer)->u.value);
13762 /* Consume the `struct' or `class' and parse it anyway. */
13763 cp_lexer_consume_token (parser->lexer);
13765 /* Parse the attributes. */
13766 attributes = cp_parser_attributes_opt (parser);
13768 /* Or, it might be `typename'. */
13769 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13772 /* Consume the `typename' token. */
13773 cp_lexer_consume_token (parser->lexer);
13774 /* Remember that it's a `typename' type. */
13775 tag_type = typename_type;
13777 /* Otherwise it must be a class-key. */
13780 tag_type = cp_parser_class_key (parser);
13781 if (tag_type == none_type)
13782 return error_mark_node;
13783 /* Parse the attributes. */
13784 attributes = cp_parser_attributes_opt (parser);
13787 /* Look for the `::' operator. */
13788 globalscope = cp_parser_global_scope_opt (parser,
13789 /*current_scope_valid_p=*/false);
13790 /* Look for the nested-name-specifier. */
13791 if (tag_type == typename_type && !globalscope)
13793 if (!cp_parser_nested_name_specifier (parser,
13794 /*typename_keyword_p=*/true,
13795 /*check_dependency_p=*/true,
13798 return error_mark_node;
13801 /* Even though `typename' is not present, the proposed resolution
13802 to Core Issue 180 says that in `class A<T>::B', `B' should be
13803 considered a type-name, even if `A<T>' is dependent. */
13804 cp_parser_nested_name_specifier_opt (parser,
13805 /*typename_keyword_p=*/true,
13806 /*check_dependency_p=*/true,
13809 /* For everything but enumeration types, consider a template-id.
13810 For an enumeration type, consider only a plain identifier. */
13811 if (tag_type != enum_type)
13813 bool template_p = false;
13816 /* Allow the `template' keyword. */
13817 template_p = cp_parser_optional_template_keyword (parser);
13818 /* If we didn't see `template', we don't know if there's a
13819 template-id or not. */
13821 cp_parser_parse_tentatively (parser);
13822 /* Parse the template-id. */
13823 token = cp_lexer_peek_token (parser->lexer);
13824 decl = cp_parser_template_id (parser, template_p,
13825 /*check_dependency_p=*/true,
13827 /* If we didn't find a template-id, look for an ordinary
13829 if (!template_p && !cp_parser_parse_definitely (parser))
13831 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13832 in effect, then we must assume that, upon instantiation, the
13833 template will correspond to a class. */
13834 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13835 && tag_type == typename_type)
13836 type = make_typename_type (parser->scope, decl,
13838 /*complain=*/tf_error);
13839 /* If the `typename' keyword is in effect and DECL is not a type
13840 decl. Then type is non existant. */
13841 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13844 type = TREE_TYPE (decl);
13849 token = cp_lexer_peek_token (parser->lexer);
13850 identifier = cp_parser_identifier (parser);
13852 if (identifier == error_mark_node)
13854 parser->scope = NULL_TREE;
13855 return error_mark_node;
13858 /* For a `typename', we needn't call xref_tag. */
13859 if (tag_type == typename_type
13860 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13861 return cp_parser_make_typename_type (parser, parser->scope,
13864 /* Look up a qualified name in the usual way. */
13868 tree ambiguous_decls;
13870 decl = cp_parser_lookup_name (parser, identifier,
13872 /*is_template=*/false,
13873 /*is_namespace=*/false,
13874 /*check_dependency=*/true,
13878 /* If the lookup was ambiguous, an error will already have been
13880 if (ambiguous_decls)
13881 return error_mark_node;
13883 /* If we are parsing friend declaration, DECL may be a
13884 TEMPLATE_DECL tree node here. However, we need to check
13885 whether this TEMPLATE_DECL results in valid code. Consider
13886 the following example:
13889 template <class T> class C {};
13892 template <class T> friend class N::C; // #1, valid code
13894 template <class T> class Y {
13895 friend class N::C; // #2, invalid code
13898 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13899 name lookup of `N::C'. We see that friend declaration must
13900 be template for the code to be valid. Note that
13901 processing_template_decl does not work here since it is
13902 always 1 for the above two cases. */
13904 decl = (cp_parser_maybe_treat_template_as_class
13905 (decl, /*tag_name_p=*/is_friend
13906 && parser->num_template_parameter_lists));
13908 if (TREE_CODE (decl) != TYPE_DECL)
13910 cp_parser_diagnose_invalid_type_name (parser,
13914 return error_mark_node;
13917 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13919 bool allow_template = (parser->num_template_parameter_lists
13920 || DECL_SELF_REFERENCE_P (decl));
13921 type = check_elaborated_type_specifier (tag_type, decl,
13924 if (type == error_mark_node)
13925 return error_mark_node;
13928 /* Forward declarations of nested types, such as
13933 are invalid unless all components preceding the final '::'
13934 are complete. If all enclosing types are complete, these
13935 declarations become merely pointless.
13937 Invalid forward declarations of nested types are errors
13938 caught elsewhere in parsing. Those that are pointless arrive
13941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13942 && !is_friend && !processing_explicit_instantiation)
13943 warning (0, "declaration %qD does not declare anything", decl);
13945 type = TREE_TYPE (decl);
13949 /* An elaborated-type-specifier sometimes introduces a new type and
13950 sometimes names an existing type. Normally, the rule is that it
13951 introduces a new type only if there is not an existing type of
13952 the same name already in scope. For example, given:
13955 void f() { struct S s; }
13957 the `struct S' in the body of `f' is the same `struct S' as in
13958 the global scope; the existing definition is used. However, if
13959 there were no global declaration, this would introduce a new
13960 local class named `S'.
13962 An exception to this rule applies to the following code:
13964 namespace N { struct S; }
13966 Here, the elaborated-type-specifier names a new type
13967 unconditionally; even if there is already an `S' in the
13968 containing scope this declaration names a new type.
13969 This exception only applies if the elaborated-type-specifier
13970 forms the complete declaration:
13974 A declaration consisting solely of `class-key identifier ;' is
13975 either a redeclaration of the name in the current scope or a
13976 forward declaration of the identifier as a class name. It
13977 introduces the name into the current scope.
13979 We are in this situation precisely when the next token is a `;'.
13981 An exception to the exception is that a `friend' declaration does
13982 *not* name a new type; i.e., given:
13984 struct S { friend struct T; };
13986 `T' is not a new type in the scope of `S'.
13988 Also, `new struct S' or `sizeof (struct S)' never results in the
13989 definition of a new type; a new type can only be declared in a
13990 declaration context. */
13996 /* Friends have special name lookup rules. */
13997 ts = ts_within_enclosing_non_class;
13998 else if (is_declaration
13999 && cp_lexer_next_token_is (parser->lexer,
14001 /* This is a `class-key identifier ;' */
14007 (parser->num_template_parameter_lists
14008 && (cp_parser_next_token_starts_class_definition_p (parser)
14009 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14010 /* An unqualified name was used to reference this type, so
14011 there were no qualifying templates. */
14012 if (!cp_parser_check_template_parameters (parser,
14013 /*num_templates=*/0,
14015 /*declarator=*/NULL))
14016 return error_mark_node;
14017 type = xref_tag (tag_type, identifier, ts, template_p);
14021 if (type == error_mark_node)
14022 return error_mark_node;
14024 /* Allow attributes on forward declarations of classes. */
14027 if (TREE_CODE (type) == TYPENAME_TYPE)
14028 warning (OPT_Wattributes,
14029 "attributes ignored on uninstantiated type");
14030 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14031 && ! processing_explicit_instantiation)
14032 warning (OPT_Wattributes,
14033 "attributes ignored on template instantiation");
14034 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14035 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14037 warning (OPT_Wattributes,
14038 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14041 if (tag_type != enum_type)
14043 /* Indicate whether this class was declared as a `class' or as a
14045 if (TREE_CODE (type) == RECORD_TYPE)
14046 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14047 cp_parser_check_class_key (tag_type, type);
14050 /* A "<" cannot follow an elaborated type specifier. If that
14051 happens, the user was probably trying to form a template-id. */
14052 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14057 /* Parse an enum-specifier.
14060 enum-head { enumerator-list [opt] }
14063 enum-key identifier [opt] enum-base [opt]
14064 enum-key nested-name-specifier identifier enum-base [opt]
14069 enum struct [C++0x]
14072 : type-specifier-seq
14074 opaque-enum-specifier:
14075 enum-key identifier enum-base [opt] ;
14078 enum-key attributes[opt] identifier [opt] enum-base [opt]
14079 { enumerator-list [opt] }attributes[opt]
14081 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14082 if the token stream isn't an enum-specifier after all. */
14085 cp_parser_enum_specifier (cp_parser* parser)
14088 tree type = NULL_TREE;
14090 tree nested_name_specifier = NULL_TREE;
14092 bool scoped_enum_p = false;
14093 bool has_underlying_type = false;
14094 bool nested_being_defined = false;
14095 bool new_value_list = false;
14096 bool is_new_type = false;
14097 bool is_anonymous = false;
14098 tree underlying_type = NULL_TREE;
14099 cp_token *type_start_token = NULL;
14100 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14102 parser->colon_corrects_to_scope_p = false;
14104 /* Parse tentatively so that we can back up if we don't find a
14106 cp_parser_parse_tentatively (parser);
14108 /* Caller guarantees that the current token is 'enum', an identifier
14109 possibly follows, and the token after that is an opening brace.
14110 If we don't have an identifier, fabricate an anonymous name for
14111 the enumeration being defined. */
14112 cp_lexer_consume_token (parser->lexer);
14114 /* Parse the "class" or "struct", which indicates a scoped
14115 enumeration type in C++0x. */
14116 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14117 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14119 if (cxx_dialect < cxx0x)
14120 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14122 /* Consume the `struct' or `class' token. */
14123 cp_lexer_consume_token (parser->lexer);
14125 scoped_enum_p = true;
14128 attributes = cp_parser_attributes_opt (parser);
14130 /* Clear the qualification. */
14131 parser->scope = NULL_TREE;
14132 parser->qualifying_scope = NULL_TREE;
14133 parser->object_scope = NULL_TREE;
14135 /* Figure out in what scope the declaration is being placed. */
14136 prev_scope = current_scope ();
14138 type_start_token = cp_lexer_peek_token (parser->lexer);
14140 push_deferring_access_checks (dk_no_check);
14141 nested_name_specifier
14142 = cp_parser_nested_name_specifier_opt (parser,
14143 /*typename_keyword_p=*/true,
14144 /*check_dependency_p=*/false,
14146 /*is_declaration=*/false);
14148 if (nested_name_specifier)
14152 identifier = cp_parser_identifier (parser);
14153 name = cp_parser_lookup_name (parser, identifier,
14155 /*is_template=*/false,
14156 /*is_namespace=*/false,
14157 /*check_dependency=*/true,
14158 /*ambiguous_decls=*/NULL,
14162 type = TREE_TYPE (name);
14163 if (TREE_CODE (type) == TYPENAME_TYPE)
14165 /* Are template enums allowed in ISO? */
14166 if (template_parm_scope_p ())
14167 pedwarn (type_start_token->location, OPT_pedantic,
14168 "%qD is an enumeration template", name);
14169 /* ignore a typename reference, for it will be solved by name
14175 error_at (type_start_token->location,
14176 "%qD is not an enumerator-name", identifier);
14180 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14181 identifier = cp_parser_identifier (parser);
14184 identifier = make_anon_name ();
14185 is_anonymous = true;
14188 pop_deferring_access_checks ();
14190 /* Check for the `:' that denotes a specified underlying type in C++0x.
14191 Note that a ':' could also indicate a bitfield width, however. */
14192 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14194 cp_decl_specifier_seq type_specifiers;
14196 /* Consume the `:'. */
14197 cp_lexer_consume_token (parser->lexer);
14199 /* Parse the type-specifier-seq. */
14200 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14201 /*is_trailing_return=*/false,
14204 /* At this point this is surely not elaborated type specifier. */
14205 if (!cp_parser_parse_definitely (parser))
14208 if (cxx_dialect < cxx0x)
14209 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14211 has_underlying_type = true;
14213 /* If that didn't work, stop. */
14214 if (type_specifiers.type != error_mark_node)
14216 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14217 /*initialized=*/0, NULL);
14218 if (underlying_type == error_mark_node)
14219 underlying_type = NULL_TREE;
14223 /* Look for the `{' but don't consume it yet. */
14224 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14226 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14228 cp_parser_error (parser, "expected %<{%>");
14229 if (has_underlying_type)
14235 /* An opaque-enum-specifier must have a ';' here. */
14236 if ((scoped_enum_p || underlying_type)
14237 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14239 cp_parser_error (parser, "expected %<;%> or %<{%>");
14240 if (has_underlying_type)
14248 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14251 if (nested_name_specifier)
14253 if (CLASS_TYPE_P (nested_name_specifier))
14255 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14256 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14257 push_scope (nested_name_specifier);
14259 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14261 push_nested_namespace (nested_name_specifier);
14265 /* Issue an error message if type-definitions are forbidden here. */
14266 if (!cp_parser_check_type_definition (parser))
14267 type = error_mark_node;
14269 /* Create the new type. We do this before consuming the opening
14270 brace so the enum will be recorded as being on the line of its
14271 tag (or the 'enum' keyword, if there is no tag). */
14272 type = start_enum (identifier, type, underlying_type,
14273 scoped_enum_p, &is_new_type);
14275 /* If the next token is not '{' it is an opaque-enum-specifier or an
14276 elaborated-type-specifier. */
14277 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14279 timevar_push (TV_PARSE_ENUM);
14280 if (nested_name_specifier)
14282 /* The following catches invalid code such as:
14283 enum class S<int>::E { A, B, C }; */
14284 if (!processing_specialization
14285 && CLASS_TYPE_P (nested_name_specifier)
14286 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14287 error_at (type_start_token->location, "cannot add an enumerator "
14288 "list to a template instantiation");
14290 /* If that scope does not contain the scope in which the
14291 class was originally declared, the program is invalid. */
14292 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14294 if (at_namespace_scope_p ())
14295 error_at (type_start_token->location,
14296 "declaration of %qD in namespace %qD which does not "
14298 type, prev_scope, nested_name_specifier);
14300 error_at (type_start_token->location,
14301 "declaration of %qD in %qD which does not enclose %qD",
14302 type, prev_scope, nested_name_specifier);
14303 type = error_mark_node;
14308 begin_scope (sk_scoped_enum, type);
14310 /* Consume the opening brace. */
14311 cp_lexer_consume_token (parser->lexer);
14313 if (type == error_mark_node)
14314 ; /* Nothing to add */
14315 else if (OPAQUE_ENUM_P (type)
14316 || (cxx_dialect > cxx98 && processing_specialization))
14318 new_value_list = true;
14319 SET_OPAQUE_ENUM_P (type, false);
14320 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14324 error_at (type_start_token->location, "multiple definition of %q#T", type);
14325 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14326 "previous definition here");
14327 type = error_mark_node;
14330 if (type == error_mark_node)
14331 cp_parser_skip_to_end_of_block_or_statement (parser);
14332 /* If the next token is not '}', then there are some enumerators. */
14333 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14334 cp_parser_enumerator_list (parser, type);
14336 /* Consume the final '}'. */
14337 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14341 timevar_pop (TV_PARSE_ENUM);
14345 /* If a ';' follows, then it is an opaque-enum-specifier
14346 and additional restrictions apply. */
14347 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14350 error_at (type_start_token->location,
14351 "opaque-enum-specifier without name");
14352 else if (nested_name_specifier)
14353 error_at (type_start_token->location,
14354 "opaque-enum-specifier must use a simple identifier");
14358 /* Look for trailing attributes to apply to this enumeration, and
14359 apply them if appropriate. */
14360 if (cp_parser_allow_gnu_extensions_p (parser))
14362 tree trailing_attr = cp_parser_attributes_opt (parser);
14363 trailing_attr = chainon (trailing_attr, attributes);
14364 cplus_decl_attributes (&type,
14366 (int) ATTR_FLAG_TYPE_IN_PLACE);
14369 /* Finish up the enumeration. */
14370 if (type != error_mark_node)
14372 if (new_value_list)
14373 finish_enum_value_list (type);
14375 finish_enum (type);
14378 if (nested_name_specifier)
14380 if (CLASS_TYPE_P (nested_name_specifier))
14382 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14383 pop_scope (nested_name_specifier);
14385 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14387 pop_nested_namespace (nested_name_specifier);
14391 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14395 /* Parse an enumerator-list. The enumerators all have the indicated
14399 enumerator-definition
14400 enumerator-list , enumerator-definition */
14403 cp_parser_enumerator_list (cp_parser* parser, tree type)
14407 /* Parse an enumerator-definition. */
14408 cp_parser_enumerator_definition (parser, type);
14410 /* If the next token is not a ',', we've reached the end of
14412 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14414 /* Otherwise, consume the `,' and keep going. */
14415 cp_lexer_consume_token (parser->lexer);
14416 /* If the next token is a `}', there is a trailing comma. */
14417 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14419 if (!in_system_header)
14420 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
14426 /* Parse an enumerator-definition. The enumerator has the indicated
14429 enumerator-definition:
14431 enumerator = constant-expression
14437 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14443 /* Save the input location because we are interested in the location
14444 of the identifier and not the location of the explicit value. */
14445 loc = cp_lexer_peek_token (parser->lexer)->location;
14447 /* Look for the identifier. */
14448 identifier = cp_parser_identifier (parser);
14449 if (identifier == error_mark_node)
14452 /* If the next token is an '=', then there is an explicit value. */
14453 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14455 /* Consume the `=' token. */
14456 cp_lexer_consume_token (parser->lexer);
14457 /* Parse the value. */
14458 value = cp_parser_constant_expression (parser,
14459 /*allow_non_constant_p=*/false,
14465 /* If we are processing a template, make sure the initializer of the
14466 enumerator doesn't contain any bare template parameter pack. */
14467 if (check_for_bare_parameter_packs (value))
14468 value = error_mark_node;
14470 /* integral_constant_value will pull out this expression, so make sure
14471 it's folded as appropriate. */
14472 value = fold_non_dependent_expr (value);
14474 /* Create the enumerator. */
14475 build_enumerator (identifier, value, type, loc);
14478 /* Parse a namespace-name.
14481 original-namespace-name
14484 Returns the NAMESPACE_DECL for the namespace. */
14487 cp_parser_namespace_name (cp_parser* parser)
14490 tree namespace_decl;
14492 cp_token *token = cp_lexer_peek_token (parser->lexer);
14494 /* Get the name of the namespace. */
14495 identifier = cp_parser_identifier (parser);
14496 if (identifier == error_mark_node)
14497 return error_mark_node;
14499 /* Look up the identifier in the currently active scope. Look only
14500 for namespaces, due to:
14502 [basic.lookup.udir]
14504 When looking up a namespace-name in a using-directive or alias
14505 definition, only namespace names are considered.
14509 [basic.lookup.qual]
14511 During the lookup of a name preceding the :: scope resolution
14512 operator, object, function, and enumerator names are ignored.
14514 (Note that cp_parser_qualifying_entity only calls this
14515 function if the token after the name is the scope resolution
14517 namespace_decl = cp_parser_lookup_name (parser, identifier,
14519 /*is_template=*/false,
14520 /*is_namespace=*/true,
14521 /*check_dependency=*/true,
14522 /*ambiguous_decls=*/NULL,
14524 /* If it's not a namespace, issue an error. */
14525 if (namespace_decl == error_mark_node
14526 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14528 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14529 error_at (token->location, "%qD is not a namespace-name", identifier);
14530 cp_parser_error (parser, "expected namespace-name");
14531 namespace_decl = error_mark_node;
14534 return namespace_decl;
14537 /* Parse a namespace-definition.
14539 namespace-definition:
14540 named-namespace-definition
14541 unnamed-namespace-definition
14543 named-namespace-definition:
14544 original-namespace-definition
14545 extension-namespace-definition
14547 original-namespace-definition:
14548 namespace identifier { namespace-body }
14550 extension-namespace-definition:
14551 namespace original-namespace-name { namespace-body }
14553 unnamed-namespace-definition:
14554 namespace { namespace-body } */
14557 cp_parser_namespace_definition (cp_parser* parser)
14559 tree identifier, attribs;
14560 bool has_visibility;
14563 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14565 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14567 cp_lexer_consume_token (parser->lexer);
14572 /* Look for the `namespace' keyword. */
14573 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14575 /* Get the name of the namespace. We do not attempt to distinguish
14576 between an original-namespace-definition and an
14577 extension-namespace-definition at this point. The semantic
14578 analysis routines are responsible for that. */
14579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14580 identifier = cp_parser_identifier (parser);
14582 identifier = NULL_TREE;
14584 /* Parse any specified attributes. */
14585 attribs = cp_parser_attributes_opt (parser);
14587 /* Look for the `{' to start the namespace. */
14588 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14589 /* Start the namespace. */
14590 push_namespace (identifier);
14592 /* "inline namespace" is equivalent to a stub namespace definition
14593 followed by a strong using directive. */
14596 tree name_space = current_namespace;
14597 /* Set up namespace association. */
14598 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14599 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14600 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14601 /* Import the contents of the inline namespace. */
14603 do_using_directive (name_space);
14604 push_namespace (identifier);
14607 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14609 /* Parse the body of the namespace. */
14610 cp_parser_namespace_body (parser);
14612 if (has_visibility)
14613 pop_visibility (1);
14615 /* Finish the namespace. */
14617 /* Look for the final `}'. */
14618 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14621 /* Parse a namespace-body.
14624 declaration-seq [opt] */
14627 cp_parser_namespace_body (cp_parser* parser)
14629 cp_parser_declaration_seq_opt (parser);
14632 /* Parse a namespace-alias-definition.
14634 namespace-alias-definition:
14635 namespace identifier = qualified-namespace-specifier ; */
14638 cp_parser_namespace_alias_definition (cp_parser* parser)
14641 tree namespace_specifier;
14643 cp_token *token = cp_lexer_peek_token (parser->lexer);
14645 /* Look for the `namespace' keyword. */
14646 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14647 /* Look for the identifier. */
14648 identifier = cp_parser_identifier (parser);
14649 if (identifier == error_mark_node)
14651 /* Look for the `=' token. */
14652 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14653 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14655 error_at (token->location, "%<namespace%> definition is not allowed here");
14656 /* Skip the definition. */
14657 cp_lexer_consume_token (parser->lexer);
14658 if (cp_parser_skip_to_closing_brace (parser))
14659 cp_lexer_consume_token (parser->lexer);
14662 cp_parser_require (parser, CPP_EQ, RT_EQ);
14663 /* Look for the qualified-namespace-specifier. */
14664 namespace_specifier
14665 = cp_parser_qualified_namespace_specifier (parser);
14666 /* Look for the `;' token. */
14667 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14669 /* Register the alias in the symbol table. */
14670 do_namespace_alias (identifier, namespace_specifier);
14673 /* Parse a qualified-namespace-specifier.
14675 qualified-namespace-specifier:
14676 :: [opt] nested-name-specifier [opt] namespace-name
14678 Returns a NAMESPACE_DECL corresponding to the specified
14682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14684 /* Look for the optional `::'. */
14685 cp_parser_global_scope_opt (parser,
14686 /*current_scope_valid_p=*/false);
14688 /* Look for the optional nested-name-specifier. */
14689 cp_parser_nested_name_specifier_opt (parser,
14690 /*typename_keyword_p=*/false,
14691 /*check_dependency_p=*/true,
14693 /*is_declaration=*/true);
14695 return cp_parser_namespace_name (parser);
14698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14699 access declaration.
14702 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14703 using :: unqualified-id ;
14705 access-declaration:
14711 cp_parser_using_declaration (cp_parser* parser,
14712 bool access_declaration_p)
14715 bool typename_p = false;
14716 bool global_scope_p;
14721 if (access_declaration_p)
14722 cp_parser_parse_tentatively (parser);
14725 /* Look for the `using' keyword. */
14726 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14728 /* Peek at the next token. */
14729 token = cp_lexer_peek_token (parser->lexer);
14730 /* See if it's `typename'. */
14731 if (token->keyword == RID_TYPENAME)
14733 /* Remember that we've seen it. */
14735 /* Consume the `typename' token. */
14736 cp_lexer_consume_token (parser->lexer);
14740 /* Look for the optional global scope qualification. */
14742 = (cp_parser_global_scope_opt (parser,
14743 /*current_scope_valid_p=*/false)
14746 /* If we saw `typename', or didn't see `::', then there must be a
14747 nested-name-specifier present. */
14748 if (typename_p || !global_scope_p)
14749 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14750 /*check_dependency_p=*/true,
14752 /*is_declaration=*/true);
14753 /* Otherwise, we could be in either of the two productions. In that
14754 case, treat the nested-name-specifier as optional. */
14756 qscope = cp_parser_nested_name_specifier_opt (parser,
14757 /*typename_keyword_p=*/false,
14758 /*check_dependency_p=*/true,
14760 /*is_declaration=*/true);
14762 qscope = global_namespace;
14764 if (access_declaration_p && cp_parser_error_occurred (parser))
14765 /* Something has already gone wrong; there's no need to parse
14766 further. Since an error has occurred, the return value of
14767 cp_parser_parse_definitely will be false, as required. */
14768 return cp_parser_parse_definitely (parser);
14770 token = cp_lexer_peek_token (parser->lexer);
14771 /* Parse the unqualified-id. */
14772 identifier = cp_parser_unqualified_id (parser,
14773 /*template_keyword_p=*/false,
14774 /*check_dependency_p=*/true,
14775 /*declarator_p=*/true,
14776 /*optional_p=*/false);
14778 if (access_declaration_p)
14780 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14781 cp_parser_simulate_error (parser);
14782 if (!cp_parser_parse_definitely (parser))
14786 /* The function we call to handle a using-declaration is different
14787 depending on what scope we are in. */
14788 if (qscope == error_mark_node || identifier == error_mark_node)
14790 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14791 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14792 /* [namespace.udecl]
14794 A using declaration shall not name a template-id. */
14795 error_at (token->location,
14796 "a template-id may not appear in a using-declaration");
14799 if (at_class_scope_p ())
14801 /* Create the USING_DECL. */
14802 decl = do_class_using_decl (parser->scope, identifier);
14804 if (check_for_bare_parameter_packs (decl))
14807 /* Add it to the list of members in this class. */
14808 finish_member_declaration (decl);
14812 decl = cp_parser_lookup_name_simple (parser,
14815 if (decl == error_mark_node)
14816 cp_parser_name_lookup_error (parser, identifier,
14819 else if (check_for_bare_parameter_packs (decl))
14821 else if (!at_namespace_scope_p ())
14822 do_local_using_decl (decl, qscope, identifier);
14824 do_toplevel_using_decl (decl, qscope, identifier);
14828 /* Look for the final `;'. */
14829 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14834 /* Parse a using-directive.
14837 using namespace :: [opt] nested-name-specifier [opt]
14838 namespace-name ; */
14841 cp_parser_using_directive (cp_parser* parser)
14843 tree namespace_decl;
14846 /* Look for the `using' keyword. */
14847 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14848 /* And the `namespace' keyword. */
14849 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14850 /* Look for the optional `::' operator. */
14851 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14852 /* And the optional nested-name-specifier. */
14853 cp_parser_nested_name_specifier_opt (parser,
14854 /*typename_keyword_p=*/false,
14855 /*check_dependency_p=*/true,
14857 /*is_declaration=*/true);
14858 /* Get the namespace being used. */
14859 namespace_decl = cp_parser_namespace_name (parser);
14860 /* And any specified attributes. */
14861 attribs = cp_parser_attributes_opt (parser);
14862 /* Update the symbol table. */
14863 parse_using_directive (namespace_decl, attribs);
14864 /* Look for the final `;'. */
14865 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14868 /* Parse an asm-definition.
14871 asm ( string-literal ) ;
14876 asm volatile [opt] ( string-literal ) ;
14877 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14878 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14879 : asm-operand-list [opt] ) ;
14880 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14881 : asm-operand-list [opt]
14882 : asm-clobber-list [opt] ) ;
14883 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14884 : asm-clobber-list [opt]
14885 : asm-goto-list ) ; */
14888 cp_parser_asm_definition (cp_parser* parser)
14891 tree outputs = NULL_TREE;
14892 tree inputs = NULL_TREE;
14893 tree clobbers = NULL_TREE;
14894 tree labels = NULL_TREE;
14896 bool volatile_p = false;
14897 bool extended_p = false;
14898 bool invalid_inputs_p = false;
14899 bool invalid_outputs_p = false;
14900 bool goto_p = false;
14901 required_token missing = RT_NONE;
14903 /* Look for the `asm' keyword. */
14904 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14905 /* See if the next token is `volatile'. */
14906 if (cp_parser_allow_gnu_extensions_p (parser)
14907 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14909 /* Remember that we saw the `volatile' keyword. */
14911 /* Consume the token. */
14912 cp_lexer_consume_token (parser->lexer);
14914 if (cp_parser_allow_gnu_extensions_p (parser)
14915 && parser->in_function_body
14916 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14918 /* Remember that we saw the `goto' keyword. */
14920 /* Consume the token. */
14921 cp_lexer_consume_token (parser->lexer);
14923 /* Look for the opening `('. */
14924 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14926 /* Look for the string. */
14927 string = cp_parser_string_literal (parser, false, false);
14928 if (string == error_mark_node)
14930 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14931 /*consume_paren=*/true);
14935 /* If we're allowing GNU extensions, check for the extended assembly
14936 syntax. Unfortunately, the `:' tokens need not be separated by
14937 a space in C, and so, for compatibility, we tolerate that here
14938 too. Doing that means that we have to treat the `::' operator as
14940 if (cp_parser_allow_gnu_extensions_p (parser)
14941 && parser->in_function_body
14942 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14943 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14945 bool inputs_p = false;
14946 bool clobbers_p = false;
14947 bool labels_p = false;
14949 /* The extended syntax was used. */
14952 /* Look for outputs. */
14953 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14955 /* Consume the `:'. */
14956 cp_lexer_consume_token (parser->lexer);
14957 /* Parse the output-operands. */
14958 if (cp_lexer_next_token_is_not (parser->lexer,
14960 && cp_lexer_next_token_is_not (parser->lexer,
14962 && cp_lexer_next_token_is_not (parser->lexer,
14965 outputs = cp_parser_asm_operand_list (parser);
14967 if (outputs == error_mark_node)
14968 invalid_outputs_p = true;
14970 /* If the next token is `::', there are no outputs, and the
14971 next token is the beginning of the inputs. */
14972 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14973 /* The inputs are coming next. */
14976 /* Look for inputs. */
14978 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14980 /* Consume the `:' or `::'. */
14981 cp_lexer_consume_token (parser->lexer);
14982 /* Parse the output-operands. */
14983 if (cp_lexer_next_token_is_not (parser->lexer,
14985 && cp_lexer_next_token_is_not (parser->lexer,
14987 && cp_lexer_next_token_is_not (parser->lexer,
14989 inputs = cp_parser_asm_operand_list (parser);
14991 if (inputs == error_mark_node)
14992 invalid_inputs_p = true;
14994 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14995 /* The clobbers are coming next. */
14998 /* Look for clobbers. */
15000 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15003 /* Consume the `:' or `::'. */
15004 cp_lexer_consume_token (parser->lexer);
15005 /* Parse the clobbers. */
15006 if (cp_lexer_next_token_is_not (parser->lexer,
15008 && cp_lexer_next_token_is_not (parser->lexer,
15010 clobbers = cp_parser_asm_clobber_list (parser);
15013 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15014 /* The labels are coming next. */
15017 /* Look for labels. */
15019 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15022 /* Consume the `:' or `::'. */
15023 cp_lexer_consume_token (parser->lexer);
15024 /* Parse the labels. */
15025 labels = cp_parser_asm_label_list (parser);
15028 if (goto_p && !labels_p)
15029 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15032 missing = RT_COLON_SCOPE;
15034 /* Look for the closing `)'. */
15035 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15036 missing ? missing : RT_CLOSE_PAREN))
15037 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15038 /*consume_paren=*/true);
15039 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15041 if (!invalid_inputs_p && !invalid_outputs_p)
15043 /* Create the ASM_EXPR. */
15044 if (parser->in_function_body)
15046 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15047 inputs, clobbers, labels);
15048 /* If the extended syntax was not used, mark the ASM_EXPR. */
15051 tree temp = asm_stmt;
15052 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15053 temp = TREE_OPERAND (temp, 0);
15055 ASM_INPUT_P (temp) = 1;
15059 cgraph_add_asm_node (string);
15063 /* Declarators [gram.dcl.decl] */
15065 /* Parse an init-declarator.
15068 declarator initializer [opt]
15073 declarator asm-specification [opt] attributes [opt] initializer [opt]
15075 function-definition:
15076 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15078 decl-specifier-seq [opt] declarator function-try-block
15082 function-definition:
15083 __extension__ function-definition
15085 The DECL_SPECIFIERS apply to this declarator. Returns a
15086 representation of the entity declared. If MEMBER_P is TRUE, then
15087 this declarator appears in a class scope. The new DECL created by
15088 this declarator is returned.
15090 The CHECKS are access checks that should be performed once we know
15091 what entity is being declared (and, therefore, what classes have
15094 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15095 for a function-definition here as well. If the declarator is a
15096 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15097 be TRUE upon return. By that point, the function-definition will
15098 have been completely parsed.
15100 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15103 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15104 parsed declaration if it is an uninitialized single declarator not followed
15105 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15106 if present, will not be consumed. If returned, this declarator will be
15107 created with SD_INITIALIZED but will not call cp_finish_decl. */
15110 cp_parser_init_declarator (cp_parser* parser,
15111 cp_decl_specifier_seq *decl_specifiers,
15112 VEC (deferred_access_check,gc)* checks,
15113 bool function_definition_allowed_p,
15115 int declares_class_or_enum,
15116 bool* function_definition_p,
15117 tree* maybe_range_for_decl)
15119 cp_token *token = NULL, *asm_spec_start_token = NULL,
15120 *attributes_start_token = NULL;
15121 cp_declarator *declarator;
15122 tree prefix_attributes;
15124 tree asm_specification;
15126 tree decl = NULL_TREE;
15128 int is_initialized;
15129 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15130 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15132 enum cpp_ttype initialization_kind;
15133 bool is_direct_init = false;
15134 bool is_non_constant_init;
15135 int ctor_dtor_or_conv_p;
15137 tree pushed_scope = NULL_TREE;
15138 bool range_for_decl_p = false;
15140 /* Gather the attributes that were provided with the
15141 decl-specifiers. */
15142 prefix_attributes = decl_specifiers->attributes;
15144 /* Assume that this is not the declarator for a function
15146 if (function_definition_p)
15147 *function_definition_p = false;
15149 /* Defer access checks while parsing the declarator; we cannot know
15150 what names are accessible until we know what is being
15152 resume_deferring_access_checks ();
15154 /* Parse the declarator. */
15155 token = cp_lexer_peek_token (parser->lexer);
15157 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15158 &ctor_dtor_or_conv_p,
15159 /*parenthesized_p=*/NULL,
15161 /* Gather up the deferred checks. */
15162 stop_deferring_access_checks ();
15164 /* If the DECLARATOR was erroneous, there's no need to go
15166 if (declarator == cp_error_declarator)
15167 return error_mark_node;
15169 /* Check that the number of template-parameter-lists is OK. */
15170 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15172 return error_mark_node;
15174 if (declares_class_or_enum & 2)
15175 cp_parser_check_for_definition_in_return_type (declarator,
15176 decl_specifiers->type,
15177 decl_specifiers->type_location);
15179 /* Figure out what scope the entity declared by the DECLARATOR is
15180 located in. `grokdeclarator' sometimes changes the scope, so
15181 we compute it now. */
15182 scope = get_scope_of_declarator (declarator);
15184 /* Perform any lookups in the declared type which were thought to be
15185 dependent, but are not in the scope of the declarator. */
15186 decl_specifiers->type
15187 = maybe_update_decl_type (decl_specifiers->type, scope);
15189 /* If we're allowing GNU extensions, look for an asm-specification
15191 if (cp_parser_allow_gnu_extensions_p (parser))
15193 /* Look for an asm-specification. */
15194 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15195 asm_specification = cp_parser_asm_specification_opt (parser);
15196 /* And attributes. */
15197 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15198 attributes = cp_parser_attributes_opt (parser);
15202 asm_specification = NULL_TREE;
15203 attributes = NULL_TREE;
15206 /* Peek at the next token. */
15207 token = cp_lexer_peek_token (parser->lexer);
15208 /* Check to see if the token indicates the start of a
15209 function-definition. */
15210 if (function_declarator_p (declarator)
15211 && cp_parser_token_starts_function_definition_p (token))
15213 if (!function_definition_allowed_p)
15215 /* If a function-definition should not appear here, issue an
15217 cp_parser_error (parser,
15218 "a function-definition is not allowed here");
15219 return error_mark_node;
15223 location_t func_brace_location
15224 = cp_lexer_peek_token (parser->lexer)->location;
15226 /* Neither attributes nor an asm-specification are allowed
15227 on a function-definition. */
15228 if (asm_specification)
15229 error_at (asm_spec_start_token->location,
15230 "an asm-specification is not allowed "
15231 "on a function-definition");
15233 error_at (attributes_start_token->location,
15234 "attributes are not allowed on a function-definition");
15235 /* This is a function-definition. */
15236 *function_definition_p = true;
15238 /* Parse the function definition. */
15240 decl = cp_parser_save_member_function_body (parser,
15243 prefix_attributes);
15246 = (cp_parser_function_definition_from_specifiers_and_declarator
15247 (parser, decl_specifiers, prefix_attributes, declarator));
15249 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15251 /* This is where the prologue starts... */
15252 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15253 = func_brace_location;
15262 Only in function declarations for constructors, destructors, and
15263 type conversions can the decl-specifier-seq be omitted.
15265 We explicitly postpone this check past the point where we handle
15266 function-definitions because we tolerate function-definitions
15267 that are missing their return types in some modes. */
15268 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15270 cp_parser_error (parser,
15271 "expected constructor, destructor, or type conversion");
15272 return error_mark_node;
15275 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15276 if (token->type == CPP_EQ
15277 || token->type == CPP_OPEN_PAREN
15278 || token->type == CPP_OPEN_BRACE)
15280 is_initialized = SD_INITIALIZED;
15281 initialization_kind = token->type;
15282 if (maybe_range_for_decl)
15283 *maybe_range_for_decl = error_mark_node;
15285 if (token->type == CPP_EQ
15286 && function_declarator_p (declarator))
15288 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15289 if (t2->keyword == RID_DEFAULT)
15290 is_initialized = SD_DEFAULTED;
15291 else if (t2->keyword == RID_DELETE)
15292 is_initialized = SD_DELETED;
15297 /* If the init-declarator isn't initialized and isn't followed by a
15298 `,' or `;', it's not a valid init-declarator. */
15299 if (token->type != CPP_COMMA
15300 && token->type != CPP_SEMICOLON)
15302 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15303 range_for_decl_p = true;
15306 cp_parser_error (parser, "expected initializer");
15307 return error_mark_node;
15310 is_initialized = SD_UNINITIALIZED;
15311 initialization_kind = CPP_EOF;
15314 /* Because start_decl has side-effects, we should only call it if we
15315 know we're going ahead. By this point, we know that we cannot
15316 possibly be looking at any other construct. */
15317 cp_parser_commit_to_tentative_parse (parser);
15319 /* If the decl specifiers were bad, issue an error now that we're
15320 sure this was intended to be a declarator. Then continue
15321 declaring the variable(s), as int, to try to cut down on further
15323 if (decl_specifiers->any_specifiers_p
15324 && decl_specifiers->type == error_mark_node)
15326 cp_parser_error (parser, "invalid type in declaration");
15327 decl_specifiers->type = integer_type_node;
15330 /* Check to see whether or not this declaration is a friend. */
15331 friend_p = cp_parser_friend_p (decl_specifiers);
15333 /* Enter the newly declared entry in the symbol table. If we're
15334 processing a declaration in a class-specifier, we wait until
15335 after processing the initializer. */
15338 if (parser->in_unbraced_linkage_specification_p)
15339 decl_specifiers->storage_class = sc_extern;
15340 decl = start_decl (declarator, decl_specifiers,
15341 range_for_decl_p? SD_INITIALIZED : is_initialized,
15342 attributes, prefix_attributes,
15344 /* Adjust location of decl if declarator->id_loc is more appropriate:
15345 set, and decl wasn't merged with another decl, in which case its
15346 location would be different from input_location, and more accurate. */
15348 && declarator->id_loc != UNKNOWN_LOCATION
15349 && DECL_SOURCE_LOCATION (decl) == input_location)
15350 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15353 /* Enter the SCOPE. That way unqualified names appearing in the
15354 initializer will be looked up in SCOPE. */
15355 pushed_scope = push_scope (scope);
15357 /* Perform deferred access control checks, now that we know in which
15358 SCOPE the declared entity resides. */
15359 if (!member_p && decl)
15361 tree saved_current_function_decl = NULL_TREE;
15363 /* If the entity being declared is a function, pretend that we
15364 are in its scope. If it is a `friend', it may have access to
15365 things that would not otherwise be accessible. */
15366 if (TREE_CODE (decl) == FUNCTION_DECL)
15368 saved_current_function_decl = current_function_decl;
15369 current_function_decl = decl;
15372 /* Perform access checks for template parameters. */
15373 cp_parser_perform_template_parameter_access_checks (checks);
15375 /* Perform the access control checks for the declarator and the
15376 decl-specifiers. */
15377 perform_deferred_access_checks ();
15379 /* Restore the saved value. */
15380 if (TREE_CODE (decl) == FUNCTION_DECL)
15381 current_function_decl = saved_current_function_decl;
15384 /* Parse the initializer. */
15385 initializer = NULL_TREE;
15386 is_direct_init = false;
15387 is_non_constant_init = true;
15388 if (is_initialized)
15390 if (function_declarator_p (declarator))
15392 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15393 if (initialization_kind == CPP_EQ)
15394 initializer = cp_parser_pure_specifier (parser);
15397 /* If the declaration was erroneous, we don't really
15398 know what the user intended, so just silently
15399 consume the initializer. */
15400 if (decl != error_mark_node)
15401 error_at (initializer_start_token->location,
15402 "initializer provided for function");
15403 cp_parser_skip_to_closing_parenthesis (parser,
15404 /*recovering=*/true,
15405 /*or_comma=*/false,
15406 /*consume_paren=*/true);
15411 /* We want to record the extra mangling scope for in-class
15412 initializers of class members and initializers of static data
15413 member templates. The former is a C++0x feature which isn't
15414 implemented yet, and I expect it will involve deferring
15415 parsing of the initializer until end of class as with default
15416 arguments. So right here we only handle the latter. */
15417 if (!member_p && processing_template_decl)
15418 start_lambda_scope (decl);
15419 initializer = cp_parser_initializer (parser,
15421 &is_non_constant_init);
15422 if (!member_p && processing_template_decl)
15423 finish_lambda_scope ();
15427 /* The old parser allows attributes to appear after a parenthesized
15428 initializer. Mark Mitchell proposed removing this functionality
15429 on the GCC mailing lists on 2002-08-13. This parser accepts the
15430 attributes -- but ignores them. */
15431 if (cp_parser_allow_gnu_extensions_p (parser)
15432 && initialization_kind == CPP_OPEN_PAREN)
15433 if (cp_parser_attributes_opt (parser))
15434 warning (OPT_Wattributes,
15435 "attributes after parenthesized initializer ignored");
15437 /* For an in-class declaration, use `grokfield' to create the
15443 pop_scope (pushed_scope);
15444 pushed_scope = NULL_TREE;
15446 decl = grokfield (declarator, decl_specifiers,
15447 initializer, !is_non_constant_init,
15448 /*asmspec=*/NULL_TREE,
15449 prefix_attributes);
15450 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15451 cp_parser_save_default_args (parser, decl);
15454 /* Finish processing the declaration. But, skip member
15456 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15458 cp_finish_decl (decl,
15459 initializer, !is_non_constant_init,
15461 /* If the initializer is in parentheses, then this is
15462 a direct-initialization, which means that an
15463 `explicit' constructor is OK. Otherwise, an
15464 `explicit' constructor cannot be used. */
15465 ((is_direct_init || !is_initialized)
15466 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15468 else if ((cxx_dialect != cxx98) && friend_p
15469 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15470 /* Core issue #226 (C++0x only): A default template-argument
15471 shall not be specified in a friend class template
15473 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15474 /*is_partial=*/0, /*is_friend_decl=*/1);
15476 if (!friend_p && pushed_scope)
15477 pop_scope (pushed_scope);
15482 /* Parse a declarator.
15486 ptr-operator declarator
15488 abstract-declarator:
15489 ptr-operator abstract-declarator [opt]
15490 direct-abstract-declarator
15495 attributes [opt] direct-declarator
15496 attributes [opt] ptr-operator declarator
15498 abstract-declarator:
15499 attributes [opt] ptr-operator abstract-declarator [opt]
15500 attributes [opt] direct-abstract-declarator
15502 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15503 detect constructor, destructor or conversion operators. It is set
15504 to -1 if the declarator is a name, and +1 if it is a
15505 function. Otherwise it is set to zero. Usually you just want to
15506 test for >0, but internally the negative value is used.
15508 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15509 a decl-specifier-seq unless it declares a constructor, destructor,
15510 or conversion. It might seem that we could check this condition in
15511 semantic analysis, rather than parsing, but that makes it difficult
15512 to handle something like `f()'. We want to notice that there are
15513 no decl-specifiers, and therefore realize that this is an
15514 expression, not a declaration.)
15516 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15517 the declarator is a direct-declarator of the form "(...)".
15519 MEMBER_P is true iff this declarator is a member-declarator. */
15521 static cp_declarator *
15522 cp_parser_declarator (cp_parser* parser,
15523 cp_parser_declarator_kind dcl_kind,
15524 int* ctor_dtor_or_conv_p,
15525 bool* parenthesized_p,
15528 cp_declarator *declarator;
15529 enum tree_code code;
15530 cp_cv_quals cv_quals;
15532 tree attributes = NULL_TREE;
15534 /* Assume this is not a constructor, destructor, or type-conversion
15536 if (ctor_dtor_or_conv_p)
15537 *ctor_dtor_or_conv_p = 0;
15539 if (cp_parser_allow_gnu_extensions_p (parser))
15540 attributes = cp_parser_attributes_opt (parser);
15542 /* Check for the ptr-operator production. */
15543 cp_parser_parse_tentatively (parser);
15544 /* Parse the ptr-operator. */
15545 code = cp_parser_ptr_operator (parser,
15548 /* If that worked, then we have a ptr-operator. */
15549 if (cp_parser_parse_definitely (parser))
15551 /* If a ptr-operator was found, then this declarator was not
15553 if (parenthesized_p)
15554 *parenthesized_p = true;
15555 /* The dependent declarator is optional if we are parsing an
15556 abstract-declarator. */
15557 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15558 cp_parser_parse_tentatively (parser);
15560 /* Parse the dependent declarator. */
15561 declarator = cp_parser_declarator (parser, dcl_kind,
15562 /*ctor_dtor_or_conv_p=*/NULL,
15563 /*parenthesized_p=*/NULL,
15564 /*member_p=*/false);
15566 /* If we are parsing an abstract-declarator, we must handle the
15567 case where the dependent declarator is absent. */
15568 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15569 && !cp_parser_parse_definitely (parser))
15572 declarator = cp_parser_make_indirect_declarator
15573 (code, class_type, cv_quals, declarator);
15575 /* Everything else is a direct-declarator. */
15578 if (parenthesized_p)
15579 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15581 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15582 ctor_dtor_or_conv_p,
15586 if (attributes && declarator && declarator != cp_error_declarator)
15587 declarator->attributes = attributes;
15592 /* Parse a direct-declarator or direct-abstract-declarator.
15596 direct-declarator ( parameter-declaration-clause )
15597 cv-qualifier-seq [opt]
15598 exception-specification [opt]
15599 direct-declarator [ constant-expression [opt] ]
15602 direct-abstract-declarator:
15603 direct-abstract-declarator [opt]
15604 ( parameter-declaration-clause )
15605 cv-qualifier-seq [opt]
15606 exception-specification [opt]
15607 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15608 ( abstract-declarator )
15610 Returns a representation of the declarator. DCL_KIND is
15611 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15612 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15613 we are parsing a direct-declarator. It is
15614 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15615 of ambiguity we prefer an abstract declarator, as per
15616 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15617 cp_parser_declarator. */
15619 static cp_declarator *
15620 cp_parser_direct_declarator (cp_parser* parser,
15621 cp_parser_declarator_kind dcl_kind,
15622 int* ctor_dtor_or_conv_p,
15626 cp_declarator *declarator = NULL;
15627 tree scope = NULL_TREE;
15628 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15629 bool saved_in_declarator_p = parser->in_declarator_p;
15631 tree pushed_scope = NULL_TREE;
15635 /* Peek at the next token. */
15636 token = cp_lexer_peek_token (parser->lexer);
15637 if (token->type == CPP_OPEN_PAREN)
15639 /* This is either a parameter-declaration-clause, or a
15640 parenthesized declarator. When we know we are parsing a
15641 named declarator, it must be a parenthesized declarator
15642 if FIRST is true. For instance, `(int)' is a
15643 parameter-declaration-clause, with an omitted
15644 direct-abstract-declarator. But `((*))', is a
15645 parenthesized abstract declarator. Finally, when T is a
15646 template parameter `(T)' is a
15647 parameter-declaration-clause, and not a parenthesized
15650 We first try and parse a parameter-declaration-clause,
15651 and then try a nested declarator (if FIRST is true).
15653 It is not an error for it not to be a
15654 parameter-declaration-clause, even when FIRST is
15660 The first is the declaration of a function while the
15661 second is the definition of a variable, including its
15664 Having seen only the parenthesis, we cannot know which of
15665 these two alternatives should be selected. Even more
15666 complex are examples like:
15671 The former is a function-declaration; the latter is a
15672 variable initialization.
15674 Thus again, we try a parameter-declaration-clause, and if
15675 that fails, we back out and return. */
15677 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15680 unsigned saved_num_template_parameter_lists;
15681 bool is_declarator = false;
15684 /* In a member-declarator, the only valid interpretation
15685 of a parenthesis is the start of a
15686 parameter-declaration-clause. (It is invalid to
15687 initialize a static data member with a parenthesized
15688 initializer; only the "=" form of initialization is
15691 cp_parser_parse_tentatively (parser);
15693 /* Consume the `('. */
15694 cp_lexer_consume_token (parser->lexer);
15697 /* If this is going to be an abstract declarator, we're
15698 in a declarator and we can't have default args. */
15699 parser->default_arg_ok_p = false;
15700 parser->in_declarator_p = true;
15703 /* Inside the function parameter list, surrounding
15704 template-parameter-lists do not apply. */
15705 saved_num_template_parameter_lists
15706 = parser->num_template_parameter_lists;
15707 parser->num_template_parameter_lists = 0;
15709 begin_scope (sk_function_parms, NULL_TREE);
15711 /* Parse the parameter-declaration-clause. */
15712 params = cp_parser_parameter_declaration_clause (parser);
15714 parser->num_template_parameter_lists
15715 = saved_num_template_parameter_lists;
15717 /* Consume the `)'. */
15718 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15720 /* If all went well, parse the cv-qualifier-seq and the
15721 exception-specification. */
15722 if (member_p || cp_parser_parse_definitely (parser))
15724 cp_cv_quals cv_quals;
15725 cp_virt_specifiers virt_specifiers;
15726 tree exception_specification;
15729 is_declarator = true;
15731 if (ctor_dtor_or_conv_p)
15732 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15735 /* Parse the cv-qualifier-seq. */
15736 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15737 /* And the exception-specification. */
15738 exception_specification
15739 = cp_parser_exception_specification_opt (parser);
15740 /* Parse the virt-specifier-seq. */
15741 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15743 late_return = (cp_parser_late_return_type_opt
15744 (parser, member_p ? cv_quals : -1));
15746 /* Create the function-declarator. */
15747 declarator = make_call_declarator (declarator,
15751 exception_specification,
15753 /* Any subsequent parameter lists are to do with
15754 return type, so are not those of the declared
15756 parser->default_arg_ok_p = false;
15759 /* Remove the function parms from scope. */
15760 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15761 pop_binding (DECL_NAME (t), t);
15765 /* Repeat the main loop. */
15769 /* If this is the first, we can try a parenthesized
15773 bool saved_in_type_id_in_expr_p;
15775 parser->default_arg_ok_p = saved_default_arg_ok_p;
15776 parser->in_declarator_p = saved_in_declarator_p;
15778 /* Consume the `('. */
15779 cp_lexer_consume_token (parser->lexer);
15780 /* Parse the nested declarator. */
15781 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15782 parser->in_type_id_in_expr_p = true;
15784 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15785 /*parenthesized_p=*/NULL,
15787 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15789 /* Expect a `)'. */
15790 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15791 declarator = cp_error_declarator;
15792 if (declarator == cp_error_declarator)
15795 goto handle_declarator;
15797 /* Otherwise, we must be done. */
15801 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15802 && token->type == CPP_OPEN_SQUARE)
15804 /* Parse an array-declarator. */
15807 if (ctor_dtor_or_conv_p)
15808 *ctor_dtor_or_conv_p = 0;
15811 parser->default_arg_ok_p = false;
15812 parser->in_declarator_p = true;
15813 /* Consume the `['. */
15814 cp_lexer_consume_token (parser->lexer);
15815 /* Peek at the next token. */
15816 token = cp_lexer_peek_token (parser->lexer);
15817 /* If the next token is `]', then there is no
15818 constant-expression. */
15819 if (token->type != CPP_CLOSE_SQUARE)
15821 bool non_constant_p;
15824 = cp_parser_constant_expression (parser,
15825 /*allow_non_constant=*/true,
15827 if (!non_constant_p)
15829 /* Normally, the array bound must be an integral constant
15830 expression. However, as an extension, we allow VLAs
15831 in function scopes as long as they aren't part of a
15832 parameter declaration. */
15833 else if (!parser->in_function_body
15834 || current_binding_level->kind == sk_function_parms)
15836 cp_parser_error (parser,
15837 "array bound is not an integer constant");
15838 bounds = error_mark_node;
15840 else if (processing_template_decl && !error_operand_p (bounds))
15842 /* Remember this wasn't a constant-expression. */
15843 bounds = build_nop (TREE_TYPE (bounds), bounds);
15844 TREE_SIDE_EFFECTS (bounds) = 1;
15848 bounds = NULL_TREE;
15849 /* Look for the closing `]'. */
15850 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15852 declarator = cp_error_declarator;
15856 declarator = make_array_declarator (declarator, bounds);
15858 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15861 tree qualifying_scope;
15862 tree unqualified_name;
15863 special_function_kind sfk;
15865 bool pack_expansion_p = false;
15866 cp_token *declarator_id_start_token;
15868 /* Parse a declarator-id */
15869 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15872 cp_parser_parse_tentatively (parser);
15874 /* If we see an ellipsis, we should be looking at a
15876 if (token->type == CPP_ELLIPSIS)
15878 /* Consume the `...' */
15879 cp_lexer_consume_token (parser->lexer);
15881 pack_expansion_p = true;
15885 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15887 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15888 qualifying_scope = parser->scope;
15893 if (!unqualified_name && pack_expansion_p)
15895 /* Check whether an error occurred. */
15896 okay = !cp_parser_error_occurred (parser);
15898 /* We already consumed the ellipsis to mark a
15899 parameter pack, but we have no way to report it,
15900 so abort the tentative parse. We will be exiting
15901 immediately anyway. */
15902 cp_parser_abort_tentative_parse (parser);
15905 okay = cp_parser_parse_definitely (parser);
15908 unqualified_name = error_mark_node;
15909 else if (unqualified_name
15910 && (qualifying_scope
15911 || (TREE_CODE (unqualified_name)
15912 != IDENTIFIER_NODE)))
15914 cp_parser_error (parser, "expected unqualified-id");
15915 unqualified_name = error_mark_node;
15919 if (!unqualified_name)
15921 if (unqualified_name == error_mark_node)
15923 declarator = cp_error_declarator;
15924 pack_expansion_p = false;
15925 declarator->parameter_pack_p = false;
15929 if (qualifying_scope && at_namespace_scope_p ()
15930 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15932 /* In the declaration of a member of a template class
15933 outside of the class itself, the SCOPE will sometimes
15934 be a TYPENAME_TYPE. For example, given:
15936 template <typename T>
15937 int S<T>::R::i = 3;
15939 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15940 this context, we must resolve S<T>::R to an ordinary
15941 type, rather than a typename type.
15943 The reason we normally avoid resolving TYPENAME_TYPEs
15944 is that a specialization of `S' might render
15945 `S<T>::R' not a type. However, if `S' is
15946 specialized, then this `i' will not be used, so there
15947 is no harm in resolving the types here. */
15950 /* Resolve the TYPENAME_TYPE. */
15951 type = resolve_typename_type (qualifying_scope,
15952 /*only_current_p=*/false);
15953 /* If that failed, the declarator is invalid. */
15954 if (TREE_CODE (type) == TYPENAME_TYPE)
15956 if (typedef_variant_p (type))
15957 error_at (declarator_id_start_token->location,
15958 "cannot define member of dependent typedef "
15961 error_at (declarator_id_start_token->location,
15962 "%<%T::%E%> is not a type",
15963 TYPE_CONTEXT (qualifying_scope),
15964 TYPE_IDENTIFIER (qualifying_scope));
15966 qualifying_scope = type;
15971 if (unqualified_name)
15975 if (qualifying_scope
15976 && CLASS_TYPE_P (qualifying_scope))
15977 class_type = qualifying_scope;
15979 class_type = current_class_type;
15981 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15983 tree name_type = TREE_TYPE (unqualified_name);
15984 if (class_type && same_type_p (name_type, class_type))
15986 if (qualifying_scope
15987 && CLASSTYPE_USE_TEMPLATE (name_type))
15989 error_at (declarator_id_start_token->location,
15990 "invalid use of constructor as a template");
15991 inform (declarator_id_start_token->location,
15992 "use %<%T::%D%> instead of %<%T::%D%> to "
15993 "name the constructor in a qualified name",
15995 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15996 class_type, name_type);
15997 declarator = cp_error_declarator;
16001 unqualified_name = constructor_name (class_type);
16005 /* We do not attempt to print the declarator
16006 here because we do not have enough
16007 information about its original syntactic
16009 cp_parser_error (parser, "invalid declarator");
16010 declarator = cp_error_declarator;
16017 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16018 sfk = sfk_destructor;
16019 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16020 sfk = sfk_conversion;
16021 else if (/* There's no way to declare a constructor
16022 for an anonymous type, even if the type
16023 got a name for linkage purposes. */
16024 !TYPE_WAS_ANONYMOUS (class_type)
16025 && constructor_name_p (unqualified_name,
16028 unqualified_name = constructor_name (class_type);
16029 sfk = sfk_constructor;
16031 else if (is_overloaded_fn (unqualified_name)
16032 && DECL_CONSTRUCTOR_P (get_first_fn
16033 (unqualified_name)))
16034 sfk = sfk_constructor;
16036 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16037 *ctor_dtor_or_conv_p = -1;
16040 declarator = make_id_declarator (qualifying_scope,
16043 declarator->id_loc = token->location;
16044 declarator->parameter_pack_p = pack_expansion_p;
16046 if (pack_expansion_p)
16047 maybe_warn_variadic_templates ();
16050 handle_declarator:;
16051 scope = get_scope_of_declarator (declarator);
16053 /* Any names that appear after the declarator-id for a
16054 member are looked up in the containing scope. */
16055 pushed_scope = push_scope (scope);
16056 parser->in_declarator_p = true;
16057 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16058 || (declarator && declarator->kind == cdk_id))
16059 /* Default args are only allowed on function
16061 parser->default_arg_ok_p = saved_default_arg_ok_p;
16063 parser->default_arg_ok_p = false;
16072 /* For an abstract declarator, we might wind up with nothing at this
16073 point. That's an error; the declarator is not optional. */
16075 cp_parser_error (parser, "expected declarator");
16077 /* If we entered a scope, we must exit it now. */
16079 pop_scope (pushed_scope);
16081 parser->default_arg_ok_p = saved_default_arg_ok_p;
16082 parser->in_declarator_p = saved_in_declarator_p;
16087 /* Parse a ptr-operator.
16090 * cv-qualifier-seq [opt]
16092 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16097 & cv-qualifier-seq [opt]
16099 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16100 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16101 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16102 filled in with the TYPE containing the member. *CV_QUALS is
16103 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16104 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16105 Note that the tree codes returned by this function have nothing
16106 to do with the types of trees that will be eventually be created
16107 to represent the pointer or reference type being parsed. They are
16108 just constants with suggestive names. */
16109 static enum tree_code
16110 cp_parser_ptr_operator (cp_parser* parser,
16112 cp_cv_quals *cv_quals)
16114 enum tree_code code = ERROR_MARK;
16117 /* Assume that it's not a pointer-to-member. */
16119 /* And that there are no cv-qualifiers. */
16120 *cv_quals = TYPE_UNQUALIFIED;
16122 /* Peek at the next token. */
16123 token = cp_lexer_peek_token (parser->lexer);
16125 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16126 if (token->type == CPP_MULT)
16127 code = INDIRECT_REF;
16128 else if (token->type == CPP_AND)
16130 else if ((cxx_dialect != cxx98) &&
16131 token->type == CPP_AND_AND) /* C++0x only */
16132 code = NON_LVALUE_EXPR;
16134 if (code != ERROR_MARK)
16136 /* Consume the `*', `&' or `&&'. */
16137 cp_lexer_consume_token (parser->lexer);
16139 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16140 `&', if we are allowing GNU extensions. (The only qualifier
16141 that can legally appear after `&' is `restrict', but that is
16142 enforced during semantic analysis. */
16143 if (code == INDIRECT_REF
16144 || cp_parser_allow_gnu_extensions_p (parser))
16145 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16149 /* Try the pointer-to-member case. */
16150 cp_parser_parse_tentatively (parser);
16151 /* Look for the optional `::' operator. */
16152 cp_parser_global_scope_opt (parser,
16153 /*current_scope_valid_p=*/false);
16154 /* Look for the nested-name specifier. */
16155 token = cp_lexer_peek_token (parser->lexer);
16156 cp_parser_nested_name_specifier (parser,
16157 /*typename_keyword_p=*/false,
16158 /*check_dependency_p=*/true,
16160 /*is_declaration=*/false);
16161 /* If we found it, and the next token is a `*', then we are
16162 indeed looking at a pointer-to-member operator. */
16163 if (!cp_parser_error_occurred (parser)
16164 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16166 /* Indicate that the `*' operator was used. */
16167 code = INDIRECT_REF;
16169 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16170 error_at (token->location, "%qD is a namespace", parser->scope);
16173 /* The type of which the member is a member is given by the
16175 *type = parser->scope;
16176 /* The next name will not be qualified. */
16177 parser->scope = NULL_TREE;
16178 parser->qualifying_scope = NULL_TREE;
16179 parser->object_scope = NULL_TREE;
16180 /* Look for the optional cv-qualifier-seq. */
16181 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16184 /* If that didn't work we don't have a ptr-operator. */
16185 if (!cp_parser_parse_definitely (parser))
16186 cp_parser_error (parser, "expected ptr-operator");
16192 /* Parse an (optional) cv-qualifier-seq.
16195 cv-qualifier cv-qualifier-seq [opt]
16206 Returns a bitmask representing the cv-qualifiers. */
16209 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16211 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16216 cp_cv_quals cv_qualifier;
16218 /* Peek at the next token. */
16219 token = cp_lexer_peek_token (parser->lexer);
16220 /* See if it's a cv-qualifier. */
16221 switch (token->keyword)
16224 cv_qualifier = TYPE_QUAL_CONST;
16228 cv_qualifier = TYPE_QUAL_VOLATILE;
16232 cv_qualifier = TYPE_QUAL_RESTRICT;
16236 cv_qualifier = TYPE_UNQUALIFIED;
16243 if (cv_quals & cv_qualifier)
16245 error_at (token->location, "duplicate cv-qualifier");
16246 cp_lexer_purge_token (parser->lexer);
16250 cp_lexer_consume_token (parser->lexer);
16251 cv_quals |= cv_qualifier;
16258 /* Parse an (optional) virt-specifier-seq.
16260 virt-specifier-seq:
16261 virt-specifier virt-specifier-seq [opt]
16267 Returns a bitmask representing the virt-specifiers. */
16269 static cp_virt_specifiers
16270 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16272 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16277 cp_virt_specifiers virt_specifier;
16279 /* Peek at the next token. */
16280 token = cp_lexer_peek_token (parser->lexer);
16281 /* See if it's a virt-specifier-qualifier. */
16282 if (token->type != CPP_NAME)
16284 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16286 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16287 virt_specifier = VIRT_SPEC_OVERRIDE;
16289 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16291 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16292 virt_specifier = VIRT_SPEC_FINAL;
16294 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16296 virt_specifier = VIRT_SPEC_FINAL;
16301 if (virt_specifiers & virt_specifier)
16303 error_at (token->location, "duplicate virt-specifier");
16304 cp_lexer_purge_token (parser->lexer);
16308 cp_lexer_consume_token (parser->lexer);
16309 virt_specifiers |= virt_specifier;
16312 return virt_specifiers;
16315 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16316 is in scope even though it isn't real. */
16319 inject_this_parameter (tree ctype, cp_cv_quals quals)
16323 if (current_class_ptr)
16325 /* We don't clear this between NSDMIs. Is it already what we want? */
16326 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16327 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16328 && cp_type_quals (type) == quals)
16332 this_parm = build_this_parm (ctype, quals);
16333 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16334 current_class_ptr = NULL_TREE;
16336 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16337 current_class_ptr = this_parm;
16340 /* Parse a late-specified return type, if any. This is not a separate
16341 non-terminal, but part of a function declarator, which looks like
16343 -> trailing-type-specifier-seq abstract-declarator(opt)
16345 Returns the type indicated by the type-id.
16347 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16351 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16356 /* Peek at the next token. */
16357 token = cp_lexer_peek_token (parser->lexer);
16358 /* A late-specified return type is indicated by an initial '->'. */
16359 if (token->type != CPP_DEREF)
16362 /* Consume the ->. */
16363 cp_lexer_consume_token (parser->lexer);
16367 /* DR 1207: 'this' is in scope in the trailing return type. */
16368 gcc_assert (current_class_ptr == NULL_TREE);
16369 inject_this_parameter (current_class_type, quals);
16372 type = cp_parser_trailing_type_id (parser);
16375 current_class_ptr = current_class_ref = NULL_TREE;
16380 /* Parse a declarator-id.
16384 :: [opt] nested-name-specifier [opt] type-name
16386 In the `id-expression' case, the value returned is as for
16387 cp_parser_id_expression if the id-expression was an unqualified-id.
16388 If the id-expression was a qualified-id, then a SCOPE_REF is
16389 returned. The first operand is the scope (either a NAMESPACE_DECL
16390 or TREE_TYPE), but the second is still just a representation of an
16394 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16397 /* The expression must be an id-expression. Assume that qualified
16398 names are the names of types so that:
16401 int S<T>::R::i = 3;
16403 will work; we must treat `S<T>::R' as the name of a type.
16404 Similarly, assume that qualified names are templates, where
16408 int S<T>::R<T>::i = 3;
16411 id = cp_parser_id_expression (parser,
16412 /*template_keyword_p=*/false,
16413 /*check_dependency_p=*/false,
16414 /*template_p=*/NULL,
16415 /*declarator_p=*/true,
16417 if (id && BASELINK_P (id))
16418 id = BASELINK_FUNCTIONS (id);
16422 /* Parse a type-id.
16425 type-specifier-seq abstract-declarator [opt]
16427 Returns the TYPE specified. */
16430 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16431 bool is_trailing_return)
16433 cp_decl_specifier_seq type_specifier_seq;
16434 cp_declarator *abstract_declarator;
16436 /* Parse the type-specifier-seq. */
16437 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16438 is_trailing_return,
16439 &type_specifier_seq);
16440 if (type_specifier_seq.type == error_mark_node)
16441 return error_mark_node;
16443 /* There might or might not be an abstract declarator. */
16444 cp_parser_parse_tentatively (parser);
16445 /* Look for the declarator. */
16446 abstract_declarator
16447 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16448 /*parenthesized_p=*/NULL,
16449 /*member_p=*/false);
16450 /* Check to see if there really was a declarator. */
16451 if (!cp_parser_parse_definitely (parser))
16452 abstract_declarator = NULL;
16454 if (type_specifier_seq.type
16455 && type_uses_auto (type_specifier_seq.type))
16457 /* A type-id with type 'auto' is only ok if the abstract declarator
16458 is a function declarator with a late-specified return type. */
16459 if (abstract_declarator
16460 && abstract_declarator->kind == cdk_function
16461 && abstract_declarator->u.function.late_return_type)
16465 error ("invalid use of %<auto%>");
16466 return error_mark_node;
16470 return groktypename (&type_specifier_seq, abstract_declarator,
16474 static tree cp_parser_type_id (cp_parser *parser)
16476 return cp_parser_type_id_1 (parser, false, false);
16479 static tree cp_parser_template_type_arg (cp_parser *parser)
16482 const char *saved_message = parser->type_definition_forbidden_message;
16483 parser->type_definition_forbidden_message
16484 = G_("types may not be defined in template arguments");
16485 r = cp_parser_type_id_1 (parser, true, false);
16486 parser->type_definition_forbidden_message = saved_message;
16490 static tree cp_parser_trailing_type_id (cp_parser *parser)
16492 return cp_parser_type_id_1 (parser, false, true);
16495 /* Parse a type-specifier-seq.
16497 type-specifier-seq:
16498 type-specifier type-specifier-seq [opt]
16502 type-specifier-seq:
16503 attributes type-specifier-seq [opt]
16505 If IS_DECLARATION is true, we are at the start of a "condition" or
16506 exception-declaration, so we might be followed by a declarator-id.
16508 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16509 i.e. we've just seen "->".
16511 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16514 cp_parser_type_specifier_seq (cp_parser* parser,
16515 bool is_declaration,
16516 bool is_trailing_return,
16517 cp_decl_specifier_seq *type_specifier_seq)
16519 bool seen_type_specifier = false;
16520 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16521 cp_token *start_token = NULL;
16523 /* Clear the TYPE_SPECIFIER_SEQ. */
16524 clear_decl_specs (type_specifier_seq);
16526 /* In the context of a trailing return type, enum E { } is an
16527 elaborated-type-specifier followed by a function-body, not an
16529 if (is_trailing_return)
16530 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16532 /* Parse the type-specifiers and attributes. */
16535 tree type_specifier;
16536 bool is_cv_qualifier;
16538 /* Check for attributes first. */
16539 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16541 type_specifier_seq->attributes =
16542 chainon (type_specifier_seq->attributes,
16543 cp_parser_attributes_opt (parser));
16547 /* record the token of the beginning of the type specifier seq,
16548 for error reporting purposes*/
16550 start_token = cp_lexer_peek_token (parser->lexer);
16552 /* Look for the type-specifier. */
16553 type_specifier = cp_parser_type_specifier (parser,
16555 type_specifier_seq,
16556 /*is_declaration=*/false,
16559 if (!type_specifier)
16561 /* If the first type-specifier could not be found, this is not a
16562 type-specifier-seq at all. */
16563 if (!seen_type_specifier)
16565 cp_parser_error (parser, "expected type-specifier");
16566 type_specifier_seq->type = error_mark_node;
16569 /* If subsequent type-specifiers could not be found, the
16570 type-specifier-seq is complete. */
16574 seen_type_specifier = true;
16575 /* The standard says that a condition can be:
16577 type-specifier-seq declarator = assignment-expression
16584 we should treat the "S" as a declarator, not as a
16585 type-specifier. The standard doesn't say that explicitly for
16586 type-specifier-seq, but it does say that for
16587 decl-specifier-seq in an ordinary declaration. Perhaps it
16588 would be clearer just to allow a decl-specifier-seq here, and
16589 then add a semantic restriction that if any decl-specifiers
16590 that are not type-specifiers appear, the program is invalid. */
16591 if (is_declaration && !is_cv_qualifier)
16592 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16595 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16598 /* Parse a parameter-declaration-clause.
16600 parameter-declaration-clause:
16601 parameter-declaration-list [opt] ... [opt]
16602 parameter-declaration-list , ...
16604 Returns a representation for the parameter declarations. A return
16605 value of NULL indicates a parameter-declaration-clause consisting
16606 only of an ellipsis. */
16609 cp_parser_parameter_declaration_clause (cp_parser* parser)
16616 /* Peek at the next token. */
16617 token = cp_lexer_peek_token (parser->lexer);
16618 /* Check for trivial parameter-declaration-clauses. */
16619 if (token->type == CPP_ELLIPSIS)
16621 /* Consume the `...' token. */
16622 cp_lexer_consume_token (parser->lexer);
16625 else if (token->type == CPP_CLOSE_PAREN)
16626 /* There are no parameters. */
16628 #ifndef NO_IMPLICIT_EXTERN_C
16629 if (in_system_header && current_class_type == NULL
16630 && current_lang_name == lang_name_c)
16634 return void_list_node;
16636 /* Check for `(void)', too, which is a special case. */
16637 else if (token->keyword == RID_VOID
16638 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16639 == CPP_CLOSE_PAREN))
16641 /* Consume the `void' token. */
16642 cp_lexer_consume_token (parser->lexer);
16643 /* There are no parameters. */
16644 return void_list_node;
16647 /* Parse the parameter-declaration-list. */
16648 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16649 /* If a parse error occurred while parsing the
16650 parameter-declaration-list, then the entire
16651 parameter-declaration-clause is erroneous. */
16655 /* Peek at the next token. */
16656 token = cp_lexer_peek_token (parser->lexer);
16657 /* If it's a `,', the clause should terminate with an ellipsis. */
16658 if (token->type == CPP_COMMA)
16660 /* Consume the `,'. */
16661 cp_lexer_consume_token (parser->lexer);
16662 /* Expect an ellipsis. */
16664 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16666 /* It might also be `...' if the optional trailing `,' was
16668 else if (token->type == CPP_ELLIPSIS)
16670 /* Consume the `...' token. */
16671 cp_lexer_consume_token (parser->lexer);
16672 /* And remember that we saw it. */
16676 ellipsis_p = false;
16678 /* Finish the parameter list. */
16680 parameters = chainon (parameters, void_list_node);
16685 /* Parse a parameter-declaration-list.
16687 parameter-declaration-list:
16688 parameter-declaration
16689 parameter-declaration-list , parameter-declaration
16691 Returns a representation of the parameter-declaration-list, as for
16692 cp_parser_parameter_declaration_clause. However, the
16693 `void_list_node' is never appended to the list. Upon return,
16694 *IS_ERROR will be true iff an error occurred. */
16697 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16699 tree parameters = NULL_TREE;
16700 tree *tail = ¶meters;
16701 bool saved_in_unbraced_linkage_specification_p;
16704 /* Assume all will go well. */
16706 /* The special considerations that apply to a function within an
16707 unbraced linkage specifications do not apply to the parameters
16708 to the function. */
16709 saved_in_unbraced_linkage_specification_p
16710 = parser->in_unbraced_linkage_specification_p;
16711 parser->in_unbraced_linkage_specification_p = false;
16713 /* Look for more parameters. */
16716 cp_parameter_declarator *parameter;
16717 tree decl = error_mark_node;
16718 bool parenthesized_p = false;
16719 /* Parse the parameter. */
16721 = cp_parser_parameter_declaration (parser,
16722 /*template_parm_p=*/false,
16725 /* We don't know yet if the enclosing context is deprecated, so wait
16726 and warn in grokparms if appropriate. */
16727 deprecated_state = DEPRECATED_SUPPRESS;
16730 decl = grokdeclarator (parameter->declarator,
16731 ¶meter->decl_specifiers,
16733 parameter->default_argument != NULL_TREE,
16734 ¶meter->decl_specifiers.attributes);
16736 deprecated_state = DEPRECATED_NORMAL;
16738 /* If a parse error occurred parsing the parameter declaration,
16739 then the entire parameter-declaration-list is erroneous. */
16740 if (decl == error_mark_node)
16743 parameters = error_mark_node;
16747 if (parameter->decl_specifiers.attributes)
16748 cplus_decl_attributes (&decl,
16749 parameter->decl_specifiers.attributes,
16751 if (DECL_NAME (decl))
16752 decl = pushdecl (decl);
16754 if (decl != error_mark_node)
16756 retrofit_lang_decl (decl);
16757 DECL_PARM_INDEX (decl) = ++index;
16758 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16761 /* Add the new parameter to the list. */
16762 *tail = build_tree_list (parameter->default_argument, decl);
16763 tail = &TREE_CHAIN (*tail);
16765 /* Peek at the next token. */
16766 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16767 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16768 /* These are for Objective-C++ */
16769 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16770 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16771 /* The parameter-declaration-list is complete. */
16773 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16777 /* Peek at the next token. */
16778 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16779 /* If it's an ellipsis, then the list is complete. */
16780 if (token->type == CPP_ELLIPSIS)
16782 /* Otherwise, there must be more parameters. Consume the
16784 cp_lexer_consume_token (parser->lexer);
16785 /* When parsing something like:
16787 int i(float f, double d)
16789 we can tell after seeing the declaration for "f" that we
16790 are not looking at an initialization of a variable "i",
16791 but rather at the declaration of a function "i".
16793 Due to the fact that the parsing of template arguments
16794 (as specified to a template-id) requires backtracking we
16795 cannot use this technique when inside a template argument
16797 if (!parser->in_template_argument_list_p
16798 && !parser->in_type_id_in_expr_p
16799 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16800 /* However, a parameter-declaration of the form
16801 "foat(f)" (which is a valid declaration of a
16802 parameter "f") can also be interpreted as an
16803 expression (the conversion of "f" to "float"). */
16804 && !parenthesized_p)
16805 cp_parser_commit_to_tentative_parse (parser);
16809 cp_parser_error (parser, "expected %<,%> or %<...%>");
16810 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16811 cp_parser_skip_to_closing_parenthesis (parser,
16812 /*recovering=*/true,
16813 /*or_comma=*/false,
16814 /*consume_paren=*/false);
16819 parser->in_unbraced_linkage_specification_p
16820 = saved_in_unbraced_linkage_specification_p;
16825 /* Parse a parameter declaration.
16827 parameter-declaration:
16828 decl-specifier-seq ... [opt] declarator
16829 decl-specifier-seq declarator = assignment-expression
16830 decl-specifier-seq ... [opt] abstract-declarator [opt]
16831 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16833 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16834 declares a template parameter. (In that case, a non-nested `>'
16835 token encountered during the parsing of the assignment-expression
16836 is not interpreted as a greater-than operator.)
16838 Returns a representation of the parameter, or NULL if an error
16839 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16840 true iff the declarator is of the form "(p)". */
16842 static cp_parameter_declarator *
16843 cp_parser_parameter_declaration (cp_parser *parser,
16844 bool template_parm_p,
16845 bool *parenthesized_p)
16847 int declares_class_or_enum;
16848 cp_decl_specifier_seq decl_specifiers;
16849 cp_declarator *declarator;
16850 tree default_argument;
16851 cp_token *token = NULL, *declarator_token_start = NULL;
16852 const char *saved_message;
16854 /* In a template parameter, `>' is not an operator.
16858 When parsing a default template-argument for a non-type
16859 template-parameter, the first non-nested `>' is taken as the end
16860 of the template parameter-list rather than a greater-than
16863 /* Type definitions may not appear in parameter types. */
16864 saved_message = parser->type_definition_forbidden_message;
16865 parser->type_definition_forbidden_message
16866 = G_("types may not be defined in parameter types");
16868 /* Parse the declaration-specifiers. */
16869 cp_parser_decl_specifier_seq (parser,
16870 CP_PARSER_FLAGS_NONE,
16872 &declares_class_or_enum);
16874 /* Complain about missing 'typename' or other invalid type names. */
16875 if (!decl_specifiers.any_type_specifiers_p)
16876 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16878 /* If an error occurred, there's no reason to attempt to parse the
16879 rest of the declaration. */
16880 if (cp_parser_error_occurred (parser))
16882 parser->type_definition_forbidden_message = saved_message;
16886 /* Peek at the next token. */
16887 token = cp_lexer_peek_token (parser->lexer);
16889 /* If the next token is a `)', `,', `=', `>', or `...', then there
16890 is no declarator. However, when variadic templates are enabled,
16891 there may be a declarator following `...'. */
16892 if (token->type == CPP_CLOSE_PAREN
16893 || token->type == CPP_COMMA
16894 || token->type == CPP_EQ
16895 || token->type == CPP_GREATER)
16898 if (parenthesized_p)
16899 *parenthesized_p = false;
16901 /* Otherwise, there should be a declarator. */
16904 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16905 parser->default_arg_ok_p = false;
16907 /* After seeing a decl-specifier-seq, if the next token is not a
16908 "(", there is no possibility that the code is a valid
16909 expression. Therefore, if parsing tentatively, we commit at
16911 if (!parser->in_template_argument_list_p
16912 /* In an expression context, having seen:
16916 we cannot be sure whether we are looking at a
16917 function-type (taking a "char" as a parameter) or a cast
16918 of some object of type "char" to "int". */
16919 && !parser->in_type_id_in_expr_p
16920 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16921 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16922 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16923 cp_parser_commit_to_tentative_parse (parser);
16924 /* Parse the declarator. */
16925 declarator_token_start = token;
16926 declarator = cp_parser_declarator (parser,
16927 CP_PARSER_DECLARATOR_EITHER,
16928 /*ctor_dtor_or_conv_p=*/NULL,
16930 /*member_p=*/false);
16931 parser->default_arg_ok_p = saved_default_arg_ok_p;
16932 /* After the declarator, allow more attributes. */
16933 decl_specifiers.attributes
16934 = chainon (decl_specifiers.attributes,
16935 cp_parser_attributes_opt (parser));
16938 /* If the next token is an ellipsis, and we have not seen a
16939 declarator name, and the type of the declarator contains parameter
16940 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16941 a parameter pack expansion expression. Otherwise, leave the
16942 ellipsis for a C-style variadic function. */
16943 token = cp_lexer_peek_token (parser->lexer);
16944 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16946 tree type = decl_specifiers.type;
16948 if (type && DECL_P (type))
16949 type = TREE_TYPE (type);
16952 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16953 && declarator_can_be_parameter_pack (declarator)
16954 && (!declarator || !declarator->parameter_pack_p)
16955 && uses_parameter_packs (type))
16957 /* Consume the `...'. */
16958 cp_lexer_consume_token (parser->lexer);
16959 maybe_warn_variadic_templates ();
16961 /* Build a pack expansion type */
16963 declarator->parameter_pack_p = true;
16965 decl_specifiers.type = make_pack_expansion (type);
16969 /* The restriction on defining new types applies only to the type
16970 of the parameter, not to the default argument. */
16971 parser->type_definition_forbidden_message = saved_message;
16973 /* If the next token is `=', then process a default argument. */
16974 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16976 /* If we are defining a class, then the tokens that make up the
16977 default argument must be saved and processed later. */
16978 if (!template_parm_p && at_class_scope_p ()
16979 && TYPE_BEING_DEFINED (current_class_type)
16980 && !LAMBDA_TYPE_P (current_class_type))
16982 unsigned depth = 0;
16983 int maybe_template_id = 0;
16984 cp_token *first_token;
16987 /* Add tokens until we have processed the entire default
16988 argument. We add the range [first_token, token). */
16989 first_token = cp_lexer_peek_token (parser->lexer);
16994 /* Peek at the next token. */
16995 token = cp_lexer_peek_token (parser->lexer);
16996 /* What we do depends on what token we have. */
16997 switch (token->type)
16999 /* In valid code, a default argument must be
17000 immediately followed by a `,' `)', or `...'. */
17002 if (depth == 0 && maybe_template_id)
17004 /* If we've seen a '<', we might be in a
17005 template-argument-list. Until Core issue 325 is
17006 resolved, we don't know how this situation ought
17007 to be handled, so try to DTRT. We check whether
17008 what comes after the comma is a valid parameter
17009 declaration list. If it is, then the comma ends
17010 the default argument; otherwise the default
17011 argument continues. */
17012 bool error = false;
17015 /* Set ITALP so cp_parser_parameter_declaration_list
17016 doesn't decide to commit to this parse. */
17017 bool saved_italp = parser->in_template_argument_list_p;
17018 parser->in_template_argument_list_p = true;
17020 cp_parser_parse_tentatively (parser);
17021 cp_lexer_consume_token (parser->lexer);
17022 begin_scope (sk_function_parms, NULL_TREE);
17023 cp_parser_parameter_declaration_list (parser, &error);
17024 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17025 pop_binding (DECL_NAME (t), t);
17027 if (!cp_parser_error_occurred (parser) && !error)
17029 cp_parser_abort_tentative_parse (parser);
17031 parser->in_template_argument_list_p = saved_italp;
17034 case CPP_CLOSE_PAREN:
17036 /* If we run into a non-nested `;', `}', or `]',
17037 then the code is invalid -- but the default
17038 argument is certainly over. */
17039 case CPP_SEMICOLON:
17040 case CPP_CLOSE_BRACE:
17041 case CPP_CLOSE_SQUARE:
17044 /* Update DEPTH, if necessary. */
17045 else if (token->type == CPP_CLOSE_PAREN
17046 || token->type == CPP_CLOSE_BRACE
17047 || token->type == CPP_CLOSE_SQUARE)
17051 case CPP_OPEN_PAREN:
17052 case CPP_OPEN_SQUARE:
17053 case CPP_OPEN_BRACE:
17059 /* This might be the comparison operator, or it might
17060 start a template argument list. */
17061 ++maybe_template_id;
17065 if (cxx_dialect == cxx98)
17067 /* Fall through for C++0x, which treats the `>>'
17068 operator like two `>' tokens in certain
17074 /* This might be an operator, or it might close a
17075 template argument list. But if a previous '<'
17076 started a template argument list, this will have
17077 closed it, so we can't be in one anymore. */
17078 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17079 if (maybe_template_id < 0)
17080 maybe_template_id = 0;
17084 /* If we run out of tokens, issue an error message. */
17086 case CPP_PRAGMA_EOL:
17087 error_at (token->location, "file ends in default argument");
17093 /* In these cases, we should look for template-ids.
17094 For example, if the default argument is
17095 `X<int, double>()', we need to do name lookup to
17096 figure out whether or not `X' is a template; if
17097 so, the `,' does not end the default argument.
17099 That is not yet done. */
17106 /* If we've reached the end, stop. */
17110 /* Add the token to the token block. */
17111 token = cp_lexer_consume_token (parser->lexer);
17114 /* Create a DEFAULT_ARG to represent the unparsed default
17116 default_argument = make_node (DEFAULT_ARG);
17117 DEFARG_TOKENS (default_argument)
17118 = cp_token_cache_new (first_token, token);
17119 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17121 /* Outside of a class definition, we can just parse the
17122 assignment-expression. */
17125 token = cp_lexer_peek_token (parser->lexer);
17127 = cp_parser_default_argument (parser, template_parm_p);
17130 if (!parser->default_arg_ok_p)
17132 if (flag_permissive)
17133 warning (0, "deprecated use of default argument for parameter of non-function");
17136 error_at (token->location,
17137 "default arguments are only "
17138 "permitted for function parameters");
17139 default_argument = NULL_TREE;
17142 else if ((declarator && declarator->parameter_pack_p)
17143 || (decl_specifiers.type
17144 && PACK_EXPANSION_P (decl_specifiers.type)))
17146 /* Find the name of the parameter pack. */
17147 cp_declarator *id_declarator = declarator;
17148 while (id_declarator && id_declarator->kind != cdk_id)
17149 id_declarator = id_declarator->declarator;
17151 if (id_declarator && id_declarator->kind == cdk_id)
17152 error_at (declarator_token_start->location,
17154 ? G_("template parameter pack %qD "
17155 "cannot have a default argument")
17156 : G_("parameter pack %qD cannot have "
17157 "a default argument"),
17158 id_declarator->u.id.unqualified_name);
17160 error_at (declarator_token_start->location,
17162 ? G_("template parameter pack cannot have "
17163 "a default argument")
17164 : G_("parameter pack cannot have a "
17165 "default argument"));
17167 default_argument = NULL_TREE;
17171 default_argument = NULL_TREE;
17173 return make_parameter_declarator (&decl_specifiers,
17178 /* Parse a default argument and return it.
17180 TEMPLATE_PARM_P is true if this is a default argument for a
17181 non-type template parameter. */
17183 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17185 tree default_argument = NULL_TREE;
17186 bool saved_greater_than_is_operator_p;
17187 bool saved_local_variables_forbidden_p;
17188 bool non_constant_p, is_direct_init;
17190 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17192 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17193 parser->greater_than_is_operator_p = !template_parm_p;
17194 /* Local variable names (and the `this' keyword) may not
17195 appear in a default argument. */
17196 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17197 parser->local_variables_forbidden_p = true;
17198 /* Parse the assignment-expression. */
17199 if (template_parm_p)
17200 push_deferring_access_checks (dk_no_deferred);
17202 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17203 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17204 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17205 if (template_parm_p)
17206 pop_deferring_access_checks ();
17207 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17208 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17210 return default_argument;
17213 /* Parse a function-body.
17216 compound_statement */
17219 cp_parser_function_body (cp_parser *parser)
17221 cp_parser_compound_statement (parser, NULL, false, true);
17224 /* Parse a ctor-initializer-opt followed by a function-body. Return
17225 true if a ctor-initializer was present. */
17228 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17231 bool ctor_initializer_p;
17232 const bool check_body_p =
17233 DECL_CONSTRUCTOR_P (current_function_decl)
17234 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17237 /* Begin the function body. */
17238 body = begin_function_body ();
17239 /* Parse the optional ctor-initializer. */
17240 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17242 /* If we're parsing a constexpr constructor definition, we need
17243 to check that the constructor body is indeed empty. However,
17244 before we get to cp_parser_function_body lot of junk has been
17245 generated, so we can't just check that we have an empty block.
17246 Rather we take a snapshot of the outermost block, and check whether
17247 cp_parser_function_body changed its state. */
17251 if (TREE_CODE (list) == BIND_EXPR)
17252 list = BIND_EXPR_BODY (list);
17253 if (TREE_CODE (list) == STATEMENT_LIST
17254 && STATEMENT_LIST_TAIL (list) != NULL)
17255 last = STATEMENT_LIST_TAIL (list)->stmt;
17257 /* Parse the function-body. */
17258 cp_parser_function_body (parser);
17260 check_constexpr_ctor_body (last, list);
17261 /* Finish the function body. */
17262 finish_function_body (body);
17264 return ctor_initializer_p;
17267 /* Parse an initializer.
17270 = initializer-clause
17271 ( expression-list )
17273 Returns an expression representing the initializer. If no
17274 initializer is present, NULL_TREE is returned.
17276 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17277 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17278 set to TRUE if there is no initializer present. If there is an
17279 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17280 is set to true; otherwise it is set to false. */
17283 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17284 bool* non_constant_p)
17289 /* Peek at the next token. */
17290 token = cp_lexer_peek_token (parser->lexer);
17292 /* Let our caller know whether or not this initializer was
17294 *is_direct_init = (token->type != CPP_EQ);
17295 /* Assume that the initializer is constant. */
17296 *non_constant_p = false;
17298 if (token->type == CPP_EQ)
17300 /* Consume the `='. */
17301 cp_lexer_consume_token (parser->lexer);
17302 /* Parse the initializer-clause. */
17303 init = cp_parser_initializer_clause (parser, non_constant_p);
17305 else if (token->type == CPP_OPEN_PAREN)
17308 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17310 /*allow_expansion_p=*/true,
17313 return error_mark_node;
17314 init = build_tree_list_vec (vec);
17315 release_tree_vector (vec);
17317 else if (token->type == CPP_OPEN_BRACE)
17319 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17320 init = cp_parser_braced_list (parser, non_constant_p);
17321 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17325 /* Anything else is an error. */
17326 cp_parser_error (parser, "expected initializer");
17327 init = error_mark_node;
17333 /* Parse an initializer-clause.
17335 initializer-clause:
17336 assignment-expression
17339 Returns an expression representing the initializer.
17341 If the `assignment-expression' production is used the value
17342 returned is simply a representation for the expression.
17344 Otherwise, calls cp_parser_braced_list. */
17347 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17351 /* Assume the expression is constant. */
17352 *non_constant_p = false;
17354 /* If it is not a `{', then we are looking at an
17355 assignment-expression. */
17356 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17359 = cp_parser_constant_expression (parser,
17360 /*allow_non_constant_p=*/true,
17364 initializer = cp_parser_braced_list (parser, non_constant_p);
17366 return initializer;
17369 /* Parse a brace-enclosed initializer list.
17372 { initializer-list , [opt] }
17375 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17376 the elements of the initializer-list (or NULL, if the last
17377 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17378 NULL_TREE. There is no way to detect whether or not the optional
17379 trailing `,' was provided. NON_CONSTANT_P is as for
17380 cp_parser_initializer. */
17383 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17387 /* Consume the `{' token. */
17388 cp_lexer_consume_token (parser->lexer);
17389 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17390 initializer = make_node (CONSTRUCTOR);
17391 /* If it's not a `}', then there is a non-trivial initializer. */
17392 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17394 /* Parse the initializer list. */
17395 CONSTRUCTOR_ELTS (initializer)
17396 = cp_parser_initializer_list (parser, non_constant_p);
17397 /* A trailing `,' token is allowed. */
17398 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17399 cp_lexer_consume_token (parser->lexer);
17401 /* Now, there should be a trailing `}'. */
17402 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17403 TREE_TYPE (initializer) = init_list_type_node;
17404 return initializer;
17407 /* Parse an initializer-list.
17410 initializer-clause ... [opt]
17411 initializer-list , initializer-clause ... [opt]
17416 designation initializer-clause ...[opt]
17417 initializer-list , designation initializer-clause ...[opt]
17422 [ constant-expression ] =
17424 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17425 for the initializer. If the INDEX of the elt is non-NULL, it is the
17426 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17427 as for cp_parser_initializer. */
17429 static VEC(constructor_elt,gc) *
17430 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17432 VEC(constructor_elt,gc) *v = NULL;
17434 /* Assume all of the expressions are constant. */
17435 *non_constant_p = false;
17437 /* Parse the rest of the list. */
17443 bool clause_non_constant_p;
17445 /* If the next token is an identifier and the following one is a
17446 colon, we are looking at the GNU designated-initializer
17448 if (cp_parser_allow_gnu_extensions_p (parser)
17449 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17450 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17452 /* Warn the user that they are using an extension. */
17453 pedwarn (input_location, OPT_pedantic,
17454 "ISO C++ does not allow designated initializers");
17455 /* Consume the identifier. */
17456 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17457 /* Consume the `:'. */
17458 cp_lexer_consume_token (parser->lexer);
17460 /* Also handle the C99 syntax, '. id ='. */
17461 else if (cp_parser_allow_gnu_extensions_p (parser)
17462 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17463 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17464 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17466 /* Warn the user that they are using an extension. */
17467 pedwarn (input_location, OPT_pedantic,
17468 "ISO C++ does not allow C99 designated initializers");
17469 /* Consume the `.'. */
17470 cp_lexer_consume_token (parser->lexer);
17471 /* Consume the identifier. */
17472 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17473 /* Consume the `='. */
17474 cp_lexer_consume_token (parser->lexer);
17476 /* Also handle C99 array designators, '[ const ] ='. */
17477 else if (cp_parser_allow_gnu_extensions_p (parser)
17478 && !c_dialect_objc ()
17479 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17481 cp_lexer_consume_token (parser->lexer);
17482 designator = cp_parser_constant_expression (parser, false, NULL);
17483 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17484 cp_parser_require (parser, CPP_EQ, RT_EQ);
17487 designator = NULL_TREE;
17489 /* Parse the initializer. */
17490 initializer = cp_parser_initializer_clause (parser,
17491 &clause_non_constant_p);
17492 /* If any clause is non-constant, so is the entire initializer. */
17493 if (clause_non_constant_p)
17494 *non_constant_p = true;
17496 /* If we have an ellipsis, this is an initializer pack
17498 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17500 /* Consume the `...'. */
17501 cp_lexer_consume_token (parser->lexer);
17503 /* Turn the initializer into an initializer expansion. */
17504 initializer = make_pack_expansion (initializer);
17507 /* Add it to the vector. */
17508 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17510 /* If the next token is not a comma, we have reached the end of
17512 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17515 /* Peek at the next token. */
17516 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17517 /* If the next token is a `}', then we're still done. An
17518 initializer-clause can have a trailing `,' after the
17519 initializer-list and before the closing `}'. */
17520 if (token->type == CPP_CLOSE_BRACE)
17523 /* Consume the `,' token. */
17524 cp_lexer_consume_token (parser->lexer);
17530 /* Classes [gram.class] */
17532 /* Parse a class-name.
17538 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17539 to indicate that names looked up in dependent types should be
17540 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17541 keyword has been used to indicate that the name that appears next
17542 is a template. TAG_TYPE indicates the explicit tag given before
17543 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17544 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17545 is the class being defined in a class-head.
17547 Returns the TYPE_DECL representing the class. */
17550 cp_parser_class_name (cp_parser *parser,
17551 bool typename_keyword_p,
17552 bool template_keyword_p,
17553 enum tag_types tag_type,
17554 bool check_dependency_p,
17556 bool is_declaration)
17562 tree identifier = NULL_TREE;
17564 /* All class-names start with an identifier. */
17565 token = cp_lexer_peek_token (parser->lexer);
17566 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17568 cp_parser_error (parser, "expected class-name");
17569 return error_mark_node;
17572 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17573 to a template-id, so we save it here. */
17574 scope = parser->scope;
17575 if (scope == error_mark_node)
17576 return error_mark_node;
17578 /* Any name names a type if we're following the `typename' keyword
17579 in a qualified name where the enclosing scope is type-dependent. */
17580 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17581 && dependent_type_p (scope));
17582 /* Handle the common case (an identifier, but not a template-id)
17584 if (token->type == CPP_NAME
17585 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17587 cp_token *identifier_token;
17590 /* Look for the identifier. */
17591 identifier_token = cp_lexer_peek_token (parser->lexer);
17592 ambiguous_p = identifier_token->ambiguous_p;
17593 identifier = cp_parser_identifier (parser);
17594 /* If the next token isn't an identifier, we are certainly not
17595 looking at a class-name. */
17596 if (identifier == error_mark_node)
17597 decl = error_mark_node;
17598 /* If we know this is a type-name, there's no need to look it
17600 else if (typename_p)
17604 tree ambiguous_decls;
17605 /* If we already know that this lookup is ambiguous, then
17606 we've already issued an error message; there's no reason
17610 cp_parser_simulate_error (parser);
17611 return error_mark_node;
17613 /* If the next token is a `::', then the name must be a type
17616 [basic.lookup.qual]
17618 During the lookup for a name preceding the :: scope
17619 resolution operator, object, function, and enumerator
17620 names are ignored. */
17621 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17622 tag_type = typename_type;
17623 /* Look up the name. */
17624 decl = cp_parser_lookup_name (parser, identifier,
17626 /*is_template=*/false,
17627 /*is_namespace=*/false,
17628 check_dependency_p,
17630 identifier_token->location);
17631 if (ambiguous_decls)
17633 if (cp_parser_parsing_tentatively (parser))
17634 cp_parser_simulate_error (parser);
17635 return error_mark_node;
17641 /* Try a template-id. */
17642 decl = cp_parser_template_id (parser, template_keyword_p,
17643 check_dependency_p,
17645 if (decl == error_mark_node)
17646 return error_mark_node;
17649 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17651 /* If this is a typename, create a TYPENAME_TYPE. */
17652 if (typename_p && decl != error_mark_node)
17654 decl = make_typename_type (scope, decl, typename_type,
17655 /*complain=*/tf_error);
17656 if (decl != error_mark_node)
17657 decl = TYPE_NAME (decl);
17660 /* Check to see that it is really the name of a class. */
17661 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17662 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17663 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17664 /* Situations like this:
17666 template <typename T> struct A {
17667 typename T::template X<int>::I i;
17670 are problematic. Is `T::template X<int>' a class-name? The
17671 standard does not seem to be definitive, but there is no other
17672 valid interpretation of the following `::'. Therefore, those
17673 names are considered class-names. */
17675 decl = make_typename_type (scope, decl, tag_type, tf_error);
17676 if (decl != error_mark_node)
17677 decl = TYPE_NAME (decl);
17679 else if (TREE_CODE (decl) != TYPE_DECL
17680 || TREE_TYPE (decl) == error_mark_node
17681 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17682 /* In Objective-C 2.0, a classname followed by '.' starts a
17683 dot-syntax expression, and it's not a type-name. */
17684 || (c_dialect_objc ()
17685 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17686 && objc_is_class_name (decl)))
17687 decl = error_mark_node;
17689 if (decl == error_mark_node)
17690 cp_parser_error (parser, "expected class-name");
17691 else if (identifier && !parser->scope)
17692 maybe_note_name_used_in_class (identifier, decl);
17697 /* Parse a class-specifier.
17700 class-head { member-specification [opt] }
17702 Returns the TREE_TYPE representing the class. */
17705 cp_parser_class_specifier_1 (cp_parser* parser)
17708 tree attributes = NULL_TREE;
17709 bool nested_name_specifier_p;
17710 unsigned saved_num_template_parameter_lists;
17711 bool saved_in_function_body;
17712 unsigned char in_statement;
17713 bool in_switch_statement_p;
17714 bool saved_in_unbraced_linkage_specification_p;
17715 tree old_scope = NULL_TREE;
17716 tree scope = NULL_TREE;
17718 cp_token *closing_brace;
17720 push_deferring_access_checks (dk_no_deferred);
17722 /* Parse the class-head. */
17723 type = cp_parser_class_head (parser,
17724 &nested_name_specifier_p,
17727 /* If the class-head was a semantic disaster, skip the entire body
17731 cp_parser_skip_to_end_of_block_or_statement (parser);
17732 pop_deferring_access_checks ();
17733 return error_mark_node;
17736 /* Look for the `{'. */
17737 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17739 pop_deferring_access_checks ();
17740 return error_mark_node;
17743 /* Process the base classes. If they're invalid, skip the
17744 entire class body. */
17745 if (!xref_basetypes (type, bases))
17747 /* Consuming the closing brace yields better error messages
17749 if (cp_parser_skip_to_closing_brace (parser))
17750 cp_lexer_consume_token (parser->lexer);
17751 pop_deferring_access_checks ();
17752 return error_mark_node;
17755 /* Issue an error message if type-definitions are forbidden here. */
17756 cp_parser_check_type_definition (parser);
17757 /* Remember that we are defining one more class. */
17758 ++parser->num_classes_being_defined;
17759 /* Inside the class, surrounding template-parameter-lists do not
17761 saved_num_template_parameter_lists
17762 = parser->num_template_parameter_lists;
17763 parser->num_template_parameter_lists = 0;
17764 /* We are not in a function body. */
17765 saved_in_function_body = parser->in_function_body;
17766 parser->in_function_body = false;
17767 /* Or in a loop. */
17768 in_statement = parser->in_statement;
17769 parser->in_statement = 0;
17770 /* Or in a switch. */
17771 in_switch_statement_p = parser->in_switch_statement_p;
17772 parser->in_switch_statement_p = false;
17773 /* We are not immediately inside an extern "lang" block. */
17774 saved_in_unbraced_linkage_specification_p
17775 = parser->in_unbraced_linkage_specification_p;
17776 parser->in_unbraced_linkage_specification_p = false;
17778 /* Start the class. */
17779 if (nested_name_specifier_p)
17781 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17782 old_scope = push_inner_scope (scope);
17784 type = begin_class_definition (type, attributes);
17786 if (type == error_mark_node)
17787 /* If the type is erroneous, skip the entire body of the class. */
17788 cp_parser_skip_to_closing_brace (parser);
17790 /* Parse the member-specification. */
17791 cp_parser_member_specification_opt (parser);
17793 /* Look for the trailing `}'. */
17794 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17795 /* Look for trailing attributes to apply to this class. */
17796 if (cp_parser_allow_gnu_extensions_p (parser))
17797 attributes = cp_parser_attributes_opt (parser);
17798 if (type != error_mark_node)
17799 type = finish_struct (type, attributes);
17800 if (nested_name_specifier_p)
17801 pop_inner_scope (old_scope, scope);
17803 /* We've finished a type definition. Check for the common syntax
17804 error of forgetting a semicolon after the definition. We need to
17805 be careful, as we can't just check for not-a-semicolon and be done
17806 with it; the user might have typed:
17808 class X { } c = ...;
17809 class X { } *p = ...;
17811 and so forth. Instead, enumerate all the possible tokens that
17812 might follow this production; if we don't see one of them, then
17813 complain and silently insert the semicolon. */
17815 cp_token *token = cp_lexer_peek_token (parser->lexer);
17816 bool want_semicolon = true;
17818 switch (token->type)
17821 case CPP_SEMICOLON:
17824 case CPP_OPEN_PAREN:
17825 case CPP_CLOSE_PAREN:
17827 want_semicolon = false;
17830 /* While it's legal for type qualifiers and storage class
17831 specifiers to follow type definitions in the grammar, only
17832 compiler testsuites contain code like that. Assume that if
17833 we see such code, then what we're really seeing is a case
17837 const <type> var = ...;
17842 static <type> func (...) ...
17844 i.e. the qualifier or specifier applies to the next
17845 declaration. To do so, however, we need to look ahead one
17846 more token to see if *that* token is a type specifier.
17848 This code could be improved to handle:
17851 static const <type> var = ...; */
17853 if (keyword_is_decl_specifier (token->keyword))
17855 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17857 /* Handling user-defined types here would be nice, but very
17860 = (lookahead->type == CPP_KEYWORD
17861 && keyword_begins_type_specifier (lookahead->keyword));
17868 /* If we don't have a type, then something is very wrong and we
17869 shouldn't try to do anything clever. Likewise for not seeing the
17871 if (closing_brace && TYPE_P (type) && want_semicolon)
17873 cp_token_position prev
17874 = cp_lexer_previous_token_position (parser->lexer);
17875 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17876 location_t loc = prev_token->location;
17878 if (CLASSTYPE_DECLARED_CLASS (type))
17879 error_at (loc, "expected %<;%> after class definition");
17880 else if (TREE_CODE (type) == RECORD_TYPE)
17881 error_at (loc, "expected %<;%> after struct definition");
17882 else if (TREE_CODE (type) == UNION_TYPE)
17883 error_at (loc, "expected %<;%> after union definition");
17885 gcc_unreachable ();
17887 /* Unget one token and smash it to look as though we encountered
17888 a semicolon in the input stream. */
17889 cp_lexer_set_token_position (parser->lexer, prev);
17890 token = cp_lexer_peek_token (parser->lexer);
17891 token->type = CPP_SEMICOLON;
17892 token->keyword = RID_MAX;
17896 /* If this class is not itself within the scope of another class,
17897 then we need to parse the bodies of all of the queued function
17898 definitions. Note that the queued functions defined in a class
17899 are not always processed immediately following the
17900 class-specifier for that class. Consider:
17903 struct B { void f() { sizeof (A); } };
17906 If `f' were processed before the processing of `A' were
17907 completed, there would be no way to compute the size of `A'.
17908 Note that the nesting we are interested in here is lexical --
17909 not the semantic nesting given by TYPE_CONTEXT. In particular,
17912 struct A { struct B; };
17913 struct A::B { void f() { } };
17915 there is no need to delay the parsing of `A::B::f'. */
17916 if (--parser->num_classes_being_defined == 0)
17919 tree class_type = NULL_TREE;
17920 tree pushed_scope = NULL_TREE;
17922 cp_default_arg_entry *e;
17923 tree save_ccp, save_ccr;
17925 /* In a first pass, parse default arguments to the functions.
17926 Then, in a second pass, parse the bodies of the functions.
17927 This two-phased approach handles cases like:
17935 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17939 /* If there are default arguments that have not yet been processed,
17940 take care of them now. */
17941 if (class_type != e->class_type)
17944 pop_scope (pushed_scope);
17945 class_type = e->class_type;
17946 pushed_scope = push_scope (class_type);
17948 /* Make sure that any template parameters are in scope. */
17949 maybe_begin_member_template_processing (decl);
17950 /* Parse the default argument expressions. */
17951 cp_parser_late_parsing_default_args (parser, decl);
17952 /* Remove any template parameters from the symbol table. */
17953 maybe_end_member_template_processing ();
17955 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17956 /* Now parse any NSDMIs. */
17957 save_ccp = current_class_ptr;
17958 save_ccr = current_class_ref;
17959 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
17961 if (class_type != DECL_CONTEXT (decl))
17964 pop_scope (pushed_scope);
17965 class_type = DECL_CONTEXT (decl);
17966 pushed_scope = push_scope (class_type);
17968 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
17969 cp_parser_late_parsing_nsdmi (parser, decl);
17971 VEC_truncate (tree, unparsed_nsdmis, 0);
17972 current_class_ptr = save_ccp;
17973 current_class_ref = save_ccr;
17975 pop_scope (pushed_scope);
17976 /* Now parse the body of the functions. */
17977 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
17978 cp_parser_late_parsing_for_member (parser, decl);
17979 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17982 /* Put back any saved access checks. */
17983 pop_deferring_access_checks ();
17985 /* Restore saved state. */
17986 parser->in_switch_statement_p = in_switch_statement_p;
17987 parser->in_statement = in_statement;
17988 parser->in_function_body = saved_in_function_body;
17989 parser->num_template_parameter_lists
17990 = saved_num_template_parameter_lists;
17991 parser->in_unbraced_linkage_specification_p
17992 = saved_in_unbraced_linkage_specification_p;
17998 cp_parser_class_specifier (cp_parser* parser)
18001 timevar_push (TV_PARSE_STRUCT);
18002 ret = cp_parser_class_specifier_1 (parser);
18003 timevar_pop (TV_PARSE_STRUCT);
18007 /* Parse a class-head.
18010 class-key identifier [opt] base-clause [opt]
18011 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18012 class-key nested-name-specifier [opt] template-id
18015 class-virt-specifier:
18019 class-key attributes identifier [opt] base-clause [opt]
18020 class-key attributes nested-name-specifier identifier base-clause [opt]
18021 class-key attributes nested-name-specifier [opt] template-id
18024 Upon return BASES is initialized to the list of base classes (or
18025 NULL, if there are none) in the same form returned by
18026 cp_parser_base_clause.
18028 Returns the TYPE of the indicated class. Sets
18029 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18030 involving a nested-name-specifier was used, and FALSE otherwise.
18032 Returns error_mark_node if this is not a class-head.
18034 Returns NULL_TREE if the class-head is syntactically valid, but
18035 semantically invalid in a way that means we should skip the entire
18036 body of the class. */
18039 cp_parser_class_head (cp_parser* parser,
18040 bool* nested_name_specifier_p,
18041 tree *attributes_p,
18044 tree nested_name_specifier;
18045 enum tag_types class_key;
18046 tree id = NULL_TREE;
18047 tree type = NULL_TREE;
18049 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18050 bool template_id_p = false;
18051 bool qualified_p = false;
18052 bool invalid_nested_name_p = false;
18053 bool invalid_explicit_specialization_p = false;
18054 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18055 tree pushed_scope = NULL_TREE;
18056 unsigned num_templates;
18057 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18058 /* Assume no nested-name-specifier will be present. */
18059 *nested_name_specifier_p = false;
18060 /* Assume no template parameter lists will be used in defining the
18063 parser->colon_corrects_to_scope_p = false;
18065 *bases = NULL_TREE;
18067 /* Look for the class-key. */
18068 class_key = cp_parser_class_key (parser);
18069 if (class_key == none_type)
18070 return error_mark_node;
18072 /* Parse the attributes. */
18073 attributes = cp_parser_attributes_opt (parser);
18075 /* If the next token is `::', that is invalid -- but sometimes
18076 people do try to write:
18080 Handle this gracefully by accepting the extra qualifier, and then
18081 issuing an error about it later if this really is a
18082 class-head. If it turns out just to be an elaborated type
18083 specifier, remain silent. */
18084 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18085 qualified_p = true;
18087 push_deferring_access_checks (dk_no_check);
18089 /* Determine the name of the class. Begin by looking for an
18090 optional nested-name-specifier. */
18091 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18092 nested_name_specifier
18093 = cp_parser_nested_name_specifier_opt (parser,
18094 /*typename_keyword_p=*/false,
18095 /*check_dependency_p=*/false,
18097 /*is_declaration=*/false);
18098 /* If there was a nested-name-specifier, then there *must* be an
18100 if (nested_name_specifier)
18102 type_start_token = cp_lexer_peek_token (parser->lexer);
18103 /* Although the grammar says `identifier', it really means
18104 `class-name' or `template-name'. You are only allowed to
18105 define a class that has already been declared with this
18108 The proposed resolution for Core Issue 180 says that wherever
18109 you see `class T::X' you should treat `X' as a type-name.
18111 It is OK to define an inaccessible class; for example:
18113 class A { class B; };
18116 We do not know if we will see a class-name, or a
18117 template-name. We look for a class-name first, in case the
18118 class-name is a template-id; if we looked for the
18119 template-name first we would stop after the template-name. */
18120 cp_parser_parse_tentatively (parser);
18121 type = cp_parser_class_name (parser,
18122 /*typename_keyword_p=*/false,
18123 /*template_keyword_p=*/false,
18125 /*check_dependency_p=*/false,
18126 /*class_head_p=*/true,
18127 /*is_declaration=*/false);
18128 /* If that didn't work, ignore the nested-name-specifier. */
18129 if (!cp_parser_parse_definitely (parser))
18131 invalid_nested_name_p = true;
18132 type_start_token = cp_lexer_peek_token (parser->lexer);
18133 id = cp_parser_identifier (parser);
18134 if (id == error_mark_node)
18137 /* If we could not find a corresponding TYPE, treat this
18138 declaration like an unqualified declaration. */
18139 if (type == error_mark_node)
18140 nested_name_specifier = NULL_TREE;
18141 /* Otherwise, count the number of templates used in TYPE and its
18142 containing scopes. */
18147 for (scope = TREE_TYPE (type);
18148 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18149 scope = (TYPE_P (scope)
18150 ? TYPE_CONTEXT (scope)
18151 : DECL_CONTEXT (scope)))
18153 && CLASS_TYPE_P (scope)
18154 && CLASSTYPE_TEMPLATE_INFO (scope)
18155 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18156 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18160 /* Otherwise, the identifier is optional. */
18163 /* We don't know whether what comes next is a template-id,
18164 an identifier, or nothing at all. */
18165 cp_parser_parse_tentatively (parser);
18166 /* Check for a template-id. */
18167 type_start_token = cp_lexer_peek_token (parser->lexer);
18168 id = cp_parser_template_id (parser,
18169 /*template_keyword_p=*/false,
18170 /*check_dependency_p=*/true,
18171 /*is_declaration=*/true);
18172 /* If that didn't work, it could still be an identifier. */
18173 if (!cp_parser_parse_definitely (parser))
18175 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18177 type_start_token = cp_lexer_peek_token (parser->lexer);
18178 id = cp_parser_identifier (parser);
18185 template_id_p = true;
18190 pop_deferring_access_checks ();
18194 cp_parser_check_for_invalid_template_id (parser, id,
18195 type_start_token->location);
18197 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18199 /* If it's not a `:' or a `{' then we can't really be looking at a
18200 class-head, since a class-head only appears as part of a
18201 class-specifier. We have to detect this situation before calling
18202 xref_tag, since that has irreversible side-effects. */
18203 if (!cp_parser_next_token_starts_class_definition_p (parser))
18205 cp_parser_error (parser, "expected %<{%> or %<:%>");
18206 type = error_mark_node;
18210 /* At this point, we're going ahead with the class-specifier, even
18211 if some other problem occurs. */
18212 cp_parser_commit_to_tentative_parse (parser);
18213 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18215 cp_parser_error (parser,
18216 "cannot specify %<override%> for a class");
18217 type = error_mark_node;
18220 /* Issue the error about the overly-qualified name now. */
18223 cp_parser_error (parser,
18224 "global qualification of class name is invalid");
18225 type = error_mark_node;
18228 else if (invalid_nested_name_p)
18230 cp_parser_error (parser,
18231 "qualified name does not name a class");
18232 type = error_mark_node;
18235 else if (nested_name_specifier)
18239 /* Reject typedef-names in class heads. */
18240 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18242 error_at (type_start_token->location,
18243 "invalid class name in declaration of %qD",
18249 /* Figure out in what scope the declaration is being placed. */
18250 scope = current_scope ();
18251 /* If that scope does not contain the scope in which the
18252 class was originally declared, the program is invalid. */
18253 if (scope && !is_ancestor (scope, nested_name_specifier))
18255 if (at_namespace_scope_p ())
18256 error_at (type_start_token->location,
18257 "declaration of %qD in namespace %qD which does not "
18259 type, scope, nested_name_specifier);
18261 error_at (type_start_token->location,
18262 "declaration of %qD in %qD which does not enclose %qD",
18263 type, scope, nested_name_specifier);
18269 A declarator-id shall not be qualified except for the
18270 definition of a ... nested class outside of its class
18271 ... [or] the definition or explicit instantiation of a
18272 class member of a namespace outside of its namespace. */
18273 if (scope == nested_name_specifier)
18275 permerror (nested_name_specifier_token_start->location,
18276 "extra qualification not allowed");
18277 nested_name_specifier = NULL_TREE;
18281 /* An explicit-specialization must be preceded by "template <>". If
18282 it is not, try to recover gracefully. */
18283 if (at_namespace_scope_p ()
18284 && parser->num_template_parameter_lists == 0
18287 error_at (type_start_token->location,
18288 "an explicit specialization must be preceded by %<template <>%>");
18289 invalid_explicit_specialization_p = true;
18290 /* Take the same action that would have been taken by
18291 cp_parser_explicit_specialization. */
18292 ++parser->num_template_parameter_lists;
18293 begin_specialization ();
18295 /* There must be no "return" statements between this point and the
18296 end of this function; set "type "to the correct return value and
18297 use "goto done;" to return. */
18298 /* Make sure that the right number of template parameters were
18300 if (!cp_parser_check_template_parameters (parser, num_templates,
18301 type_start_token->location,
18302 /*declarator=*/NULL))
18304 /* If something went wrong, there is no point in even trying to
18305 process the class-definition. */
18310 /* Look up the type. */
18313 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18314 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18315 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18317 error_at (type_start_token->location,
18318 "function template %qD redeclared as a class template", id);
18319 type = error_mark_node;
18323 type = TREE_TYPE (id);
18324 type = maybe_process_partial_specialization (type);
18326 if (nested_name_specifier)
18327 pushed_scope = push_scope (nested_name_specifier);
18329 else if (nested_name_specifier)
18335 template <typename T> struct S { struct T };
18336 template <typename T> struct S<T>::T { };
18338 we will get a TYPENAME_TYPE when processing the definition of
18339 `S::T'. We need to resolve it to the actual type before we
18340 try to define it. */
18341 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18343 class_type = resolve_typename_type (TREE_TYPE (type),
18344 /*only_current_p=*/false);
18345 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18346 type = TYPE_NAME (class_type);
18349 cp_parser_error (parser, "could not resolve typename type");
18350 type = error_mark_node;
18354 if (maybe_process_partial_specialization (TREE_TYPE (type))
18355 == error_mark_node)
18361 class_type = current_class_type;
18362 /* Enter the scope indicated by the nested-name-specifier. */
18363 pushed_scope = push_scope (nested_name_specifier);
18364 /* Get the canonical version of this type. */
18365 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18366 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18367 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18369 type = push_template_decl (type);
18370 if (type == error_mark_node)
18377 type = TREE_TYPE (type);
18378 *nested_name_specifier_p = true;
18380 else /* The name is not a nested name. */
18382 /* If the class was unnamed, create a dummy name. */
18384 id = make_anon_name ();
18385 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18386 parser->num_template_parameter_lists);
18389 /* Indicate whether this class was declared as a `class' or as a
18391 if (TREE_CODE (type) == RECORD_TYPE)
18392 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18393 cp_parser_check_class_key (class_key, type);
18395 /* If this type was already complete, and we see another definition,
18396 that's an error. */
18397 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18399 error_at (type_start_token->location, "redefinition of %q#T",
18401 error_at (type_start_token->location, "previous definition of %q+#T",
18406 else if (type == error_mark_node)
18409 /* We will have entered the scope containing the class; the names of
18410 base classes should be looked up in that context. For example:
18412 struct A { struct B {}; struct C; };
18413 struct A::C : B {};
18417 /* Get the list of base-classes, if there is one. */
18418 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18419 *bases = cp_parser_base_clause (parser);
18422 /* Leave the scope given by the nested-name-specifier. We will
18423 enter the class scope itself while processing the members. */
18425 pop_scope (pushed_scope);
18427 if (invalid_explicit_specialization_p)
18429 end_specialization ();
18430 --parser->num_template_parameter_lists;
18434 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18435 *attributes_p = attributes;
18436 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18437 CLASSTYPE_FINAL (type) = 1;
18439 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18443 /* Parse a class-key.
18450 Returns the kind of class-key specified, or none_type to indicate
18453 static enum tag_types
18454 cp_parser_class_key (cp_parser* parser)
18457 enum tag_types tag_type;
18459 /* Look for the class-key. */
18460 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18464 /* Check to see if the TOKEN is a class-key. */
18465 tag_type = cp_parser_token_is_class_key (token);
18467 cp_parser_error (parser, "expected class-key");
18471 /* Parse an (optional) member-specification.
18473 member-specification:
18474 member-declaration member-specification [opt]
18475 access-specifier : member-specification [opt] */
18478 cp_parser_member_specification_opt (cp_parser* parser)
18485 /* Peek at the next token. */
18486 token = cp_lexer_peek_token (parser->lexer);
18487 /* If it's a `}', or EOF then we've seen all the members. */
18488 if (token->type == CPP_CLOSE_BRACE
18489 || token->type == CPP_EOF
18490 || token->type == CPP_PRAGMA_EOL)
18493 /* See if this token is a keyword. */
18494 keyword = token->keyword;
18498 case RID_PROTECTED:
18500 /* Consume the access-specifier. */
18501 cp_lexer_consume_token (parser->lexer);
18502 /* Remember which access-specifier is active. */
18503 current_access_specifier = token->u.value;
18504 /* Look for the `:'. */
18505 cp_parser_require (parser, CPP_COLON, RT_COLON);
18509 /* Accept #pragmas at class scope. */
18510 if (token->type == CPP_PRAGMA)
18512 cp_parser_pragma (parser, pragma_external);
18516 /* Otherwise, the next construction must be a
18517 member-declaration. */
18518 cp_parser_member_declaration (parser);
18523 /* Parse a member-declaration.
18525 member-declaration:
18526 decl-specifier-seq [opt] member-declarator-list [opt] ;
18527 function-definition ; [opt]
18528 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18530 template-declaration
18532 member-declarator-list:
18534 member-declarator-list , member-declarator
18537 declarator pure-specifier [opt]
18538 declarator constant-initializer [opt]
18539 identifier [opt] : constant-expression
18543 member-declaration:
18544 __extension__ member-declaration
18547 declarator attributes [opt] pure-specifier [opt]
18548 declarator attributes [opt] constant-initializer [opt]
18549 identifier [opt] attributes [opt] : constant-expression
18553 member-declaration:
18554 static_assert-declaration */
18557 cp_parser_member_declaration (cp_parser* parser)
18559 cp_decl_specifier_seq decl_specifiers;
18560 tree prefix_attributes;
18562 int declares_class_or_enum;
18564 cp_token *token = NULL;
18565 cp_token *decl_spec_token_start = NULL;
18566 cp_token *initializer_token_start = NULL;
18567 int saved_pedantic;
18568 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18570 /* Check for the `__extension__' keyword. */
18571 if (cp_parser_extension_opt (parser, &saved_pedantic))
18574 cp_parser_member_declaration (parser);
18575 /* Restore the old value of the PEDANTIC flag. */
18576 pedantic = saved_pedantic;
18581 /* Check for a template-declaration. */
18582 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18584 /* An explicit specialization here is an error condition, and we
18585 expect the specialization handler to detect and report this. */
18586 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18587 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18588 cp_parser_explicit_specialization (parser);
18590 cp_parser_template_declaration (parser, /*member_p=*/true);
18595 /* Check for a using-declaration. */
18596 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18598 /* Parse the using-declaration. */
18599 cp_parser_using_declaration (parser,
18600 /*access_declaration_p=*/false);
18604 /* Check for @defs. */
18605 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18608 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18609 ivar = ivar_chains;
18613 ivar = TREE_CHAIN (member);
18614 TREE_CHAIN (member) = NULL_TREE;
18615 finish_member_declaration (member);
18620 /* If the next token is `static_assert' we have a static assertion. */
18621 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18623 cp_parser_static_assert (parser, /*member_p=*/true);
18627 parser->colon_corrects_to_scope_p = false;
18629 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18632 /* Parse the decl-specifier-seq. */
18633 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18634 cp_parser_decl_specifier_seq (parser,
18635 CP_PARSER_FLAGS_OPTIONAL,
18637 &declares_class_or_enum);
18638 prefix_attributes = decl_specifiers.attributes;
18639 decl_specifiers.attributes = NULL_TREE;
18640 /* Check for an invalid type-name. */
18641 if (!decl_specifiers.any_type_specifiers_p
18642 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18644 /* If there is no declarator, then the decl-specifier-seq should
18646 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18648 /* If there was no decl-specifier-seq, and the next token is a
18649 `;', then we have something like:
18655 Each member-declaration shall declare at least one member
18656 name of the class. */
18657 if (!decl_specifiers.any_specifiers_p)
18659 cp_token *token = cp_lexer_peek_token (parser->lexer);
18660 if (!in_system_header_at (token->location))
18661 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18667 /* See if this declaration is a friend. */
18668 friend_p = cp_parser_friend_p (&decl_specifiers);
18669 /* If there were decl-specifiers, check to see if there was
18670 a class-declaration. */
18671 type = check_tag_decl (&decl_specifiers);
18672 /* Nested classes have already been added to the class, but
18673 a `friend' needs to be explicitly registered. */
18676 /* If the `friend' keyword was present, the friend must
18677 be introduced with a class-key. */
18678 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18679 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18680 "in C++03 a class-key must be used "
18681 "when declaring a friend");
18684 template <typename T> struct A {
18685 friend struct A<T>::B;
18688 A<T>::B will be represented by a TYPENAME_TYPE, and
18689 therefore not recognized by check_tag_decl. */
18692 type = decl_specifiers.type;
18693 if (type && TREE_CODE (type) == TYPE_DECL)
18694 type = TREE_TYPE (type);
18696 if (!type || !TYPE_P (type))
18697 error_at (decl_spec_token_start->location,
18698 "friend declaration does not name a class or "
18701 make_friend_class (current_class_type, type,
18702 /*complain=*/true);
18704 /* If there is no TYPE, an error message will already have
18706 else if (!type || type == error_mark_node)
18708 /* An anonymous aggregate has to be handled specially; such
18709 a declaration really declares a data member (with a
18710 particular type), as opposed to a nested class. */
18711 else if (ANON_AGGR_TYPE_P (type))
18713 /* Remove constructors and such from TYPE, now that we
18714 know it is an anonymous aggregate. */
18715 fixup_anonymous_aggr (type);
18716 /* And make the corresponding data member. */
18717 decl = build_decl (decl_spec_token_start->location,
18718 FIELD_DECL, NULL_TREE, type);
18719 /* Add it to the class. */
18720 finish_member_declaration (decl);
18723 cp_parser_check_access_in_redeclaration
18725 decl_spec_token_start->location);
18730 bool assume_semicolon = false;
18732 /* See if these declarations will be friends. */
18733 friend_p = cp_parser_friend_p (&decl_specifiers);
18735 /* Keep going until we hit the `;' at the end of the
18737 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18739 tree attributes = NULL_TREE;
18740 tree first_attribute;
18742 /* Peek at the next token. */
18743 token = cp_lexer_peek_token (parser->lexer);
18745 /* Check for a bitfield declaration. */
18746 if (token->type == CPP_COLON
18747 || (token->type == CPP_NAME
18748 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18754 /* Get the name of the bitfield. Note that we cannot just
18755 check TOKEN here because it may have been invalidated by
18756 the call to cp_lexer_peek_nth_token above. */
18757 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18758 identifier = cp_parser_identifier (parser);
18760 identifier = NULL_TREE;
18762 /* Consume the `:' token. */
18763 cp_lexer_consume_token (parser->lexer);
18764 /* Get the width of the bitfield. */
18766 = cp_parser_constant_expression (parser,
18767 /*allow_non_constant=*/false,
18770 /* Look for attributes that apply to the bitfield. */
18771 attributes = cp_parser_attributes_opt (parser);
18772 /* Remember which attributes are prefix attributes and
18774 first_attribute = attributes;
18775 /* Combine the attributes. */
18776 attributes = chainon (prefix_attributes, attributes);
18778 /* Create the bitfield declaration. */
18779 decl = grokbitfield (identifier
18780 ? make_id_declarator (NULL_TREE,
18790 cp_declarator *declarator;
18792 tree asm_specification;
18793 int ctor_dtor_or_conv_p;
18795 /* Parse the declarator. */
18797 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18798 &ctor_dtor_or_conv_p,
18799 /*parenthesized_p=*/NULL,
18800 /*member_p=*/true);
18802 /* If something went wrong parsing the declarator, make sure
18803 that we at least consume some tokens. */
18804 if (declarator == cp_error_declarator)
18806 /* Skip to the end of the statement. */
18807 cp_parser_skip_to_end_of_statement (parser);
18808 /* If the next token is not a semicolon, that is
18809 probably because we just skipped over the body of
18810 a function. So, we consume a semicolon if
18811 present, but do not issue an error message if it
18813 if (cp_lexer_next_token_is (parser->lexer,
18815 cp_lexer_consume_token (parser->lexer);
18819 if (declares_class_or_enum & 2)
18820 cp_parser_check_for_definition_in_return_type
18821 (declarator, decl_specifiers.type,
18822 decl_specifiers.type_location);
18824 /* Look for an asm-specification. */
18825 asm_specification = cp_parser_asm_specification_opt (parser);
18826 /* Look for attributes that apply to the declaration. */
18827 attributes = cp_parser_attributes_opt (parser);
18828 /* Remember which attributes are prefix attributes and
18830 first_attribute = attributes;
18831 /* Combine the attributes. */
18832 attributes = chainon (prefix_attributes, attributes);
18834 /* If it's an `=', then we have a constant-initializer or a
18835 pure-specifier. It is not correct to parse the
18836 initializer before registering the member declaration
18837 since the member declaration should be in scope while
18838 its initializer is processed. However, the rest of the
18839 front end does not yet provide an interface that allows
18840 us to handle this correctly. */
18841 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18845 A pure-specifier shall be used only in the declaration of
18846 a virtual function.
18848 A member-declarator can contain a constant-initializer
18849 only if it declares a static member of integral or
18852 Therefore, if the DECLARATOR is for a function, we look
18853 for a pure-specifier; otherwise, we look for a
18854 constant-initializer. When we call `grokfield', it will
18855 perform more stringent semantics checks. */
18856 initializer_token_start = cp_lexer_peek_token (parser->lexer);
18857 if (function_declarator_p (declarator)
18858 || (decl_specifiers.type
18859 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18860 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18861 == FUNCTION_TYPE)))
18862 initializer = cp_parser_pure_specifier (parser);
18863 else if (decl_specifiers.storage_class != sc_static)
18864 initializer = cp_parser_save_nsdmi (parser);
18865 else if (cxx_dialect >= cxx0x)
18868 /* Don't require a constant rvalue in C++11, since we
18869 might want a reference constant. We'll enforce
18870 constancy later. */
18871 cp_lexer_consume_token (parser->lexer);
18872 /* Parse the initializer. */
18873 initializer = cp_parser_initializer_clause (parser,
18877 /* Parse the initializer. */
18878 initializer = cp_parser_constant_initializer (parser);
18880 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18881 && !function_declarator_p (declarator))
18884 if (decl_specifiers.storage_class != sc_static)
18885 initializer = cp_parser_save_nsdmi (parser);
18887 initializer = cp_parser_initializer (parser, &x, &x);
18889 /* Otherwise, there is no initializer. */
18891 initializer = NULL_TREE;
18893 /* See if we are probably looking at a function
18894 definition. We are certainly not looking at a
18895 member-declarator. Calling `grokfield' has
18896 side-effects, so we must not do it unless we are sure
18897 that we are looking at a member-declarator. */
18898 if (cp_parser_token_starts_function_definition_p
18899 (cp_lexer_peek_token (parser->lexer)))
18901 /* The grammar does not allow a pure-specifier to be
18902 used when a member function is defined. (It is
18903 possible that this fact is an oversight in the
18904 standard, since a pure function may be defined
18905 outside of the class-specifier. */
18907 error_at (initializer_token_start->location,
18908 "pure-specifier on function-definition");
18909 decl = cp_parser_save_member_function_body (parser,
18913 /* If the member was not a friend, declare it here. */
18915 finish_member_declaration (decl);
18916 /* Peek at the next token. */
18917 token = cp_lexer_peek_token (parser->lexer);
18918 /* If the next token is a semicolon, consume it. */
18919 if (token->type == CPP_SEMICOLON)
18920 cp_lexer_consume_token (parser->lexer);
18924 if (declarator->kind == cdk_function)
18925 declarator->id_loc = token->location;
18926 /* Create the declaration. */
18927 decl = grokfield (declarator, &decl_specifiers,
18928 initializer, /*init_const_expr_p=*/true,
18933 /* Reset PREFIX_ATTRIBUTES. */
18934 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18935 attributes = TREE_CHAIN (attributes);
18937 TREE_CHAIN (attributes) = NULL_TREE;
18939 /* If there is any qualification still in effect, clear it
18940 now; we will be starting fresh with the next declarator. */
18941 parser->scope = NULL_TREE;
18942 parser->qualifying_scope = NULL_TREE;
18943 parser->object_scope = NULL_TREE;
18944 /* If it's a `,', then there are more declarators. */
18945 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18946 cp_lexer_consume_token (parser->lexer);
18947 /* If the next token isn't a `;', then we have a parse error. */
18948 else if (cp_lexer_next_token_is_not (parser->lexer,
18951 /* The next token might be a ways away from where the
18952 actual semicolon is missing. Find the previous token
18953 and use that for our error position. */
18954 cp_token *token = cp_lexer_previous_token (parser->lexer);
18955 error_at (token->location,
18956 "expected %<;%> at end of member declaration");
18958 /* Assume that the user meant to provide a semicolon. If
18959 we were to cp_parser_skip_to_end_of_statement, we might
18960 skip to a semicolon inside a member function definition
18961 and issue nonsensical error messages. */
18962 assume_semicolon = true;
18967 /* Add DECL to the list of members. */
18969 finish_member_declaration (decl);
18971 if (TREE_CODE (decl) == FUNCTION_DECL)
18972 cp_parser_save_default_args (parser, decl);
18973 else if (TREE_CODE (decl) == FIELD_DECL
18974 && !DECL_C_BIT_FIELD (decl)
18975 && DECL_INITIAL (decl))
18976 /* Add DECL to the queue of NSDMI to be parsed later. */
18977 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
18980 if (assume_semicolon)
18985 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18987 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18990 /* Parse a pure-specifier.
18995 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18996 Otherwise, ERROR_MARK_NODE is returned. */
18999 cp_parser_pure_specifier (cp_parser* parser)
19003 /* Look for the `=' token. */
19004 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19005 return error_mark_node;
19006 /* Look for the `0' token. */
19007 token = cp_lexer_peek_token (parser->lexer);
19009 if (token->type == CPP_EOF
19010 || token->type == CPP_PRAGMA_EOL)
19011 return error_mark_node;
19013 cp_lexer_consume_token (parser->lexer);
19015 /* Accept = default or = delete in c++0x mode. */
19016 if (token->keyword == RID_DEFAULT
19017 || token->keyword == RID_DELETE)
19019 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19020 return token->u.value;
19023 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19024 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19026 cp_parser_error (parser,
19027 "invalid pure specifier (only %<= 0%> is allowed)");
19028 cp_parser_skip_to_end_of_statement (parser);
19029 return error_mark_node;
19031 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19033 error_at (token->location, "templates may not be %<virtual%>");
19034 return error_mark_node;
19037 return integer_zero_node;
19040 /* Parse a constant-initializer.
19042 constant-initializer:
19043 = constant-expression
19045 Returns a representation of the constant-expression. */
19048 cp_parser_constant_initializer (cp_parser* parser)
19050 /* Look for the `=' token. */
19051 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19052 return error_mark_node;
19054 /* It is invalid to write:
19056 struct S { static const int i = { 7 }; };
19059 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19061 cp_parser_error (parser,
19062 "a brace-enclosed initializer is not allowed here");
19063 /* Consume the opening brace. */
19064 cp_lexer_consume_token (parser->lexer);
19065 /* Skip the initializer. */
19066 cp_parser_skip_to_closing_brace (parser);
19067 /* Look for the trailing `}'. */
19068 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19070 return error_mark_node;
19073 return cp_parser_constant_expression (parser,
19074 /*allow_non_constant=*/false,
19078 /* Derived classes [gram.class.derived] */
19080 /* Parse a base-clause.
19083 : base-specifier-list
19085 base-specifier-list:
19086 base-specifier ... [opt]
19087 base-specifier-list , base-specifier ... [opt]
19089 Returns a TREE_LIST representing the base-classes, in the order in
19090 which they were declared. The representation of each node is as
19091 described by cp_parser_base_specifier.
19093 In the case that no bases are specified, this function will return
19094 NULL_TREE, not ERROR_MARK_NODE. */
19097 cp_parser_base_clause (cp_parser* parser)
19099 tree bases = NULL_TREE;
19101 /* Look for the `:' that begins the list. */
19102 cp_parser_require (parser, CPP_COLON, RT_COLON);
19104 /* Scan the base-specifier-list. */
19109 bool pack_expansion_p = false;
19111 /* Look for the base-specifier. */
19112 base = cp_parser_base_specifier (parser);
19113 /* Look for the (optional) ellipsis. */
19114 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19116 /* Consume the `...'. */
19117 cp_lexer_consume_token (parser->lexer);
19119 pack_expansion_p = true;
19122 /* Add BASE to the front of the list. */
19123 if (base && base != error_mark_node)
19125 if (pack_expansion_p)
19126 /* Make this a pack expansion type. */
19127 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19129 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19131 TREE_CHAIN (base) = bases;
19135 /* Peek at the next token. */
19136 token = cp_lexer_peek_token (parser->lexer);
19137 /* If it's not a comma, then the list is complete. */
19138 if (token->type != CPP_COMMA)
19140 /* Consume the `,'. */
19141 cp_lexer_consume_token (parser->lexer);
19144 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19145 base class had a qualified name. However, the next name that
19146 appears is certainly not qualified. */
19147 parser->scope = NULL_TREE;
19148 parser->qualifying_scope = NULL_TREE;
19149 parser->object_scope = NULL_TREE;
19151 return nreverse (bases);
19154 /* Parse a base-specifier.
19157 :: [opt] nested-name-specifier [opt] class-name
19158 virtual access-specifier [opt] :: [opt] nested-name-specifier
19160 access-specifier virtual [opt] :: [opt] nested-name-specifier
19163 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19164 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19165 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19166 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19169 cp_parser_base_specifier (cp_parser* parser)
19173 bool virtual_p = false;
19174 bool duplicate_virtual_error_issued_p = false;
19175 bool duplicate_access_error_issued_p = false;
19176 bool class_scope_p, template_p;
19177 tree access = access_default_node;
19180 /* Process the optional `virtual' and `access-specifier'. */
19183 /* Peek at the next token. */
19184 token = cp_lexer_peek_token (parser->lexer);
19185 /* Process `virtual'. */
19186 switch (token->keyword)
19189 /* If `virtual' appears more than once, issue an error. */
19190 if (virtual_p && !duplicate_virtual_error_issued_p)
19192 cp_parser_error (parser,
19193 "%<virtual%> specified more than once in base-specified");
19194 duplicate_virtual_error_issued_p = true;
19199 /* Consume the `virtual' token. */
19200 cp_lexer_consume_token (parser->lexer);
19205 case RID_PROTECTED:
19207 /* If more than one access specifier appears, issue an
19209 if (access != access_default_node
19210 && !duplicate_access_error_issued_p)
19212 cp_parser_error (parser,
19213 "more than one access specifier in base-specified");
19214 duplicate_access_error_issued_p = true;
19217 access = ridpointers[(int) token->keyword];
19219 /* Consume the access-specifier. */
19220 cp_lexer_consume_token (parser->lexer);
19229 /* It is not uncommon to see programs mechanically, erroneously, use
19230 the 'typename' keyword to denote (dependent) qualified types
19231 as base classes. */
19232 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19234 token = cp_lexer_peek_token (parser->lexer);
19235 if (!processing_template_decl)
19236 error_at (token->location,
19237 "keyword %<typename%> not allowed outside of templates");
19239 error_at (token->location,
19240 "keyword %<typename%> not allowed in this context "
19241 "(the base class is implicitly a type)");
19242 cp_lexer_consume_token (parser->lexer);
19245 /* Look for the optional `::' operator. */
19246 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19247 /* Look for the nested-name-specifier. The simplest way to
19252 The keyword `typename' is not permitted in a base-specifier or
19253 mem-initializer; in these contexts a qualified name that
19254 depends on a template-parameter is implicitly assumed to be a
19257 is to pretend that we have seen the `typename' keyword at this
19259 cp_parser_nested_name_specifier_opt (parser,
19260 /*typename_keyword_p=*/true,
19261 /*check_dependency_p=*/true,
19263 /*is_declaration=*/true);
19264 /* If the base class is given by a qualified name, assume that names
19265 we see are type names or templates, as appropriate. */
19266 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19267 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19270 && cp_lexer_next_token_is_decltype (parser->lexer))
19271 /* DR 950 allows decltype as a base-specifier. */
19272 type = cp_parser_decltype (parser);
19275 /* Otherwise, look for the class-name. */
19276 type = cp_parser_class_name (parser,
19280 /*check_dependency_p=*/true,
19281 /*class_head_p=*/false,
19282 /*is_declaration=*/true);
19283 type = TREE_TYPE (type);
19286 if (type == error_mark_node)
19287 return error_mark_node;
19289 return finish_base_specifier (type, access, virtual_p);
19292 /* Exception handling [gram.exception] */
19294 /* Parse an (optional) exception-specification.
19296 exception-specification:
19297 throw ( type-id-list [opt] )
19299 Returns a TREE_LIST representing the exception-specification. The
19300 TREE_VALUE of each node is a type. */
19303 cp_parser_exception_specification_opt (cp_parser* parser)
19307 const char *saved_message;
19309 /* Peek at the next token. */
19310 token = cp_lexer_peek_token (parser->lexer);
19312 /* Is it a noexcept-specification? */
19313 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19316 cp_lexer_consume_token (parser->lexer);
19318 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19320 cp_lexer_consume_token (parser->lexer);
19322 /* Types may not be defined in an exception-specification. */
19323 saved_message = parser->type_definition_forbidden_message;
19324 parser->type_definition_forbidden_message
19325 = G_("types may not be defined in an exception-specification");
19327 expr = cp_parser_constant_expression (parser, false, NULL);
19329 /* Restore the saved message. */
19330 parser->type_definition_forbidden_message = saved_message;
19332 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19335 expr = boolean_true_node;
19337 return build_noexcept_spec (expr, tf_warning_or_error);
19340 /* If it's not `throw', then there's no exception-specification. */
19341 if (!cp_parser_is_keyword (token, RID_THROW))
19345 /* Enable this once a lot of code has transitioned to noexcept? */
19346 if (cxx_dialect == cxx0x && !in_system_header)
19347 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19348 "deprecated in C++0x; use %<noexcept%> instead");
19351 /* Consume the `throw'. */
19352 cp_lexer_consume_token (parser->lexer);
19354 /* Look for the `('. */
19355 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19357 /* Peek at the next token. */
19358 token = cp_lexer_peek_token (parser->lexer);
19359 /* If it's not a `)', then there is a type-id-list. */
19360 if (token->type != CPP_CLOSE_PAREN)
19362 /* Types may not be defined in an exception-specification. */
19363 saved_message = parser->type_definition_forbidden_message;
19364 parser->type_definition_forbidden_message
19365 = G_("types may not be defined in an exception-specification");
19366 /* Parse the type-id-list. */
19367 type_id_list = cp_parser_type_id_list (parser);
19368 /* Restore the saved message. */
19369 parser->type_definition_forbidden_message = saved_message;
19372 type_id_list = empty_except_spec;
19374 /* Look for the `)'. */
19375 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19377 return type_id_list;
19380 /* Parse an (optional) type-id-list.
19384 type-id-list , type-id ... [opt]
19386 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19387 in the order that the types were presented. */
19390 cp_parser_type_id_list (cp_parser* parser)
19392 tree types = NULL_TREE;
19399 /* Get the next type-id. */
19400 type = cp_parser_type_id (parser);
19401 /* Parse the optional ellipsis. */
19402 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19404 /* Consume the `...'. */
19405 cp_lexer_consume_token (parser->lexer);
19407 /* Turn the type into a pack expansion expression. */
19408 type = make_pack_expansion (type);
19410 /* Add it to the list. */
19411 types = add_exception_specifier (types, type, /*complain=*/1);
19412 /* Peek at the next token. */
19413 token = cp_lexer_peek_token (parser->lexer);
19414 /* If it is not a `,', we are done. */
19415 if (token->type != CPP_COMMA)
19417 /* Consume the `,'. */
19418 cp_lexer_consume_token (parser->lexer);
19421 return nreverse (types);
19424 /* Parse a try-block.
19427 try compound-statement handler-seq */
19430 cp_parser_try_block (cp_parser* parser)
19434 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19435 try_block = begin_try_block ();
19436 cp_parser_compound_statement (parser, NULL, true, false);
19437 finish_try_block (try_block);
19438 cp_parser_handler_seq (parser);
19439 finish_handler_sequence (try_block);
19444 /* Parse a function-try-block.
19446 function-try-block:
19447 try ctor-initializer [opt] function-body handler-seq */
19450 cp_parser_function_try_block (cp_parser* parser)
19452 tree compound_stmt;
19454 bool ctor_initializer_p;
19456 /* Look for the `try' keyword. */
19457 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19459 /* Let the rest of the front end know where we are. */
19460 try_block = begin_function_try_block (&compound_stmt);
19461 /* Parse the function-body. */
19463 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19464 /* We're done with the `try' part. */
19465 finish_function_try_block (try_block);
19466 /* Parse the handlers. */
19467 cp_parser_handler_seq (parser);
19468 /* We're done with the handlers. */
19469 finish_function_handler_sequence (try_block, compound_stmt);
19471 return ctor_initializer_p;
19474 /* Parse a handler-seq.
19477 handler handler-seq [opt] */
19480 cp_parser_handler_seq (cp_parser* parser)
19486 /* Parse the handler. */
19487 cp_parser_handler (parser);
19488 /* Peek at the next token. */
19489 token = cp_lexer_peek_token (parser->lexer);
19490 /* If it's not `catch' then there are no more handlers. */
19491 if (!cp_parser_is_keyword (token, RID_CATCH))
19496 /* Parse a handler.
19499 catch ( exception-declaration ) compound-statement */
19502 cp_parser_handler (cp_parser* parser)
19507 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19508 handler = begin_handler ();
19509 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19510 declaration = cp_parser_exception_declaration (parser);
19511 finish_handler_parms (declaration, handler);
19512 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19513 cp_parser_compound_statement (parser, NULL, false, false);
19514 finish_handler (handler);
19517 /* Parse an exception-declaration.
19519 exception-declaration:
19520 type-specifier-seq declarator
19521 type-specifier-seq abstract-declarator
19525 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19526 ellipsis variant is used. */
19529 cp_parser_exception_declaration (cp_parser* parser)
19531 cp_decl_specifier_seq type_specifiers;
19532 cp_declarator *declarator;
19533 const char *saved_message;
19535 /* If it's an ellipsis, it's easy to handle. */
19536 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19538 /* Consume the `...' token. */
19539 cp_lexer_consume_token (parser->lexer);
19543 /* Types may not be defined in exception-declarations. */
19544 saved_message = parser->type_definition_forbidden_message;
19545 parser->type_definition_forbidden_message
19546 = G_("types may not be defined in exception-declarations");
19548 /* Parse the type-specifier-seq. */
19549 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19550 /*is_trailing_return=*/false,
19552 /* If it's a `)', then there is no declarator. */
19553 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19556 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19557 /*ctor_dtor_or_conv_p=*/NULL,
19558 /*parenthesized_p=*/NULL,
19559 /*member_p=*/false);
19561 /* Restore the saved message. */
19562 parser->type_definition_forbidden_message = saved_message;
19564 if (!type_specifiers.any_specifiers_p)
19565 return error_mark_node;
19567 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19570 /* Parse a throw-expression.
19573 throw assignment-expression [opt]
19575 Returns a THROW_EXPR representing the throw-expression. */
19578 cp_parser_throw_expression (cp_parser* parser)
19583 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19584 token = cp_lexer_peek_token (parser->lexer);
19585 /* Figure out whether or not there is an assignment-expression
19586 following the "throw" keyword. */
19587 if (token->type == CPP_COMMA
19588 || token->type == CPP_SEMICOLON
19589 || token->type == CPP_CLOSE_PAREN
19590 || token->type == CPP_CLOSE_SQUARE
19591 || token->type == CPP_CLOSE_BRACE
19592 || token->type == CPP_COLON)
19593 expression = NULL_TREE;
19595 expression = cp_parser_assignment_expression (parser,
19596 /*cast_p=*/false, NULL);
19598 return build_throw (expression);
19601 /* GNU Extensions */
19603 /* Parse an (optional) asm-specification.
19606 asm ( string-literal )
19608 If the asm-specification is present, returns a STRING_CST
19609 corresponding to the string-literal. Otherwise, returns
19613 cp_parser_asm_specification_opt (cp_parser* parser)
19616 tree asm_specification;
19618 /* Peek at the next token. */
19619 token = cp_lexer_peek_token (parser->lexer);
19620 /* If the next token isn't the `asm' keyword, then there's no
19621 asm-specification. */
19622 if (!cp_parser_is_keyword (token, RID_ASM))
19625 /* Consume the `asm' token. */
19626 cp_lexer_consume_token (parser->lexer);
19627 /* Look for the `('. */
19628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19630 /* Look for the string-literal. */
19631 asm_specification = cp_parser_string_literal (parser, false, false);
19633 /* Look for the `)'. */
19634 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19636 return asm_specification;
19639 /* Parse an asm-operand-list.
19643 asm-operand-list , asm-operand
19646 string-literal ( expression )
19647 [ string-literal ] string-literal ( expression )
19649 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19650 each node is the expression. The TREE_PURPOSE is itself a
19651 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19652 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19653 is a STRING_CST for the string literal before the parenthesis. Returns
19654 ERROR_MARK_NODE if any of the operands are invalid. */
19657 cp_parser_asm_operand_list (cp_parser* parser)
19659 tree asm_operands = NULL_TREE;
19660 bool invalid_operands = false;
19664 tree string_literal;
19668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19670 /* Consume the `[' token. */
19671 cp_lexer_consume_token (parser->lexer);
19672 /* Read the operand name. */
19673 name = cp_parser_identifier (parser);
19674 if (name != error_mark_node)
19675 name = build_string (IDENTIFIER_LENGTH (name),
19676 IDENTIFIER_POINTER (name));
19677 /* Look for the closing `]'. */
19678 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19682 /* Look for the string-literal. */
19683 string_literal = cp_parser_string_literal (parser, false, false);
19685 /* Look for the `('. */
19686 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19687 /* Parse the expression. */
19688 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19689 /* Look for the `)'. */
19690 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19692 if (name == error_mark_node
19693 || string_literal == error_mark_node
19694 || expression == error_mark_node)
19695 invalid_operands = true;
19697 /* Add this operand to the list. */
19698 asm_operands = tree_cons (build_tree_list (name, string_literal),
19701 /* If the next token is not a `,', there are no more
19703 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19705 /* Consume the `,'. */
19706 cp_lexer_consume_token (parser->lexer);
19709 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19712 /* Parse an asm-clobber-list.
19716 asm-clobber-list , string-literal
19718 Returns a TREE_LIST, indicating the clobbers in the order that they
19719 appeared. The TREE_VALUE of each node is a STRING_CST. */
19722 cp_parser_asm_clobber_list (cp_parser* parser)
19724 tree clobbers = NULL_TREE;
19728 tree string_literal;
19730 /* Look for the string literal. */
19731 string_literal = cp_parser_string_literal (parser, false, false);
19732 /* Add it to the list. */
19733 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19734 /* If the next token is not a `,', then the list is
19736 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19738 /* Consume the `,' token. */
19739 cp_lexer_consume_token (parser->lexer);
19745 /* Parse an asm-label-list.
19749 asm-label-list , identifier
19751 Returns a TREE_LIST, indicating the labels in the order that they
19752 appeared. The TREE_VALUE of each node is a label. */
19755 cp_parser_asm_label_list (cp_parser* parser)
19757 tree labels = NULL_TREE;
19761 tree identifier, label, name;
19763 /* Look for the identifier. */
19764 identifier = cp_parser_identifier (parser);
19765 if (!error_operand_p (identifier))
19767 label = lookup_label (identifier);
19768 if (TREE_CODE (label) == LABEL_DECL)
19770 TREE_USED (label) = 1;
19771 check_goto (label);
19772 name = build_string (IDENTIFIER_LENGTH (identifier),
19773 IDENTIFIER_POINTER (identifier));
19774 labels = tree_cons (name, label, labels);
19777 /* If the next token is not a `,', then the list is
19779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19781 /* Consume the `,' token. */
19782 cp_lexer_consume_token (parser->lexer);
19785 return nreverse (labels);
19788 /* Parse an (optional) series of attributes.
19791 attributes attribute
19794 __attribute__ (( attribute-list [opt] ))
19796 The return value is as for cp_parser_attribute_list. */
19799 cp_parser_attributes_opt (cp_parser* parser)
19801 tree attributes = NULL_TREE;
19806 tree attribute_list;
19808 /* Peek at the next token. */
19809 token = cp_lexer_peek_token (parser->lexer);
19810 /* If it's not `__attribute__', then we're done. */
19811 if (token->keyword != RID_ATTRIBUTE)
19814 /* Consume the `__attribute__' keyword. */
19815 cp_lexer_consume_token (parser->lexer);
19816 /* Look for the two `(' tokens. */
19817 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19818 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19820 /* Peek at the next token. */
19821 token = cp_lexer_peek_token (parser->lexer);
19822 if (token->type != CPP_CLOSE_PAREN)
19823 /* Parse the attribute-list. */
19824 attribute_list = cp_parser_attribute_list (parser);
19826 /* If the next token is a `)', then there is no attribute
19828 attribute_list = NULL;
19830 /* Look for the two `)' tokens. */
19831 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19832 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19834 /* Add these new attributes to the list. */
19835 attributes = chainon (attributes, attribute_list);
19841 /* Parse an attribute-list.
19845 attribute-list , attribute
19849 identifier ( identifier )
19850 identifier ( identifier , expression-list )
19851 identifier ( expression-list )
19853 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
19854 to an attribute. The TREE_PURPOSE of each node is the identifier
19855 indicating which attribute is in use. The TREE_VALUE represents
19856 the arguments, if any. */
19859 cp_parser_attribute_list (cp_parser* parser)
19861 tree attribute_list = NULL_TREE;
19862 bool save_translate_strings_p = parser->translate_strings_p;
19864 parser->translate_strings_p = false;
19871 /* Look for the identifier. We also allow keywords here; for
19872 example `__attribute__ ((const))' is legal. */
19873 token = cp_lexer_peek_token (parser->lexer);
19874 if (token->type == CPP_NAME
19875 || token->type == CPP_KEYWORD)
19877 tree arguments = NULL_TREE;
19879 /* Consume the token. */
19880 token = cp_lexer_consume_token (parser->lexer);
19882 /* Save away the identifier that indicates which attribute
19884 identifier = (token->type == CPP_KEYWORD)
19885 /* For keywords, use the canonical spelling, not the
19886 parsed identifier. */
19887 ? ridpointers[(int) token->keyword]
19890 attribute = build_tree_list (identifier, NULL_TREE);
19892 /* Peek at the next token. */
19893 token = cp_lexer_peek_token (parser->lexer);
19894 /* If it's an `(', then parse the attribute arguments. */
19895 if (token->type == CPP_OPEN_PAREN)
19898 int attr_flag = (attribute_takes_identifier_p (identifier)
19899 ? id_attr : normal_attr);
19900 vec = cp_parser_parenthesized_expression_list
19901 (parser, attr_flag, /*cast_p=*/false,
19902 /*allow_expansion_p=*/false,
19903 /*non_constant_p=*/NULL);
19905 arguments = error_mark_node;
19908 arguments = build_tree_list_vec (vec);
19909 release_tree_vector (vec);
19911 /* Save the arguments away. */
19912 TREE_VALUE (attribute) = arguments;
19915 if (arguments != error_mark_node)
19917 /* Add this attribute to the list. */
19918 TREE_CHAIN (attribute) = attribute_list;
19919 attribute_list = attribute;
19922 token = cp_lexer_peek_token (parser->lexer);
19924 /* Now, look for more attributes. If the next token isn't a
19925 `,', we're done. */
19926 if (token->type != CPP_COMMA)
19929 /* Consume the comma and keep going. */
19930 cp_lexer_consume_token (parser->lexer);
19932 parser->translate_strings_p = save_translate_strings_p;
19934 /* We built up the list in reverse order. */
19935 return nreverse (attribute_list);
19938 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
19939 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
19940 current value of the PEDANTIC flag, regardless of whether or not
19941 the `__extension__' keyword is present. The caller is responsible
19942 for restoring the value of the PEDANTIC flag. */
19945 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19947 /* Save the old value of the PEDANTIC flag. */
19948 *saved_pedantic = pedantic;
19950 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19952 /* Consume the `__extension__' token. */
19953 cp_lexer_consume_token (parser->lexer);
19954 /* We're not being pedantic while the `__extension__' keyword is
19964 /* Parse a label declaration.
19967 __label__ label-declarator-seq ;
19969 label-declarator-seq:
19970 identifier , label-declarator-seq
19974 cp_parser_label_declaration (cp_parser* parser)
19976 /* Look for the `__label__' keyword. */
19977 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19983 /* Look for an identifier. */
19984 identifier = cp_parser_identifier (parser);
19985 /* If we failed, stop. */
19986 if (identifier == error_mark_node)
19988 /* Declare it as a label. */
19989 finish_label_decl (identifier);
19990 /* If the next token is a `;', stop. */
19991 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19993 /* Look for the `,' separating the label declarations. */
19994 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19997 /* Look for the final `;'. */
19998 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20001 /* Support Functions */
20003 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20004 NAME should have one of the representations used for an
20005 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20006 is returned. If PARSER->SCOPE is a dependent type, then a
20007 SCOPE_REF is returned.
20009 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20010 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20011 was formed. Abstractly, such entities should not be passed to this
20012 function, because they do not need to be looked up, but it is
20013 simpler to check for this special case here, rather than at the
20016 In cases not explicitly covered above, this function returns a
20017 DECL, OVERLOAD, or baselink representing the result of the lookup.
20018 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20021 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20022 (e.g., "struct") that was used. In that case bindings that do not
20023 refer to types are ignored.
20025 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20028 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20031 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20034 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20035 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20036 NULL_TREE otherwise. */
20039 cp_parser_lookup_name (cp_parser *parser, tree name,
20040 enum tag_types tag_type,
20043 bool check_dependency,
20044 tree *ambiguous_decls,
20045 location_t name_location)
20049 tree object_type = parser->context->object_type;
20051 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20052 flags |= LOOKUP_COMPLAIN;
20054 /* Assume that the lookup will be unambiguous. */
20055 if (ambiguous_decls)
20056 *ambiguous_decls = NULL_TREE;
20058 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20059 no longer valid. Note that if we are parsing tentatively, and
20060 the parse fails, OBJECT_TYPE will be automatically restored. */
20061 parser->context->object_type = NULL_TREE;
20063 if (name == error_mark_node)
20064 return error_mark_node;
20066 /* A template-id has already been resolved; there is no lookup to
20068 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20070 if (BASELINK_P (name))
20072 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20073 == TEMPLATE_ID_EXPR);
20077 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20078 it should already have been checked to make sure that the name
20079 used matches the type being destroyed. */
20080 if (TREE_CODE (name) == BIT_NOT_EXPR)
20084 /* Figure out to which type this destructor applies. */
20086 type = parser->scope;
20087 else if (object_type)
20088 type = object_type;
20090 type = current_class_type;
20091 /* If that's not a class type, there is no destructor. */
20092 if (!type || !CLASS_TYPE_P (type))
20093 return error_mark_node;
20094 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20095 lazily_declare_fn (sfk_destructor, type);
20096 if (!CLASSTYPE_DESTRUCTORS (type))
20097 return error_mark_node;
20098 /* If it was a class type, return the destructor. */
20099 return CLASSTYPE_DESTRUCTORS (type);
20102 /* By this point, the NAME should be an ordinary identifier. If
20103 the id-expression was a qualified name, the qualifying scope is
20104 stored in PARSER->SCOPE at this point. */
20105 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20107 /* Perform the lookup. */
20112 if (parser->scope == error_mark_node)
20113 return error_mark_node;
20115 /* If the SCOPE is dependent, the lookup must be deferred until
20116 the template is instantiated -- unless we are explicitly
20117 looking up names in uninstantiated templates. Even then, we
20118 cannot look up the name if the scope is not a class type; it
20119 might, for example, be a template type parameter. */
20120 dependent_p = (TYPE_P (parser->scope)
20121 && dependent_scope_p (parser->scope));
20122 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20124 /* Defer lookup. */
20125 decl = error_mark_node;
20128 tree pushed_scope = NULL_TREE;
20130 /* If PARSER->SCOPE is a dependent type, then it must be a
20131 class type, and we must not be checking dependencies;
20132 otherwise, we would have processed this lookup above. So
20133 that PARSER->SCOPE is not considered a dependent base by
20134 lookup_member, we must enter the scope here. */
20136 pushed_scope = push_scope (parser->scope);
20138 /* If the PARSER->SCOPE is a template specialization, it
20139 may be instantiated during name lookup. In that case,
20140 errors may be issued. Even if we rollback the current
20141 tentative parse, those errors are valid. */
20142 decl = lookup_qualified_name (parser->scope, name,
20143 tag_type != none_type,
20144 /*complain=*/true);
20146 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20147 lookup result and the nested-name-specifier nominates a class C:
20148 * if the name specified after the nested-name-specifier, when
20149 looked up in C, is the injected-class-name of C (Clause 9), or
20150 * if the name specified after the nested-name-specifier is the
20151 same as the identifier or the simple-template-id's template-
20152 name in the last component of the nested-name-specifier,
20153 the name is instead considered to name the constructor of
20154 class C. [ Note: for example, the constructor is not an
20155 acceptable lookup result in an elaborated-type-specifier so
20156 the constructor would not be used in place of the
20157 injected-class-name. --end note ] Such a constructor name
20158 shall be used only in the declarator-id of a declaration that
20159 names a constructor or in a using-declaration. */
20160 if (tag_type == none_type
20161 && DECL_SELF_REFERENCE_P (decl)
20162 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20163 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20164 tag_type != none_type,
20165 /*complain=*/true);
20167 /* If we have a single function from a using decl, pull it out. */
20168 if (TREE_CODE (decl) == OVERLOAD
20169 && !really_overloaded_fn (decl))
20170 decl = OVL_FUNCTION (decl);
20173 pop_scope (pushed_scope);
20176 /* If the scope is a dependent type and either we deferred lookup or
20177 we did lookup but didn't find the name, rememeber the name. */
20178 if (decl == error_mark_node && TYPE_P (parser->scope)
20179 && dependent_type_p (parser->scope))
20185 /* The resolution to Core Issue 180 says that `struct
20186 A::B' should be considered a type-name, even if `A'
20188 type = make_typename_type (parser->scope, name, tag_type,
20189 /*complain=*/tf_error);
20190 decl = TYPE_NAME (type);
20192 else if (is_template
20193 && (cp_parser_next_token_ends_template_argument_p (parser)
20194 || cp_lexer_next_token_is (parser->lexer,
20196 decl = make_unbound_class_template (parser->scope,
20198 /*complain=*/tf_error);
20200 decl = build_qualified_name (/*type=*/NULL_TREE,
20201 parser->scope, name,
20204 parser->qualifying_scope = parser->scope;
20205 parser->object_scope = NULL_TREE;
20207 else if (object_type)
20209 tree object_decl = NULL_TREE;
20210 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20211 OBJECT_TYPE is not a class. */
20212 if (CLASS_TYPE_P (object_type))
20213 /* If the OBJECT_TYPE is a template specialization, it may
20214 be instantiated during name lookup. In that case, errors
20215 may be issued. Even if we rollback the current tentative
20216 parse, those errors are valid. */
20217 object_decl = lookup_member (object_type,
20220 tag_type != none_type);
20221 /* Look it up in the enclosing context, too. */
20222 decl = lookup_name_real (name, tag_type != none_type,
20224 /*block_p=*/true, is_namespace, flags);
20225 parser->object_scope = object_type;
20226 parser->qualifying_scope = NULL_TREE;
20228 decl = object_decl;
20232 decl = lookup_name_real (name, tag_type != none_type,
20234 /*block_p=*/true, is_namespace, flags);
20235 parser->qualifying_scope = NULL_TREE;
20236 parser->object_scope = NULL_TREE;
20239 /* If the lookup failed, let our caller know. */
20240 if (!decl || decl == error_mark_node)
20241 return error_mark_node;
20243 /* Pull out the template from an injected-class-name (or multiple). */
20245 decl = maybe_get_template_decl_from_type_decl (decl);
20247 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20248 if (TREE_CODE (decl) == TREE_LIST)
20250 if (ambiguous_decls)
20251 *ambiguous_decls = decl;
20252 /* The error message we have to print is too complicated for
20253 cp_parser_error, so we incorporate its actions directly. */
20254 if (!cp_parser_simulate_error (parser))
20256 error_at (name_location, "reference to %qD is ambiguous",
20258 print_candidates (decl);
20260 return error_mark_node;
20263 gcc_assert (DECL_P (decl)
20264 || TREE_CODE (decl) == OVERLOAD
20265 || TREE_CODE (decl) == SCOPE_REF
20266 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20267 || BASELINK_P (decl));
20269 /* If we have resolved the name of a member declaration, check to
20270 see if the declaration is accessible. When the name resolves to
20271 set of overloaded functions, accessibility is checked when
20272 overload resolution is done.
20274 During an explicit instantiation, access is not checked at all,
20275 as per [temp.explicit]. */
20277 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20279 maybe_record_typedef_use (decl);
20284 /* Like cp_parser_lookup_name, but for use in the typical case where
20285 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20286 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20289 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20291 return cp_parser_lookup_name (parser, name,
20293 /*is_template=*/false,
20294 /*is_namespace=*/false,
20295 /*check_dependency=*/true,
20296 /*ambiguous_decls=*/NULL,
20300 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20301 the current context, return the TYPE_DECL. If TAG_NAME_P is
20302 true, the DECL indicates the class being defined in a class-head,
20303 or declared in an elaborated-type-specifier.
20305 Otherwise, return DECL. */
20308 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20310 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20311 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20314 template <typename T> struct B;
20317 template <typename T> struct A::B {};
20319 Similarly, in an elaborated-type-specifier:
20321 namespace N { struct X{}; }
20324 template <typename T> friend struct N::X;
20327 However, if the DECL refers to a class type, and we are in
20328 the scope of the class, then the name lookup automatically
20329 finds the TYPE_DECL created by build_self_reference rather
20330 than a TEMPLATE_DECL. For example, in:
20332 template <class T> struct S {
20336 there is no need to handle such case. */
20338 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20339 return DECL_TEMPLATE_RESULT (decl);
20344 /* If too many, or too few, template-parameter lists apply to the
20345 declarator, issue an error message. Returns TRUE if all went well,
20346 and FALSE otherwise. */
20349 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20350 cp_declarator *declarator,
20351 location_t declarator_location)
20353 unsigned num_templates;
20355 /* We haven't seen any classes that involve template parameters yet. */
20358 switch (declarator->kind)
20361 if (declarator->u.id.qualifying_scope)
20365 scope = declarator->u.id.qualifying_scope;
20367 while (scope && CLASS_TYPE_P (scope))
20369 /* You're supposed to have one `template <...>'
20370 for every template class, but you don't need one
20371 for a full specialization. For example:
20373 template <class T> struct S{};
20374 template <> struct S<int> { void f(); };
20375 void S<int>::f () {}
20377 is correct; there shouldn't be a `template <>' for
20378 the definition of `S<int>::f'. */
20379 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20380 /* If SCOPE does not have template information of any
20381 kind, then it is not a template, nor is it nested
20382 within a template. */
20384 if (explicit_class_specialization_p (scope))
20386 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20389 scope = TYPE_CONTEXT (scope);
20392 else if (TREE_CODE (declarator->u.id.unqualified_name)
20393 == TEMPLATE_ID_EXPR)
20394 /* If the DECLARATOR has the form `X<y>' then it uses one
20395 additional level of template parameters. */
20398 return cp_parser_check_template_parameters
20399 (parser, num_templates, declarator_location, declarator);
20405 case cdk_reference:
20407 return (cp_parser_check_declarator_template_parameters
20408 (parser, declarator->declarator, declarator_location));
20414 gcc_unreachable ();
20419 /* NUM_TEMPLATES were used in the current declaration. If that is
20420 invalid, return FALSE and issue an error messages. Otherwise,
20421 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20422 declarator and we can print more accurate diagnostics. */
20425 cp_parser_check_template_parameters (cp_parser* parser,
20426 unsigned num_templates,
20427 location_t location,
20428 cp_declarator *declarator)
20430 /* If there are the same number of template classes and parameter
20431 lists, that's OK. */
20432 if (parser->num_template_parameter_lists == num_templates)
20434 /* If there are more, but only one more, then we are referring to a
20435 member template. That's OK too. */
20436 if (parser->num_template_parameter_lists == num_templates + 1)
20438 /* If there are more template classes than parameter lists, we have
20441 template <class T> void S<T>::R<T>::f (); */
20442 if (parser->num_template_parameter_lists < num_templates)
20444 if (declarator && !current_function_decl)
20445 error_at (location, "specializing member %<%T::%E%> "
20446 "requires %<template<>%> syntax",
20447 declarator->u.id.qualifying_scope,
20448 declarator->u.id.unqualified_name);
20449 else if (declarator)
20450 error_at (location, "invalid declaration of %<%T::%E%>",
20451 declarator->u.id.qualifying_scope,
20452 declarator->u.id.unqualified_name);
20454 error_at (location, "too few template-parameter-lists");
20457 /* Otherwise, there are too many template parameter lists. We have
20460 template <class T> template <class U> void S::f(); */
20461 error_at (location, "too many template-parameter-lists");
20465 /* Parse an optional `::' token indicating that the following name is
20466 from the global namespace. If so, PARSER->SCOPE is set to the
20467 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20468 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20469 Returns the new value of PARSER->SCOPE, if the `::' token is
20470 present, and NULL_TREE otherwise. */
20473 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20477 /* Peek at the next token. */
20478 token = cp_lexer_peek_token (parser->lexer);
20479 /* If we're looking at a `::' token then we're starting from the
20480 global namespace, not our current location. */
20481 if (token->type == CPP_SCOPE)
20483 /* Consume the `::' token. */
20484 cp_lexer_consume_token (parser->lexer);
20485 /* Set the SCOPE so that we know where to start the lookup. */
20486 parser->scope = global_namespace;
20487 parser->qualifying_scope = global_namespace;
20488 parser->object_scope = NULL_TREE;
20490 return parser->scope;
20492 else if (!current_scope_valid_p)
20494 parser->scope = NULL_TREE;
20495 parser->qualifying_scope = NULL_TREE;
20496 parser->object_scope = NULL_TREE;
20502 /* Returns TRUE if the upcoming token sequence is the start of a
20503 constructor declarator. If FRIEND_P is true, the declarator is
20504 preceded by the `friend' specifier. */
20507 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20509 bool constructor_p;
20510 tree nested_name_specifier;
20511 cp_token *next_token;
20513 /* The common case is that this is not a constructor declarator, so
20514 try to avoid doing lots of work if at all possible. It's not
20515 valid declare a constructor at function scope. */
20516 if (parser->in_function_body)
20518 /* And only certain tokens can begin a constructor declarator. */
20519 next_token = cp_lexer_peek_token (parser->lexer);
20520 if (next_token->type != CPP_NAME
20521 && next_token->type != CPP_SCOPE
20522 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20523 && next_token->type != CPP_TEMPLATE_ID)
20526 /* Parse tentatively; we are going to roll back all of the tokens
20528 cp_parser_parse_tentatively (parser);
20529 /* Assume that we are looking at a constructor declarator. */
20530 constructor_p = true;
20532 /* Look for the optional `::' operator. */
20533 cp_parser_global_scope_opt (parser,
20534 /*current_scope_valid_p=*/false);
20535 /* Look for the nested-name-specifier. */
20536 nested_name_specifier
20537 = (cp_parser_nested_name_specifier_opt (parser,
20538 /*typename_keyword_p=*/false,
20539 /*check_dependency_p=*/false,
20541 /*is_declaration=*/false));
20542 /* Outside of a class-specifier, there must be a
20543 nested-name-specifier. */
20544 if (!nested_name_specifier &&
20545 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20547 constructor_p = false;
20548 else if (nested_name_specifier == error_mark_node)
20549 constructor_p = false;
20551 /* If we have a class scope, this is easy; DR 147 says that S::S always
20552 names the constructor, and no other qualified name could. */
20553 if (constructor_p && nested_name_specifier
20554 && CLASS_TYPE_P (nested_name_specifier))
20556 tree id = cp_parser_unqualified_id (parser,
20557 /*template_keyword_p=*/false,
20558 /*check_dependency_p=*/false,
20559 /*declarator_p=*/true,
20560 /*optional_p=*/false);
20561 if (is_overloaded_fn (id))
20562 id = DECL_NAME (get_first_fn (id));
20563 if (!constructor_name_p (id, nested_name_specifier))
20564 constructor_p = false;
20566 /* If we still think that this might be a constructor-declarator,
20567 look for a class-name. */
20568 else if (constructor_p)
20572 template <typename T> struct S {
20576 we must recognize that the nested `S' names a class. */
20578 type_decl = cp_parser_class_name (parser,
20579 /*typename_keyword_p=*/false,
20580 /*template_keyword_p=*/false,
20582 /*check_dependency_p=*/false,
20583 /*class_head_p=*/false,
20584 /*is_declaration=*/false);
20585 /* If there was no class-name, then this is not a constructor. */
20586 constructor_p = !cp_parser_error_occurred (parser);
20588 /* If we're still considering a constructor, we have to see a `(',
20589 to begin the parameter-declaration-clause, followed by either a
20590 `)', an `...', or a decl-specifier. We need to check for a
20591 type-specifier to avoid being fooled into thinking that:
20595 is a constructor. (It is actually a function named `f' that
20596 takes one parameter (of type `int') and returns a value of type
20599 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20600 constructor_p = false;
20603 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20604 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20605 /* A parameter declaration begins with a decl-specifier,
20606 which is either the "attribute" keyword, a storage class
20607 specifier, or (usually) a type-specifier. */
20608 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20611 tree pushed_scope = NULL_TREE;
20612 unsigned saved_num_template_parameter_lists;
20614 /* Names appearing in the type-specifier should be looked up
20615 in the scope of the class. */
20616 if (current_class_type)
20620 type = TREE_TYPE (type_decl);
20621 if (TREE_CODE (type) == TYPENAME_TYPE)
20623 type = resolve_typename_type (type,
20624 /*only_current_p=*/false);
20625 if (TREE_CODE (type) == TYPENAME_TYPE)
20627 cp_parser_abort_tentative_parse (parser);
20631 pushed_scope = push_scope (type);
20634 /* Inside the constructor parameter list, surrounding
20635 template-parameter-lists do not apply. */
20636 saved_num_template_parameter_lists
20637 = parser->num_template_parameter_lists;
20638 parser->num_template_parameter_lists = 0;
20640 /* Look for the type-specifier. */
20641 cp_parser_type_specifier (parser,
20642 CP_PARSER_FLAGS_NONE,
20643 /*decl_specs=*/NULL,
20644 /*is_declarator=*/true,
20645 /*declares_class_or_enum=*/NULL,
20646 /*is_cv_qualifier=*/NULL);
20648 parser->num_template_parameter_lists
20649 = saved_num_template_parameter_lists;
20651 /* Leave the scope of the class. */
20653 pop_scope (pushed_scope);
20655 constructor_p = !cp_parser_error_occurred (parser);
20659 /* We did not really want to consume any tokens. */
20660 cp_parser_abort_tentative_parse (parser);
20662 return constructor_p;
20665 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20666 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20667 they must be performed once we are in the scope of the function.
20669 Returns the function defined. */
20672 cp_parser_function_definition_from_specifiers_and_declarator
20673 (cp_parser* parser,
20674 cp_decl_specifier_seq *decl_specifiers,
20676 const cp_declarator *declarator)
20681 /* Begin the function-definition. */
20682 success_p = start_function (decl_specifiers, declarator, attributes);
20684 /* The things we're about to see are not directly qualified by any
20685 template headers we've seen thus far. */
20686 reset_specialization ();
20688 /* If there were names looked up in the decl-specifier-seq that we
20689 did not check, check them now. We must wait until we are in the
20690 scope of the function to perform the checks, since the function
20691 might be a friend. */
20692 perform_deferred_access_checks ();
20696 /* Skip the entire function. */
20697 cp_parser_skip_to_end_of_block_or_statement (parser);
20698 fn = error_mark_node;
20700 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20702 /* Seen already, skip it. An error message has already been output. */
20703 cp_parser_skip_to_end_of_block_or_statement (parser);
20704 fn = current_function_decl;
20705 current_function_decl = NULL_TREE;
20706 /* If this is a function from a class, pop the nested class. */
20707 if (current_class_name)
20708 pop_nested_class ();
20713 if (DECL_DECLARED_INLINE_P (current_function_decl))
20714 tv = TV_PARSE_INLINE;
20716 tv = TV_PARSE_FUNC;
20718 fn = cp_parser_function_definition_after_declarator (parser,
20719 /*inline_p=*/false);
20726 /* Parse the part of a function-definition that follows the
20727 declarator. INLINE_P is TRUE iff this function is an inline
20728 function defined within a class-specifier.
20730 Returns the function defined. */
20733 cp_parser_function_definition_after_declarator (cp_parser* parser,
20737 bool ctor_initializer_p = false;
20738 bool saved_in_unbraced_linkage_specification_p;
20739 bool saved_in_function_body;
20740 unsigned saved_num_template_parameter_lists;
20743 saved_in_function_body = parser->in_function_body;
20744 parser->in_function_body = true;
20745 /* If the next token is `return', then the code may be trying to
20746 make use of the "named return value" extension that G++ used to
20748 token = cp_lexer_peek_token (parser->lexer);
20749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20751 /* Consume the `return' keyword. */
20752 cp_lexer_consume_token (parser->lexer);
20753 /* Look for the identifier that indicates what value is to be
20755 cp_parser_identifier (parser);
20756 /* Issue an error message. */
20757 error_at (token->location,
20758 "named return values are no longer supported");
20759 /* Skip tokens until we reach the start of the function body. */
20762 cp_token *token = cp_lexer_peek_token (parser->lexer);
20763 if (token->type == CPP_OPEN_BRACE
20764 || token->type == CPP_EOF
20765 || token->type == CPP_PRAGMA_EOL)
20767 cp_lexer_consume_token (parser->lexer);
20770 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20771 anything declared inside `f'. */
20772 saved_in_unbraced_linkage_specification_p
20773 = parser->in_unbraced_linkage_specification_p;
20774 parser->in_unbraced_linkage_specification_p = false;
20775 /* Inside the function, surrounding template-parameter-lists do not
20777 saved_num_template_parameter_lists
20778 = parser->num_template_parameter_lists;
20779 parser->num_template_parameter_lists = 0;
20781 start_lambda_scope (current_function_decl);
20783 /* If the next token is `try', then we are looking at a
20784 function-try-block. */
20785 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20786 ctor_initializer_p = cp_parser_function_try_block (parser);
20787 /* A function-try-block includes the function-body, so we only do
20788 this next part if we're not processing a function-try-block. */
20791 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20793 finish_lambda_scope ();
20795 /* Finish the function. */
20796 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20797 (inline_p ? 2 : 0));
20798 /* Generate code for it, if necessary. */
20799 expand_or_defer_fn (fn);
20800 /* Restore the saved values. */
20801 parser->in_unbraced_linkage_specification_p
20802 = saved_in_unbraced_linkage_specification_p;
20803 parser->num_template_parameter_lists
20804 = saved_num_template_parameter_lists;
20805 parser->in_function_body = saved_in_function_body;
20810 /* Parse a template-declaration, assuming that the `export' (and
20811 `extern') keywords, if present, has already been scanned. MEMBER_P
20812 is as for cp_parser_template_declaration. */
20815 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20817 tree decl = NULL_TREE;
20818 VEC (deferred_access_check,gc) *checks;
20819 tree parameter_list;
20820 bool friend_p = false;
20821 bool need_lang_pop;
20824 /* Look for the `template' keyword. */
20825 token = cp_lexer_peek_token (parser->lexer);
20826 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20830 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20832 if (at_class_scope_p () && current_function_decl)
20834 /* 14.5.2.2 [temp.mem]
20836 A local class shall not have member templates. */
20837 error_at (token->location,
20838 "invalid declaration of member template in local class");
20839 cp_parser_skip_to_end_of_block_or_statement (parser);
20844 A template ... shall not have C linkage. */
20845 if (current_lang_name == lang_name_c)
20847 error_at (token->location, "template with C linkage");
20848 /* Give it C++ linkage to avoid confusing other parts of the
20850 push_lang_context (lang_name_cplusplus);
20851 need_lang_pop = true;
20854 need_lang_pop = false;
20856 /* We cannot perform access checks on the template parameter
20857 declarations until we know what is being declared, just as we
20858 cannot check the decl-specifier list. */
20859 push_deferring_access_checks (dk_deferred);
20861 /* If the next token is `>', then we have an invalid
20862 specialization. Rather than complain about an invalid template
20863 parameter, issue an error message here. */
20864 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20866 cp_parser_error (parser, "invalid explicit specialization");
20867 begin_specialization ();
20868 parameter_list = NULL_TREE;
20872 /* Parse the template parameters. */
20873 parameter_list = cp_parser_template_parameter_list (parser);
20874 fixup_template_parms ();
20877 /* Get the deferred access checks from the parameter list. These
20878 will be checked once we know what is being declared, as for a
20879 member template the checks must be performed in the scope of the
20880 class containing the member. */
20881 checks = get_deferred_access_checks ();
20883 /* Look for the `>'. */
20884 cp_parser_skip_to_end_of_template_parameter_list (parser);
20885 /* We just processed one more parameter list. */
20886 ++parser->num_template_parameter_lists;
20887 /* If the next token is `template', there are more template
20889 if (cp_lexer_next_token_is_keyword (parser->lexer,
20891 cp_parser_template_declaration_after_export (parser, member_p);
20894 /* There are no access checks when parsing a template, as we do not
20895 know if a specialization will be a friend. */
20896 push_deferring_access_checks (dk_no_check);
20897 token = cp_lexer_peek_token (parser->lexer);
20898 decl = cp_parser_single_declaration (parser,
20901 /*explicit_specialization_p=*/false,
20903 pop_deferring_access_checks ();
20905 /* If this is a member template declaration, let the front
20907 if (member_p && !friend_p && decl)
20909 if (TREE_CODE (decl) == TYPE_DECL)
20910 cp_parser_check_access_in_redeclaration (decl, token->location);
20912 decl = finish_member_template_decl (decl);
20914 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20915 make_friend_class (current_class_type, TREE_TYPE (decl),
20916 /*complain=*/true);
20918 /* We are done with the current parameter list. */
20919 --parser->num_template_parameter_lists;
20921 pop_deferring_access_checks ();
20924 finish_template_decl (parameter_list);
20926 /* Check the template arguments for a literal operator template. */
20928 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
20929 && UDLIT_OPER_P (DECL_NAME (decl)))
20932 if (parameter_list == NULL_TREE)
20936 int num_parms = TREE_VEC_LENGTH (parameter_list);
20937 if (num_parms != 1)
20941 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
20942 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
20943 if (TREE_TYPE (parm) != char_type_node
20944 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
20949 error ("literal operator template %qD has invalid parameter list."
20950 " Expected non-type template argument pack <char...>",
20953 /* Register member declarations. */
20954 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20955 finish_member_declaration (decl);
20956 /* For the erroneous case of a template with C linkage, we pushed an
20957 implicit C++ linkage scope; exit that scope now. */
20959 pop_lang_context ();
20960 /* If DECL is a function template, we must return to parse it later.
20961 (Even though there is no definition, there might be default
20962 arguments that need handling.) */
20963 if (member_p && decl
20964 && (TREE_CODE (decl) == FUNCTION_DECL
20965 || DECL_FUNCTION_TEMPLATE_P (decl)))
20966 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20969 /* Perform the deferred access checks from a template-parameter-list.
20970 CHECKS is a TREE_LIST of access checks, as returned by
20971 get_deferred_access_checks. */
20974 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20976 ++processing_template_parmlist;
20977 perform_access_checks (checks);
20978 --processing_template_parmlist;
20981 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20982 `function-definition' sequence. MEMBER_P is true, this declaration
20983 appears in a class scope.
20985 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
20986 *FRIEND_P is set to TRUE iff the declaration is a friend. */
20989 cp_parser_single_declaration (cp_parser* parser,
20990 VEC (deferred_access_check,gc)* checks,
20992 bool explicit_specialization_p,
20995 int declares_class_or_enum;
20996 tree decl = NULL_TREE;
20997 cp_decl_specifier_seq decl_specifiers;
20998 bool function_definition_p = false;
20999 cp_token *decl_spec_token_start;
21001 /* This function is only used when processing a template
21003 gcc_assert (innermost_scope_kind () == sk_template_parms
21004 || innermost_scope_kind () == sk_template_spec);
21006 /* Defer access checks until we know what is being declared. */
21007 push_deferring_access_checks (dk_deferred);
21009 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21011 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21012 cp_parser_decl_specifier_seq (parser,
21013 CP_PARSER_FLAGS_OPTIONAL,
21015 &declares_class_or_enum);
21017 *friend_p = cp_parser_friend_p (&decl_specifiers);
21019 /* There are no template typedefs. */
21020 if (decl_specifiers.specs[(int) ds_typedef])
21022 error_at (decl_spec_token_start->location,
21023 "template declaration of %<typedef%>");
21024 decl = error_mark_node;
21027 /* Gather up the access checks that occurred the
21028 decl-specifier-seq. */
21029 stop_deferring_access_checks ();
21031 /* Check for the declaration of a template class. */
21032 if (declares_class_or_enum)
21034 if (cp_parser_declares_only_class_p (parser))
21036 decl = shadow_tag (&decl_specifiers);
21041 friend template <typename T> struct A<T>::B;
21044 A<T>::B will be represented by a TYPENAME_TYPE, and
21045 therefore not recognized by shadow_tag. */
21046 if (friend_p && *friend_p
21048 && decl_specifiers.type
21049 && TYPE_P (decl_specifiers.type))
21050 decl = decl_specifiers.type;
21052 if (decl && decl != error_mark_node)
21053 decl = TYPE_NAME (decl);
21055 decl = error_mark_node;
21057 /* Perform access checks for template parameters. */
21058 cp_parser_perform_template_parameter_access_checks (checks);
21062 /* Complain about missing 'typename' or other invalid type names. */
21063 if (!decl_specifiers.any_type_specifiers_p
21064 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21066 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21067 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21068 the rest of this declaration. */
21069 decl = error_mark_node;
21073 /* If it's not a template class, try for a template function. If
21074 the next token is a `;', then this declaration does not declare
21075 anything. But, if there were errors in the decl-specifiers, then
21076 the error might well have come from an attempted class-specifier.
21077 In that case, there's no need to warn about a missing declarator. */
21079 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21080 || decl_specifiers.type != error_mark_node))
21082 decl = cp_parser_init_declarator (parser,
21085 /*function_definition_allowed_p=*/true,
21087 declares_class_or_enum,
21088 &function_definition_p,
21091 /* 7.1.1-1 [dcl.stc]
21093 A storage-class-specifier shall not be specified in an explicit
21094 specialization... */
21096 && explicit_specialization_p
21097 && decl_specifiers.storage_class != sc_none)
21099 error_at (decl_spec_token_start->location,
21100 "explicit template specialization cannot have a storage class");
21101 decl = error_mark_node;
21105 /* Look for a trailing `;' after the declaration. */
21106 if (!function_definition_p
21107 && (decl == error_mark_node
21108 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21109 cp_parser_skip_to_end_of_block_or_statement (parser);
21112 pop_deferring_access_checks ();
21114 /* Clear any current qualification; whatever comes next is the start
21115 of something new. */
21116 parser->scope = NULL_TREE;
21117 parser->qualifying_scope = NULL_TREE;
21118 parser->object_scope = NULL_TREE;
21123 /* Parse a cast-expression that is not the operand of a unary "&". */
21126 cp_parser_simple_cast_expression (cp_parser *parser)
21128 return cp_parser_cast_expression (parser, /*address_p=*/false,
21129 /*cast_p=*/false, NULL);
21132 /* Parse a functional cast to TYPE. Returns an expression
21133 representing the cast. */
21136 cp_parser_functional_cast (cp_parser* parser, tree type)
21139 tree expression_list;
21143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21145 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21146 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21147 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21148 if (TREE_CODE (type) == TYPE_DECL)
21149 type = TREE_TYPE (type);
21150 return finish_compound_literal (type, expression_list,
21151 tf_warning_or_error);
21155 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21157 /*allow_expansion_p=*/true,
21158 /*non_constant_p=*/NULL);
21160 expression_list = error_mark_node;
21163 expression_list = build_tree_list_vec (vec);
21164 release_tree_vector (vec);
21167 cast = build_functional_cast (type, expression_list,
21168 tf_warning_or_error);
21169 /* [expr.const]/1: In an integral constant expression "only type
21170 conversions to integral or enumeration type can be used". */
21171 if (TREE_CODE (type) == TYPE_DECL)
21172 type = TREE_TYPE (type);
21173 if (cast != error_mark_node
21174 && !cast_valid_in_integral_constant_expression_p (type)
21175 && cp_parser_non_integral_constant_expression (parser,
21177 return error_mark_node;
21181 /* Save the tokens that make up the body of a member function defined
21182 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21183 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21184 specifiers applied to the declaration. Returns the FUNCTION_DECL
21185 for the member function. */
21188 cp_parser_save_member_function_body (cp_parser* parser,
21189 cp_decl_specifier_seq *decl_specifiers,
21190 cp_declarator *declarator,
21197 /* Create the FUNCTION_DECL. */
21198 fn = grokmethod (decl_specifiers, declarator, attributes);
21199 /* If something went badly wrong, bail out now. */
21200 if (fn == error_mark_node)
21202 /* If there's a function-body, skip it. */
21203 if (cp_parser_token_starts_function_definition_p
21204 (cp_lexer_peek_token (parser->lexer)))
21205 cp_parser_skip_to_end_of_block_or_statement (parser);
21206 return error_mark_node;
21209 /* Remember it, if there default args to post process. */
21210 cp_parser_save_default_args (parser, fn);
21212 /* Save away the tokens that make up the body of the
21214 first = parser->lexer->next_token;
21215 /* We can have braced-init-list mem-initializers before the fn body. */
21216 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21218 cp_lexer_consume_token (parser->lexer);
21219 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21220 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21222 /* cache_group will stop after an un-nested { } pair, too. */
21223 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21226 /* variadic mem-inits have ... after the ')'. */
21227 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21228 cp_lexer_consume_token (parser->lexer);
21231 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21232 /* Handle function try blocks. */
21233 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21234 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21235 last = parser->lexer->next_token;
21237 /* Save away the inline definition; we will process it when the
21238 class is complete. */
21239 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21240 DECL_PENDING_INLINE_P (fn) = 1;
21242 /* We need to know that this was defined in the class, so that
21243 friend templates are handled correctly. */
21244 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21246 /* Add FN to the queue of functions to be parsed later. */
21247 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21252 /* Save the tokens that make up the in-class initializer for a non-static
21253 data member. Returns a DEFAULT_ARG. */
21256 cp_parser_save_nsdmi (cp_parser* parser)
21258 /* Save away the tokens that make up the body of the
21260 cp_token *first = parser->lexer->next_token;
21264 /* Save tokens until the next comma or semicolon. */
21265 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21267 last = parser->lexer->next_token;
21269 node = make_node (DEFAULT_ARG);
21270 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21271 DEFARG_INSTANTIATIONS (node) = NULL;
21277 /* Parse a template-argument-list, as well as the trailing ">" (but
21278 not the opening "<"). See cp_parser_template_argument_list for the
21282 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21286 tree saved_qualifying_scope;
21287 tree saved_object_scope;
21288 bool saved_greater_than_is_operator_p;
21289 int saved_unevaluated_operand;
21290 int saved_inhibit_evaluation_warnings;
21294 When parsing a template-id, the first non-nested `>' is taken as
21295 the end of the template-argument-list rather than a greater-than
21297 saved_greater_than_is_operator_p
21298 = parser->greater_than_is_operator_p;
21299 parser->greater_than_is_operator_p = false;
21300 /* Parsing the argument list may modify SCOPE, so we save it
21302 saved_scope = parser->scope;
21303 saved_qualifying_scope = parser->qualifying_scope;
21304 saved_object_scope = parser->object_scope;
21305 /* We need to evaluate the template arguments, even though this
21306 template-id may be nested within a "sizeof". */
21307 saved_unevaluated_operand = cp_unevaluated_operand;
21308 cp_unevaluated_operand = 0;
21309 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21310 c_inhibit_evaluation_warnings = 0;
21311 /* Parse the template-argument-list itself. */
21312 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21313 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21314 arguments = NULL_TREE;
21316 arguments = cp_parser_template_argument_list (parser);
21317 /* Look for the `>' that ends the template-argument-list. If we find
21318 a '>>' instead, it's probably just a typo. */
21319 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21321 if (cxx_dialect != cxx98)
21323 /* In C++0x, a `>>' in a template argument list or cast
21324 expression is considered to be two separate `>'
21325 tokens. So, change the current token to a `>', but don't
21326 consume it: it will be consumed later when the outer
21327 template argument list (or cast expression) is parsed.
21328 Note that this replacement of `>' for `>>' is necessary
21329 even if we are parsing tentatively: in the tentative
21330 case, after calling
21331 cp_parser_enclosed_template_argument_list we will always
21332 throw away all of the template arguments and the first
21333 closing `>', either because the template argument list
21334 was erroneous or because we are replacing those tokens
21335 with a CPP_TEMPLATE_ID token. The second `>' (which will
21336 not have been thrown away) is needed either to close an
21337 outer template argument list or to complete a new-style
21339 cp_token *token = cp_lexer_peek_token (parser->lexer);
21340 token->type = CPP_GREATER;
21342 else if (!saved_greater_than_is_operator_p)
21344 /* If we're in a nested template argument list, the '>>' has
21345 to be a typo for '> >'. We emit the error message, but we
21346 continue parsing and we push a '>' as next token, so that
21347 the argument list will be parsed correctly. Note that the
21348 global source location is still on the token before the
21349 '>>', so we need to say explicitly where we want it. */
21350 cp_token *token = cp_lexer_peek_token (parser->lexer);
21351 error_at (token->location, "%<>>%> should be %<> >%> "
21352 "within a nested template argument list");
21354 token->type = CPP_GREATER;
21358 /* If this is not a nested template argument list, the '>>'
21359 is a typo for '>'. Emit an error message and continue.
21360 Same deal about the token location, but here we can get it
21361 right by consuming the '>>' before issuing the diagnostic. */
21362 cp_token *token = cp_lexer_consume_token (parser->lexer);
21363 error_at (token->location,
21364 "spurious %<>>%>, use %<>%> to terminate "
21365 "a template argument list");
21369 cp_parser_skip_to_end_of_template_parameter_list (parser);
21370 /* The `>' token might be a greater-than operator again now. */
21371 parser->greater_than_is_operator_p
21372 = saved_greater_than_is_operator_p;
21373 /* Restore the SAVED_SCOPE. */
21374 parser->scope = saved_scope;
21375 parser->qualifying_scope = saved_qualifying_scope;
21376 parser->object_scope = saved_object_scope;
21377 cp_unevaluated_operand = saved_unevaluated_operand;
21378 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21383 /* MEMBER_FUNCTION is a member function, or a friend. If default
21384 arguments, or the body of the function have not yet been parsed,
21388 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21390 timevar_push (TV_PARSE_INMETH);
21391 /* If this member is a template, get the underlying
21393 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21394 member_function = DECL_TEMPLATE_RESULT (member_function);
21396 /* There should not be any class definitions in progress at this
21397 point; the bodies of members are only parsed outside of all class
21399 gcc_assert (parser->num_classes_being_defined == 0);
21400 /* While we're parsing the member functions we might encounter more
21401 classes. We want to handle them right away, but we don't want
21402 them getting mixed up with functions that are currently in the
21404 push_unparsed_function_queues (parser);
21406 /* Make sure that any template parameters are in scope. */
21407 maybe_begin_member_template_processing (member_function);
21409 /* If the body of the function has not yet been parsed, parse it
21411 if (DECL_PENDING_INLINE_P (member_function))
21413 tree function_scope;
21414 cp_token_cache *tokens;
21416 /* The function is no longer pending; we are processing it. */
21417 tokens = DECL_PENDING_INLINE_INFO (member_function);
21418 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21419 DECL_PENDING_INLINE_P (member_function) = 0;
21421 /* If this is a local class, enter the scope of the containing
21423 function_scope = current_function_decl;
21424 if (function_scope)
21425 push_function_context ();
21427 /* Push the body of the function onto the lexer stack. */
21428 cp_parser_push_lexer_for_tokens (parser, tokens);
21430 /* Let the front end know that we going to be defining this
21432 start_preparsed_function (member_function, NULL_TREE,
21433 SF_PRE_PARSED | SF_INCLASS_INLINE);
21435 /* Don't do access checking if it is a templated function. */
21436 if (processing_template_decl)
21437 push_deferring_access_checks (dk_no_check);
21439 /* Now, parse the body of the function. */
21440 cp_parser_function_definition_after_declarator (parser,
21441 /*inline_p=*/true);
21443 if (processing_template_decl)
21444 pop_deferring_access_checks ();
21446 /* Leave the scope of the containing function. */
21447 if (function_scope)
21448 pop_function_context ();
21449 cp_parser_pop_lexer (parser);
21452 /* Remove any template parameters from the symbol table. */
21453 maybe_end_member_template_processing ();
21455 /* Restore the queue. */
21456 pop_unparsed_function_queues (parser);
21457 timevar_pop (TV_PARSE_INMETH);
21460 /* If DECL contains any default args, remember it on the unparsed
21461 functions queue. */
21464 cp_parser_save_default_args (cp_parser* parser, tree decl)
21468 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21470 probe = TREE_CHAIN (probe))
21471 if (TREE_PURPOSE (probe))
21473 cp_default_arg_entry *entry
21474 = VEC_safe_push (cp_default_arg_entry, gc,
21475 unparsed_funs_with_default_args, NULL);
21476 entry->class_type = current_class_type;
21477 entry->decl = decl;
21482 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21483 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21484 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21485 from the parameter-type-list. */
21488 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21489 tree default_arg, tree parmtype)
21491 cp_token_cache *tokens;
21495 /* Push the saved tokens for the default argument onto the parser's
21497 tokens = DEFARG_TOKENS (default_arg);
21498 cp_parser_push_lexer_for_tokens (parser, tokens);
21500 start_lambda_scope (decl);
21502 /* Parse the default argument. */
21503 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21504 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21505 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21507 finish_lambda_scope ();
21509 if (!processing_template_decl)
21511 /* In a non-template class, check conversions now. In a template,
21512 we'll wait and instantiate these as needed. */
21513 if (TREE_CODE (decl) == PARM_DECL)
21514 parsed_arg = check_default_argument (parmtype, parsed_arg);
21517 int flags = LOOKUP_IMPLICIT;
21518 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21519 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21520 flags = LOOKUP_NORMAL;
21521 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21525 /* If the token stream has not been completely used up, then
21526 there was extra junk after the end of the default
21528 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21530 if (TREE_CODE (decl) == PARM_DECL)
21531 cp_parser_error (parser, "expected %<,%>");
21533 cp_parser_error (parser, "expected %<;%>");
21536 /* Revert to the main lexer. */
21537 cp_parser_pop_lexer (parser);
21542 /* FIELD is a non-static data member with an initializer which we saved for
21543 later; parse it now. */
21546 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21550 push_unparsed_function_queues (parser);
21551 def = cp_parser_late_parse_one_default_arg (parser, field,
21552 DECL_INITIAL (field),
21554 pop_unparsed_function_queues (parser);
21556 DECL_INITIAL (field) = def;
21559 /* FN is a FUNCTION_DECL which may contains a parameter with an
21560 unparsed DEFAULT_ARG. Parse the default args now. This function
21561 assumes that the current scope is the scope in which the default
21562 argument should be processed. */
21565 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21567 bool saved_local_variables_forbidden_p;
21568 tree parm, parmdecl;
21570 /* While we're parsing the default args, we might (due to the
21571 statement expression extension) encounter more classes. We want
21572 to handle them right away, but we don't want them getting mixed
21573 up with default args that are currently in the queue. */
21574 push_unparsed_function_queues (parser);
21576 /* Local variable names (and the `this' keyword) may not appear
21577 in a default argument. */
21578 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21579 parser->local_variables_forbidden_p = true;
21581 push_defarg_context (fn);
21583 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21584 parmdecl = DECL_ARGUMENTS (fn);
21585 parm && parm != void_list_node;
21586 parm = TREE_CHAIN (parm),
21587 parmdecl = DECL_CHAIN (parmdecl))
21589 tree default_arg = TREE_PURPOSE (parm);
21591 VEC(tree,gc) *insts;
21598 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21599 /* This can happen for a friend declaration for a function
21600 already declared with default arguments. */
21604 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21606 TREE_VALUE (parm));
21607 if (parsed_arg == error_mark_node)
21612 TREE_PURPOSE (parm) = parsed_arg;
21614 /* Update any instantiations we've already created. */
21615 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21616 VEC_iterate (tree, insts, ix, copy); ix++)
21617 TREE_PURPOSE (copy) = parsed_arg;
21620 pop_defarg_context ();
21622 /* Make sure no default arg is missing. */
21623 check_default_args (fn);
21625 /* Restore the state of local_variables_forbidden_p. */
21626 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21628 /* Restore the queue. */
21629 pop_unparsed_function_queues (parser);
21632 /* Parse the operand of `sizeof' (or a similar operator). Returns
21633 either a TYPE or an expression, depending on the form of the
21634 input. The KEYWORD indicates which kind of expression we have
21638 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21640 tree expr = NULL_TREE;
21641 const char *saved_message;
21643 bool saved_integral_constant_expression_p;
21644 bool saved_non_integral_constant_expression_p;
21645 bool pack_expansion_p = false;
21647 /* Types cannot be defined in a `sizeof' expression. Save away the
21649 saved_message = parser->type_definition_forbidden_message;
21650 /* And create the new one. */
21651 tmp = concat ("types may not be defined in %<",
21652 IDENTIFIER_POINTER (ridpointers[keyword]),
21653 "%> expressions", NULL);
21654 parser->type_definition_forbidden_message = tmp;
21656 /* The restrictions on constant-expressions do not apply inside
21657 sizeof expressions. */
21658 saved_integral_constant_expression_p
21659 = parser->integral_constant_expression_p;
21660 saved_non_integral_constant_expression_p
21661 = parser->non_integral_constant_expression_p;
21662 parser->integral_constant_expression_p = false;
21664 /* If it's a `...', then we are computing the length of a parameter
21666 if (keyword == RID_SIZEOF
21667 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21669 /* Consume the `...'. */
21670 cp_lexer_consume_token (parser->lexer);
21671 maybe_warn_variadic_templates ();
21673 /* Note that this is an expansion. */
21674 pack_expansion_p = true;
21677 /* Do not actually evaluate the expression. */
21678 ++cp_unevaluated_operand;
21679 ++c_inhibit_evaluation_warnings;
21680 /* If it's a `(', then we might be looking at the type-id
21682 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21685 bool saved_in_type_id_in_expr_p;
21687 /* We can't be sure yet whether we're looking at a type-id or an
21689 cp_parser_parse_tentatively (parser);
21690 /* Consume the `('. */
21691 cp_lexer_consume_token (parser->lexer);
21692 /* Parse the type-id. */
21693 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21694 parser->in_type_id_in_expr_p = true;
21695 type = cp_parser_type_id (parser);
21696 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21697 /* Now, look for the trailing `)'. */
21698 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21699 /* If all went well, then we're done. */
21700 if (cp_parser_parse_definitely (parser))
21702 cp_decl_specifier_seq decl_specs;
21704 /* Build a trivial decl-specifier-seq. */
21705 clear_decl_specs (&decl_specs);
21706 decl_specs.type = type;
21708 /* Call grokdeclarator to figure out what type this is. */
21709 expr = grokdeclarator (NULL,
21713 /*attrlist=*/NULL);
21717 /* If the type-id production did not work out, then we must be
21718 looking at the unary-expression production. */
21720 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21721 /*cast_p=*/false, NULL);
21723 if (pack_expansion_p)
21724 /* Build a pack expansion. */
21725 expr = make_pack_expansion (expr);
21727 /* Go back to evaluating expressions. */
21728 --cp_unevaluated_operand;
21729 --c_inhibit_evaluation_warnings;
21731 /* Free the message we created. */
21733 /* And restore the old one. */
21734 parser->type_definition_forbidden_message = saved_message;
21735 parser->integral_constant_expression_p
21736 = saved_integral_constant_expression_p;
21737 parser->non_integral_constant_expression_p
21738 = saved_non_integral_constant_expression_p;
21743 /* If the current declaration has no declarator, return true. */
21746 cp_parser_declares_only_class_p (cp_parser *parser)
21748 /* If the next token is a `;' or a `,' then there is no
21750 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21751 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21754 /* Update the DECL_SPECS to reflect the storage class indicated by
21758 cp_parser_set_storage_class (cp_parser *parser,
21759 cp_decl_specifier_seq *decl_specs,
21761 location_t location)
21763 cp_storage_class storage_class;
21765 if (parser->in_unbraced_linkage_specification_p)
21767 error_at (location, "invalid use of %qD in linkage specification",
21768 ridpointers[keyword]);
21771 else if (decl_specs->storage_class != sc_none)
21773 decl_specs->conflicting_specifiers_p = true;
21777 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21778 && decl_specs->specs[(int) ds_thread])
21780 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21781 decl_specs->specs[(int) ds_thread] = 0;
21787 storage_class = sc_auto;
21790 storage_class = sc_register;
21793 storage_class = sc_static;
21796 storage_class = sc_extern;
21799 storage_class = sc_mutable;
21802 gcc_unreachable ();
21804 decl_specs->storage_class = storage_class;
21806 /* A storage class specifier cannot be applied alongside a typedef
21807 specifier. If there is a typedef specifier present then set
21808 conflicting_specifiers_p which will trigger an error later
21809 on in grokdeclarator. */
21810 if (decl_specs->specs[(int)ds_typedef])
21811 decl_specs->conflicting_specifiers_p = true;
21814 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
21815 is true, the type is a class or enum definition. */
21818 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21820 location_t location,
21821 bool type_definition_p)
21823 decl_specs->any_specifiers_p = true;
21825 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21826 (with, for example, in "typedef int wchar_t;") we remember that
21827 this is what happened. In system headers, we ignore these
21828 declarations so that G++ can work with system headers that are not
21830 if (decl_specs->specs[(int) ds_typedef]
21831 && !type_definition_p
21832 && (type_spec == boolean_type_node
21833 || type_spec == char16_type_node
21834 || type_spec == char32_type_node
21835 || type_spec == wchar_type_node)
21836 && (decl_specs->type
21837 || decl_specs->specs[(int) ds_long]
21838 || decl_specs->specs[(int) ds_short]
21839 || decl_specs->specs[(int) ds_unsigned]
21840 || decl_specs->specs[(int) ds_signed]))
21842 decl_specs->redefined_builtin_type = type_spec;
21843 if (!decl_specs->type)
21845 decl_specs->type = type_spec;
21846 decl_specs->type_definition_p = false;
21847 decl_specs->type_location = location;
21850 else if (decl_specs->type)
21851 decl_specs->multiple_types_p = true;
21854 decl_specs->type = type_spec;
21855 decl_specs->type_definition_p = type_definition_p;
21856 decl_specs->redefined_builtin_type = NULL_TREE;
21857 decl_specs->type_location = location;
21861 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21862 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
21865 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21867 return decl_specifiers->specs[(int) ds_friend] != 0;
21870 /* Issue an error message indicating that TOKEN_DESC was expected.
21871 If KEYWORD is true, it indicated this function is called by
21872 cp_parser_require_keword and the required token can only be
21873 a indicated keyword. */
21876 cp_parser_required_error (cp_parser *parser,
21877 required_token token_desc,
21880 switch (token_desc)
21883 cp_parser_error (parser, "expected %<new%>");
21886 cp_parser_error (parser, "expected %<delete%>");
21889 cp_parser_error (parser, "expected %<return%>");
21892 cp_parser_error (parser, "expected %<while%>");
21895 cp_parser_error (parser, "expected %<extern%>");
21897 case RT_STATIC_ASSERT:
21898 cp_parser_error (parser, "expected %<static_assert%>");
21901 cp_parser_error (parser, "expected %<decltype%>");
21904 cp_parser_error (parser, "expected %<operator%>");
21907 cp_parser_error (parser, "expected %<class%>");
21910 cp_parser_error (parser, "expected %<template%>");
21913 cp_parser_error (parser, "expected %<namespace%>");
21916 cp_parser_error (parser, "expected %<using%>");
21919 cp_parser_error (parser, "expected %<asm%>");
21922 cp_parser_error (parser, "expected %<try%>");
21925 cp_parser_error (parser, "expected %<catch%>");
21928 cp_parser_error (parser, "expected %<throw%>");
21931 cp_parser_error (parser, "expected %<__label__%>");
21934 cp_parser_error (parser, "expected %<@try%>");
21936 case RT_AT_SYNCHRONIZED:
21937 cp_parser_error (parser, "expected %<@synchronized%>");
21940 cp_parser_error (parser, "expected %<@throw%>");
21947 switch (token_desc)
21950 cp_parser_error (parser, "expected %<;%>");
21952 case RT_OPEN_PAREN:
21953 cp_parser_error (parser, "expected %<(%>");
21955 case RT_CLOSE_BRACE:
21956 cp_parser_error (parser, "expected %<}%>");
21958 case RT_OPEN_BRACE:
21959 cp_parser_error (parser, "expected %<{%>");
21961 case RT_CLOSE_SQUARE:
21962 cp_parser_error (parser, "expected %<]%>");
21964 case RT_OPEN_SQUARE:
21965 cp_parser_error (parser, "expected %<[%>");
21968 cp_parser_error (parser, "expected %<,%>");
21971 cp_parser_error (parser, "expected %<::%>");
21974 cp_parser_error (parser, "expected %<<%>");
21977 cp_parser_error (parser, "expected %<>%>");
21980 cp_parser_error (parser, "expected %<=%>");
21983 cp_parser_error (parser, "expected %<...%>");
21986 cp_parser_error (parser, "expected %<*%>");
21989 cp_parser_error (parser, "expected %<~%>");
21992 cp_parser_error (parser, "expected %<:%>");
21994 case RT_COLON_SCOPE:
21995 cp_parser_error (parser, "expected %<:%> or %<::%>");
21997 case RT_CLOSE_PAREN:
21998 cp_parser_error (parser, "expected %<)%>");
22000 case RT_COMMA_CLOSE_PAREN:
22001 cp_parser_error (parser, "expected %<,%> or %<)%>");
22003 case RT_PRAGMA_EOL:
22004 cp_parser_error (parser, "expected end of line");
22007 cp_parser_error (parser, "expected identifier");
22010 cp_parser_error (parser, "expected selection-statement");
22012 case RT_INTERATION:
22013 cp_parser_error (parser, "expected iteration-statement");
22016 cp_parser_error (parser, "expected jump-statement");
22019 cp_parser_error (parser, "expected class-key");
22021 case RT_CLASS_TYPENAME_TEMPLATE:
22022 cp_parser_error (parser,
22023 "expected %<class%>, %<typename%>, or %<template%>");
22026 gcc_unreachable ();
22030 gcc_unreachable ();
22035 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22036 issue an error message indicating that TOKEN_DESC was expected.
22038 Returns the token consumed, if the token had the appropriate type.
22039 Otherwise, returns NULL. */
22042 cp_parser_require (cp_parser* parser,
22043 enum cpp_ttype type,
22044 required_token token_desc)
22046 if (cp_lexer_next_token_is (parser->lexer, type))
22047 return cp_lexer_consume_token (parser->lexer);
22050 /* Output the MESSAGE -- unless we're parsing tentatively. */
22051 if (!cp_parser_simulate_error (parser))
22052 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22057 /* An error message is produced if the next token is not '>'.
22058 All further tokens are skipped until the desired token is
22059 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22062 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22064 /* Current level of '< ... >'. */
22065 unsigned level = 0;
22066 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22067 unsigned nesting_depth = 0;
22069 /* Are we ready, yet? If not, issue error message. */
22070 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22073 /* Skip tokens until the desired token is found. */
22076 /* Peek at the next token. */
22077 switch (cp_lexer_peek_token (parser->lexer)->type)
22080 if (!nesting_depth)
22085 if (cxx_dialect == cxx98)
22086 /* C++0x views the `>>' operator as two `>' tokens, but
22089 else if (!nesting_depth && level-- == 0)
22091 /* We've hit a `>>' where the first `>' closes the
22092 template argument list, and the second `>' is
22093 spurious. Just consume the `>>' and stop; we've
22094 already produced at least one error. */
22095 cp_lexer_consume_token (parser->lexer);
22098 /* Fall through for C++0x, so we handle the second `>' in
22102 if (!nesting_depth && level-- == 0)
22104 /* We've reached the token we want, consume it and stop. */
22105 cp_lexer_consume_token (parser->lexer);
22110 case CPP_OPEN_PAREN:
22111 case CPP_OPEN_SQUARE:
22115 case CPP_CLOSE_PAREN:
22116 case CPP_CLOSE_SQUARE:
22117 if (nesting_depth-- == 0)
22122 case CPP_PRAGMA_EOL:
22123 case CPP_SEMICOLON:
22124 case CPP_OPEN_BRACE:
22125 case CPP_CLOSE_BRACE:
22126 /* The '>' was probably forgotten, don't look further. */
22133 /* Consume this token. */
22134 cp_lexer_consume_token (parser->lexer);
22138 /* If the next token is the indicated keyword, consume it. Otherwise,
22139 issue an error message indicating that TOKEN_DESC was expected.
22141 Returns the token consumed, if the token had the appropriate type.
22142 Otherwise, returns NULL. */
22145 cp_parser_require_keyword (cp_parser* parser,
22147 required_token token_desc)
22149 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22151 if (token && token->keyword != keyword)
22153 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22160 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22161 function-definition. */
22164 cp_parser_token_starts_function_definition_p (cp_token* token)
22166 return (/* An ordinary function-body begins with an `{'. */
22167 token->type == CPP_OPEN_BRACE
22168 /* A ctor-initializer begins with a `:'. */
22169 || token->type == CPP_COLON
22170 /* A function-try-block begins with `try'. */
22171 || token->keyword == RID_TRY
22172 /* The named return value extension begins with `return'. */
22173 || token->keyword == RID_RETURN);
22176 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22180 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22184 token = cp_lexer_peek_token (parser->lexer);
22185 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22188 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22189 C++0x) ending a template-argument. */
22192 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22196 token = cp_lexer_peek_token (parser->lexer);
22197 return (token->type == CPP_COMMA
22198 || token->type == CPP_GREATER
22199 || token->type == CPP_ELLIPSIS
22200 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22203 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22204 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22207 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22212 token = cp_lexer_peek_nth_token (parser->lexer, n);
22213 if (token->type == CPP_LESS)
22215 /* Check for the sequence `<::' in the original code. It would be lexed as
22216 `[:', where `[' is a digraph, and there is no whitespace before
22218 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22221 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22222 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22228 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22229 or none_type otherwise. */
22231 static enum tag_types
22232 cp_parser_token_is_class_key (cp_token* token)
22234 switch (token->keyword)
22239 return record_type;
22248 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22251 cp_parser_check_class_key (enum tag_types class_key, tree type)
22253 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22254 permerror (input_location, "%qs tag used in naming %q#T",
22255 class_key == union_type ? "union"
22256 : class_key == record_type ? "struct" : "class",
22260 /* Issue an error message if DECL is redeclared with different
22261 access than its original declaration [class.access.spec/3].
22262 This applies to nested classes and nested class templates.
22266 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22268 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22271 if ((TREE_PRIVATE (decl)
22272 != (current_access_specifier == access_private_node))
22273 || (TREE_PROTECTED (decl)
22274 != (current_access_specifier == access_protected_node)))
22275 error_at (location, "%qD redeclared with different access", decl);
22278 /* Look for the `template' keyword, as a syntactic disambiguator.
22279 Return TRUE iff it is present, in which case it will be
22283 cp_parser_optional_template_keyword (cp_parser *parser)
22285 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22287 /* The `template' keyword can only be used within templates;
22288 outside templates the parser can always figure out what is a
22289 template and what is not. */
22290 if (!processing_template_decl)
22292 cp_token *token = cp_lexer_peek_token (parser->lexer);
22293 error_at (token->location,
22294 "%<template%> (as a disambiguator) is only allowed "
22295 "within templates");
22296 /* If this part of the token stream is rescanned, the same
22297 error message would be generated. So, we purge the token
22298 from the stream. */
22299 cp_lexer_purge_token (parser->lexer);
22304 /* Consume the `template' keyword. */
22305 cp_lexer_consume_token (parser->lexer);
22313 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22314 set PARSER->SCOPE, and perform other related actions. */
22317 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22320 struct tree_check *check_value;
22321 deferred_access_check *chk;
22322 VEC (deferred_access_check,gc) *checks;
22324 /* Get the stored value. */
22325 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22326 /* Perform any access checks that were deferred. */
22327 checks = check_value->checks;
22330 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22331 perform_or_defer_access_check (chk->binfo,
22335 /* Set the scope from the stored value. */
22336 parser->scope = check_value->value;
22337 parser->qualifying_scope = check_value->qualifying_scope;
22338 parser->object_scope = NULL_TREE;
22341 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22342 encounter the end of a block before what we were looking for. */
22345 cp_parser_cache_group (cp_parser *parser,
22346 enum cpp_ttype end,
22351 cp_token *token = cp_lexer_peek_token (parser->lexer);
22353 /* Abort a parenthesized expression if we encounter a semicolon. */
22354 if ((end == CPP_CLOSE_PAREN || depth == 0)
22355 && token->type == CPP_SEMICOLON)
22357 /* If we've reached the end of the file, stop. */
22358 if (token->type == CPP_EOF
22359 || (end != CPP_PRAGMA_EOL
22360 && token->type == CPP_PRAGMA_EOL))
22362 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22363 /* We've hit the end of an enclosing block, so there's been some
22364 kind of syntax error. */
22367 /* If we're caching something finished by a comma (or semicolon),
22368 such as an NSDMI, don't consume the comma. */
22369 if (end == CPP_COMMA
22370 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22373 /* Consume the token. */
22374 cp_lexer_consume_token (parser->lexer);
22375 /* See if it starts a new group. */
22376 if (token->type == CPP_OPEN_BRACE)
22378 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22379 /* In theory this should probably check end == '}', but
22380 cp_parser_save_member_function_body needs it to exit
22381 after either '}' or ')' when called with ')'. */
22385 else if (token->type == CPP_OPEN_PAREN)
22387 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22388 if (depth == 0 && end == CPP_CLOSE_PAREN)
22391 else if (token->type == CPP_PRAGMA)
22392 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22393 else if (token->type == end)
22398 /* Begin parsing tentatively. We always save tokens while parsing
22399 tentatively so that if the tentative parsing fails we can restore the
22403 cp_parser_parse_tentatively (cp_parser* parser)
22405 /* Enter a new parsing context. */
22406 parser->context = cp_parser_context_new (parser->context);
22407 /* Begin saving tokens. */
22408 cp_lexer_save_tokens (parser->lexer);
22409 /* In order to avoid repetitive access control error messages,
22410 access checks are queued up until we are no longer parsing
22412 push_deferring_access_checks (dk_deferred);
22415 /* Commit to the currently active tentative parse. */
22418 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22420 cp_parser_context *context;
22423 /* Mark all of the levels as committed. */
22424 lexer = parser->lexer;
22425 for (context = parser->context; context->next; context = context->next)
22427 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22429 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22430 while (!cp_lexer_saving_tokens (lexer))
22431 lexer = lexer->next;
22432 cp_lexer_commit_tokens (lexer);
22436 /* Abort the currently active tentative parse. All consumed tokens
22437 will be rolled back, and no diagnostics will be issued. */
22440 cp_parser_abort_tentative_parse (cp_parser* parser)
22442 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22443 || errorcount > 0);
22444 cp_parser_simulate_error (parser);
22445 /* Now, pretend that we want to see if the construct was
22446 successfully parsed. */
22447 cp_parser_parse_definitely (parser);
22450 /* Stop parsing tentatively. If a parse error has occurred, restore the
22451 token stream. Otherwise, commit to the tokens we have consumed.
22452 Returns true if no error occurred; false otherwise. */
22455 cp_parser_parse_definitely (cp_parser* parser)
22457 bool error_occurred;
22458 cp_parser_context *context;
22460 /* Remember whether or not an error occurred, since we are about to
22461 destroy that information. */
22462 error_occurred = cp_parser_error_occurred (parser);
22463 /* Remove the topmost context from the stack. */
22464 context = parser->context;
22465 parser->context = context->next;
22466 /* If no parse errors occurred, commit to the tentative parse. */
22467 if (!error_occurred)
22469 /* Commit to the tokens read tentatively, unless that was
22471 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22472 cp_lexer_commit_tokens (parser->lexer);
22474 pop_to_parent_deferring_access_checks ();
22476 /* Otherwise, if errors occurred, roll back our state so that things
22477 are just as they were before we began the tentative parse. */
22480 cp_lexer_rollback_tokens (parser->lexer);
22481 pop_deferring_access_checks ();
22483 /* Add the context to the front of the free list. */
22484 context->next = cp_parser_context_free_list;
22485 cp_parser_context_free_list = context;
22487 return !error_occurred;
22490 /* Returns true if we are parsing tentatively and are not committed to
22491 this tentative parse. */
22494 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22496 return (cp_parser_parsing_tentatively (parser)
22497 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22500 /* Returns nonzero iff an error has occurred during the most recent
22501 tentative parse. */
22504 cp_parser_error_occurred (cp_parser* parser)
22506 return (cp_parser_parsing_tentatively (parser)
22507 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22510 /* Returns nonzero if GNU extensions are allowed. */
22513 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22515 return parser->allow_gnu_extensions_p;
22518 /* Objective-C++ Productions */
22521 /* Parse an Objective-C expression, which feeds into a primary-expression
22525 objc-message-expression
22526 objc-string-literal
22527 objc-encode-expression
22528 objc-protocol-expression
22529 objc-selector-expression
22531 Returns a tree representation of the expression. */
22534 cp_parser_objc_expression (cp_parser* parser)
22536 /* Try to figure out what kind of declaration is present. */
22537 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22541 case CPP_OPEN_SQUARE:
22542 return cp_parser_objc_message_expression (parser);
22544 case CPP_OBJC_STRING:
22545 kwd = cp_lexer_consume_token (parser->lexer);
22546 return objc_build_string_object (kwd->u.value);
22549 switch (kwd->keyword)
22551 case RID_AT_ENCODE:
22552 return cp_parser_objc_encode_expression (parser);
22554 case RID_AT_PROTOCOL:
22555 return cp_parser_objc_protocol_expression (parser);
22557 case RID_AT_SELECTOR:
22558 return cp_parser_objc_selector_expression (parser);
22564 error_at (kwd->location,
22565 "misplaced %<@%D%> Objective-C++ construct",
22567 cp_parser_skip_to_end_of_block_or_statement (parser);
22570 return error_mark_node;
22573 /* Parse an Objective-C message expression.
22575 objc-message-expression:
22576 [ objc-message-receiver objc-message-args ]
22578 Returns a representation of an Objective-C message. */
22581 cp_parser_objc_message_expression (cp_parser* parser)
22583 tree receiver, messageargs;
22585 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22586 receiver = cp_parser_objc_message_receiver (parser);
22587 messageargs = cp_parser_objc_message_args (parser);
22588 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22590 return objc_build_message_expr (receiver, messageargs);
22593 /* Parse an objc-message-receiver.
22595 objc-message-receiver:
22597 simple-type-specifier
22599 Returns a representation of the type or expression. */
22602 cp_parser_objc_message_receiver (cp_parser* parser)
22606 /* An Objective-C message receiver may be either (1) a type
22607 or (2) an expression. */
22608 cp_parser_parse_tentatively (parser);
22609 rcv = cp_parser_expression (parser, false, NULL);
22611 if (cp_parser_parse_definitely (parser))
22614 rcv = cp_parser_simple_type_specifier (parser,
22615 /*decl_specs=*/NULL,
22616 CP_PARSER_FLAGS_NONE);
22618 return objc_get_class_reference (rcv);
22621 /* Parse the arguments and selectors comprising an Objective-C message.
22626 objc-selector-args , objc-comma-args
22628 objc-selector-args:
22629 objc-selector [opt] : assignment-expression
22630 objc-selector-args objc-selector [opt] : assignment-expression
22633 assignment-expression
22634 objc-comma-args , assignment-expression
22636 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22637 selector arguments and TREE_VALUE containing a list of comma
22641 cp_parser_objc_message_args (cp_parser* parser)
22643 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22644 bool maybe_unary_selector_p = true;
22645 cp_token *token = cp_lexer_peek_token (parser->lexer);
22647 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22649 tree selector = NULL_TREE, arg;
22651 if (token->type != CPP_COLON)
22652 selector = cp_parser_objc_selector (parser);
22654 /* Detect if we have a unary selector. */
22655 if (maybe_unary_selector_p
22656 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22657 return build_tree_list (selector, NULL_TREE);
22659 maybe_unary_selector_p = false;
22660 cp_parser_require (parser, CPP_COLON, RT_COLON);
22661 arg = cp_parser_assignment_expression (parser, false, NULL);
22664 = chainon (sel_args,
22665 build_tree_list (selector, arg));
22667 token = cp_lexer_peek_token (parser->lexer);
22670 /* Handle non-selector arguments, if any. */
22671 while (token->type == CPP_COMMA)
22675 cp_lexer_consume_token (parser->lexer);
22676 arg = cp_parser_assignment_expression (parser, false, NULL);
22679 = chainon (addl_args,
22680 build_tree_list (NULL_TREE, arg));
22682 token = cp_lexer_peek_token (parser->lexer);
22685 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22687 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22688 return build_tree_list (error_mark_node, error_mark_node);
22691 return build_tree_list (sel_args, addl_args);
22694 /* Parse an Objective-C encode expression.
22696 objc-encode-expression:
22697 @encode objc-typename
22699 Returns an encoded representation of the type argument. */
22702 cp_parser_objc_encode_expression (cp_parser* parser)
22707 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22708 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22709 token = cp_lexer_peek_token (parser->lexer);
22710 type = complete_type (cp_parser_type_id (parser));
22711 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22715 error_at (token->location,
22716 "%<@encode%> must specify a type as an argument");
22717 return error_mark_node;
22720 /* This happens if we find @encode(T) (where T is a template
22721 typename or something dependent on a template typename) when
22722 parsing a template. In that case, we can't compile it
22723 immediately, but we rather create an AT_ENCODE_EXPR which will
22724 need to be instantiated when the template is used.
22726 if (dependent_type_p (type))
22728 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22729 TREE_READONLY (value) = 1;
22733 return objc_build_encode_expr (type);
22736 /* Parse an Objective-C @defs expression. */
22739 cp_parser_objc_defs_expression (cp_parser *parser)
22743 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22744 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22745 name = cp_parser_identifier (parser);
22746 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22748 return objc_get_class_ivars (name);
22751 /* Parse an Objective-C protocol expression.
22753 objc-protocol-expression:
22754 @protocol ( identifier )
22756 Returns a representation of the protocol expression. */
22759 cp_parser_objc_protocol_expression (cp_parser* parser)
22763 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22764 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22765 proto = cp_parser_identifier (parser);
22766 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22768 return objc_build_protocol_expr (proto);
22771 /* Parse an Objective-C selector expression.
22773 objc-selector-expression:
22774 @selector ( objc-method-signature )
22776 objc-method-signature:
22782 objc-selector-seq objc-selector :
22784 Returns a representation of the method selector. */
22787 cp_parser_objc_selector_expression (cp_parser* parser)
22789 tree sel_seq = NULL_TREE;
22790 bool maybe_unary_selector_p = true;
22792 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22794 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
22795 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22796 token = cp_lexer_peek_token (parser->lexer);
22798 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22799 || token->type == CPP_SCOPE)
22801 tree selector = NULL_TREE;
22803 if (token->type != CPP_COLON
22804 || token->type == CPP_SCOPE)
22805 selector = cp_parser_objc_selector (parser);
22807 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22808 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22810 /* Detect if we have a unary selector. */
22811 if (maybe_unary_selector_p)
22813 sel_seq = selector;
22814 goto finish_selector;
22818 cp_parser_error (parser, "expected %<:%>");
22821 maybe_unary_selector_p = false;
22822 token = cp_lexer_consume_token (parser->lexer);
22824 if (token->type == CPP_SCOPE)
22827 = chainon (sel_seq,
22828 build_tree_list (selector, NULL_TREE));
22830 = chainon (sel_seq,
22831 build_tree_list (NULL_TREE, NULL_TREE));
22835 = chainon (sel_seq,
22836 build_tree_list (selector, NULL_TREE));
22838 token = cp_lexer_peek_token (parser->lexer);
22842 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22844 return objc_build_selector_expr (loc, sel_seq);
22847 /* Parse a list of identifiers.
22849 objc-identifier-list:
22851 objc-identifier-list , identifier
22853 Returns a TREE_LIST of identifier nodes. */
22856 cp_parser_objc_identifier_list (cp_parser* parser)
22862 identifier = cp_parser_identifier (parser);
22863 if (identifier == error_mark_node)
22864 return error_mark_node;
22866 list = build_tree_list (NULL_TREE, identifier);
22867 sep = cp_lexer_peek_token (parser->lexer);
22869 while (sep->type == CPP_COMMA)
22871 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22872 identifier = cp_parser_identifier (parser);
22873 if (identifier == error_mark_node)
22876 list = chainon (list, build_tree_list (NULL_TREE,
22878 sep = cp_lexer_peek_token (parser->lexer);
22884 /* Parse an Objective-C alias declaration.
22886 objc-alias-declaration:
22887 @compatibility_alias identifier identifier ;
22889 This function registers the alias mapping with the Objective-C front end.
22890 It returns nothing. */
22893 cp_parser_objc_alias_declaration (cp_parser* parser)
22897 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
22898 alias = cp_parser_identifier (parser);
22899 orig = cp_parser_identifier (parser);
22900 objc_declare_alias (alias, orig);
22901 cp_parser_consume_semicolon_at_end_of_statement (parser);
22904 /* Parse an Objective-C class forward-declaration.
22906 objc-class-declaration:
22907 @class objc-identifier-list ;
22909 The function registers the forward declarations with the Objective-C
22910 front end. It returns nothing. */
22913 cp_parser_objc_class_declaration (cp_parser* parser)
22915 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
22920 id = cp_parser_identifier (parser);
22921 if (id == error_mark_node)
22924 objc_declare_class (id);
22926 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22927 cp_lexer_consume_token (parser->lexer);
22931 cp_parser_consume_semicolon_at_end_of_statement (parser);
22934 /* Parse a list of Objective-C protocol references.
22936 objc-protocol-refs-opt:
22937 objc-protocol-refs [opt]
22939 objc-protocol-refs:
22940 < objc-identifier-list >
22942 Returns a TREE_LIST of identifiers, if any. */
22945 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22947 tree protorefs = NULL_TREE;
22949 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22951 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
22952 protorefs = cp_parser_objc_identifier_list (parser);
22953 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22959 /* Parse a Objective-C visibility specification. */
22962 cp_parser_objc_visibility_spec (cp_parser* parser)
22964 cp_token *vis = cp_lexer_peek_token (parser->lexer);
22966 switch (vis->keyword)
22968 case RID_AT_PRIVATE:
22969 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22971 case RID_AT_PROTECTED:
22972 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22974 case RID_AT_PUBLIC:
22975 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22977 case RID_AT_PACKAGE:
22978 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22984 /* Eat '@private'/'@protected'/'@public'. */
22985 cp_lexer_consume_token (parser->lexer);
22988 /* Parse an Objective-C method type. Return 'true' if it is a class
22989 (+) method, and 'false' if it is an instance (-) method. */
22992 cp_parser_objc_method_type (cp_parser* parser)
22994 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23000 /* Parse an Objective-C protocol qualifier. */
23003 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23005 tree quals = NULL_TREE, node;
23006 cp_token *token = cp_lexer_peek_token (parser->lexer);
23008 node = token->u.value;
23010 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23011 && (node == ridpointers [(int) RID_IN]
23012 || node == ridpointers [(int) RID_OUT]
23013 || node == ridpointers [(int) RID_INOUT]
23014 || node == ridpointers [(int) RID_BYCOPY]
23015 || node == ridpointers [(int) RID_BYREF]
23016 || node == ridpointers [(int) RID_ONEWAY]))
23018 quals = tree_cons (NULL_TREE, node, quals);
23019 cp_lexer_consume_token (parser->lexer);
23020 token = cp_lexer_peek_token (parser->lexer);
23021 node = token->u.value;
23027 /* Parse an Objective-C typename. */
23030 cp_parser_objc_typename (cp_parser* parser)
23032 tree type_name = NULL_TREE;
23034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23036 tree proto_quals, cp_type = NULL_TREE;
23038 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23039 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23041 /* An ObjC type name may consist of just protocol qualifiers, in which
23042 case the type shall default to 'id'. */
23043 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23045 cp_type = cp_parser_type_id (parser);
23047 /* If the type could not be parsed, an error has already
23048 been produced. For error recovery, behave as if it had
23049 not been specified, which will use the default type
23051 if (cp_type == error_mark_node)
23053 cp_type = NULL_TREE;
23054 /* We need to skip to the closing parenthesis as
23055 cp_parser_type_id() does not seem to do it for
23057 cp_parser_skip_to_closing_parenthesis (parser,
23058 /*recovering=*/true,
23059 /*or_comma=*/false,
23060 /*consume_paren=*/false);
23064 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23065 type_name = build_tree_list (proto_quals, cp_type);
23071 /* Check to see if TYPE refers to an Objective-C selector name. */
23074 cp_parser_objc_selector_p (enum cpp_ttype type)
23076 return (type == CPP_NAME || type == CPP_KEYWORD
23077 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23078 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23079 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23080 || type == CPP_XOR || type == CPP_XOR_EQ);
23083 /* Parse an Objective-C selector. */
23086 cp_parser_objc_selector (cp_parser* parser)
23088 cp_token *token = cp_lexer_consume_token (parser->lexer);
23090 if (!cp_parser_objc_selector_p (token->type))
23092 error_at (token->location, "invalid Objective-C++ selector name");
23093 return error_mark_node;
23096 /* C++ operator names are allowed to appear in ObjC selectors. */
23097 switch (token->type)
23099 case CPP_AND_AND: return get_identifier ("and");
23100 case CPP_AND_EQ: return get_identifier ("and_eq");
23101 case CPP_AND: return get_identifier ("bitand");
23102 case CPP_OR: return get_identifier ("bitor");
23103 case CPP_COMPL: return get_identifier ("compl");
23104 case CPP_NOT: return get_identifier ("not");
23105 case CPP_NOT_EQ: return get_identifier ("not_eq");
23106 case CPP_OR_OR: return get_identifier ("or");
23107 case CPP_OR_EQ: return get_identifier ("or_eq");
23108 case CPP_XOR: return get_identifier ("xor");
23109 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23110 default: return token->u.value;
23114 /* Parse an Objective-C params list. */
23117 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23119 tree params = NULL_TREE;
23120 bool maybe_unary_selector_p = true;
23121 cp_token *token = cp_lexer_peek_token (parser->lexer);
23123 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23125 tree selector = NULL_TREE, type_name, identifier;
23126 tree parm_attr = NULL_TREE;
23128 if (token->keyword == RID_ATTRIBUTE)
23131 if (token->type != CPP_COLON)
23132 selector = cp_parser_objc_selector (parser);
23134 /* Detect if we have a unary selector. */
23135 if (maybe_unary_selector_p
23136 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23138 params = selector; /* Might be followed by attributes. */
23142 maybe_unary_selector_p = false;
23143 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23145 /* Something went quite wrong. There should be a colon
23146 here, but there is not. Stop parsing parameters. */
23149 type_name = cp_parser_objc_typename (parser);
23150 /* New ObjC allows attributes on parameters too. */
23151 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23152 parm_attr = cp_parser_attributes_opt (parser);
23153 identifier = cp_parser_identifier (parser);
23157 objc_build_keyword_decl (selector,
23162 token = cp_lexer_peek_token (parser->lexer);
23165 if (params == NULL_TREE)
23167 cp_parser_error (parser, "objective-c++ method declaration is expected");
23168 return error_mark_node;
23171 /* We allow tail attributes for the method. */
23172 if (token->keyword == RID_ATTRIBUTE)
23174 *attributes = cp_parser_attributes_opt (parser);
23175 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23176 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23178 cp_parser_error (parser,
23179 "method attributes must be specified at the end");
23180 return error_mark_node;
23183 if (params == NULL_TREE)
23185 cp_parser_error (parser, "objective-c++ method declaration is expected");
23186 return error_mark_node;
23191 /* Parse the non-keyword Objective-C params. */
23194 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23197 tree params = make_node (TREE_LIST);
23198 cp_token *token = cp_lexer_peek_token (parser->lexer);
23199 *ellipsisp = false; /* Initially, assume no ellipsis. */
23201 while (token->type == CPP_COMMA)
23203 cp_parameter_declarator *parmdecl;
23206 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23207 token = cp_lexer_peek_token (parser->lexer);
23209 if (token->type == CPP_ELLIPSIS)
23211 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23213 token = cp_lexer_peek_token (parser->lexer);
23217 /* TODO: parse attributes for tail parameters. */
23218 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23219 parm = grokdeclarator (parmdecl->declarator,
23220 &parmdecl->decl_specifiers,
23221 PARM, /*initialized=*/0,
23222 /*attrlist=*/NULL);
23224 chainon (params, build_tree_list (NULL_TREE, parm));
23225 token = cp_lexer_peek_token (parser->lexer);
23228 /* We allow tail attributes for the method. */
23229 if (token->keyword == RID_ATTRIBUTE)
23231 if (*attributes == NULL_TREE)
23233 *attributes = cp_parser_attributes_opt (parser);
23234 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23235 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23239 /* We have an error, but parse the attributes, so that we can
23241 *attributes = cp_parser_attributes_opt (parser);
23243 cp_parser_error (parser,
23244 "method attributes must be specified at the end");
23245 return error_mark_node;
23251 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23254 cp_parser_objc_interstitial_code (cp_parser* parser)
23256 cp_token *token = cp_lexer_peek_token (parser->lexer);
23258 /* If the next token is `extern' and the following token is a string
23259 literal, then we have a linkage specification. */
23260 if (token->keyword == RID_EXTERN
23261 && cp_parser_is_pure_string_literal
23262 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23263 cp_parser_linkage_specification (parser);
23264 /* Handle #pragma, if any. */
23265 else if (token->type == CPP_PRAGMA)
23266 cp_parser_pragma (parser, pragma_external);
23267 /* Allow stray semicolons. */
23268 else if (token->type == CPP_SEMICOLON)
23269 cp_lexer_consume_token (parser->lexer);
23270 /* Mark methods as optional or required, when building protocols. */
23271 else if (token->keyword == RID_AT_OPTIONAL)
23273 cp_lexer_consume_token (parser->lexer);
23274 objc_set_method_opt (true);
23276 else if (token->keyword == RID_AT_REQUIRED)
23278 cp_lexer_consume_token (parser->lexer);
23279 objc_set_method_opt (false);
23281 else if (token->keyword == RID_NAMESPACE)
23282 cp_parser_namespace_definition (parser);
23283 /* Other stray characters must generate errors. */
23284 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23286 cp_lexer_consume_token (parser->lexer);
23287 error ("stray %qs between Objective-C++ methods",
23288 token->type == CPP_OPEN_BRACE ? "{" : "}");
23290 /* Finally, try to parse a block-declaration, or a function-definition. */
23292 cp_parser_block_declaration (parser, /*statement_p=*/false);
23295 /* Parse a method signature. */
23298 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23300 tree rettype, kwdparms, optparms;
23301 bool ellipsis = false;
23302 bool is_class_method;
23304 is_class_method = cp_parser_objc_method_type (parser);
23305 rettype = cp_parser_objc_typename (parser);
23306 *attributes = NULL_TREE;
23307 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23308 if (kwdparms == error_mark_node)
23309 return error_mark_node;
23310 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23311 if (optparms == error_mark_node)
23312 return error_mark_node;
23314 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23318 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23321 cp_lexer_save_tokens (parser->lexer);
23322 tattr = cp_parser_attributes_opt (parser);
23323 gcc_assert (tattr) ;
23325 /* If the attributes are followed by a method introducer, this is not allowed.
23326 Dump the attributes and flag the situation. */
23327 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23328 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23331 /* Otherwise, the attributes introduce some interstitial code, possibly so
23332 rewind to allow that check. */
23333 cp_lexer_rollback_tokens (parser->lexer);
23337 /* Parse an Objective-C method prototype list. */
23340 cp_parser_objc_method_prototype_list (cp_parser* parser)
23342 cp_token *token = cp_lexer_peek_token (parser->lexer);
23344 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23346 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23348 tree attributes, sig;
23349 bool is_class_method;
23350 if (token->type == CPP_PLUS)
23351 is_class_method = true;
23353 is_class_method = false;
23354 sig = cp_parser_objc_method_signature (parser, &attributes);
23355 if (sig == error_mark_node)
23357 cp_parser_skip_to_end_of_block_or_statement (parser);
23358 token = cp_lexer_peek_token (parser->lexer);
23361 objc_add_method_declaration (is_class_method, sig, attributes);
23362 cp_parser_consume_semicolon_at_end_of_statement (parser);
23364 else if (token->keyword == RID_AT_PROPERTY)
23365 cp_parser_objc_at_property_declaration (parser);
23366 else if (token->keyword == RID_ATTRIBUTE
23367 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23368 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23370 "prefix attributes are ignored for methods");
23372 /* Allow for interspersed non-ObjC++ code. */
23373 cp_parser_objc_interstitial_code (parser);
23375 token = cp_lexer_peek_token (parser->lexer);
23378 if (token->type != CPP_EOF)
23379 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23381 cp_parser_error (parser, "expected %<@end%>");
23383 objc_finish_interface ();
23386 /* Parse an Objective-C method definition list. */
23389 cp_parser_objc_method_definition_list (cp_parser* parser)
23391 cp_token *token = cp_lexer_peek_token (parser->lexer);
23393 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23397 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23400 tree sig, attribute;
23401 bool is_class_method;
23402 if (token->type == CPP_PLUS)
23403 is_class_method = true;
23405 is_class_method = false;
23406 push_deferring_access_checks (dk_deferred);
23407 sig = cp_parser_objc_method_signature (parser, &attribute);
23408 if (sig == error_mark_node)
23410 cp_parser_skip_to_end_of_block_or_statement (parser);
23411 token = cp_lexer_peek_token (parser->lexer);
23414 objc_start_method_definition (is_class_method, sig, attribute,
23417 /* For historical reasons, we accept an optional semicolon. */
23418 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23419 cp_lexer_consume_token (parser->lexer);
23421 ptk = cp_lexer_peek_token (parser->lexer);
23422 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23423 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23425 perform_deferred_access_checks ();
23426 stop_deferring_access_checks ();
23427 meth = cp_parser_function_definition_after_declarator (parser,
23429 pop_deferring_access_checks ();
23430 objc_finish_method_definition (meth);
23433 /* The following case will be removed once @synthesize is
23434 completely implemented. */
23435 else if (token->keyword == RID_AT_PROPERTY)
23436 cp_parser_objc_at_property_declaration (parser);
23437 else if (token->keyword == RID_AT_SYNTHESIZE)
23438 cp_parser_objc_at_synthesize_declaration (parser);
23439 else if (token->keyword == RID_AT_DYNAMIC)
23440 cp_parser_objc_at_dynamic_declaration (parser);
23441 else if (token->keyword == RID_ATTRIBUTE
23442 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23443 warning_at (token->location, OPT_Wattributes,
23444 "prefix attributes are ignored for methods");
23446 /* Allow for interspersed non-ObjC++ code. */
23447 cp_parser_objc_interstitial_code (parser);
23449 token = cp_lexer_peek_token (parser->lexer);
23452 if (token->type != CPP_EOF)
23453 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23455 cp_parser_error (parser, "expected %<@end%>");
23457 objc_finish_implementation ();
23460 /* Parse Objective-C ivars. */
23463 cp_parser_objc_class_ivars (cp_parser* parser)
23465 cp_token *token = cp_lexer_peek_token (parser->lexer);
23467 if (token->type != CPP_OPEN_BRACE)
23468 return; /* No ivars specified. */
23470 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23471 token = cp_lexer_peek_token (parser->lexer);
23473 while (token->type != CPP_CLOSE_BRACE
23474 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23476 cp_decl_specifier_seq declspecs;
23477 int decl_class_or_enum_p;
23478 tree prefix_attributes;
23480 cp_parser_objc_visibility_spec (parser);
23482 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23485 cp_parser_decl_specifier_seq (parser,
23486 CP_PARSER_FLAGS_OPTIONAL,
23488 &decl_class_or_enum_p);
23490 /* auto, register, static, extern, mutable. */
23491 if (declspecs.storage_class != sc_none)
23493 cp_parser_error (parser, "invalid type for instance variable");
23494 declspecs.storage_class = sc_none;
23498 if (declspecs.specs[(int) ds_thread])
23500 cp_parser_error (parser, "invalid type for instance variable");
23501 declspecs.specs[(int) ds_thread] = 0;
23505 if (declspecs.specs[(int) ds_typedef])
23507 cp_parser_error (parser, "invalid type for instance variable");
23508 declspecs.specs[(int) ds_typedef] = 0;
23511 prefix_attributes = declspecs.attributes;
23512 declspecs.attributes = NULL_TREE;
23514 /* Keep going until we hit the `;' at the end of the
23516 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23518 tree width = NULL_TREE, attributes, first_attribute, decl;
23519 cp_declarator *declarator = NULL;
23520 int ctor_dtor_or_conv_p;
23522 /* Check for a (possibly unnamed) bitfield declaration. */
23523 token = cp_lexer_peek_token (parser->lexer);
23524 if (token->type == CPP_COLON)
23527 if (token->type == CPP_NAME
23528 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23531 /* Get the name of the bitfield. */
23532 declarator = make_id_declarator (NULL_TREE,
23533 cp_parser_identifier (parser),
23537 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23538 /* Get the width of the bitfield. */
23540 = cp_parser_constant_expression (parser,
23541 /*allow_non_constant=*/false,
23546 /* Parse the declarator. */
23548 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23549 &ctor_dtor_or_conv_p,
23550 /*parenthesized_p=*/NULL,
23551 /*member_p=*/false);
23554 /* Look for attributes that apply to the ivar. */
23555 attributes = cp_parser_attributes_opt (parser);
23556 /* Remember which attributes are prefix attributes and
23558 first_attribute = attributes;
23559 /* Combine the attributes. */
23560 attributes = chainon (prefix_attributes, attributes);
23563 /* Create the bitfield declaration. */
23564 decl = grokbitfield (declarator, &declspecs,
23568 decl = grokfield (declarator, &declspecs,
23569 NULL_TREE, /*init_const_expr_p=*/false,
23570 NULL_TREE, attributes);
23572 /* Add the instance variable. */
23573 if (decl != error_mark_node && decl != NULL_TREE)
23574 objc_add_instance_variable (decl);
23576 /* Reset PREFIX_ATTRIBUTES. */
23577 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23578 attributes = TREE_CHAIN (attributes);
23580 TREE_CHAIN (attributes) = NULL_TREE;
23582 token = cp_lexer_peek_token (parser->lexer);
23584 if (token->type == CPP_COMMA)
23586 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23592 cp_parser_consume_semicolon_at_end_of_statement (parser);
23593 token = cp_lexer_peek_token (parser->lexer);
23596 if (token->keyword == RID_AT_END)
23597 cp_parser_error (parser, "expected %<}%>");
23599 /* Do not consume the RID_AT_END, so it will be read again as terminating
23600 the @interface of @implementation. */
23601 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23602 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23604 /* For historical reasons, we accept an optional semicolon. */
23605 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23606 cp_lexer_consume_token (parser->lexer);
23609 /* Parse an Objective-C protocol declaration. */
23612 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23614 tree proto, protorefs;
23617 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23620 tok = cp_lexer_peek_token (parser->lexer);
23621 error_at (tok->location, "identifier expected after %<@protocol%>");
23622 cp_parser_consume_semicolon_at_end_of_statement (parser);
23626 /* See if we have a forward declaration or a definition. */
23627 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23629 /* Try a forward declaration first. */
23630 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23636 id = cp_parser_identifier (parser);
23637 if (id == error_mark_node)
23640 objc_declare_protocol (id, attributes);
23642 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23643 cp_lexer_consume_token (parser->lexer);
23647 cp_parser_consume_semicolon_at_end_of_statement (parser);
23650 /* Ok, we got a full-fledged definition (or at least should). */
23653 proto = cp_parser_identifier (parser);
23654 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23655 objc_start_protocol (proto, protorefs, attributes);
23656 cp_parser_objc_method_prototype_list (parser);
23660 /* Parse an Objective-C superclass or category. */
23663 cp_parser_objc_superclass_or_category (cp_parser *parser,
23666 tree *categ, bool *is_class_extension)
23668 cp_token *next = cp_lexer_peek_token (parser->lexer);
23670 *super = *categ = NULL_TREE;
23671 *is_class_extension = false;
23672 if (next->type == CPP_COLON)
23674 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23675 *super = cp_parser_identifier (parser);
23677 else if (next->type == CPP_OPEN_PAREN)
23679 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23681 /* If there is no category name, and this is an @interface, we
23682 have a class extension. */
23683 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23685 *categ = NULL_TREE;
23686 *is_class_extension = true;
23689 *categ = cp_parser_identifier (parser);
23691 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23695 /* Parse an Objective-C class interface. */
23698 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23700 tree name, super, categ, protos;
23701 bool is_class_extension;
23703 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23704 name = cp_parser_identifier (parser);
23705 if (name == error_mark_node)
23707 /* It's hard to recover because even if valid @interface stuff
23708 is to follow, we can't compile it (or validate it) if we
23709 don't even know which class it refers to. Let's assume this
23710 was a stray '@interface' token in the stream and skip it.
23714 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23715 &is_class_extension);
23716 protos = cp_parser_objc_protocol_refs_opt (parser);
23718 /* We have either a class or a category on our hands. */
23719 if (categ || is_class_extension)
23720 objc_start_category_interface (name, categ, protos, attributes);
23723 objc_start_class_interface (name, super, protos, attributes);
23724 /* Handle instance variable declarations, if any. */
23725 cp_parser_objc_class_ivars (parser);
23726 objc_continue_interface ();
23729 cp_parser_objc_method_prototype_list (parser);
23732 /* Parse an Objective-C class implementation. */
23735 cp_parser_objc_class_implementation (cp_parser* parser)
23737 tree name, super, categ;
23738 bool is_class_extension;
23740 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23741 name = cp_parser_identifier (parser);
23742 if (name == error_mark_node)
23744 /* It's hard to recover because even if valid @implementation
23745 stuff is to follow, we can't compile it (or validate it) if
23746 we don't even know which class it refers to. Let's assume
23747 this was a stray '@implementation' token in the stream and
23752 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23753 &is_class_extension);
23755 /* We have either a class or a category on our hands. */
23757 objc_start_category_implementation (name, categ);
23760 objc_start_class_implementation (name, super);
23761 /* Handle instance variable declarations, if any. */
23762 cp_parser_objc_class_ivars (parser);
23763 objc_continue_implementation ();
23766 cp_parser_objc_method_definition_list (parser);
23769 /* Consume the @end token and finish off the implementation. */
23772 cp_parser_objc_end_implementation (cp_parser* parser)
23774 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23775 objc_finish_implementation ();
23778 /* Parse an Objective-C declaration. */
23781 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23783 /* Try to figure out what kind of declaration is present. */
23784 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23787 switch (kwd->keyword)
23792 error_at (kwd->location, "attributes may not be specified before"
23793 " the %<@%D%> Objective-C++ keyword",
23797 case RID_AT_IMPLEMENTATION:
23798 warning_at (kwd->location, OPT_Wattributes,
23799 "prefix attributes are ignored before %<@%D%>",
23806 switch (kwd->keyword)
23809 cp_parser_objc_alias_declaration (parser);
23812 cp_parser_objc_class_declaration (parser);
23814 case RID_AT_PROTOCOL:
23815 cp_parser_objc_protocol_declaration (parser, attributes);
23817 case RID_AT_INTERFACE:
23818 cp_parser_objc_class_interface (parser, attributes);
23820 case RID_AT_IMPLEMENTATION:
23821 cp_parser_objc_class_implementation (parser);
23824 cp_parser_objc_end_implementation (parser);
23827 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23829 cp_parser_skip_to_end_of_block_or_statement (parser);
23833 /* Parse an Objective-C try-catch-finally statement.
23835 objc-try-catch-finally-stmt:
23836 @try compound-statement objc-catch-clause-seq [opt]
23837 objc-finally-clause [opt]
23839 objc-catch-clause-seq:
23840 objc-catch-clause objc-catch-clause-seq [opt]
23843 @catch ( objc-exception-declaration ) compound-statement
23845 objc-finally-clause:
23846 @finally compound-statement
23848 objc-exception-declaration:
23849 parameter-declaration
23852 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23856 PS: This function is identical to c_parser_objc_try_catch_finally_statement
23857 for C. Keep them in sync. */
23860 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23862 location_t location;
23865 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23866 location = cp_lexer_peek_token (parser->lexer)->location;
23867 objc_maybe_warn_exceptions (location);
23868 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23869 node, lest it get absorbed into the surrounding block. */
23870 stmt = push_stmt_list ();
23871 cp_parser_compound_statement (parser, NULL, false, false);
23872 objc_begin_try_stmt (location, pop_stmt_list (stmt));
23874 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23876 cp_parameter_declarator *parm;
23877 tree parameter_declaration = error_mark_node;
23878 bool seen_open_paren = false;
23880 cp_lexer_consume_token (parser->lexer);
23881 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23882 seen_open_paren = true;
23883 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23885 /* We have "@catch (...)" (where the '...' are literally
23886 what is in the code). Skip the '...'.
23887 parameter_declaration is set to NULL_TREE, and
23888 objc_being_catch_clauses() knows that that means
23890 cp_lexer_consume_token (parser->lexer);
23891 parameter_declaration = NULL_TREE;
23895 /* We have "@catch (NSException *exception)" or something
23896 like that. Parse the parameter declaration. */
23897 parm = cp_parser_parameter_declaration (parser, false, NULL);
23899 parameter_declaration = error_mark_node;
23901 parameter_declaration = grokdeclarator (parm->declarator,
23902 &parm->decl_specifiers,
23903 PARM, /*initialized=*/0,
23904 /*attrlist=*/NULL);
23906 if (seen_open_paren)
23907 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23910 /* If there was no open parenthesis, we are recovering from
23911 an error, and we are trying to figure out what mistake
23912 the user has made. */
23914 /* If there is an immediate closing parenthesis, the user
23915 probably forgot the opening one (ie, they typed "@catch
23916 NSException *e)". Parse the closing parenthesis and keep
23918 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23919 cp_lexer_consume_token (parser->lexer);
23921 /* If these is no immediate closing parenthesis, the user
23922 probably doesn't know that parenthesis are required at
23923 all (ie, they typed "@catch NSException *e"). So, just
23924 forget about the closing parenthesis and keep going. */
23926 objc_begin_catch_clause (parameter_declaration);
23927 cp_parser_compound_statement (parser, NULL, false, false);
23928 objc_finish_catch_clause ();
23930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23932 cp_lexer_consume_token (parser->lexer);
23933 location = cp_lexer_peek_token (parser->lexer)->location;
23934 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23935 node, lest it get absorbed into the surrounding block. */
23936 stmt = push_stmt_list ();
23937 cp_parser_compound_statement (parser, NULL, false, false);
23938 objc_build_finally_clause (location, pop_stmt_list (stmt));
23941 return objc_finish_try_stmt ();
23944 /* Parse an Objective-C synchronized statement.
23946 objc-synchronized-stmt:
23947 @synchronized ( expression ) compound-statement
23949 Returns NULL_TREE. */
23952 cp_parser_objc_synchronized_statement (cp_parser *parser)
23954 location_t location;
23957 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23959 location = cp_lexer_peek_token (parser->lexer)->location;
23960 objc_maybe_warn_exceptions (location);
23961 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23962 lock = cp_parser_expression (parser, false, NULL);
23963 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23965 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23966 node, lest it get absorbed into the surrounding block. */
23967 stmt = push_stmt_list ();
23968 cp_parser_compound_statement (parser, NULL, false, false);
23970 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23973 /* Parse an Objective-C throw statement.
23976 @throw assignment-expression [opt] ;
23978 Returns a constructed '@throw' statement. */
23981 cp_parser_objc_throw_statement (cp_parser *parser)
23983 tree expr = NULL_TREE;
23984 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23986 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23989 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23991 cp_parser_consume_semicolon_at_end_of_statement (parser);
23993 return objc_build_throw_stmt (loc, expr);
23996 /* Parse an Objective-C statement. */
23999 cp_parser_objc_statement (cp_parser * parser)
24001 /* Try to figure out what kind of declaration is present. */
24002 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24004 switch (kwd->keyword)
24007 return cp_parser_objc_try_catch_finally_statement (parser);
24008 case RID_AT_SYNCHRONIZED:
24009 return cp_parser_objc_synchronized_statement (parser);
24011 return cp_parser_objc_throw_statement (parser);
24013 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24015 cp_parser_skip_to_end_of_block_or_statement (parser);
24018 return error_mark_node;
24021 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24022 look ahead to see if an objc keyword follows the attributes. This
24023 is to detect the use of prefix attributes on ObjC @interface and
24027 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24029 cp_lexer_save_tokens (parser->lexer);
24030 *attrib = cp_parser_attributes_opt (parser);
24031 gcc_assert (*attrib);
24032 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24034 cp_lexer_commit_tokens (parser->lexer);
24037 cp_lexer_rollback_tokens (parser->lexer);
24041 /* This routine is a minimal replacement for
24042 c_parser_struct_declaration () used when parsing the list of
24043 types/names or ObjC++ properties. For example, when parsing the
24046 @property (readonly) int a, b, c;
24048 this function is responsible for parsing "int a, int b, int c" and
24049 returning the declarations as CHAIN of DECLs.
24051 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24052 similar parsing. */
24054 cp_parser_objc_struct_declaration (cp_parser *parser)
24056 tree decls = NULL_TREE;
24057 cp_decl_specifier_seq declspecs;
24058 int decl_class_or_enum_p;
24059 tree prefix_attributes;
24061 cp_parser_decl_specifier_seq (parser,
24062 CP_PARSER_FLAGS_NONE,
24064 &decl_class_or_enum_p);
24066 if (declspecs.type == error_mark_node)
24067 return error_mark_node;
24069 /* auto, register, static, extern, mutable. */
24070 if (declspecs.storage_class != sc_none)
24072 cp_parser_error (parser, "invalid type for property");
24073 declspecs.storage_class = sc_none;
24077 if (declspecs.specs[(int) ds_thread])
24079 cp_parser_error (parser, "invalid type for property");
24080 declspecs.specs[(int) ds_thread] = 0;
24084 if (declspecs.specs[(int) ds_typedef])
24086 cp_parser_error (parser, "invalid type for property");
24087 declspecs.specs[(int) ds_typedef] = 0;
24090 prefix_attributes = declspecs.attributes;
24091 declspecs.attributes = NULL_TREE;
24093 /* Keep going until we hit the `;' at the end of the declaration. */
24094 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24096 tree attributes, first_attribute, decl;
24097 cp_declarator *declarator;
24100 /* Parse the declarator. */
24101 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24102 NULL, NULL, false);
24104 /* Look for attributes that apply to the ivar. */
24105 attributes = cp_parser_attributes_opt (parser);
24106 /* Remember which attributes are prefix attributes and
24108 first_attribute = attributes;
24109 /* Combine the attributes. */
24110 attributes = chainon (prefix_attributes, attributes);
24112 decl = grokfield (declarator, &declspecs,
24113 NULL_TREE, /*init_const_expr_p=*/false,
24114 NULL_TREE, attributes);
24116 if (decl == error_mark_node || decl == NULL_TREE)
24117 return error_mark_node;
24119 /* Reset PREFIX_ATTRIBUTES. */
24120 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24121 attributes = TREE_CHAIN (attributes);
24123 TREE_CHAIN (attributes) = NULL_TREE;
24125 DECL_CHAIN (decl) = decls;
24128 token = cp_lexer_peek_token (parser->lexer);
24129 if (token->type == CPP_COMMA)
24131 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24140 /* Parse an Objective-C @property declaration. The syntax is:
24142 objc-property-declaration:
24143 '@property' objc-property-attributes[opt] struct-declaration ;
24145 objc-property-attributes:
24146 '(' objc-property-attribute-list ')'
24148 objc-property-attribute-list:
24149 objc-property-attribute
24150 objc-property-attribute-list, objc-property-attribute
24152 objc-property-attribute
24153 'getter' = identifier
24154 'setter' = identifier
24163 @property NSString *name;
24164 @property (readonly) id object;
24165 @property (retain, nonatomic, getter=getTheName) id name;
24166 @property int a, b, c;
24168 PS: This function is identical to
24169 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24171 cp_parser_objc_at_property_declaration (cp_parser *parser)
24173 /* The following variables hold the attributes of the properties as
24174 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24175 seen. When we see an attribute, we set them to 'true' (if they
24176 are boolean properties) or to the identifier (if they have an
24177 argument, ie, for getter and setter). Note that here we only
24178 parse the list of attributes, check the syntax and accumulate the
24179 attributes that we find. objc_add_property_declaration() will
24180 then process the information. */
24181 bool property_assign = false;
24182 bool property_copy = false;
24183 tree property_getter_ident = NULL_TREE;
24184 bool property_nonatomic = false;
24185 bool property_readonly = false;
24186 bool property_readwrite = false;
24187 bool property_retain = false;
24188 tree property_setter_ident = NULL_TREE;
24190 /* 'properties' is the list of properties that we read. Usually a
24191 single one, but maybe more (eg, in "@property int a, b, c;" there
24196 loc = cp_lexer_peek_token (parser->lexer)->location;
24198 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24200 /* Parse the optional attribute list... */
24201 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24204 cp_lexer_consume_token (parser->lexer);
24208 bool syntax_error = false;
24209 cp_token *token = cp_lexer_peek_token (parser->lexer);
24212 if (token->type != CPP_NAME)
24214 cp_parser_error (parser, "expected identifier");
24217 keyword = C_RID_CODE (token->u.value);
24218 cp_lexer_consume_token (parser->lexer);
24221 case RID_ASSIGN: property_assign = true; break;
24222 case RID_COPY: property_copy = true; break;
24223 case RID_NONATOMIC: property_nonatomic = true; break;
24224 case RID_READONLY: property_readonly = true; break;
24225 case RID_READWRITE: property_readwrite = true; break;
24226 case RID_RETAIN: property_retain = true; break;
24230 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24232 if (keyword == RID_GETTER)
24233 cp_parser_error (parser,
24234 "missing %<=%> (after %<getter%> attribute)");
24236 cp_parser_error (parser,
24237 "missing %<=%> (after %<setter%> attribute)");
24238 syntax_error = true;
24241 cp_lexer_consume_token (parser->lexer); /* eat the = */
24242 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24244 cp_parser_error (parser, "expected identifier");
24245 syntax_error = true;
24248 if (keyword == RID_SETTER)
24250 if (property_setter_ident != NULL_TREE)
24252 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24253 cp_lexer_consume_token (parser->lexer);
24256 property_setter_ident = cp_parser_objc_selector (parser);
24257 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24258 cp_parser_error (parser, "setter name must terminate with %<:%>");
24260 cp_lexer_consume_token (parser->lexer);
24264 if (property_getter_ident != NULL_TREE)
24266 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24267 cp_lexer_consume_token (parser->lexer);
24270 property_getter_ident = cp_parser_objc_selector (parser);
24274 cp_parser_error (parser, "unknown property attribute");
24275 syntax_error = true;
24282 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24283 cp_lexer_consume_token (parser->lexer);
24288 /* FIXME: "@property (setter, assign);" will generate a spurious
24289 "error: expected ‘)’ before ‘,’ token". This is because
24290 cp_parser_require, unlike the C counterpart, will produce an
24291 error even if we are in error recovery. */
24292 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24294 cp_parser_skip_to_closing_parenthesis (parser,
24295 /*recovering=*/true,
24296 /*or_comma=*/false,
24297 /*consume_paren=*/true);
24301 /* ... and the property declaration(s). */
24302 properties = cp_parser_objc_struct_declaration (parser);
24304 if (properties == error_mark_node)
24306 cp_parser_skip_to_end_of_statement (parser);
24307 /* If the next token is now a `;', consume it. */
24308 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24309 cp_lexer_consume_token (parser->lexer);
24313 if (properties == NULL_TREE)
24314 cp_parser_error (parser, "expected identifier");
24317 /* Comma-separated properties are chained together in
24318 reverse order; add them one by one. */
24319 properties = nreverse (properties);
24321 for (; properties; properties = TREE_CHAIN (properties))
24322 objc_add_property_declaration (loc, copy_node (properties),
24323 property_readonly, property_readwrite,
24324 property_assign, property_retain,
24325 property_copy, property_nonatomic,
24326 property_getter_ident, property_setter_ident);
24329 cp_parser_consume_semicolon_at_end_of_statement (parser);
24332 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24334 objc-synthesize-declaration:
24335 @synthesize objc-synthesize-identifier-list ;
24337 objc-synthesize-identifier-list:
24338 objc-synthesize-identifier
24339 objc-synthesize-identifier-list, objc-synthesize-identifier
24341 objc-synthesize-identifier
24343 identifier = identifier
24346 @synthesize MyProperty;
24347 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24349 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24350 for C. Keep them in sync.
24353 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24355 tree list = NULL_TREE;
24357 loc = cp_lexer_peek_token (parser->lexer)->location;
24359 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24362 tree property, ivar;
24363 property = cp_parser_identifier (parser);
24364 if (property == error_mark_node)
24366 cp_parser_consume_semicolon_at_end_of_statement (parser);
24369 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24371 cp_lexer_consume_token (parser->lexer);
24372 ivar = cp_parser_identifier (parser);
24373 if (ivar == error_mark_node)
24375 cp_parser_consume_semicolon_at_end_of_statement (parser);
24381 list = chainon (list, build_tree_list (ivar, property));
24382 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24383 cp_lexer_consume_token (parser->lexer);
24387 cp_parser_consume_semicolon_at_end_of_statement (parser);
24388 objc_add_synthesize_declaration (loc, list);
24391 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24393 objc-dynamic-declaration:
24394 @dynamic identifier-list ;
24397 @dynamic MyProperty;
24398 @dynamic MyProperty, AnotherProperty;
24400 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24401 for C. Keep them in sync.
24404 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24406 tree list = NULL_TREE;
24408 loc = cp_lexer_peek_token (parser->lexer)->location;
24410 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24414 property = cp_parser_identifier (parser);
24415 if (property == error_mark_node)
24417 cp_parser_consume_semicolon_at_end_of_statement (parser);
24420 list = chainon (list, build_tree_list (NULL, property));
24421 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24422 cp_lexer_consume_token (parser->lexer);
24426 cp_parser_consume_semicolon_at_end_of_statement (parser);
24427 objc_add_dynamic_declaration (loc, list);
24431 /* OpenMP 2.5 parsing routines. */
24433 /* Returns name of the next clause.
24434 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24435 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24436 returned and the token is consumed. */
24438 static pragma_omp_clause
24439 cp_parser_omp_clause_name (cp_parser *parser)
24441 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24443 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24444 result = PRAGMA_OMP_CLAUSE_IF;
24445 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24446 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24447 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24448 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24449 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24451 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24452 const char *p = IDENTIFIER_POINTER (id);
24457 if (!strcmp ("collapse", p))
24458 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24459 else if (!strcmp ("copyin", p))
24460 result = PRAGMA_OMP_CLAUSE_COPYIN;
24461 else if (!strcmp ("copyprivate", p))
24462 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24465 if (!strcmp ("final", p))
24466 result = PRAGMA_OMP_CLAUSE_FINAL;
24467 else if (!strcmp ("firstprivate", p))
24468 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24471 if (!strcmp ("lastprivate", p))
24472 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24475 if (!strcmp ("mergeable", p))
24476 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24479 if (!strcmp ("nowait", p))
24480 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24481 else if (!strcmp ("num_threads", p))
24482 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24485 if (!strcmp ("ordered", p))
24486 result = PRAGMA_OMP_CLAUSE_ORDERED;
24489 if (!strcmp ("reduction", p))
24490 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24493 if (!strcmp ("schedule", p))
24494 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24495 else if (!strcmp ("shared", p))
24496 result = PRAGMA_OMP_CLAUSE_SHARED;
24499 if (!strcmp ("untied", p))
24500 result = PRAGMA_OMP_CLAUSE_UNTIED;
24505 if (result != PRAGMA_OMP_CLAUSE_NONE)
24506 cp_lexer_consume_token (parser->lexer);
24511 /* Validate that a clause of the given type does not already exist. */
24514 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24515 const char *name, location_t location)
24519 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24520 if (OMP_CLAUSE_CODE (c) == code)
24522 error_at (location, "too many %qs clauses", name);
24530 variable-list , identifier
24532 In addition, we match a closing parenthesis. An opening parenthesis
24533 will have been consumed by the caller.
24535 If KIND is nonzero, create the appropriate node and install the decl
24536 in OMP_CLAUSE_DECL and add the node to the head of the list.
24538 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24539 return the list created. */
24542 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24550 token = cp_lexer_peek_token (parser->lexer);
24551 name = cp_parser_id_expression (parser, /*template_p=*/false,
24552 /*check_dependency_p=*/true,
24553 /*template_p=*/NULL,
24554 /*declarator_p=*/false,
24555 /*optional_p=*/false);
24556 if (name == error_mark_node)
24559 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24560 if (decl == error_mark_node)
24561 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24563 else if (kind != 0)
24565 tree u = build_omp_clause (token->location, kind);
24566 OMP_CLAUSE_DECL (u) = decl;
24567 OMP_CLAUSE_CHAIN (u) = list;
24571 list = tree_cons (decl, NULL_TREE, list);
24574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24576 cp_lexer_consume_token (parser->lexer);
24579 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24583 /* Try to resync to an unnested comma. Copied from
24584 cp_parser_parenthesized_expression_list. */
24586 ending = cp_parser_skip_to_closing_parenthesis (parser,
24587 /*recovering=*/true,
24589 /*consume_paren=*/true);
24597 /* Similarly, but expect leading and trailing parenthesis. This is a very
24598 common case for omp clauses. */
24601 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24603 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24604 return cp_parser_omp_var_list_no_open (parser, kind, list);
24609 collapse ( constant-expression ) */
24612 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24618 loc = cp_lexer_peek_token (parser->lexer)->location;
24619 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24622 num = cp_parser_constant_expression (parser, false, NULL);
24624 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24625 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24626 /*or_comma=*/false,
24627 /*consume_paren=*/true);
24629 if (num == error_mark_node)
24631 num = fold_non_dependent_expr (num);
24632 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24633 || !host_integerp (num, 0)
24634 || (n = tree_low_cst (num, 0)) <= 0
24637 error_at (loc, "collapse argument needs positive constant integer expression");
24641 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24642 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24643 OMP_CLAUSE_CHAIN (c) = list;
24644 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24650 default ( shared | none ) */
24653 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24655 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24658 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24660 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24662 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24663 const char *p = IDENTIFIER_POINTER (id);
24668 if (strcmp ("none", p) != 0)
24670 kind = OMP_CLAUSE_DEFAULT_NONE;
24674 if (strcmp ("shared", p) != 0)
24676 kind = OMP_CLAUSE_DEFAULT_SHARED;
24683 cp_lexer_consume_token (parser->lexer);
24688 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24691 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24692 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24693 /*or_comma=*/false,
24694 /*consume_paren=*/true);
24696 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24699 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24700 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24701 OMP_CLAUSE_CHAIN (c) = list;
24702 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24708 final ( expression ) */
24711 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24715 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24718 t = cp_parser_condition (parser);
24720 if (t == error_mark_node
24721 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24722 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24723 /*or_comma=*/false,
24724 /*consume_paren=*/true);
24726 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24728 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24729 OMP_CLAUSE_FINAL_EXPR (c) = t;
24730 OMP_CLAUSE_CHAIN (c) = list;
24736 if ( expression ) */
24739 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24743 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24746 t = cp_parser_condition (parser);
24748 if (t == error_mark_node
24749 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24750 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24751 /*or_comma=*/false,
24752 /*consume_paren=*/true);
24754 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24756 c = build_omp_clause (location, OMP_CLAUSE_IF);
24757 OMP_CLAUSE_IF_EXPR (c) = t;
24758 OMP_CLAUSE_CHAIN (c) = list;
24767 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24768 tree list, location_t location)
24772 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24775 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24776 OMP_CLAUSE_CHAIN (c) = list;
24784 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24785 tree list, location_t location)
24789 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24791 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24792 OMP_CLAUSE_CHAIN (c) = list;
24797 num_threads ( expression ) */
24800 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24801 location_t location)
24805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24808 t = cp_parser_expression (parser, false, NULL);
24810 if (t == error_mark_node
24811 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24812 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24813 /*or_comma=*/false,
24814 /*consume_paren=*/true);
24816 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24817 "num_threads", location);
24819 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24820 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24821 OMP_CLAUSE_CHAIN (c) = list;
24830 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24831 tree list, location_t location)
24835 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24836 "ordered", location);
24838 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24839 OMP_CLAUSE_CHAIN (c) = list;
24844 reduction ( reduction-operator : variable-list )
24846 reduction-operator:
24847 One of: + * - & ^ | && ||
24851 reduction-operator:
24852 One of: + * - & ^ | && || min max */
24855 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24857 enum tree_code code;
24860 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24863 switch (cp_lexer_peek_token (parser->lexer)->type)
24875 code = BIT_AND_EXPR;
24878 code = BIT_XOR_EXPR;
24881 code = BIT_IOR_EXPR;
24884 code = TRUTH_ANDIF_EXPR;
24887 code = TRUTH_ORIF_EXPR;
24891 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24892 const char *p = IDENTIFIER_POINTER (id);
24894 if (strcmp (p, "min") == 0)
24899 if (strcmp (p, "max") == 0)
24907 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24908 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24910 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24911 /*or_comma=*/false,
24912 /*consume_paren=*/true);
24915 cp_lexer_consume_token (parser->lexer);
24917 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24920 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24921 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24922 OMP_CLAUSE_REDUCTION_CODE (c) = code;
24928 schedule ( schedule-kind )
24929 schedule ( schedule-kind , expression )
24932 static | dynamic | guided | runtime | auto */
24935 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24939 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24942 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24944 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24946 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24947 const char *p = IDENTIFIER_POINTER (id);
24952 if (strcmp ("dynamic", p) != 0)
24954 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24958 if (strcmp ("guided", p) != 0)
24960 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24964 if (strcmp ("runtime", p) != 0)
24966 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24973 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24974 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24975 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24976 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24979 cp_lexer_consume_token (parser->lexer);
24981 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24984 cp_lexer_consume_token (parser->lexer);
24986 token = cp_lexer_peek_token (parser->lexer);
24987 t = cp_parser_assignment_expression (parser, false, NULL);
24989 if (t == error_mark_node)
24991 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24992 error_at (token->location, "schedule %<runtime%> does not take "
24993 "a %<chunk_size%> parameter");
24994 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24995 error_at (token->location, "schedule %<auto%> does not take "
24996 "a %<chunk_size%> parameter");
24998 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25000 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25003 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25006 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25007 OMP_CLAUSE_CHAIN (c) = list;
25011 cp_parser_error (parser, "invalid schedule kind");
25013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25014 /*or_comma=*/false,
25015 /*consume_paren=*/true);
25023 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25024 tree list, location_t location)
25028 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25030 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25031 OMP_CLAUSE_CHAIN (c) = list;
25035 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25036 is a bitmask in MASK. Return the list of clauses found; the result
25037 of clause default goes in *pdefault. */
25040 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25041 const char *where, cp_token *pragma_tok)
25043 tree clauses = NULL;
25045 cp_token *token = NULL;
25047 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25049 pragma_omp_clause c_kind;
25050 const char *c_name;
25051 tree prev = clauses;
25053 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25054 cp_lexer_consume_token (parser->lexer);
25056 token = cp_lexer_peek_token (parser->lexer);
25057 c_kind = cp_parser_omp_clause_name (parser);
25062 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25063 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25065 c_name = "collapse";
25067 case PRAGMA_OMP_CLAUSE_COPYIN:
25068 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25071 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25072 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25074 c_name = "copyprivate";
25076 case PRAGMA_OMP_CLAUSE_DEFAULT:
25077 clauses = cp_parser_omp_clause_default (parser, clauses,
25079 c_name = "default";
25081 case PRAGMA_OMP_CLAUSE_FINAL:
25082 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25085 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25086 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25088 c_name = "firstprivate";
25090 case PRAGMA_OMP_CLAUSE_IF:
25091 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25094 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25095 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25097 c_name = "lastprivate";
25099 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25100 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25102 c_name = "mergeable";
25104 case PRAGMA_OMP_CLAUSE_NOWAIT:
25105 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25108 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25109 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25111 c_name = "num_threads";
25113 case PRAGMA_OMP_CLAUSE_ORDERED:
25114 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25116 c_name = "ordered";
25118 case PRAGMA_OMP_CLAUSE_PRIVATE:
25119 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25121 c_name = "private";
25123 case PRAGMA_OMP_CLAUSE_REDUCTION:
25124 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25125 c_name = "reduction";
25127 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25128 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25130 c_name = "schedule";
25132 case PRAGMA_OMP_CLAUSE_SHARED:
25133 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25137 case PRAGMA_OMP_CLAUSE_UNTIED:
25138 clauses = cp_parser_omp_clause_untied (parser, clauses,
25143 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25147 if (((mask >> c_kind) & 1) == 0)
25149 /* Remove the invalid clause(s) from the list to avoid
25150 confusing the rest of the compiler. */
25152 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25156 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25157 return finish_omp_clauses (clauses);
25164 In practice, we're also interested in adding the statement to an
25165 outer node. So it is convenient if we work around the fact that
25166 cp_parser_statement calls add_stmt. */
25169 cp_parser_begin_omp_structured_block (cp_parser *parser)
25171 unsigned save = parser->in_statement;
25173 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25174 This preserves the "not within loop or switch" style error messages
25175 for nonsense cases like
25181 if (parser->in_statement)
25182 parser->in_statement = IN_OMP_BLOCK;
25188 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25190 parser->in_statement = save;
25194 cp_parser_omp_structured_block (cp_parser *parser)
25196 tree stmt = begin_omp_structured_block ();
25197 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25199 cp_parser_statement (parser, NULL_TREE, false, NULL);
25201 cp_parser_end_omp_structured_block (parser, save);
25202 return finish_omp_structured_block (stmt);
25206 # pragma omp atomic new-line
25210 x binop= expr | x++ | ++x | x-- | --x
25212 +, *, -, /, &, ^, |, <<, >>
25214 where x is an lvalue expression with scalar type.
25217 # pragma omp atomic new-line
25220 # pragma omp atomic read new-line
25223 # pragma omp atomic write new-line
25226 # pragma omp atomic update new-line
25229 # pragma omp atomic capture new-line
25232 # pragma omp atomic capture new-line
25240 expression-stmt | x = x binop expr
25242 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25244 { v = x; update-stmt; } | { update-stmt; v = x; }
25246 where x and v are lvalue expressions with scalar type. */
25249 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25251 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25252 tree rhs1 = NULL_TREE, orig_lhs;
25253 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25254 bool structured_block = false;
25256 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25258 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25259 const char *p = IDENTIFIER_POINTER (id);
25261 if (!strcmp (p, "read"))
25262 code = OMP_ATOMIC_READ;
25263 else if (!strcmp (p, "write"))
25265 else if (!strcmp (p, "update"))
25267 else if (!strcmp (p, "capture"))
25268 code = OMP_ATOMIC_CAPTURE_NEW;
25272 cp_lexer_consume_token (parser->lexer);
25274 cp_parser_require_pragma_eol (parser, pragma_tok);
25278 case OMP_ATOMIC_READ:
25279 case NOP_EXPR: /* atomic write */
25280 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25281 /*cast_p=*/false, NULL);
25282 if (v == error_mark_node)
25284 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25286 if (code == NOP_EXPR)
25287 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25289 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25290 /*cast_p=*/false, NULL);
25291 if (lhs == error_mark_node)
25293 if (code == NOP_EXPR)
25295 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25303 case OMP_ATOMIC_CAPTURE_NEW:
25304 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25306 cp_lexer_consume_token (parser->lexer);
25307 structured_block = true;
25311 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25312 /*cast_p=*/false, NULL);
25313 if (v == error_mark_node)
25315 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25323 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25324 /*cast_p=*/false, NULL);
25326 switch (TREE_CODE (lhs))
25331 case POSTINCREMENT_EXPR:
25332 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25333 code = OMP_ATOMIC_CAPTURE_OLD;
25335 case PREINCREMENT_EXPR:
25336 lhs = TREE_OPERAND (lhs, 0);
25337 opcode = PLUS_EXPR;
25338 rhs = integer_one_node;
25341 case POSTDECREMENT_EXPR:
25342 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25343 code = OMP_ATOMIC_CAPTURE_OLD;
25345 case PREDECREMENT_EXPR:
25346 lhs = TREE_OPERAND (lhs, 0);
25347 opcode = MINUS_EXPR;
25348 rhs = integer_one_node;
25351 case COMPOUND_EXPR:
25352 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25353 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25354 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25355 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25356 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25357 (TREE_OPERAND (lhs, 1), 0), 0)))
25359 /* Undo effects of boolean_increment for post {in,de}crement. */
25360 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25363 if (TREE_CODE (lhs) == MODIFY_EXPR
25364 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25366 /* Undo effects of boolean_increment. */
25367 if (integer_onep (TREE_OPERAND (lhs, 1)))
25369 /* This is pre or post increment. */
25370 rhs = TREE_OPERAND (lhs, 1);
25371 lhs = TREE_OPERAND (lhs, 0);
25373 if (code == OMP_ATOMIC_CAPTURE_NEW
25374 && !structured_block
25375 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25376 code = OMP_ATOMIC_CAPTURE_OLD;
25382 switch (cp_lexer_peek_token (parser->lexer)->type)
25385 opcode = MULT_EXPR;
25388 opcode = TRUNC_DIV_EXPR;
25391 opcode = PLUS_EXPR;
25394 opcode = MINUS_EXPR;
25396 case CPP_LSHIFT_EQ:
25397 opcode = LSHIFT_EXPR;
25399 case CPP_RSHIFT_EQ:
25400 opcode = RSHIFT_EXPR;
25403 opcode = BIT_AND_EXPR;
25406 opcode = BIT_IOR_EXPR;
25409 opcode = BIT_XOR_EXPR;
25412 if (structured_block || code == OMP_ATOMIC)
25414 enum cp_parser_prec oprec;
25416 cp_lexer_consume_token (parser->lexer);
25417 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25418 /*cast_p=*/false, NULL);
25419 if (rhs1 == error_mark_node)
25421 token = cp_lexer_peek_token (parser->lexer);
25422 switch (token->type)
25424 case CPP_SEMICOLON:
25425 if (code == OMP_ATOMIC_CAPTURE_NEW)
25427 code = OMP_ATOMIC_CAPTURE_OLD;
25432 cp_lexer_consume_token (parser->lexer);
25435 cp_parser_error (parser,
25436 "invalid form of %<#pragma omp atomic%>");
25439 opcode = MULT_EXPR;
25442 opcode = TRUNC_DIV_EXPR;
25445 opcode = PLUS_EXPR;
25448 opcode = MINUS_EXPR;
25451 opcode = LSHIFT_EXPR;
25454 opcode = RSHIFT_EXPR;
25457 opcode = BIT_AND_EXPR;
25460 opcode = BIT_IOR_EXPR;
25463 opcode = BIT_XOR_EXPR;
25466 cp_parser_error (parser,
25467 "invalid operator for %<#pragma omp atomic%>");
25470 oprec = TOKEN_PRECEDENCE (token);
25471 gcc_assert (oprec != PREC_NOT_OPERATOR);
25472 if (commutative_tree_code (opcode))
25473 oprec = (enum cp_parser_prec) (oprec - 1);
25474 cp_lexer_consume_token (parser->lexer);
25475 rhs = cp_parser_binary_expression (parser, false, false,
25477 if (rhs == error_mark_node)
25483 cp_parser_error (parser,
25484 "invalid operator for %<#pragma omp atomic%>");
25487 cp_lexer_consume_token (parser->lexer);
25489 rhs = cp_parser_expression (parser, false, NULL);
25490 if (rhs == error_mark_node)
25495 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25497 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25499 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25500 /*cast_p=*/false, NULL);
25501 if (v == error_mark_node)
25503 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25505 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25506 /*cast_p=*/false, NULL);
25507 if (lhs1 == error_mark_node)
25510 if (structured_block)
25512 cp_parser_consume_semicolon_at_end_of_statement (parser);
25513 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25516 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25517 if (!structured_block)
25518 cp_parser_consume_semicolon_at_end_of_statement (parser);
25522 cp_parser_skip_to_end_of_block_or_statement (parser);
25523 if (structured_block)
25525 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25526 cp_lexer_consume_token (parser->lexer);
25527 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25529 cp_parser_skip_to_end_of_block_or_statement (parser);
25530 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25531 cp_lexer_consume_token (parser->lexer);
25538 # pragma omp barrier new-line */
25541 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25543 cp_parser_require_pragma_eol (parser, pragma_tok);
25544 finish_omp_barrier ();
25548 # pragma omp critical [(name)] new-line
25549 structured-block */
25552 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25554 tree stmt, name = NULL;
25556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25558 cp_lexer_consume_token (parser->lexer);
25560 name = cp_parser_identifier (parser);
25562 if (name == error_mark_node
25563 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25564 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25565 /*or_comma=*/false,
25566 /*consume_paren=*/true);
25567 if (name == error_mark_node)
25570 cp_parser_require_pragma_eol (parser, pragma_tok);
25572 stmt = cp_parser_omp_structured_block (parser);
25573 return c_finish_omp_critical (input_location, stmt, name);
25577 # pragma omp flush flush-vars[opt] new-line
25580 ( variable-list ) */
25583 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25585 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25586 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25587 cp_parser_require_pragma_eol (parser, pragma_tok);
25589 finish_omp_flush ();
25592 /* Helper function, to parse omp for increment expression. */
25595 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25597 tree cond = cp_parser_binary_expression (parser, false, true,
25598 PREC_NOT_OPERATOR, NULL);
25599 if (cond == error_mark_node
25600 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25602 cp_parser_skip_to_end_of_statement (parser);
25603 return error_mark_node;
25606 switch (TREE_CODE (cond))
25614 return error_mark_node;
25617 /* If decl is an iterator, preserve LHS and RHS of the relational
25618 expr until finish_omp_for. */
25620 && (type_dependent_expression_p (decl)
25621 || CLASS_TYPE_P (TREE_TYPE (decl))))
25624 return build_x_binary_op (TREE_CODE (cond),
25625 TREE_OPERAND (cond, 0), ERROR_MARK,
25626 TREE_OPERAND (cond, 1), ERROR_MARK,
25627 /*overload=*/NULL, tf_warning_or_error);
25630 /* Helper function, to parse omp for increment expression. */
25633 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25635 cp_token *token = cp_lexer_peek_token (parser->lexer);
25641 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25643 op = (token->type == CPP_PLUS_PLUS
25644 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25645 cp_lexer_consume_token (parser->lexer);
25646 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25648 return error_mark_node;
25649 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25652 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25654 return error_mark_node;
25656 token = cp_lexer_peek_token (parser->lexer);
25657 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25659 op = (token->type == CPP_PLUS_PLUS
25660 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25661 cp_lexer_consume_token (parser->lexer);
25662 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25665 op = cp_parser_assignment_operator_opt (parser);
25666 if (op == ERROR_MARK)
25667 return error_mark_node;
25669 if (op != NOP_EXPR)
25671 rhs = cp_parser_assignment_expression (parser, false, NULL);
25672 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25673 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25676 lhs = cp_parser_binary_expression (parser, false, false,
25677 PREC_ADDITIVE_EXPRESSION, NULL);
25678 token = cp_lexer_peek_token (parser->lexer);
25679 decl_first = lhs == decl;
25682 if (token->type != CPP_PLUS
25683 && token->type != CPP_MINUS)
25684 return error_mark_node;
25688 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25689 cp_lexer_consume_token (parser->lexer);
25690 rhs = cp_parser_binary_expression (parser, false, false,
25691 PREC_ADDITIVE_EXPRESSION, NULL);
25692 token = cp_lexer_peek_token (parser->lexer);
25693 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25695 if (lhs == NULL_TREE)
25697 if (op == PLUS_EXPR)
25700 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25703 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25704 NULL, tf_warning_or_error);
25707 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25711 if (rhs != decl || op == MINUS_EXPR)
25712 return error_mark_node;
25713 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25716 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25718 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25721 /* Parse the restricted form of the for statement allowed by OpenMP. */
25724 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25726 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25727 tree real_decl, initv, condv, incrv, declv;
25728 tree this_pre_body, cl;
25729 location_t loc_first;
25730 bool collapse_err = false;
25731 int i, collapse = 1, nbraces = 0;
25732 VEC(tree,gc) *for_block = make_tree_vector ();
25734 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25735 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25736 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25738 gcc_assert (collapse >= 1);
25740 declv = make_tree_vec (collapse);
25741 initv = make_tree_vec (collapse);
25742 condv = make_tree_vec (collapse);
25743 incrv = make_tree_vec (collapse);
25745 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25747 for (i = 0; i < collapse; i++)
25749 int bracecount = 0;
25750 bool add_private_clause = false;
25753 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25755 cp_parser_error (parser, "for statement expected");
25758 loc = cp_lexer_consume_token (parser->lexer)->location;
25760 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25763 init = decl = real_decl = NULL;
25764 this_pre_body = push_stmt_list ();
25765 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25767 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25771 integer-type var = lb
25772 random-access-iterator-type var = lb
25773 pointer-type var = lb
25775 cp_decl_specifier_seq type_specifiers;
25777 /* First, try to parse as an initialized declaration. See
25778 cp_parser_condition, from whence the bulk of this is copied. */
25780 cp_parser_parse_tentatively (parser);
25781 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25782 /*is_trailing_return=*/false,
25784 if (cp_parser_parse_definitely (parser))
25786 /* If parsing a type specifier seq succeeded, then this
25787 MUST be a initialized declaration. */
25788 tree asm_specification, attributes;
25789 cp_declarator *declarator;
25791 declarator = cp_parser_declarator (parser,
25792 CP_PARSER_DECLARATOR_NAMED,
25793 /*ctor_dtor_or_conv_p=*/NULL,
25794 /*parenthesized_p=*/NULL,
25795 /*member_p=*/false);
25796 attributes = cp_parser_attributes_opt (parser);
25797 asm_specification = cp_parser_asm_specification_opt (parser);
25799 if (declarator == cp_error_declarator)
25800 cp_parser_skip_to_end_of_statement (parser);
25804 tree pushed_scope, auto_node;
25806 decl = start_decl (declarator, &type_specifiers,
25807 SD_INITIALIZED, attributes,
25808 /*prefix_attributes=*/NULL_TREE,
25811 auto_node = type_uses_auto (TREE_TYPE (decl));
25812 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25814 if (cp_lexer_next_token_is (parser->lexer,
25816 error ("parenthesized initialization is not allowed in "
25817 "OpenMP %<for%> loop");
25819 /* Trigger an error. */
25820 cp_parser_require (parser, CPP_EQ, RT_EQ);
25822 init = error_mark_node;
25823 cp_parser_skip_to_end_of_statement (parser);
25825 else if (CLASS_TYPE_P (TREE_TYPE (decl))
25826 || type_dependent_expression_p (decl)
25829 bool is_direct_init, is_non_constant_init;
25831 init = cp_parser_initializer (parser,
25833 &is_non_constant_init);
25838 = do_auto_deduction (TREE_TYPE (decl), init,
25841 if (!CLASS_TYPE_P (TREE_TYPE (decl))
25842 && !type_dependent_expression_p (decl))
25846 cp_finish_decl (decl, init, !is_non_constant_init,
25848 LOOKUP_ONLYCONVERTING);
25849 if (CLASS_TYPE_P (TREE_TYPE (decl)))
25851 VEC_safe_push (tree, gc, for_block, this_pre_body);
25855 init = pop_stmt_list (this_pre_body);
25856 this_pre_body = NULL_TREE;
25861 cp_lexer_consume_token (parser->lexer);
25862 init = cp_parser_assignment_expression (parser, false, NULL);
25865 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25866 init = error_mark_node;
25868 cp_finish_decl (decl, NULL_TREE,
25869 /*init_const_expr_p=*/false,
25871 LOOKUP_ONLYCONVERTING);
25875 pop_scope (pushed_scope);
25881 /* If parsing a type specifier sequence failed, then
25882 this MUST be a simple expression. */
25883 cp_parser_parse_tentatively (parser);
25884 decl = cp_parser_primary_expression (parser, false, false,
25886 if (!cp_parser_error_occurred (parser)
25889 && CLASS_TYPE_P (TREE_TYPE (decl)))
25893 cp_parser_parse_definitely (parser);
25894 cp_parser_require (parser, CPP_EQ, RT_EQ);
25895 rhs = cp_parser_assignment_expression (parser, false, NULL);
25896 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25898 tf_warning_or_error));
25899 add_private_clause = true;
25904 cp_parser_abort_tentative_parse (parser);
25905 init = cp_parser_expression (parser, false, NULL);
25908 if (TREE_CODE (init) == MODIFY_EXPR
25909 || TREE_CODE (init) == MODOP_EXPR)
25910 real_decl = TREE_OPERAND (init, 0);
25915 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25918 this_pre_body = pop_stmt_list (this_pre_body);
25922 pre_body = push_stmt_list ();
25924 add_stmt (this_pre_body);
25925 pre_body = pop_stmt_list (pre_body);
25928 pre_body = this_pre_body;
25933 if (par_clauses != NULL && real_decl != NULL_TREE)
25936 for (c = par_clauses; *c ; )
25937 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25938 && OMP_CLAUSE_DECL (*c) == real_decl)
25940 error_at (loc, "iteration variable %qD"
25941 " should not be firstprivate", real_decl);
25942 *c = OMP_CLAUSE_CHAIN (*c);
25944 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25945 && OMP_CLAUSE_DECL (*c) == real_decl)
25947 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25948 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
25949 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25950 OMP_CLAUSE_DECL (l) = real_decl;
25951 OMP_CLAUSE_CHAIN (l) = clauses;
25952 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25954 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25955 CP_OMP_CLAUSE_INFO (*c) = NULL;
25956 add_private_clause = false;
25960 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25961 && OMP_CLAUSE_DECL (*c) == real_decl)
25962 add_private_clause = false;
25963 c = &OMP_CLAUSE_CHAIN (*c);
25967 if (add_private_clause)
25970 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25972 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25973 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25974 && OMP_CLAUSE_DECL (c) == decl)
25976 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25977 && OMP_CLAUSE_DECL (c) == decl)
25978 error_at (loc, "iteration variable %qD "
25979 "should not be firstprivate",
25981 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25982 && OMP_CLAUSE_DECL (c) == decl)
25983 error_at (loc, "iteration variable %qD should not be reduction",
25988 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25989 OMP_CLAUSE_DECL (c) = decl;
25990 c = finish_omp_clauses (c);
25993 OMP_CLAUSE_CHAIN (c) = clauses;
26000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26001 cond = cp_parser_omp_for_cond (parser, decl);
26002 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26005 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26007 /* If decl is an iterator, preserve the operator on decl
26008 until finish_omp_for. */
26010 && ((type_dependent_expression_p (decl)
26011 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26012 || CLASS_TYPE_P (TREE_TYPE (decl))))
26013 incr = cp_parser_omp_for_incr (parser, decl);
26015 incr = cp_parser_expression (parser, false, NULL);
26018 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26019 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26020 /*or_comma=*/false,
26021 /*consume_paren=*/true);
26023 TREE_VEC_ELT (declv, i) = decl;
26024 TREE_VEC_ELT (initv, i) = init;
26025 TREE_VEC_ELT (condv, i) = cond;
26026 TREE_VEC_ELT (incrv, i) = incr;
26028 if (i == collapse - 1)
26031 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26032 in between the collapsed for loops to be still considered perfectly
26033 nested. Hopefully the final version clarifies this.
26034 For now handle (multiple) {'s and empty statements. */
26035 cp_parser_parse_tentatively (parser);
26038 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26040 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26042 cp_lexer_consume_token (parser->lexer);
26045 else if (bracecount
26046 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26047 cp_lexer_consume_token (parser->lexer);
26050 loc = cp_lexer_peek_token (parser->lexer)->location;
26051 error_at (loc, "not enough collapsed for loops");
26052 collapse_err = true;
26053 cp_parser_abort_tentative_parse (parser);
26062 cp_parser_parse_definitely (parser);
26063 nbraces += bracecount;
26067 /* Note that we saved the original contents of this flag when we entered
26068 the structured block, and so we don't need to re-save it here. */
26069 parser->in_statement = IN_OMP_FOR;
26071 /* Note that the grammar doesn't call for a structured block here,
26072 though the loop as a whole is a structured block. */
26073 body = push_stmt_list ();
26074 cp_parser_statement (parser, NULL_TREE, false, NULL);
26075 body = pop_stmt_list (body);
26077 if (declv == NULL_TREE)
26080 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26081 pre_body, clauses);
26085 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26087 cp_lexer_consume_token (parser->lexer);
26090 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26091 cp_lexer_consume_token (parser->lexer);
26096 error_at (cp_lexer_peek_token (parser->lexer)->location,
26097 "collapsed loops not perfectly nested");
26099 collapse_err = true;
26100 cp_parser_statement_seq_opt (parser, NULL);
26101 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26106 while (!VEC_empty (tree, for_block))
26107 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26108 release_tree_vector (for_block);
26114 #pragma omp for for-clause[optseq] new-line
26117 #define OMP_FOR_CLAUSE_MASK \
26118 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26119 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26120 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26121 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26122 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26123 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26124 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26125 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26128 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26130 tree clauses, sb, ret;
26133 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26134 "#pragma omp for", pragma_tok);
26136 sb = begin_omp_structured_block ();
26137 save = cp_parser_begin_omp_structured_block (parser);
26139 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26141 cp_parser_end_omp_structured_block (parser, save);
26142 add_stmt (finish_omp_structured_block (sb));
26148 # pragma omp master new-line
26149 structured-block */
26152 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26154 cp_parser_require_pragma_eol (parser, pragma_tok);
26155 return c_finish_omp_master (input_location,
26156 cp_parser_omp_structured_block (parser));
26160 # pragma omp ordered new-line
26161 structured-block */
26164 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26166 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26167 cp_parser_require_pragma_eol (parser, pragma_tok);
26168 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26174 { section-sequence }
26177 section-directive[opt] structured-block
26178 section-sequence section-directive structured-block */
26181 cp_parser_omp_sections_scope (cp_parser *parser)
26183 tree stmt, substmt;
26184 bool error_suppress = false;
26187 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26190 stmt = push_stmt_list ();
26192 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26196 substmt = begin_omp_structured_block ();
26197 save = cp_parser_begin_omp_structured_block (parser);
26201 cp_parser_statement (parser, NULL_TREE, false, NULL);
26203 tok = cp_lexer_peek_token (parser->lexer);
26204 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26206 if (tok->type == CPP_CLOSE_BRACE)
26208 if (tok->type == CPP_EOF)
26212 cp_parser_end_omp_structured_block (parser, save);
26213 substmt = finish_omp_structured_block (substmt);
26214 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26215 add_stmt (substmt);
26220 tok = cp_lexer_peek_token (parser->lexer);
26221 if (tok->type == CPP_CLOSE_BRACE)
26223 if (tok->type == CPP_EOF)
26226 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26228 cp_lexer_consume_token (parser->lexer);
26229 cp_parser_require_pragma_eol (parser, tok);
26230 error_suppress = false;
26232 else if (!error_suppress)
26234 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26235 error_suppress = true;
26238 substmt = cp_parser_omp_structured_block (parser);
26239 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26240 add_stmt (substmt);
26242 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26244 substmt = pop_stmt_list (stmt);
26246 stmt = make_node (OMP_SECTIONS);
26247 TREE_TYPE (stmt) = void_type_node;
26248 OMP_SECTIONS_BODY (stmt) = substmt;
26255 # pragma omp sections sections-clause[optseq] newline
26258 #define OMP_SECTIONS_CLAUSE_MASK \
26259 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26260 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26261 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26262 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26263 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26266 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26270 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26271 "#pragma omp sections", pragma_tok);
26273 ret = cp_parser_omp_sections_scope (parser);
26275 OMP_SECTIONS_CLAUSES (ret) = clauses;
26281 # pragma parallel parallel-clause new-line
26282 # pragma parallel for parallel-for-clause new-line
26283 # pragma parallel sections parallel-sections-clause new-line */
26285 #define OMP_PARALLEL_CLAUSE_MASK \
26286 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26287 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26288 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26289 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26290 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26291 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26292 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26293 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26296 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26298 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26299 const char *p_name = "#pragma omp parallel";
26300 tree stmt, clauses, par_clause, ws_clause, block;
26301 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26303 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26305 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26307 cp_lexer_consume_token (parser->lexer);
26308 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26309 p_name = "#pragma omp parallel for";
26310 mask |= OMP_FOR_CLAUSE_MASK;
26311 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26313 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26315 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26316 const char *p = IDENTIFIER_POINTER (id);
26317 if (strcmp (p, "sections") == 0)
26319 cp_lexer_consume_token (parser->lexer);
26320 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26321 p_name = "#pragma omp parallel sections";
26322 mask |= OMP_SECTIONS_CLAUSE_MASK;
26323 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26327 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26328 block = begin_omp_parallel ();
26329 save = cp_parser_begin_omp_structured_block (parser);
26333 case PRAGMA_OMP_PARALLEL:
26334 cp_parser_statement (parser, NULL_TREE, false, NULL);
26335 par_clause = clauses;
26338 case PRAGMA_OMP_PARALLEL_FOR:
26339 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26340 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26343 case PRAGMA_OMP_PARALLEL_SECTIONS:
26344 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26345 stmt = cp_parser_omp_sections_scope (parser);
26347 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26351 gcc_unreachable ();
26354 cp_parser_end_omp_structured_block (parser, save);
26355 stmt = finish_omp_parallel (par_clause, block);
26356 if (p_kind != PRAGMA_OMP_PARALLEL)
26357 OMP_PARALLEL_COMBINED (stmt) = 1;
26362 # pragma omp single single-clause[optseq] new-line
26363 structured-block */
26365 #define OMP_SINGLE_CLAUSE_MASK \
26366 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26367 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26368 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26369 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26372 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26374 tree stmt = make_node (OMP_SINGLE);
26375 TREE_TYPE (stmt) = void_type_node;
26377 OMP_SINGLE_CLAUSES (stmt)
26378 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26379 "#pragma omp single", pragma_tok);
26380 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26382 return add_stmt (stmt);
26386 # pragma omp task task-clause[optseq] new-line
26387 structured-block */
26389 #define OMP_TASK_CLAUSE_MASK \
26390 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26391 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26392 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26393 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26394 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26395 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26396 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26397 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26400 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26402 tree clauses, block;
26405 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26406 "#pragma omp task", pragma_tok);
26407 block = begin_omp_task ();
26408 save = cp_parser_begin_omp_structured_block (parser);
26409 cp_parser_statement (parser, NULL_TREE, false, NULL);
26410 cp_parser_end_omp_structured_block (parser, save);
26411 return finish_omp_task (clauses, block);
26415 # pragma omp taskwait new-line */
26418 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26420 cp_parser_require_pragma_eol (parser, pragma_tok);
26421 finish_omp_taskwait ();
26425 # pragma omp taskyield new-line */
26428 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26430 cp_parser_require_pragma_eol (parser, pragma_tok);
26431 finish_omp_taskyield ();
26435 # pragma omp threadprivate (variable-list) */
26438 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26442 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26443 cp_parser_require_pragma_eol (parser, pragma_tok);
26445 finish_omp_threadprivate (vars);
26448 /* Main entry point to OpenMP statement pragmas. */
26451 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26455 switch (pragma_tok->pragma_kind)
26457 case PRAGMA_OMP_ATOMIC:
26458 cp_parser_omp_atomic (parser, pragma_tok);
26460 case PRAGMA_OMP_CRITICAL:
26461 stmt = cp_parser_omp_critical (parser, pragma_tok);
26463 case PRAGMA_OMP_FOR:
26464 stmt = cp_parser_omp_for (parser, pragma_tok);
26466 case PRAGMA_OMP_MASTER:
26467 stmt = cp_parser_omp_master (parser, pragma_tok);
26469 case PRAGMA_OMP_ORDERED:
26470 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26472 case PRAGMA_OMP_PARALLEL:
26473 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26475 case PRAGMA_OMP_SECTIONS:
26476 stmt = cp_parser_omp_sections (parser, pragma_tok);
26478 case PRAGMA_OMP_SINGLE:
26479 stmt = cp_parser_omp_single (parser, pragma_tok);
26481 case PRAGMA_OMP_TASK:
26482 stmt = cp_parser_omp_task (parser, pragma_tok);
26485 gcc_unreachable ();
26489 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26494 static GTY (()) cp_parser *the_parser;
26497 /* Special handling for the first token or line in the file. The first
26498 thing in the file might be #pragma GCC pch_preprocess, which loads a
26499 PCH file, which is a GC collection point. So we need to handle this
26500 first pragma without benefit of an existing lexer structure.
26502 Always returns one token to the caller in *FIRST_TOKEN. This is
26503 either the true first token of the file, or the first token after
26504 the initial pragma. */
26507 cp_parser_initial_pragma (cp_token *first_token)
26511 cp_lexer_get_preprocessor_token (NULL, first_token);
26512 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26515 cp_lexer_get_preprocessor_token (NULL, first_token);
26516 if (first_token->type == CPP_STRING)
26518 name = first_token->u.value;
26520 cp_lexer_get_preprocessor_token (NULL, first_token);
26521 if (first_token->type != CPP_PRAGMA_EOL)
26522 error_at (first_token->location,
26523 "junk at end of %<#pragma GCC pch_preprocess%>");
26526 error_at (first_token->location, "expected string literal");
26528 /* Skip to the end of the pragma. */
26529 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26530 cp_lexer_get_preprocessor_token (NULL, first_token);
26532 /* Now actually load the PCH file. */
26534 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
26536 /* Read one more token to return to our caller. We have to do this
26537 after reading the PCH file in, since its pointers have to be
26539 cp_lexer_get_preprocessor_token (NULL, first_token);
26542 /* Normal parsing of a pragma token. Here we can (and must) use the
26546 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
26548 cp_token *pragma_tok;
26551 pragma_tok = cp_lexer_consume_token (parser->lexer);
26552 gcc_assert (pragma_tok->type == CPP_PRAGMA);
26553 parser->lexer->in_pragma = true;
26555 id = pragma_tok->pragma_kind;
26558 case PRAGMA_GCC_PCH_PREPROCESS:
26559 error_at (pragma_tok->location,
26560 "%<#pragma GCC pch_preprocess%> must be first");
26563 case PRAGMA_OMP_BARRIER:
26566 case pragma_compound:
26567 cp_parser_omp_barrier (parser, pragma_tok);
26570 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
26571 "used in compound statements");
26578 case PRAGMA_OMP_FLUSH:
26581 case pragma_compound:
26582 cp_parser_omp_flush (parser, pragma_tok);
26585 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
26586 "used in compound statements");
26593 case PRAGMA_OMP_TASKWAIT:
26596 case pragma_compound:
26597 cp_parser_omp_taskwait (parser, pragma_tok);
26600 error_at (pragma_tok->location,
26601 "%<#pragma omp taskwait%> may only be "
26602 "used in compound statements");
26609 case PRAGMA_OMP_TASKYIELD:
26612 case pragma_compound:
26613 cp_parser_omp_taskyield (parser, pragma_tok);
26616 error_at (pragma_tok->location,
26617 "%<#pragma omp taskyield%> may only be "
26618 "used in compound statements");
26625 case PRAGMA_OMP_THREADPRIVATE:
26626 cp_parser_omp_threadprivate (parser, pragma_tok);
26629 case PRAGMA_OMP_ATOMIC:
26630 case PRAGMA_OMP_CRITICAL:
26631 case PRAGMA_OMP_FOR:
26632 case PRAGMA_OMP_MASTER:
26633 case PRAGMA_OMP_ORDERED:
26634 case PRAGMA_OMP_PARALLEL:
26635 case PRAGMA_OMP_SECTIONS:
26636 case PRAGMA_OMP_SINGLE:
26637 case PRAGMA_OMP_TASK:
26638 if (context == pragma_external)
26640 cp_parser_omp_construct (parser, pragma_tok);
26643 case PRAGMA_OMP_SECTION:
26644 error_at (pragma_tok->location,
26645 "%<#pragma omp section%> may only be used in "
26646 "%<#pragma omp sections%> construct");
26650 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
26651 c_invoke_pragma_handler (id);
26655 cp_parser_error (parser, "expected declaration specifiers");
26659 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26663 /* The interface the pragma parsers have to the lexer. */
26666 pragma_lex (tree *value)
26669 enum cpp_ttype ret;
26671 tok = cp_lexer_peek_token (the_parser->lexer);
26674 *value = tok->u.value;
26676 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26678 else if (ret == CPP_STRING)
26679 *value = cp_parser_string_literal (the_parser, false, false);
26682 cp_lexer_consume_token (the_parser->lexer);
26683 if (ret == CPP_KEYWORD)
26691 /* External interface. */
26693 /* Parse one entire translation unit. */
26696 c_parse_file (void)
26698 static bool already_called = false;
26700 if (already_called)
26702 sorry ("inter-module optimizations not implemented for C++");
26705 already_called = true;
26707 the_parser = cp_parser_new ();
26708 push_deferring_access_checks (flag_access_control
26709 ? dk_no_deferred : dk_no_check);
26710 cp_parser_translation_unit (the_parser);
26714 #include "gt-cp-parser.h"