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 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215 (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
218 static void cp_lexer_start_debugging
219 (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224 about passing NULL to functions that require non-NULL arguments
225 (fputs, fprintf). It will never be used, so all we need is a value
226 of the right type that's guaranteed not to be NULL. */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
232 static cp_token_cache *cp_token_cache_new
233 (cp_token *, cp_token *);
235 static void cp_parser_initial_pragma
238 /* Manifest constants. */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250 sizeof, typeof, or alignof. */
251 int cp_unevaluated_operand;
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
266 num = VEC_length (cp_token, buffer);
268 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
270 cp_lexer_print_token (file, token);
275 case CPP_CLOSE_BRACE:
285 if (i == num && i < VEC_length (cp_token, buffer))
287 fprintf (file, " ... ");
288 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289 VEC_length (cp_token, buffer) - 1));
292 fprintf (file, "\n");
296 /* Dump all tokens in BUFFER to stderr. */
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
301 cp_lexer_dump_tokens (stderr, buffer, 0);
306 /* Allocate memory for a new lexer object and return it. */
309 cp_lexer_alloc (void)
313 c_common_no_more_pch ();
315 /* Allocate the memory. */
316 lexer = ggc_alloc_cleared_cp_lexer ();
318 #ifdef ENABLE_CHECKING
319 /* Initially we are not debugging. */
320 lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323 CP_SAVED_TOKEN_STACK);
325 /* Create the buffer. */
326 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
336 cp_lexer_new_main (void)
341 /* It's possible that parsing the first pragma will load a PCH file,
342 which is a GC collection point. So we have to do that before
343 allocating any memory. */
344 cp_parser_initial_pragma (&token);
346 lexer = cp_lexer_alloc ();
348 /* Put the first token in the buffer. */
349 VEC_quick_push (cp_token, lexer->buffer, &token);
351 /* Get the remaining tokens from the preprocessor. */
352 while (token.type != CPP_EOF)
354 cp_lexer_get_preprocessor_token (lexer, &token);
355 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
358 lexer->last_token = VEC_address (cp_token, lexer->buffer)
359 + VEC_length (cp_token, lexer->buffer)
361 lexer->next_token = VEC_length (cp_token, lexer->buffer)
362 ? VEC_address (cp_token, lexer->buffer)
365 /* Subsequent preprocessor diagnostics should use compiler
366 diagnostic functions to get the compiler source location. */
369 gcc_assert (!lexer->next_token->purged_p);
373 /* Create a new lexer whose token stream is primed with the tokens in
374 CACHE. When these tokens are exhausted, no new tokens will be read. */
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
379 cp_token *first = cache->first;
380 cp_token *last = cache->last;
381 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
383 /* We do not own the buffer. */
384 lexer->buffer = NULL;
385 lexer->next_token = first == last ? &eof_token : first;
386 lexer->last_token = last;
388 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389 CP_SAVED_TOKEN_STACK);
391 #ifdef ENABLE_CHECKING
392 /* Initially we are not debugging. */
393 lexer->debugging_p = false;
396 gcc_assert (!lexer->next_token->purged_p);
400 /* Frees all resources associated with LEXER. */
403 cp_lexer_destroy (cp_lexer *lexer)
405 VEC_free (cp_token, gc, lexer->buffer);
406 VEC_free (cp_token_position, heap, lexer->saved_tokens);
410 /* Returns nonzero if debugging information should be output. */
412 #ifdef ENABLE_CHECKING
415 cp_lexer_debugging_p (cp_lexer *lexer)
417 return lexer->debugging_p;
420 #endif /* ENABLE_CHECKING */
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
425 gcc_assert (!previous_p || lexer->next_token != &eof_token);
427 return lexer->next_token - previous_p;
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
439 lexer->next_token = cp_lexer_token_at (lexer, pos);
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
445 if (lexer->next_token == &eof_token)
446 return lexer->last_token - 1;
448 return cp_lexer_token_position (lexer, true);
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
454 cp_token_position tp = cp_lexer_previous_token_position (lexer);
456 return cp_lexer_token_at (lexer, tp);
459 /* nonzero if we are presently saving tokens. */
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
464 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
467 /* Store the next token from the preprocessor in *TOKEN. Return true
468 if we reach EOF. If LEXER is NULL, assume we are handling an
469 initial #pragma pch_preprocess, and thus want the lexer to return
470 processed strings. */
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
475 static int is_extern_c = 0;
477 /* Get a new token from the preprocessor. */
479 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481 token->keyword = RID_MAX;
482 token->pragma_kind = PRAGMA_NONE;
483 token->purged_p = false;
485 /* On some systems, some header files are surrounded by an
486 implicit extern "C" block. Set a flag in the token if it
487 comes from such a header. */
488 is_extern_c += pending_lang_change;
489 pending_lang_change = 0;
490 token->implicit_extern_c = is_extern_c > 0;
492 /* Check to see if this token is a keyword. */
493 if (token->type == CPP_NAME)
495 if (C_IS_RESERVED_WORD (token->u.value))
497 /* Mark this token as a keyword. */
498 token->type = CPP_KEYWORD;
499 /* Record which keyword. */
500 token->keyword = C_RID_CODE (token->u.value);
504 if (warn_cxx0x_compat
505 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
508 /* Warn about the C++0x keyword (but still treat it as
510 warning (OPT_Wc__0x_compat,
511 "identifier %qE will become a keyword in C++0x",
514 /* Clear out the C_RID_CODE so we don't warn about this
515 particular identifier-turned-keyword again. */
516 C_SET_RID_CODE (token->u.value, RID_MAX);
519 token->ambiguous_p = false;
520 token->keyword = RID_MAX;
523 else if (token->type == CPP_AT_NAME)
525 /* This only happens in Objective-C++; it must be a keyword. */
526 token->type = CPP_KEYWORD;
527 switch (C_RID_CODE (token->u.value))
529 /* Replace 'class' with '@class', 'private' with '@private',
530 etc. This prevents confusion with the C++ keyword
531 'class', and makes the tokens consistent with other
532 Objective-C 'AT' keywords. For example '@class' is
533 reported as RID_AT_CLASS which is consistent with
534 '@synchronized', which is reported as
537 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
538 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
539 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
541 case RID_THROW: token->keyword = RID_AT_THROW; break;
542 case RID_TRY: token->keyword = RID_AT_TRY; break;
543 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
544 default: token->keyword = C_RID_CODE (token->u.value);
547 else if (token->type == CPP_PRAGMA)
549 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
550 token->pragma_kind = ((enum pragma_kind)
551 TREE_INT_CST_LOW (token->u.value));
552 token->u.value = NULL_TREE;
556 /* Update the globals input_location and the input file stack from TOKEN. */
558 cp_lexer_set_source_position_from_token (cp_token *token)
560 if (token->type != CPP_EOF)
562 input_location = token->location;
566 /* Return a pointer to the next token in the token stream, but do not
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576 putc ('\n', cp_lexer_debug_stream);
578 return lexer->next_token;
581 /* Return true if the next token has the indicated TYPE. */
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
586 return cp_lexer_peek_token (lexer)->type == type;
589 /* Return true if the next token does not have the indicated TYPE. */
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
594 return !cp_lexer_next_token_is (lexer, type);
597 /* Return true if the next token is the indicated KEYWORD. */
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
602 return cp_lexer_peek_token (lexer)->keyword == keyword;
605 /* Return true if the next token is not the indicated KEYWORD. */
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
610 return cp_lexer_peek_token (lexer)->keyword != keyword;
613 /* Return true if the next token is a keyword for a decl-specifier. */
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
620 token = cp_lexer_peek_token (lexer);
621 switch (token->keyword)
623 /* auto specifier: storage-class-specifier in C++,
624 simple-type-specifier in C++0x. */
626 /* Storage classes. */
632 /* Elaborated type specifiers. */
638 /* Simple type specifiers. */
653 /* GNU extensions. */
656 /* C++0x extensions. */
658 case RID_UNDERLYING_TYPE:
666 /* Return a pointer to the Nth token in the token stream. If N is 1,
667 then this is precisely equivalent to cp_lexer_peek_token (except
668 that it is not inline). One would like to disallow that case, but
669 there is one case (cp_parser_nth_token_starts_template_id) where
670 the caller passes a variable for N and it might be 1. */
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
677 /* N is 1-based, not zero-based. */
680 if (cp_lexer_debugging_p (lexer))
681 fprintf (cp_lexer_debug_stream,
682 "cp_lexer: peeking ahead %ld at token: ", (long)n);
685 token = lexer->next_token;
686 gcc_assert (!n || token != &eof_token);
690 if (token == lexer->last_token)
696 if (!token->purged_p)
700 if (cp_lexer_debugging_p (lexer))
702 cp_lexer_print_token (cp_lexer_debug_stream, token);
703 putc ('\n', cp_lexer_debug_stream);
709 /* Return the next token, and advance the lexer's next_token pointer
710 to point to the next non-purged token. */
713 cp_lexer_consume_token (cp_lexer* lexer)
715 cp_token *token = lexer->next_token;
717 gcc_assert (token != &eof_token);
718 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
723 if (lexer->next_token == lexer->last_token)
725 lexer->next_token = &eof_token;
730 while (lexer->next_token->purged_p);
732 cp_lexer_set_source_position_from_token (token);
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
737 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738 cp_lexer_print_token (cp_lexer_debug_stream, token);
739 putc ('\n', cp_lexer_debug_stream);
745 /* Permanently remove the next token from the token stream, and
746 advance the next_token pointer to refer to the next non-purged
750 cp_lexer_purge_token (cp_lexer *lexer)
752 cp_token *tok = lexer->next_token;
754 gcc_assert (tok != &eof_token);
755 tok->purged_p = true;
756 tok->location = UNKNOWN_LOCATION;
757 tok->u.value = NULL_TREE;
758 tok->keyword = RID_MAX;
763 if (tok == lexer->last_token)
769 while (tok->purged_p);
770 lexer->next_token = tok;
773 /* Permanently remove all tokens after TOK, up to, but not
774 including, the token that will be returned next by
775 cp_lexer_peek_token. */
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
780 cp_token *peek = lexer->next_token;
782 if (peek == &eof_token)
783 peek = lexer->last_token;
785 gcc_assert (tok < peek);
787 for ( tok += 1; tok != peek; tok += 1)
789 tok->purged_p = true;
790 tok->location = UNKNOWN_LOCATION;
791 tok->u.value = NULL_TREE;
792 tok->keyword = RID_MAX;
796 /* Begin saving tokens. All tokens consumed after this point will be
800 cp_lexer_save_tokens (cp_lexer* lexer)
802 /* Provide debugging output. */
803 if (cp_lexer_debugging_p (lexer))
804 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
806 VEC_safe_push (cp_token_position, heap,
807 lexer->saved_tokens, lexer->next_token);
810 /* Commit to the portion of the token stream most recently saved. */
813 cp_lexer_commit_tokens (cp_lexer* lexer)
815 /* Provide debugging output. */
816 if (cp_lexer_debugging_p (lexer))
817 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
819 VEC_pop (cp_token_position, lexer->saved_tokens);
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823 to the token stream. Stop saving tokens. */
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
828 /* Provide debugging output. */
829 if (cp_lexer_debugging_p (lexer))
830 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
832 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
835 /* Print a representation of the TOKEN on the STREAM. */
837 #ifdef ENABLE_CHECKING
840 cp_lexer_print_token (FILE * stream, cp_token *token)
842 /* We don't use cpp_type2name here because the parser defines
843 a few tokens of its own. */
844 static const char *const token_names[] = {
845 /* cpplib-defined token types */
851 /* C++ parser token types - see "Manifest constants", above. */
854 "NESTED_NAME_SPECIFIER",
857 /* For some tokens, print the associated data. */
861 /* Some keywords have a value that is not an IDENTIFIER_NODE.
862 For example, `struct' is mapped to an INTEGER_CST. */
863 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
865 /* else fall through */
867 fputs (IDENTIFIER_POINTER (token->u.value), stream);
875 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
879 print_generic_expr (stream, token->u.value, 0);
883 /* If we have a name for the token, print it out. Otherwise, we
884 simply give the numeric code. */
885 if (token->type < ARRAY_SIZE(token_names))
886 fputs (token_names[token->type], stream);
888 fprintf (stream, "[%d]", token->type);
893 /* Start emitting debugging information. */
896 cp_lexer_start_debugging (cp_lexer* lexer)
898 lexer->debugging_p = true;
901 /* Stop emitting debugging information. */
904 cp_lexer_stop_debugging (cp_lexer* lexer)
906 lexer->debugging_p = false;
909 #endif /* ENABLE_CHECKING */
911 /* Create a new cp_token_cache, representing a range of tokens. */
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
916 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917 cache->first = first;
923 /* Decl-specifiers. */
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
930 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
935 /* Nothing other than the parser should be creating declarators;
936 declarators are a semi-syntactic representation of C++ entities.
937 Other parts of the front end that need to create entities (like
938 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
940 static cp_declarator *make_call_declarator
941 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
942 static cp_declarator *make_array_declarator
943 (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945 (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947 (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949 (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951 (cp_cv_quals, tree, cp_declarator *);
953 /* An erroneous declarator. */
954 static cp_declarator *cp_error_declarator;
956 /* The obstack on which declarators and related data structures are
958 static struct obstack declarator_obstack;
960 /* Alloc BYTES from the declarator memory pool. */
963 alloc_declarator (size_t bytes)
965 return obstack_alloc (&declarator_obstack, bytes);
968 /* Allocate a declarator of the indicated KIND. Clear fields that are
969 common to all declarators. */
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
974 cp_declarator *declarator;
976 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977 declarator->kind = kind;
978 declarator->attributes = NULL_TREE;
979 declarator->declarator = NULL;
980 declarator->parameter_pack_p = false;
981 declarator->id_loc = UNKNOWN_LOCATION;
986 /* Make a declarator for a generalized identifier. If
987 QUALIFYING_SCOPE is non-NULL, the identifier is
988 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989 UNQUALIFIED_NAME. SFK indicates the kind of special function this
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994 special_function_kind sfk)
996 cp_declarator *declarator;
998 /* It is valid to write:
1000 class C { void f(); };
1004 The standard is not clear about whether `typedef const C D' is
1005 legal; as of 2002-09-15 the committee is considering that
1006 question. EDG 3.0 allows that syntax. Therefore, we do as
1008 if (qualifying_scope && TYPE_P (qualifying_scope))
1009 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1011 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1015 declarator = make_declarator (cdk_id);
1016 declarator->u.id.qualifying_scope = qualifying_scope;
1017 declarator->u.id.unqualified_name = unqualified_name;
1018 declarator->u.id.sfk = sfk;
1023 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1024 of modifiers such as const or volatile to apply to the pointer
1025 type, represented as identifiers. */
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1030 cp_declarator *declarator;
1032 declarator = make_declarator (cdk_pointer);
1033 declarator->declarator = target;
1034 declarator->u.pointer.qualifiers = cv_qualifiers;
1035 declarator->u.pointer.class_type = NULL_TREE;
1038 declarator->id_loc = target->id_loc;
1039 declarator->parameter_pack_p = target->parameter_pack_p;
1040 target->parameter_pack_p = false;
1043 declarator->parameter_pack_p = false;
1048 /* Like make_pointer_declarator -- but for references. */
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1054 cp_declarator *declarator;
1056 declarator = make_declarator (cdk_reference);
1057 declarator->declarator = target;
1058 declarator->u.reference.qualifiers = cv_qualifiers;
1059 declarator->u.reference.rvalue_ref = rvalue_ref;
1062 declarator->id_loc = target->id_loc;
1063 declarator->parameter_pack_p = target->parameter_pack_p;
1064 target->parameter_pack_p = false;
1067 declarator->parameter_pack_p = false;
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073 member of CLASS_TYPE. */
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077 cp_declarator *pointee)
1079 cp_declarator *declarator;
1081 declarator = make_declarator (cdk_ptrmem);
1082 declarator->declarator = pointee;
1083 declarator->u.pointer.qualifiers = cv_qualifiers;
1084 declarator->u.pointer.class_type = class_type;
1088 declarator->parameter_pack_p = pointee->parameter_pack_p;
1089 pointee->parameter_pack_p = false;
1092 declarator->parameter_pack_p = false;
1097 /* Make a declarator for the function given by TARGET, with the
1098 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1099 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1100 indicates what exceptions can be thrown. */
1103 make_call_declarator (cp_declarator *target,
1105 cp_cv_quals cv_qualifiers,
1106 cp_virt_specifiers virt_specifiers,
1107 tree exception_specification,
1108 tree late_return_type)
1110 cp_declarator *declarator;
1112 declarator = make_declarator (cdk_function);
1113 declarator->declarator = target;
1114 declarator->u.function.parameters = parms;
1115 declarator->u.function.qualifiers = cv_qualifiers;
1116 declarator->u.function.virt_specifiers = virt_specifiers;
1117 declarator->u.function.exception_specification = exception_specification;
1118 declarator->u.function.late_return_type = late_return_type;
1121 declarator->id_loc = target->id_loc;
1122 declarator->parameter_pack_p = target->parameter_pack_p;
1123 target->parameter_pack_p = false;
1126 declarator->parameter_pack_p = false;
1131 /* Make a declarator for an array of BOUNDS elements, each of which is
1132 defined by ELEMENT. */
1135 make_array_declarator (cp_declarator *element, tree bounds)
1137 cp_declarator *declarator;
1139 declarator = make_declarator (cdk_array);
1140 declarator->declarator = element;
1141 declarator->u.array.bounds = bounds;
1144 declarator->id_loc = element->id_loc;
1145 declarator->parameter_pack_p = element->parameter_pack_p;
1146 element->parameter_pack_p = false;
1149 declarator->parameter_pack_p = false;
1154 /* Determine whether the declarator we've seen so far can be a
1155 parameter pack, when followed by an ellipsis. */
1157 declarator_can_be_parameter_pack (cp_declarator *declarator)
1159 /* Search for a declarator name, or any other declarator that goes
1160 after the point where the ellipsis could appear in a parameter
1161 pack. If we find any of these, then this declarator can not be
1162 made into a parameter pack. */
1164 while (declarator && !found)
1166 switch ((int)declarator->kind)
1177 declarator = declarator->declarator;
1185 cp_parameter_declarator *no_parameters;
1187 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1188 DECLARATOR and DEFAULT_ARGUMENT. */
1190 cp_parameter_declarator *
1191 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1192 cp_declarator *declarator,
1193 tree default_argument)
1195 cp_parameter_declarator *parameter;
1197 parameter = ((cp_parameter_declarator *)
1198 alloc_declarator (sizeof (cp_parameter_declarator)));
1199 parameter->next = NULL;
1200 if (decl_specifiers)
1201 parameter->decl_specifiers = *decl_specifiers;
1203 clear_decl_specs (¶meter->decl_specifiers);
1204 parameter->declarator = declarator;
1205 parameter->default_argument = default_argument;
1206 parameter->ellipsis_p = false;
1211 /* Returns true iff DECLARATOR is a declaration for a function. */
1214 function_declarator_p (const cp_declarator *declarator)
1218 if (declarator->kind == cdk_function
1219 && declarator->declarator->kind == cdk_id)
1221 if (declarator->kind == cdk_id
1222 || declarator->kind == cdk_error)
1224 declarator = declarator->declarator;
1234 A cp_parser parses the token stream as specified by the C++
1235 grammar. Its job is purely parsing, not semantic analysis. For
1236 example, the parser breaks the token stream into declarators,
1237 expressions, statements, and other similar syntactic constructs.
1238 It does not check that the types of the expressions on either side
1239 of an assignment-statement are compatible, or that a function is
1240 not declared with a parameter of type `void'.
1242 The parser invokes routines elsewhere in the compiler to perform
1243 semantic analysis and to build up the abstract syntax tree for the
1246 The parser (and the template instantiation code, which is, in a
1247 way, a close relative of parsing) are the only parts of the
1248 compiler that should be calling push_scope and pop_scope, or
1249 related functions. The parser (and template instantiation code)
1250 keeps track of what scope is presently active; everything else
1251 should simply honor that. (The code that generates static
1252 initializers may also need to set the scope, in order to check
1253 access control correctly when emitting the initializers.)
1258 The parser is of the standard recursive-descent variety. Upcoming
1259 tokens in the token stream are examined in order to determine which
1260 production to use when parsing a non-terminal. Some C++ constructs
1261 require arbitrary look ahead to disambiguate. For example, it is
1262 impossible, in the general case, to tell whether a statement is an
1263 expression or declaration without scanning the entire statement.
1264 Therefore, the parser is capable of "parsing tentatively." When the
1265 parser is not sure what construct comes next, it enters this mode.
1266 Then, while we attempt to parse the construct, the parser queues up
1267 error messages, rather than issuing them immediately, and saves the
1268 tokens it consumes. If the construct is parsed successfully, the
1269 parser "commits", i.e., it issues any queued error messages and
1270 the tokens that were being preserved are permanently discarded.
1271 If, however, the construct is not parsed successfully, the parser
1272 rolls back its state completely so that it can resume parsing using
1273 a different alternative.
1278 The performance of the parser could probably be improved substantially.
1279 We could often eliminate the need to parse tentatively by looking ahead
1280 a little bit. In some places, this approach might not entirely eliminate
1281 the need to parse tentatively, but it might still speed up the average
1284 /* Flags that are passed to some parsing functions. These values can
1285 be bitwise-ored together. */
1290 CP_PARSER_FLAGS_NONE = 0x0,
1291 /* The construct is optional. If it is not present, then no error
1292 should be issued. */
1293 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1294 /* When parsing a type-specifier, treat user-defined type-names
1295 as non-type identifiers. */
1296 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1297 /* When parsing a type-specifier, do not try to parse a class-specifier
1298 or enum-specifier. */
1299 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1300 /* When parsing a decl-specifier-seq, only allow type-specifier or
1302 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1305 /* This type is used for parameters and variables which hold
1306 combinations of the above flags. */
1307 typedef int cp_parser_flags;
1309 /* The different kinds of declarators we want to parse. */
1311 typedef enum cp_parser_declarator_kind
1313 /* We want an abstract declarator. */
1314 CP_PARSER_DECLARATOR_ABSTRACT,
1315 /* We want a named declarator. */
1316 CP_PARSER_DECLARATOR_NAMED,
1317 /* We don't mind, but the name must be an unqualified-id. */
1318 CP_PARSER_DECLARATOR_EITHER
1319 } cp_parser_declarator_kind;
1321 /* The precedence values used to parse binary expressions. The minimum value
1322 of PREC must be 1, because zero is reserved to quickly discriminate
1323 binary operators from other tokens. */
1328 PREC_LOGICAL_OR_EXPRESSION,
1329 PREC_LOGICAL_AND_EXPRESSION,
1330 PREC_INCLUSIVE_OR_EXPRESSION,
1331 PREC_EXCLUSIVE_OR_EXPRESSION,
1332 PREC_AND_EXPRESSION,
1333 PREC_EQUALITY_EXPRESSION,
1334 PREC_RELATIONAL_EXPRESSION,
1335 PREC_SHIFT_EXPRESSION,
1336 PREC_ADDITIVE_EXPRESSION,
1337 PREC_MULTIPLICATIVE_EXPRESSION,
1339 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1342 /* A mapping from a token type to a corresponding tree node type, with a
1343 precedence value. */
1345 typedef struct cp_parser_binary_operations_map_node
1347 /* The token type. */
1348 enum cpp_ttype token_type;
1349 /* The corresponding tree code. */
1350 enum tree_code tree_type;
1351 /* The precedence of this operator. */
1352 enum cp_parser_prec prec;
1353 } cp_parser_binary_operations_map_node;
1355 typedef struct cp_parser_expression_stack_entry
1357 /* Left hand side of the binary operation we are currently
1360 /* Original tree code for left hand side, if it was a binary
1361 expression itself (used for -Wparentheses). */
1362 enum tree_code lhs_type;
1363 /* Tree code for the binary operation we are parsing. */
1364 enum tree_code tree_type;
1365 /* Precedence of the binary operation we are parsing. */
1366 enum cp_parser_prec prec;
1367 } cp_parser_expression_stack_entry;
1369 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1370 entries because precedence levels on the stack are monotonically
1372 typedef struct cp_parser_expression_stack_entry
1373 cp_parser_expression_stack[NUM_PREC_VALUES];
1377 /* Constructors and destructors. */
1379 static cp_parser_context *cp_parser_context_new
1380 (cp_parser_context *);
1382 /* Class variables. */
1384 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1386 /* The operator-precedence table used by cp_parser_binary_expression.
1387 Transformed into an associative array (binops_by_token) by
1390 static const cp_parser_binary_operations_map_node binops[] = {
1391 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1392 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1394 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1398 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1401 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1404 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1405 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1406 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1409 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1410 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1412 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1414 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1416 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1418 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1420 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1423 /* The same as binops, but initialized by cp_parser_new so that
1424 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1426 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1428 /* Constructors and destructors. */
1430 /* Construct a new context. The context below this one on the stack
1431 is given by NEXT. */
1433 static cp_parser_context *
1434 cp_parser_context_new (cp_parser_context* next)
1436 cp_parser_context *context;
1438 /* Allocate the storage. */
1439 if (cp_parser_context_free_list != NULL)
1441 /* Pull the first entry from the free list. */
1442 context = cp_parser_context_free_list;
1443 cp_parser_context_free_list = context->next;
1444 memset (context, 0, sizeof (*context));
1447 context = ggc_alloc_cleared_cp_parser_context ();
1449 /* No errors have occurred yet in this context. */
1450 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1451 /* If this is not the bottommost context, copy information that we
1452 need from the previous context. */
1455 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1456 expression, then we are parsing one in this context, too. */
1457 context->object_type = next->object_type;
1458 /* Thread the stack. */
1459 context->next = next;
1465 /* Managing the unparsed function queues. */
1467 #define unparsed_funs_with_default_args \
1468 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1469 #define unparsed_funs_with_definitions \
1470 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1473 push_unparsed_function_queues (cp_parser *parser)
1475 VEC_safe_push (cp_unparsed_functions_entry, gc,
1476 parser->unparsed_queues, NULL);
1477 unparsed_funs_with_default_args = NULL;
1478 unparsed_funs_with_definitions = make_tree_vector ();
1482 pop_unparsed_function_queues (cp_parser *parser)
1484 release_tree_vector (unparsed_funs_with_definitions);
1485 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1490 /* Constructors and destructors. */
1492 static cp_parser *cp_parser_new
1495 /* Routines to parse various constructs.
1497 Those that return `tree' will return the error_mark_node (rather
1498 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1499 Sometimes, they will return an ordinary node if error-recovery was
1500 attempted, even though a parse error occurred. So, to check
1501 whether or not a parse error occurred, you should always use
1502 cp_parser_error_occurred. If the construct is optional (indicated
1503 either by an `_opt' in the name of the function that does the
1504 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1505 the construct is not present. */
1507 /* Lexical conventions [gram.lex] */
1509 static tree cp_parser_identifier
1511 static tree cp_parser_string_literal
1512 (cp_parser *, bool, bool);
1514 /* Basic concepts [gram.basic] */
1516 static bool cp_parser_translation_unit
1519 /* Expressions [gram.expr] */
1521 static tree cp_parser_primary_expression
1522 (cp_parser *, bool, bool, bool, cp_id_kind *);
1523 static tree cp_parser_id_expression
1524 (cp_parser *, bool, bool, bool *, bool, bool);
1525 static tree cp_parser_unqualified_id
1526 (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier_opt
1528 (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_nested_name_specifier
1530 (cp_parser *, bool, bool, bool, bool);
1531 static tree cp_parser_qualifying_entity
1532 (cp_parser *, bool, bool, bool, bool, bool);
1533 static tree cp_parser_postfix_expression
1534 (cp_parser *, bool, bool, bool, cp_id_kind *);
1535 static tree cp_parser_postfix_open_square_expression
1536 (cp_parser *, tree, bool);
1537 static tree cp_parser_postfix_dot_deref_expression
1538 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1539 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1540 (cp_parser *, int, bool, bool, bool *);
1541 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1542 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1543 static void cp_parser_pseudo_destructor_name
1544 (cp_parser *, tree *, tree *);
1545 static tree cp_parser_unary_expression
1546 (cp_parser *, bool, bool, cp_id_kind *);
1547 static enum tree_code cp_parser_unary_operator
1549 static tree cp_parser_new_expression
1551 static VEC(tree,gc) *cp_parser_new_placement
1553 static tree cp_parser_new_type_id
1554 (cp_parser *, tree *);
1555 static cp_declarator *cp_parser_new_declarator_opt
1557 static cp_declarator *cp_parser_direct_new_declarator
1559 static VEC(tree,gc) *cp_parser_new_initializer
1561 static tree cp_parser_delete_expression
1563 static tree cp_parser_cast_expression
1564 (cp_parser *, bool, bool, cp_id_kind *);
1565 static tree cp_parser_binary_expression
1566 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1567 static tree cp_parser_question_colon_clause
1568 (cp_parser *, tree);
1569 static tree cp_parser_assignment_expression
1570 (cp_parser *, bool, cp_id_kind *);
1571 static enum tree_code cp_parser_assignment_operator_opt
1573 static tree cp_parser_expression
1574 (cp_parser *, bool, cp_id_kind *);
1575 static tree cp_parser_constant_expression
1576 (cp_parser *, bool, bool *);
1577 static tree cp_parser_builtin_offsetof
1579 static tree cp_parser_lambda_expression
1581 static void cp_parser_lambda_introducer
1582 (cp_parser *, tree);
1583 static bool cp_parser_lambda_declarator_opt
1584 (cp_parser *, tree);
1585 static void cp_parser_lambda_body
1586 (cp_parser *, tree);
1588 /* Statements [gram.stmt.stmt] */
1590 static void cp_parser_statement
1591 (cp_parser *, tree, bool, bool *);
1592 static void cp_parser_label_for_labeled_statement
1594 static tree cp_parser_expression_statement
1595 (cp_parser *, tree);
1596 static tree cp_parser_compound_statement
1597 (cp_parser *, tree, bool, bool);
1598 static void cp_parser_statement_seq_opt
1599 (cp_parser *, tree);
1600 static tree cp_parser_selection_statement
1601 (cp_parser *, bool *);
1602 static tree cp_parser_condition
1604 static tree cp_parser_iteration_statement
1606 static bool cp_parser_for_init_statement
1607 (cp_parser *, tree *decl);
1608 static tree cp_parser_for
1610 static tree cp_parser_c_for
1611 (cp_parser *, tree, tree);
1612 static tree cp_parser_range_for
1613 (cp_parser *, tree, tree, tree);
1614 static tree cp_parser_perform_range_for_lookup
1615 (tree, tree *, tree *);
1616 static tree cp_parser_range_for_member_function
1618 static tree cp_parser_jump_statement
1620 static void cp_parser_declaration_statement
1623 static tree cp_parser_implicitly_scoped_statement
1624 (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1628 /* Declarations [gram.dcl.dcl] */
1630 static void cp_parser_declaration_seq_opt
1632 static void cp_parser_declaration
1634 static void cp_parser_block_declaration
1635 (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637 (cp_parser *, bool, tree *);
1638 static void cp_parser_decl_specifier_seq
1639 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1642 static tree cp_parser_function_specifier_opt
1643 (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1647 static tree cp_parser_simple_type_specifier
1648 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1651 static tree cp_parser_nonclass_name
1652 (cp_parser* parser);
1653 static tree cp_parser_elaborated_type_specifier
1654 (cp_parser *, bool, bool);
1655 static tree cp_parser_enum_specifier
1657 static void cp_parser_enumerator_list
1658 (cp_parser *, tree);
1659 static void cp_parser_enumerator_definition
1660 (cp_parser *, tree);
1661 static tree cp_parser_namespace_name
1663 static void cp_parser_namespace_definition
1665 static void cp_parser_namespace_body
1667 static tree cp_parser_qualified_namespace_specifier
1669 static void cp_parser_namespace_alias_definition
1671 static bool cp_parser_using_declaration
1672 (cp_parser *, bool);
1673 static void cp_parser_using_directive
1675 static void cp_parser_asm_definition
1677 static void cp_parser_linkage_specification
1679 static void cp_parser_static_assert
1680 (cp_parser *, bool);
1681 static tree cp_parser_decltype
1684 /* Declarators [gram.dcl.decl] */
1686 static tree cp_parser_init_declarator
1687 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1688 static cp_declarator *cp_parser_declarator
1689 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1690 static cp_declarator *cp_parser_direct_declarator
1691 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1692 static enum tree_code cp_parser_ptr_operator
1693 (cp_parser *, tree *, cp_cv_quals *);
1694 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1696 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1698 static tree cp_parser_late_return_type_opt
1700 static tree cp_parser_declarator_id
1701 (cp_parser *, bool);
1702 static tree cp_parser_type_id
1704 static tree cp_parser_template_type_arg
1706 static tree cp_parser_trailing_type_id (cp_parser *);
1707 static tree cp_parser_type_id_1
1708 (cp_parser *, bool, bool);
1709 static void cp_parser_type_specifier_seq
1710 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1711 static tree cp_parser_parameter_declaration_clause
1713 static tree cp_parser_parameter_declaration_list
1714 (cp_parser *, bool *);
1715 static cp_parameter_declarator *cp_parser_parameter_declaration
1716 (cp_parser *, bool, bool *);
1717 static tree cp_parser_default_argument
1718 (cp_parser *, bool);
1719 static void cp_parser_function_body
1721 static tree cp_parser_initializer
1722 (cp_parser *, bool *, bool *);
1723 static tree cp_parser_initializer_clause
1724 (cp_parser *, bool *);
1725 static tree cp_parser_braced_list
1726 (cp_parser*, bool*);
1727 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1728 (cp_parser *, bool *);
1730 static bool cp_parser_ctor_initializer_opt_and_function_body
1733 /* Classes [gram.class] */
1735 static tree cp_parser_class_name
1736 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1737 static tree cp_parser_class_specifier
1739 static tree cp_parser_class_head
1740 (cp_parser *, bool *, tree *, tree *);
1741 static enum tag_types cp_parser_class_key
1743 static void cp_parser_member_specification_opt
1745 static void cp_parser_member_declaration
1747 static tree cp_parser_pure_specifier
1749 static tree cp_parser_constant_initializer
1752 /* Derived classes [gram.class.derived] */
1754 static tree cp_parser_base_clause
1756 static tree cp_parser_base_specifier
1759 /* Special member functions [gram.special] */
1761 static tree cp_parser_conversion_function_id
1763 static tree cp_parser_conversion_type_id
1765 static cp_declarator *cp_parser_conversion_declarator_opt
1767 static bool cp_parser_ctor_initializer_opt
1769 static void cp_parser_mem_initializer_list
1771 static tree cp_parser_mem_initializer
1773 static tree cp_parser_mem_initializer_id
1776 /* Overloading [gram.over] */
1778 static tree cp_parser_operator_function_id
1780 static tree cp_parser_operator
1783 /* Templates [gram.temp] */
1785 static void cp_parser_template_declaration
1786 (cp_parser *, bool);
1787 static tree cp_parser_template_parameter_list
1789 static tree cp_parser_template_parameter
1790 (cp_parser *, bool *, bool *);
1791 static tree cp_parser_type_parameter
1792 (cp_parser *, bool *);
1793 static tree cp_parser_template_id
1794 (cp_parser *, bool, bool, bool);
1795 static tree cp_parser_template_name
1796 (cp_parser *, bool, bool, bool, bool *);
1797 static tree cp_parser_template_argument_list
1799 static tree cp_parser_template_argument
1801 static void cp_parser_explicit_instantiation
1803 static void cp_parser_explicit_specialization
1806 /* Exception handling [gram.exception] */
1808 static tree cp_parser_try_block
1810 static bool cp_parser_function_try_block
1812 static void cp_parser_handler_seq
1814 static void cp_parser_handler
1816 static tree cp_parser_exception_declaration
1818 static tree cp_parser_throw_expression
1820 static tree cp_parser_exception_specification_opt
1822 static tree cp_parser_type_id_list
1825 /* GNU Extensions */
1827 static tree cp_parser_asm_specification_opt
1829 static tree cp_parser_asm_operand_list
1831 static tree cp_parser_asm_clobber_list
1833 static tree cp_parser_asm_label_list
1835 static tree cp_parser_attributes_opt
1837 static tree cp_parser_attribute_list
1839 static bool cp_parser_extension_opt
1840 (cp_parser *, int *);
1841 static void cp_parser_label_declaration
1844 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1845 static bool cp_parser_pragma
1846 (cp_parser *, enum pragma_context);
1848 /* Objective-C++ Productions */
1850 static tree cp_parser_objc_message_receiver
1852 static tree cp_parser_objc_message_args
1854 static tree cp_parser_objc_message_expression
1856 static tree cp_parser_objc_encode_expression
1858 static tree cp_parser_objc_defs_expression
1860 static tree cp_parser_objc_protocol_expression
1862 static tree cp_parser_objc_selector_expression
1864 static tree cp_parser_objc_expression
1866 static bool cp_parser_objc_selector_p
1868 static tree cp_parser_objc_selector
1870 static tree cp_parser_objc_protocol_refs_opt
1872 static void cp_parser_objc_declaration
1873 (cp_parser *, tree);
1874 static tree cp_parser_objc_statement
1876 static bool cp_parser_objc_valid_prefix_attributes
1877 (cp_parser *, tree *);
1878 static void cp_parser_objc_at_property_declaration
1880 static void cp_parser_objc_at_synthesize_declaration
1882 static void cp_parser_objc_at_dynamic_declaration
1884 static tree cp_parser_objc_struct_declaration
1887 /* Utility Routines */
1889 static tree cp_parser_lookup_name
1890 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1891 static tree cp_parser_lookup_name_simple
1892 (cp_parser *, tree, location_t);
1893 static tree cp_parser_maybe_treat_template_as_class
1895 static bool cp_parser_check_declarator_template_parameters
1896 (cp_parser *, cp_declarator *, location_t);
1897 static bool cp_parser_check_template_parameters
1898 (cp_parser *, unsigned, location_t, cp_declarator *);
1899 static tree cp_parser_simple_cast_expression
1901 static tree cp_parser_global_scope_opt
1902 (cp_parser *, bool);
1903 static bool cp_parser_constructor_declarator_p
1904 (cp_parser *, bool);
1905 static tree cp_parser_function_definition_from_specifiers_and_declarator
1906 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1907 static tree cp_parser_function_definition_after_declarator
1908 (cp_parser *, bool);
1909 static void cp_parser_template_declaration_after_export
1910 (cp_parser *, bool);
1911 static void cp_parser_perform_template_parameter_access_checks
1912 (VEC (deferred_access_check,gc)*);
1913 static tree cp_parser_single_declaration
1914 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1915 static tree cp_parser_functional_cast
1916 (cp_parser *, tree);
1917 static tree cp_parser_save_member_function_body
1918 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1919 static tree cp_parser_enclosed_template_argument_list
1921 static void cp_parser_save_default_args
1922 (cp_parser *, tree);
1923 static void cp_parser_late_parsing_for_member
1924 (cp_parser *, tree);
1925 static void cp_parser_late_parsing_default_args
1926 (cp_parser *, tree);
1927 static tree cp_parser_sizeof_operand
1928 (cp_parser *, enum rid);
1929 static tree cp_parser_trait_expr
1930 (cp_parser *, enum rid);
1931 static bool cp_parser_declares_only_class_p
1933 static void cp_parser_set_storage_class
1934 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1935 static void cp_parser_set_decl_spec_type
1936 (cp_decl_specifier_seq *, tree, location_t, bool);
1937 static bool cp_parser_friend_p
1938 (const cp_decl_specifier_seq *);
1939 static void cp_parser_required_error
1940 (cp_parser *, required_token, bool);
1941 static cp_token *cp_parser_require
1942 (cp_parser *, enum cpp_ttype, required_token);
1943 static cp_token *cp_parser_require_keyword
1944 (cp_parser *, enum rid, required_token);
1945 static bool cp_parser_token_starts_function_definition_p
1947 static bool cp_parser_next_token_starts_class_definition_p
1949 static bool cp_parser_next_token_ends_template_argument_p
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952 (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1955 static void cp_parser_check_class_key
1956 (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958 (tree type, location_t location);
1959 static bool cp_parser_optional_template_keyword
1961 static void cp_parser_pre_parsed_nested_name_specifier
1963 static bool cp_parser_cache_group
1964 (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1967 static void cp_parser_commit_to_tentative_parse
1969 static void cp_parser_abort_tentative_parse
1971 static bool cp_parser_parse_definitely
1973 static inline bool cp_parser_parsing_tentatively
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1977 static void cp_parser_error
1978 (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980 (cp_parser *, tree, tree, name_lookup_error, location_t);
1981 static bool cp_parser_simulate_error
1983 static bool cp_parser_check_type_definition
1985 static void cp_parser_check_for_definition_in_return_type
1986 (cp_declarator *, tree, location_t type_location);
1987 static void cp_parser_check_for_invalid_template_id
1988 (cp_parser *, tree, location_t location);
1989 static bool cp_parser_non_integral_constant_expression
1990 (cp_parser *, non_integral_constant);
1991 static void cp_parser_diagnose_invalid_type_name
1992 (cp_parser *, tree, tree, location_t);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1995 static int cp_parser_skip_to_closing_parenthesis
1996 (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2001 static void cp_parser_skip_to_end_of_block_or_statement
2003 static bool cp_parser_skip_to_closing_brace
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2007 static void cp_parser_skip_to_pragma_eol
2008 (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2011 static bool cp_parser_allow_gnu_extensions_p
2013 static bool cp_parser_is_string_literal
2015 static bool cp_parser_is_keyword
2016 (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018 (cp_parser *, tree, tree, location_t location);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2022 /* Returns nonzero if we are parsing tentatively. */
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2027 return parser->context->next != NULL;
2030 /* Returns nonzero if TOKEN is a string literal. */
2033 cp_parser_is_string_literal (cp_token* token)
2035 return (token->type == CPP_STRING ||
2036 token->type == CPP_STRING16 ||
2037 token->type == CPP_STRING32 ||
2038 token->type == CPP_WSTRING ||
2039 token->type == CPP_UTF8STRING);
2042 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2045 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2047 return token->keyword == keyword;
2050 /* If not parsing tentatively, issue a diagnostic of the form
2051 FILE:LINE: MESSAGE before TOKEN
2052 where TOKEN is the next token in the input stream. MESSAGE
2053 (specified by the caller) is usually of the form "expected
2057 cp_parser_error (cp_parser* parser, const char* gmsgid)
2059 if (!cp_parser_simulate_error (parser))
2061 cp_token *token = cp_lexer_peek_token (parser->lexer);
2062 /* This diagnostic makes more sense if it is tagged to the line
2063 of the token we just peeked at. */
2064 cp_lexer_set_source_position_from_token (token);
2066 if (token->type == CPP_PRAGMA)
2068 error_at (token->location,
2069 "%<#pragma%> is not allowed here");
2070 cp_parser_skip_to_pragma_eol (parser, token);
2074 c_parse_error (gmsgid,
2075 /* Because c_parser_error does not understand
2076 CPP_KEYWORD, keywords are treated like
2078 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079 token->u.value, token->flags);
2083 /* Issue an error about name-lookup failing. NAME is the
2084 IDENTIFIER_NODE DECL is the result of
2085 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2086 the thing that we hoped to find. */
2089 cp_parser_name_lookup_error (cp_parser* parser,
2092 name_lookup_error desired,
2093 location_t location)
2095 /* If name lookup completely failed, tell the user that NAME was not
2097 if (decl == error_mark_node)
2099 if (parser->scope && parser->scope != global_namespace)
2100 error_at (location, "%<%E::%E%> has not been declared",
2101 parser->scope, name);
2102 else if (parser->scope == global_namespace)
2103 error_at (location, "%<::%E%> has not been declared", name);
2104 else if (parser->object_scope
2105 && !CLASS_TYPE_P (parser->object_scope))
2106 error_at (location, "request for member %qE in non-class type %qT",
2107 name, parser->object_scope);
2108 else if (parser->object_scope)
2109 error_at (location, "%<%T::%E%> has not been declared",
2110 parser->object_scope, name);
2112 error_at (location, "%qE has not been declared", name);
2114 else if (parser->scope && parser->scope != global_namespace)
2119 error_at (location, "%<%E::%E%> is not a type",
2120 parser->scope, name);
2123 error_at (location, "%<%E::%E%> is not a class or namespace",
2124 parser->scope, name);
2128 "%<%E::%E%> is not a class, namespace, or enumeration",
2129 parser->scope, name);
2136 else if (parser->scope == global_namespace)
2141 error_at (location, "%<::%E%> is not a type", name);
2144 error_at (location, "%<::%E%> is not a class or namespace", name);
2148 "%<::%E%> is not a class, namespace, or enumeration",
2160 error_at (location, "%qE is not a type", name);
2163 error_at (location, "%qE is not a class or namespace", name);
2167 "%qE is not a class, namespace, or enumeration", name);
2175 /* If we are parsing tentatively, remember that an error has occurred
2176 during this tentative parse. Returns true if the error was
2177 simulated; false if a message should be issued by the caller. */
2180 cp_parser_simulate_error (cp_parser* parser)
2182 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2184 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2190 /* Check for repeated decl-specifiers. */
2193 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2194 location_t location)
2198 for (ds = ds_first; ds != ds_last; ++ds)
2200 unsigned count = decl_specs->specs[ds];
2203 /* The "long" specifier is a special case because of "long long". */
2207 error_at (location, "%<long long long%> is too long for GCC");
2209 pedwarn_cxx98 (location, OPT_Wlong_long,
2210 "ISO C++ 1998 does not support %<long long%>");
2214 static const char *const decl_spec_names[] = {
2231 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2236 /* This function is called when a type is defined. If type
2237 definitions are forbidden at this point, an error message is
2241 cp_parser_check_type_definition (cp_parser* parser)
2243 /* If types are forbidden here, issue a message. */
2244 if (parser->type_definition_forbidden_message)
2246 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2247 in the message need to be interpreted. */
2248 error (parser->type_definition_forbidden_message);
2254 /* This function is called when the DECLARATOR is processed. The TYPE
2255 was a type defined in the decl-specifiers. If it is invalid to
2256 define a type in the decl-specifiers for DECLARATOR, an error is
2257 issued. TYPE_LOCATION is the location of TYPE and is used
2258 for error reporting. */
2261 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2262 tree type, location_t type_location)
2264 /* [dcl.fct] forbids type definitions in return types.
2265 Unfortunately, it's not easy to know whether or not we are
2266 processing a return type until after the fact. */
2268 && (declarator->kind == cdk_pointer
2269 || declarator->kind == cdk_reference
2270 || declarator->kind == cdk_ptrmem))
2271 declarator = declarator->declarator;
2273 && declarator->kind == cdk_function)
2275 error_at (type_location,
2276 "new types may not be defined in a return type");
2277 inform (type_location,
2278 "(perhaps a semicolon is missing after the definition of %qT)",
2283 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2284 "<" in any valid C++ program. If the next token is indeed "<",
2285 issue a message warning the user about what appears to be an
2286 invalid attempt to form a template-id. LOCATION is the location
2287 of the type-specifier (TYPE) */
2290 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2291 tree type, location_t location)
2293 cp_token_position start = 0;
2295 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2298 error_at (location, "%qT is not a template", type);
2299 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2300 error_at (location, "%qE is not a template", type);
2302 error_at (location, "invalid template-id");
2303 /* Remember the location of the invalid "<". */
2304 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2305 start = cp_lexer_token_position (parser->lexer, true);
2306 /* Consume the "<". */
2307 cp_lexer_consume_token (parser->lexer);
2308 /* Parse the template arguments. */
2309 cp_parser_enclosed_template_argument_list (parser);
2310 /* Permanently remove the invalid template arguments so that
2311 this error message is not issued again. */
2313 cp_lexer_purge_tokens_after (parser->lexer, start);
2317 /* If parsing an integral constant-expression, issue an error message
2318 about the fact that THING appeared and return true. Otherwise,
2319 return false. In either case, set
2320 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2323 cp_parser_non_integral_constant_expression (cp_parser *parser,
2324 non_integral_constant thing)
2326 parser->non_integral_constant_expression_p = true;
2327 if (parser->integral_constant_expression_p)
2329 if (!parser->allow_non_integral_constant_expression_p)
2331 const char *msg = NULL;
2335 error ("floating-point literal "
2336 "cannot appear in a constant-expression");
2339 error ("a cast to a type other than an integral or "
2340 "enumeration type cannot appear in a "
2341 "constant-expression");
2344 error ("%<typeid%> operator "
2345 "cannot appear in a constant-expression");
2348 error ("non-constant compound literals "
2349 "cannot appear in a constant-expression");
2352 error ("a function call "
2353 "cannot appear in a constant-expression");
2356 error ("an increment "
2357 "cannot appear in a constant-expression");
2360 error ("an decrement "
2361 "cannot appear in a constant-expression");
2364 error ("an array reference "
2365 "cannot appear in a constant-expression");
2367 case NIC_ADDR_LABEL:
2368 error ("the address of a label "
2369 "cannot appear in a constant-expression");
2371 case NIC_OVERLOADED:
2372 error ("calls to overloaded operators "
2373 "cannot appear in a constant-expression");
2375 case NIC_ASSIGNMENT:
2376 error ("an assignment cannot appear in a constant-expression");
2379 error ("a comma operator "
2380 "cannot appear in a constant-expression");
2382 case NIC_CONSTRUCTOR:
2383 error ("a call to a constructor "
2384 "cannot appear in a constant-expression");
2390 msg = "__FUNCTION__";
2392 case NIC_PRETTY_FUNC:
2393 msg = "__PRETTY_FUNCTION__";
2413 case NIC_PREINCREMENT:
2416 case NIC_PREDECREMENT:
2429 error ("%qs cannot appear in a constant-expression", msg);
2436 /* Emit a diagnostic for an invalid type name. SCOPE is the
2437 qualifying scope (or NULL, if none) for ID. This function commits
2438 to the current active tentative parse, if any. (Otherwise, the
2439 problematic construct might be encountered again later, resulting
2440 in duplicate error messages.) LOCATION is the location of ID. */
2443 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2444 tree scope, tree id,
2445 location_t location)
2447 tree decl, old_scope;
2448 cp_parser_commit_to_tentative_parse (parser);
2449 /* Try to lookup the identifier. */
2450 old_scope = parser->scope;
2451 parser->scope = scope;
2452 decl = cp_parser_lookup_name_simple (parser, id, location);
2453 parser->scope = old_scope;
2454 /* If the lookup found a template-name, it means that the user forgot
2455 to specify an argument list. Emit a useful error message. */
2456 if (TREE_CODE (decl) == TEMPLATE_DECL)
2458 "invalid use of template-name %qE without an argument list",
2460 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2461 error_at (location, "invalid use of destructor %qD as a type", id);
2462 else if (TREE_CODE (decl) == TYPE_DECL)
2463 /* Something like 'unsigned A a;' */
2464 error_at (location, "invalid combination of multiple type-specifiers");
2465 else if (!parser->scope)
2467 /* Issue an error message. */
2468 error_at (location, "%qE does not name a type", id);
2469 /* If we're in a template class, it's possible that the user was
2470 referring to a type from a base class. For example:
2472 template <typename T> struct A { typedef T X; };
2473 template <typename T> struct B : public A<T> { X x; };
2475 The user should have said "typename A<T>::X". */
2476 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2477 inform (location, "C++0x %<constexpr%> only available with "
2478 "-std=c++0x or -std=gnu++0x");
2479 else if (processing_template_decl && current_class_type
2480 && TYPE_BINFO (current_class_type))
2484 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2488 tree base_type = BINFO_TYPE (b);
2489 if (CLASS_TYPE_P (base_type)
2490 && dependent_type_p (base_type))
2493 /* Go from a particular instantiation of the
2494 template (which will have an empty TYPE_FIELDs),
2495 to the main version. */
2496 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2497 for (field = TYPE_FIELDS (base_type);
2499 field = DECL_CHAIN (field))
2500 if (TREE_CODE (field) == TYPE_DECL
2501 && DECL_NAME (field) == id)
2504 "(perhaps %<typename %T::%E%> was intended)",
2505 BINFO_TYPE (b), id);
2514 /* Here we diagnose qualified-ids where the scope is actually correct,
2515 but the identifier does not resolve to a valid type name. */
2516 else if (parser->scope != error_mark_node)
2518 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2519 error_at (location, "%qE in namespace %qE does not name a type",
2521 else if (CLASS_TYPE_P (parser->scope)
2522 && constructor_name_p (id, parser->scope))
2525 error_at (location, "%<%T::%E%> names the constructor, not"
2526 " the type", parser->scope, id);
2527 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2528 error_at (location, "and %qT has no template constructors",
2531 else if (TYPE_P (parser->scope)
2532 && dependent_scope_p (parser->scope))
2533 error_at (location, "need %<typename%> before %<%T::%E%> because "
2534 "%qT is a dependent scope",
2535 parser->scope, id, parser->scope);
2536 else if (TYPE_P (parser->scope))
2537 error_at (location, "%qE in %q#T does not name a type",
2544 /* Check for a common situation where a type-name should be present,
2545 but is not, and issue a sensible error message. Returns true if an
2546 invalid type-name was detected.
2548 The situation handled by this function are variable declarations of the
2549 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2550 Usually, `ID' should name a type, but if we got here it means that it
2551 does not. We try to emit the best possible error message depending on
2552 how exactly the id-expression looks like. */
2555 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2558 cp_token *token = cp_lexer_peek_token (parser->lexer);
2560 /* Avoid duplicate error about ambiguous lookup. */
2561 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2563 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2564 if (next->type == CPP_NAME && next->ambiguous_p)
2568 cp_parser_parse_tentatively (parser);
2569 id = cp_parser_id_expression (parser,
2570 /*template_keyword_p=*/false,
2571 /*check_dependency_p=*/true,
2572 /*template_p=*/NULL,
2573 /*declarator_p=*/true,
2574 /*optional_p=*/false);
2575 /* If the next token is a (, this is a function with no explicit return
2576 type, i.e. constructor, destructor or conversion op. */
2577 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2578 || TREE_CODE (id) == TYPE_DECL)
2580 cp_parser_abort_tentative_parse (parser);
2583 if (!cp_parser_parse_definitely (parser))
2586 /* Emit a diagnostic for the invalid type. */
2587 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2588 id, token->location);
2590 /* If we aren't in the middle of a declarator (i.e. in a
2591 parameter-declaration-clause), skip to the end of the declaration;
2592 there's no point in trying to process it. */
2593 if (!parser->in_declarator_p)
2594 cp_parser_skip_to_end_of_block_or_statement (parser);
2598 /* Consume tokens up to, and including, the next non-nested closing `)'.
2599 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2600 are doing error recovery. Returns -1 if OR_COMMA is true and we
2601 found an unnested comma. */
2604 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2609 unsigned paren_depth = 0;
2610 unsigned brace_depth = 0;
2611 unsigned square_depth = 0;
2613 if (recovering && !or_comma
2614 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2619 cp_token * token = cp_lexer_peek_token (parser->lexer);
2621 switch (token->type)
2624 case CPP_PRAGMA_EOL:
2625 /* If we've run out of tokens, then there is no closing `)'. */
2628 /* This is good for lambda expression capture-lists. */
2629 case CPP_OPEN_SQUARE:
2632 case CPP_CLOSE_SQUARE:
2633 if (!square_depth--)
2638 /* This matches the processing in skip_to_end_of_statement. */
2643 case CPP_OPEN_BRACE:
2646 case CPP_CLOSE_BRACE:
2652 if (recovering && or_comma && !brace_depth && !paren_depth
2657 case CPP_OPEN_PAREN:
2662 case CPP_CLOSE_PAREN:
2663 if (!brace_depth && !paren_depth--)
2666 cp_lexer_consume_token (parser->lexer);
2675 /* Consume the token. */
2676 cp_lexer_consume_token (parser->lexer);
2680 /* Consume tokens until we reach the end of the current statement.
2681 Normally, that will be just before consuming a `;'. However, if a
2682 non-nested `}' comes first, then we stop before consuming that. */
2685 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2687 unsigned nesting_depth = 0;
2691 cp_token *token = cp_lexer_peek_token (parser->lexer);
2693 switch (token->type)
2696 case CPP_PRAGMA_EOL:
2697 /* If we've run out of tokens, stop. */
2701 /* If the next token is a `;', we have reached the end of the
2707 case CPP_CLOSE_BRACE:
2708 /* If this is a non-nested '}', stop before consuming it.
2709 That way, when confronted with something like:
2713 we stop before consuming the closing '}', even though we
2714 have not yet reached a `;'. */
2715 if (nesting_depth == 0)
2718 /* If it is the closing '}' for a block that we have
2719 scanned, stop -- but only after consuming the token.
2725 we will stop after the body of the erroneously declared
2726 function, but before consuming the following `typedef'
2728 if (--nesting_depth == 0)
2730 cp_lexer_consume_token (parser->lexer);
2734 case CPP_OPEN_BRACE:
2742 /* Consume the token. */
2743 cp_lexer_consume_token (parser->lexer);
2747 /* This function is called at the end of a statement or declaration.
2748 If the next token is a semicolon, it is consumed; otherwise, error
2749 recovery is attempted. */
2752 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2754 /* Look for the trailing `;'. */
2755 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2757 /* If there is additional (erroneous) input, skip to the end of
2759 cp_parser_skip_to_end_of_statement (parser);
2760 /* If the next token is now a `;', consume it. */
2761 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2762 cp_lexer_consume_token (parser->lexer);
2766 /* Skip tokens until we have consumed an entire block, or until we
2767 have consumed a non-nested `;'. */
2770 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2772 int nesting_depth = 0;
2774 while (nesting_depth >= 0)
2776 cp_token *token = cp_lexer_peek_token (parser->lexer);
2778 switch (token->type)
2781 case CPP_PRAGMA_EOL:
2782 /* If we've run out of tokens, stop. */
2786 /* Stop if this is an unnested ';'. */
2791 case CPP_CLOSE_BRACE:
2792 /* Stop if this is an unnested '}', or closes the outermost
2795 if (nesting_depth < 0)
2801 case CPP_OPEN_BRACE:
2810 /* Consume the token. */
2811 cp_lexer_consume_token (parser->lexer);
2815 /* Skip tokens until a non-nested closing curly brace is the next
2816 token, or there are no more tokens. Return true in the first case,
2820 cp_parser_skip_to_closing_brace (cp_parser *parser)
2822 unsigned nesting_depth = 0;
2826 cp_token *token = cp_lexer_peek_token (parser->lexer);
2828 switch (token->type)
2831 case CPP_PRAGMA_EOL:
2832 /* If we've run out of tokens, stop. */
2835 case CPP_CLOSE_BRACE:
2836 /* If the next token is a non-nested `}', then we have reached
2837 the end of the current block. */
2838 if (nesting_depth-- == 0)
2842 case CPP_OPEN_BRACE:
2843 /* If it the next token is a `{', then we are entering a new
2844 block. Consume the entire block. */
2852 /* Consume the token. */
2853 cp_lexer_consume_token (parser->lexer);
2857 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2858 parameter is the PRAGMA token, allowing us to purge the entire pragma
2862 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2866 parser->lexer->in_pragma = false;
2869 token = cp_lexer_consume_token (parser->lexer);
2870 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2872 /* Ensure that the pragma is not parsed again. */
2873 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2876 /* Require pragma end of line, resyncing with it as necessary. The
2877 arguments are as for cp_parser_skip_to_pragma_eol. */
2880 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2882 parser->lexer->in_pragma = false;
2883 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2884 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2887 /* This is a simple wrapper around make_typename_type. When the id is
2888 an unresolved identifier node, we can provide a superior diagnostic
2889 using cp_parser_diagnose_invalid_type_name. */
2892 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2893 tree id, location_t id_location)
2896 if (TREE_CODE (id) == IDENTIFIER_NODE)
2898 result = make_typename_type (scope, id, typename_type,
2899 /*complain=*/tf_none);
2900 if (result == error_mark_node)
2901 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2904 return make_typename_type (scope, id, typename_type, tf_error);
2907 /* This is a wrapper around the
2908 make_{pointer,ptrmem,reference}_declarator functions that decides
2909 which one to call based on the CODE and CLASS_TYPE arguments. The
2910 CODE argument should be one of the values returned by
2911 cp_parser_ptr_operator. */
2912 static cp_declarator *
2913 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2914 cp_cv_quals cv_qualifiers,
2915 cp_declarator *target)
2917 if (code == ERROR_MARK)
2918 return cp_error_declarator;
2920 if (code == INDIRECT_REF)
2921 if (class_type == NULL_TREE)
2922 return make_pointer_declarator (cv_qualifiers, target);
2924 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2925 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2926 return make_reference_declarator (cv_qualifiers, target, false);
2927 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2928 return make_reference_declarator (cv_qualifiers, target, true);
2932 /* Create a new C++ parser. */
2935 cp_parser_new (void)
2941 /* cp_lexer_new_main is called before doing GC allocation because
2942 cp_lexer_new_main might load a PCH file. */
2943 lexer = cp_lexer_new_main ();
2945 /* Initialize the binops_by_token so that we can get the tree
2946 directly from the token. */
2947 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2948 binops_by_token[binops[i].token_type] = binops[i];
2950 parser = ggc_alloc_cleared_cp_parser ();
2951 parser->lexer = lexer;
2952 parser->context = cp_parser_context_new (NULL);
2954 /* For now, we always accept GNU extensions. */
2955 parser->allow_gnu_extensions_p = 1;
2957 /* The `>' token is a greater-than operator, not the end of a
2959 parser->greater_than_is_operator_p = true;
2961 parser->default_arg_ok_p = true;
2963 /* We are not parsing a constant-expression. */
2964 parser->integral_constant_expression_p = false;
2965 parser->allow_non_integral_constant_expression_p = false;
2966 parser->non_integral_constant_expression_p = false;
2968 /* Local variable names are not forbidden. */
2969 parser->local_variables_forbidden_p = false;
2971 /* We are not processing an `extern "C"' declaration. */
2972 parser->in_unbraced_linkage_specification_p = false;
2974 /* We are not processing a declarator. */
2975 parser->in_declarator_p = false;
2977 /* We are not processing a template-argument-list. */
2978 parser->in_template_argument_list_p = false;
2980 /* We are not in an iteration statement. */
2981 parser->in_statement = 0;
2983 /* We are not in a switch statement. */
2984 parser->in_switch_statement_p = false;
2986 /* We are not parsing a type-id inside an expression. */
2987 parser->in_type_id_in_expr_p = false;
2989 /* Declarations aren't implicitly extern "C". */
2990 parser->implicit_extern_c = false;
2992 /* String literals should be translated to the execution character set. */
2993 parser->translate_strings_p = true;
2995 /* We are not parsing a function body. */
2996 parser->in_function_body = false;
2998 /* We can correct until told otherwise. */
2999 parser->colon_corrects_to_scope_p = true;
3001 /* The unparsed function queue is empty. */
3002 push_unparsed_function_queues (parser);
3004 /* There are no classes being defined. */
3005 parser->num_classes_being_defined = 0;
3007 /* No template parameters apply. */
3008 parser->num_template_parameter_lists = 0;
3013 /* Create a cp_lexer structure which will emit the tokens in CACHE
3014 and push it onto the parser's lexer stack. This is used for delayed
3015 parsing of in-class method bodies and default arguments, and should
3016 not be confused with tentative parsing. */
3018 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3020 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3021 lexer->next = parser->lexer;
3022 parser->lexer = lexer;
3024 /* Move the current source position to that of the first token in the
3026 cp_lexer_set_source_position_from_token (lexer->next_token);
3029 /* Pop the top lexer off the parser stack. This is never used for the
3030 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3032 cp_parser_pop_lexer (cp_parser *parser)
3034 cp_lexer *lexer = parser->lexer;
3035 parser->lexer = lexer->next;
3036 cp_lexer_destroy (lexer);
3038 /* Put the current source position back where it was before this
3039 lexer was pushed. */
3040 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3043 /* Lexical conventions [gram.lex] */
3045 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3049 cp_parser_identifier (cp_parser* parser)
3053 /* Look for the identifier. */
3054 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3055 /* Return the value. */
3056 return token ? token->u.value : error_mark_node;
3059 /* Parse a sequence of adjacent string constants. Returns a
3060 TREE_STRING representing the combined, nul-terminated string
3061 constant. If TRANSLATE is true, translate the string to the
3062 execution character set. If WIDE_OK is true, a wide string is
3065 C++98 [lex.string] says that if a narrow string literal token is
3066 adjacent to a wide string literal token, the behavior is undefined.
3067 However, C99 6.4.5p4 says that this results in a wide string literal.
3068 We follow C99 here, for consistency with the C front end.
3070 This code is largely lifted from lex_string() in c-lex.c.
3072 FUTURE: ObjC++ will need to handle @-strings here. */
3074 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3078 struct obstack str_ob;
3079 cpp_string str, istr, *strs;
3081 enum cpp_ttype type;
3083 tok = cp_lexer_peek_token (parser->lexer);
3084 if (!cp_parser_is_string_literal (tok))
3086 cp_parser_error (parser, "expected string-literal");
3087 return error_mark_node;
3092 /* Try to avoid the overhead of creating and destroying an obstack
3093 for the common case of just one string. */
3094 if (!cp_parser_is_string_literal
3095 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3097 cp_lexer_consume_token (parser->lexer);
3099 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3100 str.len = TREE_STRING_LENGTH (tok->u.value);
3107 gcc_obstack_init (&str_ob);
3112 cp_lexer_consume_token (parser->lexer);
3114 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3115 str.len = TREE_STRING_LENGTH (tok->u.value);
3117 if (type != tok->type)
3119 if (type == CPP_STRING)
3121 else if (tok->type != CPP_STRING)
3122 error_at (tok->location,
3123 "unsupported non-standard concatenation "
3124 "of string literals");
3127 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3129 tok = cp_lexer_peek_token (parser->lexer);
3131 while (cp_parser_is_string_literal (tok));
3133 strs = (cpp_string *) obstack_finish (&str_ob);
3136 if (type != CPP_STRING && !wide_ok)
3138 cp_parser_error (parser, "a wide string is invalid in this context");
3142 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3143 (parse_in, strs, count, &istr, type))
3145 value = build_string (istr.len, (const char *)istr.text);
3146 free (CONST_CAST (unsigned char *, istr.text));
3152 case CPP_UTF8STRING:
3153 TREE_TYPE (value) = char_array_type_node;
3156 TREE_TYPE (value) = char16_array_type_node;
3159 TREE_TYPE (value) = char32_array_type_node;
3162 TREE_TYPE (value) = wchar_array_type_node;
3166 value = fix_string_type (value);
3169 /* cpp_interpret_string has issued an error. */
3170 value = error_mark_node;
3173 obstack_free (&str_ob, 0);
3179 /* Basic concepts [gram.basic] */
3181 /* Parse a translation-unit.
3184 declaration-seq [opt]
3186 Returns TRUE if all went well. */
3189 cp_parser_translation_unit (cp_parser* parser)
3191 /* The address of the first non-permanent object on the declarator
3193 static void *declarator_obstack_base;
3197 /* Create the declarator obstack, if necessary. */
3198 if (!cp_error_declarator)
3200 gcc_obstack_init (&declarator_obstack);
3201 /* Create the error declarator. */
3202 cp_error_declarator = make_declarator (cdk_error);
3203 /* Create the empty parameter list. */
3204 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3205 /* Remember where the base of the declarator obstack lies. */
3206 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3209 cp_parser_declaration_seq_opt (parser);
3211 /* If there are no tokens left then all went well. */
3212 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3214 /* Get rid of the token array; we don't need it any more. */
3215 cp_lexer_destroy (parser->lexer);
3216 parser->lexer = NULL;
3218 /* This file might have been a context that's implicitly extern
3219 "C". If so, pop the lang context. (Only relevant for PCH.) */
3220 if (parser->implicit_extern_c)
3222 pop_lang_context ();
3223 parser->implicit_extern_c = false;
3227 finish_translation_unit ();
3233 cp_parser_error (parser, "expected declaration");
3237 /* Make sure the declarator obstack was fully cleaned up. */
3238 gcc_assert (obstack_next_free (&declarator_obstack)
3239 == declarator_obstack_base);
3241 /* All went well. */
3245 /* Expressions [gram.expr] */
3247 /* Parse a primary-expression.
3258 ( compound-statement )
3259 __builtin_va_arg ( assignment-expression , type-id )
3260 __builtin_offsetof ( type-id , offsetof-expression )
3263 __has_nothrow_assign ( type-id )
3264 __has_nothrow_constructor ( type-id )
3265 __has_nothrow_copy ( type-id )
3266 __has_trivial_assign ( type-id )
3267 __has_trivial_constructor ( type-id )
3268 __has_trivial_copy ( type-id )
3269 __has_trivial_destructor ( type-id )
3270 __has_virtual_destructor ( type-id )
3271 __is_abstract ( type-id )
3272 __is_base_of ( type-id , type-id )
3273 __is_class ( type-id )
3274 __is_convertible_to ( type-id , type-id )
3275 __is_empty ( type-id )
3276 __is_enum ( type-id )
3277 __is_literal_type ( type-id )
3278 __is_pod ( type-id )
3279 __is_polymorphic ( type-id )
3280 __is_std_layout ( type-id )
3281 __is_trivial ( type-id )
3282 __is_union ( type-id )
3284 Objective-C++ Extension:
3292 ADDRESS_P is true iff this expression was immediately preceded by
3293 "&" and therefore might denote a pointer-to-member. CAST_P is true
3294 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3295 true iff this expression is a template argument.
3297 Returns a representation of the expression. Upon return, *IDK
3298 indicates what kind of id-expression (if any) was present. */
3301 cp_parser_primary_expression (cp_parser *parser,
3304 bool template_arg_p,
3307 cp_token *token = NULL;
3309 /* Assume the primary expression is not an id-expression. */
3310 *idk = CP_ID_KIND_NONE;
3312 /* Peek at the next token. */
3313 token = cp_lexer_peek_token (parser->lexer);
3314 switch (token->type)
3327 token = cp_lexer_consume_token (parser->lexer);
3328 if (TREE_CODE (token->u.value) == FIXED_CST)
3330 error_at (token->location,
3331 "fixed-point types not supported in C++");
3332 return error_mark_node;
3334 /* Floating-point literals are only allowed in an integral
3335 constant expression if they are cast to an integral or
3336 enumeration type. */
3337 if (TREE_CODE (token->u.value) == REAL_CST
3338 && parser->integral_constant_expression_p
3341 /* CAST_P will be set even in invalid code like "int(2.7 +
3342 ...)". Therefore, we have to check that the next token
3343 is sure to end the cast. */
3346 cp_token *next_token;
3348 next_token = cp_lexer_peek_token (parser->lexer);
3349 if (/* The comma at the end of an
3350 enumerator-definition. */
3351 next_token->type != CPP_COMMA
3352 /* The curly brace at the end of an enum-specifier. */
3353 && next_token->type != CPP_CLOSE_BRACE
3354 /* The end of a statement. */
3355 && next_token->type != CPP_SEMICOLON
3356 /* The end of the cast-expression. */
3357 && next_token->type != CPP_CLOSE_PAREN
3358 /* The end of an array bound. */
3359 && next_token->type != CPP_CLOSE_SQUARE
3360 /* The closing ">" in a template-argument-list. */
3361 && (next_token->type != CPP_GREATER
3362 || parser->greater_than_is_operator_p)
3363 /* C++0x only: A ">>" treated like two ">" tokens,
3364 in a template-argument-list. */
3365 && (next_token->type != CPP_RSHIFT
3366 || (cxx_dialect == cxx98)
3367 || parser->greater_than_is_operator_p))
3371 /* If we are within a cast, then the constraint that the
3372 cast is to an integral or enumeration type will be
3373 checked at that point. If we are not within a cast, then
3374 this code is invalid. */
3376 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3378 return token->u.value;
3384 case CPP_UTF8STRING:
3385 /* ??? Should wide strings be allowed when parser->translate_strings_p
3386 is false (i.e. in attributes)? If not, we can kill the third
3387 argument to cp_parser_string_literal. */
3388 return cp_parser_string_literal (parser,
3389 parser->translate_strings_p,
3392 case CPP_OPEN_PAREN:
3395 bool saved_greater_than_is_operator_p;
3397 /* Consume the `('. */
3398 cp_lexer_consume_token (parser->lexer);
3399 /* Within a parenthesized expression, a `>' token is always
3400 the greater-than operator. */
3401 saved_greater_than_is_operator_p
3402 = parser->greater_than_is_operator_p;
3403 parser->greater_than_is_operator_p = true;
3404 /* If we see `( { ' then we are looking at the beginning of
3405 a GNU statement-expression. */
3406 if (cp_parser_allow_gnu_extensions_p (parser)
3407 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3409 /* Statement-expressions are not allowed by the standard. */
3410 pedwarn (token->location, OPT_pedantic,
3411 "ISO C++ forbids braced-groups within expressions");
3413 /* And they're not allowed outside of a function-body; you
3414 cannot, for example, write:
3416 int i = ({ int j = 3; j + 1; });
3418 at class or namespace scope. */
3419 if (!parser->in_function_body
3420 || parser->in_template_argument_list_p)
3422 error_at (token->location,
3423 "statement-expressions are not allowed outside "
3424 "functions nor in template-argument lists");
3425 cp_parser_skip_to_end_of_block_or_statement (parser);
3426 expr = error_mark_node;
3430 /* Start the statement-expression. */
3431 expr = begin_stmt_expr ();
3432 /* Parse the compound-statement. */
3433 cp_parser_compound_statement (parser, expr, false, false);
3435 expr = finish_stmt_expr (expr, false);
3440 /* Parse the parenthesized expression. */
3441 expr = cp_parser_expression (parser, cast_p, idk);
3442 /* Let the front end know that this expression was
3443 enclosed in parentheses. This matters in case, for
3444 example, the expression is of the form `A::B', since
3445 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3447 finish_parenthesized_expr (expr);
3448 /* DR 705: Wrapping an unqualified name in parentheses
3449 suppresses arg-dependent lookup. We want to pass back
3450 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3451 (c++/37862), but none of the others. */
3452 if (*idk != CP_ID_KIND_QUALIFIED)
3453 *idk = CP_ID_KIND_NONE;
3455 /* The `>' token might be the end of a template-id or
3456 template-parameter-list now. */
3457 parser->greater_than_is_operator_p
3458 = saved_greater_than_is_operator_p;
3459 /* Consume the `)'. */
3460 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3461 cp_parser_skip_to_end_of_statement (parser);
3466 case CPP_OPEN_SQUARE:
3467 if (c_dialect_objc ())
3468 /* We have an Objective-C++ message. */
3469 return cp_parser_objc_expression (parser);
3471 tree lam = cp_parser_lambda_expression (parser);
3472 /* Don't warn about a failed tentative parse. */
3473 if (cp_parser_error_occurred (parser))
3474 return error_mark_node;
3475 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3479 case CPP_OBJC_STRING:
3480 if (c_dialect_objc ())
3481 /* We have an Objective-C++ string literal. */
3482 return cp_parser_objc_expression (parser);
3483 cp_parser_error (parser, "expected primary-expression");
3484 return error_mark_node;
3487 switch (token->keyword)
3489 /* These two are the boolean literals. */
3491 cp_lexer_consume_token (parser->lexer);
3492 return boolean_true_node;
3494 cp_lexer_consume_token (parser->lexer);
3495 return boolean_false_node;
3497 /* The `__null' literal. */
3499 cp_lexer_consume_token (parser->lexer);
3502 /* The `nullptr' literal. */
3504 cp_lexer_consume_token (parser->lexer);
3505 return nullptr_node;
3507 /* Recognize the `this' keyword. */
3509 cp_lexer_consume_token (parser->lexer);
3510 if (parser->local_variables_forbidden_p)
3512 error_at (token->location,
3513 "%<this%> may not be used in this context");
3514 return error_mark_node;
3516 /* Pointers cannot appear in constant-expressions. */
3517 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3518 return error_mark_node;
3519 return finish_this_expr ();
3521 /* The `operator' keyword can be the beginning of an
3526 case RID_FUNCTION_NAME:
3527 case RID_PRETTY_FUNCTION_NAME:
3528 case RID_C99_FUNCTION_NAME:
3530 non_integral_constant name;
3532 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3533 __func__ are the names of variables -- but they are
3534 treated specially. Therefore, they are handled here,
3535 rather than relying on the generic id-expression logic
3536 below. Grammatically, these names are id-expressions.
3538 Consume the token. */
3539 token = cp_lexer_consume_token (parser->lexer);
3541 switch (token->keyword)
3543 case RID_FUNCTION_NAME:
3544 name = NIC_FUNC_NAME;
3546 case RID_PRETTY_FUNCTION_NAME:
3547 name = NIC_PRETTY_FUNC;
3549 case RID_C99_FUNCTION_NAME:
3550 name = NIC_C99_FUNC;
3556 if (cp_parser_non_integral_constant_expression (parser, name))
3557 return error_mark_node;
3559 /* Look up the name. */
3560 return finish_fname (token->u.value);
3568 /* The `__builtin_va_arg' construct is used to handle
3569 `va_arg'. Consume the `__builtin_va_arg' token. */
3570 cp_lexer_consume_token (parser->lexer);
3571 /* Look for the opening `('. */
3572 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3573 /* Now, parse the assignment-expression. */
3574 expression = cp_parser_assignment_expression (parser,
3575 /*cast_p=*/false, NULL);
3576 /* Look for the `,'. */
3577 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3578 /* Parse the type-id. */
3579 type = cp_parser_type_id (parser);
3580 /* Look for the closing `)'. */
3581 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3582 /* Using `va_arg' in a constant-expression is not
3584 if (cp_parser_non_integral_constant_expression (parser,
3586 return error_mark_node;
3587 return build_x_va_arg (expression, type);
3591 return cp_parser_builtin_offsetof (parser);
3593 case RID_HAS_NOTHROW_ASSIGN:
3594 case RID_HAS_NOTHROW_CONSTRUCTOR:
3595 case RID_HAS_NOTHROW_COPY:
3596 case RID_HAS_TRIVIAL_ASSIGN:
3597 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3598 case RID_HAS_TRIVIAL_COPY:
3599 case RID_HAS_TRIVIAL_DESTRUCTOR:
3600 case RID_HAS_VIRTUAL_DESTRUCTOR:
3601 case RID_IS_ABSTRACT:
3602 case RID_IS_BASE_OF:
3604 case RID_IS_CONVERTIBLE_TO:
3607 case RID_IS_LITERAL_TYPE:
3609 case RID_IS_POLYMORPHIC:
3610 case RID_IS_STD_LAYOUT:
3611 case RID_IS_TRIVIAL:
3613 return cp_parser_trait_expr (parser, token->keyword);
3615 /* Objective-C++ expressions. */
3617 case RID_AT_PROTOCOL:
3618 case RID_AT_SELECTOR:
3619 return cp_parser_objc_expression (parser);
3622 if (parser->in_function_body
3623 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3626 error_at (token->location,
3627 "a template declaration cannot appear at block scope");
3628 cp_parser_skip_to_end_of_block_or_statement (parser);
3629 return error_mark_node;
3632 cp_parser_error (parser, "expected primary-expression");
3633 return error_mark_node;
3636 /* An id-expression can start with either an identifier, a
3637 `::' as the beginning of a qualified-id, or the "operator"
3641 case CPP_TEMPLATE_ID:
3642 case CPP_NESTED_NAME_SPECIFIER:
3646 const char *error_msg;
3649 cp_token *id_expr_token;
3652 /* Parse the id-expression. */
3654 = cp_parser_id_expression (parser,
3655 /*template_keyword_p=*/false,
3656 /*check_dependency_p=*/true,
3658 /*declarator_p=*/false,
3659 /*optional_p=*/false);
3660 if (id_expression == error_mark_node)
3661 return error_mark_node;
3662 id_expr_token = token;
3663 token = cp_lexer_peek_token (parser->lexer);
3664 done = (token->type != CPP_OPEN_SQUARE
3665 && token->type != CPP_OPEN_PAREN
3666 && token->type != CPP_DOT
3667 && token->type != CPP_DEREF
3668 && token->type != CPP_PLUS_PLUS
3669 && token->type != CPP_MINUS_MINUS);
3670 /* If we have a template-id, then no further lookup is
3671 required. If the template-id was for a template-class, we
3672 will sometimes have a TYPE_DECL at this point. */
3673 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3674 || TREE_CODE (id_expression) == TYPE_DECL)
3675 decl = id_expression;
3676 /* Look up the name. */
3679 tree ambiguous_decls;
3681 /* If we already know that this lookup is ambiguous, then
3682 we've already issued an error message; there's no reason
3684 if (id_expr_token->type == CPP_NAME
3685 && id_expr_token->ambiguous_p)
3687 cp_parser_simulate_error (parser);
3688 return error_mark_node;
3691 decl = cp_parser_lookup_name (parser, id_expression,
3694 /*is_namespace=*/false,
3695 /*check_dependency=*/true,
3697 id_expr_token->location);
3698 /* If the lookup was ambiguous, an error will already have
3700 if (ambiguous_decls)
3701 return error_mark_node;
3703 /* In Objective-C++, we may have an Objective-C 2.0
3704 dot-syntax for classes here. */
3705 if (c_dialect_objc ()
3706 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3707 && TREE_CODE (decl) == TYPE_DECL
3708 && objc_is_class_name (decl))
3711 cp_lexer_consume_token (parser->lexer);
3712 component = cp_parser_identifier (parser);
3713 if (component == error_mark_node)
3714 return error_mark_node;
3716 return objc_build_class_component_ref (id_expression, component);
3719 /* In Objective-C++, an instance variable (ivar) may be preferred
3720 to whatever cp_parser_lookup_name() found. */
3721 decl = objc_lookup_ivar (decl, id_expression);
3723 /* If name lookup gives us a SCOPE_REF, then the
3724 qualifying scope was dependent. */
3725 if (TREE_CODE (decl) == SCOPE_REF)
3727 /* At this point, we do not know if DECL is a valid
3728 integral constant expression. We assume that it is
3729 in fact such an expression, so that code like:
3731 template <int N> struct A {
3735 is accepted. At template-instantiation time, we
3736 will check that B<N>::i is actually a constant. */
3739 /* Check to see if DECL is a local variable in a context
3740 where that is forbidden. */
3741 if (parser->local_variables_forbidden_p
3742 && local_variable_p (decl))
3744 /* It might be that we only found DECL because we are
3745 trying to be generous with pre-ISO scoping rules.
3746 For example, consider:
3750 for (int i = 0; i < 10; ++i) {}
3751 extern void f(int j = i);
3754 Here, name look up will originally find the out
3755 of scope `i'. We need to issue a warning message,
3756 but then use the global `i'. */
3757 decl = check_for_out_of_scope_variable (decl);
3758 if (local_variable_p (decl))
3760 error_at (id_expr_token->location,
3761 "local variable %qD may not appear in this context",
3763 return error_mark_node;
3768 decl = (finish_id_expression
3769 (id_expression, decl, parser->scope,
3771 parser->integral_constant_expression_p,
3772 parser->allow_non_integral_constant_expression_p,
3773 &parser->non_integral_constant_expression_p,
3774 template_p, done, address_p,
3777 id_expr_token->location));
3779 cp_parser_error (parser, error_msg);
3783 /* Anything else is an error. */
3785 cp_parser_error (parser, "expected primary-expression");
3786 return error_mark_node;
3790 /* Parse an id-expression.
3797 :: [opt] nested-name-specifier template [opt] unqualified-id
3799 :: operator-function-id
3802 Return a representation of the unqualified portion of the
3803 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3804 a `::' or nested-name-specifier.
3806 Often, if the id-expression was a qualified-id, the caller will
3807 want to make a SCOPE_REF to represent the qualified-id. This
3808 function does not do this in order to avoid wastefully creating
3809 SCOPE_REFs when they are not required.
3811 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3814 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3815 uninstantiated templates.
3817 If *TEMPLATE_P is non-NULL, it is set to true iff the
3818 `template' keyword is used to explicitly indicate that the entity
3819 named is a template.
3821 If DECLARATOR_P is true, the id-expression is appearing as part of
3822 a declarator, rather than as part of an expression. */
3825 cp_parser_id_expression (cp_parser *parser,
3826 bool template_keyword_p,
3827 bool check_dependency_p,
3832 bool global_scope_p;
3833 bool nested_name_specifier_p;
3835 /* Assume the `template' keyword was not used. */
3837 *template_p = template_keyword_p;
3839 /* Look for the optional `::' operator. */
3841 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3843 /* Look for the optional nested-name-specifier. */
3844 nested_name_specifier_p
3845 = (cp_parser_nested_name_specifier_opt (parser,
3846 /*typename_keyword_p=*/false,
3851 /* If there is a nested-name-specifier, then we are looking at
3852 the first qualified-id production. */
3853 if (nested_name_specifier_p)
3856 tree saved_object_scope;
3857 tree saved_qualifying_scope;
3858 tree unqualified_id;
3861 /* See if the next token is the `template' keyword. */
3863 template_p = &is_template;
3864 *template_p = cp_parser_optional_template_keyword (parser);
3865 /* Name lookup we do during the processing of the
3866 unqualified-id might obliterate SCOPE. */
3867 saved_scope = parser->scope;
3868 saved_object_scope = parser->object_scope;
3869 saved_qualifying_scope = parser->qualifying_scope;
3870 /* Process the final unqualified-id. */
3871 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3874 /*optional_p=*/false);
3875 /* Restore the SAVED_SCOPE for our caller. */
3876 parser->scope = saved_scope;
3877 parser->object_scope = saved_object_scope;
3878 parser->qualifying_scope = saved_qualifying_scope;
3880 return unqualified_id;
3882 /* Otherwise, if we are in global scope, then we are looking at one
3883 of the other qualified-id productions. */
3884 else if (global_scope_p)
3889 /* Peek at the next token. */
3890 token = cp_lexer_peek_token (parser->lexer);
3892 /* If it's an identifier, and the next token is not a "<", then
3893 we can avoid the template-id case. This is an optimization
3894 for this common case. */
3895 if (token->type == CPP_NAME
3896 && !cp_parser_nth_token_starts_template_argument_list_p
3898 return cp_parser_identifier (parser);
3900 cp_parser_parse_tentatively (parser);
3901 /* Try a template-id. */
3902 id = cp_parser_template_id (parser,
3903 /*template_keyword_p=*/false,
3904 /*check_dependency_p=*/true,
3906 /* If that worked, we're done. */
3907 if (cp_parser_parse_definitely (parser))
3910 /* Peek at the next token. (Changes in the token buffer may
3911 have invalidated the pointer obtained above.) */
3912 token = cp_lexer_peek_token (parser->lexer);
3914 switch (token->type)
3917 return cp_parser_identifier (parser);
3920 if (token->keyword == RID_OPERATOR)
3921 return cp_parser_operator_function_id (parser);
3925 cp_parser_error (parser, "expected id-expression");
3926 return error_mark_node;
3930 return cp_parser_unqualified_id (parser, template_keyword_p,
3931 /*check_dependency_p=*/true,
3936 /* Parse an unqualified-id.
3940 operator-function-id
3941 conversion-function-id
3945 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3946 keyword, in a construct like `A::template ...'.
3948 Returns a representation of unqualified-id. For the `identifier'
3949 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3950 production a BIT_NOT_EXPR is returned; the operand of the
3951 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3952 other productions, see the documentation accompanying the
3953 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3954 names are looked up in uninstantiated templates. If DECLARATOR_P
3955 is true, the unqualified-id is appearing as part of a declarator,
3956 rather than as part of an expression. */
3959 cp_parser_unqualified_id (cp_parser* parser,
3960 bool template_keyword_p,
3961 bool check_dependency_p,
3967 /* Peek at the next token. */
3968 token = cp_lexer_peek_token (parser->lexer);
3970 switch (token->type)
3976 /* We don't know yet whether or not this will be a
3978 cp_parser_parse_tentatively (parser);
3979 /* Try a template-id. */
3980 id = cp_parser_template_id (parser, template_keyword_p,
3983 /* If it worked, we're done. */
3984 if (cp_parser_parse_definitely (parser))
3986 /* Otherwise, it's an ordinary identifier. */
3987 return cp_parser_identifier (parser);
3990 case CPP_TEMPLATE_ID:
3991 return cp_parser_template_id (parser, template_keyword_p,
3998 tree qualifying_scope;
4003 /* Consume the `~' token. */
4004 cp_lexer_consume_token (parser->lexer);
4005 /* Parse the class-name. The standard, as written, seems to
4008 template <typename T> struct S { ~S (); };
4009 template <typename T> S<T>::~S() {}
4011 is invalid, since `~' must be followed by a class-name, but
4012 `S<T>' is dependent, and so not known to be a class.
4013 That's not right; we need to look in uninstantiated
4014 templates. A further complication arises from:
4016 template <typename T> void f(T t) {
4020 Here, it is not possible to look up `T' in the scope of `T'
4021 itself. We must look in both the current scope, and the
4022 scope of the containing complete expression.
4024 Yet another issue is:
4033 The standard does not seem to say that the `S' in `~S'
4034 should refer to the type `S' and not the data member
4037 /* DR 244 says that we look up the name after the "~" in the
4038 same scope as we looked up the qualifying name. That idea
4039 isn't fully worked out; it's more complicated than that. */
4040 scope = parser->scope;
4041 object_scope = parser->object_scope;
4042 qualifying_scope = parser->qualifying_scope;
4044 /* Check for invalid scopes. */
4045 if (scope == error_mark_node)
4047 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4048 cp_lexer_consume_token (parser->lexer);
4049 return error_mark_node;
4051 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4053 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4054 error_at (token->location,
4055 "scope %qT before %<~%> is not a class-name",
4057 cp_parser_simulate_error (parser);
4058 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4059 cp_lexer_consume_token (parser->lexer);
4060 return error_mark_node;
4062 gcc_assert (!scope || TYPE_P (scope));
4064 /* If the name is of the form "X::~X" it's OK even if X is a
4066 token = cp_lexer_peek_token (parser->lexer);
4068 && token->type == CPP_NAME
4069 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4071 && (token->u.value == TYPE_IDENTIFIER (scope)
4072 || (CLASS_TYPE_P (scope)
4073 && constructor_name_p (token->u.value, scope))))
4075 cp_lexer_consume_token (parser->lexer);
4076 return build_nt (BIT_NOT_EXPR, scope);
4079 /* If there was an explicit qualification (S::~T), first look
4080 in the scope given by the qualification (i.e., S).
4082 Note: in the calls to cp_parser_class_name below we pass
4083 typename_type so that lookup finds the injected-class-name
4084 rather than the constructor. */
4086 type_decl = NULL_TREE;
4089 cp_parser_parse_tentatively (parser);
4090 type_decl = cp_parser_class_name (parser,
4091 /*typename_keyword_p=*/false,
4092 /*template_keyword_p=*/false,
4094 /*check_dependency=*/false,
4095 /*class_head_p=*/false,
4097 if (cp_parser_parse_definitely (parser))
4100 /* In "N::S::~S", look in "N" as well. */
4101 if (!done && scope && qualifying_scope)
4103 cp_parser_parse_tentatively (parser);
4104 parser->scope = qualifying_scope;
4105 parser->object_scope = NULL_TREE;
4106 parser->qualifying_scope = NULL_TREE;
4108 = cp_parser_class_name (parser,
4109 /*typename_keyword_p=*/false,
4110 /*template_keyword_p=*/false,
4112 /*check_dependency=*/false,
4113 /*class_head_p=*/false,
4115 if (cp_parser_parse_definitely (parser))
4118 /* In "p->S::~T", look in the scope given by "*p" as well. */
4119 else if (!done && object_scope)
4121 cp_parser_parse_tentatively (parser);
4122 parser->scope = object_scope;
4123 parser->object_scope = NULL_TREE;
4124 parser->qualifying_scope = NULL_TREE;
4126 = cp_parser_class_name (parser,
4127 /*typename_keyword_p=*/false,
4128 /*template_keyword_p=*/false,
4130 /*check_dependency=*/false,
4131 /*class_head_p=*/false,
4133 if (cp_parser_parse_definitely (parser))
4136 /* Look in the surrounding context. */
4139 parser->scope = NULL_TREE;
4140 parser->object_scope = NULL_TREE;
4141 parser->qualifying_scope = NULL_TREE;
4142 if (processing_template_decl)
4143 cp_parser_parse_tentatively (parser);
4145 = cp_parser_class_name (parser,
4146 /*typename_keyword_p=*/false,
4147 /*template_keyword_p=*/false,
4149 /*check_dependency=*/false,
4150 /*class_head_p=*/false,
4152 if (processing_template_decl
4153 && ! cp_parser_parse_definitely (parser))
4155 /* We couldn't find a type with this name, so just accept
4156 it and check for a match at instantiation time. */
4157 type_decl = cp_parser_identifier (parser);
4158 if (type_decl != error_mark_node)
4159 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4163 /* If an error occurred, assume that the name of the
4164 destructor is the same as the name of the qualifying
4165 class. That allows us to keep parsing after running
4166 into ill-formed destructor names. */
4167 if (type_decl == error_mark_node && scope)
4168 return build_nt (BIT_NOT_EXPR, scope);
4169 else if (type_decl == error_mark_node)
4170 return error_mark_node;
4172 /* Check that destructor name and scope match. */
4173 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4175 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4176 error_at (token->location,
4177 "declaration of %<~%T%> as member of %qT",
4179 cp_parser_simulate_error (parser);
4180 return error_mark_node;
4185 A typedef-name that names a class shall not be used as the
4186 identifier in the declarator for a destructor declaration. */
4188 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4189 && !DECL_SELF_REFERENCE_P (type_decl)
4190 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4191 error_at (token->location,
4192 "typedef-name %qD used as destructor declarator",
4195 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4199 if (token->keyword == RID_OPERATOR)
4203 /* This could be a template-id, so we try that first. */
4204 cp_parser_parse_tentatively (parser);
4205 /* Try a template-id. */
4206 id = cp_parser_template_id (parser, template_keyword_p,
4207 /*check_dependency_p=*/true,
4209 /* If that worked, we're done. */
4210 if (cp_parser_parse_definitely (parser))
4212 /* We still don't know whether we're looking at an
4213 operator-function-id or a conversion-function-id. */
4214 cp_parser_parse_tentatively (parser);
4215 /* Try an operator-function-id. */
4216 id = cp_parser_operator_function_id (parser);
4217 /* If that didn't work, try a conversion-function-id. */
4218 if (!cp_parser_parse_definitely (parser))
4219 id = cp_parser_conversion_function_id (parser);
4228 cp_parser_error (parser, "expected unqualified-id");
4229 return error_mark_node;
4233 /* Parse an (optional) nested-name-specifier.
4235 nested-name-specifier: [C++98]
4236 class-or-namespace-name :: nested-name-specifier [opt]
4237 class-or-namespace-name :: template nested-name-specifier [opt]
4239 nested-name-specifier: [C++0x]
4242 nested-name-specifier identifier ::
4243 nested-name-specifier template [opt] simple-template-id ::
4245 PARSER->SCOPE should be set appropriately before this function is
4246 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4247 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4250 Sets PARSER->SCOPE to the class (TYPE) or namespace
4251 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4252 it unchanged if there is no nested-name-specifier. Returns the new
4253 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4255 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4256 part of a declaration and/or decl-specifier. */
4259 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4260 bool typename_keyword_p,
4261 bool check_dependency_p,
4263 bool is_declaration)
4265 bool success = false;
4266 cp_token_position start = 0;
4269 /* Remember where the nested-name-specifier starts. */
4270 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4272 start = cp_lexer_token_position (parser->lexer, false);
4273 push_deferring_access_checks (dk_deferred);
4280 tree saved_qualifying_scope;
4281 bool template_keyword_p;
4283 /* Spot cases that cannot be the beginning of a
4284 nested-name-specifier. */
4285 token = cp_lexer_peek_token (parser->lexer);
4287 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4288 the already parsed nested-name-specifier. */
4289 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4291 /* Grab the nested-name-specifier and continue the loop. */
4292 cp_parser_pre_parsed_nested_name_specifier (parser);
4293 /* If we originally encountered this nested-name-specifier
4294 with IS_DECLARATION set to false, we will not have
4295 resolved TYPENAME_TYPEs, so we must do so here. */
4297 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4299 new_scope = resolve_typename_type (parser->scope,
4300 /*only_current_p=*/false);
4301 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4302 parser->scope = new_scope;
4308 /* Spot cases that cannot be the beginning of a
4309 nested-name-specifier. On the second and subsequent times
4310 through the loop, we look for the `template' keyword. */
4311 if (success && token->keyword == RID_TEMPLATE)
4313 /* A template-id can start a nested-name-specifier. */
4314 else if (token->type == CPP_TEMPLATE_ID)
4318 /* If the next token is not an identifier, then it is
4319 definitely not a type-name or namespace-name. */
4320 if (token->type != CPP_NAME)
4322 /* If the following token is neither a `<' (to begin a
4323 template-id), nor a `::', then we are not looking at a
4324 nested-name-specifier. */
4325 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4327 if (token->type == CPP_COLON
4328 && parser->colon_corrects_to_scope_p
4329 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4331 error_at (token->location,
4332 "found %<:%> in nested-name-specifier, expected %<::%>");
4333 token->type = CPP_SCOPE;
4336 if (token->type != CPP_SCOPE
4337 && !cp_parser_nth_token_starts_template_argument_list_p
4342 /* The nested-name-specifier is optional, so we parse
4344 cp_parser_parse_tentatively (parser);
4346 /* Look for the optional `template' keyword, if this isn't the
4347 first time through the loop. */
4349 template_keyword_p = cp_parser_optional_template_keyword (parser);
4351 template_keyword_p = false;
4353 /* Save the old scope since the name lookup we are about to do
4354 might destroy it. */
4355 old_scope = parser->scope;
4356 saved_qualifying_scope = parser->qualifying_scope;
4357 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4358 look up names in "X<T>::I" in order to determine that "Y" is
4359 a template. So, if we have a typename at this point, we make
4360 an effort to look through it. */
4362 && !typename_keyword_p
4364 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4365 parser->scope = resolve_typename_type (parser->scope,
4366 /*only_current_p=*/false);
4367 /* Parse the qualifying entity. */
4369 = cp_parser_qualifying_entity (parser,
4375 /* Look for the `::' token. */
4376 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4378 /* If we found what we wanted, we keep going; otherwise, we're
4380 if (!cp_parser_parse_definitely (parser))
4382 bool error_p = false;
4384 /* Restore the OLD_SCOPE since it was valid before the
4385 failed attempt at finding the last
4386 class-or-namespace-name. */
4387 parser->scope = old_scope;
4388 parser->qualifying_scope = saved_qualifying_scope;
4389 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4391 /* If the next token is an identifier, and the one after
4392 that is a `::', then any valid interpretation would have
4393 found a class-or-namespace-name. */
4394 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4395 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4397 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4400 token = cp_lexer_consume_token (parser->lexer);
4403 if (!token->ambiguous_p)
4406 tree ambiguous_decls;
4408 decl = cp_parser_lookup_name (parser, token->u.value,
4410 /*is_template=*/false,
4411 /*is_namespace=*/false,
4412 /*check_dependency=*/true,
4415 if (TREE_CODE (decl) == TEMPLATE_DECL)
4416 error_at (token->location,
4417 "%qD used without template parameters",
4419 else if (ambiguous_decls)
4421 error_at (token->location,
4422 "reference to %qD is ambiguous",
4424 print_candidates (ambiguous_decls);
4425 decl = error_mark_node;
4429 if (cxx_dialect != cxx98)
4430 cp_parser_name_lookup_error
4431 (parser, token->u.value, decl, NLE_NOT_CXX98,
4434 cp_parser_name_lookup_error
4435 (parser, token->u.value, decl, NLE_CXX98,
4439 parser->scope = error_mark_node;
4441 /* Treat this as a successful nested-name-specifier
4446 If the name found is not a class-name (clause
4447 _class_) or namespace-name (_namespace.def_), the
4448 program is ill-formed. */
4451 cp_lexer_consume_token (parser->lexer);
4455 /* We've found one valid nested-name-specifier. */
4457 /* Name lookup always gives us a DECL. */
4458 if (TREE_CODE (new_scope) == TYPE_DECL)
4459 new_scope = TREE_TYPE (new_scope);
4460 /* Uses of "template" must be followed by actual templates. */
4461 if (template_keyword_p
4462 && !(CLASS_TYPE_P (new_scope)
4463 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4464 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4465 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4466 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4467 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4468 == TEMPLATE_ID_EXPR)))
4469 permerror (input_location, TYPE_P (new_scope)
4470 ? "%qT is not a template"
4471 : "%qD is not a template",
4473 /* If it is a class scope, try to complete it; we are about to
4474 be looking up names inside the class. */
4475 if (TYPE_P (new_scope)
4476 /* Since checking types for dependency can be expensive,
4477 avoid doing it if the type is already complete. */
4478 && !COMPLETE_TYPE_P (new_scope)
4479 /* Do not try to complete dependent types. */
4480 && !dependent_type_p (new_scope))
4482 new_scope = complete_type (new_scope);
4483 /* If it is a typedef to current class, use the current
4484 class instead, as the typedef won't have any names inside
4486 if (!COMPLETE_TYPE_P (new_scope)
4487 && currently_open_class (new_scope))
4488 new_scope = TYPE_MAIN_VARIANT (new_scope);
4490 /* Make sure we look in the right scope the next time through
4492 parser->scope = new_scope;
4495 /* If parsing tentatively, replace the sequence of tokens that makes
4496 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4497 token. That way, should we re-parse the token stream, we will
4498 not have to repeat the effort required to do the parse, nor will
4499 we issue duplicate error messages. */
4500 if (success && start)
4504 token = cp_lexer_token_at (parser->lexer, start);
4505 /* Reset the contents of the START token. */
4506 token->type = CPP_NESTED_NAME_SPECIFIER;
4507 /* Retrieve any deferred checks. Do not pop this access checks yet
4508 so the memory will not be reclaimed during token replacing below. */
4509 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4510 token->u.tree_check_value->value = parser->scope;
4511 token->u.tree_check_value->checks = get_deferred_access_checks ();
4512 token->u.tree_check_value->qualifying_scope =
4513 parser->qualifying_scope;
4514 token->keyword = RID_MAX;
4516 /* Purge all subsequent tokens. */
4517 cp_lexer_purge_tokens_after (parser->lexer, start);
4521 pop_to_parent_deferring_access_checks ();
4523 return success ? parser->scope : NULL_TREE;
4526 /* Parse a nested-name-specifier. See
4527 cp_parser_nested_name_specifier_opt for details. This function
4528 behaves identically, except that it will an issue an error if no
4529 nested-name-specifier is present. */
4532 cp_parser_nested_name_specifier (cp_parser *parser,
4533 bool typename_keyword_p,
4534 bool check_dependency_p,
4536 bool is_declaration)
4540 /* Look for the nested-name-specifier. */
4541 scope = cp_parser_nested_name_specifier_opt (parser,
4546 /* If it was not present, issue an error message. */
4549 cp_parser_error (parser, "expected nested-name-specifier");
4550 parser->scope = NULL_TREE;
4556 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4557 this is either a class-name or a namespace-name (which corresponds
4558 to the class-or-namespace-name production in the grammar). For
4559 C++0x, it can also be a type-name that refers to an enumeration
4562 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4563 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4564 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4565 TYPE_P is TRUE iff the next name should be taken as a class-name,
4566 even the same name is declared to be another entity in the same
4569 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4570 specified by the class-or-namespace-name. If neither is found the
4571 ERROR_MARK_NODE is returned. */
4574 cp_parser_qualifying_entity (cp_parser *parser,
4575 bool typename_keyword_p,
4576 bool template_keyword_p,
4577 bool check_dependency_p,
4579 bool is_declaration)
4582 tree saved_qualifying_scope;
4583 tree saved_object_scope;
4586 bool successful_parse_p;
4588 /* Before we try to parse the class-name, we must save away the
4589 current PARSER->SCOPE since cp_parser_class_name will destroy
4591 saved_scope = parser->scope;
4592 saved_qualifying_scope = parser->qualifying_scope;
4593 saved_object_scope = parser->object_scope;
4594 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4595 there is no need to look for a namespace-name. */
4596 only_class_p = template_keyword_p
4597 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4599 cp_parser_parse_tentatively (parser);
4600 scope = cp_parser_class_name (parser,
4603 type_p ? class_type : none_type,
4605 /*class_head_p=*/false,
4607 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4608 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4610 && cxx_dialect != cxx98
4611 && !successful_parse_p)
4613 /* Restore the saved scope. */
4614 parser->scope = saved_scope;
4615 parser->qualifying_scope = saved_qualifying_scope;
4616 parser->object_scope = saved_object_scope;
4618 /* Parse tentatively. */
4619 cp_parser_parse_tentatively (parser);
4621 /* Parse a typedef-name or enum-name. */
4622 scope = cp_parser_nonclass_name (parser);
4624 /* "If the name found does not designate a namespace or a class,
4625 enumeration, or dependent type, the program is ill-formed."
4627 We cover classes and dependent types above and namespaces below,
4628 so this code is only looking for enums. */
4629 if (!scope || TREE_CODE (scope) != TYPE_DECL
4630 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4631 cp_parser_simulate_error (parser);
4633 successful_parse_p = cp_parser_parse_definitely (parser);
4635 /* If that didn't work, try for a namespace-name. */
4636 if (!only_class_p && !successful_parse_p)
4638 /* Restore the saved scope. */
4639 parser->scope = saved_scope;
4640 parser->qualifying_scope = saved_qualifying_scope;
4641 parser->object_scope = saved_object_scope;
4642 /* If we are not looking at an identifier followed by the scope
4643 resolution operator, then this is not part of a
4644 nested-name-specifier. (Note that this function is only used
4645 to parse the components of a nested-name-specifier.) */
4646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4647 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4648 return error_mark_node;
4649 scope = cp_parser_namespace_name (parser);
4655 /* Parse a postfix-expression.
4659 postfix-expression [ expression ]
4660 postfix-expression ( expression-list [opt] )
4661 simple-type-specifier ( expression-list [opt] )
4662 typename :: [opt] nested-name-specifier identifier
4663 ( expression-list [opt] )
4664 typename :: [opt] nested-name-specifier template [opt] template-id
4665 ( expression-list [opt] )
4666 postfix-expression . template [opt] id-expression
4667 postfix-expression -> template [opt] id-expression
4668 postfix-expression . pseudo-destructor-name
4669 postfix-expression -> pseudo-destructor-name
4670 postfix-expression ++
4671 postfix-expression --
4672 dynamic_cast < type-id > ( expression )
4673 static_cast < type-id > ( expression )
4674 reinterpret_cast < type-id > ( expression )
4675 const_cast < type-id > ( expression )
4676 typeid ( expression )
4682 ( type-id ) { initializer-list , [opt] }
4684 This extension is a GNU version of the C99 compound-literal
4685 construct. (The C99 grammar uses `type-name' instead of `type-id',
4686 but they are essentially the same concept.)
4688 If ADDRESS_P is true, the postfix expression is the operand of the
4689 `&' operator. CAST_P is true if this expression is the target of a
4692 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4693 class member access expressions [expr.ref].
4695 Returns a representation of the expression. */
4698 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4699 bool member_access_only_p,
4700 cp_id_kind * pidk_return)
4704 cp_id_kind idk = CP_ID_KIND_NONE;
4705 tree postfix_expression = NULL_TREE;
4706 bool is_member_access = false;
4708 /* Peek at the next token. */
4709 token = cp_lexer_peek_token (parser->lexer);
4710 /* Some of the productions are determined by keywords. */
4711 keyword = token->keyword;
4721 const char *saved_message;
4723 /* All of these can be handled in the same way from the point
4724 of view of parsing. Begin by consuming the token
4725 identifying the cast. */
4726 cp_lexer_consume_token (parser->lexer);
4728 /* New types cannot be defined in the cast. */
4729 saved_message = parser->type_definition_forbidden_message;
4730 parser->type_definition_forbidden_message
4731 = G_("types may not be defined in casts");
4733 /* Look for the opening `<'. */
4734 cp_parser_require (parser, CPP_LESS, RT_LESS);
4735 /* Parse the type to which we are casting. */
4736 type = cp_parser_type_id (parser);
4737 /* Look for the closing `>'. */
4738 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4739 /* Restore the old message. */
4740 parser->type_definition_forbidden_message = saved_message;
4742 /* And the expression which is being cast. */
4743 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4744 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4745 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4747 /* Only type conversions to integral or enumeration types
4748 can be used in constant-expressions. */
4749 if (!cast_valid_in_integral_constant_expression_p (type)
4750 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4751 return error_mark_node;
4757 = build_dynamic_cast (type, expression, tf_warning_or_error);
4761 = build_static_cast (type, expression, tf_warning_or_error);
4765 = build_reinterpret_cast (type, expression,
4766 tf_warning_or_error);
4770 = build_const_cast (type, expression, tf_warning_or_error);
4781 const char *saved_message;
4782 bool saved_in_type_id_in_expr_p;
4784 /* Consume the `typeid' token. */
4785 cp_lexer_consume_token (parser->lexer);
4786 /* Look for the `(' token. */
4787 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4788 /* Types cannot be defined in a `typeid' expression. */
4789 saved_message = parser->type_definition_forbidden_message;
4790 parser->type_definition_forbidden_message
4791 = G_("types may not be defined in a %<typeid%> expression");
4792 /* We can't be sure yet whether we're looking at a type-id or an
4794 cp_parser_parse_tentatively (parser);
4795 /* Try a type-id first. */
4796 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4797 parser->in_type_id_in_expr_p = true;
4798 type = cp_parser_type_id (parser);
4799 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4800 /* Look for the `)' token. Otherwise, we can't be sure that
4801 we're not looking at an expression: consider `typeid (int
4802 (3))', for example. */
4803 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4804 /* If all went well, simply lookup the type-id. */
4805 if (cp_parser_parse_definitely (parser))
4806 postfix_expression = get_typeid (type);
4807 /* Otherwise, fall back to the expression variant. */
4812 /* Look for an expression. */
4813 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4814 /* Compute its typeid. */
4815 postfix_expression = build_typeid (expression);
4816 /* Look for the `)' token. */
4817 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4819 /* Restore the saved message. */
4820 parser->type_definition_forbidden_message = saved_message;
4821 /* `typeid' may not appear in an integral constant expression. */
4822 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4823 return error_mark_node;
4830 /* The syntax permitted here is the same permitted for an
4831 elaborated-type-specifier. */
4832 type = cp_parser_elaborated_type_specifier (parser,
4833 /*is_friend=*/false,
4834 /*is_declaration=*/false);
4835 postfix_expression = cp_parser_functional_cast (parser, type);
4843 /* If the next thing is a simple-type-specifier, we may be
4844 looking at a functional cast. We could also be looking at
4845 an id-expression. So, we try the functional cast, and if
4846 that doesn't work we fall back to the primary-expression. */
4847 cp_parser_parse_tentatively (parser);
4848 /* Look for the simple-type-specifier. */
4849 type = cp_parser_simple_type_specifier (parser,
4850 /*decl_specs=*/NULL,
4851 CP_PARSER_FLAGS_NONE);
4852 /* Parse the cast itself. */
4853 if (!cp_parser_error_occurred (parser))
4855 = cp_parser_functional_cast (parser, type);
4856 /* If that worked, we're done. */
4857 if (cp_parser_parse_definitely (parser))
4860 /* If the functional-cast didn't work out, try a
4861 compound-literal. */
4862 if (cp_parser_allow_gnu_extensions_p (parser)
4863 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4865 VEC(constructor_elt,gc) *initializer_list = NULL;
4866 bool saved_in_type_id_in_expr_p;
4868 cp_parser_parse_tentatively (parser);
4869 /* Consume the `('. */
4870 cp_lexer_consume_token (parser->lexer);
4871 /* Parse the type. */
4872 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4873 parser->in_type_id_in_expr_p = true;
4874 type = cp_parser_type_id (parser);
4875 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4876 /* Look for the `)'. */
4877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4878 /* Look for the `{'. */
4879 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4880 /* If things aren't going well, there's no need to
4882 if (!cp_parser_error_occurred (parser))
4884 bool non_constant_p;
4885 /* Parse the initializer-list. */
4887 = cp_parser_initializer_list (parser, &non_constant_p);
4888 /* Allow a trailing `,'. */
4889 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4890 cp_lexer_consume_token (parser->lexer);
4891 /* Look for the final `}'. */
4892 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4894 /* If that worked, we're definitely looking at a
4895 compound-literal expression. */
4896 if (cp_parser_parse_definitely (parser))
4898 /* Warn the user that a compound literal is not
4899 allowed in standard C++. */
4900 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4901 /* For simplicity, we disallow compound literals in
4902 constant-expressions. We could
4903 allow compound literals of integer type, whose
4904 initializer was a constant, in constant
4905 expressions. Permitting that usage, as a further
4906 extension, would not change the meaning of any
4907 currently accepted programs. (Of course, as
4908 compound literals are not part of ISO C++, the
4909 standard has nothing to say.) */
4910 if (cp_parser_non_integral_constant_expression (parser,
4913 postfix_expression = error_mark_node;
4916 /* Form the representation of the compound-literal. */
4918 = (finish_compound_literal
4919 (type, build_constructor (init_list_type_node,
4921 tf_warning_or_error));
4926 /* It must be a primary-expression. */
4928 = cp_parser_primary_expression (parser, address_p, cast_p,
4929 /*template_arg_p=*/false,
4935 /* Keep looping until the postfix-expression is complete. */
4938 if (idk == CP_ID_KIND_UNQUALIFIED
4939 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4940 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4941 /* It is not a Koenig lookup function call. */
4943 = unqualified_name_lookup_error (postfix_expression);
4945 /* Peek at the next token. */
4946 token = cp_lexer_peek_token (parser->lexer);
4948 switch (token->type)
4950 case CPP_OPEN_SQUARE:
4952 = cp_parser_postfix_open_square_expression (parser,
4955 idk = CP_ID_KIND_NONE;
4956 is_member_access = false;
4959 case CPP_OPEN_PAREN:
4960 /* postfix-expression ( expression-list [opt] ) */
4963 bool is_builtin_constant_p;
4964 bool saved_integral_constant_expression_p = false;
4965 bool saved_non_integral_constant_expression_p = false;
4968 is_member_access = false;
4970 is_builtin_constant_p
4971 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4972 if (is_builtin_constant_p)
4974 /* The whole point of __builtin_constant_p is to allow
4975 non-constant expressions to appear as arguments. */
4976 saved_integral_constant_expression_p
4977 = parser->integral_constant_expression_p;
4978 saved_non_integral_constant_expression_p
4979 = parser->non_integral_constant_expression_p;
4980 parser->integral_constant_expression_p = false;
4982 args = (cp_parser_parenthesized_expression_list
4984 /*cast_p=*/false, /*allow_expansion_p=*/true,
4985 /*non_constant_p=*/NULL));
4986 if (is_builtin_constant_p)
4988 parser->integral_constant_expression_p
4989 = saved_integral_constant_expression_p;
4990 parser->non_integral_constant_expression_p
4991 = saved_non_integral_constant_expression_p;
4996 postfix_expression = error_mark_node;
5000 /* Function calls are not permitted in
5001 constant-expressions. */
5002 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5003 && cp_parser_non_integral_constant_expression (parser,
5006 postfix_expression = error_mark_node;
5007 release_tree_vector (args);
5012 if (idk == CP_ID_KIND_UNQUALIFIED
5013 || idk == CP_ID_KIND_TEMPLATE_ID)
5015 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5017 if (!VEC_empty (tree, args))
5020 if (!any_type_dependent_arguments_p (args))
5022 = perform_koenig_lookup (postfix_expression, args,
5023 /*include_std=*/false,
5024 tf_warning_or_error);
5028 = unqualified_fn_lookup_error (postfix_expression);
5030 /* We do not perform argument-dependent lookup if
5031 normal lookup finds a non-function, in accordance
5032 with the expected resolution of DR 218. */
5033 else if (!VEC_empty (tree, args)
5034 && is_overloaded_fn (postfix_expression))
5036 tree fn = get_first_fn (postfix_expression);
5037 fn = STRIP_TEMPLATE (fn);
5039 /* Do not do argument dependent lookup if regular
5040 lookup finds a member function or a block-scope
5041 function declaration. [basic.lookup.argdep]/3 */
5042 if (!DECL_FUNCTION_MEMBER_P (fn)
5043 && !DECL_LOCAL_FUNCTION_P (fn))
5046 if (!any_type_dependent_arguments_p (args))
5048 = perform_koenig_lookup (postfix_expression, args,
5049 /*include_std=*/false,
5050 tf_warning_or_error);
5055 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5057 tree instance = TREE_OPERAND (postfix_expression, 0);
5058 tree fn = TREE_OPERAND (postfix_expression, 1);
5060 if (processing_template_decl
5061 && (type_dependent_expression_p (instance)
5062 || (!BASELINK_P (fn)
5063 && TREE_CODE (fn) != FIELD_DECL)
5064 || type_dependent_expression_p (fn)
5065 || any_type_dependent_arguments_p (args)))
5068 = build_nt_call_vec (postfix_expression, args);
5069 release_tree_vector (args);
5073 if (BASELINK_P (fn))
5076 = (build_new_method_call
5077 (instance, fn, &args, NULL_TREE,
5078 (idk == CP_ID_KIND_QUALIFIED
5079 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5082 tf_warning_or_error));
5086 = finish_call_expr (postfix_expression, &args,
5087 /*disallow_virtual=*/false,
5089 tf_warning_or_error);
5091 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5092 || TREE_CODE (postfix_expression) == MEMBER_REF
5093 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5094 postfix_expression = (build_offset_ref_call_from_tree
5095 (postfix_expression, &args));
5096 else if (idk == CP_ID_KIND_QUALIFIED)
5097 /* A call to a static class member, or a namespace-scope
5100 = finish_call_expr (postfix_expression, &args,
5101 /*disallow_virtual=*/true,
5103 tf_warning_or_error);
5105 /* All other function calls. */
5107 = finish_call_expr (postfix_expression, &args,
5108 /*disallow_virtual=*/false,
5110 tf_warning_or_error);
5112 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5113 idk = CP_ID_KIND_NONE;
5115 release_tree_vector (args);
5121 /* postfix-expression . template [opt] id-expression
5122 postfix-expression . pseudo-destructor-name
5123 postfix-expression -> template [opt] id-expression
5124 postfix-expression -> pseudo-destructor-name */
5126 /* Consume the `.' or `->' operator. */
5127 cp_lexer_consume_token (parser->lexer);
5130 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5135 is_member_access = true;
5139 /* postfix-expression ++ */
5140 /* Consume the `++' token. */
5141 cp_lexer_consume_token (parser->lexer);
5142 /* Generate a representation for the complete expression. */
5144 = finish_increment_expr (postfix_expression,
5145 POSTINCREMENT_EXPR);
5146 /* Increments may not appear in constant-expressions. */
5147 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5148 postfix_expression = error_mark_node;
5149 idk = CP_ID_KIND_NONE;
5150 is_member_access = false;
5153 case CPP_MINUS_MINUS:
5154 /* postfix-expression -- */
5155 /* Consume the `--' token. */
5156 cp_lexer_consume_token (parser->lexer);
5157 /* Generate a representation for the complete expression. */
5159 = finish_increment_expr (postfix_expression,
5160 POSTDECREMENT_EXPR);
5161 /* Decrements may not appear in constant-expressions. */
5162 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5163 postfix_expression = error_mark_node;
5164 idk = CP_ID_KIND_NONE;
5165 is_member_access = false;
5169 if (pidk_return != NULL)
5170 * pidk_return = idk;
5171 if (member_access_only_p)
5172 return is_member_access? postfix_expression : error_mark_node;
5174 return postfix_expression;
5178 /* We should never get here. */
5180 return error_mark_node;
5183 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5184 by cp_parser_builtin_offsetof. We're looking for
5186 postfix-expression [ expression ]
5188 FOR_OFFSETOF is set if we're being called in that context, which
5189 changes how we deal with integer constant expressions. */
5192 cp_parser_postfix_open_square_expression (cp_parser *parser,
5193 tree postfix_expression,
5198 /* Consume the `[' token. */
5199 cp_lexer_consume_token (parser->lexer);
5201 /* Parse the index expression. */
5202 /* ??? For offsetof, there is a question of what to allow here. If
5203 offsetof is not being used in an integral constant expression context,
5204 then we *could* get the right answer by computing the value at runtime.
5205 If we are in an integral constant expression context, then we might
5206 could accept any constant expression; hard to say without analysis.
5207 Rather than open the barn door too wide right away, allow only integer
5208 constant expressions here. */
5210 index = cp_parser_constant_expression (parser, false, NULL);
5212 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5214 /* Look for the closing `]'. */
5215 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5217 /* Build the ARRAY_REF. */
5218 postfix_expression = grok_array_decl (postfix_expression, index);
5220 /* When not doing offsetof, array references are not permitted in
5221 constant-expressions. */
5223 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5224 postfix_expression = error_mark_node;
5226 return postfix_expression;
5229 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5230 by cp_parser_builtin_offsetof. We're looking for
5232 postfix-expression . template [opt] id-expression
5233 postfix-expression . pseudo-destructor-name
5234 postfix-expression -> template [opt] id-expression
5235 postfix-expression -> pseudo-destructor-name
5237 FOR_OFFSETOF is set if we're being called in that context. That sorta
5238 limits what of the above we'll actually accept, but nevermind.
5239 TOKEN_TYPE is the "." or "->" token, which will already have been
5240 removed from the stream. */
5243 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5244 enum cpp_ttype token_type,
5245 tree postfix_expression,
5246 bool for_offsetof, cp_id_kind *idk,
5247 location_t location)
5251 bool pseudo_destructor_p;
5252 tree scope = NULL_TREE;
5254 /* If this is a `->' operator, dereference the pointer. */
5255 if (token_type == CPP_DEREF)
5256 postfix_expression = build_x_arrow (postfix_expression);
5257 /* Check to see whether or not the expression is type-dependent. */
5258 dependent_p = type_dependent_expression_p (postfix_expression);
5259 /* The identifier following the `->' or `.' is not qualified. */
5260 parser->scope = NULL_TREE;
5261 parser->qualifying_scope = NULL_TREE;
5262 parser->object_scope = NULL_TREE;
5263 *idk = CP_ID_KIND_NONE;
5265 /* Enter the scope corresponding to the type of the object
5266 given by the POSTFIX_EXPRESSION. */
5267 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5269 scope = TREE_TYPE (postfix_expression);
5270 /* According to the standard, no expression should ever have
5271 reference type. Unfortunately, we do not currently match
5272 the standard in this respect in that our internal representation
5273 of an expression may have reference type even when the standard
5274 says it does not. Therefore, we have to manually obtain the
5275 underlying type here. */
5276 scope = non_reference (scope);
5277 /* The type of the POSTFIX_EXPRESSION must be complete. */
5278 if (scope == unknown_type_node)
5280 error_at (location, "%qE does not have class type",
5281 postfix_expression);
5285 scope = complete_type_or_else (scope, NULL_TREE);
5286 /* Let the name lookup machinery know that we are processing a
5287 class member access expression. */
5288 parser->context->object_type = scope;
5289 /* If something went wrong, we want to be able to discern that case,
5290 as opposed to the case where there was no SCOPE due to the type
5291 of expression being dependent. */
5293 scope = error_mark_node;
5294 /* If the SCOPE was erroneous, make the various semantic analysis
5295 functions exit quickly -- and without issuing additional error
5297 if (scope == error_mark_node)
5298 postfix_expression = error_mark_node;
5301 /* Assume this expression is not a pseudo-destructor access. */
5302 pseudo_destructor_p = false;
5304 /* If the SCOPE is a scalar type, then, if this is a valid program,
5305 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5306 is type dependent, it can be pseudo-destructor-name or something else.
5307 Try to parse it as pseudo-destructor-name first. */
5308 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5313 cp_parser_parse_tentatively (parser);
5314 /* Parse the pseudo-destructor-name. */
5316 cp_parser_pseudo_destructor_name (parser, &s, &type);
5318 && (cp_parser_error_occurred (parser)
5319 || TREE_CODE (type) != TYPE_DECL
5320 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5321 cp_parser_abort_tentative_parse (parser);
5322 else if (cp_parser_parse_definitely (parser))
5324 pseudo_destructor_p = true;
5326 = finish_pseudo_destructor_expr (postfix_expression,
5327 s, TREE_TYPE (type));
5331 if (!pseudo_destructor_p)
5333 /* If the SCOPE is not a scalar type, we are looking at an
5334 ordinary class member access expression, rather than a
5335 pseudo-destructor-name. */
5337 cp_token *token = cp_lexer_peek_token (parser->lexer);
5338 /* Parse the id-expression. */
5339 name = (cp_parser_id_expression
5341 cp_parser_optional_template_keyword (parser),
5342 /*check_dependency_p=*/true,
5344 /*declarator_p=*/false,
5345 /*optional_p=*/false));
5346 /* In general, build a SCOPE_REF if the member name is qualified.
5347 However, if the name was not dependent and has already been
5348 resolved; there is no need to build the SCOPE_REF. For example;
5350 struct X { void f(); };
5351 template <typename T> void f(T* t) { t->X::f(); }
5353 Even though "t" is dependent, "X::f" is not and has been resolved
5354 to a BASELINK; there is no need to include scope information. */
5356 /* But we do need to remember that there was an explicit scope for
5357 virtual function calls. */
5359 *idk = CP_ID_KIND_QUALIFIED;
5361 /* If the name is a template-id that names a type, we will get a
5362 TYPE_DECL here. That is invalid code. */
5363 if (TREE_CODE (name) == TYPE_DECL)
5365 error_at (token->location, "invalid use of %qD", name);
5366 postfix_expression = error_mark_node;
5370 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5372 name = build_qualified_name (/*type=*/NULL_TREE,
5376 parser->scope = NULL_TREE;
5377 parser->qualifying_scope = NULL_TREE;
5378 parser->object_scope = NULL_TREE;
5380 if (scope && name && BASELINK_P (name))
5381 adjust_result_of_qualified_name_lookup
5382 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5384 = finish_class_member_access_expr (postfix_expression, name,
5386 tf_warning_or_error);
5390 /* We no longer need to look up names in the scope of the object on
5391 the left-hand side of the `.' or `->' operator. */
5392 parser->context->object_type = NULL_TREE;
5394 /* Outside of offsetof, these operators may not appear in
5395 constant-expressions. */
5397 && (cp_parser_non_integral_constant_expression
5398 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5399 postfix_expression = error_mark_node;
5401 return postfix_expression;
5404 /* Parse a parenthesized expression-list.
5407 assignment-expression
5408 expression-list, assignment-expression
5413 identifier, expression-list
5415 CAST_P is true if this expression is the target of a cast.
5417 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5420 Returns a vector of trees. Each element is a representation of an
5421 assignment-expression. NULL is returned if the ( and or ) are
5422 missing. An empty, but allocated, vector is returned on no
5423 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5424 if we are parsing an attribute list for an attribute that wants a
5425 plain identifier argument, normal_attr for an attribute that wants
5426 an expression, or non_attr if we aren't parsing an attribute list. If
5427 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5428 not all of the expressions in the list were constant. */
5430 static VEC(tree,gc) *
5431 cp_parser_parenthesized_expression_list (cp_parser* parser,
5432 int is_attribute_list,
5434 bool allow_expansion_p,
5435 bool *non_constant_p)
5437 VEC(tree,gc) *expression_list;
5438 bool fold_expr_p = is_attribute_list != non_attr;
5439 tree identifier = NULL_TREE;
5440 bool saved_greater_than_is_operator_p;
5442 /* Assume all the expressions will be constant. */
5444 *non_constant_p = false;
5446 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5449 expression_list = make_tree_vector ();
5451 /* Within a parenthesized expression, a `>' token is always
5452 the greater-than operator. */
5453 saved_greater_than_is_operator_p
5454 = parser->greater_than_is_operator_p;
5455 parser->greater_than_is_operator_p = true;
5457 /* Consume expressions until there are no more. */
5458 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5463 /* At the beginning of attribute lists, check to see if the
5464 next token is an identifier. */
5465 if (is_attribute_list == id_attr
5466 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5470 /* Consume the identifier. */
5471 token = cp_lexer_consume_token (parser->lexer);
5472 /* Save the identifier. */
5473 identifier = token->u.value;
5477 bool expr_non_constant_p;
5479 /* Parse the next assignment-expression. */
5480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5482 /* A braced-init-list. */
5483 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5484 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5485 if (non_constant_p && expr_non_constant_p)
5486 *non_constant_p = true;
5488 else if (non_constant_p)
5490 expr = (cp_parser_constant_expression
5491 (parser, /*allow_non_constant_p=*/true,
5492 &expr_non_constant_p));
5493 if (expr_non_constant_p)
5494 *non_constant_p = true;
5497 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5500 expr = fold_non_dependent_expr (expr);
5502 /* If we have an ellipsis, then this is an expression
5504 if (allow_expansion_p
5505 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5507 /* Consume the `...'. */
5508 cp_lexer_consume_token (parser->lexer);
5510 /* Build the argument pack. */
5511 expr = make_pack_expansion (expr);
5514 /* Add it to the list. We add error_mark_node
5515 expressions to the list, so that we can still tell if
5516 the correct form for a parenthesized expression-list
5517 is found. That gives better errors. */
5518 VEC_safe_push (tree, gc, expression_list, expr);
5520 if (expr == error_mark_node)
5524 /* After the first item, attribute lists look the same as
5525 expression lists. */
5526 is_attribute_list = non_attr;
5529 /* If the next token isn't a `,', then we are done. */
5530 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5533 /* Otherwise, consume the `,' and keep going. */
5534 cp_lexer_consume_token (parser->lexer);
5537 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5542 /* We try and resync to an unnested comma, as that will give the
5543 user better diagnostics. */
5544 ending = cp_parser_skip_to_closing_parenthesis (parser,
5545 /*recovering=*/true,
5547 /*consume_paren=*/true);
5552 parser->greater_than_is_operator_p
5553 = saved_greater_than_is_operator_p;
5558 parser->greater_than_is_operator_p
5559 = saved_greater_than_is_operator_p;
5562 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5564 return expression_list;
5567 /* Parse a pseudo-destructor-name.
5569 pseudo-destructor-name:
5570 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5571 :: [opt] nested-name-specifier template template-id :: ~ type-name
5572 :: [opt] nested-name-specifier [opt] ~ type-name
5574 If either of the first two productions is used, sets *SCOPE to the
5575 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5576 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5577 or ERROR_MARK_NODE if the parse fails. */
5580 cp_parser_pseudo_destructor_name (cp_parser* parser,
5584 bool nested_name_specifier_p;
5586 /* Assume that things will not work out. */
5587 *type = error_mark_node;
5589 /* Look for the optional `::' operator. */
5590 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5591 /* Look for the optional nested-name-specifier. */
5592 nested_name_specifier_p
5593 = (cp_parser_nested_name_specifier_opt (parser,
5594 /*typename_keyword_p=*/false,
5595 /*check_dependency_p=*/true,
5597 /*is_declaration=*/false)
5599 /* Now, if we saw a nested-name-specifier, we might be doing the
5600 second production. */
5601 if (nested_name_specifier_p
5602 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5604 /* Consume the `template' keyword. */
5605 cp_lexer_consume_token (parser->lexer);
5606 /* Parse the template-id. */
5607 cp_parser_template_id (parser,
5608 /*template_keyword_p=*/true,
5609 /*check_dependency_p=*/false,
5610 /*is_declaration=*/true);
5611 /* Look for the `::' token. */
5612 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5614 /* If the next token is not a `~', then there might be some
5615 additional qualification. */
5616 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5618 /* At this point, we're looking for "type-name :: ~". The type-name
5619 must not be a class-name, since this is a pseudo-destructor. So,
5620 it must be either an enum-name, or a typedef-name -- both of which
5621 are just identifiers. So, we peek ahead to check that the "::"
5622 and "~" tokens are present; if they are not, then we can avoid
5623 calling type_name. */
5624 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5625 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5626 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5628 cp_parser_error (parser, "non-scalar type");
5632 /* Look for the type-name. */
5633 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5634 if (*scope == error_mark_node)
5637 /* Look for the `::' token. */
5638 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5643 /* Look for the `~'. */
5644 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5646 /* Once we see the ~, this has to be a pseudo-destructor. */
5647 if (!processing_template_decl && !cp_parser_error_occurred (parser))
5648 cp_parser_commit_to_tentative_parse (parser);
5650 /* Look for the type-name again. We are not responsible for
5651 checking that it matches the first type-name. */
5652 *type = cp_parser_nonclass_name (parser);
5655 /* Parse a unary-expression.
5661 unary-operator cast-expression
5662 sizeof unary-expression
5664 alignof ( type-id ) [C++0x]
5671 __extension__ cast-expression
5672 __alignof__ unary-expression
5673 __alignof__ ( type-id )
5674 alignof unary-expression [C++0x]
5675 __real__ cast-expression
5676 __imag__ cast-expression
5679 ADDRESS_P is true iff the unary-expression is appearing as the
5680 operand of the `&' operator. CAST_P is true if this expression is
5681 the target of a cast.
5683 Returns a representation of the expression. */
5686 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5690 enum tree_code unary_operator;
5692 /* Peek at the next token. */
5693 token = cp_lexer_peek_token (parser->lexer);
5694 /* Some keywords give away the kind of expression. */
5695 if (token->type == CPP_KEYWORD)
5697 enum rid keyword = token->keyword;
5707 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5708 /* Consume the token. */
5709 cp_lexer_consume_token (parser->lexer);
5710 /* Parse the operand. */
5711 operand = cp_parser_sizeof_operand (parser, keyword);
5713 if (TYPE_P (operand))
5714 return cxx_sizeof_or_alignof_type (operand, op, true);
5717 /* ISO C++ defines alignof only with types, not with
5718 expressions. So pedwarn if alignof is used with a non-
5719 type expression. However, __alignof__ is ok. */
5720 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5721 pedwarn (token->location, OPT_pedantic,
5722 "ISO C++ does not allow %<alignof%> "
5725 return cxx_sizeof_or_alignof_expr (operand, op, true);
5730 return cp_parser_new_expression (parser);
5733 return cp_parser_delete_expression (parser);
5737 /* The saved value of the PEDANTIC flag. */
5741 /* Save away the PEDANTIC flag. */
5742 cp_parser_extension_opt (parser, &saved_pedantic);
5743 /* Parse the cast-expression. */
5744 expr = cp_parser_simple_cast_expression (parser);
5745 /* Restore the PEDANTIC flag. */
5746 pedantic = saved_pedantic;
5756 /* Consume the `__real__' or `__imag__' token. */
5757 cp_lexer_consume_token (parser->lexer);
5758 /* Parse the cast-expression. */
5759 expression = cp_parser_simple_cast_expression (parser);
5760 /* Create the complete representation. */
5761 return build_x_unary_op ((keyword == RID_REALPART
5762 ? REALPART_EXPR : IMAGPART_EXPR),
5764 tf_warning_or_error);
5771 const char *saved_message;
5772 bool saved_integral_constant_expression_p;
5773 bool saved_non_integral_constant_expression_p;
5774 bool saved_greater_than_is_operator_p;
5776 cp_lexer_consume_token (parser->lexer);
5777 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5779 saved_message = parser->type_definition_forbidden_message;
5780 parser->type_definition_forbidden_message
5781 = G_("types may not be defined in %<noexcept%> expressions");
5783 saved_integral_constant_expression_p
5784 = parser->integral_constant_expression_p;
5785 saved_non_integral_constant_expression_p
5786 = parser->non_integral_constant_expression_p;
5787 parser->integral_constant_expression_p = false;
5789 saved_greater_than_is_operator_p
5790 = parser->greater_than_is_operator_p;
5791 parser->greater_than_is_operator_p = true;
5793 ++cp_unevaluated_operand;
5794 ++c_inhibit_evaluation_warnings;
5795 expr = cp_parser_expression (parser, false, NULL);
5796 --c_inhibit_evaluation_warnings;
5797 --cp_unevaluated_operand;
5799 parser->greater_than_is_operator_p
5800 = saved_greater_than_is_operator_p;
5802 parser->integral_constant_expression_p
5803 = saved_integral_constant_expression_p;
5804 parser->non_integral_constant_expression_p
5805 = saved_non_integral_constant_expression_p;
5807 parser->type_definition_forbidden_message = saved_message;
5809 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5810 return finish_noexcept_expr (expr, tf_warning_or_error);
5818 /* Look for the `:: new' and `:: delete', which also signal the
5819 beginning of a new-expression, or delete-expression,
5820 respectively. If the next token is `::', then it might be one of
5822 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5826 /* See if the token after the `::' is one of the keywords in
5827 which we're interested. */
5828 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5829 /* If it's `new', we have a new-expression. */
5830 if (keyword == RID_NEW)
5831 return cp_parser_new_expression (parser);
5832 /* Similarly, for `delete'. */
5833 else if (keyword == RID_DELETE)
5834 return cp_parser_delete_expression (parser);
5837 /* Look for a unary operator. */
5838 unary_operator = cp_parser_unary_operator (token);
5839 /* The `++' and `--' operators can be handled similarly, even though
5840 they are not technically unary-operators in the grammar. */
5841 if (unary_operator == ERROR_MARK)
5843 if (token->type == CPP_PLUS_PLUS)
5844 unary_operator = PREINCREMENT_EXPR;
5845 else if (token->type == CPP_MINUS_MINUS)
5846 unary_operator = PREDECREMENT_EXPR;
5847 /* Handle the GNU address-of-label extension. */
5848 else if (cp_parser_allow_gnu_extensions_p (parser)
5849 && token->type == CPP_AND_AND)
5853 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5855 /* Consume the '&&' token. */
5856 cp_lexer_consume_token (parser->lexer);
5857 /* Look for the identifier. */
5858 identifier = cp_parser_identifier (parser);
5859 /* Create an expression representing the address. */
5860 expression = finish_label_address_expr (identifier, loc);
5861 if (cp_parser_non_integral_constant_expression (parser,
5863 expression = error_mark_node;
5867 if (unary_operator != ERROR_MARK)
5869 tree cast_expression;
5870 tree expression = error_mark_node;
5871 non_integral_constant non_constant_p = NIC_NONE;
5873 /* Consume the operator token. */
5874 token = cp_lexer_consume_token (parser->lexer);
5875 /* Parse the cast-expression. */
5877 = cp_parser_cast_expression (parser,
5878 unary_operator == ADDR_EXPR,
5879 /*cast_p=*/false, pidk);
5880 /* Now, build an appropriate representation. */
5881 switch (unary_operator)
5884 non_constant_p = NIC_STAR;
5885 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5886 tf_warning_or_error);
5890 non_constant_p = NIC_ADDR;
5893 expression = build_x_unary_op (unary_operator, cast_expression,
5894 tf_warning_or_error);
5897 case PREINCREMENT_EXPR:
5898 case PREDECREMENT_EXPR:
5899 non_constant_p = unary_operator == PREINCREMENT_EXPR
5900 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5902 case UNARY_PLUS_EXPR:
5904 case TRUTH_NOT_EXPR:
5905 expression = finish_unary_op_expr (unary_operator, cast_expression);
5912 if (non_constant_p != NIC_NONE
5913 && cp_parser_non_integral_constant_expression (parser,
5915 expression = error_mark_node;
5920 return cp_parser_postfix_expression (parser, address_p, cast_p,
5921 /*member_access_only_p=*/false,
5925 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5926 unary-operator, the corresponding tree code is returned. */
5928 static enum tree_code
5929 cp_parser_unary_operator (cp_token* token)
5931 switch (token->type)
5934 return INDIRECT_REF;
5940 return UNARY_PLUS_EXPR;
5946 return TRUTH_NOT_EXPR;
5949 return BIT_NOT_EXPR;
5956 /* Parse a new-expression.
5959 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5960 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5962 Returns a representation of the expression. */
5965 cp_parser_new_expression (cp_parser* parser)
5967 bool global_scope_p;
5968 VEC(tree,gc) *placement;
5970 VEC(tree,gc) *initializer;
5974 /* Look for the optional `::' operator. */
5976 = (cp_parser_global_scope_opt (parser,
5977 /*current_scope_valid_p=*/false)
5979 /* Look for the `new' operator. */
5980 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5981 /* There's no easy way to tell a new-placement from the
5982 `( type-id )' construct. */
5983 cp_parser_parse_tentatively (parser);
5984 /* Look for a new-placement. */
5985 placement = cp_parser_new_placement (parser);
5986 /* If that didn't work out, there's no new-placement. */
5987 if (!cp_parser_parse_definitely (parser))
5989 if (placement != NULL)
5990 release_tree_vector (placement);
5994 /* If the next token is a `(', then we have a parenthesized
5996 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5999 /* Consume the `('. */
6000 cp_lexer_consume_token (parser->lexer);
6001 /* Parse the type-id. */
6002 type = cp_parser_type_id (parser);
6003 /* Look for the closing `)'. */
6004 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6005 token = cp_lexer_peek_token (parser->lexer);
6006 /* There should not be a direct-new-declarator in this production,
6007 but GCC used to allowed this, so we check and emit a sensible error
6008 message for this case. */
6009 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6011 error_at (token->location,
6012 "array bound forbidden after parenthesized type-id");
6013 inform (token->location,
6014 "try removing the parentheses around the type-id");
6015 cp_parser_direct_new_declarator (parser);
6019 /* Otherwise, there must be a new-type-id. */
6021 type = cp_parser_new_type_id (parser, &nelts);
6023 /* If the next token is a `(' or '{', then we have a new-initializer. */
6024 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6025 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6026 initializer = cp_parser_new_initializer (parser);
6030 /* A new-expression may not appear in an integral constant
6032 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6033 ret = error_mark_node;
6036 /* Create a representation of the new-expression. */
6037 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6038 tf_warning_or_error);
6041 if (placement != NULL)
6042 release_tree_vector (placement);
6043 if (initializer != NULL)
6044 release_tree_vector (initializer);
6049 /* Parse a new-placement.
6054 Returns the same representation as for an expression-list. */
6056 static VEC(tree,gc) *
6057 cp_parser_new_placement (cp_parser* parser)
6059 VEC(tree,gc) *expression_list;
6061 /* Parse the expression-list. */
6062 expression_list = (cp_parser_parenthesized_expression_list
6063 (parser, non_attr, /*cast_p=*/false,
6064 /*allow_expansion_p=*/true,
6065 /*non_constant_p=*/NULL));
6067 return expression_list;
6070 /* Parse a new-type-id.
6073 type-specifier-seq new-declarator [opt]
6075 Returns the TYPE allocated. If the new-type-id indicates an array
6076 type, *NELTS is set to the number of elements in the last array
6077 bound; the TYPE will not include the last array bound. */
6080 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6082 cp_decl_specifier_seq type_specifier_seq;
6083 cp_declarator *new_declarator;
6084 cp_declarator *declarator;
6085 cp_declarator *outer_declarator;
6086 const char *saved_message;
6089 /* The type-specifier sequence must not contain type definitions.
6090 (It cannot contain declarations of new types either, but if they
6091 are not definitions we will catch that because they are not
6093 saved_message = parser->type_definition_forbidden_message;
6094 parser->type_definition_forbidden_message
6095 = G_("types may not be defined in a new-type-id");
6096 /* Parse the type-specifier-seq. */
6097 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6098 /*is_trailing_return=*/false,
6099 &type_specifier_seq);
6100 /* Restore the old message. */
6101 parser->type_definition_forbidden_message = saved_message;
6102 /* Parse the new-declarator. */
6103 new_declarator = cp_parser_new_declarator_opt (parser);
6105 /* Determine the number of elements in the last array dimension, if
6108 /* Skip down to the last array dimension. */
6109 declarator = new_declarator;
6110 outer_declarator = NULL;
6111 while (declarator && (declarator->kind == cdk_pointer
6112 || declarator->kind == cdk_ptrmem))
6114 outer_declarator = declarator;
6115 declarator = declarator->declarator;
6118 && declarator->kind == cdk_array
6119 && declarator->declarator
6120 && declarator->declarator->kind == cdk_array)
6122 outer_declarator = declarator;
6123 declarator = declarator->declarator;
6126 if (declarator && declarator->kind == cdk_array)
6128 *nelts = declarator->u.array.bounds;
6129 if (*nelts == error_mark_node)
6130 *nelts = integer_one_node;
6132 if (outer_declarator)
6133 outer_declarator->declarator = declarator->declarator;
6135 new_declarator = NULL;
6138 type = groktypename (&type_specifier_seq, new_declarator, false);
6142 /* Parse an (optional) new-declarator.
6145 ptr-operator new-declarator [opt]
6146 direct-new-declarator
6148 Returns the declarator. */
6150 static cp_declarator *
6151 cp_parser_new_declarator_opt (cp_parser* parser)
6153 enum tree_code code;
6155 cp_cv_quals cv_quals;
6157 /* We don't know if there's a ptr-operator next, or not. */
6158 cp_parser_parse_tentatively (parser);
6159 /* Look for a ptr-operator. */
6160 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6161 /* If that worked, look for more new-declarators. */
6162 if (cp_parser_parse_definitely (parser))
6164 cp_declarator *declarator;
6166 /* Parse another optional declarator. */
6167 declarator = cp_parser_new_declarator_opt (parser);
6169 return cp_parser_make_indirect_declarator
6170 (code, type, cv_quals, declarator);
6173 /* If the next token is a `[', there is a direct-new-declarator. */
6174 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6175 return cp_parser_direct_new_declarator (parser);
6180 /* Parse a direct-new-declarator.
6182 direct-new-declarator:
6184 direct-new-declarator [constant-expression]
6188 static cp_declarator *
6189 cp_parser_direct_new_declarator (cp_parser* parser)
6191 cp_declarator *declarator = NULL;
6197 /* Look for the opening `['. */
6198 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6199 /* The first expression is not required to be constant. */
6202 cp_token *token = cp_lexer_peek_token (parser->lexer);
6203 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6204 /* The standard requires that the expression have integral
6205 type. DR 74 adds enumeration types. We believe that the
6206 real intent is that these expressions be handled like the
6207 expression in a `switch' condition, which also allows
6208 classes with a single conversion to integral or
6209 enumeration type. */
6210 if (!processing_template_decl)
6213 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6218 error_at (token->location,
6219 "expression in new-declarator must have integral "
6220 "or enumeration type");
6221 expression = error_mark_node;
6225 /* But all the other expressions must be. */
6228 = cp_parser_constant_expression (parser,
6229 /*allow_non_constant=*/false,
6231 /* Look for the closing `]'. */
6232 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6234 /* Add this bound to the declarator. */
6235 declarator = make_array_declarator (declarator, expression);
6237 /* If the next token is not a `[', then there are no more
6239 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6246 /* Parse a new-initializer.
6249 ( expression-list [opt] )
6252 Returns a representation of the expression-list. */
6254 static VEC(tree,gc) *
6255 cp_parser_new_initializer (cp_parser* parser)
6257 VEC(tree,gc) *expression_list;
6259 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6262 bool expr_non_constant_p;
6263 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6264 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6265 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6266 expression_list = make_tree_vector_single (t);
6269 expression_list = (cp_parser_parenthesized_expression_list
6270 (parser, non_attr, /*cast_p=*/false,
6271 /*allow_expansion_p=*/true,
6272 /*non_constant_p=*/NULL));
6274 return expression_list;
6277 /* Parse a delete-expression.
6280 :: [opt] delete cast-expression
6281 :: [opt] delete [ ] cast-expression
6283 Returns a representation of the expression. */
6286 cp_parser_delete_expression (cp_parser* parser)
6288 bool global_scope_p;
6292 /* Look for the optional `::' operator. */
6294 = (cp_parser_global_scope_opt (parser,
6295 /*current_scope_valid_p=*/false)
6297 /* Look for the `delete' keyword. */
6298 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6299 /* See if the array syntax is in use. */
6300 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6302 /* Consume the `[' token. */
6303 cp_lexer_consume_token (parser->lexer);
6304 /* Look for the `]' token. */
6305 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6306 /* Remember that this is the `[]' construct. */
6312 /* Parse the cast-expression. */
6313 expression = cp_parser_simple_cast_expression (parser);
6315 /* A delete-expression may not appear in an integral constant
6317 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6318 return error_mark_node;
6320 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6321 tf_warning_or_error);
6324 /* Returns true if TOKEN may start a cast-expression and false
6328 cp_parser_token_starts_cast_expression (cp_token *token)
6330 switch (token->type)
6336 case CPP_CLOSE_SQUARE:
6337 case CPP_CLOSE_PAREN:
6338 case CPP_CLOSE_BRACE:
6342 case CPP_DEREF_STAR:
6350 case CPP_GREATER_EQ:
6370 /* '[' may start a primary-expression in obj-c++. */
6371 case CPP_OPEN_SQUARE:
6372 return c_dialect_objc ();
6379 /* Parse a cast-expression.
6383 ( type-id ) cast-expression
6385 ADDRESS_P is true iff the unary-expression is appearing as the
6386 operand of the `&' operator. CAST_P is true if this expression is
6387 the target of a cast.
6389 Returns a representation of the expression. */
6392 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6395 /* If it's a `(', then we might be looking at a cast. */
6396 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6398 tree type = NULL_TREE;
6399 tree expr = NULL_TREE;
6400 bool compound_literal_p;
6401 const char *saved_message;
6403 /* There's no way to know yet whether or not this is a cast.
6404 For example, `(int (3))' is a unary-expression, while `(int)
6405 3' is a cast. So, we resort to parsing tentatively. */
6406 cp_parser_parse_tentatively (parser);
6407 /* Types may not be defined in a cast. */
6408 saved_message = parser->type_definition_forbidden_message;
6409 parser->type_definition_forbidden_message
6410 = G_("types may not be defined in casts");
6411 /* Consume the `('. */
6412 cp_lexer_consume_token (parser->lexer);
6413 /* A very tricky bit is that `(struct S) { 3 }' is a
6414 compound-literal (which we permit in C++ as an extension).
6415 But, that construct is not a cast-expression -- it is a
6416 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6417 is legal; if the compound-literal were a cast-expression,
6418 you'd need an extra set of parentheses.) But, if we parse
6419 the type-id, and it happens to be a class-specifier, then we
6420 will commit to the parse at that point, because we cannot
6421 undo the action that is done when creating a new class. So,
6422 then we cannot back up and do a postfix-expression.
6424 Therefore, we scan ahead to the closing `)', and check to see
6425 if the token after the `)' is a `{'. If so, we are not
6426 looking at a cast-expression.
6428 Save tokens so that we can put them back. */
6429 cp_lexer_save_tokens (parser->lexer);
6430 /* Skip tokens until the next token is a closing parenthesis.
6431 If we find the closing `)', and the next token is a `{', then
6432 we are looking at a compound-literal. */
6434 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6435 /*consume_paren=*/true)
6436 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6437 /* Roll back the tokens we skipped. */
6438 cp_lexer_rollback_tokens (parser->lexer);
6439 /* If we were looking at a compound-literal, simulate an error
6440 so that the call to cp_parser_parse_definitely below will
6442 if (compound_literal_p)
6443 cp_parser_simulate_error (parser);
6446 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6447 parser->in_type_id_in_expr_p = true;
6448 /* Look for the type-id. */
6449 type = cp_parser_type_id (parser);
6450 /* Look for the closing `)'. */
6451 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6452 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6455 /* Restore the saved message. */
6456 parser->type_definition_forbidden_message = saved_message;
6458 /* At this point this can only be either a cast or a
6459 parenthesized ctor such as `(T ())' that looks like a cast to
6460 function returning T. */
6461 if (!cp_parser_error_occurred (parser)
6462 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6465 cp_parser_parse_definitely (parser);
6466 expr = cp_parser_cast_expression (parser,
6467 /*address_p=*/false,
6468 /*cast_p=*/true, pidk);
6470 /* Warn about old-style casts, if so requested. */
6471 if (warn_old_style_cast
6472 && !in_system_header
6473 && !VOID_TYPE_P (type)
6474 && current_lang_name != lang_name_c)
6475 warning (OPT_Wold_style_cast, "use of old-style cast");
6477 /* Only type conversions to integral or enumeration types
6478 can be used in constant-expressions. */
6479 if (!cast_valid_in_integral_constant_expression_p (type)
6480 && cp_parser_non_integral_constant_expression (parser,
6482 return error_mark_node;
6484 /* Perform the cast. */
6485 expr = build_c_cast (input_location, type, expr);
6489 cp_parser_abort_tentative_parse (parser);
6492 /* If we get here, then it's not a cast, so it must be a
6493 unary-expression. */
6494 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6497 /* Parse a binary expression of the general form:
6501 pm-expression .* cast-expression
6502 pm-expression ->* cast-expression
6504 multiplicative-expression:
6506 multiplicative-expression * pm-expression
6507 multiplicative-expression / pm-expression
6508 multiplicative-expression % pm-expression
6510 additive-expression:
6511 multiplicative-expression
6512 additive-expression + multiplicative-expression
6513 additive-expression - multiplicative-expression
6517 shift-expression << additive-expression
6518 shift-expression >> additive-expression
6520 relational-expression:
6522 relational-expression < shift-expression
6523 relational-expression > shift-expression
6524 relational-expression <= shift-expression
6525 relational-expression >= shift-expression
6529 relational-expression:
6530 relational-expression <? shift-expression
6531 relational-expression >? shift-expression
6533 equality-expression:
6534 relational-expression
6535 equality-expression == relational-expression
6536 equality-expression != relational-expression
6540 and-expression & equality-expression
6542 exclusive-or-expression:
6544 exclusive-or-expression ^ and-expression
6546 inclusive-or-expression:
6547 exclusive-or-expression
6548 inclusive-or-expression | exclusive-or-expression
6550 logical-and-expression:
6551 inclusive-or-expression
6552 logical-and-expression && inclusive-or-expression
6554 logical-or-expression:
6555 logical-and-expression
6556 logical-or-expression || logical-and-expression
6558 All these are implemented with a single function like:
6561 simple-cast-expression
6562 binary-expression <token> binary-expression
6564 CAST_P is true if this expression is the target of a cast.
6566 The binops_by_token map is used to get the tree codes for each <token> type.
6567 binary-expressions are associated according to a precedence table. */
6569 #define TOKEN_PRECEDENCE(token) \
6570 (((token->type == CPP_GREATER \
6571 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6572 && !parser->greater_than_is_operator_p) \
6573 ? PREC_NOT_OPERATOR \
6574 : binops_by_token[token->type].prec)
6577 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6578 bool no_toplevel_fold_p,
6579 enum cp_parser_prec prec,
6582 cp_parser_expression_stack stack;
6583 cp_parser_expression_stack_entry *sp = &stack[0];
6586 enum tree_code tree_type, lhs_type, rhs_type;
6587 enum cp_parser_prec new_prec, lookahead_prec;
6590 /* Parse the first expression. */
6591 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6592 lhs_type = ERROR_MARK;
6596 /* Get an operator token. */
6597 token = cp_lexer_peek_token (parser->lexer);
6599 if (warn_cxx0x_compat
6600 && token->type == CPP_RSHIFT
6601 && !parser->greater_than_is_operator_p)
6603 if (warning_at (token->location, OPT_Wc__0x_compat,
6604 "%<>>%> operator will be treated as"
6605 " two right angle brackets in C++0x"))
6606 inform (token->location,
6607 "suggest parentheses around %<>>%> expression");
6610 new_prec = TOKEN_PRECEDENCE (token);
6612 /* Popping an entry off the stack means we completed a subexpression:
6613 - either we found a token which is not an operator (`>' where it is not
6614 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6615 will happen repeatedly;
6616 - or, we found an operator which has lower priority. This is the case
6617 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6619 if (new_prec <= prec)
6628 tree_type = binops_by_token[token->type].tree_type;
6630 /* We used the operator token. */
6631 cp_lexer_consume_token (parser->lexer);
6633 /* For "false && x" or "true || x", x will never be executed;
6634 disable warnings while evaluating it. */
6635 if (tree_type == TRUTH_ANDIF_EXPR)
6636 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6637 else if (tree_type == TRUTH_ORIF_EXPR)
6638 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6640 /* Extract another operand. It may be the RHS of this expression
6641 or the LHS of a new, higher priority expression. */
6642 rhs = cp_parser_simple_cast_expression (parser);
6643 rhs_type = ERROR_MARK;
6645 /* Get another operator token. Look up its precedence to avoid
6646 building a useless (immediately popped) stack entry for common
6647 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6648 token = cp_lexer_peek_token (parser->lexer);
6649 lookahead_prec = TOKEN_PRECEDENCE (token);
6650 if (lookahead_prec > new_prec)
6652 /* ... and prepare to parse the RHS of the new, higher priority
6653 expression. Since precedence levels on the stack are
6654 monotonically increasing, we do not have to care about
6657 sp->tree_type = tree_type;
6659 sp->lhs_type = lhs_type;
6662 lhs_type = rhs_type;
6664 new_prec = lookahead_prec;
6668 lookahead_prec = new_prec;
6669 /* If the stack is not empty, we have parsed into LHS the right side
6670 (`4' in the example above) of an expression we had suspended.
6671 We can use the information on the stack to recover the LHS (`3')
6672 from the stack together with the tree code (`MULT_EXPR'), and
6673 the precedence of the higher level subexpression
6674 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6675 which will be used to actually build the additive expression. */
6678 tree_type = sp->tree_type;
6680 rhs_type = lhs_type;
6682 lhs_type = sp->lhs_type;
6685 /* Undo the disabling of warnings done above. */
6686 if (tree_type == TRUTH_ANDIF_EXPR)
6687 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6688 else if (tree_type == TRUTH_ORIF_EXPR)
6689 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6692 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6693 ERROR_MARK for everything that is not a binary expression.
6694 This makes warn_about_parentheses miss some warnings that
6695 involve unary operators. For unary expressions we should
6696 pass the correct tree_code unless the unary expression was
6697 surrounded by parentheses.
6699 if (no_toplevel_fold_p
6700 && lookahead_prec <= prec
6702 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6703 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6705 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6706 &overload, tf_warning_or_error);
6707 lhs_type = tree_type;
6709 /* If the binary operator required the use of an overloaded operator,
6710 then this expression cannot be an integral constant-expression.
6711 An overloaded operator can be used even if both operands are
6712 otherwise permissible in an integral constant-expression if at
6713 least one of the operands is of enumeration type. */
6716 && cp_parser_non_integral_constant_expression (parser,
6718 return error_mark_node;
6725 /* Parse the `? expression : assignment-expression' part of a
6726 conditional-expression. The LOGICAL_OR_EXPR is the
6727 logical-or-expression that started the conditional-expression.
6728 Returns a representation of the entire conditional-expression.
6730 This routine is used by cp_parser_assignment_expression.
6732 ? expression : assignment-expression
6736 ? : assignment-expression */
6739 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6742 tree assignment_expr;
6743 struct cp_token *token;
6745 /* Consume the `?' token. */
6746 cp_lexer_consume_token (parser->lexer);
6747 token = cp_lexer_peek_token (parser->lexer);
6748 if (cp_parser_allow_gnu_extensions_p (parser)
6749 && token->type == CPP_COLON)
6751 pedwarn (token->location, OPT_pedantic,
6752 "ISO C++ does not allow ?: with omitted middle operand");
6753 /* Implicit true clause. */
6755 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6756 warn_for_omitted_condop (token->location, logical_or_expr);
6760 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6761 parser->colon_corrects_to_scope_p = false;
6762 /* Parse the expression. */
6763 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6764 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6765 c_inhibit_evaluation_warnings +=
6766 ((logical_or_expr == truthvalue_true_node)
6767 - (logical_or_expr == truthvalue_false_node));
6768 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6771 /* The next token should be a `:'. */
6772 cp_parser_require (parser, CPP_COLON, RT_COLON);
6773 /* Parse the assignment-expression. */
6774 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6775 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6777 /* Build the conditional-expression. */
6778 return build_x_conditional_expr (logical_or_expr,
6781 tf_warning_or_error);
6784 /* Parse an assignment-expression.
6786 assignment-expression:
6787 conditional-expression
6788 logical-or-expression assignment-operator assignment_expression
6791 CAST_P is true if this expression is the target of a cast.
6793 Returns a representation for the expression. */
6796 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6801 /* If the next token is the `throw' keyword, then we're looking at
6802 a throw-expression. */
6803 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6804 expr = cp_parser_throw_expression (parser);
6805 /* Otherwise, it must be that we are looking at a
6806 logical-or-expression. */
6809 /* Parse the binary expressions (logical-or-expression). */
6810 expr = cp_parser_binary_expression (parser, cast_p, false,
6811 PREC_NOT_OPERATOR, pidk);
6812 /* If the next token is a `?' then we're actually looking at a
6813 conditional-expression. */
6814 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6815 return cp_parser_question_colon_clause (parser, expr);
6818 enum tree_code assignment_operator;
6820 /* If it's an assignment-operator, we're using the second
6823 = cp_parser_assignment_operator_opt (parser);
6824 if (assignment_operator != ERROR_MARK)
6826 bool non_constant_p;
6828 /* Parse the right-hand side of the assignment. */
6829 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6831 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6832 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6834 /* An assignment may not appear in a
6835 constant-expression. */
6836 if (cp_parser_non_integral_constant_expression (parser,
6838 return error_mark_node;
6839 /* Build the assignment expression. */
6840 expr = build_x_modify_expr (expr,
6841 assignment_operator,
6843 tf_warning_or_error);
6851 /* Parse an (optional) assignment-operator.
6853 assignment-operator: one of
6854 = *= /= %= += -= >>= <<= &= ^= |=
6858 assignment-operator: one of
6861 If the next token is an assignment operator, the corresponding tree
6862 code is returned, and the token is consumed. For example, for
6863 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6864 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6865 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6866 operator, ERROR_MARK is returned. */
6868 static enum tree_code
6869 cp_parser_assignment_operator_opt (cp_parser* parser)
6874 /* Peek at the next token. */
6875 token = cp_lexer_peek_token (parser->lexer);
6877 switch (token->type)
6888 op = TRUNC_DIV_EXPR;
6892 op = TRUNC_MOD_EXPR;
6924 /* Nothing else is an assignment operator. */
6928 /* If it was an assignment operator, consume it. */
6929 if (op != ERROR_MARK)
6930 cp_lexer_consume_token (parser->lexer);
6935 /* Parse an expression.
6938 assignment-expression
6939 expression , assignment-expression
6941 CAST_P is true if this expression is the target of a cast.
6943 Returns a representation of the expression. */
6946 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6948 tree expression = NULL_TREE;
6952 tree assignment_expression;
6954 /* Parse the next assignment-expression. */
6955 assignment_expression
6956 = cp_parser_assignment_expression (parser, cast_p, pidk);
6957 /* If this is the first assignment-expression, we can just
6960 expression = assignment_expression;
6962 expression = build_x_compound_expr (expression,
6963 assignment_expression,
6964 tf_warning_or_error);
6965 /* If the next token is not a comma, then we are done with the
6967 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6969 /* Consume the `,'. */
6970 cp_lexer_consume_token (parser->lexer);
6971 /* A comma operator cannot appear in a constant-expression. */
6972 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6973 expression = error_mark_node;
6979 /* Parse a constant-expression.
6981 constant-expression:
6982 conditional-expression
6984 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6985 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6986 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6987 is false, NON_CONSTANT_P should be NULL. */
6990 cp_parser_constant_expression (cp_parser* parser,
6991 bool allow_non_constant_p,
6992 bool *non_constant_p)
6994 bool saved_integral_constant_expression_p;
6995 bool saved_allow_non_integral_constant_expression_p;
6996 bool saved_non_integral_constant_expression_p;
6999 /* It might seem that we could simply parse the
7000 conditional-expression, and then check to see if it were
7001 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7002 one that the compiler can figure out is constant, possibly after
7003 doing some simplifications or optimizations. The standard has a
7004 precise definition of constant-expression, and we must honor
7005 that, even though it is somewhat more restrictive.
7011 is not a legal declaration, because `(2, 3)' is not a
7012 constant-expression. The `,' operator is forbidden in a
7013 constant-expression. However, GCC's constant-folding machinery
7014 will fold this operation to an INTEGER_CST for `3'. */
7016 /* Save the old settings. */
7017 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7018 saved_allow_non_integral_constant_expression_p
7019 = parser->allow_non_integral_constant_expression_p;
7020 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7021 /* We are now parsing a constant-expression. */
7022 parser->integral_constant_expression_p = true;
7023 parser->allow_non_integral_constant_expression_p
7024 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7025 parser->non_integral_constant_expression_p = false;
7026 /* Although the grammar says "conditional-expression", we parse an
7027 "assignment-expression", which also permits "throw-expression"
7028 and the use of assignment operators. In the case that
7029 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7030 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7031 actually essential that we look for an assignment-expression.
7032 For example, cp_parser_initializer_clauses uses this function to
7033 determine whether a particular assignment-expression is in fact
7035 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7036 /* Restore the old settings. */
7037 parser->integral_constant_expression_p
7038 = saved_integral_constant_expression_p;
7039 parser->allow_non_integral_constant_expression_p
7040 = saved_allow_non_integral_constant_expression_p;
7041 if (cxx_dialect >= cxx0x)
7043 /* Require an rvalue constant expression here; that's what our
7044 callers expect. Reference constant expressions are handled
7045 separately in e.g. cp_parser_template_argument. */
7046 bool is_const = potential_rvalue_constant_expression (expression);
7047 parser->non_integral_constant_expression_p = !is_const;
7048 if (!is_const && !allow_non_constant_p)
7049 require_potential_rvalue_constant_expression (expression);
7051 if (allow_non_constant_p)
7052 *non_constant_p = parser->non_integral_constant_expression_p;
7053 parser->non_integral_constant_expression_p
7054 = saved_non_integral_constant_expression_p;
7059 /* Parse __builtin_offsetof.
7061 offsetof-expression:
7062 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7064 offsetof-member-designator:
7066 | offsetof-member-designator "." id-expression
7067 | offsetof-member-designator "[" expression "]"
7068 | offsetof-member-designator "->" id-expression */
7071 cp_parser_builtin_offsetof (cp_parser *parser)
7073 int save_ice_p, save_non_ice_p;
7078 /* We're about to accept non-integral-constant things, but will
7079 definitely yield an integral constant expression. Save and
7080 restore these values around our local parsing. */
7081 save_ice_p = parser->integral_constant_expression_p;
7082 save_non_ice_p = parser->non_integral_constant_expression_p;
7084 /* Consume the "__builtin_offsetof" token. */
7085 cp_lexer_consume_token (parser->lexer);
7086 /* Consume the opening `('. */
7087 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7088 /* Parse the type-id. */
7089 type = cp_parser_type_id (parser);
7090 /* Look for the `,'. */
7091 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7092 token = cp_lexer_peek_token (parser->lexer);
7094 /* Build the (type *)null that begins the traditional offsetof macro. */
7095 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7096 tf_warning_or_error);
7098 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7099 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7100 true, &dummy, token->location);
7103 token = cp_lexer_peek_token (parser->lexer);
7104 switch (token->type)
7106 case CPP_OPEN_SQUARE:
7107 /* offsetof-member-designator "[" expression "]" */
7108 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7112 /* offsetof-member-designator "->" identifier */
7113 expr = grok_array_decl (expr, integer_zero_node);
7117 /* offsetof-member-designator "." identifier */
7118 cp_lexer_consume_token (parser->lexer);
7119 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7124 case CPP_CLOSE_PAREN:
7125 /* Consume the ")" token. */
7126 cp_lexer_consume_token (parser->lexer);
7130 /* Error. We know the following require will fail, but
7131 that gives the proper error message. */
7132 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7133 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7134 expr = error_mark_node;
7140 /* If we're processing a template, we can't finish the semantics yet.
7141 Otherwise we can fold the entire expression now. */
7142 if (processing_template_decl)
7143 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7145 expr = finish_offsetof (expr);
7148 parser->integral_constant_expression_p = save_ice_p;
7149 parser->non_integral_constant_expression_p = save_non_ice_p;
7154 /* Parse a trait expression.
7156 Returns a representation of the expression, the underlying type
7157 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7160 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7163 tree type1, type2 = NULL_TREE;
7164 bool binary = false;
7165 cp_decl_specifier_seq decl_specs;
7169 case RID_HAS_NOTHROW_ASSIGN:
7170 kind = CPTK_HAS_NOTHROW_ASSIGN;
7172 case RID_HAS_NOTHROW_CONSTRUCTOR:
7173 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7175 case RID_HAS_NOTHROW_COPY:
7176 kind = CPTK_HAS_NOTHROW_COPY;
7178 case RID_HAS_TRIVIAL_ASSIGN:
7179 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7181 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7182 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7184 case RID_HAS_TRIVIAL_COPY:
7185 kind = CPTK_HAS_TRIVIAL_COPY;
7187 case RID_HAS_TRIVIAL_DESTRUCTOR:
7188 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7190 case RID_HAS_VIRTUAL_DESTRUCTOR:
7191 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7193 case RID_IS_ABSTRACT:
7194 kind = CPTK_IS_ABSTRACT;
7196 case RID_IS_BASE_OF:
7197 kind = CPTK_IS_BASE_OF;
7201 kind = CPTK_IS_CLASS;
7203 case RID_IS_CONVERTIBLE_TO:
7204 kind = CPTK_IS_CONVERTIBLE_TO;
7208 kind = CPTK_IS_EMPTY;
7211 kind = CPTK_IS_ENUM;
7213 case RID_IS_LITERAL_TYPE:
7214 kind = CPTK_IS_LITERAL_TYPE;
7219 case RID_IS_POLYMORPHIC:
7220 kind = CPTK_IS_POLYMORPHIC;
7222 case RID_IS_STD_LAYOUT:
7223 kind = CPTK_IS_STD_LAYOUT;
7225 case RID_IS_TRIVIAL:
7226 kind = CPTK_IS_TRIVIAL;
7229 kind = CPTK_IS_UNION;
7231 case RID_UNDERLYING_TYPE:
7232 kind = CPTK_UNDERLYING_TYPE;
7238 /* Consume the token. */
7239 cp_lexer_consume_token (parser->lexer);
7241 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7243 type1 = cp_parser_type_id (parser);
7245 if (type1 == error_mark_node)
7246 return error_mark_node;
7248 /* Build a trivial decl-specifier-seq. */
7249 clear_decl_specs (&decl_specs);
7250 decl_specs.type = type1;
7252 /* Call grokdeclarator to figure out what type this is. */
7253 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7254 /*initialized=*/0, /*attrlist=*/NULL);
7258 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7260 type2 = cp_parser_type_id (parser);
7262 if (type2 == error_mark_node)
7263 return error_mark_node;
7265 /* Build a trivial decl-specifier-seq. */
7266 clear_decl_specs (&decl_specs);
7267 decl_specs.type = type2;
7269 /* Call grokdeclarator to figure out what type this is. */
7270 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7271 /*initialized=*/0, /*attrlist=*/NULL);
7274 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7276 /* Complete the trait expression, which may mean either processing
7277 the trait expr now or saving it for template instantiation. */
7278 return kind != CPTK_UNDERLYING_TYPE
7279 ? finish_trait_expr (kind, type1, type2)
7280 : finish_underlying_type (type1);
7283 /* Lambdas that appear in variable initializer or default argument scope
7284 get that in their mangling, so we need to record it. We might as well
7285 use the count for function and namespace scopes as well. */
7286 static GTY(()) tree lambda_scope;
7287 static GTY(()) int lambda_count;
7288 typedef struct GTY(()) tree_int
7293 DEF_VEC_O(tree_int);
7294 DEF_VEC_ALLOC_O(tree_int,gc);
7295 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7298 start_lambda_scope (tree decl)
7302 /* Once we're inside a function, we ignore other scopes and just push
7303 the function again so that popping works properly. */
7304 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7305 decl = current_function_decl;
7306 ti.t = lambda_scope;
7307 ti.i = lambda_count;
7308 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7309 if (lambda_scope != decl)
7311 /* Don't reset the count if we're still in the same function. */
7312 lambda_scope = decl;
7318 record_lambda_scope (tree lambda)
7320 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7321 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7325 finish_lambda_scope (void)
7327 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7328 if (lambda_scope != p->t)
7330 lambda_scope = p->t;
7331 lambda_count = p->i;
7333 VEC_pop (tree_int, lambda_scope_stack);
7336 /* Parse a lambda expression.
7339 lambda-introducer lambda-declarator [opt] compound-statement
7341 Returns a representation of the expression. */
7344 cp_parser_lambda_expression (cp_parser* parser)
7346 tree lambda_expr = build_lambda_expr ();
7350 LAMBDA_EXPR_LOCATION (lambda_expr)
7351 = cp_lexer_peek_token (parser->lexer)->location;
7353 if (cp_unevaluated_operand)
7354 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7355 "lambda-expression in unevaluated context");
7357 /* We may be in the middle of deferred access check. Disable
7359 push_deferring_access_checks (dk_no_deferred);
7361 cp_parser_lambda_introducer (parser, lambda_expr);
7363 type = begin_lambda_type (lambda_expr);
7365 record_lambda_scope (lambda_expr);
7367 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7368 determine_visibility (TYPE_NAME (type));
7370 /* Now that we've started the type, add the capture fields for any
7371 explicit captures. */
7372 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7375 /* Inside the class, surrounding template-parameter-lists do not apply. */
7376 unsigned int saved_num_template_parameter_lists
7377 = parser->num_template_parameter_lists;
7379 parser->num_template_parameter_lists = 0;
7381 /* By virtue of defining a local class, a lambda expression has access to
7382 the private variables of enclosing classes. */
7384 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7387 cp_parser_lambda_body (parser, lambda_expr);
7388 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7389 cp_parser_skip_to_end_of_block_or_statement (parser);
7391 /* The capture list was built up in reverse order; fix that now. */
7393 tree newlist = NULL_TREE;
7396 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7399 tree field = TREE_PURPOSE (elt);
7402 next = TREE_CHAIN (elt);
7403 TREE_CHAIN (elt) = newlist;
7406 /* Also add __ to the beginning of the field name so that code
7407 outside the lambda body can't see the captured name. We could
7408 just remove the name entirely, but this is more useful for
7410 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7411 /* The 'this' capture already starts with __. */
7414 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7415 buf[1] = buf[0] = '_';
7416 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7417 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7418 DECL_NAME (field) = get_identifier (buf);
7420 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7424 maybe_add_lambda_conv_op (type);
7426 type = finish_struct (type, /*attributes=*/NULL_TREE);
7428 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7431 pop_deferring_access_checks ();
7433 /* This field is only used during parsing of the lambda. */
7434 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7437 return build_lambda_object (lambda_expr);
7439 return error_mark_node;
7442 /* Parse the beginning of a lambda expression.
7445 [ lambda-capture [opt] ]
7447 LAMBDA_EXPR is the current representation of the lambda expression. */
7450 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7452 /* Need commas after the first capture. */
7455 /* Eat the leading `['. */
7456 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7458 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7459 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7460 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7461 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7462 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7463 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7465 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7467 cp_lexer_consume_token (parser->lexer);
7471 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7473 cp_token* capture_token;
7475 tree capture_init_expr;
7476 cp_id_kind idk = CP_ID_KIND_NONE;
7477 bool explicit_init_p = false;
7479 enum capture_kind_type
7484 enum capture_kind_type capture_kind = BY_COPY;
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7488 error ("expected end of capture-list");
7495 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7497 /* Possibly capture `this'. */
7498 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7500 cp_lexer_consume_token (parser->lexer);
7501 add_capture (lambda_expr,
7502 /*id=*/get_identifier ("__this"),
7503 /*initializer=*/finish_this_expr(),
7504 /*by_reference_p=*/false,
7509 /* Remember whether we want to capture as a reference or not. */
7510 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7512 capture_kind = BY_REFERENCE;
7513 cp_lexer_consume_token (parser->lexer);
7516 /* Get the identifier. */
7517 capture_token = cp_lexer_peek_token (parser->lexer);
7518 capture_id = cp_parser_identifier (parser);
7520 if (capture_id == error_mark_node)
7521 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7522 delimiters, but I modified this to stop on unnested ']' as well. It
7523 was already changed to stop on unnested '}', so the
7524 "closing_parenthesis" name is no more misleading with my change. */
7526 cp_parser_skip_to_closing_parenthesis (parser,
7527 /*recovering=*/true,
7529 /*consume_paren=*/true);
7533 /* Find the initializer for this capture. */
7534 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7536 /* An explicit expression exists. */
7537 cp_lexer_consume_token (parser->lexer);
7538 pedwarn (input_location, OPT_pedantic,
7539 "ISO C++ does not allow initializers "
7540 "in lambda expression capture lists");
7541 capture_init_expr = cp_parser_assignment_expression (parser,
7544 explicit_init_p = true;
7548 const char* error_msg;
7550 /* Turn the identifier into an id-expression. */
7552 = cp_parser_lookup_name
7556 /*is_template=*/false,
7557 /*is_namespace=*/false,
7558 /*check_dependency=*/true,
7559 /*ambiguous_decls=*/NULL,
7560 capture_token->location);
7563 = finish_id_expression
7568 /*integral_constant_expression_p=*/false,
7569 /*allow_non_integral_constant_expression_p=*/false,
7570 /*non_integral_constant_expression_p=*/NULL,
7571 /*template_p=*/false,
7573 /*address_p=*/false,
7574 /*template_arg_p=*/false,
7576 capture_token->location);
7579 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7581 = unqualified_name_lookup_error (capture_init_expr);
7583 add_capture (lambda_expr,
7586 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7590 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7593 /* Parse the (optional) middle of a lambda expression.
7596 ( parameter-declaration-clause [opt] )
7597 attribute-specifier [opt]
7599 exception-specification [opt]
7600 lambda-return-type-clause [opt]
7602 LAMBDA_EXPR is the current representation of the lambda expression. */
7605 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7607 /* 5.1.1.4 of the standard says:
7608 If a lambda-expression does not include a lambda-declarator, it is as if
7609 the lambda-declarator were ().
7610 This means an empty parameter list, no attributes, and no exception
7612 tree param_list = void_list_node;
7613 tree attributes = NULL_TREE;
7614 tree exception_spec = NULL_TREE;
7617 /* The lambda-declarator is optional, but must begin with an opening
7618 parenthesis if present. */
7619 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7621 cp_lexer_consume_token (parser->lexer);
7623 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7625 /* Parse parameters. */
7626 param_list = cp_parser_parameter_declaration_clause (parser);
7628 /* Default arguments shall not be specified in the
7629 parameter-declaration-clause of a lambda-declarator. */
7630 for (t = param_list; t; t = TREE_CHAIN (t))
7631 if (TREE_PURPOSE (t))
7632 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7633 "default argument specified for lambda parameter");
7635 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7637 attributes = cp_parser_attributes_opt (parser);
7639 /* Parse optional `mutable' keyword. */
7640 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7642 cp_lexer_consume_token (parser->lexer);
7643 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7646 /* Parse optional exception specification. */
7647 exception_spec = cp_parser_exception_specification_opt (parser);
7649 /* Parse optional trailing return type. */
7650 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7652 cp_lexer_consume_token (parser->lexer);
7653 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7656 /* The function parameters must be in scope all the way until after the
7657 trailing-return-type in case of decltype. */
7658 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7659 pop_binding (DECL_NAME (t), t);
7664 /* Create the function call operator.
7666 Messing with declarators like this is no uglier than building up the
7667 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7670 cp_decl_specifier_seq return_type_specs;
7671 cp_declarator* declarator;
7676 clear_decl_specs (&return_type_specs);
7677 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7678 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7680 /* Maybe we will deduce the return type later, but we can use void
7681 as a placeholder return type anyways. */
7682 return_type_specs.type = void_type_node;
7684 p = obstack_alloc (&declarator_obstack, 0);
7686 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7689 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7690 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7691 declarator = make_call_declarator (declarator, param_list, quals,
7692 VIRT_SPEC_UNSPECIFIED,
7694 /*late_return_type=*/NULL_TREE);
7695 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7697 fco = grokmethod (&return_type_specs,
7700 if (fco != error_mark_node)
7702 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7703 DECL_ARTIFICIAL (fco) = 1;
7706 finish_member_declaration (fco);
7708 obstack_free (&declarator_obstack, p);
7710 return (fco != error_mark_node);
7714 /* Parse the body of a lambda expression, which is simply
7718 but which requires special handling.
7719 LAMBDA_EXPR is the current representation of the lambda expression. */
7722 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7724 bool nested = (current_function_decl != NULL_TREE);
7726 push_function_context ();
7728 /* Finish the function call operator
7730 + late_parsing_for_member
7731 + function_definition_after_declarator
7732 + ctor_initializer_opt_and_function_body */
7734 tree fco = lambda_function (lambda_expr);
7739 /* Let the front end know that we are going to be defining this
7741 start_preparsed_function (fco,
7743 SF_PRE_PARSED | SF_INCLASS_INLINE);
7745 start_lambda_scope (fco);
7746 body = begin_function_body ();
7748 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7751 compound_stmt = begin_compound_stmt (0);
7753 /* 5.1.1.4 of the standard says:
7754 If a lambda-expression does not include a trailing-return-type, it
7755 is as if the trailing-return-type denotes the following type:
7756 * if the compound-statement is of the form
7757 { return attribute-specifier [opt] expression ; }
7758 the type of the returned expression after lvalue-to-rvalue
7759 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7760 (_conv.array_ 4.2), and function-to-pointer conversion
7762 * otherwise, void. */
7764 /* In a lambda that has neither a lambda-return-type-clause
7765 nor a deducible form, errors should be reported for return statements
7766 in the body. Since we used void as the placeholder return type, parsing
7767 the body as usual will give such desired behavior. */
7768 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7769 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7770 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7772 tree expr = NULL_TREE;
7773 cp_id_kind idk = CP_ID_KIND_NONE;
7775 /* Parse tentatively in case there's more after the initial return
7777 cp_parser_parse_tentatively (parser);
7779 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7781 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7783 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7784 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7786 if (cp_parser_parse_definitely (parser))
7788 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7790 /* Will get error here if type not deduced yet. */
7791 finish_return_stmt (expr);
7799 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7800 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7801 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7802 cp_parser_label_declaration (parser);
7803 cp_parser_statement_seq_opt (parser, NULL_TREE);
7804 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7805 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7808 finish_compound_stmt (compound_stmt);
7811 finish_function_body (body);
7812 finish_lambda_scope ();
7814 /* Finish the function and generate code for it if necessary. */
7815 expand_or_defer_fn (finish_function (/*inline*/2));
7819 pop_function_context();
7822 /* Statements [gram.stmt.stmt] */
7824 /* Parse a statement.
7828 expression-statement
7833 declaration-statement
7836 IN_COMPOUND is true when the statement is nested inside a
7837 cp_parser_compound_statement; this matters for certain pragmas.
7839 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7840 is a (possibly labeled) if statement which is not enclosed in braces
7841 and has an else clause. This is used to implement -Wparentheses. */
7844 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7845 bool in_compound, bool *if_p)
7849 location_t statement_location;
7854 /* There is no statement yet. */
7855 statement = NULL_TREE;
7856 /* Peek at the next token. */
7857 token = cp_lexer_peek_token (parser->lexer);
7858 /* Remember the location of the first token in the statement. */
7859 statement_location = token->location;
7860 /* If this is a keyword, then that will often determine what kind of
7861 statement we have. */
7862 if (token->type == CPP_KEYWORD)
7864 enum rid keyword = token->keyword;
7870 /* Looks like a labeled-statement with a case label.
7871 Parse the label, and then use tail recursion to parse
7873 cp_parser_label_for_labeled_statement (parser);
7878 statement = cp_parser_selection_statement (parser, if_p);
7884 statement = cp_parser_iteration_statement (parser);
7891 statement = cp_parser_jump_statement (parser);
7894 /* Objective-C++ exception-handling constructs. */
7897 case RID_AT_FINALLY:
7898 case RID_AT_SYNCHRONIZED:
7900 statement = cp_parser_objc_statement (parser);
7904 statement = cp_parser_try_block (parser);
7908 /* This must be a namespace alias definition. */
7909 cp_parser_declaration_statement (parser);
7913 /* It might be a keyword like `int' that can start a
7914 declaration-statement. */
7918 else if (token->type == CPP_NAME)
7920 /* If the next token is a `:', then we are looking at a
7921 labeled-statement. */
7922 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7923 if (token->type == CPP_COLON)
7925 /* Looks like a labeled-statement with an ordinary label.
7926 Parse the label, and then use tail recursion to parse
7928 cp_parser_label_for_labeled_statement (parser);
7932 /* Anything that starts with a `{' must be a compound-statement. */
7933 else if (token->type == CPP_OPEN_BRACE)
7934 statement = cp_parser_compound_statement (parser, NULL, false, false);
7935 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7936 a statement all its own. */
7937 else if (token->type == CPP_PRAGMA)
7939 /* Only certain OpenMP pragmas are attached to statements, and thus
7940 are considered statements themselves. All others are not. In
7941 the context of a compound, accept the pragma as a "statement" and
7942 return so that we can check for a close brace. Otherwise we
7943 require a real statement and must go back and read one. */
7945 cp_parser_pragma (parser, pragma_compound);
7946 else if (!cp_parser_pragma (parser, pragma_stmt))
7950 else if (token->type == CPP_EOF)
7952 cp_parser_error (parser, "expected statement");
7956 /* Everything else must be a declaration-statement or an
7957 expression-statement. Try for the declaration-statement
7958 first, unless we are looking at a `;', in which case we know that
7959 we have an expression-statement. */
7962 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7964 cp_parser_parse_tentatively (parser);
7965 /* Try to parse the declaration-statement. */
7966 cp_parser_declaration_statement (parser);
7967 /* If that worked, we're done. */
7968 if (cp_parser_parse_definitely (parser))
7971 /* Look for an expression-statement instead. */
7972 statement = cp_parser_expression_statement (parser, in_statement_expr);
7975 /* Set the line number for the statement. */
7976 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7977 SET_EXPR_LOCATION (statement, statement_location);
7980 /* Parse the label for a labeled-statement, i.e.
7983 case constant-expression :
7987 case constant-expression ... constant-expression : statement
7989 When a label is parsed without errors, the label is added to the
7990 parse tree by the finish_* functions, so this function doesn't
7991 have to return the label. */
7994 cp_parser_label_for_labeled_statement (cp_parser* parser)
7997 tree label = NULL_TREE;
7998 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8000 /* The next token should be an identifier. */
8001 token = cp_lexer_peek_token (parser->lexer);
8002 if (token->type != CPP_NAME
8003 && token->type != CPP_KEYWORD)
8005 cp_parser_error (parser, "expected labeled-statement");
8009 parser->colon_corrects_to_scope_p = false;
8010 switch (token->keyword)
8017 /* Consume the `case' token. */
8018 cp_lexer_consume_token (parser->lexer);
8019 /* Parse the constant-expression. */
8020 expr = cp_parser_constant_expression (parser,
8021 /*allow_non_constant_p=*/false,
8024 ellipsis = cp_lexer_peek_token (parser->lexer);
8025 if (ellipsis->type == CPP_ELLIPSIS)
8027 /* Consume the `...' token. */
8028 cp_lexer_consume_token (parser->lexer);
8030 cp_parser_constant_expression (parser,
8031 /*allow_non_constant_p=*/false,
8033 /* We don't need to emit warnings here, as the common code
8034 will do this for us. */
8037 expr_hi = NULL_TREE;
8039 if (parser->in_switch_statement_p)
8040 finish_case_label (token->location, expr, expr_hi);
8042 error_at (token->location,
8043 "case label %qE not within a switch statement",
8049 /* Consume the `default' token. */
8050 cp_lexer_consume_token (parser->lexer);
8052 if (parser->in_switch_statement_p)
8053 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8055 error_at (token->location, "case label not within a switch statement");
8059 /* Anything else must be an ordinary label. */
8060 label = finish_label_stmt (cp_parser_identifier (parser));
8064 /* Require the `:' token. */
8065 cp_parser_require (parser, CPP_COLON, RT_COLON);
8067 /* An ordinary label may optionally be followed by attributes.
8068 However, this is only permitted if the attributes are then
8069 followed by a semicolon. This is because, for backward
8070 compatibility, when parsing
8071 lab: __attribute__ ((unused)) int i;
8072 we want the attribute to attach to "i", not "lab". */
8073 if (label != NULL_TREE
8074 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8078 cp_parser_parse_tentatively (parser);
8079 attrs = cp_parser_attributes_opt (parser);
8080 if (attrs == NULL_TREE
8081 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8082 cp_parser_abort_tentative_parse (parser);
8083 else if (!cp_parser_parse_definitely (parser))
8086 cplus_decl_attributes (&label, attrs, 0);
8089 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8092 /* Parse an expression-statement.
8094 expression-statement:
8097 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8098 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8099 indicates whether this expression-statement is part of an
8100 expression statement. */
8103 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8105 tree statement = NULL_TREE;
8106 cp_token *token = cp_lexer_peek_token (parser->lexer);
8108 /* If the next token is a ';', then there is no expression
8110 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8111 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8113 /* Give a helpful message for "A<T>::type t;" and the like. */
8114 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8115 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8117 if (TREE_CODE (statement) == SCOPE_REF)
8118 error_at (token->location, "need %<typename%> before %qE because "
8119 "%qT is a dependent scope",
8120 statement, TREE_OPERAND (statement, 0));
8121 else if (is_overloaded_fn (statement)
8122 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8125 tree fn = get_first_fn (statement);
8126 error_at (token->location,
8127 "%<%T::%D%> names the constructor, not the type",
8128 DECL_CONTEXT (fn), DECL_NAME (fn));
8132 /* Consume the final `;'. */
8133 cp_parser_consume_semicolon_at_end_of_statement (parser);
8135 if (in_statement_expr
8136 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8137 /* This is the final expression statement of a statement
8139 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8141 statement = finish_expr_stmt (statement);
8148 /* Parse a compound-statement.
8151 { statement-seq [opt] }
8156 { label-declaration-seq [opt] statement-seq [opt] }
8158 label-declaration-seq:
8160 label-declaration-seq label-declaration
8162 Returns a tree representing the statement. */
8165 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8166 bool in_try, bool function_body)
8170 /* Consume the `{'. */
8171 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8172 return error_mark_node;
8173 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8175 pedwarn (input_location, OPT_pedantic,
8176 "compound-statement in constexpr function");
8177 /* Begin the compound-statement. */
8178 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8179 /* If the next keyword is `__label__' we have a label declaration. */
8180 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8181 cp_parser_label_declaration (parser);
8182 /* Parse an (optional) statement-seq. */
8183 cp_parser_statement_seq_opt (parser, in_statement_expr);
8184 /* Finish the compound-statement. */
8185 finish_compound_stmt (compound_stmt);
8186 /* Consume the `}'. */
8187 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8189 return compound_stmt;
8192 /* Parse an (optional) statement-seq.
8196 statement-seq [opt] statement */
8199 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8201 /* Scan statements until there aren't any more. */
8204 cp_token *token = cp_lexer_peek_token (parser->lexer);
8206 /* If we are looking at a `}', then we have run out of
8207 statements; the same is true if we have reached the end
8208 of file, or have stumbled upon a stray '@end'. */
8209 if (token->type == CPP_CLOSE_BRACE
8210 || token->type == CPP_EOF
8211 || token->type == CPP_PRAGMA_EOL
8212 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8215 /* If we are in a compound statement and find 'else' then
8216 something went wrong. */
8217 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8219 if (parser->in_statement & IN_IF_STMT)
8223 token = cp_lexer_consume_token (parser->lexer);
8224 error_at (token->location, "%<else%> without a previous %<if%>");
8228 /* Parse the statement. */
8229 cp_parser_statement (parser, in_statement_expr, true, NULL);
8233 /* Parse a selection-statement.
8235 selection-statement:
8236 if ( condition ) statement
8237 if ( condition ) statement else statement
8238 switch ( condition ) statement
8240 Returns the new IF_STMT or SWITCH_STMT.
8242 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8243 is a (possibly labeled) if statement which is not enclosed in
8244 braces and has an else clause. This is used to implement
8248 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8256 /* Peek at the next token. */
8257 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8259 /* See what kind of keyword it is. */
8260 keyword = token->keyword;
8269 /* Look for the `('. */
8270 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8272 cp_parser_skip_to_end_of_statement (parser);
8273 return error_mark_node;
8276 /* Begin the selection-statement. */
8277 if (keyword == RID_IF)
8278 statement = begin_if_stmt ();
8280 statement = begin_switch_stmt ();
8282 /* Parse the condition. */
8283 condition = cp_parser_condition (parser);
8284 /* Look for the `)'. */
8285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8286 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8287 /*consume_paren=*/true);
8289 if (keyword == RID_IF)
8292 unsigned char in_statement;
8294 /* Add the condition. */
8295 finish_if_stmt_cond (condition, statement);
8297 /* Parse the then-clause. */
8298 in_statement = parser->in_statement;
8299 parser->in_statement |= IN_IF_STMT;
8300 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8302 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8303 add_stmt (build_empty_stmt (loc));
8304 cp_lexer_consume_token (parser->lexer);
8305 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8306 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8307 "empty body in an %<if%> statement");
8311 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8312 parser->in_statement = in_statement;
8314 finish_then_clause (statement);
8316 /* If the next token is `else', parse the else-clause. */
8317 if (cp_lexer_next_token_is_keyword (parser->lexer,
8320 /* Consume the `else' keyword. */
8321 cp_lexer_consume_token (parser->lexer);
8322 begin_else_clause (statement);
8323 /* Parse the else-clause. */
8324 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8327 loc = cp_lexer_peek_token (parser->lexer)->location;
8329 OPT_Wempty_body, "suggest braces around "
8330 "empty body in an %<else%> statement");
8331 add_stmt (build_empty_stmt (loc));
8332 cp_lexer_consume_token (parser->lexer);
8335 cp_parser_implicitly_scoped_statement (parser, NULL);
8337 finish_else_clause (statement);
8339 /* If we are currently parsing a then-clause, then
8340 IF_P will not be NULL. We set it to true to
8341 indicate that this if statement has an else clause.
8342 This may trigger the Wparentheses warning below
8343 when we get back up to the parent if statement. */
8349 /* This if statement does not have an else clause. If
8350 NESTED_IF is true, then the then-clause is an if
8351 statement which does have an else clause. We warn
8352 about the potential ambiguity. */
8354 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8355 "suggest explicit braces to avoid ambiguous"
8359 /* Now we're all done with the if-statement. */
8360 finish_if_stmt (statement);
8364 bool in_switch_statement_p;
8365 unsigned char in_statement;
8367 /* Add the condition. */
8368 finish_switch_cond (condition, statement);
8370 /* Parse the body of the switch-statement. */
8371 in_switch_statement_p = parser->in_switch_statement_p;
8372 in_statement = parser->in_statement;
8373 parser->in_switch_statement_p = true;
8374 parser->in_statement |= IN_SWITCH_STMT;
8375 cp_parser_implicitly_scoped_statement (parser, NULL);
8376 parser->in_switch_statement_p = in_switch_statement_p;
8377 parser->in_statement = in_statement;
8379 /* Now we're all done with the switch-statement. */
8380 finish_switch_stmt (statement);
8388 cp_parser_error (parser, "expected selection-statement");
8389 return error_mark_node;
8393 /* Parse a condition.
8397 type-specifier-seq declarator = initializer-clause
8398 type-specifier-seq declarator braced-init-list
8403 type-specifier-seq declarator asm-specification [opt]
8404 attributes [opt] = assignment-expression
8406 Returns the expression that should be tested. */
8409 cp_parser_condition (cp_parser* parser)
8411 cp_decl_specifier_seq type_specifiers;
8412 const char *saved_message;
8413 int declares_class_or_enum;
8415 /* Try the declaration first. */
8416 cp_parser_parse_tentatively (parser);
8417 /* New types are not allowed in the type-specifier-seq for a
8419 saved_message = parser->type_definition_forbidden_message;
8420 parser->type_definition_forbidden_message
8421 = G_("types may not be defined in conditions");
8422 /* Parse the type-specifier-seq. */
8423 cp_parser_decl_specifier_seq (parser,
8424 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8426 &declares_class_or_enum);
8427 /* Restore the saved message. */
8428 parser->type_definition_forbidden_message = saved_message;
8429 /* If all is well, we might be looking at a declaration. */
8430 if (!cp_parser_error_occurred (parser))
8433 tree asm_specification;
8435 cp_declarator *declarator;
8436 tree initializer = NULL_TREE;
8438 /* Parse the declarator. */
8439 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8440 /*ctor_dtor_or_conv_p=*/NULL,
8441 /*parenthesized_p=*/NULL,
8442 /*member_p=*/false);
8443 /* Parse the attributes. */
8444 attributes = cp_parser_attributes_opt (parser);
8445 /* Parse the asm-specification. */
8446 asm_specification = cp_parser_asm_specification_opt (parser);
8447 /* If the next token is not an `=' or '{', then we might still be
8448 looking at an expression. For example:
8452 looks like a decl-specifier-seq and a declarator -- but then
8453 there is no `=', so this is an expression. */
8454 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8455 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8456 cp_parser_simulate_error (parser);
8458 /* If we did see an `=' or '{', then we are looking at a declaration
8460 if (cp_parser_parse_definitely (parser))
8463 bool non_constant_p;
8464 bool flags = LOOKUP_ONLYCONVERTING;
8466 /* Create the declaration. */
8467 decl = start_decl (declarator, &type_specifiers,
8468 /*initialized_p=*/true,
8469 attributes, /*prefix_attributes=*/NULL_TREE,
8472 /* Parse the initializer. */
8473 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8475 initializer = cp_parser_braced_list (parser, &non_constant_p);
8476 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8481 /* Consume the `='. */
8482 cp_parser_require (parser, CPP_EQ, RT_EQ);
8483 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8485 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8486 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8488 /* Process the initializer. */
8489 cp_finish_decl (decl,
8490 initializer, !non_constant_p,
8495 pop_scope (pushed_scope);
8497 return convert_from_reference (decl);
8500 /* If we didn't even get past the declarator successfully, we are
8501 definitely not looking at a declaration. */
8503 cp_parser_abort_tentative_parse (parser);
8505 /* Otherwise, we are looking at an expression. */
8506 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8509 /* Parses a for-statement or range-for-statement until the closing ')',
8513 cp_parser_for (cp_parser *parser)
8515 tree init, scope, decl;
8518 /* Begin the for-statement. */
8519 scope = begin_for_scope (&init);
8521 /* Parse the initialization. */
8522 is_range_for = cp_parser_for_init_statement (parser, &decl);
8525 return cp_parser_range_for (parser, scope, init, decl);
8527 return cp_parser_c_for (parser, scope, init);
8531 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8533 /* Normal for loop */
8534 tree condition = NULL_TREE;
8535 tree expression = NULL_TREE;
8538 stmt = begin_for_stmt (scope, init);
8539 /* The for-init-statement has already been parsed in
8540 cp_parser_for_init_statement, so no work is needed here. */
8541 finish_for_init_stmt (stmt);
8543 /* If there's a condition, process it. */
8544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8545 condition = cp_parser_condition (parser);
8546 finish_for_cond (condition, stmt);
8547 /* Look for the `;'. */
8548 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8550 /* If there's an expression, process it. */
8551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8552 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8553 finish_for_expr (expression, stmt);
8558 /* Tries to parse a range-based for-statement:
8561 decl-specifier-seq declarator : expression
8563 The decl-specifier-seq declarator and the `:' are already parsed by
8564 cp_parser_for_init_statement. If processing_template_decl it returns a
8565 newly created RANGE_FOR_STMT; if not, it is converted to a
8566 regular FOR_STMT. */
8569 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8571 tree stmt, range_expr;
8573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8575 bool expr_non_constant_p;
8576 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8579 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8581 /* If in template, STMT is converted to a normal for-statement
8582 at instantiation. If not, it is done just ahead. */
8583 if (processing_template_decl)
8585 stmt = begin_range_for_stmt (scope, init);
8586 finish_range_for_decl (stmt, range_decl, range_expr);
8590 stmt = begin_for_stmt (scope, init);
8591 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8596 /* Converts a range-based for-statement into a normal
8597 for-statement, as per the definition.
8599 for (RANGE_DECL : RANGE_EXPR)
8602 should be equivalent to:
8605 auto &&__range = RANGE_EXPR;
8606 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8610 RANGE_DECL = *__begin;
8615 If RANGE_EXPR is an array:
8616 BEGIN_EXPR = __range
8617 END_EXPR = __range + ARRAY_SIZE(__range)
8618 Else if RANGE_EXPR has a member 'begin' or 'end':
8619 BEGIN_EXPR = __range.begin()
8620 END_EXPR = __range.end()
8622 BEGIN_EXPR = begin(__range)
8623 END_EXPR = end(__range);
8625 If __range has a member 'begin' but not 'end', or vice versa, we must
8626 still use the second alternative (it will surely fail, however).
8627 When calling begin()/end() in the third alternative we must use
8628 argument dependent lookup, but always considering 'std' as an associated
8632 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8634 tree range_type, range_temp;
8636 tree iter_type, begin_expr, end_expr;
8637 tree condition, expression;
8639 if (range_decl == error_mark_node || range_expr == error_mark_node)
8640 /* If an error happened previously do nothing or else a lot of
8641 unhelpful errors would be issued. */
8642 begin_expr = end_expr = iter_type = error_mark_node;
8645 /* Find out the type deduced by the declaration
8646 `auto &&__range = range_expr'. */
8647 range_type = cp_build_reference_type (make_auto (), true);
8648 range_type = do_auto_deduction (range_type, range_expr,
8649 type_uses_auto (range_type));
8651 /* Create the __range variable. */
8652 range_temp = build_decl (input_location, VAR_DECL,
8653 get_identifier ("__for_range"), range_type);
8654 TREE_USED (range_temp) = 1;
8655 DECL_ARTIFICIAL (range_temp) = 1;
8656 pushdecl (range_temp);
8657 cp_finish_decl (range_temp, range_expr,
8658 /*is_constant_init*/false, NULL_TREE,
8659 LOOKUP_ONLYCONVERTING);
8661 range_temp = convert_from_reference (range_temp);
8662 iter_type = cp_parser_perform_range_for_lookup (range_temp,
8663 &begin_expr, &end_expr);
8666 /* The new for initialization statement. */
8667 begin = build_decl (input_location, VAR_DECL,
8668 get_identifier ("__for_begin"), iter_type);
8669 TREE_USED (begin) = 1;
8670 DECL_ARTIFICIAL (begin) = 1;
8672 cp_finish_decl (begin, begin_expr,
8673 /*is_constant_init*/false, NULL_TREE,
8674 LOOKUP_ONLYCONVERTING);
8676 end = build_decl (input_location, VAR_DECL,
8677 get_identifier ("__for_end"), iter_type);
8678 TREE_USED (end) = 1;
8679 DECL_ARTIFICIAL (end) = 1;
8681 cp_finish_decl (end, end_expr,
8682 /*is_constant_init*/false, NULL_TREE,
8683 LOOKUP_ONLYCONVERTING);
8685 finish_for_init_stmt (statement);
8687 /* The new for condition. */
8688 condition = build_x_binary_op (NE_EXPR,
8691 NULL, tf_warning_or_error);
8692 finish_for_cond (condition, statement);
8694 /* The new increment expression. */
8695 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8696 finish_for_expr (expression, statement);
8698 /* The declaration is initialized with *__begin inside the loop body. */
8699 cp_finish_decl (range_decl,
8700 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8701 /*is_constant_init*/false, NULL_TREE,
8702 LOOKUP_ONLYCONVERTING);
8707 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8708 We need to solve both at the same time because the method used
8709 depends on the existence of members begin or end.
8710 Returns the type deduced for the iterator expression. */
8713 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8715 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8717 error ("range-based %<for%> expression of type %qT "
8718 "has incomplete type", TREE_TYPE (range));
8719 *begin = *end = error_mark_node;
8720 return error_mark_node;
8722 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8724 /* If RANGE is an array, we will use pointer arithmetic. */
8726 *end = build_binary_op (input_location, PLUS_EXPR,
8728 array_type_nelts_top (TREE_TYPE (range)),
8730 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8734 /* If it is not an array, we must do a bit of magic. */
8735 tree id_begin, id_end;
8736 tree member_begin, member_end;
8738 *begin = *end = error_mark_node;
8740 id_begin = get_identifier ("begin");
8741 id_end = get_identifier ("end");
8742 member_begin = lookup_member (TREE_TYPE (range), id_begin,
8743 /*protect=*/2, /*want_type=*/false);
8744 member_end = lookup_member (TREE_TYPE (range), id_end,
8745 /*protect=*/2, /*want_type=*/false);
8747 if (member_begin != NULL_TREE || member_end != NULL_TREE)
8749 /* Use the member functions. */
8750 if (member_begin != NULL_TREE)
8751 *begin = cp_parser_range_for_member_function (range, id_begin);
8753 error ("range-based %<for%> expression of type %qT has an "
8754 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8756 if (member_end != NULL_TREE)
8757 *end = cp_parser_range_for_member_function (range, id_end);
8759 error ("range-based %<for%> expression of type %qT has a "
8760 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8764 /* Use global functions with ADL. */
8766 vec = make_tree_vector ();
8768 VEC_safe_push (tree, gc, vec, range);
8770 member_begin = perform_koenig_lookup (id_begin, vec,
8771 /*include_std=*/true,
8772 tf_warning_or_error);
8773 *begin = finish_call_expr (member_begin, &vec, false, true,
8774 tf_warning_or_error);
8775 member_end = perform_koenig_lookup (id_end, vec,
8776 /*include_std=*/true,
8777 tf_warning_or_error);
8778 *end = finish_call_expr (member_end, &vec, false, true,
8779 tf_warning_or_error);
8781 release_tree_vector (vec);
8784 /* Last common checks. */
8785 if (*begin == error_mark_node || *end == error_mark_node)
8787 /* If one of the expressions is an error do no more checks. */
8788 *begin = *end = error_mark_node;
8789 return error_mark_node;
8793 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8794 /* The unqualified type of the __begin and __end temporaries should
8795 be the same, as required by the multiple auto declaration. */
8796 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8797 error ("inconsistent begin/end types in range-based %<for%> "
8798 "statement: %qT and %qT",
8799 TREE_TYPE (*begin), TREE_TYPE (*end));
8805 /* Helper function for cp_parser_perform_range_for_lookup.
8806 Builds a tree for RANGE.IDENTIFIER(). */
8809 cp_parser_range_for_member_function (tree range, tree identifier)
8814 member = finish_class_member_access_expr (range, identifier,
8815 false, tf_warning_or_error);
8816 if (member == error_mark_node)
8817 return error_mark_node;
8819 vec = make_tree_vector ();
8820 res = finish_call_expr (member, &vec,
8821 /*disallow_virtual=*/false,
8823 tf_warning_or_error);
8824 release_tree_vector (vec);
8828 /* Parse an iteration-statement.
8830 iteration-statement:
8831 while ( condition ) statement
8832 do statement while ( expression ) ;
8833 for ( for-init-statement condition [opt] ; expression [opt] )
8836 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8839 cp_parser_iteration_statement (cp_parser* parser)
8844 unsigned char in_statement;
8846 /* Peek at the next token. */
8847 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8849 return error_mark_node;
8851 /* Remember whether or not we are already within an iteration
8853 in_statement = parser->in_statement;
8855 /* See what kind of keyword it is. */
8856 keyword = token->keyword;
8863 /* Begin the while-statement. */
8864 statement = begin_while_stmt ();
8865 /* Look for the `('. */
8866 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8867 /* Parse the condition. */
8868 condition = cp_parser_condition (parser);
8869 finish_while_stmt_cond (condition, statement);
8870 /* Look for the `)'. */
8871 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8872 /* Parse the dependent statement. */
8873 parser->in_statement = IN_ITERATION_STMT;
8874 cp_parser_already_scoped_statement (parser);
8875 parser->in_statement = in_statement;
8876 /* We're done with the while-statement. */
8877 finish_while_stmt (statement);
8885 /* Begin the do-statement. */
8886 statement = begin_do_stmt ();
8887 /* Parse the body of the do-statement. */
8888 parser->in_statement = IN_ITERATION_STMT;
8889 cp_parser_implicitly_scoped_statement (parser, NULL);
8890 parser->in_statement = in_statement;
8891 finish_do_body (statement);
8892 /* Look for the `while' keyword. */
8893 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8894 /* Look for the `('. */
8895 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8896 /* Parse the expression. */
8897 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8898 /* We're done with the do-statement. */
8899 finish_do_stmt (expression, statement);
8900 /* Look for the `)'. */
8901 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8902 /* Look for the `;'. */
8903 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8909 /* Look for the `('. */
8910 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8912 statement = cp_parser_for (parser);
8914 /* Look for the `)'. */
8915 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8917 /* Parse the body of the for-statement. */
8918 parser->in_statement = IN_ITERATION_STMT;
8919 cp_parser_already_scoped_statement (parser);
8920 parser->in_statement = in_statement;
8922 /* We're done with the for-statement. */
8923 finish_for_stmt (statement);
8928 cp_parser_error (parser, "expected iteration-statement");
8929 statement = error_mark_node;
8936 /* Parse a for-init-statement or the declarator of a range-based-for.
8937 Returns true if a range-based-for declaration is seen.
8940 expression-statement
8941 simple-declaration */
8944 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8946 /* If the next token is a `;', then we have an empty
8947 expression-statement. Grammatically, this is also a
8948 simple-declaration, but an invalid one, because it does not
8949 declare anything. Therefore, if we did not handle this case
8950 specially, we would issue an error message about an invalid
8952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8954 bool is_range_for = false;
8955 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8957 parser->colon_corrects_to_scope_p = false;
8959 /* We're going to speculatively look for a declaration, falling back
8960 to an expression, if necessary. */
8961 cp_parser_parse_tentatively (parser);
8962 /* Parse the declaration. */
8963 cp_parser_simple_declaration (parser,
8964 /*function_definition_allowed_p=*/false,
8966 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8967 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8969 /* It is a range-for, consume the ':' */
8970 cp_lexer_consume_token (parser->lexer);
8971 is_range_for = true;
8972 if (cxx_dialect < cxx0x)
8974 error_at (cp_lexer_peek_token (parser->lexer)->location,
8975 "range-based %<for%> loops are not allowed "
8977 *decl = error_mark_node;
8981 /* The ';' is not consumed yet because we told
8982 cp_parser_simple_declaration not to. */
8983 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8985 if (cp_parser_parse_definitely (parser))
8986 return is_range_for;
8987 /* If the tentative parse failed, then we shall need to look for an
8988 expression-statement. */
8990 /* If we are here, it is an expression-statement. */
8991 cp_parser_expression_statement (parser, NULL_TREE);
8995 /* Parse a jump-statement.
9000 return expression [opt] ;
9001 return braced-init-list ;
9009 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9012 cp_parser_jump_statement (cp_parser* parser)
9014 tree statement = error_mark_node;
9017 unsigned char in_statement;
9019 /* Peek at the next token. */
9020 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9022 return error_mark_node;
9024 /* See what kind of keyword it is. */
9025 keyword = token->keyword;
9029 in_statement = parser->in_statement & ~IN_IF_STMT;
9030 switch (in_statement)
9033 error_at (token->location, "break statement not within loop or switch");
9036 gcc_assert ((in_statement & IN_SWITCH_STMT)
9037 || in_statement == IN_ITERATION_STMT);
9038 statement = finish_break_stmt ();
9041 error_at (token->location, "invalid exit from OpenMP structured block");
9044 error_at (token->location, "break statement used with OpenMP for loop");
9047 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9051 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9054 error_at (token->location, "continue statement not within a loop");
9056 case IN_ITERATION_STMT:
9058 statement = finish_continue_stmt ();
9061 error_at (token->location, "invalid exit from OpenMP structured block");
9066 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9072 bool expr_non_constant_p;
9074 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9076 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9077 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9079 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9080 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9082 /* If the next token is a `;', then there is no
9085 /* Build the return-statement. */
9086 statement = finish_return_stmt (expr);
9087 /* Look for the final `;'. */
9088 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9093 /* Create the goto-statement. */
9094 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9096 /* Issue a warning about this use of a GNU extension. */
9097 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9098 /* Consume the '*' token. */
9099 cp_lexer_consume_token (parser->lexer);
9100 /* Parse the dependent expression. */
9101 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9104 finish_goto_stmt (cp_parser_identifier (parser));
9105 /* Look for the final `;'. */
9106 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9110 cp_parser_error (parser, "expected jump-statement");
9117 /* Parse a declaration-statement.
9119 declaration-statement:
9120 block-declaration */
9123 cp_parser_declaration_statement (cp_parser* parser)
9127 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9128 p = obstack_alloc (&declarator_obstack, 0);
9130 /* Parse the block-declaration. */
9131 cp_parser_block_declaration (parser, /*statement_p=*/true);
9133 /* Free any declarators allocated. */
9134 obstack_free (&declarator_obstack, p);
9136 /* Finish off the statement. */
9140 /* Some dependent statements (like `if (cond) statement'), are
9141 implicitly in their own scope. In other words, if the statement is
9142 a single statement (as opposed to a compound-statement), it is
9143 none-the-less treated as if it were enclosed in braces. Any
9144 declarations appearing in the dependent statement are out of scope
9145 after control passes that point. This function parses a statement,
9146 but ensures that is in its own scope, even if it is not a
9149 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9150 is a (possibly labeled) if statement which is not enclosed in
9151 braces and has an else clause. This is used to implement
9154 Returns the new statement. */
9157 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9164 /* Mark if () ; with a special NOP_EXPR. */
9165 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9167 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9168 cp_lexer_consume_token (parser->lexer);
9169 statement = add_stmt (build_empty_stmt (loc));
9171 /* if a compound is opened, we simply parse the statement directly. */
9172 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9173 statement = cp_parser_compound_statement (parser, NULL, false, false);
9174 /* If the token is not a `{', then we must take special action. */
9177 /* Create a compound-statement. */
9178 statement = begin_compound_stmt (0);
9179 /* Parse the dependent-statement. */
9180 cp_parser_statement (parser, NULL_TREE, false, if_p);
9181 /* Finish the dummy compound-statement. */
9182 finish_compound_stmt (statement);
9185 /* Return the statement. */
9189 /* For some dependent statements (like `while (cond) statement'), we
9190 have already created a scope. Therefore, even if the dependent
9191 statement is a compound-statement, we do not want to create another
9195 cp_parser_already_scoped_statement (cp_parser* parser)
9197 /* If the token is a `{', then we must take special action. */
9198 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9199 cp_parser_statement (parser, NULL_TREE, false, NULL);
9202 /* Avoid calling cp_parser_compound_statement, so that we
9203 don't create a new scope. Do everything else by hand. */
9204 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9205 /* If the next keyword is `__label__' we have a label declaration. */
9206 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9207 cp_parser_label_declaration (parser);
9208 /* Parse an (optional) statement-seq. */
9209 cp_parser_statement_seq_opt (parser, NULL_TREE);
9210 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9214 /* Declarations [gram.dcl.dcl] */
9216 /* Parse an optional declaration-sequence.
9220 declaration-seq declaration */
9223 cp_parser_declaration_seq_opt (cp_parser* parser)
9229 token = cp_lexer_peek_token (parser->lexer);
9231 if (token->type == CPP_CLOSE_BRACE
9232 || token->type == CPP_EOF
9233 || token->type == CPP_PRAGMA_EOL)
9236 if (token->type == CPP_SEMICOLON)
9238 /* A declaration consisting of a single semicolon is
9239 invalid. Allow it unless we're being pedantic. */
9240 cp_lexer_consume_token (parser->lexer);
9241 if (!in_system_header)
9242 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9246 /* If we're entering or exiting a region that's implicitly
9247 extern "C", modify the lang context appropriately. */
9248 if (!parser->implicit_extern_c && token->implicit_extern_c)
9250 push_lang_context (lang_name_c);
9251 parser->implicit_extern_c = true;
9253 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9255 pop_lang_context ();
9256 parser->implicit_extern_c = false;
9259 if (token->type == CPP_PRAGMA)
9261 /* A top-level declaration can consist solely of a #pragma.
9262 A nested declaration cannot, so this is done here and not
9263 in cp_parser_declaration. (A #pragma at block scope is
9264 handled in cp_parser_statement.) */
9265 cp_parser_pragma (parser, pragma_external);
9269 /* Parse the declaration itself. */
9270 cp_parser_declaration (parser);
9274 /* Parse a declaration.
9279 template-declaration
9280 explicit-instantiation
9281 explicit-specialization
9282 linkage-specification
9283 namespace-definition
9288 __extension__ declaration */
9291 cp_parser_declaration (cp_parser* parser)
9297 tree attributes = NULL_TREE;
9299 /* Check for the `__extension__' keyword. */
9300 if (cp_parser_extension_opt (parser, &saved_pedantic))
9302 /* Parse the qualified declaration. */
9303 cp_parser_declaration (parser);
9304 /* Restore the PEDANTIC flag. */
9305 pedantic = saved_pedantic;
9310 /* Try to figure out what kind of declaration is present. */
9311 token1 = *cp_lexer_peek_token (parser->lexer);
9313 if (token1.type != CPP_EOF)
9314 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9317 token2.type = CPP_EOF;
9318 token2.keyword = RID_MAX;
9321 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9322 p = obstack_alloc (&declarator_obstack, 0);
9324 /* If the next token is `extern' and the following token is a string
9325 literal, then we have a linkage specification. */
9326 if (token1.keyword == RID_EXTERN
9327 && cp_parser_is_string_literal (&token2))
9328 cp_parser_linkage_specification (parser);
9329 /* If the next token is `template', then we have either a template
9330 declaration, an explicit instantiation, or an explicit
9332 else if (token1.keyword == RID_TEMPLATE)
9334 /* `template <>' indicates a template specialization. */
9335 if (token2.type == CPP_LESS
9336 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9337 cp_parser_explicit_specialization (parser);
9338 /* `template <' indicates a template declaration. */
9339 else if (token2.type == CPP_LESS)
9340 cp_parser_template_declaration (parser, /*member_p=*/false);
9341 /* Anything else must be an explicit instantiation. */
9343 cp_parser_explicit_instantiation (parser);
9345 /* If the next token is `export', then we have a template
9347 else if (token1.keyword == RID_EXPORT)
9348 cp_parser_template_declaration (parser, /*member_p=*/false);
9349 /* If the next token is `extern', 'static' or 'inline' and the one
9350 after that is `template', we have a GNU extended explicit
9351 instantiation directive. */
9352 else if (cp_parser_allow_gnu_extensions_p (parser)
9353 && (token1.keyword == RID_EXTERN
9354 || token1.keyword == RID_STATIC
9355 || token1.keyword == RID_INLINE)
9356 && token2.keyword == RID_TEMPLATE)
9357 cp_parser_explicit_instantiation (parser);
9358 /* If the next token is `namespace', check for a named or unnamed
9359 namespace definition. */
9360 else if (token1.keyword == RID_NAMESPACE
9361 && (/* A named namespace definition. */
9362 (token2.type == CPP_NAME
9363 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9365 /* An unnamed namespace definition. */
9366 || token2.type == CPP_OPEN_BRACE
9367 || token2.keyword == RID_ATTRIBUTE))
9368 cp_parser_namespace_definition (parser);
9369 /* An inline (associated) namespace definition. */
9370 else if (token1.keyword == RID_INLINE
9371 && token2.keyword == RID_NAMESPACE)
9372 cp_parser_namespace_definition (parser);
9373 /* Objective-C++ declaration/definition. */
9374 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9375 cp_parser_objc_declaration (parser, NULL_TREE);
9376 else if (c_dialect_objc ()
9377 && token1.keyword == RID_ATTRIBUTE
9378 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9379 cp_parser_objc_declaration (parser, attributes);
9380 /* We must have either a block declaration or a function
9383 /* Try to parse a block-declaration, or a function-definition. */
9384 cp_parser_block_declaration (parser, /*statement_p=*/false);
9386 /* Free any declarators allocated. */
9387 obstack_free (&declarator_obstack, p);
9390 /* Parse a block-declaration.
9395 namespace-alias-definition
9402 __extension__ block-declaration
9407 static_assert-declaration
9409 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9410 part of a declaration-statement. */
9413 cp_parser_block_declaration (cp_parser *parser,
9419 /* Check for the `__extension__' keyword. */
9420 if (cp_parser_extension_opt (parser, &saved_pedantic))
9422 /* Parse the qualified declaration. */
9423 cp_parser_block_declaration (parser, statement_p);
9424 /* Restore the PEDANTIC flag. */
9425 pedantic = saved_pedantic;
9430 /* Peek at the next token to figure out which kind of declaration is
9432 token1 = cp_lexer_peek_token (parser->lexer);
9434 /* If the next keyword is `asm', we have an asm-definition. */
9435 if (token1->keyword == RID_ASM)
9438 cp_parser_commit_to_tentative_parse (parser);
9439 cp_parser_asm_definition (parser);
9441 /* If the next keyword is `namespace', we have a
9442 namespace-alias-definition. */
9443 else if (token1->keyword == RID_NAMESPACE)
9444 cp_parser_namespace_alias_definition (parser);
9445 /* If the next keyword is `using', we have either a
9446 using-declaration or a using-directive. */
9447 else if (token1->keyword == RID_USING)
9452 cp_parser_commit_to_tentative_parse (parser);
9453 /* If the token after `using' is `namespace', then we have a
9455 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9456 if (token2->keyword == RID_NAMESPACE)
9457 cp_parser_using_directive (parser);
9458 /* Otherwise, it's a using-declaration. */
9460 cp_parser_using_declaration (parser,
9461 /*access_declaration_p=*/false);
9463 /* If the next keyword is `__label__' we have a misplaced label
9465 else if (token1->keyword == RID_LABEL)
9467 cp_lexer_consume_token (parser->lexer);
9468 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9469 cp_parser_skip_to_end_of_statement (parser);
9470 /* If the next token is now a `;', consume it. */
9471 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9472 cp_lexer_consume_token (parser->lexer);
9474 /* If the next token is `static_assert' we have a static assertion. */
9475 else if (token1->keyword == RID_STATIC_ASSERT)
9476 cp_parser_static_assert (parser, /*member_p=*/false);
9477 /* Anything else must be a simple-declaration. */
9479 cp_parser_simple_declaration (parser, !statement_p,
9480 /*maybe_range_for_decl*/NULL);
9483 /* Parse a simple-declaration.
9486 decl-specifier-seq [opt] init-declarator-list [opt] ;
9488 init-declarator-list:
9490 init-declarator-list , init-declarator
9492 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9493 function-definition as a simple-declaration.
9495 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9496 parsed declaration if it is an uninitialized single declarator not followed
9497 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9498 if present, will not be consumed. */
9501 cp_parser_simple_declaration (cp_parser* parser,
9502 bool function_definition_allowed_p,
9503 tree *maybe_range_for_decl)
9505 cp_decl_specifier_seq decl_specifiers;
9506 int declares_class_or_enum;
9507 bool saw_declarator;
9509 if (maybe_range_for_decl)
9510 *maybe_range_for_decl = NULL_TREE;
9512 /* Defer access checks until we know what is being declared; the
9513 checks for names appearing in the decl-specifier-seq should be
9514 done as if we were in the scope of the thing being declared. */
9515 push_deferring_access_checks (dk_deferred);
9517 /* Parse the decl-specifier-seq. We have to keep track of whether
9518 or not the decl-specifier-seq declares a named class or
9519 enumeration type, since that is the only case in which the
9520 init-declarator-list is allowed to be empty.
9524 In a simple-declaration, the optional init-declarator-list can be
9525 omitted only when declaring a class or enumeration, that is when
9526 the decl-specifier-seq contains either a class-specifier, an
9527 elaborated-type-specifier, or an enum-specifier. */
9528 cp_parser_decl_specifier_seq (parser,
9529 CP_PARSER_FLAGS_OPTIONAL,
9531 &declares_class_or_enum);
9532 /* We no longer need to defer access checks. */
9533 stop_deferring_access_checks ();
9535 /* In a block scope, a valid declaration must always have a
9536 decl-specifier-seq. By not trying to parse declarators, we can
9537 resolve the declaration/expression ambiguity more quickly. */
9538 if (!function_definition_allowed_p
9539 && !decl_specifiers.any_specifiers_p)
9541 cp_parser_error (parser, "expected declaration");
9545 /* If the next two tokens are both identifiers, the code is
9546 erroneous. The usual cause of this situation is code like:
9550 where "T" should name a type -- but does not. */
9551 if (!decl_specifiers.any_type_specifiers_p
9552 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9554 /* If parsing tentatively, we should commit; we really are
9555 looking at a declaration. */
9556 cp_parser_commit_to_tentative_parse (parser);
9561 /* If we have seen at least one decl-specifier, and the next token
9562 is not a parenthesis, then we must be looking at a declaration.
9563 (After "int (" we might be looking at a functional cast.) */
9564 if (decl_specifiers.any_specifiers_p
9565 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9566 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9567 && !cp_parser_error_occurred (parser))
9568 cp_parser_commit_to_tentative_parse (parser);
9570 /* Keep going until we hit the `;' at the end of the simple
9572 saw_declarator = false;
9573 while (cp_lexer_next_token_is_not (parser->lexer,
9577 bool function_definition_p;
9582 /* If we are processing next declarator, coma is expected */
9583 token = cp_lexer_peek_token (parser->lexer);
9584 gcc_assert (token->type == CPP_COMMA);
9585 cp_lexer_consume_token (parser->lexer);
9586 if (maybe_range_for_decl)
9587 *maybe_range_for_decl = error_mark_node;
9590 saw_declarator = true;
9592 /* Parse the init-declarator. */
9593 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9595 function_definition_allowed_p,
9597 declares_class_or_enum,
9598 &function_definition_p,
9599 maybe_range_for_decl);
9600 /* If an error occurred while parsing tentatively, exit quickly.
9601 (That usually happens when in the body of a function; each
9602 statement is treated as a declaration-statement until proven
9604 if (cp_parser_error_occurred (parser))
9606 /* Handle function definitions specially. */
9607 if (function_definition_p)
9609 /* If the next token is a `,', then we are probably
9610 processing something like:
9614 which is erroneous. */
9615 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9617 cp_token *token = cp_lexer_peek_token (parser->lexer);
9618 error_at (token->location,
9620 " declarations and function-definitions is forbidden");
9622 /* Otherwise, we're done with the list of declarators. */
9625 pop_deferring_access_checks ();
9629 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9630 *maybe_range_for_decl = decl;
9631 /* The next token should be either a `,' or a `;'. */
9632 token = cp_lexer_peek_token (parser->lexer);
9633 /* If it's a `,', there are more declarators to come. */
9634 if (token->type == CPP_COMMA)
9635 /* will be consumed next time around */;
9636 /* If it's a `;', we are done. */
9637 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9639 /* Anything else is an error. */
9642 /* If we have already issued an error message we don't need
9643 to issue another one. */
9644 if (decl != error_mark_node
9645 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9646 cp_parser_error (parser, "expected %<,%> or %<;%>");
9647 /* Skip tokens until we reach the end of the statement. */
9648 cp_parser_skip_to_end_of_statement (parser);
9649 /* If the next token is now a `;', consume it. */
9650 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9651 cp_lexer_consume_token (parser->lexer);
9654 /* After the first time around, a function-definition is not
9655 allowed -- even if it was OK at first. For example:
9660 function_definition_allowed_p = false;
9663 /* Issue an error message if no declarators are present, and the
9664 decl-specifier-seq does not itself declare a class or
9666 if (!saw_declarator)
9668 if (cp_parser_declares_only_class_p (parser))
9669 shadow_tag (&decl_specifiers);
9670 /* Perform any deferred access checks. */
9671 perform_deferred_access_checks ();
9674 /* Consume the `;'. */
9675 if (!maybe_range_for_decl)
9676 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9679 pop_deferring_access_checks ();
9682 /* Parse a decl-specifier-seq.
9685 decl-specifier-seq [opt] decl-specifier
9688 storage-class-specifier
9699 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9701 The parser flags FLAGS is used to control type-specifier parsing.
9703 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9706 1: one of the decl-specifiers is an elaborated-type-specifier
9707 (i.e., a type declaration)
9708 2: one of the decl-specifiers is an enum-specifier or a
9709 class-specifier (i.e., a type definition)
9714 cp_parser_decl_specifier_seq (cp_parser* parser,
9715 cp_parser_flags flags,
9716 cp_decl_specifier_seq *decl_specs,
9717 int* declares_class_or_enum)
9719 bool constructor_possible_p = !parser->in_declarator_p;
9720 cp_token *start_token = NULL;
9722 /* Clear DECL_SPECS. */
9723 clear_decl_specs (decl_specs);
9725 /* Assume no class or enumeration type is declared. */
9726 *declares_class_or_enum = 0;
9728 /* Keep reading specifiers until there are no more to read. */
9732 bool found_decl_spec;
9735 /* Peek at the next token. */
9736 token = cp_lexer_peek_token (parser->lexer);
9738 /* Save the first token of the decl spec list for error
9741 start_token = token;
9742 /* Handle attributes. */
9743 if (token->keyword == RID_ATTRIBUTE)
9745 /* Parse the attributes. */
9746 decl_specs->attributes
9747 = chainon (decl_specs->attributes,
9748 cp_parser_attributes_opt (parser));
9751 /* Assume we will find a decl-specifier keyword. */
9752 found_decl_spec = true;
9753 /* If the next token is an appropriate keyword, we can simply
9754 add it to the list. */
9755 switch (token->keyword)
9761 if (!at_class_scope_p ())
9763 error_at (token->location, "%<friend%> used outside of class");
9764 cp_lexer_purge_token (parser->lexer);
9768 ++decl_specs->specs[(int) ds_friend];
9769 /* Consume the token. */
9770 cp_lexer_consume_token (parser->lexer);
9775 ++decl_specs->specs[(int) ds_constexpr];
9776 cp_lexer_consume_token (parser->lexer);
9779 /* function-specifier:
9786 cp_parser_function_specifier_opt (parser, decl_specs);
9792 ++decl_specs->specs[(int) ds_typedef];
9793 /* Consume the token. */
9794 cp_lexer_consume_token (parser->lexer);
9795 /* A constructor declarator cannot appear in a typedef. */
9796 constructor_possible_p = false;
9797 /* The "typedef" keyword can only occur in a declaration; we
9798 may as well commit at this point. */
9799 cp_parser_commit_to_tentative_parse (parser);
9801 if (decl_specs->storage_class != sc_none)
9802 decl_specs->conflicting_specifiers_p = true;
9805 /* storage-class-specifier:
9815 if (cxx_dialect == cxx98)
9817 /* Consume the token. */
9818 cp_lexer_consume_token (parser->lexer);
9820 /* Complain about `auto' as a storage specifier, if
9821 we're complaining about C++0x compatibility. */
9822 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9823 " will change meaning in C++0x; please remove it");
9825 /* Set the storage class anyway. */
9826 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9830 /* C++0x auto type-specifier. */
9831 found_decl_spec = false;
9838 /* Consume the token. */
9839 cp_lexer_consume_token (parser->lexer);
9840 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9844 /* Consume the token. */
9845 cp_lexer_consume_token (parser->lexer);
9846 ++decl_specs->specs[(int) ds_thread];
9850 /* We did not yet find a decl-specifier yet. */
9851 found_decl_spec = false;
9856 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9857 && token->keyword != RID_CONSTEXPR)
9858 error ("decl-specifier invalid in condition");
9860 /* Constructors are a special case. The `S' in `S()' is not a
9861 decl-specifier; it is the beginning of the declarator. */
9864 && constructor_possible_p
9865 && (cp_parser_constructor_declarator_p
9866 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9868 /* If we don't have a DECL_SPEC yet, then we must be looking at
9869 a type-specifier. */
9870 if (!found_decl_spec && !constructor_p)
9872 int decl_spec_declares_class_or_enum;
9873 bool is_cv_qualifier;
9877 = cp_parser_type_specifier (parser, flags,
9879 /*is_declaration=*/true,
9880 &decl_spec_declares_class_or_enum,
9882 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9884 /* If this type-specifier referenced a user-defined type
9885 (a typedef, class-name, etc.), then we can't allow any
9886 more such type-specifiers henceforth.
9890 The longest sequence of decl-specifiers that could
9891 possibly be a type name is taken as the
9892 decl-specifier-seq of a declaration. The sequence shall
9893 be self-consistent as described below.
9897 As a general rule, at most one type-specifier is allowed
9898 in the complete decl-specifier-seq of a declaration. The
9899 only exceptions are the following:
9901 -- const or volatile can be combined with any other
9904 -- signed or unsigned can be combined with char, long,
9912 void g (const int Pc);
9914 Here, Pc is *not* part of the decl-specifier seq; it's
9915 the declarator. Therefore, once we see a type-specifier
9916 (other than a cv-qualifier), we forbid any additional
9917 user-defined types. We *do* still allow things like `int
9918 int' to be considered a decl-specifier-seq, and issue the
9919 error message later. */
9920 if (type_spec && !is_cv_qualifier)
9921 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9922 /* A constructor declarator cannot follow a type-specifier. */
9925 constructor_possible_p = false;
9926 found_decl_spec = true;
9927 if (!is_cv_qualifier)
9928 decl_specs->any_type_specifiers_p = true;
9932 /* If we still do not have a DECL_SPEC, then there are no more
9934 if (!found_decl_spec)
9937 decl_specs->any_specifiers_p = true;
9938 /* After we see one decl-specifier, further decl-specifiers are
9940 flags |= CP_PARSER_FLAGS_OPTIONAL;
9943 cp_parser_check_decl_spec (decl_specs, start_token->location);
9945 /* Don't allow a friend specifier with a class definition. */
9946 if (decl_specs->specs[(int) ds_friend] != 0
9947 && (*declares_class_or_enum & 2))
9948 error_at (start_token->location,
9949 "class definition may not be declared a friend");
9952 /* Parse an (optional) storage-class-specifier.
9954 storage-class-specifier:
9963 storage-class-specifier:
9966 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9969 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9971 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9974 if (cxx_dialect != cxx98)
9976 /* Fall through for C++98. */
9983 /* Consume the token. */
9984 return cp_lexer_consume_token (parser->lexer)->u.value;
9991 /* Parse an (optional) function-specifier.
9998 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9999 Updates DECL_SPECS, if it is non-NULL. */
10002 cp_parser_function_specifier_opt (cp_parser* parser,
10003 cp_decl_specifier_seq *decl_specs)
10005 cp_token *token = cp_lexer_peek_token (parser->lexer);
10006 switch (token->keyword)
10010 ++decl_specs->specs[(int) ds_inline];
10014 /* 14.5.2.3 [temp.mem]
10016 A member function template shall not be virtual. */
10017 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10018 error_at (token->location, "templates may not be %<virtual%>");
10019 else if (decl_specs)
10020 ++decl_specs->specs[(int) ds_virtual];
10025 ++decl_specs->specs[(int) ds_explicit];
10032 /* Consume the token. */
10033 return cp_lexer_consume_token (parser->lexer)->u.value;
10036 /* Parse a linkage-specification.
10038 linkage-specification:
10039 extern string-literal { declaration-seq [opt] }
10040 extern string-literal declaration */
10043 cp_parser_linkage_specification (cp_parser* parser)
10047 /* Look for the `extern' keyword. */
10048 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10050 /* Look for the string-literal. */
10051 linkage = cp_parser_string_literal (parser, false, false);
10053 /* Transform the literal into an identifier. If the literal is a
10054 wide-character string, or contains embedded NULs, then we can't
10055 handle it as the user wants. */
10056 if (strlen (TREE_STRING_POINTER (linkage))
10057 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10059 cp_parser_error (parser, "invalid linkage-specification");
10060 /* Assume C++ linkage. */
10061 linkage = lang_name_cplusplus;
10064 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10066 /* We're now using the new linkage. */
10067 push_lang_context (linkage);
10069 /* If the next token is a `{', then we're using the first
10071 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10073 /* Consume the `{' token. */
10074 cp_lexer_consume_token (parser->lexer);
10075 /* Parse the declarations. */
10076 cp_parser_declaration_seq_opt (parser);
10077 /* Look for the closing `}'. */
10078 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10080 /* Otherwise, there's just one declaration. */
10083 bool saved_in_unbraced_linkage_specification_p;
10085 saved_in_unbraced_linkage_specification_p
10086 = parser->in_unbraced_linkage_specification_p;
10087 parser->in_unbraced_linkage_specification_p = true;
10088 cp_parser_declaration (parser);
10089 parser->in_unbraced_linkage_specification_p
10090 = saved_in_unbraced_linkage_specification_p;
10093 /* We're done with the linkage-specification. */
10094 pop_lang_context ();
10097 /* Parse a static_assert-declaration.
10099 static_assert-declaration:
10100 static_assert ( constant-expression , string-literal ) ;
10102 If MEMBER_P, this static_assert is a class member. */
10105 cp_parser_static_assert(cp_parser *parser, bool member_p)
10110 location_t saved_loc;
10113 /* Peek at the `static_assert' token so we can keep track of exactly
10114 where the static assertion started. */
10115 token = cp_lexer_peek_token (parser->lexer);
10116 saved_loc = token->location;
10118 /* Look for the `static_assert' keyword. */
10119 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10123 /* We know we are in a static assertion; commit to any tentative
10125 if (cp_parser_parsing_tentatively (parser))
10126 cp_parser_commit_to_tentative_parse (parser);
10128 /* Parse the `(' starting the static assertion condition. */
10129 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10131 /* Parse the constant-expression. Allow a non-constant expression
10132 here in order to give better diagnostics in finish_static_assert. */
10134 cp_parser_constant_expression (parser,
10135 /*allow_non_constant_p=*/true,
10136 /*non_constant_p=*/&dummy);
10138 /* Parse the separating `,'. */
10139 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10141 /* Parse the string-literal message. */
10142 message = cp_parser_string_literal (parser,
10143 /*translate=*/false,
10146 /* A `)' completes the static assertion. */
10147 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10148 cp_parser_skip_to_closing_parenthesis (parser,
10149 /*recovering=*/true,
10150 /*or_comma=*/false,
10151 /*consume_paren=*/true);
10153 /* A semicolon terminates the declaration. */
10154 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10156 /* Complete the static assertion, which may mean either processing
10157 the static assert now or saving it for template instantiation. */
10158 finish_static_assert (condition, message, saved_loc, member_p);
10161 /* Parse a `decltype' type. Returns the type.
10163 simple-type-specifier:
10164 decltype ( expression ) */
10167 cp_parser_decltype (cp_parser *parser)
10170 bool id_expression_or_member_access_p = false;
10171 const char *saved_message;
10172 bool saved_integral_constant_expression_p;
10173 bool saved_non_integral_constant_expression_p;
10174 cp_token *id_expr_start_token;
10176 /* Look for the `decltype' token. */
10177 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10178 return error_mark_node;
10180 /* Types cannot be defined in a `decltype' expression. Save away the
10182 saved_message = parser->type_definition_forbidden_message;
10184 /* And create the new one. */
10185 parser->type_definition_forbidden_message
10186 = G_("types may not be defined in %<decltype%> expressions");
10188 /* The restrictions on constant-expressions do not apply inside
10189 decltype expressions. */
10190 saved_integral_constant_expression_p
10191 = parser->integral_constant_expression_p;
10192 saved_non_integral_constant_expression_p
10193 = parser->non_integral_constant_expression_p;
10194 parser->integral_constant_expression_p = false;
10196 /* Do not actually evaluate the expression. */
10197 ++cp_unevaluated_operand;
10199 /* Do not warn about problems with the expression. */
10200 ++c_inhibit_evaluation_warnings;
10202 /* Parse the opening `('. */
10203 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10204 return error_mark_node;
10206 /* First, try parsing an id-expression. */
10207 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10208 cp_parser_parse_tentatively (parser);
10209 expr = cp_parser_id_expression (parser,
10210 /*template_keyword_p=*/false,
10211 /*check_dependency_p=*/true,
10212 /*template_p=*/NULL,
10213 /*declarator_p=*/false,
10214 /*optional_p=*/false);
10216 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10218 bool non_integral_constant_expression_p = false;
10219 tree id_expression = expr;
10221 const char *error_msg;
10223 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10224 /* Lookup the name we got back from the id-expression. */
10225 expr = cp_parser_lookup_name (parser, expr,
10227 /*is_template=*/false,
10228 /*is_namespace=*/false,
10229 /*check_dependency=*/true,
10230 /*ambiguous_decls=*/NULL,
10231 id_expr_start_token->location);
10234 && expr != error_mark_node
10235 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10236 && TREE_CODE (expr) != TYPE_DECL
10237 && (TREE_CODE (expr) != BIT_NOT_EXPR
10238 || !TYPE_P (TREE_OPERAND (expr, 0)))
10239 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10241 /* Complete lookup of the id-expression. */
10242 expr = (finish_id_expression
10243 (id_expression, expr, parser->scope, &idk,
10244 /*integral_constant_expression_p=*/false,
10245 /*allow_non_integral_constant_expression_p=*/true,
10246 &non_integral_constant_expression_p,
10247 /*template_p=*/false,
10249 /*address_p=*/false,
10250 /*template_arg_p=*/false,
10252 id_expr_start_token->location));
10254 if (expr == error_mark_node)
10255 /* We found an id-expression, but it was something that we
10256 should not have found. This is an error, not something
10257 we can recover from, so note that we found an
10258 id-expression and we'll recover as gracefully as
10260 id_expression_or_member_access_p = true;
10264 && expr != error_mark_node
10265 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10266 /* We have an id-expression. */
10267 id_expression_or_member_access_p = true;
10270 if (!id_expression_or_member_access_p)
10272 /* Abort the id-expression parse. */
10273 cp_parser_abort_tentative_parse (parser);
10275 /* Parsing tentatively, again. */
10276 cp_parser_parse_tentatively (parser);
10278 /* Parse a class member access. */
10279 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10281 /*member_access_only_p=*/true, NULL);
10284 && expr != error_mark_node
10285 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10286 /* We have an id-expression. */
10287 id_expression_or_member_access_p = true;
10290 if (id_expression_or_member_access_p)
10291 /* We have parsed the complete id-expression or member access. */
10292 cp_parser_parse_definitely (parser);
10295 bool saved_greater_than_is_operator_p;
10297 /* Abort our attempt to parse an id-expression or member access
10299 cp_parser_abort_tentative_parse (parser);
10301 /* Within a parenthesized expression, a `>' token is always
10302 the greater-than operator. */
10303 saved_greater_than_is_operator_p
10304 = parser->greater_than_is_operator_p;
10305 parser->greater_than_is_operator_p = true;
10307 /* Parse a full expression. */
10308 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10310 /* The `>' token might be the end of a template-id or
10311 template-parameter-list now. */
10312 parser->greater_than_is_operator_p
10313 = saved_greater_than_is_operator_p;
10316 /* Go back to evaluating expressions. */
10317 --cp_unevaluated_operand;
10318 --c_inhibit_evaluation_warnings;
10320 /* Restore the old message and the integral constant expression
10322 parser->type_definition_forbidden_message = saved_message;
10323 parser->integral_constant_expression_p
10324 = saved_integral_constant_expression_p;
10325 parser->non_integral_constant_expression_p
10326 = saved_non_integral_constant_expression_p;
10328 if (expr == error_mark_node)
10330 /* Skip everything up to the closing `)'. */
10331 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10332 /*consume_paren=*/true);
10333 return error_mark_node;
10336 /* Parse to the closing `)'. */
10337 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10339 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10340 /*consume_paren=*/true);
10341 return error_mark_node;
10344 return finish_decltype_type (expr, id_expression_or_member_access_p,
10345 tf_warning_or_error);
10348 /* Special member functions [gram.special] */
10350 /* Parse a conversion-function-id.
10352 conversion-function-id:
10353 operator conversion-type-id
10355 Returns an IDENTIFIER_NODE representing the operator. */
10358 cp_parser_conversion_function_id (cp_parser* parser)
10362 tree saved_qualifying_scope;
10363 tree saved_object_scope;
10364 tree pushed_scope = NULL_TREE;
10366 /* Look for the `operator' token. */
10367 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10368 return error_mark_node;
10369 /* When we parse the conversion-type-id, the current scope will be
10370 reset. However, we need that information in able to look up the
10371 conversion function later, so we save it here. */
10372 saved_scope = parser->scope;
10373 saved_qualifying_scope = parser->qualifying_scope;
10374 saved_object_scope = parser->object_scope;
10375 /* We must enter the scope of the class so that the names of
10376 entities declared within the class are available in the
10377 conversion-type-id. For example, consider:
10384 S::operator I() { ... }
10386 In order to see that `I' is a type-name in the definition, we
10387 must be in the scope of `S'. */
10389 pushed_scope = push_scope (saved_scope);
10390 /* Parse the conversion-type-id. */
10391 type = cp_parser_conversion_type_id (parser);
10392 /* Leave the scope of the class, if any. */
10394 pop_scope (pushed_scope);
10395 /* Restore the saved scope. */
10396 parser->scope = saved_scope;
10397 parser->qualifying_scope = saved_qualifying_scope;
10398 parser->object_scope = saved_object_scope;
10399 /* If the TYPE is invalid, indicate failure. */
10400 if (type == error_mark_node)
10401 return error_mark_node;
10402 return mangle_conv_op_name_for_type (type);
10405 /* Parse a conversion-type-id:
10407 conversion-type-id:
10408 type-specifier-seq conversion-declarator [opt]
10410 Returns the TYPE specified. */
10413 cp_parser_conversion_type_id (cp_parser* parser)
10416 cp_decl_specifier_seq type_specifiers;
10417 cp_declarator *declarator;
10418 tree type_specified;
10420 /* Parse the attributes. */
10421 attributes = cp_parser_attributes_opt (parser);
10422 /* Parse the type-specifiers. */
10423 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10424 /*is_trailing_return=*/false,
10426 /* If that didn't work, stop. */
10427 if (type_specifiers.type == error_mark_node)
10428 return error_mark_node;
10429 /* Parse the conversion-declarator. */
10430 declarator = cp_parser_conversion_declarator_opt (parser);
10432 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10433 /*initialized=*/0, &attributes);
10435 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10437 /* Don't give this error when parsing tentatively. This happens to
10438 work because we always parse this definitively once. */
10439 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10440 && type_uses_auto (type_specified))
10442 error ("invalid use of %<auto%> in conversion operator");
10443 return error_mark_node;
10446 return type_specified;
10449 /* Parse an (optional) conversion-declarator.
10451 conversion-declarator:
10452 ptr-operator conversion-declarator [opt]
10456 static cp_declarator *
10457 cp_parser_conversion_declarator_opt (cp_parser* parser)
10459 enum tree_code code;
10461 cp_cv_quals cv_quals;
10463 /* We don't know if there's a ptr-operator next, or not. */
10464 cp_parser_parse_tentatively (parser);
10465 /* Try the ptr-operator. */
10466 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10467 /* If it worked, look for more conversion-declarators. */
10468 if (cp_parser_parse_definitely (parser))
10470 cp_declarator *declarator;
10472 /* Parse another optional declarator. */
10473 declarator = cp_parser_conversion_declarator_opt (parser);
10475 return cp_parser_make_indirect_declarator
10476 (code, class_type, cv_quals, declarator);
10482 /* Parse an (optional) ctor-initializer.
10485 : mem-initializer-list
10487 Returns TRUE iff the ctor-initializer was actually present. */
10490 cp_parser_ctor_initializer_opt (cp_parser* parser)
10492 /* If the next token is not a `:', then there is no
10493 ctor-initializer. */
10494 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10496 /* Do default initialization of any bases and members. */
10497 if (DECL_CONSTRUCTOR_P (current_function_decl))
10498 finish_mem_initializers (NULL_TREE);
10503 /* Consume the `:' token. */
10504 cp_lexer_consume_token (parser->lexer);
10505 /* And the mem-initializer-list. */
10506 cp_parser_mem_initializer_list (parser);
10511 /* Parse a mem-initializer-list.
10513 mem-initializer-list:
10514 mem-initializer ... [opt]
10515 mem-initializer ... [opt] , mem-initializer-list */
10518 cp_parser_mem_initializer_list (cp_parser* parser)
10520 tree mem_initializer_list = NULL_TREE;
10521 cp_token *token = cp_lexer_peek_token (parser->lexer);
10523 /* Let the semantic analysis code know that we are starting the
10524 mem-initializer-list. */
10525 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10526 error_at (token->location,
10527 "only constructors take member initializers");
10529 /* Loop through the list. */
10532 tree mem_initializer;
10534 token = cp_lexer_peek_token (parser->lexer);
10535 /* Parse the mem-initializer. */
10536 mem_initializer = cp_parser_mem_initializer (parser);
10537 /* If the next token is a `...', we're expanding member initializers. */
10538 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10540 /* Consume the `...'. */
10541 cp_lexer_consume_token (parser->lexer);
10543 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10544 can be expanded but members cannot. */
10545 if (mem_initializer != error_mark_node
10546 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10548 error_at (token->location,
10549 "cannot expand initializer for member %<%D%>",
10550 TREE_PURPOSE (mem_initializer));
10551 mem_initializer = error_mark_node;
10554 /* Construct the pack expansion type. */
10555 if (mem_initializer != error_mark_node)
10556 mem_initializer = make_pack_expansion (mem_initializer);
10558 /* Add it to the list, unless it was erroneous. */
10559 if (mem_initializer != error_mark_node)
10561 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10562 mem_initializer_list = mem_initializer;
10564 /* If the next token is not a `,', we're done. */
10565 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10567 /* Consume the `,' token. */
10568 cp_lexer_consume_token (parser->lexer);
10571 /* Perform semantic analysis. */
10572 if (DECL_CONSTRUCTOR_P (current_function_decl))
10573 finish_mem_initializers (mem_initializer_list);
10576 /* Parse a mem-initializer.
10579 mem-initializer-id ( expression-list [opt] )
10580 mem-initializer-id braced-init-list
10585 ( expression-list [opt] )
10587 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10588 class) or FIELD_DECL (for a non-static data member) to initialize;
10589 the TREE_VALUE is the expression-list. An empty initialization
10590 list is represented by void_list_node. */
10593 cp_parser_mem_initializer (cp_parser* parser)
10595 tree mem_initializer_id;
10596 tree expression_list;
10598 cp_token *token = cp_lexer_peek_token (parser->lexer);
10600 /* Find out what is being initialized. */
10601 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10603 permerror (token->location,
10604 "anachronistic old-style base class initializer");
10605 mem_initializer_id = NULL_TREE;
10609 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10610 if (mem_initializer_id == error_mark_node)
10611 return mem_initializer_id;
10613 member = expand_member_init (mem_initializer_id);
10614 if (member && !DECL_P (member))
10615 in_base_initializer = 1;
10617 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10619 bool expr_non_constant_p;
10620 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10621 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10622 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10623 expression_list = build_tree_list (NULL_TREE, expression_list);
10628 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10630 /*allow_expansion_p=*/true,
10631 /*non_constant_p=*/NULL);
10633 return error_mark_node;
10634 expression_list = build_tree_list_vec (vec);
10635 release_tree_vector (vec);
10638 if (expression_list == error_mark_node)
10639 return error_mark_node;
10640 if (!expression_list)
10641 expression_list = void_type_node;
10643 in_base_initializer = 0;
10645 return member ? build_tree_list (member, expression_list) : error_mark_node;
10648 /* Parse a mem-initializer-id.
10650 mem-initializer-id:
10651 :: [opt] nested-name-specifier [opt] class-name
10654 Returns a TYPE indicating the class to be initializer for the first
10655 production. Returns an IDENTIFIER_NODE indicating the data member
10656 to be initialized for the second production. */
10659 cp_parser_mem_initializer_id (cp_parser* parser)
10661 bool global_scope_p;
10662 bool nested_name_specifier_p;
10663 bool template_p = false;
10666 cp_token *token = cp_lexer_peek_token (parser->lexer);
10668 /* `typename' is not allowed in this context ([temp.res]). */
10669 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10671 error_at (token->location,
10672 "keyword %<typename%> not allowed in this context (a qualified "
10673 "member initializer is implicitly a type)");
10674 cp_lexer_consume_token (parser->lexer);
10676 /* Look for the optional `::' operator. */
10678 = (cp_parser_global_scope_opt (parser,
10679 /*current_scope_valid_p=*/false)
10681 /* Look for the optional nested-name-specifier. The simplest way to
10686 The keyword `typename' is not permitted in a base-specifier or
10687 mem-initializer; in these contexts a qualified name that
10688 depends on a template-parameter is implicitly assumed to be a
10691 is to assume that we have seen the `typename' keyword at this
10693 nested_name_specifier_p
10694 = (cp_parser_nested_name_specifier_opt (parser,
10695 /*typename_keyword_p=*/true,
10696 /*check_dependency_p=*/true,
10698 /*is_declaration=*/true)
10700 if (nested_name_specifier_p)
10701 template_p = cp_parser_optional_template_keyword (parser);
10702 /* If there is a `::' operator or a nested-name-specifier, then we
10703 are definitely looking for a class-name. */
10704 if (global_scope_p || nested_name_specifier_p)
10705 return cp_parser_class_name (parser,
10706 /*typename_keyword_p=*/true,
10707 /*template_keyword_p=*/template_p,
10709 /*check_dependency_p=*/true,
10710 /*class_head_p=*/false,
10711 /*is_declaration=*/true);
10712 /* Otherwise, we could also be looking for an ordinary identifier. */
10713 cp_parser_parse_tentatively (parser);
10714 /* Try a class-name. */
10715 id = cp_parser_class_name (parser,
10716 /*typename_keyword_p=*/true,
10717 /*template_keyword_p=*/false,
10719 /*check_dependency_p=*/true,
10720 /*class_head_p=*/false,
10721 /*is_declaration=*/true);
10722 /* If we found one, we're done. */
10723 if (cp_parser_parse_definitely (parser))
10725 /* Otherwise, look for an ordinary identifier. */
10726 return cp_parser_identifier (parser);
10729 /* Overloading [gram.over] */
10731 /* Parse an operator-function-id.
10733 operator-function-id:
10736 Returns an IDENTIFIER_NODE for the operator which is a
10737 human-readable spelling of the identifier, e.g., `operator +'. */
10740 cp_parser_operator_function_id (cp_parser* parser)
10742 /* Look for the `operator' keyword. */
10743 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10744 return error_mark_node;
10745 /* And then the name of the operator itself. */
10746 return cp_parser_operator (parser);
10749 /* Parse an operator.
10752 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10753 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10754 || ++ -- , ->* -> () []
10761 Returns an IDENTIFIER_NODE for the operator which is a
10762 human-readable spelling of the identifier, e.g., `operator +'. */
10765 cp_parser_operator (cp_parser* parser)
10767 tree id = NULL_TREE;
10770 /* Peek at the next token. */
10771 token = cp_lexer_peek_token (parser->lexer);
10772 /* Figure out which operator we have. */
10773 switch (token->type)
10779 /* The keyword should be either `new' or `delete'. */
10780 if (token->keyword == RID_NEW)
10782 else if (token->keyword == RID_DELETE)
10787 /* Consume the `new' or `delete' token. */
10788 cp_lexer_consume_token (parser->lexer);
10790 /* Peek at the next token. */
10791 token = cp_lexer_peek_token (parser->lexer);
10792 /* If it's a `[' token then this is the array variant of the
10794 if (token->type == CPP_OPEN_SQUARE)
10796 /* Consume the `[' token. */
10797 cp_lexer_consume_token (parser->lexer);
10798 /* Look for the `]' token. */
10799 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10800 id = ansi_opname (op == NEW_EXPR
10801 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10803 /* Otherwise, we have the non-array variant. */
10805 id = ansi_opname (op);
10811 id = ansi_opname (PLUS_EXPR);
10815 id = ansi_opname (MINUS_EXPR);
10819 id = ansi_opname (MULT_EXPR);
10823 id = ansi_opname (TRUNC_DIV_EXPR);
10827 id = ansi_opname (TRUNC_MOD_EXPR);
10831 id = ansi_opname (BIT_XOR_EXPR);
10835 id = ansi_opname (BIT_AND_EXPR);
10839 id = ansi_opname (BIT_IOR_EXPR);
10843 id = ansi_opname (BIT_NOT_EXPR);
10847 id = ansi_opname (TRUTH_NOT_EXPR);
10851 id = ansi_assopname (NOP_EXPR);
10855 id = ansi_opname (LT_EXPR);
10859 id = ansi_opname (GT_EXPR);
10863 id = ansi_assopname (PLUS_EXPR);
10867 id = ansi_assopname (MINUS_EXPR);
10871 id = ansi_assopname (MULT_EXPR);
10875 id = ansi_assopname (TRUNC_DIV_EXPR);
10879 id = ansi_assopname (TRUNC_MOD_EXPR);
10883 id = ansi_assopname (BIT_XOR_EXPR);
10887 id = ansi_assopname (BIT_AND_EXPR);
10891 id = ansi_assopname (BIT_IOR_EXPR);
10895 id = ansi_opname (LSHIFT_EXPR);
10899 id = ansi_opname (RSHIFT_EXPR);
10902 case CPP_LSHIFT_EQ:
10903 id = ansi_assopname (LSHIFT_EXPR);
10906 case CPP_RSHIFT_EQ:
10907 id = ansi_assopname (RSHIFT_EXPR);
10911 id = ansi_opname (EQ_EXPR);
10915 id = ansi_opname (NE_EXPR);
10919 id = ansi_opname (LE_EXPR);
10922 case CPP_GREATER_EQ:
10923 id = ansi_opname (GE_EXPR);
10927 id = ansi_opname (TRUTH_ANDIF_EXPR);
10931 id = ansi_opname (TRUTH_ORIF_EXPR);
10934 case CPP_PLUS_PLUS:
10935 id = ansi_opname (POSTINCREMENT_EXPR);
10938 case CPP_MINUS_MINUS:
10939 id = ansi_opname (PREDECREMENT_EXPR);
10943 id = ansi_opname (COMPOUND_EXPR);
10946 case CPP_DEREF_STAR:
10947 id = ansi_opname (MEMBER_REF);
10951 id = ansi_opname (COMPONENT_REF);
10954 case CPP_OPEN_PAREN:
10955 /* Consume the `('. */
10956 cp_lexer_consume_token (parser->lexer);
10957 /* Look for the matching `)'. */
10958 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10959 return ansi_opname (CALL_EXPR);
10961 case CPP_OPEN_SQUARE:
10962 /* Consume the `['. */
10963 cp_lexer_consume_token (parser->lexer);
10964 /* Look for the matching `]'. */
10965 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10966 return ansi_opname (ARRAY_REF);
10969 /* Anything else is an error. */
10973 /* If we have selected an identifier, we need to consume the
10976 cp_lexer_consume_token (parser->lexer);
10977 /* Otherwise, no valid operator name was present. */
10980 cp_parser_error (parser, "expected operator");
10981 id = error_mark_node;
10987 /* Parse a template-declaration.
10989 template-declaration:
10990 export [opt] template < template-parameter-list > declaration
10992 If MEMBER_P is TRUE, this template-declaration occurs within a
10995 The grammar rule given by the standard isn't correct. What
10996 is really meant is:
10998 template-declaration:
10999 export [opt] template-parameter-list-seq
11000 decl-specifier-seq [opt] init-declarator [opt] ;
11001 export [opt] template-parameter-list-seq
11002 function-definition
11004 template-parameter-list-seq:
11005 template-parameter-list-seq [opt]
11006 template < template-parameter-list > */
11009 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11011 /* Check for `export'. */
11012 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11014 /* Consume the `export' token. */
11015 cp_lexer_consume_token (parser->lexer);
11016 /* Warn that we do not support `export'. */
11017 warning (0, "keyword %<export%> not implemented, and will be ignored");
11020 cp_parser_template_declaration_after_export (parser, member_p);
11023 /* Parse a template-parameter-list.
11025 template-parameter-list:
11027 template-parameter-list , template-parameter
11029 Returns a TREE_LIST. Each node represents a template parameter.
11030 The nodes are connected via their TREE_CHAINs. */
11033 cp_parser_template_parameter_list (cp_parser* parser)
11035 tree parameter_list = NULL_TREE;
11037 begin_template_parm_list ();
11039 /* The loop below parses the template parms. We first need to know
11040 the total number of template parms to be able to compute proper
11041 canonical types of each dependent type. So after the loop, when
11042 we know the total number of template parms,
11043 end_template_parm_list computes the proper canonical types and
11044 fixes up the dependent types accordingly. */
11049 bool is_parameter_pack;
11050 location_t parm_loc;
11052 /* Parse the template-parameter. */
11053 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11054 parameter = cp_parser_template_parameter (parser,
11056 &is_parameter_pack);
11057 /* Add it to the list. */
11058 if (parameter != error_mark_node)
11059 parameter_list = process_template_parm (parameter_list,
11067 tree err_parm = build_tree_list (parameter, parameter);
11068 parameter_list = chainon (parameter_list, err_parm);
11071 /* If the next token is not a `,', we're done. */
11072 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11074 /* Otherwise, consume the `,' token. */
11075 cp_lexer_consume_token (parser->lexer);
11078 return end_template_parm_list (parameter_list);
11081 /* Parse a template-parameter.
11083 template-parameter:
11085 parameter-declaration
11087 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11088 the parameter. The TREE_PURPOSE is the default value, if any.
11089 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11090 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11091 set to true iff this parameter is a parameter pack. */
11094 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11095 bool *is_parameter_pack)
11098 cp_parameter_declarator *parameter_declarator;
11099 cp_declarator *id_declarator;
11102 /* Assume it is a type parameter or a template parameter. */
11103 *is_non_type = false;
11104 /* Assume it not a parameter pack. */
11105 *is_parameter_pack = false;
11106 /* Peek at the next token. */
11107 token = cp_lexer_peek_token (parser->lexer);
11108 /* If it is `class' or `template', we have a type-parameter. */
11109 if (token->keyword == RID_TEMPLATE)
11110 return cp_parser_type_parameter (parser, is_parameter_pack);
11111 /* If it is `class' or `typename' we do not know yet whether it is a
11112 type parameter or a non-type parameter. Consider:
11114 template <typename T, typename T::X X> ...
11118 template <class C, class D*> ...
11120 Here, the first parameter is a type parameter, and the second is
11121 a non-type parameter. We can tell by looking at the token after
11122 the identifier -- if it is a `,', `=', or `>' then we have a type
11124 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11126 /* Peek at the token after `class' or `typename'. */
11127 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11128 /* If it's an ellipsis, we have a template type parameter
11130 if (token->type == CPP_ELLIPSIS)
11131 return cp_parser_type_parameter (parser, is_parameter_pack);
11132 /* If it's an identifier, skip it. */
11133 if (token->type == CPP_NAME)
11134 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11135 /* Now, see if the token looks like the end of a template
11137 if (token->type == CPP_COMMA
11138 || token->type == CPP_EQ
11139 || token->type == CPP_GREATER)
11140 return cp_parser_type_parameter (parser, is_parameter_pack);
11143 /* Otherwise, it is a non-type parameter.
11147 When parsing a default template-argument for a non-type
11148 template-parameter, the first non-nested `>' is taken as the end
11149 of the template parameter-list rather than a greater-than
11151 *is_non_type = true;
11152 parameter_declarator
11153 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11154 /*parenthesized_p=*/NULL);
11156 /* If the parameter declaration is marked as a parameter pack, set
11157 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11158 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11160 if (parameter_declarator
11161 && parameter_declarator->declarator
11162 && parameter_declarator->declarator->parameter_pack_p)
11164 *is_parameter_pack = true;
11165 parameter_declarator->declarator->parameter_pack_p = false;
11168 /* If the next token is an ellipsis, and we don't already have it
11169 marked as a parameter pack, then we have a parameter pack (that
11170 has no declarator). */
11171 if (!*is_parameter_pack
11172 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11173 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11175 /* Consume the `...'. */
11176 cp_lexer_consume_token (parser->lexer);
11177 maybe_warn_variadic_templates ();
11179 *is_parameter_pack = true;
11181 /* We might end up with a pack expansion as the type of the non-type
11182 template parameter, in which case this is a non-type template
11184 else if (parameter_declarator
11185 && parameter_declarator->decl_specifiers.type
11186 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11188 *is_parameter_pack = true;
11189 parameter_declarator->decl_specifiers.type =
11190 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11193 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11195 /* Parameter packs cannot have default arguments. However, a
11196 user may try to do so, so we'll parse them and give an
11197 appropriate diagnostic here. */
11199 /* Consume the `='. */
11200 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11201 cp_lexer_consume_token (parser->lexer);
11203 /* Find the name of the parameter pack. */
11204 id_declarator = parameter_declarator->declarator;
11205 while (id_declarator && id_declarator->kind != cdk_id)
11206 id_declarator = id_declarator->declarator;
11208 if (id_declarator && id_declarator->kind == cdk_id)
11209 error_at (start_token->location,
11210 "template parameter pack %qD cannot have a default argument",
11211 id_declarator->u.id.unqualified_name);
11213 error_at (start_token->location,
11214 "template parameter pack cannot have a default argument");
11216 /* Parse the default argument, but throw away the result. */
11217 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11220 parm = grokdeclarator (parameter_declarator->declarator,
11221 ¶meter_declarator->decl_specifiers,
11222 TPARM, /*initialized=*/0,
11223 /*attrlist=*/NULL);
11224 if (parm == error_mark_node)
11225 return error_mark_node;
11227 return build_tree_list (parameter_declarator->default_argument, parm);
11230 /* Parse a type-parameter.
11233 class identifier [opt]
11234 class identifier [opt] = type-id
11235 typename identifier [opt]
11236 typename identifier [opt] = type-id
11237 template < template-parameter-list > class identifier [opt]
11238 template < template-parameter-list > class identifier [opt]
11241 GNU Extension (variadic templates):
11244 class ... identifier [opt]
11245 typename ... identifier [opt]
11247 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11248 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11249 the declaration of the parameter.
11251 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11254 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11259 /* Look for a keyword to tell us what kind of parameter this is. */
11260 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11262 return error_mark_node;
11264 switch (token->keyword)
11270 tree default_argument;
11272 /* If the next token is an ellipsis, we have a template
11274 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11276 /* Consume the `...' token. */
11277 cp_lexer_consume_token (parser->lexer);
11278 maybe_warn_variadic_templates ();
11280 *is_parameter_pack = true;
11283 /* If the next token is an identifier, then it names the
11285 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11286 identifier = cp_parser_identifier (parser);
11288 identifier = NULL_TREE;
11290 /* Create the parameter. */
11291 parameter = finish_template_type_parm (class_type_node, identifier);
11293 /* If the next token is an `=', we have a default argument. */
11294 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11296 /* Consume the `=' token. */
11297 cp_lexer_consume_token (parser->lexer);
11298 /* Parse the default-argument. */
11299 push_deferring_access_checks (dk_no_deferred);
11300 default_argument = cp_parser_type_id (parser);
11302 /* Template parameter packs cannot have default
11304 if (*is_parameter_pack)
11307 error_at (token->location,
11308 "template parameter pack %qD cannot have a "
11309 "default argument", identifier);
11311 error_at (token->location,
11312 "template parameter packs cannot have "
11313 "default arguments");
11314 default_argument = NULL_TREE;
11316 pop_deferring_access_checks ();
11319 default_argument = NULL_TREE;
11321 /* Create the combined representation of the parameter and the
11322 default argument. */
11323 parameter = build_tree_list (default_argument, parameter);
11330 tree default_argument;
11332 /* Look for the `<'. */
11333 cp_parser_require (parser, CPP_LESS, RT_LESS);
11334 /* Parse the template-parameter-list. */
11335 cp_parser_template_parameter_list (parser);
11336 /* Look for the `>'. */
11337 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11338 /* Look for the `class' keyword. */
11339 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11340 /* If the next token is an ellipsis, we have a template
11342 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11344 /* Consume the `...' token. */
11345 cp_lexer_consume_token (parser->lexer);
11346 maybe_warn_variadic_templates ();
11348 *is_parameter_pack = true;
11350 /* If the next token is an `=', then there is a
11351 default-argument. If the next token is a `>', we are at
11352 the end of the parameter-list. If the next token is a `,',
11353 then we are at the end of this parameter. */
11354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11355 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11356 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11358 identifier = cp_parser_identifier (parser);
11359 /* Treat invalid names as if the parameter were nameless. */
11360 if (identifier == error_mark_node)
11361 identifier = NULL_TREE;
11364 identifier = NULL_TREE;
11366 /* Create the template parameter. */
11367 parameter = finish_template_template_parm (class_type_node,
11370 /* If the next token is an `=', then there is a
11371 default-argument. */
11372 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11376 /* Consume the `='. */
11377 cp_lexer_consume_token (parser->lexer);
11378 /* Parse the id-expression. */
11379 push_deferring_access_checks (dk_no_deferred);
11380 /* save token before parsing the id-expression, for error
11382 token = cp_lexer_peek_token (parser->lexer);
11384 = cp_parser_id_expression (parser,
11385 /*template_keyword_p=*/false,
11386 /*check_dependency_p=*/true,
11387 /*template_p=*/&is_template,
11388 /*declarator_p=*/false,
11389 /*optional_p=*/false);
11390 if (TREE_CODE (default_argument) == TYPE_DECL)
11391 /* If the id-expression was a template-id that refers to
11392 a template-class, we already have the declaration here,
11393 so no further lookup is needed. */
11396 /* Look up the name. */
11398 = cp_parser_lookup_name (parser, default_argument,
11400 /*is_template=*/is_template,
11401 /*is_namespace=*/false,
11402 /*check_dependency=*/true,
11403 /*ambiguous_decls=*/NULL,
11405 /* See if the default argument is valid. */
11407 = check_template_template_default_arg (default_argument);
11409 /* Template parameter packs cannot have default
11411 if (*is_parameter_pack)
11414 error_at (token->location,
11415 "template parameter pack %qD cannot "
11416 "have a default argument",
11419 error_at (token->location, "template parameter packs cannot "
11420 "have default arguments");
11421 default_argument = NULL_TREE;
11423 pop_deferring_access_checks ();
11426 default_argument = NULL_TREE;
11428 /* Create the combined representation of the parameter and the
11429 default argument. */
11430 parameter = build_tree_list (default_argument, parameter);
11435 gcc_unreachable ();
11442 /* Parse a template-id.
11445 template-name < template-argument-list [opt] >
11447 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11448 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11449 returned. Otherwise, if the template-name names a function, or set
11450 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11451 names a class, returns a TYPE_DECL for the specialization.
11453 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11454 uninstantiated templates. */
11457 cp_parser_template_id (cp_parser *parser,
11458 bool template_keyword_p,
11459 bool check_dependency_p,
11460 bool is_declaration)
11466 cp_token_position start_of_id = 0;
11467 deferred_access_check *chk;
11468 VEC (deferred_access_check,gc) *access_check;
11469 cp_token *next_token = NULL, *next_token_2 = NULL;
11470 bool is_identifier;
11472 /* If the next token corresponds to a template-id, there is no need
11474 next_token = cp_lexer_peek_token (parser->lexer);
11475 if (next_token->type == CPP_TEMPLATE_ID)
11477 struct tree_check *check_value;
11479 /* Get the stored value. */
11480 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11481 /* Perform any access checks that were deferred. */
11482 access_check = check_value->checks;
11485 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11486 perform_or_defer_access_check (chk->binfo,
11490 /* Return the stored value. */
11491 return check_value->value;
11494 /* Avoid performing name lookup if there is no possibility of
11495 finding a template-id. */
11496 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11497 || (next_token->type == CPP_NAME
11498 && !cp_parser_nth_token_starts_template_argument_list_p
11501 cp_parser_error (parser, "expected template-id");
11502 return error_mark_node;
11505 /* Remember where the template-id starts. */
11506 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11507 start_of_id = cp_lexer_token_position (parser->lexer, false);
11509 push_deferring_access_checks (dk_deferred);
11511 /* Parse the template-name. */
11512 is_identifier = false;
11513 templ = cp_parser_template_name (parser, template_keyword_p,
11514 check_dependency_p,
11517 if (templ == error_mark_node || is_identifier)
11519 pop_deferring_access_checks ();
11523 /* If we find the sequence `[:' after a template-name, it's probably
11524 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11525 parse correctly the argument list. */
11526 next_token = cp_lexer_peek_token (parser->lexer);
11527 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11528 if (next_token->type == CPP_OPEN_SQUARE
11529 && next_token->flags & DIGRAPH
11530 && next_token_2->type == CPP_COLON
11531 && !(next_token_2->flags & PREV_WHITE))
11533 cp_parser_parse_tentatively (parser);
11534 /* Change `:' into `::'. */
11535 next_token_2->type = CPP_SCOPE;
11536 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11538 cp_lexer_consume_token (parser->lexer);
11540 /* Parse the arguments. */
11541 arguments = cp_parser_enclosed_template_argument_list (parser);
11542 if (!cp_parser_parse_definitely (parser))
11544 /* If we couldn't parse an argument list, then we revert our changes
11545 and return simply an error. Maybe this is not a template-id
11547 next_token_2->type = CPP_COLON;
11548 cp_parser_error (parser, "expected %<<%>");
11549 pop_deferring_access_checks ();
11550 return error_mark_node;
11552 /* Otherwise, emit an error about the invalid digraph, but continue
11553 parsing because we got our argument list. */
11554 if (permerror (next_token->location,
11555 "%<<::%> cannot begin a template-argument list"))
11557 static bool hint = false;
11558 inform (next_token->location,
11559 "%<<:%> is an alternate spelling for %<[%>."
11560 " Insert whitespace between %<<%> and %<::%>");
11561 if (!hint && !flag_permissive)
11563 inform (next_token->location, "(if you use %<-fpermissive%>"
11564 " G++ will accept your code)");
11571 /* Look for the `<' that starts the template-argument-list. */
11572 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11574 pop_deferring_access_checks ();
11575 return error_mark_node;
11577 /* Parse the arguments. */
11578 arguments = cp_parser_enclosed_template_argument_list (parser);
11581 /* Build a representation of the specialization. */
11582 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11583 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11584 else if (DECL_CLASS_TEMPLATE_P (templ)
11585 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11587 bool entering_scope;
11588 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11589 template (rather than some instantiation thereof) only if
11590 is not nested within some other construct. For example, in
11591 "template <typename T> void f(T) { A<T>::", A<T> is just an
11592 instantiation of A. */
11593 entering_scope = (template_parm_scope_p ()
11594 && cp_lexer_next_token_is (parser->lexer,
11597 = finish_template_type (templ, arguments, entering_scope);
11601 /* If it's not a class-template or a template-template, it should be
11602 a function-template. */
11603 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11604 || TREE_CODE (templ) == OVERLOAD
11605 || BASELINK_P (templ)));
11607 template_id = lookup_template_function (templ, arguments);
11610 /* If parsing tentatively, replace the sequence of tokens that makes
11611 up the template-id with a CPP_TEMPLATE_ID token. That way,
11612 should we re-parse the token stream, we will not have to repeat
11613 the effort required to do the parse, nor will we issue duplicate
11614 error messages about problems during instantiation of the
11618 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11620 /* Reset the contents of the START_OF_ID token. */
11621 token->type = CPP_TEMPLATE_ID;
11622 /* Retrieve any deferred checks. Do not pop this access checks yet
11623 so the memory will not be reclaimed during token replacing below. */
11624 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11625 token->u.tree_check_value->value = template_id;
11626 token->u.tree_check_value->checks = get_deferred_access_checks ();
11627 token->keyword = RID_MAX;
11629 /* Purge all subsequent tokens. */
11630 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11632 /* ??? Can we actually assume that, if template_id ==
11633 error_mark_node, we will have issued a diagnostic to the
11634 user, as opposed to simply marking the tentative parse as
11636 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11637 error_at (token->location, "parse error in template argument list");
11640 pop_deferring_access_checks ();
11641 return template_id;
11644 /* Parse a template-name.
11649 The standard should actually say:
11653 operator-function-id
11655 A defect report has been filed about this issue.
11657 A conversion-function-id cannot be a template name because they cannot
11658 be part of a template-id. In fact, looking at this code:
11660 a.operator K<int>()
11662 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11663 It is impossible to call a templated conversion-function-id with an
11664 explicit argument list, since the only allowed template parameter is
11665 the type to which it is converting.
11667 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11668 `template' keyword, in a construction like:
11672 In that case `f' is taken to be a template-name, even though there
11673 is no way of knowing for sure.
11675 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11676 name refers to a set of overloaded functions, at least one of which
11677 is a template, or an IDENTIFIER_NODE with the name of the template,
11678 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11679 names are looked up inside uninstantiated templates. */
11682 cp_parser_template_name (cp_parser* parser,
11683 bool template_keyword_p,
11684 bool check_dependency_p,
11685 bool is_declaration,
11686 bool *is_identifier)
11691 cp_token *token = cp_lexer_peek_token (parser->lexer);
11693 /* If the next token is `operator', then we have either an
11694 operator-function-id or a conversion-function-id. */
11695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11697 /* We don't know whether we're looking at an
11698 operator-function-id or a conversion-function-id. */
11699 cp_parser_parse_tentatively (parser);
11700 /* Try an operator-function-id. */
11701 identifier = cp_parser_operator_function_id (parser);
11702 /* If that didn't work, try a conversion-function-id. */
11703 if (!cp_parser_parse_definitely (parser))
11705 cp_parser_error (parser, "expected template-name");
11706 return error_mark_node;
11709 /* Look for the identifier. */
11711 identifier = cp_parser_identifier (parser);
11713 /* If we didn't find an identifier, we don't have a template-id. */
11714 if (identifier == error_mark_node)
11715 return error_mark_node;
11717 /* If the name immediately followed the `template' keyword, then it
11718 is a template-name. However, if the next token is not `<', then
11719 we do not treat it as a template-name, since it is not being used
11720 as part of a template-id. This enables us to handle constructs
11723 template <typename T> struct S { S(); };
11724 template <typename T> S<T>::S();
11726 correctly. We would treat `S' as a template -- if it were `S<T>'
11727 -- but we do not if there is no `<'. */
11729 if (processing_template_decl
11730 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11732 /* In a declaration, in a dependent context, we pretend that the
11733 "template" keyword was present in order to improve error
11734 recovery. For example, given:
11736 template <typename T> void f(T::X<int>);
11738 we want to treat "X<int>" as a template-id. */
11740 && !template_keyword_p
11741 && parser->scope && TYPE_P (parser->scope)
11742 && check_dependency_p
11743 && dependent_scope_p (parser->scope)
11744 /* Do not do this for dtors (or ctors), since they never
11745 need the template keyword before their name. */
11746 && !constructor_name_p (identifier, parser->scope))
11748 cp_token_position start = 0;
11750 /* Explain what went wrong. */
11751 error_at (token->location, "non-template %qD used as template",
11753 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11754 parser->scope, identifier);
11755 /* If parsing tentatively, find the location of the "<" token. */
11756 if (cp_parser_simulate_error (parser))
11757 start = cp_lexer_token_position (parser->lexer, true);
11758 /* Parse the template arguments so that we can issue error
11759 messages about them. */
11760 cp_lexer_consume_token (parser->lexer);
11761 cp_parser_enclosed_template_argument_list (parser);
11762 /* Skip tokens until we find a good place from which to
11763 continue parsing. */
11764 cp_parser_skip_to_closing_parenthesis (parser,
11765 /*recovering=*/true,
11767 /*consume_paren=*/false);
11768 /* If parsing tentatively, permanently remove the
11769 template argument list. That will prevent duplicate
11770 error messages from being issued about the missing
11771 "template" keyword. */
11773 cp_lexer_purge_tokens_after (parser->lexer, start);
11775 *is_identifier = true;
11779 /* If the "template" keyword is present, then there is generally
11780 no point in doing name-lookup, so we just return IDENTIFIER.
11781 But, if the qualifying scope is non-dependent then we can
11782 (and must) do name-lookup normally. */
11783 if (template_keyword_p
11785 || (TYPE_P (parser->scope)
11786 && dependent_type_p (parser->scope))))
11790 /* Look up the name. */
11791 decl = cp_parser_lookup_name (parser, identifier,
11793 /*is_template=*/true,
11794 /*is_namespace=*/false,
11795 check_dependency_p,
11796 /*ambiguous_decls=*/NULL,
11799 /* If DECL is a template, then the name was a template-name. */
11800 if (TREE_CODE (decl) == TEMPLATE_DECL)
11804 tree fn = NULL_TREE;
11806 /* The standard does not explicitly indicate whether a name that
11807 names a set of overloaded declarations, some of which are
11808 templates, is a template-name. However, such a name should
11809 be a template-name; otherwise, there is no way to form a
11810 template-id for the overloaded templates. */
11811 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11812 if (TREE_CODE (fns) == OVERLOAD)
11813 for (fn = fns; fn; fn = OVL_NEXT (fn))
11814 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11819 /* The name does not name a template. */
11820 cp_parser_error (parser, "expected template-name");
11821 return error_mark_node;
11825 /* If DECL is dependent, and refers to a function, then just return
11826 its name; we will look it up again during template instantiation. */
11827 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11829 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11830 if (TYPE_P (scope) && dependent_type_p (scope))
11837 /* Parse a template-argument-list.
11839 template-argument-list:
11840 template-argument ... [opt]
11841 template-argument-list , template-argument ... [opt]
11843 Returns a TREE_VEC containing the arguments. */
11846 cp_parser_template_argument_list (cp_parser* parser)
11848 tree fixed_args[10];
11849 unsigned n_args = 0;
11850 unsigned alloced = 10;
11851 tree *arg_ary = fixed_args;
11853 bool saved_in_template_argument_list_p;
11855 bool saved_non_ice_p;
11857 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11858 parser->in_template_argument_list_p = true;
11859 /* Even if the template-id appears in an integral
11860 constant-expression, the contents of the argument list do
11862 saved_ice_p = parser->integral_constant_expression_p;
11863 parser->integral_constant_expression_p = false;
11864 saved_non_ice_p = parser->non_integral_constant_expression_p;
11865 parser->non_integral_constant_expression_p = false;
11866 /* Parse the arguments. */
11872 /* Consume the comma. */
11873 cp_lexer_consume_token (parser->lexer);
11875 /* Parse the template-argument. */
11876 argument = cp_parser_template_argument (parser);
11878 /* If the next token is an ellipsis, we're expanding a template
11880 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11882 if (argument == error_mark_node)
11884 cp_token *token = cp_lexer_peek_token (parser->lexer);
11885 error_at (token->location,
11886 "expected parameter pack before %<...%>");
11888 /* Consume the `...' token. */
11889 cp_lexer_consume_token (parser->lexer);
11891 /* Make the argument into a TYPE_PACK_EXPANSION or
11892 EXPR_PACK_EXPANSION. */
11893 argument = make_pack_expansion (argument);
11896 if (n_args == alloced)
11900 if (arg_ary == fixed_args)
11902 arg_ary = XNEWVEC (tree, alloced);
11903 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11906 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11908 arg_ary[n_args++] = argument;
11910 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11912 vec = make_tree_vec (n_args);
11915 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11917 if (arg_ary != fixed_args)
11919 parser->non_integral_constant_expression_p = saved_non_ice_p;
11920 parser->integral_constant_expression_p = saved_ice_p;
11921 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11922 #ifdef ENABLE_CHECKING
11923 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11928 /* Parse a template-argument.
11931 assignment-expression
11935 The representation is that of an assignment-expression, type-id, or
11936 id-expression -- except that the qualified id-expression is
11937 evaluated, so that the value returned is either a DECL or an
11940 Although the standard says "assignment-expression", it forbids
11941 throw-expressions or assignments in the template argument.
11942 Therefore, we use "conditional-expression" instead. */
11945 cp_parser_template_argument (cp_parser* parser)
11950 bool maybe_type_id = false;
11951 cp_token *token = NULL, *argument_start_token = NULL;
11954 /* There's really no way to know what we're looking at, so we just
11955 try each alternative in order.
11959 In a template-argument, an ambiguity between a type-id and an
11960 expression is resolved to a type-id, regardless of the form of
11961 the corresponding template-parameter.
11963 Therefore, we try a type-id first. */
11964 cp_parser_parse_tentatively (parser);
11965 argument = cp_parser_template_type_arg (parser);
11966 /* If there was no error parsing the type-id but the next token is a
11967 '>>', our behavior depends on which dialect of C++ we're
11968 parsing. In C++98, we probably found a typo for '> >'. But there
11969 are type-id which are also valid expressions. For instance:
11971 struct X { int operator >> (int); };
11972 template <int V> struct Foo {};
11975 Here 'X()' is a valid type-id of a function type, but the user just
11976 wanted to write the expression "X() >> 5". Thus, we remember that we
11977 found a valid type-id, but we still try to parse the argument as an
11978 expression to see what happens.
11980 In C++0x, the '>>' will be considered two separate '>'
11982 if (!cp_parser_error_occurred (parser)
11983 && cxx_dialect == cxx98
11984 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11986 maybe_type_id = true;
11987 cp_parser_abort_tentative_parse (parser);
11991 /* If the next token isn't a `,' or a `>', then this argument wasn't
11992 really finished. This means that the argument is not a valid
11994 if (!cp_parser_next_token_ends_template_argument_p (parser))
11995 cp_parser_error (parser, "expected template-argument");
11996 /* If that worked, we're done. */
11997 if (cp_parser_parse_definitely (parser))
12000 /* We're still not sure what the argument will be. */
12001 cp_parser_parse_tentatively (parser);
12002 /* Try a template. */
12003 argument_start_token = cp_lexer_peek_token (parser->lexer);
12004 argument = cp_parser_id_expression (parser,
12005 /*template_keyword_p=*/false,
12006 /*check_dependency_p=*/true,
12008 /*declarator_p=*/false,
12009 /*optional_p=*/false);
12010 /* If the next token isn't a `,' or a `>', then this argument wasn't
12011 really finished. */
12012 if (!cp_parser_next_token_ends_template_argument_p (parser))
12013 cp_parser_error (parser, "expected template-argument");
12014 if (!cp_parser_error_occurred (parser))
12016 /* Figure out what is being referred to. If the id-expression
12017 was for a class template specialization, then we will have a
12018 TYPE_DECL at this point. There is no need to do name lookup
12019 at this point in that case. */
12020 if (TREE_CODE (argument) != TYPE_DECL)
12021 argument = cp_parser_lookup_name (parser, argument,
12023 /*is_template=*/template_p,
12024 /*is_namespace=*/false,
12025 /*check_dependency=*/true,
12026 /*ambiguous_decls=*/NULL,
12027 argument_start_token->location);
12028 if (TREE_CODE (argument) != TEMPLATE_DECL
12029 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12030 cp_parser_error (parser, "expected template-name");
12032 if (cp_parser_parse_definitely (parser))
12034 /* It must be a non-type argument. There permitted cases are given
12035 in [temp.arg.nontype]:
12037 -- an integral constant-expression of integral or enumeration
12040 -- the name of a non-type template-parameter; or
12042 -- the name of an object or function with external linkage...
12044 -- the address of an object or function with external linkage...
12046 -- a pointer to member... */
12047 /* Look for a non-type template parameter. */
12048 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12050 cp_parser_parse_tentatively (parser);
12051 argument = cp_parser_primary_expression (parser,
12052 /*address_p=*/false,
12054 /*template_arg_p=*/true,
12056 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12057 || !cp_parser_next_token_ends_template_argument_p (parser))
12058 cp_parser_simulate_error (parser);
12059 if (cp_parser_parse_definitely (parser))
12063 /* If the next token is "&", the argument must be the address of an
12064 object or function with external linkage. */
12065 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12067 cp_lexer_consume_token (parser->lexer);
12068 /* See if we might have an id-expression. */
12069 token = cp_lexer_peek_token (parser->lexer);
12070 if (token->type == CPP_NAME
12071 || token->keyword == RID_OPERATOR
12072 || token->type == CPP_SCOPE
12073 || token->type == CPP_TEMPLATE_ID
12074 || token->type == CPP_NESTED_NAME_SPECIFIER)
12076 cp_parser_parse_tentatively (parser);
12077 argument = cp_parser_primary_expression (parser,
12080 /*template_arg_p=*/true,
12082 if (cp_parser_error_occurred (parser)
12083 || !cp_parser_next_token_ends_template_argument_p (parser))
12084 cp_parser_abort_tentative_parse (parser);
12089 if (TREE_CODE (argument) == INDIRECT_REF)
12091 gcc_assert (REFERENCE_REF_P (argument));
12092 argument = TREE_OPERAND (argument, 0);
12095 /* If we're in a template, we represent a qualified-id referring
12096 to a static data member as a SCOPE_REF even if the scope isn't
12097 dependent so that we can check access control later. */
12099 if (TREE_CODE (probe) == SCOPE_REF)
12100 probe = TREE_OPERAND (probe, 1);
12101 if (TREE_CODE (probe) == VAR_DECL)
12103 /* A variable without external linkage might still be a
12104 valid constant-expression, so no error is issued here
12105 if the external-linkage check fails. */
12106 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12107 cp_parser_simulate_error (parser);
12109 else if (is_overloaded_fn (argument))
12110 /* All overloaded functions are allowed; if the external
12111 linkage test does not pass, an error will be issued
12115 && (TREE_CODE (argument) == OFFSET_REF
12116 || TREE_CODE (argument) == SCOPE_REF))
12117 /* A pointer-to-member. */
12119 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12122 cp_parser_simulate_error (parser);
12124 if (cp_parser_parse_definitely (parser))
12127 argument = build_x_unary_op (ADDR_EXPR, argument,
12128 tf_warning_or_error);
12133 /* If the argument started with "&", there are no other valid
12134 alternatives at this point. */
12137 cp_parser_error (parser, "invalid non-type template argument");
12138 return error_mark_node;
12141 /* If the argument wasn't successfully parsed as a type-id followed
12142 by '>>', the argument can only be a constant expression now.
12143 Otherwise, we try parsing the constant-expression tentatively,
12144 because the argument could really be a type-id. */
12146 cp_parser_parse_tentatively (parser);
12147 argument = cp_parser_constant_expression (parser,
12148 /*allow_non_constant_p=*/false,
12149 /*non_constant_p=*/NULL);
12150 argument = fold_non_dependent_expr (argument);
12151 if (!maybe_type_id)
12153 if (!cp_parser_next_token_ends_template_argument_p (parser))
12154 cp_parser_error (parser, "expected template-argument");
12155 if (cp_parser_parse_definitely (parser))
12157 /* We did our best to parse the argument as a non type-id, but that
12158 was the only alternative that matched (albeit with a '>' after
12159 it). We can assume it's just a typo from the user, and a
12160 diagnostic will then be issued. */
12161 return cp_parser_template_type_arg (parser);
12164 /* Parse an explicit-instantiation.
12166 explicit-instantiation:
12167 template declaration
12169 Although the standard says `declaration', what it really means is:
12171 explicit-instantiation:
12172 template decl-specifier-seq [opt] declarator [opt] ;
12174 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12175 supposed to be allowed. A defect report has been filed about this
12180 explicit-instantiation:
12181 storage-class-specifier template
12182 decl-specifier-seq [opt] declarator [opt] ;
12183 function-specifier template
12184 decl-specifier-seq [opt] declarator [opt] ; */
12187 cp_parser_explicit_instantiation (cp_parser* parser)
12189 int declares_class_or_enum;
12190 cp_decl_specifier_seq decl_specifiers;
12191 tree extension_specifier = NULL_TREE;
12193 timevar_push (TV_TEMPLATE_INST);
12195 /* Look for an (optional) storage-class-specifier or
12196 function-specifier. */
12197 if (cp_parser_allow_gnu_extensions_p (parser))
12199 extension_specifier
12200 = cp_parser_storage_class_specifier_opt (parser);
12201 if (!extension_specifier)
12202 extension_specifier
12203 = cp_parser_function_specifier_opt (parser,
12204 /*decl_specs=*/NULL);
12207 /* Look for the `template' keyword. */
12208 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12209 /* Let the front end know that we are processing an explicit
12211 begin_explicit_instantiation ();
12212 /* [temp.explicit] says that we are supposed to ignore access
12213 control while processing explicit instantiation directives. */
12214 push_deferring_access_checks (dk_no_check);
12215 /* Parse a decl-specifier-seq. */
12216 cp_parser_decl_specifier_seq (parser,
12217 CP_PARSER_FLAGS_OPTIONAL,
12219 &declares_class_or_enum);
12220 /* If there was exactly one decl-specifier, and it declared a class,
12221 and there's no declarator, then we have an explicit type
12223 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12227 type = check_tag_decl (&decl_specifiers);
12228 /* Turn access control back on for names used during
12229 template instantiation. */
12230 pop_deferring_access_checks ();
12232 do_type_instantiation (type, extension_specifier,
12233 /*complain=*/tf_error);
12237 cp_declarator *declarator;
12240 /* Parse the declarator. */
12242 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12243 /*ctor_dtor_or_conv_p=*/NULL,
12244 /*parenthesized_p=*/NULL,
12245 /*member_p=*/false);
12246 if (declares_class_or_enum & 2)
12247 cp_parser_check_for_definition_in_return_type (declarator,
12248 decl_specifiers.type,
12249 decl_specifiers.type_location);
12250 if (declarator != cp_error_declarator)
12252 if (decl_specifiers.specs[(int)ds_inline])
12253 permerror (input_location, "explicit instantiation shall not use"
12254 " %<inline%> specifier");
12255 if (decl_specifiers.specs[(int)ds_constexpr])
12256 permerror (input_location, "explicit instantiation shall not use"
12257 " %<constexpr%> specifier");
12259 decl = grokdeclarator (declarator, &decl_specifiers,
12260 NORMAL, 0, &decl_specifiers.attributes);
12261 /* Turn access control back on for names used during
12262 template instantiation. */
12263 pop_deferring_access_checks ();
12264 /* Do the explicit instantiation. */
12265 do_decl_instantiation (decl, extension_specifier);
12269 pop_deferring_access_checks ();
12270 /* Skip the body of the explicit instantiation. */
12271 cp_parser_skip_to_end_of_statement (parser);
12274 /* We're done with the instantiation. */
12275 end_explicit_instantiation ();
12277 cp_parser_consume_semicolon_at_end_of_statement (parser);
12279 timevar_pop (TV_TEMPLATE_INST);
12282 /* Parse an explicit-specialization.
12284 explicit-specialization:
12285 template < > declaration
12287 Although the standard says `declaration', what it really means is:
12289 explicit-specialization:
12290 template <> decl-specifier [opt] init-declarator [opt] ;
12291 template <> function-definition
12292 template <> explicit-specialization
12293 template <> template-declaration */
12296 cp_parser_explicit_specialization (cp_parser* parser)
12298 bool need_lang_pop;
12299 cp_token *token = cp_lexer_peek_token (parser->lexer);
12301 /* Look for the `template' keyword. */
12302 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12303 /* Look for the `<'. */
12304 cp_parser_require (parser, CPP_LESS, RT_LESS);
12305 /* Look for the `>'. */
12306 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12307 /* We have processed another parameter list. */
12308 ++parser->num_template_parameter_lists;
12311 A template ... explicit specialization ... shall not have C
12313 if (current_lang_name == lang_name_c)
12315 error_at (token->location, "template specialization with C linkage");
12316 /* Give it C++ linkage to avoid confusing other parts of the
12318 push_lang_context (lang_name_cplusplus);
12319 need_lang_pop = true;
12322 need_lang_pop = false;
12323 /* Let the front end know that we are beginning a specialization. */
12324 if (!begin_specialization ())
12326 end_specialization ();
12330 /* If the next keyword is `template', we need to figure out whether
12331 or not we're looking a template-declaration. */
12332 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12334 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12335 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12336 cp_parser_template_declaration_after_export (parser,
12337 /*member_p=*/false);
12339 cp_parser_explicit_specialization (parser);
12342 /* Parse the dependent declaration. */
12343 cp_parser_single_declaration (parser,
12345 /*member_p=*/false,
12346 /*explicit_specialization_p=*/true,
12347 /*friend_p=*/NULL);
12348 /* We're done with the specialization. */
12349 end_specialization ();
12350 /* For the erroneous case of a template with C linkage, we pushed an
12351 implicit C++ linkage scope; exit that scope now. */
12353 pop_lang_context ();
12354 /* We're done with this parameter list. */
12355 --parser->num_template_parameter_lists;
12358 /* Parse a type-specifier.
12361 simple-type-specifier
12364 elaborated-type-specifier
12372 Returns a representation of the type-specifier. For a
12373 class-specifier, enum-specifier, or elaborated-type-specifier, a
12374 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12376 The parser flags FLAGS is used to control type-specifier parsing.
12378 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12379 in a decl-specifier-seq.
12381 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12382 class-specifier, enum-specifier, or elaborated-type-specifier, then
12383 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12384 if a type is declared; 2 if it is defined. Otherwise, it is set to
12387 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12388 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12389 is set to FALSE. */
12392 cp_parser_type_specifier (cp_parser* parser,
12393 cp_parser_flags flags,
12394 cp_decl_specifier_seq *decl_specs,
12395 bool is_declaration,
12396 int* declares_class_or_enum,
12397 bool* is_cv_qualifier)
12399 tree type_spec = NULL_TREE;
12402 cp_decl_spec ds = ds_last;
12404 /* Assume this type-specifier does not declare a new type. */
12405 if (declares_class_or_enum)
12406 *declares_class_or_enum = 0;
12407 /* And that it does not specify a cv-qualifier. */
12408 if (is_cv_qualifier)
12409 *is_cv_qualifier = false;
12410 /* Peek at the next token. */
12411 token = cp_lexer_peek_token (parser->lexer);
12413 /* If we're looking at a keyword, we can use that to guide the
12414 production we choose. */
12415 keyword = token->keyword;
12419 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12420 goto elaborated_type_specifier;
12422 /* Look for the enum-specifier. */
12423 type_spec = cp_parser_enum_specifier (parser);
12424 /* If that worked, we're done. */
12427 if (declares_class_or_enum)
12428 *declares_class_or_enum = 2;
12430 cp_parser_set_decl_spec_type (decl_specs,
12433 /*user_defined_p=*/true);
12437 goto elaborated_type_specifier;
12439 /* Any of these indicate either a class-specifier, or an
12440 elaborated-type-specifier. */
12444 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12445 goto elaborated_type_specifier;
12447 /* Parse tentatively so that we can back up if we don't find a
12448 class-specifier. */
12449 cp_parser_parse_tentatively (parser);
12450 /* Look for the class-specifier. */
12451 type_spec = cp_parser_class_specifier (parser);
12452 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12453 /* If that worked, we're done. */
12454 if (cp_parser_parse_definitely (parser))
12456 if (declares_class_or_enum)
12457 *declares_class_or_enum = 2;
12459 cp_parser_set_decl_spec_type (decl_specs,
12462 /*user_defined_p=*/true);
12466 /* Fall through. */
12467 elaborated_type_specifier:
12468 /* We're declaring (not defining) a class or enum. */
12469 if (declares_class_or_enum)
12470 *declares_class_or_enum = 1;
12472 /* Fall through. */
12474 /* Look for an elaborated-type-specifier. */
12476 = (cp_parser_elaborated_type_specifier
12478 decl_specs && decl_specs->specs[(int) ds_friend],
12481 cp_parser_set_decl_spec_type (decl_specs,
12484 /*user_defined_p=*/true);
12489 if (is_cv_qualifier)
12490 *is_cv_qualifier = true;
12495 if (is_cv_qualifier)
12496 *is_cv_qualifier = true;
12501 if (is_cv_qualifier)
12502 *is_cv_qualifier = true;
12506 /* The `__complex__' keyword is a GNU extension. */
12514 /* Handle simple keywords. */
12519 ++decl_specs->specs[(int)ds];
12520 decl_specs->any_specifiers_p = true;
12522 return cp_lexer_consume_token (parser->lexer)->u.value;
12525 /* If we do not already have a type-specifier, assume we are looking
12526 at a simple-type-specifier. */
12527 type_spec = cp_parser_simple_type_specifier (parser,
12531 /* If we didn't find a type-specifier, and a type-specifier was not
12532 optional in this context, issue an error message. */
12533 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12535 cp_parser_error (parser, "expected type specifier");
12536 return error_mark_node;
12542 /* Parse a simple-type-specifier.
12544 simple-type-specifier:
12545 :: [opt] nested-name-specifier [opt] type-name
12546 :: [opt] nested-name-specifier template template-id
12561 simple-type-specifier:
12563 decltype ( expression )
12566 __underlying_type ( type-id )
12570 simple-type-specifier:
12572 __typeof__ unary-expression
12573 __typeof__ ( type-id )
12575 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12576 appropriately updated. */
12579 cp_parser_simple_type_specifier (cp_parser* parser,
12580 cp_decl_specifier_seq *decl_specs,
12581 cp_parser_flags flags)
12583 tree type = NULL_TREE;
12586 /* Peek at the next token. */
12587 token = cp_lexer_peek_token (parser->lexer);
12589 /* If we're looking at a keyword, things are easy. */
12590 switch (token->keyword)
12594 decl_specs->explicit_char_p = true;
12595 type = char_type_node;
12598 type = char16_type_node;
12601 type = char32_type_node;
12604 type = wchar_type_node;
12607 type = boolean_type_node;
12611 ++decl_specs->specs[(int) ds_short];
12612 type = short_integer_type_node;
12616 decl_specs->explicit_int_p = true;
12617 type = integer_type_node;
12620 if (!int128_integer_type_node)
12623 decl_specs->explicit_int128_p = true;
12624 type = int128_integer_type_node;
12628 ++decl_specs->specs[(int) ds_long];
12629 type = long_integer_type_node;
12633 ++decl_specs->specs[(int) ds_signed];
12634 type = integer_type_node;
12638 ++decl_specs->specs[(int) ds_unsigned];
12639 type = unsigned_type_node;
12642 type = float_type_node;
12645 type = double_type_node;
12648 type = void_type_node;
12652 maybe_warn_cpp0x (CPP0X_AUTO);
12653 type = make_auto ();
12657 /* Parse the `decltype' type. */
12658 type = cp_parser_decltype (parser);
12661 cp_parser_set_decl_spec_type (decl_specs, type,
12663 /*user_defined_p=*/true);
12668 /* Consume the `typeof' token. */
12669 cp_lexer_consume_token (parser->lexer);
12670 /* Parse the operand to `typeof'. */
12671 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12672 /* If it is not already a TYPE, take its type. */
12673 if (!TYPE_P (type))
12674 type = finish_typeof (type);
12677 cp_parser_set_decl_spec_type (decl_specs, type,
12679 /*user_defined_p=*/true);
12683 case RID_UNDERLYING_TYPE:
12684 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12687 cp_parser_set_decl_spec_type (decl_specs, type,
12689 /*user_defined_p=*/true);
12697 /* If the type-specifier was for a built-in type, we're done. */
12700 /* Record the type. */
12702 && (token->keyword != RID_SIGNED
12703 && token->keyword != RID_UNSIGNED
12704 && token->keyword != RID_SHORT
12705 && token->keyword != RID_LONG))
12706 cp_parser_set_decl_spec_type (decl_specs,
12709 /*user_defined=*/false);
12711 decl_specs->any_specifiers_p = true;
12713 /* Consume the token. */
12714 cp_lexer_consume_token (parser->lexer);
12716 /* There is no valid C++ program where a non-template type is
12717 followed by a "<". That usually indicates that the user thought
12718 that the type was a template. */
12719 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12721 return TYPE_NAME (type);
12724 /* The type-specifier must be a user-defined type. */
12725 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12730 /* Don't gobble tokens or issue error messages if this is an
12731 optional type-specifier. */
12732 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12733 cp_parser_parse_tentatively (parser);
12735 /* Look for the optional `::' operator. */
12737 = (cp_parser_global_scope_opt (parser,
12738 /*current_scope_valid_p=*/false)
12740 /* Look for the nested-name specifier. */
12742 = (cp_parser_nested_name_specifier_opt (parser,
12743 /*typename_keyword_p=*/false,
12744 /*check_dependency_p=*/true,
12746 /*is_declaration=*/false)
12748 token = cp_lexer_peek_token (parser->lexer);
12749 /* If we have seen a nested-name-specifier, and the next token
12750 is `template', then we are using the template-id production. */
12752 && cp_parser_optional_template_keyword (parser))
12754 /* Look for the template-id. */
12755 type = cp_parser_template_id (parser,
12756 /*template_keyword_p=*/true,
12757 /*check_dependency_p=*/true,
12758 /*is_declaration=*/false);
12759 /* If the template-id did not name a type, we are out of
12761 if (TREE_CODE (type) != TYPE_DECL)
12763 cp_parser_error (parser, "expected template-id for type");
12767 /* Otherwise, look for a type-name. */
12769 type = cp_parser_type_name (parser);
12770 /* Keep track of all name-lookups performed in class scopes. */
12774 && TREE_CODE (type) == TYPE_DECL
12775 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12776 maybe_note_name_used_in_class (DECL_NAME (type), type);
12777 /* If it didn't work out, we don't have a TYPE. */
12778 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12779 && !cp_parser_parse_definitely (parser))
12781 if (type && decl_specs)
12782 cp_parser_set_decl_spec_type (decl_specs, type,
12784 /*user_defined=*/true);
12787 /* If we didn't get a type-name, issue an error message. */
12788 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12790 cp_parser_error (parser, "expected type-name");
12791 return error_mark_node;
12794 if (type && type != error_mark_node)
12796 /* See if TYPE is an Objective-C type, and if so, parse and
12797 accept any protocol references following it. Do this before
12798 the cp_parser_check_for_invalid_template_id() call, because
12799 Objective-C types can be followed by '<...>' which would
12800 enclose protocol names rather than template arguments, and so
12801 everything is fine. */
12802 if (c_dialect_objc () && !parser->scope
12803 && (objc_is_id (type) || objc_is_class_name (type)))
12805 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12806 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12808 /* Clobber the "unqualified" type previously entered into
12809 DECL_SPECS with the new, improved protocol-qualified version. */
12811 decl_specs->type = qual_type;
12816 /* There is no valid C++ program where a non-template type is
12817 followed by a "<". That usually indicates that the user
12818 thought that the type was a template. */
12819 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12826 /* Parse a type-name.
12839 Returns a TYPE_DECL for the type. */
12842 cp_parser_type_name (cp_parser* parser)
12846 /* We can't know yet whether it is a class-name or not. */
12847 cp_parser_parse_tentatively (parser);
12848 /* Try a class-name. */
12849 type_decl = cp_parser_class_name (parser,
12850 /*typename_keyword_p=*/false,
12851 /*template_keyword_p=*/false,
12853 /*check_dependency_p=*/true,
12854 /*class_head_p=*/false,
12855 /*is_declaration=*/false);
12856 /* If it's not a class-name, keep looking. */
12857 if (!cp_parser_parse_definitely (parser))
12859 /* It must be a typedef-name or an enum-name. */
12860 return cp_parser_nonclass_name (parser);
12866 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12874 Returns a TYPE_DECL for the type. */
12877 cp_parser_nonclass_name (cp_parser* parser)
12882 cp_token *token = cp_lexer_peek_token (parser->lexer);
12883 identifier = cp_parser_identifier (parser);
12884 if (identifier == error_mark_node)
12885 return error_mark_node;
12887 /* Look up the type-name. */
12888 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12890 if (TREE_CODE (type_decl) != TYPE_DECL
12891 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12893 /* See if this is an Objective-C type. */
12894 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12895 tree type = objc_get_protocol_qualified_type (identifier, protos);
12897 type_decl = TYPE_NAME (type);
12900 /* Issue an error if we did not find a type-name. */
12901 if (TREE_CODE (type_decl) != TYPE_DECL
12902 /* In Objective-C, we have the complication that class names are
12903 normally type names and start declarations (eg, the
12904 "NSObject" in "NSObject *object;"), but can be used in an
12905 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12906 is an expression. So, a classname followed by a dot is not a
12907 valid type-name. */
12908 || (objc_is_class_name (TREE_TYPE (type_decl))
12909 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12911 if (!cp_parser_simulate_error (parser))
12912 cp_parser_name_lookup_error (parser, identifier, type_decl,
12913 NLE_TYPE, token->location);
12914 return error_mark_node;
12916 /* Remember that the name was used in the definition of the
12917 current class so that we can check later to see if the
12918 meaning would have been different after the class was
12919 entirely defined. */
12920 else if (type_decl != error_mark_node
12922 maybe_note_name_used_in_class (identifier, type_decl);
12927 /* Parse an elaborated-type-specifier. Note that the grammar given
12928 here incorporates the resolution to DR68.
12930 elaborated-type-specifier:
12931 class-key :: [opt] nested-name-specifier [opt] identifier
12932 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12933 enum-key :: [opt] nested-name-specifier [opt] identifier
12934 typename :: [opt] nested-name-specifier identifier
12935 typename :: [opt] nested-name-specifier template [opt]
12940 elaborated-type-specifier:
12941 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12942 class-key attributes :: [opt] nested-name-specifier [opt]
12943 template [opt] template-id
12944 enum attributes :: [opt] nested-name-specifier [opt] identifier
12946 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12947 declared `friend'. If IS_DECLARATION is TRUE, then this
12948 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12949 something is being declared.
12951 Returns the TYPE specified. */
12954 cp_parser_elaborated_type_specifier (cp_parser* parser,
12956 bool is_declaration)
12958 enum tag_types tag_type;
12960 tree type = NULL_TREE;
12961 tree attributes = NULL_TREE;
12963 cp_token *token = NULL;
12965 /* See if we're looking at the `enum' keyword. */
12966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12968 /* Consume the `enum' token. */
12969 cp_lexer_consume_token (parser->lexer);
12970 /* Remember that it's an enumeration type. */
12971 tag_type = enum_type;
12972 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12973 enums) is used here. */
12974 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12975 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12977 pedwarn (input_location, 0, "elaborated-type-specifier "
12978 "for a scoped enum must not use the %<%D%> keyword",
12979 cp_lexer_peek_token (parser->lexer)->u.value);
12980 /* Consume the `struct' or `class' and parse it anyway. */
12981 cp_lexer_consume_token (parser->lexer);
12983 /* Parse the attributes. */
12984 attributes = cp_parser_attributes_opt (parser);
12986 /* Or, it might be `typename'. */
12987 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12990 /* Consume the `typename' token. */
12991 cp_lexer_consume_token (parser->lexer);
12992 /* Remember that it's a `typename' type. */
12993 tag_type = typename_type;
12995 /* Otherwise it must be a class-key. */
12998 tag_type = cp_parser_class_key (parser);
12999 if (tag_type == none_type)
13000 return error_mark_node;
13001 /* Parse the attributes. */
13002 attributes = cp_parser_attributes_opt (parser);
13005 /* Look for the `::' operator. */
13006 globalscope = cp_parser_global_scope_opt (parser,
13007 /*current_scope_valid_p=*/false);
13008 /* Look for the nested-name-specifier. */
13009 if (tag_type == typename_type && !globalscope)
13011 if (!cp_parser_nested_name_specifier (parser,
13012 /*typename_keyword_p=*/true,
13013 /*check_dependency_p=*/true,
13016 return error_mark_node;
13019 /* Even though `typename' is not present, the proposed resolution
13020 to Core Issue 180 says that in `class A<T>::B', `B' should be
13021 considered a type-name, even if `A<T>' is dependent. */
13022 cp_parser_nested_name_specifier_opt (parser,
13023 /*typename_keyword_p=*/true,
13024 /*check_dependency_p=*/true,
13027 /* For everything but enumeration types, consider a template-id.
13028 For an enumeration type, consider only a plain identifier. */
13029 if (tag_type != enum_type)
13031 bool template_p = false;
13034 /* Allow the `template' keyword. */
13035 template_p = cp_parser_optional_template_keyword (parser);
13036 /* If we didn't see `template', we don't know if there's a
13037 template-id or not. */
13039 cp_parser_parse_tentatively (parser);
13040 /* Parse the template-id. */
13041 token = cp_lexer_peek_token (parser->lexer);
13042 decl = cp_parser_template_id (parser, template_p,
13043 /*check_dependency_p=*/true,
13045 /* If we didn't find a template-id, look for an ordinary
13047 if (!template_p && !cp_parser_parse_definitely (parser))
13049 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13050 in effect, then we must assume that, upon instantiation, the
13051 template will correspond to a class. */
13052 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13053 && tag_type == typename_type)
13054 type = make_typename_type (parser->scope, decl,
13056 /*complain=*/tf_error);
13057 /* If the `typename' keyword is in effect and DECL is not a type
13058 decl. Then type is non existant. */
13059 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13062 type = TREE_TYPE (decl);
13067 token = cp_lexer_peek_token (parser->lexer);
13068 identifier = cp_parser_identifier (parser);
13070 if (identifier == error_mark_node)
13072 parser->scope = NULL_TREE;
13073 return error_mark_node;
13076 /* For a `typename', we needn't call xref_tag. */
13077 if (tag_type == typename_type
13078 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13079 return cp_parser_make_typename_type (parser, parser->scope,
13082 /* Look up a qualified name in the usual way. */
13086 tree ambiguous_decls;
13088 decl = cp_parser_lookup_name (parser, identifier,
13090 /*is_template=*/false,
13091 /*is_namespace=*/false,
13092 /*check_dependency=*/true,
13096 /* If the lookup was ambiguous, an error will already have been
13098 if (ambiguous_decls)
13099 return error_mark_node;
13101 /* If we are parsing friend declaration, DECL may be a
13102 TEMPLATE_DECL tree node here. However, we need to check
13103 whether this TEMPLATE_DECL results in valid code. Consider
13104 the following example:
13107 template <class T> class C {};
13110 template <class T> friend class N::C; // #1, valid code
13112 template <class T> class Y {
13113 friend class N::C; // #2, invalid code
13116 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13117 name lookup of `N::C'. We see that friend declaration must
13118 be template for the code to be valid. Note that
13119 processing_template_decl does not work here since it is
13120 always 1 for the above two cases. */
13122 decl = (cp_parser_maybe_treat_template_as_class
13123 (decl, /*tag_name_p=*/is_friend
13124 && parser->num_template_parameter_lists));
13126 if (TREE_CODE (decl) != TYPE_DECL)
13128 cp_parser_diagnose_invalid_type_name (parser,
13132 return error_mark_node;
13135 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13137 bool allow_template = (parser->num_template_parameter_lists
13138 || DECL_SELF_REFERENCE_P (decl));
13139 type = check_elaborated_type_specifier (tag_type, decl,
13142 if (type == error_mark_node)
13143 return error_mark_node;
13146 /* Forward declarations of nested types, such as
13151 are invalid unless all components preceding the final '::'
13152 are complete. If all enclosing types are complete, these
13153 declarations become merely pointless.
13155 Invalid forward declarations of nested types are errors
13156 caught elsewhere in parsing. Those that are pointless arrive
13159 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13160 && !is_friend && !processing_explicit_instantiation)
13161 warning (0, "declaration %qD does not declare anything", decl);
13163 type = TREE_TYPE (decl);
13167 /* An elaborated-type-specifier sometimes introduces a new type and
13168 sometimes names an existing type. Normally, the rule is that it
13169 introduces a new type only if there is not an existing type of
13170 the same name already in scope. For example, given:
13173 void f() { struct S s; }
13175 the `struct S' in the body of `f' is the same `struct S' as in
13176 the global scope; the existing definition is used. However, if
13177 there were no global declaration, this would introduce a new
13178 local class named `S'.
13180 An exception to this rule applies to the following code:
13182 namespace N { struct S; }
13184 Here, the elaborated-type-specifier names a new type
13185 unconditionally; even if there is already an `S' in the
13186 containing scope this declaration names a new type.
13187 This exception only applies if the elaborated-type-specifier
13188 forms the complete declaration:
13192 A declaration consisting solely of `class-key identifier ;' is
13193 either a redeclaration of the name in the current scope or a
13194 forward declaration of the identifier as a class name. It
13195 introduces the name into the current scope.
13197 We are in this situation precisely when the next token is a `;'.
13199 An exception to the exception is that a `friend' declaration does
13200 *not* name a new type; i.e., given:
13202 struct S { friend struct T; };
13204 `T' is not a new type in the scope of `S'.
13206 Also, `new struct S' or `sizeof (struct S)' never results in the
13207 definition of a new type; a new type can only be declared in a
13208 declaration context. */
13214 /* Friends have special name lookup rules. */
13215 ts = ts_within_enclosing_non_class;
13216 else if (is_declaration
13217 && cp_lexer_next_token_is (parser->lexer,
13219 /* This is a `class-key identifier ;' */
13225 (parser->num_template_parameter_lists
13226 && (cp_parser_next_token_starts_class_definition_p (parser)
13227 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13228 /* An unqualified name was used to reference this type, so
13229 there were no qualifying templates. */
13230 if (!cp_parser_check_template_parameters (parser,
13231 /*num_templates=*/0,
13233 /*declarator=*/NULL))
13234 return error_mark_node;
13235 type = xref_tag (tag_type, identifier, ts, template_p);
13239 if (type == error_mark_node)
13240 return error_mark_node;
13242 /* Allow attributes on forward declarations of classes. */
13245 if (TREE_CODE (type) == TYPENAME_TYPE)
13246 warning (OPT_Wattributes,
13247 "attributes ignored on uninstantiated type");
13248 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13249 && ! processing_explicit_instantiation)
13250 warning (OPT_Wattributes,
13251 "attributes ignored on template instantiation");
13252 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13253 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13255 warning (OPT_Wattributes,
13256 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13259 if (tag_type != enum_type)
13260 cp_parser_check_class_key (tag_type, type);
13262 /* A "<" cannot follow an elaborated type specifier. If that
13263 happens, the user was probably trying to form a template-id. */
13264 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13269 /* Parse an enum-specifier.
13272 enum-head { enumerator-list [opt] }
13275 enum-key identifier [opt] enum-base [opt]
13276 enum-key nested-name-specifier identifier enum-base [opt]
13281 enum struct [C++0x]
13284 : type-specifier-seq
13286 opaque-enum-specifier:
13287 enum-key identifier enum-base [opt] ;
13290 enum-key attributes[opt] identifier [opt] enum-base [opt]
13291 { enumerator-list [opt] }attributes[opt]
13293 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13294 if the token stream isn't an enum-specifier after all. */
13297 cp_parser_enum_specifier (cp_parser* parser)
13300 tree type = NULL_TREE;
13302 tree nested_name_specifier = NULL_TREE;
13304 bool scoped_enum_p = false;
13305 bool has_underlying_type = false;
13306 bool nested_being_defined = false;
13307 bool new_value_list = false;
13308 bool is_new_type = false;
13309 bool is_anonymous = false;
13310 tree underlying_type = NULL_TREE;
13311 cp_token *type_start_token = NULL;
13312 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13314 parser->colon_corrects_to_scope_p = false;
13316 /* Parse tentatively so that we can back up if we don't find a
13318 cp_parser_parse_tentatively (parser);
13320 /* Caller guarantees that the current token is 'enum', an identifier
13321 possibly follows, and the token after that is an opening brace.
13322 If we don't have an identifier, fabricate an anonymous name for
13323 the enumeration being defined. */
13324 cp_lexer_consume_token (parser->lexer);
13326 /* Parse the "class" or "struct", which indicates a scoped
13327 enumeration type in C++0x. */
13328 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13329 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13331 if (cxx_dialect < cxx0x)
13332 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13334 /* Consume the `struct' or `class' token. */
13335 cp_lexer_consume_token (parser->lexer);
13337 scoped_enum_p = true;
13340 attributes = cp_parser_attributes_opt (parser);
13342 /* Clear the qualification. */
13343 parser->scope = NULL_TREE;
13344 parser->qualifying_scope = NULL_TREE;
13345 parser->object_scope = NULL_TREE;
13347 /* Figure out in what scope the declaration is being placed. */
13348 prev_scope = current_scope ();
13350 type_start_token = cp_lexer_peek_token (parser->lexer);
13352 push_deferring_access_checks (dk_no_check);
13353 nested_name_specifier
13354 = cp_parser_nested_name_specifier_opt (parser,
13355 /*typename_keyword_p=*/true,
13356 /*check_dependency_p=*/false,
13358 /*is_declaration=*/false);
13360 if (nested_name_specifier)
13364 identifier = cp_parser_identifier (parser);
13365 name = cp_parser_lookup_name (parser, identifier,
13367 /*is_template=*/false,
13368 /*is_namespace=*/false,
13369 /*check_dependency=*/true,
13370 /*ambiguous_decls=*/NULL,
13374 type = TREE_TYPE (name);
13375 if (TREE_CODE (type) == TYPENAME_TYPE)
13377 /* Are template enums allowed in ISO? */
13378 if (template_parm_scope_p ())
13379 pedwarn (type_start_token->location, OPT_pedantic,
13380 "%qD is an enumeration template", name);
13381 /* ignore a typename reference, for it will be solved by name
13387 error_at (type_start_token->location,
13388 "%qD is not an enumerator-name", identifier);
13392 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13393 identifier = cp_parser_identifier (parser);
13396 identifier = make_anon_name ();
13397 is_anonymous = true;
13400 pop_deferring_access_checks ();
13402 /* Check for the `:' that denotes a specified underlying type in C++0x.
13403 Note that a ':' could also indicate a bitfield width, however. */
13404 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13406 cp_decl_specifier_seq type_specifiers;
13408 /* Consume the `:'. */
13409 cp_lexer_consume_token (parser->lexer);
13411 /* Parse the type-specifier-seq. */
13412 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13413 /*is_trailing_return=*/false,
13416 /* At this point this is surely not elaborated type specifier. */
13417 if (!cp_parser_parse_definitely (parser))
13420 if (cxx_dialect < cxx0x)
13421 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13423 has_underlying_type = true;
13425 /* If that didn't work, stop. */
13426 if (type_specifiers.type != error_mark_node)
13428 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13429 /*initialized=*/0, NULL);
13430 if (underlying_type == error_mark_node)
13431 underlying_type = NULL_TREE;
13435 /* Look for the `{' but don't consume it yet. */
13436 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13438 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13440 cp_parser_error (parser, "expected %<{%>");
13441 if (has_underlying_type)
13447 /* An opaque-enum-specifier must have a ';' here. */
13448 if ((scoped_enum_p || underlying_type)
13449 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13451 cp_parser_error (parser, "expected %<;%> or %<{%>");
13452 if (has_underlying_type)
13460 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13463 if (nested_name_specifier)
13465 if (CLASS_TYPE_P (nested_name_specifier))
13467 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13468 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13469 push_scope (nested_name_specifier);
13471 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13473 push_nested_namespace (nested_name_specifier);
13477 /* Issue an error message if type-definitions are forbidden here. */
13478 if (!cp_parser_check_type_definition (parser))
13479 type = error_mark_node;
13481 /* Create the new type. We do this before consuming the opening
13482 brace so the enum will be recorded as being on the line of its
13483 tag (or the 'enum' keyword, if there is no tag). */
13484 type = start_enum (identifier, type, underlying_type,
13485 scoped_enum_p, &is_new_type);
13487 /* If the next token is not '{' it is an opaque-enum-specifier or an
13488 elaborated-type-specifier. */
13489 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13491 timevar_push (TV_PARSE_ENUM);
13492 if (nested_name_specifier)
13494 /* The following catches invalid code such as:
13495 enum class S<int>::E { A, B, C }; */
13496 if (!processing_specialization
13497 && CLASS_TYPE_P (nested_name_specifier)
13498 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13499 error_at (type_start_token->location, "cannot add an enumerator "
13500 "list to a template instantiation");
13502 /* If that scope does not contain the scope in which the
13503 class was originally declared, the program is invalid. */
13504 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13506 if (at_namespace_scope_p ())
13507 error_at (type_start_token->location,
13508 "declaration of %qD in namespace %qD which does not "
13510 type, prev_scope, nested_name_specifier);
13512 error_at (type_start_token->location,
13513 "declaration of %qD in %qD which does not enclose %qD",
13514 type, prev_scope, nested_name_specifier);
13515 type = error_mark_node;
13520 begin_scope (sk_scoped_enum, type);
13522 /* Consume the opening brace. */
13523 cp_lexer_consume_token (parser->lexer);
13525 if (type == error_mark_node)
13526 ; /* Nothing to add */
13527 else if (OPAQUE_ENUM_P (type)
13528 || (cxx_dialect > cxx98 && processing_specialization))
13530 new_value_list = true;
13531 SET_OPAQUE_ENUM_P (type, false);
13532 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13536 error_at (type_start_token->location, "multiple definition of %q#T", type);
13537 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13538 "previous definition here");
13539 type = error_mark_node;
13542 if (type == error_mark_node)
13543 cp_parser_skip_to_end_of_block_or_statement (parser);
13544 /* If the next token is not '}', then there are some enumerators. */
13545 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13546 cp_parser_enumerator_list (parser, type);
13548 /* Consume the final '}'. */
13549 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13553 timevar_pop (TV_PARSE_ENUM);
13557 /* If a ';' follows, then it is an opaque-enum-specifier
13558 and additional restrictions apply. */
13559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13562 error_at (type_start_token->location,
13563 "opaque-enum-specifier without name");
13564 else if (nested_name_specifier)
13565 error_at (type_start_token->location,
13566 "opaque-enum-specifier must use a simple identifier");
13570 /* Look for trailing attributes to apply to this enumeration, and
13571 apply them if appropriate. */
13572 if (cp_parser_allow_gnu_extensions_p (parser))
13574 tree trailing_attr = cp_parser_attributes_opt (parser);
13575 trailing_attr = chainon (trailing_attr, attributes);
13576 cplus_decl_attributes (&type,
13578 (int) ATTR_FLAG_TYPE_IN_PLACE);
13581 /* Finish up the enumeration. */
13582 if (type != error_mark_node)
13584 if (new_value_list)
13585 finish_enum_value_list (type);
13587 finish_enum (type);
13590 if (nested_name_specifier)
13592 if (CLASS_TYPE_P (nested_name_specifier))
13594 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13595 pop_scope (nested_name_specifier);
13597 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13599 pop_nested_namespace (nested_name_specifier);
13603 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13607 /* Parse an enumerator-list. The enumerators all have the indicated
13611 enumerator-definition
13612 enumerator-list , enumerator-definition */
13615 cp_parser_enumerator_list (cp_parser* parser, tree type)
13619 /* Parse an enumerator-definition. */
13620 cp_parser_enumerator_definition (parser, type);
13622 /* If the next token is not a ',', we've reached the end of
13624 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13626 /* Otherwise, consume the `,' and keep going. */
13627 cp_lexer_consume_token (parser->lexer);
13628 /* If the next token is a `}', there is a trailing comma. */
13629 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13631 if (!in_system_header)
13632 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13638 /* Parse an enumerator-definition. The enumerator has the indicated
13641 enumerator-definition:
13643 enumerator = constant-expression
13649 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13655 /* Save the input location because we are interested in the location
13656 of the identifier and not the location of the explicit value. */
13657 loc = cp_lexer_peek_token (parser->lexer)->location;
13659 /* Look for the identifier. */
13660 identifier = cp_parser_identifier (parser);
13661 if (identifier == error_mark_node)
13664 /* If the next token is an '=', then there is an explicit value. */
13665 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13667 /* Consume the `=' token. */
13668 cp_lexer_consume_token (parser->lexer);
13669 /* Parse the value. */
13670 value = cp_parser_constant_expression (parser,
13671 /*allow_non_constant_p=*/false,
13677 /* If we are processing a template, make sure the initializer of the
13678 enumerator doesn't contain any bare template parameter pack. */
13679 if (check_for_bare_parameter_packs (value))
13680 value = error_mark_node;
13682 /* integral_constant_value will pull out this expression, so make sure
13683 it's folded as appropriate. */
13684 value = fold_non_dependent_expr (value);
13686 /* Create the enumerator. */
13687 build_enumerator (identifier, value, type, loc);
13690 /* Parse a namespace-name.
13693 original-namespace-name
13696 Returns the NAMESPACE_DECL for the namespace. */
13699 cp_parser_namespace_name (cp_parser* parser)
13702 tree namespace_decl;
13704 cp_token *token = cp_lexer_peek_token (parser->lexer);
13706 /* Get the name of the namespace. */
13707 identifier = cp_parser_identifier (parser);
13708 if (identifier == error_mark_node)
13709 return error_mark_node;
13711 /* Look up the identifier in the currently active scope. Look only
13712 for namespaces, due to:
13714 [basic.lookup.udir]
13716 When looking up a namespace-name in a using-directive or alias
13717 definition, only namespace names are considered.
13721 [basic.lookup.qual]
13723 During the lookup of a name preceding the :: scope resolution
13724 operator, object, function, and enumerator names are ignored.
13726 (Note that cp_parser_qualifying_entity only calls this
13727 function if the token after the name is the scope resolution
13729 namespace_decl = cp_parser_lookup_name (parser, identifier,
13731 /*is_template=*/false,
13732 /*is_namespace=*/true,
13733 /*check_dependency=*/true,
13734 /*ambiguous_decls=*/NULL,
13736 /* If it's not a namespace, issue an error. */
13737 if (namespace_decl == error_mark_node
13738 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13740 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13741 error_at (token->location, "%qD is not a namespace-name", identifier);
13742 cp_parser_error (parser, "expected namespace-name");
13743 namespace_decl = error_mark_node;
13746 return namespace_decl;
13749 /* Parse a namespace-definition.
13751 namespace-definition:
13752 named-namespace-definition
13753 unnamed-namespace-definition
13755 named-namespace-definition:
13756 original-namespace-definition
13757 extension-namespace-definition
13759 original-namespace-definition:
13760 namespace identifier { namespace-body }
13762 extension-namespace-definition:
13763 namespace original-namespace-name { namespace-body }
13765 unnamed-namespace-definition:
13766 namespace { namespace-body } */
13769 cp_parser_namespace_definition (cp_parser* parser)
13771 tree identifier, attribs;
13772 bool has_visibility;
13775 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13777 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13779 cp_lexer_consume_token (parser->lexer);
13784 /* Look for the `namespace' keyword. */
13785 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13787 /* Get the name of the namespace. We do not attempt to distinguish
13788 between an original-namespace-definition and an
13789 extension-namespace-definition at this point. The semantic
13790 analysis routines are responsible for that. */
13791 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13792 identifier = cp_parser_identifier (parser);
13794 identifier = NULL_TREE;
13796 /* Parse any specified attributes. */
13797 attribs = cp_parser_attributes_opt (parser);
13799 /* Look for the `{' to start the namespace. */
13800 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13801 /* Start the namespace. */
13802 push_namespace (identifier);
13804 /* "inline namespace" is equivalent to a stub namespace definition
13805 followed by a strong using directive. */
13808 tree name_space = current_namespace;
13809 /* Set up namespace association. */
13810 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13811 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13812 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13813 /* Import the contents of the inline namespace. */
13815 do_using_directive (name_space);
13816 push_namespace (identifier);
13819 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13821 /* Parse the body of the namespace. */
13822 cp_parser_namespace_body (parser);
13824 if (has_visibility)
13825 pop_visibility (1);
13827 /* Finish the namespace. */
13829 /* Look for the final `}'. */
13830 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13833 /* Parse a namespace-body.
13836 declaration-seq [opt] */
13839 cp_parser_namespace_body (cp_parser* parser)
13841 cp_parser_declaration_seq_opt (parser);
13844 /* Parse a namespace-alias-definition.
13846 namespace-alias-definition:
13847 namespace identifier = qualified-namespace-specifier ; */
13850 cp_parser_namespace_alias_definition (cp_parser* parser)
13853 tree namespace_specifier;
13855 cp_token *token = cp_lexer_peek_token (parser->lexer);
13857 /* Look for the `namespace' keyword. */
13858 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13859 /* Look for the identifier. */
13860 identifier = cp_parser_identifier (parser);
13861 if (identifier == error_mark_node)
13863 /* Look for the `=' token. */
13864 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13865 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13867 error_at (token->location, "%<namespace%> definition is not allowed here");
13868 /* Skip the definition. */
13869 cp_lexer_consume_token (parser->lexer);
13870 if (cp_parser_skip_to_closing_brace (parser))
13871 cp_lexer_consume_token (parser->lexer);
13874 cp_parser_require (parser, CPP_EQ, RT_EQ);
13875 /* Look for the qualified-namespace-specifier. */
13876 namespace_specifier
13877 = cp_parser_qualified_namespace_specifier (parser);
13878 /* Look for the `;' token. */
13879 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13881 /* Register the alias in the symbol table. */
13882 do_namespace_alias (identifier, namespace_specifier);
13885 /* Parse a qualified-namespace-specifier.
13887 qualified-namespace-specifier:
13888 :: [opt] nested-name-specifier [opt] namespace-name
13890 Returns a NAMESPACE_DECL corresponding to the specified
13894 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13896 /* Look for the optional `::'. */
13897 cp_parser_global_scope_opt (parser,
13898 /*current_scope_valid_p=*/false);
13900 /* Look for the optional nested-name-specifier. */
13901 cp_parser_nested_name_specifier_opt (parser,
13902 /*typename_keyword_p=*/false,
13903 /*check_dependency_p=*/true,
13905 /*is_declaration=*/true);
13907 return cp_parser_namespace_name (parser);
13910 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13911 access declaration.
13914 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13915 using :: unqualified-id ;
13917 access-declaration:
13923 cp_parser_using_declaration (cp_parser* parser,
13924 bool access_declaration_p)
13927 bool typename_p = false;
13928 bool global_scope_p;
13933 if (access_declaration_p)
13934 cp_parser_parse_tentatively (parser);
13937 /* Look for the `using' keyword. */
13938 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13940 /* Peek at the next token. */
13941 token = cp_lexer_peek_token (parser->lexer);
13942 /* See if it's `typename'. */
13943 if (token->keyword == RID_TYPENAME)
13945 /* Remember that we've seen it. */
13947 /* Consume the `typename' token. */
13948 cp_lexer_consume_token (parser->lexer);
13952 /* Look for the optional global scope qualification. */
13954 = (cp_parser_global_scope_opt (parser,
13955 /*current_scope_valid_p=*/false)
13958 /* If we saw `typename', or didn't see `::', then there must be a
13959 nested-name-specifier present. */
13960 if (typename_p || !global_scope_p)
13961 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13962 /*check_dependency_p=*/true,
13964 /*is_declaration=*/true);
13965 /* Otherwise, we could be in either of the two productions. In that
13966 case, treat the nested-name-specifier as optional. */
13968 qscope = cp_parser_nested_name_specifier_opt (parser,
13969 /*typename_keyword_p=*/false,
13970 /*check_dependency_p=*/true,
13972 /*is_declaration=*/true);
13974 qscope = global_namespace;
13976 if (access_declaration_p && cp_parser_error_occurred (parser))
13977 /* Something has already gone wrong; there's no need to parse
13978 further. Since an error has occurred, the return value of
13979 cp_parser_parse_definitely will be false, as required. */
13980 return cp_parser_parse_definitely (parser);
13982 token = cp_lexer_peek_token (parser->lexer);
13983 /* Parse the unqualified-id. */
13984 identifier = cp_parser_unqualified_id (parser,
13985 /*template_keyword_p=*/false,
13986 /*check_dependency_p=*/true,
13987 /*declarator_p=*/true,
13988 /*optional_p=*/false);
13990 if (access_declaration_p)
13992 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13993 cp_parser_simulate_error (parser);
13994 if (!cp_parser_parse_definitely (parser))
13998 /* The function we call to handle a using-declaration is different
13999 depending on what scope we are in. */
14000 if (qscope == error_mark_node || identifier == error_mark_node)
14002 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14003 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14004 /* [namespace.udecl]
14006 A using declaration shall not name a template-id. */
14007 error_at (token->location,
14008 "a template-id may not appear in a using-declaration");
14011 if (at_class_scope_p ())
14013 /* Create the USING_DECL. */
14014 decl = do_class_using_decl (parser->scope, identifier);
14016 if (check_for_bare_parameter_packs (decl))
14019 /* Add it to the list of members in this class. */
14020 finish_member_declaration (decl);
14024 decl = cp_parser_lookup_name_simple (parser,
14027 if (decl == error_mark_node)
14028 cp_parser_name_lookup_error (parser, identifier,
14031 else if (check_for_bare_parameter_packs (decl))
14033 else if (!at_namespace_scope_p ())
14034 do_local_using_decl (decl, qscope, identifier);
14036 do_toplevel_using_decl (decl, qscope, identifier);
14040 /* Look for the final `;'. */
14041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14046 /* Parse a using-directive.
14049 using namespace :: [opt] nested-name-specifier [opt]
14050 namespace-name ; */
14053 cp_parser_using_directive (cp_parser* parser)
14055 tree namespace_decl;
14058 /* Look for the `using' keyword. */
14059 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14060 /* And the `namespace' keyword. */
14061 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14062 /* Look for the optional `::' operator. */
14063 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14064 /* And the optional nested-name-specifier. */
14065 cp_parser_nested_name_specifier_opt (parser,
14066 /*typename_keyword_p=*/false,
14067 /*check_dependency_p=*/true,
14069 /*is_declaration=*/true);
14070 /* Get the namespace being used. */
14071 namespace_decl = cp_parser_namespace_name (parser);
14072 /* And any specified attributes. */
14073 attribs = cp_parser_attributes_opt (parser);
14074 /* Update the symbol table. */
14075 parse_using_directive (namespace_decl, attribs);
14076 /* Look for the final `;'. */
14077 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14080 /* Parse an asm-definition.
14083 asm ( string-literal ) ;
14088 asm volatile [opt] ( string-literal ) ;
14089 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14090 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14091 : asm-operand-list [opt] ) ;
14092 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14093 : asm-operand-list [opt]
14094 : asm-clobber-list [opt] ) ;
14095 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14096 : asm-clobber-list [opt]
14097 : asm-goto-list ) ; */
14100 cp_parser_asm_definition (cp_parser* parser)
14103 tree outputs = NULL_TREE;
14104 tree inputs = NULL_TREE;
14105 tree clobbers = NULL_TREE;
14106 tree labels = NULL_TREE;
14108 bool volatile_p = false;
14109 bool extended_p = false;
14110 bool invalid_inputs_p = false;
14111 bool invalid_outputs_p = false;
14112 bool goto_p = false;
14113 required_token missing = RT_NONE;
14115 /* Look for the `asm' keyword. */
14116 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14117 /* See if the next token is `volatile'. */
14118 if (cp_parser_allow_gnu_extensions_p (parser)
14119 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14121 /* Remember that we saw the `volatile' keyword. */
14123 /* Consume the token. */
14124 cp_lexer_consume_token (parser->lexer);
14126 if (cp_parser_allow_gnu_extensions_p (parser)
14127 && parser->in_function_body
14128 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14130 /* Remember that we saw the `goto' keyword. */
14132 /* Consume the token. */
14133 cp_lexer_consume_token (parser->lexer);
14135 /* Look for the opening `('. */
14136 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14138 /* Look for the string. */
14139 string = cp_parser_string_literal (parser, false, false);
14140 if (string == error_mark_node)
14142 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14143 /*consume_paren=*/true);
14147 /* If we're allowing GNU extensions, check for the extended assembly
14148 syntax. Unfortunately, the `:' tokens need not be separated by
14149 a space in C, and so, for compatibility, we tolerate that here
14150 too. Doing that means that we have to treat the `::' operator as
14152 if (cp_parser_allow_gnu_extensions_p (parser)
14153 && parser->in_function_body
14154 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14155 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14157 bool inputs_p = false;
14158 bool clobbers_p = false;
14159 bool labels_p = false;
14161 /* The extended syntax was used. */
14164 /* Look for outputs. */
14165 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14167 /* Consume the `:'. */
14168 cp_lexer_consume_token (parser->lexer);
14169 /* Parse the output-operands. */
14170 if (cp_lexer_next_token_is_not (parser->lexer,
14172 && cp_lexer_next_token_is_not (parser->lexer,
14174 && cp_lexer_next_token_is_not (parser->lexer,
14177 outputs = cp_parser_asm_operand_list (parser);
14179 if (outputs == error_mark_node)
14180 invalid_outputs_p = true;
14182 /* If the next token is `::', there are no outputs, and the
14183 next token is the beginning of the inputs. */
14184 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14185 /* The inputs are coming next. */
14188 /* Look for inputs. */
14190 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14192 /* Consume the `:' or `::'. */
14193 cp_lexer_consume_token (parser->lexer);
14194 /* Parse the output-operands. */
14195 if (cp_lexer_next_token_is_not (parser->lexer,
14197 && cp_lexer_next_token_is_not (parser->lexer,
14199 && cp_lexer_next_token_is_not (parser->lexer,
14201 inputs = cp_parser_asm_operand_list (parser);
14203 if (inputs == error_mark_node)
14204 invalid_inputs_p = true;
14206 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14207 /* The clobbers are coming next. */
14210 /* Look for clobbers. */
14212 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14215 /* Consume the `:' or `::'. */
14216 cp_lexer_consume_token (parser->lexer);
14217 /* Parse the clobbers. */
14218 if (cp_lexer_next_token_is_not (parser->lexer,
14220 && cp_lexer_next_token_is_not (parser->lexer,
14222 clobbers = cp_parser_asm_clobber_list (parser);
14225 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14226 /* The labels are coming next. */
14229 /* Look for labels. */
14231 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14234 /* Consume the `:' or `::'. */
14235 cp_lexer_consume_token (parser->lexer);
14236 /* Parse the labels. */
14237 labels = cp_parser_asm_label_list (parser);
14240 if (goto_p && !labels_p)
14241 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14244 missing = RT_COLON_SCOPE;
14246 /* Look for the closing `)'. */
14247 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14248 missing ? missing : RT_CLOSE_PAREN))
14249 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14250 /*consume_paren=*/true);
14251 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14253 if (!invalid_inputs_p && !invalid_outputs_p)
14255 /* Create the ASM_EXPR. */
14256 if (parser->in_function_body)
14258 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14259 inputs, clobbers, labels);
14260 /* If the extended syntax was not used, mark the ASM_EXPR. */
14263 tree temp = asm_stmt;
14264 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14265 temp = TREE_OPERAND (temp, 0);
14267 ASM_INPUT_P (temp) = 1;
14271 cgraph_add_asm_node (string);
14275 /* Declarators [gram.dcl.decl] */
14277 /* Parse an init-declarator.
14280 declarator initializer [opt]
14285 declarator asm-specification [opt] attributes [opt] initializer [opt]
14287 function-definition:
14288 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14290 decl-specifier-seq [opt] declarator function-try-block
14294 function-definition:
14295 __extension__ function-definition
14297 The DECL_SPECIFIERS apply to this declarator. Returns a
14298 representation of the entity declared. If MEMBER_P is TRUE, then
14299 this declarator appears in a class scope. The new DECL created by
14300 this declarator is returned.
14302 The CHECKS are access checks that should be performed once we know
14303 what entity is being declared (and, therefore, what classes have
14306 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14307 for a function-definition here as well. If the declarator is a
14308 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14309 be TRUE upon return. By that point, the function-definition will
14310 have been completely parsed.
14312 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14315 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14316 parsed declaration if it is an uninitialized single declarator not followed
14317 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14318 if present, will not be consumed. If returned, this declarator will be
14319 created with SD_INITIALIZED but will not call cp_finish_decl. */
14322 cp_parser_init_declarator (cp_parser* parser,
14323 cp_decl_specifier_seq *decl_specifiers,
14324 VEC (deferred_access_check,gc)* checks,
14325 bool function_definition_allowed_p,
14327 int declares_class_or_enum,
14328 bool* function_definition_p,
14329 tree* maybe_range_for_decl)
14331 cp_token *token = NULL, *asm_spec_start_token = NULL,
14332 *attributes_start_token = NULL;
14333 cp_declarator *declarator;
14334 tree prefix_attributes;
14336 tree asm_specification;
14338 tree decl = NULL_TREE;
14340 int is_initialized;
14341 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14342 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14344 enum cpp_ttype initialization_kind;
14345 bool is_direct_init = false;
14346 bool is_non_constant_init;
14347 int ctor_dtor_or_conv_p;
14349 tree pushed_scope = NULL_TREE;
14350 bool range_for_decl_p = false;
14352 /* Gather the attributes that were provided with the
14353 decl-specifiers. */
14354 prefix_attributes = decl_specifiers->attributes;
14356 /* Assume that this is not the declarator for a function
14358 if (function_definition_p)
14359 *function_definition_p = false;
14361 /* Defer access checks while parsing the declarator; we cannot know
14362 what names are accessible until we know what is being
14364 resume_deferring_access_checks ();
14366 /* Parse the declarator. */
14367 token = cp_lexer_peek_token (parser->lexer);
14369 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14370 &ctor_dtor_or_conv_p,
14371 /*parenthesized_p=*/NULL,
14372 /*member_p=*/false);
14373 /* Gather up the deferred checks. */
14374 stop_deferring_access_checks ();
14376 /* If the DECLARATOR was erroneous, there's no need to go
14378 if (declarator == cp_error_declarator)
14379 return error_mark_node;
14381 /* Check that the number of template-parameter-lists is OK. */
14382 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14384 return error_mark_node;
14386 if (declares_class_or_enum & 2)
14387 cp_parser_check_for_definition_in_return_type (declarator,
14388 decl_specifiers->type,
14389 decl_specifiers->type_location);
14391 /* Figure out what scope the entity declared by the DECLARATOR is
14392 located in. `grokdeclarator' sometimes changes the scope, so
14393 we compute it now. */
14394 scope = get_scope_of_declarator (declarator);
14396 /* Perform any lookups in the declared type which were thought to be
14397 dependent, but are not in the scope of the declarator. */
14398 decl_specifiers->type
14399 = maybe_update_decl_type (decl_specifiers->type, scope);
14401 /* If we're allowing GNU extensions, look for an asm-specification
14403 if (cp_parser_allow_gnu_extensions_p (parser))
14405 /* Look for an asm-specification. */
14406 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14407 asm_specification = cp_parser_asm_specification_opt (parser);
14408 /* And attributes. */
14409 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14410 attributes = cp_parser_attributes_opt (parser);
14414 asm_specification = NULL_TREE;
14415 attributes = NULL_TREE;
14418 /* Peek at the next token. */
14419 token = cp_lexer_peek_token (parser->lexer);
14420 /* Check to see if the token indicates the start of a
14421 function-definition. */
14422 if (function_declarator_p (declarator)
14423 && cp_parser_token_starts_function_definition_p (token))
14425 if (!function_definition_allowed_p)
14427 /* If a function-definition should not appear here, issue an
14429 cp_parser_error (parser,
14430 "a function-definition is not allowed here");
14431 return error_mark_node;
14435 location_t func_brace_location
14436 = cp_lexer_peek_token (parser->lexer)->location;
14438 /* Neither attributes nor an asm-specification are allowed
14439 on a function-definition. */
14440 if (asm_specification)
14441 error_at (asm_spec_start_token->location,
14442 "an asm-specification is not allowed "
14443 "on a function-definition");
14445 error_at (attributes_start_token->location,
14446 "attributes are not allowed on a function-definition");
14447 /* This is a function-definition. */
14448 *function_definition_p = true;
14450 /* Parse the function definition. */
14452 decl = cp_parser_save_member_function_body (parser,
14455 prefix_attributes);
14458 = (cp_parser_function_definition_from_specifiers_and_declarator
14459 (parser, decl_specifiers, prefix_attributes, declarator));
14461 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14463 /* This is where the prologue starts... */
14464 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14465 = func_brace_location;
14474 Only in function declarations for constructors, destructors, and
14475 type conversions can the decl-specifier-seq be omitted.
14477 We explicitly postpone this check past the point where we handle
14478 function-definitions because we tolerate function-definitions
14479 that are missing their return types in some modes. */
14480 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14482 cp_parser_error (parser,
14483 "expected constructor, destructor, or type conversion");
14484 return error_mark_node;
14487 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14488 if (token->type == CPP_EQ
14489 || token->type == CPP_OPEN_PAREN
14490 || token->type == CPP_OPEN_BRACE)
14492 is_initialized = SD_INITIALIZED;
14493 initialization_kind = token->type;
14494 if (maybe_range_for_decl)
14495 *maybe_range_for_decl = error_mark_node;
14497 if (token->type == CPP_EQ
14498 && function_declarator_p (declarator))
14500 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14501 if (t2->keyword == RID_DEFAULT)
14502 is_initialized = SD_DEFAULTED;
14503 else if (t2->keyword == RID_DELETE)
14504 is_initialized = SD_DELETED;
14509 /* If the init-declarator isn't initialized and isn't followed by a
14510 `,' or `;', it's not a valid init-declarator. */
14511 if (token->type != CPP_COMMA
14512 && token->type != CPP_SEMICOLON)
14514 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14515 range_for_decl_p = true;
14518 cp_parser_error (parser, "expected initializer");
14519 return error_mark_node;
14522 is_initialized = SD_UNINITIALIZED;
14523 initialization_kind = CPP_EOF;
14526 /* Because start_decl has side-effects, we should only call it if we
14527 know we're going ahead. By this point, we know that we cannot
14528 possibly be looking at any other construct. */
14529 cp_parser_commit_to_tentative_parse (parser);
14531 /* If the decl specifiers were bad, issue an error now that we're
14532 sure this was intended to be a declarator. Then continue
14533 declaring the variable(s), as int, to try to cut down on further
14535 if (decl_specifiers->any_specifiers_p
14536 && decl_specifiers->type == error_mark_node)
14538 cp_parser_error (parser, "invalid type in declaration");
14539 decl_specifiers->type = integer_type_node;
14542 /* Check to see whether or not this declaration is a friend. */
14543 friend_p = cp_parser_friend_p (decl_specifiers);
14545 /* Enter the newly declared entry in the symbol table. If we're
14546 processing a declaration in a class-specifier, we wait until
14547 after processing the initializer. */
14550 if (parser->in_unbraced_linkage_specification_p)
14551 decl_specifiers->storage_class = sc_extern;
14552 decl = start_decl (declarator, decl_specifiers,
14553 range_for_decl_p? SD_INITIALIZED : is_initialized,
14554 attributes, prefix_attributes,
14556 /* Adjust location of decl if declarator->id_loc is more appropriate:
14557 set, and decl wasn't merged with another decl, in which case its
14558 location would be different from input_location, and more accurate. */
14560 && declarator->id_loc != UNKNOWN_LOCATION
14561 && DECL_SOURCE_LOCATION (decl) == input_location)
14562 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14565 /* Enter the SCOPE. That way unqualified names appearing in the
14566 initializer will be looked up in SCOPE. */
14567 pushed_scope = push_scope (scope);
14569 /* Perform deferred access control checks, now that we know in which
14570 SCOPE the declared entity resides. */
14571 if (!member_p && decl)
14573 tree saved_current_function_decl = NULL_TREE;
14575 /* If the entity being declared is a function, pretend that we
14576 are in its scope. If it is a `friend', it may have access to
14577 things that would not otherwise be accessible. */
14578 if (TREE_CODE (decl) == FUNCTION_DECL)
14580 saved_current_function_decl = current_function_decl;
14581 current_function_decl = decl;
14584 /* Perform access checks for template parameters. */
14585 cp_parser_perform_template_parameter_access_checks (checks);
14587 /* Perform the access control checks for the declarator and the
14588 decl-specifiers. */
14589 perform_deferred_access_checks ();
14591 /* Restore the saved value. */
14592 if (TREE_CODE (decl) == FUNCTION_DECL)
14593 current_function_decl = saved_current_function_decl;
14596 /* Parse the initializer. */
14597 initializer = NULL_TREE;
14598 is_direct_init = false;
14599 is_non_constant_init = true;
14600 if (is_initialized)
14602 if (function_declarator_p (declarator))
14604 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14605 if (initialization_kind == CPP_EQ)
14606 initializer = cp_parser_pure_specifier (parser);
14609 /* If the declaration was erroneous, we don't really
14610 know what the user intended, so just silently
14611 consume the initializer. */
14612 if (decl != error_mark_node)
14613 error_at (initializer_start_token->location,
14614 "initializer provided for function");
14615 cp_parser_skip_to_closing_parenthesis (parser,
14616 /*recovering=*/true,
14617 /*or_comma=*/false,
14618 /*consume_paren=*/true);
14623 /* We want to record the extra mangling scope for in-class
14624 initializers of class members and initializers of static data
14625 member templates. The former is a C++0x feature which isn't
14626 implemented yet, and I expect it will involve deferring
14627 parsing of the initializer until end of class as with default
14628 arguments. So right here we only handle the latter. */
14629 if (!member_p && processing_template_decl)
14630 start_lambda_scope (decl);
14631 initializer = cp_parser_initializer (parser,
14633 &is_non_constant_init);
14634 if (!member_p && processing_template_decl)
14635 finish_lambda_scope ();
14639 /* The old parser allows attributes to appear after a parenthesized
14640 initializer. Mark Mitchell proposed removing this functionality
14641 on the GCC mailing lists on 2002-08-13. This parser accepts the
14642 attributes -- but ignores them. */
14643 if (cp_parser_allow_gnu_extensions_p (parser)
14644 && initialization_kind == CPP_OPEN_PAREN)
14645 if (cp_parser_attributes_opt (parser))
14646 warning (OPT_Wattributes,
14647 "attributes after parenthesized initializer ignored");
14649 /* For an in-class declaration, use `grokfield' to create the
14655 pop_scope (pushed_scope);
14656 pushed_scope = NULL_TREE;
14658 decl = grokfield (declarator, decl_specifiers,
14659 initializer, !is_non_constant_init,
14660 /*asmspec=*/NULL_TREE,
14661 prefix_attributes);
14662 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14663 cp_parser_save_default_args (parser, decl);
14666 /* Finish processing the declaration. But, skip member
14668 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14670 cp_finish_decl (decl,
14671 initializer, !is_non_constant_init,
14673 /* If the initializer is in parentheses, then this is
14674 a direct-initialization, which means that an
14675 `explicit' constructor is OK. Otherwise, an
14676 `explicit' constructor cannot be used. */
14677 ((is_direct_init || !is_initialized)
14678 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14680 else if ((cxx_dialect != cxx98) && friend_p
14681 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14682 /* Core issue #226 (C++0x only): A default template-argument
14683 shall not be specified in a friend class template
14685 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14686 /*is_partial=*/0, /*is_friend_decl=*/1);
14688 if (!friend_p && pushed_scope)
14689 pop_scope (pushed_scope);
14694 /* Parse a declarator.
14698 ptr-operator declarator
14700 abstract-declarator:
14701 ptr-operator abstract-declarator [opt]
14702 direct-abstract-declarator
14707 attributes [opt] direct-declarator
14708 attributes [opt] ptr-operator declarator
14710 abstract-declarator:
14711 attributes [opt] ptr-operator abstract-declarator [opt]
14712 attributes [opt] direct-abstract-declarator
14714 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14715 detect constructor, destructor or conversion operators. It is set
14716 to -1 if the declarator is a name, and +1 if it is a
14717 function. Otherwise it is set to zero. Usually you just want to
14718 test for >0, but internally the negative value is used.
14720 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14721 a decl-specifier-seq unless it declares a constructor, destructor,
14722 or conversion. It might seem that we could check this condition in
14723 semantic analysis, rather than parsing, but that makes it difficult
14724 to handle something like `f()'. We want to notice that there are
14725 no decl-specifiers, and therefore realize that this is an
14726 expression, not a declaration.)
14728 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14729 the declarator is a direct-declarator of the form "(...)".
14731 MEMBER_P is true iff this declarator is a member-declarator. */
14733 static cp_declarator *
14734 cp_parser_declarator (cp_parser* parser,
14735 cp_parser_declarator_kind dcl_kind,
14736 int* ctor_dtor_or_conv_p,
14737 bool* parenthesized_p,
14740 cp_declarator *declarator;
14741 enum tree_code code;
14742 cp_cv_quals cv_quals;
14744 tree attributes = NULL_TREE;
14746 /* Assume this is not a constructor, destructor, or type-conversion
14748 if (ctor_dtor_or_conv_p)
14749 *ctor_dtor_or_conv_p = 0;
14751 if (cp_parser_allow_gnu_extensions_p (parser))
14752 attributes = cp_parser_attributes_opt (parser);
14754 /* Check for the ptr-operator production. */
14755 cp_parser_parse_tentatively (parser);
14756 /* Parse the ptr-operator. */
14757 code = cp_parser_ptr_operator (parser,
14760 /* If that worked, then we have a ptr-operator. */
14761 if (cp_parser_parse_definitely (parser))
14763 /* If a ptr-operator was found, then this declarator was not
14765 if (parenthesized_p)
14766 *parenthesized_p = true;
14767 /* The dependent declarator is optional if we are parsing an
14768 abstract-declarator. */
14769 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14770 cp_parser_parse_tentatively (parser);
14772 /* Parse the dependent declarator. */
14773 declarator = cp_parser_declarator (parser, dcl_kind,
14774 /*ctor_dtor_or_conv_p=*/NULL,
14775 /*parenthesized_p=*/NULL,
14776 /*member_p=*/false);
14778 /* If we are parsing an abstract-declarator, we must handle the
14779 case where the dependent declarator is absent. */
14780 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14781 && !cp_parser_parse_definitely (parser))
14784 declarator = cp_parser_make_indirect_declarator
14785 (code, class_type, cv_quals, declarator);
14787 /* Everything else is a direct-declarator. */
14790 if (parenthesized_p)
14791 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14793 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14794 ctor_dtor_or_conv_p,
14798 if (attributes && declarator && declarator != cp_error_declarator)
14799 declarator->attributes = attributes;
14804 /* Parse a direct-declarator or direct-abstract-declarator.
14808 direct-declarator ( parameter-declaration-clause )
14809 cv-qualifier-seq [opt]
14810 exception-specification [opt]
14811 direct-declarator [ constant-expression [opt] ]
14814 direct-abstract-declarator:
14815 direct-abstract-declarator [opt]
14816 ( parameter-declaration-clause )
14817 cv-qualifier-seq [opt]
14818 exception-specification [opt]
14819 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14820 ( abstract-declarator )
14822 Returns a representation of the declarator. DCL_KIND is
14823 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14824 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14825 we are parsing a direct-declarator. It is
14826 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14827 of ambiguity we prefer an abstract declarator, as per
14828 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14829 cp_parser_declarator. */
14831 static cp_declarator *
14832 cp_parser_direct_declarator (cp_parser* parser,
14833 cp_parser_declarator_kind dcl_kind,
14834 int* ctor_dtor_or_conv_p,
14838 cp_declarator *declarator = NULL;
14839 tree scope = NULL_TREE;
14840 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14841 bool saved_in_declarator_p = parser->in_declarator_p;
14843 tree pushed_scope = NULL_TREE;
14847 /* Peek at the next token. */
14848 token = cp_lexer_peek_token (parser->lexer);
14849 if (token->type == CPP_OPEN_PAREN)
14851 /* This is either a parameter-declaration-clause, or a
14852 parenthesized declarator. When we know we are parsing a
14853 named declarator, it must be a parenthesized declarator
14854 if FIRST is true. For instance, `(int)' is a
14855 parameter-declaration-clause, with an omitted
14856 direct-abstract-declarator. But `((*))', is a
14857 parenthesized abstract declarator. Finally, when T is a
14858 template parameter `(T)' is a
14859 parameter-declaration-clause, and not a parenthesized
14862 We first try and parse a parameter-declaration-clause,
14863 and then try a nested declarator (if FIRST is true).
14865 It is not an error for it not to be a
14866 parameter-declaration-clause, even when FIRST is
14872 The first is the declaration of a function while the
14873 second is the definition of a variable, including its
14876 Having seen only the parenthesis, we cannot know which of
14877 these two alternatives should be selected. Even more
14878 complex are examples like:
14883 The former is a function-declaration; the latter is a
14884 variable initialization.
14886 Thus again, we try a parameter-declaration-clause, and if
14887 that fails, we back out and return. */
14889 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14892 unsigned saved_num_template_parameter_lists;
14893 bool is_declarator = false;
14896 /* In a member-declarator, the only valid interpretation
14897 of a parenthesis is the start of a
14898 parameter-declaration-clause. (It is invalid to
14899 initialize a static data member with a parenthesized
14900 initializer; only the "=" form of initialization is
14903 cp_parser_parse_tentatively (parser);
14905 /* Consume the `('. */
14906 cp_lexer_consume_token (parser->lexer);
14909 /* If this is going to be an abstract declarator, we're
14910 in a declarator and we can't have default args. */
14911 parser->default_arg_ok_p = false;
14912 parser->in_declarator_p = true;
14915 /* Inside the function parameter list, surrounding
14916 template-parameter-lists do not apply. */
14917 saved_num_template_parameter_lists
14918 = parser->num_template_parameter_lists;
14919 parser->num_template_parameter_lists = 0;
14921 begin_scope (sk_function_parms, NULL_TREE);
14923 /* Parse the parameter-declaration-clause. */
14924 params = cp_parser_parameter_declaration_clause (parser);
14926 parser->num_template_parameter_lists
14927 = saved_num_template_parameter_lists;
14929 /* Consume the `)'. */
14930 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14932 /* If all went well, parse the cv-qualifier-seq and the
14933 exception-specification. */
14934 if (member_p || cp_parser_parse_definitely (parser))
14936 cp_cv_quals cv_quals;
14937 cp_virt_specifiers virt_specifiers;
14938 tree exception_specification;
14941 is_declarator = true;
14943 if (ctor_dtor_or_conv_p)
14944 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14947 /* Parse the cv-qualifier-seq. */
14948 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14949 /* And the exception-specification. */
14950 exception_specification
14951 = cp_parser_exception_specification_opt (parser);
14952 /* Parse the virt-specifier-seq. */
14953 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14956 = cp_parser_late_return_type_opt (parser);
14958 /* Create the function-declarator. */
14959 declarator = make_call_declarator (declarator,
14963 exception_specification,
14965 /* Any subsequent parameter lists are to do with
14966 return type, so are not those of the declared
14968 parser->default_arg_ok_p = false;
14971 /* Remove the function parms from scope. */
14972 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14973 pop_binding (DECL_NAME (t), t);
14977 /* Repeat the main loop. */
14981 /* If this is the first, we can try a parenthesized
14985 bool saved_in_type_id_in_expr_p;
14987 parser->default_arg_ok_p = saved_default_arg_ok_p;
14988 parser->in_declarator_p = saved_in_declarator_p;
14990 /* Consume the `('. */
14991 cp_lexer_consume_token (parser->lexer);
14992 /* Parse the nested declarator. */
14993 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14994 parser->in_type_id_in_expr_p = true;
14996 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14997 /*parenthesized_p=*/NULL,
14999 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15001 /* Expect a `)'. */
15002 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15003 declarator = cp_error_declarator;
15004 if (declarator == cp_error_declarator)
15007 goto handle_declarator;
15009 /* Otherwise, we must be done. */
15013 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15014 && token->type == CPP_OPEN_SQUARE)
15016 /* Parse an array-declarator. */
15019 if (ctor_dtor_or_conv_p)
15020 *ctor_dtor_or_conv_p = 0;
15023 parser->default_arg_ok_p = false;
15024 parser->in_declarator_p = true;
15025 /* Consume the `['. */
15026 cp_lexer_consume_token (parser->lexer);
15027 /* Peek at the next token. */
15028 token = cp_lexer_peek_token (parser->lexer);
15029 /* If the next token is `]', then there is no
15030 constant-expression. */
15031 if (token->type != CPP_CLOSE_SQUARE)
15033 bool non_constant_p;
15036 = cp_parser_constant_expression (parser,
15037 /*allow_non_constant=*/true,
15039 if (!non_constant_p)
15041 /* Normally, the array bound must be an integral constant
15042 expression. However, as an extension, we allow VLAs
15043 in function scopes as long as they aren't part of a
15044 parameter declaration. */
15045 else if (!parser->in_function_body
15046 || current_binding_level->kind == sk_function_parms)
15048 cp_parser_error (parser,
15049 "array bound is not an integer constant");
15050 bounds = error_mark_node;
15052 else if (processing_template_decl && !error_operand_p (bounds))
15054 /* Remember this wasn't a constant-expression. */
15055 bounds = build_nop (TREE_TYPE (bounds), bounds);
15056 TREE_SIDE_EFFECTS (bounds) = 1;
15060 bounds = NULL_TREE;
15061 /* Look for the closing `]'. */
15062 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15064 declarator = cp_error_declarator;
15068 declarator = make_array_declarator (declarator, bounds);
15070 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15073 tree qualifying_scope;
15074 tree unqualified_name;
15075 special_function_kind sfk;
15077 bool pack_expansion_p = false;
15078 cp_token *declarator_id_start_token;
15080 /* Parse a declarator-id */
15081 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15084 cp_parser_parse_tentatively (parser);
15086 /* If we see an ellipsis, we should be looking at a
15088 if (token->type == CPP_ELLIPSIS)
15090 /* Consume the `...' */
15091 cp_lexer_consume_token (parser->lexer);
15093 pack_expansion_p = true;
15097 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15099 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15100 qualifying_scope = parser->scope;
15105 if (!unqualified_name && pack_expansion_p)
15107 /* Check whether an error occurred. */
15108 okay = !cp_parser_error_occurred (parser);
15110 /* We already consumed the ellipsis to mark a
15111 parameter pack, but we have no way to report it,
15112 so abort the tentative parse. We will be exiting
15113 immediately anyway. */
15114 cp_parser_abort_tentative_parse (parser);
15117 okay = cp_parser_parse_definitely (parser);
15120 unqualified_name = error_mark_node;
15121 else if (unqualified_name
15122 && (qualifying_scope
15123 || (TREE_CODE (unqualified_name)
15124 != IDENTIFIER_NODE)))
15126 cp_parser_error (parser, "expected unqualified-id");
15127 unqualified_name = error_mark_node;
15131 if (!unqualified_name)
15133 if (unqualified_name == error_mark_node)
15135 declarator = cp_error_declarator;
15136 pack_expansion_p = false;
15137 declarator->parameter_pack_p = false;
15141 if (qualifying_scope && at_namespace_scope_p ()
15142 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15144 /* In the declaration of a member of a template class
15145 outside of the class itself, the SCOPE will sometimes
15146 be a TYPENAME_TYPE. For example, given:
15148 template <typename T>
15149 int S<T>::R::i = 3;
15151 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15152 this context, we must resolve S<T>::R to an ordinary
15153 type, rather than a typename type.
15155 The reason we normally avoid resolving TYPENAME_TYPEs
15156 is that a specialization of `S' might render
15157 `S<T>::R' not a type. However, if `S' is
15158 specialized, then this `i' will not be used, so there
15159 is no harm in resolving the types here. */
15162 /* Resolve the TYPENAME_TYPE. */
15163 type = resolve_typename_type (qualifying_scope,
15164 /*only_current_p=*/false);
15165 /* If that failed, the declarator is invalid. */
15166 if (TREE_CODE (type) == TYPENAME_TYPE)
15168 if (typedef_variant_p (type))
15169 error_at (declarator_id_start_token->location,
15170 "cannot define member of dependent typedef "
15173 error_at (declarator_id_start_token->location,
15174 "%<%T::%E%> is not a type",
15175 TYPE_CONTEXT (qualifying_scope),
15176 TYPE_IDENTIFIER (qualifying_scope));
15178 qualifying_scope = type;
15183 if (unqualified_name)
15187 if (qualifying_scope
15188 && CLASS_TYPE_P (qualifying_scope))
15189 class_type = qualifying_scope;
15191 class_type = current_class_type;
15193 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15195 tree name_type = TREE_TYPE (unqualified_name);
15196 if (class_type && same_type_p (name_type, class_type))
15198 if (qualifying_scope
15199 && CLASSTYPE_USE_TEMPLATE (name_type))
15201 error_at (declarator_id_start_token->location,
15202 "invalid use of constructor as a template");
15203 inform (declarator_id_start_token->location,
15204 "use %<%T::%D%> instead of %<%T::%D%> to "
15205 "name the constructor in a qualified name",
15207 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15208 class_type, name_type);
15209 declarator = cp_error_declarator;
15213 unqualified_name = constructor_name (class_type);
15217 /* We do not attempt to print the declarator
15218 here because we do not have enough
15219 information about its original syntactic
15221 cp_parser_error (parser, "invalid declarator");
15222 declarator = cp_error_declarator;
15229 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15230 sfk = sfk_destructor;
15231 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15232 sfk = sfk_conversion;
15233 else if (/* There's no way to declare a constructor
15234 for an anonymous type, even if the type
15235 got a name for linkage purposes. */
15236 !TYPE_WAS_ANONYMOUS (class_type)
15237 && constructor_name_p (unqualified_name,
15240 unqualified_name = constructor_name (class_type);
15241 sfk = sfk_constructor;
15243 else if (is_overloaded_fn (unqualified_name)
15244 && DECL_CONSTRUCTOR_P (get_first_fn
15245 (unqualified_name)))
15246 sfk = sfk_constructor;
15248 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15249 *ctor_dtor_or_conv_p = -1;
15252 declarator = make_id_declarator (qualifying_scope,
15255 declarator->id_loc = token->location;
15256 declarator->parameter_pack_p = pack_expansion_p;
15258 if (pack_expansion_p)
15259 maybe_warn_variadic_templates ();
15262 handle_declarator:;
15263 scope = get_scope_of_declarator (declarator);
15265 /* Any names that appear after the declarator-id for a
15266 member are looked up in the containing scope. */
15267 pushed_scope = push_scope (scope);
15268 parser->in_declarator_p = true;
15269 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15270 || (declarator && declarator->kind == cdk_id))
15271 /* Default args are only allowed on function
15273 parser->default_arg_ok_p = saved_default_arg_ok_p;
15275 parser->default_arg_ok_p = false;
15284 /* For an abstract declarator, we might wind up with nothing at this
15285 point. That's an error; the declarator is not optional. */
15287 cp_parser_error (parser, "expected declarator");
15289 /* If we entered a scope, we must exit it now. */
15291 pop_scope (pushed_scope);
15293 parser->default_arg_ok_p = saved_default_arg_ok_p;
15294 parser->in_declarator_p = saved_in_declarator_p;
15299 /* Parse a ptr-operator.
15302 * cv-qualifier-seq [opt]
15304 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15309 & cv-qualifier-seq [opt]
15311 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15312 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15313 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15314 filled in with the TYPE containing the member. *CV_QUALS is
15315 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15316 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15317 Note that the tree codes returned by this function have nothing
15318 to do with the types of trees that will be eventually be created
15319 to represent the pointer or reference type being parsed. They are
15320 just constants with suggestive names. */
15321 static enum tree_code
15322 cp_parser_ptr_operator (cp_parser* parser,
15324 cp_cv_quals *cv_quals)
15326 enum tree_code code = ERROR_MARK;
15329 /* Assume that it's not a pointer-to-member. */
15331 /* And that there are no cv-qualifiers. */
15332 *cv_quals = TYPE_UNQUALIFIED;
15334 /* Peek at the next token. */
15335 token = cp_lexer_peek_token (parser->lexer);
15337 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15338 if (token->type == CPP_MULT)
15339 code = INDIRECT_REF;
15340 else if (token->type == CPP_AND)
15342 else if ((cxx_dialect != cxx98) &&
15343 token->type == CPP_AND_AND) /* C++0x only */
15344 code = NON_LVALUE_EXPR;
15346 if (code != ERROR_MARK)
15348 /* Consume the `*', `&' or `&&'. */
15349 cp_lexer_consume_token (parser->lexer);
15351 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15352 `&', if we are allowing GNU extensions. (The only qualifier
15353 that can legally appear after `&' is `restrict', but that is
15354 enforced during semantic analysis. */
15355 if (code == INDIRECT_REF
15356 || cp_parser_allow_gnu_extensions_p (parser))
15357 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15361 /* Try the pointer-to-member case. */
15362 cp_parser_parse_tentatively (parser);
15363 /* Look for the optional `::' operator. */
15364 cp_parser_global_scope_opt (parser,
15365 /*current_scope_valid_p=*/false);
15366 /* Look for the nested-name specifier. */
15367 token = cp_lexer_peek_token (parser->lexer);
15368 cp_parser_nested_name_specifier (parser,
15369 /*typename_keyword_p=*/false,
15370 /*check_dependency_p=*/true,
15372 /*is_declaration=*/false);
15373 /* If we found it, and the next token is a `*', then we are
15374 indeed looking at a pointer-to-member operator. */
15375 if (!cp_parser_error_occurred (parser)
15376 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15378 /* Indicate that the `*' operator was used. */
15379 code = INDIRECT_REF;
15381 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15382 error_at (token->location, "%qD is a namespace", parser->scope);
15385 /* The type of which the member is a member is given by the
15387 *type = parser->scope;
15388 /* The next name will not be qualified. */
15389 parser->scope = NULL_TREE;
15390 parser->qualifying_scope = NULL_TREE;
15391 parser->object_scope = NULL_TREE;
15392 /* Look for the optional cv-qualifier-seq. */
15393 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15396 /* If that didn't work we don't have a ptr-operator. */
15397 if (!cp_parser_parse_definitely (parser))
15398 cp_parser_error (parser, "expected ptr-operator");
15404 /* Parse an (optional) cv-qualifier-seq.
15407 cv-qualifier cv-qualifier-seq [opt]
15418 Returns a bitmask representing the cv-qualifiers. */
15421 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15423 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15428 cp_cv_quals cv_qualifier;
15430 /* Peek at the next token. */
15431 token = cp_lexer_peek_token (parser->lexer);
15432 /* See if it's a cv-qualifier. */
15433 switch (token->keyword)
15436 cv_qualifier = TYPE_QUAL_CONST;
15440 cv_qualifier = TYPE_QUAL_VOLATILE;
15444 cv_qualifier = TYPE_QUAL_RESTRICT;
15448 cv_qualifier = TYPE_UNQUALIFIED;
15455 if (cv_quals & cv_qualifier)
15457 error_at (token->location, "duplicate cv-qualifier");
15458 cp_lexer_purge_token (parser->lexer);
15462 cp_lexer_consume_token (parser->lexer);
15463 cv_quals |= cv_qualifier;
15470 /* Parse an (optional) virt-specifier-seq.
15472 virt-specifier-seq:
15473 virt-specifier virt-specifier-seq [opt]
15479 Returns a bitmask representing the virt-specifiers. */
15481 static cp_virt_specifiers
15482 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15484 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15489 cp_virt_specifiers virt_specifier;
15491 /* Peek at the next token. */
15492 token = cp_lexer_peek_token (parser->lexer);
15493 /* See if it's a virt-specifier-qualifier. */
15494 if (token->type != CPP_NAME)
15496 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15497 virt_specifier = VIRT_SPEC_OVERRIDE;
15498 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15499 virt_specifier = VIRT_SPEC_FINAL;
15503 if (virt_specifiers & virt_specifier)
15505 error_at (token->location, "duplicate virt-specifier");
15506 cp_lexer_purge_token (parser->lexer);
15510 cp_lexer_consume_token (parser->lexer);
15511 virt_specifiers |= virt_specifier;
15514 return virt_specifiers;
15517 /* Parse a late-specified return type, if any. This is not a separate
15518 non-terminal, but part of a function declarator, which looks like
15520 -> trailing-type-specifier-seq abstract-declarator(opt)
15522 Returns the type indicated by the type-id. */
15525 cp_parser_late_return_type_opt (cp_parser* parser)
15529 /* Peek at the next token. */
15530 token = cp_lexer_peek_token (parser->lexer);
15531 /* A late-specified return type is indicated by an initial '->'. */
15532 if (token->type != CPP_DEREF)
15535 /* Consume the ->. */
15536 cp_lexer_consume_token (parser->lexer);
15538 return cp_parser_trailing_type_id (parser);
15541 /* Parse a declarator-id.
15545 :: [opt] nested-name-specifier [opt] type-name
15547 In the `id-expression' case, the value returned is as for
15548 cp_parser_id_expression if the id-expression was an unqualified-id.
15549 If the id-expression was a qualified-id, then a SCOPE_REF is
15550 returned. The first operand is the scope (either a NAMESPACE_DECL
15551 or TREE_TYPE), but the second is still just a representation of an
15555 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15558 /* The expression must be an id-expression. Assume that qualified
15559 names are the names of types so that:
15562 int S<T>::R::i = 3;
15564 will work; we must treat `S<T>::R' as the name of a type.
15565 Similarly, assume that qualified names are templates, where
15569 int S<T>::R<T>::i = 3;
15572 id = cp_parser_id_expression (parser,
15573 /*template_keyword_p=*/false,
15574 /*check_dependency_p=*/false,
15575 /*template_p=*/NULL,
15576 /*declarator_p=*/true,
15578 if (id && BASELINK_P (id))
15579 id = BASELINK_FUNCTIONS (id);
15583 /* Parse a type-id.
15586 type-specifier-seq abstract-declarator [opt]
15588 Returns the TYPE specified. */
15591 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15592 bool is_trailing_return)
15594 cp_decl_specifier_seq type_specifier_seq;
15595 cp_declarator *abstract_declarator;
15597 /* Parse the type-specifier-seq. */
15598 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15599 is_trailing_return,
15600 &type_specifier_seq);
15601 if (type_specifier_seq.type == error_mark_node)
15602 return error_mark_node;
15604 /* There might or might not be an abstract declarator. */
15605 cp_parser_parse_tentatively (parser);
15606 /* Look for the declarator. */
15607 abstract_declarator
15608 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15609 /*parenthesized_p=*/NULL,
15610 /*member_p=*/false);
15611 /* Check to see if there really was a declarator. */
15612 if (!cp_parser_parse_definitely (parser))
15613 abstract_declarator = NULL;
15615 if (type_specifier_seq.type
15616 && type_uses_auto (type_specifier_seq.type))
15618 /* A type-id with type 'auto' is only ok if the abstract declarator
15619 is a function declarator with a late-specified return type. */
15620 if (abstract_declarator
15621 && abstract_declarator->kind == cdk_function
15622 && abstract_declarator->u.function.late_return_type)
15626 error ("invalid use of %<auto%>");
15627 return error_mark_node;
15631 return groktypename (&type_specifier_seq, abstract_declarator,
15635 static tree cp_parser_type_id (cp_parser *parser)
15637 return cp_parser_type_id_1 (parser, false, false);
15640 static tree cp_parser_template_type_arg (cp_parser *parser)
15643 const char *saved_message = parser->type_definition_forbidden_message;
15644 parser->type_definition_forbidden_message
15645 = G_("types may not be defined in template arguments");
15646 r = cp_parser_type_id_1 (parser, true, false);
15647 parser->type_definition_forbidden_message = saved_message;
15651 static tree cp_parser_trailing_type_id (cp_parser *parser)
15653 return cp_parser_type_id_1 (parser, false, true);
15656 /* Parse a type-specifier-seq.
15658 type-specifier-seq:
15659 type-specifier type-specifier-seq [opt]
15663 type-specifier-seq:
15664 attributes type-specifier-seq [opt]
15666 If IS_DECLARATION is true, we are at the start of a "condition" or
15667 exception-declaration, so we might be followed by a declarator-id.
15669 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15670 i.e. we've just seen "->".
15672 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15675 cp_parser_type_specifier_seq (cp_parser* parser,
15676 bool is_declaration,
15677 bool is_trailing_return,
15678 cp_decl_specifier_seq *type_specifier_seq)
15680 bool seen_type_specifier = false;
15681 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15682 cp_token *start_token = NULL;
15684 /* Clear the TYPE_SPECIFIER_SEQ. */
15685 clear_decl_specs (type_specifier_seq);
15687 /* In the context of a trailing return type, enum E { } is an
15688 elaborated-type-specifier followed by a function-body, not an
15690 if (is_trailing_return)
15691 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15693 /* Parse the type-specifiers and attributes. */
15696 tree type_specifier;
15697 bool is_cv_qualifier;
15699 /* Check for attributes first. */
15700 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15702 type_specifier_seq->attributes =
15703 chainon (type_specifier_seq->attributes,
15704 cp_parser_attributes_opt (parser));
15708 /* record the token of the beginning of the type specifier seq,
15709 for error reporting purposes*/
15711 start_token = cp_lexer_peek_token (parser->lexer);
15713 /* Look for the type-specifier. */
15714 type_specifier = cp_parser_type_specifier (parser,
15716 type_specifier_seq,
15717 /*is_declaration=*/false,
15720 if (!type_specifier)
15722 /* If the first type-specifier could not be found, this is not a
15723 type-specifier-seq at all. */
15724 if (!seen_type_specifier)
15726 cp_parser_error (parser, "expected type-specifier");
15727 type_specifier_seq->type = error_mark_node;
15730 /* If subsequent type-specifiers could not be found, the
15731 type-specifier-seq is complete. */
15735 seen_type_specifier = true;
15736 /* The standard says that a condition can be:
15738 type-specifier-seq declarator = assignment-expression
15745 we should treat the "S" as a declarator, not as a
15746 type-specifier. The standard doesn't say that explicitly for
15747 type-specifier-seq, but it does say that for
15748 decl-specifier-seq in an ordinary declaration. Perhaps it
15749 would be clearer just to allow a decl-specifier-seq here, and
15750 then add a semantic restriction that if any decl-specifiers
15751 that are not type-specifiers appear, the program is invalid. */
15752 if (is_declaration && !is_cv_qualifier)
15753 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15756 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15759 /* Parse a parameter-declaration-clause.
15761 parameter-declaration-clause:
15762 parameter-declaration-list [opt] ... [opt]
15763 parameter-declaration-list , ...
15765 Returns a representation for the parameter declarations. A return
15766 value of NULL indicates a parameter-declaration-clause consisting
15767 only of an ellipsis. */
15770 cp_parser_parameter_declaration_clause (cp_parser* parser)
15777 /* Peek at the next token. */
15778 token = cp_lexer_peek_token (parser->lexer);
15779 /* Check for trivial parameter-declaration-clauses. */
15780 if (token->type == CPP_ELLIPSIS)
15782 /* Consume the `...' token. */
15783 cp_lexer_consume_token (parser->lexer);
15786 else if (token->type == CPP_CLOSE_PAREN)
15787 /* There are no parameters. */
15789 #ifndef NO_IMPLICIT_EXTERN_C
15790 if (in_system_header && current_class_type == NULL
15791 && current_lang_name == lang_name_c)
15795 return void_list_node;
15797 /* Check for `(void)', too, which is a special case. */
15798 else if (token->keyword == RID_VOID
15799 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15800 == CPP_CLOSE_PAREN))
15802 /* Consume the `void' token. */
15803 cp_lexer_consume_token (parser->lexer);
15804 /* There are no parameters. */
15805 return void_list_node;
15808 /* Parse the parameter-declaration-list. */
15809 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15810 /* If a parse error occurred while parsing the
15811 parameter-declaration-list, then the entire
15812 parameter-declaration-clause is erroneous. */
15816 /* Peek at the next token. */
15817 token = cp_lexer_peek_token (parser->lexer);
15818 /* If it's a `,', the clause should terminate with an ellipsis. */
15819 if (token->type == CPP_COMMA)
15821 /* Consume the `,'. */
15822 cp_lexer_consume_token (parser->lexer);
15823 /* Expect an ellipsis. */
15825 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15827 /* It might also be `...' if the optional trailing `,' was
15829 else if (token->type == CPP_ELLIPSIS)
15831 /* Consume the `...' token. */
15832 cp_lexer_consume_token (parser->lexer);
15833 /* And remember that we saw it. */
15837 ellipsis_p = false;
15839 /* Finish the parameter list. */
15841 parameters = chainon (parameters, void_list_node);
15846 /* Parse a parameter-declaration-list.
15848 parameter-declaration-list:
15849 parameter-declaration
15850 parameter-declaration-list , parameter-declaration
15852 Returns a representation of the parameter-declaration-list, as for
15853 cp_parser_parameter_declaration_clause. However, the
15854 `void_list_node' is never appended to the list. Upon return,
15855 *IS_ERROR will be true iff an error occurred. */
15858 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15860 tree parameters = NULL_TREE;
15861 tree *tail = ¶meters;
15862 bool saved_in_unbraced_linkage_specification_p;
15865 /* Assume all will go well. */
15867 /* The special considerations that apply to a function within an
15868 unbraced linkage specifications do not apply to the parameters
15869 to the function. */
15870 saved_in_unbraced_linkage_specification_p
15871 = parser->in_unbraced_linkage_specification_p;
15872 parser->in_unbraced_linkage_specification_p = false;
15874 /* Look for more parameters. */
15877 cp_parameter_declarator *parameter;
15878 tree decl = error_mark_node;
15879 bool parenthesized_p;
15880 /* Parse the parameter. */
15882 = cp_parser_parameter_declaration (parser,
15883 /*template_parm_p=*/false,
15886 /* We don't know yet if the enclosing context is deprecated, so wait
15887 and warn in grokparms if appropriate. */
15888 deprecated_state = DEPRECATED_SUPPRESS;
15891 decl = grokdeclarator (parameter->declarator,
15892 ¶meter->decl_specifiers,
15894 parameter->default_argument != NULL_TREE,
15895 ¶meter->decl_specifiers.attributes);
15897 deprecated_state = DEPRECATED_NORMAL;
15899 /* If a parse error occurred parsing the parameter declaration,
15900 then the entire parameter-declaration-list is erroneous. */
15901 if (decl == error_mark_node)
15904 parameters = error_mark_node;
15908 if (parameter->decl_specifiers.attributes)
15909 cplus_decl_attributes (&decl,
15910 parameter->decl_specifiers.attributes,
15912 if (DECL_NAME (decl))
15913 decl = pushdecl (decl);
15915 if (decl != error_mark_node)
15917 retrofit_lang_decl (decl);
15918 DECL_PARM_INDEX (decl) = ++index;
15919 DECL_PARM_LEVEL (decl) = function_parm_depth ();
15922 /* Add the new parameter to the list. */
15923 *tail = build_tree_list (parameter->default_argument, decl);
15924 tail = &TREE_CHAIN (*tail);
15926 /* Peek at the next token. */
15927 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15928 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15929 /* These are for Objective-C++ */
15930 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15931 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15932 /* The parameter-declaration-list is complete. */
15934 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15938 /* Peek at the next token. */
15939 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15940 /* If it's an ellipsis, then the list is complete. */
15941 if (token->type == CPP_ELLIPSIS)
15943 /* Otherwise, there must be more parameters. Consume the
15945 cp_lexer_consume_token (parser->lexer);
15946 /* When parsing something like:
15948 int i(float f, double d)
15950 we can tell after seeing the declaration for "f" that we
15951 are not looking at an initialization of a variable "i",
15952 but rather at the declaration of a function "i".
15954 Due to the fact that the parsing of template arguments
15955 (as specified to a template-id) requires backtracking we
15956 cannot use this technique when inside a template argument
15958 if (!parser->in_template_argument_list_p
15959 && !parser->in_type_id_in_expr_p
15960 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15961 /* However, a parameter-declaration of the form
15962 "foat(f)" (which is a valid declaration of a
15963 parameter "f") can also be interpreted as an
15964 expression (the conversion of "f" to "float"). */
15965 && !parenthesized_p)
15966 cp_parser_commit_to_tentative_parse (parser);
15970 cp_parser_error (parser, "expected %<,%> or %<...%>");
15971 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15972 cp_parser_skip_to_closing_parenthesis (parser,
15973 /*recovering=*/true,
15974 /*or_comma=*/false,
15975 /*consume_paren=*/false);
15980 parser->in_unbraced_linkage_specification_p
15981 = saved_in_unbraced_linkage_specification_p;
15986 /* Parse a parameter declaration.
15988 parameter-declaration:
15989 decl-specifier-seq ... [opt] declarator
15990 decl-specifier-seq declarator = assignment-expression
15991 decl-specifier-seq ... [opt] abstract-declarator [opt]
15992 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15994 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15995 declares a template parameter. (In that case, a non-nested `>'
15996 token encountered during the parsing of the assignment-expression
15997 is not interpreted as a greater-than operator.)
15999 Returns a representation of the parameter, or NULL if an error
16000 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16001 true iff the declarator is of the form "(p)". */
16003 static cp_parameter_declarator *
16004 cp_parser_parameter_declaration (cp_parser *parser,
16005 bool template_parm_p,
16006 bool *parenthesized_p)
16008 int declares_class_or_enum;
16009 cp_decl_specifier_seq decl_specifiers;
16010 cp_declarator *declarator;
16011 tree default_argument;
16012 cp_token *token = NULL, *declarator_token_start = NULL;
16013 const char *saved_message;
16015 /* In a template parameter, `>' is not an operator.
16019 When parsing a default template-argument for a non-type
16020 template-parameter, the first non-nested `>' is taken as the end
16021 of the template parameter-list rather than a greater-than
16024 /* Type definitions may not appear in parameter types. */
16025 saved_message = parser->type_definition_forbidden_message;
16026 parser->type_definition_forbidden_message
16027 = G_("types may not be defined in parameter types");
16029 /* Parse the declaration-specifiers. */
16030 cp_parser_decl_specifier_seq (parser,
16031 CP_PARSER_FLAGS_NONE,
16033 &declares_class_or_enum);
16035 /* Complain about missing 'typename' or other invalid type names. */
16036 if (!decl_specifiers.any_type_specifiers_p)
16037 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16039 /* If an error occurred, there's no reason to attempt to parse the
16040 rest of the declaration. */
16041 if (cp_parser_error_occurred (parser))
16043 parser->type_definition_forbidden_message = saved_message;
16047 /* Peek at the next token. */
16048 token = cp_lexer_peek_token (parser->lexer);
16050 /* If the next token is a `)', `,', `=', `>', or `...', then there
16051 is no declarator. However, when variadic templates are enabled,
16052 there may be a declarator following `...'. */
16053 if (token->type == CPP_CLOSE_PAREN
16054 || token->type == CPP_COMMA
16055 || token->type == CPP_EQ
16056 || token->type == CPP_GREATER)
16059 if (parenthesized_p)
16060 *parenthesized_p = false;
16062 /* Otherwise, there should be a declarator. */
16065 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16066 parser->default_arg_ok_p = false;
16068 /* After seeing a decl-specifier-seq, if the next token is not a
16069 "(", there is no possibility that the code is a valid
16070 expression. Therefore, if parsing tentatively, we commit at
16072 if (!parser->in_template_argument_list_p
16073 /* In an expression context, having seen:
16077 we cannot be sure whether we are looking at a
16078 function-type (taking a "char" as a parameter) or a cast
16079 of some object of type "char" to "int". */
16080 && !parser->in_type_id_in_expr_p
16081 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16082 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16083 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16084 cp_parser_commit_to_tentative_parse (parser);
16085 /* Parse the declarator. */
16086 declarator_token_start = token;
16087 declarator = cp_parser_declarator (parser,
16088 CP_PARSER_DECLARATOR_EITHER,
16089 /*ctor_dtor_or_conv_p=*/NULL,
16091 /*member_p=*/false);
16092 parser->default_arg_ok_p = saved_default_arg_ok_p;
16093 /* After the declarator, allow more attributes. */
16094 decl_specifiers.attributes
16095 = chainon (decl_specifiers.attributes,
16096 cp_parser_attributes_opt (parser));
16099 /* If the next token is an ellipsis, and we have not seen a
16100 declarator name, and the type of the declarator contains parameter
16101 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16102 a parameter pack expansion expression. Otherwise, leave the
16103 ellipsis for a C-style variadic function. */
16104 token = cp_lexer_peek_token (parser->lexer);
16105 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16107 tree type = decl_specifiers.type;
16109 if (type && DECL_P (type))
16110 type = TREE_TYPE (type);
16113 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16114 && declarator_can_be_parameter_pack (declarator)
16115 && (!declarator || !declarator->parameter_pack_p)
16116 && uses_parameter_packs (type))
16118 /* Consume the `...'. */
16119 cp_lexer_consume_token (parser->lexer);
16120 maybe_warn_variadic_templates ();
16122 /* Build a pack expansion type */
16124 declarator->parameter_pack_p = true;
16126 decl_specifiers.type = make_pack_expansion (type);
16130 /* The restriction on defining new types applies only to the type
16131 of the parameter, not to the default argument. */
16132 parser->type_definition_forbidden_message = saved_message;
16134 /* If the next token is `=', then process a default argument. */
16135 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16137 /* Consume the `='. */
16138 cp_lexer_consume_token (parser->lexer);
16140 /* If we are defining a class, then the tokens that make up the
16141 default argument must be saved and processed later. */
16142 if (!template_parm_p && at_class_scope_p ()
16143 && TYPE_BEING_DEFINED (current_class_type)
16144 && !LAMBDA_TYPE_P (current_class_type))
16146 unsigned depth = 0;
16147 int maybe_template_id = 0;
16148 cp_token *first_token;
16151 /* Add tokens until we have processed the entire default
16152 argument. We add the range [first_token, token). */
16153 first_token = cp_lexer_peek_token (parser->lexer);
16158 /* Peek at the next token. */
16159 token = cp_lexer_peek_token (parser->lexer);
16160 /* What we do depends on what token we have. */
16161 switch (token->type)
16163 /* In valid code, a default argument must be
16164 immediately followed by a `,' `)', or `...'. */
16166 if (depth == 0 && maybe_template_id)
16168 /* If we've seen a '<', we might be in a
16169 template-argument-list. Until Core issue 325 is
16170 resolved, we don't know how this situation ought
16171 to be handled, so try to DTRT. We check whether
16172 what comes after the comma is a valid parameter
16173 declaration list. If it is, then the comma ends
16174 the default argument; otherwise the default
16175 argument continues. */
16176 bool error = false;
16179 /* Set ITALP so cp_parser_parameter_declaration_list
16180 doesn't decide to commit to this parse. */
16181 bool saved_italp = parser->in_template_argument_list_p;
16182 parser->in_template_argument_list_p = true;
16184 cp_parser_parse_tentatively (parser);
16185 cp_lexer_consume_token (parser->lexer);
16186 begin_scope (sk_function_parms, NULL_TREE);
16187 cp_parser_parameter_declaration_list (parser, &error);
16188 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16189 pop_binding (DECL_NAME (t), t);
16191 if (!cp_parser_error_occurred (parser) && !error)
16193 cp_parser_abort_tentative_parse (parser);
16195 parser->in_template_argument_list_p = saved_italp;
16198 case CPP_CLOSE_PAREN:
16200 /* If we run into a non-nested `;', `}', or `]',
16201 then the code is invalid -- but the default
16202 argument is certainly over. */
16203 case CPP_SEMICOLON:
16204 case CPP_CLOSE_BRACE:
16205 case CPP_CLOSE_SQUARE:
16208 /* Update DEPTH, if necessary. */
16209 else if (token->type == CPP_CLOSE_PAREN
16210 || token->type == CPP_CLOSE_BRACE
16211 || token->type == CPP_CLOSE_SQUARE)
16215 case CPP_OPEN_PAREN:
16216 case CPP_OPEN_SQUARE:
16217 case CPP_OPEN_BRACE:
16223 /* This might be the comparison operator, or it might
16224 start a template argument list. */
16225 ++maybe_template_id;
16229 if (cxx_dialect == cxx98)
16231 /* Fall through for C++0x, which treats the `>>'
16232 operator like two `>' tokens in certain
16238 /* This might be an operator, or it might close a
16239 template argument list. But if a previous '<'
16240 started a template argument list, this will have
16241 closed it, so we can't be in one anymore. */
16242 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16243 if (maybe_template_id < 0)
16244 maybe_template_id = 0;
16248 /* If we run out of tokens, issue an error message. */
16250 case CPP_PRAGMA_EOL:
16251 error_at (token->location, "file ends in default argument");
16257 /* In these cases, we should look for template-ids.
16258 For example, if the default argument is
16259 `X<int, double>()', we need to do name lookup to
16260 figure out whether or not `X' is a template; if
16261 so, the `,' does not end the default argument.
16263 That is not yet done. */
16270 /* If we've reached the end, stop. */
16274 /* Add the token to the token block. */
16275 token = cp_lexer_consume_token (parser->lexer);
16278 /* Create a DEFAULT_ARG to represent the unparsed default
16280 default_argument = make_node (DEFAULT_ARG);
16281 DEFARG_TOKENS (default_argument)
16282 = cp_token_cache_new (first_token, token);
16283 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16285 /* Outside of a class definition, we can just parse the
16286 assignment-expression. */
16289 token = cp_lexer_peek_token (parser->lexer);
16291 = cp_parser_default_argument (parser, template_parm_p);
16294 if (!parser->default_arg_ok_p)
16296 if (flag_permissive)
16297 warning (0, "deprecated use of default argument for parameter of non-function");
16300 error_at (token->location,
16301 "default arguments are only "
16302 "permitted for function parameters");
16303 default_argument = NULL_TREE;
16306 else if ((declarator && declarator->parameter_pack_p)
16307 || (decl_specifiers.type
16308 && PACK_EXPANSION_P (decl_specifiers.type)))
16310 /* Find the name of the parameter pack. */
16311 cp_declarator *id_declarator = declarator;
16312 while (id_declarator && id_declarator->kind != cdk_id)
16313 id_declarator = id_declarator->declarator;
16315 if (id_declarator && id_declarator->kind == cdk_id)
16316 error_at (declarator_token_start->location,
16318 ? "template parameter pack %qD"
16319 " cannot have a default argument"
16320 : "parameter pack %qD cannot have a default argument",
16321 id_declarator->u.id.unqualified_name);
16323 error_at (declarator_token_start->location,
16325 ? "template parameter pack cannot have a default argument"
16326 : "parameter pack cannot have a default argument");
16328 default_argument = NULL_TREE;
16332 default_argument = NULL_TREE;
16334 return make_parameter_declarator (&decl_specifiers,
16339 /* Parse a default argument and return it.
16341 TEMPLATE_PARM_P is true if this is a default argument for a
16342 non-type template parameter. */
16344 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16346 tree default_argument = NULL_TREE;
16347 bool saved_greater_than_is_operator_p;
16348 bool saved_local_variables_forbidden_p;
16350 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16352 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16353 parser->greater_than_is_operator_p = !template_parm_p;
16354 /* Local variable names (and the `this' keyword) may not
16355 appear in a default argument. */
16356 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16357 parser->local_variables_forbidden_p = true;
16358 /* Parse the assignment-expression. */
16359 if (template_parm_p)
16360 push_deferring_access_checks (dk_no_deferred);
16362 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16363 if (template_parm_p)
16364 pop_deferring_access_checks ();
16365 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16366 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16368 return default_argument;
16371 /* Parse a function-body.
16374 compound_statement */
16377 cp_parser_function_body (cp_parser *parser)
16379 cp_parser_compound_statement (parser, NULL, false, true);
16382 /* Parse a ctor-initializer-opt followed by a function-body. Return
16383 true if a ctor-initializer was present. */
16386 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16389 bool ctor_initializer_p;
16390 const bool check_body_p =
16391 DECL_CONSTRUCTOR_P (current_function_decl)
16392 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16395 /* Begin the function body. */
16396 body = begin_function_body ();
16397 /* Parse the optional ctor-initializer. */
16398 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16400 /* If we're parsing a constexpr constructor definition, we need
16401 to check that the constructor body is indeed empty. However,
16402 before we get to cp_parser_function_body lot of junk has been
16403 generated, so we can't just check that we have an empty block.
16404 Rather we take a snapshot of the outermost block, and check whether
16405 cp_parser_function_body changed its state. */
16409 if (TREE_CODE (list) == BIND_EXPR)
16410 list = BIND_EXPR_BODY (list);
16411 if (TREE_CODE (list) == STATEMENT_LIST
16412 && STATEMENT_LIST_TAIL (list) != NULL)
16413 last = STATEMENT_LIST_TAIL (list)->stmt;
16415 /* Parse the function-body. */
16416 cp_parser_function_body (parser);
16418 check_constexpr_ctor_body (last, list);
16419 /* Finish the function body. */
16420 finish_function_body (body);
16422 return ctor_initializer_p;
16425 /* Parse an initializer.
16428 = initializer-clause
16429 ( expression-list )
16431 Returns an expression representing the initializer. If no
16432 initializer is present, NULL_TREE is returned.
16434 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16435 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16436 set to TRUE if there is no initializer present. If there is an
16437 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16438 is set to true; otherwise it is set to false. */
16441 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16442 bool* non_constant_p)
16447 /* Peek at the next token. */
16448 token = cp_lexer_peek_token (parser->lexer);
16450 /* Let our caller know whether or not this initializer was
16452 *is_direct_init = (token->type != CPP_EQ);
16453 /* Assume that the initializer is constant. */
16454 *non_constant_p = false;
16456 if (token->type == CPP_EQ)
16458 /* Consume the `='. */
16459 cp_lexer_consume_token (parser->lexer);
16460 /* Parse the initializer-clause. */
16461 init = cp_parser_initializer_clause (parser, non_constant_p);
16463 else if (token->type == CPP_OPEN_PAREN)
16466 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16468 /*allow_expansion_p=*/true,
16471 return error_mark_node;
16472 init = build_tree_list_vec (vec);
16473 release_tree_vector (vec);
16475 else if (token->type == CPP_OPEN_BRACE)
16477 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16478 init = cp_parser_braced_list (parser, non_constant_p);
16479 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16483 /* Anything else is an error. */
16484 cp_parser_error (parser, "expected initializer");
16485 init = error_mark_node;
16491 /* Parse an initializer-clause.
16493 initializer-clause:
16494 assignment-expression
16497 Returns an expression representing the initializer.
16499 If the `assignment-expression' production is used the value
16500 returned is simply a representation for the expression.
16502 Otherwise, calls cp_parser_braced_list. */
16505 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16509 /* Assume the expression is constant. */
16510 *non_constant_p = false;
16512 /* If it is not a `{', then we are looking at an
16513 assignment-expression. */
16514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16517 = cp_parser_constant_expression (parser,
16518 /*allow_non_constant_p=*/true,
16520 if (!*non_constant_p)
16522 /* We only want to fold if this is really a constant
16523 expression. FIXME Actually, we don't want to fold here, but in
16525 tree folded = fold_non_dependent_expr (initializer);
16526 folded = maybe_constant_value (folded);
16527 if (TREE_CONSTANT (folded))
16528 initializer = folded;
16532 initializer = cp_parser_braced_list (parser, non_constant_p);
16534 return initializer;
16537 /* Parse a brace-enclosed initializer list.
16540 { initializer-list , [opt] }
16543 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16544 the elements of the initializer-list (or NULL, if the last
16545 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16546 NULL_TREE. There is no way to detect whether or not the optional
16547 trailing `,' was provided. NON_CONSTANT_P is as for
16548 cp_parser_initializer. */
16551 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16555 /* Consume the `{' token. */
16556 cp_lexer_consume_token (parser->lexer);
16557 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16558 initializer = make_node (CONSTRUCTOR);
16559 /* If it's not a `}', then there is a non-trivial initializer. */
16560 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16562 /* Parse the initializer list. */
16563 CONSTRUCTOR_ELTS (initializer)
16564 = cp_parser_initializer_list (parser, non_constant_p);
16565 /* A trailing `,' token is allowed. */
16566 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16567 cp_lexer_consume_token (parser->lexer);
16569 /* Now, there should be a trailing `}'. */
16570 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16571 TREE_TYPE (initializer) = init_list_type_node;
16572 return initializer;
16575 /* Parse an initializer-list.
16578 initializer-clause ... [opt]
16579 initializer-list , initializer-clause ... [opt]
16584 identifier : initializer-clause
16585 initializer-list, identifier : initializer-clause
16587 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16588 for the initializer. If the INDEX of the elt is non-NULL, it is the
16589 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16590 as for cp_parser_initializer. */
16592 static VEC(constructor_elt,gc) *
16593 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16595 VEC(constructor_elt,gc) *v = NULL;
16597 /* Assume all of the expressions are constant. */
16598 *non_constant_p = false;
16600 /* Parse the rest of the list. */
16606 bool clause_non_constant_p;
16608 /* If the next token is an identifier and the following one is a
16609 colon, we are looking at the GNU designated-initializer
16611 if (cp_parser_allow_gnu_extensions_p (parser)
16612 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16613 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16615 /* Warn the user that they are using an extension. */
16616 pedwarn (input_location, OPT_pedantic,
16617 "ISO C++ does not allow designated initializers");
16618 /* Consume the identifier. */
16619 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16620 /* Consume the `:'. */
16621 cp_lexer_consume_token (parser->lexer);
16624 identifier = NULL_TREE;
16626 /* Parse the initializer. */
16627 initializer = cp_parser_initializer_clause (parser,
16628 &clause_non_constant_p);
16629 /* If any clause is non-constant, so is the entire initializer. */
16630 if (clause_non_constant_p)
16631 *non_constant_p = true;
16633 /* If we have an ellipsis, this is an initializer pack
16635 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16637 /* Consume the `...'. */
16638 cp_lexer_consume_token (parser->lexer);
16640 /* Turn the initializer into an initializer expansion. */
16641 initializer = make_pack_expansion (initializer);
16644 /* Add it to the vector. */
16645 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16647 /* If the next token is not a comma, we have reached the end of
16649 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16652 /* Peek at the next token. */
16653 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16654 /* If the next token is a `}', then we're still done. An
16655 initializer-clause can have a trailing `,' after the
16656 initializer-list and before the closing `}'. */
16657 if (token->type == CPP_CLOSE_BRACE)
16660 /* Consume the `,' token. */
16661 cp_lexer_consume_token (parser->lexer);
16667 /* Classes [gram.class] */
16669 /* Parse a class-name.
16675 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16676 to indicate that names looked up in dependent types should be
16677 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16678 keyword has been used to indicate that the name that appears next
16679 is a template. TAG_TYPE indicates the explicit tag given before
16680 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16681 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16682 is the class being defined in a class-head.
16684 Returns the TYPE_DECL representing the class. */
16687 cp_parser_class_name (cp_parser *parser,
16688 bool typename_keyword_p,
16689 bool template_keyword_p,
16690 enum tag_types tag_type,
16691 bool check_dependency_p,
16693 bool is_declaration)
16699 tree identifier = NULL_TREE;
16701 /* All class-names start with an identifier. */
16702 token = cp_lexer_peek_token (parser->lexer);
16703 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16705 cp_parser_error (parser, "expected class-name");
16706 return error_mark_node;
16709 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16710 to a template-id, so we save it here. */
16711 scope = parser->scope;
16712 if (scope == error_mark_node)
16713 return error_mark_node;
16715 /* Any name names a type if we're following the `typename' keyword
16716 in a qualified name where the enclosing scope is type-dependent. */
16717 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16718 && dependent_type_p (scope));
16719 /* Handle the common case (an identifier, but not a template-id)
16721 if (token->type == CPP_NAME
16722 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16724 cp_token *identifier_token;
16727 /* Look for the identifier. */
16728 identifier_token = cp_lexer_peek_token (parser->lexer);
16729 ambiguous_p = identifier_token->ambiguous_p;
16730 identifier = cp_parser_identifier (parser);
16731 /* If the next token isn't an identifier, we are certainly not
16732 looking at a class-name. */
16733 if (identifier == error_mark_node)
16734 decl = error_mark_node;
16735 /* If we know this is a type-name, there's no need to look it
16737 else if (typename_p)
16741 tree ambiguous_decls;
16742 /* If we already know that this lookup is ambiguous, then
16743 we've already issued an error message; there's no reason
16747 cp_parser_simulate_error (parser);
16748 return error_mark_node;
16750 /* If the next token is a `::', then the name must be a type
16753 [basic.lookup.qual]
16755 During the lookup for a name preceding the :: scope
16756 resolution operator, object, function, and enumerator
16757 names are ignored. */
16758 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16759 tag_type = typename_type;
16760 /* Look up the name. */
16761 decl = cp_parser_lookup_name (parser, identifier,
16763 /*is_template=*/false,
16764 /*is_namespace=*/false,
16765 check_dependency_p,
16767 identifier_token->location);
16768 if (ambiguous_decls)
16770 if (cp_parser_parsing_tentatively (parser))
16771 cp_parser_simulate_error (parser);
16772 return error_mark_node;
16778 /* Try a template-id. */
16779 decl = cp_parser_template_id (parser, template_keyword_p,
16780 check_dependency_p,
16782 if (decl == error_mark_node)
16783 return error_mark_node;
16786 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16788 /* If this is a typename, create a TYPENAME_TYPE. */
16789 if (typename_p && decl != error_mark_node)
16791 decl = make_typename_type (scope, decl, typename_type,
16792 /*complain=*/tf_error);
16793 if (decl != error_mark_node)
16794 decl = TYPE_NAME (decl);
16797 /* Check to see that it is really the name of a class. */
16798 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16799 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16800 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16801 /* Situations like this:
16803 template <typename T> struct A {
16804 typename T::template X<int>::I i;
16807 are problematic. Is `T::template X<int>' a class-name? The
16808 standard does not seem to be definitive, but there is no other
16809 valid interpretation of the following `::'. Therefore, those
16810 names are considered class-names. */
16812 decl = make_typename_type (scope, decl, tag_type, tf_error);
16813 if (decl != error_mark_node)
16814 decl = TYPE_NAME (decl);
16816 else if (TREE_CODE (decl) != TYPE_DECL
16817 || TREE_TYPE (decl) == error_mark_node
16818 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16819 /* In Objective-C 2.0, a classname followed by '.' starts a
16820 dot-syntax expression, and it's not a type-name. */
16821 || (c_dialect_objc ()
16822 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16823 && objc_is_class_name (decl)))
16824 decl = error_mark_node;
16826 if (decl == error_mark_node)
16827 cp_parser_error (parser, "expected class-name");
16828 else if (identifier && !parser->scope)
16829 maybe_note_name_used_in_class (identifier, decl);
16834 /* Parse a class-specifier.
16837 class-head { member-specification [opt] }
16839 Returns the TREE_TYPE representing the class. */
16842 cp_parser_class_specifier_1 (cp_parser* parser)
16845 tree attributes = NULL_TREE;
16846 bool nested_name_specifier_p;
16847 unsigned saved_num_template_parameter_lists;
16848 bool saved_in_function_body;
16849 bool saved_in_unbraced_linkage_specification_p;
16850 tree old_scope = NULL_TREE;
16851 tree scope = NULL_TREE;
16853 cp_token *closing_brace;
16855 push_deferring_access_checks (dk_no_deferred);
16857 /* Parse the class-head. */
16858 type = cp_parser_class_head (parser,
16859 &nested_name_specifier_p,
16862 /* If the class-head was a semantic disaster, skip the entire body
16866 cp_parser_skip_to_end_of_block_or_statement (parser);
16867 pop_deferring_access_checks ();
16868 return error_mark_node;
16871 /* Look for the `{'. */
16872 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16874 pop_deferring_access_checks ();
16875 return error_mark_node;
16878 /* Process the base classes. If they're invalid, skip the
16879 entire class body. */
16880 if (!xref_basetypes (type, bases))
16882 /* Consuming the closing brace yields better error messages
16884 if (cp_parser_skip_to_closing_brace (parser))
16885 cp_lexer_consume_token (parser->lexer);
16886 pop_deferring_access_checks ();
16887 return error_mark_node;
16890 /* Issue an error message if type-definitions are forbidden here. */
16891 cp_parser_check_type_definition (parser);
16892 /* Remember that we are defining one more class. */
16893 ++parser->num_classes_being_defined;
16894 /* Inside the class, surrounding template-parameter-lists do not
16896 saved_num_template_parameter_lists
16897 = parser->num_template_parameter_lists;
16898 parser->num_template_parameter_lists = 0;
16899 /* We are not in a function body. */
16900 saved_in_function_body = parser->in_function_body;
16901 parser->in_function_body = false;
16902 /* We are not immediately inside an extern "lang" block. */
16903 saved_in_unbraced_linkage_specification_p
16904 = parser->in_unbraced_linkage_specification_p;
16905 parser->in_unbraced_linkage_specification_p = false;
16907 /* Start the class. */
16908 if (nested_name_specifier_p)
16910 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16911 old_scope = push_inner_scope (scope);
16913 type = begin_class_definition (type, attributes);
16915 if (type == error_mark_node)
16916 /* If the type is erroneous, skip the entire body of the class. */
16917 cp_parser_skip_to_closing_brace (parser);
16919 /* Parse the member-specification. */
16920 cp_parser_member_specification_opt (parser);
16922 /* Look for the trailing `}'. */
16923 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16924 /* Look for trailing attributes to apply to this class. */
16925 if (cp_parser_allow_gnu_extensions_p (parser))
16926 attributes = cp_parser_attributes_opt (parser);
16927 if (type != error_mark_node)
16928 type = finish_struct (type, attributes);
16929 if (nested_name_specifier_p)
16930 pop_inner_scope (old_scope, scope);
16932 /* We've finished a type definition. Check for the common syntax
16933 error of forgetting a semicolon after the definition. We need to
16934 be careful, as we can't just check for not-a-semicolon and be done
16935 with it; the user might have typed:
16937 class X { } c = ...;
16938 class X { } *p = ...;
16940 and so forth. Instead, enumerate all the possible tokens that
16941 might follow this production; if we don't see one of them, then
16942 complain and silently insert the semicolon. */
16944 cp_token *token = cp_lexer_peek_token (parser->lexer);
16945 bool want_semicolon = true;
16947 switch (token->type)
16950 case CPP_SEMICOLON:
16953 case CPP_OPEN_PAREN:
16954 case CPP_CLOSE_PAREN:
16956 want_semicolon = false;
16959 /* While it's legal for type qualifiers and storage class
16960 specifiers to follow type definitions in the grammar, only
16961 compiler testsuites contain code like that. Assume that if
16962 we see such code, then what we're really seeing is a case
16966 const <type> var = ...;
16971 static <type> func (...) ...
16973 i.e. the qualifier or specifier applies to the next
16974 declaration. To do so, however, we need to look ahead one
16975 more token to see if *that* token is a type specifier.
16977 This code could be improved to handle:
16980 static const <type> var = ...; */
16982 if (keyword_is_decl_specifier (token->keyword))
16984 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16986 /* Handling user-defined types here would be nice, but very
16989 = (lookahead->type == CPP_KEYWORD
16990 && keyword_begins_type_specifier (lookahead->keyword));
16997 /* If we don't have a type, then something is very wrong and we
16998 shouldn't try to do anything clever. Likewise for not seeing the
17000 if (closing_brace && TYPE_P (type) && want_semicolon)
17002 cp_token_position prev
17003 = cp_lexer_previous_token_position (parser->lexer);
17004 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17005 location_t loc = prev_token->location;
17007 if (CLASSTYPE_DECLARED_CLASS (type))
17008 error_at (loc, "expected %<;%> after class definition");
17009 else if (TREE_CODE (type) == RECORD_TYPE)
17010 error_at (loc, "expected %<;%> after struct definition");
17011 else if (TREE_CODE (type) == UNION_TYPE)
17012 error_at (loc, "expected %<;%> after union definition");
17014 gcc_unreachable ();
17016 /* Unget one token and smash it to look as though we encountered
17017 a semicolon in the input stream. */
17018 cp_lexer_set_token_position (parser->lexer, prev);
17019 token = cp_lexer_peek_token (parser->lexer);
17020 token->type = CPP_SEMICOLON;
17021 token->keyword = RID_MAX;
17025 /* If this class is not itself within the scope of another class,
17026 then we need to parse the bodies of all of the queued function
17027 definitions. Note that the queued functions defined in a class
17028 are not always processed immediately following the
17029 class-specifier for that class. Consider:
17032 struct B { void f() { sizeof (A); } };
17035 If `f' were processed before the processing of `A' were
17036 completed, there would be no way to compute the size of `A'.
17037 Note that the nesting we are interested in here is lexical --
17038 not the semantic nesting given by TYPE_CONTEXT. In particular,
17041 struct A { struct B; };
17042 struct A::B { void f() { } };
17044 there is no need to delay the parsing of `A::B::f'. */
17045 if (--parser->num_classes_being_defined == 0)
17048 tree class_type = NULL_TREE;
17049 tree pushed_scope = NULL_TREE;
17051 cp_default_arg_entry *e;
17053 /* In a first pass, parse default arguments to the functions.
17054 Then, in a second pass, parse the bodies of the functions.
17055 This two-phased approach handles cases like:
17063 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17067 /* If there are default arguments that have not yet been processed,
17068 take care of them now. */
17069 if (class_type != e->class_type)
17072 pop_scope (pushed_scope);
17073 class_type = e->class_type;
17074 pushed_scope = push_scope (class_type);
17076 /* Make sure that any template parameters are in scope. */
17077 maybe_begin_member_template_processing (fn);
17078 /* Parse the default argument expressions. */
17079 cp_parser_late_parsing_default_args (parser, fn);
17080 /* Remove any template parameters from the symbol table. */
17081 maybe_end_member_template_processing ();
17084 pop_scope (pushed_scope);
17085 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17086 /* Now parse the body of the functions. */
17087 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17088 cp_parser_late_parsing_for_member (parser, fn);
17089 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17092 /* Put back any saved access checks. */
17093 pop_deferring_access_checks ();
17095 /* Restore saved state. */
17096 parser->in_function_body = saved_in_function_body;
17097 parser->num_template_parameter_lists
17098 = saved_num_template_parameter_lists;
17099 parser->in_unbraced_linkage_specification_p
17100 = saved_in_unbraced_linkage_specification_p;
17106 cp_parser_class_specifier (cp_parser* parser)
17109 timevar_push (TV_PARSE_STRUCT);
17110 ret = cp_parser_class_specifier_1 (parser);
17111 timevar_pop (TV_PARSE_STRUCT);
17115 /* Parse a class-head.
17118 class-key identifier [opt] base-clause [opt]
17119 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17120 class-key nested-name-specifier [opt] template-id
17123 class-virt-specifier:
17127 class-key attributes identifier [opt] base-clause [opt]
17128 class-key attributes nested-name-specifier identifier base-clause [opt]
17129 class-key attributes nested-name-specifier [opt] template-id
17132 Upon return BASES is initialized to the list of base classes (or
17133 NULL, if there are none) in the same form returned by
17134 cp_parser_base_clause.
17136 Returns the TYPE of the indicated class. Sets
17137 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17138 involving a nested-name-specifier was used, and FALSE otherwise.
17140 Returns error_mark_node if this is not a class-head.
17142 Returns NULL_TREE if the class-head is syntactically valid, but
17143 semantically invalid in a way that means we should skip the entire
17144 body of the class. */
17147 cp_parser_class_head (cp_parser* parser,
17148 bool* nested_name_specifier_p,
17149 tree *attributes_p,
17152 tree nested_name_specifier;
17153 enum tag_types class_key;
17154 tree id = NULL_TREE;
17155 tree type = NULL_TREE;
17157 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17158 bool template_id_p = false;
17159 bool qualified_p = false;
17160 bool invalid_nested_name_p = false;
17161 bool invalid_explicit_specialization_p = false;
17162 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17163 tree pushed_scope = NULL_TREE;
17164 unsigned num_templates;
17165 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17166 /* Assume no nested-name-specifier will be present. */
17167 *nested_name_specifier_p = false;
17168 /* Assume no template parameter lists will be used in defining the
17171 parser->colon_corrects_to_scope_p = false;
17173 *bases = NULL_TREE;
17175 /* Look for the class-key. */
17176 class_key = cp_parser_class_key (parser);
17177 if (class_key == none_type)
17178 return error_mark_node;
17180 /* Parse the attributes. */
17181 attributes = cp_parser_attributes_opt (parser);
17183 /* If the next token is `::', that is invalid -- but sometimes
17184 people do try to write:
17188 Handle this gracefully by accepting the extra qualifier, and then
17189 issuing an error about it later if this really is a
17190 class-head. If it turns out just to be an elaborated type
17191 specifier, remain silent. */
17192 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17193 qualified_p = true;
17195 push_deferring_access_checks (dk_no_check);
17197 /* Determine the name of the class. Begin by looking for an
17198 optional nested-name-specifier. */
17199 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17200 nested_name_specifier
17201 = cp_parser_nested_name_specifier_opt (parser,
17202 /*typename_keyword_p=*/false,
17203 /*check_dependency_p=*/false,
17205 /*is_declaration=*/false);
17206 /* If there was a nested-name-specifier, then there *must* be an
17208 if (nested_name_specifier)
17210 type_start_token = cp_lexer_peek_token (parser->lexer);
17211 /* Although the grammar says `identifier', it really means
17212 `class-name' or `template-name'. You are only allowed to
17213 define a class that has already been declared with this
17216 The proposed resolution for Core Issue 180 says that wherever
17217 you see `class T::X' you should treat `X' as a type-name.
17219 It is OK to define an inaccessible class; for example:
17221 class A { class B; };
17224 We do not know if we will see a class-name, or a
17225 template-name. We look for a class-name first, in case the
17226 class-name is a template-id; if we looked for the
17227 template-name first we would stop after the template-name. */
17228 cp_parser_parse_tentatively (parser);
17229 type = cp_parser_class_name (parser,
17230 /*typename_keyword_p=*/false,
17231 /*template_keyword_p=*/false,
17233 /*check_dependency_p=*/false,
17234 /*class_head_p=*/true,
17235 /*is_declaration=*/false);
17236 /* If that didn't work, ignore the nested-name-specifier. */
17237 if (!cp_parser_parse_definitely (parser))
17239 invalid_nested_name_p = true;
17240 type_start_token = cp_lexer_peek_token (parser->lexer);
17241 id = cp_parser_identifier (parser);
17242 if (id == error_mark_node)
17245 /* If we could not find a corresponding TYPE, treat this
17246 declaration like an unqualified declaration. */
17247 if (type == error_mark_node)
17248 nested_name_specifier = NULL_TREE;
17249 /* Otherwise, count the number of templates used in TYPE and its
17250 containing scopes. */
17255 for (scope = TREE_TYPE (type);
17256 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17257 scope = (TYPE_P (scope)
17258 ? TYPE_CONTEXT (scope)
17259 : DECL_CONTEXT (scope)))
17261 && CLASS_TYPE_P (scope)
17262 && CLASSTYPE_TEMPLATE_INFO (scope)
17263 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17264 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17268 /* Otherwise, the identifier is optional. */
17271 /* We don't know whether what comes next is a template-id,
17272 an identifier, or nothing at all. */
17273 cp_parser_parse_tentatively (parser);
17274 /* Check for a template-id. */
17275 type_start_token = cp_lexer_peek_token (parser->lexer);
17276 id = cp_parser_template_id (parser,
17277 /*template_keyword_p=*/false,
17278 /*check_dependency_p=*/true,
17279 /*is_declaration=*/true);
17280 /* If that didn't work, it could still be an identifier. */
17281 if (!cp_parser_parse_definitely (parser))
17283 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17285 type_start_token = cp_lexer_peek_token (parser->lexer);
17286 id = cp_parser_identifier (parser);
17293 template_id_p = true;
17298 pop_deferring_access_checks ();
17302 cp_parser_check_for_invalid_template_id (parser, id,
17303 type_start_token->location);
17304 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17307 /* If it's not a `:' or a `{' then we can't really be looking at a
17308 class-head, since a class-head only appears as part of a
17309 class-specifier. We have to detect this situation before calling
17310 xref_tag, since that has irreversible side-effects. */
17311 if (!cp_parser_next_token_starts_class_definition_p (parser))
17313 cp_parser_error (parser, "expected %<{%> or %<:%>");
17314 type = error_mark_node;
17318 /* At this point, we're going ahead with the class-specifier, even
17319 if some other problem occurs. */
17320 cp_parser_commit_to_tentative_parse (parser);
17321 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17323 cp_parser_error (parser,
17324 "cannot specify %<override%> for a class");
17325 type = error_mark_node;
17328 /* Issue the error about the overly-qualified name now. */
17331 cp_parser_error (parser,
17332 "global qualification of class name is invalid");
17333 type = error_mark_node;
17336 else if (invalid_nested_name_p)
17338 cp_parser_error (parser,
17339 "qualified name does not name a class");
17340 type = error_mark_node;
17343 else if (nested_name_specifier)
17347 /* Reject typedef-names in class heads. */
17348 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17350 error_at (type_start_token->location,
17351 "invalid class name in declaration of %qD",
17357 /* Figure out in what scope the declaration is being placed. */
17358 scope = current_scope ();
17359 /* If that scope does not contain the scope in which the
17360 class was originally declared, the program is invalid. */
17361 if (scope && !is_ancestor (scope, nested_name_specifier))
17363 if (at_namespace_scope_p ())
17364 error_at (type_start_token->location,
17365 "declaration of %qD in namespace %qD which does not "
17367 type, scope, nested_name_specifier);
17369 error_at (type_start_token->location,
17370 "declaration of %qD in %qD which does not enclose %qD",
17371 type, scope, nested_name_specifier);
17377 A declarator-id shall not be qualified except for the
17378 definition of a ... nested class outside of its class
17379 ... [or] the definition or explicit instantiation of a
17380 class member of a namespace outside of its namespace. */
17381 if (scope == nested_name_specifier)
17383 permerror (nested_name_specifier_token_start->location,
17384 "extra qualification not allowed");
17385 nested_name_specifier = NULL_TREE;
17389 /* An explicit-specialization must be preceded by "template <>". If
17390 it is not, try to recover gracefully. */
17391 if (at_namespace_scope_p ()
17392 && parser->num_template_parameter_lists == 0
17395 error_at (type_start_token->location,
17396 "an explicit specialization must be preceded by %<template <>%>");
17397 invalid_explicit_specialization_p = true;
17398 /* Take the same action that would have been taken by
17399 cp_parser_explicit_specialization. */
17400 ++parser->num_template_parameter_lists;
17401 begin_specialization ();
17403 /* There must be no "return" statements between this point and the
17404 end of this function; set "type "to the correct return value and
17405 use "goto done;" to return. */
17406 /* Make sure that the right number of template parameters were
17408 if (!cp_parser_check_template_parameters (parser, num_templates,
17409 type_start_token->location,
17410 /*declarator=*/NULL))
17412 /* If something went wrong, there is no point in even trying to
17413 process the class-definition. */
17418 /* Look up the type. */
17421 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17422 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17423 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17425 error_at (type_start_token->location,
17426 "function template %qD redeclared as a class template", id);
17427 type = error_mark_node;
17431 type = TREE_TYPE (id);
17432 type = maybe_process_partial_specialization (type);
17434 if (nested_name_specifier)
17435 pushed_scope = push_scope (nested_name_specifier);
17437 else if (nested_name_specifier)
17443 template <typename T> struct S { struct T };
17444 template <typename T> struct S<T>::T { };
17446 we will get a TYPENAME_TYPE when processing the definition of
17447 `S::T'. We need to resolve it to the actual type before we
17448 try to define it. */
17449 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17451 class_type = resolve_typename_type (TREE_TYPE (type),
17452 /*only_current_p=*/false);
17453 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17454 type = TYPE_NAME (class_type);
17457 cp_parser_error (parser, "could not resolve typename type");
17458 type = error_mark_node;
17462 if (maybe_process_partial_specialization (TREE_TYPE (type))
17463 == error_mark_node)
17469 class_type = current_class_type;
17470 /* Enter the scope indicated by the nested-name-specifier. */
17471 pushed_scope = push_scope (nested_name_specifier);
17472 /* Get the canonical version of this type. */
17473 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17474 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17475 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17477 type = push_template_decl (type);
17478 if (type == error_mark_node)
17485 type = TREE_TYPE (type);
17486 *nested_name_specifier_p = true;
17488 else /* The name is not a nested name. */
17490 /* If the class was unnamed, create a dummy name. */
17492 id = make_anon_name ();
17493 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17494 parser->num_template_parameter_lists);
17497 /* Indicate whether this class was declared as a `class' or as a
17499 if (TREE_CODE (type) == RECORD_TYPE)
17500 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17501 cp_parser_check_class_key (class_key, type);
17503 /* If this type was already complete, and we see another definition,
17504 that's an error. */
17505 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17507 error_at (type_start_token->location, "redefinition of %q#T",
17509 error_at (type_start_token->location, "previous definition of %q+#T",
17514 else if (type == error_mark_node)
17517 /* We will have entered the scope containing the class; the names of
17518 base classes should be looked up in that context. For example:
17520 struct A { struct B {}; struct C; };
17521 struct A::C : B {};
17525 /* Get the list of base-classes, if there is one. */
17526 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17527 *bases = cp_parser_base_clause (parser);
17530 /* Leave the scope given by the nested-name-specifier. We will
17531 enter the class scope itself while processing the members. */
17533 pop_scope (pushed_scope);
17535 if (invalid_explicit_specialization_p)
17537 end_specialization ();
17538 --parser->num_template_parameter_lists;
17542 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17543 *attributes_p = attributes;
17544 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17545 CLASSTYPE_FINAL (type) = 1;
17547 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17551 /* Parse a class-key.
17558 Returns the kind of class-key specified, or none_type to indicate
17561 static enum tag_types
17562 cp_parser_class_key (cp_parser* parser)
17565 enum tag_types tag_type;
17567 /* Look for the class-key. */
17568 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17572 /* Check to see if the TOKEN is a class-key. */
17573 tag_type = cp_parser_token_is_class_key (token);
17575 cp_parser_error (parser, "expected class-key");
17579 /* Parse an (optional) member-specification.
17581 member-specification:
17582 member-declaration member-specification [opt]
17583 access-specifier : member-specification [opt] */
17586 cp_parser_member_specification_opt (cp_parser* parser)
17593 /* Peek at the next token. */
17594 token = cp_lexer_peek_token (parser->lexer);
17595 /* If it's a `}', or EOF then we've seen all the members. */
17596 if (token->type == CPP_CLOSE_BRACE
17597 || token->type == CPP_EOF
17598 || token->type == CPP_PRAGMA_EOL)
17601 /* See if this token is a keyword. */
17602 keyword = token->keyword;
17606 case RID_PROTECTED:
17608 /* Consume the access-specifier. */
17609 cp_lexer_consume_token (parser->lexer);
17610 /* Remember which access-specifier is active. */
17611 current_access_specifier = token->u.value;
17612 /* Look for the `:'. */
17613 cp_parser_require (parser, CPP_COLON, RT_COLON);
17617 /* Accept #pragmas at class scope. */
17618 if (token->type == CPP_PRAGMA)
17620 cp_parser_pragma (parser, pragma_external);
17624 /* Otherwise, the next construction must be a
17625 member-declaration. */
17626 cp_parser_member_declaration (parser);
17631 /* Parse a member-declaration.
17633 member-declaration:
17634 decl-specifier-seq [opt] member-declarator-list [opt] ;
17635 function-definition ; [opt]
17636 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17638 template-declaration
17640 member-declarator-list:
17642 member-declarator-list , member-declarator
17645 declarator pure-specifier [opt]
17646 declarator constant-initializer [opt]
17647 identifier [opt] : constant-expression
17651 member-declaration:
17652 __extension__ member-declaration
17655 declarator attributes [opt] pure-specifier [opt]
17656 declarator attributes [opt] constant-initializer [opt]
17657 identifier [opt] attributes [opt] : constant-expression
17661 member-declaration:
17662 static_assert-declaration */
17665 cp_parser_member_declaration (cp_parser* parser)
17667 cp_decl_specifier_seq decl_specifiers;
17668 tree prefix_attributes;
17670 int declares_class_or_enum;
17672 cp_token *token = NULL;
17673 cp_token *decl_spec_token_start = NULL;
17674 cp_token *initializer_token_start = NULL;
17675 int saved_pedantic;
17676 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17678 /* Check for the `__extension__' keyword. */
17679 if (cp_parser_extension_opt (parser, &saved_pedantic))
17682 cp_parser_member_declaration (parser);
17683 /* Restore the old value of the PEDANTIC flag. */
17684 pedantic = saved_pedantic;
17689 /* Check for a template-declaration. */
17690 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17692 /* An explicit specialization here is an error condition, and we
17693 expect the specialization handler to detect and report this. */
17694 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17695 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17696 cp_parser_explicit_specialization (parser);
17698 cp_parser_template_declaration (parser, /*member_p=*/true);
17703 /* Check for a using-declaration. */
17704 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17706 /* Parse the using-declaration. */
17707 cp_parser_using_declaration (parser,
17708 /*access_declaration_p=*/false);
17712 /* Check for @defs. */
17713 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17716 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17717 ivar = ivar_chains;
17721 ivar = TREE_CHAIN (member);
17722 TREE_CHAIN (member) = NULL_TREE;
17723 finish_member_declaration (member);
17728 /* If the next token is `static_assert' we have a static assertion. */
17729 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17731 cp_parser_static_assert (parser, /*member_p=*/true);
17735 parser->colon_corrects_to_scope_p = false;
17737 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17740 /* Parse the decl-specifier-seq. */
17741 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17742 cp_parser_decl_specifier_seq (parser,
17743 CP_PARSER_FLAGS_OPTIONAL,
17745 &declares_class_or_enum);
17746 prefix_attributes = decl_specifiers.attributes;
17747 decl_specifiers.attributes = NULL_TREE;
17748 /* Check for an invalid type-name. */
17749 if (!decl_specifiers.any_type_specifiers_p
17750 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17752 /* If there is no declarator, then the decl-specifier-seq should
17754 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17756 /* If there was no decl-specifier-seq, and the next token is a
17757 `;', then we have something like:
17763 Each member-declaration shall declare at least one member
17764 name of the class. */
17765 if (!decl_specifiers.any_specifiers_p)
17767 cp_token *token = cp_lexer_peek_token (parser->lexer);
17768 if (!in_system_header_at (token->location))
17769 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17775 /* See if this declaration is a friend. */
17776 friend_p = cp_parser_friend_p (&decl_specifiers);
17777 /* If there were decl-specifiers, check to see if there was
17778 a class-declaration. */
17779 type = check_tag_decl (&decl_specifiers);
17780 /* Nested classes have already been added to the class, but
17781 a `friend' needs to be explicitly registered. */
17784 /* If the `friend' keyword was present, the friend must
17785 be introduced with a class-key. */
17786 if (!declares_class_or_enum && cxx_dialect < cxx0x)
17787 pedwarn (decl_spec_token_start->location, OPT_pedantic,
17788 "in C++03 a class-key must be used "
17789 "when declaring a friend");
17792 template <typename T> struct A {
17793 friend struct A<T>::B;
17796 A<T>::B will be represented by a TYPENAME_TYPE, and
17797 therefore not recognized by check_tag_decl. */
17800 type = decl_specifiers.type;
17801 if (type && TREE_CODE (type) == TYPE_DECL)
17802 type = TREE_TYPE (type);
17804 if (!type || !TYPE_P (type))
17805 error_at (decl_spec_token_start->location,
17806 "friend declaration does not name a class or "
17809 make_friend_class (current_class_type, type,
17810 /*complain=*/true);
17812 /* If there is no TYPE, an error message will already have
17814 else if (!type || type == error_mark_node)
17816 /* An anonymous aggregate has to be handled specially; such
17817 a declaration really declares a data member (with a
17818 particular type), as opposed to a nested class. */
17819 else if (ANON_AGGR_TYPE_P (type))
17821 /* Remove constructors and such from TYPE, now that we
17822 know it is an anonymous aggregate. */
17823 fixup_anonymous_aggr (type);
17824 /* And make the corresponding data member. */
17825 decl = build_decl (decl_spec_token_start->location,
17826 FIELD_DECL, NULL_TREE, type);
17827 /* Add it to the class. */
17828 finish_member_declaration (decl);
17831 cp_parser_check_access_in_redeclaration
17833 decl_spec_token_start->location);
17838 bool assume_semicolon = false;
17840 /* See if these declarations will be friends. */
17841 friend_p = cp_parser_friend_p (&decl_specifiers);
17843 /* Keep going until we hit the `;' at the end of the
17845 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17847 tree attributes = NULL_TREE;
17848 tree first_attribute;
17850 /* Peek at the next token. */
17851 token = cp_lexer_peek_token (parser->lexer);
17853 /* Check for a bitfield declaration. */
17854 if (token->type == CPP_COLON
17855 || (token->type == CPP_NAME
17856 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17862 /* Get the name of the bitfield. Note that we cannot just
17863 check TOKEN here because it may have been invalidated by
17864 the call to cp_lexer_peek_nth_token above. */
17865 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17866 identifier = cp_parser_identifier (parser);
17868 identifier = NULL_TREE;
17870 /* Consume the `:' token. */
17871 cp_lexer_consume_token (parser->lexer);
17872 /* Get the width of the bitfield. */
17874 = cp_parser_constant_expression (parser,
17875 /*allow_non_constant=*/false,
17878 /* Look for attributes that apply to the bitfield. */
17879 attributes = cp_parser_attributes_opt (parser);
17880 /* Remember which attributes are prefix attributes and
17882 first_attribute = attributes;
17883 /* Combine the attributes. */
17884 attributes = chainon (prefix_attributes, attributes);
17886 /* Create the bitfield declaration. */
17887 decl = grokbitfield (identifier
17888 ? make_id_declarator (NULL_TREE,
17898 cp_declarator *declarator;
17900 tree asm_specification;
17901 int ctor_dtor_or_conv_p;
17903 /* Parse the declarator. */
17905 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17906 &ctor_dtor_or_conv_p,
17907 /*parenthesized_p=*/NULL,
17908 /*member_p=*/true);
17910 /* If something went wrong parsing the declarator, make sure
17911 that we at least consume some tokens. */
17912 if (declarator == cp_error_declarator)
17914 /* Skip to the end of the statement. */
17915 cp_parser_skip_to_end_of_statement (parser);
17916 /* If the next token is not a semicolon, that is
17917 probably because we just skipped over the body of
17918 a function. So, we consume a semicolon if
17919 present, but do not issue an error message if it
17921 if (cp_lexer_next_token_is (parser->lexer,
17923 cp_lexer_consume_token (parser->lexer);
17927 if (declares_class_or_enum & 2)
17928 cp_parser_check_for_definition_in_return_type
17929 (declarator, decl_specifiers.type,
17930 decl_specifiers.type_location);
17932 /* Look for an asm-specification. */
17933 asm_specification = cp_parser_asm_specification_opt (parser);
17934 /* Look for attributes that apply to the declaration. */
17935 attributes = cp_parser_attributes_opt (parser);
17936 /* Remember which attributes are prefix attributes and
17938 first_attribute = attributes;
17939 /* Combine the attributes. */
17940 attributes = chainon (prefix_attributes, attributes);
17942 /* If it's an `=', then we have a constant-initializer or a
17943 pure-specifier. It is not correct to parse the
17944 initializer before registering the member declaration
17945 since the member declaration should be in scope while
17946 its initializer is processed. However, the rest of the
17947 front end does not yet provide an interface that allows
17948 us to handle this correctly. */
17949 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17953 A pure-specifier shall be used only in the declaration of
17954 a virtual function.
17956 A member-declarator can contain a constant-initializer
17957 only if it declares a static member of integral or
17960 Therefore, if the DECLARATOR is for a function, we look
17961 for a pure-specifier; otherwise, we look for a
17962 constant-initializer. When we call `grokfield', it will
17963 perform more stringent semantics checks. */
17964 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17965 if (function_declarator_p (declarator))
17966 initializer = cp_parser_pure_specifier (parser);
17968 /* Parse the initializer. */
17969 initializer = cp_parser_constant_initializer (parser);
17971 /* Otherwise, there is no initializer. */
17973 initializer = NULL_TREE;
17975 /* See if we are probably looking at a function
17976 definition. We are certainly not looking at a
17977 member-declarator. Calling `grokfield' has
17978 side-effects, so we must not do it unless we are sure
17979 that we are looking at a member-declarator. */
17980 if (cp_parser_token_starts_function_definition_p
17981 (cp_lexer_peek_token (parser->lexer)))
17983 /* The grammar does not allow a pure-specifier to be
17984 used when a member function is defined. (It is
17985 possible that this fact is an oversight in the
17986 standard, since a pure function may be defined
17987 outside of the class-specifier. */
17989 error_at (initializer_token_start->location,
17990 "pure-specifier on function-definition");
17991 decl = cp_parser_save_member_function_body (parser,
17995 /* If the member was not a friend, declare it here. */
17997 finish_member_declaration (decl);
17998 /* Peek at the next token. */
17999 token = cp_lexer_peek_token (parser->lexer);
18000 /* If the next token is a semicolon, consume it. */
18001 if (token->type == CPP_SEMICOLON)
18002 cp_lexer_consume_token (parser->lexer);
18006 if (declarator->kind == cdk_function)
18007 declarator->id_loc = token->location;
18008 /* Create the declaration. */
18009 decl = grokfield (declarator, &decl_specifiers,
18010 initializer, /*init_const_expr_p=*/true,
18015 /* Reset PREFIX_ATTRIBUTES. */
18016 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18017 attributes = TREE_CHAIN (attributes);
18019 TREE_CHAIN (attributes) = NULL_TREE;
18021 /* If there is any qualification still in effect, clear it
18022 now; we will be starting fresh with the next declarator. */
18023 parser->scope = NULL_TREE;
18024 parser->qualifying_scope = NULL_TREE;
18025 parser->object_scope = NULL_TREE;
18026 /* If it's a `,', then there are more declarators. */
18027 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18028 cp_lexer_consume_token (parser->lexer);
18029 /* If the next token isn't a `;', then we have a parse error. */
18030 else if (cp_lexer_next_token_is_not (parser->lexer,
18033 /* The next token might be a ways away from where the
18034 actual semicolon is missing. Find the previous token
18035 and use that for our error position. */
18036 cp_token *token = cp_lexer_previous_token (parser->lexer);
18037 error_at (token->location,
18038 "expected %<;%> at end of member declaration");
18040 /* Assume that the user meant to provide a semicolon. If
18041 we were to cp_parser_skip_to_end_of_statement, we might
18042 skip to a semicolon inside a member function definition
18043 and issue nonsensical error messages. */
18044 assume_semicolon = true;
18049 /* Add DECL to the list of members. */
18051 finish_member_declaration (decl);
18053 if (TREE_CODE (decl) == FUNCTION_DECL)
18054 cp_parser_save_default_args (parser, decl);
18057 if (assume_semicolon)
18062 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18064 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18067 /* Parse a pure-specifier.
18072 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18073 Otherwise, ERROR_MARK_NODE is returned. */
18076 cp_parser_pure_specifier (cp_parser* parser)
18080 /* Look for the `=' token. */
18081 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18082 return error_mark_node;
18083 /* Look for the `0' token. */
18084 token = cp_lexer_peek_token (parser->lexer);
18086 if (token->type == CPP_EOF
18087 || token->type == CPP_PRAGMA_EOL)
18088 return error_mark_node;
18090 cp_lexer_consume_token (parser->lexer);
18092 /* Accept = default or = delete in c++0x mode. */
18093 if (token->keyword == RID_DEFAULT
18094 || token->keyword == RID_DELETE)
18096 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18097 return token->u.value;
18100 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18101 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18103 cp_parser_error (parser,
18104 "invalid pure specifier (only %<= 0%> is allowed)");
18105 cp_parser_skip_to_end_of_statement (parser);
18106 return error_mark_node;
18108 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18110 error_at (token->location, "templates may not be %<virtual%>");
18111 return error_mark_node;
18114 return integer_zero_node;
18117 /* Parse a constant-initializer.
18119 constant-initializer:
18120 = constant-expression
18122 Returns a representation of the constant-expression. */
18125 cp_parser_constant_initializer (cp_parser* parser)
18127 /* Look for the `=' token. */
18128 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18129 return error_mark_node;
18131 /* It is invalid to write:
18133 struct S { static const int i = { 7 }; };
18136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18138 cp_parser_error (parser,
18139 "a brace-enclosed initializer is not allowed here");
18140 /* Consume the opening brace. */
18141 cp_lexer_consume_token (parser->lexer);
18142 /* Skip the initializer. */
18143 cp_parser_skip_to_closing_brace (parser);
18144 /* Look for the trailing `}'. */
18145 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18147 return error_mark_node;
18150 return cp_parser_constant_expression (parser,
18151 /*allow_non_constant=*/false,
18155 /* Derived classes [gram.class.derived] */
18157 /* Parse a base-clause.
18160 : base-specifier-list
18162 base-specifier-list:
18163 base-specifier ... [opt]
18164 base-specifier-list , base-specifier ... [opt]
18166 Returns a TREE_LIST representing the base-classes, in the order in
18167 which they were declared. The representation of each node is as
18168 described by cp_parser_base_specifier.
18170 In the case that no bases are specified, this function will return
18171 NULL_TREE, not ERROR_MARK_NODE. */
18174 cp_parser_base_clause (cp_parser* parser)
18176 tree bases = NULL_TREE;
18178 /* Look for the `:' that begins the list. */
18179 cp_parser_require (parser, CPP_COLON, RT_COLON);
18181 /* Scan the base-specifier-list. */
18186 bool pack_expansion_p = false;
18188 /* Look for the base-specifier. */
18189 base = cp_parser_base_specifier (parser);
18190 /* Look for the (optional) ellipsis. */
18191 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18193 /* Consume the `...'. */
18194 cp_lexer_consume_token (parser->lexer);
18196 pack_expansion_p = true;
18199 /* Add BASE to the front of the list. */
18200 if (base != error_mark_node)
18202 if (pack_expansion_p)
18203 /* Make this a pack expansion type. */
18204 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18207 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18209 TREE_CHAIN (base) = bases;
18213 /* Peek at the next token. */
18214 token = cp_lexer_peek_token (parser->lexer);
18215 /* If it's not a comma, then the list is complete. */
18216 if (token->type != CPP_COMMA)
18218 /* Consume the `,'. */
18219 cp_lexer_consume_token (parser->lexer);
18222 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18223 base class had a qualified name. However, the next name that
18224 appears is certainly not qualified. */
18225 parser->scope = NULL_TREE;
18226 parser->qualifying_scope = NULL_TREE;
18227 parser->object_scope = NULL_TREE;
18229 return nreverse (bases);
18232 /* Parse a base-specifier.
18235 :: [opt] nested-name-specifier [opt] class-name
18236 virtual access-specifier [opt] :: [opt] nested-name-specifier
18238 access-specifier virtual [opt] :: [opt] nested-name-specifier
18241 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18242 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18243 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18244 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18247 cp_parser_base_specifier (cp_parser* parser)
18251 bool virtual_p = false;
18252 bool duplicate_virtual_error_issued_p = false;
18253 bool duplicate_access_error_issued_p = false;
18254 bool class_scope_p, template_p;
18255 tree access = access_default_node;
18258 /* Process the optional `virtual' and `access-specifier'. */
18261 /* Peek at the next token. */
18262 token = cp_lexer_peek_token (parser->lexer);
18263 /* Process `virtual'. */
18264 switch (token->keyword)
18267 /* If `virtual' appears more than once, issue an error. */
18268 if (virtual_p && !duplicate_virtual_error_issued_p)
18270 cp_parser_error (parser,
18271 "%<virtual%> specified more than once in base-specified");
18272 duplicate_virtual_error_issued_p = true;
18277 /* Consume the `virtual' token. */
18278 cp_lexer_consume_token (parser->lexer);
18283 case RID_PROTECTED:
18285 /* If more than one access specifier appears, issue an
18287 if (access != access_default_node
18288 && !duplicate_access_error_issued_p)
18290 cp_parser_error (parser,
18291 "more than one access specifier in base-specified");
18292 duplicate_access_error_issued_p = true;
18295 access = ridpointers[(int) token->keyword];
18297 /* Consume the access-specifier. */
18298 cp_lexer_consume_token (parser->lexer);
18307 /* It is not uncommon to see programs mechanically, erroneously, use
18308 the 'typename' keyword to denote (dependent) qualified types
18309 as base classes. */
18310 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18312 token = cp_lexer_peek_token (parser->lexer);
18313 if (!processing_template_decl)
18314 error_at (token->location,
18315 "keyword %<typename%> not allowed outside of templates");
18317 error_at (token->location,
18318 "keyword %<typename%> not allowed in this context "
18319 "(the base class is implicitly a type)");
18320 cp_lexer_consume_token (parser->lexer);
18323 /* Look for the optional `::' operator. */
18324 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18325 /* Look for the nested-name-specifier. The simplest way to
18330 The keyword `typename' is not permitted in a base-specifier or
18331 mem-initializer; in these contexts a qualified name that
18332 depends on a template-parameter is implicitly assumed to be a
18335 is to pretend that we have seen the `typename' keyword at this
18337 cp_parser_nested_name_specifier_opt (parser,
18338 /*typename_keyword_p=*/true,
18339 /*check_dependency_p=*/true,
18341 /*is_declaration=*/true);
18342 /* If the base class is given by a qualified name, assume that names
18343 we see are type names or templates, as appropriate. */
18344 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18345 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18347 /* Finally, look for the class-name. */
18348 type = cp_parser_class_name (parser,
18352 /*check_dependency_p=*/true,
18353 /*class_head_p=*/false,
18354 /*is_declaration=*/true);
18356 if (type == error_mark_node)
18357 return error_mark_node;
18359 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18362 /* Exception handling [gram.exception] */
18364 /* Parse an (optional) exception-specification.
18366 exception-specification:
18367 throw ( type-id-list [opt] )
18369 Returns a TREE_LIST representing the exception-specification. The
18370 TREE_VALUE of each node is a type. */
18373 cp_parser_exception_specification_opt (cp_parser* parser)
18377 const char *saved_message;
18379 /* Peek at the next token. */
18380 token = cp_lexer_peek_token (parser->lexer);
18382 /* Is it a noexcept-specification? */
18383 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18386 cp_lexer_consume_token (parser->lexer);
18388 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18390 cp_lexer_consume_token (parser->lexer);
18392 /* Types may not be defined in an exception-specification. */
18393 saved_message = parser->type_definition_forbidden_message;
18394 parser->type_definition_forbidden_message
18395 = G_("types may not be defined in an exception-specification");
18397 expr = cp_parser_constant_expression (parser, false, NULL);
18399 /* Restore the saved message. */
18400 parser->type_definition_forbidden_message = saved_message;
18402 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18405 expr = boolean_true_node;
18407 return build_noexcept_spec (expr, tf_warning_or_error);
18410 /* If it's not `throw', then there's no exception-specification. */
18411 if (!cp_parser_is_keyword (token, RID_THROW))
18415 /* Enable this once a lot of code has transitioned to noexcept? */
18416 if (cxx_dialect == cxx0x && !in_system_header)
18417 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18418 "deprecated in C++0x; use %<noexcept%> instead");
18421 /* Consume the `throw'. */
18422 cp_lexer_consume_token (parser->lexer);
18424 /* Look for the `('. */
18425 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18427 /* Peek at the next token. */
18428 token = cp_lexer_peek_token (parser->lexer);
18429 /* If it's not a `)', then there is a type-id-list. */
18430 if (token->type != CPP_CLOSE_PAREN)
18432 /* Types may not be defined in an exception-specification. */
18433 saved_message = parser->type_definition_forbidden_message;
18434 parser->type_definition_forbidden_message
18435 = G_("types may not be defined in an exception-specification");
18436 /* Parse the type-id-list. */
18437 type_id_list = cp_parser_type_id_list (parser);
18438 /* Restore the saved message. */
18439 parser->type_definition_forbidden_message = saved_message;
18442 type_id_list = empty_except_spec;
18444 /* Look for the `)'. */
18445 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18447 return type_id_list;
18450 /* Parse an (optional) type-id-list.
18454 type-id-list , type-id ... [opt]
18456 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18457 in the order that the types were presented. */
18460 cp_parser_type_id_list (cp_parser* parser)
18462 tree types = NULL_TREE;
18469 /* Get the next type-id. */
18470 type = cp_parser_type_id (parser);
18471 /* Parse the optional ellipsis. */
18472 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18474 /* Consume the `...'. */
18475 cp_lexer_consume_token (parser->lexer);
18477 /* Turn the type into a pack expansion expression. */
18478 type = make_pack_expansion (type);
18480 /* Add it to the list. */
18481 types = add_exception_specifier (types, type, /*complain=*/1);
18482 /* Peek at the next token. */
18483 token = cp_lexer_peek_token (parser->lexer);
18484 /* If it is not a `,', we are done. */
18485 if (token->type != CPP_COMMA)
18487 /* Consume the `,'. */
18488 cp_lexer_consume_token (parser->lexer);
18491 return nreverse (types);
18494 /* Parse a try-block.
18497 try compound-statement handler-seq */
18500 cp_parser_try_block (cp_parser* parser)
18504 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18505 try_block = begin_try_block ();
18506 cp_parser_compound_statement (parser, NULL, true, false);
18507 finish_try_block (try_block);
18508 cp_parser_handler_seq (parser);
18509 finish_handler_sequence (try_block);
18514 /* Parse a function-try-block.
18516 function-try-block:
18517 try ctor-initializer [opt] function-body handler-seq */
18520 cp_parser_function_try_block (cp_parser* parser)
18522 tree compound_stmt;
18524 bool ctor_initializer_p;
18526 /* Look for the `try' keyword. */
18527 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18529 /* Let the rest of the front end know where we are. */
18530 try_block = begin_function_try_block (&compound_stmt);
18531 /* Parse the function-body. */
18533 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18534 /* We're done with the `try' part. */
18535 finish_function_try_block (try_block);
18536 /* Parse the handlers. */
18537 cp_parser_handler_seq (parser);
18538 /* We're done with the handlers. */
18539 finish_function_handler_sequence (try_block, compound_stmt);
18541 return ctor_initializer_p;
18544 /* Parse a handler-seq.
18547 handler handler-seq [opt] */
18550 cp_parser_handler_seq (cp_parser* parser)
18556 /* Parse the handler. */
18557 cp_parser_handler (parser);
18558 /* Peek at the next token. */
18559 token = cp_lexer_peek_token (parser->lexer);
18560 /* If it's not `catch' then there are no more handlers. */
18561 if (!cp_parser_is_keyword (token, RID_CATCH))
18566 /* Parse a handler.
18569 catch ( exception-declaration ) compound-statement */
18572 cp_parser_handler (cp_parser* parser)
18577 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18578 handler = begin_handler ();
18579 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18580 declaration = cp_parser_exception_declaration (parser);
18581 finish_handler_parms (declaration, handler);
18582 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18583 cp_parser_compound_statement (parser, NULL, false, false);
18584 finish_handler (handler);
18587 /* Parse an exception-declaration.
18589 exception-declaration:
18590 type-specifier-seq declarator
18591 type-specifier-seq abstract-declarator
18595 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18596 ellipsis variant is used. */
18599 cp_parser_exception_declaration (cp_parser* parser)
18601 cp_decl_specifier_seq type_specifiers;
18602 cp_declarator *declarator;
18603 const char *saved_message;
18605 /* If it's an ellipsis, it's easy to handle. */
18606 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18608 /* Consume the `...' token. */
18609 cp_lexer_consume_token (parser->lexer);
18613 /* Types may not be defined in exception-declarations. */
18614 saved_message = parser->type_definition_forbidden_message;
18615 parser->type_definition_forbidden_message
18616 = G_("types may not be defined in exception-declarations");
18618 /* Parse the type-specifier-seq. */
18619 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18620 /*is_trailing_return=*/false,
18622 /* If it's a `)', then there is no declarator. */
18623 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18626 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18627 /*ctor_dtor_or_conv_p=*/NULL,
18628 /*parenthesized_p=*/NULL,
18629 /*member_p=*/false);
18631 /* Restore the saved message. */
18632 parser->type_definition_forbidden_message = saved_message;
18634 if (!type_specifiers.any_specifiers_p)
18635 return error_mark_node;
18637 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18640 /* Parse a throw-expression.
18643 throw assignment-expression [opt]
18645 Returns a THROW_EXPR representing the throw-expression. */
18648 cp_parser_throw_expression (cp_parser* parser)
18653 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18654 token = cp_lexer_peek_token (parser->lexer);
18655 /* Figure out whether or not there is an assignment-expression
18656 following the "throw" keyword. */
18657 if (token->type == CPP_COMMA
18658 || token->type == CPP_SEMICOLON
18659 || token->type == CPP_CLOSE_PAREN
18660 || token->type == CPP_CLOSE_SQUARE
18661 || token->type == CPP_CLOSE_BRACE
18662 || token->type == CPP_COLON)
18663 expression = NULL_TREE;
18665 expression = cp_parser_assignment_expression (parser,
18666 /*cast_p=*/false, NULL);
18668 return build_throw (expression);
18671 /* GNU Extensions */
18673 /* Parse an (optional) asm-specification.
18676 asm ( string-literal )
18678 If the asm-specification is present, returns a STRING_CST
18679 corresponding to the string-literal. Otherwise, returns
18683 cp_parser_asm_specification_opt (cp_parser* parser)
18686 tree asm_specification;
18688 /* Peek at the next token. */
18689 token = cp_lexer_peek_token (parser->lexer);
18690 /* If the next token isn't the `asm' keyword, then there's no
18691 asm-specification. */
18692 if (!cp_parser_is_keyword (token, RID_ASM))
18695 /* Consume the `asm' token. */
18696 cp_lexer_consume_token (parser->lexer);
18697 /* Look for the `('. */
18698 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18700 /* Look for the string-literal. */
18701 asm_specification = cp_parser_string_literal (parser, false, false);
18703 /* Look for the `)'. */
18704 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18706 return asm_specification;
18709 /* Parse an asm-operand-list.
18713 asm-operand-list , asm-operand
18716 string-literal ( expression )
18717 [ string-literal ] string-literal ( expression )
18719 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18720 each node is the expression. The TREE_PURPOSE is itself a
18721 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18722 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18723 is a STRING_CST for the string literal before the parenthesis. Returns
18724 ERROR_MARK_NODE if any of the operands are invalid. */
18727 cp_parser_asm_operand_list (cp_parser* parser)
18729 tree asm_operands = NULL_TREE;
18730 bool invalid_operands = false;
18734 tree string_literal;
18738 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18740 /* Consume the `[' token. */
18741 cp_lexer_consume_token (parser->lexer);
18742 /* Read the operand name. */
18743 name = cp_parser_identifier (parser);
18744 if (name != error_mark_node)
18745 name = build_string (IDENTIFIER_LENGTH (name),
18746 IDENTIFIER_POINTER (name));
18747 /* Look for the closing `]'. */
18748 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18752 /* Look for the string-literal. */
18753 string_literal = cp_parser_string_literal (parser, false, false);
18755 /* Look for the `('. */
18756 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18757 /* Parse the expression. */
18758 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18759 /* Look for the `)'. */
18760 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18762 if (name == error_mark_node
18763 || string_literal == error_mark_node
18764 || expression == error_mark_node)
18765 invalid_operands = true;
18767 /* Add this operand to the list. */
18768 asm_operands = tree_cons (build_tree_list (name, string_literal),
18771 /* If the next token is not a `,', there are no more
18773 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18775 /* Consume the `,'. */
18776 cp_lexer_consume_token (parser->lexer);
18779 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18782 /* Parse an asm-clobber-list.
18786 asm-clobber-list , string-literal
18788 Returns a TREE_LIST, indicating the clobbers in the order that they
18789 appeared. The TREE_VALUE of each node is a STRING_CST. */
18792 cp_parser_asm_clobber_list (cp_parser* parser)
18794 tree clobbers = NULL_TREE;
18798 tree string_literal;
18800 /* Look for the string literal. */
18801 string_literal = cp_parser_string_literal (parser, false, false);
18802 /* Add it to the list. */
18803 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18804 /* If the next token is not a `,', then the list is
18806 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18808 /* Consume the `,' token. */
18809 cp_lexer_consume_token (parser->lexer);
18815 /* Parse an asm-label-list.
18819 asm-label-list , identifier
18821 Returns a TREE_LIST, indicating the labels in the order that they
18822 appeared. The TREE_VALUE of each node is a label. */
18825 cp_parser_asm_label_list (cp_parser* parser)
18827 tree labels = NULL_TREE;
18831 tree identifier, label, name;
18833 /* Look for the identifier. */
18834 identifier = cp_parser_identifier (parser);
18835 if (!error_operand_p (identifier))
18837 label = lookup_label (identifier);
18838 if (TREE_CODE (label) == LABEL_DECL)
18840 TREE_USED (label) = 1;
18841 check_goto (label);
18842 name = build_string (IDENTIFIER_LENGTH (identifier),
18843 IDENTIFIER_POINTER (identifier));
18844 labels = tree_cons (name, label, labels);
18847 /* If the next token is not a `,', then the list is
18849 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18851 /* Consume the `,' token. */
18852 cp_lexer_consume_token (parser->lexer);
18855 return nreverse (labels);
18858 /* Parse an (optional) series of attributes.
18861 attributes attribute
18864 __attribute__ (( attribute-list [opt] ))
18866 The return value is as for cp_parser_attribute_list. */
18869 cp_parser_attributes_opt (cp_parser* parser)
18871 tree attributes = NULL_TREE;
18876 tree attribute_list;
18878 /* Peek at the next token. */
18879 token = cp_lexer_peek_token (parser->lexer);
18880 /* If it's not `__attribute__', then we're done. */
18881 if (token->keyword != RID_ATTRIBUTE)
18884 /* Consume the `__attribute__' keyword. */
18885 cp_lexer_consume_token (parser->lexer);
18886 /* Look for the two `(' tokens. */
18887 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18888 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18890 /* Peek at the next token. */
18891 token = cp_lexer_peek_token (parser->lexer);
18892 if (token->type != CPP_CLOSE_PAREN)
18893 /* Parse the attribute-list. */
18894 attribute_list = cp_parser_attribute_list (parser);
18896 /* If the next token is a `)', then there is no attribute
18898 attribute_list = NULL;
18900 /* Look for the two `)' tokens. */
18901 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18902 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18904 /* Add these new attributes to the list. */
18905 attributes = chainon (attributes, attribute_list);
18911 /* Parse an attribute-list.
18915 attribute-list , attribute
18919 identifier ( identifier )
18920 identifier ( identifier , expression-list )
18921 identifier ( expression-list )
18923 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18924 to an attribute. The TREE_PURPOSE of each node is the identifier
18925 indicating which attribute is in use. The TREE_VALUE represents
18926 the arguments, if any. */
18929 cp_parser_attribute_list (cp_parser* parser)
18931 tree attribute_list = NULL_TREE;
18932 bool save_translate_strings_p = parser->translate_strings_p;
18934 parser->translate_strings_p = false;
18941 /* Look for the identifier. We also allow keywords here; for
18942 example `__attribute__ ((const))' is legal. */
18943 token = cp_lexer_peek_token (parser->lexer);
18944 if (token->type == CPP_NAME
18945 || token->type == CPP_KEYWORD)
18947 tree arguments = NULL_TREE;
18949 /* Consume the token. */
18950 token = cp_lexer_consume_token (parser->lexer);
18952 /* Save away the identifier that indicates which attribute
18954 identifier = (token->type == CPP_KEYWORD)
18955 /* For keywords, use the canonical spelling, not the
18956 parsed identifier. */
18957 ? ridpointers[(int) token->keyword]
18960 attribute = build_tree_list (identifier, NULL_TREE);
18962 /* Peek at the next token. */
18963 token = cp_lexer_peek_token (parser->lexer);
18964 /* If it's an `(', then parse the attribute arguments. */
18965 if (token->type == CPP_OPEN_PAREN)
18968 int attr_flag = (attribute_takes_identifier_p (identifier)
18969 ? id_attr : normal_attr);
18970 vec = cp_parser_parenthesized_expression_list
18971 (parser, attr_flag, /*cast_p=*/false,
18972 /*allow_expansion_p=*/false,
18973 /*non_constant_p=*/NULL);
18975 arguments = error_mark_node;
18978 arguments = build_tree_list_vec (vec);
18979 release_tree_vector (vec);
18981 /* Save the arguments away. */
18982 TREE_VALUE (attribute) = arguments;
18985 if (arguments != error_mark_node)
18987 /* Add this attribute to the list. */
18988 TREE_CHAIN (attribute) = attribute_list;
18989 attribute_list = attribute;
18992 token = cp_lexer_peek_token (parser->lexer);
18994 /* Now, look for more attributes. If the next token isn't a
18995 `,', we're done. */
18996 if (token->type != CPP_COMMA)
18999 /* Consume the comma and keep going. */
19000 cp_lexer_consume_token (parser->lexer);
19002 parser->translate_strings_p = save_translate_strings_p;
19004 /* We built up the list in reverse order. */
19005 return nreverse (attribute_list);
19008 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
19009 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
19010 current value of the PEDANTIC flag, regardless of whether or not
19011 the `__extension__' keyword is present. The caller is responsible
19012 for restoring the value of the PEDANTIC flag. */
19015 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19017 /* Save the old value of the PEDANTIC flag. */
19018 *saved_pedantic = pedantic;
19020 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19022 /* Consume the `__extension__' token. */
19023 cp_lexer_consume_token (parser->lexer);
19024 /* We're not being pedantic while the `__extension__' keyword is
19034 /* Parse a label declaration.
19037 __label__ label-declarator-seq ;
19039 label-declarator-seq:
19040 identifier , label-declarator-seq
19044 cp_parser_label_declaration (cp_parser* parser)
19046 /* Look for the `__label__' keyword. */
19047 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19053 /* Look for an identifier. */
19054 identifier = cp_parser_identifier (parser);
19055 /* If we failed, stop. */
19056 if (identifier == error_mark_node)
19058 /* Declare it as a label. */
19059 finish_label_decl (identifier);
19060 /* If the next token is a `;', stop. */
19061 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19063 /* Look for the `,' separating the label declarations. */
19064 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19067 /* Look for the final `;'. */
19068 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19071 /* Support Functions */
19073 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19074 NAME should have one of the representations used for an
19075 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19076 is returned. If PARSER->SCOPE is a dependent type, then a
19077 SCOPE_REF is returned.
19079 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19080 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19081 was formed. Abstractly, such entities should not be passed to this
19082 function, because they do not need to be looked up, but it is
19083 simpler to check for this special case here, rather than at the
19086 In cases not explicitly covered above, this function returns a
19087 DECL, OVERLOAD, or baselink representing the result of the lookup.
19088 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19091 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19092 (e.g., "struct") that was used. In that case bindings that do not
19093 refer to types are ignored.
19095 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19098 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19101 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19104 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19105 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19106 NULL_TREE otherwise. */
19109 cp_parser_lookup_name (cp_parser *parser, tree name,
19110 enum tag_types tag_type,
19113 bool check_dependency,
19114 tree *ambiguous_decls,
19115 location_t name_location)
19119 tree object_type = parser->context->object_type;
19121 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19122 flags |= LOOKUP_COMPLAIN;
19124 /* Assume that the lookup will be unambiguous. */
19125 if (ambiguous_decls)
19126 *ambiguous_decls = NULL_TREE;
19128 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19129 no longer valid. Note that if we are parsing tentatively, and
19130 the parse fails, OBJECT_TYPE will be automatically restored. */
19131 parser->context->object_type = NULL_TREE;
19133 if (name == error_mark_node)
19134 return error_mark_node;
19136 /* A template-id has already been resolved; there is no lookup to
19138 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19140 if (BASELINK_P (name))
19142 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19143 == TEMPLATE_ID_EXPR);
19147 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19148 it should already have been checked to make sure that the name
19149 used matches the type being destroyed. */
19150 if (TREE_CODE (name) == BIT_NOT_EXPR)
19154 /* Figure out to which type this destructor applies. */
19156 type = parser->scope;
19157 else if (object_type)
19158 type = object_type;
19160 type = current_class_type;
19161 /* If that's not a class type, there is no destructor. */
19162 if (!type || !CLASS_TYPE_P (type))
19163 return error_mark_node;
19164 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19165 lazily_declare_fn (sfk_destructor, type);
19166 if (!CLASSTYPE_DESTRUCTORS (type))
19167 return error_mark_node;
19168 /* If it was a class type, return the destructor. */
19169 return CLASSTYPE_DESTRUCTORS (type);
19172 /* By this point, the NAME should be an ordinary identifier. If
19173 the id-expression was a qualified name, the qualifying scope is
19174 stored in PARSER->SCOPE at this point. */
19175 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19177 /* Perform the lookup. */
19182 if (parser->scope == error_mark_node)
19183 return error_mark_node;
19185 /* If the SCOPE is dependent, the lookup must be deferred until
19186 the template is instantiated -- unless we are explicitly
19187 looking up names in uninstantiated templates. Even then, we
19188 cannot look up the name if the scope is not a class type; it
19189 might, for example, be a template type parameter. */
19190 dependent_p = (TYPE_P (parser->scope)
19191 && dependent_scope_p (parser->scope));
19192 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19194 /* Defer lookup. */
19195 decl = error_mark_node;
19198 tree pushed_scope = NULL_TREE;
19200 /* If PARSER->SCOPE is a dependent type, then it must be a
19201 class type, and we must not be checking dependencies;
19202 otherwise, we would have processed this lookup above. So
19203 that PARSER->SCOPE is not considered a dependent base by
19204 lookup_member, we must enter the scope here. */
19206 pushed_scope = push_scope (parser->scope);
19208 /* If the PARSER->SCOPE is a template specialization, it
19209 may be instantiated during name lookup. In that case,
19210 errors may be issued. Even if we rollback the current
19211 tentative parse, those errors are valid. */
19212 decl = lookup_qualified_name (parser->scope, name,
19213 tag_type != none_type,
19214 /*complain=*/true);
19216 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19217 lookup result and the nested-name-specifier nominates a class C:
19218 * if the name specified after the nested-name-specifier, when
19219 looked up in C, is the injected-class-name of C (Clause 9), or
19220 * if the name specified after the nested-name-specifier is the
19221 same as the identifier or the simple-template-id's template-
19222 name in the last component of the nested-name-specifier,
19223 the name is instead considered to name the constructor of
19224 class C. [ Note: for example, the constructor is not an
19225 acceptable lookup result in an elaborated-type-specifier so
19226 the constructor would not be used in place of the
19227 injected-class-name. --end note ] Such a constructor name
19228 shall be used only in the declarator-id of a declaration that
19229 names a constructor or in a using-declaration. */
19230 if (tag_type == none_type
19231 && DECL_SELF_REFERENCE_P (decl)
19232 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19233 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19234 tag_type != none_type,
19235 /*complain=*/true);
19237 /* If we have a single function from a using decl, pull it out. */
19238 if (TREE_CODE (decl) == OVERLOAD
19239 && !really_overloaded_fn (decl))
19240 decl = OVL_FUNCTION (decl);
19243 pop_scope (pushed_scope);
19246 /* If the scope is a dependent type and either we deferred lookup or
19247 we did lookup but didn't find the name, rememeber the name. */
19248 if (decl == error_mark_node && TYPE_P (parser->scope)
19249 && dependent_type_p (parser->scope))
19255 /* The resolution to Core Issue 180 says that `struct
19256 A::B' should be considered a type-name, even if `A'
19258 type = make_typename_type (parser->scope, name, tag_type,
19259 /*complain=*/tf_error);
19260 decl = TYPE_NAME (type);
19262 else if (is_template
19263 && (cp_parser_next_token_ends_template_argument_p (parser)
19264 || cp_lexer_next_token_is (parser->lexer,
19266 decl = make_unbound_class_template (parser->scope,
19268 /*complain=*/tf_error);
19270 decl = build_qualified_name (/*type=*/NULL_TREE,
19271 parser->scope, name,
19274 parser->qualifying_scope = parser->scope;
19275 parser->object_scope = NULL_TREE;
19277 else if (object_type)
19279 tree object_decl = NULL_TREE;
19280 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19281 OBJECT_TYPE is not a class. */
19282 if (CLASS_TYPE_P (object_type))
19283 /* If the OBJECT_TYPE is a template specialization, it may
19284 be instantiated during name lookup. In that case, errors
19285 may be issued. Even if we rollback the current tentative
19286 parse, those errors are valid. */
19287 object_decl = lookup_member (object_type,
19290 tag_type != none_type);
19291 /* Look it up in the enclosing context, too. */
19292 decl = lookup_name_real (name, tag_type != none_type,
19294 /*block_p=*/true, is_namespace, flags);
19295 parser->object_scope = object_type;
19296 parser->qualifying_scope = NULL_TREE;
19298 decl = object_decl;
19302 decl = lookup_name_real (name, tag_type != none_type,
19304 /*block_p=*/true, is_namespace, flags);
19305 parser->qualifying_scope = NULL_TREE;
19306 parser->object_scope = NULL_TREE;
19309 /* If the lookup failed, let our caller know. */
19310 if (!decl || decl == error_mark_node)
19311 return error_mark_node;
19313 /* Pull out the template from an injected-class-name (or multiple). */
19315 decl = maybe_get_template_decl_from_type_decl (decl);
19317 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19318 if (TREE_CODE (decl) == TREE_LIST)
19320 if (ambiguous_decls)
19321 *ambiguous_decls = decl;
19322 /* The error message we have to print is too complicated for
19323 cp_parser_error, so we incorporate its actions directly. */
19324 if (!cp_parser_simulate_error (parser))
19326 error_at (name_location, "reference to %qD is ambiguous",
19328 print_candidates (decl);
19330 return error_mark_node;
19333 gcc_assert (DECL_P (decl)
19334 || TREE_CODE (decl) == OVERLOAD
19335 || TREE_CODE (decl) == SCOPE_REF
19336 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19337 || BASELINK_P (decl));
19339 /* If we have resolved the name of a member declaration, check to
19340 see if the declaration is accessible. When the name resolves to
19341 set of overloaded functions, accessibility is checked when
19342 overload resolution is done.
19344 During an explicit instantiation, access is not checked at all,
19345 as per [temp.explicit]. */
19347 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19352 /* Like cp_parser_lookup_name, but for use in the typical case where
19353 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19354 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19357 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19359 return cp_parser_lookup_name (parser, name,
19361 /*is_template=*/false,
19362 /*is_namespace=*/false,
19363 /*check_dependency=*/true,
19364 /*ambiguous_decls=*/NULL,
19368 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19369 the current context, return the TYPE_DECL. If TAG_NAME_P is
19370 true, the DECL indicates the class being defined in a class-head,
19371 or declared in an elaborated-type-specifier.
19373 Otherwise, return DECL. */
19376 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19378 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19379 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19382 template <typename T> struct B;
19385 template <typename T> struct A::B {};
19387 Similarly, in an elaborated-type-specifier:
19389 namespace N { struct X{}; }
19392 template <typename T> friend struct N::X;
19395 However, if the DECL refers to a class type, and we are in
19396 the scope of the class, then the name lookup automatically
19397 finds the TYPE_DECL created by build_self_reference rather
19398 than a TEMPLATE_DECL. For example, in:
19400 template <class T> struct S {
19404 there is no need to handle such case. */
19406 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19407 return DECL_TEMPLATE_RESULT (decl);
19412 /* If too many, or too few, template-parameter lists apply to the
19413 declarator, issue an error message. Returns TRUE if all went well,
19414 and FALSE otherwise. */
19417 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19418 cp_declarator *declarator,
19419 location_t declarator_location)
19421 unsigned num_templates;
19423 /* We haven't seen any classes that involve template parameters yet. */
19426 switch (declarator->kind)
19429 if (declarator->u.id.qualifying_scope)
19433 scope = declarator->u.id.qualifying_scope;
19435 while (scope && CLASS_TYPE_P (scope))
19437 /* You're supposed to have one `template <...>'
19438 for every template class, but you don't need one
19439 for a full specialization. For example:
19441 template <class T> struct S{};
19442 template <> struct S<int> { void f(); };
19443 void S<int>::f () {}
19445 is correct; there shouldn't be a `template <>' for
19446 the definition of `S<int>::f'. */
19447 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19448 /* If SCOPE does not have template information of any
19449 kind, then it is not a template, nor is it nested
19450 within a template. */
19452 if (explicit_class_specialization_p (scope))
19454 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19457 scope = TYPE_CONTEXT (scope);
19460 else if (TREE_CODE (declarator->u.id.unqualified_name)
19461 == TEMPLATE_ID_EXPR)
19462 /* If the DECLARATOR has the form `X<y>' then it uses one
19463 additional level of template parameters. */
19466 return cp_parser_check_template_parameters
19467 (parser, num_templates, declarator_location, declarator);
19473 case cdk_reference:
19475 return (cp_parser_check_declarator_template_parameters
19476 (parser, declarator->declarator, declarator_location));
19482 gcc_unreachable ();
19487 /* NUM_TEMPLATES were used in the current declaration. If that is
19488 invalid, return FALSE and issue an error messages. Otherwise,
19489 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19490 declarator and we can print more accurate diagnostics. */
19493 cp_parser_check_template_parameters (cp_parser* parser,
19494 unsigned num_templates,
19495 location_t location,
19496 cp_declarator *declarator)
19498 /* If there are the same number of template classes and parameter
19499 lists, that's OK. */
19500 if (parser->num_template_parameter_lists == num_templates)
19502 /* If there are more, but only one more, then we are referring to a
19503 member template. That's OK too. */
19504 if (parser->num_template_parameter_lists == num_templates + 1)
19506 /* If there are more template classes than parameter lists, we have
19509 template <class T> void S<T>::R<T>::f (); */
19510 if (parser->num_template_parameter_lists < num_templates)
19512 if (declarator && !current_function_decl)
19513 error_at (location, "specializing member %<%T::%E%> "
19514 "requires %<template<>%> syntax",
19515 declarator->u.id.qualifying_scope,
19516 declarator->u.id.unqualified_name);
19517 else if (declarator)
19518 error_at (location, "invalid declaration of %<%T::%E%>",
19519 declarator->u.id.qualifying_scope,
19520 declarator->u.id.unqualified_name);
19522 error_at (location, "too few template-parameter-lists");
19525 /* Otherwise, there are too many template parameter lists. We have
19528 template <class T> template <class U> void S::f(); */
19529 error_at (location, "too many template-parameter-lists");
19533 /* Parse an optional `::' token indicating that the following name is
19534 from the global namespace. If so, PARSER->SCOPE is set to the
19535 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19536 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19537 Returns the new value of PARSER->SCOPE, if the `::' token is
19538 present, and NULL_TREE otherwise. */
19541 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19545 /* Peek at the next token. */
19546 token = cp_lexer_peek_token (parser->lexer);
19547 /* If we're looking at a `::' token then we're starting from the
19548 global namespace, not our current location. */
19549 if (token->type == CPP_SCOPE)
19551 /* Consume the `::' token. */
19552 cp_lexer_consume_token (parser->lexer);
19553 /* Set the SCOPE so that we know where to start the lookup. */
19554 parser->scope = global_namespace;
19555 parser->qualifying_scope = global_namespace;
19556 parser->object_scope = NULL_TREE;
19558 return parser->scope;
19560 else if (!current_scope_valid_p)
19562 parser->scope = NULL_TREE;
19563 parser->qualifying_scope = NULL_TREE;
19564 parser->object_scope = NULL_TREE;
19570 /* Returns TRUE if the upcoming token sequence is the start of a
19571 constructor declarator. If FRIEND_P is true, the declarator is
19572 preceded by the `friend' specifier. */
19575 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19577 bool constructor_p;
19578 tree nested_name_specifier;
19579 cp_token *next_token;
19581 /* The common case is that this is not a constructor declarator, so
19582 try to avoid doing lots of work if at all possible. It's not
19583 valid declare a constructor at function scope. */
19584 if (parser->in_function_body)
19586 /* And only certain tokens can begin a constructor declarator. */
19587 next_token = cp_lexer_peek_token (parser->lexer);
19588 if (next_token->type != CPP_NAME
19589 && next_token->type != CPP_SCOPE
19590 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19591 && next_token->type != CPP_TEMPLATE_ID)
19594 /* Parse tentatively; we are going to roll back all of the tokens
19596 cp_parser_parse_tentatively (parser);
19597 /* Assume that we are looking at a constructor declarator. */
19598 constructor_p = true;
19600 /* Look for the optional `::' operator. */
19601 cp_parser_global_scope_opt (parser,
19602 /*current_scope_valid_p=*/false);
19603 /* Look for the nested-name-specifier. */
19604 nested_name_specifier
19605 = (cp_parser_nested_name_specifier_opt (parser,
19606 /*typename_keyword_p=*/false,
19607 /*check_dependency_p=*/false,
19609 /*is_declaration=*/false));
19610 /* Outside of a class-specifier, there must be a
19611 nested-name-specifier. */
19612 if (!nested_name_specifier &&
19613 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19615 constructor_p = false;
19616 else if (nested_name_specifier == error_mark_node)
19617 constructor_p = false;
19619 /* If we have a class scope, this is easy; DR 147 says that S::S always
19620 names the constructor, and no other qualified name could. */
19621 if (constructor_p && nested_name_specifier
19622 && CLASS_TYPE_P (nested_name_specifier))
19624 tree id = cp_parser_unqualified_id (parser,
19625 /*template_keyword_p=*/false,
19626 /*check_dependency_p=*/false,
19627 /*declarator_p=*/true,
19628 /*optional_p=*/false);
19629 if (is_overloaded_fn (id))
19630 id = DECL_NAME (get_first_fn (id));
19631 if (!constructor_name_p (id, nested_name_specifier))
19632 constructor_p = false;
19634 /* If we still think that this might be a constructor-declarator,
19635 look for a class-name. */
19636 else if (constructor_p)
19640 template <typename T> struct S {
19644 we must recognize that the nested `S' names a class. */
19646 type_decl = cp_parser_class_name (parser,
19647 /*typename_keyword_p=*/false,
19648 /*template_keyword_p=*/false,
19650 /*check_dependency_p=*/false,
19651 /*class_head_p=*/false,
19652 /*is_declaration=*/false);
19653 /* If there was no class-name, then this is not a constructor. */
19654 constructor_p = !cp_parser_error_occurred (parser);
19656 /* If we're still considering a constructor, we have to see a `(',
19657 to begin the parameter-declaration-clause, followed by either a
19658 `)', an `...', or a decl-specifier. We need to check for a
19659 type-specifier to avoid being fooled into thinking that:
19663 is a constructor. (It is actually a function named `f' that
19664 takes one parameter (of type `int') and returns a value of type
19667 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19668 constructor_p = false;
19671 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19672 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19673 /* A parameter declaration begins with a decl-specifier,
19674 which is either the "attribute" keyword, a storage class
19675 specifier, or (usually) a type-specifier. */
19676 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19679 tree pushed_scope = NULL_TREE;
19680 unsigned saved_num_template_parameter_lists;
19682 /* Names appearing in the type-specifier should be looked up
19683 in the scope of the class. */
19684 if (current_class_type)
19688 type = TREE_TYPE (type_decl);
19689 if (TREE_CODE (type) == TYPENAME_TYPE)
19691 type = resolve_typename_type (type,
19692 /*only_current_p=*/false);
19693 if (TREE_CODE (type) == TYPENAME_TYPE)
19695 cp_parser_abort_tentative_parse (parser);
19699 pushed_scope = push_scope (type);
19702 /* Inside the constructor parameter list, surrounding
19703 template-parameter-lists do not apply. */
19704 saved_num_template_parameter_lists
19705 = parser->num_template_parameter_lists;
19706 parser->num_template_parameter_lists = 0;
19708 /* Look for the type-specifier. */
19709 cp_parser_type_specifier (parser,
19710 CP_PARSER_FLAGS_NONE,
19711 /*decl_specs=*/NULL,
19712 /*is_declarator=*/true,
19713 /*declares_class_or_enum=*/NULL,
19714 /*is_cv_qualifier=*/NULL);
19716 parser->num_template_parameter_lists
19717 = saved_num_template_parameter_lists;
19719 /* Leave the scope of the class. */
19721 pop_scope (pushed_scope);
19723 constructor_p = !cp_parser_error_occurred (parser);
19727 /* We did not really want to consume any tokens. */
19728 cp_parser_abort_tentative_parse (parser);
19730 return constructor_p;
19733 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19734 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19735 they must be performed once we are in the scope of the function.
19737 Returns the function defined. */
19740 cp_parser_function_definition_from_specifiers_and_declarator
19741 (cp_parser* parser,
19742 cp_decl_specifier_seq *decl_specifiers,
19744 const cp_declarator *declarator)
19749 /* Begin the function-definition. */
19750 success_p = start_function (decl_specifiers, declarator, attributes);
19752 /* The things we're about to see are not directly qualified by any
19753 template headers we've seen thus far. */
19754 reset_specialization ();
19756 /* If there were names looked up in the decl-specifier-seq that we
19757 did not check, check them now. We must wait until we are in the
19758 scope of the function to perform the checks, since the function
19759 might be a friend. */
19760 perform_deferred_access_checks ();
19764 /* Skip the entire function. */
19765 cp_parser_skip_to_end_of_block_or_statement (parser);
19766 fn = error_mark_node;
19768 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19770 /* Seen already, skip it. An error message has already been output. */
19771 cp_parser_skip_to_end_of_block_or_statement (parser);
19772 fn = current_function_decl;
19773 current_function_decl = NULL_TREE;
19774 /* If this is a function from a class, pop the nested class. */
19775 if (current_class_name)
19776 pop_nested_class ();
19781 if (DECL_DECLARED_INLINE_P (current_function_decl))
19782 tv = TV_PARSE_INLINE;
19784 tv = TV_PARSE_FUNC;
19786 fn = cp_parser_function_definition_after_declarator (parser,
19787 /*inline_p=*/false);
19794 /* Parse the part of a function-definition that follows the
19795 declarator. INLINE_P is TRUE iff this function is an inline
19796 function defined within a class-specifier.
19798 Returns the function defined. */
19801 cp_parser_function_definition_after_declarator (cp_parser* parser,
19805 bool ctor_initializer_p = false;
19806 bool saved_in_unbraced_linkage_specification_p;
19807 bool saved_in_function_body;
19808 unsigned saved_num_template_parameter_lists;
19811 saved_in_function_body = parser->in_function_body;
19812 parser->in_function_body = true;
19813 /* If the next token is `return', then the code may be trying to
19814 make use of the "named return value" extension that G++ used to
19816 token = cp_lexer_peek_token (parser->lexer);
19817 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19819 /* Consume the `return' keyword. */
19820 cp_lexer_consume_token (parser->lexer);
19821 /* Look for the identifier that indicates what value is to be
19823 cp_parser_identifier (parser);
19824 /* Issue an error message. */
19825 error_at (token->location,
19826 "named return values are no longer supported");
19827 /* Skip tokens until we reach the start of the function body. */
19830 cp_token *token = cp_lexer_peek_token (parser->lexer);
19831 if (token->type == CPP_OPEN_BRACE
19832 || token->type == CPP_EOF
19833 || token->type == CPP_PRAGMA_EOL)
19835 cp_lexer_consume_token (parser->lexer);
19838 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19839 anything declared inside `f'. */
19840 saved_in_unbraced_linkage_specification_p
19841 = parser->in_unbraced_linkage_specification_p;
19842 parser->in_unbraced_linkage_specification_p = false;
19843 /* Inside the function, surrounding template-parameter-lists do not
19845 saved_num_template_parameter_lists
19846 = parser->num_template_parameter_lists;
19847 parser->num_template_parameter_lists = 0;
19849 start_lambda_scope (current_function_decl);
19851 /* If the next token is `try', then we are looking at a
19852 function-try-block. */
19853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19854 ctor_initializer_p = cp_parser_function_try_block (parser);
19855 /* A function-try-block includes the function-body, so we only do
19856 this next part if we're not processing a function-try-block. */
19859 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19861 finish_lambda_scope ();
19863 /* Finish the function. */
19864 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19865 (inline_p ? 2 : 0));
19866 /* Generate code for it, if necessary. */
19867 expand_or_defer_fn (fn);
19868 /* Restore the saved values. */
19869 parser->in_unbraced_linkage_specification_p
19870 = saved_in_unbraced_linkage_specification_p;
19871 parser->num_template_parameter_lists
19872 = saved_num_template_parameter_lists;
19873 parser->in_function_body = saved_in_function_body;
19878 /* Parse a template-declaration, assuming that the `export' (and
19879 `extern') keywords, if present, has already been scanned. MEMBER_P
19880 is as for cp_parser_template_declaration. */
19883 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19885 tree decl = NULL_TREE;
19886 VEC (deferred_access_check,gc) *checks;
19887 tree parameter_list;
19888 bool friend_p = false;
19889 bool need_lang_pop;
19892 /* Look for the `template' keyword. */
19893 token = cp_lexer_peek_token (parser->lexer);
19894 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19898 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19900 if (at_class_scope_p () && current_function_decl)
19902 /* 14.5.2.2 [temp.mem]
19904 A local class shall not have member templates. */
19905 error_at (token->location,
19906 "invalid declaration of member template in local class");
19907 cp_parser_skip_to_end_of_block_or_statement (parser);
19912 A template ... shall not have C linkage. */
19913 if (current_lang_name == lang_name_c)
19915 error_at (token->location, "template with C linkage");
19916 /* Give it C++ linkage to avoid confusing other parts of the
19918 push_lang_context (lang_name_cplusplus);
19919 need_lang_pop = true;
19922 need_lang_pop = false;
19924 /* We cannot perform access checks on the template parameter
19925 declarations until we know what is being declared, just as we
19926 cannot check the decl-specifier list. */
19927 push_deferring_access_checks (dk_deferred);
19929 /* If the next token is `>', then we have an invalid
19930 specialization. Rather than complain about an invalid template
19931 parameter, issue an error message here. */
19932 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19934 cp_parser_error (parser, "invalid explicit specialization");
19935 begin_specialization ();
19936 parameter_list = NULL_TREE;
19940 /* Parse the template parameters. */
19941 parameter_list = cp_parser_template_parameter_list (parser);
19942 fixup_template_parms ();
19945 /* Get the deferred access checks from the parameter list. These
19946 will be checked once we know what is being declared, as for a
19947 member template the checks must be performed in the scope of the
19948 class containing the member. */
19949 checks = get_deferred_access_checks ();
19951 /* Look for the `>'. */
19952 cp_parser_skip_to_end_of_template_parameter_list (parser);
19953 /* We just processed one more parameter list. */
19954 ++parser->num_template_parameter_lists;
19955 /* If the next token is `template', there are more template
19957 if (cp_lexer_next_token_is_keyword (parser->lexer,
19959 cp_parser_template_declaration_after_export (parser, member_p);
19962 /* There are no access checks when parsing a template, as we do not
19963 know if a specialization will be a friend. */
19964 push_deferring_access_checks (dk_no_check);
19965 token = cp_lexer_peek_token (parser->lexer);
19966 decl = cp_parser_single_declaration (parser,
19969 /*explicit_specialization_p=*/false,
19971 pop_deferring_access_checks ();
19973 /* If this is a member template declaration, let the front
19975 if (member_p && !friend_p && decl)
19977 if (TREE_CODE (decl) == TYPE_DECL)
19978 cp_parser_check_access_in_redeclaration (decl, token->location);
19980 decl = finish_member_template_decl (decl);
19982 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19983 make_friend_class (current_class_type, TREE_TYPE (decl),
19984 /*complain=*/true);
19986 /* We are done with the current parameter list. */
19987 --parser->num_template_parameter_lists;
19989 pop_deferring_access_checks ();
19992 finish_template_decl (parameter_list);
19994 /* Register member declarations. */
19995 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19996 finish_member_declaration (decl);
19997 /* For the erroneous case of a template with C linkage, we pushed an
19998 implicit C++ linkage scope; exit that scope now. */
20000 pop_lang_context ();
20001 /* If DECL is a function template, we must return to parse it later.
20002 (Even though there is no definition, there might be default
20003 arguments that need handling.) */
20004 if (member_p && decl
20005 && (TREE_CODE (decl) == FUNCTION_DECL
20006 || DECL_FUNCTION_TEMPLATE_P (decl)))
20007 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20010 /* Perform the deferred access checks from a template-parameter-list.
20011 CHECKS is a TREE_LIST of access checks, as returned by
20012 get_deferred_access_checks. */
20015 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20017 ++processing_template_parmlist;
20018 perform_access_checks (checks);
20019 --processing_template_parmlist;
20022 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20023 `function-definition' sequence. MEMBER_P is true, this declaration
20024 appears in a class scope.
20026 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
20027 *FRIEND_P is set to TRUE iff the declaration is a friend. */
20030 cp_parser_single_declaration (cp_parser* parser,
20031 VEC (deferred_access_check,gc)* checks,
20033 bool explicit_specialization_p,
20036 int declares_class_or_enum;
20037 tree decl = NULL_TREE;
20038 cp_decl_specifier_seq decl_specifiers;
20039 bool function_definition_p = false;
20040 cp_token *decl_spec_token_start;
20042 /* This function is only used when processing a template
20044 gcc_assert (innermost_scope_kind () == sk_template_parms
20045 || innermost_scope_kind () == sk_template_spec);
20047 /* Defer access checks until we know what is being declared. */
20048 push_deferring_access_checks (dk_deferred);
20050 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20052 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20053 cp_parser_decl_specifier_seq (parser,
20054 CP_PARSER_FLAGS_OPTIONAL,
20056 &declares_class_or_enum);
20058 *friend_p = cp_parser_friend_p (&decl_specifiers);
20060 /* There are no template typedefs. */
20061 if (decl_specifiers.specs[(int) ds_typedef])
20063 error_at (decl_spec_token_start->location,
20064 "template declaration of %<typedef%>");
20065 decl = error_mark_node;
20068 /* Gather up the access checks that occurred the
20069 decl-specifier-seq. */
20070 stop_deferring_access_checks ();
20072 /* Check for the declaration of a template class. */
20073 if (declares_class_or_enum)
20075 if (cp_parser_declares_only_class_p (parser))
20077 decl = shadow_tag (&decl_specifiers);
20082 friend template <typename T> struct A<T>::B;
20085 A<T>::B will be represented by a TYPENAME_TYPE, and
20086 therefore not recognized by shadow_tag. */
20087 if (friend_p && *friend_p
20089 && decl_specifiers.type
20090 && TYPE_P (decl_specifiers.type))
20091 decl = decl_specifiers.type;
20093 if (decl && decl != error_mark_node)
20094 decl = TYPE_NAME (decl);
20096 decl = error_mark_node;
20098 /* Perform access checks for template parameters. */
20099 cp_parser_perform_template_parameter_access_checks (checks);
20103 /* Complain about missing 'typename' or other invalid type names. */
20104 if (!decl_specifiers.any_type_specifiers_p
20105 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20107 /* cp_parser_parse_and_diagnose_invalid_type_name calls
20108 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20109 the rest of this declaration. */
20110 decl = error_mark_node;
20114 /* If it's not a template class, try for a template function. If
20115 the next token is a `;', then this declaration does not declare
20116 anything. But, if there were errors in the decl-specifiers, then
20117 the error might well have come from an attempted class-specifier.
20118 In that case, there's no need to warn about a missing declarator. */
20120 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20121 || decl_specifiers.type != error_mark_node))
20123 decl = cp_parser_init_declarator (parser,
20126 /*function_definition_allowed_p=*/true,
20128 declares_class_or_enum,
20129 &function_definition_p,
20132 /* 7.1.1-1 [dcl.stc]
20134 A storage-class-specifier shall not be specified in an explicit
20135 specialization... */
20137 && explicit_specialization_p
20138 && decl_specifiers.storage_class != sc_none)
20140 error_at (decl_spec_token_start->location,
20141 "explicit template specialization cannot have a storage class");
20142 decl = error_mark_node;
20146 /* Look for a trailing `;' after the declaration. */
20147 if (!function_definition_p
20148 && (decl == error_mark_node
20149 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20150 cp_parser_skip_to_end_of_block_or_statement (parser);
20153 pop_deferring_access_checks ();
20155 /* Clear any current qualification; whatever comes next is the start
20156 of something new. */
20157 parser->scope = NULL_TREE;
20158 parser->qualifying_scope = NULL_TREE;
20159 parser->object_scope = NULL_TREE;
20164 /* Parse a cast-expression that is not the operand of a unary "&". */
20167 cp_parser_simple_cast_expression (cp_parser *parser)
20169 return cp_parser_cast_expression (parser, /*address_p=*/false,
20170 /*cast_p=*/false, NULL);
20173 /* Parse a functional cast to TYPE. Returns an expression
20174 representing the cast. */
20177 cp_parser_functional_cast (cp_parser* parser, tree type)
20180 tree expression_list;
20184 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20186 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20187 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20188 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20189 if (TREE_CODE (type) == TYPE_DECL)
20190 type = TREE_TYPE (type);
20191 return finish_compound_literal (type, expression_list,
20192 tf_warning_or_error);
20196 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20198 /*allow_expansion_p=*/true,
20199 /*non_constant_p=*/NULL);
20201 expression_list = error_mark_node;
20204 expression_list = build_tree_list_vec (vec);
20205 release_tree_vector (vec);
20208 cast = build_functional_cast (type, expression_list,
20209 tf_warning_or_error);
20210 /* [expr.const]/1: In an integral constant expression "only type
20211 conversions to integral or enumeration type can be used". */
20212 if (TREE_CODE (type) == TYPE_DECL)
20213 type = TREE_TYPE (type);
20214 if (cast != error_mark_node
20215 && !cast_valid_in_integral_constant_expression_p (type)
20216 && cp_parser_non_integral_constant_expression (parser,
20218 return error_mark_node;
20222 /* Save the tokens that make up the body of a member function defined
20223 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20224 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20225 specifiers applied to the declaration. Returns the FUNCTION_DECL
20226 for the member function. */
20229 cp_parser_save_member_function_body (cp_parser* parser,
20230 cp_decl_specifier_seq *decl_specifiers,
20231 cp_declarator *declarator,
20238 /* Create the FUNCTION_DECL. */
20239 fn = grokmethod (decl_specifiers, declarator, attributes);
20240 /* If something went badly wrong, bail out now. */
20241 if (fn == error_mark_node)
20243 /* If there's a function-body, skip it. */
20244 if (cp_parser_token_starts_function_definition_p
20245 (cp_lexer_peek_token (parser->lexer)))
20246 cp_parser_skip_to_end_of_block_or_statement (parser);
20247 return error_mark_node;
20250 /* Remember it, if there default args to post process. */
20251 cp_parser_save_default_args (parser, fn);
20253 /* Save away the tokens that make up the body of the
20255 first = parser->lexer->next_token;
20256 /* We can have braced-init-list mem-initializers before the fn body. */
20257 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20259 cp_lexer_consume_token (parser->lexer);
20260 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20261 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20263 /* cache_group will stop after an un-nested { } pair, too. */
20264 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20267 /* variadic mem-inits have ... after the ')'. */
20268 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20269 cp_lexer_consume_token (parser->lexer);
20272 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20273 /* Handle function try blocks. */
20274 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20275 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20276 last = parser->lexer->next_token;
20278 /* Save away the inline definition; we will process it when the
20279 class is complete. */
20280 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20281 DECL_PENDING_INLINE_P (fn) = 1;
20283 /* We need to know that this was defined in the class, so that
20284 friend templates are handled correctly. */
20285 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20287 /* Add FN to the queue of functions to be parsed later. */
20288 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20293 /* Parse a template-argument-list, as well as the trailing ">" (but
20294 not the opening ">"). See cp_parser_template_argument_list for the
20298 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20302 tree saved_qualifying_scope;
20303 tree saved_object_scope;
20304 bool saved_greater_than_is_operator_p;
20305 int saved_unevaluated_operand;
20306 int saved_inhibit_evaluation_warnings;
20310 When parsing a template-id, the first non-nested `>' is taken as
20311 the end of the template-argument-list rather than a greater-than
20313 saved_greater_than_is_operator_p
20314 = parser->greater_than_is_operator_p;
20315 parser->greater_than_is_operator_p = false;
20316 /* Parsing the argument list may modify SCOPE, so we save it
20318 saved_scope = parser->scope;
20319 saved_qualifying_scope = parser->qualifying_scope;
20320 saved_object_scope = parser->object_scope;
20321 /* We need to evaluate the template arguments, even though this
20322 template-id may be nested within a "sizeof". */
20323 saved_unevaluated_operand = cp_unevaluated_operand;
20324 cp_unevaluated_operand = 0;
20325 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20326 c_inhibit_evaluation_warnings = 0;
20327 /* Parse the template-argument-list itself. */
20328 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20329 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20330 arguments = NULL_TREE;
20332 arguments = cp_parser_template_argument_list (parser);
20333 /* Look for the `>' that ends the template-argument-list. If we find
20334 a '>>' instead, it's probably just a typo. */
20335 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20337 if (cxx_dialect != cxx98)
20339 /* In C++0x, a `>>' in a template argument list or cast
20340 expression is considered to be two separate `>'
20341 tokens. So, change the current token to a `>', but don't
20342 consume it: it will be consumed later when the outer
20343 template argument list (or cast expression) is parsed.
20344 Note that this replacement of `>' for `>>' is necessary
20345 even if we are parsing tentatively: in the tentative
20346 case, after calling
20347 cp_parser_enclosed_template_argument_list we will always
20348 throw away all of the template arguments and the first
20349 closing `>', either because the template argument list
20350 was erroneous or because we are replacing those tokens
20351 with a CPP_TEMPLATE_ID token. The second `>' (which will
20352 not have been thrown away) is needed either to close an
20353 outer template argument list or to complete a new-style
20355 cp_token *token = cp_lexer_peek_token (parser->lexer);
20356 token->type = CPP_GREATER;
20358 else if (!saved_greater_than_is_operator_p)
20360 /* If we're in a nested template argument list, the '>>' has
20361 to be a typo for '> >'. We emit the error message, but we
20362 continue parsing and we push a '>' as next token, so that
20363 the argument list will be parsed correctly. Note that the
20364 global source location is still on the token before the
20365 '>>', so we need to say explicitly where we want it. */
20366 cp_token *token = cp_lexer_peek_token (parser->lexer);
20367 error_at (token->location, "%<>>%> should be %<> >%> "
20368 "within a nested template argument list");
20370 token->type = CPP_GREATER;
20374 /* If this is not a nested template argument list, the '>>'
20375 is a typo for '>'. Emit an error message and continue.
20376 Same deal about the token location, but here we can get it
20377 right by consuming the '>>' before issuing the diagnostic. */
20378 cp_token *token = cp_lexer_consume_token (parser->lexer);
20379 error_at (token->location,
20380 "spurious %<>>%>, use %<>%> to terminate "
20381 "a template argument list");
20385 cp_parser_skip_to_end_of_template_parameter_list (parser);
20386 /* The `>' token might be a greater-than operator again now. */
20387 parser->greater_than_is_operator_p
20388 = saved_greater_than_is_operator_p;
20389 /* Restore the SAVED_SCOPE. */
20390 parser->scope = saved_scope;
20391 parser->qualifying_scope = saved_qualifying_scope;
20392 parser->object_scope = saved_object_scope;
20393 cp_unevaluated_operand = saved_unevaluated_operand;
20394 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20399 /* MEMBER_FUNCTION is a member function, or a friend. If default
20400 arguments, or the body of the function have not yet been parsed,
20404 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20406 timevar_push (TV_PARSE_INMETH);
20407 /* If this member is a template, get the underlying
20409 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20410 member_function = DECL_TEMPLATE_RESULT (member_function);
20412 /* There should not be any class definitions in progress at this
20413 point; the bodies of members are only parsed outside of all class
20415 gcc_assert (parser->num_classes_being_defined == 0);
20416 /* While we're parsing the member functions we might encounter more
20417 classes. We want to handle them right away, but we don't want
20418 them getting mixed up with functions that are currently in the
20420 push_unparsed_function_queues (parser);
20422 /* Make sure that any template parameters are in scope. */
20423 maybe_begin_member_template_processing (member_function);
20425 /* If the body of the function has not yet been parsed, parse it
20427 if (DECL_PENDING_INLINE_P (member_function))
20429 tree function_scope;
20430 cp_token_cache *tokens;
20432 /* The function is no longer pending; we are processing it. */
20433 tokens = DECL_PENDING_INLINE_INFO (member_function);
20434 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20435 DECL_PENDING_INLINE_P (member_function) = 0;
20437 /* If this is a local class, enter the scope of the containing
20439 function_scope = current_function_decl;
20440 if (function_scope)
20441 push_function_context ();
20443 /* Push the body of the function onto the lexer stack. */
20444 cp_parser_push_lexer_for_tokens (parser, tokens);
20446 /* Let the front end know that we going to be defining this
20448 start_preparsed_function (member_function, NULL_TREE,
20449 SF_PRE_PARSED | SF_INCLASS_INLINE);
20451 /* Don't do access checking if it is a templated function. */
20452 if (processing_template_decl)
20453 push_deferring_access_checks (dk_no_check);
20455 /* Now, parse the body of the function. */
20456 cp_parser_function_definition_after_declarator (parser,
20457 /*inline_p=*/true);
20459 if (processing_template_decl)
20460 pop_deferring_access_checks ();
20462 /* Leave the scope of the containing function. */
20463 if (function_scope)
20464 pop_function_context ();
20465 cp_parser_pop_lexer (parser);
20468 /* Remove any template parameters from the symbol table. */
20469 maybe_end_member_template_processing ();
20471 /* Restore the queue. */
20472 pop_unparsed_function_queues (parser);
20473 timevar_pop (TV_PARSE_INMETH);
20476 /* If DECL contains any default args, remember it on the unparsed
20477 functions queue. */
20480 cp_parser_save_default_args (cp_parser* parser, tree decl)
20484 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20486 probe = TREE_CHAIN (probe))
20487 if (TREE_PURPOSE (probe))
20489 cp_default_arg_entry *entry
20490 = VEC_safe_push (cp_default_arg_entry, gc,
20491 unparsed_funs_with_default_args, NULL);
20492 entry->class_type = current_class_type;
20493 entry->decl = decl;
20498 /* FN is a FUNCTION_DECL which may contains a parameter with an
20499 unparsed DEFAULT_ARG. Parse the default args now. This function
20500 assumes that the current scope is the scope in which the default
20501 argument should be processed. */
20504 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20506 bool saved_local_variables_forbidden_p;
20507 tree parm, parmdecl;
20509 /* While we're parsing the default args, we might (due to the
20510 statement expression extension) encounter more classes. We want
20511 to handle them right away, but we don't want them getting mixed
20512 up with default args that are currently in the queue. */
20513 push_unparsed_function_queues (parser);
20515 /* Local variable names (and the `this' keyword) may not appear
20516 in a default argument. */
20517 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20518 parser->local_variables_forbidden_p = true;
20520 push_defarg_context (fn);
20522 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20523 parmdecl = DECL_ARGUMENTS (fn);
20524 parm && parm != void_list_node;
20525 parm = TREE_CHAIN (parm),
20526 parmdecl = DECL_CHAIN (parmdecl))
20528 cp_token_cache *tokens;
20529 tree default_arg = TREE_PURPOSE (parm);
20531 VEC(tree,gc) *insts;
20538 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20539 /* This can happen for a friend declaration for a function
20540 already declared with default arguments. */
20543 /* Push the saved tokens for the default argument onto the parser's
20545 tokens = DEFARG_TOKENS (default_arg);
20546 cp_parser_push_lexer_for_tokens (parser, tokens);
20548 start_lambda_scope (parmdecl);
20550 /* Parse the assignment-expression. */
20551 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20552 if (parsed_arg == error_mark_node)
20554 cp_parser_pop_lexer (parser);
20558 if (!processing_template_decl)
20559 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20561 TREE_PURPOSE (parm) = parsed_arg;
20563 /* Update any instantiations we've already created. */
20564 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20565 VEC_iterate (tree, insts, ix, copy); ix++)
20566 TREE_PURPOSE (copy) = parsed_arg;
20568 finish_lambda_scope ();
20570 /* If the token stream has not been completely used up, then
20571 there was extra junk after the end of the default
20573 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20574 cp_parser_error (parser, "expected %<,%>");
20576 /* Revert to the main lexer. */
20577 cp_parser_pop_lexer (parser);
20580 pop_defarg_context ();
20582 /* Make sure no default arg is missing. */
20583 check_default_args (fn);
20585 /* Restore the state of local_variables_forbidden_p. */
20586 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20588 /* Restore the queue. */
20589 pop_unparsed_function_queues (parser);
20592 /* Parse the operand of `sizeof' (or a similar operator). Returns
20593 either a TYPE or an expression, depending on the form of the
20594 input. The KEYWORD indicates which kind of expression we have
20598 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20600 tree expr = NULL_TREE;
20601 const char *saved_message;
20603 bool saved_integral_constant_expression_p;
20604 bool saved_non_integral_constant_expression_p;
20605 bool pack_expansion_p = false;
20607 /* Types cannot be defined in a `sizeof' expression. Save away the
20609 saved_message = parser->type_definition_forbidden_message;
20610 /* And create the new one. */
20611 tmp = concat ("types may not be defined in %<",
20612 IDENTIFIER_POINTER (ridpointers[keyword]),
20613 "%> expressions", NULL);
20614 parser->type_definition_forbidden_message = tmp;
20616 /* The restrictions on constant-expressions do not apply inside
20617 sizeof expressions. */
20618 saved_integral_constant_expression_p
20619 = parser->integral_constant_expression_p;
20620 saved_non_integral_constant_expression_p
20621 = parser->non_integral_constant_expression_p;
20622 parser->integral_constant_expression_p = false;
20624 /* If it's a `...', then we are computing the length of a parameter
20626 if (keyword == RID_SIZEOF
20627 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20629 /* Consume the `...'. */
20630 cp_lexer_consume_token (parser->lexer);
20631 maybe_warn_variadic_templates ();
20633 /* Note that this is an expansion. */
20634 pack_expansion_p = true;
20637 /* Do not actually evaluate the expression. */
20638 ++cp_unevaluated_operand;
20639 ++c_inhibit_evaluation_warnings;
20640 /* If it's a `(', then we might be looking at the type-id
20642 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20645 bool saved_in_type_id_in_expr_p;
20647 /* We can't be sure yet whether we're looking at a type-id or an
20649 cp_parser_parse_tentatively (parser);
20650 /* Consume the `('. */
20651 cp_lexer_consume_token (parser->lexer);
20652 /* Parse the type-id. */
20653 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20654 parser->in_type_id_in_expr_p = true;
20655 type = cp_parser_type_id (parser);
20656 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20657 /* Now, look for the trailing `)'. */
20658 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20659 /* If all went well, then we're done. */
20660 if (cp_parser_parse_definitely (parser))
20662 cp_decl_specifier_seq decl_specs;
20664 /* Build a trivial decl-specifier-seq. */
20665 clear_decl_specs (&decl_specs);
20666 decl_specs.type = type;
20668 /* Call grokdeclarator to figure out what type this is. */
20669 expr = grokdeclarator (NULL,
20673 /*attrlist=*/NULL);
20677 /* If the type-id production did not work out, then we must be
20678 looking at the unary-expression production. */
20680 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20681 /*cast_p=*/false, NULL);
20683 if (pack_expansion_p)
20684 /* Build a pack expansion. */
20685 expr = make_pack_expansion (expr);
20687 /* Go back to evaluating expressions. */
20688 --cp_unevaluated_operand;
20689 --c_inhibit_evaluation_warnings;
20691 /* Free the message we created. */
20693 /* And restore the old one. */
20694 parser->type_definition_forbidden_message = saved_message;
20695 parser->integral_constant_expression_p
20696 = saved_integral_constant_expression_p;
20697 parser->non_integral_constant_expression_p
20698 = saved_non_integral_constant_expression_p;
20703 /* If the current declaration has no declarator, return true. */
20706 cp_parser_declares_only_class_p (cp_parser *parser)
20708 /* If the next token is a `;' or a `,' then there is no
20710 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20711 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20714 /* Update the DECL_SPECS to reflect the storage class indicated by
20718 cp_parser_set_storage_class (cp_parser *parser,
20719 cp_decl_specifier_seq *decl_specs,
20721 location_t location)
20723 cp_storage_class storage_class;
20725 if (parser->in_unbraced_linkage_specification_p)
20727 error_at (location, "invalid use of %qD in linkage specification",
20728 ridpointers[keyword]);
20731 else if (decl_specs->storage_class != sc_none)
20733 decl_specs->conflicting_specifiers_p = true;
20737 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20738 && decl_specs->specs[(int) ds_thread])
20740 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20741 decl_specs->specs[(int) ds_thread] = 0;
20747 storage_class = sc_auto;
20750 storage_class = sc_register;
20753 storage_class = sc_static;
20756 storage_class = sc_extern;
20759 storage_class = sc_mutable;
20762 gcc_unreachable ();
20764 decl_specs->storage_class = storage_class;
20766 /* A storage class specifier cannot be applied alongside a typedef
20767 specifier. If there is a typedef specifier present then set
20768 conflicting_specifiers_p which will trigger an error later
20769 on in grokdeclarator. */
20770 if (decl_specs->specs[(int)ds_typedef])
20771 decl_specs->conflicting_specifiers_p = true;
20774 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20775 is true, the type is a user-defined type; otherwise it is a
20776 built-in type specified by a keyword. */
20779 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20781 location_t location,
20782 bool user_defined_p)
20784 decl_specs->any_specifiers_p = true;
20786 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20787 (with, for example, in "typedef int wchar_t;") we remember that
20788 this is what happened. In system headers, we ignore these
20789 declarations so that G++ can work with system headers that are not
20791 if (decl_specs->specs[(int) ds_typedef]
20793 && (type_spec == boolean_type_node
20794 || type_spec == char16_type_node
20795 || type_spec == char32_type_node
20796 || type_spec == wchar_type_node)
20797 && (decl_specs->type
20798 || decl_specs->specs[(int) ds_long]
20799 || decl_specs->specs[(int) ds_short]
20800 || decl_specs->specs[(int) ds_unsigned]
20801 || decl_specs->specs[(int) ds_signed]))
20803 decl_specs->redefined_builtin_type = type_spec;
20804 if (!decl_specs->type)
20806 decl_specs->type = type_spec;
20807 decl_specs->user_defined_type_p = false;
20808 decl_specs->type_location = location;
20811 else if (decl_specs->type)
20812 decl_specs->multiple_types_p = true;
20815 decl_specs->type = type_spec;
20816 decl_specs->user_defined_type_p = user_defined_p;
20817 decl_specs->redefined_builtin_type = NULL_TREE;
20818 decl_specs->type_location = location;
20822 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20823 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20826 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20828 return decl_specifiers->specs[(int) ds_friend] != 0;
20831 /* Issue an error message indicating that TOKEN_DESC was expected.
20832 If KEYWORD is true, it indicated this function is called by
20833 cp_parser_require_keword and the required token can only be
20834 a indicated keyword. */
20837 cp_parser_required_error (cp_parser *parser,
20838 required_token token_desc,
20841 switch (token_desc)
20844 cp_parser_error (parser, "expected %<new%>");
20847 cp_parser_error (parser, "expected %<delete%>");
20850 cp_parser_error (parser, "expected %<return%>");
20853 cp_parser_error (parser, "expected %<while%>");
20856 cp_parser_error (parser, "expected %<extern%>");
20858 case RT_STATIC_ASSERT:
20859 cp_parser_error (parser, "expected %<static_assert%>");
20862 cp_parser_error (parser, "expected %<decltype%>");
20865 cp_parser_error (parser, "expected %<operator%>");
20868 cp_parser_error (parser, "expected %<class%>");
20871 cp_parser_error (parser, "expected %<template%>");
20874 cp_parser_error (parser, "expected %<namespace%>");
20877 cp_parser_error (parser, "expected %<using%>");
20880 cp_parser_error (parser, "expected %<asm%>");
20883 cp_parser_error (parser, "expected %<try%>");
20886 cp_parser_error (parser, "expected %<catch%>");
20889 cp_parser_error (parser, "expected %<throw%>");
20892 cp_parser_error (parser, "expected %<__label__%>");
20895 cp_parser_error (parser, "expected %<@try%>");
20897 case RT_AT_SYNCHRONIZED:
20898 cp_parser_error (parser, "expected %<@synchronized%>");
20901 cp_parser_error (parser, "expected %<@throw%>");
20908 switch (token_desc)
20911 cp_parser_error (parser, "expected %<;%>");
20913 case RT_OPEN_PAREN:
20914 cp_parser_error (parser, "expected %<(%>");
20916 case RT_CLOSE_BRACE:
20917 cp_parser_error (parser, "expected %<}%>");
20919 case RT_OPEN_BRACE:
20920 cp_parser_error (parser, "expected %<{%>");
20922 case RT_CLOSE_SQUARE:
20923 cp_parser_error (parser, "expected %<]%>");
20925 case RT_OPEN_SQUARE:
20926 cp_parser_error (parser, "expected %<[%>");
20929 cp_parser_error (parser, "expected %<,%>");
20932 cp_parser_error (parser, "expected %<::%>");
20935 cp_parser_error (parser, "expected %<<%>");
20938 cp_parser_error (parser, "expected %<>%>");
20941 cp_parser_error (parser, "expected %<=%>");
20944 cp_parser_error (parser, "expected %<...%>");
20947 cp_parser_error (parser, "expected %<*%>");
20950 cp_parser_error (parser, "expected %<~%>");
20953 cp_parser_error (parser, "expected %<:%>");
20955 case RT_COLON_SCOPE:
20956 cp_parser_error (parser, "expected %<:%> or %<::%>");
20958 case RT_CLOSE_PAREN:
20959 cp_parser_error (parser, "expected %<)%>");
20961 case RT_COMMA_CLOSE_PAREN:
20962 cp_parser_error (parser, "expected %<,%> or %<)%>");
20964 case RT_PRAGMA_EOL:
20965 cp_parser_error (parser, "expected end of line");
20968 cp_parser_error (parser, "expected identifier");
20971 cp_parser_error (parser, "expected selection-statement");
20973 case RT_INTERATION:
20974 cp_parser_error (parser, "expected iteration-statement");
20977 cp_parser_error (parser, "expected jump-statement");
20980 cp_parser_error (parser, "expected class-key");
20982 case RT_CLASS_TYPENAME_TEMPLATE:
20983 cp_parser_error (parser,
20984 "expected %<class%>, %<typename%>, or %<template%>");
20987 gcc_unreachable ();
20991 gcc_unreachable ();
20996 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20997 issue an error message indicating that TOKEN_DESC was expected.
20999 Returns the token consumed, if the token had the appropriate type.
21000 Otherwise, returns NULL. */
21003 cp_parser_require (cp_parser* parser,
21004 enum cpp_ttype type,
21005 required_token token_desc)
21007 if (cp_lexer_next_token_is (parser->lexer, type))
21008 return cp_lexer_consume_token (parser->lexer);
21011 /* Output the MESSAGE -- unless we're parsing tentatively. */
21012 if (!cp_parser_simulate_error (parser))
21013 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21018 /* An error message is produced if the next token is not '>'.
21019 All further tokens are skipped until the desired token is
21020 found or '{', '}', ';' or an unbalanced ')' or ']'. */
21023 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21025 /* Current level of '< ... >'. */
21026 unsigned level = 0;
21027 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
21028 unsigned nesting_depth = 0;
21030 /* Are we ready, yet? If not, issue error message. */
21031 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21034 /* Skip tokens until the desired token is found. */
21037 /* Peek at the next token. */
21038 switch (cp_lexer_peek_token (parser->lexer)->type)
21041 if (!nesting_depth)
21046 if (cxx_dialect == cxx98)
21047 /* C++0x views the `>>' operator as two `>' tokens, but
21050 else if (!nesting_depth && level-- == 0)
21052 /* We've hit a `>>' where the first `>' closes the
21053 template argument list, and the second `>' is
21054 spurious. Just consume the `>>' and stop; we've
21055 already produced at least one error. */
21056 cp_lexer_consume_token (parser->lexer);
21059 /* Fall through for C++0x, so we handle the second `>' in
21063 if (!nesting_depth && level-- == 0)
21065 /* We've reached the token we want, consume it and stop. */
21066 cp_lexer_consume_token (parser->lexer);
21071 case CPP_OPEN_PAREN:
21072 case CPP_OPEN_SQUARE:
21076 case CPP_CLOSE_PAREN:
21077 case CPP_CLOSE_SQUARE:
21078 if (nesting_depth-- == 0)
21083 case CPP_PRAGMA_EOL:
21084 case CPP_SEMICOLON:
21085 case CPP_OPEN_BRACE:
21086 case CPP_CLOSE_BRACE:
21087 /* The '>' was probably forgotten, don't look further. */
21094 /* Consume this token. */
21095 cp_lexer_consume_token (parser->lexer);
21099 /* If the next token is the indicated keyword, consume it. Otherwise,
21100 issue an error message indicating that TOKEN_DESC was expected.
21102 Returns the token consumed, if the token had the appropriate type.
21103 Otherwise, returns NULL. */
21106 cp_parser_require_keyword (cp_parser* parser,
21108 required_token token_desc)
21110 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21112 if (token && token->keyword != keyword)
21114 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21121 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21122 function-definition. */
21125 cp_parser_token_starts_function_definition_p (cp_token* token)
21127 return (/* An ordinary function-body begins with an `{'. */
21128 token->type == CPP_OPEN_BRACE
21129 /* A ctor-initializer begins with a `:'. */
21130 || token->type == CPP_COLON
21131 /* A function-try-block begins with `try'. */
21132 || token->keyword == RID_TRY
21133 /* The named return value extension begins with `return'. */
21134 || token->keyword == RID_RETURN);
21137 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21141 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21145 token = cp_lexer_peek_token (parser->lexer);
21146 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21149 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21150 C++0x) ending a template-argument. */
21153 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21157 token = cp_lexer_peek_token (parser->lexer);
21158 return (token->type == CPP_COMMA
21159 || token->type == CPP_GREATER
21160 || token->type == CPP_ELLIPSIS
21161 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21164 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21165 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21168 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21173 token = cp_lexer_peek_nth_token (parser->lexer, n);
21174 if (token->type == CPP_LESS)
21176 /* Check for the sequence `<::' in the original code. It would be lexed as
21177 `[:', where `[' is a digraph, and there is no whitespace before
21179 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21182 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21183 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21189 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21190 or none_type otherwise. */
21192 static enum tag_types
21193 cp_parser_token_is_class_key (cp_token* token)
21195 switch (token->keyword)
21200 return record_type;
21209 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21212 cp_parser_check_class_key (enum tag_types class_key, tree type)
21214 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21215 permerror (input_location, "%qs tag used in naming %q#T",
21216 class_key == union_type ? "union"
21217 : class_key == record_type ? "struct" : "class",
21221 /* Issue an error message if DECL is redeclared with different
21222 access than its original declaration [class.access.spec/3].
21223 This applies to nested classes and nested class templates.
21227 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21229 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21232 if ((TREE_PRIVATE (decl)
21233 != (current_access_specifier == access_private_node))
21234 || (TREE_PROTECTED (decl)
21235 != (current_access_specifier == access_protected_node)))
21236 error_at (location, "%qD redeclared with different access", decl);
21239 /* Look for the `template' keyword, as a syntactic disambiguator.
21240 Return TRUE iff it is present, in which case it will be
21244 cp_parser_optional_template_keyword (cp_parser *parser)
21246 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21248 /* The `template' keyword can only be used within templates;
21249 outside templates the parser can always figure out what is a
21250 template and what is not. */
21251 if (!processing_template_decl)
21253 cp_token *token = cp_lexer_peek_token (parser->lexer);
21254 error_at (token->location,
21255 "%<template%> (as a disambiguator) is only allowed "
21256 "within templates");
21257 /* If this part of the token stream is rescanned, the same
21258 error message would be generated. So, we purge the token
21259 from the stream. */
21260 cp_lexer_purge_token (parser->lexer);
21265 /* Consume the `template' keyword. */
21266 cp_lexer_consume_token (parser->lexer);
21274 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21275 set PARSER->SCOPE, and perform other related actions. */
21278 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21281 struct tree_check *check_value;
21282 deferred_access_check *chk;
21283 VEC (deferred_access_check,gc) *checks;
21285 /* Get the stored value. */
21286 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21287 /* Perform any access checks that were deferred. */
21288 checks = check_value->checks;
21291 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21292 perform_or_defer_access_check (chk->binfo,
21296 /* Set the scope from the stored value. */
21297 parser->scope = check_value->value;
21298 parser->qualifying_scope = check_value->qualifying_scope;
21299 parser->object_scope = NULL_TREE;
21302 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21303 encounter the end of a block before what we were looking for. */
21306 cp_parser_cache_group (cp_parser *parser,
21307 enum cpp_ttype end,
21312 cp_token *token = cp_lexer_peek_token (parser->lexer);
21314 /* Abort a parenthesized expression if we encounter a semicolon. */
21315 if ((end == CPP_CLOSE_PAREN || depth == 0)
21316 && token->type == CPP_SEMICOLON)
21318 /* If we've reached the end of the file, stop. */
21319 if (token->type == CPP_EOF
21320 || (end != CPP_PRAGMA_EOL
21321 && token->type == CPP_PRAGMA_EOL))
21323 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21324 /* We've hit the end of an enclosing block, so there's been some
21325 kind of syntax error. */
21328 /* Consume the token. */
21329 cp_lexer_consume_token (parser->lexer);
21330 /* See if it starts a new group. */
21331 if (token->type == CPP_OPEN_BRACE)
21333 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21334 /* In theory this should probably check end == '}', but
21335 cp_parser_save_member_function_body needs it to exit
21336 after either '}' or ')' when called with ')'. */
21340 else if (token->type == CPP_OPEN_PAREN)
21342 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21343 if (depth == 0 && end == CPP_CLOSE_PAREN)
21346 else if (token->type == CPP_PRAGMA)
21347 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21348 else if (token->type == end)
21353 /* Begin parsing tentatively. We always save tokens while parsing
21354 tentatively so that if the tentative parsing fails we can restore the
21358 cp_parser_parse_tentatively (cp_parser* parser)
21360 /* Enter a new parsing context. */
21361 parser->context = cp_parser_context_new (parser->context);
21362 /* Begin saving tokens. */
21363 cp_lexer_save_tokens (parser->lexer);
21364 /* In order to avoid repetitive access control error messages,
21365 access checks are queued up until we are no longer parsing
21367 push_deferring_access_checks (dk_deferred);
21370 /* Commit to the currently active tentative parse. */
21373 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21375 cp_parser_context *context;
21378 /* Mark all of the levels as committed. */
21379 lexer = parser->lexer;
21380 for (context = parser->context; context->next; context = context->next)
21382 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21384 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21385 while (!cp_lexer_saving_tokens (lexer))
21386 lexer = lexer->next;
21387 cp_lexer_commit_tokens (lexer);
21391 /* Abort the currently active tentative parse. All consumed tokens
21392 will be rolled back, and no diagnostics will be issued. */
21395 cp_parser_abort_tentative_parse (cp_parser* parser)
21397 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21398 || errorcount > 0);
21399 cp_parser_simulate_error (parser);
21400 /* Now, pretend that we want to see if the construct was
21401 successfully parsed. */
21402 cp_parser_parse_definitely (parser);
21405 /* Stop parsing tentatively. If a parse error has occurred, restore the
21406 token stream. Otherwise, commit to the tokens we have consumed.
21407 Returns true if no error occurred; false otherwise. */
21410 cp_parser_parse_definitely (cp_parser* parser)
21412 bool error_occurred;
21413 cp_parser_context *context;
21415 /* Remember whether or not an error occurred, since we are about to
21416 destroy that information. */
21417 error_occurred = cp_parser_error_occurred (parser);
21418 /* Remove the topmost context from the stack. */
21419 context = parser->context;
21420 parser->context = context->next;
21421 /* If no parse errors occurred, commit to the tentative parse. */
21422 if (!error_occurred)
21424 /* Commit to the tokens read tentatively, unless that was
21426 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21427 cp_lexer_commit_tokens (parser->lexer);
21429 pop_to_parent_deferring_access_checks ();
21431 /* Otherwise, if errors occurred, roll back our state so that things
21432 are just as they were before we began the tentative parse. */
21435 cp_lexer_rollback_tokens (parser->lexer);
21436 pop_deferring_access_checks ();
21438 /* Add the context to the front of the free list. */
21439 context->next = cp_parser_context_free_list;
21440 cp_parser_context_free_list = context;
21442 return !error_occurred;
21445 /* Returns true if we are parsing tentatively and are not committed to
21446 this tentative parse. */
21449 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21451 return (cp_parser_parsing_tentatively (parser)
21452 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21455 /* Returns nonzero iff an error has occurred during the most recent
21456 tentative parse. */
21459 cp_parser_error_occurred (cp_parser* parser)
21461 return (cp_parser_parsing_tentatively (parser)
21462 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21465 /* Returns nonzero if GNU extensions are allowed. */
21468 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21470 return parser->allow_gnu_extensions_p;
21473 /* Objective-C++ Productions */
21476 /* Parse an Objective-C expression, which feeds into a primary-expression
21480 objc-message-expression
21481 objc-string-literal
21482 objc-encode-expression
21483 objc-protocol-expression
21484 objc-selector-expression
21486 Returns a tree representation of the expression. */
21489 cp_parser_objc_expression (cp_parser* parser)
21491 /* Try to figure out what kind of declaration is present. */
21492 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21496 case CPP_OPEN_SQUARE:
21497 return cp_parser_objc_message_expression (parser);
21499 case CPP_OBJC_STRING:
21500 kwd = cp_lexer_consume_token (parser->lexer);
21501 return objc_build_string_object (kwd->u.value);
21504 switch (kwd->keyword)
21506 case RID_AT_ENCODE:
21507 return cp_parser_objc_encode_expression (parser);
21509 case RID_AT_PROTOCOL:
21510 return cp_parser_objc_protocol_expression (parser);
21512 case RID_AT_SELECTOR:
21513 return cp_parser_objc_selector_expression (parser);
21519 error_at (kwd->location,
21520 "misplaced %<@%D%> Objective-C++ construct",
21522 cp_parser_skip_to_end_of_block_or_statement (parser);
21525 return error_mark_node;
21528 /* Parse an Objective-C message expression.
21530 objc-message-expression:
21531 [ objc-message-receiver objc-message-args ]
21533 Returns a representation of an Objective-C message. */
21536 cp_parser_objc_message_expression (cp_parser* parser)
21538 tree receiver, messageargs;
21540 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21541 receiver = cp_parser_objc_message_receiver (parser);
21542 messageargs = cp_parser_objc_message_args (parser);
21543 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21545 return objc_build_message_expr (receiver, messageargs);
21548 /* Parse an objc-message-receiver.
21550 objc-message-receiver:
21552 simple-type-specifier
21554 Returns a representation of the type or expression. */
21557 cp_parser_objc_message_receiver (cp_parser* parser)
21561 /* An Objective-C message receiver may be either (1) a type
21562 or (2) an expression. */
21563 cp_parser_parse_tentatively (parser);
21564 rcv = cp_parser_expression (parser, false, NULL);
21566 if (cp_parser_parse_definitely (parser))
21569 rcv = cp_parser_simple_type_specifier (parser,
21570 /*decl_specs=*/NULL,
21571 CP_PARSER_FLAGS_NONE);
21573 return objc_get_class_reference (rcv);
21576 /* Parse the arguments and selectors comprising an Objective-C message.
21581 objc-selector-args , objc-comma-args
21583 objc-selector-args:
21584 objc-selector [opt] : assignment-expression
21585 objc-selector-args objc-selector [opt] : assignment-expression
21588 assignment-expression
21589 objc-comma-args , assignment-expression
21591 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21592 selector arguments and TREE_VALUE containing a list of comma
21596 cp_parser_objc_message_args (cp_parser* parser)
21598 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21599 bool maybe_unary_selector_p = true;
21600 cp_token *token = cp_lexer_peek_token (parser->lexer);
21602 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21604 tree selector = NULL_TREE, arg;
21606 if (token->type != CPP_COLON)
21607 selector = cp_parser_objc_selector (parser);
21609 /* Detect if we have a unary selector. */
21610 if (maybe_unary_selector_p
21611 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21612 return build_tree_list (selector, NULL_TREE);
21614 maybe_unary_selector_p = false;
21615 cp_parser_require (parser, CPP_COLON, RT_COLON);
21616 arg = cp_parser_assignment_expression (parser, false, NULL);
21619 = chainon (sel_args,
21620 build_tree_list (selector, arg));
21622 token = cp_lexer_peek_token (parser->lexer);
21625 /* Handle non-selector arguments, if any. */
21626 while (token->type == CPP_COMMA)
21630 cp_lexer_consume_token (parser->lexer);
21631 arg = cp_parser_assignment_expression (parser, false, NULL);
21634 = chainon (addl_args,
21635 build_tree_list (NULL_TREE, arg));
21637 token = cp_lexer_peek_token (parser->lexer);
21640 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21642 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21643 return build_tree_list (error_mark_node, error_mark_node);
21646 return build_tree_list (sel_args, addl_args);
21649 /* Parse an Objective-C encode expression.
21651 objc-encode-expression:
21652 @encode objc-typename
21654 Returns an encoded representation of the type argument. */
21657 cp_parser_objc_encode_expression (cp_parser* parser)
21662 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21663 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21664 token = cp_lexer_peek_token (parser->lexer);
21665 type = complete_type (cp_parser_type_id (parser));
21666 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21670 error_at (token->location,
21671 "%<@encode%> must specify a type as an argument");
21672 return error_mark_node;
21675 /* This happens if we find @encode(T) (where T is a template
21676 typename or something dependent on a template typename) when
21677 parsing a template. In that case, we can't compile it
21678 immediately, but we rather create an AT_ENCODE_EXPR which will
21679 need to be instantiated when the template is used.
21681 if (dependent_type_p (type))
21683 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21684 TREE_READONLY (value) = 1;
21688 return objc_build_encode_expr (type);
21691 /* Parse an Objective-C @defs expression. */
21694 cp_parser_objc_defs_expression (cp_parser *parser)
21698 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21699 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21700 name = cp_parser_identifier (parser);
21701 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21703 return objc_get_class_ivars (name);
21706 /* Parse an Objective-C protocol expression.
21708 objc-protocol-expression:
21709 @protocol ( identifier )
21711 Returns a representation of the protocol expression. */
21714 cp_parser_objc_protocol_expression (cp_parser* parser)
21718 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21719 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21720 proto = cp_parser_identifier (parser);
21721 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21723 return objc_build_protocol_expr (proto);
21726 /* Parse an Objective-C selector expression.
21728 objc-selector-expression:
21729 @selector ( objc-method-signature )
21731 objc-method-signature:
21737 objc-selector-seq objc-selector :
21739 Returns a representation of the method selector. */
21742 cp_parser_objc_selector_expression (cp_parser* parser)
21744 tree sel_seq = NULL_TREE;
21745 bool maybe_unary_selector_p = true;
21747 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21749 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21750 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21751 token = cp_lexer_peek_token (parser->lexer);
21753 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21754 || token->type == CPP_SCOPE)
21756 tree selector = NULL_TREE;
21758 if (token->type != CPP_COLON
21759 || token->type == CPP_SCOPE)
21760 selector = cp_parser_objc_selector (parser);
21762 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21763 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21765 /* Detect if we have a unary selector. */
21766 if (maybe_unary_selector_p)
21768 sel_seq = selector;
21769 goto finish_selector;
21773 cp_parser_error (parser, "expected %<:%>");
21776 maybe_unary_selector_p = false;
21777 token = cp_lexer_consume_token (parser->lexer);
21779 if (token->type == CPP_SCOPE)
21782 = chainon (sel_seq,
21783 build_tree_list (selector, NULL_TREE));
21785 = chainon (sel_seq,
21786 build_tree_list (NULL_TREE, NULL_TREE));
21790 = chainon (sel_seq,
21791 build_tree_list (selector, NULL_TREE));
21793 token = cp_lexer_peek_token (parser->lexer);
21797 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21799 return objc_build_selector_expr (loc, sel_seq);
21802 /* Parse a list of identifiers.
21804 objc-identifier-list:
21806 objc-identifier-list , identifier
21808 Returns a TREE_LIST of identifier nodes. */
21811 cp_parser_objc_identifier_list (cp_parser* parser)
21817 identifier = cp_parser_identifier (parser);
21818 if (identifier == error_mark_node)
21819 return error_mark_node;
21821 list = build_tree_list (NULL_TREE, identifier);
21822 sep = cp_lexer_peek_token (parser->lexer);
21824 while (sep->type == CPP_COMMA)
21826 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21827 identifier = cp_parser_identifier (parser);
21828 if (identifier == error_mark_node)
21831 list = chainon (list, build_tree_list (NULL_TREE,
21833 sep = cp_lexer_peek_token (parser->lexer);
21839 /* Parse an Objective-C alias declaration.
21841 objc-alias-declaration:
21842 @compatibility_alias identifier identifier ;
21844 This function registers the alias mapping with the Objective-C front end.
21845 It returns nothing. */
21848 cp_parser_objc_alias_declaration (cp_parser* parser)
21852 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21853 alias = cp_parser_identifier (parser);
21854 orig = cp_parser_identifier (parser);
21855 objc_declare_alias (alias, orig);
21856 cp_parser_consume_semicolon_at_end_of_statement (parser);
21859 /* Parse an Objective-C class forward-declaration.
21861 objc-class-declaration:
21862 @class objc-identifier-list ;
21864 The function registers the forward declarations with the Objective-C
21865 front end. It returns nothing. */
21868 cp_parser_objc_class_declaration (cp_parser* parser)
21870 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21875 id = cp_parser_identifier (parser);
21876 if (id == error_mark_node)
21879 objc_declare_class (id);
21881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21882 cp_lexer_consume_token (parser->lexer);
21886 cp_parser_consume_semicolon_at_end_of_statement (parser);
21889 /* Parse a list of Objective-C protocol references.
21891 objc-protocol-refs-opt:
21892 objc-protocol-refs [opt]
21894 objc-protocol-refs:
21895 < objc-identifier-list >
21897 Returns a TREE_LIST of identifiers, if any. */
21900 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21902 tree protorefs = NULL_TREE;
21904 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21906 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21907 protorefs = cp_parser_objc_identifier_list (parser);
21908 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21914 /* Parse a Objective-C visibility specification. */
21917 cp_parser_objc_visibility_spec (cp_parser* parser)
21919 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21921 switch (vis->keyword)
21923 case RID_AT_PRIVATE:
21924 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21926 case RID_AT_PROTECTED:
21927 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21929 case RID_AT_PUBLIC:
21930 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21932 case RID_AT_PACKAGE:
21933 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21939 /* Eat '@private'/'@protected'/'@public'. */
21940 cp_lexer_consume_token (parser->lexer);
21943 /* Parse an Objective-C method type. Return 'true' if it is a class
21944 (+) method, and 'false' if it is an instance (-) method. */
21947 cp_parser_objc_method_type (cp_parser* parser)
21949 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21955 /* Parse an Objective-C protocol qualifier. */
21958 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21960 tree quals = NULL_TREE, node;
21961 cp_token *token = cp_lexer_peek_token (parser->lexer);
21963 node = token->u.value;
21965 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21966 && (node == ridpointers [(int) RID_IN]
21967 || node == ridpointers [(int) RID_OUT]
21968 || node == ridpointers [(int) RID_INOUT]
21969 || node == ridpointers [(int) RID_BYCOPY]
21970 || node == ridpointers [(int) RID_BYREF]
21971 || node == ridpointers [(int) RID_ONEWAY]))
21973 quals = tree_cons (NULL_TREE, node, quals);
21974 cp_lexer_consume_token (parser->lexer);
21975 token = cp_lexer_peek_token (parser->lexer);
21976 node = token->u.value;
21982 /* Parse an Objective-C typename. */
21985 cp_parser_objc_typename (cp_parser* parser)
21987 tree type_name = NULL_TREE;
21989 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21991 tree proto_quals, cp_type = NULL_TREE;
21993 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21994 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21996 /* An ObjC type name may consist of just protocol qualifiers, in which
21997 case the type shall default to 'id'. */
21998 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22000 cp_type = cp_parser_type_id (parser);
22002 /* If the type could not be parsed, an error has already
22003 been produced. For error recovery, behave as if it had
22004 not been specified, which will use the default type
22006 if (cp_type == error_mark_node)
22008 cp_type = NULL_TREE;
22009 /* We need to skip to the closing parenthesis as
22010 cp_parser_type_id() does not seem to do it for
22012 cp_parser_skip_to_closing_parenthesis (parser,
22013 /*recovering=*/true,
22014 /*or_comma=*/false,
22015 /*consume_paren=*/false);
22019 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22020 type_name = build_tree_list (proto_quals, cp_type);
22026 /* Check to see if TYPE refers to an Objective-C selector name. */
22029 cp_parser_objc_selector_p (enum cpp_ttype type)
22031 return (type == CPP_NAME || type == CPP_KEYWORD
22032 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22033 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22034 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22035 || type == CPP_XOR || type == CPP_XOR_EQ);
22038 /* Parse an Objective-C selector. */
22041 cp_parser_objc_selector (cp_parser* parser)
22043 cp_token *token = cp_lexer_consume_token (parser->lexer);
22045 if (!cp_parser_objc_selector_p (token->type))
22047 error_at (token->location, "invalid Objective-C++ selector name");
22048 return error_mark_node;
22051 /* C++ operator names are allowed to appear in ObjC selectors. */
22052 switch (token->type)
22054 case CPP_AND_AND: return get_identifier ("and");
22055 case CPP_AND_EQ: return get_identifier ("and_eq");
22056 case CPP_AND: return get_identifier ("bitand");
22057 case CPP_OR: return get_identifier ("bitor");
22058 case CPP_COMPL: return get_identifier ("compl");
22059 case CPP_NOT: return get_identifier ("not");
22060 case CPP_NOT_EQ: return get_identifier ("not_eq");
22061 case CPP_OR_OR: return get_identifier ("or");
22062 case CPP_OR_EQ: return get_identifier ("or_eq");
22063 case CPP_XOR: return get_identifier ("xor");
22064 case CPP_XOR_EQ: return get_identifier ("xor_eq");
22065 default: return token->u.value;
22069 /* Parse an Objective-C params list. */
22072 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22074 tree params = NULL_TREE;
22075 bool maybe_unary_selector_p = true;
22076 cp_token *token = cp_lexer_peek_token (parser->lexer);
22078 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22080 tree selector = NULL_TREE, type_name, identifier;
22081 tree parm_attr = NULL_TREE;
22083 if (token->keyword == RID_ATTRIBUTE)
22086 if (token->type != CPP_COLON)
22087 selector = cp_parser_objc_selector (parser);
22089 /* Detect if we have a unary selector. */
22090 if (maybe_unary_selector_p
22091 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22093 params = selector; /* Might be followed by attributes. */
22097 maybe_unary_selector_p = false;
22098 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22100 /* Something went quite wrong. There should be a colon
22101 here, but there is not. Stop parsing parameters. */
22104 type_name = cp_parser_objc_typename (parser);
22105 /* New ObjC allows attributes on parameters too. */
22106 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22107 parm_attr = cp_parser_attributes_opt (parser);
22108 identifier = cp_parser_identifier (parser);
22112 objc_build_keyword_decl (selector,
22117 token = cp_lexer_peek_token (parser->lexer);
22120 if (params == NULL_TREE)
22122 cp_parser_error (parser, "objective-c++ method declaration is expected");
22123 return error_mark_node;
22126 /* We allow tail attributes for the method. */
22127 if (token->keyword == RID_ATTRIBUTE)
22129 *attributes = cp_parser_attributes_opt (parser);
22130 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22131 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22133 cp_parser_error (parser,
22134 "method attributes must be specified at the end");
22135 return error_mark_node;
22138 if (params == NULL_TREE)
22140 cp_parser_error (parser, "objective-c++ method declaration is expected");
22141 return error_mark_node;
22146 /* Parse the non-keyword Objective-C params. */
22149 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22152 tree params = make_node (TREE_LIST);
22153 cp_token *token = cp_lexer_peek_token (parser->lexer);
22154 *ellipsisp = false; /* Initially, assume no ellipsis. */
22156 while (token->type == CPP_COMMA)
22158 cp_parameter_declarator *parmdecl;
22161 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22162 token = cp_lexer_peek_token (parser->lexer);
22164 if (token->type == CPP_ELLIPSIS)
22166 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22168 token = cp_lexer_peek_token (parser->lexer);
22172 /* TODO: parse attributes for tail parameters. */
22173 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22174 parm = grokdeclarator (parmdecl->declarator,
22175 &parmdecl->decl_specifiers,
22176 PARM, /*initialized=*/0,
22177 /*attrlist=*/NULL);
22179 chainon (params, build_tree_list (NULL_TREE, parm));
22180 token = cp_lexer_peek_token (parser->lexer);
22183 /* We allow tail attributes for the method. */
22184 if (token->keyword == RID_ATTRIBUTE)
22186 if (*attributes == NULL_TREE)
22188 *attributes = cp_parser_attributes_opt (parser);
22189 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22190 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22194 /* We have an error, but parse the attributes, so that we can
22196 *attributes = cp_parser_attributes_opt (parser);
22198 cp_parser_error (parser,
22199 "method attributes must be specified at the end");
22200 return error_mark_node;
22206 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22209 cp_parser_objc_interstitial_code (cp_parser* parser)
22211 cp_token *token = cp_lexer_peek_token (parser->lexer);
22213 /* If the next token is `extern' and the following token is a string
22214 literal, then we have a linkage specification. */
22215 if (token->keyword == RID_EXTERN
22216 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22217 cp_parser_linkage_specification (parser);
22218 /* Handle #pragma, if any. */
22219 else if (token->type == CPP_PRAGMA)
22220 cp_parser_pragma (parser, pragma_external);
22221 /* Allow stray semicolons. */
22222 else if (token->type == CPP_SEMICOLON)
22223 cp_lexer_consume_token (parser->lexer);
22224 /* Mark methods as optional or required, when building protocols. */
22225 else if (token->keyword == RID_AT_OPTIONAL)
22227 cp_lexer_consume_token (parser->lexer);
22228 objc_set_method_opt (true);
22230 else if (token->keyword == RID_AT_REQUIRED)
22232 cp_lexer_consume_token (parser->lexer);
22233 objc_set_method_opt (false);
22235 else if (token->keyword == RID_NAMESPACE)
22236 cp_parser_namespace_definition (parser);
22237 /* Other stray characters must generate errors. */
22238 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22240 cp_lexer_consume_token (parser->lexer);
22241 error ("stray %qs between Objective-C++ methods",
22242 token->type == CPP_OPEN_BRACE ? "{" : "}");
22244 /* Finally, try to parse a block-declaration, or a function-definition. */
22246 cp_parser_block_declaration (parser, /*statement_p=*/false);
22249 /* Parse a method signature. */
22252 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22254 tree rettype, kwdparms, optparms;
22255 bool ellipsis = false;
22256 bool is_class_method;
22258 is_class_method = cp_parser_objc_method_type (parser);
22259 rettype = cp_parser_objc_typename (parser);
22260 *attributes = NULL_TREE;
22261 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22262 if (kwdparms == error_mark_node)
22263 return error_mark_node;
22264 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22265 if (optparms == error_mark_node)
22266 return error_mark_node;
22268 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22272 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22275 cp_lexer_save_tokens (parser->lexer);
22276 tattr = cp_parser_attributes_opt (parser);
22277 gcc_assert (tattr) ;
22279 /* If the attributes are followed by a method introducer, this is not allowed.
22280 Dump the attributes and flag the situation. */
22281 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22282 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22285 /* Otherwise, the attributes introduce some interstitial code, possibly so
22286 rewind to allow that check. */
22287 cp_lexer_rollback_tokens (parser->lexer);
22291 /* Parse an Objective-C method prototype list. */
22294 cp_parser_objc_method_prototype_list (cp_parser* parser)
22296 cp_token *token = cp_lexer_peek_token (parser->lexer);
22298 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22300 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22302 tree attributes, sig;
22303 bool is_class_method;
22304 if (token->type == CPP_PLUS)
22305 is_class_method = true;
22307 is_class_method = false;
22308 sig = cp_parser_objc_method_signature (parser, &attributes);
22309 if (sig == error_mark_node)
22311 cp_parser_skip_to_end_of_block_or_statement (parser);
22312 token = cp_lexer_peek_token (parser->lexer);
22315 objc_add_method_declaration (is_class_method, sig, attributes);
22316 cp_parser_consume_semicolon_at_end_of_statement (parser);
22318 else if (token->keyword == RID_AT_PROPERTY)
22319 cp_parser_objc_at_property_declaration (parser);
22320 else if (token->keyword == RID_ATTRIBUTE
22321 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22322 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22324 "prefix attributes are ignored for methods");
22326 /* Allow for interspersed non-ObjC++ code. */
22327 cp_parser_objc_interstitial_code (parser);
22329 token = cp_lexer_peek_token (parser->lexer);
22332 if (token->type != CPP_EOF)
22333 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22335 cp_parser_error (parser, "expected %<@end%>");
22337 objc_finish_interface ();
22340 /* Parse an Objective-C method definition list. */
22343 cp_parser_objc_method_definition_list (cp_parser* parser)
22345 cp_token *token = cp_lexer_peek_token (parser->lexer);
22347 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22351 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22354 tree sig, attribute;
22355 bool is_class_method;
22356 if (token->type == CPP_PLUS)
22357 is_class_method = true;
22359 is_class_method = false;
22360 push_deferring_access_checks (dk_deferred);
22361 sig = cp_parser_objc_method_signature (parser, &attribute);
22362 if (sig == error_mark_node)
22364 cp_parser_skip_to_end_of_block_or_statement (parser);
22365 token = cp_lexer_peek_token (parser->lexer);
22368 objc_start_method_definition (is_class_method, sig, attribute,
22371 /* For historical reasons, we accept an optional semicolon. */
22372 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22373 cp_lexer_consume_token (parser->lexer);
22375 ptk = cp_lexer_peek_token (parser->lexer);
22376 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22377 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22379 perform_deferred_access_checks ();
22380 stop_deferring_access_checks ();
22381 meth = cp_parser_function_definition_after_declarator (parser,
22383 pop_deferring_access_checks ();
22384 objc_finish_method_definition (meth);
22387 /* The following case will be removed once @synthesize is
22388 completely implemented. */
22389 else if (token->keyword == RID_AT_PROPERTY)
22390 cp_parser_objc_at_property_declaration (parser);
22391 else if (token->keyword == RID_AT_SYNTHESIZE)
22392 cp_parser_objc_at_synthesize_declaration (parser);
22393 else if (token->keyword == RID_AT_DYNAMIC)
22394 cp_parser_objc_at_dynamic_declaration (parser);
22395 else if (token->keyword == RID_ATTRIBUTE
22396 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22397 warning_at (token->location, OPT_Wattributes,
22398 "prefix attributes are ignored for methods");
22400 /* Allow for interspersed non-ObjC++ code. */
22401 cp_parser_objc_interstitial_code (parser);
22403 token = cp_lexer_peek_token (parser->lexer);
22406 if (token->type != CPP_EOF)
22407 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22409 cp_parser_error (parser, "expected %<@end%>");
22411 objc_finish_implementation ();
22414 /* Parse Objective-C ivars. */
22417 cp_parser_objc_class_ivars (cp_parser* parser)
22419 cp_token *token = cp_lexer_peek_token (parser->lexer);
22421 if (token->type != CPP_OPEN_BRACE)
22422 return; /* No ivars specified. */
22424 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22425 token = cp_lexer_peek_token (parser->lexer);
22427 while (token->type != CPP_CLOSE_BRACE
22428 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22430 cp_decl_specifier_seq declspecs;
22431 int decl_class_or_enum_p;
22432 tree prefix_attributes;
22434 cp_parser_objc_visibility_spec (parser);
22436 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22439 cp_parser_decl_specifier_seq (parser,
22440 CP_PARSER_FLAGS_OPTIONAL,
22442 &decl_class_or_enum_p);
22444 /* auto, register, static, extern, mutable. */
22445 if (declspecs.storage_class != sc_none)
22447 cp_parser_error (parser, "invalid type for instance variable");
22448 declspecs.storage_class = sc_none;
22452 if (declspecs.specs[(int) ds_thread])
22454 cp_parser_error (parser, "invalid type for instance variable");
22455 declspecs.specs[(int) ds_thread] = 0;
22459 if (declspecs.specs[(int) ds_typedef])
22461 cp_parser_error (parser, "invalid type for instance variable");
22462 declspecs.specs[(int) ds_typedef] = 0;
22465 prefix_attributes = declspecs.attributes;
22466 declspecs.attributes = NULL_TREE;
22468 /* Keep going until we hit the `;' at the end of the
22470 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22472 tree width = NULL_TREE, attributes, first_attribute, decl;
22473 cp_declarator *declarator = NULL;
22474 int ctor_dtor_or_conv_p;
22476 /* Check for a (possibly unnamed) bitfield declaration. */
22477 token = cp_lexer_peek_token (parser->lexer);
22478 if (token->type == CPP_COLON)
22481 if (token->type == CPP_NAME
22482 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22485 /* Get the name of the bitfield. */
22486 declarator = make_id_declarator (NULL_TREE,
22487 cp_parser_identifier (parser),
22491 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22492 /* Get the width of the bitfield. */
22494 = cp_parser_constant_expression (parser,
22495 /*allow_non_constant=*/false,
22500 /* Parse the declarator. */
22502 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22503 &ctor_dtor_or_conv_p,
22504 /*parenthesized_p=*/NULL,
22505 /*member_p=*/false);
22508 /* Look for attributes that apply to the ivar. */
22509 attributes = cp_parser_attributes_opt (parser);
22510 /* Remember which attributes are prefix attributes and
22512 first_attribute = attributes;
22513 /* Combine the attributes. */
22514 attributes = chainon (prefix_attributes, attributes);
22517 /* Create the bitfield declaration. */
22518 decl = grokbitfield (declarator, &declspecs,
22522 decl = grokfield (declarator, &declspecs,
22523 NULL_TREE, /*init_const_expr_p=*/false,
22524 NULL_TREE, attributes);
22526 /* Add the instance variable. */
22527 if (decl != error_mark_node && decl != NULL_TREE)
22528 objc_add_instance_variable (decl);
22530 /* Reset PREFIX_ATTRIBUTES. */
22531 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22532 attributes = TREE_CHAIN (attributes);
22534 TREE_CHAIN (attributes) = NULL_TREE;
22536 token = cp_lexer_peek_token (parser->lexer);
22538 if (token->type == CPP_COMMA)
22540 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22546 cp_parser_consume_semicolon_at_end_of_statement (parser);
22547 token = cp_lexer_peek_token (parser->lexer);
22550 if (token->keyword == RID_AT_END)
22551 cp_parser_error (parser, "expected %<}%>");
22553 /* Do not consume the RID_AT_END, so it will be read again as terminating
22554 the @interface of @implementation. */
22555 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22556 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22558 /* For historical reasons, we accept an optional semicolon. */
22559 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22560 cp_lexer_consume_token (parser->lexer);
22563 /* Parse an Objective-C protocol declaration. */
22566 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22568 tree proto, protorefs;
22571 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22572 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22574 tok = cp_lexer_peek_token (parser->lexer);
22575 error_at (tok->location, "identifier expected after %<@protocol%>");
22576 cp_parser_consume_semicolon_at_end_of_statement (parser);
22580 /* See if we have a forward declaration or a definition. */
22581 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22583 /* Try a forward declaration first. */
22584 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22590 id = cp_parser_identifier (parser);
22591 if (id == error_mark_node)
22594 objc_declare_protocol (id, attributes);
22596 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22597 cp_lexer_consume_token (parser->lexer);
22601 cp_parser_consume_semicolon_at_end_of_statement (parser);
22604 /* Ok, we got a full-fledged definition (or at least should). */
22607 proto = cp_parser_identifier (parser);
22608 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22609 objc_start_protocol (proto, protorefs, attributes);
22610 cp_parser_objc_method_prototype_list (parser);
22614 /* Parse an Objective-C superclass or category. */
22617 cp_parser_objc_superclass_or_category (cp_parser *parser,
22620 tree *categ, bool *is_class_extension)
22622 cp_token *next = cp_lexer_peek_token (parser->lexer);
22624 *super = *categ = NULL_TREE;
22625 *is_class_extension = false;
22626 if (next->type == CPP_COLON)
22628 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22629 *super = cp_parser_identifier (parser);
22631 else if (next->type == CPP_OPEN_PAREN)
22633 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22635 /* If there is no category name, and this is an @interface, we
22636 have a class extension. */
22637 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22639 *categ = NULL_TREE;
22640 *is_class_extension = true;
22643 *categ = cp_parser_identifier (parser);
22645 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22649 /* Parse an Objective-C class interface. */
22652 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22654 tree name, super, categ, protos;
22655 bool is_class_extension;
22657 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22658 name = cp_parser_identifier (parser);
22659 if (name == error_mark_node)
22661 /* It's hard to recover because even if valid @interface stuff
22662 is to follow, we can't compile it (or validate it) if we
22663 don't even know which class it refers to. Let's assume this
22664 was a stray '@interface' token in the stream and skip it.
22668 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22669 &is_class_extension);
22670 protos = cp_parser_objc_protocol_refs_opt (parser);
22672 /* We have either a class or a category on our hands. */
22673 if (categ || is_class_extension)
22674 objc_start_category_interface (name, categ, protos, attributes);
22677 objc_start_class_interface (name, super, protos, attributes);
22678 /* Handle instance variable declarations, if any. */
22679 cp_parser_objc_class_ivars (parser);
22680 objc_continue_interface ();
22683 cp_parser_objc_method_prototype_list (parser);
22686 /* Parse an Objective-C class implementation. */
22689 cp_parser_objc_class_implementation (cp_parser* parser)
22691 tree name, super, categ;
22692 bool is_class_extension;
22694 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22695 name = cp_parser_identifier (parser);
22696 if (name == error_mark_node)
22698 /* It's hard to recover because even if valid @implementation
22699 stuff is to follow, we can't compile it (or validate it) if
22700 we don't even know which class it refers to. Let's assume
22701 this was a stray '@implementation' token in the stream and
22706 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22707 &is_class_extension);
22709 /* We have either a class or a category on our hands. */
22711 objc_start_category_implementation (name, categ);
22714 objc_start_class_implementation (name, super);
22715 /* Handle instance variable declarations, if any. */
22716 cp_parser_objc_class_ivars (parser);
22717 objc_continue_implementation ();
22720 cp_parser_objc_method_definition_list (parser);
22723 /* Consume the @end token and finish off the implementation. */
22726 cp_parser_objc_end_implementation (cp_parser* parser)
22728 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22729 objc_finish_implementation ();
22732 /* Parse an Objective-C declaration. */
22735 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22737 /* Try to figure out what kind of declaration is present. */
22738 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22741 switch (kwd->keyword)
22746 error_at (kwd->location, "attributes may not be specified before"
22747 " the %<@%D%> Objective-C++ keyword",
22751 case RID_AT_IMPLEMENTATION:
22752 warning_at (kwd->location, OPT_Wattributes,
22753 "prefix attributes are ignored before %<@%D%>",
22760 switch (kwd->keyword)
22763 cp_parser_objc_alias_declaration (parser);
22766 cp_parser_objc_class_declaration (parser);
22768 case RID_AT_PROTOCOL:
22769 cp_parser_objc_protocol_declaration (parser, attributes);
22771 case RID_AT_INTERFACE:
22772 cp_parser_objc_class_interface (parser, attributes);
22774 case RID_AT_IMPLEMENTATION:
22775 cp_parser_objc_class_implementation (parser);
22778 cp_parser_objc_end_implementation (parser);
22781 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22783 cp_parser_skip_to_end_of_block_or_statement (parser);
22787 /* Parse an Objective-C try-catch-finally statement.
22789 objc-try-catch-finally-stmt:
22790 @try compound-statement objc-catch-clause-seq [opt]
22791 objc-finally-clause [opt]
22793 objc-catch-clause-seq:
22794 objc-catch-clause objc-catch-clause-seq [opt]
22797 @catch ( objc-exception-declaration ) compound-statement
22799 objc-finally-clause:
22800 @finally compound-statement
22802 objc-exception-declaration:
22803 parameter-declaration
22806 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22810 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22811 for C. Keep them in sync. */
22814 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22816 location_t location;
22819 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22820 location = cp_lexer_peek_token (parser->lexer)->location;
22821 objc_maybe_warn_exceptions (location);
22822 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22823 node, lest it get absorbed into the surrounding block. */
22824 stmt = push_stmt_list ();
22825 cp_parser_compound_statement (parser, NULL, false, false);
22826 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22828 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22830 cp_parameter_declarator *parm;
22831 tree parameter_declaration = error_mark_node;
22832 bool seen_open_paren = false;
22834 cp_lexer_consume_token (parser->lexer);
22835 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22836 seen_open_paren = true;
22837 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22839 /* We have "@catch (...)" (where the '...' are literally
22840 what is in the code). Skip the '...'.
22841 parameter_declaration is set to NULL_TREE, and
22842 objc_being_catch_clauses() knows that that means
22844 cp_lexer_consume_token (parser->lexer);
22845 parameter_declaration = NULL_TREE;
22849 /* We have "@catch (NSException *exception)" or something
22850 like that. Parse the parameter declaration. */
22851 parm = cp_parser_parameter_declaration (parser, false, NULL);
22853 parameter_declaration = error_mark_node;
22855 parameter_declaration = grokdeclarator (parm->declarator,
22856 &parm->decl_specifiers,
22857 PARM, /*initialized=*/0,
22858 /*attrlist=*/NULL);
22860 if (seen_open_paren)
22861 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22864 /* If there was no open parenthesis, we are recovering from
22865 an error, and we are trying to figure out what mistake
22866 the user has made. */
22868 /* If there is an immediate closing parenthesis, the user
22869 probably forgot the opening one (ie, they typed "@catch
22870 NSException *e)". Parse the closing parenthesis and keep
22872 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22873 cp_lexer_consume_token (parser->lexer);
22875 /* If these is no immediate closing parenthesis, the user
22876 probably doesn't know that parenthesis are required at
22877 all (ie, they typed "@catch NSException *e"). So, just
22878 forget about the closing parenthesis and keep going. */
22880 objc_begin_catch_clause (parameter_declaration);
22881 cp_parser_compound_statement (parser, NULL, false, false);
22882 objc_finish_catch_clause ();
22884 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22886 cp_lexer_consume_token (parser->lexer);
22887 location = cp_lexer_peek_token (parser->lexer)->location;
22888 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22889 node, lest it get absorbed into the surrounding block. */
22890 stmt = push_stmt_list ();
22891 cp_parser_compound_statement (parser, NULL, false, false);
22892 objc_build_finally_clause (location, pop_stmt_list (stmt));
22895 return objc_finish_try_stmt ();
22898 /* Parse an Objective-C synchronized statement.
22900 objc-synchronized-stmt:
22901 @synchronized ( expression ) compound-statement
22903 Returns NULL_TREE. */
22906 cp_parser_objc_synchronized_statement (cp_parser *parser)
22908 location_t location;
22911 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22913 location = cp_lexer_peek_token (parser->lexer)->location;
22914 objc_maybe_warn_exceptions (location);
22915 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22916 lock = cp_parser_expression (parser, false, NULL);
22917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22919 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22920 node, lest it get absorbed into the surrounding block. */
22921 stmt = push_stmt_list ();
22922 cp_parser_compound_statement (parser, NULL, false, false);
22924 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22927 /* Parse an Objective-C throw statement.
22930 @throw assignment-expression [opt] ;
22932 Returns a constructed '@throw' statement. */
22935 cp_parser_objc_throw_statement (cp_parser *parser)
22937 tree expr = NULL_TREE;
22938 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22940 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22942 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22943 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22945 cp_parser_consume_semicolon_at_end_of_statement (parser);
22947 return objc_build_throw_stmt (loc, expr);
22950 /* Parse an Objective-C statement. */
22953 cp_parser_objc_statement (cp_parser * parser)
22955 /* Try to figure out what kind of declaration is present. */
22956 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22958 switch (kwd->keyword)
22961 return cp_parser_objc_try_catch_finally_statement (parser);
22962 case RID_AT_SYNCHRONIZED:
22963 return cp_parser_objc_synchronized_statement (parser);
22965 return cp_parser_objc_throw_statement (parser);
22967 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22969 cp_parser_skip_to_end_of_block_or_statement (parser);
22972 return error_mark_node;
22975 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22976 look ahead to see if an objc keyword follows the attributes. This
22977 is to detect the use of prefix attributes on ObjC @interface and
22981 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22983 cp_lexer_save_tokens (parser->lexer);
22984 *attrib = cp_parser_attributes_opt (parser);
22985 gcc_assert (*attrib);
22986 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22988 cp_lexer_commit_tokens (parser->lexer);
22991 cp_lexer_rollback_tokens (parser->lexer);
22995 /* This routine is a minimal replacement for
22996 c_parser_struct_declaration () used when parsing the list of
22997 types/names or ObjC++ properties. For example, when parsing the
23000 @property (readonly) int a, b, c;
23002 this function is responsible for parsing "int a, int b, int c" and
23003 returning the declarations as CHAIN of DECLs.
23005 TODO: Share this code with cp_parser_objc_class_ivars. It's very
23006 similar parsing. */
23008 cp_parser_objc_struct_declaration (cp_parser *parser)
23010 tree decls = NULL_TREE;
23011 cp_decl_specifier_seq declspecs;
23012 int decl_class_or_enum_p;
23013 tree prefix_attributes;
23015 cp_parser_decl_specifier_seq (parser,
23016 CP_PARSER_FLAGS_NONE,
23018 &decl_class_or_enum_p);
23020 if (declspecs.type == error_mark_node)
23021 return error_mark_node;
23023 /* auto, register, static, extern, mutable. */
23024 if (declspecs.storage_class != sc_none)
23026 cp_parser_error (parser, "invalid type for property");
23027 declspecs.storage_class = sc_none;
23031 if (declspecs.specs[(int) ds_thread])
23033 cp_parser_error (parser, "invalid type for property");
23034 declspecs.specs[(int) ds_thread] = 0;
23038 if (declspecs.specs[(int) ds_typedef])
23040 cp_parser_error (parser, "invalid type for property");
23041 declspecs.specs[(int) ds_typedef] = 0;
23044 prefix_attributes = declspecs.attributes;
23045 declspecs.attributes = NULL_TREE;
23047 /* Keep going until we hit the `;' at the end of the declaration. */
23048 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23050 tree attributes, first_attribute, decl;
23051 cp_declarator *declarator;
23054 /* Parse the declarator. */
23055 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23056 NULL, NULL, false);
23058 /* Look for attributes that apply to the ivar. */
23059 attributes = cp_parser_attributes_opt (parser);
23060 /* Remember which attributes are prefix attributes and
23062 first_attribute = attributes;
23063 /* Combine the attributes. */
23064 attributes = chainon (prefix_attributes, attributes);
23066 decl = grokfield (declarator, &declspecs,
23067 NULL_TREE, /*init_const_expr_p=*/false,
23068 NULL_TREE, attributes);
23070 if (decl == error_mark_node || decl == NULL_TREE)
23071 return error_mark_node;
23073 /* Reset PREFIX_ATTRIBUTES. */
23074 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23075 attributes = TREE_CHAIN (attributes);
23077 TREE_CHAIN (attributes) = NULL_TREE;
23079 DECL_CHAIN (decl) = decls;
23082 token = cp_lexer_peek_token (parser->lexer);
23083 if (token->type == CPP_COMMA)
23085 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23094 /* Parse an Objective-C @property declaration. The syntax is:
23096 objc-property-declaration:
23097 '@property' objc-property-attributes[opt] struct-declaration ;
23099 objc-property-attributes:
23100 '(' objc-property-attribute-list ')'
23102 objc-property-attribute-list:
23103 objc-property-attribute
23104 objc-property-attribute-list, objc-property-attribute
23106 objc-property-attribute
23107 'getter' = identifier
23108 'setter' = identifier
23117 @property NSString *name;
23118 @property (readonly) id object;
23119 @property (retain, nonatomic, getter=getTheName) id name;
23120 @property int a, b, c;
23122 PS: This function is identical to
23123 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23125 cp_parser_objc_at_property_declaration (cp_parser *parser)
23127 /* The following variables hold the attributes of the properties as
23128 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23129 seen. When we see an attribute, we set them to 'true' (if they
23130 are boolean properties) or to the identifier (if they have an
23131 argument, ie, for getter and setter). Note that here we only
23132 parse the list of attributes, check the syntax and accumulate the
23133 attributes that we find. objc_add_property_declaration() will
23134 then process the information. */
23135 bool property_assign = false;
23136 bool property_copy = false;
23137 tree property_getter_ident = NULL_TREE;
23138 bool property_nonatomic = false;
23139 bool property_readonly = false;
23140 bool property_readwrite = false;
23141 bool property_retain = false;
23142 tree property_setter_ident = NULL_TREE;
23144 /* 'properties' is the list of properties that we read. Usually a
23145 single one, but maybe more (eg, in "@property int a, b, c;" there
23150 loc = cp_lexer_peek_token (parser->lexer)->location;
23152 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23154 /* Parse the optional attribute list... */
23155 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23158 cp_lexer_consume_token (parser->lexer);
23162 bool syntax_error = false;
23163 cp_token *token = cp_lexer_peek_token (parser->lexer);
23166 if (token->type != CPP_NAME)
23168 cp_parser_error (parser, "expected identifier");
23171 keyword = C_RID_CODE (token->u.value);
23172 cp_lexer_consume_token (parser->lexer);
23175 case RID_ASSIGN: property_assign = true; break;
23176 case RID_COPY: property_copy = true; break;
23177 case RID_NONATOMIC: property_nonatomic = true; break;
23178 case RID_READONLY: property_readonly = true; break;
23179 case RID_READWRITE: property_readwrite = true; break;
23180 case RID_RETAIN: property_retain = true; break;
23184 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23186 if (keyword == RID_GETTER)
23187 cp_parser_error (parser,
23188 "missing %<=%> (after %<getter%> attribute)");
23190 cp_parser_error (parser,
23191 "missing %<=%> (after %<setter%> attribute)");
23192 syntax_error = true;
23195 cp_lexer_consume_token (parser->lexer); /* eat the = */
23196 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23198 cp_parser_error (parser, "expected identifier");
23199 syntax_error = true;
23202 if (keyword == RID_SETTER)
23204 if (property_setter_ident != NULL_TREE)
23206 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23207 cp_lexer_consume_token (parser->lexer);
23210 property_setter_ident = cp_parser_objc_selector (parser);
23211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23212 cp_parser_error (parser, "setter name must terminate with %<:%>");
23214 cp_lexer_consume_token (parser->lexer);
23218 if (property_getter_ident != NULL_TREE)
23220 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23221 cp_lexer_consume_token (parser->lexer);
23224 property_getter_ident = cp_parser_objc_selector (parser);
23228 cp_parser_error (parser, "unknown property attribute");
23229 syntax_error = true;
23236 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23237 cp_lexer_consume_token (parser->lexer);
23242 /* FIXME: "@property (setter, assign);" will generate a spurious
23243 "error: expected ‘)’ before ‘,’ token". This is because
23244 cp_parser_require, unlike the C counterpart, will produce an
23245 error even if we are in error recovery. */
23246 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23248 cp_parser_skip_to_closing_parenthesis (parser,
23249 /*recovering=*/true,
23250 /*or_comma=*/false,
23251 /*consume_paren=*/true);
23255 /* ... and the property declaration(s). */
23256 properties = cp_parser_objc_struct_declaration (parser);
23258 if (properties == error_mark_node)
23260 cp_parser_skip_to_end_of_statement (parser);
23261 /* If the next token is now a `;', consume it. */
23262 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23263 cp_lexer_consume_token (parser->lexer);
23267 if (properties == NULL_TREE)
23268 cp_parser_error (parser, "expected identifier");
23271 /* Comma-separated properties are chained together in
23272 reverse order; add them one by one. */
23273 properties = nreverse (properties);
23275 for (; properties; properties = TREE_CHAIN (properties))
23276 objc_add_property_declaration (loc, copy_node (properties),
23277 property_readonly, property_readwrite,
23278 property_assign, property_retain,
23279 property_copy, property_nonatomic,
23280 property_getter_ident, property_setter_ident);
23283 cp_parser_consume_semicolon_at_end_of_statement (parser);
23286 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23288 objc-synthesize-declaration:
23289 @synthesize objc-synthesize-identifier-list ;
23291 objc-synthesize-identifier-list:
23292 objc-synthesize-identifier
23293 objc-synthesize-identifier-list, objc-synthesize-identifier
23295 objc-synthesize-identifier
23297 identifier = identifier
23300 @synthesize MyProperty;
23301 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23303 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23304 for C. Keep them in sync.
23307 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23309 tree list = NULL_TREE;
23311 loc = cp_lexer_peek_token (parser->lexer)->location;
23313 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23316 tree property, ivar;
23317 property = cp_parser_identifier (parser);
23318 if (property == error_mark_node)
23320 cp_parser_consume_semicolon_at_end_of_statement (parser);
23323 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23325 cp_lexer_consume_token (parser->lexer);
23326 ivar = cp_parser_identifier (parser);
23327 if (ivar == error_mark_node)
23329 cp_parser_consume_semicolon_at_end_of_statement (parser);
23335 list = chainon (list, build_tree_list (ivar, property));
23336 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23337 cp_lexer_consume_token (parser->lexer);
23341 cp_parser_consume_semicolon_at_end_of_statement (parser);
23342 objc_add_synthesize_declaration (loc, list);
23345 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23347 objc-dynamic-declaration:
23348 @dynamic identifier-list ;
23351 @dynamic MyProperty;
23352 @dynamic MyProperty, AnotherProperty;
23354 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23355 for C. Keep them in sync.
23358 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23360 tree list = NULL_TREE;
23362 loc = cp_lexer_peek_token (parser->lexer)->location;
23364 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23368 property = cp_parser_identifier (parser);
23369 if (property == error_mark_node)
23371 cp_parser_consume_semicolon_at_end_of_statement (parser);
23374 list = chainon (list, build_tree_list (NULL, property));
23375 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23376 cp_lexer_consume_token (parser->lexer);
23380 cp_parser_consume_semicolon_at_end_of_statement (parser);
23381 objc_add_dynamic_declaration (loc, list);
23385 /* OpenMP 2.5 parsing routines. */
23387 /* Returns name of the next clause.
23388 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23389 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23390 returned and the token is consumed. */
23392 static pragma_omp_clause
23393 cp_parser_omp_clause_name (cp_parser *parser)
23395 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23397 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23398 result = PRAGMA_OMP_CLAUSE_IF;
23399 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23400 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23401 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23402 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23403 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23405 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23406 const char *p = IDENTIFIER_POINTER (id);
23411 if (!strcmp ("collapse", p))
23412 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23413 else if (!strcmp ("copyin", p))
23414 result = PRAGMA_OMP_CLAUSE_COPYIN;
23415 else if (!strcmp ("copyprivate", p))
23416 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23419 if (!strcmp ("firstprivate", p))
23420 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23423 if (!strcmp ("lastprivate", p))
23424 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23427 if (!strcmp ("nowait", p))
23428 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23429 else if (!strcmp ("num_threads", p))
23430 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23433 if (!strcmp ("ordered", p))
23434 result = PRAGMA_OMP_CLAUSE_ORDERED;
23437 if (!strcmp ("reduction", p))
23438 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23441 if (!strcmp ("schedule", p))
23442 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23443 else if (!strcmp ("shared", p))
23444 result = PRAGMA_OMP_CLAUSE_SHARED;
23447 if (!strcmp ("untied", p))
23448 result = PRAGMA_OMP_CLAUSE_UNTIED;
23453 if (result != PRAGMA_OMP_CLAUSE_NONE)
23454 cp_lexer_consume_token (parser->lexer);
23459 /* Validate that a clause of the given type does not already exist. */
23462 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23463 const char *name, location_t location)
23467 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23468 if (OMP_CLAUSE_CODE (c) == code)
23470 error_at (location, "too many %qs clauses", name);
23478 variable-list , identifier
23480 In addition, we match a closing parenthesis. An opening parenthesis
23481 will have been consumed by the caller.
23483 If KIND is nonzero, create the appropriate node and install the decl
23484 in OMP_CLAUSE_DECL and add the node to the head of the list.
23486 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23487 return the list created. */
23490 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23498 token = cp_lexer_peek_token (parser->lexer);
23499 name = cp_parser_id_expression (parser, /*template_p=*/false,
23500 /*check_dependency_p=*/true,
23501 /*template_p=*/NULL,
23502 /*declarator_p=*/false,
23503 /*optional_p=*/false);
23504 if (name == error_mark_node)
23507 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23508 if (decl == error_mark_node)
23509 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23511 else if (kind != 0)
23513 tree u = build_omp_clause (token->location, kind);
23514 OMP_CLAUSE_DECL (u) = decl;
23515 OMP_CLAUSE_CHAIN (u) = list;
23519 list = tree_cons (decl, NULL_TREE, list);
23522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23524 cp_lexer_consume_token (parser->lexer);
23527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23531 /* Try to resync to an unnested comma. Copied from
23532 cp_parser_parenthesized_expression_list. */
23534 ending = cp_parser_skip_to_closing_parenthesis (parser,
23535 /*recovering=*/true,
23537 /*consume_paren=*/true);
23545 /* Similarly, but expect leading and trailing parenthesis. This is a very
23546 common case for omp clauses. */
23549 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23551 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23552 return cp_parser_omp_var_list_no_open (parser, kind, list);
23557 collapse ( constant-expression ) */
23560 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23566 loc = cp_lexer_peek_token (parser->lexer)->location;
23567 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23570 num = cp_parser_constant_expression (parser, false, NULL);
23572 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23573 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23574 /*or_comma=*/false,
23575 /*consume_paren=*/true);
23577 if (num == error_mark_node)
23579 num = fold_non_dependent_expr (num);
23580 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23581 || !host_integerp (num, 0)
23582 || (n = tree_low_cst (num, 0)) <= 0
23585 error_at (loc, "collapse argument needs positive constant integer expression");
23589 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23590 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23591 OMP_CLAUSE_CHAIN (c) = list;
23592 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23598 default ( shared | none ) */
23601 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23603 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23606 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23608 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23610 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23611 const char *p = IDENTIFIER_POINTER (id);
23616 if (strcmp ("none", p) != 0)
23618 kind = OMP_CLAUSE_DEFAULT_NONE;
23622 if (strcmp ("shared", p) != 0)
23624 kind = OMP_CLAUSE_DEFAULT_SHARED;
23631 cp_lexer_consume_token (parser->lexer);
23636 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23639 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23640 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23641 /*or_comma=*/false,
23642 /*consume_paren=*/true);
23644 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23647 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23648 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23649 OMP_CLAUSE_CHAIN (c) = list;
23650 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23656 if ( expression ) */
23659 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23663 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23666 t = cp_parser_condition (parser);
23668 if (t == error_mark_node
23669 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23670 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23671 /*or_comma=*/false,
23672 /*consume_paren=*/true);
23674 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23676 c = build_omp_clause (location, OMP_CLAUSE_IF);
23677 OMP_CLAUSE_IF_EXPR (c) = t;
23678 OMP_CLAUSE_CHAIN (c) = list;
23687 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23688 tree list, location_t location)
23692 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23694 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23695 OMP_CLAUSE_CHAIN (c) = list;
23700 num_threads ( expression ) */
23703 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23704 location_t location)
23708 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23711 t = cp_parser_expression (parser, false, NULL);
23713 if (t == error_mark_node
23714 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23715 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23716 /*or_comma=*/false,
23717 /*consume_paren=*/true);
23719 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23720 "num_threads", location);
23722 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23723 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23724 OMP_CLAUSE_CHAIN (c) = list;
23733 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23734 tree list, location_t location)
23738 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23739 "ordered", location);
23741 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23742 OMP_CLAUSE_CHAIN (c) = list;
23747 reduction ( reduction-operator : variable-list )
23749 reduction-operator:
23750 One of: + * - & ^ | && || */
23753 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23755 enum tree_code code;
23758 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23761 switch (cp_lexer_peek_token (parser->lexer)->type)
23773 code = BIT_AND_EXPR;
23776 code = BIT_XOR_EXPR;
23779 code = BIT_IOR_EXPR;
23782 code = TRUTH_ANDIF_EXPR;
23785 code = TRUTH_ORIF_EXPR;
23788 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23789 "%<|%>, %<&&%>, or %<||%>");
23791 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23792 /*or_comma=*/false,
23793 /*consume_paren=*/true);
23796 cp_lexer_consume_token (parser->lexer);
23798 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23801 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23802 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23803 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23809 schedule ( schedule-kind )
23810 schedule ( schedule-kind , expression )
23813 static | dynamic | guided | runtime | auto */
23816 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23820 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23823 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23825 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23827 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23828 const char *p = IDENTIFIER_POINTER (id);
23833 if (strcmp ("dynamic", p) != 0)
23835 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23839 if (strcmp ("guided", p) != 0)
23841 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23845 if (strcmp ("runtime", p) != 0)
23847 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23854 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23855 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23856 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23857 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23860 cp_lexer_consume_token (parser->lexer);
23862 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23865 cp_lexer_consume_token (parser->lexer);
23867 token = cp_lexer_peek_token (parser->lexer);
23868 t = cp_parser_assignment_expression (parser, false, NULL);
23870 if (t == error_mark_node)
23872 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23873 error_at (token->location, "schedule %<runtime%> does not take "
23874 "a %<chunk_size%> parameter");
23875 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23876 error_at (token->location, "schedule %<auto%> does not take "
23877 "a %<chunk_size%> parameter");
23879 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23881 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23884 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23887 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23888 OMP_CLAUSE_CHAIN (c) = list;
23892 cp_parser_error (parser, "invalid schedule kind");
23894 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23895 /*or_comma=*/false,
23896 /*consume_paren=*/true);
23904 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23905 tree list, location_t location)
23909 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23911 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23912 OMP_CLAUSE_CHAIN (c) = list;
23916 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23917 is a bitmask in MASK. Return the list of clauses found; the result
23918 of clause default goes in *pdefault. */
23921 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23922 const char *where, cp_token *pragma_tok)
23924 tree clauses = NULL;
23926 cp_token *token = NULL;
23928 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23930 pragma_omp_clause c_kind;
23931 const char *c_name;
23932 tree prev = clauses;
23934 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23935 cp_lexer_consume_token (parser->lexer);
23937 token = cp_lexer_peek_token (parser->lexer);
23938 c_kind = cp_parser_omp_clause_name (parser);
23943 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23944 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23946 c_name = "collapse";
23948 case PRAGMA_OMP_CLAUSE_COPYIN:
23949 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23952 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23953 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23955 c_name = "copyprivate";
23957 case PRAGMA_OMP_CLAUSE_DEFAULT:
23958 clauses = cp_parser_omp_clause_default (parser, clauses,
23960 c_name = "default";
23962 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23963 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23965 c_name = "firstprivate";
23967 case PRAGMA_OMP_CLAUSE_IF:
23968 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23971 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23972 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23974 c_name = "lastprivate";
23976 case PRAGMA_OMP_CLAUSE_NOWAIT:
23977 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23980 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23981 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23983 c_name = "num_threads";
23985 case PRAGMA_OMP_CLAUSE_ORDERED:
23986 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23988 c_name = "ordered";
23990 case PRAGMA_OMP_CLAUSE_PRIVATE:
23991 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23993 c_name = "private";
23995 case PRAGMA_OMP_CLAUSE_REDUCTION:
23996 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23997 c_name = "reduction";
23999 case PRAGMA_OMP_CLAUSE_SCHEDULE:
24000 clauses = cp_parser_omp_clause_schedule (parser, clauses,
24002 c_name = "schedule";
24004 case PRAGMA_OMP_CLAUSE_SHARED:
24005 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24009 case PRAGMA_OMP_CLAUSE_UNTIED:
24010 clauses = cp_parser_omp_clause_untied (parser, clauses,
24015 cp_parser_error (parser, "expected %<#pragma omp%> clause");
24019 if (((mask >> c_kind) & 1) == 0)
24021 /* Remove the invalid clause(s) from the list to avoid
24022 confusing the rest of the compiler. */
24024 error_at (token->location, "%qs is not valid for %qs", c_name, where);
24028 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24029 return finish_omp_clauses (clauses);
24036 In practice, we're also interested in adding the statement to an
24037 outer node. So it is convenient if we work around the fact that
24038 cp_parser_statement calls add_stmt. */
24041 cp_parser_begin_omp_structured_block (cp_parser *parser)
24043 unsigned save = parser->in_statement;
24045 /* Only move the values to IN_OMP_BLOCK if they weren't false.
24046 This preserves the "not within loop or switch" style error messages
24047 for nonsense cases like
24053 if (parser->in_statement)
24054 parser->in_statement = IN_OMP_BLOCK;
24060 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24062 parser->in_statement = save;
24066 cp_parser_omp_structured_block (cp_parser *parser)
24068 tree stmt = begin_omp_structured_block ();
24069 unsigned int save = cp_parser_begin_omp_structured_block (parser);
24071 cp_parser_statement (parser, NULL_TREE, false, NULL);
24073 cp_parser_end_omp_structured_block (parser, save);
24074 return finish_omp_structured_block (stmt);
24078 # pragma omp atomic new-line
24082 x binop= expr | x++ | ++x | x-- | --x
24084 +, *, -, /, &, ^, |, <<, >>
24086 where x is an lvalue expression with scalar type. */
24089 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24092 enum tree_code code;
24094 cp_parser_require_pragma_eol (parser, pragma_tok);
24096 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24097 /*cast_p=*/false, NULL);
24098 switch (TREE_CODE (lhs))
24103 case PREINCREMENT_EXPR:
24104 case POSTINCREMENT_EXPR:
24105 lhs = TREE_OPERAND (lhs, 0);
24107 rhs = integer_one_node;
24110 case PREDECREMENT_EXPR:
24111 case POSTDECREMENT_EXPR:
24112 lhs = TREE_OPERAND (lhs, 0);
24114 rhs = integer_one_node;
24117 case COMPOUND_EXPR:
24118 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24119 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24120 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24121 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24122 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24123 (TREE_OPERAND (lhs, 1), 0), 0)))
24125 /* Undo effects of boolean_increment for post {in,de}crement. */
24126 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24129 if (TREE_CODE (lhs) == MODIFY_EXPR
24130 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24132 /* Undo effects of boolean_increment. */
24133 if (integer_onep (TREE_OPERAND (lhs, 1)))
24135 /* This is pre or post increment. */
24136 rhs = TREE_OPERAND (lhs, 1);
24137 lhs = TREE_OPERAND (lhs, 0);
24144 switch (cp_lexer_peek_token (parser->lexer)->type)
24150 code = TRUNC_DIV_EXPR;
24158 case CPP_LSHIFT_EQ:
24159 code = LSHIFT_EXPR;
24161 case CPP_RSHIFT_EQ:
24162 code = RSHIFT_EXPR;
24165 code = BIT_AND_EXPR;
24168 code = BIT_IOR_EXPR;
24171 code = BIT_XOR_EXPR;
24174 cp_parser_error (parser,
24175 "invalid operator for %<#pragma omp atomic%>");
24178 cp_lexer_consume_token (parser->lexer);
24180 rhs = cp_parser_expression (parser, false, NULL);
24181 if (rhs == error_mark_node)
24185 finish_omp_atomic (code, lhs, rhs);
24186 cp_parser_consume_semicolon_at_end_of_statement (parser);
24190 cp_parser_skip_to_end_of_block_or_statement (parser);
24195 # pragma omp barrier new-line */
24198 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24200 cp_parser_require_pragma_eol (parser, pragma_tok);
24201 finish_omp_barrier ();
24205 # pragma omp critical [(name)] new-line
24206 structured-block */
24209 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24211 tree stmt, name = NULL;
24213 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24215 cp_lexer_consume_token (parser->lexer);
24217 name = cp_parser_identifier (parser);
24219 if (name == error_mark_node
24220 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24221 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24222 /*or_comma=*/false,
24223 /*consume_paren=*/true);
24224 if (name == error_mark_node)
24227 cp_parser_require_pragma_eol (parser, pragma_tok);
24229 stmt = cp_parser_omp_structured_block (parser);
24230 return c_finish_omp_critical (input_location, stmt, name);
24234 # pragma omp flush flush-vars[opt] new-line
24237 ( variable-list ) */
24240 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24242 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24243 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24244 cp_parser_require_pragma_eol (parser, pragma_tok);
24246 finish_omp_flush ();
24249 /* Helper function, to parse omp for increment expression. */
24252 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24254 tree cond = cp_parser_binary_expression (parser, false, true,
24255 PREC_NOT_OPERATOR, NULL);
24256 if (cond == error_mark_node
24257 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24259 cp_parser_skip_to_end_of_statement (parser);
24260 return error_mark_node;
24263 switch (TREE_CODE (cond))
24271 return error_mark_node;
24274 /* If decl is an iterator, preserve LHS and RHS of the relational
24275 expr until finish_omp_for. */
24277 && (type_dependent_expression_p (decl)
24278 || CLASS_TYPE_P (TREE_TYPE (decl))))
24281 return build_x_binary_op (TREE_CODE (cond),
24282 TREE_OPERAND (cond, 0), ERROR_MARK,
24283 TREE_OPERAND (cond, 1), ERROR_MARK,
24284 /*overload=*/NULL, tf_warning_or_error);
24287 /* Helper function, to parse omp for increment expression. */
24290 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24292 cp_token *token = cp_lexer_peek_token (parser->lexer);
24298 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24300 op = (token->type == CPP_PLUS_PLUS
24301 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24302 cp_lexer_consume_token (parser->lexer);
24303 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24305 return error_mark_node;
24306 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24309 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24311 return error_mark_node;
24313 token = cp_lexer_peek_token (parser->lexer);
24314 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24316 op = (token->type == CPP_PLUS_PLUS
24317 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24318 cp_lexer_consume_token (parser->lexer);
24319 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24322 op = cp_parser_assignment_operator_opt (parser);
24323 if (op == ERROR_MARK)
24324 return error_mark_node;
24326 if (op != NOP_EXPR)
24328 rhs = cp_parser_assignment_expression (parser, false, NULL);
24329 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24330 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24333 lhs = cp_parser_binary_expression (parser, false, false,
24334 PREC_ADDITIVE_EXPRESSION, NULL);
24335 token = cp_lexer_peek_token (parser->lexer);
24336 decl_first = lhs == decl;
24339 if (token->type != CPP_PLUS
24340 && token->type != CPP_MINUS)
24341 return error_mark_node;
24345 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24346 cp_lexer_consume_token (parser->lexer);
24347 rhs = cp_parser_binary_expression (parser, false, false,
24348 PREC_ADDITIVE_EXPRESSION, NULL);
24349 token = cp_lexer_peek_token (parser->lexer);
24350 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24352 if (lhs == NULL_TREE)
24354 if (op == PLUS_EXPR)
24357 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24360 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24361 NULL, tf_warning_or_error);
24364 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24368 if (rhs != decl || op == MINUS_EXPR)
24369 return error_mark_node;
24370 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24373 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24375 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24378 /* Parse the restricted form of the for statement allowed by OpenMP. */
24381 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24383 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24384 tree real_decl, initv, condv, incrv, declv;
24385 tree this_pre_body, cl;
24386 location_t loc_first;
24387 bool collapse_err = false;
24388 int i, collapse = 1, nbraces = 0;
24389 VEC(tree,gc) *for_block = make_tree_vector ();
24391 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24392 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24393 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24395 gcc_assert (collapse >= 1);
24397 declv = make_tree_vec (collapse);
24398 initv = make_tree_vec (collapse);
24399 condv = make_tree_vec (collapse);
24400 incrv = make_tree_vec (collapse);
24402 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24404 for (i = 0; i < collapse; i++)
24406 int bracecount = 0;
24407 bool add_private_clause = false;
24410 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24412 cp_parser_error (parser, "for statement expected");
24415 loc = cp_lexer_consume_token (parser->lexer)->location;
24417 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24420 init = decl = real_decl = NULL;
24421 this_pre_body = push_stmt_list ();
24422 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24424 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24428 integer-type var = lb
24429 random-access-iterator-type var = lb
24430 pointer-type var = lb
24432 cp_decl_specifier_seq type_specifiers;
24434 /* First, try to parse as an initialized declaration. See
24435 cp_parser_condition, from whence the bulk of this is copied. */
24437 cp_parser_parse_tentatively (parser);
24438 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24439 /*is_trailing_return=*/false,
24441 if (cp_parser_parse_definitely (parser))
24443 /* If parsing a type specifier seq succeeded, then this
24444 MUST be a initialized declaration. */
24445 tree asm_specification, attributes;
24446 cp_declarator *declarator;
24448 declarator = cp_parser_declarator (parser,
24449 CP_PARSER_DECLARATOR_NAMED,
24450 /*ctor_dtor_or_conv_p=*/NULL,
24451 /*parenthesized_p=*/NULL,
24452 /*member_p=*/false);
24453 attributes = cp_parser_attributes_opt (parser);
24454 asm_specification = cp_parser_asm_specification_opt (parser);
24456 if (declarator == cp_error_declarator)
24457 cp_parser_skip_to_end_of_statement (parser);
24461 tree pushed_scope, auto_node;
24463 decl = start_decl (declarator, &type_specifiers,
24464 SD_INITIALIZED, attributes,
24465 /*prefix_attributes=*/NULL_TREE,
24468 auto_node = type_uses_auto (TREE_TYPE (decl));
24469 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24471 if (cp_lexer_next_token_is (parser->lexer,
24473 error ("parenthesized initialization is not allowed in "
24474 "OpenMP %<for%> loop");
24476 /* Trigger an error. */
24477 cp_parser_require (parser, CPP_EQ, RT_EQ);
24479 init = error_mark_node;
24480 cp_parser_skip_to_end_of_statement (parser);
24482 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24483 || type_dependent_expression_p (decl)
24486 bool is_direct_init, is_non_constant_init;
24488 init = cp_parser_initializer (parser,
24490 &is_non_constant_init);
24492 if (auto_node && describable_type (init))
24495 = do_auto_deduction (TREE_TYPE (decl), init,
24498 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24499 && !type_dependent_expression_p (decl))
24503 cp_finish_decl (decl, init, !is_non_constant_init,
24505 LOOKUP_ONLYCONVERTING);
24506 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24508 VEC_safe_push (tree, gc, for_block, this_pre_body);
24512 init = pop_stmt_list (this_pre_body);
24513 this_pre_body = NULL_TREE;
24518 cp_lexer_consume_token (parser->lexer);
24519 init = cp_parser_assignment_expression (parser, false, NULL);
24522 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24523 init = error_mark_node;
24525 cp_finish_decl (decl, NULL_TREE,
24526 /*init_const_expr_p=*/false,
24528 LOOKUP_ONLYCONVERTING);
24532 pop_scope (pushed_scope);
24538 /* If parsing a type specifier sequence failed, then
24539 this MUST be a simple expression. */
24540 cp_parser_parse_tentatively (parser);
24541 decl = cp_parser_primary_expression (parser, false, false,
24543 if (!cp_parser_error_occurred (parser)
24546 && CLASS_TYPE_P (TREE_TYPE (decl)))
24550 cp_parser_parse_definitely (parser);
24551 cp_parser_require (parser, CPP_EQ, RT_EQ);
24552 rhs = cp_parser_assignment_expression (parser, false, NULL);
24553 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24555 tf_warning_or_error));
24556 add_private_clause = true;
24561 cp_parser_abort_tentative_parse (parser);
24562 init = cp_parser_expression (parser, false, NULL);
24565 if (TREE_CODE (init) == MODIFY_EXPR
24566 || TREE_CODE (init) == MODOP_EXPR)
24567 real_decl = TREE_OPERAND (init, 0);
24572 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24575 this_pre_body = pop_stmt_list (this_pre_body);
24579 pre_body = push_stmt_list ();
24581 add_stmt (this_pre_body);
24582 pre_body = pop_stmt_list (pre_body);
24585 pre_body = this_pre_body;
24590 if (par_clauses != NULL && real_decl != NULL_TREE)
24593 for (c = par_clauses; *c ; )
24594 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24595 && OMP_CLAUSE_DECL (*c) == real_decl)
24597 error_at (loc, "iteration variable %qD"
24598 " should not be firstprivate", real_decl);
24599 *c = OMP_CLAUSE_CHAIN (*c);
24601 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24602 && OMP_CLAUSE_DECL (*c) == real_decl)
24604 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24605 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24606 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24607 OMP_CLAUSE_DECL (l) = real_decl;
24608 OMP_CLAUSE_CHAIN (l) = clauses;
24609 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24611 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24612 CP_OMP_CLAUSE_INFO (*c) = NULL;
24613 add_private_clause = false;
24617 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24618 && OMP_CLAUSE_DECL (*c) == real_decl)
24619 add_private_clause = false;
24620 c = &OMP_CLAUSE_CHAIN (*c);
24624 if (add_private_clause)
24627 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24629 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24630 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24631 && OMP_CLAUSE_DECL (c) == decl)
24633 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24634 && OMP_CLAUSE_DECL (c) == decl)
24635 error_at (loc, "iteration variable %qD "
24636 "should not be firstprivate",
24638 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24639 && OMP_CLAUSE_DECL (c) == decl)
24640 error_at (loc, "iteration variable %qD should not be reduction",
24645 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24646 OMP_CLAUSE_DECL (c) = decl;
24647 c = finish_omp_clauses (c);
24650 OMP_CLAUSE_CHAIN (c) = clauses;
24657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24658 cond = cp_parser_omp_for_cond (parser, decl);
24659 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24662 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24664 /* If decl is an iterator, preserve the operator on decl
24665 until finish_omp_for. */
24667 && ((type_dependent_expression_p (decl)
24668 && !POINTER_TYPE_P (TREE_TYPE (decl)))
24669 || CLASS_TYPE_P (TREE_TYPE (decl))))
24670 incr = cp_parser_omp_for_incr (parser, decl);
24672 incr = cp_parser_expression (parser, false, NULL);
24675 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24676 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24677 /*or_comma=*/false,
24678 /*consume_paren=*/true);
24680 TREE_VEC_ELT (declv, i) = decl;
24681 TREE_VEC_ELT (initv, i) = init;
24682 TREE_VEC_ELT (condv, i) = cond;
24683 TREE_VEC_ELT (incrv, i) = incr;
24685 if (i == collapse - 1)
24688 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24689 in between the collapsed for loops to be still considered perfectly
24690 nested. Hopefully the final version clarifies this.
24691 For now handle (multiple) {'s and empty statements. */
24692 cp_parser_parse_tentatively (parser);
24695 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24697 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24699 cp_lexer_consume_token (parser->lexer);
24702 else if (bracecount
24703 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24704 cp_lexer_consume_token (parser->lexer);
24707 loc = cp_lexer_peek_token (parser->lexer)->location;
24708 error_at (loc, "not enough collapsed for loops");
24709 collapse_err = true;
24710 cp_parser_abort_tentative_parse (parser);
24719 cp_parser_parse_definitely (parser);
24720 nbraces += bracecount;
24724 /* Note that we saved the original contents of this flag when we entered
24725 the structured block, and so we don't need to re-save it here. */
24726 parser->in_statement = IN_OMP_FOR;
24728 /* Note that the grammar doesn't call for a structured block here,
24729 though the loop as a whole is a structured block. */
24730 body = push_stmt_list ();
24731 cp_parser_statement (parser, NULL_TREE, false, NULL);
24732 body = pop_stmt_list (body);
24734 if (declv == NULL_TREE)
24737 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24738 pre_body, clauses);
24742 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24744 cp_lexer_consume_token (parser->lexer);
24747 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24748 cp_lexer_consume_token (parser->lexer);
24753 error_at (cp_lexer_peek_token (parser->lexer)->location,
24754 "collapsed loops not perfectly nested");
24756 collapse_err = true;
24757 cp_parser_statement_seq_opt (parser, NULL);
24758 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24763 while (!VEC_empty (tree, for_block))
24764 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24765 release_tree_vector (for_block);
24771 #pragma omp for for-clause[optseq] new-line
24774 #define OMP_FOR_CLAUSE_MASK \
24775 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24776 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24777 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24778 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24779 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24780 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24781 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24782 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24785 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24787 tree clauses, sb, ret;
24790 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24791 "#pragma omp for", pragma_tok);
24793 sb = begin_omp_structured_block ();
24794 save = cp_parser_begin_omp_structured_block (parser);
24796 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24798 cp_parser_end_omp_structured_block (parser, save);
24799 add_stmt (finish_omp_structured_block (sb));
24805 # pragma omp master new-line
24806 structured-block */
24809 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24811 cp_parser_require_pragma_eol (parser, pragma_tok);
24812 return c_finish_omp_master (input_location,
24813 cp_parser_omp_structured_block (parser));
24817 # pragma omp ordered new-line
24818 structured-block */
24821 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24823 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24824 cp_parser_require_pragma_eol (parser, pragma_tok);
24825 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24831 { section-sequence }
24834 section-directive[opt] structured-block
24835 section-sequence section-directive structured-block */
24838 cp_parser_omp_sections_scope (cp_parser *parser)
24840 tree stmt, substmt;
24841 bool error_suppress = false;
24844 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24847 stmt = push_stmt_list ();
24849 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24853 substmt = begin_omp_structured_block ();
24854 save = cp_parser_begin_omp_structured_block (parser);
24858 cp_parser_statement (parser, NULL_TREE, false, NULL);
24860 tok = cp_lexer_peek_token (parser->lexer);
24861 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24863 if (tok->type == CPP_CLOSE_BRACE)
24865 if (tok->type == CPP_EOF)
24869 cp_parser_end_omp_structured_block (parser, save);
24870 substmt = finish_omp_structured_block (substmt);
24871 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24872 add_stmt (substmt);
24877 tok = cp_lexer_peek_token (parser->lexer);
24878 if (tok->type == CPP_CLOSE_BRACE)
24880 if (tok->type == CPP_EOF)
24883 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24885 cp_lexer_consume_token (parser->lexer);
24886 cp_parser_require_pragma_eol (parser, tok);
24887 error_suppress = false;
24889 else if (!error_suppress)
24891 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24892 error_suppress = true;
24895 substmt = cp_parser_omp_structured_block (parser);
24896 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24897 add_stmt (substmt);
24899 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24901 substmt = pop_stmt_list (stmt);
24903 stmt = make_node (OMP_SECTIONS);
24904 TREE_TYPE (stmt) = void_type_node;
24905 OMP_SECTIONS_BODY (stmt) = substmt;
24912 # pragma omp sections sections-clause[optseq] newline
24915 #define OMP_SECTIONS_CLAUSE_MASK \
24916 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24917 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24918 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24919 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24920 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24923 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24927 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24928 "#pragma omp sections", pragma_tok);
24930 ret = cp_parser_omp_sections_scope (parser);
24932 OMP_SECTIONS_CLAUSES (ret) = clauses;
24938 # pragma parallel parallel-clause new-line
24939 # pragma parallel for parallel-for-clause new-line
24940 # pragma parallel sections parallel-sections-clause new-line */
24942 #define OMP_PARALLEL_CLAUSE_MASK \
24943 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24944 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24945 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24946 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24947 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24948 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24949 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24950 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24953 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24955 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24956 const char *p_name = "#pragma omp parallel";
24957 tree stmt, clauses, par_clause, ws_clause, block;
24958 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24960 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24964 cp_lexer_consume_token (parser->lexer);
24965 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24966 p_name = "#pragma omp parallel for";
24967 mask |= OMP_FOR_CLAUSE_MASK;
24968 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24970 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24972 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24973 const char *p = IDENTIFIER_POINTER (id);
24974 if (strcmp (p, "sections") == 0)
24976 cp_lexer_consume_token (parser->lexer);
24977 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24978 p_name = "#pragma omp parallel sections";
24979 mask |= OMP_SECTIONS_CLAUSE_MASK;
24980 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24984 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24985 block = begin_omp_parallel ();
24986 save = cp_parser_begin_omp_structured_block (parser);
24990 case PRAGMA_OMP_PARALLEL:
24991 cp_parser_statement (parser, NULL_TREE, false, NULL);
24992 par_clause = clauses;
24995 case PRAGMA_OMP_PARALLEL_FOR:
24996 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24997 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25000 case PRAGMA_OMP_PARALLEL_SECTIONS:
25001 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25002 stmt = cp_parser_omp_sections_scope (parser);
25004 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25008 gcc_unreachable ();
25011 cp_parser_end_omp_structured_block (parser, save);
25012 stmt = finish_omp_parallel (par_clause, block);
25013 if (p_kind != PRAGMA_OMP_PARALLEL)
25014 OMP_PARALLEL_COMBINED (stmt) = 1;
25019 # pragma omp single single-clause[optseq] new-line
25020 structured-block */
25022 #define OMP_SINGLE_CLAUSE_MASK \
25023 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25024 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25025 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
25026 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25029 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25031 tree stmt = make_node (OMP_SINGLE);
25032 TREE_TYPE (stmt) = void_type_node;
25034 OMP_SINGLE_CLAUSES (stmt)
25035 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25036 "#pragma omp single", pragma_tok);
25037 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25039 return add_stmt (stmt);
25043 # pragma omp task task-clause[optseq] new-line
25044 structured-block */
25046 #define OMP_TASK_CLAUSE_MASK \
25047 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
25048 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
25049 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
25050 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25051 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25052 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25055 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25057 tree clauses, block;
25060 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25061 "#pragma omp task", pragma_tok);
25062 block = begin_omp_task ();
25063 save = cp_parser_begin_omp_structured_block (parser);
25064 cp_parser_statement (parser, NULL_TREE, false, NULL);
25065 cp_parser_end_omp_structured_block (parser, save);
25066 return finish_omp_task (clauses, block);
25070 # pragma omp taskwait new-line */
25073 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25075 cp_parser_require_pragma_eol (parser, pragma_tok);
25076 finish_omp_taskwait ();
25080 # pragma omp threadprivate (variable-list) */
25083 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25087 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25088 cp_parser_require_pragma_eol (parser, pragma_tok);
25090 finish_omp_threadprivate (vars);
25093 /* Main entry point to OpenMP statement pragmas. */
25096 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25100 switch (pragma_tok->pragma_kind)
25102 case PRAGMA_OMP_ATOMIC:
25103 cp_parser_omp_atomic (parser, pragma_tok);
25105 case PRAGMA_OMP_CRITICAL:
25106 stmt = cp_parser_omp_critical (parser, pragma_tok);
25108 case PRAGMA_OMP_FOR:
25109 stmt = cp_parser_omp_for (parser, pragma_tok);
25111 case PRAGMA_OMP_MASTER:
25112 stmt = cp_parser_omp_master (parser, pragma_tok);
25114 case PRAGMA_OMP_ORDERED:
25115 stmt = cp_parser_omp_ordered (parser, pragma_tok);
25117 case PRAGMA_OMP_PARALLEL:
25118 stmt = cp_parser_omp_parallel (parser, pragma_tok);
25120 case PRAGMA_OMP_SECTIONS:
25121 stmt = cp_parser_omp_sections (parser, pragma_tok);
25123 case PRAGMA_OMP_SINGLE:
25124 stmt = cp_parser_omp_single (parser, pragma_tok);
25126 case PRAGMA_OMP_TASK:
25127 stmt = cp_parser_omp_task (parser, pragma_tok);
25130 gcc_unreachable ();
25134 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25139 static GTY (()) cp_parser *the_parser;
25142 /* Special handling for the first token or line in the file. The first
25143 thing in the file might be #pragma GCC pch_preprocess, which loads a
25144 PCH file, which is a GC collection point. So we need to handle this
25145 first pragma without benefit of an existing lexer structure.
25147 Always returns one token to the caller in *FIRST_TOKEN. This is
25148 either the true first token of the file, or the first token after
25149 the initial pragma. */
25152 cp_parser_initial_pragma (cp_token *first_token)
25156 cp_lexer_get_preprocessor_token (NULL, first_token);
25157 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25160 cp_lexer_get_preprocessor_token (NULL, first_token);
25161 if (first_token->type == CPP_STRING)
25163 name = first_token->u.value;
25165 cp_lexer_get_preprocessor_token (NULL, first_token);
25166 if (first_token->type != CPP_PRAGMA_EOL)
25167 error_at (first_token->location,
25168 "junk at end of %<#pragma GCC pch_preprocess%>");
25171 error_at (first_token->location, "expected string literal");
25173 /* Skip to the end of the pragma. */
25174 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25175 cp_lexer_get_preprocessor_token (NULL, first_token);
25177 /* Now actually load the PCH file. */
25179 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25181 /* Read one more token to return to our caller. We have to do this
25182 after reading the PCH file in, since its pointers have to be
25184 cp_lexer_get_preprocessor_token (NULL, first_token);
25187 /* Normal parsing of a pragma token. Here we can (and must) use the
25191 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25193 cp_token *pragma_tok;
25196 pragma_tok = cp_lexer_consume_token (parser->lexer);
25197 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25198 parser->lexer->in_pragma = true;
25200 id = pragma_tok->pragma_kind;
25203 case PRAGMA_GCC_PCH_PREPROCESS:
25204 error_at (pragma_tok->location,
25205 "%<#pragma GCC pch_preprocess%> must be first");
25208 case PRAGMA_OMP_BARRIER:
25211 case pragma_compound:
25212 cp_parser_omp_barrier (parser, pragma_tok);
25215 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25216 "used in compound statements");
25223 case PRAGMA_OMP_FLUSH:
25226 case pragma_compound:
25227 cp_parser_omp_flush (parser, pragma_tok);
25230 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25231 "used in compound statements");
25238 case PRAGMA_OMP_TASKWAIT:
25241 case pragma_compound:
25242 cp_parser_omp_taskwait (parser, pragma_tok);
25245 error_at (pragma_tok->location,
25246 "%<#pragma omp taskwait%> may only be "
25247 "used in compound statements");
25254 case PRAGMA_OMP_THREADPRIVATE:
25255 cp_parser_omp_threadprivate (parser, pragma_tok);
25258 case PRAGMA_OMP_ATOMIC:
25259 case PRAGMA_OMP_CRITICAL:
25260 case PRAGMA_OMP_FOR:
25261 case PRAGMA_OMP_MASTER:
25262 case PRAGMA_OMP_ORDERED:
25263 case PRAGMA_OMP_PARALLEL:
25264 case PRAGMA_OMP_SECTIONS:
25265 case PRAGMA_OMP_SINGLE:
25266 case PRAGMA_OMP_TASK:
25267 if (context == pragma_external)
25269 cp_parser_omp_construct (parser, pragma_tok);
25272 case PRAGMA_OMP_SECTION:
25273 error_at (pragma_tok->location,
25274 "%<#pragma omp section%> may only be used in "
25275 "%<#pragma omp sections%> construct");
25279 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25280 c_invoke_pragma_handler (id);
25284 cp_parser_error (parser, "expected declaration specifiers");
25288 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25292 /* The interface the pragma parsers have to the lexer. */
25295 pragma_lex (tree *value)
25298 enum cpp_ttype ret;
25300 tok = cp_lexer_peek_token (the_parser->lexer);
25303 *value = tok->u.value;
25305 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25307 else if (ret == CPP_STRING)
25308 *value = cp_parser_string_literal (the_parser, false, false);
25311 cp_lexer_consume_token (the_parser->lexer);
25312 if (ret == CPP_KEYWORD)
25320 /* External interface. */
25322 /* Parse one entire translation unit. */
25325 c_parse_file (void)
25327 static bool already_called = false;
25329 if (already_called)
25331 sorry ("inter-module optimizations not implemented for C++");
25334 already_called = true;
25336 the_parser = cp_parser_new ();
25337 push_deferring_access_checks (flag_access_control
25338 ? dk_no_deferred : dk_no_check);
25339 cp_parser_translation_unit (the_parser);
25343 #include "gt-cp-parser.h"