2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007, 2008 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"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
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 cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The input file stack index at which this token was found. */
83 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84 /* The value associated with this token, if any. */
85 union cp_token_value {
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check* GTY((tag ("1"))) tree_check_value;
88 /* Use for all other tokens. */
89 tree GTY((tag ("0"))) value;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* The location at which this token was found. */
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
100 static cp_token eof_token =
102 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
106 /* The cp_lexer structure represents the C++ lexer. It is responsible
107 for managing the token stream from the preprocessor and supplying
108 it to the parser. Tokens are never added to the cp_lexer after
111 typedef struct cp_lexer GTY (())
113 /* The memory allocated for the buffer. NULL if this lexer does not
114 own the token buffer. */
115 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
116 /* If the lexer owns the buffer, this is the number of tokens in the
118 size_t buffer_length;
120 /* A pointer just past the last available token. The tokens
121 in this lexer are [buffer, last_token). */
122 cp_token_position GTY ((skip)) last_token;
124 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
125 no more available tokens. */
126 cp_token_position GTY ((skip)) next_token;
128 /* A stack indicating positions at which cp_lexer_save_tokens was
129 called. The top entry is the most recent position at which we
130 began saving tokens. If the stack is non-empty, we are saving
132 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
134 /* The next lexer in a linked list of lexers. */
135 struct cp_lexer *next;
137 /* True if we should output debugging information. */
140 /* True if we're in the context of parsing a pragma, and should not
141 increment past the end-of-line marker. */
145 /* cp_token_cache is a range of tokens. There is no need to represent
146 allocate heap memory for it, since tokens are never removed from the
147 lexer's array. There is also no need for the GC to walk through
148 a cp_token_cache, since everything in here is referenced through
151 typedef struct cp_token_cache GTY(())
153 /* The beginning of the token range. */
154 cp_token * GTY((skip)) first;
156 /* Points immediately after the last token in the range. */
157 cp_token * GTY ((skip)) last;
162 static cp_lexer *cp_lexer_new_main
164 static cp_lexer *cp_lexer_new_from_tokens
165 (cp_token_cache *tokens);
166 static void cp_lexer_destroy
168 static int cp_lexer_saving_tokens
170 static cp_token_position cp_lexer_token_position
172 static cp_token *cp_lexer_token_at
173 (cp_lexer *, cp_token_position);
174 static void cp_lexer_get_preprocessor_token
175 (cp_lexer *, cp_token *);
176 static inline cp_token *cp_lexer_peek_token
178 static cp_token *cp_lexer_peek_nth_token
179 (cp_lexer *, size_t);
180 static inline bool cp_lexer_next_token_is
181 (cp_lexer *, enum cpp_ttype);
182 static bool cp_lexer_next_token_is_not
183 (cp_lexer *, enum cpp_ttype);
184 static bool cp_lexer_next_token_is_keyword
185 (cp_lexer *, enum rid);
186 static cp_token *cp_lexer_consume_token
188 static void cp_lexer_purge_token
190 static void cp_lexer_purge_tokens_after
191 (cp_lexer *, cp_token_position);
192 static void cp_lexer_save_tokens
194 static void cp_lexer_commit_tokens
196 static void cp_lexer_rollback_tokens
198 #ifdef ENABLE_CHECKING
199 static void cp_lexer_print_token
200 (FILE *, cp_token *);
201 static inline bool cp_lexer_debugging_p
203 static void cp_lexer_start_debugging
204 (cp_lexer *) ATTRIBUTE_UNUSED;
205 static void cp_lexer_stop_debugging
206 (cp_lexer *) ATTRIBUTE_UNUSED;
208 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
209 about passing NULL to functions that require non-NULL arguments
210 (fputs, fprintf). It will never be used, so all we need is a value
211 of the right type that's guaranteed not to be NULL. */
212 #define cp_lexer_debug_stream stdout
213 #define cp_lexer_print_token(str, tok) (void) 0
214 #define cp_lexer_debugging_p(lexer) 0
215 #endif /* ENABLE_CHECKING */
217 static cp_token_cache *cp_token_cache_new
218 (cp_token *, cp_token *);
220 static void cp_parser_initial_pragma
223 /* Manifest constants. */
224 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
225 #define CP_SAVED_TOKEN_STACK 5
227 /* A token type for keywords, as opposed to ordinary identifiers. */
228 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
230 /* A token type for template-ids. If a template-id is processed while
231 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
232 the value of the CPP_TEMPLATE_ID is whatever was returned by
233 cp_parser_template_id. */
234 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
236 /* A token type for nested-name-specifiers. If a
237 nested-name-specifier is processed while parsing tentatively, it is
238 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
239 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
240 cp_parser_nested_name_specifier_opt. */
241 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
243 /* A token type for tokens that are not tokens at all; these are used
244 to represent slots in the array where there used to be a token
245 that has now been deleted. */
246 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
248 /* The number of token types, including C++-specific ones. */
249 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
253 #ifdef ENABLE_CHECKING
254 /* The stream to which debugging output should be written. */
255 static FILE *cp_lexer_debug_stream;
256 #endif /* ENABLE_CHECKING */
258 /* Create a new main C++ lexer, the lexer that gets tokens from the
262 cp_lexer_new_main (void)
264 cp_token first_token;
271 /* It's possible that parsing the first pragma will load a PCH file,
272 which is a GC collection point. So we have to do that before
273 allocating any memory. */
274 cp_parser_initial_pragma (&first_token);
276 c_common_no_more_pch ();
278 /* Allocate the memory. */
279 lexer = GGC_CNEW (cp_lexer);
281 #ifdef ENABLE_CHECKING
282 /* Initially we are not debugging. */
283 lexer->debugging_p = false;
284 #endif /* ENABLE_CHECKING */
285 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
286 CP_SAVED_TOKEN_STACK);
288 /* Create the buffer. */
289 alloc = CP_LEXER_BUFFER_SIZE;
290 buffer = GGC_NEWVEC (cp_token, alloc);
292 /* Put the first token in the buffer. */
297 /* Get the remaining tokens from the preprocessor. */
298 while (pos->type != CPP_EOF)
305 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
306 pos = buffer + space;
308 cp_lexer_get_preprocessor_token (lexer, pos);
310 lexer->buffer = buffer;
311 lexer->buffer_length = alloc - space;
312 lexer->last_token = pos;
313 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
315 /* Subsequent preprocessor diagnostics should use compiler
316 diagnostic functions to get the compiler source location. */
317 cpp_get_options (parse_in)->client_diagnostic = true;
318 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
320 gcc_assert (lexer->next_token->type != CPP_PURGED);
324 /* Create a new lexer whose token stream is primed with the tokens in
325 CACHE. When these tokens are exhausted, no new tokens will be read. */
328 cp_lexer_new_from_tokens (cp_token_cache *cache)
330 cp_token *first = cache->first;
331 cp_token *last = cache->last;
332 cp_lexer *lexer = GGC_CNEW (cp_lexer);
334 /* We do not own the buffer. */
335 lexer->buffer = NULL;
336 lexer->buffer_length = 0;
337 lexer->next_token = first == last ? &eof_token : first;
338 lexer->last_token = last;
340 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
341 CP_SAVED_TOKEN_STACK);
343 #ifdef ENABLE_CHECKING
344 /* Initially we are not debugging. */
345 lexer->debugging_p = false;
348 gcc_assert (lexer->next_token->type != CPP_PURGED);
352 /* Frees all resources associated with LEXER. */
355 cp_lexer_destroy (cp_lexer *lexer)
358 ggc_free (lexer->buffer);
359 VEC_free (cp_token_position, heap, lexer->saved_tokens);
363 /* Returns nonzero if debugging information should be output. */
365 #ifdef ENABLE_CHECKING
368 cp_lexer_debugging_p (cp_lexer *lexer)
370 return lexer->debugging_p;
373 #endif /* ENABLE_CHECKING */
375 static inline cp_token_position
376 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
378 gcc_assert (!previous_p || lexer->next_token != &eof_token);
380 return lexer->next_token - previous_p;
383 static inline cp_token *
384 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
389 /* nonzero if we are presently saving tokens. */
392 cp_lexer_saving_tokens (const cp_lexer* lexer)
394 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
397 /* Store the next token from the preprocessor in *TOKEN. Return true
398 if we reach EOF. If LEXER is NULL, assume we are handling an
399 initial #pragma pch_preprocess, and thus want the lexer to return
400 processed strings. */
403 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
405 static int is_extern_c = 0;
407 /* Get a new token from the preprocessor. */
409 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
410 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
411 token->input_file_stack_index = input_file_stack_tick;
412 token->keyword = RID_MAX;
413 token->pragma_kind = PRAGMA_NONE;
414 token->in_system_header = in_system_header;
416 /* On some systems, some header files are surrounded by an
417 implicit extern "C" block. Set a flag in the token if it
418 comes from such a header. */
419 is_extern_c += pending_lang_change;
420 pending_lang_change = 0;
421 token->implicit_extern_c = is_extern_c > 0;
423 /* Check to see if this token is a keyword. */
424 if (token->type == CPP_NAME)
426 if (C_IS_RESERVED_WORD (token->u.value))
428 /* Mark this token as a keyword. */
429 token->type = CPP_KEYWORD;
430 /* Record which keyword. */
431 token->keyword = C_RID_CODE (token->u.value);
432 /* Update the value. Some keywords are mapped to particular
433 entities, rather than simply having the value of the
434 corresponding IDENTIFIER_NODE. For example, `__const' is
435 mapped to `const'. */
436 token->u.value = ridpointers[token->keyword];
440 if (warn_cxx0x_compat
441 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
442 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
444 /* Warn about the C++0x keyword (but still treat it as
446 warning (OPT_Wc__0x_compat,
447 "identifier %<%s%> will become a keyword in C++0x",
448 IDENTIFIER_POINTER (token->u.value));
450 /* Clear out the C_RID_CODE so we don't warn about this
451 particular identifier-turned-keyword again. */
452 C_RID_CODE (token->u.value) = RID_MAX;
455 token->ambiguous_p = false;
456 token->keyword = RID_MAX;
459 /* Handle Objective-C++ keywords. */
460 else if (token->type == CPP_AT_NAME)
462 token->type = CPP_KEYWORD;
463 switch (C_RID_CODE (token->u.value))
465 /* Map 'class' to '@class', 'private' to '@private', etc. */
466 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
467 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
468 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
469 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
470 case RID_THROW: token->keyword = RID_AT_THROW; break;
471 case RID_TRY: token->keyword = RID_AT_TRY; break;
472 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
473 default: token->keyword = C_RID_CODE (token->u.value);
476 else if (token->type == CPP_PRAGMA)
478 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
479 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
480 token->u.value = NULL_TREE;
484 /* Update the globals input_location and in_system_header and the
485 input file stack from TOKEN. */
487 cp_lexer_set_source_position_from_token (cp_token *token)
489 if (token->type != CPP_EOF)
491 input_location = token->location;
492 in_system_header = token->in_system_header;
493 restore_input_file_stack (token->input_file_stack_index);
497 /* Return a pointer to the next token in the token stream, but do not
500 static inline cp_token *
501 cp_lexer_peek_token (cp_lexer *lexer)
503 if (cp_lexer_debugging_p (lexer))
505 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
506 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
507 putc ('\n', cp_lexer_debug_stream);
509 return lexer->next_token;
512 /* Return true if the next token has the indicated TYPE. */
515 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
517 return cp_lexer_peek_token (lexer)->type == type;
520 /* Return true if the next token does not have the indicated TYPE. */
523 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
525 return !cp_lexer_next_token_is (lexer, type);
528 /* Return true if the next token is the indicated KEYWORD. */
531 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
533 return cp_lexer_peek_token (lexer)->keyword == keyword;
536 /* Return true if the next token is a keyword for a decl-specifier. */
539 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
543 token = cp_lexer_peek_token (lexer);
544 switch (token->keyword)
546 /* Storage classes. */
553 /* Elaborated type specifiers. */
559 /* Simple type specifiers. */
571 /* GNU extensions. */
574 /* C++0x extensions. */
583 /* Return a pointer to the Nth token in the token stream. If N is 1,
584 then this is precisely equivalent to cp_lexer_peek_token (except
585 that it is not inline). One would like to disallow that case, but
586 there is one case (cp_parser_nth_token_starts_template_id) where
587 the caller passes a variable for N and it might be 1. */
590 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
594 /* N is 1-based, not zero-based. */
597 if (cp_lexer_debugging_p (lexer))
598 fprintf (cp_lexer_debug_stream,
599 "cp_lexer: peeking ahead %ld at token: ", (long)n);
602 token = lexer->next_token;
603 gcc_assert (!n || token != &eof_token);
607 if (token == lexer->last_token)
613 if (token->type != CPP_PURGED)
617 if (cp_lexer_debugging_p (lexer))
619 cp_lexer_print_token (cp_lexer_debug_stream, token);
620 putc ('\n', cp_lexer_debug_stream);
626 /* Return the next token, and advance the lexer's next_token pointer
627 to point to the next non-purged token. */
630 cp_lexer_consume_token (cp_lexer* lexer)
632 cp_token *token = lexer->next_token;
634 gcc_assert (token != &eof_token);
635 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
640 if (lexer->next_token == lexer->last_token)
642 lexer->next_token = &eof_token;
647 while (lexer->next_token->type == CPP_PURGED);
649 cp_lexer_set_source_position_from_token (token);
651 /* Provide debugging output. */
652 if (cp_lexer_debugging_p (lexer))
654 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
655 cp_lexer_print_token (cp_lexer_debug_stream, token);
656 putc ('\n', cp_lexer_debug_stream);
662 /* Permanently remove the next token from the token stream, and
663 advance the next_token pointer to refer to the next non-purged
667 cp_lexer_purge_token (cp_lexer *lexer)
669 cp_token *tok = lexer->next_token;
671 gcc_assert (tok != &eof_token);
672 tok->type = CPP_PURGED;
673 tok->location = UNKNOWN_LOCATION;
674 tok->u.value = NULL_TREE;
675 tok->keyword = RID_MAX;
680 if (tok == lexer->last_token)
686 while (tok->type == CPP_PURGED);
687 lexer->next_token = tok;
690 /* Permanently remove all tokens after TOK, up to, but not
691 including, the token that will be returned next by
692 cp_lexer_peek_token. */
695 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
697 cp_token *peek = lexer->next_token;
699 if (peek == &eof_token)
700 peek = lexer->last_token;
702 gcc_assert (tok < peek);
704 for ( tok += 1; tok != peek; tok += 1)
706 tok->type = CPP_PURGED;
707 tok->location = UNKNOWN_LOCATION;
708 tok->u.value = NULL_TREE;
709 tok->keyword = RID_MAX;
713 /* Begin saving tokens. All tokens consumed after this point will be
717 cp_lexer_save_tokens (cp_lexer* lexer)
719 /* Provide debugging output. */
720 if (cp_lexer_debugging_p (lexer))
721 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
723 VEC_safe_push (cp_token_position, heap,
724 lexer->saved_tokens, lexer->next_token);
727 /* Commit to the portion of the token stream most recently saved. */
730 cp_lexer_commit_tokens (cp_lexer* lexer)
732 /* Provide debugging output. */
733 if (cp_lexer_debugging_p (lexer))
734 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
736 VEC_pop (cp_token_position, lexer->saved_tokens);
739 /* Return all tokens saved since the last call to cp_lexer_save_tokens
740 to the token stream. Stop saving tokens. */
743 cp_lexer_rollback_tokens (cp_lexer* lexer)
745 /* Provide debugging output. */
746 if (cp_lexer_debugging_p (lexer))
747 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
749 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
752 /* Print a representation of the TOKEN on the STREAM. */
754 #ifdef ENABLE_CHECKING
757 cp_lexer_print_token (FILE * stream, cp_token *token)
759 /* We don't use cpp_type2name here because the parser defines
760 a few tokens of its own. */
761 static const char *const token_names[] = {
762 /* cpplib-defined token types */
768 /* C++ parser token types - see "Manifest constants", above. */
771 "NESTED_NAME_SPECIFIER",
775 /* If we have a name for the token, print it out. Otherwise, we
776 simply give the numeric code. */
777 gcc_assert (token->type < ARRAY_SIZE(token_names));
778 fputs (token_names[token->type], stream);
780 /* For some tokens, print the associated data. */
784 /* Some keywords have a value that is not an IDENTIFIER_NODE.
785 For example, `struct' is mapped to an INTEGER_CST. */
786 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
788 /* else fall through */
790 fputs (IDENTIFIER_POINTER (token->u.value), stream);
795 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
803 /* Start emitting debugging information. */
806 cp_lexer_start_debugging (cp_lexer* lexer)
808 lexer->debugging_p = true;
811 /* Stop emitting debugging information. */
814 cp_lexer_stop_debugging (cp_lexer* lexer)
816 lexer->debugging_p = false;
819 #endif /* ENABLE_CHECKING */
821 /* Create a new cp_token_cache, representing a range of tokens. */
823 static cp_token_cache *
824 cp_token_cache_new (cp_token *first, cp_token *last)
826 cp_token_cache *cache = GGC_NEW (cp_token_cache);
827 cache->first = first;
833 /* Decl-specifiers. */
835 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
838 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
845 /* Nothing other than the parser should be creating declarators;
846 declarators are a semi-syntactic representation of C++ entities.
847 Other parts of the front end that need to create entities (like
848 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
850 static cp_declarator *make_call_declarator
851 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
852 static cp_declarator *make_array_declarator
853 (cp_declarator *, tree);
854 static cp_declarator *make_pointer_declarator
855 (cp_cv_quals, cp_declarator *);
856 static cp_declarator *make_reference_declarator
857 (cp_cv_quals, cp_declarator *, bool);
858 static cp_parameter_declarator *make_parameter_declarator
859 (cp_decl_specifier_seq *, cp_declarator *, tree);
860 static cp_declarator *make_ptrmem_declarator
861 (cp_cv_quals, tree, cp_declarator *);
863 /* An erroneous declarator. */
864 static cp_declarator *cp_error_declarator;
866 /* The obstack on which declarators and related data structures are
868 static struct obstack declarator_obstack;
870 /* Alloc BYTES from the declarator memory pool. */
873 alloc_declarator (size_t bytes)
875 return obstack_alloc (&declarator_obstack, bytes);
878 /* Allocate a declarator of the indicated KIND. Clear fields that are
879 common to all declarators. */
881 static cp_declarator *
882 make_declarator (cp_declarator_kind kind)
884 cp_declarator *declarator;
886 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
887 declarator->kind = kind;
888 declarator->attributes = NULL_TREE;
889 declarator->declarator = NULL;
890 declarator->parameter_pack_p = false;
895 /* Make a declarator for a generalized identifier. If
896 QUALIFYING_SCOPE is non-NULL, the identifier is
897 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
898 UNQUALIFIED_NAME. SFK indicates the kind of special function this
901 static cp_declarator *
902 make_id_declarator (tree qualifying_scope, tree unqualified_name,
903 special_function_kind sfk)
905 cp_declarator *declarator;
907 /* It is valid to write:
909 class C { void f(); };
913 The standard is not clear about whether `typedef const C D' is
914 legal; as of 2002-09-15 the committee is considering that
915 question. EDG 3.0 allows that syntax. Therefore, we do as
917 if (qualifying_scope && TYPE_P (qualifying_scope))
918 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
921 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
922 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924 declarator = make_declarator (cdk_id);
925 declarator->u.id.qualifying_scope = qualifying_scope;
926 declarator->u.id.unqualified_name = unqualified_name;
927 declarator->u.id.sfk = sfk;
932 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
933 of modifiers such as const or volatile to apply to the pointer
934 type, represented as identifiers. */
937 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 cp_declarator *declarator;
941 declarator = make_declarator (cdk_pointer);
942 declarator->declarator = target;
943 declarator->u.pointer.qualifiers = cv_qualifiers;
944 declarator->u.pointer.class_type = NULL_TREE;
947 declarator->parameter_pack_p = target->parameter_pack_p;
948 target->parameter_pack_p = false;
951 declarator->parameter_pack_p = false;
956 /* Like make_pointer_declarator -- but for references. */
959 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
962 cp_declarator *declarator;
964 declarator = make_declarator (cdk_reference);
965 declarator->declarator = target;
966 declarator->u.reference.qualifiers = cv_qualifiers;
967 declarator->u.reference.rvalue_ref = rvalue_ref;
970 declarator->parameter_pack_p = target->parameter_pack_p;
971 target->parameter_pack_p = false;
974 declarator->parameter_pack_p = false;
979 /* Like make_pointer_declarator -- but for a pointer to a non-static
980 member of CLASS_TYPE. */
983 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
984 cp_declarator *pointee)
986 cp_declarator *declarator;
988 declarator = make_declarator (cdk_ptrmem);
989 declarator->declarator = pointee;
990 declarator->u.pointer.qualifiers = cv_qualifiers;
991 declarator->u.pointer.class_type = class_type;
995 declarator->parameter_pack_p = pointee->parameter_pack_p;
996 pointee->parameter_pack_p = false;
999 declarator->parameter_pack_p = false;
1004 /* Make a declarator for the function given by TARGET, with the
1005 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1006 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1007 indicates what exceptions can be thrown. */
1010 make_call_declarator (cp_declarator *target,
1011 cp_parameter_declarator *parms,
1012 cp_cv_quals cv_qualifiers,
1013 tree exception_specification)
1015 cp_declarator *declarator;
1017 declarator = make_declarator (cdk_function);
1018 declarator->declarator = target;
1019 declarator->u.function.parameters = parms;
1020 declarator->u.function.qualifiers = cv_qualifiers;
1021 declarator->u.function.exception_specification = exception_specification;
1024 declarator->parameter_pack_p = target->parameter_pack_p;
1025 target->parameter_pack_p = false;
1028 declarator->parameter_pack_p = false;
1033 /* Make a declarator for an array of BOUNDS elements, each of which is
1034 defined by ELEMENT. */
1037 make_array_declarator (cp_declarator *element, tree bounds)
1039 cp_declarator *declarator;
1041 declarator = make_declarator (cdk_array);
1042 declarator->declarator = element;
1043 declarator->u.array.bounds = bounds;
1046 declarator->parameter_pack_p = element->parameter_pack_p;
1047 element->parameter_pack_p = false;
1050 declarator->parameter_pack_p = false;
1055 /* Determine whether the declarator we've seen so far can be a
1056 parameter pack, when followed by an ellipsis. */
1058 declarator_can_be_parameter_pack (cp_declarator *declarator)
1060 /* Search for a declarator name, or any other declarator that goes
1061 after the point where the ellipsis could appear in a parameter
1062 pack. If we find any of these, then this declarator can not be
1063 made into a parameter pack. */
1065 while (declarator && !found)
1067 switch ((int)declarator->kind)
1078 declarator = declarator->declarator;
1086 cp_parameter_declarator *no_parameters;
1088 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1089 DECLARATOR and DEFAULT_ARGUMENT. */
1091 cp_parameter_declarator *
1092 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1093 cp_declarator *declarator,
1094 tree default_argument)
1096 cp_parameter_declarator *parameter;
1098 parameter = ((cp_parameter_declarator *)
1099 alloc_declarator (sizeof (cp_parameter_declarator)));
1100 parameter->next = NULL;
1101 if (decl_specifiers)
1102 parameter->decl_specifiers = *decl_specifiers;
1104 clear_decl_specs (¶meter->decl_specifiers);
1105 parameter->declarator = declarator;
1106 parameter->default_argument = default_argument;
1107 parameter->ellipsis_p = false;
1112 /* Returns true iff DECLARATOR is a declaration for a function. */
1115 function_declarator_p (const cp_declarator *declarator)
1119 if (declarator->kind == cdk_function
1120 && declarator->declarator->kind == cdk_id)
1122 if (declarator->kind == cdk_id
1123 || declarator->kind == cdk_error)
1125 declarator = declarator->declarator;
1135 A cp_parser parses the token stream as specified by the C++
1136 grammar. Its job is purely parsing, not semantic analysis. For
1137 example, the parser breaks the token stream into declarators,
1138 expressions, statements, and other similar syntactic constructs.
1139 It does not check that the types of the expressions on either side
1140 of an assignment-statement are compatible, or that a function is
1141 not declared with a parameter of type `void'.
1143 The parser invokes routines elsewhere in the compiler to perform
1144 semantic analysis and to build up the abstract syntax tree for the
1147 The parser (and the template instantiation code, which is, in a
1148 way, a close relative of parsing) are the only parts of the
1149 compiler that should be calling push_scope and pop_scope, or
1150 related functions. The parser (and template instantiation code)
1151 keeps track of what scope is presently active; everything else
1152 should simply honor that. (The code that generates static
1153 initializers may also need to set the scope, in order to check
1154 access control correctly when emitting the initializers.)
1159 The parser is of the standard recursive-descent variety. Upcoming
1160 tokens in the token stream are examined in order to determine which
1161 production to use when parsing a non-terminal. Some C++ constructs
1162 require arbitrary look ahead to disambiguate. For example, it is
1163 impossible, in the general case, to tell whether a statement is an
1164 expression or declaration without scanning the entire statement.
1165 Therefore, the parser is capable of "parsing tentatively." When the
1166 parser is not sure what construct comes next, it enters this mode.
1167 Then, while we attempt to parse the construct, the parser queues up
1168 error messages, rather than issuing them immediately, and saves the
1169 tokens it consumes. If the construct is parsed successfully, the
1170 parser "commits", i.e., it issues any queued error messages and
1171 the tokens that were being preserved are permanently discarded.
1172 If, however, the construct is not parsed successfully, the parser
1173 rolls back its state completely so that it can resume parsing using
1174 a different alternative.
1179 The performance of the parser could probably be improved substantially.
1180 We could often eliminate the need to parse tentatively by looking ahead
1181 a little bit. In some places, this approach might not entirely eliminate
1182 the need to parse tentatively, but it might still speed up the average
1185 /* Flags that are passed to some parsing functions. These values can
1186 be bitwise-ored together. */
1188 typedef enum cp_parser_flags
1191 CP_PARSER_FLAGS_NONE = 0x0,
1192 /* The construct is optional. If it is not present, then no error
1193 should be issued. */
1194 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1195 /* When parsing a type-specifier, do not allow user-defined types. */
1196 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1199 /* The different kinds of declarators we want to parse. */
1201 typedef enum cp_parser_declarator_kind
1203 /* We want an abstract declarator. */
1204 CP_PARSER_DECLARATOR_ABSTRACT,
1205 /* We want a named declarator. */
1206 CP_PARSER_DECLARATOR_NAMED,
1207 /* We don't mind, but the name must be an unqualified-id. */
1208 CP_PARSER_DECLARATOR_EITHER
1209 } cp_parser_declarator_kind;
1211 /* The precedence values used to parse binary expressions. The minimum value
1212 of PREC must be 1, because zero is reserved to quickly discriminate
1213 binary operators from other tokens. */
1218 PREC_LOGICAL_OR_EXPRESSION,
1219 PREC_LOGICAL_AND_EXPRESSION,
1220 PREC_INCLUSIVE_OR_EXPRESSION,
1221 PREC_EXCLUSIVE_OR_EXPRESSION,
1222 PREC_AND_EXPRESSION,
1223 PREC_EQUALITY_EXPRESSION,
1224 PREC_RELATIONAL_EXPRESSION,
1225 PREC_SHIFT_EXPRESSION,
1226 PREC_ADDITIVE_EXPRESSION,
1227 PREC_MULTIPLICATIVE_EXPRESSION,
1229 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1232 /* A mapping from a token type to a corresponding tree node type, with a
1233 precedence value. */
1235 typedef struct cp_parser_binary_operations_map_node
1237 /* The token type. */
1238 enum cpp_ttype token_type;
1239 /* The corresponding tree code. */
1240 enum tree_code tree_type;
1241 /* The precedence of this operator. */
1242 enum cp_parser_prec prec;
1243 } cp_parser_binary_operations_map_node;
1245 /* The status of a tentative parse. */
1247 typedef enum cp_parser_status_kind
1249 /* No errors have occurred. */
1250 CP_PARSER_STATUS_KIND_NO_ERROR,
1251 /* An error has occurred. */
1252 CP_PARSER_STATUS_KIND_ERROR,
1253 /* We are committed to this tentative parse, whether or not an error
1255 CP_PARSER_STATUS_KIND_COMMITTED
1256 } cp_parser_status_kind;
1258 typedef struct cp_parser_expression_stack_entry
1260 /* Left hand side of the binary operation we are currently
1263 /* Original tree code for left hand side, if it was a binary
1264 expression itself (used for -Wparentheses). */
1265 enum tree_code lhs_type;
1266 /* Tree code for the binary operation we are parsing. */
1267 enum tree_code tree_type;
1268 /* Precedence of the binary operation we are parsing. */
1270 } cp_parser_expression_stack_entry;
1272 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1273 entries because precedence levels on the stack are monotonically
1275 typedef struct cp_parser_expression_stack_entry
1276 cp_parser_expression_stack[NUM_PREC_VALUES];
1278 /* Context that is saved and restored when parsing tentatively. */
1279 typedef struct cp_parser_context GTY (())
1281 /* If this is a tentative parsing context, the status of the
1283 enum cp_parser_status_kind status;
1284 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1285 that are looked up in this context must be looked up both in the
1286 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1287 the context of the containing expression. */
1290 /* The next parsing context in the stack. */
1291 struct cp_parser_context *next;
1292 } cp_parser_context;
1296 /* Constructors and destructors. */
1298 static cp_parser_context *cp_parser_context_new
1299 (cp_parser_context *);
1301 /* Class variables. */
1303 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1305 /* The operator-precedence table used by cp_parser_binary_expression.
1306 Transformed into an associative array (binops_by_token) by
1309 static const cp_parser_binary_operations_map_node binops[] = {
1310 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1311 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1313 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1314 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1315 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1318 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1320 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1321 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1323 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1324 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1325 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1326 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1329 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1331 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1333 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1335 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1337 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1339 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1342 /* The same as binops, but initialized by cp_parser_new so that
1343 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1345 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1347 /* Constructors and destructors. */
1349 /* Construct a new context. The context below this one on the stack
1350 is given by NEXT. */
1352 static cp_parser_context *
1353 cp_parser_context_new (cp_parser_context* next)
1355 cp_parser_context *context;
1357 /* Allocate the storage. */
1358 if (cp_parser_context_free_list != NULL)
1360 /* Pull the first entry from the free list. */
1361 context = cp_parser_context_free_list;
1362 cp_parser_context_free_list = context->next;
1363 memset (context, 0, sizeof (*context));
1366 context = GGC_CNEW (cp_parser_context);
1368 /* No errors have occurred yet in this context. */
1369 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1370 /* If this is not the bottomost context, copy information that we
1371 need from the previous context. */
1374 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1375 expression, then we are parsing one in this context, too. */
1376 context->object_type = next->object_type;
1377 /* Thread the stack. */
1378 context->next = next;
1384 /* The cp_parser structure represents the C++ parser. */
1386 typedef struct cp_parser GTY(())
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_class_or_namespace_name
1580 (cp_parser *, bool, bool, bool, bool, bool);
1581 static tree cp_parser_postfix_expression
1582 (cp_parser *, bool, bool, bool);
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 *);
1587 static tree 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);
1593 static enum tree_code cp_parser_unary_operator
1595 static tree cp_parser_new_expression
1597 static tree 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 tree cp_parser_new_initializer
1607 static tree cp_parser_delete_expression
1609 static tree cp_parser_cast_expression
1610 (cp_parser *, bool, bool);
1611 static tree cp_parser_binary_expression
1612 (cp_parser *, bool);
1613 static tree cp_parser_question_colon_clause
1614 (cp_parser *, tree);
1615 static tree cp_parser_assignment_expression
1616 (cp_parser *, bool);
1617 static enum tree_code cp_parser_assignment_operator_opt
1619 static tree cp_parser_expression
1620 (cp_parser *, bool);
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_elaborated_type_specifier
1680 (cp_parser *, bool, bool);
1681 static tree cp_parser_enum_specifier
1683 static void cp_parser_enumerator_list
1684 (cp_parser *, tree);
1685 static void cp_parser_enumerator_definition
1686 (cp_parser *, tree);
1687 static tree cp_parser_namespace_name
1689 static void cp_parser_namespace_definition
1691 static void cp_parser_namespace_body
1693 static tree cp_parser_qualified_namespace_specifier
1695 static void cp_parser_namespace_alias_definition
1697 static bool cp_parser_using_declaration
1698 (cp_parser *, bool);
1699 static void cp_parser_using_directive
1701 static void cp_parser_asm_definition
1703 static void cp_parser_linkage_specification
1705 static void cp_parser_static_assert
1706 (cp_parser *, bool);
1707 static tree cp_parser_decltype
1710 /* Declarators [gram.dcl.decl] */
1712 static tree cp_parser_init_declarator
1713 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1714 static cp_declarator *cp_parser_declarator
1715 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1716 static cp_declarator *cp_parser_direct_declarator
1717 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1718 static enum tree_code cp_parser_ptr_operator
1719 (cp_parser *, tree *, cp_cv_quals *);
1720 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1722 static tree cp_parser_declarator_id
1723 (cp_parser *, bool);
1724 static tree cp_parser_type_id
1726 static void cp_parser_type_specifier_seq
1727 (cp_parser *, bool, cp_decl_specifier_seq *);
1728 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1730 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1731 (cp_parser *, bool *);
1732 static cp_parameter_declarator *cp_parser_parameter_declaration
1733 (cp_parser *, bool, bool *);
1734 static tree cp_parser_default_argument
1735 (cp_parser *, bool);
1736 static void cp_parser_function_body
1738 static tree cp_parser_initializer
1739 (cp_parser *, bool *, bool *);
1740 static tree cp_parser_initializer_clause
1741 (cp_parser *, bool *);
1742 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1743 (cp_parser *, bool *);
1745 static bool cp_parser_ctor_initializer_opt_and_function_body
1748 /* Classes [gram.class] */
1750 static tree cp_parser_class_name
1751 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1752 static tree cp_parser_class_specifier
1754 static tree cp_parser_class_head
1755 (cp_parser *, bool *, tree *, tree *);
1756 static enum tag_types cp_parser_class_key
1758 static void cp_parser_member_specification_opt
1760 static void cp_parser_member_declaration
1762 static tree cp_parser_pure_specifier
1764 static tree cp_parser_constant_initializer
1767 /* Derived classes [gram.class.derived] */
1769 static tree cp_parser_base_clause
1771 static tree cp_parser_base_specifier
1774 /* Special member functions [gram.special] */
1776 static tree cp_parser_conversion_function_id
1778 static tree cp_parser_conversion_type_id
1780 static cp_declarator *cp_parser_conversion_declarator_opt
1782 static bool cp_parser_ctor_initializer_opt
1784 static void cp_parser_mem_initializer_list
1786 static tree cp_parser_mem_initializer
1788 static tree cp_parser_mem_initializer_id
1791 /* Overloading [gram.over] */
1793 static tree cp_parser_operator_function_id
1795 static tree cp_parser_operator
1798 /* Templates [gram.temp] */
1800 static void cp_parser_template_declaration
1801 (cp_parser *, bool);
1802 static tree cp_parser_template_parameter_list
1804 static tree cp_parser_template_parameter
1805 (cp_parser *, bool *, bool *);
1806 static tree cp_parser_type_parameter
1807 (cp_parser *, bool *);
1808 static tree cp_parser_template_id
1809 (cp_parser *, bool, bool, bool);
1810 static tree cp_parser_template_name
1811 (cp_parser *, bool, bool, bool, bool *);
1812 static tree cp_parser_template_argument_list
1814 static tree cp_parser_template_argument
1816 static void cp_parser_explicit_instantiation
1818 static void cp_parser_explicit_specialization
1821 /* Exception handling [gram.exception] */
1823 static tree cp_parser_try_block
1825 static bool cp_parser_function_try_block
1827 static void cp_parser_handler_seq
1829 static void cp_parser_handler
1831 static tree cp_parser_exception_declaration
1833 static tree cp_parser_throw_expression
1835 static tree cp_parser_exception_specification_opt
1837 static tree cp_parser_type_id_list
1840 /* GNU Extensions */
1842 static tree cp_parser_asm_specification_opt
1844 static tree cp_parser_asm_operand_list
1846 static tree cp_parser_asm_clobber_list
1848 static tree cp_parser_attributes_opt
1850 static tree cp_parser_attribute_list
1852 static bool cp_parser_extension_opt
1853 (cp_parser *, int *);
1854 static void cp_parser_label_declaration
1857 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1858 static bool cp_parser_pragma
1859 (cp_parser *, enum pragma_context);
1861 /* Objective-C++ Productions */
1863 static tree cp_parser_objc_message_receiver
1865 static tree cp_parser_objc_message_args
1867 static tree cp_parser_objc_message_expression
1869 static tree cp_parser_objc_encode_expression
1871 static tree cp_parser_objc_defs_expression
1873 static tree cp_parser_objc_protocol_expression
1875 static tree cp_parser_objc_selector_expression
1877 static tree cp_parser_objc_expression
1879 static bool cp_parser_objc_selector_p
1881 static tree cp_parser_objc_selector
1883 static tree cp_parser_objc_protocol_refs_opt
1885 static void cp_parser_objc_declaration
1887 static tree cp_parser_objc_statement
1890 /* Utility Routines */
1892 static tree cp_parser_lookup_name
1893 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1894 static tree cp_parser_lookup_name_simple
1895 (cp_parser *, tree);
1896 static tree cp_parser_maybe_treat_template_as_class
1898 static bool cp_parser_check_declarator_template_parameters
1899 (cp_parser *, cp_declarator *);
1900 static bool cp_parser_check_template_parameters
1901 (cp_parser *, unsigned);
1902 static tree cp_parser_simple_cast_expression
1904 static tree cp_parser_global_scope_opt
1905 (cp_parser *, bool);
1906 static bool cp_parser_constructor_declarator_p
1907 (cp_parser *, bool);
1908 static tree cp_parser_function_definition_from_specifiers_and_declarator
1909 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1910 static tree cp_parser_function_definition_after_declarator
1911 (cp_parser *, bool);
1912 static void cp_parser_template_declaration_after_export
1913 (cp_parser *, bool);
1914 static void cp_parser_perform_template_parameter_access_checks
1915 (VEC (deferred_access_check,gc)*);
1916 static tree cp_parser_single_declaration
1917 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1918 static tree cp_parser_functional_cast
1919 (cp_parser *, tree);
1920 static tree cp_parser_save_member_function_body
1921 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1922 static tree cp_parser_enclosed_template_argument_list
1924 static void cp_parser_save_default_args
1925 (cp_parser *, tree);
1926 static void cp_parser_late_parsing_for_member
1927 (cp_parser *, tree);
1928 static void cp_parser_late_parsing_default_args
1929 (cp_parser *, tree);
1930 static tree cp_parser_sizeof_operand
1931 (cp_parser *, enum rid);
1932 static tree cp_parser_trait_expr
1933 (cp_parser *, enum rid);
1934 static bool cp_parser_declares_only_class_p
1936 static void cp_parser_set_storage_class
1937 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1938 static void cp_parser_set_decl_spec_type
1939 (cp_decl_specifier_seq *, tree, bool);
1940 static bool cp_parser_friend_p
1941 (const cp_decl_specifier_seq *);
1942 static cp_token *cp_parser_require
1943 (cp_parser *, enum cpp_ttype, const char *);
1944 static cp_token *cp_parser_require_keyword
1945 (cp_parser *, enum rid, const char *);
1946 static bool cp_parser_token_starts_function_definition_p
1948 static bool cp_parser_next_token_starts_class_definition_p
1950 static bool cp_parser_next_token_ends_template_argument_p
1952 static bool cp_parser_nth_token_starts_template_argument_list_p
1953 (cp_parser *, size_t);
1954 static enum tag_types cp_parser_token_is_class_key
1956 static void cp_parser_check_class_key
1957 (enum tag_types, tree type);
1958 static void cp_parser_check_access_in_redeclaration
1960 static bool cp_parser_optional_template_keyword
1962 static void cp_parser_pre_parsed_nested_name_specifier
1964 static void cp_parser_cache_group
1965 (cp_parser *, enum cpp_ttype, unsigned);
1966 static void cp_parser_parse_tentatively
1968 static void cp_parser_commit_to_tentative_parse
1970 static void cp_parser_abort_tentative_parse
1972 static bool cp_parser_parse_definitely
1974 static inline bool cp_parser_parsing_tentatively
1976 static bool cp_parser_uncommitted_to_tentative_parse_p
1978 static void cp_parser_error
1979 (cp_parser *, const char *);
1980 static void cp_parser_name_lookup_error
1981 (cp_parser *, tree, tree, const char *);
1982 static bool cp_parser_simulate_error
1984 static bool cp_parser_check_type_definition
1986 static void cp_parser_check_for_definition_in_return_type
1987 (cp_declarator *, tree);
1988 static void cp_parser_check_for_invalid_template_id
1989 (cp_parser *, tree);
1990 static bool cp_parser_non_integral_constant_expression
1991 (cp_parser *, const char *);
1992 static void cp_parser_diagnose_invalid_type_name
1993 (cp_parser *, tree, tree);
1994 static bool cp_parser_parse_and_diagnose_invalid_type_name
1996 static int cp_parser_skip_to_closing_parenthesis
1997 (cp_parser *, bool, bool, bool);
1998 static void cp_parser_skip_to_end_of_statement
2000 static void cp_parser_consume_semicolon_at_end_of_statement
2002 static void cp_parser_skip_to_end_of_block_or_statement
2004 static bool cp_parser_skip_to_closing_brace
2006 static void cp_parser_skip_to_end_of_template_parameter_list
2008 static void cp_parser_skip_to_pragma_eol
2009 (cp_parser*, cp_token *);
2010 static bool cp_parser_error_occurred
2012 static bool cp_parser_allow_gnu_extensions_p
2014 static bool cp_parser_is_string_literal
2016 static bool cp_parser_is_keyword
2017 (cp_token *, enum rid);
2018 static tree cp_parser_make_typename_type
2019 (cp_parser *, tree, tree);
2020 static cp_declarator * cp_parser_make_indirect_declarator
2021 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2023 /* Returns nonzero if we are parsing tentatively. */
2026 cp_parser_parsing_tentatively (cp_parser* parser)
2028 return parser->context->next != NULL;
2031 /* Returns nonzero if TOKEN is a string literal. */
2034 cp_parser_is_string_literal (cp_token* token)
2036 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2039 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2042 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2044 return token->keyword == keyword;
2047 /* If not parsing tentatively, issue a diagnostic of the form
2048 FILE:LINE: MESSAGE before TOKEN
2049 where TOKEN is the next token in the input stream. MESSAGE
2050 (specified by the caller) is usually of the form "expected
2054 cp_parser_error (cp_parser* parser, const char* message)
2056 if (!cp_parser_simulate_error (parser))
2058 cp_token *token = cp_lexer_peek_token (parser->lexer);
2059 /* This diagnostic makes more sense if it is tagged to the line
2060 of the token we just peeked at. */
2061 cp_lexer_set_source_position_from_token (token);
2063 if (token->type == CPP_PRAGMA)
2065 error ("%<#pragma%> is not allowed here");
2066 cp_parser_skip_to_pragma_eol (parser, token);
2070 c_parse_error (message,
2071 /* Because c_parser_error does not understand
2072 CPP_KEYWORD, keywords are treated like
2074 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079 /* Issue an error about name-lookup failing. NAME is the
2080 IDENTIFIER_NODE DECL is the result of
2081 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2082 the thing that we hoped to find. */
2085 cp_parser_name_lookup_error (cp_parser* parser,
2088 const char* desired)
2090 /* If name lookup completely failed, tell the user that NAME was not
2092 if (decl == error_mark_node)
2094 if (parser->scope && parser->scope != global_namespace)
2095 error ("%<%E::%E%> has not been declared",
2096 parser->scope, name);
2097 else if (parser->scope == global_namespace)
2098 error ("%<::%E%> has not been declared", name);
2099 else if (parser->object_scope
2100 && !CLASS_TYPE_P (parser->object_scope))
2101 error ("request for member %qE in non-class type %qT",
2102 name, parser->object_scope);
2103 else if (parser->object_scope)
2104 error ("%<%T::%E%> has not been declared",
2105 parser->object_scope, name);
2107 error ("%qE has not been declared", name);
2109 else if (parser->scope && parser->scope != global_namespace)
2110 error ("%<%E::%E%> %s", parser->scope, name, desired);
2111 else if (parser->scope == global_namespace)
2112 error ("%<::%E%> %s", name, desired);
2114 error ("%qE %s", name, desired);
2117 /* If we are parsing tentatively, remember that an error has occurred
2118 during this tentative parse. Returns true if the error was
2119 simulated; false if a message should be issued by the caller. */
2122 cp_parser_simulate_error (cp_parser* parser)
2124 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2126 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2132 /* Check for repeated decl-specifiers. */
2135 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2139 for (ds = ds_first; ds != ds_last; ++ds)
2141 unsigned count = decl_specs->specs[(int)ds];
2144 /* The "long" specifier is a special case because of "long long". */
2148 error ("%<long long long%> is too long for GCC");
2149 else if (pedantic && !in_system_header && warn_long_long
2150 && cxx_dialect == cxx98)
2151 pedwarn ("ISO C++ 1998 does not support %<long long%>");
2155 static const char *const decl_spec_names[] = {
2171 error ("duplicate %qs", decl_spec_names[(int)ds]);
2176 /* This function is called when a type is defined. If type
2177 definitions are forbidden at this point, an error message is
2181 cp_parser_check_type_definition (cp_parser* parser)
2183 /* If types are forbidden here, issue a message. */
2184 if (parser->type_definition_forbidden_message)
2186 /* Use `%s' to print the string in case there are any escape
2187 characters in the message. */
2188 error ("%s", parser->type_definition_forbidden_message);
2194 /* This function is called when the DECLARATOR is processed. The TYPE
2195 was a type defined in the decl-specifiers. If it is invalid to
2196 define a type in the decl-specifiers for DECLARATOR, an error is
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2203 /* [dcl.fct] forbids type definitions in return types.
2204 Unfortunately, it's not easy to know whether or not we are
2205 processing a return type until after the fact. */
2207 && (declarator->kind == cdk_pointer
2208 || declarator->kind == cdk_reference
2209 || declarator->kind == cdk_ptrmem))
2210 declarator = declarator->declarator;
2212 && declarator->kind == cdk_function)
2214 error ("new types may not be defined in a return type");
2215 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221 "<" in any valid C++ program. If the next token is indeed "<",
2222 issue a message warning the user about what appears to be an
2223 invalid attempt to form a template-id. */
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2229 cp_token_position start = 0;
2231 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2234 error ("%qT is not a template", type);
2235 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236 error ("%qE is not a template", type);
2238 error ("invalid template-id");
2239 /* Remember the location of the invalid "<". */
2240 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241 start = cp_lexer_token_position (parser->lexer, true);
2242 /* Consume the "<". */
2243 cp_lexer_consume_token (parser->lexer);
2244 /* Parse the template arguments. */
2245 cp_parser_enclosed_template_argument_list (parser);
2246 /* Permanently remove the invalid template arguments so that
2247 this error message is not issued again. */
2249 cp_lexer_purge_tokens_after (parser->lexer, start);
2253 /* If parsing an integral constant-expression, issue an error message
2254 about the fact that THING appeared and return true. Otherwise,
2255 return false. In either case, set
2256 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2259 cp_parser_non_integral_constant_expression (cp_parser *parser,
2262 parser->non_integral_constant_expression_p = true;
2263 if (parser->integral_constant_expression_p)
2265 if (!parser->allow_non_integral_constant_expression_p)
2267 error ("%s cannot appear in a constant-expression", thing);
2274 /* Emit a diagnostic for an invalid type name. SCOPE is the
2275 qualifying scope (or NULL, if none) for ID. This function commits
2276 to the current active tentative parse, if any. (Otherwise, the
2277 problematic construct might be encountered again later, resulting
2278 in duplicate error messages.) */
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2283 tree decl, old_scope;
2284 /* Try to lookup the identifier. */
2285 old_scope = parser->scope;
2286 parser->scope = scope;
2287 decl = cp_parser_lookup_name_simple (parser, id);
2288 parser->scope = old_scope;
2289 /* If the lookup found a template-name, it means that the user forgot
2290 to specify an argument list. Emit a useful error message. */
2291 if (TREE_CODE (decl) == TEMPLATE_DECL)
2292 error ("invalid use of template-name %qE without an argument list", decl);
2293 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294 error ("invalid use of destructor %qD as a type", id);
2295 else if (TREE_CODE (decl) == TYPE_DECL)
2296 /* Something like 'unsigned A a;' */
2297 error ("invalid combination of multiple type-specifiers");
2298 else if (!parser->scope)
2300 /* Issue an error message. */
2301 error ("%qE does not name a type", id);
2302 /* If we're in a template class, it's possible that the user was
2303 referring to a type from a base class. For example:
2305 template <typename T> struct A { typedef T X; };
2306 template <typename T> struct B : public A<T> { X x; };
2308 The user should have said "typename A<T>::X". */
2309 if (processing_template_decl && current_class_type
2310 && TYPE_BINFO (current_class_type))
2314 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2318 tree base_type = BINFO_TYPE (b);
2319 if (CLASS_TYPE_P (base_type)
2320 && dependent_type_p (base_type))
2323 /* Go from a particular instantiation of the
2324 template (which will have an empty TYPE_FIELDs),
2325 to the main version. */
2326 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327 for (field = TYPE_FIELDS (base_type);
2329 field = TREE_CHAIN (field))
2330 if (TREE_CODE (field) == TYPE_DECL
2331 && DECL_NAME (field) == id)
2333 inform ("(perhaps %<typename %T::%E%> was intended)",
2334 BINFO_TYPE (b), id);
2343 /* Here we diagnose qualified-ids where the scope is actually correct,
2344 but the identifier does not resolve to a valid type name. */
2345 else if (parser->scope != error_mark_node)
2347 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348 error ("%qE in namespace %qE does not name a type",
2350 else if (TYPE_P (parser->scope))
2351 error ("%qE in class %qT does not name a type", id, parser->scope);
2355 cp_parser_commit_to_tentative_parse (parser);
2358 /* Check for a common situation where a type-name should be present,
2359 but is not, and issue a sensible error message. Returns true if an
2360 invalid type-name was detected.
2362 The situation handled by this function are variable declarations of the
2363 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364 Usually, `ID' should name a type, but if we got here it means that it
2365 does not. We try to emit the best possible error message depending on
2366 how exactly the id-expression looks like. */
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2373 cp_parser_parse_tentatively (parser);
2374 id = cp_parser_id_expression (parser,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL,
2378 /*declarator_p=*/true,
2379 /*optional_p=*/false);
2380 /* After the id-expression, there should be a plain identifier,
2381 otherwise this is not a simple variable declaration. Also, if
2382 the scope is dependent, we cannot do much. */
2383 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384 || (parser->scope && TYPE_P (parser->scope)
2385 && dependent_type_p (parser->scope))
2386 || TREE_CODE (id) == TYPE_DECL)
2388 cp_parser_abort_tentative_parse (parser);
2391 if (!cp_parser_parse_definitely (parser))
2394 /* Emit a diagnostic for the invalid type. */
2395 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396 /* Skip to the end of the declaration; there's no point in
2397 trying to process it. */
2398 cp_parser_skip_to_end_of_block_or_statement (parser);
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2404 are doing error recovery. Returns -1 if OR_COMMA is true and we
2405 found an unnested comma. */
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2413 unsigned paren_depth = 0;
2414 unsigned brace_depth = 0;
2416 if (recovering && !or_comma
2417 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2422 cp_token * token = cp_lexer_peek_token (parser->lexer);
2424 switch (token->type)
2427 case CPP_PRAGMA_EOL:
2428 /* If we've run out of tokens, then there is no closing `)'. */
2432 /* This matches the processing in skip_to_end_of_statement. */
2437 case CPP_OPEN_BRACE:
2440 case CPP_CLOSE_BRACE:
2446 if (recovering && or_comma && !brace_depth && !paren_depth)
2450 case CPP_OPEN_PAREN:
2455 case CPP_CLOSE_PAREN:
2456 if (!brace_depth && !paren_depth--)
2459 cp_lexer_consume_token (parser->lexer);
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2473 /* Consume tokens until we reach the end of the current statement.
2474 Normally, that will be just before consuming a `;'. However, if a
2475 non-nested `}' comes first, then we stop before consuming that. */
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2480 unsigned nesting_depth = 0;
2484 cp_token *token = cp_lexer_peek_token (parser->lexer);
2486 switch (token->type)
2489 case CPP_PRAGMA_EOL:
2490 /* If we've run out of tokens, stop. */
2494 /* If the next token is a `;', we have reached the end of the
2500 case CPP_CLOSE_BRACE:
2501 /* If this is a non-nested '}', stop before consuming it.
2502 That way, when confronted with something like:
2506 we stop before consuming the closing '}', even though we
2507 have not yet reached a `;'. */
2508 if (nesting_depth == 0)
2511 /* If it is the closing '}' for a block that we have
2512 scanned, stop -- but only after consuming the token.
2518 we will stop after the body of the erroneously declared
2519 function, but before consuming the following `typedef'
2521 if (--nesting_depth == 0)
2523 cp_lexer_consume_token (parser->lexer);
2527 case CPP_OPEN_BRACE:
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2540 /* This function is called at the end of a statement or declaration.
2541 If the next token is a semicolon, it is consumed; otherwise, error
2542 recovery is attempted. */
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2547 /* Look for the trailing `;'. */
2548 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2550 /* If there is additional (erroneous) input, skip to the end of
2552 cp_parser_skip_to_end_of_statement (parser);
2553 /* If the next token is now a `;', consume it. */
2554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555 cp_lexer_consume_token (parser->lexer);
2559 /* Skip tokens until we have consumed an entire block, or until we
2560 have consumed a non-nested `;'. */
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2565 int nesting_depth = 0;
2567 while (nesting_depth >= 0)
2569 cp_token *token = cp_lexer_peek_token (parser->lexer);
2571 switch (token->type)
2574 case CPP_PRAGMA_EOL:
2575 /* If we've run out of tokens, stop. */
2579 /* Stop if this is an unnested ';'. */
2584 case CPP_CLOSE_BRACE:
2585 /* Stop if this is an unnested '}', or closes the outermost
2592 case CPP_OPEN_BRACE:
2601 /* Consume the token. */
2602 cp_lexer_consume_token (parser->lexer);
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607 token, or there are no more tokens. Return true in the first case,
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2613 unsigned nesting_depth = 0;
2617 cp_token *token = cp_lexer_peek_token (parser->lexer);
2619 switch (token->type)
2622 case CPP_PRAGMA_EOL:
2623 /* If we've run out of tokens, stop. */
2626 case CPP_CLOSE_BRACE:
2627 /* If the next token is a non-nested `}', then we have reached
2628 the end of the current block. */
2629 if (nesting_depth-- == 0)
2633 case CPP_OPEN_BRACE:
2634 /* If it the next token is a `{', then we are entering a new
2635 block. Consume the entire block. */
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser->lexer);
2648 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2649 parameter is the PRAGMA token, allowing us to purge the entire pragma
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2657 parser->lexer->in_pragma = false;
2660 token = cp_lexer_consume_token (parser->lexer);
2661 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2663 /* Ensure that the pragma is not parsed again. */
2664 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2667 /* Require pragma end of line, resyncing with it as necessary. The
2668 arguments are as for cp_parser_skip_to_pragma_eol. */
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2673 parser->lexer->in_pragma = false;
2674 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679 an unresolved identifier node, we can provide a superior diagnostic
2680 using cp_parser_diagnose_invalid_type_name. */
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2686 if (TREE_CODE (id) == IDENTIFIER_NODE)
2688 result = make_typename_type (scope, id, typename_type,
2689 /*complain=*/tf_none);
2690 if (result == error_mark_node)
2691 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2694 return make_typename_type (scope, id, typename_type, tf_error);
2697 /* This is a wrapper around the
2698 make_{pointer,ptrmem,reference}_declarator functions that decides
2699 which one to call based on the CODE and CLASS_TYPE arguments. The
2700 CODE argument should be one of the values returned by
2701 cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704 cp_cv_quals cv_qualifiers,
2705 cp_declarator *target)
2707 if (code == ERROR_MARK)
2708 return cp_error_declarator;
2710 if (code == INDIRECT_REF)
2711 if (class_type == NULL_TREE)
2712 return make_pointer_declarator (cv_qualifiers, target);
2714 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, false);
2717 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718 return make_reference_declarator (cv_qualifiers, target, true);
2722 /* Create a new C++ parser. */
2725 cp_parser_new (void)
2731 /* cp_lexer_new_main is called before calling ggc_alloc because
2732 cp_lexer_new_main might load a PCH file. */
2733 lexer = cp_lexer_new_main ();
2735 /* Initialize the binops_by_token so that we can get the tree
2736 directly from the token. */
2737 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738 binops_by_token[binops[i].token_type] = binops[i];
2740 parser = GGC_CNEW (cp_parser);
2741 parser->lexer = lexer;
2742 parser->context = cp_parser_context_new (NULL);
2744 /* For now, we always accept GNU extensions. */
2745 parser->allow_gnu_extensions_p = 1;
2747 /* The `>' token is a greater-than operator, not the end of a
2749 parser->greater_than_is_operator_p = true;
2751 parser->default_arg_ok_p = true;
2753 /* We are not parsing a constant-expression. */
2754 parser->integral_constant_expression_p = false;
2755 parser->allow_non_integral_constant_expression_p = false;
2756 parser->non_integral_constant_expression_p = false;
2758 /* Local variable names are not forbidden. */
2759 parser->local_variables_forbidden_p = false;
2761 /* We are not processing an `extern "C"' declaration. */
2762 parser->in_unbraced_linkage_specification_p = false;
2764 /* We are not processing a declarator. */
2765 parser->in_declarator_p = false;
2767 /* We are not processing a template-argument-list. */
2768 parser->in_template_argument_list_p = false;
2770 /* We are not in an iteration statement. */
2771 parser->in_statement = 0;
2773 /* We are not in a switch statement. */
2774 parser->in_switch_statement_p = false;
2776 /* We are not parsing a type-id inside an expression. */
2777 parser->in_type_id_in_expr_p = false;
2779 /* Declarations aren't implicitly extern "C". */
2780 parser->implicit_extern_c = false;
2782 /* String literals should be translated to the execution character set. */
2783 parser->translate_strings_p = true;
2785 /* We are not parsing a function body. */
2786 parser->in_function_body = false;
2788 /* The unparsed function queue is empty. */
2789 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2791 /* There are no classes being defined. */
2792 parser->num_classes_being_defined = 0;
2794 /* No template parameters apply. */
2795 parser->num_template_parameter_lists = 0;
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801 and push it onto the parser's lexer stack. This is used for delayed
2802 parsing of in-class method bodies and default arguments, and should
2803 not be confused with tentative parsing. */
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2807 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808 lexer->next = parser->lexer;
2809 parser->lexer = lexer;
2811 /* Move the current source position to that of the first token in the
2813 cp_lexer_set_source_position_from_token (lexer->next_token);
2816 /* Pop the top lexer off the parser stack. This is never used for the
2817 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2819 cp_parser_pop_lexer (cp_parser *parser)
2821 cp_lexer *lexer = parser->lexer;
2822 parser->lexer = lexer->next;
2823 cp_lexer_destroy (lexer);
2825 /* Put the current source position back where it was before this
2826 lexer was pushed. */
2827 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2830 /* Lexical conventions [gram.lex] */
2832 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2836 cp_parser_identifier (cp_parser* parser)
2840 /* Look for the identifier. */
2841 token = cp_parser_require (parser, CPP_NAME, "identifier");
2842 /* Return the value. */
2843 return token ? token->u.value : error_mark_node;
2846 /* Parse a sequence of adjacent string constants. Returns a
2847 TREE_STRING representing the combined, nul-terminated string
2848 constant. If TRANSLATE is true, translate the string to the
2849 execution character set. If WIDE_OK is true, a wide string is
2852 C++98 [lex.string] says that if a narrow string literal token is
2853 adjacent to a wide string literal token, the behavior is undefined.
2854 However, C99 6.4.5p4 says that this results in a wide string literal.
2855 We follow C99 here, for consistency with the C front end.
2857 This code is largely lifted from lex_string() in c-lex.c.
2859 FUTURE: ObjC++ will need to handle @-strings here. */
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2866 struct obstack str_ob;
2867 cpp_string str, istr, *strs;
2870 tok = cp_lexer_peek_token (parser->lexer);
2871 if (!cp_parser_is_string_literal (tok))
2873 cp_parser_error (parser, "expected string-literal");
2874 return error_mark_node;
2877 /* Try to avoid the overhead of creating and destroying an obstack
2878 for the common case of just one string. */
2879 if (!cp_parser_is_string_literal
2880 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2882 cp_lexer_consume_token (parser->lexer);
2884 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885 str.len = TREE_STRING_LENGTH (tok->u.value);
2887 if (tok->type == CPP_WSTRING)
2894 gcc_obstack_init (&str_ob);
2899 cp_lexer_consume_token (parser->lexer);
2901 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902 str.len = TREE_STRING_LENGTH (tok->u.value);
2903 if (tok->type == CPP_WSTRING)
2906 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2908 tok = cp_lexer_peek_token (parser->lexer);
2910 while (cp_parser_is_string_literal (tok));
2912 strs = (cpp_string *) obstack_finish (&str_ob);
2915 if (wide && !wide_ok)
2917 cp_parser_error (parser, "a wide string is invalid in this context");
2921 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922 (parse_in, strs, count, &istr, wide))
2924 value = build_string (istr.len, (const char *)istr.text);
2925 free (CONST_CAST (unsigned char *, istr.text));
2927 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928 value = fix_string_type (value);
2931 /* cpp_interpret_string has issued an error. */
2932 value = error_mark_node;
2935 obstack_free (&str_ob, 0);
2941 /* Basic concepts [gram.basic] */
2943 /* Parse a translation-unit.
2946 declaration-seq [opt]
2948 Returns TRUE if all went well. */
2951 cp_parser_translation_unit (cp_parser* parser)
2953 /* The address of the first non-permanent object on the declarator
2955 static void *declarator_obstack_base;
2959 /* Create the declarator obstack, if necessary. */
2960 if (!cp_error_declarator)
2962 gcc_obstack_init (&declarator_obstack);
2963 /* Create the error declarator. */
2964 cp_error_declarator = make_declarator (cdk_error);
2965 /* Create the empty parameter list. */
2966 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967 /* Remember where the base of the declarator obstack lies. */
2968 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2971 cp_parser_declaration_seq_opt (parser);
2973 /* If there are no tokens left then all went well. */
2974 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2976 /* Get rid of the token array; we don't need it any more. */
2977 cp_lexer_destroy (parser->lexer);
2978 parser->lexer = NULL;
2980 /* This file might have been a context that's implicitly extern
2981 "C". If so, pop the lang context. (Only relevant for PCH.) */
2982 if (parser->implicit_extern_c)
2984 pop_lang_context ();
2985 parser->implicit_extern_c = false;
2989 finish_translation_unit ();
2995 cp_parser_error (parser, "expected declaration");
2999 /* Make sure the declarator obstack was fully cleaned up. */
3000 gcc_assert (obstack_next_free (&declarator_obstack)
3001 == declarator_obstack_base);
3003 /* All went well. */
3007 /* Expressions [gram.expr] */
3009 /* Parse a primary-expression.
3020 ( compound-statement )
3021 __builtin_va_arg ( assignment-expression , type-id )
3022 __builtin_offsetof ( type-id , offsetof-expression )
3025 __has_nothrow_assign ( type-id )
3026 __has_nothrow_constructor ( type-id )
3027 __has_nothrow_copy ( type-id )
3028 __has_trivial_assign ( type-id )
3029 __has_trivial_constructor ( type-id )
3030 __has_trivial_copy ( type-id )
3031 __has_trivial_destructor ( type-id )
3032 __has_virtual_destructor ( type-id )
3033 __is_abstract ( type-id )
3034 __is_base_of ( type-id , type-id )
3035 __is_class ( type-id )
3036 __is_convertible_to ( type-id , type-id )
3037 __is_empty ( type-id )
3038 __is_enum ( type-id )
3039 __is_pod ( type-id )
3040 __is_polymorphic ( type-id )
3041 __is_union ( type-id )
3043 Objective-C++ Extension:
3051 ADDRESS_P is true iff this expression was immediately preceded by
3052 "&" and therefore might denote a pointer-to-member. CAST_P is true
3053 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3054 true iff this expression is a template argument.
3056 Returns a representation of the expression. Upon return, *IDK
3057 indicates what kind of id-expression (if any) was present. */
3060 cp_parser_primary_expression (cp_parser *parser,
3063 bool template_arg_p,
3068 /* Assume the primary expression is not an id-expression. */
3069 *idk = CP_ID_KIND_NONE;
3071 /* Peek at the next token. */
3072 token = cp_lexer_peek_token (parser->lexer);
3073 switch (token->type)
3084 token = cp_lexer_consume_token (parser->lexer);
3085 /* Floating-point literals are only allowed in an integral
3086 constant expression if they are cast to an integral or
3087 enumeration type. */
3088 if (TREE_CODE (token->u.value) == REAL_CST
3089 && parser->integral_constant_expression_p
3092 /* CAST_P will be set even in invalid code like "int(2.7 +
3093 ...)". Therefore, we have to check that the next token
3094 is sure to end the cast. */
3097 cp_token *next_token;
3099 next_token = cp_lexer_peek_token (parser->lexer);
3100 if (/* The comma at the end of an
3101 enumerator-definition. */
3102 next_token->type != CPP_COMMA
3103 /* The curly brace at the end of an enum-specifier. */
3104 && next_token->type != CPP_CLOSE_BRACE
3105 /* The end of a statement. */
3106 && next_token->type != CPP_SEMICOLON
3107 /* The end of the cast-expression. */
3108 && next_token->type != CPP_CLOSE_PAREN
3109 /* The end of an array bound. */
3110 && next_token->type != CPP_CLOSE_SQUARE
3111 /* The closing ">" in a template-argument-list. */
3112 && (next_token->type != CPP_GREATER
3113 || parser->greater_than_is_operator_p)
3114 /* C++0x only: A ">>" treated like two ">" tokens,
3115 in a template-argument-list. */
3116 && (next_token->type != CPP_RSHIFT
3117 || (cxx_dialect == cxx98)
3118 || parser->greater_than_is_operator_p))
3122 /* If we are within a cast, then the constraint that the
3123 cast is to an integral or enumeration type will be
3124 checked at that point. If we are not within a cast, then
3125 this code is invalid. */
3127 cp_parser_non_integral_constant_expression
3128 (parser, "floating-point literal");
3130 return token->u.value;
3134 /* ??? Should wide strings be allowed when parser->translate_strings_p
3135 is false (i.e. in attributes)? If not, we can kill the third
3136 argument to cp_parser_string_literal. */
3137 return cp_parser_string_literal (parser,
3138 parser->translate_strings_p,
3141 case CPP_OPEN_PAREN:
3144 bool saved_greater_than_is_operator_p;
3146 /* Consume the `('. */
3147 cp_lexer_consume_token (parser->lexer);
3148 /* Within a parenthesized expression, a `>' token is always
3149 the greater-than operator. */
3150 saved_greater_than_is_operator_p
3151 = parser->greater_than_is_operator_p;
3152 parser->greater_than_is_operator_p = true;
3153 /* If we see `( { ' then we are looking at the beginning of
3154 a GNU statement-expression. */
3155 if (cp_parser_allow_gnu_extensions_p (parser)
3156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3158 /* Statement-expressions are not allowed by the standard. */
3160 pedwarn ("ISO C++ forbids braced-groups within expressions");
3162 /* And they're not allowed outside of a function-body; you
3163 cannot, for example, write:
3165 int i = ({ int j = 3; j + 1; });
3167 at class or namespace scope. */
3168 if (!parser->in_function_body
3169 || parser->in_template_argument_list_p)
3171 error ("statement-expressions are not allowed outside "
3172 "functions nor in template-argument lists");
3173 cp_parser_skip_to_end_of_block_or_statement (parser);
3174 expr = error_mark_node;
3178 /* Start the statement-expression. */
3179 expr = begin_stmt_expr ();
3180 /* Parse the compound-statement. */
3181 cp_parser_compound_statement (parser, expr, false);
3183 expr = finish_stmt_expr (expr, false);
3188 /* Parse the parenthesized expression. */
3189 expr = cp_parser_expression (parser, cast_p);
3190 /* Let the front end know that this expression was
3191 enclosed in parentheses. This matters in case, for
3192 example, the expression is of the form `A::B', since
3193 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3195 finish_parenthesized_expr (expr);
3197 /* The `>' token might be the end of a template-id or
3198 template-parameter-list now. */
3199 parser->greater_than_is_operator_p
3200 = saved_greater_than_is_operator_p;
3201 /* Consume the `)'. */
3202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203 cp_parser_skip_to_end_of_statement (parser);
3209 switch (token->keyword)
3211 /* These two are the boolean literals. */
3213 cp_lexer_consume_token (parser->lexer);
3214 return boolean_true_node;
3216 cp_lexer_consume_token (parser->lexer);
3217 return boolean_false_node;
3219 /* The `__null' literal. */
3221 cp_lexer_consume_token (parser->lexer);
3224 /* Recognize the `this' keyword. */
3226 cp_lexer_consume_token (parser->lexer);
3227 if (parser->local_variables_forbidden_p)
3229 error ("%<this%> may not be used in this context");
3230 return error_mark_node;
3232 /* Pointers cannot appear in constant-expressions. */
3233 if (cp_parser_non_integral_constant_expression (parser,
3235 return error_mark_node;
3236 return finish_this_expr ();
3238 /* The `operator' keyword can be the beginning of an
3243 case RID_FUNCTION_NAME:
3244 case RID_PRETTY_FUNCTION_NAME:
3245 case RID_C99_FUNCTION_NAME:
3246 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247 __func__ are the names of variables -- but they are
3248 treated specially. Therefore, they are handled here,
3249 rather than relying on the generic id-expression logic
3250 below. Grammatically, these names are id-expressions.
3252 Consume the token. */
3253 token = cp_lexer_consume_token (parser->lexer);
3254 /* Look up the name. */
3255 return finish_fname (token->u.value);
3262 /* The `__builtin_va_arg' construct is used to handle
3263 `va_arg'. Consume the `__builtin_va_arg' token. */
3264 cp_lexer_consume_token (parser->lexer);
3265 /* Look for the opening `('. */
3266 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267 /* Now, parse the assignment-expression. */
3268 expression = cp_parser_assignment_expression (parser,
3270 /* Look for the `,'. */
3271 cp_parser_require (parser, CPP_COMMA, "`,'");
3272 /* Parse the type-id. */
3273 type = cp_parser_type_id (parser);
3274 /* Look for the closing `)'. */
3275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276 /* Using `va_arg' in a constant-expression is not
3278 if (cp_parser_non_integral_constant_expression (parser,
3280 return error_mark_node;
3281 return build_x_va_arg (expression, type);
3285 return cp_parser_builtin_offsetof (parser);
3287 case RID_HAS_NOTHROW_ASSIGN:
3288 case RID_HAS_NOTHROW_CONSTRUCTOR:
3289 case RID_HAS_NOTHROW_COPY:
3290 case RID_HAS_TRIVIAL_ASSIGN:
3291 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292 case RID_HAS_TRIVIAL_COPY:
3293 case RID_HAS_TRIVIAL_DESTRUCTOR:
3294 case RID_HAS_VIRTUAL_DESTRUCTOR:
3295 case RID_IS_ABSTRACT:
3296 case RID_IS_BASE_OF:
3298 case RID_IS_CONVERTIBLE_TO:
3302 case RID_IS_POLYMORPHIC:
3304 return cp_parser_trait_expr (parser, token->keyword);
3306 /* Objective-C++ expressions. */
3308 case RID_AT_PROTOCOL:
3309 case RID_AT_SELECTOR:
3310 return cp_parser_objc_expression (parser);
3313 cp_parser_error (parser, "expected primary-expression");
3314 return error_mark_node;
3317 /* An id-expression can start with either an identifier, a
3318 `::' as the beginning of a qualified-id, or the "operator"
3322 case CPP_TEMPLATE_ID:
3323 case CPP_NESTED_NAME_SPECIFIER:
3327 const char *error_msg;
3332 /* Parse the id-expression. */
3334 = cp_parser_id_expression (parser,
3335 /*template_keyword_p=*/false,
3336 /*check_dependency_p=*/true,
3338 /*declarator_p=*/false,
3339 /*optional_p=*/false);
3340 if (id_expression == error_mark_node)
3341 return error_mark_node;
3342 token = cp_lexer_peek_token (parser->lexer);
3343 done = (token->type != CPP_OPEN_SQUARE
3344 && token->type != CPP_OPEN_PAREN
3345 && token->type != CPP_DOT
3346 && token->type != CPP_DEREF
3347 && token->type != CPP_PLUS_PLUS
3348 && token->type != CPP_MINUS_MINUS);
3349 /* If we have a template-id, then no further lookup is
3350 required. If the template-id was for a template-class, we
3351 will sometimes have a TYPE_DECL at this point. */
3352 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353 || TREE_CODE (id_expression) == TYPE_DECL)
3354 decl = id_expression;
3355 /* Look up the name. */
3358 tree ambiguous_decls;
3360 decl = cp_parser_lookup_name (parser, id_expression,
3363 /*is_namespace=*/false,
3364 /*check_dependency=*/true,
3366 /* If the lookup was ambiguous, an error will already have
3368 if (ambiguous_decls)
3369 return error_mark_node;
3371 /* In Objective-C++, an instance variable (ivar) may be preferred
3372 to whatever cp_parser_lookup_name() found. */
3373 decl = objc_lookup_ivar (decl, id_expression);
3375 /* If name lookup gives us a SCOPE_REF, then the
3376 qualifying scope was dependent. */
3377 if (TREE_CODE (decl) == SCOPE_REF)
3379 /* At this point, we do not know if DECL is a valid
3380 integral constant expression. We assume that it is
3381 in fact such an expression, so that code like:
3383 template <int N> struct A {
3387 is accepted. At template-instantiation time, we
3388 will check that B<N>::i is actually a constant. */
3391 /* Check to see if DECL is a local variable in a context
3392 where that is forbidden. */
3393 if (parser->local_variables_forbidden_p
3394 && local_variable_p (decl))
3396 /* It might be that we only found DECL because we are
3397 trying to be generous with pre-ISO scoping rules.
3398 For example, consider:
3402 for (int i = 0; i < 10; ++i) {}
3403 extern void f(int j = i);
3406 Here, name look up will originally find the out
3407 of scope `i'. We need to issue a warning message,
3408 but then use the global `i'. */
3409 decl = check_for_out_of_scope_variable (decl);
3410 if (local_variable_p (decl))
3412 error ("local variable %qD may not appear in this context",
3414 return error_mark_node;
3419 decl = (finish_id_expression
3420 (id_expression, decl, parser->scope,
3422 parser->integral_constant_expression_p,
3423 parser->allow_non_integral_constant_expression_p,
3424 &parser->non_integral_constant_expression_p,
3425 template_p, done, address_p,
3429 cp_parser_error (parser, error_msg);
3433 /* Anything else is an error. */
3435 /* ...unless we have an Objective-C++ message or string literal,
3437 if (c_dialect_objc ()
3438 && (token->type == CPP_OPEN_SQUARE
3439 || token->type == CPP_OBJC_STRING))
3440 return cp_parser_objc_expression (parser);
3442 cp_parser_error (parser, "expected primary-expression");
3443 return error_mark_node;
3447 /* Parse an id-expression.
3454 :: [opt] nested-name-specifier template [opt] unqualified-id
3456 :: operator-function-id
3459 Return a representation of the unqualified portion of the
3460 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3461 a `::' or nested-name-specifier.
3463 Often, if the id-expression was a qualified-id, the caller will
3464 want to make a SCOPE_REF to represent the qualified-id. This
3465 function does not do this in order to avoid wastefully creating
3466 SCOPE_REFs when they are not required.
3468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3471 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472 uninstantiated templates.
3474 If *TEMPLATE_P is non-NULL, it is set to true iff the
3475 `template' keyword is used to explicitly indicate that the entity
3476 named is a template.
3478 If DECLARATOR_P is true, the id-expression is appearing as part of
3479 a declarator, rather than as part of an expression. */
3482 cp_parser_id_expression (cp_parser *parser,
3483 bool template_keyword_p,
3484 bool check_dependency_p,
3489 bool global_scope_p;
3490 bool nested_name_specifier_p;
3492 /* Assume the `template' keyword was not used. */
3494 *template_p = template_keyword_p;
3496 /* Look for the optional `::' operator. */
3498 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3500 /* Look for the optional nested-name-specifier. */
3501 nested_name_specifier_p
3502 = (cp_parser_nested_name_specifier_opt (parser,
3503 /*typename_keyword_p=*/false,
3508 /* If there is a nested-name-specifier, then we are looking at
3509 the first qualified-id production. */
3510 if (nested_name_specifier_p)
3513 tree saved_object_scope;
3514 tree saved_qualifying_scope;
3515 tree unqualified_id;
3518 /* See if the next token is the `template' keyword. */
3520 template_p = &is_template;
3521 *template_p = cp_parser_optional_template_keyword (parser);
3522 /* Name lookup we do during the processing of the
3523 unqualified-id might obliterate SCOPE. */
3524 saved_scope = parser->scope;
3525 saved_object_scope = parser->object_scope;
3526 saved_qualifying_scope = parser->qualifying_scope;
3527 /* Process the final unqualified-id. */
3528 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3531 /*optional_p=*/false);
3532 /* Restore the SAVED_SCOPE for our caller. */
3533 parser->scope = saved_scope;
3534 parser->object_scope = saved_object_scope;
3535 parser->qualifying_scope = saved_qualifying_scope;
3537 return unqualified_id;
3539 /* Otherwise, if we are in global scope, then we are looking at one
3540 of the other qualified-id productions. */
3541 else if (global_scope_p)
3546 /* Peek at the next token. */
3547 token = cp_lexer_peek_token (parser->lexer);
3549 /* If it's an identifier, and the next token is not a "<", then
3550 we can avoid the template-id case. This is an optimization
3551 for this common case. */
3552 if (token->type == CPP_NAME
3553 && !cp_parser_nth_token_starts_template_argument_list_p
3555 return cp_parser_identifier (parser);
3557 cp_parser_parse_tentatively (parser);
3558 /* Try a template-id. */
3559 id = cp_parser_template_id (parser,
3560 /*template_keyword_p=*/false,
3561 /*check_dependency_p=*/true,
3563 /* If that worked, we're done. */
3564 if (cp_parser_parse_definitely (parser))
3567 /* Peek at the next token. (Changes in the token buffer may
3568 have invalidated the pointer obtained above.) */
3569 token = cp_lexer_peek_token (parser->lexer);
3571 switch (token->type)
3574 return cp_parser_identifier (parser);
3577 if (token->keyword == RID_OPERATOR)
3578 return cp_parser_operator_function_id (parser);
3582 cp_parser_error (parser, "expected id-expression");
3583 return error_mark_node;
3587 return cp_parser_unqualified_id (parser, template_keyword_p,
3588 /*check_dependency_p=*/true,
3593 /* Parse an unqualified-id.
3597 operator-function-id
3598 conversion-function-id
3602 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603 keyword, in a construct like `A::template ...'.
3605 Returns a representation of unqualified-id. For the `identifier'
3606 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3607 production a BIT_NOT_EXPR is returned; the operand of the
3608 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3609 other productions, see the documentation accompanying the
3610 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3611 names are looked up in uninstantiated templates. If DECLARATOR_P
3612 is true, the unqualified-id is appearing as part of a declarator,
3613 rather than as part of an expression. */
3616 cp_parser_unqualified_id (cp_parser* parser,
3617 bool template_keyword_p,
3618 bool check_dependency_p,
3624 /* Peek at the next token. */
3625 token = cp_lexer_peek_token (parser->lexer);
3627 switch (token->type)
3633 /* We don't know yet whether or not this will be a
3635 cp_parser_parse_tentatively (parser);
3636 /* Try a template-id. */
3637 id = cp_parser_template_id (parser, template_keyword_p,
3640 /* If it worked, we're done. */
3641 if (cp_parser_parse_definitely (parser))
3643 /* Otherwise, it's an ordinary identifier. */
3644 return cp_parser_identifier (parser);
3647 case CPP_TEMPLATE_ID:
3648 return cp_parser_template_id (parser, template_keyword_p,
3655 tree qualifying_scope;
3660 /* Consume the `~' token. */
3661 cp_lexer_consume_token (parser->lexer);
3662 /* Parse the class-name. The standard, as written, seems to
3665 template <typename T> struct S { ~S (); };
3666 template <typename T> S<T>::~S() {}
3668 is invalid, since `~' must be followed by a class-name, but
3669 `S<T>' is dependent, and so not known to be a class.
3670 That's not right; we need to look in uninstantiated
3671 templates. A further complication arises from:
3673 template <typename T> void f(T t) {
3677 Here, it is not possible to look up `T' in the scope of `T'
3678 itself. We must look in both the current scope, and the
3679 scope of the containing complete expression.
3681 Yet another issue is:
3690 The standard does not seem to say that the `S' in `~S'
3691 should refer to the type `S' and not the data member
3694 /* DR 244 says that we look up the name after the "~" in the
3695 same scope as we looked up the qualifying name. That idea
3696 isn't fully worked out; it's more complicated than that. */
3697 scope = parser->scope;
3698 object_scope = parser->object_scope;
3699 qualifying_scope = parser->qualifying_scope;
3701 /* Check for invalid scopes. */
3702 if (scope == error_mark_node)
3704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705 cp_lexer_consume_token (parser->lexer);
3706 return error_mark_node;
3708 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3710 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711 error ("scope %qT before %<~%> is not a class-name", scope);
3712 cp_parser_simulate_error (parser);
3713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714 cp_lexer_consume_token (parser->lexer);
3715 return error_mark_node;
3717 gcc_assert (!scope || TYPE_P (scope));
3719 /* If the name is of the form "X::~X" it's OK. */
3720 token = cp_lexer_peek_token (parser->lexer);
3722 && token->type == CPP_NAME
3723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3725 && constructor_name_p (token->u.value, scope))
3727 cp_lexer_consume_token (parser->lexer);
3728 return build_nt (BIT_NOT_EXPR, scope);
3731 /* If there was an explicit qualification (S::~T), first look
3732 in the scope given by the qualification (i.e., S). */
3734 type_decl = NULL_TREE;
3737 cp_parser_parse_tentatively (parser);
3738 type_decl = cp_parser_class_name (parser,
3739 /*typename_keyword_p=*/false,
3740 /*template_keyword_p=*/false,
3742 /*check_dependency=*/false,
3743 /*class_head_p=*/false,
3745 if (cp_parser_parse_definitely (parser))
3748 /* In "N::S::~S", look in "N" as well. */
3749 if (!done && scope && qualifying_scope)
3751 cp_parser_parse_tentatively (parser);
3752 parser->scope = qualifying_scope;
3753 parser->object_scope = NULL_TREE;
3754 parser->qualifying_scope = NULL_TREE;
3756 = cp_parser_class_name (parser,
3757 /*typename_keyword_p=*/false,
3758 /*template_keyword_p=*/false,
3760 /*check_dependency=*/false,
3761 /*class_head_p=*/false,
3763 if (cp_parser_parse_definitely (parser))
3766 /* In "p->S::~T", look in the scope given by "*p" as well. */
3767 else if (!done && object_scope)
3769 cp_parser_parse_tentatively (parser);
3770 parser->scope = object_scope;
3771 parser->object_scope = NULL_TREE;
3772 parser->qualifying_scope = NULL_TREE;
3774 = cp_parser_class_name (parser,
3775 /*typename_keyword_p=*/false,
3776 /*template_keyword_p=*/false,
3778 /*check_dependency=*/false,
3779 /*class_head_p=*/false,
3781 if (cp_parser_parse_definitely (parser))
3784 /* Look in the surrounding context. */
3787 parser->scope = NULL_TREE;
3788 parser->object_scope = NULL_TREE;
3789 parser->qualifying_scope = NULL_TREE;
3791 = cp_parser_class_name (parser,
3792 /*typename_keyword_p=*/false,
3793 /*template_keyword_p=*/false,
3795 /*check_dependency=*/false,
3796 /*class_head_p=*/false,
3799 /* If an error occurred, assume that the name of the
3800 destructor is the same as the name of the qualifying
3801 class. That allows us to keep parsing after running
3802 into ill-formed destructor names. */
3803 if (type_decl == error_mark_node && scope)
3804 return build_nt (BIT_NOT_EXPR, scope);
3805 else if (type_decl == error_mark_node)
3806 return error_mark_node;
3808 /* Check that destructor name and scope match. */
3809 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3811 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812 error ("declaration of %<~%T%> as member of %qT",
3814 cp_parser_simulate_error (parser);
3815 return error_mark_node;
3820 A typedef-name that names a class shall not be used as the
3821 identifier in the declarator for a destructor declaration. */
3823 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824 && !DECL_SELF_REFERENCE_P (type_decl)
3825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826 error ("typedef-name %qD used as destructor declarator",
3829 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3833 if (token->keyword == RID_OPERATOR)
3837 /* This could be a template-id, so we try that first. */
3838 cp_parser_parse_tentatively (parser);
3839 /* Try a template-id. */
3840 id = cp_parser_template_id (parser, template_keyword_p,
3841 /*check_dependency_p=*/true,
3843 /* If that worked, we're done. */
3844 if (cp_parser_parse_definitely (parser))
3846 /* We still don't know whether we're looking at an
3847 operator-function-id or a conversion-function-id. */
3848 cp_parser_parse_tentatively (parser);
3849 /* Try an operator-function-id. */
3850 id = cp_parser_operator_function_id (parser);
3851 /* If that didn't work, try a conversion-function-id. */
3852 if (!cp_parser_parse_definitely (parser))
3853 id = cp_parser_conversion_function_id (parser);
3862 cp_parser_error (parser, "expected unqualified-id");
3863 return error_mark_node;
3867 /* Parse an (optional) nested-name-specifier.
3869 nested-name-specifier:
3870 class-or-namespace-name :: nested-name-specifier [opt]
3871 class-or-namespace-name :: template nested-name-specifier [opt]
3873 PARSER->SCOPE should be set appropriately before this function is
3874 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3878 Sets PARSER->SCOPE to the class (TYPE) or namespace
3879 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880 it unchanged if there is no nested-name-specifier. Returns the new
3881 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3883 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884 part of a declaration and/or decl-specifier. */
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888 bool typename_keyword_p,
3889 bool check_dependency_p,
3891 bool is_declaration)
3893 bool success = false;
3894 cp_token_position start = 0;
3897 /* Remember where the nested-name-specifier starts. */
3898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3900 start = cp_lexer_token_position (parser->lexer, false);
3901 push_deferring_access_checks (dk_deferred);
3908 tree saved_qualifying_scope;
3909 bool template_keyword_p;
3911 /* Spot cases that cannot be the beginning of a
3912 nested-name-specifier. */
3913 token = cp_lexer_peek_token (parser->lexer);
3915 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916 the already parsed nested-name-specifier. */
3917 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3919 /* Grab the nested-name-specifier and continue the loop. */
3920 cp_parser_pre_parsed_nested_name_specifier (parser);
3921 /* If we originally encountered this nested-name-specifier
3922 with IS_DECLARATION set to false, we will not have
3923 resolved TYPENAME_TYPEs, so we must do so here. */
3925 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3927 new_scope = resolve_typename_type (parser->scope,
3928 /*only_current_p=*/false);
3929 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930 parser->scope = new_scope;
3936 /* Spot cases that cannot be the beginning of a
3937 nested-name-specifier. On the second and subsequent times
3938 through the loop, we look for the `template' keyword. */
3939 if (success && token->keyword == RID_TEMPLATE)
3941 /* A template-id can start a nested-name-specifier. */
3942 else if (token->type == CPP_TEMPLATE_ID)
3946 /* If the next token is not an identifier, then it is
3947 definitely not a class-or-namespace-name. */
3948 if (token->type != CPP_NAME)
3950 /* If the following token is neither a `<' (to begin a
3951 template-id), nor a `::', then we are not looking at a
3952 nested-name-specifier. */
3953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954 if (token->type != CPP_SCOPE
3955 && !cp_parser_nth_token_starts_template_argument_list_p
3960 /* The nested-name-specifier is optional, so we parse
3962 cp_parser_parse_tentatively (parser);
3964 /* Look for the optional `template' keyword, if this isn't the
3965 first time through the loop. */
3967 template_keyword_p = cp_parser_optional_template_keyword (parser);
3969 template_keyword_p = false;
3971 /* Save the old scope since the name lookup we are about to do
3972 might destroy it. */
3973 old_scope = parser->scope;
3974 saved_qualifying_scope = parser->qualifying_scope;
3975 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976 look up names in "X<T>::I" in order to determine that "Y" is
3977 a template. So, if we have a typename at this point, we make
3978 an effort to look through it. */
3980 && !typename_keyword_p
3982 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983 parser->scope = resolve_typename_type (parser->scope,
3984 /*only_current_p=*/false);
3985 /* Parse the qualifying entity. */
3987 = cp_parser_class_or_namespace_name (parser,
3993 /* Look for the `::' token. */
3994 cp_parser_require (parser, CPP_SCOPE, "`::'");
3996 /* If we found what we wanted, we keep going; otherwise, we're
3998 if (!cp_parser_parse_definitely (parser))
4000 bool error_p = false;
4002 /* Restore the OLD_SCOPE since it was valid before the
4003 failed attempt at finding the last
4004 class-or-namespace-name. */
4005 parser->scope = old_scope;
4006 parser->qualifying_scope = saved_qualifying_scope;
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4009 /* If the next token is an identifier, and the one after
4010 that is a `::', then any valid interpretation would have
4011 found a class-or-namespace-name. */
4012 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4018 token = cp_lexer_consume_token (parser->lexer);
4021 if (!token->ambiguous_p)
4024 tree ambiguous_decls;
4026 decl = cp_parser_lookup_name (parser, token->u.value,
4028 /*is_template=*/false,
4029 /*is_namespace=*/false,
4030 /*check_dependency=*/true,
4032 if (TREE_CODE (decl) == TEMPLATE_DECL)
4033 error ("%qD used without template parameters", decl);
4034 else if (ambiguous_decls)
4036 error ("reference to %qD is ambiguous",
4038 print_candidates (ambiguous_decls);
4039 decl = error_mark_node;
4042 cp_parser_name_lookup_error
4043 (parser, token->u.value, decl,
4044 "is not a class or namespace");
4046 parser->scope = error_mark_node;
4048 /* Treat this as a successful nested-name-specifier
4053 If the name found is not a class-name (clause
4054 _class_) or namespace-name (_namespace.def_), the
4055 program is ill-formed. */
4058 cp_lexer_consume_token (parser->lexer);
4062 /* We've found one valid nested-name-specifier. */
4064 /* Name lookup always gives us a DECL. */
4065 if (TREE_CODE (new_scope) == TYPE_DECL)
4066 new_scope = TREE_TYPE (new_scope);
4067 /* Uses of "template" must be followed by actual templates. */
4068 if (template_keyword_p
4069 && !(CLASS_TYPE_P (new_scope)
4070 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075 == TEMPLATE_ID_EXPR)))
4076 pedwarn (TYPE_P (new_scope)
4077 ? "%qT is not a template"
4078 : "%qD is not a template",
4080 /* If it is a class scope, try to complete it; we are about to
4081 be looking up names inside the class. */
4082 if (TYPE_P (new_scope)
4083 /* Since checking types for dependency can be expensive,
4084 avoid doing it if the type is already complete. */
4085 && !COMPLETE_TYPE_P (new_scope)
4086 /* Do not try to complete dependent types. */
4087 && !dependent_type_p (new_scope))
4089 new_scope = complete_type (new_scope);
4090 /* If it is a typedef to current class, use the current
4091 class instead, as the typedef won't have any names inside
4093 if (!COMPLETE_TYPE_P (new_scope)
4094 && currently_open_class (new_scope))
4095 new_scope = TYPE_MAIN_VARIANT (new_scope);
4097 /* Make sure we look in the right scope the next time through
4099 parser->scope = new_scope;
4102 /* If parsing tentatively, replace the sequence of tokens that makes
4103 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4104 token. That way, should we re-parse the token stream, we will
4105 not have to repeat the effort required to do the parse, nor will
4106 we issue duplicate error messages. */
4107 if (success && start)
4111 token = cp_lexer_token_at (parser->lexer, start);
4112 /* Reset the contents of the START token. */
4113 token->type = CPP_NESTED_NAME_SPECIFIER;
4114 /* Retrieve any deferred checks. Do not pop this access checks yet
4115 so the memory will not be reclaimed during token replacing below. */
4116 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4117 token->u.tree_check_value->value = parser->scope;
4118 token->u.tree_check_value->checks = get_deferred_access_checks ();
4119 token->u.tree_check_value->qualifying_scope =
4120 parser->qualifying_scope;
4121 token->keyword = RID_MAX;
4123 /* Purge all subsequent tokens. */
4124 cp_lexer_purge_tokens_after (parser->lexer, start);
4128 pop_to_parent_deferring_access_checks ();
4130 return success ? parser->scope : NULL_TREE;
4133 /* Parse a nested-name-specifier. See
4134 cp_parser_nested_name_specifier_opt for details. This function
4135 behaves identically, except that it will an issue an error if no
4136 nested-name-specifier is present. */
4139 cp_parser_nested_name_specifier (cp_parser *parser,
4140 bool typename_keyword_p,
4141 bool check_dependency_p,
4143 bool is_declaration)
4147 /* Look for the nested-name-specifier. */
4148 scope = cp_parser_nested_name_specifier_opt (parser,
4153 /* If it was not present, issue an error message. */
4156 cp_parser_error (parser, "expected nested-name-specifier");
4157 parser->scope = NULL_TREE;
4163 /* Parse a class-or-namespace-name.
4165 class-or-namespace-name:
4169 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4170 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4171 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4172 TYPE_P is TRUE iff the next name should be taken as a class-name,
4173 even the same name is declared to be another entity in the same
4176 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4177 specified by the class-or-namespace-name. If neither is found the
4178 ERROR_MARK_NODE is returned. */
4181 cp_parser_class_or_namespace_name (cp_parser *parser,
4182 bool typename_keyword_p,
4183 bool template_keyword_p,
4184 bool check_dependency_p,
4186 bool is_declaration)
4189 tree saved_qualifying_scope;
4190 tree saved_object_scope;
4194 /* Before we try to parse the class-name, we must save away the
4195 current PARSER->SCOPE since cp_parser_class_name will destroy
4197 saved_scope = parser->scope;
4198 saved_qualifying_scope = parser->qualifying_scope;
4199 saved_object_scope = parser->object_scope;
4200 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4201 there is no need to look for a namespace-name. */
4202 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4204 cp_parser_parse_tentatively (parser);
4205 scope = cp_parser_class_name (parser,
4208 type_p ? class_type : none_type,
4210 /*class_head_p=*/false,
4212 /* If that didn't work, try for a namespace-name. */
4213 if (!only_class_p && !cp_parser_parse_definitely (parser))
4215 /* Restore the saved scope. */
4216 parser->scope = saved_scope;
4217 parser->qualifying_scope = saved_qualifying_scope;
4218 parser->object_scope = saved_object_scope;
4219 /* If we are not looking at an identifier followed by the scope
4220 resolution operator, then this is not part of a
4221 nested-name-specifier. (Note that this function is only used
4222 to parse the components of a nested-name-specifier.) */
4223 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4224 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4225 return error_mark_node;
4226 scope = cp_parser_namespace_name (parser);
4232 /* Parse a postfix-expression.
4236 postfix-expression [ expression ]
4237 postfix-expression ( expression-list [opt] )
4238 simple-type-specifier ( expression-list [opt] )
4239 typename :: [opt] nested-name-specifier identifier
4240 ( expression-list [opt] )
4241 typename :: [opt] nested-name-specifier template [opt] template-id
4242 ( expression-list [opt] )
4243 postfix-expression . template [opt] id-expression
4244 postfix-expression -> template [opt] id-expression
4245 postfix-expression . pseudo-destructor-name
4246 postfix-expression -> pseudo-destructor-name
4247 postfix-expression ++
4248 postfix-expression --
4249 dynamic_cast < type-id > ( expression )
4250 static_cast < type-id > ( expression )
4251 reinterpret_cast < type-id > ( expression )
4252 const_cast < type-id > ( expression )
4253 typeid ( expression )
4259 ( type-id ) { initializer-list , [opt] }
4261 This extension is a GNU version of the C99 compound-literal
4262 construct. (The C99 grammar uses `type-name' instead of `type-id',
4263 but they are essentially the same concept.)
4265 If ADDRESS_P is true, the postfix expression is the operand of the
4266 `&' operator. CAST_P is true if this expression is the target of a
4269 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4270 class member access expressions [expr.ref].
4272 Returns a representation of the expression. */
4275 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4276 bool member_access_only_p)
4280 cp_id_kind idk = CP_ID_KIND_NONE;
4281 tree postfix_expression = NULL_TREE;
4282 bool is_member_access = false;
4284 /* Peek at the next token. */
4285 token = cp_lexer_peek_token (parser->lexer);
4286 /* Some of the productions are determined by keywords. */
4287 keyword = token->keyword;
4297 const char *saved_message;
4299 /* All of these can be handled in the same way from the point
4300 of view of parsing. Begin by consuming the token
4301 identifying the cast. */
4302 cp_lexer_consume_token (parser->lexer);
4304 /* New types cannot be defined in the cast. */
4305 saved_message = parser->type_definition_forbidden_message;
4306 parser->type_definition_forbidden_message
4307 = "types may not be defined in casts";
4309 /* Look for the opening `<'. */
4310 cp_parser_require (parser, CPP_LESS, "`<'");
4311 /* Parse the type to which we are casting. */
4312 type = cp_parser_type_id (parser);
4313 /* Look for the closing `>'. */
4314 cp_parser_require (parser, CPP_GREATER, "`>'");
4315 /* Restore the old message. */
4316 parser->type_definition_forbidden_message = saved_message;
4318 /* And the expression which is being cast. */
4319 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4320 expression = cp_parser_expression (parser, /*cast_p=*/true);
4321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4323 /* Only type conversions to integral or enumeration types
4324 can be used in constant-expressions. */
4325 if (!cast_valid_in_integral_constant_expression_p (type)
4326 && (cp_parser_non_integral_constant_expression
4328 "a cast to a type other than an integral or "
4329 "enumeration type")))
4330 return error_mark_node;
4336 = build_dynamic_cast (type, expression);
4340 = build_static_cast (type, expression);
4344 = build_reinterpret_cast (type, expression);
4348 = build_const_cast (type, expression);
4359 const char *saved_message;
4360 bool saved_in_type_id_in_expr_p;
4362 /* Consume the `typeid' token. */
4363 cp_lexer_consume_token (parser->lexer);
4364 /* Look for the `(' token. */
4365 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4366 /* Types cannot be defined in a `typeid' expression. */
4367 saved_message = parser->type_definition_forbidden_message;
4368 parser->type_definition_forbidden_message
4369 = "types may not be defined in a `typeid\' expression";
4370 /* We can't be sure yet whether we're looking at a type-id or an
4372 cp_parser_parse_tentatively (parser);
4373 /* Try a type-id first. */
4374 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4375 parser->in_type_id_in_expr_p = true;
4376 type = cp_parser_type_id (parser);
4377 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4378 /* Look for the `)' token. Otherwise, we can't be sure that
4379 we're not looking at an expression: consider `typeid (int
4380 (3))', for example. */
4381 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4382 /* If all went well, simply lookup the type-id. */
4383 if (cp_parser_parse_definitely (parser))
4384 postfix_expression = get_typeid (type);
4385 /* Otherwise, fall back to the expression variant. */
4390 /* Look for an expression. */
4391 expression = cp_parser_expression (parser, /*cast_p=*/false);
4392 /* Compute its typeid. */
4393 postfix_expression = build_typeid (expression);
4394 /* Look for the `)' token. */
4395 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4397 /* Restore the saved message. */
4398 parser->type_definition_forbidden_message = saved_message;
4399 /* `typeid' may not appear in an integral constant expression. */
4400 if (cp_parser_non_integral_constant_expression(parser,
4401 "`typeid' operator"))
4402 return error_mark_node;
4409 /* The syntax permitted here is the same permitted for an
4410 elaborated-type-specifier. */
4411 type = cp_parser_elaborated_type_specifier (parser,
4412 /*is_friend=*/false,
4413 /*is_declaration=*/false);
4414 postfix_expression = cp_parser_functional_cast (parser, type);
4422 /* If the next thing is a simple-type-specifier, we may be
4423 looking at a functional cast. We could also be looking at
4424 an id-expression. So, we try the functional cast, and if
4425 that doesn't work we fall back to the primary-expression. */
4426 cp_parser_parse_tentatively (parser);
4427 /* Look for the simple-type-specifier. */
4428 type = cp_parser_simple_type_specifier (parser,
4429 /*decl_specs=*/NULL,
4430 CP_PARSER_FLAGS_NONE);
4431 /* Parse the cast itself. */
4432 if (!cp_parser_error_occurred (parser))
4434 = cp_parser_functional_cast (parser, type);
4435 /* If that worked, we're done. */
4436 if (cp_parser_parse_definitely (parser))
4439 /* If the functional-cast didn't work out, try a
4440 compound-literal. */
4441 if (cp_parser_allow_gnu_extensions_p (parser)
4442 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4444 VEC(constructor_elt,gc) *initializer_list = NULL;
4445 bool saved_in_type_id_in_expr_p;
4447 cp_parser_parse_tentatively (parser);
4448 /* Consume the `('. */
4449 cp_lexer_consume_token (parser->lexer);
4450 /* Parse the type. */
4451 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4452 parser->in_type_id_in_expr_p = true;
4453 type = cp_parser_type_id (parser);
4454 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4455 /* Look for the `)'. */
4456 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4457 /* Look for the `{'. */
4458 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4459 /* If things aren't going well, there's no need to
4461 if (!cp_parser_error_occurred (parser))
4463 bool non_constant_p;
4464 /* Parse the initializer-list. */
4466 = cp_parser_initializer_list (parser, &non_constant_p);
4467 /* Allow a trailing `,'. */
4468 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4469 cp_lexer_consume_token (parser->lexer);
4470 /* Look for the final `}'. */
4471 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4473 /* If that worked, we're definitely looking at a
4474 compound-literal expression. */
4475 if (cp_parser_parse_definitely (parser))
4477 /* Warn the user that a compound literal is not
4478 allowed in standard C++. */
4480 pedwarn ("ISO C++ forbids compound-literals");
4481 /* For simplicity, we disallow compound literals in
4482 constant-expressions. We could
4483 allow compound literals of integer type, whose
4484 initializer was a constant, in constant
4485 expressions. Permitting that usage, as a further
4486 extension, would not change the meaning of any
4487 currently accepted programs. (Of course, as
4488 compound literals are not part of ISO C++, the
4489 standard has nothing to say.) */
4490 if (cp_parser_non_integral_constant_expression
4491 (parser, "non-constant compound literals"))
4493 postfix_expression = error_mark_node;
4496 /* Form the representation of the compound-literal. */
4498 = finish_compound_literal (type, initializer_list);
4503 /* It must be a primary-expression. */
4505 = cp_parser_primary_expression (parser, address_p, cast_p,
4506 /*template_arg_p=*/false,
4512 /* Keep looping until the postfix-expression is complete. */
4515 if (idk == CP_ID_KIND_UNQUALIFIED
4516 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4517 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4518 /* It is not a Koenig lookup function call. */
4520 = unqualified_name_lookup_error (postfix_expression);
4522 /* Peek at the next token. */
4523 token = cp_lexer_peek_token (parser->lexer);
4525 switch (token->type)
4527 case CPP_OPEN_SQUARE:
4529 = cp_parser_postfix_open_square_expression (parser,
4532 idk = CP_ID_KIND_NONE;
4533 is_member_access = false;
4536 case CPP_OPEN_PAREN:
4537 /* postfix-expression ( expression-list [opt] ) */
4540 bool is_builtin_constant_p;
4541 bool saved_integral_constant_expression_p = false;
4542 bool saved_non_integral_constant_expression_p = false;
4545 is_member_access = false;
4547 is_builtin_constant_p
4548 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4549 if (is_builtin_constant_p)
4551 /* The whole point of __builtin_constant_p is to allow
4552 non-constant expressions to appear as arguments. */
4553 saved_integral_constant_expression_p
4554 = parser->integral_constant_expression_p;
4555 saved_non_integral_constant_expression_p
4556 = parser->non_integral_constant_expression_p;
4557 parser->integral_constant_expression_p = false;
4559 args = (cp_parser_parenthesized_expression_list
4560 (parser, /*is_attribute_list=*/false,
4561 /*cast_p=*/false, /*allow_expansion_p=*/true,
4562 /*non_constant_p=*/NULL));
4563 if (is_builtin_constant_p)
4565 parser->integral_constant_expression_p
4566 = saved_integral_constant_expression_p;
4567 parser->non_integral_constant_expression_p
4568 = saved_non_integral_constant_expression_p;
4571 if (args == error_mark_node)
4573 postfix_expression = error_mark_node;
4577 /* Function calls are not permitted in
4578 constant-expressions. */
4579 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4580 && cp_parser_non_integral_constant_expression (parser,
4583 postfix_expression = error_mark_node;
4588 if (idk == CP_ID_KIND_UNQUALIFIED)
4590 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4596 = perform_koenig_lookup (postfix_expression, args);
4600 = unqualified_fn_lookup_error (postfix_expression);
4602 /* We do not perform argument-dependent lookup if
4603 normal lookup finds a non-function, in accordance
4604 with the expected resolution of DR 218. */
4605 else if (args && is_overloaded_fn (postfix_expression))
4607 tree fn = get_first_fn (postfix_expression);
4609 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4610 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4612 /* Only do argument dependent lookup if regular
4613 lookup does not find a set of member functions.
4614 [basic.lookup.koenig]/2a */
4615 if (!DECL_FUNCTION_MEMBER_P (fn))
4619 = perform_koenig_lookup (postfix_expression, args);
4624 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4626 tree instance = TREE_OPERAND (postfix_expression, 0);
4627 tree fn = TREE_OPERAND (postfix_expression, 1);
4629 if (processing_template_decl
4630 && (type_dependent_expression_p (instance)
4631 || (!BASELINK_P (fn)
4632 && TREE_CODE (fn) != FIELD_DECL)
4633 || type_dependent_expression_p (fn)
4634 || any_type_dependent_arguments_p (args)))
4637 = build_nt_call_list (postfix_expression, args);
4641 if (BASELINK_P (fn))
4643 = (build_new_method_call
4644 (instance, fn, args, NULL_TREE,
4645 (idk == CP_ID_KIND_QUALIFIED
4646 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4650 = finish_call_expr (postfix_expression, args,
4651 /*disallow_virtual=*/false,
4652 /*koenig_p=*/false);
4654 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4655 || TREE_CODE (postfix_expression) == MEMBER_REF
4656 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4657 postfix_expression = (build_offset_ref_call_from_tree
4658 (postfix_expression, args));
4659 else if (idk == CP_ID_KIND_QUALIFIED)
4660 /* A call to a static class member, or a namespace-scope
4663 = finish_call_expr (postfix_expression, args,
4664 /*disallow_virtual=*/true,
4667 /* All other function calls. */
4669 = finish_call_expr (postfix_expression, args,
4670 /*disallow_virtual=*/false,
4673 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4674 idk = CP_ID_KIND_NONE;
4680 /* postfix-expression . template [opt] id-expression
4681 postfix-expression . pseudo-destructor-name
4682 postfix-expression -> template [opt] id-expression
4683 postfix-expression -> pseudo-destructor-name */
4685 /* Consume the `.' or `->' operator. */
4686 cp_lexer_consume_token (parser->lexer);
4689 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4693 is_member_access = true;
4697 /* postfix-expression ++ */
4698 /* Consume the `++' token. */
4699 cp_lexer_consume_token (parser->lexer);
4700 /* Generate a representation for the complete expression. */
4702 = finish_increment_expr (postfix_expression,
4703 POSTINCREMENT_EXPR);
4704 /* Increments may not appear in constant-expressions. */
4705 if (cp_parser_non_integral_constant_expression (parser,
4707 postfix_expression = error_mark_node;
4708 idk = CP_ID_KIND_NONE;
4709 is_member_access = false;
4712 case CPP_MINUS_MINUS:
4713 /* postfix-expression -- */
4714 /* Consume the `--' token. */
4715 cp_lexer_consume_token (parser->lexer);
4716 /* Generate a representation for the complete expression. */
4718 = finish_increment_expr (postfix_expression,
4719 POSTDECREMENT_EXPR);
4720 /* Decrements may not appear in constant-expressions. */
4721 if (cp_parser_non_integral_constant_expression (parser,
4723 postfix_expression = error_mark_node;
4724 idk = CP_ID_KIND_NONE;
4725 is_member_access = false;
4729 if (member_access_only_p)
4730 return is_member_access? postfix_expression : error_mark_node;
4732 return postfix_expression;
4736 /* We should never get here. */
4738 return error_mark_node;
4741 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4742 by cp_parser_builtin_offsetof. We're looking for
4744 postfix-expression [ expression ]
4746 FOR_OFFSETOF is set if we're being called in that context, which
4747 changes how we deal with integer constant expressions. */
4750 cp_parser_postfix_open_square_expression (cp_parser *parser,
4751 tree postfix_expression,
4756 /* Consume the `[' token. */
4757 cp_lexer_consume_token (parser->lexer);
4759 /* Parse the index expression. */
4760 /* ??? For offsetof, there is a question of what to allow here. If
4761 offsetof is not being used in an integral constant expression context,
4762 then we *could* get the right answer by computing the value at runtime.
4763 If we are in an integral constant expression context, then we might
4764 could accept any constant expression; hard to say without analysis.
4765 Rather than open the barn door too wide right away, allow only integer
4766 constant expressions here. */
4768 index = cp_parser_constant_expression (parser, false, NULL);
4770 index = cp_parser_expression (parser, /*cast_p=*/false);
4772 /* Look for the closing `]'. */
4773 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4775 /* Build the ARRAY_REF. */
4776 postfix_expression = grok_array_decl (postfix_expression, index);
4778 /* When not doing offsetof, array references are not permitted in
4779 constant-expressions. */
4781 && (cp_parser_non_integral_constant_expression
4782 (parser, "an array reference")))
4783 postfix_expression = error_mark_node;
4785 return postfix_expression;
4788 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4789 by cp_parser_builtin_offsetof. We're looking for
4791 postfix-expression . template [opt] id-expression
4792 postfix-expression . pseudo-destructor-name
4793 postfix-expression -> template [opt] id-expression
4794 postfix-expression -> pseudo-destructor-name
4796 FOR_OFFSETOF is set if we're being called in that context. That sorta
4797 limits what of the above we'll actually accept, but nevermind.
4798 TOKEN_TYPE is the "." or "->" token, which will already have been
4799 removed from the stream. */
4802 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4803 enum cpp_ttype token_type,
4804 tree postfix_expression,
4805 bool for_offsetof, cp_id_kind *idk)
4809 bool pseudo_destructor_p;
4810 tree scope = NULL_TREE;
4812 /* If this is a `->' operator, dereference the pointer. */
4813 if (token_type == CPP_DEREF)
4814 postfix_expression = build_x_arrow (postfix_expression);
4815 /* Check to see whether or not the expression is type-dependent. */
4816 dependent_p = type_dependent_expression_p (postfix_expression);
4817 /* The identifier following the `->' or `.' is not qualified. */
4818 parser->scope = NULL_TREE;
4819 parser->qualifying_scope = NULL_TREE;
4820 parser->object_scope = NULL_TREE;
4821 *idk = CP_ID_KIND_NONE;
4822 /* Enter the scope corresponding to the type of the object
4823 given by the POSTFIX_EXPRESSION. */
4824 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4826 scope = TREE_TYPE (postfix_expression);
4827 /* According to the standard, no expression should ever have
4828 reference type. Unfortunately, we do not currently match
4829 the standard in this respect in that our internal representation
4830 of an expression may have reference type even when the standard
4831 says it does not. Therefore, we have to manually obtain the
4832 underlying type here. */
4833 scope = non_reference (scope);
4834 /* The type of the POSTFIX_EXPRESSION must be complete. */
4835 if (scope == unknown_type_node)
4837 error ("%qE does not have class type", postfix_expression);
4841 scope = complete_type_or_else (scope, NULL_TREE);
4842 /* Let the name lookup machinery know that we are processing a
4843 class member access expression. */
4844 parser->context->object_type = scope;
4845 /* If something went wrong, we want to be able to discern that case,
4846 as opposed to the case where there was no SCOPE due to the type
4847 of expression being dependent. */
4849 scope = error_mark_node;
4850 /* If the SCOPE was erroneous, make the various semantic analysis
4851 functions exit quickly -- and without issuing additional error
4853 if (scope == error_mark_node)
4854 postfix_expression = error_mark_node;
4857 /* Assume this expression is not a pseudo-destructor access. */
4858 pseudo_destructor_p = false;
4860 /* If the SCOPE is a scalar type, then, if this is a valid program,
4861 we must be looking at a pseudo-destructor-name. If POSTFIX_EXPRESSION
4862 is type dependent, it can be pseudo-destructor-name or something else.
4863 Try to parse it as pseudo-destructor-name first. */
4864 if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
4869 cp_parser_parse_tentatively (parser);
4870 /* Parse the pseudo-destructor-name. */
4872 cp_parser_pseudo_destructor_name (parser, &s, &type);
4874 && (cp_parser_error_occurred (parser)
4875 || TREE_CODE (type) != TYPE_DECL
4876 || !SCALAR_TYPE_P (TREE_TYPE (type))))
4877 cp_parser_abort_tentative_parse (parser);
4878 else if (cp_parser_parse_definitely (parser))
4880 pseudo_destructor_p = true;
4882 = finish_pseudo_destructor_expr (postfix_expression,
4883 s, TREE_TYPE (type));
4887 if (!pseudo_destructor_p)
4889 /* If the SCOPE is not a scalar type, we are looking at an
4890 ordinary class member access expression, rather than a
4891 pseudo-destructor-name. */
4893 /* Parse the id-expression. */
4894 name = (cp_parser_id_expression
4896 cp_parser_optional_template_keyword (parser),
4897 /*check_dependency_p=*/true,
4899 /*declarator_p=*/false,
4900 /*optional_p=*/false));
4901 /* In general, build a SCOPE_REF if the member name is qualified.
4902 However, if the name was not dependent and has already been
4903 resolved; there is no need to build the SCOPE_REF. For example;
4905 struct X { void f(); };
4906 template <typename T> void f(T* t) { t->X::f(); }
4908 Even though "t" is dependent, "X::f" is not and has been resolved
4909 to a BASELINK; there is no need to include scope information. */
4911 /* But we do need to remember that there was an explicit scope for
4912 virtual function calls. */
4914 *idk = CP_ID_KIND_QUALIFIED;
4916 /* If the name is a template-id that names a type, we will get a
4917 TYPE_DECL here. That is invalid code. */
4918 if (TREE_CODE (name) == TYPE_DECL)
4920 error ("invalid use of %qD", name);
4921 postfix_expression = error_mark_node;
4925 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4927 name = build_qualified_name (/*type=*/NULL_TREE,
4931 parser->scope = NULL_TREE;
4932 parser->qualifying_scope = NULL_TREE;
4933 parser->object_scope = NULL_TREE;
4935 if (scope && name && BASELINK_P (name))
4936 adjust_result_of_qualified_name_lookup
4937 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4939 = finish_class_member_access_expr (postfix_expression, name,
4944 /* We no longer need to look up names in the scope of the object on
4945 the left-hand side of the `.' or `->' operator. */
4946 parser->context->object_type = NULL_TREE;
4948 /* Outside of offsetof, these operators may not appear in
4949 constant-expressions. */
4951 && (cp_parser_non_integral_constant_expression
4952 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4953 postfix_expression = error_mark_node;
4955 return postfix_expression;
4958 /* Parse a parenthesized expression-list.
4961 assignment-expression
4962 expression-list, assignment-expression
4967 identifier, expression-list
4969 CAST_P is true if this expression is the target of a cast.
4971 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4974 Returns a TREE_LIST. The TREE_VALUE of each node is a
4975 representation of an assignment-expression. Note that a TREE_LIST
4976 is returned even if there is only a single expression in the list.
4977 error_mark_node is returned if the ( and or ) are
4978 missing. NULL_TREE is returned on no expressions. The parentheses
4979 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4980 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4981 indicates whether or not all of the expressions in the list were
4985 cp_parser_parenthesized_expression_list (cp_parser* parser,
4986 bool is_attribute_list,
4988 bool allow_expansion_p,
4989 bool *non_constant_p)
4991 tree expression_list = NULL_TREE;
4992 bool fold_expr_p = is_attribute_list;
4993 tree identifier = NULL_TREE;
4994 bool saved_greater_than_is_operator_p;
4996 /* Assume all the expressions will be constant. */
4998 *non_constant_p = false;
5000 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5001 return error_mark_node;
5003 /* Within a parenthesized expression, a `>' token is always
5004 the greater-than operator. */
5005 saved_greater_than_is_operator_p
5006 = parser->greater_than_is_operator_p;
5007 parser->greater_than_is_operator_p = true;
5009 /* Consume expressions until there are no more. */
5010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5015 /* At the beginning of attribute lists, check to see if the
5016 next token is an identifier. */
5017 if (is_attribute_list
5018 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5022 /* Consume the identifier. */
5023 token = cp_lexer_consume_token (parser->lexer);
5024 /* Save the identifier. */
5025 identifier = token->u.value;
5029 /* Parse the next assignment-expression. */
5032 bool expr_non_constant_p;
5033 expr = (cp_parser_constant_expression
5034 (parser, /*allow_non_constant_p=*/true,
5035 &expr_non_constant_p));
5036 if (expr_non_constant_p)
5037 *non_constant_p = true;
5040 expr = cp_parser_assignment_expression (parser, cast_p);
5043 expr = fold_non_dependent_expr (expr);
5045 /* If we have an ellipsis, then this is an expression
5047 if (allow_expansion_p
5048 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5050 /* Consume the `...'. */
5051 cp_lexer_consume_token (parser->lexer);
5053 /* Build the argument pack. */
5054 expr = make_pack_expansion (expr);
5057 /* Add it to the list. We add error_mark_node
5058 expressions to the list, so that we can still tell if
5059 the correct form for a parenthesized expression-list
5060 is found. That gives better errors. */
5061 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5063 if (expr == error_mark_node)
5067 /* After the first item, attribute lists look the same as
5068 expression lists. */
5069 is_attribute_list = false;
5072 /* If the next token isn't a `,', then we are done. */
5073 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5076 /* Otherwise, consume the `,' and keep going. */
5077 cp_lexer_consume_token (parser->lexer);
5080 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5085 /* We try and resync to an unnested comma, as that will give the
5086 user better diagnostics. */
5087 ending = cp_parser_skip_to_closing_parenthesis (parser,
5088 /*recovering=*/true,
5090 /*consume_paren=*/true);
5095 parser->greater_than_is_operator_p
5096 = saved_greater_than_is_operator_p;
5097 return error_mark_node;
5101 parser->greater_than_is_operator_p
5102 = saved_greater_than_is_operator_p;
5104 /* We built up the list in reverse order so we must reverse it now. */
5105 expression_list = nreverse (expression_list);
5107 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5109 return expression_list;
5112 /* Parse a pseudo-destructor-name.
5114 pseudo-destructor-name:
5115 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5116 :: [opt] nested-name-specifier template template-id :: ~ type-name
5117 :: [opt] nested-name-specifier [opt] ~ type-name
5119 If either of the first two productions is used, sets *SCOPE to the
5120 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5121 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5122 or ERROR_MARK_NODE if the parse fails. */
5125 cp_parser_pseudo_destructor_name (cp_parser* parser,
5129 bool nested_name_specifier_p;
5131 /* Assume that things will not work out. */
5132 *type = error_mark_node;
5134 /* Look for the optional `::' operator. */
5135 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5136 /* Look for the optional nested-name-specifier. */
5137 nested_name_specifier_p
5138 = (cp_parser_nested_name_specifier_opt (parser,
5139 /*typename_keyword_p=*/false,
5140 /*check_dependency_p=*/true,
5142 /*is_declaration=*/true)
5144 /* Now, if we saw a nested-name-specifier, we might be doing the
5145 second production. */
5146 if (nested_name_specifier_p
5147 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5149 /* Consume the `template' keyword. */
5150 cp_lexer_consume_token (parser->lexer);
5151 /* Parse the template-id. */
5152 cp_parser_template_id (parser,
5153 /*template_keyword_p=*/true,
5154 /*check_dependency_p=*/false,
5155 /*is_declaration=*/true);
5156 /* Look for the `::' token. */
5157 cp_parser_require (parser, CPP_SCOPE, "`::'");
5159 /* If the next token is not a `~', then there might be some
5160 additional qualification. */
5161 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5163 /* At this point, we're looking for "type-name :: ~". The type-name
5164 must not be a class-name, since this is a pseudo-destructor. So,
5165 it must be either an enum-name, or a typedef-name -- both of which
5166 are just identifiers. So, we peek ahead to check that the "::"
5167 and "~" tokens are present; if they are not, then we can avoid
5168 calling type_name. */
5169 if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5170 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5171 || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5173 cp_parser_error (parser, "non-scalar type");
5177 /* Look for the type-name. */
5178 *scope = TREE_TYPE (cp_parser_type_name (parser));
5180 if (*scope == error_mark_node)
5183 /* Look for the `::' token. */
5184 cp_parser_require (parser, CPP_SCOPE, "`::'");
5189 /* Look for the `~'. */
5190 cp_parser_require (parser, CPP_COMPL, "`~'");
5191 /* Look for the type-name again. We are not responsible for
5192 checking that it matches the first type-name. */
5193 *type = cp_parser_type_name (parser);
5196 /* Parse a unary-expression.
5202 unary-operator cast-expression
5203 sizeof unary-expression
5211 __extension__ cast-expression
5212 __alignof__ unary-expression
5213 __alignof__ ( type-id )
5214 __real__ cast-expression
5215 __imag__ cast-expression
5218 ADDRESS_P is true iff the unary-expression is appearing as the
5219 operand of the `&' operator. CAST_P is true if this expression is
5220 the target of a cast.
5222 Returns a representation of the expression. */
5225 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5228 enum tree_code unary_operator;
5230 /* Peek at the next token. */
5231 token = cp_lexer_peek_token (parser->lexer);
5232 /* Some keywords give away the kind of expression. */
5233 if (token->type == CPP_KEYWORD)
5235 enum rid keyword = token->keyword;
5245 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5246 /* Consume the token. */
5247 cp_lexer_consume_token (parser->lexer);
5248 /* Parse the operand. */
5249 operand = cp_parser_sizeof_operand (parser, keyword);
5251 if (TYPE_P (operand))
5252 return cxx_sizeof_or_alignof_type (operand, op, true);
5254 return cxx_sizeof_or_alignof_expr (operand, op);
5258 return cp_parser_new_expression (parser);
5261 return cp_parser_delete_expression (parser);
5265 /* The saved value of the PEDANTIC flag. */
5269 /* Save away the PEDANTIC flag. */
5270 cp_parser_extension_opt (parser, &saved_pedantic);
5271 /* Parse the cast-expression. */
5272 expr = cp_parser_simple_cast_expression (parser);
5273 /* Restore the PEDANTIC flag. */
5274 pedantic = saved_pedantic;
5284 /* Consume the `__real__' or `__imag__' token. */
5285 cp_lexer_consume_token (parser->lexer);
5286 /* Parse the cast-expression. */
5287 expression = cp_parser_simple_cast_expression (parser);
5288 /* Create the complete representation. */
5289 return build_x_unary_op ((keyword == RID_REALPART
5290 ? REALPART_EXPR : IMAGPART_EXPR),
5300 /* Look for the `:: new' and `:: delete', which also signal the
5301 beginning of a new-expression, or delete-expression,
5302 respectively. If the next token is `::', then it might be one of
5304 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5308 /* See if the token after the `::' is one of the keywords in
5309 which we're interested. */
5310 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5311 /* If it's `new', we have a new-expression. */
5312 if (keyword == RID_NEW)
5313 return cp_parser_new_expression (parser);
5314 /* Similarly, for `delete'. */
5315 else if (keyword == RID_DELETE)
5316 return cp_parser_delete_expression (parser);
5319 /* Look for a unary operator. */
5320 unary_operator = cp_parser_unary_operator (token);
5321 /* The `++' and `--' operators can be handled similarly, even though
5322 they are not technically unary-operators in the grammar. */
5323 if (unary_operator == ERROR_MARK)
5325 if (token->type == CPP_PLUS_PLUS)
5326 unary_operator = PREINCREMENT_EXPR;
5327 else if (token->type == CPP_MINUS_MINUS)
5328 unary_operator = PREDECREMENT_EXPR;
5329 /* Handle the GNU address-of-label extension. */
5330 else if (cp_parser_allow_gnu_extensions_p (parser)
5331 && token->type == CPP_AND_AND)
5336 /* Consume the '&&' token. */
5337 cp_lexer_consume_token (parser->lexer);
5338 /* Look for the identifier. */
5339 identifier = cp_parser_identifier (parser);
5340 /* Create an expression representing the address. */
5341 expression = finish_label_address_expr (identifier);
5342 if (cp_parser_non_integral_constant_expression (parser,
5343 "the address of a label"))
5344 expression = error_mark_node;
5348 if (unary_operator != ERROR_MARK)
5350 tree cast_expression;
5351 tree expression = error_mark_node;
5352 const char *non_constant_p = NULL;
5354 /* Consume the operator token. */
5355 token = cp_lexer_consume_token (parser->lexer);
5356 /* Parse the cast-expression. */
5358 = cp_parser_cast_expression (parser,
5359 unary_operator == ADDR_EXPR,
5361 /* Now, build an appropriate representation. */
5362 switch (unary_operator)
5365 non_constant_p = "`*'";
5366 expression = build_x_indirect_ref (cast_expression, "unary *");
5370 non_constant_p = "`&'";
5373 expression = build_x_unary_op (unary_operator, cast_expression);
5376 case PREINCREMENT_EXPR:
5377 case PREDECREMENT_EXPR:
5378 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5381 case UNARY_PLUS_EXPR:
5383 case TRUTH_NOT_EXPR:
5384 expression = finish_unary_op_expr (unary_operator, cast_expression);
5392 && cp_parser_non_integral_constant_expression (parser,
5394 expression = error_mark_node;
5399 return cp_parser_postfix_expression (parser, address_p, cast_p,
5400 /*member_access_only_p=*/false);
5403 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5404 unary-operator, the corresponding tree code is returned. */
5406 static enum tree_code
5407 cp_parser_unary_operator (cp_token* token)
5409 switch (token->type)
5412 return INDIRECT_REF;
5418 return UNARY_PLUS_EXPR;
5424 return TRUTH_NOT_EXPR;
5427 return BIT_NOT_EXPR;
5434 /* Parse a new-expression.
5437 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5438 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5440 Returns a representation of the expression. */
5443 cp_parser_new_expression (cp_parser* parser)
5445 bool global_scope_p;
5451 /* Look for the optional `::' operator. */
5453 = (cp_parser_global_scope_opt (parser,
5454 /*current_scope_valid_p=*/false)
5456 /* Look for the `new' operator. */
5457 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5458 /* There's no easy way to tell a new-placement from the
5459 `( type-id )' construct. */
5460 cp_parser_parse_tentatively (parser);
5461 /* Look for a new-placement. */
5462 placement = cp_parser_new_placement (parser);
5463 /* If that didn't work out, there's no new-placement. */
5464 if (!cp_parser_parse_definitely (parser))
5465 placement = NULL_TREE;
5467 /* If the next token is a `(', then we have a parenthesized
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5471 /* Consume the `('. */
5472 cp_lexer_consume_token (parser->lexer);
5473 /* Parse the type-id. */
5474 type = cp_parser_type_id (parser);
5475 /* Look for the closing `)'. */
5476 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5477 /* There should not be a direct-new-declarator in this production,
5478 but GCC used to allowed this, so we check and emit a sensible error
5479 message for this case. */
5480 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5482 error ("array bound forbidden after parenthesized type-id");
5483 inform ("try removing the parentheses around the type-id");
5484 cp_parser_direct_new_declarator (parser);
5488 /* Otherwise, there must be a new-type-id. */
5490 type = cp_parser_new_type_id (parser, &nelts);
5492 /* If the next token is a `(', then we have a new-initializer. */
5493 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5494 initializer = cp_parser_new_initializer (parser);
5496 initializer = NULL_TREE;
5498 /* A new-expression may not appear in an integral constant
5500 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5501 return error_mark_node;
5503 /* Create a representation of the new-expression. */
5504 return build_new (placement, type, nelts, initializer, global_scope_p);
5507 /* Parse a new-placement.
5512 Returns the same representation as for an expression-list. */
5515 cp_parser_new_placement (cp_parser* parser)
5517 tree expression_list;
5519 /* Parse the expression-list. */
5520 expression_list = (cp_parser_parenthesized_expression_list
5521 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5522 /*non_constant_p=*/NULL));
5524 return expression_list;
5527 /* Parse a new-type-id.
5530 type-specifier-seq new-declarator [opt]
5532 Returns the TYPE allocated. If the new-type-id indicates an array
5533 type, *NELTS is set to the number of elements in the last array
5534 bound; the TYPE will not include the last array bound. */
5537 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5539 cp_decl_specifier_seq type_specifier_seq;
5540 cp_declarator *new_declarator;
5541 cp_declarator *declarator;
5542 cp_declarator *outer_declarator;
5543 const char *saved_message;
5546 /* The type-specifier sequence must not contain type definitions.
5547 (It cannot contain declarations of new types either, but if they
5548 are not definitions we will catch that because they are not
5550 saved_message = parser->type_definition_forbidden_message;
5551 parser->type_definition_forbidden_message
5552 = "types may not be defined in a new-type-id";
5553 /* Parse the type-specifier-seq. */
5554 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5555 &type_specifier_seq);
5556 /* Restore the old message. */
5557 parser->type_definition_forbidden_message = saved_message;
5558 /* Parse the new-declarator. */
5559 new_declarator = cp_parser_new_declarator_opt (parser);
5561 /* Determine the number of elements in the last array dimension, if
5564 /* Skip down to the last array dimension. */
5565 declarator = new_declarator;
5566 outer_declarator = NULL;
5567 while (declarator && (declarator->kind == cdk_pointer
5568 || declarator->kind == cdk_ptrmem))
5570 outer_declarator = declarator;
5571 declarator = declarator->declarator;
5574 && declarator->kind == cdk_array
5575 && declarator->declarator
5576 && declarator->declarator->kind == cdk_array)
5578 outer_declarator = declarator;
5579 declarator = declarator->declarator;
5582 if (declarator && declarator->kind == cdk_array)
5584 *nelts = declarator->u.array.bounds;
5585 if (*nelts == error_mark_node)
5586 *nelts = integer_one_node;
5588 if (outer_declarator)
5589 outer_declarator->declarator = declarator->declarator;
5591 new_declarator = NULL;
5594 type = groktypename (&type_specifier_seq, new_declarator);
5598 /* Parse an (optional) new-declarator.
5601 ptr-operator new-declarator [opt]
5602 direct-new-declarator
5604 Returns the declarator. */
5606 static cp_declarator *
5607 cp_parser_new_declarator_opt (cp_parser* parser)
5609 enum tree_code code;
5611 cp_cv_quals cv_quals;
5613 /* We don't know if there's a ptr-operator next, or not. */
5614 cp_parser_parse_tentatively (parser);
5615 /* Look for a ptr-operator. */
5616 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5617 /* If that worked, look for more new-declarators. */
5618 if (cp_parser_parse_definitely (parser))
5620 cp_declarator *declarator;
5622 /* Parse another optional declarator. */
5623 declarator = cp_parser_new_declarator_opt (parser);
5625 return cp_parser_make_indirect_declarator
5626 (code, type, cv_quals, declarator);
5629 /* If the next token is a `[', there is a direct-new-declarator. */
5630 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5631 return cp_parser_direct_new_declarator (parser);
5636 /* Parse a direct-new-declarator.
5638 direct-new-declarator:
5640 direct-new-declarator [constant-expression]
5644 static cp_declarator *
5645 cp_parser_direct_new_declarator (cp_parser* parser)
5647 cp_declarator *declarator = NULL;
5653 /* Look for the opening `['. */
5654 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5655 /* The first expression is not required to be constant. */
5658 expression = cp_parser_expression (parser, /*cast_p=*/false);
5659 /* The standard requires that the expression have integral
5660 type. DR 74 adds enumeration types. We believe that the
5661 real intent is that these expressions be handled like the
5662 expression in a `switch' condition, which also allows
5663 classes with a single conversion to integral or
5664 enumeration type. */
5665 if (!processing_template_decl)
5668 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5673 error ("expression in new-declarator must have integral "
5674 "or enumeration type");
5675 expression = error_mark_node;
5679 /* But all the other expressions must be. */
5682 = cp_parser_constant_expression (parser,
5683 /*allow_non_constant=*/false,
5685 /* Look for the closing `]'. */
5686 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5688 /* Add this bound to the declarator. */
5689 declarator = make_array_declarator (declarator, expression);
5691 /* If the next token is not a `[', then there are no more
5693 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5700 /* Parse a new-initializer.
5703 ( expression-list [opt] )
5705 Returns a representation of the expression-list. If there is no
5706 expression-list, VOID_ZERO_NODE is returned. */
5709 cp_parser_new_initializer (cp_parser* parser)
5711 tree expression_list;
5713 expression_list = (cp_parser_parenthesized_expression_list
5714 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5715 /*non_constant_p=*/NULL));
5716 if (!expression_list)
5717 expression_list = void_zero_node;
5719 return expression_list;
5722 /* Parse a delete-expression.
5725 :: [opt] delete cast-expression
5726 :: [opt] delete [ ] cast-expression
5728 Returns a representation of the expression. */
5731 cp_parser_delete_expression (cp_parser* parser)
5733 bool global_scope_p;
5737 /* Look for the optional `::' operator. */
5739 = (cp_parser_global_scope_opt (parser,
5740 /*current_scope_valid_p=*/false)
5742 /* Look for the `delete' keyword. */
5743 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5744 /* See if the array syntax is in use. */
5745 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5747 /* Consume the `[' token. */
5748 cp_lexer_consume_token (parser->lexer);
5749 /* Look for the `]' token. */
5750 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5751 /* Remember that this is the `[]' construct. */
5757 /* Parse the cast-expression. */
5758 expression = cp_parser_simple_cast_expression (parser);
5760 /* A delete-expression may not appear in an integral constant
5762 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5763 return error_mark_node;
5765 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5768 /* Parse a cast-expression.
5772 ( type-id ) cast-expression
5774 ADDRESS_P is true iff the unary-expression is appearing as the
5775 operand of the `&' operator. CAST_P is true if this expression is
5776 the target of a cast.
5778 Returns a representation of the expression. */
5781 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5783 /* If it's a `(', then we might be looking at a cast. */
5784 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5786 tree type = NULL_TREE;
5787 tree expr = NULL_TREE;
5788 bool compound_literal_p;
5789 const char *saved_message;
5791 /* There's no way to know yet whether or not this is a cast.
5792 For example, `(int (3))' is a unary-expression, while `(int)
5793 3' is a cast. So, we resort to parsing tentatively. */
5794 cp_parser_parse_tentatively (parser);
5795 /* Types may not be defined in a cast. */
5796 saved_message = parser->type_definition_forbidden_message;
5797 parser->type_definition_forbidden_message
5798 = "types may not be defined in casts";
5799 /* Consume the `('. */
5800 cp_lexer_consume_token (parser->lexer);
5801 /* A very tricky bit is that `(struct S) { 3 }' is a
5802 compound-literal (which we permit in C++ as an extension).
5803 But, that construct is not a cast-expression -- it is a
5804 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5805 is legal; if the compound-literal were a cast-expression,
5806 you'd need an extra set of parentheses.) But, if we parse
5807 the type-id, and it happens to be a class-specifier, then we
5808 will commit to the parse at that point, because we cannot
5809 undo the action that is done when creating a new class. So,
5810 then we cannot back up and do a postfix-expression.
5812 Therefore, we scan ahead to the closing `)', and check to see
5813 if the token after the `)' is a `{'. If so, we are not
5814 looking at a cast-expression.
5816 Save tokens so that we can put them back. */
5817 cp_lexer_save_tokens (parser->lexer);
5818 /* Skip tokens until the next token is a closing parenthesis.
5819 If we find the closing `)', and the next token is a `{', then
5820 we are looking at a compound-literal. */
5822 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5823 /*consume_paren=*/true)
5824 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5825 /* Roll back the tokens we skipped. */
5826 cp_lexer_rollback_tokens (parser->lexer);
5827 /* If we were looking at a compound-literal, simulate an error
5828 so that the call to cp_parser_parse_definitely below will
5830 if (compound_literal_p)
5831 cp_parser_simulate_error (parser);
5834 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5835 parser->in_type_id_in_expr_p = true;
5836 /* Look for the type-id. */
5837 type = cp_parser_type_id (parser);
5838 /* Look for the closing `)'. */
5839 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5840 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5843 /* Restore the saved message. */
5844 parser->type_definition_forbidden_message = saved_message;
5846 /* If ok so far, parse the dependent expression. We cannot be
5847 sure it is a cast. Consider `(T ())'. It is a parenthesized
5848 ctor of T, but looks like a cast to function returning T
5849 without a dependent expression. */
5850 if (!cp_parser_error_occurred (parser))
5851 expr = cp_parser_cast_expression (parser,
5852 /*address_p=*/false,
5855 if (cp_parser_parse_definitely (parser))
5857 /* Warn about old-style casts, if so requested. */
5858 if (warn_old_style_cast
5859 && !in_system_header
5860 && !VOID_TYPE_P (type)
5861 && current_lang_name != lang_name_c)
5862 warning (OPT_Wold_style_cast, "use of old-style cast");
5864 /* Only type conversions to integral or enumeration types
5865 can be used in constant-expressions. */
5866 if (!cast_valid_in_integral_constant_expression_p (type)
5867 && (cp_parser_non_integral_constant_expression
5869 "a cast to a type other than an integral or "
5870 "enumeration type")))
5871 return error_mark_node;
5873 /* Perform the cast. */
5874 expr = build_c_cast (type, expr);
5879 /* If we get here, then it's not a cast, so it must be a
5880 unary-expression. */
5881 return cp_parser_unary_expression (parser, address_p, cast_p);
5884 /* Parse a binary expression of the general form:
5888 pm-expression .* cast-expression
5889 pm-expression ->* cast-expression
5891 multiplicative-expression:
5893 multiplicative-expression * pm-expression
5894 multiplicative-expression / pm-expression
5895 multiplicative-expression % pm-expression
5897 additive-expression:
5898 multiplicative-expression
5899 additive-expression + multiplicative-expression
5900 additive-expression - multiplicative-expression
5904 shift-expression << additive-expression
5905 shift-expression >> additive-expression
5907 relational-expression:
5909 relational-expression < shift-expression
5910 relational-expression > shift-expression
5911 relational-expression <= shift-expression
5912 relational-expression >= shift-expression
5916 relational-expression:
5917 relational-expression <? shift-expression
5918 relational-expression >? shift-expression
5920 equality-expression:
5921 relational-expression
5922 equality-expression == relational-expression
5923 equality-expression != relational-expression
5927 and-expression & equality-expression
5929 exclusive-or-expression:
5931 exclusive-or-expression ^ and-expression
5933 inclusive-or-expression:
5934 exclusive-or-expression
5935 inclusive-or-expression | exclusive-or-expression
5937 logical-and-expression:
5938 inclusive-or-expression
5939 logical-and-expression && inclusive-or-expression
5941 logical-or-expression:
5942 logical-and-expression
5943 logical-or-expression || logical-and-expression
5945 All these are implemented with a single function like:
5948 simple-cast-expression
5949 binary-expression <token> binary-expression
5951 CAST_P is true if this expression is the target of a cast.
5953 The binops_by_token map is used to get the tree codes for each <token> type.
5954 binary-expressions are associated according to a precedence table. */
5956 #define TOKEN_PRECEDENCE(token) \
5957 (((token->type == CPP_GREATER \
5958 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5959 && !parser->greater_than_is_operator_p) \
5960 ? PREC_NOT_OPERATOR \
5961 : binops_by_token[token->type].prec)
5964 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5966 cp_parser_expression_stack stack;
5967 cp_parser_expression_stack_entry *sp = &stack[0];
5970 enum tree_code tree_type, lhs_type, rhs_type;
5971 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5974 /* Parse the first expression. */
5975 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5976 lhs_type = ERROR_MARK;
5980 /* Get an operator token. */
5981 token = cp_lexer_peek_token (parser->lexer);
5983 if (warn_cxx0x_compat
5984 && token->type == CPP_RSHIFT
5985 && !parser->greater_than_is_operator_p)
5987 warning (OPT_Wc__0x_compat,
5988 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5990 warning (OPT_Wc__0x_compat,
5991 "suggest parentheses around %<>>%> expression");
5994 new_prec = TOKEN_PRECEDENCE (token);
5996 /* Popping an entry off the stack means we completed a subexpression:
5997 - either we found a token which is not an operator (`>' where it is not
5998 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5999 will happen repeatedly;
6000 - or, we found an operator which has lower priority. This is the case
6001 where the recursive descent *ascends*, as in `3 * 4 + 5' after
6003 if (new_prec <= prec)
6012 tree_type = binops_by_token[token->type].tree_type;
6014 /* We used the operator token. */
6015 cp_lexer_consume_token (parser->lexer);
6017 /* Extract another operand. It may be the RHS of this expression
6018 or the LHS of a new, higher priority expression. */
6019 rhs = cp_parser_simple_cast_expression (parser);
6020 rhs_type = ERROR_MARK;
6022 /* Get another operator token. Look up its precedence to avoid
6023 building a useless (immediately popped) stack entry for common
6024 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
6025 token = cp_lexer_peek_token (parser->lexer);
6026 lookahead_prec = TOKEN_PRECEDENCE (token);
6027 if (lookahead_prec > new_prec)
6029 /* ... and prepare to parse the RHS of the new, higher priority
6030 expression. Since precedence levels on the stack are
6031 monotonically increasing, we do not have to care about
6034 sp->tree_type = tree_type;
6036 sp->lhs_type = lhs_type;
6039 lhs_type = rhs_type;
6041 new_prec = lookahead_prec;
6045 /* If the stack is not empty, we have parsed into LHS the right side
6046 (`4' in the example above) of an expression we had suspended.
6047 We can use the information on the stack to recover the LHS (`3')
6048 from the stack together with the tree code (`MULT_EXPR'), and
6049 the precedence of the higher level subexpression
6050 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6051 which will be used to actually build the additive expression. */
6054 tree_type = sp->tree_type;
6056 rhs_type = lhs_type;
6058 lhs_type = sp->lhs_type;
6061 overloaded_p = false;
6062 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6064 lhs_type = tree_type;
6066 /* If the binary operator required the use of an overloaded operator,
6067 then this expression cannot be an integral constant-expression.
6068 An overloaded operator can be used even if both operands are
6069 otherwise permissible in an integral constant-expression if at
6070 least one of the operands is of enumeration type. */
6073 && (cp_parser_non_integral_constant_expression
6074 (parser, "calls to overloaded operators")))
6075 return error_mark_node;
6082 /* Parse the `? expression : assignment-expression' part of a
6083 conditional-expression. The LOGICAL_OR_EXPR is the
6084 logical-or-expression that started the conditional-expression.
6085 Returns a representation of the entire conditional-expression.
6087 This routine is used by cp_parser_assignment_expression.
6089 ? expression : assignment-expression
6093 ? : assignment-expression */
6096 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6099 tree assignment_expr;
6101 /* Consume the `?' token. */
6102 cp_lexer_consume_token (parser->lexer);
6103 if (cp_parser_allow_gnu_extensions_p (parser)
6104 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6105 /* Implicit true clause. */
6108 /* Parse the expression. */
6109 expr = cp_parser_expression (parser, /*cast_p=*/false);
6111 /* The next token should be a `:'. */
6112 cp_parser_require (parser, CPP_COLON, "`:'");
6113 /* Parse the assignment-expression. */
6114 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6116 /* Build the conditional-expression. */
6117 return build_x_conditional_expr (logical_or_expr,
6122 /* Parse an assignment-expression.
6124 assignment-expression:
6125 conditional-expression
6126 logical-or-expression assignment-operator assignment_expression
6129 CAST_P is true if this expression is the target of a cast.
6131 Returns a representation for the expression. */
6134 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6138 /* If the next token is the `throw' keyword, then we're looking at
6139 a throw-expression. */
6140 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6141 expr = cp_parser_throw_expression (parser);
6142 /* Otherwise, it must be that we are looking at a
6143 logical-or-expression. */
6146 /* Parse the binary expressions (logical-or-expression). */
6147 expr = cp_parser_binary_expression (parser, cast_p);
6148 /* If the next token is a `?' then we're actually looking at a
6149 conditional-expression. */
6150 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6151 return cp_parser_question_colon_clause (parser, expr);
6154 enum tree_code assignment_operator;
6156 /* If it's an assignment-operator, we're using the second
6159 = cp_parser_assignment_operator_opt (parser);
6160 if (assignment_operator != ERROR_MARK)
6164 /* Parse the right-hand side of the assignment. */
6165 rhs = cp_parser_assignment_expression (parser, cast_p);
6166 /* An assignment may not appear in a
6167 constant-expression. */
6168 if (cp_parser_non_integral_constant_expression (parser,
6170 return error_mark_node;
6171 /* Build the assignment expression. */
6172 expr = build_x_modify_expr (expr,
6173 assignment_operator,
6182 /* Parse an (optional) assignment-operator.
6184 assignment-operator: one of
6185 = *= /= %= += -= >>= <<= &= ^= |=
6189 assignment-operator: one of
6192 If the next token is an assignment operator, the corresponding tree
6193 code is returned, and the token is consumed. For example, for
6194 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6195 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6196 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6197 operator, ERROR_MARK is returned. */
6199 static enum tree_code
6200 cp_parser_assignment_operator_opt (cp_parser* parser)
6205 /* Peek at the next toen. */
6206 token = cp_lexer_peek_token (parser->lexer);
6208 switch (token->type)
6219 op = TRUNC_DIV_EXPR;
6223 op = TRUNC_MOD_EXPR;
6255 /* Nothing else is an assignment operator. */
6259 /* If it was an assignment operator, consume it. */
6260 if (op != ERROR_MARK)
6261 cp_lexer_consume_token (parser->lexer);
6266 /* Parse an expression.
6269 assignment-expression
6270 expression , assignment-expression
6272 CAST_P is true if this expression is the target of a cast.
6274 Returns a representation of the expression. */
6277 cp_parser_expression (cp_parser* parser, bool cast_p)
6279 tree expression = NULL_TREE;
6283 tree assignment_expression;
6285 /* Parse the next assignment-expression. */
6286 assignment_expression
6287 = cp_parser_assignment_expression (parser, cast_p);
6288 /* If this is the first assignment-expression, we can just
6291 expression = assignment_expression;
6293 expression = build_x_compound_expr (expression,
6294 assignment_expression);
6295 /* If the next token is not a comma, then we are done with the
6297 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6299 /* Consume the `,'. */
6300 cp_lexer_consume_token (parser->lexer);
6301 /* A comma operator cannot appear in a constant-expression. */
6302 if (cp_parser_non_integral_constant_expression (parser,
6303 "a comma operator"))
6304 expression = error_mark_node;
6310 /* Parse a constant-expression.
6312 constant-expression:
6313 conditional-expression
6315 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6316 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6317 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6318 is false, NON_CONSTANT_P should be NULL. */
6321 cp_parser_constant_expression (cp_parser* parser,
6322 bool allow_non_constant_p,
6323 bool *non_constant_p)
6325 bool saved_integral_constant_expression_p;
6326 bool saved_allow_non_integral_constant_expression_p;
6327 bool saved_non_integral_constant_expression_p;
6330 /* It might seem that we could simply parse the
6331 conditional-expression, and then check to see if it were
6332 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6333 one that the compiler can figure out is constant, possibly after
6334 doing some simplifications or optimizations. The standard has a
6335 precise definition of constant-expression, and we must honor
6336 that, even though it is somewhat more restrictive.
6342 is not a legal declaration, because `(2, 3)' is not a
6343 constant-expression. The `,' operator is forbidden in a
6344 constant-expression. However, GCC's constant-folding machinery
6345 will fold this operation to an INTEGER_CST for `3'. */
6347 /* Save the old settings. */
6348 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6349 saved_allow_non_integral_constant_expression_p
6350 = parser->allow_non_integral_constant_expression_p;
6351 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6352 /* We are now parsing a constant-expression. */
6353 parser->integral_constant_expression_p = true;
6354 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6355 parser->non_integral_constant_expression_p = false;
6356 /* Although the grammar says "conditional-expression", we parse an
6357 "assignment-expression", which also permits "throw-expression"
6358 and the use of assignment operators. In the case that
6359 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6360 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6361 actually essential that we look for an assignment-expression.
6362 For example, cp_parser_initializer_clauses uses this function to
6363 determine whether a particular assignment-expression is in fact
6365 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6366 /* Restore the old settings. */
6367 parser->integral_constant_expression_p
6368 = saved_integral_constant_expression_p;
6369 parser->allow_non_integral_constant_expression_p
6370 = saved_allow_non_integral_constant_expression_p;
6371 if (allow_non_constant_p)
6372 *non_constant_p = parser->non_integral_constant_expression_p;
6373 else if (parser->non_integral_constant_expression_p)
6374 expression = error_mark_node;
6375 parser->non_integral_constant_expression_p
6376 = saved_non_integral_constant_expression_p;
6381 /* Parse __builtin_offsetof.
6383 offsetof-expression:
6384 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6386 offsetof-member-designator:
6388 | offsetof-member-designator "." id-expression
6389 | offsetof-member-designator "[" expression "]" */
6392 cp_parser_builtin_offsetof (cp_parser *parser)
6394 int save_ice_p, save_non_ice_p;
6398 /* We're about to accept non-integral-constant things, but will
6399 definitely yield an integral constant expression. Save and
6400 restore these values around our local parsing. */
6401 save_ice_p = parser->integral_constant_expression_p;
6402 save_non_ice_p = parser->non_integral_constant_expression_p;
6404 /* Consume the "__builtin_offsetof" token. */
6405 cp_lexer_consume_token (parser->lexer);
6406 /* Consume the opening `('. */
6407 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6408 /* Parse the type-id. */
6409 type = cp_parser_type_id (parser);
6410 /* Look for the `,'. */
6411 cp_parser_require (parser, CPP_COMMA, "`,'");
6413 /* Build the (type *)null that begins the traditional offsetof macro. */
6414 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6416 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6417 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6421 cp_token *token = cp_lexer_peek_token (parser->lexer);
6422 switch (token->type)
6424 case CPP_OPEN_SQUARE:
6425 /* offsetof-member-designator "[" expression "]" */
6426 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6430 /* offsetof-member-designator "." identifier */
6431 cp_lexer_consume_token (parser->lexer);
6432 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6436 case CPP_CLOSE_PAREN:
6437 /* Consume the ")" token. */
6438 cp_lexer_consume_token (parser->lexer);
6442 /* Error. We know the following require will fail, but
6443 that gives the proper error message. */
6444 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6445 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6446 expr = error_mark_node;
6452 /* If we're processing a template, we can't finish the semantics yet.
6453 Otherwise we can fold the entire expression now. */
6454 if (processing_template_decl)
6455 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6457 expr = finish_offsetof (expr);
6460 parser->integral_constant_expression_p = save_ice_p;
6461 parser->non_integral_constant_expression_p = save_non_ice_p;
6466 /* Parse a trait expression. */
6469 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6472 tree type1, type2 = NULL_TREE;
6473 bool binary = false;
6474 cp_decl_specifier_seq decl_specs;
6478 case RID_HAS_NOTHROW_ASSIGN:
6479 kind = CPTK_HAS_NOTHROW_ASSIGN;
6481 case RID_HAS_NOTHROW_CONSTRUCTOR:
6482 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6484 case RID_HAS_NOTHROW_COPY:
6485 kind = CPTK_HAS_NOTHROW_COPY;
6487 case RID_HAS_TRIVIAL_ASSIGN:
6488 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6490 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6491 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6493 case RID_HAS_TRIVIAL_COPY:
6494 kind = CPTK_HAS_TRIVIAL_COPY;
6496 case RID_HAS_TRIVIAL_DESTRUCTOR:
6497 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6499 case RID_HAS_VIRTUAL_DESTRUCTOR:
6500 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6502 case RID_IS_ABSTRACT:
6503 kind = CPTK_IS_ABSTRACT;
6505 case RID_IS_BASE_OF:
6506 kind = CPTK_IS_BASE_OF;
6510 kind = CPTK_IS_CLASS;
6512 case RID_IS_CONVERTIBLE_TO:
6513 kind = CPTK_IS_CONVERTIBLE_TO;
6517 kind = CPTK_IS_EMPTY;
6520 kind = CPTK_IS_ENUM;
6525 case RID_IS_POLYMORPHIC:
6526 kind = CPTK_IS_POLYMORPHIC;
6529 kind = CPTK_IS_UNION;
6535 /* Consume the token. */
6536 cp_lexer_consume_token (parser->lexer);
6538 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6540 type1 = cp_parser_type_id (parser);
6542 if (type1 == error_mark_node)
6543 return error_mark_node;
6545 /* Build a trivial decl-specifier-seq. */
6546 clear_decl_specs (&decl_specs);
6547 decl_specs.type = type1;
6549 /* Call grokdeclarator to figure out what type this is. */
6550 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6551 /*initialized=*/0, /*attrlist=*/NULL);
6555 cp_parser_require (parser, CPP_COMMA, "`,'");
6557 type2 = cp_parser_type_id (parser);
6559 if (type2 == error_mark_node)
6560 return error_mark_node;
6562 /* Build a trivial decl-specifier-seq. */
6563 clear_decl_specs (&decl_specs);
6564 decl_specs.type = type2;
6566 /* Call grokdeclarator to figure out what type this is. */
6567 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6568 /*initialized=*/0, /*attrlist=*/NULL);
6571 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6573 /* Complete the trait expression, which may mean either processing
6574 the trait expr now or saving it for template instantiation. */
6575 return finish_trait_expr (kind, type1, type2);
6578 /* Statements [gram.stmt.stmt] */
6580 /* Parse a statement.
6584 expression-statement
6589 declaration-statement
6592 IN_COMPOUND is true when the statement is nested inside a
6593 cp_parser_compound_statement; this matters for certain pragmas.
6595 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6596 is a (possibly labeled) if statement which is not enclosed in braces
6597 and has an else clause. This is used to implement -Wparentheses. */
6600 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6601 bool in_compound, bool *if_p)
6605 location_t statement_location;
6610 /* There is no statement yet. */
6611 statement = NULL_TREE;
6612 /* Peek at the next token. */
6613 token = cp_lexer_peek_token (parser->lexer);
6614 /* Remember the location of the first token in the statement. */
6615 statement_location = token->location;
6616 /* If this is a keyword, then that will often determine what kind of
6617 statement we have. */
6618 if (token->type == CPP_KEYWORD)
6620 enum rid keyword = token->keyword;
6626 /* Looks like a labeled-statement with a case label.
6627 Parse the label, and then use tail recursion to parse
6629 cp_parser_label_for_labeled_statement (parser);
6634 statement = cp_parser_selection_statement (parser, if_p);
6640 statement = cp_parser_iteration_statement (parser);
6647 statement = cp_parser_jump_statement (parser);
6650 /* Objective-C++ exception-handling constructs. */
6653 case RID_AT_FINALLY:
6654 case RID_AT_SYNCHRONIZED:
6656 statement = cp_parser_objc_statement (parser);
6660 statement = cp_parser_try_block (parser);
6664 /* This must be a namespace alias definition. */
6665 cp_parser_declaration_statement (parser);
6669 /* It might be a keyword like `int' that can start a
6670 declaration-statement. */
6674 else if (token->type == CPP_NAME)
6676 /* If the next token is a `:', then we are looking at a
6677 labeled-statement. */
6678 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6679 if (token->type == CPP_COLON)
6681 /* Looks like a labeled-statement with an ordinary label.
6682 Parse the label, and then use tail recursion to parse
6684 cp_parser_label_for_labeled_statement (parser);
6688 /* Anything that starts with a `{' must be a compound-statement. */
6689 else if (token->type == CPP_OPEN_BRACE)
6690 statement = cp_parser_compound_statement (parser, NULL, false);
6691 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6692 a statement all its own. */
6693 else if (token->type == CPP_PRAGMA)
6695 /* Only certain OpenMP pragmas are attached to statements, and thus
6696 are considered statements themselves. All others are not. In
6697 the context of a compound, accept the pragma as a "statement" and
6698 return so that we can check for a close brace. Otherwise we
6699 require a real statement and must go back and read one. */
6701 cp_parser_pragma (parser, pragma_compound);
6702 else if (!cp_parser_pragma (parser, pragma_stmt))
6706 else if (token->type == CPP_EOF)
6708 cp_parser_error (parser, "expected statement");
6712 /* Everything else must be a declaration-statement or an
6713 expression-statement. Try for the declaration-statement
6714 first, unless we are looking at a `;', in which case we know that
6715 we have an expression-statement. */
6718 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6720 cp_parser_parse_tentatively (parser);
6721 /* Try to parse the declaration-statement. */
6722 cp_parser_declaration_statement (parser);
6723 /* If that worked, we're done. */
6724 if (cp_parser_parse_definitely (parser))
6727 /* Look for an expression-statement instead. */
6728 statement = cp_parser_expression_statement (parser, in_statement_expr);
6731 /* Set the line number for the statement. */
6732 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6733 SET_EXPR_LOCATION (statement, statement_location);
6736 /* Parse the label for a labeled-statement, i.e.
6739 case constant-expression :
6743 case constant-expression ... constant-expression : statement
6745 When a label is parsed without errors, the label is added to the
6746 parse tree by the finish_* functions, so this function doesn't
6747 have to return the label. */
6750 cp_parser_label_for_labeled_statement (cp_parser* parser)
6754 /* The next token should be an identifier. */
6755 token = cp_lexer_peek_token (parser->lexer);
6756 if (token->type != CPP_NAME
6757 && token->type != CPP_KEYWORD)
6759 cp_parser_error (parser, "expected labeled-statement");
6763 switch (token->keyword)
6770 /* Consume the `case' token. */
6771 cp_lexer_consume_token (parser->lexer);
6772 /* Parse the constant-expression. */
6773 expr = cp_parser_constant_expression (parser,
6774 /*allow_non_constant_p=*/false,
6777 ellipsis = cp_lexer_peek_token (parser->lexer);
6778 if (ellipsis->type == CPP_ELLIPSIS)
6780 /* Consume the `...' token. */
6781 cp_lexer_consume_token (parser->lexer);
6783 cp_parser_constant_expression (parser,
6784 /*allow_non_constant_p=*/false,
6786 /* We don't need to emit warnings here, as the common code
6787 will do this for us. */
6790 expr_hi = NULL_TREE;
6792 if (parser->in_switch_statement_p)
6793 finish_case_label (expr, expr_hi);
6795 error ("case label %qE not within a switch statement", expr);
6800 /* Consume the `default' token. */
6801 cp_lexer_consume_token (parser->lexer);
6803 if (parser->in_switch_statement_p)
6804 finish_case_label (NULL_TREE, NULL_TREE);
6806 error ("case label not within a switch statement");
6810 /* Anything else must be an ordinary label. */
6811 finish_label_stmt (cp_parser_identifier (parser));
6815 /* Require the `:' token. */
6816 cp_parser_require (parser, CPP_COLON, "`:'");
6819 /* Parse an expression-statement.
6821 expression-statement:
6824 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6825 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6826 indicates whether this expression-statement is part of an
6827 expression statement. */
6830 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6832 tree statement = NULL_TREE;
6834 /* If the next token is a ';', then there is no expression
6836 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6837 statement = cp_parser_expression (parser, /*cast_p=*/false);
6839 /* Consume the final `;'. */
6840 cp_parser_consume_semicolon_at_end_of_statement (parser);
6842 if (in_statement_expr
6843 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6844 /* This is the final expression statement of a statement
6846 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6848 statement = finish_expr_stmt (statement);
6855 /* Parse a compound-statement.
6858 { statement-seq [opt] }
6863 { label-declaration-seq [opt] statement-seq [opt] }
6865 label-declaration-seq:
6867 label-declaration-seq label-declaration
6869 Returns a tree representing the statement. */
6872 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6877 /* Consume the `{'. */
6878 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6879 return error_mark_node;
6880 /* Begin the compound-statement. */
6881 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6882 /* If the next keyword is `__label__' we have a label declaration. */
6883 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6884 cp_parser_label_declaration (parser);
6885 /* Parse an (optional) statement-seq. */
6886 cp_parser_statement_seq_opt (parser, in_statement_expr);
6887 /* Finish the compound-statement. */
6888 finish_compound_stmt (compound_stmt);
6889 /* Consume the `}'. */
6890 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6892 return compound_stmt;
6895 /* Parse an (optional) statement-seq.
6899 statement-seq [opt] statement */
6902 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6904 /* Scan statements until there aren't any more. */
6907 cp_token *token = cp_lexer_peek_token (parser->lexer);
6909 /* If we're looking at a `}', then we've run out of statements. */
6910 if (token->type == CPP_CLOSE_BRACE
6911 || token->type == CPP_EOF
6912 || token->type == CPP_PRAGMA_EOL)
6915 /* If we are in a compound statement and find 'else' then
6916 something went wrong. */
6917 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6919 if (parser->in_statement & IN_IF_STMT)
6923 token = cp_lexer_consume_token (parser->lexer);
6924 error ("%<else%> without a previous %<if%>");
6928 /* Parse the statement. */
6929 cp_parser_statement (parser, in_statement_expr, true, NULL);
6933 /* Parse a selection-statement.
6935 selection-statement:
6936 if ( condition ) statement
6937 if ( condition ) statement else statement
6938 switch ( condition ) statement
6940 Returns the new IF_STMT or SWITCH_STMT.
6942 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6943 is a (possibly labeled) if statement which is not enclosed in
6944 braces and has an else clause. This is used to implement
6948 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6956 /* Peek at the next token. */
6957 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6959 /* See what kind of keyword it is. */
6960 keyword = token->keyword;
6969 /* Look for the `('. */
6970 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6972 cp_parser_skip_to_end_of_statement (parser);
6973 return error_mark_node;
6976 /* Begin the selection-statement. */
6977 if (keyword == RID_IF)
6978 statement = begin_if_stmt ();
6980 statement = begin_switch_stmt ();
6982 /* Parse the condition. */
6983 condition = cp_parser_condition (parser);
6984 /* Look for the `)'. */
6985 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6986 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6987 /*consume_paren=*/true);
6989 if (keyword == RID_IF)
6992 unsigned char in_statement;
6994 /* Add the condition. */
6995 finish_if_stmt_cond (condition, statement);
6997 /* Parse the then-clause. */
6998 in_statement = parser->in_statement;
6999 parser->in_statement |= IN_IF_STMT;
7000 cp_parser_implicitly_scoped_statement (parser, &nested_if);
7001 parser->in_statement = in_statement;
7003 finish_then_clause (statement);
7005 /* If the next token is `else', parse the else-clause. */
7006 if (cp_lexer_next_token_is_keyword (parser->lexer,
7009 /* Consume the `else' keyword. */
7010 cp_lexer_consume_token (parser->lexer);
7011 begin_else_clause (statement);
7012 /* Parse the else-clause. */
7013 cp_parser_implicitly_scoped_statement (parser, NULL);
7014 finish_else_clause (statement);
7016 /* If we are currently parsing a then-clause, then
7017 IF_P will not be NULL. We set it to true to
7018 indicate that this if statement has an else clause.
7019 This may trigger the Wparentheses warning below
7020 when we get back up to the parent if statement. */
7026 /* This if statement does not have an else clause. If
7027 NESTED_IF is true, then the then-clause is an if
7028 statement which does have an else clause. We warn
7029 about the potential ambiguity. */
7031 warning (OPT_Wparentheses,
7032 ("%Hsuggest explicit braces "
7033 "to avoid ambiguous %<else%>"),
7034 EXPR_LOCUS (statement));
7037 /* Now we're all done with the if-statement. */
7038 finish_if_stmt (statement);
7042 bool in_switch_statement_p;
7043 unsigned char in_statement;
7045 /* Add the condition. */
7046 finish_switch_cond (condition, statement);
7048 /* Parse the body of the switch-statement. */
7049 in_switch_statement_p = parser->in_switch_statement_p;
7050 in_statement = parser->in_statement;
7051 parser->in_switch_statement_p = true;
7052 parser->in_statement |= IN_SWITCH_STMT;
7053 cp_parser_implicitly_scoped_statement (parser, NULL);
7054 parser->in_switch_statement_p = in_switch_statement_p;
7055 parser->in_statement = in_statement;
7057 /* Now we're all done with the switch-statement. */
7058 finish_switch_stmt (statement);
7066 cp_parser_error (parser, "expected selection-statement");
7067 return error_mark_node;
7071 /* Parse a condition.
7075 type-specifier-seq declarator = assignment-expression
7080 type-specifier-seq declarator asm-specification [opt]
7081 attributes [opt] = assignment-expression
7083 Returns the expression that should be tested. */
7086 cp_parser_condition (cp_parser* parser)
7088 cp_decl_specifier_seq type_specifiers;
7089 const char *saved_message;
7091 /* Try the declaration first. */
7092 cp_parser_parse_tentatively (parser);
7093 /* New types are not allowed in the type-specifier-seq for a
7095 saved_message = parser->type_definition_forbidden_message;
7096 parser->type_definition_forbidden_message
7097 = "types may not be defined in conditions";
7098 /* Parse the type-specifier-seq. */
7099 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7101 /* Restore the saved message. */
7102 parser->type_definition_forbidden_message = saved_message;
7103 /* If all is well, we might be looking at a declaration. */
7104 if (!cp_parser_error_occurred (parser))
7107 tree asm_specification;
7109 cp_declarator *declarator;
7110 tree initializer = NULL_TREE;
7112 /* Parse the declarator. */
7113 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7114 /*ctor_dtor_or_conv_p=*/NULL,
7115 /*parenthesized_p=*/NULL,
7116 /*member_p=*/false);
7117 /* Parse the attributes. */
7118 attributes = cp_parser_attributes_opt (parser);
7119 /* Parse the asm-specification. */
7120 asm_specification = cp_parser_asm_specification_opt (parser);
7121 /* If the next token is not an `=', then we might still be
7122 looking at an expression. For example:
7126 looks like a decl-specifier-seq and a declarator -- but then
7127 there is no `=', so this is an expression. */
7128 cp_parser_require (parser, CPP_EQ, "`='");
7129 /* If we did see an `=', then we are looking at a declaration
7131 if (cp_parser_parse_definitely (parser))
7134 bool non_constant_p;
7136 /* Create the declaration. */
7137 decl = start_decl (declarator, &type_specifiers,
7138 /*initialized_p=*/true,
7139 attributes, /*prefix_attributes=*/NULL_TREE,
7141 /* Parse the assignment-expression. */
7143 = cp_parser_constant_expression (parser,
7144 /*allow_non_constant_p=*/true,
7146 if (!non_constant_p)
7147 initializer = fold_non_dependent_expr (initializer);
7149 /* Process the initializer. */
7150 cp_finish_decl (decl,
7151 initializer, !non_constant_p,
7153 LOOKUP_ONLYCONVERTING);
7156 pop_scope (pushed_scope);
7158 return convert_from_reference (decl);
7161 /* If we didn't even get past the declarator successfully, we are
7162 definitely not looking at a declaration. */
7164 cp_parser_abort_tentative_parse (parser);
7166 /* Otherwise, we are looking at an expression. */
7167 return cp_parser_expression (parser, /*cast_p=*/false);
7170 /* We check for a ) immediately followed by ; with no whitespacing
7171 between. This is used to issue a warning for:
7179 as the semicolon is probably extraneous.
7181 On parse errors, the next token might not be a ), so do nothing in
7185 check_empty_body (cp_parser* parser, const char* type)
7188 cp_token *close_paren;
7189 expanded_location close_loc;
7190 expanded_location semi_loc;
7192 close_paren = cp_lexer_peek_token (parser->lexer);
7193 if (close_paren->type != CPP_CLOSE_PAREN)
7196 close_loc = expand_location (close_paren->location);
7197 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7199 if (token->type != CPP_SEMICOLON
7200 || (token->flags & PREV_WHITE))
7203 semi_loc = expand_location (token->location);
7204 if (close_loc.line == semi_loc.line
7205 && close_loc.column+1 == semi_loc.column)
7206 warning (OPT_Wempty_body,
7207 "suggest a space before %<;%> or explicit braces around empty "
7208 "body in %<%s%> statement",
7212 /* Parse an iteration-statement.
7214 iteration-statement:
7215 while ( condition ) statement
7216 do statement while ( expression ) ;
7217 for ( for-init-statement condition [opt] ; expression [opt] )
7220 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7223 cp_parser_iteration_statement (cp_parser* parser)
7228 unsigned char in_statement;
7230 /* Peek at the next token. */
7231 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7233 return error_mark_node;
7235 /* Remember whether or not we are already within an iteration
7237 in_statement = parser->in_statement;
7239 /* See what kind of keyword it is. */
7240 keyword = token->keyword;
7247 /* Begin the while-statement. */
7248 statement = begin_while_stmt ();
7249 /* Look for the `('. */
7250 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7251 /* Parse the condition. */
7252 condition = cp_parser_condition (parser);
7253 finish_while_stmt_cond (condition, statement);
7254 check_empty_body (parser, "while");
7255 /* Look for the `)'. */
7256 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7257 /* Parse the dependent statement. */
7258 parser->in_statement = IN_ITERATION_STMT;
7259 cp_parser_already_scoped_statement (parser);
7260 parser->in_statement = in_statement;
7261 /* We're done with the while-statement. */
7262 finish_while_stmt (statement);
7270 /* Begin the do-statement. */
7271 statement = begin_do_stmt ();
7272 /* Parse the body of the do-statement. */
7273 parser->in_statement = IN_ITERATION_STMT;
7274 cp_parser_implicitly_scoped_statement (parser, NULL);
7275 parser->in_statement = in_statement;
7276 finish_do_body (statement);
7277 /* Look for the `while' keyword. */
7278 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7279 /* Look for the `('. */
7280 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7281 /* Parse the expression. */
7282 expression = cp_parser_expression (parser, /*cast_p=*/false);
7283 /* We're done with the do-statement. */
7284 finish_do_stmt (expression, statement);
7285 /* Look for the `)'. */
7286 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7287 /* Look for the `;'. */
7288 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7294 tree condition = NULL_TREE;
7295 tree expression = NULL_TREE;
7297 /* Begin the for-statement. */
7298 statement = begin_for_stmt ();
7299 /* Look for the `('. */
7300 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7301 /* Parse the initialization. */
7302 cp_parser_for_init_statement (parser);
7303 finish_for_init_stmt (statement);
7305 /* If there's a condition, process it. */
7306 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7307 condition = cp_parser_condition (parser);
7308 finish_for_cond (condition, statement);
7309 /* Look for the `;'. */
7310 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7312 /* If there's an expression, process it. */
7313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7314 expression = cp_parser_expression (parser, /*cast_p=*/false);
7315 finish_for_expr (expression, statement);
7316 check_empty_body (parser, "for");
7317 /* Look for the `)'. */
7318 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7320 /* Parse the body of the for-statement. */
7321 parser->in_statement = IN_ITERATION_STMT;
7322 cp_parser_already_scoped_statement (parser);
7323 parser->in_statement = in_statement;
7325 /* We're done with the for-statement. */
7326 finish_for_stmt (statement);
7331 cp_parser_error (parser, "expected iteration-statement");
7332 statement = error_mark_node;
7339 /* Parse a for-init-statement.
7342 expression-statement
7343 simple-declaration */
7346 cp_parser_for_init_statement (cp_parser* parser)
7348 /* If the next token is a `;', then we have an empty
7349 expression-statement. Grammatically, this is also a
7350 simple-declaration, but an invalid one, because it does not
7351 declare anything. Therefore, if we did not handle this case
7352 specially, we would issue an error message about an invalid
7354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7356 /* We're going to speculatively look for a declaration, falling back
7357 to an expression, if necessary. */
7358 cp_parser_parse_tentatively (parser);
7359 /* Parse the declaration. */
7360 cp_parser_simple_declaration (parser,
7361 /*function_definition_allowed_p=*/false);
7362 /* If the tentative parse failed, then we shall need to look for an
7363 expression-statement. */
7364 if (cp_parser_parse_definitely (parser))
7368 cp_parser_expression_statement (parser, false);
7371 /* Parse a jump-statement.
7376 return expression [opt] ;
7384 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7387 cp_parser_jump_statement (cp_parser* parser)
7389 tree statement = error_mark_node;
7392 unsigned char in_statement;
7394 /* Peek at the next token. */
7395 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7397 return error_mark_node;
7399 /* See what kind of keyword it is. */
7400 keyword = token->keyword;
7404 in_statement = parser->in_statement & ~IN_IF_STMT;
7405 switch (in_statement)
7408 error ("break statement not within loop or switch");
7411 gcc_assert ((in_statement & IN_SWITCH_STMT)
7412 || in_statement == IN_ITERATION_STMT);
7413 statement = finish_break_stmt ();
7416 error ("invalid exit from OpenMP structured block");
7419 error ("break statement used with OpenMP for loop");
7422 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7426 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7429 error ("continue statement not within a loop");
7431 case IN_ITERATION_STMT:
7433 statement = finish_continue_stmt ();
7436 error ("invalid exit from OpenMP structured block");
7441 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7448 /* If the next token is a `;', then there is no
7450 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7451 expr = cp_parser_expression (parser, /*cast_p=*/false);
7454 /* Build the return-statement. */
7455 statement = finish_return_stmt (expr);
7456 /* Look for the final `;'. */
7457 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7462 /* Create the goto-statement. */
7463 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7465 /* Issue a warning about this use of a GNU extension. */
7467 pedwarn ("ISO C++ forbids computed gotos");
7468 /* Consume the '*' token. */
7469 cp_lexer_consume_token (parser->lexer);
7470 /* Parse the dependent expression. */
7471 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7474 finish_goto_stmt (cp_parser_identifier (parser));
7475 /* Look for the final `;'. */
7476 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7480 cp_parser_error (parser, "expected jump-statement");
7487 /* Parse a declaration-statement.
7489 declaration-statement:
7490 block-declaration */
7493 cp_parser_declaration_statement (cp_parser* parser)
7497 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7498 p = obstack_alloc (&declarator_obstack, 0);
7500 /* Parse the block-declaration. */
7501 cp_parser_block_declaration (parser, /*statement_p=*/true);
7503 /* Free any declarators allocated. */
7504 obstack_free (&declarator_obstack, p);
7506 /* Finish off the statement. */
7510 /* Some dependent statements (like `if (cond) statement'), are
7511 implicitly in their own scope. In other words, if the statement is
7512 a single statement (as opposed to a compound-statement), it is
7513 none-the-less treated as if it were enclosed in braces. Any
7514 declarations appearing in the dependent statement are out of scope
7515 after control passes that point. This function parses a statement,
7516 but ensures that is in its own scope, even if it is not a
7519 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7520 is a (possibly labeled) if statement which is not enclosed in
7521 braces and has an else clause. This is used to implement
7524 Returns the new statement. */
7527 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7534 /* Mark if () ; with a special NOP_EXPR. */
7535 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7537 cp_lexer_consume_token (parser->lexer);
7538 statement = add_stmt (build_empty_stmt ());
7540 /* if a compound is opened, we simply parse the statement directly. */
7541 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7542 statement = cp_parser_compound_statement (parser, NULL, false);
7543 /* If the token is not a `{', then we must take special action. */
7546 /* Create a compound-statement. */
7547 statement = begin_compound_stmt (0);
7548 /* Parse the dependent-statement. */
7549 cp_parser_statement (parser, NULL_TREE, false, if_p);
7550 /* Finish the dummy compound-statement. */
7551 finish_compound_stmt (statement);
7554 /* Return the statement. */
7558 /* For some dependent statements (like `while (cond) statement'), we
7559 have already created a scope. Therefore, even if the dependent
7560 statement is a compound-statement, we do not want to create another
7564 cp_parser_already_scoped_statement (cp_parser* parser)
7566 /* If the token is a `{', then we must take special action. */
7567 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7568 cp_parser_statement (parser, NULL_TREE, false, NULL);
7571 /* Avoid calling cp_parser_compound_statement, so that we
7572 don't create a new scope. Do everything else by hand. */
7573 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7574 cp_parser_statement_seq_opt (parser, NULL_TREE);
7575 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7579 /* Declarations [gram.dcl.dcl] */
7581 /* Parse an optional declaration-sequence.
7585 declaration-seq declaration */
7588 cp_parser_declaration_seq_opt (cp_parser* parser)
7594 token = cp_lexer_peek_token (parser->lexer);
7596 if (token->type == CPP_CLOSE_BRACE
7597 || token->type == CPP_EOF
7598 || token->type == CPP_PRAGMA_EOL)
7601 if (token->type == CPP_SEMICOLON)
7603 /* A declaration consisting of a single semicolon is
7604 invalid. Allow it unless we're being pedantic. */
7605 cp_lexer_consume_token (parser->lexer);
7606 if (pedantic && !in_system_header)
7607 pedwarn ("extra %<;%>");
7611 /* If we're entering or exiting a region that's implicitly
7612 extern "C", modify the lang context appropriately. */
7613 if (!parser->implicit_extern_c && token->implicit_extern_c)
7615 push_lang_context (lang_name_c);
7616 parser->implicit_extern_c = true;
7618 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7620 pop_lang_context ();
7621 parser->implicit_extern_c = false;
7624 if (token->type == CPP_PRAGMA)
7626 /* A top-level declaration can consist solely of a #pragma.
7627 A nested declaration cannot, so this is done here and not
7628 in cp_parser_declaration. (A #pragma at block scope is
7629 handled in cp_parser_statement.) */
7630 cp_parser_pragma (parser, pragma_external);
7634 /* Parse the declaration itself. */
7635 cp_parser_declaration (parser);
7639 /* Parse a declaration.
7644 template-declaration
7645 explicit-instantiation
7646 explicit-specialization
7647 linkage-specification
7648 namespace-definition
7653 __extension__ declaration */
7656 cp_parser_declaration (cp_parser* parser)
7663 /* Check for the `__extension__' keyword. */
7664 if (cp_parser_extension_opt (parser, &saved_pedantic))
7666 /* Parse the qualified declaration. */
7667 cp_parser_declaration (parser);
7668 /* Restore the PEDANTIC flag. */
7669 pedantic = saved_pedantic;
7674 /* Try to figure out what kind of declaration is present. */
7675 token1 = *cp_lexer_peek_token (parser->lexer);
7677 if (token1.type != CPP_EOF)
7678 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7681 token2.type = CPP_EOF;
7682 token2.keyword = RID_MAX;
7685 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7686 p = obstack_alloc (&declarator_obstack, 0);
7688 /* If the next token is `extern' and the following token is a string
7689 literal, then we have a linkage specification. */
7690 if (token1.keyword == RID_EXTERN
7691 && cp_parser_is_string_literal (&token2))
7692 cp_parser_linkage_specification (parser);
7693 /* If the next token is `template', then we have either a template
7694 declaration, an explicit instantiation, or an explicit
7696 else if (token1.keyword == RID_TEMPLATE)
7698 /* `template <>' indicates a template specialization. */
7699 if (token2.type == CPP_LESS
7700 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7701 cp_parser_explicit_specialization (parser);
7702 /* `template <' indicates a template declaration. */
7703 else if (token2.type == CPP_LESS)
7704 cp_parser_template_declaration (parser, /*member_p=*/false);
7705 /* Anything else must be an explicit instantiation. */
7707 cp_parser_explicit_instantiation (parser);
7709 /* If the next token is `export', then we have a template
7711 else if (token1.keyword == RID_EXPORT)
7712 cp_parser_template_declaration (parser, /*member_p=*/false);
7713 /* If the next token is `extern', 'static' or 'inline' and the one
7714 after that is `template', we have a GNU extended explicit
7715 instantiation directive. */
7716 else if (cp_parser_allow_gnu_extensions_p (parser)
7717 && (token1.keyword == RID_EXTERN
7718 || token1.keyword == RID_STATIC
7719 || token1.keyword == RID_INLINE)
7720 && token2.keyword == RID_TEMPLATE)
7721 cp_parser_explicit_instantiation (parser);
7722 /* If the next token is `namespace', check for a named or unnamed
7723 namespace definition. */
7724 else if (token1.keyword == RID_NAMESPACE
7725 && (/* A named namespace definition. */
7726 (token2.type == CPP_NAME
7727 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7729 /* An unnamed namespace definition. */
7730 || token2.type == CPP_OPEN_BRACE
7731 || token2.keyword == RID_ATTRIBUTE))
7732 cp_parser_namespace_definition (parser);
7733 /* An inline (associated) namespace definition. */
7734 else if (token1.keyword == RID_INLINE
7735 && token2.keyword == RID_NAMESPACE)
7736 cp_parser_namespace_definition (parser);
7737 /* Objective-C++ declaration/definition. */
7738 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7739 cp_parser_objc_declaration (parser);
7740 /* We must have either a block declaration or a function
7743 /* Try to parse a block-declaration, or a function-definition. */
7744 cp_parser_block_declaration (parser, /*statement_p=*/false);
7746 /* Free any declarators allocated. */
7747 obstack_free (&declarator_obstack, p);
7750 /* Parse a block-declaration.
7755 namespace-alias-definition
7762 __extension__ block-declaration
7767 static_assert-declaration
7769 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7770 part of a declaration-statement. */
7773 cp_parser_block_declaration (cp_parser *parser,
7779 /* Check for the `__extension__' keyword. */
7780 if (cp_parser_extension_opt (parser, &saved_pedantic))
7782 /* Parse the qualified declaration. */
7783 cp_parser_block_declaration (parser, statement_p);
7784 /* Restore the PEDANTIC flag. */
7785 pedantic = saved_pedantic;
7790 /* Peek at the next token to figure out which kind of declaration is
7792 token1 = cp_lexer_peek_token (parser->lexer);
7794 /* If the next keyword is `asm', we have an asm-definition. */
7795 if (token1->keyword == RID_ASM)
7798 cp_parser_commit_to_tentative_parse (parser);
7799 cp_parser_asm_definition (parser);
7801 /* If the next keyword is `namespace', we have a
7802 namespace-alias-definition. */
7803 else if (token1->keyword == RID_NAMESPACE)
7804 cp_parser_namespace_alias_definition (parser);
7805 /* If the next keyword is `using', we have either a
7806 using-declaration or a using-directive. */
7807 else if (token1->keyword == RID_USING)
7812 cp_parser_commit_to_tentative_parse (parser);
7813 /* If the token after `using' is `namespace', then we have a
7815 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7816 if (token2->keyword == RID_NAMESPACE)
7817 cp_parser_using_directive (parser);
7818 /* Otherwise, it's a using-declaration. */
7820 cp_parser_using_declaration (parser,
7821 /*access_declaration_p=*/false);
7823 /* If the next keyword is `__label__' we have a misplaced label
7825 else if (token1->keyword == RID_LABEL)
7827 cp_lexer_consume_token (parser->lexer);
7828 error ("%<__label__%> not at the beginning of a block");
7829 cp_parser_skip_to_end_of_statement (parser);
7830 /* If the next token is now a `;', consume it. */
7831 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7832 cp_lexer_consume_token (parser->lexer);
7834 /* If the next token is `static_assert' we have a static assertion. */
7835 else if (token1->keyword == RID_STATIC_ASSERT)
7836 cp_parser_static_assert (parser, /*member_p=*/false);
7837 /* Anything else must be a simple-declaration. */
7839 cp_parser_simple_declaration (parser, !statement_p);
7842 /* Parse a simple-declaration.
7845 decl-specifier-seq [opt] init-declarator-list [opt] ;
7847 init-declarator-list:
7849 init-declarator-list , init-declarator
7851 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7852 function-definition as a simple-declaration. */
7855 cp_parser_simple_declaration (cp_parser* parser,
7856 bool function_definition_allowed_p)
7858 cp_decl_specifier_seq decl_specifiers;
7859 int declares_class_or_enum;
7860 bool saw_declarator;
7862 /* Defer access checks until we know what is being declared; the
7863 checks for names appearing in the decl-specifier-seq should be
7864 done as if we were in the scope of the thing being declared. */
7865 push_deferring_access_checks (dk_deferred);
7867 /* Parse the decl-specifier-seq. We have to keep track of whether
7868 or not the decl-specifier-seq declares a named class or
7869 enumeration type, since that is the only case in which the
7870 init-declarator-list is allowed to be empty.
7874 In a simple-declaration, the optional init-declarator-list can be
7875 omitted only when declaring a class or enumeration, that is when
7876 the decl-specifier-seq contains either a class-specifier, an
7877 elaborated-type-specifier, or an enum-specifier. */
7878 cp_parser_decl_specifier_seq (parser,
7879 CP_PARSER_FLAGS_OPTIONAL,
7881 &declares_class_or_enum);
7882 /* We no longer need to defer access checks. */
7883 stop_deferring_access_checks ();
7885 /* In a block scope, a valid declaration must always have a
7886 decl-specifier-seq. By not trying to parse declarators, we can
7887 resolve the declaration/expression ambiguity more quickly. */
7888 if (!function_definition_allowed_p
7889 && !decl_specifiers.any_specifiers_p)
7891 cp_parser_error (parser, "expected declaration");
7895 /* If the next two tokens are both identifiers, the code is
7896 erroneous. The usual cause of this situation is code like:
7900 where "T" should name a type -- but does not. */
7901 if (!decl_specifiers.type
7902 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7904 /* If parsing tentatively, we should commit; we really are
7905 looking at a declaration. */
7906 cp_parser_commit_to_tentative_parse (parser);
7911 /* If we have seen at least one decl-specifier, and the next token
7912 is not a parenthesis, then we must be looking at a declaration.
7913 (After "int (" we might be looking at a functional cast.) */
7914 if (decl_specifiers.any_specifiers_p
7915 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7916 cp_parser_commit_to_tentative_parse (parser);
7918 /* Keep going until we hit the `;' at the end of the simple
7920 saw_declarator = false;
7921 while (cp_lexer_next_token_is_not (parser->lexer,
7925 bool function_definition_p;
7930 /* If we are processing next declarator, coma is expected */
7931 token = cp_lexer_peek_token (parser->lexer);
7932 gcc_assert (token->type == CPP_COMMA);
7933 cp_lexer_consume_token (parser->lexer);
7936 saw_declarator = true;
7938 /* Parse the init-declarator. */
7939 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7941 function_definition_allowed_p,
7943 declares_class_or_enum,
7944 &function_definition_p);
7945 /* If an error occurred while parsing tentatively, exit quickly.
7946 (That usually happens when in the body of a function; each
7947 statement is treated as a declaration-statement until proven
7949 if (cp_parser_error_occurred (parser))
7951 /* Handle function definitions specially. */
7952 if (function_definition_p)
7954 /* If the next token is a `,', then we are probably
7955 processing something like:
7959 which is erroneous. */
7960 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7961 error ("mixing declarations and function-definitions is forbidden");
7962 /* Otherwise, we're done with the list of declarators. */
7965 pop_deferring_access_checks ();
7969 /* The next token should be either a `,' or a `;'. */
7970 token = cp_lexer_peek_token (parser->lexer);
7971 /* If it's a `,', there are more declarators to come. */
7972 if (token->type == CPP_COMMA)
7973 /* will be consumed next time around */;
7974 /* If it's a `;', we are done. */
7975 else if (token->type == CPP_SEMICOLON)
7977 /* Anything else is an error. */
7980 /* If we have already issued an error message we don't need
7981 to issue another one. */
7982 if (decl != error_mark_node
7983 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7984 cp_parser_error (parser, "expected %<,%> or %<;%>");
7985 /* Skip tokens until we reach the end of the statement. */
7986 cp_parser_skip_to_end_of_statement (parser);
7987 /* If the next token is now a `;', consume it. */
7988 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7989 cp_lexer_consume_token (parser->lexer);
7992 /* After the first time around, a function-definition is not
7993 allowed -- even if it was OK at first. For example:
7998 function_definition_allowed_p = false;
8001 /* Issue an error message if no declarators are present, and the
8002 decl-specifier-seq does not itself declare a class or
8004 if (!saw_declarator)
8006 if (cp_parser_declares_only_class_p (parser))
8007 shadow_tag (&decl_specifiers);
8008 /* Perform any deferred access checks. */
8009 perform_deferred_access_checks ();
8012 /* Consume the `;'. */
8013 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8016 pop_deferring_access_checks ();
8019 /* Parse a decl-specifier-seq.
8022 decl-specifier-seq [opt] decl-specifier
8025 storage-class-specifier
8036 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8038 The parser flags FLAGS is used to control type-specifier parsing.
8040 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8043 1: one of the decl-specifiers is an elaborated-type-specifier
8044 (i.e., a type declaration)
8045 2: one of the decl-specifiers is an enum-specifier or a
8046 class-specifier (i.e., a type definition)
8051 cp_parser_decl_specifier_seq (cp_parser* parser,
8052 cp_parser_flags flags,
8053 cp_decl_specifier_seq *decl_specs,
8054 int* declares_class_or_enum)
8056 bool constructor_possible_p = !parser->in_declarator_p;
8058 /* Clear DECL_SPECS. */
8059 clear_decl_specs (decl_specs);
8061 /* Assume no class or enumeration type is declared. */
8062 *declares_class_or_enum = 0;
8064 /* Keep reading specifiers until there are no more to read. */
8068 bool found_decl_spec;
8071 /* Peek at the next token. */
8072 token = cp_lexer_peek_token (parser->lexer);
8073 /* Handle attributes. */
8074 if (token->keyword == RID_ATTRIBUTE)
8076 /* Parse the attributes. */
8077 decl_specs->attributes
8078 = chainon (decl_specs->attributes,
8079 cp_parser_attributes_opt (parser));
8082 /* Assume we will find a decl-specifier keyword. */
8083 found_decl_spec = true;
8084 /* If the next token is an appropriate keyword, we can simply
8085 add it to the list. */
8086 switch (token->keyword)
8091 if (!at_class_scope_p ())
8093 error ("%<friend%> used outside of class");
8094 cp_lexer_purge_token (parser->lexer);
8098 ++decl_specs->specs[(int) ds_friend];
8099 /* Consume the token. */
8100 cp_lexer_consume_token (parser->lexer);
8104 /* function-specifier:
8111 cp_parser_function_specifier_opt (parser, decl_specs);
8117 ++decl_specs->specs[(int) ds_typedef];
8118 /* Consume the token. */
8119 cp_lexer_consume_token (parser->lexer);
8120 /* A constructor declarator cannot appear in a typedef. */
8121 constructor_possible_p = false;
8122 /* The "typedef" keyword can only occur in a declaration; we
8123 may as well commit at this point. */
8124 cp_parser_commit_to_tentative_parse (parser);
8126 if (decl_specs->storage_class != sc_none)
8127 decl_specs->conflicting_specifiers_p = true;
8130 /* storage-class-specifier:
8144 /* Consume the token. */
8145 cp_lexer_consume_token (parser->lexer);
8146 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8149 /* Consume the token. */
8150 cp_lexer_consume_token (parser->lexer);
8151 ++decl_specs->specs[(int) ds_thread];
8155 /* We did not yet find a decl-specifier yet. */
8156 found_decl_spec = false;
8160 /* Constructors are a special case. The `S' in `S()' is not a
8161 decl-specifier; it is the beginning of the declarator. */
8164 && constructor_possible_p
8165 && (cp_parser_constructor_declarator_p
8166 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8168 /* If we don't have a DECL_SPEC yet, then we must be looking at
8169 a type-specifier. */
8170 if (!found_decl_spec && !constructor_p)
8172 int decl_spec_declares_class_or_enum;
8173 bool is_cv_qualifier;
8177 = cp_parser_type_specifier (parser, flags,
8179 /*is_declaration=*/true,
8180 &decl_spec_declares_class_or_enum,
8183 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8185 /* If this type-specifier referenced a user-defined type
8186 (a typedef, class-name, etc.), then we can't allow any
8187 more such type-specifiers henceforth.
8191 The longest sequence of decl-specifiers that could
8192 possibly be a type name is taken as the
8193 decl-specifier-seq of a declaration. The sequence shall
8194 be self-consistent as described below.
8198 As a general rule, at most one type-specifier is allowed
8199 in the complete decl-specifier-seq of a declaration. The
8200 only exceptions are the following:
8202 -- const or volatile can be combined with any other
8205 -- signed or unsigned can be combined with char, long,
8213 void g (const int Pc);
8215 Here, Pc is *not* part of the decl-specifier seq; it's
8216 the declarator. Therefore, once we see a type-specifier
8217 (other than a cv-qualifier), we forbid any additional
8218 user-defined types. We *do* still allow things like `int
8219 int' to be considered a decl-specifier-seq, and issue the
8220 error message later. */
8221 if (type_spec && !is_cv_qualifier)
8222 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8223 /* A constructor declarator cannot follow a type-specifier. */
8226 constructor_possible_p = false;
8227 found_decl_spec = true;
8231 /* If we still do not have a DECL_SPEC, then there are no more
8233 if (!found_decl_spec)
8236 decl_specs->any_specifiers_p = true;
8237 /* After we see one decl-specifier, further decl-specifiers are
8239 flags |= CP_PARSER_FLAGS_OPTIONAL;
8242 cp_parser_check_decl_spec (decl_specs);
8244 /* Don't allow a friend specifier with a class definition. */
8245 if (decl_specs->specs[(int) ds_friend] != 0
8246 && (*declares_class_or_enum & 2))
8247 error ("class definition may not be declared a friend");
8250 /* Parse an (optional) storage-class-specifier.
8252 storage-class-specifier:
8261 storage-class-specifier:
8264 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8267 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8269 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8277 /* Consume the token. */
8278 return cp_lexer_consume_token (parser->lexer)->u.value;
8285 /* Parse an (optional) function-specifier.
8292 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8293 Updates DECL_SPECS, if it is non-NULL. */
8296 cp_parser_function_specifier_opt (cp_parser* parser,
8297 cp_decl_specifier_seq *decl_specs)
8299 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8303 ++decl_specs->specs[(int) ds_inline];
8307 /* 14.5.2.3 [temp.mem]
8309 A member function template shall not be virtual. */
8310 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8311 error ("templates may not be %<virtual%>");
8312 else if (decl_specs)
8313 ++decl_specs->specs[(int) ds_virtual];
8318 ++decl_specs->specs[(int) ds_explicit];
8325 /* Consume the token. */
8326 return cp_lexer_consume_token (parser->lexer)->u.value;
8329 /* Parse a linkage-specification.
8331 linkage-specification:
8332 extern string-literal { declaration-seq [opt] }
8333 extern string-literal declaration */
8336 cp_parser_linkage_specification (cp_parser* parser)
8340 /* Look for the `extern' keyword. */
8341 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8343 /* Look for the string-literal. */
8344 linkage = cp_parser_string_literal (parser, false, false);
8346 /* Transform the literal into an identifier. If the literal is a
8347 wide-character string, or contains embedded NULs, then we can't
8348 handle it as the user wants. */
8349 if (strlen (TREE_STRING_POINTER (linkage))
8350 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8352 cp_parser_error (parser, "invalid linkage-specification");
8353 /* Assume C++ linkage. */
8354 linkage = lang_name_cplusplus;
8357 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8359 /* We're now using the new linkage. */
8360 push_lang_context (linkage);
8362 /* If the next token is a `{', then we're using the first
8364 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8366 /* Consume the `{' token. */
8367 cp_lexer_consume_token (parser->lexer);
8368 /* Parse the declarations. */
8369 cp_parser_declaration_seq_opt (parser);
8370 /* Look for the closing `}'. */
8371 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8373 /* Otherwise, there's just one declaration. */
8376 bool saved_in_unbraced_linkage_specification_p;
8378 saved_in_unbraced_linkage_specification_p
8379 = parser->in_unbraced_linkage_specification_p;
8380 parser->in_unbraced_linkage_specification_p = true;
8381 cp_parser_declaration (parser);
8382 parser->in_unbraced_linkage_specification_p
8383 = saved_in_unbraced_linkage_specification_p;
8386 /* We're done with the linkage-specification. */
8387 pop_lang_context ();
8390 /* Parse a static_assert-declaration.
8392 static_assert-declaration:
8393 static_assert ( constant-expression , string-literal ) ;
8395 If MEMBER_P, this static_assert is a class member. */
8398 cp_parser_static_assert(cp_parser *parser, bool member_p)
8403 location_t saved_loc;
8405 /* Peek at the `static_assert' token so we can keep track of exactly
8406 where the static assertion started. */
8407 token = cp_lexer_peek_token (parser->lexer);
8408 saved_loc = token->location;
8410 /* Look for the `static_assert' keyword. */
8411 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8415 /* We know we are in a static assertion; commit to any tentative
8417 if (cp_parser_parsing_tentatively (parser))
8418 cp_parser_commit_to_tentative_parse (parser);
8420 /* Parse the `(' starting the static assertion condition. */
8421 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8423 /* Parse the constant-expression. */
8425 cp_parser_constant_expression (parser,
8426 /*allow_non_constant_p=*/false,
8427 /*non_constant_p=*/NULL);
8429 /* Parse the separating `,'. */
8430 cp_parser_require (parser, CPP_COMMA, "`,'");
8432 /* Parse the string-literal message. */
8433 message = cp_parser_string_literal (parser,
8434 /*translate=*/false,
8437 /* A `)' completes the static assertion. */
8438 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8439 cp_parser_skip_to_closing_parenthesis (parser,
8440 /*recovering=*/true,
8442 /*consume_paren=*/true);
8444 /* A semicolon terminates the declaration. */
8445 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8447 /* Complete the static assertion, which may mean either processing
8448 the static assert now or saving it for template instantiation. */
8449 finish_static_assert (condition, message, saved_loc, member_p);
8452 /* Parse a `decltype' type. Returns the type.
8454 simple-type-specifier:
8455 decltype ( expression ) */
8458 cp_parser_decltype (cp_parser *parser)
8461 bool id_expression_or_member_access_p = false;
8462 const char *saved_message;
8463 bool saved_integral_constant_expression_p;
8464 bool saved_non_integral_constant_expression_p;
8466 /* Look for the `decltype' token. */
8467 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8468 return error_mark_node;
8470 /* Types cannot be defined in a `decltype' expression. Save away the
8472 saved_message = parser->type_definition_forbidden_message;
8474 /* And create the new one. */
8475 parser->type_definition_forbidden_message
8476 = "types may not be defined in `decltype' expressions";
8478 /* The restrictions on constant-expressions do not apply inside
8479 decltype expressions. */
8480 saved_integral_constant_expression_p
8481 = parser->integral_constant_expression_p;
8482 saved_non_integral_constant_expression_p
8483 = parser->non_integral_constant_expression_p;
8484 parser->integral_constant_expression_p = false;
8486 /* Do not actually evaluate the expression. */
8489 /* Parse the opening `('. */
8490 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
8491 return error_mark_node;
8493 /* First, try parsing an id-expression. */
8494 cp_parser_parse_tentatively (parser);
8495 expr = cp_parser_id_expression (parser,
8496 /*template_keyword_p=*/false,
8497 /*check_dependency_p=*/true,
8498 /*template_p=*/NULL,
8499 /*declarator_p=*/false,
8500 /*optional_p=*/false);
8502 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8504 bool non_integral_constant_expression_p = false;
8505 tree id_expression = expr;
8507 const char *error_msg;
8509 if (TREE_CODE (expr) == IDENTIFIER_NODE)
8510 /* Lookup the name we got back from the id-expression. */
8511 expr = cp_parser_lookup_name (parser, expr,
8513 /*is_template=*/false,
8514 /*is_namespace=*/false,
8515 /*check_dependency=*/true,
8516 /*ambiguous_decls=*/NULL);
8519 && expr != error_mark_node
8520 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8521 && TREE_CODE (expr) != TYPE_DECL
8522 && (TREE_CODE (expr) != BIT_NOT_EXPR
8523 || !TYPE_P (TREE_OPERAND (expr, 0)))
8524 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8526 /* Complete lookup of the id-expression. */
8527 expr = (finish_id_expression
8528 (id_expression, expr, parser->scope, &idk,
8529 /*integral_constant_expression_p=*/false,
8530 /*allow_non_integral_constant_expression_p=*/true,
8531 &non_integral_constant_expression_p,
8532 /*template_p=*/false,
8534 /*address_p=*/false,
8535 /*template_arg_p=*/false,
8538 if (expr == error_mark_node)
8539 /* We found an id-expression, but it was something that we
8540 should not have found. This is an error, not something
8541 we can recover from, so note that we found an
8542 id-expression and we'll recover as gracefully as
8544 id_expression_or_member_access_p = true;
8548 && expr != error_mark_node
8549 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8550 /* We have an id-expression. */
8551 id_expression_or_member_access_p = true;
8554 if (!id_expression_or_member_access_p)
8556 /* Abort the id-expression parse. */
8557 cp_parser_abort_tentative_parse (parser);
8559 /* Parsing tentatively, again. */
8560 cp_parser_parse_tentatively (parser);
8562 /* Parse a class member access. */
8563 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8565 /*member_access_only_p=*/true);
8568 && expr != error_mark_node
8569 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8570 /* We have an id-expression. */
8571 id_expression_or_member_access_p = true;
8574 if (id_expression_or_member_access_p)
8575 /* We have parsed the complete id-expression or member access. */
8576 cp_parser_parse_definitely (parser);
8579 /* Abort our attempt to parse an id-expression or member access
8581 cp_parser_abort_tentative_parse (parser);
8583 /* Parse a full expression. */
8584 expr = cp_parser_expression (parser, /*cast_p=*/false);
8587 /* Go back to evaluating expressions. */
8590 /* Restore the old message and the integral constant expression
8592 parser->type_definition_forbidden_message = saved_message;
8593 parser->integral_constant_expression_p
8594 = saved_integral_constant_expression_p;
8595 parser->non_integral_constant_expression_p
8596 = saved_non_integral_constant_expression_p;
8598 if (expr == error_mark_node)
8600 /* Skip everything up to the closing `)'. */
8601 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8602 /*consume_paren=*/true);
8603 return error_mark_node;
8606 /* Parse to the closing `)'. */
8607 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8609 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8610 /*consume_paren=*/true);
8611 return error_mark_node;
8614 return finish_decltype_type (expr, id_expression_or_member_access_p);
8617 /* Special member functions [gram.special] */
8619 /* Parse a conversion-function-id.
8621 conversion-function-id:
8622 operator conversion-type-id
8624 Returns an IDENTIFIER_NODE representing the operator. */
8627 cp_parser_conversion_function_id (cp_parser* parser)
8631 tree saved_qualifying_scope;
8632 tree saved_object_scope;
8633 tree pushed_scope = NULL_TREE;
8635 /* Look for the `operator' token. */
8636 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8637 return error_mark_node;
8638 /* When we parse the conversion-type-id, the current scope will be
8639 reset. However, we need that information in able to look up the
8640 conversion function later, so we save it here. */
8641 saved_scope = parser->scope;
8642 saved_qualifying_scope = parser->qualifying_scope;
8643 saved_object_scope = parser->object_scope;
8644 /* We must enter the scope of the class so that the names of
8645 entities declared within the class are available in the
8646 conversion-type-id. For example, consider:
8653 S::operator I() { ... }
8655 In order to see that `I' is a type-name in the definition, we
8656 must be in the scope of `S'. */
8658 pushed_scope = push_scope (saved_scope);
8659 /* Parse the conversion-type-id. */
8660 type = cp_parser_conversion_type_id (parser);
8661 /* Leave the scope of the class, if any. */
8663 pop_scope (pushed_scope);
8664 /* Restore the saved scope. */
8665 parser->scope = saved_scope;
8666 parser->qualifying_scope = saved_qualifying_scope;
8667 parser->object_scope = saved_object_scope;
8668 /* If the TYPE is invalid, indicate failure. */
8669 if (type == error_mark_node)
8670 return error_mark_node;
8671 return mangle_conv_op_name_for_type (type);
8674 /* Parse a conversion-type-id:
8677 type-specifier-seq conversion-declarator [opt]
8679 Returns the TYPE specified. */
8682 cp_parser_conversion_type_id (cp_parser* parser)
8685 cp_decl_specifier_seq type_specifiers;
8686 cp_declarator *declarator;
8687 tree type_specified;
8689 /* Parse the attributes. */
8690 attributes = cp_parser_attributes_opt (parser);
8691 /* Parse the type-specifiers. */
8692 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8694 /* If that didn't work, stop. */
8695 if (type_specifiers.type == error_mark_node)
8696 return error_mark_node;
8697 /* Parse the conversion-declarator. */
8698 declarator = cp_parser_conversion_declarator_opt (parser);
8700 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8701 /*initialized=*/0, &attributes);
8703 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8704 return type_specified;
8707 /* Parse an (optional) conversion-declarator.
8709 conversion-declarator:
8710 ptr-operator conversion-declarator [opt]
8714 static cp_declarator *
8715 cp_parser_conversion_declarator_opt (cp_parser* parser)
8717 enum tree_code code;
8719 cp_cv_quals cv_quals;
8721 /* We don't know if there's a ptr-operator next, or not. */
8722 cp_parser_parse_tentatively (parser);
8723 /* Try the ptr-operator. */
8724 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8725 /* If it worked, look for more conversion-declarators. */
8726 if (cp_parser_parse_definitely (parser))
8728 cp_declarator *declarator;
8730 /* Parse another optional declarator. */
8731 declarator = cp_parser_conversion_declarator_opt (parser);
8733 return cp_parser_make_indirect_declarator
8734 (code, class_type, cv_quals, declarator);
8740 /* Parse an (optional) ctor-initializer.
8743 : mem-initializer-list
8745 Returns TRUE iff the ctor-initializer was actually present. */
8748 cp_parser_ctor_initializer_opt (cp_parser* parser)
8750 /* If the next token is not a `:', then there is no
8751 ctor-initializer. */
8752 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8754 /* Do default initialization of any bases and members. */
8755 if (DECL_CONSTRUCTOR_P (current_function_decl))
8756 finish_mem_initializers (NULL_TREE);
8761 /* Consume the `:' token. */
8762 cp_lexer_consume_token (parser->lexer);
8763 /* And the mem-initializer-list. */
8764 cp_parser_mem_initializer_list (parser);
8769 /* Parse a mem-initializer-list.
8771 mem-initializer-list:
8772 mem-initializer ... [opt]
8773 mem-initializer ... [opt] , mem-initializer-list */
8776 cp_parser_mem_initializer_list (cp_parser* parser)
8778 tree mem_initializer_list = NULL_TREE;
8780 /* Let the semantic analysis code know that we are starting the
8781 mem-initializer-list. */
8782 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8783 error ("only constructors take base initializers");
8785 /* Loop through the list. */
8788 tree mem_initializer;
8790 /* Parse the mem-initializer. */
8791 mem_initializer = cp_parser_mem_initializer (parser);
8792 /* If the next token is a `...', we're expanding member initializers. */
8793 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8795 /* Consume the `...'. */
8796 cp_lexer_consume_token (parser->lexer);
8798 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8799 can be expanded but members cannot. */
8800 if (mem_initializer != error_mark_node
8801 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8803 error ("cannot expand initializer for member %<%D%>",
8804 TREE_PURPOSE (mem_initializer));
8805 mem_initializer = error_mark_node;
8808 /* Construct the pack expansion type. */
8809 if (mem_initializer != error_mark_node)
8810 mem_initializer = make_pack_expansion (mem_initializer);
8812 /* Add it to the list, unless it was erroneous. */
8813 if (mem_initializer != error_mark_node)
8815 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8816 mem_initializer_list = mem_initializer;
8818 /* If the next token is not a `,', we're done. */
8819 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8821 /* Consume the `,' token. */
8822 cp_lexer_consume_token (parser->lexer);
8825 /* Perform semantic analysis. */
8826 if (DECL_CONSTRUCTOR_P (current_function_decl))
8827 finish_mem_initializers (mem_initializer_list);
8830 /* Parse a mem-initializer.
8833 mem-initializer-id ( expression-list [opt] )
8838 ( expression-list [opt] )
8840 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8841 class) or FIELD_DECL (for a non-static data member) to initialize;
8842 the TREE_VALUE is the expression-list. An empty initialization
8843 list is represented by void_list_node. */
8846 cp_parser_mem_initializer (cp_parser* parser)
8848 tree mem_initializer_id;
8849 tree expression_list;
8852 /* Find out what is being initialized. */
8853 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8855 pedwarn ("anachronistic old-style base class initializer");
8856 mem_initializer_id = NULL_TREE;
8859 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8860 member = expand_member_init (mem_initializer_id);
8861 if (member && !DECL_P (member))
8862 in_base_initializer = 1;
8865 = cp_parser_parenthesized_expression_list (parser, false,
8867 /*allow_expansion_p=*/true,
8868 /*non_constant_p=*/NULL);
8869 if (expression_list == error_mark_node)
8870 return error_mark_node;
8871 if (!expression_list)
8872 expression_list = void_type_node;
8874 in_base_initializer = 0;
8876 return member ? build_tree_list (member, expression_list) : error_mark_node;
8879 /* Parse a mem-initializer-id.
8882 :: [opt] nested-name-specifier [opt] class-name
8885 Returns a TYPE indicating the class to be initializer for the first
8886 production. Returns an IDENTIFIER_NODE indicating the data member
8887 to be initialized for the second production. */
8890 cp_parser_mem_initializer_id (cp_parser* parser)
8892 bool global_scope_p;
8893 bool nested_name_specifier_p;
8894 bool template_p = false;
8897 /* `typename' is not allowed in this context ([temp.res]). */
8898 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8900 error ("keyword %<typename%> not allowed in this context (a qualified "
8901 "member initializer is implicitly a type)");
8902 cp_lexer_consume_token (parser->lexer);
8904 /* Look for the optional `::' operator. */
8906 = (cp_parser_global_scope_opt (parser,
8907 /*current_scope_valid_p=*/false)
8909 /* Look for the optional nested-name-specifier. The simplest way to
8914 The keyword `typename' is not permitted in a base-specifier or
8915 mem-initializer; in these contexts a qualified name that
8916 depends on a template-parameter is implicitly assumed to be a
8919 is to assume that we have seen the `typename' keyword at this
8921 nested_name_specifier_p
8922 = (cp_parser_nested_name_specifier_opt (parser,
8923 /*typename_keyword_p=*/true,
8924 /*check_dependency_p=*/true,
8926 /*is_declaration=*/true)
8928 if (nested_name_specifier_p)
8929 template_p = cp_parser_optional_template_keyword (parser);
8930 /* If there is a `::' operator or a nested-name-specifier, then we
8931 are definitely looking for a class-name. */
8932 if (global_scope_p || nested_name_specifier_p)
8933 return cp_parser_class_name (parser,
8934 /*typename_keyword_p=*/true,
8935 /*template_keyword_p=*/template_p,
8937 /*check_dependency_p=*/true,
8938 /*class_head_p=*/false,
8939 /*is_declaration=*/true);
8940 /* Otherwise, we could also be looking for an ordinary identifier. */
8941 cp_parser_parse_tentatively (parser);
8942 /* Try a class-name. */
8943 id = cp_parser_class_name (parser,
8944 /*typename_keyword_p=*/true,
8945 /*template_keyword_p=*/false,
8947 /*check_dependency_p=*/true,
8948 /*class_head_p=*/false,
8949 /*is_declaration=*/true);
8950 /* If we found one, we're done. */
8951 if (cp_parser_parse_definitely (parser))
8953 /* Otherwise, look for an ordinary identifier. */
8954 return cp_parser_identifier (parser);
8957 /* Overloading [gram.over] */
8959 /* Parse an operator-function-id.
8961 operator-function-id:
8964 Returns an IDENTIFIER_NODE for the operator which is a
8965 human-readable spelling of the identifier, e.g., `operator +'. */
8968 cp_parser_operator_function_id (cp_parser* parser)
8970 /* Look for the `operator' keyword. */
8971 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8972 return error_mark_node;
8973 /* And then the name of the operator itself. */
8974 return cp_parser_operator (parser);
8977 /* Parse an operator.
8980 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8981 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8982 || ++ -- , ->* -> () []
8989 Returns an IDENTIFIER_NODE for the operator which is a
8990 human-readable spelling of the identifier, e.g., `operator +'. */
8993 cp_parser_operator (cp_parser* parser)
8995 tree id = NULL_TREE;
8998 /* Peek at the next token. */
8999 token = cp_lexer_peek_token (parser->lexer);
9000 /* Figure out which operator we have. */
9001 switch (token->type)
9007 /* The keyword should be either `new' or `delete'. */
9008 if (token->keyword == RID_NEW)
9010 else if (token->keyword == RID_DELETE)
9015 /* Consume the `new' or `delete' token. */
9016 cp_lexer_consume_token (parser->lexer);
9018 /* Peek at the next token. */
9019 token = cp_lexer_peek_token (parser->lexer);
9020 /* If it's a `[' token then this is the array variant of the
9022 if (token->type == CPP_OPEN_SQUARE)
9024 /* Consume the `[' token. */
9025 cp_lexer_consume_token (parser->lexer);
9026 /* Look for the `]' token. */
9027 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9028 id = ansi_opname (op == NEW_EXPR
9029 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
9031 /* Otherwise, we have the non-array variant. */
9033 id = ansi_opname (op);
9039 id = ansi_opname (PLUS_EXPR);
9043 id = ansi_opname (MINUS_EXPR);
9047 id = ansi_opname (MULT_EXPR);
9051 id = ansi_opname (TRUNC_DIV_EXPR);
9055 id = ansi_opname (TRUNC_MOD_EXPR);
9059 id = ansi_opname (BIT_XOR_EXPR);
9063 id = ansi_opname (BIT_AND_EXPR);
9067 id = ansi_opname (BIT_IOR_EXPR);
9071 id = ansi_opname (BIT_NOT_EXPR);
9075 id = ansi_opname (TRUTH_NOT_EXPR);
9079 id = ansi_assopname (NOP_EXPR);
9083 id = ansi_opname (LT_EXPR);
9087 id = ansi_opname (GT_EXPR);
9091 id = ansi_assopname (PLUS_EXPR);
9095 id = ansi_assopname (MINUS_EXPR);
9099 id = ansi_assopname (MULT_EXPR);
9103 id = ansi_assopname (TRUNC_DIV_EXPR);
9107 id = ansi_assopname (TRUNC_MOD_EXPR);
9111 id = ansi_assopname (BIT_XOR_EXPR);
9115 id = ansi_assopname (BIT_AND_EXPR);
9119 id = ansi_assopname (BIT_IOR_EXPR);
9123 id = ansi_opname (LSHIFT_EXPR);
9127 id = ansi_opname (RSHIFT_EXPR);
9131 id = ansi_assopname (LSHIFT_EXPR);
9135 id = ansi_assopname (RSHIFT_EXPR);
9139 id = ansi_opname (EQ_EXPR);
9143 id = ansi_opname (NE_EXPR);
9147 id = ansi_opname (LE_EXPR);
9150 case CPP_GREATER_EQ:
9151 id = ansi_opname (GE_EXPR);
9155 id = ansi_opname (TRUTH_ANDIF_EXPR);
9159 id = ansi_opname (TRUTH_ORIF_EXPR);
9163 id = ansi_opname (POSTINCREMENT_EXPR);
9166 case CPP_MINUS_MINUS:
9167 id = ansi_opname (PREDECREMENT_EXPR);
9171 id = ansi_opname (COMPOUND_EXPR);
9174 case CPP_DEREF_STAR:
9175 id = ansi_opname (MEMBER_REF);
9179 id = ansi_opname (COMPONENT_REF);
9182 case CPP_OPEN_PAREN:
9183 /* Consume the `('. */
9184 cp_lexer_consume_token (parser->lexer);
9185 /* Look for the matching `)'. */
9186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9187 return ansi_opname (CALL_EXPR);
9189 case CPP_OPEN_SQUARE:
9190 /* Consume the `['. */
9191 cp_lexer_consume_token (parser->lexer);
9192 /* Look for the matching `]'. */
9193 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9194 return ansi_opname (ARRAY_REF);
9197 /* Anything else is an error. */
9201 /* If we have selected an identifier, we need to consume the
9204 cp_lexer_consume_token (parser->lexer);
9205 /* Otherwise, no valid operator name was present. */
9208 cp_parser_error (parser, "expected operator");
9209 id = error_mark_node;
9215 /* Parse a template-declaration.
9217 template-declaration:
9218 export [opt] template < template-parameter-list > declaration
9220 If MEMBER_P is TRUE, this template-declaration occurs within a
9223 The grammar rule given by the standard isn't correct. What
9226 template-declaration:
9227 export [opt] template-parameter-list-seq
9228 decl-specifier-seq [opt] init-declarator [opt] ;
9229 export [opt] template-parameter-list-seq
9232 template-parameter-list-seq:
9233 template-parameter-list-seq [opt]
9234 template < template-parameter-list > */
9237 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9239 /* Check for `export'. */
9240 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9242 /* Consume the `export' token. */
9243 cp_lexer_consume_token (parser->lexer);
9244 /* Warn that we do not support `export'. */
9245 warning (0, "keyword %<export%> not implemented, and will be ignored");
9248 cp_parser_template_declaration_after_export (parser, member_p);
9251 /* Parse a template-parameter-list.
9253 template-parameter-list:
9255 template-parameter-list , template-parameter
9257 Returns a TREE_LIST. Each node represents a template parameter.
9258 The nodes are connected via their TREE_CHAINs. */
9261 cp_parser_template_parameter_list (cp_parser* parser)
9263 tree parameter_list = NULL_TREE;
9265 begin_template_parm_list ();
9271 bool is_parameter_pack;
9273 /* Parse the template-parameter. */
9274 parameter = cp_parser_template_parameter (parser,
9276 &is_parameter_pack);
9277 /* Add it to the list. */
9278 if (parameter != error_mark_node)
9279 parameter_list = process_template_parm (parameter_list,
9285 tree err_parm = build_tree_list (parameter, parameter);
9286 TREE_VALUE (err_parm) = error_mark_node;
9287 parameter_list = chainon (parameter_list, err_parm);
9290 /* Peek at the next token. */
9291 token = cp_lexer_peek_token (parser->lexer);
9292 /* If it's not a `,', we're done. */
9293 if (token->type != CPP_COMMA)
9295 /* Otherwise, consume the `,' token. */
9296 cp_lexer_consume_token (parser->lexer);
9299 return end_template_parm_list (parameter_list);
9302 /* Parse a template-parameter.
9306 parameter-declaration
9308 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9309 the parameter. The TREE_PURPOSE is the default value, if any.
9310 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9311 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9312 set to true iff this parameter is a parameter pack. */
9315 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9316 bool *is_parameter_pack)
9319 cp_parameter_declarator *parameter_declarator;
9320 cp_declarator *id_declarator;
9323 /* Assume it is a type parameter or a template parameter. */
9324 *is_non_type = false;
9325 /* Assume it not a parameter pack. */
9326 *is_parameter_pack = false;
9327 /* Peek at the next token. */
9328 token = cp_lexer_peek_token (parser->lexer);
9329 /* If it is `class' or `template', we have a type-parameter. */
9330 if (token->keyword == RID_TEMPLATE)
9331 return cp_parser_type_parameter (parser, is_parameter_pack);
9332 /* If it is `class' or `typename' we do not know yet whether it is a
9333 type parameter or a non-type parameter. Consider:
9335 template <typename T, typename T::X X> ...
9339 template <class C, class D*> ...
9341 Here, the first parameter is a type parameter, and the second is
9342 a non-type parameter. We can tell by looking at the token after
9343 the identifier -- if it is a `,', `=', or `>' then we have a type
9345 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9347 /* Peek at the token after `class' or `typename'. */
9348 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9349 /* If it's an ellipsis, we have a template type parameter
9351 if (token->type == CPP_ELLIPSIS)
9352 return cp_parser_type_parameter (parser, is_parameter_pack);
9353 /* If it's an identifier, skip it. */
9354 if (token->type == CPP_NAME)
9355 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9356 /* Now, see if the token looks like the end of a template
9358 if (token->type == CPP_COMMA
9359 || token->type == CPP_EQ
9360 || token->type == CPP_GREATER)
9361 return cp_parser_type_parameter (parser, is_parameter_pack);
9364 /* Otherwise, it is a non-type parameter.
9368 When parsing a default template-argument for a non-type
9369 template-parameter, the first non-nested `>' is taken as the end
9370 of the template parameter-list rather than a greater-than
9372 *is_non_type = true;
9373 parameter_declarator
9374 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9375 /*parenthesized_p=*/NULL);
9377 /* If the parameter declaration is marked as a parameter pack, set
9378 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9379 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9381 if (parameter_declarator
9382 && parameter_declarator->declarator
9383 && parameter_declarator->declarator->parameter_pack_p)
9385 *is_parameter_pack = true;
9386 parameter_declarator->declarator->parameter_pack_p = false;
9389 /* If the next token is an ellipsis, and we don't already have it
9390 marked as a parameter pack, then we have a parameter pack (that
9391 has no declarator). */
9392 if (!*is_parameter_pack
9393 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9394 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9396 /* Consume the `...'. */
9397 cp_lexer_consume_token (parser->lexer);
9398 maybe_warn_variadic_templates ();
9400 *is_parameter_pack = true;
9402 /* Parameter packs cannot have default arguments. However, a
9403 user may try to do so, so we'll parse them and give an
9404 appropriate diagnostic here. */
9405 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9407 /* Consume the `='. */
9408 cp_lexer_consume_token (parser->lexer);
9410 /* Find the name of the parameter pack. */
9411 id_declarator = parameter_declarator->declarator;
9412 while (id_declarator && id_declarator->kind != cdk_id)
9413 id_declarator = id_declarator->declarator;
9415 if (id_declarator && id_declarator->kind == cdk_id)
9416 error ("template parameter pack %qD cannot have a default argument",
9417 id_declarator->u.id.unqualified_name);
9419 error ("template parameter pack cannot have a default argument");
9421 /* Parse the default argument, but throw away the result. */
9422 cp_parser_default_argument (parser, /*template_parm_p=*/true);
9426 parm = grokdeclarator (parameter_declarator->declarator,
9427 ¶meter_declarator->decl_specifiers,
9428 PARM, /*initialized=*/0,
9430 if (parm == error_mark_node)
9431 return error_mark_node;
9433 return build_tree_list (parameter_declarator->default_argument, parm);
9436 /* Parse a type-parameter.
9439 class identifier [opt]
9440 class identifier [opt] = type-id
9441 typename identifier [opt]
9442 typename identifier [opt] = type-id
9443 template < template-parameter-list > class identifier [opt]
9444 template < template-parameter-list > class identifier [opt]
9447 GNU Extension (variadic templates):
9450 class ... identifier [opt]
9451 typename ... identifier [opt]
9453 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9454 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9455 the declaration of the parameter.
9457 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9460 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9465 /* Look for a keyword to tell us what kind of parameter this is. */
9466 token = cp_parser_require (parser, CPP_KEYWORD,
9467 "`class', `typename', or `template'");
9469 return error_mark_node;
9471 switch (token->keyword)
9477 tree default_argument;
9479 /* If the next token is an ellipsis, we have a template
9481 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9483 /* Consume the `...' token. */
9484 cp_lexer_consume_token (parser->lexer);
9485 maybe_warn_variadic_templates ();
9487 *is_parameter_pack = true;
9490 /* If the next token is an identifier, then it names the
9492 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9493 identifier = cp_parser_identifier (parser);
9495 identifier = NULL_TREE;
9497 /* Create the parameter. */
9498 parameter = finish_template_type_parm (class_type_node, identifier);
9500 /* If the next token is an `=', we have a default argument. */
9501 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9503 /* Consume the `=' token. */
9504 cp_lexer_consume_token (parser->lexer);
9505 /* Parse the default-argument. */
9506 push_deferring_access_checks (dk_no_deferred);
9507 default_argument = cp_parser_type_id (parser);
9509 /* Template parameter packs cannot have default
9511 if (*is_parameter_pack)
9514 error ("template parameter pack %qD cannot have a default argument",
9517 error ("template parameter packs cannot have default arguments");
9518 default_argument = NULL_TREE;
9520 pop_deferring_access_checks ();
9523 default_argument = NULL_TREE;
9525 /* Create the combined representation of the parameter and the
9526 default argument. */
9527 parameter = build_tree_list (default_argument, parameter);
9533 tree parameter_list;
9535 tree default_argument;
9537 /* Look for the `<'. */
9538 cp_parser_require (parser, CPP_LESS, "`<'");
9539 /* Parse the template-parameter-list. */
9540 parameter_list = cp_parser_template_parameter_list (parser);
9541 /* Look for the `>'. */
9542 cp_parser_require (parser, CPP_GREATER, "`>'");
9543 /* Look for the `class' keyword. */
9544 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9545 /* If the next token is an ellipsis, we have a template
9547 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9549 /* Consume the `...' token. */
9550 cp_lexer_consume_token (parser->lexer);
9551 maybe_warn_variadic_templates ();
9553 *is_parameter_pack = true;
9555 /* If the next token is an `=', then there is a
9556 default-argument. If the next token is a `>', we are at
9557 the end of the parameter-list. If the next token is a `,',
9558 then we are at the end of this parameter. */
9559 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9560 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9561 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9563 identifier = cp_parser_identifier (parser);
9564 /* Treat invalid names as if the parameter were nameless. */
9565 if (identifier == error_mark_node)
9566 identifier = NULL_TREE;
9569 identifier = NULL_TREE;
9571 /* Create the template parameter. */
9572 parameter = finish_template_template_parm (class_type_node,
9575 /* If the next token is an `=', then there is a
9576 default-argument. */
9577 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9581 /* Consume the `='. */
9582 cp_lexer_consume_token (parser->lexer);
9583 /* Parse the id-expression. */
9584 push_deferring_access_checks (dk_no_deferred);
9586 = cp_parser_id_expression (parser,
9587 /*template_keyword_p=*/false,
9588 /*check_dependency_p=*/true,
9589 /*template_p=*/&is_template,
9590 /*declarator_p=*/false,
9591 /*optional_p=*/false);
9592 if (TREE_CODE (default_argument) == TYPE_DECL)
9593 /* If the id-expression was a template-id that refers to
9594 a template-class, we already have the declaration here,
9595 so no further lookup is needed. */
9598 /* Look up the name. */
9600 = cp_parser_lookup_name (parser, default_argument,
9602 /*is_template=*/is_template,
9603 /*is_namespace=*/false,
9604 /*check_dependency=*/true,
9605 /*ambiguous_decls=*/NULL);
9606 /* See if the default argument is valid. */
9608 = check_template_template_default_arg (default_argument);
9610 /* Template parameter packs cannot have default
9612 if (*is_parameter_pack)
9615 error ("template parameter pack %qD cannot have a default argument",
9618 error ("template parameter packs cannot have default arguments");
9619 default_argument = NULL_TREE;
9621 pop_deferring_access_checks ();
9624 default_argument = NULL_TREE;
9626 /* Create the combined representation of the parameter and the
9627 default argument. */
9628 parameter = build_tree_list (default_argument, parameter);
9640 /* Parse a template-id.
9643 template-name < template-argument-list [opt] >
9645 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9646 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9647 returned. Otherwise, if the template-name names a function, or set
9648 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9649 names a class, returns a TYPE_DECL for the specialization.
9651 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9652 uninstantiated templates. */
9655 cp_parser_template_id (cp_parser *parser,
9656 bool template_keyword_p,
9657 bool check_dependency_p,
9658 bool is_declaration)
9664 cp_token_position start_of_id = 0;
9665 deferred_access_check *chk;
9666 VEC (deferred_access_check,gc) *access_check;
9667 cp_token *next_token, *next_token_2;
9670 /* If the next token corresponds to a template-id, there is no need
9672 next_token = cp_lexer_peek_token (parser->lexer);
9673 if (next_token->type == CPP_TEMPLATE_ID)
9675 struct tree_check *check_value;
9677 /* Get the stored value. */
9678 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9679 /* Perform any access checks that were deferred. */
9680 access_check = check_value->checks;
9684 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9687 perform_or_defer_access_check (chk->binfo,
9692 /* Return the stored value. */
9693 return check_value->value;
9696 /* Avoid performing name lookup if there is no possibility of
9697 finding a template-id. */
9698 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9699 || (next_token->type == CPP_NAME
9700 && !cp_parser_nth_token_starts_template_argument_list_p
9703 cp_parser_error (parser, "expected template-id");
9704 return error_mark_node;
9707 /* Remember where the template-id starts. */
9708 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9709 start_of_id = cp_lexer_token_position (parser->lexer, false);
9711 push_deferring_access_checks (dk_deferred);
9713 /* Parse the template-name. */
9714 is_identifier = false;
9715 template = cp_parser_template_name (parser, template_keyword_p,
9719 if (template == error_mark_node || is_identifier)
9721 pop_deferring_access_checks ();
9725 /* If we find the sequence `[:' after a template-name, it's probably
9726 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9727 parse correctly the argument list. */
9728 next_token = cp_lexer_peek_token (parser->lexer);
9729 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9730 if (next_token->type == CPP_OPEN_SQUARE
9731 && next_token->flags & DIGRAPH
9732 && next_token_2->type == CPP_COLON
9733 && !(next_token_2->flags & PREV_WHITE))
9735 cp_parser_parse_tentatively (parser);
9736 /* Change `:' into `::'. */
9737 next_token_2->type = CPP_SCOPE;
9738 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9740 cp_lexer_consume_token (parser->lexer);
9741 /* Parse the arguments. */
9742 arguments = cp_parser_enclosed_template_argument_list (parser);
9743 if (!cp_parser_parse_definitely (parser))
9745 /* If we couldn't parse an argument list, then we revert our changes
9746 and return simply an error. Maybe this is not a template-id
9748 next_token_2->type = CPP_COLON;
9749 cp_parser_error (parser, "expected %<<%>");
9750 pop_deferring_access_checks ();
9751 return error_mark_node;
9753 /* Otherwise, emit an error about the invalid digraph, but continue
9754 parsing because we got our argument list. */
9755 pedwarn ("%<<::%> cannot begin a template-argument list");
9756 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9757 "between %<<%> and %<::%>");
9758 if (!flag_permissive)
9763 inform ("(if you use -fpermissive G++ will accept your code)");
9770 /* Look for the `<' that starts the template-argument-list. */
9771 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9773 pop_deferring_access_checks ();
9774 return error_mark_node;
9776 /* Parse the arguments. */
9777 arguments = cp_parser_enclosed_template_argument_list (parser);
9780 /* Build a representation of the specialization. */
9781 if (TREE_CODE (template) == IDENTIFIER_NODE)
9782 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9783 else if (DECL_CLASS_TEMPLATE_P (template)
9784 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9786 bool entering_scope;
9787 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9788 template (rather than some instantiation thereof) only if
9789 is not nested within some other construct. For example, in
9790 "template <typename T> void f(T) { A<T>::", A<T> is just an
9791 instantiation of A. */
9792 entering_scope = (template_parm_scope_p ()
9793 && cp_lexer_next_token_is (parser->lexer,
9796 = finish_template_type (template, arguments, entering_scope);
9800 /* If it's not a class-template or a template-template, it should be
9801 a function-template. */
9802 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9803 || TREE_CODE (template) == OVERLOAD
9804 || BASELINK_P (template)));
9806 template_id = lookup_template_function (template, arguments);
9809 /* If parsing tentatively, replace the sequence of tokens that makes
9810 up the template-id with a CPP_TEMPLATE_ID token. That way,
9811 should we re-parse the token stream, we will not have to repeat
9812 the effort required to do the parse, nor will we issue duplicate
9813 error messages about problems during instantiation of the
9817 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9819 /* Reset the contents of the START_OF_ID token. */
9820 token->type = CPP_TEMPLATE_ID;
9821 /* Retrieve any deferred checks. Do not pop this access checks yet
9822 so the memory will not be reclaimed during token replacing below. */
9823 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9824 token->u.tree_check_value->value = template_id;
9825 token->u.tree_check_value->checks = get_deferred_access_checks ();
9826 token->keyword = RID_MAX;
9828 /* Purge all subsequent tokens. */
9829 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9831 /* ??? Can we actually assume that, if template_id ==
9832 error_mark_node, we will have issued a diagnostic to the
9833 user, as opposed to simply marking the tentative parse as
9835 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9836 error ("parse error in template argument list");
9839 pop_deferring_access_checks ();
9843 /* Parse a template-name.
9848 The standard should actually say:
9852 operator-function-id
9854 A defect report has been filed about this issue.
9856 A conversion-function-id cannot be a template name because they cannot
9857 be part of a template-id. In fact, looking at this code:
9861 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9862 It is impossible to call a templated conversion-function-id with an
9863 explicit argument list, since the only allowed template parameter is
9864 the type to which it is converting.
9866 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9867 `template' keyword, in a construction like:
9871 In that case `f' is taken to be a template-name, even though there
9872 is no way of knowing for sure.
9874 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9875 name refers to a set of overloaded functions, at least one of which
9876 is a template, or an IDENTIFIER_NODE with the name of the template,
9877 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9878 names are looked up inside uninstantiated templates. */
9881 cp_parser_template_name (cp_parser* parser,
9882 bool template_keyword_p,
9883 bool check_dependency_p,
9884 bool is_declaration,
9885 bool *is_identifier)
9891 /* If the next token is `operator', then we have either an
9892 operator-function-id or a conversion-function-id. */
9893 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9895 /* We don't know whether we're looking at an
9896 operator-function-id or a conversion-function-id. */
9897 cp_parser_parse_tentatively (parser);
9898 /* Try an operator-function-id. */
9899 identifier = cp_parser_operator_function_id (parser);
9900 /* If that didn't work, try a conversion-function-id. */
9901 if (!cp_parser_parse_definitely (parser))
9903 cp_parser_error (parser, "expected template-name");
9904 return error_mark_node;
9907 /* Look for the identifier. */
9909 identifier = cp_parser_identifier (parser);
9911 /* If we didn't find an identifier, we don't have a template-id. */
9912 if (identifier == error_mark_node)
9913 return error_mark_node;
9915 /* If the name immediately followed the `template' keyword, then it
9916 is a template-name. However, if the next token is not `<', then
9917 we do not treat it as a template-name, since it is not being used
9918 as part of a template-id. This enables us to handle constructs
9921 template <typename T> struct S { S(); };
9922 template <typename T> S<T>::S();
9924 correctly. We would treat `S' as a template -- if it were `S<T>'
9925 -- but we do not if there is no `<'. */
9927 if (processing_template_decl
9928 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9930 /* In a declaration, in a dependent context, we pretend that the
9931 "template" keyword was present in order to improve error
9932 recovery. For example, given:
9934 template <typename T> void f(T::X<int>);
9936 we want to treat "X<int>" as a template-id. */
9938 && !template_keyword_p
9939 && parser->scope && TYPE_P (parser->scope)
9940 && check_dependency_p
9941 && dependent_type_p (parser->scope)
9942 /* Do not do this for dtors (or ctors), since they never
9943 need the template keyword before their name. */
9944 && !constructor_name_p (identifier, parser->scope))
9946 cp_token_position start = 0;
9948 /* Explain what went wrong. */
9949 error ("non-template %qD used as template", identifier);
9950 inform ("use %<%T::template %D%> to indicate that it is a template",
9951 parser->scope, identifier);
9952 /* If parsing tentatively, find the location of the "<" token. */
9953 if (cp_parser_simulate_error (parser))
9954 start = cp_lexer_token_position (parser->lexer, true);
9955 /* Parse the template arguments so that we can issue error
9956 messages about them. */
9957 cp_lexer_consume_token (parser->lexer);
9958 cp_parser_enclosed_template_argument_list (parser);
9959 /* Skip tokens until we find a good place from which to
9960 continue parsing. */
9961 cp_parser_skip_to_closing_parenthesis (parser,
9962 /*recovering=*/true,
9964 /*consume_paren=*/false);
9965 /* If parsing tentatively, permanently remove the
9966 template argument list. That will prevent duplicate
9967 error messages from being issued about the missing
9968 "template" keyword. */
9970 cp_lexer_purge_tokens_after (parser->lexer, start);
9972 *is_identifier = true;
9976 /* If the "template" keyword is present, then there is generally
9977 no point in doing name-lookup, so we just return IDENTIFIER.
9978 But, if the qualifying scope is non-dependent then we can
9979 (and must) do name-lookup normally. */
9980 if (template_keyword_p
9982 || (TYPE_P (parser->scope)
9983 && dependent_type_p (parser->scope))))
9987 /* Look up the name. */
9988 decl = cp_parser_lookup_name (parser, identifier,
9990 /*is_template=*/false,
9991 /*is_namespace=*/false,
9993 /*ambiguous_decls=*/NULL);
9994 decl = maybe_get_template_decl_from_type_decl (decl);
9996 /* If DECL is a template, then the name was a template-name. */
9997 if (TREE_CODE (decl) == TEMPLATE_DECL)
10001 tree fn = NULL_TREE;
10003 /* The standard does not explicitly indicate whether a name that
10004 names a set of overloaded declarations, some of which are
10005 templates, is a template-name. However, such a name should
10006 be a template-name; otherwise, there is no way to form a
10007 template-id for the overloaded templates. */
10008 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
10009 if (TREE_CODE (fns) == OVERLOAD)
10010 for (fn = fns; fn; fn = OVL_NEXT (fn))
10011 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
10016 /* The name does not name a template. */
10017 cp_parser_error (parser, "expected template-name");
10018 return error_mark_node;
10022 /* If DECL is dependent, and refers to a function, then just return
10023 its name; we will look it up again during template instantiation. */
10024 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
10026 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
10027 if (TYPE_P (scope) && dependent_type_p (scope))
10034 /* Parse a template-argument-list.
10036 template-argument-list:
10037 template-argument ... [opt]
10038 template-argument-list , template-argument ... [opt]
10040 Returns a TREE_VEC containing the arguments. */
10043 cp_parser_template_argument_list (cp_parser* parser)
10045 tree fixed_args[10];
10046 unsigned n_args = 0;
10047 unsigned alloced = 10;
10048 tree *arg_ary = fixed_args;
10050 bool saved_in_template_argument_list_p;
10052 bool saved_non_ice_p;
10054 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
10055 parser->in_template_argument_list_p = true;
10056 /* Even if the template-id appears in an integral
10057 constant-expression, the contents of the argument list do
10059 saved_ice_p = parser->integral_constant_expression_p;
10060 parser->integral_constant_expression_p = false;
10061 saved_non_ice_p = parser->non_integral_constant_expression_p;
10062 parser->non_integral_constant_expression_p = false;
10063 /* Parse the arguments. */
10069 /* Consume the comma. */
10070 cp_lexer_consume_token (parser->lexer);
10072 /* Parse the template-argument. */
10073 argument = cp_parser_template_argument (parser);
10075 /* If the next token is an ellipsis, we're expanding a template
10077 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10079 /* Consume the `...' token. */
10080 cp_lexer_consume_token (parser->lexer);
10082 /* Make the argument into a TYPE_PACK_EXPANSION or
10083 EXPR_PACK_EXPANSION. */
10084 argument = make_pack_expansion (argument);
10087 if (n_args == alloced)
10091 if (arg_ary == fixed_args)
10093 arg_ary = XNEWVEC (tree, alloced);
10094 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10097 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10099 arg_ary[n_args++] = argument;
10101 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10103 vec = make_tree_vec (n_args);
10106 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10108 if (arg_ary != fixed_args)
10110 parser->non_integral_constant_expression_p = saved_non_ice_p;
10111 parser->integral_constant_expression_p = saved_ice_p;
10112 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10116 /* Parse a template-argument.
10119 assignment-expression
10123 The representation is that of an assignment-expression, type-id, or
10124 id-expression -- except that the qualified id-expression is
10125 evaluated, so that the value returned is either a DECL or an
10128 Although the standard says "assignment-expression", it forbids
10129 throw-expressions or assignments in the template argument.
10130 Therefore, we use "conditional-expression" instead. */
10133 cp_parser_template_argument (cp_parser* parser)
10138 bool maybe_type_id = false;
10142 /* There's really no way to know what we're looking at, so we just
10143 try each alternative in order.
10147 In a template-argument, an ambiguity between a type-id and an
10148 expression is resolved to a type-id, regardless of the form of
10149 the corresponding template-parameter.
10151 Therefore, we try a type-id first. */
10152 cp_parser_parse_tentatively (parser);
10153 argument = cp_parser_type_id (parser);
10154 /* If there was no error parsing the type-id but the next token is a '>>',
10155 we probably found a typo for '> >'. But there are type-id which are
10156 also valid expressions. For instance:
10158 struct X { int operator >> (int); };
10159 template <int V> struct Foo {};
10162 Here 'X()' is a valid type-id of a function type, but the user just
10163 wanted to write the expression "X() >> 5". Thus, we remember that we
10164 found a valid type-id, but we still try to parse the argument as an
10165 expression to see what happens. */
10166 if (!cp_parser_error_occurred (parser)
10167 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10169 maybe_type_id = true;
10170 cp_parser_abort_tentative_parse (parser);
10174 /* If the next token isn't a `,' or a `>', then this argument wasn't
10175 really finished. This means that the argument is not a valid
10177 if (!cp_parser_next_token_ends_template_argument_p (parser))
10178 cp_parser_error (parser, "expected template-argument");
10179 /* If that worked, we're done. */
10180 if (cp_parser_parse_definitely (parser))
10183 /* We're still not sure what the argument will be. */
10184 cp_parser_parse_tentatively (parser);
10185 /* Try a template. */
10186 argument = cp_parser_id_expression (parser,
10187 /*template_keyword_p=*/false,
10188 /*check_dependency_p=*/true,
10190 /*declarator_p=*/false,
10191 /*optional_p=*/false);
10192 /* If the next token isn't a `,' or a `>', then this argument wasn't
10193 really finished. */
10194 if (!cp_parser_next_token_ends_template_argument_p (parser))
10195 cp_parser_error (parser, "expected template-argument");
10196 if (!cp_parser_error_occurred (parser))
10198 /* Figure out what is being referred to. If the id-expression
10199 was for a class template specialization, then we will have a
10200 TYPE_DECL at this point. There is no need to do name lookup
10201 at this point in that case. */
10202 if (TREE_CODE (argument) != TYPE_DECL)
10203 argument = cp_parser_lookup_name (parser, argument,
10205 /*is_template=*/template_p,
10206 /*is_namespace=*/false,
10207 /*check_dependency=*/true,
10208 /*ambiguous_decls=*/NULL);
10209 if (TREE_CODE (argument) != TEMPLATE_DECL
10210 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10211 cp_parser_error (parser, "expected template-name");
10213 if (cp_parser_parse_definitely (parser))
10215 /* It must be a non-type argument. There permitted cases are given
10216 in [temp.arg.nontype]:
10218 -- an integral constant-expression of integral or enumeration
10221 -- the name of a non-type template-parameter; or
10223 -- the name of an object or function with external linkage...
10225 -- the address of an object or function with external linkage...
10227 -- a pointer to member... */
10228 /* Look for a non-type template parameter. */
10229 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10231 cp_parser_parse_tentatively (parser);
10232 argument = cp_parser_primary_expression (parser,
10233 /*adress_p=*/false,
10235 /*template_arg_p=*/true,
10237 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10238 || !cp_parser_next_token_ends_template_argument_p (parser))
10239 cp_parser_simulate_error (parser);
10240 if (cp_parser_parse_definitely (parser))
10244 /* If the next token is "&", the argument must be the address of an
10245 object or function with external linkage. */
10246 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10248 cp_lexer_consume_token (parser->lexer);
10249 /* See if we might have an id-expression. */
10250 token = cp_lexer_peek_token (parser->lexer);
10251 if (token->type == CPP_NAME
10252 || token->keyword == RID_OPERATOR
10253 || token->type == CPP_SCOPE
10254 || token->type == CPP_TEMPLATE_ID
10255 || token->type == CPP_NESTED_NAME_SPECIFIER)
10257 cp_parser_parse_tentatively (parser);
10258 argument = cp_parser_primary_expression (parser,
10261 /*template_arg_p=*/true,
10263 if (cp_parser_error_occurred (parser)
10264 || !cp_parser_next_token_ends_template_argument_p (parser))
10265 cp_parser_abort_tentative_parse (parser);
10268 if (TREE_CODE (argument) == INDIRECT_REF)
10270 gcc_assert (REFERENCE_REF_P (argument));
10271 argument = TREE_OPERAND (argument, 0);
10274 if (TREE_CODE (argument) == VAR_DECL)
10276 /* A variable without external linkage might still be a
10277 valid constant-expression, so no error is issued here
10278 if the external-linkage check fails. */
10279 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10280 cp_parser_simulate_error (parser);
10282 else if (is_overloaded_fn (argument))
10283 /* All overloaded functions are allowed; if the external
10284 linkage test does not pass, an error will be issued
10288 && (TREE_CODE (argument) == OFFSET_REF
10289 || TREE_CODE (argument) == SCOPE_REF))
10290 /* A pointer-to-member. */
10292 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10295 cp_parser_simulate_error (parser);
10297 if (cp_parser_parse_definitely (parser))
10300 argument = build_x_unary_op (ADDR_EXPR, argument);
10305 /* If the argument started with "&", there are no other valid
10306 alternatives at this point. */
10309 cp_parser_error (parser, "invalid non-type template argument");
10310 return error_mark_node;
10313 /* If the argument wasn't successfully parsed as a type-id followed
10314 by '>>', the argument can only be a constant expression now.
10315 Otherwise, we try parsing the constant-expression tentatively,
10316 because the argument could really be a type-id. */
10318 cp_parser_parse_tentatively (parser);
10319 argument = cp_parser_constant_expression (parser,
10320 /*allow_non_constant_p=*/false,
10321 /*non_constant_p=*/NULL);
10322 argument = fold_non_dependent_expr (argument);
10323 if (!maybe_type_id)
10325 if (!cp_parser_next_token_ends_template_argument_p (parser))
10326 cp_parser_error (parser, "expected template-argument");
10327 if (cp_parser_parse_definitely (parser))
10329 /* We did our best to parse the argument as a non type-id, but that
10330 was the only alternative that matched (albeit with a '>' after
10331 it). We can assume it's just a typo from the user, and a
10332 diagnostic will then be issued. */
10333 return cp_parser_type_id (parser);
10336 /* Parse an explicit-instantiation.
10338 explicit-instantiation:
10339 template declaration
10341 Although the standard says `declaration', what it really means is:
10343 explicit-instantiation:
10344 template decl-specifier-seq [opt] declarator [opt] ;
10346 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10347 supposed to be allowed. A defect report has been filed about this
10352 explicit-instantiation:
10353 storage-class-specifier template
10354 decl-specifier-seq [opt] declarator [opt] ;
10355 function-specifier template
10356 decl-specifier-seq [opt] declarator [opt] ; */
10359 cp_parser_explicit_instantiation (cp_parser* parser)
10361 int declares_class_or_enum;
10362 cp_decl_specifier_seq decl_specifiers;
10363 tree extension_specifier = NULL_TREE;
10365 /* Look for an (optional) storage-class-specifier or
10366 function-specifier. */
10367 if (cp_parser_allow_gnu_extensions_p (parser))
10369 extension_specifier
10370 = cp_parser_storage_class_specifier_opt (parser);
10371 if (!extension_specifier)
10372 extension_specifier
10373 = cp_parser_function_specifier_opt (parser,
10374 /*decl_specs=*/NULL);
10377 /* Look for the `template' keyword. */
10378 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10379 /* Let the front end know that we are processing an explicit
10381 begin_explicit_instantiation ();
10382 /* [temp.explicit] says that we are supposed to ignore access
10383 control while processing explicit instantiation directives. */
10384 push_deferring_access_checks (dk_no_check);
10385 /* Parse a decl-specifier-seq. */
10386 cp_parser_decl_specifier_seq (parser,
10387 CP_PARSER_FLAGS_OPTIONAL,
10389 &declares_class_or_enum);
10390 /* If there was exactly one decl-specifier, and it declared a class,
10391 and there's no declarator, then we have an explicit type
10393 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10397 type = check_tag_decl (&decl_specifiers);
10398 /* Turn access control back on for names used during
10399 template instantiation. */
10400 pop_deferring_access_checks ();
10402 do_type_instantiation (type, extension_specifier,
10403 /*complain=*/tf_error);
10407 cp_declarator *declarator;
10410 /* Parse the declarator. */
10412 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10413 /*ctor_dtor_or_conv_p=*/NULL,
10414 /*parenthesized_p=*/NULL,
10415 /*member_p=*/false);
10416 if (declares_class_or_enum & 2)
10417 cp_parser_check_for_definition_in_return_type (declarator,
10418 decl_specifiers.type);
10419 if (declarator != cp_error_declarator)
10421 decl = grokdeclarator (declarator, &decl_specifiers,
10422 NORMAL, 0, &decl_specifiers.attributes);
10423 /* Turn access control back on for names used during
10424 template instantiation. */
10425 pop_deferring_access_checks ();
10426 /* Do the explicit instantiation. */
10427 do_decl_instantiation (decl, extension_specifier);
10431 pop_deferring_access_checks ();
10432 /* Skip the body of the explicit instantiation. */
10433 cp_parser_skip_to_end_of_statement (parser);
10436 /* We're done with the instantiation. */
10437 end_explicit_instantiation ();
10439 cp_parser_consume_semicolon_at_end_of_statement (parser);
10442 /* Parse an explicit-specialization.
10444 explicit-specialization:
10445 template < > declaration
10447 Although the standard says `declaration', what it really means is:
10449 explicit-specialization:
10450 template <> decl-specifier [opt] init-declarator [opt] ;
10451 template <> function-definition
10452 template <> explicit-specialization
10453 template <> template-declaration */
10456 cp_parser_explicit_specialization (cp_parser* parser)
10458 bool need_lang_pop;
10459 /* Look for the `template' keyword. */
10460 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10461 /* Look for the `<'. */
10462 cp_parser_require (parser, CPP_LESS, "`<'");
10463 /* Look for the `>'. */
10464 cp_parser_require (parser, CPP_GREATER, "`>'");
10465 /* We have processed another parameter list. */
10466 ++parser->num_template_parameter_lists;
10469 A template ... explicit specialization ... shall not have C
10471 if (current_lang_name == lang_name_c)
10473 error ("template specialization with C linkage");
10474 /* Give it C++ linkage to avoid confusing other parts of the
10476 push_lang_context (lang_name_cplusplus);
10477 need_lang_pop = true;
10480 need_lang_pop = false;
10481 /* Let the front end know that we are beginning a specialization. */
10482 if (!begin_specialization ())
10484 end_specialization ();
10485 cp_parser_skip_to_end_of_block_or_statement (parser);
10489 /* If the next keyword is `template', we need to figure out whether
10490 or not we're looking a template-declaration. */
10491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10493 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10494 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10495 cp_parser_template_declaration_after_export (parser,
10496 /*member_p=*/false);
10498 cp_parser_explicit_specialization (parser);
10501 /* Parse the dependent declaration. */
10502 cp_parser_single_declaration (parser,
10504 /*member_p=*/false,
10505 /*explicit_specialization_p=*/true,
10506 /*friend_p=*/NULL);
10507 /* We're done with the specialization. */
10508 end_specialization ();
10509 /* For the erroneous case of a template with C linkage, we pushed an
10510 implicit C++ linkage scope; exit that scope now. */
10512 pop_lang_context ();
10513 /* We're done with this parameter list. */
10514 --parser->num_template_parameter_lists;
10517 /* Parse a type-specifier.
10520 simple-type-specifier
10523 elaborated-type-specifier
10531 Returns a representation of the type-specifier. For a
10532 class-specifier, enum-specifier, or elaborated-type-specifier, a
10533 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10535 The parser flags FLAGS is used to control type-specifier parsing.
10537 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10538 in a decl-specifier-seq.
10540 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10541 class-specifier, enum-specifier, or elaborated-type-specifier, then
10542 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10543 if a type is declared; 2 if it is defined. Otherwise, it is set to
10546 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10547 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10548 is set to FALSE. */
10551 cp_parser_type_specifier (cp_parser* parser,
10552 cp_parser_flags flags,
10553 cp_decl_specifier_seq *decl_specs,
10554 bool is_declaration,
10555 int* declares_class_or_enum,
10556 bool* is_cv_qualifier)
10558 tree type_spec = NULL_TREE;
10561 cp_decl_spec ds = ds_last;
10563 /* Assume this type-specifier does not declare a new type. */
10564 if (declares_class_or_enum)
10565 *declares_class_or_enum = 0;
10566 /* And that it does not specify a cv-qualifier. */
10567 if (is_cv_qualifier)
10568 *is_cv_qualifier = false;
10569 /* Peek at the next token. */
10570 token = cp_lexer_peek_token (parser->lexer);
10572 /* If we're looking at a keyword, we can use that to guide the
10573 production we choose. */
10574 keyword = token->keyword;
10578 /* Look for the enum-specifier. */
10579 type_spec = cp_parser_enum_specifier (parser);
10580 /* If that worked, we're done. */
10583 if (declares_class_or_enum)
10584 *declares_class_or_enum = 2;
10586 cp_parser_set_decl_spec_type (decl_specs,
10588 /*user_defined_p=*/true);
10592 goto elaborated_type_specifier;
10594 /* Any of these indicate either a class-specifier, or an
10595 elaborated-type-specifier. */
10599 /* Parse tentatively so that we can back up if we don't find a
10600 class-specifier. */
10601 cp_parser_parse_tentatively (parser);
10602 /* Look for the class-specifier. */
10603 type_spec = cp_parser_class_specifier (parser);
10604 /* If that worked, we're done. */
10605 if (cp_parser_parse_definitely (parser))
10607 if (declares_class_or_enum)
10608 *declares_class_or_enum = 2;
10610 cp_parser_set_decl_spec_type (decl_specs,
10612 /*user_defined_p=*/true);
10616 /* Fall through. */
10617 elaborated_type_specifier:
10618 /* We're declaring (not defining) a class or enum. */
10619 if (declares_class_or_enum)
10620 *declares_class_or_enum = 1;
10622 /* Fall through. */
10624 /* Look for an elaborated-type-specifier. */
10626 = (cp_parser_elaborated_type_specifier
10628 decl_specs && decl_specs->specs[(int) ds_friend],
10631 cp_parser_set_decl_spec_type (decl_specs,
10633 /*user_defined_p=*/true);
10638 if (is_cv_qualifier)
10639 *is_cv_qualifier = true;
10644 if (is_cv_qualifier)
10645 *is_cv_qualifier = true;
10650 if (is_cv_qualifier)
10651 *is_cv_qualifier = true;
10655 /* The `__complex__' keyword is a GNU extension. */
10663 /* Handle simple keywords. */
10668 ++decl_specs->specs[(int)ds];
10669 decl_specs->any_specifiers_p = true;
10671 return cp_lexer_consume_token (parser->lexer)->u.value;
10674 /* If we do not already have a type-specifier, assume we are looking
10675 at a simple-type-specifier. */
10676 type_spec = cp_parser_simple_type_specifier (parser,
10680 /* If we didn't find a type-specifier, and a type-specifier was not
10681 optional in this context, issue an error message. */
10682 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10684 cp_parser_error (parser, "expected type specifier");
10685 return error_mark_node;
10691 /* Parse a simple-type-specifier.
10693 simple-type-specifier:
10694 :: [opt] nested-name-specifier [opt] type-name
10695 :: [opt] nested-name-specifier template template-id
10710 simple-type-specifier:
10711 decltype ( expression )
10715 simple-type-specifier:
10716 __typeof__ unary-expression
10717 __typeof__ ( type-id )
10719 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10720 appropriately updated. */
10723 cp_parser_simple_type_specifier (cp_parser* parser,
10724 cp_decl_specifier_seq *decl_specs,
10725 cp_parser_flags flags)
10727 tree type = NULL_TREE;
10730 /* Peek at the next token. */
10731 token = cp_lexer_peek_token (parser->lexer);
10733 /* If we're looking at a keyword, things are easy. */
10734 switch (token->keyword)
10738 decl_specs->explicit_char_p = true;
10739 type = char_type_node;
10742 type = wchar_type_node;
10745 type = boolean_type_node;
10749 ++decl_specs->specs[(int) ds_short];
10750 type = short_integer_type_node;
10754 decl_specs->explicit_int_p = true;
10755 type = integer_type_node;
10759 ++decl_specs->specs[(int) ds_long];
10760 type = long_integer_type_node;
10764 ++decl_specs->specs[(int) ds_signed];
10765 type = integer_type_node;
10769 ++decl_specs->specs[(int) ds_unsigned];
10770 type = unsigned_type_node;
10773 type = float_type_node;
10776 type = double_type_node;
10779 type = void_type_node;
10783 /* Parse the `decltype' type. */
10784 type = cp_parser_decltype (parser);
10787 cp_parser_set_decl_spec_type (decl_specs, type,
10788 /*user_defined_p=*/true);
10793 /* Consume the `typeof' token. */
10794 cp_lexer_consume_token (parser->lexer);
10795 /* Parse the operand to `typeof'. */
10796 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10797 /* If it is not already a TYPE, take its type. */
10798 if (!TYPE_P (type))
10799 type = finish_typeof (type);
10802 cp_parser_set_decl_spec_type (decl_specs, type,
10803 /*user_defined_p=*/true);
10811 /* If the type-specifier was for a built-in type, we're done. */
10816 /* Record the type. */
10818 && (token->keyword != RID_SIGNED
10819 && token->keyword != RID_UNSIGNED
10820 && token->keyword != RID_SHORT
10821 && token->keyword != RID_LONG))
10822 cp_parser_set_decl_spec_type (decl_specs,
10824 /*user_defined=*/false);
10826 decl_specs->any_specifiers_p = true;
10828 /* Consume the token. */
10829 id = cp_lexer_consume_token (parser->lexer)->u.value;
10831 /* There is no valid C++ program where a non-template type is
10832 followed by a "<". That usually indicates that the user thought
10833 that the type was a template. */
10834 cp_parser_check_for_invalid_template_id (parser, type);
10836 return TYPE_NAME (type);
10839 /* The type-specifier must be a user-defined type. */
10840 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10845 /* Don't gobble tokens or issue error messages if this is an
10846 optional type-specifier. */
10847 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10848 cp_parser_parse_tentatively (parser);
10850 /* Look for the optional `::' operator. */
10852 = (cp_parser_global_scope_opt (parser,
10853 /*current_scope_valid_p=*/false)
10855 /* Look for the nested-name specifier. */
10857 = (cp_parser_nested_name_specifier_opt (parser,
10858 /*typename_keyword_p=*/false,
10859 /*check_dependency_p=*/true,
10861 /*is_declaration=*/false)
10863 /* If we have seen a nested-name-specifier, and the next token
10864 is `template', then we are using the template-id production. */
10866 && cp_parser_optional_template_keyword (parser))
10868 /* Look for the template-id. */
10869 type = cp_parser_template_id (parser,
10870 /*template_keyword_p=*/true,
10871 /*check_dependency_p=*/true,
10872 /*is_declaration=*/false);
10873 /* If the template-id did not name a type, we are out of
10875 if (TREE_CODE (type) != TYPE_DECL)
10877 cp_parser_error (parser, "expected template-id for type");
10881 /* Otherwise, look for a type-name. */
10883 type = cp_parser_type_name (parser);
10884 /* Keep track of all name-lookups performed in class scopes. */
10888 && TREE_CODE (type) == TYPE_DECL
10889 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10890 maybe_note_name_used_in_class (DECL_NAME (type), type);
10891 /* If it didn't work out, we don't have a TYPE. */
10892 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10893 && !cp_parser_parse_definitely (parser))
10895 if (type && decl_specs)
10896 cp_parser_set_decl_spec_type (decl_specs, type,
10897 /*user_defined=*/true);
10900 /* If we didn't get a type-name, issue an error message. */
10901 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10903 cp_parser_error (parser, "expected type-name");
10904 return error_mark_node;
10907 /* There is no valid C++ program where a non-template type is
10908 followed by a "<". That usually indicates that the user thought
10909 that the type was a template. */
10910 if (type && type != error_mark_node)
10912 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10913 If it is, then the '<'...'>' enclose protocol names rather than
10914 template arguments, and so everything is fine. */
10915 if (c_dialect_objc ()
10916 && (objc_is_id (type) || objc_is_class_name (type)))
10918 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10919 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10921 /* Clobber the "unqualified" type previously entered into
10922 DECL_SPECS with the new, improved protocol-qualified version. */
10924 decl_specs->type = qual_type;
10929 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10935 /* Parse a type-name.
10948 Returns a TYPE_DECL for the type. */
10951 cp_parser_type_name (cp_parser* parser)
10956 /* We can't know yet whether it is a class-name or not. */
10957 cp_parser_parse_tentatively (parser);
10958 /* Try a class-name. */
10959 type_decl = cp_parser_class_name (parser,
10960 /*typename_keyword_p=*/false,
10961 /*template_keyword_p=*/false,
10963 /*check_dependency_p=*/true,
10964 /*class_head_p=*/false,
10965 /*is_declaration=*/false);
10966 /* If it's not a class-name, keep looking. */
10967 if (!cp_parser_parse_definitely (parser))
10969 /* It must be a typedef-name or an enum-name. */
10970 identifier = cp_parser_identifier (parser);
10971 if (identifier == error_mark_node)
10972 return error_mark_node;
10974 /* Look up the type-name. */
10975 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10977 if (TREE_CODE (type_decl) != TYPE_DECL
10978 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10980 /* See if this is an Objective-C type. */
10981 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10982 tree type = objc_get_protocol_qualified_type (identifier, protos);
10984 type_decl = TYPE_NAME (type);
10987 /* Issue an error if we did not find a type-name. */
10988 if (TREE_CODE (type_decl) != TYPE_DECL)
10990 if (!cp_parser_simulate_error (parser))
10991 cp_parser_name_lookup_error (parser, identifier, type_decl,
10993 type_decl = error_mark_node;
10995 /* Remember that the name was used in the definition of the
10996 current class so that we can check later to see if the
10997 meaning would have been different after the class was
10998 entirely defined. */
10999 else if (type_decl != error_mark_node
11001 maybe_note_name_used_in_class (identifier, type_decl);
11008 /* Parse an elaborated-type-specifier. Note that the grammar given
11009 here incorporates the resolution to DR68.
11011 elaborated-type-specifier:
11012 class-key :: [opt] nested-name-specifier [opt] identifier
11013 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
11014 enum :: [opt] nested-name-specifier [opt] identifier
11015 typename :: [opt] nested-name-specifier identifier
11016 typename :: [opt] nested-name-specifier template [opt]
11021 elaborated-type-specifier:
11022 class-key attributes :: [opt] nested-name-specifier [opt] identifier
11023 class-key attributes :: [opt] nested-name-specifier [opt]
11024 template [opt] template-id
11025 enum attributes :: [opt] nested-name-specifier [opt] identifier
11027 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
11028 declared `friend'. If IS_DECLARATION is TRUE, then this
11029 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
11030 something is being declared.
11032 Returns the TYPE specified. */
11035 cp_parser_elaborated_type_specifier (cp_parser* parser,
11037 bool is_declaration)
11039 enum tag_types tag_type;
11041 tree type = NULL_TREE;
11042 tree attributes = NULL_TREE;
11044 /* See if we're looking at the `enum' keyword. */
11045 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
11047 /* Consume the `enum' token. */
11048 cp_lexer_consume_token (parser->lexer);
11049 /* Remember that it's an enumeration type. */
11050 tag_type = enum_type;
11051 /* Parse the attributes. */
11052 attributes = cp_parser_attributes_opt (parser);
11054 /* Or, it might be `typename'. */
11055 else if (cp_lexer_next_token_is_keyword (parser->lexer,
11058 /* Consume the `typename' token. */
11059 cp_lexer_consume_token (parser->lexer);
11060 /* Remember that it's a `typename' type. */
11061 tag_type = typename_type;
11062 /* The `typename' keyword is only allowed in templates. */
11063 if (!processing_template_decl)
11064 pedwarn ("using %<typename%> outside of template");
11066 /* Otherwise it must be a class-key. */
11069 tag_type = cp_parser_class_key (parser);
11070 if (tag_type == none_type)
11071 return error_mark_node;
11072 /* Parse the attributes. */
11073 attributes = cp_parser_attributes_opt (parser);
11076 /* Look for the `::' operator. */
11077 cp_parser_global_scope_opt (parser,
11078 /*current_scope_valid_p=*/false);
11079 /* Look for the nested-name-specifier. */
11080 if (tag_type == typename_type)
11082 if (!cp_parser_nested_name_specifier (parser,
11083 /*typename_keyword_p=*/true,
11084 /*check_dependency_p=*/true,
11087 return error_mark_node;
11090 /* Even though `typename' is not present, the proposed resolution
11091 to Core Issue 180 says that in `class A<T>::B', `B' should be
11092 considered a type-name, even if `A<T>' is dependent. */
11093 cp_parser_nested_name_specifier_opt (parser,
11094 /*typename_keyword_p=*/true,
11095 /*check_dependency_p=*/true,
11098 /* For everything but enumeration types, consider a template-id.
11099 For an enumeration type, consider only a plain identifier. */
11100 if (tag_type != enum_type)
11102 bool template_p = false;
11105 /* Allow the `template' keyword. */
11106 template_p = cp_parser_optional_template_keyword (parser);
11107 /* If we didn't see `template', we don't know if there's a
11108 template-id or not. */
11110 cp_parser_parse_tentatively (parser);
11111 /* Parse the template-id. */
11112 decl = cp_parser_template_id (parser, template_p,
11113 /*check_dependency_p=*/true,
11115 /* If we didn't find a template-id, look for an ordinary
11117 if (!template_p && !cp_parser_parse_definitely (parser))
11119 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11120 in effect, then we must assume that, upon instantiation, the
11121 template will correspond to a class. */
11122 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11123 && tag_type == typename_type)
11124 type = make_typename_type (parser->scope, decl,
11126 /*complain=*/tf_error);
11128 type = TREE_TYPE (decl);
11133 identifier = cp_parser_identifier (parser);
11135 if (identifier == error_mark_node)
11137 parser->scope = NULL_TREE;
11138 return error_mark_node;
11141 /* For a `typename', we needn't call xref_tag. */
11142 if (tag_type == typename_type
11143 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11144 return cp_parser_make_typename_type (parser, parser->scope,
11146 /* Look up a qualified name in the usual way. */
11150 tree ambiguous_decls;
11152 decl = cp_parser_lookup_name (parser, identifier,
11154 /*is_template=*/false,
11155 /*is_namespace=*/false,
11156 /*check_dependency=*/true,
11159 /* If the lookup was ambiguous, an error will already have been
11161 if (ambiguous_decls)
11162 return error_mark_node;
11164 /* If we are parsing friend declaration, DECL may be a
11165 TEMPLATE_DECL tree node here. However, we need to check
11166 whether this TEMPLATE_DECL results in valid code. Consider
11167 the following example:
11170 template <class T> class C {};
11173 template <class T> friend class N::C; // #1, valid code
11175 template <class T> class Y {
11176 friend class N::C; // #2, invalid code
11179 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11180 name lookup of `N::C'. We see that friend declaration must
11181 be template for the code to be valid. Note that
11182 processing_template_decl does not work here since it is
11183 always 1 for the above two cases. */
11185 decl = (cp_parser_maybe_treat_template_as_class
11186 (decl, /*tag_name_p=*/is_friend
11187 && parser->num_template_parameter_lists));
11189 if (TREE_CODE (decl) != TYPE_DECL)
11191 cp_parser_diagnose_invalid_type_name (parser,
11194 return error_mark_node;
11197 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11199 bool allow_template = (parser->num_template_parameter_lists
11200 || DECL_SELF_REFERENCE_P (decl));
11201 type = check_elaborated_type_specifier (tag_type, decl,
11204 if (type == error_mark_node)
11205 return error_mark_node;
11208 /* Forward declarations of nested types, such as
11213 are invalid unless all components preceding the final '::'
11214 are complete. If all enclosing types are complete, these
11215 declarations become merely pointless.
11217 Invalid forward declarations of nested types are errors
11218 caught elsewhere in parsing. Those that are pointless arrive
11221 if (cp_parser_declares_only_class_p (parser)
11222 && !is_friend && !processing_explicit_instantiation)
11223 warning (0, "declaration %qD does not declare anything", decl);
11225 type = TREE_TYPE (decl);
11229 /* An elaborated-type-specifier sometimes introduces a new type and
11230 sometimes names an existing type. Normally, the rule is that it
11231 introduces a new type only if there is not an existing type of
11232 the same name already in scope. For example, given:
11235 void f() { struct S s; }
11237 the `struct S' in the body of `f' is the same `struct S' as in
11238 the global scope; the existing definition is used. However, if
11239 there were no global declaration, this would introduce a new
11240 local class named `S'.
11242 An exception to this rule applies to the following code:
11244 namespace N { struct S; }
11246 Here, the elaborated-type-specifier names a new type
11247 unconditionally; even if there is already an `S' in the
11248 containing scope this declaration names a new type.
11249 This exception only applies if the elaborated-type-specifier
11250 forms the complete declaration:
11254 A declaration consisting solely of `class-key identifier ;' is
11255 either a redeclaration of the name in the current scope or a
11256 forward declaration of the identifier as a class name. It
11257 introduces the name into the current scope.
11259 We are in this situation precisely when the next token is a `;'.
11261 An exception to the exception is that a `friend' declaration does
11262 *not* name a new type; i.e., given:
11264 struct S { friend struct T; };
11266 `T' is not a new type in the scope of `S'.
11268 Also, `new struct S' or `sizeof (struct S)' never results in the
11269 definition of a new type; a new type can only be declared in a
11270 declaration context. */
11276 /* Friends have special name lookup rules. */
11277 ts = ts_within_enclosing_non_class;
11278 else if (is_declaration
11279 && cp_lexer_next_token_is (parser->lexer,
11281 /* This is a `class-key identifier ;' */
11287 (parser->num_template_parameter_lists
11288 && (cp_parser_next_token_starts_class_definition_p (parser)
11289 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11290 /* An unqualified name was used to reference this type, so
11291 there were no qualifying templates. */
11292 if (!cp_parser_check_template_parameters (parser,
11293 /*num_templates=*/0))
11294 return error_mark_node;
11295 type = xref_tag (tag_type, identifier, ts, template_p);
11299 if (type == error_mark_node)
11300 return error_mark_node;
11302 /* Allow attributes on forward declarations of classes. */
11305 if (TREE_CODE (type) == TYPENAME_TYPE)
11306 warning (OPT_Wattributes,
11307 "attributes ignored on uninstantiated type");
11308 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11309 && ! processing_explicit_instantiation)
11310 warning (OPT_Wattributes,
11311 "attributes ignored on template instantiation");
11312 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11313 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11315 warning (OPT_Wattributes,
11316 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11319 if (tag_type != enum_type)
11320 cp_parser_check_class_key (tag_type, type);
11322 /* A "<" cannot follow an elaborated type specifier. If that
11323 happens, the user was probably trying to form a template-id. */
11324 cp_parser_check_for_invalid_template_id (parser, type);
11329 /* Parse an enum-specifier.
11332 enum identifier [opt] { enumerator-list [opt] }
11335 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11338 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11339 if the token stream isn't an enum-specifier after all. */
11342 cp_parser_enum_specifier (cp_parser* parser)
11348 /* Parse tentatively so that we can back up if we don't find a
11350 cp_parser_parse_tentatively (parser);
11352 /* Caller guarantees that the current token is 'enum', an identifier
11353 possibly follows, and the token after that is an opening brace.
11354 If we don't have an identifier, fabricate an anonymous name for
11355 the enumeration being defined. */
11356 cp_lexer_consume_token (parser->lexer);
11358 attributes = cp_parser_attributes_opt (parser);
11360 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11361 identifier = cp_parser_identifier (parser);
11363 identifier = make_anon_name ();
11365 /* Look for the `{' but don't consume it yet. */
11366 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11367 cp_parser_simulate_error (parser);
11369 if (!cp_parser_parse_definitely (parser))
11372 /* Issue an error message if type-definitions are forbidden here. */
11373 if (!cp_parser_check_type_definition (parser))
11374 type = error_mark_node;
11376 /* Create the new type. We do this before consuming the opening
11377 brace so the enum will be recorded as being on the line of its
11378 tag (or the 'enum' keyword, if there is no tag). */
11379 type = start_enum (identifier);
11381 /* Consume the opening brace. */
11382 cp_lexer_consume_token (parser->lexer);
11384 if (type == error_mark_node)
11386 cp_parser_skip_to_end_of_block_or_statement (parser);
11387 return error_mark_node;
11390 /* If the next token is not '}', then there are some enumerators. */
11391 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11392 cp_parser_enumerator_list (parser, type);
11394 /* Consume the final '}'. */
11395 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11397 /* Look for trailing attributes to apply to this enumeration, and
11398 apply them if appropriate. */
11399 if (cp_parser_allow_gnu_extensions_p (parser))
11401 tree trailing_attr = cp_parser_attributes_opt (parser);
11402 cplus_decl_attributes (&type,
11404 (int) ATTR_FLAG_TYPE_IN_PLACE);
11407 /* Finish up the enumeration. */
11408 finish_enum (type);
11413 /* Parse an enumerator-list. The enumerators all have the indicated
11417 enumerator-definition
11418 enumerator-list , enumerator-definition */
11421 cp_parser_enumerator_list (cp_parser* parser, tree type)
11425 /* Parse an enumerator-definition. */
11426 cp_parser_enumerator_definition (parser, type);
11428 /* If the next token is not a ',', we've reached the end of
11430 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11432 /* Otherwise, consume the `,' and keep going. */
11433 cp_lexer_consume_token (parser->lexer);
11434 /* If the next token is a `}', there is a trailing comma. */
11435 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11437 if (pedantic && !in_system_header)
11438 pedwarn ("comma at end of enumerator list");
11444 /* Parse an enumerator-definition. The enumerator has the indicated
11447 enumerator-definition:
11449 enumerator = constant-expression
11455 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11460 /* Look for the identifier. */
11461 identifier = cp_parser_identifier (parser);
11462 if (identifier == error_mark_node)
11465 /* If the next token is an '=', then there is an explicit value. */
11466 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11468 /* Consume the `=' token. */
11469 cp_lexer_consume_token (parser->lexer);
11470 /* Parse the value. */
11471 value = cp_parser_constant_expression (parser,
11472 /*allow_non_constant_p=*/false,
11478 /* Create the enumerator. */
11479 build_enumerator (identifier, value, type);
11482 /* Parse a namespace-name.
11485 original-namespace-name
11488 Returns the NAMESPACE_DECL for the namespace. */
11491 cp_parser_namespace_name (cp_parser* parser)
11494 tree namespace_decl;
11496 /* Get the name of the namespace. */
11497 identifier = cp_parser_identifier (parser);
11498 if (identifier == error_mark_node)
11499 return error_mark_node;
11501 /* Look up the identifier in the currently active scope. Look only
11502 for namespaces, due to:
11504 [basic.lookup.udir]
11506 When looking up a namespace-name in a using-directive or alias
11507 definition, only namespace names are considered.
11511 [basic.lookup.qual]
11513 During the lookup of a name preceding the :: scope resolution
11514 operator, object, function, and enumerator names are ignored.
11516 (Note that cp_parser_class_or_namespace_name only calls this
11517 function if the token after the name is the scope resolution
11519 namespace_decl = cp_parser_lookup_name (parser, identifier,
11521 /*is_template=*/false,
11522 /*is_namespace=*/true,
11523 /*check_dependency=*/true,
11524 /*ambiguous_decls=*/NULL);
11525 /* If it's not a namespace, issue an error. */
11526 if (namespace_decl == error_mark_node
11527 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11529 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11530 error ("%qD is not a namespace-name", identifier);
11531 cp_parser_error (parser, "expected namespace-name");
11532 namespace_decl = error_mark_node;
11535 return namespace_decl;
11538 /* Parse a namespace-definition.
11540 namespace-definition:
11541 named-namespace-definition
11542 unnamed-namespace-definition
11544 named-namespace-definition:
11545 original-namespace-definition
11546 extension-namespace-definition
11548 original-namespace-definition:
11549 namespace identifier { namespace-body }
11551 extension-namespace-definition:
11552 namespace original-namespace-name { namespace-body }
11554 unnamed-namespace-definition:
11555 namespace { namespace-body } */
11558 cp_parser_namespace_definition (cp_parser* parser)
11560 tree identifier, attribs;
11561 bool has_visibility;
11564 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
11567 cp_lexer_consume_token (parser->lexer);
11572 /* Look for the `namespace' keyword. */
11573 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11575 /* Get the name of the namespace. We do not attempt to distinguish
11576 between an original-namespace-definition and an
11577 extension-namespace-definition at this point. The semantic
11578 analysis routines are responsible for that. */
11579 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11580 identifier = cp_parser_identifier (parser);
11582 identifier = NULL_TREE;
11584 /* Parse any specified attributes. */
11585 attribs = cp_parser_attributes_opt (parser);
11587 /* Look for the `{' to start the namespace. */
11588 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11589 /* Start the namespace. */
11590 push_namespace (identifier);
11592 /* "inline namespace" is equivalent to a stub namespace definition
11593 followed by a strong using directive. */
11596 tree namespace = current_namespace;
11597 /* Set up namespace association. */
11598 DECL_NAMESPACE_ASSOCIATIONS (namespace)
11599 = tree_cons (CP_DECL_CONTEXT (namespace), NULL_TREE,
11600 DECL_NAMESPACE_ASSOCIATIONS (namespace));
11601 /* Import the contents of the inline namespace. */
11603 do_using_directive (namespace);
11604 push_namespace (identifier);
11607 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11609 /* Parse the body of the namespace. */
11610 cp_parser_namespace_body (parser);
11612 #ifdef HANDLE_PRAGMA_VISIBILITY
11613 if (has_visibility)
11617 /* Finish the namespace. */
11619 /* Look for the final `}'. */
11620 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11623 /* Parse a namespace-body.
11626 declaration-seq [opt] */
11629 cp_parser_namespace_body (cp_parser* parser)
11631 cp_parser_declaration_seq_opt (parser);
11634 /* Parse a namespace-alias-definition.
11636 namespace-alias-definition:
11637 namespace identifier = qualified-namespace-specifier ; */
11640 cp_parser_namespace_alias_definition (cp_parser* parser)
11643 tree namespace_specifier;
11645 /* Look for the `namespace' keyword. */
11646 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11647 /* Look for the identifier. */
11648 identifier = cp_parser_identifier (parser);
11649 if (identifier == error_mark_node)
11651 /* Look for the `=' token. */
11652 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11653 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11655 error ("%<namespace%> definition is not allowed here");
11656 /* Skip the definition. */
11657 cp_lexer_consume_token (parser->lexer);
11658 if (cp_parser_skip_to_closing_brace (parser))
11659 cp_lexer_consume_token (parser->lexer);
11662 cp_parser_require (parser, CPP_EQ, "`='");
11663 /* Look for the qualified-namespace-specifier. */
11664 namespace_specifier
11665 = cp_parser_qualified_namespace_specifier (parser);
11666 /* Look for the `;' token. */
11667 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11669 /* Register the alias in the symbol table. */
11670 do_namespace_alias (identifier, namespace_specifier);
11673 /* Parse a qualified-namespace-specifier.
11675 qualified-namespace-specifier:
11676 :: [opt] nested-name-specifier [opt] namespace-name
11678 Returns a NAMESPACE_DECL corresponding to the specified
11682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11684 /* Look for the optional `::'. */
11685 cp_parser_global_scope_opt (parser,
11686 /*current_scope_valid_p=*/false);
11688 /* Look for the optional nested-name-specifier. */
11689 cp_parser_nested_name_specifier_opt (parser,
11690 /*typename_keyword_p=*/false,
11691 /*check_dependency_p=*/true,
11693 /*is_declaration=*/true);
11695 return cp_parser_namespace_name (parser);
11698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11699 access declaration.
11702 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11703 using :: unqualified-id ;
11705 access-declaration:
11711 cp_parser_using_declaration (cp_parser* parser,
11712 bool access_declaration_p)
11715 bool typename_p = false;
11716 bool global_scope_p;
11721 if (access_declaration_p)
11722 cp_parser_parse_tentatively (parser);
11725 /* Look for the `using' keyword. */
11726 cp_parser_require_keyword (parser, RID_USING, "`using'");
11728 /* Peek at the next token. */
11729 token = cp_lexer_peek_token (parser->lexer);
11730 /* See if it's `typename'. */
11731 if (token->keyword == RID_TYPENAME)
11733 /* Remember that we've seen it. */
11735 /* Consume the `typename' token. */
11736 cp_lexer_consume_token (parser->lexer);
11740 /* Look for the optional global scope qualification. */
11742 = (cp_parser_global_scope_opt (parser,
11743 /*current_scope_valid_p=*/false)
11746 /* If we saw `typename', or didn't see `::', then there must be a
11747 nested-name-specifier present. */
11748 if (typename_p || !global_scope_p)
11749 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11750 /*check_dependency_p=*/true,
11752 /*is_declaration=*/true);
11753 /* Otherwise, we could be in either of the two productions. In that
11754 case, treat the nested-name-specifier as optional. */
11756 qscope = cp_parser_nested_name_specifier_opt (parser,
11757 /*typename_keyword_p=*/false,
11758 /*check_dependency_p=*/true,
11760 /*is_declaration=*/true);
11762 qscope = global_namespace;
11764 if (access_declaration_p && cp_parser_error_occurred (parser))
11765 /* Something has already gone wrong; there's no need to parse
11766 further. Since an error has occurred, the return value of
11767 cp_parser_parse_definitely will be false, as required. */
11768 return cp_parser_parse_definitely (parser);
11770 /* Parse the unqualified-id. */
11771 identifier = cp_parser_unqualified_id (parser,
11772 /*template_keyword_p=*/false,
11773 /*check_dependency_p=*/true,
11774 /*declarator_p=*/true,
11775 /*optional_p=*/false);
11777 if (access_declaration_p)
11779 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11780 cp_parser_simulate_error (parser);
11781 if (!cp_parser_parse_definitely (parser))
11785 /* The function we call to handle a using-declaration is different
11786 depending on what scope we are in. */
11787 if (qscope == error_mark_node || identifier == error_mark_node)
11789 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11790 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11791 /* [namespace.udecl]
11793 A using declaration shall not name a template-id. */
11794 error ("a template-id may not appear in a using-declaration");
11797 if (at_class_scope_p ())
11799 /* Create the USING_DECL. */
11800 decl = do_class_using_decl (parser->scope, identifier);
11802 if (check_for_bare_parameter_packs (decl))
11805 /* Add it to the list of members in this class. */
11806 finish_member_declaration (decl);
11810 decl = cp_parser_lookup_name_simple (parser, identifier);
11811 if (decl == error_mark_node)
11812 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11813 else if (check_for_bare_parameter_packs (decl))
11815 else if (!at_namespace_scope_p ())
11816 do_local_using_decl (decl, qscope, identifier);
11818 do_toplevel_using_decl (decl, qscope, identifier);
11822 /* Look for the final `;'. */
11823 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11828 /* Parse a using-directive.
11831 using namespace :: [opt] nested-name-specifier [opt]
11832 namespace-name ; */
11835 cp_parser_using_directive (cp_parser* parser)
11837 tree namespace_decl;
11840 /* Look for the `using' keyword. */
11841 cp_parser_require_keyword (parser, RID_USING, "`using'");
11842 /* And the `namespace' keyword. */
11843 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11844 /* Look for the optional `::' operator. */
11845 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11846 /* And the optional nested-name-specifier. */
11847 cp_parser_nested_name_specifier_opt (parser,
11848 /*typename_keyword_p=*/false,
11849 /*check_dependency_p=*/true,
11851 /*is_declaration=*/true);
11852 /* Get the namespace being used. */
11853 namespace_decl = cp_parser_namespace_name (parser);
11854 /* And any specified attributes. */
11855 attribs = cp_parser_attributes_opt (parser);
11856 /* Update the symbol table. */
11857 parse_using_directive (namespace_decl, attribs);
11858 /* Look for the final `;'. */
11859 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11862 /* Parse an asm-definition.
11865 asm ( string-literal ) ;
11870 asm volatile [opt] ( string-literal ) ;
11871 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11872 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11873 : asm-operand-list [opt] ) ;
11874 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11875 : asm-operand-list [opt]
11876 : asm-operand-list [opt] ) ; */
11879 cp_parser_asm_definition (cp_parser* parser)
11882 tree outputs = NULL_TREE;
11883 tree inputs = NULL_TREE;
11884 tree clobbers = NULL_TREE;
11886 bool volatile_p = false;
11887 bool extended_p = false;
11888 bool invalid_inputs_p = false;
11889 bool invalid_outputs_p = false;
11891 /* Look for the `asm' keyword. */
11892 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11893 /* See if the next token is `volatile'. */
11894 if (cp_parser_allow_gnu_extensions_p (parser)
11895 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11897 /* Remember that we saw the `volatile' keyword. */
11899 /* Consume the token. */
11900 cp_lexer_consume_token (parser->lexer);
11902 /* Look for the opening `('. */
11903 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11905 /* Look for the string. */
11906 string = cp_parser_string_literal (parser, false, false);
11907 if (string == error_mark_node)
11909 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11910 /*consume_paren=*/true);
11914 /* If we're allowing GNU extensions, check for the extended assembly
11915 syntax. Unfortunately, the `:' tokens need not be separated by
11916 a space in C, and so, for compatibility, we tolerate that here
11917 too. Doing that means that we have to treat the `::' operator as
11919 if (cp_parser_allow_gnu_extensions_p (parser)
11920 && parser->in_function_body
11921 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11922 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11924 bool inputs_p = false;
11925 bool clobbers_p = false;
11927 /* The extended syntax was used. */
11930 /* Look for outputs. */
11931 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11933 /* Consume the `:'. */
11934 cp_lexer_consume_token (parser->lexer);
11935 /* Parse the output-operands. */
11936 if (cp_lexer_next_token_is_not (parser->lexer,
11938 && cp_lexer_next_token_is_not (parser->lexer,
11940 && cp_lexer_next_token_is_not (parser->lexer,
11942 outputs = cp_parser_asm_operand_list (parser);
11944 if (outputs == error_mark_node)
11945 invalid_outputs_p = true;
11947 /* If the next token is `::', there are no outputs, and the
11948 next token is the beginning of the inputs. */
11949 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11950 /* The inputs are coming next. */
11953 /* Look for inputs. */
11955 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11957 /* Consume the `:' or `::'. */
11958 cp_lexer_consume_token (parser->lexer);
11959 /* Parse the output-operands. */
11960 if (cp_lexer_next_token_is_not (parser->lexer,
11962 && cp_lexer_next_token_is_not (parser->lexer,
11964 inputs = cp_parser_asm_operand_list (parser);
11966 if (inputs == error_mark_node)
11967 invalid_inputs_p = true;
11969 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11970 /* The clobbers are coming next. */
11973 /* Look for clobbers. */
11975 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11977 /* Consume the `:' or `::'. */
11978 cp_lexer_consume_token (parser->lexer);
11979 /* Parse the clobbers. */
11980 if (cp_lexer_next_token_is_not (parser->lexer,
11982 clobbers = cp_parser_asm_clobber_list (parser);
11985 /* Look for the closing `)'. */
11986 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11987 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11988 /*consume_paren=*/true);
11989 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11991 if (!invalid_inputs_p && !invalid_outputs_p)
11993 /* Create the ASM_EXPR. */
11994 if (parser->in_function_body)
11996 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11998 /* If the extended syntax was not used, mark the ASM_EXPR. */
12001 tree temp = asm_stmt;
12002 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
12003 temp = TREE_OPERAND (temp, 0);
12005 ASM_INPUT_P (temp) = 1;
12009 cgraph_add_asm_node (string);
12013 /* Declarators [gram.dcl.decl] */
12015 /* Parse an init-declarator.
12018 declarator initializer [opt]
12023 declarator asm-specification [opt] attributes [opt] initializer [opt]
12025 function-definition:
12026 decl-specifier-seq [opt] declarator ctor-initializer [opt]
12028 decl-specifier-seq [opt] declarator function-try-block
12032 function-definition:
12033 __extension__ function-definition
12035 The DECL_SPECIFIERS apply to this declarator. Returns a
12036 representation of the entity declared. If MEMBER_P is TRUE, then
12037 this declarator appears in a class scope. The new DECL created by
12038 this declarator is returned.
12040 The CHECKS are access checks that should be performed once we know
12041 what entity is being declared (and, therefore, what classes have
12044 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
12045 for a function-definition here as well. If the declarator is a
12046 declarator for a function-definition, *FUNCTION_DEFINITION_P will
12047 be TRUE upon return. By that point, the function-definition will
12048 have been completely parsed.
12050 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
12054 cp_parser_init_declarator (cp_parser* parser,
12055 cp_decl_specifier_seq *decl_specifiers,
12056 VEC (deferred_access_check,gc)* checks,
12057 bool function_definition_allowed_p,
12059 int declares_class_or_enum,
12060 bool* function_definition_p)
12063 cp_declarator *declarator;
12064 tree prefix_attributes;
12066 tree asm_specification;
12068 tree decl = NULL_TREE;
12070 bool is_initialized;
12071 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
12072 initialized with "= ..", CPP_OPEN_PAREN if initialized with
12074 enum cpp_ttype initialization_kind;
12075 bool is_parenthesized_init = false;
12076 bool is_non_constant_init;
12077 int ctor_dtor_or_conv_p;
12079 tree pushed_scope = NULL;
12081 /* Gather the attributes that were provided with the
12082 decl-specifiers. */
12083 prefix_attributes = decl_specifiers->attributes;
12085 /* Assume that this is not the declarator for a function
12087 if (function_definition_p)
12088 *function_definition_p = false;
12090 /* Defer access checks while parsing the declarator; we cannot know
12091 what names are accessible until we know what is being
12093 resume_deferring_access_checks ();
12095 /* Parse the declarator. */
12097 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12098 &ctor_dtor_or_conv_p,
12099 /*parenthesized_p=*/NULL,
12100 /*member_p=*/false);
12101 /* Gather up the deferred checks. */
12102 stop_deferring_access_checks ();
12104 /* If the DECLARATOR was erroneous, there's no need to go
12106 if (declarator == cp_error_declarator)
12107 return error_mark_node;
12109 /* Check that the number of template-parameter-lists is OK. */
12110 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12111 return error_mark_node;
12113 if (declares_class_or_enum & 2)
12114 cp_parser_check_for_definition_in_return_type (declarator,
12115 decl_specifiers->type);
12117 /* Figure out what scope the entity declared by the DECLARATOR is
12118 located in. `grokdeclarator' sometimes changes the scope, so
12119 we compute it now. */
12120 scope = get_scope_of_declarator (declarator);
12122 /* If we're allowing GNU extensions, look for an asm-specification
12124 if (cp_parser_allow_gnu_extensions_p (parser))
12126 /* Look for an asm-specification. */
12127 asm_specification = cp_parser_asm_specification_opt (parser);
12128 /* And attributes. */
12129 attributes = cp_parser_attributes_opt (parser);
12133 asm_specification = NULL_TREE;
12134 attributes = NULL_TREE;
12137 /* Peek at the next token. */
12138 token = cp_lexer_peek_token (parser->lexer);
12139 /* Check to see if the token indicates the start of a
12140 function-definition. */
12141 if (cp_parser_token_starts_function_definition_p (token))
12143 if (!function_definition_allowed_p)
12145 /* If a function-definition should not appear here, issue an
12147 cp_parser_error (parser,
12148 "a function-definition is not allowed here");
12149 return error_mark_node;
12153 /* Neither attributes nor an asm-specification are allowed
12154 on a function-definition. */
12155 if (asm_specification)
12156 error ("an asm-specification is not allowed on a function-definition");
12158 error ("attributes are not allowed on a function-definition");
12159 /* This is a function-definition. */
12160 *function_definition_p = true;
12162 /* Parse the function definition. */
12164 decl = cp_parser_save_member_function_body (parser,
12167 prefix_attributes);
12170 = (cp_parser_function_definition_from_specifiers_and_declarator
12171 (parser, decl_specifiers, prefix_attributes, declarator));
12179 Only in function declarations for constructors, destructors, and
12180 type conversions can the decl-specifier-seq be omitted.
12182 We explicitly postpone this check past the point where we handle
12183 function-definitions because we tolerate function-definitions
12184 that are missing their return types in some modes. */
12185 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12187 cp_parser_error (parser,
12188 "expected constructor, destructor, or type conversion");
12189 return error_mark_node;
12192 /* An `=' or an `(' indicates an initializer. */
12193 if (token->type == CPP_EQ
12194 || token->type == CPP_OPEN_PAREN)
12196 is_initialized = true;
12197 initialization_kind = token->type;
12201 /* If the init-declarator isn't initialized and isn't followed by a
12202 `,' or `;', it's not a valid init-declarator. */
12203 if (token->type != CPP_COMMA
12204 && token->type != CPP_SEMICOLON)
12206 cp_parser_error (parser, "expected initializer");
12207 return error_mark_node;
12209 is_initialized = false;
12210 initialization_kind = CPP_EOF;
12213 /* Because start_decl has side-effects, we should only call it if we
12214 know we're going ahead. By this point, we know that we cannot
12215 possibly be looking at any other construct. */
12216 cp_parser_commit_to_tentative_parse (parser);
12218 /* If the decl specifiers were bad, issue an error now that we're
12219 sure this was intended to be a declarator. Then continue
12220 declaring the variable(s), as int, to try to cut down on further
12222 if (decl_specifiers->any_specifiers_p
12223 && decl_specifiers->type == error_mark_node)
12225 cp_parser_error (parser, "invalid type in declaration");
12226 decl_specifiers->type = integer_type_node;
12229 /* Check to see whether or not this declaration is a friend. */
12230 friend_p = cp_parser_friend_p (decl_specifiers);
12232 /* Enter the newly declared entry in the symbol table. If we're
12233 processing a declaration in a class-specifier, we wait until
12234 after processing the initializer. */
12237 if (parser->in_unbraced_linkage_specification_p)
12238 decl_specifiers->storage_class = sc_extern;
12239 decl = start_decl (declarator, decl_specifiers,
12240 is_initialized, attributes, prefix_attributes,
12244 /* Enter the SCOPE. That way unqualified names appearing in the
12245 initializer will be looked up in SCOPE. */
12246 pushed_scope = push_scope (scope);
12248 /* Perform deferred access control checks, now that we know in which
12249 SCOPE the declared entity resides. */
12250 if (!member_p && decl)
12252 tree saved_current_function_decl = NULL_TREE;
12254 /* If the entity being declared is a function, pretend that we
12255 are in its scope. If it is a `friend', it may have access to
12256 things that would not otherwise be accessible. */
12257 if (TREE_CODE (decl) == FUNCTION_DECL)
12259 saved_current_function_decl = current_function_decl;
12260 current_function_decl = decl;
12263 /* Perform access checks for template parameters. */
12264 cp_parser_perform_template_parameter_access_checks (checks);
12266 /* Perform the access control checks for the declarator and the
12267 the decl-specifiers. */
12268 perform_deferred_access_checks ();
12270 /* Restore the saved value. */
12271 if (TREE_CODE (decl) == FUNCTION_DECL)
12272 current_function_decl = saved_current_function_decl;
12275 /* Parse the initializer. */
12276 initializer = NULL_TREE;
12277 is_parenthesized_init = false;
12278 is_non_constant_init = true;
12279 if (is_initialized)
12281 if (function_declarator_p (declarator))
12283 if (initialization_kind == CPP_EQ)
12284 initializer = cp_parser_pure_specifier (parser);
12287 /* If the declaration was erroneous, we don't really
12288 know what the user intended, so just silently
12289 consume the initializer. */
12290 if (decl != error_mark_node)
12291 error ("initializer provided for function");
12292 cp_parser_skip_to_closing_parenthesis (parser,
12293 /*recovering=*/true,
12294 /*or_comma=*/false,
12295 /*consume_paren=*/true);
12299 initializer = cp_parser_initializer (parser,
12300 &is_parenthesized_init,
12301 &is_non_constant_init);
12304 /* The old parser allows attributes to appear after a parenthesized
12305 initializer. Mark Mitchell proposed removing this functionality
12306 on the GCC mailing lists on 2002-08-13. This parser accepts the
12307 attributes -- but ignores them. */
12308 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12309 if (cp_parser_attributes_opt (parser))
12310 warning (OPT_Wattributes,
12311 "attributes after parenthesized initializer ignored");
12313 /* For an in-class declaration, use `grokfield' to create the
12319 pop_scope (pushed_scope);
12320 pushed_scope = false;
12322 decl = grokfield (declarator, decl_specifiers,
12323 initializer, !is_non_constant_init,
12324 /*asmspec=*/NULL_TREE,
12325 prefix_attributes);
12326 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12327 cp_parser_save_default_args (parser, decl);
12330 /* Finish processing the declaration. But, skip friend
12332 if (!friend_p && decl && decl != error_mark_node)
12334 cp_finish_decl (decl,
12335 initializer, !is_non_constant_init,
12337 /* If the initializer is in parentheses, then this is
12338 a direct-initialization, which means that an
12339 `explicit' constructor is OK. Otherwise, an
12340 `explicit' constructor cannot be used. */
12341 ((is_parenthesized_init || !is_initialized)
12342 ? 0 : LOOKUP_ONLYCONVERTING));
12344 else if ((cxx_dialect != cxx98) && friend_p
12345 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12346 /* Core issue #226 (C++0x only): A default template-argument
12347 shall not be specified in a friend class template
12349 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12350 /*is_partial=*/0, /*is_friend_decl=*/1);
12352 if (!friend_p && pushed_scope)
12353 pop_scope (pushed_scope);
12358 /* Parse a declarator.
12362 ptr-operator declarator
12364 abstract-declarator:
12365 ptr-operator abstract-declarator [opt]
12366 direct-abstract-declarator
12371 attributes [opt] direct-declarator
12372 attributes [opt] ptr-operator declarator
12374 abstract-declarator:
12375 attributes [opt] ptr-operator abstract-declarator [opt]
12376 attributes [opt] direct-abstract-declarator
12378 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12379 detect constructor, destructor or conversion operators. It is set
12380 to -1 if the declarator is a name, and +1 if it is a
12381 function. Otherwise it is set to zero. Usually you just want to
12382 test for >0, but internally the negative value is used.
12384 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12385 a decl-specifier-seq unless it declares a constructor, destructor,
12386 or conversion. It might seem that we could check this condition in
12387 semantic analysis, rather than parsing, but that makes it difficult
12388 to handle something like `f()'. We want to notice that there are
12389 no decl-specifiers, and therefore realize that this is an
12390 expression, not a declaration.)
12392 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12393 the declarator is a direct-declarator of the form "(...)".
12395 MEMBER_P is true iff this declarator is a member-declarator. */
12397 static cp_declarator *
12398 cp_parser_declarator (cp_parser* parser,
12399 cp_parser_declarator_kind dcl_kind,
12400 int* ctor_dtor_or_conv_p,
12401 bool* parenthesized_p,
12405 cp_declarator *declarator;
12406 enum tree_code code;
12407 cp_cv_quals cv_quals;
12409 tree attributes = NULL_TREE;
12411 /* Assume this is not a constructor, destructor, or type-conversion
12413 if (ctor_dtor_or_conv_p)
12414 *ctor_dtor_or_conv_p = 0;
12416 if (cp_parser_allow_gnu_extensions_p (parser))
12417 attributes = cp_parser_attributes_opt (parser);
12419 /* Peek at the next token. */
12420 token = cp_lexer_peek_token (parser->lexer);
12422 /* Check for the ptr-operator production. */
12423 cp_parser_parse_tentatively (parser);
12424 /* Parse the ptr-operator. */
12425 code = cp_parser_ptr_operator (parser,
12428 /* If that worked, then we have a ptr-operator. */
12429 if (cp_parser_parse_definitely (parser))
12431 /* If a ptr-operator was found, then this declarator was not
12433 if (parenthesized_p)
12434 *parenthesized_p = true;
12435 /* The dependent declarator is optional if we are parsing an
12436 abstract-declarator. */
12437 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12438 cp_parser_parse_tentatively (parser);
12440 /* Parse the dependent declarator. */
12441 declarator = cp_parser_declarator (parser, dcl_kind,
12442 /*ctor_dtor_or_conv_p=*/NULL,
12443 /*parenthesized_p=*/NULL,
12444 /*member_p=*/false);
12446 /* If we are parsing an abstract-declarator, we must handle the
12447 case where the dependent declarator is absent. */
12448 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12449 && !cp_parser_parse_definitely (parser))
12452 declarator = cp_parser_make_indirect_declarator
12453 (code, class_type, cv_quals, declarator);
12455 /* Everything else is a direct-declarator. */
12458 if (parenthesized_p)
12459 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12461 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12462 ctor_dtor_or_conv_p,
12466 if (attributes && declarator && declarator != cp_error_declarator)
12467 declarator->attributes = attributes;
12472 /* Parse a direct-declarator or direct-abstract-declarator.
12476 direct-declarator ( parameter-declaration-clause )
12477 cv-qualifier-seq [opt]
12478 exception-specification [opt]
12479 direct-declarator [ constant-expression [opt] ]
12482 direct-abstract-declarator:
12483 direct-abstract-declarator [opt]
12484 ( parameter-declaration-clause )
12485 cv-qualifier-seq [opt]
12486 exception-specification [opt]
12487 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12488 ( abstract-declarator )
12490 Returns a representation of the declarator. DCL_KIND is
12491 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12492 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12493 we are parsing a direct-declarator. It is
12494 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12495 of ambiguity we prefer an abstract declarator, as per
12496 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12497 cp_parser_declarator. */
12499 static cp_declarator *
12500 cp_parser_direct_declarator (cp_parser* parser,
12501 cp_parser_declarator_kind dcl_kind,
12502 int* ctor_dtor_or_conv_p,
12506 cp_declarator *declarator = NULL;
12507 tree scope = NULL_TREE;
12508 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12509 bool saved_in_declarator_p = parser->in_declarator_p;
12511 tree pushed_scope = NULL_TREE;
12515 /* Peek at the next token. */
12516 token = cp_lexer_peek_token (parser->lexer);
12517 if (token->type == CPP_OPEN_PAREN)
12519 /* This is either a parameter-declaration-clause, or a
12520 parenthesized declarator. When we know we are parsing a
12521 named declarator, it must be a parenthesized declarator
12522 if FIRST is true. For instance, `(int)' is a
12523 parameter-declaration-clause, with an omitted
12524 direct-abstract-declarator. But `((*))', is a
12525 parenthesized abstract declarator. Finally, when T is a
12526 template parameter `(T)' is a
12527 parameter-declaration-clause, and not a parenthesized
12530 We first try and parse a parameter-declaration-clause,
12531 and then try a nested declarator (if FIRST is true).
12533 It is not an error for it not to be a
12534 parameter-declaration-clause, even when FIRST is
12540 The first is the declaration of a function while the
12541 second is a the definition of a variable, including its
12544 Having seen only the parenthesis, we cannot know which of
12545 these two alternatives should be selected. Even more
12546 complex are examples like:
12551 The former is a function-declaration; the latter is a
12552 variable initialization.
12554 Thus again, we try a parameter-declaration-clause, and if
12555 that fails, we back out and return. */
12557 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12559 cp_parameter_declarator *params;
12560 unsigned saved_num_template_parameter_lists;
12562 /* In a member-declarator, the only valid interpretation
12563 of a parenthesis is the start of a
12564 parameter-declaration-clause. (It is invalid to
12565 initialize a static data member with a parenthesized
12566 initializer; only the "=" form of initialization is
12569 cp_parser_parse_tentatively (parser);
12571 /* Consume the `('. */
12572 cp_lexer_consume_token (parser->lexer);
12575 /* If this is going to be an abstract declarator, we're
12576 in a declarator and we can't have default args. */
12577 parser->default_arg_ok_p = false;
12578 parser->in_declarator_p = true;
12581 /* Inside the function parameter list, surrounding
12582 template-parameter-lists do not apply. */
12583 saved_num_template_parameter_lists
12584 = parser->num_template_parameter_lists;
12585 parser->num_template_parameter_lists = 0;
12587 /* Parse the parameter-declaration-clause. */
12588 params = cp_parser_parameter_declaration_clause (parser);
12590 parser->num_template_parameter_lists
12591 = saved_num_template_parameter_lists;
12593 /* If all went well, parse the cv-qualifier-seq and the
12594 exception-specification. */
12595 if (member_p || cp_parser_parse_definitely (parser))
12597 cp_cv_quals cv_quals;
12598 tree exception_specification;
12600 if (ctor_dtor_or_conv_p)
12601 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12603 /* Consume the `)'. */
12604 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12606 /* Parse the cv-qualifier-seq. */
12607 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12608 /* And the exception-specification. */
12609 exception_specification
12610 = cp_parser_exception_specification_opt (parser);
12612 /* Create the function-declarator. */
12613 declarator = make_call_declarator (declarator,
12616 exception_specification);
12617 /* Any subsequent parameter lists are to do with
12618 return type, so are not those of the declared
12620 parser->default_arg_ok_p = false;
12622 /* Repeat the main loop. */
12627 /* If this is the first, we can try a parenthesized
12631 bool saved_in_type_id_in_expr_p;
12633 parser->default_arg_ok_p = saved_default_arg_ok_p;
12634 parser->in_declarator_p = saved_in_declarator_p;
12636 /* Consume the `('. */
12637 cp_lexer_consume_token (parser->lexer);
12638 /* Parse the nested declarator. */
12639 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12640 parser->in_type_id_in_expr_p = true;
12642 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12643 /*parenthesized_p=*/NULL,
12645 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12647 /* Expect a `)'. */
12648 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12649 declarator = cp_error_declarator;
12650 if (declarator == cp_error_declarator)
12653 goto handle_declarator;
12655 /* Otherwise, we must be done. */
12659 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12660 && token->type == CPP_OPEN_SQUARE)
12662 /* Parse an array-declarator. */
12665 if (ctor_dtor_or_conv_p)
12666 *ctor_dtor_or_conv_p = 0;
12669 parser->default_arg_ok_p = false;
12670 parser->in_declarator_p = true;
12671 /* Consume the `['. */
12672 cp_lexer_consume_token (parser->lexer);
12673 /* Peek at the next token. */
12674 token = cp_lexer_peek_token (parser->lexer);
12675 /* If the next token is `]', then there is no
12676 constant-expression. */
12677 if (token->type != CPP_CLOSE_SQUARE)
12679 bool non_constant_p;
12682 = cp_parser_constant_expression (parser,
12683 /*allow_non_constant=*/true,
12685 if (!non_constant_p)
12686 bounds = fold_non_dependent_expr (bounds);
12687 /* Normally, the array bound must be an integral constant
12688 expression. However, as an extension, we allow VLAs
12689 in function scopes. */
12690 else if (!parser->in_function_body)
12692 error ("array bound is not an integer constant");
12693 bounds = error_mark_node;
12697 bounds = NULL_TREE;
12698 /* Look for the closing `]'. */
12699 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12701 declarator = cp_error_declarator;
12705 declarator = make_array_declarator (declarator, bounds);
12707 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12709 tree qualifying_scope;
12710 tree unqualified_name;
12711 special_function_kind sfk;
12713 bool pack_expansion_p = false;
12715 /* Parse a declarator-id */
12716 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12719 cp_parser_parse_tentatively (parser);
12721 /* If we see an ellipsis, we should be looking at a
12723 if (token->type == CPP_ELLIPSIS)
12725 /* Consume the `...' */
12726 cp_lexer_consume_token (parser->lexer);
12728 pack_expansion_p = true;
12733 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12734 qualifying_scope = parser->scope;
12739 if (!unqualified_name && pack_expansion_p)
12741 /* Check whether an error occurred. */
12742 okay = !cp_parser_error_occurred (parser);
12744 /* We already consumed the ellipsis to mark a
12745 parameter pack, but we have no way to report it,
12746 so abort the tentative parse. We will be exiting
12747 immediately anyway. */
12748 cp_parser_abort_tentative_parse (parser);
12751 okay = cp_parser_parse_definitely (parser);
12754 unqualified_name = error_mark_node;
12755 else if (unqualified_name
12756 && (qualifying_scope
12757 || (TREE_CODE (unqualified_name)
12758 != IDENTIFIER_NODE)))
12760 cp_parser_error (parser, "expected unqualified-id");
12761 unqualified_name = error_mark_node;
12765 if (!unqualified_name)
12767 if (unqualified_name == error_mark_node)
12769 declarator = cp_error_declarator;
12770 pack_expansion_p = false;
12771 declarator->parameter_pack_p = false;
12775 if (qualifying_scope && at_namespace_scope_p ()
12776 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12778 /* In the declaration of a member of a template class
12779 outside of the class itself, the SCOPE will sometimes
12780 be a TYPENAME_TYPE. For example, given:
12782 template <typename T>
12783 int S<T>::R::i = 3;
12785 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12786 this context, we must resolve S<T>::R to an ordinary
12787 type, rather than a typename type.
12789 The reason we normally avoid resolving TYPENAME_TYPEs
12790 is that a specialization of `S' might render
12791 `S<T>::R' not a type. However, if `S' is
12792 specialized, then this `i' will not be used, so there
12793 is no harm in resolving the types here. */
12796 /* Resolve the TYPENAME_TYPE. */
12797 type = resolve_typename_type (qualifying_scope,
12798 /*only_current_p=*/false);
12799 /* If that failed, the declarator is invalid. */
12800 if (TREE_CODE (type) == TYPENAME_TYPE)
12801 error ("%<%T::%E%> is not a type",
12802 TYPE_CONTEXT (qualifying_scope),
12803 TYPE_IDENTIFIER (qualifying_scope));
12804 qualifying_scope = type;
12809 if (unqualified_name)
12813 if (qualifying_scope
12814 && CLASS_TYPE_P (qualifying_scope))
12815 class_type = qualifying_scope;
12817 class_type = current_class_type;
12819 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12821 tree name_type = TREE_TYPE (unqualified_name);
12822 if (class_type && same_type_p (name_type, class_type))
12824 if (qualifying_scope
12825 && CLASSTYPE_USE_TEMPLATE (name_type))
12827 error ("invalid use of constructor as a template");
12828 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12829 "name the constructor in a qualified name",
12831 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12832 class_type, name_type);
12833 declarator = cp_error_declarator;
12837 unqualified_name = constructor_name (class_type);
12841 /* We do not attempt to print the declarator
12842 here because we do not have enough
12843 information about its original syntactic
12845 cp_parser_error (parser, "invalid declarator");
12846 declarator = cp_error_declarator;
12853 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12854 sfk = sfk_destructor;
12855 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12856 sfk = sfk_conversion;
12857 else if (/* There's no way to declare a constructor
12858 for an anonymous type, even if the type
12859 got a name for linkage purposes. */
12860 !TYPE_WAS_ANONYMOUS (class_type)
12861 && constructor_name_p (unqualified_name,
12864 unqualified_name = constructor_name (class_type);
12865 sfk = sfk_constructor;
12868 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12869 *ctor_dtor_or_conv_p = -1;
12872 declarator = make_id_declarator (qualifying_scope,
12875 declarator->id_loc = token->location;
12876 declarator->parameter_pack_p = pack_expansion_p;
12878 if (pack_expansion_p)
12879 maybe_warn_variadic_templates ();
12881 handle_declarator:;
12882 scope = get_scope_of_declarator (declarator);
12884 /* Any names that appear after the declarator-id for a
12885 member are looked up in the containing scope. */
12886 pushed_scope = push_scope (scope);
12887 parser->in_declarator_p = true;
12888 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12889 || (declarator && declarator->kind == cdk_id))
12890 /* Default args are only allowed on function
12892 parser->default_arg_ok_p = saved_default_arg_ok_p;
12894 parser->default_arg_ok_p = false;
12903 /* For an abstract declarator, we might wind up with nothing at this
12904 point. That's an error; the declarator is not optional. */
12906 cp_parser_error (parser, "expected declarator");
12908 /* If we entered a scope, we must exit it now. */
12910 pop_scope (pushed_scope);
12912 parser->default_arg_ok_p = saved_default_arg_ok_p;
12913 parser->in_declarator_p = saved_in_declarator_p;
12918 /* Parse a ptr-operator.
12921 * cv-qualifier-seq [opt]
12923 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12928 & cv-qualifier-seq [opt]
12930 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12931 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12932 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12933 filled in with the TYPE containing the member. *CV_QUALS is
12934 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12935 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12936 Note that the tree codes returned by this function have nothing
12937 to do with the types of trees that will be eventually be created
12938 to represent the pointer or reference type being parsed. They are
12939 just constants with suggestive names. */
12940 static enum tree_code
12941 cp_parser_ptr_operator (cp_parser* parser,
12943 cp_cv_quals *cv_quals)
12945 enum tree_code code = ERROR_MARK;
12948 /* Assume that it's not a pointer-to-member. */
12950 /* And that there are no cv-qualifiers. */
12951 *cv_quals = TYPE_UNQUALIFIED;
12953 /* Peek at the next token. */
12954 token = cp_lexer_peek_token (parser->lexer);
12956 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12957 if (token->type == CPP_MULT)
12958 code = INDIRECT_REF;
12959 else if (token->type == CPP_AND)
12961 else if ((cxx_dialect != cxx98) &&
12962 token->type == CPP_AND_AND) /* C++0x only */
12963 code = NON_LVALUE_EXPR;
12965 if (code != ERROR_MARK)
12967 /* Consume the `*', `&' or `&&'. */
12968 cp_lexer_consume_token (parser->lexer);
12970 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12971 `&', if we are allowing GNU extensions. (The only qualifier
12972 that can legally appear after `&' is `restrict', but that is
12973 enforced during semantic analysis. */
12974 if (code == INDIRECT_REF
12975 || cp_parser_allow_gnu_extensions_p (parser))
12976 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12980 /* Try the pointer-to-member case. */
12981 cp_parser_parse_tentatively (parser);
12982 /* Look for the optional `::' operator. */
12983 cp_parser_global_scope_opt (parser,
12984 /*current_scope_valid_p=*/false);
12985 /* Look for the nested-name specifier. */
12986 cp_parser_nested_name_specifier (parser,
12987 /*typename_keyword_p=*/false,
12988 /*check_dependency_p=*/true,
12990 /*is_declaration=*/false);
12991 /* If we found it, and the next token is a `*', then we are
12992 indeed looking at a pointer-to-member operator. */
12993 if (!cp_parser_error_occurred (parser)
12994 && cp_parser_require (parser, CPP_MULT, "`*'"))
12996 /* Indicate that the `*' operator was used. */
12997 code = INDIRECT_REF;
12999 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
13000 error ("%qD is a namespace", parser->scope);
13003 /* The type of which the member is a member is given by the
13005 *type = parser->scope;
13006 /* The next name will not be qualified. */
13007 parser->scope = NULL_TREE;
13008 parser->qualifying_scope = NULL_TREE;
13009 parser->object_scope = NULL_TREE;
13010 /* Look for the optional cv-qualifier-seq. */
13011 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13014 /* If that didn't work we don't have a ptr-operator. */
13015 if (!cp_parser_parse_definitely (parser))
13016 cp_parser_error (parser, "expected ptr-operator");
13022 /* Parse an (optional) cv-qualifier-seq.
13025 cv-qualifier cv-qualifier-seq [opt]
13036 Returns a bitmask representing the cv-qualifiers. */
13039 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
13041 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
13046 cp_cv_quals cv_qualifier;
13048 /* Peek at the next token. */
13049 token = cp_lexer_peek_token (parser->lexer);
13050 /* See if it's a cv-qualifier. */
13051 switch (token->keyword)
13054 cv_qualifier = TYPE_QUAL_CONST;
13058 cv_qualifier = TYPE_QUAL_VOLATILE;
13062 cv_qualifier = TYPE_QUAL_RESTRICT;
13066 cv_qualifier = TYPE_UNQUALIFIED;
13073 if (cv_quals & cv_qualifier)
13075 error ("duplicate cv-qualifier");
13076 cp_lexer_purge_token (parser->lexer);
13080 cp_lexer_consume_token (parser->lexer);
13081 cv_quals |= cv_qualifier;
13088 /* Parse a declarator-id.
13092 :: [opt] nested-name-specifier [opt] type-name
13094 In the `id-expression' case, the value returned is as for
13095 cp_parser_id_expression if the id-expression was an unqualified-id.
13096 If the id-expression was a qualified-id, then a SCOPE_REF is
13097 returned. The first operand is the scope (either a NAMESPACE_DECL
13098 or TREE_TYPE), but the second is still just a representation of an
13102 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13105 /* The expression must be an id-expression. Assume that qualified
13106 names are the names of types so that:
13109 int S<T>::R::i = 3;
13111 will work; we must treat `S<T>::R' as the name of a type.
13112 Similarly, assume that qualified names are templates, where
13116 int S<T>::R<T>::i = 3;
13119 id = cp_parser_id_expression (parser,
13120 /*template_keyword_p=*/false,
13121 /*check_dependency_p=*/false,
13122 /*template_p=*/NULL,
13123 /*declarator_p=*/true,
13125 if (id && BASELINK_P (id))
13126 id = BASELINK_FUNCTIONS (id);
13130 /* Parse a type-id.
13133 type-specifier-seq abstract-declarator [opt]
13135 Returns the TYPE specified. */
13138 cp_parser_type_id (cp_parser* parser)
13140 cp_decl_specifier_seq type_specifier_seq;
13141 cp_declarator *abstract_declarator;
13143 /* Parse the type-specifier-seq. */
13144 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13145 &type_specifier_seq);
13146 if (type_specifier_seq.type == error_mark_node)
13147 return error_mark_node;
13149 /* There might or might not be an abstract declarator. */
13150 cp_parser_parse_tentatively (parser);
13151 /* Look for the declarator. */
13152 abstract_declarator
13153 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13154 /*parenthesized_p=*/NULL,
13155 /*member_p=*/false);
13156 /* Check to see if there really was a declarator. */
13157 if (!cp_parser_parse_definitely (parser))
13158 abstract_declarator = NULL;
13160 return groktypename (&type_specifier_seq, abstract_declarator);
13163 /* Parse a type-specifier-seq.
13165 type-specifier-seq:
13166 type-specifier type-specifier-seq [opt]
13170 type-specifier-seq:
13171 attributes type-specifier-seq [opt]
13173 If IS_CONDITION is true, we are at the start of a "condition",
13174 e.g., we've just seen "if (".
13176 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13179 cp_parser_type_specifier_seq (cp_parser* parser,
13181 cp_decl_specifier_seq *type_specifier_seq)
13183 bool seen_type_specifier = false;
13184 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13186 /* Clear the TYPE_SPECIFIER_SEQ. */
13187 clear_decl_specs (type_specifier_seq);
13189 /* Parse the type-specifiers and attributes. */
13192 tree type_specifier;
13193 bool is_cv_qualifier;
13195 /* Check for attributes first. */
13196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13198 type_specifier_seq->attributes =
13199 chainon (type_specifier_seq->attributes,
13200 cp_parser_attributes_opt (parser));
13204 /* Look for the type-specifier. */
13205 type_specifier = cp_parser_type_specifier (parser,
13207 type_specifier_seq,
13208 /*is_declaration=*/false,
13211 if (!type_specifier)
13213 /* If the first type-specifier could not be found, this is not a
13214 type-specifier-seq at all. */
13215 if (!seen_type_specifier)
13217 cp_parser_error (parser, "expected type-specifier");
13218 type_specifier_seq->type = error_mark_node;
13221 /* If subsequent type-specifiers could not be found, the
13222 type-specifier-seq is complete. */
13226 seen_type_specifier = true;
13227 /* The standard says that a condition can be:
13229 type-specifier-seq declarator = assignment-expression
13236 we should treat the "S" as a declarator, not as a
13237 type-specifier. The standard doesn't say that explicitly for
13238 type-specifier-seq, but it does say that for
13239 decl-specifier-seq in an ordinary declaration. Perhaps it
13240 would be clearer just to allow a decl-specifier-seq here, and
13241 then add a semantic restriction that if any decl-specifiers
13242 that are not type-specifiers appear, the program is invalid. */
13243 if (is_condition && !is_cv_qualifier)
13244 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13247 cp_parser_check_decl_spec (type_specifier_seq);
13250 /* Parse a parameter-declaration-clause.
13252 parameter-declaration-clause:
13253 parameter-declaration-list [opt] ... [opt]
13254 parameter-declaration-list , ...
13256 Returns a representation for the parameter declarations. A return
13257 value of NULL indicates a parameter-declaration-clause consisting
13258 only of an ellipsis. */
13260 static cp_parameter_declarator *
13261 cp_parser_parameter_declaration_clause (cp_parser* parser)
13263 cp_parameter_declarator *parameters;
13268 /* Peek at the next token. */
13269 token = cp_lexer_peek_token (parser->lexer);
13270 /* Check for trivial parameter-declaration-clauses. */
13271 if (token->type == CPP_ELLIPSIS)
13273 /* Consume the `...' token. */
13274 cp_lexer_consume_token (parser->lexer);
13277 else if (token->type == CPP_CLOSE_PAREN)
13278 /* There are no parameters. */
13280 #ifndef NO_IMPLICIT_EXTERN_C
13281 if (in_system_header && current_class_type == NULL
13282 && current_lang_name == lang_name_c)
13286 return no_parameters;
13288 /* Check for `(void)', too, which is a special case. */
13289 else if (token->keyword == RID_VOID
13290 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13291 == CPP_CLOSE_PAREN))
13293 /* Consume the `void' token. */
13294 cp_lexer_consume_token (parser->lexer);
13295 /* There are no parameters. */
13296 return no_parameters;
13299 /* Parse the parameter-declaration-list. */
13300 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13301 /* If a parse error occurred while parsing the
13302 parameter-declaration-list, then the entire
13303 parameter-declaration-clause is erroneous. */
13307 /* Peek at the next token. */
13308 token = cp_lexer_peek_token (parser->lexer);
13309 /* If it's a `,', the clause should terminate with an ellipsis. */
13310 if (token->type == CPP_COMMA)
13312 /* Consume the `,'. */
13313 cp_lexer_consume_token (parser->lexer);
13314 /* Expect an ellipsis. */
13316 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13318 /* It might also be `...' if the optional trailing `,' was
13320 else if (token->type == CPP_ELLIPSIS)
13322 /* Consume the `...' token. */
13323 cp_lexer_consume_token (parser->lexer);
13324 /* And remember that we saw it. */
13328 ellipsis_p = false;
13330 /* Finish the parameter list. */
13331 if (parameters && ellipsis_p)
13332 parameters->ellipsis_p = true;
13337 /* Parse a parameter-declaration-list.
13339 parameter-declaration-list:
13340 parameter-declaration
13341 parameter-declaration-list , parameter-declaration
13343 Returns a representation of the parameter-declaration-list, as for
13344 cp_parser_parameter_declaration_clause. However, the
13345 `void_list_node' is never appended to the list. Upon return,
13346 *IS_ERROR will be true iff an error occurred. */
13348 static cp_parameter_declarator *
13349 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13351 cp_parameter_declarator *parameters = NULL;
13352 cp_parameter_declarator **tail = ¶meters;
13353 bool saved_in_unbraced_linkage_specification_p;
13355 /* Assume all will go well. */
13357 /* The special considerations that apply to a function within an
13358 unbraced linkage specifications do not apply to the parameters
13359 to the function. */
13360 saved_in_unbraced_linkage_specification_p
13361 = parser->in_unbraced_linkage_specification_p;
13362 parser->in_unbraced_linkage_specification_p = false;
13364 /* Look for more parameters. */
13367 cp_parameter_declarator *parameter;
13368 bool parenthesized_p;
13369 /* Parse the parameter. */
13371 = cp_parser_parameter_declaration (parser,
13372 /*template_parm_p=*/false,
13375 /* If a parse error occurred parsing the parameter declaration,
13376 then the entire parameter-declaration-list is erroneous. */
13383 /* Add the new parameter to the list. */
13385 tail = ¶meter->next;
13387 /* Peek at the next token. */
13388 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13389 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13390 /* These are for Objective-C++ */
13391 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13392 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13393 /* The parameter-declaration-list is complete. */
13395 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13399 /* Peek at the next token. */
13400 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13401 /* If it's an ellipsis, then the list is complete. */
13402 if (token->type == CPP_ELLIPSIS)
13404 /* Otherwise, there must be more parameters. Consume the
13406 cp_lexer_consume_token (parser->lexer);
13407 /* When parsing something like:
13409 int i(float f, double d)
13411 we can tell after seeing the declaration for "f" that we
13412 are not looking at an initialization of a variable "i",
13413 but rather at the declaration of a function "i".
13415 Due to the fact that the parsing of template arguments
13416 (as specified to a template-id) requires backtracking we
13417 cannot use this technique when inside a template argument
13419 if (!parser->in_template_argument_list_p
13420 && !parser->in_type_id_in_expr_p
13421 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13422 /* However, a parameter-declaration of the form
13423 "foat(f)" (which is a valid declaration of a
13424 parameter "f") can also be interpreted as an
13425 expression (the conversion of "f" to "float"). */
13426 && !parenthesized_p)
13427 cp_parser_commit_to_tentative_parse (parser);
13431 cp_parser_error (parser, "expected %<,%> or %<...%>");
13432 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13433 cp_parser_skip_to_closing_parenthesis (parser,
13434 /*recovering=*/true,
13435 /*or_comma=*/false,
13436 /*consume_paren=*/false);
13441 parser->in_unbraced_linkage_specification_p
13442 = saved_in_unbraced_linkage_specification_p;
13447 /* Parse a parameter declaration.
13449 parameter-declaration:
13450 decl-specifier-seq ... [opt] declarator
13451 decl-specifier-seq declarator = assignment-expression
13452 decl-specifier-seq ... [opt] abstract-declarator [opt]
13453 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13455 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13456 declares a template parameter. (In that case, a non-nested `>'
13457 token encountered during the parsing of the assignment-expression
13458 is not interpreted as a greater-than operator.)
13460 Returns a representation of the parameter, or NULL if an error
13461 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13462 true iff the declarator is of the form "(p)". */
13464 static cp_parameter_declarator *
13465 cp_parser_parameter_declaration (cp_parser *parser,
13466 bool template_parm_p,
13467 bool *parenthesized_p)
13469 int declares_class_or_enum;
13470 bool greater_than_is_operator_p;
13471 cp_decl_specifier_seq decl_specifiers;
13472 cp_declarator *declarator;
13473 tree default_argument;
13475 const char *saved_message;
13477 /* In a template parameter, `>' is not an operator.
13481 When parsing a default template-argument for a non-type
13482 template-parameter, the first non-nested `>' is taken as the end
13483 of the template parameter-list rather than a greater-than
13485 greater_than_is_operator_p = !template_parm_p;
13487 /* Type definitions may not appear in parameter types. */
13488 saved_message = parser->type_definition_forbidden_message;
13489 parser->type_definition_forbidden_message
13490 = "types may not be defined in parameter types";
13492 /* Parse the declaration-specifiers. */
13493 cp_parser_decl_specifier_seq (parser,
13494 CP_PARSER_FLAGS_NONE,
13496 &declares_class_or_enum);
13497 /* If an error occurred, there's no reason to attempt to parse the
13498 rest of the declaration. */
13499 if (cp_parser_error_occurred (parser))
13501 parser->type_definition_forbidden_message = saved_message;
13505 /* Peek at the next token. */
13506 token = cp_lexer_peek_token (parser->lexer);
13508 /* If the next token is a `)', `,', `=', `>', or `...', then there
13509 is no declarator. However, when variadic templates are enabled,
13510 there may be a declarator following `...'. */
13511 if (token->type == CPP_CLOSE_PAREN
13512 || token->type == CPP_COMMA
13513 || token->type == CPP_EQ
13514 || token->type == CPP_GREATER)
13517 if (parenthesized_p)
13518 *parenthesized_p = false;
13520 /* Otherwise, there should be a declarator. */
13523 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13524 parser->default_arg_ok_p = false;
13526 /* After seeing a decl-specifier-seq, if the next token is not a
13527 "(", there is no possibility that the code is a valid
13528 expression. Therefore, if parsing tentatively, we commit at
13530 if (!parser->in_template_argument_list_p
13531 /* In an expression context, having seen:
13535 we cannot be sure whether we are looking at a
13536 function-type (taking a "char" as a parameter) or a cast
13537 of some object of type "char" to "int". */
13538 && !parser->in_type_id_in_expr_p
13539 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13540 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13541 cp_parser_commit_to_tentative_parse (parser);
13542 /* Parse the declarator. */
13543 declarator = cp_parser_declarator (parser,
13544 CP_PARSER_DECLARATOR_EITHER,
13545 /*ctor_dtor_or_conv_p=*/NULL,
13547 /*member_p=*/false);
13548 parser->default_arg_ok_p = saved_default_arg_ok_p;
13549 /* After the declarator, allow more attributes. */
13550 decl_specifiers.attributes
13551 = chainon (decl_specifiers.attributes,
13552 cp_parser_attributes_opt (parser));
13555 /* If the next token is an ellipsis, and we have not seen a
13556 declarator name, and the type of the declarator contains parameter
13557 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13558 a parameter pack expansion expression. Otherwise, leave the
13559 ellipsis for a C-style variadic function. */
13560 token = cp_lexer_peek_token (parser->lexer);
13561 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13563 tree type = decl_specifiers.type;
13565 if (type && DECL_P (type))
13566 type = TREE_TYPE (type);
13569 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13570 && declarator_can_be_parameter_pack (declarator)
13571 && (!declarator || !declarator->parameter_pack_p)
13572 && uses_parameter_packs (type))
13574 /* Consume the `...'. */
13575 cp_lexer_consume_token (parser->lexer);
13576 maybe_warn_variadic_templates ();
13578 /* Build a pack expansion type */
13580 declarator->parameter_pack_p = true;
13582 decl_specifiers.type = make_pack_expansion (type);
13586 /* The restriction on defining new types applies only to the type
13587 of the parameter, not to the default argument. */
13588 parser->type_definition_forbidden_message = saved_message;
13590 /* If the next token is `=', then process a default argument. */
13591 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13593 /* Consume the `='. */
13594 cp_lexer_consume_token (parser->lexer);
13596 /* If we are defining a class, then the tokens that make up the
13597 default argument must be saved and processed later. */
13598 if (!template_parm_p && at_class_scope_p ()
13599 && TYPE_BEING_DEFINED (current_class_type))
13601 unsigned depth = 0;
13602 cp_token *first_token;
13605 /* Add tokens until we have processed the entire default
13606 argument. We add the range [first_token, token). */
13607 first_token = cp_lexer_peek_token (parser->lexer);
13612 /* Peek at the next token. */
13613 token = cp_lexer_peek_token (parser->lexer);
13614 /* What we do depends on what token we have. */
13615 switch (token->type)
13617 /* In valid code, a default argument must be
13618 immediately followed by a `,' `)', or `...'. */
13620 case CPP_CLOSE_PAREN:
13622 /* If we run into a non-nested `;', `}', or `]',
13623 then the code is invalid -- but the default
13624 argument is certainly over. */
13625 case CPP_SEMICOLON:
13626 case CPP_CLOSE_BRACE:
13627 case CPP_CLOSE_SQUARE:
13630 /* Update DEPTH, if necessary. */
13631 else if (token->type == CPP_CLOSE_PAREN
13632 || token->type == CPP_CLOSE_BRACE
13633 || token->type == CPP_CLOSE_SQUARE)
13637 case CPP_OPEN_PAREN:
13638 case CPP_OPEN_SQUARE:
13639 case CPP_OPEN_BRACE:
13644 if (cxx_dialect == cxx98)
13646 /* Fall through for C++0x, which treats the `>>'
13647 operator like two `>' tokens in certain
13651 /* If we see a non-nested `>', and `>' is not an
13652 operator, then it marks the end of the default
13654 if (!depth && !greater_than_is_operator_p)
13658 /* If we run out of tokens, issue an error message. */
13660 case CPP_PRAGMA_EOL:
13661 error ("file ends in default argument");
13667 /* In these cases, we should look for template-ids.
13668 For example, if the default argument is
13669 `X<int, double>()', we need to do name lookup to
13670 figure out whether or not `X' is a template; if
13671 so, the `,' does not end the default argument.
13673 That is not yet done. */
13680 /* If we've reached the end, stop. */
13684 /* Add the token to the token block. */
13685 token = cp_lexer_consume_token (parser->lexer);
13688 /* Create a DEFAULT_ARG to represent the unparsed default
13690 default_argument = make_node (DEFAULT_ARG);
13691 DEFARG_TOKENS (default_argument)
13692 = cp_token_cache_new (first_token, token);
13693 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13695 /* Outside of a class definition, we can just parse the
13696 assignment-expression. */
13699 = cp_parser_default_argument (parser, template_parm_p);
13701 if (!parser->default_arg_ok_p)
13703 if (!flag_pedantic_errors)
13704 warning (0, "deprecated use of default argument for parameter of non-function");
13707 error ("default arguments are only permitted for function parameters");
13708 default_argument = NULL_TREE;
13711 else if ((declarator && declarator->parameter_pack_p)
13712 || (decl_specifiers.type
13713 && PACK_EXPANSION_P (decl_specifiers.type)))
13715 const char* kind = template_parm_p? "template " : "";
13717 /* Find the name of the parameter pack. */
13718 cp_declarator *id_declarator = declarator;
13719 while (id_declarator && id_declarator->kind != cdk_id)
13720 id_declarator = id_declarator->declarator;
13722 if (id_declarator && id_declarator->kind == cdk_id)
13723 error ("%sparameter pack %qD cannot have a default argument",
13724 kind, id_declarator->u.id.unqualified_name);
13726 error ("%sparameter pack cannot have a default argument",
13729 default_argument = NULL_TREE;
13733 default_argument = NULL_TREE;
13735 return make_parameter_declarator (&decl_specifiers,
13740 /* Parse a default argument and return it.
13742 TEMPLATE_PARM_P is true if this is a default argument for a
13743 non-type template parameter. */
13745 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
13747 tree default_argument = NULL_TREE;
13748 bool saved_greater_than_is_operator_p;
13749 bool saved_local_variables_forbidden_p;
13751 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13753 saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
13754 parser->greater_than_is_operator_p = !template_parm_p;
13755 /* Local variable names (and the `this' keyword) may not
13756 appear in a default argument. */
13757 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
13758 parser->local_variables_forbidden_p = true;
13759 /* The default argument expression may cause implicitly
13760 defined member functions to be synthesized, which will
13761 result in garbage collection. We must treat this
13762 situation as if we were within the body of function so as
13763 to avoid collecting live data on the stack. */
13765 /* Parse the assignment-expression. */
13766 if (template_parm_p)
13767 push_deferring_access_checks (dk_no_deferred);
13769 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13770 if (template_parm_p)
13771 pop_deferring_access_checks ();
13772 /* Restore saved state. */
13774 parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
13775 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
13777 return default_argument;
13780 /* Parse a function-body.
13783 compound_statement */
13786 cp_parser_function_body (cp_parser *parser)
13788 cp_parser_compound_statement (parser, NULL, false);
13791 /* Parse a ctor-initializer-opt followed by a function-body. Return
13792 true if a ctor-initializer was present. */
13795 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13798 bool ctor_initializer_p;
13800 /* Begin the function body. */
13801 body = begin_function_body ();
13802 /* Parse the optional ctor-initializer. */
13803 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13804 /* Parse the function-body. */
13805 cp_parser_function_body (parser);
13806 /* Finish the function body. */
13807 finish_function_body (body);
13809 return ctor_initializer_p;
13812 /* Parse an initializer.
13815 = initializer-clause
13816 ( expression-list )
13818 Returns an expression representing the initializer. If no
13819 initializer is present, NULL_TREE is returned.
13821 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13822 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13823 set to FALSE if there is no initializer present. If there is an
13824 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13825 is set to true; otherwise it is set to false. */
13828 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13829 bool* non_constant_p)
13834 /* Peek at the next token. */
13835 token = cp_lexer_peek_token (parser->lexer);
13837 /* Let our caller know whether or not this initializer was
13839 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13840 /* Assume that the initializer is constant. */
13841 *non_constant_p = false;
13843 if (token->type == CPP_EQ)
13845 /* Consume the `='. */
13846 cp_lexer_consume_token (parser->lexer);
13847 /* Parse the initializer-clause. */
13848 init = cp_parser_initializer_clause (parser, non_constant_p);
13850 else if (token->type == CPP_OPEN_PAREN)
13851 init = cp_parser_parenthesized_expression_list (parser, false,
13853 /*allow_expansion_p=*/true,
13857 /* Anything else is an error. */
13858 cp_parser_error (parser, "expected initializer");
13859 init = error_mark_node;
13865 /* Parse an initializer-clause.
13867 initializer-clause:
13868 assignment-expression
13869 { initializer-list , [opt] }
13872 Returns an expression representing the initializer.
13874 If the `assignment-expression' production is used the value
13875 returned is simply a representation for the expression.
13877 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13878 the elements of the initializer-list (or NULL, if the last
13879 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13880 NULL_TREE. There is no way to detect whether or not the optional
13881 trailing `,' was provided. NON_CONSTANT_P is as for
13882 cp_parser_initializer. */
13885 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13889 /* Assume the expression is constant. */
13890 *non_constant_p = false;
13892 /* If it is not a `{', then we are looking at an
13893 assignment-expression. */
13894 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13897 = cp_parser_constant_expression (parser,
13898 /*allow_non_constant_p=*/true,
13900 if (!*non_constant_p)
13901 initializer = fold_non_dependent_expr (initializer);
13905 /* Consume the `{' token. */
13906 cp_lexer_consume_token (parser->lexer);
13907 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13908 initializer = make_node (CONSTRUCTOR);
13909 /* If it's not a `}', then there is a non-trivial initializer. */
13910 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13912 /* Parse the initializer list. */
13913 CONSTRUCTOR_ELTS (initializer)
13914 = cp_parser_initializer_list (parser, non_constant_p);
13915 /* A trailing `,' token is allowed. */
13916 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13917 cp_lexer_consume_token (parser->lexer);
13919 /* Now, there should be a trailing `}'. */
13920 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13923 return initializer;
13926 /* Parse an initializer-list.
13929 initializer-clause ... [opt]
13930 initializer-list , initializer-clause ... [opt]
13935 identifier : initializer-clause
13936 initializer-list, identifier : initializer-clause
13938 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13939 for the initializer. If the INDEX of the elt is non-NULL, it is the
13940 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13941 as for cp_parser_initializer. */
13943 static VEC(constructor_elt,gc) *
13944 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13946 VEC(constructor_elt,gc) *v = NULL;
13948 /* Assume all of the expressions are constant. */
13949 *non_constant_p = false;
13951 /* Parse the rest of the list. */
13957 bool clause_non_constant_p;
13959 /* If the next token is an identifier and the following one is a
13960 colon, we are looking at the GNU designated-initializer
13962 if (cp_parser_allow_gnu_extensions_p (parser)
13963 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13964 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13966 /* Warn the user that they are using an extension. */
13968 pedwarn ("ISO C++ does not allow designated initializers");
13969 /* Consume the identifier. */
13970 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13971 /* Consume the `:'. */
13972 cp_lexer_consume_token (parser->lexer);
13975 identifier = NULL_TREE;
13977 /* Parse the initializer. */
13978 initializer = cp_parser_initializer_clause (parser,
13979 &clause_non_constant_p);
13980 /* If any clause is non-constant, so is the entire initializer. */
13981 if (clause_non_constant_p)
13982 *non_constant_p = true;
13984 /* If we have an ellipsis, this is an initializer pack
13986 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13988 /* Consume the `...'. */
13989 cp_lexer_consume_token (parser->lexer);
13991 /* Turn the initializer into an initializer expansion. */
13992 initializer = make_pack_expansion (initializer);
13995 /* Add it to the vector. */
13996 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13998 /* If the next token is not a comma, we have reached the end of
14000 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14003 /* Peek at the next token. */
14004 token = cp_lexer_peek_nth_token (parser->lexer, 2);
14005 /* If the next token is a `}', then we're still done. An
14006 initializer-clause can have a trailing `,' after the
14007 initializer-list and before the closing `}'. */
14008 if (token->type == CPP_CLOSE_BRACE)
14011 /* Consume the `,' token. */
14012 cp_lexer_consume_token (parser->lexer);
14018 /* Classes [gram.class] */
14020 /* Parse a class-name.
14026 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
14027 to indicate that names looked up in dependent types should be
14028 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
14029 keyword has been used to indicate that the name that appears next
14030 is a template. TAG_TYPE indicates the explicit tag given before
14031 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
14032 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
14033 is the class being defined in a class-head.
14035 Returns the TYPE_DECL representing the class. */
14038 cp_parser_class_name (cp_parser *parser,
14039 bool typename_keyword_p,
14040 bool template_keyword_p,
14041 enum tag_types tag_type,
14042 bool check_dependency_p,
14044 bool is_declaration)
14051 /* All class-names start with an identifier. */
14052 token = cp_lexer_peek_token (parser->lexer);
14053 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
14055 cp_parser_error (parser, "expected class-name");
14056 return error_mark_node;
14059 /* PARSER->SCOPE can be cleared when parsing the template-arguments
14060 to a template-id, so we save it here. */
14061 scope = parser->scope;
14062 if (scope == error_mark_node)
14063 return error_mark_node;
14065 /* Any name names a type if we're following the `typename' keyword
14066 in a qualified name where the enclosing scope is type-dependent. */
14067 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
14068 && dependent_type_p (scope));
14069 /* Handle the common case (an identifier, but not a template-id)
14071 if (token->type == CPP_NAME
14072 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
14074 cp_token *identifier_token;
14078 /* Look for the identifier. */
14079 identifier_token = cp_lexer_peek_token (parser->lexer);
14080 ambiguous_p = identifier_token->ambiguous_p;
14081 identifier = cp_parser_identifier (parser);
14082 /* If the next token isn't an identifier, we are certainly not
14083 looking at a class-name. */
14084 if (identifier == error_mark_node)
14085 decl = error_mark_node;
14086 /* If we know this is a type-name, there's no need to look it
14088 else if (typename_p)
14092 tree ambiguous_decls;
14093 /* If we already know that this lookup is ambiguous, then
14094 we've already issued an error message; there's no reason
14098 cp_parser_simulate_error (parser);
14099 return error_mark_node;
14101 /* If the next token is a `::', then the name must be a type
14104 [basic.lookup.qual]
14106 During the lookup for a name preceding the :: scope
14107 resolution operator, object, function, and enumerator
14108 names are ignored. */
14109 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14110 tag_type = typename_type;
14111 /* Look up the name. */
14112 decl = cp_parser_lookup_name (parser, identifier,
14114 /*is_template=*/false,
14115 /*is_namespace=*/false,
14116 check_dependency_p,
14118 if (ambiguous_decls)
14120 error ("reference to %qD is ambiguous", identifier);
14121 print_candidates (ambiguous_decls);
14122 if (cp_parser_parsing_tentatively (parser))
14124 identifier_token->ambiguous_p = true;
14125 cp_parser_simulate_error (parser);
14127 return error_mark_node;
14133 /* Try a template-id. */
14134 decl = cp_parser_template_id (parser, template_keyword_p,
14135 check_dependency_p,
14137 if (decl == error_mark_node)
14138 return error_mark_node;
14141 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14143 /* If this is a typename, create a TYPENAME_TYPE. */
14144 if (typename_p && decl != error_mark_node)
14146 decl = make_typename_type (scope, decl, typename_type,
14147 /*complain=*/tf_error);
14148 if (decl != error_mark_node)
14149 decl = TYPE_NAME (decl);
14152 /* Check to see that it is really the name of a class. */
14153 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14154 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14155 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14156 /* Situations like this:
14158 template <typename T> struct A {
14159 typename T::template X<int>::I i;
14162 are problematic. Is `T::template X<int>' a class-name? The
14163 standard does not seem to be definitive, but there is no other
14164 valid interpretation of the following `::'. Therefore, those
14165 names are considered class-names. */
14167 decl = make_typename_type (scope, decl, tag_type, tf_error);
14168 if (decl != error_mark_node)
14169 decl = TYPE_NAME (decl);
14171 else if (TREE_CODE (decl) != TYPE_DECL
14172 || TREE_TYPE (decl) == error_mark_node
14173 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14174 decl = error_mark_node;
14176 if (decl == error_mark_node)
14177 cp_parser_error (parser, "expected class-name");
14182 /* Parse a class-specifier.
14185 class-head { member-specification [opt] }
14187 Returns the TREE_TYPE representing the class. */
14190 cp_parser_class_specifier (cp_parser* parser)
14194 tree attributes = NULL_TREE;
14195 int has_trailing_semicolon;
14196 bool nested_name_specifier_p;
14197 unsigned saved_num_template_parameter_lists;
14198 bool saved_in_function_body;
14199 tree old_scope = NULL_TREE;
14200 tree scope = NULL_TREE;
14203 push_deferring_access_checks (dk_no_deferred);
14205 /* Parse the class-head. */
14206 type = cp_parser_class_head (parser,
14207 &nested_name_specifier_p,
14210 /* If the class-head was a semantic disaster, skip the entire body
14214 cp_parser_skip_to_end_of_block_or_statement (parser);
14215 pop_deferring_access_checks ();
14216 return error_mark_node;
14219 /* Look for the `{'. */
14220 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14222 pop_deferring_access_checks ();
14223 return error_mark_node;
14226 /* Process the base classes. If they're invalid, skip the
14227 entire class body. */
14228 if (!xref_basetypes (type, bases))
14230 /* Consuming the closing brace yields better error messages
14232 if (cp_parser_skip_to_closing_brace (parser))
14233 cp_lexer_consume_token (parser->lexer);
14234 pop_deferring_access_checks ();
14235 return error_mark_node;
14238 /* Issue an error message if type-definitions are forbidden here. */
14239 cp_parser_check_type_definition (parser);
14240 /* Remember that we are defining one more class. */
14241 ++parser->num_classes_being_defined;
14242 /* Inside the class, surrounding template-parameter-lists do not
14244 saved_num_template_parameter_lists
14245 = parser->num_template_parameter_lists;
14246 parser->num_template_parameter_lists = 0;
14247 /* We are not in a function body. */
14248 saved_in_function_body = parser->in_function_body;
14249 parser->in_function_body = false;
14251 /* Start the class. */
14252 if (nested_name_specifier_p)
14254 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14255 old_scope = push_inner_scope (scope);
14257 type = begin_class_definition (type, attributes);
14259 if (type == error_mark_node)
14260 /* If the type is erroneous, skip the entire body of the class. */
14261 cp_parser_skip_to_closing_brace (parser);
14263 /* Parse the member-specification. */
14264 cp_parser_member_specification_opt (parser);
14266 /* Look for the trailing `}'. */
14267 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14268 /* We get better error messages by noticing a common problem: a
14269 missing trailing `;'. */
14270 token = cp_lexer_peek_token (parser->lexer);
14271 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14272 /* Look for trailing attributes to apply to this class. */
14273 if (cp_parser_allow_gnu_extensions_p (parser))
14274 attributes = cp_parser_attributes_opt (parser);
14275 if (type != error_mark_node)
14276 type = finish_struct (type, attributes);
14277 if (nested_name_specifier_p)
14278 pop_inner_scope (old_scope, scope);
14279 /* If this class is not itself within the scope of another class,
14280 then we need to parse the bodies of all of the queued function
14281 definitions. Note that the queued functions defined in a class
14282 are not always processed immediately following the
14283 class-specifier for that class. Consider:
14286 struct B { void f() { sizeof (A); } };
14289 If `f' were processed before the processing of `A' were
14290 completed, there would be no way to compute the size of `A'.
14291 Note that the nesting we are interested in here is lexical --
14292 not the semantic nesting given by TYPE_CONTEXT. In particular,
14295 struct A { struct B; };
14296 struct A::B { void f() { } };
14298 there is no need to delay the parsing of `A::B::f'. */
14299 if (--parser->num_classes_being_defined == 0)
14303 tree class_type = NULL_TREE;
14304 tree pushed_scope = NULL_TREE;
14306 /* In a first pass, parse default arguments to the functions.
14307 Then, in a second pass, parse the bodies of the functions.
14308 This two-phased approach handles cases like:
14316 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14317 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14318 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14319 TREE_PURPOSE (parser->unparsed_functions_queues)
14320 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14322 fn = TREE_VALUE (queue_entry);
14323 /* If there are default arguments that have not yet been processed,
14324 take care of them now. */
14325 if (class_type != TREE_PURPOSE (queue_entry))
14328 pop_scope (pushed_scope);
14329 class_type = TREE_PURPOSE (queue_entry);
14330 pushed_scope = push_scope (class_type);
14332 /* Make sure that any template parameters are in scope. */
14333 maybe_begin_member_template_processing (fn);
14334 /* Parse the default argument expressions. */
14335 cp_parser_late_parsing_default_args (parser, fn);
14336 /* Remove any template parameters from the symbol table. */
14337 maybe_end_member_template_processing ();
14340 pop_scope (pushed_scope);
14341 /* Now parse the body of the functions. */
14342 for (TREE_VALUE (parser->unparsed_functions_queues)
14343 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14344 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14345 TREE_VALUE (parser->unparsed_functions_queues)
14346 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14348 /* Figure out which function we need to process. */
14349 fn = TREE_VALUE (queue_entry);
14350 /* Parse the function. */
14351 cp_parser_late_parsing_for_member (parser, fn);
14355 /* Put back any saved access checks. */
14356 pop_deferring_access_checks ();
14358 /* Restore saved state. */
14359 parser->in_function_body = saved_in_function_body;
14360 parser->num_template_parameter_lists
14361 = saved_num_template_parameter_lists;
14366 /* Parse a class-head.
14369 class-key identifier [opt] base-clause [opt]
14370 class-key nested-name-specifier identifier base-clause [opt]
14371 class-key nested-name-specifier [opt] template-id
14375 class-key attributes identifier [opt] base-clause [opt]
14376 class-key attributes nested-name-specifier identifier base-clause [opt]
14377 class-key attributes nested-name-specifier [opt] template-id
14380 Upon return BASES is initialized to the list of base classes (or
14381 NULL, if there are none) in the same form returned by
14382 cp_parser_base_clause.
14384 Returns the TYPE of the indicated class. Sets
14385 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14386 involving a nested-name-specifier was used, and FALSE otherwise.
14388 Returns error_mark_node if this is not a class-head.
14390 Returns NULL_TREE if the class-head is syntactically valid, but
14391 semantically invalid in a way that means we should skip the entire
14392 body of the class. */
14395 cp_parser_class_head (cp_parser* parser,
14396 bool* nested_name_specifier_p,
14397 tree *attributes_p,
14400 tree nested_name_specifier;
14401 enum tag_types class_key;
14402 tree id = NULL_TREE;
14403 tree type = NULL_TREE;
14405 bool template_id_p = false;
14406 bool qualified_p = false;
14407 bool invalid_nested_name_p = false;
14408 bool invalid_explicit_specialization_p = false;
14409 tree pushed_scope = NULL_TREE;
14410 unsigned num_templates;
14412 /* Assume no nested-name-specifier will be present. */
14413 *nested_name_specifier_p = false;
14414 /* Assume no template parameter lists will be used in defining the
14418 *bases = NULL_TREE;
14420 /* Look for the class-key. */
14421 class_key = cp_parser_class_key (parser);
14422 if (class_key == none_type)
14423 return error_mark_node;
14425 /* Parse the attributes. */
14426 attributes = cp_parser_attributes_opt (parser);
14428 /* If the next token is `::', that is invalid -- but sometimes
14429 people do try to write:
14433 Handle this gracefully by accepting the extra qualifier, and then
14434 issuing an error about it later if this really is a
14435 class-head. If it turns out just to be an elaborated type
14436 specifier, remain silent. */
14437 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14438 qualified_p = true;
14440 push_deferring_access_checks (dk_no_check);
14442 /* Determine the name of the class. Begin by looking for an
14443 optional nested-name-specifier. */
14444 nested_name_specifier
14445 = cp_parser_nested_name_specifier_opt (parser,
14446 /*typename_keyword_p=*/false,
14447 /*check_dependency_p=*/false,
14449 /*is_declaration=*/false);
14450 /* If there was a nested-name-specifier, then there *must* be an
14452 if (nested_name_specifier)
14454 /* Although the grammar says `identifier', it really means
14455 `class-name' or `template-name'. You are only allowed to
14456 define a class that has already been declared with this
14459 The proposed resolution for Core Issue 180 says that wherever
14460 you see `class T::X' you should treat `X' as a type-name.
14462 It is OK to define an inaccessible class; for example:
14464 class A { class B; };
14467 We do not know if we will see a class-name, or a
14468 template-name. We look for a class-name first, in case the
14469 class-name is a template-id; if we looked for the
14470 template-name first we would stop after the template-name. */
14471 cp_parser_parse_tentatively (parser);
14472 type = cp_parser_class_name (parser,
14473 /*typename_keyword_p=*/false,
14474 /*template_keyword_p=*/false,
14476 /*check_dependency_p=*/false,
14477 /*class_head_p=*/true,
14478 /*is_declaration=*/false);
14479 /* If that didn't work, ignore the nested-name-specifier. */
14480 if (!cp_parser_parse_definitely (parser))
14482 invalid_nested_name_p = true;
14483 id = cp_parser_identifier (parser);
14484 if (id == error_mark_node)
14487 /* If we could not find a corresponding TYPE, treat this
14488 declaration like an unqualified declaration. */
14489 if (type == error_mark_node)
14490 nested_name_specifier = NULL_TREE;
14491 /* Otherwise, count the number of templates used in TYPE and its
14492 containing scopes. */
14497 for (scope = TREE_TYPE (type);
14498 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14499 scope = (TYPE_P (scope)
14500 ? TYPE_CONTEXT (scope)
14501 : DECL_CONTEXT (scope)))
14503 && CLASS_TYPE_P (scope)
14504 && CLASSTYPE_TEMPLATE_INFO (scope)
14505 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14506 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14510 /* Otherwise, the identifier is optional. */
14513 /* We don't know whether what comes next is a template-id,
14514 an identifier, or nothing at all. */
14515 cp_parser_parse_tentatively (parser);
14516 /* Check for a template-id. */
14517 id = cp_parser_template_id (parser,
14518 /*template_keyword_p=*/false,
14519 /*check_dependency_p=*/true,
14520 /*is_declaration=*/true);
14521 /* If that didn't work, it could still be an identifier. */
14522 if (!cp_parser_parse_definitely (parser))
14524 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14525 id = cp_parser_identifier (parser);
14531 template_id_p = true;
14536 pop_deferring_access_checks ();
14539 cp_parser_check_for_invalid_template_id (parser, id);
14541 /* If it's not a `:' or a `{' then we can't really be looking at a
14542 class-head, since a class-head only appears as part of a
14543 class-specifier. We have to detect this situation before calling
14544 xref_tag, since that has irreversible side-effects. */
14545 if (!cp_parser_next_token_starts_class_definition_p (parser))
14547 cp_parser_error (parser, "expected %<{%> or %<:%>");
14548 return error_mark_node;
14551 /* At this point, we're going ahead with the class-specifier, even
14552 if some other problem occurs. */
14553 cp_parser_commit_to_tentative_parse (parser);
14554 /* Issue the error about the overly-qualified name now. */
14556 cp_parser_error (parser,
14557 "global qualification of class name is invalid");
14558 else if (invalid_nested_name_p)
14559 cp_parser_error (parser,
14560 "qualified name does not name a class");
14561 else if (nested_name_specifier)
14565 /* Reject typedef-names in class heads. */
14566 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14568 error ("invalid class name in declaration of %qD", type);
14573 /* Figure out in what scope the declaration is being placed. */
14574 scope = current_scope ();
14575 /* If that scope does not contain the scope in which the
14576 class was originally declared, the program is invalid. */
14577 if (scope && !is_ancestor (scope, nested_name_specifier))
14579 if (at_namespace_scope_p ())
14580 error ("declaration of %qD in namespace %qD which does not "
14581 "enclose %qD", type, scope, nested_name_specifier);
14583 error ("declaration of %qD in %qD which does not enclose %qD",
14584 type, scope, nested_name_specifier);
14590 A declarator-id shall not be qualified exception of the
14591 definition of a ... nested class outside of its class
14592 ... [or] a the definition or explicit instantiation of a
14593 class member of a namespace outside of its namespace. */
14594 if (scope == nested_name_specifier)
14596 pedwarn ("extra qualification ignored");
14597 nested_name_specifier = NULL_TREE;
14601 /* An explicit-specialization must be preceded by "template <>". If
14602 it is not, try to recover gracefully. */
14603 if (at_namespace_scope_p ()
14604 && parser->num_template_parameter_lists == 0
14607 error ("an explicit specialization must be preceded by %<template <>%>");
14608 invalid_explicit_specialization_p = true;
14609 /* Take the same action that would have been taken by
14610 cp_parser_explicit_specialization. */
14611 ++parser->num_template_parameter_lists;
14612 begin_specialization ();
14614 /* There must be no "return" statements between this point and the
14615 end of this function; set "type "to the correct return value and
14616 use "goto done;" to return. */
14617 /* Make sure that the right number of template parameters were
14619 if (!cp_parser_check_template_parameters (parser, num_templates))
14621 /* If something went wrong, there is no point in even trying to
14622 process the class-definition. */
14627 /* Look up the type. */
14630 if (TREE_CODE (id) == TEMPLATE_ID_EXPR
14631 && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
14632 || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
14634 error ("function template %qD redeclared as a class template", id);
14635 type = error_mark_node;
14639 type = TREE_TYPE (id);
14640 type = maybe_process_partial_specialization (type);
14642 if (nested_name_specifier)
14643 pushed_scope = push_scope (nested_name_specifier);
14645 else if (nested_name_specifier)
14651 template <typename T> struct S { struct T };
14652 template <typename T> struct S<T>::T { };
14654 we will get a TYPENAME_TYPE when processing the definition of
14655 `S::T'. We need to resolve it to the actual type before we
14656 try to define it. */
14657 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14659 class_type = resolve_typename_type (TREE_TYPE (type),
14660 /*only_current_p=*/false);
14661 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14662 type = TYPE_NAME (class_type);
14665 cp_parser_error (parser, "could not resolve typename type");
14666 type = error_mark_node;
14670 maybe_process_partial_specialization (TREE_TYPE (type));
14671 class_type = current_class_type;
14672 /* Enter the scope indicated by the nested-name-specifier. */
14673 pushed_scope = push_scope (nested_name_specifier);
14674 /* Get the canonical version of this type. */
14675 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14676 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14677 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14679 type = push_template_decl (type);
14680 if (type == error_mark_node)
14687 type = TREE_TYPE (type);
14688 *nested_name_specifier_p = true;
14690 else /* The name is not a nested name. */
14692 /* If the class was unnamed, create a dummy name. */
14694 id = make_anon_name ();
14695 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14696 parser->num_template_parameter_lists);
14699 /* Indicate whether this class was declared as a `class' or as a
14701 if (TREE_CODE (type) == RECORD_TYPE)
14702 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14703 cp_parser_check_class_key (class_key, type);
14705 /* If this type was already complete, and we see another definition,
14706 that's an error. */
14707 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14709 error ("redefinition of %q#T", type);
14710 error ("previous definition of %q+#T", type);
14714 else if (type == error_mark_node)
14717 /* We will have entered the scope containing the class; the names of
14718 base classes should be looked up in that context. For example:
14720 struct A { struct B {}; struct C; };
14721 struct A::C : B {};
14725 /* Get the list of base-classes, if there is one. */
14726 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14727 *bases = cp_parser_base_clause (parser);
14730 /* Leave the scope given by the nested-name-specifier. We will
14731 enter the class scope itself while processing the members. */
14733 pop_scope (pushed_scope);
14735 if (invalid_explicit_specialization_p)
14737 end_specialization ();
14738 --parser->num_template_parameter_lists;
14740 *attributes_p = attributes;
14744 /* Parse a class-key.
14751 Returns the kind of class-key specified, or none_type to indicate
14754 static enum tag_types
14755 cp_parser_class_key (cp_parser* parser)
14758 enum tag_types tag_type;
14760 /* Look for the class-key. */
14761 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14765 /* Check to see if the TOKEN is a class-key. */
14766 tag_type = cp_parser_token_is_class_key (token);
14768 cp_parser_error (parser, "expected class-key");
14772 /* Parse an (optional) member-specification.
14774 member-specification:
14775 member-declaration member-specification [opt]
14776 access-specifier : member-specification [opt] */
14779 cp_parser_member_specification_opt (cp_parser* parser)
14786 /* Peek at the next token. */
14787 token = cp_lexer_peek_token (parser->lexer);
14788 /* If it's a `}', or EOF then we've seen all the members. */
14789 if (token->type == CPP_CLOSE_BRACE
14790 || token->type == CPP_EOF
14791 || token->type == CPP_PRAGMA_EOL)
14794 /* See if this token is a keyword. */
14795 keyword = token->keyword;
14799 case RID_PROTECTED:
14801 /* Consume the access-specifier. */
14802 cp_lexer_consume_token (parser->lexer);
14803 /* Remember which access-specifier is active. */
14804 current_access_specifier = token->u.value;
14805 /* Look for the `:'. */
14806 cp_parser_require (parser, CPP_COLON, "`:'");
14810 /* Accept #pragmas at class scope. */
14811 if (token->type == CPP_PRAGMA)
14813 cp_parser_pragma (parser, pragma_external);
14817 /* Otherwise, the next construction must be a
14818 member-declaration. */
14819 cp_parser_member_declaration (parser);
14824 /* Parse a member-declaration.
14826 member-declaration:
14827 decl-specifier-seq [opt] member-declarator-list [opt] ;
14828 function-definition ; [opt]
14829 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14831 template-declaration
14833 member-declarator-list:
14835 member-declarator-list , member-declarator
14838 declarator pure-specifier [opt]
14839 declarator constant-initializer [opt]
14840 identifier [opt] : constant-expression
14844 member-declaration:
14845 __extension__ member-declaration
14848 declarator attributes [opt] pure-specifier [opt]
14849 declarator attributes [opt] constant-initializer [opt]
14850 identifier [opt] attributes [opt] : constant-expression
14854 member-declaration:
14855 static_assert-declaration */
14858 cp_parser_member_declaration (cp_parser* parser)
14860 cp_decl_specifier_seq decl_specifiers;
14861 tree prefix_attributes;
14863 int declares_class_or_enum;
14866 int saved_pedantic;
14868 /* Check for the `__extension__' keyword. */
14869 if (cp_parser_extension_opt (parser, &saved_pedantic))
14872 cp_parser_member_declaration (parser);
14873 /* Restore the old value of the PEDANTIC flag. */
14874 pedantic = saved_pedantic;
14879 /* Check for a template-declaration. */
14880 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14882 /* An explicit specialization here is an error condition, and we
14883 expect the specialization handler to detect and report this. */
14884 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14885 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14886 cp_parser_explicit_specialization (parser);
14888 cp_parser_template_declaration (parser, /*member_p=*/true);
14893 /* Check for a using-declaration. */
14894 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14896 /* Parse the using-declaration. */
14897 cp_parser_using_declaration (parser,
14898 /*access_declaration_p=*/false);
14902 /* Check for @defs. */
14903 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14906 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14907 ivar = ivar_chains;
14911 ivar = TREE_CHAIN (member);
14912 TREE_CHAIN (member) = NULL_TREE;
14913 finish_member_declaration (member);
14918 /* If the next token is `static_assert' we have a static assertion. */
14919 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14921 cp_parser_static_assert (parser, /*member_p=*/true);
14925 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14928 /* Parse the decl-specifier-seq. */
14929 cp_parser_decl_specifier_seq (parser,
14930 CP_PARSER_FLAGS_OPTIONAL,
14932 &declares_class_or_enum);
14933 prefix_attributes = decl_specifiers.attributes;
14934 decl_specifiers.attributes = NULL_TREE;
14935 /* Check for an invalid type-name. */
14936 if (!decl_specifiers.type
14937 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14939 /* If there is no declarator, then the decl-specifier-seq should
14941 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14943 /* If there was no decl-specifier-seq, and the next token is a
14944 `;', then we have something like:
14950 Each member-declaration shall declare at least one member
14951 name of the class. */
14952 if (!decl_specifiers.any_specifiers_p)
14954 cp_token *token = cp_lexer_peek_token (parser->lexer);
14955 if (pedantic && !token->in_system_header)
14956 pedwarn ("%Hextra %<;%>", &token->location);
14962 /* See if this declaration is a friend. */
14963 friend_p = cp_parser_friend_p (&decl_specifiers);
14964 /* If there were decl-specifiers, check to see if there was
14965 a class-declaration. */
14966 type = check_tag_decl (&decl_specifiers);
14967 /* Nested classes have already been added to the class, but
14968 a `friend' needs to be explicitly registered. */
14971 /* If the `friend' keyword was present, the friend must
14972 be introduced with a class-key. */
14973 if (!declares_class_or_enum)
14974 error ("a class-key must be used when declaring a friend");
14977 template <typename T> struct A {
14978 friend struct A<T>::B;
14981 A<T>::B will be represented by a TYPENAME_TYPE, and
14982 therefore not recognized by check_tag_decl. */
14984 && decl_specifiers.type
14985 && TYPE_P (decl_specifiers.type))
14986 type = decl_specifiers.type;
14987 if (!type || !TYPE_P (type))
14988 error ("friend declaration does not name a class or "
14991 make_friend_class (current_class_type, type,
14992 /*complain=*/true);
14994 /* If there is no TYPE, an error message will already have
14996 else if (!type || type == error_mark_node)
14998 /* An anonymous aggregate has to be handled specially; such
14999 a declaration really declares a data member (with a
15000 particular type), as opposed to a nested class. */
15001 else if (ANON_AGGR_TYPE_P (type))
15003 /* Remove constructors and such from TYPE, now that we
15004 know it is an anonymous aggregate. */
15005 fixup_anonymous_aggr (type);
15006 /* And make the corresponding data member. */
15007 decl = build_decl (FIELD_DECL, NULL_TREE, type);
15008 /* Add it to the class. */
15009 finish_member_declaration (decl);
15012 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
15017 /* See if these declarations will be friends. */
15018 friend_p = cp_parser_friend_p (&decl_specifiers);
15020 /* Keep going until we hit the `;' at the end of the
15022 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
15024 tree attributes = NULL_TREE;
15025 tree first_attribute;
15027 /* Peek at the next token. */
15028 token = cp_lexer_peek_token (parser->lexer);
15030 /* Check for a bitfield declaration. */
15031 if (token->type == CPP_COLON
15032 || (token->type == CPP_NAME
15033 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
15039 /* Get the name of the bitfield. Note that we cannot just
15040 check TOKEN here because it may have been invalidated by
15041 the call to cp_lexer_peek_nth_token above. */
15042 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
15043 identifier = cp_parser_identifier (parser);
15045 identifier = NULL_TREE;
15047 /* Consume the `:' token. */
15048 cp_lexer_consume_token (parser->lexer);
15049 /* Get the width of the bitfield. */
15051 = cp_parser_constant_expression (parser,
15052 /*allow_non_constant=*/false,
15055 /* Look for attributes that apply to the bitfield. */
15056 attributes = cp_parser_attributes_opt (parser);
15057 /* Remember which attributes are prefix attributes and
15059 first_attribute = attributes;
15060 /* Combine the attributes. */
15061 attributes = chainon (prefix_attributes, attributes);
15063 /* Create the bitfield declaration. */
15064 decl = grokbitfield (identifier
15065 ? make_id_declarator (NULL_TREE,
15071 /* Apply the attributes. */
15072 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
15076 cp_declarator *declarator;
15078 tree asm_specification;
15079 int ctor_dtor_or_conv_p;
15081 /* Parse the declarator. */
15083 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15084 &ctor_dtor_or_conv_p,
15085 /*parenthesized_p=*/NULL,
15086 /*member_p=*/true);
15088 /* If something went wrong parsing the declarator, make sure
15089 that we at least consume some tokens. */
15090 if (declarator == cp_error_declarator)
15092 /* Skip to the end of the statement. */
15093 cp_parser_skip_to_end_of_statement (parser);
15094 /* If the next token is not a semicolon, that is
15095 probably because we just skipped over the body of
15096 a function. So, we consume a semicolon if
15097 present, but do not issue an error message if it
15099 if (cp_lexer_next_token_is (parser->lexer,
15101 cp_lexer_consume_token (parser->lexer);
15105 if (declares_class_or_enum & 2)
15106 cp_parser_check_for_definition_in_return_type
15107 (declarator, decl_specifiers.type);
15109 /* Look for an asm-specification. */
15110 asm_specification = cp_parser_asm_specification_opt (parser);
15111 /* Look for attributes that apply to the declaration. */
15112 attributes = cp_parser_attributes_opt (parser);
15113 /* Remember which attributes are prefix attributes and
15115 first_attribute = attributes;
15116 /* Combine the attributes. */
15117 attributes = chainon (prefix_attributes, attributes);
15119 /* If it's an `=', then we have a constant-initializer or a
15120 pure-specifier. It is not correct to parse the
15121 initializer before registering the member declaration
15122 since the member declaration should be in scope while
15123 its initializer is processed. However, the rest of the
15124 front end does not yet provide an interface that allows
15125 us to handle this correctly. */
15126 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15130 A pure-specifier shall be used only in the declaration of
15131 a virtual function.
15133 A member-declarator can contain a constant-initializer
15134 only if it declares a static member of integral or
15137 Therefore, if the DECLARATOR is for a function, we look
15138 for a pure-specifier; otherwise, we look for a
15139 constant-initializer. When we call `grokfield', it will
15140 perform more stringent semantics checks. */
15141 if (function_declarator_p (declarator))
15142 initializer = cp_parser_pure_specifier (parser);
15144 /* Parse the initializer. */
15145 initializer = cp_parser_constant_initializer (parser);
15147 /* Otherwise, there is no initializer. */
15149 initializer = NULL_TREE;
15151 /* See if we are probably looking at a function
15152 definition. We are certainly not looking at a
15153 member-declarator. Calling `grokfield' has
15154 side-effects, so we must not do it unless we are sure
15155 that we are looking at a member-declarator. */
15156 if (cp_parser_token_starts_function_definition_p
15157 (cp_lexer_peek_token (parser->lexer)))
15159 /* The grammar does not allow a pure-specifier to be
15160 used when a member function is defined. (It is
15161 possible that this fact is an oversight in the
15162 standard, since a pure function may be defined
15163 outside of the class-specifier. */
15165 error ("pure-specifier on function-definition");
15166 decl = cp_parser_save_member_function_body (parser,
15170 /* If the member was not a friend, declare it here. */
15172 finish_member_declaration (decl);
15173 /* Peek at the next token. */
15174 token = cp_lexer_peek_token (parser->lexer);
15175 /* If the next token is a semicolon, consume it. */
15176 if (token->type == CPP_SEMICOLON)
15177 cp_lexer_consume_token (parser->lexer);
15181 /* Create the declaration. */
15182 decl = grokfield (declarator, &decl_specifiers,
15183 initializer, /*init_const_expr_p=*/true,
15188 /* Reset PREFIX_ATTRIBUTES. */
15189 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15190 attributes = TREE_CHAIN (attributes);
15192 TREE_CHAIN (attributes) = NULL_TREE;
15194 /* If there is any qualification still in effect, clear it
15195 now; we will be starting fresh with the next declarator. */
15196 parser->scope = NULL_TREE;
15197 parser->qualifying_scope = NULL_TREE;
15198 parser->object_scope = NULL_TREE;
15199 /* If it's a `,', then there are more declarators. */
15200 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15201 cp_lexer_consume_token (parser->lexer);
15202 /* If the next token isn't a `;', then we have a parse error. */
15203 else if (cp_lexer_next_token_is_not (parser->lexer,
15206 cp_parser_error (parser, "expected %<;%>");
15207 /* Skip tokens until we find a `;'. */
15208 cp_parser_skip_to_end_of_statement (parser);
15215 /* Add DECL to the list of members. */
15217 finish_member_declaration (decl);
15219 if (TREE_CODE (decl) == FUNCTION_DECL)
15220 cp_parser_save_default_args (parser, decl);
15225 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15228 /* Parse a pure-specifier.
15233 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15234 Otherwise, ERROR_MARK_NODE is returned. */
15237 cp_parser_pure_specifier (cp_parser* parser)
15241 /* Look for the `=' token. */
15242 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15243 return error_mark_node;
15244 /* Look for the `0' token. */
15245 token = cp_lexer_consume_token (parser->lexer);
15246 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15247 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15249 cp_parser_error (parser,
15250 "invalid pure specifier (only `= 0' is allowed)");
15251 cp_parser_skip_to_end_of_statement (parser);
15252 return error_mark_node;
15254 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15256 error ("templates may not be %<virtual%>");
15257 return error_mark_node;
15260 return integer_zero_node;
15263 /* Parse a constant-initializer.
15265 constant-initializer:
15266 = constant-expression
15268 Returns a representation of the constant-expression. */
15271 cp_parser_constant_initializer (cp_parser* parser)
15273 /* Look for the `=' token. */
15274 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15275 return error_mark_node;
15277 /* It is invalid to write:
15279 struct S { static const int i = { 7 }; };
15282 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15284 cp_parser_error (parser,
15285 "a brace-enclosed initializer is not allowed here");
15286 /* Consume the opening brace. */
15287 cp_lexer_consume_token (parser->lexer);
15288 /* Skip the initializer. */
15289 cp_parser_skip_to_closing_brace (parser);
15290 /* Look for the trailing `}'. */
15291 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15293 return error_mark_node;
15296 return cp_parser_constant_expression (parser,
15297 /*allow_non_constant=*/false,
15301 /* Derived classes [gram.class.derived] */
15303 /* Parse a base-clause.
15306 : base-specifier-list
15308 base-specifier-list:
15309 base-specifier ... [opt]
15310 base-specifier-list , base-specifier ... [opt]
15312 Returns a TREE_LIST representing the base-classes, in the order in
15313 which they were declared. The representation of each node is as
15314 described by cp_parser_base_specifier.
15316 In the case that no bases are specified, this function will return
15317 NULL_TREE, not ERROR_MARK_NODE. */
15320 cp_parser_base_clause (cp_parser* parser)
15322 tree bases = NULL_TREE;
15324 /* Look for the `:' that begins the list. */
15325 cp_parser_require (parser, CPP_COLON, "`:'");
15327 /* Scan the base-specifier-list. */
15332 bool pack_expansion_p = false;
15334 /* Look for the base-specifier. */
15335 base = cp_parser_base_specifier (parser);
15336 /* Look for the (optional) ellipsis. */
15337 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15339 /* Consume the `...'. */
15340 cp_lexer_consume_token (parser->lexer);
15342 pack_expansion_p = true;
15345 /* Add BASE to the front of the list. */
15346 if (base != error_mark_node)
15348 if (pack_expansion_p)
15349 /* Make this a pack expansion type. */
15350 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15353 if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
15355 TREE_CHAIN (base) = bases;
15359 /* Peek at the next token. */
15360 token = cp_lexer_peek_token (parser->lexer);
15361 /* If it's not a comma, then the list is complete. */
15362 if (token->type != CPP_COMMA)
15364 /* Consume the `,'. */
15365 cp_lexer_consume_token (parser->lexer);
15368 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15369 base class had a qualified name. However, the next name that
15370 appears is certainly not qualified. */
15371 parser->scope = NULL_TREE;
15372 parser->qualifying_scope = NULL_TREE;
15373 parser->object_scope = NULL_TREE;
15375 return nreverse (bases);
15378 /* Parse a base-specifier.
15381 :: [opt] nested-name-specifier [opt] class-name
15382 virtual access-specifier [opt] :: [opt] nested-name-specifier
15384 access-specifier virtual [opt] :: [opt] nested-name-specifier
15387 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15388 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15389 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15390 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15393 cp_parser_base_specifier (cp_parser* parser)
15397 bool virtual_p = false;
15398 bool duplicate_virtual_error_issued_p = false;
15399 bool duplicate_access_error_issued_p = false;
15400 bool class_scope_p, template_p;
15401 tree access = access_default_node;
15404 /* Process the optional `virtual' and `access-specifier'. */
15407 /* Peek at the next token. */
15408 token = cp_lexer_peek_token (parser->lexer);
15409 /* Process `virtual'. */
15410 switch (token->keyword)
15413 /* If `virtual' appears more than once, issue an error. */
15414 if (virtual_p && !duplicate_virtual_error_issued_p)
15416 cp_parser_error (parser,
15417 "%<virtual%> specified more than once in base-specified");
15418 duplicate_virtual_error_issued_p = true;
15423 /* Consume the `virtual' token. */
15424 cp_lexer_consume_token (parser->lexer);
15429 case RID_PROTECTED:
15431 /* If more than one access specifier appears, issue an
15433 if (access != access_default_node
15434 && !duplicate_access_error_issued_p)
15436 cp_parser_error (parser,
15437 "more than one access specifier in base-specified");
15438 duplicate_access_error_issued_p = true;
15441 access = ridpointers[(int) token->keyword];
15443 /* Consume the access-specifier. */
15444 cp_lexer_consume_token (parser->lexer);
15453 /* It is not uncommon to see programs mechanically, erroneously, use
15454 the 'typename' keyword to denote (dependent) qualified types
15455 as base classes. */
15456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15458 if (!processing_template_decl)
15459 error ("keyword %<typename%> not allowed outside of templates");
15461 error ("keyword %<typename%> not allowed in this context "
15462 "(the base class is implicitly a type)");
15463 cp_lexer_consume_token (parser->lexer);
15466 /* Look for the optional `::' operator. */
15467 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15468 /* Look for the nested-name-specifier. The simplest way to
15473 The keyword `typename' is not permitted in a base-specifier or
15474 mem-initializer; in these contexts a qualified name that
15475 depends on a template-parameter is implicitly assumed to be a
15478 is to pretend that we have seen the `typename' keyword at this
15480 cp_parser_nested_name_specifier_opt (parser,
15481 /*typename_keyword_p=*/true,
15482 /*check_dependency_p=*/true,
15484 /*is_declaration=*/true);
15485 /* If the base class is given by a qualified name, assume that names
15486 we see are type names or templates, as appropriate. */
15487 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15488 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15490 /* Finally, look for the class-name. */
15491 type = cp_parser_class_name (parser,
15495 /*check_dependency_p=*/true,
15496 /*class_head_p=*/false,
15497 /*is_declaration=*/true);
15499 if (type == error_mark_node)
15500 return error_mark_node;
15502 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15505 /* Exception handling [gram.exception] */
15507 /* Parse an (optional) exception-specification.
15509 exception-specification:
15510 throw ( type-id-list [opt] )
15512 Returns a TREE_LIST representing the exception-specification. The
15513 TREE_VALUE of each node is a type. */
15516 cp_parser_exception_specification_opt (cp_parser* parser)
15521 /* Peek at the next token. */
15522 token = cp_lexer_peek_token (parser->lexer);
15523 /* If it's not `throw', then there's no exception-specification. */
15524 if (!cp_parser_is_keyword (token, RID_THROW))
15527 /* Consume the `throw'. */
15528 cp_lexer_consume_token (parser->lexer);
15530 /* Look for the `('. */
15531 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15533 /* Peek at the next token. */
15534 token = cp_lexer_peek_token (parser->lexer);
15535 /* If it's not a `)', then there is a type-id-list. */
15536 if (token->type != CPP_CLOSE_PAREN)
15538 const char *saved_message;
15540 /* Types may not be defined in an exception-specification. */
15541 saved_message = parser->type_definition_forbidden_message;
15542 parser->type_definition_forbidden_message
15543 = "types may not be defined in an exception-specification";
15544 /* Parse the type-id-list. */
15545 type_id_list = cp_parser_type_id_list (parser);
15546 /* Restore the saved message. */
15547 parser->type_definition_forbidden_message = saved_message;
15550 type_id_list = empty_except_spec;
15552 /* Look for the `)'. */
15553 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15555 return type_id_list;
15558 /* Parse an (optional) type-id-list.
15562 type-id-list , type-id ... [opt]
15564 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15565 in the order that the types were presented. */
15568 cp_parser_type_id_list (cp_parser* parser)
15570 tree types = NULL_TREE;
15577 /* Get the next type-id. */
15578 type = cp_parser_type_id (parser);
15579 /* Parse the optional ellipsis. */
15580 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15582 /* Consume the `...'. */
15583 cp_lexer_consume_token (parser->lexer);
15585 /* Turn the type into a pack expansion expression. */
15586 type = make_pack_expansion (type);
15588 /* Add it to the list. */
15589 types = add_exception_specifier (types, type, /*complain=*/1);
15590 /* Peek at the next token. */
15591 token = cp_lexer_peek_token (parser->lexer);
15592 /* If it is not a `,', we are done. */
15593 if (token->type != CPP_COMMA)
15595 /* Consume the `,'. */
15596 cp_lexer_consume_token (parser->lexer);
15599 return nreverse (types);
15602 /* Parse a try-block.
15605 try compound-statement handler-seq */
15608 cp_parser_try_block (cp_parser* parser)
15612 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15613 try_block = begin_try_block ();
15614 cp_parser_compound_statement (parser, NULL, true);
15615 finish_try_block (try_block);
15616 cp_parser_handler_seq (parser);
15617 finish_handler_sequence (try_block);
15622 /* Parse a function-try-block.
15624 function-try-block:
15625 try ctor-initializer [opt] function-body handler-seq */
15628 cp_parser_function_try_block (cp_parser* parser)
15630 tree compound_stmt;
15632 bool ctor_initializer_p;
15634 /* Look for the `try' keyword. */
15635 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15637 /* Let the rest of the front end know where we are. */
15638 try_block = begin_function_try_block (&compound_stmt);
15639 /* Parse the function-body. */
15641 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15642 /* We're done with the `try' part. */
15643 finish_function_try_block (try_block);
15644 /* Parse the handlers. */
15645 cp_parser_handler_seq (parser);
15646 /* We're done with the handlers. */
15647 finish_function_handler_sequence (try_block, compound_stmt);
15649 return ctor_initializer_p;
15652 /* Parse a handler-seq.
15655 handler handler-seq [opt] */
15658 cp_parser_handler_seq (cp_parser* parser)
15664 /* Parse the handler. */
15665 cp_parser_handler (parser);
15666 /* Peek at the next token. */
15667 token = cp_lexer_peek_token (parser->lexer);
15668 /* If it's not `catch' then there are no more handlers. */
15669 if (!cp_parser_is_keyword (token, RID_CATCH))
15674 /* Parse a handler.
15677 catch ( exception-declaration ) compound-statement */
15680 cp_parser_handler (cp_parser* parser)
15685 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15686 handler = begin_handler ();
15687 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15688 declaration = cp_parser_exception_declaration (parser);
15689 finish_handler_parms (declaration, handler);
15690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15691 cp_parser_compound_statement (parser, NULL, false);
15692 finish_handler (handler);
15695 /* Parse an exception-declaration.
15697 exception-declaration:
15698 type-specifier-seq declarator
15699 type-specifier-seq abstract-declarator
15703 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15704 ellipsis variant is used. */
15707 cp_parser_exception_declaration (cp_parser* parser)
15709 cp_decl_specifier_seq type_specifiers;
15710 cp_declarator *declarator;
15711 const char *saved_message;
15713 /* If it's an ellipsis, it's easy to handle. */
15714 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15716 /* Consume the `...' token. */
15717 cp_lexer_consume_token (parser->lexer);
15721 /* Types may not be defined in exception-declarations. */
15722 saved_message = parser->type_definition_forbidden_message;
15723 parser->type_definition_forbidden_message
15724 = "types may not be defined in exception-declarations";
15726 /* Parse the type-specifier-seq. */
15727 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15729 /* If it's a `)', then there is no declarator. */
15730 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15733 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15734 /*ctor_dtor_or_conv_p=*/NULL,
15735 /*parenthesized_p=*/NULL,
15736 /*member_p=*/false);
15738 /* Restore the saved message. */
15739 parser->type_definition_forbidden_message = saved_message;
15741 if (!type_specifiers.any_specifiers_p)
15742 return error_mark_node;
15744 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15747 /* Parse a throw-expression.
15750 throw assignment-expression [opt]
15752 Returns a THROW_EXPR representing the throw-expression. */
15755 cp_parser_throw_expression (cp_parser* parser)
15760 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15761 token = cp_lexer_peek_token (parser->lexer);
15762 /* Figure out whether or not there is an assignment-expression
15763 following the "throw" keyword. */
15764 if (token->type == CPP_COMMA
15765 || token->type == CPP_SEMICOLON
15766 || token->type == CPP_CLOSE_PAREN
15767 || token->type == CPP_CLOSE_SQUARE
15768 || token->type == CPP_CLOSE_BRACE
15769 || token->type == CPP_COLON)
15770 expression = NULL_TREE;
15772 expression = cp_parser_assignment_expression (parser,
15775 return build_throw (expression);
15778 /* GNU Extensions */
15780 /* Parse an (optional) asm-specification.
15783 asm ( string-literal )
15785 If the asm-specification is present, returns a STRING_CST
15786 corresponding to the string-literal. Otherwise, returns
15790 cp_parser_asm_specification_opt (cp_parser* parser)
15793 tree asm_specification;
15795 /* Peek at the next token. */
15796 token = cp_lexer_peek_token (parser->lexer);
15797 /* If the next token isn't the `asm' keyword, then there's no
15798 asm-specification. */
15799 if (!cp_parser_is_keyword (token, RID_ASM))
15802 /* Consume the `asm' token. */
15803 cp_lexer_consume_token (parser->lexer);
15804 /* Look for the `('. */
15805 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15807 /* Look for the string-literal. */
15808 asm_specification = cp_parser_string_literal (parser, false, false);
15810 /* Look for the `)'. */
15811 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15813 return asm_specification;
15816 /* Parse an asm-operand-list.
15820 asm-operand-list , asm-operand
15823 string-literal ( expression )
15824 [ string-literal ] string-literal ( expression )
15826 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15827 each node is the expression. The TREE_PURPOSE is itself a
15828 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15829 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15830 is a STRING_CST for the string literal before the parenthesis. Returns
15831 ERROR_MARK_NODE if any of the operands are invalid. */
15834 cp_parser_asm_operand_list (cp_parser* parser)
15836 tree asm_operands = NULL_TREE;
15837 bool invalid_operands = false;
15841 tree string_literal;
15845 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15847 /* Consume the `[' token. */
15848 cp_lexer_consume_token (parser->lexer);
15849 /* Read the operand name. */
15850 name = cp_parser_identifier (parser);
15851 if (name != error_mark_node)
15852 name = build_string (IDENTIFIER_LENGTH (name),
15853 IDENTIFIER_POINTER (name));
15854 /* Look for the closing `]'. */
15855 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15859 /* Look for the string-literal. */
15860 string_literal = cp_parser_string_literal (parser, false, false);
15862 /* Look for the `('. */
15863 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15864 /* Parse the expression. */
15865 expression = cp_parser_expression (parser, /*cast_p=*/false);
15866 /* Look for the `)'. */
15867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15869 if (name == error_mark_node
15870 || string_literal == error_mark_node
15871 || expression == error_mark_node)
15872 invalid_operands = true;
15874 /* Add this operand to the list. */
15875 asm_operands = tree_cons (build_tree_list (name, string_literal),
15878 /* If the next token is not a `,', there are no more
15880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15882 /* Consume the `,'. */
15883 cp_lexer_consume_token (parser->lexer);
15886 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15889 /* Parse an asm-clobber-list.
15893 asm-clobber-list , string-literal
15895 Returns a TREE_LIST, indicating the clobbers in the order that they
15896 appeared. The TREE_VALUE of each node is a STRING_CST. */
15899 cp_parser_asm_clobber_list (cp_parser* parser)
15901 tree clobbers = NULL_TREE;
15905 tree string_literal;
15907 /* Look for the string literal. */
15908 string_literal = cp_parser_string_literal (parser, false, false);
15909 /* Add it to the list. */
15910 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15911 /* If the next token is not a `,', then the list is
15913 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15915 /* Consume the `,' token. */
15916 cp_lexer_consume_token (parser->lexer);
15922 /* Parse an (optional) series of attributes.
15925 attributes attribute
15928 __attribute__ (( attribute-list [opt] ))
15930 The return value is as for cp_parser_attribute_list. */
15933 cp_parser_attributes_opt (cp_parser* parser)
15935 tree attributes = NULL_TREE;
15940 tree attribute_list;
15942 /* Peek at the next token. */
15943 token = cp_lexer_peek_token (parser->lexer);
15944 /* If it's not `__attribute__', then we're done. */
15945 if (token->keyword != RID_ATTRIBUTE)
15948 /* Consume the `__attribute__' keyword. */
15949 cp_lexer_consume_token (parser->lexer);
15950 /* Look for the two `(' tokens. */
15951 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15952 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15954 /* Peek at the next token. */
15955 token = cp_lexer_peek_token (parser->lexer);
15956 if (token->type != CPP_CLOSE_PAREN)
15957 /* Parse the attribute-list. */
15958 attribute_list = cp_parser_attribute_list (parser);
15960 /* If the next token is a `)', then there is no attribute
15962 attribute_list = NULL;
15964 /* Look for the two `)' tokens. */
15965 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15966 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15968 /* Add these new attributes to the list. */
15969 attributes = chainon (attributes, attribute_list);
15975 /* Parse an attribute-list.
15979 attribute-list , attribute
15983 identifier ( identifier )
15984 identifier ( identifier , expression-list )
15985 identifier ( expression-list )
15987 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15988 to an attribute. The TREE_PURPOSE of each node is the identifier
15989 indicating which attribute is in use. The TREE_VALUE represents
15990 the arguments, if any. */
15993 cp_parser_attribute_list (cp_parser* parser)
15995 tree attribute_list = NULL_TREE;
15996 bool save_translate_strings_p = parser->translate_strings_p;
15998 parser->translate_strings_p = false;
16005 /* Look for the identifier. We also allow keywords here; for
16006 example `__attribute__ ((const))' is legal. */
16007 token = cp_lexer_peek_token (parser->lexer);
16008 if (token->type == CPP_NAME
16009 || token->type == CPP_KEYWORD)
16011 tree arguments = NULL_TREE;
16013 /* Consume the token. */
16014 token = cp_lexer_consume_token (parser->lexer);
16016 /* Save away the identifier that indicates which attribute
16018 identifier = token->u.value;
16019 attribute = build_tree_list (identifier, NULL_TREE);
16021 /* Peek at the next token. */
16022 token = cp_lexer_peek_token (parser->lexer);
16023 /* If it's an `(', then parse the attribute arguments. */
16024 if (token->type == CPP_OPEN_PAREN)
16026 arguments = cp_parser_parenthesized_expression_list
16027 (parser, true, /*cast_p=*/false,
16028 /*allow_expansion_p=*/false,
16029 /*non_constant_p=*/NULL);
16030 /* Save the arguments away. */
16031 TREE_VALUE (attribute) = arguments;
16034 if (arguments != error_mark_node)
16036 /* Add this attribute to the list. */
16037 TREE_CHAIN (attribute) = attribute_list;
16038 attribute_list = attribute;
16041 token = cp_lexer_peek_token (parser->lexer);
16043 /* Now, look for more attributes. If the next token isn't a
16044 `,', we're done. */
16045 if (token->type != CPP_COMMA)
16048 /* Consume the comma and keep going. */
16049 cp_lexer_consume_token (parser->lexer);
16051 parser->translate_strings_p = save_translate_strings_p;
16053 /* We built up the list in reverse order. */
16054 return nreverse (attribute_list);
16057 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
16058 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
16059 current value of the PEDANTIC flag, regardless of whether or not
16060 the `__extension__' keyword is present. The caller is responsible
16061 for restoring the value of the PEDANTIC flag. */
16064 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
16066 /* Save the old value of the PEDANTIC flag. */
16067 *saved_pedantic = pedantic;
16069 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
16071 /* Consume the `__extension__' token. */
16072 cp_lexer_consume_token (parser->lexer);
16073 /* We're not being pedantic while the `__extension__' keyword is
16083 /* Parse a label declaration.
16086 __label__ label-declarator-seq ;
16088 label-declarator-seq:
16089 identifier , label-declarator-seq
16093 cp_parser_label_declaration (cp_parser* parser)
16095 /* Look for the `__label__' keyword. */
16096 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
16102 /* Look for an identifier. */
16103 identifier = cp_parser_identifier (parser);
16104 /* If we failed, stop. */
16105 if (identifier == error_mark_node)
16107 /* Declare it as a label. */
16108 finish_label_decl (identifier);
16109 /* If the next token is a `;', stop. */
16110 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16112 /* Look for the `,' separating the label declarations. */
16113 cp_parser_require (parser, CPP_COMMA, "`,'");
16116 /* Look for the final `;'. */
16117 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
16120 /* Support Functions */
16122 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
16123 NAME should have one of the representations used for an
16124 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
16125 is returned. If PARSER->SCOPE is a dependent type, then a
16126 SCOPE_REF is returned.
16128 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
16129 returned; the name was already resolved when the TEMPLATE_ID_EXPR
16130 was formed. Abstractly, such entities should not be passed to this
16131 function, because they do not need to be looked up, but it is
16132 simpler to check for this special case here, rather than at the
16135 In cases not explicitly covered above, this function returns a
16136 DECL, OVERLOAD, or baselink representing the result of the lookup.
16137 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16140 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16141 (e.g., "struct") that was used. In that case bindings that do not
16142 refer to types are ignored.
16144 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16147 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16150 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16153 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16154 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16155 NULL_TREE otherwise. */
16158 cp_parser_lookup_name (cp_parser *parser, tree name,
16159 enum tag_types tag_type,
16162 bool check_dependency,
16163 tree *ambiguous_decls)
16167 tree object_type = parser->context->object_type;
16169 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16170 flags |= LOOKUP_COMPLAIN;
16172 /* Assume that the lookup will be unambiguous. */
16173 if (ambiguous_decls)
16174 *ambiguous_decls = NULL_TREE;
16176 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16177 no longer valid. Note that if we are parsing tentatively, and
16178 the parse fails, OBJECT_TYPE will be automatically restored. */
16179 parser->context->object_type = NULL_TREE;
16181 if (name == error_mark_node)
16182 return error_mark_node;
16184 /* A template-id has already been resolved; there is no lookup to
16186 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16188 if (BASELINK_P (name))
16190 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16191 == TEMPLATE_ID_EXPR);
16195 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16196 it should already have been checked to make sure that the name
16197 used matches the type being destroyed. */
16198 if (TREE_CODE (name) == BIT_NOT_EXPR)
16202 /* Figure out to which type this destructor applies. */
16204 type = parser->scope;
16205 else if (object_type)
16206 type = object_type;
16208 type = current_class_type;
16209 /* If that's not a class type, there is no destructor. */
16210 if (!type || !CLASS_TYPE_P (type))
16211 return error_mark_node;
16212 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16213 lazily_declare_fn (sfk_destructor, type);
16214 if (!CLASSTYPE_DESTRUCTORS (type))
16215 return error_mark_node;
16216 /* If it was a class type, return the destructor. */
16217 return CLASSTYPE_DESTRUCTORS (type);
16220 /* By this point, the NAME should be an ordinary identifier. If
16221 the id-expression was a qualified name, the qualifying scope is
16222 stored in PARSER->SCOPE at this point. */
16223 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16225 /* Perform the lookup. */
16230 if (parser->scope == error_mark_node)
16231 return error_mark_node;
16233 /* If the SCOPE is dependent, the lookup must be deferred until
16234 the template is instantiated -- unless we are explicitly
16235 looking up names in uninstantiated templates. Even then, we
16236 cannot look up the name if the scope is not a class type; it
16237 might, for example, be a template type parameter. */
16238 dependent_p = (TYPE_P (parser->scope)
16239 && !(parser->in_declarator_p
16240 && currently_open_class (parser->scope))
16241 && dependent_type_p (parser->scope));
16242 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16249 /* The resolution to Core Issue 180 says that `struct
16250 A::B' should be considered a type-name, even if `A'
16252 type = make_typename_type (parser->scope, name, tag_type,
16253 /*complain=*/tf_error);
16254 decl = TYPE_NAME (type);
16256 else if (is_template
16257 && (cp_parser_next_token_ends_template_argument_p (parser)
16258 || cp_lexer_next_token_is (parser->lexer,
16260 decl = make_unbound_class_template (parser->scope,
16262 /*complain=*/tf_error);
16264 decl = build_qualified_name (/*type=*/NULL_TREE,
16265 parser->scope, name,
16270 tree pushed_scope = NULL_TREE;
16272 /* If PARSER->SCOPE is a dependent type, then it must be a
16273 class type, and we must not be checking dependencies;
16274 otherwise, we would have processed this lookup above. So
16275 that PARSER->SCOPE is not considered a dependent base by
16276 lookup_member, we must enter the scope here. */
16278 pushed_scope = push_scope (parser->scope);
16279 /* If the PARSER->SCOPE is a template specialization, it
16280 may be instantiated during name lookup. In that case,
16281 errors may be issued. Even if we rollback the current
16282 tentative parse, those errors are valid. */
16283 decl = lookup_qualified_name (parser->scope, name,
16284 tag_type != none_type,
16285 /*complain=*/true);
16287 pop_scope (pushed_scope);
16289 parser->qualifying_scope = parser->scope;
16290 parser->object_scope = NULL_TREE;
16292 else if (object_type)
16294 tree object_decl = NULL_TREE;
16295 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16296 OBJECT_TYPE is not a class. */
16297 if (CLASS_TYPE_P (object_type))
16298 /* If the OBJECT_TYPE is a template specialization, it may
16299 be instantiated during name lookup. In that case, errors
16300 may be issued. Even if we rollback the current tentative
16301 parse, those errors are valid. */
16302 object_decl = lookup_member (object_type,
16305 tag_type != none_type);
16306 /* Look it up in the enclosing context, too. */
16307 decl = lookup_name_real (name, tag_type != none_type,
16309 /*block_p=*/true, is_namespace, flags);
16310 parser->object_scope = object_type;
16311 parser->qualifying_scope = NULL_TREE;
16313 decl = object_decl;
16317 decl = lookup_name_real (name, tag_type != none_type,
16319 /*block_p=*/true, is_namespace, flags);
16320 parser->qualifying_scope = NULL_TREE;
16321 parser->object_scope = NULL_TREE;
16324 /* If the lookup failed, let our caller know. */
16325 if (!decl || decl == error_mark_node)
16326 return error_mark_node;
16328 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16329 if (TREE_CODE (decl) == TREE_LIST)
16331 if (ambiguous_decls)
16332 *ambiguous_decls = decl;
16333 /* The error message we have to print is too complicated for
16334 cp_parser_error, so we incorporate its actions directly. */
16335 if (!cp_parser_simulate_error (parser))
16337 error ("reference to %qD is ambiguous", name);
16338 print_candidates (decl);
16340 return error_mark_node;
16343 gcc_assert (DECL_P (decl)
16344 || TREE_CODE (decl) == OVERLOAD
16345 || TREE_CODE (decl) == SCOPE_REF
16346 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16347 || BASELINK_P (decl));
16349 /* If we have resolved the name of a member declaration, check to
16350 see if the declaration is accessible. When the name resolves to
16351 set of overloaded functions, accessibility is checked when
16352 overload resolution is done.
16354 During an explicit instantiation, access is not checked at all,
16355 as per [temp.explicit]. */
16357 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16362 /* Like cp_parser_lookup_name, but for use in the typical case where
16363 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16364 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16367 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16369 return cp_parser_lookup_name (parser, name,
16371 /*is_template=*/false,
16372 /*is_namespace=*/false,
16373 /*check_dependency=*/true,
16374 /*ambiguous_decls=*/NULL);
16377 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16378 the current context, return the TYPE_DECL. If TAG_NAME_P is
16379 true, the DECL indicates the class being defined in a class-head,
16380 or declared in an elaborated-type-specifier.
16382 Otherwise, return DECL. */
16385 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16387 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16388 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16391 template <typename T> struct B;
16394 template <typename T> struct A::B {};
16396 Similarly, in an elaborated-type-specifier:
16398 namespace N { struct X{}; }
16401 template <typename T> friend struct N::X;
16404 However, if the DECL refers to a class type, and we are in
16405 the scope of the class, then the name lookup automatically
16406 finds the TYPE_DECL created by build_self_reference rather
16407 than a TEMPLATE_DECL. For example, in:
16409 template <class T> struct S {
16413 there is no need to handle such case. */
16415 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16416 return DECL_TEMPLATE_RESULT (decl);
16421 /* If too many, or too few, template-parameter lists apply to the
16422 declarator, issue an error message. Returns TRUE if all went well,
16423 and FALSE otherwise. */
16426 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16427 cp_declarator *declarator)
16429 unsigned num_templates;
16431 /* We haven't seen any classes that involve template parameters yet. */
16434 switch (declarator->kind)
16437 if (declarator->u.id.qualifying_scope)
16442 scope = declarator->u.id.qualifying_scope;
16443 member = declarator->u.id.unqualified_name;
16445 while (scope && CLASS_TYPE_P (scope))
16447 /* You're supposed to have one `template <...>'
16448 for every template class, but you don't need one
16449 for a full specialization. For example:
16451 template <class T> struct S{};
16452 template <> struct S<int> { void f(); };
16453 void S<int>::f () {}
16455 is correct; there shouldn't be a `template <>' for
16456 the definition of `S<int>::f'. */
16457 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16458 /* If SCOPE does not have template information of any
16459 kind, then it is not a template, nor is it nested
16460 within a template. */
16462 if (explicit_class_specialization_p (scope))
16464 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16467 scope = TYPE_CONTEXT (scope);
16470 else if (TREE_CODE (declarator->u.id.unqualified_name)
16471 == TEMPLATE_ID_EXPR)
16472 /* If the DECLARATOR has the form `X<y>' then it uses one
16473 additional level of template parameters. */
16476 return cp_parser_check_template_parameters (parser,
16482 case cdk_reference:
16484 return (cp_parser_check_declarator_template_parameters
16485 (parser, declarator->declarator));
16491 gcc_unreachable ();
16496 /* NUM_TEMPLATES were used in the current declaration. If that is
16497 invalid, return FALSE and issue an error messages. Otherwise,
16501 cp_parser_check_template_parameters (cp_parser* parser,
16502 unsigned num_templates)
16504 /* If there are more template classes than parameter lists, we have
16507 template <class T> void S<T>::R<T>::f (); */
16508 if (parser->num_template_parameter_lists < num_templates)
16510 error ("too few template-parameter-lists");
16513 /* If there are the same number of template classes and parameter
16514 lists, that's OK. */
16515 if (parser->num_template_parameter_lists == num_templates)
16517 /* If there are more, but only one more, then we are referring to a
16518 member template. That's OK too. */
16519 if (parser->num_template_parameter_lists == num_templates + 1)
16521 /* Otherwise, there are too many template parameter lists. We have
16524 template <class T> template <class U> void S::f(); */
16525 error ("too many template-parameter-lists");
16529 /* Parse an optional `::' token indicating that the following name is
16530 from the global namespace. If so, PARSER->SCOPE is set to the
16531 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16532 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16533 Returns the new value of PARSER->SCOPE, if the `::' token is
16534 present, and NULL_TREE otherwise. */
16537 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16541 /* Peek at the next token. */
16542 token = cp_lexer_peek_token (parser->lexer);
16543 /* If we're looking at a `::' token then we're starting from the
16544 global namespace, not our current location. */
16545 if (token->type == CPP_SCOPE)
16547 /* Consume the `::' token. */
16548 cp_lexer_consume_token (parser->lexer);
16549 /* Set the SCOPE so that we know where to start the lookup. */
16550 parser->scope = global_namespace;
16551 parser->qualifying_scope = global_namespace;
16552 parser->object_scope = NULL_TREE;
16554 return parser->scope;
16556 else if (!current_scope_valid_p)
16558 parser->scope = NULL_TREE;
16559 parser->qualifying_scope = NULL_TREE;
16560 parser->object_scope = NULL_TREE;
16566 /* Returns TRUE if the upcoming token sequence is the start of a
16567 constructor declarator. If FRIEND_P is true, the declarator is
16568 preceded by the `friend' specifier. */
16571 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16573 bool constructor_p;
16574 tree type_decl = NULL_TREE;
16575 bool nested_name_p;
16576 cp_token *next_token;
16578 /* The common case is that this is not a constructor declarator, so
16579 try to avoid doing lots of work if at all possible. It's not
16580 valid declare a constructor at function scope. */
16581 if (parser->in_function_body)
16583 /* And only certain tokens can begin a constructor declarator. */
16584 next_token = cp_lexer_peek_token (parser->lexer);
16585 if (next_token->type != CPP_NAME
16586 && next_token->type != CPP_SCOPE
16587 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16588 && next_token->type != CPP_TEMPLATE_ID)
16591 /* Parse tentatively; we are going to roll back all of the tokens
16593 cp_parser_parse_tentatively (parser);
16594 /* Assume that we are looking at a constructor declarator. */
16595 constructor_p = true;
16597 /* Look for the optional `::' operator. */
16598 cp_parser_global_scope_opt (parser,
16599 /*current_scope_valid_p=*/false);
16600 /* Look for the nested-name-specifier. */
16602 = (cp_parser_nested_name_specifier_opt (parser,
16603 /*typename_keyword_p=*/false,
16604 /*check_dependency_p=*/false,
16606 /*is_declaration=*/false)
16608 /* Outside of a class-specifier, there must be a
16609 nested-name-specifier. */
16610 if (!nested_name_p &&
16611 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16613 constructor_p = false;
16614 /* If we still think that this might be a constructor-declarator,
16615 look for a class-name. */
16620 template <typename T> struct S { S(); };
16621 template <typename T> S<T>::S ();
16623 we must recognize that the nested `S' names a class.
16626 template <typename T> S<T>::S<T> ();
16628 we must recognize that the nested `S' names a template. */
16629 type_decl = cp_parser_class_name (parser,
16630 /*typename_keyword_p=*/false,
16631 /*template_keyword_p=*/false,
16633 /*check_dependency_p=*/false,
16634 /*class_head_p=*/false,
16635 /*is_declaration=*/false);
16636 /* If there was no class-name, then this is not a constructor. */
16637 constructor_p = !cp_parser_error_occurred (parser);
16640 /* If we're still considering a constructor, we have to see a `(',
16641 to begin the parameter-declaration-clause, followed by either a
16642 `)', an `...', or a decl-specifier. We need to check for a
16643 type-specifier to avoid being fooled into thinking that:
16647 is a constructor. (It is actually a function named `f' that
16648 takes one parameter (of type `int') and returns a value of type
16651 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16653 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16654 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16655 /* A parameter declaration begins with a decl-specifier,
16656 which is either the "attribute" keyword, a storage class
16657 specifier, or (usually) a type-specifier. */
16658 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16661 tree pushed_scope = NULL_TREE;
16662 unsigned saved_num_template_parameter_lists;
16664 /* Names appearing in the type-specifier should be looked up
16665 in the scope of the class. */
16666 if (current_class_type)
16670 type = TREE_TYPE (type_decl);
16671 if (TREE_CODE (type) == TYPENAME_TYPE)
16673 type = resolve_typename_type (type,
16674 /*only_current_p=*/false);
16675 if (TREE_CODE (type) == TYPENAME_TYPE)
16677 cp_parser_abort_tentative_parse (parser);
16681 pushed_scope = push_scope (type);
16684 /* Inside the constructor parameter list, surrounding
16685 template-parameter-lists do not apply. */
16686 saved_num_template_parameter_lists
16687 = parser->num_template_parameter_lists;
16688 parser->num_template_parameter_lists = 0;
16690 /* Look for the type-specifier. */
16691 cp_parser_type_specifier (parser,
16692 CP_PARSER_FLAGS_NONE,
16693 /*decl_specs=*/NULL,
16694 /*is_declarator=*/true,
16695 /*declares_class_or_enum=*/NULL,
16696 /*is_cv_qualifier=*/NULL);
16698 parser->num_template_parameter_lists
16699 = saved_num_template_parameter_lists;
16701 /* Leave the scope of the class. */
16703 pop_scope (pushed_scope);
16705 constructor_p = !cp_parser_error_occurred (parser);
16709 constructor_p = false;
16710 /* We did not really want to consume any tokens. */
16711 cp_parser_abort_tentative_parse (parser);
16713 return constructor_p;
16716 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16717 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16718 they must be performed once we are in the scope of the function.
16720 Returns the function defined. */
16723 cp_parser_function_definition_from_specifiers_and_declarator
16724 (cp_parser* parser,
16725 cp_decl_specifier_seq *decl_specifiers,
16727 const cp_declarator *declarator)
16732 /* Begin the function-definition. */
16733 success_p = start_function (decl_specifiers, declarator, attributes);
16735 /* The things we're about to see are not directly qualified by any
16736 template headers we've seen thus far. */
16737 reset_specialization ();
16739 /* If there were names looked up in the decl-specifier-seq that we
16740 did not check, check them now. We must wait until we are in the
16741 scope of the function to perform the checks, since the function
16742 might be a friend. */
16743 perform_deferred_access_checks ();
16747 /* Skip the entire function. */
16748 cp_parser_skip_to_end_of_block_or_statement (parser);
16749 fn = error_mark_node;
16751 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16753 /* Seen already, skip it. An error message has already been output. */
16754 cp_parser_skip_to_end_of_block_or_statement (parser);
16755 fn = current_function_decl;
16756 current_function_decl = NULL_TREE;
16757 /* If this is a function from a class, pop the nested class. */
16758 if (current_class_name)
16759 pop_nested_class ();
16762 fn = cp_parser_function_definition_after_declarator (parser,
16763 /*inline_p=*/false);
16768 /* Parse the part of a function-definition that follows the
16769 declarator. INLINE_P is TRUE iff this function is an inline
16770 function defined with a class-specifier.
16772 Returns the function defined. */
16775 cp_parser_function_definition_after_declarator (cp_parser* parser,
16779 bool ctor_initializer_p = false;
16780 bool saved_in_unbraced_linkage_specification_p;
16781 bool saved_in_function_body;
16782 unsigned saved_num_template_parameter_lists;
16784 saved_in_function_body = parser->in_function_body;
16785 parser->in_function_body = true;
16786 /* If the next token is `return', then the code may be trying to
16787 make use of the "named return value" extension that G++ used to
16789 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16791 /* Consume the `return' keyword. */
16792 cp_lexer_consume_token (parser->lexer);
16793 /* Look for the identifier that indicates what value is to be
16795 cp_parser_identifier (parser);
16796 /* Issue an error message. */
16797 error ("named return values are no longer supported");
16798 /* Skip tokens until we reach the start of the function body. */
16801 cp_token *token = cp_lexer_peek_token (parser->lexer);
16802 if (token->type == CPP_OPEN_BRACE
16803 || token->type == CPP_EOF
16804 || token->type == CPP_PRAGMA_EOL)
16806 cp_lexer_consume_token (parser->lexer);
16809 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16810 anything declared inside `f'. */
16811 saved_in_unbraced_linkage_specification_p
16812 = parser->in_unbraced_linkage_specification_p;
16813 parser->in_unbraced_linkage_specification_p = false;
16814 /* Inside the function, surrounding template-parameter-lists do not
16816 saved_num_template_parameter_lists
16817 = parser->num_template_parameter_lists;
16818 parser->num_template_parameter_lists = 0;
16819 /* If the next token is `try', then we are looking at a
16820 function-try-block. */
16821 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16822 ctor_initializer_p = cp_parser_function_try_block (parser);
16823 /* A function-try-block includes the function-body, so we only do
16824 this next part if we're not processing a function-try-block. */
16827 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16829 /* Finish the function. */
16830 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16831 (inline_p ? 2 : 0));
16832 /* Generate code for it, if necessary. */
16833 expand_or_defer_fn (fn);
16834 /* Restore the saved values. */
16835 parser->in_unbraced_linkage_specification_p
16836 = saved_in_unbraced_linkage_specification_p;
16837 parser->num_template_parameter_lists
16838 = saved_num_template_parameter_lists;
16839 parser->in_function_body = saved_in_function_body;
16844 /* Parse a template-declaration, assuming that the `export' (and
16845 `extern') keywords, if present, has already been scanned. MEMBER_P
16846 is as for cp_parser_template_declaration. */
16849 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16851 tree decl = NULL_TREE;
16852 VEC (deferred_access_check,gc) *checks;
16853 tree parameter_list;
16854 bool friend_p = false;
16855 bool need_lang_pop;
16857 /* Look for the `template' keyword. */
16858 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16862 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16864 if (at_class_scope_p () && current_function_decl)
16866 /* 14.5.2.2 [temp.mem]
16868 A local class shall not have member templates. */
16869 error ("invalid declaration of member template in local class");
16870 cp_parser_skip_to_end_of_block_or_statement (parser);
16875 A template ... shall not have C linkage. */
16876 if (current_lang_name == lang_name_c)
16878 error ("template with C linkage");
16879 /* Give it C++ linkage to avoid confusing other parts of the
16881 push_lang_context (lang_name_cplusplus);
16882 need_lang_pop = true;
16885 need_lang_pop = false;
16887 /* We cannot perform access checks on the template parameter
16888 declarations until we know what is being declared, just as we
16889 cannot check the decl-specifier list. */
16890 push_deferring_access_checks (dk_deferred);
16892 /* If the next token is `>', then we have an invalid
16893 specialization. Rather than complain about an invalid template
16894 parameter, issue an error message here. */
16895 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16897 cp_parser_error (parser, "invalid explicit specialization");
16898 begin_specialization ();
16899 parameter_list = NULL_TREE;
16902 /* Parse the template parameters. */
16903 parameter_list = cp_parser_template_parameter_list (parser);
16905 /* Get the deferred access checks from the parameter list. These
16906 will be checked once we know what is being declared, as for a
16907 member template the checks must be performed in the scope of the
16908 class containing the member. */
16909 checks = get_deferred_access_checks ();
16911 /* Look for the `>'. */
16912 cp_parser_skip_to_end_of_template_parameter_list (parser);
16913 /* We just processed one more parameter list. */
16914 ++parser->num_template_parameter_lists;
16915 /* If the next token is `template', there are more template
16917 if (cp_lexer_next_token_is_keyword (parser->lexer,
16919 cp_parser_template_declaration_after_export (parser, member_p);
16922 /* There are no access checks when parsing a template, as we do not
16923 know if a specialization will be a friend. */
16924 push_deferring_access_checks (dk_no_check);
16925 decl = cp_parser_single_declaration (parser,
16928 /*explicit_specialization_p=*/false,
16930 pop_deferring_access_checks ();
16932 /* If this is a member template declaration, let the front
16934 if (member_p && !friend_p && decl)
16936 if (TREE_CODE (decl) == TYPE_DECL)
16937 cp_parser_check_access_in_redeclaration (decl);
16939 decl = finish_member_template_decl (decl);
16941 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16942 make_friend_class (current_class_type, TREE_TYPE (decl),
16943 /*complain=*/true);
16945 /* We are done with the current parameter list. */
16946 --parser->num_template_parameter_lists;
16948 pop_deferring_access_checks ();
16951 finish_template_decl (parameter_list);
16953 /* Register member declarations. */
16954 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16955 finish_member_declaration (decl);
16956 /* For the erroneous case of a template with C linkage, we pushed an
16957 implicit C++ linkage scope; exit that scope now. */
16959 pop_lang_context ();
16960 /* If DECL is a function template, we must return to parse it later.
16961 (Even though there is no definition, there might be default
16962 arguments that need handling.) */
16963 if (member_p && decl
16964 && (TREE_CODE (decl) == FUNCTION_DECL
16965 || DECL_FUNCTION_TEMPLATE_P (decl)))
16966 TREE_VALUE (parser->unparsed_functions_queues)
16967 = tree_cons (NULL_TREE, decl,
16968 TREE_VALUE (parser->unparsed_functions_queues));
16971 /* Perform the deferred access checks from a template-parameter-list.
16972 CHECKS is a TREE_LIST of access checks, as returned by
16973 get_deferred_access_checks. */
16976 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16978 ++processing_template_parmlist;
16979 perform_access_checks (checks);
16980 --processing_template_parmlist;
16983 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16984 `function-definition' sequence. MEMBER_P is true, this declaration
16985 appears in a class scope.
16987 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16988 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16991 cp_parser_single_declaration (cp_parser* parser,
16992 VEC (deferred_access_check,gc)* checks,
16994 bool explicit_specialization_p,
16997 int declares_class_or_enum;
16998 tree decl = NULL_TREE;
16999 cp_decl_specifier_seq decl_specifiers;
17000 bool function_definition_p = false;
17002 /* This function is only used when processing a template
17004 gcc_assert (innermost_scope_kind () == sk_template_parms
17005 || innermost_scope_kind () == sk_template_spec);
17007 /* Defer access checks until we know what is being declared. */
17008 push_deferring_access_checks (dk_deferred);
17010 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
17012 cp_parser_decl_specifier_seq (parser,
17013 CP_PARSER_FLAGS_OPTIONAL,
17015 &declares_class_or_enum);
17017 *friend_p = cp_parser_friend_p (&decl_specifiers);
17019 /* There are no template typedefs. */
17020 if (decl_specifiers.specs[(int) ds_typedef])
17022 error ("template declaration of %qs", "typedef");
17023 decl = error_mark_node;
17026 /* Gather up the access checks that occurred the
17027 decl-specifier-seq. */
17028 stop_deferring_access_checks ();
17030 /* Check for the declaration of a template class. */
17031 if (declares_class_or_enum)
17033 if (cp_parser_declares_only_class_p (parser))
17035 decl = shadow_tag (&decl_specifiers);
17040 friend template <typename T> struct A<T>::B;
17043 A<T>::B will be represented by a TYPENAME_TYPE, and
17044 therefore not recognized by shadow_tag. */
17045 if (friend_p && *friend_p
17047 && decl_specifiers.type
17048 && TYPE_P (decl_specifiers.type))
17049 decl = decl_specifiers.type;
17051 if (decl && decl != error_mark_node)
17052 decl = TYPE_NAME (decl);
17054 decl = error_mark_node;
17056 /* Perform access checks for template parameters. */
17057 cp_parser_perform_template_parameter_access_checks (checks);
17060 /* If it's not a template class, try for a template function. If
17061 the next token is a `;', then this declaration does not declare
17062 anything. But, if there were errors in the decl-specifiers, then
17063 the error might well have come from an attempted class-specifier.
17064 In that case, there's no need to warn about a missing declarator. */
17066 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
17067 || decl_specifiers.type != error_mark_node))
17069 decl = cp_parser_init_declarator (parser,
17072 /*function_definition_allowed_p=*/true,
17074 declares_class_or_enum,
17075 &function_definition_p);
17077 /* 7.1.1-1 [dcl.stc]
17079 A storage-class-specifier shall not be specified in an explicit
17080 specialization... */
17082 && explicit_specialization_p
17083 && decl_specifiers.storage_class != sc_none)
17085 error ("explicit template specialization cannot have a storage class");
17086 decl = error_mark_node;
17090 pop_deferring_access_checks ();
17092 /* Clear any current qualification; whatever comes next is the start
17093 of something new. */
17094 parser->scope = NULL_TREE;
17095 parser->qualifying_scope = NULL_TREE;
17096 parser->object_scope = NULL_TREE;
17097 /* Look for a trailing `;' after the declaration. */
17098 if (!function_definition_p
17099 && (decl == error_mark_node
17100 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
17101 cp_parser_skip_to_end_of_block_or_statement (parser);
17106 /* Parse a cast-expression that is not the operand of a unary "&". */
17109 cp_parser_simple_cast_expression (cp_parser *parser)
17111 return cp_parser_cast_expression (parser, /*address_p=*/false,
17115 /* Parse a functional cast to TYPE. Returns an expression
17116 representing the cast. */
17119 cp_parser_functional_cast (cp_parser* parser, tree type)
17121 tree expression_list;
17125 = cp_parser_parenthesized_expression_list (parser, false,
17127 /*allow_expansion_p=*/true,
17128 /*non_constant_p=*/NULL);
17130 cast = build_functional_cast (type, expression_list);
17131 /* [expr.const]/1: In an integral constant expression "only type
17132 conversions to integral or enumeration type can be used". */
17133 if (TREE_CODE (type) == TYPE_DECL)
17134 type = TREE_TYPE (type);
17135 if (cast != error_mark_node
17136 && !cast_valid_in_integral_constant_expression_p (type)
17137 && (cp_parser_non_integral_constant_expression
17138 (parser, "a call to a constructor")))
17139 return error_mark_node;
17143 /* Save the tokens that make up the body of a member function defined
17144 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17145 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17146 specifiers applied to the declaration. Returns the FUNCTION_DECL
17147 for the member function. */
17150 cp_parser_save_member_function_body (cp_parser* parser,
17151 cp_decl_specifier_seq *decl_specifiers,
17152 cp_declarator *declarator,
17159 /* Create the function-declaration. */
17160 fn = start_method (decl_specifiers, declarator, attributes);
17161 /* If something went badly wrong, bail out now. */
17162 if (fn == error_mark_node)
17164 /* If there's a function-body, skip it. */
17165 if (cp_parser_token_starts_function_definition_p
17166 (cp_lexer_peek_token (parser->lexer)))
17167 cp_parser_skip_to_end_of_block_or_statement (parser);
17168 return error_mark_node;
17171 /* Remember it, if there default args to post process. */
17172 cp_parser_save_default_args (parser, fn);
17174 /* Save away the tokens that make up the body of the
17176 first = parser->lexer->next_token;
17177 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17178 /* Handle function try blocks. */
17179 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17180 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17181 last = parser->lexer->next_token;
17183 /* Save away the inline definition; we will process it when the
17184 class is complete. */
17185 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17186 DECL_PENDING_INLINE_P (fn) = 1;
17188 /* We need to know that this was defined in the class, so that
17189 friend templates are handled correctly. */
17190 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17192 /* We're done with the inline definition. */
17193 finish_method (fn);
17195 /* Add FN to the queue of functions to be parsed later. */
17196 TREE_VALUE (parser->unparsed_functions_queues)
17197 = tree_cons (NULL_TREE, fn,
17198 TREE_VALUE (parser->unparsed_functions_queues));
17203 /* Parse a template-argument-list, as well as the trailing ">" (but
17204 not the opening ">"). See cp_parser_template_argument_list for the
17208 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17212 tree saved_qualifying_scope;
17213 tree saved_object_scope;
17214 bool saved_greater_than_is_operator_p;
17215 bool saved_skip_evaluation;
17219 When parsing a template-id, the first non-nested `>' is taken as
17220 the end of the template-argument-list rather than a greater-than
17222 saved_greater_than_is_operator_p
17223 = parser->greater_than_is_operator_p;
17224 parser->greater_than_is_operator_p = false;
17225 /* Parsing the argument list may modify SCOPE, so we save it
17227 saved_scope = parser->scope;
17228 saved_qualifying_scope = parser->qualifying_scope;
17229 saved_object_scope = parser->object_scope;
17230 /* We need to evaluate the template arguments, even though this
17231 template-id may be nested within a "sizeof". */
17232 saved_skip_evaluation = skip_evaluation;
17233 skip_evaluation = false;
17234 /* Parse the template-argument-list itself. */
17235 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17236 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17237 arguments = NULL_TREE;
17239 arguments = cp_parser_template_argument_list (parser);
17240 /* Look for the `>' that ends the template-argument-list. If we find
17241 a '>>' instead, it's probably just a typo. */
17242 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17244 if (cxx_dialect != cxx98)
17246 /* In C++0x, a `>>' in a template argument list or cast
17247 expression is considered to be two separate `>'
17248 tokens. So, change the current token to a `>', but don't
17249 consume it: it will be consumed later when the outer
17250 template argument list (or cast expression) is parsed.
17251 Note that this replacement of `>' for `>>' is necessary
17252 even if we are parsing tentatively: in the tentative
17253 case, after calling
17254 cp_parser_enclosed_template_argument_list we will always
17255 throw away all of the template arguments and the first
17256 closing `>', either because the template argument list
17257 was erroneous or because we are replacing those tokens
17258 with a CPP_TEMPLATE_ID token. The second `>' (which will
17259 not have been thrown away) is needed either to close an
17260 outer template argument list or to complete a new-style
17262 cp_token *token = cp_lexer_peek_token (parser->lexer);
17263 token->type = CPP_GREATER;
17265 else if (!saved_greater_than_is_operator_p)
17267 /* If we're in a nested template argument list, the '>>' has
17268 to be a typo for '> >'. We emit the error message, but we
17269 continue parsing and we push a '>' as next token, so that
17270 the argument list will be parsed correctly. Note that the
17271 global source location is still on the token before the
17272 '>>', so we need to say explicitly where we want it. */
17273 cp_token *token = cp_lexer_peek_token (parser->lexer);
17274 error ("%H%<>>%> should be %<> >%> "
17275 "within a nested template argument list",
17278 token->type = CPP_GREATER;
17282 /* If this is not a nested template argument list, the '>>'
17283 is a typo for '>'. Emit an error message and continue.
17284 Same deal about the token location, but here we can get it
17285 right by consuming the '>>' before issuing the diagnostic. */
17286 cp_lexer_consume_token (parser->lexer);
17287 error ("spurious %<>>%>, use %<>%> to terminate "
17288 "a template argument list");
17292 cp_parser_skip_to_end_of_template_parameter_list (parser);
17293 /* The `>' token might be a greater-than operator again now. */
17294 parser->greater_than_is_operator_p
17295 = saved_greater_than_is_operator_p;
17296 /* Restore the SAVED_SCOPE. */
17297 parser->scope = saved_scope;
17298 parser->qualifying_scope = saved_qualifying_scope;
17299 parser->object_scope = saved_object_scope;
17300 skip_evaluation = saved_skip_evaluation;
17305 /* MEMBER_FUNCTION is a member function, or a friend. If default
17306 arguments, or the body of the function have not yet been parsed,
17310 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17312 /* If this member is a template, get the underlying
17314 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17315 member_function = DECL_TEMPLATE_RESULT (member_function);
17317 /* There should not be any class definitions in progress at this
17318 point; the bodies of members are only parsed outside of all class
17320 gcc_assert (parser->num_classes_being_defined == 0);
17321 /* While we're parsing the member functions we might encounter more
17322 classes. We want to handle them right away, but we don't want
17323 them getting mixed up with functions that are currently in the
17325 parser->unparsed_functions_queues
17326 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17328 /* Make sure that any template parameters are in scope. */
17329 maybe_begin_member_template_processing (member_function);
17331 /* If the body of the function has not yet been parsed, parse it
17333 if (DECL_PENDING_INLINE_P (member_function))
17335 tree function_scope;
17336 cp_token_cache *tokens;
17338 /* The function is no longer pending; we are processing it. */
17339 tokens = DECL_PENDING_INLINE_INFO (member_function);
17340 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17341 DECL_PENDING_INLINE_P (member_function) = 0;
17343 /* If this is a local class, enter the scope of the containing
17345 function_scope = current_function_decl;
17346 if (function_scope)
17347 push_function_context_to (function_scope);
17350 /* Push the body of the function onto the lexer stack. */
17351 cp_parser_push_lexer_for_tokens (parser, tokens);
17353 /* Let the front end know that we going to be defining this
17355 start_preparsed_function (member_function, NULL_TREE,
17356 SF_PRE_PARSED | SF_INCLASS_INLINE);
17358 /* Don't do access checking if it is a templated function. */
17359 if (processing_template_decl)
17360 push_deferring_access_checks (dk_no_check);
17362 /* Now, parse the body of the function. */
17363 cp_parser_function_definition_after_declarator (parser,
17364 /*inline_p=*/true);
17366 if (processing_template_decl)
17367 pop_deferring_access_checks ();
17369 /* Leave the scope of the containing function. */
17370 if (function_scope)
17371 pop_function_context_from (function_scope);
17372 cp_parser_pop_lexer (parser);
17375 /* Remove any template parameters from the symbol table. */
17376 maybe_end_member_template_processing ();
17378 /* Restore the queue. */
17379 parser->unparsed_functions_queues
17380 = TREE_CHAIN (parser->unparsed_functions_queues);
17383 /* If DECL contains any default args, remember it on the unparsed
17384 functions queue. */
17387 cp_parser_save_default_args (cp_parser* parser, tree decl)
17391 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17393 probe = TREE_CHAIN (probe))
17394 if (TREE_PURPOSE (probe))
17396 TREE_PURPOSE (parser->unparsed_functions_queues)
17397 = tree_cons (current_class_type, decl,
17398 TREE_PURPOSE (parser->unparsed_functions_queues));
17403 /* FN is a FUNCTION_DECL which may contains a parameter with an
17404 unparsed DEFAULT_ARG. Parse the default args now. This function
17405 assumes that the current scope is the scope in which the default
17406 argument should be processed. */
17409 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17411 bool saved_local_variables_forbidden_p;
17414 /* While we're parsing the default args, we might (due to the
17415 statement expression extension) encounter more classes. We want
17416 to handle them right away, but we don't want them getting mixed
17417 up with default args that are currently in the queue. */
17418 parser->unparsed_functions_queues
17419 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17421 /* Local variable names (and the `this' keyword) may not appear
17422 in a default argument. */
17423 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17424 parser->local_variables_forbidden_p = true;
17426 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17428 parm = TREE_CHAIN (parm))
17430 cp_token_cache *tokens;
17431 tree default_arg = TREE_PURPOSE (parm);
17433 VEC(tree,gc) *insts;
17440 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17441 /* This can happen for a friend declaration for a function
17442 already declared with default arguments. */
17445 /* Push the saved tokens for the default argument onto the parser's
17447 tokens = DEFARG_TOKENS (default_arg);
17448 cp_parser_push_lexer_for_tokens (parser, tokens);
17450 /* Parse the assignment-expression. */
17451 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17453 if (!processing_template_decl)
17454 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17456 TREE_PURPOSE (parm) = parsed_arg;
17458 /* Update any instantiations we've already created. */
17459 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17460 VEC_iterate (tree, insts, ix, copy); ix++)
17461 TREE_PURPOSE (copy) = parsed_arg;
17463 /* If the token stream has not been completely used up, then
17464 there was extra junk after the end of the default
17466 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17467 cp_parser_error (parser, "expected %<,%>");
17469 /* Revert to the main lexer. */
17470 cp_parser_pop_lexer (parser);
17473 /* Make sure no default arg is missing. */
17474 check_default_args (fn);
17476 /* Restore the state of local_variables_forbidden_p. */
17477 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17479 /* Restore the queue. */
17480 parser->unparsed_functions_queues
17481 = TREE_CHAIN (parser->unparsed_functions_queues);
17484 /* Parse the operand of `sizeof' (or a similar operator). Returns
17485 either a TYPE or an expression, depending on the form of the
17486 input. The KEYWORD indicates which kind of expression we have
17490 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17492 static const char *format;
17493 tree expr = NULL_TREE;
17494 const char *saved_message;
17496 bool saved_integral_constant_expression_p;
17497 bool saved_non_integral_constant_expression_p;
17498 bool pack_expansion_p = false;
17500 /* Initialize FORMAT the first time we get here. */
17502 format = "types may not be defined in '%s' expressions";
17504 /* Types cannot be defined in a `sizeof' expression. Save away the
17506 saved_message = parser->type_definition_forbidden_message;
17507 /* And create the new one. */
17508 parser->type_definition_forbidden_message = tmp
17509 = XNEWVEC (char, strlen (format)
17510 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17512 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17514 /* The restrictions on constant-expressions do not apply inside
17515 sizeof expressions. */
17516 saved_integral_constant_expression_p
17517 = parser->integral_constant_expression_p;
17518 saved_non_integral_constant_expression_p
17519 = parser->non_integral_constant_expression_p;
17520 parser->integral_constant_expression_p = false;
17522 /* If it's a `...', then we are computing the length of a parameter
17524 if (keyword == RID_SIZEOF
17525 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17527 /* Consume the `...'. */
17528 cp_lexer_consume_token (parser->lexer);
17529 maybe_warn_variadic_templates ();
17531 /* Note that this is an expansion. */
17532 pack_expansion_p = true;
17535 /* Do not actually evaluate the expression. */
17537 /* If it's a `(', then we might be looking at the type-id
17539 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17542 bool saved_in_type_id_in_expr_p;
17544 /* We can't be sure yet whether we're looking at a type-id or an
17546 cp_parser_parse_tentatively (parser);
17547 /* Consume the `('. */
17548 cp_lexer_consume_token (parser->lexer);
17549 /* Parse the type-id. */
17550 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17551 parser->in_type_id_in_expr_p = true;
17552 type = cp_parser_type_id (parser);
17553 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17554 /* Now, look for the trailing `)'. */
17555 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17556 /* If all went well, then we're done. */
17557 if (cp_parser_parse_definitely (parser))
17559 cp_decl_specifier_seq decl_specs;
17561 /* Build a trivial decl-specifier-seq. */
17562 clear_decl_specs (&decl_specs);
17563 decl_specs.type = type;
17565 /* Call grokdeclarator to figure out what type this is. */
17566 expr = grokdeclarator (NULL,
17570 /*attrlist=*/NULL);
17574 /* If the type-id production did not work out, then we must be
17575 looking at the unary-expression production. */
17577 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17580 if (pack_expansion_p)
17581 /* Build a pack expansion. */
17582 expr = make_pack_expansion (expr);
17584 /* Go back to evaluating expressions. */
17587 /* Free the message we created. */
17589 /* And restore the old one. */
17590 parser->type_definition_forbidden_message = saved_message;
17591 parser->integral_constant_expression_p
17592 = saved_integral_constant_expression_p;
17593 parser->non_integral_constant_expression_p
17594 = saved_non_integral_constant_expression_p;
17599 /* If the current declaration has no declarator, return true. */
17602 cp_parser_declares_only_class_p (cp_parser *parser)
17604 /* If the next token is a `;' or a `,' then there is no
17606 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17607 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17610 /* Update the DECL_SPECS to reflect the storage class indicated by
17614 cp_parser_set_storage_class (cp_parser *parser,
17615 cp_decl_specifier_seq *decl_specs,
17618 cp_storage_class storage_class;
17620 if (parser->in_unbraced_linkage_specification_p)
17622 error ("invalid use of %qD in linkage specification",
17623 ridpointers[keyword]);
17626 else if (decl_specs->storage_class != sc_none)
17628 decl_specs->conflicting_specifiers_p = true;
17632 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17633 && decl_specs->specs[(int) ds_thread])
17635 error ("%<__thread%> before %qD", ridpointers[keyword]);
17636 decl_specs->specs[(int) ds_thread] = 0;
17642 storage_class = sc_auto;
17645 storage_class = sc_register;
17648 storage_class = sc_static;
17651 storage_class = sc_extern;
17654 storage_class = sc_mutable;
17657 gcc_unreachable ();
17659 decl_specs->storage_class = storage_class;
17661 /* A storage class specifier cannot be applied alongside a typedef
17662 specifier. If there is a typedef specifier present then set
17663 conflicting_specifiers_p which will trigger an error later
17664 on in grokdeclarator. */
17665 if (decl_specs->specs[(int)ds_typedef])
17666 decl_specs->conflicting_specifiers_p = true;
17669 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17670 is true, the type is a user-defined type; otherwise it is a
17671 built-in type specified by a keyword. */
17674 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17676 bool user_defined_p)
17678 decl_specs->any_specifiers_p = true;
17680 /* If the user tries to redeclare bool or wchar_t (with, for
17681 example, in "typedef int wchar_t;") we remember that this is what
17682 happened. In system headers, we ignore these declarations so
17683 that G++ can work with system headers that are not C++-safe. */
17684 if (decl_specs->specs[(int) ds_typedef]
17686 && (type_spec == boolean_type_node
17687 || type_spec == wchar_type_node)
17688 && (decl_specs->type
17689 || decl_specs->specs[(int) ds_long]
17690 || decl_specs->specs[(int) ds_short]
17691 || decl_specs->specs[(int) ds_unsigned]
17692 || decl_specs->specs[(int) ds_signed]))
17694 decl_specs->redefined_builtin_type = type_spec;
17695 if (!decl_specs->type)
17697 decl_specs->type = type_spec;
17698 decl_specs->user_defined_type_p = false;
17701 else if (decl_specs->type)
17702 decl_specs->multiple_types_p = true;
17705 decl_specs->type = type_spec;
17706 decl_specs->user_defined_type_p = user_defined_p;
17707 decl_specs->redefined_builtin_type = NULL_TREE;
17711 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17712 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17715 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17717 return decl_specifiers->specs[(int) ds_friend] != 0;
17720 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17721 issue an error message indicating that TOKEN_DESC was expected.
17723 Returns the token consumed, if the token had the appropriate type.
17724 Otherwise, returns NULL. */
17727 cp_parser_require (cp_parser* parser,
17728 enum cpp_ttype type,
17729 const char* token_desc)
17731 if (cp_lexer_next_token_is (parser->lexer, type))
17732 return cp_lexer_consume_token (parser->lexer);
17735 /* Output the MESSAGE -- unless we're parsing tentatively. */
17736 if (!cp_parser_simulate_error (parser))
17738 char *message = concat ("expected ", token_desc, NULL);
17739 cp_parser_error (parser, message);
17746 /* An error message is produced if the next token is not '>'.
17747 All further tokens are skipped until the desired token is
17748 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17751 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17753 /* Current level of '< ... >'. */
17754 unsigned level = 0;
17755 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17756 unsigned nesting_depth = 0;
17758 /* Are we ready, yet? If not, issue error message. */
17759 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17762 /* Skip tokens until the desired token is found. */
17765 /* Peek at the next token. */
17766 switch (cp_lexer_peek_token (parser->lexer)->type)
17769 if (!nesting_depth)
17774 if (cxx_dialect == cxx98)
17775 /* C++0x views the `>>' operator as two `>' tokens, but
17778 else if (!nesting_depth && level-- == 0)
17780 /* We've hit a `>>' where the first `>' closes the
17781 template argument list, and the second `>' is
17782 spurious. Just consume the `>>' and stop; we've
17783 already produced at least one error. */
17784 cp_lexer_consume_token (parser->lexer);
17787 /* Fall through for C++0x, so we handle the second `>' in
17791 if (!nesting_depth && level-- == 0)
17793 /* We've reached the token we want, consume it and stop. */
17794 cp_lexer_consume_token (parser->lexer);
17799 case CPP_OPEN_PAREN:
17800 case CPP_OPEN_SQUARE:
17804 case CPP_CLOSE_PAREN:
17805 case CPP_CLOSE_SQUARE:
17806 if (nesting_depth-- == 0)
17811 case CPP_PRAGMA_EOL:
17812 case CPP_SEMICOLON:
17813 case CPP_OPEN_BRACE:
17814 case CPP_CLOSE_BRACE:
17815 /* The '>' was probably forgotten, don't look further. */
17822 /* Consume this token. */
17823 cp_lexer_consume_token (parser->lexer);
17827 /* If the next token is the indicated keyword, consume it. Otherwise,
17828 issue an error message indicating that TOKEN_DESC was expected.
17830 Returns the token consumed, if the token had the appropriate type.
17831 Otherwise, returns NULL. */
17834 cp_parser_require_keyword (cp_parser* parser,
17836 const char* token_desc)
17838 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17840 if (token && token->keyword != keyword)
17842 dyn_string_t error_msg;
17844 /* Format the error message. */
17845 error_msg = dyn_string_new (0);
17846 dyn_string_append_cstr (error_msg, "expected ");
17847 dyn_string_append_cstr (error_msg, token_desc);
17848 cp_parser_error (parser, error_msg->s);
17849 dyn_string_delete (error_msg);
17856 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17857 function-definition. */
17860 cp_parser_token_starts_function_definition_p (cp_token* token)
17862 return (/* An ordinary function-body begins with an `{'. */
17863 token->type == CPP_OPEN_BRACE
17864 /* A ctor-initializer begins with a `:'. */
17865 || token->type == CPP_COLON
17866 /* A function-try-block begins with `try'. */
17867 || token->keyword == RID_TRY
17868 /* The named return value extension begins with `return'. */
17869 || token->keyword == RID_RETURN);
17872 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17876 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17880 token = cp_lexer_peek_token (parser->lexer);
17881 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17884 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17885 C++0x) ending a template-argument. */
17888 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17892 token = cp_lexer_peek_token (parser->lexer);
17893 return (token->type == CPP_COMMA
17894 || token->type == CPP_GREATER
17895 || token->type == CPP_ELLIPSIS
17896 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17899 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17900 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17903 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17908 token = cp_lexer_peek_nth_token (parser->lexer, n);
17909 if (token->type == CPP_LESS)
17911 /* Check for the sequence `<::' in the original code. It would be lexed as
17912 `[:', where `[' is a digraph, and there is no whitespace before
17914 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17917 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17918 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17924 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17925 or none_type otherwise. */
17927 static enum tag_types
17928 cp_parser_token_is_class_key (cp_token* token)
17930 switch (token->keyword)
17935 return record_type;
17944 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17947 cp_parser_check_class_key (enum tag_types class_key, tree type)
17949 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17950 pedwarn ("%qs tag used in naming %q#T",
17951 class_key == union_type ? "union"
17952 : class_key == record_type ? "struct" : "class",
17956 /* Issue an error message if DECL is redeclared with different
17957 access than its original declaration [class.access.spec/3].
17958 This applies to nested classes and nested class templates.
17962 cp_parser_check_access_in_redeclaration (tree decl)
17964 if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
17967 if ((TREE_PRIVATE (decl)
17968 != (current_access_specifier == access_private_node))
17969 || (TREE_PROTECTED (decl)
17970 != (current_access_specifier == access_protected_node)))
17971 error ("%qD redeclared with different access", decl);
17974 /* Look for the `template' keyword, as a syntactic disambiguator.
17975 Return TRUE iff it is present, in which case it will be
17979 cp_parser_optional_template_keyword (cp_parser *parser)
17981 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17983 /* The `template' keyword can only be used within templates;
17984 outside templates the parser can always figure out what is a
17985 template and what is not. */
17986 if (!processing_template_decl)
17988 error ("%<template%> (as a disambiguator) is only allowed "
17989 "within templates");
17990 /* If this part of the token stream is rescanned, the same
17991 error message would be generated. So, we purge the token
17992 from the stream. */
17993 cp_lexer_purge_token (parser->lexer);
17998 /* Consume the `template' keyword. */
17999 cp_lexer_consume_token (parser->lexer);
18007 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
18008 set PARSER->SCOPE, and perform other related actions. */
18011 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
18014 struct tree_check *check_value;
18015 deferred_access_check *chk;
18016 VEC (deferred_access_check,gc) *checks;
18018 /* Get the stored value. */
18019 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
18020 /* Perform any access checks that were deferred. */
18021 checks = check_value->checks;
18025 VEC_iterate (deferred_access_check, checks, i, chk) ;
18028 perform_or_defer_access_check (chk->binfo,
18033 /* Set the scope from the stored value. */
18034 parser->scope = check_value->value;
18035 parser->qualifying_scope = check_value->qualifying_scope;
18036 parser->object_scope = NULL_TREE;
18039 /* Consume tokens up through a non-nested END token. */
18042 cp_parser_cache_group (cp_parser *parser,
18043 enum cpp_ttype end,
18050 /* Abort a parenthesized expression if we encounter a brace. */
18051 if ((end == CPP_CLOSE_PAREN || depth == 0)
18052 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18054 /* If we've reached the end of the file, stop. */
18055 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
18056 || (end != CPP_PRAGMA_EOL
18057 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
18059 /* Consume the next token. */
18060 token = cp_lexer_consume_token (parser->lexer);
18061 /* See if it starts a new group. */
18062 if (token->type == CPP_OPEN_BRACE)
18064 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
18068 else if (token->type == CPP_OPEN_PAREN)
18069 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
18070 else if (token->type == CPP_PRAGMA)
18071 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
18072 else if (token->type == end)
18077 /* Begin parsing tentatively. We always save tokens while parsing
18078 tentatively so that if the tentative parsing fails we can restore the
18082 cp_parser_parse_tentatively (cp_parser* parser)
18084 /* Enter a new parsing context. */
18085 parser->context = cp_parser_context_new (parser->context);
18086 /* Begin saving tokens. */
18087 cp_lexer_save_tokens (parser->lexer);
18088 /* In order to avoid repetitive access control error messages,
18089 access checks are queued up until we are no longer parsing
18091 push_deferring_access_checks (dk_deferred);
18094 /* Commit to the currently active tentative parse. */
18097 cp_parser_commit_to_tentative_parse (cp_parser* parser)
18099 cp_parser_context *context;
18102 /* Mark all of the levels as committed. */
18103 lexer = parser->lexer;
18104 for (context = parser->context; context->next; context = context->next)
18106 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
18108 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
18109 while (!cp_lexer_saving_tokens (lexer))
18110 lexer = lexer->next;
18111 cp_lexer_commit_tokens (lexer);
18115 /* Abort the currently active tentative parse. All consumed tokens
18116 will be rolled back, and no diagnostics will be issued. */
18119 cp_parser_abort_tentative_parse (cp_parser* parser)
18121 cp_parser_simulate_error (parser);
18122 /* Now, pretend that we want to see if the construct was
18123 successfully parsed. */
18124 cp_parser_parse_definitely (parser);
18127 /* Stop parsing tentatively. If a parse error has occurred, restore the
18128 token stream. Otherwise, commit to the tokens we have consumed.
18129 Returns true if no error occurred; false otherwise. */
18132 cp_parser_parse_definitely (cp_parser* parser)
18134 bool error_occurred;
18135 cp_parser_context *context;
18137 /* Remember whether or not an error occurred, since we are about to
18138 destroy that information. */
18139 error_occurred = cp_parser_error_occurred (parser);
18140 /* Remove the topmost context from the stack. */
18141 context = parser->context;
18142 parser->context = context->next;
18143 /* If no parse errors occurred, commit to the tentative parse. */
18144 if (!error_occurred)
18146 /* Commit to the tokens read tentatively, unless that was
18148 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18149 cp_lexer_commit_tokens (parser->lexer);
18151 pop_to_parent_deferring_access_checks ();
18153 /* Otherwise, if errors occurred, roll back our state so that things
18154 are just as they were before we began the tentative parse. */
18157 cp_lexer_rollback_tokens (parser->lexer);
18158 pop_deferring_access_checks ();
18160 /* Add the context to the front of the free list. */
18161 context->next = cp_parser_context_free_list;
18162 cp_parser_context_free_list = context;
18164 return !error_occurred;
18167 /* Returns true if we are parsing tentatively and are not committed to
18168 this tentative parse. */
18171 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18173 return (cp_parser_parsing_tentatively (parser)
18174 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18177 /* Returns nonzero iff an error has occurred during the most recent
18178 tentative parse. */
18181 cp_parser_error_occurred (cp_parser* parser)
18183 return (cp_parser_parsing_tentatively (parser)
18184 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18187 /* Returns nonzero if GNU extensions are allowed. */
18190 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18192 return parser->allow_gnu_extensions_p;
18195 /* Objective-C++ Productions */
18198 /* Parse an Objective-C expression, which feeds into a primary-expression
18202 objc-message-expression
18203 objc-string-literal
18204 objc-encode-expression
18205 objc-protocol-expression
18206 objc-selector-expression
18208 Returns a tree representation of the expression. */
18211 cp_parser_objc_expression (cp_parser* parser)
18213 /* Try to figure out what kind of declaration is present. */
18214 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18218 case CPP_OPEN_SQUARE:
18219 return cp_parser_objc_message_expression (parser);
18221 case CPP_OBJC_STRING:
18222 kwd = cp_lexer_consume_token (parser->lexer);
18223 return objc_build_string_object (kwd->u.value);
18226 switch (kwd->keyword)
18228 case RID_AT_ENCODE:
18229 return cp_parser_objc_encode_expression (parser);
18231 case RID_AT_PROTOCOL:
18232 return cp_parser_objc_protocol_expression (parser);
18234 case RID_AT_SELECTOR:
18235 return cp_parser_objc_selector_expression (parser);
18241 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18242 cp_parser_skip_to_end_of_block_or_statement (parser);
18245 return error_mark_node;
18248 /* Parse an Objective-C message expression.
18250 objc-message-expression:
18251 [ objc-message-receiver objc-message-args ]
18253 Returns a representation of an Objective-C message. */
18256 cp_parser_objc_message_expression (cp_parser* parser)
18258 tree receiver, messageargs;
18260 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18261 receiver = cp_parser_objc_message_receiver (parser);
18262 messageargs = cp_parser_objc_message_args (parser);
18263 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18265 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18268 /* Parse an objc-message-receiver.
18270 objc-message-receiver:
18272 simple-type-specifier
18274 Returns a representation of the type or expression. */
18277 cp_parser_objc_message_receiver (cp_parser* parser)
18281 /* An Objective-C message receiver may be either (1) a type
18282 or (2) an expression. */
18283 cp_parser_parse_tentatively (parser);
18284 rcv = cp_parser_expression (parser, false);
18286 if (cp_parser_parse_definitely (parser))
18289 rcv = cp_parser_simple_type_specifier (parser,
18290 /*decl_specs=*/NULL,
18291 CP_PARSER_FLAGS_NONE);
18293 return objc_get_class_reference (rcv);
18296 /* Parse the arguments and selectors comprising an Objective-C message.
18301 objc-selector-args , objc-comma-args
18303 objc-selector-args:
18304 objc-selector [opt] : assignment-expression
18305 objc-selector-args objc-selector [opt] : assignment-expression
18308 assignment-expression
18309 objc-comma-args , assignment-expression
18311 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18312 selector arguments and TREE_VALUE containing a list of comma
18316 cp_parser_objc_message_args (cp_parser* parser)
18318 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18319 bool maybe_unary_selector_p = true;
18320 cp_token *token = cp_lexer_peek_token (parser->lexer);
18322 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18324 tree selector = NULL_TREE, arg;
18326 if (token->type != CPP_COLON)
18327 selector = cp_parser_objc_selector (parser);
18329 /* Detect if we have a unary selector. */
18330 if (maybe_unary_selector_p
18331 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18332 return build_tree_list (selector, NULL_TREE);
18334 maybe_unary_selector_p = false;
18335 cp_parser_require (parser, CPP_COLON, "`:'");
18336 arg = cp_parser_assignment_expression (parser, false);
18339 = chainon (sel_args,
18340 build_tree_list (selector, arg));
18342 token = cp_lexer_peek_token (parser->lexer);
18345 /* Handle non-selector arguments, if any. */
18346 while (token->type == CPP_COMMA)
18350 cp_lexer_consume_token (parser->lexer);
18351 arg = cp_parser_assignment_expression (parser, false);
18354 = chainon (addl_args,
18355 build_tree_list (NULL_TREE, arg));
18357 token = cp_lexer_peek_token (parser->lexer);
18360 return build_tree_list (sel_args, addl_args);
18363 /* Parse an Objective-C encode expression.
18365 objc-encode-expression:
18366 @encode objc-typename
18368 Returns an encoded representation of the type argument. */
18371 cp_parser_objc_encode_expression (cp_parser* parser)
18375 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18376 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18377 type = complete_type (cp_parser_type_id (parser));
18378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18382 error ("%<@encode%> must specify a type as an argument");
18383 return error_mark_node;
18386 return objc_build_encode_expr (type);
18389 /* Parse an Objective-C @defs expression. */
18392 cp_parser_objc_defs_expression (cp_parser *parser)
18396 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18397 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18398 name = cp_parser_identifier (parser);
18399 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18401 return objc_get_class_ivars (name);
18404 /* Parse an Objective-C protocol expression.
18406 objc-protocol-expression:
18407 @protocol ( identifier )
18409 Returns a representation of the protocol expression. */
18412 cp_parser_objc_protocol_expression (cp_parser* parser)
18416 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18417 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18418 proto = cp_parser_identifier (parser);
18419 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18421 return objc_build_protocol_expr (proto);
18424 /* Parse an Objective-C selector expression.
18426 objc-selector-expression:
18427 @selector ( objc-method-signature )
18429 objc-method-signature:
18435 objc-selector-seq objc-selector :
18437 Returns a representation of the method selector. */
18440 cp_parser_objc_selector_expression (cp_parser* parser)
18442 tree sel_seq = NULL_TREE;
18443 bool maybe_unary_selector_p = true;
18446 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18447 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18448 token = cp_lexer_peek_token (parser->lexer);
18450 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18451 || token->type == CPP_SCOPE)
18453 tree selector = NULL_TREE;
18455 if (token->type != CPP_COLON
18456 || token->type == CPP_SCOPE)
18457 selector = cp_parser_objc_selector (parser);
18459 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18460 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18462 /* Detect if we have a unary selector. */
18463 if (maybe_unary_selector_p)
18465 sel_seq = selector;
18466 goto finish_selector;
18470 cp_parser_error (parser, "expected %<:%>");
18473 maybe_unary_selector_p = false;
18474 token = cp_lexer_consume_token (parser->lexer);
18476 if (token->type == CPP_SCOPE)
18479 = chainon (sel_seq,
18480 build_tree_list (selector, NULL_TREE));
18482 = chainon (sel_seq,
18483 build_tree_list (NULL_TREE, NULL_TREE));
18487 = chainon (sel_seq,
18488 build_tree_list (selector, NULL_TREE));
18490 token = cp_lexer_peek_token (parser->lexer);
18494 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18496 return objc_build_selector_expr (sel_seq);
18499 /* Parse a list of identifiers.
18501 objc-identifier-list:
18503 objc-identifier-list , identifier
18505 Returns a TREE_LIST of identifier nodes. */
18508 cp_parser_objc_identifier_list (cp_parser* parser)
18510 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18511 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18513 while (sep->type == CPP_COMMA)
18515 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18516 list = chainon (list,
18517 build_tree_list (NULL_TREE,
18518 cp_parser_identifier (parser)));
18519 sep = cp_lexer_peek_token (parser->lexer);
18525 /* Parse an Objective-C alias declaration.
18527 objc-alias-declaration:
18528 @compatibility_alias identifier identifier ;
18530 This function registers the alias mapping with the Objective-C front end.
18531 It returns nothing. */
18534 cp_parser_objc_alias_declaration (cp_parser* parser)
18538 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18539 alias = cp_parser_identifier (parser);
18540 orig = cp_parser_identifier (parser);
18541 objc_declare_alias (alias, orig);
18542 cp_parser_consume_semicolon_at_end_of_statement (parser);
18545 /* Parse an Objective-C class forward-declaration.
18547 objc-class-declaration:
18548 @class objc-identifier-list ;
18550 The function registers the forward declarations with the Objective-C
18551 front end. It returns nothing. */
18554 cp_parser_objc_class_declaration (cp_parser* parser)
18556 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18557 objc_declare_class (cp_parser_objc_identifier_list (parser));
18558 cp_parser_consume_semicolon_at_end_of_statement (parser);
18561 /* Parse a list of Objective-C protocol references.
18563 objc-protocol-refs-opt:
18564 objc-protocol-refs [opt]
18566 objc-protocol-refs:
18567 < objc-identifier-list >
18569 Returns a TREE_LIST of identifiers, if any. */
18572 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18574 tree protorefs = NULL_TREE;
18576 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18578 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18579 protorefs = cp_parser_objc_identifier_list (parser);
18580 cp_parser_require (parser, CPP_GREATER, "`>'");
18586 /* Parse a Objective-C visibility specification. */
18589 cp_parser_objc_visibility_spec (cp_parser* parser)
18591 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18593 switch (vis->keyword)
18595 case RID_AT_PRIVATE:
18596 objc_set_visibility (2);
18598 case RID_AT_PROTECTED:
18599 objc_set_visibility (0);
18601 case RID_AT_PUBLIC:
18602 objc_set_visibility (1);
18608 /* Eat '@private'/'@protected'/'@public'. */
18609 cp_lexer_consume_token (parser->lexer);
18612 /* Parse an Objective-C method type. */
18615 cp_parser_objc_method_type (cp_parser* parser)
18617 objc_set_method_type
18618 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18623 /* Parse an Objective-C protocol qualifier. */
18626 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18628 tree quals = NULL_TREE, node;
18629 cp_token *token = cp_lexer_peek_token (parser->lexer);
18631 node = token->u.value;
18633 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18634 && (node == ridpointers [(int) RID_IN]
18635 || node == ridpointers [(int) RID_OUT]
18636 || node == ridpointers [(int) RID_INOUT]
18637 || node == ridpointers [(int) RID_BYCOPY]
18638 || node == ridpointers [(int) RID_BYREF]
18639 || node == ridpointers [(int) RID_ONEWAY]))
18641 quals = tree_cons (NULL_TREE, node, quals);
18642 cp_lexer_consume_token (parser->lexer);
18643 token = cp_lexer_peek_token (parser->lexer);
18644 node = token->u.value;
18650 /* Parse an Objective-C typename. */
18653 cp_parser_objc_typename (cp_parser* parser)
18655 tree typename = NULL_TREE;
18657 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18659 tree proto_quals, cp_type = NULL_TREE;
18661 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18662 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18664 /* An ObjC type name may consist of just protocol qualifiers, in which
18665 case the type shall default to 'id'. */
18666 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18667 cp_type = cp_parser_type_id (parser);
18669 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18670 typename = build_tree_list (proto_quals, cp_type);
18676 /* Check to see if TYPE refers to an Objective-C selector name. */
18679 cp_parser_objc_selector_p (enum cpp_ttype type)
18681 return (type == CPP_NAME || type == CPP_KEYWORD
18682 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18683 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18684 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18685 || type == CPP_XOR || type == CPP_XOR_EQ);
18688 /* Parse an Objective-C selector. */
18691 cp_parser_objc_selector (cp_parser* parser)
18693 cp_token *token = cp_lexer_consume_token (parser->lexer);
18695 if (!cp_parser_objc_selector_p (token->type))
18697 error ("invalid Objective-C++ selector name");
18698 return error_mark_node;
18701 /* C++ operator names are allowed to appear in ObjC selectors. */
18702 switch (token->type)
18704 case CPP_AND_AND: return get_identifier ("and");
18705 case CPP_AND_EQ: return get_identifier ("and_eq");
18706 case CPP_AND: return get_identifier ("bitand");
18707 case CPP_OR: return get_identifier ("bitor");
18708 case CPP_COMPL: return get_identifier ("compl");
18709 case CPP_NOT: return get_identifier ("not");
18710 case CPP_NOT_EQ: return get_identifier ("not_eq");
18711 case CPP_OR_OR: return get_identifier ("or");
18712 case CPP_OR_EQ: return get_identifier ("or_eq");
18713 case CPP_XOR: return get_identifier ("xor");
18714 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18715 default: return token->u.value;
18719 /* Parse an Objective-C params list. */
18722 cp_parser_objc_method_keyword_params (cp_parser* parser)
18724 tree params = NULL_TREE;
18725 bool maybe_unary_selector_p = true;
18726 cp_token *token = cp_lexer_peek_token (parser->lexer);
18728 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18730 tree selector = NULL_TREE, typename, identifier;
18732 if (token->type != CPP_COLON)
18733 selector = cp_parser_objc_selector (parser);
18735 /* Detect if we have a unary selector. */
18736 if (maybe_unary_selector_p
18737 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18740 maybe_unary_selector_p = false;
18741 cp_parser_require (parser, CPP_COLON, "`:'");
18742 typename = cp_parser_objc_typename (parser);
18743 identifier = cp_parser_identifier (parser);
18747 objc_build_keyword_decl (selector,
18751 token = cp_lexer_peek_token (parser->lexer);
18757 /* Parse the non-keyword Objective-C params. */
18760 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18762 tree params = make_node (TREE_LIST);
18763 cp_token *token = cp_lexer_peek_token (parser->lexer);
18764 *ellipsisp = false; /* Initially, assume no ellipsis. */
18766 while (token->type == CPP_COMMA)
18768 cp_parameter_declarator *parmdecl;
18771 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18772 token = cp_lexer_peek_token (parser->lexer);
18774 if (token->type == CPP_ELLIPSIS)
18776 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18781 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18782 parm = grokdeclarator (parmdecl->declarator,
18783 &parmdecl->decl_specifiers,
18784 PARM, /*initialized=*/0,
18785 /*attrlist=*/NULL);
18787 chainon (params, build_tree_list (NULL_TREE, parm));
18788 token = cp_lexer_peek_token (parser->lexer);
18794 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18797 cp_parser_objc_interstitial_code (cp_parser* parser)
18799 cp_token *token = cp_lexer_peek_token (parser->lexer);
18801 /* If the next token is `extern' and the following token is a string
18802 literal, then we have a linkage specification. */
18803 if (token->keyword == RID_EXTERN
18804 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18805 cp_parser_linkage_specification (parser);
18806 /* Handle #pragma, if any. */
18807 else if (token->type == CPP_PRAGMA)
18808 cp_parser_pragma (parser, pragma_external);
18809 /* Allow stray semicolons. */
18810 else if (token->type == CPP_SEMICOLON)
18811 cp_lexer_consume_token (parser->lexer);
18812 /* Finally, try to parse a block-declaration, or a function-definition. */
18814 cp_parser_block_declaration (parser, /*statement_p=*/false);
18817 /* Parse a method signature. */
18820 cp_parser_objc_method_signature (cp_parser* parser)
18822 tree rettype, kwdparms, optparms;
18823 bool ellipsis = false;
18825 cp_parser_objc_method_type (parser);
18826 rettype = cp_parser_objc_typename (parser);
18827 kwdparms = cp_parser_objc_method_keyword_params (parser);
18828 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18830 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18833 /* Pars an Objective-C method prototype list. */
18836 cp_parser_objc_method_prototype_list (cp_parser* parser)
18838 cp_token *token = cp_lexer_peek_token (parser->lexer);
18840 while (token->keyword != RID_AT_END)
18842 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18844 objc_add_method_declaration
18845 (cp_parser_objc_method_signature (parser));
18846 cp_parser_consume_semicolon_at_end_of_statement (parser);
18849 /* Allow for interspersed non-ObjC++ code. */
18850 cp_parser_objc_interstitial_code (parser);
18852 token = cp_lexer_peek_token (parser->lexer);
18855 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18856 objc_finish_interface ();
18859 /* Parse an Objective-C method definition list. */
18862 cp_parser_objc_method_definition_list (cp_parser* parser)
18864 cp_token *token = cp_lexer_peek_token (parser->lexer);
18866 while (token->keyword != RID_AT_END)
18870 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18872 push_deferring_access_checks (dk_deferred);
18873 objc_start_method_definition
18874 (cp_parser_objc_method_signature (parser));
18876 /* For historical reasons, we accept an optional semicolon. */
18877 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18878 cp_lexer_consume_token (parser->lexer);
18880 perform_deferred_access_checks ();
18881 stop_deferring_access_checks ();
18882 meth = cp_parser_function_definition_after_declarator (parser,
18884 pop_deferring_access_checks ();
18885 objc_finish_method_definition (meth);
18888 /* Allow for interspersed non-ObjC++ code. */
18889 cp_parser_objc_interstitial_code (parser);
18891 token = cp_lexer_peek_token (parser->lexer);
18894 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18895 objc_finish_implementation ();
18898 /* Parse Objective-C ivars. */
18901 cp_parser_objc_class_ivars (cp_parser* parser)
18903 cp_token *token = cp_lexer_peek_token (parser->lexer);
18905 if (token->type != CPP_OPEN_BRACE)
18906 return; /* No ivars specified. */
18908 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18909 token = cp_lexer_peek_token (parser->lexer);
18911 while (token->type != CPP_CLOSE_BRACE)
18913 cp_decl_specifier_seq declspecs;
18914 int decl_class_or_enum_p;
18915 tree prefix_attributes;
18917 cp_parser_objc_visibility_spec (parser);
18919 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18922 cp_parser_decl_specifier_seq (parser,
18923 CP_PARSER_FLAGS_OPTIONAL,
18925 &decl_class_or_enum_p);
18926 prefix_attributes = declspecs.attributes;
18927 declspecs.attributes = NULL_TREE;
18929 /* Keep going until we hit the `;' at the end of the
18931 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18933 tree width = NULL_TREE, attributes, first_attribute, decl;
18934 cp_declarator *declarator = NULL;
18935 int ctor_dtor_or_conv_p;
18937 /* Check for a (possibly unnamed) bitfield declaration. */
18938 token = cp_lexer_peek_token (parser->lexer);
18939 if (token->type == CPP_COLON)
18942 if (token->type == CPP_NAME
18943 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18946 /* Get the name of the bitfield. */
18947 declarator = make_id_declarator (NULL_TREE,
18948 cp_parser_identifier (parser),
18952 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18953 /* Get the width of the bitfield. */
18955 = cp_parser_constant_expression (parser,
18956 /*allow_non_constant=*/false,
18961 /* Parse the declarator. */
18963 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18964 &ctor_dtor_or_conv_p,
18965 /*parenthesized_p=*/NULL,
18966 /*member_p=*/false);
18969 /* Look for attributes that apply to the ivar. */
18970 attributes = cp_parser_attributes_opt (parser);
18971 /* Remember which attributes are prefix attributes and
18973 first_attribute = attributes;
18974 /* Combine the attributes. */
18975 attributes = chainon (prefix_attributes, attributes);
18979 /* Create the bitfield declaration. */
18980 decl = grokbitfield (declarator, &declspecs, width);
18981 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18984 decl = grokfield (declarator, &declspecs,
18985 NULL_TREE, /*init_const_expr_p=*/false,
18986 NULL_TREE, attributes);
18988 /* Add the instance variable. */
18989 objc_add_instance_variable (decl);
18991 /* Reset PREFIX_ATTRIBUTES. */
18992 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18993 attributes = TREE_CHAIN (attributes);
18995 TREE_CHAIN (attributes) = NULL_TREE;
18997 token = cp_lexer_peek_token (parser->lexer);
18999 if (token->type == CPP_COMMA)
19001 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
19007 cp_parser_consume_semicolon_at_end_of_statement (parser);
19008 token = cp_lexer_peek_token (parser->lexer);
19011 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
19012 /* For historical reasons, we accept an optional semicolon. */
19013 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19014 cp_lexer_consume_token (parser->lexer);
19017 /* Parse an Objective-C protocol declaration. */
19020 cp_parser_objc_protocol_declaration (cp_parser* parser)
19022 tree proto, protorefs;
19025 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
19026 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
19028 error ("identifier expected after %<@protocol%>");
19032 /* See if we have a forward declaration or a definition. */
19033 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
19035 /* Try a forward declaration first. */
19036 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
19038 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
19040 cp_parser_consume_semicolon_at_end_of_statement (parser);
19043 /* Ok, we got a full-fledged definition (or at least should). */
19046 proto = cp_parser_identifier (parser);
19047 protorefs = cp_parser_objc_protocol_refs_opt (parser);
19048 objc_start_protocol (proto, protorefs);
19049 cp_parser_objc_method_prototype_list (parser);
19053 /* Parse an Objective-C superclass or category. */
19056 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
19059 cp_token *next = cp_lexer_peek_token (parser->lexer);
19061 *super = *categ = NULL_TREE;
19062 if (next->type == CPP_COLON)
19064 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
19065 *super = cp_parser_identifier (parser);
19067 else if (next->type == CPP_OPEN_PAREN)
19069 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
19070 *categ = cp_parser_identifier (parser);
19071 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19075 /* Parse an Objective-C class interface. */
19078 cp_parser_objc_class_interface (cp_parser* parser)
19080 tree name, super, categ, protos;
19082 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
19083 name = cp_parser_identifier (parser);
19084 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19085 protos = cp_parser_objc_protocol_refs_opt (parser);
19087 /* We have either a class or a category on our hands. */
19089 objc_start_category_interface (name, categ, protos);
19092 objc_start_class_interface (name, super, protos);
19093 /* Handle instance variable declarations, if any. */
19094 cp_parser_objc_class_ivars (parser);
19095 objc_continue_interface ();
19098 cp_parser_objc_method_prototype_list (parser);
19101 /* Parse an Objective-C class implementation. */
19104 cp_parser_objc_class_implementation (cp_parser* parser)
19106 tree name, super, categ;
19108 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
19109 name = cp_parser_identifier (parser);
19110 cp_parser_objc_superclass_or_category (parser, &super, &categ);
19112 /* We have either a class or a category on our hands. */
19114 objc_start_category_implementation (name, categ);
19117 objc_start_class_implementation (name, super);
19118 /* Handle instance variable declarations, if any. */
19119 cp_parser_objc_class_ivars (parser);
19120 objc_continue_implementation ();
19123 cp_parser_objc_method_definition_list (parser);
19126 /* Consume the @end token and finish off the implementation. */
19129 cp_parser_objc_end_implementation (cp_parser* parser)
19131 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
19132 objc_finish_implementation ();
19135 /* Parse an Objective-C declaration. */
19138 cp_parser_objc_declaration (cp_parser* parser)
19140 /* Try to figure out what kind of declaration is present. */
19141 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19143 switch (kwd->keyword)
19146 cp_parser_objc_alias_declaration (parser);
19149 cp_parser_objc_class_declaration (parser);
19151 case RID_AT_PROTOCOL:
19152 cp_parser_objc_protocol_declaration (parser);
19154 case RID_AT_INTERFACE:
19155 cp_parser_objc_class_interface (parser);
19157 case RID_AT_IMPLEMENTATION:
19158 cp_parser_objc_class_implementation (parser);
19161 cp_parser_objc_end_implementation (parser);
19164 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19165 cp_parser_skip_to_end_of_block_or_statement (parser);
19169 /* Parse an Objective-C try-catch-finally statement.
19171 objc-try-catch-finally-stmt:
19172 @try compound-statement objc-catch-clause-seq [opt]
19173 objc-finally-clause [opt]
19175 objc-catch-clause-seq:
19176 objc-catch-clause objc-catch-clause-seq [opt]
19179 @catch ( exception-declaration ) compound-statement
19181 objc-finally-clause
19182 @finally compound-statement
19184 Returns NULL_TREE. */
19187 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19188 location_t location;
19191 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19192 location = cp_lexer_peek_token (parser->lexer)->location;
19193 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19194 node, lest it get absorbed into the surrounding block. */
19195 stmt = push_stmt_list ();
19196 cp_parser_compound_statement (parser, NULL, false);
19197 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19199 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19201 cp_parameter_declarator *parmdecl;
19204 cp_lexer_consume_token (parser->lexer);
19205 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19206 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19207 parm = grokdeclarator (parmdecl->declarator,
19208 &parmdecl->decl_specifiers,
19209 PARM, /*initialized=*/0,
19210 /*attrlist=*/NULL);
19211 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19212 objc_begin_catch_clause (parm);
19213 cp_parser_compound_statement (parser, NULL, false);
19214 objc_finish_catch_clause ();
19217 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19219 cp_lexer_consume_token (parser->lexer);
19220 location = cp_lexer_peek_token (parser->lexer)->location;
19221 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19222 node, lest it get absorbed into the surrounding block. */
19223 stmt = push_stmt_list ();
19224 cp_parser_compound_statement (parser, NULL, false);
19225 objc_build_finally_clause (location, pop_stmt_list (stmt));
19228 return objc_finish_try_stmt ();
19231 /* Parse an Objective-C synchronized statement.
19233 objc-synchronized-stmt:
19234 @synchronized ( expression ) compound-statement
19236 Returns NULL_TREE. */
19239 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19240 location_t location;
19243 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19245 location = cp_lexer_peek_token (parser->lexer)->location;
19246 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19247 lock = cp_parser_expression (parser, false);
19248 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19250 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19251 node, lest it get absorbed into the surrounding block. */
19252 stmt = push_stmt_list ();
19253 cp_parser_compound_statement (parser, NULL, false);
19255 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19258 /* Parse an Objective-C throw statement.
19261 @throw assignment-expression [opt] ;
19263 Returns a constructed '@throw' statement. */
19266 cp_parser_objc_throw_statement (cp_parser *parser) {
19267 tree expr = NULL_TREE;
19269 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19271 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19272 expr = cp_parser_assignment_expression (parser, false);
19274 cp_parser_consume_semicolon_at_end_of_statement (parser);
19276 return objc_build_throw_stmt (expr);
19279 /* Parse an Objective-C statement. */
19282 cp_parser_objc_statement (cp_parser * parser) {
19283 /* Try to figure out what kind of declaration is present. */
19284 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19286 switch (kwd->keyword)
19289 return cp_parser_objc_try_catch_finally_statement (parser);
19290 case RID_AT_SYNCHRONIZED:
19291 return cp_parser_objc_synchronized_statement (parser);
19293 return cp_parser_objc_throw_statement (parser);
19295 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19296 cp_parser_skip_to_end_of_block_or_statement (parser);
19299 return error_mark_node;
19302 /* OpenMP 2.5 parsing routines. */
19304 /* Returns name of the next clause.
19305 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19306 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19307 returned and the token is consumed. */
19309 static pragma_omp_clause
19310 cp_parser_omp_clause_name (cp_parser *parser)
19312 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19314 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19315 result = PRAGMA_OMP_CLAUSE_IF;
19316 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19317 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19318 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19319 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19320 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19322 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19323 const char *p = IDENTIFIER_POINTER (id);
19328 if (!strcmp ("copyin", p))
19329 result = PRAGMA_OMP_CLAUSE_COPYIN;
19330 else if (!strcmp ("copyprivate", p))
19331 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19334 if (!strcmp ("firstprivate", p))
19335 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19338 if (!strcmp ("lastprivate", p))
19339 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19342 if (!strcmp ("nowait", p))
19343 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19344 else if (!strcmp ("num_threads", p))
19345 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19348 if (!strcmp ("ordered", p))
19349 result = PRAGMA_OMP_CLAUSE_ORDERED;
19352 if (!strcmp ("reduction", p))
19353 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19356 if (!strcmp ("schedule", p))
19357 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19358 else if (!strcmp ("shared", p))
19359 result = PRAGMA_OMP_CLAUSE_SHARED;
19364 if (result != PRAGMA_OMP_CLAUSE_NONE)
19365 cp_lexer_consume_token (parser->lexer);
19370 /* Validate that a clause of the given type does not already exist. */
19373 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19377 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19378 if (OMP_CLAUSE_CODE (c) == code)
19380 error ("too many %qs clauses", name);
19388 variable-list , identifier
19390 In addition, we match a closing parenthesis. An opening parenthesis
19391 will have been consumed by the caller.
19393 If KIND is nonzero, create the appropriate node and install the decl
19394 in OMP_CLAUSE_DECL and add the node to the head of the list.
19396 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19397 return the list created. */
19400 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19407 name = cp_parser_id_expression (parser, /*template_p=*/false,
19408 /*check_dependency_p=*/true,
19409 /*template_p=*/NULL,
19410 /*declarator_p=*/false,
19411 /*optional_p=*/false);
19412 if (name == error_mark_node)
19415 decl = cp_parser_lookup_name_simple (parser, name);
19416 if (decl == error_mark_node)
19417 cp_parser_name_lookup_error (parser, name, decl, NULL);
19418 else if (kind != 0)
19420 tree u = build_omp_clause (kind);
19421 OMP_CLAUSE_DECL (u) = decl;
19422 OMP_CLAUSE_CHAIN (u) = list;
19426 list = tree_cons (decl, NULL_TREE, list);
19429 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19431 cp_lexer_consume_token (parser->lexer);
19434 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19438 /* Try to resync to an unnested comma. Copied from
19439 cp_parser_parenthesized_expression_list. */
19441 ending = cp_parser_skip_to_closing_parenthesis (parser,
19442 /*recovering=*/true,
19444 /*consume_paren=*/true);
19452 /* Similarly, but expect leading and trailing parenthesis. This is a very
19453 common case for omp clauses. */
19456 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19458 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19459 return cp_parser_omp_var_list_no_open (parser, kind, list);
19464 default ( shared | none ) */
19467 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19469 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19472 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19474 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19476 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19477 const char *p = IDENTIFIER_POINTER (id);
19482 if (strcmp ("none", p) != 0)
19484 kind = OMP_CLAUSE_DEFAULT_NONE;
19488 if (strcmp ("shared", p) != 0)
19490 kind = OMP_CLAUSE_DEFAULT_SHARED;
19497 cp_lexer_consume_token (parser->lexer);
19502 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19505 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19506 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19507 /*or_comma=*/false,
19508 /*consume_paren=*/true);
19510 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19513 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19514 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19515 OMP_CLAUSE_CHAIN (c) = list;
19516 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19522 if ( expression ) */
19525 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19529 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19532 t = cp_parser_condition (parser);
19534 if (t == error_mark_node
19535 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19536 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19537 /*or_comma=*/false,
19538 /*consume_paren=*/true);
19540 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19542 c = build_omp_clause (OMP_CLAUSE_IF);
19543 OMP_CLAUSE_IF_EXPR (c) = t;
19544 OMP_CLAUSE_CHAIN (c) = list;
19553 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19557 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19559 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19560 OMP_CLAUSE_CHAIN (c) = list;
19565 num_threads ( expression ) */
19568 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19572 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19575 t = cp_parser_expression (parser, false);
19577 if (t == error_mark_node
19578 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19579 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19580 /*or_comma=*/false,
19581 /*consume_paren=*/true);
19583 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19585 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19586 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19587 OMP_CLAUSE_CHAIN (c) = list;
19596 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19600 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19602 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19603 OMP_CLAUSE_CHAIN (c) = list;
19608 reduction ( reduction-operator : variable-list )
19610 reduction-operator:
19611 One of: + * - & ^ | && || */
19614 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19616 enum tree_code code;
19619 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19622 switch (cp_lexer_peek_token (parser->lexer)->type)
19634 code = BIT_AND_EXPR;
19637 code = BIT_XOR_EXPR;
19640 code = BIT_IOR_EXPR;
19643 code = TRUTH_ANDIF_EXPR;
19646 code = TRUTH_ORIF_EXPR;
19649 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19651 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19652 /*or_comma=*/false,
19653 /*consume_paren=*/true);
19656 cp_lexer_consume_token (parser->lexer);
19658 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19661 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19662 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19663 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19669 schedule ( schedule-kind )
19670 schedule ( schedule-kind , expression )
19673 static | dynamic | guided | runtime */
19676 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19680 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19683 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19685 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19687 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19688 const char *p = IDENTIFIER_POINTER (id);
19693 if (strcmp ("dynamic", p) != 0)
19695 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19699 if (strcmp ("guided", p) != 0)
19701 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19705 if (strcmp ("runtime", p) != 0)
19707 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19714 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19715 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19718 cp_lexer_consume_token (parser->lexer);
19720 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19722 cp_lexer_consume_token (parser->lexer);
19724 t = cp_parser_assignment_expression (parser, false);
19726 if (t == error_mark_node)
19728 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19729 error ("schedule %<runtime%> does not take "
19730 "a %<chunk_size%> parameter");
19732 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19734 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19737 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19740 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19741 OMP_CLAUSE_CHAIN (c) = list;
19745 cp_parser_error (parser, "invalid schedule kind");
19747 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19748 /*or_comma=*/false,
19749 /*consume_paren=*/true);
19753 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19754 is a bitmask in MASK. Return the list of clauses found; the result
19755 of clause default goes in *pdefault. */
19758 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19759 const char *where, cp_token *pragma_tok)
19761 tree clauses = NULL;
19764 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19766 pragma_omp_clause c_kind;
19767 const char *c_name;
19768 tree prev = clauses;
19770 if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19771 cp_lexer_consume_token (parser->lexer);
19773 c_kind = cp_parser_omp_clause_name (parser);
19778 case PRAGMA_OMP_CLAUSE_COPYIN:
19779 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19782 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19783 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19785 c_name = "copyprivate";
19787 case PRAGMA_OMP_CLAUSE_DEFAULT:
19788 clauses = cp_parser_omp_clause_default (parser, clauses);
19789 c_name = "default";
19791 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19792 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19794 c_name = "firstprivate";
19796 case PRAGMA_OMP_CLAUSE_IF:
19797 clauses = cp_parser_omp_clause_if (parser, clauses);
19800 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19801 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19803 c_name = "lastprivate";
19805 case PRAGMA_OMP_CLAUSE_NOWAIT:
19806 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19809 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19810 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19811 c_name = "num_threads";
19813 case PRAGMA_OMP_CLAUSE_ORDERED:
19814 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19815 c_name = "ordered";
19817 case PRAGMA_OMP_CLAUSE_PRIVATE:
19818 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19820 c_name = "private";
19822 case PRAGMA_OMP_CLAUSE_REDUCTION:
19823 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19824 c_name = "reduction";
19826 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19827 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19828 c_name = "schedule";
19830 case PRAGMA_OMP_CLAUSE_SHARED:
19831 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19836 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19840 if (((mask >> c_kind) & 1) == 0)
19842 /* Remove the invalid clause(s) from the list to avoid
19843 confusing the rest of the compiler. */
19845 error ("%qs is not valid for %qs", c_name, where);
19849 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19850 return finish_omp_clauses (clauses);
19857 In practice, we're also interested in adding the statement to an
19858 outer node. So it is convenient if we work around the fact that
19859 cp_parser_statement calls add_stmt. */
19862 cp_parser_begin_omp_structured_block (cp_parser *parser)
19864 unsigned save = parser->in_statement;
19866 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19867 This preserves the "not within loop or switch" style error messages
19868 for nonsense cases like
19874 if (parser->in_statement)
19875 parser->in_statement = IN_OMP_BLOCK;
19881 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19883 parser->in_statement = save;
19887 cp_parser_omp_structured_block (cp_parser *parser)
19889 tree stmt = begin_omp_structured_block ();
19890 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19892 cp_parser_statement (parser, NULL_TREE, false, NULL);
19894 cp_parser_end_omp_structured_block (parser, save);
19895 return finish_omp_structured_block (stmt);
19899 # pragma omp atomic new-line
19903 x binop= expr | x++ | ++x | x-- | --x
19905 +, *, -, /, &, ^, |, <<, >>
19907 where x is an lvalue expression with scalar type. */
19910 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19913 enum tree_code code;
19915 cp_parser_require_pragma_eol (parser, pragma_tok);
19917 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19919 switch (TREE_CODE (lhs))
19924 case PREINCREMENT_EXPR:
19925 case POSTINCREMENT_EXPR:
19926 lhs = TREE_OPERAND (lhs, 0);
19928 rhs = integer_one_node;
19931 case PREDECREMENT_EXPR:
19932 case POSTDECREMENT_EXPR:
19933 lhs = TREE_OPERAND (lhs, 0);
19935 rhs = integer_one_node;
19939 switch (cp_lexer_peek_token (parser->lexer)->type)
19945 code = TRUNC_DIV_EXPR;
19953 case CPP_LSHIFT_EQ:
19954 code = LSHIFT_EXPR;
19956 case CPP_RSHIFT_EQ:
19957 code = RSHIFT_EXPR;
19960 code = BIT_AND_EXPR;
19963 code = BIT_IOR_EXPR;
19966 code = BIT_XOR_EXPR;
19969 cp_parser_error (parser,
19970 "invalid operator for %<#pragma omp atomic%>");
19973 cp_lexer_consume_token (parser->lexer);
19975 rhs = cp_parser_expression (parser, false);
19976 if (rhs == error_mark_node)
19980 finish_omp_atomic (code, lhs, rhs);
19981 cp_parser_consume_semicolon_at_end_of_statement (parser);
19985 cp_parser_skip_to_end_of_block_or_statement (parser);
19990 # pragma omp barrier new-line */
19993 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19995 cp_parser_require_pragma_eol (parser, pragma_tok);
19996 finish_omp_barrier ();
20000 # pragma omp critical [(name)] new-line
20001 structured-block */
20004 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
20006 tree stmt, name = NULL;
20008 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20010 cp_lexer_consume_token (parser->lexer);
20012 name = cp_parser_identifier (parser);
20014 if (name == error_mark_node
20015 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20016 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20017 /*or_comma=*/false,
20018 /*consume_paren=*/true);
20019 if (name == error_mark_node)
20022 cp_parser_require_pragma_eol (parser, pragma_tok);
20024 stmt = cp_parser_omp_structured_block (parser);
20025 return c_finish_omp_critical (stmt, name);
20029 # pragma omp flush flush-vars[opt] new-line
20032 ( variable-list ) */
20035 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
20037 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20038 (void) cp_parser_omp_var_list (parser, 0, NULL);
20039 cp_parser_require_pragma_eol (parser, pragma_tok);
20041 finish_omp_flush ();
20044 /* Parse the restricted form of the for statment allowed by OpenMP. */
20047 cp_parser_omp_for_loop (cp_parser *parser)
20049 tree init, cond, incr, body, decl, pre_body;
20052 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20054 cp_parser_error (parser, "for statement expected");
20057 loc = cp_lexer_consume_token (parser->lexer)->location;
20058 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
20061 init = decl = NULL;
20062 pre_body = push_stmt_list ();
20063 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20065 cp_decl_specifier_seq type_specifiers;
20067 /* First, try to parse as an initialized declaration. See
20068 cp_parser_condition, from whence the bulk of this is copied. */
20070 cp_parser_parse_tentatively (parser);
20071 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
20073 if (!cp_parser_error_occurred (parser))
20075 tree asm_specification, attributes;
20076 cp_declarator *declarator;
20078 declarator = cp_parser_declarator (parser,
20079 CP_PARSER_DECLARATOR_NAMED,
20080 /*ctor_dtor_or_conv_p=*/NULL,
20081 /*parenthesized_p=*/NULL,
20082 /*member_p=*/false);
20083 attributes = cp_parser_attributes_opt (parser);
20084 asm_specification = cp_parser_asm_specification_opt (parser);
20086 cp_parser_require (parser, CPP_EQ, "`='");
20087 if (cp_parser_parse_definitely (parser))
20091 decl = start_decl (declarator, &type_specifiers,
20092 /*initialized_p=*/false, attributes,
20093 /*prefix_attributes=*/NULL_TREE,
20096 init = cp_parser_assignment_expression (parser, false);
20098 if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
20099 init = error_mark_node;
20101 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
20102 asm_specification, LOOKUP_ONLYCONVERTING);
20105 pop_scope (pushed_scope);
20109 cp_parser_abort_tentative_parse (parser);
20111 /* If parsing as an initialized declaration failed, try again as
20112 a simple expression. */
20114 init = cp_parser_expression (parser, false);
20116 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20117 pre_body = pop_stmt_list (pre_body);
20120 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20121 cond = cp_parser_condition (parser);
20122 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
20125 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20126 incr = cp_parser_expression (parser, false);
20128 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
20129 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
20130 /*or_comma=*/false,
20131 /*consume_paren=*/true);
20133 /* Note that we saved the original contents of this flag when we entered
20134 the structured block, and so we don't need to re-save it here. */
20135 parser->in_statement = IN_OMP_FOR;
20137 /* Note that the grammar doesn't call for a structured block here,
20138 though the loop as a whole is a structured block. */
20139 body = push_stmt_list ();
20140 cp_parser_statement (parser, NULL_TREE, false, NULL);
20141 body = pop_stmt_list (body);
20143 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
20147 #pragma omp for for-clause[optseq] new-line
20150 #define OMP_FOR_CLAUSE_MASK \
20151 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20152 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20153 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20154 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20155 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20156 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20157 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20160 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20162 tree clauses, sb, ret;
20165 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20166 "#pragma omp for", pragma_tok);
20168 sb = begin_omp_structured_block ();
20169 save = cp_parser_begin_omp_structured_block (parser);
20171 ret = cp_parser_omp_for_loop (parser);
20173 OMP_FOR_CLAUSES (ret) = clauses;
20175 cp_parser_end_omp_structured_block (parser, save);
20176 add_stmt (finish_omp_structured_block (sb));
20182 # pragma omp master new-line
20183 structured-block */
20186 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20188 cp_parser_require_pragma_eol (parser, pragma_tok);
20189 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20193 # pragma omp ordered new-line
20194 structured-block */
20197 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20199 cp_parser_require_pragma_eol (parser, pragma_tok);
20200 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20206 { section-sequence }
20209 section-directive[opt] structured-block
20210 section-sequence section-directive structured-block */
20213 cp_parser_omp_sections_scope (cp_parser *parser)
20215 tree stmt, substmt;
20216 bool error_suppress = false;
20219 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20222 stmt = push_stmt_list ();
20224 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20228 substmt = begin_omp_structured_block ();
20229 save = cp_parser_begin_omp_structured_block (parser);
20233 cp_parser_statement (parser, NULL_TREE, false, NULL);
20235 tok = cp_lexer_peek_token (parser->lexer);
20236 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20238 if (tok->type == CPP_CLOSE_BRACE)
20240 if (tok->type == CPP_EOF)
20244 cp_parser_end_omp_structured_block (parser, save);
20245 substmt = finish_omp_structured_block (substmt);
20246 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20247 add_stmt (substmt);
20252 tok = cp_lexer_peek_token (parser->lexer);
20253 if (tok->type == CPP_CLOSE_BRACE)
20255 if (tok->type == CPP_EOF)
20258 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20260 cp_lexer_consume_token (parser->lexer);
20261 cp_parser_require_pragma_eol (parser, tok);
20262 error_suppress = false;
20264 else if (!error_suppress)
20266 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20267 error_suppress = true;
20270 substmt = cp_parser_omp_structured_block (parser);
20271 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20272 add_stmt (substmt);
20274 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20276 substmt = pop_stmt_list (stmt);
20278 stmt = make_node (OMP_SECTIONS);
20279 TREE_TYPE (stmt) = void_type_node;
20280 OMP_SECTIONS_BODY (stmt) = substmt;
20287 # pragma omp sections sections-clause[optseq] newline
20290 #define OMP_SECTIONS_CLAUSE_MASK \
20291 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20292 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20293 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20294 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20295 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20298 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20302 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20303 "#pragma omp sections", pragma_tok);
20305 ret = cp_parser_omp_sections_scope (parser);
20307 OMP_SECTIONS_CLAUSES (ret) = clauses;
20313 # pragma parallel parallel-clause new-line
20314 # pragma parallel for parallel-for-clause new-line
20315 # pragma parallel sections parallel-sections-clause new-line */
20317 #define OMP_PARALLEL_CLAUSE_MASK \
20318 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20319 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20320 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20321 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20322 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20323 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20324 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20325 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20328 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20330 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20331 const char *p_name = "#pragma omp parallel";
20332 tree stmt, clauses, par_clause, ws_clause, block;
20333 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20336 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20338 cp_lexer_consume_token (parser->lexer);
20339 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20340 p_name = "#pragma omp parallel for";
20341 mask |= OMP_FOR_CLAUSE_MASK;
20342 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20344 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20346 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20347 const char *p = IDENTIFIER_POINTER (id);
20348 if (strcmp (p, "sections") == 0)
20350 cp_lexer_consume_token (parser->lexer);
20351 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20352 p_name = "#pragma omp parallel sections";
20353 mask |= OMP_SECTIONS_CLAUSE_MASK;
20354 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20358 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20359 block = begin_omp_parallel ();
20360 save = cp_parser_begin_omp_structured_block (parser);
20364 case PRAGMA_OMP_PARALLEL:
20365 cp_parser_statement (parser, NULL_TREE, false, NULL);
20366 par_clause = clauses;
20369 case PRAGMA_OMP_PARALLEL_FOR:
20370 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20371 stmt = cp_parser_omp_for_loop (parser);
20373 OMP_FOR_CLAUSES (stmt) = ws_clause;
20376 case PRAGMA_OMP_PARALLEL_SECTIONS:
20377 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20378 stmt = cp_parser_omp_sections_scope (parser);
20380 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20384 gcc_unreachable ();
20387 cp_parser_end_omp_structured_block (parser, save);
20388 stmt = finish_omp_parallel (par_clause, block);
20389 if (p_kind != PRAGMA_OMP_PARALLEL)
20390 OMP_PARALLEL_COMBINED (stmt) = 1;
20395 # pragma omp single single-clause[optseq] new-line
20396 structured-block */
20398 #define OMP_SINGLE_CLAUSE_MASK \
20399 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20400 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20401 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20402 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20405 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20407 tree stmt = make_node (OMP_SINGLE);
20408 TREE_TYPE (stmt) = void_type_node;
20410 OMP_SINGLE_CLAUSES (stmt)
20411 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20412 "#pragma omp single", pragma_tok);
20413 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20415 return add_stmt (stmt);
20419 # pragma omp threadprivate (variable-list) */
20422 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20426 vars = cp_parser_omp_var_list (parser, 0, NULL);
20427 cp_parser_require_pragma_eol (parser, pragma_tok);
20429 finish_omp_threadprivate (vars);
20432 /* Main entry point to OpenMP statement pragmas. */
20435 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20439 switch (pragma_tok->pragma_kind)
20441 case PRAGMA_OMP_ATOMIC:
20442 cp_parser_omp_atomic (parser, pragma_tok);
20444 case PRAGMA_OMP_CRITICAL:
20445 stmt = cp_parser_omp_critical (parser, pragma_tok);
20447 case PRAGMA_OMP_FOR:
20448 stmt = cp_parser_omp_for (parser, pragma_tok);
20450 case PRAGMA_OMP_MASTER:
20451 stmt = cp_parser_omp_master (parser, pragma_tok);
20453 case PRAGMA_OMP_ORDERED:
20454 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20456 case PRAGMA_OMP_PARALLEL:
20457 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20459 case PRAGMA_OMP_SECTIONS:
20460 stmt = cp_parser_omp_sections (parser, pragma_tok);
20462 case PRAGMA_OMP_SINGLE:
20463 stmt = cp_parser_omp_single (parser, pragma_tok);
20466 gcc_unreachable ();
20470 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20475 static GTY (()) cp_parser *the_parser;
20478 /* Special handling for the first token or line in the file. The first
20479 thing in the file might be #pragma GCC pch_preprocess, which loads a
20480 PCH file, which is a GC collection point. So we need to handle this
20481 first pragma without benefit of an existing lexer structure.
20483 Always returns one token to the caller in *FIRST_TOKEN. This is
20484 either the true first token of the file, or the first token after
20485 the initial pragma. */
20488 cp_parser_initial_pragma (cp_token *first_token)
20492 cp_lexer_get_preprocessor_token (NULL, first_token);
20493 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20496 cp_lexer_get_preprocessor_token (NULL, first_token);
20497 if (first_token->type == CPP_STRING)
20499 name = first_token->u.value;
20501 cp_lexer_get_preprocessor_token (NULL, first_token);
20502 if (first_token->type != CPP_PRAGMA_EOL)
20503 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20506 error ("expected string literal");
20508 /* Skip to the end of the pragma. */
20509 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20510 cp_lexer_get_preprocessor_token (NULL, first_token);
20512 /* Now actually load the PCH file. */
20514 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20516 /* Read one more token to return to our caller. We have to do this
20517 after reading the PCH file in, since its pointers have to be
20519 cp_lexer_get_preprocessor_token (NULL, first_token);
20522 /* Normal parsing of a pragma token. Here we can (and must) use the
20526 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20528 cp_token *pragma_tok;
20531 pragma_tok = cp_lexer_consume_token (parser->lexer);
20532 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20533 parser->lexer->in_pragma = true;
20535 id = pragma_tok->pragma_kind;
20538 case PRAGMA_GCC_PCH_PREPROCESS:
20539 error ("%<#pragma GCC pch_preprocess%> must be first");
20542 case PRAGMA_OMP_BARRIER:
20545 case pragma_compound:
20546 cp_parser_omp_barrier (parser, pragma_tok);
20549 error ("%<#pragma omp barrier%> may only be "
20550 "used in compound statements");
20557 case PRAGMA_OMP_FLUSH:
20560 case pragma_compound:
20561 cp_parser_omp_flush (parser, pragma_tok);
20564 error ("%<#pragma omp flush%> may only be "
20565 "used in compound statements");
20572 case PRAGMA_OMP_THREADPRIVATE:
20573 cp_parser_omp_threadprivate (parser, pragma_tok);
20576 case PRAGMA_OMP_ATOMIC:
20577 case PRAGMA_OMP_CRITICAL:
20578 case PRAGMA_OMP_FOR:
20579 case PRAGMA_OMP_MASTER:
20580 case PRAGMA_OMP_ORDERED:
20581 case PRAGMA_OMP_PARALLEL:
20582 case PRAGMA_OMP_SECTIONS:
20583 case PRAGMA_OMP_SINGLE:
20584 if (context == pragma_external)
20586 cp_parser_omp_construct (parser, pragma_tok);
20589 case PRAGMA_OMP_SECTION:
20590 error ("%<#pragma omp section%> may only be used in "
20591 "%<#pragma omp sections%> construct");
20595 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20596 c_invoke_pragma_handler (id);
20600 cp_parser_error (parser, "expected declaration specifiers");
20604 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20608 /* The interface the pragma parsers have to the lexer. */
20611 pragma_lex (tree *value)
20614 enum cpp_ttype ret;
20616 tok = cp_lexer_peek_token (the_parser->lexer);
20619 *value = tok->u.value;
20621 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20623 else if (ret == CPP_STRING)
20624 *value = cp_parser_string_literal (the_parser, false, false);
20627 cp_lexer_consume_token (the_parser->lexer);
20628 if (ret == CPP_KEYWORD)
20636 /* External interface. */
20638 /* Parse one entire translation unit. */
20641 c_parse_file (void)
20643 bool error_occurred;
20644 static bool already_called = false;
20646 if (already_called)
20648 sorry ("inter-module optimizations not implemented for C++");
20651 already_called = true;
20653 the_parser = cp_parser_new ();
20654 push_deferring_access_checks (flag_access_control
20655 ? dk_no_deferred : dk_no_check);
20656 error_occurred = cp_parser_translation_unit (the_parser);
20660 #include "gt-cp-parser.h"