2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
4 Written by Mark Mitchell <mark@codesourcery.com>.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
31 #include "c-family/c-pragma.h"
34 #include "diagnostic-core.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
41 #include "tree-pretty-print.h"
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48 and c-lex.c) and the C++ parser. */
50 static cp_token eof_token =
52 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
58 /* floating-point literal */
62 /* %<__FUNCTION__%> */
64 /* %<__PRETTY_FUNCTION__%> */
72 /* %<typeid%> operator */
74 /* non-constant compound literals */
82 /* an array reference */
88 /* the address of a label */
102 /* calls to overloaded operators */
106 /* a comma operator */
108 /* a call to a constructor */
110 } non_integral_constant;
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
118 /* is not a class or namespace */
120 /* is not a class, namespace, or enumeration */
124 /* The various kinds of required token */
125 typedef enum required_token {
127 RT_SEMICOLON, /* ';' */
128 RT_OPEN_PAREN, /* '(' */
129 RT_CLOSE_BRACE, /* '}' */
130 RT_OPEN_BRACE, /* '{' */
131 RT_CLOSE_SQUARE, /* ']' */
132 RT_OPEN_SQUARE, /* '[' */
136 RT_GREATER, /* '>' */
138 RT_ELLIPSIS, /* '...' */
142 RT_COLON_SCOPE, /* ':' or '::' */
143 RT_CLOSE_PAREN, /* ')' */
144 RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145 RT_PRAGMA_EOL, /* end of line */
146 RT_NAME, /* identifier */
148 /* The type is CPP_KEYWORD */
150 RT_DELETE, /* delete */
151 RT_RETURN, /* return */
152 RT_WHILE, /* while */
153 RT_EXTERN, /* extern */
154 RT_STATIC_ASSERT, /* static_assert */
155 RT_DECLTYPE, /* decltype */
156 RT_OPERATOR, /* operator */
157 RT_CLASS, /* class */
158 RT_TEMPLATE, /* template */
159 RT_NAMESPACE, /* namespace */
160 RT_USING, /* using */
163 RT_CATCH, /* catch */
164 RT_THROW, /* throw */
165 RT_LABEL, /* __label__ */
166 RT_AT_TRY, /* @try */
167 RT_AT_SYNCHRONIZED, /* @synchronized */
168 RT_AT_THROW, /* @throw */
170 RT_SELECT, /* selection-statement */
171 RT_INTERATION, /* iteration-statement */
172 RT_JUMP, /* jump-statement */
173 RT_CLASS_KEY, /* class-key */
174 RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
179 static cp_lexer *cp_lexer_new_main
181 static cp_lexer *cp_lexer_new_from_tokens
182 (cp_token_cache *tokens);
183 static void cp_lexer_destroy
185 static int cp_lexer_saving_tokens
187 static cp_token *cp_lexer_token_at
188 (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190 (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
193 static cp_token *cp_lexer_peek_nth_token
194 (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196 (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198 (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200 (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
203 static void cp_lexer_purge_token
205 static void cp_lexer_purge_tokens_after
206 (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
209 static void cp_lexer_commit_tokens
211 static void cp_lexer_rollback_tokens
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215 (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
218 static void cp_lexer_start_debugging
219 (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221 (cp_lexer *) ATTRIBUTE_UNUSED;
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224 about passing NULL to functions that require non-NULL arguments
225 (fputs, fprintf). It will never be used, so all we need is a value
226 of the right type that's guaranteed not to be NULL. */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
232 static cp_token_cache *cp_token_cache_new
233 (cp_token *, cp_token *);
235 static void cp_parser_initial_pragma
238 /* Manifest constants. */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written. */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250 sizeof, typeof, or alignof. */
251 int cp_unevaluated_operand;
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE. If NUM is 0, dump all the
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
263 fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
266 num = VEC_length (cp_token, buffer);
268 for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
270 cp_lexer_print_token (file, token);
275 case CPP_CLOSE_BRACE:
285 if (i == num && i < VEC_length (cp_token, buffer))
287 fprintf (file, " ... ");
288 cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289 VEC_length (cp_token, buffer) - 1));
292 fprintf (file, "\n");
296 /* Dump all tokens in BUFFER to stderr. */
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
301 cp_lexer_dump_tokens (stderr, buffer, 0);
306 /* Allocate memory for a new lexer object and return it. */
309 cp_lexer_alloc (void)
313 c_common_no_more_pch ();
315 /* Allocate the memory. */
316 lexer = ggc_alloc_cleared_cp_lexer ();
318 #ifdef ENABLE_CHECKING
319 /* Initially we are not debugging. */
320 lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323 CP_SAVED_TOKEN_STACK);
325 /* Create the buffer. */
326 lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
336 cp_lexer_new_main (void)
341 /* It's possible that parsing the first pragma will load a PCH file,
342 which is a GC collection point. So we have to do that before
343 allocating any memory. */
344 cp_parser_initial_pragma (&token);
346 lexer = cp_lexer_alloc ();
348 /* Put the first token in the buffer. */
349 VEC_quick_push (cp_token, lexer->buffer, &token);
351 /* Get the remaining tokens from the preprocessor. */
352 while (token.type != CPP_EOF)
354 cp_lexer_get_preprocessor_token (lexer, &token);
355 VEC_safe_push (cp_token, gc, lexer->buffer, &token);
358 lexer->last_token = VEC_address (cp_token, lexer->buffer)
359 + VEC_length (cp_token, lexer->buffer)
361 lexer->next_token = VEC_length (cp_token, lexer->buffer)
362 ? VEC_address (cp_token, lexer->buffer)
365 /* Subsequent preprocessor diagnostics should use compiler
366 diagnostic functions to get the compiler source location. */
369 gcc_assert (!lexer->next_token->purged_p);
373 /* Create a new lexer whose token stream is primed with the tokens in
374 CACHE. When these tokens are exhausted, no new tokens will be read. */
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
379 cp_token *first = cache->first;
380 cp_token *last = cache->last;
381 cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
383 /* We do not own the buffer. */
384 lexer->buffer = NULL;
385 lexer->next_token = first == last ? &eof_token : first;
386 lexer->last_token = last;
388 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389 CP_SAVED_TOKEN_STACK);
391 #ifdef ENABLE_CHECKING
392 /* Initially we are not debugging. */
393 lexer->debugging_p = false;
396 gcc_assert (!lexer->next_token->purged_p);
400 /* Frees all resources associated with LEXER. */
403 cp_lexer_destroy (cp_lexer *lexer)
405 VEC_free (cp_token, gc, lexer->buffer);
406 VEC_free (cp_token_position, heap, lexer->saved_tokens);
410 /* Returns nonzero if debugging information should be output. */
412 #ifdef ENABLE_CHECKING
415 cp_lexer_debugging_p (cp_lexer *lexer)
417 return lexer->debugging_p;
420 #endif /* ENABLE_CHECKING */
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
425 gcc_assert (!previous_p || lexer->next_token != &eof_token);
427 return lexer->next_token - previous_p;
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
439 lexer->next_token = cp_lexer_token_at (lexer, pos);
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
445 if (lexer->next_token == &eof_token)
446 return lexer->last_token - 1;
448 return cp_lexer_token_position (lexer, true);
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
454 cp_token_position tp = cp_lexer_previous_token_position (lexer);
456 return cp_lexer_token_at (lexer, tp);
459 /* nonzero if we are presently saving tokens. */
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
464 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
467 /* Store the next token from the preprocessor in *TOKEN. Return true
468 if we reach EOF. If LEXER is NULL, assume we are handling an
469 initial #pragma pch_preprocess, and thus want the lexer to return
470 processed strings. */
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
475 static int is_extern_c = 0;
477 /* Get a new token from the preprocessor. */
479 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480 lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481 token->keyword = RID_MAX;
482 token->pragma_kind = PRAGMA_NONE;
483 token->purged_p = false;
485 /* On some systems, some header files are surrounded by an
486 implicit extern "C" block. Set a flag in the token if it
487 comes from such a header. */
488 is_extern_c += pending_lang_change;
489 pending_lang_change = 0;
490 token->implicit_extern_c = is_extern_c > 0;
492 /* Check to see if this token is a keyword. */
493 if (token->type == CPP_NAME)
495 if (C_IS_RESERVED_WORD (token->u.value))
497 /* Mark this token as a keyword. */
498 token->type = CPP_KEYWORD;
499 /* Record which keyword. */
500 token->keyword = C_RID_CODE (token->u.value);
504 if (warn_cxx0x_compat
505 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
508 /* Warn about the C++0x keyword (but still treat it as
510 warning (OPT_Wc__0x_compat,
511 "identifier %qE will become a keyword in C++0x",
514 /* Clear out the C_RID_CODE so we don't warn about this
515 particular identifier-turned-keyword again. */
516 C_SET_RID_CODE (token->u.value, RID_MAX);
519 token->ambiguous_p = false;
520 token->keyword = RID_MAX;
523 else if (token->type == CPP_AT_NAME)
525 /* This only happens in Objective-C++; it must be a keyword. */
526 token->type = CPP_KEYWORD;
527 switch (C_RID_CODE (token->u.value))
529 /* Replace 'class' with '@class', 'private' with '@private',
530 etc. This prevents confusion with the C++ keyword
531 'class', and makes the tokens consistent with other
532 Objective-C 'AT' keywords. For example '@class' is
533 reported as RID_AT_CLASS which is consistent with
534 '@synchronized', which is reported as
537 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
538 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
539 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
541 case RID_THROW: token->keyword = RID_AT_THROW; break;
542 case RID_TRY: token->keyword = RID_AT_TRY; break;
543 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
544 default: token->keyword = C_RID_CODE (token->u.value);
547 else if (token->type == CPP_PRAGMA)
549 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
550 token->pragma_kind = ((enum pragma_kind)
551 TREE_INT_CST_LOW (token->u.value));
552 token->u.value = NULL_TREE;
556 /* Update the globals input_location and the input file stack from TOKEN. */
558 cp_lexer_set_source_position_from_token (cp_token *token)
560 if (token->type != CPP_EOF)
562 input_location = token->location;
566 /* Return a pointer to the next token in the token stream, but do not
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
572 if (cp_lexer_debugging_p (lexer))
574 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576 putc ('\n', cp_lexer_debug_stream);
578 return lexer->next_token;
581 /* Return true if the next token has the indicated TYPE. */
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
586 return cp_lexer_peek_token (lexer)->type == type;
589 /* Return true if the next token does not have the indicated TYPE. */
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
594 return !cp_lexer_next_token_is (lexer, type);
597 /* Return true if the next token is the indicated KEYWORD. */
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
602 return cp_lexer_peek_token (lexer)->keyword == keyword;
605 /* Return true if the next token is not the indicated KEYWORD. */
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
610 return cp_lexer_peek_token (lexer)->keyword != keyword;
613 /* Return true if the next token is a keyword for a decl-specifier. */
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
620 token = cp_lexer_peek_token (lexer);
621 switch (token->keyword)
623 /* auto specifier: storage-class-specifier in C++,
624 simple-type-specifier in C++0x. */
626 /* Storage classes. */
632 /* Elaborated type specifiers. */
638 /* Simple type specifiers. */
653 /* GNU extensions. */
656 /* C++0x extensions. */
658 case RID_UNDERLYING_TYPE:
666 /* Return a pointer to the Nth token in the token stream. If N is 1,
667 then this is precisely equivalent to cp_lexer_peek_token (except
668 that it is not inline). One would like to disallow that case, but
669 there is one case (cp_parser_nth_token_starts_template_id) where
670 the caller passes a variable for N and it might be 1. */
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
677 /* N is 1-based, not zero-based. */
680 if (cp_lexer_debugging_p (lexer))
681 fprintf (cp_lexer_debug_stream,
682 "cp_lexer: peeking ahead %ld at token: ", (long)n);
685 token = lexer->next_token;
686 gcc_assert (!n || token != &eof_token);
690 if (token == lexer->last_token)
696 if (!token->purged_p)
700 if (cp_lexer_debugging_p (lexer))
702 cp_lexer_print_token (cp_lexer_debug_stream, token);
703 putc ('\n', cp_lexer_debug_stream);
709 /* Return the next token, and advance the lexer's next_token pointer
710 to point to the next non-purged token. */
713 cp_lexer_consume_token (cp_lexer* lexer)
715 cp_token *token = lexer->next_token;
717 gcc_assert (token != &eof_token);
718 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
723 if (lexer->next_token == lexer->last_token)
725 lexer->next_token = &eof_token;
730 while (lexer->next_token->purged_p);
732 cp_lexer_set_source_position_from_token (token);
734 /* Provide debugging output. */
735 if (cp_lexer_debugging_p (lexer))
737 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738 cp_lexer_print_token (cp_lexer_debug_stream, token);
739 putc ('\n', cp_lexer_debug_stream);
745 /* Permanently remove the next token from the token stream, and
746 advance the next_token pointer to refer to the next non-purged
750 cp_lexer_purge_token (cp_lexer *lexer)
752 cp_token *tok = lexer->next_token;
754 gcc_assert (tok != &eof_token);
755 tok->purged_p = true;
756 tok->location = UNKNOWN_LOCATION;
757 tok->u.value = NULL_TREE;
758 tok->keyword = RID_MAX;
763 if (tok == lexer->last_token)
769 while (tok->purged_p);
770 lexer->next_token = tok;
773 /* Permanently remove all tokens after TOK, up to, but not
774 including, the token that will be returned next by
775 cp_lexer_peek_token. */
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
780 cp_token *peek = lexer->next_token;
782 if (peek == &eof_token)
783 peek = lexer->last_token;
785 gcc_assert (tok < peek);
787 for ( tok += 1; tok != peek; tok += 1)
789 tok->purged_p = true;
790 tok->location = UNKNOWN_LOCATION;
791 tok->u.value = NULL_TREE;
792 tok->keyword = RID_MAX;
796 /* Begin saving tokens. All tokens consumed after this point will be
800 cp_lexer_save_tokens (cp_lexer* lexer)
802 /* Provide debugging output. */
803 if (cp_lexer_debugging_p (lexer))
804 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
806 VEC_safe_push (cp_token_position, heap,
807 lexer->saved_tokens, lexer->next_token);
810 /* Commit to the portion of the token stream most recently saved. */
813 cp_lexer_commit_tokens (cp_lexer* lexer)
815 /* Provide debugging output. */
816 if (cp_lexer_debugging_p (lexer))
817 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
819 VEC_pop (cp_token_position, lexer->saved_tokens);
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823 to the token stream. Stop saving tokens. */
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
828 /* Provide debugging output. */
829 if (cp_lexer_debugging_p (lexer))
830 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
832 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
835 /* Print a representation of the TOKEN on the STREAM. */
837 #ifdef ENABLE_CHECKING
840 cp_lexer_print_token (FILE * stream, cp_token *token)
842 /* We don't use cpp_type2name here because the parser defines
843 a few tokens of its own. */
844 static const char *const token_names[] = {
845 /* cpplib-defined token types */
851 /* C++ parser token types - see "Manifest constants", above. */
854 "NESTED_NAME_SPECIFIER",
857 /* For some tokens, print the associated data. */
861 /* Some keywords have a value that is not an IDENTIFIER_NODE.
862 For example, `struct' is mapped to an INTEGER_CST. */
863 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
865 /* else fall through */
867 fputs (IDENTIFIER_POINTER (token->u.value), stream);
875 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
879 print_generic_expr (stream, token->u.value, 0);
883 /* If we have a name for the token, print it out. Otherwise, we
884 simply give the numeric code. */
885 if (token->type < ARRAY_SIZE(token_names))
886 fputs (token_names[token->type], stream);
888 fprintf (stream, "[%d]", token->type);
893 /* Start emitting debugging information. */
896 cp_lexer_start_debugging (cp_lexer* lexer)
898 lexer->debugging_p = true;
901 /* Stop emitting debugging information. */
904 cp_lexer_stop_debugging (cp_lexer* lexer)
906 lexer->debugging_p = false;
909 #endif /* ENABLE_CHECKING */
911 /* Create a new cp_token_cache, representing a range of tokens. */
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
916 cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917 cache->first = first;
923 /* Decl-specifiers. */
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
930 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
935 /* Nothing other than the parser should be creating declarators;
936 declarators are a semi-syntactic representation of C++ entities.
937 Other parts of the front end that need to create entities (like
938 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
940 static cp_declarator *make_call_declarator
941 (cp_declarator *, tree, cp_cv_quals, tree, tree);
942 static cp_declarator *make_array_declarator
943 (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945 (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947 (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949 (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951 (cp_cv_quals, tree, cp_declarator *);
953 /* An erroneous declarator. */
954 static cp_declarator *cp_error_declarator;
956 /* The obstack on which declarators and related data structures are
958 static struct obstack declarator_obstack;
960 /* Alloc BYTES from the declarator memory pool. */
963 alloc_declarator (size_t bytes)
965 return obstack_alloc (&declarator_obstack, bytes);
968 /* Allocate a declarator of the indicated KIND. Clear fields that are
969 common to all declarators. */
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
974 cp_declarator *declarator;
976 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977 declarator->kind = kind;
978 declarator->attributes = NULL_TREE;
979 declarator->declarator = NULL;
980 declarator->parameter_pack_p = false;
981 declarator->id_loc = UNKNOWN_LOCATION;
986 /* Make a declarator for a generalized identifier. If
987 QUALIFYING_SCOPE is non-NULL, the identifier is
988 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989 UNQUALIFIED_NAME. SFK indicates the kind of special function this
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994 special_function_kind sfk)
996 cp_declarator *declarator;
998 /* It is valid to write:
1000 class C { void f(); };
1004 The standard is not clear about whether `typedef const C D' is
1005 legal; as of 2002-09-15 the committee is considering that
1006 question. EDG 3.0 allows that syntax. Therefore, we do as
1008 if (qualifying_scope && TYPE_P (qualifying_scope))
1009 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1011 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1015 declarator = make_declarator (cdk_id);
1016 declarator->u.id.qualifying_scope = qualifying_scope;
1017 declarator->u.id.unqualified_name = unqualified_name;
1018 declarator->u.id.sfk = sfk;
1023 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
1024 of modifiers such as const or volatile to apply to the pointer
1025 type, represented as identifiers. */
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1030 cp_declarator *declarator;
1032 declarator = make_declarator (cdk_pointer);
1033 declarator->declarator = target;
1034 declarator->u.pointer.qualifiers = cv_qualifiers;
1035 declarator->u.pointer.class_type = NULL_TREE;
1038 declarator->id_loc = target->id_loc;
1039 declarator->parameter_pack_p = target->parameter_pack_p;
1040 target->parameter_pack_p = false;
1043 declarator->parameter_pack_p = false;
1048 /* Like make_pointer_declarator -- but for references. */
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1054 cp_declarator *declarator;
1056 declarator = make_declarator (cdk_reference);
1057 declarator->declarator = target;
1058 declarator->u.reference.qualifiers = cv_qualifiers;
1059 declarator->u.reference.rvalue_ref = rvalue_ref;
1062 declarator->id_loc = target->id_loc;
1063 declarator->parameter_pack_p = target->parameter_pack_p;
1064 target->parameter_pack_p = false;
1067 declarator->parameter_pack_p = false;
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073 member of CLASS_TYPE. */
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077 cp_declarator *pointee)
1079 cp_declarator *declarator;
1081 declarator = make_declarator (cdk_ptrmem);
1082 declarator->declarator = pointee;
1083 declarator->u.pointer.qualifiers = cv_qualifiers;
1084 declarator->u.pointer.class_type = class_type;
1088 declarator->parameter_pack_p = pointee->parameter_pack_p;
1089 pointee->parameter_pack_p = false;
1092 declarator->parameter_pack_p = false;
1097 /* Make a declarator for the function given by TARGET, with the
1098 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1099 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1100 indicates what exceptions can be thrown. */
1103 make_call_declarator (cp_declarator *target,
1105 cp_cv_quals cv_qualifiers,
1106 tree exception_specification,
1107 tree late_return_type)
1109 cp_declarator *declarator;
1111 declarator = make_declarator (cdk_function);
1112 declarator->declarator = target;
1113 declarator->u.function.parameters = parms;
1114 declarator->u.function.qualifiers = cv_qualifiers;
1115 declarator->u.function.exception_specification = exception_specification;
1116 declarator->u.function.late_return_type = late_return_type;
1119 declarator->id_loc = target->id_loc;
1120 declarator->parameter_pack_p = target->parameter_pack_p;
1121 target->parameter_pack_p = false;
1124 declarator->parameter_pack_p = false;
1129 /* Make a declarator for an array of BOUNDS elements, each of which is
1130 defined by ELEMENT. */
1133 make_array_declarator (cp_declarator *element, tree bounds)
1135 cp_declarator *declarator;
1137 declarator = make_declarator (cdk_array);
1138 declarator->declarator = element;
1139 declarator->u.array.bounds = bounds;
1142 declarator->id_loc = element->id_loc;
1143 declarator->parameter_pack_p = element->parameter_pack_p;
1144 element->parameter_pack_p = false;
1147 declarator->parameter_pack_p = false;
1152 /* Determine whether the declarator we've seen so far can be a
1153 parameter pack, when followed by an ellipsis. */
1155 declarator_can_be_parameter_pack (cp_declarator *declarator)
1157 /* Search for a declarator name, or any other declarator that goes
1158 after the point where the ellipsis could appear in a parameter
1159 pack. If we find any of these, then this declarator can not be
1160 made into a parameter pack. */
1162 while (declarator && !found)
1164 switch ((int)declarator->kind)
1175 declarator = declarator->declarator;
1183 cp_parameter_declarator *no_parameters;
1185 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1186 DECLARATOR and DEFAULT_ARGUMENT. */
1188 cp_parameter_declarator *
1189 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1190 cp_declarator *declarator,
1191 tree default_argument)
1193 cp_parameter_declarator *parameter;
1195 parameter = ((cp_parameter_declarator *)
1196 alloc_declarator (sizeof (cp_parameter_declarator)));
1197 parameter->next = NULL;
1198 if (decl_specifiers)
1199 parameter->decl_specifiers = *decl_specifiers;
1201 clear_decl_specs (¶meter->decl_specifiers);
1202 parameter->declarator = declarator;
1203 parameter->default_argument = default_argument;
1204 parameter->ellipsis_p = false;
1209 /* Returns true iff DECLARATOR is a declaration for a function. */
1212 function_declarator_p (const cp_declarator *declarator)
1216 if (declarator->kind == cdk_function
1217 && declarator->declarator->kind == cdk_id)
1219 if (declarator->kind == cdk_id
1220 || declarator->kind == cdk_error)
1222 declarator = declarator->declarator;
1232 A cp_parser parses the token stream as specified by the C++
1233 grammar. Its job is purely parsing, not semantic analysis. For
1234 example, the parser breaks the token stream into declarators,
1235 expressions, statements, and other similar syntactic constructs.
1236 It does not check that the types of the expressions on either side
1237 of an assignment-statement are compatible, or that a function is
1238 not declared with a parameter of type `void'.
1240 The parser invokes routines elsewhere in the compiler to perform
1241 semantic analysis and to build up the abstract syntax tree for the
1244 The parser (and the template instantiation code, which is, in a
1245 way, a close relative of parsing) are the only parts of the
1246 compiler that should be calling push_scope and pop_scope, or
1247 related functions. The parser (and template instantiation code)
1248 keeps track of what scope is presently active; everything else
1249 should simply honor that. (The code that generates static
1250 initializers may also need to set the scope, in order to check
1251 access control correctly when emitting the initializers.)
1256 The parser is of the standard recursive-descent variety. Upcoming
1257 tokens in the token stream are examined in order to determine which
1258 production to use when parsing a non-terminal. Some C++ constructs
1259 require arbitrary look ahead to disambiguate. For example, it is
1260 impossible, in the general case, to tell whether a statement is an
1261 expression or declaration without scanning the entire statement.
1262 Therefore, the parser is capable of "parsing tentatively." When the
1263 parser is not sure what construct comes next, it enters this mode.
1264 Then, while we attempt to parse the construct, the parser queues up
1265 error messages, rather than issuing them immediately, and saves the
1266 tokens it consumes. If the construct is parsed successfully, the
1267 parser "commits", i.e., it issues any queued error messages and
1268 the tokens that were being preserved are permanently discarded.
1269 If, however, the construct is not parsed successfully, the parser
1270 rolls back its state completely so that it can resume parsing using
1271 a different alternative.
1276 The performance of the parser could probably be improved substantially.
1277 We could often eliminate the need to parse tentatively by looking ahead
1278 a little bit. In some places, this approach might not entirely eliminate
1279 the need to parse tentatively, but it might still speed up the average
1282 /* Flags that are passed to some parsing functions. These values can
1283 be bitwise-ored together. */
1288 CP_PARSER_FLAGS_NONE = 0x0,
1289 /* The construct is optional. If it is not present, then no error
1290 should be issued. */
1291 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1292 /* When parsing a type-specifier, treat user-defined type-names
1293 as non-type identifiers. */
1294 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1295 /* When parsing a type-specifier, do not try to parse a class-specifier
1296 or enum-specifier. */
1297 CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1298 /* When parsing a decl-specifier-seq, only allow type-specifier or
1300 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1303 /* This type is used for parameters and variables which hold
1304 combinations of the above flags. */
1305 typedef int cp_parser_flags;
1307 /* The different kinds of declarators we want to parse. */
1309 typedef enum cp_parser_declarator_kind
1311 /* We want an abstract declarator. */
1312 CP_PARSER_DECLARATOR_ABSTRACT,
1313 /* We want a named declarator. */
1314 CP_PARSER_DECLARATOR_NAMED,
1315 /* We don't mind, but the name must be an unqualified-id. */
1316 CP_PARSER_DECLARATOR_EITHER
1317 } cp_parser_declarator_kind;
1319 /* The precedence values used to parse binary expressions. The minimum value
1320 of PREC must be 1, because zero is reserved to quickly discriminate
1321 binary operators from other tokens. */
1326 PREC_LOGICAL_OR_EXPRESSION,
1327 PREC_LOGICAL_AND_EXPRESSION,
1328 PREC_INCLUSIVE_OR_EXPRESSION,
1329 PREC_EXCLUSIVE_OR_EXPRESSION,
1330 PREC_AND_EXPRESSION,
1331 PREC_EQUALITY_EXPRESSION,
1332 PREC_RELATIONAL_EXPRESSION,
1333 PREC_SHIFT_EXPRESSION,
1334 PREC_ADDITIVE_EXPRESSION,
1335 PREC_MULTIPLICATIVE_EXPRESSION,
1337 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1340 /* A mapping from a token type to a corresponding tree node type, with a
1341 precedence value. */
1343 typedef struct cp_parser_binary_operations_map_node
1345 /* The token type. */
1346 enum cpp_ttype token_type;
1347 /* The corresponding tree code. */
1348 enum tree_code tree_type;
1349 /* The precedence of this operator. */
1350 enum cp_parser_prec prec;
1351 } cp_parser_binary_operations_map_node;
1353 typedef struct cp_parser_expression_stack_entry
1355 /* Left hand side of the binary operation we are currently
1358 /* Original tree code for left hand side, if it was a binary
1359 expression itself (used for -Wparentheses). */
1360 enum tree_code lhs_type;
1361 /* Tree code for the binary operation we are parsing. */
1362 enum tree_code tree_type;
1363 /* Precedence of the binary operation we are parsing. */
1364 enum cp_parser_prec prec;
1365 } cp_parser_expression_stack_entry;
1367 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1368 entries because precedence levels on the stack are monotonically
1370 typedef struct cp_parser_expression_stack_entry
1371 cp_parser_expression_stack[NUM_PREC_VALUES];
1375 /* Constructors and destructors. */
1377 static cp_parser_context *cp_parser_context_new
1378 (cp_parser_context *);
1380 /* Class variables. */
1382 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1384 /* The operator-precedence table used by cp_parser_binary_expression.
1385 Transformed into an associative array (binops_by_token) by
1388 static const cp_parser_binary_operations_map_node binops[] = {
1389 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1390 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1392 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1393 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1394 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1397 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1400 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1403 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1404 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1405 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1408 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1410 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1412 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1414 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1416 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1418 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1421 /* The same as binops, but initialized by cp_parser_new so that
1422 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1424 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1426 /* Constructors and destructors. */
1428 /* Construct a new context. The context below this one on the stack
1429 is given by NEXT. */
1431 static cp_parser_context *
1432 cp_parser_context_new (cp_parser_context* next)
1434 cp_parser_context *context;
1436 /* Allocate the storage. */
1437 if (cp_parser_context_free_list != NULL)
1439 /* Pull the first entry from the free list. */
1440 context = cp_parser_context_free_list;
1441 cp_parser_context_free_list = context->next;
1442 memset (context, 0, sizeof (*context));
1445 context = ggc_alloc_cleared_cp_parser_context ();
1447 /* No errors have occurred yet in this context. */
1448 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1449 /* If this is not the bottommost context, copy information that we
1450 need from the previous context. */
1453 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1454 expression, then we are parsing one in this context, too. */
1455 context->object_type = next->object_type;
1456 /* Thread the stack. */
1457 context->next = next;
1463 /* Managing the unparsed function queues. */
1465 #define unparsed_funs_with_default_args \
1466 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1467 #define unparsed_funs_with_definitions \
1468 VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1471 push_unparsed_function_queues (cp_parser *parser)
1473 VEC_safe_push (cp_unparsed_functions_entry, gc,
1474 parser->unparsed_queues, NULL);
1475 unparsed_funs_with_default_args = NULL;
1476 unparsed_funs_with_definitions = make_tree_vector ();
1480 pop_unparsed_function_queues (cp_parser *parser)
1482 release_tree_vector (unparsed_funs_with_definitions);
1483 VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1488 /* Constructors and destructors. */
1490 static cp_parser *cp_parser_new
1493 /* Routines to parse various constructs.
1495 Those that return `tree' will return the error_mark_node (rather
1496 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1497 Sometimes, they will return an ordinary node if error-recovery was
1498 attempted, even though a parse error occurred. So, to check
1499 whether or not a parse error occurred, you should always use
1500 cp_parser_error_occurred. If the construct is optional (indicated
1501 either by an `_opt' in the name of the function that does the
1502 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1503 the construct is not present. */
1505 /* Lexical conventions [gram.lex] */
1507 static tree cp_parser_identifier
1509 static tree cp_parser_string_literal
1510 (cp_parser *, bool, bool);
1512 /* Basic concepts [gram.basic] */
1514 static bool cp_parser_translation_unit
1517 /* Expressions [gram.expr] */
1519 static tree cp_parser_primary_expression
1520 (cp_parser *, bool, bool, bool, cp_id_kind *);
1521 static tree cp_parser_id_expression
1522 (cp_parser *, bool, bool, bool *, bool, bool);
1523 static tree cp_parser_unqualified_id
1524 (cp_parser *, bool, bool, bool, bool);
1525 static tree cp_parser_nested_name_specifier_opt
1526 (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier
1528 (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_qualifying_entity
1530 (cp_parser *, bool, bool, bool, bool, bool);
1531 static tree cp_parser_postfix_expression
1532 (cp_parser *, bool, bool, bool, cp_id_kind *);
1533 static tree cp_parser_postfix_open_square_expression
1534 (cp_parser *, tree, bool);
1535 static tree cp_parser_postfix_dot_deref_expression
1536 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1537 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1538 (cp_parser *, int, bool, bool, bool *);
1539 /* Values for the second parameter of cp_parser_parenthesized_expression_list. */
1540 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1541 static void cp_parser_pseudo_destructor_name
1542 (cp_parser *, tree *, tree *);
1543 static tree cp_parser_unary_expression
1544 (cp_parser *, bool, bool, cp_id_kind *);
1545 static enum tree_code cp_parser_unary_operator
1547 static tree cp_parser_new_expression
1549 static VEC(tree,gc) *cp_parser_new_placement
1551 static tree cp_parser_new_type_id
1552 (cp_parser *, tree *);
1553 static cp_declarator *cp_parser_new_declarator_opt
1555 static cp_declarator *cp_parser_direct_new_declarator
1557 static VEC(tree,gc) *cp_parser_new_initializer
1559 static tree cp_parser_delete_expression
1561 static tree cp_parser_cast_expression
1562 (cp_parser *, bool, bool, cp_id_kind *);
1563 static tree cp_parser_binary_expression
1564 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1565 static tree cp_parser_question_colon_clause
1566 (cp_parser *, tree);
1567 static tree cp_parser_assignment_expression
1568 (cp_parser *, bool, cp_id_kind *);
1569 static enum tree_code cp_parser_assignment_operator_opt
1571 static tree cp_parser_expression
1572 (cp_parser *, bool, cp_id_kind *);
1573 static tree cp_parser_constant_expression
1574 (cp_parser *, bool, bool *);
1575 static tree cp_parser_builtin_offsetof
1577 static tree cp_parser_lambda_expression
1579 static void cp_parser_lambda_introducer
1580 (cp_parser *, tree);
1581 static void cp_parser_lambda_declarator_opt
1582 (cp_parser *, tree);
1583 static void cp_parser_lambda_body
1584 (cp_parser *, tree);
1586 /* Statements [gram.stmt.stmt] */
1588 static void cp_parser_statement
1589 (cp_parser *, tree, bool, bool *);
1590 static void cp_parser_label_for_labeled_statement
1592 static tree cp_parser_expression_statement
1593 (cp_parser *, tree);
1594 static tree cp_parser_compound_statement
1595 (cp_parser *, tree, bool, bool);
1596 static void cp_parser_statement_seq_opt
1597 (cp_parser *, tree);
1598 static tree cp_parser_selection_statement
1599 (cp_parser *, bool *);
1600 static tree cp_parser_condition
1602 static tree cp_parser_iteration_statement
1604 static bool cp_parser_for_init_statement
1605 (cp_parser *, tree *decl);
1606 static tree cp_parser_for
1608 static tree cp_parser_c_for
1609 (cp_parser *, tree, tree);
1610 static tree cp_parser_range_for
1611 (cp_parser *, tree, tree, tree);
1612 static tree cp_parser_perform_range_for_lookup
1613 (tree, tree *, tree *);
1614 static tree cp_parser_range_for_member_function
1616 static tree cp_parser_jump_statement
1618 static void cp_parser_declaration_statement
1621 static tree cp_parser_implicitly_scoped_statement
1622 (cp_parser *, bool *);
1623 static void cp_parser_already_scoped_statement
1626 /* Declarations [gram.dcl.dcl] */
1628 static void cp_parser_declaration_seq_opt
1630 static void cp_parser_declaration
1632 static void cp_parser_block_declaration
1633 (cp_parser *, bool);
1634 static void cp_parser_simple_declaration
1635 (cp_parser *, bool, tree *);
1636 static void cp_parser_decl_specifier_seq
1637 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1640 static tree cp_parser_function_specifier_opt
1641 (cp_parser *, cp_decl_specifier_seq *);
1642 static tree cp_parser_type_specifier
1643 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1645 static tree cp_parser_simple_type_specifier
1646 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1647 static tree cp_parser_type_name
1649 static tree cp_parser_nonclass_name
1650 (cp_parser* parser);
1651 static tree cp_parser_elaborated_type_specifier
1652 (cp_parser *, bool, bool);
1653 static tree cp_parser_enum_specifier
1655 static void cp_parser_enumerator_list
1656 (cp_parser *, tree);
1657 static void cp_parser_enumerator_definition
1658 (cp_parser *, tree);
1659 static tree cp_parser_namespace_name
1661 static void cp_parser_namespace_definition
1663 static void cp_parser_namespace_body
1665 static tree cp_parser_qualified_namespace_specifier
1667 static void cp_parser_namespace_alias_definition
1669 static bool cp_parser_using_declaration
1670 (cp_parser *, bool);
1671 static void cp_parser_using_directive
1673 static void cp_parser_asm_definition
1675 static void cp_parser_linkage_specification
1677 static void cp_parser_static_assert
1678 (cp_parser *, bool);
1679 static tree cp_parser_decltype
1682 /* Declarators [gram.dcl.decl] */
1684 static tree cp_parser_init_declarator
1685 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1686 static cp_declarator *cp_parser_declarator
1687 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1688 static cp_declarator *cp_parser_direct_declarator
1689 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1690 static enum tree_code cp_parser_ptr_operator
1691 (cp_parser *, tree *, cp_cv_quals *);
1692 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1694 static tree cp_parser_late_return_type_opt
1696 static tree cp_parser_declarator_id
1697 (cp_parser *, bool);
1698 static tree cp_parser_type_id
1700 static tree cp_parser_template_type_arg
1702 static tree cp_parser_trailing_type_id (cp_parser *);
1703 static tree cp_parser_type_id_1
1704 (cp_parser *, bool, bool);
1705 static void cp_parser_type_specifier_seq
1706 (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1707 static tree cp_parser_parameter_declaration_clause
1709 static tree cp_parser_parameter_declaration_list
1710 (cp_parser *, bool *);
1711 static cp_parameter_declarator *cp_parser_parameter_declaration
1712 (cp_parser *, bool, bool *);
1713 static tree cp_parser_default_argument
1714 (cp_parser *, bool);
1715 static void cp_parser_function_body
1717 static tree cp_parser_initializer
1718 (cp_parser *, bool *, bool *);
1719 static tree cp_parser_initializer_clause
1720 (cp_parser *, bool *);
1721 static tree cp_parser_braced_list
1722 (cp_parser*, bool*);
1723 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1724 (cp_parser *, bool *);
1726 static bool cp_parser_ctor_initializer_opt_and_function_body
1729 /* Classes [gram.class] */
1731 static tree cp_parser_class_name
1732 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1733 static tree cp_parser_class_specifier
1735 static tree cp_parser_class_head
1736 (cp_parser *, bool *, tree *, tree *);
1737 static enum tag_types cp_parser_class_key
1739 static void cp_parser_member_specification_opt
1741 static void cp_parser_member_declaration
1743 static tree cp_parser_pure_specifier
1745 static tree cp_parser_constant_initializer
1748 /* Derived classes [gram.class.derived] */
1750 static tree cp_parser_base_clause
1752 static tree cp_parser_base_specifier
1755 /* Special member functions [gram.special] */
1757 static tree cp_parser_conversion_function_id
1759 static tree cp_parser_conversion_type_id
1761 static cp_declarator *cp_parser_conversion_declarator_opt
1763 static bool cp_parser_ctor_initializer_opt
1765 static void cp_parser_mem_initializer_list
1767 static tree cp_parser_mem_initializer
1769 static tree cp_parser_mem_initializer_id
1772 /* Overloading [gram.over] */
1774 static tree cp_parser_operator_function_id
1776 static tree cp_parser_operator
1779 /* Templates [gram.temp] */
1781 static void cp_parser_template_declaration
1782 (cp_parser *, bool);
1783 static tree cp_parser_template_parameter_list
1785 static tree cp_parser_template_parameter
1786 (cp_parser *, bool *, bool *);
1787 static tree cp_parser_type_parameter
1788 (cp_parser *, bool *);
1789 static tree cp_parser_template_id
1790 (cp_parser *, bool, bool, bool);
1791 static tree cp_parser_template_name
1792 (cp_parser *, bool, bool, bool, bool *);
1793 static tree cp_parser_template_argument_list
1795 static tree cp_parser_template_argument
1797 static void cp_parser_explicit_instantiation
1799 static void cp_parser_explicit_specialization
1802 /* Exception handling [gram.exception] */
1804 static tree cp_parser_try_block
1806 static bool cp_parser_function_try_block
1808 static void cp_parser_handler_seq
1810 static void cp_parser_handler
1812 static tree cp_parser_exception_declaration
1814 static tree cp_parser_throw_expression
1816 static tree cp_parser_exception_specification_opt
1818 static tree cp_parser_type_id_list
1821 /* GNU Extensions */
1823 static tree cp_parser_asm_specification_opt
1825 static tree cp_parser_asm_operand_list
1827 static tree cp_parser_asm_clobber_list
1829 static tree cp_parser_asm_label_list
1831 static tree cp_parser_attributes_opt
1833 static tree cp_parser_attribute_list
1835 static bool cp_parser_extension_opt
1836 (cp_parser *, int *);
1837 static void cp_parser_label_declaration
1840 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1841 static bool cp_parser_pragma
1842 (cp_parser *, enum pragma_context);
1844 /* Objective-C++ Productions */
1846 static tree cp_parser_objc_message_receiver
1848 static tree cp_parser_objc_message_args
1850 static tree cp_parser_objc_message_expression
1852 static tree cp_parser_objc_encode_expression
1854 static tree cp_parser_objc_defs_expression
1856 static tree cp_parser_objc_protocol_expression
1858 static tree cp_parser_objc_selector_expression
1860 static tree cp_parser_objc_expression
1862 static bool cp_parser_objc_selector_p
1864 static tree cp_parser_objc_selector
1866 static tree cp_parser_objc_protocol_refs_opt
1868 static void cp_parser_objc_declaration
1869 (cp_parser *, tree);
1870 static tree cp_parser_objc_statement
1872 static bool cp_parser_objc_valid_prefix_attributes
1873 (cp_parser *, tree *);
1874 static void cp_parser_objc_at_property_declaration
1876 static void cp_parser_objc_at_synthesize_declaration
1878 static void cp_parser_objc_at_dynamic_declaration
1880 static tree cp_parser_objc_struct_declaration
1883 /* Utility Routines */
1885 static tree cp_parser_lookup_name
1886 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1887 static tree cp_parser_lookup_name_simple
1888 (cp_parser *, tree, location_t);
1889 static tree cp_parser_maybe_treat_template_as_class
1891 static bool cp_parser_check_declarator_template_parameters
1892 (cp_parser *, cp_declarator *, location_t);
1893 static bool cp_parser_check_template_parameters
1894 (cp_parser *, unsigned, location_t, cp_declarator *);
1895 static tree cp_parser_simple_cast_expression
1897 static tree cp_parser_global_scope_opt
1898 (cp_parser *, bool);
1899 static bool cp_parser_constructor_declarator_p
1900 (cp_parser *, bool);
1901 static tree cp_parser_function_definition_from_specifiers_and_declarator
1902 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1903 static tree cp_parser_function_definition_after_declarator
1904 (cp_parser *, bool);
1905 static void cp_parser_template_declaration_after_export
1906 (cp_parser *, bool);
1907 static void cp_parser_perform_template_parameter_access_checks
1908 (VEC (deferred_access_check,gc)*);
1909 static tree cp_parser_single_declaration
1910 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1911 static tree cp_parser_functional_cast
1912 (cp_parser *, tree);
1913 static tree cp_parser_save_member_function_body
1914 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1915 static tree cp_parser_enclosed_template_argument_list
1917 static void cp_parser_save_default_args
1918 (cp_parser *, tree);
1919 static void cp_parser_late_parsing_for_member
1920 (cp_parser *, tree);
1921 static void cp_parser_late_parsing_default_args
1922 (cp_parser *, tree);
1923 static tree cp_parser_sizeof_operand
1924 (cp_parser *, enum rid);
1925 static tree cp_parser_trait_expr
1926 (cp_parser *, enum rid);
1927 static bool cp_parser_declares_only_class_p
1929 static void cp_parser_set_storage_class
1930 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1931 static void cp_parser_set_decl_spec_type
1932 (cp_decl_specifier_seq *, tree, location_t, bool);
1933 static bool cp_parser_friend_p
1934 (const cp_decl_specifier_seq *);
1935 static void cp_parser_required_error
1936 (cp_parser *, required_token, bool);
1937 static cp_token *cp_parser_require
1938 (cp_parser *, enum cpp_ttype, required_token);
1939 static cp_token *cp_parser_require_keyword
1940 (cp_parser *, enum rid, required_token);
1941 static bool cp_parser_token_starts_function_definition_p
1943 static bool cp_parser_next_token_starts_class_definition_p
1945 static bool cp_parser_next_token_ends_template_argument_p
1947 static bool cp_parser_nth_token_starts_template_argument_list_p
1948 (cp_parser *, size_t);
1949 static enum tag_types cp_parser_token_is_class_key
1951 static void cp_parser_check_class_key
1952 (enum tag_types, tree type);
1953 static void cp_parser_check_access_in_redeclaration
1954 (tree type, location_t location);
1955 static bool cp_parser_optional_template_keyword
1957 static void cp_parser_pre_parsed_nested_name_specifier
1959 static bool cp_parser_cache_group
1960 (cp_parser *, enum cpp_ttype, unsigned);
1961 static void cp_parser_parse_tentatively
1963 static void cp_parser_commit_to_tentative_parse
1965 static void cp_parser_abort_tentative_parse
1967 static bool cp_parser_parse_definitely
1969 static inline bool cp_parser_parsing_tentatively
1971 static bool cp_parser_uncommitted_to_tentative_parse_p
1973 static void cp_parser_error
1974 (cp_parser *, const char *);
1975 static void cp_parser_name_lookup_error
1976 (cp_parser *, tree, tree, name_lookup_error, location_t);
1977 static bool cp_parser_simulate_error
1979 static bool cp_parser_check_type_definition
1981 static void cp_parser_check_for_definition_in_return_type
1982 (cp_declarator *, tree, location_t type_location);
1983 static void cp_parser_check_for_invalid_template_id
1984 (cp_parser *, tree, location_t location);
1985 static bool cp_parser_non_integral_constant_expression
1986 (cp_parser *, non_integral_constant);
1987 static void cp_parser_diagnose_invalid_type_name
1988 (cp_parser *, tree, tree, location_t);
1989 static bool cp_parser_parse_and_diagnose_invalid_type_name
1991 static int cp_parser_skip_to_closing_parenthesis
1992 (cp_parser *, bool, bool, bool);
1993 static void cp_parser_skip_to_end_of_statement
1995 static void cp_parser_consume_semicolon_at_end_of_statement
1997 static void cp_parser_skip_to_end_of_block_or_statement
1999 static bool cp_parser_skip_to_closing_brace
2001 static void cp_parser_skip_to_end_of_template_parameter_list
2003 static void cp_parser_skip_to_pragma_eol
2004 (cp_parser*, cp_token *);
2005 static bool cp_parser_error_occurred
2007 static bool cp_parser_allow_gnu_extensions_p
2009 static bool cp_parser_is_string_literal
2011 static bool cp_parser_is_keyword
2012 (cp_token *, enum rid);
2013 static tree cp_parser_make_typename_type
2014 (cp_parser *, tree, tree, location_t location);
2015 static cp_declarator * cp_parser_make_indirect_declarator
2016 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2018 /* Returns nonzero if we are parsing tentatively. */
2021 cp_parser_parsing_tentatively (cp_parser* parser)
2023 return parser->context->next != NULL;
2026 /* Returns nonzero if TOKEN is a string literal. */
2029 cp_parser_is_string_literal (cp_token* token)
2031 return (token->type == CPP_STRING ||
2032 token->type == CPP_STRING16 ||
2033 token->type == CPP_STRING32 ||
2034 token->type == CPP_WSTRING ||
2035 token->type == CPP_UTF8STRING);
2038 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2041 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2043 return token->keyword == keyword;
2046 /* If not parsing tentatively, issue a diagnostic of the form
2047 FILE:LINE: MESSAGE before TOKEN
2048 where TOKEN is the next token in the input stream. MESSAGE
2049 (specified by the caller) is usually of the form "expected
2053 cp_parser_error (cp_parser* parser, const char* gmsgid)
2055 if (!cp_parser_simulate_error (parser))
2057 cp_token *token = cp_lexer_peek_token (parser->lexer);
2058 /* This diagnostic makes more sense if it is tagged to the line
2059 of the token we just peeked at. */
2060 cp_lexer_set_source_position_from_token (token);
2062 if (token->type == CPP_PRAGMA)
2064 error_at (token->location,
2065 "%<#pragma%> is not allowed here");
2066 cp_parser_skip_to_pragma_eol (parser, token);
2070 c_parse_error (gmsgid,
2071 /* Because c_parser_error does not understand
2072 CPP_KEYWORD, keywords are treated like
2074 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2075 token->u.value, token->flags);
2079 /* Issue an error about name-lookup failing. NAME is the
2080 IDENTIFIER_NODE DECL is the result of
2081 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2082 the thing that we hoped to find. */
2085 cp_parser_name_lookup_error (cp_parser* parser,
2088 name_lookup_error desired,
2089 location_t location)
2091 /* If name lookup completely failed, tell the user that NAME was not
2093 if (decl == error_mark_node)
2095 if (parser->scope && parser->scope != global_namespace)
2096 error_at (location, "%<%E::%E%> has not been declared",
2097 parser->scope, name);
2098 else if (parser->scope == global_namespace)
2099 error_at (location, "%<::%E%> has not been declared", name);
2100 else if (parser->object_scope
2101 && !CLASS_TYPE_P (parser->object_scope))
2102 error_at (location, "request for member %qE in non-class type %qT",
2103 name, parser->object_scope);
2104 else if (parser->object_scope)
2105 error_at (location, "%<%T::%E%> has not been declared",
2106 parser->object_scope, name);
2108 error_at (location, "%qE has not been declared", name);
2110 else if (parser->scope && parser->scope != global_namespace)
2115 error_at (location, "%<%E::%E%> is not a type",
2116 parser->scope, name);
2119 error_at (location, "%<%E::%E%> is not a class or namespace",
2120 parser->scope, name);
2124 "%<%E::%E%> is not a class, namespace, or enumeration",
2125 parser->scope, name);
2132 else if (parser->scope == global_namespace)
2137 error_at (location, "%<::%E%> is not a type", name);
2140 error_at (location, "%<::%E%> is not a class or namespace", name);
2144 "%<::%E%> is not a class, namespace, or enumeration",
2156 error_at (location, "%qE is not a type", name);
2159 error_at (location, "%qE is not a class or namespace", name);
2163 "%qE is not a class, namespace, or enumeration", name);
2171 /* If we are parsing tentatively, remember that an error has occurred
2172 during this tentative parse. Returns true if the error was
2173 simulated; false if a message should be issued by the caller. */
2176 cp_parser_simulate_error (cp_parser* parser)
2178 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2180 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2186 /* Check for repeated decl-specifiers. */
2189 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2190 location_t location)
2194 for (ds = ds_first; ds != ds_last; ++ds)
2196 unsigned count = decl_specs->specs[ds];
2199 /* The "long" specifier is a special case because of "long long". */
2203 error_at (location, "%<long long long%> is too long for GCC");
2205 pedwarn_cxx98 (location, OPT_Wlong_long,
2206 "ISO C++ 1998 does not support %<long long%>");
2210 static const char *const decl_spec_names[] = {
2227 error_at (location, "duplicate %qs", decl_spec_names[ds]);
2232 /* This function is called when a type is defined. If type
2233 definitions are forbidden at this point, an error message is
2237 cp_parser_check_type_definition (cp_parser* parser)
2239 /* If types are forbidden here, issue a message. */
2240 if (parser->type_definition_forbidden_message)
2242 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2243 in the message need to be interpreted. */
2244 error (parser->type_definition_forbidden_message);
2250 /* This function is called when the DECLARATOR is processed. The TYPE
2251 was a type defined in the decl-specifiers. If it is invalid to
2252 define a type in the decl-specifiers for DECLARATOR, an error is
2253 issued. TYPE_LOCATION is the location of TYPE and is used
2254 for error reporting. */
2257 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2258 tree type, location_t type_location)
2260 /* [dcl.fct] forbids type definitions in return types.
2261 Unfortunately, it's not easy to know whether or not we are
2262 processing a return type until after the fact. */
2264 && (declarator->kind == cdk_pointer
2265 || declarator->kind == cdk_reference
2266 || declarator->kind == cdk_ptrmem))
2267 declarator = declarator->declarator;
2269 && declarator->kind == cdk_function)
2271 error_at (type_location,
2272 "new types may not be defined in a return type");
2273 inform (type_location,
2274 "(perhaps a semicolon is missing after the definition of %qT)",
2279 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2280 "<" in any valid C++ program. If the next token is indeed "<",
2281 issue a message warning the user about what appears to be an
2282 invalid attempt to form a template-id. LOCATION is the location
2283 of the type-specifier (TYPE) */
2286 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2287 tree type, location_t location)
2289 cp_token_position start = 0;
2291 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2294 error_at (location, "%qT is not a template", type);
2295 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2296 error_at (location, "%qE is not a template", type);
2298 error_at (location, "invalid template-id");
2299 /* Remember the location of the invalid "<". */
2300 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2301 start = cp_lexer_token_position (parser->lexer, true);
2302 /* Consume the "<". */
2303 cp_lexer_consume_token (parser->lexer);
2304 /* Parse the template arguments. */
2305 cp_parser_enclosed_template_argument_list (parser);
2306 /* Permanently remove the invalid template arguments so that
2307 this error message is not issued again. */
2309 cp_lexer_purge_tokens_after (parser->lexer, start);
2313 /* If parsing an integral constant-expression, issue an error message
2314 about the fact that THING appeared and return true. Otherwise,
2315 return false. In either case, set
2316 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2319 cp_parser_non_integral_constant_expression (cp_parser *parser,
2320 non_integral_constant thing)
2322 parser->non_integral_constant_expression_p = true;
2323 if (parser->integral_constant_expression_p)
2325 if (!parser->allow_non_integral_constant_expression_p)
2327 const char *msg = NULL;
2331 error ("floating-point literal "
2332 "cannot appear in a constant-expression");
2335 error ("a cast to a type other than an integral or "
2336 "enumeration type cannot appear in a "
2337 "constant-expression");
2340 error ("%<typeid%> operator "
2341 "cannot appear in a constant-expression");
2344 error ("non-constant compound literals "
2345 "cannot appear in a constant-expression");
2348 error ("a function call "
2349 "cannot appear in a constant-expression");
2352 error ("an increment "
2353 "cannot appear in a constant-expression");
2356 error ("an decrement "
2357 "cannot appear in a constant-expression");
2360 error ("an array reference "
2361 "cannot appear in a constant-expression");
2363 case NIC_ADDR_LABEL:
2364 error ("the address of a label "
2365 "cannot appear in a constant-expression");
2367 case NIC_OVERLOADED:
2368 error ("calls to overloaded operators "
2369 "cannot appear in a constant-expression");
2371 case NIC_ASSIGNMENT:
2372 error ("an assignment cannot appear in a constant-expression");
2375 error ("a comma operator "
2376 "cannot appear in a constant-expression");
2378 case NIC_CONSTRUCTOR:
2379 error ("a call to a constructor "
2380 "cannot appear in a constant-expression");
2386 msg = "__FUNCTION__";
2388 case NIC_PRETTY_FUNC:
2389 msg = "__PRETTY_FUNCTION__";
2409 case NIC_PREINCREMENT:
2412 case NIC_PREDECREMENT:
2425 error ("%qs cannot appear in a constant-expression", msg);
2432 /* Emit a diagnostic for an invalid type name. SCOPE is the
2433 qualifying scope (or NULL, if none) for ID. This function commits
2434 to the current active tentative parse, if any. (Otherwise, the
2435 problematic construct might be encountered again later, resulting
2436 in duplicate error messages.) LOCATION is the location of ID. */
2439 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2440 tree scope, tree id,
2441 location_t location)
2443 tree decl, old_scope;
2444 cp_parser_commit_to_tentative_parse (parser);
2445 /* Try to lookup the identifier. */
2446 old_scope = parser->scope;
2447 parser->scope = scope;
2448 decl = cp_parser_lookup_name_simple (parser, id, location);
2449 parser->scope = old_scope;
2450 /* If the lookup found a template-name, it means that the user forgot
2451 to specify an argument list. Emit a useful error message. */
2452 if (TREE_CODE (decl) == TEMPLATE_DECL)
2454 "invalid use of template-name %qE without an argument list",
2456 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2457 error_at (location, "invalid use of destructor %qD as a type", id);
2458 else if (TREE_CODE (decl) == TYPE_DECL)
2459 /* Something like 'unsigned A a;' */
2460 error_at (location, "invalid combination of multiple type-specifiers");
2461 else if (!parser->scope)
2463 /* Issue an error message. */
2464 error_at (location, "%qE does not name a type", id);
2465 /* If we're in a template class, it's possible that the user was
2466 referring to a type from a base class. For example:
2468 template <typename T> struct A { typedef T X; };
2469 template <typename T> struct B : public A<T> { X x; };
2471 The user should have said "typename A<T>::X". */
2472 if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2473 inform (location, "C++0x %<constexpr%> only available with "
2474 "-std=c++0x or -std=gnu++0x");
2475 else if (processing_template_decl && current_class_type
2476 && TYPE_BINFO (current_class_type))
2480 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2484 tree base_type = BINFO_TYPE (b);
2485 if (CLASS_TYPE_P (base_type)
2486 && dependent_type_p (base_type))
2489 /* Go from a particular instantiation of the
2490 template (which will have an empty TYPE_FIELDs),
2491 to the main version. */
2492 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2493 for (field = TYPE_FIELDS (base_type);
2495 field = DECL_CHAIN (field))
2496 if (TREE_CODE (field) == TYPE_DECL
2497 && DECL_NAME (field) == id)
2500 "(perhaps %<typename %T::%E%> was intended)",
2501 BINFO_TYPE (b), id);
2510 /* Here we diagnose qualified-ids where the scope is actually correct,
2511 but the identifier does not resolve to a valid type name. */
2512 else if (parser->scope != error_mark_node)
2514 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2515 error_at (location, "%qE in namespace %qE does not name a type",
2517 else if (CLASS_TYPE_P (parser->scope)
2518 && constructor_name_p (id, parser->scope))
2521 error_at (location, "%<%T::%E%> names the constructor, not"
2522 " the type", parser->scope, id);
2523 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2524 error_at (location, "and %qT has no template constructors",
2527 else if (TYPE_P (parser->scope)
2528 && dependent_scope_p (parser->scope))
2529 error_at (location, "need %<typename%> before %<%T::%E%> because "
2530 "%qT is a dependent scope",
2531 parser->scope, id, parser->scope);
2532 else if (TYPE_P (parser->scope))
2533 error_at (location, "%qE in class %qT does not name a type",
2540 /* Check for a common situation where a type-name should be present,
2541 but is not, and issue a sensible error message. Returns true if an
2542 invalid type-name was detected.
2544 The situation handled by this function are variable declarations of the
2545 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2546 Usually, `ID' should name a type, but if we got here it means that it
2547 does not. We try to emit the best possible error message depending on
2548 how exactly the id-expression looks like. */
2551 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2554 cp_token *token = cp_lexer_peek_token (parser->lexer);
2556 /* Avoid duplicate error about ambiguous lookup. */
2557 if (token->type == CPP_NESTED_NAME_SPECIFIER)
2559 cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2560 if (next->type == CPP_NAME && next->ambiguous_p)
2564 cp_parser_parse_tentatively (parser);
2565 id = cp_parser_id_expression (parser,
2566 /*template_keyword_p=*/false,
2567 /*check_dependency_p=*/true,
2568 /*template_p=*/NULL,
2569 /*declarator_p=*/true,
2570 /*optional_p=*/false);
2571 /* If the next token is a (, this is a function with no explicit return
2572 type, i.e. constructor, destructor or conversion op. */
2573 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2574 || TREE_CODE (id) == TYPE_DECL)
2576 cp_parser_abort_tentative_parse (parser);
2579 if (!cp_parser_parse_definitely (parser))
2582 /* Emit a diagnostic for the invalid type. */
2583 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2584 id, token->location);
2586 /* If we aren't in the middle of a declarator (i.e. in a
2587 parameter-declaration-clause), skip to the end of the declaration;
2588 there's no point in trying to process it. */
2589 if (!parser->in_declarator_p)
2590 cp_parser_skip_to_end_of_block_or_statement (parser);
2594 /* Consume tokens up to, and including, the next non-nested closing `)'.
2595 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2596 are doing error recovery. Returns -1 if OR_COMMA is true and we
2597 found an unnested comma. */
2600 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2605 unsigned paren_depth = 0;
2606 unsigned brace_depth = 0;
2607 unsigned square_depth = 0;
2609 if (recovering && !or_comma
2610 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2615 cp_token * token = cp_lexer_peek_token (parser->lexer);
2617 switch (token->type)
2620 case CPP_PRAGMA_EOL:
2621 /* If we've run out of tokens, then there is no closing `)'. */
2624 /* This is good for lambda expression capture-lists. */
2625 case CPP_OPEN_SQUARE:
2628 case CPP_CLOSE_SQUARE:
2629 if (!square_depth--)
2634 /* This matches the processing in skip_to_end_of_statement. */
2639 case CPP_OPEN_BRACE:
2642 case CPP_CLOSE_BRACE:
2648 if (recovering && or_comma && !brace_depth && !paren_depth
2653 case CPP_OPEN_PAREN:
2658 case CPP_CLOSE_PAREN:
2659 if (!brace_depth && !paren_depth--)
2662 cp_lexer_consume_token (parser->lexer);
2671 /* Consume the token. */
2672 cp_lexer_consume_token (parser->lexer);
2676 /* Consume tokens until we reach the end of the current statement.
2677 Normally, that will be just before consuming a `;'. However, if a
2678 non-nested `}' comes first, then we stop before consuming that. */
2681 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2683 unsigned nesting_depth = 0;
2687 cp_token *token = cp_lexer_peek_token (parser->lexer);
2689 switch (token->type)
2692 case CPP_PRAGMA_EOL:
2693 /* If we've run out of tokens, stop. */
2697 /* If the next token is a `;', we have reached the end of the
2703 case CPP_CLOSE_BRACE:
2704 /* If this is a non-nested '}', stop before consuming it.
2705 That way, when confronted with something like:
2709 we stop before consuming the closing '}', even though we
2710 have not yet reached a `;'. */
2711 if (nesting_depth == 0)
2714 /* If it is the closing '}' for a block that we have
2715 scanned, stop -- but only after consuming the token.
2721 we will stop after the body of the erroneously declared
2722 function, but before consuming the following `typedef'
2724 if (--nesting_depth == 0)
2726 cp_lexer_consume_token (parser->lexer);
2730 case CPP_OPEN_BRACE:
2738 /* Consume the token. */
2739 cp_lexer_consume_token (parser->lexer);
2743 /* This function is called at the end of a statement or declaration.
2744 If the next token is a semicolon, it is consumed; otherwise, error
2745 recovery is attempted. */
2748 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2750 /* Look for the trailing `;'. */
2751 if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2753 /* If there is additional (erroneous) input, skip to the end of
2755 cp_parser_skip_to_end_of_statement (parser);
2756 /* If the next token is now a `;', consume it. */
2757 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2758 cp_lexer_consume_token (parser->lexer);
2762 /* Skip tokens until we have consumed an entire block, or until we
2763 have consumed a non-nested `;'. */
2766 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2768 int nesting_depth = 0;
2770 while (nesting_depth >= 0)
2772 cp_token *token = cp_lexer_peek_token (parser->lexer);
2774 switch (token->type)
2777 case CPP_PRAGMA_EOL:
2778 /* If we've run out of tokens, stop. */
2782 /* Stop if this is an unnested ';'. */
2787 case CPP_CLOSE_BRACE:
2788 /* Stop if this is an unnested '}', or closes the outermost
2791 if (nesting_depth < 0)
2797 case CPP_OPEN_BRACE:
2806 /* Consume the token. */
2807 cp_lexer_consume_token (parser->lexer);
2811 /* Skip tokens until a non-nested closing curly brace is the next
2812 token, or there are no more tokens. Return true in the first case,
2816 cp_parser_skip_to_closing_brace (cp_parser *parser)
2818 unsigned nesting_depth = 0;
2822 cp_token *token = cp_lexer_peek_token (parser->lexer);
2824 switch (token->type)
2827 case CPP_PRAGMA_EOL:
2828 /* If we've run out of tokens, stop. */
2831 case CPP_CLOSE_BRACE:
2832 /* If the next token is a non-nested `}', then we have reached
2833 the end of the current block. */
2834 if (nesting_depth-- == 0)
2838 case CPP_OPEN_BRACE:
2839 /* If it the next token is a `{', then we are entering a new
2840 block. Consume the entire block. */
2848 /* Consume the token. */
2849 cp_lexer_consume_token (parser->lexer);
2853 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2854 parameter is the PRAGMA token, allowing us to purge the entire pragma
2858 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2862 parser->lexer->in_pragma = false;
2865 token = cp_lexer_consume_token (parser->lexer);
2866 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2868 /* Ensure that the pragma is not parsed again. */
2869 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2872 /* Require pragma end of line, resyncing with it as necessary. The
2873 arguments are as for cp_parser_skip_to_pragma_eol. */
2876 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2878 parser->lexer->in_pragma = false;
2879 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2880 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2883 /* This is a simple wrapper around make_typename_type. When the id is
2884 an unresolved identifier node, we can provide a superior diagnostic
2885 using cp_parser_diagnose_invalid_type_name. */
2888 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2889 tree id, location_t id_location)
2892 if (TREE_CODE (id) == IDENTIFIER_NODE)
2894 result = make_typename_type (scope, id, typename_type,
2895 /*complain=*/tf_none);
2896 if (result == error_mark_node)
2897 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2900 return make_typename_type (scope, id, typename_type, tf_error);
2903 /* This is a wrapper around the
2904 make_{pointer,ptrmem,reference}_declarator functions that decides
2905 which one to call based on the CODE and CLASS_TYPE arguments. The
2906 CODE argument should be one of the values returned by
2907 cp_parser_ptr_operator. */
2908 static cp_declarator *
2909 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2910 cp_cv_quals cv_qualifiers,
2911 cp_declarator *target)
2913 if (code == ERROR_MARK)
2914 return cp_error_declarator;
2916 if (code == INDIRECT_REF)
2917 if (class_type == NULL_TREE)
2918 return make_pointer_declarator (cv_qualifiers, target);
2920 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2921 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2922 return make_reference_declarator (cv_qualifiers, target, false);
2923 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2924 return make_reference_declarator (cv_qualifiers, target, true);
2928 /* Create a new C++ parser. */
2931 cp_parser_new (void)
2937 /* cp_lexer_new_main is called before doing GC allocation because
2938 cp_lexer_new_main might load a PCH file. */
2939 lexer = cp_lexer_new_main ();
2941 /* Initialize the binops_by_token so that we can get the tree
2942 directly from the token. */
2943 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2944 binops_by_token[binops[i].token_type] = binops[i];
2946 parser = ggc_alloc_cleared_cp_parser ();
2947 parser->lexer = lexer;
2948 parser->context = cp_parser_context_new (NULL);
2950 /* For now, we always accept GNU extensions. */
2951 parser->allow_gnu_extensions_p = 1;
2953 /* The `>' token is a greater-than operator, not the end of a
2955 parser->greater_than_is_operator_p = true;
2957 parser->default_arg_ok_p = true;
2959 /* We are not parsing a constant-expression. */
2960 parser->integral_constant_expression_p = false;
2961 parser->allow_non_integral_constant_expression_p = false;
2962 parser->non_integral_constant_expression_p = false;
2964 /* Local variable names are not forbidden. */
2965 parser->local_variables_forbidden_p = false;
2967 /* We are not processing an `extern "C"' declaration. */
2968 parser->in_unbraced_linkage_specification_p = false;
2970 /* We are not processing a declarator. */
2971 parser->in_declarator_p = false;
2973 /* We are not processing a template-argument-list. */
2974 parser->in_template_argument_list_p = false;
2976 /* We are not in an iteration statement. */
2977 parser->in_statement = 0;
2979 /* We are not in a switch statement. */
2980 parser->in_switch_statement_p = false;
2982 /* We are not parsing a type-id inside an expression. */
2983 parser->in_type_id_in_expr_p = false;
2985 /* Declarations aren't implicitly extern "C". */
2986 parser->implicit_extern_c = false;
2988 /* String literals should be translated to the execution character set. */
2989 parser->translate_strings_p = true;
2991 /* We are not parsing a function body. */
2992 parser->in_function_body = false;
2994 /* We can correct until told otherwise. */
2995 parser->colon_corrects_to_scope_p = true;
2997 /* The unparsed function queue is empty. */
2998 push_unparsed_function_queues (parser);
3000 /* There are no classes being defined. */
3001 parser->num_classes_being_defined = 0;
3003 /* No template parameters apply. */
3004 parser->num_template_parameter_lists = 0;
3009 /* Create a cp_lexer structure which will emit the tokens in CACHE
3010 and push it onto the parser's lexer stack. This is used for delayed
3011 parsing of in-class method bodies and default arguments, and should
3012 not be confused with tentative parsing. */
3014 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3016 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3017 lexer->next = parser->lexer;
3018 parser->lexer = lexer;
3020 /* Move the current source position to that of the first token in the
3022 cp_lexer_set_source_position_from_token (lexer->next_token);
3025 /* Pop the top lexer off the parser stack. This is never used for the
3026 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
3028 cp_parser_pop_lexer (cp_parser *parser)
3030 cp_lexer *lexer = parser->lexer;
3031 parser->lexer = lexer->next;
3032 cp_lexer_destroy (lexer);
3034 /* Put the current source position back where it was before this
3035 lexer was pushed. */
3036 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3039 /* Lexical conventions [gram.lex] */
3041 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
3045 cp_parser_identifier (cp_parser* parser)
3049 /* Look for the identifier. */
3050 token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3051 /* Return the value. */
3052 return token ? token->u.value : error_mark_node;
3055 /* Parse a sequence of adjacent string constants. Returns a
3056 TREE_STRING representing the combined, nul-terminated string
3057 constant. If TRANSLATE is true, translate the string to the
3058 execution character set. If WIDE_OK is true, a wide string is
3061 C++98 [lex.string] says that if a narrow string literal token is
3062 adjacent to a wide string literal token, the behavior is undefined.
3063 However, C99 6.4.5p4 says that this results in a wide string literal.
3064 We follow C99 here, for consistency with the C front end.
3066 This code is largely lifted from lex_string() in c-lex.c.
3068 FUTURE: ObjC++ will need to handle @-strings here. */
3070 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3074 struct obstack str_ob;
3075 cpp_string str, istr, *strs;
3077 enum cpp_ttype type;
3079 tok = cp_lexer_peek_token (parser->lexer);
3080 if (!cp_parser_is_string_literal (tok))
3082 cp_parser_error (parser, "expected string-literal");
3083 return error_mark_node;
3088 /* Try to avoid the overhead of creating and destroying an obstack
3089 for the common case of just one string. */
3090 if (!cp_parser_is_string_literal
3091 (cp_lexer_peek_nth_token (parser->lexer, 2)))
3093 cp_lexer_consume_token (parser->lexer);
3095 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3096 str.len = TREE_STRING_LENGTH (tok->u.value);
3103 gcc_obstack_init (&str_ob);
3108 cp_lexer_consume_token (parser->lexer);
3110 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3111 str.len = TREE_STRING_LENGTH (tok->u.value);
3113 if (type != tok->type)
3115 if (type == CPP_STRING)
3117 else if (tok->type != CPP_STRING)
3118 error_at (tok->location,
3119 "unsupported non-standard concatenation "
3120 "of string literals");
3123 obstack_grow (&str_ob, &str, sizeof (cpp_string));
3125 tok = cp_lexer_peek_token (parser->lexer);
3127 while (cp_parser_is_string_literal (tok));
3129 strs = (cpp_string *) obstack_finish (&str_ob);
3132 if (type != CPP_STRING && !wide_ok)
3134 cp_parser_error (parser, "a wide string is invalid in this context");
3138 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3139 (parse_in, strs, count, &istr, type))
3141 value = build_string (istr.len, (const char *)istr.text);
3142 free (CONST_CAST (unsigned char *, istr.text));
3148 case CPP_UTF8STRING:
3149 TREE_TYPE (value) = char_array_type_node;
3152 TREE_TYPE (value) = char16_array_type_node;
3155 TREE_TYPE (value) = char32_array_type_node;
3158 TREE_TYPE (value) = wchar_array_type_node;
3162 value = fix_string_type (value);
3165 /* cpp_interpret_string has issued an error. */
3166 value = error_mark_node;
3169 obstack_free (&str_ob, 0);
3175 /* Basic concepts [gram.basic] */
3177 /* Parse a translation-unit.
3180 declaration-seq [opt]
3182 Returns TRUE if all went well. */
3185 cp_parser_translation_unit (cp_parser* parser)
3187 /* The address of the first non-permanent object on the declarator
3189 static void *declarator_obstack_base;
3193 /* Create the declarator obstack, if necessary. */
3194 if (!cp_error_declarator)
3196 gcc_obstack_init (&declarator_obstack);
3197 /* Create the error declarator. */
3198 cp_error_declarator = make_declarator (cdk_error);
3199 /* Create the empty parameter list. */
3200 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3201 /* Remember where the base of the declarator obstack lies. */
3202 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3205 cp_parser_declaration_seq_opt (parser);
3207 /* If there are no tokens left then all went well. */
3208 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3210 /* Get rid of the token array; we don't need it any more. */
3211 cp_lexer_destroy (parser->lexer);
3212 parser->lexer = NULL;
3214 /* This file might have been a context that's implicitly extern
3215 "C". If so, pop the lang context. (Only relevant for PCH.) */
3216 if (parser->implicit_extern_c)
3218 pop_lang_context ();
3219 parser->implicit_extern_c = false;
3223 finish_translation_unit ();
3229 cp_parser_error (parser, "expected declaration");
3233 /* Make sure the declarator obstack was fully cleaned up. */
3234 gcc_assert (obstack_next_free (&declarator_obstack)
3235 == declarator_obstack_base);
3237 /* All went well. */
3241 /* Expressions [gram.expr] */
3243 /* Parse a primary-expression.
3254 ( compound-statement )
3255 __builtin_va_arg ( assignment-expression , type-id )
3256 __builtin_offsetof ( type-id , offsetof-expression )
3259 __has_nothrow_assign ( type-id )
3260 __has_nothrow_constructor ( type-id )
3261 __has_nothrow_copy ( type-id )
3262 __has_trivial_assign ( type-id )
3263 __has_trivial_constructor ( type-id )
3264 __has_trivial_copy ( type-id )
3265 __has_trivial_destructor ( type-id )
3266 __has_virtual_destructor ( type-id )
3267 __is_abstract ( type-id )
3268 __is_base_of ( type-id , type-id )
3269 __is_class ( type-id )
3270 __is_convertible_to ( type-id , type-id )
3271 __is_empty ( type-id )
3272 __is_enum ( type-id )
3273 __is_pod ( type-id )
3274 __is_polymorphic ( type-id )
3275 __is_union ( type-id )
3277 Objective-C++ Extension:
3285 ADDRESS_P is true iff this expression was immediately preceded by
3286 "&" and therefore might denote a pointer-to-member. CAST_P is true
3287 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3288 true iff this expression is a template argument.
3290 Returns a representation of the expression. Upon return, *IDK
3291 indicates what kind of id-expression (if any) was present. */
3294 cp_parser_primary_expression (cp_parser *parser,
3297 bool template_arg_p,
3300 cp_token *token = NULL;
3302 /* Assume the primary expression is not an id-expression. */
3303 *idk = CP_ID_KIND_NONE;
3305 /* Peek at the next token. */
3306 token = cp_lexer_peek_token (parser->lexer);
3307 switch (token->type)
3320 token = cp_lexer_consume_token (parser->lexer);
3321 if (TREE_CODE (token->u.value) == FIXED_CST)
3323 error_at (token->location,
3324 "fixed-point types not supported in C++");
3325 return error_mark_node;
3327 /* Floating-point literals are only allowed in an integral
3328 constant expression if they are cast to an integral or
3329 enumeration type. */
3330 if (TREE_CODE (token->u.value) == REAL_CST
3331 && parser->integral_constant_expression_p
3334 /* CAST_P will be set even in invalid code like "int(2.7 +
3335 ...)". Therefore, we have to check that the next token
3336 is sure to end the cast. */
3339 cp_token *next_token;
3341 next_token = cp_lexer_peek_token (parser->lexer);
3342 if (/* The comma at the end of an
3343 enumerator-definition. */
3344 next_token->type != CPP_COMMA
3345 /* The curly brace at the end of an enum-specifier. */
3346 && next_token->type != CPP_CLOSE_BRACE
3347 /* The end of a statement. */
3348 && next_token->type != CPP_SEMICOLON
3349 /* The end of the cast-expression. */
3350 && next_token->type != CPP_CLOSE_PAREN
3351 /* The end of an array bound. */
3352 && next_token->type != CPP_CLOSE_SQUARE
3353 /* The closing ">" in a template-argument-list. */
3354 && (next_token->type != CPP_GREATER
3355 || parser->greater_than_is_operator_p)
3356 /* C++0x only: A ">>" treated like two ">" tokens,
3357 in a template-argument-list. */
3358 && (next_token->type != CPP_RSHIFT
3359 || (cxx_dialect == cxx98)
3360 || parser->greater_than_is_operator_p))
3364 /* If we are within a cast, then the constraint that the
3365 cast is to an integral or enumeration type will be
3366 checked at that point. If we are not within a cast, then
3367 this code is invalid. */
3369 cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3371 return token->u.value;
3377 case CPP_UTF8STRING:
3378 /* ??? Should wide strings be allowed when parser->translate_strings_p
3379 is false (i.e. in attributes)? If not, we can kill the third
3380 argument to cp_parser_string_literal. */
3381 return cp_parser_string_literal (parser,
3382 parser->translate_strings_p,
3385 case CPP_OPEN_PAREN:
3388 bool saved_greater_than_is_operator_p;
3390 /* Consume the `('. */
3391 cp_lexer_consume_token (parser->lexer);
3392 /* Within a parenthesized expression, a `>' token is always
3393 the greater-than operator. */
3394 saved_greater_than_is_operator_p
3395 = parser->greater_than_is_operator_p;
3396 parser->greater_than_is_operator_p = true;
3397 /* If we see `( { ' then we are looking at the beginning of
3398 a GNU statement-expression. */
3399 if (cp_parser_allow_gnu_extensions_p (parser)
3400 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3402 /* Statement-expressions are not allowed by the standard. */
3403 pedwarn (token->location, OPT_pedantic,
3404 "ISO C++ forbids braced-groups within expressions");
3406 /* And they're not allowed outside of a function-body; you
3407 cannot, for example, write:
3409 int i = ({ int j = 3; j + 1; });
3411 at class or namespace scope. */
3412 if (!parser->in_function_body
3413 || parser->in_template_argument_list_p)
3415 error_at (token->location,
3416 "statement-expressions are not allowed outside "
3417 "functions nor in template-argument lists");
3418 cp_parser_skip_to_end_of_block_or_statement (parser);
3419 expr = error_mark_node;
3423 /* Start the statement-expression. */
3424 expr = begin_stmt_expr ();
3425 /* Parse the compound-statement. */
3426 cp_parser_compound_statement (parser, expr, false, false);
3428 expr = finish_stmt_expr (expr, false);
3433 /* Parse the parenthesized expression. */
3434 expr = cp_parser_expression (parser, cast_p, idk);
3435 /* Let the front end know that this expression was
3436 enclosed in parentheses. This matters in case, for
3437 example, the expression is of the form `A::B', since
3438 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3440 finish_parenthesized_expr (expr);
3441 /* DR 705: Wrapping an unqualified name in parentheses
3442 suppresses arg-dependent lookup. We want to pass back
3443 CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3444 (c++/37862), but none of the others. */
3445 if (*idk != CP_ID_KIND_QUALIFIED)
3446 *idk = CP_ID_KIND_NONE;
3448 /* The `>' token might be the end of a template-id or
3449 template-parameter-list now. */
3450 parser->greater_than_is_operator_p
3451 = saved_greater_than_is_operator_p;
3452 /* Consume the `)'. */
3453 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3454 cp_parser_skip_to_end_of_statement (parser);
3459 case CPP_OPEN_SQUARE:
3460 if (c_dialect_objc ())
3461 /* We have an Objective-C++ message. */
3462 return cp_parser_objc_expression (parser);
3464 tree lam = cp_parser_lambda_expression (parser);
3465 /* Don't warn about a failed tentative parse. */
3466 if (cp_parser_error_occurred (parser))
3467 return error_mark_node;
3468 maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3472 case CPP_OBJC_STRING:
3473 if (c_dialect_objc ())
3474 /* We have an Objective-C++ string literal. */
3475 return cp_parser_objc_expression (parser);
3476 cp_parser_error (parser, "expected primary-expression");
3477 return error_mark_node;
3480 switch (token->keyword)
3482 /* These two are the boolean literals. */
3484 cp_lexer_consume_token (parser->lexer);
3485 return boolean_true_node;
3487 cp_lexer_consume_token (parser->lexer);
3488 return boolean_false_node;
3490 /* The `__null' literal. */
3492 cp_lexer_consume_token (parser->lexer);
3495 /* The `nullptr' literal. */
3497 cp_lexer_consume_token (parser->lexer);
3498 return nullptr_node;
3500 /* Recognize the `this' keyword. */
3502 cp_lexer_consume_token (parser->lexer);
3503 if (parser->local_variables_forbidden_p)
3505 error_at (token->location,
3506 "%<this%> may not be used in this context");
3507 return error_mark_node;
3509 /* Pointers cannot appear in constant-expressions. */
3510 if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3511 return error_mark_node;
3512 return finish_this_expr ();
3514 /* The `operator' keyword can be the beginning of an
3519 case RID_FUNCTION_NAME:
3520 case RID_PRETTY_FUNCTION_NAME:
3521 case RID_C99_FUNCTION_NAME:
3523 non_integral_constant name;
3525 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3526 __func__ are the names of variables -- but they are
3527 treated specially. Therefore, they are handled here,
3528 rather than relying on the generic id-expression logic
3529 below. Grammatically, these names are id-expressions.
3531 Consume the token. */
3532 token = cp_lexer_consume_token (parser->lexer);
3534 switch (token->keyword)
3536 case RID_FUNCTION_NAME:
3537 name = NIC_FUNC_NAME;
3539 case RID_PRETTY_FUNCTION_NAME:
3540 name = NIC_PRETTY_FUNC;
3542 case RID_C99_FUNCTION_NAME:
3543 name = NIC_C99_FUNC;
3549 if (cp_parser_non_integral_constant_expression (parser, name))
3550 return error_mark_node;
3552 /* Look up the name. */
3553 return finish_fname (token->u.value);
3561 /* The `__builtin_va_arg' construct is used to handle
3562 `va_arg'. Consume the `__builtin_va_arg' token. */
3563 cp_lexer_consume_token (parser->lexer);
3564 /* Look for the opening `('. */
3565 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3566 /* Now, parse the assignment-expression. */
3567 expression = cp_parser_assignment_expression (parser,
3568 /*cast_p=*/false, NULL);
3569 /* Look for the `,'. */
3570 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3571 /* Parse the type-id. */
3572 type = cp_parser_type_id (parser);
3573 /* Look for the closing `)'. */
3574 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3575 /* Using `va_arg' in a constant-expression is not
3577 if (cp_parser_non_integral_constant_expression (parser,
3579 return error_mark_node;
3580 return build_x_va_arg (expression, type);
3584 return cp_parser_builtin_offsetof (parser);
3586 case RID_HAS_NOTHROW_ASSIGN:
3587 case RID_HAS_NOTHROW_CONSTRUCTOR:
3588 case RID_HAS_NOTHROW_COPY:
3589 case RID_HAS_TRIVIAL_ASSIGN:
3590 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3591 case RID_HAS_TRIVIAL_COPY:
3592 case RID_HAS_TRIVIAL_DESTRUCTOR:
3593 case RID_HAS_VIRTUAL_DESTRUCTOR:
3594 case RID_IS_ABSTRACT:
3595 case RID_IS_BASE_OF:
3597 case RID_IS_CONVERTIBLE_TO:
3601 case RID_IS_POLYMORPHIC:
3602 case RID_IS_STD_LAYOUT:
3603 case RID_IS_TRIVIAL:
3605 case RID_IS_LITERAL_TYPE:
3606 return cp_parser_trait_expr (parser, token->keyword);
3608 /* Objective-C++ expressions. */
3610 case RID_AT_PROTOCOL:
3611 case RID_AT_SELECTOR:
3612 return cp_parser_objc_expression (parser);
3615 if (parser->in_function_body
3616 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3619 error_at (token->location,
3620 "a template declaration cannot appear at block scope");
3621 cp_parser_skip_to_end_of_block_or_statement (parser);
3622 return error_mark_node;
3625 cp_parser_error (parser, "expected primary-expression");
3626 return error_mark_node;
3629 /* An id-expression can start with either an identifier, a
3630 `::' as the beginning of a qualified-id, or the "operator"
3634 case CPP_TEMPLATE_ID:
3635 case CPP_NESTED_NAME_SPECIFIER:
3639 const char *error_msg;
3642 cp_token *id_expr_token;
3645 /* Parse the id-expression. */
3647 = cp_parser_id_expression (parser,
3648 /*template_keyword_p=*/false,
3649 /*check_dependency_p=*/true,
3651 /*declarator_p=*/false,
3652 /*optional_p=*/false);
3653 if (id_expression == error_mark_node)
3654 return error_mark_node;
3655 id_expr_token = token;
3656 token = cp_lexer_peek_token (parser->lexer);
3657 done = (token->type != CPP_OPEN_SQUARE
3658 && token->type != CPP_OPEN_PAREN
3659 && token->type != CPP_DOT
3660 && token->type != CPP_DEREF
3661 && token->type != CPP_PLUS_PLUS
3662 && token->type != CPP_MINUS_MINUS);
3663 /* If we have a template-id, then no further lookup is
3664 required. If the template-id was for a template-class, we
3665 will sometimes have a TYPE_DECL at this point. */
3666 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3667 || TREE_CODE (id_expression) == TYPE_DECL)
3668 decl = id_expression;
3669 /* Look up the name. */
3672 tree ambiguous_decls;
3674 /* If we already know that this lookup is ambiguous, then
3675 we've already issued an error message; there's no reason
3677 if (id_expr_token->type == CPP_NAME
3678 && id_expr_token->ambiguous_p)
3680 cp_parser_simulate_error (parser);
3681 return error_mark_node;
3684 decl = cp_parser_lookup_name (parser, id_expression,
3687 /*is_namespace=*/false,
3688 /*check_dependency=*/true,
3690 id_expr_token->location);
3691 /* If the lookup was ambiguous, an error will already have
3693 if (ambiguous_decls)
3694 return error_mark_node;
3696 /* In Objective-C++, we may have an Objective-C 2.0
3697 dot-syntax for classes here. */
3698 if (c_dialect_objc ()
3699 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3700 && TREE_CODE (decl) == TYPE_DECL
3701 && objc_is_class_name (decl))
3704 cp_lexer_consume_token (parser->lexer);
3705 component = cp_parser_identifier (parser);
3706 if (component == error_mark_node)
3707 return error_mark_node;
3709 return objc_build_class_component_ref (id_expression, component);
3712 /* In Objective-C++, an instance variable (ivar) may be preferred
3713 to whatever cp_parser_lookup_name() found. */
3714 decl = objc_lookup_ivar (decl, id_expression);
3716 /* If name lookup gives us a SCOPE_REF, then the
3717 qualifying scope was dependent. */
3718 if (TREE_CODE (decl) == SCOPE_REF)
3720 /* At this point, we do not know if DECL is a valid
3721 integral constant expression. We assume that it is
3722 in fact such an expression, so that code like:
3724 template <int N> struct A {
3728 is accepted. At template-instantiation time, we
3729 will check that B<N>::i is actually a constant. */
3732 /* Check to see if DECL is a local variable in a context
3733 where that is forbidden. */
3734 if (parser->local_variables_forbidden_p
3735 && local_variable_p (decl))
3737 /* It might be that we only found DECL because we are
3738 trying to be generous with pre-ISO scoping rules.
3739 For example, consider:
3743 for (int i = 0; i < 10; ++i) {}
3744 extern void f(int j = i);
3747 Here, name look up will originally find the out
3748 of scope `i'. We need to issue a warning message,
3749 but then use the global `i'. */
3750 decl = check_for_out_of_scope_variable (decl);
3751 if (local_variable_p (decl))
3753 error_at (id_expr_token->location,
3754 "local variable %qD may not appear in this context",
3756 return error_mark_node;
3761 decl = (finish_id_expression
3762 (id_expression, decl, parser->scope,
3764 parser->integral_constant_expression_p,
3765 parser->allow_non_integral_constant_expression_p,
3766 &parser->non_integral_constant_expression_p,
3767 template_p, done, address_p,
3770 id_expr_token->location));
3772 cp_parser_error (parser, error_msg);
3776 /* Anything else is an error. */
3778 cp_parser_error (parser, "expected primary-expression");
3779 return error_mark_node;
3783 /* Parse an id-expression.
3790 :: [opt] nested-name-specifier template [opt] unqualified-id
3792 :: operator-function-id
3795 Return a representation of the unqualified portion of the
3796 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3797 a `::' or nested-name-specifier.
3799 Often, if the id-expression was a qualified-id, the caller will
3800 want to make a SCOPE_REF to represent the qualified-id. This
3801 function does not do this in order to avoid wastefully creating
3802 SCOPE_REFs when they are not required.
3804 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3807 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3808 uninstantiated templates.
3810 If *TEMPLATE_P is non-NULL, it is set to true iff the
3811 `template' keyword is used to explicitly indicate that the entity
3812 named is a template.
3814 If DECLARATOR_P is true, the id-expression is appearing as part of
3815 a declarator, rather than as part of an expression. */
3818 cp_parser_id_expression (cp_parser *parser,
3819 bool template_keyword_p,
3820 bool check_dependency_p,
3825 bool global_scope_p;
3826 bool nested_name_specifier_p;
3828 /* Assume the `template' keyword was not used. */
3830 *template_p = template_keyword_p;
3832 /* Look for the optional `::' operator. */
3834 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3836 /* Look for the optional nested-name-specifier. */
3837 nested_name_specifier_p
3838 = (cp_parser_nested_name_specifier_opt (parser,
3839 /*typename_keyword_p=*/false,
3844 /* If there is a nested-name-specifier, then we are looking at
3845 the first qualified-id production. */
3846 if (nested_name_specifier_p)
3849 tree saved_object_scope;
3850 tree saved_qualifying_scope;
3851 tree unqualified_id;
3854 /* See if the next token is the `template' keyword. */
3856 template_p = &is_template;
3857 *template_p = cp_parser_optional_template_keyword (parser);
3858 /* Name lookup we do during the processing of the
3859 unqualified-id might obliterate SCOPE. */
3860 saved_scope = parser->scope;
3861 saved_object_scope = parser->object_scope;
3862 saved_qualifying_scope = parser->qualifying_scope;
3863 /* Process the final unqualified-id. */
3864 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3867 /*optional_p=*/false);
3868 /* Restore the SAVED_SCOPE for our caller. */
3869 parser->scope = saved_scope;
3870 parser->object_scope = saved_object_scope;
3871 parser->qualifying_scope = saved_qualifying_scope;
3873 return unqualified_id;
3875 /* Otherwise, if we are in global scope, then we are looking at one
3876 of the other qualified-id productions. */
3877 else if (global_scope_p)
3882 /* Peek at the next token. */
3883 token = cp_lexer_peek_token (parser->lexer);
3885 /* If it's an identifier, and the next token is not a "<", then
3886 we can avoid the template-id case. This is an optimization
3887 for this common case. */
3888 if (token->type == CPP_NAME
3889 && !cp_parser_nth_token_starts_template_argument_list_p
3891 return cp_parser_identifier (parser);
3893 cp_parser_parse_tentatively (parser);
3894 /* Try a template-id. */
3895 id = cp_parser_template_id (parser,
3896 /*template_keyword_p=*/false,
3897 /*check_dependency_p=*/true,
3899 /* If that worked, we're done. */
3900 if (cp_parser_parse_definitely (parser))
3903 /* Peek at the next token. (Changes in the token buffer may
3904 have invalidated the pointer obtained above.) */
3905 token = cp_lexer_peek_token (parser->lexer);
3907 switch (token->type)
3910 return cp_parser_identifier (parser);
3913 if (token->keyword == RID_OPERATOR)
3914 return cp_parser_operator_function_id (parser);
3918 cp_parser_error (parser, "expected id-expression");
3919 return error_mark_node;
3923 return cp_parser_unqualified_id (parser, template_keyword_p,
3924 /*check_dependency_p=*/true,
3929 /* Parse an unqualified-id.
3933 operator-function-id
3934 conversion-function-id
3938 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3939 keyword, in a construct like `A::template ...'.
3941 Returns a representation of unqualified-id. For the `identifier'
3942 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3943 production a BIT_NOT_EXPR is returned; the operand of the
3944 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3945 other productions, see the documentation accompanying the
3946 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3947 names are looked up in uninstantiated templates. If DECLARATOR_P
3948 is true, the unqualified-id is appearing as part of a declarator,
3949 rather than as part of an expression. */
3952 cp_parser_unqualified_id (cp_parser* parser,
3953 bool template_keyword_p,
3954 bool check_dependency_p,
3960 /* Peek at the next token. */
3961 token = cp_lexer_peek_token (parser->lexer);
3963 switch (token->type)
3969 /* We don't know yet whether or not this will be a
3971 cp_parser_parse_tentatively (parser);
3972 /* Try a template-id. */
3973 id = cp_parser_template_id (parser, template_keyword_p,
3976 /* If it worked, we're done. */
3977 if (cp_parser_parse_definitely (parser))
3979 /* Otherwise, it's an ordinary identifier. */
3980 return cp_parser_identifier (parser);
3983 case CPP_TEMPLATE_ID:
3984 return cp_parser_template_id (parser, template_keyword_p,
3991 tree qualifying_scope;
3996 /* Consume the `~' token. */
3997 cp_lexer_consume_token (parser->lexer);
3998 /* Parse the class-name. The standard, as written, seems to
4001 template <typename T> struct S { ~S (); };
4002 template <typename T> S<T>::~S() {}
4004 is invalid, since `~' must be followed by a class-name, but
4005 `S<T>' is dependent, and so not known to be a class.
4006 That's not right; we need to look in uninstantiated
4007 templates. A further complication arises from:
4009 template <typename T> void f(T t) {
4013 Here, it is not possible to look up `T' in the scope of `T'
4014 itself. We must look in both the current scope, and the
4015 scope of the containing complete expression.
4017 Yet another issue is:
4026 The standard does not seem to say that the `S' in `~S'
4027 should refer to the type `S' and not the data member
4030 /* DR 244 says that we look up the name after the "~" in the
4031 same scope as we looked up the qualifying name. That idea
4032 isn't fully worked out; it's more complicated than that. */
4033 scope = parser->scope;
4034 object_scope = parser->object_scope;
4035 qualifying_scope = parser->qualifying_scope;
4037 /* Check for invalid scopes. */
4038 if (scope == error_mark_node)
4040 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4041 cp_lexer_consume_token (parser->lexer);
4042 return error_mark_node;
4044 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4046 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4047 error_at (token->location,
4048 "scope %qT before %<~%> is not a class-name",
4050 cp_parser_simulate_error (parser);
4051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4052 cp_lexer_consume_token (parser->lexer);
4053 return error_mark_node;
4055 gcc_assert (!scope || TYPE_P (scope));
4057 /* If the name is of the form "X::~X" it's OK even if X is a
4059 token = cp_lexer_peek_token (parser->lexer);
4061 && token->type == CPP_NAME
4062 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4064 && (token->u.value == TYPE_IDENTIFIER (scope)
4065 || constructor_name_p (token->u.value, scope)))
4067 cp_lexer_consume_token (parser->lexer);
4068 return build_nt (BIT_NOT_EXPR, scope);
4071 /* If there was an explicit qualification (S::~T), first look
4072 in the scope given by the qualification (i.e., S).
4074 Note: in the calls to cp_parser_class_name below we pass
4075 typename_type so that lookup finds the injected-class-name
4076 rather than the constructor. */
4078 type_decl = NULL_TREE;
4081 cp_parser_parse_tentatively (parser);
4082 type_decl = cp_parser_class_name (parser,
4083 /*typename_keyword_p=*/false,
4084 /*template_keyword_p=*/false,
4086 /*check_dependency=*/false,
4087 /*class_head_p=*/false,
4089 if (cp_parser_parse_definitely (parser))
4092 /* In "N::S::~S", look in "N" as well. */
4093 if (!done && scope && qualifying_scope)
4095 cp_parser_parse_tentatively (parser);
4096 parser->scope = qualifying_scope;
4097 parser->object_scope = NULL_TREE;
4098 parser->qualifying_scope = NULL_TREE;
4100 = cp_parser_class_name (parser,
4101 /*typename_keyword_p=*/false,
4102 /*template_keyword_p=*/false,
4104 /*check_dependency=*/false,
4105 /*class_head_p=*/false,
4107 if (cp_parser_parse_definitely (parser))
4110 /* In "p->S::~T", look in the scope given by "*p" as well. */
4111 else if (!done && object_scope)
4113 cp_parser_parse_tentatively (parser);
4114 parser->scope = object_scope;
4115 parser->object_scope = NULL_TREE;
4116 parser->qualifying_scope = NULL_TREE;
4118 = cp_parser_class_name (parser,
4119 /*typename_keyword_p=*/false,
4120 /*template_keyword_p=*/false,
4122 /*check_dependency=*/false,
4123 /*class_head_p=*/false,
4125 if (cp_parser_parse_definitely (parser))
4128 /* Look in the surrounding context. */
4131 parser->scope = NULL_TREE;
4132 parser->object_scope = NULL_TREE;
4133 parser->qualifying_scope = NULL_TREE;
4134 if (processing_template_decl)
4135 cp_parser_parse_tentatively (parser);
4137 = cp_parser_class_name (parser,
4138 /*typename_keyword_p=*/false,
4139 /*template_keyword_p=*/false,
4141 /*check_dependency=*/false,
4142 /*class_head_p=*/false,
4144 if (processing_template_decl
4145 && ! cp_parser_parse_definitely (parser))
4147 /* We couldn't find a type with this name, so just accept
4148 it and check for a match at instantiation time. */
4149 type_decl = cp_parser_identifier (parser);
4150 if (type_decl != error_mark_node)
4151 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4155 /* If an error occurred, assume that the name of the
4156 destructor is the same as the name of the qualifying
4157 class. That allows us to keep parsing after running
4158 into ill-formed destructor names. */
4159 if (type_decl == error_mark_node && scope)
4160 return build_nt (BIT_NOT_EXPR, scope);
4161 else if (type_decl == error_mark_node)
4162 return error_mark_node;
4164 /* Check that destructor name and scope match. */
4165 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4167 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4168 error_at (token->location,
4169 "declaration of %<~%T%> as member of %qT",
4171 cp_parser_simulate_error (parser);
4172 return error_mark_node;
4177 A typedef-name that names a class shall not be used as the
4178 identifier in the declarator for a destructor declaration. */
4180 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4181 && !DECL_SELF_REFERENCE_P (type_decl)
4182 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4183 error_at (token->location,
4184 "typedef-name %qD used as destructor declarator",
4187 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4191 if (token->keyword == RID_OPERATOR)
4195 /* This could be a template-id, so we try that first. */
4196 cp_parser_parse_tentatively (parser);
4197 /* Try a template-id. */
4198 id = cp_parser_template_id (parser, template_keyword_p,
4199 /*check_dependency_p=*/true,
4201 /* If that worked, we're done. */
4202 if (cp_parser_parse_definitely (parser))
4204 /* We still don't know whether we're looking at an
4205 operator-function-id or a conversion-function-id. */
4206 cp_parser_parse_tentatively (parser);
4207 /* Try an operator-function-id. */
4208 id = cp_parser_operator_function_id (parser);
4209 /* If that didn't work, try a conversion-function-id. */
4210 if (!cp_parser_parse_definitely (parser))
4211 id = cp_parser_conversion_function_id (parser);
4220 cp_parser_error (parser, "expected unqualified-id");
4221 return error_mark_node;
4225 /* Parse an (optional) nested-name-specifier.
4227 nested-name-specifier: [C++98]
4228 class-or-namespace-name :: nested-name-specifier [opt]
4229 class-or-namespace-name :: template nested-name-specifier [opt]
4231 nested-name-specifier: [C++0x]
4234 nested-name-specifier identifier ::
4235 nested-name-specifier template [opt] simple-template-id ::
4237 PARSER->SCOPE should be set appropriately before this function is
4238 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4239 effect. TYPE_P is TRUE if we non-type bindings should be ignored
4242 Sets PARSER->SCOPE to the class (TYPE) or namespace
4243 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4244 it unchanged if there is no nested-name-specifier. Returns the new
4245 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4247 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4248 part of a declaration and/or decl-specifier. */
4251 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4252 bool typename_keyword_p,
4253 bool check_dependency_p,
4255 bool is_declaration)
4257 bool success = false;
4258 cp_token_position start = 0;
4261 /* Remember where the nested-name-specifier starts. */
4262 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4264 start = cp_lexer_token_position (parser->lexer, false);
4265 push_deferring_access_checks (dk_deferred);
4272 tree saved_qualifying_scope;
4273 bool template_keyword_p;
4275 /* Spot cases that cannot be the beginning of a
4276 nested-name-specifier. */
4277 token = cp_lexer_peek_token (parser->lexer);
4279 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4280 the already parsed nested-name-specifier. */
4281 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4283 /* Grab the nested-name-specifier and continue the loop. */
4284 cp_parser_pre_parsed_nested_name_specifier (parser);
4285 /* If we originally encountered this nested-name-specifier
4286 with IS_DECLARATION set to false, we will not have
4287 resolved TYPENAME_TYPEs, so we must do so here. */
4289 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4291 new_scope = resolve_typename_type (parser->scope,
4292 /*only_current_p=*/false);
4293 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4294 parser->scope = new_scope;
4300 /* Spot cases that cannot be the beginning of a
4301 nested-name-specifier. On the second and subsequent times
4302 through the loop, we look for the `template' keyword. */
4303 if (success && token->keyword == RID_TEMPLATE)
4305 /* A template-id can start a nested-name-specifier. */
4306 else if (token->type == CPP_TEMPLATE_ID)
4310 /* If the next token is not an identifier, then it is
4311 definitely not a type-name or namespace-name. */
4312 if (token->type != CPP_NAME)
4314 /* If the following token is neither a `<' (to begin a
4315 template-id), nor a `::', then we are not looking at a
4316 nested-name-specifier. */
4317 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4319 if (token->type == CPP_COLON
4320 && parser->colon_corrects_to_scope_p
4321 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4323 error_at (token->location,
4324 "found %<:%> in nested-name-specifier, expected %<::%>");
4325 token->type = CPP_SCOPE;
4328 if (token->type != CPP_SCOPE
4329 && !cp_parser_nth_token_starts_template_argument_list_p
4334 /* The nested-name-specifier is optional, so we parse
4336 cp_parser_parse_tentatively (parser);
4338 /* Look for the optional `template' keyword, if this isn't the
4339 first time through the loop. */
4341 template_keyword_p = cp_parser_optional_template_keyword (parser);
4343 template_keyword_p = false;
4345 /* Save the old scope since the name lookup we are about to do
4346 might destroy it. */
4347 old_scope = parser->scope;
4348 saved_qualifying_scope = parser->qualifying_scope;
4349 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4350 look up names in "X<T>::I" in order to determine that "Y" is
4351 a template. So, if we have a typename at this point, we make
4352 an effort to look through it. */
4354 && !typename_keyword_p
4356 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4357 parser->scope = resolve_typename_type (parser->scope,
4358 /*only_current_p=*/false);
4359 /* Parse the qualifying entity. */
4361 = cp_parser_qualifying_entity (parser,
4367 /* Look for the `::' token. */
4368 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4370 /* If we found what we wanted, we keep going; otherwise, we're
4372 if (!cp_parser_parse_definitely (parser))
4374 bool error_p = false;
4376 /* Restore the OLD_SCOPE since it was valid before the
4377 failed attempt at finding the last
4378 class-or-namespace-name. */
4379 parser->scope = old_scope;
4380 parser->qualifying_scope = saved_qualifying_scope;
4381 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4383 /* If the next token is an identifier, and the one after
4384 that is a `::', then any valid interpretation would have
4385 found a class-or-namespace-name. */
4386 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4387 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4389 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4392 token = cp_lexer_consume_token (parser->lexer);
4395 if (!token->ambiguous_p)
4398 tree ambiguous_decls;
4400 decl = cp_parser_lookup_name (parser, token->u.value,
4402 /*is_template=*/false,
4403 /*is_namespace=*/false,
4404 /*check_dependency=*/true,
4407 if (TREE_CODE (decl) == TEMPLATE_DECL)
4408 error_at (token->location,
4409 "%qD used without template parameters",
4411 else if (ambiguous_decls)
4413 error_at (token->location,
4414 "reference to %qD is ambiguous",
4416 print_candidates (ambiguous_decls);
4417 decl = error_mark_node;
4421 if (cxx_dialect != cxx98)
4422 cp_parser_name_lookup_error
4423 (parser, token->u.value, decl, NLE_NOT_CXX98,
4426 cp_parser_name_lookup_error
4427 (parser, token->u.value, decl, NLE_CXX98,
4431 parser->scope = error_mark_node;
4433 /* Treat this as a successful nested-name-specifier
4438 If the name found is not a class-name (clause
4439 _class_) or namespace-name (_namespace.def_), the
4440 program is ill-formed. */
4443 cp_lexer_consume_token (parser->lexer);
4447 /* We've found one valid nested-name-specifier. */
4449 /* Name lookup always gives us a DECL. */
4450 if (TREE_CODE (new_scope) == TYPE_DECL)
4451 new_scope = TREE_TYPE (new_scope);
4452 /* Uses of "template" must be followed by actual templates. */
4453 if (template_keyword_p
4454 && !(CLASS_TYPE_P (new_scope)
4455 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4456 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4457 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4458 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4459 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4460 == TEMPLATE_ID_EXPR)))
4461 permerror (input_location, TYPE_P (new_scope)
4462 ? "%qT is not a template"
4463 : "%qD is not a template",
4465 /* If it is a class scope, try to complete it; we are about to
4466 be looking up names inside the class. */
4467 if (TYPE_P (new_scope)
4468 /* Since checking types for dependency can be expensive,
4469 avoid doing it if the type is already complete. */
4470 && !COMPLETE_TYPE_P (new_scope)
4471 /* Do not try to complete dependent types. */
4472 && !dependent_type_p (new_scope))
4474 new_scope = complete_type (new_scope);
4475 /* If it is a typedef to current class, use the current
4476 class instead, as the typedef won't have any names inside
4478 if (!COMPLETE_TYPE_P (new_scope)
4479 && currently_open_class (new_scope))
4480 new_scope = TYPE_MAIN_VARIANT (new_scope);
4482 /* Make sure we look in the right scope the next time through
4484 parser->scope = new_scope;
4487 /* If parsing tentatively, replace the sequence of tokens that makes
4488 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4489 token. That way, should we re-parse the token stream, we will
4490 not have to repeat the effort required to do the parse, nor will
4491 we issue duplicate error messages. */
4492 if (success && start)
4496 token = cp_lexer_token_at (parser->lexer, start);
4497 /* Reset the contents of the START token. */
4498 token->type = CPP_NESTED_NAME_SPECIFIER;
4499 /* Retrieve any deferred checks. Do not pop this access checks yet
4500 so the memory will not be reclaimed during token replacing below. */
4501 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4502 token->u.tree_check_value->value = parser->scope;
4503 token->u.tree_check_value->checks = get_deferred_access_checks ();
4504 token->u.tree_check_value->qualifying_scope =
4505 parser->qualifying_scope;
4506 token->keyword = RID_MAX;
4508 /* Purge all subsequent tokens. */
4509 cp_lexer_purge_tokens_after (parser->lexer, start);
4513 pop_to_parent_deferring_access_checks ();
4515 return success ? parser->scope : NULL_TREE;
4518 /* Parse a nested-name-specifier. See
4519 cp_parser_nested_name_specifier_opt for details. This function
4520 behaves identically, except that it will an issue an error if no
4521 nested-name-specifier is present. */
4524 cp_parser_nested_name_specifier (cp_parser *parser,
4525 bool typename_keyword_p,
4526 bool check_dependency_p,
4528 bool is_declaration)
4532 /* Look for the nested-name-specifier. */
4533 scope = cp_parser_nested_name_specifier_opt (parser,
4538 /* If it was not present, issue an error message. */
4541 cp_parser_error (parser, "expected nested-name-specifier");
4542 parser->scope = NULL_TREE;
4548 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4549 this is either a class-name or a namespace-name (which corresponds
4550 to the class-or-namespace-name production in the grammar). For
4551 C++0x, it can also be a type-name that refers to an enumeration
4554 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4555 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4556 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4557 TYPE_P is TRUE iff the next name should be taken as a class-name,
4558 even the same name is declared to be another entity in the same
4561 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4562 specified by the class-or-namespace-name. If neither is found the
4563 ERROR_MARK_NODE is returned. */
4566 cp_parser_qualifying_entity (cp_parser *parser,
4567 bool typename_keyword_p,
4568 bool template_keyword_p,
4569 bool check_dependency_p,
4571 bool is_declaration)
4574 tree saved_qualifying_scope;
4575 tree saved_object_scope;
4578 bool successful_parse_p;
4580 /* Before we try to parse the class-name, we must save away the
4581 current PARSER->SCOPE since cp_parser_class_name will destroy
4583 saved_scope = parser->scope;
4584 saved_qualifying_scope = parser->qualifying_scope;
4585 saved_object_scope = parser->object_scope;
4586 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4587 there is no need to look for a namespace-name. */
4588 only_class_p = template_keyword_p
4589 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4591 cp_parser_parse_tentatively (parser);
4592 scope = cp_parser_class_name (parser,
4595 type_p ? class_type : none_type,
4597 /*class_head_p=*/false,
4599 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4600 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4602 && cxx_dialect != cxx98
4603 && !successful_parse_p)
4605 /* Restore the saved scope. */
4606 parser->scope = saved_scope;
4607 parser->qualifying_scope = saved_qualifying_scope;
4608 parser->object_scope = saved_object_scope;
4610 /* Parse tentatively. */
4611 cp_parser_parse_tentatively (parser);
4613 /* Parse a typedef-name or enum-name. */
4614 scope = cp_parser_nonclass_name (parser);
4616 /* "If the name found does not designate a namespace or a class,
4617 enumeration, or dependent type, the program is ill-formed."
4619 We cover classes and dependent types above and namespaces below,
4620 so this code is only looking for enums. */
4621 if (!scope || TREE_CODE (scope) != TYPE_DECL
4622 || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4623 cp_parser_simulate_error (parser);
4625 successful_parse_p = cp_parser_parse_definitely (parser);
4627 /* If that didn't work, try for a namespace-name. */
4628 if (!only_class_p && !successful_parse_p)
4630 /* Restore the saved scope. */
4631 parser->scope = saved_scope;
4632 parser->qualifying_scope = saved_qualifying_scope;
4633 parser->object_scope = saved_object_scope;
4634 /* If we are not looking at an identifier followed by the scope
4635 resolution operator, then this is not part of a
4636 nested-name-specifier. (Note that this function is only used
4637 to parse the components of a nested-name-specifier.) */
4638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4639 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4640 return error_mark_node;
4641 scope = cp_parser_namespace_name (parser);
4647 /* Parse a postfix-expression.
4651 postfix-expression [ expression ]
4652 postfix-expression ( expression-list [opt] )
4653 simple-type-specifier ( expression-list [opt] )
4654 typename :: [opt] nested-name-specifier identifier
4655 ( expression-list [opt] )
4656 typename :: [opt] nested-name-specifier template [opt] template-id
4657 ( expression-list [opt] )
4658 postfix-expression . template [opt] id-expression
4659 postfix-expression -> template [opt] id-expression
4660 postfix-expression . pseudo-destructor-name
4661 postfix-expression -> pseudo-destructor-name
4662 postfix-expression ++
4663 postfix-expression --
4664 dynamic_cast < type-id > ( expression )
4665 static_cast < type-id > ( expression )
4666 reinterpret_cast < type-id > ( expression )
4667 const_cast < type-id > ( expression )
4668 typeid ( expression )
4674 ( type-id ) { initializer-list , [opt] }
4676 This extension is a GNU version of the C99 compound-literal
4677 construct. (The C99 grammar uses `type-name' instead of `type-id',
4678 but they are essentially the same concept.)
4680 If ADDRESS_P is true, the postfix expression is the operand of the
4681 `&' operator. CAST_P is true if this expression is the target of a
4684 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4685 class member access expressions [expr.ref].
4687 Returns a representation of the expression. */
4690 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4691 bool member_access_only_p,
4692 cp_id_kind * pidk_return)
4696 cp_id_kind idk = CP_ID_KIND_NONE;
4697 tree postfix_expression = NULL_TREE;
4698 bool is_member_access = false;
4700 /* Peek at the next token. */
4701 token = cp_lexer_peek_token (parser->lexer);
4702 /* Some of the productions are determined by keywords. */
4703 keyword = token->keyword;
4713 const char *saved_message;
4715 /* All of these can be handled in the same way from the point
4716 of view of parsing. Begin by consuming the token
4717 identifying the cast. */
4718 cp_lexer_consume_token (parser->lexer);
4720 /* New types cannot be defined in the cast. */
4721 saved_message = parser->type_definition_forbidden_message;
4722 parser->type_definition_forbidden_message
4723 = G_("types may not be defined in casts");
4725 /* Look for the opening `<'. */
4726 cp_parser_require (parser, CPP_LESS, RT_LESS);
4727 /* Parse the type to which we are casting. */
4728 type = cp_parser_type_id (parser);
4729 /* Look for the closing `>'. */
4730 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4731 /* Restore the old message. */
4732 parser->type_definition_forbidden_message = saved_message;
4734 /* And the expression which is being cast. */
4735 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4736 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4737 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4739 /* Only type conversions to integral or enumeration types
4740 can be used in constant-expressions. */
4741 if (!cast_valid_in_integral_constant_expression_p (type)
4742 && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4743 return error_mark_node;
4749 = build_dynamic_cast (type, expression, tf_warning_or_error);
4753 = build_static_cast (type, expression, tf_warning_or_error);
4757 = build_reinterpret_cast (type, expression,
4758 tf_warning_or_error);
4762 = build_const_cast (type, expression, tf_warning_or_error);
4773 const char *saved_message;
4774 bool saved_in_type_id_in_expr_p;
4776 /* Consume the `typeid' token. */
4777 cp_lexer_consume_token (parser->lexer);
4778 /* Look for the `(' token. */
4779 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4780 /* Types cannot be defined in a `typeid' expression. */
4781 saved_message = parser->type_definition_forbidden_message;
4782 parser->type_definition_forbidden_message
4783 = G_("types may not be defined in a %<typeid%> expression");
4784 /* We can't be sure yet whether we're looking at a type-id or an
4786 cp_parser_parse_tentatively (parser);
4787 /* Try a type-id first. */
4788 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4789 parser->in_type_id_in_expr_p = true;
4790 type = cp_parser_type_id (parser);
4791 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4792 /* Look for the `)' token. Otherwise, we can't be sure that
4793 we're not looking at an expression: consider `typeid (int
4794 (3))', for example. */
4795 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4796 /* If all went well, simply lookup the type-id. */
4797 if (cp_parser_parse_definitely (parser))
4798 postfix_expression = get_typeid (type);
4799 /* Otherwise, fall back to the expression variant. */
4804 /* Look for an expression. */
4805 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4806 /* Compute its typeid. */
4807 postfix_expression = build_typeid (expression);
4808 /* Look for the `)' token. */
4809 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4811 /* Restore the saved message. */
4812 parser->type_definition_forbidden_message = saved_message;
4813 /* `typeid' may not appear in an integral constant expression. */
4814 if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4815 return error_mark_node;
4822 /* The syntax permitted here is the same permitted for an
4823 elaborated-type-specifier. */
4824 type = cp_parser_elaborated_type_specifier (parser,
4825 /*is_friend=*/false,
4826 /*is_declaration=*/false);
4827 postfix_expression = cp_parser_functional_cast (parser, type);
4835 /* If the next thing is a simple-type-specifier, we may be
4836 looking at a functional cast. We could also be looking at
4837 an id-expression. So, we try the functional cast, and if
4838 that doesn't work we fall back to the primary-expression. */
4839 cp_parser_parse_tentatively (parser);
4840 /* Look for the simple-type-specifier. */
4841 type = cp_parser_simple_type_specifier (parser,
4842 /*decl_specs=*/NULL,
4843 CP_PARSER_FLAGS_NONE);
4844 /* Parse the cast itself. */
4845 if (!cp_parser_error_occurred (parser))
4847 = cp_parser_functional_cast (parser, type);
4848 /* If that worked, we're done. */
4849 if (cp_parser_parse_definitely (parser))
4852 /* If the functional-cast didn't work out, try a
4853 compound-literal. */
4854 if (cp_parser_allow_gnu_extensions_p (parser)
4855 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4857 VEC(constructor_elt,gc) *initializer_list = NULL;
4858 bool saved_in_type_id_in_expr_p;
4860 cp_parser_parse_tentatively (parser);
4861 /* Consume the `('. */
4862 cp_lexer_consume_token (parser->lexer);
4863 /* Parse the type. */
4864 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4865 parser->in_type_id_in_expr_p = true;
4866 type = cp_parser_type_id (parser);
4867 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4868 /* Look for the `)'. */
4869 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4870 /* Look for the `{'. */
4871 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4872 /* If things aren't going well, there's no need to
4874 if (!cp_parser_error_occurred (parser))
4876 bool non_constant_p;
4877 /* Parse the initializer-list. */
4879 = cp_parser_initializer_list (parser, &non_constant_p);
4880 /* Allow a trailing `,'. */
4881 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4882 cp_lexer_consume_token (parser->lexer);
4883 /* Look for the final `}'. */
4884 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4886 /* If that worked, we're definitely looking at a
4887 compound-literal expression. */
4888 if (cp_parser_parse_definitely (parser))
4890 /* Warn the user that a compound literal is not
4891 allowed in standard C++. */
4892 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4893 /* For simplicity, we disallow compound literals in
4894 constant-expressions. We could
4895 allow compound literals of integer type, whose
4896 initializer was a constant, in constant
4897 expressions. Permitting that usage, as a further
4898 extension, would not change the meaning of any
4899 currently accepted programs. (Of course, as
4900 compound literals are not part of ISO C++, the
4901 standard has nothing to say.) */
4902 if (cp_parser_non_integral_constant_expression (parser,
4905 postfix_expression = error_mark_node;
4908 /* Form the representation of the compound-literal. */
4910 = (finish_compound_literal
4911 (type, build_constructor (init_list_type_node,
4913 tf_warning_or_error));
4918 /* It must be a primary-expression. */
4920 = cp_parser_primary_expression (parser, address_p, cast_p,
4921 /*template_arg_p=*/false,
4927 /* Keep looping until the postfix-expression is complete. */
4930 if (idk == CP_ID_KIND_UNQUALIFIED
4931 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4932 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4933 /* It is not a Koenig lookup function call. */
4935 = unqualified_name_lookup_error (postfix_expression);
4937 /* Peek at the next token. */
4938 token = cp_lexer_peek_token (parser->lexer);
4940 switch (token->type)
4942 case CPP_OPEN_SQUARE:
4944 = cp_parser_postfix_open_square_expression (parser,
4947 idk = CP_ID_KIND_NONE;
4948 is_member_access = false;
4951 case CPP_OPEN_PAREN:
4952 /* postfix-expression ( expression-list [opt] ) */
4955 bool is_builtin_constant_p;
4956 bool saved_integral_constant_expression_p = false;
4957 bool saved_non_integral_constant_expression_p = false;
4960 is_member_access = false;
4962 is_builtin_constant_p
4963 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4964 if (is_builtin_constant_p)
4966 /* The whole point of __builtin_constant_p is to allow
4967 non-constant expressions to appear as arguments. */
4968 saved_integral_constant_expression_p
4969 = parser->integral_constant_expression_p;
4970 saved_non_integral_constant_expression_p
4971 = parser->non_integral_constant_expression_p;
4972 parser->integral_constant_expression_p = false;
4974 args = (cp_parser_parenthesized_expression_list
4976 /*cast_p=*/false, /*allow_expansion_p=*/true,
4977 /*non_constant_p=*/NULL));
4978 if (is_builtin_constant_p)
4980 parser->integral_constant_expression_p
4981 = saved_integral_constant_expression_p;
4982 parser->non_integral_constant_expression_p
4983 = saved_non_integral_constant_expression_p;
4988 postfix_expression = error_mark_node;
4992 /* Function calls are not permitted in
4993 constant-expressions. */
4994 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4995 && cp_parser_non_integral_constant_expression (parser,
4998 postfix_expression = error_mark_node;
4999 release_tree_vector (args);
5004 if (idk == CP_ID_KIND_UNQUALIFIED
5005 || idk == CP_ID_KIND_TEMPLATE_ID)
5007 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5009 if (!VEC_empty (tree, args))
5012 if (!any_type_dependent_arguments_p (args))
5014 = perform_koenig_lookup (postfix_expression, args,
5015 /*include_std=*/false);
5019 = unqualified_fn_lookup_error (postfix_expression);
5021 /* We do not perform argument-dependent lookup if
5022 normal lookup finds a non-function, in accordance
5023 with the expected resolution of DR 218. */
5024 else if (!VEC_empty (tree, args)
5025 && is_overloaded_fn (postfix_expression))
5027 tree fn = get_first_fn (postfix_expression);
5028 fn = STRIP_TEMPLATE (fn);
5030 /* Do not do argument dependent lookup if regular
5031 lookup finds a member function or a block-scope
5032 function declaration. [basic.lookup.argdep]/3 */
5033 if (!DECL_FUNCTION_MEMBER_P (fn)
5034 && !DECL_LOCAL_FUNCTION_P (fn))
5037 if (!any_type_dependent_arguments_p (args))
5039 = perform_koenig_lookup (postfix_expression, args,
5040 /*include_std=*/false);
5045 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5047 tree instance = TREE_OPERAND (postfix_expression, 0);
5048 tree fn = TREE_OPERAND (postfix_expression, 1);
5050 if (processing_template_decl
5051 && (type_dependent_expression_p (instance)
5052 || (!BASELINK_P (fn)
5053 && TREE_CODE (fn) != FIELD_DECL)
5054 || type_dependent_expression_p (fn)
5055 || any_type_dependent_arguments_p (args)))
5058 = build_nt_call_vec (postfix_expression, args);
5059 release_tree_vector (args);
5063 if (BASELINK_P (fn))
5066 = (build_new_method_call
5067 (instance, fn, &args, NULL_TREE,
5068 (idk == CP_ID_KIND_QUALIFIED
5069 ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5072 tf_warning_or_error));
5076 = finish_call_expr (postfix_expression, &args,
5077 /*disallow_virtual=*/false,
5079 tf_warning_or_error);
5081 else if (TREE_CODE (postfix_expression) == OFFSET_REF
5082 || TREE_CODE (postfix_expression) == MEMBER_REF
5083 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5084 postfix_expression = (build_offset_ref_call_from_tree
5085 (postfix_expression, &args));
5086 else if (idk == CP_ID_KIND_QUALIFIED)
5087 /* A call to a static class member, or a namespace-scope
5090 = finish_call_expr (postfix_expression, &args,
5091 /*disallow_virtual=*/true,
5093 tf_warning_or_error);
5095 /* All other function calls. */
5097 = finish_call_expr (postfix_expression, &args,
5098 /*disallow_virtual=*/false,
5100 tf_warning_or_error);
5102 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
5103 idk = CP_ID_KIND_NONE;
5105 release_tree_vector (args);
5111 /* postfix-expression . template [opt] id-expression
5112 postfix-expression . pseudo-destructor-name
5113 postfix-expression -> template [opt] id-expression
5114 postfix-expression -> pseudo-destructor-name */
5116 /* Consume the `.' or `->' operator. */
5117 cp_lexer_consume_token (parser->lexer);
5120 = cp_parser_postfix_dot_deref_expression (parser, token->type,
5125 is_member_access = true;
5129 /* postfix-expression ++ */
5130 /* Consume the `++' token. */
5131 cp_lexer_consume_token (parser->lexer);
5132 /* Generate a representation for the complete expression. */
5134 = finish_increment_expr (postfix_expression,
5135 POSTINCREMENT_EXPR);
5136 /* Increments may not appear in constant-expressions. */
5137 if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5138 postfix_expression = error_mark_node;
5139 idk = CP_ID_KIND_NONE;
5140 is_member_access = false;
5143 case CPP_MINUS_MINUS:
5144 /* postfix-expression -- */
5145 /* Consume the `--' token. */
5146 cp_lexer_consume_token (parser->lexer);
5147 /* Generate a representation for the complete expression. */
5149 = finish_increment_expr (postfix_expression,
5150 POSTDECREMENT_EXPR);
5151 /* Decrements may not appear in constant-expressions. */
5152 if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5153 postfix_expression = error_mark_node;
5154 idk = CP_ID_KIND_NONE;
5155 is_member_access = false;
5159 if (pidk_return != NULL)
5160 * pidk_return = idk;
5161 if (member_access_only_p)
5162 return is_member_access? postfix_expression : error_mark_node;
5164 return postfix_expression;
5168 /* We should never get here. */
5170 return error_mark_node;
5173 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5174 by cp_parser_builtin_offsetof. We're looking for
5176 postfix-expression [ expression ]
5178 FOR_OFFSETOF is set if we're being called in that context, which
5179 changes how we deal with integer constant expressions. */
5182 cp_parser_postfix_open_square_expression (cp_parser *parser,
5183 tree postfix_expression,
5188 /* Consume the `[' token. */
5189 cp_lexer_consume_token (parser->lexer);
5191 /* Parse the index expression. */
5192 /* ??? For offsetof, there is a question of what to allow here. If
5193 offsetof is not being used in an integral constant expression context,
5194 then we *could* get the right answer by computing the value at runtime.
5195 If we are in an integral constant expression context, then we might
5196 could accept any constant expression; hard to say without analysis.
5197 Rather than open the barn door too wide right away, allow only integer
5198 constant expressions here. */
5200 index = cp_parser_constant_expression (parser, false, NULL);
5202 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5204 /* Look for the closing `]'. */
5205 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5207 /* Build the ARRAY_REF. */
5208 postfix_expression = grok_array_decl (postfix_expression, index);
5210 /* When not doing offsetof, array references are not permitted in
5211 constant-expressions. */
5213 && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5214 postfix_expression = error_mark_node;
5216 return postfix_expression;
5219 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5220 by cp_parser_builtin_offsetof. We're looking for
5222 postfix-expression . template [opt] id-expression
5223 postfix-expression . pseudo-destructor-name
5224 postfix-expression -> template [opt] id-expression
5225 postfix-expression -> pseudo-destructor-name
5227 FOR_OFFSETOF is set if we're being called in that context. That sorta
5228 limits what of the above we'll actually accept, but nevermind.
5229 TOKEN_TYPE is the "." or "->" token, which will already have been
5230 removed from the stream. */
5233 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5234 enum cpp_ttype token_type,
5235 tree postfix_expression,
5236 bool for_offsetof, cp_id_kind *idk,
5237 location_t location)
5241 bool pseudo_destructor_p;
5242 tree scope = NULL_TREE;
5244 /* If this is a `->' operator, dereference the pointer. */
5245 if (token_type == CPP_DEREF)
5246 postfix_expression = build_x_arrow (postfix_expression);
5247 /* Check to see whether or not the expression is type-dependent. */
5248 dependent_p = type_dependent_expression_p (postfix_expression);
5249 /* The identifier following the `->' or `.' is not qualified. */
5250 parser->scope = NULL_TREE;
5251 parser->qualifying_scope = NULL_TREE;
5252 parser->object_scope = NULL_TREE;
5253 *idk = CP_ID_KIND_NONE;
5255 /* Enter the scope corresponding to the type of the object
5256 given by the POSTFIX_EXPRESSION. */
5257 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5259 scope = TREE_TYPE (postfix_expression);
5260 /* According to the standard, no expression should ever have
5261 reference type. Unfortunately, we do not currently match
5262 the standard in this respect in that our internal representation
5263 of an expression may have reference type even when the standard
5264 says it does not. Therefore, we have to manually obtain the
5265 underlying type here. */
5266 scope = non_reference (scope);
5267 /* The type of the POSTFIX_EXPRESSION must be complete. */
5268 if (scope == unknown_type_node)
5270 error_at (location, "%qE does not have class type",
5271 postfix_expression);
5275 scope = complete_type_or_else (scope, NULL_TREE);
5276 /* Let the name lookup machinery know that we are processing a
5277 class member access expression. */
5278 parser->context->object_type = scope;
5279 /* If something went wrong, we want to be able to discern that case,
5280 as opposed to the case where there was no SCOPE due to the type
5281 of expression being dependent. */
5283 scope = error_mark_node;
5284 /* If the SCOPE was erroneous, make the various semantic analysis
5285 functions exit quickly -- and without issuing additional error
5287 if (scope == error_mark_node)
5288 postfix_expression = error_mark_node;
5291 /* Assume this expression is not a pseudo-destructor access. */
5292 pseudo_destructor_p = false;
5294 /* If the SCOPE is a scalar type, then, if this is a valid program,
5295 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5296 is type dependent, it can be pseudo-destructor-name or something else.
5297 Try to parse it as pseudo-destructor-name first. */
5298 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5303 cp_parser_parse_tentatively (parser);
5304 /* Parse the pseudo-destructor-name. */
5306 cp_parser_pseudo_destructor_name (parser, &s, &type);
5308 && (cp_parser_error_occurred (parser)
5309 || TREE_CODE (type) != TYPE_DECL
5310 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5311 cp_parser_abort_tentative_parse (parser);
5312 else if (cp_parser_parse_definitely (parser))
5314 pseudo_destructor_p = true;
5316 = finish_pseudo_destructor_expr (postfix_expression,
5317 s, TREE_TYPE (type));
5321 if (!pseudo_destructor_p)
5323 /* If the SCOPE is not a scalar type, we are looking at an
5324 ordinary class member access expression, rather than a
5325 pseudo-destructor-name. */
5327 cp_token *token = cp_lexer_peek_token (parser->lexer);
5328 /* Parse the id-expression. */
5329 name = (cp_parser_id_expression
5331 cp_parser_optional_template_keyword (parser),
5332 /*check_dependency_p=*/true,
5334 /*declarator_p=*/false,
5335 /*optional_p=*/false));
5336 /* In general, build a SCOPE_REF if the member name is qualified.
5337 However, if the name was not dependent and has already been
5338 resolved; there is no need to build the SCOPE_REF. For example;
5340 struct X { void f(); };
5341 template <typename T> void f(T* t) { t->X::f(); }
5343 Even though "t" is dependent, "X::f" is not and has been resolved
5344 to a BASELINK; there is no need to include scope information. */
5346 /* But we do need to remember that there was an explicit scope for
5347 virtual function calls. */
5349 *idk = CP_ID_KIND_QUALIFIED;
5351 /* If the name is a template-id that names a type, we will get a
5352 TYPE_DECL here. That is invalid code. */
5353 if (TREE_CODE (name) == TYPE_DECL)
5355 error_at (token->location, "invalid use of %qD", name);
5356 postfix_expression = error_mark_node;
5360 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5362 name = build_qualified_name (/*type=*/NULL_TREE,
5366 parser->scope = NULL_TREE;
5367 parser->qualifying_scope = NULL_TREE;
5368 parser->object_scope = NULL_TREE;
5370 if (scope && name && BASELINK_P (name))
5371 adjust_result_of_qualified_name_lookup
5372 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5374 = finish_class_member_access_expr (postfix_expression, name,
5376 tf_warning_or_error);
5380 /* We no longer need to look up names in the scope of the object on
5381 the left-hand side of the `.' or `->' operator. */
5382 parser->context->object_type = NULL_TREE;
5384 /* Outside of offsetof, these operators may not appear in
5385 constant-expressions. */
5387 && (cp_parser_non_integral_constant_expression
5388 (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5389 postfix_expression = error_mark_node;
5391 return postfix_expression;
5394 /* Parse a parenthesized expression-list.
5397 assignment-expression
5398 expression-list, assignment-expression
5403 identifier, expression-list
5405 CAST_P is true if this expression is the target of a cast.
5407 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5410 Returns a vector of trees. Each element is a representation of an
5411 assignment-expression. NULL is returned if the ( and or ) are
5412 missing. An empty, but allocated, vector is returned on no
5413 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is id_attr
5414 if we are parsing an attribute list for an attribute that wants a
5415 plain identifier argument, normal_attr for an attribute that wants
5416 an expression, or non_attr if we aren't parsing an attribute list. If
5417 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5418 not all of the expressions in the list were constant. */
5420 static VEC(tree,gc) *
5421 cp_parser_parenthesized_expression_list (cp_parser* parser,
5422 int is_attribute_list,
5424 bool allow_expansion_p,
5425 bool *non_constant_p)
5427 VEC(tree,gc) *expression_list;
5428 bool fold_expr_p = is_attribute_list != non_attr;
5429 tree identifier = NULL_TREE;
5430 bool saved_greater_than_is_operator_p;
5432 /* Assume all the expressions will be constant. */
5434 *non_constant_p = false;
5436 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5439 expression_list = make_tree_vector ();
5441 /* Within a parenthesized expression, a `>' token is always
5442 the greater-than operator. */
5443 saved_greater_than_is_operator_p
5444 = parser->greater_than_is_operator_p;
5445 parser->greater_than_is_operator_p = true;
5447 /* Consume expressions until there are no more. */
5448 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5453 /* At the beginning of attribute lists, check to see if the
5454 next token is an identifier. */
5455 if (is_attribute_list == id_attr
5456 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5460 /* Consume the identifier. */
5461 token = cp_lexer_consume_token (parser->lexer);
5462 /* Save the identifier. */
5463 identifier = token->u.value;
5467 bool expr_non_constant_p;
5469 /* Parse the next assignment-expression. */
5470 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5472 /* A braced-init-list. */
5473 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5474 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5475 if (non_constant_p && expr_non_constant_p)
5476 *non_constant_p = true;
5478 else if (non_constant_p)
5480 expr = (cp_parser_constant_expression
5481 (parser, /*allow_non_constant_p=*/true,
5482 &expr_non_constant_p));
5483 if (expr_non_constant_p)
5484 *non_constant_p = true;
5487 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5490 expr = fold_non_dependent_expr (expr);
5492 /* If we have an ellipsis, then this is an expression
5494 if (allow_expansion_p
5495 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5497 /* Consume the `...'. */
5498 cp_lexer_consume_token (parser->lexer);
5500 /* Build the argument pack. */
5501 expr = make_pack_expansion (expr);
5504 /* Add it to the list. We add error_mark_node
5505 expressions to the list, so that we can still tell if
5506 the correct form for a parenthesized expression-list
5507 is found. That gives better errors. */
5508 VEC_safe_push (tree, gc, expression_list, expr);
5510 if (expr == error_mark_node)
5514 /* After the first item, attribute lists look the same as
5515 expression lists. */
5516 is_attribute_list = non_attr;
5519 /* If the next token isn't a `,', then we are done. */
5520 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5523 /* Otherwise, consume the `,' and keep going. */
5524 cp_lexer_consume_token (parser->lexer);
5527 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5532 /* We try and resync to an unnested comma, as that will give the
5533 user better diagnostics. */
5534 ending = cp_parser_skip_to_closing_parenthesis (parser,
5535 /*recovering=*/true,
5537 /*consume_paren=*/true);
5542 parser->greater_than_is_operator_p
5543 = saved_greater_than_is_operator_p;
5548 parser->greater_than_is_operator_p
5549 = saved_greater_than_is_operator_p;
5552 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5554 return expression_list;
5557 /* Parse a pseudo-destructor-name.
5559 pseudo-destructor-name:
5560 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5561 :: [opt] nested-name-specifier template template-id :: ~ type-name
5562 :: [opt] nested-name-specifier [opt] ~ type-name
5564 If either of the first two productions is used, sets *SCOPE to the
5565 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5566 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5567 or ERROR_MARK_NODE if the parse fails. */
5570 cp_parser_pseudo_destructor_name (cp_parser* parser,
5574 bool nested_name_specifier_p;
5576 /* Assume that things will not work out. */
5577 *type = error_mark_node;
5579 /* Look for the optional `::' operator. */
5580 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5581 /* Look for the optional nested-name-specifier. */
5582 nested_name_specifier_p
5583 = (cp_parser_nested_name_specifier_opt (parser,
5584 /*typename_keyword_p=*/false,
5585 /*check_dependency_p=*/true,
5587 /*is_declaration=*/false)
5589 /* Now, if we saw a nested-name-specifier, we might be doing the
5590 second production. */
5591 if (nested_name_specifier_p
5592 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5594 /* Consume the `template' keyword. */
5595 cp_lexer_consume_token (parser->lexer);
5596 /* Parse the template-id. */
5597 cp_parser_template_id (parser,
5598 /*template_keyword_p=*/true,
5599 /*check_dependency_p=*/false,
5600 /*is_declaration=*/true);
5601 /* Look for the `::' token. */
5602 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5604 /* If the next token is not a `~', then there might be some
5605 additional qualification. */
5606 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5608 /* At this point, we're looking for "type-name :: ~". The type-name
5609 must not be a class-name, since this is a pseudo-destructor. So,
5610 it must be either an enum-name, or a typedef-name -- both of which
5611 are just identifiers. So, we peek ahead to check that the "::"
5612 and "~" tokens are present; if they are not, then we can avoid
5613 calling type_name. */
5614 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5615 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5616 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5618 cp_parser_error (parser, "non-scalar type");
5622 /* Look for the type-name. */
5623 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5624 if (*scope == error_mark_node)
5627 /* Look for the `::' token. */
5628 cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5633 /* Look for the `~'. */
5634 cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5635 /* Look for the type-name again. We are not responsible for
5636 checking that it matches the first type-name. */
5637 *type = cp_parser_nonclass_name (parser);
5640 /* Parse a unary-expression.
5646 unary-operator cast-expression
5647 sizeof unary-expression
5649 alignof ( type-id ) [C++0x]
5656 __extension__ cast-expression
5657 __alignof__ unary-expression
5658 __alignof__ ( type-id )
5659 alignof unary-expression [C++0x]
5660 __real__ cast-expression
5661 __imag__ cast-expression
5664 ADDRESS_P is true iff the unary-expression is appearing as the
5665 operand of the `&' operator. CAST_P is true if this expression is
5666 the target of a cast.
5668 Returns a representation of the expression. */
5671 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5675 enum tree_code unary_operator;
5677 /* Peek at the next token. */
5678 token = cp_lexer_peek_token (parser->lexer);
5679 /* Some keywords give away the kind of expression. */
5680 if (token->type == CPP_KEYWORD)
5682 enum rid keyword = token->keyword;
5692 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5693 /* Consume the token. */
5694 cp_lexer_consume_token (parser->lexer);
5695 /* Parse the operand. */
5696 operand = cp_parser_sizeof_operand (parser, keyword);
5698 if (TYPE_P (operand))
5699 return cxx_sizeof_or_alignof_type (operand, op, true);
5702 /* ISO C++ defines alignof only with types, not with
5703 expressions. So pedwarn if alignof is used with a non-
5704 type expression. However, __alignof__ is ok. */
5705 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5706 pedwarn (token->location, OPT_pedantic,
5707 "ISO C++ does not allow %<alignof%> "
5710 return cxx_sizeof_or_alignof_expr (operand, op, true);
5715 return cp_parser_new_expression (parser);
5718 return cp_parser_delete_expression (parser);
5722 /* The saved value of the PEDANTIC flag. */
5726 /* Save away the PEDANTIC flag. */
5727 cp_parser_extension_opt (parser, &saved_pedantic);
5728 /* Parse the cast-expression. */
5729 expr = cp_parser_simple_cast_expression (parser);
5730 /* Restore the PEDANTIC flag. */
5731 pedantic = saved_pedantic;
5741 /* Consume the `__real__' or `__imag__' token. */
5742 cp_lexer_consume_token (parser->lexer);
5743 /* Parse the cast-expression. */
5744 expression = cp_parser_simple_cast_expression (parser);
5745 /* Create the complete representation. */
5746 return build_x_unary_op ((keyword == RID_REALPART
5747 ? REALPART_EXPR : IMAGPART_EXPR),
5749 tf_warning_or_error);
5756 const char *saved_message;
5757 bool saved_integral_constant_expression_p;
5758 bool saved_non_integral_constant_expression_p;
5759 bool saved_greater_than_is_operator_p;
5761 cp_lexer_consume_token (parser->lexer);
5762 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5764 saved_message = parser->type_definition_forbidden_message;
5765 parser->type_definition_forbidden_message
5766 = G_("types may not be defined in %<noexcept%> expressions");
5768 saved_integral_constant_expression_p
5769 = parser->integral_constant_expression_p;
5770 saved_non_integral_constant_expression_p
5771 = parser->non_integral_constant_expression_p;
5772 parser->integral_constant_expression_p = false;
5774 saved_greater_than_is_operator_p
5775 = parser->greater_than_is_operator_p;
5776 parser->greater_than_is_operator_p = true;
5778 ++cp_unevaluated_operand;
5779 ++c_inhibit_evaluation_warnings;
5780 expr = cp_parser_expression (parser, false, NULL);
5781 --c_inhibit_evaluation_warnings;
5782 --cp_unevaluated_operand;
5784 parser->greater_than_is_operator_p
5785 = saved_greater_than_is_operator_p;
5787 parser->integral_constant_expression_p
5788 = saved_integral_constant_expression_p;
5789 parser->non_integral_constant_expression_p
5790 = saved_non_integral_constant_expression_p;
5792 parser->type_definition_forbidden_message = saved_message;
5794 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5795 return finish_noexcept_expr (expr, tf_warning_or_error);
5803 /* Look for the `:: new' and `:: delete', which also signal the
5804 beginning of a new-expression, or delete-expression,
5805 respectively. If the next token is `::', then it might be one of
5807 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5811 /* See if the token after the `::' is one of the keywords in
5812 which we're interested. */
5813 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5814 /* If it's `new', we have a new-expression. */
5815 if (keyword == RID_NEW)
5816 return cp_parser_new_expression (parser);
5817 /* Similarly, for `delete'. */
5818 else if (keyword == RID_DELETE)
5819 return cp_parser_delete_expression (parser);
5822 /* Look for a unary operator. */
5823 unary_operator = cp_parser_unary_operator (token);
5824 /* The `++' and `--' operators can be handled similarly, even though
5825 they are not technically unary-operators in the grammar. */
5826 if (unary_operator == ERROR_MARK)
5828 if (token->type == CPP_PLUS_PLUS)
5829 unary_operator = PREINCREMENT_EXPR;
5830 else if (token->type == CPP_MINUS_MINUS)
5831 unary_operator = PREDECREMENT_EXPR;
5832 /* Handle the GNU address-of-label extension. */
5833 else if (cp_parser_allow_gnu_extensions_p (parser)
5834 && token->type == CPP_AND_AND)
5838 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5840 /* Consume the '&&' token. */
5841 cp_lexer_consume_token (parser->lexer);
5842 /* Look for the identifier. */
5843 identifier = cp_parser_identifier (parser);
5844 /* Create an expression representing the address. */
5845 expression = finish_label_address_expr (identifier, loc);
5846 if (cp_parser_non_integral_constant_expression (parser,
5848 expression = error_mark_node;
5852 if (unary_operator != ERROR_MARK)
5854 tree cast_expression;
5855 tree expression = error_mark_node;
5856 non_integral_constant non_constant_p = NIC_NONE;
5858 /* Consume the operator token. */
5859 token = cp_lexer_consume_token (parser->lexer);
5860 /* Parse the cast-expression. */
5862 = cp_parser_cast_expression (parser,
5863 unary_operator == ADDR_EXPR,
5864 /*cast_p=*/false, pidk);
5865 /* Now, build an appropriate representation. */
5866 switch (unary_operator)
5869 non_constant_p = NIC_STAR;
5870 expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5871 tf_warning_or_error);
5875 non_constant_p = NIC_ADDR;
5878 expression = build_x_unary_op (unary_operator, cast_expression,
5879 tf_warning_or_error);
5882 case PREINCREMENT_EXPR:
5883 case PREDECREMENT_EXPR:
5884 non_constant_p = unary_operator == PREINCREMENT_EXPR
5885 ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5887 case UNARY_PLUS_EXPR:
5889 case TRUTH_NOT_EXPR:
5890 expression = finish_unary_op_expr (unary_operator, cast_expression);
5897 if (non_constant_p != NIC_NONE
5898 && cp_parser_non_integral_constant_expression (parser,
5900 expression = error_mark_node;
5905 return cp_parser_postfix_expression (parser, address_p, cast_p,
5906 /*member_access_only_p=*/false,
5910 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5911 unary-operator, the corresponding tree code is returned. */
5913 static enum tree_code
5914 cp_parser_unary_operator (cp_token* token)
5916 switch (token->type)
5919 return INDIRECT_REF;
5925 return UNARY_PLUS_EXPR;
5931 return TRUTH_NOT_EXPR;
5934 return BIT_NOT_EXPR;
5941 /* Parse a new-expression.
5944 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5945 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5947 Returns a representation of the expression. */
5950 cp_parser_new_expression (cp_parser* parser)
5952 bool global_scope_p;
5953 VEC(tree,gc) *placement;
5955 VEC(tree,gc) *initializer;
5959 /* Look for the optional `::' operator. */
5961 = (cp_parser_global_scope_opt (parser,
5962 /*current_scope_valid_p=*/false)
5964 /* Look for the `new' operator. */
5965 cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5966 /* There's no easy way to tell a new-placement from the
5967 `( type-id )' construct. */
5968 cp_parser_parse_tentatively (parser);
5969 /* Look for a new-placement. */
5970 placement = cp_parser_new_placement (parser);
5971 /* If that didn't work out, there's no new-placement. */
5972 if (!cp_parser_parse_definitely (parser))
5974 if (placement != NULL)
5975 release_tree_vector (placement);
5979 /* If the next token is a `(', then we have a parenthesized
5981 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5984 /* Consume the `('. */
5985 cp_lexer_consume_token (parser->lexer);
5986 /* Parse the type-id. */
5987 type = cp_parser_type_id (parser);
5988 /* Look for the closing `)'. */
5989 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5990 token = cp_lexer_peek_token (parser->lexer);
5991 /* There should not be a direct-new-declarator in this production,
5992 but GCC used to allowed this, so we check and emit a sensible error
5993 message for this case. */
5994 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5996 error_at (token->location,
5997 "array bound forbidden after parenthesized type-id");
5998 inform (token->location,
5999 "try removing the parentheses around the type-id");
6000 cp_parser_direct_new_declarator (parser);
6004 /* Otherwise, there must be a new-type-id. */
6006 type = cp_parser_new_type_id (parser, &nelts);
6008 /* If the next token is a `(' or '{', then we have a new-initializer. */
6009 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6010 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6011 initializer = cp_parser_new_initializer (parser);
6015 /* A new-expression may not appear in an integral constant
6017 if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6018 ret = error_mark_node;
6021 /* Create a representation of the new-expression. */
6022 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6023 tf_warning_or_error);
6026 if (placement != NULL)
6027 release_tree_vector (placement);
6028 if (initializer != NULL)
6029 release_tree_vector (initializer);
6034 /* Parse a new-placement.
6039 Returns the same representation as for an expression-list. */
6041 static VEC(tree,gc) *
6042 cp_parser_new_placement (cp_parser* parser)
6044 VEC(tree,gc) *expression_list;
6046 /* Parse the expression-list. */
6047 expression_list = (cp_parser_parenthesized_expression_list
6048 (parser, non_attr, /*cast_p=*/false,
6049 /*allow_expansion_p=*/true,
6050 /*non_constant_p=*/NULL));
6052 return expression_list;
6055 /* Parse a new-type-id.
6058 type-specifier-seq new-declarator [opt]
6060 Returns the TYPE allocated. If the new-type-id indicates an array
6061 type, *NELTS is set to the number of elements in the last array
6062 bound; the TYPE will not include the last array bound. */
6065 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6067 cp_decl_specifier_seq type_specifier_seq;
6068 cp_declarator *new_declarator;
6069 cp_declarator *declarator;
6070 cp_declarator *outer_declarator;
6071 const char *saved_message;
6074 /* The type-specifier sequence must not contain type definitions.
6075 (It cannot contain declarations of new types either, but if they
6076 are not definitions we will catch that because they are not
6078 saved_message = parser->type_definition_forbidden_message;
6079 parser->type_definition_forbidden_message
6080 = G_("types may not be defined in a new-type-id");
6081 /* Parse the type-specifier-seq. */
6082 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6083 /*is_trailing_return=*/false,
6084 &type_specifier_seq);
6085 /* Restore the old message. */
6086 parser->type_definition_forbidden_message = saved_message;
6087 /* Parse the new-declarator. */
6088 new_declarator = cp_parser_new_declarator_opt (parser);
6090 /* Determine the number of elements in the last array dimension, if
6093 /* Skip down to the last array dimension. */
6094 declarator = new_declarator;
6095 outer_declarator = NULL;
6096 while (declarator && (declarator->kind == cdk_pointer
6097 || declarator->kind == cdk_ptrmem))
6099 outer_declarator = declarator;
6100 declarator = declarator->declarator;
6103 && declarator->kind == cdk_array
6104 && declarator->declarator
6105 && declarator->declarator->kind == cdk_array)
6107 outer_declarator = declarator;
6108 declarator = declarator->declarator;
6111 if (declarator && declarator->kind == cdk_array)
6113 *nelts = declarator->u.array.bounds;
6114 if (*nelts == error_mark_node)
6115 *nelts = integer_one_node;
6117 if (outer_declarator)
6118 outer_declarator->declarator = declarator->declarator;
6120 new_declarator = NULL;
6123 type = groktypename (&type_specifier_seq, new_declarator, false);
6127 /* Parse an (optional) new-declarator.
6130 ptr-operator new-declarator [opt]
6131 direct-new-declarator
6133 Returns the declarator. */
6135 static cp_declarator *
6136 cp_parser_new_declarator_opt (cp_parser* parser)
6138 enum tree_code code;
6140 cp_cv_quals cv_quals;
6142 /* We don't know if there's a ptr-operator next, or not. */
6143 cp_parser_parse_tentatively (parser);
6144 /* Look for a ptr-operator. */
6145 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6146 /* If that worked, look for more new-declarators. */
6147 if (cp_parser_parse_definitely (parser))
6149 cp_declarator *declarator;
6151 /* Parse another optional declarator. */
6152 declarator = cp_parser_new_declarator_opt (parser);
6154 return cp_parser_make_indirect_declarator
6155 (code, type, cv_quals, declarator);
6158 /* If the next token is a `[', there is a direct-new-declarator. */
6159 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6160 return cp_parser_direct_new_declarator (parser);
6165 /* Parse a direct-new-declarator.
6167 direct-new-declarator:
6169 direct-new-declarator [constant-expression]
6173 static cp_declarator *
6174 cp_parser_direct_new_declarator (cp_parser* parser)
6176 cp_declarator *declarator = NULL;
6182 /* Look for the opening `['. */
6183 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6184 /* The first expression is not required to be constant. */
6187 cp_token *token = cp_lexer_peek_token (parser->lexer);
6188 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6189 /* The standard requires that the expression have integral
6190 type. DR 74 adds enumeration types. We believe that the
6191 real intent is that these expressions be handled like the
6192 expression in a `switch' condition, which also allows
6193 classes with a single conversion to integral or
6194 enumeration type. */
6195 if (!processing_template_decl)
6198 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6203 error_at (token->location,
6204 "expression in new-declarator must have integral "
6205 "or enumeration type");
6206 expression = error_mark_node;
6210 /* But all the other expressions must be. */
6213 = cp_parser_constant_expression (parser,
6214 /*allow_non_constant=*/false,
6216 /* Look for the closing `]'. */
6217 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6219 /* Add this bound to the declarator. */
6220 declarator = make_array_declarator (declarator, expression);
6222 /* If the next token is not a `[', then there are no more
6224 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6231 /* Parse a new-initializer.
6234 ( expression-list [opt] )
6237 Returns a representation of the expression-list. */
6239 static VEC(tree,gc) *
6240 cp_parser_new_initializer (cp_parser* parser)
6242 VEC(tree,gc) *expression_list;
6244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6247 bool expr_non_constant_p;
6248 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6249 t = cp_parser_braced_list (parser, &expr_non_constant_p);
6250 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6251 expression_list = make_tree_vector_single (t);
6254 expression_list = (cp_parser_parenthesized_expression_list
6255 (parser, non_attr, /*cast_p=*/false,
6256 /*allow_expansion_p=*/true,
6257 /*non_constant_p=*/NULL));
6259 return expression_list;
6262 /* Parse a delete-expression.
6265 :: [opt] delete cast-expression
6266 :: [opt] delete [ ] cast-expression
6268 Returns a representation of the expression. */
6271 cp_parser_delete_expression (cp_parser* parser)
6273 bool global_scope_p;
6277 /* Look for the optional `::' operator. */
6279 = (cp_parser_global_scope_opt (parser,
6280 /*current_scope_valid_p=*/false)
6282 /* Look for the `delete' keyword. */
6283 cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6284 /* See if the array syntax is in use. */
6285 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6287 /* Consume the `[' token. */
6288 cp_lexer_consume_token (parser->lexer);
6289 /* Look for the `]' token. */
6290 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6291 /* Remember that this is the `[]' construct. */
6297 /* Parse the cast-expression. */
6298 expression = cp_parser_simple_cast_expression (parser);
6300 /* A delete-expression may not appear in an integral constant
6302 if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6303 return error_mark_node;
6305 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6306 tf_warning_or_error);
6309 /* Returns true if TOKEN may start a cast-expression and false
6313 cp_parser_token_starts_cast_expression (cp_token *token)
6315 switch (token->type)
6321 case CPP_CLOSE_SQUARE:
6322 case CPP_CLOSE_PAREN:
6323 case CPP_CLOSE_BRACE:
6327 case CPP_DEREF_STAR:
6335 case CPP_GREATER_EQ:
6355 /* '[' may start a primary-expression in obj-c++. */
6356 case CPP_OPEN_SQUARE:
6357 return c_dialect_objc ();
6364 /* Parse a cast-expression.
6368 ( type-id ) cast-expression
6370 ADDRESS_P is true iff the unary-expression is appearing as the
6371 operand of the `&' operator. CAST_P is true if this expression is
6372 the target of a cast.
6374 Returns a representation of the expression. */
6377 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6380 /* If it's a `(', then we might be looking at a cast. */
6381 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6383 tree type = NULL_TREE;
6384 tree expr = NULL_TREE;
6385 bool compound_literal_p;
6386 const char *saved_message;
6388 /* There's no way to know yet whether or not this is a cast.
6389 For example, `(int (3))' is a unary-expression, while `(int)
6390 3' is a cast. So, we resort to parsing tentatively. */
6391 cp_parser_parse_tentatively (parser);
6392 /* Types may not be defined in a cast. */
6393 saved_message = parser->type_definition_forbidden_message;
6394 parser->type_definition_forbidden_message
6395 = G_("types may not be defined in casts");
6396 /* Consume the `('. */
6397 cp_lexer_consume_token (parser->lexer);
6398 /* A very tricky bit is that `(struct S) { 3 }' is a
6399 compound-literal (which we permit in C++ as an extension).
6400 But, that construct is not a cast-expression -- it is a
6401 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6402 is legal; if the compound-literal were a cast-expression,
6403 you'd need an extra set of parentheses.) But, if we parse
6404 the type-id, and it happens to be a class-specifier, then we
6405 will commit to the parse at that point, because we cannot
6406 undo the action that is done when creating a new class. So,
6407 then we cannot back up and do a postfix-expression.
6409 Therefore, we scan ahead to the closing `)', and check to see
6410 if the token after the `)' is a `{'. If so, we are not
6411 looking at a cast-expression.
6413 Save tokens so that we can put them back. */
6414 cp_lexer_save_tokens (parser->lexer);
6415 /* Skip tokens until the next token is a closing parenthesis.
6416 If we find the closing `)', and the next token is a `{', then
6417 we are looking at a compound-literal. */
6419 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6420 /*consume_paren=*/true)
6421 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6422 /* Roll back the tokens we skipped. */
6423 cp_lexer_rollback_tokens (parser->lexer);
6424 /* If we were looking at a compound-literal, simulate an error
6425 so that the call to cp_parser_parse_definitely below will
6427 if (compound_literal_p)
6428 cp_parser_simulate_error (parser);
6431 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6432 parser->in_type_id_in_expr_p = true;
6433 /* Look for the type-id. */
6434 type = cp_parser_type_id (parser);
6435 /* Look for the closing `)'. */
6436 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6437 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6440 /* Restore the saved message. */
6441 parser->type_definition_forbidden_message = saved_message;
6443 /* At this point this can only be either a cast or a
6444 parenthesized ctor such as `(T ())' that looks like a cast to
6445 function returning T. */
6446 if (!cp_parser_error_occurred (parser)
6447 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6450 cp_parser_parse_definitely (parser);
6451 expr = cp_parser_cast_expression (parser,
6452 /*address_p=*/false,
6453 /*cast_p=*/true, pidk);
6455 /* Warn about old-style casts, if so requested. */
6456 if (warn_old_style_cast
6457 && !in_system_header
6458 && !VOID_TYPE_P (type)
6459 && current_lang_name != lang_name_c)
6460 warning (OPT_Wold_style_cast, "use of old-style cast");
6462 /* Only type conversions to integral or enumeration types
6463 can be used in constant-expressions. */
6464 if (!cast_valid_in_integral_constant_expression_p (type)
6465 && cp_parser_non_integral_constant_expression (parser,
6467 return error_mark_node;
6469 /* Perform the cast. */
6470 expr = build_c_cast (input_location, type, expr);
6474 cp_parser_abort_tentative_parse (parser);
6477 /* If we get here, then it's not a cast, so it must be a
6478 unary-expression. */
6479 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6482 /* Parse a binary expression of the general form:
6486 pm-expression .* cast-expression
6487 pm-expression ->* cast-expression
6489 multiplicative-expression:
6491 multiplicative-expression * pm-expression
6492 multiplicative-expression / pm-expression
6493 multiplicative-expression % pm-expression
6495 additive-expression:
6496 multiplicative-expression
6497 additive-expression + multiplicative-expression
6498 additive-expression - multiplicative-expression
6502 shift-expression << additive-expression
6503 shift-expression >> additive-expression
6505 relational-expression:
6507 relational-expression < shift-expression
6508 relational-expression > shift-expression
6509 relational-expression <= shift-expression
6510 relational-expression >= shift-expression
6514 relational-expression:
6515 relational-expression <? shift-expression
6516 relational-expression >? shift-expression
6518 equality-expression:
6519 relational-expression
6520 equality-expression == relational-expression
6521 equality-expression != relational-expression
6525 and-expression & equality-expression
6527 exclusive-or-expression:
6529 exclusive-or-expression ^ and-expression
6531 inclusive-or-expression:
6532 exclusive-or-expression
6533 inclusive-or-expression | exclusive-or-expression
6535 logical-and-expression:
6536 inclusive-or-expression
6537 logical-and-expression && inclusive-or-expression
6539 logical-or-expression:
6540 logical-and-expression
6541 logical-or-expression || logical-and-expression
6543 All these are implemented with a single function like:
6546 simple-cast-expression
6547 binary-expression <token> binary-expression
6549 CAST_P is true if this expression is the target of a cast.
6551 The binops_by_token map is used to get the tree codes for each <token> type.
6552 binary-expressions are associated according to a precedence table. */
6554 #define TOKEN_PRECEDENCE(token) \
6555 (((token->type == CPP_GREATER \
6556 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6557 && !parser->greater_than_is_operator_p) \
6558 ? PREC_NOT_OPERATOR \
6559 : binops_by_token[token->type].prec)
6562 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6563 bool no_toplevel_fold_p,
6564 enum cp_parser_prec prec,
6567 cp_parser_expression_stack stack;
6568 cp_parser_expression_stack_entry *sp = &stack[0];
6571 enum tree_code tree_type, lhs_type, rhs_type;
6572 enum cp_parser_prec new_prec, lookahead_prec;
6575 /* Parse the first expression. */
6576 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6577 lhs_type = ERROR_MARK;
6581 /* Get an operator token. */
6582 token = cp_lexer_peek_token (parser->lexer);
6584 if (warn_cxx0x_compat
6585 && token->type == CPP_RSHIFT
6586 && !parser->greater_than_is_operator_p)
6588 if (warning_at (token->location, OPT_Wc__0x_compat,
6589 "%<>>%> operator will be treated as"
6590 " two right angle brackets in C++0x"))
6591 inform (token->location,
6592 "suggest parentheses around %<>>%> expression");
6595 new_prec = TOKEN_PRECEDENCE (token);
6597 /* Popping an entry off the stack means we completed a subexpression:
6598 - either we found a token which is not an operator (`>' where it is not
6599 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6600 will happen repeatedly;
6601 - or, we found an operator which has lower priority. This is the case
6602 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6604 if (new_prec <= prec)
6613 tree_type = binops_by_token[token->type].tree_type;
6615 /* We used the operator token. */
6616 cp_lexer_consume_token (parser->lexer);
6618 /* For "false && x" or "true || x", x will never be executed;
6619 disable warnings while evaluating it. */
6620 if (tree_type == TRUTH_ANDIF_EXPR)
6621 c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6622 else if (tree_type == TRUTH_ORIF_EXPR)
6623 c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6625 /* Extract another operand. It may be the RHS of this expression
6626 or the LHS of a new, higher priority expression. */
6627 rhs = cp_parser_simple_cast_expression (parser);
6628 rhs_type = ERROR_MARK;
6630 /* Get another operator token. Look up its precedence to avoid
6631 building a useless (immediately popped) stack entry for common
6632 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6633 token = cp_lexer_peek_token (parser->lexer);
6634 lookahead_prec = TOKEN_PRECEDENCE (token);
6635 if (lookahead_prec > new_prec)
6637 /* ... and prepare to parse the RHS of the new, higher priority
6638 expression. Since precedence levels on the stack are
6639 monotonically increasing, we do not have to care about
6642 sp->tree_type = tree_type;
6644 sp->lhs_type = lhs_type;
6647 lhs_type = rhs_type;
6649 new_prec = lookahead_prec;
6653 lookahead_prec = new_prec;
6654 /* If the stack is not empty, we have parsed into LHS the right side
6655 (`4' in the example above) of an expression we had suspended.
6656 We can use the information on the stack to recover the LHS (`3')
6657 from the stack together with the tree code (`MULT_EXPR'), and
6658 the precedence of the higher level subexpression
6659 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6660 which will be used to actually build the additive expression. */
6663 tree_type = sp->tree_type;
6665 rhs_type = lhs_type;
6667 lhs_type = sp->lhs_type;
6670 /* Undo the disabling of warnings done above. */
6671 if (tree_type == TRUTH_ANDIF_EXPR)
6672 c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6673 else if (tree_type == TRUTH_ORIF_EXPR)
6674 c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6676 overloaded_p = false;
6677 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6678 ERROR_MARK for everything that is not a binary expression.
6679 This makes warn_about_parentheses miss some warnings that
6680 involve unary operators. For unary expressions we should
6681 pass the correct tree_code unless the unary expression was
6682 surrounded by parentheses.
6684 if (no_toplevel_fold_p
6685 && lookahead_prec <= prec
6687 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6688 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6690 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6691 &overloaded_p, tf_warning_or_error);
6692 lhs_type = tree_type;
6694 /* If the binary operator required the use of an overloaded operator,
6695 then this expression cannot be an integral constant-expression.
6696 An overloaded operator can be used even if both operands are
6697 otherwise permissible in an integral constant-expression if at
6698 least one of the operands is of enumeration type. */
6701 && cp_parser_non_integral_constant_expression (parser,
6703 return error_mark_node;
6710 /* Parse the `? expression : assignment-expression' part of a
6711 conditional-expression. The LOGICAL_OR_EXPR is the
6712 logical-or-expression that started the conditional-expression.
6713 Returns a representation of the entire conditional-expression.
6715 This routine is used by cp_parser_assignment_expression.
6717 ? expression : assignment-expression
6721 ? : assignment-expression */
6724 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6727 tree assignment_expr;
6728 struct cp_token *token;
6730 /* Consume the `?' token. */
6731 cp_lexer_consume_token (parser->lexer);
6732 token = cp_lexer_peek_token (parser->lexer);
6733 if (cp_parser_allow_gnu_extensions_p (parser)
6734 && token->type == CPP_COLON)
6736 pedwarn (token->location, OPT_pedantic,
6737 "ISO C++ does not allow ?: with omitted middle operand");
6738 /* Implicit true clause. */
6740 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6741 warn_for_omitted_condop (token->location, logical_or_expr);
6745 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6746 parser->colon_corrects_to_scope_p = false;
6747 /* Parse the expression. */
6748 c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6749 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6750 c_inhibit_evaluation_warnings +=
6751 ((logical_or_expr == truthvalue_true_node)
6752 - (logical_or_expr == truthvalue_false_node));
6753 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6756 /* The next token should be a `:'. */
6757 cp_parser_require (parser, CPP_COLON, RT_COLON);
6758 /* Parse the assignment-expression. */
6759 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6760 c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6762 /* Build the conditional-expression. */
6763 return build_x_conditional_expr (logical_or_expr,
6766 tf_warning_or_error);
6769 /* Parse an assignment-expression.
6771 assignment-expression:
6772 conditional-expression
6773 logical-or-expression assignment-operator assignment_expression
6776 CAST_P is true if this expression is the target of a cast.
6778 Returns a representation for the expression. */
6781 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6786 /* If the next token is the `throw' keyword, then we're looking at
6787 a throw-expression. */
6788 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6789 expr = cp_parser_throw_expression (parser);
6790 /* Otherwise, it must be that we are looking at a
6791 logical-or-expression. */
6794 /* Parse the binary expressions (logical-or-expression). */
6795 expr = cp_parser_binary_expression (parser, cast_p, false,
6796 PREC_NOT_OPERATOR, pidk);
6797 /* If the next token is a `?' then we're actually looking at a
6798 conditional-expression. */
6799 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6800 return cp_parser_question_colon_clause (parser, expr);
6803 enum tree_code assignment_operator;
6805 /* If it's an assignment-operator, we're using the second
6808 = cp_parser_assignment_operator_opt (parser);
6809 if (assignment_operator != ERROR_MARK)
6811 bool non_constant_p;
6813 /* Parse the right-hand side of the assignment. */
6814 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6816 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6817 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6819 /* An assignment may not appear in a
6820 constant-expression. */
6821 if (cp_parser_non_integral_constant_expression (parser,
6823 return error_mark_node;
6824 /* Build the assignment expression. */
6825 expr = build_x_modify_expr (expr,
6826 assignment_operator,
6828 tf_warning_or_error);
6836 /* Parse an (optional) assignment-operator.
6838 assignment-operator: one of
6839 = *= /= %= += -= >>= <<= &= ^= |=
6843 assignment-operator: one of
6846 If the next token is an assignment operator, the corresponding tree
6847 code is returned, and the token is consumed. For example, for
6848 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6849 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6850 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6851 operator, ERROR_MARK is returned. */
6853 static enum tree_code
6854 cp_parser_assignment_operator_opt (cp_parser* parser)
6859 /* Peek at the next token. */
6860 token = cp_lexer_peek_token (parser->lexer);
6862 switch (token->type)
6873 op = TRUNC_DIV_EXPR;
6877 op = TRUNC_MOD_EXPR;
6909 /* Nothing else is an assignment operator. */
6913 /* If it was an assignment operator, consume it. */
6914 if (op != ERROR_MARK)
6915 cp_lexer_consume_token (parser->lexer);
6920 /* Parse an expression.
6923 assignment-expression
6924 expression , assignment-expression
6926 CAST_P is true if this expression is the target of a cast.
6928 Returns a representation of the expression. */
6931 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6933 tree expression = NULL_TREE;
6937 tree assignment_expression;
6939 /* Parse the next assignment-expression. */
6940 assignment_expression
6941 = cp_parser_assignment_expression (parser, cast_p, pidk);
6942 /* If this is the first assignment-expression, we can just
6945 expression = assignment_expression;
6947 expression = build_x_compound_expr (expression,
6948 assignment_expression,
6949 tf_warning_or_error);
6950 /* If the next token is not a comma, then we are done with the
6952 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6954 /* Consume the `,'. */
6955 cp_lexer_consume_token (parser->lexer);
6956 /* A comma operator cannot appear in a constant-expression. */
6957 if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6958 expression = error_mark_node;
6964 /* Parse a constant-expression.
6966 constant-expression:
6967 conditional-expression
6969 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6970 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6971 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6972 is false, NON_CONSTANT_P should be NULL. */
6975 cp_parser_constant_expression (cp_parser* parser,
6976 bool allow_non_constant_p,
6977 bool *non_constant_p)
6979 bool saved_integral_constant_expression_p;
6980 bool saved_allow_non_integral_constant_expression_p;
6981 bool saved_non_integral_constant_expression_p;
6984 /* It might seem that we could simply parse the
6985 conditional-expression, and then check to see if it were
6986 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6987 one that the compiler can figure out is constant, possibly after
6988 doing some simplifications or optimizations. The standard has a
6989 precise definition of constant-expression, and we must honor
6990 that, even though it is somewhat more restrictive.
6996 is not a legal declaration, because `(2, 3)' is not a
6997 constant-expression. The `,' operator is forbidden in a
6998 constant-expression. However, GCC's constant-folding machinery
6999 will fold this operation to an INTEGER_CST for `3'. */
7001 /* Save the old settings. */
7002 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7003 saved_allow_non_integral_constant_expression_p
7004 = parser->allow_non_integral_constant_expression_p;
7005 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7006 /* We are now parsing a constant-expression. */
7007 parser->integral_constant_expression_p = true;
7008 parser->allow_non_integral_constant_expression_p
7009 = (allow_non_constant_p || cxx_dialect >= cxx0x);
7010 parser->non_integral_constant_expression_p = false;
7011 /* Although the grammar says "conditional-expression", we parse an
7012 "assignment-expression", which also permits "throw-expression"
7013 and the use of assignment operators. In the case that
7014 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7015 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
7016 actually essential that we look for an assignment-expression.
7017 For example, cp_parser_initializer_clauses uses this function to
7018 determine whether a particular assignment-expression is in fact
7020 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7021 /* Restore the old settings. */
7022 parser->integral_constant_expression_p
7023 = saved_integral_constant_expression_p;
7024 parser->allow_non_integral_constant_expression_p
7025 = saved_allow_non_integral_constant_expression_p;
7026 if (cxx_dialect >= cxx0x)
7028 /* Require an rvalue constant expression here; that's what our
7029 callers expect. Reference constant expressions are handled
7030 separately in e.g. cp_parser_template_argument. */
7031 bool is_const = potential_rvalue_constant_expression (expression);
7032 parser->non_integral_constant_expression_p = !is_const;
7033 if (!is_const && !allow_non_constant_p)
7034 require_potential_rvalue_constant_expression (expression);
7036 if (allow_non_constant_p)
7037 *non_constant_p = parser->non_integral_constant_expression_p;
7038 else if (parser->non_integral_constant_expression_p)
7039 expression = error_mark_node;
7040 parser->non_integral_constant_expression_p
7041 = saved_non_integral_constant_expression_p;
7046 /* Parse __builtin_offsetof.
7048 offsetof-expression:
7049 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7051 offsetof-member-designator:
7053 | offsetof-member-designator "." id-expression
7054 | offsetof-member-designator "[" expression "]"
7055 | offsetof-member-designator "->" id-expression */
7058 cp_parser_builtin_offsetof (cp_parser *parser)
7060 int save_ice_p, save_non_ice_p;
7065 /* We're about to accept non-integral-constant things, but will
7066 definitely yield an integral constant expression. Save and
7067 restore these values around our local parsing. */
7068 save_ice_p = parser->integral_constant_expression_p;
7069 save_non_ice_p = parser->non_integral_constant_expression_p;
7071 /* Consume the "__builtin_offsetof" token. */
7072 cp_lexer_consume_token (parser->lexer);
7073 /* Consume the opening `('. */
7074 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7075 /* Parse the type-id. */
7076 type = cp_parser_type_id (parser);
7077 /* Look for the `,'. */
7078 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7079 token = cp_lexer_peek_token (parser->lexer);
7081 /* Build the (type *)null that begins the traditional offsetof macro. */
7082 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7083 tf_warning_or_error);
7085 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
7086 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7087 true, &dummy, token->location);
7090 token = cp_lexer_peek_token (parser->lexer);
7091 switch (token->type)
7093 case CPP_OPEN_SQUARE:
7094 /* offsetof-member-designator "[" expression "]" */
7095 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7099 /* offsetof-member-designator "->" identifier */
7100 expr = grok_array_decl (expr, integer_zero_node);
7104 /* offsetof-member-designator "." identifier */
7105 cp_lexer_consume_token (parser->lexer);
7106 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7111 case CPP_CLOSE_PAREN:
7112 /* Consume the ")" token. */
7113 cp_lexer_consume_token (parser->lexer);
7117 /* Error. We know the following require will fail, but
7118 that gives the proper error message. */
7119 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7120 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7121 expr = error_mark_node;
7127 /* If we're processing a template, we can't finish the semantics yet.
7128 Otherwise we can fold the entire expression now. */
7129 if (processing_template_decl)
7130 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7132 expr = finish_offsetof (expr);
7135 parser->integral_constant_expression_p = save_ice_p;
7136 parser->non_integral_constant_expression_p = save_non_ice_p;
7141 /* Parse a trait expression.
7143 Returns a representation of the expression, the underlying type
7144 of the type at issue when KEYWORD is RID_UNDERLYING_TYPE. */
7147 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7150 tree type1, type2 = NULL_TREE;
7151 bool binary = false;
7152 cp_decl_specifier_seq decl_specs;
7156 case RID_HAS_NOTHROW_ASSIGN:
7157 kind = CPTK_HAS_NOTHROW_ASSIGN;
7159 case RID_HAS_NOTHROW_CONSTRUCTOR:
7160 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7162 case RID_HAS_NOTHROW_COPY:
7163 kind = CPTK_HAS_NOTHROW_COPY;
7165 case RID_HAS_TRIVIAL_ASSIGN:
7166 kind = CPTK_HAS_TRIVIAL_ASSIGN;
7168 case RID_HAS_TRIVIAL_CONSTRUCTOR:
7169 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7171 case RID_HAS_TRIVIAL_COPY:
7172 kind = CPTK_HAS_TRIVIAL_COPY;
7174 case RID_HAS_TRIVIAL_DESTRUCTOR:
7175 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7177 case RID_HAS_VIRTUAL_DESTRUCTOR:
7178 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7180 case RID_IS_ABSTRACT:
7181 kind = CPTK_IS_ABSTRACT;
7183 case RID_IS_BASE_OF:
7184 kind = CPTK_IS_BASE_OF;
7188 kind = CPTK_IS_CLASS;
7190 case RID_IS_CONVERTIBLE_TO:
7191 kind = CPTK_IS_CONVERTIBLE_TO;
7195 kind = CPTK_IS_EMPTY;
7198 kind = CPTK_IS_ENUM;
7200 case RID_IS_LITERAL_TYPE:
7201 kind = CPTK_IS_LITERAL_TYPE;
7206 case RID_IS_POLYMORPHIC:
7207 kind = CPTK_IS_POLYMORPHIC;
7209 case RID_IS_STD_LAYOUT:
7210 kind = CPTK_IS_STD_LAYOUT;
7212 case RID_IS_TRIVIAL:
7213 kind = CPTK_IS_TRIVIAL;
7216 kind = CPTK_IS_UNION;
7218 case RID_UNDERLYING_TYPE:
7219 kind = CPTK_UNDERLYING_TYPE;
7225 /* Consume the token. */
7226 cp_lexer_consume_token (parser->lexer);
7228 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7230 type1 = cp_parser_type_id (parser);
7232 if (type1 == error_mark_node)
7233 return error_mark_node;
7235 /* Build a trivial decl-specifier-seq. */
7236 clear_decl_specs (&decl_specs);
7237 decl_specs.type = type1;
7239 /* Call grokdeclarator to figure out what type this is. */
7240 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7241 /*initialized=*/0, /*attrlist=*/NULL);
7245 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7247 type2 = cp_parser_type_id (parser);
7249 if (type2 == error_mark_node)
7250 return error_mark_node;
7252 /* Build a trivial decl-specifier-seq. */
7253 clear_decl_specs (&decl_specs);
7254 decl_specs.type = type2;
7256 /* Call grokdeclarator to figure out what type this is. */
7257 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7258 /*initialized=*/0, /*attrlist=*/NULL);
7261 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7263 /* Complete the trait expression, which may mean either processing
7264 the trait expr now or saving it for template instantiation. */
7265 return kind != CPTK_UNDERLYING_TYPE
7266 ? finish_trait_expr (kind, type1, type2)
7267 : finish_underlying_type (type1);
7270 /* Lambdas that appear in variable initializer or default argument scope
7271 get that in their mangling, so we need to record it. We might as well
7272 use the count for function and namespace scopes as well. */
7273 static GTY(()) tree lambda_scope;
7274 static GTY(()) int lambda_count;
7275 typedef struct GTY(()) tree_int
7280 DEF_VEC_O(tree_int);
7281 DEF_VEC_ALLOC_O(tree_int,gc);
7282 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7285 start_lambda_scope (tree decl)
7289 /* Once we're inside a function, we ignore other scopes and just push
7290 the function again so that popping works properly. */
7291 if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7292 decl = current_function_decl;
7293 ti.t = lambda_scope;
7294 ti.i = lambda_count;
7295 VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7296 if (lambda_scope != decl)
7298 /* Don't reset the count if we're still in the same function. */
7299 lambda_scope = decl;
7305 record_lambda_scope (tree lambda)
7307 LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7308 LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7312 finish_lambda_scope (void)
7314 tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7315 if (lambda_scope != p->t)
7317 lambda_scope = p->t;
7318 lambda_count = p->i;
7320 VEC_pop (tree_int, lambda_scope_stack);
7323 /* Parse a lambda expression.
7326 lambda-introducer lambda-declarator [opt] compound-statement
7328 Returns a representation of the expression. */
7331 cp_parser_lambda_expression (cp_parser* parser)
7333 tree lambda_expr = build_lambda_expr ();
7336 LAMBDA_EXPR_LOCATION (lambda_expr)
7337 = cp_lexer_peek_token (parser->lexer)->location;
7339 if (cp_unevaluated_operand)
7340 error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7341 "lambda-expression in unevaluated context");
7343 /* We may be in the middle of deferred access check. Disable
7345 push_deferring_access_checks (dk_no_deferred);
7347 cp_parser_lambda_introducer (parser, lambda_expr);
7349 type = begin_lambda_type (lambda_expr);
7351 record_lambda_scope (lambda_expr);
7353 /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set. */
7354 determine_visibility (TYPE_NAME (type));
7356 /* Now that we've started the type, add the capture fields for any
7357 explicit captures. */
7358 register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7361 /* Inside the class, surrounding template-parameter-lists do not apply. */
7362 unsigned int saved_num_template_parameter_lists
7363 = parser->num_template_parameter_lists;
7365 parser->num_template_parameter_lists = 0;
7367 /* By virtue of defining a local class, a lambda expression has access to
7368 the private variables of enclosing classes. */
7370 cp_parser_lambda_declarator_opt (parser, lambda_expr);
7372 cp_parser_lambda_body (parser, lambda_expr);
7374 /* The capture list was built up in reverse order; fix that now. */
7376 tree newlist = NULL_TREE;
7379 for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7382 tree field = TREE_PURPOSE (elt);
7385 next = TREE_CHAIN (elt);
7386 TREE_CHAIN (elt) = newlist;
7389 /* Also add __ to the beginning of the field name so that code
7390 outside the lambda body can't see the captured name. We could
7391 just remove the name entirely, but this is more useful for
7393 if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7394 /* The 'this' capture already starts with __. */
7397 buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7398 buf[1] = buf[0] = '_';
7399 memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7400 IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7401 DECL_NAME (field) = get_identifier (buf);
7403 LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7406 maybe_add_lambda_conv_op (type);
7408 type = finish_struct (type, /*attributes=*/NULL_TREE);
7410 parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7413 pop_deferring_access_checks ();
7415 return build_lambda_object (lambda_expr);
7418 /* Parse the beginning of a lambda expression.
7421 [ lambda-capture [opt] ]
7423 LAMBDA_EXPR is the current representation of the lambda expression. */
7426 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7428 /* Need commas after the first capture. */
7431 /* Eat the leading `['. */
7432 cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7434 /* Record default capture mode. "[&" "[=" "[&," "[=," */
7435 if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7436 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7437 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7438 else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7439 LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7441 if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7443 cp_lexer_consume_token (parser->lexer);
7447 while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7449 cp_token* capture_token;
7451 tree capture_init_expr;
7452 cp_id_kind idk = CP_ID_KIND_NONE;
7453 bool explicit_init_p = false;
7455 enum capture_kind_type
7460 enum capture_kind_type capture_kind = BY_COPY;
7462 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7464 error ("expected end of capture-list");
7471 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7473 /* Possibly capture `this'. */
7474 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7476 cp_lexer_consume_token (parser->lexer);
7477 add_capture (lambda_expr,
7478 /*id=*/get_identifier ("__this"),
7479 /*initializer=*/finish_this_expr(),
7480 /*by_reference_p=*/false,
7485 /* Remember whether we want to capture as a reference or not. */
7486 if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7488 capture_kind = BY_REFERENCE;
7489 cp_lexer_consume_token (parser->lexer);
7492 /* Get the identifier. */
7493 capture_token = cp_lexer_peek_token (parser->lexer);
7494 capture_id = cp_parser_identifier (parser);
7496 if (capture_id == error_mark_node)
7497 /* Would be nice to have a cp_parser_skip_to_closing_x for general
7498 delimiters, but I modified this to stop on unnested ']' as well. It
7499 was already changed to stop on unnested '}', so the
7500 "closing_parenthesis" name is no more misleading with my change. */
7502 cp_parser_skip_to_closing_parenthesis (parser,
7503 /*recovering=*/true,
7505 /*consume_paren=*/true);
7509 /* Find the initializer for this capture. */
7510 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7512 /* An explicit expression exists. */
7513 cp_lexer_consume_token (parser->lexer);
7514 pedwarn (input_location, OPT_pedantic,
7515 "ISO C++ does not allow initializers "
7516 "in lambda expression capture lists");
7517 capture_init_expr = cp_parser_assignment_expression (parser,
7520 explicit_init_p = true;
7524 const char* error_msg;
7526 /* Turn the identifier into an id-expression. */
7528 = cp_parser_lookup_name
7532 /*is_template=*/false,
7533 /*is_namespace=*/false,
7534 /*check_dependency=*/true,
7535 /*ambiguous_decls=*/NULL,
7536 capture_token->location);
7539 = finish_id_expression
7544 /*integral_constant_expression_p=*/false,
7545 /*allow_non_integral_constant_expression_p=*/false,
7546 /*non_integral_constant_expression_p=*/NULL,
7547 /*template_p=*/false,
7549 /*address_p=*/false,
7550 /*template_arg_p=*/false,
7552 capture_token->location);
7555 if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7557 = unqualified_name_lookup_error (capture_init_expr);
7559 add_capture (lambda_expr,
7562 /*by_reference_p=*/capture_kind == BY_REFERENCE,
7566 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7569 /* Parse the (optional) middle of a lambda expression.
7572 ( parameter-declaration-clause [opt] )
7573 attribute-specifier [opt]
7575 exception-specification [opt]
7576 lambda-return-type-clause [opt]
7578 LAMBDA_EXPR is the current representation of the lambda expression. */
7581 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7583 /* 5.1.1.4 of the standard says:
7584 If a lambda-expression does not include a lambda-declarator, it is as if
7585 the lambda-declarator were ().
7586 This means an empty parameter list, no attributes, and no exception
7588 tree param_list = void_list_node;
7589 tree attributes = NULL_TREE;
7590 tree exception_spec = NULL_TREE;
7593 /* The lambda-declarator is optional, but must begin with an opening
7594 parenthesis if present. */
7595 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7597 cp_lexer_consume_token (parser->lexer);
7599 begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7601 /* Parse parameters. */
7602 param_list = cp_parser_parameter_declaration_clause (parser);
7604 /* Default arguments shall not be specified in the
7605 parameter-declaration-clause of a lambda-declarator. */
7606 for (t = param_list; t; t = TREE_CHAIN (t))
7607 if (TREE_PURPOSE (t))
7608 pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7609 "default argument specified for lambda parameter");
7611 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7613 attributes = cp_parser_attributes_opt (parser);
7615 /* Parse optional `mutable' keyword. */
7616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7618 cp_lexer_consume_token (parser->lexer);
7619 LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7622 /* Parse optional exception specification. */
7623 exception_spec = cp_parser_exception_specification_opt (parser);
7625 /* Parse optional trailing return type. */
7626 if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7628 cp_lexer_consume_token (parser->lexer);
7629 LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7632 /* The function parameters must be in scope all the way until after the
7633 trailing-return-type in case of decltype. */
7634 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7635 pop_binding (DECL_NAME (t), t);
7640 /* Create the function call operator.
7642 Messing with declarators like this is no uglier than building up the
7643 FUNCTION_DECL by hand, and this is less likely to get out of sync with
7646 cp_decl_specifier_seq return_type_specs;
7647 cp_declarator* declarator;
7652 clear_decl_specs (&return_type_specs);
7653 if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7654 return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7656 /* Maybe we will deduce the return type later, but we can use void
7657 as a placeholder return type anyways. */
7658 return_type_specs.type = void_type_node;
7660 p = obstack_alloc (&declarator_obstack, 0);
7662 declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7665 quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7666 ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7667 declarator = make_call_declarator (declarator, param_list, quals,
7669 /*late_return_type=*/NULL_TREE);
7670 declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7672 fco = grokmethod (&return_type_specs,
7675 DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7676 DECL_ARTIFICIAL (fco) = 1;
7678 finish_member_declaration (fco);
7680 obstack_free (&declarator_obstack, p);
7684 /* Parse the body of a lambda expression, which is simply
7688 but which requires special handling.
7689 LAMBDA_EXPR is the current representation of the lambda expression. */
7692 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7694 bool nested = (current_function_decl != NULL_TREE);
7696 push_function_context ();
7698 /* Finish the function call operator
7700 + late_parsing_for_member
7701 + function_definition_after_declarator
7702 + ctor_initializer_opt_and_function_body */
7704 tree fco = lambda_function (lambda_expr);
7708 /* Let the front end know that we are going to be defining this
7710 start_preparsed_function (fco,
7712 SF_PRE_PARSED | SF_INCLASS_INLINE);
7714 start_lambda_scope (fco);
7715 body = begin_function_body ();
7717 /* 5.1.1.4 of the standard says:
7718 If a lambda-expression does not include a trailing-return-type, it
7719 is as if the trailing-return-type denotes the following type:
7720 * if the compound-statement is of the form
7721 { return attribute-specifier [opt] expression ; }
7722 the type of the returned expression after lvalue-to-rvalue
7723 conversion (_conv.lval_ 4.1), array-to-pointer conversion
7724 (_conv.array_ 4.2), and function-to-pointer conversion
7726 * otherwise, void. */
7728 /* In a lambda that has neither a lambda-return-type-clause
7729 nor a deducible form, errors should be reported for return statements
7730 in the body. Since we used void as the placeholder return type, parsing
7731 the body as usual will give such desired behavior. */
7732 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7733 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7734 && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7735 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7738 tree expr = NULL_TREE;
7739 cp_id_kind idk = CP_ID_KIND_NONE;
7741 /* Parse tentatively in case there's more after the initial return
7743 cp_parser_parse_tentatively (parser);
7745 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7746 cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7748 expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7750 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7751 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7753 if (cp_parser_parse_definitely (parser))
7755 apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7757 compound_stmt = begin_compound_stmt (0);
7758 /* Will get error here if type not deduced yet. */
7759 finish_return_stmt (expr);
7760 finish_compound_stmt (compound_stmt);
7768 if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7769 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7770 /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7771 cp_parser_compound_stmt does not pass it. */
7772 cp_parser_function_body (parser);
7773 LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7776 finish_function_body (body);
7777 finish_lambda_scope ();
7779 /* Finish the function and generate code for it if necessary. */
7780 expand_or_defer_fn (finish_function (/*inline*/2));
7784 pop_function_context();
7787 /* Statements [gram.stmt.stmt] */
7789 /* Parse a statement.
7793 expression-statement
7798 declaration-statement
7801 IN_COMPOUND is true when the statement is nested inside a
7802 cp_parser_compound_statement; this matters for certain pragmas.
7804 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7805 is a (possibly labeled) if statement which is not enclosed in braces
7806 and has an else clause. This is used to implement -Wparentheses. */
7809 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7810 bool in_compound, bool *if_p)
7814 location_t statement_location;
7819 /* There is no statement yet. */
7820 statement = NULL_TREE;
7821 /* Peek at the next token. */
7822 token = cp_lexer_peek_token (parser->lexer);
7823 /* Remember the location of the first token in the statement. */
7824 statement_location = token->location;
7825 /* If this is a keyword, then that will often determine what kind of
7826 statement we have. */
7827 if (token->type == CPP_KEYWORD)
7829 enum rid keyword = token->keyword;
7835 /* Looks like a labeled-statement with a case label.
7836 Parse the label, and then use tail recursion to parse
7838 cp_parser_label_for_labeled_statement (parser);
7843 statement = cp_parser_selection_statement (parser, if_p);
7849 statement = cp_parser_iteration_statement (parser);
7856 statement = cp_parser_jump_statement (parser);
7859 /* Objective-C++ exception-handling constructs. */
7862 case RID_AT_FINALLY:
7863 case RID_AT_SYNCHRONIZED:
7865 statement = cp_parser_objc_statement (parser);
7869 statement = cp_parser_try_block (parser);
7873 /* This must be a namespace alias definition. */
7874 cp_parser_declaration_statement (parser);
7878 /* It might be a keyword like `int' that can start a
7879 declaration-statement. */
7883 else if (token->type == CPP_NAME)
7885 /* If the next token is a `:', then we are looking at a
7886 labeled-statement. */
7887 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7888 if (token->type == CPP_COLON)
7890 /* Looks like a labeled-statement with an ordinary label.
7891 Parse the label, and then use tail recursion to parse
7893 cp_parser_label_for_labeled_statement (parser);
7897 /* Anything that starts with a `{' must be a compound-statement. */
7898 else if (token->type == CPP_OPEN_BRACE)
7899 statement = cp_parser_compound_statement (parser, NULL, false, false);
7900 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7901 a statement all its own. */
7902 else if (token->type == CPP_PRAGMA)
7904 /* Only certain OpenMP pragmas are attached to statements, and thus
7905 are considered statements themselves. All others are not. In
7906 the context of a compound, accept the pragma as a "statement" and
7907 return so that we can check for a close brace. Otherwise we
7908 require a real statement and must go back and read one. */
7910 cp_parser_pragma (parser, pragma_compound);
7911 else if (!cp_parser_pragma (parser, pragma_stmt))
7915 else if (token->type == CPP_EOF)
7917 cp_parser_error (parser, "expected statement");
7921 /* Everything else must be a declaration-statement or an
7922 expression-statement. Try for the declaration-statement
7923 first, unless we are looking at a `;', in which case we know that
7924 we have an expression-statement. */
7927 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7929 cp_parser_parse_tentatively (parser);
7930 /* Try to parse the declaration-statement. */
7931 cp_parser_declaration_statement (parser);
7932 /* If that worked, we're done. */
7933 if (cp_parser_parse_definitely (parser))
7936 /* Look for an expression-statement instead. */
7937 statement = cp_parser_expression_statement (parser, in_statement_expr);
7940 /* Set the line number for the statement. */
7941 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7942 SET_EXPR_LOCATION (statement, statement_location);
7945 /* Parse the label for a labeled-statement, i.e.
7948 case constant-expression :
7952 case constant-expression ... constant-expression : statement
7954 When a label is parsed without errors, the label is added to the
7955 parse tree by the finish_* functions, so this function doesn't
7956 have to return the label. */
7959 cp_parser_label_for_labeled_statement (cp_parser* parser)
7962 tree label = NULL_TREE;
7963 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7965 /* The next token should be an identifier. */
7966 token = cp_lexer_peek_token (parser->lexer);
7967 if (token->type != CPP_NAME
7968 && token->type != CPP_KEYWORD)
7970 cp_parser_error (parser, "expected labeled-statement");
7974 parser->colon_corrects_to_scope_p = false;
7975 switch (token->keyword)
7982 /* Consume the `case' token. */
7983 cp_lexer_consume_token (parser->lexer);
7984 /* Parse the constant-expression. */
7985 expr = cp_parser_constant_expression (parser,
7986 /*allow_non_constant_p=*/false,
7989 ellipsis = cp_lexer_peek_token (parser->lexer);
7990 if (ellipsis->type == CPP_ELLIPSIS)
7992 /* Consume the `...' token. */
7993 cp_lexer_consume_token (parser->lexer);
7995 cp_parser_constant_expression (parser,
7996 /*allow_non_constant_p=*/false,
7998 /* We don't need to emit warnings here, as the common code
7999 will do this for us. */
8002 expr_hi = NULL_TREE;
8004 if (parser->in_switch_statement_p)
8005 finish_case_label (token->location, expr, expr_hi);
8007 error_at (token->location,
8008 "case label %qE not within a switch statement",
8014 /* Consume the `default' token. */
8015 cp_lexer_consume_token (parser->lexer);
8017 if (parser->in_switch_statement_p)
8018 finish_case_label (token->location, NULL_TREE, NULL_TREE);
8020 error_at (token->location, "case label not within a switch statement");
8024 /* Anything else must be an ordinary label. */
8025 label = finish_label_stmt (cp_parser_identifier (parser));
8029 /* Require the `:' token. */
8030 cp_parser_require (parser, CPP_COLON, RT_COLON);
8032 /* An ordinary label may optionally be followed by attributes.
8033 However, this is only permitted if the attributes are then
8034 followed by a semicolon. This is because, for backward
8035 compatibility, when parsing
8036 lab: __attribute__ ((unused)) int i;
8037 we want the attribute to attach to "i", not "lab". */
8038 if (label != NULL_TREE
8039 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8043 cp_parser_parse_tentatively (parser);
8044 attrs = cp_parser_attributes_opt (parser);
8045 if (attrs == NULL_TREE
8046 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8047 cp_parser_abort_tentative_parse (parser);
8048 else if (!cp_parser_parse_definitely (parser))
8051 cplus_decl_attributes (&label, attrs, 0);
8054 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8057 /* Parse an expression-statement.
8059 expression-statement:
8062 Returns the new EXPR_STMT -- or NULL_TREE if the expression
8063 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8064 indicates whether this expression-statement is part of an
8065 expression statement. */
8068 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8070 tree statement = NULL_TREE;
8071 cp_token *token = cp_lexer_peek_token (parser->lexer);
8073 /* If the next token is a ';', then there is no expression
8075 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8076 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8078 /* Give a helpful message for "A<T>::type t;" and the like. */
8079 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8080 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8082 if (TREE_CODE (statement) == SCOPE_REF)
8083 error_at (token->location, "need %<typename%> before %qE because "
8084 "%qT is a dependent scope",
8085 statement, TREE_OPERAND (statement, 0));
8086 else if (is_overloaded_fn (statement)
8087 && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8090 tree fn = get_first_fn (statement);
8091 error_at (token->location,
8092 "%<%T::%D%> names the constructor, not the type",
8093 DECL_CONTEXT (fn), DECL_NAME (fn));
8097 /* Consume the final `;'. */
8098 cp_parser_consume_semicolon_at_end_of_statement (parser);
8100 if (in_statement_expr
8101 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8102 /* This is the final expression statement of a statement
8104 statement = finish_stmt_expr_expr (statement, in_statement_expr);
8106 statement = finish_expr_stmt (statement);
8113 /* Parse a compound-statement.
8116 { statement-seq [opt] }
8121 { label-declaration-seq [opt] statement-seq [opt] }
8123 label-declaration-seq:
8125 label-declaration-seq label-declaration
8127 Returns a tree representing the statement. */
8130 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8131 bool in_try, bool function_body)
8135 /* Consume the `{'. */
8136 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8137 return error_mark_node;
8138 if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8140 pedwarn (input_location, OPT_pedantic,
8141 "compound-statement in constexpr function");
8142 /* Begin the compound-statement. */
8143 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8144 /* If the next keyword is `__label__' we have a label declaration. */
8145 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8146 cp_parser_label_declaration (parser);
8147 /* Parse an (optional) statement-seq. */
8148 cp_parser_statement_seq_opt (parser, in_statement_expr);
8149 /* Finish the compound-statement. */
8150 finish_compound_stmt (compound_stmt);
8151 /* Consume the `}'. */
8152 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8154 return compound_stmt;
8157 /* Parse an (optional) statement-seq.
8161 statement-seq [opt] statement */
8164 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8166 /* Scan statements until there aren't any more. */
8169 cp_token *token = cp_lexer_peek_token (parser->lexer);
8171 /* If we are looking at a `}', then we have run out of
8172 statements; the same is true if we have reached the end
8173 of file, or have stumbled upon a stray '@end'. */
8174 if (token->type == CPP_CLOSE_BRACE
8175 || token->type == CPP_EOF
8176 || token->type == CPP_PRAGMA_EOL
8177 || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8180 /* If we are in a compound statement and find 'else' then
8181 something went wrong. */
8182 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8184 if (parser->in_statement & IN_IF_STMT)
8188 token = cp_lexer_consume_token (parser->lexer);
8189 error_at (token->location, "%<else%> without a previous %<if%>");
8193 /* Parse the statement. */
8194 cp_parser_statement (parser, in_statement_expr, true, NULL);
8198 /* Parse a selection-statement.
8200 selection-statement:
8201 if ( condition ) statement
8202 if ( condition ) statement else statement
8203 switch ( condition ) statement
8205 Returns the new IF_STMT or SWITCH_STMT.
8207 If IF_P is not NULL, *IF_P is set to indicate whether the statement
8208 is a (possibly labeled) if statement which is not enclosed in
8209 braces and has an else clause. This is used to implement
8213 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8221 /* Peek at the next token. */
8222 token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8224 /* See what kind of keyword it is. */
8225 keyword = token->keyword;
8234 /* Look for the `('. */
8235 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8237 cp_parser_skip_to_end_of_statement (parser);
8238 return error_mark_node;
8241 /* Begin the selection-statement. */
8242 if (keyword == RID_IF)
8243 statement = begin_if_stmt ();
8245 statement = begin_switch_stmt ();
8247 /* Parse the condition. */
8248 condition = cp_parser_condition (parser);
8249 /* Look for the `)'. */
8250 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8251 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8252 /*consume_paren=*/true);
8254 if (keyword == RID_IF)
8257 unsigned char in_statement;
8259 /* Add the condition. */
8260 finish_if_stmt_cond (condition, statement);
8262 /* Parse the then-clause. */
8263 in_statement = parser->in_statement;
8264 parser->in_statement |= IN_IF_STMT;
8265 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8267 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8268 add_stmt (build_empty_stmt (loc));
8269 cp_lexer_consume_token (parser->lexer);
8270 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8271 warning_at (loc, OPT_Wempty_body, "suggest braces around "
8272 "empty body in an %<if%> statement");
8276 cp_parser_implicitly_scoped_statement (parser, &nested_if);
8277 parser->in_statement = in_statement;
8279 finish_then_clause (statement);
8281 /* If the next token is `else', parse the else-clause. */
8282 if (cp_lexer_next_token_is_keyword (parser->lexer,
8285 /* Consume the `else' keyword. */
8286 cp_lexer_consume_token (parser->lexer);
8287 begin_else_clause (statement);
8288 /* Parse the else-clause. */
8289 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8292 loc = cp_lexer_peek_token (parser->lexer)->location;
8294 OPT_Wempty_body, "suggest braces around "
8295 "empty body in an %<else%> statement");
8296 add_stmt (build_empty_stmt (loc));
8297 cp_lexer_consume_token (parser->lexer);
8300 cp_parser_implicitly_scoped_statement (parser, NULL);
8302 finish_else_clause (statement);
8304 /* If we are currently parsing a then-clause, then
8305 IF_P will not be NULL. We set it to true to
8306 indicate that this if statement has an else clause.
8307 This may trigger the Wparentheses warning below
8308 when we get back up to the parent if statement. */
8314 /* This if statement does not have an else clause. If
8315 NESTED_IF is true, then the then-clause is an if
8316 statement which does have an else clause. We warn
8317 about the potential ambiguity. */
8319 warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8320 "suggest explicit braces to avoid ambiguous"
8324 /* Now we're all done with the if-statement. */
8325 finish_if_stmt (statement);
8329 bool in_switch_statement_p;
8330 unsigned char in_statement;
8332 /* Add the condition. */
8333 finish_switch_cond (condition, statement);
8335 /* Parse the body of the switch-statement. */
8336 in_switch_statement_p = parser->in_switch_statement_p;
8337 in_statement = parser->in_statement;
8338 parser->in_switch_statement_p = true;
8339 parser->in_statement |= IN_SWITCH_STMT;
8340 cp_parser_implicitly_scoped_statement (parser, NULL);
8341 parser->in_switch_statement_p = in_switch_statement_p;
8342 parser->in_statement = in_statement;
8344 /* Now we're all done with the switch-statement. */
8345 finish_switch_stmt (statement);
8353 cp_parser_error (parser, "expected selection-statement");
8354 return error_mark_node;
8358 /* Parse a condition.
8362 type-specifier-seq declarator = initializer-clause
8363 type-specifier-seq declarator braced-init-list
8368 type-specifier-seq declarator asm-specification [opt]
8369 attributes [opt] = assignment-expression
8371 Returns the expression that should be tested. */
8374 cp_parser_condition (cp_parser* parser)
8376 cp_decl_specifier_seq type_specifiers;
8377 const char *saved_message;
8378 int declares_class_or_enum;
8380 /* Try the declaration first. */
8381 cp_parser_parse_tentatively (parser);
8382 /* New types are not allowed in the type-specifier-seq for a
8384 saved_message = parser->type_definition_forbidden_message;
8385 parser->type_definition_forbidden_message
8386 = G_("types may not be defined in conditions");
8387 /* Parse the type-specifier-seq. */
8388 cp_parser_decl_specifier_seq (parser,
8389 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8391 &declares_class_or_enum);
8392 /* Restore the saved message. */
8393 parser->type_definition_forbidden_message = saved_message;
8394 /* If all is well, we might be looking at a declaration. */
8395 if (!cp_parser_error_occurred (parser))
8398 tree asm_specification;
8400 cp_declarator *declarator;
8401 tree initializer = NULL_TREE;
8403 /* Parse the declarator. */
8404 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8405 /*ctor_dtor_or_conv_p=*/NULL,
8406 /*parenthesized_p=*/NULL,
8407 /*member_p=*/false);
8408 /* Parse the attributes. */
8409 attributes = cp_parser_attributes_opt (parser);
8410 /* Parse the asm-specification. */
8411 asm_specification = cp_parser_asm_specification_opt (parser);
8412 /* If the next token is not an `=' or '{', then we might still be
8413 looking at an expression. For example:
8417 looks like a decl-specifier-seq and a declarator -- but then
8418 there is no `=', so this is an expression. */
8419 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8420 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8421 cp_parser_simulate_error (parser);
8423 /* If we did see an `=' or '{', then we are looking at a declaration
8425 if (cp_parser_parse_definitely (parser))
8428 bool non_constant_p;
8429 bool flags = LOOKUP_ONLYCONVERTING;
8431 /* Create the declaration. */
8432 decl = start_decl (declarator, &type_specifiers,
8433 /*initialized_p=*/true,
8434 attributes, /*prefix_attributes=*/NULL_TREE,
8437 /* Parse the initializer. */
8438 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8440 initializer = cp_parser_braced_list (parser, &non_constant_p);
8441 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8446 /* Consume the `='. */
8447 cp_parser_require (parser, CPP_EQ, RT_EQ);
8448 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8450 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8451 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8453 /* Process the initializer. */
8454 cp_finish_decl (decl,
8455 initializer, !non_constant_p,
8460 pop_scope (pushed_scope);
8462 return convert_from_reference (decl);
8465 /* If we didn't even get past the declarator successfully, we are
8466 definitely not looking at a declaration. */
8468 cp_parser_abort_tentative_parse (parser);
8470 /* Otherwise, we are looking at an expression. */
8471 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8474 /* Parses a for-statement or range-for-statement until the closing ')',
8478 cp_parser_for (cp_parser *parser)
8480 tree init, scope, decl;
8483 /* Begin the for-statement. */
8484 scope = begin_for_scope (&init);
8486 /* Parse the initialization. */
8487 is_range_for = cp_parser_for_init_statement (parser, &decl);
8490 return cp_parser_range_for (parser, scope, init, decl);
8492 return cp_parser_c_for (parser, scope, init);
8496 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8498 /* Normal for loop */
8499 tree condition = NULL_TREE;
8500 tree expression = NULL_TREE;
8503 stmt = begin_for_stmt (scope, init);
8504 /* The for-init-statement has already been parsed in
8505 cp_parser_for_init_statement, so no work is needed here. */
8506 finish_for_init_stmt (stmt);
8508 /* If there's a condition, process it. */
8509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8510 condition = cp_parser_condition (parser);
8511 finish_for_cond (condition, stmt);
8512 /* Look for the `;'. */
8513 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8515 /* If there's an expression, process it. */
8516 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8517 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8518 finish_for_expr (expression, stmt);
8523 /* Tries to parse a range-based for-statement:
8526 decl-specifier-seq declarator : expression
8528 The decl-specifier-seq declarator and the `:' are already parsed by
8529 cp_parser_for_init_statement. If processing_template_decl it returns a
8530 newly created RANGE_FOR_STMT; if not, it is converted to a
8531 regular FOR_STMT. */
8534 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8536 tree stmt, range_expr;
8538 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8540 bool expr_non_constant_p;
8541 range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8544 range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8546 /* If in template, STMT is converted to a normal for-statement
8547 at instantiation. If not, it is done just ahead. */
8548 if (processing_template_decl)
8550 stmt = begin_range_for_stmt (scope, init);
8551 finish_range_for_decl (stmt, range_decl, range_expr);
8555 stmt = begin_for_stmt (scope, init);
8556 stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8561 /* Converts a range-based for-statement into a normal
8562 for-statement, as per the definition.
8564 for (RANGE_DECL : RANGE_EXPR)
8567 should be equivalent to:
8570 auto &&__range = RANGE_EXPR;
8571 for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8575 RANGE_DECL = *__begin;
8580 If RANGE_EXPR is an array:
8581 BEGIN_EXPR = __range
8582 END_EXPR = __range + ARRAY_SIZE(__range)
8583 Else if RANGE_EXPR has a member 'begin' or 'end':
8584 BEGIN_EXPR = __range.begin()
8585 END_EXPR = __range.end()
8587 BEGIN_EXPR = begin(__range)
8588 END_EXPR = end(__range);
8590 If __range has a member 'begin' but not 'end', or vice versa, we must
8591 still use the second alternative (it will surely fail, however).
8592 When calling begin()/end() in the third alternative we must use
8593 argument dependent lookup, but always considering 'std' as an associated
8597 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8599 tree range_type, range_temp;
8601 tree iter_type, begin_expr, end_expr;
8602 tree condition, expression;
8604 if (range_decl == error_mark_node || range_expr == error_mark_node)
8605 /* If an error happened previously do nothing or else a lot of
8606 unhelpful errors would be issued. */
8607 begin_expr = end_expr = iter_type = error_mark_node;
8610 /* Find out the type deduced by the declaration
8611 `auto &&__range = range_expr'. */
8612 range_type = cp_build_reference_type (make_auto (), true);
8613 range_type = do_auto_deduction (range_type, range_expr,
8614 type_uses_auto (range_type));
8616 /* Create the __range variable. */
8617 range_temp = build_decl (input_location, VAR_DECL,
8618 get_identifier ("__for_range"), range_type);
8619 TREE_USED (range_temp) = 1;
8620 DECL_ARTIFICIAL (range_temp) = 1;
8621 pushdecl (range_temp);
8622 cp_finish_decl (range_temp, range_expr,
8623 /*is_constant_init*/false, NULL_TREE,
8624 LOOKUP_ONLYCONVERTING);
8626 range_temp = convert_from_reference (range_temp);
8627 iter_type = cp_parser_perform_range_for_lookup (range_temp,
8628 &begin_expr, &end_expr);
8631 /* The new for initialization statement. */
8632 begin = build_decl (input_location, VAR_DECL,
8633 get_identifier ("__for_begin"), iter_type);
8634 TREE_USED (begin) = 1;
8635 DECL_ARTIFICIAL (begin) = 1;
8637 cp_finish_decl (begin, begin_expr,
8638 /*is_constant_init*/false, NULL_TREE,
8639 LOOKUP_ONLYCONVERTING);
8641 end = build_decl (input_location, VAR_DECL,
8642 get_identifier ("__for_end"), iter_type);
8643 TREE_USED (end) = 1;
8644 DECL_ARTIFICIAL (end) = 1;
8646 cp_finish_decl (end, end_expr,
8647 /*is_constant_init*/false, NULL_TREE,
8648 LOOKUP_ONLYCONVERTING);
8650 finish_for_init_stmt (statement);
8652 /* The new for condition. */
8653 condition = build_x_binary_op (NE_EXPR,
8656 NULL, tf_warning_or_error);
8657 finish_for_cond (condition, statement);
8659 /* The new increment expression. */
8660 expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8661 finish_for_expr (expression, statement);
8663 /* The declaration is initialized with *__begin inside the loop body. */
8664 cp_finish_decl (range_decl,
8665 build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8666 /*is_constant_init*/false, NULL_TREE,
8667 LOOKUP_ONLYCONVERTING);
8672 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8673 We need to solve both at the same time because the method used
8674 depends on the existence of members begin or end.
8675 Returns the type deduced for the iterator expression. */
8678 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8680 if (!COMPLETE_TYPE_P (TREE_TYPE (range)))
8682 error ("range-based %<for%> expression of type %qT "
8683 "has incomplete type", TREE_TYPE (range));
8684 *begin = *end = error_mark_node;
8685 return error_mark_node;
8687 if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8689 /* If RANGE is an array, we will use pointer arithmetic. */
8691 *end = build_binary_op (input_location, PLUS_EXPR,
8693 array_type_nelts_top (TREE_TYPE (range)),
8695 return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8699 /* If it is not an array, we must do a bit of magic. */
8700 tree id_begin, id_end;
8701 tree member_begin, member_end;
8703 *begin = *end = error_mark_node;
8705 id_begin = get_identifier ("begin");
8706 id_end = get_identifier ("end");
8707 member_begin = lookup_member (TREE_TYPE (range), id_begin,
8708 /*protect=*/2, /*want_type=*/false);
8709 member_end = lookup_member (TREE_TYPE (range), id_end,
8710 /*protect=*/2, /*want_type=*/false);
8712 if (member_begin != NULL_TREE || member_end != NULL_TREE)
8714 /* Use the member functions. */
8715 if (member_begin != NULL_TREE)
8716 *begin = cp_parser_range_for_member_function (range, id_begin);
8718 error ("range-based %<for%> expression of type %qT has an "
8719 "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8721 if (member_end != NULL_TREE)
8722 *end = cp_parser_range_for_member_function (range, id_end);
8724 error ("range-based %<for%> expression of type %qT has a "
8725 "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8729 /* Use global functions with ADL. */
8731 vec = make_tree_vector ();
8733 VEC_safe_push (tree, gc, vec, range);
8735 member_begin = perform_koenig_lookup (id_begin, vec,
8736 /*include_std=*/true);
8737 *begin = finish_call_expr (member_begin, &vec, false, true,
8738 tf_warning_or_error);
8739 member_end = perform_koenig_lookup (id_end, vec,
8740 /*include_std=*/true);
8741 *end = finish_call_expr (member_end, &vec, false, true,
8742 tf_warning_or_error);
8744 release_tree_vector (vec);
8747 /* Last common checks. */
8748 if (*begin == error_mark_node || *end == error_mark_node)
8750 /* If one of the expressions is an error do no more checks. */
8751 *begin = *end = error_mark_node;
8752 return error_mark_node;
8756 tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8757 /* The unqualified type of the __begin and __end temporaries should
8758 be the same, as required by the multiple auto declaration. */
8759 if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8760 error ("inconsistent begin/end types in range-based %<for%> "
8761 "statement: %qT and %qT",
8762 TREE_TYPE (*begin), TREE_TYPE (*end));
8768 /* Helper function for cp_parser_perform_range_for_lookup.
8769 Builds a tree for RANGE.IDENTIFIER(). */
8772 cp_parser_range_for_member_function (tree range, tree identifier)
8777 member = finish_class_member_access_expr (range, identifier,
8778 false, tf_warning_or_error);
8779 if (member == error_mark_node)
8780 return error_mark_node;
8782 vec = make_tree_vector ();
8783 res = finish_call_expr (member, &vec,
8784 /*disallow_virtual=*/false,
8786 tf_warning_or_error);
8787 release_tree_vector (vec);
8791 /* Parse an iteration-statement.
8793 iteration-statement:
8794 while ( condition ) statement
8795 do statement while ( expression ) ;
8796 for ( for-init-statement condition [opt] ; expression [opt] )
8799 Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT. */
8802 cp_parser_iteration_statement (cp_parser* parser)
8807 unsigned char in_statement;
8809 /* Peek at the next token. */
8810 token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8812 return error_mark_node;
8814 /* Remember whether or not we are already within an iteration
8816 in_statement = parser->in_statement;
8818 /* See what kind of keyword it is. */
8819 keyword = token->keyword;
8826 /* Begin the while-statement. */
8827 statement = begin_while_stmt ();
8828 /* Look for the `('. */
8829 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8830 /* Parse the condition. */
8831 condition = cp_parser_condition (parser);
8832 finish_while_stmt_cond (condition, statement);
8833 /* Look for the `)'. */
8834 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8835 /* Parse the dependent statement. */
8836 parser->in_statement = IN_ITERATION_STMT;
8837 cp_parser_already_scoped_statement (parser);
8838 parser->in_statement = in_statement;
8839 /* We're done with the while-statement. */
8840 finish_while_stmt (statement);
8848 /* Begin the do-statement. */
8849 statement = begin_do_stmt ();
8850 /* Parse the body of the do-statement. */
8851 parser->in_statement = IN_ITERATION_STMT;
8852 cp_parser_implicitly_scoped_statement (parser, NULL);
8853 parser->in_statement = in_statement;
8854 finish_do_body (statement);
8855 /* Look for the `while' keyword. */
8856 cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8857 /* Look for the `('. */
8858 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8859 /* Parse the expression. */
8860 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8861 /* We're done with the do-statement. */
8862 finish_do_stmt (expression, statement);
8863 /* Look for the `)'. */
8864 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8865 /* Look for the `;'. */
8866 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8872 /* Look for the `('. */
8873 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8875 statement = cp_parser_for (parser);
8877 /* Look for the `)'. */
8878 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8880 /* Parse the body of the for-statement. */
8881 parser->in_statement = IN_ITERATION_STMT;
8882 cp_parser_already_scoped_statement (parser);
8883 parser->in_statement = in_statement;
8885 /* We're done with the for-statement. */
8886 finish_for_stmt (statement);
8891 cp_parser_error (parser, "expected iteration-statement");
8892 statement = error_mark_node;
8899 /* Parse a for-init-statement or the declarator of a range-based-for.
8900 Returns true if a range-based-for declaration is seen.
8903 expression-statement
8904 simple-declaration */
8907 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8909 /* If the next token is a `;', then we have an empty
8910 expression-statement. Grammatically, this is also a
8911 simple-declaration, but an invalid one, because it does not
8912 declare anything. Therefore, if we did not handle this case
8913 specially, we would issue an error message about an invalid
8915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8917 bool is_range_for = false;
8918 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8920 parser->colon_corrects_to_scope_p = false;
8922 /* We're going to speculatively look for a declaration, falling back
8923 to an expression, if necessary. */
8924 cp_parser_parse_tentatively (parser);
8925 /* Parse the declaration. */
8926 cp_parser_simple_declaration (parser,
8927 /*function_definition_allowed_p=*/false,
8929 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8930 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8932 /* It is a range-for, consume the ':' */
8933 cp_lexer_consume_token (parser->lexer);
8934 is_range_for = true;
8935 if (cxx_dialect < cxx0x)
8937 error_at (cp_lexer_peek_token (parser->lexer)->location,
8938 "range-based %<for%> loops are not allowed "
8940 *decl = error_mark_node;
8944 /* The ';' is not consumed yet because we told
8945 cp_parser_simple_declaration not to. */
8946 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8948 if (cp_parser_parse_definitely (parser))
8949 return is_range_for;
8950 /* If the tentative parse failed, then we shall need to look for an
8951 expression-statement. */
8953 /* If we are here, it is an expression-statement. */
8954 cp_parser_expression_statement (parser, NULL_TREE);
8958 /* Parse a jump-statement.
8963 return expression [opt] ;
8964 return braced-init-list ;
8972 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
8975 cp_parser_jump_statement (cp_parser* parser)
8977 tree statement = error_mark_node;
8980 unsigned char in_statement;
8982 /* Peek at the next token. */
8983 token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8985 return error_mark_node;
8987 /* See what kind of keyword it is. */
8988 keyword = token->keyword;
8992 in_statement = parser->in_statement & ~IN_IF_STMT;
8993 switch (in_statement)
8996 error_at (token->location, "break statement not within loop or switch");
8999 gcc_assert ((in_statement & IN_SWITCH_STMT)
9000 || in_statement == IN_ITERATION_STMT);
9001 statement = finish_break_stmt ();
9004 error_at (token->location, "invalid exit from OpenMP structured block");
9007 error_at (token->location, "break statement used with OpenMP for loop");
9010 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9014 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9017 error_at (token->location, "continue statement not within a loop");
9019 case IN_ITERATION_STMT:
9021 statement = finish_continue_stmt ();
9024 error_at (token->location, "invalid exit from OpenMP structured block");
9029 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9035 bool expr_non_constant_p;
9037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9039 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9040 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9042 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9043 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9045 /* If the next token is a `;', then there is no
9048 /* Build the return-statement. */
9049 statement = finish_return_stmt (expr);
9050 /* Look for the final `;'. */
9051 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9056 /* Create the goto-statement. */
9057 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9059 /* Issue a warning about this use of a GNU extension. */
9060 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9061 /* Consume the '*' token. */
9062 cp_lexer_consume_token (parser->lexer);
9063 /* Parse the dependent expression. */
9064 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9067 finish_goto_stmt (cp_parser_identifier (parser));
9068 /* Look for the final `;'. */
9069 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9073 cp_parser_error (parser, "expected jump-statement");
9080 /* Parse a declaration-statement.
9082 declaration-statement:
9083 block-declaration */
9086 cp_parser_declaration_statement (cp_parser* parser)
9090 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9091 p = obstack_alloc (&declarator_obstack, 0);
9093 /* Parse the block-declaration. */
9094 cp_parser_block_declaration (parser, /*statement_p=*/true);
9096 /* Free any declarators allocated. */
9097 obstack_free (&declarator_obstack, p);
9099 /* Finish off the statement. */
9103 /* Some dependent statements (like `if (cond) statement'), are
9104 implicitly in their own scope. In other words, if the statement is
9105 a single statement (as opposed to a compound-statement), it is
9106 none-the-less treated as if it were enclosed in braces. Any
9107 declarations appearing in the dependent statement are out of scope
9108 after control passes that point. This function parses a statement,
9109 but ensures that is in its own scope, even if it is not a
9112 If IF_P is not NULL, *IF_P is set to indicate whether the statement
9113 is a (possibly labeled) if statement which is not enclosed in
9114 braces and has an else clause. This is used to implement
9117 Returns the new statement. */
9120 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9127 /* Mark if () ; with a special NOP_EXPR. */
9128 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9130 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9131 cp_lexer_consume_token (parser->lexer);
9132 statement = add_stmt (build_empty_stmt (loc));
9134 /* if a compound is opened, we simply parse the statement directly. */
9135 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9136 statement = cp_parser_compound_statement (parser, NULL, false, false);
9137 /* If the token is not a `{', then we must take special action. */
9140 /* Create a compound-statement. */
9141 statement = begin_compound_stmt (0);
9142 /* Parse the dependent-statement. */
9143 cp_parser_statement (parser, NULL_TREE, false, if_p);
9144 /* Finish the dummy compound-statement. */
9145 finish_compound_stmt (statement);
9148 /* Return the statement. */
9152 /* For some dependent statements (like `while (cond) statement'), we
9153 have already created a scope. Therefore, even if the dependent
9154 statement is a compound-statement, we do not want to create another
9158 cp_parser_already_scoped_statement (cp_parser* parser)
9160 /* If the token is a `{', then we must take special action. */
9161 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9162 cp_parser_statement (parser, NULL_TREE, false, NULL);
9165 /* Avoid calling cp_parser_compound_statement, so that we
9166 don't create a new scope. Do everything else by hand. */
9167 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9168 /* If the next keyword is `__label__' we have a label declaration. */
9169 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9170 cp_parser_label_declaration (parser);
9171 /* Parse an (optional) statement-seq. */
9172 cp_parser_statement_seq_opt (parser, NULL_TREE);
9173 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9177 /* Declarations [gram.dcl.dcl] */
9179 /* Parse an optional declaration-sequence.
9183 declaration-seq declaration */
9186 cp_parser_declaration_seq_opt (cp_parser* parser)
9192 token = cp_lexer_peek_token (parser->lexer);
9194 if (token->type == CPP_CLOSE_BRACE
9195 || token->type == CPP_EOF
9196 || token->type == CPP_PRAGMA_EOL)
9199 if (token->type == CPP_SEMICOLON)
9201 /* A declaration consisting of a single semicolon is
9202 invalid. Allow it unless we're being pedantic. */
9203 cp_lexer_consume_token (parser->lexer);
9204 if (!in_system_header)
9205 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9209 /* If we're entering or exiting a region that's implicitly
9210 extern "C", modify the lang context appropriately. */
9211 if (!parser->implicit_extern_c && token->implicit_extern_c)
9213 push_lang_context (lang_name_c);
9214 parser->implicit_extern_c = true;
9216 else if (parser->implicit_extern_c && !token->implicit_extern_c)
9218 pop_lang_context ();
9219 parser->implicit_extern_c = false;
9222 if (token->type == CPP_PRAGMA)
9224 /* A top-level declaration can consist solely of a #pragma.
9225 A nested declaration cannot, so this is done here and not
9226 in cp_parser_declaration. (A #pragma at block scope is
9227 handled in cp_parser_statement.) */
9228 cp_parser_pragma (parser, pragma_external);
9232 /* Parse the declaration itself. */
9233 cp_parser_declaration (parser);
9237 /* Parse a declaration.
9242 template-declaration
9243 explicit-instantiation
9244 explicit-specialization
9245 linkage-specification
9246 namespace-definition
9251 __extension__ declaration */
9254 cp_parser_declaration (cp_parser* parser)
9260 tree attributes = NULL_TREE;
9262 /* Check for the `__extension__' keyword. */
9263 if (cp_parser_extension_opt (parser, &saved_pedantic))
9265 /* Parse the qualified declaration. */
9266 cp_parser_declaration (parser);
9267 /* Restore the PEDANTIC flag. */
9268 pedantic = saved_pedantic;
9273 /* Try to figure out what kind of declaration is present. */
9274 token1 = *cp_lexer_peek_token (parser->lexer);
9276 if (token1.type != CPP_EOF)
9277 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9280 token2.type = CPP_EOF;
9281 token2.keyword = RID_MAX;
9284 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
9285 p = obstack_alloc (&declarator_obstack, 0);
9287 /* If the next token is `extern' and the following token is a string
9288 literal, then we have a linkage specification. */
9289 if (token1.keyword == RID_EXTERN
9290 && cp_parser_is_string_literal (&token2))
9291 cp_parser_linkage_specification (parser);
9292 /* If the next token is `template', then we have either a template
9293 declaration, an explicit instantiation, or an explicit
9295 else if (token1.keyword == RID_TEMPLATE)
9297 /* `template <>' indicates a template specialization. */
9298 if (token2.type == CPP_LESS
9299 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9300 cp_parser_explicit_specialization (parser);
9301 /* `template <' indicates a template declaration. */
9302 else if (token2.type == CPP_LESS)
9303 cp_parser_template_declaration (parser, /*member_p=*/false);
9304 /* Anything else must be an explicit instantiation. */
9306 cp_parser_explicit_instantiation (parser);
9308 /* If the next token is `export', then we have a template
9310 else if (token1.keyword == RID_EXPORT)
9311 cp_parser_template_declaration (parser, /*member_p=*/false);
9312 /* If the next token is `extern', 'static' or 'inline' and the one
9313 after that is `template', we have a GNU extended explicit
9314 instantiation directive. */
9315 else if (cp_parser_allow_gnu_extensions_p (parser)
9316 && (token1.keyword == RID_EXTERN
9317 || token1.keyword == RID_STATIC
9318 || token1.keyword == RID_INLINE)
9319 && token2.keyword == RID_TEMPLATE)
9320 cp_parser_explicit_instantiation (parser);
9321 /* If the next token is `namespace', check for a named or unnamed
9322 namespace definition. */
9323 else if (token1.keyword == RID_NAMESPACE
9324 && (/* A named namespace definition. */
9325 (token2.type == CPP_NAME
9326 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9328 /* An unnamed namespace definition. */
9329 || token2.type == CPP_OPEN_BRACE
9330 || token2.keyword == RID_ATTRIBUTE))
9331 cp_parser_namespace_definition (parser);
9332 /* An inline (associated) namespace definition. */
9333 else if (token1.keyword == RID_INLINE
9334 && token2.keyword == RID_NAMESPACE)
9335 cp_parser_namespace_definition (parser);
9336 /* Objective-C++ declaration/definition. */
9337 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9338 cp_parser_objc_declaration (parser, NULL_TREE);
9339 else if (c_dialect_objc ()
9340 && token1.keyword == RID_ATTRIBUTE
9341 && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9342 cp_parser_objc_declaration (parser, attributes);
9343 /* We must have either a block declaration or a function
9346 /* Try to parse a block-declaration, or a function-definition. */
9347 cp_parser_block_declaration (parser, /*statement_p=*/false);
9349 /* Free any declarators allocated. */
9350 obstack_free (&declarator_obstack, p);
9353 /* Parse a block-declaration.
9358 namespace-alias-definition
9365 __extension__ block-declaration
9370 static_assert-declaration
9372 If STATEMENT_P is TRUE, then this block-declaration is occurring as
9373 part of a declaration-statement. */
9376 cp_parser_block_declaration (cp_parser *parser,
9382 /* Check for the `__extension__' keyword. */
9383 if (cp_parser_extension_opt (parser, &saved_pedantic))
9385 /* Parse the qualified declaration. */
9386 cp_parser_block_declaration (parser, statement_p);
9387 /* Restore the PEDANTIC flag. */
9388 pedantic = saved_pedantic;
9393 /* Peek at the next token to figure out which kind of declaration is
9395 token1 = cp_lexer_peek_token (parser->lexer);
9397 /* If the next keyword is `asm', we have an asm-definition. */
9398 if (token1->keyword == RID_ASM)
9401 cp_parser_commit_to_tentative_parse (parser);
9402 cp_parser_asm_definition (parser);
9404 /* If the next keyword is `namespace', we have a
9405 namespace-alias-definition. */
9406 else if (token1->keyword == RID_NAMESPACE)
9407 cp_parser_namespace_alias_definition (parser);
9408 /* If the next keyword is `using', we have either a
9409 using-declaration or a using-directive. */
9410 else if (token1->keyword == RID_USING)
9415 cp_parser_commit_to_tentative_parse (parser);
9416 /* If the token after `using' is `namespace', then we have a
9418 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9419 if (token2->keyword == RID_NAMESPACE)
9420 cp_parser_using_directive (parser);
9421 /* Otherwise, it's a using-declaration. */
9423 cp_parser_using_declaration (parser,
9424 /*access_declaration_p=*/false);
9426 /* If the next keyword is `__label__' we have a misplaced label
9428 else if (token1->keyword == RID_LABEL)
9430 cp_lexer_consume_token (parser->lexer);
9431 error_at (token1->location, "%<__label__%> not at the beginning of a block");
9432 cp_parser_skip_to_end_of_statement (parser);
9433 /* If the next token is now a `;', consume it. */
9434 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9435 cp_lexer_consume_token (parser->lexer);
9437 /* If the next token is `static_assert' we have a static assertion. */
9438 else if (token1->keyword == RID_STATIC_ASSERT)
9439 cp_parser_static_assert (parser, /*member_p=*/false);
9440 /* Anything else must be a simple-declaration. */
9442 cp_parser_simple_declaration (parser, !statement_p,
9443 /*maybe_range_for_decl*/NULL);
9446 /* Parse a simple-declaration.
9449 decl-specifier-seq [opt] init-declarator-list [opt] ;
9451 init-declarator-list:
9453 init-declarator-list , init-declarator
9455 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9456 function-definition as a simple-declaration.
9458 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9459 parsed declaration if it is an uninitialized single declarator not followed
9460 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9461 if present, will not be consumed. */
9464 cp_parser_simple_declaration (cp_parser* parser,
9465 bool function_definition_allowed_p,
9466 tree *maybe_range_for_decl)
9468 cp_decl_specifier_seq decl_specifiers;
9469 int declares_class_or_enum;
9470 bool saw_declarator;
9472 if (maybe_range_for_decl)
9473 *maybe_range_for_decl = NULL_TREE;
9475 /* Defer access checks until we know what is being declared; the
9476 checks for names appearing in the decl-specifier-seq should be
9477 done as if we were in the scope of the thing being declared. */
9478 push_deferring_access_checks (dk_deferred);
9480 /* Parse the decl-specifier-seq. We have to keep track of whether
9481 or not the decl-specifier-seq declares a named class or
9482 enumeration type, since that is the only case in which the
9483 init-declarator-list is allowed to be empty.
9487 In a simple-declaration, the optional init-declarator-list can be
9488 omitted only when declaring a class or enumeration, that is when
9489 the decl-specifier-seq contains either a class-specifier, an
9490 elaborated-type-specifier, or an enum-specifier. */
9491 cp_parser_decl_specifier_seq (parser,
9492 CP_PARSER_FLAGS_OPTIONAL,
9494 &declares_class_or_enum);
9495 /* We no longer need to defer access checks. */
9496 stop_deferring_access_checks ();
9498 /* In a block scope, a valid declaration must always have a
9499 decl-specifier-seq. By not trying to parse declarators, we can
9500 resolve the declaration/expression ambiguity more quickly. */
9501 if (!function_definition_allowed_p
9502 && !decl_specifiers.any_specifiers_p)
9504 cp_parser_error (parser, "expected declaration");
9508 /* If the next two tokens are both identifiers, the code is
9509 erroneous. The usual cause of this situation is code like:
9513 where "T" should name a type -- but does not. */
9514 if (!decl_specifiers.any_type_specifiers_p
9515 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9517 /* If parsing tentatively, we should commit; we really are
9518 looking at a declaration. */
9519 cp_parser_commit_to_tentative_parse (parser);
9524 /* If we have seen at least one decl-specifier, and the next token
9525 is not a parenthesis, then we must be looking at a declaration.
9526 (After "int (" we might be looking at a functional cast.) */
9527 if (decl_specifiers.any_specifiers_p
9528 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9529 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9530 && !cp_parser_error_occurred (parser))
9531 cp_parser_commit_to_tentative_parse (parser);
9533 /* Keep going until we hit the `;' at the end of the simple
9535 saw_declarator = false;
9536 while (cp_lexer_next_token_is_not (parser->lexer,
9540 bool function_definition_p;
9545 /* If we are processing next declarator, coma is expected */
9546 token = cp_lexer_peek_token (parser->lexer);
9547 gcc_assert (token->type == CPP_COMMA);
9548 cp_lexer_consume_token (parser->lexer);
9549 if (maybe_range_for_decl)
9550 *maybe_range_for_decl = error_mark_node;
9553 saw_declarator = true;
9555 /* Parse the init-declarator. */
9556 decl = cp_parser_init_declarator (parser, &decl_specifiers,
9558 function_definition_allowed_p,
9560 declares_class_or_enum,
9561 &function_definition_p,
9562 maybe_range_for_decl);
9563 /* If an error occurred while parsing tentatively, exit quickly.
9564 (That usually happens when in the body of a function; each
9565 statement is treated as a declaration-statement until proven
9567 if (cp_parser_error_occurred (parser))
9569 /* Handle function definitions specially. */
9570 if (function_definition_p)
9572 /* If the next token is a `,', then we are probably
9573 processing something like:
9577 which is erroneous. */
9578 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9580 cp_token *token = cp_lexer_peek_token (parser->lexer);
9581 error_at (token->location,
9583 " declarations and function-definitions is forbidden");
9585 /* Otherwise, we're done with the list of declarators. */
9588 pop_deferring_access_checks ();
9592 if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9593 *maybe_range_for_decl = decl;
9594 /* The next token should be either a `,' or a `;'. */
9595 token = cp_lexer_peek_token (parser->lexer);
9596 /* If it's a `,', there are more declarators to come. */
9597 if (token->type == CPP_COMMA)
9598 /* will be consumed next time around */;
9599 /* If it's a `;', we are done. */
9600 else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9602 /* Anything else is an error. */
9605 /* If we have already issued an error message we don't need
9606 to issue another one. */
9607 if (decl != error_mark_node
9608 || cp_parser_uncommitted_to_tentative_parse_p (parser))
9609 cp_parser_error (parser, "expected %<,%> or %<;%>");
9610 /* Skip tokens until we reach the end of the statement. */
9611 cp_parser_skip_to_end_of_statement (parser);
9612 /* If the next token is now a `;', consume it. */
9613 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9614 cp_lexer_consume_token (parser->lexer);
9617 /* After the first time around, a function-definition is not
9618 allowed -- even if it was OK at first. For example:
9623 function_definition_allowed_p = false;
9626 /* Issue an error message if no declarators are present, and the
9627 decl-specifier-seq does not itself declare a class or
9629 if (!saw_declarator)
9631 if (cp_parser_declares_only_class_p (parser))
9632 shadow_tag (&decl_specifiers);
9633 /* Perform any deferred access checks. */
9634 perform_deferred_access_checks ();
9637 /* Consume the `;'. */
9638 if (!maybe_range_for_decl)
9639 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9642 pop_deferring_access_checks ();
9645 /* Parse a decl-specifier-seq.
9648 decl-specifier-seq [opt] decl-specifier
9651 storage-class-specifier
9662 Set *DECL_SPECS to a representation of the decl-specifier-seq.
9664 The parser flags FLAGS is used to control type-specifier parsing.
9666 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9669 1: one of the decl-specifiers is an elaborated-type-specifier
9670 (i.e., a type declaration)
9671 2: one of the decl-specifiers is an enum-specifier or a
9672 class-specifier (i.e., a type definition)
9677 cp_parser_decl_specifier_seq (cp_parser* parser,
9678 cp_parser_flags flags,
9679 cp_decl_specifier_seq *decl_specs,
9680 int* declares_class_or_enum)
9682 bool constructor_possible_p = !parser->in_declarator_p;
9683 cp_token *start_token = NULL;
9685 /* Clear DECL_SPECS. */
9686 clear_decl_specs (decl_specs);
9688 /* Assume no class or enumeration type is declared. */
9689 *declares_class_or_enum = 0;
9691 /* Keep reading specifiers until there are no more to read. */
9695 bool found_decl_spec;
9698 /* Peek at the next token. */
9699 token = cp_lexer_peek_token (parser->lexer);
9701 /* Save the first token of the decl spec list for error
9704 start_token = token;
9705 /* Handle attributes. */
9706 if (token->keyword == RID_ATTRIBUTE)
9708 /* Parse the attributes. */
9709 decl_specs->attributes
9710 = chainon (decl_specs->attributes,
9711 cp_parser_attributes_opt (parser));
9714 /* Assume we will find a decl-specifier keyword. */
9715 found_decl_spec = true;
9716 /* If the next token is an appropriate keyword, we can simply
9717 add it to the list. */
9718 switch (token->keyword)
9724 if (!at_class_scope_p ())
9726 error_at (token->location, "%<friend%> used outside of class");
9727 cp_lexer_purge_token (parser->lexer);
9731 ++decl_specs->specs[(int) ds_friend];
9732 /* Consume the token. */
9733 cp_lexer_consume_token (parser->lexer);
9738 ++decl_specs->specs[(int) ds_constexpr];
9739 cp_lexer_consume_token (parser->lexer);
9742 /* function-specifier:
9749 cp_parser_function_specifier_opt (parser, decl_specs);
9755 ++decl_specs->specs[(int) ds_typedef];
9756 /* Consume the token. */
9757 cp_lexer_consume_token (parser->lexer);
9758 /* A constructor declarator cannot appear in a typedef. */
9759 constructor_possible_p = false;
9760 /* The "typedef" keyword can only occur in a declaration; we
9761 may as well commit at this point. */
9762 cp_parser_commit_to_tentative_parse (parser);
9764 if (decl_specs->storage_class != sc_none)
9765 decl_specs->conflicting_specifiers_p = true;
9768 /* storage-class-specifier:
9778 if (cxx_dialect == cxx98)
9780 /* Consume the token. */
9781 cp_lexer_consume_token (parser->lexer);
9783 /* Complain about `auto' as a storage specifier, if
9784 we're complaining about C++0x compatibility. */
9785 warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9786 " will change meaning in C++0x; please remove it");
9788 /* Set the storage class anyway. */
9789 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9793 /* C++0x auto type-specifier. */
9794 found_decl_spec = false;
9801 /* Consume the token. */
9802 cp_lexer_consume_token (parser->lexer);
9803 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9807 /* Consume the token. */
9808 cp_lexer_consume_token (parser->lexer);
9809 ++decl_specs->specs[(int) ds_thread];
9813 /* We did not yet find a decl-specifier yet. */
9814 found_decl_spec = false;
9819 && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9820 && token->keyword != RID_CONSTEXPR)
9821 error ("decl-specifier invalid in condition");
9823 /* Constructors are a special case. The `S' in `S()' is not a
9824 decl-specifier; it is the beginning of the declarator. */
9827 && constructor_possible_p
9828 && (cp_parser_constructor_declarator_p
9829 (parser, decl_specs->specs[(int) ds_friend] != 0)));
9831 /* If we don't have a DECL_SPEC yet, then we must be looking at
9832 a type-specifier. */
9833 if (!found_decl_spec && !constructor_p)
9835 int decl_spec_declares_class_or_enum;
9836 bool is_cv_qualifier;
9840 = cp_parser_type_specifier (parser, flags,
9842 /*is_declaration=*/true,
9843 &decl_spec_declares_class_or_enum,
9845 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9847 /* If this type-specifier referenced a user-defined type
9848 (a typedef, class-name, etc.), then we can't allow any
9849 more such type-specifiers henceforth.
9853 The longest sequence of decl-specifiers that could
9854 possibly be a type name is taken as the
9855 decl-specifier-seq of a declaration. The sequence shall
9856 be self-consistent as described below.
9860 As a general rule, at most one type-specifier is allowed
9861 in the complete decl-specifier-seq of a declaration. The
9862 only exceptions are the following:
9864 -- const or volatile can be combined with any other
9867 -- signed or unsigned can be combined with char, long,
9875 void g (const int Pc);
9877 Here, Pc is *not* part of the decl-specifier seq; it's
9878 the declarator. Therefore, once we see a type-specifier
9879 (other than a cv-qualifier), we forbid any additional
9880 user-defined types. We *do* still allow things like `int
9881 int' to be considered a decl-specifier-seq, and issue the
9882 error message later. */
9883 if (type_spec && !is_cv_qualifier)
9884 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9885 /* A constructor declarator cannot follow a type-specifier. */
9888 constructor_possible_p = false;
9889 found_decl_spec = true;
9890 if (!is_cv_qualifier)
9891 decl_specs->any_type_specifiers_p = true;
9895 /* If we still do not have a DECL_SPEC, then there are no more
9897 if (!found_decl_spec)
9900 decl_specs->any_specifiers_p = true;
9901 /* After we see one decl-specifier, further decl-specifiers are
9903 flags |= CP_PARSER_FLAGS_OPTIONAL;
9906 cp_parser_check_decl_spec (decl_specs, start_token->location);
9908 /* Don't allow a friend specifier with a class definition. */
9909 if (decl_specs->specs[(int) ds_friend] != 0
9910 && (*declares_class_or_enum & 2))
9911 error_at (start_token->location,
9912 "class definition may not be declared a friend");
9915 /* Parse an (optional) storage-class-specifier.
9917 storage-class-specifier:
9926 storage-class-specifier:
9929 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
9932 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9934 switch (cp_lexer_peek_token (parser->lexer)->keyword)
9937 if (cxx_dialect != cxx98)
9939 /* Fall through for C++98. */
9946 /* Consume the token. */
9947 return cp_lexer_consume_token (parser->lexer)->u.value;
9954 /* Parse an (optional) function-specifier.
9961 Returns an IDENTIFIER_NODE corresponding to the keyword used.
9962 Updates DECL_SPECS, if it is non-NULL. */
9965 cp_parser_function_specifier_opt (cp_parser* parser,
9966 cp_decl_specifier_seq *decl_specs)
9968 cp_token *token = cp_lexer_peek_token (parser->lexer);
9969 switch (token->keyword)
9973 ++decl_specs->specs[(int) ds_inline];
9977 /* 14.5.2.3 [temp.mem]
9979 A member function template shall not be virtual. */
9980 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9981 error_at (token->location, "templates may not be %<virtual%>");
9982 else if (decl_specs)
9983 ++decl_specs->specs[(int) ds_virtual];
9988 ++decl_specs->specs[(int) ds_explicit];
9995 /* Consume the token. */
9996 return cp_lexer_consume_token (parser->lexer)->u.value;
9999 /* Parse a linkage-specification.
10001 linkage-specification:
10002 extern string-literal { declaration-seq [opt] }
10003 extern string-literal declaration */
10006 cp_parser_linkage_specification (cp_parser* parser)
10010 /* Look for the `extern' keyword. */
10011 cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10013 /* Look for the string-literal. */
10014 linkage = cp_parser_string_literal (parser, false, false);
10016 /* Transform the literal into an identifier. If the literal is a
10017 wide-character string, or contains embedded NULs, then we can't
10018 handle it as the user wants. */
10019 if (strlen (TREE_STRING_POINTER (linkage))
10020 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10022 cp_parser_error (parser, "invalid linkage-specification");
10023 /* Assume C++ linkage. */
10024 linkage = lang_name_cplusplus;
10027 linkage = get_identifier (TREE_STRING_POINTER (linkage));
10029 /* We're now using the new linkage. */
10030 push_lang_context (linkage);
10032 /* If the next token is a `{', then we're using the first
10034 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10036 /* Consume the `{' token. */
10037 cp_lexer_consume_token (parser->lexer);
10038 /* Parse the declarations. */
10039 cp_parser_declaration_seq_opt (parser);
10040 /* Look for the closing `}'. */
10041 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10043 /* Otherwise, there's just one declaration. */
10046 bool saved_in_unbraced_linkage_specification_p;
10048 saved_in_unbraced_linkage_specification_p
10049 = parser->in_unbraced_linkage_specification_p;
10050 parser->in_unbraced_linkage_specification_p = true;
10051 cp_parser_declaration (parser);
10052 parser->in_unbraced_linkage_specification_p
10053 = saved_in_unbraced_linkage_specification_p;
10056 /* We're done with the linkage-specification. */
10057 pop_lang_context ();
10060 /* Parse a static_assert-declaration.
10062 static_assert-declaration:
10063 static_assert ( constant-expression , string-literal ) ;
10065 If MEMBER_P, this static_assert is a class member. */
10068 cp_parser_static_assert(cp_parser *parser, bool member_p)
10073 location_t saved_loc;
10076 /* Peek at the `static_assert' token so we can keep track of exactly
10077 where the static assertion started. */
10078 token = cp_lexer_peek_token (parser->lexer);
10079 saved_loc = token->location;
10081 /* Look for the `static_assert' keyword. */
10082 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
10086 /* We know we are in a static assertion; commit to any tentative
10088 if (cp_parser_parsing_tentatively (parser))
10089 cp_parser_commit_to_tentative_parse (parser);
10091 /* Parse the `(' starting the static assertion condition. */
10092 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10094 /* Parse the constant-expression. Allow a non-constant expression
10095 here in order to give better diagnostics in finish_static_assert. */
10097 cp_parser_constant_expression (parser,
10098 /*allow_non_constant_p=*/true,
10099 /*non_constant_p=*/&dummy);
10101 /* Parse the separating `,'. */
10102 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10104 /* Parse the string-literal message. */
10105 message = cp_parser_string_literal (parser,
10106 /*translate=*/false,
10109 /* A `)' completes the static assertion. */
10110 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10111 cp_parser_skip_to_closing_parenthesis (parser,
10112 /*recovering=*/true,
10113 /*or_comma=*/false,
10114 /*consume_paren=*/true);
10116 /* A semicolon terminates the declaration. */
10117 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10119 /* Complete the static assertion, which may mean either processing
10120 the static assert now or saving it for template instantiation. */
10121 finish_static_assert (condition, message, saved_loc, member_p);
10124 /* Parse a `decltype' type. Returns the type.
10126 simple-type-specifier:
10127 decltype ( expression ) */
10130 cp_parser_decltype (cp_parser *parser)
10133 bool id_expression_or_member_access_p = false;
10134 const char *saved_message;
10135 bool saved_integral_constant_expression_p;
10136 bool saved_non_integral_constant_expression_p;
10137 cp_token *id_expr_start_token;
10139 /* Look for the `decltype' token. */
10140 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10141 return error_mark_node;
10143 /* Types cannot be defined in a `decltype' expression. Save away the
10145 saved_message = parser->type_definition_forbidden_message;
10147 /* And create the new one. */
10148 parser->type_definition_forbidden_message
10149 = G_("types may not be defined in %<decltype%> expressions");
10151 /* The restrictions on constant-expressions do not apply inside
10152 decltype expressions. */
10153 saved_integral_constant_expression_p
10154 = parser->integral_constant_expression_p;
10155 saved_non_integral_constant_expression_p
10156 = parser->non_integral_constant_expression_p;
10157 parser->integral_constant_expression_p = false;
10159 /* Do not actually evaluate the expression. */
10160 ++cp_unevaluated_operand;
10162 /* Do not warn about problems with the expression. */
10163 ++c_inhibit_evaluation_warnings;
10165 /* Parse the opening `('. */
10166 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10167 return error_mark_node;
10169 /* First, try parsing an id-expression. */
10170 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10171 cp_parser_parse_tentatively (parser);
10172 expr = cp_parser_id_expression (parser,
10173 /*template_keyword_p=*/false,
10174 /*check_dependency_p=*/true,
10175 /*template_p=*/NULL,
10176 /*declarator_p=*/false,
10177 /*optional_p=*/false);
10179 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10181 bool non_integral_constant_expression_p = false;
10182 tree id_expression = expr;
10184 const char *error_msg;
10186 if (TREE_CODE (expr) == IDENTIFIER_NODE)
10187 /* Lookup the name we got back from the id-expression. */
10188 expr = cp_parser_lookup_name (parser, expr,
10190 /*is_template=*/false,
10191 /*is_namespace=*/false,
10192 /*check_dependency=*/true,
10193 /*ambiguous_decls=*/NULL,
10194 id_expr_start_token->location);
10197 && expr != error_mark_node
10198 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10199 && TREE_CODE (expr) != TYPE_DECL
10200 && (TREE_CODE (expr) != BIT_NOT_EXPR
10201 || !TYPE_P (TREE_OPERAND (expr, 0)))
10202 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10204 /* Complete lookup of the id-expression. */
10205 expr = (finish_id_expression
10206 (id_expression, expr, parser->scope, &idk,
10207 /*integral_constant_expression_p=*/false,
10208 /*allow_non_integral_constant_expression_p=*/true,
10209 &non_integral_constant_expression_p,
10210 /*template_p=*/false,
10212 /*address_p=*/false,
10213 /*template_arg_p=*/false,
10215 id_expr_start_token->location));
10217 if (expr == error_mark_node)
10218 /* We found an id-expression, but it was something that we
10219 should not have found. This is an error, not something
10220 we can recover from, so note that we found an
10221 id-expression and we'll recover as gracefully as
10223 id_expression_or_member_access_p = true;
10227 && expr != error_mark_node
10228 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10229 /* We have an id-expression. */
10230 id_expression_or_member_access_p = true;
10233 if (!id_expression_or_member_access_p)
10235 /* Abort the id-expression parse. */
10236 cp_parser_abort_tentative_parse (parser);
10238 /* Parsing tentatively, again. */
10239 cp_parser_parse_tentatively (parser);
10241 /* Parse a class member access. */
10242 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10244 /*member_access_only_p=*/true, NULL);
10247 && expr != error_mark_node
10248 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10249 /* We have an id-expression. */
10250 id_expression_or_member_access_p = true;
10253 if (id_expression_or_member_access_p)
10254 /* We have parsed the complete id-expression or member access. */
10255 cp_parser_parse_definitely (parser);
10258 bool saved_greater_than_is_operator_p;
10260 /* Abort our attempt to parse an id-expression or member access
10262 cp_parser_abort_tentative_parse (parser);
10264 /* Within a parenthesized expression, a `>' token is always
10265 the greater-than operator. */
10266 saved_greater_than_is_operator_p
10267 = parser->greater_than_is_operator_p;
10268 parser->greater_than_is_operator_p = true;
10270 /* Parse a full expression. */
10271 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10273 /* The `>' token might be the end of a template-id or
10274 template-parameter-list now. */
10275 parser->greater_than_is_operator_p
10276 = saved_greater_than_is_operator_p;
10279 /* Go back to evaluating expressions. */
10280 --cp_unevaluated_operand;
10281 --c_inhibit_evaluation_warnings;
10283 /* Restore the old message and the integral constant expression
10285 parser->type_definition_forbidden_message = saved_message;
10286 parser->integral_constant_expression_p
10287 = saved_integral_constant_expression_p;
10288 parser->non_integral_constant_expression_p
10289 = saved_non_integral_constant_expression_p;
10291 if (expr == error_mark_node)
10293 /* Skip everything up to the closing `)'. */
10294 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10295 /*consume_paren=*/true);
10296 return error_mark_node;
10299 /* Parse to the closing `)'. */
10300 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10302 cp_parser_skip_to_closing_parenthesis (parser, true, false,
10303 /*consume_paren=*/true);
10304 return error_mark_node;
10307 return finish_decltype_type (expr, id_expression_or_member_access_p,
10308 tf_warning_or_error);
10311 /* Special member functions [gram.special] */
10313 /* Parse a conversion-function-id.
10315 conversion-function-id:
10316 operator conversion-type-id
10318 Returns an IDENTIFIER_NODE representing the operator. */
10321 cp_parser_conversion_function_id (cp_parser* parser)
10325 tree saved_qualifying_scope;
10326 tree saved_object_scope;
10327 tree pushed_scope = NULL_TREE;
10329 /* Look for the `operator' token. */
10330 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10331 return error_mark_node;
10332 /* When we parse the conversion-type-id, the current scope will be
10333 reset. However, we need that information in able to look up the
10334 conversion function later, so we save it here. */
10335 saved_scope = parser->scope;
10336 saved_qualifying_scope = parser->qualifying_scope;
10337 saved_object_scope = parser->object_scope;
10338 /* We must enter the scope of the class so that the names of
10339 entities declared within the class are available in the
10340 conversion-type-id. For example, consider:
10347 S::operator I() { ... }
10349 In order to see that `I' is a type-name in the definition, we
10350 must be in the scope of `S'. */
10352 pushed_scope = push_scope (saved_scope);
10353 /* Parse the conversion-type-id. */
10354 type = cp_parser_conversion_type_id (parser);
10355 /* Leave the scope of the class, if any. */
10357 pop_scope (pushed_scope);
10358 /* Restore the saved scope. */
10359 parser->scope = saved_scope;
10360 parser->qualifying_scope = saved_qualifying_scope;
10361 parser->object_scope = saved_object_scope;
10362 /* If the TYPE is invalid, indicate failure. */
10363 if (type == error_mark_node)
10364 return error_mark_node;
10365 return mangle_conv_op_name_for_type (type);
10368 /* Parse a conversion-type-id:
10370 conversion-type-id:
10371 type-specifier-seq conversion-declarator [opt]
10373 Returns the TYPE specified. */
10376 cp_parser_conversion_type_id (cp_parser* parser)
10379 cp_decl_specifier_seq type_specifiers;
10380 cp_declarator *declarator;
10381 tree type_specified;
10383 /* Parse the attributes. */
10384 attributes = cp_parser_attributes_opt (parser);
10385 /* Parse the type-specifiers. */
10386 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10387 /*is_trailing_return=*/false,
10389 /* If that didn't work, stop. */
10390 if (type_specifiers.type == error_mark_node)
10391 return error_mark_node;
10392 /* Parse the conversion-declarator. */
10393 declarator = cp_parser_conversion_declarator_opt (parser);
10395 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
10396 /*initialized=*/0, &attributes);
10398 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10400 /* Don't give this error when parsing tentatively. This happens to
10401 work because we always parse this definitively once. */
10402 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10403 && type_uses_auto (type_specified))
10405 error ("invalid use of %<auto%> in conversion operator");
10406 return error_mark_node;
10409 return type_specified;
10412 /* Parse an (optional) conversion-declarator.
10414 conversion-declarator:
10415 ptr-operator conversion-declarator [opt]
10419 static cp_declarator *
10420 cp_parser_conversion_declarator_opt (cp_parser* parser)
10422 enum tree_code code;
10424 cp_cv_quals cv_quals;
10426 /* We don't know if there's a ptr-operator next, or not. */
10427 cp_parser_parse_tentatively (parser);
10428 /* Try the ptr-operator. */
10429 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10430 /* If it worked, look for more conversion-declarators. */
10431 if (cp_parser_parse_definitely (parser))
10433 cp_declarator *declarator;
10435 /* Parse another optional declarator. */
10436 declarator = cp_parser_conversion_declarator_opt (parser);
10438 return cp_parser_make_indirect_declarator
10439 (code, class_type, cv_quals, declarator);
10445 /* Parse an (optional) ctor-initializer.
10448 : mem-initializer-list
10450 Returns TRUE iff the ctor-initializer was actually present. */
10453 cp_parser_ctor_initializer_opt (cp_parser* parser)
10455 /* If the next token is not a `:', then there is no
10456 ctor-initializer. */
10457 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10459 /* Do default initialization of any bases and members. */
10460 if (DECL_CONSTRUCTOR_P (current_function_decl))
10461 finish_mem_initializers (NULL_TREE);
10466 /* Consume the `:' token. */
10467 cp_lexer_consume_token (parser->lexer);
10468 /* And the mem-initializer-list. */
10469 cp_parser_mem_initializer_list (parser);
10474 /* Parse a mem-initializer-list.
10476 mem-initializer-list:
10477 mem-initializer ... [opt]
10478 mem-initializer ... [opt] , mem-initializer-list */
10481 cp_parser_mem_initializer_list (cp_parser* parser)
10483 tree mem_initializer_list = NULL_TREE;
10484 cp_token *token = cp_lexer_peek_token (parser->lexer);
10486 /* Let the semantic analysis code know that we are starting the
10487 mem-initializer-list. */
10488 if (!DECL_CONSTRUCTOR_P (current_function_decl))
10489 error_at (token->location,
10490 "only constructors take member initializers");
10492 /* Loop through the list. */
10495 tree mem_initializer;
10497 token = cp_lexer_peek_token (parser->lexer);
10498 /* Parse the mem-initializer. */
10499 mem_initializer = cp_parser_mem_initializer (parser);
10500 /* If the next token is a `...', we're expanding member initializers. */
10501 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10503 /* Consume the `...'. */
10504 cp_lexer_consume_token (parser->lexer);
10506 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10507 can be expanded but members cannot. */
10508 if (mem_initializer != error_mark_node
10509 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10511 error_at (token->location,
10512 "cannot expand initializer for member %<%D%>",
10513 TREE_PURPOSE (mem_initializer));
10514 mem_initializer = error_mark_node;
10517 /* Construct the pack expansion type. */
10518 if (mem_initializer != error_mark_node)
10519 mem_initializer = make_pack_expansion (mem_initializer);
10521 /* Add it to the list, unless it was erroneous. */
10522 if (mem_initializer != error_mark_node)
10524 TREE_CHAIN (mem_initializer) = mem_initializer_list;
10525 mem_initializer_list = mem_initializer;
10527 /* If the next token is not a `,', we're done. */
10528 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10530 /* Consume the `,' token. */
10531 cp_lexer_consume_token (parser->lexer);
10534 /* Perform semantic analysis. */
10535 if (DECL_CONSTRUCTOR_P (current_function_decl))
10536 finish_mem_initializers (mem_initializer_list);
10539 /* Parse a mem-initializer.
10542 mem-initializer-id ( expression-list [opt] )
10543 mem-initializer-id braced-init-list
10548 ( expression-list [opt] )
10550 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
10551 class) or FIELD_DECL (for a non-static data member) to initialize;
10552 the TREE_VALUE is the expression-list. An empty initialization
10553 list is represented by void_list_node. */
10556 cp_parser_mem_initializer (cp_parser* parser)
10558 tree mem_initializer_id;
10559 tree expression_list;
10561 cp_token *token = cp_lexer_peek_token (parser->lexer);
10563 /* Find out what is being initialized. */
10564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10566 permerror (token->location,
10567 "anachronistic old-style base class initializer");
10568 mem_initializer_id = NULL_TREE;
10572 mem_initializer_id = cp_parser_mem_initializer_id (parser);
10573 if (mem_initializer_id == error_mark_node)
10574 return mem_initializer_id;
10576 member = expand_member_init (mem_initializer_id);
10577 if (member && !DECL_P (member))
10578 in_base_initializer = 1;
10580 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10582 bool expr_non_constant_p;
10583 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10584 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10585 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10586 expression_list = build_tree_list (NULL_TREE, expression_list);
10591 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10593 /*allow_expansion_p=*/true,
10594 /*non_constant_p=*/NULL);
10596 return error_mark_node;
10597 expression_list = build_tree_list_vec (vec);
10598 release_tree_vector (vec);
10601 if (expression_list == error_mark_node)
10602 return error_mark_node;
10603 if (!expression_list)
10604 expression_list = void_type_node;
10606 in_base_initializer = 0;
10608 return member ? build_tree_list (member, expression_list) : error_mark_node;
10611 /* Parse a mem-initializer-id.
10613 mem-initializer-id:
10614 :: [opt] nested-name-specifier [opt] class-name
10617 Returns a TYPE indicating the class to be initializer for the first
10618 production. Returns an IDENTIFIER_NODE indicating the data member
10619 to be initialized for the second production. */
10622 cp_parser_mem_initializer_id (cp_parser* parser)
10624 bool global_scope_p;
10625 bool nested_name_specifier_p;
10626 bool template_p = false;
10629 cp_token *token = cp_lexer_peek_token (parser->lexer);
10631 /* `typename' is not allowed in this context ([temp.res]). */
10632 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10634 error_at (token->location,
10635 "keyword %<typename%> not allowed in this context (a qualified "
10636 "member initializer is implicitly a type)");
10637 cp_lexer_consume_token (parser->lexer);
10639 /* Look for the optional `::' operator. */
10641 = (cp_parser_global_scope_opt (parser,
10642 /*current_scope_valid_p=*/false)
10644 /* Look for the optional nested-name-specifier. The simplest way to
10649 The keyword `typename' is not permitted in a base-specifier or
10650 mem-initializer; in these contexts a qualified name that
10651 depends on a template-parameter is implicitly assumed to be a
10654 is to assume that we have seen the `typename' keyword at this
10656 nested_name_specifier_p
10657 = (cp_parser_nested_name_specifier_opt (parser,
10658 /*typename_keyword_p=*/true,
10659 /*check_dependency_p=*/true,
10661 /*is_declaration=*/true)
10663 if (nested_name_specifier_p)
10664 template_p = cp_parser_optional_template_keyword (parser);
10665 /* If there is a `::' operator or a nested-name-specifier, then we
10666 are definitely looking for a class-name. */
10667 if (global_scope_p || nested_name_specifier_p)
10668 return cp_parser_class_name (parser,
10669 /*typename_keyword_p=*/true,
10670 /*template_keyword_p=*/template_p,
10672 /*check_dependency_p=*/true,
10673 /*class_head_p=*/false,
10674 /*is_declaration=*/true);
10675 /* Otherwise, we could also be looking for an ordinary identifier. */
10676 cp_parser_parse_tentatively (parser);
10677 /* Try a class-name. */
10678 id = cp_parser_class_name (parser,
10679 /*typename_keyword_p=*/true,
10680 /*template_keyword_p=*/false,
10682 /*check_dependency_p=*/true,
10683 /*class_head_p=*/false,
10684 /*is_declaration=*/true);
10685 /* If we found one, we're done. */
10686 if (cp_parser_parse_definitely (parser))
10688 /* Otherwise, look for an ordinary identifier. */
10689 return cp_parser_identifier (parser);
10692 /* Overloading [gram.over] */
10694 /* Parse an operator-function-id.
10696 operator-function-id:
10699 Returns an IDENTIFIER_NODE for the operator which is a
10700 human-readable spelling of the identifier, e.g., `operator +'. */
10703 cp_parser_operator_function_id (cp_parser* parser)
10705 /* Look for the `operator' keyword. */
10706 if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10707 return error_mark_node;
10708 /* And then the name of the operator itself. */
10709 return cp_parser_operator (parser);
10712 /* Parse an operator.
10715 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10716 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10717 || ++ -- , ->* -> () []
10724 Returns an IDENTIFIER_NODE for the operator which is a
10725 human-readable spelling of the identifier, e.g., `operator +'. */
10728 cp_parser_operator (cp_parser* parser)
10730 tree id = NULL_TREE;
10733 /* Peek at the next token. */
10734 token = cp_lexer_peek_token (parser->lexer);
10735 /* Figure out which operator we have. */
10736 switch (token->type)
10742 /* The keyword should be either `new' or `delete'. */
10743 if (token->keyword == RID_NEW)
10745 else if (token->keyword == RID_DELETE)
10750 /* Consume the `new' or `delete' token. */
10751 cp_lexer_consume_token (parser->lexer);
10753 /* Peek at the next token. */
10754 token = cp_lexer_peek_token (parser->lexer);
10755 /* If it's a `[' token then this is the array variant of the
10757 if (token->type == CPP_OPEN_SQUARE)
10759 /* Consume the `[' token. */
10760 cp_lexer_consume_token (parser->lexer);
10761 /* Look for the `]' token. */
10762 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10763 id = ansi_opname (op == NEW_EXPR
10764 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10766 /* Otherwise, we have the non-array variant. */
10768 id = ansi_opname (op);
10774 id = ansi_opname (PLUS_EXPR);
10778 id = ansi_opname (MINUS_EXPR);
10782 id = ansi_opname (MULT_EXPR);
10786 id = ansi_opname (TRUNC_DIV_EXPR);
10790 id = ansi_opname (TRUNC_MOD_EXPR);
10794 id = ansi_opname (BIT_XOR_EXPR);
10798 id = ansi_opname (BIT_AND_EXPR);
10802 id = ansi_opname (BIT_IOR_EXPR);
10806 id = ansi_opname (BIT_NOT_EXPR);
10810 id = ansi_opname (TRUTH_NOT_EXPR);
10814 id = ansi_assopname (NOP_EXPR);
10818 id = ansi_opname (LT_EXPR);
10822 id = ansi_opname (GT_EXPR);
10826 id = ansi_assopname (PLUS_EXPR);
10830 id = ansi_assopname (MINUS_EXPR);
10834 id = ansi_assopname (MULT_EXPR);
10838 id = ansi_assopname (TRUNC_DIV_EXPR);
10842 id = ansi_assopname (TRUNC_MOD_EXPR);
10846 id = ansi_assopname (BIT_XOR_EXPR);
10850 id = ansi_assopname (BIT_AND_EXPR);
10854 id = ansi_assopname (BIT_IOR_EXPR);
10858 id = ansi_opname (LSHIFT_EXPR);
10862 id = ansi_opname (RSHIFT_EXPR);
10865 case CPP_LSHIFT_EQ:
10866 id = ansi_assopname (LSHIFT_EXPR);
10869 case CPP_RSHIFT_EQ:
10870 id = ansi_assopname (RSHIFT_EXPR);
10874 id = ansi_opname (EQ_EXPR);
10878 id = ansi_opname (NE_EXPR);
10882 id = ansi_opname (LE_EXPR);
10885 case CPP_GREATER_EQ:
10886 id = ansi_opname (GE_EXPR);
10890 id = ansi_opname (TRUTH_ANDIF_EXPR);
10894 id = ansi_opname (TRUTH_ORIF_EXPR);
10897 case CPP_PLUS_PLUS:
10898 id = ansi_opname (POSTINCREMENT_EXPR);
10901 case CPP_MINUS_MINUS:
10902 id = ansi_opname (PREDECREMENT_EXPR);
10906 id = ansi_opname (COMPOUND_EXPR);
10909 case CPP_DEREF_STAR:
10910 id = ansi_opname (MEMBER_REF);
10914 id = ansi_opname (COMPONENT_REF);
10917 case CPP_OPEN_PAREN:
10918 /* Consume the `('. */
10919 cp_lexer_consume_token (parser->lexer);
10920 /* Look for the matching `)'. */
10921 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10922 return ansi_opname (CALL_EXPR);
10924 case CPP_OPEN_SQUARE:
10925 /* Consume the `['. */
10926 cp_lexer_consume_token (parser->lexer);
10927 /* Look for the matching `]'. */
10928 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10929 return ansi_opname (ARRAY_REF);
10932 /* Anything else is an error. */
10936 /* If we have selected an identifier, we need to consume the
10939 cp_lexer_consume_token (parser->lexer);
10940 /* Otherwise, no valid operator name was present. */
10943 cp_parser_error (parser, "expected operator");
10944 id = error_mark_node;
10950 /* Parse a template-declaration.
10952 template-declaration:
10953 export [opt] template < template-parameter-list > declaration
10955 If MEMBER_P is TRUE, this template-declaration occurs within a
10958 The grammar rule given by the standard isn't correct. What
10959 is really meant is:
10961 template-declaration:
10962 export [opt] template-parameter-list-seq
10963 decl-specifier-seq [opt] init-declarator [opt] ;
10964 export [opt] template-parameter-list-seq
10965 function-definition
10967 template-parameter-list-seq:
10968 template-parameter-list-seq [opt]
10969 template < template-parameter-list > */
10972 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10974 /* Check for `export'. */
10975 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10977 /* Consume the `export' token. */
10978 cp_lexer_consume_token (parser->lexer);
10979 /* Warn that we do not support `export'. */
10980 warning (0, "keyword %<export%> not implemented, and will be ignored");
10983 cp_parser_template_declaration_after_export (parser, member_p);
10986 /* Parse a template-parameter-list.
10988 template-parameter-list:
10990 template-parameter-list , template-parameter
10992 Returns a TREE_LIST. Each node represents a template parameter.
10993 The nodes are connected via their TREE_CHAINs. */
10996 cp_parser_template_parameter_list (cp_parser* parser)
10998 tree parameter_list = NULL_TREE;
11000 begin_template_parm_list ();
11002 /* The loop below parses the template parms. We first need to know
11003 the total number of template parms to be able to compute proper
11004 canonical types of each dependent type. So after the loop, when
11005 we know the total number of template parms,
11006 end_template_parm_list computes the proper canonical types and
11007 fixes up the dependent types accordingly. */
11012 bool is_parameter_pack;
11013 location_t parm_loc;
11015 /* Parse the template-parameter. */
11016 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11017 parameter = cp_parser_template_parameter (parser,
11019 &is_parameter_pack);
11020 /* Add it to the list. */
11021 if (parameter != error_mark_node)
11022 parameter_list = process_template_parm (parameter_list,
11030 tree err_parm = build_tree_list (parameter, parameter);
11031 parameter_list = chainon (parameter_list, err_parm);
11034 /* If the next token is not a `,', we're done. */
11035 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11037 /* Otherwise, consume the `,' token. */
11038 cp_lexer_consume_token (parser->lexer);
11041 return end_template_parm_list (parameter_list);
11044 /* Parse a template-parameter.
11046 template-parameter:
11048 parameter-declaration
11050 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
11051 the parameter. The TREE_PURPOSE is the default value, if any.
11052 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
11053 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
11054 set to true iff this parameter is a parameter pack. */
11057 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11058 bool *is_parameter_pack)
11061 cp_parameter_declarator *parameter_declarator;
11062 cp_declarator *id_declarator;
11065 /* Assume it is a type parameter or a template parameter. */
11066 *is_non_type = false;
11067 /* Assume it not a parameter pack. */
11068 *is_parameter_pack = false;
11069 /* Peek at the next token. */
11070 token = cp_lexer_peek_token (parser->lexer);
11071 /* If it is `class' or `template', we have a type-parameter. */
11072 if (token->keyword == RID_TEMPLATE)
11073 return cp_parser_type_parameter (parser, is_parameter_pack);
11074 /* If it is `class' or `typename' we do not know yet whether it is a
11075 type parameter or a non-type parameter. Consider:
11077 template <typename T, typename T::X X> ...
11081 template <class C, class D*> ...
11083 Here, the first parameter is a type parameter, and the second is
11084 a non-type parameter. We can tell by looking at the token after
11085 the identifier -- if it is a `,', `=', or `>' then we have a type
11087 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11089 /* Peek at the token after `class' or `typename'. */
11090 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11091 /* If it's an ellipsis, we have a template type parameter
11093 if (token->type == CPP_ELLIPSIS)
11094 return cp_parser_type_parameter (parser, is_parameter_pack);
11095 /* If it's an identifier, skip it. */
11096 if (token->type == CPP_NAME)
11097 token = cp_lexer_peek_nth_token (parser->lexer, 3);
11098 /* Now, see if the token looks like the end of a template
11100 if (token->type == CPP_COMMA
11101 || token->type == CPP_EQ
11102 || token->type == CPP_GREATER)
11103 return cp_parser_type_parameter (parser, is_parameter_pack);
11106 /* Otherwise, it is a non-type parameter.
11110 When parsing a default template-argument for a non-type
11111 template-parameter, the first non-nested `>' is taken as the end
11112 of the template parameter-list rather than a greater-than
11114 *is_non_type = true;
11115 parameter_declarator
11116 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11117 /*parenthesized_p=*/NULL);
11119 /* If the parameter declaration is marked as a parameter pack, set
11120 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11121 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11123 if (parameter_declarator
11124 && parameter_declarator->declarator
11125 && parameter_declarator->declarator->parameter_pack_p)
11127 *is_parameter_pack = true;
11128 parameter_declarator->declarator->parameter_pack_p = false;
11131 /* If the next token is an ellipsis, and we don't already have it
11132 marked as a parameter pack, then we have a parameter pack (that
11133 has no declarator). */
11134 if (!*is_parameter_pack
11135 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11136 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11138 /* Consume the `...'. */
11139 cp_lexer_consume_token (parser->lexer);
11140 maybe_warn_variadic_templates ();
11142 *is_parameter_pack = true;
11144 /* We might end up with a pack expansion as the type of the non-type
11145 template parameter, in which case this is a non-type template
11147 else if (parameter_declarator
11148 && parameter_declarator->decl_specifiers.type
11149 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11151 *is_parameter_pack = true;
11152 parameter_declarator->decl_specifiers.type =
11153 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11156 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11158 /* Parameter packs cannot have default arguments. However, a
11159 user may try to do so, so we'll parse them and give an
11160 appropriate diagnostic here. */
11162 /* Consume the `='. */
11163 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11164 cp_lexer_consume_token (parser->lexer);
11166 /* Find the name of the parameter pack. */
11167 id_declarator = parameter_declarator->declarator;
11168 while (id_declarator && id_declarator->kind != cdk_id)
11169 id_declarator = id_declarator->declarator;
11171 if (id_declarator && id_declarator->kind == cdk_id)
11172 error_at (start_token->location,
11173 "template parameter pack %qD cannot have a default argument",
11174 id_declarator->u.id.unqualified_name);
11176 error_at (start_token->location,
11177 "template parameter pack cannot have a default argument");
11179 /* Parse the default argument, but throw away the result. */
11180 cp_parser_default_argument (parser, /*template_parm_p=*/true);
11183 parm = grokdeclarator (parameter_declarator->declarator,
11184 ¶meter_declarator->decl_specifiers,
11185 TPARM, /*initialized=*/0,
11186 /*attrlist=*/NULL);
11187 if (parm == error_mark_node)
11188 return error_mark_node;
11190 return build_tree_list (parameter_declarator->default_argument, parm);
11193 /* Parse a type-parameter.
11196 class identifier [opt]
11197 class identifier [opt] = type-id
11198 typename identifier [opt]
11199 typename identifier [opt] = type-id
11200 template < template-parameter-list > class identifier [opt]
11201 template < template-parameter-list > class identifier [opt]
11204 GNU Extension (variadic templates):
11207 class ... identifier [opt]
11208 typename ... identifier [opt]
11210 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
11211 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
11212 the declaration of the parameter.
11214 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11217 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11222 /* Look for a keyword to tell us what kind of parameter this is. */
11223 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11225 return error_mark_node;
11227 switch (token->keyword)
11233 tree default_argument;
11235 /* If the next token is an ellipsis, we have a template
11237 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11239 /* Consume the `...' token. */
11240 cp_lexer_consume_token (parser->lexer);
11241 maybe_warn_variadic_templates ();
11243 *is_parameter_pack = true;
11246 /* If the next token is an identifier, then it names the
11248 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11249 identifier = cp_parser_identifier (parser);
11251 identifier = NULL_TREE;
11253 /* Create the parameter. */
11254 parameter = finish_template_type_parm (class_type_node, identifier);
11256 /* If the next token is an `=', we have a default argument. */
11257 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11259 /* Consume the `=' token. */
11260 cp_lexer_consume_token (parser->lexer);
11261 /* Parse the default-argument. */
11262 push_deferring_access_checks (dk_no_deferred);
11263 default_argument = cp_parser_type_id (parser);
11265 /* Template parameter packs cannot have default
11267 if (*is_parameter_pack)
11270 error_at (token->location,
11271 "template parameter pack %qD cannot have a "
11272 "default argument", identifier);
11274 error_at (token->location,
11275 "template parameter packs cannot have "
11276 "default arguments");
11277 default_argument = NULL_TREE;
11279 pop_deferring_access_checks ();
11282 default_argument = NULL_TREE;
11284 /* Create the combined representation of the parameter and the
11285 default argument. */
11286 parameter = build_tree_list (default_argument, parameter);
11293 tree default_argument;
11295 /* Look for the `<'. */
11296 cp_parser_require (parser, CPP_LESS, RT_LESS);
11297 /* Parse the template-parameter-list. */
11298 cp_parser_template_parameter_list (parser);
11299 /* Look for the `>'. */
11300 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11301 /* Look for the `class' keyword. */
11302 cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11303 /* If the next token is an ellipsis, we have a template
11305 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11307 /* Consume the `...' token. */
11308 cp_lexer_consume_token (parser->lexer);
11309 maybe_warn_variadic_templates ();
11311 *is_parameter_pack = true;
11313 /* If the next token is an `=', then there is a
11314 default-argument. If the next token is a `>', we are at
11315 the end of the parameter-list. If the next token is a `,',
11316 then we are at the end of this parameter. */
11317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11318 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11319 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11321 identifier = cp_parser_identifier (parser);
11322 /* Treat invalid names as if the parameter were nameless. */
11323 if (identifier == error_mark_node)
11324 identifier = NULL_TREE;
11327 identifier = NULL_TREE;
11329 /* Create the template parameter. */
11330 parameter = finish_template_template_parm (class_type_node,
11333 /* If the next token is an `=', then there is a
11334 default-argument. */
11335 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11339 /* Consume the `='. */
11340 cp_lexer_consume_token (parser->lexer);
11341 /* Parse the id-expression. */
11342 push_deferring_access_checks (dk_no_deferred);
11343 /* save token before parsing the id-expression, for error
11345 token = cp_lexer_peek_token (parser->lexer);
11347 = cp_parser_id_expression (parser,
11348 /*template_keyword_p=*/false,
11349 /*check_dependency_p=*/true,
11350 /*template_p=*/&is_template,
11351 /*declarator_p=*/false,
11352 /*optional_p=*/false);
11353 if (TREE_CODE (default_argument) == TYPE_DECL)
11354 /* If the id-expression was a template-id that refers to
11355 a template-class, we already have the declaration here,
11356 so no further lookup is needed. */
11359 /* Look up the name. */
11361 = cp_parser_lookup_name (parser, default_argument,
11363 /*is_template=*/is_template,
11364 /*is_namespace=*/false,
11365 /*check_dependency=*/true,
11366 /*ambiguous_decls=*/NULL,
11368 /* See if the default argument is valid. */
11370 = check_template_template_default_arg (default_argument);
11372 /* Template parameter packs cannot have default
11374 if (*is_parameter_pack)
11377 error_at (token->location,
11378 "template parameter pack %qD cannot "
11379 "have a default argument",
11382 error_at (token->location, "template parameter packs cannot "
11383 "have default arguments");
11384 default_argument = NULL_TREE;
11386 pop_deferring_access_checks ();
11389 default_argument = NULL_TREE;
11391 /* Create the combined representation of the parameter and the
11392 default argument. */
11393 parameter = build_tree_list (default_argument, parameter);
11398 gcc_unreachable ();
11405 /* Parse a template-id.
11408 template-name < template-argument-list [opt] >
11410 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11411 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
11412 returned. Otherwise, if the template-name names a function, or set
11413 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
11414 names a class, returns a TYPE_DECL for the specialization.
11416 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11417 uninstantiated templates. */
11420 cp_parser_template_id (cp_parser *parser,
11421 bool template_keyword_p,
11422 bool check_dependency_p,
11423 bool is_declaration)
11429 cp_token_position start_of_id = 0;
11430 deferred_access_check *chk;
11431 VEC (deferred_access_check,gc) *access_check;
11432 cp_token *next_token = NULL, *next_token_2 = NULL;
11433 bool is_identifier;
11435 /* If the next token corresponds to a template-id, there is no need
11437 next_token = cp_lexer_peek_token (parser->lexer);
11438 if (next_token->type == CPP_TEMPLATE_ID)
11440 struct tree_check *check_value;
11442 /* Get the stored value. */
11443 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11444 /* Perform any access checks that were deferred. */
11445 access_check = check_value->checks;
11448 FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11449 perform_or_defer_access_check (chk->binfo,
11453 /* Return the stored value. */
11454 return check_value->value;
11457 /* Avoid performing name lookup if there is no possibility of
11458 finding a template-id. */
11459 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11460 || (next_token->type == CPP_NAME
11461 && !cp_parser_nth_token_starts_template_argument_list_p
11464 cp_parser_error (parser, "expected template-id");
11465 return error_mark_node;
11468 /* Remember where the template-id starts. */
11469 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11470 start_of_id = cp_lexer_token_position (parser->lexer, false);
11472 push_deferring_access_checks (dk_deferred);
11474 /* Parse the template-name. */
11475 is_identifier = false;
11476 templ = cp_parser_template_name (parser, template_keyword_p,
11477 check_dependency_p,
11480 if (templ == error_mark_node || is_identifier)
11482 pop_deferring_access_checks ();
11486 /* If we find the sequence `[:' after a template-name, it's probably
11487 a digraph-typo for `< ::'. Substitute the tokens and check if we can
11488 parse correctly the argument list. */
11489 next_token = cp_lexer_peek_token (parser->lexer);
11490 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11491 if (next_token->type == CPP_OPEN_SQUARE
11492 && next_token->flags & DIGRAPH
11493 && next_token_2->type == CPP_COLON
11494 && !(next_token_2->flags & PREV_WHITE))
11496 cp_parser_parse_tentatively (parser);
11497 /* Change `:' into `::'. */
11498 next_token_2->type = CPP_SCOPE;
11499 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11501 cp_lexer_consume_token (parser->lexer);
11503 /* Parse the arguments. */
11504 arguments = cp_parser_enclosed_template_argument_list (parser);
11505 if (!cp_parser_parse_definitely (parser))
11507 /* If we couldn't parse an argument list, then we revert our changes
11508 and return simply an error. Maybe this is not a template-id
11510 next_token_2->type = CPP_COLON;
11511 cp_parser_error (parser, "expected %<<%>");
11512 pop_deferring_access_checks ();
11513 return error_mark_node;
11515 /* Otherwise, emit an error about the invalid digraph, but continue
11516 parsing because we got our argument list. */
11517 if (permerror (next_token->location,
11518 "%<<::%> cannot begin a template-argument list"))
11520 static bool hint = false;
11521 inform (next_token->location,
11522 "%<<:%> is an alternate spelling for %<[%>."
11523 " Insert whitespace between %<<%> and %<::%>");
11524 if (!hint && !flag_permissive)
11526 inform (next_token->location, "(if you use %<-fpermissive%>"
11527 " G++ will accept your code)");
11534 /* Look for the `<' that starts the template-argument-list. */
11535 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11537 pop_deferring_access_checks ();
11538 return error_mark_node;
11540 /* Parse the arguments. */
11541 arguments = cp_parser_enclosed_template_argument_list (parser);
11544 /* Build a representation of the specialization. */
11545 if (TREE_CODE (templ) == IDENTIFIER_NODE)
11546 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11547 else if (DECL_CLASS_TEMPLATE_P (templ)
11548 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11550 bool entering_scope;
11551 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11552 template (rather than some instantiation thereof) only if
11553 is not nested within some other construct. For example, in
11554 "template <typename T> void f(T) { A<T>::", A<T> is just an
11555 instantiation of A. */
11556 entering_scope = (template_parm_scope_p ()
11557 && cp_lexer_next_token_is (parser->lexer,
11560 = finish_template_type (templ, arguments, entering_scope);
11564 /* If it's not a class-template or a template-template, it should be
11565 a function-template. */
11566 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11567 || TREE_CODE (templ) == OVERLOAD
11568 || BASELINK_P (templ)));
11570 template_id = lookup_template_function (templ, arguments);
11573 /* If parsing tentatively, replace the sequence of tokens that makes
11574 up the template-id with a CPP_TEMPLATE_ID token. That way,
11575 should we re-parse the token stream, we will not have to repeat
11576 the effort required to do the parse, nor will we issue duplicate
11577 error messages about problems during instantiation of the
11581 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11583 /* Reset the contents of the START_OF_ID token. */
11584 token->type = CPP_TEMPLATE_ID;
11585 /* Retrieve any deferred checks. Do not pop this access checks yet
11586 so the memory will not be reclaimed during token replacing below. */
11587 token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11588 token->u.tree_check_value->value = template_id;
11589 token->u.tree_check_value->checks = get_deferred_access_checks ();
11590 token->keyword = RID_MAX;
11592 /* Purge all subsequent tokens. */
11593 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11595 /* ??? Can we actually assume that, if template_id ==
11596 error_mark_node, we will have issued a diagnostic to the
11597 user, as opposed to simply marking the tentative parse as
11599 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11600 error_at (token->location, "parse error in template argument list");
11603 pop_deferring_access_checks ();
11604 return template_id;
11607 /* Parse a template-name.
11612 The standard should actually say:
11616 operator-function-id
11618 A defect report has been filed about this issue.
11620 A conversion-function-id cannot be a template name because they cannot
11621 be part of a template-id. In fact, looking at this code:
11623 a.operator K<int>()
11625 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11626 It is impossible to call a templated conversion-function-id with an
11627 explicit argument list, since the only allowed template parameter is
11628 the type to which it is converting.
11630 If TEMPLATE_KEYWORD_P is true, then we have just seen the
11631 `template' keyword, in a construction like:
11635 In that case `f' is taken to be a template-name, even though there
11636 is no way of knowing for sure.
11638 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11639 name refers to a set of overloaded functions, at least one of which
11640 is a template, or an IDENTIFIER_NODE with the name of the template,
11641 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
11642 names are looked up inside uninstantiated templates. */
11645 cp_parser_template_name (cp_parser* parser,
11646 bool template_keyword_p,
11647 bool check_dependency_p,
11648 bool is_declaration,
11649 bool *is_identifier)
11654 cp_token *token = cp_lexer_peek_token (parser->lexer);
11656 /* If the next token is `operator', then we have either an
11657 operator-function-id or a conversion-function-id. */
11658 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11660 /* We don't know whether we're looking at an
11661 operator-function-id or a conversion-function-id. */
11662 cp_parser_parse_tentatively (parser);
11663 /* Try an operator-function-id. */
11664 identifier = cp_parser_operator_function_id (parser);
11665 /* If that didn't work, try a conversion-function-id. */
11666 if (!cp_parser_parse_definitely (parser))
11668 cp_parser_error (parser, "expected template-name");
11669 return error_mark_node;
11672 /* Look for the identifier. */
11674 identifier = cp_parser_identifier (parser);
11676 /* If we didn't find an identifier, we don't have a template-id. */
11677 if (identifier == error_mark_node)
11678 return error_mark_node;
11680 /* If the name immediately followed the `template' keyword, then it
11681 is a template-name. However, if the next token is not `<', then
11682 we do not treat it as a template-name, since it is not being used
11683 as part of a template-id. This enables us to handle constructs
11686 template <typename T> struct S { S(); };
11687 template <typename T> S<T>::S();
11689 correctly. We would treat `S' as a template -- if it were `S<T>'
11690 -- but we do not if there is no `<'. */
11692 if (processing_template_decl
11693 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11695 /* In a declaration, in a dependent context, we pretend that the
11696 "template" keyword was present in order to improve error
11697 recovery. For example, given:
11699 template <typename T> void f(T::X<int>);
11701 we want to treat "X<int>" as a template-id. */
11703 && !template_keyword_p
11704 && parser->scope && TYPE_P (parser->scope)
11705 && check_dependency_p
11706 && dependent_scope_p (parser->scope)
11707 /* Do not do this for dtors (or ctors), since they never
11708 need the template keyword before their name. */
11709 && !constructor_name_p (identifier, parser->scope))
11711 cp_token_position start = 0;
11713 /* Explain what went wrong. */
11714 error_at (token->location, "non-template %qD used as template",
11716 inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11717 parser->scope, identifier);
11718 /* If parsing tentatively, find the location of the "<" token. */
11719 if (cp_parser_simulate_error (parser))
11720 start = cp_lexer_token_position (parser->lexer, true);
11721 /* Parse the template arguments so that we can issue error
11722 messages about them. */
11723 cp_lexer_consume_token (parser->lexer);
11724 cp_parser_enclosed_template_argument_list (parser);
11725 /* Skip tokens until we find a good place from which to
11726 continue parsing. */
11727 cp_parser_skip_to_closing_parenthesis (parser,
11728 /*recovering=*/true,
11730 /*consume_paren=*/false);
11731 /* If parsing tentatively, permanently remove the
11732 template argument list. That will prevent duplicate
11733 error messages from being issued about the missing
11734 "template" keyword. */
11736 cp_lexer_purge_tokens_after (parser->lexer, start);
11738 *is_identifier = true;
11742 /* If the "template" keyword is present, then there is generally
11743 no point in doing name-lookup, so we just return IDENTIFIER.
11744 But, if the qualifying scope is non-dependent then we can
11745 (and must) do name-lookup normally. */
11746 if (template_keyword_p
11748 || (TYPE_P (parser->scope)
11749 && dependent_type_p (parser->scope))))
11753 /* Look up the name. */
11754 decl = cp_parser_lookup_name (parser, identifier,
11756 /*is_template=*/true,
11757 /*is_namespace=*/false,
11758 check_dependency_p,
11759 /*ambiguous_decls=*/NULL,
11762 /* If DECL is a template, then the name was a template-name. */
11763 if (TREE_CODE (decl) == TEMPLATE_DECL)
11767 tree fn = NULL_TREE;
11769 /* The standard does not explicitly indicate whether a name that
11770 names a set of overloaded declarations, some of which are
11771 templates, is a template-name. However, such a name should
11772 be a template-name; otherwise, there is no way to form a
11773 template-id for the overloaded templates. */
11774 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11775 if (TREE_CODE (fns) == OVERLOAD)
11776 for (fn = fns; fn; fn = OVL_NEXT (fn))
11777 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11782 /* The name does not name a template. */
11783 cp_parser_error (parser, "expected template-name");
11784 return error_mark_node;
11788 /* If DECL is dependent, and refers to a function, then just return
11789 its name; we will look it up again during template instantiation. */
11790 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11792 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11793 if (TYPE_P (scope) && dependent_type_p (scope))
11800 /* Parse a template-argument-list.
11802 template-argument-list:
11803 template-argument ... [opt]
11804 template-argument-list , template-argument ... [opt]
11806 Returns a TREE_VEC containing the arguments. */
11809 cp_parser_template_argument_list (cp_parser* parser)
11811 tree fixed_args[10];
11812 unsigned n_args = 0;
11813 unsigned alloced = 10;
11814 tree *arg_ary = fixed_args;
11816 bool saved_in_template_argument_list_p;
11818 bool saved_non_ice_p;
11820 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11821 parser->in_template_argument_list_p = true;
11822 /* Even if the template-id appears in an integral
11823 constant-expression, the contents of the argument list do
11825 saved_ice_p = parser->integral_constant_expression_p;
11826 parser->integral_constant_expression_p = false;
11827 saved_non_ice_p = parser->non_integral_constant_expression_p;
11828 parser->non_integral_constant_expression_p = false;
11829 /* Parse the arguments. */
11835 /* Consume the comma. */
11836 cp_lexer_consume_token (parser->lexer);
11838 /* Parse the template-argument. */
11839 argument = cp_parser_template_argument (parser);
11841 /* If the next token is an ellipsis, we're expanding a template
11843 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11845 if (argument == error_mark_node)
11847 cp_token *token = cp_lexer_peek_token (parser->lexer);
11848 error_at (token->location,
11849 "expected parameter pack before %<...%>");
11851 /* Consume the `...' token. */
11852 cp_lexer_consume_token (parser->lexer);
11854 /* Make the argument into a TYPE_PACK_EXPANSION or
11855 EXPR_PACK_EXPANSION. */
11856 argument = make_pack_expansion (argument);
11859 if (n_args == alloced)
11863 if (arg_ary == fixed_args)
11865 arg_ary = XNEWVEC (tree, alloced);
11866 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11869 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11871 arg_ary[n_args++] = argument;
11873 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11875 vec = make_tree_vec (n_args);
11878 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11880 if (arg_ary != fixed_args)
11882 parser->non_integral_constant_expression_p = saved_non_ice_p;
11883 parser->integral_constant_expression_p = saved_ice_p;
11884 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11885 #ifdef ENABLE_CHECKING
11886 SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11891 /* Parse a template-argument.
11894 assignment-expression
11898 The representation is that of an assignment-expression, type-id, or
11899 id-expression -- except that the qualified id-expression is
11900 evaluated, so that the value returned is either a DECL or an
11903 Although the standard says "assignment-expression", it forbids
11904 throw-expressions or assignments in the template argument.
11905 Therefore, we use "conditional-expression" instead. */
11908 cp_parser_template_argument (cp_parser* parser)
11913 bool maybe_type_id = false;
11914 cp_token *token = NULL, *argument_start_token = NULL;
11917 /* There's really no way to know what we're looking at, so we just
11918 try each alternative in order.
11922 In a template-argument, an ambiguity between a type-id and an
11923 expression is resolved to a type-id, regardless of the form of
11924 the corresponding template-parameter.
11926 Therefore, we try a type-id first. */
11927 cp_parser_parse_tentatively (parser);
11928 argument = cp_parser_template_type_arg (parser);
11929 /* If there was no error parsing the type-id but the next token is a
11930 '>>', our behavior depends on which dialect of C++ we're
11931 parsing. In C++98, we probably found a typo for '> >'. But there
11932 are type-id which are also valid expressions. For instance:
11934 struct X { int operator >> (int); };
11935 template <int V> struct Foo {};
11938 Here 'X()' is a valid type-id of a function type, but the user just
11939 wanted to write the expression "X() >> 5". Thus, we remember that we
11940 found a valid type-id, but we still try to parse the argument as an
11941 expression to see what happens.
11943 In C++0x, the '>>' will be considered two separate '>'
11945 if (!cp_parser_error_occurred (parser)
11946 && cxx_dialect == cxx98
11947 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11949 maybe_type_id = true;
11950 cp_parser_abort_tentative_parse (parser);
11954 /* If the next token isn't a `,' or a `>', then this argument wasn't
11955 really finished. This means that the argument is not a valid
11957 if (!cp_parser_next_token_ends_template_argument_p (parser))
11958 cp_parser_error (parser, "expected template-argument");
11959 /* If that worked, we're done. */
11960 if (cp_parser_parse_definitely (parser))
11963 /* We're still not sure what the argument will be. */
11964 cp_parser_parse_tentatively (parser);
11965 /* Try a template. */
11966 argument_start_token = cp_lexer_peek_token (parser->lexer);
11967 argument = cp_parser_id_expression (parser,
11968 /*template_keyword_p=*/false,
11969 /*check_dependency_p=*/true,
11971 /*declarator_p=*/false,
11972 /*optional_p=*/false);
11973 /* If the next token isn't a `,' or a `>', then this argument wasn't
11974 really finished. */
11975 if (!cp_parser_next_token_ends_template_argument_p (parser))
11976 cp_parser_error (parser, "expected template-argument");
11977 if (!cp_parser_error_occurred (parser))
11979 /* Figure out what is being referred to. If the id-expression
11980 was for a class template specialization, then we will have a
11981 TYPE_DECL at this point. There is no need to do name lookup
11982 at this point in that case. */
11983 if (TREE_CODE (argument) != TYPE_DECL)
11984 argument = cp_parser_lookup_name (parser, argument,
11986 /*is_template=*/template_p,
11987 /*is_namespace=*/false,
11988 /*check_dependency=*/true,
11989 /*ambiguous_decls=*/NULL,
11990 argument_start_token->location);
11991 if (TREE_CODE (argument) != TEMPLATE_DECL
11992 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11993 cp_parser_error (parser, "expected template-name");
11995 if (cp_parser_parse_definitely (parser))
11997 /* It must be a non-type argument. There permitted cases are given
11998 in [temp.arg.nontype]:
12000 -- an integral constant-expression of integral or enumeration
12003 -- the name of a non-type template-parameter; or
12005 -- the name of an object or function with external linkage...
12007 -- the address of an object or function with external linkage...
12009 -- a pointer to member... */
12010 /* Look for a non-type template parameter. */
12011 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12013 cp_parser_parse_tentatively (parser);
12014 argument = cp_parser_primary_expression (parser,
12015 /*address_p=*/false,
12017 /*template_arg_p=*/true,
12019 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12020 || !cp_parser_next_token_ends_template_argument_p (parser))
12021 cp_parser_simulate_error (parser);
12022 if (cp_parser_parse_definitely (parser))
12026 /* If the next token is "&", the argument must be the address of an
12027 object or function with external linkage. */
12028 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12030 cp_lexer_consume_token (parser->lexer);
12031 /* See if we might have an id-expression. */
12032 token = cp_lexer_peek_token (parser->lexer);
12033 if (token->type == CPP_NAME
12034 || token->keyword == RID_OPERATOR
12035 || token->type == CPP_SCOPE
12036 || token->type == CPP_TEMPLATE_ID
12037 || token->type == CPP_NESTED_NAME_SPECIFIER)
12039 cp_parser_parse_tentatively (parser);
12040 argument = cp_parser_primary_expression (parser,
12043 /*template_arg_p=*/true,
12045 if (cp_parser_error_occurred (parser)
12046 || !cp_parser_next_token_ends_template_argument_p (parser))
12047 cp_parser_abort_tentative_parse (parser);
12052 if (TREE_CODE (argument) == INDIRECT_REF)
12054 gcc_assert (REFERENCE_REF_P (argument));
12055 argument = TREE_OPERAND (argument, 0);
12058 /* If we're in a template, we represent a qualified-id referring
12059 to a static data member as a SCOPE_REF even if the scope isn't
12060 dependent so that we can check access control later. */
12062 if (TREE_CODE (probe) == SCOPE_REF)
12063 probe = TREE_OPERAND (probe, 1);
12064 if (TREE_CODE (probe) == VAR_DECL)
12066 /* A variable without external linkage might still be a
12067 valid constant-expression, so no error is issued here
12068 if the external-linkage check fails. */
12069 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12070 cp_parser_simulate_error (parser);
12072 else if (is_overloaded_fn (argument))
12073 /* All overloaded functions are allowed; if the external
12074 linkage test does not pass, an error will be issued
12078 && (TREE_CODE (argument) == OFFSET_REF
12079 || TREE_CODE (argument) == SCOPE_REF))
12080 /* A pointer-to-member. */
12082 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12085 cp_parser_simulate_error (parser);
12087 if (cp_parser_parse_definitely (parser))
12090 argument = build_x_unary_op (ADDR_EXPR, argument,
12091 tf_warning_or_error);
12096 /* If the argument started with "&", there are no other valid
12097 alternatives at this point. */
12100 cp_parser_error (parser, "invalid non-type template argument");
12101 return error_mark_node;
12104 /* If the argument wasn't successfully parsed as a type-id followed
12105 by '>>', the argument can only be a constant expression now.
12106 Otherwise, we try parsing the constant-expression tentatively,
12107 because the argument could really be a type-id. */
12109 cp_parser_parse_tentatively (parser);
12110 argument = cp_parser_constant_expression (parser,
12111 /*allow_non_constant_p=*/false,
12112 /*non_constant_p=*/NULL);
12113 argument = fold_non_dependent_expr (argument);
12114 if (!maybe_type_id)
12116 if (!cp_parser_next_token_ends_template_argument_p (parser))
12117 cp_parser_error (parser, "expected template-argument");
12118 if (cp_parser_parse_definitely (parser))
12120 /* We did our best to parse the argument as a non type-id, but that
12121 was the only alternative that matched (albeit with a '>' after
12122 it). We can assume it's just a typo from the user, and a
12123 diagnostic will then be issued. */
12124 return cp_parser_template_type_arg (parser);
12127 /* Parse an explicit-instantiation.
12129 explicit-instantiation:
12130 template declaration
12132 Although the standard says `declaration', what it really means is:
12134 explicit-instantiation:
12135 template decl-specifier-seq [opt] declarator [opt] ;
12137 Things like `template int S<int>::i = 5, int S<double>::j;' are not
12138 supposed to be allowed. A defect report has been filed about this
12143 explicit-instantiation:
12144 storage-class-specifier template
12145 decl-specifier-seq [opt] declarator [opt] ;
12146 function-specifier template
12147 decl-specifier-seq [opt] declarator [opt] ; */
12150 cp_parser_explicit_instantiation (cp_parser* parser)
12152 int declares_class_or_enum;
12153 cp_decl_specifier_seq decl_specifiers;
12154 tree extension_specifier = NULL_TREE;
12156 timevar_push (TV_TEMPLATE_INST);
12158 /* Look for an (optional) storage-class-specifier or
12159 function-specifier. */
12160 if (cp_parser_allow_gnu_extensions_p (parser))
12162 extension_specifier
12163 = cp_parser_storage_class_specifier_opt (parser);
12164 if (!extension_specifier)
12165 extension_specifier
12166 = cp_parser_function_specifier_opt (parser,
12167 /*decl_specs=*/NULL);
12170 /* Look for the `template' keyword. */
12171 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12172 /* Let the front end know that we are processing an explicit
12174 begin_explicit_instantiation ();
12175 /* [temp.explicit] says that we are supposed to ignore access
12176 control while processing explicit instantiation directives. */
12177 push_deferring_access_checks (dk_no_check);
12178 /* Parse a decl-specifier-seq. */
12179 cp_parser_decl_specifier_seq (parser,
12180 CP_PARSER_FLAGS_OPTIONAL,
12182 &declares_class_or_enum);
12183 /* If there was exactly one decl-specifier, and it declared a class,
12184 and there's no declarator, then we have an explicit type
12186 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12190 type = check_tag_decl (&decl_specifiers);
12191 /* Turn access control back on for names used during
12192 template instantiation. */
12193 pop_deferring_access_checks ();
12195 do_type_instantiation (type, extension_specifier,
12196 /*complain=*/tf_error);
12200 cp_declarator *declarator;
12203 /* Parse the declarator. */
12205 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12206 /*ctor_dtor_or_conv_p=*/NULL,
12207 /*parenthesized_p=*/NULL,
12208 /*member_p=*/false);
12209 if (declares_class_or_enum & 2)
12210 cp_parser_check_for_definition_in_return_type (declarator,
12211 decl_specifiers.type,
12212 decl_specifiers.type_location);
12213 if (declarator != cp_error_declarator)
12215 if (decl_specifiers.specs[(int)ds_inline])
12216 permerror (input_location, "explicit instantiation shall not use"
12217 " %<inline%> specifier");
12218 if (decl_specifiers.specs[(int)ds_constexpr])
12219 permerror (input_location, "explicit instantiation shall not use"
12220 " %<constexpr%> specifier");
12222 decl = grokdeclarator (declarator, &decl_specifiers,
12223 NORMAL, 0, &decl_specifiers.attributes);
12224 /* Turn access control back on for names used during
12225 template instantiation. */
12226 pop_deferring_access_checks ();
12227 /* Do the explicit instantiation. */
12228 do_decl_instantiation (decl, extension_specifier);
12232 pop_deferring_access_checks ();
12233 /* Skip the body of the explicit instantiation. */
12234 cp_parser_skip_to_end_of_statement (parser);
12237 /* We're done with the instantiation. */
12238 end_explicit_instantiation ();
12240 cp_parser_consume_semicolon_at_end_of_statement (parser);
12242 timevar_pop (TV_TEMPLATE_INST);
12245 /* Parse an explicit-specialization.
12247 explicit-specialization:
12248 template < > declaration
12250 Although the standard says `declaration', what it really means is:
12252 explicit-specialization:
12253 template <> decl-specifier [opt] init-declarator [opt] ;
12254 template <> function-definition
12255 template <> explicit-specialization
12256 template <> template-declaration */
12259 cp_parser_explicit_specialization (cp_parser* parser)
12261 bool need_lang_pop;
12262 cp_token *token = cp_lexer_peek_token (parser->lexer);
12264 /* Look for the `template' keyword. */
12265 cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12266 /* Look for the `<'. */
12267 cp_parser_require (parser, CPP_LESS, RT_LESS);
12268 /* Look for the `>'. */
12269 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12270 /* We have processed another parameter list. */
12271 ++parser->num_template_parameter_lists;
12274 A template ... explicit specialization ... shall not have C
12276 if (current_lang_name == lang_name_c)
12278 error_at (token->location, "template specialization with C linkage");
12279 /* Give it C++ linkage to avoid confusing other parts of the
12281 push_lang_context (lang_name_cplusplus);
12282 need_lang_pop = true;
12285 need_lang_pop = false;
12286 /* Let the front end know that we are beginning a specialization. */
12287 if (!begin_specialization ())
12289 end_specialization ();
12293 /* If the next keyword is `template', we need to figure out whether
12294 or not we're looking a template-declaration. */
12295 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12297 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12298 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12299 cp_parser_template_declaration_after_export (parser,
12300 /*member_p=*/false);
12302 cp_parser_explicit_specialization (parser);
12305 /* Parse the dependent declaration. */
12306 cp_parser_single_declaration (parser,
12308 /*member_p=*/false,
12309 /*explicit_specialization_p=*/true,
12310 /*friend_p=*/NULL);
12311 /* We're done with the specialization. */
12312 end_specialization ();
12313 /* For the erroneous case of a template with C linkage, we pushed an
12314 implicit C++ linkage scope; exit that scope now. */
12316 pop_lang_context ();
12317 /* We're done with this parameter list. */
12318 --parser->num_template_parameter_lists;
12321 /* Parse a type-specifier.
12324 simple-type-specifier
12327 elaborated-type-specifier
12335 Returns a representation of the type-specifier. For a
12336 class-specifier, enum-specifier, or elaborated-type-specifier, a
12337 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12339 The parser flags FLAGS is used to control type-specifier parsing.
12341 If IS_DECLARATION is TRUE, then this type-specifier is appearing
12342 in a decl-specifier-seq.
12344 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12345 class-specifier, enum-specifier, or elaborated-type-specifier, then
12346 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
12347 if a type is declared; 2 if it is defined. Otherwise, it is set to
12350 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12351 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
12352 is set to FALSE. */
12355 cp_parser_type_specifier (cp_parser* parser,
12356 cp_parser_flags flags,
12357 cp_decl_specifier_seq *decl_specs,
12358 bool is_declaration,
12359 int* declares_class_or_enum,
12360 bool* is_cv_qualifier)
12362 tree type_spec = NULL_TREE;
12365 cp_decl_spec ds = ds_last;
12367 /* Assume this type-specifier does not declare a new type. */
12368 if (declares_class_or_enum)
12369 *declares_class_or_enum = 0;
12370 /* And that it does not specify a cv-qualifier. */
12371 if (is_cv_qualifier)
12372 *is_cv_qualifier = false;
12373 /* Peek at the next token. */
12374 token = cp_lexer_peek_token (parser->lexer);
12376 /* If we're looking at a keyword, we can use that to guide the
12377 production we choose. */
12378 keyword = token->keyword;
12382 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12383 goto elaborated_type_specifier;
12385 /* Look for the enum-specifier. */
12386 type_spec = cp_parser_enum_specifier (parser);
12387 /* If that worked, we're done. */
12390 if (declares_class_or_enum)
12391 *declares_class_or_enum = 2;
12393 cp_parser_set_decl_spec_type (decl_specs,
12396 /*user_defined_p=*/true);
12400 goto elaborated_type_specifier;
12402 /* Any of these indicate either a class-specifier, or an
12403 elaborated-type-specifier. */
12407 if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12408 goto elaborated_type_specifier;
12410 /* Parse tentatively so that we can back up if we don't find a
12411 class-specifier. */
12412 cp_parser_parse_tentatively (parser);
12413 /* Look for the class-specifier. */
12414 type_spec = cp_parser_class_specifier (parser);
12415 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12416 /* If that worked, we're done. */
12417 if (cp_parser_parse_definitely (parser))
12419 if (declares_class_or_enum)
12420 *declares_class_or_enum = 2;
12422 cp_parser_set_decl_spec_type (decl_specs,
12425 /*user_defined_p=*/true);
12429 /* Fall through. */
12430 elaborated_type_specifier:
12431 /* We're declaring (not defining) a class or enum. */
12432 if (declares_class_or_enum)
12433 *declares_class_or_enum = 1;
12435 /* Fall through. */
12437 /* Look for an elaborated-type-specifier. */
12439 = (cp_parser_elaborated_type_specifier
12441 decl_specs && decl_specs->specs[(int) ds_friend],
12444 cp_parser_set_decl_spec_type (decl_specs,
12447 /*user_defined_p=*/true);
12452 if (is_cv_qualifier)
12453 *is_cv_qualifier = true;
12458 if (is_cv_qualifier)
12459 *is_cv_qualifier = true;
12464 if (is_cv_qualifier)
12465 *is_cv_qualifier = true;
12469 /* The `__complex__' keyword is a GNU extension. */
12477 /* Handle simple keywords. */
12482 ++decl_specs->specs[(int)ds];
12483 decl_specs->any_specifiers_p = true;
12485 return cp_lexer_consume_token (parser->lexer)->u.value;
12488 /* If we do not already have a type-specifier, assume we are looking
12489 at a simple-type-specifier. */
12490 type_spec = cp_parser_simple_type_specifier (parser,
12494 /* If we didn't find a type-specifier, and a type-specifier was not
12495 optional in this context, issue an error message. */
12496 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12498 cp_parser_error (parser, "expected type specifier");
12499 return error_mark_node;
12505 /* Parse a simple-type-specifier.
12507 simple-type-specifier:
12508 :: [opt] nested-name-specifier [opt] type-name
12509 :: [opt] nested-name-specifier template template-id
12524 simple-type-specifier:
12526 decltype ( expression )
12529 __underlying_type ( type-id )
12533 simple-type-specifier:
12535 __typeof__ unary-expression
12536 __typeof__ ( type-id )
12538 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
12539 appropriately updated. */
12542 cp_parser_simple_type_specifier (cp_parser* parser,
12543 cp_decl_specifier_seq *decl_specs,
12544 cp_parser_flags flags)
12546 tree type = NULL_TREE;
12549 /* Peek at the next token. */
12550 token = cp_lexer_peek_token (parser->lexer);
12552 /* If we're looking at a keyword, things are easy. */
12553 switch (token->keyword)
12557 decl_specs->explicit_char_p = true;
12558 type = char_type_node;
12561 type = char16_type_node;
12564 type = char32_type_node;
12567 type = wchar_type_node;
12570 type = boolean_type_node;
12574 ++decl_specs->specs[(int) ds_short];
12575 type = short_integer_type_node;
12579 decl_specs->explicit_int_p = true;
12580 type = integer_type_node;
12583 if (!int128_integer_type_node)
12586 decl_specs->explicit_int128_p = true;
12587 type = int128_integer_type_node;
12591 ++decl_specs->specs[(int) ds_long];
12592 type = long_integer_type_node;
12596 ++decl_specs->specs[(int) ds_signed];
12597 type = integer_type_node;
12601 ++decl_specs->specs[(int) ds_unsigned];
12602 type = unsigned_type_node;
12605 type = float_type_node;
12608 type = double_type_node;
12611 type = void_type_node;
12615 maybe_warn_cpp0x (CPP0X_AUTO);
12616 type = make_auto ();
12620 /* Parse the `decltype' type. */
12621 type = cp_parser_decltype (parser);
12624 cp_parser_set_decl_spec_type (decl_specs, type,
12626 /*user_defined_p=*/true);
12631 /* Consume the `typeof' token. */
12632 cp_lexer_consume_token (parser->lexer);
12633 /* Parse the operand to `typeof'. */
12634 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12635 /* If it is not already a TYPE, take its type. */
12636 if (!TYPE_P (type))
12637 type = finish_typeof (type);
12640 cp_parser_set_decl_spec_type (decl_specs, type,
12642 /*user_defined_p=*/true);
12646 case RID_UNDERLYING_TYPE:
12647 type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12650 cp_parser_set_decl_spec_type (decl_specs, type,
12652 /*user_defined_p=*/true);
12660 /* If the type-specifier was for a built-in type, we're done. */
12663 /* Record the type. */
12665 && (token->keyword != RID_SIGNED
12666 && token->keyword != RID_UNSIGNED
12667 && token->keyword != RID_SHORT
12668 && token->keyword != RID_LONG))
12669 cp_parser_set_decl_spec_type (decl_specs,
12672 /*user_defined=*/false);
12674 decl_specs->any_specifiers_p = true;
12676 /* Consume the token. */
12677 cp_lexer_consume_token (parser->lexer);
12679 /* There is no valid C++ program where a non-template type is
12680 followed by a "<". That usually indicates that the user thought
12681 that the type was a template. */
12682 cp_parser_check_for_invalid_template_id (parser, type, token->location);
12684 return TYPE_NAME (type);
12687 /* The type-specifier must be a user-defined type. */
12688 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12693 /* Don't gobble tokens or issue error messages if this is an
12694 optional type-specifier. */
12695 if (flags & CP_PARSER_FLAGS_OPTIONAL)
12696 cp_parser_parse_tentatively (parser);
12698 /* Look for the optional `::' operator. */
12700 = (cp_parser_global_scope_opt (parser,
12701 /*current_scope_valid_p=*/false)
12703 /* Look for the nested-name specifier. */
12705 = (cp_parser_nested_name_specifier_opt (parser,
12706 /*typename_keyword_p=*/false,
12707 /*check_dependency_p=*/true,
12709 /*is_declaration=*/false)
12711 token = cp_lexer_peek_token (parser->lexer);
12712 /* If we have seen a nested-name-specifier, and the next token
12713 is `template', then we are using the template-id production. */
12715 && cp_parser_optional_template_keyword (parser))
12717 /* Look for the template-id. */
12718 type = cp_parser_template_id (parser,
12719 /*template_keyword_p=*/true,
12720 /*check_dependency_p=*/true,
12721 /*is_declaration=*/false);
12722 /* If the template-id did not name a type, we are out of
12724 if (TREE_CODE (type) != TYPE_DECL)
12726 cp_parser_error (parser, "expected template-id for type");
12730 /* Otherwise, look for a type-name. */
12732 type = cp_parser_type_name (parser);
12733 /* Keep track of all name-lookups performed in class scopes. */
12737 && TREE_CODE (type) == TYPE_DECL
12738 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12739 maybe_note_name_used_in_class (DECL_NAME (type), type);
12740 /* If it didn't work out, we don't have a TYPE. */
12741 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12742 && !cp_parser_parse_definitely (parser))
12744 if (type && decl_specs)
12745 cp_parser_set_decl_spec_type (decl_specs, type,
12747 /*user_defined=*/true);
12750 /* If we didn't get a type-name, issue an error message. */
12751 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12753 cp_parser_error (parser, "expected type-name");
12754 return error_mark_node;
12757 if (type && type != error_mark_node)
12759 /* See if TYPE is an Objective-C type, and if so, parse and
12760 accept any protocol references following it. Do this before
12761 the cp_parser_check_for_invalid_template_id() call, because
12762 Objective-C types can be followed by '<...>' which would
12763 enclose protocol names rather than template arguments, and so
12764 everything is fine. */
12765 if (c_dialect_objc () && !parser->scope
12766 && (objc_is_id (type) || objc_is_class_name (type)))
12768 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12769 tree qual_type = objc_get_protocol_qualified_type (type, protos);
12771 /* Clobber the "unqualified" type previously entered into
12772 DECL_SPECS with the new, improved protocol-qualified version. */
12774 decl_specs->type = qual_type;
12779 /* There is no valid C++ program where a non-template type is
12780 followed by a "<". That usually indicates that the user
12781 thought that the type was a template. */
12782 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12789 /* Parse a type-name.
12802 Returns a TYPE_DECL for the type. */
12805 cp_parser_type_name (cp_parser* parser)
12809 /* We can't know yet whether it is a class-name or not. */
12810 cp_parser_parse_tentatively (parser);
12811 /* Try a class-name. */
12812 type_decl = cp_parser_class_name (parser,
12813 /*typename_keyword_p=*/false,
12814 /*template_keyword_p=*/false,
12816 /*check_dependency_p=*/true,
12817 /*class_head_p=*/false,
12818 /*is_declaration=*/false);
12819 /* If it's not a class-name, keep looking. */
12820 if (!cp_parser_parse_definitely (parser))
12822 /* It must be a typedef-name or an enum-name. */
12823 return cp_parser_nonclass_name (parser);
12829 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12837 Returns a TYPE_DECL for the type. */
12840 cp_parser_nonclass_name (cp_parser* parser)
12845 cp_token *token = cp_lexer_peek_token (parser->lexer);
12846 identifier = cp_parser_identifier (parser);
12847 if (identifier == error_mark_node)
12848 return error_mark_node;
12850 /* Look up the type-name. */
12851 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12853 if (TREE_CODE (type_decl) != TYPE_DECL
12854 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12856 /* See if this is an Objective-C type. */
12857 tree protos = cp_parser_objc_protocol_refs_opt (parser);
12858 tree type = objc_get_protocol_qualified_type (identifier, protos);
12860 type_decl = TYPE_NAME (type);
12863 /* Issue an error if we did not find a type-name. */
12864 if (TREE_CODE (type_decl) != TYPE_DECL
12865 /* In Objective-C, we have the complication that class names are
12866 normally type names and start declarations (eg, the
12867 "NSObject" in "NSObject *object;"), but can be used in an
12868 Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12869 is an expression. So, a classname followed by a dot is not a
12870 valid type-name. */
12871 || (objc_is_class_name (TREE_TYPE (type_decl))
12872 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12874 if (!cp_parser_simulate_error (parser))
12875 cp_parser_name_lookup_error (parser, identifier, type_decl,
12876 NLE_TYPE, token->location);
12877 return error_mark_node;
12879 /* Remember that the name was used in the definition of the
12880 current class so that we can check later to see if the
12881 meaning would have been different after the class was
12882 entirely defined. */
12883 else if (type_decl != error_mark_node
12885 maybe_note_name_used_in_class (identifier, type_decl);
12890 /* Parse an elaborated-type-specifier. Note that the grammar given
12891 here incorporates the resolution to DR68.
12893 elaborated-type-specifier:
12894 class-key :: [opt] nested-name-specifier [opt] identifier
12895 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12896 enum-key :: [opt] nested-name-specifier [opt] identifier
12897 typename :: [opt] nested-name-specifier identifier
12898 typename :: [opt] nested-name-specifier template [opt]
12903 elaborated-type-specifier:
12904 class-key attributes :: [opt] nested-name-specifier [opt] identifier
12905 class-key attributes :: [opt] nested-name-specifier [opt]
12906 template [opt] template-id
12907 enum attributes :: [opt] nested-name-specifier [opt] identifier
12909 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12910 declared `friend'. If IS_DECLARATION is TRUE, then this
12911 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12912 something is being declared.
12914 Returns the TYPE specified. */
12917 cp_parser_elaborated_type_specifier (cp_parser* parser,
12919 bool is_declaration)
12921 enum tag_types tag_type;
12923 tree type = NULL_TREE;
12924 tree attributes = NULL_TREE;
12926 cp_token *token = NULL;
12928 /* See if we're looking at the `enum' keyword. */
12929 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12931 /* Consume the `enum' token. */
12932 cp_lexer_consume_token (parser->lexer);
12933 /* Remember that it's an enumeration type. */
12934 tag_type = enum_type;
12935 /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12936 enums) is used here. */
12937 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12938 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12940 pedwarn (input_location, 0, "elaborated-type-specifier "
12941 "for a scoped enum must not use the %<%D%> keyword",
12942 cp_lexer_peek_token (parser->lexer)->u.value);
12943 /* Consume the `struct' or `class' and parse it anyway. */
12944 cp_lexer_consume_token (parser->lexer);
12946 /* Parse the attributes. */
12947 attributes = cp_parser_attributes_opt (parser);
12949 /* Or, it might be `typename'. */
12950 else if (cp_lexer_next_token_is_keyword (parser->lexer,
12953 /* Consume the `typename' token. */
12954 cp_lexer_consume_token (parser->lexer);
12955 /* Remember that it's a `typename' type. */
12956 tag_type = typename_type;
12958 /* Otherwise it must be a class-key. */
12961 tag_type = cp_parser_class_key (parser);
12962 if (tag_type == none_type)
12963 return error_mark_node;
12964 /* Parse the attributes. */
12965 attributes = cp_parser_attributes_opt (parser);
12968 /* Look for the `::' operator. */
12969 globalscope = cp_parser_global_scope_opt (parser,
12970 /*current_scope_valid_p=*/false);
12971 /* Look for the nested-name-specifier. */
12972 if (tag_type == typename_type && !globalscope)
12974 if (!cp_parser_nested_name_specifier (parser,
12975 /*typename_keyword_p=*/true,
12976 /*check_dependency_p=*/true,
12979 return error_mark_node;
12982 /* Even though `typename' is not present, the proposed resolution
12983 to Core Issue 180 says that in `class A<T>::B', `B' should be
12984 considered a type-name, even if `A<T>' is dependent. */
12985 cp_parser_nested_name_specifier_opt (parser,
12986 /*typename_keyword_p=*/true,
12987 /*check_dependency_p=*/true,
12990 /* For everything but enumeration types, consider a template-id.
12991 For an enumeration type, consider only a plain identifier. */
12992 if (tag_type != enum_type)
12994 bool template_p = false;
12997 /* Allow the `template' keyword. */
12998 template_p = cp_parser_optional_template_keyword (parser);
12999 /* If we didn't see `template', we don't know if there's a
13000 template-id or not. */
13002 cp_parser_parse_tentatively (parser);
13003 /* Parse the template-id. */
13004 token = cp_lexer_peek_token (parser->lexer);
13005 decl = cp_parser_template_id (parser, template_p,
13006 /*check_dependency_p=*/true,
13008 /* If we didn't find a template-id, look for an ordinary
13010 if (!template_p && !cp_parser_parse_definitely (parser))
13012 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13013 in effect, then we must assume that, upon instantiation, the
13014 template will correspond to a class. */
13015 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13016 && tag_type == typename_type)
13017 type = make_typename_type (parser->scope, decl,
13019 /*complain=*/tf_error);
13020 /* If the `typename' keyword is in effect and DECL is not a type
13021 decl. Then type is non existant. */
13022 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13025 type = TREE_TYPE (decl);
13030 token = cp_lexer_peek_token (parser->lexer);
13031 identifier = cp_parser_identifier (parser);
13033 if (identifier == error_mark_node)
13035 parser->scope = NULL_TREE;
13036 return error_mark_node;
13039 /* For a `typename', we needn't call xref_tag. */
13040 if (tag_type == typename_type
13041 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13042 return cp_parser_make_typename_type (parser, parser->scope,
13045 /* Look up a qualified name in the usual way. */
13049 tree ambiguous_decls;
13051 decl = cp_parser_lookup_name (parser, identifier,
13053 /*is_template=*/false,
13054 /*is_namespace=*/false,
13055 /*check_dependency=*/true,
13059 /* If the lookup was ambiguous, an error will already have been
13061 if (ambiguous_decls)
13062 return error_mark_node;
13064 /* If we are parsing friend declaration, DECL may be a
13065 TEMPLATE_DECL tree node here. However, we need to check
13066 whether this TEMPLATE_DECL results in valid code. Consider
13067 the following example:
13070 template <class T> class C {};
13073 template <class T> friend class N::C; // #1, valid code
13075 template <class T> class Y {
13076 friend class N::C; // #2, invalid code
13079 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13080 name lookup of `N::C'. We see that friend declaration must
13081 be template for the code to be valid. Note that
13082 processing_template_decl does not work here since it is
13083 always 1 for the above two cases. */
13085 decl = (cp_parser_maybe_treat_template_as_class
13086 (decl, /*tag_name_p=*/is_friend
13087 && parser->num_template_parameter_lists));
13089 if (TREE_CODE (decl) != TYPE_DECL)
13091 cp_parser_diagnose_invalid_type_name (parser,
13095 return error_mark_node;
13098 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13100 bool allow_template = (parser->num_template_parameter_lists
13101 || DECL_SELF_REFERENCE_P (decl));
13102 type = check_elaborated_type_specifier (tag_type, decl,
13105 if (type == error_mark_node)
13106 return error_mark_node;
13109 /* Forward declarations of nested types, such as
13114 are invalid unless all components preceding the final '::'
13115 are complete. If all enclosing types are complete, these
13116 declarations become merely pointless.
13118 Invalid forward declarations of nested types are errors
13119 caught elsewhere in parsing. Those that are pointless arrive
13122 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13123 && !is_friend && !processing_explicit_instantiation)
13124 warning (0, "declaration %qD does not declare anything", decl);
13126 type = TREE_TYPE (decl);
13130 /* An elaborated-type-specifier sometimes introduces a new type and
13131 sometimes names an existing type. Normally, the rule is that it
13132 introduces a new type only if there is not an existing type of
13133 the same name already in scope. For example, given:
13136 void f() { struct S s; }
13138 the `struct S' in the body of `f' is the same `struct S' as in
13139 the global scope; the existing definition is used. However, if
13140 there were no global declaration, this would introduce a new
13141 local class named `S'.
13143 An exception to this rule applies to the following code:
13145 namespace N { struct S; }
13147 Here, the elaborated-type-specifier names a new type
13148 unconditionally; even if there is already an `S' in the
13149 containing scope this declaration names a new type.
13150 This exception only applies if the elaborated-type-specifier
13151 forms the complete declaration:
13155 A declaration consisting solely of `class-key identifier ;' is
13156 either a redeclaration of the name in the current scope or a
13157 forward declaration of the identifier as a class name. It
13158 introduces the name into the current scope.
13160 We are in this situation precisely when the next token is a `;'.
13162 An exception to the exception is that a `friend' declaration does
13163 *not* name a new type; i.e., given:
13165 struct S { friend struct T; };
13167 `T' is not a new type in the scope of `S'.
13169 Also, `new struct S' or `sizeof (struct S)' never results in the
13170 definition of a new type; a new type can only be declared in a
13171 declaration context. */
13177 /* Friends have special name lookup rules. */
13178 ts = ts_within_enclosing_non_class;
13179 else if (is_declaration
13180 && cp_lexer_next_token_is (parser->lexer,
13182 /* This is a `class-key identifier ;' */
13188 (parser->num_template_parameter_lists
13189 && (cp_parser_next_token_starts_class_definition_p (parser)
13190 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13191 /* An unqualified name was used to reference this type, so
13192 there were no qualifying templates. */
13193 if (!cp_parser_check_template_parameters (parser,
13194 /*num_templates=*/0,
13196 /*declarator=*/NULL))
13197 return error_mark_node;
13198 type = xref_tag (tag_type, identifier, ts, template_p);
13202 if (type == error_mark_node)
13203 return error_mark_node;
13205 /* Allow attributes on forward declarations of classes. */
13208 if (TREE_CODE (type) == TYPENAME_TYPE)
13209 warning (OPT_Wattributes,
13210 "attributes ignored on uninstantiated type");
13211 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13212 && ! processing_explicit_instantiation)
13213 warning (OPT_Wattributes,
13214 "attributes ignored on template instantiation");
13215 else if (is_declaration && cp_parser_declares_only_class_p (parser))
13216 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13218 warning (OPT_Wattributes,
13219 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13222 if (tag_type != enum_type)
13223 cp_parser_check_class_key (tag_type, type);
13225 /* A "<" cannot follow an elaborated type specifier. If that
13226 happens, the user was probably trying to form a template-id. */
13227 cp_parser_check_for_invalid_template_id (parser, type, token->location);
13232 /* Parse an enum-specifier.
13235 enum-head { enumerator-list [opt] }
13238 enum-key identifier [opt] enum-base [opt]
13239 enum-key nested-name-specifier identifier enum-base [opt]
13244 enum struct [C++0x]
13247 : type-specifier-seq
13249 opaque-enum-specifier:
13250 enum-key identifier enum-base [opt] ;
13253 enum-key attributes[opt] identifier [opt] enum-base [opt]
13254 { enumerator-list [opt] }attributes[opt]
13256 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13257 if the token stream isn't an enum-specifier after all. */
13260 cp_parser_enum_specifier (cp_parser* parser)
13263 tree type = NULL_TREE;
13265 tree nested_name_specifier = NULL_TREE;
13267 bool scoped_enum_p = false;
13268 bool has_underlying_type = false;
13269 bool nested_being_defined = false;
13270 bool new_value_list = false;
13271 bool is_new_type = false;
13272 bool is_anonymous = false;
13273 tree underlying_type = NULL_TREE;
13274 cp_token *type_start_token = NULL;
13275 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13277 parser->colon_corrects_to_scope_p = false;
13279 /* Parse tentatively so that we can back up if we don't find a
13281 cp_parser_parse_tentatively (parser);
13283 /* Caller guarantees that the current token is 'enum', an identifier
13284 possibly follows, and the token after that is an opening brace.
13285 If we don't have an identifier, fabricate an anonymous name for
13286 the enumeration being defined. */
13287 cp_lexer_consume_token (parser->lexer);
13289 /* Parse the "class" or "struct", which indicates a scoped
13290 enumeration type in C++0x. */
13291 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13292 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13294 if (cxx_dialect < cxx0x)
13295 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13297 /* Consume the `struct' or `class' token. */
13298 cp_lexer_consume_token (parser->lexer);
13300 scoped_enum_p = true;
13303 attributes = cp_parser_attributes_opt (parser);
13305 /* Clear the qualification. */
13306 parser->scope = NULL_TREE;
13307 parser->qualifying_scope = NULL_TREE;
13308 parser->object_scope = NULL_TREE;
13310 /* Figure out in what scope the declaration is being placed. */
13311 prev_scope = current_scope ();
13313 type_start_token = cp_lexer_peek_token (parser->lexer);
13315 push_deferring_access_checks (dk_no_check);
13316 nested_name_specifier
13317 = cp_parser_nested_name_specifier_opt (parser,
13318 /*typename_keyword_p=*/true,
13319 /*check_dependency_p=*/false,
13321 /*is_declaration=*/false);
13323 if (nested_name_specifier)
13327 identifier = cp_parser_identifier (parser);
13328 name = cp_parser_lookup_name (parser, identifier,
13330 /*is_template=*/false,
13331 /*is_namespace=*/false,
13332 /*check_dependency=*/true,
13333 /*ambiguous_decls=*/NULL,
13337 type = TREE_TYPE (name);
13338 if (TREE_CODE (type) == TYPENAME_TYPE)
13340 /* Are template enums allowed in ISO? */
13341 if (template_parm_scope_p ())
13342 pedwarn (type_start_token->location, OPT_pedantic,
13343 "%qD is an enumeration template", name);
13344 /* ignore a typename reference, for it will be solved by name
13350 error_at (type_start_token->location,
13351 "%qD is not an enumerator-name", identifier);
13355 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13356 identifier = cp_parser_identifier (parser);
13359 identifier = make_anon_name ();
13360 is_anonymous = true;
13363 pop_deferring_access_checks ();
13365 /* Check for the `:' that denotes a specified underlying type in C++0x.
13366 Note that a ':' could also indicate a bitfield width, however. */
13367 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13369 cp_decl_specifier_seq type_specifiers;
13371 /* Consume the `:'. */
13372 cp_lexer_consume_token (parser->lexer);
13374 /* Parse the type-specifier-seq. */
13375 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13376 /*is_trailing_return=*/false,
13379 /* At this point this is surely not elaborated type specifier. */
13380 if (!cp_parser_parse_definitely (parser))
13383 if (cxx_dialect < cxx0x)
13384 maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13386 has_underlying_type = true;
13388 /* If that didn't work, stop. */
13389 if (type_specifiers.type != error_mark_node)
13391 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13392 /*initialized=*/0, NULL);
13393 if (underlying_type == error_mark_node)
13394 underlying_type = NULL_TREE;
13398 /* Look for the `{' but don't consume it yet. */
13399 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13401 if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13403 cp_parser_error (parser, "expected %<{%>");
13404 if (has_underlying_type)
13410 /* An opaque-enum-specifier must have a ';' here. */
13411 if ((scoped_enum_p || underlying_type)
13412 && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13414 cp_parser_error (parser, "expected %<;%> or %<{%>");
13415 if (has_underlying_type)
13423 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13426 if (nested_name_specifier)
13428 if (CLASS_TYPE_P (nested_name_specifier))
13430 nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13431 TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13432 push_scope (nested_name_specifier);
13434 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13436 push_nested_namespace (nested_name_specifier);
13440 /* Issue an error message if type-definitions are forbidden here. */
13441 if (!cp_parser_check_type_definition (parser))
13442 type = error_mark_node;
13444 /* Create the new type. We do this before consuming the opening
13445 brace so the enum will be recorded as being on the line of its
13446 tag (or the 'enum' keyword, if there is no tag). */
13447 type = start_enum (identifier, type, underlying_type,
13448 scoped_enum_p, &is_new_type);
13450 /* If the next token is not '{' it is an opaque-enum-specifier or an
13451 elaborated-type-specifier. */
13452 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13454 timevar_push (TV_PARSE_ENUM);
13455 if (nested_name_specifier)
13457 /* The following catches invalid code such as:
13458 enum class S<int>::E { A, B, C }; */
13459 if (!processing_specialization
13460 && CLASS_TYPE_P (nested_name_specifier)
13461 && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13462 error_at (type_start_token->location, "cannot add an enumerator "
13463 "list to a template instantiation");
13465 /* If that scope does not contain the scope in which the
13466 class was originally declared, the program is invalid. */
13467 if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13469 if (at_namespace_scope_p ())
13470 error_at (type_start_token->location,
13471 "declaration of %qD in namespace %qD which does not "
13473 type, prev_scope, nested_name_specifier);
13475 error_at (type_start_token->location,
13476 "declaration of %qD in %qD which does not enclose %qD",
13477 type, prev_scope, nested_name_specifier);
13478 type = error_mark_node;
13483 begin_scope (sk_scoped_enum, type);
13485 /* Consume the opening brace. */
13486 cp_lexer_consume_token (parser->lexer);
13488 if (type == error_mark_node)
13489 ; /* Nothing to add */
13490 else if (OPAQUE_ENUM_P (type)
13491 || (cxx_dialect > cxx98 && processing_specialization))
13493 new_value_list = true;
13494 SET_OPAQUE_ENUM_P (type, false);
13495 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13499 error_at (type_start_token->location, "multiple definition of %q#T", type);
13500 error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13501 "previous definition here");
13502 type = error_mark_node;
13505 if (type == error_mark_node)
13506 cp_parser_skip_to_end_of_block_or_statement (parser);
13507 /* If the next token is not '}', then there are some enumerators. */
13508 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13509 cp_parser_enumerator_list (parser, type);
13511 /* Consume the final '}'. */
13512 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13516 timevar_pop (TV_PARSE_ENUM);
13520 /* If a ';' follows, then it is an opaque-enum-specifier
13521 and additional restrictions apply. */
13522 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13525 error_at (type_start_token->location,
13526 "opaque-enum-specifier without name");
13527 else if (nested_name_specifier)
13528 error_at (type_start_token->location,
13529 "opaque-enum-specifier must use a simple identifier");
13533 /* Look for trailing attributes to apply to this enumeration, and
13534 apply them if appropriate. */
13535 if (cp_parser_allow_gnu_extensions_p (parser))
13537 tree trailing_attr = cp_parser_attributes_opt (parser);
13538 trailing_attr = chainon (trailing_attr, attributes);
13539 cplus_decl_attributes (&type,
13541 (int) ATTR_FLAG_TYPE_IN_PLACE);
13544 /* Finish up the enumeration. */
13545 if (type != error_mark_node)
13547 if (new_value_list)
13548 finish_enum_value_list (type);
13550 finish_enum (type);
13553 if (nested_name_specifier)
13555 if (CLASS_TYPE_P (nested_name_specifier))
13557 TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13558 pop_scope (nested_name_specifier);
13560 else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13562 pop_nested_namespace (nested_name_specifier);
13566 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13570 /* Parse an enumerator-list. The enumerators all have the indicated
13574 enumerator-definition
13575 enumerator-list , enumerator-definition */
13578 cp_parser_enumerator_list (cp_parser* parser, tree type)
13582 /* Parse an enumerator-definition. */
13583 cp_parser_enumerator_definition (parser, type);
13585 /* If the next token is not a ',', we've reached the end of
13587 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13589 /* Otherwise, consume the `,' and keep going. */
13590 cp_lexer_consume_token (parser->lexer);
13591 /* If the next token is a `}', there is a trailing comma. */
13592 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13594 if (!in_system_header)
13595 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13601 /* Parse an enumerator-definition. The enumerator has the indicated
13604 enumerator-definition:
13606 enumerator = constant-expression
13612 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13618 /* Save the input location because we are interested in the location
13619 of the identifier and not the location of the explicit value. */
13620 loc = cp_lexer_peek_token (parser->lexer)->location;
13622 /* Look for the identifier. */
13623 identifier = cp_parser_identifier (parser);
13624 if (identifier == error_mark_node)
13627 /* If the next token is an '=', then there is an explicit value. */
13628 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13630 /* Consume the `=' token. */
13631 cp_lexer_consume_token (parser->lexer);
13632 /* Parse the value. */
13633 value = cp_parser_constant_expression (parser,
13634 /*allow_non_constant_p=*/false,
13640 /* If we are processing a template, make sure the initializer of the
13641 enumerator doesn't contain any bare template parameter pack. */
13642 if (check_for_bare_parameter_packs (value))
13643 value = error_mark_node;
13645 /* integral_constant_value will pull out this expression, so make sure
13646 it's folded as appropriate. */
13647 value = fold_non_dependent_expr (value);
13649 /* Create the enumerator. */
13650 build_enumerator (identifier, value, type, loc);
13653 /* Parse a namespace-name.
13656 original-namespace-name
13659 Returns the NAMESPACE_DECL for the namespace. */
13662 cp_parser_namespace_name (cp_parser* parser)
13665 tree namespace_decl;
13667 cp_token *token = cp_lexer_peek_token (parser->lexer);
13669 /* Get the name of the namespace. */
13670 identifier = cp_parser_identifier (parser);
13671 if (identifier == error_mark_node)
13672 return error_mark_node;
13674 /* Look up the identifier in the currently active scope. Look only
13675 for namespaces, due to:
13677 [basic.lookup.udir]
13679 When looking up a namespace-name in a using-directive or alias
13680 definition, only namespace names are considered.
13684 [basic.lookup.qual]
13686 During the lookup of a name preceding the :: scope resolution
13687 operator, object, function, and enumerator names are ignored.
13689 (Note that cp_parser_qualifying_entity only calls this
13690 function if the token after the name is the scope resolution
13692 namespace_decl = cp_parser_lookup_name (parser, identifier,
13694 /*is_template=*/false,
13695 /*is_namespace=*/true,
13696 /*check_dependency=*/true,
13697 /*ambiguous_decls=*/NULL,
13699 /* If it's not a namespace, issue an error. */
13700 if (namespace_decl == error_mark_node
13701 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13703 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13704 error_at (token->location, "%qD is not a namespace-name", identifier);
13705 cp_parser_error (parser, "expected namespace-name");
13706 namespace_decl = error_mark_node;
13709 return namespace_decl;
13712 /* Parse a namespace-definition.
13714 namespace-definition:
13715 named-namespace-definition
13716 unnamed-namespace-definition
13718 named-namespace-definition:
13719 original-namespace-definition
13720 extension-namespace-definition
13722 original-namespace-definition:
13723 namespace identifier { namespace-body }
13725 extension-namespace-definition:
13726 namespace original-namespace-name { namespace-body }
13728 unnamed-namespace-definition:
13729 namespace { namespace-body } */
13732 cp_parser_namespace_definition (cp_parser* parser)
13734 tree identifier, attribs;
13735 bool has_visibility;
13738 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13740 maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13742 cp_lexer_consume_token (parser->lexer);
13747 /* Look for the `namespace' keyword. */
13748 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13750 /* Get the name of the namespace. We do not attempt to distinguish
13751 between an original-namespace-definition and an
13752 extension-namespace-definition at this point. The semantic
13753 analysis routines are responsible for that. */
13754 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13755 identifier = cp_parser_identifier (parser);
13757 identifier = NULL_TREE;
13759 /* Parse any specified attributes. */
13760 attribs = cp_parser_attributes_opt (parser);
13762 /* Look for the `{' to start the namespace. */
13763 cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13764 /* Start the namespace. */
13765 push_namespace (identifier);
13767 /* "inline namespace" is equivalent to a stub namespace definition
13768 followed by a strong using directive. */
13771 tree name_space = current_namespace;
13772 /* Set up namespace association. */
13773 DECL_NAMESPACE_ASSOCIATIONS (name_space)
13774 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13775 DECL_NAMESPACE_ASSOCIATIONS (name_space));
13776 /* Import the contents of the inline namespace. */
13778 do_using_directive (name_space);
13779 push_namespace (identifier);
13782 has_visibility = handle_namespace_attrs (current_namespace, attribs);
13784 /* Parse the body of the namespace. */
13785 cp_parser_namespace_body (parser);
13787 if (has_visibility)
13788 pop_visibility (1);
13790 /* Finish the namespace. */
13792 /* Look for the final `}'. */
13793 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13796 /* Parse a namespace-body.
13799 declaration-seq [opt] */
13802 cp_parser_namespace_body (cp_parser* parser)
13804 cp_parser_declaration_seq_opt (parser);
13807 /* Parse a namespace-alias-definition.
13809 namespace-alias-definition:
13810 namespace identifier = qualified-namespace-specifier ; */
13813 cp_parser_namespace_alias_definition (cp_parser* parser)
13816 tree namespace_specifier;
13818 cp_token *token = cp_lexer_peek_token (parser->lexer);
13820 /* Look for the `namespace' keyword. */
13821 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13822 /* Look for the identifier. */
13823 identifier = cp_parser_identifier (parser);
13824 if (identifier == error_mark_node)
13826 /* Look for the `=' token. */
13827 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13828 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13830 error_at (token->location, "%<namespace%> definition is not allowed here");
13831 /* Skip the definition. */
13832 cp_lexer_consume_token (parser->lexer);
13833 if (cp_parser_skip_to_closing_brace (parser))
13834 cp_lexer_consume_token (parser->lexer);
13837 cp_parser_require (parser, CPP_EQ, RT_EQ);
13838 /* Look for the qualified-namespace-specifier. */
13839 namespace_specifier
13840 = cp_parser_qualified_namespace_specifier (parser);
13841 /* Look for the `;' token. */
13842 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13844 /* Register the alias in the symbol table. */
13845 do_namespace_alias (identifier, namespace_specifier);
13848 /* Parse a qualified-namespace-specifier.
13850 qualified-namespace-specifier:
13851 :: [opt] nested-name-specifier [opt] namespace-name
13853 Returns a NAMESPACE_DECL corresponding to the specified
13857 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13859 /* Look for the optional `::'. */
13860 cp_parser_global_scope_opt (parser,
13861 /*current_scope_valid_p=*/false);
13863 /* Look for the optional nested-name-specifier. */
13864 cp_parser_nested_name_specifier_opt (parser,
13865 /*typename_keyword_p=*/false,
13866 /*check_dependency_p=*/true,
13868 /*is_declaration=*/true);
13870 return cp_parser_namespace_name (parser);
13873 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13874 access declaration.
13877 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13878 using :: unqualified-id ;
13880 access-declaration:
13886 cp_parser_using_declaration (cp_parser* parser,
13887 bool access_declaration_p)
13890 bool typename_p = false;
13891 bool global_scope_p;
13896 if (access_declaration_p)
13897 cp_parser_parse_tentatively (parser);
13900 /* Look for the `using' keyword. */
13901 cp_parser_require_keyword (parser, RID_USING, RT_USING);
13903 /* Peek at the next token. */
13904 token = cp_lexer_peek_token (parser->lexer);
13905 /* See if it's `typename'. */
13906 if (token->keyword == RID_TYPENAME)
13908 /* Remember that we've seen it. */
13910 /* Consume the `typename' token. */
13911 cp_lexer_consume_token (parser->lexer);
13915 /* Look for the optional global scope qualification. */
13917 = (cp_parser_global_scope_opt (parser,
13918 /*current_scope_valid_p=*/false)
13921 /* If we saw `typename', or didn't see `::', then there must be a
13922 nested-name-specifier present. */
13923 if (typename_p || !global_scope_p)
13924 qscope = cp_parser_nested_name_specifier (parser, typename_p,
13925 /*check_dependency_p=*/true,
13927 /*is_declaration=*/true);
13928 /* Otherwise, we could be in either of the two productions. In that
13929 case, treat the nested-name-specifier as optional. */
13931 qscope = cp_parser_nested_name_specifier_opt (parser,
13932 /*typename_keyword_p=*/false,
13933 /*check_dependency_p=*/true,
13935 /*is_declaration=*/true);
13937 qscope = global_namespace;
13939 if (access_declaration_p && cp_parser_error_occurred (parser))
13940 /* Something has already gone wrong; there's no need to parse
13941 further. Since an error has occurred, the return value of
13942 cp_parser_parse_definitely will be false, as required. */
13943 return cp_parser_parse_definitely (parser);
13945 token = cp_lexer_peek_token (parser->lexer);
13946 /* Parse the unqualified-id. */
13947 identifier = cp_parser_unqualified_id (parser,
13948 /*template_keyword_p=*/false,
13949 /*check_dependency_p=*/true,
13950 /*declarator_p=*/true,
13951 /*optional_p=*/false);
13953 if (access_declaration_p)
13955 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13956 cp_parser_simulate_error (parser);
13957 if (!cp_parser_parse_definitely (parser))
13961 /* The function we call to handle a using-declaration is different
13962 depending on what scope we are in. */
13963 if (qscope == error_mark_node || identifier == error_mark_node)
13965 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13966 && TREE_CODE (identifier) != BIT_NOT_EXPR)
13967 /* [namespace.udecl]
13969 A using declaration shall not name a template-id. */
13970 error_at (token->location,
13971 "a template-id may not appear in a using-declaration");
13974 if (at_class_scope_p ())
13976 /* Create the USING_DECL. */
13977 decl = do_class_using_decl (parser->scope, identifier);
13979 if (check_for_bare_parameter_packs (decl))
13982 /* Add it to the list of members in this class. */
13983 finish_member_declaration (decl);
13987 decl = cp_parser_lookup_name_simple (parser,
13990 if (decl == error_mark_node)
13991 cp_parser_name_lookup_error (parser, identifier,
13994 else if (check_for_bare_parameter_packs (decl))
13996 else if (!at_namespace_scope_p ())
13997 do_local_using_decl (decl, qscope, identifier);
13999 do_toplevel_using_decl (decl, qscope, identifier);
14003 /* Look for the final `;'. */
14004 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14009 /* Parse a using-directive.
14012 using namespace :: [opt] nested-name-specifier [opt]
14013 namespace-name ; */
14016 cp_parser_using_directive (cp_parser* parser)
14018 tree namespace_decl;
14021 /* Look for the `using' keyword. */
14022 cp_parser_require_keyword (parser, RID_USING, RT_USING);
14023 /* And the `namespace' keyword. */
14024 cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14025 /* Look for the optional `::' operator. */
14026 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14027 /* And the optional nested-name-specifier. */
14028 cp_parser_nested_name_specifier_opt (parser,
14029 /*typename_keyword_p=*/false,
14030 /*check_dependency_p=*/true,
14032 /*is_declaration=*/true);
14033 /* Get the namespace being used. */
14034 namespace_decl = cp_parser_namespace_name (parser);
14035 /* And any specified attributes. */
14036 attribs = cp_parser_attributes_opt (parser);
14037 /* Update the symbol table. */
14038 parse_using_directive (namespace_decl, attribs);
14039 /* Look for the final `;'. */
14040 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14043 /* Parse an asm-definition.
14046 asm ( string-literal ) ;
14051 asm volatile [opt] ( string-literal ) ;
14052 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14053 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14054 : asm-operand-list [opt] ) ;
14055 asm volatile [opt] ( string-literal : asm-operand-list [opt]
14056 : asm-operand-list [opt]
14057 : asm-clobber-list [opt] ) ;
14058 asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14059 : asm-clobber-list [opt]
14060 : asm-goto-list ) ; */
14063 cp_parser_asm_definition (cp_parser* parser)
14066 tree outputs = NULL_TREE;
14067 tree inputs = NULL_TREE;
14068 tree clobbers = NULL_TREE;
14069 tree labels = NULL_TREE;
14071 bool volatile_p = false;
14072 bool extended_p = false;
14073 bool invalid_inputs_p = false;
14074 bool invalid_outputs_p = false;
14075 bool goto_p = false;
14076 required_token missing = RT_NONE;
14078 /* Look for the `asm' keyword. */
14079 cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14080 /* See if the next token is `volatile'. */
14081 if (cp_parser_allow_gnu_extensions_p (parser)
14082 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14084 /* Remember that we saw the `volatile' keyword. */
14086 /* Consume the token. */
14087 cp_lexer_consume_token (parser->lexer);
14089 if (cp_parser_allow_gnu_extensions_p (parser)
14090 && parser->in_function_body
14091 && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14093 /* Remember that we saw the `goto' keyword. */
14095 /* Consume the token. */
14096 cp_lexer_consume_token (parser->lexer);
14098 /* Look for the opening `('. */
14099 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14101 /* Look for the string. */
14102 string = cp_parser_string_literal (parser, false, false);
14103 if (string == error_mark_node)
14105 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14106 /*consume_paren=*/true);
14110 /* If we're allowing GNU extensions, check for the extended assembly
14111 syntax. Unfortunately, the `:' tokens need not be separated by
14112 a space in C, and so, for compatibility, we tolerate that here
14113 too. Doing that means that we have to treat the `::' operator as
14115 if (cp_parser_allow_gnu_extensions_p (parser)
14116 && parser->in_function_body
14117 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14118 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14120 bool inputs_p = false;
14121 bool clobbers_p = false;
14122 bool labels_p = false;
14124 /* The extended syntax was used. */
14127 /* Look for outputs. */
14128 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14130 /* Consume the `:'. */
14131 cp_lexer_consume_token (parser->lexer);
14132 /* Parse the output-operands. */
14133 if (cp_lexer_next_token_is_not (parser->lexer,
14135 && cp_lexer_next_token_is_not (parser->lexer,
14137 && cp_lexer_next_token_is_not (parser->lexer,
14140 outputs = cp_parser_asm_operand_list (parser);
14142 if (outputs == error_mark_node)
14143 invalid_outputs_p = true;
14145 /* If the next token is `::', there are no outputs, and the
14146 next token is the beginning of the inputs. */
14147 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14148 /* The inputs are coming next. */
14151 /* Look for inputs. */
14153 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14155 /* Consume the `:' or `::'. */
14156 cp_lexer_consume_token (parser->lexer);
14157 /* Parse the output-operands. */
14158 if (cp_lexer_next_token_is_not (parser->lexer,
14160 && cp_lexer_next_token_is_not (parser->lexer,
14162 && cp_lexer_next_token_is_not (parser->lexer,
14164 inputs = cp_parser_asm_operand_list (parser);
14166 if (inputs == error_mark_node)
14167 invalid_inputs_p = true;
14169 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14170 /* The clobbers are coming next. */
14173 /* Look for clobbers. */
14175 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14178 /* Consume the `:' or `::'. */
14179 cp_lexer_consume_token (parser->lexer);
14180 /* Parse the clobbers. */
14181 if (cp_lexer_next_token_is_not (parser->lexer,
14183 && cp_lexer_next_token_is_not (parser->lexer,
14185 clobbers = cp_parser_asm_clobber_list (parser);
14188 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14189 /* The labels are coming next. */
14192 /* Look for labels. */
14194 || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14197 /* Consume the `:' or `::'. */
14198 cp_lexer_consume_token (parser->lexer);
14199 /* Parse the labels. */
14200 labels = cp_parser_asm_label_list (parser);
14203 if (goto_p && !labels_p)
14204 missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14207 missing = RT_COLON_SCOPE;
14209 /* Look for the closing `)'. */
14210 if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14211 missing ? missing : RT_CLOSE_PAREN))
14212 cp_parser_skip_to_closing_parenthesis (parser, true, false,
14213 /*consume_paren=*/true);
14214 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14216 if (!invalid_inputs_p && !invalid_outputs_p)
14218 /* Create the ASM_EXPR. */
14219 if (parser->in_function_body)
14221 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14222 inputs, clobbers, labels);
14223 /* If the extended syntax was not used, mark the ASM_EXPR. */
14226 tree temp = asm_stmt;
14227 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14228 temp = TREE_OPERAND (temp, 0);
14230 ASM_INPUT_P (temp) = 1;
14234 cgraph_add_asm_node (string);
14238 /* Declarators [gram.dcl.decl] */
14240 /* Parse an init-declarator.
14243 declarator initializer [opt]
14248 declarator asm-specification [opt] attributes [opt] initializer [opt]
14250 function-definition:
14251 decl-specifier-seq [opt] declarator ctor-initializer [opt]
14253 decl-specifier-seq [opt] declarator function-try-block
14257 function-definition:
14258 __extension__ function-definition
14260 The DECL_SPECIFIERS apply to this declarator. Returns a
14261 representation of the entity declared. If MEMBER_P is TRUE, then
14262 this declarator appears in a class scope. The new DECL created by
14263 this declarator is returned.
14265 The CHECKS are access checks that should be performed once we know
14266 what entity is being declared (and, therefore, what classes have
14269 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14270 for a function-definition here as well. If the declarator is a
14271 declarator for a function-definition, *FUNCTION_DEFINITION_P will
14272 be TRUE upon return. By that point, the function-definition will
14273 have been completely parsed.
14275 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14278 If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14279 parsed declaration if it is an uninitialized single declarator not followed
14280 by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14281 if present, will not be consumed. If returned, this declarator will be
14282 created with SD_INITIALIZED but will not call cp_finish_decl. */
14285 cp_parser_init_declarator (cp_parser* parser,
14286 cp_decl_specifier_seq *decl_specifiers,
14287 VEC (deferred_access_check,gc)* checks,
14288 bool function_definition_allowed_p,
14290 int declares_class_or_enum,
14291 bool* function_definition_p,
14292 tree* maybe_range_for_decl)
14294 cp_token *token = NULL, *asm_spec_start_token = NULL,
14295 *attributes_start_token = NULL;
14296 cp_declarator *declarator;
14297 tree prefix_attributes;
14299 tree asm_specification;
14301 tree decl = NULL_TREE;
14303 int is_initialized;
14304 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
14305 initialized with "= ..", CPP_OPEN_PAREN if initialized with
14307 enum cpp_ttype initialization_kind;
14308 bool is_direct_init = false;
14309 bool is_non_constant_init;
14310 int ctor_dtor_or_conv_p;
14312 tree pushed_scope = NULL_TREE;
14313 bool range_for_decl_p = false;
14315 /* Gather the attributes that were provided with the
14316 decl-specifiers. */
14317 prefix_attributes = decl_specifiers->attributes;
14319 /* Assume that this is not the declarator for a function
14321 if (function_definition_p)
14322 *function_definition_p = false;
14324 /* Defer access checks while parsing the declarator; we cannot know
14325 what names are accessible until we know what is being
14327 resume_deferring_access_checks ();
14329 /* Parse the declarator. */
14330 token = cp_lexer_peek_token (parser->lexer);
14332 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14333 &ctor_dtor_or_conv_p,
14334 /*parenthesized_p=*/NULL,
14335 /*member_p=*/false);
14336 /* Gather up the deferred checks. */
14337 stop_deferring_access_checks ();
14339 /* If the DECLARATOR was erroneous, there's no need to go
14341 if (declarator == cp_error_declarator)
14342 return error_mark_node;
14344 /* Check that the number of template-parameter-lists is OK. */
14345 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14347 return error_mark_node;
14349 if (declares_class_or_enum & 2)
14350 cp_parser_check_for_definition_in_return_type (declarator,
14351 decl_specifiers->type,
14352 decl_specifiers->type_location);
14354 /* Figure out what scope the entity declared by the DECLARATOR is
14355 located in. `grokdeclarator' sometimes changes the scope, so
14356 we compute it now. */
14357 scope = get_scope_of_declarator (declarator);
14359 /* Perform any lookups in the declared type which were thought to be
14360 dependent, but are not in the scope of the declarator. */
14361 decl_specifiers->type
14362 = maybe_update_decl_type (decl_specifiers->type, scope);
14364 /* If we're allowing GNU extensions, look for an asm-specification
14366 if (cp_parser_allow_gnu_extensions_p (parser))
14368 /* Look for an asm-specification. */
14369 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14370 asm_specification = cp_parser_asm_specification_opt (parser);
14371 /* And attributes. */
14372 attributes_start_token = cp_lexer_peek_token (parser->lexer);
14373 attributes = cp_parser_attributes_opt (parser);
14377 asm_specification = NULL_TREE;
14378 attributes = NULL_TREE;
14381 /* Peek at the next token. */
14382 token = cp_lexer_peek_token (parser->lexer);
14383 /* Check to see if the token indicates the start of a
14384 function-definition. */
14385 if (function_declarator_p (declarator)
14386 && cp_parser_token_starts_function_definition_p (token))
14388 if (!function_definition_allowed_p)
14390 /* If a function-definition should not appear here, issue an
14392 cp_parser_error (parser,
14393 "a function-definition is not allowed here");
14394 return error_mark_node;
14398 location_t func_brace_location
14399 = cp_lexer_peek_token (parser->lexer)->location;
14401 /* Neither attributes nor an asm-specification are allowed
14402 on a function-definition. */
14403 if (asm_specification)
14404 error_at (asm_spec_start_token->location,
14405 "an asm-specification is not allowed "
14406 "on a function-definition");
14408 error_at (attributes_start_token->location,
14409 "attributes are not allowed on a function-definition");
14410 /* This is a function-definition. */
14411 *function_definition_p = true;
14413 /* Parse the function definition. */
14415 decl = cp_parser_save_member_function_body (parser,
14418 prefix_attributes);
14421 = (cp_parser_function_definition_from_specifiers_and_declarator
14422 (parser, decl_specifiers, prefix_attributes, declarator));
14424 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14426 /* This is where the prologue starts... */
14427 DECL_STRUCT_FUNCTION (decl)->function_start_locus
14428 = func_brace_location;
14437 Only in function declarations for constructors, destructors, and
14438 type conversions can the decl-specifier-seq be omitted.
14440 We explicitly postpone this check past the point where we handle
14441 function-definitions because we tolerate function-definitions
14442 that are missing their return types in some modes. */
14443 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14445 cp_parser_error (parser,
14446 "expected constructor, destructor, or type conversion");
14447 return error_mark_node;
14450 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
14451 if (token->type == CPP_EQ
14452 || token->type == CPP_OPEN_PAREN
14453 || token->type == CPP_OPEN_BRACE)
14455 is_initialized = SD_INITIALIZED;
14456 initialization_kind = token->type;
14457 if (maybe_range_for_decl)
14458 *maybe_range_for_decl = error_mark_node;
14460 if (token->type == CPP_EQ
14461 && function_declarator_p (declarator))
14463 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14464 if (t2->keyword == RID_DEFAULT)
14465 is_initialized = SD_DEFAULTED;
14466 else if (t2->keyword == RID_DELETE)
14467 is_initialized = SD_DELETED;
14472 /* If the init-declarator isn't initialized and isn't followed by a
14473 `,' or `;', it's not a valid init-declarator. */
14474 if (token->type != CPP_COMMA
14475 && token->type != CPP_SEMICOLON)
14477 if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14478 range_for_decl_p = true;
14481 cp_parser_error (parser, "expected initializer");
14482 return error_mark_node;
14485 is_initialized = SD_UNINITIALIZED;
14486 initialization_kind = CPP_EOF;
14489 /* Because start_decl has side-effects, we should only call it if we
14490 know we're going ahead. By this point, we know that we cannot
14491 possibly be looking at any other construct. */
14492 cp_parser_commit_to_tentative_parse (parser);
14494 /* If the decl specifiers were bad, issue an error now that we're
14495 sure this was intended to be a declarator. Then continue
14496 declaring the variable(s), as int, to try to cut down on further
14498 if (decl_specifiers->any_specifiers_p
14499 && decl_specifiers->type == error_mark_node)
14501 cp_parser_error (parser, "invalid type in declaration");
14502 decl_specifiers->type = integer_type_node;
14505 /* Check to see whether or not this declaration is a friend. */
14506 friend_p = cp_parser_friend_p (decl_specifiers);
14508 /* Enter the newly declared entry in the symbol table. If we're
14509 processing a declaration in a class-specifier, we wait until
14510 after processing the initializer. */
14513 if (parser->in_unbraced_linkage_specification_p)
14514 decl_specifiers->storage_class = sc_extern;
14515 decl = start_decl (declarator, decl_specifiers,
14516 range_for_decl_p? SD_INITIALIZED : is_initialized,
14517 attributes, prefix_attributes,
14519 /* Adjust location of decl if declarator->id_loc is more appropriate:
14520 set, and decl wasn't merged with another decl, in which case its
14521 location would be different from input_location, and more accurate. */
14523 && declarator->id_loc != UNKNOWN_LOCATION
14524 && DECL_SOURCE_LOCATION (decl) == input_location)
14525 DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14528 /* Enter the SCOPE. That way unqualified names appearing in the
14529 initializer will be looked up in SCOPE. */
14530 pushed_scope = push_scope (scope);
14532 /* Perform deferred access control checks, now that we know in which
14533 SCOPE the declared entity resides. */
14534 if (!member_p && decl)
14536 tree saved_current_function_decl = NULL_TREE;
14538 /* If the entity being declared is a function, pretend that we
14539 are in its scope. If it is a `friend', it may have access to
14540 things that would not otherwise be accessible. */
14541 if (TREE_CODE (decl) == FUNCTION_DECL)
14543 saved_current_function_decl = current_function_decl;
14544 current_function_decl = decl;
14547 /* Perform access checks for template parameters. */
14548 cp_parser_perform_template_parameter_access_checks (checks);
14550 /* Perform the access control checks for the declarator and the
14551 decl-specifiers. */
14552 perform_deferred_access_checks ();
14554 /* Restore the saved value. */
14555 if (TREE_CODE (decl) == FUNCTION_DECL)
14556 current_function_decl = saved_current_function_decl;
14559 /* Parse the initializer. */
14560 initializer = NULL_TREE;
14561 is_direct_init = false;
14562 is_non_constant_init = true;
14563 if (is_initialized)
14565 if (function_declarator_p (declarator))
14567 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14568 if (initialization_kind == CPP_EQ)
14569 initializer = cp_parser_pure_specifier (parser);
14572 /* If the declaration was erroneous, we don't really
14573 know what the user intended, so just silently
14574 consume the initializer. */
14575 if (decl != error_mark_node)
14576 error_at (initializer_start_token->location,
14577 "initializer provided for function");
14578 cp_parser_skip_to_closing_parenthesis (parser,
14579 /*recovering=*/true,
14580 /*or_comma=*/false,
14581 /*consume_paren=*/true);
14586 /* We want to record the extra mangling scope for in-class
14587 initializers of class members and initializers of static data
14588 member templates. The former is a C++0x feature which isn't
14589 implemented yet, and I expect it will involve deferring
14590 parsing of the initializer until end of class as with default
14591 arguments. So right here we only handle the latter. */
14592 if (!member_p && processing_template_decl)
14593 start_lambda_scope (decl);
14594 initializer = cp_parser_initializer (parser,
14596 &is_non_constant_init);
14597 if (!member_p && processing_template_decl)
14598 finish_lambda_scope ();
14602 /* The old parser allows attributes to appear after a parenthesized
14603 initializer. Mark Mitchell proposed removing this functionality
14604 on the GCC mailing lists on 2002-08-13. This parser accepts the
14605 attributes -- but ignores them. */
14606 if (cp_parser_allow_gnu_extensions_p (parser)
14607 && initialization_kind == CPP_OPEN_PAREN)
14608 if (cp_parser_attributes_opt (parser))
14609 warning (OPT_Wattributes,
14610 "attributes after parenthesized initializer ignored");
14612 /* For an in-class declaration, use `grokfield' to create the
14618 pop_scope (pushed_scope);
14619 pushed_scope = NULL_TREE;
14621 decl = grokfield (declarator, decl_specifiers,
14622 initializer, !is_non_constant_init,
14623 /*asmspec=*/NULL_TREE,
14624 prefix_attributes);
14625 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14626 cp_parser_save_default_args (parser, decl);
14629 /* Finish processing the declaration. But, skip member
14631 if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14633 cp_finish_decl (decl,
14634 initializer, !is_non_constant_init,
14636 /* If the initializer is in parentheses, then this is
14637 a direct-initialization, which means that an
14638 `explicit' constructor is OK. Otherwise, an
14639 `explicit' constructor cannot be used. */
14640 ((is_direct_init || !is_initialized)
14641 ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14643 else if ((cxx_dialect != cxx98) && friend_p
14644 && decl && TREE_CODE (decl) == FUNCTION_DECL)
14645 /* Core issue #226 (C++0x only): A default template-argument
14646 shall not be specified in a friend class template
14648 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
14649 /*is_partial=*/0, /*is_friend_decl=*/1);
14651 if (!friend_p && pushed_scope)
14652 pop_scope (pushed_scope);
14657 /* Parse a declarator.
14661 ptr-operator declarator
14663 abstract-declarator:
14664 ptr-operator abstract-declarator [opt]
14665 direct-abstract-declarator
14670 attributes [opt] direct-declarator
14671 attributes [opt] ptr-operator declarator
14673 abstract-declarator:
14674 attributes [opt] ptr-operator abstract-declarator [opt]
14675 attributes [opt] direct-abstract-declarator
14677 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14678 detect constructor, destructor or conversion operators. It is set
14679 to -1 if the declarator is a name, and +1 if it is a
14680 function. Otherwise it is set to zero. Usually you just want to
14681 test for >0, but internally the negative value is used.
14683 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14684 a decl-specifier-seq unless it declares a constructor, destructor,
14685 or conversion. It might seem that we could check this condition in
14686 semantic analysis, rather than parsing, but that makes it difficult
14687 to handle something like `f()'. We want to notice that there are
14688 no decl-specifiers, and therefore realize that this is an
14689 expression, not a declaration.)
14691 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14692 the declarator is a direct-declarator of the form "(...)".
14694 MEMBER_P is true iff this declarator is a member-declarator. */
14696 static cp_declarator *
14697 cp_parser_declarator (cp_parser* parser,
14698 cp_parser_declarator_kind dcl_kind,
14699 int* ctor_dtor_or_conv_p,
14700 bool* parenthesized_p,
14703 cp_declarator *declarator;
14704 enum tree_code code;
14705 cp_cv_quals cv_quals;
14707 tree attributes = NULL_TREE;
14709 /* Assume this is not a constructor, destructor, or type-conversion
14711 if (ctor_dtor_or_conv_p)
14712 *ctor_dtor_or_conv_p = 0;
14714 if (cp_parser_allow_gnu_extensions_p (parser))
14715 attributes = cp_parser_attributes_opt (parser);
14717 /* Check for the ptr-operator production. */
14718 cp_parser_parse_tentatively (parser);
14719 /* Parse the ptr-operator. */
14720 code = cp_parser_ptr_operator (parser,
14723 /* If that worked, then we have a ptr-operator. */
14724 if (cp_parser_parse_definitely (parser))
14726 /* If a ptr-operator was found, then this declarator was not
14728 if (parenthesized_p)
14729 *parenthesized_p = true;
14730 /* The dependent declarator is optional if we are parsing an
14731 abstract-declarator. */
14732 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14733 cp_parser_parse_tentatively (parser);
14735 /* Parse the dependent declarator. */
14736 declarator = cp_parser_declarator (parser, dcl_kind,
14737 /*ctor_dtor_or_conv_p=*/NULL,
14738 /*parenthesized_p=*/NULL,
14739 /*member_p=*/false);
14741 /* If we are parsing an abstract-declarator, we must handle the
14742 case where the dependent declarator is absent. */
14743 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14744 && !cp_parser_parse_definitely (parser))
14747 declarator = cp_parser_make_indirect_declarator
14748 (code, class_type, cv_quals, declarator);
14750 /* Everything else is a direct-declarator. */
14753 if (parenthesized_p)
14754 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14756 declarator = cp_parser_direct_declarator (parser, dcl_kind,
14757 ctor_dtor_or_conv_p,
14761 if (attributes && declarator && declarator != cp_error_declarator)
14762 declarator->attributes = attributes;
14767 /* Parse a direct-declarator or direct-abstract-declarator.
14771 direct-declarator ( parameter-declaration-clause )
14772 cv-qualifier-seq [opt]
14773 exception-specification [opt]
14774 direct-declarator [ constant-expression [opt] ]
14777 direct-abstract-declarator:
14778 direct-abstract-declarator [opt]
14779 ( parameter-declaration-clause )
14780 cv-qualifier-seq [opt]
14781 exception-specification [opt]
14782 direct-abstract-declarator [opt] [ constant-expression [opt] ]
14783 ( abstract-declarator )
14785 Returns a representation of the declarator. DCL_KIND is
14786 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14787 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
14788 we are parsing a direct-declarator. It is
14789 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14790 of ambiguity we prefer an abstract declarator, as per
14791 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14792 cp_parser_declarator. */
14794 static cp_declarator *
14795 cp_parser_direct_declarator (cp_parser* parser,
14796 cp_parser_declarator_kind dcl_kind,
14797 int* ctor_dtor_or_conv_p,
14801 cp_declarator *declarator = NULL;
14802 tree scope = NULL_TREE;
14803 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14804 bool saved_in_declarator_p = parser->in_declarator_p;
14806 tree pushed_scope = NULL_TREE;
14810 /* Peek at the next token. */
14811 token = cp_lexer_peek_token (parser->lexer);
14812 if (token->type == CPP_OPEN_PAREN)
14814 /* This is either a parameter-declaration-clause, or a
14815 parenthesized declarator. When we know we are parsing a
14816 named declarator, it must be a parenthesized declarator
14817 if FIRST is true. For instance, `(int)' is a
14818 parameter-declaration-clause, with an omitted
14819 direct-abstract-declarator. But `((*))', is a
14820 parenthesized abstract declarator. Finally, when T is a
14821 template parameter `(T)' is a
14822 parameter-declaration-clause, and not a parenthesized
14825 We first try and parse a parameter-declaration-clause,
14826 and then try a nested declarator (if FIRST is true).
14828 It is not an error for it not to be a
14829 parameter-declaration-clause, even when FIRST is
14835 The first is the declaration of a function while the
14836 second is the definition of a variable, including its
14839 Having seen only the parenthesis, we cannot know which of
14840 these two alternatives should be selected. Even more
14841 complex are examples like:
14846 The former is a function-declaration; the latter is a
14847 variable initialization.
14849 Thus again, we try a parameter-declaration-clause, and if
14850 that fails, we back out and return. */
14852 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14855 unsigned saved_num_template_parameter_lists;
14856 bool is_declarator = false;
14859 /* In a member-declarator, the only valid interpretation
14860 of a parenthesis is the start of a
14861 parameter-declaration-clause. (It is invalid to
14862 initialize a static data member with a parenthesized
14863 initializer; only the "=" form of initialization is
14866 cp_parser_parse_tentatively (parser);
14868 /* Consume the `('. */
14869 cp_lexer_consume_token (parser->lexer);
14872 /* If this is going to be an abstract declarator, we're
14873 in a declarator and we can't have default args. */
14874 parser->default_arg_ok_p = false;
14875 parser->in_declarator_p = true;
14878 /* Inside the function parameter list, surrounding
14879 template-parameter-lists do not apply. */
14880 saved_num_template_parameter_lists
14881 = parser->num_template_parameter_lists;
14882 parser->num_template_parameter_lists = 0;
14884 begin_scope (sk_function_parms, NULL_TREE);
14886 /* Parse the parameter-declaration-clause. */
14887 params = cp_parser_parameter_declaration_clause (parser);
14889 parser->num_template_parameter_lists
14890 = saved_num_template_parameter_lists;
14892 /* If all went well, parse the cv-qualifier-seq and the
14893 exception-specification. */
14894 if (member_p || cp_parser_parse_definitely (parser))
14896 cp_cv_quals cv_quals;
14897 tree exception_specification;
14900 is_declarator = true;
14902 if (ctor_dtor_or_conv_p)
14903 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14905 /* Consume the `)'. */
14906 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14908 /* Parse the cv-qualifier-seq. */
14909 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14910 /* And the exception-specification. */
14911 exception_specification
14912 = cp_parser_exception_specification_opt (parser);
14915 = cp_parser_late_return_type_opt (parser);
14917 /* Create the function-declarator. */
14918 declarator = make_call_declarator (declarator,
14921 exception_specification,
14923 /* Any subsequent parameter lists are to do with
14924 return type, so are not those of the declared
14926 parser->default_arg_ok_p = false;
14929 /* Remove the function parms from scope. */
14930 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14931 pop_binding (DECL_NAME (t), t);
14935 /* Repeat the main loop. */
14939 /* If this is the first, we can try a parenthesized
14943 bool saved_in_type_id_in_expr_p;
14945 parser->default_arg_ok_p = saved_default_arg_ok_p;
14946 parser->in_declarator_p = saved_in_declarator_p;
14948 /* Consume the `('. */
14949 cp_lexer_consume_token (parser->lexer);
14950 /* Parse the nested declarator. */
14951 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14952 parser->in_type_id_in_expr_p = true;
14954 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14955 /*parenthesized_p=*/NULL,
14957 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14959 /* Expect a `)'. */
14960 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14961 declarator = cp_error_declarator;
14962 if (declarator == cp_error_declarator)
14965 goto handle_declarator;
14967 /* Otherwise, we must be done. */
14971 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14972 && token->type == CPP_OPEN_SQUARE)
14974 /* Parse an array-declarator. */
14977 if (ctor_dtor_or_conv_p)
14978 *ctor_dtor_or_conv_p = 0;
14981 parser->default_arg_ok_p = false;
14982 parser->in_declarator_p = true;
14983 /* Consume the `['. */
14984 cp_lexer_consume_token (parser->lexer);
14985 /* Peek at the next token. */
14986 token = cp_lexer_peek_token (parser->lexer);
14987 /* If the next token is `]', then there is no
14988 constant-expression. */
14989 if (token->type != CPP_CLOSE_SQUARE)
14991 bool non_constant_p;
14994 = cp_parser_constant_expression (parser,
14995 /*allow_non_constant=*/true,
14997 if (!non_constant_p)
14999 /* Normally, the array bound must be an integral constant
15000 expression. However, as an extension, we allow VLAs
15001 in function scopes as long as they aren't part of a
15002 parameter declaration. */
15003 else if (!parser->in_function_body
15004 || current_binding_level->kind == sk_function_parms)
15006 cp_parser_error (parser,
15007 "array bound is not an integer constant");
15008 bounds = error_mark_node;
15010 else if (processing_template_decl && !error_operand_p (bounds))
15012 /* Remember this wasn't a constant-expression. */
15013 bounds = build_nop (TREE_TYPE (bounds), bounds);
15014 TREE_SIDE_EFFECTS (bounds) = 1;
15018 bounds = NULL_TREE;
15019 /* Look for the closing `]'. */
15020 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15022 declarator = cp_error_declarator;
15026 declarator = make_array_declarator (declarator, bounds);
15028 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15031 tree qualifying_scope;
15032 tree unqualified_name;
15033 special_function_kind sfk;
15035 bool pack_expansion_p = false;
15036 cp_token *declarator_id_start_token;
15038 /* Parse a declarator-id */
15039 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15042 cp_parser_parse_tentatively (parser);
15044 /* If we see an ellipsis, we should be looking at a
15046 if (token->type == CPP_ELLIPSIS)
15048 /* Consume the `...' */
15049 cp_lexer_consume_token (parser->lexer);
15051 pack_expansion_p = true;
15055 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15057 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15058 qualifying_scope = parser->scope;
15063 if (!unqualified_name && pack_expansion_p)
15065 /* Check whether an error occurred. */
15066 okay = !cp_parser_error_occurred (parser);
15068 /* We already consumed the ellipsis to mark a
15069 parameter pack, but we have no way to report it,
15070 so abort the tentative parse. We will be exiting
15071 immediately anyway. */
15072 cp_parser_abort_tentative_parse (parser);
15075 okay = cp_parser_parse_definitely (parser);
15078 unqualified_name = error_mark_node;
15079 else if (unqualified_name
15080 && (qualifying_scope
15081 || (TREE_CODE (unqualified_name)
15082 != IDENTIFIER_NODE)))
15084 cp_parser_error (parser, "expected unqualified-id");
15085 unqualified_name = error_mark_node;
15089 if (!unqualified_name)
15091 if (unqualified_name == error_mark_node)
15093 declarator = cp_error_declarator;
15094 pack_expansion_p = false;
15095 declarator->parameter_pack_p = false;
15099 if (qualifying_scope && at_namespace_scope_p ()
15100 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15102 /* In the declaration of a member of a template class
15103 outside of the class itself, the SCOPE will sometimes
15104 be a TYPENAME_TYPE. For example, given:
15106 template <typename T>
15107 int S<T>::R::i = 3;
15109 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
15110 this context, we must resolve S<T>::R to an ordinary
15111 type, rather than a typename type.
15113 The reason we normally avoid resolving TYPENAME_TYPEs
15114 is that a specialization of `S' might render
15115 `S<T>::R' not a type. However, if `S' is
15116 specialized, then this `i' will not be used, so there
15117 is no harm in resolving the types here. */
15120 /* Resolve the TYPENAME_TYPE. */
15121 type = resolve_typename_type (qualifying_scope,
15122 /*only_current_p=*/false);
15123 /* If that failed, the declarator is invalid. */
15124 if (TREE_CODE (type) == TYPENAME_TYPE)
15126 if (typedef_variant_p (type))
15127 error_at (declarator_id_start_token->location,
15128 "cannot define member of dependent typedef "
15131 error_at (declarator_id_start_token->location,
15132 "%<%T::%E%> is not a type",
15133 TYPE_CONTEXT (qualifying_scope),
15134 TYPE_IDENTIFIER (qualifying_scope));
15136 qualifying_scope = type;
15141 if (unqualified_name)
15145 if (qualifying_scope
15146 && CLASS_TYPE_P (qualifying_scope))
15147 class_type = qualifying_scope;
15149 class_type = current_class_type;
15151 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15153 tree name_type = TREE_TYPE (unqualified_name);
15154 if (class_type && same_type_p (name_type, class_type))
15156 if (qualifying_scope
15157 && CLASSTYPE_USE_TEMPLATE (name_type))
15159 error_at (declarator_id_start_token->location,
15160 "invalid use of constructor as a template");
15161 inform (declarator_id_start_token->location,
15162 "use %<%T::%D%> instead of %<%T::%D%> to "
15163 "name the constructor in a qualified name",
15165 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15166 class_type, name_type);
15167 declarator = cp_error_declarator;
15171 unqualified_name = constructor_name (class_type);
15175 /* We do not attempt to print the declarator
15176 here because we do not have enough
15177 information about its original syntactic
15179 cp_parser_error (parser, "invalid declarator");
15180 declarator = cp_error_declarator;
15187 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15188 sfk = sfk_destructor;
15189 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15190 sfk = sfk_conversion;
15191 else if (/* There's no way to declare a constructor
15192 for an anonymous type, even if the type
15193 got a name for linkage purposes. */
15194 !TYPE_WAS_ANONYMOUS (class_type)
15195 && constructor_name_p (unqualified_name,
15198 unqualified_name = constructor_name (class_type);
15199 sfk = sfk_constructor;
15201 else if (is_overloaded_fn (unqualified_name)
15202 && DECL_CONSTRUCTOR_P (get_first_fn
15203 (unqualified_name)))
15204 sfk = sfk_constructor;
15206 if (ctor_dtor_or_conv_p && sfk != sfk_none)
15207 *ctor_dtor_or_conv_p = -1;
15210 declarator = make_id_declarator (qualifying_scope,
15213 declarator->id_loc = token->location;
15214 declarator->parameter_pack_p = pack_expansion_p;
15216 if (pack_expansion_p)
15217 maybe_warn_variadic_templates ();
15220 handle_declarator:;
15221 scope = get_scope_of_declarator (declarator);
15223 /* Any names that appear after the declarator-id for a
15224 member are looked up in the containing scope. */
15225 pushed_scope = push_scope (scope);
15226 parser->in_declarator_p = true;
15227 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15228 || (declarator && declarator->kind == cdk_id))
15229 /* Default args are only allowed on function
15231 parser->default_arg_ok_p = saved_default_arg_ok_p;
15233 parser->default_arg_ok_p = false;
15242 /* For an abstract declarator, we might wind up with nothing at this
15243 point. That's an error; the declarator is not optional. */
15245 cp_parser_error (parser, "expected declarator");
15247 /* If we entered a scope, we must exit it now. */
15249 pop_scope (pushed_scope);
15251 parser->default_arg_ok_p = saved_default_arg_ok_p;
15252 parser->in_declarator_p = saved_in_declarator_p;
15257 /* Parse a ptr-operator.
15260 * cv-qualifier-seq [opt]
15262 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15267 & cv-qualifier-seq [opt]
15269 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15270 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15271 an rvalue reference. In the case of a pointer-to-member, *TYPE is
15272 filled in with the TYPE containing the member. *CV_QUALS is
15273 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15274 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
15275 Note that the tree codes returned by this function have nothing
15276 to do with the types of trees that will be eventually be created
15277 to represent the pointer or reference type being parsed. They are
15278 just constants with suggestive names. */
15279 static enum tree_code
15280 cp_parser_ptr_operator (cp_parser* parser,
15282 cp_cv_quals *cv_quals)
15284 enum tree_code code = ERROR_MARK;
15287 /* Assume that it's not a pointer-to-member. */
15289 /* And that there are no cv-qualifiers. */
15290 *cv_quals = TYPE_UNQUALIFIED;
15292 /* Peek at the next token. */
15293 token = cp_lexer_peek_token (parser->lexer);
15295 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
15296 if (token->type == CPP_MULT)
15297 code = INDIRECT_REF;
15298 else if (token->type == CPP_AND)
15300 else if ((cxx_dialect != cxx98) &&
15301 token->type == CPP_AND_AND) /* C++0x only */
15302 code = NON_LVALUE_EXPR;
15304 if (code != ERROR_MARK)
15306 /* Consume the `*', `&' or `&&'. */
15307 cp_lexer_consume_token (parser->lexer);
15309 /* A `*' can be followed by a cv-qualifier-seq, and so can a
15310 `&', if we are allowing GNU extensions. (The only qualifier
15311 that can legally appear after `&' is `restrict', but that is
15312 enforced during semantic analysis. */
15313 if (code == INDIRECT_REF
15314 || cp_parser_allow_gnu_extensions_p (parser))
15315 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15319 /* Try the pointer-to-member case. */
15320 cp_parser_parse_tentatively (parser);
15321 /* Look for the optional `::' operator. */
15322 cp_parser_global_scope_opt (parser,
15323 /*current_scope_valid_p=*/false);
15324 /* Look for the nested-name specifier. */
15325 token = cp_lexer_peek_token (parser->lexer);
15326 cp_parser_nested_name_specifier (parser,
15327 /*typename_keyword_p=*/false,
15328 /*check_dependency_p=*/true,
15330 /*is_declaration=*/false);
15331 /* If we found it, and the next token is a `*', then we are
15332 indeed looking at a pointer-to-member operator. */
15333 if (!cp_parser_error_occurred (parser)
15334 && cp_parser_require (parser, CPP_MULT, RT_MULT))
15336 /* Indicate that the `*' operator was used. */
15337 code = INDIRECT_REF;
15339 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15340 error_at (token->location, "%qD is a namespace", parser->scope);
15343 /* The type of which the member is a member is given by the
15345 *type = parser->scope;
15346 /* The next name will not be qualified. */
15347 parser->scope = NULL_TREE;
15348 parser->qualifying_scope = NULL_TREE;
15349 parser->object_scope = NULL_TREE;
15350 /* Look for the optional cv-qualifier-seq. */
15351 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15354 /* If that didn't work we don't have a ptr-operator. */
15355 if (!cp_parser_parse_definitely (parser))
15356 cp_parser_error (parser, "expected ptr-operator");
15362 /* Parse an (optional) cv-qualifier-seq.
15365 cv-qualifier cv-qualifier-seq [opt]
15376 Returns a bitmask representing the cv-qualifiers. */
15379 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15381 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15386 cp_cv_quals cv_qualifier;
15388 /* Peek at the next token. */
15389 token = cp_lexer_peek_token (parser->lexer);
15390 /* See if it's a cv-qualifier. */
15391 switch (token->keyword)
15394 cv_qualifier = TYPE_QUAL_CONST;
15398 cv_qualifier = TYPE_QUAL_VOLATILE;
15402 cv_qualifier = TYPE_QUAL_RESTRICT;
15406 cv_qualifier = TYPE_UNQUALIFIED;
15413 if (cv_quals & cv_qualifier)
15415 error_at (token->location, "duplicate cv-qualifier");
15416 cp_lexer_purge_token (parser->lexer);
15420 cp_lexer_consume_token (parser->lexer);
15421 cv_quals |= cv_qualifier;
15428 /* Parse a late-specified return type, if any. This is not a separate
15429 non-terminal, but part of a function declarator, which looks like
15431 -> trailing-type-specifier-seq abstract-declarator(opt)
15433 Returns the type indicated by the type-id. */
15436 cp_parser_late_return_type_opt (cp_parser* parser)
15440 /* Peek at the next token. */
15441 token = cp_lexer_peek_token (parser->lexer);
15442 /* A late-specified return type is indicated by an initial '->'. */
15443 if (token->type != CPP_DEREF)
15446 /* Consume the ->. */
15447 cp_lexer_consume_token (parser->lexer);
15449 return cp_parser_trailing_type_id (parser);
15452 /* Parse a declarator-id.
15456 :: [opt] nested-name-specifier [opt] type-name
15458 In the `id-expression' case, the value returned is as for
15459 cp_parser_id_expression if the id-expression was an unqualified-id.
15460 If the id-expression was a qualified-id, then a SCOPE_REF is
15461 returned. The first operand is the scope (either a NAMESPACE_DECL
15462 or TREE_TYPE), but the second is still just a representation of an
15466 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15469 /* The expression must be an id-expression. Assume that qualified
15470 names are the names of types so that:
15473 int S<T>::R::i = 3;
15475 will work; we must treat `S<T>::R' as the name of a type.
15476 Similarly, assume that qualified names are templates, where
15480 int S<T>::R<T>::i = 3;
15483 id = cp_parser_id_expression (parser,
15484 /*template_keyword_p=*/false,
15485 /*check_dependency_p=*/false,
15486 /*template_p=*/NULL,
15487 /*declarator_p=*/true,
15489 if (id && BASELINK_P (id))
15490 id = BASELINK_FUNCTIONS (id);
15494 /* Parse a type-id.
15497 type-specifier-seq abstract-declarator [opt]
15499 Returns the TYPE specified. */
15502 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15503 bool is_trailing_return)
15505 cp_decl_specifier_seq type_specifier_seq;
15506 cp_declarator *abstract_declarator;
15508 /* Parse the type-specifier-seq. */
15509 cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15510 is_trailing_return,
15511 &type_specifier_seq);
15512 if (type_specifier_seq.type == error_mark_node)
15513 return error_mark_node;
15515 /* There might or might not be an abstract declarator. */
15516 cp_parser_parse_tentatively (parser);
15517 /* Look for the declarator. */
15518 abstract_declarator
15519 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15520 /*parenthesized_p=*/NULL,
15521 /*member_p=*/false);
15522 /* Check to see if there really was a declarator. */
15523 if (!cp_parser_parse_definitely (parser))
15524 abstract_declarator = NULL;
15526 if (type_specifier_seq.type
15527 && type_uses_auto (type_specifier_seq.type))
15529 /* A type-id with type 'auto' is only ok if the abstract declarator
15530 is a function declarator with a late-specified return type. */
15531 if (abstract_declarator
15532 && abstract_declarator->kind == cdk_function
15533 && abstract_declarator->u.function.late_return_type)
15537 error ("invalid use of %<auto%>");
15538 return error_mark_node;
15542 return groktypename (&type_specifier_seq, abstract_declarator,
15546 static tree cp_parser_type_id (cp_parser *parser)
15548 return cp_parser_type_id_1 (parser, false, false);
15551 static tree cp_parser_template_type_arg (cp_parser *parser)
15554 const char *saved_message = parser->type_definition_forbidden_message;
15555 parser->type_definition_forbidden_message
15556 = G_("types may not be defined in template arguments");
15557 r = cp_parser_type_id_1 (parser, true, false);
15558 parser->type_definition_forbidden_message = saved_message;
15562 static tree cp_parser_trailing_type_id (cp_parser *parser)
15564 return cp_parser_type_id_1 (parser, false, true);
15567 /* Parse a type-specifier-seq.
15569 type-specifier-seq:
15570 type-specifier type-specifier-seq [opt]
15574 type-specifier-seq:
15575 attributes type-specifier-seq [opt]
15577 If IS_DECLARATION is true, we are at the start of a "condition" or
15578 exception-declaration, so we might be followed by a declarator-id.
15580 If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15581 i.e. we've just seen "->".
15583 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
15586 cp_parser_type_specifier_seq (cp_parser* parser,
15587 bool is_declaration,
15588 bool is_trailing_return,
15589 cp_decl_specifier_seq *type_specifier_seq)
15591 bool seen_type_specifier = false;
15592 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15593 cp_token *start_token = NULL;
15595 /* Clear the TYPE_SPECIFIER_SEQ. */
15596 clear_decl_specs (type_specifier_seq);
15598 /* In the context of a trailing return type, enum E { } is an
15599 elaborated-type-specifier followed by a function-body, not an
15601 if (is_trailing_return)
15602 flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15604 /* Parse the type-specifiers and attributes. */
15607 tree type_specifier;
15608 bool is_cv_qualifier;
15610 /* Check for attributes first. */
15611 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15613 type_specifier_seq->attributes =
15614 chainon (type_specifier_seq->attributes,
15615 cp_parser_attributes_opt (parser));
15619 /* record the token of the beginning of the type specifier seq,
15620 for error reporting purposes*/
15622 start_token = cp_lexer_peek_token (parser->lexer);
15624 /* Look for the type-specifier. */
15625 type_specifier = cp_parser_type_specifier (parser,
15627 type_specifier_seq,
15628 /*is_declaration=*/false,
15631 if (!type_specifier)
15633 /* If the first type-specifier could not be found, this is not a
15634 type-specifier-seq at all. */
15635 if (!seen_type_specifier)
15637 cp_parser_error (parser, "expected type-specifier");
15638 type_specifier_seq->type = error_mark_node;
15641 /* If subsequent type-specifiers could not be found, the
15642 type-specifier-seq is complete. */
15646 seen_type_specifier = true;
15647 /* The standard says that a condition can be:
15649 type-specifier-seq declarator = assignment-expression
15656 we should treat the "S" as a declarator, not as a
15657 type-specifier. The standard doesn't say that explicitly for
15658 type-specifier-seq, but it does say that for
15659 decl-specifier-seq in an ordinary declaration. Perhaps it
15660 would be clearer just to allow a decl-specifier-seq here, and
15661 then add a semantic restriction that if any decl-specifiers
15662 that are not type-specifiers appear, the program is invalid. */
15663 if (is_declaration && !is_cv_qualifier)
15664 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15667 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15670 /* Parse a parameter-declaration-clause.
15672 parameter-declaration-clause:
15673 parameter-declaration-list [opt] ... [opt]
15674 parameter-declaration-list , ...
15676 Returns a representation for the parameter declarations. A return
15677 value of NULL indicates a parameter-declaration-clause consisting
15678 only of an ellipsis. */
15681 cp_parser_parameter_declaration_clause (cp_parser* parser)
15688 /* Peek at the next token. */
15689 token = cp_lexer_peek_token (parser->lexer);
15690 /* Check for trivial parameter-declaration-clauses. */
15691 if (token->type == CPP_ELLIPSIS)
15693 /* Consume the `...' token. */
15694 cp_lexer_consume_token (parser->lexer);
15697 else if (token->type == CPP_CLOSE_PAREN)
15698 /* There are no parameters. */
15700 #ifndef NO_IMPLICIT_EXTERN_C
15701 if (in_system_header && current_class_type == NULL
15702 && current_lang_name == lang_name_c)
15706 return void_list_node;
15708 /* Check for `(void)', too, which is a special case. */
15709 else if (token->keyword == RID_VOID
15710 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15711 == CPP_CLOSE_PAREN))
15713 /* Consume the `void' token. */
15714 cp_lexer_consume_token (parser->lexer);
15715 /* There are no parameters. */
15716 return void_list_node;
15719 /* Parse the parameter-declaration-list. */
15720 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15721 /* If a parse error occurred while parsing the
15722 parameter-declaration-list, then the entire
15723 parameter-declaration-clause is erroneous. */
15727 /* Peek at the next token. */
15728 token = cp_lexer_peek_token (parser->lexer);
15729 /* If it's a `,', the clause should terminate with an ellipsis. */
15730 if (token->type == CPP_COMMA)
15732 /* Consume the `,'. */
15733 cp_lexer_consume_token (parser->lexer);
15734 /* Expect an ellipsis. */
15736 = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15738 /* It might also be `...' if the optional trailing `,' was
15740 else if (token->type == CPP_ELLIPSIS)
15742 /* Consume the `...' token. */
15743 cp_lexer_consume_token (parser->lexer);
15744 /* And remember that we saw it. */
15748 ellipsis_p = false;
15750 /* Finish the parameter list. */
15752 parameters = chainon (parameters, void_list_node);
15757 /* Parse a parameter-declaration-list.
15759 parameter-declaration-list:
15760 parameter-declaration
15761 parameter-declaration-list , parameter-declaration
15763 Returns a representation of the parameter-declaration-list, as for
15764 cp_parser_parameter_declaration_clause. However, the
15765 `void_list_node' is never appended to the list. Upon return,
15766 *IS_ERROR will be true iff an error occurred. */
15769 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15771 tree parameters = NULL_TREE;
15772 tree *tail = ¶meters;
15773 bool saved_in_unbraced_linkage_specification_p;
15776 /* Assume all will go well. */
15778 /* The special considerations that apply to a function within an
15779 unbraced linkage specifications do not apply to the parameters
15780 to the function. */
15781 saved_in_unbraced_linkage_specification_p
15782 = parser->in_unbraced_linkage_specification_p;
15783 parser->in_unbraced_linkage_specification_p = false;
15785 /* Look for more parameters. */
15788 cp_parameter_declarator *parameter;
15789 tree decl = error_mark_node;
15790 bool parenthesized_p;
15791 /* Parse the parameter. */
15793 = cp_parser_parameter_declaration (parser,
15794 /*template_parm_p=*/false,
15797 /* We don't know yet if the enclosing context is deprecated, so wait
15798 and warn in grokparms if appropriate. */
15799 deprecated_state = DEPRECATED_SUPPRESS;
15802 decl = grokdeclarator (parameter->declarator,
15803 ¶meter->decl_specifiers,
15805 parameter->default_argument != NULL_TREE,
15806 ¶meter->decl_specifiers.attributes);
15808 deprecated_state = DEPRECATED_NORMAL;
15810 /* If a parse error occurred parsing the parameter declaration,
15811 then the entire parameter-declaration-list is erroneous. */
15812 if (decl == error_mark_node)
15815 parameters = error_mark_node;
15819 if (parameter->decl_specifiers.attributes)
15820 cplus_decl_attributes (&decl,
15821 parameter->decl_specifiers.attributes,
15823 if (DECL_NAME (decl))
15824 decl = pushdecl (decl);
15826 if (decl != error_mark_node)
15828 retrofit_lang_decl (decl);
15829 DECL_PARM_INDEX (decl) = ++index;
15830 DECL_PARM_LEVEL (decl) = function_parm_depth ();
15833 /* Add the new parameter to the list. */
15834 *tail = build_tree_list (parameter->default_argument, decl);
15835 tail = &TREE_CHAIN (*tail);
15837 /* Peek at the next token. */
15838 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15839 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15840 /* These are for Objective-C++ */
15841 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15842 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15843 /* The parameter-declaration-list is complete. */
15845 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15849 /* Peek at the next token. */
15850 token = cp_lexer_peek_nth_token (parser->lexer, 2);
15851 /* If it's an ellipsis, then the list is complete. */
15852 if (token->type == CPP_ELLIPSIS)
15854 /* Otherwise, there must be more parameters. Consume the
15856 cp_lexer_consume_token (parser->lexer);
15857 /* When parsing something like:
15859 int i(float f, double d)
15861 we can tell after seeing the declaration for "f" that we
15862 are not looking at an initialization of a variable "i",
15863 but rather at the declaration of a function "i".
15865 Due to the fact that the parsing of template arguments
15866 (as specified to a template-id) requires backtracking we
15867 cannot use this technique when inside a template argument
15869 if (!parser->in_template_argument_list_p
15870 && !parser->in_type_id_in_expr_p
15871 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15872 /* However, a parameter-declaration of the form
15873 "foat(f)" (which is a valid declaration of a
15874 parameter "f") can also be interpreted as an
15875 expression (the conversion of "f" to "float"). */
15876 && !parenthesized_p)
15877 cp_parser_commit_to_tentative_parse (parser);
15881 cp_parser_error (parser, "expected %<,%> or %<...%>");
15882 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15883 cp_parser_skip_to_closing_parenthesis (parser,
15884 /*recovering=*/true,
15885 /*or_comma=*/false,
15886 /*consume_paren=*/false);
15891 parser->in_unbraced_linkage_specification_p
15892 = saved_in_unbraced_linkage_specification_p;
15897 /* Parse a parameter declaration.
15899 parameter-declaration:
15900 decl-specifier-seq ... [opt] declarator
15901 decl-specifier-seq declarator = assignment-expression
15902 decl-specifier-seq ... [opt] abstract-declarator [opt]
15903 decl-specifier-seq abstract-declarator [opt] = assignment-expression
15905 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15906 declares a template parameter. (In that case, a non-nested `>'
15907 token encountered during the parsing of the assignment-expression
15908 is not interpreted as a greater-than operator.)
15910 Returns a representation of the parameter, or NULL if an error
15911 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15912 true iff the declarator is of the form "(p)". */
15914 static cp_parameter_declarator *
15915 cp_parser_parameter_declaration (cp_parser *parser,
15916 bool template_parm_p,
15917 bool *parenthesized_p)
15919 int declares_class_or_enum;
15920 cp_decl_specifier_seq decl_specifiers;
15921 cp_declarator *declarator;
15922 tree default_argument;
15923 cp_token *token = NULL, *declarator_token_start = NULL;
15924 const char *saved_message;
15926 /* In a template parameter, `>' is not an operator.
15930 When parsing a default template-argument for a non-type
15931 template-parameter, the first non-nested `>' is taken as the end
15932 of the template parameter-list rather than a greater-than
15935 /* Type definitions may not appear in parameter types. */
15936 saved_message = parser->type_definition_forbidden_message;
15937 parser->type_definition_forbidden_message
15938 = G_("types may not be defined in parameter types");
15940 /* Parse the declaration-specifiers. */
15941 cp_parser_decl_specifier_seq (parser,
15942 CP_PARSER_FLAGS_NONE,
15944 &declares_class_or_enum);
15946 /* Complain about missing 'typename' or other invalid type names. */
15947 if (!decl_specifiers.any_type_specifiers_p)
15948 cp_parser_parse_and_diagnose_invalid_type_name (parser);
15950 /* If an error occurred, there's no reason to attempt to parse the
15951 rest of the declaration. */
15952 if (cp_parser_error_occurred (parser))
15954 parser->type_definition_forbidden_message = saved_message;
15958 /* Peek at the next token. */
15959 token = cp_lexer_peek_token (parser->lexer);
15961 /* If the next token is a `)', `,', `=', `>', or `...', then there
15962 is no declarator. However, when variadic templates are enabled,
15963 there may be a declarator following `...'. */
15964 if (token->type == CPP_CLOSE_PAREN
15965 || token->type == CPP_COMMA
15966 || token->type == CPP_EQ
15967 || token->type == CPP_GREATER)
15970 if (parenthesized_p)
15971 *parenthesized_p = false;
15973 /* Otherwise, there should be a declarator. */
15976 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15977 parser->default_arg_ok_p = false;
15979 /* After seeing a decl-specifier-seq, if the next token is not a
15980 "(", there is no possibility that the code is a valid
15981 expression. Therefore, if parsing tentatively, we commit at
15983 if (!parser->in_template_argument_list_p
15984 /* In an expression context, having seen:
15988 we cannot be sure whether we are looking at a
15989 function-type (taking a "char" as a parameter) or a cast
15990 of some object of type "char" to "int". */
15991 && !parser->in_type_id_in_expr_p
15992 && cp_parser_uncommitted_to_tentative_parse_p (parser)
15993 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15994 cp_parser_commit_to_tentative_parse (parser);
15995 /* Parse the declarator. */
15996 declarator_token_start = token;
15997 declarator = cp_parser_declarator (parser,
15998 CP_PARSER_DECLARATOR_EITHER,
15999 /*ctor_dtor_or_conv_p=*/NULL,
16001 /*member_p=*/false);
16002 parser->default_arg_ok_p = saved_default_arg_ok_p;
16003 /* After the declarator, allow more attributes. */
16004 decl_specifiers.attributes
16005 = chainon (decl_specifiers.attributes,
16006 cp_parser_attributes_opt (parser));
16009 /* If the next token is an ellipsis, and we have not seen a
16010 declarator name, and the type of the declarator contains parameter
16011 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16012 a parameter pack expansion expression. Otherwise, leave the
16013 ellipsis for a C-style variadic function. */
16014 token = cp_lexer_peek_token (parser->lexer);
16015 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16017 tree type = decl_specifiers.type;
16019 if (type && DECL_P (type))
16020 type = TREE_TYPE (type);
16023 && TREE_CODE (type) != TYPE_PACK_EXPANSION
16024 && declarator_can_be_parameter_pack (declarator)
16025 && (!declarator || !declarator->parameter_pack_p)
16026 && uses_parameter_packs (type))
16028 /* Consume the `...'. */
16029 cp_lexer_consume_token (parser->lexer);
16030 maybe_warn_variadic_templates ();
16032 /* Build a pack expansion type */
16034 declarator->parameter_pack_p = true;
16036 decl_specifiers.type = make_pack_expansion (type);
16040 /* The restriction on defining new types applies only to the type
16041 of the parameter, not to the default argument. */
16042 parser->type_definition_forbidden_message = saved_message;
16044 /* If the next token is `=', then process a default argument. */
16045 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16047 /* Consume the `='. */
16048 cp_lexer_consume_token (parser->lexer);
16050 /* If we are defining a class, then the tokens that make up the
16051 default argument must be saved and processed later. */
16052 if (!template_parm_p && at_class_scope_p ()
16053 && TYPE_BEING_DEFINED (current_class_type)
16054 && !LAMBDA_TYPE_P (current_class_type))
16056 unsigned depth = 0;
16057 int maybe_template_id = 0;
16058 cp_token *first_token;
16061 /* Add tokens until we have processed the entire default
16062 argument. We add the range [first_token, token). */
16063 first_token = cp_lexer_peek_token (parser->lexer);
16068 /* Peek at the next token. */
16069 token = cp_lexer_peek_token (parser->lexer);
16070 /* What we do depends on what token we have. */
16071 switch (token->type)
16073 /* In valid code, a default argument must be
16074 immediately followed by a `,' `)', or `...'. */
16076 if (depth == 0 && maybe_template_id)
16078 /* If we've seen a '<', we might be in a
16079 template-argument-list. Until Core issue 325 is
16080 resolved, we don't know how this situation ought
16081 to be handled, so try to DTRT. We check whether
16082 what comes after the comma is a valid parameter
16083 declaration list. If it is, then the comma ends
16084 the default argument; otherwise the default
16085 argument continues. */
16086 bool error = false;
16089 /* Set ITALP so cp_parser_parameter_declaration_list
16090 doesn't decide to commit to this parse. */
16091 bool saved_italp = parser->in_template_argument_list_p;
16092 parser->in_template_argument_list_p = true;
16094 cp_parser_parse_tentatively (parser);
16095 cp_lexer_consume_token (parser->lexer);
16096 begin_scope (sk_function_parms, NULL_TREE);
16097 cp_parser_parameter_declaration_list (parser, &error);
16098 for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16099 pop_binding (DECL_NAME (t), t);
16101 if (!cp_parser_error_occurred (parser) && !error)
16103 cp_parser_abort_tentative_parse (parser);
16105 parser->in_template_argument_list_p = saved_italp;
16108 case CPP_CLOSE_PAREN:
16110 /* If we run into a non-nested `;', `}', or `]',
16111 then the code is invalid -- but the default
16112 argument is certainly over. */
16113 case CPP_SEMICOLON:
16114 case CPP_CLOSE_BRACE:
16115 case CPP_CLOSE_SQUARE:
16118 /* Update DEPTH, if necessary. */
16119 else if (token->type == CPP_CLOSE_PAREN
16120 || token->type == CPP_CLOSE_BRACE
16121 || token->type == CPP_CLOSE_SQUARE)
16125 case CPP_OPEN_PAREN:
16126 case CPP_OPEN_SQUARE:
16127 case CPP_OPEN_BRACE:
16133 /* This might be the comparison operator, or it might
16134 start a template argument list. */
16135 ++maybe_template_id;
16139 if (cxx_dialect == cxx98)
16141 /* Fall through for C++0x, which treats the `>>'
16142 operator like two `>' tokens in certain
16148 /* This might be an operator, or it might close a
16149 template argument list. But if a previous '<'
16150 started a template argument list, this will have
16151 closed it, so we can't be in one anymore. */
16152 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16153 if (maybe_template_id < 0)
16154 maybe_template_id = 0;
16158 /* If we run out of tokens, issue an error message. */
16160 case CPP_PRAGMA_EOL:
16161 error_at (token->location, "file ends in default argument");
16167 /* In these cases, we should look for template-ids.
16168 For example, if the default argument is
16169 `X<int, double>()', we need to do name lookup to
16170 figure out whether or not `X' is a template; if
16171 so, the `,' does not end the default argument.
16173 That is not yet done. */
16180 /* If we've reached the end, stop. */
16184 /* Add the token to the token block. */
16185 token = cp_lexer_consume_token (parser->lexer);
16188 /* Create a DEFAULT_ARG to represent the unparsed default
16190 default_argument = make_node (DEFAULT_ARG);
16191 DEFARG_TOKENS (default_argument)
16192 = cp_token_cache_new (first_token, token);
16193 DEFARG_INSTANTIATIONS (default_argument) = NULL;
16195 /* Outside of a class definition, we can just parse the
16196 assignment-expression. */
16199 token = cp_lexer_peek_token (parser->lexer);
16201 = cp_parser_default_argument (parser, template_parm_p);
16204 if (!parser->default_arg_ok_p)
16206 if (flag_permissive)
16207 warning (0, "deprecated use of default argument for parameter of non-function");
16210 error_at (token->location,
16211 "default arguments are only "
16212 "permitted for function parameters");
16213 default_argument = NULL_TREE;
16216 else if ((declarator && declarator->parameter_pack_p)
16217 || (decl_specifiers.type
16218 && PACK_EXPANSION_P (decl_specifiers.type)))
16220 /* Find the name of the parameter pack. */
16221 cp_declarator *id_declarator = declarator;
16222 while (id_declarator && id_declarator->kind != cdk_id)
16223 id_declarator = id_declarator->declarator;
16225 if (id_declarator && id_declarator->kind == cdk_id)
16226 error_at (declarator_token_start->location,
16228 ? "template parameter pack %qD"
16229 " cannot have a default argument"
16230 : "parameter pack %qD cannot have a default argument",
16231 id_declarator->u.id.unqualified_name);
16233 error_at (declarator_token_start->location,
16235 ? "template parameter pack cannot have a default argument"
16236 : "parameter pack cannot have a default argument");
16238 default_argument = NULL_TREE;
16242 default_argument = NULL_TREE;
16244 return make_parameter_declarator (&decl_specifiers,
16249 /* Parse a default argument and return it.
16251 TEMPLATE_PARM_P is true if this is a default argument for a
16252 non-type template parameter. */
16254 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16256 tree default_argument = NULL_TREE;
16257 bool saved_greater_than_is_operator_p;
16258 bool saved_local_variables_forbidden_p;
16260 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16262 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16263 parser->greater_than_is_operator_p = !template_parm_p;
16264 /* Local variable names (and the `this' keyword) may not
16265 appear in a default argument. */
16266 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16267 parser->local_variables_forbidden_p = true;
16268 /* Parse the assignment-expression. */
16269 if (template_parm_p)
16270 push_deferring_access_checks (dk_no_deferred);
16272 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16273 if (template_parm_p)
16274 pop_deferring_access_checks ();
16275 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16276 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16278 return default_argument;
16281 /* Parse a function-body.
16284 compound_statement */
16287 cp_parser_function_body (cp_parser *parser)
16289 cp_parser_compound_statement (parser, NULL, false, true);
16292 /* Parse a ctor-initializer-opt followed by a function-body. Return
16293 true if a ctor-initializer was present. */
16296 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16299 bool ctor_initializer_p;
16300 const bool check_body_p =
16301 DECL_CONSTRUCTOR_P (current_function_decl)
16302 && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16305 /* Begin the function body. */
16306 body = begin_function_body ();
16307 /* Parse the optional ctor-initializer. */
16308 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16310 /* If we're parsing a constexpr constructor definition, we need
16311 to check that the constructor body is indeed empty. However,
16312 before we get to cp_parser_function_body lot of junk has been
16313 generated, so we can't just check that we have an empty block.
16314 Rather we take a snapshot of the outermost block, and check whether
16315 cp_parser_function_body changed its state. */
16319 if (TREE_CODE (list) == BIND_EXPR)
16320 list = BIND_EXPR_BODY (list);
16321 if (TREE_CODE (list) == STATEMENT_LIST
16322 && STATEMENT_LIST_TAIL (list) != NULL)
16323 last = STATEMENT_LIST_TAIL (list)->stmt;
16325 /* Parse the function-body. */
16326 cp_parser_function_body (parser);
16328 check_constexpr_ctor_body (last, list);
16329 /* Finish the function body. */
16330 finish_function_body (body);
16332 return ctor_initializer_p;
16335 /* Parse an initializer.
16338 = initializer-clause
16339 ( expression-list )
16341 Returns an expression representing the initializer. If no
16342 initializer is present, NULL_TREE is returned.
16344 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16345 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
16346 set to TRUE if there is no initializer present. If there is an
16347 initializer, and it is not a constant-expression, *NON_CONSTANT_P
16348 is set to true; otherwise it is set to false. */
16351 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16352 bool* non_constant_p)
16357 /* Peek at the next token. */
16358 token = cp_lexer_peek_token (parser->lexer);
16360 /* Let our caller know whether or not this initializer was
16362 *is_direct_init = (token->type != CPP_EQ);
16363 /* Assume that the initializer is constant. */
16364 *non_constant_p = false;
16366 if (token->type == CPP_EQ)
16368 /* Consume the `='. */
16369 cp_lexer_consume_token (parser->lexer);
16370 /* Parse the initializer-clause. */
16371 init = cp_parser_initializer_clause (parser, non_constant_p);
16373 else if (token->type == CPP_OPEN_PAREN)
16376 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16378 /*allow_expansion_p=*/true,
16381 return error_mark_node;
16382 init = build_tree_list_vec (vec);
16383 release_tree_vector (vec);
16385 else if (token->type == CPP_OPEN_BRACE)
16387 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16388 init = cp_parser_braced_list (parser, non_constant_p);
16389 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16393 /* Anything else is an error. */
16394 cp_parser_error (parser, "expected initializer");
16395 init = error_mark_node;
16401 /* Parse an initializer-clause.
16403 initializer-clause:
16404 assignment-expression
16407 Returns an expression representing the initializer.
16409 If the `assignment-expression' production is used the value
16410 returned is simply a representation for the expression.
16412 Otherwise, calls cp_parser_braced_list. */
16415 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16419 /* Assume the expression is constant. */
16420 *non_constant_p = false;
16422 /* If it is not a `{', then we are looking at an
16423 assignment-expression. */
16424 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16427 = cp_parser_constant_expression (parser,
16428 /*allow_non_constant_p=*/true,
16430 if (!*non_constant_p)
16432 /* We only want to fold if this is really a constant
16433 expression. FIXME Actually, we don't want to fold here, but in
16435 tree folded = fold_non_dependent_expr (initializer);
16436 folded = maybe_constant_value (folded);
16437 if (TREE_CONSTANT (folded))
16438 initializer = folded;
16442 initializer = cp_parser_braced_list (parser, non_constant_p);
16444 return initializer;
16447 /* Parse a brace-enclosed initializer list.
16450 { initializer-list , [opt] }
16453 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
16454 the elements of the initializer-list (or NULL, if the last
16455 production is used). The TREE_TYPE for the CONSTRUCTOR will be
16456 NULL_TREE. There is no way to detect whether or not the optional
16457 trailing `,' was provided. NON_CONSTANT_P is as for
16458 cp_parser_initializer. */
16461 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16465 /* Consume the `{' token. */
16466 cp_lexer_consume_token (parser->lexer);
16467 /* Create a CONSTRUCTOR to represent the braced-initializer. */
16468 initializer = make_node (CONSTRUCTOR);
16469 /* If it's not a `}', then there is a non-trivial initializer. */
16470 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16472 /* Parse the initializer list. */
16473 CONSTRUCTOR_ELTS (initializer)
16474 = cp_parser_initializer_list (parser, non_constant_p);
16475 /* A trailing `,' token is allowed. */
16476 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16477 cp_lexer_consume_token (parser->lexer);
16479 /* Now, there should be a trailing `}'. */
16480 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16481 TREE_TYPE (initializer) = init_list_type_node;
16482 return initializer;
16485 /* Parse an initializer-list.
16488 initializer-clause ... [opt]
16489 initializer-list , initializer-clause ... [opt]
16494 identifier : initializer-clause
16495 initializer-list, identifier : initializer-clause
16497 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
16498 for the initializer. If the INDEX of the elt is non-NULL, it is the
16499 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
16500 as for cp_parser_initializer. */
16502 static VEC(constructor_elt,gc) *
16503 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16505 VEC(constructor_elt,gc) *v = NULL;
16507 /* Assume all of the expressions are constant. */
16508 *non_constant_p = false;
16510 /* Parse the rest of the list. */
16516 bool clause_non_constant_p;
16518 /* If the next token is an identifier and the following one is a
16519 colon, we are looking at the GNU designated-initializer
16521 if (cp_parser_allow_gnu_extensions_p (parser)
16522 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16523 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16525 /* Warn the user that they are using an extension. */
16526 pedwarn (input_location, OPT_pedantic,
16527 "ISO C++ does not allow designated initializers");
16528 /* Consume the identifier. */
16529 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16530 /* Consume the `:'. */
16531 cp_lexer_consume_token (parser->lexer);
16534 identifier = NULL_TREE;
16536 /* Parse the initializer. */
16537 initializer = cp_parser_initializer_clause (parser,
16538 &clause_non_constant_p);
16539 /* If any clause is non-constant, so is the entire initializer. */
16540 if (clause_non_constant_p)
16541 *non_constant_p = true;
16543 /* If we have an ellipsis, this is an initializer pack
16545 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16547 /* Consume the `...'. */
16548 cp_lexer_consume_token (parser->lexer);
16550 /* Turn the initializer into an initializer expansion. */
16551 initializer = make_pack_expansion (initializer);
16554 /* Add it to the vector. */
16555 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16557 /* If the next token is not a comma, we have reached the end of
16559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16562 /* Peek at the next token. */
16563 token = cp_lexer_peek_nth_token (parser->lexer, 2);
16564 /* If the next token is a `}', then we're still done. An
16565 initializer-clause can have a trailing `,' after the
16566 initializer-list and before the closing `}'. */
16567 if (token->type == CPP_CLOSE_BRACE)
16570 /* Consume the `,' token. */
16571 cp_lexer_consume_token (parser->lexer);
16577 /* Classes [gram.class] */
16579 /* Parse a class-name.
16585 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16586 to indicate that names looked up in dependent types should be
16587 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
16588 keyword has been used to indicate that the name that appears next
16589 is a template. TAG_TYPE indicates the explicit tag given before
16590 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
16591 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
16592 is the class being defined in a class-head.
16594 Returns the TYPE_DECL representing the class. */
16597 cp_parser_class_name (cp_parser *parser,
16598 bool typename_keyword_p,
16599 bool template_keyword_p,
16600 enum tag_types tag_type,
16601 bool check_dependency_p,
16603 bool is_declaration)
16609 tree identifier = NULL_TREE;
16611 /* All class-names start with an identifier. */
16612 token = cp_lexer_peek_token (parser->lexer);
16613 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16615 cp_parser_error (parser, "expected class-name");
16616 return error_mark_node;
16619 /* PARSER->SCOPE can be cleared when parsing the template-arguments
16620 to a template-id, so we save it here. */
16621 scope = parser->scope;
16622 if (scope == error_mark_node)
16623 return error_mark_node;
16625 /* Any name names a type if we're following the `typename' keyword
16626 in a qualified name where the enclosing scope is type-dependent. */
16627 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16628 && dependent_type_p (scope));
16629 /* Handle the common case (an identifier, but not a template-id)
16631 if (token->type == CPP_NAME
16632 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16634 cp_token *identifier_token;
16637 /* Look for the identifier. */
16638 identifier_token = cp_lexer_peek_token (parser->lexer);
16639 ambiguous_p = identifier_token->ambiguous_p;
16640 identifier = cp_parser_identifier (parser);
16641 /* If the next token isn't an identifier, we are certainly not
16642 looking at a class-name. */
16643 if (identifier == error_mark_node)
16644 decl = error_mark_node;
16645 /* If we know this is a type-name, there's no need to look it
16647 else if (typename_p)
16651 tree ambiguous_decls;
16652 /* If we already know that this lookup is ambiguous, then
16653 we've already issued an error message; there's no reason
16657 cp_parser_simulate_error (parser);
16658 return error_mark_node;
16660 /* If the next token is a `::', then the name must be a type
16663 [basic.lookup.qual]
16665 During the lookup for a name preceding the :: scope
16666 resolution operator, object, function, and enumerator
16667 names are ignored. */
16668 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16669 tag_type = typename_type;
16670 /* Look up the name. */
16671 decl = cp_parser_lookup_name (parser, identifier,
16673 /*is_template=*/false,
16674 /*is_namespace=*/false,
16675 check_dependency_p,
16677 identifier_token->location);
16678 if (ambiguous_decls)
16680 if (cp_parser_parsing_tentatively (parser))
16681 cp_parser_simulate_error (parser);
16682 return error_mark_node;
16688 /* Try a template-id. */
16689 decl = cp_parser_template_id (parser, template_keyword_p,
16690 check_dependency_p,
16692 if (decl == error_mark_node)
16693 return error_mark_node;
16696 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16698 /* If this is a typename, create a TYPENAME_TYPE. */
16699 if (typename_p && decl != error_mark_node)
16701 decl = make_typename_type (scope, decl, typename_type,
16702 /*complain=*/tf_error);
16703 if (decl != error_mark_node)
16704 decl = TYPE_NAME (decl);
16707 /* Check to see that it is really the name of a class. */
16708 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16709 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16710 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16711 /* Situations like this:
16713 template <typename T> struct A {
16714 typename T::template X<int>::I i;
16717 are problematic. Is `T::template X<int>' a class-name? The
16718 standard does not seem to be definitive, but there is no other
16719 valid interpretation of the following `::'. Therefore, those
16720 names are considered class-names. */
16722 decl = make_typename_type (scope, decl, tag_type, tf_error);
16723 if (decl != error_mark_node)
16724 decl = TYPE_NAME (decl);
16726 else if (TREE_CODE (decl) != TYPE_DECL
16727 || TREE_TYPE (decl) == error_mark_node
16728 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16729 /* In Objective-C 2.0, a classname followed by '.' starts a
16730 dot-syntax expression, and it's not a type-name. */
16731 || (c_dialect_objc ()
16732 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
16733 && objc_is_class_name (decl)))
16734 decl = error_mark_node;
16736 if (decl == error_mark_node)
16737 cp_parser_error (parser, "expected class-name");
16738 else if (identifier && !parser->scope)
16739 maybe_note_name_used_in_class (identifier, decl);
16744 /* Parse a class-specifier.
16747 class-head { member-specification [opt] }
16749 Returns the TREE_TYPE representing the class. */
16752 cp_parser_class_specifier_1 (cp_parser* parser)
16755 tree attributes = NULL_TREE;
16756 bool nested_name_specifier_p;
16757 unsigned saved_num_template_parameter_lists;
16758 bool saved_in_function_body;
16759 bool saved_in_unbraced_linkage_specification_p;
16760 tree old_scope = NULL_TREE;
16761 tree scope = NULL_TREE;
16763 cp_token *closing_brace;
16765 push_deferring_access_checks (dk_no_deferred);
16767 /* Parse the class-head. */
16768 type = cp_parser_class_head (parser,
16769 &nested_name_specifier_p,
16772 /* If the class-head was a semantic disaster, skip the entire body
16776 cp_parser_skip_to_end_of_block_or_statement (parser);
16777 pop_deferring_access_checks ();
16778 return error_mark_node;
16781 /* Look for the `{'. */
16782 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16784 pop_deferring_access_checks ();
16785 return error_mark_node;
16788 /* Process the base classes. If they're invalid, skip the
16789 entire class body. */
16790 if (!xref_basetypes (type, bases))
16792 /* Consuming the closing brace yields better error messages
16794 if (cp_parser_skip_to_closing_brace (parser))
16795 cp_lexer_consume_token (parser->lexer);
16796 pop_deferring_access_checks ();
16797 return error_mark_node;
16800 /* Issue an error message if type-definitions are forbidden here. */
16801 cp_parser_check_type_definition (parser);
16802 /* Remember that we are defining one more class. */
16803 ++parser->num_classes_being_defined;
16804 /* Inside the class, surrounding template-parameter-lists do not
16806 saved_num_template_parameter_lists
16807 = parser->num_template_parameter_lists;
16808 parser->num_template_parameter_lists = 0;
16809 /* We are not in a function body. */
16810 saved_in_function_body = parser->in_function_body;
16811 parser->in_function_body = false;
16812 /* We are not immediately inside an extern "lang" block. */
16813 saved_in_unbraced_linkage_specification_p
16814 = parser->in_unbraced_linkage_specification_p;
16815 parser->in_unbraced_linkage_specification_p = false;
16817 /* Start the class. */
16818 if (nested_name_specifier_p)
16820 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16821 old_scope = push_inner_scope (scope);
16823 type = begin_class_definition (type, attributes);
16825 if (type == error_mark_node)
16826 /* If the type is erroneous, skip the entire body of the class. */
16827 cp_parser_skip_to_closing_brace (parser);
16829 /* Parse the member-specification. */
16830 cp_parser_member_specification_opt (parser);
16832 /* Look for the trailing `}'. */
16833 closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16834 /* Look for trailing attributes to apply to this class. */
16835 if (cp_parser_allow_gnu_extensions_p (parser))
16836 attributes = cp_parser_attributes_opt (parser);
16837 if (type != error_mark_node)
16838 type = finish_struct (type, attributes);
16839 if (nested_name_specifier_p)
16840 pop_inner_scope (old_scope, scope);
16842 /* We've finished a type definition. Check for the common syntax
16843 error of forgetting a semicolon after the definition. We need to
16844 be careful, as we can't just check for not-a-semicolon and be done
16845 with it; the user might have typed:
16847 class X { } c = ...;
16848 class X { } *p = ...;
16850 and so forth. Instead, enumerate all the possible tokens that
16851 might follow this production; if we don't see one of them, then
16852 complain and silently insert the semicolon. */
16854 cp_token *token = cp_lexer_peek_token (parser->lexer);
16855 bool want_semicolon = true;
16857 switch (token->type)
16860 case CPP_SEMICOLON:
16863 case CPP_OPEN_PAREN:
16864 case CPP_CLOSE_PAREN:
16866 want_semicolon = false;
16869 /* While it's legal for type qualifiers and storage class
16870 specifiers to follow type definitions in the grammar, only
16871 compiler testsuites contain code like that. Assume that if
16872 we see such code, then what we're really seeing is a case
16876 const <type> var = ...;
16881 static <type> func (...) ...
16883 i.e. the qualifier or specifier applies to the next
16884 declaration. To do so, however, we need to look ahead one
16885 more token to see if *that* token is a type specifier.
16887 This code could be improved to handle:
16890 static const <type> var = ...; */
16892 if (keyword_is_decl_specifier (token->keyword))
16894 cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16896 /* Handling user-defined types here would be nice, but very
16899 = (lookahead->type == CPP_KEYWORD
16900 && keyword_begins_type_specifier (lookahead->keyword));
16907 /* If we don't have a type, then something is very wrong and we
16908 shouldn't try to do anything clever. Likewise for not seeing the
16910 if (closing_brace && TYPE_P (type) && want_semicolon)
16912 cp_token_position prev
16913 = cp_lexer_previous_token_position (parser->lexer);
16914 cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16915 location_t loc = prev_token->location;
16917 if (CLASSTYPE_DECLARED_CLASS (type))
16918 error_at (loc, "expected %<;%> after class definition");
16919 else if (TREE_CODE (type) == RECORD_TYPE)
16920 error_at (loc, "expected %<;%> after struct definition");
16921 else if (TREE_CODE (type) == UNION_TYPE)
16922 error_at (loc, "expected %<;%> after union definition");
16924 gcc_unreachable ();
16926 /* Unget one token and smash it to look as though we encountered
16927 a semicolon in the input stream. */
16928 cp_lexer_set_token_position (parser->lexer, prev);
16929 token = cp_lexer_peek_token (parser->lexer);
16930 token->type = CPP_SEMICOLON;
16931 token->keyword = RID_MAX;
16935 /* If this class is not itself within the scope of another class,
16936 then we need to parse the bodies of all of the queued function
16937 definitions. Note that the queued functions defined in a class
16938 are not always processed immediately following the
16939 class-specifier for that class. Consider:
16942 struct B { void f() { sizeof (A); } };
16945 If `f' were processed before the processing of `A' were
16946 completed, there would be no way to compute the size of `A'.
16947 Note that the nesting we are interested in here is lexical --
16948 not the semantic nesting given by TYPE_CONTEXT. In particular,
16951 struct A { struct B; };
16952 struct A::B { void f() { } };
16954 there is no need to delay the parsing of `A::B::f'. */
16955 if (--parser->num_classes_being_defined == 0)
16958 tree class_type = NULL_TREE;
16959 tree pushed_scope = NULL_TREE;
16961 cp_default_arg_entry *e;
16963 /* In a first pass, parse default arguments to the functions.
16964 Then, in a second pass, parse the bodies of the functions.
16965 This two-phased approach handles cases like:
16973 FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
16977 /* If there are default arguments that have not yet been processed,
16978 take care of them now. */
16979 if (class_type != e->class_type)
16982 pop_scope (pushed_scope);
16983 class_type = e->class_type;
16984 pushed_scope = push_scope (class_type);
16986 /* Make sure that any template parameters are in scope. */
16987 maybe_begin_member_template_processing (fn);
16988 /* Parse the default argument expressions. */
16989 cp_parser_late_parsing_default_args (parser, fn);
16990 /* Remove any template parameters from the symbol table. */
16991 maybe_end_member_template_processing ();
16994 pop_scope (pushed_scope);
16995 VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
16996 /* Now parse the body of the functions. */
16997 FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
16998 cp_parser_late_parsing_for_member (parser, fn);
16999 VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17002 /* Put back any saved access checks. */
17003 pop_deferring_access_checks ();
17005 /* Restore saved state. */
17006 parser->in_function_body = saved_in_function_body;
17007 parser->num_template_parameter_lists
17008 = saved_num_template_parameter_lists;
17009 parser->in_unbraced_linkage_specification_p
17010 = saved_in_unbraced_linkage_specification_p;
17016 cp_parser_class_specifier (cp_parser* parser)
17019 timevar_push (TV_PARSE_STRUCT);
17020 ret = cp_parser_class_specifier_1 (parser);
17021 timevar_pop (TV_PARSE_STRUCT);
17025 /* Parse a class-head.
17028 class-key identifier [opt] base-clause [opt]
17029 class-key nested-name-specifier identifier base-clause [opt]
17030 class-key nested-name-specifier [opt] template-id
17034 class-key attributes identifier [opt] base-clause [opt]
17035 class-key attributes nested-name-specifier identifier base-clause [opt]
17036 class-key attributes nested-name-specifier [opt] template-id
17039 Upon return BASES is initialized to the list of base classes (or
17040 NULL, if there are none) in the same form returned by
17041 cp_parser_base_clause.
17043 Returns the TYPE of the indicated class. Sets
17044 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17045 involving a nested-name-specifier was used, and FALSE otherwise.
17047 Returns error_mark_node if this is not a class-head.
17049 Returns NULL_TREE if the class-head is syntactically valid, but
17050 semantically invalid in a way that means we should skip the entire
17051 body of the class. */
17054 cp_parser_class_head (cp_parser* parser,
17055 bool* nested_name_specifier_p,
17056 tree *attributes_p,
17059 tree nested_name_specifier;
17060 enum tag_types class_key;
17061 tree id = NULL_TREE;
17062 tree type = NULL_TREE;
17064 bool template_id_p = false;
17065 bool qualified_p = false;
17066 bool invalid_nested_name_p = false;
17067 bool invalid_explicit_specialization_p = false;
17068 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17069 tree pushed_scope = NULL_TREE;
17070 unsigned num_templates;
17071 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17072 /* Assume no nested-name-specifier will be present. */
17073 *nested_name_specifier_p = false;
17074 /* Assume no template parameter lists will be used in defining the
17077 parser->colon_corrects_to_scope_p = false;
17079 *bases = NULL_TREE;
17081 /* Look for the class-key. */
17082 class_key = cp_parser_class_key (parser);
17083 if (class_key == none_type)
17084 return error_mark_node;
17086 /* Parse the attributes. */
17087 attributes = cp_parser_attributes_opt (parser);
17089 /* If the next token is `::', that is invalid -- but sometimes
17090 people do try to write:
17094 Handle this gracefully by accepting the extra qualifier, and then
17095 issuing an error about it later if this really is a
17096 class-head. If it turns out just to be an elaborated type
17097 specifier, remain silent. */
17098 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17099 qualified_p = true;
17101 push_deferring_access_checks (dk_no_check);
17103 /* Determine the name of the class. Begin by looking for an
17104 optional nested-name-specifier. */
17105 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17106 nested_name_specifier
17107 = cp_parser_nested_name_specifier_opt (parser,
17108 /*typename_keyword_p=*/false,
17109 /*check_dependency_p=*/false,
17111 /*is_declaration=*/false);
17112 /* If there was a nested-name-specifier, then there *must* be an
17114 if (nested_name_specifier)
17116 type_start_token = cp_lexer_peek_token (parser->lexer);
17117 /* Although the grammar says `identifier', it really means
17118 `class-name' or `template-name'. You are only allowed to
17119 define a class that has already been declared with this
17122 The proposed resolution for Core Issue 180 says that wherever
17123 you see `class T::X' you should treat `X' as a type-name.
17125 It is OK to define an inaccessible class; for example:
17127 class A { class B; };
17130 We do not know if we will see a class-name, or a
17131 template-name. We look for a class-name first, in case the
17132 class-name is a template-id; if we looked for the
17133 template-name first we would stop after the template-name. */
17134 cp_parser_parse_tentatively (parser);
17135 type = cp_parser_class_name (parser,
17136 /*typename_keyword_p=*/false,
17137 /*template_keyword_p=*/false,
17139 /*check_dependency_p=*/false,
17140 /*class_head_p=*/true,
17141 /*is_declaration=*/false);
17142 /* If that didn't work, ignore the nested-name-specifier. */
17143 if (!cp_parser_parse_definitely (parser))
17145 invalid_nested_name_p = true;
17146 type_start_token = cp_lexer_peek_token (parser->lexer);
17147 id = cp_parser_identifier (parser);
17148 if (id == error_mark_node)
17151 /* If we could not find a corresponding TYPE, treat this
17152 declaration like an unqualified declaration. */
17153 if (type == error_mark_node)
17154 nested_name_specifier = NULL_TREE;
17155 /* Otherwise, count the number of templates used in TYPE and its
17156 containing scopes. */
17161 for (scope = TREE_TYPE (type);
17162 scope && TREE_CODE (scope) != NAMESPACE_DECL;
17163 scope = (TYPE_P (scope)
17164 ? TYPE_CONTEXT (scope)
17165 : DECL_CONTEXT (scope)))
17167 && CLASS_TYPE_P (scope)
17168 && CLASSTYPE_TEMPLATE_INFO (scope)
17169 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17170 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17174 /* Otherwise, the identifier is optional. */
17177 /* We don't know whether what comes next is a template-id,
17178 an identifier, or nothing at all. */
17179 cp_parser_parse_tentatively (parser);
17180 /* Check for a template-id. */
17181 type_start_token = cp_lexer_peek_token (parser->lexer);
17182 id = cp_parser_template_id (parser,
17183 /*template_keyword_p=*/false,
17184 /*check_dependency_p=*/true,
17185 /*is_declaration=*/true);
17186 /* If that didn't work, it could still be an identifier. */
17187 if (!cp_parser_parse_definitely (parser))
17189 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17191 type_start_token = cp_lexer_peek_token (parser->lexer);
17192 id = cp_parser_identifier (parser);
17199 template_id_p = true;
17204 pop_deferring_access_checks ();
17207 cp_parser_check_for_invalid_template_id (parser, id,
17208 type_start_token->location);
17210 /* If it's not a `:' or a `{' then we can't really be looking at a
17211 class-head, since a class-head only appears as part of a
17212 class-specifier. We have to detect this situation before calling
17213 xref_tag, since that has irreversible side-effects. */
17214 if (!cp_parser_next_token_starts_class_definition_p (parser))
17216 cp_parser_error (parser, "expected %<{%> or %<:%>");
17217 type = error_mark_node;
17221 /* At this point, we're going ahead with the class-specifier, even
17222 if some other problem occurs. */
17223 cp_parser_commit_to_tentative_parse (parser);
17224 /* Issue the error about the overly-qualified name now. */
17227 cp_parser_error (parser,
17228 "global qualification of class name is invalid");
17229 type = error_mark_node;
17232 else if (invalid_nested_name_p)
17234 cp_parser_error (parser,
17235 "qualified name does not name a class");
17236 type = error_mark_node;
17239 else if (nested_name_specifier)
17243 /* Reject typedef-names in class heads. */
17244 if (!DECL_IMPLICIT_TYPEDEF_P (type))
17246 error_at (type_start_token->location,
17247 "invalid class name in declaration of %qD",
17253 /* Figure out in what scope the declaration is being placed. */
17254 scope = current_scope ();
17255 /* If that scope does not contain the scope in which the
17256 class was originally declared, the program is invalid. */
17257 if (scope && !is_ancestor (scope, nested_name_specifier))
17259 if (at_namespace_scope_p ())
17260 error_at (type_start_token->location,
17261 "declaration of %qD in namespace %qD which does not "
17263 type, scope, nested_name_specifier);
17265 error_at (type_start_token->location,
17266 "declaration of %qD in %qD which does not enclose %qD",
17267 type, scope, nested_name_specifier);
17273 A declarator-id shall not be qualified except for the
17274 definition of a ... nested class outside of its class
17275 ... [or] the definition or explicit instantiation of a
17276 class member of a namespace outside of its namespace. */
17277 if (scope == nested_name_specifier)
17279 permerror (nested_name_specifier_token_start->location,
17280 "extra qualification not allowed");
17281 nested_name_specifier = NULL_TREE;
17285 /* An explicit-specialization must be preceded by "template <>". If
17286 it is not, try to recover gracefully. */
17287 if (at_namespace_scope_p ()
17288 && parser->num_template_parameter_lists == 0
17291 error_at (type_start_token->location,
17292 "an explicit specialization must be preceded by %<template <>%>");
17293 invalid_explicit_specialization_p = true;
17294 /* Take the same action that would have been taken by
17295 cp_parser_explicit_specialization. */
17296 ++parser->num_template_parameter_lists;
17297 begin_specialization ();
17299 /* There must be no "return" statements between this point and the
17300 end of this function; set "type "to the correct return value and
17301 use "goto done;" to return. */
17302 /* Make sure that the right number of template parameters were
17304 if (!cp_parser_check_template_parameters (parser, num_templates,
17305 type_start_token->location,
17306 /*declarator=*/NULL))
17308 /* If something went wrong, there is no point in even trying to
17309 process the class-definition. */
17314 /* Look up the type. */
17317 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17318 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17319 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17321 error_at (type_start_token->location,
17322 "function template %qD redeclared as a class template", id);
17323 type = error_mark_node;
17327 type = TREE_TYPE (id);
17328 type = maybe_process_partial_specialization (type);
17330 if (nested_name_specifier)
17331 pushed_scope = push_scope (nested_name_specifier);
17333 else if (nested_name_specifier)
17339 template <typename T> struct S { struct T };
17340 template <typename T> struct S<T>::T { };
17342 we will get a TYPENAME_TYPE when processing the definition of
17343 `S::T'. We need to resolve it to the actual type before we
17344 try to define it. */
17345 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17347 class_type = resolve_typename_type (TREE_TYPE (type),
17348 /*only_current_p=*/false);
17349 if (TREE_CODE (class_type) != TYPENAME_TYPE)
17350 type = TYPE_NAME (class_type);
17353 cp_parser_error (parser, "could not resolve typename type");
17354 type = error_mark_node;
17358 if (maybe_process_partial_specialization (TREE_TYPE (type))
17359 == error_mark_node)
17365 class_type = current_class_type;
17366 /* Enter the scope indicated by the nested-name-specifier. */
17367 pushed_scope = push_scope (nested_name_specifier);
17368 /* Get the canonical version of this type. */
17369 type = TYPE_MAIN_DECL (TREE_TYPE (type));
17370 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17371 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17373 type = push_template_decl (type);
17374 if (type == error_mark_node)
17381 type = TREE_TYPE (type);
17382 *nested_name_specifier_p = true;
17384 else /* The name is not a nested name. */
17386 /* If the class was unnamed, create a dummy name. */
17388 id = make_anon_name ();
17389 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17390 parser->num_template_parameter_lists);
17393 /* Indicate whether this class was declared as a `class' or as a
17395 if (TREE_CODE (type) == RECORD_TYPE)
17396 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17397 cp_parser_check_class_key (class_key, type);
17399 /* If this type was already complete, and we see another definition,
17400 that's an error. */
17401 if (type != error_mark_node && COMPLETE_TYPE_P (type))
17403 error_at (type_start_token->location, "redefinition of %q#T",
17405 error_at (type_start_token->location, "previous definition of %q+#T",
17410 else if (type == error_mark_node)
17413 /* We will have entered the scope containing the class; the names of
17414 base classes should be looked up in that context. For example:
17416 struct A { struct B {}; struct C; };
17417 struct A::C : B {};
17421 /* Get the list of base-classes, if there is one. */
17422 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17423 *bases = cp_parser_base_clause (parser);
17426 /* Leave the scope given by the nested-name-specifier. We will
17427 enter the class scope itself while processing the members. */
17429 pop_scope (pushed_scope);
17431 if (invalid_explicit_specialization_p)
17433 end_specialization ();
17434 --parser->num_template_parameter_lists;
17438 DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17439 *attributes_p = attributes;
17441 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17445 /* Parse a class-key.
17452 Returns the kind of class-key specified, or none_type to indicate
17455 static enum tag_types
17456 cp_parser_class_key (cp_parser* parser)
17459 enum tag_types tag_type;
17461 /* Look for the class-key. */
17462 token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17466 /* Check to see if the TOKEN is a class-key. */
17467 tag_type = cp_parser_token_is_class_key (token);
17469 cp_parser_error (parser, "expected class-key");
17473 /* Parse an (optional) member-specification.
17475 member-specification:
17476 member-declaration member-specification [opt]
17477 access-specifier : member-specification [opt] */
17480 cp_parser_member_specification_opt (cp_parser* parser)
17487 /* Peek at the next token. */
17488 token = cp_lexer_peek_token (parser->lexer);
17489 /* If it's a `}', or EOF then we've seen all the members. */
17490 if (token->type == CPP_CLOSE_BRACE
17491 || token->type == CPP_EOF
17492 || token->type == CPP_PRAGMA_EOL)
17495 /* See if this token is a keyword. */
17496 keyword = token->keyword;
17500 case RID_PROTECTED:
17502 /* Consume the access-specifier. */
17503 cp_lexer_consume_token (parser->lexer);
17504 /* Remember which access-specifier is active. */
17505 current_access_specifier = token->u.value;
17506 /* Look for the `:'. */
17507 cp_parser_require (parser, CPP_COLON, RT_COLON);
17511 /* Accept #pragmas at class scope. */
17512 if (token->type == CPP_PRAGMA)
17514 cp_parser_pragma (parser, pragma_external);
17518 /* Otherwise, the next construction must be a
17519 member-declaration. */
17520 cp_parser_member_declaration (parser);
17525 /* Parse a member-declaration.
17527 member-declaration:
17528 decl-specifier-seq [opt] member-declarator-list [opt] ;
17529 function-definition ; [opt]
17530 :: [opt] nested-name-specifier template [opt] unqualified-id ;
17532 template-declaration
17534 member-declarator-list:
17536 member-declarator-list , member-declarator
17539 declarator pure-specifier [opt]
17540 declarator constant-initializer [opt]
17541 identifier [opt] : constant-expression
17545 member-declaration:
17546 __extension__ member-declaration
17549 declarator attributes [opt] pure-specifier [opt]
17550 declarator attributes [opt] constant-initializer [opt]
17551 identifier [opt] attributes [opt] : constant-expression
17555 member-declaration:
17556 static_assert-declaration */
17559 cp_parser_member_declaration (cp_parser* parser)
17561 cp_decl_specifier_seq decl_specifiers;
17562 tree prefix_attributes;
17564 int declares_class_or_enum;
17566 cp_token *token = NULL;
17567 cp_token *decl_spec_token_start = NULL;
17568 cp_token *initializer_token_start = NULL;
17569 int saved_pedantic;
17570 bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17572 /* Check for the `__extension__' keyword. */
17573 if (cp_parser_extension_opt (parser, &saved_pedantic))
17576 cp_parser_member_declaration (parser);
17577 /* Restore the old value of the PEDANTIC flag. */
17578 pedantic = saved_pedantic;
17583 /* Check for a template-declaration. */
17584 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17586 /* An explicit specialization here is an error condition, and we
17587 expect the specialization handler to detect and report this. */
17588 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17589 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17590 cp_parser_explicit_specialization (parser);
17592 cp_parser_template_declaration (parser, /*member_p=*/true);
17597 /* Check for a using-declaration. */
17598 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17600 /* Parse the using-declaration. */
17601 cp_parser_using_declaration (parser,
17602 /*access_declaration_p=*/false);
17606 /* Check for @defs. */
17607 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17610 tree ivar_chains = cp_parser_objc_defs_expression (parser);
17611 ivar = ivar_chains;
17615 ivar = TREE_CHAIN (member);
17616 TREE_CHAIN (member) = NULL_TREE;
17617 finish_member_declaration (member);
17622 /* If the next token is `static_assert' we have a static assertion. */
17623 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17625 cp_parser_static_assert (parser, /*member_p=*/true);
17629 parser->colon_corrects_to_scope_p = false;
17631 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17634 /* Parse the decl-specifier-seq. */
17635 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17636 cp_parser_decl_specifier_seq (parser,
17637 CP_PARSER_FLAGS_OPTIONAL,
17639 &declares_class_or_enum);
17640 prefix_attributes = decl_specifiers.attributes;
17641 decl_specifiers.attributes = NULL_TREE;
17642 /* Check for an invalid type-name. */
17643 if (!decl_specifiers.any_type_specifiers_p
17644 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17646 /* If there is no declarator, then the decl-specifier-seq should
17648 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17650 /* If there was no decl-specifier-seq, and the next token is a
17651 `;', then we have something like:
17657 Each member-declaration shall declare at least one member
17658 name of the class. */
17659 if (!decl_specifiers.any_specifiers_p)
17661 cp_token *token = cp_lexer_peek_token (parser->lexer);
17662 if (!in_system_header_at (token->location))
17663 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17669 /* See if this declaration is a friend. */
17670 friend_p = cp_parser_friend_p (&decl_specifiers);
17671 /* If there were decl-specifiers, check to see if there was
17672 a class-declaration. */
17673 type = check_tag_decl (&decl_specifiers);
17674 /* Nested classes have already been added to the class, but
17675 a `friend' needs to be explicitly registered. */
17678 /* If the `friend' keyword was present, the friend must
17679 be introduced with a class-key. */
17680 if (!declares_class_or_enum)
17681 error_at (decl_spec_token_start->location,
17682 "a class-key must be used when declaring a friend");
17685 template <typename T> struct A {
17686 friend struct A<T>::B;
17689 A<T>::B will be represented by a TYPENAME_TYPE, and
17690 therefore not recognized by check_tag_decl. */
17692 && decl_specifiers.type
17693 && TYPE_P (decl_specifiers.type))
17694 type = decl_specifiers.type;
17695 if (!type || !TYPE_P (type))
17696 error_at (decl_spec_token_start->location,
17697 "friend declaration does not name a class or "
17700 make_friend_class (current_class_type, type,
17701 /*complain=*/true);
17703 /* If there is no TYPE, an error message will already have
17705 else if (!type || type == error_mark_node)
17707 /* An anonymous aggregate has to be handled specially; such
17708 a declaration really declares a data member (with a
17709 particular type), as opposed to a nested class. */
17710 else if (ANON_AGGR_TYPE_P (type))
17712 /* Remove constructors and such from TYPE, now that we
17713 know it is an anonymous aggregate. */
17714 fixup_anonymous_aggr (type);
17715 /* And make the corresponding data member. */
17716 decl = build_decl (decl_spec_token_start->location,
17717 FIELD_DECL, NULL_TREE, type);
17718 /* Add it to the class. */
17719 finish_member_declaration (decl);
17722 cp_parser_check_access_in_redeclaration
17724 decl_spec_token_start->location);
17729 bool assume_semicolon = false;
17731 /* See if these declarations will be friends. */
17732 friend_p = cp_parser_friend_p (&decl_specifiers);
17734 /* Keep going until we hit the `;' at the end of the
17736 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17738 tree attributes = NULL_TREE;
17739 tree first_attribute;
17741 /* Peek at the next token. */
17742 token = cp_lexer_peek_token (parser->lexer);
17744 /* Check for a bitfield declaration. */
17745 if (token->type == CPP_COLON
17746 || (token->type == CPP_NAME
17747 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17753 /* Get the name of the bitfield. Note that we cannot just
17754 check TOKEN here because it may have been invalidated by
17755 the call to cp_lexer_peek_nth_token above. */
17756 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17757 identifier = cp_parser_identifier (parser);
17759 identifier = NULL_TREE;
17761 /* Consume the `:' token. */
17762 cp_lexer_consume_token (parser->lexer);
17763 /* Get the width of the bitfield. */
17765 = cp_parser_constant_expression (parser,
17766 /*allow_non_constant=*/false,
17769 /* Look for attributes that apply to the bitfield. */
17770 attributes = cp_parser_attributes_opt (parser);
17771 /* Remember which attributes are prefix attributes and
17773 first_attribute = attributes;
17774 /* Combine the attributes. */
17775 attributes = chainon (prefix_attributes, attributes);
17777 /* Create the bitfield declaration. */
17778 decl = grokbitfield (identifier
17779 ? make_id_declarator (NULL_TREE,
17789 cp_declarator *declarator;
17791 tree asm_specification;
17792 int ctor_dtor_or_conv_p;
17794 /* Parse the declarator. */
17796 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17797 &ctor_dtor_or_conv_p,
17798 /*parenthesized_p=*/NULL,
17799 /*member_p=*/true);
17801 /* If something went wrong parsing the declarator, make sure
17802 that we at least consume some tokens. */
17803 if (declarator == cp_error_declarator)
17805 /* Skip to the end of the statement. */
17806 cp_parser_skip_to_end_of_statement (parser);
17807 /* If the next token is not a semicolon, that is
17808 probably because we just skipped over the body of
17809 a function. So, we consume a semicolon if
17810 present, but do not issue an error message if it
17812 if (cp_lexer_next_token_is (parser->lexer,
17814 cp_lexer_consume_token (parser->lexer);
17818 if (declares_class_or_enum & 2)
17819 cp_parser_check_for_definition_in_return_type
17820 (declarator, decl_specifiers.type,
17821 decl_specifiers.type_location);
17823 /* Look for an asm-specification. */
17824 asm_specification = cp_parser_asm_specification_opt (parser);
17825 /* Look for attributes that apply to the declaration. */
17826 attributes = cp_parser_attributes_opt (parser);
17827 /* Remember which attributes are prefix attributes and
17829 first_attribute = attributes;
17830 /* Combine the attributes. */
17831 attributes = chainon (prefix_attributes, attributes);
17833 /* If it's an `=', then we have a constant-initializer or a
17834 pure-specifier. It is not correct to parse the
17835 initializer before registering the member declaration
17836 since the member declaration should be in scope while
17837 its initializer is processed. However, the rest of the
17838 front end does not yet provide an interface that allows
17839 us to handle this correctly. */
17840 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17844 A pure-specifier shall be used only in the declaration of
17845 a virtual function.
17847 A member-declarator can contain a constant-initializer
17848 only if it declares a static member of integral or
17851 Therefore, if the DECLARATOR is for a function, we look
17852 for a pure-specifier; otherwise, we look for a
17853 constant-initializer. When we call `grokfield', it will
17854 perform more stringent semantics checks. */
17855 initializer_token_start = cp_lexer_peek_token (parser->lexer);
17856 if (function_declarator_p (declarator))
17857 initializer = cp_parser_pure_specifier (parser);
17859 /* Parse the initializer. */
17860 initializer = cp_parser_constant_initializer (parser);
17862 /* Otherwise, there is no initializer. */
17864 initializer = NULL_TREE;
17866 /* See if we are probably looking at a function
17867 definition. We are certainly not looking at a
17868 member-declarator. Calling `grokfield' has
17869 side-effects, so we must not do it unless we are sure
17870 that we are looking at a member-declarator. */
17871 if (cp_parser_token_starts_function_definition_p
17872 (cp_lexer_peek_token (parser->lexer)))
17874 /* The grammar does not allow a pure-specifier to be
17875 used when a member function is defined. (It is
17876 possible that this fact is an oversight in the
17877 standard, since a pure function may be defined
17878 outside of the class-specifier. */
17880 error_at (initializer_token_start->location,
17881 "pure-specifier on function-definition");
17882 decl = cp_parser_save_member_function_body (parser,
17886 /* If the member was not a friend, declare it here. */
17888 finish_member_declaration (decl);
17889 /* Peek at the next token. */
17890 token = cp_lexer_peek_token (parser->lexer);
17891 /* If the next token is a semicolon, consume it. */
17892 if (token->type == CPP_SEMICOLON)
17893 cp_lexer_consume_token (parser->lexer);
17897 if (declarator->kind == cdk_function)
17898 declarator->id_loc = token->location;
17899 /* Create the declaration. */
17900 decl = grokfield (declarator, &decl_specifiers,
17901 initializer, /*init_const_expr_p=*/true,
17906 /* Reset PREFIX_ATTRIBUTES. */
17907 while (attributes && TREE_CHAIN (attributes) != first_attribute)
17908 attributes = TREE_CHAIN (attributes);
17910 TREE_CHAIN (attributes) = NULL_TREE;
17912 /* If there is any qualification still in effect, clear it
17913 now; we will be starting fresh with the next declarator. */
17914 parser->scope = NULL_TREE;
17915 parser->qualifying_scope = NULL_TREE;
17916 parser->object_scope = NULL_TREE;
17917 /* If it's a `,', then there are more declarators. */
17918 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17919 cp_lexer_consume_token (parser->lexer);
17920 /* If the next token isn't a `;', then we have a parse error. */
17921 else if (cp_lexer_next_token_is_not (parser->lexer,
17924 /* The next token might be a ways away from where the
17925 actual semicolon is missing. Find the previous token
17926 and use that for our error position. */
17927 cp_token *token = cp_lexer_previous_token (parser->lexer);
17928 error_at (token->location,
17929 "expected %<;%> at end of member declaration");
17931 /* Assume that the user meant to provide a semicolon. If
17932 we were to cp_parser_skip_to_end_of_statement, we might
17933 skip to a semicolon inside a member function definition
17934 and issue nonsensical error messages. */
17935 assume_semicolon = true;
17940 /* Add DECL to the list of members. */
17942 finish_member_declaration (decl);
17944 if (TREE_CODE (decl) == FUNCTION_DECL)
17945 cp_parser_save_default_args (parser, decl);
17948 if (assume_semicolon)
17953 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
17955 parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17958 /* Parse a pure-specifier.
17963 Returns INTEGER_ZERO_NODE if a pure specifier is found.
17964 Otherwise, ERROR_MARK_NODE is returned. */
17967 cp_parser_pure_specifier (cp_parser* parser)
17971 /* Look for the `=' token. */
17972 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
17973 return error_mark_node;
17974 /* Look for the `0' token. */
17975 token = cp_lexer_peek_token (parser->lexer);
17977 if (token->type == CPP_EOF
17978 || token->type == CPP_PRAGMA_EOL)
17979 return error_mark_node;
17981 cp_lexer_consume_token (parser->lexer);
17983 /* Accept = default or = delete in c++0x mode. */
17984 if (token->keyword == RID_DEFAULT
17985 || token->keyword == RID_DELETE)
17987 maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
17988 return token->u.value;
17991 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
17992 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
17994 cp_parser_error (parser,
17995 "invalid pure specifier (only %<= 0%> is allowed)");
17996 cp_parser_skip_to_end_of_statement (parser);
17997 return error_mark_node;
17999 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18001 error_at (token->location, "templates may not be %<virtual%>");
18002 return error_mark_node;
18005 return integer_zero_node;
18008 /* Parse a constant-initializer.
18010 constant-initializer:
18011 = constant-expression
18013 Returns a representation of the constant-expression. */
18016 cp_parser_constant_initializer (cp_parser* parser)
18018 /* Look for the `=' token. */
18019 if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18020 return error_mark_node;
18022 /* It is invalid to write:
18024 struct S { static const int i = { 7 }; };
18027 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18029 cp_parser_error (parser,
18030 "a brace-enclosed initializer is not allowed here");
18031 /* Consume the opening brace. */
18032 cp_lexer_consume_token (parser->lexer);
18033 /* Skip the initializer. */
18034 cp_parser_skip_to_closing_brace (parser);
18035 /* Look for the trailing `}'. */
18036 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18038 return error_mark_node;
18041 return cp_parser_constant_expression (parser,
18042 /*allow_non_constant=*/false,
18046 /* Derived classes [gram.class.derived] */
18048 /* Parse a base-clause.
18051 : base-specifier-list
18053 base-specifier-list:
18054 base-specifier ... [opt]
18055 base-specifier-list , base-specifier ... [opt]
18057 Returns a TREE_LIST representing the base-classes, in the order in
18058 which they were declared. The representation of each node is as
18059 described by cp_parser_base_specifier.
18061 In the case that no bases are specified, this function will return
18062 NULL_TREE, not ERROR_MARK_NODE. */
18065 cp_parser_base_clause (cp_parser* parser)
18067 tree bases = NULL_TREE;
18069 /* Look for the `:' that begins the list. */
18070 cp_parser_require (parser, CPP_COLON, RT_COLON);
18072 /* Scan the base-specifier-list. */
18077 bool pack_expansion_p = false;
18079 /* Look for the base-specifier. */
18080 base = cp_parser_base_specifier (parser);
18081 /* Look for the (optional) ellipsis. */
18082 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18084 /* Consume the `...'. */
18085 cp_lexer_consume_token (parser->lexer);
18087 pack_expansion_p = true;
18090 /* Add BASE to the front of the list. */
18091 if (base != error_mark_node)
18093 if (pack_expansion_p)
18094 /* Make this a pack expansion type. */
18095 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18098 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18100 TREE_CHAIN (base) = bases;
18104 /* Peek at the next token. */
18105 token = cp_lexer_peek_token (parser->lexer);
18106 /* If it's not a comma, then the list is complete. */
18107 if (token->type != CPP_COMMA)
18109 /* Consume the `,'. */
18110 cp_lexer_consume_token (parser->lexer);
18113 /* PARSER->SCOPE may still be non-NULL at this point, if the last
18114 base class had a qualified name. However, the next name that
18115 appears is certainly not qualified. */
18116 parser->scope = NULL_TREE;
18117 parser->qualifying_scope = NULL_TREE;
18118 parser->object_scope = NULL_TREE;
18120 return nreverse (bases);
18123 /* Parse a base-specifier.
18126 :: [opt] nested-name-specifier [opt] class-name
18127 virtual access-specifier [opt] :: [opt] nested-name-specifier
18129 access-specifier virtual [opt] :: [opt] nested-name-specifier
18132 Returns a TREE_LIST. The TREE_PURPOSE will be one of
18133 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18134 indicate the specifiers provided. The TREE_VALUE will be a TYPE
18135 (or the ERROR_MARK_NODE) indicating the type that was specified. */
18138 cp_parser_base_specifier (cp_parser* parser)
18142 bool virtual_p = false;
18143 bool duplicate_virtual_error_issued_p = false;
18144 bool duplicate_access_error_issued_p = false;
18145 bool class_scope_p, template_p;
18146 tree access = access_default_node;
18149 /* Process the optional `virtual' and `access-specifier'. */
18152 /* Peek at the next token. */
18153 token = cp_lexer_peek_token (parser->lexer);
18154 /* Process `virtual'. */
18155 switch (token->keyword)
18158 /* If `virtual' appears more than once, issue an error. */
18159 if (virtual_p && !duplicate_virtual_error_issued_p)
18161 cp_parser_error (parser,
18162 "%<virtual%> specified more than once in base-specified");
18163 duplicate_virtual_error_issued_p = true;
18168 /* Consume the `virtual' token. */
18169 cp_lexer_consume_token (parser->lexer);
18174 case RID_PROTECTED:
18176 /* If more than one access specifier appears, issue an
18178 if (access != access_default_node
18179 && !duplicate_access_error_issued_p)
18181 cp_parser_error (parser,
18182 "more than one access specifier in base-specified");
18183 duplicate_access_error_issued_p = true;
18186 access = ridpointers[(int) token->keyword];
18188 /* Consume the access-specifier. */
18189 cp_lexer_consume_token (parser->lexer);
18198 /* It is not uncommon to see programs mechanically, erroneously, use
18199 the 'typename' keyword to denote (dependent) qualified types
18200 as base classes. */
18201 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18203 token = cp_lexer_peek_token (parser->lexer);
18204 if (!processing_template_decl)
18205 error_at (token->location,
18206 "keyword %<typename%> not allowed outside of templates");
18208 error_at (token->location,
18209 "keyword %<typename%> not allowed in this context "
18210 "(the base class is implicitly a type)");
18211 cp_lexer_consume_token (parser->lexer);
18214 /* Look for the optional `::' operator. */
18215 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18216 /* Look for the nested-name-specifier. The simplest way to
18221 The keyword `typename' is not permitted in a base-specifier or
18222 mem-initializer; in these contexts a qualified name that
18223 depends on a template-parameter is implicitly assumed to be a
18226 is to pretend that we have seen the `typename' keyword at this
18228 cp_parser_nested_name_specifier_opt (parser,
18229 /*typename_keyword_p=*/true,
18230 /*check_dependency_p=*/true,
18232 /*is_declaration=*/true);
18233 /* If the base class is given by a qualified name, assume that names
18234 we see are type names or templates, as appropriate. */
18235 class_scope_p = (parser->scope && TYPE_P (parser->scope));
18236 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18238 /* Finally, look for the class-name. */
18239 type = cp_parser_class_name (parser,
18243 /*check_dependency_p=*/true,
18244 /*class_head_p=*/false,
18245 /*is_declaration=*/true);
18247 if (type == error_mark_node)
18248 return error_mark_node;
18250 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18253 /* Exception handling [gram.exception] */
18255 /* Parse an (optional) exception-specification.
18257 exception-specification:
18258 throw ( type-id-list [opt] )
18260 Returns a TREE_LIST representing the exception-specification. The
18261 TREE_VALUE of each node is a type. */
18264 cp_parser_exception_specification_opt (cp_parser* parser)
18268 const char *saved_message;
18270 /* Peek at the next token. */
18271 token = cp_lexer_peek_token (parser->lexer);
18273 /* Is it a noexcept-specification? */
18274 if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18277 cp_lexer_consume_token (parser->lexer);
18279 if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18281 cp_lexer_consume_token (parser->lexer);
18283 /* Types may not be defined in an exception-specification. */
18284 saved_message = parser->type_definition_forbidden_message;
18285 parser->type_definition_forbidden_message
18286 = G_("types may not be defined in an exception-specification");
18288 expr = cp_parser_constant_expression (parser, false, NULL);
18290 /* Restore the saved message. */
18291 parser->type_definition_forbidden_message = saved_message;
18293 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18296 expr = boolean_true_node;
18298 return build_noexcept_spec (expr, tf_warning_or_error);
18301 /* If it's not `throw', then there's no exception-specification. */
18302 if (!cp_parser_is_keyword (token, RID_THROW))
18306 /* Enable this once a lot of code has transitioned to noexcept? */
18307 if (cxx_dialect == cxx0x && !in_system_header)
18308 warning (OPT_Wdeprecated, "dynamic exception specifications are "
18309 "deprecated in C++0x; use %<noexcept%> instead");
18312 /* Consume the `throw'. */
18313 cp_lexer_consume_token (parser->lexer);
18315 /* Look for the `('. */
18316 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18318 /* Peek at the next token. */
18319 token = cp_lexer_peek_token (parser->lexer);
18320 /* If it's not a `)', then there is a type-id-list. */
18321 if (token->type != CPP_CLOSE_PAREN)
18323 /* Types may not be defined in an exception-specification. */
18324 saved_message = parser->type_definition_forbidden_message;
18325 parser->type_definition_forbidden_message
18326 = G_("types may not be defined in an exception-specification");
18327 /* Parse the type-id-list. */
18328 type_id_list = cp_parser_type_id_list (parser);
18329 /* Restore the saved message. */
18330 parser->type_definition_forbidden_message = saved_message;
18333 type_id_list = empty_except_spec;
18335 /* Look for the `)'. */
18336 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18338 return type_id_list;
18341 /* Parse an (optional) type-id-list.
18345 type-id-list , type-id ... [opt]
18347 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
18348 in the order that the types were presented. */
18351 cp_parser_type_id_list (cp_parser* parser)
18353 tree types = NULL_TREE;
18360 /* Get the next type-id. */
18361 type = cp_parser_type_id (parser);
18362 /* Parse the optional ellipsis. */
18363 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18365 /* Consume the `...'. */
18366 cp_lexer_consume_token (parser->lexer);
18368 /* Turn the type into a pack expansion expression. */
18369 type = make_pack_expansion (type);
18371 /* Add it to the list. */
18372 types = add_exception_specifier (types, type, /*complain=*/1);
18373 /* Peek at the next token. */
18374 token = cp_lexer_peek_token (parser->lexer);
18375 /* If it is not a `,', we are done. */
18376 if (token->type != CPP_COMMA)
18378 /* Consume the `,'. */
18379 cp_lexer_consume_token (parser->lexer);
18382 return nreverse (types);
18385 /* Parse a try-block.
18388 try compound-statement handler-seq */
18391 cp_parser_try_block (cp_parser* parser)
18395 cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18396 try_block = begin_try_block ();
18397 cp_parser_compound_statement (parser, NULL, true, false);
18398 finish_try_block (try_block);
18399 cp_parser_handler_seq (parser);
18400 finish_handler_sequence (try_block);
18405 /* Parse a function-try-block.
18407 function-try-block:
18408 try ctor-initializer [opt] function-body handler-seq */
18411 cp_parser_function_try_block (cp_parser* parser)
18413 tree compound_stmt;
18415 bool ctor_initializer_p;
18417 /* Look for the `try' keyword. */
18418 if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18420 /* Let the rest of the front end know where we are. */
18421 try_block = begin_function_try_block (&compound_stmt);
18422 /* Parse the function-body. */
18424 = cp_parser_ctor_initializer_opt_and_function_body (parser);
18425 /* We're done with the `try' part. */
18426 finish_function_try_block (try_block);
18427 /* Parse the handlers. */
18428 cp_parser_handler_seq (parser);
18429 /* We're done with the handlers. */
18430 finish_function_handler_sequence (try_block, compound_stmt);
18432 return ctor_initializer_p;
18435 /* Parse a handler-seq.
18438 handler handler-seq [opt] */
18441 cp_parser_handler_seq (cp_parser* parser)
18447 /* Parse the handler. */
18448 cp_parser_handler (parser);
18449 /* Peek at the next token. */
18450 token = cp_lexer_peek_token (parser->lexer);
18451 /* If it's not `catch' then there are no more handlers. */
18452 if (!cp_parser_is_keyword (token, RID_CATCH))
18457 /* Parse a handler.
18460 catch ( exception-declaration ) compound-statement */
18463 cp_parser_handler (cp_parser* parser)
18468 cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18469 handler = begin_handler ();
18470 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18471 declaration = cp_parser_exception_declaration (parser);
18472 finish_handler_parms (declaration, handler);
18473 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18474 cp_parser_compound_statement (parser, NULL, false, false);
18475 finish_handler (handler);
18478 /* Parse an exception-declaration.
18480 exception-declaration:
18481 type-specifier-seq declarator
18482 type-specifier-seq abstract-declarator
18486 Returns a VAR_DECL for the declaration, or NULL_TREE if the
18487 ellipsis variant is used. */
18490 cp_parser_exception_declaration (cp_parser* parser)
18492 cp_decl_specifier_seq type_specifiers;
18493 cp_declarator *declarator;
18494 const char *saved_message;
18496 /* If it's an ellipsis, it's easy to handle. */
18497 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18499 /* Consume the `...' token. */
18500 cp_lexer_consume_token (parser->lexer);
18504 /* Types may not be defined in exception-declarations. */
18505 saved_message = parser->type_definition_forbidden_message;
18506 parser->type_definition_forbidden_message
18507 = G_("types may not be defined in exception-declarations");
18509 /* Parse the type-specifier-seq. */
18510 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18511 /*is_trailing_return=*/false,
18513 /* If it's a `)', then there is no declarator. */
18514 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18517 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18518 /*ctor_dtor_or_conv_p=*/NULL,
18519 /*parenthesized_p=*/NULL,
18520 /*member_p=*/false);
18522 /* Restore the saved message. */
18523 parser->type_definition_forbidden_message = saved_message;
18525 if (!type_specifiers.any_specifiers_p)
18526 return error_mark_node;
18528 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18531 /* Parse a throw-expression.
18534 throw assignment-expression [opt]
18536 Returns a THROW_EXPR representing the throw-expression. */
18539 cp_parser_throw_expression (cp_parser* parser)
18544 cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18545 token = cp_lexer_peek_token (parser->lexer);
18546 /* Figure out whether or not there is an assignment-expression
18547 following the "throw" keyword. */
18548 if (token->type == CPP_COMMA
18549 || token->type == CPP_SEMICOLON
18550 || token->type == CPP_CLOSE_PAREN
18551 || token->type == CPP_CLOSE_SQUARE
18552 || token->type == CPP_CLOSE_BRACE
18553 || token->type == CPP_COLON)
18554 expression = NULL_TREE;
18556 expression = cp_parser_assignment_expression (parser,
18557 /*cast_p=*/false, NULL);
18559 return build_throw (expression);
18562 /* GNU Extensions */
18564 /* Parse an (optional) asm-specification.
18567 asm ( string-literal )
18569 If the asm-specification is present, returns a STRING_CST
18570 corresponding to the string-literal. Otherwise, returns
18574 cp_parser_asm_specification_opt (cp_parser* parser)
18577 tree asm_specification;
18579 /* Peek at the next token. */
18580 token = cp_lexer_peek_token (parser->lexer);
18581 /* If the next token isn't the `asm' keyword, then there's no
18582 asm-specification. */
18583 if (!cp_parser_is_keyword (token, RID_ASM))
18586 /* Consume the `asm' token. */
18587 cp_lexer_consume_token (parser->lexer);
18588 /* Look for the `('. */
18589 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18591 /* Look for the string-literal. */
18592 asm_specification = cp_parser_string_literal (parser, false, false);
18594 /* Look for the `)'. */
18595 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18597 return asm_specification;
18600 /* Parse an asm-operand-list.
18604 asm-operand-list , asm-operand
18607 string-literal ( expression )
18608 [ string-literal ] string-literal ( expression )
18610 Returns a TREE_LIST representing the operands. The TREE_VALUE of
18611 each node is the expression. The TREE_PURPOSE is itself a
18612 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18613 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18614 is a STRING_CST for the string literal before the parenthesis. Returns
18615 ERROR_MARK_NODE if any of the operands are invalid. */
18618 cp_parser_asm_operand_list (cp_parser* parser)
18620 tree asm_operands = NULL_TREE;
18621 bool invalid_operands = false;
18625 tree string_literal;
18629 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18631 /* Consume the `[' token. */
18632 cp_lexer_consume_token (parser->lexer);
18633 /* Read the operand name. */
18634 name = cp_parser_identifier (parser);
18635 if (name != error_mark_node)
18636 name = build_string (IDENTIFIER_LENGTH (name),
18637 IDENTIFIER_POINTER (name));
18638 /* Look for the closing `]'. */
18639 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18643 /* Look for the string-literal. */
18644 string_literal = cp_parser_string_literal (parser, false, false);
18646 /* Look for the `('. */
18647 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18648 /* Parse the expression. */
18649 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18650 /* Look for the `)'. */
18651 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18653 if (name == error_mark_node
18654 || string_literal == error_mark_node
18655 || expression == error_mark_node)
18656 invalid_operands = true;
18658 /* Add this operand to the list. */
18659 asm_operands = tree_cons (build_tree_list (name, string_literal),
18662 /* If the next token is not a `,', there are no more
18664 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18666 /* Consume the `,'. */
18667 cp_lexer_consume_token (parser->lexer);
18670 return invalid_operands ? error_mark_node : nreverse (asm_operands);
18673 /* Parse an asm-clobber-list.
18677 asm-clobber-list , string-literal
18679 Returns a TREE_LIST, indicating the clobbers in the order that they
18680 appeared. The TREE_VALUE of each node is a STRING_CST. */
18683 cp_parser_asm_clobber_list (cp_parser* parser)
18685 tree clobbers = NULL_TREE;
18689 tree string_literal;
18691 /* Look for the string literal. */
18692 string_literal = cp_parser_string_literal (parser, false, false);
18693 /* Add it to the list. */
18694 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18695 /* If the next token is not a `,', then the list is
18697 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18699 /* Consume the `,' token. */
18700 cp_lexer_consume_token (parser->lexer);
18706 /* Parse an asm-label-list.
18710 asm-label-list , identifier
18712 Returns a TREE_LIST, indicating the labels in the order that they
18713 appeared. The TREE_VALUE of each node is a label. */
18716 cp_parser_asm_label_list (cp_parser* parser)
18718 tree labels = NULL_TREE;
18722 tree identifier, label, name;
18724 /* Look for the identifier. */
18725 identifier = cp_parser_identifier (parser);
18726 if (!error_operand_p (identifier))
18728 label = lookup_label (identifier);
18729 if (TREE_CODE (label) == LABEL_DECL)
18731 TREE_USED (label) = 1;
18732 check_goto (label);
18733 name = build_string (IDENTIFIER_LENGTH (identifier),
18734 IDENTIFIER_POINTER (identifier));
18735 labels = tree_cons (name, label, labels);
18738 /* If the next token is not a `,', then the list is
18740 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18742 /* Consume the `,' token. */
18743 cp_lexer_consume_token (parser->lexer);
18746 return nreverse (labels);
18749 /* Parse an (optional) series of attributes.
18752 attributes attribute
18755 __attribute__ (( attribute-list [opt] ))
18757 The return value is as for cp_parser_attribute_list. */
18760 cp_parser_attributes_opt (cp_parser* parser)
18762 tree attributes = NULL_TREE;
18767 tree attribute_list;
18769 /* Peek at the next token. */
18770 token = cp_lexer_peek_token (parser->lexer);
18771 /* If it's not `__attribute__', then we're done. */
18772 if (token->keyword != RID_ATTRIBUTE)
18775 /* Consume the `__attribute__' keyword. */
18776 cp_lexer_consume_token (parser->lexer);
18777 /* Look for the two `(' tokens. */
18778 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18779 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18781 /* Peek at the next token. */
18782 token = cp_lexer_peek_token (parser->lexer);
18783 if (token->type != CPP_CLOSE_PAREN)
18784 /* Parse the attribute-list. */
18785 attribute_list = cp_parser_attribute_list (parser);
18787 /* If the next token is a `)', then there is no attribute
18789 attribute_list = NULL;
18791 /* Look for the two `)' tokens. */
18792 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18793 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18795 /* Add these new attributes to the list. */
18796 attributes = chainon (attributes, attribute_list);
18802 /* Parse an attribute-list.
18806 attribute-list , attribute
18810 identifier ( identifier )
18811 identifier ( identifier , expression-list )
18812 identifier ( expression-list )
18814 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
18815 to an attribute. The TREE_PURPOSE of each node is the identifier
18816 indicating which attribute is in use. The TREE_VALUE represents
18817 the arguments, if any. */
18820 cp_parser_attribute_list (cp_parser* parser)
18822 tree attribute_list = NULL_TREE;
18823 bool save_translate_strings_p = parser->translate_strings_p;
18825 parser->translate_strings_p = false;
18832 /* Look for the identifier. We also allow keywords here; for
18833 example `__attribute__ ((const))' is legal. */
18834 token = cp_lexer_peek_token (parser->lexer);
18835 if (token->type == CPP_NAME
18836 || token->type == CPP_KEYWORD)
18838 tree arguments = NULL_TREE;
18840 /* Consume the token. */
18841 token = cp_lexer_consume_token (parser->lexer);
18843 /* Save away the identifier that indicates which attribute
18845 identifier = (token->type == CPP_KEYWORD)
18846 /* For keywords, use the canonical spelling, not the
18847 parsed identifier. */
18848 ? ridpointers[(int) token->keyword]
18851 attribute = build_tree_list (identifier, NULL_TREE);
18853 /* Peek at the next token. */
18854 token = cp_lexer_peek_token (parser->lexer);
18855 /* If it's an `(', then parse the attribute arguments. */
18856 if (token->type == CPP_OPEN_PAREN)
18859 int attr_flag = (attribute_takes_identifier_p (identifier)
18860 ? id_attr : normal_attr);
18861 vec = cp_parser_parenthesized_expression_list
18862 (parser, attr_flag, /*cast_p=*/false,
18863 /*allow_expansion_p=*/false,
18864 /*non_constant_p=*/NULL);
18866 arguments = error_mark_node;
18869 arguments = build_tree_list_vec (vec);
18870 release_tree_vector (vec);
18872 /* Save the arguments away. */
18873 TREE_VALUE (attribute) = arguments;
18876 if (arguments != error_mark_node)
18878 /* Add this attribute to the list. */
18879 TREE_CHAIN (attribute) = attribute_list;
18880 attribute_list = attribute;
18883 token = cp_lexer_peek_token (parser->lexer);
18885 /* Now, look for more attributes. If the next token isn't a
18886 `,', we're done. */
18887 if (token->type != CPP_COMMA)
18890 /* Consume the comma and keep going. */
18891 cp_lexer_consume_token (parser->lexer);
18893 parser->translate_strings_p = save_translate_strings_p;
18895 /* We built up the list in reverse order. */
18896 return nreverse (attribute_list);
18899 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
18900 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
18901 current value of the PEDANTIC flag, regardless of whether or not
18902 the `__extension__' keyword is present. The caller is responsible
18903 for restoring the value of the PEDANTIC flag. */
18906 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18908 /* Save the old value of the PEDANTIC flag. */
18909 *saved_pedantic = pedantic;
18911 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18913 /* Consume the `__extension__' token. */
18914 cp_lexer_consume_token (parser->lexer);
18915 /* We're not being pedantic while the `__extension__' keyword is
18925 /* Parse a label declaration.
18928 __label__ label-declarator-seq ;
18930 label-declarator-seq:
18931 identifier , label-declarator-seq
18935 cp_parser_label_declaration (cp_parser* parser)
18937 /* Look for the `__label__' keyword. */
18938 cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
18944 /* Look for an identifier. */
18945 identifier = cp_parser_identifier (parser);
18946 /* If we failed, stop. */
18947 if (identifier == error_mark_node)
18949 /* Declare it as a label. */
18950 finish_label_decl (identifier);
18951 /* If the next token is a `;', stop. */
18952 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18954 /* Look for the `,' separating the label declarations. */
18955 cp_parser_require (parser, CPP_COMMA, RT_COMMA);
18958 /* Look for the final `;'. */
18959 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18962 /* Support Functions */
18964 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
18965 NAME should have one of the representations used for an
18966 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
18967 is returned. If PARSER->SCOPE is a dependent type, then a
18968 SCOPE_REF is returned.
18970 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
18971 returned; the name was already resolved when the TEMPLATE_ID_EXPR
18972 was formed. Abstractly, such entities should not be passed to this
18973 function, because they do not need to be looked up, but it is
18974 simpler to check for this special case here, rather than at the
18977 In cases not explicitly covered above, this function returns a
18978 DECL, OVERLOAD, or baselink representing the result of the lookup.
18979 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
18982 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
18983 (e.g., "struct") that was used. In that case bindings that do not
18984 refer to types are ignored.
18986 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
18989 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
18992 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
18995 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
18996 TREE_LIST of candidates if name-lookup results in an ambiguity, and
18997 NULL_TREE otherwise. */
19000 cp_parser_lookup_name (cp_parser *parser, tree name,
19001 enum tag_types tag_type,
19004 bool check_dependency,
19005 tree *ambiguous_decls,
19006 location_t name_location)
19010 tree object_type = parser->context->object_type;
19012 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19013 flags |= LOOKUP_COMPLAIN;
19015 /* Assume that the lookup will be unambiguous. */
19016 if (ambiguous_decls)
19017 *ambiguous_decls = NULL_TREE;
19019 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19020 no longer valid. Note that if we are parsing tentatively, and
19021 the parse fails, OBJECT_TYPE will be automatically restored. */
19022 parser->context->object_type = NULL_TREE;
19024 if (name == error_mark_node)
19025 return error_mark_node;
19027 /* A template-id has already been resolved; there is no lookup to
19029 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19031 if (BASELINK_P (name))
19033 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19034 == TEMPLATE_ID_EXPR);
19038 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
19039 it should already have been checked to make sure that the name
19040 used matches the type being destroyed. */
19041 if (TREE_CODE (name) == BIT_NOT_EXPR)
19045 /* Figure out to which type this destructor applies. */
19047 type = parser->scope;
19048 else if (object_type)
19049 type = object_type;
19051 type = current_class_type;
19052 /* If that's not a class type, there is no destructor. */
19053 if (!type || !CLASS_TYPE_P (type))
19054 return error_mark_node;
19055 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19056 lazily_declare_fn (sfk_destructor, type);
19057 if (!CLASSTYPE_DESTRUCTORS (type))
19058 return error_mark_node;
19059 /* If it was a class type, return the destructor. */
19060 return CLASSTYPE_DESTRUCTORS (type);
19063 /* By this point, the NAME should be an ordinary identifier. If
19064 the id-expression was a qualified name, the qualifying scope is
19065 stored in PARSER->SCOPE at this point. */
19066 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19068 /* Perform the lookup. */
19073 if (parser->scope == error_mark_node)
19074 return error_mark_node;
19076 /* If the SCOPE is dependent, the lookup must be deferred until
19077 the template is instantiated -- unless we are explicitly
19078 looking up names in uninstantiated templates. Even then, we
19079 cannot look up the name if the scope is not a class type; it
19080 might, for example, be a template type parameter. */
19081 dependent_p = (TYPE_P (parser->scope)
19082 && dependent_scope_p (parser->scope));
19083 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19085 /* Defer lookup. */
19086 decl = error_mark_node;
19089 tree pushed_scope = NULL_TREE;
19091 /* If PARSER->SCOPE is a dependent type, then it must be a
19092 class type, and we must not be checking dependencies;
19093 otherwise, we would have processed this lookup above. So
19094 that PARSER->SCOPE is not considered a dependent base by
19095 lookup_member, we must enter the scope here. */
19097 pushed_scope = push_scope (parser->scope);
19099 /* If the PARSER->SCOPE is a template specialization, it
19100 may be instantiated during name lookup. In that case,
19101 errors may be issued. Even if we rollback the current
19102 tentative parse, those errors are valid. */
19103 decl = lookup_qualified_name (parser->scope, name,
19104 tag_type != none_type,
19105 /*complain=*/true);
19107 /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19108 lookup result and the nested-name-specifier nominates a class C:
19109 * if the name specified after the nested-name-specifier, when
19110 looked up in C, is the injected-class-name of C (Clause 9), or
19111 * if the name specified after the nested-name-specifier is the
19112 same as the identifier or the simple-template-id's template-
19113 name in the last component of the nested-name-specifier,
19114 the name is instead considered to name the constructor of
19115 class C. [ Note: for example, the constructor is not an
19116 acceptable lookup result in an elaborated-type-specifier so
19117 the constructor would not be used in place of the
19118 injected-class-name. --end note ] Such a constructor name
19119 shall be used only in the declarator-id of a declaration that
19120 names a constructor or in a using-declaration. */
19121 if (tag_type == none_type
19122 && DECL_SELF_REFERENCE_P (decl)
19123 && same_type_p (DECL_CONTEXT (decl), parser->scope))
19124 decl = lookup_qualified_name (parser->scope, ctor_identifier,
19125 tag_type != none_type,
19126 /*complain=*/true);
19128 /* If we have a single function from a using decl, pull it out. */
19129 if (TREE_CODE (decl) == OVERLOAD
19130 && !really_overloaded_fn (decl))
19131 decl = OVL_FUNCTION (decl);
19134 pop_scope (pushed_scope);
19137 /* If the scope is a dependent type and either we deferred lookup or
19138 we did lookup but didn't find the name, rememeber the name. */
19139 if (decl == error_mark_node && TYPE_P (parser->scope)
19140 && dependent_type_p (parser->scope))
19146 /* The resolution to Core Issue 180 says that `struct
19147 A::B' should be considered a type-name, even if `A'
19149 type = make_typename_type (parser->scope, name, tag_type,
19150 /*complain=*/tf_error);
19151 decl = TYPE_NAME (type);
19153 else if (is_template
19154 && (cp_parser_next_token_ends_template_argument_p (parser)
19155 || cp_lexer_next_token_is (parser->lexer,
19157 decl = make_unbound_class_template (parser->scope,
19159 /*complain=*/tf_error);
19161 decl = build_qualified_name (/*type=*/NULL_TREE,
19162 parser->scope, name,
19165 parser->qualifying_scope = parser->scope;
19166 parser->object_scope = NULL_TREE;
19168 else if (object_type)
19170 tree object_decl = NULL_TREE;
19171 /* Look up the name in the scope of the OBJECT_TYPE, unless the
19172 OBJECT_TYPE is not a class. */
19173 if (CLASS_TYPE_P (object_type))
19174 /* If the OBJECT_TYPE is a template specialization, it may
19175 be instantiated during name lookup. In that case, errors
19176 may be issued. Even if we rollback the current tentative
19177 parse, those errors are valid. */
19178 object_decl = lookup_member (object_type,
19181 tag_type != none_type);
19182 /* Look it up in the enclosing context, too. */
19183 decl = lookup_name_real (name, tag_type != none_type,
19185 /*block_p=*/true, is_namespace, flags);
19186 parser->object_scope = object_type;
19187 parser->qualifying_scope = NULL_TREE;
19189 decl = object_decl;
19193 decl = lookup_name_real (name, tag_type != none_type,
19195 /*block_p=*/true, is_namespace, flags);
19196 parser->qualifying_scope = NULL_TREE;
19197 parser->object_scope = NULL_TREE;
19200 /* If the lookup failed, let our caller know. */
19201 if (!decl || decl == error_mark_node)
19202 return error_mark_node;
19204 /* Pull out the template from an injected-class-name (or multiple). */
19206 decl = maybe_get_template_decl_from_type_decl (decl);
19208 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
19209 if (TREE_CODE (decl) == TREE_LIST)
19211 if (ambiguous_decls)
19212 *ambiguous_decls = decl;
19213 /* The error message we have to print is too complicated for
19214 cp_parser_error, so we incorporate its actions directly. */
19215 if (!cp_parser_simulate_error (parser))
19217 error_at (name_location, "reference to %qD is ambiguous",
19219 print_candidates (decl);
19221 return error_mark_node;
19224 gcc_assert (DECL_P (decl)
19225 || TREE_CODE (decl) == OVERLOAD
19226 || TREE_CODE (decl) == SCOPE_REF
19227 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19228 || BASELINK_P (decl));
19230 /* If we have resolved the name of a member declaration, check to
19231 see if the declaration is accessible. When the name resolves to
19232 set of overloaded functions, accessibility is checked when
19233 overload resolution is done.
19235 During an explicit instantiation, access is not checked at all,
19236 as per [temp.explicit]. */
19238 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19243 /* Like cp_parser_lookup_name, but for use in the typical case where
19244 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19245 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
19248 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19250 return cp_parser_lookup_name (parser, name,
19252 /*is_template=*/false,
19253 /*is_namespace=*/false,
19254 /*check_dependency=*/true,
19255 /*ambiguous_decls=*/NULL,
19259 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19260 the current context, return the TYPE_DECL. If TAG_NAME_P is
19261 true, the DECL indicates the class being defined in a class-head,
19262 or declared in an elaborated-type-specifier.
19264 Otherwise, return DECL. */
19267 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19269 /* If the TEMPLATE_DECL is being declared as part of a class-head,
19270 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19273 template <typename T> struct B;
19276 template <typename T> struct A::B {};
19278 Similarly, in an elaborated-type-specifier:
19280 namespace N { struct X{}; }
19283 template <typename T> friend struct N::X;
19286 However, if the DECL refers to a class type, and we are in
19287 the scope of the class, then the name lookup automatically
19288 finds the TYPE_DECL created by build_self_reference rather
19289 than a TEMPLATE_DECL. For example, in:
19291 template <class T> struct S {
19295 there is no need to handle such case. */
19297 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19298 return DECL_TEMPLATE_RESULT (decl);
19303 /* If too many, or too few, template-parameter lists apply to the
19304 declarator, issue an error message. Returns TRUE if all went well,
19305 and FALSE otherwise. */
19308 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19309 cp_declarator *declarator,
19310 location_t declarator_location)
19312 unsigned num_templates;
19314 /* We haven't seen any classes that involve template parameters yet. */
19317 switch (declarator->kind)
19320 if (declarator->u.id.qualifying_scope)
19324 scope = declarator->u.id.qualifying_scope;
19326 while (scope && CLASS_TYPE_P (scope))
19328 /* You're supposed to have one `template <...>'
19329 for every template class, but you don't need one
19330 for a full specialization. For example:
19332 template <class T> struct S{};
19333 template <> struct S<int> { void f(); };
19334 void S<int>::f () {}
19336 is correct; there shouldn't be a `template <>' for
19337 the definition of `S<int>::f'. */
19338 if (!CLASSTYPE_TEMPLATE_INFO (scope))
19339 /* If SCOPE does not have template information of any
19340 kind, then it is not a template, nor is it nested
19341 within a template. */
19343 if (explicit_class_specialization_p (scope))
19345 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19348 scope = TYPE_CONTEXT (scope);
19351 else if (TREE_CODE (declarator->u.id.unqualified_name)
19352 == TEMPLATE_ID_EXPR)
19353 /* If the DECLARATOR has the form `X<y>' then it uses one
19354 additional level of template parameters. */
19357 return cp_parser_check_template_parameters
19358 (parser, num_templates, declarator_location, declarator);
19364 case cdk_reference:
19366 return (cp_parser_check_declarator_template_parameters
19367 (parser, declarator->declarator, declarator_location));
19373 gcc_unreachable ();
19378 /* NUM_TEMPLATES were used in the current declaration. If that is
19379 invalid, return FALSE and issue an error messages. Otherwise,
19380 return TRUE. If DECLARATOR is non-NULL, then we are checking a
19381 declarator and we can print more accurate diagnostics. */
19384 cp_parser_check_template_parameters (cp_parser* parser,
19385 unsigned num_templates,
19386 location_t location,
19387 cp_declarator *declarator)
19389 /* If there are the same number of template classes and parameter
19390 lists, that's OK. */
19391 if (parser->num_template_parameter_lists == num_templates)
19393 /* If there are more, but only one more, then we are referring to a
19394 member template. That's OK too. */
19395 if (parser->num_template_parameter_lists == num_templates + 1)
19397 /* If there are more template classes than parameter lists, we have
19400 template <class T> void S<T>::R<T>::f (); */
19401 if (parser->num_template_parameter_lists < num_templates)
19403 if (declarator && !current_function_decl)
19404 error_at (location, "specializing member %<%T::%E%> "
19405 "requires %<template<>%> syntax",
19406 declarator->u.id.qualifying_scope,
19407 declarator->u.id.unqualified_name);
19408 else if (declarator)
19409 error_at (location, "invalid declaration of %<%T::%E%>",
19410 declarator->u.id.qualifying_scope,
19411 declarator->u.id.unqualified_name);
19413 error_at (location, "too few template-parameter-lists");
19416 /* Otherwise, there are too many template parameter lists. We have
19419 template <class T> template <class U> void S::f(); */
19420 error_at (location, "too many template-parameter-lists");
19424 /* Parse an optional `::' token indicating that the following name is
19425 from the global namespace. If so, PARSER->SCOPE is set to the
19426 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19427 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19428 Returns the new value of PARSER->SCOPE, if the `::' token is
19429 present, and NULL_TREE otherwise. */
19432 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19436 /* Peek at the next token. */
19437 token = cp_lexer_peek_token (parser->lexer);
19438 /* If we're looking at a `::' token then we're starting from the
19439 global namespace, not our current location. */
19440 if (token->type == CPP_SCOPE)
19442 /* Consume the `::' token. */
19443 cp_lexer_consume_token (parser->lexer);
19444 /* Set the SCOPE so that we know where to start the lookup. */
19445 parser->scope = global_namespace;
19446 parser->qualifying_scope = global_namespace;
19447 parser->object_scope = NULL_TREE;
19449 return parser->scope;
19451 else if (!current_scope_valid_p)
19453 parser->scope = NULL_TREE;
19454 parser->qualifying_scope = NULL_TREE;
19455 parser->object_scope = NULL_TREE;
19461 /* Returns TRUE if the upcoming token sequence is the start of a
19462 constructor declarator. If FRIEND_P is true, the declarator is
19463 preceded by the `friend' specifier. */
19466 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19468 bool constructor_p;
19469 tree nested_name_specifier;
19470 cp_token *next_token;
19472 /* The common case is that this is not a constructor declarator, so
19473 try to avoid doing lots of work if at all possible. It's not
19474 valid declare a constructor at function scope. */
19475 if (parser->in_function_body)
19477 /* And only certain tokens can begin a constructor declarator. */
19478 next_token = cp_lexer_peek_token (parser->lexer);
19479 if (next_token->type != CPP_NAME
19480 && next_token->type != CPP_SCOPE
19481 && next_token->type != CPP_NESTED_NAME_SPECIFIER
19482 && next_token->type != CPP_TEMPLATE_ID)
19485 /* Parse tentatively; we are going to roll back all of the tokens
19487 cp_parser_parse_tentatively (parser);
19488 /* Assume that we are looking at a constructor declarator. */
19489 constructor_p = true;
19491 /* Look for the optional `::' operator. */
19492 cp_parser_global_scope_opt (parser,
19493 /*current_scope_valid_p=*/false);
19494 /* Look for the nested-name-specifier. */
19495 nested_name_specifier
19496 = (cp_parser_nested_name_specifier_opt (parser,
19497 /*typename_keyword_p=*/false,
19498 /*check_dependency_p=*/false,
19500 /*is_declaration=*/false));
19501 /* Outside of a class-specifier, there must be a
19502 nested-name-specifier. */
19503 if (!nested_name_specifier &&
19504 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19506 constructor_p = false;
19507 else if (nested_name_specifier == error_mark_node)
19508 constructor_p = false;
19510 /* If we have a class scope, this is easy; DR 147 says that S::S always
19511 names the constructor, and no other qualified name could. */
19512 if (constructor_p && nested_name_specifier
19513 && TYPE_P (nested_name_specifier))
19515 tree id = cp_parser_unqualified_id (parser,
19516 /*template_keyword_p=*/false,
19517 /*check_dependency_p=*/false,
19518 /*declarator_p=*/true,
19519 /*optional_p=*/false);
19520 if (is_overloaded_fn (id))
19521 id = DECL_NAME (get_first_fn (id));
19522 if (!constructor_name_p (id, nested_name_specifier))
19523 constructor_p = false;
19525 /* If we still think that this might be a constructor-declarator,
19526 look for a class-name. */
19527 else if (constructor_p)
19531 template <typename T> struct S {
19535 we must recognize that the nested `S' names a class. */
19537 type_decl = cp_parser_class_name (parser,
19538 /*typename_keyword_p=*/false,
19539 /*template_keyword_p=*/false,
19541 /*check_dependency_p=*/false,
19542 /*class_head_p=*/false,
19543 /*is_declaration=*/false);
19544 /* If there was no class-name, then this is not a constructor. */
19545 constructor_p = !cp_parser_error_occurred (parser);
19547 /* If we're still considering a constructor, we have to see a `(',
19548 to begin the parameter-declaration-clause, followed by either a
19549 `)', an `...', or a decl-specifier. We need to check for a
19550 type-specifier to avoid being fooled into thinking that:
19554 is a constructor. (It is actually a function named `f' that
19555 takes one parameter (of type `int') and returns a value of type
19558 && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19559 constructor_p = false;
19562 && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19563 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19564 /* A parameter declaration begins with a decl-specifier,
19565 which is either the "attribute" keyword, a storage class
19566 specifier, or (usually) a type-specifier. */
19567 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19570 tree pushed_scope = NULL_TREE;
19571 unsigned saved_num_template_parameter_lists;
19573 /* Names appearing in the type-specifier should be looked up
19574 in the scope of the class. */
19575 if (current_class_type)
19579 type = TREE_TYPE (type_decl);
19580 if (TREE_CODE (type) == TYPENAME_TYPE)
19582 type = resolve_typename_type (type,
19583 /*only_current_p=*/false);
19584 if (TREE_CODE (type) == TYPENAME_TYPE)
19586 cp_parser_abort_tentative_parse (parser);
19590 pushed_scope = push_scope (type);
19593 /* Inside the constructor parameter list, surrounding
19594 template-parameter-lists do not apply. */
19595 saved_num_template_parameter_lists
19596 = parser->num_template_parameter_lists;
19597 parser->num_template_parameter_lists = 0;
19599 /* Look for the type-specifier. */
19600 cp_parser_type_specifier (parser,
19601 CP_PARSER_FLAGS_NONE,
19602 /*decl_specs=*/NULL,
19603 /*is_declarator=*/true,
19604 /*declares_class_or_enum=*/NULL,
19605 /*is_cv_qualifier=*/NULL);
19607 parser->num_template_parameter_lists
19608 = saved_num_template_parameter_lists;
19610 /* Leave the scope of the class. */
19612 pop_scope (pushed_scope);
19614 constructor_p = !cp_parser_error_occurred (parser);
19618 /* We did not really want to consume any tokens. */
19619 cp_parser_abort_tentative_parse (parser);
19621 return constructor_p;
19624 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19625 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
19626 they must be performed once we are in the scope of the function.
19628 Returns the function defined. */
19631 cp_parser_function_definition_from_specifiers_and_declarator
19632 (cp_parser* parser,
19633 cp_decl_specifier_seq *decl_specifiers,
19635 const cp_declarator *declarator)
19640 /* Begin the function-definition. */
19641 success_p = start_function (decl_specifiers, declarator, attributes);
19643 /* The things we're about to see are not directly qualified by any
19644 template headers we've seen thus far. */
19645 reset_specialization ();
19647 /* If there were names looked up in the decl-specifier-seq that we
19648 did not check, check them now. We must wait until we are in the
19649 scope of the function to perform the checks, since the function
19650 might be a friend. */
19651 perform_deferred_access_checks ();
19655 /* Skip the entire function. */
19656 cp_parser_skip_to_end_of_block_or_statement (parser);
19657 fn = error_mark_node;
19659 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19661 /* Seen already, skip it. An error message has already been output. */
19662 cp_parser_skip_to_end_of_block_or_statement (parser);
19663 fn = current_function_decl;
19664 current_function_decl = NULL_TREE;
19665 /* If this is a function from a class, pop the nested class. */
19666 if (current_class_name)
19667 pop_nested_class ();
19672 if (DECL_DECLARED_INLINE_P (current_function_decl))
19673 tv = TV_PARSE_INLINE;
19675 tv = TV_PARSE_FUNC;
19677 fn = cp_parser_function_definition_after_declarator (parser,
19678 /*inline_p=*/false);
19685 /* Parse the part of a function-definition that follows the
19686 declarator. INLINE_P is TRUE iff this function is an inline
19687 function defined within a class-specifier.
19689 Returns the function defined. */
19692 cp_parser_function_definition_after_declarator (cp_parser* parser,
19696 bool ctor_initializer_p = false;
19697 bool saved_in_unbraced_linkage_specification_p;
19698 bool saved_in_function_body;
19699 unsigned saved_num_template_parameter_lists;
19702 saved_in_function_body = parser->in_function_body;
19703 parser->in_function_body = true;
19704 /* If the next token is `return', then the code may be trying to
19705 make use of the "named return value" extension that G++ used to
19707 token = cp_lexer_peek_token (parser->lexer);
19708 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19710 /* Consume the `return' keyword. */
19711 cp_lexer_consume_token (parser->lexer);
19712 /* Look for the identifier that indicates what value is to be
19714 cp_parser_identifier (parser);
19715 /* Issue an error message. */
19716 error_at (token->location,
19717 "named return values are no longer supported");
19718 /* Skip tokens until we reach the start of the function body. */
19721 cp_token *token = cp_lexer_peek_token (parser->lexer);
19722 if (token->type == CPP_OPEN_BRACE
19723 || token->type == CPP_EOF
19724 || token->type == CPP_PRAGMA_EOL)
19726 cp_lexer_consume_token (parser->lexer);
19729 /* The `extern' in `extern "C" void f () { ... }' does not apply to
19730 anything declared inside `f'. */
19731 saved_in_unbraced_linkage_specification_p
19732 = parser->in_unbraced_linkage_specification_p;
19733 parser->in_unbraced_linkage_specification_p = false;
19734 /* Inside the function, surrounding template-parameter-lists do not
19736 saved_num_template_parameter_lists
19737 = parser->num_template_parameter_lists;
19738 parser->num_template_parameter_lists = 0;
19740 start_lambda_scope (current_function_decl);
19742 /* If the next token is `try', then we are looking at a
19743 function-try-block. */
19744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19745 ctor_initializer_p = cp_parser_function_try_block (parser);
19746 /* A function-try-block includes the function-body, so we only do
19747 this next part if we're not processing a function-try-block. */
19750 = cp_parser_ctor_initializer_opt_and_function_body (parser);
19752 finish_lambda_scope ();
19754 /* Finish the function. */
19755 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19756 (inline_p ? 2 : 0));
19757 /* Generate code for it, if necessary. */
19758 expand_or_defer_fn (fn);
19759 /* Restore the saved values. */
19760 parser->in_unbraced_linkage_specification_p
19761 = saved_in_unbraced_linkage_specification_p;
19762 parser->num_template_parameter_lists
19763 = saved_num_template_parameter_lists;
19764 parser->in_function_body = saved_in_function_body;
19769 /* Parse a template-declaration, assuming that the `export' (and
19770 `extern') keywords, if present, has already been scanned. MEMBER_P
19771 is as for cp_parser_template_declaration. */
19774 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19776 tree decl = NULL_TREE;
19777 VEC (deferred_access_check,gc) *checks;
19778 tree parameter_list;
19779 bool friend_p = false;
19780 bool need_lang_pop;
19783 /* Look for the `template' keyword. */
19784 token = cp_lexer_peek_token (parser->lexer);
19785 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19789 if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19791 if (at_class_scope_p () && current_function_decl)
19793 /* 14.5.2.2 [temp.mem]
19795 A local class shall not have member templates. */
19796 error_at (token->location,
19797 "invalid declaration of member template in local class");
19798 cp_parser_skip_to_end_of_block_or_statement (parser);
19803 A template ... shall not have C linkage. */
19804 if (current_lang_name == lang_name_c)
19806 error_at (token->location, "template with C linkage");
19807 /* Give it C++ linkage to avoid confusing other parts of the
19809 push_lang_context (lang_name_cplusplus);
19810 need_lang_pop = true;
19813 need_lang_pop = false;
19815 /* We cannot perform access checks on the template parameter
19816 declarations until we know what is being declared, just as we
19817 cannot check the decl-specifier list. */
19818 push_deferring_access_checks (dk_deferred);
19820 /* If the next token is `>', then we have an invalid
19821 specialization. Rather than complain about an invalid template
19822 parameter, issue an error message here. */
19823 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19825 cp_parser_error (parser, "invalid explicit specialization");
19826 begin_specialization ();
19827 parameter_list = NULL_TREE;
19831 /* Parse the template parameters. */
19832 parameter_list = cp_parser_template_parameter_list (parser);
19833 fixup_template_parms ();
19836 /* Get the deferred access checks from the parameter list. These
19837 will be checked once we know what is being declared, as for a
19838 member template the checks must be performed in the scope of the
19839 class containing the member. */
19840 checks = get_deferred_access_checks ();
19842 /* Look for the `>'. */
19843 cp_parser_skip_to_end_of_template_parameter_list (parser);
19844 /* We just processed one more parameter list. */
19845 ++parser->num_template_parameter_lists;
19846 /* If the next token is `template', there are more template
19848 if (cp_lexer_next_token_is_keyword (parser->lexer,
19850 cp_parser_template_declaration_after_export (parser, member_p);
19853 /* There are no access checks when parsing a template, as we do not
19854 know if a specialization will be a friend. */
19855 push_deferring_access_checks (dk_no_check);
19856 token = cp_lexer_peek_token (parser->lexer);
19857 decl = cp_parser_single_declaration (parser,
19860 /*explicit_specialization_p=*/false,
19862 pop_deferring_access_checks ();
19864 /* If this is a member template declaration, let the front
19866 if (member_p && !friend_p && decl)
19868 if (TREE_CODE (decl) == TYPE_DECL)
19869 cp_parser_check_access_in_redeclaration (decl, token->location);
19871 decl = finish_member_template_decl (decl);
19873 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19874 make_friend_class (current_class_type, TREE_TYPE (decl),
19875 /*complain=*/true);
19877 /* We are done with the current parameter list. */
19878 --parser->num_template_parameter_lists;
19880 pop_deferring_access_checks ();
19883 finish_template_decl (parameter_list);
19885 /* Register member declarations. */
19886 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19887 finish_member_declaration (decl);
19888 /* For the erroneous case of a template with C linkage, we pushed an
19889 implicit C++ linkage scope; exit that scope now. */
19891 pop_lang_context ();
19892 /* If DECL is a function template, we must return to parse it later.
19893 (Even though there is no definition, there might be default
19894 arguments that need handling.) */
19895 if (member_p && decl
19896 && (TREE_CODE (decl) == FUNCTION_DECL
19897 || DECL_FUNCTION_TEMPLATE_P (decl)))
19898 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19901 /* Perform the deferred access checks from a template-parameter-list.
19902 CHECKS is a TREE_LIST of access checks, as returned by
19903 get_deferred_access_checks. */
19906 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19908 ++processing_template_parmlist;
19909 perform_access_checks (checks);
19910 --processing_template_parmlist;
19913 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19914 `function-definition' sequence. MEMBER_P is true, this declaration
19915 appears in a class scope.
19917 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
19918 *FRIEND_P is set to TRUE iff the declaration is a friend. */
19921 cp_parser_single_declaration (cp_parser* parser,
19922 VEC (deferred_access_check,gc)* checks,
19924 bool explicit_specialization_p,
19927 int declares_class_or_enum;
19928 tree decl = NULL_TREE;
19929 cp_decl_specifier_seq decl_specifiers;
19930 bool function_definition_p = false;
19931 cp_token *decl_spec_token_start;
19933 /* This function is only used when processing a template
19935 gcc_assert (innermost_scope_kind () == sk_template_parms
19936 || innermost_scope_kind () == sk_template_spec);
19938 /* Defer access checks until we know what is being declared. */
19939 push_deferring_access_checks (dk_deferred);
19941 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
19943 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
19944 cp_parser_decl_specifier_seq (parser,
19945 CP_PARSER_FLAGS_OPTIONAL,
19947 &declares_class_or_enum);
19949 *friend_p = cp_parser_friend_p (&decl_specifiers);
19951 /* There are no template typedefs. */
19952 if (decl_specifiers.specs[(int) ds_typedef])
19954 error_at (decl_spec_token_start->location,
19955 "template declaration of %<typedef%>");
19956 decl = error_mark_node;
19959 /* Gather up the access checks that occurred the
19960 decl-specifier-seq. */
19961 stop_deferring_access_checks ();
19963 /* Check for the declaration of a template class. */
19964 if (declares_class_or_enum)
19966 if (cp_parser_declares_only_class_p (parser))
19968 decl = shadow_tag (&decl_specifiers);
19973 friend template <typename T> struct A<T>::B;
19976 A<T>::B will be represented by a TYPENAME_TYPE, and
19977 therefore not recognized by shadow_tag. */
19978 if (friend_p && *friend_p
19980 && decl_specifiers.type
19981 && TYPE_P (decl_specifiers.type))
19982 decl = decl_specifiers.type;
19984 if (decl && decl != error_mark_node)
19985 decl = TYPE_NAME (decl);
19987 decl = error_mark_node;
19989 /* Perform access checks for template parameters. */
19990 cp_parser_perform_template_parameter_access_checks (checks);
19994 /* Complain about missing 'typename' or other invalid type names. */
19995 if (!decl_specifiers.any_type_specifiers_p
19996 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
19998 /* cp_parser_parse_and_diagnose_invalid_type_name calls
19999 cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20000 the rest of this declaration. */
20001 decl = error_mark_node;
20005 /* If it's not a template class, try for a template function. If
20006 the next token is a `;', then this declaration does not declare
20007 anything. But, if there were errors in the decl-specifiers, then
20008 the error might well have come from an attempted class-specifier.
20009 In that case, there's no need to warn about a missing declarator. */
20011 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20012 || decl_specifiers.type != error_mark_node))
20014 decl = cp_parser_init_declarator (parser,
20017 /*function_definition_allowed_p=*/true,
20019 declares_class_or_enum,
20020 &function_definition_p,
20023 /* 7.1.1-1 [dcl.stc]
20025 A storage-class-specifier shall not be specified in an explicit
20026 specialization... */
20028 && explicit_specialization_p
20029 && decl_specifiers.storage_class != sc_none)
20031 error_at (decl_spec_token_start->location,
20032 "explicit template specialization cannot have a storage class");
20033 decl = error_mark_node;
20037 /* Look for a trailing `;' after the declaration. */
20038 if (!function_definition_p
20039 && (decl == error_mark_node
20040 || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20041 cp_parser_skip_to_end_of_block_or_statement (parser);
20044 pop_deferring_access_checks ();
20046 /* Clear any current qualification; whatever comes next is the start
20047 of something new. */
20048 parser->scope = NULL_TREE;
20049 parser->qualifying_scope = NULL_TREE;
20050 parser->object_scope = NULL_TREE;
20055 /* Parse a cast-expression that is not the operand of a unary "&". */
20058 cp_parser_simple_cast_expression (cp_parser *parser)
20060 return cp_parser_cast_expression (parser, /*address_p=*/false,
20061 /*cast_p=*/false, NULL);
20064 /* Parse a functional cast to TYPE. Returns an expression
20065 representing the cast. */
20068 cp_parser_functional_cast (cp_parser* parser, tree type)
20071 tree expression_list;
20075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20077 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20078 expression_list = cp_parser_braced_list (parser, &nonconst_p);
20079 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20080 if (TREE_CODE (type) == TYPE_DECL)
20081 type = TREE_TYPE (type);
20082 return finish_compound_literal (type, expression_list,
20083 tf_warning_or_error);
20087 vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20089 /*allow_expansion_p=*/true,
20090 /*non_constant_p=*/NULL);
20092 expression_list = error_mark_node;
20095 expression_list = build_tree_list_vec (vec);
20096 release_tree_vector (vec);
20099 cast = build_functional_cast (type, expression_list,
20100 tf_warning_or_error);
20101 /* [expr.const]/1: In an integral constant expression "only type
20102 conversions to integral or enumeration type can be used". */
20103 if (TREE_CODE (type) == TYPE_DECL)
20104 type = TREE_TYPE (type);
20105 if (cast != error_mark_node
20106 && !cast_valid_in_integral_constant_expression_p (type)
20107 && cp_parser_non_integral_constant_expression (parser,
20109 return error_mark_node;
20113 /* Save the tokens that make up the body of a member function defined
20114 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
20115 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
20116 specifiers applied to the declaration. Returns the FUNCTION_DECL
20117 for the member function. */
20120 cp_parser_save_member_function_body (cp_parser* parser,
20121 cp_decl_specifier_seq *decl_specifiers,
20122 cp_declarator *declarator,
20129 /* Create the FUNCTION_DECL. */
20130 fn = grokmethod (decl_specifiers, declarator, attributes);
20131 /* If something went badly wrong, bail out now. */
20132 if (fn == error_mark_node)
20134 /* If there's a function-body, skip it. */
20135 if (cp_parser_token_starts_function_definition_p
20136 (cp_lexer_peek_token (parser->lexer)))
20137 cp_parser_skip_to_end_of_block_or_statement (parser);
20138 return error_mark_node;
20141 /* Remember it, if there default args to post process. */
20142 cp_parser_save_default_args (parser, fn);
20144 /* Save away the tokens that make up the body of the
20146 first = parser->lexer->next_token;
20147 /* We can have braced-init-list mem-initializers before the fn body. */
20148 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20150 cp_lexer_consume_token (parser->lexer);
20151 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20152 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20154 /* cache_group will stop after an un-nested { } pair, too. */
20155 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20158 /* variadic mem-inits have ... after the ')'. */
20159 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20160 cp_lexer_consume_token (parser->lexer);
20163 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20164 /* Handle function try blocks. */
20165 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20166 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20167 last = parser->lexer->next_token;
20169 /* Save away the inline definition; we will process it when the
20170 class is complete. */
20171 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20172 DECL_PENDING_INLINE_P (fn) = 1;
20174 /* We need to know that this was defined in the class, so that
20175 friend templates are handled correctly. */
20176 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20178 /* Add FN to the queue of functions to be parsed later. */
20179 VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20184 /* Parse a template-argument-list, as well as the trailing ">" (but
20185 not the opening ">"). See cp_parser_template_argument_list for the
20189 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20193 tree saved_qualifying_scope;
20194 tree saved_object_scope;
20195 bool saved_greater_than_is_operator_p;
20196 int saved_unevaluated_operand;
20197 int saved_inhibit_evaluation_warnings;
20201 When parsing a template-id, the first non-nested `>' is taken as
20202 the end of the template-argument-list rather than a greater-than
20204 saved_greater_than_is_operator_p
20205 = parser->greater_than_is_operator_p;
20206 parser->greater_than_is_operator_p = false;
20207 /* Parsing the argument list may modify SCOPE, so we save it
20209 saved_scope = parser->scope;
20210 saved_qualifying_scope = parser->qualifying_scope;
20211 saved_object_scope = parser->object_scope;
20212 /* We need to evaluate the template arguments, even though this
20213 template-id may be nested within a "sizeof". */
20214 saved_unevaluated_operand = cp_unevaluated_operand;
20215 cp_unevaluated_operand = 0;
20216 saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20217 c_inhibit_evaluation_warnings = 0;
20218 /* Parse the template-argument-list itself. */
20219 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20220 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20221 arguments = NULL_TREE;
20223 arguments = cp_parser_template_argument_list (parser);
20224 /* Look for the `>' that ends the template-argument-list. If we find
20225 a '>>' instead, it's probably just a typo. */
20226 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20228 if (cxx_dialect != cxx98)
20230 /* In C++0x, a `>>' in a template argument list or cast
20231 expression is considered to be two separate `>'
20232 tokens. So, change the current token to a `>', but don't
20233 consume it: it will be consumed later when the outer
20234 template argument list (or cast expression) is parsed.
20235 Note that this replacement of `>' for `>>' is necessary
20236 even if we are parsing tentatively: in the tentative
20237 case, after calling
20238 cp_parser_enclosed_template_argument_list we will always
20239 throw away all of the template arguments and the first
20240 closing `>', either because the template argument list
20241 was erroneous or because we are replacing those tokens
20242 with a CPP_TEMPLATE_ID token. The second `>' (which will
20243 not have been thrown away) is needed either to close an
20244 outer template argument list or to complete a new-style
20246 cp_token *token = cp_lexer_peek_token (parser->lexer);
20247 token->type = CPP_GREATER;
20249 else if (!saved_greater_than_is_operator_p)
20251 /* If we're in a nested template argument list, the '>>' has
20252 to be a typo for '> >'. We emit the error message, but we
20253 continue parsing and we push a '>' as next token, so that
20254 the argument list will be parsed correctly. Note that the
20255 global source location is still on the token before the
20256 '>>', so we need to say explicitly where we want it. */
20257 cp_token *token = cp_lexer_peek_token (parser->lexer);
20258 error_at (token->location, "%<>>%> should be %<> >%> "
20259 "within a nested template argument list");
20261 token->type = CPP_GREATER;
20265 /* If this is not a nested template argument list, the '>>'
20266 is a typo for '>'. Emit an error message and continue.
20267 Same deal about the token location, but here we can get it
20268 right by consuming the '>>' before issuing the diagnostic. */
20269 cp_token *token = cp_lexer_consume_token (parser->lexer);
20270 error_at (token->location,
20271 "spurious %<>>%>, use %<>%> to terminate "
20272 "a template argument list");
20276 cp_parser_skip_to_end_of_template_parameter_list (parser);
20277 /* The `>' token might be a greater-than operator again now. */
20278 parser->greater_than_is_operator_p
20279 = saved_greater_than_is_operator_p;
20280 /* Restore the SAVED_SCOPE. */
20281 parser->scope = saved_scope;
20282 parser->qualifying_scope = saved_qualifying_scope;
20283 parser->object_scope = saved_object_scope;
20284 cp_unevaluated_operand = saved_unevaluated_operand;
20285 c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20290 /* MEMBER_FUNCTION is a member function, or a friend. If default
20291 arguments, or the body of the function have not yet been parsed,
20295 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20297 timevar_push (TV_PARSE_INMETH);
20298 /* If this member is a template, get the underlying
20300 if (DECL_FUNCTION_TEMPLATE_P (member_function))
20301 member_function = DECL_TEMPLATE_RESULT (member_function);
20303 /* There should not be any class definitions in progress at this
20304 point; the bodies of members are only parsed outside of all class
20306 gcc_assert (parser->num_classes_being_defined == 0);
20307 /* While we're parsing the member functions we might encounter more
20308 classes. We want to handle them right away, but we don't want
20309 them getting mixed up with functions that are currently in the
20311 push_unparsed_function_queues (parser);
20313 /* Make sure that any template parameters are in scope. */
20314 maybe_begin_member_template_processing (member_function);
20316 /* If the body of the function has not yet been parsed, parse it
20318 if (DECL_PENDING_INLINE_P (member_function))
20320 tree function_scope;
20321 cp_token_cache *tokens;
20323 /* The function is no longer pending; we are processing it. */
20324 tokens = DECL_PENDING_INLINE_INFO (member_function);
20325 DECL_PENDING_INLINE_INFO (member_function) = NULL;
20326 DECL_PENDING_INLINE_P (member_function) = 0;
20328 /* If this is a local class, enter the scope of the containing
20330 function_scope = current_function_decl;
20331 if (function_scope)
20332 push_function_context ();
20334 /* Push the body of the function onto the lexer stack. */
20335 cp_parser_push_lexer_for_tokens (parser, tokens);
20337 /* Let the front end know that we going to be defining this
20339 start_preparsed_function (member_function, NULL_TREE,
20340 SF_PRE_PARSED | SF_INCLASS_INLINE);
20342 /* Don't do access checking if it is a templated function. */
20343 if (processing_template_decl)
20344 push_deferring_access_checks (dk_no_check);
20346 /* Now, parse the body of the function. */
20347 cp_parser_function_definition_after_declarator (parser,
20348 /*inline_p=*/true);
20350 if (processing_template_decl)
20351 pop_deferring_access_checks ();
20353 /* Leave the scope of the containing function. */
20354 if (function_scope)
20355 pop_function_context ();
20356 cp_parser_pop_lexer (parser);
20359 /* Remove any template parameters from the symbol table. */
20360 maybe_end_member_template_processing ();
20362 /* Restore the queue. */
20363 pop_unparsed_function_queues (parser);
20364 timevar_pop (TV_PARSE_INMETH);
20367 /* If DECL contains any default args, remember it on the unparsed
20368 functions queue. */
20371 cp_parser_save_default_args (cp_parser* parser, tree decl)
20375 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20377 probe = TREE_CHAIN (probe))
20378 if (TREE_PURPOSE (probe))
20380 cp_default_arg_entry *entry
20381 = VEC_safe_push (cp_default_arg_entry, gc,
20382 unparsed_funs_with_default_args, NULL);
20383 entry->class_type = current_class_type;
20384 entry->decl = decl;
20389 /* FN is a FUNCTION_DECL which may contains a parameter with an
20390 unparsed DEFAULT_ARG. Parse the default args now. This function
20391 assumes that the current scope is the scope in which the default
20392 argument should be processed. */
20395 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20397 bool saved_local_variables_forbidden_p;
20398 tree parm, parmdecl;
20400 /* While we're parsing the default args, we might (due to the
20401 statement expression extension) encounter more classes. We want
20402 to handle them right away, but we don't want them getting mixed
20403 up with default args that are currently in the queue. */
20404 push_unparsed_function_queues (parser);
20406 /* Local variable names (and the `this' keyword) may not appear
20407 in a default argument. */
20408 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20409 parser->local_variables_forbidden_p = true;
20411 push_defarg_context (fn);
20413 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20414 parmdecl = DECL_ARGUMENTS (fn);
20415 parm && parm != void_list_node;
20416 parm = TREE_CHAIN (parm),
20417 parmdecl = DECL_CHAIN (parmdecl))
20419 cp_token_cache *tokens;
20420 tree default_arg = TREE_PURPOSE (parm);
20422 VEC(tree,gc) *insts;
20429 if (TREE_CODE (default_arg) != DEFAULT_ARG)
20430 /* This can happen for a friend declaration for a function
20431 already declared with default arguments. */
20434 /* Push the saved tokens for the default argument onto the parser's
20436 tokens = DEFARG_TOKENS (default_arg);
20437 cp_parser_push_lexer_for_tokens (parser, tokens);
20439 start_lambda_scope (parmdecl);
20441 /* Parse the assignment-expression. */
20442 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20443 if (parsed_arg == error_mark_node)
20445 cp_parser_pop_lexer (parser);
20449 if (!processing_template_decl)
20450 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20452 TREE_PURPOSE (parm) = parsed_arg;
20454 /* Update any instantiations we've already created. */
20455 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20456 VEC_iterate (tree, insts, ix, copy); ix++)
20457 TREE_PURPOSE (copy) = parsed_arg;
20459 finish_lambda_scope ();
20461 /* If the token stream has not been completely used up, then
20462 there was extra junk after the end of the default
20464 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20465 cp_parser_error (parser, "expected %<,%>");
20467 /* Revert to the main lexer. */
20468 cp_parser_pop_lexer (parser);
20471 pop_defarg_context ();
20473 /* Make sure no default arg is missing. */
20474 check_default_args (fn);
20476 /* Restore the state of local_variables_forbidden_p. */
20477 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20479 /* Restore the queue. */
20480 pop_unparsed_function_queues (parser);
20483 /* Parse the operand of `sizeof' (or a similar operator). Returns
20484 either a TYPE or an expression, depending on the form of the
20485 input. The KEYWORD indicates which kind of expression we have
20489 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20491 tree expr = NULL_TREE;
20492 const char *saved_message;
20494 bool saved_integral_constant_expression_p;
20495 bool saved_non_integral_constant_expression_p;
20496 bool pack_expansion_p = false;
20498 /* Types cannot be defined in a `sizeof' expression. Save away the
20500 saved_message = parser->type_definition_forbidden_message;
20501 /* And create the new one. */
20502 tmp = concat ("types may not be defined in %<",
20503 IDENTIFIER_POINTER (ridpointers[keyword]),
20504 "%> expressions", NULL);
20505 parser->type_definition_forbidden_message = tmp;
20507 /* The restrictions on constant-expressions do not apply inside
20508 sizeof expressions. */
20509 saved_integral_constant_expression_p
20510 = parser->integral_constant_expression_p;
20511 saved_non_integral_constant_expression_p
20512 = parser->non_integral_constant_expression_p;
20513 parser->integral_constant_expression_p = false;
20515 /* If it's a `...', then we are computing the length of a parameter
20517 if (keyword == RID_SIZEOF
20518 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20520 /* Consume the `...'. */
20521 cp_lexer_consume_token (parser->lexer);
20522 maybe_warn_variadic_templates ();
20524 /* Note that this is an expansion. */
20525 pack_expansion_p = true;
20528 /* Do not actually evaluate the expression. */
20529 ++cp_unevaluated_operand;
20530 ++c_inhibit_evaluation_warnings;
20531 /* If it's a `(', then we might be looking at the type-id
20533 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20536 bool saved_in_type_id_in_expr_p;
20538 /* We can't be sure yet whether we're looking at a type-id or an
20540 cp_parser_parse_tentatively (parser);
20541 /* Consume the `('. */
20542 cp_lexer_consume_token (parser->lexer);
20543 /* Parse the type-id. */
20544 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20545 parser->in_type_id_in_expr_p = true;
20546 type = cp_parser_type_id (parser);
20547 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20548 /* Now, look for the trailing `)'. */
20549 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20550 /* If all went well, then we're done. */
20551 if (cp_parser_parse_definitely (parser))
20553 cp_decl_specifier_seq decl_specs;
20555 /* Build a trivial decl-specifier-seq. */
20556 clear_decl_specs (&decl_specs);
20557 decl_specs.type = type;
20559 /* Call grokdeclarator to figure out what type this is. */
20560 expr = grokdeclarator (NULL,
20564 /*attrlist=*/NULL);
20568 /* If the type-id production did not work out, then we must be
20569 looking at the unary-expression production. */
20571 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20572 /*cast_p=*/false, NULL);
20574 if (pack_expansion_p)
20575 /* Build a pack expansion. */
20576 expr = make_pack_expansion (expr);
20578 /* Go back to evaluating expressions. */
20579 --cp_unevaluated_operand;
20580 --c_inhibit_evaluation_warnings;
20582 /* Free the message we created. */
20584 /* And restore the old one. */
20585 parser->type_definition_forbidden_message = saved_message;
20586 parser->integral_constant_expression_p
20587 = saved_integral_constant_expression_p;
20588 parser->non_integral_constant_expression_p
20589 = saved_non_integral_constant_expression_p;
20594 /* If the current declaration has no declarator, return true. */
20597 cp_parser_declares_only_class_p (cp_parser *parser)
20599 /* If the next token is a `;' or a `,' then there is no
20601 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20602 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20605 /* Update the DECL_SPECS to reflect the storage class indicated by
20609 cp_parser_set_storage_class (cp_parser *parser,
20610 cp_decl_specifier_seq *decl_specs,
20612 location_t location)
20614 cp_storage_class storage_class;
20616 if (parser->in_unbraced_linkage_specification_p)
20618 error_at (location, "invalid use of %qD in linkage specification",
20619 ridpointers[keyword]);
20622 else if (decl_specs->storage_class != sc_none)
20624 decl_specs->conflicting_specifiers_p = true;
20628 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20629 && decl_specs->specs[(int) ds_thread])
20631 error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20632 decl_specs->specs[(int) ds_thread] = 0;
20638 storage_class = sc_auto;
20641 storage_class = sc_register;
20644 storage_class = sc_static;
20647 storage_class = sc_extern;
20650 storage_class = sc_mutable;
20653 gcc_unreachable ();
20655 decl_specs->storage_class = storage_class;
20657 /* A storage class specifier cannot be applied alongside a typedef
20658 specifier. If there is a typedef specifier present then set
20659 conflicting_specifiers_p which will trigger an error later
20660 on in grokdeclarator. */
20661 if (decl_specs->specs[(int)ds_typedef])
20662 decl_specs->conflicting_specifiers_p = true;
20665 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
20666 is true, the type is a user-defined type; otherwise it is a
20667 built-in type specified by a keyword. */
20670 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20672 location_t location,
20673 bool user_defined_p)
20675 decl_specs->any_specifiers_p = true;
20677 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20678 (with, for example, in "typedef int wchar_t;") we remember that
20679 this is what happened. In system headers, we ignore these
20680 declarations so that G++ can work with system headers that are not
20682 if (decl_specs->specs[(int) ds_typedef]
20684 && (type_spec == boolean_type_node
20685 || type_spec == char16_type_node
20686 || type_spec == char32_type_node
20687 || type_spec == wchar_type_node)
20688 && (decl_specs->type
20689 || decl_specs->specs[(int) ds_long]
20690 || decl_specs->specs[(int) ds_short]
20691 || decl_specs->specs[(int) ds_unsigned]
20692 || decl_specs->specs[(int) ds_signed]))
20694 decl_specs->redefined_builtin_type = type_spec;
20695 if (!decl_specs->type)
20697 decl_specs->type = type_spec;
20698 decl_specs->user_defined_type_p = false;
20699 decl_specs->type_location = location;
20702 else if (decl_specs->type)
20703 decl_specs->multiple_types_p = true;
20706 decl_specs->type = type_spec;
20707 decl_specs->user_defined_type_p = user_defined_p;
20708 decl_specs->redefined_builtin_type = NULL_TREE;
20709 decl_specs->type_location = location;
20713 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20714 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
20717 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20719 return decl_specifiers->specs[(int) ds_friend] != 0;
20722 /* Issue an error message indicating that TOKEN_DESC was expected.
20723 If KEYWORD is true, it indicated this function is called by
20724 cp_parser_require_keword and the required token can only be
20725 a indicated keyword. */
20728 cp_parser_required_error (cp_parser *parser,
20729 required_token token_desc,
20732 switch (token_desc)
20735 cp_parser_error (parser, "expected %<new%>");
20738 cp_parser_error (parser, "expected %<delete%>");
20741 cp_parser_error (parser, "expected %<return%>");
20744 cp_parser_error (parser, "expected %<while%>");
20747 cp_parser_error (parser, "expected %<extern%>");
20749 case RT_STATIC_ASSERT:
20750 cp_parser_error (parser, "expected %<static_assert%>");
20753 cp_parser_error (parser, "expected %<decltype%>");
20756 cp_parser_error (parser, "expected %<operator%>");
20759 cp_parser_error (parser, "expected %<class%>");
20762 cp_parser_error (parser, "expected %<template%>");
20765 cp_parser_error (parser, "expected %<namespace%>");
20768 cp_parser_error (parser, "expected %<using%>");
20771 cp_parser_error (parser, "expected %<asm%>");
20774 cp_parser_error (parser, "expected %<try%>");
20777 cp_parser_error (parser, "expected %<catch%>");
20780 cp_parser_error (parser, "expected %<throw%>");
20783 cp_parser_error (parser, "expected %<__label__%>");
20786 cp_parser_error (parser, "expected %<@try%>");
20788 case RT_AT_SYNCHRONIZED:
20789 cp_parser_error (parser, "expected %<@synchronized%>");
20792 cp_parser_error (parser, "expected %<@throw%>");
20799 switch (token_desc)
20802 cp_parser_error (parser, "expected %<;%>");
20804 case RT_OPEN_PAREN:
20805 cp_parser_error (parser, "expected %<(%>");
20807 case RT_CLOSE_BRACE:
20808 cp_parser_error (parser, "expected %<}%>");
20810 case RT_OPEN_BRACE:
20811 cp_parser_error (parser, "expected %<{%>");
20813 case RT_CLOSE_SQUARE:
20814 cp_parser_error (parser, "expected %<]%>");
20816 case RT_OPEN_SQUARE:
20817 cp_parser_error (parser, "expected %<[%>");
20820 cp_parser_error (parser, "expected %<,%>");
20823 cp_parser_error (parser, "expected %<::%>");
20826 cp_parser_error (parser, "expected %<<%>");
20829 cp_parser_error (parser, "expected %<>%>");
20832 cp_parser_error (parser, "expected %<=%>");
20835 cp_parser_error (parser, "expected %<...%>");
20838 cp_parser_error (parser, "expected %<*%>");
20841 cp_parser_error (parser, "expected %<~%>");
20844 cp_parser_error (parser, "expected %<:%>");
20846 case RT_COLON_SCOPE:
20847 cp_parser_error (parser, "expected %<:%> or %<::%>");
20849 case RT_CLOSE_PAREN:
20850 cp_parser_error (parser, "expected %<)%>");
20852 case RT_COMMA_CLOSE_PAREN:
20853 cp_parser_error (parser, "expected %<,%> or %<)%>");
20855 case RT_PRAGMA_EOL:
20856 cp_parser_error (parser, "expected end of line");
20859 cp_parser_error (parser, "expected identifier");
20862 cp_parser_error (parser, "expected selection-statement");
20864 case RT_INTERATION:
20865 cp_parser_error (parser, "expected iteration-statement");
20868 cp_parser_error (parser, "expected jump-statement");
20871 cp_parser_error (parser, "expected class-key");
20873 case RT_CLASS_TYPENAME_TEMPLATE:
20874 cp_parser_error (parser,
20875 "expected %<class%>, %<typename%>, or %<template%>");
20878 gcc_unreachable ();
20882 gcc_unreachable ();
20887 /* If the next token is of the indicated TYPE, consume it. Otherwise,
20888 issue an error message indicating that TOKEN_DESC was expected.
20890 Returns the token consumed, if the token had the appropriate type.
20891 Otherwise, returns NULL. */
20894 cp_parser_require (cp_parser* parser,
20895 enum cpp_ttype type,
20896 required_token token_desc)
20898 if (cp_lexer_next_token_is (parser->lexer, type))
20899 return cp_lexer_consume_token (parser->lexer);
20902 /* Output the MESSAGE -- unless we're parsing tentatively. */
20903 if (!cp_parser_simulate_error (parser))
20904 cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20909 /* An error message is produced if the next token is not '>'.
20910 All further tokens are skipped until the desired token is
20911 found or '{', '}', ';' or an unbalanced ')' or ']'. */
20914 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20916 /* Current level of '< ... >'. */
20917 unsigned level = 0;
20918 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
20919 unsigned nesting_depth = 0;
20921 /* Are we ready, yet? If not, issue error message. */
20922 if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
20925 /* Skip tokens until the desired token is found. */
20928 /* Peek at the next token. */
20929 switch (cp_lexer_peek_token (parser->lexer)->type)
20932 if (!nesting_depth)
20937 if (cxx_dialect == cxx98)
20938 /* C++0x views the `>>' operator as two `>' tokens, but
20941 else if (!nesting_depth && level-- == 0)
20943 /* We've hit a `>>' where the first `>' closes the
20944 template argument list, and the second `>' is
20945 spurious. Just consume the `>>' and stop; we've
20946 already produced at least one error. */
20947 cp_lexer_consume_token (parser->lexer);
20950 /* Fall through for C++0x, so we handle the second `>' in
20954 if (!nesting_depth && level-- == 0)
20956 /* We've reached the token we want, consume it and stop. */
20957 cp_lexer_consume_token (parser->lexer);
20962 case CPP_OPEN_PAREN:
20963 case CPP_OPEN_SQUARE:
20967 case CPP_CLOSE_PAREN:
20968 case CPP_CLOSE_SQUARE:
20969 if (nesting_depth-- == 0)
20974 case CPP_PRAGMA_EOL:
20975 case CPP_SEMICOLON:
20976 case CPP_OPEN_BRACE:
20977 case CPP_CLOSE_BRACE:
20978 /* The '>' was probably forgotten, don't look further. */
20985 /* Consume this token. */
20986 cp_lexer_consume_token (parser->lexer);
20990 /* If the next token is the indicated keyword, consume it. Otherwise,
20991 issue an error message indicating that TOKEN_DESC was expected.
20993 Returns the token consumed, if the token had the appropriate type.
20994 Otherwise, returns NULL. */
20997 cp_parser_require_keyword (cp_parser* parser,
20999 required_token token_desc)
21001 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21003 if (token && token->keyword != keyword)
21005 cp_parser_required_error (parser, token_desc, /*keyword=*/true);
21012 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21013 function-definition. */
21016 cp_parser_token_starts_function_definition_p (cp_token* token)
21018 return (/* An ordinary function-body begins with an `{'. */
21019 token->type == CPP_OPEN_BRACE
21020 /* A ctor-initializer begins with a `:'. */
21021 || token->type == CPP_COLON
21022 /* A function-try-block begins with `try'. */
21023 || token->keyword == RID_TRY
21024 /* The named return value extension begins with `return'. */
21025 || token->keyword == RID_RETURN);
21028 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21032 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21036 token = cp_lexer_peek_token (parser->lexer);
21037 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21040 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21041 C++0x) ending a template-argument. */
21044 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21048 token = cp_lexer_peek_token (parser->lexer);
21049 return (token->type == CPP_COMMA
21050 || token->type == CPP_GREATER
21051 || token->type == CPP_ELLIPSIS
21052 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21055 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21056 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
21059 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21064 token = cp_lexer_peek_nth_token (parser->lexer, n);
21065 if (token->type == CPP_LESS)
21067 /* Check for the sequence `<::' in the original code. It would be lexed as
21068 `[:', where `[' is a digraph, and there is no whitespace before
21070 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21073 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21074 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21080 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21081 or none_type otherwise. */
21083 static enum tag_types
21084 cp_parser_token_is_class_key (cp_token* token)
21086 switch (token->keyword)
21091 return record_type;
21100 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
21103 cp_parser_check_class_key (enum tag_types class_key, tree type)
21105 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21106 permerror (input_location, "%qs tag used in naming %q#T",
21107 class_key == union_type ? "union"
21108 : class_key == record_type ? "struct" : "class",
21112 /* Issue an error message if DECL is redeclared with different
21113 access than its original declaration [class.access.spec/3].
21114 This applies to nested classes and nested class templates.
21118 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21120 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21123 if ((TREE_PRIVATE (decl)
21124 != (current_access_specifier == access_private_node))
21125 || (TREE_PROTECTED (decl)
21126 != (current_access_specifier == access_protected_node)))
21127 error_at (location, "%qD redeclared with different access", decl);
21130 /* Look for the `template' keyword, as a syntactic disambiguator.
21131 Return TRUE iff it is present, in which case it will be
21135 cp_parser_optional_template_keyword (cp_parser *parser)
21137 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21139 /* The `template' keyword can only be used within templates;
21140 outside templates the parser can always figure out what is a
21141 template and what is not. */
21142 if (!processing_template_decl)
21144 cp_token *token = cp_lexer_peek_token (parser->lexer);
21145 error_at (token->location,
21146 "%<template%> (as a disambiguator) is only allowed "
21147 "within templates");
21148 /* If this part of the token stream is rescanned, the same
21149 error message would be generated. So, we purge the token
21150 from the stream. */
21151 cp_lexer_purge_token (parser->lexer);
21156 /* Consume the `template' keyword. */
21157 cp_lexer_consume_token (parser->lexer);
21165 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
21166 set PARSER->SCOPE, and perform other related actions. */
21169 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21172 struct tree_check *check_value;
21173 deferred_access_check *chk;
21174 VEC (deferred_access_check,gc) *checks;
21176 /* Get the stored value. */
21177 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21178 /* Perform any access checks that were deferred. */
21179 checks = check_value->checks;
21182 FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21183 perform_or_defer_access_check (chk->binfo,
21187 /* Set the scope from the stored value. */
21188 parser->scope = check_value->value;
21189 parser->qualifying_scope = check_value->qualifying_scope;
21190 parser->object_scope = NULL_TREE;
21193 /* Consume tokens up through a non-nested END token. Returns TRUE if we
21194 encounter the end of a block before what we were looking for. */
21197 cp_parser_cache_group (cp_parser *parser,
21198 enum cpp_ttype end,
21203 cp_token *token = cp_lexer_peek_token (parser->lexer);
21205 /* Abort a parenthesized expression if we encounter a semicolon. */
21206 if ((end == CPP_CLOSE_PAREN || depth == 0)
21207 && token->type == CPP_SEMICOLON)
21209 /* If we've reached the end of the file, stop. */
21210 if (token->type == CPP_EOF
21211 || (end != CPP_PRAGMA_EOL
21212 && token->type == CPP_PRAGMA_EOL))
21214 if (token->type == CPP_CLOSE_BRACE && depth == 0)
21215 /* We've hit the end of an enclosing block, so there's been some
21216 kind of syntax error. */
21219 /* Consume the token. */
21220 cp_lexer_consume_token (parser->lexer);
21221 /* See if it starts a new group. */
21222 if (token->type == CPP_OPEN_BRACE)
21224 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21225 /* In theory this should probably check end == '}', but
21226 cp_parser_save_member_function_body needs it to exit
21227 after either '}' or ')' when called with ')'. */
21231 else if (token->type == CPP_OPEN_PAREN)
21233 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21234 if (depth == 0 && end == CPP_CLOSE_PAREN)
21237 else if (token->type == CPP_PRAGMA)
21238 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21239 else if (token->type == end)
21244 /* Begin parsing tentatively. We always save tokens while parsing
21245 tentatively so that if the tentative parsing fails we can restore the
21249 cp_parser_parse_tentatively (cp_parser* parser)
21251 /* Enter a new parsing context. */
21252 parser->context = cp_parser_context_new (parser->context);
21253 /* Begin saving tokens. */
21254 cp_lexer_save_tokens (parser->lexer);
21255 /* In order to avoid repetitive access control error messages,
21256 access checks are queued up until we are no longer parsing
21258 push_deferring_access_checks (dk_deferred);
21261 /* Commit to the currently active tentative parse. */
21264 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21266 cp_parser_context *context;
21269 /* Mark all of the levels as committed. */
21270 lexer = parser->lexer;
21271 for (context = parser->context; context->next; context = context->next)
21273 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21275 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21276 while (!cp_lexer_saving_tokens (lexer))
21277 lexer = lexer->next;
21278 cp_lexer_commit_tokens (lexer);
21282 /* Abort the currently active tentative parse. All consumed tokens
21283 will be rolled back, and no diagnostics will be issued. */
21286 cp_parser_abort_tentative_parse (cp_parser* parser)
21288 gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21289 || errorcount > 0);
21290 cp_parser_simulate_error (parser);
21291 /* Now, pretend that we want to see if the construct was
21292 successfully parsed. */
21293 cp_parser_parse_definitely (parser);
21296 /* Stop parsing tentatively. If a parse error has occurred, restore the
21297 token stream. Otherwise, commit to the tokens we have consumed.
21298 Returns true if no error occurred; false otherwise. */
21301 cp_parser_parse_definitely (cp_parser* parser)
21303 bool error_occurred;
21304 cp_parser_context *context;
21306 /* Remember whether or not an error occurred, since we are about to
21307 destroy that information. */
21308 error_occurred = cp_parser_error_occurred (parser);
21309 /* Remove the topmost context from the stack. */
21310 context = parser->context;
21311 parser->context = context->next;
21312 /* If no parse errors occurred, commit to the tentative parse. */
21313 if (!error_occurred)
21315 /* Commit to the tokens read tentatively, unless that was
21317 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21318 cp_lexer_commit_tokens (parser->lexer);
21320 pop_to_parent_deferring_access_checks ();
21322 /* Otherwise, if errors occurred, roll back our state so that things
21323 are just as they were before we began the tentative parse. */
21326 cp_lexer_rollback_tokens (parser->lexer);
21327 pop_deferring_access_checks ();
21329 /* Add the context to the front of the free list. */
21330 context->next = cp_parser_context_free_list;
21331 cp_parser_context_free_list = context;
21333 return !error_occurred;
21336 /* Returns true if we are parsing tentatively and are not committed to
21337 this tentative parse. */
21340 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21342 return (cp_parser_parsing_tentatively (parser)
21343 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21346 /* Returns nonzero iff an error has occurred during the most recent
21347 tentative parse. */
21350 cp_parser_error_occurred (cp_parser* parser)
21352 return (cp_parser_parsing_tentatively (parser)
21353 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21356 /* Returns nonzero if GNU extensions are allowed. */
21359 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21361 return parser->allow_gnu_extensions_p;
21364 /* Objective-C++ Productions */
21367 /* Parse an Objective-C expression, which feeds into a primary-expression
21371 objc-message-expression
21372 objc-string-literal
21373 objc-encode-expression
21374 objc-protocol-expression
21375 objc-selector-expression
21377 Returns a tree representation of the expression. */
21380 cp_parser_objc_expression (cp_parser* parser)
21382 /* Try to figure out what kind of declaration is present. */
21383 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21387 case CPP_OPEN_SQUARE:
21388 return cp_parser_objc_message_expression (parser);
21390 case CPP_OBJC_STRING:
21391 kwd = cp_lexer_consume_token (parser->lexer);
21392 return objc_build_string_object (kwd->u.value);
21395 switch (kwd->keyword)
21397 case RID_AT_ENCODE:
21398 return cp_parser_objc_encode_expression (parser);
21400 case RID_AT_PROTOCOL:
21401 return cp_parser_objc_protocol_expression (parser);
21403 case RID_AT_SELECTOR:
21404 return cp_parser_objc_selector_expression (parser);
21410 error_at (kwd->location,
21411 "misplaced %<@%D%> Objective-C++ construct",
21413 cp_parser_skip_to_end_of_block_or_statement (parser);
21416 return error_mark_node;
21419 /* Parse an Objective-C message expression.
21421 objc-message-expression:
21422 [ objc-message-receiver objc-message-args ]
21424 Returns a representation of an Objective-C message. */
21427 cp_parser_objc_message_expression (cp_parser* parser)
21429 tree receiver, messageargs;
21431 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
21432 receiver = cp_parser_objc_message_receiver (parser);
21433 messageargs = cp_parser_objc_message_args (parser);
21434 cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21436 return objc_build_message_expr (receiver, messageargs);
21439 /* Parse an objc-message-receiver.
21441 objc-message-receiver:
21443 simple-type-specifier
21445 Returns a representation of the type or expression. */
21448 cp_parser_objc_message_receiver (cp_parser* parser)
21452 /* An Objective-C message receiver may be either (1) a type
21453 or (2) an expression. */
21454 cp_parser_parse_tentatively (parser);
21455 rcv = cp_parser_expression (parser, false, NULL);
21457 if (cp_parser_parse_definitely (parser))
21460 rcv = cp_parser_simple_type_specifier (parser,
21461 /*decl_specs=*/NULL,
21462 CP_PARSER_FLAGS_NONE);
21464 return objc_get_class_reference (rcv);
21467 /* Parse the arguments and selectors comprising an Objective-C message.
21472 objc-selector-args , objc-comma-args
21474 objc-selector-args:
21475 objc-selector [opt] : assignment-expression
21476 objc-selector-args objc-selector [opt] : assignment-expression
21479 assignment-expression
21480 objc-comma-args , assignment-expression
21482 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21483 selector arguments and TREE_VALUE containing a list of comma
21487 cp_parser_objc_message_args (cp_parser* parser)
21489 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21490 bool maybe_unary_selector_p = true;
21491 cp_token *token = cp_lexer_peek_token (parser->lexer);
21493 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21495 tree selector = NULL_TREE, arg;
21497 if (token->type != CPP_COLON)
21498 selector = cp_parser_objc_selector (parser);
21500 /* Detect if we have a unary selector. */
21501 if (maybe_unary_selector_p
21502 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21503 return build_tree_list (selector, NULL_TREE);
21505 maybe_unary_selector_p = false;
21506 cp_parser_require (parser, CPP_COLON, RT_COLON);
21507 arg = cp_parser_assignment_expression (parser, false, NULL);
21510 = chainon (sel_args,
21511 build_tree_list (selector, arg));
21513 token = cp_lexer_peek_token (parser->lexer);
21516 /* Handle non-selector arguments, if any. */
21517 while (token->type == CPP_COMMA)
21521 cp_lexer_consume_token (parser->lexer);
21522 arg = cp_parser_assignment_expression (parser, false, NULL);
21525 = chainon (addl_args,
21526 build_tree_list (NULL_TREE, arg));
21528 token = cp_lexer_peek_token (parser->lexer);
21531 if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21533 cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21534 return build_tree_list (error_mark_node, error_mark_node);
21537 return build_tree_list (sel_args, addl_args);
21540 /* Parse an Objective-C encode expression.
21542 objc-encode-expression:
21543 @encode objc-typename
21545 Returns an encoded representation of the type argument. */
21548 cp_parser_objc_encode_expression (cp_parser* parser)
21553 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
21554 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21555 token = cp_lexer_peek_token (parser->lexer);
21556 type = complete_type (cp_parser_type_id (parser));
21557 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21561 error_at (token->location,
21562 "%<@encode%> must specify a type as an argument");
21563 return error_mark_node;
21566 /* This happens if we find @encode(T) (where T is a template
21567 typename or something dependent on a template typename) when
21568 parsing a template. In that case, we can't compile it
21569 immediately, but we rather create an AT_ENCODE_EXPR which will
21570 need to be instantiated when the template is used.
21572 if (dependent_type_p (type))
21574 tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21575 TREE_READONLY (value) = 1;
21579 return objc_build_encode_expr (type);
21582 /* Parse an Objective-C @defs expression. */
21585 cp_parser_objc_defs_expression (cp_parser *parser)
21589 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
21590 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21591 name = cp_parser_identifier (parser);
21592 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21594 return objc_get_class_ivars (name);
21597 /* Parse an Objective-C protocol expression.
21599 objc-protocol-expression:
21600 @protocol ( identifier )
21602 Returns a representation of the protocol expression. */
21605 cp_parser_objc_protocol_expression (cp_parser* parser)
21609 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
21610 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21611 proto = cp_parser_identifier (parser);
21612 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21614 return objc_build_protocol_expr (proto);
21617 /* Parse an Objective-C selector expression.
21619 objc-selector-expression:
21620 @selector ( objc-method-signature )
21622 objc-method-signature:
21628 objc-selector-seq objc-selector :
21630 Returns a representation of the method selector. */
21633 cp_parser_objc_selector_expression (cp_parser* parser)
21635 tree sel_seq = NULL_TREE;
21636 bool maybe_unary_selector_p = true;
21638 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21640 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
21641 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21642 token = cp_lexer_peek_token (parser->lexer);
21644 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21645 || token->type == CPP_SCOPE)
21647 tree selector = NULL_TREE;
21649 if (token->type != CPP_COLON
21650 || token->type == CPP_SCOPE)
21651 selector = cp_parser_objc_selector (parser);
21653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21654 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21656 /* Detect if we have a unary selector. */
21657 if (maybe_unary_selector_p)
21659 sel_seq = selector;
21660 goto finish_selector;
21664 cp_parser_error (parser, "expected %<:%>");
21667 maybe_unary_selector_p = false;
21668 token = cp_lexer_consume_token (parser->lexer);
21670 if (token->type == CPP_SCOPE)
21673 = chainon (sel_seq,
21674 build_tree_list (selector, NULL_TREE));
21676 = chainon (sel_seq,
21677 build_tree_list (NULL_TREE, NULL_TREE));
21681 = chainon (sel_seq,
21682 build_tree_list (selector, NULL_TREE));
21684 token = cp_lexer_peek_token (parser->lexer);
21688 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21690 return objc_build_selector_expr (loc, sel_seq);
21693 /* Parse a list of identifiers.
21695 objc-identifier-list:
21697 objc-identifier-list , identifier
21699 Returns a TREE_LIST of identifier nodes. */
21702 cp_parser_objc_identifier_list (cp_parser* parser)
21708 identifier = cp_parser_identifier (parser);
21709 if (identifier == error_mark_node)
21710 return error_mark_node;
21712 list = build_tree_list (NULL_TREE, identifier);
21713 sep = cp_lexer_peek_token (parser->lexer);
21715 while (sep->type == CPP_COMMA)
21717 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
21718 identifier = cp_parser_identifier (parser);
21719 if (identifier == error_mark_node)
21722 list = chainon (list, build_tree_list (NULL_TREE,
21724 sep = cp_lexer_peek_token (parser->lexer);
21730 /* Parse an Objective-C alias declaration.
21732 objc-alias-declaration:
21733 @compatibility_alias identifier identifier ;
21735 This function registers the alias mapping with the Objective-C front end.
21736 It returns nothing. */
21739 cp_parser_objc_alias_declaration (cp_parser* parser)
21743 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
21744 alias = cp_parser_identifier (parser);
21745 orig = cp_parser_identifier (parser);
21746 objc_declare_alias (alias, orig);
21747 cp_parser_consume_semicolon_at_end_of_statement (parser);
21750 /* Parse an Objective-C class forward-declaration.
21752 objc-class-declaration:
21753 @class objc-identifier-list ;
21755 The function registers the forward declarations with the Objective-C
21756 front end. It returns nothing. */
21759 cp_parser_objc_class_declaration (cp_parser* parser)
21761 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
21766 id = cp_parser_identifier (parser);
21767 if (id == error_mark_node)
21770 objc_declare_class (id);
21772 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21773 cp_lexer_consume_token (parser->lexer);
21777 cp_parser_consume_semicolon_at_end_of_statement (parser);
21780 /* Parse a list of Objective-C protocol references.
21782 objc-protocol-refs-opt:
21783 objc-protocol-refs [opt]
21785 objc-protocol-refs:
21786 < objc-identifier-list >
21788 Returns a TREE_LIST of identifiers, if any. */
21791 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21793 tree protorefs = NULL_TREE;
21795 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21797 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
21798 protorefs = cp_parser_objc_identifier_list (parser);
21799 cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21805 /* Parse a Objective-C visibility specification. */
21808 cp_parser_objc_visibility_spec (cp_parser* parser)
21810 cp_token *vis = cp_lexer_peek_token (parser->lexer);
21812 switch (vis->keyword)
21814 case RID_AT_PRIVATE:
21815 objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21817 case RID_AT_PROTECTED:
21818 objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21820 case RID_AT_PUBLIC:
21821 objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21823 case RID_AT_PACKAGE:
21824 objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21830 /* Eat '@private'/'@protected'/'@public'. */
21831 cp_lexer_consume_token (parser->lexer);
21834 /* Parse an Objective-C method type. Return 'true' if it is a class
21835 (+) method, and 'false' if it is an instance (-) method. */
21838 cp_parser_objc_method_type (cp_parser* parser)
21840 if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21846 /* Parse an Objective-C protocol qualifier. */
21849 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21851 tree quals = NULL_TREE, node;
21852 cp_token *token = cp_lexer_peek_token (parser->lexer);
21854 node = token->u.value;
21856 while (node && TREE_CODE (node) == IDENTIFIER_NODE
21857 && (node == ridpointers [(int) RID_IN]
21858 || node == ridpointers [(int) RID_OUT]
21859 || node == ridpointers [(int) RID_INOUT]
21860 || node == ridpointers [(int) RID_BYCOPY]
21861 || node == ridpointers [(int) RID_BYREF]
21862 || node == ridpointers [(int) RID_ONEWAY]))
21864 quals = tree_cons (NULL_TREE, node, quals);
21865 cp_lexer_consume_token (parser->lexer);
21866 token = cp_lexer_peek_token (parser->lexer);
21867 node = token->u.value;
21873 /* Parse an Objective-C typename. */
21876 cp_parser_objc_typename (cp_parser* parser)
21878 tree type_name = NULL_TREE;
21880 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21882 tree proto_quals, cp_type = NULL_TREE;
21884 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
21885 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21887 /* An ObjC type name may consist of just protocol qualifiers, in which
21888 case the type shall default to 'id'. */
21889 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21891 cp_type = cp_parser_type_id (parser);
21893 /* If the type could not be parsed, an error has already
21894 been produced. For error recovery, behave as if it had
21895 not been specified, which will use the default type
21897 if (cp_type == error_mark_node)
21899 cp_type = NULL_TREE;
21900 /* We need to skip to the closing parenthesis as
21901 cp_parser_type_id() does not seem to do it for
21903 cp_parser_skip_to_closing_parenthesis (parser,
21904 /*recovering=*/true,
21905 /*or_comma=*/false,
21906 /*consume_paren=*/false);
21910 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21911 type_name = build_tree_list (proto_quals, cp_type);
21917 /* Check to see if TYPE refers to an Objective-C selector name. */
21920 cp_parser_objc_selector_p (enum cpp_ttype type)
21922 return (type == CPP_NAME || type == CPP_KEYWORD
21923 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
21924 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
21925 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
21926 || type == CPP_XOR || type == CPP_XOR_EQ);
21929 /* Parse an Objective-C selector. */
21932 cp_parser_objc_selector (cp_parser* parser)
21934 cp_token *token = cp_lexer_consume_token (parser->lexer);
21936 if (!cp_parser_objc_selector_p (token->type))
21938 error_at (token->location, "invalid Objective-C++ selector name");
21939 return error_mark_node;
21942 /* C++ operator names are allowed to appear in ObjC selectors. */
21943 switch (token->type)
21945 case CPP_AND_AND: return get_identifier ("and");
21946 case CPP_AND_EQ: return get_identifier ("and_eq");
21947 case CPP_AND: return get_identifier ("bitand");
21948 case CPP_OR: return get_identifier ("bitor");
21949 case CPP_COMPL: return get_identifier ("compl");
21950 case CPP_NOT: return get_identifier ("not");
21951 case CPP_NOT_EQ: return get_identifier ("not_eq");
21952 case CPP_OR_OR: return get_identifier ("or");
21953 case CPP_OR_EQ: return get_identifier ("or_eq");
21954 case CPP_XOR: return get_identifier ("xor");
21955 case CPP_XOR_EQ: return get_identifier ("xor_eq");
21956 default: return token->u.value;
21960 /* Parse an Objective-C params list. */
21963 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
21965 tree params = NULL_TREE;
21966 bool maybe_unary_selector_p = true;
21967 cp_token *token = cp_lexer_peek_token (parser->lexer);
21969 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21971 tree selector = NULL_TREE, type_name, identifier;
21972 tree parm_attr = NULL_TREE;
21974 if (token->keyword == RID_ATTRIBUTE)
21977 if (token->type != CPP_COLON)
21978 selector = cp_parser_objc_selector (parser);
21980 /* Detect if we have a unary selector. */
21981 if (maybe_unary_selector_p
21982 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21984 params = selector; /* Might be followed by attributes. */
21988 maybe_unary_selector_p = false;
21989 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
21991 /* Something went quite wrong. There should be a colon
21992 here, but there is not. Stop parsing parameters. */
21995 type_name = cp_parser_objc_typename (parser);
21996 /* New ObjC allows attributes on parameters too. */
21997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
21998 parm_attr = cp_parser_attributes_opt (parser);
21999 identifier = cp_parser_identifier (parser);
22003 objc_build_keyword_decl (selector,
22008 token = cp_lexer_peek_token (parser->lexer);
22011 if (params == NULL_TREE)
22013 cp_parser_error (parser, "objective-c++ method declaration is expected");
22014 return error_mark_node;
22017 /* We allow tail attributes for the method. */
22018 if (token->keyword == RID_ATTRIBUTE)
22020 *attributes = cp_parser_attributes_opt (parser);
22021 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22022 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22024 cp_parser_error (parser,
22025 "method attributes must be specified at the end");
22026 return error_mark_node;
22029 if (params == NULL_TREE)
22031 cp_parser_error (parser, "objective-c++ method declaration is expected");
22032 return error_mark_node;
22037 /* Parse the non-keyword Objective-C params. */
22040 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp,
22043 tree params = make_node (TREE_LIST);
22044 cp_token *token = cp_lexer_peek_token (parser->lexer);
22045 *ellipsisp = false; /* Initially, assume no ellipsis. */
22047 while (token->type == CPP_COMMA)
22049 cp_parameter_declarator *parmdecl;
22052 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22053 token = cp_lexer_peek_token (parser->lexer);
22055 if (token->type == CPP_ELLIPSIS)
22057 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
22059 token = cp_lexer_peek_token (parser->lexer);
22063 /* TODO: parse attributes for tail parameters. */
22064 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22065 parm = grokdeclarator (parmdecl->declarator,
22066 &parmdecl->decl_specifiers,
22067 PARM, /*initialized=*/0,
22068 /*attrlist=*/NULL);
22070 chainon (params, build_tree_list (NULL_TREE, parm));
22071 token = cp_lexer_peek_token (parser->lexer);
22074 /* We allow tail attributes for the method. */
22075 if (token->keyword == RID_ATTRIBUTE)
22077 if (*attributes == NULL_TREE)
22079 *attributes = cp_parser_attributes_opt (parser);
22080 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22081 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22085 /* We have an error, but parse the attributes, so that we can
22087 *attributes = cp_parser_attributes_opt (parser);
22089 cp_parser_error (parser,
22090 "method attributes must be specified at the end");
22091 return error_mark_node;
22097 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
22100 cp_parser_objc_interstitial_code (cp_parser* parser)
22102 cp_token *token = cp_lexer_peek_token (parser->lexer);
22104 /* If the next token is `extern' and the following token is a string
22105 literal, then we have a linkage specification. */
22106 if (token->keyword == RID_EXTERN
22107 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22108 cp_parser_linkage_specification (parser);
22109 /* Handle #pragma, if any. */
22110 else if (token->type == CPP_PRAGMA)
22111 cp_parser_pragma (parser, pragma_external);
22112 /* Allow stray semicolons. */
22113 else if (token->type == CPP_SEMICOLON)
22114 cp_lexer_consume_token (parser->lexer);
22115 /* Mark methods as optional or required, when building protocols. */
22116 else if (token->keyword == RID_AT_OPTIONAL)
22118 cp_lexer_consume_token (parser->lexer);
22119 objc_set_method_opt (true);
22121 else if (token->keyword == RID_AT_REQUIRED)
22123 cp_lexer_consume_token (parser->lexer);
22124 objc_set_method_opt (false);
22126 else if (token->keyword == RID_NAMESPACE)
22127 cp_parser_namespace_definition (parser);
22128 /* Other stray characters must generate errors. */
22129 else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22131 cp_lexer_consume_token (parser->lexer);
22132 error ("stray %qs between Objective-C++ methods",
22133 token->type == CPP_OPEN_BRACE ? "{" : "}");
22135 /* Finally, try to parse a block-declaration, or a function-definition. */
22137 cp_parser_block_declaration (parser, /*statement_p=*/false);
22140 /* Parse a method signature. */
22143 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22145 tree rettype, kwdparms, optparms;
22146 bool ellipsis = false;
22147 bool is_class_method;
22149 is_class_method = cp_parser_objc_method_type (parser);
22150 rettype = cp_parser_objc_typename (parser);
22151 *attributes = NULL_TREE;
22152 kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22153 if (kwdparms == error_mark_node)
22154 return error_mark_node;
22155 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22156 if (optparms == error_mark_node)
22157 return error_mark_node;
22159 return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22163 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22166 cp_lexer_save_tokens (parser->lexer);
22167 tattr = cp_parser_attributes_opt (parser);
22168 gcc_assert (tattr) ;
22170 /* If the attributes are followed by a method introducer, this is not allowed.
22171 Dump the attributes and flag the situation. */
22172 if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22173 || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22176 /* Otherwise, the attributes introduce some interstitial code, possibly so
22177 rewind to allow that check. */
22178 cp_lexer_rollback_tokens (parser->lexer);
22182 /* Parse an Objective-C method prototype list. */
22185 cp_parser_objc_method_prototype_list (cp_parser* parser)
22187 cp_token *token = cp_lexer_peek_token (parser->lexer);
22189 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22191 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22193 tree attributes, sig;
22194 bool is_class_method;
22195 if (token->type == CPP_PLUS)
22196 is_class_method = true;
22198 is_class_method = false;
22199 sig = cp_parser_objc_method_signature (parser, &attributes);
22200 if (sig == error_mark_node)
22202 cp_parser_skip_to_end_of_block_or_statement (parser);
22203 token = cp_lexer_peek_token (parser->lexer);
22206 objc_add_method_declaration (is_class_method, sig, attributes);
22207 cp_parser_consume_semicolon_at_end_of_statement (parser);
22209 else if (token->keyword == RID_AT_PROPERTY)
22210 cp_parser_objc_at_property_declaration (parser);
22211 else if (token->keyword == RID_ATTRIBUTE
22212 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22213 warning_at (cp_lexer_peek_token (parser->lexer)->location,
22215 "prefix attributes are ignored for methods");
22217 /* Allow for interspersed non-ObjC++ code. */
22218 cp_parser_objc_interstitial_code (parser);
22220 token = cp_lexer_peek_token (parser->lexer);
22223 if (token->type != CPP_EOF)
22224 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22226 cp_parser_error (parser, "expected %<@end%>");
22228 objc_finish_interface ();
22231 /* Parse an Objective-C method definition list. */
22234 cp_parser_objc_method_definition_list (cp_parser* parser)
22236 cp_token *token = cp_lexer_peek_token (parser->lexer);
22238 while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22242 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22245 tree sig, attribute;
22246 bool is_class_method;
22247 if (token->type == CPP_PLUS)
22248 is_class_method = true;
22250 is_class_method = false;
22251 push_deferring_access_checks (dk_deferred);
22252 sig = cp_parser_objc_method_signature (parser, &attribute);
22253 if (sig == error_mark_node)
22255 cp_parser_skip_to_end_of_block_or_statement (parser);
22256 token = cp_lexer_peek_token (parser->lexer);
22259 objc_start_method_definition (is_class_method, sig, attribute,
22262 /* For historical reasons, we accept an optional semicolon. */
22263 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22264 cp_lexer_consume_token (parser->lexer);
22266 ptk = cp_lexer_peek_token (parser->lexer);
22267 if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS
22268 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22270 perform_deferred_access_checks ();
22271 stop_deferring_access_checks ();
22272 meth = cp_parser_function_definition_after_declarator (parser,
22274 pop_deferring_access_checks ();
22275 objc_finish_method_definition (meth);
22278 /* The following case will be removed once @synthesize is
22279 completely implemented. */
22280 else if (token->keyword == RID_AT_PROPERTY)
22281 cp_parser_objc_at_property_declaration (parser);
22282 else if (token->keyword == RID_AT_SYNTHESIZE)
22283 cp_parser_objc_at_synthesize_declaration (parser);
22284 else if (token->keyword == RID_AT_DYNAMIC)
22285 cp_parser_objc_at_dynamic_declaration (parser);
22286 else if (token->keyword == RID_ATTRIBUTE
22287 && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22288 warning_at (token->location, OPT_Wattributes,
22289 "prefix attributes are ignored for methods");
22291 /* Allow for interspersed non-ObjC++ code. */
22292 cp_parser_objc_interstitial_code (parser);
22294 token = cp_lexer_peek_token (parser->lexer);
22297 if (token->type != CPP_EOF)
22298 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22300 cp_parser_error (parser, "expected %<@end%>");
22302 objc_finish_implementation ();
22305 /* Parse Objective-C ivars. */
22308 cp_parser_objc_class_ivars (cp_parser* parser)
22310 cp_token *token = cp_lexer_peek_token (parser->lexer);
22312 if (token->type != CPP_OPEN_BRACE)
22313 return; /* No ivars specified. */
22315 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
22316 token = cp_lexer_peek_token (parser->lexer);
22318 while (token->type != CPP_CLOSE_BRACE
22319 && token->keyword != RID_AT_END && token->type != CPP_EOF)
22321 cp_decl_specifier_seq declspecs;
22322 int decl_class_or_enum_p;
22323 tree prefix_attributes;
22325 cp_parser_objc_visibility_spec (parser);
22327 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22330 cp_parser_decl_specifier_seq (parser,
22331 CP_PARSER_FLAGS_OPTIONAL,
22333 &decl_class_or_enum_p);
22335 /* auto, register, static, extern, mutable. */
22336 if (declspecs.storage_class != sc_none)
22338 cp_parser_error (parser, "invalid type for instance variable");
22339 declspecs.storage_class = sc_none;
22343 if (declspecs.specs[(int) ds_thread])
22345 cp_parser_error (parser, "invalid type for instance variable");
22346 declspecs.specs[(int) ds_thread] = 0;
22350 if (declspecs.specs[(int) ds_typedef])
22352 cp_parser_error (parser, "invalid type for instance variable");
22353 declspecs.specs[(int) ds_typedef] = 0;
22356 prefix_attributes = declspecs.attributes;
22357 declspecs.attributes = NULL_TREE;
22359 /* Keep going until we hit the `;' at the end of the
22361 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22363 tree width = NULL_TREE, attributes, first_attribute, decl;
22364 cp_declarator *declarator = NULL;
22365 int ctor_dtor_or_conv_p;
22367 /* Check for a (possibly unnamed) bitfield declaration. */
22368 token = cp_lexer_peek_token (parser->lexer);
22369 if (token->type == CPP_COLON)
22372 if (token->type == CPP_NAME
22373 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22376 /* Get the name of the bitfield. */
22377 declarator = make_id_declarator (NULL_TREE,
22378 cp_parser_identifier (parser),
22382 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22383 /* Get the width of the bitfield. */
22385 = cp_parser_constant_expression (parser,
22386 /*allow_non_constant=*/false,
22391 /* Parse the declarator. */
22393 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22394 &ctor_dtor_or_conv_p,
22395 /*parenthesized_p=*/NULL,
22396 /*member_p=*/false);
22399 /* Look for attributes that apply to the ivar. */
22400 attributes = cp_parser_attributes_opt (parser);
22401 /* Remember which attributes are prefix attributes and
22403 first_attribute = attributes;
22404 /* Combine the attributes. */
22405 attributes = chainon (prefix_attributes, attributes);
22408 /* Create the bitfield declaration. */
22409 decl = grokbitfield (declarator, &declspecs,
22413 decl = grokfield (declarator, &declspecs,
22414 NULL_TREE, /*init_const_expr_p=*/false,
22415 NULL_TREE, attributes);
22417 /* Add the instance variable. */
22418 objc_add_instance_variable (decl);
22420 /* Reset PREFIX_ATTRIBUTES. */
22421 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22422 attributes = TREE_CHAIN (attributes);
22424 TREE_CHAIN (attributes) = NULL_TREE;
22426 token = cp_lexer_peek_token (parser->lexer);
22428 if (token->type == CPP_COMMA)
22430 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22436 cp_parser_consume_semicolon_at_end_of_statement (parser);
22437 token = cp_lexer_peek_token (parser->lexer);
22440 if (token->keyword == RID_AT_END)
22441 cp_parser_error (parser, "expected %<}%>");
22443 /* Do not consume the RID_AT_END, so it will be read again as terminating
22444 the @interface of @implementation. */
22445 if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22446 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
22448 /* For historical reasons, we accept an optional semicolon. */
22449 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22450 cp_lexer_consume_token (parser->lexer);
22453 /* Parse an Objective-C protocol declaration. */
22456 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22458 tree proto, protorefs;
22461 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
22462 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22464 tok = cp_lexer_peek_token (parser->lexer);
22465 error_at (tok->location, "identifier expected after %<@protocol%>");
22466 cp_parser_consume_semicolon_at_end_of_statement (parser);
22470 /* See if we have a forward declaration or a definition. */
22471 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22473 /* Try a forward declaration first. */
22474 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22480 id = cp_parser_identifier (parser);
22481 if (id == error_mark_node)
22484 objc_declare_protocol (id, attributes);
22486 if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22487 cp_lexer_consume_token (parser->lexer);
22491 cp_parser_consume_semicolon_at_end_of_statement (parser);
22494 /* Ok, we got a full-fledged definition (or at least should). */
22497 proto = cp_parser_identifier (parser);
22498 protorefs = cp_parser_objc_protocol_refs_opt (parser);
22499 objc_start_protocol (proto, protorefs, attributes);
22500 cp_parser_objc_method_prototype_list (parser);
22504 /* Parse an Objective-C superclass or category. */
22507 cp_parser_objc_superclass_or_category (cp_parser *parser,
22510 tree *categ, bool *is_class_extension)
22512 cp_token *next = cp_lexer_peek_token (parser->lexer);
22514 *super = *categ = NULL_TREE;
22515 *is_class_extension = false;
22516 if (next->type == CPP_COLON)
22518 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
22519 *super = cp_parser_identifier (parser);
22521 else if (next->type == CPP_OPEN_PAREN)
22523 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
22525 /* If there is no category name, and this is an @interface, we
22526 have a class extension. */
22527 if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22529 *categ = NULL_TREE;
22530 *is_class_extension = true;
22533 *categ = cp_parser_identifier (parser);
22535 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22539 /* Parse an Objective-C class interface. */
22542 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22544 tree name, super, categ, protos;
22545 bool is_class_extension;
22547 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
22548 name = cp_parser_identifier (parser);
22549 if (name == error_mark_node)
22551 /* It's hard to recover because even if valid @interface stuff
22552 is to follow, we can't compile it (or validate it) if we
22553 don't even know which class it refers to. Let's assume this
22554 was a stray '@interface' token in the stream and skip it.
22558 cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22559 &is_class_extension);
22560 protos = cp_parser_objc_protocol_refs_opt (parser);
22562 /* We have either a class or a category on our hands. */
22563 if (categ || is_class_extension)
22564 objc_start_category_interface (name, categ, protos, attributes);
22567 objc_start_class_interface (name, super, protos, attributes);
22568 /* Handle instance variable declarations, if any. */
22569 cp_parser_objc_class_ivars (parser);
22570 objc_continue_interface ();
22573 cp_parser_objc_method_prototype_list (parser);
22576 /* Parse an Objective-C class implementation. */
22579 cp_parser_objc_class_implementation (cp_parser* parser)
22581 tree name, super, categ;
22582 bool is_class_extension;
22584 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
22585 name = cp_parser_identifier (parser);
22586 if (name == error_mark_node)
22588 /* It's hard to recover because even if valid @implementation
22589 stuff is to follow, we can't compile it (or validate it) if
22590 we don't even know which class it refers to. Let's assume
22591 this was a stray '@implementation' token in the stream and
22596 cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22597 &is_class_extension);
22599 /* We have either a class or a category on our hands. */
22601 objc_start_category_implementation (name, categ);
22604 objc_start_class_implementation (name, super);
22605 /* Handle instance variable declarations, if any. */
22606 cp_parser_objc_class_ivars (parser);
22607 objc_continue_implementation ();
22610 cp_parser_objc_method_definition_list (parser);
22613 /* Consume the @end token and finish off the implementation. */
22616 cp_parser_objc_end_implementation (cp_parser* parser)
22618 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
22619 objc_finish_implementation ();
22622 /* Parse an Objective-C declaration. */
22625 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22627 /* Try to figure out what kind of declaration is present. */
22628 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22631 switch (kwd->keyword)
22636 error_at (kwd->location, "attributes may not be specified before"
22637 " the %<@%D%> Objective-C++ keyword",
22641 case RID_AT_IMPLEMENTATION:
22642 warning_at (kwd->location, OPT_Wattributes,
22643 "prefix attributes are ignored before %<@%D%>",
22650 switch (kwd->keyword)
22653 cp_parser_objc_alias_declaration (parser);
22656 cp_parser_objc_class_declaration (parser);
22658 case RID_AT_PROTOCOL:
22659 cp_parser_objc_protocol_declaration (parser, attributes);
22661 case RID_AT_INTERFACE:
22662 cp_parser_objc_class_interface (parser, attributes);
22664 case RID_AT_IMPLEMENTATION:
22665 cp_parser_objc_class_implementation (parser);
22668 cp_parser_objc_end_implementation (parser);
22671 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22673 cp_parser_skip_to_end_of_block_or_statement (parser);
22677 /* Parse an Objective-C try-catch-finally statement.
22679 objc-try-catch-finally-stmt:
22680 @try compound-statement objc-catch-clause-seq [opt]
22681 objc-finally-clause [opt]
22683 objc-catch-clause-seq:
22684 objc-catch-clause objc-catch-clause-seq [opt]
22687 @catch ( objc-exception-declaration ) compound-statement
22689 objc-finally-clause:
22690 @finally compound-statement
22692 objc-exception-declaration:
22693 parameter-declaration
22696 where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22700 PS: This function is identical to c_parser_objc_try_catch_finally_statement
22701 for C. Keep them in sync. */
22704 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22706 location_t location;
22709 cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22710 location = cp_lexer_peek_token (parser->lexer)->location;
22711 objc_maybe_warn_exceptions (location);
22712 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22713 node, lest it get absorbed into the surrounding block. */
22714 stmt = push_stmt_list ();
22715 cp_parser_compound_statement (parser, NULL, false, false);
22716 objc_begin_try_stmt (location, pop_stmt_list (stmt));
22718 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22720 cp_parameter_declarator *parm;
22721 tree parameter_declaration = error_mark_node;
22722 bool seen_open_paren = false;
22724 cp_lexer_consume_token (parser->lexer);
22725 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22726 seen_open_paren = true;
22727 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22729 /* We have "@catch (...)" (where the '...' are literally
22730 what is in the code). Skip the '...'.
22731 parameter_declaration is set to NULL_TREE, and
22732 objc_being_catch_clauses() knows that that means
22734 cp_lexer_consume_token (parser->lexer);
22735 parameter_declaration = NULL_TREE;
22739 /* We have "@catch (NSException *exception)" or something
22740 like that. Parse the parameter declaration. */
22741 parm = cp_parser_parameter_declaration (parser, false, NULL);
22743 parameter_declaration = error_mark_node;
22745 parameter_declaration = grokdeclarator (parm->declarator,
22746 &parm->decl_specifiers,
22747 PARM, /*initialized=*/0,
22748 /*attrlist=*/NULL);
22750 if (seen_open_paren)
22751 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22754 /* If there was no open parenthesis, we are recovering from
22755 an error, and we are trying to figure out what mistake
22756 the user has made. */
22758 /* If there is an immediate closing parenthesis, the user
22759 probably forgot the opening one (ie, they typed "@catch
22760 NSException *e)". Parse the closing parenthesis and keep
22762 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22763 cp_lexer_consume_token (parser->lexer);
22765 /* If these is no immediate closing parenthesis, the user
22766 probably doesn't know that parenthesis are required at
22767 all (ie, they typed "@catch NSException *e"). So, just
22768 forget about the closing parenthesis and keep going. */
22770 objc_begin_catch_clause (parameter_declaration);
22771 cp_parser_compound_statement (parser, NULL, false, false);
22772 objc_finish_catch_clause ();
22774 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22776 cp_lexer_consume_token (parser->lexer);
22777 location = cp_lexer_peek_token (parser->lexer)->location;
22778 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22779 node, lest it get absorbed into the surrounding block. */
22780 stmt = push_stmt_list ();
22781 cp_parser_compound_statement (parser, NULL, false, false);
22782 objc_build_finally_clause (location, pop_stmt_list (stmt));
22785 return objc_finish_try_stmt ();
22788 /* Parse an Objective-C synchronized statement.
22790 objc-synchronized-stmt:
22791 @synchronized ( expression ) compound-statement
22793 Returns NULL_TREE. */
22796 cp_parser_objc_synchronized_statement (cp_parser *parser)
22798 location_t location;
22801 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22803 location = cp_lexer_peek_token (parser->lexer)->location;
22804 objc_maybe_warn_exceptions (location);
22805 cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22806 lock = cp_parser_expression (parser, false, NULL);
22807 cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22809 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22810 node, lest it get absorbed into the surrounding block. */
22811 stmt = push_stmt_list ();
22812 cp_parser_compound_statement (parser, NULL, false, false);
22814 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22817 /* Parse an Objective-C throw statement.
22820 @throw assignment-expression [opt] ;
22822 Returns a constructed '@throw' statement. */
22825 cp_parser_objc_throw_statement (cp_parser *parser)
22827 tree expr = NULL_TREE;
22828 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22830 cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22832 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22833 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22835 cp_parser_consume_semicolon_at_end_of_statement (parser);
22837 return objc_build_throw_stmt (loc, expr);
22840 /* Parse an Objective-C statement. */
22843 cp_parser_objc_statement (cp_parser * parser)
22845 /* Try to figure out what kind of declaration is present. */
22846 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22848 switch (kwd->keyword)
22851 return cp_parser_objc_try_catch_finally_statement (parser);
22852 case RID_AT_SYNCHRONIZED:
22853 return cp_parser_objc_synchronized_statement (parser);
22855 return cp_parser_objc_throw_statement (parser);
22857 error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22859 cp_parser_skip_to_end_of_block_or_statement (parser);
22862 return error_mark_node;
22865 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to
22866 look ahead to see if an objc keyword follows the attributes. This
22867 is to detect the use of prefix attributes on ObjC @interface and
22871 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22873 cp_lexer_save_tokens (parser->lexer);
22874 *attrib = cp_parser_attributes_opt (parser);
22875 gcc_assert (*attrib);
22876 if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22878 cp_lexer_commit_tokens (parser->lexer);
22881 cp_lexer_rollback_tokens (parser->lexer);
22885 /* This routine is a minimal replacement for
22886 c_parser_struct_declaration () used when parsing the list of
22887 types/names or ObjC++ properties. For example, when parsing the
22890 @property (readonly) int a, b, c;
22892 this function is responsible for parsing "int a, int b, int c" and
22893 returning the declarations as CHAIN of DECLs.
22895 TODO: Share this code with cp_parser_objc_class_ivars. It's very
22896 similar parsing. */
22898 cp_parser_objc_struct_declaration (cp_parser *parser)
22900 tree decls = NULL_TREE;
22901 cp_decl_specifier_seq declspecs;
22902 int decl_class_or_enum_p;
22903 tree prefix_attributes;
22905 cp_parser_decl_specifier_seq (parser,
22906 CP_PARSER_FLAGS_NONE,
22908 &decl_class_or_enum_p);
22910 if (declspecs.type == error_mark_node)
22911 return error_mark_node;
22913 /* auto, register, static, extern, mutable. */
22914 if (declspecs.storage_class != sc_none)
22916 cp_parser_error (parser, "invalid type for property");
22917 declspecs.storage_class = sc_none;
22921 if (declspecs.specs[(int) ds_thread])
22923 cp_parser_error (parser, "invalid type for property");
22924 declspecs.specs[(int) ds_thread] = 0;
22928 if (declspecs.specs[(int) ds_typedef])
22930 cp_parser_error (parser, "invalid type for property");
22931 declspecs.specs[(int) ds_typedef] = 0;
22934 prefix_attributes = declspecs.attributes;
22935 declspecs.attributes = NULL_TREE;
22937 /* Keep going until we hit the `;' at the end of the declaration. */
22938 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22940 tree attributes, first_attribute, decl;
22941 cp_declarator *declarator;
22944 /* Parse the declarator. */
22945 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22946 NULL, NULL, false);
22948 /* Look for attributes that apply to the ivar. */
22949 attributes = cp_parser_attributes_opt (parser);
22950 /* Remember which attributes are prefix attributes and
22952 first_attribute = attributes;
22953 /* Combine the attributes. */
22954 attributes = chainon (prefix_attributes, attributes);
22956 decl = grokfield (declarator, &declspecs,
22957 NULL_TREE, /*init_const_expr_p=*/false,
22958 NULL_TREE, attributes);
22960 if (decl == error_mark_node || decl == NULL_TREE)
22961 return error_mark_node;
22963 /* Reset PREFIX_ATTRIBUTES. */
22964 while (attributes && TREE_CHAIN (attributes) != first_attribute)
22965 attributes = TREE_CHAIN (attributes);
22967 TREE_CHAIN (attributes) = NULL_TREE;
22969 DECL_CHAIN (decl) = decls;
22972 token = cp_lexer_peek_token (parser->lexer);
22973 if (token->type == CPP_COMMA)
22975 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
22984 /* Parse an Objective-C @property declaration. The syntax is:
22986 objc-property-declaration:
22987 '@property' objc-property-attributes[opt] struct-declaration ;
22989 objc-property-attributes:
22990 '(' objc-property-attribute-list ')'
22992 objc-property-attribute-list:
22993 objc-property-attribute
22994 objc-property-attribute-list, objc-property-attribute
22996 objc-property-attribute
22997 'getter' = identifier
22998 'setter' = identifier
23007 @property NSString *name;
23008 @property (readonly) id object;
23009 @property (retain, nonatomic, getter=getTheName) id name;
23010 @property int a, b, c;
23012 PS: This function is identical to
23013 c_parser_objc_at_property_declaration for C. Keep them in sync. */
23015 cp_parser_objc_at_property_declaration (cp_parser *parser)
23017 /* The following variables hold the attributes of the properties as
23018 parsed. They are 'false' or 'NULL_TREE' if the attribute was not
23019 seen. When we see an attribute, we set them to 'true' (if they
23020 are boolean properties) or to the identifier (if they have an
23021 argument, ie, for getter and setter). Note that here we only
23022 parse the list of attributes, check the syntax and accumulate the
23023 attributes that we find. objc_add_property_declaration() will
23024 then process the information. */
23025 bool property_assign = false;
23026 bool property_copy = false;
23027 tree property_getter_ident = NULL_TREE;
23028 bool property_nonatomic = false;
23029 bool property_readonly = false;
23030 bool property_readwrite = false;
23031 bool property_retain = false;
23032 tree property_setter_ident = NULL_TREE;
23034 /* 'properties' is the list of properties that we read. Usually a
23035 single one, but maybe more (eg, in "@property int a, b, c;" there
23040 loc = cp_lexer_peek_token (parser->lexer)->location;
23042 cp_lexer_consume_token (parser->lexer); /* Eat '@property'. */
23044 /* Parse the optional attribute list... */
23045 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23048 cp_lexer_consume_token (parser->lexer);
23052 bool syntax_error = false;
23053 cp_token *token = cp_lexer_peek_token (parser->lexer);
23056 if (token->type != CPP_NAME)
23058 cp_parser_error (parser, "expected identifier");
23061 keyword = C_RID_CODE (token->u.value);
23062 cp_lexer_consume_token (parser->lexer);
23065 case RID_ASSIGN: property_assign = true; break;
23066 case RID_COPY: property_copy = true; break;
23067 case RID_NONATOMIC: property_nonatomic = true; break;
23068 case RID_READONLY: property_readonly = true; break;
23069 case RID_READWRITE: property_readwrite = true; break;
23070 case RID_RETAIN: property_retain = true; break;
23074 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23076 if (keyword == RID_GETTER)
23077 cp_parser_error (parser,
23078 "missing %<=%> (after %<getter%> attribute)");
23080 cp_parser_error (parser,
23081 "missing %<=%> (after %<setter%> attribute)");
23082 syntax_error = true;
23085 cp_lexer_consume_token (parser->lexer); /* eat the = */
23086 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23088 cp_parser_error (parser, "expected identifier");
23089 syntax_error = true;
23092 if (keyword == RID_SETTER)
23094 if (property_setter_ident != NULL_TREE)
23095 cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23097 property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23098 cp_lexer_consume_token (parser->lexer);
23099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23100 cp_parser_error (parser, "setter name must terminate with %<:%>");
23102 cp_lexer_consume_token (parser->lexer);
23106 if (property_getter_ident != NULL_TREE)
23107 cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23109 property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23110 cp_lexer_consume_token (parser->lexer);
23114 cp_parser_error (parser, "unknown property attribute");
23115 syntax_error = true;
23122 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23123 cp_lexer_consume_token (parser->lexer);
23128 /* FIXME: "@property (setter, assign);" will generate a spurious
23129 "error: expected ‘)’ before ‘,’ token". This is because
23130 cp_parser_require, unlike the C counterpart, will produce an
23131 error even if we are in error recovery. */
23132 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23134 cp_parser_skip_to_closing_parenthesis (parser,
23135 /*recovering=*/true,
23136 /*or_comma=*/false,
23137 /*consume_paren=*/true);
23141 /* ... and the property declaration(s). */
23142 properties = cp_parser_objc_struct_declaration (parser);
23144 if (properties == error_mark_node)
23146 cp_parser_skip_to_end_of_statement (parser);
23147 /* If the next token is now a `;', consume it. */
23148 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23149 cp_lexer_consume_token (parser->lexer);
23153 if (properties == NULL_TREE)
23154 cp_parser_error (parser, "expected identifier");
23157 /* Comma-separated properties are chained together in
23158 reverse order; add them one by one. */
23159 properties = nreverse (properties);
23161 for (; properties; properties = TREE_CHAIN (properties))
23162 objc_add_property_declaration (loc, copy_node (properties),
23163 property_readonly, property_readwrite,
23164 property_assign, property_retain,
23165 property_copy, property_nonatomic,
23166 property_getter_ident, property_setter_ident);
23169 cp_parser_consume_semicolon_at_end_of_statement (parser);
23172 /* Parse an Objective-C++ @synthesize declaration. The syntax is:
23174 objc-synthesize-declaration:
23175 @synthesize objc-synthesize-identifier-list ;
23177 objc-synthesize-identifier-list:
23178 objc-synthesize-identifier
23179 objc-synthesize-identifier-list, objc-synthesize-identifier
23181 objc-synthesize-identifier
23183 identifier = identifier
23186 @synthesize MyProperty;
23187 @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23189 PS: This function is identical to c_parser_objc_at_synthesize_declaration
23190 for C. Keep them in sync.
23193 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23195 tree list = NULL_TREE;
23197 loc = cp_lexer_peek_token (parser->lexer)->location;
23199 cp_lexer_consume_token (parser->lexer); /* Eat '@synthesize'. */
23202 tree property, ivar;
23203 property = cp_parser_identifier (parser);
23204 if (property == error_mark_node)
23206 cp_parser_consume_semicolon_at_end_of_statement (parser);
23209 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23211 cp_lexer_consume_token (parser->lexer);
23212 ivar = cp_parser_identifier (parser);
23213 if (ivar == error_mark_node)
23215 cp_parser_consume_semicolon_at_end_of_statement (parser);
23221 list = chainon (list, build_tree_list (ivar, property));
23222 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23223 cp_lexer_consume_token (parser->lexer);
23227 cp_parser_consume_semicolon_at_end_of_statement (parser);
23228 objc_add_synthesize_declaration (loc, list);
23231 /* Parse an Objective-C++ @dynamic declaration. The syntax is:
23233 objc-dynamic-declaration:
23234 @dynamic identifier-list ;
23237 @dynamic MyProperty;
23238 @dynamic MyProperty, AnotherProperty;
23240 PS: This function is identical to c_parser_objc_at_dynamic_declaration
23241 for C. Keep them in sync.
23244 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23246 tree list = NULL_TREE;
23248 loc = cp_lexer_peek_token (parser->lexer)->location;
23250 cp_lexer_consume_token (parser->lexer); /* Eat '@dynamic'. */
23254 property = cp_parser_identifier (parser);
23255 if (property == error_mark_node)
23257 cp_parser_consume_semicolon_at_end_of_statement (parser);
23260 list = chainon (list, build_tree_list (NULL, property));
23261 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23262 cp_lexer_consume_token (parser->lexer);
23266 cp_parser_consume_semicolon_at_end_of_statement (parser);
23267 objc_add_dynamic_declaration (loc, list);
23271 /* OpenMP 2.5 parsing routines. */
23273 /* Returns name of the next clause.
23274 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23275 the token is not consumed. Otherwise appropriate pragma_omp_clause is
23276 returned and the token is consumed. */
23278 static pragma_omp_clause
23279 cp_parser_omp_clause_name (cp_parser *parser)
23281 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23283 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23284 result = PRAGMA_OMP_CLAUSE_IF;
23285 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23286 result = PRAGMA_OMP_CLAUSE_DEFAULT;
23287 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23288 result = PRAGMA_OMP_CLAUSE_PRIVATE;
23289 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23291 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23292 const char *p = IDENTIFIER_POINTER (id);
23297 if (!strcmp ("collapse", p))
23298 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23299 else if (!strcmp ("copyin", p))
23300 result = PRAGMA_OMP_CLAUSE_COPYIN;
23301 else if (!strcmp ("copyprivate", p))
23302 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23305 if (!strcmp ("firstprivate", p))
23306 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23309 if (!strcmp ("lastprivate", p))
23310 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23313 if (!strcmp ("nowait", p))
23314 result = PRAGMA_OMP_CLAUSE_NOWAIT;
23315 else if (!strcmp ("num_threads", p))
23316 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23319 if (!strcmp ("ordered", p))
23320 result = PRAGMA_OMP_CLAUSE_ORDERED;
23323 if (!strcmp ("reduction", p))
23324 result = PRAGMA_OMP_CLAUSE_REDUCTION;
23327 if (!strcmp ("schedule", p))
23328 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23329 else if (!strcmp ("shared", p))
23330 result = PRAGMA_OMP_CLAUSE_SHARED;
23333 if (!strcmp ("untied", p))
23334 result = PRAGMA_OMP_CLAUSE_UNTIED;
23339 if (result != PRAGMA_OMP_CLAUSE_NONE)
23340 cp_lexer_consume_token (parser->lexer);
23345 /* Validate that a clause of the given type does not already exist. */
23348 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23349 const char *name, location_t location)
23353 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23354 if (OMP_CLAUSE_CODE (c) == code)
23356 error_at (location, "too many %qs clauses", name);
23364 variable-list , identifier
23366 In addition, we match a closing parenthesis. An opening parenthesis
23367 will have been consumed by the caller.
23369 If KIND is nonzero, create the appropriate node and install the decl
23370 in OMP_CLAUSE_DECL and add the node to the head of the list.
23372 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23373 return the list created. */
23376 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23384 token = cp_lexer_peek_token (parser->lexer);
23385 name = cp_parser_id_expression (parser, /*template_p=*/false,
23386 /*check_dependency_p=*/true,
23387 /*template_p=*/NULL,
23388 /*declarator_p=*/false,
23389 /*optional_p=*/false);
23390 if (name == error_mark_node)
23393 decl = cp_parser_lookup_name_simple (parser, name, token->location);
23394 if (decl == error_mark_node)
23395 cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23397 else if (kind != 0)
23399 tree u = build_omp_clause (token->location, kind);
23400 OMP_CLAUSE_DECL (u) = decl;
23401 OMP_CLAUSE_CHAIN (u) = list;
23405 list = tree_cons (decl, NULL_TREE, list);
23408 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23410 cp_lexer_consume_token (parser->lexer);
23413 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23417 /* Try to resync to an unnested comma. Copied from
23418 cp_parser_parenthesized_expression_list. */
23420 ending = cp_parser_skip_to_closing_parenthesis (parser,
23421 /*recovering=*/true,
23423 /*consume_paren=*/true);
23431 /* Similarly, but expect leading and trailing parenthesis. This is a very
23432 common case for omp clauses. */
23435 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23437 if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23438 return cp_parser_omp_var_list_no_open (parser, kind, list);
23443 collapse ( constant-expression ) */
23446 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23452 loc = cp_lexer_peek_token (parser->lexer)->location;
23453 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23456 num = cp_parser_constant_expression (parser, false, NULL);
23458 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23459 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23460 /*or_comma=*/false,
23461 /*consume_paren=*/true);
23463 if (num == error_mark_node)
23465 num = fold_non_dependent_expr (num);
23466 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23467 || !host_integerp (num, 0)
23468 || (n = tree_low_cst (num, 0)) <= 0
23471 error_at (loc, "collapse argument needs positive constant integer expression");
23475 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23476 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23477 OMP_CLAUSE_CHAIN (c) = list;
23478 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23484 default ( shared | none ) */
23487 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23489 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23492 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23494 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23496 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23497 const char *p = IDENTIFIER_POINTER (id);
23502 if (strcmp ("none", p) != 0)
23504 kind = OMP_CLAUSE_DEFAULT_NONE;
23508 if (strcmp ("shared", p) != 0)
23510 kind = OMP_CLAUSE_DEFAULT_SHARED;
23517 cp_lexer_consume_token (parser->lexer);
23522 cp_parser_error (parser, "expected %<none%> or %<shared%>");
23525 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23526 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23527 /*or_comma=*/false,
23528 /*consume_paren=*/true);
23530 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23533 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23534 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23535 OMP_CLAUSE_CHAIN (c) = list;
23536 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23542 if ( expression ) */
23545 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23549 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23552 t = cp_parser_condition (parser);
23554 if (t == error_mark_node
23555 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23556 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23557 /*or_comma=*/false,
23558 /*consume_paren=*/true);
23560 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23562 c = build_omp_clause (location, OMP_CLAUSE_IF);
23563 OMP_CLAUSE_IF_EXPR (c) = t;
23564 OMP_CLAUSE_CHAIN (c) = list;
23573 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23574 tree list, location_t location)
23578 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23580 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23581 OMP_CLAUSE_CHAIN (c) = list;
23586 num_threads ( expression ) */
23589 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23590 location_t location)
23594 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23597 t = cp_parser_expression (parser, false, NULL);
23599 if (t == error_mark_node
23600 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23602 /*or_comma=*/false,
23603 /*consume_paren=*/true);
23605 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23606 "num_threads", location);
23608 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23609 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23610 OMP_CLAUSE_CHAIN (c) = list;
23619 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23620 tree list, location_t location)
23624 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23625 "ordered", location);
23627 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23628 OMP_CLAUSE_CHAIN (c) = list;
23633 reduction ( reduction-operator : variable-list )
23635 reduction-operator:
23636 One of: + * - & ^ | && || */
23639 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23641 enum tree_code code;
23644 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23647 switch (cp_lexer_peek_token (parser->lexer)->type)
23659 code = BIT_AND_EXPR;
23662 code = BIT_XOR_EXPR;
23665 code = BIT_IOR_EXPR;
23668 code = TRUTH_ANDIF_EXPR;
23671 code = TRUTH_ORIF_EXPR;
23674 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23675 "%<|%>, %<&&%>, or %<||%>");
23677 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23678 /*or_comma=*/false,
23679 /*consume_paren=*/true);
23682 cp_lexer_consume_token (parser->lexer);
23684 if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23687 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23688 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23689 OMP_CLAUSE_REDUCTION_CODE (c) = code;
23695 schedule ( schedule-kind )
23696 schedule ( schedule-kind , expression )
23699 static | dynamic | guided | runtime | auto */
23702 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23706 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23709 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23711 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23713 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23714 const char *p = IDENTIFIER_POINTER (id);
23719 if (strcmp ("dynamic", p) != 0)
23721 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23725 if (strcmp ("guided", p) != 0)
23727 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23731 if (strcmp ("runtime", p) != 0)
23733 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23740 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23741 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23742 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23743 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23746 cp_lexer_consume_token (parser->lexer);
23748 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23751 cp_lexer_consume_token (parser->lexer);
23753 token = cp_lexer_peek_token (parser->lexer);
23754 t = cp_parser_assignment_expression (parser, false, NULL);
23756 if (t == error_mark_node)
23758 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23759 error_at (token->location, "schedule %<runtime%> does not take "
23760 "a %<chunk_size%> parameter");
23761 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23762 error_at (token->location, "schedule %<auto%> does not take "
23763 "a %<chunk_size%> parameter");
23765 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23767 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23770 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23773 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23774 OMP_CLAUSE_CHAIN (c) = list;
23778 cp_parser_error (parser, "invalid schedule kind");
23780 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23781 /*or_comma=*/false,
23782 /*consume_paren=*/true);
23790 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23791 tree list, location_t location)
23795 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23797 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23798 OMP_CLAUSE_CHAIN (c) = list;
23802 /* Parse all OpenMP clauses. The set clauses allowed by the directive
23803 is a bitmask in MASK. Return the list of clauses found; the result
23804 of clause default goes in *pdefault. */
23807 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23808 const char *where, cp_token *pragma_tok)
23810 tree clauses = NULL;
23812 cp_token *token = NULL;
23814 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23816 pragma_omp_clause c_kind;
23817 const char *c_name;
23818 tree prev = clauses;
23820 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23821 cp_lexer_consume_token (parser->lexer);
23823 token = cp_lexer_peek_token (parser->lexer);
23824 c_kind = cp_parser_omp_clause_name (parser);
23829 case PRAGMA_OMP_CLAUSE_COLLAPSE:
23830 clauses = cp_parser_omp_clause_collapse (parser, clauses,
23832 c_name = "collapse";
23834 case PRAGMA_OMP_CLAUSE_COPYIN:
23835 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23838 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23839 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23841 c_name = "copyprivate";
23843 case PRAGMA_OMP_CLAUSE_DEFAULT:
23844 clauses = cp_parser_omp_clause_default (parser, clauses,
23846 c_name = "default";
23848 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23849 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23851 c_name = "firstprivate";
23853 case PRAGMA_OMP_CLAUSE_IF:
23854 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23857 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23858 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23860 c_name = "lastprivate";
23862 case PRAGMA_OMP_CLAUSE_NOWAIT:
23863 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23866 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23867 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23869 c_name = "num_threads";
23871 case PRAGMA_OMP_CLAUSE_ORDERED:
23872 clauses = cp_parser_omp_clause_ordered (parser, clauses,
23874 c_name = "ordered";
23876 case PRAGMA_OMP_CLAUSE_PRIVATE:
23877 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23879 c_name = "private";
23881 case PRAGMA_OMP_CLAUSE_REDUCTION:
23882 clauses = cp_parser_omp_clause_reduction (parser, clauses);
23883 c_name = "reduction";
23885 case PRAGMA_OMP_CLAUSE_SCHEDULE:
23886 clauses = cp_parser_omp_clause_schedule (parser, clauses,
23888 c_name = "schedule";
23890 case PRAGMA_OMP_CLAUSE_SHARED:
23891 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23895 case PRAGMA_OMP_CLAUSE_UNTIED:
23896 clauses = cp_parser_omp_clause_untied (parser, clauses,
23901 cp_parser_error (parser, "expected %<#pragma omp%> clause");
23905 if (((mask >> c_kind) & 1) == 0)
23907 /* Remove the invalid clause(s) from the list to avoid
23908 confusing the rest of the compiler. */
23910 error_at (token->location, "%qs is not valid for %qs", c_name, where);
23914 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23915 return finish_omp_clauses (clauses);
23922 In practice, we're also interested in adding the statement to an
23923 outer node. So it is convenient if we work around the fact that
23924 cp_parser_statement calls add_stmt. */
23927 cp_parser_begin_omp_structured_block (cp_parser *parser)
23929 unsigned save = parser->in_statement;
23931 /* Only move the values to IN_OMP_BLOCK if they weren't false.
23932 This preserves the "not within loop or switch" style error messages
23933 for nonsense cases like
23939 if (parser->in_statement)
23940 parser->in_statement = IN_OMP_BLOCK;
23946 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
23948 parser->in_statement = save;
23952 cp_parser_omp_structured_block (cp_parser *parser)
23954 tree stmt = begin_omp_structured_block ();
23955 unsigned int save = cp_parser_begin_omp_structured_block (parser);
23957 cp_parser_statement (parser, NULL_TREE, false, NULL);
23959 cp_parser_end_omp_structured_block (parser, save);
23960 return finish_omp_structured_block (stmt);
23964 # pragma omp atomic new-line
23968 x binop= expr | x++ | ++x | x-- | --x
23970 +, *, -, /, &, ^, |, <<, >>
23972 where x is an lvalue expression with scalar type. */
23975 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
23978 enum tree_code code;
23980 cp_parser_require_pragma_eol (parser, pragma_tok);
23982 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
23983 /*cast_p=*/false, NULL);
23984 switch (TREE_CODE (lhs))
23989 case PREINCREMENT_EXPR:
23990 case POSTINCREMENT_EXPR:
23991 lhs = TREE_OPERAND (lhs, 0);
23993 rhs = integer_one_node;
23996 case PREDECREMENT_EXPR:
23997 case POSTDECREMENT_EXPR:
23998 lhs = TREE_OPERAND (lhs, 0);
24000 rhs = integer_one_node;
24003 case COMPOUND_EXPR:
24004 if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24005 && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24006 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24007 && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24008 && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24009 (TREE_OPERAND (lhs, 1), 0), 0)))
24011 /* Undo effects of boolean_increment for post {in,de}crement. */
24012 lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24015 if (TREE_CODE (lhs) == MODIFY_EXPR
24016 && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24018 /* Undo effects of boolean_increment. */
24019 if (integer_onep (TREE_OPERAND (lhs, 1)))
24021 /* This is pre or post increment. */
24022 rhs = TREE_OPERAND (lhs, 1);
24023 lhs = TREE_OPERAND (lhs, 0);
24030 switch (cp_lexer_peek_token (parser->lexer)->type)
24036 code = TRUNC_DIV_EXPR;
24044 case CPP_LSHIFT_EQ:
24045 code = LSHIFT_EXPR;
24047 case CPP_RSHIFT_EQ:
24048 code = RSHIFT_EXPR;
24051 code = BIT_AND_EXPR;
24054 code = BIT_IOR_EXPR;
24057 code = BIT_XOR_EXPR;
24060 cp_parser_error (parser,
24061 "invalid operator for %<#pragma omp atomic%>");
24064 cp_lexer_consume_token (parser->lexer);
24066 rhs = cp_parser_expression (parser, false, NULL);
24067 if (rhs == error_mark_node)
24071 finish_omp_atomic (code, lhs, rhs);
24072 cp_parser_consume_semicolon_at_end_of_statement (parser);
24076 cp_parser_skip_to_end_of_block_or_statement (parser);
24081 # pragma omp barrier new-line */
24084 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24086 cp_parser_require_pragma_eol (parser, pragma_tok);
24087 finish_omp_barrier ();
24091 # pragma omp critical [(name)] new-line
24092 structured-block */
24095 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24097 tree stmt, name = NULL;
24099 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24101 cp_lexer_consume_token (parser->lexer);
24103 name = cp_parser_identifier (parser);
24105 if (name == error_mark_node
24106 || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24107 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24108 /*or_comma=*/false,
24109 /*consume_paren=*/true);
24110 if (name == error_mark_node)
24113 cp_parser_require_pragma_eol (parser, pragma_tok);
24115 stmt = cp_parser_omp_structured_block (parser);
24116 return c_finish_omp_critical (input_location, stmt, name);
24120 # pragma omp flush flush-vars[opt] new-line
24123 ( variable-list ) */
24126 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24128 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24129 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24130 cp_parser_require_pragma_eol (parser, pragma_tok);
24132 finish_omp_flush ();
24135 /* Helper function, to parse omp for increment expression. */
24138 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24140 tree cond = cp_parser_binary_expression (parser, false, true,
24141 PREC_NOT_OPERATOR, NULL);
24144 if (cond == error_mark_node
24145 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24147 cp_parser_skip_to_end_of_statement (parser);
24148 return error_mark_node;
24151 switch (TREE_CODE (cond))
24159 return error_mark_node;
24162 /* If decl is an iterator, preserve LHS and RHS of the relational
24163 expr until finish_omp_for. */
24165 && (type_dependent_expression_p (decl)
24166 || CLASS_TYPE_P (TREE_TYPE (decl))))
24169 return build_x_binary_op (TREE_CODE (cond),
24170 TREE_OPERAND (cond, 0), ERROR_MARK,
24171 TREE_OPERAND (cond, 1), ERROR_MARK,
24172 &overloaded_p, tf_warning_or_error);
24175 /* Helper function, to parse omp for increment expression. */
24178 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24180 cp_token *token = cp_lexer_peek_token (parser->lexer);
24186 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24188 op = (token->type == CPP_PLUS_PLUS
24189 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24190 cp_lexer_consume_token (parser->lexer);
24191 lhs = cp_parser_cast_expression (parser, false, false, NULL);
24193 return error_mark_node;
24194 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24197 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24199 return error_mark_node;
24201 token = cp_lexer_peek_token (parser->lexer);
24202 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24204 op = (token->type == CPP_PLUS_PLUS
24205 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24206 cp_lexer_consume_token (parser->lexer);
24207 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24210 op = cp_parser_assignment_operator_opt (parser);
24211 if (op == ERROR_MARK)
24212 return error_mark_node;
24214 if (op != NOP_EXPR)
24216 rhs = cp_parser_assignment_expression (parser, false, NULL);
24217 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24218 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24221 lhs = cp_parser_binary_expression (parser, false, false,
24222 PREC_ADDITIVE_EXPRESSION, NULL);
24223 token = cp_lexer_peek_token (parser->lexer);
24224 decl_first = lhs == decl;
24227 if (token->type != CPP_PLUS
24228 && token->type != CPP_MINUS)
24229 return error_mark_node;
24233 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24234 cp_lexer_consume_token (parser->lexer);
24235 rhs = cp_parser_binary_expression (parser, false, false,
24236 PREC_ADDITIVE_EXPRESSION, NULL);
24237 token = cp_lexer_peek_token (parser->lexer);
24238 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24240 if (lhs == NULL_TREE)
24242 if (op == PLUS_EXPR)
24245 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24248 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24249 NULL, tf_warning_or_error);
24252 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24256 if (rhs != decl || op == MINUS_EXPR)
24257 return error_mark_node;
24258 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24261 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24263 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24266 /* Parse the restricted form of the for statement allowed by OpenMP. */
24269 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24271 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24272 tree real_decl, initv, condv, incrv, declv;
24273 tree this_pre_body, cl;
24274 location_t loc_first;
24275 bool collapse_err = false;
24276 int i, collapse = 1, nbraces = 0;
24277 VEC(tree,gc) *for_block = make_tree_vector ();
24279 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24280 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24281 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24283 gcc_assert (collapse >= 1);
24285 declv = make_tree_vec (collapse);
24286 initv = make_tree_vec (collapse);
24287 condv = make_tree_vec (collapse);
24288 incrv = make_tree_vec (collapse);
24290 loc_first = cp_lexer_peek_token (parser->lexer)->location;
24292 for (i = 0; i < collapse; i++)
24294 int bracecount = 0;
24295 bool add_private_clause = false;
24298 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24300 cp_parser_error (parser, "for statement expected");
24303 loc = cp_lexer_consume_token (parser->lexer)->location;
24305 if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24308 init = decl = real_decl = NULL;
24309 this_pre_body = push_stmt_list ();
24310 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24312 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24316 integer-type var = lb
24317 random-access-iterator-type var = lb
24318 pointer-type var = lb
24320 cp_decl_specifier_seq type_specifiers;
24322 /* First, try to parse as an initialized declaration. See
24323 cp_parser_condition, from whence the bulk of this is copied. */
24325 cp_parser_parse_tentatively (parser);
24326 cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24327 /*is_trailing_return=*/false,
24329 if (cp_parser_parse_definitely (parser))
24331 /* If parsing a type specifier seq succeeded, then this
24332 MUST be a initialized declaration. */
24333 tree asm_specification, attributes;
24334 cp_declarator *declarator;
24336 declarator = cp_parser_declarator (parser,
24337 CP_PARSER_DECLARATOR_NAMED,
24338 /*ctor_dtor_or_conv_p=*/NULL,
24339 /*parenthesized_p=*/NULL,
24340 /*member_p=*/false);
24341 attributes = cp_parser_attributes_opt (parser);
24342 asm_specification = cp_parser_asm_specification_opt (parser);
24344 if (declarator == cp_error_declarator)
24345 cp_parser_skip_to_end_of_statement (parser);
24349 tree pushed_scope, auto_node;
24351 decl = start_decl (declarator, &type_specifiers,
24352 SD_INITIALIZED, attributes,
24353 /*prefix_attributes=*/NULL_TREE,
24356 auto_node = type_uses_auto (TREE_TYPE (decl));
24357 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24359 if (cp_lexer_next_token_is (parser->lexer,
24361 error ("parenthesized initialization is not allowed in "
24362 "OpenMP %<for%> loop");
24364 /* Trigger an error. */
24365 cp_parser_require (parser, CPP_EQ, RT_EQ);
24367 init = error_mark_node;
24368 cp_parser_skip_to_end_of_statement (parser);
24370 else if (CLASS_TYPE_P (TREE_TYPE (decl))
24371 || type_dependent_expression_p (decl)
24374 bool is_direct_init, is_non_constant_init;
24376 init = cp_parser_initializer (parser,
24378 &is_non_constant_init);
24380 if (auto_node && describable_type (init))
24383 = do_auto_deduction (TREE_TYPE (decl), init,
24386 if (!CLASS_TYPE_P (TREE_TYPE (decl))
24387 && !type_dependent_expression_p (decl))
24391 cp_finish_decl (decl, init, !is_non_constant_init,
24393 LOOKUP_ONLYCONVERTING);
24394 if (CLASS_TYPE_P (TREE_TYPE (decl)))
24396 VEC_safe_push (tree, gc, for_block, this_pre_body);
24400 init = pop_stmt_list (this_pre_body);
24401 this_pre_body = NULL_TREE;
24406 cp_lexer_consume_token (parser->lexer);
24407 init = cp_parser_assignment_expression (parser, false, NULL);
24410 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24411 init = error_mark_node;
24413 cp_finish_decl (decl, NULL_TREE,
24414 /*init_const_expr_p=*/false,
24416 LOOKUP_ONLYCONVERTING);
24420 pop_scope (pushed_scope);
24426 /* If parsing a type specifier sequence failed, then
24427 this MUST be a simple expression. */
24428 cp_parser_parse_tentatively (parser);
24429 decl = cp_parser_primary_expression (parser, false, false,
24431 if (!cp_parser_error_occurred (parser)
24434 && CLASS_TYPE_P (TREE_TYPE (decl)))
24438 cp_parser_parse_definitely (parser);
24439 cp_parser_require (parser, CPP_EQ, RT_EQ);
24440 rhs = cp_parser_assignment_expression (parser, false, NULL);
24441 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24443 tf_warning_or_error));
24444 add_private_clause = true;
24449 cp_parser_abort_tentative_parse (parser);
24450 init = cp_parser_expression (parser, false, NULL);
24453 if (TREE_CODE (init) == MODIFY_EXPR
24454 || TREE_CODE (init) == MODOP_EXPR)
24455 real_decl = TREE_OPERAND (init, 0);
24460 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24463 this_pre_body = pop_stmt_list (this_pre_body);
24467 pre_body = push_stmt_list ();
24469 add_stmt (this_pre_body);
24470 pre_body = pop_stmt_list (pre_body);
24473 pre_body = this_pre_body;
24478 if (par_clauses != NULL && real_decl != NULL_TREE)
24481 for (c = par_clauses; *c ; )
24482 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24483 && OMP_CLAUSE_DECL (*c) == real_decl)
24485 error_at (loc, "iteration variable %qD"
24486 " should not be firstprivate", real_decl);
24487 *c = OMP_CLAUSE_CHAIN (*c);
24489 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24490 && OMP_CLAUSE_DECL (*c) == real_decl)
24492 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24493 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
24494 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24495 OMP_CLAUSE_DECL (l) = real_decl;
24496 OMP_CLAUSE_CHAIN (l) = clauses;
24497 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24499 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24500 CP_OMP_CLAUSE_INFO (*c) = NULL;
24501 add_private_clause = false;
24505 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24506 && OMP_CLAUSE_DECL (*c) == real_decl)
24507 add_private_clause = false;
24508 c = &OMP_CLAUSE_CHAIN (*c);
24512 if (add_private_clause)
24515 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24517 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24518 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24519 && OMP_CLAUSE_DECL (c) == decl)
24521 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24522 && OMP_CLAUSE_DECL (c) == decl)
24523 error_at (loc, "iteration variable %qD "
24524 "should not be firstprivate",
24526 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24527 && OMP_CLAUSE_DECL (c) == decl)
24528 error_at (loc, "iteration variable %qD should not be reduction",
24533 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24534 OMP_CLAUSE_DECL (c) = decl;
24535 c = finish_omp_clauses (c);
24538 OMP_CLAUSE_CHAIN (c) = clauses;
24545 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24546 cond = cp_parser_omp_for_cond (parser, decl);
24547 cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24552 /* If decl is an iterator, preserve the operator on decl
24553 until finish_omp_for. */
24555 && ((type_dependent_expression_p (decl)
24556 && !POINTER_TYPE_P (TREE_TYPE (decl)))
24557 || CLASS_TYPE_P (TREE_TYPE (decl))))
24558 incr = cp_parser_omp_for_incr (parser, decl);
24560 incr = cp_parser_expression (parser, false, NULL);
24563 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24564 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24565 /*or_comma=*/false,
24566 /*consume_paren=*/true);
24568 TREE_VEC_ELT (declv, i) = decl;
24569 TREE_VEC_ELT (initv, i) = init;
24570 TREE_VEC_ELT (condv, i) = cond;
24571 TREE_VEC_ELT (incrv, i) = incr;
24573 if (i == collapse - 1)
24576 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24577 in between the collapsed for loops to be still considered perfectly
24578 nested. Hopefully the final version clarifies this.
24579 For now handle (multiple) {'s and empty statements. */
24580 cp_parser_parse_tentatively (parser);
24583 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24585 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24587 cp_lexer_consume_token (parser->lexer);
24590 else if (bracecount
24591 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24592 cp_lexer_consume_token (parser->lexer);
24595 loc = cp_lexer_peek_token (parser->lexer)->location;
24596 error_at (loc, "not enough collapsed for loops");
24597 collapse_err = true;
24598 cp_parser_abort_tentative_parse (parser);
24607 cp_parser_parse_definitely (parser);
24608 nbraces += bracecount;
24612 /* Note that we saved the original contents of this flag when we entered
24613 the structured block, and so we don't need to re-save it here. */
24614 parser->in_statement = IN_OMP_FOR;
24616 /* Note that the grammar doesn't call for a structured block here,
24617 though the loop as a whole is a structured block. */
24618 body = push_stmt_list ();
24619 cp_parser_statement (parser, NULL_TREE, false, NULL);
24620 body = pop_stmt_list (body);
24622 if (declv == NULL_TREE)
24625 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24626 pre_body, clauses);
24630 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24632 cp_lexer_consume_token (parser->lexer);
24635 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24636 cp_lexer_consume_token (parser->lexer);
24641 error_at (cp_lexer_peek_token (parser->lexer)->location,
24642 "collapsed loops not perfectly nested");
24644 collapse_err = true;
24645 cp_parser_statement_seq_opt (parser, NULL);
24646 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24651 while (!VEC_empty (tree, for_block))
24652 add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24653 release_tree_vector (for_block);
24659 #pragma omp for for-clause[optseq] new-line
24662 #define OMP_FOR_CLAUSE_MASK \
24663 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24664 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24665 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24666 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24667 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
24668 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
24669 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
24670 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24673 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24675 tree clauses, sb, ret;
24678 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24679 "#pragma omp for", pragma_tok);
24681 sb = begin_omp_structured_block ();
24682 save = cp_parser_begin_omp_structured_block (parser);
24684 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24686 cp_parser_end_omp_structured_block (parser, save);
24687 add_stmt (finish_omp_structured_block (sb));
24693 # pragma omp master new-line
24694 structured-block */
24697 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24699 cp_parser_require_pragma_eol (parser, pragma_tok);
24700 return c_finish_omp_master (input_location,
24701 cp_parser_omp_structured_block (parser));
24705 # pragma omp ordered new-line
24706 structured-block */
24709 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24711 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24712 cp_parser_require_pragma_eol (parser, pragma_tok);
24713 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24719 { section-sequence }
24722 section-directive[opt] structured-block
24723 section-sequence section-directive structured-block */
24726 cp_parser_omp_sections_scope (cp_parser *parser)
24728 tree stmt, substmt;
24729 bool error_suppress = false;
24732 if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24735 stmt = push_stmt_list ();
24737 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24741 substmt = begin_omp_structured_block ();
24742 save = cp_parser_begin_omp_structured_block (parser);
24746 cp_parser_statement (parser, NULL_TREE, false, NULL);
24748 tok = cp_lexer_peek_token (parser->lexer);
24749 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24751 if (tok->type == CPP_CLOSE_BRACE)
24753 if (tok->type == CPP_EOF)
24757 cp_parser_end_omp_structured_block (parser, save);
24758 substmt = finish_omp_structured_block (substmt);
24759 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24760 add_stmt (substmt);
24765 tok = cp_lexer_peek_token (parser->lexer);
24766 if (tok->type == CPP_CLOSE_BRACE)
24768 if (tok->type == CPP_EOF)
24771 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24773 cp_lexer_consume_token (parser->lexer);
24774 cp_parser_require_pragma_eol (parser, tok);
24775 error_suppress = false;
24777 else if (!error_suppress)
24779 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24780 error_suppress = true;
24783 substmt = cp_parser_omp_structured_block (parser);
24784 substmt = build1 (OMP_SECTION, void_type_node, substmt);
24785 add_stmt (substmt);
24787 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24789 substmt = pop_stmt_list (stmt);
24791 stmt = make_node (OMP_SECTIONS);
24792 TREE_TYPE (stmt) = void_type_node;
24793 OMP_SECTIONS_BODY (stmt) = substmt;
24800 # pragma omp sections sections-clause[optseq] newline
24803 #define OMP_SECTIONS_CLAUSE_MASK \
24804 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24805 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24806 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
24807 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24808 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24811 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24815 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24816 "#pragma omp sections", pragma_tok);
24818 ret = cp_parser_omp_sections_scope (parser);
24820 OMP_SECTIONS_CLAUSES (ret) = clauses;
24826 # pragma parallel parallel-clause new-line
24827 # pragma parallel for parallel-for-clause new-line
24828 # pragma parallel sections parallel-sections-clause new-line */
24830 #define OMP_PARALLEL_CLAUSE_MASK \
24831 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24832 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24833 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24834 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24835 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
24836 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
24837 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
24838 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24841 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24843 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24844 const char *p_name = "#pragma omp parallel";
24845 tree stmt, clauses, par_clause, ws_clause, block;
24846 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24848 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24850 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24852 cp_lexer_consume_token (parser->lexer);
24853 p_kind = PRAGMA_OMP_PARALLEL_FOR;
24854 p_name = "#pragma omp parallel for";
24855 mask |= OMP_FOR_CLAUSE_MASK;
24856 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24858 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24860 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24861 const char *p = IDENTIFIER_POINTER (id);
24862 if (strcmp (p, "sections") == 0)
24864 cp_lexer_consume_token (parser->lexer);
24865 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24866 p_name = "#pragma omp parallel sections";
24867 mask |= OMP_SECTIONS_CLAUSE_MASK;
24868 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24872 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24873 block = begin_omp_parallel ();
24874 save = cp_parser_begin_omp_structured_block (parser);
24878 case PRAGMA_OMP_PARALLEL:
24879 cp_parser_statement (parser, NULL_TREE, false, NULL);
24880 par_clause = clauses;
24883 case PRAGMA_OMP_PARALLEL_FOR:
24884 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24885 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24888 case PRAGMA_OMP_PARALLEL_SECTIONS:
24889 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24890 stmt = cp_parser_omp_sections_scope (parser);
24892 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24896 gcc_unreachable ();
24899 cp_parser_end_omp_structured_block (parser, save);
24900 stmt = finish_omp_parallel (par_clause, block);
24901 if (p_kind != PRAGMA_OMP_PARALLEL)
24902 OMP_PARALLEL_COMBINED (stmt) = 1;
24907 # pragma omp single single-clause[optseq] new-line
24908 structured-block */
24910 #define OMP_SINGLE_CLAUSE_MASK \
24911 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24912 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24913 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
24914 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24917 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
24919 tree stmt = make_node (OMP_SINGLE);
24920 TREE_TYPE (stmt) = void_type_node;
24922 OMP_SINGLE_CLAUSES (stmt)
24923 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
24924 "#pragma omp single", pragma_tok);
24925 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
24927 return add_stmt (stmt);
24931 # pragma omp task task-clause[optseq] new-line
24932 structured-block */
24934 #define OMP_TASK_CLAUSE_MASK \
24935 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
24936 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
24937 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
24938 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
24939 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
24940 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
24943 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
24945 tree clauses, block;
24948 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
24949 "#pragma omp task", pragma_tok);
24950 block = begin_omp_task ();
24951 save = cp_parser_begin_omp_structured_block (parser);
24952 cp_parser_statement (parser, NULL_TREE, false, NULL);
24953 cp_parser_end_omp_structured_block (parser, save);
24954 return finish_omp_task (clauses, block);
24958 # pragma omp taskwait new-line */
24961 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
24963 cp_parser_require_pragma_eol (parser, pragma_tok);
24964 finish_omp_taskwait ();
24968 # pragma omp threadprivate (variable-list) */
24971 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
24975 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24976 cp_parser_require_pragma_eol (parser, pragma_tok);
24978 finish_omp_threadprivate (vars);
24981 /* Main entry point to OpenMP statement pragmas. */
24984 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
24988 switch (pragma_tok->pragma_kind)
24990 case PRAGMA_OMP_ATOMIC:
24991 cp_parser_omp_atomic (parser, pragma_tok);
24993 case PRAGMA_OMP_CRITICAL:
24994 stmt = cp_parser_omp_critical (parser, pragma_tok);
24996 case PRAGMA_OMP_FOR:
24997 stmt = cp_parser_omp_for (parser, pragma_tok);
24999 case PRAGMA_OMP_MASTER:
25000 stmt = cp_parser_omp_master (parser, pragma_tok);
25002 case PRAGMA_OMP_ORDERED:
25003 stmt = cp_parser_omp_ordered (parser, pragma_tok);
25005 case PRAGMA_OMP_PARALLEL:
25006 stmt = cp_parser_omp_parallel (parser, pragma_tok);
25008 case PRAGMA_OMP_SECTIONS:
25009 stmt = cp_parser_omp_sections (parser, pragma_tok);
25011 case PRAGMA_OMP_SINGLE:
25012 stmt = cp_parser_omp_single (parser, pragma_tok);
25014 case PRAGMA_OMP_TASK:
25015 stmt = cp_parser_omp_task (parser, pragma_tok);
25018 gcc_unreachable ();
25022 SET_EXPR_LOCATION (stmt, pragma_tok->location);
25027 static GTY (()) cp_parser *the_parser;
25030 /* Special handling for the first token or line in the file. The first
25031 thing in the file might be #pragma GCC pch_preprocess, which loads a
25032 PCH file, which is a GC collection point. So we need to handle this
25033 first pragma without benefit of an existing lexer structure.
25035 Always returns one token to the caller in *FIRST_TOKEN. This is
25036 either the true first token of the file, or the first token after
25037 the initial pragma. */
25040 cp_parser_initial_pragma (cp_token *first_token)
25044 cp_lexer_get_preprocessor_token (NULL, first_token);
25045 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25048 cp_lexer_get_preprocessor_token (NULL, first_token);
25049 if (first_token->type == CPP_STRING)
25051 name = first_token->u.value;
25053 cp_lexer_get_preprocessor_token (NULL, first_token);
25054 if (first_token->type != CPP_PRAGMA_EOL)
25055 error_at (first_token->location,
25056 "junk at end of %<#pragma GCC pch_preprocess%>");
25059 error_at (first_token->location, "expected string literal");
25061 /* Skip to the end of the pragma. */
25062 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25063 cp_lexer_get_preprocessor_token (NULL, first_token);
25065 /* Now actually load the PCH file. */
25067 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25069 /* Read one more token to return to our caller. We have to do this
25070 after reading the PCH file in, since its pointers have to be
25072 cp_lexer_get_preprocessor_token (NULL, first_token);
25075 /* Normal parsing of a pragma token. Here we can (and must) use the
25079 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25081 cp_token *pragma_tok;
25084 pragma_tok = cp_lexer_consume_token (parser->lexer);
25085 gcc_assert (pragma_tok->type == CPP_PRAGMA);
25086 parser->lexer->in_pragma = true;
25088 id = pragma_tok->pragma_kind;
25091 case PRAGMA_GCC_PCH_PREPROCESS:
25092 error_at (pragma_tok->location,
25093 "%<#pragma GCC pch_preprocess%> must be first");
25096 case PRAGMA_OMP_BARRIER:
25099 case pragma_compound:
25100 cp_parser_omp_barrier (parser, pragma_tok);
25103 error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25104 "used in compound statements");
25111 case PRAGMA_OMP_FLUSH:
25114 case pragma_compound:
25115 cp_parser_omp_flush (parser, pragma_tok);
25118 error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25119 "used in compound statements");
25126 case PRAGMA_OMP_TASKWAIT:
25129 case pragma_compound:
25130 cp_parser_omp_taskwait (parser, pragma_tok);
25133 error_at (pragma_tok->location,
25134 "%<#pragma omp taskwait%> may only be "
25135 "used in compound statements");
25142 case PRAGMA_OMP_THREADPRIVATE:
25143 cp_parser_omp_threadprivate (parser, pragma_tok);
25146 case PRAGMA_OMP_ATOMIC:
25147 case PRAGMA_OMP_CRITICAL:
25148 case PRAGMA_OMP_FOR:
25149 case PRAGMA_OMP_MASTER:
25150 case PRAGMA_OMP_ORDERED:
25151 case PRAGMA_OMP_PARALLEL:
25152 case PRAGMA_OMP_SECTIONS:
25153 case PRAGMA_OMP_SINGLE:
25154 case PRAGMA_OMP_TASK:
25155 if (context == pragma_external)
25157 cp_parser_omp_construct (parser, pragma_tok);
25160 case PRAGMA_OMP_SECTION:
25161 error_at (pragma_tok->location,
25162 "%<#pragma omp section%> may only be used in "
25163 "%<#pragma omp sections%> construct");
25167 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25168 c_invoke_pragma_handler (id);
25172 cp_parser_error (parser, "expected declaration specifiers");
25176 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25180 /* The interface the pragma parsers have to the lexer. */
25183 pragma_lex (tree *value)
25186 enum cpp_ttype ret;
25188 tok = cp_lexer_peek_token (the_parser->lexer);
25191 *value = tok->u.value;
25193 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25195 else if (ret == CPP_STRING)
25196 *value = cp_parser_string_literal (the_parser, false, false);
25199 cp_lexer_consume_token (the_parser->lexer);
25200 if (ret == CPP_KEYWORD)
25208 /* External interface. */
25210 /* Parse one entire translation unit. */
25213 c_parse_file (void)
25215 static bool already_called = false;
25217 if (already_called)
25219 sorry ("inter-module optimizations not implemented for C++");
25222 already_called = true;
25224 the_parser = cp_parser_new ();
25225 push_deferring_access_checks (flag_access_control
25226 ? dk_no_deferred : dk_no_check);
25227 cp_parser_translation_unit (the_parser);
25231 #include "gt-cp-parser.h"