2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008, 2009 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"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct GTY(()) tree_check {
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct GTY (()) cp_token {
64 /* The kind of token. */
65 ENUM_BITFIELD (cpp_ttype) type : 8;
66 /* If this token is a keyword, this value indicates which keyword.
67 Otherwise, this value is RID_MAX. */
68 ENUM_BITFIELD (rid) keyword : 8;
71 /* Identifier for the pragma. */
72 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73 /* True if this token is from a context where it is implicitly extern "C" */
74 BOOL_BITFIELD implicit_extern_c : 1;
75 /* True for a CPP_NAME token that is not a keyword (i.e., for which
76 KEYWORD is RID_MAX) iff this name was looked up and found to be
77 ambiguous. An error has already been reported. */
78 BOOL_BITFIELD ambiguous_p : 1;
79 /* The location at which this token was found. */
81 /* The value associated with this token, if any. */
82 union cp_token_value {
83 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
84 struct tree_check* GTY((tag ("1"))) tree_check_value;
85 /* Use for all other tokens. */
86 tree GTY((tag ("0"))) value;
87 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
90 /* We use a stack of token pointer for saving token sets. */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
95 static cp_token eof_token =
97 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
100 /* The cp_lexer structure represents the C++ lexer. It is responsible
101 for managing the token stream from the preprocessor and supplying
102 it to the parser. Tokens are never added to the cp_lexer after
105 typedef struct GTY (()) cp_lexer {
106 /* The memory allocated for the buffer. NULL if this lexer does not
107 own the token buffer. */
108 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109 /* If the lexer owns the buffer, this is the number of tokens in the
111 size_t buffer_length;
113 /* A pointer just past the last available token. The tokens
114 in this lexer are [buffer, last_token). */
115 cp_token_position GTY ((skip)) last_token;
117 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
118 no more available tokens. */
119 cp_token_position GTY ((skip)) next_token;
121 /* A stack indicating positions at which cp_lexer_save_tokens was
122 called. The top entry is the most recent position at which we
123 began saving tokens. If the stack is non-empty, we are saving
125 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
127 /* The next lexer in a linked list of lexers. */
128 struct cp_lexer *next;
130 /* True if we should output debugging information. */
133 /* True if we're in the context of parsing a pragma, and should not
134 increment past the end-of-line marker. */
138 /* cp_token_cache is a range of tokens. There is no need to represent
139 allocate heap memory for it, since tokens are never removed from the
140 lexer's array. There is also no need for the GC to walk through
141 a cp_token_cache, since everything in here is referenced through
144 typedef struct GTY(()) cp_token_cache {
145 /* The beginning of the token range. */
146 cp_token * GTY((skip)) first;
148 /* Points immediately after the last token in the range. */
149 cp_token * GTY ((skip)) last;
154 static cp_lexer *cp_lexer_new_main
156 static cp_lexer *cp_lexer_new_from_tokens
157 (cp_token_cache *tokens);
158 static void cp_lexer_destroy
160 static int cp_lexer_saving_tokens
162 static cp_token_position cp_lexer_token_position
164 static cp_token *cp_lexer_token_at
165 (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167 (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
170 static cp_token *cp_lexer_peek_nth_token
171 (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173 (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175 (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177 (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
180 static void cp_lexer_purge_token
182 static void cp_lexer_purge_tokens_after
183 (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
186 static void cp_lexer_commit_tokens
188 static void cp_lexer_rollback_tokens
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192 (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
195 static void cp_lexer_start_debugging
196 (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198 (cp_lexer *) ATTRIBUTE_UNUSED;
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201 about passing NULL to functions that require non-NULL arguments
202 (fputs, fprintf). It will never be used, so all we need is a value
203 of the right type that's guaranteed not to be NULL. */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
209 static cp_token_cache *cp_token_cache_new
210 (cp_token *, cp_token *);
212 static void cp_parser_initial_pragma
215 /* Manifest constants. */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
219 /* A token type for keywords, as opposed to ordinary identifiers. */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
222 /* A token type for template-ids. If a template-id is processed while
223 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224 the value of the CPP_TEMPLATE_ID is whatever was returned by
225 cp_parser_template_id. */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
228 /* A token type for nested-name-specifiers. If a
229 nested-name-specifier is processed while parsing tentatively, it is
230 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232 cp_parser_nested_name_specifier_opt. */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
235 /* A token type for tokens that are not tokens at all; these are used
236 to represent slots in the array where there used to be a token
237 that has now been deleted. */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
240 /* The number of token types, including C++-specific ones. */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written. */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
250 /* Create a new main C++ lexer, the lexer that gets tokens from the
254 cp_lexer_new_main (void)
256 cp_token first_token;
263 /* It's possible that parsing the first pragma will load a PCH file,
264 which is a GC collection point. So we have to do that before
265 allocating any memory. */
266 cp_parser_initial_pragma (&first_token);
268 c_common_no_more_pch ();
270 /* Allocate the memory. */
271 lexer = GGC_CNEW (cp_lexer);
273 #ifdef ENABLE_CHECKING
274 /* Initially we are not debugging. */
275 lexer->debugging_p = false;
276 #endif /* ENABLE_CHECKING */
277 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
278 CP_SAVED_TOKEN_STACK);
280 /* Create the buffer. */
281 alloc = CP_LEXER_BUFFER_SIZE;
282 buffer = GGC_NEWVEC (cp_token, alloc);
284 /* Put the first token in the buffer. */
289 /* Get the remaining tokens from the preprocessor. */
290 while (pos->type != CPP_EOF)
297 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
298 pos = buffer + space;
300 cp_lexer_get_preprocessor_token (lexer, pos);
302 lexer->buffer = buffer;
303 lexer->buffer_length = alloc - space;
304 lexer->last_token = pos;
305 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
307 /* Subsequent preprocessor diagnostics should use compiler
308 diagnostic functions to get the compiler source location. */
311 gcc_assert (lexer->next_token->type != CPP_PURGED);
315 /* Create a new lexer whose token stream is primed with the tokens in
316 CACHE. When these tokens are exhausted, no new tokens will be read. */
319 cp_lexer_new_from_tokens (cp_token_cache *cache)
321 cp_token *first = cache->first;
322 cp_token *last = cache->last;
323 cp_lexer *lexer = GGC_CNEW (cp_lexer);
325 /* We do not own the buffer. */
326 lexer->buffer = NULL;
327 lexer->buffer_length = 0;
328 lexer->next_token = first == last ? &eof_token : first;
329 lexer->last_token = last;
331 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
332 CP_SAVED_TOKEN_STACK);
334 #ifdef ENABLE_CHECKING
335 /* Initially we are not debugging. */
336 lexer->debugging_p = false;
339 gcc_assert (lexer->next_token->type != CPP_PURGED);
343 /* Frees all resources associated with LEXER. */
346 cp_lexer_destroy (cp_lexer *lexer)
349 ggc_free (lexer->buffer);
350 VEC_free (cp_token_position, heap, lexer->saved_tokens);
354 /* Returns nonzero if debugging information should be output. */
356 #ifdef ENABLE_CHECKING
359 cp_lexer_debugging_p (cp_lexer *lexer)
361 return lexer->debugging_p;
364 #endif /* ENABLE_CHECKING */
366 static inline cp_token_position
367 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
369 gcc_assert (!previous_p || lexer->next_token != &eof_token);
371 return lexer->next_token - previous_p;
374 static inline cp_token *
375 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 /* nonzero if we are presently saving tokens. */
383 cp_lexer_saving_tokens (const cp_lexer* lexer)
385 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
388 /* Store the next token from the preprocessor in *TOKEN. Return true
389 if we reach EOF. If LEXER is NULL, assume we are handling an
390 initial #pragma pch_preprocess, and thus want the lexer to return
391 processed strings. */
394 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
396 static int is_extern_c = 0;
398 /* Get a new token from the preprocessor. */
400 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
401 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
402 token->keyword = RID_MAX;
403 token->pragma_kind = PRAGMA_NONE;
405 /* On some systems, some header files are surrounded by an
406 implicit extern "C" block. Set a flag in the token if it
407 comes from such a header. */
408 is_extern_c += pending_lang_change;
409 pending_lang_change = 0;
410 token->implicit_extern_c = is_extern_c > 0;
412 /* Check to see if this token is a keyword. */
413 if (token->type == CPP_NAME)
415 if (C_IS_RESERVED_WORD (token->u.value))
417 /* Mark this token as a keyword. */
418 token->type = CPP_KEYWORD;
419 /* Record which keyword. */
420 token->keyword = C_RID_CODE (token->u.value);
424 if (warn_cxx0x_compat
425 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
426 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
428 /* Warn about the C++0x keyword (but still treat it as
430 warning (OPT_Wc__0x_compat,
431 "identifier %qE will become a keyword in C++0x",
434 /* Clear out the C_RID_CODE so we don't warn about this
435 particular identifier-turned-keyword again. */
436 C_SET_RID_CODE (token->u.value, RID_MAX);
439 token->ambiguous_p = false;
440 token->keyword = RID_MAX;
443 /* Handle Objective-C++ keywords. */
444 else if (token->type == CPP_AT_NAME)
446 token->type = CPP_KEYWORD;
447 switch (C_RID_CODE (token->u.value))
449 /* Map 'class' to '@class', 'private' to '@private', etc. */
450 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
451 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
452 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
453 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
454 case RID_THROW: token->keyword = RID_AT_THROW; break;
455 case RID_TRY: token->keyword = RID_AT_TRY; break;
456 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
457 default: token->keyword = C_RID_CODE (token->u.value);
460 else if (token->type == CPP_PRAGMA)
462 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
463 token->pragma_kind = ((enum pragma_kind)
464 TREE_INT_CST_LOW (token->u.value));
465 token->u.value = NULL_TREE;
469 /* Update the globals input_location and the input file stack from TOKEN. */
471 cp_lexer_set_source_position_from_token (cp_token *token)
473 if (token->type != CPP_EOF)
475 input_location = token->location;
479 /* Return a pointer to the next token in the token stream, but do not
482 static inline cp_token *
483 cp_lexer_peek_token (cp_lexer *lexer)
485 if (cp_lexer_debugging_p (lexer))
487 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
488 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
489 putc ('\n', cp_lexer_debug_stream);
491 return lexer->next_token;
494 /* Return true if the next token has the indicated TYPE. */
497 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
499 return cp_lexer_peek_token (lexer)->type == type;
502 /* Return true if the next token does not have the indicated TYPE. */
505 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
507 return !cp_lexer_next_token_is (lexer, type);
510 /* Return true if the next token is the indicated KEYWORD. */
513 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
515 return cp_lexer_peek_token (lexer)->keyword == keyword;
518 /* Return true if the next token is not the indicated KEYWORD. */
521 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
523 return cp_lexer_peek_token (lexer)->keyword != keyword;
526 /* Return true if the next token is a keyword for a decl-specifier. */
529 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
533 token = cp_lexer_peek_token (lexer);
534 switch (token->keyword)
536 /* auto specifier: storage-class-specifier in C++,
537 simple-type-specifier in C++0x. */
539 /* Storage classes. */
545 /* Elaborated type specifiers. */
551 /* Simple type specifiers. */
565 /* GNU extensions. */
568 /* C++0x extensions. */
577 /* Return a pointer to the Nth token in the token stream. If N is 1,
578 then this is precisely equivalent to cp_lexer_peek_token (except
579 that it is not inline). One would like to disallow that case, but
580 there is one case (cp_parser_nth_token_starts_template_id) where
581 the caller passes a variable for N and it might be 1. */
584 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
588 /* N is 1-based, not zero-based. */
591 if (cp_lexer_debugging_p (lexer))
592 fprintf (cp_lexer_debug_stream,
593 "cp_lexer: peeking ahead %ld at token: ", (long)n);
596 token = lexer->next_token;
597 gcc_assert (!n || token != &eof_token);
601 if (token == lexer->last_token)
607 if (token->type != CPP_PURGED)
611 if (cp_lexer_debugging_p (lexer))
613 cp_lexer_print_token (cp_lexer_debug_stream, token);
614 putc ('\n', cp_lexer_debug_stream);
620 /* Return the next token, and advance the lexer's next_token pointer
621 to point to the next non-purged token. */
624 cp_lexer_consume_token (cp_lexer* lexer)
626 cp_token *token = lexer->next_token;
628 gcc_assert (token != &eof_token);
629 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634 if (lexer->next_token == lexer->last_token)
636 lexer->next_token = &eof_token;
641 while (lexer->next_token->type == CPP_PURGED);
643 cp_lexer_set_source_position_from_token (token);
645 /* Provide debugging output. */
646 if (cp_lexer_debugging_p (lexer))
648 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
649 cp_lexer_print_token (cp_lexer_debug_stream, token);
650 putc ('\n', cp_lexer_debug_stream);
656 /* Permanently remove the next token from the token stream, and
657 advance the next_token pointer to refer to the next non-purged
661 cp_lexer_purge_token (cp_lexer *lexer)
663 cp_token *tok = lexer->next_token;
665 gcc_assert (tok != &eof_token);
666 tok->type = CPP_PURGED;
667 tok->location = UNKNOWN_LOCATION;
668 tok->u.value = NULL_TREE;
669 tok->keyword = RID_MAX;
674 if (tok == lexer->last_token)
680 while (tok->type == CPP_PURGED);
681 lexer->next_token = tok;
684 /* Permanently remove all tokens after TOK, up to, but not
685 including, the token that will be returned next by
686 cp_lexer_peek_token. */
689 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
691 cp_token *peek = lexer->next_token;
693 if (peek == &eof_token)
694 peek = lexer->last_token;
696 gcc_assert (tok < peek);
698 for ( tok += 1; tok != peek; tok += 1)
700 tok->type = CPP_PURGED;
701 tok->location = UNKNOWN_LOCATION;
702 tok->u.value = NULL_TREE;
703 tok->keyword = RID_MAX;
707 /* Begin saving tokens. All tokens consumed after this point will be
711 cp_lexer_save_tokens (cp_lexer* lexer)
713 /* Provide debugging output. */
714 if (cp_lexer_debugging_p (lexer))
715 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
717 VEC_safe_push (cp_token_position, heap,
718 lexer->saved_tokens, lexer->next_token);
721 /* Commit to the portion of the token stream most recently saved. */
724 cp_lexer_commit_tokens (cp_lexer* lexer)
726 /* Provide debugging output. */
727 if (cp_lexer_debugging_p (lexer))
728 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
730 VEC_pop (cp_token_position, lexer->saved_tokens);
733 /* Return all tokens saved since the last call to cp_lexer_save_tokens
734 to the token stream. Stop saving tokens. */
737 cp_lexer_rollback_tokens (cp_lexer* lexer)
739 /* Provide debugging output. */
740 if (cp_lexer_debugging_p (lexer))
741 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
743 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
746 /* Print a representation of the TOKEN on the STREAM. */
748 #ifdef ENABLE_CHECKING
751 cp_lexer_print_token (FILE * stream, cp_token *token)
753 /* We don't use cpp_type2name here because the parser defines
754 a few tokens of its own. */
755 static const char *const token_names[] = {
756 /* cpplib-defined token types */
762 /* C++ parser token types - see "Manifest constants", above. */
765 "NESTED_NAME_SPECIFIER",
769 /* If we have a name for the token, print it out. Otherwise, we
770 simply give the numeric code. */
771 gcc_assert (token->type < ARRAY_SIZE(token_names));
772 fputs (token_names[token->type], stream);
774 /* For some tokens, print the associated data. */
778 /* Some keywords have a value that is not an IDENTIFIER_NODE.
779 For example, `struct' is mapped to an INTEGER_CST. */
780 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
782 /* else fall through */
784 fputs (IDENTIFIER_POINTER (token->u.value), stream);
791 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
799 /* Start emitting debugging information. */
802 cp_lexer_start_debugging (cp_lexer* lexer)
804 lexer->debugging_p = true;
807 /* Stop emitting debugging information. */
810 cp_lexer_stop_debugging (cp_lexer* lexer)
812 lexer->debugging_p = false;
815 #endif /* ENABLE_CHECKING */
817 /* Create a new cp_token_cache, representing a range of tokens. */
819 static cp_token_cache *
820 cp_token_cache_new (cp_token *first, cp_token *last)
822 cp_token_cache *cache = GGC_NEW (cp_token_cache);
823 cache->first = first;
829 /* Decl-specifiers. */
831 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
834 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
836 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
841 /* Nothing other than the parser should be creating declarators;
842 declarators are a semi-syntactic representation of C++ entities.
843 Other parts of the front end that need to create entities (like
844 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
846 static cp_declarator *make_call_declarator
847 (cp_declarator *, tree, cp_cv_quals, tree, tree);
848 static cp_declarator *make_array_declarator
849 (cp_declarator *, tree);
850 static cp_declarator *make_pointer_declarator
851 (cp_cv_quals, cp_declarator *);
852 static cp_declarator *make_reference_declarator
853 (cp_cv_quals, cp_declarator *, bool);
854 static cp_parameter_declarator *make_parameter_declarator
855 (cp_decl_specifier_seq *, cp_declarator *, tree);
856 static cp_declarator *make_ptrmem_declarator
857 (cp_cv_quals, tree, cp_declarator *);
859 /* An erroneous declarator. */
860 static cp_declarator *cp_error_declarator;
862 /* The obstack on which declarators and related data structures are
864 static struct obstack declarator_obstack;
866 /* Alloc BYTES from the declarator memory pool. */
869 alloc_declarator (size_t bytes)
871 return obstack_alloc (&declarator_obstack, bytes);
874 /* Allocate a declarator of the indicated KIND. Clear fields that are
875 common to all declarators. */
877 static cp_declarator *
878 make_declarator (cp_declarator_kind kind)
880 cp_declarator *declarator;
882 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
883 declarator->kind = kind;
884 declarator->attributes = NULL_TREE;
885 declarator->declarator = NULL;
886 declarator->parameter_pack_p = false;
891 /* Make a declarator for a generalized identifier. If
892 QUALIFYING_SCOPE is non-NULL, the identifier is
893 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
894 UNQUALIFIED_NAME. SFK indicates the kind of special function this
897 static cp_declarator *
898 make_id_declarator (tree qualifying_scope, tree unqualified_name,
899 special_function_kind sfk)
901 cp_declarator *declarator;
903 /* It is valid to write:
905 class C { void f(); };
909 The standard is not clear about whether `typedef const C D' is
910 legal; as of 2002-09-15 the committee is considering that
911 question. EDG 3.0 allows that syntax. Therefore, we do as
913 if (qualifying_scope && TYPE_P (qualifying_scope))
914 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
916 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
917 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
918 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
920 declarator = make_declarator (cdk_id);
921 declarator->u.id.qualifying_scope = qualifying_scope;
922 declarator->u.id.unqualified_name = unqualified_name;
923 declarator->u.id.sfk = sfk;
928 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
929 of modifiers such as const or volatile to apply to the pointer
930 type, represented as identifiers. */
933 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
935 cp_declarator *declarator;
937 declarator = make_declarator (cdk_pointer);
938 declarator->declarator = target;
939 declarator->u.pointer.qualifiers = cv_qualifiers;
940 declarator->u.pointer.class_type = NULL_TREE;
943 declarator->parameter_pack_p = target->parameter_pack_p;
944 target->parameter_pack_p = false;
947 declarator->parameter_pack_p = false;
952 /* Like make_pointer_declarator -- but for references. */
955 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
958 cp_declarator *declarator;
960 declarator = make_declarator (cdk_reference);
961 declarator->declarator = target;
962 declarator->u.reference.qualifiers = cv_qualifiers;
963 declarator->u.reference.rvalue_ref = rvalue_ref;
966 declarator->parameter_pack_p = target->parameter_pack_p;
967 target->parameter_pack_p = false;
970 declarator->parameter_pack_p = false;
975 /* Like make_pointer_declarator -- but for a pointer to a non-static
976 member of CLASS_TYPE. */
979 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
980 cp_declarator *pointee)
982 cp_declarator *declarator;
984 declarator = make_declarator (cdk_ptrmem);
985 declarator->declarator = pointee;
986 declarator->u.pointer.qualifiers = cv_qualifiers;
987 declarator->u.pointer.class_type = class_type;
991 declarator->parameter_pack_p = pointee->parameter_pack_p;
992 pointee->parameter_pack_p = false;
995 declarator->parameter_pack_p = false;
1000 /* Make a declarator for the function given by TARGET, with the
1001 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1002 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1003 indicates what exceptions can be thrown. */
1006 make_call_declarator (cp_declarator *target,
1008 cp_cv_quals cv_qualifiers,
1009 tree exception_specification,
1010 tree late_return_type)
1012 cp_declarator *declarator;
1014 declarator = make_declarator (cdk_function);
1015 declarator->declarator = target;
1016 declarator->u.function.parameters = parms;
1017 declarator->u.function.qualifiers = cv_qualifiers;
1018 declarator->u.function.exception_specification = exception_specification;
1019 declarator->u.function.late_return_type = late_return_type;
1022 declarator->parameter_pack_p = target->parameter_pack_p;
1023 target->parameter_pack_p = false;
1026 declarator->parameter_pack_p = false;
1031 /* Make a declarator for an array of BOUNDS elements, each of which is
1032 defined by ELEMENT. */
1035 make_array_declarator (cp_declarator *element, tree bounds)
1037 cp_declarator *declarator;
1039 declarator = make_declarator (cdk_array);
1040 declarator->declarator = element;
1041 declarator->u.array.bounds = bounds;
1044 declarator->parameter_pack_p = element->parameter_pack_p;
1045 element->parameter_pack_p = false;
1048 declarator->parameter_pack_p = false;
1053 /* Determine whether the declarator we've seen so far can be a
1054 parameter pack, when followed by an ellipsis. */
1056 declarator_can_be_parameter_pack (cp_declarator *declarator)
1058 /* Search for a declarator name, or any other declarator that goes
1059 after the point where the ellipsis could appear in a parameter
1060 pack. If we find any of these, then this declarator can not be
1061 made into a parameter pack. */
1063 while (declarator && !found)
1065 switch ((int)declarator->kind)
1076 declarator = declarator->declarator;
1084 cp_parameter_declarator *no_parameters;
1086 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1087 DECLARATOR and DEFAULT_ARGUMENT. */
1089 cp_parameter_declarator *
1090 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1091 cp_declarator *declarator,
1092 tree default_argument)
1094 cp_parameter_declarator *parameter;
1096 parameter = ((cp_parameter_declarator *)
1097 alloc_declarator (sizeof (cp_parameter_declarator)));
1098 parameter->next = NULL;
1099 if (decl_specifiers)
1100 parameter->decl_specifiers = *decl_specifiers;
1102 clear_decl_specs (¶meter->decl_specifiers);
1103 parameter->declarator = declarator;
1104 parameter->default_argument = default_argument;
1105 parameter->ellipsis_p = false;
1110 /* Returns true iff DECLARATOR is a declaration for a function. */
1113 function_declarator_p (const cp_declarator *declarator)
1117 if (declarator->kind == cdk_function
1118 && declarator->declarator->kind == cdk_id)
1120 if (declarator->kind == cdk_id
1121 || declarator->kind == cdk_error)
1123 declarator = declarator->declarator;
1133 A cp_parser parses the token stream as specified by the C++
1134 grammar. Its job is purely parsing, not semantic analysis. For
1135 example, the parser breaks the token stream into declarators,
1136 expressions, statements, and other similar syntactic constructs.
1137 It does not check that the types of the expressions on either side
1138 of an assignment-statement are compatible, or that a function is
1139 not declared with a parameter of type `void'.
1141 The parser invokes routines elsewhere in the compiler to perform
1142 semantic analysis and to build up the abstract syntax tree for the
1145 The parser (and the template instantiation code, which is, in a
1146 way, a close relative of parsing) are the only parts of the
1147 compiler that should be calling push_scope and pop_scope, or
1148 related functions. The parser (and template instantiation code)
1149 keeps track of what scope is presently active; everything else
1150 should simply honor that. (The code that generates static
1151 initializers may also need to set the scope, in order to check
1152 access control correctly when emitting the initializers.)
1157 The parser is of the standard recursive-descent variety. Upcoming
1158 tokens in the token stream are examined in order to determine which
1159 production to use when parsing a non-terminal. Some C++ constructs
1160 require arbitrary look ahead to disambiguate. For example, it is
1161 impossible, in the general case, to tell whether a statement is an
1162 expression or declaration without scanning the entire statement.
1163 Therefore, the parser is capable of "parsing tentatively." When the
1164 parser is not sure what construct comes next, it enters this mode.
1165 Then, while we attempt to parse the construct, the parser queues up
1166 error messages, rather than issuing them immediately, and saves the
1167 tokens it consumes. If the construct is parsed successfully, the
1168 parser "commits", i.e., it issues any queued error messages and
1169 the tokens that were being preserved are permanently discarded.
1170 If, however, the construct is not parsed successfully, the parser
1171 rolls back its state completely so that it can resume parsing using
1172 a different alternative.
1177 The performance of the parser could probably be improved substantially.
1178 We could often eliminate the need to parse tentatively by looking ahead
1179 a little bit. In some places, this approach might not entirely eliminate
1180 the need to parse tentatively, but it might still speed up the average
1183 /* Flags that are passed to some parsing functions. These values can
1184 be bitwise-ored together. */
1189 CP_PARSER_FLAGS_NONE = 0x0,
1190 /* The construct is optional. If it is not present, then no error
1191 should be issued. */
1192 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1193 /* When parsing a type-specifier, do not allow user-defined types. */
1194 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1197 /* This type is used for parameters and variables which hold
1198 combinations of the above flags. */
1199 typedef int cp_parser_flags;
1201 /* The different kinds of declarators we want to parse. */
1203 typedef enum cp_parser_declarator_kind
1205 /* We want an abstract declarator. */
1206 CP_PARSER_DECLARATOR_ABSTRACT,
1207 /* We want a named declarator. */
1208 CP_PARSER_DECLARATOR_NAMED,
1209 /* We don't mind, but the name must be an unqualified-id. */
1210 CP_PARSER_DECLARATOR_EITHER
1211 } cp_parser_declarator_kind;
1213 /* The precedence values used to parse binary expressions. The minimum value
1214 of PREC must be 1, because zero is reserved to quickly discriminate
1215 binary operators from other tokens. */
1220 PREC_LOGICAL_OR_EXPRESSION,
1221 PREC_LOGICAL_AND_EXPRESSION,
1222 PREC_INCLUSIVE_OR_EXPRESSION,
1223 PREC_EXCLUSIVE_OR_EXPRESSION,
1224 PREC_AND_EXPRESSION,
1225 PREC_EQUALITY_EXPRESSION,
1226 PREC_RELATIONAL_EXPRESSION,
1227 PREC_SHIFT_EXPRESSION,
1228 PREC_ADDITIVE_EXPRESSION,
1229 PREC_MULTIPLICATIVE_EXPRESSION,
1231 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1234 /* A mapping from a token type to a corresponding tree node type, with a
1235 precedence value. */
1237 typedef struct cp_parser_binary_operations_map_node
1239 /* The token type. */
1240 enum cpp_ttype token_type;
1241 /* The corresponding tree code. */
1242 enum tree_code tree_type;
1243 /* The precedence of this operator. */
1244 enum cp_parser_prec prec;
1245 } cp_parser_binary_operations_map_node;
1247 /* The status of a tentative parse. */
1249 typedef enum cp_parser_status_kind
1251 /* No errors have occurred. */
1252 CP_PARSER_STATUS_KIND_NO_ERROR,
1253 /* An error has occurred. */
1254 CP_PARSER_STATUS_KIND_ERROR,
1255 /* We are committed to this tentative parse, whether or not an error
1257 CP_PARSER_STATUS_KIND_COMMITTED
1258 } cp_parser_status_kind;
1260 typedef struct cp_parser_expression_stack_entry
1262 /* Left hand side of the binary operation we are currently
1265 /* Original tree code for left hand side, if it was a binary
1266 expression itself (used for -Wparentheses). */
1267 enum tree_code lhs_type;
1268 /* Tree code for the binary operation we are parsing. */
1269 enum tree_code tree_type;
1270 /* Precedence of the binary operation we are parsing. */
1271 enum cp_parser_prec prec;
1272 } cp_parser_expression_stack_entry;
1274 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1275 entries because precedence levels on the stack are monotonically
1277 typedef struct cp_parser_expression_stack_entry
1278 cp_parser_expression_stack[NUM_PREC_VALUES];
1280 /* Context that is saved and restored when parsing tentatively. */
1281 typedef struct GTY (()) cp_parser_context {
1282 /* If this is a tentative parsing context, the status of the
1284 enum cp_parser_status_kind status;
1285 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1286 that are looked up in this context must be looked up both in the
1287 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1288 the context of the containing expression. */
1291 /* The next parsing context in the stack. */
1292 struct cp_parser_context *next;
1293 } cp_parser_context;
1297 /* Constructors and destructors. */
1299 static cp_parser_context *cp_parser_context_new
1300 (cp_parser_context *);
1302 /* Class variables. */
1304 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1306 /* The operator-precedence table used by cp_parser_binary_expression.
1307 Transformed into an associative array (binops_by_token) by
1310 static const cp_parser_binary_operations_map_node binops[] = {
1311 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1312 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1314 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1316 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1319 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1322 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1330 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1332 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1334 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1336 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1338 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1340 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1343 /* The same as binops, but initialized by cp_parser_new so that
1344 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1346 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1348 /* Constructors and destructors. */
1350 /* Construct a new context. The context below this one on the stack
1351 is given by NEXT. */
1353 static cp_parser_context *
1354 cp_parser_context_new (cp_parser_context* next)
1356 cp_parser_context *context;
1358 /* Allocate the storage. */
1359 if (cp_parser_context_free_list != NULL)
1361 /* Pull the first entry from the free list. */
1362 context = cp_parser_context_free_list;
1363 cp_parser_context_free_list = context->next;
1364 memset (context, 0, sizeof (*context));
1367 context = GGC_CNEW (cp_parser_context);
1369 /* No errors have occurred yet in this context. */
1370 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1371 /* If this is not the bottommost context, copy information that we
1372 need from the previous context. */
1375 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1376 expression, then we are parsing one in this context, too. */
1377 context->object_type = next->object_type;
1378 /* Thread the stack. */
1379 context->next = next;
1385 /* The cp_parser structure represents the C++ parser. */
1387 typedef struct GTY(()) cp_parser {
1388 /* The lexer from which we are obtaining tokens. */
1391 /* The scope in which names should be looked up. If NULL_TREE, then
1392 we look up names in the scope that is currently open in the
1393 source program. If non-NULL, this is either a TYPE or
1394 NAMESPACE_DECL for the scope in which we should look. It can
1395 also be ERROR_MARK, when we've parsed a bogus scope.
1397 This value is not cleared automatically after a name is looked
1398 up, so we must be careful to clear it before starting a new look
1399 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1400 will look up `Z' in the scope of `X', rather than the current
1401 scope.) Unfortunately, it is difficult to tell when name lookup
1402 is complete, because we sometimes peek at a token, look it up,
1403 and then decide not to consume it. */
1406 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1407 last lookup took place. OBJECT_SCOPE is used if an expression
1408 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1409 respectively. QUALIFYING_SCOPE is used for an expression of the
1410 form "X::Y"; it refers to X. */
1412 tree qualifying_scope;
1414 /* A stack of parsing contexts. All but the bottom entry on the
1415 stack will be tentative contexts.
1417 We parse tentatively in order to determine which construct is in
1418 use in some situations. For example, in order to determine
1419 whether a statement is an expression-statement or a
1420 declaration-statement we parse it tentatively as a
1421 declaration-statement. If that fails, we then reparse the same
1422 token stream as an expression-statement. */
1423 cp_parser_context *context;
1425 /* True if we are parsing GNU C++. If this flag is not set, then
1426 GNU extensions are not recognized. */
1427 bool allow_gnu_extensions_p;
1429 /* TRUE if the `>' token should be interpreted as the greater-than
1430 operator. FALSE if it is the end of a template-id or
1431 template-parameter-list. In C++0x mode, this flag also applies to
1432 `>>' tokens, which are viewed as two consecutive `>' tokens when
1433 this flag is FALSE. */
1434 bool greater_than_is_operator_p;
1436 /* TRUE if default arguments are allowed within a parameter list
1437 that starts at this point. FALSE if only a gnu extension makes
1438 them permissible. */
1439 bool default_arg_ok_p;
1441 /* TRUE if we are parsing an integral constant-expression. See
1442 [expr.const] for a precise definition. */
1443 bool integral_constant_expression_p;
1445 /* TRUE if we are parsing an integral constant-expression -- but a
1446 non-constant expression should be permitted as well. This flag
1447 is used when parsing an array bound so that GNU variable-length
1448 arrays are tolerated. */
1449 bool allow_non_integral_constant_expression_p;
1451 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1452 been seen that makes the expression non-constant. */
1453 bool non_integral_constant_expression_p;
1455 /* TRUE if local variable names and `this' are forbidden in the
1457 bool local_variables_forbidden_p;
1459 /* TRUE if the declaration we are parsing is part of a
1460 linkage-specification of the form `extern string-literal
1462 bool in_unbraced_linkage_specification_p;
1464 /* TRUE if we are presently parsing a declarator, after the
1465 direct-declarator. */
1466 bool in_declarator_p;
1468 /* TRUE if we are presently parsing a template-argument-list. */
1469 bool in_template_argument_list_p;
1471 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1472 to IN_OMP_BLOCK if parsing OpenMP structured block and
1473 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1474 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1475 iteration-statement, OpenMP block or loop within that switch. */
1476 #define IN_SWITCH_STMT 1
1477 #define IN_ITERATION_STMT 2
1478 #define IN_OMP_BLOCK 4
1479 #define IN_OMP_FOR 8
1480 #define IN_IF_STMT 16
1481 unsigned char in_statement;
1483 /* TRUE if we are presently parsing the body of a switch statement.
1484 Note that this doesn't quite overlap with in_statement above.
1485 The difference relates to giving the right sets of error messages:
1486 "case not in switch" vs "break statement used with OpenMP...". */
1487 bool in_switch_statement_p;
1489 /* TRUE if we are parsing a type-id in an expression context. In
1490 such a situation, both "type (expr)" and "type (type)" are valid
1492 bool in_type_id_in_expr_p;
1494 /* TRUE if we are currently in a header file where declarations are
1495 implicitly extern "C". */
1496 bool implicit_extern_c;
1498 /* TRUE if strings in expressions should be translated to the execution
1500 bool translate_strings_p;
1502 /* TRUE if we are presently parsing the body of a function, but not
1504 bool in_function_body;
1506 /* If non-NULL, then we are parsing a construct where new type
1507 definitions are not permitted. The string stored here will be
1508 issued as an error message if a type is defined. */
1509 const char *type_definition_forbidden_message;
1511 /* A list of lists. The outer list is a stack, used for member
1512 functions of local classes. At each level there are two sub-list,
1513 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1514 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1515 TREE_VALUE's. The functions are chained in reverse declaration
1518 The TREE_PURPOSE sublist contains those functions with default
1519 arguments that need post processing, and the TREE_VALUE sublist
1520 contains those functions with definitions that need post
1523 These lists can only be processed once the outermost class being
1524 defined is complete. */
1525 tree unparsed_functions_queues;
1527 /* The number of classes whose definitions are currently in
1529 unsigned num_classes_being_defined;
1531 /* The number of template parameter lists that apply directly to the
1532 current declaration. */
1533 unsigned num_template_parameter_lists;
1538 /* Constructors and destructors. */
1540 static cp_parser *cp_parser_new
1543 /* Routines to parse various constructs.
1545 Those that return `tree' will return the error_mark_node (rather
1546 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1547 Sometimes, they will return an ordinary node if error-recovery was
1548 attempted, even though a parse error occurred. So, to check
1549 whether or not a parse error occurred, you should always use
1550 cp_parser_error_occurred. If the construct is optional (indicated
1551 either by an `_opt' in the name of the function that does the
1552 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1553 the construct is not present. */
1555 /* Lexical conventions [gram.lex] */
1557 static tree cp_parser_identifier
1559 static tree cp_parser_string_literal
1560 (cp_parser *, bool, bool);
1562 /* Basic concepts [gram.basic] */
1564 static bool cp_parser_translation_unit
1567 /* Expressions [gram.expr] */
1569 static tree cp_parser_primary_expression
1570 (cp_parser *, bool, bool, bool, cp_id_kind *);
1571 static tree cp_parser_id_expression
1572 (cp_parser *, bool, bool, bool *, bool, bool);
1573 static tree cp_parser_unqualified_id
1574 (cp_parser *, bool, bool, bool, bool);
1575 static tree cp_parser_nested_name_specifier_opt
1576 (cp_parser *, bool, bool, bool, bool);
1577 static tree cp_parser_nested_name_specifier
1578 (cp_parser *, bool, bool, bool, bool);
1579 static tree cp_parser_qualifying_entity
1580 (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582 (cp_parser *, bool, bool, bool, cp_id_kind *);
1583 static tree cp_parser_postfix_open_square_expression
1584 (cp_parser *, tree, bool);
1585 static tree cp_parser_postfix_dot_deref_expression
1586 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1587 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1588 (cp_parser *, bool, bool, bool, bool *);
1589 static void cp_parser_pseudo_destructor_name
1590 (cp_parser *, tree *, tree *);
1591 static tree cp_parser_unary_expression
1592 (cp_parser *, bool, bool, cp_id_kind *);
1593 static enum tree_code cp_parser_unary_operator
1595 static tree cp_parser_new_expression
1597 static VEC(tree,gc) *cp_parser_new_placement
1599 static tree cp_parser_new_type_id
1600 (cp_parser *, tree *);
1601 static cp_declarator *cp_parser_new_declarator_opt
1603 static cp_declarator *cp_parser_direct_new_declarator
1605 static VEC(tree,gc) *cp_parser_new_initializer
1607 static tree cp_parser_delete_expression
1609 static tree cp_parser_cast_expression
1610 (cp_parser *, bool, bool, cp_id_kind *);
1611 static tree cp_parser_binary_expression
1612 (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1613 static tree cp_parser_question_colon_clause
1614 (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616 (cp_parser *, bool, cp_id_kind *);
1617 static enum tree_code cp_parser_assignment_operator_opt
1619 static tree cp_parser_expression
1620 (cp_parser *, bool, cp_id_kind *);
1621 static tree cp_parser_constant_expression
1622 (cp_parser *, bool, bool *);
1623 static tree cp_parser_builtin_offsetof
1626 /* Statements [gram.stmt.stmt] */
1628 static void cp_parser_statement
1629 (cp_parser *, tree, bool, bool *);
1630 static void cp_parser_label_for_labeled_statement
1632 static tree cp_parser_expression_statement
1633 (cp_parser *, tree);
1634 static tree cp_parser_compound_statement
1635 (cp_parser *, tree, bool);
1636 static void cp_parser_statement_seq_opt
1637 (cp_parser *, tree);
1638 static tree cp_parser_selection_statement
1639 (cp_parser *, bool *);
1640 static tree cp_parser_condition
1642 static tree cp_parser_iteration_statement
1644 static void cp_parser_for_init_statement
1646 static tree cp_parser_jump_statement
1648 static void cp_parser_declaration_statement
1651 static tree cp_parser_implicitly_scoped_statement
1652 (cp_parser *, bool *);
1653 static void cp_parser_already_scoped_statement
1656 /* Declarations [gram.dcl.dcl] */
1658 static void cp_parser_declaration_seq_opt
1660 static void cp_parser_declaration
1662 static void cp_parser_block_declaration
1663 (cp_parser *, bool);
1664 static void cp_parser_simple_declaration
1665 (cp_parser *, bool);
1666 static void cp_parser_decl_specifier_seq
1667 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1668 static tree cp_parser_storage_class_specifier_opt
1670 static tree cp_parser_function_specifier_opt
1671 (cp_parser *, cp_decl_specifier_seq *);
1672 static tree cp_parser_type_specifier
1673 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1675 static tree cp_parser_simple_type_specifier
1676 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1677 static tree cp_parser_type_name
1679 static tree cp_parser_nonclass_name
1680 (cp_parser* parser);
1681 static tree cp_parser_elaborated_type_specifier
1682 (cp_parser *, bool, bool);
1683 static tree cp_parser_enum_specifier
1685 static void cp_parser_enumerator_list
1686 (cp_parser *, tree);
1687 static void cp_parser_enumerator_definition
1688 (cp_parser *, tree);
1689 static tree cp_parser_namespace_name
1691 static void cp_parser_namespace_definition
1693 static void cp_parser_namespace_body
1695 static tree cp_parser_qualified_namespace_specifier
1697 static void cp_parser_namespace_alias_definition
1699 static bool cp_parser_using_declaration
1700 (cp_parser *, bool);
1701 static void cp_parser_using_directive
1703 static void cp_parser_asm_definition
1705 static void cp_parser_linkage_specification
1707 static void cp_parser_static_assert
1708 (cp_parser *, bool);
1709 static tree cp_parser_decltype
1712 /* Declarators [gram.dcl.decl] */
1714 static tree cp_parser_init_declarator
1715 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1716 static cp_declarator *cp_parser_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1718 static cp_declarator *cp_parser_direct_declarator
1719 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1720 static enum tree_code cp_parser_ptr_operator
1721 (cp_parser *, tree *, cp_cv_quals *);
1722 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1724 static tree cp_parser_late_return_type_opt
1726 static tree cp_parser_declarator_id
1727 (cp_parser *, bool);
1728 static tree cp_parser_type_id
1730 static tree cp_parser_template_type_arg
1732 static tree cp_parser_type_id_1
1733 (cp_parser *, bool);
1734 static void cp_parser_type_specifier_seq
1735 (cp_parser *, bool, cp_decl_specifier_seq *);
1736 static tree cp_parser_parameter_declaration_clause
1738 static tree cp_parser_parameter_declaration_list
1739 (cp_parser *, bool *);
1740 static cp_parameter_declarator *cp_parser_parameter_declaration
1741 (cp_parser *, bool, bool *);
1742 static tree cp_parser_default_argument
1743 (cp_parser *, bool);
1744 static void cp_parser_function_body
1746 static tree cp_parser_initializer
1747 (cp_parser *, bool *, bool *);
1748 static tree cp_parser_initializer_clause
1749 (cp_parser *, bool *);
1750 static tree cp_parser_braced_list
1751 (cp_parser*, bool*);
1752 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1753 (cp_parser *, bool *);
1755 static bool cp_parser_ctor_initializer_opt_and_function_body
1758 /* Classes [gram.class] */
1760 static tree cp_parser_class_name
1761 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1762 static tree cp_parser_class_specifier
1764 static tree cp_parser_class_head
1765 (cp_parser *, bool *, tree *, tree *);
1766 static enum tag_types cp_parser_class_key
1768 static void cp_parser_member_specification_opt
1770 static void cp_parser_member_declaration
1772 static tree cp_parser_pure_specifier
1774 static tree cp_parser_constant_initializer
1777 /* Derived classes [gram.class.derived] */
1779 static tree cp_parser_base_clause
1781 static tree cp_parser_base_specifier
1784 /* Special member functions [gram.special] */
1786 static tree cp_parser_conversion_function_id
1788 static tree cp_parser_conversion_type_id
1790 static cp_declarator *cp_parser_conversion_declarator_opt
1792 static bool cp_parser_ctor_initializer_opt
1794 static void cp_parser_mem_initializer_list
1796 static tree cp_parser_mem_initializer
1798 static tree cp_parser_mem_initializer_id
1801 /* Overloading [gram.over] */
1803 static tree cp_parser_operator_function_id
1805 static tree cp_parser_operator
1808 /* Templates [gram.temp] */
1810 static void cp_parser_template_declaration
1811 (cp_parser *, bool);
1812 static tree cp_parser_template_parameter_list
1814 static tree cp_parser_template_parameter
1815 (cp_parser *, bool *, bool *);
1816 static tree cp_parser_type_parameter
1817 (cp_parser *, bool *);
1818 static tree cp_parser_template_id
1819 (cp_parser *, bool, bool, bool);
1820 static tree cp_parser_template_name
1821 (cp_parser *, bool, bool, bool, bool *);
1822 static tree cp_parser_template_argument_list
1824 static tree cp_parser_template_argument
1826 static void cp_parser_explicit_instantiation
1828 static void cp_parser_explicit_specialization
1831 /* Exception handling [gram.exception] */
1833 static tree cp_parser_try_block
1835 static bool cp_parser_function_try_block
1837 static void cp_parser_handler_seq
1839 static void cp_parser_handler
1841 static tree cp_parser_exception_declaration
1843 static tree cp_parser_throw_expression
1845 static tree cp_parser_exception_specification_opt
1847 static tree cp_parser_type_id_list
1850 /* GNU Extensions */
1852 static tree cp_parser_asm_specification_opt
1854 static tree cp_parser_asm_operand_list
1856 static tree cp_parser_asm_clobber_list
1858 static tree cp_parser_attributes_opt
1860 static tree cp_parser_attribute_list
1862 static bool cp_parser_extension_opt
1863 (cp_parser *, int *);
1864 static void cp_parser_label_declaration
1867 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1868 static bool cp_parser_pragma
1869 (cp_parser *, enum pragma_context);
1871 /* Objective-C++ Productions */
1873 static tree cp_parser_objc_message_receiver
1875 static tree cp_parser_objc_message_args
1877 static tree cp_parser_objc_message_expression
1879 static tree cp_parser_objc_encode_expression
1881 static tree cp_parser_objc_defs_expression
1883 static tree cp_parser_objc_protocol_expression
1885 static tree cp_parser_objc_selector_expression
1887 static tree cp_parser_objc_expression
1889 static bool cp_parser_objc_selector_p
1891 static tree cp_parser_objc_selector
1893 static tree cp_parser_objc_protocol_refs_opt
1895 static void cp_parser_objc_declaration
1897 static tree cp_parser_objc_statement
1900 /* Utility Routines */
1902 static tree cp_parser_lookup_name
1903 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1904 static tree cp_parser_lookup_name_simple
1905 (cp_parser *, tree, location_t);
1906 static tree cp_parser_maybe_treat_template_as_class
1908 static bool cp_parser_check_declarator_template_parameters
1909 (cp_parser *, cp_declarator *, location_t);
1910 static bool cp_parser_check_template_parameters
1911 (cp_parser *, unsigned, location_t, cp_declarator *);
1912 static tree cp_parser_simple_cast_expression
1914 static tree cp_parser_global_scope_opt
1915 (cp_parser *, bool);
1916 static bool cp_parser_constructor_declarator_p
1917 (cp_parser *, bool);
1918 static tree cp_parser_function_definition_from_specifiers_and_declarator
1919 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1920 static tree cp_parser_function_definition_after_declarator
1921 (cp_parser *, bool);
1922 static void cp_parser_template_declaration_after_export
1923 (cp_parser *, bool);
1924 static void cp_parser_perform_template_parameter_access_checks
1925 (VEC (deferred_access_check,gc)*);
1926 static tree cp_parser_single_declaration
1927 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1928 static tree cp_parser_functional_cast
1929 (cp_parser *, tree);
1930 static tree cp_parser_save_member_function_body
1931 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1932 static tree cp_parser_enclosed_template_argument_list
1934 static void cp_parser_save_default_args
1935 (cp_parser *, tree);
1936 static void cp_parser_late_parsing_for_member
1937 (cp_parser *, tree);
1938 static void cp_parser_late_parsing_default_args
1939 (cp_parser *, tree);
1940 static tree cp_parser_sizeof_operand
1941 (cp_parser *, enum rid);
1942 static tree cp_parser_trait_expr
1943 (cp_parser *, enum rid);
1944 static bool cp_parser_declares_only_class_p
1946 static void cp_parser_set_storage_class
1947 (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1948 static void cp_parser_set_decl_spec_type
1949 (cp_decl_specifier_seq *, tree, location_t, bool);
1950 static bool cp_parser_friend_p
1951 (const cp_decl_specifier_seq *);
1952 static cp_token *cp_parser_require
1953 (cp_parser *, enum cpp_ttype, const char *);
1954 static cp_token *cp_parser_require_keyword
1955 (cp_parser *, enum rid, const char *);
1956 static bool cp_parser_token_starts_function_definition_p
1958 static bool cp_parser_next_token_starts_class_definition_p
1960 static bool cp_parser_next_token_ends_template_argument_p
1962 static bool cp_parser_nth_token_starts_template_argument_list_p
1963 (cp_parser *, size_t);
1964 static enum tag_types cp_parser_token_is_class_key
1966 static void cp_parser_check_class_key
1967 (enum tag_types, tree type);
1968 static void cp_parser_check_access_in_redeclaration
1969 (tree type, location_t location);
1970 static bool cp_parser_optional_template_keyword
1972 static void cp_parser_pre_parsed_nested_name_specifier
1974 static bool cp_parser_cache_group
1975 (cp_parser *, enum cpp_ttype, unsigned);
1976 static void cp_parser_parse_tentatively
1978 static void cp_parser_commit_to_tentative_parse
1980 static void cp_parser_abort_tentative_parse
1982 static bool cp_parser_parse_definitely
1984 static inline bool cp_parser_parsing_tentatively
1986 static bool cp_parser_uncommitted_to_tentative_parse_p
1988 static void cp_parser_error
1989 (cp_parser *, const char *);
1990 static void cp_parser_name_lookup_error
1991 (cp_parser *, tree, tree, const char *, location_t);
1992 static bool cp_parser_simulate_error
1994 static bool cp_parser_check_type_definition
1996 static void cp_parser_check_for_definition_in_return_type
1997 (cp_declarator *, tree, location_t type_location);
1998 static void cp_parser_check_for_invalid_template_id
1999 (cp_parser *, tree, location_t location);
2000 static bool cp_parser_non_integral_constant_expression
2001 (cp_parser *, const char *);
2002 static void cp_parser_diagnose_invalid_type_name
2003 (cp_parser *, tree, tree, location_t);
2004 static bool cp_parser_parse_and_diagnose_invalid_type_name
2006 static int cp_parser_skip_to_closing_parenthesis
2007 (cp_parser *, bool, bool, bool);
2008 static void cp_parser_skip_to_end_of_statement
2010 static void cp_parser_consume_semicolon_at_end_of_statement
2012 static void cp_parser_skip_to_end_of_block_or_statement
2014 static bool cp_parser_skip_to_closing_brace
2016 static void cp_parser_skip_to_end_of_template_parameter_list
2018 static void cp_parser_skip_to_pragma_eol
2019 (cp_parser*, cp_token *);
2020 static bool cp_parser_error_occurred
2022 static bool cp_parser_allow_gnu_extensions_p
2024 static bool cp_parser_is_string_literal
2026 static bool cp_parser_is_keyword
2027 (cp_token *, enum rid);
2028 static tree cp_parser_make_typename_type
2029 (cp_parser *, tree, tree, location_t location);
2030 static cp_declarator * cp_parser_make_indirect_declarator
2031 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2033 /* Returns nonzero if we are parsing tentatively. */
2036 cp_parser_parsing_tentatively (cp_parser* parser)
2038 return parser->context->next != NULL;
2041 /* Returns nonzero if TOKEN is a string literal. */
2044 cp_parser_is_string_literal (cp_token* token)
2046 return (token->type == CPP_STRING ||
2047 token->type == CPP_STRING16 ||
2048 token->type == CPP_STRING32 ||
2049 token->type == CPP_WSTRING);
2052 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2055 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2057 return token->keyword == keyword;
2060 /* If not parsing tentatively, issue a diagnostic of the form
2061 FILE:LINE: MESSAGE before TOKEN
2062 where TOKEN is the next token in the input stream. MESSAGE
2063 (specified by the caller) is usually of the form "expected
2067 cp_parser_error (cp_parser* parser, const char* message)
2069 if (!cp_parser_simulate_error (parser))
2071 cp_token *token = cp_lexer_peek_token (parser->lexer);
2072 /* This diagnostic makes more sense if it is tagged to the line
2073 of the token we just peeked at. */
2074 cp_lexer_set_source_position_from_token (token);
2076 if (token->type == CPP_PRAGMA)
2078 error ("%H%<#pragma%> is not allowed here", &token->location);
2079 cp_parser_skip_to_pragma_eol (parser, token);
2083 c_parse_error (message,
2084 /* Because c_parser_error does not understand
2085 CPP_KEYWORD, keywords are treated like
2087 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2088 token->u.value, token->flags);
2092 /* Issue an error about name-lookup failing. NAME is the
2093 IDENTIFIER_NODE DECL is the result of
2094 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2095 the thing that we hoped to find. */
2098 cp_parser_name_lookup_error (cp_parser* parser,
2101 const char* desired,
2102 location_t location)
2104 /* If name lookup completely failed, tell the user that NAME was not
2106 if (decl == error_mark_node)
2108 if (parser->scope && parser->scope != global_namespace)
2109 error ("%H%<%E::%E%> has not been declared",
2110 &location, parser->scope, name);
2111 else if (parser->scope == global_namespace)
2112 error ("%H%<::%E%> has not been declared", &location, name);
2113 else if (parser->object_scope
2114 && !CLASS_TYPE_P (parser->object_scope))
2115 error ("%Hrequest for member %qE in non-class type %qT",
2116 &location, name, parser->object_scope);
2117 else if (parser->object_scope)
2118 error ("%H%<%T::%E%> has not been declared",
2119 &location, parser->object_scope, name);
2121 error ("%H%qE has not been declared", &location, name);
2123 else if (parser->scope && parser->scope != global_namespace)
2124 error ("%H%<%E::%E%> %s", &location, parser->scope, name, desired);
2125 else if (parser->scope == global_namespace)
2126 error ("%H%<::%E%> %s", &location, name, desired);
2128 error ("%H%qE %s", &location, name, desired);
2131 /* If we are parsing tentatively, remember that an error has occurred
2132 during this tentative parse. Returns true if the error was
2133 simulated; false if a message should be issued by the caller. */
2136 cp_parser_simulate_error (cp_parser* parser)
2138 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2140 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2146 /* Check for repeated decl-specifiers. */
2149 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2150 location_t location)
2154 for (ds = ds_first; ds != ds_last; ++ds)
2156 unsigned count = decl_specs->specs[ds];
2159 /* The "long" specifier is a special case because of "long long". */
2163 error ("%H%<long long long%> is too long for GCC", &location);
2165 pedwarn_cxx98 (location, OPT_Wlong_long,
2166 "ISO C++ 1998 does not support %<long long%>");
2170 static const char *const decl_spec_names[] = {
2186 error ("%Hduplicate %qs", &location, decl_spec_names[ds]);
2191 /* This function is called when a type is defined. If type
2192 definitions are forbidden at this point, an error message is
2196 cp_parser_check_type_definition (cp_parser* parser)
2198 /* If types are forbidden here, issue a message. */
2199 if (parser->type_definition_forbidden_message)
2201 /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2202 in the message need to be interpreted. */
2203 error (parser->type_definition_forbidden_message);
2209 /* This function is called when the DECLARATOR is processed. The TYPE
2210 was a type defined in the decl-specifiers. If it is invalid to
2211 define a type in the decl-specifiers for DECLARATOR, an error is
2212 issued. TYPE_LOCATION is the location of TYPE and is used
2213 for error reporting. */
2216 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2217 tree type, location_t type_location)
2219 /* [dcl.fct] forbids type definitions in return types.
2220 Unfortunately, it's not easy to know whether or not we are
2221 processing a return type until after the fact. */
2223 && (declarator->kind == cdk_pointer
2224 || declarator->kind == cdk_reference
2225 || declarator->kind == cdk_ptrmem))
2226 declarator = declarator->declarator;
2228 && declarator->kind == cdk_function)
2230 error ("%Hnew types may not be defined in a return type", &type_location);
2231 inform (type_location,
2232 "(perhaps a semicolon is missing after the definition of %qT)",
2237 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2238 "<" in any valid C++ program. If the next token is indeed "<",
2239 issue a message warning the user about what appears to be an
2240 invalid attempt to form a template-id. LOCATION is the location
2241 of the type-specifier (TYPE) */
2244 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2245 tree type, location_t location)
2247 cp_token_position start = 0;
2249 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2252 error ("%H%qT is not a template", &location, type);
2253 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2254 error ("%H%qE is not a template", &location, type);
2256 error ("%Hinvalid template-id", &location);
2257 /* Remember the location of the invalid "<". */
2258 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2259 start = cp_lexer_token_position (parser->lexer, true);
2260 /* Consume the "<". */
2261 cp_lexer_consume_token (parser->lexer);
2262 /* Parse the template arguments. */
2263 cp_parser_enclosed_template_argument_list (parser);
2264 /* Permanently remove the invalid template arguments so that
2265 this error message is not issued again. */
2267 cp_lexer_purge_tokens_after (parser->lexer, start);
2271 /* If parsing an integral constant-expression, issue an error message
2272 about the fact that THING appeared and return true. Otherwise,
2273 return false. In either case, set
2274 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2277 cp_parser_non_integral_constant_expression (cp_parser *parser,
2280 parser->non_integral_constant_expression_p = true;
2281 if (parser->integral_constant_expression_p)
2283 if (!parser->allow_non_integral_constant_expression_p)
2285 /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2286 in the message need to be interpreted. */
2287 char *message = concat (thing,
2288 " cannot appear in a constant-expression",
2298 /* Emit a diagnostic for an invalid type name. SCOPE is the
2299 qualifying scope (or NULL, if none) for ID. This function commits
2300 to the current active tentative parse, if any. (Otherwise, the
2301 problematic construct might be encountered again later, resulting
2302 in duplicate error messages.) LOCATION is the location of ID. */
2305 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2306 tree scope, tree id,
2307 location_t location)
2309 tree decl, old_scope;
2310 /* Try to lookup the identifier. */
2311 old_scope = parser->scope;
2312 parser->scope = scope;
2313 decl = cp_parser_lookup_name_simple (parser, id, location);
2314 parser->scope = old_scope;
2315 /* If the lookup found a template-name, it means that the user forgot
2316 to specify an argument list. Emit a useful error message. */
2317 if (TREE_CODE (decl) == TEMPLATE_DECL)
2318 error ("%Hinvalid use of template-name %qE without an argument list",
2320 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2321 error ("%Hinvalid use of destructor %qD as a type", &location, id);
2322 else if (TREE_CODE (decl) == TYPE_DECL)
2323 /* Something like 'unsigned A a;' */
2324 error ("%Hinvalid combination of multiple type-specifiers",
2326 else if (!parser->scope)
2328 /* Issue an error message. */
2329 error ("%H%qE does not name a type", &location, id);
2330 /* If we're in a template class, it's possible that the user was
2331 referring to a type from a base class. For example:
2333 template <typename T> struct A { typedef T X; };
2334 template <typename T> struct B : public A<T> { X x; };
2336 The user should have said "typename A<T>::X". */
2337 if (processing_template_decl && current_class_type
2338 && TYPE_BINFO (current_class_type))
2342 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2346 tree base_type = BINFO_TYPE (b);
2347 if (CLASS_TYPE_P (base_type)
2348 && dependent_type_p (base_type))
2351 /* Go from a particular instantiation of the
2352 template (which will have an empty TYPE_FIELDs),
2353 to the main version. */
2354 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2355 for (field = TYPE_FIELDS (base_type);
2357 field = TREE_CHAIN (field))
2358 if (TREE_CODE (field) == TYPE_DECL
2359 && DECL_NAME (field) == id)
2362 "(perhaps %<typename %T::%E%> was intended)",
2363 BINFO_TYPE (b), id);
2372 /* Here we diagnose qualified-ids where the scope is actually correct,
2373 but the identifier does not resolve to a valid type name. */
2374 else if (parser->scope != error_mark_node)
2376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2377 error ("%H%qE in namespace %qE does not name a type",
2378 &location, id, parser->scope);
2379 else if (TYPE_P (parser->scope))
2380 error ("%H%qE in class %qT does not name a type",
2381 &location, id, parser->scope);
2385 cp_parser_commit_to_tentative_parse (parser);
2388 /* Check for a common situation where a type-name should be present,
2389 but is not, and issue a sensible error message. Returns true if an
2390 invalid type-name was detected.
2392 The situation handled by this function are variable declarations of the
2393 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2394 Usually, `ID' should name a type, but if we got here it means that it
2395 does not. We try to emit the best possible error message depending on
2396 how exactly the id-expression looks like. */
2399 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2402 cp_token *token = cp_lexer_peek_token (parser->lexer);
2404 cp_parser_parse_tentatively (parser);
2405 id = cp_parser_id_expression (parser,
2406 /*template_keyword_p=*/false,
2407 /*check_dependency_p=*/true,
2408 /*template_p=*/NULL,
2409 /*declarator_p=*/true,
2410 /*optional_p=*/false);
2411 /* After the id-expression, there should be a plain identifier,
2412 otherwise this is not a simple variable declaration. Also, if
2413 the scope is dependent, we cannot do much. */
2414 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2415 || (parser->scope && TYPE_P (parser->scope)
2416 && dependent_type_p (parser->scope))
2417 || TREE_CODE (id) == TYPE_DECL)
2419 cp_parser_abort_tentative_parse (parser);
2422 if (!cp_parser_parse_definitely (parser))
2425 /* Emit a diagnostic for the invalid type. */
2426 cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2427 id, token->location);
2428 /* Skip to the end of the declaration; there's no point in
2429 trying to process it. */
2430 cp_parser_skip_to_end_of_block_or_statement (parser);
2434 /* Consume tokens up to, and including, the next non-nested closing `)'.
2435 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2436 are doing error recovery. Returns -1 if OR_COMMA is true and we
2437 found an unnested comma. */
2440 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2445 unsigned paren_depth = 0;
2446 unsigned brace_depth = 0;
2448 if (recovering && !or_comma
2449 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2454 cp_token * token = cp_lexer_peek_token (parser->lexer);
2456 switch (token->type)
2459 case CPP_PRAGMA_EOL:
2460 /* If we've run out of tokens, then there is no closing `)'. */
2464 /* This matches the processing in skip_to_end_of_statement. */
2469 case CPP_OPEN_BRACE:
2472 case CPP_CLOSE_BRACE:
2478 if (recovering && or_comma && !brace_depth && !paren_depth)
2482 case CPP_OPEN_PAREN:
2487 case CPP_CLOSE_PAREN:
2488 if (!brace_depth && !paren_depth--)
2491 cp_lexer_consume_token (parser->lexer);
2500 /* Consume the token. */
2501 cp_lexer_consume_token (parser->lexer);
2505 /* Consume tokens until we reach the end of the current statement.
2506 Normally, that will be just before consuming a `;'. However, if a
2507 non-nested `}' comes first, then we stop before consuming that. */
2510 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2512 unsigned nesting_depth = 0;
2516 cp_token *token = cp_lexer_peek_token (parser->lexer);
2518 switch (token->type)
2521 case CPP_PRAGMA_EOL:
2522 /* If we've run out of tokens, stop. */
2526 /* If the next token is a `;', we have reached the end of the
2532 case CPP_CLOSE_BRACE:
2533 /* If this is a non-nested '}', stop before consuming it.
2534 That way, when confronted with something like:
2538 we stop before consuming the closing '}', even though we
2539 have not yet reached a `;'. */
2540 if (nesting_depth == 0)
2543 /* If it is the closing '}' for a block that we have
2544 scanned, stop -- but only after consuming the token.
2550 we will stop after the body of the erroneously declared
2551 function, but before consuming the following `typedef'
2553 if (--nesting_depth == 0)
2555 cp_lexer_consume_token (parser->lexer);
2559 case CPP_OPEN_BRACE:
2567 /* Consume the token. */
2568 cp_lexer_consume_token (parser->lexer);
2572 /* This function is called at the end of a statement or declaration.
2573 If the next token is a semicolon, it is consumed; otherwise, error
2574 recovery is attempted. */
2577 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2579 /* Look for the trailing `;'. */
2580 if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2582 /* If there is additional (erroneous) input, skip to the end of
2584 cp_parser_skip_to_end_of_statement (parser);
2585 /* If the next token is now a `;', consume it. */
2586 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2587 cp_lexer_consume_token (parser->lexer);
2591 /* Skip tokens until we have consumed an entire block, or until we
2592 have consumed a non-nested `;'. */
2595 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2597 int nesting_depth = 0;
2599 while (nesting_depth >= 0)
2601 cp_token *token = cp_lexer_peek_token (parser->lexer);
2603 switch (token->type)
2606 case CPP_PRAGMA_EOL:
2607 /* If we've run out of tokens, stop. */
2611 /* Stop if this is an unnested ';'. */
2616 case CPP_CLOSE_BRACE:
2617 /* Stop if this is an unnested '}', or closes the outermost
2620 if (nesting_depth < 0)
2626 case CPP_OPEN_BRACE:
2635 /* Consume the token. */
2636 cp_lexer_consume_token (parser->lexer);
2640 /* Skip tokens until a non-nested closing curly brace is the next
2641 token, or there are no more tokens. Return true in the first case,
2645 cp_parser_skip_to_closing_brace (cp_parser *parser)
2647 unsigned nesting_depth = 0;
2651 cp_token *token = cp_lexer_peek_token (parser->lexer);
2653 switch (token->type)
2656 case CPP_PRAGMA_EOL:
2657 /* If we've run out of tokens, stop. */
2660 case CPP_CLOSE_BRACE:
2661 /* If the next token is a non-nested `}', then we have reached
2662 the end of the current block. */
2663 if (nesting_depth-- == 0)
2667 case CPP_OPEN_BRACE:
2668 /* If it the next token is a `{', then we are entering a new
2669 block. Consume the entire block. */
2677 /* Consume the token. */
2678 cp_lexer_consume_token (parser->lexer);
2682 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2683 parameter is the PRAGMA token, allowing us to purge the entire pragma
2687 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2691 parser->lexer->in_pragma = false;
2694 token = cp_lexer_consume_token (parser->lexer);
2695 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2697 /* Ensure that the pragma is not parsed again. */
2698 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2701 /* Require pragma end of line, resyncing with it as necessary. The
2702 arguments are as for cp_parser_skip_to_pragma_eol. */
2705 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2707 parser->lexer->in_pragma = false;
2708 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2709 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2712 /* This is a simple wrapper around make_typename_type. When the id is
2713 an unresolved identifier node, we can provide a superior diagnostic
2714 using cp_parser_diagnose_invalid_type_name. */
2717 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2718 tree id, location_t id_location)
2721 if (TREE_CODE (id) == IDENTIFIER_NODE)
2723 result = make_typename_type (scope, id, typename_type,
2724 /*complain=*/tf_none);
2725 if (result == error_mark_node)
2726 cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2729 return make_typename_type (scope, id, typename_type, tf_error);
2732 /* This is a wrapper around the
2733 make_{pointer,ptrmem,reference}_declarator functions that decides
2734 which one to call based on the CODE and CLASS_TYPE arguments. The
2735 CODE argument should be one of the values returned by
2736 cp_parser_ptr_operator. */
2737 static cp_declarator *
2738 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2739 cp_cv_quals cv_qualifiers,
2740 cp_declarator *target)
2742 if (code == ERROR_MARK)
2743 return cp_error_declarator;
2745 if (code == INDIRECT_REF)
2746 if (class_type == NULL_TREE)
2747 return make_pointer_declarator (cv_qualifiers, target);
2749 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2750 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2751 return make_reference_declarator (cv_qualifiers, target, false);
2752 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2753 return make_reference_declarator (cv_qualifiers, target, true);
2757 /* Create a new C++ parser. */
2760 cp_parser_new (void)
2766 /* cp_lexer_new_main is called before calling ggc_alloc because
2767 cp_lexer_new_main might load a PCH file. */
2768 lexer = cp_lexer_new_main ();
2770 /* Initialize the binops_by_token so that we can get the tree
2771 directly from the token. */
2772 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2773 binops_by_token[binops[i].token_type] = binops[i];
2775 parser = GGC_CNEW (cp_parser);
2776 parser->lexer = lexer;
2777 parser->context = cp_parser_context_new (NULL);
2779 /* For now, we always accept GNU extensions. */
2780 parser->allow_gnu_extensions_p = 1;
2782 /* The `>' token is a greater-than operator, not the end of a
2784 parser->greater_than_is_operator_p = true;
2786 parser->default_arg_ok_p = true;
2788 /* We are not parsing a constant-expression. */
2789 parser->integral_constant_expression_p = false;
2790 parser->allow_non_integral_constant_expression_p = false;
2791 parser->non_integral_constant_expression_p = false;
2793 /* Local variable names are not forbidden. */
2794 parser->local_variables_forbidden_p = false;
2796 /* We are not processing an `extern "C"' declaration. */
2797 parser->in_unbraced_linkage_specification_p = false;
2799 /* We are not processing a declarator. */
2800 parser->in_declarator_p = false;
2802 /* We are not processing a template-argument-list. */
2803 parser->in_template_argument_list_p = false;
2805 /* We are not in an iteration statement. */
2806 parser->in_statement = 0;
2808 /* We are not in a switch statement. */
2809 parser->in_switch_statement_p = false;
2811 /* We are not parsing a type-id inside an expression. */
2812 parser->in_type_id_in_expr_p = false;
2814 /* Declarations aren't implicitly extern "C". */
2815 parser->implicit_extern_c = false;
2817 /* String literals should be translated to the execution character set. */
2818 parser->translate_strings_p = true;
2820 /* We are not parsing a function body. */
2821 parser->in_function_body = false;
2823 /* The unparsed function queue is empty. */
2824 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2826 /* There are no classes being defined. */
2827 parser->num_classes_being_defined = 0;
2829 /* No template parameters apply. */
2830 parser->num_template_parameter_lists = 0;
2835 /* Create a cp_lexer structure which will emit the tokens in CACHE
2836 and push it onto the parser's lexer stack. This is used for delayed
2837 parsing of in-class method bodies and default arguments, and should
2838 not be confused with tentative parsing. */
2840 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2842 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2843 lexer->next = parser->lexer;
2844 parser->lexer = lexer;
2846 /* Move the current source position to that of the first token in the
2848 cp_lexer_set_source_position_from_token (lexer->next_token);
2851 /* Pop the top lexer off the parser stack. This is never used for the
2852 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2854 cp_parser_pop_lexer (cp_parser *parser)
2856 cp_lexer *lexer = parser->lexer;
2857 parser->lexer = lexer->next;
2858 cp_lexer_destroy (lexer);
2860 /* Put the current source position back where it was before this
2861 lexer was pushed. */
2862 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2865 /* Lexical conventions [gram.lex] */
2867 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2871 cp_parser_identifier (cp_parser* parser)
2875 /* Look for the identifier. */
2876 token = cp_parser_require (parser, CPP_NAME, "identifier");
2877 /* Return the value. */
2878 return token ? token->u.value : error_mark_node;
2881 /* Parse a sequence of adjacent string constants. Returns a
2882 TREE_STRING representing the combined, nul-terminated string
2883 constant. If TRANSLATE is true, translate the string to the
2884 execution character set. If WIDE_OK is true, a wide string is
2887 C++98 [lex.string] says that if a narrow string literal token is
2888 adjacent to a wide string literal token, the behavior is undefined.
2889 However, C99 6.4.5p4 says that this results in a wide string literal.
2890 We follow C99 here, for consistency with the C front end.
2892 This code is largely lifted from lex_string() in c-lex.c.
2894 FUTURE: ObjC++ will need to handle @-strings here. */
2896 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2900 struct obstack str_ob;
2901 cpp_string str, istr, *strs;
2903 enum cpp_ttype type;
2905 tok = cp_lexer_peek_token (parser->lexer);
2906 if (!cp_parser_is_string_literal (tok))
2908 cp_parser_error (parser, "expected string-literal");
2909 return error_mark_node;
2914 /* Try to avoid the overhead of creating and destroying an obstack
2915 for the common case of just one string. */
2916 if (!cp_parser_is_string_literal
2917 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2919 cp_lexer_consume_token (parser->lexer);
2921 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2922 str.len = TREE_STRING_LENGTH (tok->u.value);
2929 gcc_obstack_init (&str_ob);
2934 cp_lexer_consume_token (parser->lexer);
2936 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2937 str.len = TREE_STRING_LENGTH (tok->u.value);
2939 if (type != tok->type)
2941 if (type == CPP_STRING)
2943 else if (tok->type != CPP_STRING)
2944 error ("%Hunsupported non-standard concatenation "
2945 "of string literals", &tok->location);
2948 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2950 tok = cp_lexer_peek_token (parser->lexer);
2952 while (cp_parser_is_string_literal (tok));
2954 strs = (cpp_string *) obstack_finish (&str_ob);
2957 if (type != CPP_STRING && !wide_ok)
2959 cp_parser_error (parser, "a wide string is invalid in this context");
2963 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2964 (parse_in, strs, count, &istr, type))
2966 value = build_string (istr.len, (const char *)istr.text);
2967 free (CONST_CAST (unsigned char *, istr.text));
2973 TREE_TYPE (value) = char_array_type_node;
2976 TREE_TYPE (value) = char16_array_type_node;
2979 TREE_TYPE (value) = char32_array_type_node;
2982 TREE_TYPE (value) = wchar_array_type_node;
2986 value = fix_string_type (value);
2989 /* cpp_interpret_string has issued an error. */
2990 value = error_mark_node;
2993 obstack_free (&str_ob, 0);
2999 /* Basic concepts [gram.basic] */
3001 /* Parse a translation-unit.
3004 declaration-seq [opt]
3006 Returns TRUE if all went well. */
3009 cp_parser_translation_unit (cp_parser* parser)
3011 /* The address of the first non-permanent object on the declarator
3013 static void *declarator_obstack_base;
3017 /* Create the declarator obstack, if necessary. */
3018 if (!cp_error_declarator)
3020 gcc_obstack_init (&declarator_obstack);
3021 /* Create the error declarator. */
3022 cp_error_declarator = make_declarator (cdk_error);
3023 /* Create the empty parameter list. */
3024 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3025 /* Remember where the base of the declarator obstack lies. */
3026 declarator_obstack_base = obstack_next_free (&declarator_obstack);
3029 cp_parser_declaration_seq_opt (parser);
3031 /* If there are no tokens left then all went well. */
3032 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3034 /* Get rid of the token array; we don't need it any more. */
3035 cp_lexer_destroy (parser->lexer);
3036 parser->lexer = NULL;
3038 /* This file might have been a context that's implicitly extern
3039 "C". If so, pop the lang context. (Only relevant for PCH.) */
3040 if (parser->implicit_extern_c)
3042 pop_lang_context ();
3043 parser->implicit_extern_c = false;
3047 finish_translation_unit ();
3053 cp_parser_error (parser, "expected declaration");
3057 /* Make sure the declarator obstack was fully cleaned up. */
3058 gcc_assert (obstack_next_free (&declarator_obstack)
3059 == declarator_obstack_base);
3061 /* All went well. */
3065 /* Expressions [gram.expr] */
3067 /* Parse a primary-expression.
3078 ( compound-statement )
3079 __builtin_va_arg ( assignment-expression , type-id )
3080 __builtin_offsetof ( type-id , offsetof-expression )
3083 __has_nothrow_assign ( type-id )
3084 __has_nothrow_constructor ( type-id )
3085 __has_nothrow_copy ( type-id )
3086 __has_trivial_assign ( type-id )
3087 __has_trivial_constructor ( type-id )
3088 __has_trivial_copy ( type-id )
3089 __has_trivial_destructor ( type-id )
3090 __has_virtual_destructor ( type-id )
3091 __is_abstract ( type-id )
3092 __is_base_of ( type-id , type-id )
3093 __is_class ( type-id )
3094 __is_convertible_to ( type-id , type-id )
3095 __is_empty ( type-id )
3096 __is_enum ( type-id )
3097 __is_pod ( type-id )
3098 __is_polymorphic ( type-id )
3099 __is_union ( type-id )
3101 Objective-C++ Extension:
3109 ADDRESS_P is true iff this expression was immediately preceded by
3110 "&" and therefore might denote a pointer-to-member. CAST_P is true
3111 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3112 true iff this expression is a template argument.
3114 Returns a representation of the expression. Upon return, *IDK
3115 indicates what kind of id-expression (if any) was present. */
3118 cp_parser_primary_expression (cp_parser *parser,
3121 bool template_arg_p,
3124 cp_token *token = NULL;
3126 /* Assume the primary expression is not an id-expression. */
3127 *idk = CP_ID_KIND_NONE;
3129 /* Peek at the next token. */
3130 token = cp_lexer_peek_token (parser->lexer);
3131 switch (token->type)
3144 token = cp_lexer_consume_token (parser->lexer);
3145 if (TREE_CODE (token->u.value) == FIXED_CST)
3147 error ("%Hfixed-point types not supported in C++",
3149 return error_mark_node;
3151 /* Floating-point literals are only allowed in an integral
3152 constant expression if they are cast to an integral or
3153 enumeration type. */
3154 if (TREE_CODE (token->u.value) == REAL_CST
3155 && parser->integral_constant_expression_p
3158 /* CAST_P will be set even in invalid code like "int(2.7 +
3159 ...)". Therefore, we have to check that the next token
3160 is sure to end the cast. */
3163 cp_token *next_token;
3165 next_token = cp_lexer_peek_token (parser->lexer);
3166 if (/* The comma at the end of an
3167 enumerator-definition. */
3168 next_token->type != CPP_COMMA
3169 /* The curly brace at the end of an enum-specifier. */
3170 && next_token->type != CPP_CLOSE_BRACE
3171 /* The end of a statement. */
3172 && next_token->type != CPP_SEMICOLON
3173 /* The end of the cast-expression. */
3174 && next_token->type != CPP_CLOSE_PAREN
3175 /* The end of an array bound. */
3176 && next_token->type != CPP_CLOSE_SQUARE
3177 /* The closing ">" in a template-argument-list. */
3178 && (next_token->type != CPP_GREATER
3179 || parser->greater_than_is_operator_p)
3180 /* C++0x only: A ">>" treated like two ">" tokens,
3181 in a template-argument-list. */
3182 && (next_token->type != CPP_RSHIFT
3183 || (cxx_dialect == cxx98)
3184 || parser->greater_than_is_operator_p))
3188 /* If we are within a cast, then the constraint that the
3189 cast is to an integral or enumeration type will be
3190 checked at that point. If we are not within a cast, then
3191 this code is invalid. */
3193 cp_parser_non_integral_constant_expression
3194 (parser, "floating-point literal");
3196 return token->u.value;
3202 /* ??? Should wide strings be allowed when parser->translate_strings_p
3203 is false (i.e. in attributes)? If not, we can kill the third
3204 argument to cp_parser_string_literal. */
3205 return cp_parser_string_literal (parser,
3206 parser->translate_strings_p,
3209 case CPP_OPEN_PAREN:
3212 bool saved_greater_than_is_operator_p;
3214 /* Consume the `('. */
3215 cp_lexer_consume_token (parser->lexer);
3216 /* Within a parenthesized expression, a `>' token is always
3217 the greater-than operator. */
3218 saved_greater_than_is_operator_p
3219 = parser->greater_than_is_operator_p;
3220 parser->greater_than_is_operator_p = true;
3221 /* If we see `( { ' then we are looking at the beginning of
3222 a GNU statement-expression. */
3223 if (cp_parser_allow_gnu_extensions_p (parser)
3224 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3226 /* Statement-expressions are not allowed by the standard. */
3227 pedwarn (token->location, OPT_pedantic,
3228 "ISO C++ forbids braced-groups within expressions");
3230 /* And they're not allowed outside of a function-body; you
3231 cannot, for example, write:
3233 int i = ({ int j = 3; j + 1; });
3235 at class or namespace scope. */
3236 if (!parser->in_function_body
3237 || parser->in_template_argument_list_p)
3239 error ("%Hstatement-expressions are not allowed outside "
3240 "functions nor in template-argument lists",
3242 cp_parser_skip_to_end_of_block_or_statement (parser);
3243 expr = error_mark_node;
3247 /* Start the statement-expression. */
3248 expr = begin_stmt_expr ();
3249 /* Parse the compound-statement. */
3250 cp_parser_compound_statement (parser, expr, false);
3252 expr = finish_stmt_expr (expr, false);
3257 /* Parse the parenthesized expression. */
3258 expr = cp_parser_expression (parser, cast_p, idk);
3259 /* Let the front end know that this expression was
3260 enclosed in parentheses. This matters in case, for
3261 example, the expression is of the form `A::B', since
3262 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3264 finish_parenthesized_expr (expr);
3266 /* The `>' token might be the end of a template-id or
3267 template-parameter-list now. */
3268 parser->greater_than_is_operator_p
3269 = saved_greater_than_is_operator_p;
3270 /* Consume the `)'. */
3271 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3272 cp_parser_skip_to_end_of_statement (parser);
3278 switch (token->keyword)
3280 /* These two are the boolean literals. */
3282 cp_lexer_consume_token (parser->lexer);
3283 return boolean_true_node;
3285 cp_lexer_consume_token (parser->lexer);
3286 return boolean_false_node;
3288 /* The `__null' literal. */
3290 cp_lexer_consume_token (parser->lexer);
3293 /* Recognize the `this' keyword. */
3295 cp_lexer_consume_token (parser->lexer);
3296 if (parser->local_variables_forbidden_p)
3298 error ("%H%<this%> may not be used in this context",
3300 return error_mark_node;
3302 /* Pointers cannot appear in constant-expressions. */
3303 if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3304 return error_mark_node;
3305 return finish_this_expr ();
3307 /* The `operator' keyword can be the beginning of an
3312 case RID_FUNCTION_NAME:
3313 case RID_PRETTY_FUNCTION_NAME:
3314 case RID_C99_FUNCTION_NAME:
3318 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3319 __func__ are the names of variables -- but they are
3320 treated specially. Therefore, they are handled here,
3321 rather than relying on the generic id-expression logic
3322 below. Grammatically, these names are id-expressions.
3324 Consume the token. */
3325 token = cp_lexer_consume_token (parser->lexer);
3327 switch (token->keyword)
3329 case RID_FUNCTION_NAME:
3330 name = "%<__FUNCTION__%>";
3332 case RID_PRETTY_FUNCTION_NAME:
3333 name = "%<__PRETTY_FUNCTION__%>";
3335 case RID_C99_FUNCTION_NAME:
3336 name = "%<__func__%>";
3342 if (cp_parser_non_integral_constant_expression (parser, name))
3343 return error_mark_node;
3345 /* Look up the name. */
3346 return finish_fname (token->u.value);
3354 /* The `__builtin_va_arg' construct is used to handle
3355 `va_arg'. Consume the `__builtin_va_arg' token. */
3356 cp_lexer_consume_token (parser->lexer);
3357 /* Look for the opening `('. */
3358 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3359 /* Now, parse the assignment-expression. */
3360 expression = cp_parser_assignment_expression (parser,
3361 /*cast_p=*/false, NULL);
3362 /* Look for the `,'. */
3363 cp_parser_require (parser, CPP_COMMA, "%<,%>");
3364 /* Parse the type-id. */
3365 type = cp_parser_type_id (parser);
3366 /* Look for the closing `)'. */
3367 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3368 /* Using `va_arg' in a constant-expression is not
3370 if (cp_parser_non_integral_constant_expression (parser,
3372 return error_mark_node;
3373 return build_x_va_arg (expression, type);
3377 return cp_parser_builtin_offsetof (parser);
3379 case RID_HAS_NOTHROW_ASSIGN:
3380 case RID_HAS_NOTHROW_CONSTRUCTOR:
3381 case RID_HAS_NOTHROW_COPY:
3382 case RID_HAS_TRIVIAL_ASSIGN:
3383 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3384 case RID_HAS_TRIVIAL_COPY:
3385 case RID_HAS_TRIVIAL_DESTRUCTOR:
3386 case RID_HAS_VIRTUAL_DESTRUCTOR:
3387 case RID_IS_ABSTRACT:
3388 case RID_IS_BASE_OF:
3390 case RID_IS_CONVERTIBLE_TO:
3394 case RID_IS_POLYMORPHIC:
3396 return cp_parser_trait_expr (parser, token->keyword);
3398 /* Objective-C++ expressions. */
3400 case RID_AT_PROTOCOL:
3401 case RID_AT_SELECTOR:
3402 return cp_parser_objc_expression (parser);
3405 cp_parser_error (parser, "expected primary-expression");
3406 return error_mark_node;
3409 /* An id-expression can start with either an identifier, a
3410 `::' as the beginning of a qualified-id, or the "operator"
3414 case CPP_TEMPLATE_ID:
3415 case CPP_NESTED_NAME_SPECIFIER:
3419 const char *error_msg;
3422 cp_token *id_expr_token;
3425 /* Parse the id-expression. */
3427 = cp_parser_id_expression (parser,
3428 /*template_keyword_p=*/false,
3429 /*check_dependency_p=*/true,
3431 /*declarator_p=*/false,
3432 /*optional_p=*/false);
3433 if (id_expression == error_mark_node)
3434 return error_mark_node;
3435 id_expr_token = token;
3436 token = cp_lexer_peek_token (parser->lexer);
3437 done = (token->type != CPP_OPEN_SQUARE
3438 && token->type != CPP_OPEN_PAREN
3439 && token->type != CPP_DOT
3440 && token->type != CPP_DEREF
3441 && token->type != CPP_PLUS_PLUS
3442 && token->type != CPP_MINUS_MINUS);
3443 /* If we have a template-id, then no further lookup is
3444 required. If the template-id was for a template-class, we
3445 will sometimes have a TYPE_DECL at this point. */
3446 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3447 || TREE_CODE (id_expression) == TYPE_DECL)
3448 decl = id_expression;
3449 /* Look up the name. */
3452 tree ambiguous_decls;
3454 decl = cp_parser_lookup_name (parser, id_expression,
3457 /*is_namespace=*/false,
3458 /*check_dependency=*/true,
3460 id_expr_token->location);
3461 /* If the lookup was ambiguous, an error will already have
3463 if (ambiguous_decls)
3464 return error_mark_node;
3466 /* In Objective-C++, an instance variable (ivar) may be preferred
3467 to whatever cp_parser_lookup_name() found. */
3468 decl = objc_lookup_ivar (decl, id_expression);
3470 /* If name lookup gives us a SCOPE_REF, then the
3471 qualifying scope was dependent. */
3472 if (TREE_CODE (decl) == SCOPE_REF)
3474 /* At this point, we do not know if DECL is a valid
3475 integral constant expression. We assume that it is
3476 in fact such an expression, so that code like:
3478 template <int N> struct A {
3482 is accepted. At template-instantiation time, we
3483 will check that B<N>::i is actually a constant. */
3486 /* Check to see if DECL is a local variable in a context
3487 where that is forbidden. */
3488 if (parser->local_variables_forbidden_p
3489 && local_variable_p (decl))
3491 /* It might be that we only found DECL because we are
3492 trying to be generous with pre-ISO scoping rules.
3493 For example, consider:
3497 for (int i = 0; i < 10; ++i) {}
3498 extern void f(int j = i);
3501 Here, name look up will originally find the out
3502 of scope `i'. We need to issue a warning message,
3503 but then use the global `i'. */
3504 decl = check_for_out_of_scope_variable (decl);
3505 if (local_variable_p (decl))
3507 error ("%Hlocal variable %qD may not appear in this context",
3508 &id_expr_token->location, decl);
3509 return error_mark_node;
3514 decl = (finish_id_expression
3515 (id_expression, decl, parser->scope,
3517 parser->integral_constant_expression_p,
3518 parser->allow_non_integral_constant_expression_p,
3519 &parser->non_integral_constant_expression_p,
3520 template_p, done, address_p,
3523 id_expr_token->location));
3525 cp_parser_error (parser, error_msg);
3529 /* Anything else is an error. */
3531 /* ...unless we have an Objective-C++ message or string literal,
3533 if (c_dialect_objc ()
3534 && (token->type == CPP_OPEN_SQUARE
3535 || token->type == CPP_OBJC_STRING))
3536 return cp_parser_objc_expression (parser);
3538 cp_parser_error (parser, "expected primary-expression");
3539 return error_mark_node;
3543 /* Parse an id-expression.
3550 :: [opt] nested-name-specifier template [opt] unqualified-id
3552 :: operator-function-id
3555 Return a representation of the unqualified portion of the
3556 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3557 a `::' or nested-name-specifier.
3559 Often, if the id-expression was a qualified-id, the caller will
3560 want to make a SCOPE_REF to represent the qualified-id. This
3561 function does not do this in order to avoid wastefully creating
3562 SCOPE_REFs when they are not required.
3564 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3567 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3568 uninstantiated templates.
3570 If *TEMPLATE_P is non-NULL, it is set to true iff the
3571 `template' keyword is used to explicitly indicate that the entity
3572 named is a template.
3574 If DECLARATOR_P is true, the id-expression is appearing as part of
3575 a declarator, rather than as part of an expression. */
3578 cp_parser_id_expression (cp_parser *parser,
3579 bool template_keyword_p,
3580 bool check_dependency_p,
3585 bool global_scope_p;
3586 bool nested_name_specifier_p;
3588 /* Assume the `template' keyword was not used. */
3590 *template_p = template_keyword_p;
3592 /* Look for the optional `::' operator. */
3594 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3596 /* Look for the optional nested-name-specifier. */
3597 nested_name_specifier_p
3598 = (cp_parser_nested_name_specifier_opt (parser,
3599 /*typename_keyword_p=*/false,
3604 /* If there is a nested-name-specifier, then we are looking at
3605 the first qualified-id production. */
3606 if (nested_name_specifier_p)
3609 tree saved_object_scope;
3610 tree saved_qualifying_scope;
3611 tree unqualified_id;
3614 /* See if the next token is the `template' keyword. */
3616 template_p = &is_template;
3617 *template_p = cp_parser_optional_template_keyword (parser);
3618 /* Name lookup we do during the processing of the
3619 unqualified-id might obliterate SCOPE. */
3620 saved_scope = parser->scope;
3621 saved_object_scope = parser->object_scope;
3622 saved_qualifying_scope = parser->qualifying_scope;
3623 /* Process the final unqualified-id. */
3624 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3627 /*optional_p=*/false);
3628 /* Restore the SAVED_SCOPE for our caller. */
3629 parser->scope = saved_scope;
3630 parser->object_scope = saved_object_scope;
3631 parser->qualifying_scope = saved_qualifying_scope;
3633 return unqualified_id;
3635 /* Otherwise, if we are in global scope, then we are looking at one
3636 of the other qualified-id productions. */
3637 else if (global_scope_p)
3642 /* Peek at the next token. */
3643 token = cp_lexer_peek_token (parser->lexer);
3645 /* If it's an identifier, and the next token is not a "<", then
3646 we can avoid the template-id case. This is an optimization
3647 for this common case. */
3648 if (token->type == CPP_NAME
3649 && !cp_parser_nth_token_starts_template_argument_list_p
3651 return cp_parser_identifier (parser);
3653 cp_parser_parse_tentatively (parser);
3654 /* Try a template-id. */
3655 id = cp_parser_template_id (parser,
3656 /*template_keyword_p=*/false,
3657 /*check_dependency_p=*/true,
3659 /* If that worked, we're done. */
3660 if (cp_parser_parse_definitely (parser))
3663 /* Peek at the next token. (Changes in the token buffer may
3664 have invalidated the pointer obtained above.) */
3665 token = cp_lexer_peek_token (parser->lexer);
3667 switch (token->type)
3670 return cp_parser_identifier (parser);
3673 if (token->keyword == RID_OPERATOR)
3674 return cp_parser_operator_function_id (parser);
3678 cp_parser_error (parser, "expected id-expression");
3679 return error_mark_node;
3683 return cp_parser_unqualified_id (parser, template_keyword_p,
3684 /*check_dependency_p=*/true,
3689 /* Parse an unqualified-id.
3693 operator-function-id
3694 conversion-function-id
3698 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3699 keyword, in a construct like `A::template ...'.
3701 Returns a representation of unqualified-id. For the `identifier'
3702 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3703 production a BIT_NOT_EXPR is returned; the operand of the
3704 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3705 other productions, see the documentation accompanying the
3706 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3707 names are looked up in uninstantiated templates. If DECLARATOR_P
3708 is true, the unqualified-id is appearing as part of a declarator,
3709 rather than as part of an expression. */
3712 cp_parser_unqualified_id (cp_parser* parser,
3713 bool template_keyword_p,
3714 bool check_dependency_p,
3720 /* Peek at the next token. */
3721 token = cp_lexer_peek_token (parser->lexer);
3723 switch (token->type)
3729 /* We don't know yet whether or not this will be a
3731 cp_parser_parse_tentatively (parser);
3732 /* Try a template-id. */
3733 id = cp_parser_template_id (parser, template_keyword_p,
3736 /* If it worked, we're done. */
3737 if (cp_parser_parse_definitely (parser))
3739 /* Otherwise, it's an ordinary identifier. */
3740 return cp_parser_identifier (parser);
3743 case CPP_TEMPLATE_ID:
3744 return cp_parser_template_id (parser, template_keyword_p,
3751 tree qualifying_scope;
3756 /* Consume the `~' token. */
3757 cp_lexer_consume_token (parser->lexer);
3758 /* Parse the class-name. The standard, as written, seems to
3761 template <typename T> struct S { ~S (); };
3762 template <typename T> S<T>::~S() {}
3764 is invalid, since `~' must be followed by a class-name, but
3765 `S<T>' is dependent, and so not known to be a class.
3766 That's not right; we need to look in uninstantiated
3767 templates. A further complication arises from:
3769 template <typename T> void f(T t) {
3773 Here, it is not possible to look up `T' in the scope of `T'
3774 itself. We must look in both the current scope, and the
3775 scope of the containing complete expression.
3777 Yet another issue is:
3786 The standard does not seem to say that the `S' in `~S'
3787 should refer to the type `S' and not the data member
3790 /* DR 244 says that we look up the name after the "~" in the
3791 same scope as we looked up the qualifying name. That idea
3792 isn't fully worked out; it's more complicated than that. */
3793 scope = parser->scope;
3794 object_scope = parser->object_scope;
3795 qualifying_scope = parser->qualifying_scope;
3797 /* Check for invalid scopes. */
3798 if (scope == error_mark_node)
3800 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3801 cp_lexer_consume_token (parser->lexer);
3802 return error_mark_node;
3804 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3806 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3807 error ("%Hscope %qT before %<~%> is not a class-name",
3808 &token->location, scope);
3809 cp_parser_simulate_error (parser);
3810 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3811 cp_lexer_consume_token (parser->lexer);
3812 return error_mark_node;
3814 gcc_assert (!scope || TYPE_P (scope));
3816 /* If the name is of the form "X::~X" it's OK. */
3817 token = cp_lexer_peek_token (parser->lexer);
3819 && token->type == CPP_NAME
3820 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3822 && constructor_name_p (token->u.value, scope))
3824 cp_lexer_consume_token (parser->lexer);
3825 return build_nt (BIT_NOT_EXPR, scope);
3828 /* If there was an explicit qualification (S::~T), first look
3829 in the scope given by the qualification (i.e., S). */
3831 type_decl = NULL_TREE;
3834 cp_parser_parse_tentatively (parser);
3835 type_decl = cp_parser_class_name (parser,
3836 /*typename_keyword_p=*/false,
3837 /*template_keyword_p=*/false,
3839 /*check_dependency=*/false,
3840 /*class_head_p=*/false,
3842 if (cp_parser_parse_definitely (parser))
3845 /* In "N::S::~S", look in "N" as well. */
3846 if (!done && scope && qualifying_scope)
3848 cp_parser_parse_tentatively (parser);
3849 parser->scope = qualifying_scope;
3850 parser->object_scope = NULL_TREE;
3851 parser->qualifying_scope = NULL_TREE;
3853 = cp_parser_class_name (parser,
3854 /*typename_keyword_p=*/false,
3855 /*template_keyword_p=*/false,
3857 /*check_dependency=*/false,
3858 /*class_head_p=*/false,
3860 if (cp_parser_parse_definitely (parser))
3863 /* In "p->S::~T", look in the scope given by "*p" as well. */
3864 else if (!done && object_scope)
3866 cp_parser_parse_tentatively (parser);
3867 parser->scope = object_scope;
3868 parser->object_scope = NULL_TREE;
3869 parser->qualifying_scope = NULL_TREE;
3871 = cp_parser_class_name (parser,
3872 /*typename_keyword_p=*/false,
3873 /*template_keyword_p=*/false,
3875 /*check_dependency=*/false,
3876 /*class_head_p=*/false,
3878 if (cp_parser_parse_definitely (parser))
3881 /* Look in the surrounding context. */
3884 parser->scope = NULL_TREE;
3885 parser->object_scope = NULL_TREE;
3886 parser->qualifying_scope = NULL_TREE;
3887 if (processing_template_decl)
3888 cp_parser_parse_tentatively (parser);
3890 = cp_parser_class_name (parser,
3891 /*typename_keyword_p=*/false,
3892 /*template_keyword_p=*/false,
3894 /*check_dependency=*/false,
3895 /*class_head_p=*/false,
3897 if (processing_template_decl
3898 && ! cp_parser_parse_definitely (parser))
3900 /* We couldn't find a type with this name, so just accept
3901 it and check for a match at instantiation time. */
3902 type_decl = cp_parser_identifier (parser);
3903 if (type_decl != error_mark_node)
3904 type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3908 /* If an error occurred, assume that the name of the
3909 destructor is the same as the name of the qualifying
3910 class. That allows us to keep parsing after running
3911 into ill-formed destructor names. */
3912 if (type_decl == error_mark_node && scope)
3913 return build_nt (BIT_NOT_EXPR, scope);
3914 else if (type_decl == error_mark_node)
3915 return error_mark_node;
3917 /* Check that destructor name and scope match. */
3918 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3920 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3921 error ("%Hdeclaration of %<~%T%> as member of %qT",
3922 &token->location, type_decl, scope);
3923 cp_parser_simulate_error (parser);
3924 return error_mark_node;
3929 A typedef-name that names a class shall not be used as the
3930 identifier in the declarator for a destructor declaration. */
3932 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3933 && !DECL_SELF_REFERENCE_P (type_decl)
3934 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3935 error ("%Htypedef-name %qD used as destructor declarator",
3936 &token->location, type_decl);
3938 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3942 if (token->keyword == RID_OPERATOR)
3946 /* This could be a template-id, so we try that first. */
3947 cp_parser_parse_tentatively (parser);
3948 /* Try a template-id. */
3949 id = cp_parser_template_id (parser, template_keyword_p,
3950 /*check_dependency_p=*/true,
3952 /* If that worked, we're done. */
3953 if (cp_parser_parse_definitely (parser))
3955 /* We still don't know whether we're looking at an
3956 operator-function-id or a conversion-function-id. */
3957 cp_parser_parse_tentatively (parser);
3958 /* Try an operator-function-id. */
3959 id = cp_parser_operator_function_id (parser);
3960 /* If that didn't work, try a conversion-function-id. */
3961 if (!cp_parser_parse_definitely (parser))
3962 id = cp_parser_conversion_function_id (parser);
3971 cp_parser_error (parser, "expected unqualified-id");
3972 return error_mark_node;
3976 /* Parse an (optional) nested-name-specifier.
3978 nested-name-specifier: [C++98]
3979 class-or-namespace-name :: nested-name-specifier [opt]
3980 class-or-namespace-name :: template nested-name-specifier [opt]
3982 nested-name-specifier: [C++0x]
3985 nested-name-specifier identifier ::
3986 nested-name-specifier template [opt] simple-template-id ::
3988 PARSER->SCOPE should be set appropriately before this function is
3989 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3990 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3993 Sets PARSER->SCOPE to the class (TYPE) or namespace
3994 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3995 it unchanged if there is no nested-name-specifier. Returns the new
3996 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3998 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3999 part of a declaration and/or decl-specifier. */
4002 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4003 bool typename_keyword_p,
4004 bool check_dependency_p,
4006 bool is_declaration)
4008 bool success = false;
4009 cp_token_position start = 0;
4012 /* Remember where the nested-name-specifier starts. */
4013 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4015 start = cp_lexer_token_position (parser->lexer, false);
4016 push_deferring_access_checks (dk_deferred);
4023 tree saved_qualifying_scope;
4024 bool template_keyword_p;
4026 /* Spot cases that cannot be the beginning of a
4027 nested-name-specifier. */
4028 token = cp_lexer_peek_token (parser->lexer);
4030 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4031 the already parsed nested-name-specifier. */
4032 if (token->type == CPP_NESTED_NAME_SPECIFIER)
4034 /* Grab the nested-name-specifier and continue the loop. */
4035 cp_parser_pre_parsed_nested_name_specifier (parser);
4036 /* If we originally encountered this nested-name-specifier
4037 with IS_DECLARATION set to false, we will not have
4038 resolved TYPENAME_TYPEs, so we must do so here. */
4040 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4042 new_scope = resolve_typename_type (parser->scope,
4043 /*only_current_p=*/false);
4044 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4045 parser->scope = new_scope;
4051 /* Spot cases that cannot be the beginning of a
4052 nested-name-specifier. On the second and subsequent times
4053 through the loop, we look for the `template' keyword. */
4054 if (success && token->keyword == RID_TEMPLATE)
4056 /* A template-id can start a nested-name-specifier. */
4057 else if (token->type == CPP_TEMPLATE_ID)
4061 /* If the next token is not an identifier, then it is
4062 definitely not a type-name or namespace-name. */
4063 if (token->type != CPP_NAME)
4065 /* If the following token is neither a `<' (to begin a
4066 template-id), nor a `::', then we are not looking at a
4067 nested-name-specifier. */
4068 token = cp_lexer_peek_nth_token (parser->lexer, 2);
4069 if (token->type != CPP_SCOPE
4070 && !cp_parser_nth_token_starts_template_argument_list_p
4075 /* The nested-name-specifier is optional, so we parse
4077 cp_parser_parse_tentatively (parser);
4079 /* Look for the optional `template' keyword, if this isn't the
4080 first time through the loop. */
4082 template_keyword_p = cp_parser_optional_template_keyword (parser);
4084 template_keyword_p = false;
4086 /* Save the old scope since the name lookup we are about to do
4087 might destroy it. */
4088 old_scope = parser->scope;
4089 saved_qualifying_scope = parser->qualifying_scope;
4090 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4091 look up names in "X<T>::I" in order to determine that "Y" is
4092 a template. So, if we have a typename at this point, we make
4093 an effort to look through it. */
4095 && !typename_keyword_p
4097 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4098 parser->scope = resolve_typename_type (parser->scope,
4099 /*only_current_p=*/false);
4100 /* Parse the qualifying entity. */
4102 = cp_parser_qualifying_entity (parser,
4108 /* Look for the `::' token. */
4109 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4111 /* If we found what we wanted, we keep going; otherwise, we're
4113 if (!cp_parser_parse_definitely (parser))
4115 bool error_p = false;
4117 /* Restore the OLD_SCOPE since it was valid before the
4118 failed attempt at finding the last
4119 class-or-namespace-name. */
4120 parser->scope = old_scope;
4121 parser->qualifying_scope = saved_qualifying_scope;
4122 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4124 /* If the next token is an identifier, and the one after
4125 that is a `::', then any valid interpretation would have
4126 found a class-or-namespace-name. */
4127 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4128 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4130 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4133 token = cp_lexer_consume_token (parser->lexer);
4136 if (!token->ambiguous_p)
4139 tree ambiguous_decls;
4141 decl = cp_parser_lookup_name (parser, token->u.value,
4143 /*is_template=*/false,
4144 /*is_namespace=*/false,
4145 /*check_dependency=*/true,
4148 if (TREE_CODE (decl) == TEMPLATE_DECL)
4149 error ("%H%qD used without template parameters",
4150 &token->location, decl);
4151 else if (ambiguous_decls)
4153 error ("%Hreference to %qD is ambiguous",
4154 &token->location, token->u.value);
4155 print_candidates (ambiguous_decls);
4156 decl = error_mark_node;
4160 const char* msg = "is not a class or namespace";
4161 if (cxx_dialect != cxx98)
4162 msg = "is not a class, namespace, or enumeration";
4163 cp_parser_name_lookup_error
4164 (parser, token->u.value, decl, msg,
4168 parser->scope = error_mark_node;
4170 /* Treat this as a successful nested-name-specifier
4175 If the name found is not a class-name (clause
4176 _class_) or namespace-name (_namespace.def_), the
4177 program is ill-formed. */
4180 cp_lexer_consume_token (parser->lexer);
4184 /* We've found one valid nested-name-specifier. */
4186 /* Name lookup always gives us a DECL. */
4187 if (TREE_CODE (new_scope) == TYPE_DECL)
4188 new_scope = TREE_TYPE (new_scope);
4189 /* Uses of "template" must be followed by actual templates. */
4190 if (template_keyword_p
4191 && !(CLASS_TYPE_P (new_scope)
4192 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4193 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4194 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4195 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4196 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4197 == TEMPLATE_ID_EXPR)))
4198 permerror (input_location, TYPE_P (new_scope)
4199 ? "%qT is not a template"
4200 : "%qD is not a template",
4202 /* If it is a class scope, try to complete it; we are about to
4203 be looking up names inside the class. */
4204 if (TYPE_P (new_scope)
4205 /* Since checking types for dependency can be expensive,
4206 avoid doing it if the type is already complete. */
4207 && !COMPLETE_TYPE_P (new_scope)
4208 /* Do not try to complete dependent types. */
4209 && !dependent_type_p (new_scope))
4211 new_scope = complete_type (new_scope);
4212 /* If it is a typedef to current class, use the current
4213 class instead, as the typedef won't have any names inside
4215 if (!COMPLETE_TYPE_P (new_scope)
4216 && currently_open_class (new_scope))
4217 new_scope = TYPE_MAIN_VARIANT (new_scope);
4219 /* Make sure we look in the right scope the next time through
4221 parser->scope = new_scope;
4224 /* If parsing tentatively, replace the sequence of tokens that makes
4225 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4226 token. That way, should we re-parse the token stream, we will
4227 not have to repeat the effort required to do the parse, nor will
4228 we issue duplicate error messages. */
4229 if (success && start)
4233 token = cp_lexer_token_at (parser->lexer, start);
4234 /* Reset the contents of the START token. */
4235 token->type = CPP_NESTED_NAME_SPECIFIER;
4236 /* Retrieve any deferred checks. Do not pop this access checks yet
4237 so the memory will not be reclaimed during token replacing below. */
4238 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4239 token->u.tree_check_value->value = parser->scope;
4240 token->u.tree_check_value->checks = get_deferred_access_checks ();
4241 token->u.tree_check_value->qualifying_scope =
4242 parser->qualifying_scope;
4243 token->keyword = RID_MAX;
4245 /* Purge all subsequent tokens. */
4246 cp_lexer_purge_tokens_after (parser->lexer, start);
4250 pop_to_parent_deferring_access_checks ();
4252 return success ? parser->scope : NULL_TREE;
4255 /* Parse a nested-name-specifier. See
4256 cp_parser_nested_name_specifier_opt for details. This function
4257 behaves identically, except that it will an issue an error if no
4258 nested-name-specifier is present. */
4261 cp_parser_nested_name_specifier (cp_parser *parser,
4262 bool typename_keyword_p,
4263 bool check_dependency_p,
4265 bool is_declaration)
4269 /* Look for the nested-name-specifier. */
4270 scope = cp_parser_nested_name_specifier_opt (parser,
4275 /* If it was not present, issue an error message. */
4278 cp_parser_error (parser, "expected nested-name-specifier");
4279 parser->scope = NULL_TREE;
4285 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4286 this is either a class-name or a namespace-name (which corresponds
4287 to the class-or-namespace-name production in the grammar). For
4288 C++0x, it can also be a type-name that refers to an enumeration
4291 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4292 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4293 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4294 TYPE_P is TRUE iff the next name should be taken as a class-name,
4295 even the same name is declared to be another entity in the same
4298 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4299 specified by the class-or-namespace-name. If neither is found the
4300 ERROR_MARK_NODE is returned. */
4303 cp_parser_qualifying_entity (cp_parser *parser,
4304 bool typename_keyword_p,
4305 bool template_keyword_p,
4306 bool check_dependency_p,
4308 bool is_declaration)
4311 tree saved_qualifying_scope;
4312 tree saved_object_scope;
4315 bool successful_parse_p;
4317 /* Before we try to parse the class-name, we must save away the
4318 current PARSER->SCOPE since cp_parser_class_name will destroy
4320 saved_scope = parser->scope;
4321 saved_qualifying_scope = parser->qualifying_scope;
4322 saved_object_scope = parser->object_scope;
4323 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4324 there is no need to look for a namespace-name. */
4325 only_class_p = template_keyword_p
4326 || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4328 cp_parser_parse_tentatively (parser);
4329 scope = cp_parser_class_name (parser,
4332 type_p ? class_type : none_type,
4334 /*class_head_p=*/false,
4336 successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4337 /* If that didn't work and we're in C++0x mode, try for a type-name. */
4339 && cxx_dialect != cxx98
4340 && !successful_parse_p)
4342 /* Restore the saved scope. */
4343 parser->scope = saved_scope;
4344 parser->qualifying_scope = saved_qualifying_scope;
4345 parser->object_scope = saved_object_scope;
4347 /* Parse tentatively. */
4348 cp_parser_parse_tentatively (parser);
4350 /* Parse a typedef-name or enum-name. */
4351 scope = cp_parser_nonclass_name (parser);
4352 successful_parse_p = cp_parser_parse_definitely (parser);
4354 /* If that didn't work, try for a namespace-name. */
4355 if (!only_class_p && !successful_parse_p)
4357 /* Restore the saved scope. */
4358 parser->scope = saved_scope;
4359 parser->qualifying_scope = saved_qualifying_scope;
4360 parser->object_scope = saved_object_scope;
4361 /* If we are not looking at an identifier followed by the scope
4362 resolution operator, then this is not part of a
4363 nested-name-specifier. (Note that this function is only used
4364 to parse the components of a nested-name-specifier.) */
4365 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4366 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4367 return error_mark_node;
4368 scope = cp_parser_namespace_name (parser);
4374 /* Parse a postfix-expression.
4378 postfix-expression [ expression ]
4379 postfix-expression ( expression-list [opt] )
4380 simple-type-specifier ( expression-list [opt] )
4381 typename :: [opt] nested-name-specifier identifier
4382 ( expression-list [opt] )
4383 typename :: [opt] nested-name-specifier template [opt] template-id
4384 ( expression-list [opt] )
4385 postfix-expression . template [opt] id-expression
4386 postfix-expression -> template [opt] id-expression
4387 postfix-expression . pseudo-destructor-name
4388 postfix-expression -> pseudo-destructor-name
4389 postfix-expression ++
4390 postfix-expression --
4391 dynamic_cast < type-id > ( expression )
4392 static_cast < type-id > ( expression )
4393 reinterpret_cast < type-id > ( expression )
4394 const_cast < type-id > ( expression )
4395 typeid ( expression )
4401 ( type-id ) { initializer-list , [opt] }
4403 This extension is a GNU version of the C99 compound-literal
4404 construct. (The C99 grammar uses `type-name' instead of `type-id',
4405 but they are essentially the same concept.)
4407 If ADDRESS_P is true, the postfix expression is the operand of the
4408 `&' operator. CAST_P is true if this expression is the target of a
4411 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4412 class member access expressions [expr.ref].
4414 Returns a representation of the expression. */
4417 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4418 bool member_access_only_p,
4419 cp_id_kind * pidk_return)
4423 cp_id_kind idk = CP_ID_KIND_NONE;
4424 tree postfix_expression = NULL_TREE;
4425 bool is_member_access = false;
4427 /* Peek at the next token. */
4428 token = cp_lexer_peek_token (parser->lexer);
4429 /* Some of the productions are determined by keywords. */
4430 keyword = token->keyword;
4440 const char *saved_message;
4442 /* All of these can be handled in the same way from the point
4443 of view of parsing. Begin by consuming the token
4444 identifying the cast. */
4445 cp_lexer_consume_token (parser->lexer);
4447 /* New types cannot be defined in the cast. */
4448 saved_message = parser->type_definition_forbidden_message;
4449 parser->type_definition_forbidden_message
4450 = "types may not be defined in casts";
4452 /* Look for the opening `<'. */
4453 cp_parser_require (parser, CPP_LESS, "%<<%>");
4454 /* Parse the type to which we are casting. */
4455 type = cp_parser_type_id (parser);
4456 /* Look for the closing `>'. */
4457 cp_parser_require (parser, CPP_GREATER, "%<>%>");
4458 /* Restore the old message. */
4459 parser->type_definition_forbidden_message = saved_message;
4461 /* And the expression which is being cast. */
4462 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4463 expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4464 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4466 /* Only type conversions to integral or enumeration types
4467 can be used in constant-expressions. */
4468 if (!cast_valid_in_integral_constant_expression_p (type)
4469 && (cp_parser_non_integral_constant_expression
4471 "a cast to a type other than an integral or "
4472 "enumeration type")))
4473 return error_mark_node;
4479 = build_dynamic_cast (type, expression, tf_warning_or_error);
4483 = build_static_cast (type, expression, tf_warning_or_error);
4487 = build_reinterpret_cast (type, expression,
4488 tf_warning_or_error);
4492 = build_const_cast (type, expression, tf_warning_or_error);
4503 const char *saved_message;
4504 bool saved_in_type_id_in_expr_p;
4506 /* Consume the `typeid' token. */
4507 cp_lexer_consume_token (parser->lexer);
4508 /* Look for the `(' token. */
4509 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4510 /* Types cannot be defined in a `typeid' expression. */
4511 saved_message = parser->type_definition_forbidden_message;
4512 parser->type_definition_forbidden_message
4513 = "types may not be defined in a %<typeid%> expression";
4514 /* We can't be sure yet whether we're looking at a type-id or an
4516 cp_parser_parse_tentatively (parser);
4517 /* Try a type-id first. */
4518 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4519 parser->in_type_id_in_expr_p = true;
4520 type = cp_parser_type_id (parser);
4521 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4522 /* Look for the `)' token. Otherwise, we can't be sure that
4523 we're not looking at an expression: consider `typeid (int
4524 (3))', for example. */
4525 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4526 /* If all went well, simply lookup the type-id. */
4527 if (cp_parser_parse_definitely (parser))
4528 postfix_expression = get_typeid (type);
4529 /* Otherwise, fall back to the expression variant. */
4534 /* Look for an expression. */
4535 expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4536 /* Compute its typeid. */
4537 postfix_expression = build_typeid (expression);
4538 /* Look for the `)' token. */
4539 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4541 /* Restore the saved message. */
4542 parser->type_definition_forbidden_message = saved_message;
4543 /* `typeid' may not appear in an integral constant expression. */
4544 if (cp_parser_non_integral_constant_expression(parser,
4545 "%<typeid%> operator"))
4546 return error_mark_node;
4553 /* The syntax permitted here is the same permitted for an
4554 elaborated-type-specifier. */
4555 type = cp_parser_elaborated_type_specifier (parser,
4556 /*is_friend=*/false,
4557 /*is_declaration=*/false);
4558 postfix_expression = cp_parser_functional_cast (parser, type);
4566 /* If the next thing is a simple-type-specifier, we may be
4567 looking at a functional cast. We could also be looking at
4568 an id-expression. So, we try the functional cast, and if
4569 that doesn't work we fall back to the primary-expression. */
4570 cp_parser_parse_tentatively (parser);
4571 /* Look for the simple-type-specifier. */
4572 type = cp_parser_simple_type_specifier (parser,
4573 /*decl_specs=*/NULL,
4574 CP_PARSER_FLAGS_NONE);
4575 /* Parse the cast itself. */
4576 if (!cp_parser_error_occurred (parser))
4578 = cp_parser_functional_cast (parser, type);
4579 /* If that worked, we're done. */
4580 if (cp_parser_parse_definitely (parser))
4583 /* If the functional-cast didn't work out, try a
4584 compound-literal. */
4585 if (cp_parser_allow_gnu_extensions_p (parser)
4586 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4588 VEC(constructor_elt,gc) *initializer_list = NULL;
4589 bool saved_in_type_id_in_expr_p;
4591 cp_parser_parse_tentatively (parser);
4592 /* Consume the `('. */
4593 cp_lexer_consume_token (parser->lexer);
4594 /* Parse the type. */
4595 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4596 parser->in_type_id_in_expr_p = true;
4597 type = cp_parser_type_id (parser);
4598 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4599 /* Look for the `)'. */
4600 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4601 /* Look for the `{'. */
4602 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4603 /* If things aren't going well, there's no need to
4605 if (!cp_parser_error_occurred (parser))
4607 bool non_constant_p;
4608 /* Parse the initializer-list. */
4610 = cp_parser_initializer_list (parser, &non_constant_p);
4611 /* Allow a trailing `,'. */
4612 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4613 cp_lexer_consume_token (parser->lexer);
4614 /* Look for the final `}'. */
4615 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4617 /* If that worked, we're definitely looking at a
4618 compound-literal expression. */
4619 if (cp_parser_parse_definitely (parser))
4621 /* Warn the user that a compound literal is not
4622 allowed in standard C++. */
4623 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4624 /* For simplicity, we disallow compound literals in
4625 constant-expressions. We could
4626 allow compound literals of integer type, whose
4627 initializer was a constant, in constant
4628 expressions. Permitting that usage, as a further
4629 extension, would not change the meaning of any
4630 currently accepted programs. (Of course, as
4631 compound literals are not part of ISO C++, the
4632 standard has nothing to say.) */
4633 if (cp_parser_non_integral_constant_expression
4634 (parser, "non-constant compound literals"))
4636 postfix_expression = error_mark_node;
4639 /* Form the representation of the compound-literal. */
4641 = (finish_compound_literal
4642 (type, build_constructor (init_list_type_node,
4643 initializer_list)));
4648 /* It must be a primary-expression. */
4650 = cp_parser_primary_expression (parser, address_p, cast_p,
4651 /*template_arg_p=*/false,
4657 /* Keep looping until the postfix-expression is complete. */
4660 if (idk == CP_ID_KIND_UNQUALIFIED
4661 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4662 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4663 /* It is not a Koenig lookup function call. */
4665 = unqualified_name_lookup_error (postfix_expression);
4667 /* Peek at the next token. */
4668 token = cp_lexer_peek_token (parser->lexer);
4670 switch (token->type)
4672 case CPP_OPEN_SQUARE:
4674 = cp_parser_postfix_open_square_expression (parser,
4677 idk = CP_ID_KIND_NONE;
4678 is_member_access = false;
4681 case CPP_OPEN_PAREN:
4682 /* postfix-expression ( expression-list [opt] ) */
4685 bool is_builtin_constant_p;
4686 bool saved_integral_constant_expression_p = false;
4687 bool saved_non_integral_constant_expression_p = false;
4690 is_member_access = false;
4692 is_builtin_constant_p
4693 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4694 if (is_builtin_constant_p)
4696 /* The whole point of __builtin_constant_p is to allow
4697 non-constant expressions to appear as arguments. */
4698 saved_integral_constant_expression_p
4699 = parser->integral_constant_expression_p;
4700 saved_non_integral_constant_expression_p
4701 = parser->non_integral_constant_expression_p;
4702 parser->integral_constant_expression_p = false;
4704 args = (cp_parser_parenthesized_expression_list
4705 (parser, /*is_attribute_list=*/false,
4706 /*cast_p=*/false, /*allow_expansion_p=*/true,
4707 /*non_constant_p=*/NULL));
4708 if (is_builtin_constant_p)
4710 parser->integral_constant_expression_p
4711 = saved_integral_constant_expression_p;
4712 parser->non_integral_constant_expression_p
4713 = saved_non_integral_constant_expression_p;
4718 postfix_expression = error_mark_node;
4722 /* Function calls are not permitted in
4723 constant-expressions. */
4724 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4725 && cp_parser_non_integral_constant_expression (parser,
4728 postfix_expression = error_mark_node;
4729 release_tree_vector (args);
4734 if (idk == CP_ID_KIND_UNQUALIFIED
4735 || idk == CP_ID_KIND_TEMPLATE_ID)
4737 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4739 if (!VEC_empty (tree, args))
4742 if (!any_type_dependent_arguments_p (args))
4744 = perform_koenig_lookup (postfix_expression, args);
4748 = unqualified_fn_lookup_error (postfix_expression);
4750 /* We do not perform argument-dependent lookup if
4751 normal lookup finds a non-function, in accordance
4752 with the expected resolution of DR 218. */
4753 else if (!VEC_empty (tree, args)
4754 && is_overloaded_fn (postfix_expression))
4756 tree fn = get_first_fn (postfix_expression);
4758 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4759 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4761 /* Only do argument dependent lookup if regular
4762 lookup does not find a set of member functions.
4763 [basic.lookup.koenig]/2a */
4764 if (!DECL_FUNCTION_MEMBER_P (fn))
4767 if (!any_type_dependent_arguments_p (args))
4769 = perform_koenig_lookup (postfix_expression, args);
4774 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4776 tree instance = TREE_OPERAND (postfix_expression, 0);
4777 tree fn = TREE_OPERAND (postfix_expression, 1);
4779 if (processing_template_decl
4780 && (type_dependent_expression_p (instance)
4781 || (!BASELINK_P (fn)
4782 && TREE_CODE (fn) != FIELD_DECL)
4783 || type_dependent_expression_p (fn)
4784 || any_type_dependent_arguments_p (args)))
4787 = build_nt_call_vec (postfix_expression, args);
4788 release_tree_vector (args);
4792 if (BASELINK_P (fn))
4795 = (build_new_method_call
4796 (instance, fn, &args, NULL_TREE,
4797 (idk == CP_ID_KIND_QUALIFIED
4798 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4800 tf_warning_or_error));
4804 = finish_call_expr (postfix_expression, &args,
4805 /*disallow_virtual=*/false,
4807 tf_warning_or_error);
4809 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4810 || TREE_CODE (postfix_expression) == MEMBER_REF
4811 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4812 postfix_expression = (build_offset_ref_call_from_tree
4813 (postfix_expression, &args));
4814 else if (idk == CP_ID_KIND_QUALIFIED)
4815 /* A call to a static class member, or a namespace-scope
4818 = finish_call_expr (postfix_expression, &args,
4819 /*disallow_virtual=*/true,
4821 tf_warning_or_error);
4823 /* All other function calls. */
4825 = finish_call_expr (postfix_expression, &args,
4826 /*disallow_virtual=*/false,
4828 tf_warning_or_error);
4830 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4831 idk = CP_ID_KIND_NONE;
4833 release_tree_vector (args);
4839 /* postfix-expression . template [opt] id-expression
4840 postfix-expression . pseudo-destructor-name
4841 postfix-expression -> template [opt] id-expression
4842 postfix-expression -> pseudo-destructor-name */
4844 /* Consume the `.' or `->' operator. */
4845 cp_lexer_consume_token (parser->lexer);
4848 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4853 is_member_access = true;
4857 /* postfix-expression ++ */
4858 /* Consume the `++' token. */
4859 cp_lexer_consume_token (parser->lexer);
4860 /* Generate a representation for the complete expression. */
4862 = finish_increment_expr (postfix_expression,
4863 POSTINCREMENT_EXPR);
4864 /* Increments may not appear in constant-expressions. */
4865 if (cp_parser_non_integral_constant_expression (parser,
4867 postfix_expression = error_mark_node;
4868 idk = CP_ID_KIND_NONE;
4869 is_member_access = false;
4872 case CPP_MINUS_MINUS:
4873 /* postfix-expression -- */
4874 /* Consume the `--' token. */
4875 cp_lexer_consume_token (parser->lexer);
4876 /* Generate a representation for the complete expression. */
4878 = finish_increment_expr (postfix_expression,
4879 POSTDECREMENT_EXPR);
4880 /* Decrements may not appear in constant-expressions. */
4881 if (cp_parser_non_integral_constant_expression (parser,
4883 postfix_expression = error_mark_node;
4884 idk = CP_ID_KIND_NONE;
4885 is_member_access = false;
4889 if (pidk_return != NULL)
4890 * pidk_return = idk;
4891 if (member_access_only_p)
4892 return is_member_access? postfix_expression : error_mark_node;
4894 return postfix_expression;
4898 /* We should never get here. */
4900 return error_mark_node;
4903 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4904 by cp_parser_builtin_offsetof. We're looking for
4906 postfix-expression [ expression ]
4908 FOR_OFFSETOF is set if we're being called in that context, which
4909 changes how we deal with integer constant expressions. */
4912 cp_parser_postfix_open_square_expression (cp_parser *parser,
4913 tree postfix_expression,
4918 /* Consume the `[' token. */
4919 cp_lexer_consume_token (parser->lexer);
4921 /* Parse the index expression. */
4922 /* ??? For offsetof, there is a question of what to allow here. If
4923 offsetof is not being used in an integral constant expression context,
4924 then we *could* get the right answer by computing the value at runtime.
4925 If we are in an integral constant expression context, then we might
4926 could accept any constant expression; hard to say without analysis.
4927 Rather than open the barn door too wide right away, allow only integer
4928 constant expressions here. */
4930 index = cp_parser_constant_expression (parser, false, NULL);
4932 index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4934 /* Look for the closing `]'. */
4935 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4937 /* Build the ARRAY_REF. */
4938 postfix_expression = grok_array_decl (postfix_expression, index);
4940 /* When not doing offsetof, array references are not permitted in
4941 constant-expressions. */
4943 && (cp_parser_non_integral_constant_expression
4944 (parser, "an array reference")))
4945 postfix_expression = error_mark_node;
4947 return postfix_expression;
4950 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4951 by cp_parser_builtin_offsetof. We're looking for
4953 postfix-expression . template [opt] id-expression
4954 postfix-expression . pseudo-destructor-name
4955 postfix-expression -> template [opt] id-expression
4956 postfix-expression -> pseudo-destructor-name
4958 FOR_OFFSETOF is set if we're being called in that context. That sorta
4959 limits what of the above we'll actually accept, but nevermind.
4960 TOKEN_TYPE is the "." or "->" token, which will already have been
4961 removed from the stream. */
4964 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4965 enum cpp_ttype token_type,
4966 tree postfix_expression,
4967 bool for_offsetof, cp_id_kind *idk,
4968 location_t location)
4972 bool pseudo_destructor_p;
4973 tree scope = NULL_TREE;
4975 /* If this is a `->' operator, dereference the pointer. */
4976 if (token_type == CPP_DEREF)
4977 postfix_expression = build_x_arrow (postfix_expression);
4978 /* Check to see whether or not the expression is type-dependent. */
4979 dependent_p = type_dependent_expression_p (postfix_expression);
4980 /* The identifier following the `->' or `.' is not qualified. */
4981 parser->scope = NULL_TREE;
4982 parser->qualifying_scope = NULL_TREE;
4983 parser->object_scope = NULL_TREE;
4984 *idk = CP_ID_KIND_NONE;
4986 /* Enter the scope corresponding to the type of the object
4987 given by the POSTFIX_EXPRESSION. */
4988 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4990 scope = TREE_TYPE (postfix_expression);
4991 /* According to the standard, no expression should ever have
4992 reference type. Unfortunately, we do not currently match
4993 the standard in this respect in that our internal representation
4994 of an expression may have reference type even when the standard
4995 says it does not. Therefore, we have to manually obtain the
4996 underlying type here. */
4997 scope = non_reference (scope);
4998 /* The type of the POSTFIX_EXPRESSION must be complete. */
4999 if (scope == unknown_type_node)
5001 error ("%H%qE does not have class type", &location, postfix_expression);
5005 scope = complete_type_or_else (scope, NULL_TREE);
5006 /* Let the name lookup machinery know that we are processing a
5007 class member access expression. */
5008 parser->context->object_type = scope;
5009 /* If something went wrong, we want to be able to discern that case,
5010 as opposed to the case where there was no SCOPE due to the type
5011 of expression being dependent. */
5013 scope = error_mark_node;
5014 /* If the SCOPE was erroneous, make the various semantic analysis
5015 functions exit quickly -- and without issuing additional error
5017 if (scope == error_mark_node)
5018 postfix_expression = error_mark_node;
5021 /* Assume this expression is not a pseudo-destructor access. */
5022 pseudo_destructor_p = false;
5024 /* If the SCOPE is a scalar type, then, if this is a valid program,
5025 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
5026 is type dependent, it can be pseudo-destructor-name or something else.
5027 Try to parse it as pseudo-destructor-name first. */
5028 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5033 cp_parser_parse_tentatively (parser);
5034 /* Parse the pseudo-destructor-name. */
5036 cp_parser_pseudo_destructor_name (parser, &s, &type);
5038 && (cp_parser_error_occurred (parser)
5039 || TREE_CODE (type) != TYPE_DECL
5040 || !SCALAR_TYPE_P (TREE_TYPE (type))))
5041 cp_parser_abort_tentative_parse (parser);
5042 else if (cp_parser_parse_definitely (parser))
5044 pseudo_destructor_p = true;
5046 = finish_pseudo_destructor_expr (postfix_expression,
5047 s, TREE_TYPE (type));
5051 if (!pseudo_destructor_p)
5053 /* If the SCOPE is not a scalar type, we are looking at an
5054 ordinary class member access expression, rather than a
5055 pseudo-destructor-name. */
5057 cp_token *token = cp_lexer_peek_token (parser->lexer);
5058 /* Parse the id-expression. */
5059 name = (cp_parser_id_expression
5061 cp_parser_optional_template_keyword (parser),
5062 /*check_dependency_p=*/true,
5064 /*declarator_p=*/false,
5065 /*optional_p=*/false));
5066 /* In general, build a SCOPE_REF if the member name is qualified.
5067 However, if the name was not dependent and has already been
5068 resolved; there is no need to build the SCOPE_REF. For example;
5070 struct X { void f(); };
5071 template <typename T> void f(T* t) { t->X::f(); }
5073 Even though "t" is dependent, "X::f" is not and has been resolved
5074 to a BASELINK; there is no need to include scope information. */
5076 /* But we do need to remember that there was an explicit scope for
5077 virtual function calls. */
5079 *idk = CP_ID_KIND_QUALIFIED;
5081 /* If the name is a template-id that names a type, we will get a
5082 TYPE_DECL here. That is invalid code. */
5083 if (TREE_CODE (name) == TYPE_DECL)
5085 error ("%Hinvalid use of %qD", &token->location, name);
5086 postfix_expression = error_mark_node;
5090 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5092 name = build_qualified_name (/*type=*/NULL_TREE,
5096 parser->scope = NULL_TREE;
5097 parser->qualifying_scope = NULL_TREE;
5098 parser->object_scope = NULL_TREE;
5100 if (scope && name && BASELINK_P (name))
5101 adjust_result_of_qualified_name_lookup
5102 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5104 = finish_class_member_access_expr (postfix_expression, name,
5106 tf_warning_or_error);
5110 /* We no longer need to look up names in the scope of the object on
5111 the left-hand side of the `.' or `->' operator. */
5112 parser->context->object_type = NULL_TREE;
5114 /* Outside of offsetof, these operators may not appear in
5115 constant-expressions. */
5117 && (cp_parser_non_integral_constant_expression
5118 (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5119 postfix_expression = error_mark_node;
5121 return postfix_expression;
5124 /* Parse a parenthesized expression-list.
5127 assignment-expression
5128 expression-list, assignment-expression
5133 identifier, expression-list
5135 CAST_P is true if this expression is the target of a cast.
5137 ALLOW_EXPANSION_P is true if this expression allows expansion of an
5140 Returns a vector of trees. Each element is a representation of an
5141 assignment-expression. NULL is returned if the ( and or ) are
5142 missing. An empty, but allocated, vector is returned on no
5143 expressions. The parentheses are eaten. IS_ATTRIBUTE_LIST is true
5144 if this is really an attribute list being parsed. If
5145 NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5146 not all of the expressions in the list were constant. */
5148 static VEC(tree,gc) *
5149 cp_parser_parenthesized_expression_list (cp_parser* parser,
5150 bool is_attribute_list,
5152 bool allow_expansion_p,
5153 bool *non_constant_p)
5155 VEC(tree,gc) *expression_list;
5156 bool fold_expr_p = is_attribute_list;
5157 tree identifier = NULL_TREE;
5158 bool saved_greater_than_is_operator_p;
5160 /* Assume all the expressions will be constant. */
5162 *non_constant_p = false;
5164 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5167 expression_list = make_tree_vector ();
5169 /* Within a parenthesized expression, a `>' token is always
5170 the greater-than operator. */
5171 saved_greater_than_is_operator_p
5172 = parser->greater_than_is_operator_p;
5173 parser->greater_than_is_operator_p = true;
5175 /* Consume expressions until there are no more. */
5176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5181 /* At the beginning of attribute lists, check to see if the
5182 next token is an identifier. */
5183 if (is_attribute_list
5184 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5188 /* Consume the identifier. */
5189 token = cp_lexer_consume_token (parser->lexer);
5190 /* Save the identifier. */
5191 identifier = token->u.value;
5195 bool expr_non_constant_p;
5197 /* Parse the next assignment-expression. */
5198 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5200 /* A braced-init-list. */
5201 maybe_warn_cpp0x ("extended initializer lists");
5202 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5203 if (non_constant_p && expr_non_constant_p)
5204 *non_constant_p = true;
5206 else if (non_constant_p)
5208 expr = (cp_parser_constant_expression
5209 (parser, /*allow_non_constant_p=*/true,
5210 &expr_non_constant_p));
5211 if (expr_non_constant_p)
5212 *non_constant_p = true;
5215 expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5218 expr = fold_non_dependent_expr (expr);
5220 /* If we have an ellipsis, then this is an expression
5222 if (allow_expansion_p
5223 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5225 /* Consume the `...'. */
5226 cp_lexer_consume_token (parser->lexer);
5228 /* Build the argument pack. */
5229 expr = make_pack_expansion (expr);
5232 /* Add it to the list. We add error_mark_node
5233 expressions to the list, so that we can still tell if
5234 the correct form for a parenthesized expression-list
5235 is found. That gives better errors. */
5236 VEC_safe_push (tree, gc, expression_list, expr);
5238 if (expr == error_mark_node)
5242 /* After the first item, attribute lists look the same as
5243 expression lists. */
5244 is_attribute_list = false;
5247 /* If the next token isn't a `,', then we are done. */
5248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5251 /* Otherwise, consume the `,' and keep going. */
5252 cp_lexer_consume_token (parser->lexer);
5255 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5260 /* We try and resync to an unnested comma, as that will give the
5261 user better diagnostics. */
5262 ending = cp_parser_skip_to_closing_parenthesis (parser,
5263 /*recovering=*/true,
5265 /*consume_paren=*/true);
5270 parser->greater_than_is_operator_p
5271 = saved_greater_than_is_operator_p;
5276 parser->greater_than_is_operator_p
5277 = saved_greater_than_is_operator_p;
5280 VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5282 return expression_list;
5285 /* Parse a pseudo-destructor-name.
5287 pseudo-destructor-name:
5288 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5289 :: [opt] nested-name-specifier template template-id :: ~ type-name
5290 :: [opt] nested-name-specifier [opt] ~ type-name
5292 If either of the first two productions is used, sets *SCOPE to the
5293 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5294 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5295 or ERROR_MARK_NODE if the parse fails. */
5298 cp_parser_pseudo_destructor_name (cp_parser* parser,
5302 bool nested_name_specifier_p;
5304 /* Assume that things will not work out. */
5305 *type = error_mark_node;
5307 /* Look for the optional `::' operator. */
5308 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5309 /* Look for the optional nested-name-specifier. */
5310 nested_name_specifier_p
5311 = (cp_parser_nested_name_specifier_opt (parser,
5312 /*typename_keyword_p=*/false,
5313 /*check_dependency_p=*/true,
5315 /*is_declaration=*/false)
5317 /* Now, if we saw a nested-name-specifier, we might be doing the
5318 second production. */
5319 if (nested_name_specifier_p
5320 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5322 /* Consume the `template' keyword. */
5323 cp_lexer_consume_token (parser->lexer);
5324 /* Parse the template-id. */
5325 cp_parser_template_id (parser,
5326 /*template_keyword_p=*/true,
5327 /*check_dependency_p=*/false,
5328 /*is_declaration=*/true);
5329 /* Look for the `::' token. */
5330 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5332 /* If the next token is not a `~', then there might be some
5333 additional qualification. */
5334 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5336 /* At this point, we're looking for "type-name :: ~". The type-name
5337 must not be a class-name, since this is a pseudo-destructor. So,
5338 it must be either an enum-name, or a typedef-name -- both of which
5339 are just identifiers. So, we peek ahead to check that the "::"
5340 and "~" tokens are present; if they are not, then we can avoid
5341 calling type_name. */
5342 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5343 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5344 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5346 cp_parser_error (parser, "non-scalar type");
5350 /* Look for the type-name. */
5351 *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5352 if (*scope == error_mark_node)
5355 /* Look for the `::' token. */
5356 cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5361 /* Look for the `~'. */
5362 cp_parser_require (parser, CPP_COMPL, "%<~%>");
5363 /* Look for the type-name again. We are not responsible for
5364 checking that it matches the first type-name. */
5365 *type = cp_parser_nonclass_name (parser);
5368 /* Parse a unary-expression.
5374 unary-operator cast-expression
5375 sizeof unary-expression
5383 __extension__ cast-expression
5384 __alignof__ unary-expression
5385 __alignof__ ( type-id )
5386 __real__ cast-expression
5387 __imag__ cast-expression
5390 ADDRESS_P is true iff the unary-expression is appearing as the
5391 operand of the `&' operator. CAST_P is true if this expression is
5392 the target of a cast.
5394 Returns a representation of the expression. */
5397 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5401 enum tree_code unary_operator;
5403 /* Peek at the next token. */
5404 token = cp_lexer_peek_token (parser->lexer);
5405 /* Some keywords give away the kind of expression. */
5406 if (token->type == CPP_KEYWORD)
5408 enum rid keyword = token->keyword;
5418 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5419 /* Consume the token. */
5420 cp_lexer_consume_token (parser->lexer);
5421 /* Parse the operand. */
5422 operand = cp_parser_sizeof_operand (parser, keyword);
5424 if (TYPE_P (operand))
5425 return cxx_sizeof_or_alignof_type (operand, op, true);
5427 return cxx_sizeof_or_alignof_expr (operand, op, true);
5431 return cp_parser_new_expression (parser);
5434 return cp_parser_delete_expression (parser);
5438 /* The saved value of the PEDANTIC flag. */
5442 /* Save away the PEDANTIC flag. */
5443 cp_parser_extension_opt (parser, &saved_pedantic);
5444 /* Parse the cast-expression. */
5445 expr = cp_parser_simple_cast_expression (parser);
5446 /* Restore the PEDANTIC flag. */
5447 pedantic = saved_pedantic;
5457 /* Consume the `__real__' or `__imag__' token. */
5458 cp_lexer_consume_token (parser->lexer);
5459 /* Parse the cast-expression. */
5460 expression = cp_parser_simple_cast_expression (parser);
5461 /* Create the complete representation. */
5462 return build_x_unary_op ((keyword == RID_REALPART
5463 ? REALPART_EXPR : IMAGPART_EXPR),
5465 tf_warning_or_error);
5474 /* Look for the `:: new' and `:: delete', which also signal the
5475 beginning of a new-expression, or delete-expression,
5476 respectively. If the next token is `::', then it might be one of
5478 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5482 /* See if the token after the `::' is one of the keywords in
5483 which we're interested. */
5484 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5485 /* If it's `new', we have a new-expression. */
5486 if (keyword == RID_NEW)
5487 return cp_parser_new_expression (parser);
5488 /* Similarly, for `delete'. */
5489 else if (keyword == RID_DELETE)
5490 return cp_parser_delete_expression (parser);
5493 /* Look for a unary operator. */
5494 unary_operator = cp_parser_unary_operator (token);
5495 /* The `++' and `--' operators can be handled similarly, even though
5496 they are not technically unary-operators in the grammar. */
5497 if (unary_operator == ERROR_MARK)
5499 if (token->type == CPP_PLUS_PLUS)
5500 unary_operator = PREINCREMENT_EXPR;
5501 else if (token->type == CPP_MINUS_MINUS)
5502 unary_operator = PREDECREMENT_EXPR;
5503 /* Handle the GNU address-of-label extension. */
5504 else if (cp_parser_allow_gnu_extensions_p (parser)
5505 && token->type == CPP_AND_AND)
5509 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5511 /* Consume the '&&' token. */
5512 cp_lexer_consume_token (parser->lexer);
5513 /* Look for the identifier. */
5514 identifier = cp_parser_identifier (parser);
5515 /* Create an expression representing the address. */
5516 expression = finish_label_address_expr (identifier, loc);
5517 if (cp_parser_non_integral_constant_expression (parser,
5518 "the address of a label"))
5519 expression = error_mark_node;
5523 if (unary_operator != ERROR_MARK)
5525 tree cast_expression;
5526 tree expression = error_mark_node;
5527 const char *non_constant_p = NULL;
5529 /* Consume the operator token. */
5530 token = cp_lexer_consume_token (parser->lexer);
5531 /* Parse the cast-expression. */
5533 = cp_parser_cast_expression (parser,
5534 unary_operator == ADDR_EXPR,
5535 /*cast_p=*/false, pidk);
5536 /* Now, build an appropriate representation. */
5537 switch (unary_operator)
5540 non_constant_p = "%<*%>";
5541 expression = build_x_indirect_ref (cast_expression, "unary *",
5542 tf_warning_or_error);
5546 non_constant_p = "%<&%>";
5549 expression = build_x_unary_op (unary_operator, cast_expression,
5550 tf_warning_or_error);
5553 case PREINCREMENT_EXPR:
5554 case PREDECREMENT_EXPR:
5555 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5556 ? "%<++%>" : "%<--%>");
5558 case UNARY_PLUS_EXPR:
5560 case TRUTH_NOT_EXPR:
5561 expression = finish_unary_op_expr (unary_operator, cast_expression);
5569 && cp_parser_non_integral_constant_expression (parser,
5571 expression = error_mark_node;
5576 return cp_parser_postfix_expression (parser, address_p, cast_p,
5577 /*member_access_only_p=*/false,
5581 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5582 unary-operator, the corresponding tree code is returned. */
5584 static enum tree_code
5585 cp_parser_unary_operator (cp_token* token)
5587 switch (token->type)
5590 return INDIRECT_REF;
5596 return UNARY_PLUS_EXPR;
5602 return TRUTH_NOT_EXPR;
5605 return BIT_NOT_EXPR;
5612 /* Parse a new-expression.
5615 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5616 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5618 Returns a representation of the expression. */
5621 cp_parser_new_expression (cp_parser* parser)
5623 bool global_scope_p;
5624 VEC(tree,gc) *placement;
5626 VEC(tree,gc) *initializer;
5630 /* Look for the optional `::' operator. */
5632 = (cp_parser_global_scope_opt (parser,
5633 /*current_scope_valid_p=*/false)
5635 /* Look for the `new' operator. */
5636 cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5637 /* There's no easy way to tell a new-placement from the
5638 `( type-id )' construct. */
5639 cp_parser_parse_tentatively (parser);
5640 /* Look for a new-placement. */
5641 placement = cp_parser_new_placement (parser);
5642 /* If that didn't work out, there's no new-placement. */
5643 if (!cp_parser_parse_definitely (parser))
5645 if (placement != NULL)
5646 release_tree_vector (placement);
5650 /* If the next token is a `(', then we have a parenthesized
5652 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5655 /* Consume the `('. */
5656 cp_lexer_consume_token (parser->lexer);
5657 /* Parse the type-id. */
5658 type = cp_parser_type_id (parser);
5659 /* Look for the closing `)'. */
5660 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5661 token = cp_lexer_peek_token (parser->lexer);
5662 /* There should not be a direct-new-declarator in this production,
5663 but GCC used to allowed this, so we check and emit a sensible error
5664 message for this case. */
5665 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5667 error ("%Harray bound forbidden after parenthesized type-id",
5669 inform (token->location,
5670 "try removing the parentheses around the type-id");
5671 cp_parser_direct_new_declarator (parser);
5675 /* Otherwise, there must be a new-type-id. */
5677 type = cp_parser_new_type_id (parser, &nelts);
5679 /* If the next token is a `(' or '{', then we have a new-initializer. */
5680 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5681 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5682 initializer = cp_parser_new_initializer (parser);
5686 /* A new-expression may not appear in an integral constant
5688 if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5689 ret = error_mark_node;
5692 /* Create a representation of the new-expression. */
5693 ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5694 tf_warning_or_error);
5697 if (placement != NULL)
5698 release_tree_vector (placement);
5699 if (initializer != NULL)
5700 release_tree_vector (initializer);
5705 /* Parse a new-placement.
5710 Returns the same representation as for an expression-list. */
5712 static VEC(tree,gc) *
5713 cp_parser_new_placement (cp_parser* parser)
5715 VEC(tree,gc) *expression_list;
5717 /* Parse the expression-list. */
5718 expression_list = (cp_parser_parenthesized_expression_list
5719 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5720 /*non_constant_p=*/NULL));
5722 return expression_list;
5725 /* Parse a new-type-id.
5728 type-specifier-seq new-declarator [opt]
5730 Returns the TYPE allocated. If the new-type-id indicates an array
5731 type, *NELTS is set to the number of elements in the last array
5732 bound; the TYPE will not include the last array bound. */
5735 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5737 cp_decl_specifier_seq type_specifier_seq;
5738 cp_declarator *new_declarator;
5739 cp_declarator *declarator;
5740 cp_declarator *outer_declarator;
5741 const char *saved_message;
5744 /* The type-specifier sequence must not contain type definitions.
5745 (It cannot contain declarations of new types either, but if they
5746 are not definitions we will catch that because they are not
5748 saved_message = parser->type_definition_forbidden_message;
5749 parser->type_definition_forbidden_message
5750 = "types may not be defined in a new-type-id";
5751 /* Parse the type-specifier-seq. */
5752 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5753 &type_specifier_seq);
5754 /* Restore the old message. */
5755 parser->type_definition_forbidden_message = saved_message;
5756 /* Parse the new-declarator. */
5757 new_declarator = cp_parser_new_declarator_opt (parser);
5759 /* Determine the number of elements in the last array dimension, if
5762 /* Skip down to the last array dimension. */
5763 declarator = new_declarator;
5764 outer_declarator = NULL;
5765 while (declarator && (declarator->kind == cdk_pointer
5766 || declarator->kind == cdk_ptrmem))
5768 outer_declarator = declarator;
5769 declarator = declarator->declarator;
5772 && declarator->kind == cdk_array
5773 && declarator->declarator
5774 && declarator->declarator->kind == cdk_array)
5776 outer_declarator = declarator;
5777 declarator = declarator->declarator;
5780 if (declarator && declarator->kind == cdk_array)
5782 *nelts = declarator->u.array.bounds;
5783 if (*nelts == error_mark_node)
5784 *nelts = integer_one_node;
5786 if (outer_declarator)
5787 outer_declarator->declarator = declarator->declarator;
5789 new_declarator = NULL;
5792 type = groktypename (&type_specifier_seq, new_declarator, false);
5796 /* Parse an (optional) new-declarator.
5799 ptr-operator new-declarator [opt]
5800 direct-new-declarator
5802 Returns the declarator. */
5804 static cp_declarator *
5805 cp_parser_new_declarator_opt (cp_parser* parser)
5807 enum tree_code code;
5809 cp_cv_quals cv_quals;
5811 /* We don't know if there's a ptr-operator next, or not. */
5812 cp_parser_parse_tentatively (parser);
5813 /* Look for a ptr-operator. */
5814 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5815 /* If that worked, look for more new-declarators. */
5816 if (cp_parser_parse_definitely (parser))
5818 cp_declarator *declarator;
5820 /* Parse another optional declarator. */
5821 declarator = cp_parser_new_declarator_opt (parser);
5823 return cp_parser_make_indirect_declarator
5824 (code, type, cv_quals, declarator);
5827 /* If the next token is a `[', there is a direct-new-declarator. */
5828 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5829 return cp_parser_direct_new_declarator (parser);
5834 /* Parse a direct-new-declarator.
5836 direct-new-declarator:
5838 direct-new-declarator [constant-expression]
5842 static cp_declarator *
5843 cp_parser_direct_new_declarator (cp_parser* parser)
5845 cp_declarator *declarator = NULL;
5851 /* Look for the opening `['. */
5852 cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5853 /* The first expression is not required to be constant. */
5856 cp_token *token = cp_lexer_peek_token (parser->lexer);
5857 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5858 /* The standard requires that the expression have integral
5859 type. DR 74 adds enumeration types. We believe that the
5860 real intent is that these expressions be handled like the
5861 expression in a `switch' condition, which also allows
5862 classes with a single conversion to integral or
5863 enumeration type. */
5864 if (!processing_template_decl)
5867 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5872 error ("%Hexpression in new-declarator must have integral "
5873 "or enumeration type", &token->location);
5874 expression = error_mark_node;
5878 /* But all the other expressions must be. */
5881 = cp_parser_constant_expression (parser,
5882 /*allow_non_constant=*/false,
5884 /* Look for the closing `]'. */
5885 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5887 /* Add this bound to the declarator. */
5888 declarator = make_array_declarator (declarator, expression);
5890 /* If the next token is not a `[', then there are no more
5892 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5899 /* Parse a new-initializer.
5902 ( expression-list [opt] )
5905 Returns a representation of the expression-list. */
5907 static VEC(tree,gc) *
5908 cp_parser_new_initializer (cp_parser* parser)
5910 VEC(tree,gc) *expression_list;
5912 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5915 bool expr_non_constant_p;
5916 maybe_warn_cpp0x ("extended initializer lists");
5917 t = cp_parser_braced_list (parser, &expr_non_constant_p);
5918 CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5919 expression_list = make_tree_vector_single (t);
5922 expression_list = (cp_parser_parenthesized_expression_list
5923 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5924 /*non_constant_p=*/NULL));
5926 return expression_list;
5929 /* Parse a delete-expression.
5932 :: [opt] delete cast-expression
5933 :: [opt] delete [ ] cast-expression
5935 Returns a representation of the expression. */
5938 cp_parser_delete_expression (cp_parser* parser)
5940 bool global_scope_p;
5944 /* Look for the optional `::' operator. */
5946 = (cp_parser_global_scope_opt (parser,
5947 /*current_scope_valid_p=*/false)
5949 /* Look for the `delete' keyword. */
5950 cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
5951 /* See if the array syntax is in use. */
5952 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5954 /* Consume the `[' token. */
5955 cp_lexer_consume_token (parser->lexer);
5956 /* Look for the `]' token. */
5957 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5958 /* Remember that this is the `[]' construct. */
5964 /* Parse the cast-expression. */
5965 expression = cp_parser_simple_cast_expression (parser);
5967 /* A delete-expression may not appear in an integral constant
5969 if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
5970 return error_mark_node;
5972 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5975 /* Returns true if TOKEN may start a cast-expression and false
5979 cp_parser_token_starts_cast_expression (cp_token *token)
5981 switch (token->type)
5987 case CPP_CLOSE_SQUARE:
5988 case CPP_CLOSE_PAREN:
5989 case CPP_CLOSE_BRACE:
5993 case CPP_DEREF_STAR:
6001 case CPP_GREATER_EQ:
6021 /* '[' may start a primary-expression in obj-c++. */
6022 case CPP_OPEN_SQUARE:
6023 return c_dialect_objc ();
6030 /* Parse a cast-expression.
6034 ( type-id ) cast-expression
6036 ADDRESS_P is true iff the unary-expression is appearing as the
6037 operand of the `&' operator. CAST_P is true if this expression is
6038 the target of a cast.
6040 Returns a representation of the expression. */
6043 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6046 /* If it's a `(', then we might be looking at a cast. */
6047 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6049 tree type = NULL_TREE;
6050 tree expr = NULL_TREE;
6051 bool compound_literal_p;
6052 const char *saved_message;
6054 /* There's no way to know yet whether or not this is a cast.
6055 For example, `(int (3))' is a unary-expression, while `(int)
6056 3' is a cast. So, we resort to parsing tentatively. */
6057 cp_parser_parse_tentatively (parser);
6058 /* Types may not be defined in a cast. */
6059 saved_message = parser->type_definition_forbidden_message;
6060 parser->type_definition_forbidden_message
6061 = "types may not be defined in casts";
6062 /* Consume the `('. */
6063 cp_lexer_consume_token (parser->lexer);
6064 /* A very tricky bit is that `(struct S) { 3 }' is a
6065 compound-literal (which we permit in C++ as an extension).
6066 But, that construct is not a cast-expression -- it is a
6067 postfix-expression. (The reason is that `(struct S) { 3 }.i'
6068 is legal; if the compound-literal were a cast-expression,
6069 you'd need an extra set of parentheses.) But, if we parse
6070 the type-id, and it happens to be a class-specifier, then we
6071 will commit to the parse at that point, because we cannot
6072 undo the action that is done when creating a new class. So,
6073 then we cannot back up and do a postfix-expression.
6075 Therefore, we scan ahead to the closing `)', and check to see
6076 if the token after the `)' is a `{'. If so, we are not
6077 looking at a cast-expression.
6079 Save tokens so that we can put them back. */
6080 cp_lexer_save_tokens (parser->lexer);
6081 /* Skip tokens until the next token is a closing parenthesis.
6082 If we find the closing `)', and the next token is a `{', then
6083 we are looking at a compound-literal. */
6085 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6086 /*consume_paren=*/true)
6087 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6088 /* Roll back the tokens we skipped. */
6089 cp_lexer_rollback_tokens (parser->lexer);
6090 /* If we were looking at a compound-literal, simulate an error
6091 so that the call to cp_parser_parse_definitely below will
6093 if (compound_literal_p)
6094 cp_parser_simulate_error (parser);
6097 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6098 parser->in_type_id_in_expr_p = true;
6099 /* Look for the type-id. */
6100 type = cp_parser_type_id (parser);
6101 /* Look for the closing `)'. */
6102 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6103 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6106 /* Restore the saved message. */
6107 parser->type_definition_forbidden_message = saved_message;
6109 /* At this point this can only be either a cast or a
6110 parenthesized ctor such as `(T ())' that looks like a cast to
6111 function returning T. */
6112 if (!cp_parser_error_occurred (parser)
6113 && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6116 cp_parser_parse_definitely (parser);
6117 expr = cp_parser_cast_expression (parser,
6118 /*address_p=*/false,
6119 /*cast_p=*/true, pidk);
6121 /* Warn about old-style casts, if so requested. */
6122 if (warn_old_style_cast
6123 && !in_system_header
6124 && !VOID_TYPE_P (type)
6125 && current_lang_name != lang_name_c)
6126 warning (OPT_Wold_style_cast, "use of old-style cast");
6128 /* Only type conversions to integral or enumeration types
6129 can be used in constant-expressions. */
6130 if (!cast_valid_in_integral_constant_expression_p (type)
6131 && (cp_parser_non_integral_constant_expression
6133 "a cast to a type other than an integral or "
6134 "enumeration type")))
6135 return error_mark_node;
6137 /* Perform the cast. */
6138 expr = build_c_cast (input_location, type, expr);
6142 cp_parser_abort_tentative_parse (parser);
6145 /* If we get here, then it's not a cast, so it must be a
6146 unary-expression. */
6147 return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6150 /* Parse a binary expression of the general form:
6154 pm-expression .* cast-expression
6155 pm-expression ->* cast-expression
6157 multiplicative-expression:
6159 multiplicative-expression * pm-expression
6160 multiplicative-expression / pm-expression
6161 multiplicative-expression % pm-expression
6163 additive-expression:
6164 multiplicative-expression
6165 additive-expression + multiplicative-expression
6166 additive-expression - multiplicative-expression
6170 shift-expression << additive-expression
6171 shift-expression >> additive-expression
6173 relational-expression:
6175 relational-expression < shift-expression
6176 relational-expression > shift-expression
6177 relational-expression <= shift-expression
6178 relational-expression >= shift-expression
6182 relational-expression:
6183 relational-expression <? shift-expression
6184 relational-expression >? shift-expression
6186 equality-expression:
6187 relational-expression
6188 equality-expression == relational-expression
6189 equality-expression != relational-expression
6193 and-expression & equality-expression
6195 exclusive-or-expression:
6197 exclusive-or-expression ^ and-expression
6199 inclusive-or-expression:
6200 exclusive-or-expression
6201 inclusive-or-expression | exclusive-or-expression
6203 logical-and-expression:
6204 inclusive-or-expression
6205 logical-and-expression && inclusive-or-expression
6207 logical-or-expression:
6208 logical-and-expression
6209 logical-or-expression || logical-and-expression
6211 All these are implemented with a single function like:
6214 simple-cast-expression
6215 binary-expression <token> binary-expression
6217 CAST_P is true if this expression is the target of a cast.
6219 The binops_by_token map is used to get the tree codes for each <token> type.
6220 binary-expressions are associated according to a precedence table. */
6222 #define TOKEN_PRECEDENCE(token) \
6223 (((token->type == CPP_GREATER \
6224 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6225 && !parser->greater_than_is_operator_p) \
6226 ? PREC_NOT_OPERATOR \
6227 : binops_by_token[token->type].prec)
6230 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6231 bool no_toplevel_fold_p,
6232 enum cp_parser_prec prec,
6235 cp_parser_expression_stack stack;
6236 cp_parser_expression_stack_entry *sp = &stack[0];
6239 enum tree_code tree_type, lhs_type, rhs_type;
6240 enum cp_parser_prec new_prec, lookahead_prec;
6243 /* Parse the first expression. */
6244 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6245 lhs_type = ERROR_MARK;
6249 /* Get an operator token. */
6250 token = cp_lexer_peek_token (parser->lexer);
6252 if (warn_cxx0x_compat
6253 && token->type == CPP_RSHIFT
6254 && !parser->greater_than_is_operator_p)
6256 warning (OPT_Wc__0x_compat,
6257 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
6259 warning (OPT_Wc__0x_compat,
6260 "suggest parentheses around %<>>%> expression");
6263 new_prec = TOKEN_PRECEDENCE (token);
6265 /* Popping an entry off the stack means we completed a subexpression:
6266 - either we found a token which is not an operator (`>' where it is not
6267 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6268 will happen repeatedly;
6269 - or, we found an operator which has lower priority. This is the case
6270 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6272 if (new_prec <= prec)
6281 tree_type = binops_by_token[token->type].tree_type;
6283 /* We used the operator token. */
6284 cp_lexer_consume_token (parser->lexer);
6286 /* Extract another operand. It may be the RHS of this expression
6287 or the LHS of a new, higher priority expression. */
6288 rhs = cp_parser_simple_cast_expression (parser);
6289 rhs_type = ERROR_MARK;
6291 /* Get another operator token. Look up its precedence to avoid
6292 building a useless (immediately popped) stack entry for common
6293 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6294 token = cp_lexer_peek_token (parser->lexer);
6295 lookahead_prec = TOKEN_PRECEDENCE (token);
6296 if (lookahead_prec > new_prec)
6298 /* ... and prepare to parse the RHS of the new, higher priority
6299 expression. Since precedence levels on the stack are
6300 monotonically increasing, we do not have to care about
6303 sp->tree_type = tree_type;
6305 sp->lhs_type = lhs_type;
6308 lhs_type = rhs_type;
6310 new_prec = lookahead_prec;
6314 lookahead_prec = new_prec;
6315 /* If the stack is not empty, we have parsed into LHS the right side
6316 (`4' in the example above) of an expression we had suspended.
6317 We can use the information on the stack to recover the LHS (`3')
6318 from the stack together with the tree code (`MULT_EXPR'), and
6319 the precedence of the higher level subexpression
6320 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6321 which will be used to actually build the additive expression. */
6324 tree_type = sp->tree_type;
6326 rhs_type = lhs_type;
6328 lhs_type = sp->lhs_type;
6331 overloaded_p = false;
6332 /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6333 ERROR_MARK for everything that is not a binary expression.
6334 This makes warn_about_parentheses miss some warnings that
6335 involve unary operators. For unary expressions we should
6336 pass the correct tree_code unless the unary expression was
6337 surrounded by parentheses.
6339 if (no_toplevel_fold_p
6340 && lookahead_prec <= prec
6342 && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6343 lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6345 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6346 &overloaded_p, tf_warning_or_error);
6347 lhs_type = tree_type;
6349 /* If the binary operator required the use of an overloaded operator,
6350 then this expression cannot be an integral constant-expression.
6351 An overloaded operator can be used even if both operands are
6352 otherwise permissible in an integral constant-expression if at
6353 least one of the operands is of enumeration type. */
6356 && (cp_parser_non_integral_constant_expression
6357 (parser, "calls to overloaded operators")))
6358 return error_mark_node;
6365 /* Parse the `? expression : assignment-expression' part of a
6366 conditional-expression. The LOGICAL_OR_EXPR is the
6367 logical-or-expression that started the conditional-expression.
6368 Returns a representation of the entire conditional-expression.
6370 This routine is used by cp_parser_assignment_expression.
6372 ? expression : assignment-expression
6376 ? : assignment-expression */
6379 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6382 tree assignment_expr;
6384 /* Consume the `?' token. */
6385 cp_lexer_consume_token (parser->lexer);
6386 if (cp_parser_allow_gnu_extensions_p (parser)
6387 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6388 /* Implicit true clause. */
6391 /* Parse the expression. */
6392 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6394 /* The next token should be a `:'. */
6395 cp_parser_require (parser, CPP_COLON, "%<:%>");
6396 /* Parse the assignment-expression. */
6397 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6399 /* Build the conditional-expression. */
6400 return build_x_conditional_expr (logical_or_expr,
6403 tf_warning_or_error);
6406 /* Parse an assignment-expression.
6408 assignment-expression:
6409 conditional-expression
6410 logical-or-expression assignment-operator assignment_expression
6413 CAST_P is true if this expression is the target of a cast.
6415 Returns a representation for the expression. */
6418 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6423 /* If the next token is the `throw' keyword, then we're looking at
6424 a throw-expression. */
6425 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6426 expr = cp_parser_throw_expression (parser);
6427 /* Otherwise, it must be that we are looking at a
6428 logical-or-expression. */
6431 /* Parse the binary expressions (logical-or-expression). */
6432 expr = cp_parser_binary_expression (parser, cast_p, false,
6433 PREC_NOT_OPERATOR, pidk);
6434 /* If the next token is a `?' then we're actually looking at a
6435 conditional-expression. */
6436 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6437 return cp_parser_question_colon_clause (parser, expr);
6440 enum tree_code assignment_operator;
6442 /* If it's an assignment-operator, we're using the second
6445 = cp_parser_assignment_operator_opt (parser);
6446 if (assignment_operator != ERROR_MARK)
6448 bool non_constant_p;
6450 /* Parse the right-hand side of the assignment. */
6451 tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6453 if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6454 maybe_warn_cpp0x ("extended initializer lists");
6456 /* An assignment may not appear in a
6457 constant-expression. */
6458 if (cp_parser_non_integral_constant_expression (parser,
6460 return error_mark_node;
6461 /* Build the assignment expression. */
6462 expr = build_x_modify_expr (expr,
6463 assignment_operator,
6465 tf_warning_or_error);
6473 /* Parse an (optional) assignment-operator.
6475 assignment-operator: one of
6476 = *= /= %= += -= >>= <<= &= ^= |=
6480 assignment-operator: one of
6483 If the next token is an assignment operator, the corresponding tree
6484 code is returned, and the token is consumed. For example, for
6485 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6486 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6487 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6488 operator, ERROR_MARK is returned. */
6490 static enum tree_code
6491 cp_parser_assignment_operator_opt (cp_parser* parser)
6496 /* Peek at the next token. */
6497 token = cp_lexer_peek_token (parser->lexer);
6499 switch (token->type)
6510 op = TRUNC_DIV_EXPR;
6514 op = TRUNC_MOD_EXPR;
6546 /* Nothing else is an assignment operator. */
6550 /* If it was an assignment operator, consume it. */
6551 if (op != ERROR_MARK)
6552 cp_lexer_consume_token (parser->lexer);
6557 /* Parse an expression.
6560 assignment-expression
6561 expression , assignment-expression
6563 CAST_P is true if this expression is the target of a cast.
6565 Returns a representation of the expression. */
6568 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6570 tree expression = NULL_TREE;
6574 tree assignment_expression;
6576 /* Parse the next assignment-expression. */
6577 assignment_expression
6578 = cp_parser_assignment_expression (parser, cast_p, pidk);
6579 /* If this is the first assignment-expression, we can just
6582 expression = assignment_expression;
6584 expression = build_x_compound_expr (expression,
6585 assignment_expression,
6586 tf_warning_or_error);
6587 /* If the next token is not a comma, then we are done with the
6589 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6591 /* Consume the `,'. */
6592 cp_lexer_consume_token (parser->lexer);
6593 /* A comma operator cannot appear in a constant-expression. */
6594 if (cp_parser_non_integral_constant_expression (parser,
6595 "a comma operator"))
6596 expression = error_mark_node;
6602 /* Parse a constant-expression.
6604 constant-expression:
6605 conditional-expression
6607 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6608 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6609 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6610 is false, NON_CONSTANT_P should be NULL. */
6613 cp_parser_constant_expression (cp_parser* parser,
6614 bool allow_non_constant_p,
6615 bool *non_constant_p)
6617 bool saved_integral_constant_expression_p;
6618 bool saved_allow_non_integral_constant_expression_p;
6619 bool saved_non_integral_constant_expression_p;
6622 /* It might seem that we could simply parse the
6623 conditional-expression, and then check to see if it were
6624 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6625 one that the compiler can figure out is constant, possibly after
6626 doing some simplifications or optimizations. The standard has a
6627 precise definition of constant-expression, and we must honor
6628 that, even though it is somewhat more restrictive.
6634 is not a legal declaration, because `(2, 3)' is not a
6635 constant-expression. The `,' operator is forbidden in a
6636 constant-expression. However, GCC's constant-folding machinery
6637 will fold this operation to an INTEGER_CST for `3'. */
6639 /* Save the old settings. */
6640 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6641 saved_allow_non_integral_constant_expression_p
6642 = parser->allow_non_integral_constant_expression_p;
6643 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6644 /* We are now parsing a constant-expression. */
6645 parser->integral_constant_expression_p = true;
6646 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6647 parser->non_integral_constant_expression_p = false;
6648 /* Although the grammar says "conditional-expression", we parse an
6649 "assignment-expression", which also permits "throw-expression"
6650 and the use of assignment operators. In the case that
6651 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6652 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6653 actually essential that we look for an assignment-expression.
6654 For example, cp_parser_initializer_clauses uses this function to
6655 determine whether a particular assignment-expression is in fact
6657 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6658 /* Restore the old settings. */
6659 parser->integral_constant_expression_p
6660 = saved_integral_constant_expression_p;
6661 parser->allow_non_integral_constant_expression_p
6662 = saved_allow_non_integral_constant_expression_p;
6663 if (allow_non_constant_p)
6664 *non_constant_p = parser->non_integral_constant_expression_p;
6665 else if (parser->non_integral_constant_expression_p)
6666 expression = error_mark_node;
6667 parser->non_integral_constant_expression_p
6668 = saved_non_integral_constant_expression_p;
6673 /* Parse __builtin_offsetof.
6675 offsetof-expression:
6676 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6678 offsetof-member-designator:
6680 | offsetof-member-designator "." id-expression
6681 | offsetof-member-designator "[" expression "]"
6682 | offsetof-member-designator "->" id-expression */
6685 cp_parser_builtin_offsetof (cp_parser *parser)
6687 int save_ice_p, save_non_ice_p;
6692 /* We're about to accept non-integral-constant things, but will
6693 definitely yield an integral constant expression. Save and
6694 restore these values around our local parsing. */
6695 save_ice_p = parser->integral_constant_expression_p;
6696 save_non_ice_p = parser->non_integral_constant_expression_p;
6698 /* Consume the "__builtin_offsetof" token. */
6699 cp_lexer_consume_token (parser->lexer);
6700 /* Consume the opening `('. */
6701 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6702 /* Parse the type-id. */
6703 type = cp_parser_type_id (parser);
6704 /* Look for the `,'. */
6705 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6706 token = cp_lexer_peek_token (parser->lexer);
6708 /* Build the (type *)null that begins the traditional offsetof macro. */
6709 expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6710 tf_warning_or_error);
6712 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6713 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6714 true, &dummy, token->location);
6717 token = cp_lexer_peek_token (parser->lexer);
6718 switch (token->type)
6720 case CPP_OPEN_SQUARE:
6721 /* offsetof-member-designator "[" expression "]" */
6722 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6726 /* offsetof-member-designator "->" identifier */
6727 expr = grok_array_decl (expr, integer_zero_node);
6731 /* offsetof-member-designator "." identifier */
6732 cp_lexer_consume_token (parser->lexer);
6733 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6738 case CPP_CLOSE_PAREN:
6739 /* Consume the ")" token. */
6740 cp_lexer_consume_token (parser->lexer);
6744 /* Error. We know the following require will fail, but
6745 that gives the proper error message. */
6746 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6747 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6748 expr = error_mark_node;
6754 /* If we're processing a template, we can't finish the semantics yet.
6755 Otherwise we can fold the entire expression now. */
6756 if (processing_template_decl)
6757 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6759 expr = finish_offsetof (expr);
6762 parser->integral_constant_expression_p = save_ice_p;
6763 parser->non_integral_constant_expression_p = save_non_ice_p;
6768 /* Parse a trait expression. */
6771 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6774 tree type1, type2 = NULL_TREE;
6775 bool binary = false;
6776 cp_decl_specifier_seq decl_specs;
6780 case RID_HAS_NOTHROW_ASSIGN:
6781 kind = CPTK_HAS_NOTHROW_ASSIGN;
6783 case RID_HAS_NOTHROW_CONSTRUCTOR:
6784 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6786 case RID_HAS_NOTHROW_COPY:
6787 kind = CPTK_HAS_NOTHROW_COPY;
6789 case RID_HAS_TRIVIAL_ASSIGN:
6790 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6792 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6793 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6795 case RID_HAS_TRIVIAL_COPY:
6796 kind = CPTK_HAS_TRIVIAL_COPY;
6798 case RID_HAS_TRIVIAL_DESTRUCTOR:
6799 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6801 case RID_HAS_VIRTUAL_DESTRUCTOR:
6802 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6804 case RID_IS_ABSTRACT:
6805 kind = CPTK_IS_ABSTRACT;
6807 case RID_IS_BASE_OF:
6808 kind = CPTK_IS_BASE_OF;
6812 kind = CPTK_IS_CLASS;
6814 case RID_IS_CONVERTIBLE_TO:
6815 kind = CPTK_IS_CONVERTIBLE_TO;
6819 kind = CPTK_IS_EMPTY;
6822 kind = CPTK_IS_ENUM;
6827 case RID_IS_POLYMORPHIC:
6828 kind = CPTK_IS_POLYMORPHIC;
6831 kind = CPTK_IS_UNION;
6837 /* Consume the token. */
6838 cp_lexer_consume_token (parser->lexer);
6840 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6842 type1 = cp_parser_type_id (parser);
6844 if (type1 == error_mark_node)
6845 return error_mark_node;
6847 /* Build a trivial decl-specifier-seq. */
6848 clear_decl_specs (&decl_specs);
6849 decl_specs.type = type1;
6851 /* Call grokdeclarator to figure out what type this is. */
6852 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6853 /*initialized=*/0, /*attrlist=*/NULL);
6857 cp_parser_require (parser, CPP_COMMA, "%<,%>");
6859 type2 = cp_parser_type_id (parser);
6861 if (type2 == error_mark_node)
6862 return error_mark_node;
6864 /* Build a trivial decl-specifier-seq. */
6865 clear_decl_specs (&decl_specs);
6866 decl_specs.type = type2;
6868 /* Call grokdeclarator to figure out what type this is. */
6869 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6870 /*initialized=*/0, /*attrlist=*/NULL);
6873 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6875 /* Complete the trait expression, which may mean either processing
6876 the trait expr now or saving it for template instantiation. */
6877 return finish_trait_expr (kind, type1, type2);
6880 /* Statements [gram.stmt.stmt] */
6882 /* Parse a statement.
6886 expression-statement
6891 declaration-statement
6894 IN_COMPOUND is true when the statement is nested inside a
6895 cp_parser_compound_statement; this matters for certain pragmas.
6897 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6898 is a (possibly labeled) if statement which is not enclosed in braces
6899 and has an else clause. This is used to implement -Wparentheses. */
6902 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6903 bool in_compound, bool *if_p)
6907 location_t statement_location;
6912 /* There is no statement yet. */
6913 statement = NULL_TREE;
6914 /* Peek at the next token. */
6915 token = cp_lexer_peek_token (parser->lexer);
6916 /* Remember the location of the first token in the statement. */
6917 statement_location = token->location;
6918 /* If this is a keyword, then that will often determine what kind of
6919 statement we have. */
6920 if (token->type == CPP_KEYWORD)
6922 enum rid keyword = token->keyword;
6928 /* Looks like a labeled-statement with a case label.
6929 Parse the label, and then use tail recursion to parse
6931 cp_parser_label_for_labeled_statement (parser);
6936 statement = cp_parser_selection_statement (parser, if_p);
6942 statement = cp_parser_iteration_statement (parser);
6949 statement = cp_parser_jump_statement (parser);
6952 /* Objective-C++ exception-handling constructs. */
6955 case RID_AT_FINALLY:
6956 case RID_AT_SYNCHRONIZED:
6958 statement = cp_parser_objc_statement (parser);
6962 statement = cp_parser_try_block (parser);
6966 /* This must be a namespace alias definition. */
6967 cp_parser_declaration_statement (parser);
6971 /* It might be a keyword like `int' that can start a
6972 declaration-statement. */
6976 else if (token->type == CPP_NAME)
6978 /* If the next token is a `:', then we are looking at a
6979 labeled-statement. */
6980 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6981 if (token->type == CPP_COLON)
6983 /* Looks like a labeled-statement with an ordinary label.
6984 Parse the label, and then use tail recursion to parse
6986 cp_parser_label_for_labeled_statement (parser);
6990 /* Anything that starts with a `{' must be a compound-statement. */
6991 else if (token->type == CPP_OPEN_BRACE)
6992 statement = cp_parser_compound_statement (parser, NULL, false);
6993 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6994 a statement all its own. */
6995 else if (token->type == CPP_PRAGMA)
6997 /* Only certain OpenMP pragmas are attached to statements, and thus
6998 are considered statements themselves. All others are not. In
6999 the context of a compound, accept the pragma as a "statement" and
7000 return so that we can check for a close brace. Otherwise we
7001 require a real statement and must go back and read one. */
7003 cp_parser_pragma (parser, pragma_compound);
7004 else if (!cp_parser_pragma (parser, pragma_stmt))
7008 else if (token->type == CPP_EOF)
7010 cp_parser_error (parser, "expected statement");
7014 /* Everything else must be a declaration-statement or an
7015 expression-statement. Try for the declaration-statement
7016 first, unless we are looking at a `;', in which case we know that
7017 we have an expression-statement. */
7020 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7022 cp_parser_parse_tentatively (parser);
7023 /* Try to parse the declaration-statement. */
7024 cp_parser_declaration_statement (parser);
7025 /* If that worked, we're done. */
7026 if (cp_parser_parse_definitely (parser))
7029 /* Look for an expression-statement instead. */
7030 statement = cp_parser_expression_statement (parser, in_statement_expr);
7033 /* Set the line number for the statement. */
7034 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7035 SET_EXPR_LOCATION (statement, statement_location);
7038 /* Parse the label for a labeled-statement, i.e.
7041 case constant-expression :
7045 case constant-expression ... constant-expression : statement
7047 When a label is parsed without errors, the label is added to the
7048 parse tree by the finish_* functions, so this function doesn't
7049 have to return the label. */
7052 cp_parser_label_for_labeled_statement (cp_parser* parser)
7055 tree label = NULL_TREE;
7057 /* The next token should be an identifier. */
7058 token = cp_lexer_peek_token (parser->lexer);
7059 if (token->type != CPP_NAME
7060 && token->type != CPP_KEYWORD)
7062 cp_parser_error (parser, "expected labeled-statement");
7066 switch (token->keyword)
7073 /* Consume the `case' token. */
7074 cp_lexer_consume_token (parser->lexer);
7075 /* Parse the constant-expression. */
7076 expr = cp_parser_constant_expression (parser,
7077 /*allow_non_constant_p=*/false,
7080 ellipsis = cp_lexer_peek_token (parser->lexer);
7081 if (ellipsis->type == CPP_ELLIPSIS)
7083 /* Consume the `...' token. */
7084 cp_lexer_consume_token (parser->lexer);
7086 cp_parser_constant_expression (parser,
7087 /*allow_non_constant_p=*/false,
7089 /* We don't need to emit warnings here, as the common code
7090 will do this for us. */
7093 expr_hi = NULL_TREE;
7095 if (parser->in_switch_statement_p)
7096 finish_case_label (token->location, expr, expr_hi);
7098 error ("%Hcase label %qE not within a switch statement",
7099 &token->location, expr);
7104 /* Consume the `default' token. */
7105 cp_lexer_consume_token (parser->lexer);
7107 if (parser->in_switch_statement_p)
7108 finish_case_label (token->location, NULL_TREE, NULL_TREE);
7110 error ("%Hcase label not within a switch statement", &token->location);
7114 /* Anything else must be an ordinary label. */
7115 label = finish_label_stmt (cp_parser_identifier (parser));
7119 /* Require the `:' token. */
7120 cp_parser_require (parser, CPP_COLON, "%<:%>");
7122 /* An ordinary label may optionally be followed by attributes.
7123 However, this is only permitted if the attributes are then
7124 followed by a semicolon. This is because, for backward
7125 compatibility, when parsing
7126 lab: __attribute__ ((unused)) int i;
7127 we want the attribute to attach to "i", not "lab". */
7128 if (label != NULL_TREE
7129 && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7133 cp_parser_parse_tentatively (parser);
7134 attrs = cp_parser_attributes_opt (parser);
7135 if (attrs == NULL_TREE
7136 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7137 cp_parser_abort_tentative_parse (parser);
7138 else if (!cp_parser_parse_definitely (parser))
7141 cplus_decl_attributes (&label, attrs, 0);
7145 /* Parse an expression-statement.
7147 expression-statement:
7150 Returns the new EXPR_STMT -- or NULL_TREE if the expression
7151 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7152 indicates whether this expression-statement is part of an
7153 expression statement. */
7156 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7158 tree statement = NULL_TREE;
7160 /* If the next token is a ';', then there is no expression
7162 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7163 statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7165 /* Consume the final `;'. */
7166 cp_parser_consume_semicolon_at_end_of_statement (parser);
7168 if (in_statement_expr
7169 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7170 /* This is the final expression statement of a statement
7172 statement = finish_stmt_expr_expr (statement, in_statement_expr);
7174 statement = finish_expr_stmt (statement);
7181 /* Parse a compound-statement.
7184 { statement-seq [opt] }
7189 { label-declaration-seq [opt] statement-seq [opt] }
7191 label-declaration-seq:
7193 label-declaration-seq label-declaration
7195 Returns a tree representing the statement. */
7198 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7203 /* Consume the `{'. */
7204 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7205 return error_mark_node;
7206 /* Begin the compound-statement. */
7207 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7208 /* If the next keyword is `__label__' we have a label declaration. */
7209 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7210 cp_parser_label_declaration (parser);
7211 /* Parse an (optional) statement-seq. */
7212 cp_parser_statement_seq_opt (parser, in_statement_expr);
7213 /* Finish the compound-statement. */
7214 finish_compound_stmt (compound_stmt);
7215 /* Consume the `}'. */
7216 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7218 return compound_stmt;
7221 /* Parse an (optional) statement-seq.
7225 statement-seq [opt] statement */
7228 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7230 /* Scan statements until there aren't any more. */
7233 cp_token *token = cp_lexer_peek_token (parser->lexer);
7235 /* If we're looking at a `}', then we've run out of statements. */
7236 if (token->type == CPP_CLOSE_BRACE
7237 || token->type == CPP_EOF
7238 || token->type == CPP_PRAGMA_EOL)
7241 /* If we are in a compound statement and find 'else' then
7242 something went wrong. */
7243 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7245 if (parser->in_statement & IN_IF_STMT)
7249 token = cp_lexer_consume_token (parser->lexer);
7250 error ("%H%<else%> without a previous %<if%>", &token->location);
7254 /* Parse the statement. */
7255 cp_parser_statement (parser, in_statement_expr, true, NULL);
7259 /* Parse a selection-statement.
7261 selection-statement:
7262 if ( condition ) statement
7263 if ( condition ) statement else statement
7264 switch ( condition ) statement
7266 Returns the new IF_STMT or SWITCH_STMT.
7268 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7269 is a (possibly labeled) if statement which is not enclosed in
7270 braces and has an else clause. This is used to implement
7274 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7282 /* Peek at the next token. */
7283 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7285 /* See what kind of keyword it is. */
7286 keyword = token->keyword;
7295 /* Look for the `('. */
7296 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7298 cp_parser_skip_to_end_of_statement (parser);
7299 return error_mark_node;
7302 /* Begin the selection-statement. */
7303 if (keyword == RID_IF)
7304 statement = begin_if_stmt ();
7306 statement = begin_switch_stmt ();
7308 /* Parse the condition. */
7309 condition = cp_parser_condition (parser);
7310 /* Look for the `)'. */
7311 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7312 cp_parser_skip_to_closing_parenthesis (parser, true, false,
7313 /*consume_paren=*/true);
7315 if (keyword == RID_IF)
7318 unsigned char in_statement;
7320 /* Add the condition. */
7321 finish_if_stmt_cond (condition, statement);
7323 /* Parse the then-clause. */
7324 in_statement = parser->in_statement;
7325 parser->in_statement |= IN_IF_STMT;
7326 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7328 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7329 add_stmt (build_empty_stmt (loc));
7330 cp_lexer_consume_token (parser->lexer);
7331 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7332 warning_at (loc, OPT_Wempty_body, "suggest braces around "
7333 "empty body in an %<if%> statement");
7337 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7338 parser->in_statement = in_statement;
7340 finish_then_clause (statement);
7342 /* If the next token is `else', parse the else-clause. */
7343 if (cp_lexer_next_token_is_keyword (parser->lexer,
7346 /* Consume the `else' keyword. */
7347 cp_lexer_consume_token (parser->lexer);
7348 begin_else_clause (statement);
7349 /* Parse the else-clause. */
7350 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7353 loc = cp_lexer_peek_token (parser->lexer)->location;
7355 OPT_Wempty_body, "suggest braces around "
7356 "empty body in an %<else%> statement");
7357 add_stmt (build_empty_stmt (loc));
7358 cp_lexer_consume_token (parser->lexer);
7361 cp_parser_implicitly_scoped_statement (parser, NULL);
7363 finish_else_clause (statement);
7365 /* If we are currently parsing a then-clause, then
7366 IF_P will not be NULL. We set it to true to
7367 indicate that this if statement has an else clause.
7368 This may trigger the Wparentheses warning below
7369 when we get back up to the parent if statement. */
7375 /* This if statement does not have an else clause. If
7376 NESTED_IF is true, then the then-clause is an if
7377 statement which does have an else clause. We warn
7378 about the potential ambiguity. */
7380 warning (OPT_Wparentheses,
7381 ("%Hsuggest explicit braces "
7382 "to avoid ambiguous %<else%>"),
7383 EXPR_LOCUS (statement));
7386 /* Now we're all done with the if-statement. */
7387 finish_if_stmt (statement);
7391 bool in_switch_statement_p;
7392 unsigned char in_statement;
7394 /* Add the condition. */
7395 finish_switch_cond (condition, statement);
7397 /* Parse the body of the switch-statement. */
7398 in_switch_statement_p = parser->in_switch_statement_p;
7399 in_statement = parser->in_statement;
7400 parser->in_switch_statement_p = true;
7401 parser->in_statement |= IN_SWITCH_STMT;
7402 cp_parser_implicitly_scoped_statement (parser, NULL);
7403 parser->in_switch_statement_p = in_switch_statement_p;
7404 parser->in_statement = in_statement;
7406 /* Now we're all done with the switch-statement. */
7407 finish_switch_stmt (statement);
7415 cp_parser_error (parser, "expected selection-statement");
7416 return error_mark_node;
7420 /* Parse a condition.
7424 type-specifier-seq declarator = initializer-clause
7425 type-specifier-seq declarator braced-init-list
7430 type-specifier-seq declarator asm-specification [opt]
7431 attributes [opt] = assignment-expression
7433 Returns the expression that should be tested. */
7436 cp_parser_condition (cp_parser* parser)
7438 cp_decl_specifier_seq type_specifiers;
7439 const char *saved_message;
7441 /* Try the declaration first. */
7442 cp_parser_parse_tentatively (parser);
7443 /* New types are not allowed in the type-specifier-seq for a
7445 saved_message = parser->type_definition_forbidden_message;
7446 parser->type_definition_forbidden_message
7447 = "types may not be defined in conditions";
7448 /* Parse the type-specifier-seq. */
7449 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7451 /* Restore the saved message. */
7452 parser->type_definition_forbidden_message = saved_message;
7453 /* If all is well, we might be looking at a declaration. */
7454 if (!cp_parser_error_occurred (parser))
7457 tree asm_specification;
7459 cp_declarator *declarator;
7460 tree initializer = NULL_TREE;
7462 /* Parse the declarator. */
7463 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7464 /*ctor_dtor_or_conv_p=*/NULL,
7465 /*parenthesized_p=*/NULL,
7466 /*member_p=*/false);
7467 /* Parse the attributes. */
7468 attributes = cp_parser_attributes_opt (parser);
7469 /* Parse the asm-specification. */
7470 asm_specification = cp_parser_asm_specification_opt (parser);
7471 /* If the next token is not an `=' or '{', then we might still be
7472 looking at an expression. For example:
7476 looks like a decl-specifier-seq and a declarator -- but then
7477 there is no `=', so this is an expression. */
7478 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7479 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7480 cp_parser_simulate_error (parser);
7482 /* If we did see an `=' or '{', then we are looking at a declaration
7484 if (cp_parser_parse_definitely (parser))
7487 bool non_constant_p;
7488 bool flags = LOOKUP_ONLYCONVERTING;
7490 /* Create the declaration. */
7491 decl = start_decl (declarator, &type_specifiers,
7492 /*initialized_p=*/true,
7493 attributes, /*prefix_attributes=*/NULL_TREE,
7496 /* Parse the initializer. */
7497 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7499 initializer = cp_parser_braced_list (parser, &non_constant_p);
7500 CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
7505 /* Consume the `='. */
7506 cp_parser_require (parser, CPP_EQ, "%<=%>");
7507 initializer = cp_parser_initializer_clause (parser, &non_constant_p);
7509 if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
7510 maybe_warn_cpp0x ("extended initializer lists");
7512 if (!non_constant_p)
7513 initializer = fold_non_dependent_expr (initializer);
7515 /* Process the initializer. */
7516 cp_finish_decl (decl,
7517 initializer, !non_constant_p,
7522 pop_scope (pushed_scope);
7524 return convert_from_reference (decl);
7527 /* If we didn't even get past the declarator successfully, we are
7528 definitely not looking at a declaration. */
7530 cp_parser_abort_tentative_parse (parser);
7532 /* Otherwise, we are looking at an expression. */
7533 return cp_parser_expression (parser, /*cast_p=*/false, NULL);
7536 /* Parse an iteration-statement.
7538 iteration-statement:
7539 while ( condition ) statement
7540 do statement while ( expression ) ;
7541 for ( for-init-statement condition [opt] ; expression [opt] )
7544 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7547 cp_parser_iteration_statement (cp_parser* parser)
7552 unsigned char in_statement;
7554 /* Peek at the next token. */
7555 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7557 return error_mark_node;
7559 /* Remember whether or not we are already within an iteration
7561 in_statement = parser->in_statement;
7563 /* See what kind of keyword it is. */
7564 keyword = token->keyword;
7571 /* Begin the while-statement. */
7572 statement = begin_while_stmt ();
7573 /* Look for the `('. */
7574 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7575 /* Parse the condition. */
7576 condition = cp_parser_condition (parser);
7577 finish_while_stmt_cond (condition, statement);
7578 /* Look for the `)'. */
7579 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7580 /* Parse the dependent statement. */
7581 parser->in_statement = IN_ITERATION_STMT;
7582 cp_parser_already_scoped_statement (parser);
7583 parser->in_statement = in_statement;
7584 /* We're done with the while-statement. */
7585 finish_while_stmt (statement);
7593 /* Begin the do-statement. */
7594 statement = begin_do_stmt ();
7595 /* Parse the body of the do-statement. */
7596 parser->in_statement = IN_ITERATION_STMT;
7597 cp_parser_implicitly_scoped_statement (parser, NULL);
7598 parser->in_statement = in_statement;
7599 finish_do_body (statement);
7600 /* Look for the `while' keyword. */
7601 cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
7602 /* Look for the `('. */
7603 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7604 /* Parse the expression. */
7605 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7606 /* We're done with the do-statement. */
7607 finish_do_stmt (expression, statement);
7608 /* Look for the `)'. */
7609 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7610 /* Look for the `;'. */
7611 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7617 tree condition = NULL_TREE;
7618 tree expression = NULL_TREE;
7620 /* Begin the for-statement. */
7621 statement = begin_for_stmt ();
7622 /* Look for the `('. */
7623 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
7624 /* Parse the initialization. */
7625 cp_parser_for_init_statement (parser);
7626 finish_for_init_stmt (statement);
7628 /* If there's a condition, process it. */
7629 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7630 condition = cp_parser_condition (parser);
7631 finish_for_cond (condition, statement);
7632 /* Look for the `;'. */
7633 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7635 /* If there's an expression, process it. */
7636 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7637 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7638 finish_for_expr (expression, statement);
7639 /* Look for the `)'. */
7640 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7642 /* Parse the body of the for-statement. */
7643 parser->in_statement = IN_ITERATION_STMT;
7644 cp_parser_already_scoped_statement (parser);
7645 parser->in_statement = in_statement;
7647 /* We're done with the for-statement. */
7648 finish_for_stmt (statement);
7653 cp_parser_error (parser, "expected iteration-statement");
7654 statement = error_mark_node;
7661 /* Parse a for-init-statement.
7664 expression-statement
7665 simple-declaration */
7668 cp_parser_for_init_statement (cp_parser* parser)
7670 /* If the next token is a `;', then we have an empty
7671 expression-statement. Grammatically, this is also a
7672 simple-declaration, but an invalid one, because it does not
7673 declare anything. Therefore, if we did not handle this case
7674 specially, we would issue an error message about an invalid
7676 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7678 /* We're going to speculatively look for a declaration, falling back
7679 to an expression, if necessary. */
7680 cp_parser_parse_tentatively (parser);
7681 /* Parse the declaration. */
7682 cp_parser_simple_declaration (parser,
7683 /*function_definition_allowed_p=*/false);
7684 /* If the tentative parse failed, then we shall need to look for an
7685 expression-statement. */
7686 if (cp_parser_parse_definitely (parser))
7690 cp_parser_expression_statement (parser, false);
7693 /* Parse a jump-statement.
7698 return expression [opt] ;
7699 return braced-init-list ;
7707 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7710 cp_parser_jump_statement (cp_parser* parser)
7712 tree statement = error_mark_node;
7715 unsigned char in_statement;
7717 /* Peek at the next token. */
7718 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7720 return error_mark_node;
7722 /* See what kind of keyword it is. */
7723 keyword = token->keyword;
7727 in_statement = parser->in_statement & ~IN_IF_STMT;
7728 switch (in_statement)
7731 error ("%Hbreak statement not within loop or switch", &token->location);
7734 gcc_assert ((in_statement & IN_SWITCH_STMT)
7735 || in_statement == IN_ITERATION_STMT);
7736 statement = finish_break_stmt ();
7739 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7742 error ("%Hbreak statement used with OpenMP for loop", &token->location);
7745 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7749 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7752 error ("%Hcontinue statement not within a loop", &token->location);
7754 case IN_ITERATION_STMT:
7756 statement = finish_continue_stmt ();
7759 error ("%Hinvalid exit from OpenMP structured block", &token->location);
7764 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7770 bool expr_non_constant_p;
7772 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7774 maybe_warn_cpp0x ("extended initializer lists");
7775 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
7777 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7778 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7780 /* If the next token is a `;', then there is no
7783 /* Build the return-statement. */
7784 statement = finish_return_stmt (expr);
7785 /* Look for the final `;'. */
7786 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7791 /* Create the goto-statement. */
7792 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7794 /* Issue a warning about this use of a GNU extension. */
7795 pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
7796 /* Consume the '*' token. */
7797 cp_lexer_consume_token (parser->lexer);
7798 /* Parse the dependent expression. */
7799 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
7802 finish_goto_stmt (cp_parser_identifier (parser));
7803 /* Look for the final `;'. */
7804 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7808 cp_parser_error (parser, "expected jump-statement");
7815 /* Parse a declaration-statement.
7817 declaration-statement:
7818 block-declaration */
7821 cp_parser_declaration_statement (cp_parser* parser)
7825 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7826 p = obstack_alloc (&declarator_obstack, 0);
7828 /* Parse the block-declaration. */
7829 cp_parser_block_declaration (parser, /*statement_p=*/true);
7831 /* Free any declarators allocated. */
7832 obstack_free (&declarator_obstack, p);
7834 /* Finish off the statement. */
7838 /* Some dependent statements (like `if (cond) statement'), are
7839 implicitly in their own scope. In other words, if the statement is
7840 a single statement (as opposed to a compound-statement), it is
7841 none-the-less treated as if it were enclosed in braces. Any
7842 declarations appearing in the dependent statement are out of scope
7843 after control passes that point. This function parses a statement,
7844 but ensures that is in its own scope, even if it is not a
7847 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7848 is a (possibly labeled) if statement which is not enclosed in
7849 braces and has an else clause. This is used to implement
7852 Returns the new statement. */
7855 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7862 /* Mark if () ; with a special NOP_EXPR. */
7863 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7865 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7866 cp_lexer_consume_token (parser->lexer);
7867 statement = add_stmt (build_empty_stmt (loc));
7869 /* if a compound is opened, we simply parse the statement directly. */
7870 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7871 statement = cp_parser_compound_statement (parser, NULL, false);
7872 /* If the token is not a `{', then we must take special action. */
7875 /* Create a compound-statement. */
7876 statement = begin_compound_stmt (0);
7877 /* Parse the dependent-statement. */
7878 cp_parser_statement (parser, NULL_TREE, false, if_p);
7879 /* Finish the dummy compound-statement. */
7880 finish_compound_stmt (statement);
7883 /* Return the statement. */
7887 /* For some dependent statements (like `while (cond) statement'), we
7888 have already created a scope. Therefore, even if the dependent
7889 statement is a compound-statement, we do not want to create another
7893 cp_parser_already_scoped_statement (cp_parser* parser)
7895 /* If the token is a `{', then we must take special action. */
7896 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7897 cp_parser_statement (parser, NULL_TREE, false, NULL);
7900 /* Avoid calling cp_parser_compound_statement, so that we
7901 don't create a new scope. Do everything else by hand. */
7902 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7903 /* If the next keyword is `__label__' we have a label declaration. */
7904 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7905 cp_parser_label_declaration (parser);
7906 /* Parse an (optional) statement-seq. */
7907 cp_parser_statement_seq_opt (parser, NULL_TREE);
7908 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7912 /* Declarations [gram.dcl.dcl] */
7914 /* Parse an optional declaration-sequence.
7918 declaration-seq declaration */
7921 cp_parser_declaration_seq_opt (cp_parser* parser)
7927 token = cp_lexer_peek_token (parser->lexer);
7929 if (token->type == CPP_CLOSE_BRACE
7930 || token->type == CPP_EOF
7931 || token->type == CPP_PRAGMA_EOL)
7934 if (token->type == CPP_SEMICOLON)
7936 /* A declaration consisting of a single semicolon is
7937 invalid. Allow it unless we're being pedantic. */
7938 cp_lexer_consume_token (parser->lexer);
7939 if (!in_system_header)
7940 pedwarn (input_location, OPT_pedantic, "extra %<;%>");
7944 /* If we're entering or exiting a region that's implicitly
7945 extern "C", modify the lang context appropriately. */
7946 if (!parser->implicit_extern_c && token->implicit_extern_c)
7948 push_lang_context (lang_name_c);
7949 parser->implicit_extern_c = true;
7951 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7953 pop_lang_context ();
7954 parser->implicit_extern_c = false;
7957 if (token->type == CPP_PRAGMA)
7959 /* A top-level declaration can consist solely of a #pragma.
7960 A nested declaration cannot, so this is done here and not
7961 in cp_parser_declaration. (A #pragma at block scope is
7962 handled in cp_parser_statement.) */
7963 cp_parser_pragma (parser, pragma_external);
7967 /* Parse the declaration itself. */
7968 cp_parser_declaration (parser);
7972 /* Parse a declaration.
7977 template-declaration
7978 explicit-instantiation
7979 explicit-specialization
7980 linkage-specification
7981 namespace-definition
7986 __extension__ declaration */
7989 cp_parser_declaration (cp_parser* parser)
7996 /* Check for the `__extension__' keyword. */
7997 if (cp_parser_extension_opt (parser, &saved_pedantic))
7999 /* Parse the qualified declaration. */
8000 cp_parser_declaration (parser);
8001 /* Restore the PEDANTIC flag. */
8002 pedantic = saved_pedantic;
8007 /* Try to figure out what kind of declaration is present. */
8008 token1 = *cp_lexer_peek_token (parser->lexer);
8010 if (token1.type != CPP_EOF)
8011 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8014 token2.type = CPP_EOF;
8015 token2.keyword = RID_MAX;
8018 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
8019 p = obstack_alloc (&declarator_obstack, 0);
8021 /* If the next token is `extern' and the following token is a string
8022 literal, then we have a linkage specification. */
8023 if (token1.keyword == RID_EXTERN
8024 && cp_parser_is_string_literal (&token2))
8025 cp_parser_linkage_specification (parser);
8026 /* If the next token is `template', then we have either a template
8027 declaration, an explicit instantiation, or an explicit
8029 else if (token1.keyword == RID_TEMPLATE)
8031 /* `template <>' indicates a template specialization. */
8032 if (token2.type == CPP_LESS
8033 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8034 cp_parser_explicit_specialization (parser);
8035 /* `template <' indicates a template declaration. */
8036 else if (token2.type == CPP_LESS)
8037 cp_parser_template_declaration (parser, /*member_p=*/false);
8038 /* Anything else must be an explicit instantiation. */
8040 cp_parser_explicit_instantiation (parser);
8042 /* If the next token is `export', then we have a template
8044 else if (token1.keyword == RID_EXPORT)
8045 cp_parser_template_declaration (parser, /*member_p=*/false);
8046 /* If the next token is `extern', 'static' or 'inline' and the one
8047 after that is `template', we have a GNU extended explicit
8048 instantiation directive. */
8049 else if (cp_parser_allow_gnu_extensions_p (parser)
8050 && (token1.keyword == RID_EXTERN
8051 || token1.keyword == RID_STATIC
8052 || token1.keyword == RID_INLINE)
8053 && token2.keyword == RID_TEMPLATE)
8054 cp_parser_explicit_instantiation (parser);
8055 /* If the next token is `namespace', check for a named or unnamed
8056 namespace definition. */
8057 else if (token1.keyword == RID_NAMESPACE
8058 && (/* A named namespace definition. */
8059 (token2.type == CPP_NAME
8060 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8062 /* An unnamed namespace definition. */
8063 || token2.type == CPP_OPEN_BRACE
8064 || token2.keyword == RID_ATTRIBUTE))
8065 cp_parser_namespace_definition (parser);
8066 /* An inline (associated) namespace definition. */
8067 else if (token1.keyword == RID_INLINE
8068 && token2.keyword == RID_NAMESPACE)
8069 cp_parser_namespace_definition (parser);
8070 /* Objective-C++ declaration/definition. */
8071 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8072 cp_parser_objc_declaration (parser);
8073 /* We must have either a block declaration or a function
8076 /* Try to parse a block-declaration, or a function-definition. */
8077 cp_parser_block_declaration (parser, /*statement_p=*/false);
8079 /* Free any declarators allocated. */
8080 obstack_free (&declarator_obstack, p);
8083 /* Parse a block-declaration.
8088 namespace-alias-definition
8095 __extension__ block-declaration
8100 static_assert-declaration
8102 If STATEMENT_P is TRUE, then this block-declaration is occurring as
8103 part of a declaration-statement. */
8106 cp_parser_block_declaration (cp_parser *parser,
8112 /* Check for the `__extension__' keyword. */
8113 if (cp_parser_extension_opt (parser, &saved_pedantic))
8115 /* Parse the qualified declaration. */
8116 cp_parser_block_declaration (parser, statement_p);
8117 /* Restore the PEDANTIC flag. */
8118 pedantic = saved_pedantic;
8123 /* Peek at the next token to figure out which kind of declaration is
8125 token1 = cp_lexer_peek_token (parser->lexer);
8127 /* If the next keyword is `asm', we have an asm-definition. */
8128 if (token1->keyword == RID_ASM)
8131 cp_parser_commit_to_tentative_parse (parser);
8132 cp_parser_asm_definition (parser);
8134 /* If the next keyword is `namespace', we have a
8135 namespace-alias-definition. */
8136 else if (token1->keyword == RID_NAMESPACE)
8137 cp_parser_namespace_alias_definition (parser);
8138 /* If the next keyword is `using', we have either a
8139 using-declaration or a using-directive. */
8140 else if (token1->keyword == RID_USING)
8145 cp_parser_commit_to_tentative_parse (parser);
8146 /* If the token after `using' is `namespace', then we have a
8148 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8149 if (token2->keyword == RID_NAMESPACE)
8150 cp_parser_using_directive (parser);
8151 /* Otherwise, it's a using-declaration. */
8153 cp_parser_using_declaration (parser,
8154 /*access_declaration_p=*/false);
8156 /* If the next keyword is `__label__' we have a misplaced label
8158 else if (token1->keyword == RID_LABEL)
8160 cp_lexer_consume_token (parser->lexer);
8161 error ("%H%<__label__%> not at the beginning of a block", &token1->location);
8162 cp_parser_skip_to_end_of_statement (parser);
8163 /* If the next token is now a `;', consume it. */
8164 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8165 cp_lexer_consume_token (parser->lexer);
8167 /* If the next token is `static_assert' we have a static assertion. */
8168 else if (token1->keyword == RID_STATIC_ASSERT)
8169 cp_parser_static_assert (parser, /*member_p=*/false);
8170 /* Anything else must be a simple-declaration. */
8172 cp_parser_simple_declaration (parser, !statement_p);
8175 /* Parse a simple-declaration.
8178 decl-specifier-seq [opt] init-declarator-list [opt] ;
8180 init-declarator-list:
8182 init-declarator-list , init-declarator
8184 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8185 function-definition as a simple-declaration. */
8188 cp_parser_simple_declaration (cp_parser* parser,
8189 bool function_definition_allowed_p)
8191 cp_decl_specifier_seq decl_specifiers;
8192 int declares_class_or_enum;
8193 bool saw_declarator;
8195 /* Defer access checks until we know what is being declared; the
8196 checks for names appearing in the decl-specifier-seq should be
8197 done as if we were in the scope of the thing being declared. */
8198 push_deferring_access_checks (dk_deferred);
8200 /* Parse the decl-specifier-seq. We have to keep track of whether
8201 or not the decl-specifier-seq declares a named class or
8202 enumeration type, since that is the only case in which the
8203 init-declarator-list is allowed to be empty.
8207 In a simple-declaration, the optional init-declarator-list can be
8208 omitted only when declaring a class or enumeration, that is when
8209 the decl-specifier-seq contains either a class-specifier, an
8210 elaborated-type-specifier, or an enum-specifier. */
8211 cp_parser_decl_specifier_seq (parser,
8212 CP_PARSER_FLAGS_OPTIONAL,
8214 &declares_class_or_enum);
8215 /* We no longer need to defer access checks. */
8216 stop_deferring_access_checks ();
8218 /* In a block scope, a valid declaration must always have a
8219 decl-specifier-seq. By not trying to parse declarators, we can
8220 resolve the declaration/expression ambiguity more quickly. */
8221 if (!function_definition_allowed_p
8222 && !decl_specifiers.any_specifiers_p)
8224 cp_parser_error (parser, "expected declaration");
8228 /* If the next two tokens are both identifiers, the code is
8229 erroneous. The usual cause of this situation is code like:
8233 where "T" should name a type -- but does not. */
8234 if (!decl_specifiers.type
8235 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8237 /* If parsing tentatively, we should commit; we really are
8238 looking at a declaration. */
8239 cp_parser_commit_to_tentative_parse (parser);
8244 /* If we have seen at least one decl-specifier, and the next token
8245 is not a parenthesis, then we must be looking at a declaration.
8246 (After "int (" we might be looking at a functional cast.) */
8247 if (decl_specifiers.any_specifiers_p
8248 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8249 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8250 && !cp_parser_error_occurred (parser))
8251 cp_parser_commit_to_tentative_parse (parser);
8253 /* Keep going until we hit the `;' at the end of the simple
8255 saw_declarator = false;
8256 while (cp_lexer_next_token_is_not (parser->lexer,
8260 bool function_definition_p;
8265 /* If we are processing next declarator, coma is expected */
8266 token = cp_lexer_peek_token (parser->lexer);
8267 gcc_assert (token->type == CPP_COMMA);
8268 cp_lexer_consume_token (parser->lexer);
8271 saw_declarator = true;
8273 /* Parse the init-declarator. */
8274 decl = cp_parser_init_declarator (parser, &decl_specifiers,
8276 function_definition_allowed_p,
8278 declares_class_or_enum,
8279 &function_definition_p);
8280 /* If an error occurred while parsing tentatively, exit quickly.
8281 (That usually happens when in the body of a function; each
8282 statement is treated as a declaration-statement until proven
8284 if (cp_parser_error_occurred (parser))
8286 /* Handle function definitions specially. */
8287 if (function_definition_p)
8289 /* If the next token is a `,', then we are probably
8290 processing something like:
8294 which is erroneous. */
8295 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8297 cp_token *token = cp_lexer_peek_token (parser->lexer);
8298 error ("%Hmixing declarations and function-definitions is forbidden",
8301 /* Otherwise, we're done with the list of declarators. */
8304 pop_deferring_access_checks ();
8308 /* The next token should be either a `,' or a `;'. */
8309 token = cp_lexer_peek_token (parser->lexer);
8310 /* If it's a `,', there are more declarators to come. */
8311 if (token->type == CPP_COMMA)
8312 /* will be consumed next time around */;
8313 /* If it's a `;', we are done. */
8314 else if (token->type == CPP_SEMICOLON)
8316 /* Anything else is an error. */
8319 /* If we have already issued an error message we don't need
8320 to issue another one. */
8321 if (decl != error_mark_node
8322 || cp_parser_uncommitted_to_tentative_parse_p (parser))
8323 cp_parser_error (parser, "expected %<,%> or %<;%>");
8324 /* Skip tokens until we reach the end of the statement. */
8325 cp_parser_skip_to_end_of_statement (parser);
8326 /* If the next token is now a `;', consume it. */
8327 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8328 cp_lexer_consume_token (parser->lexer);
8331 /* After the first time around, a function-definition is not
8332 allowed -- even if it was OK at first. For example:
8337 function_definition_allowed_p = false;
8340 /* Issue an error message if no declarators are present, and the
8341 decl-specifier-seq does not itself declare a class or
8343 if (!saw_declarator)
8345 if (cp_parser_declares_only_class_p (parser))
8346 shadow_tag (&decl_specifiers);
8347 /* Perform any deferred access checks. */
8348 perform_deferred_access_checks ();
8351 /* Consume the `;'. */
8352 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8355 pop_deferring_access_checks ();
8358 /* Parse a decl-specifier-seq.
8361 decl-specifier-seq [opt] decl-specifier
8364 storage-class-specifier
8375 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8377 The parser flags FLAGS is used to control type-specifier parsing.
8379 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8382 1: one of the decl-specifiers is an elaborated-type-specifier
8383 (i.e., a type declaration)
8384 2: one of the decl-specifiers is an enum-specifier or a
8385 class-specifier (i.e., a type definition)
8390 cp_parser_decl_specifier_seq (cp_parser* parser,
8391 cp_parser_flags flags,
8392 cp_decl_specifier_seq *decl_specs,
8393 int* declares_class_or_enum)
8395 bool constructor_possible_p = !parser->in_declarator_p;
8396 cp_token *start_token = NULL;
8398 /* Clear DECL_SPECS. */
8399 clear_decl_specs (decl_specs);
8401 /* Assume no class or enumeration type is declared. */
8402 *declares_class_or_enum = 0;
8404 /* Keep reading specifiers until there are no more to read. */
8408 bool found_decl_spec;
8411 /* Peek at the next token. */
8412 token = cp_lexer_peek_token (parser->lexer);
8414 /* Save the first token of the decl spec list for error
8417 start_token = token;
8418 /* Handle attributes. */
8419 if (token->keyword == RID_ATTRIBUTE)
8421 /* Parse the attributes. */
8422 decl_specs->attributes
8423 = chainon (decl_specs->attributes,
8424 cp_parser_attributes_opt (parser));
8427 /* Assume we will find a decl-specifier keyword. */
8428 found_decl_spec = true;
8429 /* If the next token is an appropriate keyword, we can simply
8430 add it to the list. */
8431 switch (token->keyword)
8436 if (!at_class_scope_p ())
8438 error ("%H%<friend%> used outside of class", &token->location);
8439 cp_lexer_purge_token (parser->lexer);
8443 ++decl_specs->specs[(int) ds_friend];
8444 /* Consume the token. */
8445 cp_lexer_consume_token (parser->lexer);
8449 /* function-specifier:
8456 cp_parser_function_specifier_opt (parser, decl_specs);
8462 ++decl_specs->specs[(int) ds_typedef];
8463 /* Consume the token. */
8464 cp_lexer_consume_token (parser->lexer);
8465 /* A constructor declarator cannot appear in a typedef. */
8466 constructor_possible_p = false;
8467 /* The "typedef" keyword can only occur in a declaration; we
8468 may as well commit at this point. */
8469 cp_parser_commit_to_tentative_parse (parser);
8471 if (decl_specs->storage_class != sc_none)
8472 decl_specs->conflicting_specifiers_p = true;
8475 /* storage-class-specifier:
8485 if (cxx_dialect == cxx98)
8487 /* Consume the token. */
8488 cp_lexer_consume_token (parser->lexer);
8490 /* Complain about `auto' as a storage specifier, if
8491 we're complaining about C++0x compatibility. */
8494 "%H%<auto%> will change meaning in C++0x; please remove it",
8497 /* Set the storage class anyway. */
8498 cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
8502 /* C++0x auto type-specifier. */
8503 found_decl_spec = false;
8510 /* Consume the token. */
8511 cp_lexer_consume_token (parser->lexer);
8512 cp_parser_set_storage_class (parser, decl_specs, token->keyword,
8516 /* Consume the token. */
8517 cp_lexer_consume_token (parser->lexer);
8518 ++decl_specs->specs[(int) ds_thread];
8522 /* We did not yet find a decl-specifier yet. */
8523 found_decl_spec = false;
8527 /* Constructors are a special case. The `S' in `S()' is not a
8528 decl-specifier; it is the beginning of the declarator. */
8531 && constructor_possible_p
8532 && (cp_parser_constructor_declarator_p
8533 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8535 /* If we don't have a DECL_SPEC yet, then we must be looking at
8536 a type-specifier. */
8537 if (!found_decl_spec && !constructor_p)
8539 int decl_spec_declares_class_or_enum;
8540 bool is_cv_qualifier;
8544 = cp_parser_type_specifier (parser, flags,
8546 /*is_declaration=*/true,
8547 &decl_spec_declares_class_or_enum,
8549 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8551 /* If this type-specifier referenced a user-defined type
8552 (a typedef, class-name, etc.), then we can't allow any
8553 more such type-specifiers henceforth.
8557 The longest sequence of decl-specifiers that could
8558 possibly be a type name is taken as the
8559 decl-specifier-seq of a declaration. The sequence shall
8560 be self-consistent as described below.
8564 As a general rule, at most one type-specifier is allowed
8565 in the complete decl-specifier-seq of a declaration. The
8566 only exceptions are the following:
8568 -- const or volatile can be combined with any other
8571 -- signed or unsigned can be combined with char, long,
8579 void g (const int Pc);
8581 Here, Pc is *not* part of the decl-specifier seq; it's
8582 the declarator. Therefore, once we see a type-specifier
8583 (other than a cv-qualifier), we forbid any additional
8584 user-defined types. We *do* still allow things like `int
8585 int' to be considered a decl-specifier-seq, and issue the
8586 error message later. */
8587 if (type_spec && !is_cv_qualifier)
8588 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8589 /* A constructor declarator cannot follow a type-specifier. */
8592 constructor_possible_p = false;
8593 found_decl_spec = true;
8597 /* If we still do not have a DECL_SPEC, then there are no more
8599 if (!found_decl_spec)
8602 decl_specs->any_specifiers_p = true;
8603 /* After we see one decl-specifier, further decl-specifiers are
8605 flags |= CP_PARSER_FLAGS_OPTIONAL;
8608 cp_parser_check_decl_spec (decl_specs, start_token->location);
8610 /* Don't allow a friend specifier with a class definition. */
8611 if (decl_specs->specs[(int) ds_friend] != 0
8612 && (*declares_class_or_enum & 2))
8613 error ("%Hclass definition may not be declared a friend",
8614 &start_token->location);
8617 /* Parse an (optional) storage-class-specifier.
8619 storage-class-specifier:
8628 storage-class-specifier:
8631 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8634 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8636 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8639 if (cxx_dialect != cxx98)
8641 /* Fall through for C++98. */
8648 /* Consume the token. */
8649 return cp_lexer_consume_token (parser->lexer)->u.value;
8656 /* Parse an (optional) function-specifier.
8663 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8664 Updates DECL_SPECS, if it is non-NULL. */
8667 cp_parser_function_specifier_opt (cp_parser* parser,
8668 cp_decl_specifier_seq *decl_specs)
8670 cp_token *token = cp_lexer_peek_token (parser->lexer);
8671 switch (token->keyword)
8675 ++decl_specs->specs[(int) ds_inline];
8679 /* 14.5.2.3 [temp.mem]
8681 A member function template shall not be virtual. */
8682 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8683 error ("%Htemplates may not be %<virtual%>", &token->location);
8684 else if (decl_specs)
8685 ++decl_specs->specs[(int) ds_virtual];
8690 ++decl_specs->specs[(int) ds_explicit];
8697 /* Consume the token. */
8698 return cp_lexer_consume_token (parser->lexer)->u.value;
8701 /* Parse a linkage-specification.
8703 linkage-specification:
8704 extern string-literal { declaration-seq [opt] }
8705 extern string-literal declaration */
8708 cp_parser_linkage_specification (cp_parser* parser)
8712 /* Look for the `extern' keyword. */
8713 cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
8715 /* Look for the string-literal. */
8716 linkage = cp_parser_string_literal (parser, false, false);
8718 /* Transform the literal into an identifier. If the literal is a
8719 wide-character string, or contains embedded NULs, then we can't
8720 handle it as the user wants. */
8721 if (strlen (TREE_STRING_POINTER (linkage))
8722 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8724 cp_parser_error (parser, "invalid linkage-specification");
8725 /* Assume C++ linkage. */
8726 linkage = lang_name_cplusplus;
8729 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8731 /* We're now using the new linkage. */
8732 push_lang_context (linkage);
8734 /* If the next token is a `{', then we're using the first
8736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8738 /* Consume the `{' token. */
8739 cp_lexer_consume_token (parser->lexer);
8740 /* Parse the declarations. */
8741 cp_parser_declaration_seq_opt (parser);
8742 /* Look for the closing `}'. */
8743 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8745 /* Otherwise, there's just one declaration. */
8748 bool saved_in_unbraced_linkage_specification_p;
8750 saved_in_unbraced_linkage_specification_p
8751 = parser->in_unbraced_linkage_specification_p;
8752 parser->in_unbraced_linkage_specification_p = true;
8753 cp_parser_declaration (parser);
8754 parser->in_unbraced_linkage_specification_p
8755 = saved_in_unbraced_linkage_specification_p;
8758 /* We're done with the linkage-specification. */
8759 pop_lang_context ();
8762 /* Parse a static_assert-declaration.
8764 static_assert-declaration:
8765 static_assert ( constant-expression , string-literal ) ;
8767 If MEMBER_P, this static_assert is a class member. */
8770 cp_parser_static_assert(cp_parser *parser, bool member_p)
8775 location_t saved_loc;
8777 /* Peek at the `static_assert' token so we can keep track of exactly
8778 where the static assertion started. */
8779 token = cp_lexer_peek_token (parser->lexer);
8780 saved_loc = token->location;
8782 /* Look for the `static_assert' keyword. */
8783 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8784 "%<static_assert%>"))
8787 /* We know we are in a static assertion; commit to any tentative
8789 if (cp_parser_parsing_tentatively (parser))
8790 cp_parser_commit_to_tentative_parse (parser);
8792 /* Parse the `(' starting the static assertion condition. */
8793 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8795 /* Parse the constant-expression. */
8797 cp_parser_constant_expression (parser,
8798 /*allow_non_constant_p=*/false,
8799 /*non_constant_p=*/NULL);
8801 /* Parse the separating `,'. */
8802 cp_parser_require (parser, CPP_COMMA, "%<,%>");
8804 /* Parse the string-literal message. */
8805 message = cp_parser_string_literal (parser,
8806 /*translate=*/false,
8809 /* A `)' completes the static assertion. */
8810 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8811 cp_parser_skip_to_closing_parenthesis (parser,
8812 /*recovering=*/true,
8814 /*consume_paren=*/true);
8816 /* A semicolon terminates the declaration. */
8817 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8819 /* Complete the static assertion, which may mean either processing
8820 the static assert now or saving it for template instantiation. */
8821 finish_static_assert (condition, message, saved_loc, member_p);
8824 /* Parse a `decltype' type. Returns the type.
8826 simple-type-specifier:
8827 decltype ( expression ) */
8830 cp_parser_decltype (cp_parser *parser)
8833 bool id_expression_or_member_access_p = false;
8834 const char *saved_message;
8835 bool saved_integral_constant_expression_p;
8836 bool saved_non_integral_constant_expression_p;
8837 cp_token *id_expr_start_token;
8839 /* Look for the `decltype' token. */
8840 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
8841 return error_mark_node;
8843 /* Types cannot be defined in a `decltype' expression. Save away the
8845 saved_message = parser->type_definition_forbidden_message;
8847 /* And create the new one. */
8848 parser->type_definition_forbidden_message
8849 = "types may not be defined in %<decltype%> expressions";
8851 /* The restrictions on constant-expressions do not apply inside
8852 decltype expressions. */
8853 saved_integral_constant_expression_p
8854 = parser->integral_constant_expression_p;
8855 saved_non_integral_constant_expression_p
8856 = parser->non_integral_constant_expression_p;
8857 parser->integral_constant_expression_p = false;
8859 /* Do not actually evaluate the expression. */
8862 /* Parse the opening `('. */
8863 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
8864 return error_mark_node;
8866 /* First, try parsing an id-expression. */
8867 id_expr_start_token = cp_lexer_peek_token (parser->lexer);
8868 cp_parser_parse_tentatively (parser);
8869 expr = cp_parser_id_expression (parser,
8870 /*template_keyword_p=*/false,
8871 /*check_dependency_p=*/true,
8872 /*template_p=*/NULL,
8873 /*declarator_p=*/false,
8874 /*optional_p=*/false);
8876 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8878 bool non_integral_constant_expression_p = false;
8879 tree id_expression = expr;
8881 const char *error_msg;
8883 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8884 /* Lookup the name we got back from the id-expression. */
8885 expr = cp_parser_lookup_name (parser, expr,
8887 /*is_template=*/false,
8888 /*is_namespace=*/false,
8889 /*check_dependency=*/true,
8890 /*ambiguous_decls=*/NULL,
8891 id_expr_start_token->location);
8894 && expr != error_mark_node
8895 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8896 && TREE_CODE (expr) != TYPE_DECL
8897 && (TREE_CODE (expr) != BIT_NOT_EXPR
8898 || !TYPE_P (TREE_OPERAND (expr, 0)))
8899 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8901 /* Complete lookup of the id-expression. */
8902 expr = (finish_id_expression
8903 (id_expression, expr, parser->scope, &idk,
8904 /*integral_constant_expression_p=*/false,
8905 /*allow_non_integral_constant_expression_p=*/true,
8906 &non_integral_constant_expression_p,
8907 /*template_p=*/false,
8909 /*address_p=*/false,
8910 /*template_arg_p=*/false,
8912 id_expr_start_token->location));
8914 if (expr == error_mark_node)
8915 /* We found an id-expression, but it was something that we
8916 should not have found. This is an error, not something
8917 we can recover from, so note that we found an
8918 id-expression and we'll recover as gracefully as
8920 id_expression_or_member_access_p = true;
8924 && expr != error_mark_node
8925 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8926 /* We have an id-expression. */
8927 id_expression_or_member_access_p = true;
8930 if (!id_expression_or_member_access_p)
8932 /* Abort the id-expression parse. */
8933 cp_parser_abort_tentative_parse (parser);
8935 /* Parsing tentatively, again. */
8936 cp_parser_parse_tentatively (parser);
8938 /* Parse a class member access. */
8939 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8941 /*member_access_only_p=*/true, NULL);
8944 && expr != error_mark_node
8945 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8946 /* We have an id-expression. */
8947 id_expression_or_member_access_p = true;
8950 if (id_expression_or_member_access_p)
8951 /* We have parsed the complete id-expression or member access. */
8952 cp_parser_parse_definitely (parser);
8955 /* Abort our attempt to parse an id-expression or member access
8957 cp_parser_abort_tentative_parse (parser);
8959 /* Parse a full expression. */
8960 expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8963 /* Go back to evaluating expressions. */
8966 /* Restore the old message and the integral constant expression
8968 parser->type_definition_forbidden_message = saved_message;
8969 parser->integral_constant_expression_p
8970 = saved_integral_constant_expression_p;
8971 parser->non_integral_constant_expression_p
8972 = saved_non_integral_constant_expression_p;
8974 if (expr == error_mark_node)
8976 /* Skip everything up to the closing `)'. */
8977 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8978 /*consume_paren=*/true);
8979 return error_mark_node;
8982 /* Parse to the closing `)'. */
8983 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
8985 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8986 /*consume_paren=*/true);
8987 return error_mark_node;
8990 return finish_decltype_type (expr, id_expression_or_member_access_p);
8993 /* Special member functions [gram.special] */
8995 /* Parse a conversion-function-id.
8997 conversion-function-id:
8998 operator conversion-type-id
9000 Returns an IDENTIFIER_NODE representing the operator. */
9003 cp_parser_conversion_function_id (cp_parser* parser)
9007 tree saved_qualifying_scope;
9008 tree saved_object_scope;
9009 tree pushed_scope = NULL_TREE;
9011 /* Look for the `operator' token. */
9012 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9013 return error_mark_node;
9014 /* When we parse the conversion-type-id, the current scope will be
9015 reset. However, we need that information in able to look up the
9016 conversion function later, so we save it here. */
9017 saved_scope = parser->scope;
9018 saved_qualifying_scope = parser->qualifying_scope;
9019 saved_object_scope = parser->object_scope;
9020 /* We must enter the scope of the class so that the names of
9021 entities declared within the class are available in the
9022 conversion-type-id. For example, consider:
9029 S::operator I() { ... }
9031 In order to see that `I' is a type-name in the definition, we
9032 must be in the scope of `S'. */
9034 pushed_scope = push_scope (saved_scope);
9035 /* Parse the conversion-type-id. */
9036 type = cp_parser_conversion_type_id (parser);
9037 /* Leave the scope of the class, if any. */
9039 pop_scope (pushed_scope);
9040 /* Restore the saved scope. */
9041 parser->scope = saved_scope;
9042 parser->qualifying_scope = saved_qualifying_scope;
9043 parser->object_scope = saved_object_scope;
9044 /* If the TYPE is invalid, indicate failure. */
9045 if (type == error_mark_node)
9046 return error_mark_node;
9047 return mangle_conv_op_name_for_type (type);
9050 /* Parse a conversion-type-id:
9053 type-specifier-seq conversion-declarator [opt]
9055 Returns the TYPE specified. */
9058 cp_parser_conversion_type_id (cp_parser* parser)
9061 cp_decl_specifier_seq type_specifiers;
9062 cp_declarator *declarator;
9063 tree type_specified;
9065 /* Parse the attributes. */
9066 attributes = cp_parser_attributes_opt (parser);
9067 /* Parse the type-specifiers. */
9068 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9070 /* If that didn't work, stop. */
9071 if (type_specifiers.type == error_mark_node)
9072 return error_mark_node;
9073 /* Parse the conversion-declarator. */
9074 declarator = cp_parser_conversion_declarator_opt (parser);
9076 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
9077 /*initialized=*/0, &attributes);
9079 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9081 /* Don't give this error when parsing tentatively. This happens to
9082 work because we always parse this definitively once. */
9083 if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9084 && type_uses_auto (type_specified))
9086 error ("invalid use of %<auto%> in conversion operator");
9087 return error_mark_node;
9090 return type_specified;
9093 /* Parse an (optional) conversion-declarator.
9095 conversion-declarator:
9096 ptr-operator conversion-declarator [opt]
9100 static cp_declarator *
9101 cp_parser_conversion_declarator_opt (cp_parser* parser)
9103 enum tree_code code;
9105 cp_cv_quals cv_quals;
9107 /* We don't know if there's a ptr-operator next, or not. */
9108 cp_parser_parse_tentatively (parser);
9109 /* Try the ptr-operator. */
9110 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9111 /* If it worked, look for more conversion-declarators. */
9112 if (cp_parser_parse_definitely (parser))
9114 cp_declarator *declarator;
9116 /* Parse another optional declarator. */
9117 declarator = cp_parser_conversion_declarator_opt (parser);
9119 return cp_parser_make_indirect_declarator
9120 (code, class_type, cv_quals, declarator);
9126 /* Parse an (optional) ctor-initializer.
9129 : mem-initializer-list
9131 Returns TRUE iff the ctor-initializer was actually present. */
9134 cp_parser_ctor_initializer_opt (cp_parser* parser)
9136 /* If the next token is not a `:', then there is no
9137 ctor-initializer. */
9138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9140 /* Do default initialization of any bases and members. */
9141 if (DECL_CONSTRUCTOR_P (current_function_decl))
9142 finish_mem_initializers (NULL_TREE);
9147 /* Consume the `:' token. */
9148 cp_lexer_consume_token (parser->lexer);
9149 /* And the mem-initializer-list. */
9150 cp_parser_mem_initializer_list (parser);
9155 /* Parse a mem-initializer-list.
9157 mem-initializer-list:
9158 mem-initializer ... [opt]
9159 mem-initializer ... [opt] , mem-initializer-list */
9162 cp_parser_mem_initializer_list (cp_parser* parser)
9164 tree mem_initializer_list = NULL_TREE;
9165 cp_token *token = cp_lexer_peek_token (parser->lexer);
9167 /* Let the semantic analysis code know that we are starting the
9168 mem-initializer-list. */
9169 if (!DECL_CONSTRUCTOR_P (current_function_decl))
9170 error ("%Honly constructors take base initializers",
9173 /* Loop through the list. */
9176 tree mem_initializer;
9178 token = cp_lexer_peek_token (parser->lexer);
9179 /* Parse the mem-initializer. */
9180 mem_initializer = cp_parser_mem_initializer (parser);
9181 /* If the next token is a `...', we're expanding member initializers. */
9182 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9184 /* Consume the `...'. */
9185 cp_lexer_consume_token (parser->lexer);
9187 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9188 can be expanded but members cannot. */
9189 if (mem_initializer != error_mark_node
9190 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9192 error ("%Hcannot expand initializer for member %<%D%>",
9193 &token->location, TREE_PURPOSE (mem_initializer));
9194 mem_initializer = error_mark_node;
9197 /* Construct the pack expansion type. */
9198 if (mem_initializer != error_mark_node)
9199 mem_initializer = make_pack_expansion (mem_initializer);
9201 /* Add it to the list, unless it was erroneous. */
9202 if (mem_initializer != error_mark_node)
9204 TREE_CHAIN (mem_initializer) = mem_initializer_list;
9205 mem_initializer_list = mem_initializer;
9207 /* If the next token is not a `,', we're done. */
9208 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9210 /* Consume the `,' token. */
9211 cp_lexer_consume_token (parser->lexer);
9214 /* Perform semantic analysis. */
9215 if (DECL_CONSTRUCTOR_P (current_function_decl))
9216 finish_mem_initializers (mem_initializer_list);
9219 /* Parse a mem-initializer.
9222 mem-initializer-id ( expression-list [opt] )
9223 mem-initializer-id braced-init-list
9228 ( expression-list [opt] )
9230 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
9231 class) or FIELD_DECL (for a non-static data member) to initialize;
9232 the TREE_VALUE is the expression-list. An empty initialization
9233 list is represented by void_list_node. */
9236 cp_parser_mem_initializer (cp_parser* parser)
9238 tree mem_initializer_id;
9239 tree expression_list;
9241 cp_token *token = cp_lexer_peek_token (parser->lexer);
9243 /* Find out what is being initialized. */
9244 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9246 permerror (token->location,
9247 "anachronistic old-style base class initializer");
9248 mem_initializer_id = NULL_TREE;
9252 mem_initializer_id = cp_parser_mem_initializer_id (parser);
9253 if (mem_initializer_id == error_mark_node)
9254 return mem_initializer_id;
9256 member = expand_member_init (mem_initializer_id);
9257 if (member && !DECL_P (member))
9258 in_base_initializer = 1;
9260 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9262 bool expr_non_constant_p;
9263 maybe_warn_cpp0x ("extended initializer lists");
9264 expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9265 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9266 expression_list = build_tree_list (NULL_TREE, expression_list);
9271 vec = cp_parser_parenthesized_expression_list (parser, false,
9273 /*allow_expansion_p=*/true,
9274 /*non_constant_p=*/NULL);
9276 return error_mark_node;
9277 expression_list = build_tree_list_vec (vec);
9278 release_tree_vector (vec);
9281 if (expression_list == error_mark_node)
9282 return error_mark_node;
9283 if (!expression_list)
9284 expression_list = void_type_node;
9286 in_base_initializer = 0;
9288 return member ? build_tree_list (member, expression_list) : error_mark_node;
9291 /* Parse a mem-initializer-id.
9294 :: [opt] nested-name-specifier [opt] class-name
9297 Returns a TYPE indicating the class to be initializer for the first
9298 production. Returns an IDENTIFIER_NODE indicating the data member
9299 to be initialized for the second production. */
9302 cp_parser_mem_initializer_id (cp_parser* parser)
9304 bool global_scope_p;
9305 bool nested_name_specifier_p;
9306 bool template_p = false;
9309 cp_token *token = cp_lexer_peek_token (parser->lexer);
9311 /* `typename' is not allowed in this context ([temp.res]). */
9312 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9314 error ("%Hkeyword %<typename%> not allowed in this context (a qualified "
9315 "member initializer is implicitly a type)",
9317 cp_lexer_consume_token (parser->lexer);
9319 /* Look for the optional `::' operator. */
9321 = (cp_parser_global_scope_opt (parser,
9322 /*current_scope_valid_p=*/false)
9324 /* Look for the optional nested-name-specifier. The simplest way to
9329 The keyword `typename' is not permitted in a base-specifier or
9330 mem-initializer; in these contexts a qualified name that
9331 depends on a template-parameter is implicitly assumed to be a
9334 is to assume that we have seen the `typename' keyword at this
9336 nested_name_specifier_p
9337 = (cp_parser_nested_name_specifier_opt (parser,
9338 /*typename_keyword_p=*/true,
9339 /*check_dependency_p=*/true,
9341 /*is_declaration=*/true)
9343 if (nested_name_specifier_p)
9344 template_p = cp_parser_optional_template_keyword (parser);
9345 /* If there is a `::' operator or a nested-name-specifier, then we
9346 are definitely looking for a class-name. */
9347 if (global_scope_p || nested_name_specifier_p)
9348 return cp_parser_class_name (parser,
9349 /*typename_keyword_p=*/true,
9350 /*template_keyword_p=*/template_p,
9352 /*check_dependency_p=*/true,
9353 /*class_head_p=*/false,
9354 /*is_declaration=*/true);
9355 /* Otherwise, we could also be looking for an ordinary identifier. */
9356 cp_parser_parse_tentatively (parser);
9357 /* Try a class-name. */
9358 id = cp_parser_class_name (parser,
9359 /*typename_keyword_p=*/true,
9360 /*template_keyword_p=*/false,
9362 /*check_dependency_p=*/true,
9363 /*class_head_p=*/false,
9364 /*is_declaration=*/true);
9365 /* If we found one, we're done. */
9366 if (cp_parser_parse_definitely (parser))
9368 /* Otherwise, look for an ordinary identifier. */
9369 return cp_parser_identifier (parser);
9372 /* Overloading [gram.over] */
9374 /* Parse an operator-function-id.
9376 operator-function-id:
9379 Returns an IDENTIFIER_NODE for the operator which is a
9380 human-readable spelling of the identifier, e.g., `operator +'. */
9383 cp_parser_operator_function_id (cp_parser* parser)
9385 /* Look for the `operator' keyword. */
9386 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9387 return error_mark_node;
9388 /* And then the name of the operator itself. */
9389 return cp_parser_operator (parser);
9392 /* Parse an operator.
9395 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
9396 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
9397 || ++ -- , ->* -> () []
9404 Returns an IDENTIFIER_NODE for the operator which is a
9405 human-readable spelling of the identifier, e.g., `operator +'. */
9408 cp_parser_operator (cp_parser* parser)
9410 tree id = NULL_TREE;
9413 /* Peek at the next token. */
9414 token = cp_lexer_peek_token (parser->lexer);
9415 /* Figure out which operator we have. */
9416 switch (token->type)
9422 /* The keyword should be either `new' or `delete'. */
9423 if (token->keyword == RID_NEW)
9425 else if (token->keyword == RID_DELETE)
9430 /* Consume the `new' or `delete' token. */
9431 cp_lexer_consume_token (parser->lexer);
9433 /* Peek at the next token. */
9434 token = cp_lexer_peek_token (parser->lexer);
9435 /* If it's a `[' token then this is the array variant of the
9437 if (token->type == CPP_OPEN_SQUARE)
9439 /* Consume the `[' token. */
9440 cp_lexer_consume_token (parser->lexer);
9441 /* Look for the `]' token. */
9442 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9443 id = ansi_opname (op == NEW_EXPR
9444 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9446 /* Otherwise, we have the non-array variant. */
9448 id = ansi_opname (op);
9454 id = ansi_opname (PLUS_EXPR);
9458 id = ansi_opname (MINUS_EXPR);
9462 id = ansi_opname (MULT_EXPR);
9466 id = ansi_opname (TRUNC_DIV_EXPR);
9470 id = ansi_opname (TRUNC_MOD_EXPR);
9474 id = ansi_opname (BIT_XOR_EXPR);
9478 id = ansi_opname (BIT_AND_EXPR);
9482 id = ansi_opname (BIT_IOR_EXPR);
9486 id = ansi_opname (BIT_NOT_EXPR);
9490 id = ansi_opname (TRUTH_NOT_EXPR);
9494 id = ansi_assopname (NOP_EXPR);
9498 id = ansi_opname (LT_EXPR);
9502 id = ansi_opname (GT_EXPR);
9506 id = ansi_assopname (PLUS_EXPR);
9510 id = ansi_assopname (MINUS_EXPR);
9514 id = ansi_assopname (MULT_EXPR);
9518 id = ansi_assopname (TRUNC_DIV_EXPR);
9522 id = ansi_assopname (TRUNC_MOD_EXPR);
9526 id = ansi_assopname (BIT_XOR_EXPR);
9530 id = ansi_assopname (BIT_AND_EXPR);
9534 id = ansi_assopname (BIT_IOR_EXPR);
9538 id = ansi_opname (LSHIFT_EXPR);
9542 id = ansi_opname (RSHIFT_EXPR);
9546 id = ansi_assopname (LSHIFT_EXPR);
9550 id = ansi_assopname (RSHIFT_EXPR);
9554 id = ansi_opname (EQ_EXPR);
9558 id = ansi_opname (NE_EXPR);
9562 id = ansi_opname (LE_EXPR);
9565 case CPP_GREATER_EQ:
9566 id = ansi_opname (GE_EXPR);
9570 id = ansi_opname (TRUTH_ANDIF_EXPR);
9574 id = ansi_opname (TRUTH_ORIF_EXPR);
9578 id = ansi_opname (POSTINCREMENT_EXPR);
9581 case CPP_MINUS_MINUS:
9582 id = ansi_opname (PREDECREMENT_EXPR);
9586 id = ansi_opname (COMPOUND_EXPR);
9589 case CPP_DEREF_STAR:
9590 id = ansi_opname (MEMBER_REF);
9594 id = ansi_opname (COMPONENT_REF);
9597 case CPP_OPEN_PAREN:
9598 /* Consume the `('. */
9599 cp_lexer_consume_token (parser->lexer);
9600 /* Look for the matching `)'. */
9601 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
9602 return ansi_opname (CALL_EXPR);
9604 case CPP_OPEN_SQUARE:
9605 /* Consume the `['. */
9606 cp_lexer_consume_token (parser->lexer);
9607 /* Look for the matching `]'. */
9608 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
9609 return ansi_opname (ARRAY_REF);
9612 /* Anything else is an error. */
9616 /* If we have selected an identifier, we need to consume the
9619 cp_lexer_consume_token (parser->lexer);
9620 /* Otherwise, no valid operator name was present. */
9623 cp_parser_error (parser, "expected operator");
9624 id = error_mark_node;
9630 /* Parse a template-declaration.
9632 template-declaration:
9633 export [opt] template < template-parameter-list > declaration
9635 If MEMBER_P is TRUE, this template-declaration occurs within a
9638 The grammar rule given by the standard isn't correct. What
9641 template-declaration:
9642 export [opt] template-parameter-list-seq
9643 decl-specifier-seq [opt] init-declarator [opt] ;
9644 export [opt] template-parameter-list-seq
9647 template-parameter-list-seq:
9648 template-parameter-list-seq [opt]
9649 template < template-parameter-list > */
9652 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9654 /* Check for `export'. */
9655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9657 /* Consume the `export' token. */
9658 cp_lexer_consume_token (parser->lexer);
9659 /* Warn that we do not support `export'. */
9660 warning (0, "keyword %<export%> not implemented, and will be ignored");
9663 cp_parser_template_declaration_after_export (parser, member_p);
9666 /* Parse a template-parameter-list.
9668 template-parameter-list:
9670 template-parameter-list , template-parameter
9672 Returns a TREE_LIST. Each node represents a template parameter.
9673 The nodes are connected via their TREE_CHAINs. */
9676 cp_parser_template_parameter_list (cp_parser* parser)
9678 tree parameter_list = NULL_TREE;
9680 begin_template_parm_list ();
9685 bool is_parameter_pack;
9686 location_t parm_loc;
9688 /* Parse the template-parameter. */
9689 parm_loc = cp_lexer_peek_token (parser->lexer)->location;
9690 parameter = cp_parser_template_parameter (parser,
9692 &is_parameter_pack);
9693 /* Add it to the list. */
9694 if (parameter != error_mark_node)
9695 parameter_list = process_template_parm (parameter_list,
9702 tree err_parm = build_tree_list (parameter, parameter);
9703 TREE_VALUE (err_parm) = error_mark_node;
9704 parameter_list = chainon (parameter_list, err_parm);
9707 /* If the next token is not a `,', we're done. */
9708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9710 /* Otherwise, consume the `,' token. */
9711 cp_lexer_consume_token (parser->lexer);
9714 return end_template_parm_list (parameter_list);
9717 /* Parse a template-parameter.
9721 parameter-declaration
9723 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9724 the parameter. The TREE_PURPOSE is the default value, if any.
9725 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9726 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9727 set to true iff this parameter is a parameter pack. */
9730 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9731 bool *is_parameter_pack)
9734 cp_parameter_declarator *parameter_declarator;
9735 cp_declarator *id_declarator;
9738 /* Assume it is a type parameter or a template parameter. */
9739 *is_non_type = false;
9740 /* Assume it not a parameter pack. */
9741 *is_parameter_pack = false;
9742 /* Peek at the next token. */
9743 token = cp_lexer_peek_token (parser->lexer);
9744 /* If it is `class' or `template', we have a type-parameter. */
9745 if (token->keyword == RID_TEMPLATE)
9746 return cp_parser_type_parameter (parser, is_parameter_pack);
9747 /* If it is `class' or `typename' we do not know yet whether it is a
9748 type parameter or a non-type parameter. Consider:
9750 template <typename T, typename T::X X> ...
9754 template <class C, class D*> ...
9756 Here, the first parameter is a type parameter, and the second is
9757 a non-type parameter. We can tell by looking at the token after
9758 the identifier -- if it is a `,', `=', or `>' then we have a type
9760 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9762 /* Peek at the token after `class' or `typename'. */
9763 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9764 /* If it's an ellipsis, we have a template type parameter
9766 if (token->type == CPP_ELLIPSIS)
9767 return cp_parser_type_parameter (parser, is_parameter_pack);
9768 /* If it's an identifier, skip it. */
9769 if (token->type == CPP_NAME)
9770 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9771 /* Now, see if the token looks like the end of a template
9773 if (token->type == CPP_COMMA
9774 || token->type == CPP_EQ
9775 || token->type == CPP_GREATER)
9776 return cp_parser_type_parameter (parser, is_parameter_pack);
9779 /* Otherwise, it is a non-type parameter.
9783 When parsing a default template-argument for a non-type
9784 template-parameter, the first non-nested `>' is taken as the end
9785 of the template parameter-list rather than a greater-than
9787 *is_non_type = true;
9788 parameter_declarator
9789 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9790 /*parenthesized_p=*/NULL);
9792 /* If the parameter declaration is marked as a parameter pack, set
9793 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9794 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9796 if (parameter_declarator
9797 && parameter_declarator->declarator
9798 && parameter_declarator->declarator->parameter_pack_p)
9800 *is_parameter_pack = true;
9801 parameter_declarator->declarator->parameter_pack_p = false;
9804 /* If the next token is an ellipsis, and we don't already have it
9805 marked as a parameter pack, then we have a parameter pack (that
9806 has no declarator). */
9807 if (!*is_parameter_pack
9808 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9809 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9811 /* Consume the `...'. */
9812 cp_lexer_consume_token (parser->lexer);
9813 maybe_warn_variadic_templates ();
9815 *is_parameter_pack = true;
9817 /* We might end up with a pack expansion as the type of the non-type
9818 template parameter, in which case this is a non-type template
9820 else if (parameter_declarator
9821 && parameter_declarator->decl_specifiers.type
9822 && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
9824 *is_parameter_pack = true;
9825 parameter_declarator->decl_specifiers.type =
9826 PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
9829 if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9831 /* Parameter packs cannot have default arguments. However, a
9832 user may try to do so, so we'll parse them and give an
9833 appropriate diagnostic here. */
9835 /* Consume the `='. */
9836 cp_token *start_token = cp_lexer_peek_token (parser->lexer);
9837 cp_lexer_consume_token (parser->lexer);
9839 /* Find the name of the parameter pack. */
9840 id_declarator = parameter_declarator->declarator;
9841 while (id_declarator && id_declarator->kind != cdk_id)
9842 id_declarator = id_declarator->declarator;
9844 if (id_declarator && id_declarator->kind == cdk_id)
9845 error ("%Htemplate parameter pack %qD cannot have a default argument",
9846 &start_token->location, id_declarator->u.id.unqualified_name);
9848 error ("%Htemplate parameter pack cannot have a default argument",
9849 &start_token->location);
9851 /* Parse the default argument, but throw away the result. */
9852 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9855 parm = grokdeclarator (parameter_declarator->declarator,
9856 ¶meter_declarator->decl_specifiers,
9857 PARM, /*initialized=*/0,
9859 if (parm == error_mark_node)
9860 return error_mark_node;
9862 return build_tree_list (parameter_declarator->default_argument, parm);
9865 /* Parse a type-parameter.
9868 class identifier [opt]
9869 class identifier [opt] = type-id
9870 typename identifier [opt]
9871 typename identifier [opt] = type-id
9872 template < template-parameter-list > class identifier [opt]
9873 template < template-parameter-list > class identifier [opt]
9876 GNU Extension (variadic templates):
9879 class ... identifier [opt]
9880 typename ... identifier [opt]
9882 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9883 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9884 the declaration of the parameter.
9886 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9889 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9894 /* Look for a keyword to tell us what kind of parameter this is. */
9895 token = cp_parser_require (parser, CPP_KEYWORD,
9896 "%<class%>, %<typename%>, or %<template%>");
9898 return error_mark_node;
9900 switch (token->keyword)
9906 tree default_argument;
9908 /* If the next token is an ellipsis, we have a template
9910 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9912 /* Consume the `...' token. */
9913 cp_lexer_consume_token (parser->lexer);
9914 maybe_warn_variadic_templates ();
9916 *is_parameter_pack = true;
9919 /* If the next token is an identifier, then it names the
9921 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9922 identifier = cp_parser_identifier (parser);
9924 identifier = NULL_TREE;
9926 /* Create the parameter. */
9927 parameter = finish_template_type_parm (class_type_node, identifier);
9929 /* If the next token is an `=', we have a default argument. */
9930 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9932 /* Consume the `=' token. */
9933 cp_lexer_consume_token (parser->lexer);
9934 /* Parse the default-argument. */
9935 push_deferring_access_checks (dk_no_deferred);
9936 default_argument = cp_parser_type_id (parser);
9938 /* Template parameter packs cannot have default
9940 if (*is_parameter_pack)
9943 error ("%Htemplate parameter pack %qD cannot have a "
9944 "default argument", &token->location, identifier);
9946 error ("%Htemplate parameter packs cannot have "
9947 "default arguments", &token->location);
9948 default_argument = NULL_TREE;
9950 pop_deferring_access_checks ();
9953 default_argument = NULL_TREE;
9955 /* Create the combined representation of the parameter and the
9956 default argument. */
9957 parameter = build_tree_list (default_argument, parameter);
9963 tree parameter_list;
9965 tree default_argument;
9967 /* Look for the `<'. */
9968 cp_parser_require (parser, CPP_LESS, "%<<%>");
9969 /* Parse the template-parameter-list. */
9970 parameter_list = cp_parser_template_parameter_list (parser);
9971 /* Look for the `>'. */
9972 cp_parser_require (parser, CPP_GREATER, "%<>%>");
9973 /* Look for the `class' keyword. */
9974 cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
9975 /* If the next token is an ellipsis, we have a template
9977 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9979 /* Consume the `...' token. */
9980 cp_lexer_consume_token (parser->lexer);
9981 maybe_warn_variadic_templates ();
9983 *is_parameter_pack = true;
9985 /* If the next token is an `=', then there is a
9986 default-argument. If the next token is a `>', we are at
9987 the end of the parameter-list. If the next token is a `,',
9988 then we are at the end of this parameter. */
9989 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9990 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9991 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9993 identifier = cp_parser_identifier (parser);
9994 /* Treat invalid names as if the parameter were nameless. */
9995 if (identifier == error_mark_node)
9996 identifier = NULL_TREE;
9999 identifier = NULL_TREE;
10001 /* Create the template parameter. */
10002 parameter = finish_template_template_parm (class_type_node,
10005 /* If the next token is an `=', then there is a
10006 default-argument. */
10007 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10011 /* Consume the `='. */
10012 cp_lexer_consume_token (parser->lexer);
10013 /* Parse the id-expression. */
10014 push_deferring_access_checks (dk_no_deferred);
10015 /* save token before parsing the id-expression, for error
10017 token = cp_lexer_peek_token (parser->lexer);
10019 = cp_parser_id_expression (parser,
10020 /*template_keyword_p=*/false,
10021 /*check_dependency_p=*/true,
10022 /*template_p=*/&is_template,
10023 /*declarator_p=*/false,
10024 /*optional_p=*/false);
10025 if (TREE_CODE (default_argument) == TYPE_DECL)
10026 /* If the id-expression was a template-id that refers to
10027 a template-class, we already have the declaration here,
10028 so no further lookup is needed. */
10031 /* Look up the name. */
10033 = cp_parser_lookup_name (parser, default_argument,
10035 /*is_template=*/is_template,
10036 /*is_namespace=*/false,
10037 /*check_dependency=*/true,
10038 /*ambiguous_decls=*/NULL,
10040 /* See if the default argument is valid. */
10042 = check_template_template_default_arg (default_argument);
10044 /* Template parameter packs cannot have default
10046 if (*is_parameter_pack)
10049 error ("%Htemplate parameter pack %qD cannot "
10050 "have a default argument",
10051 &token->location, identifier);
10053 error ("%Htemplate parameter packs cannot "
10054 "have default arguments",
10056 default_argument = NULL_TREE;
10058 pop_deferring_access_checks ();
10061 default_argument = NULL_TREE;
10063 /* Create the combined representation of the parameter and the
10064 default argument. */
10065 parameter = build_tree_list (default_argument, parameter);
10070 gcc_unreachable ();
10077 /* Parse a template-id.
10080 template-name < template-argument-list [opt] >
10082 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10083 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
10084 returned. Otherwise, if the template-name names a function, or set
10085 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
10086 names a class, returns a TYPE_DECL for the specialization.
10088 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10089 uninstantiated templates. */
10092 cp_parser_template_id (cp_parser *parser,
10093 bool template_keyword_p,
10094 bool check_dependency_p,
10095 bool is_declaration)
10101 cp_token_position start_of_id = 0;
10102 deferred_access_check *chk;
10103 VEC (deferred_access_check,gc) *access_check;
10104 cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10105 bool is_identifier;
10107 /* If the next token corresponds to a template-id, there is no need
10109 next_token = cp_lexer_peek_token (parser->lexer);
10110 if (next_token->type == CPP_TEMPLATE_ID)
10112 struct tree_check *check_value;
10114 /* Get the stored value. */
10115 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10116 /* Perform any access checks that were deferred. */
10117 access_check = check_value->checks;
10121 VEC_iterate (deferred_access_check, access_check, i, chk) ;
10124 perform_or_defer_access_check (chk->binfo,
10129 /* Return the stored value. */
10130 return check_value->value;
10133 /* Avoid performing name lookup if there is no possibility of
10134 finding a template-id. */
10135 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10136 || (next_token->type == CPP_NAME
10137 && !cp_parser_nth_token_starts_template_argument_list_p
10140 cp_parser_error (parser, "expected template-id");
10141 return error_mark_node;
10144 /* Remember where the template-id starts. */
10145 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10146 start_of_id = cp_lexer_token_position (parser->lexer, false);
10148 push_deferring_access_checks (dk_deferred);
10150 /* Parse the template-name. */
10151 is_identifier = false;
10152 token = cp_lexer_peek_token (parser->lexer);
10153 templ = cp_parser_template_name (parser, template_keyword_p,
10154 check_dependency_p,
10157 if (templ == error_mark_node || is_identifier)
10159 pop_deferring_access_checks ();
10163 /* If we find the sequence `[:' after a template-name, it's probably
10164 a digraph-typo for `< ::'. Substitute the tokens and check if we can
10165 parse correctly the argument list. */
10166 next_token = cp_lexer_peek_token (parser->lexer);
10167 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10168 if (next_token->type == CPP_OPEN_SQUARE
10169 && next_token->flags & DIGRAPH
10170 && next_token_2->type == CPP_COLON
10171 && !(next_token_2->flags & PREV_WHITE))
10173 cp_parser_parse_tentatively (parser);
10174 /* Change `:' into `::'. */
10175 next_token_2->type = CPP_SCOPE;
10176 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10178 cp_lexer_consume_token (parser->lexer);
10180 /* Parse the arguments. */
10181 arguments = cp_parser_enclosed_template_argument_list (parser);
10182 if (!cp_parser_parse_definitely (parser))
10184 /* If we couldn't parse an argument list, then we revert our changes
10185 and return simply an error. Maybe this is not a template-id
10187 next_token_2->type = CPP_COLON;
10188 cp_parser_error (parser, "expected %<<%>");
10189 pop_deferring_access_checks ();
10190 return error_mark_node;
10192 /* Otherwise, emit an error about the invalid digraph, but continue
10193 parsing because we got our argument list. */
10194 if (permerror (next_token->location,
10195 "%<<::%> cannot begin a template-argument list"))
10197 static bool hint = false;
10198 inform (next_token->location,
10199 "%<<:%> is an alternate spelling for %<[%>."
10200 " Insert whitespace between %<<%> and %<::%>");
10201 if (!hint && !flag_permissive)
10203 inform (next_token->location, "(if you use %<-fpermissive%>"
10204 " G++ will accept your code)");
10211 /* Look for the `<' that starts the template-argument-list. */
10212 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10214 pop_deferring_access_checks ();
10215 return error_mark_node;
10217 /* Parse the arguments. */
10218 arguments = cp_parser_enclosed_template_argument_list (parser);
10221 /* Build a representation of the specialization. */
10222 if (TREE_CODE (templ) == IDENTIFIER_NODE)
10223 template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10224 else if (DECL_CLASS_TEMPLATE_P (templ)
10225 || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10227 bool entering_scope;
10228 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10229 template (rather than some instantiation thereof) only if
10230 is not nested within some other construct. For example, in
10231 "template <typename T> void f(T) { A<T>::", A<T> is just an
10232 instantiation of A. */
10233 entering_scope = (template_parm_scope_p ()
10234 && cp_lexer_next_token_is (parser->lexer,
10237 = finish_template_type (templ, arguments, entering_scope);
10241 /* If it's not a class-template or a template-template, it should be
10242 a function-template. */
10243 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10244 || TREE_CODE (templ) == OVERLOAD
10245 || BASELINK_P (templ)));
10247 template_id = lookup_template_function (templ, arguments);
10250 /* If parsing tentatively, replace the sequence of tokens that makes
10251 up the template-id with a CPP_TEMPLATE_ID token. That way,
10252 should we re-parse the token stream, we will not have to repeat
10253 the effort required to do the parse, nor will we issue duplicate
10254 error messages about problems during instantiation of the
10258 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10260 /* Reset the contents of the START_OF_ID token. */
10261 token->type = CPP_TEMPLATE_ID;
10262 /* Retrieve any deferred checks. Do not pop this access checks yet
10263 so the memory will not be reclaimed during token replacing below. */
10264 token->u.tree_check_value = GGC_CNEW (struct tree_check);
10265 token->u.tree_check_value->value = template_id;
10266 token->u.tree_check_value->checks = get_deferred_access_checks ();
10267 token->keyword = RID_MAX;
10269 /* Purge all subsequent tokens. */
10270 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10272 /* ??? Can we actually assume that, if template_id ==
10273 error_mark_node, we will have issued a diagnostic to the
10274 user, as opposed to simply marking the tentative parse as
10276 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10277 error ("%Hparse error in template argument list",
10281 pop_deferring_access_checks ();
10282 return template_id;
10285 /* Parse a template-name.
10290 The standard should actually say:
10294 operator-function-id
10296 A defect report has been filed about this issue.
10298 A conversion-function-id cannot be a template name because they cannot
10299 be part of a template-id. In fact, looking at this code:
10301 a.operator K<int>()
10303 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10304 It is impossible to call a templated conversion-function-id with an
10305 explicit argument list, since the only allowed template parameter is
10306 the type to which it is converting.
10308 If TEMPLATE_KEYWORD_P is true, then we have just seen the
10309 `template' keyword, in a construction like:
10313 In that case `f' is taken to be a template-name, even though there
10314 is no way of knowing for sure.
10316 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10317 name refers to a set of overloaded functions, at least one of which
10318 is a template, or an IDENTIFIER_NODE with the name of the template,
10319 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
10320 names are looked up inside uninstantiated templates. */
10323 cp_parser_template_name (cp_parser* parser,
10324 bool template_keyword_p,
10325 bool check_dependency_p,
10326 bool is_declaration,
10327 bool *is_identifier)
10332 cp_token *token = cp_lexer_peek_token (parser->lexer);
10334 /* If the next token is `operator', then we have either an
10335 operator-function-id or a conversion-function-id. */
10336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10338 /* We don't know whether we're looking at an
10339 operator-function-id or a conversion-function-id. */
10340 cp_parser_parse_tentatively (parser);
10341 /* Try an operator-function-id. */
10342 identifier = cp_parser_operator_function_id (parser);
10343 /* If that didn't work, try a conversion-function-id. */
10344 if (!cp_parser_parse_definitely (parser))
10346 cp_parser_error (parser, "expected template-name");
10347 return error_mark_node;
10350 /* Look for the identifier. */
10352 identifier = cp_parser_identifier (parser);
10354 /* If we didn't find an identifier, we don't have a template-id. */
10355 if (identifier == error_mark_node)
10356 return error_mark_node;
10358 /* If the name immediately followed the `template' keyword, then it
10359 is a template-name. However, if the next token is not `<', then
10360 we do not treat it as a template-name, since it is not being used
10361 as part of a template-id. This enables us to handle constructs
10364 template <typename T> struct S { S(); };
10365 template <typename T> S<T>::S();
10367 correctly. We would treat `S' as a template -- if it were `S<T>'
10368 -- but we do not if there is no `<'. */
10370 if (processing_template_decl
10371 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10373 /* In a declaration, in a dependent context, we pretend that the
10374 "template" keyword was present in order to improve error
10375 recovery. For example, given:
10377 template <typename T> void f(T::X<int>);
10379 we want to treat "X<int>" as a template-id. */
10381 && !template_keyword_p
10382 && parser->scope && TYPE_P (parser->scope)
10383 && check_dependency_p
10384 && dependent_scope_p (parser->scope)
10385 /* Do not do this for dtors (or ctors), since they never
10386 need the template keyword before their name. */
10387 && !constructor_name_p (identifier, parser->scope))
10389 cp_token_position start = 0;
10391 /* Explain what went wrong. */
10392 error ("%Hnon-template %qD used as template",
10393 &token->location, identifier);
10394 inform (input_location, "use %<%T::template %D%> to indicate that it is a template",
10395 parser->scope, identifier);
10396 /* If parsing tentatively, find the location of the "<" token. */
10397 if (cp_parser_simulate_error (parser))
10398 start = cp_lexer_token_position (parser->lexer, true);
10399 /* Parse the template arguments so that we can issue error
10400 messages about them. */
10401 cp_lexer_consume_token (parser->lexer);
10402 cp_parser_enclosed_template_argument_list (parser);
10403 /* Skip tokens until we find a good place from which to
10404 continue parsing. */
10405 cp_parser_skip_to_closing_parenthesis (parser,
10406 /*recovering=*/true,
10408 /*consume_paren=*/false);
10409 /* If parsing tentatively, permanently remove the
10410 template argument list. That will prevent duplicate
10411 error messages from being issued about the missing
10412 "template" keyword. */
10414 cp_lexer_purge_tokens_after (parser->lexer, start);
10416 *is_identifier = true;
10420 /* If the "template" keyword is present, then there is generally
10421 no point in doing name-lookup, so we just return IDENTIFIER.
10422 But, if the qualifying scope is non-dependent then we can
10423 (and must) do name-lookup normally. */
10424 if (template_keyword_p
10426 || (TYPE_P (parser->scope)
10427 && dependent_type_p (parser->scope))))
10431 /* Look up the name. */
10432 decl = cp_parser_lookup_name (parser, identifier,
10434 /*is_template=*/false,
10435 /*is_namespace=*/false,
10436 check_dependency_p,
10437 /*ambiguous_decls=*/NULL,
10439 decl = maybe_get_template_decl_from_type_decl (decl);
10441 /* If DECL is a template, then the name was a template-name. */
10442 if (TREE_CODE (decl) == TEMPLATE_DECL)
10446 tree fn = NULL_TREE;
10448 /* The standard does not explicitly indicate whether a name that
10449 names a set of overloaded declarations, some of which are
10450 templates, is a template-name. However, such a name should
10451 be a template-name; otherwise, there is no way to form a
10452 template-id for the overloaded templates. */
10453 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10454 if (TREE_CODE (fns) == OVERLOAD)
10455 for (fn = fns; fn; fn = OVL_NEXT (fn))
10456 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10461 /* The name does not name a template. */
10462 cp_parser_error (parser, "expected template-name");
10463 return error_mark_node;
10467 /* If DECL is dependent, and refers to a function, then just return
10468 its name; we will look it up again during template instantiation. */
10469 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10471 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10472 if (TYPE_P (scope) && dependent_type_p (scope))
10479 /* Parse a template-argument-list.
10481 template-argument-list:
10482 template-argument ... [opt]
10483 template-argument-list , template-argument ... [opt]
10485 Returns a TREE_VEC containing the arguments. */
10488 cp_parser_template_argument_list (cp_parser* parser)
10490 tree fixed_args[10];
10491 unsigned n_args = 0;
10492 unsigned alloced = 10;
10493 tree *arg_ary = fixed_args;
10495 bool saved_in_template_argument_list_p;
10497 bool saved_non_ice_p;
10499 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10500 parser->in_template_argument_list_p = true;
10501 /* Even if the template-id appears in an integral
10502 constant-expression, the contents of the argument list do
10504 saved_ice_p = parser->integral_constant_expression_p;
10505 parser->integral_constant_expression_p = false;
10506 saved_non_ice_p = parser->non_integral_constant_expression_p;
10507 parser->non_integral_constant_expression_p = false;
10508 /* Parse the arguments. */
10514 /* Consume the comma. */
10515 cp_lexer_consume_token (parser->lexer);
10517 /* Parse the template-argument. */
10518 argument = cp_parser_template_argument (parser);
10520 /* If the next token is an ellipsis, we're expanding a template
10522 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10524 if (argument == error_mark_node)
10526 cp_token *token = cp_lexer_peek_token (parser->lexer);
10527 error ("%Hexpected parameter pack before %<...%>",
10530 /* Consume the `...' token. */
10531 cp_lexer_consume_token (parser->lexer);
10533 /* Make the argument into a TYPE_PACK_EXPANSION or
10534 EXPR_PACK_EXPANSION. */
10535 argument = make_pack_expansion (argument);
10538 if (n_args == alloced)
10542 if (arg_ary == fixed_args)
10544 arg_ary = XNEWVEC (tree, alloced);
10545 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10548 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10550 arg_ary[n_args++] = argument;
10552 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10554 vec = make_tree_vec (n_args);
10557 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10559 if (arg_ary != fixed_args)
10561 parser->non_integral_constant_expression_p = saved_non_ice_p;
10562 parser->integral_constant_expression_p = saved_ice_p;
10563 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10567 /* Parse a template-argument.
10570 assignment-expression
10574 The representation is that of an assignment-expression, type-id, or
10575 id-expression -- except that the qualified id-expression is
10576 evaluated, so that the value returned is either a DECL or an
10579 Although the standard says "assignment-expression", it forbids
10580 throw-expressions or assignments in the template argument.
10581 Therefore, we use "conditional-expression" instead. */
10584 cp_parser_template_argument (cp_parser* parser)
10589 bool maybe_type_id = false;
10590 cp_token *token = NULL, *argument_start_token = NULL;
10593 /* There's really no way to know what we're looking at, so we just
10594 try each alternative in order.
10598 In a template-argument, an ambiguity between a type-id and an
10599 expression is resolved to a type-id, regardless of the form of
10600 the corresponding template-parameter.
10602 Therefore, we try a type-id first. */
10603 cp_parser_parse_tentatively (parser);
10604 argument = cp_parser_template_type_arg (parser);
10605 /* If there was no error parsing the type-id but the next token is a
10606 '>>', our behavior depends on which dialect of C++ we're
10607 parsing. In C++98, we probably found a typo for '> >'. But there
10608 are type-id which are also valid expressions. For instance:
10610 struct X { int operator >> (int); };
10611 template <int V> struct Foo {};
10614 Here 'X()' is a valid type-id of a function type, but the user just
10615 wanted to write the expression "X() >> 5". Thus, we remember that we
10616 found a valid type-id, but we still try to parse the argument as an
10617 expression to see what happens.
10619 In C++0x, the '>>' will be considered two separate '>'
10621 if (!cp_parser_error_occurred (parser)
10622 && cxx_dialect == cxx98
10623 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10625 maybe_type_id = true;
10626 cp_parser_abort_tentative_parse (parser);
10630 /* If the next token isn't a `,' or a `>', then this argument wasn't
10631 really finished. This means that the argument is not a valid
10633 if (!cp_parser_next_token_ends_template_argument_p (parser))
10634 cp_parser_error (parser, "expected template-argument");
10635 /* If that worked, we're done. */
10636 if (cp_parser_parse_definitely (parser))
10639 /* We're still not sure what the argument will be. */
10640 cp_parser_parse_tentatively (parser);
10641 /* Try a template. */
10642 argument_start_token = cp_lexer_peek_token (parser->lexer);
10643 argument = cp_parser_id_expression (parser,
10644 /*template_keyword_p=*/false,
10645 /*check_dependency_p=*/true,
10647 /*declarator_p=*/false,
10648 /*optional_p=*/false);
10649 /* If the next token isn't a `,' or a `>', then this argument wasn't
10650 really finished. */
10651 if (!cp_parser_next_token_ends_template_argument_p (parser))
10652 cp_parser_error (parser, "expected template-argument");
10653 if (!cp_parser_error_occurred (parser))
10655 /* Figure out what is being referred to. If the id-expression
10656 was for a class template specialization, then we will have a
10657 TYPE_DECL at this point. There is no need to do name lookup
10658 at this point in that case. */
10659 if (TREE_CODE (argument) != TYPE_DECL)
10660 argument = cp_parser_lookup_name (parser, argument,
10662 /*is_template=*/template_p,
10663 /*is_namespace=*/false,
10664 /*check_dependency=*/true,
10665 /*ambiguous_decls=*/NULL,
10666 argument_start_token->location);
10667 if (TREE_CODE (argument) != TEMPLATE_DECL
10668 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10669 cp_parser_error (parser, "expected template-name");
10671 if (cp_parser_parse_definitely (parser))
10673 /* It must be a non-type argument. There permitted cases are given
10674 in [temp.arg.nontype]:
10676 -- an integral constant-expression of integral or enumeration
10679 -- the name of a non-type template-parameter; or
10681 -- the name of an object or function with external linkage...
10683 -- the address of an object or function with external linkage...
10685 -- a pointer to member... */
10686 /* Look for a non-type template parameter. */
10687 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10689 cp_parser_parse_tentatively (parser);
10690 argument = cp_parser_primary_expression (parser,
10691 /*address_p=*/false,
10693 /*template_arg_p=*/true,
10695 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10696 || !cp_parser_next_token_ends_template_argument_p (parser))
10697 cp_parser_simulate_error (parser);
10698 if (cp_parser_parse_definitely (parser))
10702 /* If the next token is "&", the argument must be the address of an
10703 object or function with external linkage. */
10704 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10706 cp_lexer_consume_token (parser->lexer);
10707 /* See if we might have an id-expression. */
10708 token = cp_lexer_peek_token (parser->lexer);
10709 if (token->type == CPP_NAME
10710 || token->keyword == RID_OPERATOR
10711 || token->type == CPP_SCOPE
10712 || token->type == CPP_TEMPLATE_ID
10713 || token->type == CPP_NESTED_NAME_SPECIFIER)
10715 cp_parser_parse_tentatively (parser);
10716 argument = cp_parser_primary_expression (parser,
10719 /*template_arg_p=*/true,
10721 if (cp_parser_error_occurred (parser)
10722 || !cp_parser_next_token_ends_template_argument_p (parser))
10723 cp_parser_abort_tentative_parse (parser);
10726 if (TREE_CODE (argument) == INDIRECT_REF)
10728 gcc_assert (REFERENCE_REF_P (argument));
10729 argument = TREE_OPERAND (argument, 0);
10732 if (TREE_CODE (argument) == VAR_DECL)
10734 /* A variable without external linkage might still be a
10735 valid constant-expression, so no error is issued here
10736 if the external-linkage check fails. */
10737 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10738 cp_parser_simulate_error (parser);
10740 else if (is_overloaded_fn (argument))
10741 /* All overloaded functions are allowed; if the external
10742 linkage test does not pass, an error will be issued
10746 && (TREE_CODE (argument) == OFFSET_REF
10747 || TREE_CODE (argument) == SCOPE_REF))
10748 /* A pointer-to-member. */
10750 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10753 cp_parser_simulate_error (parser);
10755 if (cp_parser_parse_definitely (parser))
10758 argument = build_x_unary_op (ADDR_EXPR, argument,
10759 tf_warning_or_error);
10764 /* If the argument started with "&", there are no other valid
10765 alternatives at this point. */
10768 cp_parser_error (parser, "invalid non-type template argument");
10769 return error_mark_node;
10772 /* If the argument wasn't successfully parsed as a type-id followed
10773 by '>>', the argument can only be a constant expression now.
10774 Otherwise, we try parsing the constant-expression tentatively,
10775 because the argument could really be a type-id. */
10777 cp_parser_parse_tentatively (parser);
10778 argument = cp_parser_constant_expression (parser,
10779 /*allow_non_constant_p=*/false,
10780 /*non_constant_p=*/NULL);
10781 argument = fold_non_dependent_expr (argument);
10782 if (!maybe_type_id)
10784 if (!cp_parser_next_token_ends_template_argument_p (parser))
10785 cp_parser_error (parser, "expected template-argument");
10786 if (cp_parser_parse_definitely (parser))
10788 /* We did our best to parse the argument as a non type-id, but that
10789 was the only alternative that matched (albeit with a '>' after
10790 it). We can assume it's just a typo from the user, and a
10791 diagnostic will then be issued. */
10792 return cp_parser_template_type_arg (parser);
10795 /* Parse an explicit-instantiation.
10797 explicit-instantiation:
10798 template declaration
10800 Although the standard says `declaration', what it really means is:
10802 explicit-instantiation:
10803 template decl-specifier-seq [opt] declarator [opt] ;
10805 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10806 supposed to be allowed. A defect report has been filed about this
10811 explicit-instantiation:
10812 storage-class-specifier template
10813 decl-specifier-seq [opt] declarator [opt] ;
10814 function-specifier template
10815 decl-specifier-seq [opt] declarator [opt] ; */
10818 cp_parser_explicit_instantiation (cp_parser* parser)
10820 int declares_class_or_enum;
10821 cp_decl_specifier_seq decl_specifiers;
10822 tree extension_specifier = NULL_TREE;
10825 /* Look for an (optional) storage-class-specifier or
10826 function-specifier. */
10827 if (cp_parser_allow_gnu_extensions_p (parser))
10829 extension_specifier
10830 = cp_parser_storage_class_specifier_opt (parser);
10831 if (!extension_specifier)
10832 extension_specifier
10833 = cp_parser_function_specifier_opt (parser,
10834 /*decl_specs=*/NULL);
10837 /* Look for the `template' keyword. */
10838 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10839 /* Let the front end know that we are processing an explicit
10841 begin_explicit_instantiation ();
10842 /* [temp.explicit] says that we are supposed to ignore access
10843 control while processing explicit instantiation directives. */
10844 push_deferring_access_checks (dk_no_check);
10845 /* Parse a decl-specifier-seq. */
10846 token = cp_lexer_peek_token (parser->lexer);
10847 cp_parser_decl_specifier_seq (parser,
10848 CP_PARSER_FLAGS_OPTIONAL,
10850 &declares_class_or_enum);
10851 /* If there was exactly one decl-specifier, and it declared a class,
10852 and there's no declarator, then we have an explicit type
10854 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10858 type = check_tag_decl (&decl_specifiers);
10859 /* Turn access control back on for names used during
10860 template instantiation. */
10861 pop_deferring_access_checks ();
10863 do_type_instantiation (type, extension_specifier,
10864 /*complain=*/tf_error);
10868 cp_declarator *declarator;
10871 /* Parse the declarator. */
10873 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10874 /*ctor_dtor_or_conv_p=*/NULL,
10875 /*parenthesized_p=*/NULL,
10876 /*member_p=*/false);
10877 if (declares_class_or_enum & 2)
10878 cp_parser_check_for_definition_in_return_type (declarator,
10879 decl_specifiers.type,
10880 decl_specifiers.type_location);
10881 if (declarator != cp_error_declarator)
10883 decl = grokdeclarator (declarator, &decl_specifiers,
10884 NORMAL, 0, &decl_specifiers.attributes);
10885 /* Turn access control back on for names used during
10886 template instantiation. */
10887 pop_deferring_access_checks ();
10888 /* Do the explicit instantiation. */
10889 do_decl_instantiation (decl, extension_specifier);
10893 pop_deferring_access_checks ();
10894 /* Skip the body of the explicit instantiation. */
10895 cp_parser_skip_to_end_of_statement (parser);
10898 /* We're done with the instantiation. */
10899 end_explicit_instantiation ();
10901 cp_parser_consume_semicolon_at_end_of_statement (parser);
10904 /* Parse an explicit-specialization.
10906 explicit-specialization:
10907 template < > declaration
10909 Although the standard says `declaration', what it really means is:
10911 explicit-specialization:
10912 template <> decl-specifier [opt] init-declarator [opt] ;
10913 template <> function-definition
10914 template <> explicit-specialization
10915 template <> template-declaration */
10918 cp_parser_explicit_specialization (cp_parser* parser)
10920 bool need_lang_pop;
10921 cp_token *token = cp_lexer_peek_token (parser->lexer);
10923 /* Look for the `template' keyword. */
10924 cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
10925 /* Look for the `<'. */
10926 cp_parser_require (parser, CPP_LESS, "%<<%>");
10927 /* Look for the `>'. */
10928 cp_parser_require (parser, CPP_GREATER, "%<>%>");
10929 /* We have processed another parameter list. */
10930 ++parser->num_template_parameter_lists;
10933 A template ... explicit specialization ... shall not have C
10935 if (current_lang_name == lang_name_c)
10937 error ("%Htemplate specialization with C linkage", &token->location);
10938 /* Give it C++ linkage to avoid confusing other parts of the
10940 push_lang_context (lang_name_cplusplus);
10941 need_lang_pop = true;
10944 need_lang_pop = false;
10945 /* Let the front end know that we are beginning a specialization. */
10946 if (!begin_specialization ())
10948 end_specialization ();
10952 /* If the next keyword is `template', we need to figure out whether
10953 or not we're looking a template-declaration. */
10954 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10956 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10957 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10958 cp_parser_template_declaration_after_export (parser,
10959 /*member_p=*/false);
10961 cp_parser_explicit_specialization (parser);
10964 /* Parse the dependent declaration. */
10965 cp_parser_single_declaration (parser,
10967 /*member_p=*/false,
10968 /*explicit_specialization_p=*/true,
10969 /*friend_p=*/NULL);
10970 /* We're done with the specialization. */
10971 end_specialization ();
10972 /* For the erroneous case of a template with C linkage, we pushed an
10973 implicit C++ linkage scope; exit that scope now. */
10975 pop_lang_context ();
10976 /* We're done with this parameter list. */
10977 --parser->num_template_parameter_lists;
10980 /* Parse a type-specifier.
10983 simple-type-specifier
10986 elaborated-type-specifier
10994 Returns a representation of the type-specifier. For a
10995 class-specifier, enum-specifier, or elaborated-type-specifier, a
10996 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10998 The parser flags FLAGS is used to control type-specifier parsing.
11000 If IS_DECLARATION is TRUE, then this type-specifier is appearing
11001 in a decl-specifier-seq.
11003 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11004 class-specifier, enum-specifier, or elaborated-type-specifier, then
11005 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
11006 if a type is declared; 2 if it is defined. Otherwise, it is set to
11009 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11010 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
11011 is set to FALSE. */
11014 cp_parser_type_specifier (cp_parser* parser,
11015 cp_parser_flags flags,
11016 cp_decl_specifier_seq *decl_specs,
11017 bool is_declaration,
11018 int* declares_class_or_enum,
11019 bool* is_cv_qualifier)
11021 tree type_spec = NULL_TREE;
11024 cp_decl_spec ds = ds_last;
11026 /* Assume this type-specifier does not declare a new type. */
11027 if (declares_class_or_enum)
11028 *declares_class_or_enum = 0;
11029 /* And that it does not specify a cv-qualifier. */
11030 if (is_cv_qualifier)
11031 *is_cv_qualifier = false;
11032 /* Peek at the next token. */
11033 token = cp_lexer_peek_token (parser->lexer);
11035 /* If we're looking at a keyword, we can use that to guide the
11036 production we choose. */
11037 keyword = token->keyword;
11041 /* Look for the enum-specifier. */
11042 type_spec = cp_parser_enum_specifier (parser);
11043 /* If that worked, we're done. */
11046 if (declares_class_or_enum)
11047 *declares_class_or_enum = 2;
11049 cp_parser_set_decl_spec_type (decl_specs,
11052 /*user_defined_p=*/true);
11056 goto elaborated_type_specifier;
11058 /* Any of these indicate either a class-specifier, or an
11059 elaborated-type-specifier. */
11063 /* Parse tentatively so that we can back up if we don't find a
11064 class-specifier. */
11065 cp_parser_parse_tentatively (parser);
11066 /* Look for the class-specifier. */
11067 type_spec = cp_parser_class_specifier (parser);
11068 invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11069 /* If that worked, we're done. */
11070 if (cp_parser_parse_definitely (parser))
11072 if (declares_class_or_enum)
11073 *declares_class_or_enum = 2;
11075 cp_parser_set_decl_spec_type (decl_specs,
11078 /*user_defined_p=*/true);
11082 /* Fall through. */
11083 elaborated_type_specifier:
11084 /* We're declaring (not defining) a class or enum. */
11085 if (declares_class_or_enum)
11086 *declares_class_or_enum = 1;
11088 /* Fall through. */
11090 /* Look for an elaborated-type-specifier. */
11092 = (cp_parser_elaborated_type_specifier
11094 decl_specs && decl_specs->specs[(int) ds_friend],
11097 cp_parser_set_decl_spec_type (decl_specs,
11100 /*user_defined_p=*/true);
11105 if (is_cv_qualifier)
11106 *is_cv_qualifier = true;
11111 if (is_cv_qualifier)
11112 *is_cv_qualifier = true;
11117 if (is_cv_qualifier)
11118 *is_cv_qualifier = true;
11122 /* The `__complex__' keyword is a GNU extension. */
11130 /* Handle simple keywords. */
11135 ++decl_specs->specs[(int)ds];
11136 decl_specs->any_specifiers_p = true;
11138 return cp_lexer_consume_token (parser->lexer)->u.value;
11141 /* If we do not already have a type-specifier, assume we are looking
11142 at a simple-type-specifier. */
11143 type_spec = cp_parser_simple_type_specifier (parser,
11147 /* If we didn't find a type-specifier, and a type-specifier was not
11148 optional in this context, issue an error message. */
11149 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11151 cp_parser_error (parser, "expected type specifier");
11152 return error_mark_node;
11158 /* Parse a simple-type-specifier.
11160 simple-type-specifier:
11161 :: [opt] nested-name-specifier [opt] type-name
11162 :: [opt] nested-name-specifier template template-id
11177 simple-type-specifier:
11179 decltype ( expression )
11185 simple-type-specifier:
11186 __typeof__ unary-expression
11187 __typeof__ ( type-id )
11189 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
11190 appropriately updated. */
11193 cp_parser_simple_type_specifier (cp_parser* parser,
11194 cp_decl_specifier_seq *decl_specs,
11195 cp_parser_flags flags)
11197 tree type = NULL_TREE;
11200 /* Peek at the next token. */
11201 token = cp_lexer_peek_token (parser->lexer);
11203 /* If we're looking at a keyword, things are easy. */
11204 switch (token->keyword)
11208 decl_specs->explicit_char_p = true;
11209 type = char_type_node;
11212 type = char16_type_node;
11215 type = char32_type_node;
11218 type = wchar_type_node;
11221 type = boolean_type_node;
11225 ++decl_specs->specs[(int) ds_short];
11226 type = short_integer_type_node;
11230 decl_specs->explicit_int_p = true;
11231 type = integer_type_node;
11235 ++decl_specs->specs[(int) ds_long];
11236 type = long_integer_type_node;
11240 ++decl_specs->specs[(int) ds_signed];
11241 type = integer_type_node;
11245 ++decl_specs->specs[(int) ds_unsigned];
11246 type = unsigned_type_node;
11249 type = float_type_node;
11252 type = double_type_node;
11255 type = void_type_node;
11259 maybe_warn_cpp0x ("C++0x auto");
11260 type = make_auto ();
11264 /* Parse the `decltype' type. */
11265 type = cp_parser_decltype (parser);
11268 cp_parser_set_decl_spec_type (decl_specs, type,
11270 /*user_defined_p=*/true);
11275 /* Consume the `typeof' token. */
11276 cp_lexer_consume_token (parser->lexer);
11277 /* Parse the operand to `typeof'. */
11278 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11279 /* If it is not already a TYPE, take its type. */
11280 if (!TYPE_P (type))
11281 type = finish_typeof (type);
11284 cp_parser_set_decl_spec_type (decl_specs, type,
11286 /*user_defined_p=*/true);
11294 /* If the type-specifier was for a built-in type, we're done. */
11299 /* Record the type. */
11301 && (token->keyword != RID_SIGNED
11302 && token->keyword != RID_UNSIGNED
11303 && token->keyword != RID_SHORT
11304 && token->keyword != RID_LONG))
11305 cp_parser_set_decl_spec_type (decl_specs,
11308 /*user_defined=*/false);
11310 decl_specs->any_specifiers_p = true;
11312 /* Consume the token. */
11313 id = cp_lexer_consume_token (parser->lexer)->u.value;
11315 /* There is no valid C++ program where a non-template type is
11316 followed by a "<". That usually indicates that the user thought
11317 that the type was a template. */
11318 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11320 return TYPE_NAME (type);
11323 /* The type-specifier must be a user-defined type. */
11324 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11329 /* Don't gobble tokens or issue error messages if this is an
11330 optional type-specifier. */
11331 if (flags & CP_PARSER_FLAGS_OPTIONAL)
11332 cp_parser_parse_tentatively (parser);
11334 /* Look for the optional `::' operator. */
11336 = (cp_parser_global_scope_opt (parser,
11337 /*current_scope_valid_p=*/false)
11339 /* Look for the nested-name specifier. */
11341 = (cp_parser_nested_name_specifier_opt (parser,
11342 /*typename_keyword_p=*/false,
11343 /*check_dependency_p=*/true,
11345 /*is_declaration=*/false)
11347 token = cp_lexer_peek_token (parser->lexer);
11348 /* If we have seen a nested-name-specifier, and the next token
11349 is `template', then we are using the template-id production. */
11351 && cp_parser_optional_template_keyword (parser))
11353 /* Look for the template-id. */
11354 type = cp_parser_template_id (parser,
11355 /*template_keyword_p=*/true,
11356 /*check_dependency_p=*/true,
11357 /*is_declaration=*/false);
11358 /* If the template-id did not name a type, we are out of
11360 if (TREE_CODE (type) != TYPE_DECL)
11362 cp_parser_error (parser, "expected template-id for type");
11366 /* Otherwise, look for a type-name. */
11368 type = cp_parser_type_name (parser);
11369 /* Keep track of all name-lookups performed in class scopes. */
11373 && TREE_CODE (type) == TYPE_DECL
11374 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11375 maybe_note_name_used_in_class (DECL_NAME (type), type);
11376 /* If it didn't work out, we don't have a TYPE. */
11377 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
11378 && !cp_parser_parse_definitely (parser))
11380 if (type && decl_specs)
11381 cp_parser_set_decl_spec_type (decl_specs, type,
11383 /*user_defined=*/true);
11386 /* If we didn't get a type-name, issue an error message. */
11387 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11389 cp_parser_error (parser, "expected type-name");
11390 return error_mark_node;
11393 /* There is no valid C++ program where a non-template type is
11394 followed by a "<". That usually indicates that the user thought
11395 that the type was a template. */
11396 if (type && type != error_mark_node)
11398 /* As a last-ditch effort, see if TYPE is an Objective-C type.
11399 If it is, then the '<'...'>' enclose protocol names rather than
11400 template arguments, and so everything is fine. */
11401 if (c_dialect_objc ()
11402 && (objc_is_id (type) || objc_is_class_name (type)))
11404 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11405 tree qual_type = objc_get_protocol_qualified_type (type, protos);
11407 /* Clobber the "unqualified" type previously entered into
11408 DECL_SPECS with the new, improved protocol-qualified version. */
11410 decl_specs->type = qual_type;
11415 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
11422 /* Parse a type-name.
11435 Returns a TYPE_DECL for the type. */
11438 cp_parser_type_name (cp_parser* parser)
11442 /* We can't know yet whether it is a class-name or not. */
11443 cp_parser_parse_tentatively (parser);
11444 /* Try a class-name. */
11445 type_decl = cp_parser_class_name (parser,
11446 /*typename_keyword_p=*/false,
11447 /*template_keyword_p=*/false,
11449 /*check_dependency_p=*/true,
11450 /*class_head_p=*/false,
11451 /*is_declaration=*/false);
11452 /* If it's not a class-name, keep looking. */
11453 if (!cp_parser_parse_definitely (parser))
11455 /* It must be a typedef-name or an enum-name. */
11456 return cp_parser_nonclass_name (parser);
11462 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
11470 Returns a TYPE_DECL for the type. */
11473 cp_parser_nonclass_name (cp_parser* parser)
11478 cp_token *token = cp_lexer_peek_token (parser->lexer);
11479 identifier = cp_parser_identifier (parser);
11480 if (identifier == error_mark_node)
11481 return error_mark_node;
11483 /* Look up the type-name. */
11484 type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
11486 if (TREE_CODE (type_decl) != TYPE_DECL
11487 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
11489 /* See if this is an Objective-C type. */
11490 tree protos = cp_parser_objc_protocol_refs_opt (parser);
11491 tree type = objc_get_protocol_qualified_type (identifier, protos);
11493 type_decl = TYPE_NAME (type);
11496 /* Issue an error if we did not find a type-name. */
11497 if (TREE_CODE (type_decl) != TYPE_DECL)
11499 if (!cp_parser_simulate_error (parser))
11500 cp_parser_name_lookup_error (parser, identifier, type_decl,
11501 "is not a type", token->location);
11502 return error_mark_node;
11504 /* Remember that the name was used in the definition of the
11505 current class so that we can check later to see if the
11506 meaning would have been different after the class was
11507 entirely defined. */
11508 else if (type_decl != error_mark_node
11510 maybe_note_name_used_in_class (identifier, type_decl);
11515 /* Parse an elaborated-type-specifier. Note that the grammar given
11516 here incorporates the resolution to DR68.
11518 elaborated-type-specifier:
11519 class-key :: [opt] nested-name-specifier [opt] identifier
11520 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11521 enum-key :: [opt] nested-name-specifier [opt] identifier
11522 typename :: [opt] nested-name-specifier identifier
11523 typename :: [opt] nested-name-specifier template [opt]
11528 elaborated-type-specifier:
11529 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11530 class-key attributes :: [opt] nested-name-specifier [opt]
11531 template [opt] template-id
11532 enum attributes :: [opt] nested-name-specifier [opt] identifier
11534 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11535 declared `friend'. If IS_DECLARATION is TRUE, then this
11536 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11537 something is being declared.
11539 Returns the TYPE specified. */
11542 cp_parser_elaborated_type_specifier (cp_parser* parser,
11544 bool is_declaration)
11546 enum tag_types tag_type;
11548 tree type = NULL_TREE;
11549 tree attributes = NULL_TREE;
11550 cp_token *token = NULL;
11552 /* See if we're looking at the `enum' keyword. */
11553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11555 /* Consume the `enum' token. */
11556 cp_lexer_consume_token (parser->lexer);
11557 /* Remember that it's an enumeration type. */
11558 tag_type = enum_type;
11559 /* Parse the optional `struct' or `class' key (for C++0x scoped
11561 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11562 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11564 if (cxx_dialect == cxx98)
11565 maybe_warn_cpp0x ("scoped enums");
11567 /* Consume the `struct' or `class'. */
11568 cp_lexer_consume_token (parser->lexer);
11570 /* Parse the attributes. */
11571 attributes = cp_parser_attributes_opt (parser);
11573 /* Or, it might be `typename'. */
11574 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11577 /* Consume the `typename' token. */
11578 cp_lexer_consume_token (parser->lexer);
11579 /* Remember that it's a `typename' type. */
11580 tag_type = typename_type;
11581 /* The `typename' keyword is only allowed in templates. */
11582 if (!processing_template_decl)
11583 permerror (input_location, "using %<typename%> outside of template");
11585 /* Otherwise it must be a class-key. */
11588 tag_type = cp_parser_class_key (parser);
11589 if (tag_type == none_type)
11590 return error_mark_node;
11591 /* Parse the attributes. */
11592 attributes = cp_parser_attributes_opt (parser);
11595 /* Look for the `::' operator. */
11596 cp_parser_global_scope_opt (parser,
11597 /*current_scope_valid_p=*/false);
11598 /* Look for the nested-name-specifier. */
11599 if (tag_type == typename_type)
11601 if (!cp_parser_nested_name_specifier (parser,
11602 /*typename_keyword_p=*/true,
11603 /*check_dependency_p=*/true,
11606 return error_mark_node;
11609 /* Even though `typename' is not present, the proposed resolution
11610 to Core Issue 180 says that in `class A<T>::B', `B' should be
11611 considered a type-name, even if `A<T>' is dependent. */
11612 cp_parser_nested_name_specifier_opt (parser,
11613 /*typename_keyword_p=*/true,
11614 /*check_dependency_p=*/true,
11617 /* For everything but enumeration types, consider a template-id.
11618 For an enumeration type, consider only a plain identifier. */
11619 if (tag_type != enum_type)
11621 bool template_p = false;
11624 /* Allow the `template' keyword. */
11625 template_p = cp_parser_optional_template_keyword (parser);
11626 /* If we didn't see `template', we don't know if there's a
11627 template-id or not. */
11629 cp_parser_parse_tentatively (parser);
11630 /* Parse the template-id. */
11631 token = cp_lexer_peek_token (parser->lexer);
11632 decl = cp_parser_template_id (parser, template_p,
11633 /*check_dependency_p=*/true,
11635 /* If we didn't find a template-id, look for an ordinary
11637 if (!template_p && !cp_parser_parse_definitely (parser))
11639 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11640 in effect, then we must assume that, upon instantiation, the
11641 template will correspond to a class. */
11642 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11643 && tag_type == typename_type)
11644 type = make_typename_type (parser->scope, decl,
11646 /*complain=*/tf_error);
11647 /* If the `typename' keyword is in effect and DECL is not a type
11648 decl. Then type is non existant. */
11649 else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
11652 type = TREE_TYPE (decl);
11657 token = cp_lexer_peek_token (parser->lexer);
11658 identifier = cp_parser_identifier (parser);
11660 if (identifier == error_mark_node)
11662 parser->scope = NULL_TREE;
11663 return error_mark_node;
11666 /* For a `typename', we needn't call xref_tag. */
11667 if (tag_type == typename_type
11668 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11669 return cp_parser_make_typename_type (parser, parser->scope,
11672 /* Look up a qualified name in the usual way. */
11676 tree ambiguous_decls;
11678 decl = cp_parser_lookup_name (parser, identifier,
11680 /*is_template=*/false,
11681 /*is_namespace=*/false,
11682 /*check_dependency=*/true,
11686 /* If the lookup was ambiguous, an error will already have been
11688 if (ambiguous_decls)
11689 return error_mark_node;
11691 /* If we are parsing friend declaration, DECL may be a
11692 TEMPLATE_DECL tree node here. However, we need to check
11693 whether this TEMPLATE_DECL results in valid code. Consider
11694 the following example:
11697 template <class T> class C {};
11700 template <class T> friend class N::C; // #1, valid code
11702 template <class T> class Y {
11703 friend class N::C; // #2, invalid code
11706 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11707 name lookup of `N::C'. We see that friend declaration must
11708 be template for the code to be valid. Note that
11709 processing_template_decl does not work here since it is
11710 always 1 for the above two cases. */
11712 decl = (cp_parser_maybe_treat_template_as_class
11713 (decl, /*tag_name_p=*/is_friend
11714 && parser->num_template_parameter_lists));
11716 if (TREE_CODE (decl) != TYPE_DECL)
11718 cp_parser_diagnose_invalid_type_name (parser,
11722 return error_mark_node;
11725 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11727 bool allow_template = (parser->num_template_parameter_lists
11728 || DECL_SELF_REFERENCE_P (decl));
11729 type = check_elaborated_type_specifier (tag_type, decl,
11732 if (type == error_mark_node)
11733 return error_mark_node;
11736 /* Forward declarations of nested types, such as
11741 are invalid unless all components preceding the final '::'
11742 are complete. If all enclosing types are complete, these
11743 declarations become merely pointless.
11745 Invalid forward declarations of nested types are errors
11746 caught elsewhere in parsing. Those that are pointless arrive
11749 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11750 && !is_friend && !processing_explicit_instantiation)
11751 warning (0, "declaration %qD does not declare anything", decl);
11753 type = TREE_TYPE (decl);
11757 /* An elaborated-type-specifier sometimes introduces a new type and
11758 sometimes names an existing type. Normally, the rule is that it
11759 introduces a new type only if there is not an existing type of
11760 the same name already in scope. For example, given:
11763 void f() { struct S s; }
11765 the `struct S' in the body of `f' is the same `struct S' as in
11766 the global scope; the existing definition is used. However, if
11767 there were no global declaration, this would introduce a new
11768 local class named `S'.
11770 An exception to this rule applies to the following code:
11772 namespace N { struct S; }
11774 Here, the elaborated-type-specifier names a new type
11775 unconditionally; even if there is already an `S' in the
11776 containing scope this declaration names a new type.
11777 This exception only applies if the elaborated-type-specifier
11778 forms the complete declaration:
11782 A declaration consisting solely of `class-key identifier ;' is
11783 either a redeclaration of the name in the current scope or a
11784 forward declaration of the identifier as a class name. It
11785 introduces the name into the current scope.
11787 We are in this situation precisely when the next token is a `;'.
11789 An exception to the exception is that a `friend' declaration does
11790 *not* name a new type; i.e., given:
11792 struct S { friend struct T; };
11794 `T' is not a new type in the scope of `S'.
11796 Also, `new struct S' or `sizeof (struct S)' never results in the
11797 definition of a new type; a new type can only be declared in a
11798 declaration context. */
11804 /* Friends have special name lookup rules. */
11805 ts = ts_within_enclosing_non_class;
11806 else if (is_declaration
11807 && cp_lexer_next_token_is (parser->lexer,
11809 /* This is a `class-key identifier ;' */
11815 (parser->num_template_parameter_lists
11816 && (cp_parser_next_token_starts_class_definition_p (parser)
11817 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11818 /* An unqualified name was used to reference this type, so
11819 there were no qualifying templates. */
11820 if (!cp_parser_check_template_parameters (parser,
11821 /*num_templates=*/0,
11823 /*declarator=*/NULL))
11824 return error_mark_node;
11825 type = xref_tag (tag_type, identifier, ts, template_p);
11829 if (type == error_mark_node)
11830 return error_mark_node;
11832 /* Allow attributes on forward declarations of classes. */
11835 if (TREE_CODE (type) == TYPENAME_TYPE)
11836 warning (OPT_Wattributes,
11837 "attributes ignored on uninstantiated type");
11838 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11839 && ! processing_explicit_instantiation)
11840 warning (OPT_Wattributes,
11841 "attributes ignored on template instantiation");
11842 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11843 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11845 warning (OPT_Wattributes,
11846 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11849 if (tag_type != enum_type)
11850 cp_parser_check_class_key (tag_type, type);
11852 /* A "<" cannot follow an elaborated type specifier. If that
11853 happens, the user was probably trying to form a template-id. */
11854 cp_parser_check_for_invalid_template_id (parser, type, token->location);
11859 /* Parse an enum-specifier.
11862 enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
11867 enum struct [C++0x]
11870 : type-specifier-seq
11873 enum-key attributes[opt] identifier [opt] enum-base [opt]
11874 { enumerator-list [opt] }attributes[opt]
11876 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11877 if the token stream isn't an enum-specifier after all. */
11880 cp_parser_enum_specifier (cp_parser* parser)
11885 bool scoped_enum_p = false;
11886 bool has_underlying_type = false;
11887 tree underlying_type = NULL_TREE;
11889 /* Parse tentatively so that we can back up if we don't find a
11891 cp_parser_parse_tentatively (parser);
11893 /* Caller guarantees that the current token is 'enum', an identifier
11894 possibly follows, and the token after that is an opening brace.
11895 If we don't have an identifier, fabricate an anonymous name for
11896 the enumeration being defined. */
11897 cp_lexer_consume_token (parser->lexer);
11899 /* Parse the "class" or "struct", which indicates a scoped
11900 enumeration type in C++0x. */
11901 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
11902 || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
11904 if (cxx_dialect == cxx98)
11905 maybe_warn_cpp0x ("scoped enums");
11907 /* Consume the `struct' or `class' token. */
11908 cp_lexer_consume_token (parser->lexer);
11910 scoped_enum_p = true;
11913 attributes = cp_parser_attributes_opt (parser);
11915 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11916 identifier = cp_parser_identifier (parser);
11918 identifier = make_anon_name ();
11920 /* Check for the `:' that denotes a specified underlying type in C++0x. */
11921 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11923 cp_decl_specifier_seq type_specifiers;
11925 /* At this point this is surely not elaborated type specifier. */
11926 if (!cp_parser_parse_definitely (parser))
11929 if (cxx_dialect == cxx98)
11930 maybe_warn_cpp0x ("scoped enums");
11932 /* Consume the `:'. */
11933 cp_lexer_consume_token (parser->lexer);
11935 has_underlying_type = true;
11937 /* Parse the type-specifier-seq. */
11938 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11941 /* If that didn't work, stop. */
11942 if (type_specifiers.type != error_mark_node)
11944 underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
11945 /*initialized=*/0, NULL);
11946 if (underlying_type == error_mark_node)
11947 underlying_type = NULL_TREE;
11951 /* Look for the `{' but don't consume it yet. */
11952 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11954 cp_parser_error (parser, "expected %<{%>");
11955 if (has_underlying_type)
11959 if (!has_underlying_type && !cp_parser_parse_definitely (parser))
11962 /* Issue an error message if type-definitions are forbidden here. */
11963 if (!cp_parser_check_type_definition (parser))
11964 type = error_mark_node;
11966 /* Create the new type. We do this before consuming the opening
11967 brace so the enum will be recorded as being on the line of its
11968 tag (or the 'enum' keyword, if there is no tag). */
11969 type = start_enum (identifier, underlying_type, scoped_enum_p);
11971 /* Consume the opening brace. */
11972 cp_lexer_consume_token (parser->lexer);
11974 if (type == error_mark_node)
11976 cp_parser_skip_to_end_of_block_or_statement (parser);
11977 return error_mark_node;
11980 /* If the next token is not '}', then there are some enumerators. */
11981 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11982 cp_parser_enumerator_list (parser, type);
11984 /* Consume the final '}'. */
11985 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
11987 /* Look for trailing attributes to apply to this enumeration, and
11988 apply them if appropriate. */
11989 if (cp_parser_allow_gnu_extensions_p (parser))
11991 tree trailing_attr = cp_parser_attributes_opt (parser);
11992 trailing_attr = chainon (trailing_attr, attributes);
11993 cplus_decl_attributes (&type,
11995 (int) ATTR_FLAG_TYPE_IN_PLACE);
11998 /* Finish up the enumeration. */
11999 finish_enum (type);
12004 /* Parse an enumerator-list. The enumerators all have the indicated
12008 enumerator-definition
12009 enumerator-list , enumerator-definition */
12012 cp_parser_enumerator_list (cp_parser* parser, tree type)
12016 /* Parse an enumerator-definition. */
12017 cp_parser_enumerator_definition (parser, type);
12019 /* If the next token is not a ',', we've reached the end of
12021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12023 /* Otherwise, consume the `,' and keep going. */
12024 cp_lexer_consume_token (parser->lexer);
12025 /* If the next token is a `}', there is a trailing comma. */
12026 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12028 if (!in_system_header)
12029 pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12035 /* Parse an enumerator-definition. The enumerator has the indicated
12038 enumerator-definition:
12040 enumerator = constant-expression
12046 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12051 /* Look for the identifier. */
12052 identifier = cp_parser_identifier (parser);
12053 if (identifier == error_mark_node)
12056 /* If the next token is an '=', then there is an explicit value. */
12057 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12059 /* Consume the `=' token. */
12060 cp_lexer_consume_token (parser->lexer);
12061 /* Parse the value. */
12062 value = cp_parser_constant_expression (parser,
12063 /*allow_non_constant_p=*/false,
12069 /* If we are processing a template, make sure the initializer of the
12070 enumerator doesn't contain any bare template parameter pack. */
12071 if (check_for_bare_parameter_packs (value))
12072 value = error_mark_node;
12074 /* Create the enumerator. */
12075 build_enumerator (identifier, value, type);
12078 /* Parse a namespace-name.
12081 original-namespace-name
12084 Returns the NAMESPACE_DECL for the namespace. */
12087 cp_parser_namespace_name (cp_parser* parser)
12090 tree namespace_decl;
12092 cp_token *token = cp_lexer_peek_token (parser->lexer);
12094 /* Get the name of the namespace. */
12095 identifier = cp_parser_identifier (parser);
12096 if (identifier == error_mark_node)
12097 return error_mark_node;
12099 /* Look up the identifier in the currently active scope. Look only
12100 for namespaces, due to:
12102 [basic.lookup.udir]
12104 When looking up a namespace-name in a using-directive or alias
12105 definition, only namespace names are considered.
12109 [basic.lookup.qual]
12111 During the lookup of a name preceding the :: scope resolution
12112 operator, object, function, and enumerator names are ignored.
12114 (Note that cp_parser_qualifying_entity only calls this
12115 function if the token after the name is the scope resolution
12117 namespace_decl = cp_parser_lookup_name (parser, identifier,
12119 /*is_template=*/false,
12120 /*is_namespace=*/true,
12121 /*check_dependency=*/true,
12122 /*ambiguous_decls=*/NULL,
12124 /* If it's not a namespace, issue an error. */
12125 if (namespace_decl == error_mark_node
12126 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12128 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12129 error ("%H%qD is not a namespace-name", &token->location, identifier);
12130 cp_parser_error (parser, "expected namespace-name");
12131 namespace_decl = error_mark_node;
12134 return namespace_decl;
12137 /* Parse a namespace-definition.
12139 namespace-definition:
12140 named-namespace-definition
12141 unnamed-namespace-definition
12143 named-namespace-definition:
12144 original-namespace-definition
12145 extension-namespace-definition
12147 original-namespace-definition:
12148 namespace identifier { namespace-body }
12150 extension-namespace-definition:
12151 namespace original-namespace-name { namespace-body }
12153 unnamed-namespace-definition:
12154 namespace { namespace-body } */
12157 cp_parser_namespace_definition (cp_parser* parser)
12159 tree identifier, attribs;
12160 bool has_visibility;
12163 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12166 cp_lexer_consume_token (parser->lexer);
12171 /* Look for the `namespace' keyword. */
12172 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12174 /* Get the name of the namespace. We do not attempt to distinguish
12175 between an original-namespace-definition and an
12176 extension-namespace-definition at this point. The semantic
12177 analysis routines are responsible for that. */
12178 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12179 identifier = cp_parser_identifier (parser);
12181 identifier = NULL_TREE;
12183 /* Parse any specified attributes. */
12184 attribs = cp_parser_attributes_opt (parser);
12186 /* Look for the `{' to start the namespace. */
12187 cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12188 /* Start the namespace. */
12189 push_namespace (identifier);
12191 /* "inline namespace" is equivalent to a stub namespace definition
12192 followed by a strong using directive. */
12195 tree name_space = current_namespace;
12196 /* Set up namespace association. */
12197 DECL_NAMESPACE_ASSOCIATIONS (name_space)
12198 = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12199 DECL_NAMESPACE_ASSOCIATIONS (name_space));
12200 /* Import the contents of the inline namespace. */
12202 do_using_directive (name_space);
12203 push_namespace (identifier);
12206 has_visibility = handle_namespace_attrs (current_namespace, attribs);
12208 /* Parse the body of the namespace. */
12209 cp_parser_namespace_body (parser);
12211 #ifdef HANDLE_PRAGMA_VISIBILITY
12212 if (has_visibility)
12216 /* Finish the namespace. */
12218 /* Look for the final `}'. */
12219 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12222 /* Parse a namespace-body.
12225 declaration-seq [opt] */
12228 cp_parser_namespace_body (cp_parser* parser)
12230 cp_parser_declaration_seq_opt (parser);
12233 /* Parse a namespace-alias-definition.
12235 namespace-alias-definition:
12236 namespace identifier = qualified-namespace-specifier ; */
12239 cp_parser_namespace_alias_definition (cp_parser* parser)
12242 tree namespace_specifier;
12244 cp_token *token = cp_lexer_peek_token (parser->lexer);
12246 /* Look for the `namespace' keyword. */
12247 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12248 /* Look for the identifier. */
12249 identifier = cp_parser_identifier (parser);
12250 if (identifier == error_mark_node)
12252 /* Look for the `=' token. */
12253 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12254 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12256 error ("%H%<namespace%> definition is not allowed here", &token->location);
12257 /* Skip the definition. */
12258 cp_lexer_consume_token (parser->lexer);
12259 if (cp_parser_skip_to_closing_brace (parser))
12260 cp_lexer_consume_token (parser->lexer);
12263 cp_parser_require (parser, CPP_EQ, "%<=%>");
12264 /* Look for the qualified-namespace-specifier. */
12265 namespace_specifier
12266 = cp_parser_qualified_namespace_specifier (parser);
12267 /* Look for the `;' token. */
12268 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12270 /* Register the alias in the symbol table. */
12271 do_namespace_alias (identifier, namespace_specifier);
12274 /* Parse a qualified-namespace-specifier.
12276 qualified-namespace-specifier:
12277 :: [opt] nested-name-specifier [opt] namespace-name
12279 Returns a NAMESPACE_DECL corresponding to the specified
12283 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12285 /* Look for the optional `::'. */
12286 cp_parser_global_scope_opt (parser,
12287 /*current_scope_valid_p=*/false);
12289 /* Look for the optional nested-name-specifier. */
12290 cp_parser_nested_name_specifier_opt (parser,
12291 /*typename_keyword_p=*/false,
12292 /*check_dependency_p=*/true,
12294 /*is_declaration=*/true);
12296 return cp_parser_namespace_name (parser);
12299 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12300 access declaration.
12303 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12304 using :: unqualified-id ;
12306 access-declaration:
12312 cp_parser_using_declaration (cp_parser* parser,
12313 bool access_declaration_p)
12316 bool typename_p = false;
12317 bool global_scope_p;
12322 if (access_declaration_p)
12323 cp_parser_parse_tentatively (parser);
12326 /* Look for the `using' keyword. */
12327 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12329 /* Peek at the next token. */
12330 token = cp_lexer_peek_token (parser->lexer);
12331 /* See if it's `typename'. */
12332 if (token->keyword == RID_TYPENAME)
12334 /* Remember that we've seen it. */
12336 /* Consume the `typename' token. */
12337 cp_lexer_consume_token (parser->lexer);
12341 /* Look for the optional global scope qualification. */
12343 = (cp_parser_global_scope_opt (parser,
12344 /*current_scope_valid_p=*/false)
12347 /* If we saw `typename', or didn't see `::', then there must be a
12348 nested-name-specifier present. */
12349 if (typename_p || !global_scope_p)
12350 qscope = cp_parser_nested_name_specifier (parser, typename_p,
12351 /*check_dependency_p=*/true,
12353 /*is_declaration=*/true);
12354 /* Otherwise, we could be in either of the two productions. In that
12355 case, treat the nested-name-specifier as optional. */
12357 qscope = cp_parser_nested_name_specifier_opt (parser,
12358 /*typename_keyword_p=*/false,
12359 /*check_dependency_p=*/true,
12361 /*is_declaration=*/true);
12363 qscope = global_namespace;
12365 if (access_declaration_p && cp_parser_error_occurred (parser))
12366 /* Something has already gone wrong; there's no need to parse
12367 further. Since an error has occurred, the return value of
12368 cp_parser_parse_definitely will be false, as required. */
12369 return cp_parser_parse_definitely (parser);
12371 token = cp_lexer_peek_token (parser->lexer);
12372 /* Parse the unqualified-id. */
12373 identifier = cp_parser_unqualified_id (parser,
12374 /*template_keyword_p=*/false,
12375 /*check_dependency_p=*/true,
12376 /*declarator_p=*/true,
12377 /*optional_p=*/false);
12379 if (access_declaration_p)
12381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12382 cp_parser_simulate_error (parser);
12383 if (!cp_parser_parse_definitely (parser))
12387 /* The function we call to handle a using-declaration is different
12388 depending on what scope we are in. */
12389 if (qscope == error_mark_node || identifier == error_mark_node)
12391 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
12392 && TREE_CODE (identifier) != BIT_NOT_EXPR)
12393 /* [namespace.udecl]
12395 A using declaration shall not name a template-id. */
12396 error ("%Ha template-id may not appear in a using-declaration",
12400 if (at_class_scope_p ())
12402 /* Create the USING_DECL. */
12403 decl = do_class_using_decl (parser->scope, identifier);
12405 if (check_for_bare_parameter_packs (decl))
12408 /* Add it to the list of members in this class. */
12409 finish_member_declaration (decl);
12413 decl = cp_parser_lookup_name_simple (parser,
12416 if (decl == error_mark_node)
12417 cp_parser_name_lookup_error (parser, identifier,
12420 else if (check_for_bare_parameter_packs (decl))
12422 else if (!at_namespace_scope_p ())
12423 do_local_using_decl (decl, qscope, identifier);
12425 do_toplevel_using_decl (decl, qscope, identifier);
12429 /* Look for the final `;'. */
12430 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12435 /* Parse a using-directive.
12438 using namespace :: [opt] nested-name-specifier [opt]
12439 namespace-name ; */
12442 cp_parser_using_directive (cp_parser* parser)
12444 tree namespace_decl;
12447 /* Look for the `using' keyword. */
12448 cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12449 /* And the `namespace' keyword. */
12450 cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12451 /* Look for the optional `::' operator. */
12452 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12453 /* And the optional nested-name-specifier. */
12454 cp_parser_nested_name_specifier_opt (parser,
12455 /*typename_keyword_p=*/false,
12456 /*check_dependency_p=*/true,
12458 /*is_declaration=*/true);
12459 /* Get the namespace being used. */
12460 namespace_decl = cp_parser_namespace_name (parser);
12461 /* And any specified attributes. */
12462 attribs = cp_parser_attributes_opt (parser);
12463 /* Update the symbol table. */
12464 parse_using_directive (namespace_decl, attribs);
12465 /* Look for the final `;'. */
12466 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12469 /* Parse an asm-definition.
12472 asm ( string-literal ) ;
12477 asm volatile [opt] ( string-literal ) ;
12478 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
12479 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12480 : asm-operand-list [opt] ) ;
12481 asm volatile [opt] ( string-literal : asm-operand-list [opt]
12482 : asm-operand-list [opt]
12483 : asm-operand-list [opt] ) ; */
12486 cp_parser_asm_definition (cp_parser* parser)
12489 tree outputs = NULL_TREE;
12490 tree inputs = NULL_TREE;
12491 tree clobbers = NULL_TREE;
12493 bool volatile_p = false;
12494 bool extended_p = false;
12495 bool invalid_inputs_p = false;
12496 bool invalid_outputs_p = false;
12498 /* Look for the `asm' keyword. */
12499 cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
12500 /* See if the next token is `volatile'. */
12501 if (cp_parser_allow_gnu_extensions_p (parser)
12502 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
12504 /* Remember that we saw the `volatile' keyword. */
12506 /* Consume the token. */
12507 cp_lexer_consume_token (parser->lexer);
12509 /* Look for the opening `('. */
12510 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
12512 /* Look for the string. */
12513 string = cp_parser_string_literal (parser, false, false);
12514 if (string == error_mark_node)
12516 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12517 /*consume_paren=*/true);
12521 /* If we're allowing GNU extensions, check for the extended assembly
12522 syntax. Unfortunately, the `:' tokens need not be separated by
12523 a space in C, and so, for compatibility, we tolerate that here
12524 too. Doing that means that we have to treat the `::' operator as
12526 if (cp_parser_allow_gnu_extensions_p (parser)
12527 && parser->in_function_body
12528 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
12529 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
12531 bool inputs_p = false;
12532 bool clobbers_p = false;
12534 /* The extended syntax was used. */
12537 /* Look for outputs. */
12538 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12540 /* Consume the `:'. */
12541 cp_lexer_consume_token (parser->lexer);
12542 /* Parse the output-operands. */
12543 if (cp_lexer_next_token_is_not (parser->lexer,
12545 && cp_lexer_next_token_is_not (parser->lexer,
12547 && cp_lexer_next_token_is_not (parser->lexer,
12549 outputs = cp_parser_asm_operand_list (parser);
12551 if (outputs == error_mark_node)
12552 invalid_outputs_p = true;
12554 /* If the next token is `::', there are no outputs, and the
12555 next token is the beginning of the inputs. */
12556 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12557 /* The inputs are coming next. */
12560 /* Look for inputs. */
12562 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12564 /* Consume the `:' or `::'. */
12565 cp_lexer_consume_token (parser->lexer);
12566 /* Parse the output-operands. */
12567 if (cp_lexer_next_token_is_not (parser->lexer,
12569 && cp_lexer_next_token_is_not (parser->lexer,
12571 inputs = cp_parser_asm_operand_list (parser);
12573 if (inputs == error_mark_node)
12574 invalid_inputs_p = true;
12576 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12577 /* The clobbers are coming next. */
12580 /* Look for clobbers. */
12582 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12584 /* Consume the `:' or `::'. */
12585 cp_lexer_consume_token (parser->lexer);
12586 /* Parse the clobbers. */
12587 if (cp_lexer_next_token_is_not (parser->lexer,
12589 clobbers = cp_parser_asm_clobber_list (parser);
12592 /* Look for the closing `)'. */
12593 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
12594 cp_parser_skip_to_closing_parenthesis (parser, true, false,
12595 /*consume_paren=*/true);
12596 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12598 if (!invalid_inputs_p && !invalid_outputs_p)
12600 /* Create the ASM_EXPR. */
12601 if (parser->in_function_body)
12603 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
12605 /* If the extended syntax was not used, mark the ASM_EXPR. */
12608 tree temp = asm_stmt;
12609 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12610 temp = TREE_OPERAND (temp, 0);
12612 ASM_INPUT_P (temp) = 1;
12616 cgraph_add_asm_node (string);
12620 /* Declarators [gram.dcl.decl] */
12622 /* Parse an init-declarator.
12625 declarator initializer [opt]
12630 declarator asm-specification [opt] attributes [opt] initializer [opt]
12632 function-definition:
12633 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12635 decl-specifier-seq [opt] declarator function-try-block
12639 function-definition:
12640 __extension__ function-definition
12642 The DECL_SPECIFIERS apply to this declarator. Returns a
12643 representation of the entity declared. If MEMBER_P is TRUE, then
12644 this declarator appears in a class scope. The new DECL created by
12645 this declarator is returned.
12647 The CHECKS are access checks that should be performed once we know
12648 what entity is being declared (and, therefore, what classes have
12651 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12652 for a function-definition here as well. If the declarator is a
12653 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12654 be TRUE upon return. By that point, the function-definition will
12655 have been completely parsed.
12657 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12661 cp_parser_init_declarator (cp_parser* parser,
12662 cp_decl_specifier_seq *decl_specifiers,
12663 VEC (deferred_access_check,gc)* checks,
12664 bool function_definition_allowed_p,
12666 int declares_class_or_enum,
12667 bool* function_definition_p)
12669 cp_token *token = NULL, *asm_spec_start_token = NULL,
12670 *attributes_start_token = NULL;
12671 cp_declarator *declarator;
12672 tree prefix_attributes;
12674 tree asm_specification;
12676 tree decl = NULL_TREE;
12678 int is_initialized;
12679 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12680 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12682 enum cpp_ttype initialization_kind;
12683 bool is_direct_init = false;
12684 bool is_non_constant_init;
12685 int ctor_dtor_or_conv_p;
12687 tree pushed_scope = NULL;
12689 /* Gather the attributes that were provided with the
12690 decl-specifiers. */
12691 prefix_attributes = decl_specifiers->attributes;
12693 /* Assume that this is not the declarator for a function
12695 if (function_definition_p)
12696 *function_definition_p = false;
12698 /* Defer access checks while parsing the declarator; we cannot know
12699 what names are accessible until we know what is being
12701 resume_deferring_access_checks ();
12703 /* Parse the declarator. */
12704 token = cp_lexer_peek_token (parser->lexer);
12706 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12707 &ctor_dtor_or_conv_p,
12708 /*parenthesized_p=*/NULL,
12709 /*member_p=*/false);
12710 /* Gather up the deferred checks. */
12711 stop_deferring_access_checks ();
12713 /* If the DECLARATOR was erroneous, there's no need to go
12715 if (declarator == cp_error_declarator)
12716 return error_mark_node;
12718 /* Check that the number of template-parameter-lists is OK. */
12719 if (!cp_parser_check_declarator_template_parameters (parser, declarator,
12721 return error_mark_node;
12723 if (declares_class_or_enum & 2)
12724 cp_parser_check_for_definition_in_return_type (declarator,
12725 decl_specifiers->type,
12726 decl_specifiers->type_location);
12728 /* Figure out what scope the entity declared by the DECLARATOR is
12729 located in. `grokdeclarator' sometimes changes the scope, so
12730 we compute it now. */
12731 scope = get_scope_of_declarator (declarator);
12733 /* If we're allowing GNU extensions, look for an asm-specification
12735 if (cp_parser_allow_gnu_extensions_p (parser))
12737 /* Look for an asm-specification. */
12738 asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
12739 asm_specification = cp_parser_asm_specification_opt (parser);
12740 /* And attributes. */
12741 attributes_start_token = cp_lexer_peek_token (parser->lexer);
12742 attributes = cp_parser_attributes_opt (parser);
12746 asm_specification = NULL_TREE;
12747 attributes = NULL_TREE;
12750 /* Peek at the next token. */
12751 token = cp_lexer_peek_token (parser->lexer);
12752 /* Check to see if the token indicates the start of a
12753 function-definition. */
12754 if (function_declarator_p (declarator)
12755 && cp_parser_token_starts_function_definition_p (token))
12757 if (!function_definition_allowed_p)
12759 /* If a function-definition should not appear here, issue an
12761 cp_parser_error (parser,
12762 "a function-definition is not allowed here");
12763 return error_mark_node;
12767 location_t func_brace_location
12768 = cp_lexer_peek_token (parser->lexer)->location;
12770 /* Neither attributes nor an asm-specification are allowed
12771 on a function-definition. */
12772 if (asm_specification)
12773 error ("%Han asm-specification is not allowed "
12774 "on a function-definition",
12775 &asm_spec_start_token->location);
12777 error ("%Hattributes are not allowed on a function-definition",
12778 &attributes_start_token->location);
12779 /* This is a function-definition. */
12780 *function_definition_p = true;
12782 /* Parse the function definition. */
12784 decl = cp_parser_save_member_function_body (parser,
12787 prefix_attributes);
12790 = (cp_parser_function_definition_from_specifiers_and_declarator
12791 (parser, decl_specifiers, prefix_attributes, declarator));
12793 if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
12795 /* This is where the prologue starts... */
12796 DECL_STRUCT_FUNCTION (decl)->function_start_locus
12797 = func_brace_location;
12806 Only in function declarations for constructors, destructors, and
12807 type conversions can the decl-specifier-seq be omitted.
12809 We explicitly postpone this check past the point where we handle
12810 function-definitions because we tolerate function-definitions
12811 that are missing their return types in some modes. */
12812 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12814 cp_parser_error (parser,
12815 "expected constructor, destructor, or type conversion");
12816 return error_mark_node;
12819 /* An `=' or an `(', or an '{' in C++0x, indicates an initializer. */
12820 if (token->type == CPP_EQ
12821 || token->type == CPP_OPEN_PAREN
12822 || token->type == CPP_OPEN_BRACE)
12824 is_initialized = SD_INITIALIZED;
12825 initialization_kind = token->type;
12827 if (token->type == CPP_EQ
12828 && function_declarator_p (declarator))
12830 cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12831 if (t2->keyword == RID_DEFAULT)
12832 is_initialized = SD_DEFAULTED;
12833 else if (t2->keyword == RID_DELETE)
12834 is_initialized = SD_DELETED;
12839 /* If the init-declarator isn't initialized and isn't followed by a
12840 `,' or `;', it's not a valid init-declarator. */
12841 if (token->type != CPP_COMMA
12842 && token->type != CPP_SEMICOLON)
12844 cp_parser_error (parser, "expected initializer");
12845 return error_mark_node;
12847 is_initialized = SD_UNINITIALIZED;
12848 initialization_kind = CPP_EOF;
12851 /* Because start_decl has side-effects, we should only call it if we
12852 know we're going ahead. By this point, we know that we cannot
12853 possibly be looking at any other construct. */
12854 cp_parser_commit_to_tentative_parse (parser);
12856 /* If the decl specifiers were bad, issue an error now that we're
12857 sure this was intended to be a declarator. Then continue
12858 declaring the variable(s), as int, to try to cut down on further
12860 if (decl_specifiers->any_specifiers_p
12861 && decl_specifiers->type == error_mark_node)
12863 cp_parser_error (parser, "invalid type in declaration");
12864 decl_specifiers->type = integer_type_node;
12867 /* Check to see whether or not this declaration is a friend. */
12868 friend_p = cp_parser_friend_p (decl_specifiers);
12870 /* Enter the newly declared entry in the symbol table. If we're
12871 processing a declaration in a class-specifier, we wait until
12872 after processing the initializer. */
12875 if (parser->in_unbraced_linkage_specification_p)
12876 decl_specifiers->storage_class = sc_extern;
12877 decl = start_decl (declarator, decl_specifiers,
12878 is_initialized, attributes, prefix_attributes,
12882 /* Enter the SCOPE. That way unqualified names appearing in the
12883 initializer will be looked up in SCOPE. */
12884 pushed_scope = push_scope (scope);
12886 /* Perform deferred access control checks, now that we know in which
12887 SCOPE the declared entity resides. */
12888 if (!member_p && decl)
12890 tree saved_current_function_decl = NULL_TREE;
12892 /* If the entity being declared is a function, pretend that we
12893 are in its scope. If it is a `friend', it may have access to
12894 things that would not otherwise be accessible. */
12895 if (TREE_CODE (decl) == FUNCTION_DECL)
12897 saved_current_function_decl = current_function_decl;
12898 current_function_decl = decl;
12901 /* Perform access checks for template parameters. */
12902 cp_parser_perform_template_parameter_access_checks (checks);
12904 /* Perform the access control checks for the declarator and the
12905 decl-specifiers. */
12906 perform_deferred_access_checks ();
12908 /* Restore the saved value. */
12909 if (TREE_CODE (decl) == FUNCTION_DECL)
12910 current_function_decl = saved_current_function_decl;
12913 /* Parse the initializer. */
12914 initializer = NULL_TREE;
12915 is_direct_init = false;
12916 is_non_constant_init = true;
12917 if (is_initialized)
12919 if (function_declarator_p (declarator))
12921 cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
12922 if (initialization_kind == CPP_EQ)
12923 initializer = cp_parser_pure_specifier (parser);
12926 /* If the declaration was erroneous, we don't really
12927 know what the user intended, so just silently
12928 consume the initializer. */
12929 if (decl != error_mark_node)
12930 error ("%Hinitializer provided for function",
12931 &initializer_start_token->location);
12932 cp_parser_skip_to_closing_parenthesis (parser,
12933 /*recovering=*/true,
12934 /*or_comma=*/false,
12935 /*consume_paren=*/true);
12939 initializer = cp_parser_initializer (parser,
12941 &is_non_constant_init);
12944 /* The old parser allows attributes to appear after a parenthesized
12945 initializer. Mark Mitchell proposed removing this functionality
12946 on the GCC mailing lists on 2002-08-13. This parser accepts the
12947 attributes -- but ignores them. */
12948 if (cp_parser_allow_gnu_extensions_p (parser)
12949 && initialization_kind == CPP_OPEN_PAREN)
12950 if (cp_parser_attributes_opt (parser))
12951 warning (OPT_Wattributes,
12952 "attributes after parenthesized initializer ignored");
12954 /* For an in-class declaration, use `grokfield' to create the
12960 pop_scope (pushed_scope);
12961 pushed_scope = false;
12963 decl = grokfield (declarator, decl_specifiers,
12964 initializer, !is_non_constant_init,
12965 /*asmspec=*/NULL_TREE,
12966 prefix_attributes);
12967 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12968 cp_parser_save_default_args (parser, decl);
12971 /* Finish processing the declaration. But, skip friend
12973 if (!friend_p && decl && decl != error_mark_node)
12975 cp_finish_decl (decl,
12976 initializer, !is_non_constant_init,
12978 /* If the initializer is in parentheses, then this is
12979 a direct-initialization, which means that an
12980 `explicit' constructor is OK. Otherwise, an
12981 `explicit' constructor cannot be used. */
12982 ((is_direct_init || !is_initialized)
12983 ? 0 : LOOKUP_ONLYCONVERTING));
12985 else if ((cxx_dialect != cxx98) && friend_p
12986 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12987 /* Core issue #226 (C++0x only): A default template-argument
12988 shall not be specified in a friend class template
12990 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12991 /*is_partial=*/0, /*is_friend_decl=*/1);
12993 if (!friend_p && pushed_scope)
12994 pop_scope (pushed_scope);
12999 /* Parse a declarator.
13003 ptr-operator declarator
13005 abstract-declarator:
13006 ptr-operator abstract-declarator [opt]
13007 direct-abstract-declarator
13012 attributes [opt] direct-declarator
13013 attributes [opt] ptr-operator declarator
13015 abstract-declarator:
13016 attributes [opt] ptr-operator abstract-declarator [opt]
13017 attributes [opt] direct-abstract-declarator
13019 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13020 detect constructor, destructor or conversion operators. It is set
13021 to -1 if the declarator is a name, and +1 if it is a
13022 function. Otherwise it is set to zero. Usually you just want to
13023 test for >0, but internally the negative value is used.
13025 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13026 a decl-specifier-seq unless it declares a constructor, destructor,
13027 or conversion. It might seem that we could check this condition in
13028 semantic analysis, rather than parsing, but that makes it difficult
13029 to handle something like `f()'. We want to notice that there are
13030 no decl-specifiers, and therefore realize that this is an
13031 expression, not a declaration.)
13033 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13034 the declarator is a direct-declarator of the form "(...)".
13036 MEMBER_P is true iff this declarator is a member-declarator. */
13038 static cp_declarator *
13039 cp_parser_declarator (cp_parser* parser,
13040 cp_parser_declarator_kind dcl_kind,
13041 int* ctor_dtor_or_conv_p,
13042 bool* parenthesized_p,
13046 cp_declarator *declarator;
13047 enum tree_code code;
13048 cp_cv_quals cv_quals;
13050 tree attributes = NULL_TREE;
13052 /* Assume this is not a constructor, destructor, or type-conversion
13054 if (ctor_dtor_or_conv_p)
13055 *ctor_dtor_or_conv_p = 0;
13057 if (cp_parser_allow_gnu_extensions_p (parser))
13058 attributes = cp_parser_attributes_opt (parser);
13060 /* Peek at the next token. */
13061 token = cp_lexer_peek_token (parser->lexer);
13063 /* Check for the ptr-operator production. */
13064 cp_parser_parse_tentatively (parser);
13065 /* Parse the ptr-operator. */
13066 code = cp_parser_ptr_operator (parser,
13069 /* If that worked, then we have a ptr-operator. */
13070 if (cp_parser_parse_definitely (parser))
13072 /* If a ptr-operator was found, then this declarator was not
13074 if (parenthesized_p)
13075 *parenthesized_p = true;
13076 /* The dependent declarator is optional if we are parsing an
13077 abstract-declarator. */
13078 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13079 cp_parser_parse_tentatively (parser);
13081 /* Parse the dependent declarator. */
13082 declarator = cp_parser_declarator (parser, dcl_kind,
13083 /*ctor_dtor_or_conv_p=*/NULL,
13084 /*parenthesized_p=*/NULL,
13085 /*member_p=*/false);
13087 /* If we are parsing an abstract-declarator, we must handle the
13088 case where the dependent declarator is absent. */
13089 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13090 && !cp_parser_parse_definitely (parser))
13093 declarator = cp_parser_make_indirect_declarator
13094 (code, class_type, cv_quals, declarator);
13096 /* Everything else is a direct-declarator. */
13099 if (parenthesized_p)
13100 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13102 declarator = cp_parser_direct_declarator (parser, dcl_kind,
13103 ctor_dtor_or_conv_p,
13107 if (attributes && declarator && declarator != cp_error_declarator)
13108 declarator->attributes = attributes;
13113 /* Parse a direct-declarator or direct-abstract-declarator.
13117 direct-declarator ( parameter-declaration-clause )
13118 cv-qualifier-seq [opt]
13119 exception-specification [opt]
13120 direct-declarator [ constant-expression [opt] ]
13123 direct-abstract-declarator:
13124 direct-abstract-declarator [opt]
13125 ( parameter-declaration-clause )
13126 cv-qualifier-seq [opt]
13127 exception-specification [opt]
13128 direct-abstract-declarator [opt] [ constant-expression [opt] ]
13129 ( abstract-declarator )
13131 Returns a representation of the declarator. DCL_KIND is
13132 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13133 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
13134 we are parsing a direct-declarator. It is
13135 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13136 of ambiguity we prefer an abstract declarator, as per
13137 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13138 cp_parser_declarator. */
13140 static cp_declarator *
13141 cp_parser_direct_declarator (cp_parser* parser,
13142 cp_parser_declarator_kind dcl_kind,
13143 int* ctor_dtor_or_conv_p,
13147 cp_declarator *declarator = NULL;
13148 tree scope = NULL_TREE;
13149 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13150 bool saved_in_declarator_p = parser->in_declarator_p;
13152 tree pushed_scope = NULL_TREE;
13156 /* Peek at the next token. */
13157 token = cp_lexer_peek_token (parser->lexer);
13158 if (token->type == CPP_OPEN_PAREN)
13160 /* This is either a parameter-declaration-clause, or a
13161 parenthesized declarator. When we know we are parsing a
13162 named declarator, it must be a parenthesized declarator
13163 if FIRST is true. For instance, `(int)' is a
13164 parameter-declaration-clause, with an omitted
13165 direct-abstract-declarator. But `((*))', is a
13166 parenthesized abstract declarator. Finally, when T is a
13167 template parameter `(T)' is a
13168 parameter-declaration-clause, and not a parenthesized
13171 We first try and parse a parameter-declaration-clause,
13172 and then try a nested declarator (if FIRST is true).
13174 It is not an error for it not to be a
13175 parameter-declaration-clause, even when FIRST is
13181 The first is the declaration of a function while the
13182 second is the definition of a variable, including its
13185 Having seen only the parenthesis, we cannot know which of
13186 these two alternatives should be selected. Even more
13187 complex are examples like:
13192 The former is a function-declaration; the latter is a
13193 variable initialization.
13195 Thus again, we try a parameter-declaration-clause, and if
13196 that fails, we back out and return. */
13198 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13201 unsigned saved_num_template_parameter_lists;
13202 bool is_declarator = false;
13205 /* In a member-declarator, the only valid interpretation
13206 of a parenthesis is the start of a
13207 parameter-declaration-clause. (It is invalid to
13208 initialize a static data member with a parenthesized
13209 initializer; only the "=" form of initialization is
13212 cp_parser_parse_tentatively (parser);
13214 /* Consume the `('. */
13215 cp_lexer_consume_token (parser->lexer);
13218 /* If this is going to be an abstract declarator, we're
13219 in a declarator and we can't have default args. */
13220 parser->default_arg_ok_p = false;
13221 parser->in_declarator_p = true;
13224 /* Inside the function parameter list, surrounding
13225 template-parameter-lists do not apply. */
13226 saved_num_template_parameter_lists
13227 = parser->num_template_parameter_lists;
13228 parser->num_template_parameter_lists = 0;
13230 begin_scope (sk_function_parms, NULL_TREE);
13232 /* Parse the parameter-declaration-clause. */
13233 params = cp_parser_parameter_declaration_clause (parser);
13235 parser->num_template_parameter_lists
13236 = saved_num_template_parameter_lists;
13238 /* If all went well, parse the cv-qualifier-seq and the
13239 exception-specification. */
13240 if (member_p || cp_parser_parse_definitely (parser))
13242 cp_cv_quals cv_quals;
13243 tree exception_specification;
13246 is_declarator = true;
13248 if (ctor_dtor_or_conv_p)
13249 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13251 /* Consume the `)'. */
13252 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13254 /* Parse the cv-qualifier-seq. */
13255 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13256 /* And the exception-specification. */
13257 exception_specification
13258 = cp_parser_exception_specification_opt (parser);
13261 = cp_parser_late_return_type_opt (parser);
13263 /* Create the function-declarator. */
13264 declarator = make_call_declarator (declarator,
13267 exception_specification,
13269 /* Any subsequent parameter lists are to do with
13270 return type, so are not those of the declared
13272 parser->default_arg_ok_p = false;
13275 /* Remove the function parms from scope. */
13276 for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13277 pop_binding (DECL_NAME (t), t);
13281 /* Repeat the main loop. */
13285 /* If this is the first, we can try a parenthesized
13289 bool saved_in_type_id_in_expr_p;
13291 parser->default_arg_ok_p = saved_default_arg_ok_p;
13292 parser->in_declarator_p = saved_in_declarator_p;
13294 /* Consume the `('. */
13295 cp_lexer_consume_token (parser->lexer);
13296 /* Parse the nested declarator. */
13297 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13298 parser->in_type_id_in_expr_p = true;
13300 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13301 /*parenthesized_p=*/NULL,
13303 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13305 /* Expect a `)'. */
13306 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13307 declarator = cp_error_declarator;
13308 if (declarator == cp_error_declarator)
13311 goto handle_declarator;
13313 /* Otherwise, we must be done. */
13317 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13318 && token->type == CPP_OPEN_SQUARE)
13320 /* Parse an array-declarator. */
13323 if (ctor_dtor_or_conv_p)
13324 *ctor_dtor_or_conv_p = 0;
13327 parser->default_arg_ok_p = false;
13328 parser->in_declarator_p = true;
13329 /* Consume the `['. */
13330 cp_lexer_consume_token (parser->lexer);
13331 /* Peek at the next token. */
13332 token = cp_lexer_peek_token (parser->lexer);
13333 /* If the next token is `]', then there is no
13334 constant-expression. */
13335 if (token->type != CPP_CLOSE_SQUARE)
13337 bool non_constant_p;
13340 = cp_parser_constant_expression (parser,
13341 /*allow_non_constant=*/true,
13343 if (!non_constant_p)
13344 bounds = fold_non_dependent_expr (bounds);
13345 /* Normally, the array bound must be an integral constant
13346 expression. However, as an extension, we allow VLAs
13347 in function scopes. */
13348 else if (!parser->in_function_body)
13350 error ("%Harray bound is not an integer constant",
13352 bounds = error_mark_node;
13354 else if (processing_template_decl && !error_operand_p (bounds))
13356 /* Remember this wasn't a constant-expression. */
13357 bounds = build_nop (TREE_TYPE (bounds), bounds);
13358 TREE_SIDE_EFFECTS (bounds) = 1;
13362 bounds = NULL_TREE;
13363 /* Look for the closing `]'. */
13364 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
13366 declarator = cp_error_declarator;
13370 declarator = make_array_declarator (declarator, bounds);
13372 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
13375 tree qualifying_scope;
13376 tree unqualified_name;
13377 special_function_kind sfk;
13379 bool pack_expansion_p = false;
13380 cp_token *declarator_id_start_token;
13382 /* Parse a declarator-id */
13383 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
13386 cp_parser_parse_tentatively (parser);
13388 /* If we see an ellipsis, we should be looking at a
13390 if (token->type == CPP_ELLIPSIS)
13392 /* Consume the `...' */
13393 cp_lexer_consume_token (parser->lexer);
13395 pack_expansion_p = true;
13399 declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
13401 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
13402 qualifying_scope = parser->scope;
13407 if (!unqualified_name && pack_expansion_p)
13409 /* Check whether an error occurred. */
13410 okay = !cp_parser_error_occurred (parser);
13412 /* We already consumed the ellipsis to mark a
13413 parameter pack, but we have no way to report it,
13414 so abort the tentative parse. We will be exiting
13415 immediately anyway. */
13416 cp_parser_abort_tentative_parse (parser);
13419 okay = cp_parser_parse_definitely (parser);
13422 unqualified_name = error_mark_node;
13423 else if (unqualified_name
13424 && (qualifying_scope
13425 || (TREE_CODE (unqualified_name)
13426 != IDENTIFIER_NODE)))
13428 cp_parser_error (parser, "expected unqualified-id");
13429 unqualified_name = error_mark_node;
13433 if (!unqualified_name)
13435 if (unqualified_name == error_mark_node)
13437 declarator = cp_error_declarator;
13438 pack_expansion_p = false;
13439 declarator->parameter_pack_p = false;
13443 if (qualifying_scope && at_namespace_scope_p ()
13444 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
13446 /* In the declaration of a member of a template class
13447 outside of the class itself, the SCOPE will sometimes
13448 be a TYPENAME_TYPE. For example, given:
13450 template <typename T>
13451 int S<T>::R::i = 3;
13453 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
13454 this context, we must resolve S<T>::R to an ordinary
13455 type, rather than a typename type.
13457 The reason we normally avoid resolving TYPENAME_TYPEs
13458 is that a specialization of `S' might render
13459 `S<T>::R' not a type. However, if `S' is
13460 specialized, then this `i' will not be used, so there
13461 is no harm in resolving the types here. */
13464 /* Resolve the TYPENAME_TYPE. */
13465 type = resolve_typename_type (qualifying_scope,
13466 /*only_current_p=*/false);
13467 /* If that failed, the declarator is invalid. */
13468 if (TREE_CODE (type) == TYPENAME_TYPE)
13469 error ("%H%<%T::%E%> is not a type",
13470 &declarator_id_start_token->location,
13471 TYPE_CONTEXT (qualifying_scope),
13472 TYPE_IDENTIFIER (qualifying_scope));
13473 qualifying_scope = type;
13478 if (unqualified_name)
13482 if (qualifying_scope
13483 && CLASS_TYPE_P (qualifying_scope))
13484 class_type = qualifying_scope;
13486 class_type = current_class_type;
13488 if (TREE_CODE (unqualified_name) == TYPE_DECL)
13490 tree name_type = TREE_TYPE (unqualified_name);
13491 if (class_type && same_type_p (name_type, class_type))
13493 if (qualifying_scope
13494 && CLASSTYPE_USE_TEMPLATE (name_type))
13496 error ("%Hinvalid use of constructor as a template",
13497 &declarator_id_start_token->location);
13498 inform (input_location, "use %<%T::%D%> instead of %<%T::%D%> to "
13499 "name the constructor in a qualified name",
13501 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
13502 class_type, name_type);
13503 declarator = cp_error_declarator;
13507 unqualified_name = constructor_name (class_type);
13511 /* We do not attempt to print the declarator
13512 here because we do not have enough
13513 information about its original syntactic
13515 cp_parser_error (parser, "invalid declarator");
13516 declarator = cp_error_declarator;
13523 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
13524 sfk = sfk_destructor;
13525 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
13526 sfk = sfk_conversion;
13527 else if (/* There's no way to declare a constructor
13528 for an anonymous type, even if the type
13529 got a name for linkage purposes. */
13530 !TYPE_WAS_ANONYMOUS (class_type)
13531 && constructor_name_p (unqualified_name,
13534 unqualified_name = constructor_name (class_type);
13535 sfk = sfk_constructor;
13538 if (ctor_dtor_or_conv_p && sfk != sfk_none)
13539 *ctor_dtor_or_conv_p = -1;
13542 declarator = make_id_declarator (qualifying_scope,
13545 declarator->id_loc = token->location;
13546 declarator->parameter_pack_p = pack_expansion_p;
13548 if (pack_expansion_p)
13549 maybe_warn_variadic_templates ();
13552 handle_declarator:;
13553 scope = get_scope_of_declarator (declarator);
13555 /* Any names that appear after the declarator-id for a
13556 member are looked up in the containing scope. */
13557 pushed_scope = push_scope (scope);
13558 parser->in_declarator_p = true;
13559 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
13560 || (declarator && declarator->kind == cdk_id))
13561 /* Default args are only allowed on function
13563 parser->default_arg_ok_p = saved_default_arg_ok_p;
13565 parser->default_arg_ok_p = false;
13574 /* For an abstract declarator, we might wind up with nothing at this
13575 point. That's an error; the declarator is not optional. */
13577 cp_parser_error (parser, "expected declarator");
13579 /* If we entered a scope, we must exit it now. */
13581 pop_scope (pushed_scope);
13583 parser->default_arg_ok_p = saved_default_arg_ok_p;
13584 parser->in_declarator_p = saved_in_declarator_p;
13589 /* Parse a ptr-operator.
13592 * cv-qualifier-seq [opt]
13594 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
13599 & cv-qualifier-seq [opt]
13601 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
13602 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
13603 an rvalue reference. In the case of a pointer-to-member, *TYPE is
13604 filled in with the TYPE containing the member. *CV_QUALS is
13605 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
13606 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
13607 Note that the tree codes returned by this function have nothing
13608 to do with the types of trees that will be eventually be created
13609 to represent the pointer or reference type being parsed. They are
13610 just constants with suggestive names. */
13611 static enum tree_code
13612 cp_parser_ptr_operator (cp_parser* parser,
13614 cp_cv_quals *cv_quals)
13616 enum tree_code code = ERROR_MARK;
13619 /* Assume that it's not a pointer-to-member. */
13621 /* And that there are no cv-qualifiers. */
13622 *cv_quals = TYPE_UNQUALIFIED;
13624 /* Peek at the next token. */
13625 token = cp_lexer_peek_token (parser->lexer);
13627 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
13628 if (token->type == CPP_MULT)
13629 code = INDIRECT_REF;
13630 else if (token->type == CPP_AND)
13632 else if ((cxx_dialect != cxx98) &&
13633 token->type == CPP_AND_AND) /* C++0x only */
13634 code = NON_LVALUE_EXPR;
13636 if (code != ERROR_MARK)
13638 /* Consume the `*', `&' or `&&'. */
13639 cp_lexer_consume_token (parser->lexer);
13641 /* A `*' can be followed by a cv-qualifier-seq, and so can a
13642 `&', if we are allowing GNU extensions. (The only qualifier
13643 that can legally appear after `&' is `restrict', but that is
13644 enforced during semantic analysis. */
13645 if (code == INDIRECT_REF
13646 || cp_parser_allow_gnu_extensions_p (parser))
13647 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13651 /* Try the pointer-to-member case. */
13652 cp_parser_parse_tentatively (parser);
13653 /* Look for the optional `::' operator. */
13654 cp_parser_global_scope_opt (parser,
13655 /*current_scope_valid_p=*/false);
13656 /* Look for the nested-name specifier. */
13657 token = cp_lexer_peek_token (parser->lexer);
13658 cp_parser_nested_name_specifier (parser,
13659 /*typename_keyword_p=*/false,
13660 /*check_dependency_p=*/true,
13662 /*is_declaration=*/false);
13663 /* If we found it, and the next token is a `*', then we are
13664 indeed looking at a pointer-to-member operator. */
13665 if (!cp_parser_error_occurred (parser)
13666 && cp_parser_require (parser, CPP_MULT, "%<*%>"))
13668 /* Indicate that the `*' operator was used. */
13669 code = INDIRECT_REF;
13671 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13672 error ("%H%qD is a namespace", &token->location, parser->scope);
13675 /* The type of which the member is a member is given by the
13677 *type = parser->scope;
13678 /* The next name will not be qualified. */
13679 parser->scope = NULL_TREE;
13680 parser->qualifying_scope = NULL_TREE;
13681 parser->object_scope = NULL_TREE;
13682 /* Look for the optional cv-qualifier-seq. */
13683 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13686 /* If that didn't work we don't have a ptr-operator. */
13687 if (!cp_parser_parse_definitely (parser))
13688 cp_parser_error (parser, "expected ptr-operator");
13694 /* Parse an (optional) cv-qualifier-seq.
13697 cv-qualifier cv-qualifier-seq [opt]
13708 Returns a bitmask representing the cv-qualifiers. */
13711 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13713 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13718 cp_cv_quals cv_qualifier;
13720 /* Peek at the next token. */
13721 token = cp_lexer_peek_token (parser->lexer);
13722 /* See if it's a cv-qualifier. */
13723 switch (token->keyword)
13726 cv_qualifier = TYPE_QUAL_CONST;
13730 cv_qualifier = TYPE_QUAL_VOLATILE;
13734 cv_qualifier = TYPE_QUAL_RESTRICT;
13738 cv_qualifier = TYPE_UNQUALIFIED;
13745 if (cv_quals & cv_qualifier)
13747 error ("%Hduplicate cv-qualifier", &token->location);
13748 cp_lexer_purge_token (parser->lexer);
13752 cp_lexer_consume_token (parser->lexer);
13753 cv_quals |= cv_qualifier;
13760 /* Parse a late-specified return type, if any. This is not a separate
13761 non-terminal, but part of a function declarator, which looks like
13765 Returns the type indicated by the type-id. */
13768 cp_parser_late_return_type_opt (cp_parser* parser)
13772 /* Peek at the next token. */
13773 token = cp_lexer_peek_token (parser->lexer);
13774 /* A late-specified return type is indicated by an initial '->'. */
13775 if (token->type != CPP_DEREF)
13778 /* Consume the ->. */
13779 cp_lexer_consume_token (parser->lexer);
13781 return cp_parser_type_id (parser);
13784 /* Parse a declarator-id.
13788 :: [opt] nested-name-specifier [opt] type-name
13790 In the `id-expression' case, the value returned is as for
13791 cp_parser_id_expression if the id-expression was an unqualified-id.
13792 If the id-expression was a qualified-id, then a SCOPE_REF is
13793 returned. The first operand is the scope (either a NAMESPACE_DECL
13794 or TREE_TYPE), but the second is still just a representation of an
13798 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13801 /* The expression must be an id-expression. Assume that qualified
13802 names are the names of types so that:
13805 int S<T>::R::i = 3;
13807 will work; we must treat `S<T>::R' as the name of a type.
13808 Similarly, assume that qualified names are templates, where
13812 int S<T>::R<T>::i = 3;
13815 id = cp_parser_id_expression (parser,
13816 /*template_keyword_p=*/false,
13817 /*check_dependency_p=*/false,
13818 /*template_p=*/NULL,
13819 /*declarator_p=*/true,
13821 if (id && BASELINK_P (id))
13822 id = BASELINK_FUNCTIONS (id);
13826 /* Parse a type-id.
13829 type-specifier-seq abstract-declarator [opt]
13831 Returns the TYPE specified. */
13834 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg)
13836 cp_decl_specifier_seq type_specifier_seq;
13837 cp_declarator *abstract_declarator;
13839 /* Parse the type-specifier-seq. */
13840 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13841 &type_specifier_seq);
13842 if (type_specifier_seq.type == error_mark_node)
13843 return error_mark_node;
13845 /* There might or might not be an abstract declarator. */
13846 cp_parser_parse_tentatively (parser);
13847 /* Look for the declarator. */
13848 abstract_declarator
13849 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13850 /*parenthesized_p=*/NULL,
13851 /*member_p=*/false);
13852 /* Check to see if there really was a declarator. */
13853 if (!cp_parser_parse_definitely (parser))
13854 abstract_declarator = NULL;
13856 if (type_specifier_seq.type
13857 && type_uses_auto (type_specifier_seq.type))
13859 /* A type-id with type 'auto' is only ok if the abstract declarator
13860 is a function declarator with a late-specified return type. */
13861 if (abstract_declarator
13862 && abstract_declarator->kind == cdk_function
13863 && abstract_declarator->u.function.late_return_type)
13867 error ("invalid use of %<auto%>");
13868 return error_mark_node;
13872 return groktypename (&type_specifier_seq, abstract_declarator,
13876 static tree cp_parser_type_id (cp_parser *parser)
13878 return cp_parser_type_id_1 (parser, false);
13881 static tree cp_parser_template_type_arg (cp_parser *parser)
13883 return cp_parser_type_id_1 (parser, true);
13886 /* Parse a type-specifier-seq.
13888 type-specifier-seq:
13889 type-specifier type-specifier-seq [opt]
13893 type-specifier-seq:
13894 attributes type-specifier-seq [opt]
13896 If IS_CONDITION is true, we are at the start of a "condition",
13897 e.g., we've just seen "if (".
13899 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13902 cp_parser_type_specifier_seq (cp_parser* parser,
13904 cp_decl_specifier_seq *type_specifier_seq)
13906 bool seen_type_specifier = false;
13907 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13908 cp_token *start_token = NULL;
13910 /* Clear the TYPE_SPECIFIER_SEQ. */
13911 clear_decl_specs (type_specifier_seq);
13913 /* Parse the type-specifiers and attributes. */
13916 tree type_specifier;
13917 bool is_cv_qualifier;
13919 /* Check for attributes first. */
13920 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13922 type_specifier_seq->attributes =
13923 chainon (type_specifier_seq->attributes,
13924 cp_parser_attributes_opt (parser));
13928 /* record the token of the beginning of the type specifier seq,
13929 for error reporting purposes*/
13931 start_token = cp_lexer_peek_token (parser->lexer);
13933 /* Look for the type-specifier. */
13934 type_specifier = cp_parser_type_specifier (parser,
13936 type_specifier_seq,
13937 /*is_declaration=*/false,
13940 if (!type_specifier)
13942 /* If the first type-specifier could not be found, this is not a
13943 type-specifier-seq at all. */
13944 if (!seen_type_specifier)
13946 cp_parser_error (parser, "expected type-specifier");
13947 type_specifier_seq->type = error_mark_node;
13950 /* If subsequent type-specifiers could not be found, the
13951 type-specifier-seq is complete. */
13955 seen_type_specifier = true;
13956 /* The standard says that a condition can be:
13958 type-specifier-seq declarator = assignment-expression
13965 we should treat the "S" as a declarator, not as a
13966 type-specifier. The standard doesn't say that explicitly for
13967 type-specifier-seq, but it does say that for
13968 decl-specifier-seq in an ordinary declaration. Perhaps it
13969 would be clearer just to allow a decl-specifier-seq here, and
13970 then add a semantic restriction that if any decl-specifiers
13971 that are not type-specifiers appear, the program is invalid. */
13972 if (is_condition && !is_cv_qualifier)
13973 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13976 cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
13979 /* Parse a parameter-declaration-clause.
13981 parameter-declaration-clause:
13982 parameter-declaration-list [opt] ... [opt]
13983 parameter-declaration-list , ...
13985 Returns a representation for the parameter declarations. A return
13986 value of NULL indicates a parameter-declaration-clause consisting
13987 only of an ellipsis. */
13990 cp_parser_parameter_declaration_clause (cp_parser* parser)
13997 /* Peek at the next token. */
13998 token = cp_lexer_peek_token (parser->lexer);
13999 /* Check for trivial parameter-declaration-clauses. */
14000 if (token->type == CPP_ELLIPSIS)
14002 /* Consume the `...' token. */
14003 cp_lexer_consume_token (parser->lexer);
14006 else if (token->type == CPP_CLOSE_PAREN)
14007 /* There are no parameters. */
14009 #ifndef NO_IMPLICIT_EXTERN_C
14010 if (in_system_header && current_class_type == NULL
14011 && current_lang_name == lang_name_c)
14015 return void_list_node;
14017 /* Check for `(void)', too, which is a special case. */
14018 else if (token->keyword == RID_VOID
14019 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14020 == CPP_CLOSE_PAREN))
14022 /* Consume the `void' token. */
14023 cp_lexer_consume_token (parser->lexer);
14024 /* There are no parameters. */
14025 return void_list_node;
14028 /* Parse the parameter-declaration-list. */
14029 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14030 /* If a parse error occurred while parsing the
14031 parameter-declaration-list, then the entire
14032 parameter-declaration-clause is erroneous. */
14036 /* Peek at the next token. */
14037 token = cp_lexer_peek_token (parser->lexer);
14038 /* If it's a `,', the clause should terminate with an ellipsis. */
14039 if (token->type == CPP_COMMA)
14041 /* Consume the `,'. */
14042 cp_lexer_consume_token (parser->lexer);
14043 /* Expect an ellipsis. */
14045 = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14047 /* It might also be `...' if the optional trailing `,' was
14049 else if (token->type == CPP_ELLIPSIS)
14051 /* Consume the `...' token. */
14052 cp_lexer_consume_token (parser->lexer);
14053 /* And remember that we saw it. */
14057 ellipsis_p = false;
14059 /* Finish the parameter list. */
14061 parameters = chainon (parameters, void_list_node);
14066 /* Parse a parameter-declaration-list.
14068 parameter-declaration-list:
14069 parameter-declaration
14070 parameter-declaration-list , parameter-declaration
14072 Returns a representation of the parameter-declaration-list, as for
14073 cp_parser_parameter_declaration_clause. However, the
14074 `void_list_node' is never appended to the list. Upon return,
14075 *IS_ERROR will be true iff an error occurred. */
14078 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14080 tree parameters = NULL_TREE;
14081 tree *tail = ¶meters;
14082 bool saved_in_unbraced_linkage_specification_p;
14084 /* Assume all will go well. */
14086 /* The special considerations that apply to a function within an
14087 unbraced linkage specifications do not apply to the parameters
14088 to the function. */
14089 saved_in_unbraced_linkage_specification_p
14090 = parser->in_unbraced_linkage_specification_p;
14091 parser->in_unbraced_linkage_specification_p = false;
14093 /* Look for more parameters. */
14096 cp_parameter_declarator *parameter;
14097 tree decl = error_mark_node;
14098 bool parenthesized_p;
14099 /* Parse the parameter. */
14101 = cp_parser_parameter_declaration (parser,
14102 /*template_parm_p=*/false,
14105 /* We don't know yet if the enclosing context is deprecated, so wait
14106 and warn in grokparms if appropriate. */
14107 deprecated_state = DEPRECATED_SUPPRESS;
14110 decl = grokdeclarator (parameter->declarator,
14111 ¶meter->decl_specifiers,
14113 parameter->default_argument != NULL_TREE,
14114 ¶meter->decl_specifiers.attributes);
14116 deprecated_state = DEPRECATED_NORMAL;
14118 /* If a parse error occurred parsing the parameter declaration,
14119 then the entire parameter-declaration-list is erroneous. */
14120 if (decl == error_mark_node)
14123 parameters = error_mark_node;
14127 if (parameter->decl_specifiers.attributes)
14128 cplus_decl_attributes (&decl,
14129 parameter->decl_specifiers.attributes,
14131 if (DECL_NAME (decl))
14132 decl = pushdecl (decl);
14134 /* Add the new parameter to the list. */
14135 *tail = build_tree_list (parameter->default_argument, decl);
14136 tail = &TREE_CHAIN (*tail);
14138 /* Peek at the next token. */
14139 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14140 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14141 /* These are for Objective-C++ */
14142 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14143 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14144 /* The parameter-declaration-list is complete. */
14146 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14150 /* Peek at the next token. */
14151 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14152 /* If it's an ellipsis, then the list is complete. */
14153 if (token->type == CPP_ELLIPSIS)
14155 /* Otherwise, there must be more parameters. Consume the
14157 cp_lexer_consume_token (parser->lexer);
14158 /* When parsing something like:
14160 int i(float f, double d)
14162 we can tell after seeing the declaration for "f" that we
14163 are not looking at an initialization of a variable "i",
14164 but rather at the declaration of a function "i".
14166 Due to the fact that the parsing of template arguments
14167 (as specified to a template-id) requires backtracking we
14168 cannot use this technique when inside a template argument
14170 if (!parser->in_template_argument_list_p
14171 && !parser->in_type_id_in_expr_p
14172 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14173 /* However, a parameter-declaration of the form
14174 "foat(f)" (which is a valid declaration of a
14175 parameter "f") can also be interpreted as an
14176 expression (the conversion of "f" to "float"). */
14177 && !parenthesized_p)
14178 cp_parser_commit_to_tentative_parse (parser);
14182 cp_parser_error (parser, "expected %<,%> or %<...%>");
14183 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14184 cp_parser_skip_to_closing_parenthesis (parser,
14185 /*recovering=*/true,
14186 /*or_comma=*/false,
14187 /*consume_paren=*/false);
14192 parser->in_unbraced_linkage_specification_p
14193 = saved_in_unbraced_linkage_specification_p;
14198 /* Parse a parameter declaration.
14200 parameter-declaration:
14201 decl-specifier-seq ... [opt] declarator
14202 decl-specifier-seq declarator = assignment-expression
14203 decl-specifier-seq ... [opt] abstract-declarator [opt]
14204 decl-specifier-seq abstract-declarator [opt] = assignment-expression
14206 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14207 declares a template parameter. (In that case, a non-nested `>'
14208 token encountered during the parsing of the assignment-expression
14209 is not interpreted as a greater-than operator.)
14211 Returns a representation of the parameter, or NULL if an error
14212 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14213 true iff the declarator is of the form "(p)". */
14215 static cp_parameter_declarator *
14216 cp_parser_parameter_declaration (cp_parser *parser,
14217 bool template_parm_p,
14218 bool *parenthesized_p)
14220 int declares_class_or_enum;
14221 bool greater_than_is_operator_p;
14222 cp_decl_specifier_seq decl_specifiers;
14223 cp_declarator *declarator;
14224 tree default_argument;
14225 cp_token *token = NULL, *declarator_token_start = NULL;
14226 const char *saved_message;
14228 /* In a template parameter, `>' is not an operator.
14232 When parsing a default template-argument for a non-type
14233 template-parameter, the first non-nested `>' is taken as the end
14234 of the template parameter-list rather than a greater-than
14236 greater_than_is_operator_p = !template_parm_p;
14238 /* Type definitions may not appear in parameter types. */
14239 saved_message = parser->type_definition_forbidden_message;
14240 parser->type_definition_forbidden_message
14241 = "types may not be defined in parameter types";
14243 /* Parse the declaration-specifiers. */
14244 cp_parser_decl_specifier_seq (parser,
14245 CP_PARSER_FLAGS_NONE,
14247 &declares_class_or_enum);
14248 /* If an error occurred, there's no reason to attempt to parse the
14249 rest of the declaration. */
14250 if (cp_parser_error_occurred (parser))
14252 parser->type_definition_forbidden_message = saved_message;
14256 /* Peek at the next token. */
14257 token = cp_lexer_peek_token (parser->lexer);
14259 /* If the next token is a `)', `,', `=', `>', or `...', then there
14260 is no declarator. However, when variadic templates are enabled,
14261 there may be a declarator following `...'. */
14262 if (token->type == CPP_CLOSE_PAREN
14263 || token->type == CPP_COMMA
14264 || token->type == CPP_EQ
14265 || token->type == CPP_GREATER)
14268 if (parenthesized_p)
14269 *parenthesized_p = false;
14271 /* Otherwise, there should be a declarator. */
14274 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14275 parser->default_arg_ok_p = false;
14277 /* After seeing a decl-specifier-seq, if the next token is not a
14278 "(", there is no possibility that the code is a valid
14279 expression. Therefore, if parsing tentatively, we commit at
14281 if (!parser->in_template_argument_list_p
14282 /* In an expression context, having seen:
14286 we cannot be sure whether we are looking at a
14287 function-type (taking a "char" as a parameter) or a cast
14288 of some object of type "char" to "int". */
14289 && !parser->in_type_id_in_expr_p
14290 && cp_parser_uncommitted_to_tentative_parse_p (parser)
14291 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14292 cp_parser_commit_to_tentative_parse (parser);
14293 /* Parse the declarator. */
14294 declarator_token_start = token;
14295 declarator = cp_parser_declarator (parser,
14296 CP_PARSER_DECLARATOR_EITHER,
14297 /*ctor_dtor_or_conv_p=*/NULL,
14299 /*member_p=*/false);
14300 parser->default_arg_ok_p = saved_default_arg_ok_p;
14301 /* After the declarator, allow more attributes. */
14302 decl_specifiers.attributes
14303 = chainon (decl_specifiers.attributes,
14304 cp_parser_attributes_opt (parser));
14307 /* If the next token is an ellipsis, and we have not seen a
14308 declarator name, and the type of the declarator contains parameter
14309 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
14310 a parameter pack expansion expression. Otherwise, leave the
14311 ellipsis for a C-style variadic function. */
14312 token = cp_lexer_peek_token (parser->lexer);
14313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14315 tree type = decl_specifiers.type;
14317 if (type && DECL_P (type))
14318 type = TREE_TYPE (type);
14321 && TREE_CODE (type) != TYPE_PACK_EXPANSION
14322 && declarator_can_be_parameter_pack (declarator)
14323 && (!declarator || !declarator->parameter_pack_p)
14324 && uses_parameter_packs (type))
14326 /* Consume the `...'. */
14327 cp_lexer_consume_token (parser->lexer);
14328 maybe_warn_variadic_templates ();
14330 /* Build a pack expansion type */
14332 declarator->parameter_pack_p = true;
14334 decl_specifiers.type = make_pack_expansion (type);
14338 /* The restriction on defining new types applies only to the type
14339 of the parameter, not to the default argument. */
14340 parser->type_definition_forbidden_message = saved_message;
14342 /* If the next token is `=', then process a default argument. */
14343 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14345 /* Consume the `='. */
14346 cp_lexer_consume_token (parser->lexer);
14348 /* If we are defining a class, then the tokens that make up the
14349 default argument must be saved and processed later. */
14350 if (!template_parm_p && at_class_scope_p ()
14351 && TYPE_BEING_DEFINED (current_class_type))
14353 unsigned depth = 0;
14354 int maybe_template_id = 0;
14355 cp_token *first_token;
14358 /* Add tokens until we have processed the entire default
14359 argument. We add the range [first_token, token). */
14360 first_token = cp_lexer_peek_token (parser->lexer);
14365 /* Peek at the next token. */
14366 token = cp_lexer_peek_token (parser->lexer);
14367 /* What we do depends on what token we have. */
14368 switch (token->type)
14370 /* In valid code, a default argument must be
14371 immediately followed by a `,' `)', or `...'. */
14373 if (depth == 0 && maybe_template_id)
14375 /* If we've seen a '<', we might be in a
14376 template-argument-list. Until Core issue 325 is
14377 resolved, we don't know how this situation ought
14378 to be handled, so try to DTRT. We check whether
14379 what comes after the comma is a valid parameter
14380 declaration list. If it is, then the comma ends
14381 the default argument; otherwise the default
14382 argument continues. */
14383 bool error = false;
14385 /* Set ITALP so cp_parser_parameter_declaration_list
14386 doesn't decide to commit to this parse. */
14387 bool saved_italp = parser->in_template_argument_list_p;
14388 parser->in_template_argument_list_p = true;
14390 cp_parser_parse_tentatively (parser);
14391 cp_lexer_consume_token (parser->lexer);
14392 cp_parser_parameter_declaration_list (parser, &error);
14393 if (!cp_parser_error_occurred (parser) && !error)
14395 cp_parser_abort_tentative_parse (parser);
14397 parser->in_template_argument_list_p = saved_italp;
14400 case CPP_CLOSE_PAREN:
14402 /* If we run into a non-nested `;', `}', or `]',
14403 then the code is invalid -- but the default
14404 argument is certainly over. */
14405 case CPP_SEMICOLON:
14406 case CPP_CLOSE_BRACE:
14407 case CPP_CLOSE_SQUARE:
14410 /* Update DEPTH, if necessary. */
14411 else if (token->type == CPP_CLOSE_PAREN
14412 || token->type == CPP_CLOSE_BRACE
14413 || token->type == CPP_CLOSE_SQUARE)
14417 case CPP_OPEN_PAREN:
14418 case CPP_OPEN_SQUARE:
14419 case CPP_OPEN_BRACE:
14425 /* This might be the comparison operator, or it might
14426 start a template argument list. */
14427 ++maybe_template_id;
14431 if (cxx_dialect == cxx98)
14433 /* Fall through for C++0x, which treats the `>>'
14434 operator like two `>' tokens in certain
14440 /* This might be an operator, or it might close a
14441 template argument list. But if a previous '<'
14442 started a template argument list, this will have
14443 closed it, so we can't be in one anymore. */
14444 maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
14445 if (maybe_template_id < 0)
14446 maybe_template_id = 0;
14450 /* If we run out of tokens, issue an error message. */
14452 case CPP_PRAGMA_EOL:
14453 error ("%Hfile ends in default argument", &token->location);
14459 /* In these cases, we should look for template-ids.
14460 For example, if the default argument is
14461 `X<int, double>()', we need to do name lookup to
14462 figure out whether or not `X' is a template; if
14463 so, the `,' does not end the default argument.
14465 That is not yet done. */
14472 /* If we've reached the end, stop. */
14476 /* Add the token to the token block. */
14477 token = cp_lexer_consume_token (parser->lexer);
14480 /* Create a DEFAULT_ARG to represent the unparsed default
14482 default_argument = make_node (DEFAULT_ARG);
14483 DEFARG_TOKENS (default_argument)
14484 = cp_token_cache_new (first_token, token);
14485 DEFARG_INSTANTIATIONS (default_argument) = NULL;
14487 /* Outside of a class definition, we can just parse the
14488 assignment-expression. */
14491 token = cp_lexer_peek_token (parser->lexer);
14493 = cp_parser_default_argument (parser, template_parm_p);
14496 if (!parser->default_arg_ok_p)
14498 if (flag_permissive)
14499 warning (0, "deprecated use of default argument for parameter of non-function");
14502 error ("%Hdefault arguments are only "
14503 "permitted for function parameters",
14505 default_argument = NULL_TREE;
14508 else if ((declarator && declarator->parameter_pack_p)
14509 || (decl_specifiers.type
14510 && PACK_EXPANSION_P (decl_specifiers.type)))
14512 const char* kind = template_parm_p? "template " : "";
14514 /* Find the name of the parameter pack. */
14515 cp_declarator *id_declarator = declarator;
14516 while (id_declarator && id_declarator->kind != cdk_id)
14517 id_declarator = id_declarator->declarator;
14519 if (id_declarator && id_declarator->kind == cdk_id)
14520 error ("%H%sparameter pack %qD cannot have a default argument",
14521 &declarator_token_start->location,
14522 kind, id_declarator->u.id.unqualified_name);
14524 error ("%H%sparameter pack cannot have a default argument",
14525 &declarator_token_start->location, kind);
14527 default_argument = NULL_TREE;
14531 default_argument = NULL_TREE;
14533 return make_parameter_declarator (&decl_specifiers,
14538 /* Parse a default argument and return it.
14540 TEMPLATE_PARM_P is true if this is a default argument for a
14541 non-type template parameter. */
14543 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
14545 tree default_argument = NULL_TREE;
14546 bool saved_greater_than_is_operator_p;
14547 bool saved_local_variables_forbidden_p;
14549 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
14551 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
14552 parser->greater_than_is_operator_p = !template_parm_p;
14553 /* Local variable names (and the `this' keyword) may not
14554 appear in a default argument. */
14555 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14556 parser->local_variables_forbidden_p = true;
14557 /* The default argument expression may cause implicitly
14558 defined member functions to be synthesized, which will
14559 result in garbage collection. We must treat this
14560 situation as if we were within the body of function so as
14561 to avoid collecting live data on the stack. */
14563 /* Parse the assignment-expression. */
14564 if (template_parm_p)
14565 push_deferring_access_checks (dk_no_deferred);
14567 = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
14568 if (template_parm_p)
14569 pop_deferring_access_checks ();
14570 /* Restore saved state. */
14572 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
14573 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14575 return default_argument;
14578 /* Parse a function-body.
14581 compound_statement */
14584 cp_parser_function_body (cp_parser *parser)
14586 cp_parser_compound_statement (parser, NULL, false);
14589 /* Parse a ctor-initializer-opt followed by a function-body. Return
14590 true if a ctor-initializer was present. */
14593 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
14596 bool ctor_initializer_p;
14598 /* Begin the function body. */
14599 body = begin_function_body ();
14600 /* Parse the optional ctor-initializer. */
14601 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
14602 /* Parse the function-body. */
14603 cp_parser_function_body (parser);
14604 /* Finish the function body. */
14605 finish_function_body (body);
14607 return ctor_initializer_p;
14610 /* Parse an initializer.
14613 = initializer-clause
14614 ( expression-list )
14616 Returns an expression representing the initializer. If no
14617 initializer is present, NULL_TREE is returned.
14619 *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
14620 production is used, and TRUE otherwise. *IS_DIRECT_INIT is
14621 set to TRUE if there is no initializer present. If there is an
14622 initializer, and it is not a constant-expression, *NON_CONSTANT_P
14623 is set to true; otherwise it is set to false. */
14626 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
14627 bool* non_constant_p)
14632 /* Peek at the next token. */
14633 token = cp_lexer_peek_token (parser->lexer);
14635 /* Let our caller know whether or not this initializer was
14637 *is_direct_init = (token->type != CPP_EQ);
14638 /* Assume that the initializer is constant. */
14639 *non_constant_p = false;
14641 if (token->type == CPP_EQ)
14643 /* Consume the `='. */
14644 cp_lexer_consume_token (parser->lexer);
14645 /* Parse the initializer-clause. */
14646 init = cp_parser_initializer_clause (parser, non_constant_p);
14648 else if (token->type == CPP_OPEN_PAREN)
14651 vec = cp_parser_parenthesized_expression_list (parser, false,
14653 /*allow_expansion_p=*/true,
14656 return error_mark_node;
14657 init = build_tree_list_vec (vec);
14658 release_tree_vector (vec);
14660 else if (token->type == CPP_OPEN_BRACE)
14662 maybe_warn_cpp0x ("extended initializer lists");
14663 init = cp_parser_braced_list (parser, non_constant_p);
14664 CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
14668 /* Anything else is an error. */
14669 cp_parser_error (parser, "expected initializer");
14670 init = error_mark_node;
14676 /* Parse an initializer-clause.
14678 initializer-clause:
14679 assignment-expression
14682 Returns an expression representing the initializer.
14684 If the `assignment-expression' production is used the value
14685 returned is simply a representation for the expression.
14687 Otherwise, calls cp_parser_braced_list. */
14690 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
14694 /* Assume the expression is constant. */
14695 *non_constant_p = false;
14697 /* If it is not a `{', then we are looking at an
14698 assignment-expression. */
14699 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
14702 = cp_parser_constant_expression (parser,
14703 /*allow_non_constant_p=*/true,
14705 if (!*non_constant_p)
14706 initializer = fold_non_dependent_expr (initializer);
14709 initializer = cp_parser_braced_list (parser, non_constant_p);
14711 return initializer;
14714 /* Parse a brace-enclosed initializer list.
14717 { initializer-list , [opt] }
14720 Returns a CONSTRUCTOR. The CONSTRUCTOR_ELTS will be
14721 the elements of the initializer-list (or NULL, if the last
14722 production is used). The TREE_TYPE for the CONSTRUCTOR will be
14723 NULL_TREE. There is no way to detect whether or not the optional
14724 trailing `,' was provided. NON_CONSTANT_P is as for
14725 cp_parser_initializer. */
14728 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
14732 /* Consume the `{' token. */
14733 cp_lexer_consume_token (parser->lexer);
14734 /* Create a CONSTRUCTOR to represent the braced-initializer. */
14735 initializer = make_node (CONSTRUCTOR);
14736 /* If it's not a `}', then there is a non-trivial initializer. */
14737 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14739 /* Parse the initializer list. */
14740 CONSTRUCTOR_ELTS (initializer)
14741 = cp_parser_initializer_list (parser, non_constant_p);
14742 /* A trailing `,' token is allowed. */
14743 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14744 cp_lexer_consume_token (parser->lexer);
14746 /* Now, there should be a trailing `}'. */
14747 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
14748 TREE_TYPE (initializer) = init_list_type_node;
14749 return initializer;
14752 /* Parse an initializer-list.
14755 initializer-clause ... [opt]
14756 initializer-list , initializer-clause ... [opt]
14761 identifier : initializer-clause
14762 initializer-list, identifier : initializer-clause
14764 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
14765 for the initializer. If the INDEX of the elt is non-NULL, it is the
14766 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
14767 as for cp_parser_initializer. */
14769 static VEC(constructor_elt,gc) *
14770 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
14772 VEC(constructor_elt,gc) *v = NULL;
14774 /* Assume all of the expressions are constant. */
14775 *non_constant_p = false;
14777 /* Parse the rest of the list. */
14783 bool clause_non_constant_p;
14785 /* If the next token is an identifier and the following one is a
14786 colon, we are looking at the GNU designated-initializer
14788 if (cp_parser_allow_gnu_extensions_p (parser)
14789 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
14790 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
14792 /* Warn the user that they are using an extension. */
14793 pedwarn (input_location, OPT_pedantic,
14794 "ISO C++ does not allow designated initializers");
14795 /* Consume the identifier. */
14796 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
14797 /* Consume the `:'. */
14798 cp_lexer_consume_token (parser->lexer);
14801 identifier = NULL_TREE;
14803 /* Parse the initializer. */
14804 initializer = cp_parser_initializer_clause (parser,
14805 &clause_non_constant_p);
14806 /* If any clause is non-constant, so is the entire initializer. */
14807 if (clause_non_constant_p)
14808 *non_constant_p = true;
14810 /* If we have an ellipsis, this is an initializer pack
14812 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14814 /* Consume the `...'. */
14815 cp_lexer_consume_token (parser->lexer);
14817 /* Turn the initializer into an initializer expansion. */
14818 initializer = make_pack_expansion (initializer);
14821 /* Add it to the vector. */
14822 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
14824 /* If the next token is not a comma, we have reached the end of
14826 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14829 /* Peek at the next token. */
14830 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14831 /* If the next token is a `}', then we're still done. An
14832 initializer-clause can have a trailing `,' after the
14833 initializer-list and before the closing `}'. */
14834 if (token->type == CPP_CLOSE_BRACE)
14837 /* Consume the `,' token. */
14838 cp_lexer_consume_token (parser->lexer);
14844 /* Classes [gram.class] */
14846 /* Parse a class-name.
14852 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14853 to indicate that names looked up in dependent types should be
14854 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14855 keyword has been used to indicate that the name that appears next
14856 is a template. TAG_TYPE indicates the explicit tag given before
14857 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14858 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14859 is the class being defined in a class-head.
14861 Returns the TYPE_DECL representing the class. */
14864 cp_parser_class_name (cp_parser *parser,
14865 bool typename_keyword_p,
14866 bool template_keyword_p,
14867 enum tag_types tag_type,
14868 bool check_dependency_p,
14870 bool is_declaration)
14876 tree identifier = NULL_TREE;
14878 /* All class-names start with an identifier. */
14879 token = cp_lexer_peek_token (parser->lexer);
14880 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14882 cp_parser_error (parser, "expected class-name");
14883 return error_mark_node;
14886 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14887 to a template-id, so we save it here. */
14888 scope = parser->scope;
14889 if (scope == error_mark_node)
14890 return error_mark_node;
14892 /* Any name names a type if we're following the `typename' keyword
14893 in a qualified name where the enclosing scope is type-dependent. */
14894 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14895 && dependent_type_p (scope));
14896 /* Handle the common case (an identifier, but not a template-id)
14898 if (token->type == CPP_NAME
14899 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14901 cp_token *identifier_token;
14904 /* Look for the identifier. */
14905 identifier_token = cp_lexer_peek_token (parser->lexer);
14906 ambiguous_p = identifier_token->ambiguous_p;
14907 identifier = cp_parser_identifier (parser);
14908 /* If the next token isn't an identifier, we are certainly not
14909 looking at a class-name. */
14910 if (identifier == error_mark_node)
14911 decl = error_mark_node;
14912 /* If we know this is a type-name, there's no need to look it
14914 else if (typename_p)
14918 tree ambiguous_decls;
14919 /* If we already know that this lookup is ambiguous, then
14920 we've already issued an error message; there's no reason
14924 cp_parser_simulate_error (parser);
14925 return error_mark_node;
14927 /* If the next token is a `::', then the name must be a type
14930 [basic.lookup.qual]
14932 During the lookup for a name preceding the :: scope
14933 resolution operator, object, function, and enumerator
14934 names are ignored. */
14935 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14936 tag_type = typename_type;
14937 /* Look up the name. */
14938 decl = cp_parser_lookup_name (parser, identifier,
14940 /*is_template=*/false,
14941 /*is_namespace=*/false,
14942 check_dependency_p,
14944 identifier_token->location);
14945 if (ambiguous_decls)
14947 error ("%Hreference to %qD is ambiguous",
14948 &identifier_token->location, identifier);
14949 print_candidates (ambiguous_decls);
14950 if (cp_parser_parsing_tentatively (parser))
14952 identifier_token->ambiguous_p = true;
14953 cp_parser_simulate_error (parser);
14955 return error_mark_node;
14961 /* Try a template-id. */
14962 decl = cp_parser_template_id (parser, template_keyword_p,
14963 check_dependency_p,
14965 if (decl == error_mark_node)
14966 return error_mark_node;
14969 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14971 /* If this is a typename, create a TYPENAME_TYPE. */
14972 if (typename_p && decl != error_mark_node)
14974 decl = make_typename_type (scope, decl, typename_type,
14975 /*complain=*/tf_error);
14976 if (decl != error_mark_node)
14977 decl = TYPE_NAME (decl);
14980 /* Check to see that it is really the name of a class. */
14981 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14982 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14983 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14984 /* Situations like this:
14986 template <typename T> struct A {
14987 typename T::template X<int>::I i;
14990 are problematic. Is `T::template X<int>' a class-name? The
14991 standard does not seem to be definitive, but there is no other
14992 valid interpretation of the following `::'. Therefore, those
14993 names are considered class-names. */
14995 decl = make_typename_type (scope, decl, tag_type, tf_error);
14996 if (decl != error_mark_node)
14997 decl = TYPE_NAME (decl);
14999 else if (TREE_CODE (decl) != TYPE_DECL
15000 || TREE_TYPE (decl) == error_mark_node
15001 || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15002 decl = error_mark_node;
15004 if (decl == error_mark_node)
15005 cp_parser_error (parser, "expected class-name");
15006 else if (identifier && !parser->scope)
15007 maybe_note_name_used_in_class (identifier, decl);
15012 /* Parse a class-specifier.
15015 class-head { member-specification [opt] }
15017 Returns the TREE_TYPE representing the class. */
15020 cp_parser_class_specifier (cp_parser* parser)
15023 tree attributes = NULL_TREE;
15024 bool nested_name_specifier_p;
15025 unsigned saved_num_template_parameter_lists;
15026 bool saved_in_function_body;
15027 bool saved_in_unbraced_linkage_specification_p;
15028 tree old_scope = NULL_TREE;
15029 tree scope = NULL_TREE;
15032 push_deferring_access_checks (dk_no_deferred);
15034 /* Parse the class-head. */
15035 type = cp_parser_class_head (parser,
15036 &nested_name_specifier_p,
15039 /* If the class-head was a semantic disaster, skip the entire body
15043 cp_parser_skip_to_end_of_block_or_statement (parser);
15044 pop_deferring_access_checks ();
15045 return error_mark_node;
15048 /* Look for the `{'. */
15049 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15051 pop_deferring_access_checks ();
15052 return error_mark_node;
15055 /* Process the base classes. If they're invalid, skip the
15056 entire class body. */
15057 if (!xref_basetypes (type, bases))
15059 /* Consuming the closing brace yields better error messages
15061 if (cp_parser_skip_to_closing_brace (parser))
15062 cp_lexer_consume_token (parser->lexer);
15063 pop_deferring_access_checks ();
15064 return error_mark_node;
15067 /* Issue an error message if type-definitions are forbidden here. */
15068 cp_parser_check_type_definition (parser);
15069 /* Remember that we are defining one more class. */
15070 ++parser->num_classes_being_defined;
15071 /* Inside the class, surrounding template-parameter-lists do not
15073 saved_num_template_parameter_lists
15074 = parser->num_template_parameter_lists;
15075 parser->num_template_parameter_lists = 0;
15076 /* We are not in a function body. */
15077 saved_in_function_body = parser->in_function_body;
15078 parser->in_function_body = false;
15079 /* We are not immediately inside an extern "lang" block. */
15080 saved_in_unbraced_linkage_specification_p
15081 = parser->in_unbraced_linkage_specification_p;
15082 parser->in_unbraced_linkage_specification_p = false;
15084 /* Start the class. */
15085 if (nested_name_specifier_p)
15087 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15088 old_scope = push_inner_scope (scope);
15090 type = begin_class_definition (type, attributes);
15092 if (type == error_mark_node)
15093 /* If the type is erroneous, skip the entire body of the class. */
15094 cp_parser_skip_to_closing_brace (parser);
15096 /* Parse the member-specification. */
15097 cp_parser_member_specification_opt (parser);
15099 /* Look for the trailing `}'. */
15100 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15101 /* Look for trailing attributes to apply to this class. */
15102 if (cp_parser_allow_gnu_extensions_p (parser))
15103 attributes = cp_parser_attributes_opt (parser);
15104 if (type != error_mark_node)
15105 type = finish_struct (type, attributes);
15106 if (nested_name_specifier_p)
15107 pop_inner_scope (old_scope, scope);
15108 /* If this class is not itself within the scope of another class,
15109 then we need to parse the bodies of all of the queued function
15110 definitions. Note that the queued functions defined in a class
15111 are not always processed immediately following the
15112 class-specifier for that class. Consider:
15115 struct B { void f() { sizeof (A); } };
15118 If `f' were processed before the processing of `A' were
15119 completed, there would be no way to compute the size of `A'.
15120 Note that the nesting we are interested in here is lexical --
15121 not the semantic nesting given by TYPE_CONTEXT. In particular,
15124 struct A { struct B; };
15125 struct A::B { void f() { } };
15127 there is no need to delay the parsing of `A::B::f'. */
15128 if (--parser->num_classes_being_defined == 0)
15132 tree class_type = NULL_TREE;
15133 tree pushed_scope = NULL_TREE;
15135 /* In a first pass, parse default arguments to the functions.
15136 Then, in a second pass, parse the bodies of the functions.
15137 This two-phased approach handles cases like:
15145 for (TREE_PURPOSE (parser->unparsed_functions_queues)
15146 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15147 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15148 TREE_PURPOSE (parser->unparsed_functions_queues)
15149 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15151 fn = TREE_VALUE (queue_entry);
15152 /* If there are default arguments that have not yet been processed,
15153 take care of them now. */
15154 if (class_type != TREE_PURPOSE (queue_entry))
15157 pop_scope (pushed_scope);
15158 class_type = TREE_PURPOSE (queue_entry);
15159 pushed_scope = push_scope (class_type);
15161 /* Make sure that any template parameters are in scope. */
15162 maybe_begin_member_template_processing (fn);
15163 /* Parse the default argument expressions. */
15164 cp_parser_late_parsing_default_args (parser, fn);
15165 /* Remove any template parameters from the symbol table. */
15166 maybe_end_member_template_processing ();
15169 pop_scope (pushed_scope);
15170 /* Now parse the body of the functions. */
15171 for (TREE_VALUE (parser->unparsed_functions_queues)
15172 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15173 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15174 TREE_VALUE (parser->unparsed_functions_queues)
15175 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15177 /* Figure out which function we need to process. */
15178 fn = TREE_VALUE (queue_entry);
15179 /* Parse the function. */
15180 cp_parser_late_parsing_for_member (parser, fn);
15184 /* Put back any saved access checks. */
15185 pop_deferring_access_checks ();
15187 /* Restore saved state. */
15188 parser->in_function_body = saved_in_function_body;
15189 parser->num_template_parameter_lists
15190 = saved_num_template_parameter_lists;
15191 parser->in_unbraced_linkage_specification_p
15192 = saved_in_unbraced_linkage_specification_p;
15197 /* Parse a class-head.
15200 class-key identifier [opt] base-clause [opt]
15201 class-key nested-name-specifier identifier base-clause [opt]
15202 class-key nested-name-specifier [opt] template-id
15206 class-key attributes identifier [opt] base-clause [opt]
15207 class-key attributes nested-name-specifier identifier base-clause [opt]
15208 class-key attributes nested-name-specifier [opt] template-id
15211 Upon return BASES is initialized to the list of base classes (or
15212 NULL, if there are none) in the same form returned by
15213 cp_parser_base_clause.
15215 Returns the TYPE of the indicated class. Sets
15216 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15217 involving a nested-name-specifier was used, and FALSE otherwise.
15219 Returns error_mark_node if this is not a class-head.
15221 Returns NULL_TREE if the class-head is syntactically valid, but
15222 semantically invalid in a way that means we should skip the entire
15223 body of the class. */
15226 cp_parser_class_head (cp_parser* parser,
15227 bool* nested_name_specifier_p,
15228 tree *attributes_p,
15231 tree nested_name_specifier;
15232 enum tag_types class_key;
15233 tree id = NULL_TREE;
15234 tree type = NULL_TREE;
15236 bool template_id_p = false;
15237 bool qualified_p = false;
15238 bool invalid_nested_name_p = false;
15239 bool invalid_explicit_specialization_p = false;
15240 tree pushed_scope = NULL_TREE;
15241 unsigned num_templates;
15242 cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15243 /* Assume no nested-name-specifier will be present. */
15244 *nested_name_specifier_p = false;
15245 /* Assume no template parameter lists will be used in defining the
15249 *bases = NULL_TREE;
15251 /* Look for the class-key. */
15252 class_key = cp_parser_class_key (parser);
15253 if (class_key == none_type)
15254 return error_mark_node;
15256 /* Parse the attributes. */
15257 attributes = cp_parser_attributes_opt (parser);
15259 /* If the next token is `::', that is invalid -- but sometimes
15260 people do try to write:
15264 Handle this gracefully by accepting the extra qualifier, and then
15265 issuing an error about it later if this really is a
15266 class-head. If it turns out just to be an elaborated type
15267 specifier, remain silent. */
15268 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15269 qualified_p = true;
15271 push_deferring_access_checks (dk_no_check);
15273 /* Determine the name of the class. Begin by looking for an
15274 optional nested-name-specifier. */
15275 nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15276 nested_name_specifier
15277 = cp_parser_nested_name_specifier_opt (parser,
15278 /*typename_keyword_p=*/false,
15279 /*check_dependency_p=*/false,
15281 /*is_declaration=*/false);
15282 /* If there was a nested-name-specifier, then there *must* be an
15284 if (nested_name_specifier)
15286 type_start_token = cp_lexer_peek_token (parser->lexer);
15287 /* Although the grammar says `identifier', it really means
15288 `class-name' or `template-name'. You are only allowed to
15289 define a class that has already been declared with this
15292 The proposed resolution for Core Issue 180 says that wherever
15293 you see `class T::X' you should treat `X' as a type-name.
15295 It is OK to define an inaccessible class; for example:
15297 class A { class B; };
15300 We do not know if we will see a class-name, or a
15301 template-name. We look for a class-name first, in case the
15302 class-name is a template-id; if we looked for the
15303 template-name first we would stop after the template-name. */
15304 cp_parser_parse_tentatively (parser);
15305 type = cp_parser_class_name (parser,
15306 /*typename_keyword_p=*/false,
15307 /*template_keyword_p=*/false,
15309 /*check_dependency_p=*/false,
15310 /*class_head_p=*/true,
15311 /*is_declaration=*/false);
15312 /* If that didn't work, ignore the nested-name-specifier. */
15313 if (!cp_parser_parse_definitely (parser))
15315 invalid_nested_name_p = true;
15316 type_start_token = cp_lexer_peek_token (parser->lexer);
15317 id = cp_parser_identifier (parser);
15318 if (id == error_mark_node)
15321 /* If we could not find a corresponding TYPE, treat this
15322 declaration like an unqualified declaration. */
15323 if (type == error_mark_node)
15324 nested_name_specifier = NULL_TREE;
15325 /* Otherwise, count the number of templates used in TYPE and its
15326 containing scopes. */
15331 for (scope = TREE_TYPE (type);
15332 scope && TREE_CODE (scope) != NAMESPACE_DECL;
15333 scope = (TYPE_P (scope)
15334 ? TYPE_CONTEXT (scope)
15335 : DECL_CONTEXT (scope)))
15337 && CLASS_TYPE_P (scope)
15338 && CLASSTYPE_TEMPLATE_INFO (scope)
15339 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
15340 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
15344 /* Otherwise, the identifier is optional. */
15347 /* We don't know whether what comes next is a template-id,
15348 an identifier, or nothing at all. */
15349 cp_parser_parse_tentatively (parser);
15350 /* Check for a template-id. */
15351 type_start_token = cp_lexer_peek_token (parser->lexer);
15352 id = cp_parser_template_id (parser,
15353 /*template_keyword_p=*/false,
15354 /*check_dependency_p=*/true,
15355 /*is_declaration=*/true);
15356 /* If that didn't work, it could still be an identifier. */
15357 if (!cp_parser_parse_definitely (parser))
15359 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
15361 type_start_token = cp_lexer_peek_token (parser->lexer);
15362 id = cp_parser_identifier (parser);
15369 template_id_p = true;
15374 pop_deferring_access_checks ();
15377 cp_parser_check_for_invalid_template_id (parser, id,
15378 type_start_token->location);
15380 /* If it's not a `:' or a `{' then we can't really be looking at a
15381 class-head, since a class-head only appears as part of a
15382 class-specifier. We have to detect this situation before calling
15383 xref_tag, since that has irreversible side-effects. */
15384 if (!cp_parser_next_token_starts_class_definition_p (parser))
15386 cp_parser_error (parser, "expected %<{%> or %<:%>");
15387 return error_mark_node;
15390 /* At this point, we're going ahead with the class-specifier, even
15391 if some other problem occurs. */
15392 cp_parser_commit_to_tentative_parse (parser);
15393 /* Issue the error about the overly-qualified name now. */
15396 cp_parser_error (parser,
15397 "global qualification of class name is invalid");
15398 return error_mark_node;
15400 else if (invalid_nested_name_p)
15402 cp_parser_error (parser,
15403 "qualified name does not name a class");
15404 return error_mark_node;
15406 else if (nested_name_specifier)
15410 /* Reject typedef-names in class heads. */
15411 if (!DECL_IMPLICIT_TYPEDEF_P (type))
15413 error ("%Hinvalid class name in declaration of %qD",
15414 &type_start_token->location, type);
15419 /* Figure out in what scope the declaration is being placed. */
15420 scope = current_scope ();
15421 /* If that scope does not contain the scope in which the
15422 class was originally declared, the program is invalid. */
15423 if (scope && !is_ancestor (scope, nested_name_specifier))
15425 if (at_namespace_scope_p ())
15426 error ("%Hdeclaration of %qD in namespace %qD which does not "
15428 &type_start_token->location,
15429 type, scope, nested_name_specifier);
15431 error ("%Hdeclaration of %qD in %qD which does not enclose %qD",
15432 &type_start_token->location,
15433 type, scope, nested_name_specifier);
15439 A declarator-id shall not be qualified except for the
15440 definition of a ... nested class outside of its class
15441 ... [or] the definition or explicit instantiation of a
15442 class member of a namespace outside of its namespace. */
15443 if (scope == nested_name_specifier)
15445 permerror (input_location, "%Hextra qualification not allowed",
15446 &nested_name_specifier_token_start->location);
15447 nested_name_specifier = NULL_TREE;
15451 /* An explicit-specialization must be preceded by "template <>". If
15452 it is not, try to recover gracefully. */
15453 if (at_namespace_scope_p ()
15454 && parser->num_template_parameter_lists == 0
15457 error ("%Han explicit specialization must be preceded by %<template <>%>",
15458 &type_start_token->location);
15459 invalid_explicit_specialization_p = true;
15460 /* Take the same action that would have been taken by
15461 cp_parser_explicit_specialization. */
15462 ++parser->num_template_parameter_lists;
15463 begin_specialization ();
15465 /* There must be no "return" statements between this point and the
15466 end of this function; set "type "to the correct return value and
15467 use "goto done;" to return. */
15468 /* Make sure that the right number of template parameters were
15470 if (!cp_parser_check_template_parameters (parser, num_templates,
15471 type_start_token->location,
15472 /*declarator=*/NULL))
15474 /* If something went wrong, there is no point in even trying to
15475 process the class-definition. */
15480 /* Look up the type. */
15483 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
15484 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
15485 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
15487 error ("%Hfunction template %qD redeclared as a class template",
15488 &type_start_token->location, id);
15489 type = error_mark_node;
15493 type = TREE_TYPE (id);
15494 type = maybe_process_partial_specialization (type);
15496 if (nested_name_specifier)
15497 pushed_scope = push_scope (nested_name_specifier);
15499 else if (nested_name_specifier)
15505 template <typename T> struct S { struct T };
15506 template <typename T> struct S<T>::T { };
15508 we will get a TYPENAME_TYPE when processing the definition of
15509 `S::T'. We need to resolve it to the actual type before we
15510 try to define it. */
15511 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
15513 class_type = resolve_typename_type (TREE_TYPE (type),
15514 /*only_current_p=*/false);
15515 if (TREE_CODE (class_type) != TYPENAME_TYPE)
15516 type = TYPE_NAME (class_type);
15519 cp_parser_error (parser, "could not resolve typename type");
15520 type = error_mark_node;
15524 if (maybe_process_partial_specialization (TREE_TYPE (type))
15525 == error_mark_node)
15531 class_type = current_class_type;
15532 /* Enter the scope indicated by the nested-name-specifier. */
15533 pushed_scope = push_scope (nested_name_specifier);
15534 /* Get the canonical version of this type. */
15535 type = TYPE_MAIN_DECL (TREE_TYPE (type));
15536 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
15537 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
15539 type = push_template_decl (type);
15540 if (type == error_mark_node)
15547 type = TREE_TYPE (type);
15548 *nested_name_specifier_p = true;
15550 else /* The name is not a nested name. */
15552 /* If the class was unnamed, create a dummy name. */
15554 id = make_anon_name ();
15555 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
15556 parser->num_template_parameter_lists);
15559 /* Indicate whether this class was declared as a `class' or as a
15561 if (TREE_CODE (type) == RECORD_TYPE)
15562 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
15563 cp_parser_check_class_key (class_key, type);
15565 /* If this type was already complete, and we see another definition,
15566 that's an error. */
15567 if (type != error_mark_node && COMPLETE_TYPE_P (type))
15569 error ("%Hredefinition of %q#T",
15570 &type_start_token->location, type);
15571 error ("%Hprevious definition of %q+#T",
15572 &type_start_token->location, type);
15576 else if (type == error_mark_node)
15579 /* We will have entered the scope containing the class; the names of
15580 base classes should be looked up in that context. For example:
15582 struct A { struct B {}; struct C; };
15583 struct A::C : B {};
15587 /* Get the list of base-classes, if there is one. */
15588 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15589 *bases = cp_parser_base_clause (parser);
15592 /* Leave the scope given by the nested-name-specifier. We will
15593 enter the class scope itself while processing the members. */
15595 pop_scope (pushed_scope);
15597 if (invalid_explicit_specialization_p)
15599 end_specialization ();
15600 --parser->num_template_parameter_lists;
15602 *attributes_p = attributes;
15606 /* Parse a class-key.
15613 Returns the kind of class-key specified, or none_type to indicate
15616 static enum tag_types
15617 cp_parser_class_key (cp_parser* parser)
15620 enum tag_types tag_type;
15622 /* Look for the class-key. */
15623 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
15627 /* Check to see if the TOKEN is a class-key. */
15628 tag_type = cp_parser_token_is_class_key (token);
15630 cp_parser_error (parser, "expected class-key");
15634 /* Parse an (optional) member-specification.
15636 member-specification:
15637 member-declaration member-specification [opt]
15638 access-specifier : member-specification [opt] */
15641 cp_parser_member_specification_opt (cp_parser* parser)
15648 /* Peek at the next token. */
15649 token = cp_lexer_peek_token (parser->lexer);
15650 /* If it's a `}', or EOF then we've seen all the members. */
15651 if (token->type == CPP_CLOSE_BRACE
15652 || token->type == CPP_EOF
15653 || token->type == CPP_PRAGMA_EOL)
15656 /* See if this token is a keyword. */
15657 keyword = token->keyword;
15661 case RID_PROTECTED:
15663 /* Consume the access-specifier. */
15664 cp_lexer_consume_token (parser->lexer);
15665 /* Remember which access-specifier is active. */
15666 current_access_specifier = token->u.value;
15667 /* Look for the `:'. */
15668 cp_parser_require (parser, CPP_COLON, "%<:%>");
15672 /* Accept #pragmas at class scope. */
15673 if (token->type == CPP_PRAGMA)
15675 cp_parser_pragma (parser, pragma_external);
15679 /* Otherwise, the next construction must be a
15680 member-declaration. */
15681 cp_parser_member_declaration (parser);
15686 /* Parse a member-declaration.
15688 member-declaration:
15689 decl-specifier-seq [opt] member-declarator-list [opt] ;
15690 function-definition ; [opt]
15691 :: [opt] nested-name-specifier template [opt] unqualified-id ;
15693 template-declaration
15695 member-declarator-list:
15697 member-declarator-list , member-declarator
15700 declarator pure-specifier [opt]
15701 declarator constant-initializer [opt]
15702 identifier [opt] : constant-expression
15706 member-declaration:
15707 __extension__ member-declaration
15710 declarator attributes [opt] pure-specifier [opt]
15711 declarator attributes [opt] constant-initializer [opt]
15712 identifier [opt] attributes [opt] : constant-expression
15716 member-declaration:
15717 static_assert-declaration */
15720 cp_parser_member_declaration (cp_parser* parser)
15722 cp_decl_specifier_seq decl_specifiers;
15723 tree prefix_attributes;
15725 int declares_class_or_enum;
15727 cp_token *token = NULL;
15728 cp_token *decl_spec_token_start = NULL;
15729 cp_token *initializer_token_start = NULL;
15730 int saved_pedantic;
15732 /* Check for the `__extension__' keyword. */
15733 if (cp_parser_extension_opt (parser, &saved_pedantic))
15736 cp_parser_member_declaration (parser);
15737 /* Restore the old value of the PEDANTIC flag. */
15738 pedantic = saved_pedantic;
15743 /* Check for a template-declaration. */
15744 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15746 /* An explicit specialization here is an error condition, and we
15747 expect the specialization handler to detect and report this. */
15748 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
15749 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
15750 cp_parser_explicit_specialization (parser);
15752 cp_parser_template_declaration (parser, /*member_p=*/true);
15757 /* Check for a using-declaration. */
15758 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
15760 /* Parse the using-declaration. */
15761 cp_parser_using_declaration (parser,
15762 /*access_declaration_p=*/false);
15766 /* Check for @defs. */
15767 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
15770 tree ivar_chains = cp_parser_objc_defs_expression (parser);
15771 ivar = ivar_chains;
15775 ivar = TREE_CHAIN (member);
15776 TREE_CHAIN (member) = NULL_TREE;
15777 finish_member_declaration (member);
15782 /* If the next token is `static_assert' we have a static assertion. */
15783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
15785 cp_parser_static_assert (parser, /*member_p=*/true);
15789 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
15792 /* Parse the decl-specifier-seq. */
15793 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
15794 cp_parser_decl_specifier_seq (parser,
15795 CP_PARSER_FLAGS_OPTIONAL,
15797 &declares_class_or_enum);
15798 prefix_attributes = decl_specifiers.attributes;
15799 decl_specifiers.attributes = NULL_TREE;
15800 /* Check for an invalid type-name. */
15801 if (!decl_specifiers.type
15802 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
15804 /* If there is no declarator, then the decl-specifier-seq should
15806 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15808 /* If there was no decl-specifier-seq, and the next token is a
15809 `;', then we have something like:
15815 Each member-declaration shall declare at least one member
15816 name of the class. */
15817 if (!decl_specifiers.any_specifiers_p)
15819 cp_token *token = cp_lexer_peek_token (parser->lexer);
15820 if (!in_system_header_at (token->location))
15821 pedwarn (token->location, OPT_pedantic, "extra %<;%>");
15827 /* See if this declaration is a friend. */
15828 friend_p = cp_parser_friend_p (&decl_specifiers);
15829 /* If there were decl-specifiers, check to see if there was
15830 a class-declaration. */
15831 type = check_tag_decl (&decl_specifiers);
15832 /* Nested classes have already been added to the class, but
15833 a `friend' needs to be explicitly registered. */
15836 /* If the `friend' keyword was present, the friend must
15837 be introduced with a class-key. */
15838 if (!declares_class_or_enum)
15839 error ("%Ha class-key must be used when declaring a friend",
15840 &decl_spec_token_start->location);
15843 template <typename T> struct A {
15844 friend struct A<T>::B;
15847 A<T>::B will be represented by a TYPENAME_TYPE, and
15848 therefore not recognized by check_tag_decl. */
15850 && decl_specifiers.type
15851 && TYPE_P (decl_specifiers.type))
15852 type = decl_specifiers.type;
15853 if (!type || !TYPE_P (type))
15854 error ("%Hfriend declaration does not name a class or "
15855 "function", &decl_spec_token_start->location);
15857 make_friend_class (current_class_type, type,
15858 /*complain=*/true);
15860 /* If there is no TYPE, an error message will already have
15862 else if (!type || type == error_mark_node)
15864 /* An anonymous aggregate has to be handled specially; such
15865 a declaration really declares a data member (with a
15866 particular type), as opposed to a nested class. */
15867 else if (ANON_AGGR_TYPE_P (type))
15869 /* Remove constructors and such from TYPE, now that we
15870 know it is an anonymous aggregate. */
15871 fixup_anonymous_aggr (type);
15872 /* And make the corresponding data member. */
15873 decl = build_decl (decl_spec_token_start->location,
15874 FIELD_DECL, NULL_TREE, type);
15875 /* Add it to the class. */
15876 finish_member_declaration (decl);
15879 cp_parser_check_access_in_redeclaration
15881 decl_spec_token_start->location);
15886 /* See if these declarations will be friends. */
15887 friend_p = cp_parser_friend_p (&decl_specifiers);
15889 /* Keep going until we hit the `;' at the end of the
15891 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15893 tree attributes = NULL_TREE;
15894 tree first_attribute;
15896 /* Peek at the next token. */
15897 token = cp_lexer_peek_token (parser->lexer);
15899 /* Check for a bitfield declaration. */
15900 if (token->type == CPP_COLON
15901 || (token->type == CPP_NAME
15902 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15908 /* Get the name of the bitfield. Note that we cannot just
15909 check TOKEN here because it may have been invalidated by
15910 the call to cp_lexer_peek_nth_token above. */
15911 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15912 identifier = cp_parser_identifier (parser);
15914 identifier = NULL_TREE;
15916 /* Consume the `:' token. */
15917 cp_lexer_consume_token (parser->lexer);
15918 /* Get the width of the bitfield. */
15920 = cp_parser_constant_expression (parser,
15921 /*allow_non_constant=*/false,
15924 /* Look for attributes that apply to the bitfield. */
15925 attributes = cp_parser_attributes_opt (parser);
15926 /* Remember which attributes are prefix attributes and
15928 first_attribute = attributes;
15929 /* Combine the attributes. */
15930 attributes = chainon (prefix_attributes, attributes);
15932 /* Create the bitfield declaration. */
15933 decl = grokbitfield (identifier
15934 ? make_id_declarator (NULL_TREE,
15944 cp_declarator *declarator;
15946 tree asm_specification;
15947 int ctor_dtor_or_conv_p;
15949 /* Parse the declarator. */
15951 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15952 &ctor_dtor_or_conv_p,
15953 /*parenthesized_p=*/NULL,
15954 /*member_p=*/true);
15956 /* If something went wrong parsing the declarator, make sure
15957 that we at least consume some tokens. */
15958 if (declarator == cp_error_declarator)
15960 /* Skip to the end of the statement. */
15961 cp_parser_skip_to_end_of_statement (parser);
15962 /* If the next token is not a semicolon, that is
15963 probably because we just skipped over the body of
15964 a function. So, we consume a semicolon if
15965 present, but do not issue an error message if it
15967 if (cp_lexer_next_token_is (parser->lexer,
15969 cp_lexer_consume_token (parser->lexer);
15973 if (declares_class_or_enum & 2)
15974 cp_parser_check_for_definition_in_return_type
15975 (declarator, decl_specifiers.type,
15976 decl_specifiers.type_location);
15978 /* Look for an asm-specification. */
15979 asm_specification = cp_parser_asm_specification_opt (parser);
15980 /* Look for attributes that apply to the declaration. */
15981 attributes = cp_parser_attributes_opt (parser);
15982 /* Remember which attributes are prefix attributes and
15984 first_attribute = attributes;
15985 /* Combine the attributes. */
15986 attributes = chainon (prefix_attributes, attributes);
15988 /* If it's an `=', then we have a constant-initializer or a
15989 pure-specifier. It is not correct to parse the
15990 initializer before registering the member declaration
15991 since the member declaration should be in scope while
15992 its initializer is processed. However, the rest of the
15993 front end does not yet provide an interface that allows
15994 us to handle this correctly. */
15995 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15999 A pure-specifier shall be used only in the declaration of
16000 a virtual function.
16002 A member-declarator can contain a constant-initializer
16003 only if it declares a static member of integral or
16006 Therefore, if the DECLARATOR is for a function, we look
16007 for a pure-specifier; otherwise, we look for a
16008 constant-initializer. When we call `grokfield', it will
16009 perform more stringent semantics checks. */
16010 initializer_token_start = cp_lexer_peek_token (parser->lexer);
16011 if (function_declarator_p (declarator))
16012 initializer = cp_parser_pure_specifier (parser);
16014 /* Parse the initializer. */
16015 initializer = cp_parser_constant_initializer (parser);
16017 /* Otherwise, there is no initializer. */
16019 initializer = NULL_TREE;
16021 /* See if we are probably looking at a function
16022 definition. We are certainly not looking at a
16023 member-declarator. Calling `grokfield' has
16024 side-effects, so we must not do it unless we are sure
16025 that we are looking at a member-declarator. */
16026 if (cp_parser_token_starts_function_definition_p
16027 (cp_lexer_peek_token (parser->lexer)))
16029 /* The grammar does not allow a pure-specifier to be
16030 used when a member function is defined. (It is
16031 possible that this fact is an oversight in the
16032 standard, since a pure function may be defined
16033 outside of the class-specifier. */
16035 error ("%Hpure-specifier on function-definition",
16036 &initializer_token_start->location);
16037 decl = cp_parser_save_member_function_body (parser,
16041 /* If the member was not a friend, declare it here. */
16043 finish_member_declaration (decl);
16044 /* Peek at the next token. */
16045 token = cp_lexer_peek_token (parser->lexer);
16046 /* If the next token is a semicolon, consume it. */
16047 if (token->type == CPP_SEMICOLON)
16048 cp_lexer_consume_token (parser->lexer);
16052 if (declarator->kind == cdk_function)
16053 declarator->id_loc = token->location;
16054 /* Create the declaration. */
16055 decl = grokfield (declarator, &decl_specifiers,
16056 initializer, /*init_const_expr_p=*/true,
16061 /* Reset PREFIX_ATTRIBUTES. */
16062 while (attributes && TREE_CHAIN (attributes) != first_attribute)
16063 attributes = TREE_CHAIN (attributes);
16065 TREE_CHAIN (attributes) = NULL_TREE;
16067 /* If there is any qualification still in effect, clear it
16068 now; we will be starting fresh with the next declarator. */
16069 parser->scope = NULL_TREE;
16070 parser->qualifying_scope = NULL_TREE;
16071 parser->object_scope = NULL_TREE;
16072 /* If it's a `,', then there are more declarators. */
16073 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16074 cp_lexer_consume_token (parser->lexer);
16075 /* If the next token isn't a `;', then we have a parse error. */
16076 else if (cp_lexer_next_token_is_not (parser->lexer,
16079 cp_parser_error (parser, "expected %<;%>");
16080 /* Skip tokens until we find a `;'. */
16081 cp_parser_skip_to_end_of_statement (parser);
16088 /* Add DECL to the list of members. */
16090 finish_member_declaration (decl);
16092 if (TREE_CODE (decl) == FUNCTION_DECL)
16093 cp_parser_save_default_args (parser, decl);
16098 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16101 /* Parse a pure-specifier.
16106 Returns INTEGER_ZERO_NODE if a pure specifier is found.
16107 Otherwise, ERROR_MARK_NODE is returned. */
16110 cp_parser_pure_specifier (cp_parser* parser)
16114 /* Look for the `=' token. */
16115 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16116 return error_mark_node;
16117 /* Look for the `0' token. */
16118 token = cp_lexer_peek_token (parser->lexer);
16120 if (token->type == CPP_EOF
16121 || token->type == CPP_PRAGMA_EOL)
16122 return error_mark_node;
16124 cp_lexer_consume_token (parser->lexer);
16126 /* Accept = default or = delete in c++0x mode. */
16127 if (token->keyword == RID_DEFAULT
16128 || token->keyword == RID_DELETE)
16130 maybe_warn_cpp0x ("defaulted and deleted functions");
16131 return token->u.value;
16134 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
16135 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16137 cp_parser_error (parser,
16138 "invalid pure specifier (only %<= 0%> is allowed)");
16139 cp_parser_skip_to_end_of_statement (parser);
16140 return error_mark_node;
16142 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16144 error ("%Htemplates may not be %<virtual%>", &token->location);
16145 return error_mark_node;
16148 return integer_zero_node;
16151 /* Parse a constant-initializer.
16153 constant-initializer:
16154 = constant-expression
16156 Returns a representation of the constant-expression. */
16159 cp_parser_constant_initializer (cp_parser* parser)
16161 /* Look for the `=' token. */
16162 if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16163 return error_mark_node;
16165 /* It is invalid to write:
16167 struct S { static const int i = { 7 }; };
16170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16172 cp_parser_error (parser,
16173 "a brace-enclosed initializer is not allowed here");
16174 /* Consume the opening brace. */
16175 cp_lexer_consume_token (parser->lexer);
16176 /* Skip the initializer. */
16177 cp_parser_skip_to_closing_brace (parser);
16178 /* Look for the trailing `}'. */
16179 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16181 return error_mark_node;
16184 return cp_parser_constant_expression (parser,
16185 /*allow_non_constant=*/false,
16189 /* Derived classes [gram.class.derived] */
16191 /* Parse a base-clause.
16194 : base-specifier-list
16196 base-specifier-list:
16197 base-specifier ... [opt]
16198 base-specifier-list , base-specifier ... [opt]
16200 Returns a TREE_LIST representing the base-classes, in the order in
16201 which they were declared. The representation of each node is as
16202 described by cp_parser_base_specifier.
16204 In the case that no bases are specified, this function will return
16205 NULL_TREE, not ERROR_MARK_NODE. */
16208 cp_parser_base_clause (cp_parser* parser)
16210 tree bases = NULL_TREE;
16212 /* Look for the `:' that begins the list. */
16213 cp_parser_require (parser, CPP_COLON, "%<:%>");
16215 /* Scan the base-specifier-list. */
16220 bool pack_expansion_p = false;
16222 /* Look for the base-specifier. */
16223 base = cp_parser_base_specifier (parser);
16224 /* Look for the (optional) ellipsis. */
16225 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16227 /* Consume the `...'. */
16228 cp_lexer_consume_token (parser->lexer);
16230 pack_expansion_p = true;
16233 /* Add BASE to the front of the list. */
16234 if (base != error_mark_node)
16236 if (pack_expansion_p)
16237 /* Make this a pack expansion type. */
16238 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16241 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16243 TREE_CHAIN (base) = bases;
16247 /* Peek at the next token. */
16248 token = cp_lexer_peek_token (parser->lexer);
16249 /* If it's not a comma, then the list is complete. */
16250 if (token->type != CPP_COMMA)
16252 /* Consume the `,'. */
16253 cp_lexer_consume_token (parser->lexer);
16256 /* PARSER->SCOPE may still be non-NULL at this point, if the last
16257 base class had a qualified name. However, the next name that
16258 appears is certainly not qualified. */
16259 parser->scope = NULL_TREE;
16260 parser->qualifying_scope = NULL_TREE;
16261 parser->object_scope = NULL_TREE;
16263 return nreverse (bases);
16266 /* Parse a base-specifier.
16269 :: [opt] nested-name-specifier [opt] class-name
16270 virtual access-specifier [opt] :: [opt] nested-name-specifier
16272 access-specifier virtual [opt] :: [opt] nested-name-specifier
16275 Returns a TREE_LIST. The TREE_PURPOSE will be one of
16276 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16277 indicate the specifiers provided. The TREE_VALUE will be a TYPE
16278 (or the ERROR_MARK_NODE) indicating the type that was specified. */
16281 cp_parser_base_specifier (cp_parser* parser)
16285 bool virtual_p = false;
16286 bool duplicate_virtual_error_issued_p = false;
16287 bool duplicate_access_error_issued_p = false;
16288 bool class_scope_p, template_p;
16289 tree access = access_default_node;
16292 /* Process the optional `virtual' and `access-specifier'. */
16295 /* Peek at the next token. */
16296 token = cp_lexer_peek_token (parser->lexer);
16297 /* Process `virtual'. */
16298 switch (token->keyword)
16301 /* If `virtual' appears more than once, issue an error. */
16302 if (virtual_p && !duplicate_virtual_error_issued_p)
16304 cp_parser_error (parser,
16305 "%<virtual%> specified more than once in base-specified");
16306 duplicate_virtual_error_issued_p = true;
16311 /* Consume the `virtual' token. */
16312 cp_lexer_consume_token (parser->lexer);
16317 case RID_PROTECTED:
16319 /* If more than one access specifier appears, issue an
16321 if (access != access_default_node
16322 && !duplicate_access_error_issued_p)
16324 cp_parser_error (parser,
16325 "more than one access specifier in base-specified");
16326 duplicate_access_error_issued_p = true;
16329 access = ridpointers[(int) token->keyword];
16331 /* Consume the access-specifier. */
16332 cp_lexer_consume_token (parser->lexer);
16341 /* It is not uncommon to see programs mechanically, erroneously, use
16342 the 'typename' keyword to denote (dependent) qualified types
16343 as base classes. */
16344 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
16346 token = cp_lexer_peek_token (parser->lexer);
16347 if (!processing_template_decl)
16348 error ("%Hkeyword %<typename%> not allowed outside of templates",
16351 error ("%Hkeyword %<typename%> not allowed in this context "
16352 "(the base class is implicitly a type)",
16354 cp_lexer_consume_token (parser->lexer);
16357 /* Look for the optional `::' operator. */
16358 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
16359 /* Look for the nested-name-specifier. The simplest way to
16364 The keyword `typename' is not permitted in a base-specifier or
16365 mem-initializer; in these contexts a qualified name that
16366 depends on a template-parameter is implicitly assumed to be a
16369 is to pretend that we have seen the `typename' keyword at this
16371 cp_parser_nested_name_specifier_opt (parser,
16372 /*typename_keyword_p=*/true,
16373 /*check_dependency_p=*/true,
16375 /*is_declaration=*/true);
16376 /* If the base class is given by a qualified name, assume that names
16377 we see are type names or templates, as appropriate. */
16378 class_scope_p = (parser->scope && TYPE_P (parser->scope));
16379 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
16381 /* Finally, look for the class-name. */
16382 type = cp_parser_class_name (parser,
16386 /*check_dependency_p=*/true,
16387 /*class_head_p=*/false,
16388 /*is_declaration=*/true);
16390 if (type == error_mark_node)
16391 return error_mark_node;
16393 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
16396 /* Exception handling [gram.exception] */
16398 /* Parse an (optional) exception-specification.
16400 exception-specification:
16401 throw ( type-id-list [opt] )
16403 Returns a TREE_LIST representing the exception-specification. The
16404 TREE_VALUE of each node is a type. */
16407 cp_parser_exception_specification_opt (cp_parser* parser)
16412 /* Peek at the next token. */
16413 token = cp_lexer_peek_token (parser->lexer);
16414 /* If it's not `throw', then there's no exception-specification. */
16415 if (!cp_parser_is_keyword (token, RID_THROW))
16418 /* Consume the `throw'. */
16419 cp_lexer_consume_token (parser->lexer);
16421 /* Look for the `('. */
16422 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16424 /* Peek at the next token. */
16425 token = cp_lexer_peek_token (parser->lexer);
16426 /* If it's not a `)', then there is a type-id-list. */
16427 if (token->type != CPP_CLOSE_PAREN)
16429 const char *saved_message;
16431 /* Types may not be defined in an exception-specification. */
16432 saved_message = parser->type_definition_forbidden_message;
16433 parser->type_definition_forbidden_message
16434 = "types may not be defined in an exception-specification";
16435 /* Parse the type-id-list. */
16436 type_id_list = cp_parser_type_id_list (parser);
16437 /* Restore the saved message. */
16438 parser->type_definition_forbidden_message = saved_message;
16441 type_id_list = empty_except_spec;
16443 /* Look for the `)'. */
16444 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16446 return type_id_list;
16449 /* Parse an (optional) type-id-list.
16453 type-id-list , type-id ... [opt]
16455 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
16456 in the order that the types were presented. */
16459 cp_parser_type_id_list (cp_parser* parser)
16461 tree types = NULL_TREE;
16468 /* Get the next type-id. */
16469 type = cp_parser_type_id (parser);
16470 /* Parse the optional ellipsis. */
16471 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16473 /* Consume the `...'. */
16474 cp_lexer_consume_token (parser->lexer);
16476 /* Turn the type into a pack expansion expression. */
16477 type = make_pack_expansion (type);
16479 /* Add it to the list. */
16480 types = add_exception_specifier (types, type, /*complain=*/1);
16481 /* Peek at the next token. */
16482 token = cp_lexer_peek_token (parser->lexer);
16483 /* If it is not a `,', we are done. */
16484 if (token->type != CPP_COMMA)
16486 /* Consume the `,'. */
16487 cp_lexer_consume_token (parser->lexer);
16490 return nreverse (types);
16493 /* Parse a try-block.
16496 try compound-statement handler-seq */
16499 cp_parser_try_block (cp_parser* parser)
16503 cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
16504 try_block = begin_try_block ();
16505 cp_parser_compound_statement (parser, NULL, true);
16506 finish_try_block (try_block);
16507 cp_parser_handler_seq (parser);
16508 finish_handler_sequence (try_block);
16513 /* Parse a function-try-block.
16515 function-try-block:
16516 try ctor-initializer [opt] function-body handler-seq */
16519 cp_parser_function_try_block (cp_parser* parser)
16521 tree compound_stmt;
16523 bool ctor_initializer_p;
16525 /* Look for the `try' keyword. */
16526 if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
16528 /* Let the rest of the front end know where we are. */
16529 try_block = begin_function_try_block (&compound_stmt);
16530 /* Parse the function-body. */
16532 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16533 /* We're done with the `try' part. */
16534 finish_function_try_block (try_block);
16535 /* Parse the handlers. */
16536 cp_parser_handler_seq (parser);
16537 /* We're done with the handlers. */
16538 finish_function_handler_sequence (try_block, compound_stmt);
16540 return ctor_initializer_p;
16543 /* Parse a handler-seq.
16546 handler handler-seq [opt] */
16549 cp_parser_handler_seq (cp_parser* parser)
16555 /* Parse the handler. */
16556 cp_parser_handler (parser);
16557 /* Peek at the next token. */
16558 token = cp_lexer_peek_token (parser->lexer);
16559 /* If it's not `catch' then there are no more handlers. */
16560 if (!cp_parser_is_keyword (token, RID_CATCH))
16565 /* Parse a handler.
16568 catch ( exception-declaration ) compound-statement */
16571 cp_parser_handler (cp_parser* parser)
16576 cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
16577 handler = begin_handler ();
16578 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16579 declaration = cp_parser_exception_declaration (parser);
16580 finish_handler_parms (declaration, handler);
16581 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16582 cp_parser_compound_statement (parser, NULL, false);
16583 finish_handler (handler);
16586 /* Parse an exception-declaration.
16588 exception-declaration:
16589 type-specifier-seq declarator
16590 type-specifier-seq abstract-declarator
16594 Returns a VAR_DECL for the declaration, or NULL_TREE if the
16595 ellipsis variant is used. */
16598 cp_parser_exception_declaration (cp_parser* parser)
16600 cp_decl_specifier_seq type_specifiers;
16601 cp_declarator *declarator;
16602 const char *saved_message;
16604 /* If it's an ellipsis, it's easy to handle. */
16605 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16607 /* Consume the `...' token. */
16608 cp_lexer_consume_token (parser->lexer);
16612 /* Types may not be defined in exception-declarations. */
16613 saved_message = parser->type_definition_forbidden_message;
16614 parser->type_definition_forbidden_message
16615 = "types may not be defined in exception-declarations";
16617 /* Parse the type-specifier-seq. */
16618 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
16620 /* If it's a `)', then there is no declarator. */
16621 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
16624 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
16625 /*ctor_dtor_or_conv_p=*/NULL,
16626 /*parenthesized_p=*/NULL,
16627 /*member_p=*/false);
16629 /* Restore the saved message. */
16630 parser->type_definition_forbidden_message = saved_message;
16632 if (!type_specifiers.any_specifiers_p)
16633 return error_mark_node;
16635 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
16638 /* Parse a throw-expression.
16641 throw assignment-expression [opt]
16643 Returns a THROW_EXPR representing the throw-expression. */
16646 cp_parser_throw_expression (cp_parser* parser)
16651 cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
16652 token = cp_lexer_peek_token (parser->lexer);
16653 /* Figure out whether or not there is an assignment-expression
16654 following the "throw" keyword. */
16655 if (token->type == CPP_COMMA
16656 || token->type == CPP_SEMICOLON
16657 || token->type == CPP_CLOSE_PAREN
16658 || token->type == CPP_CLOSE_SQUARE
16659 || token->type == CPP_CLOSE_BRACE
16660 || token->type == CPP_COLON)
16661 expression = NULL_TREE;
16663 expression = cp_parser_assignment_expression (parser,
16664 /*cast_p=*/false, NULL);
16666 return build_throw (expression);
16669 /* GNU Extensions */
16671 /* Parse an (optional) asm-specification.
16674 asm ( string-literal )
16676 If the asm-specification is present, returns a STRING_CST
16677 corresponding to the string-literal. Otherwise, returns
16681 cp_parser_asm_specification_opt (cp_parser* parser)
16684 tree asm_specification;
16686 /* Peek at the next token. */
16687 token = cp_lexer_peek_token (parser->lexer);
16688 /* If the next token isn't the `asm' keyword, then there's no
16689 asm-specification. */
16690 if (!cp_parser_is_keyword (token, RID_ASM))
16693 /* Consume the `asm' token. */
16694 cp_lexer_consume_token (parser->lexer);
16695 /* Look for the `('. */
16696 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16698 /* Look for the string-literal. */
16699 asm_specification = cp_parser_string_literal (parser, false, false);
16701 /* Look for the `)'. */
16702 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16704 return asm_specification;
16707 /* Parse an asm-operand-list.
16711 asm-operand-list , asm-operand
16714 string-literal ( expression )
16715 [ string-literal ] string-literal ( expression )
16717 Returns a TREE_LIST representing the operands. The TREE_VALUE of
16718 each node is the expression. The TREE_PURPOSE is itself a
16719 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
16720 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
16721 is a STRING_CST for the string literal before the parenthesis. Returns
16722 ERROR_MARK_NODE if any of the operands are invalid. */
16725 cp_parser_asm_operand_list (cp_parser* parser)
16727 tree asm_operands = NULL_TREE;
16728 bool invalid_operands = false;
16732 tree string_literal;
16736 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16738 /* Consume the `[' token. */
16739 cp_lexer_consume_token (parser->lexer);
16740 /* Read the operand name. */
16741 name = cp_parser_identifier (parser);
16742 if (name != error_mark_node)
16743 name = build_string (IDENTIFIER_LENGTH (name),
16744 IDENTIFIER_POINTER (name));
16745 /* Look for the closing `]'. */
16746 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
16750 /* Look for the string-literal. */
16751 string_literal = cp_parser_string_literal (parser, false, false);
16753 /* Look for the `('. */
16754 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16755 /* Parse the expression. */
16756 expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
16757 /* Look for the `)'. */
16758 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16760 if (name == error_mark_node
16761 || string_literal == error_mark_node
16762 || expression == error_mark_node)
16763 invalid_operands = true;
16765 /* Add this operand to the list. */
16766 asm_operands = tree_cons (build_tree_list (name, string_literal),
16769 /* If the next token is not a `,', there are no more
16771 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16773 /* Consume the `,'. */
16774 cp_lexer_consume_token (parser->lexer);
16777 return invalid_operands ? error_mark_node : nreverse (asm_operands);
16780 /* Parse an asm-clobber-list.
16784 asm-clobber-list , string-literal
16786 Returns a TREE_LIST, indicating the clobbers in the order that they
16787 appeared. The TREE_VALUE of each node is a STRING_CST. */
16790 cp_parser_asm_clobber_list (cp_parser* parser)
16792 tree clobbers = NULL_TREE;
16796 tree string_literal;
16798 /* Look for the string literal. */
16799 string_literal = cp_parser_string_literal (parser, false, false);
16800 /* Add it to the list. */
16801 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
16802 /* If the next token is not a `,', then the list is
16804 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16806 /* Consume the `,' token. */
16807 cp_lexer_consume_token (parser->lexer);
16813 /* Parse an (optional) series of attributes.
16816 attributes attribute
16819 __attribute__ (( attribute-list [opt] ))
16821 The return value is as for cp_parser_attribute_list. */
16824 cp_parser_attributes_opt (cp_parser* parser)
16826 tree attributes = NULL_TREE;
16831 tree attribute_list;
16833 /* Peek at the next token. */
16834 token = cp_lexer_peek_token (parser->lexer);
16835 /* If it's not `__attribute__', then we're done. */
16836 if (token->keyword != RID_ATTRIBUTE)
16839 /* Consume the `__attribute__' keyword. */
16840 cp_lexer_consume_token (parser->lexer);
16841 /* Look for the two `(' tokens. */
16842 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16843 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
16845 /* Peek at the next token. */
16846 token = cp_lexer_peek_token (parser->lexer);
16847 if (token->type != CPP_CLOSE_PAREN)
16848 /* Parse the attribute-list. */
16849 attribute_list = cp_parser_attribute_list (parser);
16851 /* If the next token is a `)', then there is no attribute
16853 attribute_list = NULL;
16855 /* Look for the two `)' tokens. */
16856 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16857 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16859 /* Add these new attributes to the list. */
16860 attributes = chainon (attributes, attribute_list);
16866 /* Parse an attribute-list.
16870 attribute-list , attribute
16874 identifier ( identifier )
16875 identifier ( identifier , expression-list )
16876 identifier ( expression-list )
16878 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
16879 to an attribute. The TREE_PURPOSE of each node is the identifier
16880 indicating which attribute is in use. The TREE_VALUE represents
16881 the arguments, if any. */
16884 cp_parser_attribute_list (cp_parser* parser)
16886 tree attribute_list = NULL_TREE;
16887 bool save_translate_strings_p = parser->translate_strings_p;
16889 parser->translate_strings_p = false;
16896 /* Look for the identifier. We also allow keywords here; for
16897 example `__attribute__ ((const))' is legal. */
16898 token = cp_lexer_peek_token (parser->lexer);
16899 if (token->type == CPP_NAME
16900 || token->type == CPP_KEYWORD)
16902 tree arguments = NULL_TREE;
16904 /* Consume the token. */
16905 token = cp_lexer_consume_token (parser->lexer);
16907 /* Save away the identifier that indicates which attribute
16909 identifier = (token->type == CPP_KEYWORD)
16910 /* For keywords, use the canonical spelling, not the
16911 parsed identifier. */
16912 ? ridpointers[(int) token->keyword]
16915 attribute = build_tree_list (identifier, NULL_TREE);
16917 /* Peek at the next token. */
16918 token = cp_lexer_peek_token (parser->lexer);
16919 /* If it's an `(', then parse the attribute arguments. */
16920 if (token->type == CPP_OPEN_PAREN)
16923 vec = cp_parser_parenthesized_expression_list
16924 (parser, true, /*cast_p=*/false,
16925 /*allow_expansion_p=*/false,
16926 /*non_constant_p=*/NULL);
16928 arguments = error_mark_node;
16931 arguments = build_tree_list_vec (vec);
16932 release_tree_vector (vec);
16934 /* Save the arguments away. */
16935 TREE_VALUE (attribute) = arguments;
16938 if (arguments != error_mark_node)
16940 /* Add this attribute to the list. */
16941 TREE_CHAIN (attribute) = attribute_list;
16942 attribute_list = attribute;
16945 token = cp_lexer_peek_token (parser->lexer);
16947 /* Now, look for more attributes. If the next token isn't a
16948 `,', we're done. */
16949 if (token->type != CPP_COMMA)
16952 /* Consume the comma and keep going. */
16953 cp_lexer_consume_token (parser->lexer);
16955 parser->translate_strings_p = save_translate_strings_p;
16957 /* We built up the list in reverse order. */
16958 return nreverse (attribute_list);
16961 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16962 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16963 current value of the PEDANTIC flag, regardless of whether or not
16964 the `__extension__' keyword is present. The caller is responsible
16965 for restoring the value of the PEDANTIC flag. */
16968 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16970 /* Save the old value of the PEDANTIC flag. */
16971 *saved_pedantic = pedantic;
16973 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16975 /* Consume the `__extension__' token. */
16976 cp_lexer_consume_token (parser->lexer);
16977 /* We're not being pedantic while the `__extension__' keyword is
16987 /* Parse a label declaration.
16990 __label__ label-declarator-seq ;
16992 label-declarator-seq:
16993 identifier , label-declarator-seq
16997 cp_parser_label_declaration (cp_parser* parser)
16999 /* Look for the `__label__' keyword. */
17000 cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17006 /* Look for an identifier. */
17007 identifier = cp_parser_identifier (parser);
17008 /* If we failed, stop. */
17009 if (identifier == error_mark_node)
17011 /* Declare it as a label. */
17012 finish_label_decl (identifier);
17013 /* If the next token is a `;', stop. */
17014 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17016 /* Look for the `,' separating the label declarations. */
17017 cp_parser_require (parser, CPP_COMMA, "%<,%>");
17020 /* Look for the final `;'. */
17021 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17024 /* Support Functions */
17026 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17027 NAME should have one of the representations used for an
17028 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17029 is returned. If PARSER->SCOPE is a dependent type, then a
17030 SCOPE_REF is returned.
17032 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17033 returned; the name was already resolved when the TEMPLATE_ID_EXPR
17034 was formed. Abstractly, such entities should not be passed to this
17035 function, because they do not need to be looked up, but it is
17036 simpler to check for this special case here, rather than at the
17039 In cases not explicitly covered above, this function returns a
17040 DECL, OVERLOAD, or baselink representing the result of the lookup.
17041 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17044 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17045 (e.g., "struct") that was used. In that case bindings that do not
17046 refer to types are ignored.
17048 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17051 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17054 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17057 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17058 TREE_LIST of candidates if name-lookup results in an ambiguity, and
17059 NULL_TREE otherwise. */
17062 cp_parser_lookup_name (cp_parser *parser, tree name,
17063 enum tag_types tag_type,
17066 bool check_dependency,
17067 tree *ambiguous_decls,
17068 location_t name_location)
17072 tree object_type = parser->context->object_type;
17074 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17075 flags |= LOOKUP_COMPLAIN;
17077 /* Assume that the lookup will be unambiguous. */
17078 if (ambiguous_decls)
17079 *ambiguous_decls = NULL_TREE;
17081 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17082 no longer valid. Note that if we are parsing tentatively, and
17083 the parse fails, OBJECT_TYPE will be automatically restored. */
17084 parser->context->object_type = NULL_TREE;
17086 if (name == error_mark_node)
17087 return error_mark_node;
17089 /* A template-id has already been resolved; there is no lookup to
17091 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17093 if (BASELINK_P (name))
17095 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17096 == TEMPLATE_ID_EXPR);
17100 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
17101 it should already have been checked to make sure that the name
17102 used matches the type being destroyed. */
17103 if (TREE_CODE (name) == BIT_NOT_EXPR)
17107 /* Figure out to which type this destructor applies. */
17109 type = parser->scope;
17110 else if (object_type)
17111 type = object_type;
17113 type = current_class_type;
17114 /* If that's not a class type, there is no destructor. */
17115 if (!type || !CLASS_TYPE_P (type))
17116 return error_mark_node;
17117 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17118 lazily_declare_fn (sfk_destructor, type);
17119 if (!CLASSTYPE_DESTRUCTORS (type))
17120 return error_mark_node;
17121 /* If it was a class type, return the destructor. */
17122 return CLASSTYPE_DESTRUCTORS (type);
17125 /* By this point, the NAME should be an ordinary identifier. If
17126 the id-expression was a qualified name, the qualifying scope is
17127 stored in PARSER->SCOPE at this point. */
17128 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17130 /* Perform the lookup. */
17135 if (parser->scope == error_mark_node)
17136 return error_mark_node;
17138 /* If the SCOPE is dependent, the lookup must be deferred until
17139 the template is instantiated -- unless we are explicitly
17140 looking up names in uninstantiated templates. Even then, we
17141 cannot look up the name if the scope is not a class type; it
17142 might, for example, be a template type parameter. */
17143 dependent_p = (TYPE_P (parser->scope)
17144 && dependent_scope_p (parser->scope));
17145 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17147 /* Defer lookup. */
17148 decl = error_mark_node;
17151 tree pushed_scope = NULL_TREE;
17153 /* If PARSER->SCOPE is a dependent type, then it must be a
17154 class type, and we must not be checking dependencies;
17155 otherwise, we would have processed this lookup above. So
17156 that PARSER->SCOPE is not considered a dependent base by
17157 lookup_member, we must enter the scope here. */
17159 pushed_scope = push_scope (parser->scope);
17160 /* If the PARSER->SCOPE is a template specialization, it
17161 may be instantiated during name lookup. In that case,
17162 errors may be issued. Even if we rollback the current
17163 tentative parse, those errors are valid. */
17164 decl = lookup_qualified_name (parser->scope, name,
17165 tag_type != none_type,
17166 /*complain=*/true);
17168 /* If we have a single function from a using decl, pull it out. */
17169 if (TREE_CODE (decl) == OVERLOAD
17170 && !really_overloaded_fn (decl))
17171 decl = OVL_FUNCTION (decl);
17174 pop_scope (pushed_scope);
17177 /* If the scope is a dependent type and either we deferred lookup or
17178 we did lookup but didn't find the name, rememeber the name. */
17179 if (decl == error_mark_node && TYPE_P (parser->scope)
17180 && dependent_type_p (parser->scope))
17186 /* The resolution to Core Issue 180 says that `struct
17187 A::B' should be considered a type-name, even if `A'
17189 type = make_typename_type (parser->scope, name, tag_type,
17190 /*complain=*/tf_error);
17191 decl = TYPE_NAME (type);
17193 else if (is_template
17194 && (cp_parser_next_token_ends_template_argument_p (parser)
17195 || cp_lexer_next_token_is (parser->lexer,
17197 decl = make_unbound_class_template (parser->scope,
17199 /*complain=*/tf_error);
17201 decl = build_qualified_name (/*type=*/NULL_TREE,
17202 parser->scope, name,
17205 parser->qualifying_scope = parser->scope;
17206 parser->object_scope = NULL_TREE;
17208 else if (object_type)
17210 tree object_decl = NULL_TREE;
17211 /* Look up the name in the scope of the OBJECT_TYPE, unless the
17212 OBJECT_TYPE is not a class. */
17213 if (CLASS_TYPE_P (object_type))
17214 /* If the OBJECT_TYPE is a template specialization, it may
17215 be instantiated during name lookup. In that case, errors
17216 may be issued. Even if we rollback the current tentative
17217 parse, those errors are valid. */
17218 object_decl = lookup_member (object_type,
17221 tag_type != none_type);
17222 /* Look it up in the enclosing context, too. */
17223 decl = lookup_name_real (name, tag_type != none_type,
17225 /*block_p=*/true, is_namespace, flags);
17226 parser->object_scope = object_type;
17227 parser->qualifying_scope = NULL_TREE;
17229 decl = object_decl;
17233 decl = lookup_name_real (name, tag_type != none_type,
17235 /*block_p=*/true, is_namespace, flags);
17236 parser->qualifying_scope = NULL_TREE;
17237 parser->object_scope = NULL_TREE;
17240 /* If the lookup failed, let our caller know. */
17241 if (!decl || decl == error_mark_node)
17242 return error_mark_node;
17244 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
17245 if (TREE_CODE (decl) == TREE_LIST)
17247 if (ambiguous_decls)
17248 *ambiguous_decls = decl;
17249 /* The error message we have to print is too complicated for
17250 cp_parser_error, so we incorporate its actions directly. */
17251 if (!cp_parser_simulate_error (parser))
17253 error ("%Hreference to %qD is ambiguous",
17254 &name_location, name);
17255 print_candidates (decl);
17257 return error_mark_node;
17260 gcc_assert (DECL_P (decl)
17261 || TREE_CODE (decl) == OVERLOAD
17262 || TREE_CODE (decl) == SCOPE_REF
17263 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
17264 || BASELINK_P (decl));
17266 /* If we have resolved the name of a member declaration, check to
17267 see if the declaration is accessible. When the name resolves to
17268 set of overloaded functions, accessibility is checked when
17269 overload resolution is done.
17271 During an explicit instantiation, access is not checked at all,
17272 as per [temp.explicit]. */
17274 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
17279 /* Like cp_parser_lookup_name, but for use in the typical case where
17280 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
17281 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
17284 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
17286 return cp_parser_lookup_name (parser, name,
17288 /*is_template=*/false,
17289 /*is_namespace=*/false,
17290 /*check_dependency=*/true,
17291 /*ambiguous_decls=*/NULL,
17295 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
17296 the current context, return the TYPE_DECL. If TAG_NAME_P is
17297 true, the DECL indicates the class being defined in a class-head,
17298 or declared in an elaborated-type-specifier.
17300 Otherwise, return DECL. */
17303 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
17305 /* If the TEMPLATE_DECL is being declared as part of a class-head,
17306 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
17309 template <typename T> struct B;
17312 template <typename T> struct A::B {};
17314 Similarly, in an elaborated-type-specifier:
17316 namespace N { struct X{}; }
17319 template <typename T> friend struct N::X;
17322 However, if the DECL refers to a class type, and we are in
17323 the scope of the class, then the name lookup automatically
17324 finds the TYPE_DECL created by build_self_reference rather
17325 than a TEMPLATE_DECL. For example, in:
17327 template <class T> struct S {
17331 there is no need to handle such case. */
17333 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
17334 return DECL_TEMPLATE_RESULT (decl);
17339 /* If too many, or too few, template-parameter lists apply to the
17340 declarator, issue an error message. Returns TRUE if all went well,
17341 and FALSE otherwise. */
17344 cp_parser_check_declarator_template_parameters (cp_parser* parser,
17345 cp_declarator *declarator,
17346 location_t declarator_location)
17348 unsigned num_templates;
17350 /* We haven't seen any classes that involve template parameters yet. */
17353 switch (declarator->kind)
17356 if (declarator->u.id.qualifying_scope)
17361 scope = declarator->u.id.qualifying_scope;
17362 member = declarator->u.id.unqualified_name;
17364 while (scope && CLASS_TYPE_P (scope))
17366 /* You're supposed to have one `template <...>'
17367 for every template class, but you don't need one
17368 for a full specialization. For example:
17370 template <class T> struct S{};
17371 template <> struct S<int> { void f(); };
17372 void S<int>::f () {}
17374 is correct; there shouldn't be a `template <>' for
17375 the definition of `S<int>::f'. */
17376 if (!CLASSTYPE_TEMPLATE_INFO (scope))
17377 /* If SCOPE does not have template information of any
17378 kind, then it is not a template, nor is it nested
17379 within a template. */
17381 if (explicit_class_specialization_p (scope))
17383 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
17386 scope = TYPE_CONTEXT (scope);
17389 else if (TREE_CODE (declarator->u.id.unqualified_name)
17390 == TEMPLATE_ID_EXPR)
17391 /* If the DECLARATOR has the form `X<y>' then it uses one
17392 additional level of template parameters. */
17395 return cp_parser_check_template_parameters
17396 (parser, num_templates, declarator_location, declarator);
17402 case cdk_reference:
17404 return (cp_parser_check_declarator_template_parameters
17405 (parser, declarator->declarator, declarator_location));
17411 gcc_unreachable ();
17416 /* NUM_TEMPLATES were used in the current declaration. If that is
17417 invalid, return FALSE and issue an error messages. Otherwise,
17418 return TRUE. If DECLARATOR is non-NULL, then we are checking a
17419 declarator and we can print more accurate diagnostics. */
17422 cp_parser_check_template_parameters (cp_parser* parser,
17423 unsigned num_templates,
17424 location_t location,
17425 cp_declarator *declarator)
17427 /* If there are the same number of template classes and parameter
17428 lists, that's OK. */
17429 if (parser->num_template_parameter_lists == num_templates)
17431 /* If there are more, but only one more, then we are referring to a
17432 member template. That's OK too. */
17433 if (parser->num_template_parameter_lists == num_templates + 1)
17435 /* If there are more template classes than parameter lists, we have
17438 template <class T> void S<T>::R<T>::f (); */
17439 if (parser->num_template_parameter_lists < num_templates)
17442 error_at (location, "specializing member %<%T::%E%> "
17443 "requires %<template<>%> syntax",
17444 declarator->u.id.qualifying_scope,
17445 declarator->u.id.unqualified_name);
17447 error_at (location, "too few template-parameter-lists");
17450 /* Otherwise, there are too many template parameter lists. We have
17453 template <class T> template <class U> void S::f(); */
17454 error ("%Htoo many template-parameter-lists", &location);
17458 /* Parse an optional `::' token indicating that the following name is
17459 from the global namespace. If so, PARSER->SCOPE is set to the
17460 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
17461 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
17462 Returns the new value of PARSER->SCOPE, if the `::' token is
17463 present, and NULL_TREE otherwise. */
17466 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
17470 /* Peek at the next token. */
17471 token = cp_lexer_peek_token (parser->lexer);
17472 /* If we're looking at a `::' token then we're starting from the
17473 global namespace, not our current location. */
17474 if (token->type == CPP_SCOPE)
17476 /* Consume the `::' token. */
17477 cp_lexer_consume_token (parser->lexer);
17478 /* Set the SCOPE so that we know where to start the lookup. */
17479 parser->scope = global_namespace;
17480 parser->qualifying_scope = global_namespace;
17481 parser->object_scope = NULL_TREE;
17483 return parser->scope;
17485 else if (!current_scope_valid_p)
17487 parser->scope = NULL_TREE;
17488 parser->qualifying_scope = NULL_TREE;
17489 parser->object_scope = NULL_TREE;
17495 /* Returns TRUE if the upcoming token sequence is the start of a
17496 constructor declarator. If FRIEND_P is true, the declarator is
17497 preceded by the `friend' specifier. */
17500 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
17502 bool constructor_p;
17503 tree type_decl = NULL_TREE;
17504 bool nested_name_p;
17505 cp_token *next_token;
17507 /* The common case is that this is not a constructor declarator, so
17508 try to avoid doing lots of work if at all possible. It's not
17509 valid declare a constructor at function scope. */
17510 if (parser->in_function_body)
17512 /* And only certain tokens can begin a constructor declarator. */
17513 next_token = cp_lexer_peek_token (parser->lexer);
17514 if (next_token->type != CPP_NAME
17515 && next_token->type != CPP_SCOPE
17516 && next_token->type != CPP_NESTED_NAME_SPECIFIER
17517 && next_token->type != CPP_TEMPLATE_ID)
17520 /* Parse tentatively; we are going to roll back all of the tokens
17522 cp_parser_parse_tentatively (parser);
17523 /* Assume that we are looking at a constructor declarator. */
17524 constructor_p = true;
17526 /* Look for the optional `::' operator. */
17527 cp_parser_global_scope_opt (parser,
17528 /*current_scope_valid_p=*/false);
17529 /* Look for the nested-name-specifier. */
17531 = (cp_parser_nested_name_specifier_opt (parser,
17532 /*typename_keyword_p=*/false,
17533 /*check_dependency_p=*/false,
17535 /*is_declaration=*/false)
17537 /* Outside of a class-specifier, there must be a
17538 nested-name-specifier. */
17539 if (!nested_name_p &&
17540 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
17542 constructor_p = false;
17543 /* If we still think that this might be a constructor-declarator,
17544 look for a class-name. */
17549 template <typename T> struct S { S(); };
17550 template <typename T> S<T>::S ();
17552 we must recognize that the nested `S' names a class.
17555 template <typename T> S<T>::S<T> ();
17557 we must recognize that the nested `S' names a template. */
17558 type_decl = cp_parser_class_name (parser,
17559 /*typename_keyword_p=*/false,
17560 /*template_keyword_p=*/false,
17562 /*check_dependency_p=*/false,
17563 /*class_head_p=*/false,
17564 /*is_declaration=*/false);
17565 /* If there was no class-name, then this is not a constructor. */
17566 constructor_p = !cp_parser_error_occurred (parser);
17569 /* If we're still considering a constructor, we have to see a `(',
17570 to begin the parameter-declaration-clause, followed by either a
17571 `)', an `...', or a decl-specifier. We need to check for a
17572 type-specifier to avoid being fooled into thinking that:
17576 is a constructor. (It is actually a function named `f' that
17577 takes one parameter (of type `int') and returns a value of type
17580 && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
17582 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
17583 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
17584 /* A parameter declaration begins with a decl-specifier,
17585 which is either the "attribute" keyword, a storage class
17586 specifier, or (usually) a type-specifier. */
17587 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
17590 tree pushed_scope = NULL_TREE;
17591 unsigned saved_num_template_parameter_lists;
17593 /* Names appearing in the type-specifier should be looked up
17594 in the scope of the class. */
17595 if (current_class_type)
17599 type = TREE_TYPE (type_decl);
17600 if (TREE_CODE (type) == TYPENAME_TYPE)
17602 type = resolve_typename_type (type,
17603 /*only_current_p=*/false);
17604 if (TREE_CODE (type) == TYPENAME_TYPE)
17606 cp_parser_abort_tentative_parse (parser);
17610 pushed_scope = push_scope (type);
17613 /* Inside the constructor parameter list, surrounding
17614 template-parameter-lists do not apply. */
17615 saved_num_template_parameter_lists
17616 = parser->num_template_parameter_lists;
17617 parser->num_template_parameter_lists = 0;
17619 /* Look for the type-specifier. */
17620 cp_parser_type_specifier (parser,
17621 CP_PARSER_FLAGS_NONE,
17622 /*decl_specs=*/NULL,
17623 /*is_declarator=*/true,
17624 /*declares_class_or_enum=*/NULL,
17625 /*is_cv_qualifier=*/NULL);
17627 parser->num_template_parameter_lists
17628 = saved_num_template_parameter_lists;
17630 /* Leave the scope of the class. */
17632 pop_scope (pushed_scope);
17634 constructor_p = !cp_parser_error_occurred (parser);
17638 constructor_p = false;
17639 /* We did not really want to consume any tokens. */
17640 cp_parser_abort_tentative_parse (parser);
17642 return constructor_p;
17645 /* Parse the definition of the function given by the DECL_SPECIFIERS,
17646 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
17647 they must be performed once we are in the scope of the function.
17649 Returns the function defined. */
17652 cp_parser_function_definition_from_specifiers_and_declarator
17653 (cp_parser* parser,
17654 cp_decl_specifier_seq *decl_specifiers,
17656 const cp_declarator *declarator)
17661 /* Begin the function-definition. */
17662 success_p = start_function (decl_specifiers, declarator, attributes);
17664 /* The things we're about to see are not directly qualified by any
17665 template headers we've seen thus far. */
17666 reset_specialization ();
17668 /* If there were names looked up in the decl-specifier-seq that we
17669 did not check, check them now. We must wait until we are in the
17670 scope of the function to perform the checks, since the function
17671 might be a friend. */
17672 perform_deferred_access_checks ();
17676 /* Skip the entire function. */
17677 cp_parser_skip_to_end_of_block_or_statement (parser);
17678 fn = error_mark_node;
17680 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
17682 /* Seen already, skip it. An error message has already been output. */
17683 cp_parser_skip_to_end_of_block_or_statement (parser);
17684 fn = current_function_decl;
17685 current_function_decl = NULL_TREE;
17686 /* If this is a function from a class, pop the nested class. */
17687 if (current_class_name)
17688 pop_nested_class ();
17691 fn = cp_parser_function_definition_after_declarator (parser,
17692 /*inline_p=*/false);
17697 /* Parse the part of a function-definition that follows the
17698 declarator. INLINE_P is TRUE iff this function is an inline
17699 function defined with a class-specifier.
17701 Returns the function defined. */
17704 cp_parser_function_definition_after_declarator (cp_parser* parser,
17708 bool ctor_initializer_p = false;
17709 bool saved_in_unbraced_linkage_specification_p;
17710 bool saved_in_function_body;
17711 unsigned saved_num_template_parameter_lists;
17714 saved_in_function_body = parser->in_function_body;
17715 parser->in_function_body = true;
17716 /* If the next token is `return', then the code may be trying to
17717 make use of the "named return value" extension that G++ used to
17719 token = cp_lexer_peek_token (parser->lexer);
17720 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
17722 /* Consume the `return' keyword. */
17723 cp_lexer_consume_token (parser->lexer);
17724 /* Look for the identifier that indicates what value is to be
17726 cp_parser_identifier (parser);
17727 /* Issue an error message. */
17728 error ("%Hnamed return values are no longer supported",
17730 /* Skip tokens until we reach the start of the function body. */
17733 cp_token *token = cp_lexer_peek_token (parser->lexer);
17734 if (token->type == CPP_OPEN_BRACE
17735 || token->type == CPP_EOF
17736 || token->type == CPP_PRAGMA_EOL)
17738 cp_lexer_consume_token (parser->lexer);
17741 /* The `extern' in `extern "C" void f () { ... }' does not apply to
17742 anything declared inside `f'. */
17743 saved_in_unbraced_linkage_specification_p
17744 = parser->in_unbraced_linkage_specification_p;
17745 parser->in_unbraced_linkage_specification_p = false;
17746 /* Inside the function, surrounding template-parameter-lists do not
17748 saved_num_template_parameter_lists
17749 = parser->num_template_parameter_lists;
17750 parser->num_template_parameter_lists = 0;
17751 /* If the next token is `try', then we are looking at a
17752 function-try-block. */
17753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
17754 ctor_initializer_p = cp_parser_function_try_block (parser);
17755 /* A function-try-block includes the function-body, so we only do
17756 this next part if we're not processing a function-try-block. */
17759 = cp_parser_ctor_initializer_opt_and_function_body (parser);
17761 /* Finish the function. */
17762 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
17763 (inline_p ? 2 : 0));
17764 /* Generate code for it, if necessary. */
17765 expand_or_defer_fn (fn);
17766 /* Restore the saved values. */
17767 parser->in_unbraced_linkage_specification_p
17768 = saved_in_unbraced_linkage_specification_p;
17769 parser->num_template_parameter_lists
17770 = saved_num_template_parameter_lists;
17771 parser->in_function_body = saved_in_function_body;
17776 /* Parse a template-declaration, assuming that the `export' (and
17777 `extern') keywords, if present, has already been scanned. MEMBER_P
17778 is as for cp_parser_template_declaration. */
17781 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
17783 tree decl = NULL_TREE;
17784 VEC (deferred_access_check,gc) *checks;
17785 tree parameter_list;
17786 bool friend_p = false;
17787 bool need_lang_pop;
17790 /* Look for the `template' keyword. */
17791 token = cp_lexer_peek_token (parser->lexer);
17792 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
17796 if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
17798 if (at_class_scope_p () && current_function_decl)
17800 /* 14.5.2.2 [temp.mem]
17802 A local class shall not have member templates. */
17803 error ("%Hinvalid declaration of member template in local class",
17805 cp_parser_skip_to_end_of_block_or_statement (parser);
17810 A template ... shall not have C linkage. */
17811 if (current_lang_name == lang_name_c)
17813 error ("%Htemplate with C linkage", &token->location);
17814 /* Give it C++ linkage to avoid confusing other parts of the
17816 push_lang_context (lang_name_cplusplus);
17817 need_lang_pop = true;
17820 need_lang_pop = false;
17822 /* We cannot perform access checks on the template parameter
17823 declarations until we know what is being declared, just as we
17824 cannot check the decl-specifier list. */
17825 push_deferring_access_checks (dk_deferred);
17827 /* If the next token is `>', then we have an invalid
17828 specialization. Rather than complain about an invalid template
17829 parameter, issue an error message here. */
17830 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
17832 cp_parser_error (parser, "invalid explicit specialization");
17833 begin_specialization ();
17834 parameter_list = NULL_TREE;
17837 /* Parse the template parameters. */
17838 parameter_list = cp_parser_template_parameter_list (parser);
17840 /* Get the deferred access checks from the parameter list. These
17841 will be checked once we know what is being declared, as for a
17842 member template the checks must be performed in the scope of the
17843 class containing the member. */
17844 checks = get_deferred_access_checks ();
17846 /* Look for the `>'. */
17847 cp_parser_skip_to_end_of_template_parameter_list (parser);
17848 /* We just processed one more parameter list. */
17849 ++parser->num_template_parameter_lists;
17850 /* If the next token is `template', there are more template
17852 if (cp_lexer_next_token_is_keyword (parser->lexer,
17854 cp_parser_template_declaration_after_export (parser, member_p);
17857 /* There are no access checks when parsing a template, as we do not
17858 know if a specialization will be a friend. */
17859 push_deferring_access_checks (dk_no_check);
17860 token = cp_lexer_peek_token (parser->lexer);
17861 decl = cp_parser_single_declaration (parser,
17864 /*explicit_specialization_p=*/false,
17866 pop_deferring_access_checks ();
17868 /* If this is a member template declaration, let the front
17870 if (member_p && !friend_p && decl)
17872 if (TREE_CODE (decl) == TYPE_DECL)
17873 cp_parser_check_access_in_redeclaration (decl, token->location);
17875 decl = finish_member_template_decl (decl);
17877 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
17878 make_friend_class (current_class_type, TREE_TYPE (decl),
17879 /*complain=*/true);
17881 /* We are done with the current parameter list. */
17882 --parser->num_template_parameter_lists;
17884 pop_deferring_access_checks ();
17887 finish_template_decl (parameter_list);
17889 /* Register member declarations. */
17890 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
17891 finish_member_declaration (decl);
17892 /* For the erroneous case of a template with C linkage, we pushed an
17893 implicit C++ linkage scope; exit that scope now. */
17895 pop_lang_context ();
17896 /* If DECL is a function template, we must return to parse it later.
17897 (Even though there is no definition, there might be default
17898 arguments that need handling.) */
17899 if (member_p && decl
17900 && (TREE_CODE (decl) == FUNCTION_DECL
17901 || DECL_FUNCTION_TEMPLATE_P (decl)))
17902 TREE_VALUE (parser->unparsed_functions_queues)
17903 = tree_cons (NULL_TREE, decl,
17904 TREE_VALUE (parser->unparsed_functions_queues));
17907 /* Perform the deferred access checks from a template-parameter-list.
17908 CHECKS is a TREE_LIST of access checks, as returned by
17909 get_deferred_access_checks. */
17912 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
17914 ++processing_template_parmlist;
17915 perform_access_checks (checks);
17916 --processing_template_parmlist;
17919 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
17920 `function-definition' sequence. MEMBER_P is true, this declaration
17921 appears in a class scope.
17923 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
17924 *FRIEND_P is set to TRUE iff the declaration is a friend. */
17927 cp_parser_single_declaration (cp_parser* parser,
17928 VEC (deferred_access_check,gc)* checks,
17930 bool explicit_specialization_p,
17933 int declares_class_or_enum;
17934 tree decl = NULL_TREE;
17935 cp_decl_specifier_seq decl_specifiers;
17936 bool function_definition_p = false;
17937 cp_token *decl_spec_token_start;
17939 /* This function is only used when processing a template
17941 gcc_assert (innermost_scope_kind () == sk_template_parms
17942 || innermost_scope_kind () == sk_template_spec);
17944 /* Defer access checks until we know what is being declared. */
17945 push_deferring_access_checks (dk_deferred);
17947 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17949 decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17950 cp_parser_decl_specifier_seq (parser,
17951 CP_PARSER_FLAGS_OPTIONAL,
17953 &declares_class_or_enum);
17955 *friend_p = cp_parser_friend_p (&decl_specifiers);
17957 /* There are no template typedefs. */
17958 if (decl_specifiers.specs[(int) ds_typedef])
17960 error ("%Htemplate declaration of %qs",
17961 &decl_spec_token_start->location, "typedef");
17962 decl = error_mark_node;
17965 /* Gather up the access checks that occurred the
17966 decl-specifier-seq. */
17967 stop_deferring_access_checks ();
17969 /* Check for the declaration of a template class. */
17970 if (declares_class_or_enum)
17972 if (cp_parser_declares_only_class_p (parser))
17974 decl = shadow_tag (&decl_specifiers);
17979 friend template <typename T> struct A<T>::B;
17982 A<T>::B will be represented by a TYPENAME_TYPE, and
17983 therefore not recognized by shadow_tag. */
17984 if (friend_p && *friend_p
17986 && decl_specifiers.type
17987 && TYPE_P (decl_specifiers.type))
17988 decl = decl_specifiers.type;
17990 if (decl && decl != error_mark_node)
17991 decl = TYPE_NAME (decl);
17993 decl = error_mark_node;
17995 /* Perform access checks for template parameters. */
17996 cp_parser_perform_template_parameter_access_checks (checks);
17999 /* If it's not a template class, try for a template function. If
18000 the next token is a `;', then this declaration does not declare
18001 anything. But, if there were errors in the decl-specifiers, then
18002 the error might well have come from an attempted class-specifier.
18003 In that case, there's no need to warn about a missing declarator. */
18005 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18006 || decl_specifiers.type != error_mark_node))
18008 decl = cp_parser_init_declarator (parser,
18011 /*function_definition_allowed_p=*/true,
18013 declares_class_or_enum,
18014 &function_definition_p);
18016 /* 7.1.1-1 [dcl.stc]
18018 A storage-class-specifier shall not be specified in an explicit
18019 specialization... */
18021 && explicit_specialization_p
18022 && decl_specifiers.storage_class != sc_none)
18024 error ("%Hexplicit template specialization cannot have a storage class",
18025 &decl_spec_token_start->location);
18026 decl = error_mark_node;
18030 pop_deferring_access_checks ();
18032 /* Clear any current qualification; whatever comes next is the start
18033 of something new. */
18034 parser->scope = NULL_TREE;
18035 parser->qualifying_scope = NULL_TREE;
18036 parser->object_scope = NULL_TREE;
18037 /* Look for a trailing `;' after the declaration. */
18038 if (!function_definition_p
18039 && (decl == error_mark_node
18040 || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18041 cp_parser_skip_to_end_of_block_or_statement (parser);
18046 /* Parse a cast-expression that is not the operand of a unary "&". */
18049 cp_parser_simple_cast_expression (cp_parser *parser)
18051 return cp_parser_cast_expression (parser, /*address_p=*/false,
18052 /*cast_p=*/false, NULL);
18055 /* Parse a functional cast to TYPE. Returns an expression
18056 representing the cast. */
18059 cp_parser_functional_cast (cp_parser* parser, tree type)
18062 tree expression_list;
18066 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18068 maybe_warn_cpp0x ("extended initializer lists");
18069 expression_list = cp_parser_braced_list (parser, &nonconst_p);
18070 CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18071 if (TREE_CODE (type) == TYPE_DECL)
18072 type = TREE_TYPE (type);
18073 return finish_compound_literal (type, expression_list);
18077 vec = cp_parser_parenthesized_expression_list (parser, false,
18079 /*allow_expansion_p=*/true,
18080 /*non_constant_p=*/NULL);
18082 expression_list = error_mark_node;
18085 expression_list = build_tree_list_vec (vec);
18086 release_tree_vector (vec);
18089 cast = build_functional_cast (type, expression_list,
18090 tf_warning_or_error);
18091 /* [expr.const]/1: In an integral constant expression "only type
18092 conversions to integral or enumeration type can be used". */
18093 if (TREE_CODE (type) == TYPE_DECL)
18094 type = TREE_TYPE (type);
18095 if (cast != error_mark_node
18096 && !cast_valid_in_integral_constant_expression_p (type)
18097 && (cp_parser_non_integral_constant_expression
18098 (parser, "a call to a constructor")))
18099 return error_mark_node;
18103 /* Save the tokens that make up the body of a member function defined
18104 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
18105 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
18106 specifiers applied to the declaration. Returns the FUNCTION_DECL
18107 for the member function. */
18110 cp_parser_save_member_function_body (cp_parser* parser,
18111 cp_decl_specifier_seq *decl_specifiers,
18112 cp_declarator *declarator,
18119 /* Create the function-declaration. */
18120 fn = start_method (decl_specifiers, declarator, attributes);
18121 /* If something went badly wrong, bail out now. */
18122 if (fn == error_mark_node)
18124 /* If there's a function-body, skip it. */
18125 if (cp_parser_token_starts_function_definition_p
18126 (cp_lexer_peek_token (parser->lexer)))
18127 cp_parser_skip_to_end_of_block_or_statement (parser);
18128 return error_mark_node;
18131 /* Remember it, if there default args to post process. */
18132 cp_parser_save_default_args (parser, fn);
18134 /* Save away the tokens that make up the body of the
18136 first = parser->lexer->next_token;
18137 /* We can have braced-init-list mem-initializers before the fn body. */
18138 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18140 cp_lexer_consume_token (parser->lexer);
18141 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18142 && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18144 /* cache_group will stop after an un-nested { } pair, too. */
18145 if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18148 /* variadic mem-inits have ... after the ')'. */
18149 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18150 cp_lexer_consume_token (parser->lexer);
18153 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18154 /* Handle function try blocks. */
18155 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18156 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18157 last = parser->lexer->next_token;
18159 /* Save away the inline definition; we will process it when the
18160 class is complete. */
18161 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18162 DECL_PENDING_INLINE_P (fn) = 1;
18164 /* We need to know that this was defined in the class, so that
18165 friend templates are handled correctly. */
18166 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18168 /* We're done with the inline definition. */
18169 finish_method (fn);
18171 /* Add FN to the queue of functions to be parsed later. */
18172 TREE_VALUE (parser->unparsed_functions_queues)
18173 = tree_cons (NULL_TREE, fn,
18174 TREE_VALUE (parser->unparsed_functions_queues));
18179 /* Parse a template-argument-list, as well as the trailing ">" (but
18180 not the opening ">"). See cp_parser_template_argument_list for the
18184 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18188 tree saved_qualifying_scope;
18189 tree saved_object_scope;
18190 bool saved_greater_than_is_operator_p;
18191 bool saved_skip_evaluation;
18195 When parsing a template-id, the first non-nested `>' is taken as
18196 the end of the template-argument-list rather than a greater-than
18198 saved_greater_than_is_operator_p
18199 = parser->greater_than_is_operator_p;
18200 parser->greater_than_is_operator_p = false;
18201 /* Parsing the argument list may modify SCOPE, so we save it
18203 saved_scope = parser->scope;
18204 saved_qualifying_scope = parser->qualifying_scope;
18205 saved_object_scope = parser->object_scope;
18206 /* We need to evaluate the template arguments, even though this
18207 template-id may be nested within a "sizeof". */
18208 saved_skip_evaluation = skip_evaluation;
18209 skip_evaluation = false;
18210 /* Parse the template-argument-list itself. */
18211 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18212 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18213 arguments = NULL_TREE;
18215 arguments = cp_parser_template_argument_list (parser);
18216 /* Look for the `>' that ends the template-argument-list. If we find
18217 a '>>' instead, it's probably just a typo. */
18218 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18220 if (cxx_dialect != cxx98)
18222 /* In C++0x, a `>>' in a template argument list or cast
18223 expression is considered to be two separate `>'
18224 tokens. So, change the current token to a `>', but don't
18225 consume it: it will be consumed later when the outer
18226 template argument list (or cast expression) is parsed.
18227 Note that this replacement of `>' for `>>' is necessary
18228 even if we are parsing tentatively: in the tentative
18229 case, after calling
18230 cp_parser_enclosed_template_argument_list we will always
18231 throw away all of the template arguments and the first
18232 closing `>', either because the template argument list
18233 was erroneous or because we are replacing those tokens
18234 with a CPP_TEMPLATE_ID token. The second `>' (which will
18235 not have been thrown away) is needed either to close an
18236 outer template argument list or to complete a new-style
18238 cp_token *token = cp_lexer_peek_token (parser->lexer);
18239 token->type = CPP_GREATER;
18241 else if (!saved_greater_than_is_operator_p)
18243 /* If we're in a nested template argument list, the '>>' has
18244 to be a typo for '> >'. We emit the error message, but we
18245 continue parsing and we push a '>' as next token, so that
18246 the argument list will be parsed correctly. Note that the
18247 global source location is still on the token before the
18248 '>>', so we need to say explicitly where we want it. */
18249 cp_token *token = cp_lexer_peek_token (parser->lexer);
18250 error ("%H%<>>%> should be %<> >%> "
18251 "within a nested template argument list",
18254 token->type = CPP_GREATER;
18258 /* If this is not a nested template argument list, the '>>'
18259 is a typo for '>'. Emit an error message and continue.
18260 Same deal about the token location, but here we can get it
18261 right by consuming the '>>' before issuing the diagnostic. */
18262 cp_token *token = cp_lexer_consume_token (parser->lexer);
18263 error ("%Hspurious %<>>%>, use %<>%> to terminate "
18264 "a template argument list", &token->location);
18268 cp_parser_skip_to_end_of_template_parameter_list (parser);
18269 /* The `>' token might be a greater-than operator again now. */
18270 parser->greater_than_is_operator_p
18271 = saved_greater_than_is_operator_p;
18272 /* Restore the SAVED_SCOPE. */
18273 parser->scope = saved_scope;
18274 parser->qualifying_scope = saved_qualifying_scope;
18275 parser->object_scope = saved_object_scope;
18276 skip_evaluation = saved_skip_evaluation;
18281 /* MEMBER_FUNCTION is a member function, or a friend. If default
18282 arguments, or the body of the function have not yet been parsed,
18286 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
18288 /* If this member is a template, get the underlying
18290 if (DECL_FUNCTION_TEMPLATE_P (member_function))
18291 member_function = DECL_TEMPLATE_RESULT (member_function);
18293 /* There should not be any class definitions in progress at this
18294 point; the bodies of members are only parsed outside of all class
18296 gcc_assert (parser->num_classes_being_defined == 0);
18297 /* While we're parsing the member functions we might encounter more
18298 classes. We want to handle them right away, but we don't want
18299 them getting mixed up with functions that are currently in the
18301 parser->unparsed_functions_queues
18302 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18304 /* Make sure that any template parameters are in scope. */
18305 maybe_begin_member_template_processing (member_function);
18307 /* If the body of the function has not yet been parsed, parse it
18309 if (DECL_PENDING_INLINE_P (member_function))
18311 tree function_scope;
18312 cp_token_cache *tokens;
18314 /* The function is no longer pending; we are processing it. */
18315 tokens = DECL_PENDING_INLINE_INFO (member_function);
18316 DECL_PENDING_INLINE_INFO (member_function) = NULL;
18317 DECL_PENDING_INLINE_P (member_function) = 0;
18319 /* If this is a local class, enter the scope of the containing
18321 function_scope = current_function_decl;
18322 if (function_scope)
18323 push_function_context ();
18325 /* Push the body of the function onto the lexer stack. */
18326 cp_parser_push_lexer_for_tokens (parser, tokens);
18328 /* Let the front end know that we going to be defining this
18330 start_preparsed_function (member_function, NULL_TREE,
18331 SF_PRE_PARSED | SF_INCLASS_INLINE);
18333 /* Don't do access checking if it is a templated function. */
18334 if (processing_template_decl)
18335 push_deferring_access_checks (dk_no_check);
18337 /* Now, parse the body of the function. */
18338 cp_parser_function_definition_after_declarator (parser,
18339 /*inline_p=*/true);
18341 if (processing_template_decl)
18342 pop_deferring_access_checks ();
18344 /* Leave the scope of the containing function. */
18345 if (function_scope)
18346 pop_function_context ();
18347 cp_parser_pop_lexer (parser);
18350 /* Remove any template parameters from the symbol table. */
18351 maybe_end_member_template_processing ();
18353 /* Restore the queue. */
18354 parser->unparsed_functions_queues
18355 = TREE_CHAIN (parser->unparsed_functions_queues);
18358 /* If DECL contains any default args, remember it on the unparsed
18359 functions queue. */
18362 cp_parser_save_default_args (cp_parser* parser, tree decl)
18366 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
18368 probe = TREE_CHAIN (probe))
18369 if (TREE_PURPOSE (probe))
18371 TREE_PURPOSE (parser->unparsed_functions_queues)
18372 = tree_cons (current_class_type, decl,
18373 TREE_PURPOSE (parser->unparsed_functions_queues));
18378 /* FN is a FUNCTION_DECL which may contains a parameter with an
18379 unparsed DEFAULT_ARG. Parse the default args now. This function
18380 assumes that the current scope is the scope in which the default
18381 argument should be processed. */
18384 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
18386 bool saved_local_variables_forbidden_p;
18389 /* While we're parsing the default args, we might (due to the
18390 statement expression extension) encounter more classes. We want
18391 to handle them right away, but we don't want them getting mixed
18392 up with default args that are currently in the queue. */
18393 parser->unparsed_functions_queues
18394 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
18396 /* Local variable names (and the `this' keyword) may not appear
18397 in a default argument. */
18398 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
18399 parser->local_variables_forbidden_p = true;
18401 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
18403 parm = TREE_CHAIN (parm))
18405 cp_token_cache *tokens;
18406 tree default_arg = TREE_PURPOSE (parm);
18408 VEC(tree,gc) *insts;
18415 if (TREE_CODE (default_arg) != DEFAULT_ARG)
18416 /* This can happen for a friend declaration for a function
18417 already declared with default arguments. */
18420 /* Push the saved tokens for the default argument onto the parser's
18422 tokens = DEFARG_TOKENS (default_arg);
18423 cp_parser_push_lexer_for_tokens (parser, tokens);
18425 /* Parse the assignment-expression. */
18426 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
18427 if (parsed_arg == error_mark_node)
18429 cp_parser_pop_lexer (parser);
18433 if (!processing_template_decl)
18434 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
18436 TREE_PURPOSE (parm) = parsed_arg;
18438 /* Update any instantiations we've already created. */
18439 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
18440 VEC_iterate (tree, insts, ix, copy); ix++)
18441 TREE_PURPOSE (copy) = parsed_arg;
18443 /* If the token stream has not been completely used up, then
18444 there was extra junk after the end of the default
18446 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
18447 cp_parser_error (parser, "expected %<,%>");
18449 /* Revert to the main lexer. */
18450 cp_parser_pop_lexer (parser);
18453 /* Make sure no default arg is missing. */
18454 check_default_args (fn);
18456 /* Restore the state of local_variables_forbidden_p. */
18457 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
18459 /* Restore the queue. */
18460 parser->unparsed_functions_queues
18461 = TREE_CHAIN (parser->unparsed_functions_queues);
18464 /* Parse the operand of `sizeof' (or a similar operator). Returns
18465 either a TYPE or an expression, depending on the form of the
18466 input. The KEYWORD indicates which kind of expression we have
18470 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
18472 tree expr = NULL_TREE;
18473 const char *saved_message;
18475 bool saved_integral_constant_expression_p;
18476 bool saved_non_integral_constant_expression_p;
18477 bool pack_expansion_p = false;
18479 /* Types cannot be defined in a `sizeof' expression. Save away the
18481 saved_message = parser->type_definition_forbidden_message;
18482 /* And create the new one. */
18483 tmp = concat ("types may not be defined in %<",
18484 IDENTIFIER_POINTER (ridpointers[keyword]),
18485 "%> expressions", NULL);
18486 parser->type_definition_forbidden_message = tmp;
18488 /* The restrictions on constant-expressions do not apply inside
18489 sizeof expressions. */
18490 saved_integral_constant_expression_p
18491 = parser->integral_constant_expression_p;
18492 saved_non_integral_constant_expression_p
18493 = parser->non_integral_constant_expression_p;
18494 parser->integral_constant_expression_p = false;
18496 /* If it's a `...', then we are computing the length of a parameter
18498 if (keyword == RID_SIZEOF
18499 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18501 /* Consume the `...'. */
18502 cp_lexer_consume_token (parser->lexer);
18503 maybe_warn_variadic_templates ();
18505 /* Note that this is an expansion. */
18506 pack_expansion_p = true;
18509 /* Do not actually evaluate the expression. */
18511 /* If it's a `(', then we might be looking at the type-id
18513 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18516 bool saved_in_type_id_in_expr_p;
18518 /* We can't be sure yet whether we're looking at a type-id or an
18520 cp_parser_parse_tentatively (parser);
18521 /* Consume the `('. */
18522 cp_lexer_consume_token (parser->lexer);
18523 /* Parse the type-id. */
18524 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
18525 parser->in_type_id_in_expr_p = true;
18526 type = cp_parser_type_id (parser);
18527 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
18528 /* Now, look for the trailing `)'. */
18529 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
18530 /* If all went well, then we're done. */
18531 if (cp_parser_parse_definitely (parser))
18533 cp_decl_specifier_seq decl_specs;
18535 /* Build a trivial decl-specifier-seq. */
18536 clear_decl_specs (&decl_specs);
18537 decl_specs.type = type;
18539 /* Call grokdeclarator to figure out what type this is. */
18540 expr = grokdeclarator (NULL,
18544 /*attrlist=*/NULL);
18548 /* If the type-id production did not work out, then we must be
18549 looking at the unary-expression production. */
18551 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
18552 /*cast_p=*/false, NULL);
18554 if (pack_expansion_p)
18555 /* Build a pack expansion. */
18556 expr = make_pack_expansion (expr);
18558 /* Go back to evaluating expressions. */
18561 /* Free the message we created. */
18563 /* And restore the old one. */
18564 parser->type_definition_forbidden_message = saved_message;
18565 parser->integral_constant_expression_p
18566 = saved_integral_constant_expression_p;
18567 parser->non_integral_constant_expression_p
18568 = saved_non_integral_constant_expression_p;
18573 /* If the current declaration has no declarator, return true. */
18576 cp_parser_declares_only_class_p (cp_parser *parser)
18578 /* If the next token is a `;' or a `,' then there is no
18580 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
18581 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
18584 /* Update the DECL_SPECS to reflect the storage class indicated by
18588 cp_parser_set_storage_class (cp_parser *parser,
18589 cp_decl_specifier_seq *decl_specs,
18591 location_t location)
18593 cp_storage_class storage_class;
18595 if (parser->in_unbraced_linkage_specification_p)
18597 error ("%Hinvalid use of %qD in linkage specification",
18598 &location, ridpointers[keyword]);
18601 else if (decl_specs->storage_class != sc_none)
18603 decl_specs->conflicting_specifiers_p = true;
18607 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
18608 && decl_specs->specs[(int) ds_thread])
18610 error ("%H%<__thread%> before %qD", &location, ridpointers[keyword]);
18611 decl_specs->specs[(int) ds_thread] = 0;
18617 storage_class = sc_auto;
18620 storage_class = sc_register;
18623 storage_class = sc_static;
18626 storage_class = sc_extern;
18629 storage_class = sc_mutable;
18632 gcc_unreachable ();
18634 decl_specs->storage_class = storage_class;
18636 /* A storage class specifier cannot be applied alongside a typedef
18637 specifier. If there is a typedef specifier present then set
18638 conflicting_specifiers_p which will trigger an error later
18639 on in grokdeclarator. */
18640 if (decl_specs->specs[(int)ds_typedef])
18641 decl_specs->conflicting_specifiers_p = true;
18644 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
18645 is true, the type is a user-defined type; otherwise it is a
18646 built-in type specified by a keyword. */
18649 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
18651 location_t location,
18652 bool user_defined_p)
18654 decl_specs->any_specifiers_p = true;
18656 /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
18657 (with, for example, in "typedef int wchar_t;") we remember that
18658 this is what happened. In system headers, we ignore these
18659 declarations so that G++ can work with system headers that are not
18661 if (decl_specs->specs[(int) ds_typedef]
18663 && (type_spec == boolean_type_node
18664 || type_spec == char16_type_node
18665 || type_spec == char32_type_node
18666 || type_spec == wchar_type_node)
18667 && (decl_specs->type
18668 || decl_specs->specs[(int) ds_long]
18669 || decl_specs->specs[(int) ds_short]
18670 || decl_specs->specs[(int) ds_unsigned]
18671 || decl_specs->specs[(int) ds_signed]))
18673 decl_specs->redefined_builtin_type = type_spec;
18674 if (!decl_specs->type)
18676 decl_specs->type = type_spec;
18677 decl_specs->user_defined_type_p = false;
18678 decl_specs->type_location = location;
18681 else if (decl_specs->type)
18682 decl_specs->multiple_types_p = true;
18685 decl_specs->type = type_spec;
18686 decl_specs->user_defined_type_p = user_defined_p;
18687 decl_specs->redefined_builtin_type = NULL_TREE;
18688 decl_specs->type_location = location;
18692 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
18693 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
18696 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
18698 return decl_specifiers->specs[(int) ds_friend] != 0;
18701 /* If the next token is of the indicated TYPE, consume it. Otherwise,
18702 issue an error message indicating that TOKEN_DESC was expected.
18704 Returns the token consumed, if the token had the appropriate type.
18705 Otherwise, returns NULL. */
18708 cp_parser_require (cp_parser* parser,
18709 enum cpp_ttype type,
18710 const char* token_desc)
18712 if (cp_lexer_next_token_is (parser->lexer, type))
18713 return cp_lexer_consume_token (parser->lexer);
18716 /* Output the MESSAGE -- unless we're parsing tentatively. */
18717 if (!cp_parser_simulate_error (parser))
18719 char *message = concat ("expected ", token_desc, NULL);
18720 cp_parser_error (parser, message);
18727 /* An error message is produced if the next token is not '>'.
18728 All further tokens are skipped until the desired token is
18729 found or '{', '}', ';' or an unbalanced ')' or ']'. */
18732 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
18734 /* Current level of '< ... >'. */
18735 unsigned level = 0;
18736 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
18737 unsigned nesting_depth = 0;
18739 /* Are we ready, yet? If not, issue error message. */
18740 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
18743 /* Skip tokens until the desired token is found. */
18746 /* Peek at the next token. */
18747 switch (cp_lexer_peek_token (parser->lexer)->type)
18750 if (!nesting_depth)
18755 if (cxx_dialect == cxx98)
18756 /* C++0x views the `>>' operator as two `>' tokens, but
18759 else if (!nesting_depth && level-- == 0)
18761 /* We've hit a `>>' where the first `>' closes the
18762 template argument list, and the second `>' is
18763 spurious. Just consume the `>>' and stop; we've
18764 already produced at least one error. */
18765 cp_lexer_consume_token (parser->lexer);
18768 /* Fall through for C++0x, so we handle the second `>' in
18772 if (!nesting_depth && level-- == 0)
18774 /* We've reached the token we want, consume it and stop. */
18775 cp_lexer_consume_token (parser->lexer);
18780 case CPP_OPEN_PAREN:
18781 case CPP_OPEN_SQUARE:
18785 case CPP_CLOSE_PAREN:
18786 case CPP_CLOSE_SQUARE:
18787 if (nesting_depth-- == 0)
18792 case CPP_PRAGMA_EOL:
18793 case CPP_SEMICOLON:
18794 case CPP_OPEN_BRACE:
18795 case CPP_CLOSE_BRACE:
18796 /* The '>' was probably forgotten, don't look further. */
18803 /* Consume this token. */
18804 cp_lexer_consume_token (parser->lexer);
18808 /* If the next token is the indicated keyword, consume it. Otherwise,
18809 issue an error message indicating that TOKEN_DESC was expected.
18811 Returns the token consumed, if the token had the appropriate type.
18812 Otherwise, returns NULL. */
18815 cp_parser_require_keyword (cp_parser* parser,
18817 const char* token_desc)
18819 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
18821 if (token && token->keyword != keyword)
18823 dyn_string_t error_msg;
18825 /* Format the error message. */
18826 error_msg = dyn_string_new (0);
18827 dyn_string_append_cstr (error_msg, "expected ");
18828 dyn_string_append_cstr (error_msg, token_desc);
18829 cp_parser_error (parser, error_msg->s);
18830 dyn_string_delete (error_msg);
18837 /* Returns TRUE iff TOKEN is a token that can begin the body of a
18838 function-definition. */
18841 cp_parser_token_starts_function_definition_p (cp_token* token)
18843 return (/* An ordinary function-body begins with an `{'. */
18844 token->type == CPP_OPEN_BRACE
18845 /* A ctor-initializer begins with a `:'. */
18846 || token->type == CPP_COLON
18847 /* A function-try-block begins with `try'. */
18848 || token->keyword == RID_TRY
18849 /* The named return value extension begins with `return'. */
18850 || token->keyword == RID_RETURN);
18853 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
18857 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
18861 token = cp_lexer_peek_token (parser->lexer);
18862 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
18865 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
18866 C++0x) ending a template-argument. */
18869 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
18873 token = cp_lexer_peek_token (parser->lexer);
18874 return (token->type == CPP_COMMA
18875 || token->type == CPP_GREATER
18876 || token->type == CPP_ELLIPSIS
18877 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
18880 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
18881 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
18884 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
18889 token = cp_lexer_peek_nth_token (parser->lexer, n);
18890 if (token->type == CPP_LESS)
18892 /* Check for the sequence `<::' in the original code. It would be lexed as
18893 `[:', where `[' is a digraph, and there is no whitespace before
18895 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
18898 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
18899 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
18905 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
18906 or none_type otherwise. */
18908 static enum tag_types
18909 cp_parser_token_is_class_key (cp_token* token)
18911 switch (token->keyword)
18916 return record_type;
18925 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
18928 cp_parser_check_class_key (enum tag_types class_key, tree type)
18930 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
18931 permerror (input_location, "%qs tag used in naming %q#T",
18932 class_key == union_type ? "union"
18933 : class_key == record_type ? "struct" : "class",
18937 /* Issue an error message if DECL is redeclared with different
18938 access than its original declaration [class.access.spec/3].
18939 This applies to nested classes and nested class templates.
18943 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
18945 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
18948 if ((TREE_PRIVATE (decl)
18949 != (current_access_specifier == access_private_node))
18950 || (TREE_PROTECTED (decl)
18951 != (current_access_specifier == access_protected_node)))
18952 error ("%H%qD redeclared with different access", &location, decl);
18955 /* Look for the `template' keyword, as a syntactic disambiguator.
18956 Return TRUE iff it is present, in which case it will be
18960 cp_parser_optional_template_keyword (cp_parser *parser)
18962 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18964 /* The `template' keyword can only be used within templates;
18965 outside templates the parser can always figure out what is a
18966 template and what is not. */
18967 if (!processing_template_decl)
18969 cp_token *token = cp_lexer_peek_token (parser->lexer);
18970 error ("%H%<template%> (as a disambiguator) is only allowed "
18971 "within templates", &token->location);
18972 /* If this part of the token stream is rescanned, the same
18973 error message would be generated. So, we purge the token
18974 from the stream. */
18975 cp_lexer_purge_token (parser->lexer);
18980 /* Consume the `template' keyword. */
18981 cp_lexer_consume_token (parser->lexer);
18989 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18990 set PARSER->SCOPE, and perform other related actions. */
18993 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18996 struct tree_check *check_value;
18997 deferred_access_check *chk;
18998 VEC (deferred_access_check,gc) *checks;
19000 /* Get the stored value. */
19001 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19002 /* Perform any access checks that were deferred. */
19003 checks = check_value->checks;
19007 VEC_iterate (deferred_access_check, checks, i, chk) ;
19010 perform_or_defer_access_check (chk->binfo,
19015 /* Set the scope from the stored value. */
19016 parser->scope = check_value->value;
19017 parser->qualifying_scope = check_value->qualifying_scope;
19018 parser->object_scope = NULL_TREE;
19021 /* Consume tokens up through a non-nested END token. Returns TRUE if we
19022 encounter the end of a block before what we were looking for. */
19025 cp_parser_cache_group (cp_parser *parser,
19026 enum cpp_ttype end,
19031 cp_token *token = cp_lexer_peek_token (parser->lexer);
19033 /* Abort a parenthesized expression if we encounter a semicolon. */
19034 if ((end == CPP_CLOSE_PAREN || depth == 0)
19035 && token->type == CPP_SEMICOLON)
19037 /* If we've reached the end of the file, stop. */
19038 if (token->type == CPP_EOF
19039 || (end != CPP_PRAGMA_EOL
19040 && token->type == CPP_PRAGMA_EOL))
19042 if (token->type == CPP_CLOSE_BRACE && depth == 0)
19043 /* We've hit the end of an enclosing block, so there's been some
19044 kind of syntax error. */
19047 /* Consume the token. */
19048 cp_lexer_consume_token (parser->lexer);
19049 /* See if it starts a new group. */
19050 if (token->type == CPP_OPEN_BRACE)
19052 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19053 /* In theory this should probably check end == '}', but
19054 cp_parser_save_member_function_body needs it to exit
19055 after either '}' or ')' when called with ')'. */
19059 else if (token->type == CPP_OPEN_PAREN)
19061 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19062 if (depth == 0 && end == CPP_CLOSE_PAREN)
19065 else if (token->type == CPP_PRAGMA)
19066 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19067 else if (token->type == end)
19072 /* Begin parsing tentatively. We always save tokens while parsing
19073 tentatively so that if the tentative parsing fails we can restore the
19077 cp_parser_parse_tentatively (cp_parser* parser)
19079 /* Enter a new parsing context. */
19080 parser->context = cp_parser_context_new (parser->context);
19081 /* Begin saving tokens. */
19082 cp_lexer_save_tokens (parser->lexer);
19083 /* In order to avoid repetitive access control error messages,
19084 access checks are queued up until we are no longer parsing
19086 push_deferring_access_checks (dk_deferred);
19089 /* Commit to the currently active tentative parse. */
19092 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19094 cp_parser_context *context;
19097 /* Mark all of the levels as committed. */
19098 lexer = parser->lexer;
19099 for (context = parser->context; context->next; context = context->next)
19101 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19103 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19104 while (!cp_lexer_saving_tokens (lexer))
19105 lexer = lexer->next;
19106 cp_lexer_commit_tokens (lexer);
19110 /* Abort the currently active tentative parse. All consumed tokens
19111 will be rolled back, and no diagnostics will be issued. */
19114 cp_parser_abort_tentative_parse (cp_parser* parser)
19116 cp_parser_simulate_error (parser);
19117 /* Now, pretend that we want to see if the construct was
19118 successfully parsed. */
19119 cp_parser_parse_definitely (parser);
19122 /* Stop parsing tentatively. If a parse error has occurred, restore the
19123 token stream. Otherwise, commit to the tokens we have consumed.
19124 Returns true if no error occurred; false otherwise. */
19127 cp_parser_parse_definitely (cp_parser* parser)
19129 bool error_occurred;
19130 cp_parser_context *context;
19132 /* Remember whether or not an error occurred, since we are about to
19133 destroy that information. */
19134 error_occurred = cp_parser_error_occurred (parser);
19135 /* Remove the topmost context from the stack. */
19136 context = parser->context;
19137 parser->context = context->next;
19138 /* If no parse errors occurred, commit to the tentative parse. */
19139 if (!error_occurred)
19141 /* Commit to the tokens read tentatively, unless that was
19143 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19144 cp_lexer_commit_tokens (parser->lexer);
19146 pop_to_parent_deferring_access_checks ();
19148 /* Otherwise, if errors occurred, roll back our state so that things
19149 are just as they were before we began the tentative parse. */
19152 cp_lexer_rollback_tokens (parser->lexer);
19153 pop_deferring_access_checks ();
19155 /* Add the context to the front of the free list. */
19156 context->next = cp_parser_context_free_list;
19157 cp_parser_context_free_list = context;
19159 return !error_occurred;
19162 /* Returns true if we are parsing tentatively and are not committed to
19163 this tentative parse. */
19166 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19168 return (cp_parser_parsing_tentatively (parser)
19169 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19172 /* Returns nonzero iff an error has occurred during the most recent
19173 tentative parse. */
19176 cp_parser_error_occurred (cp_parser* parser)
19178 return (cp_parser_parsing_tentatively (parser)
19179 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19182 /* Returns nonzero if GNU extensions are allowed. */
19185 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19187 return parser->allow_gnu_extensions_p;
19190 /* Objective-C++ Productions */
19193 /* Parse an Objective-C expression, which feeds into a primary-expression
19197 objc-message-expression
19198 objc-string-literal
19199 objc-encode-expression
19200 objc-protocol-expression
19201 objc-selector-expression
19203 Returns a tree representation of the expression. */
19206 cp_parser_objc_expression (cp_parser* parser)
19208 /* Try to figure out what kind of declaration is present. */
19209 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19213 case CPP_OPEN_SQUARE:
19214 return cp_parser_objc_message_expression (parser);
19216 case CPP_OBJC_STRING:
19217 kwd = cp_lexer_consume_token (parser->lexer);
19218 return objc_build_string_object (kwd->u.value);
19221 switch (kwd->keyword)
19223 case RID_AT_ENCODE:
19224 return cp_parser_objc_encode_expression (parser);
19226 case RID_AT_PROTOCOL:
19227 return cp_parser_objc_protocol_expression (parser);
19229 case RID_AT_SELECTOR:
19230 return cp_parser_objc_selector_expression (parser);
19236 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
19237 &kwd->location, kwd->u.value);
19238 cp_parser_skip_to_end_of_block_or_statement (parser);
19241 return error_mark_node;
19244 /* Parse an Objective-C message expression.
19246 objc-message-expression:
19247 [ objc-message-receiver objc-message-args ]
19249 Returns a representation of an Objective-C message. */
19252 cp_parser_objc_message_expression (cp_parser* parser)
19254 tree receiver, messageargs;
19256 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
19257 receiver = cp_parser_objc_message_receiver (parser);
19258 messageargs = cp_parser_objc_message_args (parser);
19259 cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
19261 return objc_build_message_expr (build_tree_list (receiver, messageargs));
19264 /* Parse an objc-message-receiver.
19266 objc-message-receiver:
19268 simple-type-specifier
19270 Returns a representation of the type or expression. */
19273 cp_parser_objc_message_receiver (cp_parser* parser)
19277 /* An Objective-C message receiver may be either (1) a type
19278 or (2) an expression. */
19279 cp_parser_parse_tentatively (parser);
19280 rcv = cp_parser_expression (parser, false, NULL);
19282 if (cp_parser_parse_definitely (parser))
19285 rcv = cp_parser_simple_type_specifier (parser,
19286 /*decl_specs=*/NULL,
19287 CP_PARSER_FLAGS_NONE);
19289 return objc_get_class_reference (rcv);
19292 /* Parse the arguments and selectors comprising an Objective-C message.
19297 objc-selector-args , objc-comma-args
19299 objc-selector-args:
19300 objc-selector [opt] : assignment-expression
19301 objc-selector-args objc-selector [opt] : assignment-expression
19304 assignment-expression
19305 objc-comma-args , assignment-expression
19307 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
19308 selector arguments and TREE_VALUE containing a list of comma
19312 cp_parser_objc_message_args (cp_parser* parser)
19314 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
19315 bool maybe_unary_selector_p = true;
19316 cp_token *token = cp_lexer_peek_token (parser->lexer);
19318 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19320 tree selector = NULL_TREE, arg;
19322 if (token->type != CPP_COLON)
19323 selector = cp_parser_objc_selector (parser);
19325 /* Detect if we have a unary selector. */
19326 if (maybe_unary_selector_p
19327 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19328 return build_tree_list (selector, NULL_TREE);
19330 maybe_unary_selector_p = false;
19331 cp_parser_require (parser, CPP_COLON, "%<:%>");
19332 arg = cp_parser_assignment_expression (parser, false, NULL);
19335 = chainon (sel_args,
19336 build_tree_list (selector, arg));
19338 token = cp_lexer_peek_token (parser->lexer);
19341 /* Handle non-selector arguments, if any. */
19342 while (token->type == CPP_COMMA)
19346 cp_lexer_consume_token (parser->lexer);
19347 arg = cp_parser_assignment_expression (parser, false, NULL);
19350 = chainon (addl_args,
19351 build_tree_list (NULL_TREE, arg));
19353 token = cp_lexer_peek_token (parser->lexer);
19356 return build_tree_list (sel_args, addl_args);
19359 /* Parse an Objective-C encode expression.
19361 objc-encode-expression:
19362 @encode objc-typename
19364 Returns an encoded representation of the type argument. */
19367 cp_parser_objc_encode_expression (cp_parser* parser)
19372 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
19373 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19374 token = cp_lexer_peek_token (parser->lexer);
19375 type = complete_type (cp_parser_type_id (parser));
19376 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19380 error ("%H%<@encode%> must specify a type as an argument",
19382 return error_mark_node;
19385 return objc_build_encode_expr (type);
19388 /* Parse an Objective-C @defs expression. */
19391 cp_parser_objc_defs_expression (cp_parser *parser)
19395 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
19396 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19397 name = cp_parser_identifier (parser);
19398 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19400 return objc_get_class_ivars (name);
19403 /* Parse an Objective-C protocol expression.
19405 objc-protocol-expression:
19406 @protocol ( identifier )
19408 Returns a representation of the protocol expression. */
19411 cp_parser_objc_protocol_expression (cp_parser* parser)
19415 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19416 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19417 proto = cp_parser_identifier (parser);
19418 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19420 return objc_build_protocol_expr (proto);
19423 /* Parse an Objective-C selector expression.
19425 objc-selector-expression:
19426 @selector ( objc-method-signature )
19428 objc-method-signature:
19434 objc-selector-seq objc-selector :
19436 Returns a representation of the method selector. */
19439 cp_parser_objc_selector_expression (cp_parser* parser)
19441 tree sel_seq = NULL_TREE;
19442 bool maybe_unary_selector_p = true;
19444 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
19446 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
19447 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
19448 token = cp_lexer_peek_token (parser->lexer);
19450 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
19451 || token->type == CPP_SCOPE)
19453 tree selector = NULL_TREE;
19455 if (token->type != CPP_COLON
19456 || token->type == CPP_SCOPE)
19457 selector = cp_parser_objc_selector (parser);
19459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
19460 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
19462 /* Detect if we have a unary selector. */
19463 if (maybe_unary_selector_p)
19465 sel_seq = selector;
19466 goto finish_selector;
19470 cp_parser_error (parser, "expected %<:%>");
19473 maybe_unary_selector_p = false;
19474 token = cp_lexer_consume_token (parser->lexer);
19476 if (token->type == CPP_SCOPE)
19479 = chainon (sel_seq,
19480 build_tree_list (selector, NULL_TREE));
19482 = chainon (sel_seq,
19483 build_tree_list (NULL_TREE, NULL_TREE));
19487 = chainon (sel_seq,
19488 build_tree_list (selector, NULL_TREE));
19490 token = cp_lexer_peek_token (parser->lexer);
19494 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19496 return objc_build_selector_expr (loc, sel_seq);
19499 /* Parse a list of identifiers.
19501 objc-identifier-list:
19503 objc-identifier-list , identifier
19505 Returns a TREE_LIST of identifier nodes. */
19508 cp_parser_objc_identifier_list (cp_parser* parser)
19510 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
19511 cp_token *sep = cp_lexer_peek_token (parser->lexer);
19513 while (sep->type == CPP_COMMA)
19515 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19516 list = chainon (list,
19517 build_tree_list (NULL_TREE,
19518 cp_parser_identifier (parser)));
19519 sep = cp_lexer_peek_token (parser->lexer);
19525 /* Parse an Objective-C alias declaration.
19527 objc-alias-declaration:
19528 @compatibility_alias identifier identifier ;
19530 This function registers the alias mapping with the Objective-C front end.
19531 It returns nothing. */
19534 cp_parser_objc_alias_declaration (cp_parser* parser)
19538 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
19539 alias = cp_parser_identifier (parser);
19540 orig = cp_parser_identifier (parser);
19541 objc_declare_alias (alias, orig);
19542 cp_parser_consume_semicolon_at_end_of_statement (parser);
19545 /* Parse an Objective-C class forward-declaration.
19547 objc-class-declaration:
19548 @class objc-identifier-list ;
19550 The function registers the forward declarations with the Objective-C
19551 front end. It returns nothing. */
19554 cp_parser_objc_class_declaration (cp_parser* parser)
19556 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
19557 objc_declare_class (cp_parser_objc_identifier_list (parser));
19558 cp_parser_consume_semicolon_at_end_of_statement (parser);
19561 /* Parse a list of Objective-C protocol references.
19563 objc-protocol-refs-opt:
19564 objc-protocol-refs [opt]
19566 objc-protocol-refs:
19567 < objc-identifier-list >
19569 Returns a TREE_LIST of identifiers, if any. */
19572 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
19574 tree protorefs = NULL_TREE;
19576 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
19578 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
19579 protorefs = cp_parser_objc_identifier_list (parser);
19580 cp_parser_require (parser, CPP_GREATER, "%<>%>");
19586 /* Parse a Objective-C visibility specification. */
19589 cp_parser_objc_visibility_spec (cp_parser* parser)
19591 cp_token *vis = cp_lexer_peek_token (parser->lexer);
19593 switch (vis->keyword)
19595 case RID_AT_PRIVATE:
19596 objc_set_visibility (2);
19598 case RID_AT_PROTECTED:
19599 objc_set_visibility (0);
19601 case RID_AT_PUBLIC:
19602 objc_set_visibility (1);
19608 /* Eat '@private'/'@protected'/'@public'. */
19609 cp_lexer_consume_token (parser->lexer);
19612 /* Parse an Objective-C method type. */
19615 cp_parser_objc_method_type (cp_parser* parser)
19617 objc_set_method_type
19618 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
19623 /* Parse an Objective-C protocol qualifier. */
19626 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
19628 tree quals = NULL_TREE, node;
19629 cp_token *token = cp_lexer_peek_token (parser->lexer);
19631 node = token->u.value;
19633 while (node && TREE_CODE (node) == IDENTIFIER_NODE
19634 && (node == ridpointers [(int) RID_IN]
19635 || node == ridpointers [(int) RID_OUT]
19636 || node == ridpointers [(int) RID_INOUT]
19637 || node == ridpointers [(int) RID_BYCOPY]
19638 || node == ridpointers [(int) RID_BYREF]
19639 || node == ridpointers [(int) RID_ONEWAY]))
19641 quals = tree_cons (NULL_TREE, node, quals);
19642 cp_lexer_consume_token (parser->lexer);
19643 token = cp_lexer_peek_token (parser->lexer);
19644 node = token->u.value;
19650 /* Parse an Objective-C typename. */
19653 cp_parser_objc_typename (cp_parser* parser)
19655 tree type_name = NULL_TREE;
19657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19659 tree proto_quals, cp_type = NULL_TREE;
19661 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19662 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
19664 /* An ObjC type name may consist of just protocol qualifiers, in which
19665 case the type shall default to 'id'. */
19666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19667 cp_type = cp_parser_type_id (parser);
19669 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19670 type_name = build_tree_list (proto_quals, cp_type);
19676 /* Check to see if TYPE refers to an Objective-C selector name. */
19679 cp_parser_objc_selector_p (enum cpp_ttype type)
19681 return (type == CPP_NAME || type == CPP_KEYWORD
19682 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
19683 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
19684 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
19685 || type == CPP_XOR || type == CPP_XOR_EQ);
19688 /* Parse an Objective-C selector. */
19691 cp_parser_objc_selector (cp_parser* parser)
19693 cp_token *token = cp_lexer_consume_token (parser->lexer);
19695 if (!cp_parser_objc_selector_p (token->type))
19697 error ("%Hinvalid Objective-C++ selector name", &token->location);
19698 return error_mark_node;
19701 /* C++ operator names are allowed to appear in ObjC selectors. */
19702 switch (token->type)
19704 case CPP_AND_AND: return get_identifier ("and");
19705 case CPP_AND_EQ: return get_identifier ("and_eq");
19706 case CPP_AND: return get_identifier ("bitand");
19707 case CPP_OR: return get_identifier ("bitor");
19708 case CPP_COMPL: return get_identifier ("compl");
19709 case CPP_NOT: return get_identifier ("not");
19710 case CPP_NOT_EQ: return get_identifier ("not_eq");
19711 case CPP_OR_OR: return get_identifier ("or");
19712 case CPP_OR_EQ: return get_identifier ("or_eq");
19713 case CPP_XOR: return get_identifier ("xor");
19714 case CPP_XOR_EQ: return get_identifier ("xor_eq");
19715 default: return token->u.value;
19719 /* Parse an Objective-C params list. */
19722 cp_parser_objc_method_keyword_params (cp_parser* parser)
19724 tree params = NULL_TREE;
19725 bool maybe_unary_selector_p = true;
19726 cp_token *token = cp_lexer_peek_token (parser->lexer);
19728 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
19730 tree selector = NULL_TREE, type_name, identifier;
19732 if (token->type != CPP_COLON)
19733 selector = cp_parser_objc_selector (parser);
19735 /* Detect if we have a unary selector. */
19736 if (maybe_unary_selector_p
19737 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
19740 maybe_unary_selector_p = false;
19741 cp_parser_require (parser, CPP_COLON, "%<:%>");
19742 type_name = cp_parser_objc_typename (parser);
19743 identifier = cp_parser_identifier (parser);
19747 objc_build_keyword_decl (selector,
19751 token = cp_lexer_peek_token (parser->lexer);
19757 /* Parse the non-keyword Objective-C params. */
19760 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
19762 tree params = make_node (TREE_LIST);
19763 cp_token *token = cp_lexer_peek_token (parser->lexer);
19764 *ellipsisp = false; /* Initially, assume no ellipsis. */
19766 while (token->type == CPP_COMMA)
19768 cp_parameter_declarator *parmdecl;
19771 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19772 token = cp_lexer_peek_token (parser->lexer);
19774 if (token->type == CPP_ELLIPSIS)
19776 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
19781 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19782 parm = grokdeclarator (parmdecl->declarator,
19783 &parmdecl->decl_specifiers,
19784 PARM, /*initialized=*/0,
19785 /*attrlist=*/NULL);
19787 chainon (params, build_tree_list (NULL_TREE, parm));
19788 token = cp_lexer_peek_token (parser->lexer);
19794 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
19797 cp_parser_objc_interstitial_code (cp_parser* parser)
19799 cp_token *token = cp_lexer_peek_token (parser->lexer);
19801 /* If the next token is `extern' and the following token is a string
19802 literal, then we have a linkage specification. */
19803 if (token->keyword == RID_EXTERN
19804 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
19805 cp_parser_linkage_specification (parser);
19806 /* Handle #pragma, if any. */
19807 else if (token->type == CPP_PRAGMA)
19808 cp_parser_pragma (parser, pragma_external);
19809 /* Allow stray semicolons. */
19810 else if (token->type == CPP_SEMICOLON)
19811 cp_lexer_consume_token (parser->lexer);
19812 /* Finally, try to parse a block-declaration, or a function-definition. */
19814 cp_parser_block_declaration (parser, /*statement_p=*/false);
19817 /* Parse a method signature. */
19820 cp_parser_objc_method_signature (cp_parser* parser)
19822 tree rettype, kwdparms, optparms;
19823 bool ellipsis = false;
19825 cp_parser_objc_method_type (parser);
19826 rettype = cp_parser_objc_typename (parser);
19827 kwdparms = cp_parser_objc_method_keyword_params (parser);
19828 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
19830 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
19833 /* Pars an Objective-C method prototype list. */
19836 cp_parser_objc_method_prototype_list (cp_parser* parser)
19838 cp_token *token = cp_lexer_peek_token (parser->lexer);
19840 while (token->keyword != RID_AT_END)
19842 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19844 objc_add_method_declaration
19845 (cp_parser_objc_method_signature (parser));
19846 cp_parser_consume_semicolon_at_end_of_statement (parser);
19849 /* Allow for interspersed non-ObjC++ code. */
19850 cp_parser_objc_interstitial_code (parser);
19852 token = cp_lexer_peek_token (parser->lexer);
19855 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19856 objc_finish_interface ();
19859 /* Parse an Objective-C method definition list. */
19862 cp_parser_objc_method_definition_list (cp_parser* parser)
19864 cp_token *token = cp_lexer_peek_token (parser->lexer);
19866 while (token->keyword != RID_AT_END)
19870 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
19872 push_deferring_access_checks (dk_deferred);
19873 objc_start_method_definition
19874 (cp_parser_objc_method_signature (parser));
19876 /* For historical reasons, we accept an optional semicolon. */
19877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19878 cp_lexer_consume_token (parser->lexer);
19880 perform_deferred_access_checks ();
19881 stop_deferring_access_checks ();
19882 meth = cp_parser_function_definition_after_declarator (parser,
19884 pop_deferring_access_checks ();
19885 objc_finish_method_definition (meth);
19888 /* Allow for interspersed non-ObjC++ code. */
19889 cp_parser_objc_interstitial_code (parser);
19891 token = cp_lexer_peek_token (parser->lexer);
19894 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19895 objc_finish_implementation ();
19898 /* Parse Objective-C ivars. */
19901 cp_parser_objc_class_ivars (cp_parser* parser)
19903 cp_token *token = cp_lexer_peek_token (parser->lexer);
19905 if (token->type != CPP_OPEN_BRACE)
19906 return; /* No ivars specified. */
19908 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
19909 token = cp_lexer_peek_token (parser->lexer);
19911 while (token->type != CPP_CLOSE_BRACE)
19913 cp_decl_specifier_seq declspecs;
19914 int decl_class_or_enum_p;
19915 tree prefix_attributes;
19917 cp_parser_objc_visibility_spec (parser);
19919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
19922 cp_parser_decl_specifier_seq (parser,
19923 CP_PARSER_FLAGS_OPTIONAL,
19925 &decl_class_or_enum_p);
19926 prefix_attributes = declspecs.attributes;
19927 declspecs.attributes = NULL_TREE;
19929 /* Keep going until we hit the `;' at the end of the
19931 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19933 tree width = NULL_TREE, attributes, first_attribute, decl;
19934 cp_declarator *declarator = NULL;
19935 int ctor_dtor_or_conv_p;
19937 /* Check for a (possibly unnamed) bitfield declaration. */
19938 token = cp_lexer_peek_token (parser->lexer);
19939 if (token->type == CPP_COLON)
19942 if (token->type == CPP_NAME
19943 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
19946 /* Get the name of the bitfield. */
19947 declarator = make_id_declarator (NULL_TREE,
19948 cp_parser_identifier (parser),
19952 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19953 /* Get the width of the bitfield. */
19955 = cp_parser_constant_expression (parser,
19956 /*allow_non_constant=*/false,
19961 /* Parse the declarator. */
19963 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19964 &ctor_dtor_or_conv_p,
19965 /*parenthesized_p=*/NULL,
19966 /*member_p=*/false);
19969 /* Look for attributes that apply to the ivar. */
19970 attributes = cp_parser_attributes_opt (parser);
19971 /* Remember which attributes are prefix attributes and
19973 first_attribute = attributes;
19974 /* Combine the attributes. */
19975 attributes = chainon (prefix_attributes, attributes);
19978 /* Create the bitfield declaration. */
19979 decl = grokbitfield (declarator, &declspecs,
19983 decl = grokfield (declarator, &declspecs,
19984 NULL_TREE, /*init_const_expr_p=*/false,
19985 NULL_TREE, attributes);
19987 /* Add the instance variable. */
19988 objc_add_instance_variable (decl);
19990 /* Reset PREFIX_ATTRIBUTES. */
19991 while (attributes && TREE_CHAIN (attributes) != first_attribute)
19992 attributes = TREE_CHAIN (attributes);
19994 TREE_CHAIN (attributes) = NULL_TREE;
19996 token = cp_lexer_peek_token (parser->lexer);
19998 if (token->type == CPP_COMMA)
20000 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
20006 cp_parser_consume_semicolon_at_end_of_statement (parser);
20007 token = cp_lexer_peek_token (parser->lexer);
20010 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
20011 /* For historical reasons, we accept an optional semicolon. */
20012 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20013 cp_lexer_consume_token (parser->lexer);
20016 /* Parse an Objective-C protocol declaration. */
20019 cp_parser_objc_protocol_declaration (cp_parser* parser)
20021 tree proto, protorefs;
20024 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
20025 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20027 tok = cp_lexer_peek_token (parser->lexer);
20028 error ("%Hidentifier expected after %<@protocol%>", &tok->location);
20032 /* See if we have a forward declaration or a definition. */
20033 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20035 /* Try a forward declaration first. */
20036 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20038 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20040 cp_parser_consume_semicolon_at_end_of_statement (parser);
20043 /* Ok, we got a full-fledged definition (or at least should). */
20046 proto = cp_parser_identifier (parser);
20047 protorefs = cp_parser_objc_protocol_refs_opt (parser);
20048 objc_start_protocol (proto, protorefs);
20049 cp_parser_objc_method_prototype_list (parser);
20053 /* Parse an Objective-C superclass or category. */
20056 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20059 cp_token *next = cp_lexer_peek_token (parser->lexer);
20061 *super = *categ = NULL_TREE;
20062 if (next->type == CPP_COLON)
20064 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
20065 *super = cp_parser_identifier (parser);
20067 else if (next->type == CPP_OPEN_PAREN)
20069 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
20070 *categ = cp_parser_identifier (parser);
20071 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20075 /* Parse an Objective-C class interface. */
20078 cp_parser_objc_class_interface (cp_parser* parser)
20080 tree name, super, categ, protos;
20082 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
20083 name = cp_parser_identifier (parser);
20084 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20085 protos = cp_parser_objc_protocol_refs_opt (parser);
20087 /* We have either a class or a category on our hands. */
20089 objc_start_category_interface (name, categ, protos);
20092 objc_start_class_interface (name, super, protos);
20093 /* Handle instance variable declarations, if any. */
20094 cp_parser_objc_class_ivars (parser);
20095 objc_continue_interface ();
20098 cp_parser_objc_method_prototype_list (parser);
20101 /* Parse an Objective-C class implementation. */
20104 cp_parser_objc_class_implementation (cp_parser* parser)
20106 tree name, super, categ;
20108 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
20109 name = cp_parser_identifier (parser);
20110 cp_parser_objc_superclass_or_category (parser, &super, &categ);
20112 /* We have either a class or a category on our hands. */
20114 objc_start_category_implementation (name, categ);
20117 objc_start_class_implementation (name, super);
20118 /* Handle instance variable declarations, if any. */
20119 cp_parser_objc_class_ivars (parser);
20120 objc_continue_implementation ();
20123 cp_parser_objc_method_definition_list (parser);
20126 /* Consume the @end token and finish off the implementation. */
20129 cp_parser_objc_end_implementation (cp_parser* parser)
20131 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
20132 objc_finish_implementation ();
20135 /* Parse an Objective-C declaration. */
20138 cp_parser_objc_declaration (cp_parser* parser)
20140 /* Try to figure out what kind of declaration is present. */
20141 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20143 switch (kwd->keyword)
20146 cp_parser_objc_alias_declaration (parser);
20149 cp_parser_objc_class_declaration (parser);
20151 case RID_AT_PROTOCOL:
20152 cp_parser_objc_protocol_declaration (parser);
20154 case RID_AT_INTERFACE:
20155 cp_parser_objc_class_interface (parser);
20157 case RID_AT_IMPLEMENTATION:
20158 cp_parser_objc_class_implementation (parser);
20161 cp_parser_objc_end_implementation (parser);
20164 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20165 &kwd->location, kwd->u.value);
20166 cp_parser_skip_to_end_of_block_or_statement (parser);
20170 /* Parse an Objective-C try-catch-finally statement.
20172 objc-try-catch-finally-stmt:
20173 @try compound-statement objc-catch-clause-seq [opt]
20174 objc-finally-clause [opt]
20176 objc-catch-clause-seq:
20177 objc-catch-clause objc-catch-clause-seq [opt]
20180 @catch ( exception-declaration ) compound-statement
20182 objc-finally-clause
20183 @finally compound-statement
20185 Returns NULL_TREE. */
20188 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20189 location_t location;
20192 cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20193 location = cp_lexer_peek_token (parser->lexer)->location;
20194 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20195 node, lest it get absorbed into the surrounding block. */
20196 stmt = push_stmt_list ();
20197 cp_parser_compound_statement (parser, NULL, false);
20198 objc_begin_try_stmt (location, pop_stmt_list (stmt));
20200 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20202 cp_parameter_declarator *parmdecl;
20205 cp_lexer_consume_token (parser->lexer);
20206 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20207 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20208 parm = grokdeclarator (parmdecl->declarator,
20209 &parmdecl->decl_specifiers,
20210 PARM, /*initialized=*/0,
20211 /*attrlist=*/NULL);
20212 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20213 objc_begin_catch_clause (parm);
20214 cp_parser_compound_statement (parser, NULL, false);
20215 objc_finish_catch_clause ();
20218 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20220 cp_lexer_consume_token (parser->lexer);
20221 location = cp_lexer_peek_token (parser->lexer)->location;
20222 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20223 node, lest it get absorbed into the surrounding block. */
20224 stmt = push_stmt_list ();
20225 cp_parser_compound_statement (parser, NULL, false);
20226 objc_build_finally_clause (location, pop_stmt_list (stmt));
20229 return objc_finish_try_stmt ();
20232 /* Parse an Objective-C synchronized statement.
20234 objc-synchronized-stmt:
20235 @synchronized ( expression ) compound-statement
20237 Returns NULL_TREE. */
20240 cp_parser_objc_synchronized_statement (cp_parser *parser) {
20241 location_t location;
20244 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
20246 location = cp_lexer_peek_token (parser->lexer)->location;
20247 cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20248 lock = cp_parser_expression (parser, false, NULL);
20249 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20251 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
20252 node, lest it get absorbed into the surrounding block. */
20253 stmt = push_stmt_list ();
20254 cp_parser_compound_statement (parser, NULL, false);
20256 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
20259 /* Parse an Objective-C throw statement.
20262 @throw assignment-expression [opt] ;
20264 Returns a constructed '@throw' statement. */
20267 cp_parser_objc_throw_statement (cp_parser *parser) {
20268 tree expr = NULL_TREE;
20269 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20271 cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
20273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20274 expr = cp_parser_assignment_expression (parser, false, NULL);
20276 cp_parser_consume_semicolon_at_end_of_statement (parser);
20278 return objc_build_throw_stmt (loc, expr);
20281 /* Parse an Objective-C statement. */
20284 cp_parser_objc_statement (cp_parser * parser) {
20285 /* Try to figure out what kind of declaration is present. */
20286 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20288 switch (kwd->keyword)
20291 return cp_parser_objc_try_catch_finally_statement (parser);
20292 case RID_AT_SYNCHRONIZED:
20293 return cp_parser_objc_synchronized_statement (parser);
20295 return cp_parser_objc_throw_statement (parser);
20297 error ("%Hmisplaced %<@%D%> Objective-C++ construct",
20298 &kwd->location, kwd->u.value);
20299 cp_parser_skip_to_end_of_block_or_statement (parser);
20302 return error_mark_node;
20305 /* OpenMP 2.5 parsing routines. */
20307 /* Returns name of the next clause.
20308 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
20309 the token is not consumed. Otherwise appropriate pragma_omp_clause is
20310 returned and the token is consumed. */
20312 static pragma_omp_clause
20313 cp_parser_omp_clause_name (cp_parser *parser)
20315 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
20317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
20318 result = PRAGMA_OMP_CLAUSE_IF;
20319 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
20320 result = PRAGMA_OMP_CLAUSE_DEFAULT;
20321 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
20322 result = PRAGMA_OMP_CLAUSE_PRIVATE;
20323 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20325 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20326 const char *p = IDENTIFIER_POINTER (id);
20331 if (!strcmp ("collapse", p))
20332 result = PRAGMA_OMP_CLAUSE_COLLAPSE;
20333 else if (!strcmp ("copyin", p))
20334 result = PRAGMA_OMP_CLAUSE_COPYIN;
20335 else if (!strcmp ("copyprivate", p))
20336 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
20339 if (!strcmp ("firstprivate", p))
20340 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
20343 if (!strcmp ("lastprivate", p))
20344 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
20347 if (!strcmp ("nowait", p))
20348 result = PRAGMA_OMP_CLAUSE_NOWAIT;
20349 else if (!strcmp ("num_threads", p))
20350 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
20353 if (!strcmp ("ordered", p))
20354 result = PRAGMA_OMP_CLAUSE_ORDERED;
20357 if (!strcmp ("reduction", p))
20358 result = PRAGMA_OMP_CLAUSE_REDUCTION;
20361 if (!strcmp ("schedule", p))
20362 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
20363 else if (!strcmp ("shared", p))
20364 result = PRAGMA_OMP_CLAUSE_SHARED;
20367 if (!strcmp ("untied", p))
20368 result = PRAGMA_OMP_CLAUSE_UNTIED;
20373 if (result != PRAGMA_OMP_CLAUSE_NONE)
20374 cp_lexer_consume_token (parser->lexer);
20379 /* Validate that a clause of the given type does not already exist. */
20382 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
20383 const char *name, location_t location)
20387 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
20388 if (OMP_CLAUSE_CODE (c) == code)
20390 error ("%Htoo many %qs clauses", &location, name);
20398 variable-list , identifier
20400 In addition, we match a closing parenthesis. An opening parenthesis
20401 will have been consumed by the caller.
20403 If KIND is nonzero, create the appropriate node and install the decl
20404 in OMP_CLAUSE_DECL and add the node to the head of the list.
20406 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
20407 return the list created. */
20410 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
20418 token = cp_lexer_peek_token (parser->lexer);
20419 name = cp_parser_id_expression (parser, /*template_p=*/false,
20420 /*check_dependency_p=*/true,
20421 /*template_p=*/NULL,
20422 /*declarator_p=*/false,
20423 /*optional_p=*/false);
20424 if (name == error_mark_node)
20427 decl = cp_parser_lookup_name_simple (parser, name, token->location);
20428 if (decl == error_mark_node)
20429 cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
20430 else if (kind != 0)
20432 tree u = build_omp_clause (token->location, kind);
20433 OMP_CLAUSE_DECL (u) = decl;
20434 OMP_CLAUSE_CHAIN (u) = list;
20438 list = tree_cons (decl, NULL_TREE, list);
20441 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20443 cp_lexer_consume_token (parser->lexer);
20446 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20450 /* Try to resync to an unnested comma. Copied from
20451 cp_parser_parenthesized_expression_list. */
20453 ending = cp_parser_skip_to_closing_parenthesis (parser,
20454 /*recovering=*/true,
20456 /*consume_paren=*/true);
20464 /* Similarly, but expect leading and trailing parenthesis. This is a very
20465 common case for omp clauses. */
20468 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
20470 if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20471 return cp_parser_omp_var_list_no_open (parser, kind, list);
20476 collapse ( constant-expression ) */
20479 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
20485 loc = cp_lexer_peek_token (parser->lexer)->location;
20486 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20489 num = cp_parser_constant_expression (parser, false, NULL);
20491 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20492 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20493 /*or_comma=*/false,
20494 /*consume_paren=*/true);
20496 if (num == error_mark_node)
20498 num = fold_non_dependent_expr (num);
20499 if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
20500 || !host_integerp (num, 0)
20501 || (n = tree_low_cst (num, 0)) <= 0
20504 error ("%Hcollapse argument needs positive constant integer expression",
20509 check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
20510 c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
20511 OMP_CLAUSE_CHAIN (c) = list;
20512 OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
20518 default ( shared | none ) */
20521 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
20523 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
20526 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20528 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20530 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20531 const char *p = IDENTIFIER_POINTER (id);
20536 if (strcmp ("none", p) != 0)
20538 kind = OMP_CLAUSE_DEFAULT_NONE;
20542 if (strcmp ("shared", p) != 0)
20544 kind = OMP_CLAUSE_DEFAULT_SHARED;
20551 cp_lexer_consume_token (parser->lexer);
20556 cp_parser_error (parser, "expected %<none%> or %<shared%>");
20559 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20560 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20561 /*or_comma=*/false,
20562 /*consume_paren=*/true);
20564 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
20567 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
20568 c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
20569 OMP_CLAUSE_CHAIN (c) = list;
20570 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
20576 if ( expression ) */
20579 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
20583 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20586 t = cp_parser_condition (parser);
20588 if (t == error_mark_node
20589 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20590 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20591 /*or_comma=*/false,
20592 /*consume_paren=*/true);
20594 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
20596 c = build_omp_clause (location, OMP_CLAUSE_IF);
20597 OMP_CLAUSE_IF_EXPR (c) = t;
20598 OMP_CLAUSE_CHAIN (c) = list;
20607 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
20608 tree list, location_t location)
20612 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
20614 c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
20615 OMP_CLAUSE_CHAIN (c) = list;
20620 num_threads ( expression ) */
20623 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
20624 location_t location)
20628 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20631 t = cp_parser_expression (parser, false, NULL);
20633 if (t == error_mark_node
20634 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20635 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20636 /*or_comma=*/false,
20637 /*consume_paren=*/true);
20639 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
20640 "num_threads", location);
20642 c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
20643 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
20644 OMP_CLAUSE_CHAIN (c) = list;
20653 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
20654 tree list, location_t location)
20658 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
20659 "ordered", location);
20661 c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
20662 OMP_CLAUSE_CHAIN (c) = list;
20667 reduction ( reduction-operator : variable-list )
20669 reduction-operator:
20670 One of: + * - & ^ | && || */
20673 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
20675 enum tree_code code;
20678 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20681 switch (cp_lexer_peek_token (parser->lexer)->type)
20693 code = BIT_AND_EXPR;
20696 code = BIT_XOR_EXPR;
20699 code = BIT_IOR_EXPR;
20702 code = TRUTH_ANDIF_EXPR;
20705 code = TRUTH_ORIF_EXPR;
20708 cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
20709 "%<|%>, %<&&%>, or %<||%>");
20711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20712 /*or_comma=*/false,
20713 /*consume_paren=*/true);
20716 cp_lexer_consume_token (parser->lexer);
20718 if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
20721 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
20722 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
20723 OMP_CLAUSE_REDUCTION_CODE (c) = code;
20729 schedule ( schedule-kind )
20730 schedule ( schedule-kind , expression )
20733 static | dynamic | guided | runtime | auto */
20736 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
20740 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
20743 c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
20745 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20747 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20748 const char *p = IDENTIFIER_POINTER (id);
20753 if (strcmp ("dynamic", p) != 0)
20755 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
20759 if (strcmp ("guided", p) != 0)
20761 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
20765 if (strcmp ("runtime", p) != 0)
20767 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
20774 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
20775 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
20776 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
20777 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
20780 cp_lexer_consume_token (parser->lexer);
20782 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20785 cp_lexer_consume_token (parser->lexer);
20787 token = cp_lexer_peek_token (parser->lexer);
20788 t = cp_parser_assignment_expression (parser, false, NULL);
20790 if (t == error_mark_node)
20792 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
20793 error ("%Hschedule %<runtime%> does not take "
20794 "a %<chunk_size%> parameter", &token->location);
20795 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
20796 error ("%Hschedule %<auto%> does not take "
20797 "a %<chunk_size%> parameter", &token->location);
20799 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
20801 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
20804 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
20807 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
20808 OMP_CLAUSE_CHAIN (c) = list;
20812 cp_parser_error (parser, "invalid schedule kind");
20814 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20815 /*or_comma=*/false,
20816 /*consume_paren=*/true);
20824 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
20825 tree list, location_t location)
20829 check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
20831 c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
20832 OMP_CLAUSE_CHAIN (c) = list;
20836 /* Parse all OpenMP clauses. The set clauses allowed by the directive
20837 is a bitmask in MASK. Return the list of clauses found; the result
20838 of clause default goes in *pdefault. */
20841 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
20842 const char *where, cp_token *pragma_tok)
20844 tree clauses = NULL;
20846 cp_token *token = NULL;
20848 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
20850 pragma_omp_clause c_kind;
20851 const char *c_name;
20852 tree prev = clauses;
20854 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
20855 cp_lexer_consume_token (parser->lexer);
20857 token = cp_lexer_peek_token (parser->lexer);
20858 c_kind = cp_parser_omp_clause_name (parser);
20863 case PRAGMA_OMP_CLAUSE_COLLAPSE:
20864 clauses = cp_parser_omp_clause_collapse (parser, clauses,
20866 c_name = "collapse";
20868 case PRAGMA_OMP_CLAUSE_COPYIN:
20869 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
20872 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
20873 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
20875 c_name = "copyprivate";
20877 case PRAGMA_OMP_CLAUSE_DEFAULT:
20878 clauses = cp_parser_omp_clause_default (parser, clauses,
20880 c_name = "default";
20882 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
20883 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
20885 c_name = "firstprivate";
20887 case PRAGMA_OMP_CLAUSE_IF:
20888 clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
20891 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
20892 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
20894 c_name = "lastprivate";
20896 case PRAGMA_OMP_CLAUSE_NOWAIT:
20897 clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
20900 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
20901 clauses = cp_parser_omp_clause_num_threads (parser, clauses,
20903 c_name = "num_threads";
20905 case PRAGMA_OMP_CLAUSE_ORDERED:
20906 clauses = cp_parser_omp_clause_ordered (parser, clauses,
20908 c_name = "ordered";
20910 case PRAGMA_OMP_CLAUSE_PRIVATE:
20911 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
20913 c_name = "private";
20915 case PRAGMA_OMP_CLAUSE_REDUCTION:
20916 clauses = cp_parser_omp_clause_reduction (parser, clauses);
20917 c_name = "reduction";
20919 case PRAGMA_OMP_CLAUSE_SCHEDULE:
20920 clauses = cp_parser_omp_clause_schedule (parser, clauses,
20922 c_name = "schedule";
20924 case PRAGMA_OMP_CLAUSE_SHARED:
20925 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
20929 case PRAGMA_OMP_CLAUSE_UNTIED:
20930 clauses = cp_parser_omp_clause_untied (parser, clauses,
20935 cp_parser_error (parser, "expected %<#pragma omp%> clause");
20939 if (((mask >> c_kind) & 1) == 0)
20941 /* Remove the invalid clause(s) from the list to avoid
20942 confusing the rest of the compiler. */
20944 error ("%H%qs is not valid for %qs", &token->location, c_name, where);
20948 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20949 return finish_omp_clauses (clauses);
20956 In practice, we're also interested in adding the statement to an
20957 outer node. So it is convenient if we work around the fact that
20958 cp_parser_statement calls add_stmt. */
20961 cp_parser_begin_omp_structured_block (cp_parser *parser)
20963 unsigned save = parser->in_statement;
20965 /* Only move the values to IN_OMP_BLOCK if they weren't false.
20966 This preserves the "not within loop or switch" style error messages
20967 for nonsense cases like
20973 if (parser->in_statement)
20974 parser->in_statement = IN_OMP_BLOCK;
20980 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
20982 parser->in_statement = save;
20986 cp_parser_omp_structured_block (cp_parser *parser)
20988 tree stmt = begin_omp_structured_block ();
20989 unsigned int save = cp_parser_begin_omp_structured_block (parser);
20991 cp_parser_statement (parser, NULL_TREE, false, NULL);
20993 cp_parser_end_omp_structured_block (parser, save);
20994 return finish_omp_structured_block (stmt);
20998 # pragma omp atomic new-line
21002 x binop= expr | x++ | ++x | x-- | --x
21004 +, *, -, /, &, ^, |, <<, >>
21006 where x is an lvalue expression with scalar type. */
21009 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21012 enum tree_code code;
21014 cp_parser_require_pragma_eol (parser, pragma_tok);
21016 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21017 /*cast_p=*/false, NULL);
21018 switch (TREE_CODE (lhs))
21023 case PREINCREMENT_EXPR:
21024 case POSTINCREMENT_EXPR:
21025 lhs = TREE_OPERAND (lhs, 0);
21027 rhs = integer_one_node;
21030 case PREDECREMENT_EXPR:
21031 case POSTDECREMENT_EXPR:
21032 lhs = TREE_OPERAND (lhs, 0);
21034 rhs = integer_one_node;
21038 switch (cp_lexer_peek_token (parser->lexer)->type)
21044 code = TRUNC_DIV_EXPR;
21052 case CPP_LSHIFT_EQ:
21053 code = LSHIFT_EXPR;
21055 case CPP_RSHIFT_EQ:
21056 code = RSHIFT_EXPR;
21059 code = BIT_AND_EXPR;
21062 code = BIT_IOR_EXPR;
21065 code = BIT_XOR_EXPR;
21068 cp_parser_error (parser,
21069 "invalid operator for %<#pragma omp atomic%>");
21072 cp_lexer_consume_token (parser->lexer);
21074 rhs = cp_parser_expression (parser, false, NULL);
21075 if (rhs == error_mark_node)
21079 finish_omp_atomic (code, lhs, rhs);
21080 cp_parser_consume_semicolon_at_end_of_statement (parser);
21084 cp_parser_skip_to_end_of_block_or_statement (parser);
21089 # pragma omp barrier new-line */
21092 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21094 cp_parser_require_pragma_eol (parser, pragma_tok);
21095 finish_omp_barrier ();
21099 # pragma omp critical [(name)] new-line
21100 structured-block */
21103 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21105 tree stmt, name = NULL;
21107 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21109 cp_lexer_consume_token (parser->lexer);
21111 name = cp_parser_identifier (parser);
21113 if (name == error_mark_node
21114 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21116 /*or_comma=*/false,
21117 /*consume_paren=*/true);
21118 if (name == error_mark_node)
21121 cp_parser_require_pragma_eol (parser, pragma_tok);
21123 stmt = cp_parser_omp_structured_block (parser);
21124 return c_finish_omp_critical (input_location, stmt, name);
21128 # pragma omp flush flush-vars[opt] new-line
21131 ( variable-list ) */
21134 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21136 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21137 (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21138 cp_parser_require_pragma_eol (parser, pragma_tok);
21140 finish_omp_flush ();
21143 /* Helper function, to parse omp for increment expression. */
21146 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21148 tree cond = cp_parser_binary_expression (parser, false, true,
21149 PREC_NOT_OPERATOR, NULL);
21152 if (cond == error_mark_node
21153 || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21155 cp_parser_skip_to_end_of_statement (parser);
21156 return error_mark_node;
21159 switch (TREE_CODE (cond))
21167 return error_mark_node;
21170 /* If decl is an iterator, preserve LHS and RHS of the relational
21171 expr until finish_omp_for. */
21173 && (type_dependent_expression_p (decl)
21174 || CLASS_TYPE_P (TREE_TYPE (decl))))
21177 return build_x_binary_op (TREE_CODE (cond),
21178 TREE_OPERAND (cond, 0), ERROR_MARK,
21179 TREE_OPERAND (cond, 1), ERROR_MARK,
21180 &overloaded_p, tf_warning_or_error);
21183 /* Helper function, to parse omp for increment expression. */
21186 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21188 cp_token *token = cp_lexer_peek_token (parser->lexer);
21194 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21196 op = (token->type == CPP_PLUS_PLUS
21197 ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21198 cp_lexer_consume_token (parser->lexer);
21199 lhs = cp_parser_cast_expression (parser, false, false, NULL);
21201 return error_mark_node;
21202 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21205 lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21207 return error_mark_node;
21209 token = cp_lexer_peek_token (parser->lexer);
21210 if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21212 op = (token->type == CPP_PLUS_PLUS
21213 ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21214 cp_lexer_consume_token (parser->lexer);
21215 return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21218 op = cp_parser_assignment_operator_opt (parser);
21219 if (op == ERROR_MARK)
21220 return error_mark_node;
21222 if (op != NOP_EXPR)
21224 rhs = cp_parser_assignment_expression (parser, false, NULL);
21225 rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21226 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21229 lhs = cp_parser_binary_expression (parser, false, false,
21230 PREC_ADDITIVE_EXPRESSION, NULL);
21231 token = cp_lexer_peek_token (parser->lexer);
21232 decl_first = lhs == decl;
21235 if (token->type != CPP_PLUS
21236 && token->type != CPP_MINUS)
21237 return error_mark_node;
21241 op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
21242 cp_lexer_consume_token (parser->lexer);
21243 rhs = cp_parser_binary_expression (parser, false, false,
21244 PREC_ADDITIVE_EXPRESSION, NULL);
21245 token = cp_lexer_peek_token (parser->lexer);
21246 if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
21248 if (lhs == NULL_TREE)
21250 if (op == PLUS_EXPR)
21253 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
21256 lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
21257 NULL, tf_warning_or_error);
21260 while (token->type == CPP_PLUS || token->type == CPP_MINUS);
21264 if (rhs != decl || op == MINUS_EXPR)
21265 return error_mark_node;
21266 rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
21269 rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
21271 return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21274 /* Parse the restricted form of the for statement allowed by OpenMP. */
21277 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
21279 tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
21280 tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
21281 tree this_pre_body, cl;
21282 location_t loc_first;
21283 bool collapse_err = false;
21284 int i, collapse = 1, nbraces = 0;
21286 for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
21287 if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
21288 collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
21290 gcc_assert (collapse >= 1);
21292 declv = make_tree_vec (collapse);
21293 initv = make_tree_vec (collapse);
21294 condv = make_tree_vec (collapse);
21295 incrv = make_tree_vec (collapse);
21297 loc_first = cp_lexer_peek_token (parser->lexer)->location;
21299 for (i = 0; i < collapse; i++)
21301 int bracecount = 0;
21302 bool add_private_clause = false;
21305 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21307 cp_parser_error (parser, "for statement expected");
21310 loc = cp_lexer_consume_token (parser->lexer)->location;
21312 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21315 init = decl = real_decl = NULL;
21316 this_pre_body = push_stmt_list ();
21317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21319 /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
21323 integer-type var = lb
21324 random-access-iterator-type var = lb
21325 pointer-type var = lb
21327 cp_decl_specifier_seq type_specifiers;
21329 /* First, try to parse as an initialized declaration. See
21330 cp_parser_condition, from whence the bulk of this is copied. */
21332 cp_parser_parse_tentatively (parser);
21333 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
21335 if (cp_parser_parse_definitely (parser))
21337 /* If parsing a type specifier seq succeeded, then this
21338 MUST be a initialized declaration. */
21339 tree asm_specification, attributes;
21340 cp_declarator *declarator;
21342 declarator = cp_parser_declarator (parser,
21343 CP_PARSER_DECLARATOR_NAMED,
21344 /*ctor_dtor_or_conv_p=*/NULL,
21345 /*parenthesized_p=*/NULL,
21346 /*member_p=*/false);
21347 attributes = cp_parser_attributes_opt (parser);
21348 asm_specification = cp_parser_asm_specification_opt (parser);
21350 if (declarator == cp_error_declarator)
21351 cp_parser_skip_to_end_of_statement (parser);
21355 tree pushed_scope, auto_node;
21357 decl = start_decl (declarator, &type_specifiers,
21358 SD_INITIALIZED, attributes,
21359 /*prefix_attributes=*/NULL_TREE,
21362 auto_node = type_uses_auto (TREE_TYPE (decl));
21363 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
21365 if (cp_lexer_next_token_is (parser->lexer,
21367 error ("parenthesized initialization is not allowed in "
21368 "OpenMP %<for%> loop");
21370 /* Trigger an error. */
21371 cp_parser_require (parser, CPP_EQ, "%<=%>");
21373 init = error_mark_node;
21374 cp_parser_skip_to_end_of_statement (parser);
21376 else if (CLASS_TYPE_P (TREE_TYPE (decl))
21377 || type_dependent_expression_p (decl)
21380 bool is_direct_init, is_non_constant_init;
21382 init = cp_parser_initializer (parser,
21384 &is_non_constant_init);
21386 if (auto_node && describable_type (init))
21389 = do_auto_deduction (TREE_TYPE (decl), init,
21392 if (!CLASS_TYPE_P (TREE_TYPE (decl))
21393 && !type_dependent_expression_p (decl))
21397 cp_finish_decl (decl, init, !is_non_constant_init,
21399 LOOKUP_ONLYCONVERTING);
21400 if (CLASS_TYPE_P (TREE_TYPE (decl)))
21403 = tree_cons (NULL, this_pre_body, for_block);
21407 init = pop_stmt_list (this_pre_body);
21408 this_pre_body = NULL_TREE;
21413 cp_lexer_consume_token (parser->lexer);
21414 init = cp_parser_assignment_expression (parser, false, NULL);
21417 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
21418 init = error_mark_node;
21420 cp_finish_decl (decl, NULL_TREE,
21421 /*init_const_expr_p=*/false,
21423 LOOKUP_ONLYCONVERTING);
21427 pop_scope (pushed_scope);
21433 /* If parsing a type specifier sequence failed, then
21434 this MUST be a simple expression. */
21435 cp_parser_parse_tentatively (parser);
21436 decl = cp_parser_primary_expression (parser, false, false,
21438 if (!cp_parser_error_occurred (parser)
21441 && CLASS_TYPE_P (TREE_TYPE (decl)))
21445 cp_parser_parse_definitely (parser);
21446 cp_parser_require (parser, CPP_EQ, "%<=%>");
21447 rhs = cp_parser_assignment_expression (parser, false, NULL);
21448 finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
21450 tf_warning_or_error));
21451 add_private_clause = true;
21456 cp_parser_abort_tentative_parse (parser);
21457 init = cp_parser_expression (parser, false, NULL);
21460 if (TREE_CODE (init) == MODIFY_EXPR
21461 || TREE_CODE (init) == MODOP_EXPR)
21462 real_decl = TREE_OPERAND (init, 0);
21467 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21470 this_pre_body = pop_stmt_list (this_pre_body);
21474 pre_body = push_stmt_list ();
21476 add_stmt (this_pre_body);
21477 pre_body = pop_stmt_list (pre_body);
21480 pre_body = this_pre_body;
21485 if (par_clauses != NULL && real_decl != NULL_TREE)
21488 for (c = par_clauses; *c ; )
21489 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
21490 && OMP_CLAUSE_DECL (*c) == real_decl)
21492 error ("%Hiteration variable %qD should not be firstprivate",
21494 *c = OMP_CLAUSE_CHAIN (*c);
21496 else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
21497 && OMP_CLAUSE_DECL (*c) == real_decl)
21499 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
21500 change it to shared (decl) in OMP_PARALLEL_CLAUSES. */
21501 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
21502 OMP_CLAUSE_DECL (l) = real_decl;
21503 OMP_CLAUSE_CHAIN (l) = clauses;
21504 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
21506 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
21507 CP_OMP_CLAUSE_INFO (*c) = NULL;
21508 add_private_clause = false;
21512 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
21513 && OMP_CLAUSE_DECL (*c) == real_decl)
21514 add_private_clause = false;
21515 c = &OMP_CLAUSE_CHAIN (*c);
21519 if (add_private_clause)
21522 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21524 if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
21525 || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
21526 && OMP_CLAUSE_DECL (c) == decl)
21528 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
21529 && OMP_CLAUSE_DECL (c) == decl)
21530 error ("%Hiteration variable %qD should not be firstprivate",
21532 else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
21533 && OMP_CLAUSE_DECL (c) == decl)
21534 error ("%Hiteration variable %qD should not be reduction",
21539 c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
21540 OMP_CLAUSE_DECL (c) = decl;
21541 c = finish_omp_clauses (c);
21544 OMP_CLAUSE_CHAIN (c) = clauses;
21551 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21552 cond = cp_parser_omp_for_cond (parser, decl);
21553 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
21556 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21558 /* If decl is an iterator, preserve the operator on decl
21559 until finish_omp_for. */
21561 && (type_dependent_expression_p (decl)
21562 || CLASS_TYPE_P (TREE_TYPE (decl))))
21563 incr = cp_parser_omp_for_incr (parser, decl);
21565 incr = cp_parser_expression (parser, false, NULL);
21568 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21569 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21570 /*or_comma=*/false,
21571 /*consume_paren=*/true);
21573 TREE_VEC_ELT (declv, i) = decl;
21574 TREE_VEC_ELT (initv, i) = init;
21575 TREE_VEC_ELT (condv, i) = cond;
21576 TREE_VEC_ELT (incrv, i) = incr;
21578 if (i == collapse - 1)
21581 /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
21582 in between the collapsed for loops to be still considered perfectly
21583 nested. Hopefully the final version clarifies this.
21584 For now handle (multiple) {'s and empty statements. */
21585 cp_parser_parse_tentatively (parser);
21588 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21590 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21592 cp_lexer_consume_token (parser->lexer);
21595 else if (bracecount
21596 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21597 cp_lexer_consume_token (parser->lexer);
21600 loc = cp_lexer_peek_token (parser->lexer)->location;
21601 error ("%Hnot enough collapsed for loops", &loc);
21602 collapse_err = true;
21603 cp_parser_abort_tentative_parse (parser);
21612 cp_parser_parse_definitely (parser);
21613 nbraces += bracecount;
21617 /* Note that we saved the original contents of this flag when we entered
21618 the structured block, and so we don't need to re-save it here. */
21619 parser->in_statement = IN_OMP_FOR;
21621 /* Note that the grammar doesn't call for a structured block here,
21622 though the loop as a whole is a structured block. */
21623 body = push_stmt_list ();
21624 cp_parser_statement (parser, NULL_TREE, false, NULL);
21625 body = pop_stmt_list (body);
21627 if (declv == NULL_TREE)
21630 ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
21631 pre_body, clauses);
21635 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
21637 cp_lexer_consume_token (parser->lexer);
21640 else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
21641 cp_lexer_consume_token (parser->lexer);
21646 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21647 error ("%Hcollapsed loops not perfectly nested", &loc);
21649 collapse_err = true;
21650 cp_parser_statement_seq_opt (parser, NULL);
21651 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21657 add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
21658 for_block = TREE_CHAIN (for_block);
21665 #pragma omp for for-clause[optseq] new-line
21668 #define OMP_FOR_CLAUSE_MASK \
21669 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21670 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21671 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21672 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21673 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
21674 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
21675 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT) \
21676 | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
21679 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
21681 tree clauses, sb, ret;
21684 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
21685 "#pragma omp for", pragma_tok);
21687 sb = begin_omp_structured_block ();
21688 save = cp_parser_begin_omp_structured_block (parser);
21690 ret = cp_parser_omp_for_loop (parser, clauses, NULL);
21692 cp_parser_end_omp_structured_block (parser, save);
21693 add_stmt (finish_omp_structured_block (sb));
21699 # pragma omp master new-line
21700 structured-block */
21703 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
21705 cp_parser_require_pragma_eol (parser, pragma_tok);
21706 return c_finish_omp_master (input_location,
21707 cp_parser_omp_structured_block (parser));
21711 # pragma omp ordered new-line
21712 structured-block */
21715 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
21717 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21718 cp_parser_require_pragma_eol (parser, pragma_tok);
21719 return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
21725 { section-sequence }
21728 section-directive[opt] structured-block
21729 section-sequence section-directive structured-block */
21732 cp_parser_omp_sections_scope (cp_parser *parser)
21734 tree stmt, substmt;
21735 bool error_suppress = false;
21738 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
21741 stmt = push_stmt_list ();
21743 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
21747 substmt = begin_omp_structured_block ();
21748 save = cp_parser_begin_omp_structured_block (parser);
21752 cp_parser_statement (parser, NULL_TREE, false, NULL);
21754 tok = cp_lexer_peek_token (parser->lexer);
21755 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21757 if (tok->type == CPP_CLOSE_BRACE)
21759 if (tok->type == CPP_EOF)
21763 cp_parser_end_omp_structured_block (parser, save);
21764 substmt = finish_omp_structured_block (substmt);
21765 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21766 add_stmt (substmt);
21771 tok = cp_lexer_peek_token (parser->lexer);
21772 if (tok->type == CPP_CLOSE_BRACE)
21774 if (tok->type == CPP_EOF)
21777 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
21779 cp_lexer_consume_token (parser->lexer);
21780 cp_parser_require_pragma_eol (parser, tok);
21781 error_suppress = false;
21783 else if (!error_suppress)
21785 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
21786 error_suppress = true;
21789 substmt = cp_parser_omp_structured_block (parser);
21790 substmt = build1 (OMP_SECTION, void_type_node, substmt);
21791 add_stmt (substmt);
21793 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
21795 substmt = pop_stmt_list (stmt);
21797 stmt = make_node (OMP_SECTIONS);
21798 TREE_TYPE (stmt) = void_type_node;
21799 OMP_SECTIONS_BODY (stmt) = substmt;
21806 # pragma omp sections sections-clause[optseq] newline
21809 #define OMP_SECTIONS_CLAUSE_MASK \
21810 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21811 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21812 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
21813 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21814 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21817 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
21821 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
21822 "#pragma omp sections", pragma_tok);
21824 ret = cp_parser_omp_sections_scope (parser);
21826 OMP_SECTIONS_CLAUSES (ret) = clauses;
21832 # pragma parallel parallel-clause new-line
21833 # pragma parallel for parallel-for-clause new-line
21834 # pragma parallel sections parallel-sections-clause new-line */
21836 #define OMP_PARALLEL_CLAUSE_MASK \
21837 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21838 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21839 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21840 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21841 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
21842 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
21843 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
21844 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
21847 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
21849 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
21850 const char *p_name = "#pragma omp parallel";
21851 tree stmt, clauses, par_clause, ws_clause, block;
21852 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
21854 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21856 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
21858 cp_lexer_consume_token (parser->lexer);
21859 p_kind = PRAGMA_OMP_PARALLEL_FOR;
21860 p_name = "#pragma omp parallel for";
21861 mask |= OMP_FOR_CLAUSE_MASK;
21862 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21864 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21866 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21867 const char *p = IDENTIFIER_POINTER (id);
21868 if (strcmp (p, "sections") == 0)
21870 cp_lexer_consume_token (parser->lexer);
21871 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
21872 p_name = "#pragma omp parallel sections";
21873 mask |= OMP_SECTIONS_CLAUSE_MASK;
21874 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
21878 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
21879 block = begin_omp_parallel ();
21880 save = cp_parser_begin_omp_structured_block (parser);
21884 case PRAGMA_OMP_PARALLEL:
21885 cp_parser_statement (parser, NULL_TREE, false, NULL);
21886 par_clause = clauses;
21889 case PRAGMA_OMP_PARALLEL_FOR:
21890 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21891 cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
21894 case PRAGMA_OMP_PARALLEL_SECTIONS:
21895 c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
21896 stmt = cp_parser_omp_sections_scope (parser);
21898 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
21902 gcc_unreachable ();
21905 cp_parser_end_omp_structured_block (parser, save);
21906 stmt = finish_omp_parallel (par_clause, block);
21907 if (p_kind != PRAGMA_OMP_PARALLEL)
21908 OMP_PARALLEL_COMBINED (stmt) = 1;
21913 # pragma omp single single-clause[optseq] new-line
21914 structured-block */
21916 #define OMP_SINGLE_CLAUSE_MASK \
21917 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21918 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21919 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
21920 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
21923 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
21925 tree stmt = make_node (OMP_SINGLE);
21926 TREE_TYPE (stmt) = void_type_node;
21928 OMP_SINGLE_CLAUSES (stmt)
21929 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
21930 "#pragma omp single", pragma_tok);
21931 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
21933 return add_stmt (stmt);
21937 # pragma omp task task-clause[optseq] new-line
21938 structured-block */
21940 #define OMP_TASK_CLAUSE_MASK \
21941 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
21942 | (1u << PRAGMA_OMP_CLAUSE_UNTIED) \
21943 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
21944 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
21945 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
21946 | (1u << PRAGMA_OMP_CLAUSE_SHARED))
21949 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
21951 tree clauses, block;
21954 clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
21955 "#pragma omp task", pragma_tok);
21956 block = begin_omp_task ();
21957 save = cp_parser_begin_omp_structured_block (parser);
21958 cp_parser_statement (parser, NULL_TREE, false, NULL);
21959 cp_parser_end_omp_structured_block (parser, save);
21960 return finish_omp_task (clauses, block);
21964 # pragma omp taskwait new-line */
21967 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
21969 cp_parser_require_pragma_eol (parser, pragma_tok);
21970 finish_omp_taskwait ();
21974 # pragma omp threadprivate (variable-list) */
21977 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
21981 vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21982 cp_parser_require_pragma_eol (parser, pragma_tok);
21984 finish_omp_threadprivate (vars);
21987 /* Main entry point to OpenMP statement pragmas. */
21990 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
21994 switch (pragma_tok->pragma_kind)
21996 case PRAGMA_OMP_ATOMIC:
21997 cp_parser_omp_atomic (parser, pragma_tok);
21999 case PRAGMA_OMP_CRITICAL:
22000 stmt = cp_parser_omp_critical (parser, pragma_tok);
22002 case PRAGMA_OMP_FOR:
22003 stmt = cp_parser_omp_for (parser, pragma_tok);
22005 case PRAGMA_OMP_MASTER:
22006 stmt = cp_parser_omp_master (parser, pragma_tok);
22008 case PRAGMA_OMP_ORDERED:
22009 stmt = cp_parser_omp_ordered (parser, pragma_tok);
22011 case PRAGMA_OMP_PARALLEL:
22012 stmt = cp_parser_omp_parallel (parser, pragma_tok);
22014 case PRAGMA_OMP_SECTIONS:
22015 stmt = cp_parser_omp_sections (parser, pragma_tok);
22017 case PRAGMA_OMP_SINGLE:
22018 stmt = cp_parser_omp_single (parser, pragma_tok);
22020 case PRAGMA_OMP_TASK:
22021 stmt = cp_parser_omp_task (parser, pragma_tok);
22024 gcc_unreachable ();
22028 SET_EXPR_LOCATION (stmt, pragma_tok->location);
22033 static GTY (()) cp_parser *the_parser;
22036 /* Special handling for the first token or line in the file. The first
22037 thing in the file might be #pragma GCC pch_preprocess, which loads a
22038 PCH file, which is a GC collection point. So we need to handle this
22039 first pragma without benefit of an existing lexer structure.
22041 Always returns one token to the caller in *FIRST_TOKEN. This is
22042 either the true first token of the file, or the first token after
22043 the initial pragma. */
22046 cp_parser_initial_pragma (cp_token *first_token)
22050 cp_lexer_get_preprocessor_token (NULL, first_token);
22051 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22054 cp_lexer_get_preprocessor_token (NULL, first_token);
22055 if (first_token->type == CPP_STRING)
22057 name = first_token->u.value;
22059 cp_lexer_get_preprocessor_token (NULL, first_token);
22060 if (first_token->type != CPP_PRAGMA_EOL)
22061 error ("%Hjunk at end of %<#pragma GCC pch_preprocess%>",
22062 &first_token->location);
22065 error ("%Hexpected string literal", &first_token->location);
22067 /* Skip to the end of the pragma. */
22068 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22069 cp_lexer_get_preprocessor_token (NULL, first_token);
22071 /* Now actually load the PCH file. */
22073 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22075 /* Read one more token to return to our caller. We have to do this
22076 after reading the PCH file in, since its pointers have to be
22078 cp_lexer_get_preprocessor_token (NULL, first_token);
22081 /* Normal parsing of a pragma token. Here we can (and must) use the
22085 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22087 cp_token *pragma_tok;
22090 pragma_tok = cp_lexer_consume_token (parser->lexer);
22091 gcc_assert (pragma_tok->type == CPP_PRAGMA);
22092 parser->lexer->in_pragma = true;
22094 id = pragma_tok->pragma_kind;
22097 case PRAGMA_GCC_PCH_PREPROCESS:
22098 error ("%H%<#pragma GCC pch_preprocess%> must be first",
22099 &pragma_tok->location);
22102 case PRAGMA_OMP_BARRIER:
22105 case pragma_compound:
22106 cp_parser_omp_barrier (parser, pragma_tok);
22109 error ("%H%<#pragma omp barrier%> may only be "
22110 "used in compound statements", &pragma_tok->location);
22117 case PRAGMA_OMP_FLUSH:
22120 case pragma_compound:
22121 cp_parser_omp_flush (parser, pragma_tok);
22124 error ("%H%<#pragma omp flush%> may only be "
22125 "used in compound statements", &pragma_tok->location);
22132 case PRAGMA_OMP_TASKWAIT:
22135 case pragma_compound:
22136 cp_parser_omp_taskwait (parser, pragma_tok);
22139 error ("%H%<#pragma omp taskwait%> may only be "
22140 "used in compound statements",
22141 &pragma_tok->location);
22148 case PRAGMA_OMP_THREADPRIVATE:
22149 cp_parser_omp_threadprivate (parser, pragma_tok);
22152 case PRAGMA_OMP_ATOMIC:
22153 case PRAGMA_OMP_CRITICAL:
22154 case PRAGMA_OMP_FOR:
22155 case PRAGMA_OMP_MASTER:
22156 case PRAGMA_OMP_ORDERED:
22157 case PRAGMA_OMP_PARALLEL:
22158 case PRAGMA_OMP_SECTIONS:
22159 case PRAGMA_OMP_SINGLE:
22160 case PRAGMA_OMP_TASK:
22161 if (context == pragma_external)
22163 cp_parser_omp_construct (parser, pragma_tok);
22166 case PRAGMA_OMP_SECTION:
22167 error ("%H%<#pragma omp section%> may only be used in "
22168 "%<#pragma omp sections%> construct", &pragma_tok->location);
22172 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22173 c_invoke_pragma_handler (id);
22177 cp_parser_error (parser, "expected declaration specifiers");
22181 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22185 /* The interface the pragma parsers have to the lexer. */
22188 pragma_lex (tree *value)
22191 enum cpp_ttype ret;
22193 tok = cp_lexer_peek_token (the_parser->lexer);
22196 *value = tok->u.value;
22198 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22200 else if (ret == CPP_STRING)
22201 *value = cp_parser_string_literal (the_parser, false, false);
22204 cp_lexer_consume_token (the_parser->lexer);
22205 if (ret == CPP_KEYWORD)
22213 /* External interface. */
22215 /* Parse one entire translation unit. */
22218 c_parse_file (void)
22220 bool error_occurred;
22221 static bool already_called = false;
22223 if (already_called)
22225 sorry ("inter-module optimizations not implemented for C++");
22228 already_called = true;
22230 the_parser = cp_parser_new ();
22231 push_deferring_access_checks (flag_access_control
22232 ? dk_no_deferred : dk_no_check);
22233 error_occurred = cp_parser_translation_unit (the_parser);
22237 #include "gt-cp-parser.h"