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 /* Returns TRUE iff the token T begins a decltype type. */
669 token_is_decltype (cp_token *t)
671 return (t->keyword == RID_DECLTYPE
672 || t->type == CPP_DECLTYPE);
675 /* Returns TRUE iff the next token begins a decltype type. */
678 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
680 cp_token *t = cp_lexer_peek_token (lexer);
681 return token_is_decltype (t);
684 /* Return a pointer to the Nth token in the token stream. If N is 1,
685 then this is precisely equivalent to cp_lexer_peek_token (except
686 that it is not inline). One would like to disallow that case, but
687 there is one case (cp_parser_nth_token_starts_template_id) where
688 the caller passes a variable for N and it might be 1. */
691 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
695 /* N is 1-based, not zero-based. */
698 if (cp_lexer_debugging_p (lexer))
699 fprintf (cp_lexer_debug_stream,
700 "cp_lexer: peeking ahead %ld at token: ", (long)n);
703 token = lexer->next_token;
704 gcc_assert (!n || token != &eof_token);
708 if (token == lexer->last_token)
714 if (!token->purged_p)
718 if (cp_lexer_debugging_p (lexer))
720 cp_lexer_print_token (cp_lexer_debug_stream, token);
721 putc ('\n', cp_lexer_debug_stream);
727 /* Return the next token, and advance the lexer's next_token pointer
728 to point to the next non-purged token. */
731 cp_lexer_consume_token (cp_lexer* lexer)
733 cp_token *token = lexer->next_token;
735 gcc_assert (token != &eof_token);
736 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
741 if (lexer->next_token == lexer->last_token)
743 lexer->next_token = &eof_token;
748 while (lexer->next_token->purged_p);
750 cp_lexer_set_source_position_from_token (token);
752 /* Provide debugging output. */
753 if (cp_lexer_debugging_p (lexer))
755 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
756 cp_lexer_print_token (cp_lexer_debug_stream, token);
757 putc ('\n', cp_lexer_debug_stream);
763 /* Permanently remove the next token from the token stream, and
764 advance the next_token pointer to refer to the next non-purged
768 cp_lexer_purge_token (cp_lexer *lexer)
770 cp_token *tok = lexer->next_token;
772 gcc_assert (tok != &eof_token);
773 tok->purged_p = true;
774 tok->location = UNKNOWN_LOCATION;
775 tok->u.value = NULL_TREE;
776 tok->keyword = RID_MAX;
781 if (tok == lexer->last_token)
787 while (tok->purged_p);
788 lexer->next_token = tok;
791 /* Permanently remove all tokens after TOK, up to, but not
792 including, the token that will be returned next by
793 cp_lexer_peek_token. */
796 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
798 cp_token *peek = lexer->next_token;
800 if (peek == &eof_token)
801 peek = lexer->last_token;
803 gcc_assert (tok < peek);
805 for ( tok += 1; tok != peek; tok += 1)
807 tok->purged_p = true;
808 tok->location = UNKNOWN_LOCATION;
809 tok->u.value = NULL_TREE;
810 tok->keyword = RID_MAX;
814 /* Begin saving tokens. All tokens consumed after this point will be
818 cp_lexer_save_tokens (cp_lexer* lexer)
820 /* Provide debugging output. */
821 if (cp_lexer_debugging_p (lexer))
822 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
824 VEC_safe_push (cp_token_position, heap,
825 lexer->saved_tokens, lexer->next_token);
828 /* Commit to the portion of the token stream most recently saved. */
831 cp_lexer_commit_tokens (cp_lexer* lexer)
833 /* Provide debugging output. */
834 if (cp_lexer_debugging_p (lexer))
835 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
837 VEC_pop (cp_token_position, lexer->saved_tokens);
840 /* Return all tokens saved since the last call to cp_lexer_save_tokens
841 to the token stream. Stop saving tokens. */
844 cp_lexer_rollback_tokens (cp_lexer* lexer)
846 /* Provide debugging output. */
847 if (cp_lexer_debugging_p (lexer))
848 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
850 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
853 /* Print a representation of the TOKEN on the STREAM. */
855 #ifdef ENABLE_CHECKING
858 cp_lexer_print_token (FILE * stream, cp_token *token)
860 /* We don't use cpp_type2name here because the parser defines
861 a few tokens of its own. */
862 static const char *const token_names[] = {
863 /* cpplib-defined token types */
869 /* C++ parser token types - see "Manifest constants", above. */
872 "NESTED_NAME_SPECIFIER",
875 /* For some tokens, print the associated data. */
879 /* Some keywords have a value that is not an IDENTIFIER_NODE.
880 For example, `struct' is mapped to an INTEGER_CST. */
881 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
883 /* else fall through */
885 fputs (IDENTIFIER_POINTER (token->u.value), stream);
893 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
897 print_generic_expr (stream, token->u.value, 0);
901 /* If we have a name for the token, print it out. Otherwise, we
902 simply give the numeric code. */
903 if (token->type < ARRAY_SIZE(token_names))
904 fputs (token_names[token->type], stream);
906 fprintf (stream, "[%d]", token->type);
911 /* Start emitting debugging information. */
914 cp_lexer_start_debugging (cp_lexer* lexer)
916 lexer->debugging_p = true;
919 /* Stop emitting debugging information. */
922 cp_lexer_stop_debugging (cp_lexer* lexer)
924 lexer->debugging_p = false;
927 #endif /* ENABLE_CHECKING */
929 /* Create a new cp_token_cache, representing a range of tokens. */
931 static cp_token_cache *
932 cp_token_cache_new (cp_token *first, cp_token *last)
934 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
935 cache->first = first;
941 /* Decl-specifiers. */
943 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
946 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
948 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
953 /* Nothing other than the parser should be creating declarators;
954 declarators are a semi-syntactic representation of C++ entities.
955 Other parts of the front end that need to create entities (like
956 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
958 static cp_declarator *make_call_declarator
959 (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
960 static cp_declarator *make_array_declarator
961 (cp_declarator *, tree);
962 static cp_declarator *make_pointer_declarator
963 (cp_cv_quals, cp_declarator *);
964 static cp_declarator *make_reference_declarator
965 (cp_cv_quals, cp_declarator *, bool);
966 static cp_parameter_declarator *make_parameter_declarator
967 (cp_decl_specifier_seq *, cp_declarator *, tree);
968 static cp_declarator *make_ptrmem_declarator
969 (cp_cv_quals, tree, cp_declarator *);
971 /* An erroneous declarator. */
972 static cp_declarator *cp_error_declarator;
974 /* The obstack on which declarators and related data structures are
976 static struct obstack declarator_obstack;
978 /* Alloc BYTES from the declarator memory pool. */
981 alloc_declarator (size_t bytes)
983 return obstack_alloc (&declarator_obstack, bytes);
986 /* Allocate a declarator of the indicated KIND. Clear fields that are
987 common to all declarators. */
989 static cp_declarator *
990 make_declarator (cp_declarator_kind kind)
992 cp_declarator *declarator;
994 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
995 declarator->kind = kind;
996 declarator->attributes = NULL_TREE;
997 declarator->declarator = NULL;
998 declarator->parameter_pack_p = false;
999 declarator->id_loc = UNKNOWN_LOCATION;
1004 /* Make a declarator for a generalized identifier. If
1005 QUALIFYING_SCOPE is non-NULL, the identifier is
1006 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1007 UNQUALIFIED_NAME. SFK indicates the kind of special function this
1010 static cp_declarator *
1011 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1012 special_function_kind sfk)
1014 cp_declarator *declarator;
1016 /* It is valid to write:
1018 class C { void f(); };
1022 The standard is not clear about whether `typedef const C D' is
1023 legal; as of 2002-09-15 the committee is considering that
1024 question. EDG 3.0 allows that syntax. Therefore, we do as
1026 if (qualifying_scope && TYPE_P (qualifying_scope))
1027 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1029 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1030 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1031 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1033 declarator = make_declarator (cdk_id);
1034 declarator->u.id.qualifying_scope = qualifying_scope;
1035 declarator->u.id.unqualified_name = unqualified_name;
1036 declarator->u.id.sfk = sfk;
1041 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1042 of modifiers such as const or volatile to apply to the pointer
1043 type, represented as identifiers. */
1046 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1048 cp_declarator *declarator;
1050 declarator = make_declarator (cdk_pointer);
1051 declarator->declarator = target;
1052 declarator->u.pointer.qualifiers = cv_qualifiers;
1053 declarator->u.pointer.class_type = NULL_TREE;
1056 declarator->id_loc = target->id_loc;
1057 declarator->parameter_pack_p = target->parameter_pack_p;
1058 target->parameter_pack_p = false;
1061 declarator->parameter_pack_p = false;
1066 /* Like make_pointer_declarator -- but for references. */
1069 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1072 cp_declarator *declarator;
1074 declarator = make_declarator (cdk_reference);
1075 declarator->declarator = target;
1076 declarator->u.reference.qualifiers = cv_qualifiers;
1077 declarator->u.reference.rvalue_ref = rvalue_ref;
1080 declarator->id_loc = target->id_loc;
1081 declarator->parameter_pack_p = target->parameter_pack_p;
1082 target->parameter_pack_p = false;
1085 declarator->parameter_pack_p = false;
1090 /* Like make_pointer_declarator -- but for a pointer to a non-static
1091 member of CLASS_TYPE. */
1094 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1095 cp_declarator *pointee)
1097 cp_declarator *declarator;
1099 declarator = make_declarator (cdk_ptrmem);
1100 declarator->declarator = pointee;
1101 declarator->u.pointer.qualifiers = cv_qualifiers;
1102 declarator->u.pointer.class_type = class_type;
1106 declarator->parameter_pack_p = pointee->parameter_pack_p;
1107 pointee->parameter_pack_p = false;
1110 declarator->parameter_pack_p = false;
1115 /* Make a declarator for the function given by TARGET, with the
1116 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1117 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1118 indicates what exceptions can be thrown. */
1121 make_call_declarator (cp_declarator *target,
1123 cp_cv_quals cv_qualifiers,
1124 cp_virt_specifiers virt_specifiers,
1125 tree exception_specification,
1126 tree late_return_type)
1128 cp_declarator *declarator;
1130 declarator = make_declarator (cdk_function);
1131 declarator->declarator = target;
1132 declarator->u.function.parameters = parms;
1133 declarator->u.function.qualifiers = cv_qualifiers;
1134 declarator->u.function.virt_specifiers = virt_specifiers;
1135 declarator->u.function.exception_specification = exception_specification;
1136 declarator->u.function.late_return_type = late_return_type;
1139 declarator->id_loc = target->id_loc;
1140 declarator->parameter_pack_p = target->parameter_pack_p;
1141 target->parameter_pack_p = false;
1144 declarator->parameter_pack_p = false;
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150 defined by ELEMENT. */
1153 make_array_declarator (cp_declarator *element, tree bounds)
1155 cp_declarator *declarator;
1157 declarator = make_declarator (cdk_array);
1158 declarator->declarator = element;
1159 declarator->u.array.bounds = bounds;
1162 declarator->id_loc = element->id_loc;
1163 declarator->parameter_pack_p = element->parameter_pack_p;
1164 element->parameter_pack_p = false;
1167 declarator->parameter_pack_p = false;
1172 /* Determine whether the declarator we've seen so far can be a
1173 parameter pack, when followed by an ellipsis. */
1175 declarator_can_be_parameter_pack (cp_declarator *declarator)
1177 /* Search for a declarator name, or any other declarator that goes
1178 after the point where the ellipsis could appear in a parameter
1179 pack. If we find any of these, then this declarator can not be
1180 made into a parameter pack. */
1182 while (declarator && !found)
1184 switch ((int)declarator->kind)
1195 declarator = declarator->declarator;
1203 cp_parameter_declarator *no_parameters;
1205 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1206 DECLARATOR and DEFAULT_ARGUMENT. */
1208 cp_parameter_declarator *
1209 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1210 cp_declarator *declarator,
1211 tree default_argument)
1213 cp_parameter_declarator *parameter;
1215 parameter = ((cp_parameter_declarator *)
1216 alloc_declarator (sizeof (cp_parameter_declarator)));
1217 parameter->next = NULL;
1218 if (decl_specifiers)
1219 parameter->decl_specifiers = *decl_specifiers;
1221 clear_decl_specs (¶meter->decl_specifiers);
1222 parameter->declarator = declarator;
1223 parameter->default_argument = default_argument;
1224 parameter->ellipsis_p = false;
1229 /* Returns true iff DECLARATOR is a declaration for a function. */
1232 function_declarator_p (const cp_declarator *declarator)
1236 if (declarator->kind == cdk_function
1237 && declarator->declarator->kind == cdk_id)
1239 if (declarator->kind == cdk_id
1240 || declarator->kind == cdk_error)
1242 declarator = declarator->declarator;
1252 A cp_parser parses the token stream as specified by the C++
1253 grammar. Its job is purely parsing, not semantic analysis. For
1254 example, the parser breaks the token stream into declarators,
1255 expressions, statements, and other similar syntactic constructs.
1256 It does not check that the types of the expressions on either side
1257 of an assignment-statement are compatible, or that a function is
1258 not declared with a parameter of type `void'.
1260 The parser invokes routines elsewhere in the compiler to perform
1261 semantic analysis and to build up the abstract syntax tree for the
1264 The parser (and the template instantiation code, which is, in a
1265 way, a close relative of parsing) are the only parts of the
1266 compiler that should be calling push_scope and pop_scope, or
1267 related functions. The parser (and template instantiation code)
1268 keeps track of what scope is presently active; everything else
1269 should simply honor that. (The code that generates static
1270 initializers may also need to set the scope, in order to check
1271 access control correctly when emitting the initializers.)
1276 The parser is of the standard recursive-descent variety. Upcoming
1277 tokens in the token stream are examined in order to determine which
1278 production to use when parsing a non-terminal. Some C++ constructs
1279 require arbitrary look ahead to disambiguate. For example, it is
1280 impossible, in the general case, to tell whether a statement is an
1281 expression or declaration without scanning the entire statement.
1282 Therefore, the parser is capable of "parsing tentatively." When the
1283 parser is not sure what construct comes next, it enters this mode.
1284 Then, while we attempt to parse the construct, the parser queues up
1285 error messages, rather than issuing them immediately, and saves the
1286 tokens it consumes. If the construct is parsed successfully, the
1287 parser "commits", i.e., it issues any queued error messages and
1288 the tokens that were being preserved are permanently discarded.
1289 If, however, the construct is not parsed successfully, the parser
1290 rolls back its state completely so that it can resume parsing using
1291 a different alternative.
1296 The performance of the parser could probably be improved substantially.
1297 We could often eliminate the need to parse tentatively by looking ahead
1298 a little bit. In some places, this approach might not entirely eliminate
1299 the need to parse tentatively, but it might still speed up the average
1302 /* Flags that are passed to some parsing functions. These values can
1303 be bitwise-ored together. */
1308 CP_PARSER_FLAGS_NONE = 0x0,
1309 /* The construct is optional. If it is not present, then no error
1310 should be issued. */
1311 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1312 /* When parsing a type-specifier, treat user-defined type-names
1313 as non-type identifiers. */
1314 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1315 /* When parsing a type-specifier, do not try to parse a class-specifier
1316 or enum-specifier. */
1317 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1318 /* When parsing a decl-specifier-seq, only allow type-specifier or
1320 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1323 /* This type is used for parameters and variables which hold
1324 combinations of the above flags. */
1325 typedef int cp_parser_flags;
1327 /* The different kinds of declarators we want to parse. */
1329 typedef enum cp_parser_declarator_kind
1331 /* We want an abstract declarator. */
1332 CP_PARSER_DECLARATOR_ABSTRACT,
1333 /* We want a named declarator. */
1334 CP_PARSER_DECLARATOR_NAMED,
1335 /* We don't mind, but the name must be an unqualified-id. */
1336 CP_PARSER_DECLARATOR_EITHER
1337 } cp_parser_declarator_kind;
1339 /* The precedence values used to parse binary expressions. The minimum value
1340 of PREC must be 1, because zero is reserved to quickly discriminate
1341 binary operators from other tokens. */
1346 PREC_LOGICAL_OR_EXPRESSION,
1347 PREC_LOGICAL_AND_EXPRESSION,
1348 PREC_INCLUSIVE_OR_EXPRESSION,
1349 PREC_EXCLUSIVE_OR_EXPRESSION,
1350 PREC_AND_EXPRESSION,
1351 PREC_EQUALITY_EXPRESSION,
1352 PREC_RELATIONAL_EXPRESSION,
1353 PREC_SHIFT_EXPRESSION,
1354 PREC_ADDITIVE_EXPRESSION,
1355 PREC_MULTIPLICATIVE_EXPRESSION,
1357 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1360 /* A mapping from a token type to a corresponding tree node type, with a
1361 precedence value. */
1363 typedef struct cp_parser_binary_operations_map_node
1365 /* The token type. */
1366 enum cpp_ttype token_type;
1367 /* The corresponding tree code. */
1368 enum tree_code tree_type;
1369 /* The precedence of this operator. */
1370 enum cp_parser_prec prec;
1371 } cp_parser_binary_operations_map_node;
1373 typedef struct cp_parser_expression_stack_entry
1375 /* Left hand side of the binary operation we are currently
1378 /* Original tree code for left hand side, if it was a binary
1379 expression itself (used for -Wparentheses). */
1380 enum tree_code lhs_type;
1381 /* Tree code for the binary operation we are parsing. */
1382 enum tree_code tree_type;
1383 /* Precedence of the binary operation we are parsing. */
1384 enum cp_parser_prec prec;
1385 } cp_parser_expression_stack_entry;
1387 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1388 entries because precedence levels on the stack are monotonically
1390 typedef struct cp_parser_expression_stack_entry
1391 cp_parser_expression_stack[NUM_PREC_VALUES];
1395 /* Constructors and destructors. */
1397 static cp_parser_context *cp_parser_context_new
1398 (cp_parser_context *);
1400 /* Class variables. */
1402 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1404 /* The operator-precedence table used by cp_parser_binary_expression.
1405 Transformed into an associative array (binops_by_token) by
1408 static const cp_parser_binary_operations_map_node binops[] = {
1409 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1410 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1412 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1413 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1414 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1416 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1417 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1419 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1420 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1422 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1423 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1424 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1425 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1427 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1428 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1430 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1432 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1434 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1436 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1438 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1441 /* The same as binops, but initialized by cp_parser_new so that
1442 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1444 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1446 /* Constructors and destructors. */
1448 /* Construct a new context. The context below this one on the stack
1449 is given by NEXT. */
1451 static cp_parser_context *
1452 cp_parser_context_new (cp_parser_context* next)
1454 cp_parser_context *context;
1456 /* Allocate the storage. */
1457 if (cp_parser_context_free_list != NULL)
1459 /* Pull the first entry from the free list. */
1460 context = cp_parser_context_free_list;
1461 cp_parser_context_free_list = context->next;
1462 memset (context, 0, sizeof (*context));
1465 context = ggc_alloc_cleared_cp_parser_context ();
1467 /* No errors have occurred yet in this context. */
1468 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1469 /* If this is not the bottommost context, copy information that we
1470 need from the previous context. */
1473 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1474 expression, then we are parsing one in this context, too. */
1475 context->object_type = next->object_type;
1476 /* Thread the stack. */
1477 context->next = next;
1483 /* Managing the unparsed function queues. */
1485 #define unparsed_funs_with_default_args \
1486 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1487 #define unparsed_funs_with_definitions \
1488 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1491 push_unparsed_function_queues (cp_parser *parser)
1493 VEC_safe_push (cp_unparsed_functions_entry, gc,
1494 parser->unparsed_queues, NULL);
1495 unparsed_funs_with_default_args = NULL;
1496 unparsed_funs_with_definitions = make_tree_vector ();
1500 pop_unparsed_function_queues (cp_parser *parser)
1502 release_tree_vector (unparsed_funs_with_definitions);
1503 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1508 /* Constructors and destructors. */
1510 static cp_parser *cp_parser_new
1513 /* Routines to parse various constructs.
1515 Those that return `tree' will return the error_mark_node (rather
1516 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517 Sometimes, they will return an ordinary node if error-recovery was
1518 attempted, even though a parse error occurred. So, to check
1519 whether or not a parse error occurred, you should always use
1520 cp_parser_error_occurred. If the construct is optional (indicated
1521 either by an `_opt' in the name of the function that does the
1522 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523 the construct is not present. */
1525 /* Lexical conventions [gram.lex] */
1527 static tree cp_parser_identifier
1529 static tree cp_parser_string_literal
1530 (cp_parser *, bool, bool);
1532 /* Basic concepts [gram.basic] */
1534 static bool cp_parser_translation_unit
1537 /* Expressions [gram.expr] */
1539 static tree cp_parser_primary_expression
1540 (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542 (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544 (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546 (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548 (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_qualifying_entity
1550 (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552 (cp_parser *, bool, bool, bool, cp_id_kind *);
1553 static tree cp_parser_postfix_open_square_expression
1554 (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1557 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1558 (cp_parser *, int, bool, bool, bool *);
1559 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1560 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1561 static void cp_parser_pseudo_destructor_name
1562 (cp_parser *, tree *, tree *);
1563 static tree cp_parser_unary_expression
1564 (cp_parser *, bool, bool, cp_id_kind *);
1565 static enum tree_code cp_parser_unary_operator
1567 static tree cp_parser_new_expression
1569 static VEC(tree,gc) *cp_parser_new_placement
1571 static tree cp_parser_new_type_id
1572 (cp_parser *, tree *);
1573 static cp_declarator *cp_parser_new_declarator_opt
1575 static cp_declarator *cp_parser_direct_new_declarator
1577 static VEC(tree,gc) *cp_parser_new_initializer
1579 static tree cp_parser_delete_expression
1581 static tree cp_parser_cast_expression
1582 (cp_parser *, bool, bool, cp_id_kind *);
1583 static tree cp_parser_binary_expression
1584 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1585 static tree cp_parser_question_colon_clause
1586 (cp_parser *, tree);
1587 static tree cp_parser_assignment_expression
1588 (cp_parser *, bool, cp_id_kind *);
1589 static enum tree_code cp_parser_assignment_operator_opt
1591 static tree cp_parser_expression
1592 (cp_parser *, bool, cp_id_kind *);
1593 static tree cp_parser_constant_expression
1594 (cp_parser *, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1597 static tree cp_parser_lambda_expression
1599 static void cp_parser_lambda_introducer
1600 (cp_parser *, tree);
1601 static bool cp_parser_lambda_declarator_opt
1602 (cp_parser *, tree);
1603 static void cp_parser_lambda_body
1604 (cp_parser *, tree);
1606 /* Statements [gram.stmt.stmt] */
1608 static void cp_parser_statement
1609 (cp_parser *, tree, bool, bool *);
1610 static void cp_parser_label_for_labeled_statement
1612 static tree cp_parser_expression_statement
1613 (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615 (cp_parser *, tree, bool, bool);
1616 static void cp_parser_statement_seq_opt
1617 (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1619 (cp_parser *, bool *);
1620 static tree cp_parser_condition
1622 static tree cp_parser_iteration_statement
1624 static bool cp_parser_for_init_statement
1625 (cp_parser *, tree *decl);
1626 static tree cp_parser_for
1628 static tree cp_parser_c_for
1629 (cp_parser *, tree, tree);
1630 static tree cp_parser_range_for
1631 (cp_parser *, tree, tree, tree);
1632 static void do_range_for_auto_deduction
1634 static tree cp_parser_perform_range_for_lookup
1635 (tree, tree *, tree *);
1636 static tree cp_parser_range_for_member_function
1638 static tree cp_parser_jump_statement
1640 static void cp_parser_declaration_statement
1643 static tree cp_parser_implicitly_scoped_statement
1644 (cp_parser *, bool *);
1645 static void cp_parser_already_scoped_statement
1648 /* Declarations [gram.dcl.dcl] */
1650 static void cp_parser_declaration_seq_opt
1652 static void cp_parser_declaration
1654 static void cp_parser_block_declaration
1655 (cp_parser *, bool);
1656 static void cp_parser_simple_declaration
1657 (cp_parser *, bool, tree *);
1658 static void cp_parser_decl_specifier_seq
1659 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1660 static tree cp_parser_storage_class_specifier_opt
1662 static tree cp_parser_function_specifier_opt
1663 (cp_parser *, cp_decl_specifier_seq *);
1664 static tree cp_parser_type_specifier
1665 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1667 static tree cp_parser_simple_type_specifier
1668 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1669 static tree cp_parser_type_name
1671 static tree cp_parser_nonclass_name
1672 (cp_parser* parser);
1673 static tree cp_parser_elaborated_type_specifier
1674 (cp_parser *, bool, bool);
1675 static tree cp_parser_enum_specifier
1677 static void cp_parser_enumerator_list
1678 (cp_parser *, tree);
1679 static void cp_parser_enumerator_definition
1680 (cp_parser *, tree);
1681 static tree cp_parser_namespace_name
1683 static void cp_parser_namespace_definition
1685 static void cp_parser_namespace_body
1687 static tree cp_parser_qualified_namespace_specifier
1689 static void cp_parser_namespace_alias_definition
1691 static bool cp_parser_using_declaration
1692 (cp_parser *, bool);
1693 static void cp_parser_using_directive
1695 static void cp_parser_asm_definition
1697 static void cp_parser_linkage_specification
1699 static void cp_parser_static_assert
1700 (cp_parser *, bool);
1701 static tree cp_parser_decltype
1704 /* Declarators [gram.dcl.decl] */
1706 static tree cp_parser_init_declarator
1707 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1708 static cp_declarator *cp_parser_declarator
1709 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1710 static cp_declarator *cp_parser_direct_declarator
1711 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1712 static enum tree_code cp_parser_ptr_operator
1713 (cp_parser *, tree *, cp_cv_quals *);
1714 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1716 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1718 static tree cp_parser_late_return_type_opt
1719 (cp_parser *, cp_cv_quals);
1720 static tree cp_parser_declarator_id
1721 (cp_parser *, bool);
1722 static tree cp_parser_type_id
1724 static tree cp_parser_template_type_arg
1726 static tree cp_parser_trailing_type_id (cp_parser *);
1727 static tree cp_parser_type_id_1
1728 (cp_parser *, bool, bool);
1729 static void cp_parser_type_specifier_seq
1730 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1731 static tree cp_parser_parameter_declaration_clause
1733 static tree cp_parser_parameter_declaration_list
1734 (cp_parser *, bool *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration
1736 (cp_parser *, bool, bool *);
1737 static tree cp_parser_default_argument
1738 (cp_parser *, bool);
1739 static void cp_parser_function_body
1741 static tree cp_parser_initializer
1742 (cp_parser *, bool *, bool *);
1743 static tree cp_parser_initializer_clause
1744 (cp_parser *, bool *);
1745 static tree cp_parser_braced_list
1746 (cp_parser*, bool*);
1747 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1748 (cp_parser *, bool *);
1750 static bool cp_parser_ctor_initializer_opt_and_function_body
1753 /* Classes [gram.class] */
1755 static tree cp_parser_class_name
1756 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1757 static tree cp_parser_class_specifier
1759 static tree cp_parser_class_head
1760 (cp_parser *, bool *, tree *, tree *);
1761 static enum tag_types cp_parser_class_key
1763 static void cp_parser_member_specification_opt
1765 static void cp_parser_member_declaration
1767 static tree cp_parser_pure_specifier
1769 static tree cp_parser_constant_initializer
1772 /* Derived classes [gram.class.derived] */
1774 static tree cp_parser_base_clause
1776 static tree cp_parser_base_specifier
1779 /* Special member functions [gram.special] */
1781 static tree cp_parser_conversion_function_id
1783 static tree cp_parser_conversion_type_id
1785 static cp_declarator *cp_parser_conversion_declarator_opt
1787 static bool cp_parser_ctor_initializer_opt
1789 static void cp_parser_mem_initializer_list
1791 static tree cp_parser_mem_initializer
1793 static tree cp_parser_mem_initializer_id
1796 /* Overloading [gram.over] */
1798 static tree cp_parser_operator_function_id
1800 static tree cp_parser_operator
1803 /* Templates [gram.temp] */
1805 static void cp_parser_template_declaration
1806 (cp_parser *, bool);
1807 static tree cp_parser_template_parameter_list
1809 static tree cp_parser_template_parameter
1810 (cp_parser *, bool *, bool *);
1811 static tree cp_parser_type_parameter
1812 (cp_parser *, bool *);
1813 static tree cp_parser_template_id
1814 (cp_parser *, bool, bool, bool);
1815 static tree cp_parser_template_name
1816 (cp_parser *, bool, bool, bool, bool *);
1817 static tree cp_parser_template_argument_list
1819 static tree cp_parser_template_argument
1821 static void cp_parser_explicit_instantiation
1823 static void cp_parser_explicit_specialization
1826 /* Exception handling [gram.exception] */
1828 static tree cp_parser_try_block
1830 static bool cp_parser_function_try_block
1832 static void cp_parser_handler_seq
1834 static void cp_parser_handler
1836 static tree cp_parser_exception_declaration
1838 static tree cp_parser_throw_expression
1840 static tree cp_parser_exception_specification_opt
1842 static tree cp_parser_type_id_list
1845 /* GNU Extensions */
1847 static tree cp_parser_asm_specification_opt
1849 static tree cp_parser_asm_operand_list
1851 static tree cp_parser_asm_clobber_list
1853 static tree cp_parser_asm_label_list
1855 static tree cp_parser_attributes_opt
1857 static tree cp_parser_attribute_list
1859 static bool cp_parser_extension_opt
1860 (cp_parser *, int *);
1861 static void cp_parser_label_declaration
1864 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1865 static bool cp_parser_pragma
1866 (cp_parser *, enum pragma_context);
1868 /* Objective-C++ Productions */
1870 static tree cp_parser_objc_message_receiver
1872 static tree cp_parser_objc_message_args
1874 static tree cp_parser_objc_message_expression
1876 static tree cp_parser_objc_encode_expression
1878 static tree cp_parser_objc_defs_expression
1880 static tree cp_parser_objc_protocol_expression
1882 static tree cp_parser_objc_selector_expression
1884 static tree cp_parser_objc_expression
1886 static bool cp_parser_objc_selector_p
1888 static tree cp_parser_objc_selector
1890 static tree cp_parser_objc_protocol_refs_opt
1892 static void cp_parser_objc_declaration
1893 (cp_parser *, tree);
1894 static tree cp_parser_objc_statement
1896 static bool cp_parser_objc_valid_prefix_attributes
1897 (cp_parser *, tree *);
1898 static void cp_parser_objc_at_property_declaration
1900 static void cp_parser_objc_at_synthesize_declaration
1902 static void cp_parser_objc_at_dynamic_declaration
1904 static tree cp_parser_objc_struct_declaration
1907 /* Utility Routines */
1909 static tree cp_parser_lookup_name
1910 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1911 static tree cp_parser_lookup_name_simple
1912 (cp_parser *, tree, location_t);
1913 static tree cp_parser_maybe_treat_template_as_class
1915 static bool cp_parser_check_declarator_template_parameters
1916 (cp_parser *, cp_declarator *, location_t);
1917 static bool cp_parser_check_template_parameters
1918 (cp_parser *, unsigned, location_t, cp_declarator *);
1919 static tree cp_parser_simple_cast_expression
1921 static tree cp_parser_global_scope_opt
1922 (cp_parser *, bool);
1923 static bool cp_parser_constructor_declarator_p
1924 (cp_parser *, bool);
1925 static tree cp_parser_function_definition_from_specifiers_and_declarator
1926 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1927 static tree cp_parser_function_definition_after_declarator
1928 (cp_parser *, bool);
1929 static void cp_parser_template_declaration_after_export
1930 (cp_parser *, bool);
1931 static void cp_parser_perform_template_parameter_access_checks
1932 (VEC (deferred_access_check,gc)*);
1933 static tree cp_parser_single_declaration
1934 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1935 static tree cp_parser_functional_cast
1936 (cp_parser *, tree);
1937 static tree cp_parser_save_member_function_body
1938 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1939 static tree cp_parser_enclosed_template_argument_list
1941 static void cp_parser_save_default_args
1942 (cp_parser *, tree);
1943 static void cp_parser_late_parsing_for_member
1944 (cp_parser *, tree);
1945 static void cp_parser_late_parsing_default_args
1946 (cp_parser *, tree);
1947 static tree cp_parser_sizeof_operand
1948 (cp_parser *, enum rid);
1949 static tree cp_parser_trait_expr
1950 (cp_parser *, enum rid);
1951 static bool cp_parser_declares_only_class_p
1953 static void cp_parser_set_storage_class
1954 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1955 static void cp_parser_set_decl_spec_type
1956 (cp_decl_specifier_seq *, tree, location_t, bool);
1957 static bool cp_parser_friend_p
1958 (const cp_decl_specifier_seq *);
1959 static void cp_parser_required_error
1960 (cp_parser *, required_token, bool);
1961 static cp_token *cp_parser_require
1962 (cp_parser *, enum cpp_ttype, required_token);
1963 static cp_token *cp_parser_require_keyword
1964 (cp_parser *, enum rid, required_token);
1965 static bool cp_parser_token_starts_function_definition_p
1967 static bool cp_parser_next_token_starts_class_definition_p
1969 static bool cp_parser_next_token_ends_template_argument_p
1971 static bool cp_parser_nth_token_starts_template_argument_list_p
1972 (cp_parser *, size_t);
1973 static enum tag_types cp_parser_token_is_class_key
1975 static void cp_parser_check_class_key
1976 (enum tag_types, tree type);
1977 static void cp_parser_check_access_in_redeclaration
1978 (tree type, location_t location);
1979 static bool cp_parser_optional_template_keyword
1981 static void cp_parser_pre_parsed_nested_name_specifier
1983 static bool cp_parser_cache_group
1984 (cp_parser *, enum cpp_ttype, unsigned);
1985 static void cp_parser_parse_tentatively
1987 static void cp_parser_commit_to_tentative_parse
1989 static void cp_parser_abort_tentative_parse
1991 static bool cp_parser_parse_definitely
1993 static inline bool cp_parser_parsing_tentatively
1995 static bool cp_parser_uncommitted_to_tentative_parse_p
1997 static void cp_parser_error
1998 (cp_parser *, const char *);
1999 static void cp_parser_name_lookup_error
2000 (cp_parser *, tree, tree, name_lookup_error, location_t);
2001 static bool cp_parser_simulate_error
2003 static bool cp_parser_check_type_definition
2005 static void cp_parser_check_for_definition_in_return_type
2006 (cp_declarator *, tree, location_t type_location);
2007 static void cp_parser_check_for_invalid_template_id
2008 (cp_parser *, tree, location_t location);
2009 static bool cp_parser_non_integral_constant_expression
2010 (cp_parser *, non_integral_constant);
2011 static void cp_parser_diagnose_invalid_type_name
2012 (cp_parser *, tree, tree, location_t);
2013 static bool cp_parser_parse_and_diagnose_invalid_type_name
2015 static int cp_parser_skip_to_closing_parenthesis
2016 (cp_parser *, bool, bool, bool);
2017 static void cp_parser_skip_to_end_of_statement
2019 static void cp_parser_consume_semicolon_at_end_of_statement
2021 static void cp_parser_skip_to_end_of_block_or_statement
2023 static bool cp_parser_skip_to_closing_brace
2025 static void cp_parser_skip_to_end_of_template_parameter_list
2027 static void cp_parser_skip_to_pragma_eol
2028 (cp_parser*, cp_token *);
2029 static bool cp_parser_error_occurred
2031 static bool cp_parser_allow_gnu_extensions_p
2033 static bool cp_parser_is_string_literal
2035 static bool cp_parser_is_keyword
2036 (cp_token *, enum rid);
2037 static tree cp_parser_make_typename_type
2038 (cp_parser *, tree, tree, location_t location);
2039 static cp_declarator * cp_parser_make_indirect_declarator
2040 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2042 /* Returns nonzero if we are parsing tentatively. */
2045 cp_parser_parsing_tentatively (cp_parser* parser)
2047 return parser->context->next != NULL;
2050 /* Returns nonzero if TOKEN is a string literal. */
2053 cp_parser_is_string_literal (cp_token* token)
2055 return (token->type == CPP_STRING ||
2056 token->type == CPP_STRING16 ||
2057 token->type == CPP_STRING32 ||
2058 token->type == CPP_WSTRING ||
2059 token->type == CPP_UTF8STRING);
2062 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2065 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2067 return token->keyword == keyword;
2070 /* If not parsing tentatively, issue a diagnostic of the form
2071 FILE:LINE: MESSAGE before TOKEN
2072 where TOKEN is the next token in the input stream. MESSAGE
2073 (specified by the caller) is usually of the form "expected
2077 cp_parser_error (cp_parser* parser, const char* gmsgid)
2079 if (!cp_parser_simulate_error (parser))
2081 cp_token *token = cp_lexer_peek_token (parser->lexer);
2082 /* This diagnostic makes more sense if it is tagged to the line
2083 of the token we just peeked at. */
2084 cp_lexer_set_source_position_from_token (token);
2086 if (token->type == CPP_PRAGMA)
2088 error_at (token->location,
2089 "%<#pragma%> is not allowed here");
2090 cp_parser_skip_to_pragma_eol (parser, token);
2094 c_parse_error (gmsgid,
2095 /* Because c_parser_error does not understand
2096 CPP_KEYWORD, keywords are treated like
2098 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2099 token->u.value, token->flags);
2103 /* Issue an error about name-lookup failing. NAME is the
2104 IDENTIFIER_NODE DECL is the result of
2105 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2106 the thing that we hoped to find. */
2109 cp_parser_name_lookup_error (cp_parser* parser,
2112 name_lookup_error desired,
2113 location_t location)
2115 /* If name lookup completely failed, tell the user that NAME was not
2117 if (decl == error_mark_node)
2119 if (parser->scope && parser->scope != global_namespace)
2120 error_at (location, "%<%E::%E%> has not been declared",
2121 parser->scope, name);
2122 else if (parser->scope == global_namespace)
2123 error_at (location, "%<::%E%> has not been declared", name);
2124 else if (parser->object_scope
2125 && !CLASS_TYPE_P (parser->object_scope))
2126 error_at (location, "request for member %qE in non-class type %qT",
2127 name, parser->object_scope);
2128 else if (parser->object_scope)
2129 error_at (location, "%<%T::%E%> has not been declared",
2130 parser->object_scope, name);
2132 error_at (location, "%qE has not been declared", name);
2134 else if (parser->scope && parser->scope != global_namespace)
2139 error_at (location, "%<%E::%E%> is not a type",
2140 parser->scope, name);
2143 error_at (location, "%<%E::%E%> is not a class or namespace",
2144 parser->scope, name);
2148 "%<%E::%E%> is not a class, namespace, or enumeration",
2149 parser->scope, name);
2156 else if (parser->scope == global_namespace)
2161 error_at (location, "%<::%E%> is not a type", name);
2164 error_at (location, "%<::%E%> is not a class or namespace", name);
2168 "%<::%E%> is not a class, namespace, or enumeration",
2180 error_at (location, "%qE is not a type", name);
2183 error_at (location, "%qE is not a class or namespace", name);
2187 "%qE is not a class, namespace, or enumeration", name);
2195 /* If we are parsing tentatively, remember that an error has occurred
2196 during this tentative parse. Returns true if the error was
2197 simulated; false if a message should be issued by the caller. */
2200 cp_parser_simulate_error (cp_parser* parser)
2202 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2204 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2210 /* Check for repeated decl-specifiers. */
2213 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2214 location_t location)
2218 for (ds = ds_first; ds != ds_last; ++ds)
2220 unsigned count = decl_specs->specs[ds];
2223 /* The "long" specifier is a special case because of "long long". */
2227 error_at (location, "%<long long long%> is too long for GCC");
2229 pedwarn_cxx98 (location, OPT_Wlong_long,
2230 "ISO C++ 1998 does not support %<long long%>");
2234 static const char *const decl_spec_names[] = {
2251 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2256 /* This function is called when a type is defined. If type
2257 definitions are forbidden at this point, an error message is
2261 cp_parser_check_type_definition (cp_parser* parser)
2263 /* If types are forbidden here, issue a message. */
2264 if (parser->type_definition_forbidden_message)
2266 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2267 in the message need to be interpreted. */
2268 error (parser->type_definition_forbidden_message);
2274 /* This function is called when the DECLARATOR is processed. The TYPE
2275 was a type defined in the decl-specifiers. If it is invalid to
2276 define a type in the decl-specifiers for DECLARATOR, an error is
2277 issued. TYPE_LOCATION is the location of TYPE and is used
2278 for error reporting. */
2281 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2282 tree type, location_t type_location)
2284 /* [dcl.fct] forbids type definitions in return types.
2285 Unfortunately, it's not easy to know whether or not we are
2286 processing a return type until after the fact. */
2288 && (declarator->kind == cdk_pointer
2289 || declarator->kind == cdk_reference
2290 || declarator->kind == cdk_ptrmem))
2291 declarator = declarator->declarator;
2293 && declarator->kind == cdk_function)
2295 error_at (type_location,
2296 "new types may not be defined in a return type");
2297 inform (type_location,
2298 "(perhaps a semicolon is missing after the definition of %qT)",
2303 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2304 "<" in any valid C++ program. If the next token is indeed "<",
2305 issue a message warning the user about what appears to be an
2306 invalid attempt to form a template-id. LOCATION is the location
2307 of the type-specifier (TYPE) */
2310 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2311 tree type, location_t location)
2313 cp_token_position start = 0;
2315 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2318 error_at (location, "%qT is not a template", type);
2319 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2320 error_at (location, "%qE is not a template", type);
2322 error_at (location, "invalid template-id");
2323 /* Remember the location of the invalid "<". */
2324 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2325 start = cp_lexer_token_position (parser->lexer, true);
2326 /* Consume the "<". */
2327 cp_lexer_consume_token (parser->lexer);
2328 /* Parse the template arguments. */
2329 cp_parser_enclosed_template_argument_list (parser);
2330 /* Permanently remove the invalid template arguments so that
2331 this error message is not issued again. */
2333 cp_lexer_purge_tokens_after (parser->lexer, start);
2337 /* If parsing an integral constant-expression, issue an error message
2338 about the fact that THING appeared and return true. Otherwise,
2339 return false. In either case, set
2340 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2343 cp_parser_non_integral_constant_expression (cp_parser *parser,
2344 non_integral_constant thing)
2346 parser->non_integral_constant_expression_p = true;
2347 if (parser->integral_constant_expression_p)
2349 if (!parser->allow_non_integral_constant_expression_p)
2351 const char *msg = NULL;
2355 error ("floating-point literal "
2356 "cannot appear in a constant-expression");
2359 error ("a cast to a type other than an integral or "
2360 "enumeration type cannot appear in a "
2361 "constant-expression");
2364 error ("%<typeid%> operator "
2365 "cannot appear in a constant-expression");
2368 error ("non-constant compound literals "
2369 "cannot appear in a constant-expression");
2372 error ("a function call "
2373 "cannot appear in a constant-expression");
2376 error ("an increment "
2377 "cannot appear in a constant-expression");
2380 error ("an decrement "
2381 "cannot appear in a constant-expression");
2384 error ("an array reference "
2385 "cannot appear in a constant-expression");
2387 case NIC_ADDR_LABEL:
2388 error ("the address of a label "
2389 "cannot appear in a constant-expression");
2391 case NIC_OVERLOADED:
2392 error ("calls to overloaded operators "
2393 "cannot appear in a constant-expression");
2395 case NIC_ASSIGNMENT:
2396 error ("an assignment cannot appear in a constant-expression");
2399 error ("a comma operator "
2400 "cannot appear in a constant-expression");
2402 case NIC_CONSTRUCTOR:
2403 error ("a call to a constructor "
2404 "cannot appear in a constant-expression");
2410 msg = "__FUNCTION__";
2412 case NIC_PRETTY_FUNC:
2413 msg = "__PRETTY_FUNCTION__";
2433 case NIC_PREINCREMENT:
2436 case NIC_PREDECREMENT:
2449 error ("%qs cannot appear in a constant-expression", msg);
2456 /* Emit a diagnostic for an invalid type name. SCOPE is the
2457 qualifying scope (or NULL, if none) for ID. This function commits
2458 to the current active tentative parse, if any. (Otherwise, the
2459 problematic construct might be encountered again later, resulting
2460 in duplicate error messages.) LOCATION is the location of ID. */
2463 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2464 tree scope, tree id,
2465 location_t location)
2467 tree decl, old_scope;
2468 cp_parser_commit_to_tentative_parse (parser);
2469 /* Try to lookup the identifier. */
2470 old_scope = parser->scope;
2471 parser->scope = scope;
2472 decl = cp_parser_lookup_name_simple (parser, id, location);
2473 parser->scope = old_scope;
2474 /* If the lookup found a template-name, it means that the user forgot
2475 to specify an argument list. Emit a useful error message. */
2476 if (TREE_CODE (decl) == TEMPLATE_DECL)
2478 "invalid use of template-name %qE without an argument list",
2480 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2481 error_at (location, "invalid use of destructor %qD as a type", id);
2482 else if (TREE_CODE (decl) == TYPE_DECL)
2483 /* Something like 'unsigned A a;' */
2484 error_at (location, "invalid combination of multiple type-specifiers");
2485 else if (!parser->scope)
2487 /* Issue an error message. */
2488 error_at (location, "%qE does not name a type", id);
2489 /* If we're in a template class, it's possible that the user was
2490 referring to a type from a base class. For example:
2492 template <typename T> struct A { typedef T X; };
2493 template <typename T> struct B : public A<T> { X x; };
2495 The user should have said "typename A<T>::X". */
2496 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2497 inform (location, "C++0x %<constexpr%> only available with "
2498 "-std=c++0x or -std=gnu++0x");
2499 else if (processing_template_decl && current_class_type
2500 && TYPE_BINFO (current_class_type))
2504 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2508 tree base_type = BINFO_TYPE (b);
2509 if (CLASS_TYPE_P (base_type)
2510 && dependent_type_p (base_type))
2513 /* Go from a particular instantiation of the
2514 template (which will have an empty TYPE_FIELDs),
2515 to the main version. */
2516 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2517 for (field = TYPE_FIELDS (base_type);
2519 field = DECL_CHAIN (field))
2520 if (TREE_CODE (field) == TYPE_DECL
2521 && DECL_NAME (field) == id)
2524 "(perhaps %<typename %T::%E%> was intended)",
2525 BINFO_TYPE (b), id);
2534 /* Here we diagnose qualified-ids where the scope is actually correct,
2535 but the identifier does not resolve to a valid type name. */
2536 else if (parser->scope != error_mark_node)
2538 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2539 error_at (location, "%qE in namespace %qE does not name a type",
2541 else if (CLASS_TYPE_P (parser->scope)
2542 && constructor_name_p (id, parser->scope))
2545 error_at (location, "%<%T::%E%> names the constructor, not"
2546 " the type", parser->scope, id);
2547 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2548 error_at (location, "and %qT has no template constructors",
2551 else if (TYPE_P (parser->scope)
2552 && dependent_scope_p (parser->scope))
2553 error_at (location, "need %<typename%> before %<%T::%E%> because "
2554 "%qT is a dependent scope",
2555 parser->scope, id, parser->scope);
2556 else if (TYPE_P (parser->scope))
2557 error_at (location, "%qE in %q#T does not name a type",
2564 /* Check for a common situation where a type-name should be present,
2565 but is not, and issue a sensible error message. Returns true if an
2566 invalid type-name was detected.
2568 The situation handled by this function are variable declarations of the
2569 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2570 Usually, `ID' should name a type, but if we got here it means that it
2571 does not. We try to emit the best possible error message depending on
2572 how exactly the id-expression looks like. */
2575 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2578 cp_token *token = cp_lexer_peek_token (parser->lexer);
2580 /* Avoid duplicate error about ambiguous lookup. */
2581 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2583 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2584 if (next->type == CPP_NAME && next->ambiguous_p)
2588 cp_parser_parse_tentatively (parser);
2589 id = cp_parser_id_expression (parser,
2590 /*template_keyword_p=*/false,
2591 /*check_dependency_p=*/true,
2592 /*template_p=*/NULL,
2593 /*declarator_p=*/true,
2594 /*optional_p=*/false);
2595 /* If the next token is a (, this is a function with no explicit return
2596 type, i.e. constructor, destructor or conversion op. */
2597 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2598 || TREE_CODE (id) == TYPE_DECL)
2600 cp_parser_abort_tentative_parse (parser);
2603 if (!cp_parser_parse_definitely (parser))
2606 /* Emit a diagnostic for the invalid type. */
2607 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2608 id, token->location);
2610 /* If we aren't in the middle of a declarator (i.e. in a
2611 parameter-declaration-clause), skip to the end of the declaration;
2612 there's no point in trying to process it. */
2613 if (!parser->in_declarator_p)
2614 cp_parser_skip_to_end_of_block_or_statement (parser);
2618 /* Consume tokens up to, and including, the next non-nested closing `)'.
2619 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2620 are doing error recovery. Returns -1 if OR_COMMA is true and we
2621 found an unnested comma. */
2624 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2629 unsigned paren_depth = 0;
2630 unsigned brace_depth = 0;
2631 unsigned square_depth = 0;
2633 if (recovering && !or_comma
2634 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2639 cp_token * token = cp_lexer_peek_token (parser->lexer);
2641 switch (token->type)
2644 case CPP_PRAGMA_EOL:
2645 /* If we've run out of tokens, then there is no closing `)'. */
2648 /* This is good for lambda expression capture-lists. */
2649 case CPP_OPEN_SQUARE:
2652 case CPP_CLOSE_SQUARE:
2653 if (!square_depth--)
2658 /* This matches the processing in skip_to_end_of_statement. */
2663 case CPP_OPEN_BRACE:
2666 case CPP_CLOSE_BRACE:
2672 if (recovering && or_comma && !brace_depth && !paren_depth
2677 case CPP_OPEN_PAREN:
2682 case CPP_CLOSE_PAREN:
2683 if (!brace_depth && !paren_depth--)
2686 cp_lexer_consume_token (parser->lexer);
2695 /* Consume the token. */
2696 cp_lexer_consume_token (parser->lexer);
2700 /* Consume tokens until we reach the end of the current statement.
2701 Normally, that will be just before consuming a `;'. However, if a
2702 non-nested `}' comes first, then we stop before consuming that. */
2705 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2707 unsigned nesting_depth = 0;
2711 cp_token *token = cp_lexer_peek_token (parser->lexer);
2713 switch (token->type)
2716 case CPP_PRAGMA_EOL:
2717 /* If we've run out of tokens, stop. */
2721 /* If the next token is a `;', we have reached the end of the
2727 case CPP_CLOSE_BRACE:
2728 /* If this is a non-nested '}', stop before consuming it.
2729 That way, when confronted with something like:
2733 we stop before consuming the closing '}', even though we
2734 have not yet reached a `;'. */
2735 if (nesting_depth == 0)
2738 /* If it is the closing '}' for a block that we have
2739 scanned, stop -- but only after consuming the token.
2745 we will stop after the body of the erroneously declared
2746 function, but before consuming the following `typedef'
2748 if (--nesting_depth == 0)
2750 cp_lexer_consume_token (parser->lexer);
2754 case CPP_OPEN_BRACE:
2762 /* Consume the token. */
2763 cp_lexer_consume_token (parser->lexer);
2767 /* This function is called at the end of a statement or declaration.
2768 If the next token is a semicolon, it is consumed; otherwise, error
2769 recovery is attempted. */
2772 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2774 /* Look for the trailing `;'. */
2775 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2777 /* If there is additional (erroneous) input, skip to the end of
2779 cp_parser_skip_to_end_of_statement (parser);
2780 /* If the next token is now a `;', consume it. */
2781 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2782 cp_lexer_consume_token (parser->lexer);
2786 /* Skip tokens until we have consumed an entire block, or until we
2787 have consumed a non-nested `;'. */
2790 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2792 int nesting_depth = 0;
2794 while (nesting_depth >= 0)
2796 cp_token *token = cp_lexer_peek_token (parser->lexer);
2798 switch (token->type)
2801 case CPP_PRAGMA_EOL:
2802 /* If we've run out of tokens, stop. */
2806 /* Stop if this is an unnested ';'. */
2811 case CPP_CLOSE_BRACE:
2812 /* Stop if this is an unnested '}', or closes the outermost
2815 if (nesting_depth < 0)
2821 case CPP_OPEN_BRACE:
2830 /* Consume the token. */
2831 cp_lexer_consume_token (parser->lexer);
2835 /* Skip tokens until a non-nested closing curly brace is the next
2836 token, or there are no more tokens. Return true in the first case,
2840 cp_parser_skip_to_closing_brace (cp_parser *parser)
2842 unsigned nesting_depth = 0;
2846 cp_token *token = cp_lexer_peek_token (parser->lexer);
2848 switch (token->type)
2851 case CPP_PRAGMA_EOL:
2852 /* If we've run out of tokens, stop. */
2855 case CPP_CLOSE_BRACE:
2856 /* If the next token is a non-nested `}', then we have reached
2857 the end of the current block. */
2858 if (nesting_depth-- == 0)
2862 case CPP_OPEN_BRACE:
2863 /* If it the next token is a `{', then we are entering a new
2864 block. Consume the entire block. */
2872 /* Consume the token. */
2873 cp_lexer_consume_token (parser->lexer);
2877 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2878 parameter is the PRAGMA token, allowing us to purge the entire pragma
2882 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2886 parser->lexer->in_pragma = false;
2889 token = cp_lexer_consume_token (parser->lexer);
2890 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2892 /* Ensure that the pragma is not parsed again. */
2893 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2896 /* Require pragma end of line, resyncing with it as necessary. The
2897 arguments are as for cp_parser_skip_to_pragma_eol. */
2900 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2902 parser->lexer->in_pragma = false;
2903 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2904 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2907 /* This is a simple wrapper around make_typename_type. When the id is
2908 an unresolved identifier node, we can provide a superior diagnostic
2909 using cp_parser_diagnose_invalid_type_name. */
2912 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2913 tree id, location_t id_location)
2916 if (TREE_CODE (id) == IDENTIFIER_NODE)
2918 result = make_typename_type (scope, id, typename_type,
2919 /*complain=*/tf_none);
2920 if (result == error_mark_node)
2921 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2924 return make_typename_type (scope, id, typename_type, tf_error);
2927 /* This is a wrapper around the
2928 make_{pointer,ptrmem,reference}_declarator functions that decides
2929 which one to call based on the CODE and CLASS_TYPE arguments. The
2930 CODE argument should be one of the values returned by
2931 cp_parser_ptr_operator. */
2932 static cp_declarator *
2933 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2934 cp_cv_quals cv_qualifiers,
2935 cp_declarator *target)
2937 if (code == ERROR_MARK)
2938 return cp_error_declarator;
2940 if (code == INDIRECT_REF)
2941 if (class_type == NULL_TREE)
2942 return make_pointer_declarator (cv_qualifiers, target);
2944 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2945 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2946 return make_reference_declarator (cv_qualifiers, target, false);
2947 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2948 return make_reference_declarator (cv_qualifiers, target, true);
2952 /* Create a new C++ parser. */
2955 cp_parser_new (void)
2961 /* cp_lexer_new_main is called before doing GC allocation because
2962 cp_lexer_new_main might load a PCH file. */
2963 lexer = cp_lexer_new_main ();
2965 /* Initialize the binops_by_token so that we can get the tree
2966 directly from the token. */
2967 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2968 binops_by_token[binops[i].token_type] = binops[i];
2970 parser = ggc_alloc_cleared_cp_parser ();
2971 parser->lexer = lexer;
2972 parser->context = cp_parser_context_new (NULL);
2974 /* For now, we always accept GNU extensions. */
2975 parser->allow_gnu_extensions_p = 1;
2977 /* The `>' token is a greater-than operator, not the end of a
2979 parser->greater_than_is_operator_p = true;
2981 parser->default_arg_ok_p = true;
2983 /* We are not parsing a constant-expression. */
2984 parser->integral_constant_expression_p = false;
2985 parser->allow_non_integral_constant_expression_p = false;
2986 parser->non_integral_constant_expression_p = false;
2988 /* Local variable names are not forbidden. */
2989 parser->local_variables_forbidden_p = false;
2991 /* We are not processing an `extern "C"' declaration. */
2992 parser->in_unbraced_linkage_specification_p = false;
2994 /* We are not processing a declarator. */
2995 parser->in_declarator_p = false;
2997 /* We are not processing a template-argument-list. */
2998 parser->in_template_argument_list_p = false;
3000 /* We are not in an iteration statement. */
3001 parser->in_statement = 0;
3003 /* We are not in a switch statement. */
3004 parser->in_switch_statement_p = false;
3006 /* We are not parsing a type-id inside an expression. */
3007 parser->in_type_id_in_expr_p = false;
3009 /* Declarations aren't implicitly extern "C". */
3010 parser->implicit_extern_c = false;
3012 /* String literals should be translated to the execution character set. */
3013 parser->translate_strings_p = true;
3015 /* We are not parsing a function body. */
3016 parser->in_function_body = false;
3018 /* We can correct until told otherwise. */
3019 parser->colon_corrects_to_scope_p = true;
3021 /* The unparsed function queue is empty. */
3022 push_unparsed_function_queues (parser);
3024 /* There are no classes being defined. */
3025 parser->num_classes_being_defined = 0;
3027 /* No template parameters apply. */
3028 parser->num_template_parameter_lists = 0;
3033 /* Create a cp_lexer structure which will emit the tokens in CACHE
3034 and push it onto the parser's lexer stack. This is used for delayed
3035 parsing of in-class method bodies and default arguments, and should
3036 not be confused with tentative parsing. */
3038 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3040 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3041 lexer->next = parser->lexer;
3042 parser->lexer = lexer;
3044 /* Move the current source position to that of the first token in the
3046 cp_lexer_set_source_position_from_token (lexer->next_token);
3049 /* Pop the top lexer off the parser stack. This is never used for the
3050 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3052 cp_parser_pop_lexer (cp_parser *parser)
3054 cp_lexer *lexer = parser->lexer;
3055 parser->lexer = lexer->next;
3056 cp_lexer_destroy (lexer);
3058 /* Put the current source position back where it was before this
3059 lexer was pushed. */
3060 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3063 /* Lexical conventions [gram.lex] */
3065 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3069 cp_parser_identifier (cp_parser* parser)
3073 /* Look for the identifier. */
3074 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3075 /* Return the value. */
3076 return token ? token->u.value : error_mark_node;
3079 /* Parse a sequence of adjacent string constants. Returns a
3080 TREE_STRING representing the combined, nul-terminated string
3081 constant. If TRANSLATE is true, translate the string to the
3082 execution character set. If WIDE_OK is true, a wide string is
3085 C++98 [lex.string] says that if a narrow string literal token is
3086 adjacent to a wide string literal token, the behavior is undefined.
3087 However, C99 6.4.5p4 says that this results in a wide string literal.
3088 We follow C99 here, for consistency with the C front end.
3090 This code is largely lifted from lex_string() in c-lex.c.
3092 FUTURE: ObjC++ will need to handle @-strings here. */
3094 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3098 struct obstack str_ob;
3099 cpp_string str, istr, *strs;
3101 enum cpp_ttype type;
3103 tok = cp_lexer_peek_token (parser->lexer);
3104 if (!cp_parser_is_string_literal (tok))
3106 cp_parser_error (parser, "expected string-literal");
3107 return error_mark_node;
3112 /* Try to avoid the overhead of creating and destroying an obstack
3113 for the common case of just one string. */
3114 if (!cp_parser_is_string_literal
3115 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3117 cp_lexer_consume_token (parser->lexer);
3119 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3120 str.len = TREE_STRING_LENGTH (tok->u.value);
3127 gcc_obstack_init (&str_ob);
3132 cp_lexer_consume_token (parser->lexer);
3134 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3135 str.len = TREE_STRING_LENGTH (tok->u.value);
3137 if (type != tok->type)
3139 if (type == CPP_STRING)
3141 else if (tok->type != CPP_STRING)
3142 error_at (tok->location,
3143 "unsupported non-standard concatenation "
3144 "of string literals");
3147 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3149 tok = cp_lexer_peek_token (parser->lexer);
3151 while (cp_parser_is_string_literal (tok));
3153 strs = (cpp_string *) obstack_finish (&str_ob);
3156 if (type != CPP_STRING && !wide_ok)
3158 cp_parser_error (parser, "a wide string is invalid in this context");
3162 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3163 (parse_in, strs, count, &istr, type))
3165 value = build_string (istr.len, (const char *)istr.text);
3166 free (CONST_CAST (unsigned char *, istr.text));
3172 case CPP_UTF8STRING:
3173 TREE_TYPE (value) = char_array_type_node;
3176 TREE_TYPE (value) = char16_array_type_node;
3179 TREE_TYPE (value) = char32_array_type_node;
3182 TREE_TYPE (value) = wchar_array_type_node;
3186 value = fix_string_type (value);
3189 /* cpp_interpret_string has issued an error. */
3190 value = error_mark_node;
3193 obstack_free (&str_ob, 0);
3199 /* Basic concepts [gram.basic] */
3201 /* Parse a translation-unit.
3204 declaration-seq [opt]
3206 Returns TRUE if all went well. */
3209 cp_parser_translation_unit (cp_parser* parser)
3211 /* The address of the first non-permanent object on the declarator
3213 static void *declarator_obstack_base;
3217 /* Create the declarator obstack, if necessary. */
3218 if (!cp_error_declarator)
3220 gcc_obstack_init (&declarator_obstack);
3221 /* Create the error declarator. */
3222 cp_error_declarator = make_declarator (cdk_error);
3223 /* Create the empty parameter list. */
3224 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3225 /* Remember where the base of the declarator obstack lies. */
3226 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3229 cp_parser_declaration_seq_opt (parser);
3231 /* If there are no tokens left then all went well. */
3232 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3234 /* Get rid of the token array; we don't need it any more. */
3235 cp_lexer_destroy (parser->lexer);
3236 parser->lexer = NULL;
3238 /* This file might have been a context that's implicitly extern
3239 "C". If so, pop the lang context. (Only relevant for PCH.) */
3240 if (parser->implicit_extern_c)
3242 pop_lang_context ();
3243 parser->implicit_extern_c = false;
3247 finish_translation_unit ();
3253 cp_parser_error (parser, "expected declaration");
3257 /* Make sure the declarator obstack was fully cleaned up. */
3258 gcc_assert (obstack_next_free (&declarator_obstack)
3259 == declarator_obstack_base);
3261 /* All went well. */
3265 /* Expressions [gram.expr] */
3267 /* Parse a primary-expression.
3278 ( compound-statement )
3279 __builtin_va_arg ( assignment-expression , type-id )
3280 __builtin_offsetof ( type-id , offsetof-expression )
3283 __has_nothrow_assign ( type-id )
3284 __has_nothrow_constructor ( type-id )
3285 __has_nothrow_copy ( type-id )
3286 __has_trivial_assign ( type-id )
3287 __has_trivial_constructor ( type-id )
3288 __has_trivial_copy ( type-id )
3289 __has_trivial_destructor ( type-id )
3290 __has_virtual_destructor ( type-id )
3291 __is_abstract ( type-id )
3292 __is_base_of ( type-id , type-id )
3293 __is_class ( type-id )
3294 __is_convertible_to ( type-id , type-id )
3295 __is_empty ( type-id )
3296 __is_enum ( type-id )
3297 __is_literal_type ( type-id )
3298 __is_pod ( type-id )
3299 __is_polymorphic ( type-id )
3300 __is_std_layout ( type-id )
3301 __is_trivial ( type-id )
3302 __is_union ( type-id )
3304 Objective-C++ Extension:
3312 ADDRESS_P is true iff this expression was immediately preceded by
3313 "&" and therefore might denote a pointer-to-member. CAST_P is true
3314 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3315 true iff this expression is a template argument.
3317 Returns a representation of the expression. Upon return, *IDK
3318 indicates what kind of id-expression (if any) was present. */
3321 cp_parser_primary_expression (cp_parser *parser,
3324 bool template_arg_p,
3327 cp_token *token = NULL;
3329 /* Assume the primary expression is not an id-expression. */
3330 *idk = CP_ID_KIND_NONE;
3332 /* Peek at the next token. */
3333 token = cp_lexer_peek_token (parser->lexer);
3334 switch (token->type)
3347 token = cp_lexer_consume_token (parser->lexer);
3348 if (TREE_CODE (token->u.value) == FIXED_CST)
3350 error_at (token->location,
3351 "fixed-point types not supported in C++");
3352 return error_mark_node;
3354 /* Floating-point literals are only allowed in an integral
3355 constant expression if they are cast to an integral or
3356 enumeration type. */
3357 if (TREE_CODE (token->u.value) == REAL_CST
3358 && parser->integral_constant_expression_p
3361 /* CAST_P will be set even in invalid code like "int(2.7 +
3362 ...)". Therefore, we have to check that the next token
3363 is sure to end the cast. */
3366 cp_token *next_token;
3368 next_token = cp_lexer_peek_token (parser->lexer);
3369 if (/* The comma at the end of an
3370 enumerator-definition. */
3371 next_token->type != CPP_COMMA
3372 /* The curly brace at the end of an enum-specifier. */
3373 && next_token->type != CPP_CLOSE_BRACE
3374 /* The end of a statement. */
3375 && next_token->type != CPP_SEMICOLON
3376 /* The end of the cast-expression. */
3377 && next_token->type != CPP_CLOSE_PAREN
3378 /* The end of an array bound. */
3379 && next_token->type != CPP_CLOSE_SQUARE
3380 /* The closing ">" in a template-argument-list. */
3381 && (next_token->type != CPP_GREATER
3382 || parser->greater_than_is_operator_p)
3383 /* C++0x only: A ">>" treated like two ">" tokens,
3384 in a template-argument-list. */
3385 && (next_token->type != CPP_RSHIFT
3386 || (cxx_dialect == cxx98)
3387 || parser->greater_than_is_operator_p))
3391 /* If we are within a cast, then the constraint that the
3392 cast is to an integral or enumeration type will be
3393 checked at that point. If we are not within a cast, then
3394 this code is invalid. */
3396 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3398 return token->u.value;
3404 case CPP_UTF8STRING:
3405 /* ??? Should wide strings be allowed when parser->translate_strings_p
3406 is false (i.e. in attributes)? If not, we can kill the third
3407 argument to cp_parser_string_literal. */
3408 return cp_parser_string_literal (parser,
3409 parser->translate_strings_p,
3412 case CPP_OPEN_PAREN:
3415 bool saved_greater_than_is_operator_p;
3417 /* Consume the `('. */
3418 cp_lexer_consume_token (parser->lexer);
3419 /* Within a parenthesized expression, a `>' token is always
3420 the greater-than operator. */
3421 saved_greater_than_is_operator_p
3422 = parser->greater_than_is_operator_p;
3423 parser->greater_than_is_operator_p = true;
3424 /* If we see `( { ' then we are looking at the beginning of
3425 a GNU statement-expression. */
3426 if (cp_parser_allow_gnu_extensions_p (parser)
3427 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3429 /* Statement-expressions are not allowed by the standard. */
3430 pedwarn (token->location, OPT_pedantic,
3431 "ISO C++ forbids braced-groups within expressions");
3433 /* And they're not allowed outside of a function-body; you
3434 cannot, for example, write:
3436 int i = ({ int j = 3; j + 1; });
3438 at class or namespace scope. */
3439 if (!parser->in_function_body
3440 || parser->in_template_argument_list_p)
3442 error_at (token->location,
3443 "statement-expressions are not allowed outside "
3444 "functions nor in template-argument lists");
3445 cp_parser_skip_to_end_of_block_or_statement (parser);
3446 expr = error_mark_node;
3450 /* Start the statement-expression. */
3451 expr = begin_stmt_expr ();
3452 /* Parse the compound-statement. */
3453 cp_parser_compound_statement (parser, expr, false, false);
3455 expr = finish_stmt_expr (expr, false);
3460 /* Parse the parenthesized expression. */
3461 expr = cp_parser_expression (parser, cast_p, idk);
3462 /* Let the front end know that this expression was
3463 enclosed in parentheses. This matters in case, for
3464 example, the expression is of the form `A::B', since
3465 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3467 finish_parenthesized_expr (expr);
3468 /* DR 705: Wrapping an unqualified name in parentheses
3469 suppresses arg-dependent lookup. We want to pass back
3470 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3471 (c++/37862), but none of the others. */
3472 if (*idk != CP_ID_KIND_QUALIFIED)
3473 *idk = CP_ID_KIND_NONE;
3475 /* The `>' token might be the end of a template-id or
3476 template-parameter-list now. */
3477 parser->greater_than_is_operator_p
3478 = saved_greater_than_is_operator_p;
3479 /* Consume the `)'. */
3480 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3481 cp_parser_skip_to_end_of_statement (parser);
3486 case CPP_OPEN_SQUARE:
3487 if (c_dialect_objc ())
3488 /* We have an Objective-C++ message. */
3489 return cp_parser_objc_expression (parser);
3491 tree lam = cp_parser_lambda_expression (parser);
3492 /* Don't warn about a failed tentative parse. */
3493 if (cp_parser_error_occurred (parser))
3494 return error_mark_node;
3495 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3499 case CPP_OBJC_STRING:
3500 if (c_dialect_objc ())
3501 /* We have an Objective-C++ string literal. */
3502 return cp_parser_objc_expression (parser);
3503 cp_parser_error (parser, "expected primary-expression");
3504 return error_mark_node;
3507 switch (token->keyword)
3509 /* These two are the boolean literals. */
3511 cp_lexer_consume_token (parser->lexer);
3512 return boolean_true_node;
3514 cp_lexer_consume_token (parser->lexer);
3515 return boolean_false_node;
3517 /* The `__null' literal. */
3519 cp_lexer_consume_token (parser->lexer);
3522 /* The `nullptr' literal. */
3524 cp_lexer_consume_token (parser->lexer);
3525 return nullptr_node;
3527 /* Recognize the `this' keyword. */
3529 cp_lexer_consume_token (parser->lexer);
3530 if (parser->local_variables_forbidden_p)
3532 error_at (token->location,
3533 "%<this%> may not be used in this context");
3534 return error_mark_node;
3536 /* Pointers cannot appear in constant-expressions. */
3537 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3538 return error_mark_node;
3539 return finish_this_expr ();
3541 /* The `operator' keyword can be the beginning of an
3546 case RID_FUNCTION_NAME:
3547 case RID_PRETTY_FUNCTION_NAME:
3548 case RID_C99_FUNCTION_NAME:
3550 non_integral_constant name;
3552 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3553 __func__ are the names of variables -- but they are
3554 treated specially. Therefore, they are handled here,
3555 rather than relying on the generic id-expression logic
3556 below. Grammatically, these names are id-expressions.
3558 Consume the token. */
3559 token = cp_lexer_consume_token (parser->lexer);
3561 switch (token->keyword)
3563 case RID_FUNCTION_NAME:
3564 name = NIC_FUNC_NAME;
3566 case RID_PRETTY_FUNCTION_NAME:
3567 name = NIC_PRETTY_FUNC;
3569 case RID_C99_FUNCTION_NAME:
3570 name = NIC_C99_FUNC;
3576 if (cp_parser_non_integral_constant_expression (parser, name))
3577 return error_mark_node;
3579 /* Look up the name. */
3580 return finish_fname (token->u.value);
3588 /* The `__builtin_va_arg' construct is used to handle
3589 `va_arg'. Consume the `__builtin_va_arg' token. */
3590 cp_lexer_consume_token (parser->lexer);
3591 /* Look for the opening `('. */
3592 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3593 /* Now, parse the assignment-expression. */
3594 expression = cp_parser_assignment_expression (parser,
3595 /*cast_p=*/false, NULL);
3596 /* Look for the `,'. */
3597 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3598 /* Parse the type-id. */
3599 type = cp_parser_type_id (parser);
3600 /* Look for the closing `)'. */
3601 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3602 /* Using `va_arg' in a constant-expression is not
3604 if (cp_parser_non_integral_constant_expression (parser,
3606 return error_mark_node;
3607 return build_x_va_arg (expression, type);
3611 return cp_parser_builtin_offsetof (parser);
3613 case RID_HAS_NOTHROW_ASSIGN:
3614 case RID_HAS_NOTHROW_CONSTRUCTOR:
3615 case RID_HAS_NOTHROW_COPY:
3616 case RID_HAS_TRIVIAL_ASSIGN:
3617 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3618 case RID_HAS_TRIVIAL_COPY:
3619 case RID_HAS_TRIVIAL_DESTRUCTOR:
3620 case RID_HAS_VIRTUAL_DESTRUCTOR:
3621 case RID_IS_ABSTRACT:
3622 case RID_IS_BASE_OF:
3624 case RID_IS_CONVERTIBLE_TO:
3627 case RID_IS_LITERAL_TYPE:
3629 case RID_IS_POLYMORPHIC:
3630 case RID_IS_STD_LAYOUT:
3631 case RID_IS_TRIVIAL:
3633 return cp_parser_trait_expr (parser, token->keyword);
3635 /* Objective-C++ expressions. */
3637 case RID_AT_PROTOCOL:
3638 case RID_AT_SELECTOR:
3639 return cp_parser_objc_expression (parser);
3642 if (parser->in_function_body
3643 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3646 error_at (token->location,
3647 "a template declaration cannot appear at block scope");
3648 cp_parser_skip_to_end_of_block_or_statement (parser);
3649 return error_mark_node;
3652 cp_parser_error (parser, "expected primary-expression");
3653 return error_mark_node;
3656 /* An id-expression can start with either an identifier, a
3657 `::' as the beginning of a qualified-id, or the "operator"
3661 case CPP_TEMPLATE_ID:
3662 case CPP_NESTED_NAME_SPECIFIER:
3666 const char *error_msg;
3669 cp_token *id_expr_token;
3672 /* Parse the id-expression. */
3674 = cp_parser_id_expression (parser,
3675 /*template_keyword_p=*/false,
3676 /*check_dependency_p=*/true,
3678 /*declarator_p=*/false,
3679 /*optional_p=*/false);
3680 if (id_expression == error_mark_node)
3681 return error_mark_node;
3682 id_expr_token = token;
3683 token = cp_lexer_peek_token (parser->lexer);
3684 done = (token->type != CPP_OPEN_SQUARE
3685 && token->type != CPP_OPEN_PAREN
3686 && token->type != CPP_DOT
3687 && token->type != CPP_DEREF
3688 && token->type != CPP_PLUS_PLUS
3689 && token->type != CPP_MINUS_MINUS);
3690 /* If we have a template-id, then no further lookup is
3691 required. If the template-id was for a template-class, we
3692 will sometimes have a TYPE_DECL at this point. */
3693 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3694 || TREE_CODE (id_expression) == TYPE_DECL)
3695 decl = id_expression;
3696 /* Look up the name. */
3699 tree ambiguous_decls;
3701 /* If we already know that this lookup is ambiguous, then
3702 we've already issued an error message; there's no reason
3704 if (id_expr_token->type == CPP_NAME
3705 && id_expr_token->ambiguous_p)
3707 cp_parser_simulate_error (parser);
3708 return error_mark_node;
3711 decl = cp_parser_lookup_name (parser, id_expression,
3714 /*is_namespace=*/false,
3715 /*check_dependency=*/true,
3717 id_expr_token->location);
3718 /* If the lookup was ambiguous, an error will already have
3720 if (ambiguous_decls)
3721 return error_mark_node;
3723 /* In Objective-C++, we may have an Objective-C 2.0
3724 dot-syntax for classes here. */
3725 if (c_dialect_objc ()
3726 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3727 && TREE_CODE (decl) == TYPE_DECL
3728 && objc_is_class_name (decl))
3731 cp_lexer_consume_token (parser->lexer);
3732 component = cp_parser_identifier (parser);
3733 if (component == error_mark_node)
3734 return error_mark_node;
3736 return objc_build_class_component_ref (id_expression, component);
3739 /* In Objective-C++, an instance variable (ivar) may be preferred
3740 to whatever cp_parser_lookup_name() found. */
3741 decl = objc_lookup_ivar (decl, id_expression);
3743 /* If name lookup gives us a SCOPE_REF, then the
3744 qualifying scope was dependent. */
3745 if (TREE_CODE (decl) == SCOPE_REF)
3747 /* At this point, we do not know if DECL is a valid
3748 integral constant expression. We assume that it is
3749 in fact such an expression, so that code like:
3751 template <int N> struct A {
3755 is accepted. At template-instantiation time, we
3756 will check that B<N>::i is actually a constant. */
3759 /* Check to see if DECL is a local variable in a context
3760 where that is forbidden. */
3761 if (parser->local_variables_forbidden_p
3762 && local_variable_p (decl))
3764 /* It might be that we only found DECL because we are
3765 trying to be generous with pre-ISO scoping rules.
3766 For example, consider:
3770 for (int i = 0; i < 10; ++i) {}
3771 extern void f(int j = i);
3774 Here, name look up will originally find the out
3775 of scope `i'. We need to issue a warning message,
3776 but then use the global `i'. */
3777 decl = check_for_out_of_scope_variable (decl);
3778 if (local_variable_p (decl))
3780 error_at (id_expr_token->location,
3781 "local variable %qD may not appear in this context",
3783 return error_mark_node;
3788 decl = (finish_id_expression
3789 (id_expression, decl, parser->scope,
3791 parser->integral_constant_expression_p,
3792 parser->allow_non_integral_constant_expression_p,
3793 &parser->non_integral_constant_expression_p,
3794 template_p, done, address_p,
3797 id_expr_token->location));
3799 cp_parser_error (parser, error_msg);
3803 /* Anything else is an error. */
3805 cp_parser_error (parser, "expected primary-expression");
3806 return error_mark_node;
3810 /* Parse an id-expression.
3817 :: [opt] nested-name-specifier template [opt] unqualified-id
3819 :: operator-function-id
3822 Return a representation of the unqualified portion of the
3823 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3824 a `::' or nested-name-specifier.
3826 Often, if the id-expression was a qualified-id, the caller will
3827 want to make a SCOPE_REF to represent the qualified-id. This
3828 function does not do this in order to avoid wastefully creating
3829 SCOPE_REFs when they are not required.
3831 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3834 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3835 uninstantiated templates.
3837 If *TEMPLATE_P is non-NULL, it is set to true iff the
3838 `template' keyword is used to explicitly indicate that the entity
3839 named is a template.
3841 If DECLARATOR_P is true, the id-expression is appearing as part of
3842 a declarator, rather than as part of an expression. */
3845 cp_parser_id_expression (cp_parser *parser,
3846 bool template_keyword_p,
3847 bool check_dependency_p,
3852 bool global_scope_p;
3853 bool nested_name_specifier_p;
3855 /* Assume the `template' keyword was not used. */
3857 *template_p = template_keyword_p;
3859 /* Look for the optional `::' operator. */
3861 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3863 /* Look for the optional nested-name-specifier. */
3864 nested_name_specifier_p
3865 = (cp_parser_nested_name_specifier_opt (parser,
3866 /*typename_keyword_p=*/false,
3871 /* If there is a nested-name-specifier, then we are looking at
3872 the first qualified-id production. */
3873 if (nested_name_specifier_p)
3876 tree saved_object_scope;
3877 tree saved_qualifying_scope;
3878 tree unqualified_id;
3881 /* See if the next token is the `template' keyword. */
3883 template_p = &is_template;
3884 *template_p = cp_parser_optional_template_keyword (parser);
3885 /* Name lookup we do during the processing of the
3886 unqualified-id might obliterate SCOPE. */
3887 saved_scope = parser->scope;
3888 saved_object_scope = parser->object_scope;
3889 saved_qualifying_scope = parser->qualifying_scope;
3890 /* Process the final unqualified-id. */
3891 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3894 /*optional_p=*/false);
3895 /* Restore the SAVED_SCOPE for our caller. */
3896 parser->scope = saved_scope;
3897 parser->object_scope = saved_object_scope;
3898 parser->qualifying_scope = saved_qualifying_scope;
3900 return unqualified_id;
3902 /* Otherwise, if we are in global scope, then we are looking at one
3903 of the other qualified-id productions. */
3904 else if (global_scope_p)
3909 /* Peek at the next token. */
3910 token = cp_lexer_peek_token (parser->lexer);
3912 /* If it's an identifier, and the next token is not a "<", then
3913 we can avoid the template-id case. This is an optimization
3914 for this common case. */
3915 if (token->type == CPP_NAME
3916 && !cp_parser_nth_token_starts_template_argument_list_p
3918 return cp_parser_identifier (parser);
3920 cp_parser_parse_tentatively (parser);
3921 /* Try a template-id. */
3922 id = cp_parser_template_id (parser,
3923 /*template_keyword_p=*/false,
3924 /*check_dependency_p=*/true,
3926 /* If that worked, we're done. */
3927 if (cp_parser_parse_definitely (parser))
3930 /* Peek at the next token. (Changes in the token buffer may
3931 have invalidated the pointer obtained above.) */
3932 token = cp_lexer_peek_token (parser->lexer);
3934 switch (token->type)
3937 return cp_parser_identifier (parser);
3940 if (token->keyword == RID_OPERATOR)
3941 return cp_parser_operator_function_id (parser);
3945 cp_parser_error (parser, "expected id-expression");
3946 return error_mark_node;
3950 return cp_parser_unqualified_id (parser, template_keyword_p,
3951 /*check_dependency_p=*/true,
3956 /* Parse an unqualified-id.
3960 operator-function-id
3961 conversion-function-id
3965 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3966 keyword, in a construct like `A::template ...'.
3968 Returns a representation of unqualified-id. For the `identifier'
3969 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3970 production a BIT_NOT_EXPR is returned; the operand of the
3971 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3972 other productions, see the documentation accompanying the
3973 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3974 names are looked up in uninstantiated templates. If DECLARATOR_P
3975 is true, the unqualified-id is appearing as part of a declarator,
3976 rather than as part of an expression. */
3979 cp_parser_unqualified_id (cp_parser* parser,
3980 bool template_keyword_p,
3981 bool check_dependency_p,
3987 /* Peek at the next token. */
3988 token = cp_lexer_peek_token (parser->lexer);
3990 switch (token->type)
3996 /* We don't know yet whether or not this will be a
3998 cp_parser_parse_tentatively (parser);
3999 /* Try a template-id. */
4000 id = cp_parser_template_id (parser, template_keyword_p,
4003 /* If it worked, we're done. */
4004 if (cp_parser_parse_definitely (parser))
4006 /* Otherwise, it's an ordinary identifier. */
4007 return cp_parser_identifier (parser);
4010 case CPP_TEMPLATE_ID:
4011 return cp_parser_template_id (parser, template_keyword_p,
4018 tree qualifying_scope;
4023 /* Consume the `~' token. */
4024 cp_lexer_consume_token (parser->lexer);
4025 /* Parse the class-name. The standard, as written, seems to
4028 template <typename T> struct S { ~S (); };
4029 template <typename T> S<T>::~S() {}
4031 is invalid, since `~' must be followed by a class-name, but
4032 `S<T>' is dependent, and so not known to be a class.
4033 That's not right; we need to look in uninstantiated
4034 templates. A further complication arises from:
4036 template <typename T> void f(T t) {
4040 Here, it is not possible to look up `T' in the scope of `T'
4041 itself. We must look in both the current scope, and the
4042 scope of the containing complete expression.
4044 Yet another issue is:
4053 The standard does not seem to say that the `S' in `~S'
4054 should refer to the type `S' and not the data member
4057 /* DR 244 says that we look up the name after the "~" in the
4058 same scope as we looked up the qualifying name. That idea
4059 isn't fully worked out; it's more complicated than that. */
4060 scope = parser->scope;
4061 object_scope = parser->object_scope;
4062 qualifying_scope = parser->qualifying_scope;
4064 /* Check for invalid scopes. */
4065 if (scope == error_mark_node)
4067 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4068 cp_lexer_consume_token (parser->lexer);
4069 return error_mark_node;
4071 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4073 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4074 error_at (token->location,
4075 "scope %qT before %<~%> is not a class-name",
4077 cp_parser_simulate_error (parser);
4078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4079 cp_lexer_consume_token (parser->lexer);
4080 return error_mark_node;
4082 gcc_assert (!scope || TYPE_P (scope));
4084 /* If the name is of the form "X::~X" it's OK even if X is a
4086 token = cp_lexer_peek_token (parser->lexer);
4088 && token->type == CPP_NAME
4089 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4091 && (token->u.value == TYPE_IDENTIFIER (scope)
4092 || (CLASS_TYPE_P (scope)
4093 && constructor_name_p (token->u.value, scope))))
4095 cp_lexer_consume_token (parser->lexer);
4096 return build_nt (BIT_NOT_EXPR, scope);
4099 /* If there was an explicit qualification (S::~T), first look
4100 in the scope given by the qualification (i.e., S).
4102 Note: in the calls to cp_parser_class_name below we pass
4103 typename_type so that lookup finds the injected-class-name
4104 rather than the constructor. */
4106 type_decl = NULL_TREE;
4109 cp_parser_parse_tentatively (parser);
4110 type_decl = cp_parser_class_name (parser,
4111 /*typename_keyword_p=*/false,
4112 /*template_keyword_p=*/false,
4114 /*check_dependency=*/false,
4115 /*class_head_p=*/false,
4117 if (cp_parser_parse_definitely (parser))
4120 /* In "N::S::~S", look in "N" as well. */
4121 if (!done && scope && qualifying_scope)
4123 cp_parser_parse_tentatively (parser);
4124 parser->scope = qualifying_scope;
4125 parser->object_scope = NULL_TREE;
4126 parser->qualifying_scope = NULL_TREE;
4128 = cp_parser_class_name (parser,
4129 /*typename_keyword_p=*/false,
4130 /*template_keyword_p=*/false,
4132 /*check_dependency=*/false,
4133 /*class_head_p=*/false,
4135 if (cp_parser_parse_definitely (parser))
4138 /* In "p->S::~T", look in the scope given by "*p" as well. */
4139 else if (!done && object_scope)
4141 cp_parser_parse_tentatively (parser);
4142 parser->scope = object_scope;
4143 parser->object_scope = NULL_TREE;
4144 parser->qualifying_scope = NULL_TREE;
4146 = cp_parser_class_name (parser,
4147 /*typename_keyword_p=*/false,
4148 /*template_keyword_p=*/false,
4150 /*check_dependency=*/false,
4151 /*class_head_p=*/false,
4153 if (cp_parser_parse_definitely (parser))
4156 /* Look in the surrounding context. */
4159 parser->scope = NULL_TREE;
4160 parser->object_scope = NULL_TREE;
4161 parser->qualifying_scope = NULL_TREE;
4162 if (processing_template_decl)
4163 cp_parser_parse_tentatively (parser);
4165 = cp_parser_class_name (parser,
4166 /*typename_keyword_p=*/false,
4167 /*template_keyword_p=*/false,
4169 /*check_dependency=*/false,
4170 /*class_head_p=*/false,
4172 if (processing_template_decl
4173 && ! cp_parser_parse_definitely (parser))
4175 /* We couldn't find a type with this name, so just accept
4176 it and check for a match at instantiation time. */
4177 type_decl = cp_parser_identifier (parser);
4178 if (type_decl != error_mark_node)
4179 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4183 /* If an error occurred, assume that the name of the
4184 destructor is the same as the name of the qualifying
4185 class. That allows us to keep parsing after running
4186 into ill-formed destructor names. */
4187 if (type_decl == error_mark_node && scope)
4188 return build_nt (BIT_NOT_EXPR, scope);
4189 else if (type_decl == error_mark_node)
4190 return error_mark_node;
4192 /* Check that destructor name and scope match. */
4193 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4195 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4196 error_at (token->location,
4197 "declaration of %<~%T%> as member of %qT",
4199 cp_parser_simulate_error (parser);
4200 return error_mark_node;
4205 A typedef-name that names a class shall not be used as the
4206 identifier in the declarator for a destructor declaration. */
4208 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4209 && !DECL_SELF_REFERENCE_P (type_decl)
4210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4211 error_at (token->location,
4212 "typedef-name %qD used as destructor declarator",
4215 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4219 if (token->keyword == RID_OPERATOR)
4223 /* This could be a template-id, so we try that first. */
4224 cp_parser_parse_tentatively (parser);
4225 /* Try a template-id. */
4226 id = cp_parser_template_id (parser, template_keyword_p,
4227 /*check_dependency_p=*/true,
4229 /* If that worked, we're done. */
4230 if (cp_parser_parse_definitely (parser))
4232 /* We still don't know whether we're looking at an
4233 operator-function-id or a conversion-function-id. */
4234 cp_parser_parse_tentatively (parser);
4235 /* Try an operator-function-id. */
4236 id = cp_parser_operator_function_id (parser);
4237 /* If that didn't work, try a conversion-function-id. */
4238 if (!cp_parser_parse_definitely (parser))
4239 id = cp_parser_conversion_function_id (parser);
4248 cp_parser_error (parser, "expected unqualified-id");
4249 return error_mark_node;
4253 /* Parse an (optional) nested-name-specifier.
4255 nested-name-specifier: [C++98]
4256 class-or-namespace-name :: nested-name-specifier [opt]
4257 class-or-namespace-name :: template nested-name-specifier [opt]
4259 nested-name-specifier: [C++0x]
4262 nested-name-specifier identifier ::
4263 nested-name-specifier template [opt] simple-template-id ::
4265 PARSER->SCOPE should be set appropriately before this function is
4266 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4267 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4270 Sets PARSER->SCOPE to the class (TYPE) or namespace
4271 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4272 it unchanged if there is no nested-name-specifier. Returns the new
4273 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4275 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4276 part of a declaration and/or decl-specifier. */
4279 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4280 bool typename_keyword_p,
4281 bool check_dependency_p,
4283 bool is_declaration)
4285 bool success = false;
4286 cp_token_position start = 0;
4289 /* Remember where the nested-name-specifier starts. */
4290 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4292 start = cp_lexer_token_position (parser->lexer, false);
4293 push_deferring_access_checks (dk_deferred);
4300 tree saved_qualifying_scope;
4301 bool template_keyword_p;
4303 /* Spot cases that cannot be the beginning of a
4304 nested-name-specifier. */
4305 token = cp_lexer_peek_token (parser->lexer);
4307 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4308 the already parsed nested-name-specifier. */
4309 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4311 /* Grab the nested-name-specifier and continue the loop. */
4312 cp_parser_pre_parsed_nested_name_specifier (parser);
4313 /* If we originally encountered this nested-name-specifier
4314 with IS_DECLARATION set to false, we will not have
4315 resolved TYPENAME_TYPEs, so we must do so here. */
4317 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4319 new_scope = resolve_typename_type (parser->scope,
4320 /*only_current_p=*/false);
4321 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4322 parser->scope = new_scope;
4328 /* Spot cases that cannot be the beginning of a
4329 nested-name-specifier. On the second and subsequent times
4330 through the loop, we look for the `template' keyword. */
4331 if (success && token->keyword == RID_TEMPLATE)
4333 /* A template-id can start a nested-name-specifier. */
4334 else if (token->type == CPP_TEMPLATE_ID)
4336 /* DR 743: decltype can be used in a nested-name-specifier. */
4337 else if (token_is_decltype (token))
4341 /* If the next token is not an identifier, then it is
4342 definitely not a type-name or namespace-name. */
4343 if (token->type != CPP_NAME)
4345 /* If the following token is neither a `<' (to begin a
4346 template-id), nor a `::', then we are not looking at a
4347 nested-name-specifier. */
4348 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4350 if (token->type == CPP_COLON
4351 && parser->colon_corrects_to_scope_p
4352 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4354 error_at (token->location,
4355 "found %<:%> in nested-name-specifier, expected %<::%>");
4356 token->type = CPP_SCOPE;
4359 if (token->type != CPP_SCOPE
4360 && !cp_parser_nth_token_starts_template_argument_list_p
4365 /* The nested-name-specifier is optional, so we parse
4367 cp_parser_parse_tentatively (parser);
4369 /* Look for the optional `template' keyword, if this isn't the
4370 first time through the loop. */
4372 template_keyword_p = cp_parser_optional_template_keyword (parser);
4374 template_keyword_p = false;
4376 /* Save the old scope since the name lookup we are about to do
4377 might destroy it. */
4378 old_scope = parser->scope;
4379 saved_qualifying_scope = parser->qualifying_scope;
4380 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4381 look up names in "X<T>::I" in order to determine that "Y" is
4382 a template. So, if we have a typename at this point, we make
4383 an effort to look through it. */
4385 && !typename_keyword_p
4387 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4388 parser->scope = resolve_typename_type (parser->scope,
4389 /*only_current_p=*/false);
4390 /* Parse the qualifying entity. */
4392 = cp_parser_qualifying_entity (parser,
4398 /* Look for the `::' token. */
4399 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4401 /* If we found what we wanted, we keep going; otherwise, we're
4403 if (!cp_parser_parse_definitely (parser))
4405 bool error_p = false;
4407 /* Restore the OLD_SCOPE since it was valid before the
4408 failed attempt at finding the last
4409 class-or-namespace-name. */
4410 parser->scope = old_scope;
4411 parser->qualifying_scope = saved_qualifying_scope;
4413 /* If the next token is a decltype, and the one after that is a
4414 `::', then the decltype has failed to resolve to a class or
4415 enumeration type. Give this error even when parsing
4416 tentatively since it can't possibly be valid--and we're going
4417 to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4418 won't get another chance.*/
4419 if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4420 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4423 token = cp_lexer_consume_token (parser->lexer);
4424 error_at (token->location, "decltype evaluates to %qT, "
4425 "which is not a class or enumeration type",
4427 parser->scope = error_mark_node;
4431 cp_lexer_consume_token (parser->lexer);
4434 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4436 /* If the next token is an identifier, and the one after
4437 that is a `::', then any valid interpretation would have
4438 found a class-or-namespace-name. */
4439 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4440 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4442 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4445 token = cp_lexer_consume_token (parser->lexer);
4448 if (!token->ambiguous_p)
4451 tree ambiguous_decls;
4453 decl = cp_parser_lookup_name (parser, token->u.value,
4455 /*is_template=*/false,
4456 /*is_namespace=*/false,
4457 /*check_dependency=*/true,
4460 if (TREE_CODE (decl) == TEMPLATE_DECL)
4461 error_at (token->location,
4462 "%qD used without template parameters",
4464 else if (ambiguous_decls)
4466 error_at (token->location,
4467 "reference to %qD is ambiguous",
4469 print_candidates (ambiguous_decls);
4470 decl = error_mark_node;
4474 if (cxx_dialect != cxx98)
4475 cp_parser_name_lookup_error
4476 (parser, token->u.value, decl, NLE_NOT_CXX98,
4479 cp_parser_name_lookup_error
4480 (parser, token->u.value, decl, NLE_CXX98,
4484 parser->scope = error_mark_node;
4486 /* Treat this as a successful nested-name-specifier
4491 If the name found is not a class-name (clause
4492 _class_) or namespace-name (_namespace.def_), the
4493 program is ill-formed. */
4496 cp_lexer_consume_token (parser->lexer);
4500 /* We've found one valid nested-name-specifier. */
4502 /* Name lookup always gives us a DECL. */
4503 if (TREE_CODE (new_scope) == TYPE_DECL)
4504 new_scope = TREE_TYPE (new_scope);
4505 /* Uses of "template" must be followed by actual templates. */
4506 if (template_keyword_p
4507 && !(CLASS_TYPE_P (new_scope)
4508 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4509 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4510 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4511 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4512 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4513 == TEMPLATE_ID_EXPR)))
4514 permerror (input_location, TYPE_P (new_scope)
4515 ? "%qT is not a template"
4516 : "%qD is not a template",
4518 /* If it is a class scope, try to complete it; we are about to
4519 be looking up names inside the class. */
4520 if (TYPE_P (new_scope)
4521 /* Since checking types for dependency can be expensive,
4522 avoid doing it if the type is already complete. */
4523 && !COMPLETE_TYPE_P (new_scope)
4524 /* Do not try to complete dependent types. */
4525 && !dependent_type_p (new_scope))
4527 new_scope = complete_type (new_scope);
4528 /* If it is a typedef to current class, use the current
4529 class instead, as the typedef won't have any names inside
4531 if (!COMPLETE_TYPE_P (new_scope)
4532 && currently_open_class (new_scope))
4533 new_scope = TYPE_MAIN_VARIANT (new_scope);
4535 /* Make sure we look in the right scope the next time through
4537 parser->scope = new_scope;
4540 /* If parsing tentatively, replace the sequence of tokens that makes
4541 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4542 token. That way, should we re-parse the token stream, we will
4543 not have to repeat the effort required to do the parse, nor will
4544 we issue duplicate error messages. */
4545 if (success && start)
4549 token = cp_lexer_token_at (parser->lexer, start);
4550 /* Reset the contents of the START token. */
4551 token->type = CPP_NESTED_NAME_SPECIFIER;
4552 /* Retrieve any deferred checks. Do not pop this access checks yet
4553 so the memory will not be reclaimed during token replacing below. */
4554 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4555 token->u.tree_check_value->value = parser->scope;
4556 token->u.tree_check_value->checks = get_deferred_access_checks ();
4557 token->u.tree_check_value->qualifying_scope =
4558 parser->qualifying_scope;
4559 token->keyword = RID_MAX;
4561 /* Purge all subsequent tokens. */
4562 cp_lexer_purge_tokens_after (parser->lexer, start);
4566 pop_to_parent_deferring_access_checks ();
4568 return success ? parser->scope : NULL_TREE;
4571 /* Parse a nested-name-specifier. See
4572 cp_parser_nested_name_specifier_opt for details. This function
4573 behaves identically, except that it will an issue an error if no
4574 nested-name-specifier is present. */
4577 cp_parser_nested_name_specifier (cp_parser *parser,
4578 bool typename_keyword_p,
4579 bool check_dependency_p,
4581 bool is_declaration)
4585 /* Look for the nested-name-specifier. */
4586 scope = cp_parser_nested_name_specifier_opt (parser,
4591 /* If it was not present, issue an error message. */
4594 cp_parser_error (parser, "expected nested-name-specifier");
4595 parser->scope = NULL_TREE;
4601 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4602 this is either a class-name or a namespace-name (which corresponds
4603 to the class-or-namespace-name production in the grammar). For
4604 C++0x, it can also be a type-name that refers to an enumeration
4607 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4608 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4609 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4610 TYPE_P is TRUE iff the next name should be taken as a class-name,
4611 even the same name is declared to be another entity in the same
4614 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4615 specified by the class-or-namespace-name. If neither is found the
4616 ERROR_MARK_NODE is returned. */
4619 cp_parser_qualifying_entity (cp_parser *parser,
4620 bool typename_keyword_p,
4621 bool template_keyword_p,
4622 bool check_dependency_p,
4624 bool is_declaration)
4627 tree saved_qualifying_scope;
4628 tree saved_object_scope;
4631 bool successful_parse_p;
4633 /* DR 743: decltype can appear in a nested-name-specifier. */
4634 if (cp_lexer_next_token_is_decltype (parser->lexer))
4636 scope = cp_parser_decltype (parser);
4637 if (TREE_CODE (scope) != ENUMERAL_TYPE
4638 && !MAYBE_CLASS_TYPE_P (scope))
4640 cp_parser_simulate_error (parser);
4641 return error_mark_node;
4643 if (TYPE_NAME (scope))
4644 scope = TYPE_NAME (scope);
4648 /* Before we try to parse the class-name, we must save away the
4649 current PARSER->SCOPE since cp_parser_class_name will destroy
4651 saved_scope = parser->scope;
4652 saved_qualifying_scope = parser->qualifying_scope;
4653 saved_object_scope = parser->object_scope;
4654 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4655 there is no need to look for a namespace-name. */
4656 only_class_p = template_keyword_p
4657 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4659 cp_parser_parse_tentatively (parser);
4660 scope = cp_parser_class_name (parser,
4663 type_p ? class_type : none_type,
4665 /*class_head_p=*/false,
4667 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4668 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4670 && cxx_dialect != cxx98
4671 && !successful_parse_p)
4673 /* Restore the saved scope. */
4674 parser->scope = saved_scope;
4675 parser->qualifying_scope = saved_qualifying_scope;
4676 parser->object_scope = saved_object_scope;
4678 /* Parse tentatively. */
4679 cp_parser_parse_tentatively (parser);
4681 /* Parse a typedef-name or enum-name. */
4682 scope = cp_parser_nonclass_name (parser);
4684 /* "If the name found does not designate a namespace or a class,
4685 enumeration, or dependent type, the program is ill-formed."
4687 We cover classes and dependent types above and namespaces below,
4688 so this code is only looking for enums. */
4689 if (!scope || TREE_CODE (scope) != TYPE_DECL
4690 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4691 cp_parser_simulate_error (parser);
4693 successful_parse_p = cp_parser_parse_definitely (parser);
4695 /* If that didn't work, try for a namespace-name. */
4696 if (!only_class_p && !successful_parse_p)
4698 /* Restore the saved scope. */
4699 parser->scope = saved_scope;
4700 parser->qualifying_scope = saved_qualifying_scope;
4701 parser->object_scope = saved_object_scope;
4702 /* If we are not looking at an identifier followed by the scope
4703 resolution operator, then this is not part of a
4704 nested-name-specifier. (Note that this function is only used
4705 to parse the components of a nested-name-specifier.) */
4706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4707 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4708 return error_mark_node;
4709 scope = cp_parser_namespace_name (parser);
4715 /* Parse a postfix-expression.
4719 postfix-expression [ expression ]
4720 postfix-expression ( expression-list [opt] )
4721 simple-type-specifier ( expression-list [opt] )
4722 typename :: [opt] nested-name-specifier identifier
4723 ( expression-list [opt] )
4724 typename :: [opt] nested-name-specifier template [opt] template-id
4725 ( expression-list [opt] )
4726 postfix-expression . template [opt] id-expression
4727 postfix-expression -> template [opt] id-expression
4728 postfix-expression . pseudo-destructor-name
4729 postfix-expression -> pseudo-destructor-name
4730 postfix-expression ++
4731 postfix-expression --
4732 dynamic_cast < type-id > ( expression )
4733 static_cast < type-id > ( expression )
4734 reinterpret_cast < type-id > ( expression )
4735 const_cast < type-id > ( expression )
4736 typeid ( expression )
4742 ( type-id ) { initializer-list , [opt] }
4744 This extension is a GNU version of the C99 compound-literal
4745 construct. (The C99 grammar uses `type-name' instead of `type-id',
4746 but they are essentially the same concept.)
4748 If ADDRESS_P is true, the postfix expression is the operand of the
4749 `&' operator. CAST_P is true if this expression is the target of a
4752 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4753 class member access expressions [expr.ref].
4755 Returns a representation of the expression. */
4758 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4759 bool member_access_only_p,
4760 cp_id_kind * pidk_return)
4764 cp_id_kind idk = CP_ID_KIND_NONE;
4765 tree postfix_expression = NULL_TREE;
4766 bool is_member_access = false;
4768 /* Peek at the next token. */
4769 token = cp_lexer_peek_token (parser->lexer);
4770 /* Some of the productions are determined by keywords. */
4771 keyword = token->keyword;
4781 const char *saved_message;
4783 /* All of these can be handled in the same way from the point
4784 of view of parsing. Begin by consuming the token
4785 identifying the cast. */
4786 cp_lexer_consume_token (parser->lexer);
4788 /* New types cannot be defined in the cast. */
4789 saved_message = parser->type_definition_forbidden_message;
4790 parser->type_definition_forbidden_message
4791 = G_("types may not be defined in casts");
4793 /* Look for the opening `<'. */
4794 cp_parser_require (parser, CPP_LESS, RT_LESS);
4795 /* Parse the type to which we are casting. */
4796 type = cp_parser_type_id (parser);
4797 /* Look for the closing `>'. */
4798 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4799 /* Restore the old message. */
4800 parser->type_definition_forbidden_message = saved_message;
4802 /* And the expression which is being cast. */
4803 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4804 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4805 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4807 /* Only type conversions to integral or enumeration types
4808 can be used in constant-expressions. */
4809 if (!cast_valid_in_integral_constant_expression_p (type)
4810 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4811 return error_mark_node;
4817 = build_dynamic_cast (type, expression, tf_warning_or_error);
4821 = build_static_cast (type, expression, tf_warning_or_error);
4825 = build_reinterpret_cast (type, expression,
4826 tf_warning_or_error);
4830 = build_const_cast (type, expression, tf_warning_or_error);
4841 const char *saved_message;
4842 bool saved_in_type_id_in_expr_p;
4844 /* Consume the `typeid' token. */
4845 cp_lexer_consume_token (parser->lexer);
4846 /* Look for the `(' token. */
4847 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4848 /* Types cannot be defined in a `typeid' expression. */
4849 saved_message = parser->type_definition_forbidden_message;
4850 parser->type_definition_forbidden_message
4851 = G_("types may not be defined in a %<typeid%> expression");
4852 /* We can't be sure yet whether we're looking at a type-id or an
4854 cp_parser_parse_tentatively (parser);
4855 /* Try a type-id first. */
4856 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4857 parser->in_type_id_in_expr_p = true;
4858 type = cp_parser_type_id (parser);
4859 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4860 /* Look for the `)' token. Otherwise, we can't be sure that
4861 we're not looking at an expression: consider `typeid (int
4862 (3))', for example. */
4863 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4864 /* If all went well, simply lookup the type-id. */
4865 if (cp_parser_parse_definitely (parser))
4866 postfix_expression = get_typeid (type);
4867 /* Otherwise, fall back to the expression variant. */
4872 /* Look for an expression. */
4873 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4874 /* Compute its typeid. */
4875 postfix_expression = build_typeid (expression);
4876 /* Look for the `)' token. */
4877 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4879 /* Restore the saved message. */
4880 parser->type_definition_forbidden_message = saved_message;
4881 /* `typeid' may not appear in an integral constant expression. */
4882 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4883 return error_mark_node;
4890 /* The syntax permitted here is the same permitted for an
4891 elaborated-type-specifier. */
4892 type = cp_parser_elaborated_type_specifier (parser,
4893 /*is_friend=*/false,
4894 /*is_declaration=*/false);
4895 postfix_expression = cp_parser_functional_cast (parser, type);
4903 /* If the next thing is a simple-type-specifier, we may be
4904 looking at a functional cast. We could also be looking at
4905 an id-expression. So, we try the functional cast, and if
4906 that doesn't work we fall back to the primary-expression. */
4907 cp_parser_parse_tentatively (parser);
4908 /* Look for the simple-type-specifier. */
4909 type = cp_parser_simple_type_specifier (parser,
4910 /*decl_specs=*/NULL,
4911 CP_PARSER_FLAGS_NONE);
4912 /* Parse the cast itself. */
4913 if (!cp_parser_error_occurred (parser))
4915 = cp_parser_functional_cast (parser, type);
4916 /* If that worked, we're done. */
4917 if (cp_parser_parse_definitely (parser))
4920 /* If the functional-cast didn't work out, try a
4921 compound-literal. */
4922 if (cp_parser_allow_gnu_extensions_p (parser)
4923 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4925 VEC(constructor_elt,gc) *initializer_list = NULL;
4926 bool saved_in_type_id_in_expr_p;
4928 cp_parser_parse_tentatively (parser);
4929 /* Consume the `('. */
4930 cp_lexer_consume_token (parser->lexer);
4931 /* Parse the type. */
4932 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4933 parser->in_type_id_in_expr_p = true;
4934 type = cp_parser_type_id (parser);
4935 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4936 /* Look for the `)'. */
4937 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4938 /* Look for the `{'. */
4939 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4940 /* If things aren't going well, there's no need to
4942 if (!cp_parser_error_occurred (parser))
4944 bool non_constant_p;
4945 /* Parse the initializer-list. */
4947 = cp_parser_initializer_list (parser, &non_constant_p);
4948 /* Allow a trailing `,'. */
4949 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4950 cp_lexer_consume_token (parser->lexer);
4951 /* Look for the final `}'. */
4952 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4954 /* If that worked, we're definitely looking at a
4955 compound-literal expression. */
4956 if (cp_parser_parse_definitely (parser))
4958 /* Warn the user that a compound literal is not
4959 allowed in standard C++. */
4960 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4961 /* For simplicity, we disallow compound literals in
4962 constant-expressions. We could
4963 allow compound literals of integer type, whose
4964 initializer was a constant, in constant
4965 expressions. Permitting that usage, as a further
4966 extension, would not change the meaning of any
4967 currently accepted programs. (Of course, as
4968 compound literals are not part of ISO C++, the
4969 standard has nothing to say.) */
4970 if (cp_parser_non_integral_constant_expression (parser,
4973 postfix_expression = error_mark_node;
4976 /* Form the representation of the compound-literal. */
4978 = (finish_compound_literal
4979 (type, build_constructor (init_list_type_node,
4981 tf_warning_or_error));
4986 /* It must be a primary-expression. */
4988 = cp_parser_primary_expression (parser, address_p, cast_p,
4989 /*template_arg_p=*/false,
4995 /* Keep looping until the postfix-expression is complete. */
4998 if (idk == CP_ID_KIND_UNQUALIFIED
4999 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5000 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5001 /* It is not a Koenig lookup function call. */
5003 = unqualified_name_lookup_error (postfix_expression);
5005 /* Peek at the next token. */
5006 token = cp_lexer_peek_token (parser->lexer);
5008 switch (token->type)
5010 case CPP_OPEN_SQUARE:
5012 = cp_parser_postfix_open_square_expression (parser,
5015 idk = CP_ID_KIND_NONE;
5016 is_member_access = false;
5019 case CPP_OPEN_PAREN:
5020 /* postfix-expression ( expression-list [opt] ) */
5023 bool is_builtin_constant_p;
5024 bool saved_integral_constant_expression_p = false;
5025 bool saved_non_integral_constant_expression_p = false;
5028 is_member_access = false;
5030 is_builtin_constant_p
5031 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5032 if (is_builtin_constant_p)
5034 /* The whole point of __builtin_constant_p is to allow
5035 non-constant expressions to appear as arguments. */
5036 saved_integral_constant_expression_p
5037 = parser->integral_constant_expression_p;
5038 saved_non_integral_constant_expression_p
5039 = parser->non_integral_constant_expression_p;
5040 parser->integral_constant_expression_p = false;
5042 args = (cp_parser_parenthesized_expression_list
5044 /*cast_p=*/false, /*allow_expansion_p=*/true,
5045 /*non_constant_p=*/NULL));
5046 if (is_builtin_constant_p)
5048 parser->integral_constant_expression_p
5049 = saved_integral_constant_expression_p;
5050 parser->non_integral_constant_expression_p
5051 = saved_non_integral_constant_expression_p;
5056 postfix_expression = error_mark_node;
5060 /* Function calls are not permitted in
5061 constant-expressions. */
5062 if (! builtin_valid_in_constant_expr_p (postfix_expression)
5063 && cp_parser_non_integral_constant_expression (parser,
5066 postfix_expression = error_mark_node;
5067 release_tree_vector (args);
5072 if (idk == CP_ID_KIND_UNQUALIFIED
5073 || idk == CP_ID_KIND_TEMPLATE_ID)
5075 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5077 if (!VEC_empty (tree, args))
5080 if (!any_type_dependent_arguments_p (args))
5082 = perform_koenig_lookup (postfix_expression, args,
5083 /*include_std=*/false,
5084 tf_warning_or_error);
5088 = unqualified_fn_lookup_error (postfix_expression);
5090 /* We do not perform argument-dependent lookup if
5091 normal lookup finds a non-function, in accordance
5092 with the expected resolution of DR 218. */
5093 else if (!VEC_empty (tree, args)
5094 && is_overloaded_fn (postfix_expression))
5096 tree fn = get_first_fn (postfix_expression);
5097 fn = STRIP_TEMPLATE (fn);
5099 /* Do not do argument dependent lookup if regular
5100 lookup finds a member function or a block-scope
5101 function declaration. [basic.lookup.argdep]/3 */
5102 if (!DECL_FUNCTION_MEMBER_P (fn)
5103 && !DECL_LOCAL_FUNCTION_P (fn))
5106 if (!any_type_dependent_arguments_p (args))
5108 = perform_koenig_lookup (postfix_expression, args,
5109 /*include_std=*/false,
5110 tf_warning_or_error);
5115 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5117 tree instance = TREE_OPERAND (postfix_expression, 0);
5118 tree fn = TREE_OPERAND (postfix_expression, 1);
5120 if (processing_template_decl
5121 && (type_dependent_expression_p (instance)
5122 || (!BASELINK_P (fn)
5123 && TREE_CODE (fn) != FIELD_DECL)
5124 || type_dependent_expression_p (fn)
5125 || any_type_dependent_arguments_p (args)))
5128 = build_nt_call_vec (postfix_expression, args);
5129 release_tree_vector (args);
5133 if (BASELINK_P (fn))
5136 = (build_new_method_call
5137 (instance, fn, &args, NULL_TREE,
5138 (idk == CP_ID_KIND_QUALIFIED
5139 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5142 tf_warning_or_error));
5146 = finish_call_expr (postfix_expression, &args,
5147 /*disallow_virtual=*/false,
5149 tf_warning_or_error);
5151 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5152 || TREE_CODE (postfix_expression) == MEMBER_REF
5153 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5154 postfix_expression = (build_offset_ref_call_from_tree
5155 (postfix_expression, &args));
5156 else if (idk == CP_ID_KIND_QUALIFIED)
5157 /* A call to a static class member, or a namespace-scope
5160 = finish_call_expr (postfix_expression, &args,
5161 /*disallow_virtual=*/true,
5163 tf_warning_or_error);
5165 /* All other function calls. */
5167 = finish_call_expr (postfix_expression, &args,
5168 /*disallow_virtual=*/false,
5170 tf_warning_or_error);
5172 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5173 idk = CP_ID_KIND_NONE;
5175 release_tree_vector (args);
5181 /* postfix-expression . template [opt] id-expression
5182 postfix-expression . pseudo-destructor-name
5183 postfix-expression -> template [opt] id-expression
5184 postfix-expression -> pseudo-destructor-name */
5186 /* Consume the `.' or `->' operator. */
5187 cp_lexer_consume_token (parser->lexer);
5190 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5195 is_member_access = true;
5199 /* postfix-expression ++ */
5200 /* Consume the `++' token. */
5201 cp_lexer_consume_token (parser->lexer);
5202 /* Generate a representation for the complete expression. */
5204 = finish_increment_expr (postfix_expression,
5205 POSTINCREMENT_EXPR);
5206 /* Increments may not appear in constant-expressions. */
5207 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5208 postfix_expression = error_mark_node;
5209 idk = CP_ID_KIND_NONE;
5210 is_member_access = false;
5213 case CPP_MINUS_MINUS:
5214 /* postfix-expression -- */
5215 /* Consume the `--' token. */
5216 cp_lexer_consume_token (parser->lexer);
5217 /* Generate a representation for the complete expression. */
5219 = finish_increment_expr (postfix_expression,
5220 POSTDECREMENT_EXPR);
5221 /* Decrements may not appear in constant-expressions. */
5222 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5223 postfix_expression = error_mark_node;
5224 idk = CP_ID_KIND_NONE;
5225 is_member_access = false;
5229 if (pidk_return != NULL)
5230 * pidk_return = idk;
5231 if (member_access_only_p)
5232 return is_member_access? postfix_expression : error_mark_node;
5234 return postfix_expression;
5238 /* We should never get here. */
5240 return error_mark_node;
5243 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5244 by cp_parser_builtin_offsetof. We're looking for
5246 postfix-expression [ expression ]
5248 FOR_OFFSETOF is set if we're being called in that context, which
5249 changes how we deal with integer constant expressions. */
5252 cp_parser_postfix_open_square_expression (cp_parser *parser,
5253 tree postfix_expression,
5258 /* Consume the `[' token. */
5259 cp_lexer_consume_token (parser->lexer);
5261 /* Parse the index expression. */
5262 /* ??? For offsetof, there is a question of what to allow here. If
5263 offsetof is not being used in an integral constant expression context,
5264 then we *could* get the right answer by computing the value at runtime.
5265 If we are in an integral constant expression context, then we might
5266 could accept any constant expression; hard to say without analysis.
5267 Rather than open the barn door too wide right away, allow only integer
5268 constant expressions here. */
5270 index = cp_parser_constant_expression (parser, false, NULL);
5272 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5274 /* Look for the closing `]'. */
5275 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5277 /* Build the ARRAY_REF. */
5278 postfix_expression = grok_array_decl (postfix_expression, index);
5280 /* When not doing offsetof, array references are not permitted in
5281 constant-expressions. */
5283 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5284 postfix_expression = error_mark_node;
5286 return postfix_expression;
5289 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5290 by cp_parser_builtin_offsetof. We're looking for
5292 postfix-expression . template [opt] id-expression
5293 postfix-expression . pseudo-destructor-name
5294 postfix-expression -> template [opt] id-expression
5295 postfix-expression -> pseudo-destructor-name
5297 FOR_OFFSETOF is set if we're being called in that context. That sorta
5298 limits what of the above we'll actually accept, but nevermind.
5299 TOKEN_TYPE is the "." or "->" token, which will already have been
5300 removed from the stream. */
5303 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5304 enum cpp_ttype token_type,
5305 tree postfix_expression,
5306 bool for_offsetof, cp_id_kind *idk,
5307 location_t location)
5311 bool pseudo_destructor_p;
5312 tree scope = NULL_TREE;
5314 /* If this is a `->' operator, dereference the pointer. */
5315 if (token_type == CPP_DEREF)
5316 postfix_expression = build_x_arrow (postfix_expression);
5317 /* Check to see whether or not the expression is type-dependent. */
5318 dependent_p = type_dependent_expression_p (postfix_expression);
5319 /* The identifier following the `->' or `.' is not qualified. */
5320 parser->scope = NULL_TREE;
5321 parser->qualifying_scope = NULL_TREE;
5322 parser->object_scope = NULL_TREE;
5323 *idk = CP_ID_KIND_NONE;
5325 /* Enter the scope corresponding to the type of the object
5326 given by the POSTFIX_EXPRESSION. */
5327 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5329 scope = TREE_TYPE (postfix_expression);
5330 /* According to the standard, no expression should ever have
5331 reference type. Unfortunately, we do not currently match
5332 the standard in this respect in that our internal representation
5333 of an expression may have reference type even when the standard
5334 says it does not. Therefore, we have to manually obtain the
5335 underlying type here. */
5336 scope = non_reference (scope);
5337 /* The type of the POSTFIX_EXPRESSION must be complete. */
5338 if (scope == unknown_type_node)
5340 error_at (location, "%qE does not have class type",
5341 postfix_expression);
5344 /* Unlike the object expression in other contexts, *this is not
5345 required to be of complete type for purposes of class member
5346 access (5.2.5) outside the member function body. */
5347 else if (scope != current_class_ref
5348 && !(processing_template_decl && scope == current_class_type))
5349 scope = complete_type_or_else (scope, NULL_TREE);
5350 /* Let the name lookup machinery know that we are processing a
5351 class member access expression. */
5352 parser->context->object_type = scope;
5353 /* If something went wrong, we want to be able to discern that case,
5354 as opposed to the case where there was no SCOPE due to the type
5355 of expression being dependent. */
5357 scope = error_mark_node;
5358 /* If the SCOPE was erroneous, make the various semantic analysis
5359 functions exit quickly -- and without issuing additional error
5361 if (scope == error_mark_node)
5362 postfix_expression = error_mark_node;
5365 /* Assume this expression is not a pseudo-destructor access. */
5366 pseudo_destructor_p = false;
5368 /* If the SCOPE is a scalar type, then, if this is a valid program,
5369 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5370 is type dependent, it can be pseudo-destructor-name or something else.
5371 Try to parse it as pseudo-destructor-name first. */
5372 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5377 cp_parser_parse_tentatively (parser);
5378 /* Parse the pseudo-destructor-name. */
5380 cp_parser_pseudo_destructor_name (parser, &s, &type);
5382 && (cp_parser_error_occurred (parser)
5383 || TREE_CODE (type) != TYPE_DECL
5384 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5385 cp_parser_abort_tentative_parse (parser);
5386 else if (cp_parser_parse_definitely (parser))
5388 pseudo_destructor_p = true;
5390 = finish_pseudo_destructor_expr (postfix_expression,
5391 s, TREE_TYPE (type));
5395 if (!pseudo_destructor_p)
5397 /* If the SCOPE is not a scalar type, we are looking at an
5398 ordinary class member access expression, rather than a
5399 pseudo-destructor-name. */
5401 cp_token *token = cp_lexer_peek_token (parser->lexer);
5402 /* Parse the id-expression. */
5403 name = (cp_parser_id_expression
5405 cp_parser_optional_template_keyword (parser),
5406 /*check_dependency_p=*/true,
5408 /*declarator_p=*/false,
5409 /*optional_p=*/false));
5410 /* In general, build a SCOPE_REF if the member name is qualified.
5411 However, if the name was not dependent and has already been
5412 resolved; there is no need to build the SCOPE_REF. For example;
5414 struct X { void f(); };
5415 template <typename T> void f(T* t) { t->X::f(); }
5417 Even though "t" is dependent, "X::f" is not and has been resolved
5418 to a BASELINK; there is no need to include scope information. */
5420 /* But we do need to remember that there was an explicit scope for
5421 virtual function calls. */
5423 *idk = CP_ID_KIND_QUALIFIED;
5425 /* If the name is a template-id that names a type, we will get a
5426 TYPE_DECL here. That is invalid code. */
5427 if (TREE_CODE (name) == TYPE_DECL)
5429 error_at (token->location, "invalid use of %qD", name);
5430 postfix_expression = error_mark_node;
5434 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5436 name = build_qualified_name (/*type=*/NULL_TREE,
5440 parser->scope = NULL_TREE;
5441 parser->qualifying_scope = NULL_TREE;
5442 parser->object_scope = NULL_TREE;
5444 if (scope && name && BASELINK_P (name))
5445 adjust_result_of_qualified_name_lookup
5446 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5448 = finish_class_member_access_expr (postfix_expression, name,
5450 tf_warning_or_error);
5454 /* We no longer need to look up names in the scope of the object on
5455 the left-hand side of the `.' or `->' operator. */
5456 parser->context->object_type = NULL_TREE;
5458 /* Outside of offsetof, these operators may not appear in
5459 constant-expressions. */
5461 && (cp_parser_non_integral_constant_expression
5462 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5463 postfix_expression = error_mark_node;
5465 return postfix_expression;
5468 /* Parse a parenthesized expression-list.
5471 assignment-expression
5472 expression-list, assignment-expression
5477 identifier, expression-list
5479 CAST_P is true if this expression is the target of a cast.
5481 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5484 Returns a vector of trees. Each element is a representation of an
5485 assignment-expression. NULL is returned if the ( and or ) are
5486 missing. An empty, but allocated, vector is returned on no
5487 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5488 if we are parsing an attribute list for an attribute that wants a
5489 plain identifier argument, normal_attr for an attribute that wants
5490 an expression, or non_attr if we aren't parsing an attribute list. If
5491 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5492 not all of the expressions in the list were constant. */
5494 static VEC(tree,gc) *
5495 cp_parser_parenthesized_expression_list (cp_parser* parser,
5496 int is_attribute_list,
5498 bool allow_expansion_p,
5499 bool *non_constant_p)
5501 VEC(tree,gc) *expression_list;
5502 bool fold_expr_p = is_attribute_list != non_attr;
5503 tree identifier = NULL_TREE;
5504 bool saved_greater_than_is_operator_p;
5506 /* Assume all the expressions will be constant. */
5508 *non_constant_p = false;
5510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5513 expression_list = make_tree_vector ();
5515 /* Within a parenthesized expression, a `>' token is always
5516 the greater-than operator. */
5517 saved_greater_than_is_operator_p
5518 = parser->greater_than_is_operator_p;
5519 parser->greater_than_is_operator_p = true;
5521 /* Consume expressions until there are no more. */
5522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5527 /* At the beginning of attribute lists, check to see if the
5528 next token is an identifier. */
5529 if (is_attribute_list == id_attr
5530 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5534 /* Consume the identifier. */
5535 token = cp_lexer_consume_token (parser->lexer);
5536 /* Save the identifier. */
5537 identifier = token->u.value;
5541 bool expr_non_constant_p;
5543 /* Parse the next assignment-expression. */
5544 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5546 /* A braced-init-list. */
5547 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5548 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5549 if (non_constant_p && expr_non_constant_p)
5550 *non_constant_p = true;
5552 else if (non_constant_p)
5554 expr = (cp_parser_constant_expression
5555 (parser, /*allow_non_constant_p=*/true,
5556 &expr_non_constant_p));
5557 if (expr_non_constant_p)
5558 *non_constant_p = true;
5561 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5564 expr = fold_non_dependent_expr (expr);
5566 /* If we have an ellipsis, then this is an expression
5568 if (allow_expansion_p
5569 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5571 /* Consume the `...'. */
5572 cp_lexer_consume_token (parser->lexer);
5574 /* Build the argument pack. */
5575 expr = make_pack_expansion (expr);
5578 /* Add it to the list. We add error_mark_node
5579 expressions to the list, so that we can still tell if
5580 the correct form for a parenthesized expression-list
5581 is found. That gives better errors. */
5582 VEC_safe_push (tree, gc, expression_list, expr);
5584 if (expr == error_mark_node)
5588 /* After the first item, attribute lists look the same as
5589 expression lists. */
5590 is_attribute_list = non_attr;
5593 /* If the next token isn't a `,', then we are done. */
5594 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5597 /* Otherwise, consume the `,' and keep going. */
5598 cp_lexer_consume_token (parser->lexer);
5601 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5606 /* We try and resync to an unnested comma, as that will give the
5607 user better diagnostics. */
5608 ending = cp_parser_skip_to_closing_parenthesis (parser,
5609 /*recovering=*/true,
5611 /*consume_paren=*/true);
5616 parser->greater_than_is_operator_p
5617 = saved_greater_than_is_operator_p;
5622 parser->greater_than_is_operator_p
5623 = saved_greater_than_is_operator_p;
5626 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5628 return expression_list;
5631 /* Parse a pseudo-destructor-name.
5633 pseudo-destructor-name:
5634 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5635 :: [opt] nested-name-specifier template template-id :: ~ type-name
5636 :: [opt] nested-name-specifier [opt] ~ type-name
5638 If either of the first two productions is used, sets *SCOPE to the
5639 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5640 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5641 or ERROR_MARK_NODE if the parse fails. */
5644 cp_parser_pseudo_destructor_name (cp_parser* parser,
5648 bool nested_name_specifier_p;
5650 /* Assume that things will not work out. */
5651 *type = error_mark_node;
5653 /* Look for the optional `::' operator. */
5654 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5655 /* Look for the optional nested-name-specifier. */
5656 nested_name_specifier_p
5657 = (cp_parser_nested_name_specifier_opt (parser,
5658 /*typename_keyword_p=*/false,
5659 /*check_dependency_p=*/true,
5661 /*is_declaration=*/false)
5663 /* Now, if we saw a nested-name-specifier, we might be doing the
5664 second production. */
5665 if (nested_name_specifier_p
5666 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5668 /* Consume the `template' keyword. */
5669 cp_lexer_consume_token (parser->lexer);
5670 /* Parse the template-id. */
5671 cp_parser_template_id (parser,
5672 /*template_keyword_p=*/true,
5673 /*check_dependency_p=*/false,
5674 /*is_declaration=*/true);
5675 /* Look for the `::' token. */
5676 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5678 /* If the next token is not a `~', then there might be some
5679 additional qualification. */
5680 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5682 /* At this point, we're looking for "type-name :: ~". The type-name
5683 must not be a class-name, since this is a pseudo-destructor. So,
5684 it must be either an enum-name, or a typedef-name -- both of which
5685 are just identifiers. So, we peek ahead to check that the "::"
5686 and "~" tokens are present; if they are not, then we can avoid
5687 calling type_name. */
5688 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5689 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5690 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5692 cp_parser_error (parser, "non-scalar type");
5696 /* Look for the type-name. */
5697 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5698 if (*scope == error_mark_node)
5701 /* Look for the `::' token. */
5702 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5707 /* Look for the `~'. */
5708 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5710 /* Once we see the ~, this has to be a pseudo-destructor. */
5711 if (!processing_template_decl && !cp_parser_error_occurred (parser))
5712 cp_parser_commit_to_tentative_parse (parser);
5714 /* Look for the type-name again. We are not responsible for
5715 checking that it matches the first type-name. */
5716 *type = cp_parser_nonclass_name (parser);
5719 /* Parse a unary-expression.
5725 unary-operator cast-expression
5726 sizeof unary-expression
5728 alignof ( type-id ) [C++0x]
5735 __extension__ cast-expression
5736 __alignof__ unary-expression
5737 __alignof__ ( type-id )
5738 alignof unary-expression [C++0x]
5739 __real__ cast-expression
5740 __imag__ cast-expression
5743 ADDRESS_P is true iff the unary-expression is appearing as the
5744 operand of the `&' operator. CAST_P is true if this expression is
5745 the target of a cast.
5747 Returns a representation of the expression. */
5750 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5754 enum tree_code unary_operator;
5756 /* Peek at the next token. */
5757 token = cp_lexer_peek_token (parser->lexer);
5758 /* Some keywords give away the kind of expression. */
5759 if (token->type == CPP_KEYWORD)
5761 enum rid keyword = token->keyword;
5771 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5772 /* Consume the token. */
5773 cp_lexer_consume_token (parser->lexer);
5774 /* Parse the operand. */
5775 operand = cp_parser_sizeof_operand (parser, keyword);
5777 if (TYPE_P (operand))
5778 return cxx_sizeof_or_alignof_type (operand, op, true);
5781 /* ISO C++ defines alignof only with types, not with
5782 expressions. So pedwarn if alignof is used with a non-
5783 type expression. However, __alignof__ is ok. */
5784 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5785 pedwarn (token->location, OPT_pedantic,
5786 "ISO C++ does not allow %<alignof%> "
5789 return cxx_sizeof_or_alignof_expr (operand, op, true);
5794 return cp_parser_new_expression (parser);
5797 return cp_parser_delete_expression (parser);
5801 /* The saved value of the PEDANTIC flag. */
5805 /* Save away the PEDANTIC flag. */
5806 cp_parser_extension_opt (parser, &saved_pedantic);
5807 /* Parse the cast-expression. */
5808 expr = cp_parser_simple_cast_expression (parser);
5809 /* Restore the PEDANTIC flag. */
5810 pedantic = saved_pedantic;
5820 /* Consume the `__real__' or `__imag__' token. */
5821 cp_lexer_consume_token (parser->lexer);
5822 /* Parse the cast-expression. */
5823 expression = cp_parser_simple_cast_expression (parser);
5824 /* Create the complete representation. */
5825 return build_x_unary_op ((keyword == RID_REALPART
5826 ? REALPART_EXPR : IMAGPART_EXPR),
5828 tf_warning_or_error);
5835 const char *saved_message;
5836 bool saved_integral_constant_expression_p;
5837 bool saved_non_integral_constant_expression_p;
5838 bool saved_greater_than_is_operator_p;
5840 cp_lexer_consume_token (parser->lexer);
5841 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5843 saved_message = parser->type_definition_forbidden_message;
5844 parser->type_definition_forbidden_message
5845 = G_("types may not be defined in %<noexcept%> expressions");
5847 saved_integral_constant_expression_p
5848 = parser->integral_constant_expression_p;
5849 saved_non_integral_constant_expression_p
5850 = parser->non_integral_constant_expression_p;
5851 parser->integral_constant_expression_p = false;
5853 saved_greater_than_is_operator_p
5854 = parser->greater_than_is_operator_p;
5855 parser->greater_than_is_operator_p = true;
5857 ++cp_unevaluated_operand;
5858 ++c_inhibit_evaluation_warnings;
5859 expr = cp_parser_expression (parser, false, NULL);
5860 --c_inhibit_evaluation_warnings;
5861 --cp_unevaluated_operand;
5863 parser->greater_than_is_operator_p
5864 = saved_greater_than_is_operator_p;
5866 parser->integral_constant_expression_p
5867 = saved_integral_constant_expression_p;
5868 parser->non_integral_constant_expression_p
5869 = saved_non_integral_constant_expression_p;
5871 parser->type_definition_forbidden_message = saved_message;
5873 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5874 return finish_noexcept_expr (expr, tf_warning_or_error);
5882 /* Look for the `:: new' and `:: delete', which also signal the
5883 beginning of a new-expression, or delete-expression,
5884 respectively. If the next token is `::', then it might be one of
5886 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5890 /* See if the token after the `::' is one of the keywords in
5891 which we're interested. */
5892 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5893 /* If it's `new', we have a new-expression. */
5894 if (keyword == RID_NEW)
5895 return cp_parser_new_expression (parser);
5896 /* Similarly, for `delete'. */
5897 else if (keyword == RID_DELETE)
5898 return cp_parser_delete_expression (parser);
5901 /* Look for a unary operator. */
5902 unary_operator = cp_parser_unary_operator (token);
5903 /* The `++' and `--' operators can be handled similarly, even though
5904 they are not technically unary-operators in the grammar. */
5905 if (unary_operator == ERROR_MARK)
5907 if (token->type == CPP_PLUS_PLUS)
5908 unary_operator = PREINCREMENT_EXPR;
5909 else if (token->type == CPP_MINUS_MINUS)
5910 unary_operator = PREDECREMENT_EXPR;
5911 /* Handle the GNU address-of-label extension. */
5912 else if (cp_parser_allow_gnu_extensions_p (parser)
5913 && token->type == CPP_AND_AND)
5917 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5919 /* Consume the '&&' token. */
5920 cp_lexer_consume_token (parser->lexer);
5921 /* Look for the identifier. */
5922 identifier = cp_parser_identifier (parser);
5923 /* Create an expression representing the address. */
5924 expression = finish_label_address_expr (identifier, loc);
5925 if (cp_parser_non_integral_constant_expression (parser,
5927 expression = error_mark_node;
5931 if (unary_operator != ERROR_MARK)
5933 tree cast_expression;
5934 tree expression = error_mark_node;
5935 non_integral_constant non_constant_p = NIC_NONE;
5937 /* Consume the operator token. */
5938 token = cp_lexer_consume_token (parser->lexer);
5939 /* Parse the cast-expression. */
5941 = cp_parser_cast_expression (parser,
5942 unary_operator == ADDR_EXPR,
5943 /*cast_p=*/false, pidk);
5944 /* Now, build an appropriate representation. */
5945 switch (unary_operator)
5948 non_constant_p = NIC_STAR;
5949 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5950 tf_warning_or_error);
5954 non_constant_p = NIC_ADDR;
5957 expression = build_x_unary_op (unary_operator, cast_expression,
5958 tf_warning_or_error);
5961 case PREINCREMENT_EXPR:
5962 case PREDECREMENT_EXPR:
5963 non_constant_p = unary_operator == PREINCREMENT_EXPR
5964 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5966 case UNARY_PLUS_EXPR:
5968 case TRUTH_NOT_EXPR:
5969 expression = finish_unary_op_expr (unary_operator, cast_expression);
5976 if (non_constant_p != NIC_NONE
5977 && cp_parser_non_integral_constant_expression (parser,
5979 expression = error_mark_node;
5984 return cp_parser_postfix_expression (parser, address_p, cast_p,
5985 /*member_access_only_p=*/false,
5989 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5990 unary-operator, the corresponding tree code is returned. */
5992 static enum tree_code
5993 cp_parser_unary_operator (cp_token* token)
5995 switch (token->type)
5998 return INDIRECT_REF;
6004 return UNARY_PLUS_EXPR;
6010 return TRUTH_NOT_EXPR;
6013 return BIT_NOT_EXPR;
6020 /* Parse a new-expression.
6023 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6024 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6026 Returns a representation of the expression. */
6029 cp_parser_new_expression (cp_parser* parser)
6031 bool global_scope_p;
6032 VEC(tree,gc) *placement;
6034 VEC(tree,gc) *initializer;
6038 /* Look for the optional `::' operator. */
6040 = (cp_parser_global_scope_opt (parser,
6041 /*current_scope_valid_p=*/false)
6043 /* Look for the `new' operator. */
6044 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6045 /* There's no easy way to tell a new-placement from the
6046 `( type-id )' construct. */
6047 cp_parser_parse_tentatively (parser);
6048 /* Look for a new-placement. */
6049 placement = cp_parser_new_placement (parser);
6050 /* If that didn't work out, there's no new-placement. */
6051 if (!cp_parser_parse_definitely (parser))
6053 if (placement != NULL)
6054 release_tree_vector (placement);
6058 /* If the next token is a `(', then we have a parenthesized
6060 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6063 /* Consume the `('. */
6064 cp_lexer_consume_token (parser->lexer);
6065 /* Parse the type-id. */
6066 type = cp_parser_type_id (parser);
6067 /* Look for the closing `)'. */
6068 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6069 token = cp_lexer_peek_token (parser->lexer);
6070 /* There should not be a direct-new-declarator in this production,
6071 but GCC used to allowed this, so we check and emit a sensible error
6072 message for this case. */
6073 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6075 error_at (token->location,
6076 "array bound forbidden after parenthesized type-id");
6077 inform (token->location,
6078 "try removing the parentheses around the type-id");
6079 cp_parser_direct_new_declarator (parser);
6083 /* Otherwise, there must be a new-type-id. */
6085 type = cp_parser_new_type_id (parser, &nelts);
6087 /* If the next token is a `(' or '{', then we have a new-initializer. */
6088 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6089 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6090 initializer = cp_parser_new_initializer (parser);
6094 /* A new-expression may not appear in an integral constant
6096 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6097 ret = error_mark_node;
6100 /* Create a representation of the new-expression. */
6101 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6102 tf_warning_or_error);
6105 if (placement != NULL)
6106 release_tree_vector (placement);
6107 if (initializer != NULL)
6108 release_tree_vector (initializer);
6113 /* Parse a new-placement.
6118 Returns the same representation as for an expression-list. */
6120 static VEC(tree,gc) *
6121 cp_parser_new_placement (cp_parser* parser)
6123 VEC(tree,gc) *expression_list;
6125 /* Parse the expression-list. */
6126 expression_list = (cp_parser_parenthesized_expression_list
6127 (parser, non_attr, /*cast_p=*/false,
6128 /*allow_expansion_p=*/true,
6129 /*non_constant_p=*/NULL));
6131 return expression_list;
6134 /* Parse a new-type-id.
6137 type-specifier-seq new-declarator [opt]
6139 Returns the TYPE allocated. If the new-type-id indicates an array
6140 type, *NELTS is set to the number of elements in the last array
6141 bound; the TYPE will not include the last array bound. */
6144 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6146 cp_decl_specifier_seq type_specifier_seq;
6147 cp_declarator *new_declarator;
6148 cp_declarator *declarator;
6149 cp_declarator *outer_declarator;
6150 const char *saved_message;
6153 /* The type-specifier sequence must not contain type definitions.
6154 (It cannot contain declarations of new types either, but if they
6155 are not definitions we will catch that because they are not
6157 saved_message = parser->type_definition_forbidden_message;
6158 parser->type_definition_forbidden_message
6159 = G_("types may not be defined in a new-type-id");
6160 /* Parse the type-specifier-seq. */
6161 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6162 /*is_trailing_return=*/false,
6163 &type_specifier_seq);
6164 /* Restore the old message. */
6165 parser->type_definition_forbidden_message = saved_message;
6166 /* Parse the new-declarator. */
6167 new_declarator = cp_parser_new_declarator_opt (parser);
6169 /* Determine the number of elements in the last array dimension, if
6172 /* Skip down to the last array dimension. */
6173 declarator = new_declarator;
6174 outer_declarator = NULL;
6175 while (declarator && (declarator->kind == cdk_pointer
6176 || declarator->kind == cdk_ptrmem))
6178 outer_declarator = declarator;
6179 declarator = declarator->declarator;
6182 && declarator->kind == cdk_array
6183 && declarator->declarator
6184 && declarator->declarator->kind == cdk_array)
6186 outer_declarator = declarator;
6187 declarator = declarator->declarator;
6190 if (declarator && declarator->kind == cdk_array)
6192 *nelts = declarator->u.array.bounds;
6193 if (*nelts == error_mark_node)
6194 *nelts = integer_one_node;
6196 if (outer_declarator)
6197 outer_declarator->declarator = declarator->declarator;
6199 new_declarator = NULL;
6202 type = groktypename (&type_specifier_seq, new_declarator, false);
6206 /* Parse an (optional) new-declarator.
6209 ptr-operator new-declarator [opt]
6210 direct-new-declarator
6212 Returns the declarator. */
6214 static cp_declarator *
6215 cp_parser_new_declarator_opt (cp_parser* parser)
6217 enum tree_code code;
6219 cp_cv_quals cv_quals;
6221 /* We don't know if there's a ptr-operator next, or not. */
6222 cp_parser_parse_tentatively (parser);
6223 /* Look for a ptr-operator. */
6224 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6225 /* If that worked, look for more new-declarators. */
6226 if (cp_parser_parse_definitely (parser))
6228 cp_declarator *declarator;
6230 /* Parse another optional declarator. */
6231 declarator = cp_parser_new_declarator_opt (parser);
6233 return cp_parser_make_indirect_declarator
6234 (code, type, cv_quals, declarator);
6237 /* If the next token is a `[', there is a direct-new-declarator. */
6238 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6239 return cp_parser_direct_new_declarator (parser);
6244 /* Parse a direct-new-declarator.
6246 direct-new-declarator:
6248 direct-new-declarator [constant-expression]
6252 static cp_declarator *
6253 cp_parser_direct_new_declarator (cp_parser* parser)
6255 cp_declarator *declarator = NULL;
6261 /* Look for the opening `['. */
6262 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6263 /* The first expression is not required to be constant. */
6266 cp_token *token = cp_lexer_peek_token (parser->lexer);
6267 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6268 /* The standard requires that the expression have integral
6269 type. DR 74 adds enumeration types. We believe that the
6270 real intent is that these expressions be handled like the
6271 expression in a `switch' condition, which also allows
6272 classes with a single conversion to integral or
6273 enumeration type. */
6274 if (!processing_template_decl)
6277 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6282 error_at (token->location,
6283 "expression in new-declarator must have integral "
6284 "or enumeration type");
6285 expression = error_mark_node;
6289 /* But all the other expressions must be. */
6292 = cp_parser_constant_expression (parser,
6293 /*allow_non_constant=*/false,
6295 /* Look for the closing `]'. */
6296 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6298 /* Add this bound to the declarator. */
6299 declarator = make_array_declarator (declarator, expression);
6301 /* If the next token is not a `[', then there are no more
6303 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6310 /* Parse a new-initializer.
6313 ( expression-list [opt] )
6316 Returns a representation of the expression-list. */
6318 static VEC(tree,gc) *
6319 cp_parser_new_initializer (cp_parser* parser)
6321 VEC(tree,gc) *expression_list;
6323 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6326 bool expr_non_constant_p;
6327 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6328 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6329 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6330 expression_list = make_tree_vector_single (t);
6333 expression_list = (cp_parser_parenthesized_expression_list
6334 (parser, non_attr, /*cast_p=*/false,
6335 /*allow_expansion_p=*/true,
6336 /*non_constant_p=*/NULL));
6338 return expression_list;
6341 /* Parse a delete-expression.
6344 :: [opt] delete cast-expression
6345 :: [opt] delete [ ] cast-expression
6347 Returns a representation of the expression. */
6350 cp_parser_delete_expression (cp_parser* parser)
6352 bool global_scope_p;
6356 /* Look for the optional `::' operator. */
6358 = (cp_parser_global_scope_opt (parser,
6359 /*current_scope_valid_p=*/false)
6361 /* Look for the `delete' keyword. */
6362 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6363 /* See if the array syntax is in use. */
6364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6366 /* Consume the `[' token. */
6367 cp_lexer_consume_token (parser->lexer);
6368 /* Look for the `]' token. */
6369 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6370 /* Remember that this is the `[]' construct. */
6376 /* Parse the cast-expression. */
6377 expression = cp_parser_simple_cast_expression (parser);
6379 /* A delete-expression may not appear in an integral constant
6381 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6382 return error_mark_node;
6384 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6385 tf_warning_or_error);
6388 /* Returns true if TOKEN may start a cast-expression and false
6392 cp_parser_token_starts_cast_expression (cp_token *token)
6394 switch (token->type)
6400 case CPP_CLOSE_SQUARE:
6401 case CPP_CLOSE_PAREN:
6402 case CPP_CLOSE_BRACE:
6406 case CPP_DEREF_STAR:
6414 case CPP_GREATER_EQ:
6434 /* '[' may start a primary-expression in obj-c++. */
6435 case CPP_OPEN_SQUARE:
6436 return c_dialect_objc ();
6443 /* Parse a cast-expression.
6447 ( type-id ) cast-expression
6449 ADDRESS_P is true iff the unary-expression is appearing as the
6450 operand of the `&' operator. CAST_P is true if this expression is
6451 the target of a cast.
6453 Returns a representation of the expression. */
6456 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6459 /* If it's a `(', then we might be looking at a cast. */
6460 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6462 tree type = NULL_TREE;
6463 tree expr = NULL_TREE;
6464 bool compound_literal_p;
6465 const char *saved_message;
6467 /* There's no way to know yet whether or not this is a cast.
6468 For example, `(int (3))' is a unary-expression, while `(int)
6469 3' is a cast. So, we resort to parsing tentatively. */
6470 cp_parser_parse_tentatively (parser);
6471 /* Types may not be defined in a cast. */
6472 saved_message = parser->type_definition_forbidden_message;
6473 parser->type_definition_forbidden_message
6474 = G_("types may not be defined in casts");
6475 /* Consume the `('. */
6476 cp_lexer_consume_token (parser->lexer);
6477 /* A very tricky bit is that `(struct S) { 3 }' is a
6478 compound-literal (which we permit in C++ as an extension).
6479 But, that construct is not a cast-expression -- it is a
6480 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6481 is legal; if the compound-literal were a cast-expression,
6482 you'd need an extra set of parentheses.) But, if we parse
6483 the type-id, and it happens to be a class-specifier, then we
6484 will commit to the parse at that point, because we cannot
6485 undo the action that is done when creating a new class. So,
6486 then we cannot back up and do a postfix-expression.
6488 Therefore, we scan ahead to the closing `)', and check to see
6489 if the token after the `)' is a `{'. If so, we are not
6490 looking at a cast-expression.
6492 Save tokens so that we can put them back. */
6493 cp_lexer_save_tokens (parser->lexer);
6494 /* Skip tokens until the next token is a closing parenthesis.
6495 If we find the closing `)', and the next token is a `{', then
6496 we are looking at a compound-literal. */
6498 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6499 /*consume_paren=*/true)
6500 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6501 /* Roll back the tokens we skipped. */
6502 cp_lexer_rollback_tokens (parser->lexer);
6503 /* If we were looking at a compound-literal, simulate an error
6504 so that the call to cp_parser_parse_definitely below will
6506 if (compound_literal_p)
6507 cp_parser_simulate_error (parser);
6510 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6511 parser->in_type_id_in_expr_p = true;
6512 /* Look for the type-id. */
6513 type = cp_parser_type_id (parser);
6514 /* Look for the closing `)'. */
6515 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6516 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6519 /* Restore the saved message. */
6520 parser->type_definition_forbidden_message = saved_message;
6522 /* At this point this can only be either a cast or a
6523 parenthesized ctor such as `(T ())' that looks like a cast to
6524 function returning T. */
6525 if (!cp_parser_error_occurred (parser)
6526 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6529 cp_parser_parse_definitely (parser);
6530 expr = cp_parser_cast_expression (parser,
6531 /*address_p=*/false,
6532 /*cast_p=*/true, pidk);
6534 /* Warn about old-style casts, if so requested. */
6535 if (warn_old_style_cast
6536 && !in_system_header
6537 && !VOID_TYPE_P (type)
6538 && current_lang_name != lang_name_c)
6539 warning (OPT_Wold_style_cast, "use of old-style cast");
6541 /* Only type conversions to integral or enumeration types
6542 can be used in constant-expressions. */
6543 if (!cast_valid_in_integral_constant_expression_p (type)
6544 && cp_parser_non_integral_constant_expression (parser,
6546 return error_mark_node;
6548 /* Perform the cast. */
6549 expr = build_c_cast (input_location, type, expr);
6553 cp_parser_abort_tentative_parse (parser);
6556 /* If we get here, then it's not a cast, so it must be a
6557 unary-expression. */
6558 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6561 /* Parse a binary expression of the general form:
6565 pm-expression .* cast-expression
6566 pm-expression ->* cast-expression
6568 multiplicative-expression:
6570 multiplicative-expression * pm-expression
6571 multiplicative-expression / pm-expression
6572 multiplicative-expression % pm-expression
6574 additive-expression:
6575 multiplicative-expression
6576 additive-expression + multiplicative-expression
6577 additive-expression - multiplicative-expression
6581 shift-expression << additive-expression
6582 shift-expression >> additive-expression
6584 relational-expression:
6586 relational-expression < shift-expression
6587 relational-expression > shift-expression
6588 relational-expression <= shift-expression
6589 relational-expression >= shift-expression
6593 relational-expression:
6594 relational-expression <? shift-expression
6595 relational-expression >? shift-expression
6597 equality-expression:
6598 relational-expression
6599 equality-expression == relational-expression
6600 equality-expression != relational-expression
6604 and-expression & equality-expression
6606 exclusive-or-expression:
6608 exclusive-or-expression ^ and-expression
6610 inclusive-or-expression:
6611 exclusive-or-expression
6612 inclusive-or-expression | exclusive-or-expression
6614 logical-and-expression:
6615 inclusive-or-expression
6616 logical-and-expression && inclusive-or-expression
6618 logical-or-expression:
6619 logical-and-expression
6620 logical-or-expression || logical-and-expression
6622 All these are implemented with a single function like:
6625 simple-cast-expression
6626 binary-expression <token> binary-expression
6628 CAST_P is true if this expression is the target of a cast.
6630 The binops_by_token map is used to get the tree codes for each <token> type.
6631 binary-expressions are associated according to a precedence table. */
6633 #define TOKEN_PRECEDENCE(token) \
6634 (((token->type == CPP_GREATER \
6635 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6636 && !parser->greater_than_is_operator_p) \
6637 ? PREC_NOT_OPERATOR \
6638 : binops_by_token[token->type].prec)
6641 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6642 bool no_toplevel_fold_p,
6643 enum cp_parser_prec prec,
6646 cp_parser_expression_stack stack;
6647 cp_parser_expression_stack_entry *sp = &stack[0];
6650 enum tree_code tree_type, lhs_type, rhs_type;
6651 enum cp_parser_prec new_prec, lookahead_prec;
6654 /* Parse the first expression. */
6655 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6656 lhs_type = ERROR_MARK;
6660 /* Get an operator token. */
6661 token = cp_lexer_peek_token (parser->lexer);
6663 if (warn_cxx0x_compat
6664 && token->type == CPP_RSHIFT
6665 && !parser->greater_than_is_operator_p)
6667 if (warning_at (token->location, OPT_Wc__0x_compat,
6668 "%<>>%> operator will be treated as"
6669 " two right angle brackets in C++0x"))
6670 inform (token->location,
6671 "suggest parentheses around %<>>%> expression");
6674 new_prec = TOKEN_PRECEDENCE (token);
6676 /* Popping an entry off the stack means we completed a subexpression:
6677 - either we found a token which is not an operator (`>' where it is not
6678 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6679 will happen repeatedly;
6680 - or, we found an operator which has lower priority. This is the case
6681 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6683 if (new_prec <= prec)
6692 tree_type = binops_by_token[token->type].tree_type;
6694 /* We used the operator token. */
6695 cp_lexer_consume_token (parser->lexer);
6697 /* For "false && x" or "true || x", x will never be executed;
6698 disable warnings while evaluating it. */
6699 if (tree_type == TRUTH_ANDIF_EXPR)
6700 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6701 else if (tree_type == TRUTH_ORIF_EXPR)
6702 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6704 /* Extract another operand. It may be the RHS of this expression
6705 or the LHS of a new, higher priority expression. */
6706 rhs = cp_parser_simple_cast_expression (parser);
6707 rhs_type = ERROR_MARK;
6709 /* Get another operator token. Look up its precedence to avoid
6710 building a useless (immediately popped) stack entry for common
6711 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6712 token = cp_lexer_peek_token (parser->lexer);
6713 lookahead_prec = TOKEN_PRECEDENCE (token);
6714 if (lookahead_prec > new_prec)
6716 /* ... and prepare to parse the RHS of the new, higher priority
6717 expression. Since precedence levels on the stack are
6718 monotonically increasing, we do not have to care about
6721 sp->tree_type = tree_type;
6723 sp->lhs_type = lhs_type;
6726 lhs_type = rhs_type;
6728 new_prec = lookahead_prec;
6732 lookahead_prec = new_prec;
6733 /* If the stack is not empty, we have parsed into LHS the right side
6734 (`4' in the example above) of an expression we had suspended.
6735 We can use the information on the stack to recover the LHS (`3')
6736 from the stack together with the tree code (`MULT_EXPR'), and
6737 the precedence of the higher level subexpression
6738 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6739 which will be used to actually build the additive expression. */
6742 tree_type = sp->tree_type;
6744 rhs_type = lhs_type;
6746 lhs_type = sp->lhs_type;
6749 /* Undo the disabling of warnings done above. */
6750 if (tree_type == TRUTH_ANDIF_EXPR)
6751 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6752 else if (tree_type == TRUTH_ORIF_EXPR)
6753 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6756 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6757 ERROR_MARK for everything that is not a binary expression.
6758 This makes warn_about_parentheses miss some warnings that
6759 involve unary operators. For unary expressions we should
6760 pass the correct tree_code unless the unary expression was
6761 surrounded by parentheses.
6763 if (no_toplevel_fold_p
6764 && lookahead_prec <= prec
6766 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6767 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6769 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6770 &overload, tf_warning_or_error);
6771 lhs_type = tree_type;
6773 /* If the binary operator required the use of an overloaded operator,
6774 then this expression cannot be an integral constant-expression.
6775 An overloaded operator can be used even if both operands are
6776 otherwise permissible in an integral constant-expression if at
6777 least one of the operands is of enumeration type. */
6780 && cp_parser_non_integral_constant_expression (parser,
6782 return error_mark_node;
6789 /* Parse the `? expression : assignment-expression' part of a
6790 conditional-expression. The LOGICAL_OR_EXPR is the
6791 logical-or-expression that started the conditional-expression.
6792 Returns a representation of the entire conditional-expression.
6794 This routine is used by cp_parser_assignment_expression.
6796 ? expression : assignment-expression
6800 ? : assignment-expression */
6803 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6806 tree assignment_expr;
6807 struct cp_token *token;
6809 /* Consume the `?' token. */
6810 cp_lexer_consume_token (parser->lexer);
6811 token = cp_lexer_peek_token (parser->lexer);
6812 if (cp_parser_allow_gnu_extensions_p (parser)
6813 && token->type == CPP_COLON)
6815 pedwarn (token->location, OPT_pedantic,
6816 "ISO C++ does not allow ?: with omitted middle operand");
6817 /* Implicit true clause. */
6819 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6820 warn_for_omitted_condop (token->location, logical_or_expr);
6824 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6825 parser->colon_corrects_to_scope_p = false;
6826 /* Parse the expression. */
6827 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6828 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6829 c_inhibit_evaluation_warnings +=
6830 ((logical_or_expr == truthvalue_true_node)
6831 - (logical_or_expr == truthvalue_false_node));
6832 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6835 /* The next token should be a `:'. */
6836 cp_parser_require (parser, CPP_COLON, RT_COLON);
6837 /* Parse the assignment-expression. */
6838 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6839 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6841 /* Build the conditional-expression. */
6842 return build_x_conditional_expr (logical_or_expr,
6845 tf_warning_or_error);
6848 /* Parse an assignment-expression.
6850 assignment-expression:
6851 conditional-expression
6852 logical-or-expression assignment-operator assignment_expression
6855 CAST_P is true if this expression is the target of a cast.
6857 Returns a representation for the expression. */
6860 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6865 /* If the next token is the `throw' keyword, then we're looking at
6866 a throw-expression. */
6867 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6868 expr = cp_parser_throw_expression (parser);
6869 /* Otherwise, it must be that we are looking at a
6870 logical-or-expression. */
6873 /* Parse the binary expressions (logical-or-expression). */
6874 expr = cp_parser_binary_expression (parser, cast_p, false,
6875 PREC_NOT_OPERATOR, pidk);
6876 /* If the next token is a `?' then we're actually looking at a
6877 conditional-expression. */
6878 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6879 return cp_parser_question_colon_clause (parser, expr);
6882 enum tree_code assignment_operator;
6884 /* If it's an assignment-operator, we're using the second
6887 = cp_parser_assignment_operator_opt (parser);
6888 if (assignment_operator != ERROR_MARK)
6890 bool non_constant_p;
6892 /* Parse the right-hand side of the assignment. */
6893 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6895 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6896 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6898 /* An assignment may not appear in a
6899 constant-expression. */
6900 if (cp_parser_non_integral_constant_expression (parser,
6902 return error_mark_node;
6903 /* Build the assignment expression. */
6904 expr = build_x_modify_expr (expr,
6905 assignment_operator,
6907 tf_warning_or_error);
6915 /* Parse an (optional) assignment-operator.
6917 assignment-operator: one of
6918 = *= /= %= += -= >>= <<= &= ^= |=
6922 assignment-operator: one of
6925 If the next token is an assignment operator, the corresponding tree
6926 code is returned, and the token is consumed. For example, for
6927 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6928 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6929 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6930 operator, ERROR_MARK is returned. */
6932 static enum tree_code
6933 cp_parser_assignment_operator_opt (cp_parser* parser)
6938 /* Peek at the next token. */
6939 token = cp_lexer_peek_token (parser->lexer);
6941 switch (token->type)
6952 op = TRUNC_DIV_EXPR;
6956 op = TRUNC_MOD_EXPR;
6988 /* Nothing else is an assignment operator. */
6992 /* If it was an assignment operator, consume it. */
6993 if (op != ERROR_MARK)
6994 cp_lexer_consume_token (parser->lexer);
6999 /* Parse an expression.
7002 assignment-expression
7003 expression , assignment-expression
7005 CAST_P is true if this expression is the target of a cast.
7007 Returns a representation of the expression. */
7010 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7012 tree expression = NULL_TREE;
7016 tree assignment_expression;
7018 /* Parse the next assignment-expression. */
7019 assignment_expression
7020 = cp_parser_assignment_expression (parser, cast_p, pidk);
7021 /* If this is the first assignment-expression, we can just
7024 expression = assignment_expression;
7026 expression = build_x_compound_expr (expression,
7027 assignment_expression,
7028 tf_warning_or_error);
7029 /* If the next token is not a comma, then we are done with the
7031 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7033 /* Consume the `,'. */
7034 cp_lexer_consume_token (parser->lexer);
7035 /* A comma operator cannot appear in a constant-expression. */
7036 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7037 expression = error_mark_node;
7043 /* Parse a constant-expression.
7045 constant-expression:
7046 conditional-expression
7048 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7049 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
7050 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
7051 is false, NON_CONSTANT_P should be NULL. */
7054 cp_parser_constant_expression (cp_parser* parser,
7055 bool allow_non_constant_p,
7056 bool *non_constant_p)
7058 bool saved_integral_constant_expression_p;
7059 bool saved_allow_non_integral_constant_expression_p;
7060 bool saved_non_integral_constant_expression_p;
7063 /* It might seem that we could simply parse the
7064 conditional-expression, and then check to see if it were
7065 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
7066 one that the compiler can figure out is constant, possibly after
7067 doing some simplifications or optimizations. The standard has a
7068 precise definition of constant-expression, and we must honor
7069 that, even though it is somewhat more restrictive.
7075 is not a legal declaration, because `(2, 3)' is not a
7076 constant-expression. The `,' operator is forbidden in a
7077 constant-expression. However, GCC's constant-folding machinery
7078 will fold this operation to an INTEGER_CST for `3'. */
7080 /* Save the old settings. */
7081 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7082 saved_allow_non_integral_constant_expression_p
7083 = parser->allow_non_integral_constant_expression_p;
7084 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7085 /* We are now parsing a constant-expression. */
7086 parser->integral_constant_expression_p = true;
7087 parser->allow_non_integral_constant_expression_p
7088 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7089 parser->non_integral_constant_expression_p = false;
7090 /* Although the grammar says "conditional-expression", we parse an
7091 "assignment-expression", which also permits "throw-expression"
7092 and the use of assignment operators. In the case that
7093 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7094 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7095 actually essential that we look for an assignment-expression.
7096 For example, cp_parser_initializer_clauses uses this function to
7097 determine whether a particular assignment-expression is in fact
7099 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7100 /* Restore the old settings. */
7101 parser->integral_constant_expression_p
7102 = saved_integral_constant_expression_p;
7103 parser->allow_non_integral_constant_expression_p
7104 = saved_allow_non_integral_constant_expression_p;
7105 if (cxx_dialect >= cxx0x)
7107 /* Require an rvalue constant expression here; that's what our
7108 callers expect. Reference constant expressions are handled
7109 separately in e.g. cp_parser_template_argument. */
7110 bool is_const = potential_rvalue_constant_expression (expression);
7111 parser->non_integral_constant_expression_p = !is_const;
7112 if (!is_const && !allow_non_constant_p)
7113 require_potential_rvalue_constant_expression (expression);
7115 if (allow_non_constant_p)
7116 *non_constant_p = parser->non_integral_constant_expression_p;
7117 parser->non_integral_constant_expression_p
7118 = saved_non_integral_constant_expression_p;
7123 /* Parse __builtin_offsetof.
7125 offsetof-expression:
7126 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7128 offsetof-member-designator:
7130 | offsetof-member-designator "." id-expression
7131 | offsetof-member-designator "[" expression "]"
7132 | offsetof-member-designator "->" id-expression */
7135 cp_parser_builtin_offsetof (cp_parser *parser)
7137 int save_ice_p, save_non_ice_p;
7142 /* We're about to accept non-integral-constant things, but will
7143 definitely yield an integral constant expression. Save and
7144 restore these values around our local parsing. */
7145 save_ice_p = parser->integral_constant_expression_p;
7146 save_non_ice_p = parser->non_integral_constant_expression_p;
7148 /* Consume the "__builtin_offsetof" token. */
7149 cp_lexer_consume_token (parser->lexer);
7150 /* Consume the opening `('. */
7151 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7152 /* Parse the type-id. */
7153 type = cp_parser_type_id (parser);
7154 /* Look for the `,'. */
7155 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7156 token = cp_lexer_peek_token (parser->lexer);
7158 /* Build the (type *)null that begins the traditional offsetof macro. */
7159 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7160 tf_warning_or_error);
7162 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7163 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7164 true, &dummy, token->location);
7167 token = cp_lexer_peek_token (parser->lexer);
7168 switch (token->type)
7170 case CPP_OPEN_SQUARE:
7171 /* offsetof-member-designator "[" expression "]" */
7172 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7176 /* offsetof-member-designator "->" identifier */
7177 expr = grok_array_decl (expr, integer_zero_node);
7181 /* offsetof-member-designator "." identifier */
7182 cp_lexer_consume_token (parser->lexer);
7183 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7188 case CPP_CLOSE_PAREN:
7189 /* Consume the ")" token. */
7190 cp_lexer_consume_token (parser->lexer);
7194 /* Error. We know the following require will fail, but
7195 that gives the proper error message. */
7196 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7197 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7198 expr = error_mark_node;
7204 /* If we're processing a template, we can't finish the semantics yet.
7205 Otherwise we can fold the entire expression now. */
7206 if (processing_template_decl)
7207 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7209 expr = finish_offsetof (expr);
7212 parser->integral_constant_expression_p = save_ice_p;
7213 parser->non_integral_constant_expression_p = save_non_ice_p;
7218 /* Parse a trait expression.
7220 Returns a representation of the expression, the underlying type
7221 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7224 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7227 tree type1, type2 = NULL_TREE;
7228 bool binary = false;
7229 cp_decl_specifier_seq decl_specs;
7233 case RID_HAS_NOTHROW_ASSIGN:
7234 kind = CPTK_HAS_NOTHROW_ASSIGN;
7236 case RID_HAS_NOTHROW_CONSTRUCTOR:
7237 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7239 case RID_HAS_NOTHROW_COPY:
7240 kind = CPTK_HAS_NOTHROW_COPY;
7242 case RID_HAS_TRIVIAL_ASSIGN:
7243 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7245 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7246 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7248 case RID_HAS_TRIVIAL_COPY:
7249 kind = CPTK_HAS_TRIVIAL_COPY;
7251 case RID_HAS_TRIVIAL_DESTRUCTOR:
7252 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7254 case RID_HAS_VIRTUAL_DESTRUCTOR:
7255 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7257 case RID_IS_ABSTRACT:
7258 kind = CPTK_IS_ABSTRACT;
7260 case RID_IS_BASE_OF:
7261 kind = CPTK_IS_BASE_OF;
7265 kind = CPTK_IS_CLASS;
7267 case RID_IS_CONVERTIBLE_TO:
7268 kind = CPTK_IS_CONVERTIBLE_TO;
7272 kind = CPTK_IS_EMPTY;
7275 kind = CPTK_IS_ENUM;
7277 case RID_IS_LITERAL_TYPE:
7278 kind = CPTK_IS_LITERAL_TYPE;
7283 case RID_IS_POLYMORPHIC:
7284 kind = CPTK_IS_POLYMORPHIC;
7286 case RID_IS_STD_LAYOUT:
7287 kind = CPTK_IS_STD_LAYOUT;
7289 case RID_IS_TRIVIAL:
7290 kind = CPTK_IS_TRIVIAL;
7293 kind = CPTK_IS_UNION;
7295 case RID_UNDERLYING_TYPE:
7296 kind = CPTK_UNDERLYING_TYPE;
7302 /* Consume the token. */
7303 cp_lexer_consume_token (parser->lexer);
7305 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7307 type1 = cp_parser_type_id (parser);
7309 if (type1 == error_mark_node)
7310 return error_mark_node;
7312 /* Build a trivial decl-specifier-seq. */
7313 clear_decl_specs (&decl_specs);
7314 decl_specs.type = type1;
7316 /* Call grokdeclarator to figure out what type this is. */
7317 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7318 /*initialized=*/0, /*attrlist=*/NULL);
7322 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7324 type2 = cp_parser_type_id (parser);
7326 if (type2 == error_mark_node)
7327 return error_mark_node;
7329 /* Build a trivial decl-specifier-seq. */
7330 clear_decl_specs (&decl_specs);
7331 decl_specs.type = type2;
7333 /* Call grokdeclarator to figure out what type this is. */
7334 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7335 /*initialized=*/0, /*attrlist=*/NULL);
7338 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7340 /* Complete the trait expression, which may mean either processing
7341 the trait expr now or saving it for template instantiation. */
7342 return kind != CPTK_UNDERLYING_TYPE
7343 ? finish_trait_expr (kind, type1, type2)
7344 : finish_underlying_type (type1);
7347 /* Lambdas that appear in variable initializer or default argument scope
7348 get that in their mangling, so we need to record it. We might as well
7349 use the count for function and namespace scopes as well. */
7350 static GTY(()) tree lambda_scope;
7351 static GTY(()) int lambda_count;
7352 typedef struct GTY(()) tree_int
7357 DEF_VEC_O(tree_int);
7358 DEF_VEC_ALLOC_O(tree_int,gc);
7359 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7362 start_lambda_scope (tree decl)
7366 /* Once we're inside a function, we ignore other scopes and just push
7367 the function again so that popping works properly. */
7368 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7369 decl = current_function_decl;
7370 ti.t = lambda_scope;
7371 ti.i = lambda_count;
7372 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7373 if (lambda_scope != decl)
7375 /* Don't reset the count if we're still in the same function. */
7376 lambda_scope = decl;
7382 record_lambda_scope (tree lambda)
7384 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7385 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7389 finish_lambda_scope (void)
7391 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7392 if (lambda_scope != p->t)
7394 lambda_scope = p->t;
7395 lambda_count = p->i;
7397 VEC_pop (tree_int, lambda_scope_stack);
7400 /* Parse a lambda expression.
7403 lambda-introducer lambda-declarator [opt] compound-statement
7405 Returns a representation of the expression. */
7408 cp_parser_lambda_expression (cp_parser* parser)
7410 tree lambda_expr = build_lambda_expr ();
7414 LAMBDA_EXPR_LOCATION (lambda_expr)
7415 = cp_lexer_peek_token (parser->lexer)->location;
7417 if (cp_unevaluated_operand)
7418 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7419 "lambda-expression in unevaluated context");
7421 /* We may be in the middle of deferred access check. Disable
7423 push_deferring_access_checks (dk_no_deferred);
7425 cp_parser_lambda_introducer (parser, lambda_expr);
7427 type = begin_lambda_type (lambda_expr);
7429 record_lambda_scope (lambda_expr);
7431 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7432 determine_visibility (TYPE_NAME (type));
7434 /* Now that we've started the type, add the capture fields for any
7435 explicit captures. */
7436 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7439 /* Inside the class, surrounding template-parameter-lists do not apply. */
7440 unsigned int saved_num_template_parameter_lists
7441 = parser->num_template_parameter_lists;
7442 unsigned char in_statement = parser->in_statement;
7443 bool in_switch_statement_p = parser->in_switch_statement_p;
7445 parser->num_template_parameter_lists = 0;
7446 parser->in_statement = 0;
7447 parser->in_switch_statement_p = false;
7449 /* By virtue of defining a local class, a lambda expression has access to
7450 the private variables of enclosing classes. */
7452 ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7455 cp_parser_lambda_body (parser, lambda_expr);
7456 else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7457 cp_parser_skip_to_end_of_block_or_statement (parser);
7459 /* The capture list was built up in reverse order; fix that now. */
7461 tree newlist = NULL_TREE;
7464 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7467 next = TREE_CHAIN (elt);
7468 TREE_CHAIN (elt) = newlist;
7471 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7475 maybe_add_lambda_conv_op (type);
7477 type = finish_struct (type, /*attributes=*/NULL_TREE);
7479 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7480 parser->in_statement = in_statement;
7481 parser->in_switch_statement_p = in_switch_statement_p;
7484 pop_deferring_access_checks ();
7486 /* This field is only used during parsing of the lambda. */
7487 LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7489 /* This lambda shouldn't have any proxies left at this point. */
7490 gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7491 /* And now that we're done, push proxies for an enclosing lambda. */
7492 insert_pending_capture_proxies ();
7495 return build_lambda_object (lambda_expr);
7497 return error_mark_node;
7500 /* Parse the beginning of a lambda expression.
7503 [ lambda-capture [opt] ]
7505 LAMBDA_EXPR is the current representation of the lambda expression. */
7508 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7510 /* Need commas after the first capture. */
7513 /* Eat the leading `['. */
7514 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7516 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7517 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7518 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7519 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7520 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7521 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7523 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7525 cp_lexer_consume_token (parser->lexer);
7529 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7531 cp_token* capture_token;
7533 tree capture_init_expr;
7534 cp_id_kind idk = CP_ID_KIND_NONE;
7535 bool explicit_init_p = false;
7537 enum capture_kind_type
7542 enum capture_kind_type capture_kind = BY_COPY;
7544 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7546 error ("expected end of capture-list");
7553 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7555 /* Possibly capture `this'. */
7556 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7558 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7559 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7560 pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7561 "with by-copy capture default");
7562 cp_lexer_consume_token (parser->lexer);
7563 add_capture (lambda_expr,
7564 /*id=*/this_identifier,
7565 /*initializer=*/finish_this_expr(),
7566 /*by_reference_p=*/false,
7571 /* Remember whether we want to capture as a reference or not. */
7572 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7574 capture_kind = BY_REFERENCE;
7575 cp_lexer_consume_token (parser->lexer);
7578 /* Get the identifier. */
7579 capture_token = cp_lexer_peek_token (parser->lexer);
7580 capture_id = cp_parser_identifier (parser);
7582 if (capture_id == error_mark_node)
7583 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7584 delimiters, but I modified this to stop on unnested ']' as well. It
7585 was already changed to stop on unnested '}', so the
7586 "closing_parenthesis" name is no more misleading with my change. */
7588 cp_parser_skip_to_closing_parenthesis (parser,
7589 /*recovering=*/true,
7591 /*consume_paren=*/true);
7595 /* Find the initializer for this capture. */
7596 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7598 /* An explicit expression exists. */
7599 cp_lexer_consume_token (parser->lexer);
7600 pedwarn (input_location, OPT_pedantic,
7601 "ISO C++ does not allow initializers "
7602 "in lambda expression capture lists");
7603 capture_init_expr = cp_parser_assignment_expression (parser,
7606 explicit_init_p = true;
7610 const char* error_msg;
7612 /* Turn the identifier into an id-expression. */
7614 = cp_parser_lookup_name
7618 /*is_template=*/false,
7619 /*is_namespace=*/false,
7620 /*check_dependency=*/true,
7621 /*ambiguous_decls=*/NULL,
7622 capture_token->location);
7625 = finish_id_expression
7630 /*integral_constant_expression_p=*/false,
7631 /*allow_non_integral_constant_expression_p=*/false,
7632 /*non_integral_constant_expression_p=*/NULL,
7633 /*template_p=*/false,
7635 /*address_p=*/false,
7636 /*template_arg_p=*/false,
7638 capture_token->location);
7641 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7643 = unqualified_name_lookup_error (capture_init_expr);
7645 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7646 && !explicit_init_p)
7648 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7649 && capture_kind == BY_COPY)
7650 pedwarn (capture_token->location, 0, "explicit by-copy capture "
7651 "of %qD redundant with by-copy capture default",
7653 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7654 && capture_kind == BY_REFERENCE)
7655 pedwarn (capture_token->location, 0, "explicit by-reference "
7656 "capture of %qD redundant with by-reference capture "
7657 "default", capture_id);
7660 add_capture (lambda_expr,
7663 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7667 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7670 /* Parse the (optional) middle of a lambda expression.
7673 ( parameter-declaration-clause [opt] )
7674 attribute-specifier [opt]
7676 exception-specification [opt]
7677 lambda-return-type-clause [opt]
7679 LAMBDA_EXPR is the current representation of the lambda expression. */
7682 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7684 /* 5.1.1.4 of the standard says:
7685 If a lambda-expression does not include a lambda-declarator, it is as if
7686 the lambda-declarator were ().
7687 This means an empty parameter list, no attributes, and no exception
7689 tree param_list = void_list_node;
7690 tree attributes = NULL_TREE;
7691 tree exception_spec = NULL_TREE;
7694 /* The lambda-declarator is optional, but must begin with an opening
7695 parenthesis if present. */
7696 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7698 cp_lexer_consume_token (parser->lexer);
7700 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7702 /* Parse parameters. */
7703 param_list = cp_parser_parameter_declaration_clause (parser);
7705 /* Default arguments shall not be specified in the
7706 parameter-declaration-clause of a lambda-declarator. */
7707 for (t = param_list; t; t = TREE_CHAIN (t))
7708 if (TREE_PURPOSE (t))
7709 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7710 "default argument specified for lambda parameter");
7712 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7714 attributes = cp_parser_attributes_opt (parser);
7716 /* Parse optional `mutable' keyword. */
7717 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7719 cp_lexer_consume_token (parser->lexer);
7720 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7723 /* Parse optional exception specification. */
7724 exception_spec = cp_parser_exception_specification_opt (parser);
7726 /* Parse optional trailing return type. */
7727 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7729 cp_lexer_consume_token (parser->lexer);
7730 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7733 /* The function parameters must be in scope all the way until after the
7734 trailing-return-type in case of decltype. */
7735 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7736 pop_binding (DECL_NAME (t), t);
7741 /* Create the function call operator.
7743 Messing with declarators like this is no uglier than building up the
7744 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7747 cp_decl_specifier_seq return_type_specs;
7748 cp_declarator* declarator;
7753 clear_decl_specs (&return_type_specs);
7754 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7755 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7757 /* Maybe we will deduce the return type later, but we can use void
7758 as a placeholder return type anyways. */
7759 return_type_specs.type = void_type_node;
7761 p = obstack_alloc (&declarator_obstack, 0);
7763 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7766 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7767 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7768 declarator = make_call_declarator (declarator, param_list, quals,
7769 VIRT_SPEC_UNSPECIFIED,
7771 /*late_return_type=*/NULL_TREE);
7772 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7774 fco = grokmethod (&return_type_specs,
7777 if (fco != error_mark_node)
7779 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7780 DECL_ARTIFICIAL (fco) = 1;
7781 /* Give the object parameter a different name. */
7782 DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7785 finish_member_declaration (fco);
7787 obstack_free (&declarator_obstack, p);
7789 return (fco != error_mark_node);
7793 /* Parse the body of a lambda expression, which is simply
7797 but which requires special handling.
7798 LAMBDA_EXPR is the current representation of the lambda expression. */
7801 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7803 bool nested = (current_function_decl != NULL_TREE);
7804 bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
7806 push_function_context ();
7808 /* Still increment function_depth so that we don't GC in the
7809 middle of an expression. */
7811 /* Clear this in case we're in the middle of a default argument. */
7812 parser->local_variables_forbidden_p = false;
7814 /* Finish the function call operator
7816 + late_parsing_for_member
7817 + function_definition_after_declarator
7818 + ctor_initializer_opt_and_function_body */
7820 tree fco = lambda_function (lambda_expr);
7826 /* Let the front end know that we are going to be defining this
7828 start_preparsed_function (fco,
7830 SF_PRE_PARSED | SF_INCLASS_INLINE);
7832 start_lambda_scope (fco);
7833 body = begin_function_body ();
7835 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7838 /* Push the proxies for any explicit captures. */
7839 for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7840 cap = TREE_CHAIN (cap))
7841 build_capture_proxy (TREE_PURPOSE (cap));
7843 compound_stmt = begin_compound_stmt (0);
7845 /* 5.1.1.4 of the standard says:
7846 If a lambda-expression does not include a trailing-return-type, it
7847 is as if the trailing-return-type denotes the following type:
7848 * if the compound-statement is of the form
7849 { return attribute-specifier [opt] expression ; }
7850 the type of the returned expression after lvalue-to-rvalue
7851 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7852 (_conv.array_ 4.2), and function-to-pointer conversion
7854 * otherwise, void. */
7856 /* In a lambda that has neither a lambda-return-type-clause
7857 nor a deducible form, errors should be reported for return statements
7858 in the body. Since we used void as the placeholder return type, parsing
7859 the body as usual will give such desired behavior. */
7860 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7861 && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7862 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7864 tree expr = NULL_TREE;
7865 cp_id_kind idk = CP_ID_KIND_NONE;
7867 /* Parse tentatively in case there's more after the initial return
7869 cp_parser_parse_tentatively (parser);
7871 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7873 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7875 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7876 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7878 if (cp_parser_parse_definitely (parser))
7880 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7882 /* Will get error here if type not deduced yet. */
7883 finish_return_stmt (expr);
7891 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7892 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7893 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7894 cp_parser_label_declaration (parser);
7895 cp_parser_statement_seq_opt (parser, NULL_TREE);
7896 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7897 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7900 finish_compound_stmt (compound_stmt);
7903 finish_function_body (body);
7904 finish_lambda_scope ();
7906 /* Finish the function and generate code for it if necessary. */
7907 expand_or_defer_fn (finish_function (/*inline*/2));
7910 parser->local_variables_forbidden_p = local_variables_forbidden_p;
7912 pop_function_context();
7917 /* Statements [gram.stmt.stmt] */
7919 /* Parse a statement.
7923 expression-statement
7928 declaration-statement
7931 IN_COMPOUND is true when the statement is nested inside a
7932 cp_parser_compound_statement; this matters for certain pragmas.
7934 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7935 is a (possibly labeled) if statement which is not enclosed in braces
7936 and has an else clause. This is used to implement -Wparentheses. */
7939 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7940 bool in_compound, bool *if_p)
7944 location_t statement_location;
7949 /* There is no statement yet. */
7950 statement = NULL_TREE;
7951 /* Peek at the next token. */
7952 token = cp_lexer_peek_token (parser->lexer);
7953 /* Remember the location of the first token in the statement. */
7954 statement_location = token->location;
7955 /* If this is a keyword, then that will often determine what kind of
7956 statement we have. */
7957 if (token->type == CPP_KEYWORD)
7959 enum rid keyword = token->keyword;
7965 /* Looks like a labeled-statement with a case label.
7966 Parse the label, and then use tail recursion to parse
7968 cp_parser_label_for_labeled_statement (parser);
7973 statement = cp_parser_selection_statement (parser, if_p);
7979 statement = cp_parser_iteration_statement (parser);
7986 statement = cp_parser_jump_statement (parser);
7989 /* Objective-C++ exception-handling constructs. */
7992 case RID_AT_FINALLY:
7993 case RID_AT_SYNCHRONIZED:
7995 statement = cp_parser_objc_statement (parser);
7999 statement = cp_parser_try_block (parser);
8003 /* This must be a namespace alias definition. */
8004 cp_parser_declaration_statement (parser);
8008 /* It might be a keyword like `int' that can start a
8009 declaration-statement. */
8013 else if (token->type == CPP_NAME)
8015 /* If the next token is a `:', then we are looking at a
8016 labeled-statement. */
8017 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8018 if (token->type == CPP_COLON)
8020 /* Looks like a labeled-statement with an ordinary label.
8021 Parse the label, and then use tail recursion to parse
8023 cp_parser_label_for_labeled_statement (parser);
8027 /* Anything that starts with a `{' must be a compound-statement. */
8028 else if (token->type == CPP_OPEN_BRACE)
8029 statement = cp_parser_compound_statement (parser, NULL, false, false);
8030 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8031 a statement all its own. */
8032 else if (token->type == CPP_PRAGMA)
8034 /* Only certain OpenMP pragmas are attached to statements, and thus
8035 are considered statements themselves. All others are not. In
8036 the context of a compound, accept the pragma as a "statement" and
8037 return so that we can check for a close brace. Otherwise we
8038 require a real statement and must go back and read one. */
8040 cp_parser_pragma (parser, pragma_compound);
8041 else if (!cp_parser_pragma (parser, pragma_stmt))
8045 else if (token->type == CPP_EOF)
8047 cp_parser_error (parser, "expected statement");
8051 /* Everything else must be a declaration-statement or an
8052 expression-statement. Try for the declaration-statement
8053 first, unless we are looking at a `;', in which case we know that
8054 we have an expression-statement. */
8057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8059 cp_parser_parse_tentatively (parser);
8060 /* Try to parse the declaration-statement. */
8061 cp_parser_declaration_statement (parser);
8062 /* If that worked, we're done. */
8063 if (cp_parser_parse_definitely (parser))
8066 /* Look for an expression-statement instead. */
8067 statement = cp_parser_expression_statement (parser, in_statement_expr);
8070 /* Set the line number for the statement. */
8071 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8072 SET_EXPR_LOCATION (statement, statement_location);
8075 /* Parse the label for a labeled-statement, i.e.
8078 case constant-expression :
8082 case constant-expression ... constant-expression : statement
8084 When a label is parsed without errors, the label is added to the
8085 parse tree by the finish_* functions, so this function doesn't
8086 have to return the label. */
8089 cp_parser_label_for_labeled_statement (cp_parser* parser)
8092 tree label = NULL_TREE;
8093 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8095 /* The next token should be an identifier. */
8096 token = cp_lexer_peek_token (parser->lexer);
8097 if (token->type != CPP_NAME
8098 && token->type != CPP_KEYWORD)
8100 cp_parser_error (parser, "expected labeled-statement");
8104 parser->colon_corrects_to_scope_p = false;
8105 switch (token->keyword)
8112 /* Consume the `case' token. */
8113 cp_lexer_consume_token (parser->lexer);
8114 /* Parse the constant-expression. */
8115 expr = cp_parser_constant_expression (parser,
8116 /*allow_non_constant_p=*/false,
8119 ellipsis = cp_lexer_peek_token (parser->lexer);
8120 if (ellipsis->type == CPP_ELLIPSIS)
8122 /* Consume the `...' token. */
8123 cp_lexer_consume_token (parser->lexer);
8125 cp_parser_constant_expression (parser,
8126 /*allow_non_constant_p=*/false,
8128 /* We don't need to emit warnings here, as the common code
8129 will do this for us. */
8132 expr_hi = NULL_TREE;
8134 if (parser->in_switch_statement_p)
8135 finish_case_label (token->location, expr, expr_hi);
8137 error_at (token->location,
8138 "case label %qE not within a switch statement",
8144 /* Consume the `default' token. */
8145 cp_lexer_consume_token (parser->lexer);
8147 if (parser->in_switch_statement_p)
8148 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8150 error_at (token->location, "case label not within a switch statement");
8154 /* Anything else must be an ordinary label. */
8155 label = finish_label_stmt (cp_parser_identifier (parser));
8159 /* Require the `:' token. */
8160 cp_parser_require (parser, CPP_COLON, RT_COLON);
8162 /* An ordinary label may optionally be followed by attributes.
8163 However, this is only permitted if the attributes are then
8164 followed by a semicolon. This is because, for backward
8165 compatibility, when parsing
8166 lab: __attribute__ ((unused)) int i;
8167 we want the attribute to attach to "i", not "lab". */
8168 if (label != NULL_TREE
8169 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8173 cp_parser_parse_tentatively (parser);
8174 attrs = cp_parser_attributes_opt (parser);
8175 if (attrs == NULL_TREE
8176 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8177 cp_parser_abort_tentative_parse (parser);
8178 else if (!cp_parser_parse_definitely (parser))
8181 cplus_decl_attributes (&label, attrs, 0);
8184 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8187 /* Parse an expression-statement.
8189 expression-statement:
8192 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194 indicates whether this expression-statement is part of an
8195 expression statement. */
8198 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8200 tree statement = NULL_TREE;
8201 cp_token *token = cp_lexer_peek_token (parser->lexer);
8203 /* If the next token is a ';', then there is no expression
8205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8206 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8208 /* Give a helpful message for "A<T>::type t;" and the like. */
8209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8210 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8212 if (TREE_CODE (statement) == SCOPE_REF)
8213 error_at (token->location, "need %<typename%> before %qE because "
8214 "%qT is a dependent scope",
8215 statement, TREE_OPERAND (statement, 0));
8216 else if (is_overloaded_fn (statement)
8217 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8220 tree fn = get_first_fn (statement);
8221 error_at (token->location,
8222 "%<%T::%D%> names the constructor, not the type",
8223 DECL_CONTEXT (fn), DECL_NAME (fn));
8227 /* Consume the final `;'. */
8228 cp_parser_consume_semicolon_at_end_of_statement (parser);
8230 if (in_statement_expr
8231 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8232 /* This is the final expression statement of a statement
8234 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8236 statement = finish_expr_stmt (statement);
8243 /* Parse a compound-statement.
8246 { statement-seq [opt] }
8251 { label-declaration-seq [opt] statement-seq [opt] }
8253 label-declaration-seq:
8255 label-declaration-seq label-declaration
8257 Returns a tree representing the statement. */
8260 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8261 bool in_try, bool function_body)
8265 /* Consume the `{'. */
8266 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8267 return error_mark_node;
8268 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8270 pedwarn (input_location, OPT_pedantic,
8271 "compound-statement in constexpr function");
8272 /* Begin the compound-statement. */
8273 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8274 /* If the next keyword is `__label__' we have a label declaration. */
8275 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8276 cp_parser_label_declaration (parser);
8277 /* Parse an (optional) statement-seq. */
8278 cp_parser_statement_seq_opt (parser, in_statement_expr);
8279 /* Finish the compound-statement. */
8280 finish_compound_stmt (compound_stmt);
8281 /* Consume the `}'. */
8282 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8284 return compound_stmt;
8287 /* Parse an (optional) statement-seq.
8291 statement-seq [opt] statement */
8294 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8296 /* Scan statements until there aren't any more. */
8299 cp_token *token = cp_lexer_peek_token (parser->lexer);
8301 /* If we are looking at a `}', then we have run out of
8302 statements; the same is true if we have reached the end
8303 of file, or have stumbled upon a stray '@end'. */
8304 if (token->type == CPP_CLOSE_BRACE
8305 || token->type == CPP_EOF
8306 || token->type == CPP_PRAGMA_EOL
8307 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8310 /* If we are in a compound statement and find 'else' then
8311 something went wrong. */
8312 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8314 if (parser->in_statement & IN_IF_STMT)
8318 token = cp_lexer_consume_token (parser->lexer);
8319 error_at (token->location, "%<else%> without a previous %<if%>");
8323 /* Parse the statement. */
8324 cp_parser_statement (parser, in_statement_expr, true, NULL);
8328 /* Parse a selection-statement.
8330 selection-statement:
8331 if ( condition ) statement
8332 if ( condition ) statement else statement
8333 switch ( condition ) statement
8335 Returns the new IF_STMT or SWITCH_STMT.
8337 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8338 is a (possibly labeled) if statement which is not enclosed in
8339 braces and has an else clause. This is used to implement
8343 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8351 /* Peek at the next token. */
8352 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8354 /* See what kind of keyword it is. */
8355 keyword = token->keyword;
8364 /* Look for the `('. */
8365 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8367 cp_parser_skip_to_end_of_statement (parser);
8368 return error_mark_node;
8371 /* Begin the selection-statement. */
8372 if (keyword == RID_IF)
8373 statement = begin_if_stmt ();
8375 statement = begin_switch_stmt ();
8377 /* Parse the condition. */
8378 condition = cp_parser_condition (parser);
8379 /* Look for the `)'. */
8380 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8381 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8382 /*consume_paren=*/true);
8384 if (keyword == RID_IF)
8387 unsigned char in_statement;
8389 /* Add the condition. */
8390 finish_if_stmt_cond (condition, statement);
8392 /* Parse the then-clause. */
8393 in_statement = parser->in_statement;
8394 parser->in_statement |= IN_IF_STMT;
8395 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8397 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8398 add_stmt (build_empty_stmt (loc));
8399 cp_lexer_consume_token (parser->lexer);
8400 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8401 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8402 "empty body in an %<if%> statement");
8406 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8407 parser->in_statement = in_statement;
8409 finish_then_clause (statement);
8411 /* If the next token is `else', parse the else-clause. */
8412 if (cp_lexer_next_token_is_keyword (parser->lexer,
8415 /* Consume the `else' keyword. */
8416 cp_lexer_consume_token (parser->lexer);
8417 begin_else_clause (statement);
8418 /* Parse the else-clause. */
8419 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8422 loc = cp_lexer_peek_token (parser->lexer)->location;
8424 OPT_Wempty_body, "suggest braces around "
8425 "empty body in an %<else%> statement");
8426 add_stmt (build_empty_stmt (loc));
8427 cp_lexer_consume_token (parser->lexer);
8430 cp_parser_implicitly_scoped_statement (parser, NULL);
8432 finish_else_clause (statement);
8434 /* If we are currently parsing a then-clause, then
8435 IF_P will not be NULL. We set it to true to
8436 indicate that this if statement has an else clause.
8437 This may trigger the Wparentheses warning below
8438 when we get back up to the parent if statement. */
8444 /* This if statement does not have an else clause. If
8445 NESTED_IF is true, then the then-clause is an if
8446 statement which does have an else clause. We warn
8447 about the potential ambiguity. */
8449 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8450 "suggest explicit braces to avoid ambiguous"
8454 /* Now we're all done with the if-statement. */
8455 finish_if_stmt (statement);
8459 bool in_switch_statement_p;
8460 unsigned char in_statement;
8462 /* Add the condition. */
8463 finish_switch_cond (condition, statement);
8465 /* Parse the body of the switch-statement. */
8466 in_switch_statement_p = parser->in_switch_statement_p;
8467 in_statement = parser->in_statement;
8468 parser->in_switch_statement_p = true;
8469 parser->in_statement |= IN_SWITCH_STMT;
8470 cp_parser_implicitly_scoped_statement (parser, NULL);
8471 parser->in_switch_statement_p = in_switch_statement_p;
8472 parser->in_statement = in_statement;
8474 /* Now we're all done with the switch-statement. */
8475 finish_switch_stmt (statement);
8483 cp_parser_error (parser, "expected selection-statement");
8484 return error_mark_node;
8488 /* Parse a condition.
8492 type-specifier-seq declarator = initializer-clause
8493 type-specifier-seq declarator braced-init-list
8498 type-specifier-seq declarator asm-specification [opt]
8499 attributes [opt] = assignment-expression
8501 Returns the expression that should be tested. */
8504 cp_parser_condition (cp_parser* parser)
8506 cp_decl_specifier_seq type_specifiers;
8507 const char *saved_message;
8508 int declares_class_or_enum;
8510 /* Try the declaration first. */
8511 cp_parser_parse_tentatively (parser);
8512 /* New types are not allowed in the type-specifier-seq for a
8514 saved_message = parser->type_definition_forbidden_message;
8515 parser->type_definition_forbidden_message
8516 = G_("types may not be defined in conditions");
8517 /* Parse the type-specifier-seq. */
8518 cp_parser_decl_specifier_seq (parser,
8519 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8521 &declares_class_or_enum);
8522 /* Restore the saved message. */
8523 parser->type_definition_forbidden_message = saved_message;
8524 /* If all is well, we might be looking at a declaration. */
8525 if (!cp_parser_error_occurred (parser))
8528 tree asm_specification;
8530 cp_declarator *declarator;
8531 tree initializer = NULL_TREE;
8533 /* Parse the declarator. */
8534 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8535 /*ctor_dtor_or_conv_p=*/NULL,
8536 /*parenthesized_p=*/NULL,
8537 /*member_p=*/false);
8538 /* Parse the attributes. */
8539 attributes = cp_parser_attributes_opt (parser);
8540 /* Parse the asm-specification. */
8541 asm_specification = cp_parser_asm_specification_opt (parser);
8542 /* If the next token is not an `=' or '{', then we might still be
8543 looking at an expression. For example:
8547 looks like a decl-specifier-seq and a declarator -- but then
8548 there is no `=', so this is an expression. */
8549 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8550 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8551 cp_parser_simulate_error (parser);
8553 /* If we did see an `=' or '{', then we are looking at a declaration
8555 if (cp_parser_parse_definitely (parser))
8558 bool non_constant_p;
8559 bool flags = LOOKUP_ONLYCONVERTING;
8561 /* Create the declaration. */
8562 decl = start_decl (declarator, &type_specifiers,
8563 /*initialized_p=*/true,
8564 attributes, /*prefix_attributes=*/NULL_TREE,
8567 /* Parse the initializer. */
8568 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8570 initializer = cp_parser_braced_list (parser, &non_constant_p);
8571 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8576 /* Consume the `='. */
8577 cp_parser_require (parser, CPP_EQ, RT_EQ);
8578 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8580 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8581 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8583 /* Process the initializer. */
8584 cp_finish_decl (decl,
8585 initializer, !non_constant_p,
8590 pop_scope (pushed_scope);
8592 return convert_from_reference (decl);
8595 /* If we didn't even get past the declarator successfully, we are
8596 definitely not looking at a declaration. */
8598 cp_parser_abort_tentative_parse (parser);
8600 /* Otherwise, we are looking at an expression. */
8601 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8604 /* Parses a for-statement or range-for-statement until the closing ')',
8608 cp_parser_for (cp_parser *parser)
8610 tree init, scope, decl;
8613 /* Begin the for-statement. */
8614 scope = begin_for_scope (&init);
8616 /* Parse the initialization. */
8617 is_range_for = cp_parser_for_init_statement (parser, &decl);
8620 return cp_parser_range_for (parser, scope, init, decl);
8622 return cp_parser_c_for (parser, scope, init);
8626 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8628 /* Normal for loop */
8629 tree condition = NULL_TREE;
8630 tree expression = NULL_TREE;
8633 stmt = begin_for_stmt (scope, init);
8634 /* The for-init-statement has already been parsed in
8635 cp_parser_for_init_statement, so no work is needed here. */
8636 finish_for_init_stmt (stmt);
8638 /* If there's a condition, process it. */
8639 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8640 condition = cp_parser_condition (parser);
8641 finish_for_cond (condition, stmt);
8642 /* Look for the `;'. */
8643 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8645 /* If there's an expression, process it. */
8646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8647 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8648 finish_for_expr (expression, stmt);
8653 /* Tries to parse a range-based for-statement:
8656 decl-specifier-seq declarator : expression
8658 The decl-specifier-seq declarator and the `:' are already parsed by
8659 cp_parser_for_init_statement. If processing_template_decl it returns a
8660 newly created RANGE_FOR_STMT; if not, it is converted to a
8661 regular FOR_STMT. */
8664 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8666 tree stmt, range_expr;
8668 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8670 bool expr_non_constant_p;
8671 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8674 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8676 /* If in template, STMT is converted to a normal for-statement
8677 at instantiation. If not, it is done just ahead. */
8678 if (processing_template_decl)
8680 stmt = begin_range_for_stmt (scope, init);
8681 finish_range_for_decl (stmt, range_decl, range_expr);
8682 if (!type_dependent_expression_p (range_expr))
8683 do_range_for_auto_deduction (range_decl, range_expr);
8687 stmt = begin_for_stmt (scope, init);
8688 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8693 /* Subroutine of cp_convert_range_for: given the initializer expression,
8694 builds up the range temporary. */
8697 build_range_temp (tree range_expr)
8699 tree range_type, range_temp;
8701 /* Find out the type deduced by the declaration
8702 `auto &&__range = range_expr'. */
8703 range_type = cp_build_reference_type (make_auto (), true);
8704 range_type = do_auto_deduction (range_type, range_expr,
8705 type_uses_auto (range_type));
8707 /* Create the __range variable. */
8708 range_temp = build_decl (input_location, VAR_DECL,
8709 get_identifier ("__for_range"), range_type);
8710 TREE_USED (range_temp) = 1;
8711 DECL_ARTIFICIAL (range_temp) = 1;
8716 /* Used by cp_parser_range_for in template context: we aren't going to
8717 do a full conversion yet, but we still need to resolve auto in the
8718 type of the for-range-declaration if present. This is basically
8719 a shortcut version of cp_convert_range_for. */
8722 do_range_for_auto_deduction (tree decl, tree range_expr)
8724 tree auto_node = type_uses_auto (TREE_TYPE (decl));
8727 tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
8728 range_temp = convert_from_reference (build_range_temp (range_expr));
8729 iter_type = (cp_parser_perform_range_for_lookup
8730 (range_temp, &begin_dummy, &end_dummy));
8731 iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
8732 iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
8733 tf_warning_or_error);
8734 TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
8735 iter_decl, auto_node);
8739 /* Converts a range-based for-statement into a normal
8740 for-statement, as per the definition.
8742 for (RANGE_DECL : RANGE_EXPR)
8745 should be equivalent to:
8748 auto &&__range = RANGE_EXPR;
8749 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8753 RANGE_DECL = *__begin;
8758 If RANGE_EXPR is an array:
8759 BEGIN_EXPR = __range
8760 END_EXPR = __range + ARRAY_SIZE(__range)
8761 Else if RANGE_EXPR has a member 'begin' or 'end':
8762 BEGIN_EXPR = __range.begin()
8763 END_EXPR = __range.end()
8765 BEGIN_EXPR = begin(__range)
8766 END_EXPR = end(__range);
8768 If __range has a member 'begin' but not 'end', or vice versa, we must
8769 still use the second alternative (it will surely fail, however).
8770 When calling begin()/end() in the third alternative we must use
8771 argument dependent lookup, but always considering 'std' as an associated
8775 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8778 tree iter_type, begin_expr, end_expr;
8779 tree condition, expression;
8781 if (range_decl == error_mark_node || range_expr == error_mark_node)
8782 /* If an error happened previously do nothing or else a lot of
8783 unhelpful errors would be issued. */
8784 begin_expr = end_expr = iter_type = error_mark_node;
8787 tree range_temp = build_range_temp (range_expr);
8788 pushdecl (range_temp);
8789 cp_finish_decl (range_temp, range_expr,
8790 /*is_constant_init*/false, NULL_TREE,
8791 LOOKUP_ONLYCONVERTING);
8793 range_temp = convert_from_reference (range_temp);
8794 iter_type = cp_parser_perform_range_for_lookup (range_temp,
8795 &begin_expr, &end_expr);
8798 /* The new for initialization statement. */
8799 begin = build_decl (input_location, VAR_DECL,
8800 get_identifier ("__for_begin"), iter_type);
8801 TREE_USED (begin) = 1;
8802 DECL_ARTIFICIAL (begin) = 1;
8804 cp_finish_decl (begin, begin_expr,
8805 /*is_constant_init*/false, NULL_TREE,
8806 LOOKUP_ONLYCONVERTING);
8808 end = build_decl (input_location, VAR_DECL,
8809 get_identifier ("__for_end"), iter_type);
8810 TREE_USED (end) = 1;
8811 DECL_ARTIFICIAL (end) = 1;
8813 cp_finish_decl (end, end_expr,
8814 /*is_constant_init*/false, NULL_TREE,
8815 LOOKUP_ONLYCONVERTING);
8817 finish_for_init_stmt (statement);
8819 /* The new for condition. */
8820 condition = build_x_binary_op (NE_EXPR,
8823 NULL, tf_warning_or_error);
8824 finish_for_cond (condition, statement);
8826 /* The new increment expression. */
8827 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8828 finish_for_expr (expression, statement);
8830 /* The declaration is initialized with *__begin inside the loop body. */
8831 cp_finish_decl (range_decl,
8832 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8833 /*is_constant_init*/false, NULL_TREE,
8834 LOOKUP_ONLYCONVERTING);
8839 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8840 We need to solve both at the same time because the method used
8841 depends on the existence of members begin or end.
8842 Returns the type deduced for the iterator expression. */
8845 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8847 if (error_operand_p (range))
8849 *begin = *end = error_mark_node;
8850 return error_mark_node;
8853 if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8855 error ("range-based %<for%> expression of type %qT "
8856 "has incomplete type", TREE_TYPE (range));
8857 *begin = *end = error_mark_node;
8858 return error_mark_node;
8860 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8862 /* If RANGE is an array, we will use pointer arithmetic. */
8864 *end = build_binary_op (input_location, PLUS_EXPR,
8866 array_type_nelts_top (TREE_TYPE (range)),
8868 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8872 /* If it is not an array, we must do a bit of magic. */
8873 tree id_begin, id_end;
8874 tree member_begin, member_end;
8876 *begin = *end = error_mark_node;
8878 id_begin = get_identifier ("begin");
8879 id_end = get_identifier ("end");
8880 member_begin = lookup_member (TREE_TYPE (range), id_begin,
8881 /*protect=*/2, /*want_type=*/false);
8882 member_end = lookup_member (TREE_TYPE (range), id_end,
8883 /*protect=*/2, /*want_type=*/false);
8885 if (member_begin != NULL_TREE || member_end != NULL_TREE)
8887 /* Use the member functions. */
8888 if (member_begin != NULL_TREE)
8889 *begin = cp_parser_range_for_member_function (range, id_begin);
8891 error ("range-based %<for%> expression of type %qT has an "
8892 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8894 if (member_end != NULL_TREE)
8895 *end = cp_parser_range_for_member_function (range, id_end);
8897 error ("range-based %<for%> expression of type %qT has a "
8898 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8902 /* Use global functions with ADL. */
8904 vec = make_tree_vector ();
8906 VEC_safe_push (tree, gc, vec, range);
8908 member_begin = perform_koenig_lookup (id_begin, vec,
8909 /*include_std=*/true,
8910 tf_warning_or_error);
8911 *begin = finish_call_expr (member_begin, &vec, false, true,
8912 tf_warning_or_error);
8913 member_end = perform_koenig_lookup (id_end, vec,
8914 /*include_std=*/true,
8915 tf_warning_or_error);
8916 *end = finish_call_expr (member_end, &vec, false, true,
8917 tf_warning_or_error);
8919 release_tree_vector (vec);
8922 /* Last common checks. */
8923 if (*begin == error_mark_node || *end == error_mark_node)
8925 /* If one of the expressions is an error do no more checks. */
8926 *begin = *end = error_mark_node;
8927 return error_mark_node;
8931 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8932 /* The unqualified type of the __begin and __end temporaries should
8933 be the same, as required by the multiple auto declaration. */
8934 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8935 error ("inconsistent begin/end types in range-based %<for%> "
8936 "statement: %qT and %qT",
8937 TREE_TYPE (*begin), TREE_TYPE (*end));
8943 /* Helper function for cp_parser_perform_range_for_lookup.
8944 Builds a tree for RANGE.IDENTIFIER(). */
8947 cp_parser_range_for_member_function (tree range, tree identifier)
8952 member = finish_class_member_access_expr (range, identifier,
8953 false, tf_warning_or_error);
8954 if (member == error_mark_node)
8955 return error_mark_node;
8957 vec = make_tree_vector ();
8958 res = finish_call_expr (member, &vec,
8959 /*disallow_virtual=*/false,
8961 tf_warning_or_error);
8962 release_tree_vector (vec);
8966 /* Parse an iteration-statement.
8968 iteration-statement:
8969 while ( condition ) statement
8970 do statement while ( expression ) ;
8971 for ( for-init-statement condition [opt] ; expression [opt] )
8974 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8977 cp_parser_iteration_statement (cp_parser* parser)
8982 unsigned char in_statement;
8984 /* Peek at the next token. */
8985 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8987 return error_mark_node;
8989 /* Remember whether or not we are already within an iteration
8991 in_statement = parser->in_statement;
8993 /* See what kind of keyword it is. */
8994 keyword = token->keyword;
9001 /* Begin the while-statement. */
9002 statement = begin_while_stmt ();
9003 /* Look for the `('. */
9004 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9005 /* Parse the condition. */
9006 condition = cp_parser_condition (parser);
9007 finish_while_stmt_cond (condition, statement);
9008 /* Look for the `)'. */
9009 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9010 /* Parse the dependent statement. */
9011 parser->in_statement = IN_ITERATION_STMT;
9012 cp_parser_already_scoped_statement (parser);
9013 parser->in_statement = in_statement;
9014 /* We're done with the while-statement. */
9015 finish_while_stmt (statement);
9023 /* Begin the do-statement. */
9024 statement = begin_do_stmt ();
9025 /* Parse the body of the do-statement. */
9026 parser->in_statement = IN_ITERATION_STMT;
9027 cp_parser_implicitly_scoped_statement (parser, NULL);
9028 parser->in_statement = in_statement;
9029 finish_do_body (statement);
9030 /* Look for the `while' keyword. */
9031 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9032 /* Look for the `('. */
9033 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9034 /* Parse the expression. */
9035 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9036 /* We're done with the do-statement. */
9037 finish_do_stmt (expression, statement);
9038 /* Look for the `)'. */
9039 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9040 /* Look for the `;'. */
9041 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9047 /* Look for the `('. */
9048 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9050 statement = cp_parser_for (parser);
9052 /* Look for the `)'. */
9053 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9055 /* Parse the body of the for-statement. */
9056 parser->in_statement = IN_ITERATION_STMT;
9057 cp_parser_already_scoped_statement (parser);
9058 parser->in_statement = in_statement;
9060 /* We're done with the for-statement. */
9061 finish_for_stmt (statement);
9066 cp_parser_error (parser, "expected iteration-statement");
9067 statement = error_mark_node;
9074 /* Parse a for-init-statement or the declarator of a range-based-for.
9075 Returns true if a range-based-for declaration is seen.
9078 expression-statement
9079 simple-declaration */
9082 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9084 /* If the next token is a `;', then we have an empty
9085 expression-statement. Grammatically, this is also a
9086 simple-declaration, but an invalid one, because it does not
9087 declare anything. Therefore, if we did not handle this case
9088 specially, we would issue an error message about an invalid
9090 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9092 bool is_range_for = false;
9093 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9095 parser->colon_corrects_to_scope_p = false;
9097 /* We're going to speculatively look for a declaration, falling back
9098 to an expression, if necessary. */
9099 cp_parser_parse_tentatively (parser);
9100 /* Parse the declaration. */
9101 cp_parser_simple_declaration (parser,
9102 /*function_definition_allowed_p=*/false,
9104 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9105 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9107 /* It is a range-for, consume the ':' */
9108 cp_lexer_consume_token (parser->lexer);
9109 is_range_for = true;
9110 if (cxx_dialect < cxx0x)
9112 error_at (cp_lexer_peek_token (parser->lexer)->location,
9113 "range-based %<for%> loops are not allowed "
9115 *decl = error_mark_node;
9119 /* The ';' is not consumed yet because we told
9120 cp_parser_simple_declaration not to. */
9121 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9123 if (cp_parser_parse_definitely (parser))
9124 return is_range_for;
9125 /* If the tentative parse failed, then we shall need to look for an
9126 expression-statement. */
9128 /* If we are here, it is an expression-statement. */
9129 cp_parser_expression_statement (parser, NULL_TREE);
9133 /* Parse a jump-statement.
9138 return expression [opt] ;
9139 return braced-init-list ;
9147 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
9150 cp_parser_jump_statement (cp_parser* parser)
9152 tree statement = error_mark_node;
9155 unsigned char in_statement;
9157 /* Peek at the next token. */
9158 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9160 return error_mark_node;
9162 /* See what kind of keyword it is. */
9163 keyword = token->keyword;
9167 in_statement = parser->in_statement & ~IN_IF_STMT;
9168 switch (in_statement)
9171 error_at (token->location, "break statement not within loop or switch");
9174 gcc_assert ((in_statement & IN_SWITCH_STMT)
9175 || in_statement == IN_ITERATION_STMT);
9176 statement = finish_break_stmt ();
9179 error_at (token->location, "invalid exit from OpenMP structured block");
9182 error_at (token->location, "break statement used with OpenMP for loop");
9185 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9189 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9192 error_at (token->location, "continue statement not within a loop");
9194 case IN_ITERATION_STMT:
9196 statement = finish_continue_stmt ();
9199 error_at (token->location, "invalid exit from OpenMP structured block");
9204 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9210 bool expr_non_constant_p;
9212 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9214 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9215 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9217 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9218 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9220 /* If the next token is a `;', then there is no
9223 /* Build the return-statement. */
9224 statement = finish_return_stmt (expr);
9225 /* Look for the final `;'. */
9226 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9231 /* Create the goto-statement. */
9232 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9234 /* Issue a warning about this use of a GNU extension. */
9235 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9236 /* Consume the '*' token. */
9237 cp_lexer_consume_token (parser->lexer);
9238 /* Parse the dependent expression. */
9239 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9242 finish_goto_stmt (cp_parser_identifier (parser));
9243 /* Look for the final `;'. */
9244 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9248 cp_parser_error (parser, "expected jump-statement");
9255 /* Parse a declaration-statement.
9257 declaration-statement:
9258 block-declaration */
9261 cp_parser_declaration_statement (cp_parser* parser)
9265 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9266 p = obstack_alloc (&declarator_obstack, 0);
9268 /* Parse the block-declaration. */
9269 cp_parser_block_declaration (parser, /*statement_p=*/true);
9271 /* Free any declarators allocated. */
9272 obstack_free (&declarator_obstack, p);
9274 /* Finish off the statement. */
9278 /* Some dependent statements (like `if (cond) statement'), are
9279 implicitly in their own scope. In other words, if the statement is
9280 a single statement (as opposed to a compound-statement), it is
9281 none-the-less treated as if it were enclosed in braces. Any
9282 declarations appearing in the dependent statement are out of scope
9283 after control passes that point. This function parses a statement,
9284 but ensures that is in its own scope, even if it is not a
9287 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9288 is a (possibly labeled) if statement which is not enclosed in
9289 braces and has an else clause. This is used to implement
9292 Returns the new statement. */
9295 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9302 /* Mark if () ; with a special NOP_EXPR. */
9303 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9305 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9306 cp_lexer_consume_token (parser->lexer);
9307 statement = add_stmt (build_empty_stmt (loc));
9309 /* if a compound is opened, we simply parse the statement directly. */
9310 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9311 statement = cp_parser_compound_statement (parser, NULL, false, false);
9312 /* If the token is not a `{', then we must take special action. */
9315 /* Create a compound-statement. */
9316 statement = begin_compound_stmt (0);
9317 /* Parse the dependent-statement. */
9318 cp_parser_statement (parser, NULL_TREE, false, if_p);
9319 /* Finish the dummy compound-statement. */
9320 finish_compound_stmt (statement);
9323 /* Return the statement. */
9327 /* For some dependent statements (like `while (cond) statement'), we
9328 have already created a scope. Therefore, even if the dependent
9329 statement is a compound-statement, we do not want to create another
9333 cp_parser_already_scoped_statement (cp_parser* parser)
9335 /* If the token is a `{', then we must take special action. */
9336 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9337 cp_parser_statement (parser, NULL_TREE, false, NULL);
9340 /* Avoid calling cp_parser_compound_statement, so that we
9341 don't create a new scope. Do everything else by hand. */
9342 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9343 /* If the next keyword is `__label__' we have a label declaration. */
9344 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9345 cp_parser_label_declaration (parser);
9346 /* Parse an (optional) statement-seq. */
9347 cp_parser_statement_seq_opt (parser, NULL_TREE);
9348 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9352 /* Declarations [gram.dcl.dcl] */
9354 /* Parse an optional declaration-sequence.
9358 declaration-seq declaration */
9361 cp_parser_declaration_seq_opt (cp_parser* parser)
9367 token = cp_lexer_peek_token (parser->lexer);
9369 if (token->type == CPP_CLOSE_BRACE
9370 || token->type == CPP_EOF
9371 || token->type == CPP_PRAGMA_EOL)
9374 if (token->type == CPP_SEMICOLON)
9376 /* A declaration consisting of a single semicolon is
9377 invalid. Allow it unless we're being pedantic. */
9378 cp_lexer_consume_token (parser->lexer);
9379 if (!in_system_header)
9380 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9384 /* If we're entering or exiting a region that's implicitly
9385 extern "C", modify the lang context appropriately. */
9386 if (!parser->implicit_extern_c && token->implicit_extern_c)
9388 push_lang_context (lang_name_c);
9389 parser->implicit_extern_c = true;
9391 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9393 pop_lang_context ();
9394 parser->implicit_extern_c = false;
9397 if (token->type == CPP_PRAGMA)
9399 /* A top-level declaration can consist solely of a #pragma.
9400 A nested declaration cannot, so this is done here and not
9401 in cp_parser_declaration. (A #pragma at block scope is
9402 handled in cp_parser_statement.) */
9403 cp_parser_pragma (parser, pragma_external);
9407 /* Parse the declaration itself. */
9408 cp_parser_declaration (parser);
9412 /* Parse a declaration.
9417 template-declaration
9418 explicit-instantiation
9419 explicit-specialization
9420 linkage-specification
9421 namespace-definition
9426 __extension__ declaration */
9429 cp_parser_declaration (cp_parser* parser)
9435 tree attributes = NULL_TREE;
9437 /* Check for the `__extension__' keyword. */
9438 if (cp_parser_extension_opt (parser, &saved_pedantic))
9440 /* Parse the qualified declaration. */
9441 cp_parser_declaration (parser);
9442 /* Restore the PEDANTIC flag. */
9443 pedantic = saved_pedantic;
9448 /* Try to figure out what kind of declaration is present. */
9449 token1 = *cp_lexer_peek_token (parser->lexer);
9451 if (token1.type != CPP_EOF)
9452 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9455 token2.type = CPP_EOF;
9456 token2.keyword = RID_MAX;
9459 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9460 p = obstack_alloc (&declarator_obstack, 0);
9462 /* If the next token is `extern' and the following token is a string
9463 literal, then we have a linkage specification. */
9464 if (token1.keyword == RID_EXTERN
9465 && cp_parser_is_string_literal (&token2))
9466 cp_parser_linkage_specification (parser);
9467 /* If the next token is `template', then we have either a template
9468 declaration, an explicit instantiation, or an explicit
9470 else if (token1.keyword == RID_TEMPLATE)
9472 /* `template <>' indicates a template specialization. */
9473 if (token2.type == CPP_LESS
9474 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9475 cp_parser_explicit_specialization (parser);
9476 /* `template <' indicates a template declaration. */
9477 else if (token2.type == CPP_LESS)
9478 cp_parser_template_declaration (parser, /*member_p=*/false);
9479 /* Anything else must be an explicit instantiation. */
9481 cp_parser_explicit_instantiation (parser);
9483 /* If the next token is `export', then we have a template
9485 else if (token1.keyword == RID_EXPORT)
9486 cp_parser_template_declaration (parser, /*member_p=*/false);
9487 /* If the next token is `extern', 'static' or 'inline' and the one
9488 after that is `template', we have a GNU extended explicit
9489 instantiation directive. */
9490 else if (cp_parser_allow_gnu_extensions_p (parser)
9491 && (token1.keyword == RID_EXTERN
9492 || token1.keyword == RID_STATIC
9493 || token1.keyword == RID_INLINE)
9494 && token2.keyword == RID_TEMPLATE)
9495 cp_parser_explicit_instantiation (parser);
9496 /* If the next token is `namespace', check for a named or unnamed
9497 namespace definition. */
9498 else if (token1.keyword == RID_NAMESPACE
9499 && (/* A named namespace definition. */
9500 (token2.type == CPP_NAME
9501 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9503 /* An unnamed namespace definition. */
9504 || token2.type == CPP_OPEN_BRACE
9505 || token2.keyword == RID_ATTRIBUTE))
9506 cp_parser_namespace_definition (parser);
9507 /* An inline (associated) namespace definition. */
9508 else if (token1.keyword == RID_INLINE
9509 && token2.keyword == RID_NAMESPACE)
9510 cp_parser_namespace_definition (parser);
9511 /* Objective-C++ declaration/definition. */
9512 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9513 cp_parser_objc_declaration (parser, NULL_TREE);
9514 else if (c_dialect_objc ()
9515 && token1.keyword == RID_ATTRIBUTE
9516 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9517 cp_parser_objc_declaration (parser, attributes);
9518 /* We must have either a block declaration or a function
9521 /* Try to parse a block-declaration, or a function-definition. */
9522 cp_parser_block_declaration (parser, /*statement_p=*/false);
9524 /* Free any declarators allocated. */
9525 obstack_free (&declarator_obstack, p);
9528 /* Parse a block-declaration.
9533 namespace-alias-definition
9540 __extension__ block-declaration
9545 static_assert-declaration
9547 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9548 part of a declaration-statement. */
9551 cp_parser_block_declaration (cp_parser *parser,
9557 /* Check for the `__extension__' keyword. */
9558 if (cp_parser_extension_opt (parser, &saved_pedantic))
9560 /* Parse the qualified declaration. */
9561 cp_parser_block_declaration (parser, statement_p);
9562 /* Restore the PEDANTIC flag. */
9563 pedantic = saved_pedantic;
9568 /* Peek at the next token to figure out which kind of declaration is
9570 token1 = cp_lexer_peek_token (parser->lexer);
9572 /* If the next keyword is `asm', we have an asm-definition. */
9573 if (token1->keyword == RID_ASM)
9576 cp_parser_commit_to_tentative_parse (parser);
9577 cp_parser_asm_definition (parser);
9579 /* If the next keyword is `namespace', we have a
9580 namespace-alias-definition. */
9581 else if (token1->keyword == RID_NAMESPACE)
9582 cp_parser_namespace_alias_definition (parser);
9583 /* If the next keyword is `using', we have either a
9584 using-declaration or a using-directive. */
9585 else if (token1->keyword == RID_USING)
9590 cp_parser_commit_to_tentative_parse (parser);
9591 /* If the token after `using' is `namespace', then we have a
9593 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9594 if (token2->keyword == RID_NAMESPACE)
9595 cp_parser_using_directive (parser);
9596 /* Otherwise, it's a using-declaration. */
9598 cp_parser_using_declaration (parser,
9599 /*access_declaration_p=*/false);
9601 /* If the next keyword is `__label__' we have a misplaced label
9603 else if (token1->keyword == RID_LABEL)
9605 cp_lexer_consume_token (parser->lexer);
9606 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9607 cp_parser_skip_to_end_of_statement (parser);
9608 /* If the next token is now a `;', consume it. */
9609 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9610 cp_lexer_consume_token (parser->lexer);
9612 /* If the next token is `static_assert' we have a static assertion. */
9613 else if (token1->keyword == RID_STATIC_ASSERT)
9614 cp_parser_static_assert (parser, /*member_p=*/false);
9615 /* Anything else must be a simple-declaration. */
9617 cp_parser_simple_declaration (parser, !statement_p,
9618 /*maybe_range_for_decl*/NULL);
9621 /* Parse a simple-declaration.
9624 decl-specifier-seq [opt] init-declarator-list [opt] ;
9626 init-declarator-list:
9628 init-declarator-list , init-declarator
9630 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9631 function-definition as a simple-declaration.
9633 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9634 parsed declaration if it is an uninitialized single declarator not followed
9635 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9636 if present, will not be consumed. */
9639 cp_parser_simple_declaration (cp_parser* parser,
9640 bool function_definition_allowed_p,
9641 tree *maybe_range_for_decl)
9643 cp_decl_specifier_seq decl_specifiers;
9644 int declares_class_or_enum;
9645 bool saw_declarator;
9647 if (maybe_range_for_decl)
9648 *maybe_range_for_decl = NULL_TREE;
9650 /* Defer access checks until we know what is being declared; the
9651 checks for names appearing in the decl-specifier-seq should be
9652 done as if we were in the scope of the thing being declared. */
9653 push_deferring_access_checks (dk_deferred);
9655 /* Parse the decl-specifier-seq. We have to keep track of whether
9656 or not the decl-specifier-seq declares a named class or
9657 enumeration type, since that is the only case in which the
9658 init-declarator-list is allowed to be empty.
9662 In a simple-declaration, the optional init-declarator-list can be
9663 omitted only when declaring a class or enumeration, that is when
9664 the decl-specifier-seq contains either a class-specifier, an
9665 elaborated-type-specifier, or an enum-specifier. */
9666 cp_parser_decl_specifier_seq (parser,
9667 CP_PARSER_FLAGS_OPTIONAL,
9669 &declares_class_or_enum);
9670 /* We no longer need to defer access checks. */
9671 stop_deferring_access_checks ();
9673 /* In a block scope, a valid declaration must always have a
9674 decl-specifier-seq. By not trying to parse declarators, we can
9675 resolve the declaration/expression ambiguity more quickly. */
9676 if (!function_definition_allowed_p
9677 && !decl_specifiers.any_specifiers_p)
9679 cp_parser_error (parser, "expected declaration");
9683 /* If the next two tokens are both identifiers, the code is
9684 erroneous. The usual cause of this situation is code like:
9688 where "T" should name a type -- but does not. */
9689 if (!decl_specifiers.any_type_specifiers_p
9690 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9692 /* If parsing tentatively, we should commit; we really are
9693 looking at a declaration. */
9694 cp_parser_commit_to_tentative_parse (parser);
9699 /* If we have seen at least one decl-specifier, and the next token
9700 is not a parenthesis, then we must be looking at a declaration.
9701 (After "int (" we might be looking at a functional cast.) */
9702 if (decl_specifiers.any_specifiers_p
9703 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9704 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9705 && !cp_parser_error_occurred (parser))
9706 cp_parser_commit_to_tentative_parse (parser);
9708 /* Keep going until we hit the `;' at the end of the simple
9710 saw_declarator = false;
9711 while (cp_lexer_next_token_is_not (parser->lexer,
9715 bool function_definition_p;
9720 /* If we are processing next declarator, coma is expected */
9721 token = cp_lexer_peek_token (parser->lexer);
9722 gcc_assert (token->type == CPP_COMMA);
9723 cp_lexer_consume_token (parser->lexer);
9724 if (maybe_range_for_decl)
9725 *maybe_range_for_decl = error_mark_node;
9728 saw_declarator = true;
9730 /* Parse the init-declarator. */
9731 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9733 function_definition_allowed_p,
9735 declares_class_or_enum,
9736 &function_definition_p,
9737 maybe_range_for_decl);
9738 /* If an error occurred while parsing tentatively, exit quickly.
9739 (That usually happens when in the body of a function; each
9740 statement is treated as a declaration-statement until proven
9742 if (cp_parser_error_occurred (parser))
9744 /* Handle function definitions specially. */
9745 if (function_definition_p)
9747 /* If the next token is a `,', then we are probably
9748 processing something like:
9752 which is erroneous. */
9753 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9755 cp_token *token = cp_lexer_peek_token (parser->lexer);
9756 error_at (token->location,
9758 " declarations and function-definitions is forbidden");
9760 /* Otherwise, we're done with the list of declarators. */
9763 pop_deferring_access_checks ();
9767 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9768 *maybe_range_for_decl = decl;
9769 /* The next token should be either a `,' or a `;'. */
9770 token = cp_lexer_peek_token (parser->lexer);
9771 /* If it's a `,', there are more declarators to come. */
9772 if (token->type == CPP_COMMA)
9773 /* will be consumed next time around */;
9774 /* If it's a `;', we are done. */
9775 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9777 /* Anything else is an error. */
9780 /* If we have already issued an error message we don't need
9781 to issue another one. */
9782 if (decl != error_mark_node
9783 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9784 cp_parser_error (parser, "expected %<,%> or %<;%>");
9785 /* Skip tokens until we reach the end of the statement. */
9786 cp_parser_skip_to_end_of_statement (parser);
9787 /* If the next token is now a `;', consume it. */
9788 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9789 cp_lexer_consume_token (parser->lexer);
9792 /* After the first time around, a function-definition is not
9793 allowed -- even if it was OK at first. For example:
9798 function_definition_allowed_p = false;
9801 /* Issue an error message if no declarators are present, and the
9802 decl-specifier-seq does not itself declare a class or
9804 if (!saw_declarator)
9806 if (cp_parser_declares_only_class_p (parser))
9807 shadow_tag (&decl_specifiers);
9808 /* Perform any deferred access checks. */
9809 perform_deferred_access_checks ();
9812 /* Consume the `;'. */
9813 if (!maybe_range_for_decl)
9814 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9817 pop_deferring_access_checks ();
9820 /* Parse a decl-specifier-seq.
9823 decl-specifier-seq [opt] decl-specifier
9826 storage-class-specifier
9837 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9839 The parser flags FLAGS is used to control type-specifier parsing.
9841 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9844 1: one of the decl-specifiers is an elaborated-type-specifier
9845 (i.e., a type declaration)
9846 2: one of the decl-specifiers is an enum-specifier or a
9847 class-specifier (i.e., a type definition)
9852 cp_parser_decl_specifier_seq (cp_parser* parser,
9853 cp_parser_flags flags,
9854 cp_decl_specifier_seq *decl_specs,
9855 int* declares_class_or_enum)
9857 bool constructor_possible_p = !parser->in_declarator_p;
9858 cp_token *start_token = NULL;
9860 /* Clear DECL_SPECS. */
9861 clear_decl_specs (decl_specs);
9863 /* Assume no class or enumeration type is declared. */
9864 *declares_class_or_enum = 0;
9866 /* Keep reading specifiers until there are no more to read. */
9870 bool found_decl_spec;
9873 /* Peek at the next token. */
9874 token = cp_lexer_peek_token (parser->lexer);
9876 /* Save the first token of the decl spec list for error
9879 start_token = token;
9880 /* Handle attributes. */
9881 if (token->keyword == RID_ATTRIBUTE)
9883 /* Parse the attributes. */
9884 decl_specs->attributes
9885 = chainon (decl_specs->attributes,
9886 cp_parser_attributes_opt (parser));
9889 /* Assume we will find a decl-specifier keyword. */
9890 found_decl_spec = true;
9891 /* If the next token is an appropriate keyword, we can simply
9892 add it to the list. */
9893 switch (token->keyword)
9899 if (!at_class_scope_p ())
9901 error_at (token->location, "%<friend%> used outside of class");
9902 cp_lexer_purge_token (parser->lexer);
9906 ++decl_specs->specs[(int) ds_friend];
9907 /* Consume the token. */
9908 cp_lexer_consume_token (parser->lexer);
9913 ++decl_specs->specs[(int) ds_constexpr];
9914 cp_lexer_consume_token (parser->lexer);
9917 /* function-specifier:
9924 cp_parser_function_specifier_opt (parser, decl_specs);
9930 ++decl_specs->specs[(int) ds_typedef];
9931 /* Consume the token. */
9932 cp_lexer_consume_token (parser->lexer);
9933 /* A constructor declarator cannot appear in a typedef. */
9934 constructor_possible_p = false;
9935 /* The "typedef" keyword can only occur in a declaration; we
9936 may as well commit at this point. */
9937 cp_parser_commit_to_tentative_parse (parser);
9939 if (decl_specs->storage_class != sc_none)
9940 decl_specs->conflicting_specifiers_p = true;
9943 /* storage-class-specifier:
9953 if (cxx_dialect == cxx98)
9955 /* Consume the token. */
9956 cp_lexer_consume_token (parser->lexer);
9958 /* Complain about `auto' as a storage specifier, if
9959 we're complaining about C++0x compatibility. */
9960 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9961 " will change meaning in C++0x; please remove it");
9963 /* Set the storage class anyway. */
9964 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9968 /* C++0x auto type-specifier. */
9969 found_decl_spec = false;
9976 /* Consume the token. */
9977 cp_lexer_consume_token (parser->lexer);
9978 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9982 /* Consume the token. */
9983 cp_lexer_consume_token (parser->lexer);
9984 ++decl_specs->specs[(int) ds_thread];
9988 /* We did not yet find a decl-specifier yet. */
9989 found_decl_spec = false;
9994 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9995 && token->keyword != RID_CONSTEXPR)
9996 error ("decl-specifier invalid in condition");
9998 /* Constructors are a special case. The `S' in `S()' is not a
9999 decl-specifier; it is the beginning of the declarator. */
10001 = (!found_decl_spec
10002 && constructor_possible_p
10003 && (cp_parser_constructor_declarator_p
10004 (parser, decl_specs->specs[(int) ds_friend] != 0)));
10006 /* If we don't have a DECL_SPEC yet, then we must be looking at
10007 a type-specifier. */
10008 if (!found_decl_spec && !constructor_p)
10010 int decl_spec_declares_class_or_enum;
10011 bool is_cv_qualifier;
10015 = cp_parser_type_specifier (parser, flags,
10017 /*is_declaration=*/true,
10018 &decl_spec_declares_class_or_enum,
10020 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10022 /* If this type-specifier referenced a user-defined type
10023 (a typedef, class-name, etc.), then we can't allow any
10024 more such type-specifiers henceforth.
10028 The longest sequence of decl-specifiers that could
10029 possibly be a type name is taken as the
10030 decl-specifier-seq of a declaration. The sequence shall
10031 be self-consistent as described below.
10035 As a general rule, at most one type-specifier is allowed
10036 in the complete decl-specifier-seq of a declaration. The
10037 only exceptions are the following:
10039 -- const or volatile can be combined with any other
10042 -- signed or unsigned can be combined with char, long,
10050 void g (const int Pc);
10052 Here, Pc is *not* part of the decl-specifier seq; it's
10053 the declarator. Therefore, once we see a type-specifier
10054 (other than a cv-qualifier), we forbid any additional
10055 user-defined types. We *do* still allow things like `int
10056 int' to be considered a decl-specifier-seq, and issue the
10057 error message later. */
10058 if (type_spec && !is_cv_qualifier)
10059 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10060 /* A constructor declarator cannot follow a type-specifier. */
10063 constructor_possible_p = false;
10064 found_decl_spec = true;
10065 if (!is_cv_qualifier)
10066 decl_specs->any_type_specifiers_p = true;
10070 /* If we still do not have a DECL_SPEC, then there are no more
10071 decl-specifiers. */
10072 if (!found_decl_spec)
10075 decl_specs->any_specifiers_p = true;
10076 /* After we see one decl-specifier, further decl-specifiers are
10077 always optional. */
10078 flags |= CP_PARSER_FLAGS_OPTIONAL;
10081 cp_parser_check_decl_spec (decl_specs, start_token->location);
10083 /* Don't allow a friend specifier with a class definition. */
10084 if (decl_specs->specs[(int) ds_friend] != 0
10085 && (*declares_class_or_enum & 2))
10086 error_at (start_token->location,
10087 "class definition may not be declared a friend");
10090 /* Parse an (optional) storage-class-specifier.
10092 storage-class-specifier:
10101 storage-class-specifier:
10104 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
10107 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10109 switch (cp_lexer_peek_token (parser->lexer)->keyword)
10112 if (cxx_dialect != cxx98)
10114 /* Fall through for C++98. */
10121 /* Consume the token. */
10122 return cp_lexer_consume_token (parser->lexer)->u.value;
10129 /* Parse an (optional) function-specifier.
10131 function-specifier:
10136 Returns an IDENTIFIER_NODE corresponding to the keyword used.
10137 Updates DECL_SPECS, if it is non-NULL. */
10140 cp_parser_function_specifier_opt (cp_parser* parser,
10141 cp_decl_specifier_seq *decl_specs)
10143 cp_token *token = cp_lexer_peek_token (parser->lexer);
10144 switch (token->keyword)
10148 ++decl_specs->specs[(int) ds_inline];
10152 /* 14.5.2.3 [temp.mem]
10154 A member function template shall not be virtual. */
10155 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10156 error_at (token->location, "templates may not be %<virtual%>");
10157 else if (decl_specs)
10158 ++decl_specs->specs[(int) ds_virtual];
10163 ++decl_specs->specs[(int) ds_explicit];
10170 /* Consume the token. */
10171 return cp_lexer_consume_token (parser->lexer)->u.value;
10174 /* Parse a linkage-specification.
10176 linkage-specification:
10177 extern string-literal { declaration-seq [opt] }
10178 extern string-literal declaration */
10181 cp_parser_linkage_specification (cp_parser* parser)
10185 /* Look for the `extern' keyword. */
10186 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10188 /* Look for the string-literal. */
10189 linkage = cp_parser_string_literal (parser, false, false);
10191 /* Transform the literal into an identifier. If the literal is a
10192 wide-character string, or contains embedded NULs, then we can't
10193 handle it as the user wants. */
10194 if (strlen (TREE_STRING_POINTER (linkage))
10195 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10197 cp_parser_error (parser, "invalid linkage-specification");
10198 /* Assume C++ linkage. */
10199 linkage = lang_name_cplusplus;
10202 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10204 /* We're now using the new linkage. */
10205 push_lang_context (linkage);
10207 /* If the next token is a `{', then we're using the first
10209 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10211 /* Consume the `{' token. */
10212 cp_lexer_consume_token (parser->lexer);
10213 /* Parse the declarations. */
10214 cp_parser_declaration_seq_opt (parser);
10215 /* Look for the closing `}'. */
10216 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10218 /* Otherwise, there's just one declaration. */
10221 bool saved_in_unbraced_linkage_specification_p;
10223 saved_in_unbraced_linkage_specification_p
10224 = parser->in_unbraced_linkage_specification_p;
10225 parser->in_unbraced_linkage_specification_p = true;
10226 cp_parser_declaration (parser);
10227 parser->in_unbraced_linkage_specification_p
10228 = saved_in_unbraced_linkage_specification_p;
10231 /* We're done with the linkage-specification. */
10232 pop_lang_context ();
10235 /* Parse a static_assert-declaration.
10237 static_assert-declaration:
10238 static_assert ( constant-expression , string-literal ) ;
10240 If MEMBER_P, this static_assert is a class member. */
10243 cp_parser_static_assert(cp_parser *parser, bool member_p)
10248 location_t saved_loc;
10251 /* Peek at the `static_assert' token so we can keep track of exactly
10252 where the static assertion started. */
10253 token = cp_lexer_peek_token (parser->lexer);
10254 saved_loc = token->location;
10256 /* Look for the `static_assert' keyword. */
10257 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10261 /* We know we are in a static assertion; commit to any tentative
10263 if (cp_parser_parsing_tentatively (parser))
10264 cp_parser_commit_to_tentative_parse (parser);
10266 /* Parse the `(' starting the static assertion condition. */
10267 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10269 /* Parse the constant-expression. Allow a non-constant expression
10270 here in order to give better diagnostics in finish_static_assert. */
10272 cp_parser_constant_expression (parser,
10273 /*allow_non_constant_p=*/true,
10274 /*non_constant_p=*/&dummy);
10276 /* Parse the separating `,'. */
10277 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10279 /* Parse the string-literal message. */
10280 message = cp_parser_string_literal (parser,
10281 /*translate=*/false,
10284 /* A `)' completes the static assertion. */
10285 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10286 cp_parser_skip_to_closing_parenthesis (parser,
10287 /*recovering=*/true,
10288 /*or_comma=*/false,
10289 /*consume_paren=*/true);
10291 /* A semicolon terminates the declaration. */
10292 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10294 /* Complete the static assertion, which may mean either processing
10295 the static assert now or saving it for template instantiation. */
10296 finish_static_assert (condition, message, saved_loc, member_p);
10299 /* Parse a `decltype' type. Returns the type.
10301 simple-type-specifier:
10302 decltype ( expression ) */
10305 cp_parser_decltype (cp_parser *parser)
10308 bool id_expression_or_member_access_p = false;
10309 const char *saved_message;
10310 bool saved_integral_constant_expression_p;
10311 bool saved_non_integral_constant_expression_p;
10312 cp_token *id_expr_start_token;
10313 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10315 if (start_token->type == CPP_DECLTYPE)
10317 /* Already parsed. */
10318 cp_lexer_consume_token (parser->lexer);
10319 return start_token->u.value;
10322 /* Look for the `decltype' token. */
10323 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10324 return error_mark_node;
10326 /* Types cannot be defined in a `decltype' expression. Save away the
10328 saved_message = parser->type_definition_forbidden_message;
10330 /* And create the new one. */
10331 parser->type_definition_forbidden_message
10332 = G_("types may not be defined in %<decltype%> expressions");
10334 /* The restrictions on constant-expressions do not apply inside
10335 decltype expressions. */
10336 saved_integral_constant_expression_p
10337 = parser->integral_constant_expression_p;
10338 saved_non_integral_constant_expression_p
10339 = parser->non_integral_constant_expression_p;
10340 parser->integral_constant_expression_p = false;
10342 /* Do not actually evaluate the expression. */
10343 ++cp_unevaluated_operand;
10345 /* Do not warn about problems with the expression. */
10346 ++c_inhibit_evaluation_warnings;
10348 /* Parse the opening `('. */
10349 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10350 return error_mark_node;
10352 /* First, try parsing an id-expression. */
10353 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10354 cp_parser_parse_tentatively (parser);
10355 expr = cp_parser_id_expression (parser,
10356 /*template_keyword_p=*/false,
10357 /*check_dependency_p=*/true,
10358 /*template_p=*/NULL,
10359 /*declarator_p=*/false,
10360 /*optional_p=*/false);
10362 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10364 bool non_integral_constant_expression_p = false;
10365 tree id_expression = expr;
10367 const char *error_msg;
10369 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10370 /* Lookup the name we got back from the id-expression. */
10371 expr = cp_parser_lookup_name (parser, expr,
10373 /*is_template=*/false,
10374 /*is_namespace=*/false,
10375 /*check_dependency=*/true,
10376 /*ambiguous_decls=*/NULL,
10377 id_expr_start_token->location);
10380 && expr != error_mark_node
10381 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10382 && TREE_CODE (expr) != TYPE_DECL
10383 && (TREE_CODE (expr) != BIT_NOT_EXPR
10384 || !TYPE_P (TREE_OPERAND (expr, 0)))
10385 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10387 /* Complete lookup of the id-expression. */
10388 expr = (finish_id_expression
10389 (id_expression, expr, parser->scope, &idk,
10390 /*integral_constant_expression_p=*/false,
10391 /*allow_non_integral_constant_expression_p=*/true,
10392 &non_integral_constant_expression_p,
10393 /*template_p=*/false,
10395 /*address_p=*/false,
10396 /*template_arg_p=*/false,
10398 id_expr_start_token->location));
10400 if (expr == error_mark_node)
10401 /* We found an id-expression, but it was something that we
10402 should not have found. This is an error, not something
10403 we can recover from, so note that we found an
10404 id-expression and we'll recover as gracefully as
10406 id_expression_or_member_access_p = true;
10410 && expr != error_mark_node
10411 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10412 /* We have an id-expression. */
10413 id_expression_or_member_access_p = true;
10416 if (!id_expression_or_member_access_p)
10418 /* Abort the id-expression parse. */
10419 cp_parser_abort_tentative_parse (parser);
10421 /* Parsing tentatively, again. */
10422 cp_parser_parse_tentatively (parser);
10424 /* Parse a class member access. */
10425 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10427 /*member_access_only_p=*/true, NULL);
10430 && expr != error_mark_node
10431 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10432 /* We have an id-expression. */
10433 id_expression_or_member_access_p = true;
10436 if (id_expression_or_member_access_p)
10437 /* We have parsed the complete id-expression or member access. */
10438 cp_parser_parse_definitely (parser);
10441 bool saved_greater_than_is_operator_p;
10443 /* Abort our attempt to parse an id-expression or member access
10445 cp_parser_abort_tentative_parse (parser);
10447 /* Within a parenthesized expression, a `>' token is always
10448 the greater-than operator. */
10449 saved_greater_than_is_operator_p
10450 = parser->greater_than_is_operator_p;
10451 parser->greater_than_is_operator_p = true;
10453 /* Parse a full expression. */
10454 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10456 /* The `>' token might be the end of a template-id or
10457 template-parameter-list now. */
10458 parser->greater_than_is_operator_p
10459 = saved_greater_than_is_operator_p;
10462 /* Go back to evaluating expressions. */
10463 --cp_unevaluated_operand;
10464 --c_inhibit_evaluation_warnings;
10466 /* Restore the old message and the integral constant expression
10468 parser->type_definition_forbidden_message = saved_message;
10469 parser->integral_constant_expression_p
10470 = saved_integral_constant_expression_p;
10471 parser->non_integral_constant_expression_p
10472 = saved_non_integral_constant_expression_p;
10474 /* Parse to the closing `)'. */
10475 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10477 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10478 /*consume_paren=*/true);
10479 return error_mark_node;
10482 expr = finish_decltype_type (expr, id_expression_or_member_access_p,
10483 tf_warning_or_error);
10485 /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
10487 start_token->type = CPP_DECLTYPE;
10488 start_token->u.value = expr;
10489 start_token->keyword = RID_MAX;
10490 cp_lexer_purge_tokens_after (parser->lexer, start_token);
10495 /* Special member functions [gram.special] */
10497 /* Parse a conversion-function-id.
10499 conversion-function-id:
10500 operator conversion-type-id
10502 Returns an IDENTIFIER_NODE representing the operator. */
10505 cp_parser_conversion_function_id (cp_parser* parser)
10509 tree saved_qualifying_scope;
10510 tree saved_object_scope;
10511 tree pushed_scope = NULL_TREE;
10513 /* Look for the `operator' token. */
10514 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10515 return error_mark_node;
10516 /* When we parse the conversion-type-id, the current scope will be
10517 reset. However, we need that information in able to look up the
10518 conversion function later, so we save it here. */
10519 saved_scope = parser->scope;
10520 saved_qualifying_scope = parser->qualifying_scope;
10521 saved_object_scope = parser->object_scope;
10522 /* We must enter the scope of the class so that the names of
10523 entities declared within the class are available in the
10524 conversion-type-id. For example, consider:
10531 S::operator I() { ... }
10533 In order to see that `I' is a type-name in the definition, we
10534 must be in the scope of `S'. */
10536 pushed_scope = push_scope (saved_scope);
10537 /* Parse the conversion-type-id. */
10538 type = cp_parser_conversion_type_id (parser);
10539 /* Leave the scope of the class, if any. */
10541 pop_scope (pushed_scope);
10542 /* Restore the saved scope. */
10543 parser->scope = saved_scope;
10544 parser->qualifying_scope = saved_qualifying_scope;
10545 parser->object_scope = saved_object_scope;
10546 /* If the TYPE is invalid, indicate failure. */
10547 if (type == error_mark_node)
10548 return error_mark_node;
10549 return mangle_conv_op_name_for_type (type);
10552 /* Parse a conversion-type-id:
10554 conversion-type-id:
10555 type-specifier-seq conversion-declarator [opt]
10557 Returns the TYPE specified. */
10560 cp_parser_conversion_type_id (cp_parser* parser)
10563 cp_decl_specifier_seq type_specifiers;
10564 cp_declarator *declarator;
10565 tree type_specified;
10567 /* Parse the attributes. */
10568 attributes = cp_parser_attributes_opt (parser);
10569 /* Parse the type-specifiers. */
10570 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10571 /*is_trailing_return=*/false,
10573 /* If that didn't work, stop. */
10574 if (type_specifiers.type == error_mark_node)
10575 return error_mark_node;
10576 /* Parse the conversion-declarator. */
10577 declarator = cp_parser_conversion_declarator_opt (parser);
10579 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10580 /*initialized=*/0, &attributes);
10582 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10584 /* Don't give this error when parsing tentatively. This happens to
10585 work because we always parse this definitively once. */
10586 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10587 && type_uses_auto (type_specified))
10589 error ("invalid use of %<auto%> in conversion operator");
10590 return error_mark_node;
10593 return type_specified;
10596 /* Parse an (optional) conversion-declarator.
10598 conversion-declarator:
10599 ptr-operator conversion-declarator [opt]
10603 static cp_declarator *
10604 cp_parser_conversion_declarator_opt (cp_parser* parser)
10606 enum tree_code code;
10608 cp_cv_quals cv_quals;
10610 /* We don't know if there's a ptr-operator next, or not. */
10611 cp_parser_parse_tentatively (parser);
10612 /* Try the ptr-operator. */
10613 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10614 /* If it worked, look for more conversion-declarators. */
10615 if (cp_parser_parse_definitely (parser))
10617 cp_declarator *declarator;
10619 /* Parse another optional declarator. */
10620 declarator = cp_parser_conversion_declarator_opt (parser);
10622 return cp_parser_make_indirect_declarator
10623 (code, class_type, cv_quals, declarator);
10629 /* Parse an (optional) ctor-initializer.
10632 : mem-initializer-list
10634 Returns TRUE iff the ctor-initializer was actually present. */
10637 cp_parser_ctor_initializer_opt (cp_parser* parser)
10639 /* If the next token is not a `:', then there is no
10640 ctor-initializer. */
10641 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10643 /* Do default initialization of any bases and members. */
10644 if (DECL_CONSTRUCTOR_P (current_function_decl))
10645 finish_mem_initializers (NULL_TREE);
10650 /* Consume the `:' token. */
10651 cp_lexer_consume_token (parser->lexer);
10652 /* And the mem-initializer-list. */
10653 cp_parser_mem_initializer_list (parser);
10658 /* Parse a mem-initializer-list.
10660 mem-initializer-list:
10661 mem-initializer ... [opt]
10662 mem-initializer ... [opt] , mem-initializer-list */
10665 cp_parser_mem_initializer_list (cp_parser* parser)
10667 tree mem_initializer_list = NULL_TREE;
10668 cp_token *token = cp_lexer_peek_token (parser->lexer);
10670 /* Let the semantic analysis code know that we are starting the
10671 mem-initializer-list. */
10672 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10673 error_at (token->location,
10674 "only constructors take member initializers");
10676 /* Loop through the list. */
10679 tree mem_initializer;
10681 token = cp_lexer_peek_token (parser->lexer);
10682 /* Parse the mem-initializer. */
10683 mem_initializer = cp_parser_mem_initializer (parser);
10684 /* If the next token is a `...', we're expanding member initializers. */
10685 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10687 /* Consume the `...'. */
10688 cp_lexer_consume_token (parser->lexer);
10690 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10691 can be expanded but members cannot. */
10692 if (mem_initializer != error_mark_node
10693 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10695 error_at (token->location,
10696 "cannot expand initializer for member %<%D%>",
10697 TREE_PURPOSE (mem_initializer));
10698 mem_initializer = error_mark_node;
10701 /* Construct the pack expansion type. */
10702 if (mem_initializer != error_mark_node)
10703 mem_initializer = make_pack_expansion (mem_initializer);
10705 /* Add it to the list, unless it was erroneous. */
10706 if (mem_initializer != error_mark_node)
10708 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10709 mem_initializer_list = mem_initializer;
10711 /* If the next token is not a `,', we're done. */
10712 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10714 /* Consume the `,' token. */
10715 cp_lexer_consume_token (parser->lexer);
10718 /* Perform semantic analysis. */
10719 if (DECL_CONSTRUCTOR_P (current_function_decl))
10720 finish_mem_initializers (mem_initializer_list);
10723 /* Parse a mem-initializer.
10726 mem-initializer-id ( expression-list [opt] )
10727 mem-initializer-id braced-init-list
10732 ( expression-list [opt] )
10734 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10735 class) or FIELD_DECL (for a non-static data member) to initialize;
10736 the TREE_VALUE is the expression-list. An empty initialization
10737 list is represented by void_list_node. */
10740 cp_parser_mem_initializer (cp_parser* parser)
10742 tree mem_initializer_id;
10743 tree expression_list;
10745 cp_token *token = cp_lexer_peek_token (parser->lexer);
10747 /* Find out what is being initialized. */
10748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10750 permerror (token->location,
10751 "anachronistic old-style base class initializer");
10752 mem_initializer_id = NULL_TREE;
10756 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10757 if (mem_initializer_id == error_mark_node)
10758 return mem_initializer_id;
10760 member = expand_member_init (mem_initializer_id);
10761 if (member && !DECL_P (member))
10762 in_base_initializer = 1;
10764 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10766 bool expr_non_constant_p;
10767 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10768 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10769 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10770 expression_list = build_tree_list (NULL_TREE, expression_list);
10775 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10777 /*allow_expansion_p=*/true,
10778 /*non_constant_p=*/NULL);
10780 return error_mark_node;
10781 expression_list = build_tree_list_vec (vec);
10782 release_tree_vector (vec);
10785 if (expression_list == error_mark_node)
10786 return error_mark_node;
10787 if (!expression_list)
10788 expression_list = void_type_node;
10790 in_base_initializer = 0;
10792 return member ? build_tree_list (member, expression_list) : error_mark_node;
10795 /* Parse a mem-initializer-id.
10797 mem-initializer-id:
10798 :: [opt] nested-name-specifier [opt] class-name
10801 Returns a TYPE indicating the class to be initializer for the first
10802 production. Returns an IDENTIFIER_NODE indicating the data member
10803 to be initialized for the second production. */
10806 cp_parser_mem_initializer_id (cp_parser* parser)
10808 bool global_scope_p;
10809 bool nested_name_specifier_p;
10810 bool template_p = false;
10813 cp_token *token = cp_lexer_peek_token (parser->lexer);
10815 /* `typename' is not allowed in this context ([temp.res]). */
10816 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10818 error_at (token->location,
10819 "keyword %<typename%> not allowed in this context (a qualified "
10820 "member initializer is implicitly a type)");
10821 cp_lexer_consume_token (parser->lexer);
10823 /* Look for the optional `::' operator. */
10825 = (cp_parser_global_scope_opt (parser,
10826 /*current_scope_valid_p=*/false)
10828 /* Look for the optional nested-name-specifier. The simplest way to
10833 The keyword `typename' is not permitted in a base-specifier or
10834 mem-initializer; in these contexts a qualified name that
10835 depends on a template-parameter is implicitly assumed to be a
10838 is to assume that we have seen the `typename' keyword at this
10840 nested_name_specifier_p
10841 = (cp_parser_nested_name_specifier_opt (parser,
10842 /*typename_keyword_p=*/true,
10843 /*check_dependency_p=*/true,
10845 /*is_declaration=*/true)
10847 if (nested_name_specifier_p)
10848 template_p = cp_parser_optional_template_keyword (parser);
10849 /* If there is a `::' operator or a nested-name-specifier, then we
10850 are definitely looking for a class-name. */
10851 if (global_scope_p || nested_name_specifier_p)
10852 return cp_parser_class_name (parser,
10853 /*typename_keyword_p=*/true,
10854 /*template_keyword_p=*/template_p,
10856 /*check_dependency_p=*/true,
10857 /*class_head_p=*/false,
10858 /*is_declaration=*/true);
10859 /* Otherwise, we could also be looking for an ordinary identifier. */
10860 cp_parser_parse_tentatively (parser);
10861 /* Try a class-name. */
10862 id = cp_parser_class_name (parser,
10863 /*typename_keyword_p=*/true,
10864 /*template_keyword_p=*/false,
10866 /*check_dependency_p=*/true,
10867 /*class_head_p=*/false,
10868 /*is_declaration=*/true);
10869 /* If we found one, we're done. */
10870 if (cp_parser_parse_definitely (parser))
10872 /* Otherwise, look for an ordinary identifier. */
10873 return cp_parser_identifier (parser);
10876 /* Overloading [gram.over] */
10878 /* Parse an operator-function-id.
10880 operator-function-id:
10883 Returns an IDENTIFIER_NODE for the operator which is a
10884 human-readable spelling of the identifier, e.g., `operator +'. */
10887 cp_parser_operator_function_id (cp_parser* parser)
10889 /* Look for the `operator' keyword. */
10890 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10891 return error_mark_node;
10892 /* And then the name of the operator itself. */
10893 return cp_parser_operator (parser);
10896 /* Parse an operator.
10899 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10900 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10901 || ++ -- , ->* -> () []
10908 Returns an IDENTIFIER_NODE for the operator which is a
10909 human-readable spelling of the identifier, e.g., `operator +'. */
10912 cp_parser_operator (cp_parser* parser)
10914 tree id = NULL_TREE;
10917 /* Peek at the next token. */
10918 token = cp_lexer_peek_token (parser->lexer);
10919 /* Figure out which operator we have. */
10920 switch (token->type)
10926 /* The keyword should be either `new' or `delete'. */
10927 if (token->keyword == RID_NEW)
10929 else if (token->keyword == RID_DELETE)
10934 /* Consume the `new' or `delete' token. */
10935 cp_lexer_consume_token (parser->lexer);
10937 /* Peek at the next token. */
10938 token = cp_lexer_peek_token (parser->lexer);
10939 /* If it's a `[' token then this is the array variant of the
10941 if (token->type == CPP_OPEN_SQUARE)
10943 /* Consume the `[' token. */
10944 cp_lexer_consume_token (parser->lexer);
10945 /* Look for the `]' token. */
10946 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10947 id = ansi_opname (op == NEW_EXPR
10948 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10950 /* Otherwise, we have the non-array variant. */
10952 id = ansi_opname (op);
10958 id = ansi_opname (PLUS_EXPR);
10962 id = ansi_opname (MINUS_EXPR);
10966 id = ansi_opname (MULT_EXPR);
10970 id = ansi_opname (TRUNC_DIV_EXPR);
10974 id = ansi_opname (TRUNC_MOD_EXPR);
10978 id = ansi_opname (BIT_XOR_EXPR);
10982 id = ansi_opname (BIT_AND_EXPR);
10986 id = ansi_opname (BIT_IOR_EXPR);
10990 id = ansi_opname (BIT_NOT_EXPR);
10994 id = ansi_opname (TRUTH_NOT_EXPR);
10998 id = ansi_assopname (NOP_EXPR);
11002 id = ansi_opname (LT_EXPR);
11006 id = ansi_opname (GT_EXPR);
11010 id = ansi_assopname (PLUS_EXPR);
11014 id = ansi_assopname (MINUS_EXPR);
11018 id = ansi_assopname (MULT_EXPR);
11022 id = ansi_assopname (TRUNC_DIV_EXPR);
11026 id = ansi_assopname (TRUNC_MOD_EXPR);
11030 id = ansi_assopname (BIT_XOR_EXPR);
11034 id = ansi_assopname (BIT_AND_EXPR);
11038 id = ansi_assopname (BIT_IOR_EXPR);
11042 id = ansi_opname (LSHIFT_EXPR);
11046 id = ansi_opname (RSHIFT_EXPR);
11049 case CPP_LSHIFT_EQ:
11050 id = ansi_assopname (LSHIFT_EXPR);
11053 case CPP_RSHIFT_EQ:
11054 id = ansi_assopname (RSHIFT_EXPR);
11058 id = ansi_opname (EQ_EXPR);
11062 id = ansi_opname (NE_EXPR);
11066 id = ansi_opname (LE_EXPR);
11069 case CPP_GREATER_EQ:
11070 id = ansi_opname (GE_EXPR);
11074 id = ansi_opname (TRUTH_ANDIF_EXPR);
11078 id = ansi_opname (TRUTH_ORIF_EXPR);
11081 case CPP_PLUS_PLUS:
11082 id = ansi_opname (POSTINCREMENT_EXPR);
11085 case CPP_MINUS_MINUS:
11086 id = ansi_opname (PREDECREMENT_EXPR);
11090 id = ansi_opname (COMPOUND_EXPR);
11093 case CPP_DEREF_STAR:
11094 id = ansi_opname (MEMBER_REF);
11098 id = ansi_opname (COMPONENT_REF);
11101 case CPP_OPEN_PAREN:
11102 /* Consume the `('. */
11103 cp_lexer_consume_token (parser->lexer);
11104 /* Look for the matching `)'. */
11105 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11106 return ansi_opname (CALL_EXPR);
11108 case CPP_OPEN_SQUARE:
11109 /* Consume the `['. */
11110 cp_lexer_consume_token (parser->lexer);
11111 /* Look for the matching `]'. */
11112 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11113 return ansi_opname (ARRAY_REF);
11116 /* Anything else is an error. */
11120 /* If we have selected an identifier, we need to consume the
11123 cp_lexer_consume_token (parser->lexer);
11124 /* Otherwise, no valid operator name was present. */
11127 cp_parser_error (parser, "expected operator");
11128 id = error_mark_node;
11134 /* Parse a template-declaration.
11136 template-declaration:
11137 export [opt] template < template-parameter-list > declaration
11139 If MEMBER_P is TRUE, this template-declaration occurs within a
11142 The grammar rule given by the standard isn't correct. What
11143 is really meant is:
11145 template-declaration:
11146 export [opt] template-parameter-list-seq
11147 decl-specifier-seq [opt] init-declarator [opt] ;
11148 export [opt] template-parameter-list-seq
11149 function-definition
11151 template-parameter-list-seq:
11152 template-parameter-list-seq [opt]
11153 template < template-parameter-list > */
11156 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11158 /* Check for `export'. */
11159 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11161 /* Consume the `export' token. */
11162 cp_lexer_consume_token (parser->lexer);
11163 /* Warn that we do not support `export'. */
11164 warning (0, "keyword %<export%> not implemented, and will be ignored");
11167 cp_parser_template_declaration_after_export (parser, member_p);
11170 /* Parse a template-parameter-list.
11172 template-parameter-list:
11174 template-parameter-list , template-parameter
11176 Returns a TREE_LIST. Each node represents a template parameter.
11177 The nodes are connected via their TREE_CHAINs. */
11180 cp_parser_template_parameter_list (cp_parser* parser)
11182 tree parameter_list = NULL_TREE;
11184 begin_template_parm_list ();
11186 /* The loop below parses the template parms. We first need to know
11187 the total number of template parms to be able to compute proper
11188 canonical types of each dependent type. So after the loop, when
11189 we know the total number of template parms,
11190 end_template_parm_list computes the proper canonical types and
11191 fixes up the dependent types accordingly. */
11196 bool is_parameter_pack;
11197 location_t parm_loc;
11199 /* Parse the template-parameter. */
11200 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11201 parameter = cp_parser_template_parameter (parser,
11203 &is_parameter_pack);
11204 /* Add it to the list. */
11205 if (parameter != error_mark_node)
11206 parameter_list = process_template_parm (parameter_list,
11214 tree err_parm = build_tree_list (parameter, parameter);
11215 parameter_list = chainon (parameter_list, err_parm);
11218 /* If the next token is not a `,', we're done. */
11219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11221 /* Otherwise, consume the `,' token. */
11222 cp_lexer_consume_token (parser->lexer);
11225 return end_template_parm_list (parameter_list);
11228 /* Parse a template-parameter.
11230 template-parameter:
11232 parameter-declaration
11234 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11235 the parameter. The TREE_PURPOSE is the default value, if any.
11236 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11237 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11238 set to true iff this parameter is a parameter pack. */
11241 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11242 bool *is_parameter_pack)
11245 cp_parameter_declarator *parameter_declarator;
11246 cp_declarator *id_declarator;
11249 /* Assume it is a type parameter or a template parameter. */
11250 *is_non_type = false;
11251 /* Assume it not a parameter pack. */
11252 *is_parameter_pack = false;
11253 /* Peek at the next token. */
11254 token = cp_lexer_peek_token (parser->lexer);
11255 /* If it is `class' or `template', we have a type-parameter. */
11256 if (token->keyword == RID_TEMPLATE)
11257 return cp_parser_type_parameter (parser, is_parameter_pack);
11258 /* If it is `class' or `typename' we do not know yet whether it is a
11259 type parameter or a non-type parameter. Consider:
11261 template <typename T, typename T::X X> ...
11265 template <class C, class D*> ...
11267 Here, the first parameter is a type parameter, and the second is
11268 a non-type parameter. We can tell by looking at the token after
11269 the identifier -- if it is a `,', `=', or `>' then we have a type
11271 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11273 /* Peek at the token after `class' or `typename'. */
11274 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11275 /* If it's an ellipsis, we have a template type parameter
11277 if (token->type == CPP_ELLIPSIS)
11278 return cp_parser_type_parameter (parser, is_parameter_pack);
11279 /* If it's an identifier, skip it. */
11280 if (token->type == CPP_NAME)
11281 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11282 /* Now, see if the token looks like the end of a template
11284 if (token->type == CPP_COMMA
11285 || token->type == CPP_EQ
11286 || token->type == CPP_GREATER)
11287 return cp_parser_type_parameter (parser, is_parameter_pack);
11290 /* Otherwise, it is a non-type parameter.
11294 When parsing a default template-argument for a non-type
11295 template-parameter, the first non-nested `>' is taken as the end
11296 of the template parameter-list rather than a greater-than
11298 *is_non_type = true;
11299 parameter_declarator
11300 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11301 /*parenthesized_p=*/NULL);
11303 /* If the parameter declaration is marked as a parameter pack, set
11304 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11305 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11307 if (parameter_declarator
11308 && parameter_declarator->declarator
11309 && parameter_declarator->declarator->parameter_pack_p)
11311 *is_parameter_pack = true;
11312 parameter_declarator->declarator->parameter_pack_p = false;
11315 /* If the next token is an ellipsis, and we don't already have it
11316 marked as a parameter pack, then we have a parameter pack (that
11317 has no declarator). */
11318 if (!*is_parameter_pack
11319 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11320 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11322 /* Consume the `...'. */
11323 cp_lexer_consume_token (parser->lexer);
11324 maybe_warn_variadic_templates ();
11326 *is_parameter_pack = true;
11328 /* We might end up with a pack expansion as the type of the non-type
11329 template parameter, in which case this is a non-type template
11331 else if (parameter_declarator
11332 && parameter_declarator->decl_specifiers.type
11333 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11335 *is_parameter_pack = true;
11336 parameter_declarator->decl_specifiers.type =
11337 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11340 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11342 /* Parameter packs cannot have default arguments. However, a
11343 user may try to do so, so we'll parse them and give an
11344 appropriate diagnostic here. */
11346 /* Consume the `='. */
11347 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11348 cp_lexer_consume_token (parser->lexer);
11350 /* Find the name of the parameter pack. */
11351 id_declarator = parameter_declarator->declarator;
11352 while (id_declarator && id_declarator->kind != cdk_id)
11353 id_declarator = id_declarator->declarator;
11355 if (id_declarator && id_declarator->kind == cdk_id)
11356 error_at (start_token->location,
11357 "template parameter pack %qD cannot have a default argument",
11358 id_declarator->u.id.unqualified_name);
11360 error_at (start_token->location,
11361 "template parameter pack cannot have a default argument");
11363 /* Parse the default argument, but throw away the result. */
11364 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11367 parm = grokdeclarator (parameter_declarator->declarator,
11368 ¶meter_declarator->decl_specifiers,
11369 TPARM, /*initialized=*/0,
11370 /*attrlist=*/NULL);
11371 if (parm == error_mark_node)
11372 return error_mark_node;
11374 return build_tree_list (parameter_declarator->default_argument, parm);
11377 /* Parse a type-parameter.
11380 class identifier [opt]
11381 class identifier [opt] = type-id
11382 typename identifier [opt]
11383 typename identifier [opt] = type-id
11384 template < template-parameter-list > class identifier [opt]
11385 template < template-parameter-list > class identifier [opt]
11388 GNU Extension (variadic templates):
11391 class ... identifier [opt]
11392 typename ... identifier [opt]
11394 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11395 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11396 the declaration of the parameter.
11398 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11401 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11406 /* Look for a keyword to tell us what kind of parameter this is. */
11407 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11409 return error_mark_node;
11411 switch (token->keyword)
11417 tree default_argument;
11419 /* If the next token is an ellipsis, we have a template
11421 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11423 /* Consume the `...' token. */
11424 cp_lexer_consume_token (parser->lexer);
11425 maybe_warn_variadic_templates ();
11427 *is_parameter_pack = true;
11430 /* If the next token is an identifier, then it names the
11432 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11433 identifier = cp_parser_identifier (parser);
11435 identifier = NULL_TREE;
11437 /* Create the parameter. */
11438 parameter = finish_template_type_parm (class_type_node, identifier);
11440 /* If the next token is an `=', we have a default argument. */
11441 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11443 /* Consume the `=' token. */
11444 cp_lexer_consume_token (parser->lexer);
11445 /* Parse the default-argument. */
11446 push_deferring_access_checks (dk_no_deferred);
11447 default_argument = cp_parser_type_id (parser);
11449 /* Template parameter packs cannot have default
11451 if (*is_parameter_pack)
11454 error_at (token->location,
11455 "template parameter pack %qD cannot have a "
11456 "default argument", identifier);
11458 error_at (token->location,
11459 "template parameter packs cannot have "
11460 "default arguments");
11461 default_argument = NULL_TREE;
11463 pop_deferring_access_checks ();
11466 default_argument = NULL_TREE;
11468 /* Create the combined representation of the parameter and the
11469 default argument. */
11470 parameter = build_tree_list (default_argument, parameter);
11477 tree default_argument;
11479 /* Look for the `<'. */
11480 cp_parser_require (parser, CPP_LESS, RT_LESS);
11481 /* Parse the template-parameter-list. */
11482 cp_parser_template_parameter_list (parser);
11483 /* Look for the `>'. */
11484 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11485 /* Look for the `class' keyword. */
11486 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11487 /* If the next token is an ellipsis, we have a template
11489 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11491 /* Consume the `...' token. */
11492 cp_lexer_consume_token (parser->lexer);
11493 maybe_warn_variadic_templates ();
11495 *is_parameter_pack = true;
11497 /* If the next token is an `=', then there is a
11498 default-argument. If the next token is a `>', we are at
11499 the end of the parameter-list. If the next token is a `,',
11500 then we are at the end of this parameter. */
11501 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11502 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11503 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11505 identifier = cp_parser_identifier (parser);
11506 /* Treat invalid names as if the parameter were nameless. */
11507 if (identifier == error_mark_node)
11508 identifier = NULL_TREE;
11511 identifier = NULL_TREE;
11513 /* Create the template parameter. */
11514 parameter = finish_template_template_parm (class_type_node,
11517 /* If the next token is an `=', then there is a
11518 default-argument. */
11519 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11523 /* Consume the `='. */
11524 cp_lexer_consume_token (parser->lexer);
11525 /* Parse the id-expression. */
11526 push_deferring_access_checks (dk_no_deferred);
11527 /* save token before parsing the id-expression, for error
11529 token = cp_lexer_peek_token (parser->lexer);
11531 = cp_parser_id_expression (parser,
11532 /*template_keyword_p=*/false,
11533 /*check_dependency_p=*/true,
11534 /*template_p=*/&is_template,
11535 /*declarator_p=*/false,
11536 /*optional_p=*/false);
11537 if (TREE_CODE (default_argument) == TYPE_DECL)
11538 /* If the id-expression was a template-id that refers to
11539 a template-class, we already have the declaration here,
11540 so no further lookup is needed. */
11543 /* Look up the name. */
11545 = cp_parser_lookup_name (parser, default_argument,
11547 /*is_template=*/is_template,
11548 /*is_namespace=*/false,
11549 /*check_dependency=*/true,
11550 /*ambiguous_decls=*/NULL,
11552 /* See if the default argument is valid. */
11554 = check_template_template_default_arg (default_argument);
11556 /* Template parameter packs cannot have default
11558 if (*is_parameter_pack)
11561 error_at (token->location,
11562 "template parameter pack %qD cannot "
11563 "have a default argument",
11566 error_at (token->location, "template parameter packs cannot "
11567 "have default arguments");
11568 default_argument = NULL_TREE;
11570 pop_deferring_access_checks ();
11573 default_argument = NULL_TREE;
11575 /* Create the combined representation of the parameter and the
11576 default argument. */
11577 parameter = build_tree_list (default_argument, parameter);
11582 gcc_unreachable ();
11589 /* Parse a template-id.
11592 template-name < template-argument-list [opt] >
11594 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11595 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11596 returned. Otherwise, if the template-name names a function, or set
11597 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11598 names a class, returns a TYPE_DECL for the specialization.
11600 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11601 uninstantiated templates. */
11604 cp_parser_template_id (cp_parser *parser,
11605 bool template_keyword_p,
11606 bool check_dependency_p,
11607 bool is_declaration)
11613 cp_token_position start_of_id = 0;
11614 deferred_access_check *chk;
11615 VEC (deferred_access_check,gc) *access_check;
11616 cp_token *next_token = NULL, *next_token_2 = NULL;
11617 bool is_identifier;
11619 /* If the next token corresponds to a template-id, there is no need
11621 next_token = cp_lexer_peek_token (parser->lexer);
11622 if (next_token->type == CPP_TEMPLATE_ID)
11624 struct tree_check *check_value;
11626 /* Get the stored value. */
11627 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11628 /* Perform any access checks that were deferred. */
11629 access_check = check_value->checks;
11632 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11633 perform_or_defer_access_check (chk->binfo,
11637 /* Return the stored value. */
11638 return check_value->value;
11641 /* Avoid performing name lookup if there is no possibility of
11642 finding a template-id. */
11643 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11644 || (next_token->type == CPP_NAME
11645 && !cp_parser_nth_token_starts_template_argument_list_p
11648 cp_parser_error (parser, "expected template-id");
11649 return error_mark_node;
11652 /* Remember where the template-id starts. */
11653 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11654 start_of_id = cp_lexer_token_position (parser->lexer, false);
11656 push_deferring_access_checks (dk_deferred);
11658 /* Parse the template-name. */
11659 is_identifier = false;
11660 templ = cp_parser_template_name (parser, template_keyword_p,
11661 check_dependency_p,
11664 if (templ == error_mark_node || is_identifier)
11666 pop_deferring_access_checks ();
11670 /* If we find the sequence `[:' after a template-name, it's probably
11671 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11672 parse correctly the argument list. */
11673 next_token = cp_lexer_peek_token (parser->lexer);
11674 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11675 if (next_token->type == CPP_OPEN_SQUARE
11676 && next_token->flags & DIGRAPH
11677 && next_token_2->type == CPP_COLON
11678 && !(next_token_2->flags & PREV_WHITE))
11680 cp_parser_parse_tentatively (parser);
11681 /* Change `:' into `::'. */
11682 next_token_2->type = CPP_SCOPE;
11683 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11685 cp_lexer_consume_token (parser->lexer);
11687 /* Parse the arguments. */
11688 arguments = cp_parser_enclosed_template_argument_list (parser);
11689 if (!cp_parser_parse_definitely (parser))
11691 /* If we couldn't parse an argument list, then we revert our changes
11692 and return simply an error. Maybe this is not a template-id
11694 next_token_2->type = CPP_COLON;
11695 cp_parser_error (parser, "expected %<<%>");
11696 pop_deferring_access_checks ();
11697 return error_mark_node;
11699 /* Otherwise, emit an error about the invalid digraph, but continue
11700 parsing because we got our argument list. */
11701 if (permerror (next_token->location,
11702 "%<<::%> cannot begin a template-argument list"))
11704 static bool hint = false;
11705 inform (next_token->location,
11706 "%<<:%> is an alternate spelling for %<[%>."
11707 " Insert whitespace between %<<%> and %<::%>");
11708 if (!hint && !flag_permissive)
11710 inform (next_token->location, "(if you use %<-fpermissive%>"
11711 " G++ will accept your code)");
11718 /* Look for the `<' that starts the template-argument-list. */
11719 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11721 pop_deferring_access_checks ();
11722 return error_mark_node;
11724 /* Parse the arguments. */
11725 arguments = cp_parser_enclosed_template_argument_list (parser);
11728 /* Build a representation of the specialization. */
11729 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11730 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11731 else if (DECL_CLASS_TEMPLATE_P (templ)
11732 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11734 bool entering_scope;
11735 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11736 template (rather than some instantiation thereof) only if
11737 is not nested within some other construct. For example, in
11738 "template <typename T> void f(T) { A<T>::", A<T> is just an
11739 instantiation of A. */
11740 entering_scope = (template_parm_scope_p ()
11741 && cp_lexer_next_token_is (parser->lexer,
11744 = finish_template_type (templ, arguments, entering_scope);
11748 /* If it's not a class-template or a template-template, it should be
11749 a function-template. */
11750 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11751 || TREE_CODE (templ) == OVERLOAD
11752 || BASELINK_P (templ)));
11754 template_id = lookup_template_function (templ, arguments);
11757 /* If parsing tentatively, replace the sequence of tokens that makes
11758 up the template-id with a CPP_TEMPLATE_ID token. That way,
11759 should we re-parse the token stream, we will not have to repeat
11760 the effort required to do the parse, nor will we issue duplicate
11761 error messages about problems during instantiation of the
11765 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11767 /* Reset the contents of the START_OF_ID token. */
11768 token->type = CPP_TEMPLATE_ID;
11769 /* Retrieve any deferred checks. Do not pop this access checks yet
11770 so the memory will not be reclaimed during token replacing below. */
11771 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11772 token->u.tree_check_value->value = template_id;
11773 token->u.tree_check_value->checks = get_deferred_access_checks ();
11774 token->keyword = RID_MAX;
11776 /* Purge all subsequent tokens. */
11777 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11779 /* ??? Can we actually assume that, if template_id ==
11780 error_mark_node, we will have issued a diagnostic to the
11781 user, as opposed to simply marking the tentative parse as
11783 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11784 error_at (token->location, "parse error in template argument list");
11787 pop_deferring_access_checks ();
11788 return template_id;
11791 /* Parse a template-name.
11796 The standard should actually say:
11800 operator-function-id
11802 A defect report has been filed about this issue.
11804 A conversion-function-id cannot be a template name because they cannot
11805 be part of a template-id. In fact, looking at this code:
11807 a.operator K<int>()
11809 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11810 It is impossible to call a templated conversion-function-id with an
11811 explicit argument list, since the only allowed template parameter is
11812 the type to which it is converting.
11814 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11815 `template' keyword, in a construction like:
11819 In that case `f' is taken to be a template-name, even though there
11820 is no way of knowing for sure.
11822 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11823 name refers to a set of overloaded functions, at least one of which
11824 is a template, or an IDENTIFIER_NODE with the name of the template,
11825 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11826 names are looked up inside uninstantiated templates. */
11829 cp_parser_template_name (cp_parser* parser,
11830 bool template_keyword_p,
11831 bool check_dependency_p,
11832 bool is_declaration,
11833 bool *is_identifier)
11838 cp_token *token = cp_lexer_peek_token (parser->lexer);
11840 /* If the next token is `operator', then we have either an
11841 operator-function-id or a conversion-function-id. */
11842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11844 /* We don't know whether we're looking at an
11845 operator-function-id or a conversion-function-id. */
11846 cp_parser_parse_tentatively (parser);
11847 /* Try an operator-function-id. */
11848 identifier = cp_parser_operator_function_id (parser);
11849 /* If that didn't work, try a conversion-function-id. */
11850 if (!cp_parser_parse_definitely (parser))
11852 cp_parser_error (parser, "expected template-name");
11853 return error_mark_node;
11856 /* Look for the identifier. */
11858 identifier = cp_parser_identifier (parser);
11860 /* If we didn't find an identifier, we don't have a template-id. */
11861 if (identifier == error_mark_node)
11862 return error_mark_node;
11864 /* If the name immediately followed the `template' keyword, then it
11865 is a template-name. However, if the next token is not `<', then
11866 we do not treat it as a template-name, since it is not being used
11867 as part of a template-id. This enables us to handle constructs
11870 template <typename T> struct S { S(); };
11871 template <typename T> S<T>::S();
11873 correctly. We would treat `S' as a template -- if it were `S<T>'
11874 -- but we do not if there is no `<'. */
11876 if (processing_template_decl
11877 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11879 /* In a declaration, in a dependent context, we pretend that the
11880 "template" keyword was present in order to improve error
11881 recovery. For example, given:
11883 template <typename T> void f(T::X<int>);
11885 we want to treat "X<int>" as a template-id. */
11887 && !template_keyword_p
11888 && parser->scope && TYPE_P (parser->scope)
11889 && check_dependency_p
11890 && dependent_scope_p (parser->scope)
11891 /* Do not do this for dtors (or ctors), since they never
11892 need the template keyword before their name. */
11893 && !constructor_name_p (identifier, parser->scope))
11895 cp_token_position start = 0;
11897 /* Explain what went wrong. */
11898 error_at (token->location, "non-template %qD used as template",
11900 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11901 parser->scope, identifier);
11902 /* If parsing tentatively, find the location of the "<" token. */
11903 if (cp_parser_simulate_error (parser))
11904 start = cp_lexer_token_position (parser->lexer, true);
11905 /* Parse the template arguments so that we can issue error
11906 messages about them. */
11907 cp_lexer_consume_token (parser->lexer);
11908 cp_parser_enclosed_template_argument_list (parser);
11909 /* Skip tokens until we find a good place from which to
11910 continue parsing. */
11911 cp_parser_skip_to_closing_parenthesis (parser,
11912 /*recovering=*/true,
11914 /*consume_paren=*/false);
11915 /* If parsing tentatively, permanently remove the
11916 template argument list. That will prevent duplicate
11917 error messages from being issued about the missing
11918 "template" keyword. */
11920 cp_lexer_purge_tokens_after (parser->lexer, start);
11922 *is_identifier = true;
11926 /* If the "template" keyword is present, then there is generally
11927 no point in doing name-lookup, so we just return IDENTIFIER.
11928 But, if the qualifying scope is non-dependent then we can
11929 (and must) do name-lookup normally. */
11930 if (template_keyword_p
11932 || (TYPE_P (parser->scope)
11933 && dependent_type_p (parser->scope))))
11937 /* Look up the name. */
11938 decl = cp_parser_lookup_name (parser, identifier,
11940 /*is_template=*/true,
11941 /*is_namespace=*/false,
11942 check_dependency_p,
11943 /*ambiguous_decls=*/NULL,
11946 /* If DECL is a template, then the name was a template-name. */
11947 if (TREE_CODE (decl) == TEMPLATE_DECL)
11951 tree fn = NULL_TREE;
11953 /* The standard does not explicitly indicate whether a name that
11954 names a set of overloaded declarations, some of which are
11955 templates, is a template-name. However, such a name should
11956 be a template-name; otherwise, there is no way to form a
11957 template-id for the overloaded templates. */
11958 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11959 if (TREE_CODE (fns) == OVERLOAD)
11960 for (fn = fns; fn; fn = OVL_NEXT (fn))
11961 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11966 /* The name does not name a template. */
11967 cp_parser_error (parser, "expected template-name");
11968 return error_mark_node;
11972 /* If DECL is dependent, and refers to a function, then just return
11973 its name; we will look it up again during template instantiation. */
11974 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11976 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11977 if (TYPE_P (scope) && dependent_type_p (scope))
11984 /* Parse a template-argument-list.
11986 template-argument-list:
11987 template-argument ... [opt]
11988 template-argument-list , template-argument ... [opt]
11990 Returns a TREE_VEC containing the arguments. */
11993 cp_parser_template_argument_list (cp_parser* parser)
11995 tree fixed_args[10];
11996 unsigned n_args = 0;
11997 unsigned alloced = 10;
11998 tree *arg_ary = fixed_args;
12000 bool saved_in_template_argument_list_p;
12002 bool saved_non_ice_p;
12004 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12005 parser->in_template_argument_list_p = true;
12006 /* Even if the template-id appears in an integral
12007 constant-expression, the contents of the argument list do
12009 saved_ice_p = parser->integral_constant_expression_p;
12010 parser->integral_constant_expression_p = false;
12011 saved_non_ice_p = parser->non_integral_constant_expression_p;
12012 parser->non_integral_constant_expression_p = false;
12013 /* Parse the arguments. */
12019 /* Consume the comma. */
12020 cp_lexer_consume_token (parser->lexer);
12022 /* Parse the template-argument. */
12023 argument = cp_parser_template_argument (parser);
12025 /* If the next token is an ellipsis, we're expanding a template
12027 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12029 if (argument == error_mark_node)
12031 cp_token *token = cp_lexer_peek_token (parser->lexer);
12032 error_at (token->location,
12033 "expected parameter pack before %<...%>");
12035 /* Consume the `...' token. */
12036 cp_lexer_consume_token (parser->lexer);
12038 /* Make the argument into a TYPE_PACK_EXPANSION or
12039 EXPR_PACK_EXPANSION. */
12040 argument = make_pack_expansion (argument);
12043 if (n_args == alloced)
12047 if (arg_ary == fixed_args)
12049 arg_ary = XNEWVEC (tree, alloced);
12050 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12053 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12055 arg_ary[n_args++] = argument;
12057 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12059 vec = make_tree_vec (n_args);
12062 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12064 if (arg_ary != fixed_args)
12066 parser->non_integral_constant_expression_p = saved_non_ice_p;
12067 parser->integral_constant_expression_p = saved_ice_p;
12068 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12069 #ifdef ENABLE_CHECKING
12070 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12075 /* Parse a template-argument.
12078 assignment-expression
12082 The representation is that of an assignment-expression, type-id, or
12083 id-expression -- except that the qualified id-expression is
12084 evaluated, so that the value returned is either a DECL or an
12087 Although the standard says "assignment-expression", it forbids
12088 throw-expressions or assignments in the template argument.
12089 Therefore, we use "conditional-expression" instead. */
12092 cp_parser_template_argument (cp_parser* parser)
12097 bool maybe_type_id = false;
12098 cp_token *token = NULL, *argument_start_token = NULL;
12101 /* There's really no way to know what we're looking at, so we just
12102 try each alternative in order.
12106 In a template-argument, an ambiguity between a type-id and an
12107 expression is resolved to a type-id, regardless of the form of
12108 the corresponding template-parameter.
12110 Therefore, we try a type-id first. */
12111 cp_parser_parse_tentatively (parser);
12112 argument = cp_parser_template_type_arg (parser);
12113 /* If there was no error parsing the type-id but the next token is a
12114 '>>', our behavior depends on which dialect of C++ we're
12115 parsing. In C++98, we probably found a typo for '> >'. But there
12116 are type-id which are also valid expressions. For instance:
12118 struct X { int operator >> (int); };
12119 template <int V> struct Foo {};
12122 Here 'X()' is a valid type-id of a function type, but the user just
12123 wanted to write the expression "X() >> 5". Thus, we remember that we
12124 found a valid type-id, but we still try to parse the argument as an
12125 expression to see what happens.
12127 In C++0x, the '>>' will be considered two separate '>'
12129 if (!cp_parser_error_occurred (parser)
12130 && cxx_dialect == cxx98
12131 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12133 maybe_type_id = true;
12134 cp_parser_abort_tentative_parse (parser);
12138 /* If the next token isn't a `,' or a `>', then this argument wasn't
12139 really finished. This means that the argument is not a valid
12141 if (!cp_parser_next_token_ends_template_argument_p (parser))
12142 cp_parser_error (parser, "expected template-argument");
12143 /* If that worked, we're done. */
12144 if (cp_parser_parse_definitely (parser))
12147 /* We're still not sure what the argument will be. */
12148 cp_parser_parse_tentatively (parser);
12149 /* Try a template. */
12150 argument_start_token = cp_lexer_peek_token (parser->lexer);
12151 argument = cp_parser_id_expression (parser,
12152 /*template_keyword_p=*/false,
12153 /*check_dependency_p=*/true,
12155 /*declarator_p=*/false,
12156 /*optional_p=*/false);
12157 /* If the next token isn't a `,' or a `>', then this argument wasn't
12158 really finished. */
12159 if (!cp_parser_next_token_ends_template_argument_p (parser))
12160 cp_parser_error (parser, "expected template-argument");
12161 if (!cp_parser_error_occurred (parser))
12163 /* Figure out what is being referred to. If the id-expression
12164 was for a class template specialization, then we will have a
12165 TYPE_DECL at this point. There is no need to do name lookup
12166 at this point in that case. */
12167 if (TREE_CODE (argument) != TYPE_DECL)
12168 argument = cp_parser_lookup_name (parser, argument,
12170 /*is_template=*/template_p,
12171 /*is_namespace=*/false,
12172 /*check_dependency=*/true,
12173 /*ambiguous_decls=*/NULL,
12174 argument_start_token->location);
12175 if (TREE_CODE (argument) != TEMPLATE_DECL
12176 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12177 cp_parser_error (parser, "expected template-name");
12179 if (cp_parser_parse_definitely (parser))
12181 /* It must be a non-type argument. There permitted cases are given
12182 in [temp.arg.nontype]:
12184 -- an integral constant-expression of integral or enumeration
12187 -- the name of a non-type template-parameter; or
12189 -- the name of an object or function with external linkage...
12191 -- the address of an object or function with external linkage...
12193 -- a pointer to member... */
12194 /* Look for a non-type template parameter. */
12195 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12197 cp_parser_parse_tentatively (parser);
12198 argument = cp_parser_primary_expression (parser,
12199 /*address_p=*/false,
12201 /*template_arg_p=*/true,
12203 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12204 || !cp_parser_next_token_ends_template_argument_p (parser))
12205 cp_parser_simulate_error (parser);
12206 if (cp_parser_parse_definitely (parser))
12210 /* If the next token is "&", the argument must be the address of an
12211 object or function with external linkage. */
12212 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12214 cp_lexer_consume_token (parser->lexer);
12215 /* See if we might have an id-expression. */
12216 token = cp_lexer_peek_token (parser->lexer);
12217 if (token->type == CPP_NAME
12218 || token->keyword == RID_OPERATOR
12219 || token->type == CPP_SCOPE
12220 || token->type == CPP_TEMPLATE_ID
12221 || token->type == CPP_NESTED_NAME_SPECIFIER)
12223 cp_parser_parse_tentatively (parser);
12224 argument = cp_parser_primary_expression (parser,
12227 /*template_arg_p=*/true,
12229 if (cp_parser_error_occurred (parser)
12230 || !cp_parser_next_token_ends_template_argument_p (parser))
12231 cp_parser_abort_tentative_parse (parser);
12236 if (TREE_CODE (argument) == INDIRECT_REF)
12238 gcc_assert (REFERENCE_REF_P (argument));
12239 argument = TREE_OPERAND (argument, 0);
12242 /* If we're in a template, we represent a qualified-id referring
12243 to a static data member as a SCOPE_REF even if the scope isn't
12244 dependent so that we can check access control later. */
12246 if (TREE_CODE (probe) == SCOPE_REF)
12247 probe = TREE_OPERAND (probe, 1);
12248 if (TREE_CODE (probe) == VAR_DECL)
12250 /* A variable without external linkage might still be a
12251 valid constant-expression, so no error is issued here
12252 if the external-linkage check fails. */
12253 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12254 cp_parser_simulate_error (parser);
12256 else if (is_overloaded_fn (argument))
12257 /* All overloaded functions are allowed; if the external
12258 linkage test does not pass, an error will be issued
12262 && (TREE_CODE (argument) == OFFSET_REF
12263 || TREE_CODE (argument) == SCOPE_REF))
12264 /* A pointer-to-member. */
12266 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12269 cp_parser_simulate_error (parser);
12271 if (cp_parser_parse_definitely (parser))
12274 argument = build_x_unary_op (ADDR_EXPR, argument,
12275 tf_warning_or_error);
12280 /* If the argument started with "&", there are no other valid
12281 alternatives at this point. */
12284 cp_parser_error (parser, "invalid non-type template argument");
12285 return error_mark_node;
12288 /* If the argument wasn't successfully parsed as a type-id followed
12289 by '>>', the argument can only be a constant expression now.
12290 Otherwise, we try parsing the constant-expression tentatively,
12291 because the argument could really be a type-id. */
12293 cp_parser_parse_tentatively (parser);
12294 argument = cp_parser_constant_expression (parser,
12295 /*allow_non_constant_p=*/false,
12296 /*non_constant_p=*/NULL);
12297 argument = fold_non_dependent_expr (argument);
12298 if (!maybe_type_id)
12300 if (!cp_parser_next_token_ends_template_argument_p (parser))
12301 cp_parser_error (parser, "expected template-argument");
12302 if (cp_parser_parse_definitely (parser))
12304 /* We did our best to parse the argument as a non type-id, but that
12305 was the only alternative that matched (albeit with a '>' after
12306 it). We can assume it's just a typo from the user, and a
12307 diagnostic will then be issued. */
12308 return cp_parser_template_type_arg (parser);
12311 /* Parse an explicit-instantiation.
12313 explicit-instantiation:
12314 template declaration
12316 Although the standard says `declaration', what it really means is:
12318 explicit-instantiation:
12319 template decl-specifier-seq [opt] declarator [opt] ;
12321 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12322 supposed to be allowed. A defect report has been filed about this
12327 explicit-instantiation:
12328 storage-class-specifier template
12329 decl-specifier-seq [opt] declarator [opt] ;
12330 function-specifier template
12331 decl-specifier-seq [opt] declarator [opt] ; */
12334 cp_parser_explicit_instantiation (cp_parser* parser)
12336 int declares_class_or_enum;
12337 cp_decl_specifier_seq decl_specifiers;
12338 tree extension_specifier = NULL_TREE;
12340 timevar_push (TV_TEMPLATE_INST);
12342 /* Look for an (optional) storage-class-specifier or
12343 function-specifier. */
12344 if (cp_parser_allow_gnu_extensions_p (parser))
12346 extension_specifier
12347 = cp_parser_storage_class_specifier_opt (parser);
12348 if (!extension_specifier)
12349 extension_specifier
12350 = cp_parser_function_specifier_opt (parser,
12351 /*decl_specs=*/NULL);
12354 /* Look for the `template' keyword. */
12355 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12356 /* Let the front end know that we are processing an explicit
12358 begin_explicit_instantiation ();
12359 /* [temp.explicit] says that we are supposed to ignore access
12360 control while processing explicit instantiation directives. */
12361 push_deferring_access_checks (dk_no_check);
12362 /* Parse a decl-specifier-seq. */
12363 cp_parser_decl_specifier_seq (parser,
12364 CP_PARSER_FLAGS_OPTIONAL,
12366 &declares_class_or_enum);
12367 /* If there was exactly one decl-specifier, and it declared a class,
12368 and there's no declarator, then we have an explicit type
12370 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12374 type = check_tag_decl (&decl_specifiers);
12375 /* Turn access control back on for names used during
12376 template instantiation. */
12377 pop_deferring_access_checks ();
12379 do_type_instantiation (type, extension_specifier,
12380 /*complain=*/tf_error);
12384 cp_declarator *declarator;
12387 /* Parse the declarator. */
12389 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12390 /*ctor_dtor_or_conv_p=*/NULL,
12391 /*parenthesized_p=*/NULL,
12392 /*member_p=*/false);
12393 if (declares_class_or_enum & 2)
12394 cp_parser_check_for_definition_in_return_type (declarator,
12395 decl_specifiers.type,
12396 decl_specifiers.type_location);
12397 if (declarator != cp_error_declarator)
12399 if (decl_specifiers.specs[(int)ds_inline])
12400 permerror (input_location, "explicit instantiation shall not use"
12401 " %<inline%> specifier");
12402 if (decl_specifiers.specs[(int)ds_constexpr])
12403 permerror (input_location, "explicit instantiation shall not use"
12404 " %<constexpr%> specifier");
12406 decl = grokdeclarator (declarator, &decl_specifiers,
12407 NORMAL, 0, &decl_specifiers.attributes);
12408 /* Turn access control back on for names used during
12409 template instantiation. */
12410 pop_deferring_access_checks ();
12411 /* Do the explicit instantiation. */
12412 do_decl_instantiation (decl, extension_specifier);
12416 pop_deferring_access_checks ();
12417 /* Skip the body of the explicit instantiation. */
12418 cp_parser_skip_to_end_of_statement (parser);
12421 /* We're done with the instantiation. */
12422 end_explicit_instantiation ();
12424 cp_parser_consume_semicolon_at_end_of_statement (parser);
12426 timevar_pop (TV_TEMPLATE_INST);
12429 /* Parse an explicit-specialization.
12431 explicit-specialization:
12432 template < > declaration
12434 Although the standard says `declaration', what it really means is:
12436 explicit-specialization:
12437 template <> decl-specifier [opt] init-declarator [opt] ;
12438 template <> function-definition
12439 template <> explicit-specialization
12440 template <> template-declaration */
12443 cp_parser_explicit_specialization (cp_parser* parser)
12445 bool need_lang_pop;
12446 cp_token *token = cp_lexer_peek_token (parser->lexer);
12448 /* Look for the `template' keyword. */
12449 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12450 /* Look for the `<'. */
12451 cp_parser_require (parser, CPP_LESS, RT_LESS);
12452 /* Look for the `>'. */
12453 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12454 /* We have processed another parameter list. */
12455 ++parser->num_template_parameter_lists;
12458 A template ... explicit specialization ... shall not have C
12460 if (current_lang_name == lang_name_c)
12462 error_at (token->location, "template specialization with C linkage");
12463 /* Give it C++ linkage to avoid confusing other parts of the
12465 push_lang_context (lang_name_cplusplus);
12466 need_lang_pop = true;
12469 need_lang_pop = false;
12470 /* Let the front end know that we are beginning a specialization. */
12471 if (!begin_specialization ())
12473 end_specialization ();
12477 /* If the next keyword is `template', we need to figure out whether
12478 or not we're looking a template-declaration. */
12479 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12481 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12482 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12483 cp_parser_template_declaration_after_export (parser,
12484 /*member_p=*/false);
12486 cp_parser_explicit_specialization (parser);
12489 /* Parse the dependent declaration. */
12490 cp_parser_single_declaration (parser,
12492 /*member_p=*/false,
12493 /*explicit_specialization_p=*/true,
12494 /*friend_p=*/NULL);
12495 /* We're done with the specialization. */
12496 end_specialization ();
12497 /* For the erroneous case of a template with C linkage, we pushed an
12498 implicit C++ linkage scope; exit that scope now. */
12500 pop_lang_context ();
12501 /* We're done with this parameter list. */
12502 --parser->num_template_parameter_lists;
12505 /* Parse a type-specifier.
12508 simple-type-specifier
12511 elaborated-type-specifier
12519 Returns a representation of the type-specifier. For a
12520 class-specifier, enum-specifier, or elaborated-type-specifier, a
12521 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12523 The parser flags FLAGS is used to control type-specifier parsing.
12525 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12526 in a decl-specifier-seq.
12528 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12529 class-specifier, enum-specifier, or elaborated-type-specifier, then
12530 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12531 if a type is declared; 2 if it is defined. Otherwise, it is set to
12534 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12535 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12536 is set to FALSE. */
12539 cp_parser_type_specifier (cp_parser* parser,
12540 cp_parser_flags flags,
12541 cp_decl_specifier_seq *decl_specs,
12542 bool is_declaration,
12543 int* declares_class_or_enum,
12544 bool* is_cv_qualifier)
12546 tree type_spec = NULL_TREE;
12549 cp_decl_spec ds = ds_last;
12551 /* Assume this type-specifier does not declare a new type. */
12552 if (declares_class_or_enum)
12553 *declares_class_or_enum = 0;
12554 /* And that it does not specify a cv-qualifier. */
12555 if (is_cv_qualifier)
12556 *is_cv_qualifier = false;
12557 /* Peek at the next token. */
12558 token = cp_lexer_peek_token (parser->lexer);
12560 /* If we're looking at a keyword, we can use that to guide the
12561 production we choose. */
12562 keyword = token->keyword;
12566 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12567 goto elaborated_type_specifier;
12569 /* Look for the enum-specifier. */
12570 type_spec = cp_parser_enum_specifier (parser);
12571 /* If that worked, we're done. */
12574 if (declares_class_or_enum)
12575 *declares_class_or_enum = 2;
12577 cp_parser_set_decl_spec_type (decl_specs,
12580 /*type_definition_p=*/true);
12584 goto elaborated_type_specifier;
12586 /* Any of these indicate either a class-specifier, or an
12587 elaborated-type-specifier. */
12591 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12592 goto elaborated_type_specifier;
12594 /* Parse tentatively so that we can back up if we don't find a
12595 class-specifier. */
12596 cp_parser_parse_tentatively (parser);
12597 /* Look for the class-specifier. */
12598 type_spec = cp_parser_class_specifier (parser);
12599 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12600 /* If that worked, we're done. */
12601 if (cp_parser_parse_definitely (parser))
12603 if (declares_class_or_enum)
12604 *declares_class_or_enum = 2;
12606 cp_parser_set_decl_spec_type (decl_specs,
12609 /*type_definition_p=*/true);
12613 /* Fall through. */
12614 elaborated_type_specifier:
12615 /* We're declaring (not defining) a class or enum. */
12616 if (declares_class_or_enum)
12617 *declares_class_or_enum = 1;
12619 /* Fall through. */
12621 /* Look for an elaborated-type-specifier. */
12623 = (cp_parser_elaborated_type_specifier
12625 decl_specs && decl_specs->specs[(int) ds_friend],
12628 cp_parser_set_decl_spec_type (decl_specs,
12631 /*type_definition_p=*/false);
12636 if (is_cv_qualifier)
12637 *is_cv_qualifier = true;
12642 if (is_cv_qualifier)
12643 *is_cv_qualifier = true;
12648 if (is_cv_qualifier)
12649 *is_cv_qualifier = true;
12653 /* The `__complex__' keyword is a GNU extension. */
12661 /* Handle simple keywords. */
12666 ++decl_specs->specs[(int)ds];
12667 decl_specs->any_specifiers_p = true;
12669 return cp_lexer_consume_token (parser->lexer)->u.value;
12672 /* If we do not already have a type-specifier, assume we are looking
12673 at a simple-type-specifier. */
12674 type_spec = cp_parser_simple_type_specifier (parser,
12678 /* If we didn't find a type-specifier, and a type-specifier was not
12679 optional in this context, issue an error message. */
12680 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12682 cp_parser_error (parser, "expected type specifier");
12683 return error_mark_node;
12689 /* Parse a simple-type-specifier.
12691 simple-type-specifier:
12692 :: [opt] nested-name-specifier [opt] type-name
12693 :: [opt] nested-name-specifier template template-id
12708 simple-type-specifier:
12710 decltype ( expression )
12713 __underlying_type ( type-id )
12717 simple-type-specifier:
12719 __typeof__ unary-expression
12720 __typeof__ ( type-id )
12722 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12723 appropriately updated. */
12726 cp_parser_simple_type_specifier (cp_parser* parser,
12727 cp_decl_specifier_seq *decl_specs,
12728 cp_parser_flags flags)
12730 tree type = NULL_TREE;
12733 /* Peek at the next token. */
12734 token = cp_lexer_peek_token (parser->lexer);
12736 /* If we're looking at a keyword, things are easy. */
12737 switch (token->keyword)
12741 decl_specs->explicit_char_p = true;
12742 type = char_type_node;
12745 type = char16_type_node;
12748 type = char32_type_node;
12751 type = wchar_type_node;
12754 type = boolean_type_node;
12758 ++decl_specs->specs[(int) ds_short];
12759 type = short_integer_type_node;
12763 decl_specs->explicit_int_p = true;
12764 type = integer_type_node;
12767 if (!int128_integer_type_node)
12770 decl_specs->explicit_int128_p = true;
12771 type = int128_integer_type_node;
12775 ++decl_specs->specs[(int) ds_long];
12776 type = long_integer_type_node;
12780 ++decl_specs->specs[(int) ds_signed];
12781 type = integer_type_node;
12785 ++decl_specs->specs[(int) ds_unsigned];
12786 type = unsigned_type_node;
12789 type = float_type_node;
12792 type = double_type_node;
12795 type = void_type_node;
12799 maybe_warn_cpp0x (CPP0X_AUTO);
12800 type = make_auto ();
12804 /* Since DR 743, decltype can either be a simple-type-specifier by
12805 itself or begin a nested-name-specifier. Parsing it will replace
12806 it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
12807 handling below decide what to do. */
12808 cp_parser_decltype (parser);
12809 cp_lexer_set_token_position (parser->lexer, token);
12813 /* Consume the `typeof' token. */
12814 cp_lexer_consume_token (parser->lexer);
12815 /* Parse the operand to `typeof'. */
12816 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12817 /* If it is not already a TYPE, take its type. */
12818 if (!TYPE_P (type))
12819 type = finish_typeof (type);
12822 cp_parser_set_decl_spec_type (decl_specs, type,
12824 /*type_definition_p=*/false);
12828 case RID_UNDERLYING_TYPE:
12829 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12832 cp_parser_set_decl_spec_type (decl_specs, type,
12834 /*type_definition_p=*/false);
12842 /* If token is an already-parsed decltype not followed by ::,
12843 it's a simple-type-specifier. */
12844 if (token->type == CPP_DECLTYPE
12845 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
12847 type = token->u.value;
12849 cp_parser_set_decl_spec_type (decl_specs, type,
12851 /*type_definition_p=*/false);
12852 cp_lexer_consume_token (parser->lexer);
12856 /* If the type-specifier was for a built-in type, we're done. */
12859 /* Record the type. */
12861 && (token->keyword != RID_SIGNED
12862 && token->keyword != RID_UNSIGNED
12863 && token->keyword != RID_SHORT
12864 && token->keyword != RID_LONG))
12865 cp_parser_set_decl_spec_type (decl_specs,
12868 /*type_definition_p=*/false);
12870 decl_specs->any_specifiers_p = true;
12872 /* Consume the token. */
12873 cp_lexer_consume_token (parser->lexer);
12875 /* There is no valid C++ program where a non-template type is
12876 followed by a "<". That usually indicates that the user thought
12877 that the type was a template. */
12878 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12880 return TYPE_NAME (type);
12883 /* The type-specifier must be a user-defined type. */
12884 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12889 /* Don't gobble tokens or issue error messages if this is an
12890 optional type-specifier. */
12891 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12892 cp_parser_parse_tentatively (parser);
12894 /* Look for the optional `::' operator. */
12896 = (cp_parser_global_scope_opt (parser,
12897 /*current_scope_valid_p=*/false)
12899 /* Look for the nested-name specifier. */
12901 = (cp_parser_nested_name_specifier_opt (parser,
12902 /*typename_keyword_p=*/false,
12903 /*check_dependency_p=*/true,
12905 /*is_declaration=*/false)
12907 token = cp_lexer_peek_token (parser->lexer);
12908 /* If we have seen a nested-name-specifier, and the next token
12909 is `template', then we are using the template-id production. */
12911 && cp_parser_optional_template_keyword (parser))
12913 /* Look for the template-id. */
12914 type = cp_parser_template_id (parser,
12915 /*template_keyword_p=*/true,
12916 /*check_dependency_p=*/true,
12917 /*is_declaration=*/false);
12918 /* If the template-id did not name a type, we are out of
12920 if (TREE_CODE (type) != TYPE_DECL)
12922 cp_parser_error (parser, "expected template-id for type");
12926 /* Otherwise, look for a type-name. */
12928 type = cp_parser_type_name (parser);
12929 /* Keep track of all name-lookups performed in class scopes. */
12933 && TREE_CODE (type) == TYPE_DECL
12934 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12935 maybe_note_name_used_in_class (DECL_NAME (type), type);
12936 /* If it didn't work out, we don't have a TYPE. */
12937 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12938 && !cp_parser_parse_definitely (parser))
12940 if (type && decl_specs)
12941 cp_parser_set_decl_spec_type (decl_specs, type,
12943 /*type_definition_p=*/false);
12946 /* If we didn't get a type-name, issue an error message. */
12947 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12949 cp_parser_error (parser, "expected type-name");
12950 return error_mark_node;
12953 if (type && type != error_mark_node)
12955 /* See if TYPE is an Objective-C type, and if so, parse and
12956 accept any protocol references following it. Do this before
12957 the cp_parser_check_for_invalid_template_id() call, because
12958 Objective-C types can be followed by '<...>' which would
12959 enclose protocol names rather than template arguments, and so
12960 everything is fine. */
12961 if (c_dialect_objc () && !parser->scope
12962 && (objc_is_id (type) || objc_is_class_name (type)))
12964 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12965 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12967 /* Clobber the "unqualified" type previously entered into
12968 DECL_SPECS with the new, improved protocol-qualified version. */
12970 decl_specs->type = qual_type;
12975 /* There is no valid C++ program where a non-template type is
12976 followed by a "<". That usually indicates that the user
12977 thought that the type was a template. */
12978 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12985 /* Parse a type-name.
12998 Returns a TYPE_DECL for the type. */
13001 cp_parser_type_name (cp_parser* parser)
13005 /* We can't know yet whether it is a class-name or not. */
13006 cp_parser_parse_tentatively (parser);
13007 /* Try a class-name. */
13008 type_decl = cp_parser_class_name (parser,
13009 /*typename_keyword_p=*/false,
13010 /*template_keyword_p=*/false,
13012 /*check_dependency_p=*/true,
13013 /*class_head_p=*/false,
13014 /*is_declaration=*/false);
13015 /* If it's not a class-name, keep looking. */
13016 if (!cp_parser_parse_definitely (parser))
13018 /* It must be a typedef-name or an enum-name. */
13019 return cp_parser_nonclass_name (parser);
13025 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13033 Returns a TYPE_DECL for the type. */
13036 cp_parser_nonclass_name (cp_parser* parser)
13041 cp_token *token = cp_lexer_peek_token (parser->lexer);
13042 identifier = cp_parser_identifier (parser);
13043 if (identifier == error_mark_node)
13044 return error_mark_node;
13046 /* Look up the type-name. */
13047 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13049 if (TREE_CODE (type_decl) != TYPE_DECL
13050 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13052 /* See if this is an Objective-C type. */
13053 tree protos = cp_parser_objc_protocol_refs_opt (parser);
13054 tree type = objc_get_protocol_qualified_type (identifier, protos);
13056 type_decl = TYPE_NAME (type);
13059 /* Issue an error if we did not find a type-name. */
13060 if (TREE_CODE (type_decl) != TYPE_DECL
13061 /* In Objective-C, we have the complication that class names are
13062 normally type names and start declarations (eg, the
13063 "NSObject" in "NSObject *object;"), but can be used in an
13064 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13065 is an expression. So, a classname followed by a dot is not a
13066 valid type-name. */
13067 || (objc_is_class_name (TREE_TYPE (type_decl))
13068 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13070 if (!cp_parser_simulate_error (parser))
13071 cp_parser_name_lookup_error (parser, identifier, type_decl,
13072 NLE_TYPE, token->location);
13073 return error_mark_node;
13075 /* Remember that the name was used in the definition of the
13076 current class so that we can check later to see if the
13077 meaning would have been different after the class was
13078 entirely defined. */
13079 else if (type_decl != error_mark_node
13081 maybe_note_name_used_in_class (identifier, type_decl);
13086 /* Parse an elaborated-type-specifier. Note that the grammar given
13087 here incorporates the resolution to DR68.
13089 elaborated-type-specifier:
13090 class-key :: [opt] nested-name-specifier [opt] identifier
13091 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13092 enum-key :: [opt] nested-name-specifier [opt] identifier
13093 typename :: [opt] nested-name-specifier identifier
13094 typename :: [opt] nested-name-specifier template [opt]
13099 elaborated-type-specifier:
13100 class-key attributes :: [opt] nested-name-specifier [opt] identifier
13101 class-key attributes :: [opt] nested-name-specifier [opt]
13102 template [opt] template-id
13103 enum attributes :: [opt] nested-name-specifier [opt] identifier
13105 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13106 declared `friend'. If IS_DECLARATION is TRUE, then this
13107 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13108 something is being declared.
13110 Returns the TYPE specified. */
13113 cp_parser_elaborated_type_specifier (cp_parser* parser,
13115 bool is_declaration)
13117 enum tag_types tag_type;
13119 tree type = NULL_TREE;
13120 tree attributes = NULL_TREE;
13122 cp_token *token = NULL;
13124 /* See if we're looking at the `enum' keyword. */
13125 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13127 /* Consume the `enum' token. */
13128 cp_lexer_consume_token (parser->lexer);
13129 /* Remember that it's an enumeration type. */
13130 tag_type = enum_type;
13131 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13132 enums) is used here. */
13133 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13134 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13136 pedwarn (input_location, 0, "elaborated-type-specifier "
13137 "for a scoped enum must not use the %<%D%> keyword",
13138 cp_lexer_peek_token (parser->lexer)->u.value);
13139 /* Consume the `struct' or `class' and parse it anyway. */
13140 cp_lexer_consume_token (parser->lexer);
13142 /* Parse the attributes. */
13143 attributes = cp_parser_attributes_opt (parser);
13145 /* Or, it might be `typename'. */
13146 else if (cp_lexer_next_token_is_keyword (parser->lexer,
13149 /* Consume the `typename' token. */
13150 cp_lexer_consume_token (parser->lexer);
13151 /* Remember that it's a `typename' type. */
13152 tag_type = typename_type;
13154 /* Otherwise it must be a class-key. */
13157 tag_type = cp_parser_class_key (parser);
13158 if (tag_type == none_type)
13159 return error_mark_node;
13160 /* Parse the attributes. */
13161 attributes = cp_parser_attributes_opt (parser);
13164 /* Look for the `::' operator. */
13165 globalscope = cp_parser_global_scope_opt (parser,
13166 /*current_scope_valid_p=*/false);
13167 /* Look for the nested-name-specifier. */
13168 if (tag_type == typename_type && !globalscope)
13170 if (!cp_parser_nested_name_specifier (parser,
13171 /*typename_keyword_p=*/true,
13172 /*check_dependency_p=*/true,
13175 return error_mark_node;
13178 /* Even though `typename' is not present, the proposed resolution
13179 to Core Issue 180 says that in `class A<T>::B', `B' should be
13180 considered a type-name, even if `A<T>' is dependent. */
13181 cp_parser_nested_name_specifier_opt (parser,
13182 /*typename_keyword_p=*/true,
13183 /*check_dependency_p=*/true,
13186 /* For everything but enumeration types, consider a template-id.
13187 For an enumeration type, consider only a plain identifier. */
13188 if (tag_type != enum_type)
13190 bool template_p = false;
13193 /* Allow the `template' keyword. */
13194 template_p = cp_parser_optional_template_keyword (parser);
13195 /* If we didn't see `template', we don't know if there's a
13196 template-id or not. */
13198 cp_parser_parse_tentatively (parser);
13199 /* Parse the template-id. */
13200 token = cp_lexer_peek_token (parser->lexer);
13201 decl = cp_parser_template_id (parser, template_p,
13202 /*check_dependency_p=*/true,
13204 /* If we didn't find a template-id, look for an ordinary
13206 if (!template_p && !cp_parser_parse_definitely (parser))
13208 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13209 in effect, then we must assume that, upon instantiation, the
13210 template will correspond to a class. */
13211 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13212 && tag_type == typename_type)
13213 type = make_typename_type (parser->scope, decl,
13215 /*complain=*/tf_error);
13216 /* If the `typename' keyword is in effect and DECL is not a type
13217 decl. Then type is non existant. */
13218 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13221 type = TREE_TYPE (decl);
13226 token = cp_lexer_peek_token (parser->lexer);
13227 identifier = cp_parser_identifier (parser);
13229 if (identifier == error_mark_node)
13231 parser->scope = NULL_TREE;
13232 return error_mark_node;
13235 /* For a `typename', we needn't call xref_tag. */
13236 if (tag_type == typename_type
13237 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13238 return cp_parser_make_typename_type (parser, parser->scope,
13241 /* Look up a qualified name in the usual way. */
13245 tree ambiguous_decls;
13247 decl = cp_parser_lookup_name (parser, identifier,
13249 /*is_template=*/false,
13250 /*is_namespace=*/false,
13251 /*check_dependency=*/true,
13255 /* If the lookup was ambiguous, an error will already have been
13257 if (ambiguous_decls)
13258 return error_mark_node;
13260 /* If we are parsing friend declaration, DECL may be a
13261 TEMPLATE_DECL tree node here. However, we need to check
13262 whether this TEMPLATE_DECL results in valid code. Consider
13263 the following example:
13266 template <class T> class C {};
13269 template <class T> friend class N::C; // #1, valid code
13271 template <class T> class Y {
13272 friend class N::C; // #2, invalid code
13275 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13276 name lookup of `N::C'. We see that friend declaration must
13277 be template for the code to be valid. Note that
13278 processing_template_decl does not work here since it is
13279 always 1 for the above two cases. */
13281 decl = (cp_parser_maybe_treat_template_as_class
13282 (decl, /*tag_name_p=*/is_friend
13283 && parser->num_template_parameter_lists));
13285 if (TREE_CODE (decl) != TYPE_DECL)
13287 cp_parser_diagnose_invalid_type_name (parser,
13291 return error_mark_node;
13294 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13296 bool allow_template = (parser->num_template_parameter_lists
13297 || DECL_SELF_REFERENCE_P (decl));
13298 type = check_elaborated_type_specifier (tag_type, decl,
13301 if (type == error_mark_node)
13302 return error_mark_node;
13305 /* Forward declarations of nested types, such as
13310 are invalid unless all components preceding the final '::'
13311 are complete. If all enclosing types are complete, these
13312 declarations become merely pointless.
13314 Invalid forward declarations of nested types are errors
13315 caught elsewhere in parsing. Those that are pointless arrive
13318 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13319 && !is_friend && !processing_explicit_instantiation)
13320 warning (0, "declaration %qD does not declare anything", decl);
13322 type = TREE_TYPE (decl);
13326 /* An elaborated-type-specifier sometimes introduces a new type and
13327 sometimes names an existing type. Normally, the rule is that it
13328 introduces a new type only if there is not an existing type of
13329 the same name already in scope. For example, given:
13332 void f() { struct S s; }
13334 the `struct S' in the body of `f' is the same `struct S' as in
13335 the global scope; the existing definition is used. However, if
13336 there were no global declaration, this would introduce a new
13337 local class named `S'.
13339 An exception to this rule applies to the following code:
13341 namespace N { struct S; }
13343 Here, the elaborated-type-specifier names a new type
13344 unconditionally; even if there is already an `S' in the
13345 containing scope this declaration names a new type.
13346 This exception only applies if the elaborated-type-specifier
13347 forms the complete declaration:
13351 A declaration consisting solely of `class-key identifier ;' is
13352 either a redeclaration of the name in the current scope or a
13353 forward declaration of the identifier as a class name. It
13354 introduces the name into the current scope.
13356 We are in this situation precisely when the next token is a `;'.
13358 An exception to the exception is that a `friend' declaration does
13359 *not* name a new type; i.e., given:
13361 struct S { friend struct T; };
13363 `T' is not a new type in the scope of `S'.
13365 Also, `new struct S' or `sizeof (struct S)' never results in the
13366 definition of a new type; a new type can only be declared in a
13367 declaration context. */
13373 /* Friends have special name lookup rules. */
13374 ts = ts_within_enclosing_non_class;
13375 else if (is_declaration
13376 && cp_lexer_next_token_is (parser->lexer,
13378 /* This is a `class-key identifier ;' */
13384 (parser->num_template_parameter_lists
13385 && (cp_parser_next_token_starts_class_definition_p (parser)
13386 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13387 /* An unqualified name was used to reference this type, so
13388 there were no qualifying templates. */
13389 if (!cp_parser_check_template_parameters (parser,
13390 /*num_templates=*/0,
13392 /*declarator=*/NULL))
13393 return error_mark_node;
13394 type = xref_tag (tag_type, identifier, ts, template_p);
13398 if (type == error_mark_node)
13399 return error_mark_node;
13401 /* Allow attributes on forward declarations of classes. */
13404 if (TREE_CODE (type) == TYPENAME_TYPE)
13405 warning (OPT_Wattributes,
13406 "attributes ignored on uninstantiated type");
13407 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13408 && ! processing_explicit_instantiation)
13409 warning (OPT_Wattributes,
13410 "attributes ignored on template instantiation");
13411 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13412 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13414 warning (OPT_Wattributes,
13415 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13418 if (tag_type != enum_type)
13419 cp_parser_check_class_key (tag_type, type);
13421 /* A "<" cannot follow an elaborated type specifier. If that
13422 happens, the user was probably trying to form a template-id. */
13423 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13428 /* Parse an enum-specifier.
13431 enum-head { enumerator-list [opt] }
13434 enum-key identifier [opt] enum-base [opt]
13435 enum-key nested-name-specifier identifier enum-base [opt]
13440 enum struct [C++0x]
13443 : type-specifier-seq
13445 opaque-enum-specifier:
13446 enum-key identifier enum-base [opt] ;
13449 enum-key attributes[opt] identifier [opt] enum-base [opt]
13450 { enumerator-list [opt] }attributes[opt]
13452 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13453 if the token stream isn't an enum-specifier after all. */
13456 cp_parser_enum_specifier (cp_parser* parser)
13459 tree type = NULL_TREE;
13461 tree nested_name_specifier = NULL_TREE;
13463 bool scoped_enum_p = false;
13464 bool has_underlying_type = false;
13465 bool nested_being_defined = false;
13466 bool new_value_list = false;
13467 bool is_new_type = false;
13468 bool is_anonymous = false;
13469 tree underlying_type = NULL_TREE;
13470 cp_token *type_start_token = NULL;
13471 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13473 parser->colon_corrects_to_scope_p = false;
13475 /* Parse tentatively so that we can back up if we don't find a
13477 cp_parser_parse_tentatively (parser);
13479 /* Caller guarantees that the current token is 'enum', an identifier
13480 possibly follows, and the token after that is an opening brace.
13481 If we don't have an identifier, fabricate an anonymous name for
13482 the enumeration being defined. */
13483 cp_lexer_consume_token (parser->lexer);
13485 /* Parse the "class" or "struct", which indicates a scoped
13486 enumeration type in C++0x. */
13487 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13488 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13490 if (cxx_dialect < cxx0x)
13491 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13493 /* Consume the `struct' or `class' token. */
13494 cp_lexer_consume_token (parser->lexer);
13496 scoped_enum_p = true;
13499 attributes = cp_parser_attributes_opt (parser);
13501 /* Clear the qualification. */
13502 parser->scope = NULL_TREE;
13503 parser->qualifying_scope = NULL_TREE;
13504 parser->object_scope = NULL_TREE;
13506 /* Figure out in what scope the declaration is being placed. */
13507 prev_scope = current_scope ();
13509 type_start_token = cp_lexer_peek_token (parser->lexer);
13511 push_deferring_access_checks (dk_no_check);
13512 nested_name_specifier
13513 = cp_parser_nested_name_specifier_opt (parser,
13514 /*typename_keyword_p=*/true,
13515 /*check_dependency_p=*/false,
13517 /*is_declaration=*/false);
13519 if (nested_name_specifier)
13523 identifier = cp_parser_identifier (parser);
13524 name = cp_parser_lookup_name (parser, identifier,
13526 /*is_template=*/false,
13527 /*is_namespace=*/false,
13528 /*check_dependency=*/true,
13529 /*ambiguous_decls=*/NULL,
13533 type = TREE_TYPE (name);
13534 if (TREE_CODE (type) == TYPENAME_TYPE)
13536 /* Are template enums allowed in ISO? */
13537 if (template_parm_scope_p ())
13538 pedwarn (type_start_token->location, OPT_pedantic,
13539 "%qD is an enumeration template", name);
13540 /* ignore a typename reference, for it will be solved by name
13546 error_at (type_start_token->location,
13547 "%qD is not an enumerator-name", identifier);
13551 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13552 identifier = cp_parser_identifier (parser);
13555 identifier = make_anon_name ();
13556 is_anonymous = true;
13559 pop_deferring_access_checks ();
13561 /* Check for the `:' that denotes a specified underlying type in C++0x.
13562 Note that a ':' could also indicate a bitfield width, however. */
13563 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13565 cp_decl_specifier_seq type_specifiers;
13567 /* Consume the `:'. */
13568 cp_lexer_consume_token (parser->lexer);
13570 /* Parse the type-specifier-seq. */
13571 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13572 /*is_trailing_return=*/false,
13575 /* At this point this is surely not elaborated type specifier. */
13576 if (!cp_parser_parse_definitely (parser))
13579 if (cxx_dialect < cxx0x)
13580 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13582 has_underlying_type = true;
13584 /* If that didn't work, stop. */
13585 if (type_specifiers.type != error_mark_node)
13587 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13588 /*initialized=*/0, NULL);
13589 if (underlying_type == error_mark_node)
13590 underlying_type = NULL_TREE;
13594 /* Look for the `{' but don't consume it yet. */
13595 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13597 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13599 cp_parser_error (parser, "expected %<{%>");
13600 if (has_underlying_type)
13606 /* An opaque-enum-specifier must have a ';' here. */
13607 if ((scoped_enum_p || underlying_type)
13608 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13610 cp_parser_error (parser, "expected %<;%> or %<{%>");
13611 if (has_underlying_type)
13619 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13622 if (nested_name_specifier)
13624 if (CLASS_TYPE_P (nested_name_specifier))
13626 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13627 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13628 push_scope (nested_name_specifier);
13630 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13632 push_nested_namespace (nested_name_specifier);
13636 /* Issue an error message if type-definitions are forbidden here. */
13637 if (!cp_parser_check_type_definition (parser))
13638 type = error_mark_node;
13640 /* Create the new type. We do this before consuming the opening
13641 brace so the enum will be recorded as being on the line of its
13642 tag (or the 'enum' keyword, if there is no tag). */
13643 type = start_enum (identifier, type, underlying_type,
13644 scoped_enum_p, &is_new_type);
13646 /* If the next token is not '{' it is an opaque-enum-specifier or an
13647 elaborated-type-specifier. */
13648 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13650 timevar_push (TV_PARSE_ENUM);
13651 if (nested_name_specifier)
13653 /* The following catches invalid code such as:
13654 enum class S<int>::E { A, B, C }; */
13655 if (!processing_specialization
13656 && CLASS_TYPE_P (nested_name_specifier)
13657 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13658 error_at (type_start_token->location, "cannot add an enumerator "
13659 "list to a template instantiation");
13661 /* If that scope does not contain the scope in which the
13662 class was originally declared, the program is invalid. */
13663 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13665 if (at_namespace_scope_p ())
13666 error_at (type_start_token->location,
13667 "declaration of %qD in namespace %qD which does not "
13669 type, prev_scope, nested_name_specifier);
13671 error_at (type_start_token->location,
13672 "declaration of %qD in %qD which does not enclose %qD",
13673 type, prev_scope, nested_name_specifier);
13674 type = error_mark_node;
13679 begin_scope (sk_scoped_enum, type);
13681 /* Consume the opening brace. */
13682 cp_lexer_consume_token (parser->lexer);
13684 if (type == error_mark_node)
13685 ; /* Nothing to add */
13686 else if (OPAQUE_ENUM_P (type)
13687 || (cxx_dialect > cxx98 && processing_specialization))
13689 new_value_list = true;
13690 SET_OPAQUE_ENUM_P (type, false);
13691 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13695 error_at (type_start_token->location, "multiple definition of %q#T", type);
13696 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13697 "previous definition here");
13698 type = error_mark_node;
13701 if (type == error_mark_node)
13702 cp_parser_skip_to_end_of_block_or_statement (parser);
13703 /* If the next token is not '}', then there are some enumerators. */
13704 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13705 cp_parser_enumerator_list (parser, type);
13707 /* Consume the final '}'. */
13708 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13712 timevar_pop (TV_PARSE_ENUM);
13716 /* If a ';' follows, then it is an opaque-enum-specifier
13717 and additional restrictions apply. */
13718 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13721 error_at (type_start_token->location,
13722 "opaque-enum-specifier without name");
13723 else if (nested_name_specifier)
13724 error_at (type_start_token->location,
13725 "opaque-enum-specifier must use a simple identifier");
13729 /* Look for trailing attributes to apply to this enumeration, and
13730 apply them if appropriate. */
13731 if (cp_parser_allow_gnu_extensions_p (parser))
13733 tree trailing_attr = cp_parser_attributes_opt (parser);
13734 trailing_attr = chainon (trailing_attr, attributes);
13735 cplus_decl_attributes (&type,
13737 (int) ATTR_FLAG_TYPE_IN_PLACE);
13740 /* Finish up the enumeration. */
13741 if (type != error_mark_node)
13743 if (new_value_list)
13744 finish_enum_value_list (type);
13746 finish_enum (type);
13749 if (nested_name_specifier)
13751 if (CLASS_TYPE_P (nested_name_specifier))
13753 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13754 pop_scope (nested_name_specifier);
13756 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13758 pop_nested_namespace (nested_name_specifier);
13762 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13766 /* Parse an enumerator-list. The enumerators all have the indicated
13770 enumerator-definition
13771 enumerator-list , enumerator-definition */
13774 cp_parser_enumerator_list (cp_parser* parser, tree type)
13778 /* Parse an enumerator-definition. */
13779 cp_parser_enumerator_definition (parser, type);
13781 /* If the next token is not a ',', we've reached the end of
13783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13785 /* Otherwise, consume the `,' and keep going. */
13786 cp_lexer_consume_token (parser->lexer);
13787 /* If the next token is a `}', there is a trailing comma. */
13788 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13790 if (!in_system_header)
13791 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13797 /* Parse an enumerator-definition. The enumerator has the indicated
13800 enumerator-definition:
13802 enumerator = constant-expression
13808 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13814 /* Save the input location because we are interested in the location
13815 of the identifier and not the location of the explicit value. */
13816 loc = cp_lexer_peek_token (parser->lexer)->location;
13818 /* Look for the identifier. */
13819 identifier = cp_parser_identifier (parser);
13820 if (identifier == error_mark_node)
13823 /* If the next token is an '=', then there is an explicit value. */
13824 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13826 /* Consume the `=' token. */
13827 cp_lexer_consume_token (parser->lexer);
13828 /* Parse the value. */
13829 value = cp_parser_constant_expression (parser,
13830 /*allow_non_constant_p=*/false,
13836 /* If we are processing a template, make sure the initializer of the
13837 enumerator doesn't contain any bare template parameter pack. */
13838 if (check_for_bare_parameter_packs (value))
13839 value = error_mark_node;
13841 /* integral_constant_value will pull out this expression, so make sure
13842 it's folded as appropriate. */
13843 value = fold_non_dependent_expr (value);
13845 /* Create the enumerator. */
13846 build_enumerator (identifier, value, type, loc);
13849 /* Parse a namespace-name.
13852 original-namespace-name
13855 Returns the NAMESPACE_DECL for the namespace. */
13858 cp_parser_namespace_name (cp_parser* parser)
13861 tree namespace_decl;
13863 cp_token *token = cp_lexer_peek_token (parser->lexer);
13865 /* Get the name of the namespace. */
13866 identifier = cp_parser_identifier (parser);
13867 if (identifier == error_mark_node)
13868 return error_mark_node;
13870 /* Look up the identifier in the currently active scope. Look only
13871 for namespaces, due to:
13873 [basic.lookup.udir]
13875 When looking up a namespace-name in a using-directive or alias
13876 definition, only namespace names are considered.
13880 [basic.lookup.qual]
13882 During the lookup of a name preceding the :: scope resolution
13883 operator, object, function, and enumerator names are ignored.
13885 (Note that cp_parser_qualifying_entity only calls this
13886 function if the token after the name is the scope resolution
13888 namespace_decl = cp_parser_lookup_name (parser, identifier,
13890 /*is_template=*/false,
13891 /*is_namespace=*/true,
13892 /*check_dependency=*/true,
13893 /*ambiguous_decls=*/NULL,
13895 /* If it's not a namespace, issue an error. */
13896 if (namespace_decl == error_mark_node
13897 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13899 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13900 error_at (token->location, "%qD is not a namespace-name", identifier);
13901 cp_parser_error (parser, "expected namespace-name");
13902 namespace_decl = error_mark_node;
13905 return namespace_decl;
13908 /* Parse a namespace-definition.
13910 namespace-definition:
13911 named-namespace-definition
13912 unnamed-namespace-definition
13914 named-namespace-definition:
13915 original-namespace-definition
13916 extension-namespace-definition
13918 original-namespace-definition:
13919 namespace identifier { namespace-body }
13921 extension-namespace-definition:
13922 namespace original-namespace-name { namespace-body }
13924 unnamed-namespace-definition:
13925 namespace { namespace-body } */
13928 cp_parser_namespace_definition (cp_parser* parser)
13930 tree identifier, attribs;
13931 bool has_visibility;
13934 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13936 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13938 cp_lexer_consume_token (parser->lexer);
13943 /* Look for the `namespace' keyword. */
13944 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13946 /* Get the name of the namespace. We do not attempt to distinguish
13947 between an original-namespace-definition and an
13948 extension-namespace-definition at this point. The semantic
13949 analysis routines are responsible for that. */
13950 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13951 identifier = cp_parser_identifier (parser);
13953 identifier = NULL_TREE;
13955 /* Parse any specified attributes. */
13956 attribs = cp_parser_attributes_opt (parser);
13958 /* Look for the `{' to start the namespace. */
13959 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13960 /* Start the namespace. */
13961 push_namespace (identifier);
13963 /* "inline namespace" is equivalent to a stub namespace definition
13964 followed by a strong using directive. */
13967 tree name_space = current_namespace;
13968 /* Set up namespace association. */
13969 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13970 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13971 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13972 /* Import the contents of the inline namespace. */
13974 do_using_directive (name_space);
13975 push_namespace (identifier);
13978 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13980 /* Parse the body of the namespace. */
13981 cp_parser_namespace_body (parser);
13983 if (has_visibility)
13984 pop_visibility (1);
13986 /* Finish the namespace. */
13988 /* Look for the final `}'. */
13989 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13992 /* Parse a namespace-body.
13995 declaration-seq [opt] */
13998 cp_parser_namespace_body (cp_parser* parser)
14000 cp_parser_declaration_seq_opt (parser);
14003 /* Parse a namespace-alias-definition.
14005 namespace-alias-definition:
14006 namespace identifier = qualified-namespace-specifier ; */
14009 cp_parser_namespace_alias_definition (cp_parser* parser)
14012 tree namespace_specifier;
14014 cp_token *token = cp_lexer_peek_token (parser->lexer);
14016 /* Look for the `namespace' keyword. */
14017 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14018 /* Look for the identifier. */
14019 identifier = cp_parser_identifier (parser);
14020 if (identifier == error_mark_node)
14022 /* Look for the `=' token. */
14023 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14024 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14026 error_at (token->location, "%<namespace%> definition is not allowed here");
14027 /* Skip the definition. */
14028 cp_lexer_consume_token (parser->lexer);
14029 if (cp_parser_skip_to_closing_brace (parser))
14030 cp_lexer_consume_token (parser->lexer);
14033 cp_parser_require (parser, CPP_EQ, RT_EQ);
14034 /* Look for the qualified-namespace-specifier. */
14035 namespace_specifier
14036 = cp_parser_qualified_namespace_specifier (parser);
14037 /* Look for the `;' token. */
14038 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14040 /* Register the alias in the symbol table. */
14041 do_namespace_alias (identifier, namespace_specifier);
14044 /* Parse a qualified-namespace-specifier.
14046 qualified-namespace-specifier:
14047 :: [opt] nested-name-specifier [opt] namespace-name
14049 Returns a NAMESPACE_DECL corresponding to the specified
14053 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14055 /* Look for the optional `::'. */
14056 cp_parser_global_scope_opt (parser,
14057 /*current_scope_valid_p=*/false);
14059 /* Look for the optional nested-name-specifier. */
14060 cp_parser_nested_name_specifier_opt (parser,
14061 /*typename_keyword_p=*/false,
14062 /*check_dependency_p=*/true,
14064 /*is_declaration=*/true);
14066 return cp_parser_namespace_name (parser);
14069 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14070 access declaration.
14073 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14074 using :: unqualified-id ;
14076 access-declaration:
14082 cp_parser_using_declaration (cp_parser* parser,
14083 bool access_declaration_p)
14086 bool typename_p = false;
14087 bool global_scope_p;
14092 if (access_declaration_p)
14093 cp_parser_parse_tentatively (parser);
14096 /* Look for the `using' keyword. */
14097 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14099 /* Peek at the next token. */
14100 token = cp_lexer_peek_token (parser->lexer);
14101 /* See if it's `typename'. */
14102 if (token->keyword == RID_TYPENAME)
14104 /* Remember that we've seen it. */
14106 /* Consume the `typename' token. */
14107 cp_lexer_consume_token (parser->lexer);
14111 /* Look for the optional global scope qualification. */
14113 = (cp_parser_global_scope_opt (parser,
14114 /*current_scope_valid_p=*/false)
14117 /* If we saw `typename', or didn't see `::', then there must be a
14118 nested-name-specifier present. */
14119 if (typename_p || !global_scope_p)
14120 qscope = cp_parser_nested_name_specifier (parser, typename_p,
14121 /*check_dependency_p=*/true,
14123 /*is_declaration=*/true);
14124 /* Otherwise, we could be in either of the two productions. In that
14125 case, treat the nested-name-specifier as optional. */
14127 qscope = cp_parser_nested_name_specifier_opt (parser,
14128 /*typename_keyword_p=*/false,
14129 /*check_dependency_p=*/true,
14131 /*is_declaration=*/true);
14133 qscope = global_namespace;
14135 if (access_declaration_p && cp_parser_error_occurred (parser))
14136 /* Something has already gone wrong; there's no need to parse
14137 further. Since an error has occurred, the return value of
14138 cp_parser_parse_definitely will be false, as required. */
14139 return cp_parser_parse_definitely (parser);
14141 token = cp_lexer_peek_token (parser->lexer);
14142 /* Parse the unqualified-id. */
14143 identifier = cp_parser_unqualified_id (parser,
14144 /*template_keyword_p=*/false,
14145 /*check_dependency_p=*/true,
14146 /*declarator_p=*/true,
14147 /*optional_p=*/false);
14149 if (access_declaration_p)
14151 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14152 cp_parser_simulate_error (parser);
14153 if (!cp_parser_parse_definitely (parser))
14157 /* The function we call to handle a using-declaration is different
14158 depending on what scope we are in. */
14159 if (qscope == error_mark_node || identifier == error_mark_node)
14161 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14162 && TREE_CODE (identifier) != BIT_NOT_EXPR)
14163 /* [namespace.udecl]
14165 A using declaration shall not name a template-id. */
14166 error_at (token->location,
14167 "a template-id may not appear in a using-declaration");
14170 if (at_class_scope_p ())
14172 /* Create the USING_DECL. */
14173 decl = do_class_using_decl (parser->scope, identifier);
14175 if (check_for_bare_parameter_packs (decl))
14178 /* Add it to the list of members in this class. */
14179 finish_member_declaration (decl);
14183 decl = cp_parser_lookup_name_simple (parser,
14186 if (decl == error_mark_node)
14187 cp_parser_name_lookup_error (parser, identifier,
14190 else if (check_for_bare_parameter_packs (decl))
14192 else if (!at_namespace_scope_p ())
14193 do_local_using_decl (decl, qscope, identifier);
14195 do_toplevel_using_decl (decl, qscope, identifier);
14199 /* Look for the final `;'. */
14200 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14205 /* Parse a using-directive.
14208 using namespace :: [opt] nested-name-specifier [opt]
14209 namespace-name ; */
14212 cp_parser_using_directive (cp_parser* parser)
14214 tree namespace_decl;
14217 /* Look for the `using' keyword. */
14218 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14219 /* And the `namespace' keyword. */
14220 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14221 /* Look for the optional `::' operator. */
14222 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14223 /* And the optional nested-name-specifier. */
14224 cp_parser_nested_name_specifier_opt (parser,
14225 /*typename_keyword_p=*/false,
14226 /*check_dependency_p=*/true,
14228 /*is_declaration=*/true);
14229 /* Get the namespace being used. */
14230 namespace_decl = cp_parser_namespace_name (parser);
14231 /* And any specified attributes. */
14232 attribs = cp_parser_attributes_opt (parser);
14233 /* Update the symbol table. */
14234 parse_using_directive (namespace_decl, attribs);
14235 /* Look for the final `;'. */
14236 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14239 /* Parse an asm-definition.
14242 asm ( string-literal ) ;
14247 asm volatile [opt] ( string-literal ) ;
14248 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14249 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14250 : asm-operand-list [opt] ) ;
14251 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14252 : asm-operand-list [opt]
14253 : asm-clobber-list [opt] ) ;
14254 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14255 : asm-clobber-list [opt]
14256 : asm-goto-list ) ; */
14259 cp_parser_asm_definition (cp_parser* parser)
14262 tree outputs = NULL_TREE;
14263 tree inputs = NULL_TREE;
14264 tree clobbers = NULL_TREE;
14265 tree labels = NULL_TREE;
14267 bool volatile_p = false;
14268 bool extended_p = false;
14269 bool invalid_inputs_p = false;
14270 bool invalid_outputs_p = false;
14271 bool goto_p = false;
14272 required_token missing = RT_NONE;
14274 /* Look for the `asm' keyword. */
14275 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14276 /* See if the next token is `volatile'. */
14277 if (cp_parser_allow_gnu_extensions_p (parser)
14278 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14280 /* Remember that we saw the `volatile' keyword. */
14282 /* Consume the token. */
14283 cp_lexer_consume_token (parser->lexer);
14285 if (cp_parser_allow_gnu_extensions_p (parser)
14286 && parser->in_function_body
14287 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14289 /* Remember that we saw the `goto' keyword. */
14291 /* Consume the token. */
14292 cp_lexer_consume_token (parser->lexer);
14294 /* Look for the opening `('. */
14295 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14297 /* Look for the string. */
14298 string = cp_parser_string_literal (parser, false, false);
14299 if (string == error_mark_node)
14301 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14302 /*consume_paren=*/true);
14306 /* If we're allowing GNU extensions, check for the extended assembly
14307 syntax. Unfortunately, the `:' tokens need not be separated by
14308 a space in C, and so, for compatibility, we tolerate that here
14309 too. Doing that means that we have to treat the `::' operator as
14311 if (cp_parser_allow_gnu_extensions_p (parser)
14312 && parser->in_function_body
14313 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14314 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14316 bool inputs_p = false;
14317 bool clobbers_p = false;
14318 bool labels_p = false;
14320 /* The extended syntax was used. */
14323 /* Look for outputs. */
14324 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14326 /* Consume the `:'. */
14327 cp_lexer_consume_token (parser->lexer);
14328 /* Parse the output-operands. */
14329 if (cp_lexer_next_token_is_not (parser->lexer,
14331 && cp_lexer_next_token_is_not (parser->lexer,
14333 && cp_lexer_next_token_is_not (parser->lexer,
14336 outputs = cp_parser_asm_operand_list (parser);
14338 if (outputs == error_mark_node)
14339 invalid_outputs_p = true;
14341 /* If the next token is `::', there are no outputs, and the
14342 next token is the beginning of the inputs. */
14343 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14344 /* The inputs are coming next. */
14347 /* Look for inputs. */
14349 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14351 /* Consume the `:' or `::'. */
14352 cp_lexer_consume_token (parser->lexer);
14353 /* Parse the output-operands. */
14354 if (cp_lexer_next_token_is_not (parser->lexer,
14356 && cp_lexer_next_token_is_not (parser->lexer,
14358 && cp_lexer_next_token_is_not (parser->lexer,
14360 inputs = cp_parser_asm_operand_list (parser);
14362 if (inputs == error_mark_node)
14363 invalid_inputs_p = true;
14365 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14366 /* The clobbers are coming next. */
14369 /* Look for clobbers. */
14371 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14374 /* Consume the `:' or `::'. */
14375 cp_lexer_consume_token (parser->lexer);
14376 /* Parse the clobbers. */
14377 if (cp_lexer_next_token_is_not (parser->lexer,
14379 && cp_lexer_next_token_is_not (parser->lexer,
14381 clobbers = cp_parser_asm_clobber_list (parser);
14384 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14385 /* The labels are coming next. */
14388 /* Look for labels. */
14390 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14393 /* Consume the `:' or `::'. */
14394 cp_lexer_consume_token (parser->lexer);
14395 /* Parse the labels. */
14396 labels = cp_parser_asm_label_list (parser);
14399 if (goto_p && !labels_p)
14400 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14403 missing = RT_COLON_SCOPE;
14405 /* Look for the closing `)'. */
14406 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14407 missing ? missing : RT_CLOSE_PAREN))
14408 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14409 /*consume_paren=*/true);
14410 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14412 if (!invalid_inputs_p && !invalid_outputs_p)
14414 /* Create the ASM_EXPR. */
14415 if (parser->in_function_body)
14417 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14418 inputs, clobbers, labels);
14419 /* If the extended syntax was not used, mark the ASM_EXPR. */
14422 tree temp = asm_stmt;
14423 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14424 temp = TREE_OPERAND (temp, 0);
14426 ASM_INPUT_P (temp) = 1;
14430 cgraph_add_asm_node (string);
14434 /* Declarators [gram.dcl.decl] */
14436 /* Parse an init-declarator.
14439 declarator initializer [opt]
14444 declarator asm-specification [opt] attributes [opt] initializer [opt]
14446 function-definition:
14447 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14449 decl-specifier-seq [opt] declarator function-try-block
14453 function-definition:
14454 __extension__ function-definition
14456 The DECL_SPECIFIERS apply to this declarator. Returns a
14457 representation of the entity declared. If MEMBER_P is TRUE, then
14458 this declarator appears in a class scope. The new DECL created by
14459 this declarator is returned.
14461 The CHECKS are access checks that should be performed once we know
14462 what entity is being declared (and, therefore, what classes have
14465 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14466 for a function-definition here as well. If the declarator is a
14467 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14468 be TRUE upon return. By that point, the function-definition will
14469 have been completely parsed.
14471 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14474 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14475 parsed declaration if it is an uninitialized single declarator not followed
14476 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14477 if present, will not be consumed. If returned, this declarator will be
14478 created with SD_INITIALIZED but will not call cp_finish_decl. */
14481 cp_parser_init_declarator (cp_parser* parser,
14482 cp_decl_specifier_seq *decl_specifiers,
14483 VEC (deferred_access_check,gc)* checks,
14484 bool function_definition_allowed_p,
14486 int declares_class_or_enum,
14487 bool* function_definition_p,
14488 tree* maybe_range_for_decl)
14490 cp_token *token = NULL, *asm_spec_start_token = NULL,
14491 *attributes_start_token = NULL;
14492 cp_declarator *declarator;
14493 tree prefix_attributes;
14495 tree asm_specification;
14497 tree decl = NULL_TREE;
14499 int is_initialized;
14500 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14501 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14503 enum cpp_ttype initialization_kind;
14504 bool is_direct_init = false;
14505 bool is_non_constant_init;
14506 int ctor_dtor_or_conv_p;
14508 tree pushed_scope = NULL_TREE;
14509 bool range_for_decl_p = false;
14511 /* Gather the attributes that were provided with the
14512 decl-specifiers. */
14513 prefix_attributes = decl_specifiers->attributes;
14515 /* Assume that this is not the declarator for a function
14517 if (function_definition_p)
14518 *function_definition_p = false;
14520 /* Defer access checks while parsing the declarator; we cannot know
14521 what names are accessible until we know what is being
14523 resume_deferring_access_checks ();
14525 /* Parse the declarator. */
14526 token = cp_lexer_peek_token (parser->lexer);
14528 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14529 &ctor_dtor_or_conv_p,
14530 /*parenthesized_p=*/NULL,
14532 /* Gather up the deferred checks. */
14533 stop_deferring_access_checks ();
14535 /* If the DECLARATOR was erroneous, there's no need to go
14537 if (declarator == cp_error_declarator)
14538 return error_mark_node;
14540 /* Check that the number of template-parameter-lists is OK. */
14541 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14543 return error_mark_node;
14545 if (declares_class_or_enum & 2)
14546 cp_parser_check_for_definition_in_return_type (declarator,
14547 decl_specifiers->type,
14548 decl_specifiers->type_location);
14550 /* Figure out what scope the entity declared by the DECLARATOR is
14551 located in. `grokdeclarator' sometimes changes the scope, so
14552 we compute it now. */
14553 scope = get_scope_of_declarator (declarator);
14555 /* Perform any lookups in the declared type which were thought to be
14556 dependent, but are not in the scope of the declarator. */
14557 decl_specifiers->type
14558 = maybe_update_decl_type (decl_specifiers->type, scope);
14560 /* If we're allowing GNU extensions, look for an asm-specification
14562 if (cp_parser_allow_gnu_extensions_p (parser))
14564 /* Look for an asm-specification. */
14565 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14566 asm_specification = cp_parser_asm_specification_opt (parser);
14567 /* And attributes. */
14568 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14569 attributes = cp_parser_attributes_opt (parser);
14573 asm_specification = NULL_TREE;
14574 attributes = NULL_TREE;
14577 /* Peek at the next token. */
14578 token = cp_lexer_peek_token (parser->lexer);
14579 /* Check to see if the token indicates the start of a
14580 function-definition. */
14581 if (function_declarator_p (declarator)
14582 && cp_parser_token_starts_function_definition_p (token))
14584 if (!function_definition_allowed_p)
14586 /* If a function-definition should not appear here, issue an
14588 cp_parser_error (parser,
14589 "a function-definition is not allowed here");
14590 return error_mark_node;
14594 location_t func_brace_location
14595 = cp_lexer_peek_token (parser->lexer)->location;
14597 /* Neither attributes nor an asm-specification are allowed
14598 on a function-definition. */
14599 if (asm_specification)
14600 error_at (asm_spec_start_token->location,
14601 "an asm-specification is not allowed "
14602 "on a function-definition");
14604 error_at (attributes_start_token->location,
14605 "attributes are not allowed on a function-definition");
14606 /* This is a function-definition. */
14607 *function_definition_p = true;
14609 /* Parse the function definition. */
14611 decl = cp_parser_save_member_function_body (parser,
14614 prefix_attributes);
14617 = (cp_parser_function_definition_from_specifiers_and_declarator
14618 (parser, decl_specifiers, prefix_attributes, declarator));
14620 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14622 /* This is where the prologue starts... */
14623 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14624 = func_brace_location;
14633 Only in function declarations for constructors, destructors, and
14634 type conversions can the decl-specifier-seq be omitted.
14636 We explicitly postpone this check past the point where we handle
14637 function-definitions because we tolerate function-definitions
14638 that are missing their return types in some modes. */
14639 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14641 cp_parser_error (parser,
14642 "expected constructor, destructor, or type conversion");
14643 return error_mark_node;
14646 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14647 if (token->type == CPP_EQ
14648 || token->type == CPP_OPEN_PAREN
14649 || token->type == CPP_OPEN_BRACE)
14651 is_initialized = SD_INITIALIZED;
14652 initialization_kind = token->type;
14653 if (maybe_range_for_decl)
14654 *maybe_range_for_decl = error_mark_node;
14656 if (token->type == CPP_EQ
14657 && function_declarator_p (declarator))
14659 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14660 if (t2->keyword == RID_DEFAULT)
14661 is_initialized = SD_DEFAULTED;
14662 else if (t2->keyword == RID_DELETE)
14663 is_initialized = SD_DELETED;
14668 /* If the init-declarator isn't initialized and isn't followed by a
14669 `,' or `;', it's not a valid init-declarator. */
14670 if (token->type != CPP_COMMA
14671 && token->type != CPP_SEMICOLON)
14673 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14674 range_for_decl_p = true;
14677 cp_parser_error (parser, "expected initializer");
14678 return error_mark_node;
14681 is_initialized = SD_UNINITIALIZED;
14682 initialization_kind = CPP_EOF;
14685 /* Because start_decl has side-effects, we should only call it if we
14686 know we're going ahead. By this point, we know that we cannot
14687 possibly be looking at any other construct. */
14688 cp_parser_commit_to_tentative_parse (parser);
14690 /* If the decl specifiers were bad, issue an error now that we're
14691 sure this was intended to be a declarator. Then continue
14692 declaring the variable(s), as int, to try to cut down on further
14694 if (decl_specifiers->any_specifiers_p
14695 && decl_specifiers->type == error_mark_node)
14697 cp_parser_error (parser, "invalid type in declaration");
14698 decl_specifiers->type = integer_type_node;
14701 /* Check to see whether or not this declaration is a friend. */
14702 friend_p = cp_parser_friend_p (decl_specifiers);
14704 /* Enter the newly declared entry in the symbol table. If we're
14705 processing a declaration in a class-specifier, we wait until
14706 after processing the initializer. */
14709 if (parser->in_unbraced_linkage_specification_p)
14710 decl_specifiers->storage_class = sc_extern;
14711 decl = start_decl (declarator, decl_specifiers,
14712 range_for_decl_p? SD_INITIALIZED : is_initialized,
14713 attributes, prefix_attributes,
14715 /* Adjust location of decl if declarator->id_loc is more appropriate:
14716 set, and decl wasn't merged with another decl, in which case its
14717 location would be different from input_location, and more accurate. */
14719 && declarator->id_loc != UNKNOWN_LOCATION
14720 && DECL_SOURCE_LOCATION (decl) == input_location)
14721 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14724 /* Enter the SCOPE. That way unqualified names appearing in the
14725 initializer will be looked up in SCOPE. */
14726 pushed_scope = push_scope (scope);
14728 /* Perform deferred access control checks, now that we know in which
14729 SCOPE the declared entity resides. */
14730 if (!member_p && decl)
14732 tree saved_current_function_decl = NULL_TREE;
14734 /* If the entity being declared is a function, pretend that we
14735 are in its scope. If it is a `friend', it may have access to
14736 things that would not otherwise be accessible. */
14737 if (TREE_CODE (decl) == FUNCTION_DECL)
14739 saved_current_function_decl = current_function_decl;
14740 current_function_decl = decl;
14743 /* Perform access checks for template parameters. */
14744 cp_parser_perform_template_parameter_access_checks (checks);
14746 /* Perform the access control checks for the declarator and the
14747 decl-specifiers. */
14748 perform_deferred_access_checks ();
14750 /* Restore the saved value. */
14751 if (TREE_CODE (decl) == FUNCTION_DECL)
14752 current_function_decl = saved_current_function_decl;
14755 /* Parse the initializer. */
14756 initializer = NULL_TREE;
14757 is_direct_init = false;
14758 is_non_constant_init = true;
14759 if (is_initialized)
14761 if (function_declarator_p (declarator))
14763 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14764 if (initialization_kind == CPP_EQ)
14765 initializer = cp_parser_pure_specifier (parser);
14768 /* If the declaration was erroneous, we don't really
14769 know what the user intended, so just silently
14770 consume the initializer. */
14771 if (decl != error_mark_node)
14772 error_at (initializer_start_token->location,
14773 "initializer provided for function");
14774 cp_parser_skip_to_closing_parenthesis (parser,
14775 /*recovering=*/true,
14776 /*or_comma=*/false,
14777 /*consume_paren=*/true);
14782 /* We want to record the extra mangling scope for in-class
14783 initializers of class members and initializers of static data
14784 member templates. The former is a C++0x feature which isn't
14785 implemented yet, and I expect it will involve deferring
14786 parsing of the initializer until end of class as with default
14787 arguments. So right here we only handle the latter. */
14788 if (!member_p && processing_template_decl)
14789 start_lambda_scope (decl);
14790 initializer = cp_parser_initializer (parser,
14792 &is_non_constant_init);
14793 if (!member_p && processing_template_decl)
14794 finish_lambda_scope ();
14798 /* The old parser allows attributes to appear after a parenthesized
14799 initializer. Mark Mitchell proposed removing this functionality
14800 on the GCC mailing lists on 2002-08-13. This parser accepts the
14801 attributes -- but ignores them. */
14802 if (cp_parser_allow_gnu_extensions_p (parser)
14803 && initialization_kind == CPP_OPEN_PAREN)
14804 if (cp_parser_attributes_opt (parser))
14805 warning (OPT_Wattributes,
14806 "attributes after parenthesized initializer ignored");
14808 /* For an in-class declaration, use `grokfield' to create the
14814 pop_scope (pushed_scope);
14815 pushed_scope = NULL_TREE;
14817 decl = grokfield (declarator, decl_specifiers,
14818 initializer, !is_non_constant_init,
14819 /*asmspec=*/NULL_TREE,
14820 prefix_attributes);
14821 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14822 cp_parser_save_default_args (parser, decl);
14825 /* Finish processing the declaration. But, skip member
14827 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14829 cp_finish_decl (decl,
14830 initializer, !is_non_constant_init,
14832 /* If the initializer is in parentheses, then this is
14833 a direct-initialization, which means that an
14834 `explicit' constructor is OK. Otherwise, an
14835 `explicit' constructor cannot be used. */
14836 ((is_direct_init || !is_initialized)
14837 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14839 else if ((cxx_dialect != cxx98) && friend_p
14840 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14841 /* Core issue #226 (C++0x only): A default template-argument
14842 shall not be specified in a friend class template
14844 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14845 /*is_partial=*/0, /*is_friend_decl=*/1);
14847 if (!friend_p && pushed_scope)
14848 pop_scope (pushed_scope);
14853 /* Parse a declarator.
14857 ptr-operator declarator
14859 abstract-declarator:
14860 ptr-operator abstract-declarator [opt]
14861 direct-abstract-declarator
14866 attributes [opt] direct-declarator
14867 attributes [opt] ptr-operator declarator
14869 abstract-declarator:
14870 attributes [opt] ptr-operator abstract-declarator [opt]
14871 attributes [opt] direct-abstract-declarator
14873 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14874 detect constructor, destructor or conversion operators. It is set
14875 to -1 if the declarator is a name, and +1 if it is a
14876 function. Otherwise it is set to zero. Usually you just want to
14877 test for >0, but internally the negative value is used.
14879 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14880 a decl-specifier-seq unless it declares a constructor, destructor,
14881 or conversion. It might seem that we could check this condition in
14882 semantic analysis, rather than parsing, but that makes it difficult
14883 to handle something like `f()'. We want to notice that there are
14884 no decl-specifiers, and therefore realize that this is an
14885 expression, not a declaration.)
14887 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14888 the declarator is a direct-declarator of the form "(...)".
14890 MEMBER_P is true iff this declarator is a member-declarator. */
14892 static cp_declarator *
14893 cp_parser_declarator (cp_parser* parser,
14894 cp_parser_declarator_kind dcl_kind,
14895 int* ctor_dtor_or_conv_p,
14896 bool* parenthesized_p,
14899 cp_declarator *declarator;
14900 enum tree_code code;
14901 cp_cv_quals cv_quals;
14903 tree attributes = NULL_TREE;
14905 /* Assume this is not a constructor, destructor, or type-conversion
14907 if (ctor_dtor_or_conv_p)
14908 *ctor_dtor_or_conv_p = 0;
14910 if (cp_parser_allow_gnu_extensions_p (parser))
14911 attributes = cp_parser_attributes_opt (parser);
14913 /* Check for the ptr-operator production. */
14914 cp_parser_parse_tentatively (parser);
14915 /* Parse the ptr-operator. */
14916 code = cp_parser_ptr_operator (parser,
14919 /* If that worked, then we have a ptr-operator. */
14920 if (cp_parser_parse_definitely (parser))
14922 /* If a ptr-operator was found, then this declarator was not
14924 if (parenthesized_p)
14925 *parenthesized_p = true;
14926 /* The dependent declarator is optional if we are parsing an
14927 abstract-declarator. */
14928 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14929 cp_parser_parse_tentatively (parser);
14931 /* Parse the dependent declarator. */
14932 declarator = cp_parser_declarator (parser, dcl_kind,
14933 /*ctor_dtor_or_conv_p=*/NULL,
14934 /*parenthesized_p=*/NULL,
14935 /*member_p=*/false);
14937 /* If we are parsing an abstract-declarator, we must handle the
14938 case where the dependent declarator is absent. */
14939 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14940 && !cp_parser_parse_definitely (parser))
14943 declarator = cp_parser_make_indirect_declarator
14944 (code, class_type, cv_quals, declarator);
14946 /* Everything else is a direct-declarator. */
14949 if (parenthesized_p)
14950 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14952 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14953 ctor_dtor_or_conv_p,
14957 if (attributes && declarator && declarator != cp_error_declarator)
14958 declarator->attributes = attributes;
14963 /* Parse a direct-declarator or direct-abstract-declarator.
14967 direct-declarator ( parameter-declaration-clause )
14968 cv-qualifier-seq [opt]
14969 exception-specification [opt]
14970 direct-declarator [ constant-expression [opt] ]
14973 direct-abstract-declarator:
14974 direct-abstract-declarator [opt]
14975 ( parameter-declaration-clause )
14976 cv-qualifier-seq [opt]
14977 exception-specification [opt]
14978 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14979 ( abstract-declarator )
14981 Returns a representation of the declarator. DCL_KIND is
14982 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14983 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14984 we are parsing a direct-declarator. It is
14985 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14986 of ambiguity we prefer an abstract declarator, as per
14987 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14988 cp_parser_declarator. */
14990 static cp_declarator *
14991 cp_parser_direct_declarator (cp_parser* parser,
14992 cp_parser_declarator_kind dcl_kind,
14993 int* ctor_dtor_or_conv_p,
14997 cp_declarator *declarator = NULL;
14998 tree scope = NULL_TREE;
14999 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15000 bool saved_in_declarator_p = parser->in_declarator_p;
15002 tree pushed_scope = NULL_TREE;
15006 /* Peek at the next token. */
15007 token = cp_lexer_peek_token (parser->lexer);
15008 if (token->type == CPP_OPEN_PAREN)
15010 /* This is either a parameter-declaration-clause, or a
15011 parenthesized declarator. When we know we are parsing a
15012 named declarator, it must be a parenthesized declarator
15013 if FIRST is true. For instance, `(int)' is a
15014 parameter-declaration-clause, with an omitted
15015 direct-abstract-declarator. But `((*))', is a
15016 parenthesized abstract declarator. Finally, when T is a
15017 template parameter `(T)' is a
15018 parameter-declaration-clause, and not a parenthesized
15021 We first try and parse a parameter-declaration-clause,
15022 and then try a nested declarator (if FIRST is true).
15024 It is not an error for it not to be a
15025 parameter-declaration-clause, even when FIRST is
15031 The first is the declaration of a function while the
15032 second is the definition of a variable, including its
15035 Having seen only the parenthesis, we cannot know which of
15036 these two alternatives should be selected. Even more
15037 complex are examples like:
15042 The former is a function-declaration; the latter is a
15043 variable initialization.
15045 Thus again, we try a parameter-declaration-clause, and if
15046 that fails, we back out and return. */
15048 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15051 unsigned saved_num_template_parameter_lists;
15052 bool is_declarator = false;
15055 /* In a member-declarator, the only valid interpretation
15056 of a parenthesis is the start of a
15057 parameter-declaration-clause. (It is invalid to
15058 initialize a static data member with a parenthesized
15059 initializer; only the "=" form of initialization is
15062 cp_parser_parse_tentatively (parser);
15064 /* Consume the `('. */
15065 cp_lexer_consume_token (parser->lexer);
15068 /* If this is going to be an abstract declarator, we're
15069 in a declarator and we can't have default args. */
15070 parser->default_arg_ok_p = false;
15071 parser->in_declarator_p = true;
15074 /* Inside the function parameter list, surrounding
15075 template-parameter-lists do not apply. */
15076 saved_num_template_parameter_lists
15077 = parser->num_template_parameter_lists;
15078 parser->num_template_parameter_lists = 0;
15080 begin_scope (sk_function_parms, NULL_TREE);
15082 /* Parse the parameter-declaration-clause. */
15083 params = cp_parser_parameter_declaration_clause (parser);
15085 parser->num_template_parameter_lists
15086 = saved_num_template_parameter_lists;
15088 /* Consume the `)'. */
15089 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15091 /* If all went well, parse the cv-qualifier-seq and the
15092 exception-specification. */
15093 if (member_p || cp_parser_parse_definitely (parser))
15095 cp_cv_quals cv_quals;
15096 cp_virt_specifiers virt_specifiers;
15097 tree exception_specification;
15100 is_declarator = true;
15102 if (ctor_dtor_or_conv_p)
15103 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15106 /* Parse the cv-qualifier-seq. */
15107 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15108 /* And the exception-specification. */
15109 exception_specification
15110 = cp_parser_exception_specification_opt (parser);
15111 /* Parse the virt-specifier-seq. */
15112 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15114 late_return = (cp_parser_late_return_type_opt
15115 (parser, member_p ? cv_quals : -1));
15117 /* Create the function-declarator. */
15118 declarator = make_call_declarator (declarator,
15122 exception_specification,
15124 /* Any subsequent parameter lists are to do with
15125 return type, so are not those of the declared
15127 parser->default_arg_ok_p = false;
15130 /* Remove the function parms from scope. */
15131 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15132 pop_binding (DECL_NAME (t), t);
15136 /* Repeat the main loop. */
15140 /* If this is the first, we can try a parenthesized
15144 bool saved_in_type_id_in_expr_p;
15146 parser->default_arg_ok_p = saved_default_arg_ok_p;
15147 parser->in_declarator_p = saved_in_declarator_p;
15149 /* Consume the `('. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Parse the nested declarator. */
15152 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15153 parser->in_type_id_in_expr_p = true;
15155 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15156 /*parenthesized_p=*/NULL,
15158 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15160 /* Expect a `)'. */
15161 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15162 declarator = cp_error_declarator;
15163 if (declarator == cp_error_declarator)
15166 goto handle_declarator;
15168 /* Otherwise, we must be done. */
15172 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15173 && token->type == CPP_OPEN_SQUARE)
15175 /* Parse an array-declarator. */
15178 if (ctor_dtor_or_conv_p)
15179 *ctor_dtor_or_conv_p = 0;
15182 parser->default_arg_ok_p = false;
15183 parser->in_declarator_p = true;
15184 /* Consume the `['. */
15185 cp_lexer_consume_token (parser->lexer);
15186 /* Peek at the next token. */
15187 token = cp_lexer_peek_token (parser->lexer);
15188 /* If the next token is `]', then there is no
15189 constant-expression. */
15190 if (token->type != CPP_CLOSE_SQUARE)
15192 bool non_constant_p;
15195 = cp_parser_constant_expression (parser,
15196 /*allow_non_constant=*/true,
15198 if (!non_constant_p)
15200 /* Normally, the array bound must be an integral constant
15201 expression. However, as an extension, we allow VLAs
15202 in function scopes as long as they aren't part of a
15203 parameter declaration. */
15204 else if (!parser->in_function_body
15205 || current_binding_level->kind == sk_function_parms)
15207 cp_parser_error (parser,
15208 "array bound is not an integer constant");
15209 bounds = error_mark_node;
15211 else if (processing_template_decl && !error_operand_p (bounds))
15213 /* Remember this wasn't a constant-expression. */
15214 bounds = build_nop (TREE_TYPE (bounds), bounds);
15215 TREE_SIDE_EFFECTS (bounds) = 1;
15219 bounds = NULL_TREE;
15220 /* Look for the closing `]'. */
15221 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15223 declarator = cp_error_declarator;
15227 declarator = make_array_declarator (declarator, bounds);
15229 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15232 tree qualifying_scope;
15233 tree unqualified_name;
15234 special_function_kind sfk;
15236 bool pack_expansion_p = false;
15237 cp_token *declarator_id_start_token;
15239 /* Parse a declarator-id */
15240 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15243 cp_parser_parse_tentatively (parser);
15245 /* If we see an ellipsis, we should be looking at a
15247 if (token->type == CPP_ELLIPSIS)
15249 /* Consume the `...' */
15250 cp_lexer_consume_token (parser->lexer);
15252 pack_expansion_p = true;
15256 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15258 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15259 qualifying_scope = parser->scope;
15264 if (!unqualified_name && pack_expansion_p)
15266 /* Check whether an error occurred. */
15267 okay = !cp_parser_error_occurred (parser);
15269 /* We already consumed the ellipsis to mark a
15270 parameter pack, but we have no way to report it,
15271 so abort the tentative parse. We will be exiting
15272 immediately anyway. */
15273 cp_parser_abort_tentative_parse (parser);
15276 okay = cp_parser_parse_definitely (parser);
15279 unqualified_name = error_mark_node;
15280 else if (unqualified_name
15281 && (qualifying_scope
15282 || (TREE_CODE (unqualified_name)
15283 != IDENTIFIER_NODE)))
15285 cp_parser_error (parser, "expected unqualified-id");
15286 unqualified_name = error_mark_node;
15290 if (!unqualified_name)
15292 if (unqualified_name == error_mark_node)
15294 declarator = cp_error_declarator;
15295 pack_expansion_p = false;
15296 declarator->parameter_pack_p = false;
15300 if (qualifying_scope && at_namespace_scope_p ()
15301 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15303 /* In the declaration of a member of a template class
15304 outside of the class itself, the SCOPE will sometimes
15305 be a TYPENAME_TYPE. For example, given:
15307 template <typename T>
15308 int S<T>::R::i = 3;
15310 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15311 this context, we must resolve S<T>::R to an ordinary
15312 type, rather than a typename type.
15314 The reason we normally avoid resolving TYPENAME_TYPEs
15315 is that a specialization of `S' might render
15316 `S<T>::R' not a type. However, if `S' is
15317 specialized, then this `i' will not be used, so there
15318 is no harm in resolving the types here. */
15321 /* Resolve the TYPENAME_TYPE. */
15322 type = resolve_typename_type (qualifying_scope,
15323 /*only_current_p=*/false);
15324 /* If that failed, the declarator is invalid. */
15325 if (TREE_CODE (type) == TYPENAME_TYPE)
15327 if (typedef_variant_p (type))
15328 error_at (declarator_id_start_token->location,
15329 "cannot define member of dependent typedef "
15332 error_at (declarator_id_start_token->location,
15333 "%<%T::%E%> is not a type",
15334 TYPE_CONTEXT (qualifying_scope),
15335 TYPE_IDENTIFIER (qualifying_scope));
15337 qualifying_scope = type;
15342 if (unqualified_name)
15346 if (qualifying_scope
15347 && CLASS_TYPE_P (qualifying_scope))
15348 class_type = qualifying_scope;
15350 class_type = current_class_type;
15352 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15354 tree name_type = TREE_TYPE (unqualified_name);
15355 if (class_type && same_type_p (name_type, class_type))
15357 if (qualifying_scope
15358 && CLASSTYPE_USE_TEMPLATE (name_type))
15360 error_at (declarator_id_start_token->location,
15361 "invalid use of constructor as a template");
15362 inform (declarator_id_start_token->location,
15363 "use %<%T::%D%> instead of %<%T::%D%> to "
15364 "name the constructor in a qualified name",
15366 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15367 class_type, name_type);
15368 declarator = cp_error_declarator;
15372 unqualified_name = constructor_name (class_type);
15376 /* We do not attempt to print the declarator
15377 here because we do not have enough
15378 information about its original syntactic
15380 cp_parser_error (parser, "invalid declarator");
15381 declarator = cp_error_declarator;
15388 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15389 sfk = sfk_destructor;
15390 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15391 sfk = sfk_conversion;
15392 else if (/* There's no way to declare a constructor
15393 for an anonymous type, even if the type
15394 got a name for linkage purposes. */
15395 !TYPE_WAS_ANONYMOUS (class_type)
15396 && constructor_name_p (unqualified_name,
15399 unqualified_name = constructor_name (class_type);
15400 sfk = sfk_constructor;
15402 else if (is_overloaded_fn (unqualified_name)
15403 && DECL_CONSTRUCTOR_P (get_first_fn
15404 (unqualified_name)))
15405 sfk = sfk_constructor;
15407 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15408 *ctor_dtor_or_conv_p = -1;
15411 declarator = make_id_declarator (qualifying_scope,
15414 declarator->id_loc = token->location;
15415 declarator->parameter_pack_p = pack_expansion_p;
15417 if (pack_expansion_p)
15418 maybe_warn_variadic_templates ();
15421 handle_declarator:;
15422 scope = get_scope_of_declarator (declarator);
15424 /* Any names that appear after the declarator-id for a
15425 member are looked up in the containing scope. */
15426 pushed_scope = push_scope (scope);
15427 parser->in_declarator_p = true;
15428 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15429 || (declarator && declarator->kind == cdk_id))
15430 /* Default args are only allowed on function
15432 parser->default_arg_ok_p = saved_default_arg_ok_p;
15434 parser->default_arg_ok_p = false;
15443 /* For an abstract declarator, we might wind up with nothing at this
15444 point. That's an error; the declarator is not optional. */
15446 cp_parser_error (parser, "expected declarator");
15448 /* If we entered a scope, we must exit it now. */
15450 pop_scope (pushed_scope);
15452 parser->default_arg_ok_p = saved_default_arg_ok_p;
15453 parser->in_declarator_p = saved_in_declarator_p;
15458 /* Parse a ptr-operator.
15461 * cv-qualifier-seq [opt]
15463 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15468 & cv-qualifier-seq [opt]
15470 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15471 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15472 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15473 filled in with the TYPE containing the member. *CV_QUALS is
15474 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15475 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15476 Note that the tree codes returned by this function have nothing
15477 to do with the types of trees that will be eventually be created
15478 to represent the pointer or reference type being parsed. They are
15479 just constants with suggestive names. */
15480 static enum tree_code
15481 cp_parser_ptr_operator (cp_parser* parser,
15483 cp_cv_quals *cv_quals)
15485 enum tree_code code = ERROR_MARK;
15488 /* Assume that it's not a pointer-to-member. */
15490 /* And that there are no cv-qualifiers. */
15491 *cv_quals = TYPE_UNQUALIFIED;
15493 /* Peek at the next token. */
15494 token = cp_lexer_peek_token (parser->lexer);
15496 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15497 if (token->type == CPP_MULT)
15498 code = INDIRECT_REF;
15499 else if (token->type == CPP_AND)
15501 else if ((cxx_dialect != cxx98) &&
15502 token->type == CPP_AND_AND) /* C++0x only */
15503 code = NON_LVALUE_EXPR;
15505 if (code != ERROR_MARK)
15507 /* Consume the `*', `&' or `&&'. */
15508 cp_lexer_consume_token (parser->lexer);
15510 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15511 `&', if we are allowing GNU extensions. (The only qualifier
15512 that can legally appear after `&' is `restrict', but that is
15513 enforced during semantic analysis. */
15514 if (code == INDIRECT_REF
15515 || cp_parser_allow_gnu_extensions_p (parser))
15516 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15520 /* Try the pointer-to-member case. */
15521 cp_parser_parse_tentatively (parser);
15522 /* Look for the optional `::' operator. */
15523 cp_parser_global_scope_opt (parser,
15524 /*current_scope_valid_p=*/false);
15525 /* Look for the nested-name specifier. */
15526 token = cp_lexer_peek_token (parser->lexer);
15527 cp_parser_nested_name_specifier (parser,
15528 /*typename_keyword_p=*/false,
15529 /*check_dependency_p=*/true,
15531 /*is_declaration=*/false);
15532 /* If we found it, and the next token is a `*', then we are
15533 indeed looking at a pointer-to-member operator. */
15534 if (!cp_parser_error_occurred (parser)
15535 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15537 /* Indicate that the `*' operator was used. */
15538 code = INDIRECT_REF;
15540 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15541 error_at (token->location, "%qD is a namespace", parser->scope);
15544 /* The type of which the member is a member is given by the
15546 *type = parser->scope;
15547 /* The next name will not be qualified. */
15548 parser->scope = NULL_TREE;
15549 parser->qualifying_scope = NULL_TREE;
15550 parser->object_scope = NULL_TREE;
15551 /* Look for the optional cv-qualifier-seq. */
15552 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15555 /* If that didn't work we don't have a ptr-operator. */
15556 if (!cp_parser_parse_definitely (parser))
15557 cp_parser_error (parser, "expected ptr-operator");
15563 /* Parse an (optional) cv-qualifier-seq.
15566 cv-qualifier cv-qualifier-seq [opt]
15577 Returns a bitmask representing the cv-qualifiers. */
15580 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15582 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15587 cp_cv_quals cv_qualifier;
15589 /* Peek at the next token. */
15590 token = cp_lexer_peek_token (parser->lexer);
15591 /* See if it's a cv-qualifier. */
15592 switch (token->keyword)
15595 cv_qualifier = TYPE_QUAL_CONST;
15599 cv_qualifier = TYPE_QUAL_VOLATILE;
15603 cv_qualifier = TYPE_QUAL_RESTRICT;
15607 cv_qualifier = TYPE_UNQUALIFIED;
15614 if (cv_quals & cv_qualifier)
15616 error_at (token->location, "duplicate cv-qualifier");
15617 cp_lexer_purge_token (parser->lexer);
15621 cp_lexer_consume_token (parser->lexer);
15622 cv_quals |= cv_qualifier;
15629 /* Parse an (optional) virt-specifier-seq.
15631 virt-specifier-seq:
15632 virt-specifier virt-specifier-seq [opt]
15638 Returns a bitmask representing the virt-specifiers. */
15640 static cp_virt_specifiers
15641 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15643 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15648 cp_virt_specifiers virt_specifier;
15650 /* Peek at the next token. */
15651 token = cp_lexer_peek_token (parser->lexer);
15652 /* See if it's a virt-specifier-qualifier. */
15653 if (token->type != CPP_NAME)
15655 if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15657 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15658 virt_specifier = VIRT_SPEC_OVERRIDE;
15660 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15662 maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15663 virt_specifier = VIRT_SPEC_FINAL;
15665 else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
15667 virt_specifier = VIRT_SPEC_FINAL;
15672 if (virt_specifiers & virt_specifier)
15674 error_at (token->location, "duplicate virt-specifier");
15675 cp_lexer_purge_token (parser->lexer);
15679 cp_lexer_consume_token (parser->lexer);
15680 virt_specifiers |= virt_specifier;
15683 return virt_specifiers;
15686 /* Parse a late-specified return type, if any. This is not a separate
15687 non-terminal, but part of a function declarator, which looks like
15689 -> trailing-type-specifier-seq abstract-declarator(opt)
15691 Returns the type indicated by the type-id.
15693 QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15697 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15702 /* Peek at the next token. */
15703 token = cp_lexer_peek_token (parser->lexer);
15704 /* A late-specified return type is indicated by an initial '->'. */
15705 if (token->type != CPP_DEREF)
15708 /* Consume the ->. */
15709 cp_lexer_consume_token (parser->lexer);
15713 /* DR 1207: 'this' is in scope in the trailing return type. */
15714 tree this_parm = build_this_parm (current_class_type, quals);
15715 gcc_assert (current_class_ptr == NULL_TREE);
15717 = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15718 /* Set this second to avoid shortcut in cp_build_indirect_ref. */
15719 current_class_ptr = this_parm;
15722 type = cp_parser_trailing_type_id (parser);
15725 current_class_ptr = current_class_ref = NULL_TREE;
15730 /* Parse a declarator-id.
15734 :: [opt] nested-name-specifier [opt] type-name
15736 In the `id-expression' case, the value returned is as for
15737 cp_parser_id_expression if the id-expression was an unqualified-id.
15738 If the id-expression was a qualified-id, then a SCOPE_REF is
15739 returned. The first operand is the scope (either a NAMESPACE_DECL
15740 or TREE_TYPE), but the second is still just a representation of an
15744 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15747 /* The expression must be an id-expression. Assume that qualified
15748 names are the names of types so that:
15751 int S<T>::R::i = 3;
15753 will work; we must treat `S<T>::R' as the name of a type.
15754 Similarly, assume that qualified names are templates, where
15758 int S<T>::R<T>::i = 3;
15761 id = cp_parser_id_expression (parser,
15762 /*template_keyword_p=*/false,
15763 /*check_dependency_p=*/false,
15764 /*template_p=*/NULL,
15765 /*declarator_p=*/true,
15767 if (id && BASELINK_P (id))
15768 id = BASELINK_FUNCTIONS (id);
15772 /* Parse a type-id.
15775 type-specifier-seq abstract-declarator [opt]
15777 Returns the TYPE specified. */
15780 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15781 bool is_trailing_return)
15783 cp_decl_specifier_seq type_specifier_seq;
15784 cp_declarator *abstract_declarator;
15786 /* Parse the type-specifier-seq. */
15787 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15788 is_trailing_return,
15789 &type_specifier_seq);
15790 if (type_specifier_seq.type == error_mark_node)
15791 return error_mark_node;
15793 /* There might or might not be an abstract declarator. */
15794 cp_parser_parse_tentatively (parser);
15795 /* Look for the declarator. */
15796 abstract_declarator
15797 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15798 /*parenthesized_p=*/NULL,
15799 /*member_p=*/false);
15800 /* Check to see if there really was a declarator. */
15801 if (!cp_parser_parse_definitely (parser))
15802 abstract_declarator = NULL;
15804 if (type_specifier_seq.type
15805 && type_uses_auto (type_specifier_seq.type))
15807 /* A type-id with type 'auto' is only ok if the abstract declarator
15808 is a function declarator with a late-specified return type. */
15809 if (abstract_declarator
15810 && abstract_declarator->kind == cdk_function
15811 && abstract_declarator->u.function.late_return_type)
15815 error ("invalid use of %<auto%>");
15816 return error_mark_node;
15820 return groktypename (&type_specifier_seq, abstract_declarator,
15824 static tree cp_parser_type_id (cp_parser *parser)
15826 return cp_parser_type_id_1 (parser, false, false);
15829 static tree cp_parser_template_type_arg (cp_parser *parser)
15832 const char *saved_message = parser->type_definition_forbidden_message;
15833 parser->type_definition_forbidden_message
15834 = G_("types may not be defined in template arguments");
15835 r = cp_parser_type_id_1 (parser, true, false);
15836 parser->type_definition_forbidden_message = saved_message;
15840 static tree cp_parser_trailing_type_id (cp_parser *parser)
15842 return cp_parser_type_id_1 (parser, false, true);
15845 /* Parse a type-specifier-seq.
15847 type-specifier-seq:
15848 type-specifier type-specifier-seq [opt]
15852 type-specifier-seq:
15853 attributes type-specifier-seq [opt]
15855 If IS_DECLARATION is true, we are at the start of a "condition" or
15856 exception-declaration, so we might be followed by a declarator-id.
15858 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15859 i.e. we've just seen "->".
15861 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15864 cp_parser_type_specifier_seq (cp_parser* parser,
15865 bool is_declaration,
15866 bool is_trailing_return,
15867 cp_decl_specifier_seq *type_specifier_seq)
15869 bool seen_type_specifier = false;
15870 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15871 cp_token *start_token = NULL;
15873 /* Clear the TYPE_SPECIFIER_SEQ. */
15874 clear_decl_specs (type_specifier_seq);
15876 /* In the context of a trailing return type, enum E { } is an
15877 elaborated-type-specifier followed by a function-body, not an
15879 if (is_trailing_return)
15880 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15882 /* Parse the type-specifiers and attributes. */
15885 tree type_specifier;
15886 bool is_cv_qualifier;
15888 /* Check for attributes first. */
15889 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15891 type_specifier_seq->attributes =
15892 chainon (type_specifier_seq->attributes,
15893 cp_parser_attributes_opt (parser));
15897 /* record the token of the beginning of the type specifier seq,
15898 for error reporting purposes*/
15900 start_token = cp_lexer_peek_token (parser->lexer);
15902 /* Look for the type-specifier. */
15903 type_specifier = cp_parser_type_specifier (parser,
15905 type_specifier_seq,
15906 /*is_declaration=*/false,
15909 if (!type_specifier)
15911 /* If the first type-specifier could not be found, this is not a
15912 type-specifier-seq at all. */
15913 if (!seen_type_specifier)
15915 cp_parser_error (parser, "expected type-specifier");
15916 type_specifier_seq->type = error_mark_node;
15919 /* If subsequent type-specifiers could not be found, the
15920 type-specifier-seq is complete. */
15924 seen_type_specifier = true;
15925 /* The standard says that a condition can be:
15927 type-specifier-seq declarator = assignment-expression
15934 we should treat the "S" as a declarator, not as a
15935 type-specifier. The standard doesn't say that explicitly for
15936 type-specifier-seq, but it does say that for
15937 decl-specifier-seq in an ordinary declaration. Perhaps it
15938 would be clearer just to allow a decl-specifier-seq here, and
15939 then add a semantic restriction that if any decl-specifiers
15940 that are not type-specifiers appear, the program is invalid. */
15941 if (is_declaration && !is_cv_qualifier)
15942 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15945 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15948 /* Parse a parameter-declaration-clause.
15950 parameter-declaration-clause:
15951 parameter-declaration-list [opt] ... [opt]
15952 parameter-declaration-list , ...
15954 Returns a representation for the parameter declarations. A return
15955 value of NULL indicates a parameter-declaration-clause consisting
15956 only of an ellipsis. */
15959 cp_parser_parameter_declaration_clause (cp_parser* parser)
15966 /* Peek at the next token. */
15967 token = cp_lexer_peek_token (parser->lexer);
15968 /* Check for trivial parameter-declaration-clauses. */
15969 if (token->type == CPP_ELLIPSIS)
15971 /* Consume the `...' token. */
15972 cp_lexer_consume_token (parser->lexer);
15975 else if (token->type == CPP_CLOSE_PAREN)
15976 /* There are no parameters. */
15978 #ifndef NO_IMPLICIT_EXTERN_C
15979 if (in_system_header && current_class_type == NULL
15980 && current_lang_name == lang_name_c)
15984 return void_list_node;
15986 /* Check for `(void)', too, which is a special case. */
15987 else if (token->keyword == RID_VOID
15988 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15989 == CPP_CLOSE_PAREN))
15991 /* Consume the `void' token. */
15992 cp_lexer_consume_token (parser->lexer);
15993 /* There are no parameters. */
15994 return void_list_node;
15997 /* Parse the parameter-declaration-list. */
15998 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15999 /* If a parse error occurred while parsing the
16000 parameter-declaration-list, then the entire
16001 parameter-declaration-clause is erroneous. */
16005 /* Peek at the next token. */
16006 token = cp_lexer_peek_token (parser->lexer);
16007 /* If it's a `,', the clause should terminate with an ellipsis. */
16008 if (token->type == CPP_COMMA)
16010 /* Consume the `,'. */
16011 cp_lexer_consume_token (parser->lexer);
16012 /* Expect an ellipsis. */
16014 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16016 /* It might also be `...' if the optional trailing `,' was
16018 else if (token->type == CPP_ELLIPSIS)
16020 /* Consume the `...' token. */
16021 cp_lexer_consume_token (parser->lexer);
16022 /* And remember that we saw it. */
16026 ellipsis_p = false;
16028 /* Finish the parameter list. */
16030 parameters = chainon (parameters, void_list_node);
16035 /* Parse a parameter-declaration-list.
16037 parameter-declaration-list:
16038 parameter-declaration
16039 parameter-declaration-list , parameter-declaration
16041 Returns a representation of the parameter-declaration-list, as for
16042 cp_parser_parameter_declaration_clause. However, the
16043 `void_list_node' is never appended to the list. Upon return,
16044 *IS_ERROR will be true iff an error occurred. */
16047 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16049 tree parameters = NULL_TREE;
16050 tree *tail = ¶meters;
16051 bool saved_in_unbraced_linkage_specification_p;
16054 /* Assume all will go well. */
16056 /* The special considerations that apply to a function within an
16057 unbraced linkage specifications do not apply to the parameters
16058 to the function. */
16059 saved_in_unbraced_linkage_specification_p
16060 = parser->in_unbraced_linkage_specification_p;
16061 parser->in_unbraced_linkage_specification_p = false;
16063 /* Look for more parameters. */
16066 cp_parameter_declarator *parameter;
16067 tree decl = error_mark_node;
16068 bool parenthesized_p = false;
16069 /* Parse the parameter. */
16071 = cp_parser_parameter_declaration (parser,
16072 /*template_parm_p=*/false,
16075 /* We don't know yet if the enclosing context is deprecated, so wait
16076 and warn in grokparms if appropriate. */
16077 deprecated_state = DEPRECATED_SUPPRESS;
16080 decl = grokdeclarator (parameter->declarator,
16081 ¶meter->decl_specifiers,
16083 parameter->default_argument != NULL_TREE,
16084 ¶meter->decl_specifiers.attributes);
16086 deprecated_state = DEPRECATED_NORMAL;
16088 /* If a parse error occurred parsing the parameter declaration,
16089 then the entire parameter-declaration-list is erroneous. */
16090 if (decl == error_mark_node)
16093 parameters = error_mark_node;
16097 if (parameter->decl_specifiers.attributes)
16098 cplus_decl_attributes (&decl,
16099 parameter->decl_specifiers.attributes,
16101 if (DECL_NAME (decl))
16102 decl = pushdecl (decl);
16104 if (decl != error_mark_node)
16106 retrofit_lang_decl (decl);
16107 DECL_PARM_INDEX (decl) = ++index;
16108 DECL_PARM_LEVEL (decl) = function_parm_depth ();
16111 /* Add the new parameter to the list. */
16112 *tail = build_tree_list (parameter->default_argument, decl);
16113 tail = &TREE_CHAIN (*tail);
16115 /* Peek at the next token. */
16116 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16117 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16118 /* These are for Objective-C++ */
16119 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16120 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16121 /* The parameter-declaration-list is complete. */
16123 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16127 /* Peek at the next token. */
16128 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16129 /* If it's an ellipsis, then the list is complete. */
16130 if (token->type == CPP_ELLIPSIS)
16132 /* Otherwise, there must be more parameters. Consume the
16134 cp_lexer_consume_token (parser->lexer);
16135 /* When parsing something like:
16137 int i(float f, double d)
16139 we can tell after seeing the declaration for "f" that we
16140 are not looking at an initialization of a variable "i",
16141 but rather at the declaration of a function "i".
16143 Due to the fact that the parsing of template arguments
16144 (as specified to a template-id) requires backtracking we
16145 cannot use this technique when inside a template argument
16147 if (!parser->in_template_argument_list_p
16148 && !parser->in_type_id_in_expr_p
16149 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16150 /* However, a parameter-declaration of the form
16151 "foat(f)" (which is a valid declaration of a
16152 parameter "f") can also be interpreted as an
16153 expression (the conversion of "f" to "float"). */
16154 && !parenthesized_p)
16155 cp_parser_commit_to_tentative_parse (parser);
16159 cp_parser_error (parser, "expected %<,%> or %<...%>");
16160 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16161 cp_parser_skip_to_closing_parenthesis (parser,
16162 /*recovering=*/true,
16163 /*or_comma=*/false,
16164 /*consume_paren=*/false);
16169 parser->in_unbraced_linkage_specification_p
16170 = saved_in_unbraced_linkage_specification_p;
16175 /* Parse a parameter declaration.
16177 parameter-declaration:
16178 decl-specifier-seq ... [opt] declarator
16179 decl-specifier-seq declarator = assignment-expression
16180 decl-specifier-seq ... [opt] abstract-declarator [opt]
16181 decl-specifier-seq abstract-declarator [opt] = assignment-expression
16183 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16184 declares a template parameter. (In that case, a non-nested `>'
16185 token encountered during the parsing of the assignment-expression
16186 is not interpreted as a greater-than operator.)
16188 Returns a representation of the parameter, or NULL if an error
16189 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16190 true iff the declarator is of the form "(p)". */
16192 static cp_parameter_declarator *
16193 cp_parser_parameter_declaration (cp_parser *parser,
16194 bool template_parm_p,
16195 bool *parenthesized_p)
16197 int declares_class_or_enum;
16198 cp_decl_specifier_seq decl_specifiers;
16199 cp_declarator *declarator;
16200 tree default_argument;
16201 cp_token *token = NULL, *declarator_token_start = NULL;
16202 const char *saved_message;
16204 /* In a template parameter, `>' is not an operator.
16208 When parsing a default template-argument for a non-type
16209 template-parameter, the first non-nested `>' is taken as the end
16210 of the template parameter-list rather than a greater-than
16213 /* Type definitions may not appear in parameter types. */
16214 saved_message = parser->type_definition_forbidden_message;
16215 parser->type_definition_forbidden_message
16216 = G_("types may not be defined in parameter types");
16218 /* Parse the declaration-specifiers. */
16219 cp_parser_decl_specifier_seq (parser,
16220 CP_PARSER_FLAGS_NONE,
16222 &declares_class_or_enum);
16224 /* Complain about missing 'typename' or other invalid type names. */
16225 if (!decl_specifiers.any_type_specifiers_p)
16226 cp_parser_parse_and_diagnose_invalid_type_name (parser);
16228 /* If an error occurred, there's no reason to attempt to parse the
16229 rest of the declaration. */
16230 if (cp_parser_error_occurred (parser))
16232 parser->type_definition_forbidden_message = saved_message;
16236 /* Peek at the next token. */
16237 token = cp_lexer_peek_token (parser->lexer);
16239 /* If the next token is a `)', `,', `=', `>', or `...', then there
16240 is no declarator. However, when variadic templates are enabled,
16241 there may be a declarator following `...'. */
16242 if (token->type == CPP_CLOSE_PAREN
16243 || token->type == CPP_COMMA
16244 || token->type == CPP_EQ
16245 || token->type == CPP_GREATER)
16248 if (parenthesized_p)
16249 *parenthesized_p = false;
16251 /* Otherwise, there should be a declarator. */
16254 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16255 parser->default_arg_ok_p = false;
16257 /* After seeing a decl-specifier-seq, if the next token is not a
16258 "(", there is no possibility that the code is a valid
16259 expression. Therefore, if parsing tentatively, we commit at
16261 if (!parser->in_template_argument_list_p
16262 /* In an expression context, having seen:
16266 we cannot be sure whether we are looking at a
16267 function-type (taking a "char" as a parameter) or a cast
16268 of some object of type "char" to "int". */
16269 && !parser->in_type_id_in_expr_p
16270 && cp_parser_uncommitted_to_tentative_parse_p (parser)
16271 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16272 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16273 cp_parser_commit_to_tentative_parse (parser);
16274 /* Parse the declarator. */
16275 declarator_token_start = token;
16276 declarator = cp_parser_declarator (parser,
16277 CP_PARSER_DECLARATOR_EITHER,
16278 /*ctor_dtor_or_conv_p=*/NULL,
16280 /*member_p=*/false);
16281 parser->default_arg_ok_p = saved_default_arg_ok_p;
16282 /* After the declarator, allow more attributes. */
16283 decl_specifiers.attributes
16284 = chainon (decl_specifiers.attributes,
16285 cp_parser_attributes_opt (parser));
16288 /* If the next token is an ellipsis, and we have not seen a
16289 declarator name, and the type of the declarator contains parameter
16290 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16291 a parameter pack expansion expression. Otherwise, leave the
16292 ellipsis for a C-style variadic function. */
16293 token = cp_lexer_peek_token (parser->lexer);
16294 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16296 tree type = decl_specifiers.type;
16298 if (type && DECL_P (type))
16299 type = TREE_TYPE (type);
16302 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16303 && declarator_can_be_parameter_pack (declarator)
16304 && (!declarator || !declarator->parameter_pack_p)
16305 && uses_parameter_packs (type))
16307 /* Consume the `...'. */
16308 cp_lexer_consume_token (parser->lexer);
16309 maybe_warn_variadic_templates ();
16311 /* Build a pack expansion type */
16313 declarator->parameter_pack_p = true;
16315 decl_specifiers.type = make_pack_expansion (type);
16319 /* The restriction on defining new types applies only to the type
16320 of the parameter, not to the default argument. */
16321 parser->type_definition_forbidden_message = saved_message;
16323 /* If the next token is `=', then process a default argument. */
16324 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16326 /* Consume the `='. */
16327 cp_lexer_consume_token (parser->lexer);
16329 /* If we are defining a class, then the tokens that make up the
16330 default argument must be saved and processed later. */
16331 if (!template_parm_p && at_class_scope_p ()
16332 && TYPE_BEING_DEFINED (current_class_type)
16333 && !LAMBDA_TYPE_P (current_class_type))
16335 unsigned depth = 0;
16336 int maybe_template_id = 0;
16337 cp_token *first_token;
16340 /* Add tokens until we have processed the entire default
16341 argument. We add the range [first_token, token). */
16342 first_token = cp_lexer_peek_token (parser->lexer);
16347 /* Peek at the next token. */
16348 token = cp_lexer_peek_token (parser->lexer);
16349 /* What we do depends on what token we have. */
16350 switch (token->type)
16352 /* In valid code, a default argument must be
16353 immediately followed by a `,' `)', or `...'. */
16355 if (depth == 0 && maybe_template_id)
16357 /* If we've seen a '<', we might be in a
16358 template-argument-list. Until Core issue 325 is
16359 resolved, we don't know how this situation ought
16360 to be handled, so try to DTRT. We check whether
16361 what comes after the comma is a valid parameter
16362 declaration list. If it is, then the comma ends
16363 the default argument; otherwise the default
16364 argument continues. */
16365 bool error = false;
16368 /* Set ITALP so cp_parser_parameter_declaration_list
16369 doesn't decide to commit to this parse. */
16370 bool saved_italp = parser->in_template_argument_list_p;
16371 parser->in_template_argument_list_p = true;
16373 cp_parser_parse_tentatively (parser);
16374 cp_lexer_consume_token (parser->lexer);
16375 begin_scope (sk_function_parms, NULL_TREE);
16376 cp_parser_parameter_declaration_list (parser, &error);
16377 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16378 pop_binding (DECL_NAME (t), t);
16380 if (!cp_parser_error_occurred (parser) && !error)
16382 cp_parser_abort_tentative_parse (parser);
16384 parser->in_template_argument_list_p = saved_italp;
16387 case CPP_CLOSE_PAREN:
16389 /* If we run into a non-nested `;', `}', or `]',
16390 then the code is invalid -- but the default
16391 argument is certainly over. */
16392 case CPP_SEMICOLON:
16393 case CPP_CLOSE_BRACE:
16394 case CPP_CLOSE_SQUARE:
16397 /* Update DEPTH, if necessary. */
16398 else if (token->type == CPP_CLOSE_PAREN
16399 || token->type == CPP_CLOSE_BRACE
16400 || token->type == CPP_CLOSE_SQUARE)
16404 case CPP_OPEN_PAREN:
16405 case CPP_OPEN_SQUARE:
16406 case CPP_OPEN_BRACE:
16412 /* This might be the comparison operator, or it might
16413 start a template argument list. */
16414 ++maybe_template_id;
16418 if (cxx_dialect == cxx98)
16420 /* Fall through for C++0x, which treats the `>>'
16421 operator like two `>' tokens in certain
16427 /* This might be an operator, or it might close a
16428 template argument list. But if a previous '<'
16429 started a template argument list, this will have
16430 closed it, so we can't be in one anymore. */
16431 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16432 if (maybe_template_id < 0)
16433 maybe_template_id = 0;
16437 /* If we run out of tokens, issue an error message. */
16439 case CPP_PRAGMA_EOL:
16440 error_at (token->location, "file ends in default argument");
16446 /* In these cases, we should look for template-ids.
16447 For example, if the default argument is
16448 `X<int, double>()', we need to do name lookup to
16449 figure out whether or not `X' is a template; if
16450 so, the `,' does not end the default argument.
16452 That is not yet done. */
16459 /* If we've reached the end, stop. */
16463 /* Add the token to the token block. */
16464 token = cp_lexer_consume_token (parser->lexer);
16467 /* Create a DEFAULT_ARG to represent the unparsed default
16469 default_argument = make_node (DEFAULT_ARG);
16470 DEFARG_TOKENS (default_argument)
16471 = cp_token_cache_new (first_token, token);
16472 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16474 /* Outside of a class definition, we can just parse the
16475 assignment-expression. */
16478 token = cp_lexer_peek_token (parser->lexer);
16480 = cp_parser_default_argument (parser, template_parm_p);
16483 if (!parser->default_arg_ok_p)
16485 if (flag_permissive)
16486 warning (0, "deprecated use of default argument for parameter of non-function");
16489 error_at (token->location,
16490 "default arguments are only "
16491 "permitted for function parameters");
16492 default_argument = NULL_TREE;
16495 else if ((declarator && declarator->parameter_pack_p)
16496 || (decl_specifiers.type
16497 && PACK_EXPANSION_P (decl_specifiers.type)))
16499 /* Find the name of the parameter pack. */
16500 cp_declarator *id_declarator = declarator;
16501 while (id_declarator && id_declarator->kind != cdk_id)
16502 id_declarator = id_declarator->declarator;
16504 if (id_declarator && id_declarator->kind == cdk_id)
16505 error_at (declarator_token_start->location,
16507 ? "template parameter pack %qD"
16508 " cannot have a default argument"
16509 : "parameter pack %qD cannot have a default argument",
16510 id_declarator->u.id.unqualified_name);
16512 error_at (declarator_token_start->location,
16514 ? "template parameter pack cannot have a default argument"
16515 : "parameter pack cannot have a default argument");
16517 default_argument = NULL_TREE;
16521 default_argument = NULL_TREE;
16523 return make_parameter_declarator (&decl_specifiers,
16528 /* Parse a default argument and return it.
16530 TEMPLATE_PARM_P is true if this is a default argument for a
16531 non-type template parameter. */
16533 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16535 tree default_argument = NULL_TREE;
16536 bool saved_greater_than_is_operator_p;
16537 bool saved_local_variables_forbidden_p;
16538 bool non_constant_p;
16540 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16542 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16543 parser->greater_than_is_operator_p = !template_parm_p;
16544 /* Local variable names (and the `this' keyword) may not
16545 appear in a default argument. */
16546 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16547 parser->local_variables_forbidden_p = true;
16548 /* Parse the assignment-expression. */
16549 if (template_parm_p)
16550 push_deferring_access_checks (dk_no_deferred);
16552 = cp_parser_initializer_clause (parser, &non_constant_p);
16553 if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
16554 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16555 if (template_parm_p)
16556 pop_deferring_access_checks ();
16557 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16558 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16560 return default_argument;
16563 /* Parse a function-body.
16566 compound_statement */
16569 cp_parser_function_body (cp_parser *parser)
16571 cp_parser_compound_statement (parser, NULL, false, true);
16574 /* Parse a ctor-initializer-opt followed by a function-body. Return
16575 true if a ctor-initializer was present. */
16578 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16581 bool ctor_initializer_p;
16582 const bool check_body_p =
16583 DECL_CONSTRUCTOR_P (current_function_decl)
16584 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16587 /* Begin the function body. */
16588 body = begin_function_body ();
16589 /* Parse the optional ctor-initializer. */
16590 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16592 /* If we're parsing a constexpr constructor definition, we need
16593 to check that the constructor body is indeed empty. However,
16594 before we get to cp_parser_function_body lot of junk has been
16595 generated, so we can't just check that we have an empty block.
16596 Rather we take a snapshot of the outermost block, and check whether
16597 cp_parser_function_body changed its state. */
16601 if (TREE_CODE (list) == BIND_EXPR)
16602 list = BIND_EXPR_BODY (list);
16603 if (TREE_CODE (list) == STATEMENT_LIST
16604 && STATEMENT_LIST_TAIL (list) != NULL)
16605 last = STATEMENT_LIST_TAIL (list)->stmt;
16607 /* Parse the function-body. */
16608 cp_parser_function_body (parser);
16610 check_constexpr_ctor_body (last, list);
16611 /* Finish the function body. */
16612 finish_function_body (body);
16614 return ctor_initializer_p;
16617 /* Parse an initializer.
16620 = initializer-clause
16621 ( expression-list )
16623 Returns an expression representing the initializer. If no
16624 initializer is present, NULL_TREE is returned.
16626 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16627 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16628 set to TRUE if there is no initializer present. If there is an
16629 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16630 is set to true; otherwise it is set to false. */
16633 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16634 bool* non_constant_p)
16639 /* Peek at the next token. */
16640 token = cp_lexer_peek_token (parser->lexer);
16642 /* Let our caller know whether or not this initializer was
16644 *is_direct_init = (token->type != CPP_EQ);
16645 /* Assume that the initializer is constant. */
16646 *non_constant_p = false;
16648 if (token->type == CPP_EQ)
16650 /* Consume the `='. */
16651 cp_lexer_consume_token (parser->lexer);
16652 /* Parse the initializer-clause. */
16653 init = cp_parser_initializer_clause (parser, non_constant_p);
16655 else if (token->type == CPP_OPEN_PAREN)
16658 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16660 /*allow_expansion_p=*/true,
16663 return error_mark_node;
16664 init = build_tree_list_vec (vec);
16665 release_tree_vector (vec);
16667 else if (token->type == CPP_OPEN_BRACE)
16669 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16670 init = cp_parser_braced_list (parser, non_constant_p);
16671 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16675 /* Anything else is an error. */
16676 cp_parser_error (parser, "expected initializer");
16677 init = error_mark_node;
16683 /* Parse an initializer-clause.
16685 initializer-clause:
16686 assignment-expression
16689 Returns an expression representing the initializer.
16691 If the `assignment-expression' production is used the value
16692 returned is simply a representation for the expression.
16694 Otherwise, calls cp_parser_braced_list. */
16697 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16701 /* Assume the expression is constant. */
16702 *non_constant_p = false;
16704 /* If it is not a `{', then we are looking at an
16705 assignment-expression. */
16706 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16709 = cp_parser_constant_expression (parser,
16710 /*allow_non_constant_p=*/true,
16714 initializer = cp_parser_braced_list (parser, non_constant_p);
16716 return initializer;
16719 /* Parse a brace-enclosed initializer list.
16722 { initializer-list , [opt] }
16725 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16726 the elements of the initializer-list (or NULL, if the last
16727 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16728 NULL_TREE. There is no way to detect whether or not the optional
16729 trailing `,' was provided. NON_CONSTANT_P is as for
16730 cp_parser_initializer. */
16733 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16737 /* Consume the `{' token. */
16738 cp_lexer_consume_token (parser->lexer);
16739 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16740 initializer = make_node (CONSTRUCTOR);
16741 /* If it's not a `}', then there is a non-trivial initializer. */
16742 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16744 /* Parse the initializer list. */
16745 CONSTRUCTOR_ELTS (initializer)
16746 = cp_parser_initializer_list (parser, non_constant_p);
16747 /* A trailing `,' token is allowed. */
16748 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16749 cp_lexer_consume_token (parser->lexer);
16751 /* Now, there should be a trailing `}'. */
16752 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16753 TREE_TYPE (initializer) = init_list_type_node;
16754 return initializer;
16757 /* Parse an initializer-list.
16760 initializer-clause ... [opt]
16761 initializer-list , initializer-clause ... [opt]
16766 designation initializer-clause ...[opt]
16767 initializer-list , designation initializer-clause ...[opt]
16772 [ constant-expression ] =
16774 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16775 for the initializer. If the INDEX of the elt is non-NULL, it is the
16776 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16777 as for cp_parser_initializer. */
16779 static VEC(constructor_elt,gc) *
16780 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16782 VEC(constructor_elt,gc) *v = NULL;
16784 /* Assume all of the expressions are constant. */
16785 *non_constant_p = false;
16787 /* Parse the rest of the list. */
16793 bool clause_non_constant_p;
16795 /* If the next token is an identifier and the following one is a
16796 colon, we are looking at the GNU designated-initializer
16798 if (cp_parser_allow_gnu_extensions_p (parser)
16799 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16800 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16802 /* Warn the user that they are using an extension. */
16803 pedwarn (input_location, OPT_pedantic,
16804 "ISO C++ does not allow designated initializers");
16805 /* Consume the identifier. */
16806 designator = cp_lexer_consume_token (parser->lexer)->u.value;
16807 /* Consume the `:'. */
16808 cp_lexer_consume_token (parser->lexer);
16810 /* Also handle the C99 syntax, '. id ='. */
16811 else if (cp_parser_allow_gnu_extensions_p (parser)
16812 && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
16813 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
16814 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
16816 /* Warn the user that they are using an extension. */
16817 pedwarn (input_location, OPT_pedantic,
16818 "ISO C++ does not allow C99 designated initializers");
16819 /* Consume the `.'. */
16820 cp_lexer_consume_token (parser->lexer);
16821 /* Consume the identifier. */
16822 designator = cp_lexer_consume_token (parser->lexer)->u.value;
16823 /* Consume the `='. */
16824 cp_lexer_consume_token (parser->lexer);
16826 /* Also handle C99 array designators, '[ const ] ='. */
16827 else if (cp_parser_allow_gnu_extensions_p (parser)
16828 && !c_dialect_objc ()
16829 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16831 cp_lexer_consume_token (parser->lexer);
16832 designator = cp_parser_constant_expression (parser, false, NULL);
16833 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16834 cp_parser_require (parser, CPP_EQ, RT_EQ);
16837 designator = NULL_TREE;
16839 /* Parse the initializer. */
16840 initializer = cp_parser_initializer_clause (parser,
16841 &clause_non_constant_p);
16842 /* If any clause is non-constant, so is the entire initializer. */
16843 if (clause_non_constant_p)
16844 *non_constant_p = true;
16846 /* If we have an ellipsis, this is an initializer pack
16848 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16850 /* Consume the `...'. */
16851 cp_lexer_consume_token (parser->lexer);
16853 /* Turn the initializer into an initializer expansion. */
16854 initializer = make_pack_expansion (initializer);
16857 /* Add it to the vector. */
16858 CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
16860 /* If the next token is not a comma, we have reached the end of
16862 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16865 /* Peek at the next token. */
16866 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16867 /* If the next token is a `}', then we're still done. An
16868 initializer-clause can have a trailing `,' after the
16869 initializer-list and before the closing `}'. */
16870 if (token->type == CPP_CLOSE_BRACE)
16873 /* Consume the `,' token. */
16874 cp_lexer_consume_token (parser->lexer);
16880 /* Classes [gram.class] */
16882 /* Parse a class-name.
16888 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16889 to indicate that names looked up in dependent types should be
16890 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16891 keyword has been used to indicate that the name that appears next
16892 is a template. TAG_TYPE indicates the explicit tag given before
16893 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16894 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16895 is the class being defined in a class-head.
16897 Returns the TYPE_DECL representing the class. */
16900 cp_parser_class_name (cp_parser *parser,
16901 bool typename_keyword_p,
16902 bool template_keyword_p,
16903 enum tag_types tag_type,
16904 bool check_dependency_p,
16906 bool is_declaration)
16912 tree identifier = NULL_TREE;
16914 /* All class-names start with an identifier. */
16915 token = cp_lexer_peek_token (parser->lexer);
16916 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16918 cp_parser_error (parser, "expected class-name");
16919 return error_mark_node;
16922 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16923 to a template-id, so we save it here. */
16924 scope = parser->scope;
16925 if (scope == error_mark_node)
16926 return error_mark_node;
16928 /* Any name names a type if we're following the `typename' keyword
16929 in a qualified name where the enclosing scope is type-dependent. */
16930 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16931 && dependent_type_p (scope));
16932 /* Handle the common case (an identifier, but not a template-id)
16934 if (token->type == CPP_NAME
16935 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16937 cp_token *identifier_token;
16940 /* Look for the identifier. */
16941 identifier_token = cp_lexer_peek_token (parser->lexer);
16942 ambiguous_p = identifier_token->ambiguous_p;
16943 identifier = cp_parser_identifier (parser);
16944 /* If the next token isn't an identifier, we are certainly not
16945 looking at a class-name. */
16946 if (identifier == error_mark_node)
16947 decl = error_mark_node;
16948 /* If we know this is a type-name, there's no need to look it
16950 else if (typename_p)
16954 tree ambiguous_decls;
16955 /* If we already know that this lookup is ambiguous, then
16956 we've already issued an error message; there's no reason
16960 cp_parser_simulate_error (parser);
16961 return error_mark_node;
16963 /* If the next token is a `::', then the name must be a type
16966 [basic.lookup.qual]
16968 During the lookup for a name preceding the :: scope
16969 resolution operator, object, function, and enumerator
16970 names are ignored. */
16971 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16972 tag_type = typename_type;
16973 /* Look up the name. */
16974 decl = cp_parser_lookup_name (parser, identifier,
16976 /*is_template=*/false,
16977 /*is_namespace=*/false,
16978 check_dependency_p,
16980 identifier_token->location);
16981 if (ambiguous_decls)
16983 if (cp_parser_parsing_tentatively (parser))
16984 cp_parser_simulate_error (parser);
16985 return error_mark_node;
16991 /* Try a template-id. */
16992 decl = cp_parser_template_id (parser, template_keyword_p,
16993 check_dependency_p,
16995 if (decl == error_mark_node)
16996 return error_mark_node;
16999 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17001 /* If this is a typename, create a TYPENAME_TYPE. */
17002 if (typename_p && decl != error_mark_node)
17004 decl = make_typename_type (scope, decl, typename_type,
17005 /*complain=*/tf_error);
17006 if (decl != error_mark_node)
17007 decl = TYPE_NAME (decl);
17010 /* Check to see that it is really the name of a class. */
17011 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17012 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17013 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17014 /* Situations like this:
17016 template <typename T> struct A {
17017 typename T::template X<int>::I i;
17020 are problematic. Is `T::template X<int>' a class-name? The
17021 standard does not seem to be definitive, but there is no other
17022 valid interpretation of the following `::'. Therefore, those
17023 names are considered class-names. */
17025 decl = make_typename_type (scope, decl, tag_type, tf_error);
17026 if (decl != error_mark_node)
17027 decl = TYPE_NAME (decl);
17029 else if (TREE_CODE (decl) != TYPE_DECL
17030 || TREE_TYPE (decl) == error_mark_node
17031 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17032 /* In Objective-C 2.0, a classname followed by '.' starts a
17033 dot-syntax expression, and it's not a type-name. */
17034 || (c_dialect_objc ()
17035 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
17036 && objc_is_class_name (decl)))
17037 decl = error_mark_node;
17039 if (decl == error_mark_node)
17040 cp_parser_error (parser, "expected class-name");
17041 else if (identifier && !parser->scope)
17042 maybe_note_name_used_in_class (identifier, decl);
17047 /* Parse a class-specifier.
17050 class-head { member-specification [opt] }
17052 Returns the TREE_TYPE representing the class. */
17055 cp_parser_class_specifier_1 (cp_parser* parser)
17058 tree attributes = NULL_TREE;
17059 bool nested_name_specifier_p;
17060 unsigned saved_num_template_parameter_lists;
17061 bool saved_in_function_body;
17062 unsigned char in_statement;
17063 bool in_switch_statement_p;
17064 bool saved_in_unbraced_linkage_specification_p;
17065 tree old_scope = NULL_TREE;
17066 tree scope = NULL_TREE;
17068 cp_token *closing_brace;
17070 push_deferring_access_checks (dk_no_deferred);
17072 /* Parse the class-head. */
17073 type = cp_parser_class_head (parser,
17074 &nested_name_specifier_p,
17077 /* If the class-head was a semantic disaster, skip the entire body
17081 cp_parser_skip_to_end_of_block_or_statement (parser);
17082 pop_deferring_access_checks ();
17083 return error_mark_node;
17086 /* Look for the `{'. */
17087 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17089 pop_deferring_access_checks ();
17090 return error_mark_node;
17093 /* Process the base classes. If they're invalid, skip the
17094 entire class body. */
17095 if (!xref_basetypes (type, bases))
17097 /* Consuming the closing brace yields better error messages
17099 if (cp_parser_skip_to_closing_brace (parser))
17100 cp_lexer_consume_token (parser->lexer);
17101 pop_deferring_access_checks ();
17102 return error_mark_node;
17105 /* Issue an error message if type-definitions are forbidden here. */
17106 cp_parser_check_type_definition (parser);
17107 /* Remember that we are defining one more class. */
17108 ++parser->num_classes_being_defined;
17109 /* Inside the class, surrounding template-parameter-lists do not
17111 saved_num_template_parameter_lists
17112 = parser->num_template_parameter_lists;
17113 parser->num_template_parameter_lists = 0;
17114 /* We are not in a function body. */
17115 saved_in_function_body = parser->in_function_body;
17116 parser->in_function_body = false;
17117 /* Or in a loop. */
17118 in_statement = parser->in_statement;
17119 parser->in_statement = 0;
17120 /* Or in a switch. */
17121 in_switch_statement_p = parser->in_switch_statement_p;
17122 parser->in_switch_statement_p = false;
17123 /* We are not immediately inside an extern "lang" block. */
17124 saved_in_unbraced_linkage_specification_p
17125 = parser->in_unbraced_linkage_specification_p;
17126 parser->in_unbraced_linkage_specification_p = false;
17128 /* Start the class. */
17129 if (nested_name_specifier_p)
17131 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17132 old_scope = push_inner_scope (scope);
17134 type = begin_class_definition (type, attributes);
17136 if (type == error_mark_node)
17137 /* If the type is erroneous, skip the entire body of the class. */
17138 cp_parser_skip_to_closing_brace (parser);
17140 /* Parse the member-specification. */
17141 cp_parser_member_specification_opt (parser);
17143 /* Look for the trailing `}'. */
17144 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17145 /* Look for trailing attributes to apply to this class. */
17146 if (cp_parser_allow_gnu_extensions_p (parser))
17147 attributes = cp_parser_attributes_opt (parser);
17148 if (type != error_mark_node)
17149 type = finish_struct (type, attributes);
17150 if (nested_name_specifier_p)
17151 pop_inner_scope (old_scope, scope);
17153 /* We've finished a type definition. Check for the common syntax
17154 error of forgetting a semicolon after the definition. We need to
17155 be careful, as we can't just check for not-a-semicolon and be done
17156 with it; the user might have typed:
17158 class X { } c = ...;
17159 class X { } *p = ...;
17161 and so forth. Instead, enumerate all the possible tokens that
17162 might follow this production; if we don't see one of them, then
17163 complain and silently insert the semicolon. */
17165 cp_token *token = cp_lexer_peek_token (parser->lexer);
17166 bool want_semicolon = true;
17168 switch (token->type)
17171 case CPP_SEMICOLON:
17174 case CPP_OPEN_PAREN:
17175 case CPP_CLOSE_PAREN:
17177 want_semicolon = false;
17180 /* While it's legal for type qualifiers and storage class
17181 specifiers to follow type definitions in the grammar, only
17182 compiler testsuites contain code like that. Assume that if
17183 we see such code, then what we're really seeing is a case
17187 const <type> var = ...;
17192 static <type> func (...) ...
17194 i.e. the qualifier or specifier applies to the next
17195 declaration. To do so, however, we need to look ahead one
17196 more token to see if *that* token is a type specifier.
17198 This code could be improved to handle:
17201 static const <type> var = ...; */
17203 if (keyword_is_decl_specifier (token->keyword))
17205 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17207 /* Handling user-defined types here would be nice, but very
17210 = (lookahead->type == CPP_KEYWORD
17211 && keyword_begins_type_specifier (lookahead->keyword));
17218 /* If we don't have a type, then something is very wrong and we
17219 shouldn't try to do anything clever. Likewise for not seeing the
17221 if (closing_brace && TYPE_P (type) && want_semicolon)
17223 cp_token_position prev
17224 = cp_lexer_previous_token_position (parser->lexer);
17225 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17226 location_t loc = prev_token->location;
17228 if (CLASSTYPE_DECLARED_CLASS (type))
17229 error_at (loc, "expected %<;%> after class definition");
17230 else if (TREE_CODE (type) == RECORD_TYPE)
17231 error_at (loc, "expected %<;%> after struct definition");
17232 else if (TREE_CODE (type) == UNION_TYPE)
17233 error_at (loc, "expected %<;%> after union definition");
17235 gcc_unreachable ();
17237 /* Unget one token and smash it to look as though we encountered
17238 a semicolon in the input stream. */
17239 cp_lexer_set_token_position (parser->lexer, prev);
17240 token = cp_lexer_peek_token (parser->lexer);
17241 token->type = CPP_SEMICOLON;
17242 token->keyword = RID_MAX;
17246 /* If this class is not itself within the scope of another class,
17247 then we need to parse the bodies of all of the queued function
17248 definitions. Note that the queued functions defined in a class
17249 are not always processed immediately following the
17250 class-specifier for that class. Consider:
17253 struct B { void f() { sizeof (A); } };
17256 If `f' were processed before the processing of `A' were
17257 completed, there would be no way to compute the size of `A'.
17258 Note that the nesting we are interested in here is lexical --
17259 not the semantic nesting given by TYPE_CONTEXT. In particular,
17262 struct A { struct B; };
17263 struct A::B { void f() { } };
17265 there is no need to delay the parsing of `A::B::f'. */
17266 if (--parser->num_classes_being_defined == 0)
17269 tree class_type = NULL_TREE;
17270 tree pushed_scope = NULL_TREE;
17272 cp_default_arg_entry *e;
17274 /* In a first pass, parse default arguments to the functions.
17275 Then, in a second pass, parse the bodies of the functions.
17276 This two-phased approach handles cases like:
17284 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17288 /* If there are default arguments that have not yet been processed,
17289 take care of them now. */
17290 if (class_type != e->class_type)
17293 pop_scope (pushed_scope);
17294 class_type = e->class_type;
17295 pushed_scope = push_scope (class_type);
17297 /* Make sure that any template parameters are in scope. */
17298 maybe_begin_member_template_processing (fn);
17299 /* Parse the default argument expressions. */
17300 cp_parser_late_parsing_default_args (parser, fn);
17301 /* Remove any template parameters from the symbol table. */
17302 maybe_end_member_template_processing ();
17305 pop_scope (pushed_scope);
17306 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17307 /* Now parse the body of the functions. */
17308 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17309 cp_parser_late_parsing_for_member (parser, fn);
17310 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17313 /* Put back any saved access checks. */
17314 pop_deferring_access_checks ();
17316 /* Restore saved state. */
17317 parser->in_switch_statement_p = in_switch_statement_p;
17318 parser->in_statement = in_statement;
17319 parser->in_function_body = saved_in_function_body;
17320 parser->num_template_parameter_lists
17321 = saved_num_template_parameter_lists;
17322 parser->in_unbraced_linkage_specification_p
17323 = saved_in_unbraced_linkage_specification_p;
17329 cp_parser_class_specifier (cp_parser* parser)
17332 timevar_push (TV_PARSE_STRUCT);
17333 ret = cp_parser_class_specifier_1 (parser);
17334 timevar_pop (TV_PARSE_STRUCT);
17338 /* Parse a class-head.
17341 class-key identifier [opt] base-clause [opt]
17342 class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17343 class-key nested-name-specifier [opt] template-id
17346 class-virt-specifier:
17350 class-key attributes identifier [opt] base-clause [opt]
17351 class-key attributes nested-name-specifier identifier base-clause [opt]
17352 class-key attributes nested-name-specifier [opt] template-id
17355 Upon return BASES is initialized to the list of base classes (or
17356 NULL, if there are none) in the same form returned by
17357 cp_parser_base_clause.
17359 Returns the TYPE of the indicated class. Sets
17360 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17361 involving a nested-name-specifier was used, and FALSE otherwise.
17363 Returns error_mark_node if this is not a class-head.
17365 Returns NULL_TREE if the class-head is syntactically valid, but
17366 semantically invalid in a way that means we should skip the entire
17367 body of the class. */
17370 cp_parser_class_head (cp_parser* parser,
17371 bool* nested_name_specifier_p,
17372 tree *attributes_p,
17375 tree nested_name_specifier;
17376 enum tag_types class_key;
17377 tree id = NULL_TREE;
17378 tree type = NULL_TREE;
17380 cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17381 bool template_id_p = false;
17382 bool qualified_p = false;
17383 bool invalid_nested_name_p = false;
17384 bool invalid_explicit_specialization_p = false;
17385 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17386 tree pushed_scope = NULL_TREE;
17387 unsigned num_templates;
17388 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17389 /* Assume no nested-name-specifier will be present. */
17390 *nested_name_specifier_p = false;
17391 /* Assume no template parameter lists will be used in defining the
17394 parser->colon_corrects_to_scope_p = false;
17396 *bases = NULL_TREE;
17398 /* Look for the class-key. */
17399 class_key = cp_parser_class_key (parser);
17400 if (class_key == none_type)
17401 return error_mark_node;
17403 /* Parse the attributes. */
17404 attributes = cp_parser_attributes_opt (parser);
17406 /* If the next token is `::', that is invalid -- but sometimes
17407 people do try to write:
17411 Handle this gracefully by accepting the extra qualifier, and then
17412 issuing an error about it later if this really is a
17413 class-head. If it turns out just to be an elaborated type
17414 specifier, remain silent. */
17415 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17416 qualified_p = true;
17418 push_deferring_access_checks (dk_no_check);
17420 /* Determine the name of the class. Begin by looking for an
17421 optional nested-name-specifier. */
17422 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17423 nested_name_specifier
17424 = cp_parser_nested_name_specifier_opt (parser,
17425 /*typename_keyword_p=*/false,
17426 /*check_dependency_p=*/false,
17428 /*is_declaration=*/false);
17429 /* If there was a nested-name-specifier, then there *must* be an
17431 if (nested_name_specifier)
17433 type_start_token = cp_lexer_peek_token (parser->lexer);
17434 /* Although the grammar says `identifier', it really means
17435 `class-name' or `template-name'. You are only allowed to
17436 define a class that has already been declared with this
17439 The proposed resolution for Core Issue 180 says that wherever
17440 you see `class T::X' you should treat `X' as a type-name.
17442 It is OK to define an inaccessible class; for example:
17444 class A { class B; };
17447 We do not know if we will see a class-name, or a
17448 template-name. We look for a class-name first, in case the
17449 class-name is a template-id; if we looked for the
17450 template-name first we would stop after the template-name. */
17451 cp_parser_parse_tentatively (parser);
17452 type = cp_parser_class_name (parser,
17453 /*typename_keyword_p=*/false,
17454 /*template_keyword_p=*/false,
17456 /*check_dependency_p=*/false,
17457 /*class_head_p=*/true,
17458 /*is_declaration=*/false);
17459 /* If that didn't work, ignore the nested-name-specifier. */
17460 if (!cp_parser_parse_definitely (parser))
17462 invalid_nested_name_p = true;
17463 type_start_token = cp_lexer_peek_token (parser->lexer);
17464 id = cp_parser_identifier (parser);
17465 if (id == error_mark_node)
17468 /* If we could not find a corresponding TYPE, treat this
17469 declaration like an unqualified declaration. */
17470 if (type == error_mark_node)
17471 nested_name_specifier = NULL_TREE;
17472 /* Otherwise, count the number of templates used in TYPE and its
17473 containing scopes. */
17478 for (scope = TREE_TYPE (type);
17479 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17480 scope = (TYPE_P (scope)
17481 ? TYPE_CONTEXT (scope)
17482 : DECL_CONTEXT (scope)))
17484 && CLASS_TYPE_P (scope)
17485 && CLASSTYPE_TEMPLATE_INFO (scope)
17486 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17487 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17491 /* Otherwise, the identifier is optional. */
17494 /* We don't know whether what comes next is a template-id,
17495 an identifier, or nothing at all. */
17496 cp_parser_parse_tentatively (parser);
17497 /* Check for a template-id. */
17498 type_start_token = cp_lexer_peek_token (parser->lexer);
17499 id = cp_parser_template_id (parser,
17500 /*template_keyword_p=*/false,
17501 /*check_dependency_p=*/true,
17502 /*is_declaration=*/true);
17503 /* If that didn't work, it could still be an identifier. */
17504 if (!cp_parser_parse_definitely (parser))
17506 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17508 type_start_token = cp_lexer_peek_token (parser->lexer);
17509 id = cp_parser_identifier (parser);
17516 template_id_p = true;
17521 pop_deferring_access_checks ();
17525 cp_parser_check_for_invalid_template_id (parser, id,
17526 type_start_token->location);
17527 virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17530 /* If it's not a `:' or a `{' then we can't really be looking at a
17531 class-head, since a class-head only appears as part of a
17532 class-specifier. We have to detect this situation before calling
17533 xref_tag, since that has irreversible side-effects. */
17534 if (!cp_parser_next_token_starts_class_definition_p (parser))
17536 cp_parser_error (parser, "expected %<{%> or %<:%>");
17537 type = error_mark_node;
17541 /* At this point, we're going ahead with the class-specifier, even
17542 if some other problem occurs. */
17543 cp_parser_commit_to_tentative_parse (parser);
17544 if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17546 cp_parser_error (parser,
17547 "cannot specify %<override%> for a class");
17548 type = error_mark_node;
17551 /* Issue the error about the overly-qualified name now. */
17554 cp_parser_error (parser,
17555 "global qualification of class name is invalid");
17556 type = error_mark_node;
17559 else if (invalid_nested_name_p)
17561 cp_parser_error (parser,
17562 "qualified name does not name a class");
17563 type = error_mark_node;
17566 else if (nested_name_specifier)
17570 /* Reject typedef-names in class heads. */
17571 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17573 error_at (type_start_token->location,
17574 "invalid class name in declaration of %qD",
17580 /* Figure out in what scope the declaration is being placed. */
17581 scope = current_scope ();
17582 /* If that scope does not contain the scope in which the
17583 class was originally declared, the program is invalid. */
17584 if (scope && !is_ancestor (scope, nested_name_specifier))
17586 if (at_namespace_scope_p ())
17587 error_at (type_start_token->location,
17588 "declaration of %qD in namespace %qD which does not "
17590 type, scope, nested_name_specifier);
17592 error_at (type_start_token->location,
17593 "declaration of %qD in %qD which does not enclose %qD",
17594 type, scope, nested_name_specifier);
17600 A declarator-id shall not be qualified except for the
17601 definition of a ... nested class outside of its class
17602 ... [or] the definition or explicit instantiation of a
17603 class member of a namespace outside of its namespace. */
17604 if (scope == nested_name_specifier)
17606 permerror (nested_name_specifier_token_start->location,
17607 "extra qualification not allowed");
17608 nested_name_specifier = NULL_TREE;
17612 /* An explicit-specialization must be preceded by "template <>". If
17613 it is not, try to recover gracefully. */
17614 if (at_namespace_scope_p ()
17615 && parser->num_template_parameter_lists == 0
17618 error_at (type_start_token->location,
17619 "an explicit specialization must be preceded by %<template <>%>");
17620 invalid_explicit_specialization_p = true;
17621 /* Take the same action that would have been taken by
17622 cp_parser_explicit_specialization. */
17623 ++parser->num_template_parameter_lists;
17624 begin_specialization ();
17626 /* There must be no "return" statements between this point and the
17627 end of this function; set "type "to the correct return value and
17628 use "goto done;" to return. */
17629 /* Make sure that the right number of template parameters were
17631 if (!cp_parser_check_template_parameters (parser, num_templates,
17632 type_start_token->location,
17633 /*declarator=*/NULL))
17635 /* If something went wrong, there is no point in even trying to
17636 process the class-definition. */
17641 /* Look up the type. */
17644 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17645 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17646 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17648 error_at (type_start_token->location,
17649 "function template %qD redeclared as a class template", id);
17650 type = error_mark_node;
17654 type = TREE_TYPE (id);
17655 type = maybe_process_partial_specialization (type);
17657 if (nested_name_specifier)
17658 pushed_scope = push_scope (nested_name_specifier);
17660 else if (nested_name_specifier)
17666 template <typename T> struct S { struct T };
17667 template <typename T> struct S<T>::T { };
17669 we will get a TYPENAME_TYPE when processing the definition of
17670 `S::T'. We need to resolve it to the actual type before we
17671 try to define it. */
17672 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17674 class_type = resolve_typename_type (TREE_TYPE (type),
17675 /*only_current_p=*/false);
17676 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17677 type = TYPE_NAME (class_type);
17680 cp_parser_error (parser, "could not resolve typename type");
17681 type = error_mark_node;
17685 if (maybe_process_partial_specialization (TREE_TYPE (type))
17686 == error_mark_node)
17692 class_type = current_class_type;
17693 /* Enter the scope indicated by the nested-name-specifier. */
17694 pushed_scope = push_scope (nested_name_specifier);
17695 /* Get the canonical version of this type. */
17696 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17697 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17698 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17700 type = push_template_decl (type);
17701 if (type == error_mark_node)
17708 type = TREE_TYPE (type);
17709 *nested_name_specifier_p = true;
17711 else /* The name is not a nested name. */
17713 /* If the class was unnamed, create a dummy name. */
17715 id = make_anon_name ();
17716 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17717 parser->num_template_parameter_lists);
17720 /* Indicate whether this class was declared as a `class' or as a
17722 if (TREE_CODE (type) == RECORD_TYPE)
17723 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17724 cp_parser_check_class_key (class_key, type);
17726 /* If this type was already complete, and we see another definition,
17727 that's an error. */
17728 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17730 error_at (type_start_token->location, "redefinition of %q#T",
17732 error_at (type_start_token->location, "previous definition of %q+#T",
17737 else if (type == error_mark_node)
17740 /* We will have entered the scope containing the class; the names of
17741 base classes should be looked up in that context. For example:
17743 struct A { struct B {}; struct C; };
17744 struct A::C : B {};
17748 /* Get the list of base-classes, if there is one. */
17749 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17750 *bases = cp_parser_base_clause (parser);
17753 /* Leave the scope given by the nested-name-specifier. We will
17754 enter the class scope itself while processing the members. */
17756 pop_scope (pushed_scope);
17758 if (invalid_explicit_specialization_p)
17760 end_specialization ();
17761 --parser->num_template_parameter_lists;
17765 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17766 *attributes_p = attributes;
17767 if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17768 CLASSTYPE_FINAL (type) = 1;
17770 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17774 /* Parse a class-key.
17781 Returns the kind of class-key specified, or none_type to indicate
17784 static enum tag_types
17785 cp_parser_class_key (cp_parser* parser)
17788 enum tag_types tag_type;
17790 /* Look for the class-key. */
17791 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17795 /* Check to see if the TOKEN is a class-key. */
17796 tag_type = cp_parser_token_is_class_key (token);
17798 cp_parser_error (parser, "expected class-key");
17802 /* Parse an (optional) member-specification.
17804 member-specification:
17805 member-declaration member-specification [opt]
17806 access-specifier : member-specification [opt] */
17809 cp_parser_member_specification_opt (cp_parser* parser)
17816 /* Peek at the next token. */
17817 token = cp_lexer_peek_token (parser->lexer);
17818 /* If it's a `}', or EOF then we've seen all the members. */
17819 if (token->type == CPP_CLOSE_BRACE
17820 || token->type == CPP_EOF
17821 || token->type == CPP_PRAGMA_EOL)
17824 /* See if this token is a keyword. */
17825 keyword = token->keyword;
17829 case RID_PROTECTED:
17831 /* Consume the access-specifier. */
17832 cp_lexer_consume_token (parser->lexer);
17833 /* Remember which access-specifier is active. */
17834 current_access_specifier = token->u.value;
17835 /* Look for the `:'. */
17836 cp_parser_require (parser, CPP_COLON, RT_COLON);
17840 /* Accept #pragmas at class scope. */
17841 if (token->type == CPP_PRAGMA)
17843 cp_parser_pragma (parser, pragma_external);
17847 /* Otherwise, the next construction must be a
17848 member-declaration. */
17849 cp_parser_member_declaration (parser);
17854 /* Parse a member-declaration.
17856 member-declaration:
17857 decl-specifier-seq [opt] member-declarator-list [opt] ;
17858 function-definition ; [opt]
17859 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17861 template-declaration
17863 member-declarator-list:
17865 member-declarator-list , member-declarator
17868 declarator pure-specifier [opt]
17869 declarator constant-initializer [opt]
17870 identifier [opt] : constant-expression
17874 member-declaration:
17875 __extension__ member-declaration
17878 declarator attributes [opt] pure-specifier [opt]
17879 declarator attributes [opt] constant-initializer [opt]
17880 identifier [opt] attributes [opt] : constant-expression
17884 member-declaration:
17885 static_assert-declaration */
17888 cp_parser_member_declaration (cp_parser* parser)
17890 cp_decl_specifier_seq decl_specifiers;
17891 tree prefix_attributes;
17893 int declares_class_or_enum;
17895 cp_token *token = NULL;
17896 cp_token *decl_spec_token_start = NULL;
17897 cp_token *initializer_token_start = NULL;
17898 int saved_pedantic;
17899 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17901 /* Check for the `__extension__' keyword. */
17902 if (cp_parser_extension_opt (parser, &saved_pedantic))
17905 cp_parser_member_declaration (parser);
17906 /* Restore the old value of the PEDANTIC flag. */
17907 pedantic = saved_pedantic;
17912 /* Check for a template-declaration. */
17913 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17915 /* An explicit specialization here is an error condition, and we
17916 expect the specialization handler to detect and report this. */
17917 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17918 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17919 cp_parser_explicit_specialization (parser);
17921 cp_parser_template_declaration (parser, /*member_p=*/true);
17926 /* Check for a using-declaration. */
17927 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17929 /* Parse the using-declaration. */
17930 cp_parser_using_declaration (parser,
17931 /*access_declaration_p=*/false);
17935 /* Check for @defs. */
17936 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17939 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17940 ivar = ivar_chains;
17944 ivar = TREE_CHAIN (member);
17945 TREE_CHAIN (member) = NULL_TREE;
17946 finish_member_declaration (member);
17951 /* If the next token is `static_assert' we have a static assertion. */
17952 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17954 cp_parser_static_assert (parser, /*member_p=*/true);
17958 parser->colon_corrects_to_scope_p = false;
17960 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17963 /* Parse the decl-specifier-seq. */
17964 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17965 cp_parser_decl_specifier_seq (parser,
17966 CP_PARSER_FLAGS_OPTIONAL,
17968 &declares_class_or_enum);
17969 prefix_attributes = decl_specifiers.attributes;
17970 decl_specifiers.attributes = NULL_TREE;
17971 /* Check for an invalid type-name. */
17972 if (!decl_specifiers.any_type_specifiers_p
17973 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17975 /* If there is no declarator, then the decl-specifier-seq should
17977 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17979 /* If there was no decl-specifier-seq, and the next token is a
17980 `;', then we have something like:
17986 Each member-declaration shall declare at least one member
17987 name of the class. */
17988 if (!decl_specifiers.any_specifiers_p)
17990 cp_token *token = cp_lexer_peek_token (parser->lexer);
17991 if (!in_system_header_at (token->location))
17992 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17998 /* See if this declaration is a friend. */
17999 friend_p = cp_parser_friend_p (&decl_specifiers);
18000 /* If there were decl-specifiers, check to see if there was
18001 a class-declaration. */
18002 type = check_tag_decl (&decl_specifiers);
18003 /* Nested classes have already been added to the class, but
18004 a `friend' needs to be explicitly registered. */
18007 /* If the `friend' keyword was present, the friend must
18008 be introduced with a class-key. */
18009 if (!declares_class_or_enum && cxx_dialect < cxx0x)
18010 pedwarn (decl_spec_token_start->location, OPT_pedantic,
18011 "in C++03 a class-key must be used "
18012 "when declaring a friend");
18015 template <typename T> struct A {
18016 friend struct A<T>::B;
18019 A<T>::B will be represented by a TYPENAME_TYPE, and
18020 therefore not recognized by check_tag_decl. */
18023 type = decl_specifiers.type;
18024 if (type && TREE_CODE (type) == TYPE_DECL)
18025 type = TREE_TYPE (type);
18027 if (!type || !TYPE_P (type))
18028 error_at (decl_spec_token_start->location,
18029 "friend declaration does not name a class or "
18032 make_friend_class (current_class_type, type,
18033 /*complain=*/true);
18035 /* If there is no TYPE, an error message will already have
18037 else if (!type || type == error_mark_node)
18039 /* An anonymous aggregate has to be handled specially; such
18040 a declaration really declares a data member (with a
18041 particular type), as opposed to a nested class. */
18042 else if (ANON_AGGR_TYPE_P (type))
18044 /* Remove constructors and such from TYPE, now that we
18045 know it is an anonymous aggregate. */
18046 fixup_anonymous_aggr (type);
18047 /* And make the corresponding data member. */
18048 decl = build_decl (decl_spec_token_start->location,
18049 FIELD_DECL, NULL_TREE, type);
18050 /* Add it to the class. */
18051 finish_member_declaration (decl);
18054 cp_parser_check_access_in_redeclaration
18056 decl_spec_token_start->location);
18061 bool assume_semicolon = false;
18063 /* See if these declarations will be friends. */
18064 friend_p = cp_parser_friend_p (&decl_specifiers);
18066 /* Keep going until we hit the `;' at the end of the
18068 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18070 tree attributes = NULL_TREE;
18071 tree first_attribute;
18073 /* Peek at the next token. */
18074 token = cp_lexer_peek_token (parser->lexer);
18076 /* Check for a bitfield declaration. */
18077 if (token->type == CPP_COLON
18078 || (token->type == CPP_NAME
18079 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18085 /* Get the name of the bitfield. Note that we cannot just
18086 check TOKEN here because it may have been invalidated by
18087 the call to cp_lexer_peek_nth_token above. */
18088 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18089 identifier = cp_parser_identifier (parser);
18091 identifier = NULL_TREE;
18093 /* Consume the `:' token. */
18094 cp_lexer_consume_token (parser->lexer);
18095 /* Get the width of the bitfield. */
18097 = cp_parser_constant_expression (parser,
18098 /*allow_non_constant=*/false,
18101 /* Look for attributes that apply to the bitfield. */
18102 attributes = cp_parser_attributes_opt (parser);
18103 /* Remember which attributes are prefix attributes and
18105 first_attribute = attributes;
18106 /* Combine the attributes. */
18107 attributes = chainon (prefix_attributes, attributes);
18109 /* Create the bitfield declaration. */
18110 decl = grokbitfield (identifier
18111 ? make_id_declarator (NULL_TREE,
18121 cp_declarator *declarator;
18123 tree asm_specification;
18124 int ctor_dtor_or_conv_p;
18126 /* Parse the declarator. */
18128 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18129 &ctor_dtor_or_conv_p,
18130 /*parenthesized_p=*/NULL,
18131 /*member_p=*/true);
18133 /* If something went wrong parsing the declarator, make sure
18134 that we at least consume some tokens. */
18135 if (declarator == cp_error_declarator)
18137 /* Skip to the end of the statement. */
18138 cp_parser_skip_to_end_of_statement (parser);
18139 /* If the next token is not a semicolon, that is
18140 probably because we just skipped over the body of
18141 a function. So, we consume a semicolon if
18142 present, but do not issue an error message if it
18144 if (cp_lexer_next_token_is (parser->lexer,
18146 cp_lexer_consume_token (parser->lexer);
18150 if (declares_class_or_enum & 2)
18151 cp_parser_check_for_definition_in_return_type
18152 (declarator, decl_specifiers.type,
18153 decl_specifiers.type_location);
18155 /* Look for an asm-specification. */
18156 asm_specification = cp_parser_asm_specification_opt (parser);
18157 /* Look for attributes that apply to the declaration. */
18158 attributes = cp_parser_attributes_opt (parser);
18159 /* Remember which attributes are prefix attributes and
18161 first_attribute = attributes;
18162 /* Combine the attributes. */
18163 attributes = chainon (prefix_attributes, attributes);
18165 /* If it's an `=', then we have a constant-initializer or a
18166 pure-specifier. It is not correct to parse the
18167 initializer before registering the member declaration
18168 since the member declaration should be in scope while
18169 its initializer is processed. However, the rest of the
18170 front end does not yet provide an interface that allows
18171 us to handle this correctly. */
18172 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18176 A pure-specifier shall be used only in the declaration of
18177 a virtual function.
18179 A member-declarator can contain a constant-initializer
18180 only if it declares a static member of integral or
18183 Therefore, if the DECLARATOR is for a function, we look
18184 for a pure-specifier; otherwise, we look for a
18185 constant-initializer. When we call `grokfield', it will
18186 perform more stringent semantics checks. */
18187 initializer_token_start = cp_lexer_peek_token (parser->lexer);
18188 if (function_declarator_p (declarator))
18189 initializer = cp_parser_pure_specifier (parser);
18190 else if (cxx_dialect >= cxx0x)
18193 /* Don't require a constant rvalue in C++11, since we
18194 might want a reference constant. We'll enforce
18195 constancy later. */
18196 cp_lexer_consume_token (parser->lexer);
18197 /* Parse the initializer. */
18198 initializer = cp_parser_initializer_clause (parser,
18202 /* Parse the initializer. */
18203 initializer = cp_parser_constant_initializer (parser);
18205 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18206 && !function_declarator_p (declarator))
18209 initializer = cp_parser_initializer (parser, &x, &x);
18211 /* Otherwise, there is no initializer. */
18213 initializer = NULL_TREE;
18215 /* See if we are probably looking at a function
18216 definition. We are certainly not looking at a
18217 member-declarator. Calling `grokfield' has
18218 side-effects, so we must not do it unless we are sure
18219 that we are looking at a member-declarator. */
18220 if (cp_parser_token_starts_function_definition_p
18221 (cp_lexer_peek_token (parser->lexer)))
18223 /* The grammar does not allow a pure-specifier to be
18224 used when a member function is defined. (It is
18225 possible that this fact is an oversight in the
18226 standard, since a pure function may be defined
18227 outside of the class-specifier. */
18229 error_at (initializer_token_start->location,
18230 "pure-specifier on function-definition");
18231 decl = cp_parser_save_member_function_body (parser,
18235 /* If the member was not a friend, declare it here. */
18237 finish_member_declaration (decl);
18238 /* Peek at the next token. */
18239 token = cp_lexer_peek_token (parser->lexer);
18240 /* If the next token is a semicolon, consume it. */
18241 if (token->type == CPP_SEMICOLON)
18242 cp_lexer_consume_token (parser->lexer);
18246 if (declarator->kind == cdk_function)
18247 declarator->id_loc = token->location;
18248 /* Create the declaration. */
18249 decl = grokfield (declarator, &decl_specifiers,
18250 initializer, /*init_const_expr_p=*/true,
18255 /* Reset PREFIX_ATTRIBUTES. */
18256 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18257 attributes = TREE_CHAIN (attributes);
18259 TREE_CHAIN (attributes) = NULL_TREE;
18261 /* If there is any qualification still in effect, clear it
18262 now; we will be starting fresh with the next declarator. */
18263 parser->scope = NULL_TREE;
18264 parser->qualifying_scope = NULL_TREE;
18265 parser->object_scope = NULL_TREE;
18266 /* If it's a `,', then there are more declarators. */
18267 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18268 cp_lexer_consume_token (parser->lexer);
18269 /* If the next token isn't a `;', then we have a parse error. */
18270 else if (cp_lexer_next_token_is_not (parser->lexer,
18273 /* The next token might be a ways away from where the
18274 actual semicolon is missing. Find the previous token
18275 and use that for our error position. */
18276 cp_token *token = cp_lexer_previous_token (parser->lexer);
18277 error_at (token->location,
18278 "expected %<;%> at end of member declaration");
18280 /* Assume that the user meant to provide a semicolon. If
18281 we were to cp_parser_skip_to_end_of_statement, we might
18282 skip to a semicolon inside a member function definition
18283 and issue nonsensical error messages. */
18284 assume_semicolon = true;
18289 /* Add DECL to the list of members. */
18291 finish_member_declaration (decl);
18293 if (TREE_CODE (decl) == FUNCTION_DECL)
18294 cp_parser_save_default_args (parser, decl);
18297 if (assume_semicolon)
18302 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18304 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18307 /* Parse a pure-specifier.
18312 Returns INTEGER_ZERO_NODE if a pure specifier is found.
18313 Otherwise, ERROR_MARK_NODE is returned. */
18316 cp_parser_pure_specifier (cp_parser* parser)
18320 /* Look for the `=' token. */
18321 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18322 return error_mark_node;
18323 /* Look for the `0' token. */
18324 token = cp_lexer_peek_token (parser->lexer);
18326 if (token->type == CPP_EOF
18327 || token->type == CPP_PRAGMA_EOL)
18328 return error_mark_node;
18330 cp_lexer_consume_token (parser->lexer);
18332 /* Accept = default or = delete in c++0x mode. */
18333 if (token->keyword == RID_DEFAULT
18334 || token->keyword == RID_DELETE)
18336 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18337 return token->u.value;
18340 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
18341 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18343 cp_parser_error (parser,
18344 "invalid pure specifier (only %<= 0%> is allowed)");
18345 cp_parser_skip_to_end_of_statement (parser);
18346 return error_mark_node;
18348 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18350 error_at (token->location, "templates may not be %<virtual%>");
18351 return error_mark_node;
18354 return integer_zero_node;
18357 /* Parse a constant-initializer.
18359 constant-initializer:
18360 = constant-expression
18362 Returns a representation of the constant-expression. */
18365 cp_parser_constant_initializer (cp_parser* parser)
18367 /* Look for the `=' token. */
18368 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18369 return error_mark_node;
18371 /* It is invalid to write:
18373 struct S { static const int i = { 7 }; };
18376 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18378 cp_parser_error (parser,
18379 "a brace-enclosed initializer is not allowed here");
18380 /* Consume the opening brace. */
18381 cp_lexer_consume_token (parser->lexer);
18382 /* Skip the initializer. */
18383 cp_parser_skip_to_closing_brace (parser);
18384 /* Look for the trailing `}'. */
18385 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18387 return error_mark_node;
18390 return cp_parser_constant_expression (parser,
18391 /*allow_non_constant=*/false,
18395 /* Derived classes [gram.class.derived] */
18397 /* Parse a base-clause.
18400 : base-specifier-list
18402 base-specifier-list:
18403 base-specifier ... [opt]
18404 base-specifier-list , base-specifier ... [opt]
18406 Returns a TREE_LIST representing the base-classes, in the order in
18407 which they were declared. The representation of each node is as
18408 described by cp_parser_base_specifier.
18410 In the case that no bases are specified, this function will return
18411 NULL_TREE, not ERROR_MARK_NODE. */
18414 cp_parser_base_clause (cp_parser* parser)
18416 tree bases = NULL_TREE;
18418 /* Look for the `:' that begins the list. */
18419 cp_parser_require (parser, CPP_COLON, RT_COLON);
18421 /* Scan the base-specifier-list. */
18426 bool pack_expansion_p = false;
18428 /* Look for the base-specifier. */
18429 base = cp_parser_base_specifier (parser);
18430 /* Look for the (optional) ellipsis. */
18431 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18433 /* Consume the `...'. */
18434 cp_lexer_consume_token (parser->lexer);
18436 pack_expansion_p = true;
18439 /* Add BASE to the front of the list. */
18440 if (base && base != error_mark_node)
18442 if (pack_expansion_p)
18443 /* Make this a pack expansion type. */
18444 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18446 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18448 TREE_CHAIN (base) = bases;
18452 /* Peek at the next token. */
18453 token = cp_lexer_peek_token (parser->lexer);
18454 /* If it's not a comma, then the list is complete. */
18455 if (token->type != CPP_COMMA)
18457 /* Consume the `,'. */
18458 cp_lexer_consume_token (parser->lexer);
18461 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18462 base class had a qualified name. However, the next name that
18463 appears is certainly not qualified. */
18464 parser->scope = NULL_TREE;
18465 parser->qualifying_scope = NULL_TREE;
18466 parser->object_scope = NULL_TREE;
18468 return nreverse (bases);
18471 /* Parse a base-specifier.
18474 :: [opt] nested-name-specifier [opt] class-name
18475 virtual access-specifier [opt] :: [opt] nested-name-specifier
18477 access-specifier virtual [opt] :: [opt] nested-name-specifier
18480 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18481 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18482 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18483 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18486 cp_parser_base_specifier (cp_parser* parser)
18490 bool virtual_p = false;
18491 bool duplicate_virtual_error_issued_p = false;
18492 bool duplicate_access_error_issued_p = false;
18493 bool class_scope_p, template_p;
18494 tree access = access_default_node;
18497 /* Process the optional `virtual' and `access-specifier'. */
18500 /* Peek at the next token. */
18501 token = cp_lexer_peek_token (parser->lexer);
18502 /* Process `virtual'. */
18503 switch (token->keyword)
18506 /* If `virtual' appears more than once, issue an error. */
18507 if (virtual_p && !duplicate_virtual_error_issued_p)
18509 cp_parser_error (parser,
18510 "%<virtual%> specified more than once in base-specified");
18511 duplicate_virtual_error_issued_p = true;
18516 /* Consume the `virtual' token. */
18517 cp_lexer_consume_token (parser->lexer);
18522 case RID_PROTECTED:
18524 /* If more than one access specifier appears, issue an
18526 if (access != access_default_node
18527 && !duplicate_access_error_issued_p)
18529 cp_parser_error (parser,
18530 "more than one access specifier in base-specified");
18531 duplicate_access_error_issued_p = true;
18534 access = ridpointers[(int) token->keyword];
18536 /* Consume the access-specifier. */
18537 cp_lexer_consume_token (parser->lexer);
18546 /* It is not uncommon to see programs mechanically, erroneously, use
18547 the 'typename' keyword to denote (dependent) qualified types
18548 as base classes. */
18549 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18551 token = cp_lexer_peek_token (parser->lexer);
18552 if (!processing_template_decl)
18553 error_at (token->location,
18554 "keyword %<typename%> not allowed outside of templates");
18556 error_at (token->location,
18557 "keyword %<typename%> not allowed in this context "
18558 "(the base class is implicitly a type)");
18559 cp_lexer_consume_token (parser->lexer);
18562 /* Look for the optional `::' operator. */
18563 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18564 /* Look for the nested-name-specifier. The simplest way to
18569 The keyword `typename' is not permitted in a base-specifier or
18570 mem-initializer; in these contexts a qualified name that
18571 depends on a template-parameter is implicitly assumed to be a
18574 is to pretend that we have seen the `typename' keyword at this
18576 cp_parser_nested_name_specifier_opt (parser,
18577 /*typename_keyword_p=*/true,
18578 /*check_dependency_p=*/true,
18580 /*is_declaration=*/true);
18581 /* If the base class is given by a qualified name, assume that names
18582 we see are type names or templates, as appropriate. */
18583 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18584 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18587 && cp_lexer_next_token_is_decltype (parser->lexer))
18588 /* DR 950 allows decltype as a base-specifier. */
18589 type = cp_parser_decltype (parser);
18592 /* Otherwise, look for the class-name. */
18593 type = cp_parser_class_name (parser,
18597 /*check_dependency_p=*/true,
18598 /*class_head_p=*/false,
18599 /*is_declaration=*/true);
18600 type = TREE_TYPE (type);
18603 if (type == error_mark_node)
18604 return error_mark_node;
18606 return finish_base_specifier (type, access, virtual_p);
18609 /* Exception handling [gram.exception] */
18611 /* Parse an (optional) exception-specification.
18613 exception-specification:
18614 throw ( type-id-list [opt] )
18616 Returns a TREE_LIST representing the exception-specification. The
18617 TREE_VALUE of each node is a type. */
18620 cp_parser_exception_specification_opt (cp_parser* parser)
18624 const char *saved_message;
18626 /* Peek at the next token. */
18627 token = cp_lexer_peek_token (parser->lexer);
18629 /* Is it a noexcept-specification? */
18630 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18633 cp_lexer_consume_token (parser->lexer);
18635 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18637 cp_lexer_consume_token (parser->lexer);
18639 /* Types may not be defined in an exception-specification. */
18640 saved_message = parser->type_definition_forbidden_message;
18641 parser->type_definition_forbidden_message
18642 = G_("types may not be defined in an exception-specification");
18644 expr = cp_parser_constant_expression (parser, false, NULL);
18646 /* Restore the saved message. */
18647 parser->type_definition_forbidden_message = saved_message;
18649 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18652 expr = boolean_true_node;
18654 return build_noexcept_spec (expr, tf_warning_or_error);
18657 /* If it's not `throw', then there's no exception-specification. */
18658 if (!cp_parser_is_keyword (token, RID_THROW))
18662 /* Enable this once a lot of code has transitioned to noexcept? */
18663 if (cxx_dialect == cxx0x && !in_system_header)
18664 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18665 "deprecated in C++0x; use %<noexcept%> instead");
18668 /* Consume the `throw'. */
18669 cp_lexer_consume_token (parser->lexer);
18671 /* Look for the `('. */
18672 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18674 /* Peek at the next token. */
18675 token = cp_lexer_peek_token (parser->lexer);
18676 /* If it's not a `)', then there is a type-id-list. */
18677 if (token->type != CPP_CLOSE_PAREN)
18679 /* Types may not be defined in an exception-specification. */
18680 saved_message = parser->type_definition_forbidden_message;
18681 parser->type_definition_forbidden_message
18682 = G_("types may not be defined in an exception-specification");
18683 /* Parse the type-id-list. */
18684 type_id_list = cp_parser_type_id_list (parser);
18685 /* Restore the saved message. */
18686 parser->type_definition_forbidden_message = saved_message;
18689 type_id_list = empty_except_spec;
18691 /* Look for the `)'. */
18692 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18694 return type_id_list;
18697 /* Parse an (optional) type-id-list.
18701 type-id-list , type-id ... [opt]
18703 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18704 in the order that the types were presented. */
18707 cp_parser_type_id_list (cp_parser* parser)
18709 tree types = NULL_TREE;
18716 /* Get the next type-id. */
18717 type = cp_parser_type_id (parser);
18718 /* Parse the optional ellipsis. */
18719 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18721 /* Consume the `...'. */
18722 cp_lexer_consume_token (parser->lexer);
18724 /* Turn the type into a pack expansion expression. */
18725 type = make_pack_expansion (type);
18727 /* Add it to the list. */
18728 types = add_exception_specifier (types, type, /*complain=*/1);
18729 /* Peek at the next token. */
18730 token = cp_lexer_peek_token (parser->lexer);
18731 /* If it is not a `,', we are done. */
18732 if (token->type != CPP_COMMA)
18734 /* Consume the `,'. */
18735 cp_lexer_consume_token (parser->lexer);
18738 return nreverse (types);
18741 /* Parse a try-block.
18744 try compound-statement handler-seq */
18747 cp_parser_try_block (cp_parser* parser)
18751 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18752 try_block = begin_try_block ();
18753 cp_parser_compound_statement (parser, NULL, true, false);
18754 finish_try_block (try_block);
18755 cp_parser_handler_seq (parser);
18756 finish_handler_sequence (try_block);
18761 /* Parse a function-try-block.
18763 function-try-block:
18764 try ctor-initializer [opt] function-body handler-seq */
18767 cp_parser_function_try_block (cp_parser* parser)
18769 tree compound_stmt;
18771 bool ctor_initializer_p;
18773 /* Look for the `try' keyword. */
18774 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18776 /* Let the rest of the front end know where we are. */
18777 try_block = begin_function_try_block (&compound_stmt);
18778 /* Parse the function-body. */
18780 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18781 /* We're done with the `try' part. */
18782 finish_function_try_block (try_block);
18783 /* Parse the handlers. */
18784 cp_parser_handler_seq (parser);
18785 /* We're done with the handlers. */
18786 finish_function_handler_sequence (try_block, compound_stmt);
18788 return ctor_initializer_p;
18791 /* Parse a handler-seq.
18794 handler handler-seq [opt] */
18797 cp_parser_handler_seq (cp_parser* parser)
18803 /* Parse the handler. */
18804 cp_parser_handler (parser);
18805 /* Peek at the next token. */
18806 token = cp_lexer_peek_token (parser->lexer);
18807 /* If it's not `catch' then there are no more handlers. */
18808 if (!cp_parser_is_keyword (token, RID_CATCH))
18813 /* Parse a handler.
18816 catch ( exception-declaration ) compound-statement */
18819 cp_parser_handler (cp_parser* parser)
18824 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18825 handler = begin_handler ();
18826 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18827 declaration = cp_parser_exception_declaration (parser);
18828 finish_handler_parms (declaration, handler);
18829 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18830 cp_parser_compound_statement (parser, NULL, false, false);
18831 finish_handler (handler);
18834 /* Parse an exception-declaration.
18836 exception-declaration:
18837 type-specifier-seq declarator
18838 type-specifier-seq abstract-declarator
18842 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18843 ellipsis variant is used. */
18846 cp_parser_exception_declaration (cp_parser* parser)
18848 cp_decl_specifier_seq type_specifiers;
18849 cp_declarator *declarator;
18850 const char *saved_message;
18852 /* If it's an ellipsis, it's easy to handle. */
18853 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18855 /* Consume the `...' token. */
18856 cp_lexer_consume_token (parser->lexer);
18860 /* Types may not be defined in exception-declarations. */
18861 saved_message = parser->type_definition_forbidden_message;
18862 parser->type_definition_forbidden_message
18863 = G_("types may not be defined in exception-declarations");
18865 /* Parse the type-specifier-seq. */
18866 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18867 /*is_trailing_return=*/false,
18869 /* If it's a `)', then there is no declarator. */
18870 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18873 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18874 /*ctor_dtor_or_conv_p=*/NULL,
18875 /*parenthesized_p=*/NULL,
18876 /*member_p=*/false);
18878 /* Restore the saved message. */
18879 parser->type_definition_forbidden_message = saved_message;
18881 if (!type_specifiers.any_specifiers_p)
18882 return error_mark_node;
18884 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18887 /* Parse a throw-expression.
18890 throw assignment-expression [opt]
18892 Returns a THROW_EXPR representing the throw-expression. */
18895 cp_parser_throw_expression (cp_parser* parser)
18900 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18901 token = cp_lexer_peek_token (parser->lexer);
18902 /* Figure out whether or not there is an assignment-expression
18903 following the "throw" keyword. */
18904 if (token->type == CPP_COMMA
18905 || token->type == CPP_SEMICOLON
18906 || token->type == CPP_CLOSE_PAREN
18907 || token->type == CPP_CLOSE_SQUARE
18908 || token->type == CPP_CLOSE_BRACE
18909 || token->type == CPP_COLON)
18910 expression = NULL_TREE;
18912 expression = cp_parser_assignment_expression (parser,
18913 /*cast_p=*/false, NULL);
18915 return build_throw (expression);
18918 /* GNU Extensions */
18920 /* Parse an (optional) asm-specification.
18923 asm ( string-literal )
18925 If the asm-specification is present, returns a STRING_CST
18926 corresponding to the string-literal. Otherwise, returns
18930 cp_parser_asm_specification_opt (cp_parser* parser)
18933 tree asm_specification;
18935 /* Peek at the next token. */
18936 token = cp_lexer_peek_token (parser->lexer);
18937 /* If the next token isn't the `asm' keyword, then there's no
18938 asm-specification. */
18939 if (!cp_parser_is_keyword (token, RID_ASM))
18942 /* Consume the `asm' token. */
18943 cp_lexer_consume_token (parser->lexer);
18944 /* Look for the `('. */
18945 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18947 /* Look for the string-literal. */
18948 asm_specification = cp_parser_string_literal (parser, false, false);
18950 /* Look for the `)'. */
18951 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18953 return asm_specification;
18956 /* Parse an asm-operand-list.
18960 asm-operand-list , asm-operand
18963 string-literal ( expression )
18964 [ string-literal ] string-literal ( expression )
18966 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18967 each node is the expression. The TREE_PURPOSE is itself a
18968 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18969 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18970 is a STRING_CST for the string literal before the parenthesis. Returns
18971 ERROR_MARK_NODE if any of the operands are invalid. */
18974 cp_parser_asm_operand_list (cp_parser* parser)
18976 tree asm_operands = NULL_TREE;
18977 bool invalid_operands = false;
18981 tree string_literal;
18985 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18987 /* Consume the `[' token. */
18988 cp_lexer_consume_token (parser->lexer);
18989 /* Read the operand name. */
18990 name = cp_parser_identifier (parser);
18991 if (name != error_mark_node)
18992 name = build_string (IDENTIFIER_LENGTH (name),
18993 IDENTIFIER_POINTER (name));
18994 /* Look for the closing `]'. */
18995 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18999 /* Look for the string-literal. */
19000 string_literal = cp_parser_string_literal (parser, false, false);
19002 /* Look for the `('. */
19003 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19004 /* Parse the expression. */
19005 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19006 /* Look for the `)'. */
19007 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19009 if (name == error_mark_node
19010 || string_literal == error_mark_node
19011 || expression == error_mark_node)
19012 invalid_operands = true;
19014 /* Add this operand to the list. */
19015 asm_operands = tree_cons (build_tree_list (name, string_literal),
19018 /* If the next token is not a `,', there are no more
19020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19022 /* Consume the `,'. */
19023 cp_lexer_consume_token (parser->lexer);
19026 return invalid_operands ? error_mark_node : nreverse (asm_operands);
19029 /* Parse an asm-clobber-list.
19033 asm-clobber-list , string-literal
19035 Returns a TREE_LIST, indicating the clobbers in the order that they
19036 appeared. The TREE_VALUE of each node is a STRING_CST. */
19039 cp_parser_asm_clobber_list (cp_parser* parser)
19041 tree clobbers = NULL_TREE;
19045 tree string_literal;
19047 /* Look for the string literal. */
19048 string_literal = cp_parser_string_literal (parser, false, false);
19049 /* Add it to the list. */
19050 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19051 /* If the next token is not a `,', then the list is
19053 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19055 /* Consume the `,' token. */
19056 cp_lexer_consume_token (parser->lexer);
19062 /* Parse an asm-label-list.
19066 asm-label-list , identifier
19068 Returns a TREE_LIST, indicating the labels in the order that they
19069 appeared. The TREE_VALUE of each node is a label. */
19072 cp_parser_asm_label_list (cp_parser* parser)
19074 tree labels = NULL_TREE;
19078 tree identifier, label, name;
19080 /* Look for the identifier. */
19081 identifier = cp_parser_identifier (parser);
19082 if (!error_operand_p (identifier))
19084 label = lookup_label (identifier);
19085 if (TREE_CODE (label) == LABEL_DECL)
19087 TREE_USED (label) = 1;
19088 check_goto (label);
19089 name = build_string (IDENTIFIER_LENGTH (identifier),
19090 IDENTIFIER_POINTER (identifier));
19091 labels = tree_cons (name, label, labels);
19094 /* If the next token is not a `,', then the list is
19096 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19098 /* Consume the `,' token. */
19099 cp_lexer_consume_token (parser->lexer);
19102 return nreverse (labels);
19105 /* Parse an (optional) series of attributes.
19108 attributes attribute
19111 __attribute__ (( attribute-list [opt] ))
19113 The return value is as for cp_parser_attribute_list. */
19116 cp_parser_attributes_opt (cp_parser* parser)
19118 tree attributes = NULL_TREE;
19123 tree attribute_list;
19125 /* Peek at the next token. */
19126 token = cp_lexer_peek_token (parser->lexer);
19127 /* If it's not `__attribute__', then we're done. */
19128 if (token->keyword != RID_ATTRIBUTE)
19131 /* Consume the `__attribute__' keyword. */
19132 cp_lexer_consume_token (parser->lexer);
19133 /* Look for the two `(' tokens. */
19134 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19135 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19137 /* Peek at the next token. */
19138 token = cp_lexer_peek_token (parser->lexer);
19139 if (token->type != CPP_CLOSE_PAREN)
19140 /* Parse the attribute-list. */
19141 attribute_list = cp_parser_attribute_list (parser);
19143 /* If the next token is a `)', then there is no attribute
19145 attribute_list = NULL;
19147 /* Look for the two `)' tokens. */
19148 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19149 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19151 /* Add these new attributes to the list. */
19152 attributes = chainon (attributes, attribute_list);
19158 /* Parse an attribute-list.
19162 attribute-list , attribute
19166 identifier ( identifier )
19167 identifier ( identifier , expression-list )
19168 identifier ( expression-list )
19170 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
19171 to an attribute. The TREE_PURPOSE of each node is the identifier
19172 indicating which attribute is in use. The TREE_VALUE represents
19173 the arguments, if any. */
19176 cp_parser_attribute_list (cp_parser* parser)
19178 tree attribute_list = NULL_TREE;
19179 bool save_translate_strings_p = parser->translate_strings_p;
19181 parser->translate_strings_p = false;
19188 /* Look for the identifier. We also allow keywords here; for
19189 example `__attribute__ ((const))' is legal. */
19190 token = cp_lexer_peek_token (parser->lexer);
19191 if (token->type == CPP_NAME
19192 || token->type == CPP_KEYWORD)
19194 tree arguments = NULL_TREE;
19196 /* Consume the token. */
19197 token = cp_lexer_consume_token (parser->lexer);
19199 /* Save away the identifier that indicates which attribute
19201 identifier = (token->type == CPP_KEYWORD)
19202 /* For keywords, use the canonical spelling, not the
19203 parsed identifier. */
19204 ? ridpointers[(int) token->keyword]
19207 attribute = build_tree_list (identifier, NULL_TREE);
19209 /* Peek at the next token. */
19210 token = cp_lexer_peek_token (parser->lexer);
19211 /* If it's an `(', then parse the attribute arguments. */
19212 if (token->type == CPP_OPEN_PAREN)
19215 int attr_flag = (attribute_takes_identifier_p (identifier)
19216 ? id_attr : normal_attr);
19217 vec = cp_parser_parenthesized_expression_list
19218 (parser, attr_flag, /*cast_p=*/false,
19219 /*allow_expansion_p=*/false,
19220 /*non_constant_p=*/NULL);
19222 arguments = error_mark_node;
19225 arguments = build_tree_list_vec (vec);
19226 release_tree_vector (vec);
19228 /* Save the arguments away. */
19229 TREE_VALUE (attribute) = arguments;
19232 if (arguments != error_mark_node)
19234 /* Add this attribute to the list. */
19235 TREE_CHAIN (attribute) = attribute_list;
19236 attribute_list = attribute;
19239 token = cp_lexer_peek_token (parser->lexer);
19241 /* Now, look for more attributes. If the next token isn't a
19242 `,', we're done. */
19243 if (token->type != CPP_COMMA)
19246 /* Consume the comma and keep going. */
19247 cp_lexer_consume_token (parser->lexer);
19249 parser->translate_strings_p = save_translate_strings_p;
19251 /* We built up the list in reverse order. */
19252 return nreverse (attribute_list);
19255 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
19256 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
19257 current value of the PEDANTIC flag, regardless of whether or not
19258 the `__extension__' keyword is present. The caller is responsible
19259 for restoring the value of the PEDANTIC flag. */
19262 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19264 /* Save the old value of the PEDANTIC flag. */
19265 *saved_pedantic = pedantic;
19267 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19269 /* Consume the `__extension__' token. */
19270 cp_lexer_consume_token (parser->lexer);
19271 /* We're not being pedantic while the `__extension__' keyword is
19281 /* Parse a label declaration.
19284 __label__ label-declarator-seq ;
19286 label-declarator-seq:
19287 identifier , label-declarator-seq
19291 cp_parser_label_declaration (cp_parser* parser)
19293 /* Look for the `__label__' keyword. */
19294 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19300 /* Look for an identifier. */
19301 identifier = cp_parser_identifier (parser);
19302 /* If we failed, stop. */
19303 if (identifier == error_mark_node)
19305 /* Declare it as a label. */
19306 finish_label_decl (identifier);
19307 /* If the next token is a `;', stop. */
19308 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19310 /* Look for the `,' separating the label declarations. */
19311 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19314 /* Look for the final `;'. */
19315 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19318 /* Support Functions */
19320 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19321 NAME should have one of the representations used for an
19322 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19323 is returned. If PARSER->SCOPE is a dependent type, then a
19324 SCOPE_REF is returned.
19326 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19327 returned; the name was already resolved when the TEMPLATE_ID_EXPR
19328 was formed. Abstractly, such entities should not be passed to this
19329 function, because they do not need to be looked up, but it is
19330 simpler to check for this special case here, rather than at the
19333 In cases not explicitly covered above, this function returns a
19334 DECL, OVERLOAD, or baselink representing the result of the lookup.
19335 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19338 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19339 (e.g., "struct") that was used. In that case bindings that do not
19340 refer to types are ignored.
19342 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19345 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19348 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19351 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19352 TREE_LIST of candidates if name-lookup results in an ambiguity, and
19353 NULL_TREE otherwise. */
19356 cp_parser_lookup_name (cp_parser *parser, tree name,
19357 enum tag_types tag_type,
19360 bool check_dependency,
19361 tree *ambiguous_decls,
19362 location_t name_location)
19366 tree object_type = parser->context->object_type;
19368 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19369 flags |= LOOKUP_COMPLAIN;
19371 /* Assume that the lookup will be unambiguous. */
19372 if (ambiguous_decls)
19373 *ambiguous_decls = NULL_TREE;
19375 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19376 no longer valid. Note that if we are parsing tentatively, and
19377 the parse fails, OBJECT_TYPE will be automatically restored. */
19378 parser->context->object_type = NULL_TREE;
19380 if (name == error_mark_node)
19381 return error_mark_node;
19383 /* A template-id has already been resolved; there is no lookup to
19385 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19387 if (BASELINK_P (name))
19389 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19390 == TEMPLATE_ID_EXPR);
19394 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19395 it should already have been checked to make sure that the name
19396 used matches the type being destroyed. */
19397 if (TREE_CODE (name) == BIT_NOT_EXPR)
19401 /* Figure out to which type this destructor applies. */
19403 type = parser->scope;
19404 else if (object_type)
19405 type = object_type;
19407 type = current_class_type;
19408 /* If that's not a class type, there is no destructor. */
19409 if (!type || !CLASS_TYPE_P (type))
19410 return error_mark_node;
19411 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19412 lazily_declare_fn (sfk_destructor, type);
19413 if (!CLASSTYPE_DESTRUCTORS (type))
19414 return error_mark_node;
19415 /* If it was a class type, return the destructor. */
19416 return CLASSTYPE_DESTRUCTORS (type);
19419 /* By this point, the NAME should be an ordinary identifier. If
19420 the id-expression was a qualified name, the qualifying scope is
19421 stored in PARSER->SCOPE at this point. */
19422 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19424 /* Perform the lookup. */
19429 if (parser->scope == error_mark_node)
19430 return error_mark_node;
19432 /* If the SCOPE is dependent, the lookup must be deferred until
19433 the template is instantiated -- unless we are explicitly
19434 looking up names in uninstantiated templates. Even then, we
19435 cannot look up the name if the scope is not a class type; it
19436 might, for example, be a template type parameter. */
19437 dependent_p = (TYPE_P (parser->scope)
19438 && dependent_scope_p (parser->scope));
19439 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19441 /* Defer lookup. */
19442 decl = error_mark_node;
19445 tree pushed_scope = NULL_TREE;
19447 /* If PARSER->SCOPE is a dependent type, then it must be a
19448 class type, and we must not be checking dependencies;
19449 otherwise, we would have processed this lookup above. So
19450 that PARSER->SCOPE is not considered a dependent base by
19451 lookup_member, we must enter the scope here. */
19453 pushed_scope = push_scope (parser->scope);
19455 /* If the PARSER->SCOPE is a template specialization, it
19456 may be instantiated during name lookup. In that case,
19457 errors may be issued. Even if we rollback the current
19458 tentative parse, those errors are valid. */
19459 decl = lookup_qualified_name (parser->scope, name,
19460 tag_type != none_type,
19461 /*complain=*/true);
19463 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19464 lookup result and the nested-name-specifier nominates a class C:
19465 * if the name specified after the nested-name-specifier, when
19466 looked up in C, is the injected-class-name of C (Clause 9), or
19467 * if the name specified after the nested-name-specifier is the
19468 same as the identifier or the simple-template-id's template-
19469 name in the last component of the nested-name-specifier,
19470 the name is instead considered to name the constructor of
19471 class C. [ Note: for example, the constructor is not an
19472 acceptable lookup result in an elaborated-type-specifier so
19473 the constructor would not be used in place of the
19474 injected-class-name. --end note ] Such a constructor name
19475 shall be used only in the declarator-id of a declaration that
19476 names a constructor or in a using-declaration. */
19477 if (tag_type == none_type
19478 && DECL_SELF_REFERENCE_P (decl)
19479 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19480 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19481 tag_type != none_type,
19482 /*complain=*/true);
19484 /* If we have a single function from a using decl, pull it out. */
19485 if (TREE_CODE (decl) == OVERLOAD
19486 && !really_overloaded_fn (decl))
19487 decl = OVL_FUNCTION (decl);
19490 pop_scope (pushed_scope);
19493 /* If the scope is a dependent type and either we deferred lookup or
19494 we did lookup but didn't find the name, rememeber the name. */
19495 if (decl == error_mark_node && TYPE_P (parser->scope)
19496 && dependent_type_p (parser->scope))
19502 /* The resolution to Core Issue 180 says that `struct
19503 A::B' should be considered a type-name, even if `A'
19505 type = make_typename_type (parser->scope, name, tag_type,
19506 /*complain=*/tf_error);
19507 decl = TYPE_NAME (type);
19509 else if (is_template
19510 && (cp_parser_next_token_ends_template_argument_p (parser)
19511 || cp_lexer_next_token_is (parser->lexer,
19513 decl = make_unbound_class_template (parser->scope,
19515 /*complain=*/tf_error);
19517 decl = build_qualified_name (/*type=*/NULL_TREE,
19518 parser->scope, name,
19521 parser->qualifying_scope = parser->scope;
19522 parser->object_scope = NULL_TREE;
19524 else if (object_type)
19526 tree object_decl = NULL_TREE;
19527 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19528 OBJECT_TYPE is not a class. */
19529 if (CLASS_TYPE_P (object_type))
19530 /* If the OBJECT_TYPE is a template specialization, it may
19531 be instantiated during name lookup. In that case, errors
19532 may be issued. Even if we rollback the current tentative
19533 parse, those errors are valid. */
19534 object_decl = lookup_member (object_type,
19537 tag_type != none_type);
19538 /* Look it up in the enclosing context, too. */
19539 decl = lookup_name_real (name, tag_type != none_type,
19541 /*block_p=*/true, is_namespace, flags);
19542 parser->object_scope = object_type;
19543 parser->qualifying_scope = NULL_TREE;
19545 decl = object_decl;
19549 decl = lookup_name_real (name, tag_type != none_type,
19551 /*block_p=*/true, is_namespace, flags);
19552 parser->qualifying_scope = NULL_TREE;
19553 parser->object_scope = NULL_TREE;
19556 /* If the lookup failed, let our caller know. */
19557 if (!decl || decl == error_mark_node)
19558 return error_mark_node;
19560 /* Pull out the template from an injected-class-name (or multiple). */
19562 decl = maybe_get_template_decl_from_type_decl (decl);
19564 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19565 if (TREE_CODE (decl) == TREE_LIST)
19567 if (ambiguous_decls)
19568 *ambiguous_decls = decl;
19569 /* The error message we have to print is too complicated for
19570 cp_parser_error, so we incorporate its actions directly. */
19571 if (!cp_parser_simulate_error (parser))
19573 error_at (name_location, "reference to %qD is ambiguous",
19575 print_candidates (decl);
19577 return error_mark_node;
19580 gcc_assert (DECL_P (decl)
19581 || TREE_CODE (decl) == OVERLOAD
19582 || TREE_CODE (decl) == SCOPE_REF
19583 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19584 || BASELINK_P (decl));
19586 /* If we have resolved the name of a member declaration, check to
19587 see if the declaration is accessible. When the name resolves to
19588 set of overloaded functions, accessibility is checked when
19589 overload resolution is done.
19591 During an explicit instantiation, access is not checked at all,
19592 as per [temp.explicit]. */
19594 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19596 maybe_record_typedef_use (decl);
19601 /* Like cp_parser_lookup_name, but for use in the typical case where
19602 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19603 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19606 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19608 return cp_parser_lookup_name (parser, name,
19610 /*is_template=*/false,
19611 /*is_namespace=*/false,
19612 /*check_dependency=*/true,
19613 /*ambiguous_decls=*/NULL,
19617 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19618 the current context, return the TYPE_DECL. If TAG_NAME_P is
19619 true, the DECL indicates the class being defined in a class-head,
19620 or declared in an elaborated-type-specifier.
19622 Otherwise, return DECL. */
19625 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19627 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19628 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19631 template <typename T> struct B;
19634 template <typename T> struct A::B {};
19636 Similarly, in an elaborated-type-specifier:
19638 namespace N { struct X{}; }
19641 template <typename T> friend struct N::X;
19644 However, if the DECL refers to a class type, and we are in
19645 the scope of the class, then the name lookup automatically
19646 finds the TYPE_DECL created by build_self_reference rather
19647 than a TEMPLATE_DECL. For example, in:
19649 template <class T> struct S {
19653 there is no need to handle such case. */
19655 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19656 return DECL_TEMPLATE_RESULT (decl);
19661 /* If too many, or too few, template-parameter lists apply to the
19662 declarator, issue an error message. Returns TRUE if all went well,
19663 and FALSE otherwise. */
19666 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19667 cp_declarator *declarator,
19668 location_t declarator_location)
19670 unsigned num_templates;
19672 /* We haven't seen any classes that involve template parameters yet. */
19675 switch (declarator->kind)
19678 if (declarator->u.id.qualifying_scope)
19682 scope = declarator->u.id.qualifying_scope;
19684 while (scope && CLASS_TYPE_P (scope))
19686 /* You're supposed to have one `template <...>'
19687 for every template class, but you don't need one
19688 for a full specialization. For example:
19690 template <class T> struct S{};
19691 template <> struct S<int> { void f(); };
19692 void S<int>::f () {}
19694 is correct; there shouldn't be a `template <>' for
19695 the definition of `S<int>::f'. */
19696 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19697 /* If SCOPE does not have template information of any
19698 kind, then it is not a template, nor is it nested
19699 within a template. */
19701 if (explicit_class_specialization_p (scope))
19703 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19706 scope = TYPE_CONTEXT (scope);
19709 else if (TREE_CODE (declarator->u.id.unqualified_name)
19710 == TEMPLATE_ID_EXPR)
19711 /* If the DECLARATOR has the form `X<y>' then it uses one
19712 additional level of template parameters. */
19715 return cp_parser_check_template_parameters
19716 (parser, num_templates, declarator_location, declarator);
19722 case cdk_reference:
19724 return (cp_parser_check_declarator_template_parameters
19725 (parser, declarator->declarator, declarator_location));
19731 gcc_unreachable ();
19736 /* NUM_TEMPLATES were used in the current declaration. If that is
19737 invalid, return FALSE and issue an error messages. Otherwise,
19738 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19739 declarator and we can print more accurate diagnostics. */
19742 cp_parser_check_template_parameters (cp_parser* parser,
19743 unsigned num_templates,
19744 location_t location,
19745 cp_declarator *declarator)
19747 /* If there are the same number of template classes and parameter
19748 lists, that's OK. */
19749 if (parser->num_template_parameter_lists == num_templates)
19751 /* If there are more, but only one more, then we are referring to a
19752 member template. That's OK too. */
19753 if (parser->num_template_parameter_lists == num_templates + 1)
19755 /* If there are more template classes than parameter lists, we have
19758 template <class T> void S<T>::R<T>::f (); */
19759 if (parser->num_template_parameter_lists < num_templates)
19761 if (declarator && !current_function_decl)
19762 error_at (location, "specializing member %<%T::%E%> "
19763 "requires %<template<>%> syntax",
19764 declarator->u.id.qualifying_scope,
19765 declarator->u.id.unqualified_name);
19766 else if (declarator)
19767 error_at (location, "invalid declaration of %<%T::%E%>",
19768 declarator->u.id.qualifying_scope,
19769 declarator->u.id.unqualified_name);
19771 error_at (location, "too few template-parameter-lists");
19774 /* Otherwise, there are too many template parameter lists. We have
19777 template <class T> template <class U> void S::f(); */
19778 error_at (location, "too many template-parameter-lists");
19782 /* Parse an optional `::' token indicating that the following name is
19783 from the global namespace. If so, PARSER->SCOPE is set to the
19784 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19785 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19786 Returns the new value of PARSER->SCOPE, if the `::' token is
19787 present, and NULL_TREE otherwise. */
19790 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19794 /* Peek at the next token. */
19795 token = cp_lexer_peek_token (parser->lexer);
19796 /* If we're looking at a `::' token then we're starting from the
19797 global namespace, not our current location. */
19798 if (token->type == CPP_SCOPE)
19800 /* Consume the `::' token. */
19801 cp_lexer_consume_token (parser->lexer);
19802 /* Set the SCOPE so that we know where to start the lookup. */
19803 parser->scope = global_namespace;
19804 parser->qualifying_scope = global_namespace;
19805 parser->object_scope = NULL_TREE;
19807 return parser->scope;
19809 else if (!current_scope_valid_p)
19811 parser->scope = NULL_TREE;
19812 parser->qualifying_scope = NULL_TREE;
19813 parser->object_scope = NULL_TREE;
19819 /* Returns TRUE if the upcoming token sequence is the start of a
19820 constructor declarator. If FRIEND_P is true, the declarator is
19821 preceded by the `friend' specifier. */
19824 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19826 bool constructor_p;
19827 tree nested_name_specifier;
19828 cp_token *next_token;
19830 /* The common case is that this is not a constructor declarator, so
19831 try to avoid doing lots of work if at all possible. It's not
19832 valid declare a constructor at function scope. */
19833 if (parser->in_function_body)
19835 /* And only certain tokens can begin a constructor declarator. */
19836 next_token = cp_lexer_peek_token (parser->lexer);
19837 if (next_token->type != CPP_NAME
19838 && next_token->type != CPP_SCOPE
19839 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19840 && next_token->type != CPP_TEMPLATE_ID)
19843 /* Parse tentatively; we are going to roll back all of the tokens
19845 cp_parser_parse_tentatively (parser);
19846 /* Assume that we are looking at a constructor declarator. */
19847 constructor_p = true;
19849 /* Look for the optional `::' operator. */
19850 cp_parser_global_scope_opt (parser,
19851 /*current_scope_valid_p=*/false);
19852 /* Look for the nested-name-specifier. */
19853 nested_name_specifier
19854 = (cp_parser_nested_name_specifier_opt (parser,
19855 /*typename_keyword_p=*/false,
19856 /*check_dependency_p=*/false,
19858 /*is_declaration=*/false));
19859 /* Outside of a class-specifier, there must be a
19860 nested-name-specifier. */
19861 if (!nested_name_specifier &&
19862 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19864 constructor_p = false;
19865 else if (nested_name_specifier == error_mark_node)
19866 constructor_p = false;
19868 /* If we have a class scope, this is easy; DR 147 says that S::S always
19869 names the constructor, and no other qualified name could. */
19870 if (constructor_p && nested_name_specifier
19871 && CLASS_TYPE_P (nested_name_specifier))
19873 tree id = cp_parser_unqualified_id (parser,
19874 /*template_keyword_p=*/false,
19875 /*check_dependency_p=*/false,
19876 /*declarator_p=*/true,
19877 /*optional_p=*/false);
19878 if (is_overloaded_fn (id))
19879 id = DECL_NAME (get_first_fn (id));
19880 if (!constructor_name_p (id, nested_name_specifier))
19881 constructor_p = false;
19883 /* If we still think that this might be a constructor-declarator,
19884 look for a class-name. */
19885 else if (constructor_p)
19889 template <typename T> struct S {
19893 we must recognize that the nested `S' names a class. */
19895 type_decl = cp_parser_class_name (parser,
19896 /*typename_keyword_p=*/false,
19897 /*template_keyword_p=*/false,
19899 /*check_dependency_p=*/false,
19900 /*class_head_p=*/false,
19901 /*is_declaration=*/false);
19902 /* If there was no class-name, then this is not a constructor. */
19903 constructor_p = !cp_parser_error_occurred (parser);
19905 /* If we're still considering a constructor, we have to see a `(',
19906 to begin the parameter-declaration-clause, followed by either a
19907 `)', an `...', or a decl-specifier. We need to check for a
19908 type-specifier to avoid being fooled into thinking that:
19912 is a constructor. (It is actually a function named `f' that
19913 takes one parameter (of type `int') and returns a value of type
19916 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19917 constructor_p = false;
19920 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19921 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19922 /* A parameter declaration begins with a decl-specifier,
19923 which is either the "attribute" keyword, a storage class
19924 specifier, or (usually) a type-specifier. */
19925 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19928 tree pushed_scope = NULL_TREE;
19929 unsigned saved_num_template_parameter_lists;
19931 /* Names appearing in the type-specifier should be looked up
19932 in the scope of the class. */
19933 if (current_class_type)
19937 type = TREE_TYPE (type_decl);
19938 if (TREE_CODE (type) == TYPENAME_TYPE)
19940 type = resolve_typename_type (type,
19941 /*only_current_p=*/false);
19942 if (TREE_CODE (type) == TYPENAME_TYPE)
19944 cp_parser_abort_tentative_parse (parser);
19948 pushed_scope = push_scope (type);
19951 /* Inside the constructor parameter list, surrounding
19952 template-parameter-lists do not apply. */
19953 saved_num_template_parameter_lists
19954 = parser->num_template_parameter_lists;
19955 parser->num_template_parameter_lists = 0;
19957 /* Look for the type-specifier. */
19958 cp_parser_type_specifier (parser,
19959 CP_PARSER_FLAGS_NONE,
19960 /*decl_specs=*/NULL,
19961 /*is_declarator=*/true,
19962 /*declares_class_or_enum=*/NULL,
19963 /*is_cv_qualifier=*/NULL);
19965 parser->num_template_parameter_lists
19966 = saved_num_template_parameter_lists;
19968 /* Leave the scope of the class. */
19970 pop_scope (pushed_scope);
19972 constructor_p = !cp_parser_error_occurred (parser);
19976 /* We did not really want to consume any tokens. */
19977 cp_parser_abort_tentative_parse (parser);
19979 return constructor_p;
19982 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19983 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19984 they must be performed once we are in the scope of the function.
19986 Returns the function defined. */
19989 cp_parser_function_definition_from_specifiers_and_declarator
19990 (cp_parser* parser,
19991 cp_decl_specifier_seq *decl_specifiers,
19993 const cp_declarator *declarator)
19998 /* Begin the function-definition. */
19999 success_p = start_function (decl_specifiers, declarator, attributes);
20001 /* The things we're about to see are not directly qualified by any
20002 template headers we've seen thus far. */
20003 reset_specialization ();
20005 /* If there were names looked up in the decl-specifier-seq that we
20006 did not check, check them now. We must wait until we are in the
20007 scope of the function to perform the checks, since the function
20008 might be a friend. */
20009 perform_deferred_access_checks ();
20013 /* Skip the entire function. */
20014 cp_parser_skip_to_end_of_block_or_statement (parser);
20015 fn = error_mark_node;
20017 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20019 /* Seen already, skip it. An error message has already been output. */
20020 cp_parser_skip_to_end_of_block_or_statement (parser);
20021 fn = current_function_decl;
20022 current_function_decl = NULL_TREE;
20023 /* If this is a function from a class, pop the nested class. */
20024 if (current_class_name)
20025 pop_nested_class ();
20030 if (DECL_DECLARED_INLINE_P (current_function_decl))
20031 tv = TV_PARSE_INLINE;
20033 tv = TV_PARSE_FUNC;
20035 fn = cp_parser_function_definition_after_declarator (parser,
20036 /*inline_p=*/false);
20043 /* Parse the part of a function-definition that follows the
20044 declarator. INLINE_P is TRUE iff this function is an inline
20045 function defined within a class-specifier.
20047 Returns the function defined. */
20050 cp_parser_function_definition_after_declarator (cp_parser* parser,
20054 bool ctor_initializer_p = false;
20055 bool saved_in_unbraced_linkage_specification_p;
20056 bool saved_in_function_body;
20057 unsigned saved_num_template_parameter_lists;
20060 saved_in_function_body = parser->in_function_body;
20061 parser->in_function_body = true;
20062 /* If the next token is `return', then the code may be trying to
20063 make use of the "named return value" extension that G++ used to
20065 token = cp_lexer_peek_token (parser->lexer);
20066 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20068 /* Consume the `return' keyword. */
20069 cp_lexer_consume_token (parser->lexer);
20070 /* Look for the identifier that indicates what value is to be
20072 cp_parser_identifier (parser);
20073 /* Issue an error message. */
20074 error_at (token->location,
20075 "named return values are no longer supported");
20076 /* Skip tokens until we reach the start of the function body. */
20079 cp_token *token = cp_lexer_peek_token (parser->lexer);
20080 if (token->type == CPP_OPEN_BRACE
20081 || token->type == CPP_EOF
20082 || token->type == CPP_PRAGMA_EOL)
20084 cp_lexer_consume_token (parser->lexer);
20087 /* The `extern' in `extern "C" void f () { ... }' does not apply to
20088 anything declared inside `f'. */
20089 saved_in_unbraced_linkage_specification_p
20090 = parser->in_unbraced_linkage_specification_p;
20091 parser->in_unbraced_linkage_specification_p = false;
20092 /* Inside the function, surrounding template-parameter-lists do not
20094 saved_num_template_parameter_lists
20095 = parser->num_template_parameter_lists;
20096 parser->num_template_parameter_lists = 0;
20098 start_lambda_scope (current_function_decl);
20100 /* If the next token is `try', then we are looking at a
20101 function-try-block. */
20102 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20103 ctor_initializer_p = cp_parser_function_try_block (parser);
20104 /* A function-try-block includes the function-body, so we only do
20105 this next part if we're not processing a function-try-block. */
20108 = cp_parser_ctor_initializer_opt_and_function_body (parser);
20110 finish_lambda_scope ();
20112 /* Finish the function. */
20113 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20114 (inline_p ? 2 : 0));
20115 /* Generate code for it, if necessary. */
20116 expand_or_defer_fn (fn);
20117 /* Restore the saved values. */
20118 parser->in_unbraced_linkage_specification_p
20119 = saved_in_unbraced_linkage_specification_p;
20120 parser->num_template_parameter_lists
20121 = saved_num_template_parameter_lists;
20122 parser->in_function_body = saved_in_function_body;
20127 /* Parse a template-declaration, assuming that the `export' (and
20128 `extern') keywords, if present, has already been scanned. MEMBER_P
20129 is as for cp_parser_template_declaration. */
20132 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20134 tree decl = NULL_TREE;
20135 VEC (deferred_access_check,gc) *checks;
20136 tree parameter_list;
20137 bool friend_p = false;
20138 bool need_lang_pop;
20141 /* Look for the `template' keyword. */
20142 token = cp_lexer_peek_token (parser->lexer);
20143 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20147 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20149 if (at_class_scope_p () && current_function_decl)
20151 /* 14.5.2.2 [temp.mem]
20153 A local class shall not have member templates. */
20154 error_at (token->location,
20155 "invalid declaration of member template in local class");
20156 cp_parser_skip_to_end_of_block_or_statement (parser);
20161 A template ... shall not have C linkage. */
20162 if (current_lang_name == lang_name_c)
20164 error_at (token->location, "template with C linkage");
20165 /* Give it C++ linkage to avoid confusing other parts of the
20167 push_lang_context (lang_name_cplusplus);
20168 need_lang_pop = true;
20171 need_lang_pop = false;
20173 /* We cannot perform access checks on the template parameter
20174 declarations until we know what is being declared, just as we
20175 cannot check the decl-specifier list. */
20176 push_deferring_access_checks (dk_deferred);
20178 /* If the next token is `>', then we have an invalid
20179 specialization. Rather than complain about an invalid template
20180 parameter, issue an error message here. */
20181 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20183 cp_parser_error (parser, "invalid explicit specialization");
20184 begin_specialization ();
20185 parameter_list = NULL_TREE;
20189 /* Parse the template parameters. */
20190 parameter_list = cp_parser_template_parameter_list (parser);
20191 fixup_template_parms ();
20194 /* Get the deferred access checks from the parameter list. These
20195 will be checked once we know what is being declared, as for a
20196 member template the checks must be performed in the scope of the
20197 class containing the member. */
20198 checks = get_deferred_access_checks ();
20200 /* Look for the `>'. */
20201 cp_parser_skip_to_end_of_template_parameter_list (parser);
20202 /* We just processed one more parameter list. */
20203 ++parser->num_template_parameter_lists;
20204 /* If the next token is `template', there are more template
20206 if (cp_lexer_next_token_is_keyword (parser->lexer,
20208 cp_parser_template_declaration_after_export (parser, member_p);
20211 /* There are no access checks when parsing a template, as we do not
20212 know if a specialization will be a friend. */
20213 push_deferring_access_checks (dk_no_check);
20214 token = cp_lexer_peek_token (parser->lexer);
20215 decl = cp_parser_single_declaration (parser,
20218 /*explicit_specialization_p=*/false,
20220 pop_deferring_access_checks ();
20222 /* If this is a member template declaration, let the front
20224 if (member_p && !friend_p && decl)
20226 if (TREE_CODE (decl) == TYPE_DECL)
20227 cp_parser_check_access_in_redeclaration (decl, token->location);
20229 decl = finish_member_template_decl (decl);
20231 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20232 make_friend_class (current_class_type, TREE_TYPE (decl),
20233 /*complain=*/true);
20235 /* We are done with the current parameter list. */
20236 --parser->num_template_parameter_lists;
20238 pop_deferring_access_checks ();
20241 finish_template_decl (parameter_list);
20243 /* Register member declarations. */
20244 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20245 finish_member_declaration (decl);
20246 /* For the erroneous case of a template with C linkage, we pushed an
20247 implicit C++ linkage scope; exit that scope now. */
20249 pop_lang_context ();
20250 /* If DECL is a function template, we must return to parse it later.
20251 (Even though there is no definition, there might be default
20252 arguments that need handling.) */
20253 if (member_p && decl
20254 && (TREE_CODE (decl) == FUNCTION_DECL
20255 || DECL_FUNCTION_TEMPLATE_P (decl)))
20256 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20259 /* Perform the deferred access checks from a template-parameter-list.
20260 CHECKS is a TREE_LIST of access checks, as returned by
20261 get_deferred_access_checks. */
20264 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20266 ++processing_template_parmlist;
20267 perform_access_checks (checks);
20268 --processing_template_parmlist;
20271 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20272 `function-definition' sequence. MEMBER_P is true, this declaration
20273 appears in a class scope.
20275 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
20276 *FRIEND_P is set to TRUE iff the declaration is a friend. */
20279 cp_parser_single_declaration (cp_parser* parser,
20280 VEC (deferred_access_check,gc)* checks,
20282 bool explicit_specialization_p,
20285 int declares_class_or_enum;
20286 tree decl = NULL_TREE;
20287 cp_decl_specifier_seq decl_specifiers;
20288 bool function_definition_p = false;
20289 cp_token *decl_spec_token_start;
20291 /* This function is only used when processing a template
20293 gcc_assert (innermost_scope_kind () == sk_template_parms
20294 || innermost_scope_kind () == sk_template_spec);
20296 /* Defer access checks until we know what is being declared. */
20297 push_deferring_access_checks (dk_deferred);
20299 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20301 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20302 cp_parser_decl_specifier_seq (parser,
20303 CP_PARSER_FLAGS_OPTIONAL,
20305 &declares_class_or_enum);
20307 *friend_p = cp_parser_friend_p (&decl_specifiers);
20309 /* There are no template typedefs. */
20310 if (decl_specifiers.specs[(int) ds_typedef])
20312 error_at (decl_spec_token_start->location,
20313 "template declaration of %<typedef%>");
20314 decl = error_mark_node;
20317 /* Gather up the access checks that occurred the
20318 decl-specifier-seq. */
20319 stop_deferring_access_checks ();
20321 /* Check for the declaration of a template class. */
20322 if (declares_class_or_enum)
20324 if (cp_parser_declares_only_class_p (parser))
20326 decl = shadow_tag (&decl_specifiers);
20331 friend template <typename T> struct A<T>::B;
20334 A<T>::B will be represented by a TYPENAME_TYPE, and
20335 therefore not recognized by shadow_tag. */
20336 if (friend_p && *friend_p
20338 && decl_specifiers.type
20339 && TYPE_P (decl_specifiers.type))
20340 decl = decl_specifiers.type;
20342 if (decl && decl != error_mark_node)
20343 decl = TYPE_NAME (decl);
20345 decl = error_mark_node;
20347 /* Perform access checks for template parameters. */
20348 cp_parser_perform_template_parameter_access_checks (checks);
20352 /* Complain about missing 'typename' or other invalid type names. */
20353 if (!decl_specifiers.any_type_specifiers_p
20354 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20356 /* cp_parser_parse_and_diagnose_invalid_type_name calls
20357 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20358 the rest of this declaration. */
20359 decl = error_mark_node;
20363 /* If it's not a template class, try for a template function. If
20364 the next token is a `;', then this declaration does not declare
20365 anything. But, if there were errors in the decl-specifiers, then
20366 the error might well have come from an attempted class-specifier.
20367 In that case, there's no need to warn about a missing declarator. */
20369 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20370 || decl_specifiers.type != error_mark_node))
20372 decl = cp_parser_init_declarator (parser,
20375 /*function_definition_allowed_p=*/true,
20377 declares_class_or_enum,
20378 &function_definition_p,
20381 /* 7.1.1-1 [dcl.stc]
20383 A storage-class-specifier shall not be specified in an explicit
20384 specialization... */
20386 && explicit_specialization_p
20387 && decl_specifiers.storage_class != sc_none)
20389 error_at (decl_spec_token_start->location,
20390 "explicit template specialization cannot have a storage class");
20391 decl = error_mark_node;
20395 /* Look for a trailing `;' after the declaration. */
20396 if (!function_definition_p
20397 && (decl == error_mark_node
20398 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20399 cp_parser_skip_to_end_of_block_or_statement (parser);
20402 pop_deferring_access_checks ();
20404 /* Clear any current qualification; whatever comes next is the start
20405 of something new. */
20406 parser->scope = NULL_TREE;
20407 parser->qualifying_scope = NULL_TREE;
20408 parser->object_scope = NULL_TREE;
20413 /* Parse a cast-expression that is not the operand of a unary "&". */
20416 cp_parser_simple_cast_expression (cp_parser *parser)
20418 return cp_parser_cast_expression (parser, /*address_p=*/false,
20419 /*cast_p=*/false, NULL);
20422 /* Parse a functional cast to TYPE. Returns an expression
20423 representing the cast. */
20426 cp_parser_functional_cast (cp_parser* parser, tree type)
20429 tree expression_list;
20433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20435 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20436 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20437 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20438 if (TREE_CODE (type) == TYPE_DECL)
20439 type = TREE_TYPE (type);
20440 return finish_compound_literal (type, expression_list,
20441 tf_warning_or_error);
20445 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20447 /*allow_expansion_p=*/true,
20448 /*non_constant_p=*/NULL);
20450 expression_list = error_mark_node;
20453 expression_list = build_tree_list_vec (vec);
20454 release_tree_vector (vec);
20457 cast = build_functional_cast (type, expression_list,
20458 tf_warning_or_error);
20459 /* [expr.const]/1: In an integral constant expression "only type
20460 conversions to integral or enumeration type can be used". */
20461 if (TREE_CODE (type) == TYPE_DECL)
20462 type = TREE_TYPE (type);
20463 if (cast != error_mark_node
20464 && !cast_valid_in_integral_constant_expression_p (type)
20465 && cp_parser_non_integral_constant_expression (parser,
20467 return error_mark_node;
20471 /* Save the tokens that make up the body of a member function defined
20472 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20473 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20474 specifiers applied to the declaration. Returns the FUNCTION_DECL
20475 for the member function. */
20478 cp_parser_save_member_function_body (cp_parser* parser,
20479 cp_decl_specifier_seq *decl_specifiers,
20480 cp_declarator *declarator,
20487 /* Create the FUNCTION_DECL. */
20488 fn = grokmethod (decl_specifiers, declarator, attributes);
20489 /* If something went badly wrong, bail out now. */
20490 if (fn == error_mark_node)
20492 /* If there's a function-body, skip it. */
20493 if (cp_parser_token_starts_function_definition_p
20494 (cp_lexer_peek_token (parser->lexer)))
20495 cp_parser_skip_to_end_of_block_or_statement (parser);
20496 return error_mark_node;
20499 /* Remember it, if there default args to post process. */
20500 cp_parser_save_default_args (parser, fn);
20502 /* Save away the tokens that make up the body of the
20504 first = parser->lexer->next_token;
20505 /* We can have braced-init-list mem-initializers before the fn body. */
20506 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20508 cp_lexer_consume_token (parser->lexer);
20509 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20510 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20512 /* cache_group will stop after an un-nested { } pair, too. */
20513 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20516 /* variadic mem-inits have ... after the ')'. */
20517 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20518 cp_lexer_consume_token (parser->lexer);
20521 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20522 /* Handle function try blocks. */
20523 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20524 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20525 last = parser->lexer->next_token;
20527 /* Save away the inline definition; we will process it when the
20528 class is complete. */
20529 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20530 DECL_PENDING_INLINE_P (fn) = 1;
20532 /* We need to know that this was defined in the class, so that
20533 friend templates are handled correctly. */
20534 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20536 /* Add FN to the queue of functions to be parsed later. */
20537 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20542 /* Parse a template-argument-list, as well as the trailing ">" (but
20543 not the opening ">"). See cp_parser_template_argument_list for the
20547 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20551 tree saved_qualifying_scope;
20552 tree saved_object_scope;
20553 bool saved_greater_than_is_operator_p;
20554 int saved_unevaluated_operand;
20555 int saved_inhibit_evaluation_warnings;
20559 When parsing a template-id, the first non-nested `>' is taken as
20560 the end of the template-argument-list rather than a greater-than
20562 saved_greater_than_is_operator_p
20563 = parser->greater_than_is_operator_p;
20564 parser->greater_than_is_operator_p = false;
20565 /* Parsing the argument list may modify SCOPE, so we save it
20567 saved_scope = parser->scope;
20568 saved_qualifying_scope = parser->qualifying_scope;
20569 saved_object_scope = parser->object_scope;
20570 /* We need to evaluate the template arguments, even though this
20571 template-id may be nested within a "sizeof". */
20572 saved_unevaluated_operand = cp_unevaluated_operand;
20573 cp_unevaluated_operand = 0;
20574 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20575 c_inhibit_evaluation_warnings = 0;
20576 /* Parse the template-argument-list itself. */
20577 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20578 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20579 arguments = NULL_TREE;
20581 arguments = cp_parser_template_argument_list (parser);
20582 /* Look for the `>' that ends the template-argument-list. If we find
20583 a '>>' instead, it's probably just a typo. */
20584 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20586 if (cxx_dialect != cxx98)
20588 /* In C++0x, a `>>' in a template argument list or cast
20589 expression is considered to be two separate `>'
20590 tokens. So, change the current token to a `>', but don't
20591 consume it: it will be consumed later when the outer
20592 template argument list (or cast expression) is parsed.
20593 Note that this replacement of `>' for `>>' is necessary
20594 even if we are parsing tentatively: in the tentative
20595 case, after calling
20596 cp_parser_enclosed_template_argument_list we will always
20597 throw away all of the template arguments and the first
20598 closing `>', either because the template argument list
20599 was erroneous or because we are replacing those tokens
20600 with a CPP_TEMPLATE_ID token. The second `>' (which will
20601 not have been thrown away) is needed either to close an
20602 outer template argument list or to complete a new-style
20604 cp_token *token = cp_lexer_peek_token (parser->lexer);
20605 token->type = CPP_GREATER;
20607 else if (!saved_greater_than_is_operator_p)
20609 /* If we're in a nested template argument list, the '>>' has
20610 to be a typo for '> >'. We emit the error message, but we
20611 continue parsing and we push a '>' as next token, so that
20612 the argument list will be parsed correctly. Note that the
20613 global source location is still on the token before the
20614 '>>', so we need to say explicitly where we want it. */
20615 cp_token *token = cp_lexer_peek_token (parser->lexer);
20616 error_at (token->location, "%<>>%> should be %<> >%> "
20617 "within a nested template argument list");
20619 token->type = CPP_GREATER;
20623 /* If this is not a nested template argument list, the '>>'
20624 is a typo for '>'. Emit an error message and continue.
20625 Same deal about the token location, but here we can get it
20626 right by consuming the '>>' before issuing the diagnostic. */
20627 cp_token *token = cp_lexer_consume_token (parser->lexer);
20628 error_at (token->location,
20629 "spurious %<>>%>, use %<>%> to terminate "
20630 "a template argument list");
20634 cp_parser_skip_to_end_of_template_parameter_list (parser);
20635 /* The `>' token might be a greater-than operator again now. */
20636 parser->greater_than_is_operator_p
20637 = saved_greater_than_is_operator_p;
20638 /* Restore the SAVED_SCOPE. */
20639 parser->scope = saved_scope;
20640 parser->qualifying_scope = saved_qualifying_scope;
20641 parser->object_scope = saved_object_scope;
20642 cp_unevaluated_operand = saved_unevaluated_operand;
20643 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20648 /* MEMBER_FUNCTION is a member function, or a friend. If default
20649 arguments, or the body of the function have not yet been parsed,
20653 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20655 timevar_push (TV_PARSE_INMETH);
20656 /* If this member is a template, get the underlying
20658 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20659 member_function = DECL_TEMPLATE_RESULT (member_function);
20661 /* There should not be any class definitions in progress at this
20662 point; the bodies of members are only parsed outside of all class
20664 gcc_assert (parser->num_classes_being_defined == 0);
20665 /* While we're parsing the member functions we might encounter more
20666 classes. We want to handle them right away, but we don't want
20667 them getting mixed up with functions that are currently in the
20669 push_unparsed_function_queues (parser);
20671 /* Make sure that any template parameters are in scope. */
20672 maybe_begin_member_template_processing (member_function);
20674 /* If the body of the function has not yet been parsed, parse it
20676 if (DECL_PENDING_INLINE_P (member_function))
20678 tree function_scope;
20679 cp_token_cache *tokens;
20681 /* The function is no longer pending; we are processing it. */
20682 tokens = DECL_PENDING_INLINE_INFO (member_function);
20683 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20684 DECL_PENDING_INLINE_P (member_function) = 0;
20686 /* If this is a local class, enter the scope of the containing
20688 function_scope = current_function_decl;
20689 if (function_scope)
20690 push_function_context ();
20692 /* Push the body of the function onto the lexer stack. */
20693 cp_parser_push_lexer_for_tokens (parser, tokens);
20695 /* Let the front end know that we going to be defining this
20697 start_preparsed_function (member_function, NULL_TREE,
20698 SF_PRE_PARSED | SF_INCLASS_INLINE);
20700 /* Don't do access checking if it is a templated function. */
20701 if (processing_template_decl)
20702 push_deferring_access_checks (dk_no_check);
20704 /* Now, parse the body of the function. */
20705 cp_parser_function_definition_after_declarator (parser,
20706 /*inline_p=*/true);
20708 if (processing_template_decl)
20709 pop_deferring_access_checks ();
20711 /* Leave the scope of the containing function. */
20712 if (function_scope)
20713 pop_function_context ();
20714 cp_parser_pop_lexer (parser);
20717 /* Remove any template parameters from the symbol table. */
20718 maybe_end_member_template_processing ();
20720 /* Restore the queue. */
20721 pop_unparsed_function_queues (parser);
20722 timevar_pop (TV_PARSE_INMETH);
20725 /* If DECL contains any default args, remember it on the unparsed
20726 functions queue. */
20729 cp_parser_save_default_args (cp_parser* parser, tree decl)
20733 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20735 probe = TREE_CHAIN (probe))
20736 if (TREE_PURPOSE (probe))
20738 cp_default_arg_entry *entry
20739 = VEC_safe_push (cp_default_arg_entry, gc,
20740 unparsed_funs_with_default_args, NULL);
20741 entry->class_type = current_class_type;
20742 entry->decl = decl;
20747 /* FN is a FUNCTION_DECL which may contains a parameter with an
20748 unparsed DEFAULT_ARG. Parse the default args now. This function
20749 assumes that the current scope is the scope in which the default
20750 argument should be processed. */
20753 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20755 bool saved_local_variables_forbidden_p;
20756 bool non_constant_p;
20757 tree parm, parmdecl;
20759 /* While we're parsing the default args, we might (due to the
20760 statement expression extension) encounter more classes. We want
20761 to handle them right away, but we don't want them getting mixed
20762 up with default args that are currently in the queue. */
20763 push_unparsed_function_queues (parser);
20765 /* Local variable names (and the `this' keyword) may not appear
20766 in a default argument. */
20767 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20768 parser->local_variables_forbidden_p = true;
20770 push_defarg_context (fn);
20772 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20773 parmdecl = DECL_ARGUMENTS (fn);
20774 parm && parm != void_list_node;
20775 parm = TREE_CHAIN (parm),
20776 parmdecl = DECL_CHAIN (parmdecl))
20778 cp_token_cache *tokens;
20779 tree default_arg = TREE_PURPOSE (parm);
20781 VEC(tree,gc) *insts;
20788 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20789 /* This can happen for a friend declaration for a function
20790 already declared with default arguments. */
20793 /* Push the saved tokens for the default argument onto the parser's
20795 tokens = DEFARG_TOKENS (default_arg);
20796 cp_parser_push_lexer_for_tokens (parser, tokens);
20798 start_lambda_scope (parmdecl);
20800 /* Parse the assignment-expression. */
20801 parsed_arg = cp_parser_initializer_clause (parser, &non_constant_p);
20802 if (parsed_arg == error_mark_node)
20804 cp_parser_pop_lexer (parser);
20807 if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
20808 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20810 if (!processing_template_decl)
20811 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20813 TREE_PURPOSE (parm) = parsed_arg;
20815 /* Update any instantiations we've already created. */
20816 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20817 VEC_iterate (tree, insts, ix, copy); ix++)
20818 TREE_PURPOSE (copy) = parsed_arg;
20820 finish_lambda_scope ();
20822 /* If the token stream has not been completely used up, then
20823 there was extra junk after the end of the default
20825 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20826 cp_parser_error (parser, "expected %<,%>");
20828 /* Revert to the main lexer. */
20829 cp_parser_pop_lexer (parser);
20832 pop_defarg_context ();
20834 /* Make sure no default arg is missing. */
20835 check_default_args (fn);
20837 /* Restore the state of local_variables_forbidden_p. */
20838 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20840 /* Restore the queue. */
20841 pop_unparsed_function_queues (parser);
20844 /* Parse the operand of `sizeof' (or a similar operator). Returns
20845 either a TYPE or an expression, depending on the form of the
20846 input. The KEYWORD indicates which kind of expression we have
20850 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20852 tree expr = NULL_TREE;
20853 const char *saved_message;
20855 bool saved_integral_constant_expression_p;
20856 bool saved_non_integral_constant_expression_p;
20857 bool pack_expansion_p = false;
20859 /* Types cannot be defined in a `sizeof' expression. Save away the
20861 saved_message = parser->type_definition_forbidden_message;
20862 /* And create the new one. */
20863 tmp = concat ("types may not be defined in %<",
20864 IDENTIFIER_POINTER (ridpointers[keyword]),
20865 "%> expressions", NULL);
20866 parser->type_definition_forbidden_message = tmp;
20868 /* The restrictions on constant-expressions do not apply inside
20869 sizeof expressions. */
20870 saved_integral_constant_expression_p
20871 = parser->integral_constant_expression_p;
20872 saved_non_integral_constant_expression_p
20873 = parser->non_integral_constant_expression_p;
20874 parser->integral_constant_expression_p = false;
20876 /* If it's a `...', then we are computing the length of a parameter
20878 if (keyword == RID_SIZEOF
20879 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20881 /* Consume the `...'. */
20882 cp_lexer_consume_token (parser->lexer);
20883 maybe_warn_variadic_templates ();
20885 /* Note that this is an expansion. */
20886 pack_expansion_p = true;
20889 /* Do not actually evaluate the expression. */
20890 ++cp_unevaluated_operand;
20891 ++c_inhibit_evaluation_warnings;
20892 /* If it's a `(', then we might be looking at the type-id
20894 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20897 bool saved_in_type_id_in_expr_p;
20899 /* We can't be sure yet whether we're looking at a type-id or an
20901 cp_parser_parse_tentatively (parser);
20902 /* Consume the `('. */
20903 cp_lexer_consume_token (parser->lexer);
20904 /* Parse the type-id. */
20905 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20906 parser->in_type_id_in_expr_p = true;
20907 type = cp_parser_type_id (parser);
20908 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20909 /* Now, look for the trailing `)'. */
20910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20911 /* If all went well, then we're done. */
20912 if (cp_parser_parse_definitely (parser))
20914 cp_decl_specifier_seq decl_specs;
20916 /* Build a trivial decl-specifier-seq. */
20917 clear_decl_specs (&decl_specs);
20918 decl_specs.type = type;
20920 /* Call grokdeclarator to figure out what type this is. */
20921 expr = grokdeclarator (NULL,
20925 /*attrlist=*/NULL);
20929 /* If the type-id production did not work out, then we must be
20930 looking at the unary-expression production. */
20932 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20933 /*cast_p=*/false, NULL);
20935 if (pack_expansion_p)
20936 /* Build a pack expansion. */
20937 expr = make_pack_expansion (expr);
20939 /* Go back to evaluating expressions. */
20940 --cp_unevaluated_operand;
20941 --c_inhibit_evaluation_warnings;
20943 /* Free the message we created. */
20945 /* And restore the old one. */
20946 parser->type_definition_forbidden_message = saved_message;
20947 parser->integral_constant_expression_p
20948 = saved_integral_constant_expression_p;
20949 parser->non_integral_constant_expression_p
20950 = saved_non_integral_constant_expression_p;
20955 /* If the current declaration has no declarator, return true. */
20958 cp_parser_declares_only_class_p (cp_parser *parser)
20960 /* If the next token is a `;' or a `,' then there is no
20962 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20963 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20966 /* Update the DECL_SPECS to reflect the storage class indicated by
20970 cp_parser_set_storage_class (cp_parser *parser,
20971 cp_decl_specifier_seq *decl_specs,
20973 location_t location)
20975 cp_storage_class storage_class;
20977 if (parser->in_unbraced_linkage_specification_p)
20979 error_at (location, "invalid use of %qD in linkage specification",
20980 ridpointers[keyword]);
20983 else if (decl_specs->storage_class != sc_none)
20985 decl_specs->conflicting_specifiers_p = true;
20989 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20990 && decl_specs->specs[(int) ds_thread])
20992 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20993 decl_specs->specs[(int) ds_thread] = 0;
20999 storage_class = sc_auto;
21002 storage_class = sc_register;
21005 storage_class = sc_static;
21008 storage_class = sc_extern;
21011 storage_class = sc_mutable;
21014 gcc_unreachable ();
21016 decl_specs->storage_class = storage_class;
21018 /* A storage class specifier cannot be applied alongside a typedef
21019 specifier. If there is a typedef specifier present then set
21020 conflicting_specifiers_p which will trigger an error later
21021 on in grokdeclarator. */
21022 if (decl_specs->specs[(int)ds_typedef])
21023 decl_specs->conflicting_specifiers_p = true;
21026 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If TYPE_DEFINITION_P
21027 is true, the type is a class or enum definition. */
21030 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21032 location_t location,
21033 bool type_definition_p)
21035 decl_specs->any_specifiers_p = true;
21037 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21038 (with, for example, in "typedef int wchar_t;") we remember that
21039 this is what happened. In system headers, we ignore these
21040 declarations so that G++ can work with system headers that are not
21042 if (decl_specs->specs[(int) ds_typedef]
21043 && !type_definition_p
21044 && (type_spec == boolean_type_node
21045 || type_spec == char16_type_node
21046 || type_spec == char32_type_node
21047 || type_spec == wchar_type_node)
21048 && (decl_specs->type
21049 || decl_specs->specs[(int) ds_long]
21050 || decl_specs->specs[(int) ds_short]
21051 || decl_specs->specs[(int) ds_unsigned]
21052 || decl_specs->specs[(int) ds_signed]))
21054 decl_specs->redefined_builtin_type = type_spec;
21055 if (!decl_specs->type)
21057 decl_specs->type = type_spec;
21058 decl_specs->type_definition_p = false;
21059 decl_specs->type_location = location;
21062 else if (decl_specs->type)
21063 decl_specs->multiple_types_p = true;
21066 decl_specs->type = type_spec;
21067 decl_specs->type_definition_p = type_definition_p;
21068 decl_specs->redefined_builtin_type = NULL_TREE;
21069 decl_specs->type_location = location;
21073 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21074 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
21077 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21079 return decl_specifiers->specs[(int) ds_friend] != 0;
21082 /* Issue an error message indicating that TOKEN_DESC was expected.
21083 If KEYWORD is true, it indicated this function is called by
21084 cp_parser_require_keword and the required token can only be
21085 a indicated keyword. */
21088 cp_parser_required_error (cp_parser *parser,
21089 required_token token_desc,
21092 switch (token_desc)
21095 cp_parser_error (parser, "expected %<new%>");
21098 cp_parser_error (parser, "expected %<delete%>");
21101 cp_parser_error (parser, "expected %<return%>");
21104 cp_parser_error (parser, "expected %<while%>");
21107 cp_parser_error (parser, "expected %<extern%>");
21109 case RT_STATIC_ASSERT:
21110 cp_parser_error (parser, "expected %<static_assert%>");
21113 cp_parser_error (parser, "expected %<decltype%>");
21116 cp_parser_error (parser, "expected %<operator%>");
21119 cp_parser_error (parser, "expected %<class%>");
21122 cp_parser_error (parser, "expected %<template%>");
21125 cp_parser_error (parser, "expected %<namespace%>");
21128 cp_parser_error (parser, "expected %<using%>");
21131 cp_parser_error (parser, "expected %<asm%>");
21134 cp_parser_error (parser, "expected %<try%>");
21137 cp_parser_error (parser, "expected %<catch%>");
21140 cp_parser_error (parser, "expected %<throw%>");
21143 cp_parser_error (parser, "expected %<__label__%>");
21146 cp_parser_error (parser, "expected %<@try%>");
21148 case RT_AT_SYNCHRONIZED:
21149 cp_parser_error (parser, "expected %<@synchronized%>");
21152 cp_parser_error (parser, "expected %<@throw%>");
21159 switch (token_desc)
21162 cp_parser_error (parser, "expected %<;%>");
21164 case RT_OPEN_PAREN:
21165 cp_parser_error (parser, "expected %<(%>");
21167 case RT_CLOSE_BRACE:
21168 cp_parser_error (parser, "expected %<}%>");
21170 case RT_OPEN_BRACE:
21171 cp_parser_error (parser, "expected %<{%>");
21173 case RT_CLOSE_SQUARE:
21174 cp_parser_error (parser, "expected %<]%>");
21176 case RT_OPEN_SQUARE:
21177 cp_parser_error (parser, "expected %<[%>");
21180 cp_parser_error (parser, "expected %<,%>");
21183 cp_parser_error (parser, "expected %<::%>");
21186 cp_parser_error (parser, "expected %<<%>");
21189 cp_parser_error (parser, "expected %<>%>");
21192 cp_parser_error (parser, "expected %<=%>");
21195 cp_parser_error (parser, "expected %<...%>");
21198 cp_parser_error (parser, "expected %<*%>");
21201 cp_parser_error (parser, "expected %<~%>");
21204 cp_parser_error (parser, "expected %<:%>");
21206 case RT_COLON_SCOPE:
21207 cp_parser_error (parser, "expected %<:%> or %<::%>");
21209 case RT_CLOSE_PAREN:
21210 cp_parser_error (parser, "expected %<)%>");
21212 case RT_COMMA_CLOSE_PAREN:
21213 cp_parser_error (parser, "expected %<,%> or %<)%>");
21215 case RT_PRAGMA_EOL:
21216 cp_parser_error (parser, "expected end of line");
21219 cp_parser_error (parser, "expected identifier");
21222 cp_parser_error (parser, "expected selection-statement");
21224 case RT_INTERATION:
21225 cp_parser_error (parser, "expected iteration-statement");
21228 cp_parser_error (parser, "expected jump-statement");
21231 cp_parser_error (parser, "expected class-key");
21233 case RT_CLASS_TYPENAME_TEMPLATE:
21234 cp_parser_error (parser,
21235 "expected %<class%>, %<typename%>, or %<template%>");
21238 gcc_unreachable ();
21242 gcc_unreachable ();
21247 /* If the next token is of the indicated TYPE, consume it. Otherwise,
21248 issue an error message indicating that TOKEN_DESC was expected.
21250 Returns the token consumed, if the token had the appropriate type.
21251 Otherwise, returns NULL. */
21254 cp_parser_require (cp_parser* parser,
21255 enum cpp_ttype type,
21256 required_token token_desc)
21258 if (cp_lexer_next_token_is (parser->lexer, type))
21259 return cp_lexer_consume_token (parser->lexer);
21262 /* Output the MESSAGE -- unless we're parsing tentatively. */
21263 if (!cp_parser_simulate_error (parser))
21264 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21269 /* An error message is produced if the next token is not '>'.
21270 All further tokens are skipped until the desired token is
21271 found or '{', '}', ';' or an unbalanced ')' or ']'. */
21274 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21276 /* Current level of '< ... >'. */
21277 unsigned level = 0;
21278 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
21279 unsigned nesting_depth = 0;
21281 /* Are we ready, yet? If not, issue error message. */
21282 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21285 /* Skip tokens until the desired token is found. */
21288 /* Peek at the next token. */
21289 switch (cp_lexer_peek_token (parser->lexer)->type)
21292 if (!nesting_depth)
21297 if (cxx_dialect == cxx98)
21298 /* C++0x views the `>>' operator as two `>' tokens, but
21301 else if (!nesting_depth && level-- == 0)
21303 /* We've hit a `>>' where the first `>' closes the
21304 template argument list, and the second `>' is
21305 spurious. Just consume the `>>' and stop; we've
21306 already produced at least one error. */
21307 cp_lexer_consume_token (parser->lexer);
21310 /* Fall through for C++0x, so we handle the second `>' in
21314 if (!nesting_depth && level-- == 0)
21316 /* We've reached the token we want, consume it and stop. */
21317 cp_lexer_consume_token (parser->lexer);
21322 case CPP_OPEN_PAREN:
21323 case CPP_OPEN_SQUARE:
21327 case CPP_CLOSE_PAREN:
21328 case CPP_CLOSE_SQUARE:
21329 if (nesting_depth-- == 0)
21334 case CPP_PRAGMA_EOL:
21335 case CPP_SEMICOLON:
21336 case CPP_OPEN_BRACE:
21337 case CPP_CLOSE_BRACE:
21338 /* The '>' was probably forgotten, don't look further. */
21345 /* Consume this token. */
21346 cp_lexer_consume_token (parser->lexer);
21350 /* If the next token is the indicated keyword, consume it. Otherwise,
21351 issue an error message indicating that TOKEN_DESC was expected.
21353 Returns the token consumed, if the token had the appropriate type.
21354 Otherwise, returns NULL. */
21357 cp_parser_require_keyword (cp_parser* parser,
21359 required_token token_desc)
21361 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21363 if (token && token->keyword != keyword)
21365 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21372 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21373 function-definition. */
21376 cp_parser_token_starts_function_definition_p (cp_token* token)
21378 return (/* An ordinary function-body begins with an `{'. */
21379 token->type == CPP_OPEN_BRACE
21380 /* A ctor-initializer begins with a `:'. */
21381 || token->type == CPP_COLON
21382 /* A function-try-block begins with `try'. */
21383 || token->keyword == RID_TRY
21384 /* The named return value extension begins with `return'. */
21385 || token->keyword == RID_RETURN);
21388 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21392 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21396 token = cp_lexer_peek_token (parser->lexer);
21397 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21400 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21401 C++0x) ending a template-argument. */
21404 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21408 token = cp_lexer_peek_token (parser->lexer);
21409 return (token->type == CPP_COMMA
21410 || token->type == CPP_GREATER
21411 || token->type == CPP_ELLIPSIS
21412 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21415 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21416 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21419 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21424 token = cp_lexer_peek_nth_token (parser->lexer, n);
21425 if (token->type == CPP_LESS)
21427 /* Check for the sequence `<::' in the original code. It would be lexed as
21428 `[:', where `[' is a digraph, and there is no whitespace before
21430 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21433 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21434 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21440 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21441 or none_type otherwise. */
21443 static enum tag_types
21444 cp_parser_token_is_class_key (cp_token* token)
21446 switch (token->keyword)
21451 return record_type;
21460 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21463 cp_parser_check_class_key (enum tag_types class_key, tree type)
21465 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21466 permerror (input_location, "%qs tag used in naming %q#T",
21467 class_key == union_type ? "union"
21468 : class_key == record_type ? "struct" : "class",
21472 /* Issue an error message if DECL is redeclared with different
21473 access than its original declaration [class.access.spec/3].
21474 This applies to nested classes and nested class templates.
21478 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21480 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21483 if ((TREE_PRIVATE (decl)
21484 != (current_access_specifier == access_private_node))
21485 || (TREE_PROTECTED (decl)
21486 != (current_access_specifier == access_protected_node)))
21487 error_at (location, "%qD redeclared with different access", decl);
21490 /* Look for the `template' keyword, as a syntactic disambiguator.
21491 Return TRUE iff it is present, in which case it will be
21495 cp_parser_optional_template_keyword (cp_parser *parser)
21497 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21499 /* The `template' keyword can only be used within templates;
21500 outside templates the parser can always figure out what is a
21501 template and what is not. */
21502 if (!processing_template_decl)
21504 cp_token *token = cp_lexer_peek_token (parser->lexer);
21505 error_at (token->location,
21506 "%<template%> (as a disambiguator) is only allowed "
21507 "within templates");
21508 /* If this part of the token stream is rescanned, the same
21509 error message would be generated. So, we purge the token
21510 from the stream. */
21511 cp_lexer_purge_token (parser->lexer);
21516 /* Consume the `template' keyword. */
21517 cp_lexer_consume_token (parser->lexer);
21525 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21526 set PARSER->SCOPE, and perform other related actions. */
21529 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21532 struct tree_check *check_value;
21533 deferred_access_check *chk;
21534 VEC (deferred_access_check,gc) *checks;
21536 /* Get the stored value. */
21537 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21538 /* Perform any access checks that were deferred. */
21539 checks = check_value->checks;
21542 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21543 perform_or_defer_access_check (chk->binfo,
21547 /* Set the scope from the stored value. */
21548 parser->scope = check_value->value;
21549 parser->qualifying_scope = check_value->qualifying_scope;
21550 parser->object_scope = NULL_TREE;
21553 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21554 encounter the end of a block before what we were looking for. */
21557 cp_parser_cache_group (cp_parser *parser,
21558 enum cpp_ttype end,
21563 cp_token *token = cp_lexer_peek_token (parser->lexer);
21565 /* Abort a parenthesized expression if we encounter a semicolon. */
21566 if ((end == CPP_CLOSE_PAREN || depth == 0)
21567 && token->type == CPP_SEMICOLON)
21569 /* If we've reached the end of the file, stop. */
21570 if (token->type == CPP_EOF
21571 || (end != CPP_PRAGMA_EOL
21572 && token->type == CPP_PRAGMA_EOL))
21574 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21575 /* We've hit the end of an enclosing block, so there's been some
21576 kind of syntax error. */
21579 /* Consume the token. */
21580 cp_lexer_consume_token (parser->lexer);
21581 /* See if it starts a new group. */
21582 if (token->type == CPP_OPEN_BRACE)
21584 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21585 /* In theory this should probably check end == '}', but
21586 cp_parser_save_member_function_body needs it to exit
21587 after either '}' or ')' when called with ')'. */
21591 else if (token->type == CPP_OPEN_PAREN)
21593 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21594 if (depth == 0 && end == CPP_CLOSE_PAREN)
21597 else if (token->type == CPP_PRAGMA)
21598 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21599 else if (token->type == end)
21604 /* Begin parsing tentatively. We always save tokens while parsing
21605 tentatively so that if the tentative parsing fails we can restore the
21609 cp_parser_parse_tentatively (cp_parser* parser)
21611 /* Enter a new parsing context. */
21612 parser->context = cp_parser_context_new (parser->context);
21613 /* Begin saving tokens. */
21614 cp_lexer_save_tokens (parser->lexer);
21615 /* In order to avoid repetitive access control error messages,
21616 access checks are queued up until we are no longer parsing
21618 push_deferring_access_checks (dk_deferred);
21621 /* Commit to the currently active tentative parse. */
21624 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21626 cp_parser_context *context;
21629 /* Mark all of the levels as committed. */
21630 lexer = parser->lexer;
21631 for (context = parser->context; context->next; context = context->next)
21633 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21635 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21636 while (!cp_lexer_saving_tokens (lexer))
21637 lexer = lexer->next;
21638 cp_lexer_commit_tokens (lexer);
21642 /* Abort the currently active tentative parse. All consumed tokens
21643 will be rolled back, and no diagnostics will be issued. */
21646 cp_parser_abort_tentative_parse (cp_parser* parser)
21648 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21649 || errorcount > 0);
21650 cp_parser_simulate_error (parser);
21651 /* Now, pretend that we want to see if the construct was
21652 successfully parsed. */
21653 cp_parser_parse_definitely (parser);
21656 /* Stop parsing tentatively. If a parse error has occurred, restore the
21657 token stream. Otherwise, commit to the tokens we have consumed.
21658 Returns true if no error occurred; false otherwise. */
21661 cp_parser_parse_definitely (cp_parser* parser)
21663 bool error_occurred;
21664 cp_parser_context *context;
21666 /* Remember whether or not an error occurred, since we are about to
21667 destroy that information. */
21668 error_occurred = cp_parser_error_occurred (parser);
21669 /* Remove the topmost context from the stack. */
21670 context = parser->context;
21671 parser->context = context->next;
21672 /* If no parse errors occurred, commit to the tentative parse. */
21673 if (!error_occurred)
21675 /* Commit to the tokens read tentatively, unless that was
21677 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21678 cp_lexer_commit_tokens (parser->lexer);
21680 pop_to_parent_deferring_access_checks ();
21682 /* Otherwise, if errors occurred, roll back our state so that things
21683 are just as they were before we began the tentative parse. */
21686 cp_lexer_rollback_tokens (parser->lexer);
21687 pop_deferring_access_checks ();
21689 /* Add the context to the front of the free list. */
21690 context->next = cp_parser_context_free_list;
21691 cp_parser_context_free_list = context;
21693 return !error_occurred;
21696 /* Returns true if we are parsing tentatively and are not committed to
21697 this tentative parse. */
21700 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21702 return (cp_parser_parsing_tentatively (parser)
21703 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21706 /* Returns nonzero iff an error has occurred during the most recent
21707 tentative parse. */
21710 cp_parser_error_occurred (cp_parser* parser)
21712 return (cp_parser_parsing_tentatively (parser)
21713 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21716 /* Returns nonzero if GNU extensions are allowed. */
21719 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21721 return parser->allow_gnu_extensions_p;
21724 /* Objective-C++ Productions */
21727 /* Parse an Objective-C expression, which feeds into a primary-expression
21731 objc-message-expression
21732 objc-string-literal
21733 objc-encode-expression
21734 objc-protocol-expression
21735 objc-selector-expression
21737 Returns a tree representation of the expression. */
21740 cp_parser_objc_expression (cp_parser* parser)
21742 /* Try to figure out what kind of declaration is present. */
21743 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21747 case CPP_OPEN_SQUARE:
21748 return cp_parser_objc_message_expression (parser);
21750 case CPP_OBJC_STRING:
21751 kwd = cp_lexer_consume_token (parser->lexer);
21752 return objc_build_string_object (kwd->u.value);
21755 switch (kwd->keyword)
21757 case RID_AT_ENCODE:
21758 return cp_parser_objc_encode_expression (parser);
21760 case RID_AT_PROTOCOL:
21761 return cp_parser_objc_protocol_expression (parser);
21763 case RID_AT_SELECTOR:
21764 return cp_parser_objc_selector_expression (parser);
21770 error_at (kwd->location,
21771 "misplaced %<@%D%> Objective-C++ construct",
21773 cp_parser_skip_to_end_of_block_or_statement (parser);
21776 return error_mark_node;
21779 /* Parse an Objective-C message expression.
21781 objc-message-expression:
21782 [ objc-message-receiver objc-message-args ]
21784 Returns a representation of an Objective-C message. */
21787 cp_parser_objc_message_expression (cp_parser* parser)
21789 tree receiver, messageargs;
21791 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21792 receiver = cp_parser_objc_message_receiver (parser);
21793 messageargs = cp_parser_objc_message_args (parser);
21794 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21796 return objc_build_message_expr (receiver, messageargs);
21799 /* Parse an objc-message-receiver.
21801 objc-message-receiver:
21803 simple-type-specifier
21805 Returns a representation of the type or expression. */
21808 cp_parser_objc_message_receiver (cp_parser* parser)
21812 /* An Objective-C message receiver may be either (1) a type
21813 or (2) an expression. */
21814 cp_parser_parse_tentatively (parser);
21815 rcv = cp_parser_expression (parser, false, NULL);
21817 if (cp_parser_parse_definitely (parser))
21820 rcv = cp_parser_simple_type_specifier (parser,
21821 /*decl_specs=*/NULL,
21822 CP_PARSER_FLAGS_NONE);
21824 return objc_get_class_reference (rcv);
21827 /* Parse the arguments and selectors comprising an Objective-C message.
21832 objc-selector-args , objc-comma-args
21834 objc-selector-args:
21835 objc-selector [opt] : assignment-expression
21836 objc-selector-args objc-selector [opt] : assignment-expression
21839 assignment-expression
21840 objc-comma-args , assignment-expression
21842 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21843 selector arguments and TREE_VALUE containing a list of comma
21847 cp_parser_objc_message_args (cp_parser* parser)
21849 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21850 bool maybe_unary_selector_p = true;
21851 cp_token *token = cp_lexer_peek_token (parser->lexer);
21853 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21855 tree selector = NULL_TREE, arg;
21857 if (token->type != CPP_COLON)
21858 selector = cp_parser_objc_selector (parser);
21860 /* Detect if we have a unary selector. */
21861 if (maybe_unary_selector_p
21862 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21863 return build_tree_list (selector, NULL_TREE);
21865 maybe_unary_selector_p = false;
21866 cp_parser_require (parser, CPP_COLON, RT_COLON);
21867 arg = cp_parser_assignment_expression (parser, false, NULL);
21870 = chainon (sel_args,
21871 build_tree_list (selector, arg));
21873 token = cp_lexer_peek_token (parser->lexer);
21876 /* Handle non-selector arguments, if any. */
21877 while (token->type == CPP_COMMA)
21881 cp_lexer_consume_token (parser->lexer);
21882 arg = cp_parser_assignment_expression (parser, false, NULL);
21885 = chainon (addl_args,
21886 build_tree_list (NULL_TREE, arg));
21888 token = cp_lexer_peek_token (parser->lexer);
21891 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21893 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21894 return build_tree_list (error_mark_node, error_mark_node);
21897 return build_tree_list (sel_args, addl_args);
21900 /* Parse an Objective-C encode expression.
21902 objc-encode-expression:
21903 @encode objc-typename
21905 Returns an encoded representation of the type argument. */
21908 cp_parser_objc_encode_expression (cp_parser* parser)
21913 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21914 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21915 token = cp_lexer_peek_token (parser->lexer);
21916 type = complete_type (cp_parser_type_id (parser));
21917 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21921 error_at (token->location,
21922 "%<@encode%> must specify a type as an argument");
21923 return error_mark_node;
21926 /* This happens if we find @encode(T) (where T is a template
21927 typename or something dependent on a template typename) when
21928 parsing a template. In that case, we can't compile it
21929 immediately, but we rather create an AT_ENCODE_EXPR which will
21930 need to be instantiated when the template is used.
21932 if (dependent_type_p (type))
21934 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21935 TREE_READONLY (value) = 1;
21939 return objc_build_encode_expr (type);
21942 /* Parse an Objective-C @defs expression. */
21945 cp_parser_objc_defs_expression (cp_parser *parser)
21949 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21950 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21951 name = cp_parser_identifier (parser);
21952 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21954 return objc_get_class_ivars (name);
21957 /* Parse an Objective-C protocol expression.
21959 objc-protocol-expression:
21960 @protocol ( identifier )
21962 Returns a representation of the protocol expression. */
21965 cp_parser_objc_protocol_expression (cp_parser* parser)
21969 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21970 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21971 proto = cp_parser_identifier (parser);
21972 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21974 return objc_build_protocol_expr (proto);
21977 /* Parse an Objective-C selector expression.
21979 objc-selector-expression:
21980 @selector ( objc-method-signature )
21982 objc-method-signature:
21988 objc-selector-seq objc-selector :
21990 Returns a representation of the method selector. */
21993 cp_parser_objc_selector_expression (cp_parser* parser)
21995 tree sel_seq = NULL_TREE;
21996 bool maybe_unary_selector_p = true;
21998 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22000 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
22001 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22002 token = cp_lexer_peek_token (parser->lexer);
22004 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22005 || token->type == CPP_SCOPE)
22007 tree selector = NULL_TREE;
22009 if (token->type != CPP_COLON
22010 || token->type == CPP_SCOPE)
22011 selector = cp_parser_objc_selector (parser);
22013 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22014 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22016 /* Detect if we have a unary selector. */
22017 if (maybe_unary_selector_p)
22019 sel_seq = selector;
22020 goto finish_selector;
22024 cp_parser_error (parser, "expected %<:%>");
22027 maybe_unary_selector_p = false;
22028 token = cp_lexer_consume_token (parser->lexer);
22030 if (token->type == CPP_SCOPE)
22033 = chainon (sel_seq,
22034 build_tree_list (selector, NULL_TREE));
22036 = chainon (sel_seq,
22037 build_tree_list (NULL_TREE, NULL_TREE));
22041 = chainon (sel_seq,
22042 build_tree_list (selector, NULL_TREE));
22044 token = cp_lexer_peek_token (parser->lexer);
22048 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22050 return objc_build_selector_expr (loc, sel_seq);
22053 /* Parse a list of identifiers.
22055 objc-identifier-list:
22057 objc-identifier-list , identifier
22059 Returns a TREE_LIST of identifier nodes. */
22062 cp_parser_objc_identifier_list (cp_parser* parser)
22068 identifier = cp_parser_identifier (parser);
22069 if (identifier == error_mark_node)
22070 return error_mark_node;
22072 list = build_tree_list (NULL_TREE, identifier);
22073 sep = cp_lexer_peek_token (parser->lexer);
22075 while (sep->type == CPP_COMMA)
22077 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22078 identifier = cp_parser_identifier (parser);
22079 if (identifier == error_mark_node)
22082 list = chainon (list, build_tree_list (NULL_TREE,
22084 sep = cp_lexer_peek_token (parser->lexer);
22090 /* Parse an Objective-C alias declaration.
22092 objc-alias-declaration:
22093 @compatibility_alias identifier identifier ;
22095 This function registers the alias mapping with the Objective-C front end.
22096 It returns nothing. */
22099 cp_parser_objc_alias_declaration (cp_parser* parser)
22103 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
22104 alias = cp_parser_identifier (parser);
22105 orig = cp_parser_identifier (parser);
22106 objc_declare_alias (alias, orig);
22107 cp_parser_consume_semicolon_at_end_of_statement (parser);
22110 /* Parse an Objective-C class forward-declaration.
22112 objc-class-declaration:
22113 @class objc-identifier-list ;
22115 The function registers the forward declarations with the Objective-C
22116 front end. It returns nothing. */
22119 cp_parser_objc_class_declaration (cp_parser* parser)
22121 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
22126 id = cp_parser_identifier (parser);
22127 if (id == error_mark_node)
22130 objc_declare_class (id);
22132 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22133 cp_lexer_consume_token (parser->lexer);
22137 cp_parser_consume_semicolon_at_end_of_statement (parser);
22140 /* Parse a list of Objective-C protocol references.
22142 objc-protocol-refs-opt:
22143 objc-protocol-refs [opt]
22145 objc-protocol-refs:
22146 < objc-identifier-list >
22148 Returns a TREE_LIST of identifiers, if any. */
22151 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22153 tree protorefs = NULL_TREE;
22155 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22157 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
22158 protorefs = cp_parser_objc_identifier_list (parser);
22159 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22165 /* Parse a Objective-C visibility specification. */
22168 cp_parser_objc_visibility_spec (cp_parser* parser)
22170 cp_token *vis = cp_lexer_peek_token (parser->lexer);
22172 switch (vis->keyword)
22174 case RID_AT_PRIVATE:
22175 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22177 case RID_AT_PROTECTED:
22178 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22180 case RID_AT_PUBLIC:
22181 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22183 case RID_AT_PACKAGE:
22184 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22190 /* Eat '@private'/'@protected'/'@public'. */
22191 cp_lexer_consume_token (parser->lexer);
22194 /* Parse an Objective-C method type. Return 'true' if it is a class
22195 (+) method, and 'false' if it is an instance (-) method. */
22198 cp_parser_objc_method_type (cp_parser* parser)
22200 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22206 /* Parse an Objective-C protocol qualifier. */
22209 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
22211 tree quals = NULL_TREE, node;
22212 cp_token *token = cp_lexer_peek_token (parser->lexer);
22214 node = token->u.value;
22216 while (node && TREE_CODE (node) == IDENTIFIER_NODE
22217 && (node == ridpointers [(int) RID_IN]
22218 || node == ridpointers [(int) RID_OUT]
22219 || node == ridpointers [(int) RID_INOUT]
22220 || node == ridpointers [(int) RID_BYCOPY]
22221 || node == ridpointers [(int) RID_BYREF]
22222 || node == ridpointers [(int) RID_ONEWAY]))
22224 quals = tree_cons (NULL_TREE, node, quals);
22225 cp_lexer_consume_token (parser->lexer);
22226 token = cp_lexer_peek_token (parser->lexer);
22227 node = token->u.value;
22233 /* Parse an Objective-C typename. */
22236 cp_parser_objc_typename (cp_parser* parser)
22238 tree type_name = NULL_TREE;
22240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22242 tree proto_quals, cp_type = NULL_TREE;
22244 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22245 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22247 /* An ObjC type name may consist of just protocol qualifiers, in which
22248 case the type shall default to 'id'. */
22249 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22251 cp_type = cp_parser_type_id (parser);
22253 /* If the type could not be parsed, an error has already
22254 been produced. For error recovery, behave as if it had
22255 not been specified, which will use the default type
22257 if (cp_type == error_mark_node)
22259 cp_type = NULL_TREE;
22260 /* We need to skip to the closing parenthesis as
22261 cp_parser_type_id() does not seem to do it for
22263 cp_parser_skip_to_closing_parenthesis (parser,
22264 /*recovering=*/true,
22265 /*or_comma=*/false,
22266 /*consume_paren=*/false);
22270 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22271 type_name = build_tree_list (proto_quals, cp_type);
22277 /* Check to see if TYPE refers to an Objective-C selector name. */
22280 cp_parser_objc_selector_p (enum cpp_ttype type)
22282 return (type == CPP_NAME || type == CPP_KEYWORD
22283 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22284 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22285 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22286 || type == CPP_XOR || type == CPP_XOR_EQ);
22289 /* Parse an Objective-C selector. */
22292 cp_parser_objc_selector (cp_parser* parser)
22294 cp_token *token = cp_lexer_consume_token (parser->lexer);
22296 if (!cp_parser_objc_selector_p (token->type))
22298 error_at (token->location, "invalid Objective-C++ selector name");
22299 return error_mark_node;
22302 /* C++ operator names are allowed to appear in ObjC selectors. */
22303 switch (token->type)
22305 case CPP_AND_AND: return get_identifier ("and");
22306 case CPP_AND_EQ: return get_identifier ("and_eq");
22307 case CPP_AND: return get_identifier ("bitand");
22308 case CPP_OR: return get_identifier ("bitor");
22309 case CPP_COMPL: return get_identifier ("compl");
22310 case CPP_NOT: return get_identifier ("not");
22311 case CPP_NOT_EQ: return get_identifier ("not_eq");
22312 case CPP_OR_OR: return get_identifier ("or");
22313 case CPP_OR_EQ: return get_identifier ("or_eq");
22314 case CPP_XOR: return get_identifier ("xor");
22315 case CPP_XOR_EQ: return get_identifier ("xor_eq");
22316 default: return token->u.value;
22320 /* Parse an Objective-C params list. */
22323 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22325 tree params = NULL_TREE;
22326 bool maybe_unary_selector_p = true;
22327 cp_token *token = cp_lexer_peek_token (parser->lexer);
22329 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22331 tree selector = NULL_TREE, type_name, identifier;
22332 tree parm_attr = NULL_TREE;
22334 if (token->keyword == RID_ATTRIBUTE)
22337 if (token->type != CPP_COLON)
22338 selector = cp_parser_objc_selector (parser);
22340 /* Detect if we have a unary selector. */
22341 if (maybe_unary_selector_p
22342 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22344 params = selector; /* Might be followed by attributes. */
22348 maybe_unary_selector_p = false;
22349 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22351 /* Something went quite wrong. There should be a colon
22352 here, but there is not. Stop parsing parameters. */
22355 type_name = cp_parser_objc_typename (parser);
22356 /* New ObjC allows attributes on parameters too. */
22357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22358 parm_attr = cp_parser_attributes_opt (parser);
22359 identifier = cp_parser_identifier (parser);
22363 objc_build_keyword_decl (selector,
22368 token = cp_lexer_peek_token (parser->lexer);
22371 if (params == NULL_TREE)
22373 cp_parser_error (parser, "objective-c++ method declaration is expected");
22374 return error_mark_node;
22377 /* We allow tail attributes for the method. */
22378 if (token->keyword == RID_ATTRIBUTE)
22380 *attributes = cp_parser_attributes_opt (parser);
22381 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22382 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22384 cp_parser_error (parser,
22385 "method attributes must be specified at the end");
22386 return error_mark_node;
22389 if (params == NULL_TREE)
22391 cp_parser_error (parser, "objective-c++ method declaration is expected");
22392 return error_mark_node;
22397 /* Parse the non-keyword Objective-C params. */
22400 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22403 tree params = make_node (TREE_LIST);
22404 cp_token *token = cp_lexer_peek_token (parser->lexer);
22405 *ellipsisp = false; /* Initially, assume no ellipsis. */
22407 while (token->type == CPP_COMMA)
22409 cp_parameter_declarator *parmdecl;
22412 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22413 token = cp_lexer_peek_token (parser->lexer);
22415 if (token->type == CPP_ELLIPSIS)
22417 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22419 token = cp_lexer_peek_token (parser->lexer);
22423 /* TODO: parse attributes for tail parameters. */
22424 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22425 parm = grokdeclarator (parmdecl->declarator,
22426 &parmdecl->decl_specifiers,
22427 PARM, /*initialized=*/0,
22428 /*attrlist=*/NULL);
22430 chainon (params, build_tree_list (NULL_TREE, parm));
22431 token = cp_lexer_peek_token (parser->lexer);
22434 /* We allow tail attributes for the method. */
22435 if (token->keyword == RID_ATTRIBUTE)
22437 if (*attributes == NULL_TREE)
22439 *attributes = cp_parser_attributes_opt (parser);
22440 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22441 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22445 /* We have an error, but parse the attributes, so that we can
22447 *attributes = cp_parser_attributes_opt (parser);
22449 cp_parser_error (parser,
22450 "method attributes must be specified at the end");
22451 return error_mark_node;
22457 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22460 cp_parser_objc_interstitial_code (cp_parser* parser)
22462 cp_token *token = cp_lexer_peek_token (parser->lexer);
22464 /* If the next token is `extern' and the following token is a string
22465 literal, then we have a linkage specification. */
22466 if (token->keyword == RID_EXTERN
22467 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22468 cp_parser_linkage_specification (parser);
22469 /* Handle #pragma, if any. */
22470 else if (token->type == CPP_PRAGMA)
22471 cp_parser_pragma (parser, pragma_external);
22472 /* Allow stray semicolons. */
22473 else if (token->type == CPP_SEMICOLON)
22474 cp_lexer_consume_token (parser->lexer);
22475 /* Mark methods as optional or required, when building protocols. */
22476 else if (token->keyword == RID_AT_OPTIONAL)
22478 cp_lexer_consume_token (parser->lexer);
22479 objc_set_method_opt (true);
22481 else if (token->keyword == RID_AT_REQUIRED)
22483 cp_lexer_consume_token (parser->lexer);
22484 objc_set_method_opt (false);
22486 else if (token->keyword == RID_NAMESPACE)
22487 cp_parser_namespace_definition (parser);
22488 /* Other stray characters must generate errors. */
22489 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22491 cp_lexer_consume_token (parser->lexer);
22492 error ("stray %qs between Objective-C++ methods",
22493 token->type == CPP_OPEN_BRACE ? "{" : "}");
22495 /* Finally, try to parse a block-declaration, or a function-definition. */
22497 cp_parser_block_declaration (parser, /*statement_p=*/false);
22500 /* Parse a method signature. */
22503 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22505 tree rettype, kwdparms, optparms;
22506 bool ellipsis = false;
22507 bool is_class_method;
22509 is_class_method = cp_parser_objc_method_type (parser);
22510 rettype = cp_parser_objc_typename (parser);
22511 *attributes = NULL_TREE;
22512 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22513 if (kwdparms == error_mark_node)
22514 return error_mark_node;
22515 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22516 if (optparms == error_mark_node)
22517 return error_mark_node;
22519 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22523 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22526 cp_lexer_save_tokens (parser->lexer);
22527 tattr = cp_parser_attributes_opt (parser);
22528 gcc_assert (tattr) ;
22530 /* If the attributes are followed by a method introducer, this is not allowed.
22531 Dump the attributes and flag the situation. */
22532 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22533 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22536 /* Otherwise, the attributes introduce some interstitial code, possibly so
22537 rewind to allow that check. */
22538 cp_lexer_rollback_tokens (parser->lexer);
22542 /* Parse an Objective-C method prototype list. */
22545 cp_parser_objc_method_prototype_list (cp_parser* parser)
22547 cp_token *token = cp_lexer_peek_token (parser->lexer);
22549 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22551 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22553 tree attributes, sig;
22554 bool is_class_method;
22555 if (token->type == CPP_PLUS)
22556 is_class_method = true;
22558 is_class_method = false;
22559 sig = cp_parser_objc_method_signature (parser, &attributes);
22560 if (sig == error_mark_node)
22562 cp_parser_skip_to_end_of_block_or_statement (parser);
22563 token = cp_lexer_peek_token (parser->lexer);
22566 objc_add_method_declaration (is_class_method, sig, attributes);
22567 cp_parser_consume_semicolon_at_end_of_statement (parser);
22569 else if (token->keyword == RID_AT_PROPERTY)
22570 cp_parser_objc_at_property_declaration (parser);
22571 else if (token->keyword == RID_ATTRIBUTE
22572 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22573 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22575 "prefix attributes are ignored for methods");
22577 /* Allow for interspersed non-ObjC++ code. */
22578 cp_parser_objc_interstitial_code (parser);
22580 token = cp_lexer_peek_token (parser->lexer);
22583 if (token->type != CPP_EOF)
22584 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22586 cp_parser_error (parser, "expected %<@end%>");
22588 objc_finish_interface ();
22591 /* Parse an Objective-C method definition list. */
22594 cp_parser_objc_method_definition_list (cp_parser* parser)
22596 cp_token *token = cp_lexer_peek_token (parser->lexer);
22598 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22602 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22605 tree sig, attribute;
22606 bool is_class_method;
22607 if (token->type == CPP_PLUS)
22608 is_class_method = true;
22610 is_class_method = false;
22611 push_deferring_access_checks (dk_deferred);
22612 sig = cp_parser_objc_method_signature (parser, &attribute);
22613 if (sig == error_mark_node)
22615 cp_parser_skip_to_end_of_block_or_statement (parser);
22616 token = cp_lexer_peek_token (parser->lexer);
22619 objc_start_method_definition (is_class_method, sig, attribute,
22622 /* For historical reasons, we accept an optional semicolon. */
22623 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22624 cp_lexer_consume_token (parser->lexer);
22626 ptk = cp_lexer_peek_token (parser->lexer);
22627 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22628 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22630 perform_deferred_access_checks ();
22631 stop_deferring_access_checks ();
22632 meth = cp_parser_function_definition_after_declarator (parser,
22634 pop_deferring_access_checks ();
22635 objc_finish_method_definition (meth);
22638 /* The following case will be removed once @synthesize is
22639 completely implemented. */
22640 else if (token->keyword == RID_AT_PROPERTY)
22641 cp_parser_objc_at_property_declaration (parser);
22642 else if (token->keyword == RID_AT_SYNTHESIZE)
22643 cp_parser_objc_at_synthesize_declaration (parser);
22644 else if (token->keyword == RID_AT_DYNAMIC)
22645 cp_parser_objc_at_dynamic_declaration (parser);
22646 else if (token->keyword == RID_ATTRIBUTE
22647 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22648 warning_at (token->location, OPT_Wattributes,
22649 "prefix attributes are ignored for methods");
22651 /* Allow for interspersed non-ObjC++ code. */
22652 cp_parser_objc_interstitial_code (parser);
22654 token = cp_lexer_peek_token (parser->lexer);
22657 if (token->type != CPP_EOF)
22658 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22660 cp_parser_error (parser, "expected %<@end%>");
22662 objc_finish_implementation ();
22665 /* Parse Objective-C ivars. */
22668 cp_parser_objc_class_ivars (cp_parser* parser)
22670 cp_token *token = cp_lexer_peek_token (parser->lexer);
22672 if (token->type != CPP_OPEN_BRACE)
22673 return; /* No ivars specified. */
22675 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22676 token = cp_lexer_peek_token (parser->lexer);
22678 while (token->type != CPP_CLOSE_BRACE
22679 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22681 cp_decl_specifier_seq declspecs;
22682 int decl_class_or_enum_p;
22683 tree prefix_attributes;
22685 cp_parser_objc_visibility_spec (parser);
22687 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22690 cp_parser_decl_specifier_seq (parser,
22691 CP_PARSER_FLAGS_OPTIONAL,
22693 &decl_class_or_enum_p);
22695 /* auto, register, static, extern, mutable. */
22696 if (declspecs.storage_class != sc_none)
22698 cp_parser_error (parser, "invalid type for instance variable");
22699 declspecs.storage_class = sc_none;
22703 if (declspecs.specs[(int) ds_thread])
22705 cp_parser_error (parser, "invalid type for instance variable");
22706 declspecs.specs[(int) ds_thread] = 0;
22710 if (declspecs.specs[(int) ds_typedef])
22712 cp_parser_error (parser, "invalid type for instance variable");
22713 declspecs.specs[(int) ds_typedef] = 0;
22716 prefix_attributes = declspecs.attributes;
22717 declspecs.attributes = NULL_TREE;
22719 /* Keep going until we hit the `;' at the end of the
22721 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22723 tree width = NULL_TREE, attributes, first_attribute, decl;
22724 cp_declarator *declarator = NULL;
22725 int ctor_dtor_or_conv_p;
22727 /* Check for a (possibly unnamed) bitfield declaration. */
22728 token = cp_lexer_peek_token (parser->lexer);
22729 if (token->type == CPP_COLON)
22732 if (token->type == CPP_NAME
22733 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22736 /* Get the name of the bitfield. */
22737 declarator = make_id_declarator (NULL_TREE,
22738 cp_parser_identifier (parser),
22742 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22743 /* Get the width of the bitfield. */
22745 = cp_parser_constant_expression (parser,
22746 /*allow_non_constant=*/false,
22751 /* Parse the declarator. */
22753 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22754 &ctor_dtor_or_conv_p,
22755 /*parenthesized_p=*/NULL,
22756 /*member_p=*/false);
22759 /* Look for attributes that apply to the ivar. */
22760 attributes = cp_parser_attributes_opt (parser);
22761 /* Remember which attributes are prefix attributes and
22763 first_attribute = attributes;
22764 /* Combine the attributes. */
22765 attributes = chainon (prefix_attributes, attributes);
22768 /* Create the bitfield declaration. */
22769 decl = grokbitfield (declarator, &declspecs,
22773 decl = grokfield (declarator, &declspecs,
22774 NULL_TREE, /*init_const_expr_p=*/false,
22775 NULL_TREE, attributes);
22777 /* Add the instance variable. */
22778 if (decl != error_mark_node && decl != NULL_TREE)
22779 objc_add_instance_variable (decl);
22781 /* Reset PREFIX_ATTRIBUTES. */
22782 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22783 attributes = TREE_CHAIN (attributes);
22785 TREE_CHAIN (attributes) = NULL_TREE;
22787 token = cp_lexer_peek_token (parser->lexer);
22789 if (token->type == CPP_COMMA)
22791 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22797 cp_parser_consume_semicolon_at_end_of_statement (parser);
22798 token = cp_lexer_peek_token (parser->lexer);
22801 if (token->keyword == RID_AT_END)
22802 cp_parser_error (parser, "expected %<}%>");
22804 /* Do not consume the RID_AT_END, so it will be read again as terminating
22805 the @interface of @implementation. */
22806 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22807 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22809 /* For historical reasons, we accept an optional semicolon. */
22810 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22811 cp_lexer_consume_token (parser->lexer);
22814 /* Parse an Objective-C protocol declaration. */
22817 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22819 tree proto, protorefs;
22822 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22823 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22825 tok = cp_lexer_peek_token (parser->lexer);
22826 error_at (tok->location, "identifier expected after %<@protocol%>");
22827 cp_parser_consume_semicolon_at_end_of_statement (parser);
22831 /* See if we have a forward declaration or a definition. */
22832 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22834 /* Try a forward declaration first. */
22835 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22841 id = cp_parser_identifier (parser);
22842 if (id == error_mark_node)
22845 objc_declare_protocol (id, attributes);
22847 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22848 cp_lexer_consume_token (parser->lexer);
22852 cp_parser_consume_semicolon_at_end_of_statement (parser);
22855 /* Ok, we got a full-fledged definition (or at least should). */
22858 proto = cp_parser_identifier (parser);
22859 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22860 objc_start_protocol (proto, protorefs, attributes);
22861 cp_parser_objc_method_prototype_list (parser);
22865 /* Parse an Objective-C superclass or category. */
22868 cp_parser_objc_superclass_or_category (cp_parser *parser,
22871 tree *categ, bool *is_class_extension)
22873 cp_token *next = cp_lexer_peek_token (parser->lexer);
22875 *super = *categ = NULL_TREE;
22876 *is_class_extension = false;
22877 if (next->type == CPP_COLON)
22879 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22880 *super = cp_parser_identifier (parser);
22882 else if (next->type == CPP_OPEN_PAREN)
22884 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22886 /* If there is no category name, and this is an @interface, we
22887 have a class extension. */
22888 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22890 *categ = NULL_TREE;
22891 *is_class_extension = true;
22894 *categ = cp_parser_identifier (parser);
22896 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22900 /* Parse an Objective-C class interface. */
22903 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22905 tree name, super, categ, protos;
22906 bool is_class_extension;
22908 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22909 name = cp_parser_identifier (parser);
22910 if (name == error_mark_node)
22912 /* It's hard to recover because even if valid @interface stuff
22913 is to follow, we can't compile it (or validate it) if we
22914 don't even know which class it refers to. Let's assume this
22915 was a stray '@interface' token in the stream and skip it.
22919 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22920 &is_class_extension);
22921 protos = cp_parser_objc_protocol_refs_opt (parser);
22923 /* We have either a class or a category on our hands. */
22924 if (categ || is_class_extension)
22925 objc_start_category_interface (name, categ, protos, attributes);
22928 objc_start_class_interface (name, super, protos, attributes);
22929 /* Handle instance variable declarations, if any. */
22930 cp_parser_objc_class_ivars (parser);
22931 objc_continue_interface ();
22934 cp_parser_objc_method_prototype_list (parser);
22937 /* Parse an Objective-C class implementation. */
22940 cp_parser_objc_class_implementation (cp_parser* parser)
22942 tree name, super, categ;
22943 bool is_class_extension;
22945 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22946 name = cp_parser_identifier (parser);
22947 if (name == error_mark_node)
22949 /* It's hard to recover because even if valid @implementation
22950 stuff is to follow, we can't compile it (or validate it) if
22951 we don't even know which class it refers to. Let's assume
22952 this was a stray '@implementation' token in the stream and
22957 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22958 &is_class_extension);
22960 /* We have either a class or a category on our hands. */
22962 objc_start_category_implementation (name, categ);
22965 objc_start_class_implementation (name, super);
22966 /* Handle instance variable declarations, if any. */
22967 cp_parser_objc_class_ivars (parser);
22968 objc_continue_implementation ();
22971 cp_parser_objc_method_definition_list (parser);
22974 /* Consume the @end token and finish off the implementation. */
22977 cp_parser_objc_end_implementation (cp_parser* parser)
22979 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22980 objc_finish_implementation ();
22983 /* Parse an Objective-C declaration. */
22986 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22988 /* Try to figure out what kind of declaration is present. */
22989 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22992 switch (kwd->keyword)
22997 error_at (kwd->location, "attributes may not be specified before"
22998 " the %<@%D%> Objective-C++ keyword",
23002 case RID_AT_IMPLEMENTATION:
23003 warning_at (kwd->location, OPT_Wattributes,
23004 "prefix attributes are ignored before %<@%D%>",
23011 switch (kwd->keyword)
23014 cp_parser_objc_alias_declaration (parser);
23017 cp_parser_objc_class_declaration (parser);
23019 case RID_AT_PROTOCOL:
23020 cp_parser_objc_protocol_declaration (parser, attributes);
23022 case RID_AT_INTERFACE:
23023 cp_parser_objc_class_interface (parser, attributes);
23025 case RID_AT_IMPLEMENTATION:
23026 cp_parser_objc_class_implementation (parser);
23029 cp_parser_objc_end_implementation (parser);
23032 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23034 cp_parser_skip_to_end_of_block_or_statement (parser);
23038 /* Parse an Objective-C try-catch-finally statement.
23040 objc-try-catch-finally-stmt:
23041 @try compound-statement objc-catch-clause-seq [opt]
23042 objc-finally-clause [opt]
23044 objc-catch-clause-seq:
23045 objc-catch-clause objc-catch-clause-seq [opt]
23048 @catch ( objc-exception-declaration ) compound-statement
23050 objc-finally-clause:
23051 @finally compound-statement
23053 objc-exception-declaration:
23054 parameter-declaration
23057 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23061 PS: This function is identical to c_parser_objc_try_catch_finally_statement
23062 for C. Keep them in sync. */
23065 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23067 location_t location;
23070 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23071 location = cp_lexer_peek_token (parser->lexer)->location;
23072 objc_maybe_warn_exceptions (location);
23073 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23074 node, lest it get absorbed into the surrounding block. */
23075 stmt = push_stmt_list ();
23076 cp_parser_compound_statement (parser, NULL, false, false);
23077 objc_begin_try_stmt (location, pop_stmt_list (stmt));
23079 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23081 cp_parameter_declarator *parm;
23082 tree parameter_declaration = error_mark_node;
23083 bool seen_open_paren = false;
23085 cp_lexer_consume_token (parser->lexer);
23086 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23087 seen_open_paren = true;
23088 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23090 /* We have "@catch (...)" (where the '...' are literally
23091 what is in the code). Skip the '...'.
23092 parameter_declaration is set to NULL_TREE, and
23093 objc_being_catch_clauses() knows that that means
23095 cp_lexer_consume_token (parser->lexer);
23096 parameter_declaration = NULL_TREE;
23100 /* We have "@catch (NSException *exception)" or something
23101 like that. Parse the parameter declaration. */
23102 parm = cp_parser_parameter_declaration (parser, false, NULL);
23104 parameter_declaration = error_mark_node;
23106 parameter_declaration = grokdeclarator (parm->declarator,
23107 &parm->decl_specifiers,
23108 PARM, /*initialized=*/0,
23109 /*attrlist=*/NULL);
23111 if (seen_open_paren)
23112 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23115 /* If there was no open parenthesis, we are recovering from
23116 an error, and we are trying to figure out what mistake
23117 the user has made. */
23119 /* If there is an immediate closing parenthesis, the user
23120 probably forgot the opening one (ie, they typed "@catch
23121 NSException *e)". Parse the closing parenthesis and keep
23123 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23124 cp_lexer_consume_token (parser->lexer);
23126 /* If these is no immediate closing parenthesis, the user
23127 probably doesn't know that parenthesis are required at
23128 all (ie, they typed "@catch NSException *e"). So, just
23129 forget about the closing parenthesis and keep going. */
23131 objc_begin_catch_clause (parameter_declaration);
23132 cp_parser_compound_statement (parser, NULL, false, false);
23133 objc_finish_catch_clause ();
23135 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23137 cp_lexer_consume_token (parser->lexer);
23138 location = cp_lexer_peek_token (parser->lexer)->location;
23139 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23140 node, lest it get absorbed into the surrounding block. */
23141 stmt = push_stmt_list ();
23142 cp_parser_compound_statement (parser, NULL, false, false);
23143 objc_build_finally_clause (location, pop_stmt_list (stmt));
23146 return objc_finish_try_stmt ();
23149 /* Parse an Objective-C synchronized statement.
23151 objc-synchronized-stmt:
23152 @synchronized ( expression ) compound-statement
23154 Returns NULL_TREE. */
23157 cp_parser_objc_synchronized_statement (cp_parser *parser)
23159 location_t location;
23162 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23164 location = cp_lexer_peek_token (parser->lexer)->location;
23165 objc_maybe_warn_exceptions (location);
23166 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23167 lock = cp_parser_expression (parser, false, NULL);
23168 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23170 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23171 node, lest it get absorbed into the surrounding block. */
23172 stmt = push_stmt_list ();
23173 cp_parser_compound_statement (parser, NULL, false, false);
23175 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23178 /* Parse an Objective-C throw statement.
23181 @throw assignment-expression [opt] ;
23183 Returns a constructed '@throw' statement. */
23186 cp_parser_objc_throw_statement (cp_parser *parser)
23188 tree expr = NULL_TREE;
23189 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23191 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23193 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23194 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23196 cp_parser_consume_semicolon_at_end_of_statement (parser);
23198 return objc_build_throw_stmt (loc, expr);
23201 /* Parse an Objective-C statement. */
23204 cp_parser_objc_statement (cp_parser * parser)
23206 /* Try to figure out what kind of declaration is present. */
23207 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23209 switch (kwd->keyword)
23212 return cp_parser_objc_try_catch_finally_statement (parser);
23213 case RID_AT_SYNCHRONIZED:
23214 return cp_parser_objc_synchronized_statement (parser);
23216 return cp_parser_objc_throw_statement (parser);
23218 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23220 cp_parser_skip_to_end_of_block_or_statement (parser);
23223 return error_mark_node;
23226 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
23227 look ahead to see if an objc keyword follows the attributes. This
23228 is to detect the use of prefix attributes on ObjC @interface and
23232 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23234 cp_lexer_save_tokens (parser->lexer);
23235 *attrib = cp_parser_attributes_opt (parser);
23236 gcc_assert (*attrib);
23237 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23239 cp_lexer_commit_tokens (parser->lexer);
23242 cp_lexer_rollback_tokens (parser->lexer);
23246 /* This routine is a minimal replacement for
23247 c_parser_struct_declaration () used when parsing the list of
23248 types/names or ObjC++ properties. For example, when parsing the
23251 @property (readonly) int a, b, c;
23253 this function is responsible for parsing "int a, int b, int c" and
23254 returning the declarations as CHAIN of DECLs.
23256 TODO: Share this code with cp_parser_objc_class_ivars. It's very
23257 similar parsing. */
23259 cp_parser_objc_struct_declaration (cp_parser *parser)
23261 tree decls = NULL_TREE;
23262 cp_decl_specifier_seq declspecs;
23263 int decl_class_or_enum_p;
23264 tree prefix_attributes;
23266 cp_parser_decl_specifier_seq (parser,
23267 CP_PARSER_FLAGS_NONE,
23269 &decl_class_or_enum_p);
23271 if (declspecs.type == error_mark_node)
23272 return error_mark_node;
23274 /* auto, register, static, extern, mutable. */
23275 if (declspecs.storage_class != sc_none)
23277 cp_parser_error (parser, "invalid type for property");
23278 declspecs.storage_class = sc_none;
23282 if (declspecs.specs[(int) ds_thread])
23284 cp_parser_error (parser, "invalid type for property");
23285 declspecs.specs[(int) ds_thread] = 0;
23289 if (declspecs.specs[(int) ds_typedef])
23291 cp_parser_error (parser, "invalid type for property");
23292 declspecs.specs[(int) ds_typedef] = 0;
23295 prefix_attributes = declspecs.attributes;
23296 declspecs.attributes = NULL_TREE;
23298 /* Keep going until we hit the `;' at the end of the declaration. */
23299 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23301 tree attributes, first_attribute, decl;
23302 cp_declarator *declarator;
23305 /* Parse the declarator. */
23306 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23307 NULL, NULL, false);
23309 /* Look for attributes that apply to the ivar. */
23310 attributes = cp_parser_attributes_opt (parser);
23311 /* Remember which attributes are prefix attributes and
23313 first_attribute = attributes;
23314 /* Combine the attributes. */
23315 attributes = chainon (prefix_attributes, attributes);
23317 decl = grokfield (declarator, &declspecs,
23318 NULL_TREE, /*init_const_expr_p=*/false,
23319 NULL_TREE, attributes);
23321 if (decl == error_mark_node || decl == NULL_TREE)
23322 return error_mark_node;
23324 /* Reset PREFIX_ATTRIBUTES. */
23325 while (attributes && TREE_CHAIN (attributes) != first_attribute)
23326 attributes = TREE_CHAIN (attributes);
23328 TREE_CHAIN (attributes) = NULL_TREE;
23330 DECL_CHAIN (decl) = decls;
23333 token = cp_lexer_peek_token (parser->lexer);
23334 if (token->type == CPP_COMMA)
23336 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
23345 /* Parse an Objective-C @property declaration. The syntax is:
23347 objc-property-declaration:
23348 '@property' objc-property-attributes[opt] struct-declaration ;
23350 objc-property-attributes:
23351 '(' objc-property-attribute-list ')'
23353 objc-property-attribute-list:
23354 objc-property-attribute
23355 objc-property-attribute-list, objc-property-attribute
23357 objc-property-attribute
23358 'getter' = identifier
23359 'setter' = identifier
23368 @property NSString *name;
23369 @property (readonly) id object;
23370 @property (retain, nonatomic, getter=getTheName) id name;
23371 @property int a, b, c;
23373 PS: This function is identical to
23374 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23376 cp_parser_objc_at_property_declaration (cp_parser *parser)
23378 /* The following variables hold the attributes of the properties as
23379 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23380 seen. When we see an attribute, we set them to 'true' (if they
23381 are boolean properties) or to the identifier (if they have an
23382 argument, ie, for getter and setter). Note that here we only
23383 parse the list of attributes, check the syntax and accumulate the
23384 attributes that we find. objc_add_property_declaration() will
23385 then process the information. */
23386 bool property_assign = false;
23387 bool property_copy = false;
23388 tree property_getter_ident = NULL_TREE;
23389 bool property_nonatomic = false;
23390 bool property_readonly = false;
23391 bool property_readwrite = false;
23392 bool property_retain = false;
23393 tree property_setter_ident = NULL_TREE;
23395 /* 'properties' is the list of properties that we read. Usually a
23396 single one, but maybe more (eg, in "@property int a, b, c;" there
23401 loc = cp_lexer_peek_token (parser->lexer)->location;
23403 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23405 /* Parse the optional attribute list... */
23406 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23409 cp_lexer_consume_token (parser->lexer);
23413 bool syntax_error = false;
23414 cp_token *token = cp_lexer_peek_token (parser->lexer);
23417 if (token->type != CPP_NAME)
23419 cp_parser_error (parser, "expected identifier");
23422 keyword = C_RID_CODE (token->u.value);
23423 cp_lexer_consume_token (parser->lexer);
23426 case RID_ASSIGN: property_assign = true; break;
23427 case RID_COPY: property_copy = true; break;
23428 case RID_NONATOMIC: property_nonatomic = true; break;
23429 case RID_READONLY: property_readonly = true; break;
23430 case RID_READWRITE: property_readwrite = true; break;
23431 case RID_RETAIN: property_retain = true; break;
23435 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23437 if (keyword == RID_GETTER)
23438 cp_parser_error (parser,
23439 "missing %<=%> (after %<getter%> attribute)");
23441 cp_parser_error (parser,
23442 "missing %<=%> (after %<setter%> attribute)");
23443 syntax_error = true;
23446 cp_lexer_consume_token (parser->lexer); /* eat the = */
23447 if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23449 cp_parser_error (parser, "expected identifier");
23450 syntax_error = true;
23453 if (keyword == RID_SETTER)
23455 if (property_setter_ident != NULL_TREE)
23457 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23458 cp_lexer_consume_token (parser->lexer);
23461 property_setter_ident = cp_parser_objc_selector (parser);
23462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23463 cp_parser_error (parser, "setter name must terminate with %<:%>");
23465 cp_lexer_consume_token (parser->lexer);
23469 if (property_getter_ident != NULL_TREE)
23471 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23472 cp_lexer_consume_token (parser->lexer);
23475 property_getter_ident = cp_parser_objc_selector (parser);
23479 cp_parser_error (parser, "unknown property attribute");
23480 syntax_error = true;
23487 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23488 cp_lexer_consume_token (parser->lexer);
23493 /* FIXME: "@property (setter, assign);" will generate a spurious
23494 "error: expected ‘)’ before ‘,’ token". This is because
23495 cp_parser_require, unlike the C counterpart, will produce an
23496 error even if we are in error recovery. */
23497 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23499 cp_parser_skip_to_closing_parenthesis (parser,
23500 /*recovering=*/true,
23501 /*or_comma=*/false,
23502 /*consume_paren=*/true);
23506 /* ... and the property declaration(s). */
23507 properties = cp_parser_objc_struct_declaration (parser);
23509 if (properties == error_mark_node)
23511 cp_parser_skip_to_end_of_statement (parser);
23512 /* If the next token is now a `;', consume it. */
23513 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23514 cp_lexer_consume_token (parser->lexer);
23518 if (properties == NULL_TREE)
23519 cp_parser_error (parser, "expected identifier");
23522 /* Comma-separated properties are chained together in
23523 reverse order; add them one by one. */
23524 properties = nreverse (properties);
23526 for (; properties; properties = TREE_CHAIN (properties))
23527 objc_add_property_declaration (loc, copy_node (properties),
23528 property_readonly, property_readwrite,
23529 property_assign, property_retain,
23530 property_copy, property_nonatomic,
23531 property_getter_ident, property_setter_ident);
23534 cp_parser_consume_semicolon_at_end_of_statement (parser);
23537 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23539 objc-synthesize-declaration:
23540 @synthesize objc-synthesize-identifier-list ;
23542 objc-synthesize-identifier-list:
23543 objc-synthesize-identifier
23544 objc-synthesize-identifier-list, objc-synthesize-identifier
23546 objc-synthesize-identifier
23548 identifier = identifier
23551 @synthesize MyProperty;
23552 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23554 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23555 for C. Keep them in sync.
23558 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23560 tree list = NULL_TREE;
23562 loc = cp_lexer_peek_token (parser->lexer)->location;
23564 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23567 tree property, ivar;
23568 property = cp_parser_identifier (parser);
23569 if (property == error_mark_node)
23571 cp_parser_consume_semicolon_at_end_of_statement (parser);
23574 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23576 cp_lexer_consume_token (parser->lexer);
23577 ivar = cp_parser_identifier (parser);
23578 if (ivar == error_mark_node)
23580 cp_parser_consume_semicolon_at_end_of_statement (parser);
23586 list = chainon (list, build_tree_list (ivar, property));
23587 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23588 cp_lexer_consume_token (parser->lexer);
23592 cp_parser_consume_semicolon_at_end_of_statement (parser);
23593 objc_add_synthesize_declaration (loc, list);
23596 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23598 objc-dynamic-declaration:
23599 @dynamic identifier-list ;
23602 @dynamic MyProperty;
23603 @dynamic MyProperty, AnotherProperty;
23605 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23606 for C. Keep them in sync.
23609 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23611 tree list = NULL_TREE;
23613 loc = cp_lexer_peek_token (parser->lexer)->location;
23615 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23619 property = cp_parser_identifier (parser);
23620 if (property == error_mark_node)
23622 cp_parser_consume_semicolon_at_end_of_statement (parser);
23625 list = chainon (list, build_tree_list (NULL, property));
23626 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23627 cp_lexer_consume_token (parser->lexer);
23631 cp_parser_consume_semicolon_at_end_of_statement (parser);
23632 objc_add_dynamic_declaration (loc, list);
23636 /* OpenMP 2.5 parsing routines. */
23638 /* Returns name of the next clause.
23639 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23640 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23641 returned and the token is consumed. */
23643 static pragma_omp_clause
23644 cp_parser_omp_clause_name (cp_parser *parser)
23646 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23648 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23649 result = PRAGMA_OMP_CLAUSE_IF;
23650 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23651 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23652 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23653 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23654 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23656 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23657 const char *p = IDENTIFIER_POINTER (id);
23662 if (!strcmp ("collapse", p))
23663 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23664 else if (!strcmp ("copyin", p))
23665 result = PRAGMA_OMP_CLAUSE_COPYIN;
23666 else if (!strcmp ("copyprivate", p))
23667 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23670 if (!strcmp ("final", p))
23671 result = PRAGMA_OMP_CLAUSE_FINAL;
23672 else if (!strcmp ("firstprivate", p))
23673 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23676 if (!strcmp ("lastprivate", p))
23677 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23680 if (!strcmp ("mergeable", p))
23681 result = PRAGMA_OMP_CLAUSE_MERGEABLE;
23684 if (!strcmp ("nowait", p))
23685 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23686 else if (!strcmp ("num_threads", p))
23687 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23690 if (!strcmp ("ordered", p))
23691 result = PRAGMA_OMP_CLAUSE_ORDERED;
23694 if (!strcmp ("reduction", p))
23695 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23698 if (!strcmp ("schedule", p))
23699 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23700 else if (!strcmp ("shared", p))
23701 result = PRAGMA_OMP_CLAUSE_SHARED;
23704 if (!strcmp ("untied", p))
23705 result = PRAGMA_OMP_CLAUSE_UNTIED;
23710 if (result != PRAGMA_OMP_CLAUSE_NONE)
23711 cp_lexer_consume_token (parser->lexer);
23716 /* Validate that a clause of the given type does not already exist. */
23719 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23720 const char *name, location_t location)
23724 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23725 if (OMP_CLAUSE_CODE (c) == code)
23727 error_at (location, "too many %qs clauses", name);
23735 variable-list , identifier
23737 In addition, we match a closing parenthesis. An opening parenthesis
23738 will have been consumed by the caller.
23740 If KIND is nonzero, create the appropriate node and install the decl
23741 in OMP_CLAUSE_DECL and add the node to the head of the list.
23743 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23744 return the list created. */
23747 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23755 token = cp_lexer_peek_token (parser->lexer);
23756 name = cp_parser_id_expression (parser, /*template_p=*/false,
23757 /*check_dependency_p=*/true,
23758 /*template_p=*/NULL,
23759 /*declarator_p=*/false,
23760 /*optional_p=*/false);
23761 if (name == error_mark_node)
23764 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23765 if (decl == error_mark_node)
23766 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23768 else if (kind != 0)
23770 tree u = build_omp_clause (token->location, kind);
23771 OMP_CLAUSE_DECL (u) = decl;
23772 OMP_CLAUSE_CHAIN (u) = list;
23776 list = tree_cons (decl, NULL_TREE, list);
23779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23781 cp_lexer_consume_token (parser->lexer);
23784 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23788 /* Try to resync to an unnested comma. Copied from
23789 cp_parser_parenthesized_expression_list. */
23791 ending = cp_parser_skip_to_closing_parenthesis (parser,
23792 /*recovering=*/true,
23794 /*consume_paren=*/true);
23802 /* Similarly, but expect leading and trailing parenthesis. This is a very
23803 common case for omp clauses. */
23806 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23808 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23809 return cp_parser_omp_var_list_no_open (parser, kind, list);
23814 collapse ( constant-expression ) */
23817 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23823 loc = cp_lexer_peek_token (parser->lexer)->location;
23824 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23827 num = cp_parser_constant_expression (parser, false, NULL);
23829 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23830 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23831 /*or_comma=*/false,
23832 /*consume_paren=*/true);
23834 if (num == error_mark_node)
23836 num = fold_non_dependent_expr (num);
23837 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23838 || !host_integerp (num, 0)
23839 || (n = tree_low_cst (num, 0)) <= 0
23842 error_at (loc, "collapse argument needs positive constant integer expression");
23846 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23847 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23848 OMP_CLAUSE_CHAIN (c) = list;
23849 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23855 default ( shared | none ) */
23858 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23860 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23865 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23867 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23868 const char *p = IDENTIFIER_POINTER (id);
23873 if (strcmp ("none", p) != 0)
23875 kind = OMP_CLAUSE_DEFAULT_NONE;
23879 if (strcmp ("shared", p) != 0)
23881 kind = OMP_CLAUSE_DEFAULT_SHARED;
23888 cp_lexer_consume_token (parser->lexer);
23893 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23896 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23897 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23898 /*or_comma=*/false,
23899 /*consume_paren=*/true);
23901 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23904 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23905 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23906 OMP_CLAUSE_CHAIN (c) = list;
23907 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23913 final ( expression ) */
23916 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
23920 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23923 t = cp_parser_condition (parser);
23925 if (t == error_mark_node
23926 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23927 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23928 /*or_comma=*/false,
23929 /*consume_paren=*/true);
23931 check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
23933 c = build_omp_clause (location, OMP_CLAUSE_FINAL);
23934 OMP_CLAUSE_FINAL_EXPR (c) = t;
23935 OMP_CLAUSE_CHAIN (c) = list;
23941 if ( expression ) */
23944 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23948 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23951 t = cp_parser_condition (parser);
23953 if (t == error_mark_node
23954 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23955 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23956 /*or_comma=*/false,
23957 /*consume_paren=*/true);
23959 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23961 c = build_omp_clause (location, OMP_CLAUSE_IF);
23962 OMP_CLAUSE_IF_EXPR (c) = t;
23963 OMP_CLAUSE_CHAIN (c) = list;
23972 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
23973 tree list, location_t location)
23977 check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
23980 c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
23981 OMP_CLAUSE_CHAIN (c) = list;
23989 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23990 tree list, location_t location)
23994 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23996 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23997 OMP_CLAUSE_CHAIN (c) = list;
24002 num_threads ( expression ) */
24005 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24006 location_t location)
24010 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24013 t = cp_parser_expression (parser, false, NULL);
24015 if (t == error_mark_node
24016 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24017 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24018 /*or_comma=*/false,
24019 /*consume_paren=*/true);
24021 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24022 "num_threads", location);
24024 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24025 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24026 OMP_CLAUSE_CHAIN (c) = list;
24035 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24036 tree list, location_t location)
24040 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24041 "ordered", location);
24043 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24044 OMP_CLAUSE_CHAIN (c) = list;
24049 reduction ( reduction-operator : variable-list )
24051 reduction-operator:
24052 One of: + * - & ^ | && ||
24056 reduction-operator:
24057 One of: + * - & ^ | && || min max */
24060 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24062 enum tree_code code;
24065 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24068 switch (cp_lexer_peek_token (parser->lexer)->type)
24080 code = BIT_AND_EXPR;
24083 code = BIT_XOR_EXPR;
24086 code = BIT_IOR_EXPR;
24089 code = TRUTH_ANDIF_EXPR;
24092 code = TRUTH_ORIF_EXPR;
24096 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24097 const char *p = IDENTIFIER_POINTER (id);
24099 if (strcmp (p, "min") == 0)
24104 if (strcmp (p, "max") == 0)
24112 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24113 "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24116 /*or_comma=*/false,
24117 /*consume_paren=*/true);
24120 cp_lexer_consume_token (parser->lexer);
24122 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24125 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24126 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24127 OMP_CLAUSE_REDUCTION_CODE (c) = code;
24133 schedule ( schedule-kind )
24134 schedule ( schedule-kind , expression )
24137 static | dynamic | guided | runtime | auto */
24140 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24144 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24147 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24149 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24151 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24152 const char *p = IDENTIFIER_POINTER (id);
24157 if (strcmp ("dynamic", p) != 0)
24159 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24163 if (strcmp ("guided", p) != 0)
24165 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24169 if (strcmp ("runtime", p) != 0)
24171 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24178 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24179 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24180 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24181 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24184 cp_lexer_consume_token (parser->lexer);
24186 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24189 cp_lexer_consume_token (parser->lexer);
24191 token = cp_lexer_peek_token (parser->lexer);
24192 t = cp_parser_assignment_expression (parser, false, NULL);
24194 if (t == error_mark_node)
24196 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24197 error_at (token->location, "schedule %<runtime%> does not take "
24198 "a %<chunk_size%> parameter");
24199 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24200 error_at (token->location, "schedule %<auto%> does not take "
24201 "a %<chunk_size%> parameter");
24203 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24205 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24208 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
24211 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
24212 OMP_CLAUSE_CHAIN (c) = list;
24216 cp_parser_error (parser, "invalid schedule kind");
24218 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24219 /*or_comma=*/false,
24220 /*consume_paren=*/true);
24228 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
24229 tree list, location_t location)
24233 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
24235 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
24236 OMP_CLAUSE_CHAIN (c) = list;
24240 /* Parse all OpenMP clauses. The set clauses allowed by the directive
24241 is a bitmask in MASK. Return the list of clauses found; the result
24242 of clause default goes in *pdefault. */
24245 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
24246 const char *where, cp_token *pragma_tok)
24248 tree clauses = NULL;
24250 cp_token *token = NULL;
24252 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
24254 pragma_omp_clause c_kind;
24255 const char *c_name;
24256 tree prev = clauses;
24258 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24259 cp_lexer_consume_token (parser->lexer);
24261 token = cp_lexer_peek_token (parser->lexer);
24262 c_kind = cp_parser_omp_clause_name (parser);
24267 case PRAGMA_OMP_CLAUSE_COLLAPSE:
24268 clauses = cp_parser_omp_clause_collapse (parser, clauses,
24270 c_name = "collapse";
24272 case PRAGMA_OMP_CLAUSE_COPYIN:
24273 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
24276 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
24277 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
24279 c_name = "copyprivate";
24281 case PRAGMA_OMP_CLAUSE_DEFAULT:
24282 clauses = cp_parser_omp_clause_default (parser, clauses,
24284 c_name = "default";
24286 case PRAGMA_OMP_CLAUSE_FINAL:
24287 clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
24290 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
24291 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
24293 c_name = "firstprivate";
24295 case PRAGMA_OMP_CLAUSE_IF:
24296 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
24299 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24300 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24302 c_name = "lastprivate";
24304 case PRAGMA_OMP_CLAUSE_MERGEABLE:
24305 clauses = cp_parser_omp_clause_mergeable (parser, clauses,
24307 c_name = "mergeable";
24309 case PRAGMA_OMP_CLAUSE_NOWAIT:
24310 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24313 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24314 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24316 c_name = "num_threads";
24318 case PRAGMA_OMP_CLAUSE_ORDERED:
24319 clauses = cp_parser_omp_clause_ordered (parser, clauses,
24321 c_name = "ordered";
24323 case PRAGMA_OMP_CLAUSE_PRIVATE:
24324 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24326 c_name = "private";
24328 case PRAGMA_OMP_CLAUSE_REDUCTION:
24329 clauses = cp_parser_omp_clause_reduction (parser, clauses);
24330 c_name = "reduction";
24332 case PRAGMA_OMP_CLAUSE_SCHEDULE:
24333 clauses = cp_parser_omp_clause_schedule (parser, clauses,
24335 c_name = "schedule";
24337 case PRAGMA_OMP_CLAUSE_SHARED:
24338 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24342 case PRAGMA_OMP_CLAUSE_UNTIED:
24343 clauses = cp_parser_omp_clause_untied (parser, clauses,
24348 cp_parser_error (parser, "expected %<#pragma omp%> clause");
24352 if (((mask >> c_kind) & 1) == 0)
24354 /* Remove the invalid clause(s) from the list to avoid
24355 confusing the rest of the compiler. */
24357 error_at (token->location, "%qs is not valid for %qs", c_name, where);
24361 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24362 return finish_omp_clauses (clauses);
24369 In practice, we're also interested in adding the statement to an
24370 outer node. So it is convenient if we work around the fact that
24371 cp_parser_statement calls add_stmt. */
24374 cp_parser_begin_omp_structured_block (cp_parser *parser)
24376 unsigned save = parser->in_statement;
24378 /* Only move the values to IN_OMP_BLOCK if they weren't false.
24379 This preserves the "not within loop or switch" style error messages
24380 for nonsense cases like
24386 if (parser->in_statement)
24387 parser->in_statement = IN_OMP_BLOCK;
24393 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24395 parser->in_statement = save;
24399 cp_parser_omp_structured_block (cp_parser *parser)
24401 tree stmt = begin_omp_structured_block ();
24402 unsigned int save = cp_parser_begin_omp_structured_block (parser);
24404 cp_parser_statement (parser, NULL_TREE, false, NULL);
24406 cp_parser_end_omp_structured_block (parser, save);
24407 return finish_omp_structured_block (stmt);
24411 # pragma omp atomic new-line
24415 x binop= expr | x++ | ++x | x-- | --x
24417 +, *, -, /, &, ^, |, <<, >>
24419 where x is an lvalue expression with scalar type.
24422 # pragma omp atomic new-line
24425 # pragma omp atomic read new-line
24428 # pragma omp atomic write new-line
24431 # pragma omp atomic update new-line
24434 # pragma omp atomic capture new-line
24437 # pragma omp atomic capture new-line
24445 expression-stmt | x = x binop expr
24447 v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
24449 { v = x; update-stmt; } | { update-stmt; v = x; }
24451 where x and v are lvalue expressions with scalar type. */
24454 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24456 tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
24457 tree rhs1 = NULL_TREE, orig_lhs;
24458 enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
24459 bool structured_block = false;
24461 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24463 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24464 const char *p = IDENTIFIER_POINTER (id);
24466 if (!strcmp (p, "read"))
24467 code = OMP_ATOMIC_READ;
24468 else if (!strcmp (p, "write"))
24470 else if (!strcmp (p, "update"))
24472 else if (!strcmp (p, "capture"))
24473 code = OMP_ATOMIC_CAPTURE_NEW;
24477 cp_lexer_consume_token (parser->lexer);
24479 cp_parser_require_pragma_eol (parser, pragma_tok);
24483 case OMP_ATOMIC_READ:
24484 case NOP_EXPR: /* atomic write */
24485 v = cp_parser_unary_expression (parser, /*address_p=*/false,
24486 /*cast_p=*/false, NULL);
24487 if (v == error_mark_node)
24489 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24491 if (code == NOP_EXPR)
24492 lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24494 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24495 /*cast_p=*/false, NULL);
24496 if (lhs == error_mark_node)
24498 if (code == NOP_EXPR)
24500 /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
24508 case OMP_ATOMIC_CAPTURE_NEW:
24509 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24511 cp_lexer_consume_token (parser->lexer);
24512 structured_block = true;
24516 v = cp_parser_unary_expression (parser, /*address_p=*/false,
24517 /*cast_p=*/false, NULL);
24518 if (v == error_mark_node)
24520 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24528 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24529 /*cast_p=*/false, NULL);
24531 switch (TREE_CODE (lhs))
24536 case POSTINCREMENT_EXPR:
24537 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24538 code = OMP_ATOMIC_CAPTURE_OLD;
24540 case PREINCREMENT_EXPR:
24541 lhs = TREE_OPERAND (lhs, 0);
24542 opcode = PLUS_EXPR;
24543 rhs = integer_one_node;
24546 case POSTDECREMENT_EXPR:
24547 if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24548 code = OMP_ATOMIC_CAPTURE_OLD;
24550 case PREDECREMENT_EXPR:
24551 lhs = TREE_OPERAND (lhs, 0);
24552 opcode = MINUS_EXPR;
24553 rhs = integer_one_node;
24556 case COMPOUND_EXPR:
24557 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24558 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24559 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24560 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24561 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24562 (TREE_OPERAND (lhs, 1), 0), 0)))
24564 /* Undo effects of boolean_increment for post {in,de}crement. */
24565 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24568 if (TREE_CODE (lhs) == MODIFY_EXPR
24569 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24571 /* Undo effects of boolean_increment. */
24572 if (integer_onep (TREE_OPERAND (lhs, 1)))
24574 /* This is pre or post increment. */
24575 rhs = TREE_OPERAND (lhs, 1);
24576 lhs = TREE_OPERAND (lhs, 0);
24578 if (code == OMP_ATOMIC_CAPTURE_NEW
24579 && !structured_block
24580 && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
24581 code = OMP_ATOMIC_CAPTURE_OLD;
24587 switch (cp_lexer_peek_token (parser->lexer)->type)
24590 opcode = MULT_EXPR;
24593 opcode = TRUNC_DIV_EXPR;
24596 opcode = PLUS_EXPR;
24599 opcode = MINUS_EXPR;
24601 case CPP_LSHIFT_EQ:
24602 opcode = LSHIFT_EXPR;
24604 case CPP_RSHIFT_EQ:
24605 opcode = RSHIFT_EXPR;
24608 opcode = BIT_AND_EXPR;
24611 opcode = BIT_IOR_EXPR;
24614 opcode = BIT_XOR_EXPR;
24617 if (structured_block || code == OMP_ATOMIC)
24619 enum cp_parser_prec oprec;
24621 cp_lexer_consume_token (parser->lexer);
24622 rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24623 /*cast_p=*/false, NULL);
24624 if (rhs1 == error_mark_node)
24626 token = cp_lexer_peek_token (parser->lexer);
24627 switch (token->type)
24629 case CPP_SEMICOLON:
24630 if (code == OMP_ATOMIC_CAPTURE_NEW)
24632 code = OMP_ATOMIC_CAPTURE_OLD;
24637 cp_lexer_consume_token (parser->lexer);
24640 cp_parser_error (parser,
24641 "invalid form of %<#pragma omp atomic%>");
24644 opcode = MULT_EXPR;
24647 opcode = TRUNC_DIV_EXPR;
24650 opcode = PLUS_EXPR;
24653 opcode = MINUS_EXPR;
24656 opcode = LSHIFT_EXPR;
24659 opcode = RSHIFT_EXPR;
24662 opcode = BIT_AND_EXPR;
24665 opcode = BIT_IOR_EXPR;
24668 opcode = BIT_XOR_EXPR;
24671 cp_parser_error (parser,
24672 "invalid operator for %<#pragma omp atomic%>");
24675 oprec = TOKEN_PRECEDENCE (token);
24676 gcc_assert (oprec != PREC_NOT_OPERATOR);
24677 if (commutative_tree_code (opcode))
24678 oprec = (enum cp_parser_prec) (oprec - 1);
24679 cp_lexer_consume_token (parser->lexer);
24680 rhs = cp_parser_binary_expression (parser, false, false,
24682 if (rhs == error_mark_node)
24688 cp_parser_error (parser,
24689 "invalid operator for %<#pragma omp atomic%>");
24692 cp_lexer_consume_token (parser->lexer);
24694 rhs = cp_parser_expression (parser, false, NULL);
24695 if (rhs == error_mark_node)
24700 if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
24702 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24704 v = cp_parser_unary_expression (parser, /*address_p=*/false,
24705 /*cast_p=*/false, NULL);
24706 if (v == error_mark_node)
24708 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24710 lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24711 /*cast_p=*/false, NULL);
24712 if (lhs1 == error_mark_node)
24715 if (structured_block)
24717 cp_parser_consume_semicolon_at_end_of_statement (parser);
24718 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24721 finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
24722 if (!structured_block)
24723 cp_parser_consume_semicolon_at_end_of_statement (parser);
24727 cp_parser_skip_to_end_of_block_or_statement (parser);
24728 if (structured_block)
24730 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24731 cp_lexer_consume_token (parser->lexer);
24732 else if (code == OMP_ATOMIC_CAPTURE_NEW)
24734 cp_parser_skip_to_end_of_block_or_statement (parser);
24735 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24736 cp_lexer_consume_token (parser->lexer);
24743 # pragma omp barrier new-line */
24746 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24748 cp_parser_require_pragma_eol (parser, pragma_tok);
24749 finish_omp_barrier ();
24753 # pragma omp critical [(name)] new-line
24754 structured-block */
24757 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24759 tree stmt, name = NULL;
24761 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24763 cp_lexer_consume_token (parser->lexer);
24765 name = cp_parser_identifier (parser);
24767 if (name == error_mark_node
24768 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24769 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24770 /*or_comma=*/false,
24771 /*consume_paren=*/true);
24772 if (name == error_mark_node)
24775 cp_parser_require_pragma_eol (parser, pragma_tok);
24777 stmt = cp_parser_omp_structured_block (parser);
24778 return c_finish_omp_critical (input_location, stmt, name);
24782 # pragma omp flush flush-vars[opt] new-line
24785 ( variable-list ) */
24788 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24790 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24791 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24792 cp_parser_require_pragma_eol (parser, pragma_tok);
24794 finish_omp_flush ();
24797 /* Helper function, to parse omp for increment expression. */
24800 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24802 tree cond = cp_parser_binary_expression (parser, false, true,
24803 PREC_NOT_OPERATOR, NULL);
24804 if (cond == error_mark_node
24805 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24807 cp_parser_skip_to_end_of_statement (parser);
24808 return error_mark_node;
24811 switch (TREE_CODE (cond))
24819 return error_mark_node;
24822 /* If decl is an iterator, preserve LHS and RHS of the relational
24823 expr until finish_omp_for. */
24825 && (type_dependent_expression_p (decl)
24826 || CLASS_TYPE_P (TREE_TYPE (decl))))
24829 return build_x_binary_op (TREE_CODE (cond),
24830 TREE_OPERAND (cond, 0), ERROR_MARK,
24831 TREE_OPERAND (cond, 1), ERROR_MARK,
24832 /*overload=*/NULL, tf_warning_or_error);
24835 /* Helper function, to parse omp for increment expression. */
24838 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24840 cp_token *token = cp_lexer_peek_token (parser->lexer);
24846 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24848 op = (token->type == CPP_PLUS_PLUS
24849 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24850 cp_lexer_consume_token (parser->lexer);
24851 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24853 return error_mark_node;
24854 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24857 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24859 return error_mark_node;
24861 token = cp_lexer_peek_token (parser->lexer);
24862 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24864 op = (token->type == CPP_PLUS_PLUS
24865 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24866 cp_lexer_consume_token (parser->lexer);
24867 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24870 op = cp_parser_assignment_operator_opt (parser);
24871 if (op == ERROR_MARK)
24872 return error_mark_node;
24874 if (op != NOP_EXPR)
24876 rhs = cp_parser_assignment_expression (parser, false, NULL);
24877 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24878 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24881 lhs = cp_parser_binary_expression (parser, false, false,
24882 PREC_ADDITIVE_EXPRESSION, NULL);
24883 token = cp_lexer_peek_token (parser->lexer);
24884 decl_first = lhs == decl;
24887 if (token->type != CPP_PLUS
24888 && token->type != CPP_MINUS)
24889 return error_mark_node;
24893 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24894 cp_lexer_consume_token (parser->lexer);
24895 rhs = cp_parser_binary_expression (parser, false, false,
24896 PREC_ADDITIVE_EXPRESSION, NULL);
24897 token = cp_lexer_peek_token (parser->lexer);
24898 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24900 if (lhs == NULL_TREE)
24902 if (op == PLUS_EXPR)
24905 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24908 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24909 NULL, tf_warning_or_error);
24912 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24916 if (rhs != decl || op == MINUS_EXPR)
24917 return error_mark_node;
24918 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24921 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24923 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24926 /* Parse the restricted form of the for statement allowed by OpenMP. */
24929 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24931 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24932 tree real_decl, initv, condv, incrv, declv;
24933 tree this_pre_body, cl;
24934 location_t loc_first;
24935 bool collapse_err = false;
24936 int i, collapse = 1, nbraces = 0;
24937 VEC(tree,gc) *for_block = make_tree_vector ();
24939 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24940 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24941 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24943 gcc_assert (collapse >= 1);
24945 declv = make_tree_vec (collapse);
24946 initv = make_tree_vec (collapse);
24947 condv = make_tree_vec (collapse);
24948 incrv = make_tree_vec (collapse);
24950 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24952 for (i = 0; i < collapse; i++)
24954 int bracecount = 0;
24955 bool add_private_clause = false;
24958 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24960 cp_parser_error (parser, "for statement expected");
24963 loc = cp_lexer_consume_token (parser->lexer)->location;
24965 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24968 init = decl = real_decl = NULL;
24969 this_pre_body = push_stmt_list ();
24970 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24972 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24976 integer-type var = lb
24977 random-access-iterator-type var = lb
24978 pointer-type var = lb
24980 cp_decl_specifier_seq type_specifiers;
24982 /* First, try to parse as an initialized declaration. See
24983 cp_parser_condition, from whence the bulk of this is copied. */
24985 cp_parser_parse_tentatively (parser);
24986 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24987 /*is_trailing_return=*/false,
24989 if (cp_parser_parse_definitely (parser))
24991 /* If parsing a type specifier seq succeeded, then this
24992 MUST be a initialized declaration. */
24993 tree asm_specification, attributes;
24994 cp_declarator *declarator;
24996 declarator = cp_parser_declarator (parser,
24997 CP_PARSER_DECLARATOR_NAMED,
24998 /*ctor_dtor_or_conv_p=*/NULL,
24999 /*parenthesized_p=*/NULL,
25000 /*member_p=*/false);
25001 attributes = cp_parser_attributes_opt (parser);
25002 asm_specification = cp_parser_asm_specification_opt (parser);
25004 if (declarator == cp_error_declarator)
25005 cp_parser_skip_to_end_of_statement (parser);
25009 tree pushed_scope, auto_node;
25011 decl = start_decl (declarator, &type_specifiers,
25012 SD_INITIALIZED, attributes,
25013 /*prefix_attributes=*/NULL_TREE,
25016 auto_node = type_uses_auto (TREE_TYPE (decl));
25017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25019 if (cp_lexer_next_token_is (parser->lexer,
25021 error ("parenthesized initialization is not allowed in "
25022 "OpenMP %<for%> loop");
25024 /* Trigger an error. */
25025 cp_parser_require (parser, CPP_EQ, RT_EQ);
25027 init = error_mark_node;
25028 cp_parser_skip_to_end_of_statement (parser);
25030 else if (CLASS_TYPE_P (TREE_TYPE (decl))
25031 || type_dependent_expression_p (decl)
25034 bool is_direct_init, is_non_constant_init;
25036 init = cp_parser_initializer (parser,
25038 &is_non_constant_init);
25043 = do_auto_deduction (TREE_TYPE (decl), init,
25046 if (!CLASS_TYPE_P (TREE_TYPE (decl))
25047 && !type_dependent_expression_p (decl))
25051 cp_finish_decl (decl, init, !is_non_constant_init,
25053 LOOKUP_ONLYCONVERTING);
25054 if (CLASS_TYPE_P (TREE_TYPE (decl)))
25056 VEC_safe_push (tree, gc, for_block, this_pre_body);
25060 init = pop_stmt_list (this_pre_body);
25061 this_pre_body = NULL_TREE;
25066 cp_lexer_consume_token (parser->lexer);
25067 init = cp_parser_assignment_expression (parser, false, NULL);
25070 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25071 init = error_mark_node;
25073 cp_finish_decl (decl, NULL_TREE,
25074 /*init_const_expr_p=*/false,
25076 LOOKUP_ONLYCONVERTING);
25080 pop_scope (pushed_scope);
25086 /* If parsing a type specifier sequence failed, then
25087 this MUST be a simple expression. */
25088 cp_parser_parse_tentatively (parser);
25089 decl = cp_parser_primary_expression (parser, false, false,
25091 if (!cp_parser_error_occurred (parser)
25094 && CLASS_TYPE_P (TREE_TYPE (decl)))
25098 cp_parser_parse_definitely (parser);
25099 cp_parser_require (parser, CPP_EQ, RT_EQ);
25100 rhs = cp_parser_assignment_expression (parser, false, NULL);
25101 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25103 tf_warning_or_error));
25104 add_private_clause = true;
25109 cp_parser_abort_tentative_parse (parser);
25110 init = cp_parser_expression (parser, false, NULL);
25113 if (TREE_CODE (init) == MODIFY_EXPR
25114 || TREE_CODE (init) == MODOP_EXPR)
25115 real_decl = TREE_OPERAND (init, 0);
25120 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25123 this_pre_body = pop_stmt_list (this_pre_body);
25127 pre_body = push_stmt_list ();
25129 add_stmt (this_pre_body);
25130 pre_body = pop_stmt_list (pre_body);
25133 pre_body = this_pre_body;
25138 if (par_clauses != NULL && real_decl != NULL_TREE)
25141 for (c = par_clauses; *c ; )
25142 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25143 && OMP_CLAUSE_DECL (*c) == real_decl)
25145 error_at (loc, "iteration variable %qD"
25146 " should not be firstprivate", real_decl);
25147 *c = OMP_CLAUSE_CHAIN (*c);
25149 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25150 && OMP_CLAUSE_DECL (*c) == real_decl)
25152 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25153 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
25154 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25155 OMP_CLAUSE_DECL (l) = real_decl;
25156 OMP_CLAUSE_CHAIN (l) = clauses;
25157 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25159 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25160 CP_OMP_CLAUSE_INFO (*c) = NULL;
25161 add_private_clause = false;
25165 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25166 && OMP_CLAUSE_DECL (*c) == real_decl)
25167 add_private_clause = false;
25168 c = &OMP_CLAUSE_CHAIN (*c);
25172 if (add_private_clause)
25175 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25177 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25178 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25179 && OMP_CLAUSE_DECL (c) == decl)
25181 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25182 && OMP_CLAUSE_DECL (c) == decl)
25183 error_at (loc, "iteration variable %qD "
25184 "should not be firstprivate",
25186 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25187 && OMP_CLAUSE_DECL (c) == decl)
25188 error_at (loc, "iteration variable %qD should not be reduction",
25193 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25194 OMP_CLAUSE_DECL (c) = decl;
25195 c = finish_omp_clauses (c);
25198 OMP_CLAUSE_CHAIN (c) = clauses;
25205 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25206 cond = cp_parser_omp_for_cond (parser, decl);
25207 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25210 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25212 /* If decl is an iterator, preserve the operator on decl
25213 until finish_omp_for. */
25215 && ((type_dependent_expression_p (decl)
25216 && !POINTER_TYPE_P (TREE_TYPE (decl)))
25217 || CLASS_TYPE_P (TREE_TYPE (decl))))
25218 incr = cp_parser_omp_for_incr (parser, decl);
25220 incr = cp_parser_expression (parser, false, NULL);
25223 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25224 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25225 /*or_comma=*/false,
25226 /*consume_paren=*/true);
25228 TREE_VEC_ELT (declv, i) = decl;
25229 TREE_VEC_ELT (initv, i) = init;
25230 TREE_VEC_ELT (condv, i) = cond;
25231 TREE_VEC_ELT (incrv, i) = incr;
25233 if (i == collapse - 1)
25236 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
25237 in between the collapsed for loops to be still considered perfectly
25238 nested. Hopefully the final version clarifies this.
25239 For now handle (multiple) {'s and empty statements. */
25240 cp_parser_parse_tentatively (parser);
25243 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25245 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25247 cp_lexer_consume_token (parser->lexer);
25250 else if (bracecount
25251 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25252 cp_lexer_consume_token (parser->lexer);
25255 loc = cp_lexer_peek_token (parser->lexer)->location;
25256 error_at (loc, "not enough collapsed for loops");
25257 collapse_err = true;
25258 cp_parser_abort_tentative_parse (parser);
25267 cp_parser_parse_definitely (parser);
25268 nbraces += bracecount;
25272 /* Note that we saved the original contents of this flag when we entered
25273 the structured block, and so we don't need to re-save it here. */
25274 parser->in_statement = IN_OMP_FOR;
25276 /* Note that the grammar doesn't call for a structured block here,
25277 though the loop as a whole is a structured block. */
25278 body = push_stmt_list ();
25279 cp_parser_statement (parser, NULL_TREE, false, NULL);
25280 body = pop_stmt_list (body);
25282 if (declv == NULL_TREE)
25285 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
25286 pre_body, clauses);
25290 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25292 cp_lexer_consume_token (parser->lexer);
25295 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25296 cp_lexer_consume_token (parser->lexer);
25301 error_at (cp_lexer_peek_token (parser->lexer)->location,
25302 "collapsed loops not perfectly nested");
25304 collapse_err = true;
25305 cp_parser_statement_seq_opt (parser, NULL);
25306 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25311 while (!VEC_empty (tree, for_block))
25312 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
25313 release_tree_vector (for_block);
25319 #pragma omp for for-clause[optseq] new-line
25322 #define OMP_FOR_CLAUSE_MASK \
25323 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25324 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25325 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
25326 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
25327 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
25328 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
25329 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
25330 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
25333 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
25335 tree clauses, sb, ret;
25338 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
25339 "#pragma omp for", pragma_tok);
25341 sb = begin_omp_structured_block ();
25342 save = cp_parser_begin_omp_structured_block (parser);
25344 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
25346 cp_parser_end_omp_structured_block (parser, save);
25347 add_stmt (finish_omp_structured_block (sb));
25353 # pragma omp master new-line
25354 structured-block */
25357 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
25359 cp_parser_require_pragma_eol (parser, pragma_tok);
25360 return c_finish_omp_master (input_location,
25361 cp_parser_omp_structured_block (parser));
25365 # pragma omp ordered new-line
25366 structured-block */
25369 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
25371 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25372 cp_parser_require_pragma_eol (parser, pragma_tok);
25373 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
25379 { section-sequence }
25382 section-directive[opt] structured-block
25383 section-sequence section-directive structured-block */
25386 cp_parser_omp_sections_scope (cp_parser *parser)
25388 tree stmt, substmt;
25389 bool error_suppress = false;
25392 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25395 stmt = push_stmt_list ();
25397 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
25401 substmt = begin_omp_structured_block ();
25402 save = cp_parser_begin_omp_structured_block (parser);
25406 cp_parser_statement (parser, NULL_TREE, false, NULL);
25408 tok = cp_lexer_peek_token (parser->lexer);
25409 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25411 if (tok->type == CPP_CLOSE_BRACE)
25413 if (tok->type == CPP_EOF)
25417 cp_parser_end_omp_structured_block (parser, save);
25418 substmt = finish_omp_structured_block (substmt);
25419 substmt = build1 (OMP_SECTION, void_type_node, substmt);
25420 add_stmt (substmt);
25425 tok = cp_lexer_peek_token (parser->lexer);
25426 if (tok->type == CPP_CLOSE_BRACE)
25428 if (tok->type == CPP_EOF)
25431 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25433 cp_lexer_consume_token (parser->lexer);
25434 cp_parser_require_pragma_eol (parser, tok);
25435 error_suppress = false;
25437 else if (!error_suppress)
25439 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
25440 error_suppress = true;
25443 substmt = cp_parser_omp_structured_block (parser);
25444 substmt = build1 (OMP_SECTION, void_type_node, substmt);
25445 add_stmt (substmt);
25447 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25449 substmt = pop_stmt_list (stmt);
25451 stmt = make_node (OMP_SECTIONS);
25452 TREE_TYPE (stmt) = void_type_node;
25453 OMP_SECTIONS_BODY (stmt) = substmt;
25460 # pragma omp sections sections-clause[optseq] newline
25463 #define OMP_SECTIONS_CLAUSE_MASK \
25464 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25465 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25466 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
25467 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
25468 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25471 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
25475 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
25476 "#pragma omp sections", pragma_tok);
25478 ret = cp_parser_omp_sections_scope (parser);
25480 OMP_SECTIONS_CLAUSES (ret) = clauses;
25486 # pragma parallel parallel-clause new-line
25487 # pragma parallel for parallel-for-clause new-line
25488 # pragma parallel sections parallel-sections-clause new-line */
25490 #define OMP_PARALLEL_CLAUSE_MASK \
25491 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
25492 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25493 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25494 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
25495 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
25496 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
25497 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
25498 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
25501 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
25503 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
25504 const char *p_name = "#pragma omp parallel";
25505 tree stmt, clauses, par_clause, ws_clause, block;
25506 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
25508 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25510 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25512 cp_lexer_consume_token (parser->lexer);
25513 p_kind = PRAGMA_OMP_PARALLEL_FOR;
25514 p_name = "#pragma omp parallel for";
25515 mask |= OMP_FOR_CLAUSE_MASK;
25516 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25518 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25520 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25521 const char *p = IDENTIFIER_POINTER (id);
25522 if (strcmp (p, "sections") == 0)
25524 cp_lexer_consume_token (parser->lexer);
25525 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25526 p_name = "#pragma omp parallel sections";
25527 mask |= OMP_SECTIONS_CLAUSE_MASK;
25528 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25532 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25533 block = begin_omp_parallel ();
25534 save = cp_parser_begin_omp_structured_block (parser);
25538 case PRAGMA_OMP_PARALLEL:
25539 cp_parser_statement (parser, NULL_TREE, false, NULL);
25540 par_clause = clauses;
25543 case PRAGMA_OMP_PARALLEL_FOR:
25544 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25545 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25548 case PRAGMA_OMP_PARALLEL_SECTIONS:
25549 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25550 stmt = cp_parser_omp_sections_scope (parser);
25552 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25556 gcc_unreachable ();
25559 cp_parser_end_omp_structured_block (parser, save);
25560 stmt = finish_omp_parallel (par_clause, block);
25561 if (p_kind != PRAGMA_OMP_PARALLEL)
25562 OMP_PARALLEL_COMBINED (stmt) = 1;
25567 # pragma omp single single-clause[optseq] new-line
25568 structured-block */
25570 #define OMP_SINGLE_CLAUSE_MASK \
25571 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25572 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25573 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
25574 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25577 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25579 tree stmt = make_node (OMP_SINGLE);
25580 TREE_TYPE (stmt) = void_type_node;
25582 OMP_SINGLE_CLAUSES (stmt)
25583 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25584 "#pragma omp single", pragma_tok);
25585 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25587 return add_stmt (stmt);
25591 # pragma omp task task-clause[optseq] new-line
25592 structured-block */
25594 #define OMP_TASK_CLAUSE_MASK \
25595 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
25596 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
25597 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
25598 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
25599 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
25600 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
25601 | (1u << PRAGMA_OMP_CLAUSE_FINAL) \
25602 | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
25605 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25607 tree clauses, block;
25610 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25611 "#pragma omp task", pragma_tok);
25612 block = begin_omp_task ();
25613 save = cp_parser_begin_omp_structured_block (parser);
25614 cp_parser_statement (parser, NULL_TREE, false, NULL);
25615 cp_parser_end_omp_structured_block (parser, save);
25616 return finish_omp_task (clauses, block);
25620 # pragma omp taskwait new-line */
25623 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25625 cp_parser_require_pragma_eol (parser, pragma_tok);
25626 finish_omp_taskwait ();
25630 # pragma omp taskyield new-line */
25633 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
25635 cp_parser_require_pragma_eol (parser, pragma_tok);
25636 finish_omp_taskyield ();
25640 # pragma omp threadprivate (variable-list) */
25643 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25647 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25648 cp_parser_require_pragma_eol (parser, pragma_tok);
25650 finish_omp_threadprivate (vars);
25653 /* Main entry point to OpenMP statement pragmas. */
25656 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25660 switch (pragma_tok->pragma_kind)
25662 case PRAGMA_OMP_ATOMIC:
25663 cp_parser_omp_atomic (parser, pragma_tok);
25665 case PRAGMA_OMP_CRITICAL:
25666 stmt = cp_parser_omp_critical (parser, pragma_tok);
25668 case PRAGMA_OMP_FOR:
25669 stmt = cp_parser_omp_for (parser, pragma_tok);
25671 case PRAGMA_OMP_MASTER:
25672 stmt = cp_parser_omp_master (parser, pragma_tok);
25674 case PRAGMA_OMP_ORDERED:
25675 stmt = cp_parser_omp_ordered (parser, pragma_tok);
25677 case PRAGMA_OMP_PARALLEL:
25678 stmt = cp_parser_omp_parallel (parser, pragma_tok);
25680 case PRAGMA_OMP_SECTIONS:
25681 stmt = cp_parser_omp_sections (parser, pragma_tok);
25683 case PRAGMA_OMP_SINGLE:
25684 stmt = cp_parser_omp_single (parser, pragma_tok);
25686 case PRAGMA_OMP_TASK:
25687 stmt = cp_parser_omp_task (parser, pragma_tok);
25690 gcc_unreachable ();
25694 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25699 static GTY (()) cp_parser *the_parser;
25702 /* Special handling for the first token or line in the file. The first
25703 thing in the file might be #pragma GCC pch_preprocess, which loads a
25704 PCH file, which is a GC collection point. So we need to handle this
25705 first pragma without benefit of an existing lexer structure.
25707 Always returns one token to the caller in *FIRST_TOKEN. This is
25708 either the true first token of the file, or the first token after
25709 the initial pragma. */
25712 cp_parser_initial_pragma (cp_token *first_token)
25716 cp_lexer_get_preprocessor_token (NULL, first_token);
25717 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25720 cp_lexer_get_preprocessor_token (NULL, first_token);
25721 if (first_token->type == CPP_STRING)
25723 name = first_token->u.value;
25725 cp_lexer_get_preprocessor_token (NULL, first_token);
25726 if (first_token->type != CPP_PRAGMA_EOL)
25727 error_at (first_token->location,
25728 "junk at end of %<#pragma GCC pch_preprocess%>");
25731 error_at (first_token->location, "expected string literal");
25733 /* Skip to the end of the pragma. */
25734 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25735 cp_lexer_get_preprocessor_token (NULL, first_token);
25737 /* Now actually load the PCH file. */
25739 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25741 /* Read one more token to return to our caller. We have to do this
25742 after reading the PCH file in, since its pointers have to be
25744 cp_lexer_get_preprocessor_token (NULL, first_token);
25747 /* Normal parsing of a pragma token. Here we can (and must) use the
25751 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25753 cp_token *pragma_tok;
25756 pragma_tok = cp_lexer_consume_token (parser->lexer);
25757 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25758 parser->lexer->in_pragma = true;
25760 id = pragma_tok->pragma_kind;
25763 case PRAGMA_GCC_PCH_PREPROCESS:
25764 error_at (pragma_tok->location,
25765 "%<#pragma GCC pch_preprocess%> must be first");
25768 case PRAGMA_OMP_BARRIER:
25771 case pragma_compound:
25772 cp_parser_omp_barrier (parser, pragma_tok);
25775 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25776 "used in compound statements");
25783 case PRAGMA_OMP_FLUSH:
25786 case pragma_compound:
25787 cp_parser_omp_flush (parser, pragma_tok);
25790 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25791 "used in compound statements");
25798 case PRAGMA_OMP_TASKWAIT:
25801 case pragma_compound:
25802 cp_parser_omp_taskwait (parser, pragma_tok);
25805 error_at (pragma_tok->location,
25806 "%<#pragma omp taskwait%> may only be "
25807 "used in compound statements");
25814 case PRAGMA_OMP_TASKYIELD:
25817 case pragma_compound:
25818 cp_parser_omp_taskyield (parser, pragma_tok);
25821 error_at (pragma_tok->location,
25822 "%<#pragma omp taskyield%> may only be "
25823 "used in compound statements");
25830 case PRAGMA_OMP_THREADPRIVATE:
25831 cp_parser_omp_threadprivate (parser, pragma_tok);
25834 case PRAGMA_OMP_ATOMIC:
25835 case PRAGMA_OMP_CRITICAL:
25836 case PRAGMA_OMP_FOR:
25837 case PRAGMA_OMP_MASTER:
25838 case PRAGMA_OMP_ORDERED:
25839 case PRAGMA_OMP_PARALLEL:
25840 case PRAGMA_OMP_SECTIONS:
25841 case PRAGMA_OMP_SINGLE:
25842 case PRAGMA_OMP_TASK:
25843 if (context == pragma_external)
25845 cp_parser_omp_construct (parser, pragma_tok);
25848 case PRAGMA_OMP_SECTION:
25849 error_at (pragma_tok->location,
25850 "%<#pragma omp section%> may only be used in "
25851 "%<#pragma omp sections%> construct");
25855 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25856 c_invoke_pragma_handler (id);
25860 cp_parser_error (parser, "expected declaration specifiers");
25864 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25868 /* The interface the pragma parsers have to the lexer. */
25871 pragma_lex (tree *value)
25874 enum cpp_ttype ret;
25876 tok = cp_lexer_peek_token (the_parser->lexer);
25879 *value = tok->u.value;
25881 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25883 else if (ret == CPP_STRING)
25884 *value = cp_parser_string_literal (the_parser, false, false);
25887 cp_lexer_consume_token (the_parser->lexer);
25888 if (ret == CPP_KEYWORD)
25896 /* External interface. */
25898 /* Parse one entire translation unit. */
25901 c_parse_file (void)
25903 static bool already_called = false;
25905 if (already_called)
25907 sorry ("inter-module optimizations not implemented for C++");
25910 already_called = true;
25912 the_parser = cp_parser_new ();
25913 push_deferring_access_checks (flag_access_control
25914 ? dk_no_deferred : dk_no_check);
25915 cp_parser_translation_unit (the_parser);
25919 #include "gt-cp-parser.h"