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 is a keyword in C++11",
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 tree cp_parser_alias_declaration
1940 static void cp_parser_asm_definition
1942 static void cp_parser_linkage_specification
1944 static void cp_parser_static_assert
1945 (cp_parser *, bool);
1946 static tree cp_parser_decltype
1949 /* Declarators [gram.dcl.decl] */
1951 static tree cp_parser_init_declarator
1952 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1953 static cp_declarator *cp_parser_declarator
1954 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1955 static cp_declarator *cp_parser_direct_declarator
1956 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1957 static enum tree_code cp_parser_ptr_operator
1958 (cp_parser *, tree *, cp_cv_quals *);
1959 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1961 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1963 static tree cp_parser_late_return_type_opt
1964 (cp_parser *, cp_cv_quals);
1965 static tree cp_parser_declarator_id
1966 (cp_parser *, bool);
1967 static tree cp_parser_type_id
1969 static tree cp_parser_template_type_arg
1971 static tree cp_parser_trailing_type_id (cp_parser *);
1972 static tree cp_parser_type_id_1
1973 (cp_parser *, bool, bool);
1974 static void cp_parser_type_specifier_seq
1975 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1976 static tree cp_parser_parameter_declaration_clause
1978 static tree cp_parser_parameter_declaration_list
1979 (cp_parser *, bool *);
1980 static cp_parameter_declarator *cp_parser_parameter_declaration
1981 (cp_parser *, bool, bool *);
1982 static tree cp_parser_default_argument
1983 (cp_parser *, bool);
1984 static void cp_parser_function_body
1986 static tree cp_parser_initializer
1987 (cp_parser *, bool *, bool *);
1988 static tree cp_parser_initializer_clause
1989 (cp_parser *, bool *);
1990 static tree cp_parser_braced_list
1991 (cp_parser*, bool*);
1992 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1993 (cp_parser *, bool *);
1995 static bool cp_parser_ctor_initializer_opt_and_function_body
1998 /* Classes [gram.class] */
2000 static tree cp_parser_class_name
2001 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2002 static tree cp_parser_class_specifier
2004 static tree cp_parser_class_head
2005 (cp_parser *, bool *, tree *, tree *);
2006 static enum tag_types cp_parser_class_key
2008 static void cp_parser_member_specification_opt
2010 static void cp_parser_member_declaration
2012 static tree cp_parser_pure_specifier
2014 static tree cp_parser_constant_initializer
2017 /* Derived classes [gram.class.derived] */
2019 static tree cp_parser_base_clause
2021 static tree cp_parser_base_specifier
2024 /* Special member functions [gram.special] */
2026 static tree cp_parser_conversion_function_id
2028 static tree cp_parser_conversion_type_id
2030 static cp_declarator *cp_parser_conversion_declarator_opt
2032 static bool cp_parser_ctor_initializer_opt
2034 static void cp_parser_mem_initializer_list
2036 static tree cp_parser_mem_initializer
2038 static tree cp_parser_mem_initializer_id
2041 /* Overloading [gram.over] */
2043 static tree cp_parser_operator_function_id
2045 static tree cp_parser_operator
2048 /* Templates [gram.temp] */
2050 static void cp_parser_template_declaration
2051 (cp_parser *, bool);
2052 static tree cp_parser_template_parameter_list
2054 static tree cp_parser_template_parameter
2055 (cp_parser *, bool *, bool *);
2056 static tree cp_parser_type_parameter
2057 (cp_parser *, bool *);
2058 static tree cp_parser_template_id
2059 (cp_parser *, bool, bool, bool);
2060 static tree cp_parser_template_name
2061 (cp_parser *, bool, bool, bool, bool *);
2062 static tree cp_parser_template_argument_list
2064 static tree cp_parser_template_argument
2066 static void cp_parser_explicit_instantiation
2068 static void cp_parser_explicit_specialization
2071 /* Exception handling [gram.exception] */
2073 static tree cp_parser_try_block
2075 static bool cp_parser_function_try_block
2077 static void cp_parser_handler_seq
2079 static void cp_parser_handler
2081 static tree cp_parser_exception_declaration
2083 static tree cp_parser_throw_expression
2085 static tree cp_parser_exception_specification_opt
2087 static tree cp_parser_type_id_list
2090 /* GNU Extensions */
2092 static tree cp_parser_asm_specification_opt
2094 static tree cp_parser_asm_operand_list
2096 static tree cp_parser_asm_clobber_list
2098 static tree cp_parser_asm_label_list
2100 static tree cp_parser_attributes_opt
2102 static tree cp_parser_attribute_list
2104 static bool cp_parser_extension_opt
2105 (cp_parser *, int *);
2106 static void cp_parser_label_declaration
2109 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2110 static bool cp_parser_pragma
2111 (cp_parser *, enum pragma_context);
2113 /* Objective-C++ Productions */
2115 static tree cp_parser_objc_message_receiver
2117 static tree cp_parser_objc_message_args
2119 static tree cp_parser_objc_message_expression
2121 static tree cp_parser_objc_encode_expression
2123 static tree cp_parser_objc_defs_expression
2125 static tree cp_parser_objc_protocol_expression
2127 static tree cp_parser_objc_selector_expression
2129 static tree cp_parser_objc_expression
2131 static bool cp_parser_objc_selector_p
2133 static tree cp_parser_objc_selector
2135 static tree cp_parser_objc_protocol_refs_opt
2137 static void cp_parser_objc_declaration
2138 (cp_parser *, tree);
2139 static tree cp_parser_objc_statement
2141 static bool cp_parser_objc_valid_prefix_attributes
2142 (cp_parser *, tree *);
2143 static void cp_parser_objc_at_property_declaration
2145 static void cp_parser_objc_at_synthesize_declaration
2147 static void cp_parser_objc_at_dynamic_declaration
2149 static tree cp_parser_objc_struct_declaration
2152 /* Utility Routines */
2154 static tree cp_parser_lookup_name
2155 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2156 static tree cp_parser_lookup_name_simple
2157 (cp_parser *, tree, location_t);
2158 static tree cp_parser_maybe_treat_template_as_class
2160 static bool cp_parser_check_declarator_template_parameters
2161 (cp_parser *, cp_declarator *, location_t);
2162 static bool cp_parser_check_template_parameters
2163 (cp_parser *, unsigned, location_t, cp_declarator *);
2164 static tree cp_parser_simple_cast_expression
2166 static tree cp_parser_global_scope_opt
2167 (cp_parser *, bool);
2168 static bool cp_parser_constructor_declarator_p
2169 (cp_parser *, bool);
2170 static tree cp_parser_function_definition_from_specifiers_and_declarator
2171 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2172 static tree cp_parser_function_definition_after_declarator
2173 (cp_parser *, bool);
2174 static void cp_parser_template_declaration_after_export
2175 (cp_parser *, bool);
2176 static void cp_parser_perform_template_parameter_access_checks
2177 (VEC (deferred_access_check,gc)*);
2178 static tree cp_parser_single_declaration
2179 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2180 static tree cp_parser_functional_cast
2181 (cp_parser *, tree);
2182 static tree cp_parser_save_member_function_body
2183 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2184 static tree cp_parser_save_nsdmi
2186 static tree cp_parser_enclosed_template_argument_list
2188 static void cp_parser_save_default_args
2189 (cp_parser *, tree);
2190 static void cp_parser_late_parsing_for_member
2191 (cp_parser *, tree);
2192 static tree cp_parser_late_parse_one_default_arg
2193 (cp_parser *, tree, tree, tree);
2194 static void cp_parser_late_parsing_nsdmi
2195 (cp_parser *, tree);
2196 static void cp_parser_late_parsing_default_args
2197 (cp_parser *, tree);
2198 static tree cp_parser_sizeof_operand
2199 (cp_parser *, enum rid);
2200 static tree cp_parser_trait_expr
2201 (cp_parser *, enum rid);
2202 static bool cp_parser_declares_only_class_p
2204 static void cp_parser_set_storage_class
2205 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2206 static void cp_parser_set_decl_spec_type
2207 (cp_decl_specifier_seq *, tree, location_t, bool);
2208 static bool cp_parser_friend_p
2209 (const cp_decl_specifier_seq *);
2210 static void cp_parser_required_error
2211 (cp_parser *, required_token, bool);
2212 static cp_token *cp_parser_require
2213 (cp_parser *, enum cpp_ttype, required_token);
2214 static cp_token *cp_parser_require_keyword
2215 (cp_parser *, enum rid, required_token);
2216 static bool cp_parser_token_starts_function_definition_p
2218 static bool cp_parser_next_token_starts_class_definition_p
2220 static bool cp_parser_next_token_ends_template_argument_p
2222 static bool cp_parser_nth_token_starts_template_argument_list_p
2223 (cp_parser *, size_t);
2224 static enum tag_types cp_parser_token_is_class_key
2226 static void cp_parser_check_class_key
2227 (enum tag_types, tree type);
2228 static void cp_parser_check_access_in_redeclaration
2229 (tree type, location_t location);
2230 static bool cp_parser_optional_template_keyword
2232 static void cp_parser_pre_parsed_nested_name_specifier
2234 static bool cp_parser_cache_group
2235 (cp_parser *, enum cpp_ttype, unsigned);
2236 static void cp_parser_parse_tentatively
2238 static void cp_parser_commit_to_tentative_parse
2240 static void cp_parser_abort_tentative_parse
2242 static bool cp_parser_parse_definitely
2244 static inline bool cp_parser_parsing_tentatively
2246 static bool cp_parser_uncommitted_to_tentative_parse_p
2248 static void cp_parser_error
2249 (cp_parser *, const char *);
2250 static void cp_parser_name_lookup_error
2251 (cp_parser *, tree, tree, name_lookup_error, location_t);
2252 static bool cp_parser_simulate_error
2254 static bool cp_parser_check_type_definition
2256 static void cp_parser_check_for_definition_in_return_type
2257 (cp_declarator *, tree, location_t type_location);
2258 static void cp_parser_check_for_invalid_template_id
2259 (cp_parser *, tree, location_t location);
2260 static bool cp_parser_non_integral_constant_expression
2261 (cp_parser *, non_integral_constant);
2262 static void cp_parser_diagnose_invalid_type_name
2263 (cp_parser *, tree, tree, location_t);
2264 static bool cp_parser_parse_and_diagnose_invalid_type_name
2266 static int cp_parser_skip_to_closing_parenthesis
2267 (cp_parser *, bool, bool, bool);
2268 static void cp_parser_skip_to_end_of_statement
2270 static void cp_parser_consume_semicolon_at_end_of_statement
2272 static void cp_parser_skip_to_end_of_block_or_statement
2274 static bool cp_parser_skip_to_closing_brace
2276 static void cp_parser_skip_to_end_of_template_parameter_list
2278 static void cp_parser_skip_to_pragma_eol
2279 (cp_parser*, cp_token *);
2280 static bool cp_parser_error_occurred
2282 static bool cp_parser_allow_gnu_extensions_p
2284 static bool cp_parser_is_pure_string_literal
2286 static bool cp_parser_is_string_literal
2288 static bool cp_parser_is_keyword
2289 (cp_token *, enum rid);
2290 static tree cp_parser_make_typename_type
2291 (cp_parser *, tree, tree, location_t location);
2292 static cp_declarator * cp_parser_make_indirect_declarator
2293 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2295 /* Returns nonzero if we are parsing tentatively. */
2298 cp_parser_parsing_tentatively (cp_parser* parser)
2300 return parser->context->next != NULL;
2303 /* Returns nonzero if TOKEN is a string literal. */
2306 cp_parser_is_pure_string_literal (cp_token* token)
2308 return (token->type == CPP_STRING ||
2309 token->type == CPP_STRING16 ||
2310 token->type == CPP_STRING32 ||
2311 token->type == CPP_WSTRING ||
2312 token->type == CPP_UTF8STRING);
2315 /* Returns nonzero if TOKEN is a string literal
2316 of a user-defined string literal. */
2319 cp_parser_is_string_literal (cp_token* token)
2321 return (cp_parser_is_pure_string_literal (token) ||
2322 token->type == CPP_STRING_USERDEF ||
2323 token->type == CPP_STRING16_USERDEF ||
2324 token->type == CPP_STRING32_USERDEF ||
2325 token->type == CPP_WSTRING_USERDEF ||
2326 token->type == CPP_UTF8STRING_USERDEF);
2329 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2332 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2334 return token->keyword == keyword;
2337 /* If not parsing tentatively, issue a diagnostic of the form
2338 FILE:LINE: MESSAGE before TOKEN
2339 where TOKEN is the next token in the input stream. MESSAGE
2340 (specified by the caller) is usually of the form "expected
2344 cp_parser_error (cp_parser* parser, const char* gmsgid)
2346 if (!cp_parser_simulate_error (parser))
2348 cp_token *token = cp_lexer_peek_token (parser->lexer);
2349 /* This diagnostic makes more sense if it is tagged to the line
2350 of the token we just peeked at. */
2351 cp_lexer_set_source_position_from_token (token);
2353 if (token->type == CPP_PRAGMA)
2355 error_at (token->location,
2356 "%<#pragma%> is not allowed here");
2357 cp_parser_skip_to_pragma_eol (parser, token);
2361 c_parse_error (gmsgid,
2362 /* Because c_parser_error does not understand
2363 CPP_KEYWORD, keywords are treated like
2365 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2366 token->u.value, token->flags);
2370 /* Issue an error about name-lookup failing. NAME is the
2371 IDENTIFIER_NODE DECL is the result of
2372 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2373 the thing that we hoped to find. */
2376 cp_parser_name_lookup_error (cp_parser* parser,
2379 name_lookup_error desired,
2380 location_t location)
2382 /* If name lookup completely failed, tell the user that NAME was not
2384 if (decl == error_mark_node)
2386 if (parser->scope && parser->scope != global_namespace)
2387 error_at (location, "%<%E::%E%> has not been declared",
2388 parser->scope, name);
2389 else if (parser->scope == global_namespace)
2390 error_at (location, "%<::%E%> has not been declared", name);
2391 else if (parser->object_scope
2392 && !CLASS_TYPE_P (parser->object_scope))
2393 error_at (location, "request for member %qE in non-class type %qT",
2394 name, parser->object_scope);
2395 else if (parser->object_scope)
2396 error_at (location, "%<%T::%E%> has not been declared",
2397 parser->object_scope, name);
2399 error_at (location, "%qE has not been declared", name);
2401 else if (parser->scope && parser->scope != global_namespace)
2406 error_at (location, "%<%E::%E%> is not a type",
2407 parser->scope, name);
2410 error_at (location, "%<%E::%E%> is not a class or namespace",
2411 parser->scope, name);
2415 "%<%E::%E%> is not a class, namespace, or enumeration",
2416 parser->scope, name);
2423 else if (parser->scope == global_namespace)
2428 error_at (location, "%<::%E%> is not a type", name);
2431 error_at (location, "%<::%E%> is not a class or namespace", name);
2435 "%<::%E%> is not a class, namespace, or enumeration",
2447 error_at (location, "%qE is not a type", name);
2450 error_at (location, "%qE is not a class or namespace", name);
2454 "%qE is not a class, namespace, or enumeration", name);
2462 /* If we are parsing tentatively, remember that an error has occurred
2463 during this tentative parse. Returns true if the error was
2464 simulated; false if a message should be issued by the caller. */
2467 cp_parser_simulate_error (cp_parser* parser)
2469 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2471 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2477 /* Check for repeated decl-specifiers. */
2480 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2481 location_t location)
2485 for (ds = ds_first; ds != ds_last; ++ds)
2487 unsigned count = decl_specs->specs[ds];
2490 /* The "long" specifier is a special case because of "long long". */
2494 error_at (location, "%<long long long%> is too long for GCC");
2496 pedwarn_cxx98 (location, OPT_Wlong_long,
2497 "ISO C++ 1998 does not support %<long long%>");
2501 static const char *const decl_spec_names[] = {
2519 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2524 /* This function is called when a type is defined. If type
2525 definitions are forbidden at this point, an error message is
2529 cp_parser_check_type_definition (cp_parser* parser)
2531 /* If types are forbidden here, issue a message. */
2532 if (parser->type_definition_forbidden_message)
2534 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2535 in the message need to be interpreted. */
2536 error (parser->type_definition_forbidden_message);
2542 /* This function is called when the DECLARATOR is processed. The TYPE
2543 was a type defined in the decl-specifiers. If it is invalid to
2544 define a type in the decl-specifiers for DECLARATOR, an error is
2545 issued. TYPE_LOCATION is the location of TYPE and is used
2546 for error reporting. */
2549 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2550 tree type, location_t type_location)
2552 /* [dcl.fct] forbids type definitions in return types.
2553 Unfortunately, it's not easy to know whether or not we are
2554 processing a return type until after the fact. */
2556 && (declarator->kind == cdk_pointer
2557 || declarator->kind == cdk_reference
2558 || declarator->kind == cdk_ptrmem))
2559 declarator = declarator->declarator;
2561 && declarator->kind == cdk_function)
2563 error_at (type_location,
2564 "new types may not be defined in a return type");
2565 inform (type_location,
2566 "(perhaps a semicolon is missing after the definition of %qT)",
2571 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2572 "<" in any valid C++ program. If the next token is indeed "<",
2573 issue a message warning the user about what appears to be an
2574 invalid attempt to form a template-id. LOCATION is the location
2575 of the type-specifier (TYPE) */
2578 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2579 tree type, location_t location)
2581 cp_token_position start = 0;
2583 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2586 error_at (location, "%qT is not a template", type);
2587 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2588 error_at (location, "%qE is not a template", type);
2590 error_at (location, "invalid template-id");
2591 /* Remember the location of the invalid "<". */
2592 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2593 start = cp_lexer_token_position (parser->lexer, true);
2594 /* Consume the "<". */
2595 cp_lexer_consume_token (parser->lexer);
2596 /* Parse the template arguments. */
2597 cp_parser_enclosed_template_argument_list (parser);
2598 /* Permanently remove the invalid template arguments so that
2599 this error message is not issued again. */
2601 cp_lexer_purge_tokens_after (parser->lexer, start);
2605 /* If parsing an integral constant-expression, issue an error message
2606 about the fact that THING appeared and return true. Otherwise,
2607 return false. In either case, set
2608 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2611 cp_parser_non_integral_constant_expression (cp_parser *parser,
2612 non_integral_constant thing)
2614 parser->non_integral_constant_expression_p = true;
2615 if (parser->integral_constant_expression_p)
2617 if (!parser->allow_non_integral_constant_expression_p)
2619 const char *msg = NULL;
2623 error ("floating-point literal "
2624 "cannot appear in a constant-expression");
2627 error ("a cast to a type other than an integral or "
2628 "enumeration type cannot appear in a "
2629 "constant-expression");
2632 error ("%<typeid%> operator "
2633 "cannot appear in a constant-expression");
2636 error ("non-constant compound literals "
2637 "cannot appear in a constant-expression");
2640 error ("a function call "
2641 "cannot appear in a constant-expression");
2644 error ("an increment "
2645 "cannot appear in a constant-expression");
2648 error ("an decrement "
2649 "cannot appear in a constant-expression");
2652 error ("an array reference "
2653 "cannot appear in a constant-expression");
2655 case NIC_ADDR_LABEL:
2656 error ("the address of a label "
2657 "cannot appear in a constant-expression");
2659 case NIC_OVERLOADED:
2660 error ("calls to overloaded operators "
2661 "cannot appear in a constant-expression");
2663 case NIC_ASSIGNMENT:
2664 error ("an assignment cannot appear in a constant-expression");
2667 error ("a comma operator "
2668 "cannot appear in a constant-expression");
2670 case NIC_CONSTRUCTOR:
2671 error ("a call to a constructor "
2672 "cannot appear in a constant-expression");
2678 msg = "__FUNCTION__";
2680 case NIC_PRETTY_FUNC:
2681 msg = "__PRETTY_FUNCTION__";
2701 case NIC_PREINCREMENT:
2704 case NIC_PREDECREMENT:
2717 error ("%qs cannot appear in a constant-expression", msg);
2724 /* Emit a diagnostic for an invalid type name. SCOPE is the
2725 qualifying scope (or NULL, if none) for ID. This function commits
2726 to the current active tentative parse, if any. (Otherwise, the
2727 problematic construct might be encountered again later, resulting
2728 in duplicate error messages.) LOCATION is the location of ID. */
2731 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2732 tree scope, tree id,
2733 location_t location)
2735 tree decl, old_scope;
2736 cp_parser_commit_to_tentative_parse (parser);
2737 /* Try to lookup the identifier. */
2738 old_scope = parser->scope;
2739 parser->scope = scope;
2740 decl = cp_parser_lookup_name_simple (parser, id, location);
2741 parser->scope = old_scope;
2742 /* If the lookup found a template-name, it means that the user forgot
2743 to specify an argument list. Emit a useful error message. */
2744 if (TREE_CODE (decl) == TEMPLATE_DECL)
2746 "invalid use of template-name %qE without an argument list",
2748 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2749 error_at (location, "invalid use of destructor %qD as a type", id);
2750 else if (TREE_CODE (decl) == TYPE_DECL)
2751 /* Something like 'unsigned A a;' */
2752 error_at (location, "invalid combination of multiple type-specifiers");
2753 else if (!parser->scope)
2755 /* Issue an error message. */
2756 error_at (location, "%qE does not name a type", id);
2757 /* If we're in a template class, it's possible that the user was
2758 referring to a type from a base class. For example:
2760 template <typename T> struct A { typedef T X; };
2761 template <typename T> struct B : public A<T> { X x; };
2763 The user should have said "typename A<T>::X". */
2764 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2765 inform (location, "C++11 %<constexpr%> only available with "
2766 "-std=c++11 or -std=gnu++11");
2767 else if (processing_template_decl && current_class_type
2768 && TYPE_BINFO (current_class_type))
2772 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2776 tree base_type = BINFO_TYPE (b);
2777 if (CLASS_TYPE_P (base_type)
2778 && dependent_type_p (base_type))
2781 /* Go from a particular instantiation of the
2782 template (which will have an empty TYPE_FIELDs),
2783 to the main version. */
2784 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2785 for (field = TYPE_FIELDS (base_type);
2787 field = DECL_CHAIN (field))
2788 if (TREE_CODE (field) == TYPE_DECL
2789 && DECL_NAME (field) == id)
2792 "(perhaps %<typename %T::%E%> was intended)",
2793 BINFO_TYPE (b), id);
2802 /* Here we diagnose qualified-ids where the scope is actually correct,
2803 but the identifier does not resolve to a valid type name. */
2804 else if (parser->scope != error_mark_node)
2806 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2807 error_at (location, "%qE in namespace %qE does not name a type",
2809 else if (CLASS_TYPE_P (parser->scope)
2810 && constructor_name_p (id, parser->scope))
2813 error_at (location, "%<%T::%E%> names the constructor, not"
2814 " the type", parser->scope, id);
2815 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2816 error_at (location, "and %qT has no template constructors",
2819 else if (TYPE_P (parser->scope)
2820 && dependent_scope_p (parser->scope))
2821 error_at (location, "need %<typename%> before %<%T::%E%> because "
2822 "%qT is a dependent scope",
2823 parser->scope, id, parser->scope);
2824 else if (TYPE_P (parser->scope))
2825 error_at (location, "%qE in %q#T does not name a type",
2832 /* Check for a common situation where a type-name should be present,
2833 but is not, and issue a sensible error message. Returns true if an
2834 invalid type-name was detected.
2836 The situation handled by this function are variable declarations of the
2837 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2838 Usually, `ID' should name a type, but if we got here it means that it
2839 does not. We try to emit the best possible error message depending on
2840 how exactly the id-expression looks like. */
2843 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2846 cp_token *token = cp_lexer_peek_token (parser->lexer);
2848 /* Avoid duplicate error about ambiguous lookup. */
2849 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2851 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2852 if (next->type == CPP_NAME && next->ambiguous_p)
2856 cp_parser_parse_tentatively (parser);
2857 id = cp_parser_id_expression (parser,
2858 /*template_keyword_p=*/false,
2859 /*check_dependency_p=*/true,
2860 /*template_p=*/NULL,
2861 /*declarator_p=*/true,
2862 /*optional_p=*/false);
2863 /* If the next token is a (, this is a function with no explicit return
2864 type, i.e. constructor, destructor or conversion op. */
2865 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2866 || TREE_CODE (id) == TYPE_DECL)
2868 cp_parser_abort_tentative_parse (parser);
2871 if (!cp_parser_parse_definitely (parser))
2874 /* Emit a diagnostic for the invalid type. */
2875 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2876 id, token->location);
2878 /* If we aren't in the middle of a declarator (i.e. in a
2879 parameter-declaration-clause), skip to the end of the declaration;
2880 there's no point in trying to process it. */
2881 if (!parser->in_declarator_p)
2882 cp_parser_skip_to_end_of_block_or_statement (parser);
2886 /* Consume tokens up to, and including, the next non-nested closing `)'.
2887 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2888 are doing error recovery. Returns -1 if OR_COMMA is true and we
2889 found an unnested comma. */
2892 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2897 unsigned paren_depth = 0;
2898 unsigned brace_depth = 0;
2899 unsigned square_depth = 0;
2901 if (recovering && !or_comma
2902 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2907 cp_token * token = cp_lexer_peek_token (parser->lexer);
2909 switch (token->type)
2912 case CPP_PRAGMA_EOL:
2913 /* If we've run out of tokens, then there is no closing `)'. */
2916 /* This is good for lambda expression capture-lists. */
2917 case CPP_OPEN_SQUARE:
2920 case CPP_CLOSE_SQUARE:
2921 if (!square_depth--)
2926 /* This matches the processing in skip_to_end_of_statement. */
2931 case CPP_OPEN_BRACE:
2934 case CPP_CLOSE_BRACE:
2940 if (recovering && or_comma && !brace_depth && !paren_depth
2945 case CPP_OPEN_PAREN:
2950 case CPP_CLOSE_PAREN:
2951 if (!brace_depth && !paren_depth--)
2954 cp_lexer_consume_token (parser->lexer);
2963 /* Consume the token. */
2964 cp_lexer_consume_token (parser->lexer);
2968 /* Consume tokens until we reach the end of the current statement.
2969 Normally, that will be just before consuming a `;'. However, if a
2970 non-nested `}' comes first, then we stop before consuming that. */
2973 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2975 unsigned nesting_depth = 0;
2979 cp_token *token = cp_lexer_peek_token (parser->lexer);
2981 switch (token->type)
2984 case CPP_PRAGMA_EOL:
2985 /* If we've run out of tokens, stop. */
2989 /* If the next token is a `;', we have reached the end of the
2995 case CPP_CLOSE_BRACE:
2996 /* If this is a non-nested '}', stop before consuming it.
2997 That way, when confronted with something like:
3001 we stop before consuming the closing '}', even though we
3002 have not yet reached a `;'. */
3003 if (nesting_depth == 0)
3006 /* If it is the closing '}' for a block that we have
3007 scanned, stop -- but only after consuming the token.
3013 we will stop after the body of the erroneously declared
3014 function, but before consuming the following `typedef'
3016 if (--nesting_depth == 0)
3018 cp_lexer_consume_token (parser->lexer);
3022 case CPP_OPEN_BRACE:
3030 /* Consume the token. */
3031 cp_lexer_consume_token (parser->lexer);
3035 /* This function is called at the end of a statement or declaration.
3036 If the next token is a semicolon, it is consumed; otherwise, error
3037 recovery is attempted. */
3040 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3042 /* Look for the trailing `;'. */
3043 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3045 /* If there is additional (erroneous) input, skip to the end of
3047 cp_parser_skip_to_end_of_statement (parser);
3048 /* If the next token is now a `;', consume it. */
3049 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3050 cp_lexer_consume_token (parser->lexer);
3054 /* Skip tokens until we have consumed an entire block, or until we
3055 have consumed a non-nested `;'. */
3058 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3060 int nesting_depth = 0;
3062 while (nesting_depth >= 0)
3064 cp_token *token = cp_lexer_peek_token (parser->lexer);
3066 switch (token->type)
3069 case CPP_PRAGMA_EOL:
3070 /* If we've run out of tokens, stop. */
3074 /* Stop if this is an unnested ';'. */
3079 case CPP_CLOSE_BRACE:
3080 /* Stop if this is an unnested '}', or closes the outermost
3083 if (nesting_depth < 0)
3089 case CPP_OPEN_BRACE:
3098 /* Consume the token. */
3099 cp_lexer_consume_token (parser->lexer);
3103 /* Skip tokens until a non-nested closing curly brace is the next
3104 token, or there are no more tokens. Return true in the first case,
3108 cp_parser_skip_to_closing_brace (cp_parser *parser)
3110 unsigned nesting_depth = 0;
3114 cp_token *token = cp_lexer_peek_token (parser->lexer);
3116 switch (token->type)
3119 case CPP_PRAGMA_EOL:
3120 /* If we've run out of tokens, stop. */
3123 case CPP_CLOSE_BRACE:
3124 /* If the next token is a non-nested `}', then we have reached
3125 the end of the current block. */
3126 if (nesting_depth-- == 0)
3130 case CPP_OPEN_BRACE:
3131 /* If it the next token is a `{', then we are entering a new
3132 block. Consume the entire block. */
3140 /* Consume the token. */
3141 cp_lexer_consume_token (parser->lexer);
3145 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
3146 parameter is the PRAGMA token, allowing us to purge the entire pragma
3150 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3154 parser->lexer->in_pragma = false;
3157 token = cp_lexer_consume_token (parser->lexer);
3158 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3160 /* Ensure that the pragma is not parsed again. */
3161 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3164 /* Require pragma end of line, resyncing with it as necessary. The
3165 arguments are as for cp_parser_skip_to_pragma_eol. */
3168 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3170 parser->lexer->in_pragma = false;
3171 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3172 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3175 /* This is a simple wrapper around make_typename_type. When the id is
3176 an unresolved identifier node, we can provide a superior diagnostic
3177 using cp_parser_diagnose_invalid_type_name. */
3180 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3181 tree id, location_t id_location)
3184 if (TREE_CODE (id) == IDENTIFIER_NODE)
3186 result = make_typename_type (scope, id, typename_type,
3187 /*complain=*/tf_none);
3188 if (result == error_mark_node)
3189 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3192 return make_typename_type (scope, id, typename_type, tf_error);
3195 /* This is a wrapper around the
3196 make_{pointer,ptrmem,reference}_declarator functions that decides
3197 which one to call based on the CODE and CLASS_TYPE arguments. The
3198 CODE argument should be one of the values returned by
3199 cp_parser_ptr_operator. */
3200 static cp_declarator *
3201 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3202 cp_cv_quals cv_qualifiers,
3203 cp_declarator *target)
3205 if (code == ERROR_MARK)
3206 return cp_error_declarator;
3208 if (code == INDIRECT_REF)
3209 if (class_type == NULL_TREE)
3210 return make_pointer_declarator (cv_qualifiers, target);
3212 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3213 else if (code == ADDR_EXPR && class_type == NULL_TREE)
3214 return make_reference_declarator (cv_qualifiers, target, false);
3215 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3216 return make_reference_declarator (cv_qualifiers, target, true);
3220 /* Create a new C++ parser. */
3223 cp_parser_new (void)
3229 /* cp_lexer_new_main is called before doing GC allocation because
3230 cp_lexer_new_main might load a PCH file. */
3231 lexer = cp_lexer_new_main ();
3233 /* Initialize the binops_by_token so that we can get the tree
3234 directly from the token. */
3235 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3236 binops_by_token[binops[i].token_type] = binops[i];
3238 parser = ggc_alloc_cleared_cp_parser ();
3239 parser->lexer = lexer;
3240 parser->context = cp_parser_context_new (NULL);
3242 /* For now, we always accept GNU extensions. */
3243 parser->allow_gnu_extensions_p = 1;
3245 /* The `>' token is a greater-than operator, not the end of a
3247 parser->greater_than_is_operator_p = true;
3249 parser->default_arg_ok_p = true;
3251 /* We are not parsing a constant-expression. */
3252 parser->integral_constant_expression_p = false;
3253 parser->allow_non_integral_constant_expression_p = false;
3254 parser->non_integral_constant_expression_p = false;
3256 /* Local variable names are not forbidden. */
3257 parser->local_variables_forbidden_p = false;
3259 /* We are not processing an `extern "C"' declaration. */
3260 parser->in_unbraced_linkage_specification_p = false;
3262 /* We are not processing a declarator. */
3263 parser->in_declarator_p = false;
3265 /* We are not processing a template-argument-list. */
3266 parser->in_template_argument_list_p = false;
3268 /* We are not in an iteration statement. */
3269 parser->in_statement = 0;
3271 /* We are not in a switch statement. */
3272 parser->in_switch_statement_p = false;
3274 /* We are not parsing a type-id inside an expression. */
3275 parser->in_type_id_in_expr_p = false;
3277 /* Declarations aren't implicitly extern "C". */
3278 parser->implicit_extern_c = false;
3280 /* String literals should be translated to the execution character set. */
3281 parser->translate_strings_p = true;
3283 /* We are not parsing a function body. */
3284 parser->in_function_body = false;
3286 /* We can correct until told otherwise. */
3287 parser->colon_corrects_to_scope_p = true;
3289 /* The unparsed function queue is empty. */
3290 push_unparsed_function_queues (parser);
3292 /* There are no classes being defined. */
3293 parser->num_classes_being_defined = 0;
3295 /* No template parameters apply. */
3296 parser->num_template_parameter_lists = 0;
3301 /* Create a cp_lexer structure which will emit the tokens in CACHE
3302 and push it onto the parser's lexer stack. This is used for delayed
3303 parsing of in-class method bodies and default arguments, and should
3304 not be confused with tentative parsing. */
3306 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3308 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3309 lexer->next = parser->lexer;
3310 parser->lexer = lexer;
3312 /* Move the current source position to that of the first token in the
3314 cp_lexer_set_source_position_from_token (lexer->next_token);
3317 /* Pop the top lexer off the parser stack. This is never used for the
3318 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3320 cp_parser_pop_lexer (cp_parser *parser)
3322 cp_lexer *lexer = parser->lexer;
3323 parser->lexer = lexer->next;
3324 cp_lexer_destroy (lexer);
3326 /* Put the current source position back where it was before this
3327 lexer was pushed. */
3328 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3331 /* Lexical conventions [gram.lex] */
3333 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3337 cp_parser_identifier (cp_parser* parser)
3341 /* Look for the identifier. */
3342 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3343 /* Return the value. */
3344 return token ? token->u.value : error_mark_node;
3347 /* Parse a sequence of adjacent string constants. Returns a
3348 TREE_STRING representing the combined, nul-terminated string
3349 constant. If TRANSLATE is true, translate the string to the
3350 execution character set. If WIDE_OK is true, a wide string is
3353 C++98 [lex.string] says that if a narrow string literal token is
3354 adjacent to a wide string literal token, the behavior is undefined.
3355 However, C99 6.4.5p4 says that this results in a wide string literal.
3356 We follow C99 here, for consistency with the C front end.
3358 This code is largely lifted from lex_string() in c-lex.c.
3360 FUTURE: ObjC++ will need to handle @-strings here. */
3362 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3366 struct obstack str_ob;
3367 cpp_string str, istr, *strs;
3369 enum cpp_ttype type, curr_type;
3370 int have_suffix_p = 0;
3372 tree suffix_id = NULL_TREE;
3373 bool curr_tok_is_userdef_p = false;
3375 tok = cp_lexer_peek_token (parser->lexer);
3376 if (!cp_parser_is_string_literal (tok))
3378 cp_parser_error (parser, "expected string-literal");
3379 return error_mark_node;
3382 if (cpp_userdef_string_p (tok->type))
3384 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3385 curr_type = cpp_userdef_string_remove_type (tok->type);
3386 curr_tok_is_userdef_p = true;
3390 string_tree = tok->u.value;
3391 curr_type = tok->type;
3395 /* Try to avoid the overhead of creating and destroying an obstack
3396 for the common case of just one string. */
3397 if (!cp_parser_is_string_literal
3398 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3400 cp_lexer_consume_token (parser->lexer);
3402 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3403 str.len = TREE_STRING_LENGTH (string_tree);
3406 if (curr_tok_is_userdef_p)
3408 suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3410 curr_type = cpp_userdef_string_remove_type (tok->type);
3413 curr_type = tok->type;
3419 gcc_obstack_init (&str_ob);
3424 cp_lexer_consume_token (parser->lexer);
3426 str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3427 str.len = TREE_STRING_LENGTH (string_tree);
3429 if (curr_tok_is_userdef_p)
3431 tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3432 if (have_suffix_p == 0)
3434 suffix_id = curr_suffix_id;
3437 else if (have_suffix_p == 1
3438 && curr_suffix_id != suffix_id)
3440 error ("inconsistent user-defined literal suffixes"
3441 " %qD and %qD in string literal",
3442 suffix_id, curr_suffix_id);
3445 curr_type = cpp_userdef_string_remove_type (tok->type);
3448 curr_type = tok->type;
3450 if (type != curr_type)
3452 if (type == CPP_STRING)
3454 else if (curr_type != CPP_STRING)
3455 error_at (tok->location,
3456 "unsupported non-standard concatenation "
3457 "of string literals");
3460 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3462 tok = cp_lexer_peek_token (parser->lexer);
3463 if (cpp_userdef_string_p (tok->type))
3465 string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3466 curr_type = cpp_userdef_string_remove_type (tok->type);
3467 curr_tok_is_userdef_p = true;
3471 string_tree = tok->u.value;
3472 curr_type = tok->type;
3473 curr_tok_is_userdef_p = false;
3476 while (cp_parser_is_string_literal (tok));
3478 strs = (cpp_string *) obstack_finish (&str_ob);
3481 if (type != CPP_STRING && !wide_ok)
3483 cp_parser_error (parser, "a wide string is invalid in this context");
3487 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3488 (parse_in, strs, count, &istr, type))
3490 value = build_string (istr.len, (const char *)istr.text);
3491 free (CONST_CAST (unsigned char *, istr.text));
3497 case CPP_UTF8STRING:
3498 TREE_TYPE (value) = char_array_type_node;
3501 TREE_TYPE (value) = char16_array_type_node;
3504 TREE_TYPE (value) = char32_array_type_node;
3507 TREE_TYPE (value) = wchar_array_type_node;
3511 value = fix_string_type (value);
3515 tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3516 tok->u.value = literal;
3517 return cp_parser_userdef_string_literal (tok);
3521 /* cpp_interpret_string has issued an error. */
3522 value = error_mark_node;
3525 obstack_free (&str_ob, 0);
3530 /* Parse a user-defined char constant. Returns a call to a user-defined
3531 literal operator taking the character as an argument. */
3534 cp_parser_userdef_char_literal (cp_parser *parser)
3536 cp_token *token = NULL;
3537 tree literal, suffix_id, value;
3542 token = cp_lexer_consume_token (parser->lexer);
3543 literal = token->u.value;
3544 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3545 value = USERDEF_LITERAL_VALUE (literal);
3546 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3548 /* Build up a call to the user-defined operator */
3549 /* Lookup the name we got back from the id-expression. */
3550 vec = make_tree_vector ();
3551 VEC_safe_push (tree, gc, vec, value);
3552 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3553 if (!decl || decl == error_mark_node)
3555 error ("unable to find user-defined character literal operator %qD",
3557 release_tree_vector (vec);
3558 return error_mark_node;
3560 result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3561 release_tree_vector (vec);
3566 /* A subroutine of cp_parser_userdef_numeric_literal to
3567 create a char... template parameter pack from a string node. */
3570 make_char_string_pack (tree value)
3573 tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3574 const char *str = TREE_STRING_POINTER (value);
3575 int i, len = TREE_STRING_LENGTH (value) - 1;
3576 tree argvec = make_tree_vec (1);
3578 /* Fill in CHARVEC with all of the parameters. */
3579 charvec = make_tree_vec (len);
3580 for (i = 0; i < len; ++i)
3581 TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3583 /* Build the argument packs. */
3584 SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3585 TREE_TYPE (argpack) = char_type_node;
3587 TREE_VEC_ELT (argvec, 0) = argpack;
3592 /* Parse a user-defined numeric constant. returns a call to a user-defined
3593 literal operator. */
3596 cp_parser_userdef_numeric_literal (cp_parser *parser)
3598 cp_token *token = NULL;
3599 tree literal, suffix_id, value, num_string;
3601 tree result = error_mark_node;
3604 token = cp_lexer_consume_token (parser->lexer);
3605 literal = token->u.value;
3606 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3607 value = USERDEF_LITERAL_VALUE (literal);
3608 num_string = USERDEF_LITERAL_NUM_STRING (literal);
3609 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3611 /* Build up a call to the user-defined operator */
3612 /* Lookup the name we got back from the id-expression. */
3613 /* Try to find the literal operator by finishing the call expression
3614 with the numeric argument. */
3615 args = make_tree_vector ();
3616 VEC_safe_push (tree, gc, args, value);
3617 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3618 if (decl && decl != error_mark_node)
3620 result = finish_call_expr (decl, &args, false, true, tf_none);
3621 if (result != error_mark_node)
3623 release_tree_vector (args);
3627 release_tree_vector (args);
3629 /* If the numeric argument didn't work, look for a raw literal
3630 operator taking a const char* argument consisting of the number
3631 in string format. */
3632 args = make_tree_vector ();
3633 VEC_safe_push (tree, gc, args, num_string);
3634 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3635 if (decl && decl != error_mark_node)
3637 result = finish_call_expr (decl, &args, false, true, tf_none);
3638 if (result != error_mark_node)
3640 release_tree_vector (args);
3644 release_tree_vector (args);
3646 /* If the raw literal didn't work, look for a non-type template
3647 function with parameter pack char.... Call the function with
3648 template parameter characters representing the number. */
3649 args = make_tree_vector ();
3650 decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3651 if (decl && decl != error_mark_node)
3653 tree tmpl_args = make_char_string_pack (num_string);
3654 decl = lookup_template_function (decl, tmpl_args);
3655 result = finish_call_expr (decl, &args, false, true, tf_none);
3656 if (result != error_mark_node)
3658 release_tree_vector (args);
3662 release_tree_vector (args);
3664 if (result == error_mark_node)
3665 error ("unable to find user-defined numeric literal operator %qD", name);
3670 /* Parse a user-defined string constant. Returns a call to a user-defined
3671 literal operator taking a character pointer and the length of the string
3675 cp_parser_userdef_string_literal (cp_token *token)
3677 tree literal, suffix_id, value;
3683 literal = token->u.value;
3684 suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3685 name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3686 value = USERDEF_LITERAL_VALUE (literal);
3687 len = TREE_STRING_LENGTH (value)
3688 / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3689 /* Build up a call to the user-defined operator */
3690 /* Lookup the name we got back from the id-expression. */
3691 vec = make_tree_vector ();
3692 VEC_safe_push (tree, gc, vec, value);
3693 VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3694 decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3695 if (!decl || decl == error_mark_node)
3697 error ("unable to find user-defined string literal operator %qD", name);
3698 release_tree_vector (vec);
3699 return error_mark_node;
3701 result = finish_call_expr (decl, &vec, false, true, tf_none);
3702 if (result == error_mark_node)
3703 error ("unable to find valid user-defined string literal operator %qD."
3704 " Possible missing length argument in string literal operator.",
3706 release_tree_vector (vec);
3712 /* Basic concepts [gram.basic] */
3714 /* Parse a translation-unit.
3717 declaration-seq [opt]
3719 Returns TRUE if all went well. */
3722 cp_parser_translation_unit (cp_parser* parser)
3724 /* The address of the first non-permanent object on the declarator
3726 static void *declarator_obstack_base;
3730 /* Create the declarator obstack, if necessary. */
3731 if (!cp_error_declarator)
3733 gcc_obstack_init (&declarator_obstack);
3734 /* Create the error declarator. */
3735 cp_error_declarator = make_declarator (cdk_error);
3736 /* Create the empty parameter list. */
3737 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3738 /* Remember where the base of the declarator obstack lies. */
3739 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3742 cp_parser_declaration_seq_opt (parser);
3744 /* If there are no tokens left then all went well. */
3745 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3747 /* Get rid of the token array; we don't need it any more. */
3748 cp_lexer_destroy (parser->lexer);
3749 parser->lexer = NULL;
3751 /* This file might have been a context that's implicitly extern
3752 "C". If so, pop the lang context. (Only relevant for PCH.) */
3753 if (parser->implicit_extern_c)
3755 pop_lang_context ();
3756 parser->implicit_extern_c = false;
3760 finish_translation_unit ();
3766 cp_parser_error (parser, "expected declaration");
3770 /* Make sure the declarator obstack was fully cleaned up. */
3771 gcc_assert (obstack_next_free (&declarator_obstack)
3772 == declarator_obstack_base);
3774 /* All went well. */
3778 /* Expressions [gram.expr] */
3780 /* Parse a primary-expression.
3791 ( compound-statement )
3792 __builtin_va_arg ( assignment-expression , type-id )
3793 __builtin_offsetof ( type-id , offsetof-expression )
3796 __has_nothrow_assign ( type-id )
3797 __has_nothrow_constructor ( type-id )
3798 __has_nothrow_copy ( type-id )
3799 __has_trivial_assign ( type-id )
3800 __has_trivial_constructor ( type-id )
3801 __has_trivial_copy ( type-id )
3802 __has_trivial_destructor ( type-id )
3803 __has_virtual_destructor ( type-id )
3804 __is_abstract ( type-id )
3805 __is_base_of ( type-id , type-id )
3806 __is_class ( type-id )
3807 __is_convertible_to ( type-id , type-id )
3808 __is_empty ( type-id )
3809 __is_enum ( type-id )
3810 __is_literal_type ( type-id )
3811 __is_pod ( type-id )
3812 __is_polymorphic ( type-id )
3813 __is_std_layout ( type-id )
3814 __is_trivial ( type-id )
3815 __is_union ( type-id )
3817 Objective-C++ Extension:
3825 ADDRESS_P is true iff this expression was immediately preceded by
3826 "&" and therefore might denote a pointer-to-member. CAST_P is true
3827 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3828 true iff this expression is a template argument.
3830 Returns a representation of the expression. Upon return, *IDK
3831 indicates what kind of id-expression (if any) was present. */
3834 cp_parser_primary_expression (cp_parser *parser,
3837 bool template_arg_p,
3840 cp_token *token = NULL;
3842 /* Assume the primary expression is not an id-expression. */
3843 *idk = CP_ID_KIND_NONE;
3845 /* Peek at the next token. */
3846 token = cp_lexer_peek_token (parser->lexer);
3847 switch (token->type)
3856 user-defined-literal */
3862 if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3863 return cp_parser_userdef_numeric_literal (parser);
3864 token = cp_lexer_consume_token (parser->lexer);
3865 if (TREE_CODE (token->u.value) == FIXED_CST)
3867 error_at (token->location,
3868 "fixed-point types not supported in C++");
3869 return error_mark_node;
3871 /* Floating-point literals are only allowed in an integral
3872 constant expression if they are cast to an integral or
3873 enumeration type. */
3874 if (TREE_CODE (token->u.value) == REAL_CST
3875 && parser->integral_constant_expression_p
3878 /* CAST_P will be set even in invalid code like "int(2.7 +
3879 ...)". Therefore, we have to check that the next token
3880 is sure to end the cast. */
3883 cp_token *next_token;
3885 next_token = cp_lexer_peek_token (parser->lexer);
3886 if (/* The comma at the end of an
3887 enumerator-definition. */
3888 next_token->type != CPP_COMMA
3889 /* The curly brace at the end of an enum-specifier. */
3890 && next_token->type != CPP_CLOSE_BRACE
3891 /* The end of a statement. */
3892 && next_token->type != CPP_SEMICOLON
3893 /* The end of the cast-expression. */
3894 && next_token->type != CPP_CLOSE_PAREN
3895 /* The end of an array bound. */
3896 && next_token->type != CPP_CLOSE_SQUARE
3897 /* The closing ">" in a template-argument-list. */
3898 && (next_token->type != CPP_GREATER
3899 || parser->greater_than_is_operator_p)
3900 /* C++0x only: A ">>" treated like two ">" tokens,
3901 in a template-argument-list. */
3902 && (next_token->type != CPP_RSHIFT
3903 || (cxx_dialect == cxx98)
3904 || parser->greater_than_is_operator_p))
3908 /* If we are within a cast, then the constraint that the
3909 cast is to an integral or enumeration type will be
3910 checked at that point. If we are not within a cast, then
3911 this code is invalid. */
3913 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3915 return token->u.value;
3917 case CPP_CHAR_USERDEF:
3918 case CPP_CHAR16_USERDEF:
3919 case CPP_CHAR32_USERDEF:
3920 case CPP_WCHAR_USERDEF:
3921 return cp_parser_userdef_char_literal (parser);
3927 case CPP_UTF8STRING:
3928 case CPP_STRING_USERDEF:
3929 case CPP_STRING16_USERDEF:
3930 case CPP_STRING32_USERDEF:
3931 case CPP_WSTRING_USERDEF:
3932 case CPP_UTF8STRING_USERDEF:
3933 /* ??? Should wide strings be allowed when parser->translate_strings_p
3934 is false (i.e. in attributes)? If not, we can kill the third
3935 argument to cp_parser_string_literal. */
3936 return cp_parser_string_literal (parser,
3937 parser->translate_strings_p,
3940 case CPP_OPEN_PAREN:
3943 bool saved_greater_than_is_operator_p;
3945 /* Consume the `('. */
3946 cp_lexer_consume_token (parser->lexer);
3947 /* Within a parenthesized expression, a `>' token is always
3948 the greater-than operator. */
3949 saved_greater_than_is_operator_p
3950 = parser->greater_than_is_operator_p;
3951 parser->greater_than_is_operator_p = true;
3952 /* If we see `( { ' then we are looking at the beginning of
3953 a GNU statement-expression. */
3954 if (cp_parser_allow_gnu_extensions_p (parser)
3955 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3957 /* Statement-expressions are not allowed by the standard. */
3958 pedwarn (token->location, OPT_pedantic,
3959 "ISO C++ forbids braced-groups within expressions");
3961 /* And they're not allowed outside of a function-body; you
3962 cannot, for example, write:
3964 int i = ({ int j = 3; j + 1; });
3966 at class or namespace scope. */
3967 if (!parser->in_function_body
3968 || parser->in_template_argument_list_p)
3970 error_at (token->location,
3971 "statement-expressions are not allowed outside "
3972 "functions nor in template-argument lists");
3973 cp_parser_skip_to_end_of_block_or_statement (parser);
3974 expr = error_mark_node;
3978 /* Start the statement-expression. */
3979 expr = begin_stmt_expr ();
3980 /* Parse the compound-statement. */
3981 cp_parser_compound_statement (parser, expr, false, false);
3983 expr = finish_stmt_expr (expr, false);
3988 /* Parse the parenthesized expression. */
3989 expr = cp_parser_expression (parser, cast_p, idk);
3990 /* Let the front end know that this expression was
3991 enclosed in parentheses. This matters in case, for
3992 example, the expression is of the form `A::B', since
3993 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3995 finish_parenthesized_expr (expr);
3996 /* DR 705: Wrapping an unqualified name in parentheses
3997 suppresses arg-dependent lookup. We want to pass back
3998 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3999 (c++/37862), but none of the others. */
4000 if (*idk != CP_ID_KIND_QUALIFIED)
4001 *idk = CP_ID_KIND_NONE;
4003 /* The `>' token might be the end of a template-id or
4004 template-parameter-list now. */
4005 parser->greater_than_is_operator_p
4006 = saved_greater_than_is_operator_p;
4007 /* Consume the `)'. */
4008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4009 cp_parser_skip_to_end_of_statement (parser);
4014 case CPP_OPEN_SQUARE:
4015 if (c_dialect_objc ())
4016 /* We have an Objective-C++ message. */
4017 return cp_parser_objc_expression (parser);
4019 tree lam = cp_parser_lambda_expression (parser);
4020 /* Don't warn about a failed tentative parse. */
4021 if (cp_parser_error_occurred (parser))
4022 return error_mark_node;
4023 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4027 case CPP_OBJC_STRING:
4028 if (c_dialect_objc ())
4029 /* We have an Objective-C++ string literal. */
4030 return cp_parser_objc_expression (parser);
4031 cp_parser_error (parser, "expected primary-expression");
4032 return error_mark_node;
4035 switch (token->keyword)
4037 /* These two are the boolean literals. */
4039 cp_lexer_consume_token (parser->lexer);
4040 return boolean_true_node;
4042 cp_lexer_consume_token (parser->lexer);
4043 return boolean_false_node;
4045 /* The `__null' literal. */
4047 cp_lexer_consume_token (parser->lexer);
4050 /* The `nullptr' literal. */
4052 cp_lexer_consume_token (parser->lexer);
4053 return nullptr_node;
4055 /* Recognize the `this' keyword. */
4057 cp_lexer_consume_token (parser->lexer);
4058 if (parser->local_variables_forbidden_p)
4060 error_at (token->location,
4061 "%<this%> may not be used in this context");
4062 return error_mark_node;
4064 /* Pointers cannot appear in constant-expressions. */
4065 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4066 return error_mark_node;
4067 return finish_this_expr ();
4069 /* The `operator' keyword can be the beginning of an
4074 case RID_FUNCTION_NAME:
4075 case RID_PRETTY_FUNCTION_NAME:
4076 case RID_C99_FUNCTION_NAME:
4078 non_integral_constant name;
4080 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4081 __func__ are the names of variables -- but they are
4082 treated specially. Therefore, they are handled here,
4083 rather than relying on the generic id-expression logic
4084 below. Grammatically, these names are id-expressions.
4086 Consume the token. */
4087 token = cp_lexer_consume_token (parser->lexer);
4089 switch (token->keyword)
4091 case RID_FUNCTION_NAME:
4092 name = NIC_FUNC_NAME;
4094 case RID_PRETTY_FUNCTION_NAME:
4095 name = NIC_PRETTY_FUNC;
4097 case RID_C99_FUNCTION_NAME:
4098 name = NIC_C99_FUNC;
4104 if (cp_parser_non_integral_constant_expression (parser, name))
4105 return error_mark_node;
4107 /* Look up the name. */
4108 return finish_fname (token->u.value);
4116 /* The `__builtin_va_arg' construct is used to handle
4117 `va_arg'. Consume the `__builtin_va_arg' token. */
4118 cp_lexer_consume_token (parser->lexer);
4119 /* Look for the opening `('. */
4120 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4121 /* Now, parse the assignment-expression. */
4122 expression = cp_parser_assignment_expression (parser,
4123 /*cast_p=*/false, NULL);
4124 /* Look for the `,'. */
4125 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4126 /* Parse the type-id. */
4127 type = cp_parser_type_id (parser);
4128 /* Look for the closing `)'. */
4129 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4130 /* Using `va_arg' in a constant-expression is not
4132 if (cp_parser_non_integral_constant_expression (parser,
4134 return error_mark_node;
4135 return build_x_va_arg (expression, type);
4139 return cp_parser_builtin_offsetof (parser);
4141 case RID_HAS_NOTHROW_ASSIGN:
4142 case RID_HAS_NOTHROW_CONSTRUCTOR:
4143 case RID_HAS_NOTHROW_COPY:
4144 case RID_HAS_TRIVIAL_ASSIGN:
4145 case RID_HAS_TRIVIAL_CONSTRUCTOR:
4146 case RID_HAS_TRIVIAL_COPY:
4147 case RID_HAS_TRIVIAL_DESTRUCTOR:
4148 case RID_HAS_VIRTUAL_DESTRUCTOR:
4149 case RID_IS_ABSTRACT:
4150 case RID_IS_BASE_OF:
4152 case RID_IS_CONVERTIBLE_TO:
4155 case RID_IS_LITERAL_TYPE:
4157 case RID_IS_POLYMORPHIC:
4158 case RID_IS_STD_LAYOUT:
4159 case RID_IS_TRIVIAL:
4161 return cp_parser_trait_expr (parser, token->keyword);
4163 /* Objective-C++ expressions. */
4165 case RID_AT_PROTOCOL:
4166 case RID_AT_SELECTOR:
4167 return cp_parser_objc_expression (parser);
4170 if (parser->in_function_body
4171 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4174 error_at (token->location,
4175 "a template declaration cannot appear at block scope");
4176 cp_parser_skip_to_end_of_block_or_statement (parser);
4177 return error_mark_node;
4180 cp_parser_error (parser, "expected primary-expression");
4181 return error_mark_node;
4184 /* An id-expression can start with either an identifier, a
4185 `::' as the beginning of a qualified-id, or the "operator"
4189 case CPP_TEMPLATE_ID:
4190 case CPP_NESTED_NAME_SPECIFIER:
4194 const char *error_msg;
4197 cp_token *id_expr_token;
4200 /* Parse the id-expression. */
4202 = cp_parser_id_expression (parser,
4203 /*template_keyword_p=*/false,
4204 /*check_dependency_p=*/true,
4206 /*declarator_p=*/false,
4207 /*optional_p=*/false);
4208 if (id_expression == error_mark_node)
4209 return error_mark_node;
4210 id_expr_token = token;
4211 token = cp_lexer_peek_token (parser->lexer);
4212 done = (token->type != CPP_OPEN_SQUARE
4213 && token->type != CPP_OPEN_PAREN
4214 && token->type != CPP_DOT
4215 && token->type != CPP_DEREF
4216 && token->type != CPP_PLUS_PLUS
4217 && token->type != CPP_MINUS_MINUS);
4218 /* If we have a template-id, then no further lookup is
4219 required. If the template-id was for a template-class, we
4220 will sometimes have a TYPE_DECL at this point. */
4221 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4222 || TREE_CODE (id_expression) == TYPE_DECL)
4223 decl = id_expression;
4224 /* Look up the name. */
4227 tree ambiguous_decls;
4229 /* If we already know that this lookup is ambiguous, then
4230 we've already issued an error message; there's no reason
4232 if (id_expr_token->type == CPP_NAME
4233 && id_expr_token->ambiguous_p)
4235 cp_parser_simulate_error (parser);
4236 return error_mark_node;
4239 decl = cp_parser_lookup_name (parser, id_expression,
4242 /*is_namespace=*/false,
4243 /*check_dependency=*/true,
4245 id_expr_token->location);
4246 /* If the lookup was ambiguous, an error will already have
4248 if (ambiguous_decls)
4249 return error_mark_node;
4251 /* In Objective-C++, we may have an Objective-C 2.0
4252 dot-syntax for classes here. */
4253 if (c_dialect_objc ()
4254 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4255 && TREE_CODE (decl) == TYPE_DECL
4256 && objc_is_class_name (decl))
4259 cp_lexer_consume_token (parser->lexer);
4260 component = cp_parser_identifier (parser);
4261 if (component == error_mark_node)
4262 return error_mark_node;
4264 return objc_build_class_component_ref (id_expression, component);
4267 /* In Objective-C++, an instance variable (ivar) may be preferred
4268 to whatever cp_parser_lookup_name() found. */
4269 decl = objc_lookup_ivar (decl, id_expression);
4271 /* If name lookup gives us a SCOPE_REF, then the
4272 qualifying scope was dependent. */
4273 if (TREE_CODE (decl) == SCOPE_REF)
4275 /* At this point, we do not know if DECL is a valid
4276 integral constant expression. We assume that it is
4277 in fact such an expression, so that code like:
4279 template <int N> struct A {
4283 is accepted. At template-instantiation time, we
4284 will check that B<N>::i is actually a constant. */
4287 /* Check to see if DECL is a local variable in a context
4288 where that is forbidden. */
4289 if (parser->local_variables_forbidden_p
4290 && local_variable_p (decl))
4292 /* It might be that we only found DECL because we are
4293 trying to be generous with pre-ISO scoping rules.
4294 For example, consider:
4298 for (int i = 0; i < 10; ++i) {}
4299 extern void f(int j = i);
4302 Here, name look up will originally find the out
4303 of scope `i'. We need to issue a warning message,
4304 but then use the global `i'. */
4305 decl = check_for_out_of_scope_variable (decl);
4306 if (local_variable_p (decl))
4308 error_at (id_expr_token->location,
4309 "local variable %qD may not appear in this context",
4311 return error_mark_node;
4316 decl = (finish_id_expression
4317 (id_expression, decl, parser->scope,
4319 parser->integral_constant_expression_p,
4320 parser->allow_non_integral_constant_expression_p,
4321 &parser->non_integral_constant_expression_p,
4322 template_p, done, address_p,
4325 id_expr_token->location));
4327 cp_parser_error (parser, error_msg);
4331 /* Anything else is an error. */
4333 cp_parser_error (parser, "expected primary-expression");
4334 return error_mark_node;
4338 /* Parse an id-expression.
4345 :: [opt] nested-name-specifier template [opt] unqualified-id
4347 :: operator-function-id
4350 Return a representation of the unqualified portion of the
4351 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
4352 a `::' or nested-name-specifier.
4354 Often, if the id-expression was a qualified-id, the caller will
4355 want to make a SCOPE_REF to represent the qualified-id. This
4356 function does not do this in order to avoid wastefully creating
4357 SCOPE_REFs when they are not required.
4359 If TEMPLATE_KEYWORD_P is true, then we have just seen the
4362 If CHECK_DEPENDENCY_P is false, then names are looked up inside
4363 uninstantiated templates.
4365 If *TEMPLATE_P is non-NULL, it is set to true iff the
4366 `template' keyword is used to explicitly indicate that the entity
4367 named is a template.
4369 If DECLARATOR_P is true, the id-expression is appearing as part of
4370 a declarator, rather than as part of an expression. */
4373 cp_parser_id_expression (cp_parser *parser,
4374 bool template_keyword_p,
4375 bool check_dependency_p,
4380 bool global_scope_p;
4381 bool nested_name_specifier_p;
4383 /* Assume the `template' keyword was not used. */
4385 *template_p = template_keyword_p;
4387 /* Look for the optional `::' operator. */
4389 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4391 /* Look for the optional nested-name-specifier. */
4392 nested_name_specifier_p
4393 = (cp_parser_nested_name_specifier_opt (parser,
4394 /*typename_keyword_p=*/false,
4399 /* If there is a nested-name-specifier, then we are looking at
4400 the first qualified-id production. */
4401 if (nested_name_specifier_p)
4404 tree saved_object_scope;
4405 tree saved_qualifying_scope;
4406 tree unqualified_id;
4409 /* See if the next token is the `template' keyword. */
4411 template_p = &is_template;
4412 *template_p = cp_parser_optional_template_keyword (parser);
4413 /* Name lookup we do during the processing of the
4414 unqualified-id might obliterate SCOPE. */
4415 saved_scope = parser->scope;
4416 saved_object_scope = parser->object_scope;
4417 saved_qualifying_scope = parser->qualifying_scope;
4418 /* Process the final unqualified-id. */
4419 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4422 /*optional_p=*/false);
4423 /* Restore the SAVED_SCOPE for our caller. */
4424 parser->scope = saved_scope;
4425 parser->object_scope = saved_object_scope;
4426 parser->qualifying_scope = saved_qualifying_scope;
4428 return unqualified_id;
4430 /* Otherwise, if we are in global scope, then we are looking at one
4431 of the other qualified-id productions. */
4432 else if (global_scope_p)
4437 /* Peek at the next token. */
4438 token = cp_lexer_peek_token (parser->lexer);
4440 /* If it's an identifier, and the next token is not a "<", then
4441 we can avoid the template-id case. This is an optimization
4442 for this common case. */
4443 if (token->type == CPP_NAME
4444 && !cp_parser_nth_token_starts_template_argument_list_p
4446 return cp_parser_identifier (parser);
4448 cp_parser_parse_tentatively (parser);
4449 /* Try a template-id. */
4450 id = cp_parser_template_id (parser,
4451 /*template_keyword_p=*/false,
4452 /*check_dependency_p=*/true,
4454 /* If that worked, we're done. */
4455 if (cp_parser_parse_definitely (parser))
4458 /* Peek at the next token. (Changes in the token buffer may
4459 have invalidated the pointer obtained above.) */
4460 token = cp_lexer_peek_token (parser->lexer);
4462 switch (token->type)
4465 return cp_parser_identifier (parser);
4468 if (token->keyword == RID_OPERATOR)
4469 return cp_parser_operator_function_id (parser);
4473 cp_parser_error (parser, "expected id-expression");
4474 return error_mark_node;
4478 return cp_parser_unqualified_id (parser, template_keyword_p,
4479 /*check_dependency_p=*/true,
4484 /* Parse an unqualified-id.
4488 operator-function-id
4489 conversion-function-id
4493 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4494 keyword, in a construct like `A::template ...'.
4496 Returns a representation of unqualified-id. For the `identifier'
4497 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
4498 production a BIT_NOT_EXPR is returned; the operand of the
4499 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
4500 other productions, see the documentation accompanying the
4501 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
4502 names are looked up in uninstantiated templates. If DECLARATOR_P
4503 is true, the unqualified-id is appearing as part of a declarator,
4504 rather than as part of an expression. */
4507 cp_parser_unqualified_id (cp_parser* parser,
4508 bool template_keyword_p,
4509 bool check_dependency_p,
4515 /* Peek at the next token. */
4516 token = cp_lexer_peek_token (parser->lexer);
4518 switch (token->type)
4524 /* We don't know yet whether or not this will be a
4526 cp_parser_parse_tentatively (parser);
4527 /* Try a template-id. */
4528 id = cp_parser_template_id (parser, template_keyword_p,
4531 /* If it worked, we're done. */
4532 if (cp_parser_parse_definitely (parser))
4534 /* Otherwise, it's an ordinary identifier. */
4535 return cp_parser_identifier (parser);
4538 case CPP_TEMPLATE_ID:
4539 return cp_parser_template_id (parser, template_keyword_p,
4546 tree qualifying_scope;
4551 /* Consume the `~' token. */
4552 cp_lexer_consume_token (parser->lexer);
4553 /* Parse the class-name. The standard, as written, seems to
4556 template <typename T> struct S { ~S (); };
4557 template <typename T> S<T>::~S() {}
4559 is invalid, since `~' must be followed by a class-name, but
4560 `S<T>' is dependent, and so not known to be a class.
4561 That's not right; we need to look in uninstantiated
4562 templates. A further complication arises from:
4564 template <typename T> void f(T t) {
4568 Here, it is not possible to look up `T' in the scope of `T'
4569 itself. We must look in both the current scope, and the
4570 scope of the containing complete expression.
4572 Yet another issue is:
4581 The standard does not seem to say that the `S' in `~S'
4582 should refer to the type `S' and not the data member
4585 /* DR 244 says that we look up the name after the "~" in the
4586 same scope as we looked up the qualifying name. That idea
4587 isn't fully worked out; it's more complicated than that. */
4588 scope = parser->scope;
4589 object_scope = parser->object_scope;
4590 qualifying_scope = parser->qualifying_scope;
4592 /* Check for invalid scopes. */
4593 if (scope == error_mark_node)
4595 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4596 cp_lexer_consume_token (parser->lexer);
4597 return error_mark_node;
4599 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4601 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4602 error_at (token->location,
4603 "scope %qT before %<~%> is not a class-name",
4605 cp_parser_simulate_error (parser);
4606 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4607 cp_lexer_consume_token (parser->lexer);
4608 return error_mark_node;
4610 gcc_assert (!scope || TYPE_P (scope));
4612 /* If the name is of the form "X::~X" it's OK even if X is a
4614 token = cp_lexer_peek_token (parser->lexer);
4616 && token->type == CPP_NAME
4617 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4619 && (token->u.value == TYPE_IDENTIFIER (scope)
4620 || (CLASS_TYPE_P (scope)
4621 && constructor_name_p (token->u.value, scope))))
4623 cp_lexer_consume_token (parser->lexer);
4624 return build_nt (BIT_NOT_EXPR, scope);
4627 /* If there was an explicit qualification (S::~T), first look
4628 in the scope given by the qualification (i.e., S).
4630 Note: in the calls to cp_parser_class_name below we pass
4631 typename_type so that lookup finds the injected-class-name
4632 rather than the constructor. */
4634 type_decl = NULL_TREE;
4637 cp_parser_parse_tentatively (parser);
4638 type_decl = cp_parser_class_name (parser,
4639 /*typename_keyword_p=*/false,
4640 /*template_keyword_p=*/false,
4642 /*check_dependency=*/false,
4643 /*class_head_p=*/false,
4645 if (cp_parser_parse_definitely (parser))
4648 /* In "N::S::~S", look in "N" as well. */
4649 if (!done && scope && qualifying_scope)
4651 cp_parser_parse_tentatively (parser);
4652 parser->scope = qualifying_scope;
4653 parser->object_scope = NULL_TREE;
4654 parser->qualifying_scope = NULL_TREE;
4656 = cp_parser_class_name (parser,
4657 /*typename_keyword_p=*/false,
4658 /*template_keyword_p=*/false,
4660 /*check_dependency=*/false,
4661 /*class_head_p=*/false,
4663 if (cp_parser_parse_definitely (parser))
4666 /* In "p->S::~T", look in the scope given by "*p" as well. */
4667 else if (!done && object_scope)
4669 cp_parser_parse_tentatively (parser);
4670 parser->scope = object_scope;
4671 parser->object_scope = NULL_TREE;
4672 parser->qualifying_scope = NULL_TREE;
4674 = cp_parser_class_name (parser,
4675 /*typename_keyword_p=*/false,
4676 /*template_keyword_p=*/false,
4678 /*check_dependency=*/false,
4679 /*class_head_p=*/false,
4681 if (cp_parser_parse_definitely (parser))
4684 /* Look in the surrounding context. */
4687 parser->scope = NULL_TREE;
4688 parser->object_scope = NULL_TREE;
4689 parser->qualifying_scope = NULL_TREE;
4690 if (processing_template_decl)
4691 cp_parser_parse_tentatively (parser);
4693 = cp_parser_class_name (parser,
4694 /*typename_keyword_p=*/false,
4695 /*template_keyword_p=*/false,
4697 /*check_dependency=*/false,
4698 /*class_head_p=*/false,
4700 if (processing_template_decl
4701 && ! cp_parser_parse_definitely (parser))
4703 /* We couldn't find a type with this name, so just accept
4704 it and check for a match at instantiation time. */
4705 type_decl = cp_parser_identifier (parser);
4706 if (type_decl != error_mark_node)
4707 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4711 /* If an error occurred, assume that the name of the
4712 destructor is the same as the name of the qualifying
4713 class. That allows us to keep parsing after running
4714 into ill-formed destructor names. */
4715 if (type_decl == error_mark_node && scope)
4716 return build_nt (BIT_NOT_EXPR, scope);
4717 else if (type_decl == error_mark_node)
4718 return error_mark_node;
4720 /* Check that destructor name and scope match. */
4721 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4723 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4724 error_at (token->location,
4725 "declaration of %<~%T%> as member of %qT",
4727 cp_parser_simulate_error (parser);
4728 return error_mark_node;
4733 A typedef-name that names a class shall not be used as the
4734 identifier in the declarator for a destructor declaration. */
4736 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4737 && !DECL_SELF_REFERENCE_P (type_decl)
4738 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4739 error_at (token->location,
4740 "typedef-name %qD used as destructor declarator",
4743 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4747 if (token->keyword == RID_OPERATOR)
4751 /* This could be a template-id, so we try that first. */
4752 cp_parser_parse_tentatively (parser);
4753 /* Try a template-id. */
4754 id = cp_parser_template_id (parser, template_keyword_p,
4755 /*check_dependency_p=*/true,
4757 /* If that worked, we're done. */
4758 if (cp_parser_parse_definitely (parser))
4760 /* We still don't know whether we're looking at an
4761 operator-function-id or a conversion-function-id. */
4762 cp_parser_parse_tentatively (parser);
4763 /* Try an operator-function-id. */
4764 id = cp_parser_operator_function_id (parser);
4765 /* If that didn't work, try a conversion-function-id. */
4766 if (!cp_parser_parse_definitely (parser))
4767 id = cp_parser_conversion_function_id (parser);
4768 else if (UDLIT_OPER_P (id))
4771 const char *name = UDLIT_OP_SUFFIX (id);
4772 if (name[0] != '_' && !in_system_header)
4773 warning (0, "literal operator suffixes not preceded by %<_%>"
4774 " are reserved for future standardization");
4784 cp_parser_error (parser, "expected unqualified-id");
4785 return error_mark_node;
4789 /* Parse an (optional) nested-name-specifier.
4791 nested-name-specifier: [C++98]
4792 class-or-namespace-name :: nested-name-specifier [opt]
4793 class-or-namespace-name :: template nested-name-specifier [opt]
4795 nested-name-specifier: [C++0x]
4798 nested-name-specifier identifier ::
4799 nested-name-specifier template [opt] simple-template-id ::
4801 PARSER->SCOPE should be set appropriately before this function is
4802 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4803 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4806 Sets PARSER->SCOPE to the class (TYPE) or namespace
4807 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4808 it unchanged if there is no nested-name-specifier. Returns the new
4809 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4811 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4812 part of a declaration and/or decl-specifier. */
4815 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4816 bool typename_keyword_p,
4817 bool check_dependency_p,
4819 bool is_declaration)
4821 bool success = false;
4822 cp_token_position start = 0;
4825 /* Remember where the nested-name-specifier starts. */
4826 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4828 start = cp_lexer_token_position (parser->lexer, false);
4829 push_deferring_access_checks (dk_deferred);
4836 tree saved_qualifying_scope;
4837 bool template_keyword_p;
4839 /* Spot cases that cannot be the beginning of a
4840 nested-name-specifier. */
4841 token = cp_lexer_peek_token (parser->lexer);
4843 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4844 the already parsed nested-name-specifier. */
4845 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4847 /* Grab the nested-name-specifier and continue the loop. */
4848 cp_parser_pre_parsed_nested_name_specifier (parser);
4849 /* If we originally encountered this nested-name-specifier
4850 with IS_DECLARATION set to false, we will not have
4851 resolved TYPENAME_TYPEs, so we must do so here. */
4853 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4855 new_scope = resolve_typename_type (parser->scope,
4856 /*only_current_p=*/false);
4857 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4858 parser->scope = new_scope;
4864 /* Spot cases that cannot be the beginning of a
4865 nested-name-specifier. On the second and subsequent times
4866 through the loop, we look for the `template' keyword. */
4867 if (success && token->keyword == RID_TEMPLATE)
4869 /* A template-id can start a nested-name-specifier. */
4870 else if (token->type == CPP_TEMPLATE_ID)
4872 /* DR 743: decltype can be used in a nested-name-specifier. */
4873 else if (token_is_decltype (token))
4877 /* If the next token is not an identifier, then it is
4878 definitely not a type-name or namespace-name. */
4879 if (token->type != CPP_NAME)
4881 /* If the following token is neither a `<' (to begin a
4882 template-id), nor a `::', then we are not looking at a
4883 nested-name-specifier. */
4884 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4886 if (token->type == CPP_COLON
4887 && parser->colon_corrects_to_scope_p
4888 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4890 error_at (token->location,
4891 "found %<:%> in nested-name-specifier, expected %<::%>");
4892 token->type = CPP_SCOPE;
4895 if (token->type != CPP_SCOPE
4896 && !cp_parser_nth_token_starts_template_argument_list_p
4901 /* The nested-name-specifier is optional, so we parse
4903 cp_parser_parse_tentatively (parser);
4905 /* Look for the optional `template' keyword, if this isn't the
4906 first time through the loop. */
4908 template_keyword_p = cp_parser_optional_template_keyword (parser);
4910 template_keyword_p = false;
4912 /* Save the old scope since the name lookup we are about to do
4913 might destroy it. */
4914 old_scope = parser->scope;
4915 saved_qualifying_scope = parser->qualifying_scope;
4916 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4917 look up names in "X<T>::I" in order to determine that "Y" is
4918 a template. So, if we have a typename at this point, we make
4919 an effort to look through it. */
4921 && !typename_keyword_p
4923 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4924 parser->scope = resolve_typename_type (parser->scope,
4925 /*only_current_p=*/false);
4926 /* Parse the qualifying entity. */
4928 = cp_parser_qualifying_entity (parser,
4934 /* Look for the `::' token. */
4935 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4937 /* If we found what we wanted, we keep going; otherwise, we're
4939 if (!cp_parser_parse_definitely (parser))
4941 bool error_p = false;
4943 /* Restore the OLD_SCOPE since it was valid before the
4944 failed attempt at finding the last
4945 class-or-namespace-name. */
4946 parser->scope = old_scope;
4947 parser->qualifying_scope = saved_qualifying_scope;
4949 /* If the next token is a decltype, and the one after that is a
4950 `::', then the decltype has failed to resolve to a class or
4951 enumeration type. Give this error even when parsing
4952 tentatively since it can't possibly be valid--and we're going
4953 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4954 won't get another chance.*/
4955 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4956 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4959 token = cp_lexer_consume_token (parser->lexer);
4960 error_at (token->location, "decltype evaluates to %qT, "
4961 "which is not a class or enumeration type",
4963 parser->scope = error_mark_node;
4967 cp_lexer_consume_token (parser->lexer);
4970 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4972 /* If the next token is an identifier, and the one after
4973 that is a `::', then any valid interpretation would have
4974 found a class-or-namespace-name. */
4975 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4976 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4978 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4981 token = cp_lexer_consume_token (parser->lexer);
4984 if (!token->ambiguous_p)
4987 tree ambiguous_decls;
4989 decl = cp_parser_lookup_name (parser, token->u.value,
4991 /*is_template=*/false,
4992 /*is_namespace=*/false,
4993 /*check_dependency=*/true,
4996 if (TREE_CODE (decl) == TEMPLATE_DECL)
4997 error_at (token->location,
4998 "%qD used without template parameters",
5000 else if (ambiguous_decls)
5002 error_at (token->location,
5003 "reference to %qD is ambiguous",
5005 print_candidates (ambiguous_decls);
5006 decl = error_mark_node;
5010 if (cxx_dialect != cxx98)
5011 cp_parser_name_lookup_error
5012 (parser, token->u.value, decl, NLE_NOT_CXX98,
5015 cp_parser_name_lookup_error
5016 (parser, token->u.value, decl, NLE_CXX98,
5020 parser->scope = error_mark_node;
5022 /* Treat this as a successful nested-name-specifier
5027 If the name found is not a class-name (clause
5028 _class_) or namespace-name (_namespace.def_), the
5029 program is ill-formed. */
5032 cp_lexer_consume_token (parser->lexer);
5036 /* We've found one valid nested-name-specifier. */
5038 /* Name lookup always gives us a DECL. */
5039 if (TREE_CODE (new_scope) == TYPE_DECL)
5040 new_scope = TREE_TYPE (new_scope);
5041 /* Uses of "template" must be followed by actual templates. */
5042 if (template_keyword_p
5043 && !(CLASS_TYPE_P (new_scope)
5044 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5045 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5046 || CLASSTYPE_IS_TEMPLATE (new_scope)))
5047 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5048 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5049 == TEMPLATE_ID_EXPR)))
5050 permerror (input_location, TYPE_P (new_scope)
5051 ? G_("%qT is not a template")
5052 : G_("%qD is not a template"),
5054 /* If it is a class scope, try to complete it; we are about to
5055 be looking up names inside the class. */
5056 if (TYPE_P (new_scope)
5057 /* Since checking types for dependency can be expensive,
5058 avoid doing it if the type is already complete. */
5059 && !COMPLETE_TYPE_P (new_scope)
5060 /* Do not try to complete dependent types. */
5061 && !dependent_type_p (new_scope))
5063 new_scope = complete_type (new_scope);
5064 /* If it is a typedef to current class, use the current
5065 class instead, as the typedef won't have any names inside
5067 if (!COMPLETE_TYPE_P (new_scope)
5068 && currently_open_class (new_scope))
5069 new_scope = TYPE_MAIN_VARIANT (new_scope);
5071 /* Make sure we look in the right scope the next time through
5073 parser->scope = new_scope;
5076 /* If parsing tentatively, replace the sequence of tokens that makes
5077 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5078 token. That way, should we re-parse the token stream, we will
5079 not have to repeat the effort required to do the parse, nor will
5080 we issue duplicate error messages. */
5081 if (success && start)
5085 token = cp_lexer_token_at (parser->lexer, start);
5086 /* Reset the contents of the START token. */
5087 token->type = CPP_NESTED_NAME_SPECIFIER;
5088 /* Retrieve any deferred checks. Do not pop this access checks yet
5089 so the memory will not be reclaimed during token replacing below. */
5090 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5091 token->u.tree_check_value->value = parser->scope;
5092 token->u.tree_check_value->checks = get_deferred_access_checks ();
5093 token->u.tree_check_value->qualifying_scope =
5094 parser->qualifying_scope;
5095 token->keyword = RID_MAX;
5097 /* Purge all subsequent tokens. */
5098 cp_lexer_purge_tokens_after (parser->lexer, start);
5102 pop_to_parent_deferring_access_checks ();
5104 return success ? parser->scope : NULL_TREE;
5107 /* Parse a nested-name-specifier. See
5108 cp_parser_nested_name_specifier_opt for details. This function
5109 behaves identically, except that it will an issue an error if no
5110 nested-name-specifier is present. */
5113 cp_parser_nested_name_specifier (cp_parser *parser,
5114 bool typename_keyword_p,
5115 bool check_dependency_p,
5117 bool is_declaration)
5121 /* Look for the nested-name-specifier. */
5122 scope = cp_parser_nested_name_specifier_opt (parser,
5127 /* If it was not present, issue an error message. */
5130 cp_parser_error (parser, "expected nested-name-specifier");
5131 parser->scope = NULL_TREE;
5137 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5138 this is either a class-name or a namespace-name (which corresponds
5139 to the class-or-namespace-name production in the grammar). For
5140 C++0x, it can also be a type-name that refers to an enumeration
5141 type or a simple-template-id.
5143 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5144 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5145 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5146 TYPE_P is TRUE iff the next name should be taken as a class-name,
5147 even the same name is declared to be another entity in the same
5150 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5151 specified by the class-or-namespace-name. If neither is found the
5152 ERROR_MARK_NODE is returned. */
5155 cp_parser_qualifying_entity (cp_parser *parser,
5156 bool typename_keyword_p,
5157 bool template_keyword_p,
5158 bool check_dependency_p,
5160 bool is_declaration)
5163 tree saved_qualifying_scope;
5164 tree saved_object_scope;
5167 bool successful_parse_p;
5169 /* DR 743: decltype can appear in a nested-name-specifier. */
5170 if (cp_lexer_next_token_is_decltype (parser->lexer))
5172 scope = cp_parser_decltype (parser);
5173 if (TREE_CODE (scope) != ENUMERAL_TYPE
5174 && !MAYBE_CLASS_TYPE_P (scope))
5176 cp_parser_simulate_error (parser);
5177 return error_mark_node;
5179 if (TYPE_NAME (scope))
5180 scope = TYPE_NAME (scope);
5184 /* Before we try to parse the class-name, we must save away the
5185 current PARSER->SCOPE since cp_parser_class_name will destroy
5187 saved_scope = parser->scope;
5188 saved_qualifying_scope = parser->qualifying_scope;
5189 saved_object_scope = parser->object_scope;
5190 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
5191 there is no need to look for a namespace-name. */
5192 only_class_p = template_keyword_p
5193 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5195 cp_parser_parse_tentatively (parser);
5196 scope = cp_parser_class_name (parser,
5199 type_p ? class_type : none_type,
5201 /*class_head_p=*/false,
5203 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5204 /* If that didn't work and we're in C++0x mode, try for a type-name. */
5206 && cxx_dialect != cxx98
5207 && !successful_parse_p)
5209 /* Restore the saved scope. */
5210 parser->scope = saved_scope;
5211 parser->qualifying_scope = saved_qualifying_scope;
5212 parser->object_scope = saved_object_scope;
5214 /* Parse tentatively. */
5215 cp_parser_parse_tentatively (parser);
5217 /* Parse a type-name */
5218 scope = cp_parser_type_name (parser);
5220 /* "If the name found does not designate a namespace or a class,
5221 enumeration, or dependent type, the program is ill-formed."
5223 We cover classes and dependent types above and namespaces below,
5224 so this code is only looking for enums. */
5225 if (!scope || TREE_CODE (scope) != TYPE_DECL
5226 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5227 cp_parser_simulate_error (parser);
5229 successful_parse_p = cp_parser_parse_definitely (parser);
5231 /* If that didn't work, try for a namespace-name. */
5232 if (!only_class_p && !successful_parse_p)
5234 /* Restore the saved scope. */
5235 parser->scope = saved_scope;
5236 parser->qualifying_scope = saved_qualifying_scope;
5237 parser->object_scope = saved_object_scope;
5238 /* If we are not looking at an identifier followed by the scope
5239 resolution operator, then this is not part of a
5240 nested-name-specifier. (Note that this function is only used
5241 to parse the components of a nested-name-specifier.) */
5242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5243 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5244 return error_mark_node;
5245 scope = cp_parser_namespace_name (parser);
5251 /* Parse a postfix-expression.
5255 postfix-expression [ expression ]
5256 postfix-expression ( expression-list [opt] )
5257 simple-type-specifier ( expression-list [opt] )
5258 typename :: [opt] nested-name-specifier identifier
5259 ( expression-list [opt] )
5260 typename :: [opt] nested-name-specifier template [opt] template-id
5261 ( expression-list [opt] )
5262 postfix-expression . template [opt] id-expression
5263 postfix-expression -> template [opt] id-expression
5264 postfix-expression . pseudo-destructor-name
5265 postfix-expression -> pseudo-destructor-name
5266 postfix-expression ++
5267 postfix-expression --
5268 dynamic_cast < type-id > ( expression )
5269 static_cast < type-id > ( expression )
5270 reinterpret_cast < type-id > ( expression )
5271 const_cast < type-id > ( expression )
5272 typeid ( expression )
5278 ( type-id ) { initializer-list , [opt] }
5280 This extension is a GNU version of the C99 compound-literal
5281 construct. (The C99 grammar uses `type-name' instead of `type-id',
5282 but they are essentially the same concept.)
5284 If ADDRESS_P is true, the postfix expression is the operand of the
5285 `&' operator. CAST_P is true if this expression is the target of a
5288 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5289 class member access expressions [expr.ref].
5291 Returns a representation of the expression. */
5294 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5295 bool member_access_only_p,
5296 cp_id_kind * pidk_return)
5300 cp_id_kind idk = CP_ID_KIND_NONE;
5301 tree postfix_expression = NULL_TREE;
5302 bool is_member_access = false;
5304 /* Peek at the next token. */
5305 token = cp_lexer_peek_token (parser->lexer);
5306 /* Some of the productions are determined by keywords. */
5307 keyword = token->keyword;
5317 const char *saved_message;
5319 /* All of these can be handled in the same way from the point
5320 of view of parsing. Begin by consuming the token
5321 identifying the cast. */
5322 cp_lexer_consume_token (parser->lexer);
5324 /* New types cannot be defined in the cast. */
5325 saved_message = parser->type_definition_forbidden_message;
5326 parser->type_definition_forbidden_message
5327 = G_("types may not be defined in casts");
5329 /* Look for the opening `<'. */
5330 cp_parser_require (parser, CPP_LESS, RT_LESS);
5331 /* Parse the type to which we are casting. */
5332 type = cp_parser_type_id (parser);
5333 /* Look for the closing `>'. */
5334 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5335 /* Restore the old message. */
5336 parser->type_definition_forbidden_message = saved_message;
5338 /* And the expression which is being cast. */
5339 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5340 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5341 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5343 /* Only type conversions to integral or enumeration types
5344 can be used in constant-expressions. */
5345 if (!cast_valid_in_integral_constant_expression_p (type)
5346 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5347 return error_mark_node;
5353 = build_dynamic_cast (type, expression, tf_warning_or_error);
5357 = build_static_cast (type, expression, tf_warning_or_error);
5361 = build_reinterpret_cast (type, expression,
5362 tf_warning_or_error);
5366 = build_const_cast (type, expression, tf_warning_or_error);
5377 const char *saved_message;
5378 bool saved_in_type_id_in_expr_p;
5380 /* Consume the `typeid' token. */
5381 cp_lexer_consume_token (parser->lexer);
5382 /* Look for the `(' token. */
5383 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5384 /* Types cannot be defined in a `typeid' expression. */
5385 saved_message = parser->type_definition_forbidden_message;
5386 parser->type_definition_forbidden_message
5387 = G_("types may not be defined in a %<typeid%> expression");
5388 /* We can't be sure yet whether we're looking at a type-id or an
5390 cp_parser_parse_tentatively (parser);
5391 /* Try a type-id first. */
5392 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5393 parser->in_type_id_in_expr_p = true;
5394 type = cp_parser_type_id (parser);
5395 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5396 /* Look for the `)' token. Otherwise, we can't be sure that
5397 we're not looking at an expression: consider `typeid (int
5398 (3))', for example. */
5399 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5400 /* If all went well, simply lookup the type-id. */
5401 if (cp_parser_parse_definitely (parser))
5402 postfix_expression = get_typeid (type);
5403 /* Otherwise, fall back to the expression variant. */
5408 /* Look for an expression. */
5409 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5410 /* Compute its typeid. */
5411 postfix_expression = build_typeid (expression);
5412 /* Look for the `)' token. */
5413 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5415 /* Restore the saved message. */
5416 parser->type_definition_forbidden_message = saved_message;
5417 /* `typeid' may not appear in an integral constant expression. */
5418 if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5419 return error_mark_node;
5426 /* The syntax permitted here is the same permitted for an
5427 elaborated-type-specifier. */
5428 type = cp_parser_elaborated_type_specifier (parser,
5429 /*is_friend=*/false,
5430 /*is_declaration=*/false);
5431 postfix_expression = cp_parser_functional_cast (parser, type);
5439 /* If the next thing is a simple-type-specifier, we may be
5440 looking at a functional cast. We could also be looking at
5441 an id-expression. So, we try the functional cast, and if
5442 that doesn't work we fall back to the primary-expression. */
5443 cp_parser_parse_tentatively (parser);
5444 /* Look for the simple-type-specifier. */
5445 type = cp_parser_simple_type_specifier (parser,
5446 /*decl_specs=*/NULL,
5447 CP_PARSER_FLAGS_NONE);
5448 /* Parse the cast itself. */
5449 if (!cp_parser_error_occurred (parser))
5451 = cp_parser_functional_cast (parser, type);
5452 /* If that worked, we're done. */
5453 if (cp_parser_parse_definitely (parser))
5456 /* If the functional-cast didn't work out, try a
5457 compound-literal. */
5458 if (cp_parser_allow_gnu_extensions_p (parser)
5459 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5461 VEC(constructor_elt,gc) *initializer_list = NULL;
5462 bool saved_in_type_id_in_expr_p;
5464 cp_parser_parse_tentatively (parser);
5465 /* Consume the `('. */
5466 cp_lexer_consume_token (parser->lexer);
5467 /* Parse the type. */
5468 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5469 parser->in_type_id_in_expr_p = true;
5470 type = cp_parser_type_id (parser);
5471 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5472 /* Look for the `)'. */
5473 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5474 /* Look for the `{'. */
5475 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5476 /* If things aren't going well, there's no need to
5478 if (!cp_parser_error_occurred (parser))
5480 bool non_constant_p;
5481 /* Parse the initializer-list. */
5483 = cp_parser_initializer_list (parser, &non_constant_p);
5484 /* Allow a trailing `,'. */
5485 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5486 cp_lexer_consume_token (parser->lexer);
5487 /* Look for the final `}'. */
5488 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5490 /* If that worked, we're definitely looking at a
5491 compound-literal expression. */
5492 if (cp_parser_parse_definitely (parser))
5494 /* Warn the user that a compound literal is not
5495 allowed in standard C++. */
5496 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5497 /* For simplicity, we disallow compound literals in
5498 constant-expressions. We could
5499 allow compound literals of integer type, whose
5500 initializer was a constant, in constant
5501 expressions. Permitting that usage, as a further
5502 extension, would not change the meaning of any
5503 currently accepted programs. (Of course, as
5504 compound literals are not part of ISO C++, the
5505 standard has nothing to say.) */
5506 if (cp_parser_non_integral_constant_expression (parser,
5509 postfix_expression = error_mark_node;
5512 /* Form the representation of the compound-literal. */
5514 = (finish_compound_literal
5515 (type, build_constructor (init_list_type_node,
5517 tf_warning_or_error));
5522 /* It must be a primary-expression. */
5524 = cp_parser_primary_expression (parser, address_p, cast_p,
5525 /*template_arg_p=*/false,
5531 /* Keep looping until the postfix-expression is complete. */
5534 if (idk == CP_ID_KIND_UNQUALIFIED
5535 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5536 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5537 /* It is not a Koenig lookup function call. */
5539 = unqualified_name_lookup_error (postfix_expression);
5541 /* Peek at the next token. */
5542 token = cp_lexer_peek_token (parser->lexer);
5544 switch (token->type)
5546 case CPP_OPEN_SQUARE:
5548 = cp_parser_postfix_open_square_expression (parser,
5551 idk = CP_ID_KIND_NONE;
5552 is_member_access = false;
5555 case CPP_OPEN_PAREN:
5556 /* postfix-expression ( expression-list [opt] ) */
5559 bool is_builtin_constant_p;
5560 bool saved_integral_constant_expression_p = false;
5561 bool saved_non_integral_constant_expression_p = false;
5564 is_member_access = false;
5566 is_builtin_constant_p
5567 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5568 if (is_builtin_constant_p)
5570 /* The whole point of __builtin_constant_p is to allow
5571 non-constant expressions to appear as arguments. */
5572 saved_integral_constant_expression_p
5573 = parser->integral_constant_expression_p;
5574 saved_non_integral_constant_expression_p
5575 = parser->non_integral_constant_expression_p;
5576 parser->integral_constant_expression_p = false;
5578 args = (cp_parser_parenthesized_expression_list
5580 /*cast_p=*/false, /*allow_expansion_p=*/true,
5581 /*non_constant_p=*/NULL));
5582 if (is_builtin_constant_p)
5584 parser->integral_constant_expression_p
5585 = saved_integral_constant_expression_p;
5586 parser->non_integral_constant_expression_p
5587 = saved_non_integral_constant_expression_p;
5592 postfix_expression = error_mark_node;
5596 /* Function calls are not permitted in
5597 constant-expressions. */
5598 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5599 && cp_parser_non_integral_constant_expression (parser,
5602 postfix_expression = error_mark_node;
5603 release_tree_vector (args);
5608 if (idk == CP_ID_KIND_UNQUALIFIED
5609 || idk == CP_ID_KIND_TEMPLATE_ID)
5611 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5613 if (!VEC_empty (tree, args))
5616 if (!any_type_dependent_arguments_p (args))
5618 = perform_koenig_lookup (postfix_expression, args,
5619 /*include_std=*/false,
5620 tf_warning_or_error);
5624 = unqualified_fn_lookup_error (postfix_expression);
5626 /* We do not perform argument-dependent lookup if
5627 normal lookup finds a non-function, in accordance
5628 with the expected resolution of DR 218. */
5629 else if (!VEC_empty (tree, args)
5630 && is_overloaded_fn (postfix_expression))
5632 tree fn = get_first_fn (postfix_expression);
5633 fn = STRIP_TEMPLATE (fn);
5635 /* Do not do argument dependent lookup if regular
5636 lookup finds a member function or a block-scope
5637 function declaration. [basic.lookup.argdep]/3 */
5638 if (!DECL_FUNCTION_MEMBER_P (fn)
5639 && !DECL_LOCAL_FUNCTION_P (fn))
5642 if (!any_type_dependent_arguments_p (args))
5644 = perform_koenig_lookup (postfix_expression, args,
5645 /*include_std=*/false,
5646 tf_warning_or_error);
5651 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5653 tree instance = TREE_OPERAND (postfix_expression, 0);
5654 tree fn = TREE_OPERAND (postfix_expression, 1);
5656 if (processing_template_decl
5657 && (type_dependent_expression_p (instance)
5658 || (!BASELINK_P (fn)
5659 && TREE_CODE (fn) != FIELD_DECL)
5660 || type_dependent_expression_p (fn)
5661 || any_type_dependent_arguments_p (args)))
5664 = build_nt_call_vec (postfix_expression, args);
5665 release_tree_vector (args);
5669 if (BASELINK_P (fn))
5672 = (build_new_method_call
5673 (instance, fn, &args, NULL_TREE,
5674 (idk == CP_ID_KIND_QUALIFIED
5675 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5678 tf_warning_or_error));
5682 = finish_call_expr (postfix_expression, &args,
5683 /*disallow_virtual=*/false,
5685 tf_warning_or_error);
5687 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5688 || TREE_CODE (postfix_expression) == MEMBER_REF
5689 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5690 postfix_expression = (build_offset_ref_call_from_tree
5691 (postfix_expression, &args));
5692 else if (idk == CP_ID_KIND_QUALIFIED)
5693 /* A call to a static class member, or a namespace-scope
5696 = finish_call_expr (postfix_expression, &args,
5697 /*disallow_virtual=*/true,
5699 tf_warning_or_error);
5701 /* All other function calls. */
5703 = finish_call_expr (postfix_expression, &args,
5704 /*disallow_virtual=*/false,
5706 tf_warning_or_error);
5708 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5709 idk = CP_ID_KIND_NONE;
5711 release_tree_vector (args);
5717 /* postfix-expression . template [opt] id-expression
5718 postfix-expression . pseudo-destructor-name
5719 postfix-expression -> template [opt] id-expression
5720 postfix-expression -> pseudo-destructor-name */
5722 /* Consume the `.' or `->' operator. */
5723 cp_lexer_consume_token (parser->lexer);
5726 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5731 is_member_access = true;
5735 /* postfix-expression ++ */
5736 /* Consume the `++' token. */
5737 cp_lexer_consume_token (parser->lexer);
5738 /* Generate a representation for the complete expression. */
5740 = finish_increment_expr (postfix_expression,
5741 POSTINCREMENT_EXPR);
5742 /* Increments may not appear in constant-expressions. */
5743 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5744 postfix_expression = error_mark_node;
5745 idk = CP_ID_KIND_NONE;
5746 is_member_access = false;
5749 case CPP_MINUS_MINUS:
5750 /* postfix-expression -- */
5751 /* Consume the `--' token. */
5752 cp_lexer_consume_token (parser->lexer);
5753 /* Generate a representation for the complete expression. */
5755 = finish_increment_expr (postfix_expression,
5756 POSTDECREMENT_EXPR);
5757 /* Decrements may not appear in constant-expressions. */
5758 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5759 postfix_expression = error_mark_node;
5760 idk = CP_ID_KIND_NONE;
5761 is_member_access = false;
5765 if (pidk_return != NULL)
5766 * pidk_return = idk;
5767 if (member_access_only_p)
5768 return is_member_access? postfix_expression : error_mark_node;
5770 return postfix_expression;
5774 /* We should never get here. */
5776 return error_mark_node;
5779 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5780 by cp_parser_builtin_offsetof. We're looking for
5782 postfix-expression [ expression ]
5784 FOR_OFFSETOF is set if we're being called in that context, which
5785 changes how we deal with integer constant expressions. */
5788 cp_parser_postfix_open_square_expression (cp_parser *parser,
5789 tree postfix_expression,
5794 /* Consume the `[' token. */
5795 cp_lexer_consume_token (parser->lexer);
5797 /* Parse the index expression. */
5798 /* ??? For offsetof, there is a question of what to allow here. If
5799 offsetof is not being used in an integral constant expression context,
5800 then we *could* get the right answer by computing the value at runtime.
5801 If we are in an integral constant expression context, then we might
5802 could accept any constant expression; hard to say without analysis.
5803 Rather than open the barn door too wide right away, allow only integer
5804 constant expressions here. */
5806 index = cp_parser_constant_expression (parser, false, NULL);
5808 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5810 /* Look for the closing `]'. */
5811 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5813 /* Build the ARRAY_REF. */
5814 postfix_expression = grok_array_decl (postfix_expression, index);
5816 /* When not doing offsetof, array references are not permitted in
5817 constant-expressions. */
5819 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5820 postfix_expression = error_mark_node;
5822 return postfix_expression;
5825 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5826 by cp_parser_builtin_offsetof. We're looking for
5828 postfix-expression . template [opt] id-expression
5829 postfix-expression . pseudo-destructor-name
5830 postfix-expression -> template [opt] id-expression
5831 postfix-expression -> pseudo-destructor-name
5833 FOR_OFFSETOF is set if we're being called in that context. That sorta
5834 limits what of the above we'll actually accept, but nevermind.
5835 TOKEN_TYPE is the "." or "->" token, which will already have been
5836 removed from the stream. */
5839 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5840 enum cpp_ttype token_type,
5841 tree postfix_expression,
5842 bool for_offsetof, cp_id_kind *idk,
5843 location_t location)
5847 bool pseudo_destructor_p;
5848 tree scope = NULL_TREE;
5850 /* If this is a `->' operator, dereference the pointer. */
5851 if (token_type == CPP_DEREF)
5852 postfix_expression = build_x_arrow (postfix_expression);
5853 /* Check to see whether or not the expression is type-dependent. */
5854 dependent_p = type_dependent_expression_p (postfix_expression);
5855 /* The identifier following the `->' or `.' is not qualified. */
5856 parser->scope = NULL_TREE;
5857 parser->qualifying_scope = NULL_TREE;
5858 parser->object_scope = NULL_TREE;
5859 *idk = CP_ID_KIND_NONE;
5861 /* Enter the scope corresponding to the type of the object
5862 given by the POSTFIX_EXPRESSION. */
5863 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5865 scope = TREE_TYPE (postfix_expression);
5866 /* According to the standard, no expression should ever have
5867 reference type. Unfortunately, we do not currently match
5868 the standard in this respect in that our internal representation
5869 of an expression may have reference type even when the standard
5870 says it does not. Therefore, we have to manually obtain the
5871 underlying type here. */
5872 scope = non_reference (scope);
5873 /* The type of the POSTFIX_EXPRESSION must be complete. */
5874 if (scope == unknown_type_node)
5876 error_at (location, "%qE does not have class type",
5877 postfix_expression);
5880 /* Unlike the object expression in other contexts, *this is not
5881 required to be of complete type for purposes of class member
5882 access (5.2.5) outside the member function body. */
5883 else if (scope != current_class_ref
5884 && !(processing_template_decl && scope == current_class_type))
5885 scope = complete_type_or_else (scope, NULL_TREE);
5886 /* Let the name lookup machinery know that we are processing a
5887 class member access expression. */
5888 parser->context->object_type = scope;
5889 /* If something went wrong, we want to be able to discern that case,
5890 as opposed to the case where there was no SCOPE due to the type
5891 of expression being dependent. */
5893 scope = error_mark_node;
5894 /* If the SCOPE was erroneous, make the various semantic analysis
5895 functions exit quickly -- and without issuing additional error
5897 if (scope == error_mark_node)
5898 postfix_expression = error_mark_node;
5901 /* Assume this expression is not a pseudo-destructor access. */
5902 pseudo_destructor_p = false;
5904 /* If the SCOPE is a scalar type, then, if this is a valid program,
5905 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5906 is type dependent, it can be pseudo-destructor-name or something else.
5907 Try to parse it as pseudo-destructor-name first. */
5908 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5913 cp_parser_parse_tentatively (parser);
5914 /* Parse the pseudo-destructor-name. */
5916 cp_parser_pseudo_destructor_name (parser, &s, &type);
5918 && (cp_parser_error_occurred (parser)
5919 || TREE_CODE (type) != TYPE_DECL
5920 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5921 cp_parser_abort_tentative_parse (parser);
5922 else if (cp_parser_parse_definitely (parser))
5924 pseudo_destructor_p = true;
5926 = finish_pseudo_destructor_expr (postfix_expression,
5927 s, TREE_TYPE (type));
5931 if (!pseudo_destructor_p)
5933 /* If the SCOPE is not a scalar type, we are looking at an
5934 ordinary class member access expression, rather than a
5935 pseudo-destructor-name. */
5937 cp_token *token = cp_lexer_peek_token (parser->lexer);
5938 /* Parse the id-expression. */
5939 name = (cp_parser_id_expression
5941 cp_parser_optional_template_keyword (parser),
5942 /*check_dependency_p=*/true,
5944 /*declarator_p=*/false,
5945 /*optional_p=*/false));
5946 /* In general, build a SCOPE_REF if the member name is qualified.
5947 However, if the name was not dependent and has already been
5948 resolved; there is no need to build the SCOPE_REF. For example;
5950 struct X { void f(); };
5951 template <typename T> void f(T* t) { t->X::f(); }
5953 Even though "t" is dependent, "X::f" is not and has been resolved
5954 to a BASELINK; there is no need to include scope information. */
5956 /* But we do need to remember that there was an explicit scope for
5957 virtual function calls. */
5959 *idk = CP_ID_KIND_QUALIFIED;
5961 /* If the name is a template-id that names a type, we will get a
5962 TYPE_DECL here. That is invalid code. */
5963 if (TREE_CODE (name) == TYPE_DECL)
5965 error_at (token->location, "invalid use of %qD", name);
5966 postfix_expression = error_mark_node;
5970 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5972 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
5974 error_at (token->location, "%<%D::%D%> is not a class member",
5975 parser->scope, name);
5976 postfix_expression = error_mark_node;
5979 name = build_qualified_name (/*type=*/NULL_TREE,
5983 parser->scope = NULL_TREE;
5984 parser->qualifying_scope = NULL_TREE;
5985 parser->object_scope = NULL_TREE;
5987 if (scope && name && BASELINK_P (name))
5988 adjust_result_of_qualified_name_lookup
5989 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5991 = finish_class_member_access_expr (postfix_expression, name,
5993 tf_warning_or_error);
5997 /* We no longer need to look up names in the scope of the object on
5998 the left-hand side of the `.' or `->' operator. */
5999 parser->context->object_type = NULL_TREE;
6001 /* Outside of offsetof, these operators may not appear in
6002 constant-expressions. */
6004 && (cp_parser_non_integral_constant_expression
6005 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6006 postfix_expression = error_mark_node;
6008 return postfix_expression;
6011 /* Parse a parenthesized expression-list.
6014 assignment-expression
6015 expression-list, assignment-expression
6020 identifier, expression-list
6022 CAST_P is true if this expression is the target of a cast.
6024 ALLOW_EXPANSION_P is true if this expression allows expansion of an
6027 Returns a vector of trees. Each element is a representation of an
6028 assignment-expression. NULL is returned if the ( and or ) are
6029 missing. An empty, but allocated, vector is returned on no
6030 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
6031 if we are parsing an attribute list for an attribute that wants a
6032 plain identifier argument, normal_attr for an attribute that wants
6033 an expression, or non_attr if we aren't parsing an attribute list. If
6034 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6035 not all of the expressions in the list were constant. */
6037 static VEC(tree,gc) *
6038 cp_parser_parenthesized_expression_list (cp_parser* parser,
6039 int is_attribute_list,
6041 bool allow_expansion_p,
6042 bool *non_constant_p)
6044 VEC(tree,gc) *expression_list;
6045 bool fold_expr_p = is_attribute_list != non_attr;
6046 tree identifier = NULL_TREE;
6047 bool saved_greater_than_is_operator_p;
6049 /* Assume all the expressions will be constant. */
6051 *non_constant_p = false;
6053 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6056 expression_list = make_tree_vector ();
6058 /* Within a parenthesized expression, a `>' token is always
6059 the greater-than operator. */
6060 saved_greater_than_is_operator_p
6061 = parser->greater_than_is_operator_p;
6062 parser->greater_than_is_operator_p = true;
6064 /* Consume expressions until there are no more. */
6065 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6070 /* At the beginning of attribute lists, check to see if the
6071 next token is an identifier. */
6072 if (is_attribute_list == id_attr
6073 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6077 /* Consume the identifier. */
6078 token = cp_lexer_consume_token (parser->lexer);
6079 /* Save the identifier. */
6080 identifier = token->u.value;
6084 bool expr_non_constant_p;
6086 /* Parse the next assignment-expression. */
6087 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6089 /* A braced-init-list. */
6090 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6091 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6092 if (non_constant_p && expr_non_constant_p)
6093 *non_constant_p = true;
6095 else if (non_constant_p)
6097 expr = (cp_parser_constant_expression
6098 (parser, /*allow_non_constant_p=*/true,
6099 &expr_non_constant_p));
6100 if (expr_non_constant_p)
6101 *non_constant_p = true;
6104 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6107 expr = fold_non_dependent_expr (expr);
6109 /* If we have an ellipsis, then this is an expression
6111 if (allow_expansion_p
6112 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6114 /* Consume the `...'. */
6115 cp_lexer_consume_token (parser->lexer);
6117 /* Build the argument pack. */
6118 expr = make_pack_expansion (expr);
6121 /* Add it to the list. We add error_mark_node
6122 expressions to the list, so that we can still tell if
6123 the correct form for a parenthesized expression-list
6124 is found. That gives better errors. */
6125 VEC_safe_push (tree, gc, expression_list, expr);
6127 if (expr == error_mark_node)
6131 /* After the first item, attribute lists look the same as
6132 expression lists. */
6133 is_attribute_list = non_attr;
6136 /* If the next token isn't a `,', then we are done. */
6137 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6140 /* Otherwise, consume the `,' and keep going. */
6141 cp_lexer_consume_token (parser->lexer);
6144 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6149 /* We try and resync to an unnested comma, as that will give the
6150 user better diagnostics. */
6151 ending = cp_parser_skip_to_closing_parenthesis (parser,
6152 /*recovering=*/true,
6154 /*consume_paren=*/true);
6159 parser->greater_than_is_operator_p
6160 = saved_greater_than_is_operator_p;
6165 parser->greater_than_is_operator_p
6166 = saved_greater_than_is_operator_p;
6169 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6171 return expression_list;
6174 /* Parse a pseudo-destructor-name.
6176 pseudo-destructor-name:
6177 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6178 :: [opt] nested-name-specifier template template-id :: ~ type-name
6179 :: [opt] nested-name-specifier [opt] ~ type-name
6181 If either of the first two productions is used, sets *SCOPE to the
6182 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
6183 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
6184 or ERROR_MARK_NODE if the parse fails. */
6187 cp_parser_pseudo_destructor_name (cp_parser* parser,
6191 bool nested_name_specifier_p;
6193 /* Assume that things will not work out. */
6194 *type = error_mark_node;
6196 /* Look for the optional `::' operator. */
6197 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6198 /* Look for the optional nested-name-specifier. */
6199 nested_name_specifier_p
6200 = (cp_parser_nested_name_specifier_opt (parser,
6201 /*typename_keyword_p=*/false,
6202 /*check_dependency_p=*/true,
6204 /*is_declaration=*/false)
6206 /* Now, if we saw a nested-name-specifier, we might be doing the
6207 second production. */
6208 if (nested_name_specifier_p
6209 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6211 /* Consume the `template' keyword. */
6212 cp_lexer_consume_token (parser->lexer);
6213 /* Parse the template-id. */
6214 cp_parser_template_id (parser,
6215 /*template_keyword_p=*/true,
6216 /*check_dependency_p=*/false,
6217 /*is_declaration=*/true);
6218 /* Look for the `::' token. */
6219 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6221 /* If the next token is not a `~', then there might be some
6222 additional qualification. */
6223 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6225 /* At this point, we're looking for "type-name :: ~". The type-name
6226 must not be a class-name, since this is a pseudo-destructor. So,
6227 it must be either an enum-name, or a typedef-name -- both of which
6228 are just identifiers. So, we peek ahead to check that the "::"
6229 and "~" tokens are present; if they are not, then we can avoid
6230 calling type_name. */
6231 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6232 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6233 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6235 cp_parser_error (parser, "non-scalar type");
6239 /* Look for the type-name. */
6240 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6241 if (*scope == error_mark_node)
6244 /* Look for the `::' token. */
6245 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6250 /* Look for the `~'. */
6251 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6253 /* Once we see the ~, this has to be a pseudo-destructor. */
6254 if (!processing_template_decl && !cp_parser_error_occurred (parser))
6255 cp_parser_commit_to_tentative_parse (parser);
6257 /* Look for the type-name again. We are not responsible for
6258 checking that it matches the first type-name. */
6259 *type = cp_parser_nonclass_name (parser);
6262 /* Parse a unary-expression.
6268 unary-operator cast-expression
6269 sizeof unary-expression
6271 alignof ( type-id ) [C++0x]
6278 __extension__ cast-expression
6279 __alignof__ unary-expression
6280 __alignof__ ( type-id )
6281 alignof unary-expression [C++0x]
6282 __real__ cast-expression
6283 __imag__ cast-expression
6286 ADDRESS_P is true iff the unary-expression is appearing as the
6287 operand of the `&' operator. CAST_P is true if this expression is
6288 the target of a cast.
6290 Returns a representation of the expression. */
6293 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6297 enum tree_code unary_operator;
6299 /* Peek at the next token. */
6300 token = cp_lexer_peek_token (parser->lexer);
6301 /* Some keywords give away the kind of expression. */
6302 if (token->type == CPP_KEYWORD)
6304 enum rid keyword = token->keyword;
6314 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6315 /* Consume the token. */
6316 cp_lexer_consume_token (parser->lexer);
6317 /* Parse the operand. */
6318 operand = cp_parser_sizeof_operand (parser, keyword);
6320 if (TYPE_P (operand))
6321 return cxx_sizeof_or_alignof_type (operand, op, true);
6324 /* ISO C++ defines alignof only with types, not with
6325 expressions. So pedwarn if alignof is used with a non-
6326 type expression. However, __alignof__ is ok. */
6327 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6328 pedwarn (token->location, OPT_pedantic,
6329 "ISO C++ does not allow %<alignof%> "
6332 return cxx_sizeof_or_alignof_expr (operand, op, true);
6337 return cp_parser_new_expression (parser);
6340 return cp_parser_delete_expression (parser);
6344 /* The saved value of the PEDANTIC flag. */
6348 /* Save away the PEDANTIC flag. */
6349 cp_parser_extension_opt (parser, &saved_pedantic);
6350 /* Parse the cast-expression. */
6351 expr = cp_parser_simple_cast_expression (parser);
6352 /* Restore the PEDANTIC flag. */
6353 pedantic = saved_pedantic;
6363 /* Consume the `__real__' or `__imag__' token. */
6364 cp_lexer_consume_token (parser->lexer);
6365 /* Parse the cast-expression. */
6366 expression = cp_parser_simple_cast_expression (parser);
6367 /* Create the complete representation. */
6368 return build_x_unary_op ((keyword == RID_REALPART
6369 ? REALPART_EXPR : IMAGPART_EXPR),
6371 tf_warning_or_error);
6378 const char *saved_message;
6379 bool saved_integral_constant_expression_p;
6380 bool saved_non_integral_constant_expression_p;
6381 bool saved_greater_than_is_operator_p;
6383 cp_lexer_consume_token (parser->lexer);
6384 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6386 saved_message = parser->type_definition_forbidden_message;
6387 parser->type_definition_forbidden_message
6388 = G_("types may not be defined in %<noexcept%> expressions");
6390 saved_integral_constant_expression_p
6391 = parser->integral_constant_expression_p;
6392 saved_non_integral_constant_expression_p
6393 = parser->non_integral_constant_expression_p;
6394 parser->integral_constant_expression_p = false;
6396 saved_greater_than_is_operator_p
6397 = parser->greater_than_is_operator_p;
6398 parser->greater_than_is_operator_p = true;
6400 ++cp_unevaluated_operand;
6401 ++c_inhibit_evaluation_warnings;
6402 expr = cp_parser_expression (parser, false, NULL);
6403 --c_inhibit_evaluation_warnings;
6404 --cp_unevaluated_operand;
6406 parser->greater_than_is_operator_p
6407 = saved_greater_than_is_operator_p;
6409 parser->integral_constant_expression_p
6410 = saved_integral_constant_expression_p;
6411 parser->non_integral_constant_expression_p
6412 = saved_non_integral_constant_expression_p;
6414 parser->type_definition_forbidden_message = saved_message;
6416 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6417 return finish_noexcept_expr (expr, tf_warning_or_error);
6425 /* Look for the `:: new' and `:: delete', which also signal the
6426 beginning of a new-expression, or delete-expression,
6427 respectively. If the next token is `::', then it might be one of
6429 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6433 /* See if the token after the `::' is one of the keywords in
6434 which we're interested. */
6435 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6436 /* If it's `new', we have a new-expression. */
6437 if (keyword == RID_NEW)
6438 return cp_parser_new_expression (parser);
6439 /* Similarly, for `delete'. */
6440 else if (keyword == RID_DELETE)
6441 return cp_parser_delete_expression (parser);
6444 /* Look for a unary operator. */
6445 unary_operator = cp_parser_unary_operator (token);
6446 /* The `++' and `--' operators can be handled similarly, even though
6447 they are not technically unary-operators in the grammar. */
6448 if (unary_operator == ERROR_MARK)
6450 if (token->type == CPP_PLUS_PLUS)
6451 unary_operator = PREINCREMENT_EXPR;
6452 else if (token->type == CPP_MINUS_MINUS)
6453 unary_operator = PREDECREMENT_EXPR;
6454 /* Handle the GNU address-of-label extension. */
6455 else if (cp_parser_allow_gnu_extensions_p (parser)
6456 && token->type == CPP_AND_AND)
6460 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6462 /* Consume the '&&' token. */
6463 cp_lexer_consume_token (parser->lexer);
6464 /* Look for the identifier. */
6465 identifier = cp_parser_identifier (parser);
6466 /* Create an expression representing the address. */
6467 expression = finish_label_address_expr (identifier, loc);
6468 if (cp_parser_non_integral_constant_expression (parser,
6470 expression = error_mark_node;
6474 if (unary_operator != ERROR_MARK)
6476 tree cast_expression;
6477 tree expression = error_mark_node;
6478 non_integral_constant non_constant_p = NIC_NONE;
6480 /* Consume the operator token. */
6481 token = cp_lexer_consume_token (parser->lexer);
6482 /* Parse the cast-expression. */
6484 = cp_parser_cast_expression (parser,
6485 unary_operator == ADDR_EXPR,
6486 /*cast_p=*/false, pidk);
6487 /* Now, build an appropriate representation. */
6488 switch (unary_operator)
6491 non_constant_p = NIC_STAR;
6492 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6493 tf_warning_or_error);
6497 non_constant_p = NIC_ADDR;
6500 expression = build_x_unary_op (unary_operator, cast_expression,
6501 tf_warning_or_error);
6504 case PREINCREMENT_EXPR:
6505 case PREDECREMENT_EXPR:
6506 non_constant_p = unary_operator == PREINCREMENT_EXPR
6507 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6509 case UNARY_PLUS_EXPR:
6511 case TRUTH_NOT_EXPR:
6512 expression = finish_unary_op_expr (unary_operator, cast_expression);
6519 if (non_constant_p != NIC_NONE
6520 && cp_parser_non_integral_constant_expression (parser,
6522 expression = error_mark_node;
6527 return cp_parser_postfix_expression (parser, address_p, cast_p,
6528 /*member_access_only_p=*/false,
6532 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
6533 unary-operator, the corresponding tree code is returned. */
6535 static enum tree_code
6536 cp_parser_unary_operator (cp_token* token)
6538 switch (token->type)
6541 return INDIRECT_REF;
6547 return UNARY_PLUS_EXPR;
6553 return TRUTH_NOT_EXPR;
6556 return BIT_NOT_EXPR;
6563 /* Parse a new-expression.
6566 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6567 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6569 Returns a representation of the expression. */
6572 cp_parser_new_expression (cp_parser* parser)
6574 bool global_scope_p;
6575 VEC(tree,gc) *placement;
6577 VEC(tree,gc) *initializer;
6581 /* Look for the optional `::' operator. */
6583 = (cp_parser_global_scope_opt (parser,
6584 /*current_scope_valid_p=*/false)
6586 /* Look for the `new' operator. */
6587 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6588 /* There's no easy way to tell a new-placement from the
6589 `( type-id )' construct. */
6590 cp_parser_parse_tentatively (parser);
6591 /* Look for a new-placement. */
6592 placement = cp_parser_new_placement (parser);
6593 /* If that didn't work out, there's no new-placement. */
6594 if (!cp_parser_parse_definitely (parser))
6596 if (placement != NULL)
6597 release_tree_vector (placement);
6601 /* If the next token is a `(', then we have a parenthesized
6603 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6606 /* Consume the `('. */
6607 cp_lexer_consume_token (parser->lexer);
6608 /* Parse the type-id. */
6609 type = cp_parser_type_id (parser);
6610 /* Look for the closing `)'. */
6611 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6612 token = cp_lexer_peek_token (parser->lexer);
6613 /* There should not be a direct-new-declarator in this production,
6614 but GCC used to allowed this, so we check and emit a sensible error
6615 message for this case. */
6616 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6618 error_at (token->location,
6619 "array bound forbidden after parenthesized type-id");
6620 inform (token->location,
6621 "try removing the parentheses around the type-id");
6622 cp_parser_direct_new_declarator (parser);
6626 /* Otherwise, there must be a new-type-id. */
6628 type = cp_parser_new_type_id (parser, &nelts);
6630 /* If the next token is a `(' or '{', then we have a new-initializer. */
6631 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6632 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6633 initializer = cp_parser_new_initializer (parser);
6637 /* A new-expression may not appear in an integral constant
6639 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6640 ret = error_mark_node;
6643 /* Create a representation of the new-expression. */
6644 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6645 tf_warning_or_error);
6648 if (placement != NULL)
6649 release_tree_vector (placement);
6650 if (initializer != NULL)
6651 release_tree_vector (initializer);
6656 /* Parse a new-placement.
6661 Returns the same representation as for an expression-list. */
6663 static VEC(tree,gc) *
6664 cp_parser_new_placement (cp_parser* parser)
6666 VEC(tree,gc) *expression_list;
6668 /* Parse the expression-list. */
6669 expression_list = (cp_parser_parenthesized_expression_list
6670 (parser, non_attr, /*cast_p=*/false,
6671 /*allow_expansion_p=*/true,
6672 /*non_constant_p=*/NULL));
6674 return expression_list;
6677 /* Parse a new-type-id.
6680 type-specifier-seq new-declarator [opt]
6682 Returns the TYPE allocated. If the new-type-id indicates an array
6683 type, *NELTS is set to the number of elements in the last array
6684 bound; the TYPE will not include the last array bound. */
6687 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6689 cp_decl_specifier_seq type_specifier_seq;
6690 cp_declarator *new_declarator;
6691 cp_declarator *declarator;
6692 cp_declarator *outer_declarator;
6693 const char *saved_message;
6696 /* The type-specifier sequence must not contain type definitions.
6697 (It cannot contain declarations of new types either, but if they
6698 are not definitions we will catch that because they are not
6700 saved_message = parser->type_definition_forbidden_message;
6701 parser->type_definition_forbidden_message
6702 = G_("types may not be defined in a new-type-id");
6703 /* Parse the type-specifier-seq. */
6704 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6705 /*is_trailing_return=*/false,
6706 &type_specifier_seq);
6707 /* Restore the old message. */
6708 parser->type_definition_forbidden_message = saved_message;
6709 /* Parse the new-declarator. */
6710 new_declarator = cp_parser_new_declarator_opt (parser);
6712 /* Determine the number of elements in the last array dimension, if
6715 /* Skip down to the last array dimension. */
6716 declarator = new_declarator;
6717 outer_declarator = NULL;
6718 while (declarator && (declarator->kind == cdk_pointer
6719 || declarator->kind == cdk_ptrmem))
6721 outer_declarator = declarator;
6722 declarator = declarator->declarator;
6725 && declarator->kind == cdk_array
6726 && declarator->declarator
6727 && declarator->declarator->kind == cdk_array)
6729 outer_declarator = declarator;
6730 declarator = declarator->declarator;
6733 if (declarator && declarator->kind == cdk_array)
6735 *nelts = declarator->u.array.bounds;
6736 if (*nelts == error_mark_node)
6737 *nelts = integer_one_node;
6739 if (outer_declarator)
6740 outer_declarator->declarator = declarator->declarator;
6742 new_declarator = NULL;
6745 type = groktypename (&type_specifier_seq, new_declarator, false);
6749 /* Parse an (optional) new-declarator.
6752 ptr-operator new-declarator [opt]
6753 direct-new-declarator
6755 Returns the declarator. */
6757 static cp_declarator *
6758 cp_parser_new_declarator_opt (cp_parser* parser)
6760 enum tree_code code;
6762 cp_cv_quals cv_quals;
6764 /* We don't know if there's a ptr-operator next, or not. */
6765 cp_parser_parse_tentatively (parser);
6766 /* Look for a ptr-operator. */
6767 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6768 /* If that worked, look for more new-declarators. */
6769 if (cp_parser_parse_definitely (parser))
6771 cp_declarator *declarator;
6773 /* Parse another optional declarator. */
6774 declarator = cp_parser_new_declarator_opt (parser);
6776 return cp_parser_make_indirect_declarator
6777 (code, type, cv_quals, declarator);
6780 /* If the next token is a `[', there is a direct-new-declarator. */
6781 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6782 return cp_parser_direct_new_declarator (parser);
6787 /* Parse a direct-new-declarator.
6789 direct-new-declarator:
6791 direct-new-declarator [constant-expression]
6795 static cp_declarator *
6796 cp_parser_direct_new_declarator (cp_parser* parser)
6798 cp_declarator *declarator = NULL;
6804 /* Look for the opening `['. */
6805 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6806 /* The first expression is not required to be constant. */
6809 cp_token *token = cp_lexer_peek_token (parser->lexer);
6810 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6811 /* The standard requires that the expression have integral
6812 type. DR 74 adds enumeration types. We believe that the
6813 real intent is that these expressions be handled like the
6814 expression in a `switch' condition, which also allows
6815 classes with a single conversion to integral or
6816 enumeration type. */
6817 if (!processing_template_decl)
6820 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6825 error_at (token->location,
6826 "expression in new-declarator must have integral "
6827 "or enumeration type");
6828 expression = error_mark_node;
6832 /* But all the other expressions must be. */
6835 = cp_parser_constant_expression (parser,
6836 /*allow_non_constant=*/false,
6838 /* Look for the closing `]'. */
6839 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6841 /* Add this bound to the declarator. */
6842 declarator = make_array_declarator (declarator, expression);
6844 /* If the next token is not a `[', then there are no more
6846 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6853 /* Parse a new-initializer.
6856 ( expression-list [opt] )
6859 Returns a representation of the expression-list. */
6861 static VEC(tree,gc) *
6862 cp_parser_new_initializer (cp_parser* parser)
6864 VEC(tree,gc) *expression_list;
6866 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6869 bool expr_non_constant_p;
6870 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6871 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6872 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6873 expression_list = make_tree_vector_single (t);
6876 expression_list = (cp_parser_parenthesized_expression_list
6877 (parser, non_attr, /*cast_p=*/false,
6878 /*allow_expansion_p=*/true,
6879 /*non_constant_p=*/NULL));
6881 return expression_list;
6884 /* Parse a delete-expression.
6887 :: [opt] delete cast-expression
6888 :: [opt] delete [ ] cast-expression
6890 Returns a representation of the expression. */
6893 cp_parser_delete_expression (cp_parser* parser)
6895 bool global_scope_p;
6899 /* Look for the optional `::' operator. */
6901 = (cp_parser_global_scope_opt (parser,
6902 /*current_scope_valid_p=*/false)
6904 /* Look for the `delete' keyword. */
6905 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6906 /* See if the array syntax is in use. */
6907 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6909 /* Consume the `[' token. */
6910 cp_lexer_consume_token (parser->lexer);
6911 /* Look for the `]' token. */
6912 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6913 /* Remember that this is the `[]' construct. */
6919 /* Parse the cast-expression. */
6920 expression = cp_parser_simple_cast_expression (parser);
6922 /* A delete-expression may not appear in an integral constant
6924 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6925 return error_mark_node;
6927 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6928 tf_warning_or_error);
6931 /* Returns true if TOKEN may start a cast-expression and false
6935 cp_parser_token_starts_cast_expression (cp_token *token)
6937 switch (token->type)
6943 case CPP_CLOSE_SQUARE:
6944 case CPP_CLOSE_PAREN:
6945 case CPP_CLOSE_BRACE:
6949 case CPP_DEREF_STAR:
6957 case CPP_GREATER_EQ:
6977 /* '[' may start a primary-expression in obj-c++. */
6978 case CPP_OPEN_SQUARE:
6979 return c_dialect_objc ();
6986 /* Parse a cast-expression.
6990 ( type-id ) cast-expression
6992 ADDRESS_P is true iff the unary-expression is appearing as the
6993 operand of the `&' operator. CAST_P is true if this expression is
6994 the target of a cast.
6996 Returns a representation of the expression. */
6999 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7002 /* If it's a `(', then we might be looking at a cast. */
7003 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7005 tree type = NULL_TREE;
7006 tree expr = NULL_TREE;
7007 bool compound_literal_p;
7008 const char *saved_message;
7010 /* There's no way to know yet whether or not this is a cast.
7011 For example, `(int (3))' is a unary-expression, while `(int)
7012 3' is a cast. So, we resort to parsing tentatively. */
7013 cp_parser_parse_tentatively (parser);
7014 /* Types may not be defined in a cast. */
7015 saved_message = parser->type_definition_forbidden_message;
7016 parser->type_definition_forbidden_message
7017 = G_("types may not be defined in casts");
7018 /* Consume the `('. */
7019 cp_lexer_consume_token (parser->lexer);
7020 /* A very tricky bit is that `(struct S) { 3 }' is a
7021 compound-literal (which we permit in C++ as an extension).
7022 But, that construct is not a cast-expression -- it is a
7023 postfix-expression. (The reason is that `(struct S) { 3 }.i'
7024 is legal; if the compound-literal were a cast-expression,
7025 you'd need an extra set of parentheses.) But, if we parse
7026 the type-id, and it happens to be a class-specifier, then we
7027 will commit to the parse at that point, because we cannot
7028 undo the action that is done when creating a new class. So,
7029 then we cannot back up and do a postfix-expression.
7031 Therefore, we scan ahead to the closing `)', and check to see
7032 if the token after the `)' is a `{'. If so, we are not
7033 looking at a cast-expression.
7035 Save tokens so that we can put them back. */
7036 cp_lexer_save_tokens (parser->lexer);
7037 /* Skip tokens until the next token is a closing parenthesis.
7038 If we find the closing `)', and the next token is a `{', then
7039 we are looking at a compound-literal. */
7041 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7042 /*consume_paren=*/true)
7043 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7044 /* Roll back the tokens we skipped. */
7045 cp_lexer_rollback_tokens (parser->lexer);
7046 /* If we were looking at a compound-literal, simulate an error
7047 so that the call to cp_parser_parse_definitely below will
7049 if (compound_literal_p)
7050 cp_parser_simulate_error (parser);
7053 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7054 parser->in_type_id_in_expr_p = true;
7055 /* Look for the type-id. */
7056 type = cp_parser_type_id (parser);
7057 /* Look for the closing `)'. */
7058 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7059 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7062 /* Restore the saved message. */
7063 parser->type_definition_forbidden_message = saved_message;
7065 /* At this point this can only be either a cast or a
7066 parenthesized ctor such as `(T ())' that looks like a cast to
7067 function returning T. */
7068 if (!cp_parser_error_occurred (parser)
7069 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7072 cp_parser_parse_definitely (parser);
7073 expr = cp_parser_cast_expression (parser,
7074 /*address_p=*/false,
7075 /*cast_p=*/true, pidk);
7077 /* Warn about old-style casts, if so requested. */
7078 if (warn_old_style_cast
7079 && !in_system_header
7080 && !VOID_TYPE_P (type)
7081 && current_lang_name != lang_name_c)
7082 warning (OPT_Wold_style_cast, "use of old-style cast");
7084 /* Only type conversions to integral or enumeration types
7085 can be used in constant-expressions. */
7086 if (!cast_valid_in_integral_constant_expression_p (type)
7087 && cp_parser_non_integral_constant_expression (parser,
7089 return error_mark_node;
7091 /* Perform the cast. */
7092 expr = build_c_cast (input_location, type, expr);
7096 cp_parser_abort_tentative_parse (parser);
7099 /* If we get here, then it's not a cast, so it must be a
7100 unary-expression. */
7101 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7104 /* Parse a binary expression of the general form:
7108 pm-expression .* cast-expression
7109 pm-expression ->* cast-expression
7111 multiplicative-expression:
7113 multiplicative-expression * pm-expression
7114 multiplicative-expression / pm-expression
7115 multiplicative-expression % pm-expression
7117 additive-expression:
7118 multiplicative-expression
7119 additive-expression + multiplicative-expression
7120 additive-expression - multiplicative-expression
7124 shift-expression << additive-expression
7125 shift-expression >> additive-expression
7127 relational-expression:
7129 relational-expression < shift-expression
7130 relational-expression > shift-expression
7131 relational-expression <= shift-expression
7132 relational-expression >= shift-expression
7136 relational-expression:
7137 relational-expression <? shift-expression
7138 relational-expression >? shift-expression
7140 equality-expression:
7141 relational-expression
7142 equality-expression == relational-expression
7143 equality-expression != relational-expression
7147 and-expression & equality-expression
7149 exclusive-or-expression:
7151 exclusive-or-expression ^ and-expression
7153 inclusive-or-expression:
7154 exclusive-or-expression
7155 inclusive-or-expression | exclusive-or-expression
7157 logical-and-expression:
7158 inclusive-or-expression
7159 logical-and-expression && inclusive-or-expression
7161 logical-or-expression:
7162 logical-and-expression
7163 logical-or-expression || logical-and-expression
7165 All these are implemented with a single function like:
7168 simple-cast-expression
7169 binary-expression <token> binary-expression
7171 CAST_P is true if this expression is the target of a cast.
7173 The binops_by_token map is used to get the tree codes for each <token> type.
7174 binary-expressions are associated according to a precedence table. */
7176 #define TOKEN_PRECEDENCE(token) \
7177 (((token->type == CPP_GREATER \
7178 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7179 && !parser->greater_than_is_operator_p) \
7180 ? PREC_NOT_OPERATOR \
7181 : binops_by_token[token->type].prec)
7184 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7185 bool no_toplevel_fold_p,
7186 enum cp_parser_prec prec,
7189 cp_parser_expression_stack stack;
7190 cp_parser_expression_stack_entry *sp = &stack[0];
7193 enum tree_code tree_type, lhs_type, rhs_type;
7194 enum cp_parser_prec new_prec, lookahead_prec;
7197 /* Parse the first expression. */
7198 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7199 lhs_type = ERROR_MARK;
7203 /* Get an operator token. */
7204 token = cp_lexer_peek_token (parser->lexer);
7206 if (warn_cxx0x_compat
7207 && token->type == CPP_RSHIFT
7208 && !parser->greater_than_is_operator_p)
7210 if (warning_at (token->location, OPT_Wc__0x_compat,
7211 "%<>>%> operator is treated as"
7212 " two right angle brackets in C++11"))
7213 inform (token->location,
7214 "suggest parentheses around %<>>%> expression");
7217 new_prec = TOKEN_PRECEDENCE (token);
7219 /* Popping an entry off the stack means we completed a subexpression:
7220 - either we found a token which is not an operator (`>' where it is not
7221 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7222 will happen repeatedly;
7223 - or, we found an operator which has lower priority. This is the case
7224 where the recursive descent *ascends*, as in `3 * 4 + 5' after
7226 if (new_prec <= prec)
7235 tree_type = binops_by_token[token->type].tree_type;
7237 /* We used the operator token. */
7238 cp_lexer_consume_token (parser->lexer);
7240 /* For "false && x" or "true || x", x will never be executed;
7241 disable warnings while evaluating it. */
7242 if (tree_type == TRUTH_ANDIF_EXPR)
7243 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7244 else if (tree_type == TRUTH_ORIF_EXPR)
7245 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7247 /* Extract another operand. It may be the RHS of this expression
7248 or the LHS of a new, higher priority expression. */
7249 rhs = cp_parser_simple_cast_expression (parser);
7250 rhs_type = ERROR_MARK;
7252 /* Get another operator token. Look up its precedence to avoid
7253 building a useless (immediately popped) stack entry for common
7254 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
7255 token = cp_lexer_peek_token (parser->lexer);
7256 lookahead_prec = TOKEN_PRECEDENCE (token);
7257 if (lookahead_prec > new_prec)
7259 /* ... and prepare to parse the RHS of the new, higher priority
7260 expression. Since precedence levels on the stack are
7261 monotonically increasing, we do not have to care about
7264 sp->tree_type = tree_type;
7266 sp->lhs_type = lhs_type;
7269 lhs_type = rhs_type;
7271 new_prec = lookahead_prec;
7275 lookahead_prec = new_prec;
7276 /* If the stack is not empty, we have parsed into LHS the right side
7277 (`4' in the example above) of an expression we had suspended.
7278 We can use the information on the stack to recover the LHS (`3')
7279 from the stack together with the tree code (`MULT_EXPR'), and
7280 the precedence of the higher level subexpression
7281 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
7282 which will be used to actually build the additive expression. */
7285 tree_type = sp->tree_type;
7287 rhs_type = lhs_type;
7289 lhs_type = sp->lhs_type;
7292 /* Undo the disabling of warnings done above. */
7293 if (tree_type == TRUTH_ANDIF_EXPR)
7294 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7295 else if (tree_type == TRUTH_ORIF_EXPR)
7296 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7299 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7300 ERROR_MARK for everything that is not a binary expression.
7301 This makes warn_about_parentheses miss some warnings that
7302 involve unary operators. For unary expressions we should
7303 pass the correct tree_code unless the unary expression was
7304 surrounded by parentheses.
7306 if (no_toplevel_fold_p
7307 && lookahead_prec <= prec
7309 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7310 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7312 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7313 &overload, tf_warning_or_error);
7314 lhs_type = tree_type;
7316 /* If the binary operator required the use of an overloaded operator,
7317 then this expression cannot be an integral constant-expression.
7318 An overloaded operator can be used even if both operands are
7319 otherwise permissible in an integral constant-expression if at
7320 least one of the operands is of enumeration type. */
7323 && cp_parser_non_integral_constant_expression (parser,
7325 return error_mark_node;
7332 /* Parse the `? expression : assignment-expression' part of a
7333 conditional-expression. The LOGICAL_OR_EXPR is the
7334 logical-or-expression that started the conditional-expression.
7335 Returns a representation of the entire conditional-expression.
7337 This routine is used by cp_parser_assignment_expression.
7339 ? expression : assignment-expression
7343 ? : assignment-expression */
7346 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7349 tree assignment_expr;
7350 struct cp_token *token;
7352 /* Consume the `?' token. */
7353 cp_lexer_consume_token (parser->lexer);
7354 token = cp_lexer_peek_token (parser->lexer);
7355 if (cp_parser_allow_gnu_extensions_p (parser)
7356 && token->type == CPP_COLON)
7358 pedwarn (token->location, OPT_pedantic,
7359 "ISO C++ does not allow ?: with omitted middle operand");
7360 /* Implicit true clause. */
7362 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7363 warn_for_omitted_condop (token->location, logical_or_expr);
7367 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7368 parser->colon_corrects_to_scope_p = false;
7369 /* Parse the expression. */
7370 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7371 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7372 c_inhibit_evaluation_warnings +=
7373 ((logical_or_expr == truthvalue_true_node)
7374 - (logical_or_expr == truthvalue_false_node));
7375 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7378 /* The next token should be a `:'. */
7379 cp_parser_require (parser, CPP_COLON, RT_COLON);
7380 /* Parse the assignment-expression. */
7381 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7382 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7384 /* Build the conditional-expression. */
7385 return build_x_conditional_expr (logical_or_expr,
7388 tf_warning_or_error);
7391 /* Parse an assignment-expression.
7393 assignment-expression:
7394 conditional-expression
7395 logical-or-expression assignment-operator assignment_expression
7398 CAST_P is true if this expression is the target of a cast.
7400 Returns a representation for the expression. */
7403 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7408 /* If the next token is the `throw' keyword, then we're looking at
7409 a throw-expression. */
7410 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7411 expr = cp_parser_throw_expression (parser);
7412 /* Otherwise, it must be that we are looking at a
7413 logical-or-expression. */
7416 /* Parse the binary expressions (logical-or-expression). */
7417 expr = cp_parser_binary_expression (parser, cast_p, false,
7418 PREC_NOT_OPERATOR, pidk);
7419 /* If the next token is a `?' then we're actually looking at a
7420 conditional-expression. */
7421 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7422 return cp_parser_question_colon_clause (parser, expr);
7425 enum tree_code assignment_operator;
7427 /* If it's an assignment-operator, we're using the second
7430 = cp_parser_assignment_operator_opt (parser);
7431 if (assignment_operator != ERROR_MARK)
7433 bool non_constant_p;
7435 /* Parse the right-hand side of the assignment. */
7436 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7438 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7439 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7441 /* An assignment may not appear in a
7442 constant-expression. */
7443 if (cp_parser_non_integral_constant_expression (parser,
7445 return error_mark_node;
7446 /* Build the assignment expression. */
7447 expr = build_x_modify_expr (expr,
7448 assignment_operator,
7450 tf_warning_or_error);
7458 /* Parse an (optional) assignment-operator.
7460 assignment-operator: one of
7461 = *= /= %= += -= >>= <<= &= ^= |=
7465 assignment-operator: one of
7468 If the next token is an assignment operator, the corresponding tree
7469 code is returned, and the token is consumed. For example, for
7470 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
7471 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
7472 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
7473 operator, ERROR_MARK is returned. */
7475 static enum tree_code
7476 cp_parser_assignment_operator_opt (cp_parser* parser)
7481 /* Peek at the next token. */
7482 token = cp_lexer_peek_token (parser->lexer);
7484 switch (token->type)
7495 op = TRUNC_DIV_EXPR;
7499 op = TRUNC_MOD_EXPR;
7531 /* Nothing else is an assignment operator. */
7535 /* If it was an assignment operator, consume it. */
7536 if (op != ERROR_MARK)
7537 cp_lexer_consume_token (parser->lexer);
7542 /* Parse an expression.
7545 assignment-expression
7546 expression , assignment-expression
7548 CAST_P is true if this expression is the target of a cast.
7550 Returns a representation of the expression. */
7553 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7555 tree expression = NULL_TREE;
7559 tree assignment_expression;
7561 /* Parse the next assignment-expression. */
7562 assignment_expression
7563 = cp_parser_assignment_expression (parser, cast_p, pidk);
7564 /* If this is the first assignment-expression, we can just
7567 expression = assignment_expression;
7569 expression = build_x_compound_expr (expression,
7570 assignment_expression,
7571 tf_warning_or_error);
7572 /* If the next token is not a comma, then we are done with the
7574 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7576 /* Consume the `,'. */
7577 cp_lexer_consume_token (parser->lexer);
7578 /* A comma operator cannot appear in a constant-expression. */
7579 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7580 expression = error_mark_node;
7586 /* Parse a constant-expression.
7588 constant-expression:
7589 conditional-expression
7591 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7592 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7593 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7594 is false, NON_CONSTANT_P should be NULL. */
7597 cp_parser_constant_expression (cp_parser* parser,
7598 bool allow_non_constant_p,
7599 bool *non_constant_p)
7601 bool saved_integral_constant_expression_p;
7602 bool saved_allow_non_integral_constant_expression_p;
7603 bool saved_non_integral_constant_expression_p;
7606 /* It might seem that we could simply parse the
7607 conditional-expression, and then check to see if it were
7608 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7609 one that the compiler can figure out is constant, possibly after
7610 doing some simplifications or optimizations. The standard has a
7611 precise definition of constant-expression, and we must honor
7612 that, even though it is somewhat more restrictive.
7618 is not a legal declaration, because `(2, 3)' is not a
7619 constant-expression. The `,' operator is forbidden in a
7620 constant-expression. However, GCC's constant-folding machinery
7621 will fold this operation to an INTEGER_CST for `3'. */
7623 /* Save the old settings. */
7624 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7625 saved_allow_non_integral_constant_expression_p
7626 = parser->allow_non_integral_constant_expression_p;
7627 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7628 /* We are now parsing a constant-expression. */
7629 parser->integral_constant_expression_p = true;
7630 parser->allow_non_integral_constant_expression_p
7631 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7632 parser->non_integral_constant_expression_p = false;
7633 /* Although the grammar says "conditional-expression", we parse an
7634 "assignment-expression", which also permits "throw-expression"
7635 and the use of assignment operators. In the case that
7636 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7637 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7638 actually essential that we look for an assignment-expression.
7639 For example, cp_parser_initializer_clauses uses this function to
7640 determine whether a particular assignment-expression is in fact
7642 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7643 /* Restore the old settings. */
7644 parser->integral_constant_expression_p
7645 = saved_integral_constant_expression_p;
7646 parser->allow_non_integral_constant_expression_p
7647 = saved_allow_non_integral_constant_expression_p;
7648 if (cxx_dialect >= cxx0x)
7650 /* Require an rvalue constant expression here; that's what our
7651 callers expect. Reference constant expressions are handled
7652 separately in e.g. cp_parser_template_argument. */
7653 bool is_const = potential_rvalue_constant_expression (expression);
7654 parser->non_integral_constant_expression_p = !is_const;
7655 if (!is_const && !allow_non_constant_p)
7656 require_potential_rvalue_constant_expression (expression);
7658 if (allow_non_constant_p)
7659 *non_constant_p = parser->non_integral_constant_expression_p;
7660 parser->non_integral_constant_expression_p
7661 = saved_non_integral_constant_expression_p;
7666 /* Parse __builtin_offsetof.
7668 offsetof-expression:
7669 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7671 offsetof-member-designator:
7673 | offsetof-member-designator "." id-expression
7674 | offsetof-member-designator "[" expression "]"
7675 | offsetof-member-designator "->" id-expression */
7678 cp_parser_builtin_offsetof (cp_parser *parser)
7680 int save_ice_p, save_non_ice_p;
7685 /* We're about to accept non-integral-constant things, but will
7686 definitely yield an integral constant expression. Save and
7687 restore these values around our local parsing. */
7688 save_ice_p = parser->integral_constant_expression_p;
7689 save_non_ice_p = parser->non_integral_constant_expression_p;
7691 /* Consume the "__builtin_offsetof" token. */
7692 cp_lexer_consume_token (parser->lexer);
7693 /* Consume the opening `('. */
7694 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7695 /* Parse the type-id. */
7696 type = cp_parser_type_id (parser);
7697 /* Look for the `,'. */
7698 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7699 token = cp_lexer_peek_token (parser->lexer);
7701 /* Build the (type *)null that begins the traditional offsetof macro. */
7702 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7703 tf_warning_or_error);
7705 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7706 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7707 true, &dummy, token->location);
7710 token = cp_lexer_peek_token (parser->lexer);
7711 switch (token->type)
7713 case CPP_OPEN_SQUARE:
7714 /* offsetof-member-designator "[" expression "]" */
7715 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7719 /* offsetof-member-designator "->" identifier */
7720 expr = grok_array_decl (expr, integer_zero_node);
7724 /* offsetof-member-designator "." identifier */
7725 cp_lexer_consume_token (parser->lexer);
7726 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7731 case CPP_CLOSE_PAREN:
7732 /* Consume the ")" token. */
7733 cp_lexer_consume_token (parser->lexer);
7737 /* Error. We know the following require will fail, but
7738 that gives the proper error message. */
7739 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7740 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7741 expr = error_mark_node;
7747 /* If we're processing a template, we can't finish the semantics yet.
7748 Otherwise we can fold the entire expression now. */
7749 if (processing_template_decl)
7750 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7752 expr = finish_offsetof (expr);
7755 parser->integral_constant_expression_p = save_ice_p;
7756 parser->non_integral_constant_expression_p = save_non_ice_p;
7761 /* Parse a trait expression.
7763 Returns a representation of the expression, the underlying type
7764 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7767 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7770 tree type1, type2 = NULL_TREE;
7771 bool binary = false;
7772 cp_decl_specifier_seq decl_specs;
7776 case RID_HAS_NOTHROW_ASSIGN:
7777 kind = CPTK_HAS_NOTHROW_ASSIGN;
7779 case RID_HAS_NOTHROW_CONSTRUCTOR:
7780 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7782 case RID_HAS_NOTHROW_COPY:
7783 kind = CPTK_HAS_NOTHROW_COPY;
7785 case RID_HAS_TRIVIAL_ASSIGN:
7786 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7788 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7789 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7791 case RID_HAS_TRIVIAL_COPY:
7792 kind = CPTK_HAS_TRIVIAL_COPY;
7794 case RID_HAS_TRIVIAL_DESTRUCTOR:
7795 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7797 case RID_HAS_VIRTUAL_DESTRUCTOR:
7798 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7800 case RID_IS_ABSTRACT:
7801 kind = CPTK_IS_ABSTRACT;
7803 case RID_IS_BASE_OF:
7804 kind = CPTK_IS_BASE_OF;
7808 kind = CPTK_IS_CLASS;
7810 case RID_IS_CONVERTIBLE_TO:
7811 kind = CPTK_IS_CONVERTIBLE_TO;
7815 kind = CPTK_IS_EMPTY;
7818 kind = CPTK_IS_ENUM;
7820 case RID_IS_LITERAL_TYPE:
7821 kind = CPTK_IS_LITERAL_TYPE;
7826 case RID_IS_POLYMORPHIC:
7827 kind = CPTK_IS_POLYMORPHIC;
7829 case RID_IS_STD_LAYOUT:
7830 kind = CPTK_IS_STD_LAYOUT;
7832 case RID_IS_TRIVIAL:
7833 kind = CPTK_IS_TRIVIAL;
7836 kind = CPTK_IS_UNION;
7838 case RID_UNDERLYING_TYPE:
7839 kind = CPTK_UNDERLYING_TYPE;
7844 case RID_DIRECT_BASES:
7845 kind = CPTK_DIRECT_BASES;
7851 /* Consume the token. */
7852 cp_lexer_consume_token (parser->lexer);
7854 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7856 type1 = cp_parser_type_id (parser);
7858 if (type1 == error_mark_node)
7859 return error_mark_node;
7861 /* Build a trivial decl-specifier-seq. */
7862 clear_decl_specs (&decl_specs);
7863 decl_specs.type = type1;
7865 /* Call grokdeclarator to figure out what type this is. */
7866 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7867 /*initialized=*/0, /*attrlist=*/NULL);
7871 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7873 type2 = cp_parser_type_id (parser);
7875 if (type2 == error_mark_node)
7876 return error_mark_node;
7878 /* Build a trivial decl-specifier-seq. */
7879 clear_decl_specs (&decl_specs);
7880 decl_specs.type = type2;
7882 /* Call grokdeclarator to figure out what type this is. */
7883 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7884 /*initialized=*/0, /*attrlist=*/NULL);
7887 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7889 /* Complete the trait expression, which may mean either processing
7890 the trait expr now or saving it for template instantiation. */
7893 case CPTK_UNDERLYING_TYPE:
7894 return finish_underlying_type (type1);
7896 return finish_bases (type1, false);
7897 case CPTK_DIRECT_BASES:
7898 return finish_bases (type1, true);
7900 return finish_trait_expr (kind, type1, type2);
7904 /* Lambdas that appear in variable initializer or default argument scope
7905 get that in their mangling, so we need to record it. We might as well
7906 use the count for function and namespace scopes as well. */
7907 static GTY(()) tree lambda_scope;
7908 static GTY(()) int lambda_count;
7909 typedef struct GTY(()) tree_int
7914 DEF_VEC_O(tree_int);
7915 DEF_VEC_ALLOC_O(tree_int,gc);
7916 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7919 start_lambda_scope (tree decl)
7923 /* Once we're inside a function, we ignore other scopes and just push
7924 the function again so that popping works properly. */
7925 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7926 decl = current_function_decl;
7927 ti.t = lambda_scope;
7928 ti.i = lambda_count;
7929 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7930 if (lambda_scope != decl)
7932 /* Don't reset the count if we're still in the same function. */
7933 lambda_scope = decl;
7939 record_lambda_scope (tree lambda)
7941 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7942 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7946 finish_lambda_scope (void)
7948 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7949 if (lambda_scope != p->t)
7951 lambda_scope = p->t;
7952 lambda_count = p->i;
7954 VEC_pop (tree_int, lambda_scope_stack);
7957 /* Parse a lambda expression.
7960 lambda-introducer lambda-declarator [opt] compound-statement
7962 Returns a representation of the expression. */
7965 cp_parser_lambda_expression (cp_parser* parser)
7967 tree lambda_expr = build_lambda_expr ();
7971 LAMBDA_EXPR_LOCATION (lambda_expr)
7972 = cp_lexer_peek_token (parser->lexer)->location;
7974 if (cp_unevaluated_operand)
7975 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7976 "lambda-expression in unevaluated context");
7978 /* We may be in the middle of deferred access check. Disable
7980 push_deferring_access_checks (dk_no_deferred);
7982 cp_parser_lambda_introducer (parser, lambda_expr);
7984 type = begin_lambda_type (lambda_expr);
7986 record_lambda_scope (lambda_expr);
7988 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7989 determine_visibility (TYPE_NAME (type));
7991 /* Now that we've started the type, add the capture fields for any
7992 explicit captures. */
7993 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7996 /* Inside the class, surrounding template-parameter-lists do not apply. */
7997 unsigned int saved_num_template_parameter_lists
7998 = parser->num_template_parameter_lists;
7999 unsigned char in_statement = parser->in_statement;
8000 bool in_switch_statement_p = parser->in_switch_statement_p;
8002 parser->num_template_parameter_lists = 0;
8003 parser->in_statement = 0;
8004 parser->in_switch_statement_p = false;
8006 /* By virtue of defining a local class, a lambda expression has access to
8007 the private variables of enclosing classes. */
8009 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8012 cp_parser_lambda_body (parser, lambda_expr);
8013 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8014 cp_parser_skip_to_end_of_block_or_statement (parser);
8016 /* The capture list was built up in reverse order; fix that now. */
8018 tree newlist = NULL_TREE;
8021 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8024 next = TREE_CHAIN (elt);
8025 TREE_CHAIN (elt) = newlist;
8028 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8032 maybe_add_lambda_conv_op (type);
8034 type = finish_struct (type, /*attributes=*/NULL_TREE);
8036 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8037 parser->in_statement = in_statement;
8038 parser->in_switch_statement_p = in_switch_statement_p;
8041 pop_deferring_access_checks ();
8043 /* This field is only used during parsing of the lambda. */
8044 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8046 /* This lambda shouldn't have any proxies left at this point. */
8047 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8048 /* And now that we're done, push proxies for an enclosing lambda. */
8049 insert_pending_capture_proxies ();
8052 return build_lambda_object (lambda_expr);
8054 return error_mark_node;
8057 /* Parse the beginning of a lambda expression.
8060 [ lambda-capture [opt] ]
8062 LAMBDA_EXPR is the current representation of the lambda expression. */
8065 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8067 /* Need commas after the first capture. */
8070 /* Eat the leading `['. */
8071 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8073 /* Record default capture mode. "[&" "[=" "[&," "[=," */
8074 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8075 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8076 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8077 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8078 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8080 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8082 cp_lexer_consume_token (parser->lexer);
8086 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8088 cp_token* capture_token;
8090 tree capture_init_expr;
8091 cp_id_kind idk = CP_ID_KIND_NONE;
8092 bool explicit_init_p = false;
8094 enum capture_kind_type
8099 enum capture_kind_type capture_kind = BY_COPY;
8101 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8103 error ("expected end of capture-list");
8110 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8112 /* Possibly capture `this'. */
8113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8115 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8116 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8117 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8118 "with by-copy capture default");
8119 cp_lexer_consume_token (parser->lexer);
8120 add_capture (lambda_expr,
8121 /*id=*/this_identifier,
8122 /*initializer=*/finish_this_expr(),
8123 /*by_reference_p=*/false,
8128 /* Remember whether we want to capture as a reference or not. */
8129 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8131 capture_kind = BY_REFERENCE;
8132 cp_lexer_consume_token (parser->lexer);
8135 /* Get the identifier. */
8136 capture_token = cp_lexer_peek_token (parser->lexer);
8137 capture_id = cp_parser_identifier (parser);
8139 if (capture_id == error_mark_node)
8140 /* Would be nice to have a cp_parser_skip_to_closing_x for general
8141 delimiters, but I modified this to stop on unnested ']' as well. It
8142 was already changed to stop on unnested '}', so the
8143 "closing_parenthesis" name is no more misleading with my change. */
8145 cp_parser_skip_to_closing_parenthesis (parser,
8146 /*recovering=*/true,
8148 /*consume_paren=*/true);
8152 /* Find the initializer for this capture. */
8153 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8155 /* An explicit expression exists. */
8156 cp_lexer_consume_token (parser->lexer);
8157 pedwarn (input_location, OPT_pedantic,
8158 "ISO C++ does not allow initializers "
8159 "in lambda expression capture lists");
8160 capture_init_expr = cp_parser_assignment_expression (parser,
8163 explicit_init_p = true;
8167 const char* error_msg;
8169 /* Turn the identifier into an id-expression. */
8171 = cp_parser_lookup_name
8175 /*is_template=*/false,
8176 /*is_namespace=*/false,
8177 /*check_dependency=*/true,
8178 /*ambiguous_decls=*/NULL,
8179 capture_token->location);
8181 if (capture_init_expr == error_mark_node)
8183 unqualified_name_lookup_error (capture_id);
8186 else if (DECL_P (capture_init_expr)
8187 && (TREE_CODE (capture_init_expr) != VAR_DECL
8188 && TREE_CODE (capture_init_expr) != PARM_DECL))
8190 error_at (capture_token->location,
8191 "capture of non-variable %qD ",
8193 inform (0, "%q+#D declared here", capture_init_expr);
8196 if (TREE_CODE (capture_init_expr) == VAR_DECL
8197 && decl_storage_duration (capture_init_expr) != dk_auto)
8199 pedwarn (capture_token->location, 0, "capture of variable "
8200 "%qD with non-automatic storage duration",
8202 inform (0, "%q+#D declared here", capture_init_expr);
8207 = finish_id_expression
8212 /*integral_constant_expression_p=*/false,
8213 /*allow_non_integral_constant_expression_p=*/false,
8214 /*non_integral_constant_expression_p=*/NULL,
8215 /*template_p=*/false,
8217 /*address_p=*/false,
8218 /*template_arg_p=*/false,
8220 capture_token->location);
8223 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8224 && !explicit_init_p)
8226 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8227 && capture_kind == BY_COPY)
8228 pedwarn (capture_token->location, 0, "explicit by-copy capture "
8229 "of %qD redundant with by-copy capture default",
8231 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8232 && capture_kind == BY_REFERENCE)
8233 pedwarn (capture_token->location, 0, "explicit by-reference "
8234 "capture of %qD redundant with by-reference capture "
8235 "default", capture_id);
8238 add_capture (lambda_expr,
8241 /*by_reference_p=*/capture_kind == BY_REFERENCE,
8245 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8248 /* Parse the (optional) middle of a lambda expression.
8251 ( parameter-declaration-clause [opt] )
8252 attribute-specifier [opt]
8254 exception-specification [opt]
8255 lambda-return-type-clause [opt]
8257 LAMBDA_EXPR is the current representation of the lambda expression. */
8260 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8262 /* 5.1.1.4 of the standard says:
8263 If a lambda-expression does not include a lambda-declarator, it is as if
8264 the lambda-declarator were ().
8265 This means an empty parameter list, no attributes, and no exception
8267 tree param_list = void_list_node;
8268 tree attributes = NULL_TREE;
8269 tree exception_spec = NULL_TREE;
8272 /* The lambda-declarator is optional, but must begin with an opening
8273 parenthesis if present. */
8274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8276 cp_lexer_consume_token (parser->lexer);
8278 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8280 /* Parse parameters. */
8281 param_list = cp_parser_parameter_declaration_clause (parser);
8283 /* Default arguments shall not be specified in the
8284 parameter-declaration-clause of a lambda-declarator. */
8285 for (t = param_list; t; t = TREE_CHAIN (t))
8286 if (TREE_PURPOSE (t))
8287 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8288 "default argument specified for lambda parameter");
8290 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8292 attributes = cp_parser_attributes_opt (parser);
8294 /* Parse optional `mutable' keyword. */
8295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8297 cp_lexer_consume_token (parser->lexer);
8298 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8301 /* Parse optional exception specification. */
8302 exception_spec = cp_parser_exception_specification_opt (parser);
8304 /* Parse optional trailing return type. */
8305 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8307 cp_lexer_consume_token (parser->lexer);
8308 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8311 /* The function parameters must be in scope all the way until after the
8312 trailing-return-type in case of decltype. */
8313 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8314 pop_binding (DECL_NAME (t), t);
8319 /* Create the function call operator.
8321 Messing with declarators like this is no uglier than building up the
8322 FUNCTION_DECL by hand, and this is less likely to get out of sync with
8325 cp_decl_specifier_seq return_type_specs;
8326 cp_declarator* declarator;
8331 clear_decl_specs (&return_type_specs);
8332 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8333 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8335 /* Maybe we will deduce the return type later, but we can use void
8336 as a placeholder return type anyways. */
8337 return_type_specs.type = void_type_node;
8339 p = obstack_alloc (&declarator_obstack, 0);
8341 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8344 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8345 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8346 declarator = make_call_declarator (declarator, param_list, quals,
8347 VIRT_SPEC_UNSPECIFIED,
8349 /*late_return_type=*/NULL_TREE);
8350 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8352 fco = grokmethod (&return_type_specs,
8355 if (fco != error_mark_node)
8357 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8358 DECL_ARTIFICIAL (fco) = 1;
8359 /* Give the object parameter a different name. */
8360 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8363 finish_member_declaration (fco);
8365 obstack_free (&declarator_obstack, p);
8367 return (fco != error_mark_node);
8371 /* Parse the body of a lambda expression, which is simply
8375 but which requires special handling.
8376 LAMBDA_EXPR is the current representation of the lambda expression. */
8379 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8381 bool nested = (current_function_decl != NULL_TREE);
8382 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8384 push_function_context ();
8386 /* Still increment function_depth so that we don't GC in the
8387 middle of an expression. */
8389 /* Clear this in case we're in the middle of a default argument. */
8390 parser->local_variables_forbidden_p = false;
8392 /* Finish the function call operator
8394 + late_parsing_for_member
8395 + function_definition_after_declarator
8396 + ctor_initializer_opt_and_function_body */
8398 tree fco = lambda_function (lambda_expr);
8404 /* Let the front end know that we are going to be defining this
8406 start_preparsed_function (fco,
8408 SF_PRE_PARSED | SF_INCLASS_INLINE);
8410 start_lambda_scope (fco);
8411 body = begin_function_body ();
8413 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8416 /* Push the proxies for any explicit captures. */
8417 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8418 cap = TREE_CHAIN (cap))
8419 build_capture_proxy (TREE_PURPOSE (cap));
8421 compound_stmt = begin_compound_stmt (0);
8423 /* 5.1.1.4 of the standard says:
8424 If a lambda-expression does not include a trailing-return-type, it
8425 is as if the trailing-return-type denotes the following type:
8426 * if the compound-statement is of the form
8427 { return attribute-specifier [opt] expression ; }
8428 the type of the returned expression after lvalue-to-rvalue
8429 conversion (_conv.lval_ 4.1), array-to-pointer conversion
8430 (_conv.array_ 4.2), and function-to-pointer conversion
8432 * otherwise, void. */
8434 /* In a lambda that has neither a lambda-return-type-clause
8435 nor a deducible form, errors should be reported for return statements
8436 in the body. Since we used void as the placeholder return type, parsing
8437 the body as usual will give such desired behavior. */
8438 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8439 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8440 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8442 tree expr = NULL_TREE;
8443 cp_id_kind idk = CP_ID_KIND_NONE;
8445 /* Parse tentatively in case there's more after the initial return
8447 cp_parser_parse_tentatively (parser);
8449 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8451 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8453 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8454 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8456 if (cp_parser_parse_definitely (parser))
8458 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8460 /* Will get error here if type not deduced yet. */
8461 finish_return_stmt (expr);
8469 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8470 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8471 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8472 cp_parser_label_declaration (parser);
8473 cp_parser_statement_seq_opt (parser, NULL_TREE);
8474 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8475 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8478 finish_compound_stmt (compound_stmt);
8481 finish_function_body (body);
8482 finish_lambda_scope ();
8484 /* Finish the function and generate code for it if necessary. */
8485 expand_or_defer_fn (finish_function (/*inline*/2));
8488 parser->local_variables_forbidden_p = local_variables_forbidden_p;
8490 pop_function_context();
8495 /* Statements [gram.stmt.stmt] */
8497 /* Parse a statement.
8501 expression-statement
8506 declaration-statement
8509 IN_COMPOUND is true when the statement is nested inside a
8510 cp_parser_compound_statement; this matters for certain pragmas.
8512 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8513 is a (possibly labeled) if statement which is not enclosed in braces
8514 and has an else clause. This is used to implement -Wparentheses. */
8517 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8518 bool in_compound, bool *if_p)
8522 location_t statement_location;
8527 /* There is no statement yet. */
8528 statement = NULL_TREE;
8529 /* Peek at the next token. */
8530 token = cp_lexer_peek_token (parser->lexer);
8531 /* Remember the location of the first token in the statement. */
8532 statement_location = token->location;
8533 /* If this is a keyword, then that will often determine what kind of
8534 statement we have. */
8535 if (token->type == CPP_KEYWORD)
8537 enum rid keyword = token->keyword;
8543 /* Looks like a labeled-statement with a case label.
8544 Parse the label, and then use tail recursion to parse
8546 cp_parser_label_for_labeled_statement (parser);
8551 statement = cp_parser_selection_statement (parser, if_p);
8557 statement = cp_parser_iteration_statement (parser);
8564 statement = cp_parser_jump_statement (parser);
8567 /* Objective-C++ exception-handling constructs. */
8570 case RID_AT_FINALLY:
8571 case RID_AT_SYNCHRONIZED:
8573 statement = cp_parser_objc_statement (parser);
8577 statement = cp_parser_try_block (parser);
8581 /* This must be a namespace alias definition. */
8582 cp_parser_declaration_statement (parser);
8586 /* It might be a keyword like `int' that can start a
8587 declaration-statement. */
8591 else if (token->type == CPP_NAME)
8593 /* If the next token is a `:', then we are looking at a
8594 labeled-statement. */
8595 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8596 if (token->type == CPP_COLON)
8598 /* Looks like a labeled-statement with an ordinary label.
8599 Parse the label, and then use tail recursion to parse
8601 cp_parser_label_for_labeled_statement (parser);
8605 /* Anything that starts with a `{' must be a compound-statement. */
8606 else if (token->type == CPP_OPEN_BRACE)
8607 statement = cp_parser_compound_statement (parser, NULL, false, false);
8608 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8609 a statement all its own. */
8610 else if (token->type == CPP_PRAGMA)
8612 /* Only certain OpenMP pragmas are attached to statements, and thus
8613 are considered statements themselves. All others are not. In
8614 the context of a compound, accept the pragma as a "statement" and
8615 return so that we can check for a close brace. Otherwise we
8616 require a real statement and must go back and read one. */
8618 cp_parser_pragma (parser, pragma_compound);
8619 else if (!cp_parser_pragma (parser, pragma_stmt))
8623 else if (token->type == CPP_EOF)
8625 cp_parser_error (parser, "expected statement");
8629 /* Everything else must be a declaration-statement or an
8630 expression-statement. Try for the declaration-statement
8631 first, unless we are looking at a `;', in which case we know that
8632 we have an expression-statement. */
8635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8637 cp_parser_parse_tentatively (parser);
8638 /* Try to parse the declaration-statement. */
8639 cp_parser_declaration_statement (parser);
8640 /* If that worked, we're done. */
8641 if (cp_parser_parse_definitely (parser))
8644 /* Look for an expression-statement instead. */
8645 statement = cp_parser_expression_statement (parser, in_statement_expr);
8648 /* Set the line number for the statement. */
8649 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8650 SET_EXPR_LOCATION (statement, statement_location);
8653 /* Parse the label for a labeled-statement, i.e.
8656 case constant-expression :
8660 case constant-expression ... constant-expression : statement
8662 When a label is parsed without errors, the label is added to the
8663 parse tree by the finish_* functions, so this function doesn't
8664 have to return the label. */
8667 cp_parser_label_for_labeled_statement (cp_parser* parser)
8670 tree label = NULL_TREE;
8671 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8673 /* The next token should be an identifier. */
8674 token = cp_lexer_peek_token (parser->lexer);
8675 if (token->type != CPP_NAME
8676 && token->type != CPP_KEYWORD)
8678 cp_parser_error (parser, "expected labeled-statement");
8682 parser->colon_corrects_to_scope_p = false;
8683 switch (token->keyword)
8690 /* Consume the `case' token. */
8691 cp_lexer_consume_token (parser->lexer);
8692 /* Parse the constant-expression. */
8693 expr = cp_parser_constant_expression (parser,
8694 /*allow_non_constant_p=*/false,
8697 ellipsis = cp_lexer_peek_token (parser->lexer);
8698 if (ellipsis->type == CPP_ELLIPSIS)
8700 /* Consume the `...' token. */
8701 cp_lexer_consume_token (parser->lexer);
8703 cp_parser_constant_expression (parser,
8704 /*allow_non_constant_p=*/false,
8706 /* We don't need to emit warnings here, as the common code
8707 will do this for us. */
8710 expr_hi = NULL_TREE;
8712 if (parser->in_switch_statement_p)
8713 finish_case_label (token->location, expr, expr_hi);
8715 error_at (token->location,
8716 "case label %qE not within a switch statement",
8722 /* Consume the `default' token. */
8723 cp_lexer_consume_token (parser->lexer);
8725 if (parser->in_switch_statement_p)
8726 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8728 error_at (token->location, "case label not within a switch statement");
8732 /* Anything else must be an ordinary label. */
8733 label = finish_label_stmt (cp_parser_identifier (parser));
8737 /* Require the `:' token. */
8738 cp_parser_require (parser, CPP_COLON, RT_COLON);
8740 /* An ordinary label may optionally be followed by attributes.
8741 However, this is only permitted if the attributes are then
8742 followed by a semicolon. This is because, for backward
8743 compatibility, when parsing
8744 lab: __attribute__ ((unused)) int i;
8745 we want the attribute to attach to "i", not "lab". */
8746 if (label != NULL_TREE
8747 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8751 cp_parser_parse_tentatively (parser);
8752 attrs = cp_parser_attributes_opt (parser);
8753 if (attrs == NULL_TREE
8754 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8755 cp_parser_abort_tentative_parse (parser);
8756 else if (!cp_parser_parse_definitely (parser))
8759 cplus_decl_attributes (&label, attrs, 0);
8762 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8765 /* Parse an expression-statement.
8767 expression-statement:
8770 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8771 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8772 indicates whether this expression-statement is part of an
8773 expression statement. */
8776 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8778 tree statement = NULL_TREE;
8779 cp_token *token = cp_lexer_peek_token (parser->lexer);
8781 /* If the next token is a ';', then there is no expression
8783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8784 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8786 /* Give a helpful message for "A<T>::type t;" and the like. */
8787 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8788 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8790 if (TREE_CODE (statement) == SCOPE_REF)
8791 error_at (token->location, "need %<typename%> before %qE because "
8792 "%qT is a dependent scope",
8793 statement, TREE_OPERAND (statement, 0));
8794 else if (is_overloaded_fn (statement)
8795 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8798 tree fn = get_first_fn (statement);
8799 error_at (token->location,
8800 "%<%T::%D%> names the constructor, not the type",
8801 DECL_CONTEXT (fn), DECL_NAME (fn));
8805 /* Consume the final `;'. */
8806 cp_parser_consume_semicolon_at_end_of_statement (parser);
8808 if (in_statement_expr
8809 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8810 /* This is the final expression statement of a statement
8812 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8814 statement = finish_expr_stmt (statement);
8821 /* Parse a compound-statement.
8824 { statement-seq [opt] }
8829 { label-declaration-seq [opt] statement-seq [opt] }
8831 label-declaration-seq:
8833 label-declaration-seq label-declaration
8835 Returns a tree representing the statement. */
8838 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8839 bool in_try, bool function_body)
8843 /* Consume the `{'. */
8844 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8845 return error_mark_node;
8846 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8848 pedwarn (input_location, OPT_pedantic,
8849 "compound-statement in constexpr function");
8850 /* Begin the compound-statement. */
8851 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8852 /* If the next keyword is `__label__' we have a label declaration. */
8853 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8854 cp_parser_label_declaration (parser);
8855 /* Parse an (optional) statement-seq. */
8856 cp_parser_statement_seq_opt (parser, in_statement_expr);
8857 /* Finish the compound-statement. */
8858 finish_compound_stmt (compound_stmt);
8859 /* Consume the `}'. */
8860 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8862 return compound_stmt;
8865 /* Parse an (optional) statement-seq.
8869 statement-seq [opt] statement */
8872 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8874 /* Scan statements until there aren't any more. */
8877 cp_token *token = cp_lexer_peek_token (parser->lexer);
8879 /* If we are looking at a `}', then we have run out of
8880 statements; the same is true if we have reached the end
8881 of file, or have stumbled upon a stray '@end'. */
8882 if (token->type == CPP_CLOSE_BRACE
8883 || token->type == CPP_EOF
8884 || token->type == CPP_PRAGMA_EOL
8885 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8888 /* If we are in a compound statement and find 'else' then
8889 something went wrong. */
8890 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8892 if (parser->in_statement & IN_IF_STMT)
8896 token = cp_lexer_consume_token (parser->lexer);
8897 error_at (token->location, "%<else%> without a previous %<if%>");
8901 /* Parse the statement. */
8902 cp_parser_statement (parser, in_statement_expr, true, NULL);
8906 /* Parse a selection-statement.
8908 selection-statement:
8909 if ( condition ) statement
8910 if ( condition ) statement else statement
8911 switch ( condition ) statement
8913 Returns the new IF_STMT or SWITCH_STMT.
8915 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8916 is a (possibly labeled) if statement which is not enclosed in
8917 braces and has an else clause. This is used to implement
8921 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8929 /* Peek at the next token. */
8930 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8932 /* See what kind of keyword it is. */
8933 keyword = token->keyword;
8942 /* Look for the `('. */
8943 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8945 cp_parser_skip_to_end_of_statement (parser);
8946 return error_mark_node;
8949 /* Begin the selection-statement. */
8950 if (keyword == RID_IF)
8951 statement = begin_if_stmt ();
8953 statement = begin_switch_stmt ();
8955 /* Parse the condition. */
8956 condition = cp_parser_condition (parser);
8957 /* Look for the `)'. */
8958 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8959 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8960 /*consume_paren=*/true);
8962 if (keyword == RID_IF)
8965 unsigned char in_statement;
8967 /* Add the condition. */
8968 finish_if_stmt_cond (condition, statement);
8970 /* Parse the then-clause. */
8971 in_statement = parser->in_statement;
8972 parser->in_statement |= IN_IF_STMT;
8973 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8975 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8976 add_stmt (build_empty_stmt (loc));
8977 cp_lexer_consume_token (parser->lexer);
8978 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8979 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8980 "empty body in an %<if%> statement");
8984 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8985 parser->in_statement = in_statement;
8987 finish_then_clause (statement);
8989 /* If the next token is `else', parse the else-clause. */
8990 if (cp_lexer_next_token_is_keyword (parser->lexer,
8993 /* Consume the `else' keyword. */
8994 cp_lexer_consume_token (parser->lexer);
8995 begin_else_clause (statement);
8996 /* Parse the else-clause. */
8997 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9000 loc = cp_lexer_peek_token (parser->lexer)->location;
9002 OPT_Wempty_body, "suggest braces around "
9003 "empty body in an %<else%> statement");
9004 add_stmt (build_empty_stmt (loc));
9005 cp_lexer_consume_token (parser->lexer);
9008 cp_parser_implicitly_scoped_statement (parser, NULL);
9010 finish_else_clause (statement);
9012 /* If we are currently parsing a then-clause, then
9013 IF_P will not be NULL. We set it to true to
9014 indicate that this if statement has an else clause.
9015 This may trigger the Wparentheses warning below
9016 when we get back up to the parent if statement. */
9022 /* This if statement does not have an else clause. If
9023 NESTED_IF is true, then the then-clause is an if
9024 statement which does have an else clause. We warn
9025 about the potential ambiguity. */
9027 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9028 "suggest explicit braces to avoid ambiguous"
9032 /* Now we're all done with the if-statement. */
9033 finish_if_stmt (statement);
9037 bool in_switch_statement_p;
9038 unsigned char in_statement;
9040 /* Add the condition. */
9041 finish_switch_cond (condition, statement);
9043 /* Parse the body of the switch-statement. */
9044 in_switch_statement_p = parser->in_switch_statement_p;
9045 in_statement = parser->in_statement;
9046 parser->in_switch_statement_p = true;
9047 parser->in_statement |= IN_SWITCH_STMT;
9048 cp_parser_implicitly_scoped_statement (parser, NULL);
9049 parser->in_switch_statement_p = in_switch_statement_p;
9050 parser->in_statement = in_statement;
9052 /* Now we're all done with the switch-statement. */
9053 finish_switch_stmt (statement);
9061 cp_parser_error (parser, "expected selection-statement");
9062 return error_mark_node;
9066 /* Parse a condition.
9070 type-specifier-seq declarator = initializer-clause
9071 type-specifier-seq declarator braced-init-list
9076 type-specifier-seq declarator asm-specification [opt]
9077 attributes [opt] = assignment-expression
9079 Returns the expression that should be tested. */
9082 cp_parser_condition (cp_parser* parser)
9084 cp_decl_specifier_seq type_specifiers;
9085 const char *saved_message;
9086 int declares_class_or_enum;
9088 /* Try the declaration first. */
9089 cp_parser_parse_tentatively (parser);
9090 /* New types are not allowed in the type-specifier-seq for a
9092 saved_message = parser->type_definition_forbidden_message;
9093 parser->type_definition_forbidden_message
9094 = G_("types may not be defined in conditions");
9095 /* Parse the type-specifier-seq. */
9096 cp_parser_decl_specifier_seq (parser,
9097 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9099 &declares_class_or_enum);
9100 /* Restore the saved message. */
9101 parser->type_definition_forbidden_message = saved_message;
9102 /* If all is well, we might be looking at a declaration. */
9103 if (!cp_parser_error_occurred (parser))
9106 tree asm_specification;
9108 cp_declarator *declarator;
9109 tree initializer = NULL_TREE;
9111 /* Parse the declarator. */
9112 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9113 /*ctor_dtor_or_conv_p=*/NULL,
9114 /*parenthesized_p=*/NULL,
9115 /*member_p=*/false);
9116 /* Parse the attributes. */
9117 attributes = cp_parser_attributes_opt (parser);
9118 /* Parse the asm-specification. */
9119 asm_specification = cp_parser_asm_specification_opt (parser);
9120 /* If the next token is not an `=' or '{', then we might still be
9121 looking at an expression. For example:
9125 looks like a decl-specifier-seq and a declarator -- but then
9126 there is no `=', so this is an expression. */
9127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9128 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9129 cp_parser_simulate_error (parser);
9131 /* If we did see an `=' or '{', then we are looking at a declaration
9133 if (cp_parser_parse_definitely (parser))
9136 bool non_constant_p;
9137 bool flags = LOOKUP_ONLYCONVERTING;
9139 /* Create the declaration. */
9140 decl = start_decl (declarator, &type_specifiers,
9141 /*initialized_p=*/true,
9142 attributes, /*prefix_attributes=*/NULL_TREE,
9145 /* Parse the initializer. */
9146 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9148 initializer = cp_parser_braced_list (parser, &non_constant_p);
9149 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9154 /* Consume the `='. */
9155 cp_parser_require (parser, CPP_EQ, RT_EQ);
9156 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9158 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9159 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9161 /* Process the initializer. */
9162 cp_finish_decl (decl,
9163 initializer, !non_constant_p,
9168 pop_scope (pushed_scope);
9170 return convert_from_reference (decl);
9173 /* If we didn't even get past the declarator successfully, we are
9174 definitely not looking at a declaration. */
9176 cp_parser_abort_tentative_parse (parser);
9178 /* Otherwise, we are looking at an expression. */
9179 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9182 /* Parses a for-statement or range-for-statement until the closing ')',
9186 cp_parser_for (cp_parser *parser)
9188 tree init, scope, decl;
9191 /* Begin the for-statement. */
9192 scope = begin_for_scope (&init);
9194 /* Parse the initialization. */
9195 is_range_for = cp_parser_for_init_statement (parser, &decl);
9198 return cp_parser_range_for (parser, scope, init, decl);
9200 return cp_parser_c_for (parser, scope, init);
9204 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9206 /* Normal for loop */
9207 tree condition = NULL_TREE;
9208 tree expression = NULL_TREE;
9211 stmt = begin_for_stmt (scope, init);
9212 /* The for-init-statement has already been parsed in
9213 cp_parser_for_init_statement, so no work is needed here. */
9214 finish_for_init_stmt (stmt);
9216 /* If there's a condition, process it. */
9217 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9218 condition = cp_parser_condition (parser);
9219 finish_for_cond (condition, stmt);
9220 /* Look for the `;'. */
9221 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9223 /* If there's an expression, process it. */
9224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9225 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9226 finish_for_expr (expression, stmt);
9231 /* Tries to parse a range-based for-statement:
9234 decl-specifier-seq declarator : expression
9236 The decl-specifier-seq declarator and the `:' are already parsed by
9237 cp_parser_for_init_statement. If processing_template_decl it returns a
9238 newly created RANGE_FOR_STMT; if not, it is converted to a
9239 regular FOR_STMT. */
9242 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9244 tree stmt, range_expr;
9246 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9248 bool expr_non_constant_p;
9249 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9252 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9254 /* If in template, STMT is converted to a normal for-statement
9255 at instantiation. If not, it is done just ahead. */
9256 if (processing_template_decl)
9258 stmt = begin_range_for_stmt (scope, init);
9259 finish_range_for_decl (stmt, range_decl, range_expr);
9260 if (!type_dependent_expression_p (range_expr)
9261 /* do_auto_deduction doesn't mess with template init-lists. */
9262 && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9263 do_range_for_auto_deduction (range_decl, range_expr);
9267 stmt = begin_for_stmt (scope, init);
9268 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9273 /* Subroutine of cp_convert_range_for: given the initializer expression,
9274 builds up the range temporary. */
9277 build_range_temp (tree range_expr)
9279 tree range_type, range_temp;
9281 /* Find out the type deduced by the declaration
9282 `auto &&__range = range_expr'. */
9283 range_type = cp_build_reference_type (make_auto (), true);
9284 range_type = do_auto_deduction (range_type, range_expr,
9285 type_uses_auto (range_type));
9287 /* Create the __range variable. */
9288 range_temp = build_decl (input_location, VAR_DECL,
9289 get_identifier ("__for_range"), range_type);
9290 TREE_USED (range_temp) = 1;
9291 DECL_ARTIFICIAL (range_temp) = 1;
9296 /* Used by cp_parser_range_for in template context: we aren't going to
9297 do a full conversion yet, but we still need to resolve auto in the
9298 type of the for-range-declaration if present. This is basically
9299 a shortcut version of cp_convert_range_for. */
9302 do_range_for_auto_deduction (tree decl, tree range_expr)
9304 tree auto_node = type_uses_auto (TREE_TYPE (decl));
9307 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9308 range_temp = convert_from_reference (build_range_temp (range_expr));
9309 iter_type = (cp_parser_perform_range_for_lookup
9310 (range_temp, &begin_dummy, &end_dummy));
9311 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9312 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9313 tf_warning_or_error);
9314 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9315 iter_decl, auto_node);
9319 /* Converts a range-based for-statement into a normal
9320 for-statement, as per the definition.
9322 for (RANGE_DECL : RANGE_EXPR)
9325 should be equivalent to:
9328 auto &&__range = RANGE_EXPR;
9329 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9333 RANGE_DECL = *__begin;
9338 If RANGE_EXPR is an array:
9339 BEGIN_EXPR = __range
9340 END_EXPR = __range + ARRAY_SIZE(__range)
9341 Else if RANGE_EXPR has a member 'begin' or 'end':
9342 BEGIN_EXPR = __range.begin()
9343 END_EXPR = __range.end()
9345 BEGIN_EXPR = begin(__range)
9346 END_EXPR = end(__range);
9348 If __range has a member 'begin' but not 'end', or vice versa, we must
9349 still use the second alternative (it will surely fail, however).
9350 When calling begin()/end() in the third alternative we must use
9351 argument dependent lookup, but always considering 'std' as an associated
9355 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9358 tree iter_type, begin_expr, end_expr;
9359 tree condition, expression;
9361 if (range_decl == error_mark_node || range_expr == error_mark_node)
9362 /* If an error happened previously do nothing or else a lot of
9363 unhelpful errors would be issued. */
9364 begin_expr = end_expr = iter_type = error_mark_node;
9367 tree range_temp = build_range_temp (range_expr);
9368 pushdecl (range_temp);
9369 cp_finish_decl (range_temp, range_expr,
9370 /*is_constant_init*/false, NULL_TREE,
9371 LOOKUP_ONLYCONVERTING);
9373 range_temp = convert_from_reference (range_temp);
9374 iter_type = cp_parser_perform_range_for_lookup (range_temp,
9375 &begin_expr, &end_expr);
9378 /* The new for initialization statement. */
9379 begin = build_decl (input_location, VAR_DECL,
9380 get_identifier ("__for_begin"), iter_type);
9381 TREE_USED (begin) = 1;
9382 DECL_ARTIFICIAL (begin) = 1;
9384 cp_finish_decl (begin, begin_expr,
9385 /*is_constant_init*/false, NULL_TREE,
9386 LOOKUP_ONLYCONVERTING);
9388 end = build_decl (input_location, VAR_DECL,
9389 get_identifier ("__for_end"), iter_type);
9390 TREE_USED (end) = 1;
9391 DECL_ARTIFICIAL (end) = 1;
9393 cp_finish_decl (end, end_expr,
9394 /*is_constant_init*/false, NULL_TREE,
9395 LOOKUP_ONLYCONVERTING);
9397 finish_for_init_stmt (statement);
9399 /* The new for condition. */
9400 condition = build_x_binary_op (NE_EXPR,
9403 NULL, tf_warning_or_error);
9404 finish_for_cond (condition, statement);
9406 /* The new increment expression. */
9407 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9408 finish_for_expr (expression, statement);
9410 /* The declaration is initialized with *__begin inside the loop body. */
9411 cp_finish_decl (range_decl,
9412 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9413 /*is_constant_init*/false, NULL_TREE,
9414 LOOKUP_ONLYCONVERTING);
9419 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9420 We need to solve both at the same time because the method used
9421 depends on the existence of members begin or end.
9422 Returns the type deduced for the iterator expression. */
9425 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9427 if (error_operand_p (range))
9429 *begin = *end = error_mark_node;
9430 return error_mark_node;
9433 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9435 error ("range-based %<for%> expression of type %qT "
9436 "has incomplete type", TREE_TYPE (range));
9437 *begin = *end = error_mark_node;
9438 return error_mark_node;
9440 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9442 /* If RANGE is an array, we will use pointer arithmetic. */
9444 *end = build_binary_op (input_location, PLUS_EXPR,
9446 array_type_nelts_top (TREE_TYPE (range)),
9448 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9452 /* If it is not an array, we must do a bit of magic. */
9453 tree id_begin, id_end;
9454 tree member_begin, member_end;
9456 *begin = *end = error_mark_node;
9458 id_begin = get_identifier ("begin");
9459 id_end = get_identifier ("end");
9460 member_begin = lookup_member (TREE_TYPE (range), id_begin,
9461 /*protect=*/2, /*want_type=*/false);
9462 member_end = lookup_member (TREE_TYPE (range), id_end,
9463 /*protect=*/2, /*want_type=*/false);
9465 if (member_begin != NULL_TREE || member_end != NULL_TREE)
9467 /* Use the member functions. */
9468 if (member_begin != NULL_TREE)
9469 *begin = cp_parser_range_for_member_function (range, id_begin);
9471 error ("range-based %<for%> expression of type %qT has an "
9472 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9474 if (member_end != NULL_TREE)
9475 *end = cp_parser_range_for_member_function (range, id_end);
9477 error ("range-based %<for%> expression of type %qT has a "
9478 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9482 /* Use global functions with ADL. */
9484 vec = make_tree_vector ();
9486 VEC_safe_push (tree, gc, vec, range);
9488 member_begin = perform_koenig_lookup (id_begin, vec,
9489 /*include_std=*/true,
9490 tf_warning_or_error);
9491 *begin = finish_call_expr (member_begin, &vec, false, true,
9492 tf_warning_or_error);
9493 member_end = perform_koenig_lookup (id_end, vec,
9494 /*include_std=*/true,
9495 tf_warning_or_error);
9496 *end = finish_call_expr (member_end, &vec, false, true,
9497 tf_warning_or_error);
9499 release_tree_vector (vec);
9502 /* Last common checks. */
9503 if (*begin == error_mark_node || *end == error_mark_node)
9505 /* If one of the expressions is an error do no more checks. */
9506 *begin = *end = error_mark_node;
9507 return error_mark_node;
9511 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9512 /* The unqualified type of the __begin and __end temporaries should
9513 be the same, as required by the multiple auto declaration. */
9514 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9515 error ("inconsistent begin/end types in range-based %<for%> "
9516 "statement: %qT and %qT",
9517 TREE_TYPE (*begin), TREE_TYPE (*end));
9523 /* Helper function for cp_parser_perform_range_for_lookup.
9524 Builds a tree for RANGE.IDENTIFIER(). */
9527 cp_parser_range_for_member_function (tree range, tree identifier)
9532 member = finish_class_member_access_expr (range, identifier,
9533 false, tf_warning_or_error);
9534 if (member == error_mark_node)
9535 return error_mark_node;
9537 vec = make_tree_vector ();
9538 res = finish_call_expr (member, &vec,
9539 /*disallow_virtual=*/false,
9541 tf_warning_or_error);
9542 release_tree_vector (vec);
9546 /* Parse an iteration-statement.
9548 iteration-statement:
9549 while ( condition ) statement
9550 do statement while ( expression ) ;
9551 for ( for-init-statement condition [opt] ; expression [opt] )
9554 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
9557 cp_parser_iteration_statement (cp_parser* parser)
9562 unsigned char in_statement;
9564 /* Peek at the next token. */
9565 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9567 return error_mark_node;
9569 /* Remember whether or not we are already within an iteration
9571 in_statement = parser->in_statement;
9573 /* See what kind of keyword it is. */
9574 keyword = token->keyword;
9581 /* Begin the while-statement. */
9582 statement = begin_while_stmt ();
9583 /* Look for the `('. */
9584 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9585 /* Parse the condition. */
9586 condition = cp_parser_condition (parser);
9587 finish_while_stmt_cond (condition, statement);
9588 /* Look for the `)'. */
9589 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9590 /* Parse the dependent statement. */
9591 parser->in_statement = IN_ITERATION_STMT;
9592 cp_parser_already_scoped_statement (parser);
9593 parser->in_statement = in_statement;
9594 /* We're done with the while-statement. */
9595 finish_while_stmt (statement);
9603 /* Begin the do-statement. */
9604 statement = begin_do_stmt ();
9605 /* Parse the body of the do-statement. */
9606 parser->in_statement = IN_ITERATION_STMT;
9607 cp_parser_implicitly_scoped_statement (parser, NULL);
9608 parser->in_statement = in_statement;
9609 finish_do_body (statement);
9610 /* Look for the `while' keyword. */
9611 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9612 /* Look for the `('. */
9613 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9614 /* Parse the expression. */
9615 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9616 /* We're done with the do-statement. */
9617 finish_do_stmt (expression, statement);
9618 /* Look for the `)'. */
9619 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9620 /* Look for the `;'. */
9621 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9627 /* Look for the `('. */
9628 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9630 statement = cp_parser_for (parser);
9632 /* Look for the `)'. */
9633 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9635 /* Parse the body of the for-statement. */
9636 parser->in_statement = IN_ITERATION_STMT;
9637 cp_parser_already_scoped_statement (parser);
9638 parser->in_statement = in_statement;
9640 /* We're done with the for-statement. */
9641 finish_for_stmt (statement);
9646 cp_parser_error (parser, "expected iteration-statement");
9647 statement = error_mark_node;
9654 /* Parse a for-init-statement or the declarator of a range-based-for.
9655 Returns true if a range-based-for declaration is seen.
9658 expression-statement
9659 simple-declaration */
9662 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9664 /* If the next token is a `;', then we have an empty
9665 expression-statement. Grammatically, this is also a
9666 simple-declaration, but an invalid one, because it does not
9667 declare anything. Therefore, if we did not handle this case
9668 specially, we would issue an error message about an invalid
9670 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9672 bool is_range_for = false;
9673 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9675 parser->colon_corrects_to_scope_p = false;
9677 /* We're going to speculatively look for a declaration, falling back
9678 to an expression, if necessary. */
9679 cp_parser_parse_tentatively (parser);
9680 /* Parse the declaration. */
9681 cp_parser_simple_declaration (parser,
9682 /*function_definition_allowed_p=*/false,
9684 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9685 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9687 /* It is a range-for, consume the ':' */
9688 cp_lexer_consume_token (parser->lexer);
9689 is_range_for = true;
9690 if (cxx_dialect < cxx0x)
9692 error_at (cp_lexer_peek_token (parser->lexer)->location,
9693 "range-based %<for%> loops are not allowed "
9695 *decl = error_mark_node;
9699 /* The ';' is not consumed yet because we told
9700 cp_parser_simple_declaration not to. */
9701 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9703 if (cp_parser_parse_definitely (parser))
9704 return is_range_for;
9705 /* If the tentative parse failed, then we shall need to look for an
9706 expression-statement. */
9708 /* If we are here, it is an expression-statement. */
9709 cp_parser_expression_statement (parser, NULL_TREE);
9713 /* Parse a jump-statement.
9718 return expression [opt] ;
9719 return braced-init-list ;
9727 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9730 cp_parser_jump_statement (cp_parser* parser)
9732 tree statement = error_mark_node;
9735 unsigned char in_statement;
9737 /* Peek at the next token. */
9738 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9740 return error_mark_node;
9742 /* See what kind of keyword it is. */
9743 keyword = token->keyword;
9747 in_statement = parser->in_statement & ~IN_IF_STMT;
9748 switch (in_statement)
9751 error_at (token->location, "break statement not within loop or switch");
9754 gcc_assert ((in_statement & IN_SWITCH_STMT)
9755 || in_statement == IN_ITERATION_STMT);
9756 statement = finish_break_stmt ();
9759 error_at (token->location, "invalid exit from OpenMP structured block");
9762 error_at (token->location, "break statement used with OpenMP for loop");
9765 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9769 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9772 error_at (token->location, "continue statement not within a loop");
9774 case IN_ITERATION_STMT:
9776 statement = finish_continue_stmt ();
9779 error_at (token->location, "invalid exit from OpenMP structured block");
9784 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9790 bool expr_non_constant_p;
9792 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9794 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9795 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9797 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9798 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9800 /* If the next token is a `;', then there is no
9803 /* Build the return-statement. */
9804 statement = finish_return_stmt (expr);
9805 /* Look for the final `;'. */
9806 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9811 /* Create the goto-statement. */
9812 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9814 /* Issue a warning about this use of a GNU extension. */
9815 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9816 /* Consume the '*' token. */
9817 cp_lexer_consume_token (parser->lexer);
9818 /* Parse the dependent expression. */
9819 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9822 finish_goto_stmt (cp_parser_identifier (parser));
9823 /* Look for the final `;'. */
9824 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9828 cp_parser_error (parser, "expected jump-statement");
9835 /* Parse a declaration-statement.
9837 declaration-statement:
9838 block-declaration */
9841 cp_parser_declaration_statement (cp_parser* parser)
9845 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9846 p = obstack_alloc (&declarator_obstack, 0);
9848 /* Parse the block-declaration. */
9849 cp_parser_block_declaration (parser, /*statement_p=*/true);
9851 /* Free any declarators allocated. */
9852 obstack_free (&declarator_obstack, p);
9854 /* Finish off the statement. */
9858 /* Some dependent statements (like `if (cond) statement'), are
9859 implicitly in their own scope. In other words, if the statement is
9860 a single statement (as opposed to a compound-statement), it is
9861 none-the-less treated as if it were enclosed in braces. Any
9862 declarations appearing in the dependent statement are out of scope
9863 after control passes that point. This function parses a statement,
9864 but ensures that is in its own scope, even if it is not a
9867 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9868 is a (possibly labeled) if statement which is not enclosed in
9869 braces and has an else clause. This is used to implement
9872 Returns the new statement. */
9875 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9882 /* Mark if () ; with a special NOP_EXPR. */
9883 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9885 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9886 cp_lexer_consume_token (parser->lexer);
9887 statement = add_stmt (build_empty_stmt (loc));
9889 /* if a compound is opened, we simply parse the statement directly. */
9890 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9891 statement = cp_parser_compound_statement (parser, NULL, false, false);
9892 /* If the token is not a `{', then we must take special action. */
9895 /* Create a compound-statement. */
9896 statement = begin_compound_stmt (0);
9897 /* Parse the dependent-statement. */
9898 cp_parser_statement (parser, NULL_TREE, false, if_p);
9899 /* Finish the dummy compound-statement. */
9900 finish_compound_stmt (statement);
9903 /* Return the statement. */
9907 /* For some dependent statements (like `while (cond) statement'), we
9908 have already created a scope. Therefore, even if the dependent
9909 statement is a compound-statement, we do not want to create another
9913 cp_parser_already_scoped_statement (cp_parser* parser)
9915 /* If the token is a `{', then we must take special action. */
9916 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9917 cp_parser_statement (parser, NULL_TREE, false, NULL);
9920 /* Avoid calling cp_parser_compound_statement, so that we
9921 don't create a new scope. Do everything else by hand. */
9922 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9923 /* If the next keyword is `__label__' we have a label declaration. */
9924 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9925 cp_parser_label_declaration (parser);
9926 /* Parse an (optional) statement-seq. */
9927 cp_parser_statement_seq_opt (parser, NULL_TREE);
9928 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9932 /* Declarations [gram.dcl.dcl] */
9934 /* Parse an optional declaration-sequence.
9938 declaration-seq declaration */
9941 cp_parser_declaration_seq_opt (cp_parser* parser)
9947 token = cp_lexer_peek_token (parser->lexer);
9949 if (token->type == CPP_CLOSE_BRACE
9950 || token->type == CPP_EOF
9951 || token->type == CPP_PRAGMA_EOL)
9954 if (token->type == CPP_SEMICOLON)
9956 /* A declaration consisting of a single semicolon is
9957 invalid. Allow it unless we're being pedantic. */
9958 cp_lexer_consume_token (parser->lexer);
9959 if (!in_system_header)
9960 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9964 /* If we're entering or exiting a region that's implicitly
9965 extern "C", modify the lang context appropriately. */
9966 if (!parser->implicit_extern_c && token->implicit_extern_c)
9968 push_lang_context (lang_name_c);
9969 parser->implicit_extern_c = true;
9971 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9973 pop_lang_context ();
9974 parser->implicit_extern_c = false;
9977 if (token->type == CPP_PRAGMA)
9979 /* A top-level declaration can consist solely of a #pragma.
9980 A nested declaration cannot, so this is done here and not
9981 in cp_parser_declaration. (A #pragma at block scope is
9982 handled in cp_parser_statement.) */
9983 cp_parser_pragma (parser, pragma_external);
9987 /* Parse the declaration itself. */
9988 cp_parser_declaration (parser);
9992 /* Parse a declaration.
9997 template-declaration
9998 explicit-instantiation
9999 explicit-specialization
10000 linkage-specification
10001 namespace-definition
10006 __extension__ declaration */
10009 cp_parser_declaration (cp_parser* parser)
10013 int saved_pedantic;
10015 tree attributes = NULL_TREE;
10017 /* Check for the `__extension__' keyword. */
10018 if (cp_parser_extension_opt (parser, &saved_pedantic))
10020 /* Parse the qualified declaration. */
10021 cp_parser_declaration (parser);
10022 /* Restore the PEDANTIC flag. */
10023 pedantic = saved_pedantic;
10028 /* Try to figure out what kind of declaration is present. */
10029 token1 = *cp_lexer_peek_token (parser->lexer);
10031 if (token1.type != CPP_EOF)
10032 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10035 token2.type = CPP_EOF;
10036 token2.keyword = RID_MAX;
10039 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
10040 p = obstack_alloc (&declarator_obstack, 0);
10042 /* If the next token is `extern' and the following token is a string
10043 literal, then we have a linkage specification. */
10044 if (token1.keyword == RID_EXTERN
10045 && cp_parser_is_pure_string_literal (&token2))
10046 cp_parser_linkage_specification (parser);
10047 /* If the next token is `template', then we have either a template
10048 declaration, an explicit instantiation, or an explicit
10050 else if (token1.keyword == RID_TEMPLATE)
10052 /* `template <>' indicates a template specialization. */
10053 if (token2.type == CPP_LESS
10054 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10055 cp_parser_explicit_specialization (parser);
10056 /* `template <' indicates a template declaration. */
10057 else if (token2.type == CPP_LESS)
10058 cp_parser_template_declaration (parser, /*member_p=*/false);
10059 /* Anything else must be an explicit instantiation. */
10061 cp_parser_explicit_instantiation (parser);
10063 /* If the next token is `export', then we have a template
10065 else if (token1.keyword == RID_EXPORT)
10066 cp_parser_template_declaration (parser, /*member_p=*/false);
10067 /* If the next token is `extern', 'static' or 'inline' and the one
10068 after that is `template', we have a GNU extended explicit
10069 instantiation directive. */
10070 else if (cp_parser_allow_gnu_extensions_p (parser)
10071 && (token1.keyword == RID_EXTERN
10072 || token1.keyword == RID_STATIC
10073 || token1.keyword == RID_INLINE)
10074 && token2.keyword == RID_TEMPLATE)
10075 cp_parser_explicit_instantiation (parser);
10076 /* If the next token is `namespace', check for a named or unnamed
10077 namespace definition. */
10078 else if (token1.keyword == RID_NAMESPACE
10079 && (/* A named namespace definition. */
10080 (token2.type == CPP_NAME
10081 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10083 /* An unnamed namespace definition. */
10084 || token2.type == CPP_OPEN_BRACE
10085 || token2.keyword == RID_ATTRIBUTE))
10086 cp_parser_namespace_definition (parser);
10087 /* An inline (associated) namespace definition. */
10088 else if (token1.keyword == RID_INLINE
10089 && token2.keyword == RID_NAMESPACE)
10090 cp_parser_namespace_definition (parser);
10091 /* Objective-C++ declaration/definition. */
10092 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10093 cp_parser_objc_declaration (parser, NULL_TREE);
10094 else if (c_dialect_objc ()
10095 && token1.keyword == RID_ATTRIBUTE
10096 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10097 cp_parser_objc_declaration (parser, attributes);
10098 /* We must have either a block declaration or a function
10101 /* Try to parse a block-declaration, or a function-definition. */
10102 cp_parser_block_declaration (parser, /*statement_p=*/false);
10104 /* Free any declarators allocated. */
10105 obstack_free (&declarator_obstack, p);
10108 /* Parse a block-declaration.
10113 namespace-alias-definition
10120 __extension__ block-declaration
10125 static_assert-declaration
10127 If STATEMENT_P is TRUE, then this block-declaration is occurring as
10128 part of a declaration-statement. */
10131 cp_parser_block_declaration (cp_parser *parser,
10135 int saved_pedantic;
10137 /* Check for the `__extension__' keyword. */
10138 if (cp_parser_extension_opt (parser, &saved_pedantic))
10140 /* Parse the qualified declaration. */
10141 cp_parser_block_declaration (parser, statement_p);
10142 /* Restore the PEDANTIC flag. */
10143 pedantic = saved_pedantic;
10148 /* Peek at the next token to figure out which kind of declaration is
10150 token1 = cp_lexer_peek_token (parser->lexer);
10152 /* If the next keyword is `asm', we have an asm-definition. */
10153 if (token1->keyword == RID_ASM)
10156 cp_parser_commit_to_tentative_parse (parser);
10157 cp_parser_asm_definition (parser);
10159 /* If the next keyword is `namespace', we have a
10160 namespace-alias-definition. */
10161 else if (token1->keyword == RID_NAMESPACE)
10162 cp_parser_namespace_alias_definition (parser);
10163 /* If the next keyword is `using', we have a
10164 using-declaration, a using-directive, or an alias-declaration. */
10165 else if (token1->keyword == RID_USING)
10170 cp_parser_commit_to_tentative_parse (parser);
10171 /* If the token after `using' is `namespace', then we have a
10172 using-directive. */
10173 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10174 if (token2->keyword == RID_NAMESPACE)
10175 cp_parser_using_directive (parser);
10176 /* If the second token after 'using' is '=', then we have an
10177 alias-declaration. */
10178 else if (cxx_dialect >= cxx0x
10179 && token2->type == CPP_NAME
10180 && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10181 || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10182 == RID_ATTRIBUTE)))
10183 cp_parser_alias_declaration (parser);
10184 /* Otherwise, it's a using-declaration. */
10186 cp_parser_using_declaration (parser,
10187 /*access_declaration_p=*/false);
10189 /* If the next keyword is `__label__' we have a misplaced label
10191 else if (token1->keyword == RID_LABEL)
10193 cp_lexer_consume_token (parser->lexer);
10194 error_at (token1->location, "%<__label__%> not at the beginning of a block");
10195 cp_parser_skip_to_end_of_statement (parser);
10196 /* If the next token is now a `;', consume it. */
10197 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10198 cp_lexer_consume_token (parser->lexer);
10200 /* If the next token is `static_assert' we have a static assertion. */
10201 else if (token1->keyword == RID_STATIC_ASSERT)
10202 cp_parser_static_assert (parser, /*member_p=*/false);
10203 /* Anything else must be a simple-declaration. */
10205 cp_parser_simple_declaration (parser, !statement_p,
10206 /*maybe_range_for_decl*/NULL);
10209 /* Parse a simple-declaration.
10211 simple-declaration:
10212 decl-specifier-seq [opt] init-declarator-list [opt] ;
10214 init-declarator-list:
10216 init-declarator-list , init-declarator
10218 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10219 function-definition as a simple-declaration.
10221 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10222 parsed declaration if it is an uninitialized single declarator not followed
10223 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10224 if present, will not be consumed. */
10227 cp_parser_simple_declaration (cp_parser* parser,
10228 bool function_definition_allowed_p,
10229 tree *maybe_range_for_decl)
10231 cp_decl_specifier_seq decl_specifiers;
10232 int declares_class_or_enum;
10233 bool saw_declarator;
10235 if (maybe_range_for_decl)
10236 *maybe_range_for_decl = NULL_TREE;
10238 /* Defer access checks until we know what is being declared; the
10239 checks for names appearing in the decl-specifier-seq should be
10240 done as if we were in the scope of the thing being declared. */
10241 push_deferring_access_checks (dk_deferred);
10243 /* Parse the decl-specifier-seq. We have to keep track of whether
10244 or not the decl-specifier-seq declares a named class or
10245 enumeration type, since that is the only case in which the
10246 init-declarator-list is allowed to be empty.
10250 In a simple-declaration, the optional init-declarator-list can be
10251 omitted only when declaring a class or enumeration, that is when
10252 the decl-specifier-seq contains either a class-specifier, an
10253 elaborated-type-specifier, or an enum-specifier. */
10254 cp_parser_decl_specifier_seq (parser,
10255 CP_PARSER_FLAGS_OPTIONAL,
10257 &declares_class_or_enum);
10258 /* We no longer need to defer access checks. */
10259 stop_deferring_access_checks ();
10261 /* In a block scope, a valid declaration must always have a
10262 decl-specifier-seq. By not trying to parse declarators, we can
10263 resolve the declaration/expression ambiguity more quickly. */
10264 if (!function_definition_allowed_p
10265 && !decl_specifiers.any_specifiers_p)
10267 cp_parser_error (parser, "expected declaration");
10271 /* If the next two tokens are both identifiers, the code is
10272 erroneous. The usual cause of this situation is code like:
10276 where "T" should name a type -- but does not. */
10277 if (!decl_specifiers.any_type_specifiers_p
10278 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10280 /* If parsing tentatively, we should commit; we really are
10281 looking at a declaration. */
10282 cp_parser_commit_to_tentative_parse (parser);
10287 /* If we have seen at least one decl-specifier, and the next token
10288 is not a parenthesis, then we must be looking at a declaration.
10289 (After "int (" we might be looking at a functional cast.) */
10290 if (decl_specifiers.any_specifiers_p
10291 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10292 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10293 && !cp_parser_error_occurred (parser))
10294 cp_parser_commit_to_tentative_parse (parser);
10296 /* Keep going until we hit the `;' at the end of the simple
10298 saw_declarator = false;
10299 while (cp_lexer_next_token_is_not (parser->lexer,
10303 bool function_definition_p;
10306 if (saw_declarator)
10308 /* If we are processing next declarator, coma is expected */
10309 token = cp_lexer_peek_token (parser->lexer);
10310 gcc_assert (token->type == CPP_COMMA);
10311 cp_lexer_consume_token (parser->lexer);
10312 if (maybe_range_for_decl)
10313 *maybe_range_for_decl = error_mark_node;
10316 saw_declarator = true;
10318 /* Parse the init-declarator. */
10319 decl = cp_parser_init_declarator (parser, &decl_specifiers,
10321 function_definition_allowed_p,
10322 /*member_p=*/false,
10323 declares_class_or_enum,
10324 &function_definition_p,
10325 maybe_range_for_decl);
10326 /* If an error occurred while parsing tentatively, exit quickly.
10327 (That usually happens when in the body of a function; each
10328 statement is treated as a declaration-statement until proven
10330 if (cp_parser_error_occurred (parser))
10332 /* Handle function definitions specially. */
10333 if (function_definition_p)
10335 /* If the next token is a `,', then we are probably
10336 processing something like:
10340 which is erroneous. */
10341 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10343 cp_token *token = cp_lexer_peek_token (parser->lexer);
10344 error_at (token->location,
10346 " declarations and function-definitions is forbidden");
10348 /* Otherwise, we're done with the list of declarators. */
10351 pop_deferring_access_checks ();
10355 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10356 *maybe_range_for_decl = decl;
10357 /* The next token should be either a `,' or a `;'. */
10358 token = cp_lexer_peek_token (parser->lexer);
10359 /* If it's a `,', there are more declarators to come. */
10360 if (token->type == CPP_COMMA)
10361 /* will be consumed next time around */;
10362 /* If it's a `;', we are done. */
10363 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10365 /* Anything else is an error. */
10368 /* If we have already issued an error message we don't need
10369 to issue another one. */
10370 if (decl != error_mark_node
10371 || cp_parser_uncommitted_to_tentative_parse_p (parser))
10372 cp_parser_error (parser, "expected %<,%> or %<;%>");
10373 /* Skip tokens until we reach the end of the statement. */
10374 cp_parser_skip_to_end_of_statement (parser);
10375 /* If the next token is now a `;', consume it. */
10376 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10377 cp_lexer_consume_token (parser->lexer);
10380 /* After the first time around, a function-definition is not
10381 allowed -- even if it was OK at first. For example:
10386 function_definition_allowed_p = false;
10389 /* Issue an error message if no declarators are present, and the
10390 decl-specifier-seq does not itself declare a class or
10392 if (!saw_declarator)
10394 if (cp_parser_declares_only_class_p (parser))
10395 shadow_tag (&decl_specifiers);
10396 /* Perform any deferred access checks. */
10397 perform_deferred_access_checks ();
10400 /* Consume the `;'. */
10401 if (!maybe_range_for_decl)
10402 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10405 pop_deferring_access_checks ();
10408 /* Parse a decl-specifier-seq.
10410 decl-specifier-seq:
10411 decl-specifier-seq [opt] decl-specifier
10414 storage-class-specifier
10425 Set *DECL_SPECS to a representation of the decl-specifier-seq.
10427 The parser flags FLAGS is used to control type-specifier parsing.
10429 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10432 1: one of the decl-specifiers is an elaborated-type-specifier
10433 (i.e., a type declaration)
10434 2: one of the decl-specifiers is an enum-specifier or a
10435 class-specifier (i.e., a type definition)
10440 cp_parser_decl_specifier_seq (cp_parser* parser,
10441 cp_parser_flags flags,
10442 cp_decl_specifier_seq *decl_specs,
10443 int* declares_class_or_enum)
10445 bool constructor_possible_p = !parser->in_declarator_p;
10446 cp_token *start_token = NULL;
10448 /* Clear DECL_SPECS. */
10449 clear_decl_specs (decl_specs);
10451 /* Assume no class or enumeration type is declared. */
10452 *declares_class_or_enum = 0;
10454 /* Keep reading specifiers until there are no more to read. */
10457 bool constructor_p;
10458 bool found_decl_spec;
10461 /* Peek at the next token. */
10462 token = cp_lexer_peek_token (parser->lexer);
10464 /* Save the first token of the decl spec list for error
10467 start_token = token;
10468 /* Handle attributes. */
10469 if (token->keyword == RID_ATTRIBUTE)
10471 /* Parse the attributes. */
10472 decl_specs->attributes
10473 = chainon (decl_specs->attributes,
10474 cp_parser_attributes_opt (parser));
10477 /* Assume we will find a decl-specifier keyword. */
10478 found_decl_spec = true;
10479 /* If the next token is an appropriate keyword, we can simply
10480 add it to the list. */
10481 switch (token->keyword)
10487 if (!at_class_scope_p ())
10489 error_at (token->location, "%<friend%> used outside of class");
10490 cp_lexer_purge_token (parser->lexer);
10494 ++decl_specs->specs[(int) ds_friend];
10495 /* Consume the token. */
10496 cp_lexer_consume_token (parser->lexer);
10500 case RID_CONSTEXPR:
10501 ++decl_specs->specs[(int) ds_constexpr];
10502 cp_lexer_consume_token (parser->lexer);
10505 /* function-specifier:
10512 cp_parser_function_specifier_opt (parser, decl_specs);
10518 ++decl_specs->specs[(int) ds_typedef];
10519 /* Consume the token. */
10520 cp_lexer_consume_token (parser->lexer);
10521 /* A constructor declarator cannot appear in a typedef. */
10522 constructor_possible_p = false;
10523 /* The "typedef" keyword can only occur in a declaration; we
10524 may as well commit at this point. */
10525 cp_parser_commit_to_tentative_parse (parser);
10527 if (decl_specs->storage_class != sc_none)
10528 decl_specs->conflicting_specifiers_p = true;
10531 /* storage-class-specifier:
10541 if (cxx_dialect == cxx98)
10543 /* Consume the token. */
10544 cp_lexer_consume_token (parser->lexer);
10546 /* Complain about `auto' as a storage specifier, if
10547 we're complaining about C++0x compatibility. */
10548 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10549 " changes meaning in C++11; please remove it");
10551 /* Set the storage class anyway. */
10552 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10556 /* C++0x auto type-specifier. */
10557 found_decl_spec = false;
10564 /* Consume the token. */
10565 cp_lexer_consume_token (parser->lexer);
10566 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10570 /* Consume the token. */
10571 cp_lexer_consume_token (parser->lexer);
10572 ++decl_specs->specs[(int) ds_thread];
10576 /* We did not yet find a decl-specifier yet. */
10577 found_decl_spec = false;
10581 if (found_decl_spec
10582 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10583 && token->keyword != RID_CONSTEXPR)
10584 error ("decl-specifier invalid in condition");
10586 /* Constructors are a special case. The `S' in `S()' is not a
10587 decl-specifier; it is the beginning of the declarator. */
10589 = (!found_decl_spec
10590 && constructor_possible_p
10591 && (cp_parser_constructor_declarator_p
10592 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10594 /* If we don't have a DECL_SPEC yet, then we must be looking at
10595 a type-specifier. */
10596 if (!found_decl_spec && !constructor_p)
10598 int decl_spec_declares_class_or_enum;
10599 bool is_cv_qualifier;
10603 = cp_parser_type_specifier (parser, flags,
10605 /*is_declaration=*/true,
10606 &decl_spec_declares_class_or_enum,
10608 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10610 /* If this type-specifier referenced a user-defined type
10611 (a typedef, class-name, etc.), then we can't allow any
10612 more such type-specifiers henceforth.
10616 The longest sequence of decl-specifiers that could
10617 possibly be a type name is taken as the
10618 decl-specifier-seq of a declaration. The sequence shall
10619 be self-consistent as described below.
10623 As a general rule, at most one type-specifier is allowed
10624 in the complete decl-specifier-seq of a declaration. The
10625 only exceptions are the following:
10627 -- const or volatile can be combined with any other
10630 -- signed or unsigned can be combined with char, long,
10638 void g (const int Pc);
10640 Here, Pc is *not* part of the decl-specifier seq; it's
10641 the declarator. Therefore, once we see a type-specifier
10642 (other than a cv-qualifier), we forbid any additional
10643 user-defined types. We *do* still allow things like `int
10644 int' to be considered a decl-specifier-seq, and issue the
10645 error message later. */
10646 if (type_spec && !is_cv_qualifier)
10647 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10648 /* A constructor declarator cannot follow a type-specifier. */
10651 constructor_possible_p = false;
10652 found_decl_spec = true;
10653 if (!is_cv_qualifier)
10654 decl_specs->any_type_specifiers_p = true;
10658 /* If we still do not have a DECL_SPEC, then there are no more
10659 decl-specifiers. */
10660 if (!found_decl_spec)
10663 decl_specs->any_specifiers_p = true;
10664 /* After we see one decl-specifier, further decl-specifiers are
10665 always optional. */
10666 flags |= CP_PARSER_FLAGS_OPTIONAL;
10669 cp_parser_check_decl_spec (decl_specs, start_token->location);
10671 /* Don't allow a friend specifier with a class definition. */
10672 if (decl_specs->specs[(int) ds_friend] != 0
10673 && (*declares_class_or_enum & 2))
10674 error_at (start_token->location,
10675 "class definition may not be declared a friend");
10678 /* Parse an (optional) storage-class-specifier.
10680 storage-class-specifier:
10689 storage-class-specifier:
10692 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10695 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10697 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10700 if (cxx_dialect != cxx98)
10702 /* Fall through for C++98. */
10709 /* Consume the token. */
10710 return cp_lexer_consume_token (parser->lexer)->u.value;
10717 /* Parse an (optional) function-specifier.
10719 function-specifier:
10724 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10725 Updates DECL_SPECS, if it is non-NULL. */
10728 cp_parser_function_specifier_opt (cp_parser* parser,
10729 cp_decl_specifier_seq *decl_specs)
10731 cp_token *token = cp_lexer_peek_token (parser->lexer);
10732 switch (token->keyword)
10736 ++decl_specs->specs[(int) ds_inline];
10740 /* 14.5.2.3 [temp.mem]
10742 A member function template shall not be virtual. */
10743 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10744 error_at (token->location, "templates may not be %<virtual%>");
10745 else if (decl_specs)
10746 ++decl_specs->specs[(int) ds_virtual];
10751 ++decl_specs->specs[(int) ds_explicit];
10758 /* Consume the token. */
10759 return cp_lexer_consume_token (parser->lexer)->u.value;
10762 /* Parse a linkage-specification.
10764 linkage-specification:
10765 extern string-literal { declaration-seq [opt] }
10766 extern string-literal declaration */
10769 cp_parser_linkage_specification (cp_parser* parser)
10773 /* Look for the `extern' keyword. */
10774 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10776 /* Look for the string-literal. */
10777 linkage = cp_parser_string_literal (parser, false, false);
10779 /* Transform the literal into an identifier. If the literal is a
10780 wide-character string, or contains embedded NULs, then we can't
10781 handle it as the user wants. */
10782 if (strlen (TREE_STRING_POINTER (linkage))
10783 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10785 cp_parser_error (parser, "invalid linkage-specification");
10786 /* Assume C++ linkage. */
10787 linkage = lang_name_cplusplus;
10790 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10792 /* We're now using the new linkage. */
10793 push_lang_context (linkage);
10795 /* If the next token is a `{', then we're using the first
10797 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10799 /* Consume the `{' token. */
10800 cp_lexer_consume_token (parser->lexer);
10801 /* Parse the declarations. */
10802 cp_parser_declaration_seq_opt (parser);
10803 /* Look for the closing `}'. */
10804 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10806 /* Otherwise, there's just one declaration. */
10809 bool saved_in_unbraced_linkage_specification_p;
10811 saved_in_unbraced_linkage_specification_p
10812 = parser->in_unbraced_linkage_specification_p;
10813 parser->in_unbraced_linkage_specification_p = true;
10814 cp_parser_declaration (parser);
10815 parser->in_unbraced_linkage_specification_p
10816 = saved_in_unbraced_linkage_specification_p;
10819 /* We're done with the linkage-specification. */
10820 pop_lang_context ();
10823 /* Parse a static_assert-declaration.
10825 static_assert-declaration:
10826 static_assert ( constant-expression , string-literal ) ;
10828 If MEMBER_P, this static_assert is a class member. */
10831 cp_parser_static_assert(cp_parser *parser, bool member_p)
10836 location_t saved_loc;
10839 /* Peek at the `static_assert' token so we can keep track of exactly
10840 where the static assertion started. */
10841 token = cp_lexer_peek_token (parser->lexer);
10842 saved_loc = token->location;
10844 /* Look for the `static_assert' keyword. */
10845 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10849 /* We know we are in a static assertion; commit to any tentative
10851 if (cp_parser_parsing_tentatively (parser))
10852 cp_parser_commit_to_tentative_parse (parser);
10854 /* Parse the `(' starting the static assertion condition. */
10855 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10857 /* Parse the constant-expression. Allow a non-constant expression
10858 here in order to give better diagnostics in finish_static_assert. */
10860 cp_parser_constant_expression (parser,
10861 /*allow_non_constant_p=*/true,
10862 /*non_constant_p=*/&dummy);
10864 /* Parse the separating `,'. */
10865 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10867 /* Parse the string-literal message. */
10868 message = cp_parser_string_literal (parser,
10869 /*translate=*/false,
10872 /* A `)' completes the static assertion. */
10873 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10874 cp_parser_skip_to_closing_parenthesis (parser,
10875 /*recovering=*/true,
10876 /*or_comma=*/false,
10877 /*consume_paren=*/true);
10879 /* A semicolon terminates the declaration. */
10880 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10882 /* Complete the static assertion, which may mean either processing
10883 the static assert now or saving it for template instantiation. */
10884 finish_static_assert (condition, message, saved_loc, member_p);
10887 /* Parse a `decltype' type. Returns the type.
10889 simple-type-specifier:
10890 decltype ( expression ) */
10893 cp_parser_decltype (cp_parser *parser)
10896 bool id_expression_or_member_access_p = false;
10897 const char *saved_message;
10898 bool saved_integral_constant_expression_p;
10899 bool saved_non_integral_constant_expression_p;
10900 cp_token *id_expr_start_token;
10901 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10903 if (start_token->type == CPP_DECLTYPE)
10905 /* Already parsed. */
10906 cp_lexer_consume_token (parser->lexer);
10907 return start_token->u.value;
10910 /* Look for the `decltype' token. */
10911 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10912 return error_mark_node;
10914 /* Types cannot be defined in a `decltype' expression. Save away the
10916 saved_message = parser->type_definition_forbidden_message;
10918 /* And create the new one. */
10919 parser->type_definition_forbidden_message
10920 = G_("types may not be defined in %<decltype%> expressions");
10922 /* The restrictions on constant-expressions do not apply inside
10923 decltype expressions. */
10924 saved_integral_constant_expression_p
10925 = parser->integral_constant_expression_p;
10926 saved_non_integral_constant_expression_p
10927 = parser->non_integral_constant_expression_p;
10928 parser->integral_constant_expression_p = false;
10930 /* Do not actually evaluate the expression. */
10931 ++cp_unevaluated_operand;
10933 /* Do not warn about problems with the expression. */
10934 ++c_inhibit_evaluation_warnings;
10936 /* Parse the opening `('. */
10937 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10938 return error_mark_node;
10940 /* First, try parsing an id-expression. */
10941 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10942 cp_parser_parse_tentatively (parser);
10943 expr = cp_parser_id_expression (parser,
10944 /*template_keyword_p=*/false,
10945 /*check_dependency_p=*/true,
10946 /*template_p=*/NULL,
10947 /*declarator_p=*/false,
10948 /*optional_p=*/false);
10950 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10952 bool non_integral_constant_expression_p = false;
10953 tree id_expression = expr;
10955 const char *error_msg;
10957 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10958 /* Lookup the name we got back from the id-expression. */
10959 expr = cp_parser_lookup_name (parser, expr,
10961 /*is_template=*/false,
10962 /*is_namespace=*/false,
10963 /*check_dependency=*/true,
10964 /*ambiguous_decls=*/NULL,
10965 id_expr_start_token->location);
10968 && expr != error_mark_node
10969 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10970 && TREE_CODE (expr) != TYPE_DECL
10971 && (TREE_CODE (expr) != BIT_NOT_EXPR
10972 || !TYPE_P (TREE_OPERAND (expr, 0)))
10973 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10975 /* Complete lookup of the id-expression. */
10976 expr = (finish_id_expression
10977 (id_expression, expr, parser->scope, &idk,
10978 /*integral_constant_expression_p=*/false,
10979 /*allow_non_integral_constant_expression_p=*/true,
10980 &non_integral_constant_expression_p,
10981 /*template_p=*/false,
10983 /*address_p=*/false,
10984 /*template_arg_p=*/false,
10986 id_expr_start_token->location));
10988 if (expr == error_mark_node)
10989 /* We found an id-expression, but it was something that we
10990 should not have found. This is an error, not something
10991 we can recover from, so note that we found an
10992 id-expression and we'll recover as gracefully as
10994 id_expression_or_member_access_p = true;
10998 && expr != error_mark_node
10999 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11000 /* We have an id-expression. */
11001 id_expression_or_member_access_p = true;
11004 if (!id_expression_or_member_access_p)
11006 /* Abort the id-expression parse. */
11007 cp_parser_abort_tentative_parse (parser);
11009 /* Parsing tentatively, again. */
11010 cp_parser_parse_tentatively (parser);
11012 /* Parse a class member access. */
11013 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11015 /*member_access_only_p=*/true, NULL);
11018 && expr != error_mark_node
11019 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11020 /* We have an id-expression. */
11021 id_expression_or_member_access_p = true;
11024 if (id_expression_or_member_access_p)
11025 /* We have parsed the complete id-expression or member access. */
11026 cp_parser_parse_definitely (parser);
11029 bool saved_greater_than_is_operator_p;
11031 /* Abort our attempt to parse an id-expression or member access
11033 cp_parser_abort_tentative_parse (parser);
11035 /* Within a parenthesized expression, a `>' token is always
11036 the greater-than operator. */
11037 saved_greater_than_is_operator_p
11038 = parser->greater_than_is_operator_p;
11039 parser->greater_than_is_operator_p = true;
11041 /* Parse a full expression. */
11042 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11044 /* The `>' token might be the end of a template-id or
11045 template-parameter-list now. */
11046 parser->greater_than_is_operator_p
11047 = saved_greater_than_is_operator_p;
11050 /* Go back to evaluating expressions. */
11051 --cp_unevaluated_operand;
11052 --c_inhibit_evaluation_warnings;
11054 /* Restore the old message and the integral constant expression
11056 parser->type_definition_forbidden_message = saved_message;
11057 parser->integral_constant_expression_p
11058 = saved_integral_constant_expression_p;
11059 parser->non_integral_constant_expression_p
11060 = saved_non_integral_constant_expression_p;
11062 /* Parse to the closing `)'. */
11063 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11065 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11066 /*consume_paren=*/true);
11067 return error_mark_node;
11070 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11071 tf_warning_or_error);
11073 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11075 start_token->type = CPP_DECLTYPE;
11076 start_token->u.value = expr;
11077 start_token->keyword = RID_MAX;
11078 cp_lexer_purge_tokens_after (parser->lexer, start_token);
11083 /* Special member functions [gram.special] */
11085 /* Parse a conversion-function-id.
11087 conversion-function-id:
11088 operator conversion-type-id
11090 Returns an IDENTIFIER_NODE representing the operator. */
11093 cp_parser_conversion_function_id (cp_parser* parser)
11097 tree saved_qualifying_scope;
11098 tree saved_object_scope;
11099 tree pushed_scope = NULL_TREE;
11101 /* Look for the `operator' token. */
11102 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11103 return error_mark_node;
11104 /* When we parse the conversion-type-id, the current scope will be
11105 reset. However, we need that information in able to look up the
11106 conversion function later, so we save it here. */
11107 saved_scope = parser->scope;
11108 saved_qualifying_scope = parser->qualifying_scope;
11109 saved_object_scope = parser->object_scope;
11110 /* We must enter the scope of the class so that the names of
11111 entities declared within the class are available in the
11112 conversion-type-id. For example, consider:
11119 S::operator I() { ... }
11121 In order to see that `I' is a type-name in the definition, we
11122 must be in the scope of `S'. */
11124 pushed_scope = push_scope (saved_scope);
11125 /* Parse the conversion-type-id. */
11126 type = cp_parser_conversion_type_id (parser);
11127 /* Leave the scope of the class, if any. */
11129 pop_scope (pushed_scope);
11130 /* Restore the saved scope. */
11131 parser->scope = saved_scope;
11132 parser->qualifying_scope = saved_qualifying_scope;
11133 parser->object_scope = saved_object_scope;
11134 /* If the TYPE is invalid, indicate failure. */
11135 if (type == error_mark_node)
11136 return error_mark_node;
11137 return mangle_conv_op_name_for_type (type);
11140 /* Parse a conversion-type-id:
11142 conversion-type-id:
11143 type-specifier-seq conversion-declarator [opt]
11145 Returns the TYPE specified. */
11148 cp_parser_conversion_type_id (cp_parser* parser)
11151 cp_decl_specifier_seq type_specifiers;
11152 cp_declarator *declarator;
11153 tree type_specified;
11155 /* Parse the attributes. */
11156 attributes = cp_parser_attributes_opt (parser);
11157 /* Parse the type-specifiers. */
11158 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11159 /*is_trailing_return=*/false,
11161 /* If that didn't work, stop. */
11162 if (type_specifiers.type == error_mark_node)
11163 return error_mark_node;
11164 /* Parse the conversion-declarator. */
11165 declarator = cp_parser_conversion_declarator_opt (parser);
11167 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
11168 /*initialized=*/0, &attributes);
11170 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11172 /* Don't give this error when parsing tentatively. This happens to
11173 work because we always parse this definitively once. */
11174 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11175 && type_uses_auto (type_specified))
11177 error ("invalid use of %<auto%> in conversion operator");
11178 return error_mark_node;
11181 return type_specified;
11184 /* Parse an (optional) conversion-declarator.
11186 conversion-declarator:
11187 ptr-operator conversion-declarator [opt]
11191 static cp_declarator *
11192 cp_parser_conversion_declarator_opt (cp_parser* parser)
11194 enum tree_code code;
11196 cp_cv_quals cv_quals;
11198 /* We don't know if there's a ptr-operator next, or not. */
11199 cp_parser_parse_tentatively (parser);
11200 /* Try the ptr-operator. */
11201 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11202 /* If it worked, look for more conversion-declarators. */
11203 if (cp_parser_parse_definitely (parser))
11205 cp_declarator *declarator;
11207 /* Parse another optional declarator. */
11208 declarator = cp_parser_conversion_declarator_opt (parser);
11210 return cp_parser_make_indirect_declarator
11211 (code, class_type, cv_quals, declarator);
11217 /* Parse an (optional) ctor-initializer.
11220 : mem-initializer-list
11222 Returns TRUE iff the ctor-initializer was actually present. */
11225 cp_parser_ctor_initializer_opt (cp_parser* parser)
11227 /* If the next token is not a `:', then there is no
11228 ctor-initializer. */
11229 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11231 /* Do default initialization of any bases and members. */
11232 if (DECL_CONSTRUCTOR_P (current_function_decl))
11233 finish_mem_initializers (NULL_TREE);
11238 /* Consume the `:' token. */
11239 cp_lexer_consume_token (parser->lexer);
11240 /* And the mem-initializer-list. */
11241 cp_parser_mem_initializer_list (parser);
11246 /* Parse a mem-initializer-list.
11248 mem-initializer-list:
11249 mem-initializer ... [opt]
11250 mem-initializer ... [opt] , mem-initializer-list */
11253 cp_parser_mem_initializer_list (cp_parser* parser)
11255 tree mem_initializer_list = NULL_TREE;
11256 cp_token *token = cp_lexer_peek_token (parser->lexer);
11258 /* Let the semantic analysis code know that we are starting the
11259 mem-initializer-list. */
11260 if (!DECL_CONSTRUCTOR_P (current_function_decl))
11261 error_at (token->location,
11262 "only constructors take member initializers");
11264 /* Loop through the list. */
11267 tree mem_initializer;
11269 token = cp_lexer_peek_token (parser->lexer);
11270 /* Parse the mem-initializer. */
11271 mem_initializer = cp_parser_mem_initializer (parser);
11272 /* If the next token is a `...', we're expanding member initializers. */
11273 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11275 /* Consume the `...'. */
11276 cp_lexer_consume_token (parser->lexer);
11278 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11279 can be expanded but members cannot. */
11280 if (mem_initializer != error_mark_node
11281 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11283 error_at (token->location,
11284 "cannot expand initializer for member %<%D%>",
11285 TREE_PURPOSE (mem_initializer));
11286 mem_initializer = error_mark_node;
11289 /* Construct the pack expansion type. */
11290 if (mem_initializer != error_mark_node)
11291 mem_initializer = make_pack_expansion (mem_initializer);
11293 /* Add it to the list, unless it was erroneous. */
11294 if (mem_initializer != error_mark_node)
11296 TREE_CHAIN (mem_initializer) = mem_initializer_list;
11297 mem_initializer_list = mem_initializer;
11299 /* If the next token is not a `,', we're done. */
11300 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11302 /* Consume the `,' token. */
11303 cp_lexer_consume_token (parser->lexer);
11306 /* Perform semantic analysis. */
11307 if (DECL_CONSTRUCTOR_P (current_function_decl))
11308 finish_mem_initializers (mem_initializer_list);
11311 /* Parse a mem-initializer.
11314 mem-initializer-id ( expression-list [opt] )
11315 mem-initializer-id braced-init-list
11320 ( expression-list [opt] )
11322 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
11323 class) or FIELD_DECL (for a non-static data member) to initialize;
11324 the TREE_VALUE is the expression-list. An empty initialization
11325 list is represented by void_list_node. */
11328 cp_parser_mem_initializer (cp_parser* parser)
11330 tree mem_initializer_id;
11331 tree expression_list;
11333 cp_token *token = cp_lexer_peek_token (parser->lexer);
11335 /* Find out what is being initialized. */
11336 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11338 permerror (token->location,
11339 "anachronistic old-style base class initializer");
11340 mem_initializer_id = NULL_TREE;
11344 mem_initializer_id = cp_parser_mem_initializer_id (parser);
11345 if (mem_initializer_id == error_mark_node)
11346 return mem_initializer_id;
11348 member = expand_member_init (mem_initializer_id);
11349 if (member && !DECL_P (member))
11350 in_base_initializer = 1;
11352 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11354 bool expr_non_constant_p;
11355 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11356 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11357 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11358 expression_list = build_tree_list (NULL_TREE, expression_list);
11363 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11365 /*allow_expansion_p=*/true,
11366 /*non_constant_p=*/NULL);
11368 return error_mark_node;
11369 expression_list = build_tree_list_vec (vec);
11370 release_tree_vector (vec);
11373 if (expression_list == error_mark_node)
11374 return error_mark_node;
11375 if (!expression_list)
11376 expression_list = void_type_node;
11378 in_base_initializer = 0;
11380 return member ? build_tree_list (member, expression_list) : error_mark_node;
11383 /* Parse a mem-initializer-id.
11385 mem-initializer-id:
11386 :: [opt] nested-name-specifier [opt] class-name
11389 Returns a TYPE indicating the class to be initializer for the first
11390 production. Returns an IDENTIFIER_NODE indicating the data member
11391 to be initialized for the second production. */
11394 cp_parser_mem_initializer_id (cp_parser* parser)
11396 bool global_scope_p;
11397 bool nested_name_specifier_p;
11398 bool template_p = false;
11401 cp_token *token = cp_lexer_peek_token (parser->lexer);
11403 /* `typename' is not allowed in this context ([temp.res]). */
11404 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11406 error_at (token->location,
11407 "keyword %<typename%> not allowed in this context (a qualified "
11408 "member initializer is implicitly a type)");
11409 cp_lexer_consume_token (parser->lexer);
11411 /* Look for the optional `::' operator. */
11413 = (cp_parser_global_scope_opt (parser,
11414 /*current_scope_valid_p=*/false)
11416 /* Look for the optional nested-name-specifier. The simplest way to
11421 The keyword `typename' is not permitted in a base-specifier or
11422 mem-initializer; in these contexts a qualified name that
11423 depends on a template-parameter is implicitly assumed to be a
11426 is to assume that we have seen the `typename' keyword at this
11428 nested_name_specifier_p
11429 = (cp_parser_nested_name_specifier_opt (parser,
11430 /*typename_keyword_p=*/true,
11431 /*check_dependency_p=*/true,
11433 /*is_declaration=*/true)
11435 if (nested_name_specifier_p)
11436 template_p = cp_parser_optional_template_keyword (parser);
11437 /* If there is a `::' operator or a nested-name-specifier, then we
11438 are definitely looking for a class-name. */
11439 if (global_scope_p || nested_name_specifier_p)
11440 return cp_parser_class_name (parser,
11441 /*typename_keyword_p=*/true,
11442 /*template_keyword_p=*/template_p,
11444 /*check_dependency_p=*/true,
11445 /*class_head_p=*/false,
11446 /*is_declaration=*/true);
11447 /* Otherwise, we could also be looking for an ordinary identifier. */
11448 cp_parser_parse_tentatively (parser);
11449 /* Try a class-name. */
11450 id = cp_parser_class_name (parser,
11451 /*typename_keyword_p=*/true,
11452 /*template_keyword_p=*/false,
11454 /*check_dependency_p=*/true,
11455 /*class_head_p=*/false,
11456 /*is_declaration=*/true);
11457 /* If we found one, we're done. */
11458 if (cp_parser_parse_definitely (parser))
11460 /* Otherwise, look for an ordinary identifier. */
11461 return cp_parser_identifier (parser);
11464 /* Overloading [gram.over] */
11466 /* Parse an operator-function-id.
11468 operator-function-id:
11471 Returns an IDENTIFIER_NODE for the operator which is a
11472 human-readable spelling of the identifier, e.g., `operator +'. */
11475 cp_parser_operator_function_id (cp_parser* parser)
11477 /* Look for the `operator' keyword. */
11478 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11479 return error_mark_node;
11480 /* And then the name of the operator itself. */
11481 return cp_parser_operator (parser);
11484 /* Return an identifier node for a user-defined literal operator.
11485 The suffix identifier is chained to the operator name identifier. */
11488 cp_literal_operator_id (const char* name)
11491 char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11492 + strlen (name) + 10);
11493 sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11494 identifier = get_identifier (buffer);
11495 /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11500 /* Parse an operator.
11503 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11504 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11505 || ++ -- , ->* -> () []
11512 Returns an IDENTIFIER_NODE for the operator which is a
11513 human-readable spelling of the identifier, e.g., `operator +'. */
11516 cp_parser_operator (cp_parser* parser)
11518 tree id = NULL_TREE;
11521 /* Peek at the next token. */
11522 token = cp_lexer_peek_token (parser->lexer);
11523 /* Figure out which operator we have. */
11524 switch (token->type)
11530 /* The keyword should be either `new' or `delete'. */
11531 if (token->keyword == RID_NEW)
11533 else if (token->keyword == RID_DELETE)
11538 /* Consume the `new' or `delete' token. */
11539 cp_lexer_consume_token (parser->lexer);
11541 /* Peek at the next token. */
11542 token = cp_lexer_peek_token (parser->lexer);
11543 /* If it's a `[' token then this is the array variant of the
11545 if (token->type == CPP_OPEN_SQUARE)
11547 /* Consume the `[' token. */
11548 cp_lexer_consume_token (parser->lexer);
11549 /* Look for the `]' token. */
11550 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11551 id = ansi_opname (op == NEW_EXPR
11552 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11554 /* Otherwise, we have the non-array variant. */
11556 id = ansi_opname (op);
11562 id = ansi_opname (PLUS_EXPR);
11566 id = ansi_opname (MINUS_EXPR);
11570 id = ansi_opname (MULT_EXPR);
11574 id = ansi_opname (TRUNC_DIV_EXPR);
11578 id = ansi_opname (TRUNC_MOD_EXPR);
11582 id = ansi_opname (BIT_XOR_EXPR);
11586 id = ansi_opname (BIT_AND_EXPR);
11590 id = ansi_opname (BIT_IOR_EXPR);
11594 id = ansi_opname (BIT_NOT_EXPR);
11598 id = ansi_opname (TRUTH_NOT_EXPR);
11602 id = ansi_assopname (NOP_EXPR);
11606 id = ansi_opname (LT_EXPR);
11610 id = ansi_opname (GT_EXPR);
11614 id = ansi_assopname (PLUS_EXPR);
11618 id = ansi_assopname (MINUS_EXPR);
11622 id = ansi_assopname (MULT_EXPR);
11626 id = ansi_assopname (TRUNC_DIV_EXPR);
11630 id = ansi_assopname (TRUNC_MOD_EXPR);
11634 id = ansi_assopname (BIT_XOR_EXPR);
11638 id = ansi_assopname (BIT_AND_EXPR);
11642 id = ansi_assopname (BIT_IOR_EXPR);
11646 id = ansi_opname (LSHIFT_EXPR);
11650 id = ansi_opname (RSHIFT_EXPR);
11653 case CPP_LSHIFT_EQ:
11654 id = ansi_assopname (LSHIFT_EXPR);
11657 case CPP_RSHIFT_EQ:
11658 id = ansi_assopname (RSHIFT_EXPR);
11662 id = ansi_opname (EQ_EXPR);
11666 id = ansi_opname (NE_EXPR);
11670 id = ansi_opname (LE_EXPR);
11673 case CPP_GREATER_EQ:
11674 id = ansi_opname (GE_EXPR);
11678 id = ansi_opname (TRUTH_ANDIF_EXPR);
11682 id = ansi_opname (TRUTH_ORIF_EXPR);
11685 case CPP_PLUS_PLUS:
11686 id = ansi_opname (POSTINCREMENT_EXPR);
11689 case CPP_MINUS_MINUS:
11690 id = ansi_opname (PREDECREMENT_EXPR);
11694 id = ansi_opname (COMPOUND_EXPR);
11697 case CPP_DEREF_STAR:
11698 id = ansi_opname (MEMBER_REF);
11702 id = ansi_opname (COMPONENT_REF);
11705 case CPP_OPEN_PAREN:
11706 /* Consume the `('. */
11707 cp_lexer_consume_token (parser->lexer);
11708 /* Look for the matching `)'. */
11709 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11710 return ansi_opname (CALL_EXPR);
11712 case CPP_OPEN_SQUARE:
11713 /* Consume the `['. */
11714 cp_lexer_consume_token (parser->lexer);
11715 /* Look for the matching `]'. */
11716 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11717 return ansi_opname (ARRAY_REF);
11720 if (cxx_dialect == cxx98)
11721 maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11722 if (TREE_STRING_LENGTH (token->u.value) > 2)
11724 error ("expected empty string after %<operator%> keyword");
11725 return error_mark_node;
11727 /* Consume the string. */
11728 cp_lexer_consume_token (parser->lexer);
11729 /* Look for the suffix identifier. */
11730 token = cp_lexer_peek_token (parser->lexer);
11731 if (token->type == CPP_NAME)
11733 id = cp_parser_identifier (parser);
11734 if (id != error_mark_node)
11736 const char *name = IDENTIFIER_POINTER (id);
11737 return cp_literal_operator_id (name);
11742 error ("expected suffix identifier");
11743 return error_mark_node;
11746 case CPP_STRING_USERDEF:
11747 error ("missing space between %<\"\"%> and suffix identifier");
11748 return error_mark_node;
11751 /* Anything else is an error. */
11755 /* If we have selected an identifier, we need to consume the
11758 cp_lexer_consume_token (parser->lexer);
11759 /* Otherwise, no valid operator name was present. */
11762 cp_parser_error (parser, "expected operator");
11763 id = error_mark_node;
11769 /* Parse a template-declaration.
11771 template-declaration:
11772 export [opt] template < template-parameter-list > declaration
11774 If MEMBER_P is TRUE, this template-declaration occurs within a
11777 The grammar rule given by the standard isn't correct. What
11778 is really meant is:
11780 template-declaration:
11781 export [opt] template-parameter-list-seq
11782 decl-specifier-seq [opt] init-declarator [opt] ;
11783 export [opt] template-parameter-list-seq
11784 function-definition
11786 template-parameter-list-seq:
11787 template-parameter-list-seq [opt]
11788 template < template-parameter-list > */
11791 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11793 /* Check for `export'. */
11794 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11796 /* Consume the `export' token. */
11797 cp_lexer_consume_token (parser->lexer);
11798 /* Warn that we do not support `export'. */
11799 warning (0, "keyword %<export%> not implemented, and will be ignored");
11802 cp_parser_template_declaration_after_export (parser, member_p);
11805 /* Parse a template-parameter-list.
11807 template-parameter-list:
11809 template-parameter-list , template-parameter
11811 Returns a TREE_LIST. Each node represents a template parameter.
11812 The nodes are connected via their TREE_CHAINs. */
11815 cp_parser_template_parameter_list (cp_parser* parser)
11817 tree parameter_list = NULL_TREE;
11819 begin_template_parm_list ();
11821 /* The loop below parses the template parms. We first need to know
11822 the total number of template parms to be able to compute proper
11823 canonical types of each dependent type. So after the loop, when
11824 we know the total number of template parms,
11825 end_template_parm_list computes the proper canonical types and
11826 fixes up the dependent types accordingly. */
11831 bool is_parameter_pack;
11832 location_t parm_loc;
11834 /* Parse the template-parameter. */
11835 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11836 parameter = cp_parser_template_parameter (parser,
11838 &is_parameter_pack);
11839 /* Add it to the list. */
11840 if (parameter != error_mark_node)
11841 parameter_list = process_template_parm (parameter_list,
11849 tree err_parm = build_tree_list (parameter, parameter);
11850 parameter_list = chainon (parameter_list, err_parm);
11853 /* If the next token is not a `,', we're done. */
11854 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11856 /* Otherwise, consume the `,' token. */
11857 cp_lexer_consume_token (parser->lexer);
11860 return end_template_parm_list (parameter_list);
11863 /* Parse a template-parameter.
11865 template-parameter:
11867 parameter-declaration
11869 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11870 the parameter. The TREE_PURPOSE is the default value, if any.
11871 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11872 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11873 set to true iff this parameter is a parameter pack. */
11876 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11877 bool *is_parameter_pack)
11880 cp_parameter_declarator *parameter_declarator;
11881 cp_declarator *id_declarator;
11884 /* Assume it is a type parameter or a template parameter. */
11885 *is_non_type = false;
11886 /* Assume it not a parameter pack. */
11887 *is_parameter_pack = false;
11888 /* Peek at the next token. */
11889 token = cp_lexer_peek_token (parser->lexer);
11890 /* If it is `class' or `template', we have a type-parameter. */
11891 if (token->keyword == RID_TEMPLATE)
11892 return cp_parser_type_parameter (parser, is_parameter_pack);
11893 /* If it is `class' or `typename' we do not know yet whether it is a
11894 type parameter or a non-type parameter. Consider:
11896 template <typename T, typename T::X X> ...
11900 template <class C, class D*> ...
11902 Here, the first parameter is a type parameter, and the second is
11903 a non-type parameter. We can tell by looking at the token after
11904 the identifier -- if it is a `,', `=', or `>' then we have a type
11906 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11908 /* Peek at the token after `class' or `typename'. */
11909 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11910 /* If it's an ellipsis, we have a template type parameter
11912 if (token->type == CPP_ELLIPSIS)
11913 return cp_parser_type_parameter (parser, is_parameter_pack);
11914 /* If it's an identifier, skip it. */
11915 if (token->type == CPP_NAME)
11916 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11917 /* Now, see if the token looks like the end of a template
11919 if (token->type == CPP_COMMA
11920 || token->type == CPP_EQ
11921 || token->type == CPP_GREATER)
11922 return cp_parser_type_parameter (parser, is_parameter_pack);
11925 /* Otherwise, it is a non-type parameter.
11929 When parsing a default template-argument for a non-type
11930 template-parameter, the first non-nested `>' is taken as the end
11931 of the template parameter-list rather than a greater-than
11933 *is_non_type = true;
11934 parameter_declarator
11935 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11936 /*parenthesized_p=*/NULL);
11938 /* If the parameter declaration is marked as a parameter pack, set
11939 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11940 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11942 if (parameter_declarator
11943 && parameter_declarator->declarator
11944 && parameter_declarator->declarator->parameter_pack_p)
11946 *is_parameter_pack = true;
11947 parameter_declarator->declarator->parameter_pack_p = false;
11950 /* If the next token is an ellipsis, and we don't already have it
11951 marked as a parameter pack, then we have a parameter pack (that
11952 has no declarator). */
11953 if (!*is_parameter_pack
11954 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11955 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11957 /* Consume the `...'. */
11958 cp_lexer_consume_token (parser->lexer);
11959 maybe_warn_variadic_templates ();
11961 *is_parameter_pack = true;
11963 /* We might end up with a pack expansion as the type of the non-type
11964 template parameter, in which case this is a non-type template
11966 else if (parameter_declarator
11967 && parameter_declarator->decl_specifiers.type
11968 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11970 *is_parameter_pack = true;
11971 parameter_declarator->decl_specifiers.type =
11972 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11975 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11977 /* Parameter packs cannot have default arguments. However, a
11978 user may try to do so, so we'll parse them and give an
11979 appropriate diagnostic here. */
11981 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11983 /* Find the name of the parameter pack. */
11984 id_declarator = parameter_declarator->declarator;
11985 while (id_declarator && id_declarator->kind != cdk_id)
11986 id_declarator = id_declarator->declarator;
11988 if (id_declarator && id_declarator->kind == cdk_id)
11989 error_at (start_token->location,
11990 "template parameter pack %qD cannot have a default argument",
11991 id_declarator->u.id.unqualified_name);
11993 error_at (start_token->location,
11994 "template parameter pack cannot have a default argument");
11996 /* Parse the default argument, but throw away the result. */
11997 cp_parser_default_argument (parser, /*template_parm_p=*/true);
12000 parm = grokdeclarator (parameter_declarator->declarator,
12001 ¶meter_declarator->decl_specifiers,
12002 TPARM, /*initialized=*/0,
12003 /*attrlist=*/NULL);
12004 if (parm == error_mark_node)
12005 return error_mark_node;
12007 return build_tree_list (parameter_declarator->default_argument, parm);
12010 /* Parse a type-parameter.
12013 class identifier [opt]
12014 class identifier [opt] = type-id
12015 typename identifier [opt]
12016 typename identifier [opt] = type-id
12017 template < template-parameter-list > class identifier [opt]
12018 template < template-parameter-list > class identifier [opt]
12021 GNU Extension (variadic templates):
12024 class ... identifier [opt]
12025 typename ... identifier [opt]
12027 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
12028 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
12029 the declaration of the parameter.
12031 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12034 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12039 /* Look for a keyword to tell us what kind of parameter this is. */
12040 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12042 return error_mark_node;
12044 switch (token->keyword)
12050 tree default_argument;
12052 /* If the next token is an ellipsis, we have a template
12054 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12056 /* Consume the `...' token. */
12057 cp_lexer_consume_token (parser->lexer);
12058 maybe_warn_variadic_templates ();
12060 *is_parameter_pack = true;
12063 /* If the next token is an identifier, then it names the
12065 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12066 identifier = cp_parser_identifier (parser);
12068 identifier = NULL_TREE;
12070 /* Create the parameter. */
12071 parameter = finish_template_type_parm (class_type_node, identifier);
12073 /* If the next token is an `=', we have a default argument. */
12074 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12076 /* Consume the `=' token. */
12077 cp_lexer_consume_token (parser->lexer);
12078 /* Parse the default-argument. */
12079 push_deferring_access_checks (dk_no_deferred);
12080 default_argument = cp_parser_type_id (parser);
12082 /* Template parameter packs cannot have default
12084 if (*is_parameter_pack)
12087 error_at (token->location,
12088 "template parameter pack %qD cannot have a "
12089 "default argument", identifier);
12091 error_at (token->location,
12092 "template parameter packs cannot have "
12093 "default arguments");
12094 default_argument = NULL_TREE;
12096 pop_deferring_access_checks ();
12099 default_argument = NULL_TREE;
12101 /* Create the combined representation of the parameter and the
12102 default argument. */
12103 parameter = build_tree_list (default_argument, parameter);
12110 tree default_argument;
12112 /* Look for the `<'. */
12113 cp_parser_require (parser, CPP_LESS, RT_LESS);
12114 /* Parse the template-parameter-list. */
12115 cp_parser_template_parameter_list (parser);
12116 /* Look for the `>'. */
12117 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12118 /* Look for the `class' keyword. */
12119 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12120 /* If the next token is an ellipsis, we have a template
12122 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12124 /* Consume the `...' token. */
12125 cp_lexer_consume_token (parser->lexer);
12126 maybe_warn_variadic_templates ();
12128 *is_parameter_pack = true;
12130 /* If the next token is an `=', then there is a
12131 default-argument. If the next token is a `>', we are at
12132 the end of the parameter-list. If the next token is a `,',
12133 then we are at the end of this parameter. */
12134 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12135 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12136 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12138 identifier = cp_parser_identifier (parser);
12139 /* Treat invalid names as if the parameter were nameless. */
12140 if (identifier == error_mark_node)
12141 identifier = NULL_TREE;
12144 identifier = NULL_TREE;
12146 /* Create the template parameter. */
12147 parameter = finish_template_template_parm (class_type_node,
12150 /* If the next token is an `=', then there is a
12151 default-argument. */
12152 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12156 /* Consume the `='. */
12157 cp_lexer_consume_token (parser->lexer);
12158 /* Parse the id-expression. */
12159 push_deferring_access_checks (dk_no_deferred);
12160 /* save token before parsing the id-expression, for error
12162 token = cp_lexer_peek_token (parser->lexer);
12164 = cp_parser_id_expression (parser,
12165 /*template_keyword_p=*/false,
12166 /*check_dependency_p=*/true,
12167 /*template_p=*/&is_template,
12168 /*declarator_p=*/false,
12169 /*optional_p=*/false);
12170 if (TREE_CODE (default_argument) == TYPE_DECL)
12171 /* If the id-expression was a template-id that refers to
12172 a template-class, we already have the declaration here,
12173 so no further lookup is needed. */
12176 /* Look up the name. */
12178 = cp_parser_lookup_name (parser, default_argument,
12180 /*is_template=*/is_template,
12181 /*is_namespace=*/false,
12182 /*check_dependency=*/true,
12183 /*ambiguous_decls=*/NULL,
12185 /* See if the default argument is valid. */
12187 = check_template_template_default_arg (default_argument);
12189 /* Template parameter packs cannot have default
12191 if (*is_parameter_pack)
12194 error_at (token->location,
12195 "template parameter pack %qD cannot "
12196 "have a default argument",
12199 error_at (token->location, "template parameter packs cannot "
12200 "have default arguments");
12201 default_argument = NULL_TREE;
12203 pop_deferring_access_checks ();
12206 default_argument = NULL_TREE;
12208 /* Create the combined representation of the parameter and the
12209 default argument. */
12210 parameter = build_tree_list (default_argument, parameter);
12215 gcc_unreachable ();
12222 /* Parse a template-id.
12225 template-name < template-argument-list [opt] >
12227 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12228 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
12229 returned. Otherwise, if the template-name names a function, or set
12230 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
12231 names a class, returns a TYPE_DECL for the specialization.
12233 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12234 uninstantiated templates. */
12237 cp_parser_template_id (cp_parser *parser,
12238 bool template_keyword_p,
12239 bool check_dependency_p,
12240 bool is_declaration)
12246 cp_token_position start_of_id = 0;
12247 deferred_access_check *chk;
12248 VEC (deferred_access_check,gc) *access_check;
12249 cp_token *next_token = NULL, *next_token_2 = NULL;
12250 bool is_identifier;
12252 /* If the next token corresponds to a template-id, there is no need
12254 next_token = cp_lexer_peek_token (parser->lexer);
12255 if (next_token->type == CPP_TEMPLATE_ID)
12257 struct tree_check *check_value;
12259 /* Get the stored value. */
12260 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12261 /* Perform any access checks that were deferred. */
12262 access_check = check_value->checks;
12265 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12266 perform_or_defer_access_check (chk->binfo,
12270 /* Return the stored value. */
12271 return check_value->value;
12274 /* Avoid performing name lookup if there is no possibility of
12275 finding a template-id. */
12276 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12277 || (next_token->type == CPP_NAME
12278 && !cp_parser_nth_token_starts_template_argument_list_p
12281 cp_parser_error (parser, "expected template-id");
12282 return error_mark_node;
12285 /* Remember where the template-id starts. */
12286 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12287 start_of_id = cp_lexer_token_position (parser->lexer, false);
12289 push_deferring_access_checks (dk_deferred);
12291 /* Parse the template-name. */
12292 is_identifier = false;
12293 templ = cp_parser_template_name (parser, template_keyword_p,
12294 check_dependency_p,
12297 if (templ == error_mark_node || is_identifier)
12299 pop_deferring_access_checks ();
12303 /* If we find the sequence `[:' after a template-name, it's probably
12304 a digraph-typo for `< ::'. Substitute the tokens and check if we can
12305 parse correctly the argument list. */
12306 next_token = cp_lexer_peek_token (parser->lexer);
12307 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12308 if (next_token->type == CPP_OPEN_SQUARE
12309 && next_token->flags & DIGRAPH
12310 && next_token_2->type == CPP_COLON
12311 && !(next_token_2->flags & PREV_WHITE))
12313 cp_parser_parse_tentatively (parser);
12314 /* Change `:' into `::'. */
12315 next_token_2->type = CPP_SCOPE;
12316 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12318 cp_lexer_consume_token (parser->lexer);
12320 /* Parse the arguments. */
12321 arguments = cp_parser_enclosed_template_argument_list (parser);
12322 if (!cp_parser_parse_definitely (parser))
12324 /* If we couldn't parse an argument list, then we revert our changes
12325 and return simply an error. Maybe this is not a template-id
12327 next_token_2->type = CPP_COLON;
12328 cp_parser_error (parser, "expected %<<%>");
12329 pop_deferring_access_checks ();
12330 return error_mark_node;
12332 /* Otherwise, emit an error about the invalid digraph, but continue
12333 parsing because we got our argument list. */
12334 if (permerror (next_token->location,
12335 "%<<::%> cannot begin a template-argument list"))
12337 static bool hint = false;
12338 inform (next_token->location,
12339 "%<<:%> is an alternate spelling for %<[%>."
12340 " Insert whitespace between %<<%> and %<::%>");
12341 if (!hint && !flag_permissive)
12343 inform (next_token->location, "(if you use %<-fpermissive%>"
12344 " G++ will accept your code)");
12351 /* Look for the `<' that starts the template-argument-list. */
12352 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12354 pop_deferring_access_checks ();
12355 return error_mark_node;
12357 /* Parse the arguments. */
12358 arguments = cp_parser_enclosed_template_argument_list (parser);
12361 /* Build a representation of the specialization. */
12362 if (TREE_CODE (templ) == IDENTIFIER_NODE)
12363 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12364 else if (DECL_TYPE_TEMPLATE_P (templ)
12365 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12367 bool entering_scope;
12368 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12369 template (rather than some instantiation thereof) only if
12370 is not nested within some other construct. For example, in
12371 "template <typename T> void f(T) { A<T>::", A<T> is just an
12372 instantiation of A. */
12373 entering_scope = (template_parm_scope_p ()
12374 && cp_lexer_next_token_is (parser->lexer,
12377 = finish_template_type (templ, arguments, entering_scope);
12381 /* If it's not a class-template or a template-template, it should be
12382 a function-template. */
12383 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12384 || TREE_CODE (templ) == OVERLOAD
12385 || BASELINK_P (templ)));
12387 template_id = lookup_template_function (templ, arguments);
12390 /* If parsing tentatively, replace the sequence of tokens that makes
12391 up the template-id with a CPP_TEMPLATE_ID token. That way,
12392 should we re-parse the token stream, we will not have to repeat
12393 the effort required to do the parse, nor will we issue duplicate
12394 error messages about problems during instantiation of the
12398 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12400 /* Reset the contents of the START_OF_ID token. */
12401 token->type = CPP_TEMPLATE_ID;
12402 /* Retrieve any deferred checks. Do not pop this access checks yet
12403 so the memory will not be reclaimed during token replacing below. */
12404 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12405 token->u.tree_check_value->value = template_id;
12406 token->u.tree_check_value->checks = get_deferred_access_checks ();
12407 token->keyword = RID_MAX;
12409 /* Purge all subsequent tokens. */
12410 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12412 /* ??? Can we actually assume that, if template_id ==
12413 error_mark_node, we will have issued a diagnostic to the
12414 user, as opposed to simply marking the tentative parse as
12416 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12417 error_at (token->location, "parse error in template argument list");
12420 pop_deferring_access_checks ();
12421 return template_id;
12424 /* Parse a template-name.
12429 The standard should actually say:
12433 operator-function-id
12435 A defect report has been filed about this issue.
12437 A conversion-function-id cannot be a template name because they cannot
12438 be part of a template-id. In fact, looking at this code:
12440 a.operator K<int>()
12442 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12443 It is impossible to call a templated conversion-function-id with an
12444 explicit argument list, since the only allowed template parameter is
12445 the type to which it is converting.
12447 If TEMPLATE_KEYWORD_P is true, then we have just seen the
12448 `template' keyword, in a construction like:
12452 In that case `f' is taken to be a template-name, even though there
12453 is no way of knowing for sure.
12455 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12456 name refers to a set of overloaded functions, at least one of which
12457 is a template, or an IDENTIFIER_NODE with the name of the template,
12458 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
12459 names are looked up inside uninstantiated templates. */
12462 cp_parser_template_name (cp_parser* parser,
12463 bool template_keyword_p,
12464 bool check_dependency_p,
12465 bool is_declaration,
12466 bool *is_identifier)
12471 cp_token *token = cp_lexer_peek_token (parser->lexer);
12473 /* If the next token is `operator', then we have either an
12474 operator-function-id or a conversion-function-id. */
12475 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12477 /* We don't know whether we're looking at an
12478 operator-function-id or a conversion-function-id. */
12479 cp_parser_parse_tentatively (parser);
12480 /* Try an operator-function-id. */
12481 identifier = cp_parser_operator_function_id (parser);
12482 /* If that didn't work, try a conversion-function-id. */
12483 if (!cp_parser_parse_definitely (parser))
12485 cp_parser_error (parser, "expected template-name");
12486 return error_mark_node;
12489 /* Look for the identifier. */
12491 identifier = cp_parser_identifier (parser);
12493 /* If we didn't find an identifier, we don't have a template-id. */
12494 if (identifier == error_mark_node)
12495 return error_mark_node;
12497 /* If the name immediately followed the `template' keyword, then it
12498 is a template-name. However, if the next token is not `<', then
12499 we do not treat it as a template-name, since it is not being used
12500 as part of a template-id. This enables us to handle constructs
12503 template <typename T> struct S { S(); };
12504 template <typename T> S<T>::S();
12506 correctly. We would treat `S' as a template -- if it were `S<T>'
12507 -- but we do not if there is no `<'. */
12509 if (processing_template_decl
12510 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12512 /* In a declaration, in a dependent context, we pretend that the
12513 "template" keyword was present in order to improve error
12514 recovery. For example, given:
12516 template <typename T> void f(T::X<int>);
12518 we want to treat "X<int>" as a template-id. */
12520 && !template_keyword_p
12521 && parser->scope && TYPE_P (parser->scope)
12522 && check_dependency_p
12523 && dependent_scope_p (parser->scope)
12524 /* Do not do this for dtors (or ctors), since they never
12525 need the template keyword before their name. */
12526 && !constructor_name_p (identifier, parser->scope))
12528 cp_token_position start = 0;
12530 /* Explain what went wrong. */
12531 error_at (token->location, "non-template %qD used as template",
12533 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12534 parser->scope, identifier);
12535 /* If parsing tentatively, find the location of the "<" token. */
12536 if (cp_parser_simulate_error (parser))
12537 start = cp_lexer_token_position (parser->lexer, true);
12538 /* Parse the template arguments so that we can issue error
12539 messages about them. */
12540 cp_lexer_consume_token (parser->lexer);
12541 cp_parser_enclosed_template_argument_list (parser);
12542 /* Skip tokens until we find a good place from which to
12543 continue parsing. */
12544 cp_parser_skip_to_closing_parenthesis (parser,
12545 /*recovering=*/true,
12547 /*consume_paren=*/false);
12548 /* If parsing tentatively, permanently remove the
12549 template argument list. That will prevent duplicate
12550 error messages from being issued about the missing
12551 "template" keyword. */
12553 cp_lexer_purge_tokens_after (parser->lexer, start);
12555 *is_identifier = true;
12559 /* If the "template" keyword is present, then there is generally
12560 no point in doing name-lookup, so we just return IDENTIFIER.
12561 But, if the qualifying scope is non-dependent then we can
12562 (and must) do name-lookup normally. */
12563 if (template_keyword_p
12565 || (TYPE_P (parser->scope)
12566 && dependent_type_p (parser->scope))))
12570 /* Look up the name. */
12571 decl = cp_parser_lookup_name (parser, identifier,
12573 /*is_template=*/true,
12574 /*is_namespace=*/false,
12575 check_dependency_p,
12576 /*ambiguous_decls=*/NULL,
12579 /* If DECL is a template, then the name was a template-name. */
12580 if (TREE_CODE (decl) == TEMPLATE_DECL)
12584 tree fn = NULL_TREE;
12586 /* The standard does not explicitly indicate whether a name that
12587 names a set of overloaded declarations, some of which are
12588 templates, is a template-name. However, such a name should
12589 be a template-name; otherwise, there is no way to form a
12590 template-id for the overloaded templates. */
12591 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12592 if (TREE_CODE (fns) == OVERLOAD)
12593 for (fn = fns; fn; fn = OVL_NEXT (fn))
12594 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12599 /* The name does not name a template. */
12600 cp_parser_error (parser, "expected template-name");
12601 return error_mark_node;
12605 /* If DECL is dependent, and refers to a function, then just return
12606 its name; we will look it up again during template instantiation. */
12607 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12609 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12610 if (TYPE_P (scope) && dependent_type_p (scope))
12617 /* Parse a template-argument-list.
12619 template-argument-list:
12620 template-argument ... [opt]
12621 template-argument-list , template-argument ... [opt]
12623 Returns a TREE_VEC containing the arguments. */
12626 cp_parser_template_argument_list (cp_parser* parser)
12628 tree fixed_args[10];
12629 unsigned n_args = 0;
12630 unsigned alloced = 10;
12631 tree *arg_ary = fixed_args;
12633 bool saved_in_template_argument_list_p;
12635 bool saved_non_ice_p;
12637 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12638 parser->in_template_argument_list_p = true;
12639 /* Even if the template-id appears in an integral
12640 constant-expression, the contents of the argument list do
12642 saved_ice_p = parser->integral_constant_expression_p;
12643 parser->integral_constant_expression_p = false;
12644 saved_non_ice_p = parser->non_integral_constant_expression_p;
12645 parser->non_integral_constant_expression_p = false;
12647 /* Parse the arguments. */
12653 /* Consume the comma. */
12654 cp_lexer_consume_token (parser->lexer);
12656 /* Parse the template-argument. */
12657 argument = cp_parser_template_argument (parser);
12659 /* If the next token is an ellipsis, we're expanding a template
12661 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12663 if (argument == error_mark_node)
12665 cp_token *token = cp_lexer_peek_token (parser->lexer);
12666 error_at (token->location,
12667 "expected parameter pack before %<...%>");
12669 /* Consume the `...' token. */
12670 cp_lexer_consume_token (parser->lexer);
12672 /* Make the argument into a TYPE_PACK_EXPANSION or
12673 EXPR_PACK_EXPANSION. */
12674 argument = make_pack_expansion (argument);
12677 if (n_args == alloced)
12681 if (arg_ary == fixed_args)
12683 arg_ary = XNEWVEC (tree, alloced);
12684 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12687 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12689 arg_ary[n_args++] = argument;
12691 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12693 vec = make_tree_vec (n_args);
12696 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12698 if (arg_ary != fixed_args)
12700 parser->non_integral_constant_expression_p = saved_non_ice_p;
12701 parser->integral_constant_expression_p = saved_ice_p;
12702 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12703 #ifdef ENABLE_CHECKING
12704 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12709 /* Parse a template-argument.
12712 assignment-expression
12716 The representation is that of an assignment-expression, type-id, or
12717 id-expression -- except that the qualified id-expression is
12718 evaluated, so that the value returned is either a DECL or an
12721 Although the standard says "assignment-expression", it forbids
12722 throw-expressions or assignments in the template argument.
12723 Therefore, we use "conditional-expression" instead. */
12726 cp_parser_template_argument (cp_parser* parser)
12731 bool maybe_type_id = false;
12732 cp_token *token = NULL, *argument_start_token = NULL;
12735 /* There's really no way to know what we're looking at, so we just
12736 try each alternative in order.
12740 In a template-argument, an ambiguity between a type-id and an
12741 expression is resolved to a type-id, regardless of the form of
12742 the corresponding template-parameter.
12744 Therefore, we try a type-id first. */
12745 cp_parser_parse_tentatively (parser);
12746 argument = cp_parser_template_type_arg (parser);
12747 /* If there was no error parsing the type-id but the next token is a
12748 '>>', our behavior depends on which dialect of C++ we're
12749 parsing. In C++98, we probably found a typo for '> >'. But there
12750 are type-id which are also valid expressions. For instance:
12752 struct X { int operator >> (int); };
12753 template <int V> struct Foo {};
12756 Here 'X()' is a valid type-id of a function type, but the user just
12757 wanted to write the expression "X() >> 5". Thus, we remember that we
12758 found a valid type-id, but we still try to parse the argument as an
12759 expression to see what happens.
12761 In C++0x, the '>>' will be considered two separate '>'
12763 if (!cp_parser_error_occurred (parser)
12764 && cxx_dialect == cxx98
12765 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12767 maybe_type_id = true;
12768 cp_parser_abort_tentative_parse (parser);
12772 /* If the next token isn't a `,' or a `>', then this argument wasn't
12773 really finished. This means that the argument is not a valid
12775 if (!cp_parser_next_token_ends_template_argument_p (parser))
12776 cp_parser_error (parser, "expected template-argument");
12777 /* If that worked, we're done. */
12778 if (cp_parser_parse_definitely (parser))
12781 /* We're still not sure what the argument will be. */
12782 cp_parser_parse_tentatively (parser);
12783 /* Try a template. */
12784 argument_start_token = cp_lexer_peek_token (parser->lexer);
12785 argument = cp_parser_id_expression (parser,
12786 /*template_keyword_p=*/false,
12787 /*check_dependency_p=*/true,
12789 /*declarator_p=*/false,
12790 /*optional_p=*/false);
12791 /* If the next token isn't a `,' or a `>', then this argument wasn't
12792 really finished. */
12793 if (!cp_parser_next_token_ends_template_argument_p (parser))
12794 cp_parser_error (parser, "expected template-argument");
12795 if (!cp_parser_error_occurred (parser))
12797 /* Figure out what is being referred to. If the id-expression
12798 was for a class template specialization, then we will have a
12799 TYPE_DECL at this point. There is no need to do name lookup
12800 at this point in that case. */
12801 if (TREE_CODE (argument) != TYPE_DECL)
12802 argument = cp_parser_lookup_name (parser, argument,
12804 /*is_template=*/template_p,
12805 /*is_namespace=*/false,
12806 /*check_dependency=*/true,
12807 /*ambiguous_decls=*/NULL,
12808 argument_start_token->location);
12809 if (TREE_CODE (argument) != TEMPLATE_DECL
12810 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12811 cp_parser_error (parser, "expected template-name");
12813 if (cp_parser_parse_definitely (parser))
12815 /* It must be a non-type argument. There permitted cases are given
12816 in [temp.arg.nontype]:
12818 -- an integral constant-expression of integral or enumeration
12821 -- the name of a non-type template-parameter; or
12823 -- the name of an object or function with external linkage...
12825 -- the address of an object or function with external linkage...
12827 -- a pointer to member... */
12828 /* Look for a non-type template parameter. */
12829 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12831 cp_parser_parse_tentatively (parser);
12832 argument = cp_parser_primary_expression (parser,
12833 /*address_p=*/false,
12835 /*template_arg_p=*/true,
12837 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12838 || !cp_parser_next_token_ends_template_argument_p (parser))
12839 cp_parser_simulate_error (parser);
12840 if (cp_parser_parse_definitely (parser))
12844 /* If the next token is "&", the argument must be the address of an
12845 object or function with external linkage. */
12846 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12848 cp_lexer_consume_token (parser->lexer);
12849 /* See if we might have an id-expression. */
12850 token = cp_lexer_peek_token (parser->lexer);
12851 if (token->type == CPP_NAME
12852 || token->keyword == RID_OPERATOR
12853 || token->type == CPP_SCOPE
12854 || token->type == CPP_TEMPLATE_ID
12855 || token->type == CPP_NESTED_NAME_SPECIFIER)
12857 cp_parser_parse_tentatively (parser);
12858 argument = cp_parser_primary_expression (parser,
12861 /*template_arg_p=*/true,
12863 if (cp_parser_error_occurred (parser)
12864 || !cp_parser_next_token_ends_template_argument_p (parser))
12865 cp_parser_abort_tentative_parse (parser);
12870 if (TREE_CODE (argument) == INDIRECT_REF)
12872 gcc_assert (REFERENCE_REF_P (argument));
12873 argument = TREE_OPERAND (argument, 0);
12876 /* If we're in a template, we represent a qualified-id referring
12877 to a static data member as a SCOPE_REF even if the scope isn't
12878 dependent so that we can check access control later. */
12880 if (TREE_CODE (probe) == SCOPE_REF)
12881 probe = TREE_OPERAND (probe, 1);
12882 if (TREE_CODE (probe) == VAR_DECL)
12884 /* A variable without external linkage might still be a
12885 valid constant-expression, so no error is issued here
12886 if the external-linkage check fails. */
12887 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12888 cp_parser_simulate_error (parser);
12890 else if (is_overloaded_fn (argument))
12891 /* All overloaded functions are allowed; if the external
12892 linkage test does not pass, an error will be issued
12896 && (TREE_CODE (argument) == OFFSET_REF
12897 || TREE_CODE (argument) == SCOPE_REF))
12898 /* A pointer-to-member. */
12900 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12903 cp_parser_simulate_error (parser);
12905 if (cp_parser_parse_definitely (parser))
12908 argument = build_x_unary_op (ADDR_EXPR, argument,
12909 tf_warning_or_error);
12914 /* If the argument started with "&", there are no other valid
12915 alternatives at this point. */
12918 cp_parser_error (parser, "invalid non-type template argument");
12919 return error_mark_node;
12922 /* If the argument wasn't successfully parsed as a type-id followed
12923 by '>>', the argument can only be a constant expression now.
12924 Otherwise, we try parsing the constant-expression tentatively,
12925 because the argument could really be a type-id. */
12927 cp_parser_parse_tentatively (parser);
12928 argument = cp_parser_constant_expression (parser,
12929 /*allow_non_constant_p=*/false,
12930 /*non_constant_p=*/NULL);
12931 argument = fold_non_dependent_expr (argument);
12932 if (!maybe_type_id)
12934 if (!cp_parser_next_token_ends_template_argument_p (parser))
12935 cp_parser_error (parser, "expected template-argument");
12936 if (cp_parser_parse_definitely (parser))
12938 /* We did our best to parse the argument as a non type-id, but that
12939 was the only alternative that matched (albeit with a '>' after
12940 it). We can assume it's just a typo from the user, and a
12941 diagnostic will then be issued. */
12942 return cp_parser_template_type_arg (parser);
12945 /* Parse an explicit-instantiation.
12947 explicit-instantiation:
12948 template declaration
12950 Although the standard says `declaration', what it really means is:
12952 explicit-instantiation:
12953 template decl-specifier-seq [opt] declarator [opt] ;
12955 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12956 supposed to be allowed. A defect report has been filed about this
12961 explicit-instantiation:
12962 storage-class-specifier template
12963 decl-specifier-seq [opt] declarator [opt] ;
12964 function-specifier template
12965 decl-specifier-seq [opt] declarator [opt] ; */
12968 cp_parser_explicit_instantiation (cp_parser* parser)
12970 int declares_class_or_enum;
12971 cp_decl_specifier_seq decl_specifiers;
12972 tree extension_specifier = NULL_TREE;
12974 timevar_push (TV_TEMPLATE_INST);
12976 /* Look for an (optional) storage-class-specifier or
12977 function-specifier. */
12978 if (cp_parser_allow_gnu_extensions_p (parser))
12980 extension_specifier
12981 = cp_parser_storage_class_specifier_opt (parser);
12982 if (!extension_specifier)
12983 extension_specifier
12984 = cp_parser_function_specifier_opt (parser,
12985 /*decl_specs=*/NULL);
12988 /* Look for the `template' keyword. */
12989 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12990 /* Let the front end know that we are processing an explicit
12992 begin_explicit_instantiation ();
12993 /* [temp.explicit] says that we are supposed to ignore access
12994 control while processing explicit instantiation directives. */
12995 push_deferring_access_checks (dk_no_check);
12996 /* Parse a decl-specifier-seq. */
12997 cp_parser_decl_specifier_seq (parser,
12998 CP_PARSER_FLAGS_OPTIONAL,
13000 &declares_class_or_enum);
13001 /* If there was exactly one decl-specifier, and it declared a class,
13002 and there's no declarator, then we have an explicit type
13004 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13008 type = check_tag_decl (&decl_specifiers);
13009 /* Turn access control back on for names used during
13010 template instantiation. */
13011 pop_deferring_access_checks ();
13013 do_type_instantiation (type, extension_specifier,
13014 /*complain=*/tf_error);
13018 cp_declarator *declarator;
13021 /* Parse the declarator. */
13023 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13024 /*ctor_dtor_or_conv_p=*/NULL,
13025 /*parenthesized_p=*/NULL,
13026 /*member_p=*/false);
13027 if (declares_class_or_enum & 2)
13028 cp_parser_check_for_definition_in_return_type (declarator,
13029 decl_specifiers.type,
13030 decl_specifiers.type_location);
13031 if (declarator != cp_error_declarator)
13033 if (decl_specifiers.specs[(int)ds_inline])
13034 permerror (input_location, "explicit instantiation shall not use"
13035 " %<inline%> specifier");
13036 if (decl_specifiers.specs[(int)ds_constexpr])
13037 permerror (input_location, "explicit instantiation shall not use"
13038 " %<constexpr%> specifier");
13040 decl = grokdeclarator (declarator, &decl_specifiers,
13041 NORMAL, 0, &decl_specifiers.attributes);
13042 /* Turn access control back on for names used during
13043 template instantiation. */
13044 pop_deferring_access_checks ();
13045 /* Do the explicit instantiation. */
13046 do_decl_instantiation (decl, extension_specifier);
13050 pop_deferring_access_checks ();
13051 /* Skip the body of the explicit instantiation. */
13052 cp_parser_skip_to_end_of_statement (parser);
13055 /* We're done with the instantiation. */
13056 end_explicit_instantiation ();
13058 cp_parser_consume_semicolon_at_end_of_statement (parser);
13060 timevar_pop (TV_TEMPLATE_INST);
13063 /* Parse an explicit-specialization.
13065 explicit-specialization:
13066 template < > declaration
13068 Although the standard says `declaration', what it really means is:
13070 explicit-specialization:
13071 template <> decl-specifier [opt] init-declarator [opt] ;
13072 template <> function-definition
13073 template <> explicit-specialization
13074 template <> template-declaration */
13077 cp_parser_explicit_specialization (cp_parser* parser)
13079 bool need_lang_pop;
13080 cp_token *token = cp_lexer_peek_token (parser->lexer);
13082 /* Look for the `template' keyword. */
13083 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13084 /* Look for the `<'. */
13085 cp_parser_require (parser, CPP_LESS, RT_LESS);
13086 /* Look for the `>'. */
13087 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13088 /* We have processed another parameter list. */
13089 ++parser->num_template_parameter_lists;
13092 A template ... explicit specialization ... shall not have C
13094 if (current_lang_name == lang_name_c)
13096 error_at (token->location, "template specialization with C linkage");
13097 /* Give it C++ linkage to avoid confusing other parts of the
13099 push_lang_context (lang_name_cplusplus);
13100 need_lang_pop = true;
13103 need_lang_pop = false;
13104 /* Let the front end know that we are beginning a specialization. */
13105 if (!begin_specialization ())
13107 end_specialization ();
13111 /* If the next keyword is `template', we need to figure out whether
13112 or not we're looking a template-declaration. */
13113 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13115 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13116 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13117 cp_parser_template_declaration_after_export (parser,
13118 /*member_p=*/false);
13120 cp_parser_explicit_specialization (parser);
13123 /* Parse the dependent declaration. */
13124 cp_parser_single_declaration (parser,
13126 /*member_p=*/false,
13127 /*explicit_specialization_p=*/true,
13128 /*friend_p=*/NULL);
13129 /* We're done with the specialization. */
13130 end_specialization ();
13131 /* For the erroneous case of a template with C linkage, we pushed an
13132 implicit C++ linkage scope; exit that scope now. */
13134 pop_lang_context ();
13135 /* We're done with this parameter list. */
13136 --parser->num_template_parameter_lists;
13139 /* Parse a type-specifier.
13142 simple-type-specifier
13145 elaborated-type-specifier
13153 Returns a representation of the type-specifier. For a
13154 class-specifier, enum-specifier, or elaborated-type-specifier, a
13155 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13157 The parser flags FLAGS is used to control type-specifier parsing.
13159 If IS_DECLARATION is TRUE, then this type-specifier is appearing
13160 in a decl-specifier-seq.
13162 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13163 class-specifier, enum-specifier, or elaborated-type-specifier, then
13164 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
13165 if a type is declared; 2 if it is defined. Otherwise, it is set to
13168 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13169 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
13170 is set to FALSE. */
13173 cp_parser_type_specifier (cp_parser* parser,
13174 cp_parser_flags flags,
13175 cp_decl_specifier_seq *decl_specs,
13176 bool is_declaration,
13177 int* declares_class_or_enum,
13178 bool* is_cv_qualifier)
13180 tree type_spec = NULL_TREE;
13183 cp_decl_spec ds = ds_last;
13185 /* Assume this type-specifier does not declare a new type. */
13186 if (declares_class_or_enum)
13187 *declares_class_or_enum = 0;
13188 /* And that it does not specify a cv-qualifier. */
13189 if (is_cv_qualifier)
13190 *is_cv_qualifier = false;
13191 /* Peek at the next token. */
13192 token = cp_lexer_peek_token (parser->lexer);
13194 /* If we're looking at a keyword, we can use that to guide the
13195 production we choose. */
13196 keyword = token->keyword;
13200 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13201 goto elaborated_type_specifier;
13203 /* Look for the enum-specifier. */
13204 type_spec = cp_parser_enum_specifier (parser);
13205 /* If that worked, we're done. */
13208 if (declares_class_or_enum)
13209 *declares_class_or_enum = 2;
13211 cp_parser_set_decl_spec_type (decl_specs,
13214 /*type_definition_p=*/true);
13218 goto elaborated_type_specifier;
13220 /* Any of these indicate either a class-specifier, or an
13221 elaborated-type-specifier. */
13225 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13226 goto elaborated_type_specifier;
13228 /* Parse tentatively so that we can back up if we don't find a
13229 class-specifier. */
13230 cp_parser_parse_tentatively (parser);
13231 /* Look for the class-specifier. */
13232 type_spec = cp_parser_class_specifier (parser);
13233 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13234 /* If that worked, we're done. */
13235 if (cp_parser_parse_definitely (parser))
13237 if (declares_class_or_enum)
13238 *declares_class_or_enum = 2;
13240 cp_parser_set_decl_spec_type (decl_specs,
13243 /*type_definition_p=*/true);
13247 /* Fall through. */
13248 elaborated_type_specifier:
13249 /* We're declaring (not defining) a class or enum. */
13250 if (declares_class_or_enum)
13251 *declares_class_or_enum = 1;
13253 /* Fall through. */
13255 /* Look for an elaborated-type-specifier. */
13257 = (cp_parser_elaborated_type_specifier
13259 decl_specs && decl_specs->specs[(int) ds_friend],
13262 cp_parser_set_decl_spec_type (decl_specs,
13265 /*type_definition_p=*/false);
13270 if (is_cv_qualifier)
13271 *is_cv_qualifier = true;
13276 if (is_cv_qualifier)
13277 *is_cv_qualifier = true;
13282 if (is_cv_qualifier)
13283 *is_cv_qualifier = true;
13287 /* The `__complex__' keyword is a GNU extension. */
13295 /* Handle simple keywords. */
13300 ++decl_specs->specs[(int)ds];
13301 decl_specs->any_specifiers_p = true;
13303 return cp_lexer_consume_token (parser->lexer)->u.value;
13306 /* If we do not already have a type-specifier, assume we are looking
13307 at a simple-type-specifier. */
13308 type_spec = cp_parser_simple_type_specifier (parser,
13312 /* If we didn't find a type-specifier, and a type-specifier was not
13313 optional in this context, issue an error message. */
13314 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13316 cp_parser_error (parser, "expected type specifier");
13317 return error_mark_node;
13323 /* Parse a simple-type-specifier.
13325 simple-type-specifier:
13326 :: [opt] nested-name-specifier [opt] type-name
13327 :: [opt] nested-name-specifier template template-id
13342 simple-type-specifier:
13344 decltype ( expression )
13347 __underlying_type ( type-id )
13351 simple-type-specifier:
13353 __typeof__ unary-expression
13354 __typeof__ ( type-id )
13356 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
13357 appropriately updated. */
13360 cp_parser_simple_type_specifier (cp_parser* parser,
13361 cp_decl_specifier_seq *decl_specs,
13362 cp_parser_flags flags)
13364 tree type = NULL_TREE;
13367 /* Peek at the next token. */
13368 token = cp_lexer_peek_token (parser->lexer);
13370 /* If we're looking at a keyword, things are easy. */
13371 switch (token->keyword)
13375 decl_specs->explicit_char_p = true;
13376 type = char_type_node;
13379 type = char16_type_node;
13382 type = char32_type_node;
13385 type = wchar_type_node;
13388 type = boolean_type_node;
13392 ++decl_specs->specs[(int) ds_short];
13393 type = short_integer_type_node;
13397 decl_specs->explicit_int_p = true;
13398 type = integer_type_node;
13401 if (!int128_integer_type_node)
13404 decl_specs->explicit_int128_p = true;
13405 type = int128_integer_type_node;
13409 ++decl_specs->specs[(int) ds_long];
13410 type = long_integer_type_node;
13414 ++decl_specs->specs[(int) ds_signed];
13415 type = integer_type_node;
13419 ++decl_specs->specs[(int) ds_unsigned];
13420 type = unsigned_type_node;
13423 type = float_type_node;
13426 type = double_type_node;
13429 type = void_type_node;
13433 maybe_warn_cpp0x (CPP0X_AUTO);
13434 type = make_auto ();
13438 /* Since DR 743, decltype can either be a simple-type-specifier by
13439 itself or begin a nested-name-specifier. Parsing it will replace
13440 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13441 handling below decide what to do. */
13442 cp_parser_decltype (parser);
13443 cp_lexer_set_token_position (parser->lexer, token);
13447 /* Consume the `typeof' token. */
13448 cp_lexer_consume_token (parser->lexer);
13449 /* Parse the operand to `typeof'. */
13450 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13451 /* If it is not already a TYPE, take its type. */
13452 if (!TYPE_P (type))
13453 type = finish_typeof (type);
13456 cp_parser_set_decl_spec_type (decl_specs, type,
13458 /*type_definition_p=*/false);
13462 case RID_UNDERLYING_TYPE:
13463 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13465 cp_parser_set_decl_spec_type (decl_specs, type,
13467 /*type_definition_p=*/false);
13472 case RID_DIRECT_BASES:
13473 type = cp_parser_trait_expr (parser, token->keyword);
13475 cp_parser_set_decl_spec_type (decl_specs, type,
13477 /*type_definition_p=*/false);
13483 /* If token is an already-parsed decltype not followed by ::,
13484 it's a simple-type-specifier. */
13485 if (token->type == CPP_DECLTYPE
13486 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13488 type = token->u.value;
13490 cp_parser_set_decl_spec_type (decl_specs, type,
13492 /*type_definition_p=*/false);
13493 cp_lexer_consume_token (parser->lexer);
13497 /* If the type-specifier was for a built-in type, we're done. */
13500 /* Record the type. */
13502 && (token->keyword != RID_SIGNED
13503 && token->keyword != RID_UNSIGNED
13504 && token->keyword != RID_SHORT
13505 && token->keyword != RID_LONG))
13506 cp_parser_set_decl_spec_type (decl_specs,
13509 /*type_definition_p=*/false);
13511 decl_specs->any_specifiers_p = true;
13513 /* Consume the token. */
13514 cp_lexer_consume_token (parser->lexer);
13516 /* There is no valid C++ program where a non-template type is
13517 followed by a "<". That usually indicates that the user thought
13518 that the type was a template. */
13519 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13521 return TYPE_NAME (type);
13524 /* The type-specifier must be a user-defined type. */
13525 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13530 /* Don't gobble tokens or issue error messages if this is an
13531 optional type-specifier. */
13532 if (flags & CP_PARSER_FLAGS_OPTIONAL)
13533 cp_parser_parse_tentatively (parser);
13535 /* Look for the optional `::' operator. */
13537 = (cp_parser_global_scope_opt (parser,
13538 /*current_scope_valid_p=*/false)
13540 /* Look for the nested-name specifier. */
13542 = (cp_parser_nested_name_specifier_opt (parser,
13543 /*typename_keyword_p=*/false,
13544 /*check_dependency_p=*/true,
13546 /*is_declaration=*/false)
13548 token = cp_lexer_peek_token (parser->lexer);
13549 /* If we have seen a nested-name-specifier, and the next token
13550 is `template', then we are using the template-id production. */
13552 && cp_parser_optional_template_keyword (parser))
13554 /* Look for the template-id. */
13555 type = cp_parser_template_id (parser,
13556 /*template_keyword_p=*/true,
13557 /*check_dependency_p=*/true,
13558 /*is_declaration=*/false);
13559 /* If the template-id did not name a type, we are out of
13561 if (TREE_CODE (type) != TYPE_DECL)
13563 cp_parser_error (parser, "expected template-id for type");
13567 /* Otherwise, look for a type-name. */
13569 type = cp_parser_type_name (parser);
13570 /* Keep track of all name-lookups performed in class scopes. */
13574 && TREE_CODE (type) == TYPE_DECL
13575 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13576 maybe_note_name_used_in_class (DECL_NAME (type), type);
13577 /* If it didn't work out, we don't have a TYPE. */
13578 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13579 && !cp_parser_parse_definitely (parser))
13581 if (type && decl_specs)
13582 cp_parser_set_decl_spec_type (decl_specs, type,
13584 /*type_definition_p=*/false);
13587 /* If we didn't get a type-name, issue an error message. */
13588 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13590 cp_parser_error (parser, "expected type-name");
13591 return error_mark_node;
13594 if (type && type != error_mark_node)
13596 /* See if TYPE is an Objective-C type, and if so, parse and
13597 accept any protocol references following it. Do this before
13598 the cp_parser_check_for_invalid_template_id() call, because
13599 Objective-C types can be followed by '<...>' which would
13600 enclose protocol names rather than template arguments, and so
13601 everything is fine. */
13602 if (c_dialect_objc () && !parser->scope
13603 && (objc_is_id (type) || objc_is_class_name (type)))
13605 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13606 tree qual_type = objc_get_protocol_qualified_type (type, protos);
13608 /* Clobber the "unqualified" type previously entered into
13609 DECL_SPECS with the new, improved protocol-qualified version. */
13611 decl_specs->type = qual_type;
13616 /* There is no valid C++ program where a non-template type is
13617 followed by a "<". That usually indicates that the user
13618 thought that the type was a template. */
13619 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13626 /* Parse a type-name.
13632 simple-template-id [in c++0x]
13640 Returns a TYPE_DECL for the type. */
13643 cp_parser_type_name (cp_parser* parser)
13647 /* We can't know yet whether it is a class-name or not. */
13648 cp_parser_parse_tentatively (parser);
13649 /* Try a class-name. */
13650 type_decl = cp_parser_class_name (parser,
13651 /*typename_keyword_p=*/false,
13652 /*template_keyword_p=*/false,
13654 /*check_dependency_p=*/true,
13655 /*class_head_p=*/false,
13656 /*is_declaration=*/false);
13657 /* If it's not a class-name, keep looking. */
13658 if (!cp_parser_parse_definitely (parser))
13660 if (cxx_dialect < cxx0x)
13661 /* It must be a typedef-name or an enum-name. */
13662 return cp_parser_nonclass_name (parser);
13664 cp_parser_parse_tentatively (parser);
13665 /* It is either a simple-template-id representing an
13666 instantiation of an alias template... */
13667 type_decl = cp_parser_template_id (parser,
13668 /*template_keyword_p=*/false,
13669 /*check_dependency_p=*/false,
13670 /*is_declaration=*/false);
13671 /* Note that this must be an instantiation of an alias template
13672 because [temp.names]/6 says:
13674 A template-id that names an alias template specialization
13677 Whereas [temp.names]/7 says:
13679 A simple-template-id that names a class template
13680 specialization is a class-name. */
13681 if (type_decl != NULL_TREE
13682 && TREE_CODE (type_decl) == TYPE_DECL
13683 && TYPE_DECL_ALIAS_P (type_decl))
13684 gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13686 cp_parser_simulate_error (parser);
13688 if (!cp_parser_parse_definitely (parser))
13689 /* ... Or a typedef-name or an enum-name. */
13690 return cp_parser_nonclass_name (parser);
13696 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13704 Returns a TYPE_DECL for the type. */
13707 cp_parser_nonclass_name (cp_parser* parser)
13712 cp_token *token = cp_lexer_peek_token (parser->lexer);
13713 identifier = cp_parser_identifier (parser);
13714 if (identifier == error_mark_node)
13715 return error_mark_node;
13717 /* Look up the type-name. */
13718 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13720 if (TREE_CODE (type_decl) != TYPE_DECL
13721 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13723 /* See if this is an Objective-C type. */
13724 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13725 tree type = objc_get_protocol_qualified_type (identifier, protos);
13727 type_decl = TYPE_NAME (type);
13730 /* Issue an error if we did not find a type-name. */
13731 if (TREE_CODE (type_decl) != TYPE_DECL
13732 /* In Objective-C, we have the complication that class names are
13733 normally type names and start declarations (eg, the
13734 "NSObject" in "NSObject *object;"), but can be used in an
13735 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13736 is an expression. So, a classname followed by a dot is not a
13737 valid type-name. */
13738 || (objc_is_class_name (TREE_TYPE (type_decl))
13739 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13741 if (!cp_parser_simulate_error (parser))
13742 cp_parser_name_lookup_error (parser, identifier, type_decl,
13743 NLE_TYPE, token->location);
13744 return error_mark_node;
13746 /* Remember that the name was used in the definition of the
13747 current class so that we can check later to see if the
13748 meaning would have been different after the class was
13749 entirely defined. */
13750 else if (type_decl != error_mark_node
13752 maybe_note_name_used_in_class (identifier, type_decl);
13757 /* Parse an elaborated-type-specifier. Note that the grammar given
13758 here incorporates the resolution to DR68.
13760 elaborated-type-specifier:
13761 class-key :: [opt] nested-name-specifier [opt] identifier
13762 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13763 enum-key :: [opt] nested-name-specifier [opt] identifier
13764 typename :: [opt] nested-name-specifier identifier
13765 typename :: [opt] nested-name-specifier template [opt]
13770 elaborated-type-specifier:
13771 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13772 class-key attributes :: [opt] nested-name-specifier [opt]
13773 template [opt] template-id
13774 enum attributes :: [opt] nested-name-specifier [opt] identifier
13776 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13777 declared `friend'. If IS_DECLARATION is TRUE, then this
13778 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13779 something is being declared.
13781 Returns the TYPE specified. */
13784 cp_parser_elaborated_type_specifier (cp_parser* parser,
13786 bool is_declaration)
13788 enum tag_types tag_type;
13790 tree type = NULL_TREE;
13791 tree attributes = NULL_TREE;
13793 cp_token *token = NULL;
13795 /* See if we're looking at the `enum' keyword. */
13796 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13798 /* Consume the `enum' token. */
13799 cp_lexer_consume_token (parser->lexer);
13800 /* Remember that it's an enumeration type. */
13801 tag_type = enum_type;
13802 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13803 enums) is used here. */
13804 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13805 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13807 pedwarn (input_location, 0, "elaborated-type-specifier "
13808 "for a scoped enum must not use the %<%D%> keyword",
13809 cp_lexer_peek_token (parser->lexer)->u.value);
13810 /* Consume the `struct' or `class' and parse it anyway. */
13811 cp_lexer_consume_token (parser->lexer);
13813 /* Parse the attributes. */
13814 attributes = cp_parser_attributes_opt (parser);
13816 /* Or, it might be `typename'. */
13817 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13820 /* Consume the `typename' token. */
13821 cp_lexer_consume_token (parser->lexer);
13822 /* Remember that it's a `typename' type. */
13823 tag_type = typename_type;
13825 /* Otherwise it must be a class-key. */
13828 tag_type = cp_parser_class_key (parser);
13829 if (tag_type == none_type)
13830 return error_mark_node;
13831 /* Parse the attributes. */
13832 attributes = cp_parser_attributes_opt (parser);
13835 /* Look for the `::' operator. */
13836 globalscope = cp_parser_global_scope_opt (parser,
13837 /*current_scope_valid_p=*/false);
13838 /* Look for the nested-name-specifier. */
13839 if (tag_type == typename_type && !globalscope)
13841 if (!cp_parser_nested_name_specifier (parser,
13842 /*typename_keyword_p=*/true,
13843 /*check_dependency_p=*/true,
13846 return error_mark_node;
13849 /* Even though `typename' is not present, the proposed resolution
13850 to Core Issue 180 says that in `class A<T>::B', `B' should be
13851 considered a type-name, even if `A<T>' is dependent. */
13852 cp_parser_nested_name_specifier_opt (parser,
13853 /*typename_keyword_p=*/true,
13854 /*check_dependency_p=*/true,
13857 /* For everything but enumeration types, consider a template-id.
13858 For an enumeration type, consider only a plain identifier. */
13859 if (tag_type != enum_type)
13861 bool template_p = false;
13864 /* Allow the `template' keyword. */
13865 template_p = cp_parser_optional_template_keyword (parser);
13866 /* If we didn't see `template', we don't know if there's a
13867 template-id or not. */
13869 cp_parser_parse_tentatively (parser);
13870 /* Parse the template-id. */
13871 token = cp_lexer_peek_token (parser->lexer);
13872 decl = cp_parser_template_id (parser, template_p,
13873 /*check_dependency_p=*/true,
13875 /* If we didn't find a template-id, look for an ordinary
13877 if (!template_p && !cp_parser_parse_definitely (parser))
13879 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13880 in effect, then we must assume that, upon instantiation, the
13881 template will correspond to a class. */
13882 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13883 && tag_type == typename_type)
13884 type = make_typename_type (parser->scope, decl,
13886 /*complain=*/tf_error);
13887 /* If the `typename' keyword is in effect and DECL is not a type
13888 decl. Then type is non existant. */
13889 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13892 type = TREE_TYPE (decl);
13897 token = cp_lexer_peek_token (parser->lexer);
13898 identifier = cp_parser_identifier (parser);
13900 if (identifier == error_mark_node)
13902 parser->scope = NULL_TREE;
13903 return error_mark_node;
13906 /* For a `typename', we needn't call xref_tag. */
13907 if (tag_type == typename_type
13908 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13909 return cp_parser_make_typename_type (parser, parser->scope,
13912 /* Look up a qualified name in the usual way. */
13916 tree ambiguous_decls;
13918 decl = cp_parser_lookup_name (parser, identifier,
13920 /*is_template=*/false,
13921 /*is_namespace=*/false,
13922 /*check_dependency=*/true,
13926 /* If the lookup was ambiguous, an error will already have been
13928 if (ambiguous_decls)
13929 return error_mark_node;
13931 /* If we are parsing friend declaration, DECL may be a
13932 TEMPLATE_DECL tree node here. However, we need to check
13933 whether this TEMPLATE_DECL results in valid code. Consider
13934 the following example:
13937 template <class T> class C {};
13940 template <class T> friend class N::C; // #1, valid code
13942 template <class T> class Y {
13943 friend class N::C; // #2, invalid code
13946 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13947 name lookup of `N::C'. We see that friend declaration must
13948 be template for the code to be valid. Note that
13949 processing_template_decl does not work here since it is
13950 always 1 for the above two cases. */
13952 decl = (cp_parser_maybe_treat_template_as_class
13953 (decl, /*tag_name_p=*/is_friend
13954 && parser->num_template_parameter_lists));
13956 if (TREE_CODE (decl) != TYPE_DECL)
13958 cp_parser_diagnose_invalid_type_name (parser,
13962 return error_mark_node;
13965 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13967 bool allow_template = (parser->num_template_parameter_lists
13968 || DECL_SELF_REFERENCE_P (decl));
13969 type = check_elaborated_type_specifier (tag_type, decl,
13972 if (type == error_mark_node)
13973 return error_mark_node;
13976 /* Forward declarations of nested types, such as
13981 are invalid unless all components preceding the final '::'
13982 are complete. If all enclosing types are complete, these
13983 declarations become merely pointless.
13985 Invalid forward declarations of nested types are errors
13986 caught elsewhere in parsing. Those that are pointless arrive
13989 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13990 && !is_friend && !processing_explicit_instantiation)
13991 warning (0, "declaration %qD does not declare anything", decl);
13993 type = TREE_TYPE (decl);
13997 /* An elaborated-type-specifier sometimes introduces a new type and
13998 sometimes names an existing type. Normally, the rule is that it
13999 introduces a new type only if there is not an existing type of
14000 the same name already in scope. For example, given:
14003 void f() { struct S s; }
14005 the `struct S' in the body of `f' is the same `struct S' as in
14006 the global scope; the existing definition is used. However, if
14007 there were no global declaration, this would introduce a new
14008 local class named `S'.
14010 An exception to this rule applies to the following code:
14012 namespace N { struct S; }
14014 Here, the elaborated-type-specifier names a new type
14015 unconditionally; even if there is already an `S' in the
14016 containing scope this declaration names a new type.
14017 This exception only applies if the elaborated-type-specifier
14018 forms the complete declaration:
14022 A declaration consisting solely of `class-key identifier ;' is
14023 either a redeclaration of the name in the current scope or a
14024 forward declaration of the identifier as a class name. It
14025 introduces the name into the current scope.
14027 We are in this situation precisely when the next token is a `;'.
14029 An exception to the exception is that a `friend' declaration does
14030 *not* name a new type; i.e., given:
14032 struct S { friend struct T; };
14034 `T' is not a new type in the scope of `S'.
14036 Also, `new struct S' or `sizeof (struct S)' never results in the
14037 definition of a new type; a new type can only be declared in a
14038 declaration context. */
14044 /* Friends have special name lookup rules. */
14045 ts = ts_within_enclosing_non_class;
14046 else if (is_declaration
14047 && cp_lexer_next_token_is (parser->lexer,
14049 /* This is a `class-key identifier ;' */
14055 (parser->num_template_parameter_lists
14056 && (cp_parser_next_token_starts_class_definition_p (parser)
14057 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14058 /* An unqualified name was used to reference this type, so
14059 there were no qualifying templates. */
14060 if (!cp_parser_check_template_parameters (parser,
14061 /*num_templates=*/0,
14063 /*declarator=*/NULL))
14064 return error_mark_node;
14065 type = xref_tag (tag_type, identifier, ts, template_p);
14069 if (type == error_mark_node)
14070 return error_mark_node;
14072 /* Allow attributes on forward declarations of classes. */
14075 if (TREE_CODE (type) == TYPENAME_TYPE)
14076 warning (OPT_Wattributes,
14077 "attributes ignored on uninstantiated type");
14078 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14079 && ! processing_explicit_instantiation)
14080 warning (OPT_Wattributes,
14081 "attributes ignored on template instantiation");
14082 else if (is_declaration && cp_parser_declares_only_class_p (parser))
14083 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14085 warning (OPT_Wattributes,
14086 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14089 if (tag_type != enum_type)
14091 /* Indicate whether this class was declared as a `class' or as a
14093 if (TREE_CODE (type) == RECORD_TYPE)
14094 CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14095 cp_parser_check_class_key (tag_type, type);
14098 /* A "<" cannot follow an elaborated type specifier. If that
14099 happens, the user was probably trying to form a template-id. */
14100 cp_parser_check_for_invalid_template_id (parser, type, token->location);
14105 /* Parse an enum-specifier.
14108 enum-head { enumerator-list [opt] }
14109 enum-head { enumerator-list , } [C++0x]
14112 enum-key identifier [opt] enum-base [opt]
14113 enum-key nested-name-specifier identifier enum-base [opt]
14118 enum struct [C++0x]
14121 : type-specifier-seq
14123 opaque-enum-specifier:
14124 enum-key identifier enum-base [opt] ;
14127 enum-key attributes[opt] identifier [opt] enum-base [opt]
14128 { enumerator-list [opt] }attributes[opt]
14129 enum-key attributes[opt] identifier [opt] enum-base [opt]
14130 { enumerator-list, }attributes[opt] [C++0x]
14132 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14133 if the token stream isn't an enum-specifier after all. */
14136 cp_parser_enum_specifier (cp_parser* parser)
14139 tree type = NULL_TREE;
14141 tree nested_name_specifier = NULL_TREE;
14143 bool scoped_enum_p = false;
14144 bool has_underlying_type = false;
14145 bool nested_being_defined = false;
14146 bool new_value_list = false;
14147 bool is_new_type = false;
14148 bool is_anonymous = false;
14149 tree underlying_type = NULL_TREE;
14150 cp_token *type_start_token = NULL;
14151 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14153 parser->colon_corrects_to_scope_p = false;
14155 /* Parse tentatively so that we can back up if we don't find a
14157 cp_parser_parse_tentatively (parser);
14159 /* Caller guarantees that the current token is 'enum', an identifier
14160 possibly follows, and the token after that is an opening brace.
14161 If we don't have an identifier, fabricate an anonymous name for
14162 the enumeration being defined. */
14163 cp_lexer_consume_token (parser->lexer);
14165 /* Parse the "class" or "struct", which indicates a scoped
14166 enumeration type in C++0x. */
14167 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14168 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14170 if (cxx_dialect < cxx0x)
14171 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14173 /* Consume the `struct' or `class' token. */
14174 cp_lexer_consume_token (parser->lexer);
14176 scoped_enum_p = true;
14179 attributes = cp_parser_attributes_opt (parser);
14181 /* Clear the qualification. */
14182 parser->scope = NULL_TREE;
14183 parser->qualifying_scope = NULL_TREE;
14184 parser->object_scope = NULL_TREE;
14186 /* Figure out in what scope the declaration is being placed. */
14187 prev_scope = current_scope ();
14189 type_start_token = cp_lexer_peek_token (parser->lexer);
14191 push_deferring_access_checks (dk_no_check);
14192 nested_name_specifier
14193 = cp_parser_nested_name_specifier_opt (parser,
14194 /*typename_keyword_p=*/true,
14195 /*check_dependency_p=*/false,
14197 /*is_declaration=*/false);
14199 if (nested_name_specifier)
14203 identifier = cp_parser_identifier (parser);
14204 name = cp_parser_lookup_name (parser, identifier,
14206 /*is_template=*/false,
14207 /*is_namespace=*/false,
14208 /*check_dependency=*/true,
14209 /*ambiguous_decls=*/NULL,
14213 type = TREE_TYPE (name);
14214 if (TREE_CODE (type) == TYPENAME_TYPE)
14216 /* Are template enums allowed in ISO? */
14217 if (template_parm_scope_p ())
14218 pedwarn (type_start_token->location, OPT_pedantic,
14219 "%qD is an enumeration template", name);
14220 /* ignore a typename reference, for it will be solved by name
14226 error_at (type_start_token->location,
14227 "%qD is not an enumerator-name", identifier);
14231 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14232 identifier = cp_parser_identifier (parser);
14235 identifier = make_anon_name ();
14236 is_anonymous = true;
14239 pop_deferring_access_checks ();
14241 /* Check for the `:' that denotes a specified underlying type in C++0x.
14242 Note that a ':' could also indicate a bitfield width, however. */
14243 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14245 cp_decl_specifier_seq type_specifiers;
14247 /* Consume the `:'. */
14248 cp_lexer_consume_token (parser->lexer);
14250 /* Parse the type-specifier-seq. */
14251 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14252 /*is_trailing_return=*/false,
14255 /* At this point this is surely not elaborated type specifier. */
14256 if (!cp_parser_parse_definitely (parser))
14259 if (cxx_dialect < cxx0x)
14260 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14262 has_underlying_type = true;
14264 /* If that didn't work, stop. */
14265 if (type_specifiers.type != error_mark_node)
14267 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14268 /*initialized=*/0, NULL);
14269 if (underlying_type == error_mark_node)
14270 underlying_type = NULL_TREE;
14274 /* Look for the `{' but don't consume it yet. */
14275 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14277 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14279 cp_parser_error (parser, "expected %<{%>");
14280 if (has_underlying_type)
14286 /* An opaque-enum-specifier must have a ';' here. */
14287 if ((scoped_enum_p || underlying_type)
14288 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14290 cp_parser_error (parser, "expected %<;%> or %<{%>");
14291 if (has_underlying_type)
14299 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14302 if (nested_name_specifier)
14304 if (CLASS_TYPE_P (nested_name_specifier))
14306 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14307 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14308 push_scope (nested_name_specifier);
14310 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14312 push_nested_namespace (nested_name_specifier);
14316 /* Issue an error message if type-definitions are forbidden here. */
14317 if (!cp_parser_check_type_definition (parser))
14318 type = error_mark_node;
14320 /* Create the new type. We do this before consuming the opening
14321 brace so the enum will be recorded as being on the line of its
14322 tag (or the 'enum' keyword, if there is no tag). */
14323 type = start_enum (identifier, type, underlying_type,
14324 scoped_enum_p, &is_new_type);
14326 /* If the next token is not '{' it is an opaque-enum-specifier or an
14327 elaborated-type-specifier. */
14328 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14330 timevar_push (TV_PARSE_ENUM);
14331 if (nested_name_specifier)
14333 /* The following catches invalid code such as:
14334 enum class S<int>::E { A, B, C }; */
14335 if (!processing_specialization
14336 && CLASS_TYPE_P (nested_name_specifier)
14337 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14338 error_at (type_start_token->location, "cannot add an enumerator "
14339 "list to a template instantiation");
14341 /* If that scope does not contain the scope in which the
14342 class was originally declared, the program is invalid. */
14343 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14345 if (at_namespace_scope_p ())
14346 error_at (type_start_token->location,
14347 "declaration of %qD in namespace %qD which does not "
14349 type, prev_scope, nested_name_specifier);
14351 error_at (type_start_token->location,
14352 "declaration of %qD in %qD which does not enclose %qD",
14353 type, prev_scope, nested_name_specifier);
14354 type = error_mark_node;
14359 begin_scope (sk_scoped_enum, type);
14361 /* Consume the opening brace. */
14362 cp_lexer_consume_token (parser->lexer);
14364 if (type == error_mark_node)
14365 ; /* Nothing to add */
14366 else if (OPAQUE_ENUM_P (type)
14367 || (cxx_dialect > cxx98 && processing_specialization))
14369 new_value_list = true;
14370 SET_OPAQUE_ENUM_P (type, false);
14371 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14375 error_at (type_start_token->location, "multiple definition of %q#T", type);
14376 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14377 "previous definition here");
14378 type = error_mark_node;
14381 if (type == error_mark_node)
14382 cp_parser_skip_to_end_of_block_or_statement (parser);
14383 /* If the next token is not '}', then there are some enumerators. */
14384 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14385 cp_parser_enumerator_list (parser, type);
14387 /* Consume the final '}'. */
14388 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14392 timevar_pop (TV_PARSE_ENUM);
14396 /* If a ';' follows, then it is an opaque-enum-specifier
14397 and additional restrictions apply. */
14398 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14401 error_at (type_start_token->location,
14402 "opaque-enum-specifier without name");
14403 else if (nested_name_specifier)
14404 error_at (type_start_token->location,
14405 "opaque-enum-specifier must use a simple identifier");
14409 /* Look for trailing attributes to apply to this enumeration, and
14410 apply them if appropriate. */
14411 if (cp_parser_allow_gnu_extensions_p (parser))
14413 tree trailing_attr = cp_parser_attributes_opt (parser);
14414 trailing_attr = chainon (trailing_attr, attributes);
14415 cplus_decl_attributes (&type,
14417 (int) ATTR_FLAG_TYPE_IN_PLACE);
14420 /* Finish up the enumeration. */
14421 if (type != error_mark_node)
14423 if (new_value_list)
14424 finish_enum_value_list (type);
14426 finish_enum (type);
14429 if (nested_name_specifier)
14431 if (CLASS_TYPE_P (nested_name_specifier))
14433 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14434 pop_scope (nested_name_specifier);
14436 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14438 pop_nested_namespace (nested_name_specifier);
14442 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14446 /* Parse an enumerator-list. The enumerators all have the indicated
14450 enumerator-definition
14451 enumerator-list , enumerator-definition */
14454 cp_parser_enumerator_list (cp_parser* parser, tree type)
14458 /* Parse an enumerator-definition. */
14459 cp_parser_enumerator_definition (parser, type);
14461 /* If the next token is not a ',', we've reached the end of
14463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14465 /* Otherwise, consume the `,' and keep going. */
14466 cp_lexer_consume_token (parser->lexer);
14467 /* If the next token is a `}', there is a trailing comma. */
14468 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14470 if (cxx_dialect < cxx0x && !in_system_header)
14471 pedwarn (input_location, OPT_pedantic,
14472 "comma at end of enumerator list");
14478 /* Parse an enumerator-definition. The enumerator has the indicated
14481 enumerator-definition:
14483 enumerator = constant-expression
14489 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14495 /* Save the input location because we are interested in the location
14496 of the identifier and not the location of the explicit value. */
14497 loc = cp_lexer_peek_token (parser->lexer)->location;
14499 /* Look for the identifier. */
14500 identifier = cp_parser_identifier (parser);
14501 if (identifier == error_mark_node)
14504 /* If the next token is an '=', then there is an explicit value. */
14505 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14507 /* Consume the `=' token. */
14508 cp_lexer_consume_token (parser->lexer);
14509 /* Parse the value. */
14510 value = cp_parser_constant_expression (parser,
14511 /*allow_non_constant_p=*/false,
14517 /* If we are processing a template, make sure the initializer of the
14518 enumerator doesn't contain any bare template parameter pack. */
14519 if (check_for_bare_parameter_packs (value))
14520 value = error_mark_node;
14522 /* integral_constant_value will pull out this expression, so make sure
14523 it's folded as appropriate. */
14524 value = fold_non_dependent_expr (value);
14526 /* Create the enumerator. */
14527 build_enumerator (identifier, value, type, loc);
14530 /* Parse a namespace-name.
14533 original-namespace-name
14536 Returns the NAMESPACE_DECL for the namespace. */
14539 cp_parser_namespace_name (cp_parser* parser)
14542 tree namespace_decl;
14544 cp_token *token = cp_lexer_peek_token (parser->lexer);
14546 /* Get the name of the namespace. */
14547 identifier = cp_parser_identifier (parser);
14548 if (identifier == error_mark_node)
14549 return error_mark_node;
14551 /* Look up the identifier in the currently active scope. Look only
14552 for namespaces, due to:
14554 [basic.lookup.udir]
14556 When looking up a namespace-name in a using-directive or alias
14557 definition, only namespace names are considered.
14561 [basic.lookup.qual]
14563 During the lookup of a name preceding the :: scope resolution
14564 operator, object, function, and enumerator names are ignored.
14566 (Note that cp_parser_qualifying_entity only calls this
14567 function if the token after the name is the scope resolution
14569 namespace_decl = cp_parser_lookup_name (parser, identifier,
14571 /*is_template=*/false,
14572 /*is_namespace=*/true,
14573 /*check_dependency=*/true,
14574 /*ambiguous_decls=*/NULL,
14576 /* If it's not a namespace, issue an error. */
14577 if (namespace_decl == error_mark_node
14578 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14580 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14581 error_at (token->location, "%qD is not a namespace-name", identifier);
14582 cp_parser_error (parser, "expected namespace-name");
14583 namespace_decl = error_mark_node;
14586 return namespace_decl;
14589 /* Parse a namespace-definition.
14591 namespace-definition:
14592 named-namespace-definition
14593 unnamed-namespace-definition
14595 named-namespace-definition:
14596 original-namespace-definition
14597 extension-namespace-definition
14599 original-namespace-definition:
14600 namespace identifier { namespace-body }
14602 extension-namespace-definition:
14603 namespace original-namespace-name { namespace-body }
14605 unnamed-namespace-definition:
14606 namespace { namespace-body } */
14609 cp_parser_namespace_definition (cp_parser* parser)
14611 tree identifier, attribs;
14612 bool has_visibility;
14615 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14617 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14619 cp_lexer_consume_token (parser->lexer);
14624 /* Look for the `namespace' keyword. */
14625 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14627 /* Get the name of the namespace. We do not attempt to distinguish
14628 between an original-namespace-definition and an
14629 extension-namespace-definition at this point. The semantic
14630 analysis routines are responsible for that. */
14631 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14632 identifier = cp_parser_identifier (parser);
14634 identifier = NULL_TREE;
14636 /* Parse any specified attributes. */
14637 attribs = cp_parser_attributes_opt (parser);
14639 /* Look for the `{' to start the namespace. */
14640 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14641 /* Start the namespace. */
14642 push_namespace (identifier);
14644 /* "inline namespace" is equivalent to a stub namespace definition
14645 followed by a strong using directive. */
14648 tree name_space = current_namespace;
14649 /* Set up namespace association. */
14650 DECL_NAMESPACE_ASSOCIATIONS (name_space)
14651 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14652 DECL_NAMESPACE_ASSOCIATIONS (name_space));
14653 /* Import the contents of the inline namespace. */
14655 do_using_directive (name_space);
14656 push_namespace (identifier);
14659 has_visibility = handle_namespace_attrs (current_namespace, attribs);
14661 /* Parse the body of the namespace. */
14662 cp_parser_namespace_body (parser);
14664 if (has_visibility)
14665 pop_visibility (1);
14667 /* Finish the namespace. */
14669 /* Look for the final `}'. */
14670 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14673 /* Parse a namespace-body.
14676 declaration-seq [opt] */
14679 cp_parser_namespace_body (cp_parser* parser)
14681 cp_parser_declaration_seq_opt (parser);
14684 /* Parse a namespace-alias-definition.
14686 namespace-alias-definition:
14687 namespace identifier = qualified-namespace-specifier ; */
14690 cp_parser_namespace_alias_definition (cp_parser* parser)
14693 tree namespace_specifier;
14695 cp_token *token = cp_lexer_peek_token (parser->lexer);
14697 /* Look for the `namespace' keyword. */
14698 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14699 /* Look for the identifier. */
14700 identifier = cp_parser_identifier (parser);
14701 if (identifier == error_mark_node)
14703 /* Look for the `=' token. */
14704 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14705 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14707 error_at (token->location, "%<namespace%> definition is not allowed here");
14708 /* Skip the definition. */
14709 cp_lexer_consume_token (parser->lexer);
14710 if (cp_parser_skip_to_closing_brace (parser))
14711 cp_lexer_consume_token (parser->lexer);
14714 cp_parser_require (parser, CPP_EQ, RT_EQ);
14715 /* Look for the qualified-namespace-specifier. */
14716 namespace_specifier
14717 = cp_parser_qualified_namespace_specifier (parser);
14718 /* Look for the `;' token. */
14719 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14721 /* Register the alias in the symbol table. */
14722 do_namespace_alias (identifier, namespace_specifier);
14725 /* Parse a qualified-namespace-specifier.
14727 qualified-namespace-specifier:
14728 :: [opt] nested-name-specifier [opt] namespace-name
14730 Returns a NAMESPACE_DECL corresponding to the specified
14734 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14736 /* Look for the optional `::'. */
14737 cp_parser_global_scope_opt (parser,
14738 /*current_scope_valid_p=*/false);
14740 /* Look for the optional nested-name-specifier. */
14741 cp_parser_nested_name_specifier_opt (parser,
14742 /*typename_keyword_p=*/false,
14743 /*check_dependency_p=*/true,
14745 /*is_declaration=*/true);
14747 return cp_parser_namespace_name (parser);
14750 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14751 access declaration.
14754 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14755 using :: unqualified-id ;
14757 access-declaration:
14763 cp_parser_using_declaration (cp_parser* parser,
14764 bool access_declaration_p)
14767 bool typename_p = false;
14768 bool global_scope_p;
14773 if (access_declaration_p)
14774 cp_parser_parse_tentatively (parser);
14777 /* Look for the `using' keyword. */
14778 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14780 /* Peek at the next token. */
14781 token = cp_lexer_peek_token (parser->lexer);
14782 /* See if it's `typename'. */
14783 if (token->keyword == RID_TYPENAME)
14785 /* Remember that we've seen it. */
14787 /* Consume the `typename' token. */
14788 cp_lexer_consume_token (parser->lexer);
14792 /* Look for the optional global scope qualification. */
14794 = (cp_parser_global_scope_opt (parser,
14795 /*current_scope_valid_p=*/false)
14798 /* If we saw `typename', or didn't see `::', then there must be a
14799 nested-name-specifier present. */
14800 if (typename_p || !global_scope_p)
14801 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14802 /*check_dependency_p=*/true,
14804 /*is_declaration=*/true);
14805 /* Otherwise, we could be in either of the two productions. In that
14806 case, treat the nested-name-specifier as optional. */
14808 qscope = cp_parser_nested_name_specifier_opt (parser,
14809 /*typename_keyword_p=*/false,
14810 /*check_dependency_p=*/true,
14812 /*is_declaration=*/true);
14814 qscope = global_namespace;
14816 if (access_declaration_p && cp_parser_error_occurred (parser))
14817 /* Something has already gone wrong; there's no need to parse
14818 further. Since an error has occurred, the return value of
14819 cp_parser_parse_definitely will be false, as required. */
14820 return cp_parser_parse_definitely (parser);
14822 token = cp_lexer_peek_token (parser->lexer);
14823 /* Parse the unqualified-id. */
14824 identifier = cp_parser_unqualified_id (parser,
14825 /*template_keyword_p=*/false,
14826 /*check_dependency_p=*/true,
14827 /*declarator_p=*/true,
14828 /*optional_p=*/false);
14830 if (access_declaration_p)
14832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14833 cp_parser_simulate_error (parser);
14834 if (!cp_parser_parse_definitely (parser))
14838 /* The function we call to handle a using-declaration is different
14839 depending on what scope we are in. */
14840 if (qscope == error_mark_node || identifier == error_mark_node)
14842 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14843 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14844 /* [namespace.udecl]
14846 A using declaration shall not name a template-id. */
14847 error_at (token->location,
14848 "a template-id may not appear in a using-declaration");
14851 if (at_class_scope_p ())
14853 /* Create the USING_DECL. */
14854 decl = do_class_using_decl (parser->scope, identifier);
14856 if (check_for_bare_parameter_packs (decl))
14859 /* Add it to the list of members in this class. */
14860 finish_member_declaration (decl);
14864 decl = cp_parser_lookup_name_simple (parser,
14867 if (decl == error_mark_node)
14868 cp_parser_name_lookup_error (parser, identifier,
14871 else if (check_for_bare_parameter_packs (decl))
14873 else if (!at_namespace_scope_p ())
14874 do_local_using_decl (decl, qscope, identifier);
14876 do_toplevel_using_decl (decl, qscope, identifier);
14880 /* Look for the final `;'. */
14881 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14886 /* Parse an alias-declaration.
14889 using identifier attribute-specifier-seq [opt] = type-id */
14892 cp_parser_alias_declaration (cp_parser* parser)
14894 tree id, type, decl, dummy, attributes;
14895 location_t id_location;
14896 cp_declarator *declarator;
14897 cp_decl_specifier_seq decl_specs;
14899 /* Look for the `using' keyword. */
14900 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14901 id_location = cp_lexer_peek_token (parser->lexer)->location;
14902 id = cp_parser_identifier (parser);
14903 attributes = cp_parser_attributes_opt (parser);
14904 cp_parser_require (parser, CPP_EQ, RT_EQ);
14906 type = cp_parser_type_id (parser);
14908 /* A typedef-name can also be introduced by an alias-declaration. The
14909 identifier following the using keyword becomes a typedef-name. It has
14910 the same semantics as if it were introduced by the typedef
14911 specifier. In particular, it does not define a new type and it shall
14912 not appear in the type-id. */
14914 clear_decl_specs (&decl_specs);
14915 decl_specs.type = type;
14916 decl_specs.attributes = attributes;
14917 ++decl_specs.specs[(int) ds_typedef];
14918 ++decl_specs.specs[(int) ds_alias];
14920 declarator = make_id_declarator (NULL_TREE, id, sfk_none);
14921 declarator->id_loc = id_location;
14923 if (at_class_scope_p ())
14924 decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
14925 NULL_TREE, attributes);
14927 decl = start_decl (declarator, &decl_specs, 0,
14928 attributes, NULL_TREE, &dummy);
14929 if (decl == error_mark_node)
14932 cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
14934 /* If decl is a template, return its TEMPLATE_DECL so that it gets
14935 added into the symbol table; otherwise, return the TYPE_DECL. */
14936 if (DECL_LANG_SPECIFIC (decl)
14937 && DECL_TEMPLATE_INFO (decl)
14938 && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
14939 decl = DECL_TI_TEMPLATE (decl);
14943 /* Parse a using-directive.
14946 using namespace :: [opt] nested-name-specifier [opt]
14947 namespace-name ; */
14950 cp_parser_using_directive (cp_parser* parser)
14952 tree namespace_decl;
14955 /* Look for the `using' keyword. */
14956 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14957 /* And the `namespace' keyword. */
14958 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14959 /* Look for the optional `::' operator. */
14960 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14961 /* And the optional nested-name-specifier. */
14962 cp_parser_nested_name_specifier_opt (parser,
14963 /*typename_keyword_p=*/false,
14964 /*check_dependency_p=*/true,
14966 /*is_declaration=*/true);
14967 /* Get the namespace being used. */
14968 namespace_decl = cp_parser_namespace_name (parser);
14969 /* And any specified attributes. */
14970 attribs = cp_parser_attributes_opt (parser);
14971 /* Update the symbol table. */
14972 parse_using_directive (namespace_decl, attribs);
14973 /* Look for the final `;'. */
14974 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14977 /* Parse an asm-definition.
14980 asm ( string-literal ) ;
14985 asm volatile [opt] ( string-literal ) ;
14986 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14987 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14988 : asm-operand-list [opt] ) ;
14989 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14990 : asm-operand-list [opt]
14991 : asm-clobber-list [opt] ) ;
14992 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14993 : asm-clobber-list [opt]
14994 : asm-goto-list ) ; */
14997 cp_parser_asm_definition (cp_parser* parser)
15000 tree outputs = NULL_TREE;
15001 tree inputs = NULL_TREE;
15002 tree clobbers = NULL_TREE;
15003 tree labels = NULL_TREE;
15005 bool volatile_p = false;
15006 bool extended_p = false;
15007 bool invalid_inputs_p = false;
15008 bool invalid_outputs_p = false;
15009 bool goto_p = false;
15010 required_token missing = RT_NONE;
15012 /* Look for the `asm' keyword. */
15013 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15014 /* See if the next token is `volatile'. */
15015 if (cp_parser_allow_gnu_extensions_p (parser)
15016 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15018 /* Remember that we saw the `volatile' keyword. */
15020 /* Consume the token. */
15021 cp_lexer_consume_token (parser->lexer);
15023 if (cp_parser_allow_gnu_extensions_p (parser)
15024 && parser->in_function_body
15025 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15027 /* Remember that we saw the `goto' keyword. */
15029 /* Consume the token. */
15030 cp_lexer_consume_token (parser->lexer);
15032 /* Look for the opening `('. */
15033 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15035 /* Look for the string. */
15036 string = cp_parser_string_literal (parser, false, false);
15037 if (string == error_mark_node)
15039 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15040 /*consume_paren=*/true);
15044 /* If we're allowing GNU extensions, check for the extended assembly
15045 syntax. Unfortunately, the `:' tokens need not be separated by
15046 a space in C, and so, for compatibility, we tolerate that here
15047 too. Doing that means that we have to treat the `::' operator as
15049 if (cp_parser_allow_gnu_extensions_p (parser)
15050 && parser->in_function_body
15051 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15052 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15054 bool inputs_p = false;
15055 bool clobbers_p = false;
15056 bool labels_p = false;
15058 /* The extended syntax was used. */
15061 /* Look for outputs. */
15062 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15064 /* Consume the `:'. */
15065 cp_lexer_consume_token (parser->lexer);
15066 /* Parse the output-operands. */
15067 if (cp_lexer_next_token_is_not (parser->lexer,
15069 && cp_lexer_next_token_is_not (parser->lexer,
15071 && cp_lexer_next_token_is_not (parser->lexer,
15074 outputs = cp_parser_asm_operand_list (parser);
15076 if (outputs == error_mark_node)
15077 invalid_outputs_p = true;
15079 /* If the next token is `::', there are no outputs, and the
15080 next token is the beginning of the inputs. */
15081 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15082 /* The inputs are coming next. */
15085 /* Look for inputs. */
15087 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15089 /* Consume the `:' or `::'. */
15090 cp_lexer_consume_token (parser->lexer);
15091 /* Parse the output-operands. */
15092 if (cp_lexer_next_token_is_not (parser->lexer,
15094 && cp_lexer_next_token_is_not (parser->lexer,
15096 && cp_lexer_next_token_is_not (parser->lexer,
15098 inputs = cp_parser_asm_operand_list (parser);
15100 if (inputs == error_mark_node)
15101 invalid_inputs_p = true;
15103 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15104 /* The clobbers are coming next. */
15107 /* Look for clobbers. */
15109 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15112 /* Consume the `:' or `::'. */
15113 cp_lexer_consume_token (parser->lexer);
15114 /* Parse the clobbers. */
15115 if (cp_lexer_next_token_is_not (parser->lexer,
15117 && cp_lexer_next_token_is_not (parser->lexer,
15119 clobbers = cp_parser_asm_clobber_list (parser);
15122 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15123 /* The labels are coming next. */
15126 /* Look for labels. */
15128 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15131 /* Consume the `:' or `::'. */
15132 cp_lexer_consume_token (parser->lexer);
15133 /* Parse the labels. */
15134 labels = cp_parser_asm_label_list (parser);
15137 if (goto_p && !labels_p)
15138 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15141 missing = RT_COLON_SCOPE;
15143 /* Look for the closing `)'. */
15144 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15145 missing ? missing : RT_CLOSE_PAREN))
15146 cp_parser_skip_to_closing_parenthesis (parser, true, false,
15147 /*consume_paren=*/true);
15148 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15150 if (!invalid_inputs_p && !invalid_outputs_p)
15152 /* Create the ASM_EXPR. */
15153 if (parser->in_function_body)
15155 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15156 inputs, clobbers, labels);
15157 /* If the extended syntax was not used, mark the ASM_EXPR. */
15160 tree temp = asm_stmt;
15161 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15162 temp = TREE_OPERAND (temp, 0);
15164 ASM_INPUT_P (temp) = 1;
15168 cgraph_add_asm_node (string);
15172 /* Declarators [gram.dcl.decl] */
15174 /* Parse an init-declarator.
15177 declarator initializer [opt]
15182 declarator asm-specification [opt] attributes [opt] initializer [opt]
15184 function-definition:
15185 decl-specifier-seq [opt] declarator ctor-initializer [opt]
15187 decl-specifier-seq [opt] declarator function-try-block
15191 function-definition:
15192 __extension__ function-definition
15194 The DECL_SPECIFIERS apply to this declarator. Returns a
15195 representation of the entity declared. If MEMBER_P is TRUE, then
15196 this declarator appears in a class scope. The new DECL created by
15197 this declarator is returned.
15199 The CHECKS are access checks that should be performed once we know
15200 what entity is being declared (and, therefore, what classes have
15203 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15204 for a function-definition here as well. If the declarator is a
15205 declarator for a function-definition, *FUNCTION_DEFINITION_P will
15206 be TRUE upon return. By that point, the function-definition will
15207 have been completely parsed.
15209 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15212 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15213 parsed declaration if it is an uninitialized single declarator not followed
15214 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15215 if present, will not be consumed. If returned, this declarator will be
15216 created with SD_INITIALIZED but will not call cp_finish_decl. */
15219 cp_parser_init_declarator (cp_parser* parser,
15220 cp_decl_specifier_seq *decl_specifiers,
15221 VEC (deferred_access_check,gc)* checks,
15222 bool function_definition_allowed_p,
15224 int declares_class_or_enum,
15225 bool* function_definition_p,
15226 tree* maybe_range_for_decl)
15228 cp_token *token = NULL, *asm_spec_start_token = NULL,
15229 *attributes_start_token = NULL;
15230 cp_declarator *declarator;
15231 tree prefix_attributes;
15233 tree asm_specification;
15235 tree decl = NULL_TREE;
15237 int is_initialized;
15238 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
15239 initialized with "= ..", CPP_OPEN_PAREN if initialized with
15241 enum cpp_ttype initialization_kind;
15242 bool is_direct_init = false;
15243 bool is_non_constant_init;
15244 int ctor_dtor_or_conv_p;
15246 tree pushed_scope = NULL_TREE;
15247 bool range_for_decl_p = false;
15249 /* Gather the attributes that were provided with the
15250 decl-specifiers. */
15251 prefix_attributes = decl_specifiers->attributes;
15253 /* Assume that this is not the declarator for a function
15255 if (function_definition_p)
15256 *function_definition_p = false;
15258 /* Defer access checks while parsing the declarator; we cannot know
15259 what names are accessible until we know what is being
15261 resume_deferring_access_checks ();
15263 /* Parse the declarator. */
15264 token = cp_lexer_peek_token (parser->lexer);
15266 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15267 &ctor_dtor_or_conv_p,
15268 /*parenthesized_p=*/NULL,
15270 /* Gather up the deferred checks. */
15271 stop_deferring_access_checks ();
15273 /* If the DECLARATOR was erroneous, there's no need to go
15275 if (declarator == cp_error_declarator)
15276 return error_mark_node;
15278 /* Check that the number of template-parameter-lists is OK. */
15279 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15281 return error_mark_node;
15283 if (declares_class_or_enum & 2)
15284 cp_parser_check_for_definition_in_return_type (declarator,
15285 decl_specifiers->type,
15286 decl_specifiers->type_location);
15288 /* Figure out what scope the entity declared by the DECLARATOR is
15289 located in. `grokdeclarator' sometimes changes the scope, so
15290 we compute it now. */
15291 scope = get_scope_of_declarator (declarator);
15293 /* Perform any lookups in the declared type which were thought to be
15294 dependent, but are not in the scope of the declarator. */
15295 decl_specifiers->type
15296 = maybe_update_decl_type (decl_specifiers->type, scope);
15298 /* If we're allowing GNU extensions, look for an asm-specification
15300 if (cp_parser_allow_gnu_extensions_p (parser))
15302 /* Look for an asm-specification. */
15303 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15304 asm_specification = cp_parser_asm_specification_opt (parser);
15305 /* And attributes. */
15306 attributes_start_token = cp_lexer_peek_token (parser->lexer);
15307 attributes = cp_parser_attributes_opt (parser);
15311 asm_specification = NULL_TREE;
15312 attributes = NULL_TREE;
15315 /* Peek at the next token. */
15316 token = cp_lexer_peek_token (parser->lexer);
15317 /* Check to see if the token indicates the start of a
15318 function-definition. */
15319 if (function_declarator_p (declarator)
15320 && cp_parser_token_starts_function_definition_p (token))
15322 if (!function_definition_allowed_p)
15324 /* If a function-definition should not appear here, issue an
15326 cp_parser_error (parser,
15327 "a function-definition is not allowed here");
15328 return error_mark_node;
15332 location_t func_brace_location
15333 = cp_lexer_peek_token (parser->lexer)->location;
15335 /* Neither attributes nor an asm-specification are allowed
15336 on a function-definition. */
15337 if (asm_specification)
15338 error_at (asm_spec_start_token->location,
15339 "an asm-specification is not allowed "
15340 "on a function-definition");
15342 error_at (attributes_start_token->location,
15343 "attributes are not allowed on a function-definition");
15344 /* This is a function-definition. */
15345 *function_definition_p = true;
15347 /* Parse the function definition. */
15349 decl = cp_parser_save_member_function_body (parser,
15352 prefix_attributes);
15355 = (cp_parser_function_definition_from_specifiers_and_declarator
15356 (parser, decl_specifiers, prefix_attributes, declarator));
15358 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15360 /* This is where the prologue starts... */
15361 DECL_STRUCT_FUNCTION (decl)->function_start_locus
15362 = func_brace_location;
15371 Only in function declarations for constructors, destructors, and
15372 type conversions can the decl-specifier-seq be omitted.
15374 We explicitly postpone this check past the point where we handle
15375 function-definitions because we tolerate function-definitions
15376 that are missing their return types in some modes. */
15377 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15379 cp_parser_error (parser,
15380 "expected constructor, destructor, or type conversion");
15381 return error_mark_node;
15384 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
15385 if (token->type == CPP_EQ
15386 || token->type == CPP_OPEN_PAREN
15387 || token->type == CPP_OPEN_BRACE)
15389 is_initialized = SD_INITIALIZED;
15390 initialization_kind = token->type;
15391 if (maybe_range_for_decl)
15392 *maybe_range_for_decl = error_mark_node;
15394 if (token->type == CPP_EQ
15395 && function_declarator_p (declarator))
15397 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15398 if (t2->keyword == RID_DEFAULT)
15399 is_initialized = SD_DEFAULTED;
15400 else if (t2->keyword == RID_DELETE)
15401 is_initialized = SD_DELETED;
15406 /* If the init-declarator isn't initialized and isn't followed by a
15407 `,' or `;', it's not a valid init-declarator. */
15408 if (token->type != CPP_COMMA
15409 && token->type != CPP_SEMICOLON)
15411 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15412 range_for_decl_p = true;
15415 cp_parser_error (parser, "expected initializer");
15416 return error_mark_node;
15419 is_initialized = SD_UNINITIALIZED;
15420 initialization_kind = CPP_EOF;
15423 /* Because start_decl has side-effects, we should only call it if we
15424 know we're going ahead. By this point, we know that we cannot
15425 possibly be looking at any other construct. */
15426 cp_parser_commit_to_tentative_parse (parser);
15428 /* If the decl specifiers were bad, issue an error now that we're
15429 sure this was intended to be a declarator. Then continue
15430 declaring the variable(s), as int, to try to cut down on further
15432 if (decl_specifiers->any_specifiers_p
15433 && decl_specifiers->type == error_mark_node)
15435 cp_parser_error (parser, "invalid type in declaration");
15436 decl_specifiers->type = integer_type_node;
15439 /* Check to see whether or not this declaration is a friend. */
15440 friend_p = cp_parser_friend_p (decl_specifiers);
15442 /* Enter the newly declared entry in the symbol table. If we're
15443 processing a declaration in a class-specifier, we wait until
15444 after processing the initializer. */
15447 if (parser->in_unbraced_linkage_specification_p)
15448 decl_specifiers->storage_class = sc_extern;
15449 decl = start_decl (declarator, decl_specifiers,
15450 range_for_decl_p? SD_INITIALIZED : is_initialized,
15451 attributes, prefix_attributes,
15453 /* Adjust location of decl if declarator->id_loc is more appropriate:
15454 set, and decl wasn't merged with another decl, in which case its
15455 location would be different from input_location, and more accurate. */
15457 && declarator->id_loc != UNKNOWN_LOCATION
15458 && DECL_SOURCE_LOCATION (decl) == input_location)
15459 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15462 /* Enter the SCOPE. That way unqualified names appearing in the
15463 initializer will be looked up in SCOPE. */
15464 pushed_scope = push_scope (scope);
15466 /* Perform deferred access control checks, now that we know in which
15467 SCOPE the declared entity resides. */
15468 if (!member_p && decl)
15470 tree saved_current_function_decl = NULL_TREE;
15472 /* If the entity being declared is a function, pretend that we
15473 are in its scope. If it is a `friend', it may have access to
15474 things that would not otherwise be accessible. */
15475 if (TREE_CODE (decl) == FUNCTION_DECL)
15477 saved_current_function_decl = current_function_decl;
15478 current_function_decl = decl;
15481 /* Perform access checks for template parameters. */
15482 cp_parser_perform_template_parameter_access_checks (checks);
15484 /* Perform the access control checks for the declarator and the
15485 decl-specifiers. */
15486 perform_deferred_access_checks ();
15488 /* Restore the saved value. */
15489 if (TREE_CODE (decl) == FUNCTION_DECL)
15490 current_function_decl = saved_current_function_decl;
15493 /* Parse the initializer. */
15494 initializer = NULL_TREE;
15495 is_direct_init = false;
15496 is_non_constant_init = true;
15497 if (is_initialized)
15499 if (function_declarator_p (declarator))
15501 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15502 if (initialization_kind == CPP_EQ)
15503 initializer = cp_parser_pure_specifier (parser);
15506 /* If the declaration was erroneous, we don't really
15507 know what the user intended, so just silently
15508 consume the initializer. */
15509 if (decl != error_mark_node)
15510 error_at (initializer_start_token->location,
15511 "initializer provided for function");
15512 cp_parser_skip_to_closing_parenthesis (parser,
15513 /*recovering=*/true,
15514 /*or_comma=*/false,
15515 /*consume_paren=*/true);
15520 /* We want to record the extra mangling scope for in-class
15521 initializers of class members and initializers of static data
15522 member templates. The former is a C++0x feature which isn't
15523 implemented yet, and I expect it will involve deferring
15524 parsing of the initializer until end of class as with default
15525 arguments. So right here we only handle the latter. */
15526 if (!member_p && processing_template_decl)
15527 start_lambda_scope (decl);
15528 initializer = cp_parser_initializer (parser,
15530 &is_non_constant_init);
15531 if (!member_p && processing_template_decl)
15532 finish_lambda_scope ();
15536 /* The old parser allows attributes to appear after a parenthesized
15537 initializer. Mark Mitchell proposed removing this functionality
15538 on the GCC mailing lists on 2002-08-13. This parser accepts the
15539 attributes -- but ignores them. */
15540 if (cp_parser_allow_gnu_extensions_p (parser)
15541 && initialization_kind == CPP_OPEN_PAREN)
15542 if (cp_parser_attributes_opt (parser))
15543 warning (OPT_Wattributes,
15544 "attributes after parenthesized initializer ignored");
15546 /* For an in-class declaration, use `grokfield' to create the
15552 pop_scope (pushed_scope);
15553 pushed_scope = NULL_TREE;
15555 decl = grokfield (declarator, decl_specifiers,
15556 initializer, !is_non_constant_init,
15557 /*asmspec=*/NULL_TREE,
15558 prefix_attributes);
15559 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15560 cp_parser_save_default_args (parser, decl);
15563 /* Finish processing the declaration. But, skip member
15565 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15567 cp_finish_decl (decl,
15568 initializer, !is_non_constant_init,
15570 /* If the initializer is in parentheses, then this is
15571 a direct-initialization, which means that an
15572 `explicit' constructor is OK. Otherwise, an
15573 `explicit' constructor cannot be used. */
15574 ((is_direct_init || !is_initialized)
15575 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15577 else if ((cxx_dialect != cxx98) && friend_p
15578 && decl && TREE_CODE (decl) == FUNCTION_DECL)
15579 /* Core issue #226 (C++0x only): A default template-argument
15580 shall not be specified in a friend class template
15582 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
15583 /*is_partial=*/0, /*is_friend_decl=*/1);
15585 if (!friend_p && pushed_scope)
15586 pop_scope (pushed_scope);
15591 /* Parse a declarator.
15595 ptr-operator declarator
15597 abstract-declarator:
15598 ptr-operator abstract-declarator [opt]
15599 direct-abstract-declarator
15604 attributes [opt] direct-declarator
15605 attributes [opt] ptr-operator declarator
15607 abstract-declarator:
15608 attributes [opt] ptr-operator abstract-declarator [opt]
15609 attributes [opt] direct-abstract-declarator
15611 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15612 detect constructor, destructor or conversion operators. It is set
15613 to -1 if the declarator is a name, and +1 if it is a
15614 function. Otherwise it is set to zero. Usually you just want to
15615 test for >0, but internally the negative value is used.
15617 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15618 a decl-specifier-seq unless it declares a constructor, destructor,
15619 or conversion. It might seem that we could check this condition in
15620 semantic analysis, rather than parsing, but that makes it difficult
15621 to handle something like `f()'. We want to notice that there are
15622 no decl-specifiers, and therefore realize that this is an
15623 expression, not a declaration.)
15625 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15626 the declarator is a direct-declarator of the form "(...)".
15628 MEMBER_P is true iff this declarator is a member-declarator. */
15630 static cp_declarator *
15631 cp_parser_declarator (cp_parser* parser,
15632 cp_parser_declarator_kind dcl_kind,
15633 int* ctor_dtor_or_conv_p,
15634 bool* parenthesized_p,
15637 cp_declarator *declarator;
15638 enum tree_code code;
15639 cp_cv_quals cv_quals;
15641 tree attributes = NULL_TREE;
15643 /* Assume this is not a constructor, destructor, or type-conversion
15645 if (ctor_dtor_or_conv_p)
15646 *ctor_dtor_or_conv_p = 0;
15648 if (cp_parser_allow_gnu_extensions_p (parser))
15649 attributes = cp_parser_attributes_opt (parser);
15651 /* Check for the ptr-operator production. */
15652 cp_parser_parse_tentatively (parser);
15653 /* Parse the ptr-operator. */
15654 code = cp_parser_ptr_operator (parser,
15657 /* If that worked, then we have a ptr-operator. */
15658 if (cp_parser_parse_definitely (parser))
15660 /* If a ptr-operator was found, then this declarator was not
15662 if (parenthesized_p)
15663 *parenthesized_p = true;
15664 /* The dependent declarator is optional if we are parsing an
15665 abstract-declarator. */
15666 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15667 cp_parser_parse_tentatively (parser);
15669 /* Parse the dependent declarator. */
15670 declarator = cp_parser_declarator (parser, dcl_kind,
15671 /*ctor_dtor_or_conv_p=*/NULL,
15672 /*parenthesized_p=*/NULL,
15673 /*member_p=*/false);
15675 /* If we are parsing an abstract-declarator, we must handle the
15676 case where the dependent declarator is absent. */
15677 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15678 && !cp_parser_parse_definitely (parser))
15681 declarator = cp_parser_make_indirect_declarator
15682 (code, class_type, cv_quals, declarator);
15684 /* Everything else is a direct-declarator. */
15687 if (parenthesized_p)
15688 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15690 declarator = cp_parser_direct_declarator (parser, dcl_kind,
15691 ctor_dtor_or_conv_p,
15695 if (attributes && declarator && declarator != cp_error_declarator)
15696 declarator->attributes = attributes;
15701 /* Parse a direct-declarator or direct-abstract-declarator.
15705 direct-declarator ( parameter-declaration-clause )
15706 cv-qualifier-seq [opt]
15707 exception-specification [opt]
15708 direct-declarator [ constant-expression [opt] ]
15711 direct-abstract-declarator:
15712 direct-abstract-declarator [opt]
15713 ( parameter-declaration-clause )
15714 cv-qualifier-seq [opt]
15715 exception-specification [opt]
15716 direct-abstract-declarator [opt] [ constant-expression [opt] ]
15717 ( abstract-declarator )
15719 Returns a representation of the declarator. DCL_KIND is
15720 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15721 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
15722 we are parsing a direct-declarator. It is
15723 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15724 of ambiguity we prefer an abstract declarator, as per
15725 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15726 cp_parser_declarator. */
15728 static cp_declarator *
15729 cp_parser_direct_declarator (cp_parser* parser,
15730 cp_parser_declarator_kind dcl_kind,
15731 int* ctor_dtor_or_conv_p,
15735 cp_declarator *declarator = NULL;
15736 tree scope = NULL_TREE;
15737 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15738 bool saved_in_declarator_p = parser->in_declarator_p;
15740 tree pushed_scope = NULL_TREE;
15744 /* Peek at the next token. */
15745 token = cp_lexer_peek_token (parser->lexer);
15746 if (token->type == CPP_OPEN_PAREN)
15748 /* This is either a parameter-declaration-clause, or a
15749 parenthesized declarator. When we know we are parsing a
15750 named declarator, it must be a parenthesized declarator
15751 if FIRST is true. For instance, `(int)' is a
15752 parameter-declaration-clause, with an omitted
15753 direct-abstract-declarator. But `((*))', is a
15754 parenthesized abstract declarator. Finally, when T is a
15755 template parameter `(T)' is a
15756 parameter-declaration-clause, and not a parenthesized
15759 We first try and parse a parameter-declaration-clause,
15760 and then try a nested declarator (if FIRST is true).
15762 It is not an error for it not to be a
15763 parameter-declaration-clause, even when FIRST is
15769 The first is the declaration of a function while the
15770 second is the definition of a variable, including its
15773 Having seen only the parenthesis, we cannot know which of
15774 these two alternatives should be selected. Even more
15775 complex are examples like:
15780 The former is a function-declaration; the latter is a
15781 variable initialization.
15783 Thus again, we try a parameter-declaration-clause, and if
15784 that fails, we back out and return. */
15786 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15789 unsigned saved_num_template_parameter_lists;
15790 bool is_declarator = false;
15793 /* In a member-declarator, the only valid interpretation
15794 of a parenthesis is the start of a
15795 parameter-declaration-clause. (It is invalid to
15796 initialize a static data member with a parenthesized
15797 initializer; only the "=" form of initialization is
15800 cp_parser_parse_tentatively (parser);
15802 /* Consume the `('. */
15803 cp_lexer_consume_token (parser->lexer);
15806 /* If this is going to be an abstract declarator, we're
15807 in a declarator and we can't have default args. */
15808 parser->default_arg_ok_p = false;
15809 parser->in_declarator_p = true;
15812 /* Inside the function parameter list, surrounding
15813 template-parameter-lists do not apply. */
15814 saved_num_template_parameter_lists
15815 = parser->num_template_parameter_lists;
15816 parser->num_template_parameter_lists = 0;
15818 begin_scope (sk_function_parms, NULL_TREE);
15820 /* Parse the parameter-declaration-clause. */
15821 params = cp_parser_parameter_declaration_clause (parser);
15823 parser->num_template_parameter_lists
15824 = saved_num_template_parameter_lists;
15826 /* Consume the `)'. */
15827 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15829 /* If all went well, parse the cv-qualifier-seq and the
15830 exception-specification. */
15831 if (member_p || cp_parser_parse_definitely (parser))
15833 cp_cv_quals cv_quals;
15834 cp_virt_specifiers virt_specifiers;
15835 tree exception_specification;
15838 is_declarator = true;
15840 if (ctor_dtor_or_conv_p)
15841 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15844 /* Parse the cv-qualifier-seq. */
15845 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15846 /* And the exception-specification. */
15847 exception_specification
15848 = cp_parser_exception_specification_opt (parser);
15849 /* Parse the virt-specifier-seq. */
15850 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15852 late_return = (cp_parser_late_return_type_opt
15853 (parser, member_p ? cv_quals : -1));
15855 /* Create the function-declarator. */
15856 declarator = make_call_declarator (declarator,
15860 exception_specification,
15862 /* Any subsequent parameter lists are to do with
15863 return type, so are not those of the declared
15865 parser->default_arg_ok_p = false;
15868 /* Remove the function parms from scope. */
15869 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15870 pop_binding (DECL_NAME (t), t);
15874 /* Repeat the main loop. */
15878 /* If this is the first, we can try a parenthesized
15882 bool saved_in_type_id_in_expr_p;
15884 parser->default_arg_ok_p = saved_default_arg_ok_p;
15885 parser->in_declarator_p = saved_in_declarator_p;
15887 /* Consume the `('. */
15888 cp_lexer_consume_token (parser->lexer);
15889 /* Parse the nested declarator. */
15890 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15891 parser->in_type_id_in_expr_p = true;
15893 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15894 /*parenthesized_p=*/NULL,
15896 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15898 /* Expect a `)'. */
15899 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15900 declarator = cp_error_declarator;
15901 if (declarator == cp_error_declarator)
15904 goto handle_declarator;
15906 /* Otherwise, we must be done. */
15910 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15911 && token->type == CPP_OPEN_SQUARE)
15913 /* Parse an array-declarator. */
15916 if (ctor_dtor_or_conv_p)
15917 *ctor_dtor_or_conv_p = 0;
15920 parser->default_arg_ok_p = false;
15921 parser->in_declarator_p = true;
15922 /* Consume the `['. */
15923 cp_lexer_consume_token (parser->lexer);
15924 /* Peek at the next token. */
15925 token = cp_lexer_peek_token (parser->lexer);
15926 /* If the next token is `]', then there is no
15927 constant-expression. */
15928 if (token->type != CPP_CLOSE_SQUARE)
15930 bool non_constant_p;
15933 = cp_parser_constant_expression (parser,
15934 /*allow_non_constant=*/true,
15936 if (!non_constant_p)
15938 /* Normally, the array bound must be an integral constant
15939 expression. However, as an extension, we allow VLAs
15940 in function scopes as long as they aren't part of a
15941 parameter declaration. */
15942 else if (!parser->in_function_body
15943 || current_binding_level->kind == sk_function_parms)
15945 cp_parser_error (parser,
15946 "array bound is not an integer constant");
15947 bounds = error_mark_node;
15949 else if (processing_template_decl && !error_operand_p (bounds))
15951 /* Remember this wasn't a constant-expression. */
15952 bounds = build_nop (TREE_TYPE (bounds), bounds);
15953 TREE_SIDE_EFFECTS (bounds) = 1;
15957 bounds = NULL_TREE;
15958 /* Look for the closing `]'. */
15959 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15961 declarator = cp_error_declarator;
15965 declarator = make_array_declarator (declarator, bounds);
15967 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15970 tree qualifying_scope;
15971 tree unqualified_name;
15972 special_function_kind sfk;
15974 bool pack_expansion_p = false;
15975 cp_token *declarator_id_start_token;
15977 /* Parse a declarator-id */
15978 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15981 cp_parser_parse_tentatively (parser);
15983 /* If we see an ellipsis, we should be looking at a
15985 if (token->type == CPP_ELLIPSIS)
15987 /* Consume the `...' */
15988 cp_lexer_consume_token (parser->lexer);
15990 pack_expansion_p = true;
15994 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15996 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15997 qualifying_scope = parser->scope;
16002 if (!unqualified_name && pack_expansion_p)
16004 /* Check whether an error occurred. */
16005 okay = !cp_parser_error_occurred (parser);
16007 /* We already consumed the ellipsis to mark a
16008 parameter pack, but we have no way to report it,
16009 so abort the tentative parse. We will be exiting
16010 immediately anyway. */
16011 cp_parser_abort_tentative_parse (parser);
16014 okay = cp_parser_parse_definitely (parser);
16017 unqualified_name = error_mark_node;
16018 else if (unqualified_name
16019 && (qualifying_scope
16020 || (TREE_CODE (unqualified_name)
16021 != IDENTIFIER_NODE)))
16023 cp_parser_error (parser, "expected unqualified-id");
16024 unqualified_name = error_mark_node;
16028 if (!unqualified_name)
16030 if (unqualified_name == error_mark_node)
16032 declarator = cp_error_declarator;
16033 pack_expansion_p = false;
16034 declarator->parameter_pack_p = false;
16038 if (qualifying_scope && at_namespace_scope_p ()
16039 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16041 /* In the declaration of a member of a template class
16042 outside of the class itself, the SCOPE will sometimes
16043 be a TYPENAME_TYPE. For example, given:
16045 template <typename T>
16046 int S<T>::R::i = 3;
16048 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
16049 this context, we must resolve S<T>::R to an ordinary
16050 type, rather than a typename type.
16052 The reason we normally avoid resolving TYPENAME_TYPEs
16053 is that a specialization of `S' might render
16054 `S<T>::R' not a type. However, if `S' is
16055 specialized, then this `i' will not be used, so there
16056 is no harm in resolving the types here. */
16059 /* Resolve the TYPENAME_TYPE. */
16060 type = resolve_typename_type (qualifying_scope,
16061 /*only_current_p=*/false);
16062 /* If that failed, the declarator is invalid. */
16063 if (TREE_CODE (type) == TYPENAME_TYPE)
16065 if (typedef_variant_p (type))
16066 error_at (declarator_id_start_token->location,
16067 "cannot define member of dependent typedef "
16070 error_at (declarator_id_start_token->location,
16071 "%<%T::%E%> is not a type",
16072 TYPE_CONTEXT (qualifying_scope),
16073 TYPE_IDENTIFIER (qualifying_scope));
16075 qualifying_scope = type;
16080 if (unqualified_name)
16084 if (qualifying_scope
16085 && CLASS_TYPE_P (qualifying_scope))
16086 class_type = qualifying_scope;
16088 class_type = current_class_type;
16090 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16092 tree name_type = TREE_TYPE (unqualified_name);
16093 if (class_type && same_type_p (name_type, class_type))
16095 if (qualifying_scope
16096 && CLASSTYPE_USE_TEMPLATE (name_type))
16098 error_at (declarator_id_start_token->location,
16099 "invalid use of constructor as a template");
16100 inform (declarator_id_start_token->location,
16101 "use %<%T::%D%> instead of %<%T::%D%> to "
16102 "name the constructor in a qualified name",
16104 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16105 class_type, name_type);
16106 declarator = cp_error_declarator;
16110 unqualified_name = constructor_name (class_type);
16114 /* We do not attempt to print the declarator
16115 here because we do not have enough
16116 information about its original syntactic
16118 cp_parser_error (parser, "invalid declarator");
16119 declarator = cp_error_declarator;
16126 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16127 sfk = sfk_destructor;
16128 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16129 sfk = sfk_conversion;
16130 else if (/* There's no way to declare a constructor
16131 for an anonymous type, even if the type
16132 got a name for linkage purposes. */
16133 !TYPE_WAS_ANONYMOUS (class_type)
16134 && constructor_name_p (unqualified_name,
16137 unqualified_name = constructor_name (class_type);
16138 sfk = sfk_constructor;
16140 else if (is_overloaded_fn (unqualified_name)
16141 && DECL_CONSTRUCTOR_P (get_first_fn
16142 (unqualified_name)))
16143 sfk = sfk_constructor;
16145 if (ctor_dtor_or_conv_p && sfk != sfk_none)
16146 *ctor_dtor_or_conv_p = -1;
16149 declarator = make_id_declarator (qualifying_scope,
16152 declarator->id_loc = token->location;
16153 declarator->parameter_pack_p = pack_expansion_p;
16155 if (pack_expansion_p)
16156 maybe_warn_variadic_templates ();
16159 handle_declarator:;
16160 scope = get_scope_of_declarator (declarator);
16162 /* Any names that appear after the declarator-id for a
16163 member are looked up in the containing scope. */
16164 pushed_scope = push_scope (scope);
16165 parser->in_declarator_p = true;
16166 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16167 || (declarator && declarator->kind == cdk_id))
16168 /* Default args are only allowed on function
16170 parser->default_arg_ok_p = saved_default_arg_ok_p;
16172 parser->default_arg_ok_p = false;
16181 /* For an abstract declarator, we might wind up with nothing at this
16182 point. That's an error; the declarator is not optional. */
16184 cp_parser_error (parser, "expected declarator");
16186 /* If we entered a scope, we must exit it now. */
16188 pop_scope (pushed_scope);
16190 parser->default_arg_ok_p = saved_default_arg_ok_p;
16191 parser->in_declarator_p = saved_in_declarator_p;
16196 /* Parse a ptr-operator.
16199 * cv-qualifier-seq [opt]
16201 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16206 & cv-qualifier-seq [opt]
16208 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16209 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16210 an rvalue reference. In the case of a pointer-to-member, *TYPE is
16211 filled in with the TYPE containing the member. *CV_QUALS is
16212 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16213 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
16214 Note that the tree codes returned by this function have nothing
16215 to do with the types of trees that will be eventually be created
16216 to represent the pointer or reference type being parsed. They are
16217 just constants with suggestive names. */
16218 static enum tree_code
16219 cp_parser_ptr_operator (cp_parser* parser,
16221 cp_cv_quals *cv_quals)
16223 enum tree_code code = ERROR_MARK;
16226 /* Assume that it's not a pointer-to-member. */
16228 /* And that there are no cv-qualifiers. */
16229 *cv_quals = TYPE_UNQUALIFIED;
16231 /* Peek at the next token. */
16232 token = cp_lexer_peek_token (parser->lexer);
16234 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
16235 if (token->type == CPP_MULT)
16236 code = INDIRECT_REF;
16237 else if (token->type == CPP_AND)
16239 else if ((cxx_dialect != cxx98) &&
16240 token->type == CPP_AND_AND) /* C++0x only */
16241 code = NON_LVALUE_EXPR;
16243 if (code != ERROR_MARK)
16245 /* Consume the `*', `&' or `&&'. */
16246 cp_lexer_consume_token (parser->lexer);
16248 /* A `*' can be followed by a cv-qualifier-seq, and so can a
16249 `&', if we are allowing GNU extensions. (The only qualifier
16250 that can legally appear after `&' is `restrict', but that is
16251 enforced during semantic analysis. */
16252 if (code == INDIRECT_REF
16253 || cp_parser_allow_gnu_extensions_p (parser))
16254 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16258 /* Try the pointer-to-member case. */
16259 cp_parser_parse_tentatively (parser);
16260 /* Look for the optional `::' operator. */
16261 cp_parser_global_scope_opt (parser,
16262 /*current_scope_valid_p=*/false);
16263 /* Look for the nested-name specifier. */
16264 token = cp_lexer_peek_token (parser->lexer);
16265 cp_parser_nested_name_specifier (parser,
16266 /*typename_keyword_p=*/false,
16267 /*check_dependency_p=*/true,
16269 /*is_declaration=*/false);
16270 /* If we found it, and the next token is a `*', then we are
16271 indeed looking at a pointer-to-member operator. */
16272 if (!cp_parser_error_occurred (parser)
16273 && cp_parser_require (parser, CPP_MULT, RT_MULT))
16275 /* Indicate that the `*' operator was used. */
16276 code = INDIRECT_REF;
16278 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16279 error_at (token->location, "%qD is a namespace", parser->scope);
16282 /* The type of which the member is a member is given by the
16284 *type = parser->scope;
16285 /* The next name will not be qualified. */
16286 parser->scope = NULL_TREE;
16287 parser->qualifying_scope = NULL_TREE;
16288 parser->object_scope = NULL_TREE;
16289 /* Look for the optional cv-qualifier-seq. */
16290 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16293 /* If that didn't work we don't have a ptr-operator. */
16294 if (!cp_parser_parse_definitely (parser))
16295 cp_parser_error (parser, "expected ptr-operator");
16301 /* Parse an (optional) cv-qualifier-seq.
16304 cv-qualifier cv-qualifier-seq [opt]
16315 Returns a bitmask representing the cv-qualifiers. */
16318 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16320 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16325 cp_cv_quals cv_qualifier;
16327 /* Peek at the next token. */
16328 token = cp_lexer_peek_token (parser->lexer);
16329 /* See if it's a cv-qualifier. */
16330 switch (token->keyword)
16333 cv_qualifier = TYPE_QUAL_CONST;
16337 cv_qualifier = TYPE_QUAL_VOLATILE;
16341 cv_qualifier = TYPE_QUAL_RESTRICT;
16345 cv_qualifier = TYPE_UNQUALIFIED;
16352 if (cv_quals & cv_qualifier)
16354 error_at (token->location, "duplicate cv-qualifier");
16355 cp_lexer_purge_token (parser->lexer);
16359 cp_lexer_consume_token (parser->lexer);
16360 cv_quals |= cv_qualifier;
16367 /* Parse an (optional) virt-specifier-seq.
16369 virt-specifier-seq:
16370 virt-specifier virt-specifier-seq [opt]
16376 Returns a bitmask representing the virt-specifiers. */
16378 static cp_virt_specifiers
16379 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16381 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16386 cp_virt_specifiers virt_specifier;
16388 /* Peek at the next token. */
16389 token = cp_lexer_peek_token (parser->lexer);
16390 /* See if it's a virt-specifier-qualifier. */
16391 if (token->type != CPP_NAME)
16393 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16395 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16396 virt_specifier = VIRT_SPEC_OVERRIDE;
16398 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16400 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16401 virt_specifier = VIRT_SPEC_FINAL;
16403 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16405 virt_specifier = VIRT_SPEC_FINAL;
16410 if (virt_specifiers & virt_specifier)
16412 error_at (token->location, "duplicate virt-specifier");
16413 cp_lexer_purge_token (parser->lexer);
16417 cp_lexer_consume_token (parser->lexer);
16418 virt_specifiers |= virt_specifier;
16421 return virt_specifiers;
16424 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16425 is in scope even though it isn't real. */
16428 inject_this_parameter (tree ctype, cp_cv_quals quals)
16432 if (current_class_ptr)
16434 /* We don't clear this between NSDMIs. Is it already what we want? */
16435 tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16436 if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16437 && cp_type_quals (type) == quals)
16441 this_parm = build_this_parm (ctype, quals);
16442 /* Clear this first to avoid shortcut in cp_build_indirect_ref. */
16443 current_class_ptr = NULL_TREE;
16445 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16446 current_class_ptr = this_parm;
16449 /* Parse a late-specified return type, if any. This is not a separate
16450 non-terminal, but part of a function declarator, which looks like
16452 -> trailing-type-specifier-seq abstract-declarator(opt)
16454 Returns the type indicated by the type-id.
16456 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16460 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16465 /* Peek at the next token. */
16466 token = cp_lexer_peek_token (parser->lexer);
16467 /* A late-specified return type is indicated by an initial '->'. */
16468 if (token->type != CPP_DEREF)
16471 /* Consume the ->. */
16472 cp_lexer_consume_token (parser->lexer);
16476 /* DR 1207: 'this' is in scope in the trailing return type. */
16477 gcc_assert (current_class_ptr == NULL_TREE);
16478 inject_this_parameter (current_class_type, quals);
16481 type = cp_parser_trailing_type_id (parser);
16484 current_class_ptr = current_class_ref = NULL_TREE;
16489 /* Parse a declarator-id.
16493 :: [opt] nested-name-specifier [opt] type-name
16495 In the `id-expression' case, the value returned is as for
16496 cp_parser_id_expression if the id-expression was an unqualified-id.
16497 If the id-expression was a qualified-id, then a SCOPE_REF is
16498 returned. The first operand is the scope (either a NAMESPACE_DECL
16499 or TREE_TYPE), but the second is still just a representation of an
16503 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16506 /* The expression must be an id-expression. Assume that qualified
16507 names are the names of types so that:
16510 int S<T>::R::i = 3;
16512 will work; we must treat `S<T>::R' as the name of a type.
16513 Similarly, assume that qualified names are templates, where
16517 int S<T>::R<T>::i = 3;
16520 id = cp_parser_id_expression (parser,
16521 /*template_keyword_p=*/false,
16522 /*check_dependency_p=*/false,
16523 /*template_p=*/NULL,
16524 /*declarator_p=*/true,
16526 if (id && BASELINK_P (id))
16527 id = BASELINK_FUNCTIONS (id);
16531 /* Parse a type-id.
16534 type-specifier-seq abstract-declarator [opt]
16536 Returns the TYPE specified. */
16539 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16540 bool is_trailing_return)
16542 cp_decl_specifier_seq type_specifier_seq;
16543 cp_declarator *abstract_declarator;
16545 /* Parse the type-specifier-seq. */
16546 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16547 is_trailing_return,
16548 &type_specifier_seq);
16549 if (type_specifier_seq.type == error_mark_node)
16550 return error_mark_node;
16552 /* There might or might not be an abstract declarator. */
16553 cp_parser_parse_tentatively (parser);
16554 /* Look for the declarator. */
16555 abstract_declarator
16556 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16557 /*parenthesized_p=*/NULL,
16558 /*member_p=*/false);
16559 /* Check to see if there really was a declarator. */
16560 if (!cp_parser_parse_definitely (parser))
16561 abstract_declarator = NULL;
16563 if (type_specifier_seq.type
16564 && type_uses_auto (type_specifier_seq.type))
16566 /* A type-id with type 'auto' is only ok if the abstract declarator
16567 is a function declarator with a late-specified return type. */
16568 if (abstract_declarator
16569 && abstract_declarator->kind == cdk_function
16570 && abstract_declarator->u.function.late_return_type)
16574 error ("invalid use of %<auto%>");
16575 return error_mark_node;
16579 return groktypename (&type_specifier_seq, abstract_declarator,
16583 static tree cp_parser_type_id (cp_parser *parser)
16585 return cp_parser_type_id_1 (parser, false, false);
16588 static tree cp_parser_template_type_arg (cp_parser *parser)
16591 const char *saved_message = parser->type_definition_forbidden_message;
16592 parser->type_definition_forbidden_message
16593 = G_("types may not be defined in template arguments");
16594 r = cp_parser_type_id_1 (parser, true, false);
16595 parser->type_definition_forbidden_message = saved_message;
16599 static tree cp_parser_trailing_type_id (cp_parser *parser)
16601 return cp_parser_type_id_1 (parser, false, true);
16604 /* Parse a type-specifier-seq.
16606 type-specifier-seq:
16607 type-specifier type-specifier-seq [opt]
16611 type-specifier-seq:
16612 attributes type-specifier-seq [opt]
16614 If IS_DECLARATION is true, we are at the start of a "condition" or
16615 exception-declaration, so we might be followed by a declarator-id.
16617 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16618 i.e. we've just seen "->".
16620 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
16623 cp_parser_type_specifier_seq (cp_parser* parser,
16624 bool is_declaration,
16625 bool is_trailing_return,
16626 cp_decl_specifier_seq *type_specifier_seq)
16628 bool seen_type_specifier = false;
16629 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16630 cp_token *start_token = NULL;
16632 /* Clear the TYPE_SPECIFIER_SEQ. */
16633 clear_decl_specs (type_specifier_seq);
16635 /* In the context of a trailing return type, enum E { } is an
16636 elaborated-type-specifier followed by a function-body, not an
16638 if (is_trailing_return)
16639 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16641 /* Parse the type-specifiers and attributes. */
16644 tree type_specifier;
16645 bool is_cv_qualifier;
16647 /* Check for attributes first. */
16648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16650 type_specifier_seq->attributes =
16651 chainon (type_specifier_seq->attributes,
16652 cp_parser_attributes_opt (parser));
16656 /* record the token of the beginning of the type specifier seq,
16657 for error reporting purposes*/
16659 start_token = cp_lexer_peek_token (parser->lexer);
16661 /* Look for the type-specifier. */
16662 type_specifier = cp_parser_type_specifier (parser,
16664 type_specifier_seq,
16665 /*is_declaration=*/false,
16668 if (!type_specifier)
16670 /* If the first type-specifier could not be found, this is not a
16671 type-specifier-seq at all. */
16672 if (!seen_type_specifier)
16674 cp_parser_error (parser, "expected type-specifier");
16675 type_specifier_seq->type = error_mark_node;
16678 /* If subsequent type-specifiers could not be found, the
16679 type-specifier-seq is complete. */
16683 seen_type_specifier = true;
16684 /* The standard says that a condition can be:
16686 type-specifier-seq declarator = assignment-expression
16693 we should treat the "S" as a declarator, not as a
16694 type-specifier. The standard doesn't say that explicitly for
16695 type-specifier-seq, but it does say that for
16696 decl-specifier-seq in an ordinary declaration. Perhaps it
16697 would be clearer just to allow a decl-specifier-seq here, and
16698 then add a semantic restriction that if any decl-specifiers
16699 that are not type-specifiers appear, the program is invalid. */
16700 if (is_declaration && !is_cv_qualifier)
16701 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16704 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16707 /* Parse a parameter-declaration-clause.
16709 parameter-declaration-clause:
16710 parameter-declaration-list [opt] ... [opt]
16711 parameter-declaration-list , ...
16713 Returns a representation for the parameter declarations. A return
16714 value of NULL indicates a parameter-declaration-clause consisting
16715 only of an ellipsis. */
16718 cp_parser_parameter_declaration_clause (cp_parser* parser)
16725 /* Peek at the next token. */
16726 token = cp_lexer_peek_token (parser->lexer);
16727 /* Check for trivial parameter-declaration-clauses. */
16728 if (token->type == CPP_ELLIPSIS)
16730 /* Consume the `...' token. */
16731 cp_lexer_consume_token (parser->lexer);
16734 else if (token->type == CPP_CLOSE_PAREN)
16735 /* There are no parameters. */
16737 #ifndef NO_IMPLICIT_EXTERN_C
16738 if (in_system_header && current_class_type == NULL
16739 && current_lang_name == lang_name_c)
16743 return void_list_node;
16745 /* Check for `(void)', too, which is a special case. */
16746 else if (token->keyword == RID_VOID
16747 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16748 == CPP_CLOSE_PAREN))
16750 /* Consume the `void' token. */
16751 cp_lexer_consume_token (parser->lexer);
16752 /* There are no parameters. */
16753 return void_list_node;
16756 /* Parse the parameter-declaration-list. */
16757 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16758 /* If a parse error occurred while parsing the
16759 parameter-declaration-list, then the entire
16760 parameter-declaration-clause is erroneous. */
16764 /* Peek at the next token. */
16765 token = cp_lexer_peek_token (parser->lexer);
16766 /* If it's a `,', the clause should terminate with an ellipsis. */
16767 if (token->type == CPP_COMMA)
16769 /* Consume the `,'. */
16770 cp_lexer_consume_token (parser->lexer);
16771 /* Expect an ellipsis. */
16773 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16775 /* It might also be `...' if the optional trailing `,' was
16777 else if (token->type == CPP_ELLIPSIS)
16779 /* Consume the `...' token. */
16780 cp_lexer_consume_token (parser->lexer);
16781 /* And remember that we saw it. */
16785 ellipsis_p = false;
16787 /* Finish the parameter list. */
16789 parameters = chainon (parameters, void_list_node);
16794 /* Parse a parameter-declaration-list.
16796 parameter-declaration-list:
16797 parameter-declaration
16798 parameter-declaration-list , parameter-declaration
16800 Returns a representation of the parameter-declaration-list, as for
16801 cp_parser_parameter_declaration_clause. However, the
16802 `void_list_node' is never appended to the list. Upon return,
16803 *IS_ERROR will be true iff an error occurred. */
16806 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16808 tree parameters = NULL_TREE;
16809 tree *tail = ¶meters;
16810 bool saved_in_unbraced_linkage_specification_p;
16813 /* Assume all will go well. */
16815 /* The special considerations that apply to a function within an
16816 unbraced linkage specifications do not apply to the parameters
16817 to the function. */
16818 saved_in_unbraced_linkage_specification_p
16819 = parser->in_unbraced_linkage_specification_p;
16820 parser->in_unbraced_linkage_specification_p = false;
16822 /* Look for more parameters. */
16825 cp_parameter_declarator *parameter;
16826 tree decl = error_mark_node;
16827 bool parenthesized_p = false;
16828 /* Parse the parameter. */
16830 = cp_parser_parameter_declaration (parser,
16831 /*template_parm_p=*/false,
16834 /* We don't know yet if the enclosing context is deprecated, so wait
16835 and warn in grokparms if appropriate. */
16836 deprecated_state = DEPRECATED_SUPPRESS;
16839 decl = grokdeclarator (parameter->declarator,
16840 ¶meter->decl_specifiers,
16842 parameter->default_argument != NULL_TREE,
16843 ¶meter->decl_specifiers.attributes);
16845 deprecated_state = DEPRECATED_NORMAL;
16847 /* If a parse error occurred parsing the parameter declaration,
16848 then the entire parameter-declaration-list is erroneous. */
16849 if (decl == error_mark_node)
16852 parameters = error_mark_node;
16856 if (parameter->decl_specifiers.attributes)
16857 cplus_decl_attributes (&decl,
16858 parameter->decl_specifiers.attributes,
16860 if (DECL_NAME (decl))
16861 decl = pushdecl (decl);
16863 if (decl != error_mark_node)
16865 retrofit_lang_decl (decl);
16866 DECL_PARM_INDEX (decl) = ++index;
16867 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16870 /* Add the new parameter to the list. */
16871 *tail = build_tree_list (parameter->default_argument, decl);
16872 tail = &TREE_CHAIN (*tail);
16874 /* Peek at the next token. */
16875 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16876 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16877 /* These are for Objective-C++ */
16878 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16879 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16880 /* The parameter-declaration-list is complete. */
16882 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16886 /* Peek at the next token. */
16887 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16888 /* If it's an ellipsis, then the list is complete. */
16889 if (token->type == CPP_ELLIPSIS)
16891 /* Otherwise, there must be more parameters. Consume the
16893 cp_lexer_consume_token (parser->lexer);
16894 /* When parsing something like:
16896 int i(float f, double d)
16898 we can tell after seeing the declaration for "f" that we
16899 are not looking at an initialization of a variable "i",
16900 but rather at the declaration of a function "i".
16902 Due to the fact that the parsing of template arguments
16903 (as specified to a template-id) requires backtracking we
16904 cannot use this technique when inside a template argument
16906 if (!parser->in_template_argument_list_p
16907 && !parser->in_type_id_in_expr_p
16908 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16909 /* However, a parameter-declaration of the form
16910 "foat(f)" (which is a valid declaration of a
16911 parameter "f") can also be interpreted as an
16912 expression (the conversion of "f" to "float"). */
16913 && !parenthesized_p)
16914 cp_parser_commit_to_tentative_parse (parser);
16918 cp_parser_error (parser, "expected %<,%> or %<...%>");
16919 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16920 cp_parser_skip_to_closing_parenthesis (parser,
16921 /*recovering=*/true,
16922 /*or_comma=*/false,
16923 /*consume_paren=*/false);
16928 parser->in_unbraced_linkage_specification_p
16929 = saved_in_unbraced_linkage_specification_p;
16934 /* Parse a parameter declaration.
16936 parameter-declaration:
16937 decl-specifier-seq ... [opt] declarator
16938 decl-specifier-seq declarator = assignment-expression
16939 decl-specifier-seq ... [opt] abstract-declarator [opt]
16940 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16942 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16943 declares a template parameter. (In that case, a non-nested `>'
16944 token encountered during the parsing of the assignment-expression
16945 is not interpreted as a greater-than operator.)
16947 Returns a representation of the parameter, or NULL if an error
16948 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16949 true iff the declarator is of the form "(p)". */
16951 static cp_parameter_declarator *
16952 cp_parser_parameter_declaration (cp_parser *parser,
16953 bool template_parm_p,
16954 bool *parenthesized_p)
16956 int declares_class_or_enum;
16957 cp_decl_specifier_seq decl_specifiers;
16958 cp_declarator *declarator;
16959 tree default_argument;
16960 cp_token *token = NULL, *declarator_token_start = NULL;
16961 const char *saved_message;
16963 /* In a template parameter, `>' is not an operator.
16967 When parsing a default template-argument for a non-type
16968 template-parameter, the first non-nested `>' is taken as the end
16969 of the template parameter-list rather than a greater-than
16972 /* Type definitions may not appear in parameter types. */
16973 saved_message = parser->type_definition_forbidden_message;
16974 parser->type_definition_forbidden_message
16975 = G_("types may not be defined in parameter types");
16977 /* Parse the declaration-specifiers. */
16978 cp_parser_decl_specifier_seq (parser,
16979 CP_PARSER_FLAGS_NONE,
16981 &declares_class_or_enum);
16983 /* Complain about missing 'typename' or other invalid type names. */
16984 if (!decl_specifiers.any_type_specifiers_p)
16985 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16987 /* If an error occurred, there's no reason to attempt to parse the
16988 rest of the declaration. */
16989 if (cp_parser_error_occurred (parser))
16991 parser->type_definition_forbidden_message = saved_message;
16995 /* Peek at the next token. */
16996 token = cp_lexer_peek_token (parser->lexer);
16998 /* If the next token is a `)', `,', `=', `>', or `...', then there
16999 is no declarator. However, when variadic templates are enabled,
17000 there may be a declarator following `...'. */
17001 if (token->type == CPP_CLOSE_PAREN
17002 || token->type == CPP_COMMA
17003 || token->type == CPP_EQ
17004 || token->type == CPP_GREATER)
17007 if (parenthesized_p)
17008 *parenthesized_p = false;
17010 /* Otherwise, there should be a declarator. */
17013 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17014 parser->default_arg_ok_p = false;
17016 /* After seeing a decl-specifier-seq, if the next token is not a
17017 "(", there is no possibility that the code is a valid
17018 expression. Therefore, if parsing tentatively, we commit at
17020 if (!parser->in_template_argument_list_p
17021 /* In an expression context, having seen:
17025 we cannot be sure whether we are looking at a
17026 function-type (taking a "char" as a parameter) or a cast
17027 of some object of type "char" to "int". */
17028 && !parser->in_type_id_in_expr_p
17029 && cp_parser_uncommitted_to_tentative_parse_p (parser)
17030 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17031 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17032 cp_parser_commit_to_tentative_parse (parser);
17033 /* Parse the declarator. */
17034 declarator_token_start = token;
17035 declarator = cp_parser_declarator (parser,
17036 CP_PARSER_DECLARATOR_EITHER,
17037 /*ctor_dtor_or_conv_p=*/NULL,
17039 /*member_p=*/false);
17040 parser->default_arg_ok_p = saved_default_arg_ok_p;
17041 /* After the declarator, allow more attributes. */
17042 decl_specifiers.attributes
17043 = chainon (decl_specifiers.attributes,
17044 cp_parser_attributes_opt (parser));
17047 /* If the next token is an ellipsis, and we have not seen a
17048 declarator name, and the type of the declarator contains parameter
17049 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17050 a parameter pack expansion expression. Otherwise, leave the
17051 ellipsis for a C-style variadic function. */
17052 token = cp_lexer_peek_token (parser->lexer);
17053 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17055 tree type = decl_specifiers.type;
17057 if (type && DECL_P (type))
17058 type = TREE_TYPE (type);
17061 && TREE_CODE (type) != TYPE_PACK_EXPANSION
17062 && declarator_can_be_parameter_pack (declarator)
17063 && (!declarator || !declarator->parameter_pack_p)
17064 && uses_parameter_packs (type))
17066 /* Consume the `...'. */
17067 cp_lexer_consume_token (parser->lexer);
17068 maybe_warn_variadic_templates ();
17070 /* Build a pack expansion type */
17072 declarator->parameter_pack_p = true;
17074 decl_specifiers.type = make_pack_expansion (type);
17078 /* The restriction on defining new types applies only to the type
17079 of the parameter, not to the default argument. */
17080 parser->type_definition_forbidden_message = saved_message;
17082 /* If the next token is `=', then process a default argument. */
17083 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17085 /* If we are defining a class, then the tokens that make up the
17086 default argument must be saved and processed later. */
17087 if (!template_parm_p && at_class_scope_p ()
17088 && TYPE_BEING_DEFINED (current_class_type)
17089 && !LAMBDA_TYPE_P (current_class_type))
17091 unsigned depth = 0;
17092 int maybe_template_id = 0;
17093 cp_token *first_token;
17096 /* Add tokens until we have processed the entire default
17097 argument. We add the range [first_token, token). */
17098 first_token = cp_lexer_peek_token (parser->lexer);
17103 /* Peek at the next token. */
17104 token = cp_lexer_peek_token (parser->lexer);
17105 /* What we do depends on what token we have. */
17106 switch (token->type)
17108 /* In valid code, a default argument must be
17109 immediately followed by a `,' `)', or `...'. */
17111 if (depth == 0 && maybe_template_id)
17113 /* If we've seen a '<', we might be in a
17114 template-argument-list. Until Core issue 325 is
17115 resolved, we don't know how this situation ought
17116 to be handled, so try to DTRT. We check whether
17117 what comes after the comma is a valid parameter
17118 declaration list. If it is, then the comma ends
17119 the default argument; otherwise the default
17120 argument continues. */
17121 bool error = false;
17124 /* Set ITALP so cp_parser_parameter_declaration_list
17125 doesn't decide to commit to this parse. */
17126 bool saved_italp = parser->in_template_argument_list_p;
17127 parser->in_template_argument_list_p = true;
17129 cp_parser_parse_tentatively (parser);
17130 cp_lexer_consume_token (parser->lexer);
17131 begin_scope (sk_function_parms, NULL_TREE);
17132 cp_parser_parameter_declaration_list (parser, &error);
17133 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17134 pop_binding (DECL_NAME (t), t);
17136 if (!cp_parser_error_occurred (parser) && !error)
17138 cp_parser_abort_tentative_parse (parser);
17140 parser->in_template_argument_list_p = saved_italp;
17143 case CPP_CLOSE_PAREN:
17145 /* If we run into a non-nested `;', `}', or `]',
17146 then the code is invalid -- but the default
17147 argument is certainly over. */
17148 case CPP_SEMICOLON:
17149 case CPP_CLOSE_BRACE:
17150 case CPP_CLOSE_SQUARE:
17153 /* Update DEPTH, if necessary. */
17154 else if (token->type == CPP_CLOSE_PAREN
17155 || token->type == CPP_CLOSE_BRACE
17156 || token->type == CPP_CLOSE_SQUARE)
17160 case CPP_OPEN_PAREN:
17161 case CPP_OPEN_SQUARE:
17162 case CPP_OPEN_BRACE:
17168 /* This might be the comparison operator, or it might
17169 start a template argument list. */
17170 ++maybe_template_id;
17174 if (cxx_dialect == cxx98)
17176 /* Fall through for C++0x, which treats the `>>'
17177 operator like two `>' tokens in certain
17183 /* This might be an operator, or it might close a
17184 template argument list. But if a previous '<'
17185 started a template argument list, this will have
17186 closed it, so we can't be in one anymore. */
17187 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17188 if (maybe_template_id < 0)
17189 maybe_template_id = 0;
17193 /* If we run out of tokens, issue an error message. */
17195 case CPP_PRAGMA_EOL:
17196 error_at (token->location, "file ends in default argument");
17202 /* In these cases, we should look for template-ids.
17203 For example, if the default argument is
17204 `X<int, double>()', we need to do name lookup to
17205 figure out whether or not `X' is a template; if
17206 so, the `,' does not end the default argument.
17208 That is not yet done. */
17215 /* If we've reached the end, stop. */
17219 /* Add the token to the token block. */
17220 token = cp_lexer_consume_token (parser->lexer);
17223 /* Create a DEFAULT_ARG to represent the unparsed default
17225 default_argument = make_node (DEFAULT_ARG);
17226 DEFARG_TOKENS (default_argument)
17227 = cp_token_cache_new (first_token, token);
17228 DEFARG_INSTANTIATIONS (default_argument) = NULL;
17230 /* Outside of a class definition, we can just parse the
17231 assignment-expression. */
17234 token = cp_lexer_peek_token (parser->lexer);
17236 = cp_parser_default_argument (parser, template_parm_p);
17239 if (!parser->default_arg_ok_p)
17241 if (flag_permissive)
17242 warning (0, "deprecated use of default argument for parameter of non-function");
17245 error_at (token->location,
17246 "default arguments are only "
17247 "permitted for function parameters");
17248 default_argument = NULL_TREE;
17251 else if ((declarator && declarator->parameter_pack_p)
17252 || (decl_specifiers.type
17253 && PACK_EXPANSION_P (decl_specifiers.type)))
17255 /* Find the name of the parameter pack. */
17256 cp_declarator *id_declarator = declarator;
17257 while (id_declarator && id_declarator->kind != cdk_id)
17258 id_declarator = id_declarator->declarator;
17260 if (id_declarator && id_declarator->kind == cdk_id)
17261 error_at (declarator_token_start->location,
17263 ? G_("template parameter pack %qD "
17264 "cannot have a default argument")
17265 : G_("parameter pack %qD cannot have "
17266 "a default argument"),
17267 id_declarator->u.id.unqualified_name);
17269 error_at (declarator_token_start->location,
17271 ? G_("template parameter pack cannot have "
17272 "a default argument")
17273 : G_("parameter pack cannot have a "
17274 "default argument"));
17276 default_argument = NULL_TREE;
17280 default_argument = NULL_TREE;
17282 return make_parameter_declarator (&decl_specifiers,
17287 /* Parse a default argument and return it.
17289 TEMPLATE_PARM_P is true if this is a default argument for a
17290 non-type template parameter. */
17292 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17294 tree default_argument = NULL_TREE;
17295 bool saved_greater_than_is_operator_p;
17296 bool saved_local_variables_forbidden_p;
17297 bool non_constant_p, is_direct_init;
17299 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17301 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17302 parser->greater_than_is_operator_p = !template_parm_p;
17303 /* Local variable names (and the `this' keyword) may not
17304 appear in a default argument. */
17305 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17306 parser->local_variables_forbidden_p = true;
17307 /* Parse the assignment-expression. */
17308 if (template_parm_p)
17309 push_deferring_access_checks (dk_no_deferred);
17311 = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17312 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17313 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17314 if (template_parm_p)
17315 pop_deferring_access_checks ();
17316 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17317 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17319 return default_argument;
17322 /* Parse a function-body.
17325 compound_statement */
17328 cp_parser_function_body (cp_parser *parser)
17330 cp_parser_compound_statement (parser, NULL, false, true);
17333 /* Parse a ctor-initializer-opt followed by a function-body. Return
17334 true if a ctor-initializer was present. */
17337 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17340 bool ctor_initializer_p;
17341 const bool check_body_p =
17342 DECL_CONSTRUCTOR_P (current_function_decl)
17343 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17346 /* Begin the function body. */
17347 body = begin_function_body ();
17348 /* Parse the optional ctor-initializer. */
17349 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17351 /* If we're parsing a constexpr constructor definition, we need
17352 to check that the constructor body is indeed empty. However,
17353 before we get to cp_parser_function_body lot of junk has been
17354 generated, so we can't just check that we have an empty block.
17355 Rather we take a snapshot of the outermost block, and check whether
17356 cp_parser_function_body changed its state. */
17360 if (TREE_CODE (list) == BIND_EXPR)
17361 list = BIND_EXPR_BODY (list);
17362 if (TREE_CODE (list) == STATEMENT_LIST
17363 && STATEMENT_LIST_TAIL (list) != NULL)
17364 last = STATEMENT_LIST_TAIL (list)->stmt;
17366 /* Parse the function-body. */
17367 cp_parser_function_body (parser);
17369 check_constexpr_ctor_body (last, list);
17370 /* Finish the function body. */
17371 finish_function_body (body);
17373 return ctor_initializer_p;
17376 /* Parse an initializer.
17379 = initializer-clause
17380 ( expression-list )
17382 Returns an expression representing the initializer. If no
17383 initializer is present, NULL_TREE is returned.
17385 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17386 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
17387 set to TRUE if there is no initializer present. If there is an
17388 initializer, and it is not a constant-expression, *NON_CONSTANT_P
17389 is set to true; otherwise it is set to false. */
17392 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17393 bool* non_constant_p)
17398 /* Peek at the next token. */
17399 token = cp_lexer_peek_token (parser->lexer);
17401 /* Let our caller know whether or not this initializer was
17403 *is_direct_init = (token->type != CPP_EQ);
17404 /* Assume that the initializer is constant. */
17405 *non_constant_p = false;
17407 if (token->type == CPP_EQ)
17409 /* Consume the `='. */
17410 cp_lexer_consume_token (parser->lexer);
17411 /* Parse the initializer-clause. */
17412 init = cp_parser_initializer_clause (parser, non_constant_p);
17414 else if (token->type == CPP_OPEN_PAREN)
17417 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17419 /*allow_expansion_p=*/true,
17422 return error_mark_node;
17423 init = build_tree_list_vec (vec);
17424 release_tree_vector (vec);
17426 else if (token->type == CPP_OPEN_BRACE)
17428 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17429 init = cp_parser_braced_list (parser, non_constant_p);
17430 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17434 /* Anything else is an error. */
17435 cp_parser_error (parser, "expected initializer");
17436 init = error_mark_node;
17442 /* Parse an initializer-clause.
17444 initializer-clause:
17445 assignment-expression
17448 Returns an expression representing the initializer.
17450 If the `assignment-expression' production is used the value
17451 returned is simply a representation for the expression.
17453 Otherwise, calls cp_parser_braced_list. */
17456 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17460 /* Assume the expression is constant. */
17461 *non_constant_p = false;
17463 /* If it is not a `{', then we are looking at an
17464 assignment-expression. */
17465 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17468 = cp_parser_constant_expression (parser,
17469 /*allow_non_constant_p=*/true,
17473 initializer = cp_parser_braced_list (parser, non_constant_p);
17475 return initializer;
17478 /* Parse a brace-enclosed initializer list.
17481 { initializer-list , [opt] }
17484 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
17485 the elements of the initializer-list (or NULL, if the last
17486 production is used). The TREE_TYPE for the CONSTRUCTOR will be
17487 NULL_TREE. There is no way to detect whether or not the optional
17488 trailing `,' was provided. NON_CONSTANT_P is as for
17489 cp_parser_initializer. */
17492 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17496 /* Consume the `{' token. */
17497 cp_lexer_consume_token (parser->lexer);
17498 /* Create a CONSTRUCTOR to represent the braced-initializer. */
17499 initializer = make_node (CONSTRUCTOR);
17500 /* If it's not a `}', then there is a non-trivial initializer. */
17501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17503 /* Parse the initializer list. */
17504 CONSTRUCTOR_ELTS (initializer)
17505 = cp_parser_initializer_list (parser, non_constant_p);
17506 /* A trailing `,' token is allowed. */
17507 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17508 cp_lexer_consume_token (parser->lexer);
17510 /* Now, there should be a trailing `}'. */
17511 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17512 TREE_TYPE (initializer) = init_list_type_node;
17513 return initializer;
17516 /* Parse an initializer-list.
17519 initializer-clause ... [opt]
17520 initializer-list , initializer-clause ... [opt]
17525 designation initializer-clause ...[opt]
17526 initializer-list , designation initializer-clause ...[opt]
17531 [ constant-expression ] =
17533 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
17534 for the initializer. If the INDEX of the elt is non-NULL, it is the
17535 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
17536 as for cp_parser_initializer. */
17538 static VEC(constructor_elt,gc) *
17539 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17541 VEC(constructor_elt,gc) *v = NULL;
17543 /* Assume all of the expressions are constant. */
17544 *non_constant_p = false;
17546 /* Parse the rest of the list. */
17552 bool clause_non_constant_p;
17554 /* If the next token is an identifier and the following one is a
17555 colon, we are looking at the GNU designated-initializer
17557 if (cp_parser_allow_gnu_extensions_p (parser)
17558 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17559 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17561 /* Warn the user that they are using an extension. */
17562 pedwarn (input_location, OPT_pedantic,
17563 "ISO C++ does not allow designated initializers");
17564 /* Consume the identifier. */
17565 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17566 /* Consume the `:'. */
17567 cp_lexer_consume_token (parser->lexer);
17569 /* Also handle the C99 syntax, '. id ='. */
17570 else if (cp_parser_allow_gnu_extensions_p (parser)
17571 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17572 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17573 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17575 /* Warn the user that they are using an extension. */
17576 pedwarn (input_location, OPT_pedantic,
17577 "ISO C++ does not allow C99 designated initializers");
17578 /* Consume the `.'. */
17579 cp_lexer_consume_token (parser->lexer);
17580 /* Consume the identifier. */
17581 designator = cp_lexer_consume_token (parser->lexer)->u.value;
17582 /* Consume the `='. */
17583 cp_lexer_consume_token (parser->lexer);
17585 /* Also handle C99 array designators, '[ const ] ='. */
17586 else if (cp_parser_allow_gnu_extensions_p (parser)
17587 && !c_dialect_objc ()
17588 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17590 /* In C++11, [ could start a lambda-introducer. */
17591 cp_parser_parse_tentatively (parser);
17592 cp_lexer_consume_token (parser->lexer);
17593 designator = cp_parser_constant_expression (parser, false, NULL);
17594 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17595 cp_parser_require (parser, CPP_EQ, RT_EQ);
17596 cp_parser_parse_definitely (parser);
17599 designator = NULL_TREE;
17601 /* Parse the initializer. */
17602 initializer = cp_parser_initializer_clause (parser,
17603 &clause_non_constant_p);
17604 /* If any clause is non-constant, so is the entire initializer. */
17605 if (clause_non_constant_p)
17606 *non_constant_p = true;
17608 /* If we have an ellipsis, this is an initializer pack
17610 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17612 /* Consume the `...'. */
17613 cp_lexer_consume_token (parser->lexer);
17615 /* Turn the initializer into an initializer expansion. */
17616 initializer = make_pack_expansion (initializer);
17619 /* Add it to the vector. */
17620 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17622 /* If the next token is not a comma, we have reached the end of
17624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17627 /* Peek at the next token. */
17628 token = cp_lexer_peek_nth_token (parser->lexer, 2);
17629 /* If the next token is a `}', then we're still done. An
17630 initializer-clause can have a trailing `,' after the
17631 initializer-list and before the closing `}'. */
17632 if (token->type == CPP_CLOSE_BRACE)
17635 /* Consume the `,' token. */
17636 cp_lexer_consume_token (parser->lexer);
17642 /* Classes [gram.class] */
17644 /* Parse a class-name.
17650 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17651 to indicate that names looked up in dependent types should be
17652 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
17653 keyword has been used to indicate that the name that appears next
17654 is a template. TAG_TYPE indicates the explicit tag given before
17655 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
17656 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
17657 is the class being defined in a class-head.
17659 Returns the TYPE_DECL representing the class. */
17662 cp_parser_class_name (cp_parser *parser,
17663 bool typename_keyword_p,
17664 bool template_keyword_p,
17665 enum tag_types tag_type,
17666 bool check_dependency_p,
17668 bool is_declaration)
17674 tree identifier = NULL_TREE;
17676 /* All class-names start with an identifier. */
17677 token = cp_lexer_peek_token (parser->lexer);
17678 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17680 cp_parser_error (parser, "expected class-name");
17681 return error_mark_node;
17684 /* PARSER->SCOPE can be cleared when parsing the template-arguments
17685 to a template-id, so we save it here. */
17686 scope = parser->scope;
17687 if (scope == error_mark_node)
17688 return error_mark_node;
17690 /* Any name names a type if we're following the `typename' keyword
17691 in a qualified name where the enclosing scope is type-dependent. */
17692 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17693 && dependent_type_p (scope));
17694 /* Handle the common case (an identifier, but not a template-id)
17696 if (token->type == CPP_NAME
17697 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17699 cp_token *identifier_token;
17702 /* Look for the identifier. */
17703 identifier_token = cp_lexer_peek_token (parser->lexer);
17704 ambiguous_p = identifier_token->ambiguous_p;
17705 identifier = cp_parser_identifier (parser);
17706 /* If the next token isn't an identifier, we are certainly not
17707 looking at a class-name. */
17708 if (identifier == error_mark_node)
17709 decl = error_mark_node;
17710 /* If we know this is a type-name, there's no need to look it
17712 else if (typename_p)
17716 tree ambiguous_decls;
17717 /* If we already know that this lookup is ambiguous, then
17718 we've already issued an error message; there's no reason
17722 cp_parser_simulate_error (parser);
17723 return error_mark_node;
17725 /* If the next token is a `::', then the name must be a type
17728 [basic.lookup.qual]
17730 During the lookup for a name preceding the :: scope
17731 resolution operator, object, function, and enumerator
17732 names are ignored. */
17733 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17734 tag_type = typename_type;
17735 /* Look up the name. */
17736 decl = cp_parser_lookup_name (parser, identifier,
17738 /*is_template=*/false,
17739 /*is_namespace=*/false,
17740 check_dependency_p,
17742 identifier_token->location);
17743 if (ambiguous_decls)
17745 if (cp_parser_parsing_tentatively (parser))
17746 cp_parser_simulate_error (parser);
17747 return error_mark_node;
17753 /* Try a template-id. */
17754 decl = cp_parser_template_id (parser, template_keyword_p,
17755 check_dependency_p,
17757 if (decl == error_mark_node)
17758 return error_mark_node;
17761 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17763 /* If this is a typename, create a TYPENAME_TYPE. */
17764 if (typename_p && decl != error_mark_node)
17766 decl = make_typename_type (scope, decl, typename_type,
17767 /*complain=*/tf_error);
17768 if (decl != error_mark_node)
17769 decl = TYPE_NAME (decl);
17772 /* Check to see that it is really the name of a class. */
17773 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17774 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17775 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17776 /* Situations like this:
17778 template <typename T> struct A {
17779 typename T::template X<int>::I i;
17782 are problematic. Is `T::template X<int>' a class-name? The
17783 standard does not seem to be definitive, but there is no other
17784 valid interpretation of the following `::'. Therefore, those
17785 names are considered class-names. */
17787 decl = make_typename_type (scope, decl, tag_type, tf_error);
17788 if (decl != error_mark_node)
17789 decl = TYPE_NAME (decl);
17791 else if (TREE_CODE (decl) != TYPE_DECL
17792 || TREE_TYPE (decl) == error_mark_node
17793 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17794 /* In Objective-C 2.0, a classname followed by '.' starts a
17795 dot-syntax expression, and it's not a type-name. */
17796 || (c_dialect_objc ()
17797 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17798 && objc_is_class_name (decl)))
17799 decl = error_mark_node;
17801 if (decl == error_mark_node)
17802 cp_parser_error (parser, "expected class-name");
17803 else if (identifier && !parser->scope)
17804 maybe_note_name_used_in_class (identifier, decl);
17809 /* Parse a class-specifier.
17812 class-head { member-specification [opt] }
17814 Returns the TREE_TYPE representing the class. */
17817 cp_parser_class_specifier_1 (cp_parser* parser)
17820 tree attributes = NULL_TREE;
17821 bool nested_name_specifier_p;
17822 unsigned saved_num_template_parameter_lists;
17823 bool saved_in_function_body;
17824 unsigned char in_statement;
17825 bool in_switch_statement_p;
17826 bool saved_in_unbraced_linkage_specification_p;
17827 tree old_scope = NULL_TREE;
17828 tree scope = NULL_TREE;
17830 cp_token *closing_brace;
17832 push_deferring_access_checks (dk_no_deferred);
17834 /* Parse the class-head. */
17835 type = cp_parser_class_head (parser,
17836 &nested_name_specifier_p,
17839 /* If the class-head was a semantic disaster, skip the entire body
17843 cp_parser_skip_to_end_of_block_or_statement (parser);
17844 pop_deferring_access_checks ();
17845 return error_mark_node;
17848 /* Look for the `{'. */
17849 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17851 pop_deferring_access_checks ();
17852 return error_mark_node;
17855 /* Process the base classes. If they're invalid, skip the
17856 entire class body. */
17857 if (!xref_basetypes (type, bases))
17859 /* Consuming the closing brace yields better error messages
17861 if (cp_parser_skip_to_closing_brace (parser))
17862 cp_lexer_consume_token (parser->lexer);
17863 pop_deferring_access_checks ();
17864 return error_mark_node;
17867 /* Issue an error message if type-definitions are forbidden here. */
17868 cp_parser_check_type_definition (parser);
17869 /* Remember that we are defining one more class. */
17870 ++parser->num_classes_being_defined;
17871 /* Inside the class, surrounding template-parameter-lists do not
17873 saved_num_template_parameter_lists
17874 = parser->num_template_parameter_lists;
17875 parser->num_template_parameter_lists = 0;
17876 /* We are not in a function body. */
17877 saved_in_function_body = parser->in_function_body;
17878 parser->in_function_body = false;
17879 /* Or in a loop. */
17880 in_statement = parser->in_statement;
17881 parser->in_statement = 0;
17882 /* Or in a switch. */
17883 in_switch_statement_p = parser->in_switch_statement_p;
17884 parser->in_switch_statement_p = false;
17885 /* We are not immediately inside an extern "lang" block. */
17886 saved_in_unbraced_linkage_specification_p
17887 = parser->in_unbraced_linkage_specification_p;
17888 parser->in_unbraced_linkage_specification_p = false;
17890 /* Start the class. */
17891 if (nested_name_specifier_p)
17893 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17894 old_scope = push_inner_scope (scope);
17896 type = begin_class_definition (type, attributes);
17898 if (type == error_mark_node)
17899 /* If the type is erroneous, skip the entire body of the class. */
17900 cp_parser_skip_to_closing_brace (parser);
17902 /* Parse the member-specification. */
17903 cp_parser_member_specification_opt (parser);
17905 /* Look for the trailing `}'. */
17906 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17907 /* Look for trailing attributes to apply to this class. */
17908 if (cp_parser_allow_gnu_extensions_p (parser))
17909 attributes = cp_parser_attributes_opt (parser);
17910 if (type != error_mark_node)
17911 type = finish_struct (type, attributes);
17912 if (nested_name_specifier_p)
17913 pop_inner_scope (old_scope, scope);
17915 /* We've finished a type definition. Check for the common syntax
17916 error of forgetting a semicolon after the definition. We need to
17917 be careful, as we can't just check for not-a-semicolon and be done
17918 with it; the user might have typed:
17920 class X { } c = ...;
17921 class X { } *p = ...;
17923 and so forth. Instead, enumerate all the possible tokens that
17924 might follow this production; if we don't see one of them, then
17925 complain and silently insert the semicolon. */
17927 cp_token *token = cp_lexer_peek_token (parser->lexer);
17928 bool want_semicolon = true;
17930 switch (token->type)
17933 case CPP_SEMICOLON:
17936 case CPP_OPEN_PAREN:
17937 case CPP_CLOSE_PAREN:
17939 want_semicolon = false;
17942 /* While it's legal for type qualifiers and storage class
17943 specifiers to follow type definitions in the grammar, only
17944 compiler testsuites contain code like that. Assume that if
17945 we see such code, then what we're really seeing is a case
17949 const <type> var = ...;
17954 static <type> func (...) ...
17956 i.e. the qualifier or specifier applies to the next
17957 declaration. To do so, however, we need to look ahead one
17958 more token to see if *that* token is a type specifier.
17960 This code could be improved to handle:
17963 static const <type> var = ...; */
17965 if (keyword_is_decl_specifier (token->keyword))
17967 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17969 /* Handling user-defined types here would be nice, but very
17972 = (lookahead->type == CPP_KEYWORD
17973 && keyword_begins_type_specifier (lookahead->keyword));
17980 /* If we don't have a type, then something is very wrong and we
17981 shouldn't try to do anything clever. Likewise for not seeing the
17983 if (closing_brace && TYPE_P (type) && want_semicolon)
17985 cp_token_position prev
17986 = cp_lexer_previous_token_position (parser->lexer);
17987 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17988 location_t loc = prev_token->location;
17990 if (CLASSTYPE_DECLARED_CLASS (type))
17991 error_at (loc, "expected %<;%> after class definition");
17992 else if (TREE_CODE (type) == RECORD_TYPE)
17993 error_at (loc, "expected %<;%> after struct definition");
17994 else if (TREE_CODE (type) == UNION_TYPE)
17995 error_at (loc, "expected %<;%> after union definition");
17997 gcc_unreachable ();
17999 /* Unget one token and smash it to look as though we encountered
18000 a semicolon in the input stream. */
18001 cp_lexer_set_token_position (parser->lexer, prev);
18002 token = cp_lexer_peek_token (parser->lexer);
18003 token->type = CPP_SEMICOLON;
18004 token->keyword = RID_MAX;
18008 /* If this class is not itself within the scope of another class,
18009 then we need to parse the bodies of all of the queued function
18010 definitions. Note that the queued functions defined in a class
18011 are not always processed immediately following the
18012 class-specifier for that class. Consider:
18015 struct B { void f() { sizeof (A); } };
18018 If `f' were processed before the processing of `A' were
18019 completed, there would be no way to compute the size of `A'.
18020 Note that the nesting we are interested in here is lexical --
18021 not the semantic nesting given by TYPE_CONTEXT. In particular,
18024 struct A { struct B; };
18025 struct A::B { void f() { } };
18027 there is no need to delay the parsing of `A::B::f'. */
18028 if (--parser->num_classes_being_defined == 0)
18031 tree class_type = NULL_TREE;
18032 tree pushed_scope = NULL_TREE;
18034 cp_default_arg_entry *e;
18035 tree save_ccp, save_ccr;
18037 /* In a first pass, parse default arguments to the functions.
18038 Then, in a second pass, parse the bodies of the functions.
18039 This two-phased approach handles cases like:
18047 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18051 /* If there are default arguments that have not yet been processed,
18052 take care of them now. */
18053 if (class_type != e->class_type)
18056 pop_scope (pushed_scope);
18057 class_type = e->class_type;
18058 pushed_scope = push_scope (class_type);
18060 /* Make sure that any template parameters are in scope. */
18061 maybe_begin_member_template_processing (decl);
18062 /* Parse the default argument expressions. */
18063 cp_parser_late_parsing_default_args (parser, decl);
18064 /* Remove any template parameters from the symbol table. */
18065 maybe_end_member_template_processing ();
18067 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18068 /* Now parse any NSDMIs. */
18069 save_ccp = current_class_ptr;
18070 save_ccr = current_class_ref;
18071 FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18073 if (class_type != DECL_CONTEXT (decl))
18076 pop_scope (pushed_scope);
18077 class_type = DECL_CONTEXT (decl);
18078 pushed_scope = push_scope (class_type);
18080 inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18081 cp_parser_late_parsing_nsdmi (parser, decl);
18083 VEC_truncate (tree, unparsed_nsdmis, 0);
18084 current_class_ptr = save_ccp;
18085 current_class_ref = save_ccr;
18087 pop_scope (pushed_scope);
18088 /* Now parse the body of the functions. */
18089 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18090 cp_parser_late_parsing_for_member (parser, decl);
18091 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18094 /* Put back any saved access checks. */
18095 pop_deferring_access_checks ();
18097 /* Restore saved state. */
18098 parser->in_switch_statement_p = in_switch_statement_p;
18099 parser->in_statement = in_statement;
18100 parser->in_function_body = saved_in_function_body;
18101 parser->num_template_parameter_lists
18102 = saved_num_template_parameter_lists;
18103 parser->in_unbraced_linkage_specification_p
18104 = saved_in_unbraced_linkage_specification_p;
18110 cp_parser_class_specifier (cp_parser* parser)
18113 timevar_push (TV_PARSE_STRUCT);
18114 ret = cp_parser_class_specifier_1 (parser);
18115 timevar_pop (TV_PARSE_STRUCT);
18119 /* Parse a class-head.
18122 class-key identifier [opt] base-clause [opt]
18123 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18124 class-key nested-name-specifier [opt] template-id
18127 class-virt-specifier:
18131 class-key attributes identifier [opt] base-clause [opt]
18132 class-key attributes nested-name-specifier identifier base-clause [opt]
18133 class-key attributes nested-name-specifier [opt] template-id
18136 Upon return BASES is initialized to the list of base classes (or
18137 NULL, if there are none) in the same form returned by
18138 cp_parser_base_clause.
18140 Returns the TYPE of the indicated class. Sets
18141 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18142 involving a nested-name-specifier was used, and FALSE otherwise.
18144 Returns error_mark_node if this is not a class-head.
18146 Returns NULL_TREE if the class-head is syntactically valid, but
18147 semantically invalid in a way that means we should skip the entire
18148 body of the class. */
18151 cp_parser_class_head (cp_parser* parser,
18152 bool* nested_name_specifier_p,
18153 tree *attributes_p,
18156 tree nested_name_specifier;
18157 enum tag_types class_key;
18158 tree id = NULL_TREE;
18159 tree type = NULL_TREE;
18161 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18162 bool template_id_p = false;
18163 bool qualified_p = false;
18164 bool invalid_nested_name_p = false;
18165 bool invalid_explicit_specialization_p = false;
18166 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18167 tree pushed_scope = NULL_TREE;
18168 unsigned num_templates;
18169 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18170 /* Assume no nested-name-specifier will be present. */
18171 *nested_name_specifier_p = false;
18172 /* Assume no template parameter lists will be used in defining the
18175 parser->colon_corrects_to_scope_p = false;
18177 *bases = NULL_TREE;
18179 /* Look for the class-key. */
18180 class_key = cp_parser_class_key (parser);
18181 if (class_key == none_type)
18182 return error_mark_node;
18184 /* Parse the attributes. */
18185 attributes = cp_parser_attributes_opt (parser);
18187 /* If the next token is `::', that is invalid -- but sometimes
18188 people do try to write:
18192 Handle this gracefully by accepting the extra qualifier, and then
18193 issuing an error about it later if this really is a
18194 class-head. If it turns out just to be an elaborated type
18195 specifier, remain silent. */
18196 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18197 qualified_p = true;
18199 push_deferring_access_checks (dk_no_check);
18201 /* Determine the name of the class. Begin by looking for an
18202 optional nested-name-specifier. */
18203 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18204 nested_name_specifier
18205 = cp_parser_nested_name_specifier_opt (parser,
18206 /*typename_keyword_p=*/false,
18207 /*check_dependency_p=*/false,
18209 /*is_declaration=*/false);
18210 /* If there was a nested-name-specifier, then there *must* be an
18212 if (nested_name_specifier)
18214 type_start_token = cp_lexer_peek_token (parser->lexer);
18215 /* Although the grammar says `identifier', it really means
18216 `class-name' or `template-name'. You are only allowed to
18217 define a class that has already been declared with this
18220 The proposed resolution for Core Issue 180 says that wherever
18221 you see `class T::X' you should treat `X' as a type-name.
18223 It is OK to define an inaccessible class; for example:
18225 class A { class B; };
18228 We do not know if we will see a class-name, or a
18229 template-name. We look for a class-name first, in case the
18230 class-name is a template-id; if we looked for the
18231 template-name first we would stop after the template-name. */
18232 cp_parser_parse_tentatively (parser);
18233 type = cp_parser_class_name (parser,
18234 /*typename_keyword_p=*/false,
18235 /*template_keyword_p=*/false,
18237 /*check_dependency_p=*/false,
18238 /*class_head_p=*/true,
18239 /*is_declaration=*/false);
18240 /* If that didn't work, ignore the nested-name-specifier. */
18241 if (!cp_parser_parse_definitely (parser))
18243 invalid_nested_name_p = true;
18244 type_start_token = cp_lexer_peek_token (parser->lexer);
18245 id = cp_parser_identifier (parser);
18246 if (id == error_mark_node)
18249 /* If we could not find a corresponding TYPE, treat this
18250 declaration like an unqualified declaration. */
18251 if (type == error_mark_node)
18252 nested_name_specifier = NULL_TREE;
18253 /* Otherwise, count the number of templates used in TYPE and its
18254 containing scopes. */
18259 for (scope = TREE_TYPE (type);
18260 scope && TREE_CODE (scope) != NAMESPACE_DECL;
18261 scope = (TYPE_P (scope)
18262 ? TYPE_CONTEXT (scope)
18263 : DECL_CONTEXT (scope)))
18265 && CLASS_TYPE_P (scope)
18266 && CLASSTYPE_TEMPLATE_INFO (scope)
18267 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18268 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18272 /* Otherwise, the identifier is optional. */
18275 /* We don't know whether what comes next is a template-id,
18276 an identifier, or nothing at all. */
18277 cp_parser_parse_tentatively (parser);
18278 /* Check for a template-id. */
18279 type_start_token = cp_lexer_peek_token (parser->lexer);
18280 id = cp_parser_template_id (parser,
18281 /*template_keyword_p=*/false,
18282 /*check_dependency_p=*/true,
18283 /*is_declaration=*/true);
18284 /* If that didn't work, it could still be an identifier. */
18285 if (!cp_parser_parse_definitely (parser))
18287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18289 type_start_token = cp_lexer_peek_token (parser->lexer);
18290 id = cp_parser_identifier (parser);
18297 template_id_p = true;
18302 pop_deferring_access_checks ();
18306 cp_parser_check_for_invalid_template_id (parser, id,
18307 type_start_token->location);
18309 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18311 /* If it's not a `:' or a `{' then we can't really be looking at a
18312 class-head, since a class-head only appears as part of a
18313 class-specifier. We have to detect this situation before calling
18314 xref_tag, since that has irreversible side-effects. */
18315 if (!cp_parser_next_token_starts_class_definition_p (parser))
18317 cp_parser_error (parser, "expected %<{%> or %<:%>");
18318 type = error_mark_node;
18322 /* At this point, we're going ahead with the class-specifier, even
18323 if some other problem occurs. */
18324 cp_parser_commit_to_tentative_parse (parser);
18325 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18327 cp_parser_error (parser,
18328 "cannot specify %<override%> for a class");
18329 type = error_mark_node;
18332 /* Issue the error about the overly-qualified name now. */
18335 cp_parser_error (parser,
18336 "global qualification of class name is invalid");
18337 type = error_mark_node;
18340 else if (invalid_nested_name_p)
18342 cp_parser_error (parser,
18343 "qualified name does not name a class");
18344 type = error_mark_node;
18347 else if (nested_name_specifier)
18351 /* Reject typedef-names in class heads. */
18352 if (!DECL_IMPLICIT_TYPEDEF_P (type))
18354 error_at (type_start_token->location,
18355 "invalid class name in declaration of %qD",
18361 /* Figure out in what scope the declaration is being placed. */
18362 scope = current_scope ();
18363 /* If that scope does not contain the scope in which the
18364 class was originally declared, the program is invalid. */
18365 if (scope && !is_ancestor (scope, nested_name_specifier))
18367 if (at_namespace_scope_p ())
18368 error_at (type_start_token->location,
18369 "declaration of %qD in namespace %qD which does not "
18371 type, scope, nested_name_specifier);
18373 error_at (type_start_token->location,
18374 "declaration of %qD in %qD which does not enclose %qD",
18375 type, scope, nested_name_specifier);
18381 A declarator-id shall not be qualified except for the
18382 definition of a ... nested class outside of its class
18383 ... [or] the definition or explicit instantiation of a
18384 class member of a namespace outside of its namespace. */
18385 if (scope == nested_name_specifier)
18387 permerror (nested_name_specifier_token_start->location,
18388 "extra qualification not allowed");
18389 nested_name_specifier = NULL_TREE;
18393 /* An explicit-specialization must be preceded by "template <>". If
18394 it is not, try to recover gracefully. */
18395 if (at_namespace_scope_p ()
18396 && parser->num_template_parameter_lists == 0
18399 error_at (type_start_token->location,
18400 "an explicit specialization must be preceded by %<template <>%>");
18401 invalid_explicit_specialization_p = true;
18402 /* Take the same action that would have been taken by
18403 cp_parser_explicit_specialization. */
18404 ++parser->num_template_parameter_lists;
18405 begin_specialization ();
18407 /* There must be no "return" statements between this point and the
18408 end of this function; set "type "to the correct return value and
18409 use "goto done;" to return. */
18410 /* Make sure that the right number of template parameters were
18412 if (!cp_parser_check_template_parameters (parser, num_templates,
18413 type_start_token->location,
18414 /*declarator=*/NULL))
18416 /* If something went wrong, there is no point in even trying to
18417 process the class-definition. */
18422 /* Look up the type. */
18425 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18426 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18427 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18429 error_at (type_start_token->location,
18430 "function template %qD redeclared as a class template", id);
18431 type = error_mark_node;
18435 type = TREE_TYPE (id);
18436 type = maybe_process_partial_specialization (type);
18438 if (nested_name_specifier)
18439 pushed_scope = push_scope (nested_name_specifier);
18441 else if (nested_name_specifier)
18447 template <typename T> struct S { struct T };
18448 template <typename T> struct S<T>::T { };
18450 we will get a TYPENAME_TYPE when processing the definition of
18451 `S::T'. We need to resolve it to the actual type before we
18452 try to define it. */
18453 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18455 class_type = resolve_typename_type (TREE_TYPE (type),
18456 /*only_current_p=*/false);
18457 if (TREE_CODE (class_type) != TYPENAME_TYPE)
18458 type = TYPE_NAME (class_type);
18461 cp_parser_error (parser, "could not resolve typename type");
18462 type = error_mark_node;
18466 if (maybe_process_partial_specialization (TREE_TYPE (type))
18467 == error_mark_node)
18473 class_type = current_class_type;
18474 /* Enter the scope indicated by the nested-name-specifier. */
18475 pushed_scope = push_scope (nested_name_specifier);
18476 /* Get the canonical version of this type. */
18477 type = TYPE_MAIN_DECL (TREE_TYPE (type));
18478 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18479 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18481 type = push_template_decl (type);
18482 if (type == error_mark_node)
18489 type = TREE_TYPE (type);
18490 *nested_name_specifier_p = true;
18492 else /* The name is not a nested name. */
18494 /* If the class was unnamed, create a dummy name. */
18496 id = make_anon_name ();
18497 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18498 parser->num_template_parameter_lists);
18501 /* Indicate whether this class was declared as a `class' or as a
18503 if (TREE_CODE (type) == RECORD_TYPE)
18504 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18505 cp_parser_check_class_key (class_key, type);
18507 /* If this type was already complete, and we see another definition,
18508 that's an error. */
18509 if (type != error_mark_node && COMPLETE_TYPE_P (type))
18511 error_at (type_start_token->location, "redefinition of %q#T",
18513 error_at (type_start_token->location, "previous definition of %q+#T",
18518 else if (type == error_mark_node)
18521 /* We will have entered the scope containing the class; the names of
18522 base classes should be looked up in that context. For example:
18524 struct A { struct B {}; struct C; };
18525 struct A::C : B {};
18529 /* Get the list of base-classes, if there is one. */
18530 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18531 *bases = cp_parser_base_clause (parser);
18534 /* Leave the scope given by the nested-name-specifier. We will
18535 enter the class scope itself while processing the members. */
18537 pop_scope (pushed_scope);
18539 if (invalid_explicit_specialization_p)
18541 end_specialization ();
18542 --parser->num_template_parameter_lists;
18546 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18547 *attributes_p = attributes;
18548 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18549 CLASSTYPE_FINAL (type) = 1;
18551 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18555 /* Parse a class-key.
18562 Returns the kind of class-key specified, or none_type to indicate
18565 static enum tag_types
18566 cp_parser_class_key (cp_parser* parser)
18569 enum tag_types tag_type;
18571 /* Look for the class-key. */
18572 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18576 /* Check to see if the TOKEN is a class-key. */
18577 tag_type = cp_parser_token_is_class_key (token);
18579 cp_parser_error (parser, "expected class-key");
18583 /* Parse an (optional) member-specification.
18585 member-specification:
18586 member-declaration member-specification [opt]
18587 access-specifier : member-specification [opt] */
18590 cp_parser_member_specification_opt (cp_parser* parser)
18597 /* Peek at the next token. */
18598 token = cp_lexer_peek_token (parser->lexer);
18599 /* If it's a `}', or EOF then we've seen all the members. */
18600 if (token->type == CPP_CLOSE_BRACE
18601 || token->type == CPP_EOF
18602 || token->type == CPP_PRAGMA_EOL)
18605 /* See if this token is a keyword. */
18606 keyword = token->keyword;
18610 case RID_PROTECTED:
18612 /* Consume the access-specifier. */
18613 cp_lexer_consume_token (parser->lexer);
18614 /* Remember which access-specifier is active. */
18615 current_access_specifier = token->u.value;
18616 /* Look for the `:'. */
18617 cp_parser_require (parser, CPP_COLON, RT_COLON);
18621 /* Accept #pragmas at class scope. */
18622 if (token->type == CPP_PRAGMA)
18624 cp_parser_pragma (parser, pragma_external);
18628 /* Otherwise, the next construction must be a
18629 member-declaration. */
18630 cp_parser_member_declaration (parser);
18635 /* Parse a member-declaration.
18637 member-declaration:
18638 decl-specifier-seq [opt] member-declarator-list [opt] ;
18639 function-definition ; [opt]
18640 :: [opt] nested-name-specifier template [opt] unqualified-id ;
18642 template-declaration
18645 member-declarator-list:
18647 member-declarator-list , member-declarator
18650 declarator pure-specifier [opt]
18651 declarator constant-initializer [opt]
18652 identifier [opt] : constant-expression
18656 member-declaration:
18657 __extension__ member-declaration
18660 declarator attributes [opt] pure-specifier [opt]
18661 declarator attributes [opt] constant-initializer [opt]
18662 identifier [opt] attributes [opt] : constant-expression
18666 member-declaration:
18667 static_assert-declaration */
18670 cp_parser_member_declaration (cp_parser* parser)
18672 cp_decl_specifier_seq decl_specifiers;
18673 tree prefix_attributes;
18675 int declares_class_or_enum;
18677 cp_token *token = NULL;
18678 cp_token *decl_spec_token_start = NULL;
18679 cp_token *initializer_token_start = NULL;
18680 int saved_pedantic;
18681 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18683 /* Check for the `__extension__' keyword. */
18684 if (cp_parser_extension_opt (parser, &saved_pedantic))
18687 cp_parser_member_declaration (parser);
18688 /* Restore the old value of the PEDANTIC flag. */
18689 pedantic = saved_pedantic;
18694 /* Check for a template-declaration. */
18695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18697 /* An explicit specialization here is an error condition, and we
18698 expect the specialization handler to detect and report this. */
18699 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18700 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18701 cp_parser_explicit_specialization (parser);
18703 cp_parser_template_declaration (parser, /*member_p=*/true);
18708 /* Check for a using-declaration. */
18709 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18711 if (cxx_dialect < cxx0x)
18713 /* Parse the using-declaration. */
18714 cp_parser_using_declaration (parser,
18715 /*access_declaration_p=*/false);
18721 cp_parser_parse_tentatively (parser);
18722 decl = cp_parser_alias_declaration (parser);
18723 if (cp_parser_parse_definitely (parser))
18724 finish_member_declaration (decl);
18726 cp_parser_using_declaration (parser,
18727 /*access_declaration_p=*/false);
18732 /* Check for @defs. */
18733 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18736 tree ivar_chains = cp_parser_objc_defs_expression (parser);
18737 ivar = ivar_chains;
18741 ivar = TREE_CHAIN (member);
18742 TREE_CHAIN (member) = NULL_TREE;
18743 finish_member_declaration (member);
18748 /* If the next token is `static_assert' we have a static assertion. */
18749 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18751 cp_parser_static_assert (parser, /*member_p=*/true);
18755 parser->colon_corrects_to_scope_p = false;
18757 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18760 /* Parse the decl-specifier-seq. */
18761 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18762 cp_parser_decl_specifier_seq (parser,
18763 CP_PARSER_FLAGS_OPTIONAL,
18765 &declares_class_or_enum);
18766 prefix_attributes = decl_specifiers.attributes;
18767 decl_specifiers.attributes = NULL_TREE;
18768 /* Check for an invalid type-name. */
18769 if (!decl_specifiers.any_type_specifiers_p
18770 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18772 /* If there is no declarator, then the decl-specifier-seq should
18774 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18776 /* If there was no decl-specifier-seq, and the next token is a
18777 `;', then we have something like:
18783 Each member-declaration shall declare at least one member
18784 name of the class. */
18785 if (!decl_specifiers.any_specifiers_p)
18787 cp_token *token = cp_lexer_peek_token (parser->lexer);
18788 if (!in_system_header_at (token->location))
18789 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18795 /* See if this declaration is a friend. */
18796 friend_p = cp_parser_friend_p (&decl_specifiers);
18797 /* If there were decl-specifiers, check to see if there was
18798 a class-declaration. */
18799 type = check_tag_decl (&decl_specifiers);
18800 /* Nested classes have already been added to the class, but
18801 a `friend' needs to be explicitly registered. */
18804 /* If the `friend' keyword was present, the friend must
18805 be introduced with a class-key. */
18806 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18807 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18808 "in C++03 a class-key must be used "
18809 "when declaring a friend");
18812 template <typename T> struct A {
18813 friend struct A<T>::B;
18816 A<T>::B will be represented by a TYPENAME_TYPE, and
18817 therefore not recognized by check_tag_decl. */
18820 type = decl_specifiers.type;
18821 if (type && TREE_CODE (type) == TYPE_DECL)
18822 type = TREE_TYPE (type);
18824 if (!type || !TYPE_P (type))
18825 error_at (decl_spec_token_start->location,
18826 "friend declaration does not name a class or "
18829 make_friend_class (current_class_type, type,
18830 /*complain=*/true);
18832 /* If there is no TYPE, an error message will already have
18834 else if (!type || type == error_mark_node)
18836 /* An anonymous aggregate has to be handled specially; such
18837 a declaration really declares a data member (with a
18838 particular type), as opposed to a nested class. */
18839 else if (ANON_AGGR_TYPE_P (type))
18841 /* Remove constructors and such from TYPE, now that we
18842 know it is an anonymous aggregate. */
18843 fixup_anonymous_aggr (type);
18844 /* And make the corresponding data member. */
18845 decl = build_decl (decl_spec_token_start->location,
18846 FIELD_DECL, NULL_TREE, type);
18847 /* Add it to the class. */
18848 finish_member_declaration (decl);
18851 cp_parser_check_access_in_redeclaration
18853 decl_spec_token_start->location);
18858 bool assume_semicolon = false;
18860 /* See if these declarations will be friends. */
18861 friend_p = cp_parser_friend_p (&decl_specifiers);
18863 /* Keep going until we hit the `;' at the end of the
18865 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18867 tree attributes = NULL_TREE;
18868 tree first_attribute;
18870 /* Peek at the next token. */
18871 token = cp_lexer_peek_token (parser->lexer);
18873 /* Check for a bitfield declaration. */
18874 if (token->type == CPP_COLON
18875 || (token->type == CPP_NAME
18876 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18882 /* Get the name of the bitfield. Note that we cannot just
18883 check TOKEN here because it may have been invalidated by
18884 the call to cp_lexer_peek_nth_token above. */
18885 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18886 identifier = cp_parser_identifier (parser);
18888 identifier = NULL_TREE;
18890 /* Consume the `:' token. */
18891 cp_lexer_consume_token (parser->lexer);
18892 /* Get the width of the bitfield. */
18894 = cp_parser_constant_expression (parser,
18895 /*allow_non_constant=*/false,
18898 /* Look for attributes that apply to the bitfield. */
18899 attributes = cp_parser_attributes_opt (parser);
18900 /* Remember which attributes are prefix attributes and
18902 first_attribute = attributes;
18903 /* Combine the attributes. */
18904 attributes = chainon (prefix_attributes, attributes);
18906 /* Create the bitfield declaration. */
18907 decl = grokbitfield (identifier
18908 ? make_id_declarator (NULL_TREE,
18918 cp_declarator *declarator;
18920 tree asm_specification;
18921 int ctor_dtor_or_conv_p;
18923 /* Parse the declarator. */
18925 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18926 &ctor_dtor_or_conv_p,
18927 /*parenthesized_p=*/NULL,
18928 /*member_p=*/true);
18930 /* If something went wrong parsing the declarator, make sure
18931 that we at least consume some tokens. */
18932 if (declarator == cp_error_declarator)
18934 /* Skip to the end of the statement. */
18935 cp_parser_skip_to_end_of_statement (parser);
18936 /* If the next token is not a semicolon, that is
18937 probably because we just skipped over the body of
18938 a function. So, we consume a semicolon if
18939 present, but do not issue an error message if it
18941 if (cp_lexer_next_token_is (parser->lexer,
18943 cp_lexer_consume_token (parser->lexer);
18947 if (declares_class_or_enum & 2)
18948 cp_parser_check_for_definition_in_return_type
18949 (declarator, decl_specifiers.type,
18950 decl_specifiers.type_location);
18952 /* Look for an asm-specification. */
18953 asm_specification = cp_parser_asm_specification_opt (parser);
18954 /* Look for attributes that apply to the declaration. */
18955 attributes = cp_parser_attributes_opt (parser);
18956 /* Remember which attributes are prefix attributes and
18958 first_attribute = attributes;
18959 /* Combine the attributes. */
18960 attributes = chainon (prefix_attributes, attributes);
18962 /* If it's an `=', then we have a constant-initializer or a
18963 pure-specifier. It is not correct to parse the
18964 initializer before registering the member declaration
18965 since the member declaration should be in scope while
18966 its initializer is processed. However, the rest of the
18967 front end does not yet provide an interface that allows
18968 us to handle this correctly. */
18969 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18973 A pure-specifier shall be used only in the declaration of
18974 a virtual function.
18976 A member-declarator can contain a constant-initializer
18977 only if it declares a static member of integral or
18980 Therefore, if the DECLARATOR is for a function, we look
18981 for a pure-specifier; otherwise, we look for a
18982 constant-initializer. When we call `grokfield', it will
18983 perform more stringent semantics checks. */
18984 initializer_token_start = cp_lexer_peek_token (parser->lexer);
18985 if (function_declarator_p (declarator)
18986 || (decl_specifiers.type
18987 && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18988 && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18989 == FUNCTION_TYPE)))
18990 initializer = cp_parser_pure_specifier (parser);
18991 else if (decl_specifiers.storage_class != sc_static)
18992 initializer = cp_parser_save_nsdmi (parser);
18993 else if (cxx_dialect >= cxx0x)
18996 /* Don't require a constant rvalue in C++11, since we
18997 might want a reference constant. We'll enforce
18998 constancy later. */
18999 cp_lexer_consume_token (parser->lexer);
19000 /* Parse the initializer. */
19001 initializer = cp_parser_initializer_clause (parser,
19005 /* Parse the initializer. */
19006 initializer = cp_parser_constant_initializer (parser);
19008 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19009 && !function_declarator_p (declarator))
19012 if (decl_specifiers.storage_class != sc_static)
19013 initializer = cp_parser_save_nsdmi (parser);
19015 initializer = cp_parser_initializer (parser, &x, &x);
19017 /* Otherwise, there is no initializer. */
19019 initializer = NULL_TREE;
19021 /* See if we are probably looking at a function
19022 definition. We are certainly not looking at a
19023 member-declarator. Calling `grokfield' has
19024 side-effects, so we must not do it unless we are sure
19025 that we are looking at a member-declarator. */
19026 if (cp_parser_token_starts_function_definition_p
19027 (cp_lexer_peek_token (parser->lexer)))
19029 /* The grammar does not allow a pure-specifier to be
19030 used when a member function is defined. (It is
19031 possible that this fact is an oversight in the
19032 standard, since a pure function may be defined
19033 outside of the class-specifier. */
19035 error_at (initializer_token_start->location,
19036 "pure-specifier on function-definition");
19037 decl = cp_parser_save_member_function_body (parser,
19041 /* If the member was not a friend, declare it here. */
19043 finish_member_declaration (decl);
19044 /* Peek at the next token. */
19045 token = cp_lexer_peek_token (parser->lexer);
19046 /* If the next token is a semicolon, consume it. */
19047 if (token->type == CPP_SEMICOLON)
19048 cp_lexer_consume_token (parser->lexer);
19052 if (declarator->kind == cdk_function)
19053 declarator->id_loc = token->location;
19054 /* Create the declaration. */
19055 decl = grokfield (declarator, &decl_specifiers,
19056 initializer, /*init_const_expr_p=*/true,
19061 /* Reset PREFIX_ATTRIBUTES. */
19062 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19063 attributes = TREE_CHAIN (attributes);
19065 TREE_CHAIN (attributes) = NULL_TREE;
19067 /* If there is any qualification still in effect, clear it
19068 now; we will be starting fresh with the next declarator. */
19069 parser->scope = NULL_TREE;
19070 parser->qualifying_scope = NULL_TREE;
19071 parser->object_scope = NULL_TREE;
19072 /* If it's a `,', then there are more declarators. */
19073 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19074 cp_lexer_consume_token (parser->lexer);
19075 /* If the next token isn't a `;', then we have a parse error. */
19076 else if (cp_lexer_next_token_is_not (parser->lexer,
19079 /* The next token might be a ways away from where the
19080 actual semicolon is missing. Find the previous token
19081 and use that for our error position. */
19082 cp_token *token = cp_lexer_previous_token (parser->lexer);
19083 error_at (token->location,
19084 "expected %<;%> at end of member declaration");
19086 /* Assume that the user meant to provide a semicolon. If
19087 we were to cp_parser_skip_to_end_of_statement, we might
19088 skip to a semicolon inside a member function definition
19089 and issue nonsensical error messages. */
19090 assume_semicolon = true;
19095 /* Add DECL to the list of members. */
19097 finish_member_declaration (decl);
19099 if (TREE_CODE (decl) == FUNCTION_DECL)
19100 cp_parser_save_default_args (parser, decl);
19101 else if (TREE_CODE (decl) == FIELD_DECL
19102 && !DECL_C_BIT_FIELD (decl)
19103 && DECL_INITIAL (decl))
19104 /* Add DECL to the queue of NSDMI to be parsed later. */
19105 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19108 if (assume_semicolon)
19113 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19115 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19118 /* Parse a pure-specifier.
19123 Returns INTEGER_ZERO_NODE if a pure specifier is found.
19124 Otherwise, ERROR_MARK_NODE is returned. */
19127 cp_parser_pure_specifier (cp_parser* parser)
19131 /* Look for the `=' token. */
19132 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19133 return error_mark_node;
19134 /* Look for the `0' token. */
19135 token = cp_lexer_peek_token (parser->lexer);
19137 if (token->type == CPP_EOF
19138 || token->type == CPP_PRAGMA_EOL)
19139 return error_mark_node;
19141 cp_lexer_consume_token (parser->lexer);
19143 /* Accept = default or = delete in c++0x mode. */
19144 if (token->keyword == RID_DEFAULT
19145 || token->keyword == RID_DELETE)
19147 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19148 return token->u.value;
19151 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
19152 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19154 cp_parser_error (parser,
19155 "invalid pure specifier (only %<= 0%> is allowed)");
19156 cp_parser_skip_to_end_of_statement (parser);
19157 return error_mark_node;
19159 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19161 error_at (token->location, "templates may not be %<virtual%>");
19162 return error_mark_node;
19165 return integer_zero_node;
19168 /* Parse a constant-initializer.
19170 constant-initializer:
19171 = constant-expression
19173 Returns a representation of the constant-expression. */
19176 cp_parser_constant_initializer (cp_parser* parser)
19178 /* Look for the `=' token. */
19179 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19180 return error_mark_node;
19182 /* It is invalid to write:
19184 struct S { static const int i = { 7 }; };
19187 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19189 cp_parser_error (parser,
19190 "a brace-enclosed initializer is not allowed here");
19191 /* Consume the opening brace. */
19192 cp_lexer_consume_token (parser->lexer);
19193 /* Skip the initializer. */
19194 cp_parser_skip_to_closing_brace (parser);
19195 /* Look for the trailing `}'. */
19196 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19198 return error_mark_node;
19201 return cp_parser_constant_expression (parser,
19202 /*allow_non_constant=*/false,
19206 /* Derived classes [gram.class.derived] */
19208 /* Parse a base-clause.
19211 : base-specifier-list
19213 base-specifier-list:
19214 base-specifier ... [opt]
19215 base-specifier-list , base-specifier ... [opt]
19217 Returns a TREE_LIST representing the base-classes, in the order in
19218 which they were declared. The representation of each node is as
19219 described by cp_parser_base_specifier.
19221 In the case that no bases are specified, this function will return
19222 NULL_TREE, not ERROR_MARK_NODE. */
19225 cp_parser_base_clause (cp_parser* parser)
19227 tree bases = NULL_TREE;
19229 /* Look for the `:' that begins the list. */
19230 cp_parser_require (parser, CPP_COLON, RT_COLON);
19232 /* Scan the base-specifier-list. */
19237 bool pack_expansion_p = false;
19239 /* Look for the base-specifier. */
19240 base = cp_parser_base_specifier (parser);
19241 /* Look for the (optional) ellipsis. */
19242 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19244 /* Consume the `...'. */
19245 cp_lexer_consume_token (parser->lexer);
19247 pack_expansion_p = true;
19250 /* Add BASE to the front of the list. */
19251 if (base && base != error_mark_node)
19253 if (pack_expansion_p)
19254 /* Make this a pack expansion type. */
19255 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19257 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19259 TREE_CHAIN (base) = bases;
19263 /* Peek at the next token. */
19264 token = cp_lexer_peek_token (parser->lexer);
19265 /* If it's not a comma, then the list is complete. */
19266 if (token->type != CPP_COMMA)
19268 /* Consume the `,'. */
19269 cp_lexer_consume_token (parser->lexer);
19272 /* PARSER->SCOPE may still be non-NULL at this point, if the last
19273 base class had a qualified name. However, the next name that
19274 appears is certainly not qualified. */
19275 parser->scope = NULL_TREE;
19276 parser->qualifying_scope = NULL_TREE;
19277 parser->object_scope = NULL_TREE;
19279 return nreverse (bases);
19282 /* Parse a base-specifier.
19285 :: [opt] nested-name-specifier [opt] class-name
19286 virtual access-specifier [opt] :: [opt] nested-name-specifier
19288 access-specifier virtual [opt] :: [opt] nested-name-specifier
19291 Returns a TREE_LIST. The TREE_PURPOSE will be one of
19292 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19293 indicate the specifiers provided. The TREE_VALUE will be a TYPE
19294 (or the ERROR_MARK_NODE) indicating the type that was specified. */
19297 cp_parser_base_specifier (cp_parser* parser)
19301 bool virtual_p = false;
19302 bool duplicate_virtual_error_issued_p = false;
19303 bool duplicate_access_error_issued_p = false;
19304 bool class_scope_p, template_p;
19305 tree access = access_default_node;
19308 /* Process the optional `virtual' and `access-specifier'. */
19311 /* Peek at the next token. */
19312 token = cp_lexer_peek_token (parser->lexer);
19313 /* Process `virtual'. */
19314 switch (token->keyword)
19317 /* If `virtual' appears more than once, issue an error. */
19318 if (virtual_p && !duplicate_virtual_error_issued_p)
19320 cp_parser_error (parser,
19321 "%<virtual%> specified more than once in base-specified");
19322 duplicate_virtual_error_issued_p = true;
19327 /* Consume the `virtual' token. */
19328 cp_lexer_consume_token (parser->lexer);
19333 case RID_PROTECTED:
19335 /* If more than one access specifier appears, issue an
19337 if (access != access_default_node
19338 && !duplicate_access_error_issued_p)
19340 cp_parser_error (parser,
19341 "more than one access specifier in base-specified");
19342 duplicate_access_error_issued_p = true;
19345 access = ridpointers[(int) token->keyword];
19347 /* Consume the access-specifier. */
19348 cp_lexer_consume_token (parser->lexer);
19357 /* It is not uncommon to see programs mechanically, erroneously, use
19358 the 'typename' keyword to denote (dependent) qualified types
19359 as base classes. */
19360 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19362 token = cp_lexer_peek_token (parser->lexer);
19363 if (!processing_template_decl)
19364 error_at (token->location,
19365 "keyword %<typename%> not allowed outside of templates");
19367 error_at (token->location,
19368 "keyword %<typename%> not allowed in this context "
19369 "(the base class is implicitly a type)");
19370 cp_lexer_consume_token (parser->lexer);
19373 /* Look for the optional `::' operator. */
19374 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19375 /* Look for the nested-name-specifier. The simplest way to
19380 The keyword `typename' is not permitted in a base-specifier or
19381 mem-initializer; in these contexts a qualified name that
19382 depends on a template-parameter is implicitly assumed to be a
19385 is to pretend that we have seen the `typename' keyword at this
19387 cp_parser_nested_name_specifier_opt (parser,
19388 /*typename_keyword_p=*/true,
19389 /*check_dependency_p=*/true,
19391 /*is_declaration=*/true);
19392 /* If the base class is given by a qualified name, assume that names
19393 we see are type names or templates, as appropriate. */
19394 class_scope_p = (parser->scope && TYPE_P (parser->scope));
19395 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19398 && cp_lexer_next_token_is_decltype (parser->lexer))
19399 /* DR 950 allows decltype as a base-specifier. */
19400 type = cp_parser_decltype (parser);
19403 /* Otherwise, look for the class-name. */
19404 type = cp_parser_class_name (parser,
19408 /*check_dependency_p=*/true,
19409 /*class_head_p=*/false,
19410 /*is_declaration=*/true);
19411 type = TREE_TYPE (type);
19414 if (type == error_mark_node)
19415 return error_mark_node;
19417 return finish_base_specifier (type, access, virtual_p);
19420 /* Exception handling [gram.exception] */
19422 /* Parse an (optional) exception-specification.
19424 exception-specification:
19425 throw ( type-id-list [opt] )
19427 Returns a TREE_LIST representing the exception-specification. The
19428 TREE_VALUE of each node is a type. */
19431 cp_parser_exception_specification_opt (cp_parser* parser)
19435 const char *saved_message;
19437 /* Peek at the next token. */
19438 token = cp_lexer_peek_token (parser->lexer);
19440 /* Is it a noexcept-specification? */
19441 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19444 cp_lexer_consume_token (parser->lexer);
19446 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19448 cp_lexer_consume_token (parser->lexer);
19450 /* Types may not be defined in an exception-specification. */
19451 saved_message = parser->type_definition_forbidden_message;
19452 parser->type_definition_forbidden_message
19453 = G_("types may not be defined in an exception-specification");
19455 expr = cp_parser_constant_expression (parser, false, NULL);
19457 /* Restore the saved message. */
19458 parser->type_definition_forbidden_message = saved_message;
19460 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19463 expr = boolean_true_node;
19465 return build_noexcept_spec (expr, tf_warning_or_error);
19468 /* If it's not `throw', then there's no exception-specification. */
19469 if (!cp_parser_is_keyword (token, RID_THROW))
19473 /* Enable this once a lot of code has transitioned to noexcept? */
19474 if (cxx_dialect == cxx0x && !in_system_header)
19475 warning (OPT_Wdeprecated, "dynamic exception specifications are "
19476 "deprecated in C++0x; use %<noexcept%> instead");
19479 /* Consume the `throw'. */
19480 cp_lexer_consume_token (parser->lexer);
19482 /* Look for the `('. */
19483 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19485 /* Peek at the next token. */
19486 token = cp_lexer_peek_token (parser->lexer);
19487 /* If it's not a `)', then there is a type-id-list. */
19488 if (token->type != CPP_CLOSE_PAREN)
19490 /* Types may not be defined in an exception-specification. */
19491 saved_message = parser->type_definition_forbidden_message;
19492 parser->type_definition_forbidden_message
19493 = G_("types may not be defined in an exception-specification");
19494 /* Parse the type-id-list. */
19495 type_id_list = cp_parser_type_id_list (parser);
19496 /* Restore the saved message. */
19497 parser->type_definition_forbidden_message = saved_message;
19500 type_id_list = empty_except_spec;
19502 /* Look for the `)'. */
19503 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19505 return type_id_list;
19508 /* Parse an (optional) type-id-list.
19512 type-id-list , type-id ... [opt]
19514 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
19515 in the order that the types were presented. */
19518 cp_parser_type_id_list (cp_parser* parser)
19520 tree types = NULL_TREE;
19527 /* Get the next type-id. */
19528 type = cp_parser_type_id (parser);
19529 /* Parse the optional ellipsis. */
19530 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19532 /* Consume the `...'. */
19533 cp_lexer_consume_token (parser->lexer);
19535 /* Turn the type into a pack expansion expression. */
19536 type = make_pack_expansion (type);
19538 /* Add it to the list. */
19539 types = add_exception_specifier (types, type, /*complain=*/1);
19540 /* Peek at the next token. */
19541 token = cp_lexer_peek_token (parser->lexer);
19542 /* If it is not a `,', we are done. */
19543 if (token->type != CPP_COMMA)
19545 /* Consume the `,'. */
19546 cp_lexer_consume_token (parser->lexer);
19549 return nreverse (types);
19552 /* Parse a try-block.
19555 try compound-statement handler-seq */
19558 cp_parser_try_block (cp_parser* parser)
19562 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19563 try_block = begin_try_block ();
19564 cp_parser_compound_statement (parser, NULL, true, false);
19565 finish_try_block (try_block);
19566 cp_parser_handler_seq (parser);
19567 finish_handler_sequence (try_block);
19572 /* Parse a function-try-block.
19574 function-try-block:
19575 try ctor-initializer [opt] function-body handler-seq */
19578 cp_parser_function_try_block (cp_parser* parser)
19580 tree compound_stmt;
19582 bool ctor_initializer_p;
19584 /* Look for the `try' keyword. */
19585 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19587 /* Let the rest of the front end know where we are. */
19588 try_block = begin_function_try_block (&compound_stmt);
19589 /* Parse the function-body. */
19591 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19592 /* We're done with the `try' part. */
19593 finish_function_try_block (try_block);
19594 /* Parse the handlers. */
19595 cp_parser_handler_seq (parser);
19596 /* We're done with the handlers. */
19597 finish_function_handler_sequence (try_block, compound_stmt);
19599 return ctor_initializer_p;
19602 /* Parse a handler-seq.
19605 handler handler-seq [opt] */
19608 cp_parser_handler_seq (cp_parser* parser)
19614 /* Parse the handler. */
19615 cp_parser_handler (parser);
19616 /* Peek at the next token. */
19617 token = cp_lexer_peek_token (parser->lexer);
19618 /* If it's not `catch' then there are no more handlers. */
19619 if (!cp_parser_is_keyword (token, RID_CATCH))
19624 /* Parse a handler.
19627 catch ( exception-declaration ) compound-statement */
19630 cp_parser_handler (cp_parser* parser)
19635 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19636 handler = begin_handler ();
19637 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19638 declaration = cp_parser_exception_declaration (parser);
19639 finish_handler_parms (declaration, handler);
19640 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19641 cp_parser_compound_statement (parser, NULL, false, false);
19642 finish_handler (handler);
19645 /* Parse an exception-declaration.
19647 exception-declaration:
19648 type-specifier-seq declarator
19649 type-specifier-seq abstract-declarator
19653 Returns a VAR_DECL for the declaration, or NULL_TREE if the
19654 ellipsis variant is used. */
19657 cp_parser_exception_declaration (cp_parser* parser)
19659 cp_decl_specifier_seq type_specifiers;
19660 cp_declarator *declarator;
19661 const char *saved_message;
19663 /* If it's an ellipsis, it's easy to handle. */
19664 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19666 /* Consume the `...' token. */
19667 cp_lexer_consume_token (parser->lexer);
19671 /* Types may not be defined in exception-declarations. */
19672 saved_message = parser->type_definition_forbidden_message;
19673 parser->type_definition_forbidden_message
19674 = G_("types may not be defined in exception-declarations");
19676 /* Parse the type-specifier-seq. */
19677 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19678 /*is_trailing_return=*/false,
19680 /* If it's a `)', then there is no declarator. */
19681 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19684 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19685 /*ctor_dtor_or_conv_p=*/NULL,
19686 /*parenthesized_p=*/NULL,
19687 /*member_p=*/false);
19689 /* Restore the saved message. */
19690 parser->type_definition_forbidden_message = saved_message;
19692 if (!type_specifiers.any_specifiers_p)
19693 return error_mark_node;
19695 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19698 /* Parse a throw-expression.
19701 throw assignment-expression [opt]
19703 Returns a THROW_EXPR representing the throw-expression. */
19706 cp_parser_throw_expression (cp_parser* parser)
19711 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19712 token = cp_lexer_peek_token (parser->lexer);
19713 /* Figure out whether or not there is an assignment-expression
19714 following the "throw" keyword. */
19715 if (token->type == CPP_COMMA
19716 || token->type == CPP_SEMICOLON
19717 || token->type == CPP_CLOSE_PAREN
19718 || token->type == CPP_CLOSE_SQUARE
19719 || token->type == CPP_CLOSE_BRACE
19720 || token->type == CPP_COLON)
19721 expression = NULL_TREE;
19723 expression = cp_parser_assignment_expression (parser,
19724 /*cast_p=*/false, NULL);
19726 return build_throw (expression);
19729 /* GNU Extensions */
19731 /* Parse an (optional) asm-specification.
19734 asm ( string-literal )
19736 If the asm-specification is present, returns a STRING_CST
19737 corresponding to the string-literal. Otherwise, returns
19741 cp_parser_asm_specification_opt (cp_parser* parser)
19744 tree asm_specification;
19746 /* Peek at the next token. */
19747 token = cp_lexer_peek_token (parser->lexer);
19748 /* If the next token isn't the `asm' keyword, then there's no
19749 asm-specification. */
19750 if (!cp_parser_is_keyword (token, RID_ASM))
19753 /* Consume the `asm' token. */
19754 cp_lexer_consume_token (parser->lexer);
19755 /* Look for the `('. */
19756 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19758 /* Look for the string-literal. */
19759 asm_specification = cp_parser_string_literal (parser, false, false);
19761 /* Look for the `)'. */
19762 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19764 return asm_specification;
19767 /* Parse an asm-operand-list.
19771 asm-operand-list , asm-operand
19774 string-literal ( expression )
19775 [ string-literal ] string-literal ( expression )
19777 Returns a TREE_LIST representing the operands. The TREE_VALUE of
19778 each node is the expression. The TREE_PURPOSE is itself a
19779 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19780 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19781 is a STRING_CST for the string literal before the parenthesis. Returns
19782 ERROR_MARK_NODE if any of the operands are invalid. */
19785 cp_parser_asm_operand_list (cp_parser* parser)
19787 tree asm_operands = NULL_TREE;
19788 bool invalid_operands = false;
19792 tree string_literal;
19796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19798 /* Consume the `[' token. */
19799 cp_lexer_consume_token (parser->lexer);
19800 /* Read the operand name. */
19801 name = cp_parser_identifier (parser);
19802 if (name != error_mark_node)
19803 name = build_string (IDENTIFIER_LENGTH (name),
19804 IDENTIFIER_POINTER (name));
19805 /* Look for the closing `]'. */
19806 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19810 /* Look for the string-literal. */
19811 string_literal = cp_parser_string_literal (parser, false, false);
19813 /* Look for the `('. */
19814 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19815 /* Parse the expression. */
19816 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19817 /* Look for the `)'. */
19818 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19820 if (name == error_mark_node
19821 || string_literal == error_mark_node
19822 || expression == error_mark_node)
19823 invalid_operands = true;
19825 /* Add this operand to the list. */
19826 asm_operands = tree_cons (build_tree_list (name, string_literal),
19829 /* If the next token is not a `,', there are no more
19831 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19833 /* Consume the `,'. */
19834 cp_lexer_consume_token (parser->lexer);
19837 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19840 /* Parse an asm-clobber-list.
19844 asm-clobber-list , string-literal
19846 Returns a TREE_LIST, indicating the clobbers in the order that they
19847 appeared. The TREE_VALUE of each node is a STRING_CST. */
19850 cp_parser_asm_clobber_list (cp_parser* parser)
19852 tree clobbers = NULL_TREE;
19856 tree string_literal;
19858 /* Look for the string literal. */
19859 string_literal = cp_parser_string_literal (parser, false, false);
19860 /* Add it to the list. */
19861 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19862 /* If the next token is not a `,', then the list is
19864 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19866 /* Consume the `,' token. */
19867 cp_lexer_consume_token (parser->lexer);
19873 /* Parse an asm-label-list.
19877 asm-label-list , identifier
19879 Returns a TREE_LIST, indicating the labels in the order that they
19880 appeared. The TREE_VALUE of each node is a label. */
19883 cp_parser_asm_label_list (cp_parser* parser)
19885 tree labels = NULL_TREE;
19889 tree identifier, label, name;
19891 /* Look for the identifier. */
19892 identifier = cp_parser_identifier (parser);
19893 if (!error_operand_p (identifier))
19895 label = lookup_label (identifier);
19896 if (TREE_CODE (label) == LABEL_DECL)
19898 TREE_USED (label) = 1;
19899 check_goto (label);
19900 name = build_string (IDENTIFIER_LENGTH (identifier),
19901 IDENTIFIER_POINTER (identifier));
19902 labels = tree_cons (name, label, labels);
19905 /* If the next token is not a `,', then the list is
19907 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19909 /* Consume the `,' token. */
19910 cp_lexer_consume_token (parser->lexer);
19913 return nreverse (labels);
19916 /* Parse an (optional) series of attributes.
19919 attributes attribute
19922 __attribute__ (( attribute-list [opt] ))
19924 The return value is as for cp_parser_attribute_list. */
19927 cp_parser_attributes_opt (cp_parser* parser)
19929 tree attributes = NULL_TREE;
19934 tree attribute_list;
19936 /* Peek at the next token. */
19937 token = cp_lexer_peek_token (parser->lexer);
19938 /* If it's not `__attribute__', then we're done. */
19939 if (token->keyword != RID_ATTRIBUTE)
19942 /* Consume the `__attribute__' keyword. */
19943 cp_lexer_consume_token (parser->lexer);
19944 /* Look for the two `(' tokens. */
19945 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19946 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19948 /* Peek at the next token. */
19949 token = cp_lexer_peek_token (parser->lexer);
19950 if (token->type != CPP_CLOSE_PAREN)
19951 /* Parse the attribute-list. */
19952 attribute_list = cp_parser_attribute_list (parser);
19954 /* If the next token is a `)', then there is no attribute
19956 attribute_list = NULL;
19958 /* Look for the two `)' tokens. */
19959 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19960 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19962 /* Add these new attributes to the list. */
19963 attributes = chainon (attributes, attribute_list);
19969 /* Parse an attribute-list.
19973 attribute-list , attribute
19977 identifier ( identifier )
19978 identifier ( identifier , expression-list )
19979 identifier ( expression-list )
19981 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
19982 to an attribute. The TREE_PURPOSE of each node is the identifier
19983 indicating which attribute is in use. The TREE_VALUE represents
19984 the arguments, if any. */
19987 cp_parser_attribute_list (cp_parser* parser)
19989 tree attribute_list = NULL_TREE;
19990 bool save_translate_strings_p = parser->translate_strings_p;
19992 parser->translate_strings_p = false;
19999 /* Look for the identifier. We also allow keywords here; for
20000 example `__attribute__ ((const))' is legal. */
20001 token = cp_lexer_peek_token (parser->lexer);
20002 if (token->type == CPP_NAME
20003 || token->type == CPP_KEYWORD)
20005 tree arguments = NULL_TREE;
20007 /* Consume the token. */
20008 token = cp_lexer_consume_token (parser->lexer);
20010 /* Save away the identifier that indicates which attribute
20012 identifier = (token->type == CPP_KEYWORD)
20013 /* For keywords, use the canonical spelling, not the
20014 parsed identifier. */
20015 ? ridpointers[(int) token->keyword]
20018 attribute = build_tree_list (identifier, NULL_TREE);
20020 /* Peek at the next token. */
20021 token = cp_lexer_peek_token (parser->lexer);
20022 /* If it's an `(', then parse the attribute arguments. */
20023 if (token->type == CPP_OPEN_PAREN)
20026 int attr_flag = (attribute_takes_identifier_p (identifier)
20027 ? id_attr : normal_attr);
20028 vec = cp_parser_parenthesized_expression_list
20029 (parser, attr_flag, /*cast_p=*/false,
20030 /*allow_expansion_p=*/false,
20031 /*non_constant_p=*/NULL);
20033 arguments = error_mark_node;
20036 arguments = build_tree_list_vec (vec);
20037 release_tree_vector (vec);
20039 /* Save the arguments away. */
20040 TREE_VALUE (attribute) = arguments;
20043 if (arguments != error_mark_node)
20045 /* Add this attribute to the list. */
20046 TREE_CHAIN (attribute) = attribute_list;
20047 attribute_list = attribute;
20050 token = cp_lexer_peek_token (parser->lexer);
20052 /* Now, look for more attributes. If the next token isn't a
20053 `,', we're done. */
20054 if (token->type != CPP_COMMA)
20057 /* Consume the comma and keep going. */
20058 cp_lexer_consume_token (parser->lexer);
20060 parser->translate_strings_p = save_translate_strings_p;
20062 /* We built up the list in reverse order. */
20063 return nreverse (attribute_list);
20066 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
20067 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
20068 current value of the PEDANTIC flag, regardless of whether or not
20069 the `__extension__' keyword is present. The caller is responsible
20070 for restoring the value of the PEDANTIC flag. */
20073 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20075 /* Save the old value of the PEDANTIC flag. */
20076 *saved_pedantic = pedantic;
20078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20080 /* Consume the `__extension__' token. */
20081 cp_lexer_consume_token (parser->lexer);
20082 /* We're not being pedantic while the `__extension__' keyword is
20092 /* Parse a label declaration.
20095 __label__ label-declarator-seq ;
20097 label-declarator-seq:
20098 identifier , label-declarator-seq
20102 cp_parser_label_declaration (cp_parser* parser)
20104 /* Look for the `__label__' keyword. */
20105 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20111 /* Look for an identifier. */
20112 identifier = cp_parser_identifier (parser);
20113 /* If we failed, stop. */
20114 if (identifier == error_mark_node)
20116 /* Declare it as a label. */
20117 finish_label_decl (identifier);
20118 /* If the next token is a `;', stop. */
20119 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20121 /* Look for the `,' separating the label declarations. */
20122 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20125 /* Look for the final `;'. */
20126 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20129 /* Support Functions */
20131 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20132 NAME should have one of the representations used for an
20133 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20134 is returned. If PARSER->SCOPE is a dependent type, then a
20135 SCOPE_REF is returned.
20137 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20138 returned; the name was already resolved when the TEMPLATE_ID_EXPR
20139 was formed. Abstractly, such entities should not be passed to this
20140 function, because they do not need to be looked up, but it is
20141 simpler to check for this special case here, rather than at the
20144 In cases not explicitly covered above, this function returns a
20145 DECL, OVERLOAD, or baselink representing the result of the lookup.
20146 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20149 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20150 (e.g., "struct") that was used. In that case bindings that do not
20151 refer to types are ignored.
20153 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20156 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20159 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20162 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20163 TREE_LIST of candidates if name-lookup results in an ambiguity, and
20164 NULL_TREE otherwise. */
20167 cp_parser_lookup_name (cp_parser *parser, tree name,
20168 enum tag_types tag_type,
20171 bool check_dependency,
20172 tree *ambiguous_decls,
20173 location_t name_location)
20177 tree object_type = parser->context->object_type;
20179 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20180 flags |= LOOKUP_COMPLAIN;
20182 /* Assume that the lookup will be unambiguous. */
20183 if (ambiguous_decls)
20184 *ambiguous_decls = NULL_TREE;
20186 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20187 no longer valid. Note that if we are parsing tentatively, and
20188 the parse fails, OBJECT_TYPE will be automatically restored. */
20189 parser->context->object_type = NULL_TREE;
20191 if (name == error_mark_node)
20192 return error_mark_node;
20194 /* A template-id has already been resolved; there is no lookup to
20196 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20198 if (BASELINK_P (name))
20200 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20201 == TEMPLATE_ID_EXPR);
20205 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
20206 it should already have been checked to make sure that the name
20207 used matches the type being destroyed. */
20208 if (TREE_CODE (name) == BIT_NOT_EXPR)
20212 /* Figure out to which type this destructor applies. */
20214 type = parser->scope;
20215 else if (object_type)
20216 type = object_type;
20218 type = current_class_type;
20219 /* If that's not a class type, there is no destructor. */
20220 if (!type || !CLASS_TYPE_P (type))
20221 return error_mark_node;
20222 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20223 lazily_declare_fn (sfk_destructor, type);
20224 if (!CLASSTYPE_DESTRUCTORS (type))
20225 return error_mark_node;
20226 /* If it was a class type, return the destructor. */
20227 return CLASSTYPE_DESTRUCTORS (type);
20230 /* By this point, the NAME should be an ordinary identifier. If
20231 the id-expression was a qualified name, the qualifying scope is
20232 stored in PARSER->SCOPE at this point. */
20233 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20235 /* Perform the lookup. */
20240 if (parser->scope == error_mark_node)
20241 return error_mark_node;
20243 /* If the SCOPE is dependent, the lookup must be deferred until
20244 the template is instantiated -- unless we are explicitly
20245 looking up names in uninstantiated templates. Even then, we
20246 cannot look up the name if the scope is not a class type; it
20247 might, for example, be a template type parameter. */
20248 dependent_p = (TYPE_P (parser->scope)
20249 && dependent_scope_p (parser->scope));
20250 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20252 /* Defer lookup. */
20253 decl = error_mark_node;
20256 tree pushed_scope = NULL_TREE;
20258 /* If PARSER->SCOPE is a dependent type, then it must be a
20259 class type, and we must not be checking dependencies;
20260 otherwise, we would have processed this lookup above. So
20261 that PARSER->SCOPE is not considered a dependent base by
20262 lookup_member, we must enter the scope here. */
20264 pushed_scope = push_scope (parser->scope);
20266 /* If the PARSER->SCOPE is a template specialization, it
20267 may be instantiated during name lookup. In that case,
20268 errors may be issued. Even if we rollback the current
20269 tentative parse, those errors are valid. */
20270 decl = lookup_qualified_name (parser->scope, name,
20271 tag_type != none_type,
20272 /*complain=*/true);
20274 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20275 lookup result and the nested-name-specifier nominates a class C:
20276 * if the name specified after the nested-name-specifier, when
20277 looked up in C, is the injected-class-name of C (Clause 9), or
20278 * if the name specified after the nested-name-specifier is the
20279 same as the identifier or the simple-template-id's template-
20280 name in the last component of the nested-name-specifier,
20281 the name is instead considered to name the constructor of
20282 class C. [ Note: for example, the constructor is not an
20283 acceptable lookup result in an elaborated-type-specifier so
20284 the constructor would not be used in place of the
20285 injected-class-name. --end note ] Such a constructor name
20286 shall be used only in the declarator-id of a declaration that
20287 names a constructor or in a using-declaration. */
20288 if (tag_type == none_type
20289 && DECL_SELF_REFERENCE_P (decl)
20290 && same_type_p (DECL_CONTEXT (decl), parser->scope))
20291 decl = lookup_qualified_name (parser->scope, ctor_identifier,
20292 tag_type != none_type,
20293 /*complain=*/true);
20295 /* If we have a single function from a using decl, pull it out. */
20296 if (TREE_CODE (decl) == OVERLOAD
20297 && !really_overloaded_fn (decl))
20298 decl = OVL_FUNCTION (decl);
20301 pop_scope (pushed_scope);
20304 /* If the scope is a dependent type and either we deferred lookup or
20305 we did lookup but didn't find the name, rememeber the name. */
20306 if (decl == error_mark_node && TYPE_P (parser->scope)
20307 && dependent_type_p (parser->scope))
20313 /* The resolution to Core Issue 180 says that `struct
20314 A::B' should be considered a type-name, even if `A'
20316 type = make_typename_type (parser->scope, name, tag_type,
20317 /*complain=*/tf_error);
20318 decl = TYPE_NAME (type);
20320 else if (is_template
20321 && (cp_parser_next_token_ends_template_argument_p (parser)
20322 || cp_lexer_next_token_is (parser->lexer,
20324 decl = make_unbound_class_template (parser->scope,
20326 /*complain=*/tf_error);
20328 decl = build_qualified_name (/*type=*/NULL_TREE,
20329 parser->scope, name,
20332 parser->qualifying_scope = parser->scope;
20333 parser->object_scope = NULL_TREE;
20335 else if (object_type)
20337 tree object_decl = NULL_TREE;
20338 /* Look up the name in the scope of the OBJECT_TYPE, unless the
20339 OBJECT_TYPE is not a class. */
20340 if (CLASS_TYPE_P (object_type))
20341 /* If the OBJECT_TYPE is a template specialization, it may
20342 be instantiated during name lookup. In that case, errors
20343 may be issued. Even if we rollback the current tentative
20344 parse, those errors are valid. */
20345 object_decl = lookup_member (object_type,
20348 tag_type != none_type);
20349 /* Look it up in the enclosing context, too. */
20350 decl = lookup_name_real (name, tag_type != none_type,
20352 /*block_p=*/true, is_namespace, flags);
20353 parser->object_scope = object_type;
20354 parser->qualifying_scope = NULL_TREE;
20356 decl = object_decl;
20360 decl = lookup_name_real (name, tag_type != none_type,
20362 /*block_p=*/true, is_namespace, flags);
20363 parser->qualifying_scope = NULL_TREE;
20364 parser->object_scope = NULL_TREE;
20367 /* If the lookup failed, let our caller know. */
20368 if (!decl || decl == error_mark_node)
20369 return error_mark_node;
20371 /* Pull out the template from an injected-class-name (or multiple). */
20373 decl = maybe_get_template_decl_from_type_decl (decl);
20375 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
20376 if (TREE_CODE (decl) == TREE_LIST)
20378 if (ambiguous_decls)
20379 *ambiguous_decls = decl;
20380 /* The error message we have to print is too complicated for
20381 cp_parser_error, so we incorporate its actions directly. */
20382 if (!cp_parser_simulate_error (parser))
20384 error_at (name_location, "reference to %qD is ambiguous",
20386 print_candidates (decl);
20388 return error_mark_node;
20391 gcc_assert (DECL_P (decl)
20392 || TREE_CODE (decl) == OVERLOAD
20393 || TREE_CODE (decl) == SCOPE_REF
20394 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20395 || BASELINK_P (decl));
20397 /* If we have resolved the name of a member declaration, check to
20398 see if the declaration is accessible. When the name resolves to
20399 set of overloaded functions, accessibility is checked when
20400 overload resolution is done.
20402 During an explicit instantiation, access is not checked at all,
20403 as per [temp.explicit]. */
20405 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20407 maybe_record_typedef_use (decl);
20412 /* Like cp_parser_lookup_name, but for use in the typical case where
20413 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20414 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
20417 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20419 return cp_parser_lookup_name (parser, name,
20421 /*is_template=*/false,
20422 /*is_namespace=*/false,
20423 /*check_dependency=*/true,
20424 /*ambiguous_decls=*/NULL,
20428 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20429 the current context, return the TYPE_DECL. If TAG_NAME_P is
20430 true, the DECL indicates the class being defined in a class-head,
20431 or declared in an elaborated-type-specifier.
20433 Otherwise, return DECL. */
20436 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20438 /* If the TEMPLATE_DECL is being declared as part of a class-head,
20439 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20442 template <typename T> struct B;
20445 template <typename T> struct A::B {};
20447 Similarly, in an elaborated-type-specifier:
20449 namespace N { struct X{}; }
20452 template <typename T> friend struct N::X;
20455 However, if the DECL refers to a class type, and we are in
20456 the scope of the class, then the name lookup automatically
20457 finds the TYPE_DECL created by build_self_reference rather
20458 than a TEMPLATE_DECL. For example, in:
20460 template <class T> struct S {
20464 there is no need to handle such case. */
20466 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20467 return DECL_TEMPLATE_RESULT (decl);
20472 /* If too many, or too few, template-parameter lists apply to the
20473 declarator, issue an error message. Returns TRUE if all went well,
20474 and FALSE otherwise. */
20477 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20478 cp_declarator *declarator,
20479 location_t declarator_location)
20481 unsigned num_templates;
20483 /* We haven't seen any classes that involve template parameters yet. */
20486 switch (declarator->kind)
20489 if (declarator->u.id.qualifying_scope)
20493 scope = declarator->u.id.qualifying_scope;
20495 while (scope && CLASS_TYPE_P (scope))
20497 /* You're supposed to have one `template <...>'
20498 for every template class, but you don't need one
20499 for a full specialization. For example:
20501 template <class T> struct S{};
20502 template <> struct S<int> { void f(); };
20503 void S<int>::f () {}
20505 is correct; there shouldn't be a `template <>' for
20506 the definition of `S<int>::f'. */
20507 if (!CLASSTYPE_TEMPLATE_INFO (scope))
20508 /* If SCOPE does not have template information of any
20509 kind, then it is not a template, nor is it nested
20510 within a template. */
20512 if (explicit_class_specialization_p (scope))
20514 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20517 scope = TYPE_CONTEXT (scope);
20520 else if (TREE_CODE (declarator->u.id.unqualified_name)
20521 == TEMPLATE_ID_EXPR)
20522 /* If the DECLARATOR has the form `X<y>' then it uses one
20523 additional level of template parameters. */
20526 return cp_parser_check_template_parameters
20527 (parser, num_templates, declarator_location, declarator);
20533 case cdk_reference:
20535 return (cp_parser_check_declarator_template_parameters
20536 (parser, declarator->declarator, declarator_location));
20542 gcc_unreachable ();
20547 /* NUM_TEMPLATES were used in the current declaration. If that is
20548 invalid, return FALSE and issue an error messages. Otherwise,
20549 return TRUE. If DECLARATOR is non-NULL, then we are checking a
20550 declarator and we can print more accurate diagnostics. */
20553 cp_parser_check_template_parameters (cp_parser* parser,
20554 unsigned num_templates,
20555 location_t location,
20556 cp_declarator *declarator)
20558 /* If there are the same number of template classes and parameter
20559 lists, that's OK. */
20560 if (parser->num_template_parameter_lists == num_templates)
20562 /* If there are more, but only one more, then we are referring to a
20563 member template. That's OK too. */
20564 if (parser->num_template_parameter_lists == num_templates + 1)
20566 /* If there are more template classes than parameter lists, we have
20569 template <class T> void S<T>::R<T>::f (); */
20570 if (parser->num_template_parameter_lists < num_templates)
20572 if (declarator && !current_function_decl)
20573 error_at (location, "specializing member %<%T::%E%> "
20574 "requires %<template<>%> syntax",
20575 declarator->u.id.qualifying_scope,
20576 declarator->u.id.unqualified_name);
20577 else if (declarator)
20578 error_at (location, "invalid declaration of %<%T::%E%>",
20579 declarator->u.id.qualifying_scope,
20580 declarator->u.id.unqualified_name);
20582 error_at (location, "too few template-parameter-lists");
20585 /* Otherwise, there are too many template parameter lists. We have
20588 template <class T> template <class U> void S::f(); */
20589 error_at (location, "too many template-parameter-lists");
20593 /* Parse an optional `::' token indicating that the following name is
20594 from the global namespace. If so, PARSER->SCOPE is set to the
20595 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20596 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20597 Returns the new value of PARSER->SCOPE, if the `::' token is
20598 present, and NULL_TREE otherwise. */
20601 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20605 /* Peek at the next token. */
20606 token = cp_lexer_peek_token (parser->lexer);
20607 /* If we're looking at a `::' token then we're starting from the
20608 global namespace, not our current location. */
20609 if (token->type == CPP_SCOPE)
20611 /* Consume the `::' token. */
20612 cp_lexer_consume_token (parser->lexer);
20613 /* Set the SCOPE so that we know where to start the lookup. */
20614 parser->scope = global_namespace;
20615 parser->qualifying_scope = global_namespace;
20616 parser->object_scope = NULL_TREE;
20618 return parser->scope;
20620 else if (!current_scope_valid_p)
20622 parser->scope = NULL_TREE;
20623 parser->qualifying_scope = NULL_TREE;
20624 parser->object_scope = NULL_TREE;
20630 /* Returns TRUE if the upcoming token sequence is the start of a
20631 constructor declarator. If FRIEND_P is true, the declarator is
20632 preceded by the `friend' specifier. */
20635 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20637 bool constructor_p;
20638 tree nested_name_specifier;
20639 cp_token *next_token;
20641 /* The common case is that this is not a constructor declarator, so
20642 try to avoid doing lots of work if at all possible. It's not
20643 valid declare a constructor at function scope. */
20644 if (parser->in_function_body)
20646 /* And only certain tokens can begin a constructor declarator. */
20647 next_token = cp_lexer_peek_token (parser->lexer);
20648 if (next_token->type != CPP_NAME
20649 && next_token->type != CPP_SCOPE
20650 && next_token->type != CPP_NESTED_NAME_SPECIFIER
20651 && next_token->type != CPP_TEMPLATE_ID)
20654 /* Parse tentatively; we are going to roll back all of the tokens
20656 cp_parser_parse_tentatively (parser);
20657 /* Assume that we are looking at a constructor declarator. */
20658 constructor_p = true;
20660 /* Look for the optional `::' operator. */
20661 cp_parser_global_scope_opt (parser,
20662 /*current_scope_valid_p=*/false);
20663 /* Look for the nested-name-specifier. */
20664 nested_name_specifier
20665 = (cp_parser_nested_name_specifier_opt (parser,
20666 /*typename_keyword_p=*/false,
20667 /*check_dependency_p=*/false,
20669 /*is_declaration=*/false));
20670 /* Outside of a class-specifier, there must be a
20671 nested-name-specifier. */
20672 if (!nested_name_specifier &&
20673 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20675 constructor_p = false;
20676 else if (nested_name_specifier == error_mark_node)
20677 constructor_p = false;
20679 /* If we have a class scope, this is easy; DR 147 says that S::S always
20680 names the constructor, and no other qualified name could. */
20681 if (constructor_p && nested_name_specifier
20682 && CLASS_TYPE_P (nested_name_specifier))
20684 tree id = cp_parser_unqualified_id (parser,
20685 /*template_keyword_p=*/false,
20686 /*check_dependency_p=*/false,
20687 /*declarator_p=*/true,
20688 /*optional_p=*/false);
20689 if (is_overloaded_fn (id))
20690 id = DECL_NAME (get_first_fn (id));
20691 if (!constructor_name_p (id, nested_name_specifier))
20692 constructor_p = false;
20694 /* If we still think that this might be a constructor-declarator,
20695 look for a class-name. */
20696 else if (constructor_p)
20700 template <typename T> struct S {
20704 we must recognize that the nested `S' names a class. */
20706 type_decl = cp_parser_class_name (parser,
20707 /*typename_keyword_p=*/false,
20708 /*template_keyword_p=*/false,
20710 /*check_dependency_p=*/false,
20711 /*class_head_p=*/false,
20712 /*is_declaration=*/false);
20713 /* If there was no class-name, then this is not a constructor. */
20714 constructor_p = !cp_parser_error_occurred (parser);
20716 /* If we're still considering a constructor, we have to see a `(',
20717 to begin the parameter-declaration-clause, followed by either a
20718 `)', an `...', or a decl-specifier. We need to check for a
20719 type-specifier to avoid being fooled into thinking that:
20723 is a constructor. (It is actually a function named `f' that
20724 takes one parameter (of type `int') and returns a value of type
20727 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20728 constructor_p = false;
20731 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20732 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20733 /* A parameter declaration begins with a decl-specifier,
20734 which is either the "attribute" keyword, a storage class
20735 specifier, or (usually) a type-specifier. */
20736 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20739 tree pushed_scope = NULL_TREE;
20740 unsigned saved_num_template_parameter_lists;
20742 /* Names appearing in the type-specifier should be looked up
20743 in the scope of the class. */
20744 if (current_class_type)
20748 type = TREE_TYPE (type_decl);
20749 if (TREE_CODE (type) == TYPENAME_TYPE)
20751 type = resolve_typename_type (type,
20752 /*only_current_p=*/false);
20753 if (TREE_CODE (type) == TYPENAME_TYPE)
20755 cp_parser_abort_tentative_parse (parser);
20759 pushed_scope = push_scope (type);
20762 /* Inside the constructor parameter list, surrounding
20763 template-parameter-lists do not apply. */
20764 saved_num_template_parameter_lists
20765 = parser->num_template_parameter_lists;
20766 parser->num_template_parameter_lists = 0;
20768 /* Look for the type-specifier. */
20769 cp_parser_type_specifier (parser,
20770 CP_PARSER_FLAGS_NONE,
20771 /*decl_specs=*/NULL,
20772 /*is_declarator=*/true,
20773 /*declares_class_or_enum=*/NULL,
20774 /*is_cv_qualifier=*/NULL);
20776 parser->num_template_parameter_lists
20777 = saved_num_template_parameter_lists;
20779 /* Leave the scope of the class. */
20781 pop_scope (pushed_scope);
20783 constructor_p = !cp_parser_error_occurred (parser);
20787 /* We did not really want to consume any tokens. */
20788 cp_parser_abort_tentative_parse (parser);
20790 return constructor_p;
20793 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20794 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
20795 they must be performed once we are in the scope of the function.
20797 Returns the function defined. */
20800 cp_parser_function_definition_from_specifiers_and_declarator
20801 (cp_parser* parser,
20802 cp_decl_specifier_seq *decl_specifiers,
20804 const cp_declarator *declarator)
20809 /* Begin the function-definition. */
20810 success_p = start_function (decl_specifiers, declarator, attributes);
20812 /* The things we're about to see are not directly qualified by any
20813 template headers we've seen thus far. */
20814 reset_specialization ();
20816 /* If there were names looked up in the decl-specifier-seq that we
20817 did not check, check them now. We must wait until we are in the
20818 scope of the function to perform the checks, since the function
20819 might be a friend. */
20820 perform_deferred_access_checks ();
20824 /* Skip the entire function. */
20825 cp_parser_skip_to_end_of_block_or_statement (parser);
20826 fn = error_mark_node;
20828 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20830 /* Seen already, skip it. An error message has already been output. */
20831 cp_parser_skip_to_end_of_block_or_statement (parser);
20832 fn = current_function_decl;
20833 current_function_decl = NULL_TREE;
20834 /* If this is a function from a class, pop the nested class. */
20835 if (current_class_name)
20836 pop_nested_class ();
20841 if (DECL_DECLARED_INLINE_P (current_function_decl))
20842 tv = TV_PARSE_INLINE;
20844 tv = TV_PARSE_FUNC;
20846 fn = cp_parser_function_definition_after_declarator (parser,
20847 /*inline_p=*/false);
20854 /* Parse the part of a function-definition that follows the
20855 declarator. INLINE_P is TRUE iff this function is an inline
20856 function defined within a class-specifier.
20858 Returns the function defined. */
20861 cp_parser_function_definition_after_declarator (cp_parser* parser,
20865 bool ctor_initializer_p = false;
20866 bool saved_in_unbraced_linkage_specification_p;
20867 bool saved_in_function_body;
20868 unsigned saved_num_template_parameter_lists;
20871 saved_in_function_body = parser->in_function_body;
20872 parser->in_function_body = true;
20873 /* If the next token is `return', then the code may be trying to
20874 make use of the "named return value" extension that G++ used to
20876 token = cp_lexer_peek_token (parser->lexer);
20877 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20879 /* Consume the `return' keyword. */
20880 cp_lexer_consume_token (parser->lexer);
20881 /* Look for the identifier that indicates what value is to be
20883 cp_parser_identifier (parser);
20884 /* Issue an error message. */
20885 error_at (token->location,
20886 "named return values are no longer supported");
20887 /* Skip tokens until we reach the start of the function body. */
20890 cp_token *token = cp_lexer_peek_token (parser->lexer);
20891 if (token->type == CPP_OPEN_BRACE
20892 || token->type == CPP_EOF
20893 || token->type == CPP_PRAGMA_EOL)
20895 cp_lexer_consume_token (parser->lexer);
20898 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20899 anything declared inside `f'. */
20900 saved_in_unbraced_linkage_specification_p
20901 = parser->in_unbraced_linkage_specification_p;
20902 parser->in_unbraced_linkage_specification_p = false;
20903 /* Inside the function, surrounding template-parameter-lists do not
20905 saved_num_template_parameter_lists
20906 = parser->num_template_parameter_lists;
20907 parser->num_template_parameter_lists = 0;
20909 start_lambda_scope (current_function_decl);
20911 /* If the next token is `try', then we are looking at a
20912 function-try-block. */
20913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20914 ctor_initializer_p = cp_parser_function_try_block (parser);
20915 /* A function-try-block includes the function-body, so we only do
20916 this next part if we're not processing a function-try-block. */
20919 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20921 finish_lambda_scope ();
20923 /* Finish the function. */
20924 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20925 (inline_p ? 2 : 0));
20926 /* Generate code for it, if necessary. */
20927 expand_or_defer_fn (fn);
20928 /* Restore the saved values. */
20929 parser->in_unbraced_linkage_specification_p
20930 = saved_in_unbraced_linkage_specification_p;
20931 parser->num_template_parameter_lists
20932 = saved_num_template_parameter_lists;
20933 parser->in_function_body = saved_in_function_body;
20938 /* Parse a template-declaration, assuming that the `export' (and
20939 `extern') keywords, if present, has already been scanned. MEMBER_P
20940 is as for cp_parser_template_declaration. */
20943 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20945 tree decl = NULL_TREE;
20946 VEC (deferred_access_check,gc) *checks;
20947 tree parameter_list;
20948 bool friend_p = false;
20949 bool need_lang_pop;
20952 /* Look for the `template' keyword. */
20953 token = cp_lexer_peek_token (parser->lexer);
20954 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20958 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20960 if (at_class_scope_p () && current_function_decl)
20962 /* 14.5.2.2 [temp.mem]
20964 A local class shall not have member templates. */
20965 error_at (token->location,
20966 "invalid declaration of member template in local class");
20967 cp_parser_skip_to_end_of_block_or_statement (parser);
20972 A template ... shall not have C linkage. */
20973 if (current_lang_name == lang_name_c)
20975 error_at (token->location, "template with C linkage");
20976 /* Give it C++ linkage to avoid confusing other parts of the
20978 push_lang_context (lang_name_cplusplus);
20979 need_lang_pop = true;
20982 need_lang_pop = false;
20984 /* We cannot perform access checks on the template parameter
20985 declarations until we know what is being declared, just as we
20986 cannot check the decl-specifier list. */
20987 push_deferring_access_checks (dk_deferred);
20989 /* If the next token is `>', then we have an invalid
20990 specialization. Rather than complain about an invalid template
20991 parameter, issue an error message here. */
20992 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20994 cp_parser_error (parser, "invalid explicit specialization");
20995 begin_specialization ();
20996 parameter_list = NULL_TREE;
21000 /* Parse the template parameters. */
21001 parameter_list = cp_parser_template_parameter_list (parser);
21002 fixup_template_parms ();
21005 /* Get the deferred access checks from the parameter list. These
21006 will be checked once we know what is being declared, as for a
21007 member template the checks must be performed in the scope of the
21008 class containing the member. */
21009 checks = get_deferred_access_checks ();
21011 /* Look for the `>'. */
21012 cp_parser_skip_to_end_of_template_parameter_list (parser);
21013 /* We just processed one more parameter list. */
21014 ++parser->num_template_parameter_lists;
21015 /* If the next token is `template', there are more template
21017 if (cp_lexer_next_token_is_keyword (parser->lexer,
21019 cp_parser_template_declaration_after_export (parser, member_p);
21020 else if (cxx_dialect >= cxx0x
21021 && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21022 decl = cp_parser_alias_declaration (parser);
21025 /* There are no access checks when parsing a template, as we do not
21026 know if a specialization will be a friend. */
21027 push_deferring_access_checks (dk_no_check);
21028 token = cp_lexer_peek_token (parser->lexer);
21029 decl = cp_parser_single_declaration (parser,
21032 /*explicit_specialization_p=*/false,
21034 pop_deferring_access_checks ();
21036 /* If this is a member template declaration, let the front
21038 if (member_p && !friend_p && decl)
21040 if (TREE_CODE (decl) == TYPE_DECL)
21041 cp_parser_check_access_in_redeclaration (decl, token->location);
21043 decl = finish_member_template_decl (decl);
21045 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21046 make_friend_class (current_class_type, TREE_TYPE (decl),
21047 /*complain=*/true);
21049 /* We are done with the current parameter list. */
21050 --parser->num_template_parameter_lists;
21052 pop_deferring_access_checks ();
21055 finish_template_decl (parameter_list);
21057 /* Check the template arguments for a literal operator template. */
21059 && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21060 && UDLIT_OPER_P (DECL_NAME (decl)))
21063 if (parameter_list == NULL_TREE)
21067 int num_parms = TREE_VEC_LENGTH (parameter_list);
21068 if (num_parms != 1)
21072 tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21073 tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21074 if (TREE_TYPE (parm) != char_type_node
21075 || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21080 error ("literal operator template %qD has invalid parameter list."
21081 " Expected non-type template argument pack <char...>",
21084 /* Register member declarations. */
21085 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21086 finish_member_declaration (decl);
21087 /* For the erroneous case of a template with C linkage, we pushed an
21088 implicit C++ linkage scope; exit that scope now. */
21090 pop_lang_context ();
21091 /* If DECL is a function template, we must return to parse it later.
21092 (Even though there is no definition, there might be default
21093 arguments that need handling.) */
21094 if (member_p && decl
21095 && (TREE_CODE (decl) == FUNCTION_DECL
21096 || DECL_FUNCTION_TEMPLATE_P (decl)))
21097 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21100 /* Perform the deferred access checks from a template-parameter-list.
21101 CHECKS is a TREE_LIST of access checks, as returned by
21102 get_deferred_access_checks. */
21105 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21107 ++processing_template_parmlist;
21108 perform_access_checks (checks);
21109 --processing_template_parmlist;
21112 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21113 `function-definition' sequence. MEMBER_P is true, this declaration
21114 appears in a class scope.
21116 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
21117 *FRIEND_P is set to TRUE iff the declaration is a friend. */
21120 cp_parser_single_declaration (cp_parser* parser,
21121 VEC (deferred_access_check,gc)* checks,
21123 bool explicit_specialization_p,
21126 int declares_class_or_enum;
21127 tree decl = NULL_TREE;
21128 cp_decl_specifier_seq decl_specifiers;
21129 bool function_definition_p = false;
21130 cp_token *decl_spec_token_start;
21132 /* This function is only used when processing a template
21134 gcc_assert (innermost_scope_kind () == sk_template_parms
21135 || innermost_scope_kind () == sk_template_spec);
21137 /* Defer access checks until we know what is being declared. */
21138 push_deferring_access_checks (dk_deferred);
21140 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21142 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21143 cp_parser_decl_specifier_seq (parser,
21144 CP_PARSER_FLAGS_OPTIONAL,
21146 &declares_class_or_enum);
21148 *friend_p = cp_parser_friend_p (&decl_specifiers);
21150 /* There are no template typedefs. */
21151 if (decl_specifiers.specs[(int) ds_typedef])
21153 error_at (decl_spec_token_start->location,
21154 "template declaration of %<typedef%>");
21155 decl = error_mark_node;
21158 /* Gather up the access checks that occurred the
21159 decl-specifier-seq. */
21160 stop_deferring_access_checks ();
21162 /* Check for the declaration of a template class. */
21163 if (declares_class_or_enum)
21165 if (cp_parser_declares_only_class_p (parser))
21167 decl = shadow_tag (&decl_specifiers);
21172 friend template <typename T> struct A<T>::B;
21175 A<T>::B will be represented by a TYPENAME_TYPE, and
21176 therefore not recognized by shadow_tag. */
21177 if (friend_p && *friend_p
21179 && decl_specifiers.type
21180 && TYPE_P (decl_specifiers.type))
21181 decl = decl_specifiers.type;
21183 if (decl && decl != error_mark_node)
21184 decl = TYPE_NAME (decl);
21186 decl = error_mark_node;
21188 /* Perform access checks for template parameters. */
21189 cp_parser_perform_template_parameter_access_checks (checks);
21193 /* Complain about missing 'typename' or other invalid type names. */
21194 if (!decl_specifiers.any_type_specifiers_p
21195 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21197 /* cp_parser_parse_and_diagnose_invalid_type_name calls
21198 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21199 the rest of this declaration. */
21200 decl = error_mark_node;
21204 /* If it's not a template class, try for a template function. If
21205 the next token is a `;', then this declaration does not declare
21206 anything. But, if there were errors in the decl-specifiers, then
21207 the error might well have come from an attempted class-specifier.
21208 In that case, there's no need to warn about a missing declarator. */
21210 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21211 || decl_specifiers.type != error_mark_node))
21213 decl = cp_parser_init_declarator (parser,
21216 /*function_definition_allowed_p=*/true,
21218 declares_class_or_enum,
21219 &function_definition_p,
21222 /* 7.1.1-1 [dcl.stc]
21224 A storage-class-specifier shall not be specified in an explicit
21225 specialization... */
21227 && explicit_specialization_p
21228 && decl_specifiers.storage_class != sc_none)
21230 error_at (decl_spec_token_start->location,
21231 "explicit template specialization cannot have a storage class");
21232 decl = error_mark_node;
21236 /* Look for a trailing `;' after the declaration. */
21237 if (!function_definition_p
21238 && (decl == error_mark_node
21239 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21240 cp_parser_skip_to_end_of_block_or_statement (parser);
21243 pop_deferring_access_checks ();
21245 /* Clear any current qualification; whatever comes next is the start
21246 of something new. */
21247 parser->scope = NULL_TREE;
21248 parser->qualifying_scope = NULL_TREE;
21249 parser->object_scope = NULL_TREE;
21254 /* Parse a cast-expression that is not the operand of a unary "&". */
21257 cp_parser_simple_cast_expression (cp_parser *parser)
21259 return cp_parser_cast_expression (parser, /*address_p=*/false,
21260 /*cast_p=*/false, NULL);
21263 /* Parse a functional cast to TYPE. Returns an expression
21264 representing the cast. */
21267 cp_parser_functional_cast (cp_parser* parser, tree type)
21270 tree expression_list;
21274 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21276 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21277 expression_list = cp_parser_braced_list (parser, &nonconst_p);
21278 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21279 if (TREE_CODE (type) == TYPE_DECL)
21280 type = TREE_TYPE (type);
21281 return finish_compound_literal (type, expression_list,
21282 tf_warning_or_error);
21286 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21288 /*allow_expansion_p=*/true,
21289 /*non_constant_p=*/NULL);
21291 expression_list = error_mark_node;
21294 expression_list = build_tree_list_vec (vec);
21295 release_tree_vector (vec);
21298 cast = build_functional_cast (type, expression_list,
21299 tf_warning_or_error);
21300 /* [expr.const]/1: In an integral constant expression "only type
21301 conversions to integral or enumeration type can be used". */
21302 if (TREE_CODE (type) == TYPE_DECL)
21303 type = TREE_TYPE (type);
21304 if (cast != error_mark_node
21305 && !cast_valid_in_integral_constant_expression_p (type)
21306 && cp_parser_non_integral_constant_expression (parser,
21308 return error_mark_node;
21312 /* Save the tokens that make up the body of a member function defined
21313 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
21314 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
21315 specifiers applied to the declaration. Returns the FUNCTION_DECL
21316 for the member function. */
21319 cp_parser_save_member_function_body (cp_parser* parser,
21320 cp_decl_specifier_seq *decl_specifiers,
21321 cp_declarator *declarator,
21328 /* Create the FUNCTION_DECL. */
21329 fn = grokmethod (decl_specifiers, declarator, attributes);
21330 /* If something went badly wrong, bail out now. */
21331 if (fn == error_mark_node)
21333 /* If there's a function-body, skip it. */
21334 if (cp_parser_token_starts_function_definition_p
21335 (cp_lexer_peek_token (parser->lexer)))
21336 cp_parser_skip_to_end_of_block_or_statement (parser);
21337 return error_mark_node;
21340 /* Remember it, if there default args to post process. */
21341 cp_parser_save_default_args (parser, fn);
21343 /* Save away the tokens that make up the body of the
21345 first = parser->lexer->next_token;
21346 /* We can have braced-init-list mem-initializers before the fn body. */
21347 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21349 cp_lexer_consume_token (parser->lexer);
21350 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21351 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21353 /* cache_group will stop after an un-nested { } pair, too. */
21354 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21357 /* variadic mem-inits have ... after the ')'. */
21358 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21359 cp_lexer_consume_token (parser->lexer);
21362 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21363 /* Handle function try blocks. */
21364 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21365 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21366 last = parser->lexer->next_token;
21368 /* Save away the inline definition; we will process it when the
21369 class is complete. */
21370 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21371 DECL_PENDING_INLINE_P (fn) = 1;
21373 /* We need to know that this was defined in the class, so that
21374 friend templates are handled correctly. */
21375 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21377 /* Add FN to the queue of functions to be parsed later. */
21378 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21383 /* Save the tokens that make up the in-class initializer for a non-static
21384 data member. Returns a DEFAULT_ARG. */
21387 cp_parser_save_nsdmi (cp_parser* parser)
21389 /* Save away the tokens that make up the body of the
21391 cp_token *first = parser->lexer->next_token;
21395 /* Save tokens until the next comma or semicolon. */
21396 cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21398 last = parser->lexer->next_token;
21400 node = make_node (DEFAULT_ARG);
21401 DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21402 DEFARG_INSTANTIATIONS (node) = NULL;
21408 /* Parse a template-argument-list, as well as the trailing ">" (but
21409 not the opening "<"). See cp_parser_template_argument_list for the
21413 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21417 tree saved_qualifying_scope;
21418 tree saved_object_scope;
21419 bool saved_greater_than_is_operator_p;
21420 int saved_unevaluated_operand;
21421 int saved_inhibit_evaluation_warnings;
21425 When parsing a template-id, the first non-nested `>' is taken as
21426 the end of the template-argument-list rather than a greater-than
21428 saved_greater_than_is_operator_p
21429 = parser->greater_than_is_operator_p;
21430 parser->greater_than_is_operator_p = false;
21431 /* Parsing the argument list may modify SCOPE, so we save it
21433 saved_scope = parser->scope;
21434 saved_qualifying_scope = parser->qualifying_scope;
21435 saved_object_scope = parser->object_scope;
21436 /* We need to evaluate the template arguments, even though this
21437 template-id may be nested within a "sizeof". */
21438 saved_unevaluated_operand = cp_unevaluated_operand;
21439 cp_unevaluated_operand = 0;
21440 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21441 c_inhibit_evaluation_warnings = 0;
21442 /* Parse the template-argument-list itself. */
21443 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21444 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21445 arguments = NULL_TREE;
21447 arguments = cp_parser_template_argument_list (parser);
21448 /* Look for the `>' that ends the template-argument-list. If we find
21449 a '>>' instead, it's probably just a typo. */
21450 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21452 if (cxx_dialect != cxx98)
21454 /* In C++0x, a `>>' in a template argument list or cast
21455 expression is considered to be two separate `>'
21456 tokens. So, change the current token to a `>', but don't
21457 consume it: it will be consumed later when the outer
21458 template argument list (or cast expression) is parsed.
21459 Note that this replacement of `>' for `>>' is necessary
21460 even if we are parsing tentatively: in the tentative
21461 case, after calling
21462 cp_parser_enclosed_template_argument_list we will always
21463 throw away all of the template arguments and the first
21464 closing `>', either because the template argument list
21465 was erroneous or because we are replacing those tokens
21466 with a CPP_TEMPLATE_ID token. The second `>' (which will
21467 not have been thrown away) is needed either to close an
21468 outer template argument list or to complete a new-style
21470 cp_token *token = cp_lexer_peek_token (parser->lexer);
21471 token->type = CPP_GREATER;
21473 else if (!saved_greater_than_is_operator_p)
21475 /* If we're in a nested template argument list, the '>>' has
21476 to be a typo for '> >'. We emit the error message, but we
21477 continue parsing and we push a '>' as next token, so that
21478 the argument list will be parsed correctly. Note that the
21479 global source location is still on the token before the
21480 '>>', so we need to say explicitly where we want it. */
21481 cp_token *token = cp_lexer_peek_token (parser->lexer);
21482 error_at (token->location, "%<>>%> should be %<> >%> "
21483 "within a nested template argument list");
21485 token->type = CPP_GREATER;
21489 /* If this is not a nested template argument list, the '>>'
21490 is a typo for '>'. Emit an error message and continue.
21491 Same deal about the token location, but here we can get it
21492 right by consuming the '>>' before issuing the diagnostic. */
21493 cp_token *token = cp_lexer_consume_token (parser->lexer);
21494 error_at (token->location,
21495 "spurious %<>>%>, use %<>%> to terminate "
21496 "a template argument list");
21500 cp_parser_skip_to_end_of_template_parameter_list (parser);
21501 /* The `>' token might be a greater-than operator again now. */
21502 parser->greater_than_is_operator_p
21503 = saved_greater_than_is_operator_p;
21504 /* Restore the SAVED_SCOPE. */
21505 parser->scope = saved_scope;
21506 parser->qualifying_scope = saved_qualifying_scope;
21507 parser->object_scope = saved_object_scope;
21508 cp_unevaluated_operand = saved_unevaluated_operand;
21509 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21514 /* MEMBER_FUNCTION is a member function, or a friend. If default
21515 arguments, or the body of the function have not yet been parsed,
21519 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21521 timevar_push (TV_PARSE_INMETH);
21522 /* If this member is a template, get the underlying
21524 if (DECL_FUNCTION_TEMPLATE_P (member_function))
21525 member_function = DECL_TEMPLATE_RESULT (member_function);
21527 /* There should not be any class definitions in progress at this
21528 point; the bodies of members are only parsed outside of all class
21530 gcc_assert (parser->num_classes_being_defined == 0);
21531 /* While we're parsing the member functions we might encounter more
21532 classes. We want to handle them right away, but we don't want
21533 them getting mixed up with functions that are currently in the
21535 push_unparsed_function_queues (parser);
21537 /* Make sure that any template parameters are in scope. */
21538 maybe_begin_member_template_processing (member_function);
21540 /* If the body of the function has not yet been parsed, parse it
21542 if (DECL_PENDING_INLINE_P (member_function))
21544 tree function_scope;
21545 cp_token_cache *tokens;
21547 /* The function is no longer pending; we are processing it. */
21548 tokens = DECL_PENDING_INLINE_INFO (member_function);
21549 DECL_PENDING_INLINE_INFO (member_function) = NULL;
21550 DECL_PENDING_INLINE_P (member_function) = 0;
21552 /* If this is a local class, enter the scope of the containing
21554 function_scope = current_function_decl;
21555 if (function_scope)
21556 push_function_context ();
21558 /* Push the body of the function onto the lexer stack. */
21559 cp_parser_push_lexer_for_tokens (parser, tokens);
21561 /* Let the front end know that we going to be defining this
21563 start_preparsed_function (member_function, NULL_TREE,
21564 SF_PRE_PARSED | SF_INCLASS_INLINE);
21566 /* Don't do access checking if it is a templated function. */
21567 if (processing_template_decl)
21568 push_deferring_access_checks (dk_no_check);
21570 /* Now, parse the body of the function. */
21571 cp_parser_function_definition_after_declarator (parser,
21572 /*inline_p=*/true);
21574 if (processing_template_decl)
21575 pop_deferring_access_checks ();
21577 /* Leave the scope of the containing function. */
21578 if (function_scope)
21579 pop_function_context ();
21580 cp_parser_pop_lexer (parser);
21583 /* Remove any template parameters from the symbol table. */
21584 maybe_end_member_template_processing ();
21586 /* Restore the queue. */
21587 pop_unparsed_function_queues (parser);
21588 timevar_pop (TV_PARSE_INMETH);
21591 /* If DECL contains any default args, remember it on the unparsed
21592 functions queue. */
21595 cp_parser_save_default_args (cp_parser* parser, tree decl)
21599 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21601 probe = TREE_CHAIN (probe))
21602 if (TREE_PURPOSE (probe))
21604 cp_default_arg_entry *entry
21605 = VEC_safe_push (cp_default_arg_entry, gc,
21606 unparsed_funs_with_default_args, NULL);
21607 entry->class_type = current_class_type;
21608 entry->decl = decl;
21613 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21614 which is either a FIELD_DECL or PARM_DECL. Parse it and return
21615 the result. For a PARM_DECL, PARMTYPE is the corresponding type
21616 from the parameter-type-list. */
21619 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21620 tree default_arg, tree parmtype)
21622 cp_token_cache *tokens;
21626 /* Push the saved tokens for the default argument onto the parser's
21628 tokens = DEFARG_TOKENS (default_arg);
21629 cp_parser_push_lexer_for_tokens (parser, tokens);
21631 start_lambda_scope (decl);
21633 /* Parse the default argument. */
21634 parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21635 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21636 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21638 finish_lambda_scope ();
21640 if (!processing_template_decl)
21642 /* In a non-template class, check conversions now. In a template,
21643 we'll wait and instantiate these as needed. */
21644 if (TREE_CODE (decl) == PARM_DECL)
21645 parsed_arg = check_default_argument (parmtype, parsed_arg);
21648 int flags = LOOKUP_IMPLICIT;
21649 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21650 && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21651 flags = LOOKUP_NORMAL;
21652 parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21656 /* If the token stream has not been completely used up, then
21657 there was extra junk after the end of the default
21659 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21661 if (TREE_CODE (decl) == PARM_DECL)
21662 cp_parser_error (parser, "expected %<,%>");
21664 cp_parser_error (parser, "expected %<;%>");
21667 /* Revert to the main lexer. */
21668 cp_parser_pop_lexer (parser);
21673 /* FIELD is a non-static data member with an initializer which we saved for
21674 later; parse it now. */
21677 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21681 push_unparsed_function_queues (parser);
21682 def = cp_parser_late_parse_one_default_arg (parser, field,
21683 DECL_INITIAL (field),
21685 pop_unparsed_function_queues (parser);
21687 DECL_INITIAL (field) = def;
21690 /* FN is a FUNCTION_DECL which may contains a parameter with an
21691 unparsed DEFAULT_ARG. Parse the default args now. This function
21692 assumes that the current scope is the scope in which the default
21693 argument should be processed. */
21696 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21698 bool saved_local_variables_forbidden_p;
21699 tree parm, parmdecl;
21701 /* While we're parsing the default args, we might (due to the
21702 statement expression extension) encounter more classes. We want
21703 to handle them right away, but we don't want them getting mixed
21704 up with default args that are currently in the queue. */
21705 push_unparsed_function_queues (parser);
21707 /* Local variable names (and the `this' keyword) may not appear
21708 in a default argument. */
21709 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21710 parser->local_variables_forbidden_p = true;
21712 push_defarg_context (fn);
21714 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21715 parmdecl = DECL_ARGUMENTS (fn);
21716 parm && parm != void_list_node;
21717 parm = TREE_CHAIN (parm),
21718 parmdecl = DECL_CHAIN (parmdecl))
21720 tree default_arg = TREE_PURPOSE (parm);
21722 VEC(tree,gc) *insts;
21729 if (TREE_CODE (default_arg) != DEFAULT_ARG)
21730 /* This can happen for a friend declaration for a function
21731 already declared with default arguments. */
21735 = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21737 TREE_VALUE (parm));
21738 if (parsed_arg == error_mark_node)
21743 TREE_PURPOSE (parm) = parsed_arg;
21745 /* Update any instantiations we've already created. */
21746 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21747 VEC_iterate (tree, insts, ix, copy); ix++)
21748 TREE_PURPOSE (copy) = parsed_arg;
21751 pop_defarg_context ();
21753 /* Make sure no default arg is missing. */
21754 check_default_args (fn);
21756 /* Restore the state of local_variables_forbidden_p. */
21757 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21759 /* Restore the queue. */
21760 pop_unparsed_function_queues (parser);
21763 /* Parse the operand of `sizeof' (or a similar operator). Returns
21764 either a TYPE or an expression, depending on the form of the
21765 input. The KEYWORD indicates which kind of expression we have
21769 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21771 tree expr = NULL_TREE;
21772 const char *saved_message;
21774 bool saved_integral_constant_expression_p;
21775 bool saved_non_integral_constant_expression_p;
21776 bool pack_expansion_p = false;
21778 /* Types cannot be defined in a `sizeof' expression. Save away the
21780 saved_message = parser->type_definition_forbidden_message;
21781 /* And create the new one. */
21782 tmp = concat ("types may not be defined in %<",
21783 IDENTIFIER_POINTER (ridpointers[keyword]),
21784 "%> expressions", NULL);
21785 parser->type_definition_forbidden_message = tmp;
21787 /* The restrictions on constant-expressions do not apply inside
21788 sizeof expressions. */
21789 saved_integral_constant_expression_p
21790 = parser->integral_constant_expression_p;
21791 saved_non_integral_constant_expression_p
21792 = parser->non_integral_constant_expression_p;
21793 parser->integral_constant_expression_p = false;
21795 /* If it's a `...', then we are computing the length of a parameter
21797 if (keyword == RID_SIZEOF
21798 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21800 /* Consume the `...'. */
21801 cp_lexer_consume_token (parser->lexer);
21802 maybe_warn_variadic_templates ();
21804 /* Note that this is an expansion. */
21805 pack_expansion_p = true;
21808 /* Do not actually evaluate the expression. */
21809 ++cp_unevaluated_operand;
21810 ++c_inhibit_evaluation_warnings;
21811 /* If it's a `(', then we might be looking at the type-id
21813 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21816 bool saved_in_type_id_in_expr_p;
21818 /* We can't be sure yet whether we're looking at a type-id or an
21820 cp_parser_parse_tentatively (parser);
21821 /* Consume the `('. */
21822 cp_lexer_consume_token (parser->lexer);
21823 /* Parse the type-id. */
21824 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21825 parser->in_type_id_in_expr_p = true;
21826 type = cp_parser_type_id (parser);
21827 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21828 /* Now, look for the trailing `)'. */
21829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21830 /* If all went well, then we're done. */
21831 if (cp_parser_parse_definitely (parser))
21833 cp_decl_specifier_seq decl_specs;
21835 /* Build a trivial decl-specifier-seq. */
21836 clear_decl_specs (&decl_specs);
21837 decl_specs.type = type;
21839 /* Call grokdeclarator to figure out what type this is. */
21840 expr = grokdeclarator (NULL,
21844 /*attrlist=*/NULL);
21848 /* If the type-id production did not work out, then we must be
21849 looking at the unary-expression production. */
21851 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21852 /*cast_p=*/false, NULL);
21854 if (pack_expansion_p)
21855 /* Build a pack expansion. */
21856 expr = make_pack_expansion (expr);
21858 /* Go back to evaluating expressions. */
21859 --cp_unevaluated_operand;
21860 --c_inhibit_evaluation_warnings;
21862 /* Free the message we created. */
21864 /* And restore the old one. */
21865 parser->type_definition_forbidden_message = saved_message;
21866 parser->integral_constant_expression_p
21867 = saved_integral_constant_expression_p;
21868 parser->non_integral_constant_expression_p
21869 = saved_non_integral_constant_expression_p;
21874 /* If the current declaration has no declarator, return true. */
21877 cp_parser_declares_only_class_p (cp_parser *parser)
21879 /* If the next token is a `;' or a `,' then there is no
21881 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21882 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21885 /* Update the DECL_SPECS to reflect the storage class indicated by
21889 cp_parser_set_storage_class (cp_parser *parser,
21890 cp_decl_specifier_seq *decl_specs,
21892 location_t location)
21894 cp_storage_class storage_class;
21896 if (parser->in_unbraced_linkage_specification_p)
21898 error_at (location, "invalid use of %qD in linkage specification",
21899 ridpointers[keyword]);
21902 else if (decl_specs->storage_class != sc_none)
21904 decl_specs->conflicting_specifiers_p = true;
21908 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21909 && decl_specs->specs[(int) ds_thread])
21911 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21912 decl_specs->specs[(int) ds_thread] = 0;
21918 storage_class = sc_auto;
21921 storage_class = sc_register;
21924 storage_class = sc_static;
21927 storage_class = sc_extern;
21930 storage_class = sc_mutable;
21933 gcc_unreachable ();
21935 decl_specs->storage_class = storage_class;
21937 /* A storage class specifier cannot be applied alongside a typedef
21938 specifier. If there is a typedef specifier present then set
21939 conflicting_specifiers_p which will trigger an error later
21940 on in grokdeclarator. */
21941 if (decl_specs->specs[(int)ds_typedef])
21942 decl_specs->conflicting_specifiers_p = true;
21945 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
21946 is true, the type is a class or enum definition. */
21949 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21951 location_t location,
21952 bool type_definition_p)
21954 decl_specs->any_specifiers_p = true;
21956 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21957 (with, for example, in "typedef int wchar_t;") we remember that
21958 this is what happened. In system headers, we ignore these
21959 declarations so that G++ can work with system headers that are not
21961 if (decl_specs->specs[(int) ds_typedef]
21962 && !type_definition_p
21963 && (type_spec == boolean_type_node
21964 || type_spec == char16_type_node
21965 || type_spec == char32_type_node
21966 || type_spec == wchar_type_node)
21967 && (decl_specs->type
21968 || decl_specs->specs[(int) ds_long]
21969 || decl_specs->specs[(int) ds_short]
21970 || decl_specs->specs[(int) ds_unsigned]
21971 || decl_specs->specs[(int) ds_signed]))
21973 decl_specs->redefined_builtin_type = type_spec;
21974 if (!decl_specs->type)
21976 decl_specs->type = type_spec;
21977 decl_specs->type_definition_p = false;
21978 decl_specs->type_location = location;
21981 else if (decl_specs->type)
21982 decl_specs->multiple_types_p = true;
21985 decl_specs->type = type_spec;
21986 decl_specs->type_definition_p = type_definition_p;
21987 decl_specs->redefined_builtin_type = NULL_TREE;
21988 decl_specs->type_location = location;
21992 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21993 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
21996 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21998 return decl_specifiers->specs[(int) ds_friend] != 0;
22001 /* Issue an error message indicating that TOKEN_DESC was expected.
22002 If KEYWORD is true, it indicated this function is called by
22003 cp_parser_require_keword and the required token can only be
22004 a indicated keyword. */
22007 cp_parser_required_error (cp_parser *parser,
22008 required_token token_desc,
22011 switch (token_desc)
22014 cp_parser_error (parser, "expected %<new%>");
22017 cp_parser_error (parser, "expected %<delete%>");
22020 cp_parser_error (parser, "expected %<return%>");
22023 cp_parser_error (parser, "expected %<while%>");
22026 cp_parser_error (parser, "expected %<extern%>");
22028 case RT_STATIC_ASSERT:
22029 cp_parser_error (parser, "expected %<static_assert%>");
22032 cp_parser_error (parser, "expected %<decltype%>");
22035 cp_parser_error (parser, "expected %<operator%>");
22038 cp_parser_error (parser, "expected %<class%>");
22041 cp_parser_error (parser, "expected %<template%>");
22044 cp_parser_error (parser, "expected %<namespace%>");
22047 cp_parser_error (parser, "expected %<using%>");
22050 cp_parser_error (parser, "expected %<asm%>");
22053 cp_parser_error (parser, "expected %<try%>");
22056 cp_parser_error (parser, "expected %<catch%>");
22059 cp_parser_error (parser, "expected %<throw%>");
22062 cp_parser_error (parser, "expected %<__label__%>");
22065 cp_parser_error (parser, "expected %<@try%>");
22067 case RT_AT_SYNCHRONIZED:
22068 cp_parser_error (parser, "expected %<@synchronized%>");
22071 cp_parser_error (parser, "expected %<@throw%>");
22078 switch (token_desc)
22081 cp_parser_error (parser, "expected %<;%>");
22083 case RT_OPEN_PAREN:
22084 cp_parser_error (parser, "expected %<(%>");
22086 case RT_CLOSE_BRACE:
22087 cp_parser_error (parser, "expected %<}%>");
22089 case RT_OPEN_BRACE:
22090 cp_parser_error (parser, "expected %<{%>");
22092 case RT_CLOSE_SQUARE:
22093 cp_parser_error (parser, "expected %<]%>");
22095 case RT_OPEN_SQUARE:
22096 cp_parser_error (parser, "expected %<[%>");
22099 cp_parser_error (parser, "expected %<,%>");
22102 cp_parser_error (parser, "expected %<::%>");
22105 cp_parser_error (parser, "expected %<<%>");
22108 cp_parser_error (parser, "expected %<>%>");
22111 cp_parser_error (parser, "expected %<=%>");
22114 cp_parser_error (parser, "expected %<...%>");
22117 cp_parser_error (parser, "expected %<*%>");
22120 cp_parser_error (parser, "expected %<~%>");
22123 cp_parser_error (parser, "expected %<:%>");
22125 case RT_COLON_SCOPE:
22126 cp_parser_error (parser, "expected %<:%> or %<::%>");
22128 case RT_CLOSE_PAREN:
22129 cp_parser_error (parser, "expected %<)%>");
22131 case RT_COMMA_CLOSE_PAREN:
22132 cp_parser_error (parser, "expected %<,%> or %<)%>");
22134 case RT_PRAGMA_EOL:
22135 cp_parser_error (parser, "expected end of line");
22138 cp_parser_error (parser, "expected identifier");
22141 cp_parser_error (parser, "expected selection-statement");
22143 case RT_INTERATION:
22144 cp_parser_error (parser, "expected iteration-statement");
22147 cp_parser_error (parser, "expected jump-statement");
22150 cp_parser_error (parser, "expected class-key");
22152 case RT_CLASS_TYPENAME_TEMPLATE:
22153 cp_parser_error (parser,
22154 "expected %<class%>, %<typename%>, or %<template%>");
22157 gcc_unreachable ();
22161 gcc_unreachable ();
22166 /* If the next token is of the indicated TYPE, consume it. Otherwise,
22167 issue an error message indicating that TOKEN_DESC was expected.
22169 Returns the token consumed, if the token had the appropriate type.
22170 Otherwise, returns NULL. */
22173 cp_parser_require (cp_parser* parser,
22174 enum cpp_ttype type,
22175 required_token token_desc)
22177 if (cp_lexer_next_token_is (parser->lexer, type))
22178 return cp_lexer_consume_token (parser->lexer);
22181 /* Output the MESSAGE -- unless we're parsing tentatively. */
22182 if (!cp_parser_simulate_error (parser))
22183 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22188 /* An error message is produced if the next token is not '>'.
22189 All further tokens are skipped until the desired token is
22190 found or '{', '}', ';' or an unbalanced ')' or ']'. */
22193 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22195 /* Current level of '< ... >'. */
22196 unsigned level = 0;
22197 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
22198 unsigned nesting_depth = 0;
22200 /* Are we ready, yet? If not, issue error message. */
22201 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22204 /* Skip tokens until the desired token is found. */
22207 /* Peek at the next token. */
22208 switch (cp_lexer_peek_token (parser->lexer)->type)
22211 if (!nesting_depth)
22216 if (cxx_dialect == cxx98)
22217 /* C++0x views the `>>' operator as two `>' tokens, but
22220 else if (!nesting_depth && level-- == 0)
22222 /* We've hit a `>>' where the first `>' closes the
22223 template argument list, and the second `>' is
22224 spurious. Just consume the `>>' and stop; we've
22225 already produced at least one error. */
22226 cp_lexer_consume_token (parser->lexer);
22229 /* Fall through for C++0x, so we handle the second `>' in
22233 if (!nesting_depth && level-- == 0)
22235 /* We've reached the token we want, consume it and stop. */
22236 cp_lexer_consume_token (parser->lexer);
22241 case CPP_OPEN_PAREN:
22242 case CPP_OPEN_SQUARE:
22246 case CPP_CLOSE_PAREN:
22247 case CPP_CLOSE_SQUARE:
22248 if (nesting_depth-- == 0)
22253 case CPP_PRAGMA_EOL:
22254 case CPP_SEMICOLON:
22255 case CPP_OPEN_BRACE:
22256 case CPP_CLOSE_BRACE:
22257 /* The '>' was probably forgotten, don't look further. */
22264 /* Consume this token. */
22265 cp_lexer_consume_token (parser->lexer);
22269 /* If the next token is the indicated keyword, consume it. Otherwise,
22270 issue an error message indicating that TOKEN_DESC was expected.
22272 Returns the token consumed, if the token had the appropriate type.
22273 Otherwise, returns NULL. */
22276 cp_parser_require_keyword (cp_parser* parser,
22278 required_token token_desc)
22280 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22282 if (token && token->keyword != keyword)
22284 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
22291 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22292 function-definition. */
22295 cp_parser_token_starts_function_definition_p (cp_token* token)
22297 return (/* An ordinary function-body begins with an `{'. */
22298 token->type == CPP_OPEN_BRACE
22299 /* A ctor-initializer begins with a `:'. */
22300 || token->type == CPP_COLON
22301 /* A function-try-block begins with `try'. */
22302 || token->keyword == RID_TRY
22303 /* The named return value extension begins with `return'. */
22304 || token->keyword == RID_RETURN);
22307 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22311 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22315 token = cp_lexer_peek_token (parser->lexer);
22316 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22319 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22320 C++0x) ending a template-argument. */
22323 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22327 token = cp_lexer_peek_token (parser->lexer);
22328 return (token->type == CPP_COMMA
22329 || token->type == CPP_GREATER
22330 || token->type == CPP_ELLIPSIS
22331 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22334 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22335 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
22338 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22343 token = cp_lexer_peek_nth_token (parser->lexer, n);
22344 if (token->type == CPP_LESS)
22346 /* Check for the sequence `<::' in the original code. It would be lexed as
22347 `[:', where `[' is a digraph, and there is no whitespace before
22349 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22352 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22353 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22359 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22360 or none_type otherwise. */
22362 static enum tag_types
22363 cp_parser_token_is_class_key (cp_token* token)
22365 switch (token->keyword)
22370 return record_type;
22379 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
22382 cp_parser_check_class_key (enum tag_types class_key, tree type)
22384 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22385 permerror (input_location, "%qs tag used in naming %q#T",
22386 class_key == union_type ? "union"
22387 : class_key == record_type ? "struct" : "class",
22391 /* Issue an error message if DECL is redeclared with different
22392 access than its original declaration [class.access.spec/3].
22393 This applies to nested classes and nested class templates.
22397 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22399 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22402 if ((TREE_PRIVATE (decl)
22403 != (current_access_specifier == access_private_node))
22404 || (TREE_PROTECTED (decl)
22405 != (current_access_specifier == access_protected_node)))
22406 error_at (location, "%qD redeclared with different access", decl);
22409 /* Look for the `template' keyword, as a syntactic disambiguator.
22410 Return TRUE iff it is present, in which case it will be
22414 cp_parser_optional_template_keyword (cp_parser *parser)
22416 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22418 /* The `template' keyword can only be used within templates;
22419 outside templates the parser can always figure out what is a
22420 template and what is not. */
22421 if (!processing_template_decl)
22423 cp_token *token = cp_lexer_peek_token (parser->lexer);
22424 error_at (token->location,
22425 "%<template%> (as a disambiguator) is only allowed "
22426 "within templates");
22427 /* If this part of the token stream is rescanned, the same
22428 error message would be generated. So, we purge the token
22429 from the stream. */
22430 cp_lexer_purge_token (parser->lexer);
22435 /* Consume the `template' keyword. */
22436 cp_lexer_consume_token (parser->lexer);
22444 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
22445 set PARSER->SCOPE, and perform other related actions. */
22448 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22451 struct tree_check *check_value;
22452 deferred_access_check *chk;
22453 VEC (deferred_access_check,gc) *checks;
22455 /* Get the stored value. */
22456 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22457 /* Perform any access checks that were deferred. */
22458 checks = check_value->checks;
22461 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22462 perform_or_defer_access_check (chk->binfo,
22466 /* Set the scope from the stored value. */
22467 parser->scope = check_value->value;
22468 parser->qualifying_scope = check_value->qualifying_scope;
22469 parser->object_scope = NULL_TREE;
22472 /* Consume tokens up through a non-nested END token. Returns TRUE if we
22473 encounter the end of a block before what we were looking for. */
22476 cp_parser_cache_group (cp_parser *parser,
22477 enum cpp_ttype end,
22482 cp_token *token = cp_lexer_peek_token (parser->lexer);
22484 /* Abort a parenthesized expression if we encounter a semicolon. */
22485 if ((end == CPP_CLOSE_PAREN || depth == 0)
22486 && token->type == CPP_SEMICOLON)
22488 /* If we've reached the end of the file, stop. */
22489 if (token->type == CPP_EOF
22490 || (end != CPP_PRAGMA_EOL
22491 && token->type == CPP_PRAGMA_EOL))
22493 if (token->type == CPP_CLOSE_BRACE && depth == 0)
22494 /* We've hit the end of an enclosing block, so there's been some
22495 kind of syntax error. */
22498 /* If we're caching something finished by a comma (or semicolon),
22499 such as an NSDMI, don't consume the comma. */
22500 if (end == CPP_COMMA
22501 && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22504 /* Consume the token. */
22505 cp_lexer_consume_token (parser->lexer);
22506 /* See if it starts a new group. */
22507 if (token->type == CPP_OPEN_BRACE)
22509 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22510 /* In theory this should probably check end == '}', but
22511 cp_parser_save_member_function_body needs it to exit
22512 after either '}' or ')' when called with ')'. */
22516 else if (token->type == CPP_OPEN_PAREN)
22518 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22519 if (depth == 0 && end == CPP_CLOSE_PAREN)
22522 else if (token->type == CPP_PRAGMA)
22523 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22524 else if (token->type == end)
22529 /* Begin parsing tentatively. We always save tokens while parsing
22530 tentatively so that if the tentative parsing fails we can restore the
22534 cp_parser_parse_tentatively (cp_parser* parser)
22536 /* Enter a new parsing context. */
22537 parser->context = cp_parser_context_new (parser->context);
22538 /* Begin saving tokens. */
22539 cp_lexer_save_tokens (parser->lexer);
22540 /* In order to avoid repetitive access control error messages,
22541 access checks are queued up until we are no longer parsing
22543 push_deferring_access_checks (dk_deferred);
22546 /* Commit to the currently active tentative parse. */
22549 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22551 cp_parser_context *context;
22554 /* Mark all of the levels as committed. */
22555 lexer = parser->lexer;
22556 for (context = parser->context; context->next; context = context->next)
22558 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22560 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22561 while (!cp_lexer_saving_tokens (lexer))
22562 lexer = lexer->next;
22563 cp_lexer_commit_tokens (lexer);
22567 /* Abort the currently active tentative parse. All consumed tokens
22568 will be rolled back, and no diagnostics will be issued. */
22571 cp_parser_abort_tentative_parse (cp_parser* parser)
22573 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22574 || errorcount > 0);
22575 cp_parser_simulate_error (parser);
22576 /* Now, pretend that we want to see if the construct was
22577 successfully parsed. */
22578 cp_parser_parse_definitely (parser);
22581 /* Stop parsing tentatively. If a parse error has occurred, restore the
22582 token stream. Otherwise, commit to the tokens we have consumed.
22583 Returns true if no error occurred; false otherwise. */
22586 cp_parser_parse_definitely (cp_parser* parser)
22588 bool error_occurred;
22589 cp_parser_context *context;
22591 /* Remember whether or not an error occurred, since we are about to
22592 destroy that information. */
22593 error_occurred = cp_parser_error_occurred (parser);
22594 /* Remove the topmost context from the stack. */
22595 context = parser->context;
22596 parser->context = context->next;
22597 /* If no parse errors occurred, commit to the tentative parse. */
22598 if (!error_occurred)
22600 /* Commit to the tokens read tentatively, unless that was
22602 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22603 cp_lexer_commit_tokens (parser->lexer);
22605 pop_to_parent_deferring_access_checks ();
22607 /* Otherwise, if errors occurred, roll back our state so that things
22608 are just as they were before we began the tentative parse. */
22611 cp_lexer_rollback_tokens (parser->lexer);
22612 pop_deferring_access_checks ();
22614 /* Add the context to the front of the free list. */
22615 context->next = cp_parser_context_free_list;
22616 cp_parser_context_free_list = context;
22618 return !error_occurred;
22621 /* Returns true if we are parsing tentatively and are not committed to
22622 this tentative parse. */
22625 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22627 return (cp_parser_parsing_tentatively (parser)
22628 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22631 /* Returns nonzero iff an error has occurred during the most recent
22632 tentative parse. */
22635 cp_parser_error_occurred (cp_parser* parser)
22637 return (cp_parser_parsing_tentatively (parser)
22638 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22641 /* Returns nonzero if GNU extensions are allowed. */
22644 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22646 return parser->allow_gnu_extensions_p;
22649 /* Objective-C++ Productions */
22652 /* Parse an Objective-C expression, which feeds into a primary-expression
22656 objc-message-expression
22657 objc-string-literal
22658 objc-encode-expression
22659 objc-protocol-expression
22660 objc-selector-expression
22662 Returns a tree representation of the expression. */
22665 cp_parser_objc_expression (cp_parser* parser)
22667 /* Try to figure out what kind of declaration is present. */
22668 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22672 case CPP_OPEN_SQUARE:
22673 return cp_parser_objc_message_expression (parser);
22675 case CPP_OBJC_STRING:
22676 kwd = cp_lexer_consume_token (parser->lexer);
22677 return objc_build_string_object (kwd->u.value);
22680 switch (kwd->keyword)
22682 case RID_AT_ENCODE:
22683 return cp_parser_objc_encode_expression (parser);
22685 case RID_AT_PROTOCOL:
22686 return cp_parser_objc_protocol_expression (parser);
22688 case RID_AT_SELECTOR:
22689 return cp_parser_objc_selector_expression (parser);
22695 error_at (kwd->location,
22696 "misplaced %<@%D%> Objective-C++ construct",
22698 cp_parser_skip_to_end_of_block_or_statement (parser);
22701 return error_mark_node;
22704 /* Parse an Objective-C message expression.
22706 objc-message-expression:
22707 [ objc-message-receiver objc-message-args ]
22709 Returns a representation of an Objective-C message. */
22712 cp_parser_objc_message_expression (cp_parser* parser)
22714 tree receiver, messageargs;
22716 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
22717 receiver = cp_parser_objc_message_receiver (parser);
22718 messageargs = cp_parser_objc_message_args (parser);
22719 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22721 return objc_build_message_expr (receiver, messageargs);
22724 /* Parse an objc-message-receiver.
22726 objc-message-receiver:
22728 simple-type-specifier
22730 Returns a representation of the type or expression. */
22733 cp_parser_objc_message_receiver (cp_parser* parser)
22737 /* An Objective-C message receiver may be either (1) a type
22738 or (2) an expression. */
22739 cp_parser_parse_tentatively (parser);
22740 rcv = cp_parser_expression (parser, false, NULL);
22742 if (cp_parser_parse_definitely (parser))
22745 rcv = cp_parser_simple_type_specifier (parser,
22746 /*decl_specs=*/NULL,
22747 CP_PARSER_FLAGS_NONE);
22749 return objc_get_class_reference (rcv);
22752 /* Parse the arguments and selectors comprising an Objective-C message.
22757 objc-selector-args , objc-comma-args
22759 objc-selector-args:
22760 objc-selector [opt] : assignment-expression
22761 objc-selector-args objc-selector [opt] : assignment-expression
22764 assignment-expression
22765 objc-comma-args , assignment-expression
22767 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22768 selector arguments and TREE_VALUE containing a list of comma
22772 cp_parser_objc_message_args (cp_parser* parser)
22774 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22775 bool maybe_unary_selector_p = true;
22776 cp_token *token = cp_lexer_peek_token (parser->lexer);
22778 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22780 tree selector = NULL_TREE, arg;
22782 if (token->type != CPP_COLON)
22783 selector = cp_parser_objc_selector (parser);
22785 /* Detect if we have a unary selector. */
22786 if (maybe_unary_selector_p
22787 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22788 return build_tree_list (selector, NULL_TREE);
22790 maybe_unary_selector_p = false;
22791 cp_parser_require (parser, CPP_COLON, RT_COLON);
22792 arg = cp_parser_assignment_expression (parser, false, NULL);
22795 = chainon (sel_args,
22796 build_tree_list (selector, arg));
22798 token = cp_lexer_peek_token (parser->lexer);
22801 /* Handle non-selector arguments, if any. */
22802 while (token->type == CPP_COMMA)
22806 cp_lexer_consume_token (parser->lexer);
22807 arg = cp_parser_assignment_expression (parser, false, NULL);
22810 = chainon (addl_args,
22811 build_tree_list (NULL_TREE, arg));
22813 token = cp_lexer_peek_token (parser->lexer);
22816 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22818 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22819 return build_tree_list (error_mark_node, error_mark_node);
22822 return build_tree_list (sel_args, addl_args);
22825 /* Parse an Objective-C encode expression.
22827 objc-encode-expression:
22828 @encode objc-typename
22830 Returns an encoded representation of the type argument. */
22833 cp_parser_objc_encode_expression (cp_parser* parser)
22838 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
22839 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22840 token = cp_lexer_peek_token (parser->lexer);
22841 type = complete_type (cp_parser_type_id (parser));
22842 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22846 error_at (token->location,
22847 "%<@encode%> must specify a type as an argument");
22848 return error_mark_node;
22851 /* This happens if we find @encode(T) (where T is a template
22852 typename or something dependent on a template typename) when
22853 parsing a template. In that case, we can't compile it
22854 immediately, but we rather create an AT_ENCODE_EXPR which will
22855 need to be instantiated when the template is used.
22857 if (dependent_type_p (type))
22859 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22860 TREE_READONLY (value) = 1;
22864 return objc_build_encode_expr (type);
22867 /* Parse an Objective-C @defs expression. */
22870 cp_parser_objc_defs_expression (cp_parser *parser)
22874 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
22875 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22876 name = cp_parser_identifier (parser);
22877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22879 return objc_get_class_ivars (name);
22882 /* Parse an Objective-C protocol expression.
22884 objc-protocol-expression:
22885 @protocol ( identifier )
22887 Returns a representation of the protocol expression. */
22890 cp_parser_objc_protocol_expression (cp_parser* parser)
22894 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22896 proto = cp_parser_identifier (parser);
22897 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22899 return objc_build_protocol_expr (proto);
22902 /* Parse an Objective-C selector expression.
22904 objc-selector-expression:
22905 @selector ( objc-method-signature )
22907 objc-method-signature:
22913 objc-selector-seq objc-selector :
22915 Returns a representation of the method selector. */
22918 cp_parser_objc_selector_expression (cp_parser* parser)
22920 tree sel_seq = NULL_TREE;
22921 bool maybe_unary_selector_p = true;
22923 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22925 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
22926 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22927 token = cp_lexer_peek_token (parser->lexer);
22929 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22930 || token->type == CPP_SCOPE)
22932 tree selector = NULL_TREE;
22934 if (token->type != CPP_COLON
22935 || token->type == CPP_SCOPE)
22936 selector = cp_parser_objc_selector (parser);
22938 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22939 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22941 /* Detect if we have a unary selector. */
22942 if (maybe_unary_selector_p)
22944 sel_seq = selector;
22945 goto finish_selector;
22949 cp_parser_error (parser, "expected %<:%>");
22952 maybe_unary_selector_p = false;
22953 token = cp_lexer_consume_token (parser->lexer);
22955 if (token->type == CPP_SCOPE)
22958 = chainon (sel_seq,
22959 build_tree_list (selector, NULL_TREE));
22961 = chainon (sel_seq,
22962 build_tree_list (NULL_TREE, NULL_TREE));
22966 = chainon (sel_seq,
22967 build_tree_list (selector, NULL_TREE));
22969 token = cp_lexer_peek_token (parser->lexer);
22973 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22975 return objc_build_selector_expr (loc, sel_seq);
22978 /* Parse a list of identifiers.
22980 objc-identifier-list:
22982 objc-identifier-list , identifier
22984 Returns a TREE_LIST of identifier nodes. */
22987 cp_parser_objc_identifier_list (cp_parser* parser)
22993 identifier = cp_parser_identifier (parser);
22994 if (identifier == error_mark_node)
22995 return error_mark_node;
22997 list = build_tree_list (NULL_TREE, identifier);
22998 sep = cp_lexer_peek_token (parser->lexer);
23000 while (sep->type == CPP_COMMA)
23002 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23003 identifier = cp_parser_identifier (parser);
23004 if (identifier == error_mark_node)
23007 list = chainon (list, build_tree_list (NULL_TREE,
23009 sep = cp_lexer_peek_token (parser->lexer);
23015 /* Parse an Objective-C alias declaration.
23017 objc-alias-declaration:
23018 @compatibility_alias identifier identifier ;
23020 This function registers the alias mapping with the Objective-C front end.
23021 It returns nothing. */
23024 cp_parser_objc_alias_declaration (cp_parser* parser)
23028 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
23029 alias = cp_parser_identifier (parser);
23030 orig = cp_parser_identifier (parser);
23031 objc_declare_alias (alias, orig);
23032 cp_parser_consume_semicolon_at_end_of_statement (parser);
23035 /* Parse an Objective-C class forward-declaration.
23037 objc-class-declaration:
23038 @class objc-identifier-list ;
23040 The function registers the forward declarations with the Objective-C
23041 front end. It returns nothing. */
23044 cp_parser_objc_class_declaration (cp_parser* parser)
23046 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
23051 id = cp_parser_identifier (parser);
23052 if (id == error_mark_node)
23055 objc_declare_class (id);
23057 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23058 cp_lexer_consume_token (parser->lexer);
23062 cp_parser_consume_semicolon_at_end_of_statement (parser);
23065 /* Parse a list of Objective-C protocol references.
23067 objc-protocol-refs-opt:
23068 objc-protocol-refs [opt]
23070 objc-protocol-refs:
23071 < objc-identifier-list >
23073 Returns a TREE_LIST of identifiers, if any. */
23076 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23078 tree protorefs = NULL_TREE;
23080 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23082 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
23083 protorefs = cp_parser_objc_identifier_list (parser);
23084 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23090 /* Parse a Objective-C visibility specification. */
23093 cp_parser_objc_visibility_spec (cp_parser* parser)
23095 cp_token *vis = cp_lexer_peek_token (parser->lexer);
23097 switch (vis->keyword)
23099 case RID_AT_PRIVATE:
23100 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23102 case RID_AT_PROTECTED:
23103 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23105 case RID_AT_PUBLIC:
23106 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23108 case RID_AT_PACKAGE:
23109 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23115 /* Eat '@private'/'@protected'/'@public'. */
23116 cp_lexer_consume_token (parser->lexer);
23119 /* Parse an Objective-C method type. Return 'true' if it is a class
23120 (+) method, and 'false' if it is an instance (-) method. */
23123 cp_parser_objc_method_type (cp_parser* parser)
23125 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23131 /* Parse an Objective-C protocol qualifier. */
23134 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23136 tree quals = NULL_TREE, node;
23137 cp_token *token = cp_lexer_peek_token (parser->lexer);
23139 node = token->u.value;
23141 while (node && TREE_CODE (node) == IDENTIFIER_NODE
23142 && (node == ridpointers [(int) RID_IN]
23143 || node == ridpointers [(int) RID_OUT]
23144 || node == ridpointers [(int) RID_INOUT]
23145 || node == ridpointers [(int) RID_BYCOPY]
23146 || node == ridpointers [(int) RID_BYREF]
23147 || node == ridpointers [(int) RID_ONEWAY]))
23149 quals = tree_cons (NULL_TREE, node, quals);
23150 cp_lexer_consume_token (parser->lexer);
23151 token = cp_lexer_peek_token (parser->lexer);
23152 node = token->u.value;
23158 /* Parse an Objective-C typename. */
23161 cp_parser_objc_typename (cp_parser* parser)
23163 tree type_name = NULL_TREE;
23165 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23167 tree proto_quals, cp_type = NULL_TREE;
23169 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23170 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23172 /* An ObjC type name may consist of just protocol qualifiers, in which
23173 case the type shall default to 'id'. */
23174 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23176 cp_type = cp_parser_type_id (parser);
23178 /* If the type could not be parsed, an error has already
23179 been produced. For error recovery, behave as if it had
23180 not been specified, which will use the default type
23182 if (cp_type == error_mark_node)
23184 cp_type = NULL_TREE;
23185 /* We need to skip to the closing parenthesis as
23186 cp_parser_type_id() does not seem to do it for
23188 cp_parser_skip_to_closing_parenthesis (parser,
23189 /*recovering=*/true,
23190 /*or_comma=*/false,
23191 /*consume_paren=*/false);
23195 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23196 type_name = build_tree_list (proto_quals, cp_type);
23202 /* Check to see if TYPE refers to an Objective-C selector name. */
23205 cp_parser_objc_selector_p (enum cpp_ttype type)
23207 return (type == CPP_NAME || type == CPP_KEYWORD
23208 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23209 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23210 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23211 || type == CPP_XOR || type == CPP_XOR_EQ);
23214 /* Parse an Objective-C selector. */
23217 cp_parser_objc_selector (cp_parser* parser)
23219 cp_token *token = cp_lexer_consume_token (parser->lexer);
23221 if (!cp_parser_objc_selector_p (token->type))
23223 error_at (token->location, "invalid Objective-C++ selector name");
23224 return error_mark_node;
23227 /* C++ operator names are allowed to appear in ObjC selectors. */
23228 switch (token->type)
23230 case CPP_AND_AND: return get_identifier ("and");
23231 case CPP_AND_EQ: return get_identifier ("and_eq");
23232 case CPP_AND: return get_identifier ("bitand");
23233 case CPP_OR: return get_identifier ("bitor");
23234 case CPP_COMPL: return get_identifier ("compl");
23235 case CPP_NOT: return get_identifier ("not");
23236 case CPP_NOT_EQ: return get_identifier ("not_eq");
23237 case CPP_OR_OR: return get_identifier ("or");
23238 case CPP_OR_EQ: return get_identifier ("or_eq");
23239 case CPP_XOR: return get_identifier ("xor");
23240 case CPP_XOR_EQ: return get_identifier ("xor_eq");
23241 default: return token->u.value;
23245 /* Parse an Objective-C params list. */
23248 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23250 tree params = NULL_TREE;
23251 bool maybe_unary_selector_p = true;
23252 cp_token *token = cp_lexer_peek_token (parser->lexer);
23254 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23256 tree selector = NULL_TREE, type_name, identifier;
23257 tree parm_attr = NULL_TREE;
23259 if (token->keyword == RID_ATTRIBUTE)
23262 if (token->type != CPP_COLON)
23263 selector = cp_parser_objc_selector (parser);
23265 /* Detect if we have a unary selector. */
23266 if (maybe_unary_selector_p
23267 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23269 params = selector; /* Might be followed by attributes. */
23273 maybe_unary_selector_p = false;
23274 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23276 /* Something went quite wrong. There should be a colon
23277 here, but there is not. Stop parsing parameters. */
23280 type_name = cp_parser_objc_typename (parser);
23281 /* New ObjC allows attributes on parameters too. */
23282 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23283 parm_attr = cp_parser_attributes_opt (parser);
23284 identifier = cp_parser_identifier (parser);
23288 objc_build_keyword_decl (selector,
23293 token = cp_lexer_peek_token (parser->lexer);
23296 if (params == NULL_TREE)
23298 cp_parser_error (parser, "objective-c++ method declaration is expected");
23299 return error_mark_node;
23302 /* We allow tail attributes for the method. */
23303 if (token->keyword == RID_ATTRIBUTE)
23305 *attributes = cp_parser_attributes_opt (parser);
23306 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23307 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23309 cp_parser_error (parser,
23310 "method attributes must be specified at the end");
23311 return error_mark_node;
23314 if (params == NULL_TREE)
23316 cp_parser_error (parser, "objective-c++ method declaration is expected");
23317 return error_mark_node;
23322 /* Parse the non-keyword Objective-C params. */
23325 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
23328 tree params = make_node (TREE_LIST);
23329 cp_token *token = cp_lexer_peek_token (parser->lexer);
23330 *ellipsisp = false; /* Initially, assume no ellipsis. */
23332 while (token->type == CPP_COMMA)
23334 cp_parameter_declarator *parmdecl;
23337 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23338 token = cp_lexer_peek_token (parser->lexer);
23340 if (token->type == CPP_ELLIPSIS)
23342 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
23344 token = cp_lexer_peek_token (parser->lexer);
23348 /* TODO: parse attributes for tail parameters. */
23349 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23350 parm = grokdeclarator (parmdecl->declarator,
23351 &parmdecl->decl_specifiers,
23352 PARM, /*initialized=*/0,
23353 /*attrlist=*/NULL);
23355 chainon (params, build_tree_list (NULL_TREE, parm));
23356 token = cp_lexer_peek_token (parser->lexer);
23359 /* We allow tail attributes for the method. */
23360 if (token->keyword == RID_ATTRIBUTE)
23362 if (*attributes == NULL_TREE)
23364 *attributes = cp_parser_attributes_opt (parser);
23365 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23366 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23370 /* We have an error, but parse the attributes, so that we can
23372 *attributes = cp_parser_attributes_opt (parser);
23374 cp_parser_error (parser,
23375 "method attributes must be specified at the end");
23376 return error_mark_node;
23382 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
23385 cp_parser_objc_interstitial_code (cp_parser* parser)
23387 cp_token *token = cp_lexer_peek_token (parser->lexer);
23389 /* If the next token is `extern' and the following token is a string
23390 literal, then we have a linkage specification. */
23391 if (token->keyword == RID_EXTERN
23392 && cp_parser_is_pure_string_literal
23393 (cp_lexer_peek_nth_token (parser->lexer, 2)))
23394 cp_parser_linkage_specification (parser);
23395 /* Handle #pragma, if any. */
23396 else if (token->type == CPP_PRAGMA)
23397 cp_parser_pragma (parser, pragma_external);
23398 /* Allow stray semicolons. */
23399 else if (token->type == CPP_SEMICOLON)
23400 cp_lexer_consume_token (parser->lexer);
23401 /* Mark methods as optional or required, when building protocols. */
23402 else if (token->keyword == RID_AT_OPTIONAL)
23404 cp_lexer_consume_token (parser->lexer);
23405 objc_set_method_opt (true);
23407 else if (token->keyword == RID_AT_REQUIRED)
23409 cp_lexer_consume_token (parser->lexer);
23410 objc_set_method_opt (false);
23412 else if (token->keyword == RID_NAMESPACE)
23413 cp_parser_namespace_definition (parser);
23414 /* Other stray characters must generate errors. */
23415 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23417 cp_lexer_consume_token (parser->lexer);
23418 error ("stray %qs between Objective-C++ methods",
23419 token->type == CPP_OPEN_BRACE ? "{" : "}");
23421 /* Finally, try to parse a block-declaration, or a function-definition. */
23423 cp_parser_block_declaration (parser, /*statement_p=*/false);
23426 /* Parse a method signature. */
23429 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23431 tree rettype, kwdparms, optparms;
23432 bool ellipsis = false;
23433 bool is_class_method;
23435 is_class_method = cp_parser_objc_method_type (parser);
23436 rettype = cp_parser_objc_typename (parser);
23437 *attributes = NULL_TREE;
23438 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23439 if (kwdparms == error_mark_node)
23440 return error_mark_node;
23441 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23442 if (optparms == error_mark_node)
23443 return error_mark_node;
23445 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23449 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23452 cp_lexer_save_tokens (parser->lexer);
23453 tattr = cp_parser_attributes_opt (parser);
23454 gcc_assert (tattr) ;
23456 /* If the attributes are followed by a method introducer, this is not allowed.
23457 Dump the attributes and flag the situation. */
23458 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23459 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23462 /* Otherwise, the attributes introduce some interstitial code, possibly so
23463 rewind to allow that check. */
23464 cp_lexer_rollback_tokens (parser->lexer);
23468 /* Parse an Objective-C method prototype list. */
23471 cp_parser_objc_method_prototype_list (cp_parser* parser)
23473 cp_token *token = cp_lexer_peek_token (parser->lexer);
23475 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23477 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23479 tree attributes, sig;
23480 bool is_class_method;
23481 if (token->type == CPP_PLUS)
23482 is_class_method = true;
23484 is_class_method = false;
23485 sig = cp_parser_objc_method_signature (parser, &attributes);
23486 if (sig == error_mark_node)
23488 cp_parser_skip_to_end_of_block_or_statement (parser);
23489 token = cp_lexer_peek_token (parser->lexer);
23492 objc_add_method_declaration (is_class_method, sig, attributes);
23493 cp_parser_consume_semicolon_at_end_of_statement (parser);
23495 else if (token->keyword == RID_AT_PROPERTY)
23496 cp_parser_objc_at_property_declaration (parser);
23497 else if (token->keyword == RID_ATTRIBUTE
23498 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23499 warning_at (cp_lexer_peek_token (parser->lexer)->location,
23501 "prefix attributes are ignored for methods");
23503 /* Allow for interspersed non-ObjC++ code. */
23504 cp_parser_objc_interstitial_code (parser);
23506 token = cp_lexer_peek_token (parser->lexer);
23509 if (token->type != CPP_EOF)
23510 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23512 cp_parser_error (parser, "expected %<@end%>");
23514 objc_finish_interface ();
23517 /* Parse an Objective-C method definition list. */
23520 cp_parser_objc_method_definition_list (cp_parser* parser)
23522 cp_token *token = cp_lexer_peek_token (parser->lexer);
23524 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23528 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23531 tree sig, attribute;
23532 bool is_class_method;
23533 if (token->type == CPP_PLUS)
23534 is_class_method = true;
23536 is_class_method = false;
23537 push_deferring_access_checks (dk_deferred);
23538 sig = cp_parser_objc_method_signature (parser, &attribute);
23539 if (sig == error_mark_node)
23541 cp_parser_skip_to_end_of_block_or_statement (parser);
23542 token = cp_lexer_peek_token (parser->lexer);
23545 objc_start_method_definition (is_class_method, sig, attribute,
23548 /* For historical reasons, we accept an optional semicolon. */
23549 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23550 cp_lexer_consume_token (parser->lexer);
23552 ptk = cp_lexer_peek_token (parser->lexer);
23553 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
23554 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23556 perform_deferred_access_checks ();
23557 stop_deferring_access_checks ();
23558 meth = cp_parser_function_definition_after_declarator (parser,
23560 pop_deferring_access_checks ();
23561 objc_finish_method_definition (meth);
23564 /* The following case will be removed once @synthesize is
23565 completely implemented. */
23566 else if (token->keyword == RID_AT_PROPERTY)
23567 cp_parser_objc_at_property_declaration (parser);
23568 else if (token->keyword == RID_AT_SYNTHESIZE)
23569 cp_parser_objc_at_synthesize_declaration (parser);
23570 else if (token->keyword == RID_AT_DYNAMIC)
23571 cp_parser_objc_at_dynamic_declaration (parser);
23572 else if (token->keyword == RID_ATTRIBUTE
23573 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23574 warning_at (token->location, OPT_Wattributes,
23575 "prefix attributes are ignored for methods");
23577 /* Allow for interspersed non-ObjC++ code. */
23578 cp_parser_objc_interstitial_code (parser);
23580 token = cp_lexer_peek_token (parser->lexer);
23583 if (token->type != CPP_EOF)
23584 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23586 cp_parser_error (parser, "expected %<@end%>");
23588 objc_finish_implementation ();
23591 /* Parse Objective-C ivars. */
23594 cp_parser_objc_class_ivars (cp_parser* parser)
23596 cp_token *token = cp_lexer_peek_token (parser->lexer);
23598 if (token->type != CPP_OPEN_BRACE)
23599 return; /* No ivars specified. */
23601 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
23602 token = cp_lexer_peek_token (parser->lexer);
23604 while (token->type != CPP_CLOSE_BRACE
23605 && token->keyword != RID_AT_END && token->type != CPP_EOF)
23607 cp_decl_specifier_seq declspecs;
23608 int decl_class_or_enum_p;
23609 tree prefix_attributes;
23611 cp_parser_objc_visibility_spec (parser);
23613 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23616 cp_parser_decl_specifier_seq (parser,
23617 CP_PARSER_FLAGS_OPTIONAL,
23619 &decl_class_or_enum_p);
23621 /* auto, register, static, extern, mutable. */
23622 if (declspecs.storage_class != sc_none)
23624 cp_parser_error (parser, "invalid type for instance variable");
23625 declspecs.storage_class = sc_none;
23629 if (declspecs.specs[(int) ds_thread])
23631 cp_parser_error (parser, "invalid type for instance variable");
23632 declspecs.specs[(int) ds_thread] = 0;
23636 if (declspecs.specs[(int) ds_typedef])
23638 cp_parser_error (parser, "invalid type for instance variable");
23639 declspecs.specs[(int) ds_typedef] = 0;
23642 prefix_attributes = declspecs.attributes;
23643 declspecs.attributes = NULL_TREE;
23645 /* Keep going until we hit the `;' at the end of the
23647 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23649 tree width = NULL_TREE, attributes, first_attribute, decl;
23650 cp_declarator *declarator = NULL;
23651 int ctor_dtor_or_conv_p;
23653 /* Check for a (possibly unnamed) bitfield declaration. */
23654 token = cp_lexer_peek_token (parser->lexer);
23655 if (token->type == CPP_COLON)
23658 if (token->type == CPP_NAME
23659 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23662 /* Get the name of the bitfield. */
23663 declarator = make_id_declarator (NULL_TREE,
23664 cp_parser_identifier (parser),
23668 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23669 /* Get the width of the bitfield. */
23671 = cp_parser_constant_expression (parser,
23672 /*allow_non_constant=*/false,
23677 /* Parse the declarator. */
23679 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23680 &ctor_dtor_or_conv_p,
23681 /*parenthesized_p=*/NULL,
23682 /*member_p=*/false);
23685 /* Look for attributes that apply to the ivar. */
23686 attributes = cp_parser_attributes_opt (parser);
23687 /* Remember which attributes are prefix attributes and
23689 first_attribute = attributes;
23690 /* Combine the attributes. */
23691 attributes = chainon (prefix_attributes, attributes);
23694 /* Create the bitfield declaration. */
23695 decl = grokbitfield (declarator, &declspecs,
23699 decl = grokfield (declarator, &declspecs,
23700 NULL_TREE, /*init_const_expr_p=*/false,
23701 NULL_TREE, attributes);
23703 /* Add the instance variable. */
23704 if (decl != error_mark_node && decl != NULL_TREE)
23705 objc_add_instance_variable (decl);
23707 /* Reset PREFIX_ATTRIBUTES. */
23708 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23709 attributes = TREE_CHAIN (attributes);
23711 TREE_CHAIN (attributes) = NULL_TREE;
23713 token = cp_lexer_peek_token (parser->lexer);
23715 if (token->type == CPP_COMMA)
23717 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23723 cp_parser_consume_semicolon_at_end_of_statement (parser);
23724 token = cp_lexer_peek_token (parser->lexer);
23727 if (token->keyword == RID_AT_END)
23728 cp_parser_error (parser, "expected %<}%>");
23730 /* Do not consume the RID_AT_END, so it will be read again as terminating
23731 the @interface of @implementation. */
23732 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23733 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
23735 /* For historical reasons, we accept an optional semicolon. */
23736 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23737 cp_lexer_consume_token (parser->lexer);
23740 /* Parse an Objective-C protocol declaration. */
23743 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23745 tree proto, protorefs;
23748 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
23749 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23751 tok = cp_lexer_peek_token (parser->lexer);
23752 error_at (tok->location, "identifier expected after %<@protocol%>");
23753 cp_parser_consume_semicolon_at_end_of_statement (parser);
23757 /* See if we have a forward declaration or a definition. */
23758 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23760 /* Try a forward declaration first. */
23761 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23767 id = cp_parser_identifier (parser);
23768 if (id == error_mark_node)
23771 objc_declare_protocol (id, attributes);
23773 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23774 cp_lexer_consume_token (parser->lexer);
23778 cp_parser_consume_semicolon_at_end_of_statement (parser);
23781 /* Ok, we got a full-fledged definition (or at least should). */
23784 proto = cp_parser_identifier (parser);
23785 protorefs = cp_parser_objc_protocol_refs_opt (parser);
23786 objc_start_protocol (proto, protorefs, attributes);
23787 cp_parser_objc_method_prototype_list (parser);
23791 /* Parse an Objective-C superclass or category. */
23794 cp_parser_objc_superclass_or_category (cp_parser *parser,
23797 tree *categ, bool *is_class_extension)
23799 cp_token *next = cp_lexer_peek_token (parser->lexer);
23801 *super = *categ = NULL_TREE;
23802 *is_class_extension = false;
23803 if (next->type == CPP_COLON)
23805 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
23806 *super = cp_parser_identifier (parser);
23808 else if (next->type == CPP_OPEN_PAREN)
23810 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
23812 /* If there is no category name, and this is an @interface, we
23813 have a class extension. */
23814 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23816 *categ = NULL_TREE;
23817 *is_class_extension = true;
23820 *categ = cp_parser_identifier (parser);
23822 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23826 /* Parse an Objective-C class interface. */
23829 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23831 tree name, super, categ, protos;
23832 bool is_class_extension;
23834 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
23835 name = cp_parser_identifier (parser);
23836 if (name == error_mark_node)
23838 /* It's hard to recover because even if valid @interface stuff
23839 is to follow, we can't compile it (or validate it) if we
23840 don't even know which class it refers to. Let's assume this
23841 was a stray '@interface' token in the stream and skip it.
23845 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23846 &is_class_extension);
23847 protos = cp_parser_objc_protocol_refs_opt (parser);
23849 /* We have either a class or a category on our hands. */
23850 if (categ || is_class_extension)
23851 objc_start_category_interface (name, categ, protos, attributes);
23854 objc_start_class_interface (name, super, protos, attributes);
23855 /* Handle instance variable declarations, if any. */
23856 cp_parser_objc_class_ivars (parser);
23857 objc_continue_interface ();
23860 cp_parser_objc_method_prototype_list (parser);
23863 /* Parse an Objective-C class implementation. */
23866 cp_parser_objc_class_implementation (cp_parser* parser)
23868 tree name, super, categ;
23869 bool is_class_extension;
23871 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
23872 name = cp_parser_identifier (parser);
23873 if (name == error_mark_node)
23875 /* It's hard to recover because even if valid @implementation
23876 stuff is to follow, we can't compile it (or validate it) if
23877 we don't even know which class it refers to. Let's assume
23878 this was a stray '@implementation' token in the stream and
23883 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23884 &is_class_extension);
23886 /* We have either a class or a category on our hands. */
23888 objc_start_category_implementation (name, categ);
23891 objc_start_class_implementation (name, super);
23892 /* Handle instance variable declarations, if any. */
23893 cp_parser_objc_class_ivars (parser);
23894 objc_continue_implementation ();
23897 cp_parser_objc_method_definition_list (parser);
23900 /* Consume the @end token and finish off the implementation. */
23903 cp_parser_objc_end_implementation (cp_parser* parser)
23905 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
23906 objc_finish_implementation ();
23909 /* Parse an Objective-C declaration. */
23912 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23914 /* Try to figure out what kind of declaration is present. */
23915 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23918 switch (kwd->keyword)
23923 error_at (kwd->location, "attributes may not be specified before"
23924 " the %<@%D%> Objective-C++ keyword",
23928 case RID_AT_IMPLEMENTATION:
23929 warning_at (kwd->location, OPT_Wattributes,
23930 "prefix attributes are ignored before %<@%D%>",
23937 switch (kwd->keyword)
23940 cp_parser_objc_alias_declaration (parser);
23943 cp_parser_objc_class_declaration (parser);
23945 case RID_AT_PROTOCOL:
23946 cp_parser_objc_protocol_declaration (parser, attributes);
23948 case RID_AT_INTERFACE:
23949 cp_parser_objc_class_interface (parser, attributes);
23951 case RID_AT_IMPLEMENTATION:
23952 cp_parser_objc_class_implementation (parser);
23955 cp_parser_objc_end_implementation (parser);
23958 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23960 cp_parser_skip_to_end_of_block_or_statement (parser);
23964 /* Parse an Objective-C try-catch-finally statement.
23966 objc-try-catch-finally-stmt:
23967 @try compound-statement objc-catch-clause-seq [opt]
23968 objc-finally-clause [opt]
23970 objc-catch-clause-seq:
23971 objc-catch-clause objc-catch-clause-seq [opt]
23974 @catch ( objc-exception-declaration ) compound-statement
23976 objc-finally-clause:
23977 @finally compound-statement
23979 objc-exception-declaration:
23980 parameter-declaration
23983 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23987 PS: This function is identical to c_parser_objc_try_catch_finally_statement
23988 for C. Keep them in sync. */
23991 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23993 location_t location;
23996 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23997 location = cp_lexer_peek_token (parser->lexer)->location;
23998 objc_maybe_warn_exceptions (location);
23999 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24000 node, lest it get absorbed into the surrounding block. */
24001 stmt = push_stmt_list ();
24002 cp_parser_compound_statement (parser, NULL, false, false);
24003 objc_begin_try_stmt (location, pop_stmt_list (stmt));
24005 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24007 cp_parameter_declarator *parm;
24008 tree parameter_declaration = error_mark_node;
24009 bool seen_open_paren = false;
24011 cp_lexer_consume_token (parser->lexer);
24012 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24013 seen_open_paren = true;
24014 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24016 /* We have "@catch (...)" (where the '...' are literally
24017 what is in the code). Skip the '...'.
24018 parameter_declaration is set to NULL_TREE, and
24019 objc_being_catch_clauses() knows that that means
24021 cp_lexer_consume_token (parser->lexer);
24022 parameter_declaration = NULL_TREE;
24026 /* We have "@catch (NSException *exception)" or something
24027 like that. Parse the parameter declaration. */
24028 parm = cp_parser_parameter_declaration (parser, false, NULL);
24030 parameter_declaration = error_mark_node;
24032 parameter_declaration = grokdeclarator (parm->declarator,
24033 &parm->decl_specifiers,
24034 PARM, /*initialized=*/0,
24035 /*attrlist=*/NULL);
24037 if (seen_open_paren)
24038 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24041 /* If there was no open parenthesis, we are recovering from
24042 an error, and we are trying to figure out what mistake
24043 the user has made. */
24045 /* If there is an immediate closing parenthesis, the user
24046 probably forgot the opening one (ie, they typed "@catch
24047 NSException *e)". Parse the closing parenthesis and keep
24049 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24050 cp_lexer_consume_token (parser->lexer);
24052 /* If these is no immediate closing parenthesis, the user
24053 probably doesn't know that parenthesis are required at
24054 all (ie, they typed "@catch NSException *e"). So, just
24055 forget about the closing parenthesis and keep going. */
24057 objc_begin_catch_clause (parameter_declaration);
24058 cp_parser_compound_statement (parser, NULL, false, false);
24059 objc_finish_catch_clause ();
24061 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24063 cp_lexer_consume_token (parser->lexer);
24064 location = cp_lexer_peek_token (parser->lexer)->location;
24065 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24066 node, lest it get absorbed into the surrounding block. */
24067 stmt = push_stmt_list ();
24068 cp_parser_compound_statement (parser, NULL, false, false);
24069 objc_build_finally_clause (location, pop_stmt_list (stmt));
24072 return objc_finish_try_stmt ();
24075 /* Parse an Objective-C synchronized statement.
24077 objc-synchronized-stmt:
24078 @synchronized ( expression ) compound-statement
24080 Returns NULL_TREE. */
24083 cp_parser_objc_synchronized_statement (cp_parser *parser)
24085 location_t location;
24088 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24090 location = cp_lexer_peek_token (parser->lexer)->location;
24091 objc_maybe_warn_exceptions (location);
24092 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24093 lock = cp_parser_expression (parser, false, NULL);
24094 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24096 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24097 node, lest it get absorbed into the surrounding block. */
24098 stmt = push_stmt_list ();
24099 cp_parser_compound_statement (parser, NULL, false, false);
24101 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24104 /* Parse an Objective-C throw statement.
24107 @throw assignment-expression [opt] ;
24109 Returns a constructed '@throw' statement. */
24112 cp_parser_objc_throw_statement (cp_parser *parser)
24114 tree expr = NULL_TREE;
24115 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24117 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24119 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24120 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24122 cp_parser_consume_semicolon_at_end_of_statement (parser);
24124 return objc_build_throw_stmt (loc, expr);
24127 /* Parse an Objective-C statement. */
24130 cp_parser_objc_statement (cp_parser * parser)
24132 /* Try to figure out what kind of declaration is present. */
24133 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24135 switch (kwd->keyword)
24138 return cp_parser_objc_try_catch_finally_statement (parser);
24139 case RID_AT_SYNCHRONIZED:
24140 return cp_parser_objc_synchronized_statement (parser);
24142 return cp_parser_objc_throw_statement (parser);
24144 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24146 cp_parser_skip_to_end_of_block_or_statement (parser);
24149 return error_mark_node;
24152 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
24153 look ahead to see if an objc keyword follows the attributes. This
24154 is to detect the use of prefix attributes on ObjC @interface and
24158 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24160 cp_lexer_save_tokens (parser->lexer);
24161 *attrib = cp_parser_attributes_opt (parser);
24162 gcc_assert (*attrib);
24163 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24165 cp_lexer_commit_tokens (parser->lexer);
24168 cp_lexer_rollback_tokens (parser->lexer);
24172 /* This routine is a minimal replacement for
24173 c_parser_struct_declaration () used when parsing the list of
24174 types/names or ObjC++ properties. For example, when parsing the
24177 @property (readonly) int a, b, c;
24179 this function is responsible for parsing "int a, int b, int c" and
24180 returning the declarations as CHAIN of DECLs.
24182 TODO: Share this code with cp_parser_objc_class_ivars. It's very
24183 similar parsing. */
24185 cp_parser_objc_struct_declaration (cp_parser *parser)
24187 tree decls = NULL_TREE;
24188 cp_decl_specifier_seq declspecs;
24189 int decl_class_or_enum_p;
24190 tree prefix_attributes;
24192 cp_parser_decl_specifier_seq (parser,
24193 CP_PARSER_FLAGS_NONE,
24195 &decl_class_or_enum_p);
24197 if (declspecs.type == error_mark_node)
24198 return error_mark_node;
24200 /* auto, register, static, extern, mutable. */
24201 if (declspecs.storage_class != sc_none)
24203 cp_parser_error (parser, "invalid type for property");
24204 declspecs.storage_class = sc_none;
24208 if (declspecs.specs[(int) ds_thread])
24210 cp_parser_error (parser, "invalid type for property");
24211 declspecs.specs[(int) ds_thread] = 0;
24215 if (declspecs.specs[(int) ds_typedef])
24217 cp_parser_error (parser, "invalid type for property");
24218 declspecs.specs[(int) ds_typedef] = 0;
24221 prefix_attributes = declspecs.attributes;
24222 declspecs.attributes = NULL_TREE;
24224 /* Keep going until we hit the `;' at the end of the declaration. */
24225 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24227 tree attributes, first_attribute, decl;
24228 cp_declarator *declarator;
24231 /* Parse the declarator. */
24232 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24233 NULL, NULL, false);
24235 /* Look for attributes that apply to the ivar. */
24236 attributes = cp_parser_attributes_opt (parser);
24237 /* Remember which attributes are prefix attributes and
24239 first_attribute = attributes;
24240 /* Combine the attributes. */
24241 attributes = chainon (prefix_attributes, attributes);
24243 decl = grokfield (declarator, &declspecs,
24244 NULL_TREE, /*init_const_expr_p=*/false,
24245 NULL_TREE, attributes);
24247 if (decl == error_mark_node || decl == NULL_TREE)
24248 return error_mark_node;
24250 /* Reset PREFIX_ATTRIBUTES. */
24251 while (attributes && TREE_CHAIN (attributes) != first_attribute)
24252 attributes = TREE_CHAIN (attributes);
24254 TREE_CHAIN (attributes) = NULL_TREE;
24256 DECL_CHAIN (decl) = decls;
24259 token = cp_lexer_peek_token (parser->lexer);
24260 if (token->type == CPP_COMMA)
24262 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
24271 /* Parse an Objective-C @property declaration. The syntax is:
24273 objc-property-declaration:
24274 '@property' objc-property-attributes[opt] struct-declaration ;
24276 objc-property-attributes:
24277 '(' objc-property-attribute-list ')'
24279 objc-property-attribute-list:
24280 objc-property-attribute
24281 objc-property-attribute-list, objc-property-attribute
24283 objc-property-attribute
24284 'getter' = identifier
24285 'setter' = identifier
24294 @property NSString *name;
24295 @property (readonly) id object;
24296 @property (retain, nonatomic, getter=getTheName) id name;
24297 @property int a, b, c;
24299 PS: This function is identical to
24300 c_parser_objc_at_property_declaration for C. Keep them in sync. */
24302 cp_parser_objc_at_property_declaration (cp_parser *parser)
24304 /* The following variables hold the attributes of the properties as
24305 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
24306 seen. When we see an attribute, we set them to 'true' (if they
24307 are boolean properties) or to the identifier (if they have an
24308 argument, ie, for getter and setter). Note that here we only
24309 parse the list of attributes, check the syntax and accumulate the
24310 attributes that we find. objc_add_property_declaration() will
24311 then process the information. */
24312 bool property_assign = false;
24313 bool property_copy = false;
24314 tree property_getter_ident = NULL_TREE;
24315 bool property_nonatomic = false;
24316 bool property_readonly = false;
24317 bool property_readwrite = false;
24318 bool property_retain = false;
24319 tree property_setter_ident = NULL_TREE;
24321 /* 'properties' is the list of properties that we read. Usually a
24322 single one, but maybe more (eg, in "@property int a, b, c;" there
24327 loc = cp_lexer_peek_token (parser->lexer)->location;
24329 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
24331 /* Parse the optional attribute list... */
24332 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24335 cp_lexer_consume_token (parser->lexer);
24339 bool syntax_error = false;
24340 cp_token *token = cp_lexer_peek_token (parser->lexer);
24343 if (token->type != CPP_NAME)
24345 cp_parser_error (parser, "expected identifier");
24348 keyword = C_RID_CODE (token->u.value);
24349 cp_lexer_consume_token (parser->lexer);
24352 case RID_ASSIGN: property_assign = true; break;
24353 case RID_COPY: property_copy = true; break;
24354 case RID_NONATOMIC: property_nonatomic = true; break;
24355 case RID_READONLY: property_readonly = true; break;
24356 case RID_READWRITE: property_readwrite = true; break;
24357 case RID_RETAIN: property_retain = true; break;
24361 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24363 if (keyword == RID_GETTER)
24364 cp_parser_error (parser,
24365 "missing %<=%> (after %<getter%> attribute)");
24367 cp_parser_error (parser,
24368 "missing %<=%> (after %<setter%> attribute)");
24369 syntax_error = true;
24372 cp_lexer_consume_token (parser->lexer); /* eat the = */
24373 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24375 cp_parser_error (parser, "expected identifier");
24376 syntax_error = true;
24379 if (keyword == RID_SETTER)
24381 if (property_setter_ident != NULL_TREE)
24383 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24384 cp_lexer_consume_token (parser->lexer);
24387 property_setter_ident = cp_parser_objc_selector (parser);
24388 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24389 cp_parser_error (parser, "setter name must terminate with %<:%>");
24391 cp_lexer_consume_token (parser->lexer);
24395 if (property_getter_ident != NULL_TREE)
24397 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24398 cp_lexer_consume_token (parser->lexer);
24401 property_getter_ident = cp_parser_objc_selector (parser);
24405 cp_parser_error (parser, "unknown property attribute");
24406 syntax_error = true;
24413 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24414 cp_lexer_consume_token (parser->lexer);
24419 /* FIXME: "@property (setter, assign);" will generate a spurious
24420 "error: expected ‘)’ before ‘,’ token". This is because
24421 cp_parser_require, unlike the C counterpart, will produce an
24422 error even if we are in error recovery. */
24423 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24425 cp_parser_skip_to_closing_parenthesis (parser,
24426 /*recovering=*/true,
24427 /*or_comma=*/false,
24428 /*consume_paren=*/true);
24432 /* ... and the property declaration(s). */
24433 properties = cp_parser_objc_struct_declaration (parser);
24435 if (properties == error_mark_node)
24437 cp_parser_skip_to_end_of_statement (parser);
24438 /* If the next token is now a `;', consume it. */
24439 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24440 cp_lexer_consume_token (parser->lexer);
24444 if (properties == NULL_TREE)
24445 cp_parser_error (parser, "expected identifier");
24448 /* Comma-separated properties are chained together in
24449 reverse order; add them one by one. */
24450 properties = nreverse (properties);
24452 for (; properties; properties = TREE_CHAIN (properties))
24453 objc_add_property_declaration (loc, copy_node (properties),
24454 property_readonly, property_readwrite,
24455 property_assign, property_retain,
24456 property_copy, property_nonatomic,
24457 property_getter_ident, property_setter_ident);
24460 cp_parser_consume_semicolon_at_end_of_statement (parser);
24463 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
24465 objc-synthesize-declaration:
24466 @synthesize objc-synthesize-identifier-list ;
24468 objc-synthesize-identifier-list:
24469 objc-synthesize-identifier
24470 objc-synthesize-identifier-list, objc-synthesize-identifier
24472 objc-synthesize-identifier
24474 identifier = identifier
24477 @synthesize MyProperty;
24478 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24480 PS: This function is identical to c_parser_objc_at_synthesize_declaration
24481 for C. Keep them in sync.
24484 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24486 tree list = NULL_TREE;
24488 loc = cp_lexer_peek_token (parser->lexer)->location;
24490 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
24493 tree property, ivar;
24494 property = cp_parser_identifier (parser);
24495 if (property == error_mark_node)
24497 cp_parser_consume_semicolon_at_end_of_statement (parser);
24500 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24502 cp_lexer_consume_token (parser->lexer);
24503 ivar = cp_parser_identifier (parser);
24504 if (ivar == error_mark_node)
24506 cp_parser_consume_semicolon_at_end_of_statement (parser);
24512 list = chainon (list, build_tree_list (ivar, property));
24513 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24514 cp_lexer_consume_token (parser->lexer);
24518 cp_parser_consume_semicolon_at_end_of_statement (parser);
24519 objc_add_synthesize_declaration (loc, list);
24522 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
24524 objc-dynamic-declaration:
24525 @dynamic identifier-list ;
24528 @dynamic MyProperty;
24529 @dynamic MyProperty, AnotherProperty;
24531 PS: This function is identical to c_parser_objc_at_dynamic_declaration
24532 for C. Keep them in sync.
24535 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24537 tree list = NULL_TREE;
24539 loc = cp_lexer_peek_token (parser->lexer)->location;
24541 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
24545 property = cp_parser_identifier (parser);
24546 if (property == error_mark_node)
24548 cp_parser_consume_semicolon_at_end_of_statement (parser);
24551 list = chainon (list, build_tree_list (NULL, property));
24552 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24553 cp_lexer_consume_token (parser->lexer);
24557 cp_parser_consume_semicolon_at_end_of_statement (parser);
24558 objc_add_dynamic_declaration (loc, list);
24562 /* OpenMP 2.5 parsing routines. */
24564 /* Returns name of the next clause.
24565 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24566 the token is not consumed. Otherwise appropriate pragma_omp_clause is
24567 returned and the token is consumed. */
24569 static pragma_omp_clause
24570 cp_parser_omp_clause_name (cp_parser *parser)
24572 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24574 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24575 result = PRAGMA_OMP_CLAUSE_IF;
24576 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24577 result = PRAGMA_OMP_CLAUSE_DEFAULT;
24578 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24579 result = PRAGMA_OMP_CLAUSE_PRIVATE;
24580 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24582 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24583 const char *p = IDENTIFIER_POINTER (id);
24588 if (!strcmp ("collapse", p))
24589 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24590 else if (!strcmp ("copyin", p))
24591 result = PRAGMA_OMP_CLAUSE_COPYIN;
24592 else if (!strcmp ("copyprivate", p))
24593 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24596 if (!strcmp ("final", p))
24597 result = PRAGMA_OMP_CLAUSE_FINAL;
24598 else if (!strcmp ("firstprivate", p))
24599 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24602 if (!strcmp ("lastprivate", p))
24603 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24606 if (!strcmp ("mergeable", p))
24607 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24610 if (!strcmp ("nowait", p))
24611 result = PRAGMA_OMP_CLAUSE_NOWAIT;
24612 else if (!strcmp ("num_threads", p))
24613 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24616 if (!strcmp ("ordered", p))
24617 result = PRAGMA_OMP_CLAUSE_ORDERED;
24620 if (!strcmp ("reduction", p))
24621 result = PRAGMA_OMP_CLAUSE_REDUCTION;
24624 if (!strcmp ("schedule", p))
24625 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24626 else if (!strcmp ("shared", p))
24627 result = PRAGMA_OMP_CLAUSE_SHARED;
24630 if (!strcmp ("untied", p))
24631 result = PRAGMA_OMP_CLAUSE_UNTIED;
24636 if (result != PRAGMA_OMP_CLAUSE_NONE)
24637 cp_lexer_consume_token (parser->lexer);
24642 /* Validate that a clause of the given type does not already exist. */
24645 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24646 const char *name, location_t location)
24650 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24651 if (OMP_CLAUSE_CODE (c) == code)
24653 error_at (location, "too many %qs clauses", name);
24661 variable-list , identifier
24663 In addition, we match a closing parenthesis. An opening parenthesis
24664 will have been consumed by the caller.
24666 If KIND is nonzero, create the appropriate node and install the decl
24667 in OMP_CLAUSE_DECL and add the node to the head of the list.
24669 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24670 return the list created. */
24673 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24681 token = cp_lexer_peek_token (parser->lexer);
24682 name = cp_parser_id_expression (parser, /*template_p=*/false,
24683 /*check_dependency_p=*/true,
24684 /*template_p=*/NULL,
24685 /*declarator_p=*/false,
24686 /*optional_p=*/false);
24687 if (name == error_mark_node)
24690 decl = cp_parser_lookup_name_simple (parser, name, token->location);
24691 if (decl == error_mark_node)
24692 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24694 else if (kind != 0)
24696 tree u = build_omp_clause (token->location, kind);
24697 OMP_CLAUSE_DECL (u) = decl;
24698 OMP_CLAUSE_CHAIN (u) = list;
24702 list = tree_cons (decl, NULL_TREE, list);
24705 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24707 cp_lexer_consume_token (parser->lexer);
24710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24714 /* Try to resync to an unnested comma. Copied from
24715 cp_parser_parenthesized_expression_list. */
24717 ending = cp_parser_skip_to_closing_parenthesis (parser,
24718 /*recovering=*/true,
24720 /*consume_paren=*/true);
24728 /* Similarly, but expect leading and trailing parenthesis. This is a very
24729 common case for omp clauses. */
24732 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24734 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24735 return cp_parser_omp_var_list_no_open (parser, kind, list);
24740 collapse ( constant-expression ) */
24743 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24749 loc = cp_lexer_peek_token (parser->lexer)->location;
24750 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24753 num = cp_parser_constant_expression (parser, false, NULL);
24755 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24756 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24757 /*or_comma=*/false,
24758 /*consume_paren=*/true);
24760 if (num == error_mark_node)
24762 num = fold_non_dependent_expr (num);
24763 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24764 || !host_integerp (num, 0)
24765 || (n = tree_low_cst (num, 0)) <= 0
24768 error_at (loc, "collapse argument needs positive constant integer expression");
24772 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24773 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24774 OMP_CLAUSE_CHAIN (c) = list;
24775 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24781 default ( shared | none ) */
24784 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24786 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24789 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24791 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24793 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24794 const char *p = IDENTIFIER_POINTER (id);
24799 if (strcmp ("none", p) != 0)
24801 kind = OMP_CLAUSE_DEFAULT_NONE;
24805 if (strcmp ("shared", p) != 0)
24807 kind = OMP_CLAUSE_DEFAULT_SHARED;
24814 cp_lexer_consume_token (parser->lexer);
24819 cp_parser_error (parser, "expected %<none%> or %<shared%>");
24822 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24823 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24824 /*or_comma=*/false,
24825 /*consume_paren=*/true);
24827 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24830 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24831 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24832 OMP_CLAUSE_CHAIN (c) = list;
24833 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24839 final ( expression ) */
24842 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24846 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24849 t = cp_parser_condition (parser);
24851 if (t == error_mark_node
24852 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24853 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24854 /*or_comma=*/false,
24855 /*consume_paren=*/true);
24857 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24859 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24860 OMP_CLAUSE_FINAL_EXPR (c) = t;
24861 OMP_CLAUSE_CHAIN (c) = list;
24867 if ( expression ) */
24870 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24874 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24877 t = cp_parser_condition (parser);
24879 if (t == error_mark_node
24880 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24881 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24882 /*or_comma=*/false,
24883 /*consume_paren=*/true);
24885 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24887 c = build_omp_clause (location, OMP_CLAUSE_IF);
24888 OMP_CLAUSE_IF_EXPR (c) = t;
24889 OMP_CLAUSE_CHAIN (c) = list;
24898 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24899 tree list, location_t location)
24903 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24906 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24907 OMP_CLAUSE_CHAIN (c) = list;
24915 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24916 tree list, location_t location)
24920 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24922 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24923 OMP_CLAUSE_CHAIN (c) = list;
24928 num_threads ( expression ) */
24931 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24932 location_t location)
24936 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24939 t = cp_parser_expression (parser, false, NULL);
24941 if (t == error_mark_node
24942 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24943 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24944 /*or_comma=*/false,
24945 /*consume_paren=*/true);
24947 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24948 "num_threads", location);
24950 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24951 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24952 OMP_CLAUSE_CHAIN (c) = list;
24961 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24962 tree list, location_t location)
24966 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24967 "ordered", location);
24969 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24970 OMP_CLAUSE_CHAIN (c) = list;
24975 reduction ( reduction-operator : variable-list )
24977 reduction-operator:
24978 One of: + * - & ^ | && ||
24982 reduction-operator:
24983 One of: + * - & ^ | && || min max */
24986 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24988 enum tree_code code;
24991 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24994 switch (cp_lexer_peek_token (parser->lexer)->type)
25006 code = BIT_AND_EXPR;
25009 code = BIT_XOR_EXPR;
25012 code = BIT_IOR_EXPR;
25015 code = TRUTH_ANDIF_EXPR;
25018 code = TRUTH_ORIF_EXPR;
25022 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25023 const char *p = IDENTIFIER_POINTER (id);
25025 if (strcmp (p, "min") == 0)
25030 if (strcmp (p, "max") == 0)
25038 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25039 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25041 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25042 /*or_comma=*/false,
25043 /*consume_paren=*/true);
25046 cp_lexer_consume_token (parser->lexer);
25048 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25051 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25052 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25053 OMP_CLAUSE_REDUCTION_CODE (c) = code;
25059 schedule ( schedule-kind )
25060 schedule ( schedule-kind , expression )
25063 static | dynamic | guided | runtime | auto */
25066 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25070 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25073 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25075 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25077 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25078 const char *p = IDENTIFIER_POINTER (id);
25083 if (strcmp ("dynamic", p) != 0)
25085 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25089 if (strcmp ("guided", p) != 0)
25091 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25095 if (strcmp ("runtime", p) != 0)
25097 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25104 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25105 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25106 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25107 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25110 cp_lexer_consume_token (parser->lexer);
25112 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25115 cp_lexer_consume_token (parser->lexer);
25117 token = cp_lexer_peek_token (parser->lexer);
25118 t = cp_parser_assignment_expression (parser, false, NULL);
25120 if (t == error_mark_node)
25122 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25123 error_at (token->location, "schedule %<runtime%> does not take "
25124 "a %<chunk_size%> parameter");
25125 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25126 error_at (token->location, "schedule %<auto%> does not take "
25127 "a %<chunk_size%> parameter");
25129 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25131 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25134 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25137 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25138 OMP_CLAUSE_CHAIN (c) = list;
25142 cp_parser_error (parser, "invalid schedule kind");
25144 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25145 /*or_comma=*/false,
25146 /*consume_paren=*/true);
25154 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25155 tree list, location_t location)
25159 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25161 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25162 OMP_CLAUSE_CHAIN (c) = list;
25166 /* Parse all OpenMP clauses. The set clauses allowed by the directive
25167 is a bitmask in MASK. Return the list of clauses found; the result
25168 of clause default goes in *pdefault. */
25171 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25172 const char *where, cp_token *pragma_tok)
25174 tree clauses = NULL;
25176 cp_token *token = NULL;
25178 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25180 pragma_omp_clause c_kind;
25181 const char *c_name;
25182 tree prev = clauses;
25184 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25185 cp_lexer_consume_token (parser->lexer);
25187 token = cp_lexer_peek_token (parser->lexer);
25188 c_kind = cp_parser_omp_clause_name (parser);
25193 case PRAGMA_OMP_CLAUSE_COLLAPSE:
25194 clauses = cp_parser_omp_clause_collapse (parser, clauses,
25196 c_name = "collapse";
25198 case PRAGMA_OMP_CLAUSE_COPYIN:
25199 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25202 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25203 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25205 c_name = "copyprivate";
25207 case PRAGMA_OMP_CLAUSE_DEFAULT:
25208 clauses = cp_parser_omp_clause_default (parser, clauses,
25210 c_name = "default";
25212 case PRAGMA_OMP_CLAUSE_FINAL:
25213 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25216 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25217 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25219 c_name = "firstprivate";
25221 case PRAGMA_OMP_CLAUSE_IF:
25222 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25225 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25226 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25228 c_name = "lastprivate";
25230 case PRAGMA_OMP_CLAUSE_MERGEABLE:
25231 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25233 c_name = "mergeable";
25235 case PRAGMA_OMP_CLAUSE_NOWAIT:
25236 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25239 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25240 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25242 c_name = "num_threads";
25244 case PRAGMA_OMP_CLAUSE_ORDERED:
25245 clauses = cp_parser_omp_clause_ordered (parser, clauses,
25247 c_name = "ordered";
25249 case PRAGMA_OMP_CLAUSE_PRIVATE:
25250 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25252 c_name = "private";
25254 case PRAGMA_OMP_CLAUSE_REDUCTION:
25255 clauses = cp_parser_omp_clause_reduction (parser, clauses);
25256 c_name = "reduction";
25258 case PRAGMA_OMP_CLAUSE_SCHEDULE:
25259 clauses = cp_parser_omp_clause_schedule (parser, clauses,
25261 c_name = "schedule";
25263 case PRAGMA_OMP_CLAUSE_SHARED:
25264 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25268 case PRAGMA_OMP_CLAUSE_UNTIED:
25269 clauses = cp_parser_omp_clause_untied (parser, clauses,
25274 cp_parser_error (parser, "expected %<#pragma omp%> clause");
25278 if (((mask >> c_kind) & 1) == 0)
25280 /* Remove the invalid clause(s) from the list to avoid
25281 confusing the rest of the compiler. */
25283 error_at (token->location, "%qs is not valid for %qs", c_name, where);
25287 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25288 return finish_omp_clauses (clauses);
25295 In practice, we're also interested in adding the statement to an
25296 outer node. So it is convenient if we work around the fact that
25297 cp_parser_statement calls add_stmt. */
25300 cp_parser_begin_omp_structured_block (cp_parser *parser)
25302 unsigned save = parser->in_statement;
25304 /* Only move the values to IN_OMP_BLOCK if they weren't false.
25305 This preserves the "not within loop or switch" style error messages
25306 for nonsense cases like
25312 if (parser->in_statement)
25313 parser->in_statement = IN_OMP_BLOCK;
25319 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25321 parser->in_statement = save;
25325 cp_parser_omp_structured_block (cp_parser *parser)
25327 tree stmt = begin_omp_structured_block ();
25328 unsigned int save = cp_parser_begin_omp_structured_block (parser);
25330 cp_parser_statement (parser, NULL_TREE, false, NULL);
25332 cp_parser_end_omp_structured_block (parser, save);
25333 return finish_omp_structured_block (stmt);
25337 # pragma omp atomic new-line
25341 x binop= expr | x++ | ++x | x-- | --x
25343 +, *, -, /, &, ^, |, <<, >>
25345 where x is an lvalue expression with scalar type.
25348 # pragma omp atomic new-line
25351 # pragma omp atomic read new-line
25354 # pragma omp atomic write new-line
25357 # pragma omp atomic update new-line
25360 # pragma omp atomic capture new-line
25363 # pragma omp atomic capture new-line
25371 expression-stmt | x = x binop expr
25373 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25375 { v = x; update-stmt; } | { update-stmt; v = x; }
25377 where x and v are lvalue expressions with scalar type. */
25380 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25382 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25383 tree rhs1 = NULL_TREE, orig_lhs;
25384 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25385 bool structured_block = false;
25387 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25389 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25390 const char *p = IDENTIFIER_POINTER (id);
25392 if (!strcmp (p, "read"))
25393 code = OMP_ATOMIC_READ;
25394 else if (!strcmp (p, "write"))
25396 else if (!strcmp (p, "update"))
25398 else if (!strcmp (p, "capture"))
25399 code = OMP_ATOMIC_CAPTURE_NEW;
25403 cp_lexer_consume_token (parser->lexer);
25405 cp_parser_require_pragma_eol (parser, pragma_tok);
25409 case OMP_ATOMIC_READ:
25410 case NOP_EXPR: /* atomic write */
25411 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25412 /*cast_p=*/false, NULL);
25413 if (v == error_mark_node)
25415 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25417 if (code == NOP_EXPR)
25418 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25420 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25421 /*cast_p=*/false, NULL);
25422 if (lhs == error_mark_node)
25424 if (code == NOP_EXPR)
25426 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25434 case OMP_ATOMIC_CAPTURE_NEW:
25435 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25437 cp_lexer_consume_token (parser->lexer);
25438 structured_block = true;
25442 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25443 /*cast_p=*/false, NULL);
25444 if (v == error_mark_node)
25446 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25454 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25455 /*cast_p=*/false, NULL);
25457 switch (TREE_CODE (lhs))
25462 case POSTINCREMENT_EXPR:
25463 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25464 code = OMP_ATOMIC_CAPTURE_OLD;
25466 case PREINCREMENT_EXPR:
25467 lhs = TREE_OPERAND (lhs, 0);
25468 opcode = PLUS_EXPR;
25469 rhs = integer_one_node;
25472 case POSTDECREMENT_EXPR:
25473 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25474 code = OMP_ATOMIC_CAPTURE_OLD;
25476 case PREDECREMENT_EXPR:
25477 lhs = TREE_OPERAND (lhs, 0);
25478 opcode = MINUS_EXPR;
25479 rhs = integer_one_node;
25482 case COMPOUND_EXPR:
25483 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25484 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25485 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25486 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25487 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25488 (TREE_OPERAND (lhs, 1), 0), 0)))
25490 /* Undo effects of boolean_increment for post {in,de}crement. */
25491 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25494 if (TREE_CODE (lhs) == MODIFY_EXPR
25495 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25497 /* Undo effects of boolean_increment. */
25498 if (integer_onep (TREE_OPERAND (lhs, 1)))
25500 /* This is pre or post increment. */
25501 rhs = TREE_OPERAND (lhs, 1);
25502 lhs = TREE_OPERAND (lhs, 0);
25504 if (code == OMP_ATOMIC_CAPTURE_NEW
25505 && !structured_block
25506 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25507 code = OMP_ATOMIC_CAPTURE_OLD;
25513 switch (cp_lexer_peek_token (parser->lexer)->type)
25516 opcode = MULT_EXPR;
25519 opcode = TRUNC_DIV_EXPR;
25522 opcode = PLUS_EXPR;
25525 opcode = MINUS_EXPR;
25527 case CPP_LSHIFT_EQ:
25528 opcode = LSHIFT_EXPR;
25530 case CPP_RSHIFT_EQ:
25531 opcode = RSHIFT_EXPR;
25534 opcode = BIT_AND_EXPR;
25537 opcode = BIT_IOR_EXPR;
25540 opcode = BIT_XOR_EXPR;
25543 if (structured_block || code == OMP_ATOMIC)
25545 enum cp_parser_prec oprec;
25547 cp_lexer_consume_token (parser->lexer);
25548 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25549 /*cast_p=*/false, NULL);
25550 if (rhs1 == error_mark_node)
25552 token = cp_lexer_peek_token (parser->lexer);
25553 switch (token->type)
25555 case CPP_SEMICOLON:
25556 if (code == OMP_ATOMIC_CAPTURE_NEW)
25558 code = OMP_ATOMIC_CAPTURE_OLD;
25563 cp_lexer_consume_token (parser->lexer);
25566 cp_parser_error (parser,
25567 "invalid form of %<#pragma omp atomic%>");
25570 opcode = MULT_EXPR;
25573 opcode = TRUNC_DIV_EXPR;
25576 opcode = PLUS_EXPR;
25579 opcode = MINUS_EXPR;
25582 opcode = LSHIFT_EXPR;
25585 opcode = RSHIFT_EXPR;
25588 opcode = BIT_AND_EXPR;
25591 opcode = BIT_IOR_EXPR;
25594 opcode = BIT_XOR_EXPR;
25597 cp_parser_error (parser,
25598 "invalid operator for %<#pragma omp atomic%>");
25601 oprec = TOKEN_PRECEDENCE (token);
25602 gcc_assert (oprec != PREC_NOT_OPERATOR);
25603 if (commutative_tree_code (opcode))
25604 oprec = (enum cp_parser_prec) (oprec - 1);
25605 cp_lexer_consume_token (parser->lexer);
25606 rhs = cp_parser_binary_expression (parser, false, false,
25608 if (rhs == error_mark_node)
25614 cp_parser_error (parser,
25615 "invalid operator for %<#pragma omp atomic%>");
25618 cp_lexer_consume_token (parser->lexer);
25620 rhs = cp_parser_expression (parser, false, NULL);
25621 if (rhs == error_mark_node)
25626 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25628 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25630 v = cp_parser_unary_expression (parser, /*address_p=*/false,
25631 /*cast_p=*/false, NULL);
25632 if (v == error_mark_node)
25634 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25636 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25637 /*cast_p=*/false, NULL);
25638 if (lhs1 == error_mark_node)
25641 if (structured_block)
25643 cp_parser_consume_semicolon_at_end_of_statement (parser);
25644 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25647 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25648 if (!structured_block)
25649 cp_parser_consume_semicolon_at_end_of_statement (parser);
25653 cp_parser_skip_to_end_of_block_or_statement (parser);
25654 if (structured_block)
25656 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25657 cp_lexer_consume_token (parser->lexer);
25658 else if (code == OMP_ATOMIC_CAPTURE_NEW)
25660 cp_parser_skip_to_end_of_block_or_statement (parser);
25661 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25662 cp_lexer_consume_token (parser->lexer);
25669 # pragma omp barrier new-line */
25672 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25674 cp_parser_require_pragma_eol (parser, pragma_tok);
25675 finish_omp_barrier ();
25679 # pragma omp critical [(name)] new-line
25680 structured-block */
25683 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25685 tree stmt, name = NULL;
25687 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25689 cp_lexer_consume_token (parser->lexer);
25691 name = cp_parser_identifier (parser);
25693 if (name == error_mark_node
25694 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25695 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25696 /*or_comma=*/false,
25697 /*consume_paren=*/true);
25698 if (name == error_mark_node)
25701 cp_parser_require_pragma_eol (parser, pragma_tok);
25703 stmt = cp_parser_omp_structured_block (parser);
25704 return c_finish_omp_critical (input_location, stmt, name);
25708 # pragma omp flush flush-vars[opt] new-line
25711 ( variable-list ) */
25714 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25716 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25717 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25718 cp_parser_require_pragma_eol (parser, pragma_tok);
25720 finish_omp_flush ();
25723 /* Helper function, to parse omp for increment expression. */
25726 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25728 tree cond = cp_parser_binary_expression (parser, false, true,
25729 PREC_NOT_OPERATOR, NULL);
25730 if (cond == error_mark_node
25731 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25733 cp_parser_skip_to_end_of_statement (parser);
25734 return error_mark_node;
25737 switch (TREE_CODE (cond))
25745 return error_mark_node;
25748 /* If decl is an iterator, preserve LHS and RHS of the relational
25749 expr until finish_omp_for. */
25751 && (type_dependent_expression_p (decl)
25752 || CLASS_TYPE_P (TREE_TYPE (decl))))
25755 return build_x_binary_op (TREE_CODE (cond),
25756 TREE_OPERAND (cond, 0), ERROR_MARK,
25757 TREE_OPERAND (cond, 1), ERROR_MARK,
25758 /*overload=*/NULL, tf_warning_or_error);
25761 /* Helper function, to parse omp for increment expression. */
25764 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25766 cp_token *token = cp_lexer_peek_token (parser->lexer);
25772 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25774 op = (token->type == CPP_PLUS_PLUS
25775 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25776 cp_lexer_consume_token (parser->lexer);
25777 lhs = cp_parser_cast_expression (parser, false, false, NULL);
25779 return error_mark_node;
25780 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25783 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25785 return error_mark_node;
25787 token = cp_lexer_peek_token (parser->lexer);
25788 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25790 op = (token->type == CPP_PLUS_PLUS
25791 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25792 cp_lexer_consume_token (parser->lexer);
25793 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25796 op = cp_parser_assignment_operator_opt (parser);
25797 if (op == ERROR_MARK)
25798 return error_mark_node;
25800 if (op != NOP_EXPR)
25802 rhs = cp_parser_assignment_expression (parser, false, NULL);
25803 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25804 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25807 lhs = cp_parser_binary_expression (parser, false, false,
25808 PREC_ADDITIVE_EXPRESSION, NULL);
25809 token = cp_lexer_peek_token (parser->lexer);
25810 decl_first = lhs == decl;
25813 if (token->type != CPP_PLUS
25814 && token->type != CPP_MINUS)
25815 return error_mark_node;
25819 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25820 cp_lexer_consume_token (parser->lexer);
25821 rhs = cp_parser_binary_expression (parser, false, false,
25822 PREC_ADDITIVE_EXPRESSION, NULL);
25823 token = cp_lexer_peek_token (parser->lexer);
25824 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25826 if (lhs == NULL_TREE)
25828 if (op == PLUS_EXPR)
25831 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25834 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25835 NULL, tf_warning_or_error);
25838 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25842 if (rhs != decl || op == MINUS_EXPR)
25843 return error_mark_node;
25844 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25847 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25849 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25852 /* Parse the restricted form of the for statement allowed by OpenMP. */
25855 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25857 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25858 tree real_decl, initv, condv, incrv, declv;
25859 tree this_pre_body, cl;
25860 location_t loc_first;
25861 bool collapse_err = false;
25862 int i, collapse = 1, nbraces = 0;
25863 VEC(tree,gc) *for_block = make_tree_vector ();
25865 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25866 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25867 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25869 gcc_assert (collapse >= 1);
25871 declv = make_tree_vec (collapse);
25872 initv = make_tree_vec (collapse);
25873 condv = make_tree_vec (collapse);
25874 incrv = make_tree_vec (collapse);
25876 loc_first = cp_lexer_peek_token (parser->lexer)->location;
25878 for (i = 0; i < collapse; i++)
25880 int bracecount = 0;
25881 bool add_private_clause = false;
25884 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25886 cp_parser_error (parser, "for statement expected");
25889 loc = cp_lexer_consume_token (parser->lexer)->location;
25891 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25894 init = decl = real_decl = NULL;
25895 this_pre_body = push_stmt_list ();
25896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25898 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25902 integer-type var = lb
25903 random-access-iterator-type var = lb
25904 pointer-type var = lb
25906 cp_decl_specifier_seq type_specifiers;
25908 /* First, try to parse as an initialized declaration. See
25909 cp_parser_condition, from whence the bulk of this is copied. */
25911 cp_parser_parse_tentatively (parser);
25912 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25913 /*is_trailing_return=*/false,
25915 if (cp_parser_parse_definitely (parser))
25917 /* If parsing a type specifier seq succeeded, then this
25918 MUST be a initialized declaration. */
25919 tree asm_specification, attributes;
25920 cp_declarator *declarator;
25922 declarator = cp_parser_declarator (parser,
25923 CP_PARSER_DECLARATOR_NAMED,
25924 /*ctor_dtor_or_conv_p=*/NULL,
25925 /*parenthesized_p=*/NULL,
25926 /*member_p=*/false);
25927 attributes = cp_parser_attributes_opt (parser);
25928 asm_specification = cp_parser_asm_specification_opt (parser);
25930 if (declarator == cp_error_declarator)
25931 cp_parser_skip_to_end_of_statement (parser);
25935 tree pushed_scope, auto_node;
25937 decl = start_decl (declarator, &type_specifiers,
25938 SD_INITIALIZED, attributes,
25939 /*prefix_attributes=*/NULL_TREE,
25942 auto_node = type_uses_auto (TREE_TYPE (decl));
25943 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25945 if (cp_lexer_next_token_is (parser->lexer,
25947 error ("parenthesized initialization is not allowed in "
25948 "OpenMP %<for%> loop");
25950 /* Trigger an error. */
25951 cp_parser_require (parser, CPP_EQ, RT_EQ);
25953 init = error_mark_node;
25954 cp_parser_skip_to_end_of_statement (parser);
25956 else if (CLASS_TYPE_P (TREE_TYPE (decl))
25957 || type_dependent_expression_p (decl)
25960 bool is_direct_init, is_non_constant_init;
25962 init = cp_parser_initializer (parser,
25964 &is_non_constant_init);
25969 = do_auto_deduction (TREE_TYPE (decl), init,
25972 if (!CLASS_TYPE_P (TREE_TYPE (decl))
25973 && !type_dependent_expression_p (decl))
25977 cp_finish_decl (decl, init, !is_non_constant_init,
25979 LOOKUP_ONLYCONVERTING);
25980 if (CLASS_TYPE_P (TREE_TYPE (decl)))
25982 VEC_safe_push (tree, gc, for_block, this_pre_body);
25986 init = pop_stmt_list (this_pre_body);
25987 this_pre_body = NULL_TREE;
25992 cp_lexer_consume_token (parser->lexer);
25993 init = cp_parser_assignment_expression (parser, false, NULL);
25996 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25997 init = error_mark_node;
25999 cp_finish_decl (decl, NULL_TREE,
26000 /*init_const_expr_p=*/false,
26002 LOOKUP_ONLYCONVERTING);
26006 pop_scope (pushed_scope);
26012 /* If parsing a type specifier sequence failed, then
26013 this MUST be a simple expression. */
26014 cp_parser_parse_tentatively (parser);
26015 decl = cp_parser_primary_expression (parser, false, false,
26017 if (!cp_parser_error_occurred (parser)
26020 && CLASS_TYPE_P (TREE_TYPE (decl)))
26024 cp_parser_parse_definitely (parser);
26025 cp_parser_require (parser, CPP_EQ, RT_EQ);
26026 rhs = cp_parser_assignment_expression (parser, false, NULL);
26027 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26029 tf_warning_or_error));
26030 add_private_clause = true;
26035 cp_parser_abort_tentative_parse (parser);
26036 init = cp_parser_expression (parser, false, NULL);
26039 if (TREE_CODE (init) == MODIFY_EXPR
26040 || TREE_CODE (init) == MODOP_EXPR)
26041 real_decl = TREE_OPERAND (init, 0);
26046 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26049 this_pre_body = pop_stmt_list (this_pre_body);
26053 pre_body = push_stmt_list ();
26055 add_stmt (this_pre_body);
26056 pre_body = pop_stmt_list (pre_body);
26059 pre_body = this_pre_body;
26064 if (par_clauses != NULL && real_decl != NULL_TREE)
26067 for (c = par_clauses; *c ; )
26068 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26069 && OMP_CLAUSE_DECL (*c) == real_decl)
26071 error_at (loc, "iteration variable %qD"
26072 " should not be firstprivate", real_decl);
26073 *c = OMP_CLAUSE_CHAIN (*c);
26075 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26076 && OMP_CLAUSE_DECL (*c) == real_decl)
26078 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26079 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
26080 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26081 OMP_CLAUSE_DECL (l) = real_decl;
26082 OMP_CLAUSE_CHAIN (l) = clauses;
26083 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26085 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26086 CP_OMP_CLAUSE_INFO (*c) = NULL;
26087 add_private_clause = false;
26091 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26092 && OMP_CLAUSE_DECL (*c) == real_decl)
26093 add_private_clause = false;
26094 c = &OMP_CLAUSE_CHAIN (*c);
26098 if (add_private_clause)
26101 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26103 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26104 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26105 && OMP_CLAUSE_DECL (c) == decl)
26107 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26108 && OMP_CLAUSE_DECL (c) == decl)
26109 error_at (loc, "iteration variable %qD "
26110 "should not be firstprivate",
26112 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26113 && OMP_CLAUSE_DECL (c) == decl)
26114 error_at (loc, "iteration variable %qD should not be reduction",
26119 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26120 OMP_CLAUSE_DECL (c) = decl;
26121 c = finish_omp_clauses (c);
26124 OMP_CLAUSE_CHAIN (c) = clauses;
26131 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26132 cond = cp_parser_omp_for_cond (parser, decl);
26133 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26136 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26138 /* If decl is an iterator, preserve the operator on decl
26139 until finish_omp_for. */
26141 && ((type_dependent_expression_p (decl)
26142 && !POINTER_TYPE_P (TREE_TYPE (decl)))
26143 || CLASS_TYPE_P (TREE_TYPE (decl))))
26144 incr = cp_parser_omp_for_incr (parser, decl);
26146 incr = cp_parser_expression (parser, false, NULL);
26149 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26150 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26151 /*or_comma=*/false,
26152 /*consume_paren=*/true);
26154 TREE_VEC_ELT (declv, i) = decl;
26155 TREE_VEC_ELT (initv, i) = init;
26156 TREE_VEC_ELT (condv, i) = cond;
26157 TREE_VEC_ELT (incrv, i) = incr;
26159 if (i == collapse - 1)
26162 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26163 in between the collapsed for loops to be still considered perfectly
26164 nested. Hopefully the final version clarifies this.
26165 For now handle (multiple) {'s and empty statements. */
26166 cp_parser_parse_tentatively (parser);
26169 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26171 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26173 cp_lexer_consume_token (parser->lexer);
26176 else if (bracecount
26177 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26178 cp_lexer_consume_token (parser->lexer);
26181 loc = cp_lexer_peek_token (parser->lexer)->location;
26182 error_at (loc, "not enough collapsed for loops");
26183 collapse_err = true;
26184 cp_parser_abort_tentative_parse (parser);
26193 cp_parser_parse_definitely (parser);
26194 nbraces += bracecount;
26198 /* Note that we saved the original contents of this flag when we entered
26199 the structured block, and so we don't need to re-save it here. */
26200 parser->in_statement = IN_OMP_FOR;
26202 /* Note that the grammar doesn't call for a structured block here,
26203 though the loop as a whole is a structured block. */
26204 body = push_stmt_list ();
26205 cp_parser_statement (parser, NULL_TREE, false, NULL);
26206 body = pop_stmt_list (body);
26208 if (declv == NULL_TREE)
26211 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26212 pre_body, clauses);
26216 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26218 cp_lexer_consume_token (parser->lexer);
26221 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26222 cp_lexer_consume_token (parser->lexer);
26227 error_at (cp_lexer_peek_token (parser->lexer)->location,
26228 "collapsed loops not perfectly nested");
26230 collapse_err = true;
26231 cp_parser_statement_seq_opt (parser, NULL);
26232 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26237 while (!VEC_empty (tree, for_block))
26238 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26239 release_tree_vector (for_block);
26245 #pragma omp for for-clause[optseq] new-line
26248 #define OMP_FOR_CLAUSE_MASK \
26249 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26250 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26251 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26252 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26253 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
26254 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
26255 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
26256 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26259 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26261 tree clauses, sb, ret;
26264 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26265 "#pragma omp for", pragma_tok);
26267 sb = begin_omp_structured_block ();
26268 save = cp_parser_begin_omp_structured_block (parser);
26270 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26272 cp_parser_end_omp_structured_block (parser, save);
26273 add_stmt (finish_omp_structured_block (sb));
26279 # pragma omp master new-line
26280 structured-block */
26283 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26285 cp_parser_require_pragma_eol (parser, pragma_tok);
26286 return c_finish_omp_master (input_location,
26287 cp_parser_omp_structured_block (parser));
26291 # pragma omp ordered new-line
26292 structured-block */
26295 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26297 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26298 cp_parser_require_pragma_eol (parser, pragma_tok);
26299 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26305 { section-sequence }
26308 section-directive[opt] structured-block
26309 section-sequence section-directive structured-block */
26312 cp_parser_omp_sections_scope (cp_parser *parser)
26314 tree stmt, substmt;
26315 bool error_suppress = false;
26318 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26321 stmt = push_stmt_list ();
26323 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26327 substmt = begin_omp_structured_block ();
26328 save = cp_parser_begin_omp_structured_block (parser);
26332 cp_parser_statement (parser, NULL_TREE, false, NULL);
26334 tok = cp_lexer_peek_token (parser->lexer);
26335 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26337 if (tok->type == CPP_CLOSE_BRACE)
26339 if (tok->type == CPP_EOF)
26343 cp_parser_end_omp_structured_block (parser, save);
26344 substmt = finish_omp_structured_block (substmt);
26345 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26346 add_stmt (substmt);
26351 tok = cp_lexer_peek_token (parser->lexer);
26352 if (tok->type == CPP_CLOSE_BRACE)
26354 if (tok->type == CPP_EOF)
26357 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26359 cp_lexer_consume_token (parser->lexer);
26360 cp_parser_require_pragma_eol (parser, tok);
26361 error_suppress = false;
26363 else if (!error_suppress)
26365 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26366 error_suppress = true;
26369 substmt = cp_parser_omp_structured_block (parser);
26370 substmt = build1 (OMP_SECTION, void_type_node, substmt);
26371 add_stmt (substmt);
26373 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26375 substmt = pop_stmt_list (stmt);
26377 stmt = make_node (OMP_SECTIONS);
26378 TREE_TYPE (stmt) = void_type_node;
26379 OMP_SECTIONS_BODY (stmt) = substmt;
26386 # pragma omp sections sections-clause[optseq] newline
26389 #define OMP_SECTIONS_CLAUSE_MASK \
26390 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26391 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26392 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
26393 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26394 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26397 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26401 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26402 "#pragma omp sections", pragma_tok);
26404 ret = cp_parser_omp_sections_scope (parser);
26406 OMP_SECTIONS_CLAUSES (ret) = clauses;
26412 # pragma parallel parallel-clause new-line
26413 # pragma parallel for parallel-for-clause new-line
26414 # pragma parallel sections parallel-sections-clause new-line */
26416 #define OMP_PARALLEL_CLAUSE_MASK \
26417 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26418 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26419 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26420 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26421 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26422 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
26423 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
26424 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26427 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26429 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26430 const char *p_name = "#pragma omp parallel";
26431 tree stmt, clauses, par_clause, ws_clause, block;
26432 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26434 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26436 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26438 cp_lexer_consume_token (parser->lexer);
26439 p_kind = PRAGMA_OMP_PARALLEL_FOR;
26440 p_name = "#pragma omp parallel for";
26441 mask |= OMP_FOR_CLAUSE_MASK;
26442 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26444 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26446 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26447 const char *p = IDENTIFIER_POINTER (id);
26448 if (strcmp (p, "sections") == 0)
26450 cp_lexer_consume_token (parser->lexer);
26451 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26452 p_name = "#pragma omp parallel sections";
26453 mask |= OMP_SECTIONS_CLAUSE_MASK;
26454 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26458 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26459 block = begin_omp_parallel ();
26460 save = cp_parser_begin_omp_structured_block (parser);
26464 case PRAGMA_OMP_PARALLEL:
26465 cp_parser_statement (parser, NULL_TREE, false, NULL);
26466 par_clause = clauses;
26469 case PRAGMA_OMP_PARALLEL_FOR:
26470 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26471 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26474 case PRAGMA_OMP_PARALLEL_SECTIONS:
26475 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26476 stmt = cp_parser_omp_sections_scope (parser);
26478 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26482 gcc_unreachable ();
26485 cp_parser_end_omp_structured_block (parser, save);
26486 stmt = finish_omp_parallel (par_clause, block);
26487 if (p_kind != PRAGMA_OMP_PARALLEL)
26488 OMP_PARALLEL_COMBINED (stmt) = 1;
26493 # pragma omp single single-clause[optseq] new-line
26494 structured-block */
26496 #define OMP_SINGLE_CLAUSE_MASK \
26497 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26498 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26499 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
26500 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26503 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26505 tree stmt = make_node (OMP_SINGLE);
26506 TREE_TYPE (stmt) = void_type_node;
26508 OMP_SINGLE_CLAUSES (stmt)
26509 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26510 "#pragma omp single", pragma_tok);
26511 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26513 return add_stmt (stmt);
26517 # pragma omp task task-clause[optseq] new-line
26518 structured-block */
26520 #define OMP_TASK_CLAUSE_MASK \
26521 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
26522 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
26523 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
26524 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
26525 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
26526 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
26527 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
26528 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26531 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26533 tree clauses, block;
26536 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26537 "#pragma omp task", pragma_tok);
26538 block = begin_omp_task ();
26539 save = cp_parser_begin_omp_structured_block (parser);
26540 cp_parser_statement (parser, NULL_TREE, false, NULL);
26541 cp_parser_end_omp_structured_block (parser, save);
26542 return finish_omp_task (clauses, block);
26546 # pragma omp taskwait new-line */
26549 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26551 cp_parser_require_pragma_eol (parser, pragma_tok);
26552 finish_omp_taskwait ();
26556 # pragma omp taskyield new-line */
26559 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26561 cp_parser_require_pragma_eol (parser, pragma_tok);
26562 finish_omp_taskyield ();
26566 # pragma omp threadprivate (variable-list) */
26569 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26573 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26574 cp_parser_require_pragma_eol (parser, pragma_tok);
26576 finish_omp_threadprivate (vars);
26579 /* Main entry point to OpenMP statement pragmas. */
26582 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26586 switch (pragma_tok->pragma_kind)
26588 case PRAGMA_OMP_ATOMIC:
26589 cp_parser_omp_atomic (parser, pragma_tok);
26591 case PRAGMA_OMP_CRITICAL:
26592 stmt = cp_parser_omp_critical (parser, pragma_tok);
26594 case PRAGMA_OMP_FOR:
26595 stmt = cp_parser_omp_for (parser, pragma_tok);
26597 case PRAGMA_OMP_MASTER:
26598 stmt = cp_parser_omp_master (parser, pragma_tok);
26600 case PRAGMA_OMP_ORDERED:
26601 stmt = cp_parser_omp_ordered (parser, pragma_tok);
26603 case PRAGMA_OMP_PARALLEL:
26604 stmt = cp_parser_omp_parallel (parser, pragma_tok);
26606 case PRAGMA_OMP_SECTIONS:
26607 stmt = cp_parser_omp_sections (parser, pragma_tok);
26609 case PRAGMA_OMP_SINGLE:
26610 stmt = cp_parser_omp_single (parser, pragma_tok);
26612 case PRAGMA_OMP_TASK:
26613 stmt = cp_parser_omp_task (parser, pragma_tok);
26616 gcc_unreachable ();
26620 SET_EXPR_LOCATION (stmt, pragma_tok->location);
26625 static GTY (()) cp_parser *the_parser;
26628 /* Special handling for the first token or line in the file. The first
26629 thing in the file might be #pragma GCC pch_preprocess, which loads a
26630 PCH file, which is a GC collection point. So we need to handle this
26631 first pragma without benefit of an existing lexer structure.
26633 Always returns one token to the caller in *FIRST_TOKEN. This is
26634 either the true first token of the file, or the first token after
26635 the initial pragma. */
26638 cp_parser_initial_pragma (cp_token *first_token)
26642 cp_lexer_get_preprocessor_token (NULL, first_token);
26643 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26646 cp_lexer_get_preprocessor_token (NULL, first_token);
26647 if (first_token->type == CPP_STRING)
26649 name = first_token->u.value;
26651 cp_lexer_get_preprocessor_token (NULL, first_token);
26652 if (first_token->type != CPP_PRAGMA_EOL)
26653 error_at (first_token->location,
26654 "junk at end of %<#pragma GCC pch_preprocess%>");
26657 error_at (first_token->location, "expected string literal");
26659 /* Skip to the end of the pragma. */
26660 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26661 cp_lexer_get_preprocessor_token (NULL, first_token);
26663 /* Now actually load the PCH file. */
26665 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
26667 /* Read one more token to return to our caller. We have to do this
26668 after reading the PCH file in, since its pointers have to be
26670 cp_lexer_get_preprocessor_token (NULL, first_token);
26673 /* Normal parsing of a pragma token. Here we can (and must) use the
26677 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
26679 cp_token *pragma_tok;
26682 pragma_tok = cp_lexer_consume_token (parser->lexer);
26683 gcc_assert (pragma_tok->type == CPP_PRAGMA);
26684 parser->lexer->in_pragma = true;
26686 id = pragma_tok->pragma_kind;
26689 case PRAGMA_GCC_PCH_PREPROCESS:
26690 error_at (pragma_tok->location,
26691 "%<#pragma GCC pch_preprocess%> must be first");
26694 case PRAGMA_OMP_BARRIER:
26697 case pragma_compound:
26698 cp_parser_omp_barrier (parser, pragma_tok);
26701 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
26702 "used in compound statements");
26709 case PRAGMA_OMP_FLUSH:
26712 case pragma_compound:
26713 cp_parser_omp_flush (parser, pragma_tok);
26716 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
26717 "used in compound statements");
26724 case PRAGMA_OMP_TASKWAIT:
26727 case pragma_compound:
26728 cp_parser_omp_taskwait (parser, pragma_tok);
26731 error_at (pragma_tok->location,
26732 "%<#pragma omp taskwait%> may only be "
26733 "used in compound statements");
26740 case PRAGMA_OMP_TASKYIELD:
26743 case pragma_compound:
26744 cp_parser_omp_taskyield (parser, pragma_tok);
26747 error_at (pragma_tok->location,
26748 "%<#pragma omp taskyield%> may only be "
26749 "used in compound statements");
26756 case PRAGMA_OMP_THREADPRIVATE:
26757 cp_parser_omp_threadprivate (parser, pragma_tok);
26760 case PRAGMA_OMP_ATOMIC:
26761 case PRAGMA_OMP_CRITICAL:
26762 case PRAGMA_OMP_FOR:
26763 case PRAGMA_OMP_MASTER:
26764 case PRAGMA_OMP_ORDERED:
26765 case PRAGMA_OMP_PARALLEL:
26766 case PRAGMA_OMP_SECTIONS:
26767 case PRAGMA_OMP_SINGLE:
26768 case PRAGMA_OMP_TASK:
26769 if (context == pragma_external)
26771 cp_parser_omp_construct (parser, pragma_tok);
26774 case PRAGMA_OMP_SECTION:
26775 error_at (pragma_tok->location,
26776 "%<#pragma omp section%> may only be used in "
26777 "%<#pragma omp sections%> construct");
26781 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
26782 c_invoke_pragma_handler (id);
26786 cp_parser_error (parser, "expected declaration specifiers");
26790 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26794 /* The interface the pragma parsers have to the lexer. */
26797 pragma_lex (tree *value)
26800 enum cpp_ttype ret;
26802 tok = cp_lexer_peek_token (the_parser->lexer);
26805 *value = tok->u.value;
26807 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26809 else if (ret == CPP_STRING)
26810 *value = cp_parser_string_literal (the_parser, false, false);
26813 cp_lexer_consume_token (the_parser->lexer);
26814 if (ret == CPP_KEYWORD)
26822 /* External interface. */
26824 /* Parse one entire translation unit. */
26827 c_parse_file (void)
26829 static bool already_called = false;
26831 if (already_called)
26833 sorry ("inter-module optimizations not implemented for C++");
26836 already_called = true;
26838 the_parser = cp_parser_new ();
26839 push_deferring_access_checks (flag_access_control
26840 ? dk_no_deferred : dk_no_check);
26841 cp_parser_translation_unit (the_parser);
26845 #include "gt-cp-parser.h"