2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005 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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
25 #include "coretypes.h"
27 #include "dyn-string.h"
35 #include "diagnostic.h"
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46 and c-lex.c) and the C++ parser. */
48 /* A token's value and its associated deferred access checks and
51 struct tree_check GTY(())
53 /* The value associated with the token. */
55 /* The checks that have been associated with value. */
56 VEC (deferred_access_check, gc)* checks;
57 /* The token's qualifying scope (used when it is a
58 CPP_NESTED_NAME_SPECIFIER). */
59 tree qualifying_scope;
64 typedef struct cp_token GTY (())
66 /* The kind of token. */
67 ENUM_BITFIELD (cpp_ttype) type : 8;
68 /* If this token is a keyword, this value indicates which keyword.
69 Otherwise, this value is RID_MAX. */
70 ENUM_BITFIELD (rid) keyword : 8;
73 /* Identifier for the pragma. */
74 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
75 /* True if this token is from a system header. */
76 BOOL_BITFIELD in_system_header : 1;
77 /* True if this token is from a context where it is implicitly extern "C" */
78 BOOL_BITFIELD implicit_extern_c : 1;
79 /* True for a CPP_NAME token that is not a keyword (i.e., for which
80 KEYWORD is RID_MAX) iff this name was looked up and found to be
81 ambiguous. An error has already been reported. */
82 BOOL_BITFIELD ambiguous_p : 1;
83 /* The input file stack index at which this token was found. */
84 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
85 /* The value associated with this token, if any. */
86 union cp_token_value {
87 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
88 struct tree_check* GTY((tag ("1"))) tree_check_value;
89 /* Use for all other tokens. */
90 tree GTY((tag ("0"))) value;
91 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
92 /* The location at which this token was found. */
96 /* We use a stack of token pointer for saving token sets. */
97 typedef struct cp_token *cp_token_position;
98 DEF_VEC_P (cp_token_position);
99 DEF_VEC_ALLOC_P (cp_token_position,heap);
101 static const cp_token eof_token =
103 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
104 #if USE_MAPPED_LOCATION
111 /* The cp_lexer structure represents the C++ lexer. It is responsible
112 for managing the token stream from the preprocessor and supplying
113 it to the parser. Tokens are never added to the cp_lexer after
116 typedef struct cp_lexer GTY (())
118 /* The memory allocated for the buffer. NULL if this lexer does not
119 own the token buffer. */
120 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
121 /* If the lexer owns the buffer, this is the number of tokens in the
123 size_t buffer_length;
125 /* A pointer just past the last available token. The tokens
126 in this lexer are [buffer, last_token). */
127 cp_token_position GTY ((skip)) last_token;
129 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
130 no more available tokens. */
131 cp_token_position GTY ((skip)) next_token;
133 /* A stack indicating positions at which cp_lexer_save_tokens was
134 called. The top entry is the most recent position at which we
135 began saving tokens. If the stack is non-empty, we are saving
137 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
139 /* The next lexer in a linked list of lexers. */
140 struct cp_lexer *next;
142 /* True if we should output debugging information. */
145 /* True if we're in the context of parsing a pragma, and should not
146 increment past the end-of-line marker. */
150 /* cp_token_cache is a range of tokens. There is no need to represent
151 allocate heap memory for it, since tokens are never removed from the
152 lexer's array. There is also no need for the GC to walk through
153 a cp_token_cache, since everything in here is referenced through
156 typedef struct cp_token_cache GTY(())
158 /* The beginning of the token range. */
159 cp_token * GTY((skip)) first;
161 /* Points immediately after the last token in the range. */
162 cp_token * GTY ((skip)) last;
167 static cp_lexer *cp_lexer_new_main
169 static cp_lexer *cp_lexer_new_from_tokens
170 (cp_token_cache *tokens);
171 static void cp_lexer_destroy
173 static int cp_lexer_saving_tokens
175 static cp_token_position cp_lexer_token_position
177 static cp_token *cp_lexer_token_at
178 (cp_lexer *, cp_token_position);
179 static void cp_lexer_get_preprocessor_token
180 (cp_lexer *, cp_token *);
181 static inline cp_token *cp_lexer_peek_token
183 static cp_token *cp_lexer_peek_nth_token
184 (cp_lexer *, size_t);
185 static inline bool cp_lexer_next_token_is
186 (cp_lexer *, enum cpp_ttype);
187 static bool cp_lexer_next_token_is_not
188 (cp_lexer *, enum cpp_ttype);
189 static bool cp_lexer_next_token_is_keyword
190 (cp_lexer *, enum rid);
191 static cp_token *cp_lexer_consume_token
193 static void cp_lexer_purge_token
195 static void cp_lexer_purge_tokens_after
196 (cp_lexer *, cp_token_position);
197 static void cp_lexer_save_tokens
199 static void cp_lexer_commit_tokens
201 static void cp_lexer_rollback_tokens
203 #ifdef ENABLE_CHECKING
204 static void cp_lexer_print_token
205 (FILE *, cp_token *);
206 static inline bool cp_lexer_debugging_p
208 static void cp_lexer_start_debugging
209 (cp_lexer *) ATTRIBUTE_UNUSED;
210 static void cp_lexer_stop_debugging
211 (cp_lexer *) ATTRIBUTE_UNUSED;
213 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
214 about passing NULL to functions that require non-NULL arguments
215 (fputs, fprintf). It will never be used, so all we need is a value
216 of the right type that's guaranteed not to be NULL. */
217 #define cp_lexer_debug_stream stdout
218 #define cp_lexer_print_token(str, tok) (void) 0
219 #define cp_lexer_debugging_p(lexer) 0
220 #endif /* ENABLE_CHECKING */
222 static cp_token_cache *cp_token_cache_new
223 (cp_token *, cp_token *);
225 static void cp_parser_initial_pragma
228 /* Manifest constants. */
229 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
230 #define CP_SAVED_TOKEN_STACK 5
232 /* A token type for keywords, as opposed to ordinary identifiers. */
233 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
235 /* A token type for template-ids. If a template-id is processed while
236 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
237 the value of the CPP_TEMPLATE_ID is whatever was returned by
238 cp_parser_template_id. */
239 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
241 /* A token type for nested-name-specifiers. If a
242 nested-name-specifier is processed while parsing tentatively, it is
243 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
244 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
245 cp_parser_nested_name_specifier_opt. */
246 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
248 /* A token type for tokens that are not tokens at all; these are used
249 to represent slots in the array where there used to be a token
250 that has now been deleted. */
251 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
253 /* The number of token types, including C++-specific ones. */
254 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
258 #ifdef ENABLE_CHECKING
259 /* The stream to which debugging output should be written. */
260 static FILE *cp_lexer_debug_stream;
261 #endif /* ENABLE_CHECKING */
263 /* Create a new main C++ lexer, the lexer that gets tokens from the
267 cp_lexer_new_main (void)
269 cp_token first_token;
276 /* It's possible that parsing the first pragma will load a PCH file,
277 which is a GC collection point. So we have to do that before
278 allocating any memory. */
279 cp_parser_initial_pragma (&first_token);
281 /* Tell c_lex_with_flags not to merge string constants. */
282 c_lex_return_raw_strings = true;
284 c_common_no_more_pch ();
286 /* Allocate the memory. */
287 lexer = GGC_CNEW (cp_lexer);
289 #ifdef ENABLE_CHECKING
290 /* Initially we are not debugging. */
291 lexer->debugging_p = false;
292 #endif /* ENABLE_CHECKING */
293 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
294 CP_SAVED_TOKEN_STACK);
296 /* Create the buffer. */
297 alloc = CP_LEXER_BUFFER_SIZE;
298 buffer = GGC_NEWVEC (cp_token, alloc);
300 /* Put the first token in the buffer. */
305 /* Get the remaining tokens from the preprocessor. */
306 while (pos->type != CPP_EOF)
313 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
314 pos = buffer + space;
316 cp_lexer_get_preprocessor_token (lexer, pos);
318 lexer->buffer = buffer;
319 lexer->buffer_length = alloc - space;
320 lexer->last_token = pos;
321 lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
323 /* Subsequent preprocessor diagnostics should use compiler
324 diagnostic functions to get the compiler source location. */
325 cpp_get_options (parse_in)->client_diagnostic = true;
326 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
328 gcc_assert (lexer->next_token->type != CPP_PURGED);
332 /* Create a new lexer whose token stream is primed with the tokens in
333 CACHE. When these tokens are exhausted, no new tokens will be read. */
336 cp_lexer_new_from_tokens (cp_token_cache *cache)
338 cp_token *first = cache->first;
339 cp_token *last = cache->last;
340 cp_lexer *lexer = GGC_CNEW (cp_lexer);
342 /* We do not own the buffer. */
343 lexer->buffer = NULL;
344 lexer->buffer_length = 0;
345 lexer->next_token = first == last ? (cp_token *)&eof_token : first;
346 lexer->last_token = last;
348 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
349 CP_SAVED_TOKEN_STACK);
351 #ifdef ENABLE_CHECKING
352 /* Initially we are not debugging. */
353 lexer->debugging_p = false;
356 gcc_assert (lexer->next_token->type != CPP_PURGED);
360 /* Frees all resources associated with LEXER. */
363 cp_lexer_destroy (cp_lexer *lexer)
366 ggc_free (lexer->buffer);
367 VEC_free (cp_token_position, heap, lexer->saved_tokens);
371 /* Returns nonzero if debugging information should be output. */
373 #ifdef ENABLE_CHECKING
376 cp_lexer_debugging_p (cp_lexer *lexer)
378 return lexer->debugging_p;
381 #endif /* ENABLE_CHECKING */
383 static inline cp_token_position
384 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
386 gcc_assert (!previous_p || lexer->next_token != &eof_token);
388 return lexer->next_token - previous_p;
391 static inline cp_token *
392 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
397 /* nonzero if we are presently saving tokens. */
400 cp_lexer_saving_tokens (const cp_lexer* lexer)
402 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
405 /* Store the next token from the preprocessor in *TOKEN. Return true
409 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
412 static int is_extern_c = 0;
414 /* Get a new token from the preprocessor. */
416 = c_lex_with_flags (&token->u.value, &token->location, &token->flags);
417 token->input_file_stack_index = input_file_stack_tick;
418 token->keyword = RID_MAX;
419 token->pragma_kind = PRAGMA_NONE;
420 token->in_system_header = in_system_header;
422 /* On some systems, some header files are surrounded by an
423 implicit extern "C" block. Set a flag in the token if it
424 comes from such a header. */
425 is_extern_c += pending_lang_change;
426 pending_lang_change = 0;
427 token->implicit_extern_c = is_extern_c > 0;
429 /* Check to see if this token is a keyword. */
430 if (token->type == CPP_NAME)
432 if (C_IS_RESERVED_WORD (token->u.value))
434 /* Mark this token as a keyword. */
435 token->type = CPP_KEYWORD;
436 /* Record which keyword. */
437 token->keyword = C_RID_CODE (token->u.value);
438 /* Update the value. Some keywords are mapped to particular
439 entities, rather than simply having the value of the
440 corresponding IDENTIFIER_NODE. For example, `__const' is
441 mapped to `const'. */
442 token->u.value = ridpointers[token->keyword];
446 if (warn_cxx0x_compat
447 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
448 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
450 /* Warn about the C++0x keyword (but still treat it as
452 warning (OPT_Wc__0x_compat,
453 "identifier %<%s%> will become a keyword in C++0x",
454 IDENTIFIER_POINTER (token->u.value));
456 /* Clear out the C_RID_CODE so we don't warn about this
457 particular identifier-turned-keyword again. */
458 C_RID_CODE (token->u.value) = RID_MAX;
461 token->ambiguous_p = false;
462 token->keyword = RID_MAX;
465 /* Handle Objective-C++ keywords. */
466 else if (token->type == CPP_AT_NAME)
468 token->type = CPP_KEYWORD;
469 switch (C_RID_CODE (token->u.value))
471 /* Map 'class' to '@class', 'private' to '@private', etc. */
472 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
473 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
474 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
475 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
476 case RID_THROW: token->keyword = RID_AT_THROW; break;
477 case RID_TRY: token->keyword = RID_AT_TRY; break;
478 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
479 default: token->keyword = C_RID_CODE (token->u.value);
482 else if (token->type == CPP_PRAGMA)
484 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
485 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
486 token->u.value = NULL_TREE;
490 /* Update the globals input_location and in_system_header and the
491 input file stack from TOKEN. */
493 cp_lexer_set_source_position_from_token (cp_token *token)
495 if (token->type != CPP_EOF)
497 input_location = token->location;
498 in_system_header = token->in_system_header;
499 restore_input_file_stack (token->input_file_stack_index);
503 /* Return a pointer to the next token in the token stream, but do not
506 static inline cp_token *
507 cp_lexer_peek_token (cp_lexer *lexer)
509 if (cp_lexer_debugging_p (lexer))
511 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
512 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
513 putc ('\n', cp_lexer_debug_stream);
515 return lexer->next_token;
518 /* Return true if the next token has the indicated TYPE. */
521 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
523 return cp_lexer_peek_token (lexer)->type == type;
526 /* Return true if the next token does not have the indicated TYPE. */
529 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
531 return !cp_lexer_next_token_is (lexer, type);
534 /* Return true if the next token is the indicated KEYWORD. */
537 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
539 return cp_lexer_peek_token (lexer)->keyword == keyword;
542 /* Return true if the next token is a keyword for a decl-specifier. */
545 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
549 token = cp_lexer_peek_token (lexer);
550 switch (token->keyword)
552 /* Storage classes. */
559 /* Elaborated type specifiers. */
565 /* Simple type specifiers. */
577 /* GNU extensions. */
587 /* Return a pointer to the Nth token in the token stream. If N is 1,
588 then this is precisely equivalent to cp_lexer_peek_token (except
589 that it is not inline). One would like to disallow that case, but
590 there is one case (cp_parser_nth_token_starts_template_id) where
591 the caller passes a variable for N and it might be 1. */
594 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
598 /* N is 1-based, not zero-based. */
601 if (cp_lexer_debugging_p (lexer))
602 fprintf (cp_lexer_debug_stream,
603 "cp_lexer: peeking ahead %ld at token: ", (long)n);
606 token = lexer->next_token;
607 gcc_assert (!n || token != &eof_token);
611 if (token == lexer->last_token)
613 token = (cp_token *)&eof_token;
617 if (token->type != CPP_PURGED)
621 if (cp_lexer_debugging_p (lexer))
623 cp_lexer_print_token (cp_lexer_debug_stream, token);
624 putc ('\n', cp_lexer_debug_stream);
630 /* Return the next token, and advance the lexer's next_token pointer
631 to point to the next non-purged token. */
634 cp_lexer_consume_token (cp_lexer* lexer)
636 cp_token *token = lexer->next_token;
638 gcc_assert (token != &eof_token);
639 gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
644 if (lexer->next_token == lexer->last_token)
646 lexer->next_token = (cp_token *)&eof_token;
651 while (lexer->next_token->type == CPP_PURGED);
653 cp_lexer_set_source_position_from_token (token);
655 /* Provide debugging output. */
656 if (cp_lexer_debugging_p (lexer))
658 fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
659 cp_lexer_print_token (cp_lexer_debug_stream, token);
660 putc ('\n', cp_lexer_debug_stream);
666 /* Permanently remove the next token from the token stream, and
667 advance the next_token pointer to refer to the next non-purged
671 cp_lexer_purge_token (cp_lexer *lexer)
673 cp_token *tok = lexer->next_token;
675 gcc_assert (tok != &eof_token);
676 tok->type = CPP_PURGED;
677 tok->location = UNKNOWN_LOCATION;
678 tok->u.value = NULL_TREE;
679 tok->keyword = RID_MAX;
684 if (tok == lexer->last_token)
686 tok = (cp_token *)&eof_token;
690 while (tok->type == CPP_PURGED);
691 lexer->next_token = tok;
694 /* Permanently remove all tokens after TOK, up to, but not
695 including, the token that will be returned next by
696 cp_lexer_peek_token. */
699 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
701 cp_token *peek = lexer->next_token;
703 if (peek == &eof_token)
704 peek = lexer->last_token;
706 gcc_assert (tok < peek);
708 for ( tok += 1; tok != peek; tok += 1)
710 tok->type = CPP_PURGED;
711 tok->location = UNKNOWN_LOCATION;
712 tok->u.value = NULL_TREE;
713 tok->keyword = RID_MAX;
717 /* Begin saving tokens. All tokens consumed after this point will be
721 cp_lexer_save_tokens (cp_lexer* lexer)
723 /* Provide debugging output. */
724 if (cp_lexer_debugging_p (lexer))
725 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
727 VEC_safe_push (cp_token_position, heap,
728 lexer->saved_tokens, lexer->next_token);
731 /* Commit to the portion of the token stream most recently saved. */
734 cp_lexer_commit_tokens (cp_lexer* lexer)
736 /* Provide debugging output. */
737 if (cp_lexer_debugging_p (lexer))
738 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
740 VEC_pop (cp_token_position, lexer->saved_tokens);
743 /* Return all tokens saved since the last call to cp_lexer_save_tokens
744 to the token stream. Stop saving tokens. */
747 cp_lexer_rollback_tokens (cp_lexer* lexer)
749 /* Provide debugging output. */
750 if (cp_lexer_debugging_p (lexer))
751 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
753 lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
756 /* Print a representation of the TOKEN on the STREAM. */
758 #ifdef ENABLE_CHECKING
761 cp_lexer_print_token (FILE * stream, cp_token *token)
763 /* We don't use cpp_type2name here because the parser defines
764 a few tokens of its own. */
765 static const char *const token_names[] = {
766 /* cpplib-defined token types */
772 /* C++ parser token types - see "Manifest constants", above. */
775 "NESTED_NAME_SPECIFIER",
779 /* If we have a name for the token, print it out. Otherwise, we
780 simply give the numeric code. */
781 gcc_assert (token->type < ARRAY_SIZE(token_names));
782 fputs (token_names[token->type], stream);
784 /* For some tokens, print the associated data. */
788 /* Some keywords have a value that is not an IDENTIFIER_NODE.
789 For example, `struct' is mapped to an INTEGER_CST. */
790 if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
792 /* else fall through */
794 fputs (IDENTIFIER_POINTER (token->u.value), stream);
799 fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
807 /* Start emitting debugging information. */
810 cp_lexer_start_debugging (cp_lexer* lexer)
812 lexer->debugging_p = true;
815 /* Stop emitting debugging information. */
818 cp_lexer_stop_debugging (cp_lexer* lexer)
820 lexer->debugging_p = false;
823 #endif /* ENABLE_CHECKING */
825 /* Create a new cp_token_cache, representing a range of tokens. */
827 static cp_token_cache *
828 cp_token_cache_new (cp_token *first, cp_token *last)
830 cp_token_cache *cache = GGC_NEW (cp_token_cache);
831 cache->first = first;
837 /* Decl-specifiers. */
839 /* Set *DECL_SPECS to represent an empty decl-specifier-seq. */
842 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
844 memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
849 /* Nothing other than the parser should be creating declarators;
850 declarators are a semi-syntactic representation of C++ entities.
851 Other parts of the front end that need to create entities (like
852 VAR_DECLs or FUNCTION_DECLs) should do that directly. */
854 static cp_declarator *make_call_declarator
855 (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
856 static cp_declarator *make_array_declarator
857 (cp_declarator *, tree);
858 static cp_declarator *make_pointer_declarator
859 (cp_cv_quals, cp_declarator *);
860 static cp_declarator *make_reference_declarator
861 (cp_cv_quals, cp_declarator *);
862 static cp_parameter_declarator *make_parameter_declarator
863 (cp_decl_specifier_seq *, cp_declarator *, tree);
864 static cp_declarator *make_ptrmem_declarator
865 (cp_cv_quals, tree, cp_declarator *);
867 /* An erroneous declarator. */
868 static cp_declarator *cp_error_declarator;
870 /* The obstack on which declarators and related data structures are
872 static struct obstack declarator_obstack;
874 /* Alloc BYTES from the declarator memory pool. */
877 alloc_declarator (size_t bytes)
879 return obstack_alloc (&declarator_obstack, bytes);
882 /* Allocate a declarator of the indicated KIND. Clear fields that are
883 common to all declarators. */
885 static cp_declarator *
886 make_declarator (cp_declarator_kind kind)
888 cp_declarator *declarator;
890 declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
891 declarator->kind = kind;
892 declarator->attributes = NULL_TREE;
893 declarator->declarator = NULL;
894 declarator->parameter_pack_p = false;
899 /* Make a declarator for a generalized identifier. If
900 QUALIFYING_SCOPE is non-NULL, the identifier is
901 QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
902 UNQUALIFIED_NAME. SFK indicates the kind of special function this
905 static cp_declarator *
906 make_id_declarator (tree qualifying_scope, tree unqualified_name,
907 special_function_kind sfk)
909 cp_declarator *declarator;
911 /* It is valid to write:
913 class C { void f(); };
917 The standard is not clear about whether `typedef const C D' is
918 legal; as of 2002-09-15 the committee is considering that
919 question. EDG 3.0 allows that syntax. Therefore, we do as
921 if (qualifying_scope && TYPE_P (qualifying_scope))
922 qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
924 gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
925 || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
926 || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
928 declarator = make_declarator (cdk_id);
929 declarator->u.id.qualifying_scope = qualifying_scope;
930 declarator->u.id.unqualified_name = unqualified_name;
931 declarator->u.id.sfk = sfk;
936 /* Make a declarator for a pointer to TARGET. CV_QUALIFIERS is a list
937 of modifiers such as const or volatile to apply to the pointer
938 type, represented as identifiers. */
941 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
943 cp_declarator *declarator;
945 declarator = make_declarator (cdk_pointer);
946 declarator->declarator = target;
947 declarator->u.pointer.qualifiers = cv_qualifiers;
948 declarator->u.pointer.class_type = NULL_TREE;
951 declarator->parameter_pack_p = target->parameter_pack_p;
952 target->parameter_pack_p = false;
955 declarator->parameter_pack_p = false;
960 /* Like make_pointer_declarator -- but for references. */
963 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
965 cp_declarator *declarator;
967 declarator = make_declarator (cdk_reference);
968 declarator->declarator = target;
969 declarator->u.pointer.qualifiers = cv_qualifiers;
970 declarator->u.pointer.class_type = NULL_TREE;
973 declarator->parameter_pack_p = target->parameter_pack_p;
974 target->parameter_pack_p = false;
977 declarator->parameter_pack_p = false;
982 /* Like make_pointer_declarator -- but for a pointer to a non-static
983 member of CLASS_TYPE. */
986 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
987 cp_declarator *pointee)
989 cp_declarator *declarator;
991 declarator = make_declarator (cdk_ptrmem);
992 declarator->declarator = pointee;
993 declarator->u.pointer.qualifiers = cv_qualifiers;
994 declarator->u.pointer.class_type = class_type;
998 declarator->parameter_pack_p = pointee->parameter_pack_p;
999 pointee->parameter_pack_p = false;
1002 declarator->parameter_pack_p = false;
1007 /* Make a declarator for the function given by TARGET, with the
1008 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1009 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1010 indicates what exceptions can be thrown. */
1013 make_call_declarator (cp_declarator *target,
1014 cp_parameter_declarator *parms,
1015 cp_cv_quals cv_qualifiers,
1016 tree exception_specification)
1018 cp_declarator *declarator;
1020 declarator = make_declarator (cdk_function);
1021 declarator->declarator = target;
1022 declarator->u.function.parameters = parms;
1023 declarator->u.function.qualifiers = cv_qualifiers;
1024 declarator->u.function.exception_specification = exception_specification;
1027 declarator->parameter_pack_p = target->parameter_pack_p;
1028 target->parameter_pack_p = false;
1031 declarator->parameter_pack_p = false;
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037 defined by ELEMENT. */
1040 make_array_declarator (cp_declarator *element, tree bounds)
1042 cp_declarator *declarator;
1044 declarator = make_declarator (cdk_array);
1045 declarator->declarator = element;
1046 declarator->u.array.bounds = bounds;
1049 declarator->parameter_pack_p = element->parameter_pack_p;
1050 element->parameter_pack_p = false;
1053 declarator->parameter_pack_p = false;
1058 cp_parameter_declarator *no_parameters;
1060 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1061 DECLARATOR and DEFAULT_ARGUMENT. */
1063 cp_parameter_declarator *
1064 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1065 cp_declarator *declarator,
1066 tree default_argument)
1068 cp_parameter_declarator *parameter;
1070 parameter = ((cp_parameter_declarator *)
1071 alloc_declarator (sizeof (cp_parameter_declarator)));
1072 parameter->next = NULL;
1073 if (decl_specifiers)
1074 parameter->decl_specifiers = *decl_specifiers;
1076 clear_decl_specs (¶meter->decl_specifiers);
1077 parameter->declarator = declarator;
1078 parameter->default_argument = default_argument;
1079 parameter->ellipsis_p = false;
1084 /* Returns true iff DECLARATOR is a declaration for a function. */
1087 function_declarator_p (const cp_declarator *declarator)
1091 if (declarator->kind == cdk_function
1092 && declarator->declarator->kind == cdk_id)
1094 if (declarator->kind == cdk_id
1095 || declarator->kind == cdk_error)
1097 declarator = declarator->declarator;
1107 A cp_parser parses the token stream as specified by the C++
1108 grammar. Its job is purely parsing, not semantic analysis. For
1109 example, the parser breaks the token stream into declarators,
1110 expressions, statements, and other similar syntactic constructs.
1111 It does not check that the types of the expressions on either side
1112 of an assignment-statement are compatible, or that a function is
1113 not declared with a parameter of type `void'.
1115 The parser invokes routines elsewhere in the compiler to perform
1116 semantic analysis and to build up the abstract syntax tree for the
1119 The parser (and the template instantiation code, which is, in a
1120 way, a close relative of parsing) are the only parts of the
1121 compiler that should be calling push_scope and pop_scope, or
1122 related functions. The parser (and template instantiation code)
1123 keeps track of what scope is presently active; everything else
1124 should simply honor that. (The code that generates static
1125 initializers may also need to set the scope, in order to check
1126 access control correctly when emitting the initializers.)
1131 The parser is of the standard recursive-descent variety. Upcoming
1132 tokens in the token stream are examined in order to determine which
1133 production to use when parsing a non-terminal. Some C++ constructs
1134 require arbitrary look ahead to disambiguate. For example, it is
1135 impossible, in the general case, to tell whether a statement is an
1136 expression or declaration without scanning the entire statement.
1137 Therefore, the parser is capable of "parsing tentatively." When the
1138 parser is not sure what construct comes next, it enters this mode.
1139 Then, while we attempt to parse the construct, the parser queues up
1140 error messages, rather than issuing them immediately, and saves the
1141 tokens it consumes. If the construct is parsed successfully, the
1142 parser "commits", i.e., it issues any queued error messages and
1143 the tokens that were being preserved are permanently discarded.
1144 If, however, the construct is not parsed successfully, the parser
1145 rolls back its state completely so that it can resume parsing using
1146 a different alternative.
1151 The performance of the parser could probably be improved substantially.
1152 We could often eliminate the need to parse tentatively by looking ahead
1153 a little bit. In some places, this approach might not entirely eliminate
1154 the need to parse tentatively, but it might still speed up the average
1157 /* Flags that are passed to some parsing functions. These values can
1158 be bitwise-ored together. */
1160 typedef enum cp_parser_flags
1163 CP_PARSER_FLAGS_NONE = 0x0,
1164 /* The construct is optional. If it is not present, then no error
1165 should be issued. */
1166 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1167 /* When parsing a type-specifier, do not allow user-defined types. */
1168 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1171 /* The different kinds of declarators we want to parse. */
1173 typedef enum cp_parser_declarator_kind
1175 /* We want an abstract declarator. */
1176 CP_PARSER_DECLARATOR_ABSTRACT,
1177 /* We want a named declarator. */
1178 CP_PARSER_DECLARATOR_NAMED,
1179 /* We don't mind, but the name must be an unqualified-id. */
1180 CP_PARSER_DECLARATOR_EITHER
1181 } cp_parser_declarator_kind;
1183 /* The precedence values used to parse binary expressions. The minimum value
1184 of PREC must be 1, because zero is reserved to quickly discriminate
1185 binary operators from other tokens. */
1190 PREC_LOGICAL_OR_EXPRESSION,
1191 PREC_LOGICAL_AND_EXPRESSION,
1192 PREC_INCLUSIVE_OR_EXPRESSION,
1193 PREC_EXCLUSIVE_OR_EXPRESSION,
1194 PREC_AND_EXPRESSION,
1195 PREC_EQUALITY_EXPRESSION,
1196 PREC_RELATIONAL_EXPRESSION,
1197 PREC_SHIFT_EXPRESSION,
1198 PREC_ADDITIVE_EXPRESSION,
1199 PREC_MULTIPLICATIVE_EXPRESSION,
1201 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1204 /* A mapping from a token type to a corresponding tree node type, with a
1205 precedence value. */
1207 typedef struct cp_parser_binary_operations_map_node
1209 /* The token type. */
1210 enum cpp_ttype token_type;
1211 /* The corresponding tree code. */
1212 enum tree_code tree_type;
1213 /* The precedence of this operator. */
1214 enum cp_parser_prec prec;
1215 } cp_parser_binary_operations_map_node;
1217 /* The status of a tentative parse. */
1219 typedef enum cp_parser_status_kind
1221 /* No errors have occurred. */
1222 CP_PARSER_STATUS_KIND_NO_ERROR,
1223 /* An error has occurred. */
1224 CP_PARSER_STATUS_KIND_ERROR,
1225 /* We are committed to this tentative parse, whether or not an error
1227 CP_PARSER_STATUS_KIND_COMMITTED
1228 } cp_parser_status_kind;
1230 typedef struct cp_parser_expression_stack_entry
1232 /* Left hand side of the binary operation we are currently
1235 /* Original tree code for left hand side, if it was a binary
1236 expression itself (used for -Wparentheses). */
1237 enum tree_code lhs_type;
1238 /* Tree code for the binary operation we are parsing. */
1239 enum tree_code tree_type;
1240 /* Precedence of the binary operation we are parsing. */
1242 } cp_parser_expression_stack_entry;
1244 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1245 entries because precedence levels on the stack are monotonically
1247 typedef struct cp_parser_expression_stack_entry
1248 cp_parser_expression_stack[NUM_PREC_VALUES];
1250 /* Context that is saved and restored when parsing tentatively. */
1251 typedef struct cp_parser_context GTY (())
1253 /* If this is a tentative parsing context, the status of the
1255 enum cp_parser_status_kind status;
1256 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1257 that are looked up in this context must be looked up both in the
1258 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1259 the context of the containing expression. */
1262 /* The next parsing context in the stack. */
1263 struct cp_parser_context *next;
1264 } cp_parser_context;
1268 /* Constructors and destructors. */
1270 static cp_parser_context *cp_parser_context_new
1271 (cp_parser_context *);
1273 /* Class variables. */
1275 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1277 /* The operator-precedence table used by cp_parser_binary_expression.
1278 Transformed into an associative array (binops_by_token) by
1281 static const cp_parser_binary_operations_map_node binops[] = {
1282 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1283 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1285 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1286 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1287 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1289 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1290 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1292 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1293 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1295 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1296 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1297 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1298 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1300 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1301 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1303 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1305 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1307 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1309 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1311 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1314 /* The same as binops, but initialized by cp_parser_new so that
1315 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1317 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1319 /* Constructors and destructors. */
1321 /* Construct a new context. The context below this one on the stack
1322 is given by NEXT. */
1324 static cp_parser_context *
1325 cp_parser_context_new (cp_parser_context* next)
1327 cp_parser_context *context;
1329 /* Allocate the storage. */
1330 if (cp_parser_context_free_list != NULL)
1332 /* Pull the first entry from the free list. */
1333 context = cp_parser_context_free_list;
1334 cp_parser_context_free_list = context->next;
1335 memset (context, 0, sizeof (*context));
1338 context = GGC_CNEW (cp_parser_context);
1340 /* No errors have occurred yet in this context. */
1341 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1342 /* If this is not the bottomost context, copy information that we
1343 need from the previous context. */
1346 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1347 expression, then we are parsing one in this context, too. */
1348 context->object_type = next->object_type;
1349 /* Thread the stack. */
1350 context->next = next;
1356 /* The cp_parser structure represents the C++ parser. */
1358 typedef struct cp_parser GTY(())
1360 /* The lexer from which we are obtaining tokens. */
1363 /* The scope in which names should be looked up. If NULL_TREE, then
1364 we look up names in the scope that is currently open in the
1365 source program. If non-NULL, this is either a TYPE or
1366 NAMESPACE_DECL for the scope in which we should look. It can
1367 also be ERROR_MARK, when we've parsed a bogus scope.
1369 This value is not cleared automatically after a name is looked
1370 up, so we must be careful to clear it before starting a new look
1371 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1372 will look up `Z' in the scope of `X', rather than the current
1373 scope.) Unfortunately, it is difficult to tell when name lookup
1374 is complete, because we sometimes peek at a token, look it up,
1375 and then decide not to consume it. */
1378 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1379 last lookup took place. OBJECT_SCOPE is used if an expression
1380 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1381 respectively. QUALIFYING_SCOPE is used for an expression of the
1382 form "X::Y"; it refers to X. */
1384 tree qualifying_scope;
1386 /* A stack of parsing contexts. All but the bottom entry on the
1387 stack will be tentative contexts.
1389 We parse tentatively in order to determine which construct is in
1390 use in some situations. For example, in order to determine
1391 whether a statement is an expression-statement or a
1392 declaration-statement we parse it tentatively as a
1393 declaration-statement. If that fails, we then reparse the same
1394 token stream as an expression-statement. */
1395 cp_parser_context *context;
1397 /* True if we are parsing GNU C++. If this flag is not set, then
1398 GNU extensions are not recognized. */
1399 bool allow_gnu_extensions_p;
1401 /* TRUE if the `>' token should be interpreted as the greater-than
1402 operator. FALSE if it is the end of a template-id or
1403 template-parameter-list. */
1404 bool greater_than_is_operator_p;
1406 /* TRUE if default arguments are allowed within a parameter list
1407 that starts at this point. FALSE if only a gnu extension makes
1408 them permissible. */
1409 bool default_arg_ok_p;
1411 /* TRUE if we are parsing an integral constant-expression. See
1412 [expr.const] for a precise definition. */
1413 bool integral_constant_expression_p;
1415 /* TRUE if we are parsing an integral constant-expression -- but a
1416 non-constant expression should be permitted as well. This flag
1417 is used when parsing an array bound so that GNU variable-length
1418 arrays are tolerated. */
1419 bool allow_non_integral_constant_expression_p;
1421 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1422 been seen that makes the expression non-constant. */
1423 bool non_integral_constant_expression_p;
1425 /* TRUE if local variable names and `this' are forbidden in the
1427 bool local_variables_forbidden_p;
1429 /* TRUE if the declaration we are parsing is part of a
1430 linkage-specification of the form `extern string-literal
1432 bool in_unbraced_linkage_specification_p;
1434 /* TRUE if we are presently parsing a declarator, after the
1435 direct-declarator. */
1436 bool in_declarator_p;
1438 /* TRUE if we are presently parsing a template-argument-list. */
1439 bool in_template_argument_list_p;
1441 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1442 to IN_OMP_BLOCK if parsing OpenMP structured block and
1443 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1444 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1445 iteration-statement, OpenMP block or loop within that switch. */
1446 #define IN_SWITCH_STMT 1
1447 #define IN_ITERATION_STMT 2
1448 #define IN_OMP_BLOCK 4
1449 #define IN_OMP_FOR 8
1450 #define IN_IF_STMT 16
1451 unsigned char in_statement;
1453 /* TRUE if we are presently parsing the body of a switch statement.
1454 Note that this doesn't quite overlap with in_statement above.
1455 The difference relates to giving the right sets of error messages:
1456 "case not in switch" vs "break statement used with OpenMP...". */
1457 bool in_switch_statement_p;
1459 /* TRUE if we are parsing a type-id in an expression context. In
1460 such a situation, both "type (expr)" and "type (type)" are valid
1462 bool in_type_id_in_expr_p;
1464 /* TRUE if we are currently in a header file where declarations are
1465 implicitly extern "C". */
1466 bool implicit_extern_c;
1468 /* TRUE if strings in expressions should be translated to the execution
1470 bool translate_strings_p;
1472 /* TRUE if we are presently parsing the body of a function, but not
1474 bool in_function_body;
1476 /* If non-NULL, then we are parsing a construct where new type
1477 definitions are not permitted. The string stored here will be
1478 issued as an error message if a type is defined. */
1479 const char *type_definition_forbidden_message;
1481 /* A list of lists. The outer list is a stack, used for member
1482 functions of local classes. At each level there are two sub-list,
1483 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1484 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1485 TREE_VALUE's. The functions are chained in reverse declaration
1488 The TREE_PURPOSE sublist contains those functions with default
1489 arguments that need post processing, and the TREE_VALUE sublist
1490 contains those functions with definitions that need post
1493 These lists can only be processed once the outermost class being
1494 defined is complete. */
1495 tree unparsed_functions_queues;
1497 /* The number of classes whose definitions are currently in
1499 unsigned num_classes_being_defined;
1501 /* The number of template parameter lists that apply directly to the
1502 current declaration. */
1503 unsigned num_template_parameter_lists;
1508 /* Constructors and destructors. */
1510 static cp_parser *cp_parser_new
1513 /* Routines to parse various constructs.
1515 Those that return `tree' will return the error_mark_node (rather
1516 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517 Sometimes, they will return an ordinary node if error-recovery was
1518 attempted, even though a parse error occurred. So, to check
1519 whether or not a parse error occurred, you should always use
1520 cp_parser_error_occurred. If the construct is optional (indicated
1521 either by an `_opt' in the name of the function that does the
1522 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523 the construct is not present. */
1525 /* Lexical conventions [gram.lex] */
1527 static tree cp_parser_identifier
1529 static tree cp_parser_string_literal
1530 (cp_parser *, bool, bool);
1532 /* Basic concepts [gram.basic] */
1534 static bool cp_parser_translation_unit
1537 /* Expressions [gram.expr] */
1539 static tree cp_parser_primary_expression
1540 (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542 (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544 (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546 (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548 (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_class_or_namespace_name
1550 (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552 (cp_parser *, bool, bool);
1553 static tree cp_parser_postfix_open_square_expression
1554 (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1557 static tree cp_parser_parenthesized_expression_list
1558 (cp_parser *, bool, bool, bool, bool *);
1559 static void cp_parser_pseudo_destructor_name
1560 (cp_parser *, tree *, tree *);
1561 static tree cp_parser_unary_expression
1562 (cp_parser *, bool, bool);
1563 static enum tree_code cp_parser_unary_operator
1565 static tree cp_parser_new_expression
1567 static tree cp_parser_new_placement
1569 static tree cp_parser_new_type_id
1570 (cp_parser *, tree *);
1571 static cp_declarator *cp_parser_new_declarator_opt
1573 static cp_declarator *cp_parser_direct_new_declarator
1575 static tree cp_parser_new_initializer
1577 static tree cp_parser_delete_expression
1579 static tree cp_parser_cast_expression
1580 (cp_parser *, bool, bool);
1581 static tree cp_parser_binary_expression
1582 (cp_parser *, bool);
1583 static tree cp_parser_question_colon_clause
1584 (cp_parser *, tree);
1585 static tree cp_parser_assignment_expression
1586 (cp_parser *, bool);
1587 static enum tree_code cp_parser_assignment_operator_opt
1589 static tree cp_parser_expression
1590 (cp_parser *, bool);
1591 static tree cp_parser_constant_expression
1592 (cp_parser *, bool, bool *);
1593 static tree cp_parser_builtin_offsetof
1596 /* Statements [gram.stmt.stmt] */
1598 static void cp_parser_statement
1599 (cp_parser *, tree, bool, bool *);
1600 static void cp_parser_label_for_labeled_statement
1602 static tree cp_parser_expression_statement
1603 (cp_parser *, tree);
1604 static tree cp_parser_compound_statement
1605 (cp_parser *, tree, bool);
1606 static void cp_parser_statement_seq_opt
1607 (cp_parser *, tree);
1608 static tree cp_parser_selection_statement
1609 (cp_parser *, bool *);
1610 static tree cp_parser_condition
1612 static tree cp_parser_iteration_statement
1614 static void cp_parser_for_init_statement
1616 static tree cp_parser_jump_statement
1618 static void cp_parser_declaration_statement
1621 static tree cp_parser_implicitly_scoped_statement
1622 (cp_parser *, bool *);
1623 static void cp_parser_already_scoped_statement
1626 /* Declarations [gram.dcl.dcl] */
1628 static void cp_parser_declaration_seq_opt
1630 static void cp_parser_declaration
1632 static void cp_parser_block_declaration
1633 (cp_parser *, bool);
1634 static void cp_parser_simple_declaration
1635 (cp_parser *, bool);
1636 static void cp_parser_decl_specifier_seq
1637 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1638 static tree cp_parser_storage_class_specifier_opt
1640 static tree cp_parser_function_specifier_opt
1641 (cp_parser *, cp_decl_specifier_seq *);
1642 static tree cp_parser_type_specifier
1643 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1645 static tree cp_parser_simple_type_specifier
1646 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1647 static tree cp_parser_type_name
1649 static tree cp_parser_elaborated_type_specifier
1650 (cp_parser *, bool, bool);
1651 static tree cp_parser_enum_specifier
1653 static void cp_parser_enumerator_list
1654 (cp_parser *, tree);
1655 static void cp_parser_enumerator_definition
1656 (cp_parser *, tree);
1657 static tree cp_parser_namespace_name
1659 static void cp_parser_namespace_definition
1661 static void cp_parser_namespace_body
1663 static tree cp_parser_qualified_namespace_specifier
1665 static void cp_parser_namespace_alias_definition
1667 static bool cp_parser_using_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_using_directive
1671 static void cp_parser_asm_definition
1673 static void cp_parser_linkage_specification
1675 static void cp_parser_static_assert
1676 (cp_parser *, bool);
1678 /* Declarators [gram.dcl.decl] */
1680 static tree cp_parser_init_declarator
1681 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1682 static cp_declarator *cp_parser_declarator
1683 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1684 static cp_declarator *cp_parser_direct_declarator
1685 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1686 static enum tree_code cp_parser_ptr_operator
1687 (cp_parser *, tree *, cp_cv_quals *);
1688 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1690 static tree cp_parser_declarator_id
1691 (cp_parser *, bool);
1692 static tree cp_parser_type_id
1694 static void cp_parser_type_specifier_seq
1695 (cp_parser *, bool, cp_decl_specifier_seq *);
1696 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1698 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1699 (cp_parser *, bool *);
1700 static cp_parameter_declarator *cp_parser_parameter_declaration
1701 (cp_parser *, bool, bool *);
1702 static void cp_parser_function_body
1704 static tree cp_parser_initializer
1705 (cp_parser *, bool *, bool *);
1706 static tree cp_parser_initializer_clause
1707 (cp_parser *, bool *);
1708 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1709 (cp_parser *, bool *);
1711 static bool cp_parser_ctor_initializer_opt_and_function_body
1714 /* Classes [gram.class] */
1716 static tree cp_parser_class_name
1717 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1718 static tree cp_parser_class_specifier
1720 static tree cp_parser_class_head
1721 (cp_parser *, bool *, tree *, tree *);
1722 static enum tag_types cp_parser_class_key
1724 static void cp_parser_member_specification_opt
1726 static void cp_parser_member_declaration
1728 static tree cp_parser_pure_specifier
1730 static tree cp_parser_constant_initializer
1733 /* Derived classes [gram.class.derived] */
1735 static tree cp_parser_base_clause
1737 static tree cp_parser_base_specifier
1740 /* Special member functions [gram.special] */
1742 static tree cp_parser_conversion_function_id
1744 static tree cp_parser_conversion_type_id
1746 static cp_declarator *cp_parser_conversion_declarator_opt
1748 static bool cp_parser_ctor_initializer_opt
1750 static void cp_parser_mem_initializer_list
1752 static tree cp_parser_mem_initializer
1754 static tree cp_parser_mem_initializer_id
1757 /* Overloading [gram.over] */
1759 static tree cp_parser_operator_function_id
1761 static tree cp_parser_operator
1764 /* Templates [gram.temp] */
1766 static void cp_parser_template_declaration
1767 (cp_parser *, bool);
1768 static tree cp_parser_template_parameter_list
1770 static tree cp_parser_template_parameter
1771 (cp_parser *, bool *, bool *);
1772 static tree cp_parser_type_parameter
1773 (cp_parser *, bool *);
1774 static tree cp_parser_template_id
1775 (cp_parser *, bool, bool, bool);
1776 static tree cp_parser_template_name
1777 (cp_parser *, bool, bool, bool, bool *);
1778 static tree cp_parser_template_argument_list
1780 static tree cp_parser_template_argument
1782 static void cp_parser_explicit_instantiation
1784 static void cp_parser_explicit_specialization
1787 /* Exception handling [gram.exception] */
1789 static tree cp_parser_try_block
1791 static bool cp_parser_function_try_block
1793 static void cp_parser_handler_seq
1795 static void cp_parser_handler
1797 static tree cp_parser_exception_declaration
1799 static tree cp_parser_throw_expression
1801 static tree cp_parser_exception_specification_opt
1803 static tree cp_parser_type_id_list
1806 /* GNU Extensions */
1808 static tree cp_parser_asm_specification_opt
1810 static tree cp_parser_asm_operand_list
1812 static tree cp_parser_asm_clobber_list
1814 static tree cp_parser_attributes_opt
1816 static tree cp_parser_attribute_list
1818 static bool cp_parser_extension_opt
1819 (cp_parser *, int *);
1820 static void cp_parser_label_declaration
1823 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1824 static bool cp_parser_pragma
1825 (cp_parser *, enum pragma_context);
1827 /* Objective-C++ Productions */
1829 static tree cp_parser_objc_message_receiver
1831 static tree cp_parser_objc_message_args
1833 static tree cp_parser_objc_message_expression
1835 static tree cp_parser_objc_encode_expression
1837 static tree cp_parser_objc_defs_expression
1839 static tree cp_parser_objc_protocol_expression
1841 static tree cp_parser_objc_selector_expression
1843 static tree cp_parser_objc_expression
1845 static bool cp_parser_objc_selector_p
1847 static tree cp_parser_objc_selector
1849 static tree cp_parser_objc_protocol_refs_opt
1851 static void cp_parser_objc_declaration
1853 static tree cp_parser_objc_statement
1856 /* Utility Routines */
1858 static tree cp_parser_lookup_name
1859 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1860 static tree cp_parser_lookup_name_simple
1861 (cp_parser *, tree);
1862 static tree cp_parser_maybe_treat_template_as_class
1864 static bool cp_parser_check_declarator_template_parameters
1865 (cp_parser *, cp_declarator *);
1866 static bool cp_parser_check_template_parameters
1867 (cp_parser *, unsigned);
1868 static tree cp_parser_simple_cast_expression
1870 static tree cp_parser_global_scope_opt
1871 (cp_parser *, bool);
1872 static bool cp_parser_constructor_declarator_p
1873 (cp_parser *, bool);
1874 static tree cp_parser_function_definition_from_specifiers_and_declarator
1875 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1876 static tree cp_parser_function_definition_after_declarator
1877 (cp_parser *, bool);
1878 static void cp_parser_template_declaration_after_export
1879 (cp_parser *, bool);
1880 static void cp_parser_perform_template_parameter_access_checks
1881 (VEC (deferred_access_check,gc)*);
1882 static tree cp_parser_single_declaration
1883 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool *);
1884 static tree cp_parser_functional_cast
1885 (cp_parser *, tree);
1886 static tree cp_parser_save_member_function_body
1887 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1888 static tree cp_parser_enclosed_template_argument_list
1890 static void cp_parser_save_default_args
1891 (cp_parser *, tree);
1892 static void cp_parser_late_parsing_for_member
1893 (cp_parser *, tree);
1894 static void cp_parser_late_parsing_default_args
1895 (cp_parser *, tree);
1896 static tree cp_parser_sizeof_operand
1897 (cp_parser *, enum rid);
1898 static bool cp_parser_declares_only_class_p
1900 static void cp_parser_set_storage_class
1901 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1902 static void cp_parser_set_decl_spec_type
1903 (cp_decl_specifier_seq *, tree, bool);
1904 static bool cp_parser_friend_p
1905 (const cp_decl_specifier_seq *);
1906 static cp_token *cp_parser_require
1907 (cp_parser *, enum cpp_ttype, const char *);
1908 static cp_token *cp_parser_require_keyword
1909 (cp_parser *, enum rid, const char *);
1910 static bool cp_parser_token_starts_function_definition_p
1912 static bool cp_parser_next_token_starts_class_definition_p
1914 static bool cp_parser_next_token_ends_template_argument_p
1916 static bool cp_parser_nth_token_starts_template_argument_list_p
1917 (cp_parser *, size_t);
1918 static enum tag_types cp_parser_token_is_class_key
1920 static void cp_parser_check_class_key
1921 (enum tag_types, tree type);
1922 static void cp_parser_check_access_in_redeclaration
1924 static bool cp_parser_optional_template_keyword
1926 static void cp_parser_pre_parsed_nested_name_specifier
1928 static void cp_parser_cache_group
1929 (cp_parser *, enum cpp_ttype, unsigned);
1930 static void cp_parser_parse_tentatively
1932 static void cp_parser_commit_to_tentative_parse
1934 static void cp_parser_abort_tentative_parse
1936 static bool cp_parser_parse_definitely
1938 static inline bool cp_parser_parsing_tentatively
1940 static bool cp_parser_uncommitted_to_tentative_parse_p
1942 static void cp_parser_error
1943 (cp_parser *, const char *);
1944 static void cp_parser_name_lookup_error
1945 (cp_parser *, tree, tree, const char *);
1946 static bool cp_parser_simulate_error
1948 static bool cp_parser_check_type_definition
1950 static void cp_parser_check_for_definition_in_return_type
1951 (cp_declarator *, tree);
1952 static void cp_parser_check_for_invalid_template_id
1953 (cp_parser *, tree);
1954 static bool cp_parser_non_integral_constant_expression
1955 (cp_parser *, const char *);
1956 static void cp_parser_diagnose_invalid_type_name
1957 (cp_parser *, tree, tree);
1958 static bool cp_parser_parse_and_diagnose_invalid_type_name
1960 static int cp_parser_skip_to_closing_parenthesis
1961 (cp_parser *, bool, bool, bool);
1962 static void cp_parser_skip_to_end_of_statement
1964 static void cp_parser_consume_semicolon_at_end_of_statement
1966 static void cp_parser_skip_to_end_of_block_or_statement
1968 static void cp_parser_skip_to_closing_brace
1970 static void cp_parser_skip_to_end_of_template_parameter_list
1972 static void cp_parser_skip_to_pragma_eol
1973 (cp_parser*, cp_token *);
1974 static bool cp_parser_error_occurred
1976 static bool cp_parser_allow_gnu_extensions_p
1978 static bool cp_parser_is_string_literal
1980 static bool cp_parser_is_keyword
1981 (cp_token *, enum rid);
1982 static tree cp_parser_make_typename_type
1983 (cp_parser *, tree, tree);
1985 /* Returns nonzero if we are parsing tentatively. */
1988 cp_parser_parsing_tentatively (cp_parser* parser)
1990 return parser->context->next != NULL;
1993 /* Returns nonzero if TOKEN is a string literal. */
1996 cp_parser_is_string_literal (cp_token* token)
1998 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2001 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2004 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2006 return token->keyword == keyword;
2009 /* If not parsing tentatively, issue a diagnostic of the form
2010 FILE:LINE: MESSAGE before TOKEN
2011 where TOKEN is the next token in the input stream. MESSAGE
2012 (specified by the caller) is usually of the form "expected
2016 cp_parser_error (cp_parser* parser, const char* message)
2018 if (!cp_parser_simulate_error (parser))
2020 cp_token *token = cp_lexer_peek_token (parser->lexer);
2021 /* This diagnostic makes more sense if it is tagged to the line
2022 of the token we just peeked at. */
2023 cp_lexer_set_source_position_from_token (token);
2025 if (token->type == CPP_PRAGMA)
2027 error ("%<#pragma%> is not allowed here");
2028 cp_parser_skip_to_pragma_eol (parser, token);
2032 c_parse_error (message,
2033 /* Because c_parser_error does not understand
2034 CPP_KEYWORD, keywords are treated like
2036 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2041 /* Issue an error about name-lookup failing. NAME is the
2042 IDENTIFIER_NODE DECL is the result of
2043 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2044 the thing that we hoped to find. */
2047 cp_parser_name_lookup_error (cp_parser* parser,
2050 const char* desired)
2052 /* If name lookup completely failed, tell the user that NAME was not
2054 if (decl == error_mark_node)
2056 if (parser->scope && parser->scope != global_namespace)
2057 error ("%<%E::%E%> has not been declared",
2058 parser->scope, name);
2059 else if (parser->scope == global_namespace)
2060 error ("%<::%E%> has not been declared", name);
2061 else if (parser->object_scope
2062 && !CLASS_TYPE_P (parser->object_scope))
2063 error ("request for member %qE in non-class type %qT",
2064 name, parser->object_scope);
2065 else if (parser->object_scope)
2066 error ("%<%T::%E%> has not been declared",
2067 parser->object_scope, name);
2069 error ("%qE has not been declared", name);
2071 else if (parser->scope && parser->scope != global_namespace)
2072 error ("%<%E::%E%> %s", parser->scope, name, desired);
2073 else if (parser->scope == global_namespace)
2074 error ("%<::%E%> %s", name, desired);
2076 error ("%qE %s", name, desired);
2079 /* If we are parsing tentatively, remember that an error has occurred
2080 during this tentative parse. Returns true if the error was
2081 simulated; false if a message should be issued by the caller. */
2084 cp_parser_simulate_error (cp_parser* parser)
2086 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2088 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2094 /* Check for repeated decl-specifiers. */
2097 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2101 for (ds = ds_first; ds != ds_last; ++ds)
2103 unsigned count = decl_specs->specs[(int)ds];
2106 /* The "long" specifier is a special case because of "long long". */
2110 error ("%<long long long%> is too long for GCC");
2111 else if (pedantic && !in_system_header && warn_long_long)
2112 pedwarn ("ISO C++ does not support %<long long%>");
2116 static const char *const decl_spec_names[] = {
2132 error ("duplicate %qs", decl_spec_names[(int)ds]);
2137 /* This function is called when a type is defined. If type
2138 definitions are forbidden at this point, an error message is
2142 cp_parser_check_type_definition (cp_parser* parser)
2144 /* If types are forbidden here, issue a message. */
2145 if (parser->type_definition_forbidden_message)
2147 /* Use `%s' to print the string in case there are any escape
2148 characters in the message. */
2149 error ("%s", parser->type_definition_forbidden_message);
2155 /* This function is called when the DECLARATOR is processed. The TYPE
2156 was a type defined in the decl-specifiers. If it is invalid to
2157 define a type in the decl-specifiers for DECLARATOR, an error is
2161 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2164 /* [dcl.fct] forbids type definitions in return types.
2165 Unfortunately, it's not easy to know whether or not we are
2166 processing a return type until after the fact. */
2168 && (declarator->kind == cdk_pointer
2169 || declarator->kind == cdk_reference
2170 || declarator->kind == cdk_ptrmem))
2171 declarator = declarator->declarator;
2173 && declarator->kind == cdk_function)
2175 error ("new types may not be defined in a return type");
2176 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2181 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2182 "<" in any valid C++ program. If the next token is indeed "<",
2183 issue a message warning the user about what appears to be an
2184 invalid attempt to form a template-id. */
2187 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2190 cp_token_position start = 0;
2192 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2195 error ("%qT is not a template", type);
2196 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2197 error ("%qE is not a template", type);
2199 error ("invalid template-id");
2200 /* Remember the location of the invalid "<". */
2201 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2202 start = cp_lexer_token_position (parser->lexer, true);
2203 /* Consume the "<". */
2204 cp_lexer_consume_token (parser->lexer);
2205 /* Parse the template arguments. */
2206 cp_parser_enclosed_template_argument_list (parser);
2207 /* Permanently remove the invalid template arguments so that
2208 this error message is not issued again. */
2210 cp_lexer_purge_tokens_after (parser->lexer, start);
2214 /* If parsing an integral constant-expression, issue an error message
2215 about the fact that THING appeared and return true. Otherwise,
2216 return false. In either case, set
2217 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2220 cp_parser_non_integral_constant_expression (cp_parser *parser,
2223 parser->non_integral_constant_expression_p = true;
2224 if (parser->integral_constant_expression_p)
2226 if (!parser->allow_non_integral_constant_expression_p)
2228 error ("%s cannot appear in a constant-expression", thing);
2235 /* Emit a diagnostic for an invalid type name. SCOPE is the
2236 qualifying scope (or NULL, if none) for ID. This function commits
2237 to the current active tentative parse, if any. (Otherwise, the
2238 problematic construct might be encountered again later, resulting
2239 in duplicate error messages.) */
2242 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2244 tree decl, old_scope;
2245 /* Try to lookup the identifier. */
2246 old_scope = parser->scope;
2247 parser->scope = scope;
2248 decl = cp_parser_lookup_name_simple (parser, id);
2249 parser->scope = old_scope;
2250 /* If the lookup found a template-name, it means that the user forgot
2251 to specify an argument list. Emit a useful error message. */
2252 if (TREE_CODE (decl) == TEMPLATE_DECL)
2253 error ("invalid use of template-name %qE without an argument list", decl);
2254 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2255 error ("invalid use of destructor %qD as a type", id);
2256 else if (TREE_CODE (decl) == TYPE_DECL)
2257 /* Something like 'unsigned A a;' */
2258 error ("invalid combination of multiple type-specifiers");
2259 else if (!parser->scope)
2261 /* Issue an error message. */
2262 error ("%qE does not name a type", id);
2263 /* If we're in a template class, it's possible that the user was
2264 referring to a type from a base class. For example:
2266 template <typename T> struct A { typedef T X; };
2267 template <typename T> struct B : public A<T> { X x; };
2269 The user should have said "typename A<T>::X". */
2270 if (processing_template_decl && current_class_type
2271 && TYPE_BINFO (current_class_type))
2275 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2279 tree base_type = BINFO_TYPE (b);
2280 if (CLASS_TYPE_P (base_type)
2281 && dependent_type_p (base_type))
2284 /* Go from a particular instantiation of the
2285 template (which will have an empty TYPE_FIELDs),
2286 to the main version. */
2287 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2288 for (field = TYPE_FIELDS (base_type);
2290 field = TREE_CHAIN (field))
2291 if (TREE_CODE (field) == TYPE_DECL
2292 && DECL_NAME (field) == id)
2294 inform ("(perhaps %<typename %T::%E%> was intended)",
2295 BINFO_TYPE (b), id);
2304 /* Here we diagnose qualified-ids where the scope is actually correct,
2305 but the identifier does not resolve to a valid type name. */
2306 else if (parser->scope != error_mark_node)
2308 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2309 error ("%qE in namespace %qE does not name a type",
2311 else if (TYPE_P (parser->scope))
2312 error ("%qE in class %qT does not name a type", id, parser->scope);
2316 cp_parser_commit_to_tentative_parse (parser);
2319 /* Check for a common situation where a type-name should be present,
2320 but is not, and issue a sensible error message. Returns true if an
2321 invalid type-name was detected.
2323 The situation handled by this function are variable declarations of the
2324 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2325 Usually, `ID' should name a type, but if we got here it means that it
2326 does not. We try to emit the best possible error message depending on
2327 how exactly the id-expression looks like. */
2330 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2334 cp_parser_parse_tentatively (parser);
2335 id = cp_parser_id_expression (parser,
2336 /*template_keyword_p=*/false,
2337 /*check_dependency_p=*/true,
2338 /*template_p=*/NULL,
2339 /*declarator_p=*/true,
2340 /*optional_p=*/false);
2341 /* After the id-expression, there should be a plain identifier,
2342 otherwise this is not a simple variable declaration. Also, if
2343 the scope is dependent, we cannot do much. */
2344 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2345 || (parser->scope && TYPE_P (parser->scope)
2346 && dependent_type_p (parser->scope))
2347 || TREE_CODE (id) == TYPE_DECL)
2349 cp_parser_abort_tentative_parse (parser);
2352 if (!cp_parser_parse_definitely (parser))
2355 /* Emit a diagnostic for the invalid type. */
2356 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2357 /* Skip to the end of the declaration; there's no point in
2358 trying to process it. */
2359 cp_parser_skip_to_end_of_block_or_statement (parser);
2363 /* Consume tokens up to, and including, the next non-nested closing `)'.
2364 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2365 are doing error recovery. Returns -1 if OR_COMMA is true and we
2366 found an unnested comma. */
2369 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2374 unsigned paren_depth = 0;
2375 unsigned brace_depth = 0;
2377 if (recovering && !or_comma
2378 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2383 cp_token * token = cp_lexer_peek_token (parser->lexer);
2385 switch (token->type)
2388 case CPP_PRAGMA_EOL:
2389 /* If we've run out of tokens, then there is no closing `)'. */
2393 /* This matches the processing in skip_to_end_of_statement. */
2398 case CPP_OPEN_BRACE:
2401 case CPP_CLOSE_BRACE:
2407 if (recovering && or_comma && !brace_depth && !paren_depth)
2411 case CPP_OPEN_PAREN:
2416 case CPP_CLOSE_PAREN:
2417 if (!brace_depth && !paren_depth--)
2420 cp_lexer_consume_token (parser->lexer);
2429 /* Consume the token. */
2430 cp_lexer_consume_token (parser->lexer);
2434 /* Consume tokens until we reach the end of the current statement.
2435 Normally, that will be just before consuming a `;'. However, if a
2436 non-nested `}' comes first, then we stop before consuming that. */
2439 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2441 unsigned nesting_depth = 0;
2445 cp_token *token = cp_lexer_peek_token (parser->lexer);
2447 switch (token->type)
2450 case CPP_PRAGMA_EOL:
2451 /* If we've run out of tokens, stop. */
2455 /* If the next token is a `;', we have reached the end of the
2461 case CPP_CLOSE_BRACE:
2462 /* If this is a non-nested '}', stop before consuming it.
2463 That way, when confronted with something like:
2467 we stop before consuming the closing '}', even though we
2468 have not yet reached a `;'. */
2469 if (nesting_depth == 0)
2472 /* If it is the closing '}' for a block that we have
2473 scanned, stop -- but only after consuming the token.
2479 we will stop after the body of the erroneously declared
2480 function, but before consuming the following `typedef'
2482 if (--nesting_depth == 0)
2484 cp_lexer_consume_token (parser->lexer);
2488 case CPP_OPEN_BRACE:
2496 /* Consume the token. */
2497 cp_lexer_consume_token (parser->lexer);
2501 /* This function is called at the end of a statement or declaration.
2502 If the next token is a semicolon, it is consumed; otherwise, error
2503 recovery is attempted. */
2506 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2508 /* Look for the trailing `;'. */
2509 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2511 /* If there is additional (erroneous) input, skip to the end of
2513 cp_parser_skip_to_end_of_statement (parser);
2514 /* If the next token is now a `;', consume it. */
2515 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2516 cp_lexer_consume_token (parser->lexer);
2520 /* Skip tokens until we have consumed an entire block, or until we
2521 have consumed a non-nested `;'. */
2524 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2526 int nesting_depth = 0;
2528 while (nesting_depth >= 0)
2530 cp_token *token = cp_lexer_peek_token (parser->lexer);
2532 switch (token->type)
2535 case CPP_PRAGMA_EOL:
2536 /* If we've run out of tokens, stop. */
2540 /* Stop if this is an unnested ';'. */
2545 case CPP_CLOSE_BRACE:
2546 /* Stop if this is an unnested '}', or closes the outermost
2553 case CPP_OPEN_BRACE:
2562 /* Consume the token. */
2563 cp_lexer_consume_token (parser->lexer);
2567 /* Skip tokens until a non-nested closing curly brace is the next
2571 cp_parser_skip_to_closing_brace (cp_parser *parser)
2573 unsigned nesting_depth = 0;
2577 cp_token *token = cp_lexer_peek_token (parser->lexer);
2579 switch (token->type)
2582 case CPP_PRAGMA_EOL:
2583 /* If we've run out of tokens, stop. */
2586 case CPP_CLOSE_BRACE:
2587 /* If the next token is a non-nested `}', then we have reached
2588 the end of the current block. */
2589 if (nesting_depth-- == 0)
2593 case CPP_OPEN_BRACE:
2594 /* If it the next token is a `{', then we are entering a new
2595 block. Consume the entire block. */
2603 /* Consume the token. */
2604 cp_lexer_consume_token (parser->lexer);
2608 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2609 parameter is the PRAGMA token, allowing us to purge the entire pragma
2613 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2617 parser->lexer->in_pragma = false;
2620 token = cp_lexer_consume_token (parser->lexer);
2621 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2623 /* Ensure that the pragma is not parsed again. */
2624 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2627 /* Require pragma end of line, resyncing with it as necessary. The
2628 arguments are as for cp_parser_skip_to_pragma_eol. */
2631 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2633 parser->lexer->in_pragma = false;
2634 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2635 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2638 /* This is a simple wrapper around make_typename_type. When the id is
2639 an unresolved identifier node, we can provide a superior diagnostic
2640 using cp_parser_diagnose_invalid_type_name. */
2643 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2646 if (TREE_CODE (id) == IDENTIFIER_NODE)
2648 result = make_typename_type (scope, id, typename_type,
2649 /*complain=*/tf_none);
2650 if (result == error_mark_node)
2651 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2654 return make_typename_type (scope, id, typename_type, tf_error);
2658 /* Create a new C++ parser. */
2661 cp_parser_new (void)
2667 /* cp_lexer_new_main is called before calling ggc_alloc because
2668 cp_lexer_new_main might load a PCH file. */
2669 lexer = cp_lexer_new_main ();
2671 /* Initialize the binops_by_token so that we can get the tree
2672 directly from the token. */
2673 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2674 binops_by_token[binops[i].token_type] = binops[i];
2676 parser = GGC_CNEW (cp_parser);
2677 parser->lexer = lexer;
2678 parser->context = cp_parser_context_new (NULL);
2680 /* For now, we always accept GNU extensions. */
2681 parser->allow_gnu_extensions_p = 1;
2683 /* The `>' token is a greater-than operator, not the end of a
2685 parser->greater_than_is_operator_p = true;
2687 parser->default_arg_ok_p = true;
2689 /* We are not parsing a constant-expression. */
2690 parser->integral_constant_expression_p = false;
2691 parser->allow_non_integral_constant_expression_p = false;
2692 parser->non_integral_constant_expression_p = false;
2694 /* Local variable names are not forbidden. */
2695 parser->local_variables_forbidden_p = false;
2697 /* We are not processing an `extern "C"' declaration. */
2698 parser->in_unbraced_linkage_specification_p = false;
2700 /* We are not processing a declarator. */
2701 parser->in_declarator_p = false;
2703 /* We are not processing a template-argument-list. */
2704 parser->in_template_argument_list_p = false;
2706 /* We are not in an iteration statement. */
2707 parser->in_statement = 0;
2709 /* We are not in a switch statement. */
2710 parser->in_switch_statement_p = false;
2712 /* We are not parsing a type-id inside an expression. */
2713 parser->in_type_id_in_expr_p = false;
2715 /* Declarations aren't implicitly extern "C". */
2716 parser->implicit_extern_c = false;
2718 /* String literals should be translated to the execution character set. */
2719 parser->translate_strings_p = true;
2721 /* We are not parsing a function body. */
2722 parser->in_function_body = false;
2724 /* The unparsed function queue is empty. */
2725 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2727 /* There are no classes being defined. */
2728 parser->num_classes_being_defined = 0;
2730 /* No template parameters apply. */
2731 parser->num_template_parameter_lists = 0;
2736 /* Create a cp_lexer structure which will emit the tokens in CACHE
2737 and push it onto the parser's lexer stack. This is used for delayed
2738 parsing of in-class method bodies and default arguments, and should
2739 not be confused with tentative parsing. */
2741 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2743 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2744 lexer->next = parser->lexer;
2745 parser->lexer = lexer;
2747 /* Move the current source position to that of the first token in the
2749 cp_lexer_set_source_position_from_token (lexer->next_token);
2752 /* Pop the top lexer off the parser stack. This is never used for the
2753 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2755 cp_parser_pop_lexer (cp_parser *parser)
2757 cp_lexer *lexer = parser->lexer;
2758 parser->lexer = lexer->next;
2759 cp_lexer_destroy (lexer);
2761 /* Put the current source position back where it was before this
2762 lexer was pushed. */
2763 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2766 /* Lexical conventions [gram.lex] */
2768 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2772 cp_parser_identifier (cp_parser* parser)
2776 /* Look for the identifier. */
2777 token = cp_parser_require (parser, CPP_NAME, "identifier");
2778 /* Return the value. */
2779 return token ? token->u.value : error_mark_node;
2782 /* Parse a sequence of adjacent string constants. Returns a
2783 TREE_STRING representing the combined, nul-terminated string
2784 constant. If TRANSLATE is true, translate the string to the
2785 execution character set. If WIDE_OK is true, a wide string is
2788 C++98 [lex.string] says that if a narrow string literal token is
2789 adjacent to a wide string literal token, the behavior is undefined.
2790 However, C99 6.4.5p4 says that this results in a wide string literal.
2791 We follow C99 here, for consistency with the C front end.
2793 This code is largely lifted from lex_string() in c-lex.c.
2795 FUTURE: ObjC++ will need to handle @-strings here. */
2797 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2802 struct obstack str_ob;
2803 cpp_string str, istr, *strs;
2806 tok = cp_lexer_peek_token (parser->lexer);
2807 if (!cp_parser_is_string_literal (tok))
2809 cp_parser_error (parser, "expected string-literal");
2810 return error_mark_node;
2813 /* Try to avoid the overhead of creating and destroying an obstack
2814 for the common case of just one string. */
2815 if (!cp_parser_is_string_literal
2816 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2818 cp_lexer_consume_token (parser->lexer);
2820 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2821 str.len = TREE_STRING_LENGTH (tok->u.value);
2823 if (tok->type == CPP_WSTRING)
2830 gcc_obstack_init (&str_ob);
2835 cp_lexer_consume_token (parser->lexer);
2837 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2838 str.len = TREE_STRING_LENGTH (tok->u.value);
2839 if (tok->type == CPP_WSTRING)
2842 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2844 tok = cp_lexer_peek_token (parser->lexer);
2846 while (cp_parser_is_string_literal (tok));
2848 strs = (cpp_string *) obstack_finish (&str_ob);
2851 if (wide && !wide_ok)
2853 cp_parser_error (parser, "a wide string is invalid in this context");
2857 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2858 (parse_in, strs, count, &istr, wide))
2860 value = build_string (istr.len, (char *)istr.text);
2861 free ((void *)istr.text);
2863 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2864 value = fix_string_type (value);
2867 /* cpp_interpret_string has issued an error. */
2868 value = error_mark_node;
2871 obstack_free (&str_ob, 0);
2877 /* Basic concepts [gram.basic] */
2879 /* Parse a translation-unit.
2882 declaration-seq [opt]
2884 Returns TRUE if all went well. */
2887 cp_parser_translation_unit (cp_parser* parser)
2889 /* The address of the first non-permanent object on the declarator
2891 static void *declarator_obstack_base;
2895 /* Create the declarator obstack, if necessary. */
2896 if (!cp_error_declarator)
2898 gcc_obstack_init (&declarator_obstack);
2899 /* Create the error declarator. */
2900 cp_error_declarator = make_declarator (cdk_error);
2901 /* Create the empty parameter list. */
2902 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2903 /* Remember where the base of the declarator obstack lies. */
2904 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2907 cp_parser_declaration_seq_opt (parser);
2909 /* If there are no tokens left then all went well. */
2910 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2912 /* Get rid of the token array; we don't need it any more. */
2913 cp_lexer_destroy (parser->lexer);
2914 parser->lexer = NULL;
2916 /* This file might have been a context that's implicitly extern
2917 "C". If so, pop the lang context. (Only relevant for PCH.) */
2918 if (parser->implicit_extern_c)
2920 pop_lang_context ();
2921 parser->implicit_extern_c = false;
2925 finish_translation_unit ();
2931 cp_parser_error (parser, "expected declaration");
2935 /* Make sure the declarator obstack was fully cleaned up. */
2936 gcc_assert (obstack_next_free (&declarator_obstack)
2937 == declarator_obstack_base);
2939 /* All went well. */
2943 /* Expressions [gram.expr] */
2945 /* Parse a primary-expression.
2956 ( compound-statement )
2957 __builtin_va_arg ( assignment-expression , type-id )
2958 __builtin_offsetof ( type-id , offsetof-expression )
2960 Objective-C++ Extension:
2968 ADDRESS_P is true iff this expression was immediately preceded by
2969 "&" and therefore might denote a pointer-to-member. CAST_P is true
2970 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2971 true iff this expression is a template argument.
2973 Returns a representation of the expression. Upon return, *IDK
2974 indicates what kind of id-expression (if any) was present. */
2977 cp_parser_primary_expression (cp_parser *parser,
2980 bool template_arg_p,
2985 /* Assume the primary expression is not an id-expression. */
2986 *idk = CP_ID_KIND_NONE;
2988 /* Peek at the next token. */
2989 token = cp_lexer_peek_token (parser->lexer);
2990 switch (token->type)
3001 token = cp_lexer_consume_token (parser->lexer);
3002 /* Floating-point literals are only allowed in an integral
3003 constant expression if they are cast to an integral or
3004 enumeration type. */
3005 if (TREE_CODE (token->u.value) == REAL_CST
3006 && parser->integral_constant_expression_p
3009 /* CAST_P will be set even in invalid code like "int(2.7 +
3010 ...)". Therefore, we have to check that the next token
3011 is sure to end the cast. */
3014 cp_token *next_token;
3016 next_token = cp_lexer_peek_token (parser->lexer);
3017 if (/* The comma at the end of an
3018 enumerator-definition. */
3019 next_token->type != CPP_COMMA
3020 /* The curly brace at the end of an enum-specifier. */
3021 && next_token->type != CPP_CLOSE_BRACE
3022 /* The end of a statement. */
3023 && next_token->type != CPP_SEMICOLON
3024 /* The end of the cast-expression. */
3025 && next_token->type != CPP_CLOSE_PAREN
3026 /* The end of an array bound. */
3027 && next_token->type != CPP_CLOSE_SQUARE
3028 /* The closing ">" in a template-argument-list. */
3029 && (next_token->type != CPP_GREATER
3030 || parser->greater_than_is_operator_p))
3034 /* If we are within a cast, then the constraint that the
3035 cast is to an integral or enumeration type will be
3036 checked at that point. If we are not within a cast, then
3037 this code is invalid. */
3039 cp_parser_non_integral_constant_expression
3040 (parser, "floating-point literal");
3042 return token->u.value;
3046 /* ??? Should wide strings be allowed when parser->translate_strings_p
3047 is false (i.e. in attributes)? If not, we can kill the third
3048 argument to cp_parser_string_literal. */
3049 return cp_parser_string_literal (parser,
3050 parser->translate_strings_p,
3053 case CPP_OPEN_PAREN:
3056 bool saved_greater_than_is_operator_p;
3058 /* Consume the `('. */
3059 cp_lexer_consume_token (parser->lexer);
3060 /* Within a parenthesized expression, a `>' token is always
3061 the greater-than operator. */
3062 saved_greater_than_is_operator_p
3063 = parser->greater_than_is_operator_p;
3064 parser->greater_than_is_operator_p = true;
3065 /* If we see `( { ' then we are looking at the beginning of
3066 a GNU statement-expression. */
3067 if (cp_parser_allow_gnu_extensions_p (parser)
3068 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3070 /* Statement-expressions are not allowed by the standard. */
3072 pedwarn ("ISO C++ forbids braced-groups within expressions");
3074 /* And they're not allowed outside of a function-body; you
3075 cannot, for example, write:
3077 int i = ({ int j = 3; j + 1; });
3079 at class or namespace scope. */
3080 if (!parser->in_function_body)
3082 error ("statement-expressions are allowed only inside functions");
3083 cp_parser_skip_to_end_of_block_or_statement (parser);
3084 expr = error_mark_node;
3088 /* Start the statement-expression. */
3089 expr = begin_stmt_expr ();
3090 /* Parse the compound-statement. */
3091 cp_parser_compound_statement (parser, expr, false);
3093 expr = finish_stmt_expr (expr, false);
3098 /* Parse the parenthesized expression. */
3099 expr = cp_parser_expression (parser, cast_p);
3100 /* Let the front end know that this expression was
3101 enclosed in parentheses. This matters in case, for
3102 example, the expression is of the form `A::B', since
3103 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3105 finish_parenthesized_expr (expr);
3107 /* The `>' token might be the end of a template-id or
3108 template-parameter-list now. */
3109 parser->greater_than_is_operator_p
3110 = saved_greater_than_is_operator_p;
3111 /* Consume the `)'. */
3112 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3113 cp_parser_skip_to_end_of_statement (parser);
3119 switch (token->keyword)
3121 /* These two are the boolean literals. */
3123 cp_lexer_consume_token (parser->lexer);
3124 return boolean_true_node;
3126 cp_lexer_consume_token (parser->lexer);
3127 return boolean_false_node;
3129 /* The `__null' literal. */
3131 cp_lexer_consume_token (parser->lexer);
3134 /* Recognize the `this' keyword. */
3136 cp_lexer_consume_token (parser->lexer);
3137 if (parser->local_variables_forbidden_p)
3139 error ("%<this%> may not be used in this context");
3140 return error_mark_node;
3142 /* Pointers cannot appear in constant-expressions. */
3143 if (cp_parser_non_integral_constant_expression (parser,
3145 return error_mark_node;
3146 return finish_this_expr ();
3148 /* The `operator' keyword can be the beginning of an
3153 case RID_FUNCTION_NAME:
3154 case RID_PRETTY_FUNCTION_NAME:
3155 case RID_C99_FUNCTION_NAME:
3156 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3157 __func__ are the names of variables -- but they are
3158 treated specially. Therefore, they are handled here,
3159 rather than relying on the generic id-expression logic
3160 below. Grammatically, these names are id-expressions.
3162 Consume the token. */
3163 token = cp_lexer_consume_token (parser->lexer);
3164 /* Look up the name. */
3165 return finish_fname (token->u.value);
3172 /* The `__builtin_va_arg' construct is used to handle
3173 `va_arg'. Consume the `__builtin_va_arg' token. */
3174 cp_lexer_consume_token (parser->lexer);
3175 /* Look for the opening `('. */
3176 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3177 /* Now, parse the assignment-expression. */
3178 expression = cp_parser_assignment_expression (parser,
3180 /* Look for the `,'. */
3181 cp_parser_require (parser, CPP_COMMA, "`,'");
3182 /* Parse the type-id. */
3183 type = cp_parser_type_id (parser);
3184 /* Look for the closing `)'. */
3185 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3186 /* Using `va_arg' in a constant-expression is not
3188 if (cp_parser_non_integral_constant_expression (parser,
3190 return error_mark_node;
3191 return build_x_va_arg (expression, type);
3195 return cp_parser_builtin_offsetof (parser);
3197 /* Objective-C++ expressions. */
3199 case RID_AT_PROTOCOL:
3200 case RID_AT_SELECTOR:
3201 return cp_parser_objc_expression (parser);
3204 cp_parser_error (parser, "expected primary-expression");
3205 return error_mark_node;
3208 /* An id-expression can start with either an identifier, a
3209 `::' as the beginning of a qualified-id, or the "operator"
3213 case CPP_TEMPLATE_ID:
3214 case CPP_NESTED_NAME_SPECIFIER:
3218 const char *error_msg;
3223 /* Parse the id-expression. */
3225 = cp_parser_id_expression (parser,
3226 /*template_keyword_p=*/false,
3227 /*check_dependency_p=*/true,
3229 /*declarator_p=*/false,
3230 /*optional_p=*/false);
3231 if (id_expression == error_mark_node)
3232 return error_mark_node;
3233 token = cp_lexer_peek_token (parser->lexer);
3234 done = (token->type != CPP_OPEN_SQUARE
3235 && token->type != CPP_OPEN_PAREN
3236 && token->type != CPP_DOT
3237 && token->type != CPP_DEREF
3238 && token->type != CPP_PLUS_PLUS
3239 && token->type != CPP_MINUS_MINUS);
3240 /* If we have a template-id, then no further lookup is
3241 required. If the template-id was for a template-class, we
3242 will sometimes have a TYPE_DECL at this point. */
3243 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3244 || TREE_CODE (id_expression) == TYPE_DECL)
3245 decl = id_expression;
3246 /* Look up the name. */
3249 tree ambiguous_decls;
3251 decl = cp_parser_lookup_name (parser, id_expression,
3254 /*is_namespace=*/false,
3255 /*check_dependency=*/true,
3257 /* If the lookup was ambiguous, an error will already have
3259 if (ambiguous_decls)
3260 return error_mark_node;
3262 /* In Objective-C++, an instance variable (ivar) may be preferred
3263 to whatever cp_parser_lookup_name() found. */
3264 decl = objc_lookup_ivar (decl, id_expression);
3266 /* If name lookup gives us a SCOPE_REF, then the
3267 qualifying scope was dependent. */
3268 if (TREE_CODE (decl) == SCOPE_REF)
3270 /* Check to see if DECL is a local variable in a context
3271 where that is forbidden. */
3272 if (parser->local_variables_forbidden_p
3273 && local_variable_p (decl))
3275 /* It might be that we only found DECL because we are
3276 trying to be generous with pre-ISO scoping rules.
3277 For example, consider:
3281 for (int i = 0; i < 10; ++i) {}
3282 extern void f(int j = i);
3285 Here, name look up will originally find the out
3286 of scope `i'. We need to issue a warning message,
3287 but then use the global `i'. */
3288 decl = check_for_out_of_scope_variable (decl);
3289 if (local_variable_p (decl))
3291 error ("local variable %qD may not appear in this context",
3293 return error_mark_node;
3298 decl = (finish_id_expression
3299 (id_expression, decl, parser->scope,
3301 parser->integral_constant_expression_p,
3302 parser->allow_non_integral_constant_expression_p,
3303 &parser->non_integral_constant_expression_p,
3304 template_p, done, address_p,
3308 cp_parser_error (parser, error_msg);
3312 /* Anything else is an error. */
3314 /* ...unless we have an Objective-C++ message or string literal,
3316 if (c_dialect_objc ()
3317 && (token->type == CPP_OPEN_SQUARE
3318 || token->type == CPP_OBJC_STRING))
3319 return cp_parser_objc_expression (parser);
3321 cp_parser_error (parser, "expected primary-expression");
3322 return error_mark_node;
3326 /* Parse an id-expression.
3333 :: [opt] nested-name-specifier template [opt] unqualified-id
3335 :: operator-function-id
3338 Return a representation of the unqualified portion of the
3339 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3340 a `::' or nested-name-specifier.
3342 Often, if the id-expression was a qualified-id, the caller will
3343 want to make a SCOPE_REF to represent the qualified-id. This
3344 function does not do this in order to avoid wastefully creating
3345 SCOPE_REFs when they are not required.
3347 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3350 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3351 uninstantiated templates.
3353 If *TEMPLATE_P is non-NULL, it is set to true iff the
3354 `template' keyword is used to explicitly indicate that the entity
3355 named is a template.
3357 If DECLARATOR_P is true, the id-expression is appearing as part of
3358 a declarator, rather than as part of an expression. */
3361 cp_parser_id_expression (cp_parser *parser,
3362 bool template_keyword_p,
3363 bool check_dependency_p,
3368 bool global_scope_p;
3369 bool nested_name_specifier_p;
3371 /* Assume the `template' keyword was not used. */
3373 *template_p = template_keyword_p;
3375 /* Look for the optional `::' operator. */
3377 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3379 /* Look for the optional nested-name-specifier. */
3380 nested_name_specifier_p
3381 = (cp_parser_nested_name_specifier_opt (parser,
3382 /*typename_keyword_p=*/false,
3387 /* If there is a nested-name-specifier, then we are looking at
3388 the first qualified-id production. */
3389 if (nested_name_specifier_p)
3392 tree saved_object_scope;
3393 tree saved_qualifying_scope;
3394 tree unqualified_id;
3397 /* See if the next token is the `template' keyword. */
3399 template_p = &is_template;
3400 *template_p = cp_parser_optional_template_keyword (parser);
3401 /* Name lookup we do during the processing of the
3402 unqualified-id might obliterate SCOPE. */
3403 saved_scope = parser->scope;
3404 saved_object_scope = parser->object_scope;
3405 saved_qualifying_scope = parser->qualifying_scope;
3406 /* Process the final unqualified-id. */
3407 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3410 /*optional_p=*/false);
3411 /* Restore the SAVED_SCOPE for our caller. */
3412 parser->scope = saved_scope;
3413 parser->object_scope = saved_object_scope;
3414 parser->qualifying_scope = saved_qualifying_scope;
3416 return unqualified_id;
3418 /* Otherwise, if we are in global scope, then we are looking at one
3419 of the other qualified-id productions. */
3420 else if (global_scope_p)
3425 /* Peek at the next token. */
3426 token = cp_lexer_peek_token (parser->lexer);
3428 /* If it's an identifier, and the next token is not a "<", then
3429 we can avoid the template-id case. This is an optimization
3430 for this common case. */
3431 if (token->type == CPP_NAME
3432 && !cp_parser_nth_token_starts_template_argument_list_p
3434 return cp_parser_identifier (parser);
3436 cp_parser_parse_tentatively (parser);
3437 /* Try a template-id. */
3438 id = cp_parser_template_id (parser,
3439 /*template_keyword_p=*/false,
3440 /*check_dependency_p=*/true,
3442 /* If that worked, we're done. */
3443 if (cp_parser_parse_definitely (parser))
3446 /* Peek at the next token. (Changes in the token buffer may
3447 have invalidated the pointer obtained above.) */
3448 token = cp_lexer_peek_token (parser->lexer);
3450 switch (token->type)
3453 return cp_parser_identifier (parser);
3456 if (token->keyword == RID_OPERATOR)
3457 return cp_parser_operator_function_id (parser);
3461 cp_parser_error (parser, "expected id-expression");
3462 return error_mark_node;
3466 return cp_parser_unqualified_id (parser, template_keyword_p,
3467 /*check_dependency_p=*/true,
3472 /* Parse an unqualified-id.
3476 operator-function-id
3477 conversion-function-id
3481 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3482 keyword, in a construct like `A::template ...'.
3484 Returns a representation of unqualified-id. For the `identifier'
3485 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3486 production a BIT_NOT_EXPR is returned; the operand of the
3487 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3488 other productions, see the documentation accompanying the
3489 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3490 names are looked up in uninstantiated templates. If DECLARATOR_P
3491 is true, the unqualified-id is appearing as part of a declarator,
3492 rather than as part of an expression. */
3495 cp_parser_unqualified_id (cp_parser* parser,
3496 bool template_keyword_p,
3497 bool check_dependency_p,
3503 /* Peek at the next token. */
3504 token = cp_lexer_peek_token (parser->lexer);
3506 switch (token->type)
3512 /* We don't know yet whether or not this will be a
3514 cp_parser_parse_tentatively (parser);
3515 /* Try a template-id. */
3516 id = cp_parser_template_id (parser, template_keyword_p,
3519 /* If it worked, we're done. */
3520 if (cp_parser_parse_definitely (parser))
3522 /* Otherwise, it's an ordinary identifier. */
3523 return cp_parser_identifier (parser);
3526 case CPP_TEMPLATE_ID:
3527 return cp_parser_template_id (parser, template_keyword_p,
3534 tree qualifying_scope;
3539 /* Consume the `~' token. */
3540 cp_lexer_consume_token (parser->lexer);
3541 /* Parse the class-name. The standard, as written, seems to
3544 template <typename T> struct S { ~S (); };
3545 template <typename T> S<T>::~S() {}
3547 is invalid, since `~' must be followed by a class-name, but
3548 `S<T>' is dependent, and so not known to be a class.
3549 That's not right; we need to look in uninstantiated
3550 templates. A further complication arises from:
3552 template <typename T> void f(T t) {
3556 Here, it is not possible to look up `T' in the scope of `T'
3557 itself. We must look in both the current scope, and the
3558 scope of the containing complete expression.
3560 Yet another issue is:
3569 The standard does not seem to say that the `S' in `~S'
3570 should refer to the type `S' and not the data member
3573 /* DR 244 says that we look up the name after the "~" in the
3574 same scope as we looked up the qualifying name. That idea
3575 isn't fully worked out; it's more complicated than that. */
3576 scope = parser->scope;
3577 object_scope = parser->object_scope;
3578 qualifying_scope = parser->qualifying_scope;
3580 /* Check for invalid scopes. */
3581 if (scope == error_mark_node)
3583 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3584 cp_lexer_consume_token (parser->lexer);
3585 return error_mark_node;
3587 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3589 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3590 error ("scope %qT before %<~%> is not a class-name", scope);
3591 cp_parser_simulate_error (parser);
3592 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3593 cp_lexer_consume_token (parser->lexer);
3594 return error_mark_node;
3596 gcc_assert (!scope || TYPE_P (scope));
3598 /* If the name is of the form "X::~X" it's OK. */
3599 token = cp_lexer_peek_token (parser->lexer);
3601 && token->type == CPP_NAME
3602 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3604 && constructor_name_p (token->u.value, scope))
3606 cp_lexer_consume_token (parser->lexer);
3607 return build_nt (BIT_NOT_EXPR, scope);
3610 /* If there was an explicit qualification (S::~T), first look
3611 in the scope given by the qualification (i.e., S). */
3613 type_decl = NULL_TREE;
3616 cp_parser_parse_tentatively (parser);
3617 type_decl = cp_parser_class_name (parser,
3618 /*typename_keyword_p=*/false,
3619 /*template_keyword_p=*/false,
3621 /*check_dependency=*/false,
3622 /*class_head_p=*/false,
3624 if (cp_parser_parse_definitely (parser))
3627 /* In "N::S::~S", look in "N" as well. */
3628 if (!done && scope && qualifying_scope)
3630 cp_parser_parse_tentatively (parser);
3631 parser->scope = qualifying_scope;
3632 parser->object_scope = NULL_TREE;
3633 parser->qualifying_scope = NULL_TREE;
3635 = cp_parser_class_name (parser,
3636 /*typename_keyword_p=*/false,
3637 /*template_keyword_p=*/false,
3639 /*check_dependency=*/false,
3640 /*class_head_p=*/false,
3642 if (cp_parser_parse_definitely (parser))
3645 /* In "p->S::~T", look in the scope given by "*p" as well. */
3646 else if (!done && object_scope)
3648 cp_parser_parse_tentatively (parser);
3649 parser->scope = object_scope;
3650 parser->object_scope = NULL_TREE;
3651 parser->qualifying_scope = NULL_TREE;
3653 = cp_parser_class_name (parser,
3654 /*typename_keyword_p=*/false,
3655 /*template_keyword_p=*/false,
3657 /*check_dependency=*/false,
3658 /*class_head_p=*/false,
3660 if (cp_parser_parse_definitely (parser))
3663 /* Look in the surrounding context. */
3666 parser->scope = NULL_TREE;
3667 parser->object_scope = NULL_TREE;
3668 parser->qualifying_scope = NULL_TREE;
3670 = cp_parser_class_name (parser,
3671 /*typename_keyword_p=*/false,
3672 /*template_keyword_p=*/false,
3674 /*check_dependency=*/false,
3675 /*class_head_p=*/false,
3678 /* If an error occurred, assume that the name of the
3679 destructor is the same as the name of the qualifying
3680 class. That allows us to keep parsing after running
3681 into ill-formed destructor names. */
3682 if (type_decl == error_mark_node && scope)
3683 return build_nt (BIT_NOT_EXPR, scope);
3684 else if (type_decl == error_mark_node)
3685 return error_mark_node;
3687 /* Check that destructor name and scope match. */
3688 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3690 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3691 error ("declaration of %<~%T%> as member of %qT",
3693 cp_parser_simulate_error (parser);
3694 return error_mark_node;
3699 A typedef-name that names a class shall not be used as the
3700 identifier in the declarator for a destructor declaration. */
3702 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3703 && !DECL_SELF_REFERENCE_P (type_decl)
3704 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3705 error ("typedef-name %qD used as destructor declarator",
3708 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3712 if (token->keyword == RID_OPERATOR)
3716 /* This could be a template-id, so we try that first. */
3717 cp_parser_parse_tentatively (parser);
3718 /* Try a template-id. */
3719 id = cp_parser_template_id (parser, template_keyword_p,
3720 /*check_dependency_p=*/true,
3722 /* If that worked, we're done. */
3723 if (cp_parser_parse_definitely (parser))
3725 /* We still don't know whether we're looking at an
3726 operator-function-id or a conversion-function-id. */
3727 cp_parser_parse_tentatively (parser);
3728 /* Try an operator-function-id. */
3729 id = cp_parser_operator_function_id (parser);
3730 /* If that didn't work, try a conversion-function-id. */
3731 if (!cp_parser_parse_definitely (parser))
3732 id = cp_parser_conversion_function_id (parser);
3741 cp_parser_error (parser, "expected unqualified-id");
3742 return error_mark_node;
3746 /* Parse an (optional) nested-name-specifier.
3748 nested-name-specifier:
3749 class-or-namespace-name :: nested-name-specifier [opt]
3750 class-or-namespace-name :: template nested-name-specifier [opt]
3752 PARSER->SCOPE should be set appropriately before this function is
3753 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3754 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3757 Sets PARSER->SCOPE to the class (TYPE) or namespace
3758 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3759 it unchanged if there is no nested-name-specifier. Returns the new
3760 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3762 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3763 part of a declaration and/or decl-specifier. */
3766 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3767 bool typename_keyword_p,
3768 bool check_dependency_p,
3770 bool is_declaration)
3772 bool success = false;
3773 cp_token_position start = 0;
3776 /* Remember where the nested-name-specifier starts. */
3777 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3779 start = cp_lexer_token_position (parser->lexer, false);
3780 push_deferring_access_checks (dk_deferred);
3787 tree saved_qualifying_scope;
3788 bool template_keyword_p;
3790 /* Spot cases that cannot be the beginning of a
3791 nested-name-specifier. */
3792 token = cp_lexer_peek_token (parser->lexer);
3794 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3795 the already parsed nested-name-specifier. */
3796 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3798 /* Grab the nested-name-specifier and continue the loop. */
3799 cp_parser_pre_parsed_nested_name_specifier (parser);
3800 /* If we originally encountered this nested-name-specifier
3801 with IS_DECLARATION set to false, we will not have
3802 resolved TYPENAME_TYPEs, so we must do so here. */
3804 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3806 new_scope = resolve_typename_type (parser->scope,
3807 /*only_current_p=*/false);
3808 if (new_scope != error_mark_node)
3809 parser->scope = new_scope;
3815 /* Spot cases that cannot be the beginning of a
3816 nested-name-specifier. On the second and subsequent times
3817 through the loop, we look for the `template' keyword. */
3818 if (success && token->keyword == RID_TEMPLATE)
3820 /* A template-id can start a nested-name-specifier. */
3821 else if (token->type == CPP_TEMPLATE_ID)
3825 /* If the next token is not an identifier, then it is
3826 definitely not a class-or-namespace-name. */
3827 if (token->type != CPP_NAME)
3829 /* If the following token is neither a `<' (to begin a
3830 template-id), nor a `::', then we are not looking at a
3831 nested-name-specifier. */
3832 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3833 if (token->type != CPP_SCOPE
3834 && !cp_parser_nth_token_starts_template_argument_list_p
3839 /* The nested-name-specifier is optional, so we parse
3841 cp_parser_parse_tentatively (parser);
3843 /* Look for the optional `template' keyword, if this isn't the
3844 first time through the loop. */
3846 template_keyword_p = cp_parser_optional_template_keyword (parser);
3848 template_keyword_p = false;
3850 /* Save the old scope since the name lookup we are about to do
3851 might destroy it. */
3852 old_scope = parser->scope;
3853 saved_qualifying_scope = parser->qualifying_scope;
3854 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3855 look up names in "X<T>::I" in order to determine that "Y" is
3856 a template. So, if we have a typename at this point, we make
3857 an effort to look through it. */
3859 && !typename_keyword_p
3861 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3862 parser->scope = resolve_typename_type (parser->scope,
3863 /*only_current_p=*/false);
3864 /* Parse the qualifying entity. */
3866 = cp_parser_class_or_namespace_name (parser,
3872 /* Look for the `::' token. */
3873 cp_parser_require (parser, CPP_SCOPE, "`::'");
3875 /* If we found what we wanted, we keep going; otherwise, we're
3877 if (!cp_parser_parse_definitely (parser))
3879 bool error_p = false;
3881 /* Restore the OLD_SCOPE since it was valid before the
3882 failed attempt at finding the last
3883 class-or-namespace-name. */
3884 parser->scope = old_scope;
3885 parser->qualifying_scope = saved_qualifying_scope;
3886 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3888 /* If the next token is an identifier, and the one after
3889 that is a `::', then any valid interpretation would have
3890 found a class-or-namespace-name. */
3891 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3892 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3894 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3897 token = cp_lexer_consume_token (parser->lexer);
3900 if (!token->ambiguous_p)
3903 tree ambiguous_decls;
3905 decl = cp_parser_lookup_name (parser, token->u.value,
3907 /*is_template=*/false,
3908 /*is_namespace=*/false,
3909 /*check_dependency=*/true,
3911 if (TREE_CODE (decl) == TEMPLATE_DECL)
3912 error ("%qD used without template parameters", decl);
3913 else if (ambiguous_decls)
3915 error ("reference to %qD is ambiguous",
3917 print_candidates (ambiguous_decls);
3918 decl = error_mark_node;
3921 cp_parser_name_lookup_error
3922 (parser, token->u.value, decl,
3923 "is not a class or namespace");
3925 parser->scope = error_mark_node;
3927 /* Treat this as a successful nested-name-specifier
3932 If the name found is not a class-name (clause
3933 _class_) or namespace-name (_namespace.def_), the
3934 program is ill-formed. */
3937 cp_lexer_consume_token (parser->lexer);
3941 /* We've found one valid nested-name-specifier. */
3943 /* Name lookup always gives us a DECL. */
3944 if (TREE_CODE (new_scope) == TYPE_DECL)
3945 new_scope = TREE_TYPE (new_scope);
3946 /* Uses of "template" must be followed by actual templates. */
3947 if (template_keyword_p
3948 && !(CLASS_TYPE_P (new_scope)
3949 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3950 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3951 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3952 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3953 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3954 == TEMPLATE_ID_EXPR)))
3955 pedwarn (TYPE_P (new_scope)
3956 ? "%qT is not a template"
3957 : "%qD is not a template",
3959 /* If it is a class scope, try to complete it; we are about to
3960 be looking up names inside the class. */
3961 if (TYPE_P (new_scope)
3962 /* Since checking types for dependency can be expensive,
3963 avoid doing it if the type is already complete. */
3964 && !COMPLETE_TYPE_P (new_scope)
3965 /* Do not try to complete dependent types. */
3966 && !dependent_type_p (new_scope))
3967 new_scope = complete_type (new_scope);
3968 /* Make sure we look in the right scope the next time through
3970 parser->scope = new_scope;
3973 /* If parsing tentatively, replace the sequence of tokens that makes
3974 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3975 token. That way, should we re-parse the token stream, we will
3976 not have to repeat the effort required to do the parse, nor will
3977 we issue duplicate error messages. */
3978 if (success && start)
3982 token = cp_lexer_token_at (parser->lexer, start);
3983 /* Reset the contents of the START token. */
3984 token->type = CPP_NESTED_NAME_SPECIFIER;
3985 /* Retrieve any deferred checks. Do not pop this access checks yet
3986 so the memory will not be reclaimed during token replacing below. */
3987 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3988 token->u.tree_check_value->value = parser->scope;
3989 token->u.tree_check_value->checks = get_deferred_access_checks ();
3990 token->u.tree_check_value->qualifying_scope =
3991 parser->qualifying_scope;
3992 token->keyword = RID_MAX;
3994 /* Purge all subsequent tokens. */
3995 cp_lexer_purge_tokens_after (parser->lexer, start);
3999 pop_to_parent_deferring_access_checks ();
4001 return success ? parser->scope : NULL_TREE;
4004 /* Parse a nested-name-specifier. See
4005 cp_parser_nested_name_specifier_opt for details. This function
4006 behaves identically, except that it will an issue an error if no
4007 nested-name-specifier is present. */
4010 cp_parser_nested_name_specifier (cp_parser *parser,
4011 bool typename_keyword_p,
4012 bool check_dependency_p,
4014 bool is_declaration)
4018 /* Look for the nested-name-specifier. */
4019 scope = cp_parser_nested_name_specifier_opt (parser,
4024 /* If it was not present, issue an error message. */
4027 cp_parser_error (parser, "expected nested-name-specifier");
4028 parser->scope = NULL_TREE;
4034 /* Parse a class-or-namespace-name.
4036 class-or-namespace-name:
4040 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4041 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4042 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4043 TYPE_P is TRUE iff the next name should be taken as a class-name,
4044 even the same name is declared to be another entity in the same
4047 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4048 specified by the class-or-namespace-name. If neither is found the
4049 ERROR_MARK_NODE is returned. */
4052 cp_parser_class_or_namespace_name (cp_parser *parser,
4053 bool typename_keyword_p,
4054 bool template_keyword_p,
4055 bool check_dependency_p,
4057 bool is_declaration)
4060 tree saved_qualifying_scope;
4061 tree saved_object_scope;
4065 /* Before we try to parse the class-name, we must save away the
4066 current PARSER->SCOPE since cp_parser_class_name will destroy
4068 saved_scope = parser->scope;
4069 saved_qualifying_scope = parser->qualifying_scope;
4070 saved_object_scope = parser->object_scope;
4071 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4072 there is no need to look for a namespace-name. */
4073 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4075 cp_parser_parse_tentatively (parser);
4076 scope = cp_parser_class_name (parser,
4079 type_p ? class_type : none_type,
4081 /*class_head_p=*/false,
4083 /* If that didn't work, try for a namespace-name. */
4084 if (!only_class_p && !cp_parser_parse_definitely (parser))
4086 /* Restore the saved scope. */
4087 parser->scope = saved_scope;
4088 parser->qualifying_scope = saved_qualifying_scope;
4089 parser->object_scope = saved_object_scope;
4090 /* If we are not looking at an identifier followed by the scope
4091 resolution operator, then this is not part of a
4092 nested-name-specifier. (Note that this function is only used
4093 to parse the components of a nested-name-specifier.) */
4094 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4095 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4096 return error_mark_node;
4097 scope = cp_parser_namespace_name (parser);
4103 /* Parse a postfix-expression.
4107 postfix-expression [ expression ]
4108 postfix-expression ( expression-list [opt] )
4109 simple-type-specifier ( expression-list [opt] )
4110 typename :: [opt] nested-name-specifier identifier
4111 ( expression-list [opt] )
4112 typename :: [opt] nested-name-specifier template [opt] template-id
4113 ( expression-list [opt] )
4114 postfix-expression . template [opt] id-expression
4115 postfix-expression -> template [opt] id-expression
4116 postfix-expression . pseudo-destructor-name
4117 postfix-expression -> pseudo-destructor-name
4118 postfix-expression ++
4119 postfix-expression --
4120 dynamic_cast < type-id > ( expression )
4121 static_cast < type-id > ( expression )
4122 reinterpret_cast < type-id > ( expression )
4123 const_cast < type-id > ( expression )
4124 typeid ( expression )
4130 ( type-id ) { initializer-list , [opt] }
4132 This extension is a GNU version of the C99 compound-literal
4133 construct. (The C99 grammar uses `type-name' instead of `type-id',
4134 but they are essentially the same concept.)
4136 If ADDRESS_P is true, the postfix expression is the operand of the
4137 `&' operator. CAST_P is true if this expression is the target of a
4140 Returns a representation of the expression. */
4143 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4147 cp_id_kind idk = CP_ID_KIND_NONE;
4148 tree postfix_expression = NULL_TREE;
4150 /* Peek at the next token. */
4151 token = cp_lexer_peek_token (parser->lexer);
4152 /* Some of the productions are determined by keywords. */
4153 keyword = token->keyword;
4163 const char *saved_message;
4165 /* All of these can be handled in the same way from the point
4166 of view of parsing. Begin by consuming the token
4167 identifying the cast. */
4168 cp_lexer_consume_token (parser->lexer);
4170 /* New types cannot be defined in the cast. */
4171 saved_message = parser->type_definition_forbidden_message;
4172 parser->type_definition_forbidden_message
4173 = "types may not be defined in casts";
4175 /* Look for the opening `<'. */
4176 cp_parser_require (parser, CPP_LESS, "`<'");
4177 /* Parse the type to which we are casting. */
4178 type = cp_parser_type_id (parser);
4179 /* Look for the closing `>'. */
4180 cp_parser_require (parser, CPP_GREATER, "`>'");
4181 /* Restore the old message. */
4182 parser->type_definition_forbidden_message = saved_message;
4184 /* And the expression which is being cast. */
4185 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4186 expression = cp_parser_expression (parser, /*cast_p=*/true);
4187 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4189 /* Only type conversions to integral or enumeration types
4190 can be used in constant-expressions. */
4191 if (!cast_valid_in_integral_constant_expression_p (type)
4192 && (cp_parser_non_integral_constant_expression
4194 "a cast to a type other than an integral or "
4195 "enumeration type")))
4196 return error_mark_node;
4202 = build_dynamic_cast (type, expression);
4206 = build_static_cast (type, expression);
4210 = build_reinterpret_cast (type, expression);
4214 = build_const_cast (type, expression);
4225 const char *saved_message;
4226 bool saved_in_type_id_in_expr_p;
4228 /* Consume the `typeid' token. */
4229 cp_lexer_consume_token (parser->lexer);
4230 /* Look for the `(' token. */
4231 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4232 /* Types cannot be defined in a `typeid' expression. */
4233 saved_message = parser->type_definition_forbidden_message;
4234 parser->type_definition_forbidden_message
4235 = "types may not be defined in a `typeid\' expression";
4236 /* We can't be sure yet whether we're looking at a type-id or an
4238 cp_parser_parse_tentatively (parser);
4239 /* Try a type-id first. */
4240 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4241 parser->in_type_id_in_expr_p = true;
4242 type = cp_parser_type_id (parser);
4243 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4244 /* Look for the `)' token. Otherwise, we can't be sure that
4245 we're not looking at an expression: consider `typeid (int
4246 (3))', for example. */
4247 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4248 /* If all went well, simply lookup the type-id. */
4249 if (cp_parser_parse_definitely (parser))
4250 postfix_expression = get_typeid (type);
4251 /* Otherwise, fall back to the expression variant. */
4256 /* Look for an expression. */
4257 expression = cp_parser_expression (parser, /*cast_p=*/false);
4258 /* Compute its typeid. */
4259 postfix_expression = build_typeid (expression);
4260 /* Look for the `)' token. */
4261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4263 /* Restore the saved message. */
4264 parser->type_definition_forbidden_message = saved_message;
4265 /* `typeid' may not appear in an integral constant expression. */
4266 if (cp_parser_non_integral_constant_expression(parser,
4267 "`typeid' operator"))
4268 return error_mark_node;
4275 /* The syntax permitted here is the same permitted for an
4276 elaborated-type-specifier. */
4277 type = cp_parser_elaborated_type_specifier (parser,
4278 /*is_friend=*/false,
4279 /*is_declaration=*/false);
4280 postfix_expression = cp_parser_functional_cast (parser, type);
4288 /* If the next thing is a simple-type-specifier, we may be
4289 looking at a functional cast. We could also be looking at
4290 an id-expression. So, we try the functional cast, and if
4291 that doesn't work we fall back to the primary-expression. */
4292 cp_parser_parse_tentatively (parser);
4293 /* Look for the simple-type-specifier. */
4294 type = cp_parser_simple_type_specifier (parser,
4295 /*decl_specs=*/NULL,
4296 CP_PARSER_FLAGS_NONE);
4297 /* Parse the cast itself. */
4298 if (!cp_parser_error_occurred (parser))
4300 = cp_parser_functional_cast (parser, type);
4301 /* If that worked, we're done. */
4302 if (cp_parser_parse_definitely (parser))
4305 /* If the functional-cast didn't work out, try a
4306 compound-literal. */
4307 if (cp_parser_allow_gnu_extensions_p (parser)
4308 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4310 VEC(constructor_elt,gc) *initializer_list = NULL;
4311 bool saved_in_type_id_in_expr_p;
4313 cp_parser_parse_tentatively (parser);
4314 /* Consume the `('. */
4315 cp_lexer_consume_token (parser->lexer);
4316 /* Parse the type. */
4317 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4318 parser->in_type_id_in_expr_p = true;
4319 type = cp_parser_type_id (parser);
4320 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4321 /* Look for the `)'. */
4322 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4323 /* Look for the `{'. */
4324 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4325 /* If things aren't going well, there's no need to
4327 if (!cp_parser_error_occurred (parser))
4329 bool non_constant_p;
4330 /* Parse the initializer-list. */
4332 = cp_parser_initializer_list (parser, &non_constant_p);
4333 /* Allow a trailing `,'. */
4334 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4335 cp_lexer_consume_token (parser->lexer);
4336 /* Look for the final `}'. */
4337 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4339 /* If that worked, we're definitely looking at a
4340 compound-literal expression. */
4341 if (cp_parser_parse_definitely (parser))
4343 /* Warn the user that a compound literal is not
4344 allowed in standard C++. */
4346 pedwarn ("ISO C++ forbids compound-literals");
4347 /* For simplicity, we disallow compound literals in
4348 constant-expressions. We could
4349 allow compound literals of integer type, whose
4350 initializer was a constant, in constant
4351 expressions. Permitting that usage, as a further
4352 extension, would not change the meaning of any
4353 currently accepted programs. (Of course, as
4354 compound literals are not part of ISO C++, the
4355 standard has nothing to say.) */
4356 if (cp_parser_non_integral_constant_expression
4357 (parser, "non-constant compound literals"))
4359 postfix_expression = error_mark_node;
4362 /* Form the representation of the compound-literal. */
4364 = finish_compound_literal (type, initializer_list);
4369 /* It must be a primary-expression. */
4371 = cp_parser_primary_expression (parser, address_p, cast_p,
4372 /*template_arg_p=*/false,
4378 /* Keep looping until the postfix-expression is complete. */
4381 if (idk == CP_ID_KIND_UNQUALIFIED
4382 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4383 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4384 /* It is not a Koenig lookup function call. */
4386 = unqualified_name_lookup_error (postfix_expression);
4388 /* Peek at the next token. */
4389 token = cp_lexer_peek_token (parser->lexer);
4391 switch (token->type)
4393 case CPP_OPEN_SQUARE:
4395 = cp_parser_postfix_open_square_expression (parser,
4398 idk = CP_ID_KIND_NONE;
4401 case CPP_OPEN_PAREN:
4402 /* postfix-expression ( expression-list [opt] ) */
4405 bool is_builtin_constant_p;
4406 bool saved_integral_constant_expression_p = false;
4407 bool saved_non_integral_constant_expression_p = false;
4410 is_builtin_constant_p
4411 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4412 if (is_builtin_constant_p)
4414 /* The whole point of __builtin_constant_p is to allow
4415 non-constant expressions to appear as arguments. */
4416 saved_integral_constant_expression_p
4417 = parser->integral_constant_expression_p;
4418 saved_non_integral_constant_expression_p
4419 = parser->non_integral_constant_expression_p;
4420 parser->integral_constant_expression_p = false;
4422 args = (cp_parser_parenthesized_expression_list
4423 (parser, /*is_attribute_list=*/false,
4424 /*cast_p=*/false, /*allow_expansion_p=*/true,
4425 /*non_constant_p=*/NULL));
4426 if (is_builtin_constant_p)
4428 parser->integral_constant_expression_p
4429 = saved_integral_constant_expression_p;
4430 parser->non_integral_constant_expression_p
4431 = saved_non_integral_constant_expression_p;
4434 if (args == error_mark_node)
4436 postfix_expression = error_mark_node;
4440 /* Function calls are not permitted in
4441 constant-expressions. */
4442 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4443 && cp_parser_non_integral_constant_expression (parser,
4446 postfix_expression = error_mark_node;
4451 if (idk == CP_ID_KIND_UNQUALIFIED)
4453 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4459 = perform_koenig_lookup (postfix_expression, args);
4463 = unqualified_fn_lookup_error (postfix_expression);
4465 /* We do not perform argument-dependent lookup if
4466 normal lookup finds a non-function, in accordance
4467 with the expected resolution of DR 218. */
4468 else if (args && is_overloaded_fn (postfix_expression))
4470 tree fn = get_first_fn (postfix_expression);
4472 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4473 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4475 /* Only do argument dependent lookup if regular
4476 lookup does not find a set of member functions.
4477 [basic.lookup.koenig]/2a */
4478 if (!DECL_FUNCTION_MEMBER_P (fn))
4482 = perform_koenig_lookup (postfix_expression, args);
4487 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4489 tree instance = TREE_OPERAND (postfix_expression, 0);
4490 tree fn = TREE_OPERAND (postfix_expression, 1);
4492 if (processing_template_decl
4493 && (type_dependent_expression_p (instance)
4494 || (!BASELINK_P (fn)
4495 && TREE_CODE (fn) != FIELD_DECL)
4496 || type_dependent_expression_p (fn)
4497 || any_type_dependent_arguments_p (args)))
4500 = build_nt_call_list (postfix_expression, args);
4504 if (BASELINK_P (fn))
4506 = (build_new_method_call
4507 (instance, fn, args, NULL_TREE,
4508 (idk == CP_ID_KIND_QUALIFIED
4509 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4513 = finish_call_expr (postfix_expression, args,
4514 /*disallow_virtual=*/false,
4515 /*koenig_p=*/false);
4517 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4518 || TREE_CODE (postfix_expression) == MEMBER_REF
4519 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4520 postfix_expression = (build_offset_ref_call_from_tree
4521 (postfix_expression, args));
4522 else if (idk == CP_ID_KIND_QUALIFIED)
4523 /* A call to a static class member, or a namespace-scope
4526 = finish_call_expr (postfix_expression, args,
4527 /*disallow_virtual=*/true,
4530 /* All other function calls. */
4532 = finish_call_expr (postfix_expression, args,
4533 /*disallow_virtual=*/false,
4536 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4537 idk = CP_ID_KIND_NONE;
4543 /* postfix-expression . template [opt] id-expression
4544 postfix-expression . pseudo-destructor-name
4545 postfix-expression -> template [opt] id-expression
4546 postfix-expression -> pseudo-destructor-name */
4548 /* Consume the `.' or `->' operator. */
4549 cp_lexer_consume_token (parser->lexer);
4552 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4558 /* postfix-expression ++ */
4559 /* Consume the `++' token. */
4560 cp_lexer_consume_token (parser->lexer);
4561 /* Generate a representation for the complete expression. */
4563 = finish_increment_expr (postfix_expression,
4564 POSTINCREMENT_EXPR);
4565 /* Increments may not appear in constant-expressions. */
4566 if (cp_parser_non_integral_constant_expression (parser,
4568 postfix_expression = error_mark_node;
4569 idk = CP_ID_KIND_NONE;
4572 case CPP_MINUS_MINUS:
4573 /* postfix-expression -- */
4574 /* Consume the `--' token. */
4575 cp_lexer_consume_token (parser->lexer);
4576 /* Generate a representation for the complete expression. */
4578 = finish_increment_expr (postfix_expression,
4579 POSTDECREMENT_EXPR);
4580 /* Decrements may not appear in constant-expressions. */
4581 if (cp_parser_non_integral_constant_expression (parser,
4583 postfix_expression = error_mark_node;
4584 idk = CP_ID_KIND_NONE;
4588 return postfix_expression;
4592 /* We should never get here. */
4594 return error_mark_node;
4597 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4598 by cp_parser_builtin_offsetof. We're looking for
4600 postfix-expression [ expression ]
4602 FOR_OFFSETOF is set if we're being called in that context, which
4603 changes how we deal with integer constant expressions. */
4606 cp_parser_postfix_open_square_expression (cp_parser *parser,
4607 tree postfix_expression,
4612 /* Consume the `[' token. */
4613 cp_lexer_consume_token (parser->lexer);
4615 /* Parse the index expression. */
4616 /* ??? For offsetof, there is a question of what to allow here. If
4617 offsetof is not being used in an integral constant expression context,
4618 then we *could* get the right answer by computing the value at runtime.
4619 If we are in an integral constant expression context, then we might
4620 could accept any constant expression; hard to say without analysis.
4621 Rather than open the barn door too wide right away, allow only integer
4622 constant expressions here. */
4624 index = cp_parser_constant_expression (parser, false, NULL);
4626 index = cp_parser_expression (parser, /*cast_p=*/false);
4628 /* Look for the closing `]'. */
4629 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4631 /* Build the ARRAY_REF. */
4632 postfix_expression = grok_array_decl (postfix_expression, index);
4634 /* When not doing offsetof, array references are not permitted in
4635 constant-expressions. */
4637 && (cp_parser_non_integral_constant_expression
4638 (parser, "an array reference")))
4639 postfix_expression = error_mark_node;
4641 return postfix_expression;
4644 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4645 by cp_parser_builtin_offsetof. We're looking for
4647 postfix-expression . template [opt] id-expression
4648 postfix-expression . pseudo-destructor-name
4649 postfix-expression -> template [opt] id-expression
4650 postfix-expression -> pseudo-destructor-name
4652 FOR_OFFSETOF is set if we're being called in that context. That sorta
4653 limits what of the above we'll actually accept, but nevermind.
4654 TOKEN_TYPE is the "." or "->" token, which will already have been
4655 removed from the stream. */
4658 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4659 enum cpp_ttype token_type,
4660 tree postfix_expression,
4661 bool for_offsetof, cp_id_kind *idk)
4665 bool pseudo_destructor_p;
4666 tree scope = NULL_TREE;
4668 /* If this is a `->' operator, dereference the pointer. */
4669 if (token_type == CPP_DEREF)
4670 postfix_expression = build_x_arrow (postfix_expression);
4671 /* Check to see whether or not the expression is type-dependent. */
4672 dependent_p = type_dependent_expression_p (postfix_expression);
4673 /* The identifier following the `->' or `.' is not qualified. */
4674 parser->scope = NULL_TREE;
4675 parser->qualifying_scope = NULL_TREE;
4676 parser->object_scope = NULL_TREE;
4677 *idk = CP_ID_KIND_NONE;
4678 /* Enter the scope corresponding to the type of the object
4679 given by the POSTFIX_EXPRESSION. */
4680 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4682 scope = TREE_TYPE (postfix_expression);
4683 /* According to the standard, no expression should ever have
4684 reference type. Unfortunately, we do not currently match
4685 the standard in this respect in that our internal representation
4686 of an expression may have reference type even when the standard
4687 says it does not. Therefore, we have to manually obtain the
4688 underlying type here. */
4689 scope = non_reference (scope);
4690 /* The type of the POSTFIX_EXPRESSION must be complete. */
4691 if (scope == unknown_type_node)
4693 error ("%qE does not have class type", postfix_expression);
4697 scope = complete_type_or_else (scope, NULL_TREE);
4698 /* Let the name lookup machinery know that we are processing a
4699 class member access expression. */
4700 parser->context->object_type = scope;
4701 /* If something went wrong, we want to be able to discern that case,
4702 as opposed to the case where there was no SCOPE due to the type
4703 of expression being dependent. */
4705 scope = error_mark_node;
4706 /* If the SCOPE was erroneous, make the various semantic analysis
4707 functions exit quickly -- and without issuing additional error
4709 if (scope == error_mark_node)
4710 postfix_expression = error_mark_node;
4713 /* Assume this expression is not a pseudo-destructor access. */
4714 pseudo_destructor_p = false;
4716 /* If the SCOPE is a scalar type, then, if this is a valid program,
4717 we must be looking at a pseudo-destructor-name. */
4718 if (scope && SCALAR_TYPE_P (scope))
4723 cp_parser_parse_tentatively (parser);
4724 /* Parse the pseudo-destructor-name. */
4726 cp_parser_pseudo_destructor_name (parser, &s, &type);
4727 if (cp_parser_parse_definitely (parser))
4729 pseudo_destructor_p = true;
4731 = finish_pseudo_destructor_expr (postfix_expression,
4732 s, TREE_TYPE (type));
4736 if (!pseudo_destructor_p)
4738 /* If the SCOPE is not a scalar type, we are looking at an
4739 ordinary class member access expression, rather than a
4740 pseudo-destructor-name. */
4742 /* Parse the id-expression. */
4743 name = (cp_parser_id_expression
4745 cp_parser_optional_template_keyword (parser),
4746 /*check_dependency_p=*/true,
4748 /*declarator_p=*/false,
4749 /*optional_p=*/false));
4750 /* In general, build a SCOPE_REF if the member name is qualified.
4751 However, if the name was not dependent and has already been
4752 resolved; there is no need to build the SCOPE_REF. For example;
4754 struct X { void f(); };
4755 template <typename T> void f(T* t) { t->X::f(); }
4757 Even though "t" is dependent, "X::f" is not and has been resolved
4758 to a BASELINK; there is no need to include scope information. */
4760 /* But we do need to remember that there was an explicit scope for
4761 virtual function calls. */
4763 *idk = CP_ID_KIND_QUALIFIED;
4765 /* If the name is a template-id that names a type, we will get a
4766 TYPE_DECL here. That is invalid code. */
4767 if (TREE_CODE (name) == TYPE_DECL)
4769 error ("invalid use of %qD", name);
4770 postfix_expression = error_mark_node;
4774 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4776 name = build_qualified_name (/*type=*/NULL_TREE,
4780 parser->scope = NULL_TREE;
4781 parser->qualifying_scope = NULL_TREE;
4782 parser->object_scope = NULL_TREE;
4784 if (scope && name && BASELINK_P (name))
4785 adjust_result_of_qualified_name_lookup
4786 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4788 = finish_class_member_access_expr (postfix_expression, name,
4793 /* We no longer need to look up names in the scope of the object on
4794 the left-hand side of the `.' or `->' operator. */
4795 parser->context->object_type = NULL_TREE;
4797 /* Outside of offsetof, these operators may not appear in
4798 constant-expressions. */
4800 && (cp_parser_non_integral_constant_expression
4801 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4802 postfix_expression = error_mark_node;
4804 return postfix_expression;
4807 /* Parse a parenthesized expression-list.
4810 assignment-expression
4811 expression-list, assignment-expression
4816 identifier, expression-list
4818 CAST_P is true if this expression is the target of a cast.
4820 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4823 Returns a TREE_LIST. The TREE_VALUE of each node is a
4824 representation of an assignment-expression. Note that a TREE_LIST
4825 is returned even if there is only a single expression in the list.
4826 error_mark_node is returned if the ( and or ) are
4827 missing. NULL_TREE is returned on no expressions. The parentheses
4828 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4829 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4830 indicates whether or not all of the expressions in the list were
4834 cp_parser_parenthesized_expression_list (cp_parser* parser,
4835 bool is_attribute_list,
4837 bool allow_expansion_p,
4838 bool *non_constant_p)
4840 tree expression_list = NULL_TREE;
4841 bool fold_expr_p = is_attribute_list;
4842 tree identifier = NULL_TREE;
4844 /* Assume all the expressions will be constant. */
4846 *non_constant_p = false;
4848 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4849 return error_mark_node;
4851 /* Consume expressions until there are no more. */
4852 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4857 /* At the beginning of attribute lists, check to see if the
4858 next token is an identifier. */
4859 if (is_attribute_list
4860 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4864 /* Consume the identifier. */
4865 token = cp_lexer_consume_token (parser->lexer);
4866 /* Save the identifier. */
4867 identifier = token->u.value;
4871 /* Parse the next assignment-expression. */
4874 bool expr_non_constant_p;
4875 expr = (cp_parser_constant_expression
4876 (parser, /*allow_non_constant_p=*/true,
4877 &expr_non_constant_p));
4878 if (expr_non_constant_p)
4879 *non_constant_p = true;
4882 expr = cp_parser_assignment_expression (parser, cast_p);
4885 expr = fold_non_dependent_expr (expr);
4887 /* If we have an ellipsis, then this is an expression
4889 if (allow_expansion_p
4890 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4892 /* Consume the `...'. */
4893 cp_lexer_consume_token (parser->lexer);
4895 /* Build the argument pack. */
4896 expr = make_pack_expansion (expr);
4899 /* Add it to the list. We add error_mark_node
4900 expressions to the list, so that we can still tell if
4901 the correct form for a parenthesized expression-list
4902 is found. That gives better errors. */
4903 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4905 if (expr == error_mark_node)
4909 /* After the first item, attribute lists look the same as
4910 expression lists. */
4911 is_attribute_list = false;
4914 /* If the next token isn't a `,', then we are done. */
4915 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4918 /* Otherwise, consume the `,' and keep going. */
4919 cp_lexer_consume_token (parser->lexer);
4922 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4927 /* We try and resync to an unnested comma, as that will give the
4928 user better diagnostics. */
4929 ending = cp_parser_skip_to_closing_parenthesis (parser,
4930 /*recovering=*/true,
4932 /*consume_paren=*/true);
4936 return error_mark_node;
4939 /* We built up the list in reverse order so we must reverse it now. */
4940 expression_list = nreverse (expression_list);
4942 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4944 return expression_list;
4947 /* Parse a pseudo-destructor-name.
4949 pseudo-destructor-name:
4950 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4951 :: [opt] nested-name-specifier template template-id :: ~ type-name
4952 :: [opt] nested-name-specifier [opt] ~ type-name
4954 If either of the first two productions is used, sets *SCOPE to the
4955 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4956 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4957 or ERROR_MARK_NODE if the parse fails. */
4960 cp_parser_pseudo_destructor_name (cp_parser* parser,
4964 bool nested_name_specifier_p;
4966 /* Assume that things will not work out. */
4967 *type = error_mark_node;
4969 /* Look for the optional `::' operator. */
4970 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4971 /* Look for the optional nested-name-specifier. */
4972 nested_name_specifier_p
4973 = (cp_parser_nested_name_specifier_opt (parser,
4974 /*typename_keyword_p=*/false,
4975 /*check_dependency_p=*/true,
4977 /*is_declaration=*/true)
4979 /* Now, if we saw a nested-name-specifier, we might be doing the
4980 second production. */
4981 if (nested_name_specifier_p
4982 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4984 /* Consume the `template' keyword. */
4985 cp_lexer_consume_token (parser->lexer);
4986 /* Parse the template-id. */
4987 cp_parser_template_id (parser,
4988 /*template_keyword_p=*/true,
4989 /*check_dependency_p=*/false,
4990 /*is_declaration=*/true);
4991 /* Look for the `::' token. */
4992 cp_parser_require (parser, CPP_SCOPE, "`::'");
4994 /* If the next token is not a `~', then there might be some
4995 additional qualification. */
4996 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4998 /* Look for the type-name. */
4999 *scope = TREE_TYPE (cp_parser_type_name (parser));
5001 if (*scope == error_mark_node)
5004 /* If we don't have ::~, then something has gone wrong. Since
5005 the only caller of this function is looking for something
5006 after `.' or `->' after a scalar type, most likely the
5007 program is trying to get a member of a non-aggregate
5009 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5010 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5012 cp_parser_error (parser, "request for member of non-aggregate type");
5016 /* Look for the `::' token. */
5017 cp_parser_require (parser, CPP_SCOPE, "`::'");
5022 /* Look for the `~'. */
5023 cp_parser_require (parser, CPP_COMPL, "`~'");
5024 /* Look for the type-name again. We are not responsible for
5025 checking that it matches the first type-name. */
5026 *type = cp_parser_type_name (parser);
5029 /* Parse a unary-expression.
5035 unary-operator cast-expression
5036 sizeof unary-expression
5044 __extension__ cast-expression
5045 __alignof__ unary-expression
5046 __alignof__ ( type-id )
5047 __real__ cast-expression
5048 __imag__ cast-expression
5051 ADDRESS_P is true iff the unary-expression is appearing as the
5052 operand of the `&' operator. CAST_P is true if this expression is
5053 the target of a cast.
5055 Returns a representation of the expression. */
5058 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5061 enum tree_code unary_operator;
5063 /* Peek at the next token. */
5064 token = cp_lexer_peek_token (parser->lexer);
5065 /* Some keywords give away the kind of expression. */
5066 if (token->type == CPP_KEYWORD)
5068 enum rid keyword = token->keyword;
5078 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5079 /* Consume the token. */
5080 cp_lexer_consume_token (parser->lexer);
5081 /* Parse the operand. */
5082 operand = cp_parser_sizeof_operand (parser, keyword);
5084 if (TYPE_P (operand))
5085 return cxx_sizeof_or_alignof_type (operand, op, true);
5087 return cxx_sizeof_or_alignof_expr (operand, op);
5091 return cp_parser_new_expression (parser);
5094 return cp_parser_delete_expression (parser);
5098 /* The saved value of the PEDANTIC flag. */
5102 /* Save away the PEDANTIC flag. */
5103 cp_parser_extension_opt (parser, &saved_pedantic);
5104 /* Parse the cast-expression. */
5105 expr = cp_parser_simple_cast_expression (parser);
5106 /* Restore the PEDANTIC flag. */
5107 pedantic = saved_pedantic;
5117 /* Consume the `__real__' or `__imag__' token. */
5118 cp_lexer_consume_token (parser->lexer);
5119 /* Parse the cast-expression. */
5120 expression = cp_parser_simple_cast_expression (parser);
5121 /* Create the complete representation. */
5122 return build_x_unary_op ((keyword == RID_REALPART
5123 ? REALPART_EXPR : IMAGPART_EXPR),
5133 /* Look for the `:: new' and `:: delete', which also signal the
5134 beginning of a new-expression, or delete-expression,
5135 respectively. If the next token is `::', then it might be one of
5137 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5141 /* See if the token after the `::' is one of the keywords in
5142 which we're interested. */
5143 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5144 /* If it's `new', we have a new-expression. */
5145 if (keyword == RID_NEW)
5146 return cp_parser_new_expression (parser);
5147 /* Similarly, for `delete'. */
5148 else if (keyword == RID_DELETE)
5149 return cp_parser_delete_expression (parser);
5152 /* Look for a unary operator. */
5153 unary_operator = cp_parser_unary_operator (token);
5154 /* The `++' and `--' operators can be handled similarly, even though
5155 they are not technically unary-operators in the grammar. */
5156 if (unary_operator == ERROR_MARK)
5158 if (token->type == CPP_PLUS_PLUS)
5159 unary_operator = PREINCREMENT_EXPR;
5160 else if (token->type == CPP_MINUS_MINUS)
5161 unary_operator = PREDECREMENT_EXPR;
5162 /* Handle the GNU address-of-label extension. */
5163 else if (cp_parser_allow_gnu_extensions_p (parser)
5164 && token->type == CPP_AND_AND)
5168 /* Consume the '&&' token. */
5169 cp_lexer_consume_token (parser->lexer);
5170 /* Look for the identifier. */
5171 identifier = cp_parser_identifier (parser);
5172 /* Create an expression representing the address. */
5173 return finish_label_address_expr (identifier);
5176 if (unary_operator != ERROR_MARK)
5178 tree cast_expression;
5179 tree expression = error_mark_node;
5180 const char *non_constant_p = NULL;
5182 /* Consume the operator token. */
5183 token = cp_lexer_consume_token (parser->lexer);
5184 /* Parse the cast-expression. */
5186 = cp_parser_cast_expression (parser,
5187 unary_operator == ADDR_EXPR,
5189 /* Now, build an appropriate representation. */
5190 switch (unary_operator)
5193 non_constant_p = "`*'";
5194 expression = build_x_indirect_ref (cast_expression, "unary *");
5198 non_constant_p = "`&'";
5201 expression = build_x_unary_op (unary_operator, cast_expression);
5204 case PREINCREMENT_EXPR:
5205 case PREDECREMENT_EXPR:
5206 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5209 case UNARY_PLUS_EXPR:
5211 case TRUTH_NOT_EXPR:
5212 expression = finish_unary_op_expr (unary_operator, cast_expression);
5220 && cp_parser_non_integral_constant_expression (parser,
5222 expression = error_mark_node;
5227 return cp_parser_postfix_expression (parser, address_p, cast_p);
5230 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5231 unary-operator, the corresponding tree code is returned. */
5233 static enum tree_code
5234 cp_parser_unary_operator (cp_token* token)
5236 switch (token->type)
5239 return INDIRECT_REF;
5245 return UNARY_PLUS_EXPR;
5251 return TRUTH_NOT_EXPR;
5254 return BIT_NOT_EXPR;
5261 /* Parse a new-expression.
5264 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5265 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5267 Returns a representation of the expression. */
5270 cp_parser_new_expression (cp_parser* parser)
5272 bool global_scope_p;
5278 /* Look for the optional `::' operator. */
5280 = (cp_parser_global_scope_opt (parser,
5281 /*current_scope_valid_p=*/false)
5283 /* Look for the `new' operator. */
5284 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5285 /* There's no easy way to tell a new-placement from the
5286 `( type-id )' construct. */
5287 cp_parser_parse_tentatively (parser);
5288 /* Look for a new-placement. */
5289 placement = cp_parser_new_placement (parser);
5290 /* If that didn't work out, there's no new-placement. */
5291 if (!cp_parser_parse_definitely (parser))
5292 placement = NULL_TREE;
5294 /* If the next token is a `(', then we have a parenthesized
5296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5298 /* Consume the `('. */
5299 cp_lexer_consume_token (parser->lexer);
5300 /* Parse the type-id. */
5301 type = cp_parser_type_id (parser);
5302 /* Look for the closing `)'. */
5303 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5304 /* There should not be a direct-new-declarator in this production,
5305 but GCC used to allowed this, so we check and emit a sensible error
5306 message for this case. */
5307 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5309 error ("array bound forbidden after parenthesized type-id");
5310 inform ("try removing the parentheses around the type-id");
5311 cp_parser_direct_new_declarator (parser);
5315 /* Otherwise, there must be a new-type-id. */
5317 type = cp_parser_new_type_id (parser, &nelts);
5319 /* If the next token is a `(', then we have a new-initializer. */
5320 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5321 initializer = cp_parser_new_initializer (parser);
5323 initializer = NULL_TREE;
5325 /* A new-expression may not appear in an integral constant
5327 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5328 return error_mark_node;
5330 /* Create a representation of the new-expression. */
5331 return build_new (placement, type, nelts, initializer, global_scope_p);
5334 /* Parse a new-placement.
5339 Returns the same representation as for an expression-list. */
5342 cp_parser_new_placement (cp_parser* parser)
5344 tree expression_list;
5346 /* Parse the expression-list. */
5347 expression_list = (cp_parser_parenthesized_expression_list
5348 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5349 /*non_constant_p=*/NULL));
5351 return expression_list;
5354 /* Parse a new-type-id.
5357 type-specifier-seq new-declarator [opt]
5359 Returns the TYPE allocated. If the new-type-id indicates an array
5360 type, *NELTS is set to the number of elements in the last array
5361 bound; the TYPE will not include the last array bound. */
5364 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5366 cp_decl_specifier_seq type_specifier_seq;
5367 cp_declarator *new_declarator;
5368 cp_declarator *declarator;
5369 cp_declarator *outer_declarator;
5370 const char *saved_message;
5373 /* The type-specifier sequence must not contain type definitions.
5374 (It cannot contain declarations of new types either, but if they
5375 are not definitions we will catch that because they are not
5377 saved_message = parser->type_definition_forbidden_message;
5378 parser->type_definition_forbidden_message
5379 = "types may not be defined in a new-type-id";
5380 /* Parse the type-specifier-seq. */
5381 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5382 &type_specifier_seq);
5383 /* Restore the old message. */
5384 parser->type_definition_forbidden_message = saved_message;
5385 /* Parse the new-declarator. */
5386 new_declarator = cp_parser_new_declarator_opt (parser);
5388 /* Determine the number of elements in the last array dimension, if
5391 /* Skip down to the last array dimension. */
5392 declarator = new_declarator;
5393 outer_declarator = NULL;
5394 while (declarator && (declarator->kind == cdk_pointer
5395 || declarator->kind == cdk_ptrmem))
5397 outer_declarator = declarator;
5398 declarator = declarator->declarator;
5401 && declarator->kind == cdk_array
5402 && declarator->declarator
5403 && declarator->declarator->kind == cdk_array)
5405 outer_declarator = declarator;
5406 declarator = declarator->declarator;
5409 if (declarator && declarator->kind == cdk_array)
5411 *nelts = declarator->u.array.bounds;
5412 if (*nelts == error_mark_node)
5413 *nelts = integer_one_node;
5415 if (outer_declarator)
5416 outer_declarator->declarator = declarator->declarator;
5418 new_declarator = NULL;
5421 type = groktypename (&type_specifier_seq, new_declarator);
5422 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5424 *nelts = array_type_nelts_top (type);
5425 type = TREE_TYPE (type);
5430 /* Parse an (optional) new-declarator.
5433 ptr-operator new-declarator [opt]
5434 direct-new-declarator
5436 Returns the declarator. */
5438 static cp_declarator *
5439 cp_parser_new_declarator_opt (cp_parser* parser)
5441 enum tree_code code;
5443 cp_cv_quals cv_quals;
5445 /* We don't know if there's a ptr-operator next, or not. */
5446 cp_parser_parse_tentatively (parser);
5447 /* Look for a ptr-operator. */
5448 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5449 /* If that worked, look for more new-declarators. */
5450 if (cp_parser_parse_definitely (parser))
5452 cp_declarator *declarator;
5454 /* Parse another optional declarator. */
5455 declarator = cp_parser_new_declarator_opt (parser);
5457 /* Create the representation of the declarator. */
5459 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5460 else if (code == INDIRECT_REF)
5461 declarator = make_pointer_declarator (cv_quals, declarator);
5463 declarator = make_reference_declarator (cv_quals, declarator);
5468 /* If the next token is a `[', there is a direct-new-declarator. */
5469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5470 return cp_parser_direct_new_declarator (parser);
5475 /* Parse a direct-new-declarator.
5477 direct-new-declarator:
5479 direct-new-declarator [constant-expression]
5483 static cp_declarator *
5484 cp_parser_direct_new_declarator (cp_parser* parser)
5486 cp_declarator *declarator = NULL;
5492 /* Look for the opening `['. */
5493 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5494 /* The first expression is not required to be constant. */
5497 expression = cp_parser_expression (parser, /*cast_p=*/false);
5498 /* The standard requires that the expression have integral
5499 type. DR 74 adds enumeration types. We believe that the
5500 real intent is that these expressions be handled like the
5501 expression in a `switch' condition, which also allows
5502 classes with a single conversion to integral or
5503 enumeration type. */
5504 if (!processing_template_decl)
5507 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5512 error ("expression in new-declarator must have integral "
5513 "or enumeration type");
5514 expression = error_mark_node;
5518 /* But all the other expressions must be. */
5521 = cp_parser_constant_expression (parser,
5522 /*allow_non_constant=*/false,
5524 /* Look for the closing `]'. */
5525 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5527 /* Add this bound to the declarator. */
5528 declarator = make_array_declarator (declarator, expression);
5530 /* If the next token is not a `[', then there are no more
5532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5539 /* Parse a new-initializer.
5542 ( expression-list [opt] )
5544 Returns a representation of the expression-list. If there is no
5545 expression-list, VOID_ZERO_NODE is returned. */
5548 cp_parser_new_initializer (cp_parser* parser)
5550 tree expression_list;
5552 expression_list = (cp_parser_parenthesized_expression_list
5553 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5554 /*non_constant_p=*/NULL));
5555 if (!expression_list)
5556 expression_list = void_zero_node;
5558 return expression_list;
5561 /* Parse a delete-expression.
5564 :: [opt] delete cast-expression
5565 :: [opt] delete [ ] cast-expression
5567 Returns a representation of the expression. */
5570 cp_parser_delete_expression (cp_parser* parser)
5572 bool global_scope_p;
5576 /* Look for the optional `::' operator. */
5578 = (cp_parser_global_scope_opt (parser,
5579 /*current_scope_valid_p=*/false)
5581 /* Look for the `delete' keyword. */
5582 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5583 /* See if the array syntax is in use. */
5584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5586 /* Consume the `[' token. */
5587 cp_lexer_consume_token (parser->lexer);
5588 /* Look for the `]' token. */
5589 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5590 /* Remember that this is the `[]' construct. */
5596 /* Parse the cast-expression. */
5597 expression = cp_parser_simple_cast_expression (parser);
5599 /* A delete-expression may not appear in an integral constant
5601 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5602 return error_mark_node;
5604 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5607 /* Parse a cast-expression.
5611 ( type-id ) cast-expression
5613 ADDRESS_P is true iff the unary-expression is appearing as the
5614 operand of the `&' operator. CAST_P is true if this expression is
5615 the target of a cast.
5617 Returns a representation of the expression. */
5620 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5622 /* If it's a `(', then we might be looking at a cast. */
5623 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5625 tree type = NULL_TREE;
5626 tree expr = NULL_TREE;
5627 bool compound_literal_p;
5628 const char *saved_message;
5630 /* There's no way to know yet whether or not this is a cast.
5631 For example, `(int (3))' is a unary-expression, while `(int)
5632 3' is a cast. So, we resort to parsing tentatively. */
5633 cp_parser_parse_tentatively (parser);
5634 /* Types may not be defined in a cast. */
5635 saved_message = parser->type_definition_forbidden_message;
5636 parser->type_definition_forbidden_message
5637 = "types may not be defined in casts";
5638 /* Consume the `('. */
5639 cp_lexer_consume_token (parser->lexer);
5640 /* A very tricky bit is that `(struct S) { 3 }' is a
5641 compound-literal (which we permit in C++ as an extension).
5642 But, that construct is not a cast-expression -- it is a
5643 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5644 is legal; if the compound-literal were a cast-expression,
5645 you'd need an extra set of parentheses.) But, if we parse
5646 the type-id, and it happens to be a class-specifier, then we
5647 will commit to the parse at that point, because we cannot
5648 undo the action that is done when creating a new class. So,
5649 then we cannot back up and do a postfix-expression.
5651 Therefore, we scan ahead to the closing `)', and check to see
5652 if the token after the `)' is a `{'. If so, we are not
5653 looking at a cast-expression.
5655 Save tokens so that we can put them back. */
5656 cp_lexer_save_tokens (parser->lexer);
5657 /* Skip tokens until the next token is a closing parenthesis.
5658 If we find the closing `)', and the next token is a `{', then
5659 we are looking at a compound-literal. */
5661 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5662 /*consume_paren=*/true)
5663 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5664 /* Roll back the tokens we skipped. */
5665 cp_lexer_rollback_tokens (parser->lexer);
5666 /* If we were looking at a compound-literal, simulate an error
5667 so that the call to cp_parser_parse_definitely below will
5669 if (compound_literal_p)
5670 cp_parser_simulate_error (parser);
5673 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5674 parser->in_type_id_in_expr_p = true;
5675 /* Look for the type-id. */
5676 type = cp_parser_type_id (parser);
5677 /* Look for the closing `)'. */
5678 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5679 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5682 /* Restore the saved message. */
5683 parser->type_definition_forbidden_message = saved_message;
5685 /* If ok so far, parse the dependent expression. We cannot be
5686 sure it is a cast. Consider `(T ())'. It is a parenthesized
5687 ctor of T, but looks like a cast to function returning T
5688 without a dependent expression. */
5689 if (!cp_parser_error_occurred (parser))
5690 expr = cp_parser_cast_expression (parser,
5691 /*address_p=*/false,
5694 if (cp_parser_parse_definitely (parser))
5696 /* Warn about old-style casts, if so requested. */
5697 if (warn_old_style_cast
5698 && !in_system_header
5699 && !VOID_TYPE_P (type)
5700 && current_lang_name != lang_name_c)
5701 warning (OPT_Wold_style_cast, "use of old-style cast");
5703 /* Only type conversions to integral or enumeration types
5704 can be used in constant-expressions. */
5705 if (!cast_valid_in_integral_constant_expression_p (type)
5706 && (cp_parser_non_integral_constant_expression
5708 "a cast to a type other than an integral or "
5709 "enumeration type")))
5710 return error_mark_node;
5712 /* Perform the cast. */
5713 expr = build_c_cast (type, expr);
5718 /* If we get here, then it's not a cast, so it must be a
5719 unary-expression. */
5720 return cp_parser_unary_expression (parser, address_p, cast_p);
5723 /* Parse a binary expression of the general form:
5727 pm-expression .* cast-expression
5728 pm-expression ->* cast-expression
5730 multiplicative-expression:
5732 multiplicative-expression * pm-expression
5733 multiplicative-expression / pm-expression
5734 multiplicative-expression % pm-expression
5736 additive-expression:
5737 multiplicative-expression
5738 additive-expression + multiplicative-expression
5739 additive-expression - multiplicative-expression
5743 shift-expression << additive-expression
5744 shift-expression >> additive-expression
5746 relational-expression:
5748 relational-expression < shift-expression
5749 relational-expression > shift-expression
5750 relational-expression <= shift-expression
5751 relational-expression >= shift-expression
5755 relational-expression:
5756 relational-expression <? shift-expression
5757 relational-expression >? shift-expression
5759 equality-expression:
5760 relational-expression
5761 equality-expression == relational-expression
5762 equality-expression != relational-expression
5766 and-expression & equality-expression
5768 exclusive-or-expression:
5770 exclusive-or-expression ^ and-expression
5772 inclusive-or-expression:
5773 exclusive-or-expression
5774 inclusive-or-expression | exclusive-or-expression
5776 logical-and-expression:
5777 inclusive-or-expression
5778 logical-and-expression && inclusive-or-expression
5780 logical-or-expression:
5781 logical-and-expression
5782 logical-or-expression || logical-and-expression
5784 All these are implemented with a single function like:
5787 simple-cast-expression
5788 binary-expression <token> binary-expression
5790 CAST_P is true if this expression is the target of a cast.
5792 The binops_by_token map is used to get the tree codes for each <token> type.
5793 binary-expressions are associated according to a precedence table. */
5795 #define TOKEN_PRECEDENCE(token) \
5796 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5797 ? PREC_NOT_OPERATOR \
5798 : binops_by_token[token->type].prec)
5801 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5803 cp_parser_expression_stack stack;
5804 cp_parser_expression_stack_entry *sp = &stack[0];
5807 enum tree_code tree_type, lhs_type, rhs_type;
5808 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5811 /* Parse the first expression. */
5812 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5813 lhs_type = ERROR_MARK;
5817 /* Get an operator token. */
5818 token = cp_lexer_peek_token (parser->lexer);
5820 new_prec = TOKEN_PRECEDENCE (token);
5822 /* Popping an entry off the stack means we completed a subexpression:
5823 - either we found a token which is not an operator (`>' where it is not
5824 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5825 will happen repeatedly;
5826 - or, we found an operator which has lower priority. This is the case
5827 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5829 if (new_prec <= prec)
5838 tree_type = binops_by_token[token->type].tree_type;
5840 /* We used the operator token. */
5841 cp_lexer_consume_token (parser->lexer);
5843 /* Extract another operand. It may be the RHS of this expression
5844 or the LHS of a new, higher priority expression. */
5845 rhs = cp_parser_simple_cast_expression (parser);
5846 rhs_type = ERROR_MARK;
5848 /* Get another operator token. Look up its precedence to avoid
5849 building a useless (immediately popped) stack entry for common
5850 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5851 token = cp_lexer_peek_token (parser->lexer);
5852 lookahead_prec = TOKEN_PRECEDENCE (token);
5853 if (lookahead_prec > new_prec)
5855 /* ... and prepare to parse the RHS of the new, higher priority
5856 expression. Since precedence levels on the stack are
5857 monotonically increasing, we do not have to care about
5860 sp->tree_type = tree_type;
5862 sp->lhs_type = lhs_type;
5865 lhs_type = rhs_type;
5867 new_prec = lookahead_prec;
5871 /* If the stack is not empty, we have parsed into LHS the right side
5872 (`4' in the example above) of an expression we had suspended.
5873 We can use the information on the stack to recover the LHS (`3')
5874 from the stack together with the tree code (`MULT_EXPR'), and
5875 the precedence of the higher level subexpression
5876 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5877 which will be used to actually build the additive expression. */
5880 tree_type = sp->tree_type;
5882 rhs_type = lhs_type;
5884 lhs_type = sp->lhs_type;
5887 overloaded_p = false;
5888 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5890 lhs_type = tree_type;
5892 /* If the binary operator required the use of an overloaded operator,
5893 then this expression cannot be an integral constant-expression.
5894 An overloaded operator can be used even if both operands are
5895 otherwise permissible in an integral constant-expression if at
5896 least one of the operands is of enumeration type. */
5899 && (cp_parser_non_integral_constant_expression
5900 (parser, "calls to overloaded operators")))
5901 return error_mark_node;
5908 /* Parse the `? expression : assignment-expression' part of a
5909 conditional-expression. The LOGICAL_OR_EXPR is the
5910 logical-or-expression that started the conditional-expression.
5911 Returns a representation of the entire conditional-expression.
5913 This routine is used by cp_parser_assignment_expression.
5915 ? expression : assignment-expression
5919 ? : assignment-expression */
5922 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5925 tree assignment_expr;
5927 /* Consume the `?' token. */
5928 cp_lexer_consume_token (parser->lexer);
5929 if (cp_parser_allow_gnu_extensions_p (parser)
5930 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5931 /* Implicit true clause. */
5934 /* Parse the expression. */
5935 expr = cp_parser_expression (parser, /*cast_p=*/false);
5937 /* The next token should be a `:'. */
5938 cp_parser_require (parser, CPP_COLON, "`:'");
5939 /* Parse the assignment-expression. */
5940 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5942 /* Build the conditional-expression. */
5943 return build_x_conditional_expr (logical_or_expr,
5948 /* Parse an assignment-expression.
5950 assignment-expression:
5951 conditional-expression
5952 logical-or-expression assignment-operator assignment_expression
5955 CAST_P is true if this expression is the target of a cast.
5957 Returns a representation for the expression. */
5960 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5964 /* If the next token is the `throw' keyword, then we're looking at
5965 a throw-expression. */
5966 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5967 expr = cp_parser_throw_expression (parser);
5968 /* Otherwise, it must be that we are looking at a
5969 logical-or-expression. */
5972 /* Parse the binary expressions (logical-or-expression). */
5973 expr = cp_parser_binary_expression (parser, cast_p);
5974 /* If the next token is a `?' then we're actually looking at a
5975 conditional-expression. */
5976 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5977 return cp_parser_question_colon_clause (parser, expr);
5980 enum tree_code assignment_operator;
5982 /* If it's an assignment-operator, we're using the second
5985 = cp_parser_assignment_operator_opt (parser);
5986 if (assignment_operator != ERROR_MARK)
5990 /* Parse the right-hand side of the assignment. */
5991 rhs = cp_parser_assignment_expression (parser, cast_p);
5992 /* An assignment may not appear in a
5993 constant-expression. */
5994 if (cp_parser_non_integral_constant_expression (parser,
5996 return error_mark_node;
5997 /* Build the assignment expression. */
5998 expr = build_x_modify_expr (expr,
5999 assignment_operator,
6008 /* Parse an (optional) assignment-operator.
6010 assignment-operator: one of
6011 = *= /= %= += -= >>= <<= &= ^= |=
6015 assignment-operator: one of
6018 If the next token is an assignment operator, the corresponding tree
6019 code is returned, and the token is consumed. For example, for
6020 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6021 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6022 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6023 operator, ERROR_MARK is returned. */
6025 static enum tree_code
6026 cp_parser_assignment_operator_opt (cp_parser* parser)
6031 /* Peek at the next toen. */
6032 token = cp_lexer_peek_token (parser->lexer);
6034 switch (token->type)
6045 op = TRUNC_DIV_EXPR;
6049 op = TRUNC_MOD_EXPR;
6081 /* Nothing else is an assignment operator. */
6085 /* If it was an assignment operator, consume it. */
6086 if (op != ERROR_MARK)
6087 cp_lexer_consume_token (parser->lexer);
6092 /* Parse an expression.
6095 assignment-expression
6096 expression , assignment-expression
6098 CAST_P is true if this expression is the target of a cast.
6100 Returns a representation of the expression. */
6103 cp_parser_expression (cp_parser* parser, bool cast_p)
6105 tree expression = NULL_TREE;
6109 tree assignment_expression;
6111 /* Parse the next assignment-expression. */
6112 assignment_expression
6113 = cp_parser_assignment_expression (parser, cast_p);
6114 /* If this is the first assignment-expression, we can just
6117 expression = assignment_expression;
6119 expression = build_x_compound_expr (expression,
6120 assignment_expression);
6121 /* If the next token is not a comma, then we are done with the
6123 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6125 /* Consume the `,'. */
6126 cp_lexer_consume_token (parser->lexer);
6127 /* A comma operator cannot appear in a constant-expression. */
6128 if (cp_parser_non_integral_constant_expression (parser,
6129 "a comma operator"))
6130 expression = error_mark_node;
6136 /* Parse a constant-expression.
6138 constant-expression:
6139 conditional-expression
6141 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6142 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6143 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6144 is false, NON_CONSTANT_P should be NULL. */
6147 cp_parser_constant_expression (cp_parser* parser,
6148 bool allow_non_constant_p,
6149 bool *non_constant_p)
6151 bool saved_integral_constant_expression_p;
6152 bool saved_allow_non_integral_constant_expression_p;
6153 bool saved_non_integral_constant_expression_p;
6156 /* It might seem that we could simply parse the
6157 conditional-expression, and then check to see if it were
6158 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6159 one that the compiler can figure out is constant, possibly after
6160 doing some simplifications or optimizations. The standard has a
6161 precise definition of constant-expression, and we must honor
6162 that, even though it is somewhat more restrictive.
6168 is not a legal declaration, because `(2, 3)' is not a
6169 constant-expression. The `,' operator is forbidden in a
6170 constant-expression. However, GCC's constant-folding machinery
6171 will fold this operation to an INTEGER_CST for `3'. */
6173 /* Save the old settings. */
6174 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6175 saved_allow_non_integral_constant_expression_p
6176 = parser->allow_non_integral_constant_expression_p;
6177 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6178 /* We are now parsing a constant-expression. */
6179 parser->integral_constant_expression_p = true;
6180 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6181 parser->non_integral_constant_expression_p = false;
6182 /* Although the grammar says "conditional-expression", we parse an
6183 "assignment-expression", which also permits "throw-expression"
6184 and the use of assignment operators. In the case that
6185 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6186 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6187 actually essential that we look for an assignment-expression.
6188 For example, cp_parser_initializer_clauses uses this function to
6189 determine whether a particular assignment-expression is in fact
6191 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6192 /* Restore the old settings. */
6193 parser->integral_constant_expression_p
6194 = saved_integral_constant_expression_p;
6195 parser->allow_non_integral_constant_expression_p
6196 = saved_allow_non_integral_constant_expression_p;
6197 if (allow_non_constant_p)
6198 *non_constant_p = parser->non_integral_constant_expression_p;
6199 else if (parser->non_integral_constant_expression_p)
6200 expression = error_mark_node;
6201 parser->non_integral_constant_expression_p
6202 = saved_non_integral_constant_expression_p;
6207 /* Parse __builtin_offsetof.
6209 offsetof-expression:
6210 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6212 offsetof-member-designator:
6214 | offsetof-member-designator "." id-expression
6215 | offsetof-member-designator "[" expression "]" */
6218 cp_parser_builtin_offsetof (cp_parser *parser)
6220 int save_ice_p, save_non_ice_p;
6224 /* We're about to accept non-integral-constant things, but will
6225 definitely yield an integral constant expression. Save and
6226 restore these values around our local parsing. */
6227 save_ice_p = parser->integral_constant_expression_p;
6228 save_non_ice_p = parser->non_integral_constant_expression_p;
6230 /* Consume the "__builtin_offsetof" token. */
6231 cp_lexer_consume_token (parser->lexer);
6232 /* Consume the opening `('. */
6233 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6234 /* Parse the type-id. */
6235 type = cp_parser_type_id (parser);
6236 /* Look for the `,'. */
6237 cp_parser_require (parser, CPP_COMMA, "`,'");
6239 /* Build the (type *)null that begins the traditional offsetof macro. */
6240 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6242 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6243 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6247 cp_token *token = cp_lexer_peek_token (parser->lexer);
6248 switch (token->type)
6250 case CPP_OPEN_SQUARE:
6251 /* offsetof-member-designator "[" expression "]" */
6252 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6256 /* offsetof-member-designator "." identifier */
6257 cp_lexer_consume_token (parser->lexer);
6258 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6262 case CPP_CLOSE_PAREN:
6263 /* Consume the ")" token. */
6264 cp_lexer_consume_token (parser->lexer);
6268 /* Error. We know the following require will fail, but
6269 that gives the proper error message. */
6270 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6271 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6272 expr = error_mark_node;
6278 /* If we're processing a template, we can't finish the semantics yet.
6279 Otherwise we can fold the entire expression now. */
6280 if (processing_template_decl)
6281 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6283 expr = finish_offsetof (expr);
6286 parser->integral_constant_expression_p = save_ice_p;
6287 parser->non_integral_constant_expression_p = save_non_ice_p;
6292 /* Statements [gram.stmt.stmt] */
6294 /* Parse a statement.
6298 expression-statement
6303 declaration-statement
6306 IN_COMPOUND is true when the statement is nested inside a
6307 cp_parser_compound_statement; this matters for certain pragmas.
6309 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6310 is a (possibly labeled) if statement which is not enclosed in braces
6311 and has an else clause. This is used to implement -Wparentheses. */
6314 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6315 bool in_compound, bool *if_p)
6319 location_t statement_location;
6324 /* There is no statement yet. */
6325 statement = NULL_TREE;
6326 /* Peek at the next token. */
6327 token = cp_lexer_peek_token (parser->lexer);
6328 /* Remember the location of the first token in the statement. */
6329 statement_location = token->location;
6330 /* If this is a keyword, then that will often determine what kind of
6331 statement we have. */
6332 if (token->type == CPP_KEYWORD)
6334 enum rid keyword = token->keyword;
6340 /* Looks like a labeled-statement with a case label.
6341 Parse the label, and then use tail recursion to parse
6343 cp_parser_label_for_labeled_statement (parser);
6348 statement = cp_parser_selection_statement (parser, if_p);
6354 statement = cp_parser_iteration_statement (parser);
6361 statement = cp_parser_jump_statement (parser);
6364 /* Objective-C++ exception-handling constructs. */
6367 case RID_AT_FINALLY:
6368 case RID_AT_SYNCHRONIZED:
6370 statement = cp_parser_objc_statement (parser);
6374 statement = cp_parser_try_block (parser);
6378 /* This must be a namespace alias definition. */
6379 cp_parser_declaration_statement (parser);
6383 /* It might be a keyword like `int' that can start a
6384 declaration-statement. */
6388 else if (token->type == CPP_NAME)
6390 /* If the next token is a `:', then we are looking at a
6391 labeled-statement. */
6392 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6393 if (token->type == CPP_COLON)
6395 /* Looks like a labeled-statement with an ordinary label.
6396 Parse the label, and then use tail recursion to parse
6398 cp_parser_label_for_labeled_statement (parser);
6402 /* Anything that starts with a `{' must be a compound-statement. */
6403 else if (token->type == CPP_OPEN_BRACE)
6404 statement = cp_parser_compound_statement (parser, NULL, false);
6405 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6406 a statement all its own. */
6407 else if (token->type == CPP_PRAGMA)
6409 /* Only certain OpenMP pragmas are attached to statements, and thus
6410 are considered statements themselves. All others are not. In
6411 the context of a compound, accept the pragma as a "statement" and
6412 return so that we can check for a close brace. Otherwise we
6413 require a real statement and must go back and read one. */
6415 cp_parser_pragma (parser, pragma_compound);
6416 else if (!cp_parser_pragma (parser, pragma_stmt))
6420 else if (token->type == CPP_EOF)
6422 cp_parser_error (parser, "expected statement");
6426 /* Everything else must be a declaration-statement or an
6427 expression-statement. Try for the declaration-statement
6428 first, unless we are looking at a `;', in which case we know that
6429 we have an expression-statement. */
6432 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6434 cp_parser_parse_tentatively (parser);
6435 /* Try to parse the declaration-statement. */
6436 cp_parser_declaration_statement (parser);
6437 /* If that worked, we're done. */
6438 if (cp_parser_parse_definitely (parser))
6441 /* Look for an expression-statement instead. */
6442 statement = cp_parser_expression_statement (parser, in_statement_expr);
6445 /* Set the line number for the statement. */
6446 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6447 SET_EXPR_LOCATION (statement, statement_location);
6450 /* Parse the label for a labeled-statement, i.e.
6453 case constant-expression :
6457 case constant-expression ... constant-expression : statement
6459 When a label is parsed without errors, the label is added to the
6460 parse tree by the finish_* functions, so this function doesn't
6461 have to return the label. */
6464 cp_parser_label_for_labeled_statement (cp_parser* parser)
6468 /* The next token should be an identifier. */
6469 token = cp_lexer_peek_token (parser->lexer);
6470 if (token->type != CPP_NAME
6471 && token->type != CPP_KEYWORD)
6473 cp_parser_error (parser, "expected labeled-statement");
6477 switch (token->keyword)
6484 /* Consume the `case' token. */
6485 cp_lexer_consume_token (parser->lexer);
6486 /* Parse the constant-expression. */
6487 expr = cp_parser_constant_expression (parser,
6488 /*allow_non_constant_p=*/false,
6491 ellipsis = cp_lexer_peek_token (parser->lexer);
6492 if (ellipsis->type == CPP_ELLIPSIS)
6494 /* Consume the `...' token. */
6495 cp_lexer_consume_token (parser->lexer);
6497 cp_parser_constant_expression (parser,
6498 /*allow_non_constant_p=*/false,
6500 /* We don't need to emit warnings here, as the common code
6501 will do this for us. */
6504 expr_hi = NULL_TREE;
6506 if (parser->in_switch_statement_p)
6507 finish_case_label (expr, expr_hi);
6509 error ("case label %qE not within a switch statement", expr);
6514 /* Consume the `default' token. */
6515 cp_lexer_consume_token (parser->lexer);
6517 if (parser->in_switch_statement_p)
6518 finish_case_label (NULL_TREE, NULL_TREE);
6520 error ("case label not within a switch statement");
6524 /* Anything else must be an ordinary label. */
6525 finish_label_stmt (cp_parser_identifier (parser));
6529 /* Require the `:' token. */
6530 cp_parser_require (parser, CPP_COLON, "`:'");
6533 /* Parse an expression-statement.
6535 expression-statement:
6538 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6539 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6540 indicates whether this expression-statement is part of an
6541 expression statement. */
6544 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6546 tree statement = NULL_TREE;
6548 /* If the next token is a ';', then there is no expression
6550 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6551 statement = cp_parser_expression (parser, /*cast_p=*/false);
6553 /* Consume the final `;'. */
6554 cp_parser_consume_semicolon_at_end_of_statement (parser);
6556 if (in_statement_expr
6557 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6558 /* This is the final expression statement of a statement
6560 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6562 statement = finish_expr_stmt (statement);
6569 /* Parse a compound-statement.
6572 { statement-seq [opt] }
6574 Returns a tree representing the statement. */
6577 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6582 /* Consume the `{'. */
6583 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6584 return error_mark_node;
6585 /* Begin the compound-statement. */
6586 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6587 /* Parse an (optional) statement-seq. */
6588 cp_parser_statement_seq_opt (parser, in_statement_expr);
6589 /* Finish the compound-statement. */
6590 finish_compound_stmt (compound_stmt);
6591 /* Consume the `}'. */
6592 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6594 return compound_stmt;
6597 /* Parse an (optional) statement-seq.
6601 statement-seq [opt] statement */
6604 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6606 /* Scan statements until there aren't any more. */
6609 cp_token *token = cp_lexer_peek_token (parser->lexer);
6611 /* If we're looking at a `}', then we've run out of statements. */
6612 if (token->type == CPP_CLOSE_BRACE
6613 || token->type == CPP_EOF
6614 || token->type == CPP_PRAGMA_EOL)
6617 /* If we are in a compound statement and find 'else' then
6618 something went wrong. */
6619 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6621 if (parser->in_statement & IN_IF_STMT)
6625 token = cp_lexer_consume_token (parser->lexer);
6626 error ("%<else%> without a previous %<if%>");
6630 /* Parse the statement. */
6631 cp_parser_statement (parser, in_statement_expr, true, NULL);
6635 /* Parse a selection-statement.
6637 selection-statement:
6638 if ( condition ) statement
6639 if ( condition ) statement else statement
6640 switch ( condition ) statement
6642 Returns the new IF_STMT or SWITCH_STMT.
6644 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6645 is a (possibly labeled) if statement which is not enclosed in
6646 braces and has an else clause. This is used to implement
6650 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6658 /* Peek at the next token. */
6659 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6661 /* See what kind of keyword it is. */
6662 keyword = token->keyword;
6671 /* Look for the `('. */
6672 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6674 cp_parser_skip_to_end_of_statement (parser);
6675 return error_mark_node;
6678 /* Begin the selection-statement. */
6679 if (keyword == RID_IF)
6680 statement = begin_if_stmt ();
6682 statement = begin_switch_stmt ();
6684 /* Parse the condition. */
6685 condition = cp_parser_condition (parser);
6686 /* Look for the `)'. */
6687 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6688 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6689 /*consume_paren=*/true);
6691 if (keyword == RID_IF)
6694 unsigned char in_statement;
6696 /* Add the condition. */
6697 finish_if_stmt_cond (condition, statement);
6699 /* Parse the then-clause. */
6700 in_statement = parser->in_statement;
6701 parser->in_statement |= IN_IF_STMT;
6702 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6703 parser->in_statement = in_statement;
6705 finish_then_clause (statement);
6707 /* If the next token is `else', parse the else-clause. */
6708 if (cp_lexer_next_token_is_keyword (parser->lexer,
6711 /* Consume the `else' keyword. */
6712 cp_lexer_consume_token (parser->lexer);
6713 begin_else_clause (statement);
6714 /* Parse the else-clause. */
6715 cp_parser_implicitly_scoped_statement (parser, NULL);
6716 finish_else_clause (statement);
6718 /* If we are currently parsing a then-clause, then
6719 IF_P will not be NULL. We set it to true to
6720 indicate that this if statement has an else clause.
6721 This may trigger the Wparentheses warning below
6722 when we get back up to the parent if statement. */
6728 /* This if statement does not have an else clause. If
6729 NESTED_IF is true, then the then-clause is an if
6730 statement which does have an else clause. We warn
6731 about the potential ambiguity. */
6733 warning (OPT_Wparentheses,
6734 ("%Hsuggest explicit braces "
6735 "to avoid ambiguous %<else%>"),
6736 EXPR_LOCUS (statement));
6739 /* Now we're all done with the if-statement. */
6740 finish_if_stmt (statement);
6744 bool in_switch_statement_p;
6745 unsigned char in_statement;
6747 /* Add the condition. */
6748 finish_switch_cond (condition, statement);
6750 /* Parse the body of the switch-statement. */
6751 in_switch_statement_p = parser->in_switch_statement_p;
6752 in_statement = parser->in_statement;
6753 parser->in_switch_statement_p = true;
6754 parser->in_statement |= IN_SWITCH_STMT;
6755 cp_parser_implicitly_scoped_statement (parser, NULL);
6756 parser->in_switch_statement_p = in_switch_statement_p;
6757 parser->in_statement = in_statement;
6759 /* Now we're all done with the switch-statement. */
6760 finish_switch_stmt (statement);
6768 cp_parser_error (parser, "expected selection-statement");
6769 return error_mark_node;
6773 /* Parse a condition.
6777 type-specifier-seq declarator = assignment-expression
6782 type-specifier-seq declarator asm-specification [opt]
6783 attributes [opt] = assignment-expression
6785 Returns the expression that should be tested. */
6788 cp_parser_condition (cp_parser* parser)
6790 cp_decl_specifier_seq type_specifiers;
6791 const char *saved_message;
6793 /* Try the declaration first. */
6794 cp_parser_parse_tentatively (parser);
6795 /* New types are not allowed in the type-specifier-seq for a
6797 saved_message = parser->type_definition_forbidden_message;
6798 parser->type_definition_forbidden_message
6799 = "types may not be defined in conditions";
6800 /* Parse the type-specifier-seq. */
6801 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6803 /* Restore the saved message. */
6804 parser->type_definition_forbidden_message = saved_message;
6805 /* If all is well, we might be looking at a declaration. */
6806 if (!cp_parser_error_occurred (parser))
6809 tree asm_specification;
6811 cp_declarator *declarator;
6812 tree initializer = NULL_TREE;
6814 /* Parse the declarator. */
6815 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6816 /*ctor_dtor_or_conv_p=*/NULL,
6817 /*parenthesized_p=*/NULL,
6818 /*member_p=*/false);
6819 /* Parse the attributes. */
6820 attributes = cp_parser_attributes_opt (parser);
6821 /* Parse the asm-specification. */
6822 asm_specification = cp_parser_asm_specification_opt (parser);
6823 /* If the next token is not an `=', then we might still be
6824 looking at an expression. For example:
6828 looks like a decl-specifier-seq and a declarator -- but then
6829 there is no `=', so this is an expression. */
6830 cp_parser_require (parser, CPP_EQ, "`='");
6831 /* If we did see an `=', then we are looking at a declaration
6833 if (cp_parser_parse_definitely (parser))
6836 bool non_constant_p;
6838 /* Create the declaration. */
6839 decl = start_decl (declarator, &type_specifiers,
6840 /*initialized_p=*/true,
6841 attributes, /*prefix_attributes=*/NULL_TREE,
6843 /* Parse the assignment-expression. */
6845 = cp_parser_constant_expression (parser,
6846 /*allow_non_constant_p=*/true,
6848 if (!non_constant_p)
6849 initializer = fold_non_dependent_expr (initializer);
6851 /* Process the initializer. */
6852 cp_finish_decl (decl,
6853 initializer, !non_constant_p,
6855 LOOKUP_ONLYCONVERTING);
6858 pop_scope (pushed_scope);
6860 return convert_from_reference (decl);
6863 /* If we didn't even get past the declarator successfully, we are
6864 definitely not looking at a declaration. */
6866 cp_parser_abort_tentative_parse (parser);
6868 /* Otherwise, we are looking at an expression. */
6869 return cp_parser_expression (parser, /*cast_p=*/false);
6872 /* Parse an iteration-statement.
6874 iteration-statement:
6875 while ( condition ) statement
6876 do statement while ( expression ) ;
6877 for ( for-init-statement condition [opt] ; expression [opt] )
6880 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6883 cp_parser_iteration_statement (cp_parser* parser)
6888 unsigned char in_statement;
6890 /* Peek at the next token. */
6891 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6893 return error_mark_node;
6895 /* Remember whether or not we are already within an iteration
6897 in_statement = parser->in_statement;
6899 /* See what kind of keyword it is. */
6900 keyword = token->keyword;
6907 /* Begin the while-statement. */
6908 statement = begin_while_stmt ();
6909 /* Look for the `('. */
6910 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6911 /* Parse the condition. */
6912 condition = cp_parser_condition (parser);
6913 finish_while_stmt_cond (condition, statement);
6914 /* Look for the `)'. */
6915 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6916 /* Parse the dependent statement. */
6917 parser->in_statement = IN_ITERATION_STMT;
6918 cp_parser_already_scoped_statement (parser);
6919 parser->in_statement = in_statement;
6920 /* We're done with the while-statement. */
6921 finish_while_stmt (statement);
6929 /* Begin the do-statement. */
6930 statement = begin_do_stmt ();
6931 /* Parse the body of the do-statement. */
6932 parser->in_statement = IN_ITERATION_STMT;
6933 cp_parser_implicitly_scoped_statement (parser, NULL);
6934 parser->in_statement = in_statement;
6935 finish_do_body (statement);
6936 /* Look for the `while' keyword. */
6937 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6938 /* Look for the `('. */
6939 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6940 /* Parse the expression. */
6941 expression = cp_parser_expression (parser, /*cast_p=*/false);
6942 /* We're done with the do-statement. */
6943 finish_do_stmt (expression, statement);
6944 /* Look for the `)'. */
6945 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6946 /* Look for the `;'. */
6947 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6953 tree condition = NULL_TREE;
6954 tree expression = NULL_TREE;
6956 /* Begin the for-statement. */
6957 statement = begin_for_stmt ();
6958 /* Look for the `('. */
6959 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6960 /* Parse the initialization. */
6961 cp_parser_for_init_statement (parser);
6962 finish_for_init_stmt (statement);
6964 /* If there's a condition, process it. */
6965 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6966 condition = cp_parser_condition (parser);
6967 finish_for_cond (condition, statement);
6968 /* Look for the `;'. */
6969 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6971 /* If there's an expression, process it. */
6972 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6973 expression = cp_parser_expression (parser, /*cast_p=*/false);
6974 finish_for_expr (expression, statement);
6975 /* Look for the `)'. */
6976 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6978 /* Parse the body of the for-statement. */
6979 parser->in_statement = IN_ITERATION_STMT;
6980 cp_parser_already_scoped_statement (parser);
6981 parser->in_statement = in_statement;
6983 /* We're done with the for-statement. */
6984 finish_for_stmt (statement);
6989 cp_parser_error (parser, "expected iteration-statement");
6990 statement = error_mark_node;
6997 /* Parse a for-init-statement.
7000 expression-statement
7001 simple-declaration */
7004 cp_parser_for_init_statement (cp_parser* parser)
7006 /* If the next token is a `;', then we have an empty
7007 expression-statement. Grammatically, this is also a
7008 simple-declaration, but an invalid one, because it does not
7009 declare anything. Therefore, if we did not handle this case
7010 specially, we would issue an error message about an invalid
7012 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7014 /* We're going to speculatively look for a declaration, falling back
7015 to an expression, if necessary. */
7016 cp_parser_parse_tentatively (parser);
7017 /* Parse the declaration. */
7018 cp_parser_simple_declaration (parser,
7019 /*function_definition_allowed_p=*/false);
7020 /* If the tentative parse failed, then we shall need to look for an
7021 expression-statement. */
7022 if (cp_parser_parse_definitely (parser))
7026 cp_parser_expression_statement (parser, false);
7029 /* Parse a jump-statement.
7034 return expression [opt] ;
7042 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7045 cp_parser_jump_statement (cp_parser* parser)
7047 tree statement = error_mark_node;
7050 unsigned char in_statement;
7052 /* Peek at the next token. */
7053 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7055 return error_mark_node;
7057 /* See what kind of keyword it is. */
7058 keyword = token->keyword;
7062 in_statement = parser->in_statement & ~IN_IF_STMT;
7063 switch (in_statement)
7066 error ("break statement not within loop or switch");
7069 gcc_assert ((in_statement & IN_SWITCH_STMT)
7070 || in_statement == IN_ITERATION_STMT);
7071 statement = finish_break_stmt ();
7074 error ("invalid exit from OpenMP structured block");
7077 error ("break statement used with OpenMP for loop");
7080 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7084 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7087 error ("continue statement not within a loop");
7089 case IN_ITERATION_STMT:
7091 statement = finish_continue_stmt ();
7094 error ("invalid exit from OpenMP structured block");
7099 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7106 /* If the next token is a `;', then there is no
7108 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7109 expr = cp_parser_expression (parser, /*cast_p=*/false);
7112 /* Build the return-statement. */
7113 statement = finish_return_stmt (expr);
7114 /* Look for the final `;'. */
7115 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7120 /* Create the goto-statement. */
7121 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7123 /* Issue a warning about this use of a GNU extension. */
7125 pedwarn ("ISO C++ forbids computed gotos");
7126 /* Consume the '*' token. */
7127 cp_lexer_consume_token (parser->lexer);
7128 /* Parse the dependent expression. */
7129 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7132 finish_goto_stmt (cp_parser_identifier (parser));
7133 /* Look for the final `;'. */
7134 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7138 cp_parser_error (parser, "expected jump-statement");
7145 /* Parse a declaration-statement.
7147 declaration-statement:
7148 block-declaration */
7151 cp_parser_declaration_statement (cp_parser* parser)
7155 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7156 p = obstack_alloc (&declarator_obstack, 0);
7158 /* Parse the block-declaration. */
7159 cp_parser_block_declaration (parser, /*statement_p=*/true);
7161 /* Free any declarators allocated. */
7162 obstack_free (&declarator_obstack, p);
7164 /* Finish off the statement. */
7168 /* Some dependent statements (like `if (cond) statement'), are
7169 implicitly in their own scope. In other words, if the statement is
7170 a single statement (as opposed to a compound-statement), it is
7171 none-the-less treated as if it were enclosed in braces. Any
7172 declarations appearing in the dependent statement are out of scope
7173 after control passes that point. This function parses a statement,
7174 but ensures that is in its own scope, even if it is not a
7177 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7178 is a (possibly labeled) if statement which is not enclosed in
7179 braces and has an else clause. This is used to implement
7182 Returns the new statement. */
7185 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7192 /* Mark if () ; with a special NOP_EXPR. */
7193 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7195 cp_lexer_consume_token (parser->lexer);
7196 statement = add_stmt (build_empty_stmt ());
7198 /* if a compound is opened, we simply parse the statement directly. */
7199 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7200 statement = cp_parser_compound_statement (parser, NULL, false);
7201 /* If the token is not a `{', then we must take special action. */
7204 /* Create a compound-statement. */
7205 statement = begin_compound_stmt (0);
7206 /* Parse the dependent-statement. */
7207 cp_parser_statement (parser, NULL_TREE, false, if_p);
7208 /* Finish the dummy compound-statement. */
7209 finish_compound_stmt (statement);
7212 /* Return the statement. */
7216 /* For some dependent statements (like `while (cond) statement'), we
7217 have already created a scope. Therefore, even if the dependent
7218 statement is a compound-statement, we do not want to create another
7222 cp_parser_already_scoped_statement (cp_parser* parser)
7224 /* If the token is a `{', then we must take special action. */
7225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7226 cp_parser_statement (parser, NULL_TREE, false, NULL);
7229 /* Avoid calling cp_parser_compound_statement, so that we
7230 don't create a new scope. Do everything else by hand. */
7231 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7232 cp_parser_statement_seq_opt (parser, NULL_TREE);
7233 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7237 /* Declarations [gram.dcl.dcl] */
7239 /* Parse an optional declaration-sequence.
7243 declaration-seq declaration */
7246 cp_parser_declaration_seq_opt (cp_parser* parser)
7252 token = cp_lexer_peek_token (parser->lexer);
7254 if (token->type == CPP_CLOSE_BRACE
7255 || token->type == CPP_EOF
7256 || token->type == CPP_PRAGMA_EOL)
7259 if (token->type == CPP_SEMICOLON)
7261 /* A declaration consisting of a single semicolon is
7262 invalid. Allow it unless we're being pedantic. */
7263 cp_lexer_consume_token (parser->lexer);
7264 if (pedantic && !in_system_header)
7265 pedwarn ("extra %<;%>");
7269 /* If we're entering or exiting a region that's implicitly
7270 extern "C", modify the lang context appropriately. */
7271 if (!parser->implicit_extern_c && token->implicit_extern_c)
7273 push_lang_context (lang_name_c);
7274 parser->implicit_extern_c = true;
7276 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7278 pop_lang_context ();
7279 parser->implicit_extern_c = false;
7282 if (token->type == CPP_PRAGMA)
7284 /* A top-level declaration can consist solely of a #pragma.
7285 A nested declaration cannot, so this is done here and not
7286 in cp_parser_declaration. (A #pragma at block scope is
7287 handled in cp_parser_statement.) */
7288 cp_parser_pragma (parser, pragma_external);
7292 /* Parse the declaration itself. */
7293 cp_parser_declaration (parser);
7297 /* Parse a declaration.
7302 template-declaration
7303 explicit-instantiation
7304 explicit-specialization
7305 linkage-specification
7306 namespace-definition
7311 __extension__ declaration */
7314 cp_parser_declaration (cp_parser* parser)
7321 /* Check for the `__extension__' keyword. */
7322 if (cp_parser_extension_opt (parser, &saved_pedantic))
7324 /* Parse the qualified declaration. */
7325 cp_parser_declaration (parser);
7326 /* Restore the PEDANTIC flag. */
7327 pedantic = saved_pedantic;
7332 /* Try to figure out what kind of declaration is present. */
7333 token1 = *cp_lexer_peek_token (parser->lexer);
7335 if (token1.type != CPP_EOF)
7336 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7339 token2.type = CPP_EOF;
7340 token2.keyword = RID_MAX;
7343 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7344 p = obstack_alloc (&declarator_obstack, 0);
7346 /* If the next token is `extern' and the following token is a string
7347 literal, then we have a linkage specification. */
7348 if (token1.keyword == RID_EXTERN
7349 && cp_parser_is_string_literal (&token2))
7350 cp_parser_linkage_specification (parser);
7351 /* If the next token is `template', then we have either a template
7352 declaration, an explicit instantiation, or an explicit
7354 else if (token1.keyword == RID_TEMPLATE)
7356 /* `template <>' indicates a template specialization. */
7357 if (token2.type == CPP_LESS
7358 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7359 cp_parser_explicit_specialization (parser);
7360 /* `template <' indicates a template declaration. */
7361 else if (token2.type == CPP_LESS)
7362 cp_parser_template_declaration (parser, /*member_p=*/false);
7363 /* Anything else must be an explicit instantiation. */
7365 cp_parser_explicit_instantiation (parser);
7367 /* If the next token is `export', then we have a template
7369 else if (token1.keyword == RID_EXPORT)
7370 cp_parser_template_declaration (parser, /*member_p=*/false);
7371 /* If the next token is `extern', 'static' or 'inline' and the one
7372 after that is `template', we have a GNU extended explicit
7373 instantiation directive. */
7374 else if (cp_parser_allow_gnu_extensions_p (parser)
7375 && (token1.keyword == RID_EXTERN
7376 || token1.keyword == RID_STATIC
7377 || token1.keyword == RID_INLINE)
7378 && token2.keyword == RID_TEMPLATE)
7379 cp_parser_explicit_instantiation (parser);
7380 /* If the next token is `namespace', check for a named or unnamed
7381 namespace definition. */
7382 else if (token1.keyword == RID_NAMESPACE
7383 && (/* A named namespace definition. */
7384 (token2.type == CPP_NAME
7385 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7387 /* An unnamed namespace definition. */
7388 || token2.type == CPP_OPEN_BRACE
7389 || token2.keyword == RID_ATTRIBUTE))
7390 cp_parser_namespace_definition (parser);
7391 /* Objective-C++ declaration/definition. */
7392 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7393 cp_parser_objc_declaration (parser);
7394 /* We must have either a block declaration or a function
7397 /* Try to parse a block-declaration, or a function-definition. */
7398 cp_parser_block_declaration (parser, /*statement_p=*/false);
7400 /* Free any declarators allocated. */
7401 obstack_free (&declarator_obstack, p);
7404 /* Parse a block-declaration.
7409 namespace-alias-definition
7416 __extension__ block-declaration
7422 static_assert-declaration
7424 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7425 part of a declaration-statement. */
7428 cp_parser_block_declaration (cp_parser *parser,
7434 /* Check for the `__extension__' keyword. */
7435 if (cp_parser_extension_opt (parser, &saved_pedantic))
7437 /* Parse the qualified declaration. */
7438 cp_parser_block_declaration (parser, statement_p);
7439 /* Restore the PEDANTIC flag. */
7440 pedantic = saved_pedantic;
7445 /* Peek at the next token to figure out which kind of declaration is
7447 token1 = cp_lexer_peek_token (parser->lexer);
7449 /* If the next keyword is `asm', we have an asm-definition. */
7450 if (token1->keyword == RID_ASM)
7453 cp_parser_commit_to_tentative_parse (parser);
7454 cp_parser_asm_definition (parser);
7456 /* If the next keyword is `namespace', we have a
7457 namespace-alias-definition. */
7458 else if (token1->keyword == RID_NAMESPACE)
7459 cp_parser_namespace_alias_definition (parser);
7460 /* If the next keyword is `using', we have either a
7461 using-declaration or a using-directive. */
7462 else if (token1->keyword == RID_USING)
7467 cp_parser_commit_to_tentative_parse (parser);
7468 /* If the token after `using' is `namespace', then we have a
7470 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7471 if (token2->keyword == RID_NAMESPACE)
7472 cp_parser_using_directive (parser);
7473 /* Otherwise, it's a using-declaration. */
7475 cp_parser_using_declaration (parser,
7476 /*access_declaration_p=*/false);
7478 /* If the next keyword is `__label__' we have a label declaration. */
7479 else if (token1->keyword == RID_LABEL)
7482 cp_parser_commit_to_tentative_parse (parser);
7483 cp_parser_label_declaration (parser);
7485 /* If the next token is `static_assert' we have a static assertion. */
7486 else if (token1->keyword == RID_STATIC_ASSERT)
7487 cp_parser_static_assert (parser, /*member_p=*/false);
7488 /* Anything else must be a simple-declaration. */
7490 cp_parser_simple_declaration (parser, !statement_p);
7493 /* Parse a simple-declaration.
7496 decl-specifier-seq [opt] init-declarator-list [opt] ;
7498 init-declarator-list:
7500 init-declarator-list , init-declarator
7502 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7503 function-definition as a simple-declaration. */
7506 cp_parser_simple_declaration (cp_parser* parser,
7507 bool function_definition_allowed_p)
7509 cp_decl_specifier_seq decl_specifiers;
7510 int declares_class_or_enum;
7511 bool saw_declarator;
7513 /* Defer access checks until we know what is being declared; the
7514 checks for names appearing in the decl-specifier-seq should be
7515 done as if we were in the scope of the thing being declared. */
7516 push_deferring_access_checks (dk_deferred);
7518 /* Parse the decl-specifier-seq. We have to keep track of whether
7519 or not the decl-specifier-seq declares a named class or
7520 enumeration type, since that is the only case in which the
7521 init-declarator-list is allowed to be empty.
7525 In a simple-declaration, the optional init-declarator-list can be
7526 omitted only when declaring a class or enumeration, that is when
7527 the decl-specifier-seq contains either a class-specifier, an
7528 elaborated-type-specifier, or an enum-specifier. */
7529 cp_parser_decl_specifier_seq (parser,
7530 CP_PARSER_FLAGS_OPTIONAL,
7532 &declares_class_or_enum);
7533 /* We no longer need to defer access checks. */
7534 stop_deferring_access_checks ();
7536 /* In a block scope, a valid declaration must always have a
7537 decl-specifier-seq. By not trying to parse declarators, we can
7538 resolve the declaration/expression ambiguity more quickly. */
7539 if (!function_definition_allowed_p
7540 && !decl_specifiers.any_specifiers_p)
7542 cp_parser_error (parser, "expected declaration");
7546 /* If the next two tokens are both identifiers, the code is
7547 erroneous. The usual cause of this situation is code like:
7551 where "T" should name a type -- but does not. */
7552 if (!decl_specifiers.type
7553 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7555 /* If parsing tentatively, we should commit; we really are
7556 looking at a declaration. */
7557 cp_parser_commit_to_tentative_parse (parser);
7562 /* If we have seen at least one decl-specifier, and the next token
7563 is not a parenthesis, then we must be looking at a declaration.
7564 (After "int (" we might be looking at a functional cast.) */
7565 if (decl_specifiers.any_specifiers_p
7566 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7567 cp_parser_commit_to_tentative_parse (parser);
7569 /* Keep going until we hit the `;' at the end of the simple
7571 saw_declarator = false;
7572 while (cp_lexer_next_token_is_not (parser->lexer,
7576 bool function_definition_p;
7581 /* If we are processing next declarator, coma is expected */
7582 token = cp_lexer_peek_token (parser->lexer);
7583 gcc_assert (token->type == CPP_COMMA);
7584 cp_lexer_consume_token (parser->lexer);
7587 saw_declarator = true;
7589 /* Parse the init-declarator. */
7590 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7592 function_definition_allowed_p,
7594 declares_class_or_enum,
7595 &function_definition_p);
7596 /* If an error occurred while parsing tentatively, exit quickly.
7597 (That usually happens when in the body of a function; each
7598 statement is treated as a declaration-statement until proven
7600 if (cp_parser_error_occurred (parser))
7602 /* Handle function definitions specially. */
7603 if (function_definition_p)
7605 /* If the next token is a `,', then we are probably
7606 processing something like:
7610 which is erroneous. */
7611 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7612 error ("mixing declarations and function-definitions is forbidden");
7613 /* Otherwise, we're done with the list of declarators. */
7616 pop_deferring_access_checks ();
7620 /* The next token should be either a `,' or a `;'. */
7621 token = cp_lexer_peek_token (parser->lexer);
7622 /* If it's a `,', there are more declarators to come. */
7623 if (token->type == CPP_COMMA)
7624 /* will be consumed next time around */;
7625 /* If it's a `;', we are done. */
7626 else if (token->type == CPP_SEMICOLON)
7628 /* Anything else is an error. */
7631 /* If we have already issued an error message we don't need
7632 to issue another one. */
7633 if (decl != error_mark_node
7634 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7635 cp_parser_error (parser, "expected %<,%> or %<;%>");
7636 /* Skip tokens until we reach the end of the statement. */
7637 cp_parser_skip_to_end_of_statement (parser);
7638 /* If the next token is now a `;', consume it. */
7639 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7640 cp_lexer_consume_token (parser->lexer);
7643 /* After the first time around, a function-definition is not
7644 allowed -- even if it was OK at first. For example:
7649 function_definition_allowed_p = false;
7652 /* Issue an error message if no declarators are present, and the
7653 decl-specifier-seq does not itself declare a class or
7655 if (!saw_declarator)
7657 if (cp_parser_declares_only_class_p (parser))
7658 shadow_tag (&decl_specifiers);
7659 /* Perform any deferred access checks. */
7660 perform_deferred_access_checks ();
7663 /* Consume the `;'. */
7664 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7667 pop_deferring_access_checks ();
7670 /* Parse a decl-specifier-seq.
7673 decl-specifier-seq [opt] decl-specifier
7676 storage-class-specifier
7687 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7689 The parser flags FLAGS is used to control type-specifier parsing.
7691 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7694 1: one of the decl-specifiers is an elaborated-type-specifier
7695 (i.e., a type declaration)
7696 2: one of the decl-specifiers is an enum-specifier or a
7697 class-specifier (i.e., a type definition)
7702 cp_parser_decl_specifier_seq (cp_parser* parser,
7703 cp_parser_flags flags,
7704 cp_decl_specifier_seq *decl_specs,
7705 int* declares_class_or_enum)
7707 bool constructor_possible_p = !parser->in_declarator_p;
7709 /* Clear DECL_SPECS. */
7710 clear_decl_specs (decl_specs);
7712 /* Assume no class or enumeration type is declared. */
7713 *declares_class_or_enum = 0;
7715 /* Keep reading specifiers until there are no more to read. */
7719 bool found_decl_spec;
7722 /* Peek at the next token. */
7723 token = cp_lexer_peek_token (parser->lexer);
7724 /* Handle attributes. */
7725 if (token->keyword == RID_ATTRIBUTE)
7727 /* Parse the attributes. */
7728 decl_specs->attributes
7729 = chainon (decl_specs->attributes,
7730 cp_parser_attributes_opt (parser));
7733 /* Assume we will find a decl-specifier keyword. */
7734 found_decl_spec = true;
7735 /* If the next token is an appropriate keyword, we can simply
7736 add it to the list. */
7737 switch (token->keyword)
7742 if (!at_class_scope_p ())
7744 error ("%<friend%> used outside of class");
7745 cp_lexer_purge_token (parser->lexer);
7749 ++decl_specs->specs[(int) ds_friend];
7750 /* Consume the token. */
7751 cp_lexer_consume_token (parser->lexer);
7755 /* function-specifier:
7762 cp_parser_function_specifier_opt (parser, decl_specs);
7768 ++decl_specs->specs[(int) ds_typedef];
7769 /* Consume the token. */
7770 cp_lexer_consume_token (parser->lexer);
7771 /* A constructor declarator cannot appear in a typedef. */
7772 constructor_possible_p = false;
7773 /* The "typedef" keyword can only occur in a declaration; we
7774 may as well commit at this point. */
7775 cp_parser_commit_to_tentative_parse (parser);
7777 if (decl_specs->storage_class != sc_none)
7778 decl_specs->conflicting_specifiers_p = true;
7781 /* storage-class-specifier:
7795 /* Consume the token. */
7796 cp_lexer_consume_token (parser->lexer);
7797 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7800 /* Consume the token. */
7801 cp_lexer_consume_token (parser->lexer);
7802 ++decl_specs->specs[(int) ds_thread];
7806 /* We did not yet find a decl-specifier yet. */
7807 found_decl_spec = false;
7811 /* Constructors are a special case. The `S' in `S()' is not a
7812 decl-specifier; it is the beginning of the declarator. */
7815 && constructor_possible_p
7816 && (cp_parser_constructor_declarator_p
7817 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7819 /* If we don't have a DECL_SPEC yet, then we must be looking at
7820 a type-specifier. */
7821 if (!found_decl_spec && !constructor_p)
7823 int decl_spec_declares_class_or_enum;
7824 bool is_cv_qualifier;
7828 = cp_parser_type_specifier (parser, flags,
7830 /*is_declaration=*/true,
7831 &decl_spec_declares_class_or_enum,
7834 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7836 /* If this type-specifier referenced a user-defined type
7837 (a typedef, class-name, etc.), then we can't allow any
7838 more such type-specifiers henceforth.
7842 The longest sequence of decl-specifiers that could
7843 possibly be a type name is taken as the
7844 decl-specifier-seq of a declaration. The sequence shall
7845 be self-consistent as described below.
7849 As a general rule, at most one type-specifier is allowed
7850 in the complete decl-specifier-seq of a declaration. The
7851 only exceptions are the following:
7853 -- const or volatile can be combined with any other
7856 -- signed or unsigned can be combined with char, long,
7864 void g (const int Pc);
7866 Here, Pc is *not* part of the decl-specifier seq; it's
7867 the declarator. Therefore, once we see a type-specifier
7868 (other than a cv-qualifier), we forbid any additional
7869 user-defined types. We *do* still allow things like `int
7870 int' to be considered a decl-specifier-seq, and issue the
7871 error message later. */
7872 if (type_spec && !is_cv_qualifier)
7873 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7874 /* A constructor declarator cannot follow a type-specifier. */
7877 constructor_possible_p = false;
7878 found_decl_spec = true;
7882 /* If we still do not have a DECL_SPEC, then there are no more
7884 if (!found_decl_spec)
7887 decl_specs->any_specifiers_p = true;
7888 /* After we see one decl-specifier, further decl-specifiers are
7890 flags |= CP_PARSER_FLAGS_OPTIONAL;
7893 cp_parser_check_decl_spec (decl_specs);
7895 /* Don't allow a friend specifier with a class definition. */
7896 if (decl_specs->specs[(int) ds_friend] != 0
7897 && (*declares_class_or_enum & 2))
7898 error ("class definition may not be declared a friend");
7901 /* Parse an (optional) storage-class-specifier.
7903 storage-class-specifier:
7912 storage-class-specifier:
7915 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7918 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7920 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7928 /* Consume the token. */
7929 return cp_lexer_consume_token (parser->lexer)->u.value;
7936 /* Parse an (optional) function-specifier.
7943 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7944 Updates DECL_SPECS, if it is non-NULL. */
7947 cp_parser_function_specifier_opt (cp_parser* parser,
7948 cp_decl_specifier_seq *decl_specs)
7950 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7954 ++decl_specs->specs[(int) ds_inline];
7958 /* 14.5.2.3 [temp.mem]
7960 A member function template shall not be virtual. */
7961 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7962 error ("templates may not be %<virtual%>");
7963 else if (decl_specs)
7964 ++decl_specs->specs[(int) ds_virtual];
7969 ++decl_specs->specs[(int) ds_explicit];
7976 /* Consume the token. */
7977 return cp_lexer_consume_token (parser->lexer)->u.value;
7980 /* Parse a linkage-specification.
7982 linkage-specification:
7983 extern string-literal { declaration-seq [opt] }
7984 extern string-literal declaration */
7987 cp_parser_linkage_specification (cp_parser* parser)
7991 /* Look for the `extern' keyword. */
7992 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7994 /* Look for the string-literal. */
7995 linkage = cp_parser_string_literal (parser, false, false);
7997 /* Transform the literal into an identifier. If the literal is a
7998 wide-character string, or contains embedded NULs, then we can't
7999 handle it as the user wants. */
8000 if (strlen (TREE_STRING_POINTER (linkage))
8001 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8003 cp_parser_error (parser, "invalid linkage-specification");
8004 /* Assume C++ linkage. */
8005 linkage = lang_name_cplusplus;
8008 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8010 /* We're now using the new linkage. */
8011 push_lang_context (linkage);
8013 /* If the next token is a `{', then we're using the first
8015 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8017 /* Consume the `{' token. */
8018 cp_lexer_consume_token (parser->lexer);
8019 /* Parse the declarations. */
8020 cp_parser_declaration_seq_opt (parser);
8021 /* Look for the closing `}'. */
8022 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8024 /* Otherwise, there's just one declaration. */
8027 bool saved_in_unbraced_linkage_specification_p;
8029 saved_in_unbraced_linkage_specification_p
8030 = parser->in_unbraced_linkage_specification_p;
8031 parser->in_unbraced_linkage_specification_p = true;
8032 cp_parser_declaration (parser);
8033 parser->in_unbraced_linkage_specification_p
8034 = saved_in_unbraced_linkage_specification_p;
8037 /* We're done with the linkage-specification. */
8038 pop_lang_context ();
8041 /* Parse a static_assert-declaration.
8043 static_assert-declaration:
8044 static_assert ( constant-expression , string-literal ) ;
8046 If MEMBER_P, this static_assert is a class member. */
8049 cp_parser_static_assert(cp_parser *parser, bool member_p)
8054 location_t saved_loc;
8056 /* Peek at the `static_assert' token so we can keep track of exactly
8057 where the static assertion started. */
8058 token = cp_lexer_peek_token (parser->lexer);
8059 saved_loc = token->location;
8061 /* Look for the `static_assert' keyword. */
8062 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8066 /* We know we are in a static assertion; commit to any tentative
8068 if (cp_parser_parsing_tentatively (parser))
8069 cp_parser_commit_to_tentative_parse (parser);
8071 /* Parse the `(' starting the static assertion condition. */
8072 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8074 /* Parse the constant-expression. */
8076 cp_parser_constant_expression (parser,
8077 /*allow_non_constant_p=*/false,
8078 /*non_constant_p=*/NULL);
8080 /* Parse the separating `,'. */
8081 cp_parser_require (parser, CPP_COMMA, "`,'");
8083 /* Parse the string-literal message. */
8084 message = cp_parser_string_literal (parser,
8085 /*translate=*/false,
8088 /* A `)' completes the static assertion. */
8089 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8090 cp_parser_skip_to_closing_parenthesis (parser,
8091 /*recovering=*/true,
8093 /*consume_paren=*/true);
8095 /* A semicolon terminates the declaration. */
8096 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8098 /* Complete the static assertion, which may mean either processing
8099 the static assert now or saving it for template instantiation. */
8100 finish_static_assert (condition, message, saved_loc, member_p);
8103 /* Special member functions [gram.special] */
8105 /* Parse a conversion-function-id.
8107 conversion-function-id:
8108 operator conversion-type-id
8110 Returns an IDENTIFIER_NODE representing the operator. */
8113 cp_parser_conversion_function_id (cp_parser* parser)
8117 tree saved_qualifying_scope;
8118 tree saved_object_scope;
8119 tree pushed_scope = NULL_TREE;
8121 /* Look for the `operator' token. */
8122 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8123 return error_mark_node;
8124 /* When we parse the conversion-type-id, the current scope will be
8125 reset. However, we need that information in able to look up the
8126 conversion function later, so we save it here. */
8127 saved_scope = parser->scope;
8128 saved_qualifying_scope = parser->qualifying_scope;
8129 saved_object_scope = parser->object_scope;
8130 /* We must enter the scope of the class so that the names of
8131 entities declared within the class are available in the
8132 conversion-type-id. For example, consider:
8139 S::operator I() { ... }
8141 In order to see that `I' is a type-name in the definition, we
8142 must be in the scope of `S'. */
8144 pushed_scope = push_scope (saved_scope);
8145 /* Parse the conversion-type-id. */
8146 type = cp_parser_conversion_type_id (parser);
8147 /* Leave the scope of the class, if any. */
8149 pop_scope (pushed_scope);
8150 /* Restore the saved scope. */
8151 parser->scope = saved_scope;
8152 parser->qualifying_scope = saved_qualifying_scope;
8153 parser->object_scope = saved_object_scope;
8154 /* If the TYPE is invalid, indicate failure. */
8155 if (type == error_mark_node)
8156 return error_mark_node;
8157 return mangle_conv_op_name_for_type (type);
8160 /* Parse a conversion-type-id:
8163 type-specifier-seq conversion-declarator [opt]
8165 Returns the TYPE specified. */
8168 cp_parser_conversion_type_id (cp_parser* parser)
8171 cp_decl_specifier_seq type_specifiers;
8172 cp_declarator *declarator;
8173 tree type_specified;
8175 /* Parse the attributes. */
8176 attributes = cp_parser_attributes_opt (parser);
8177 /* Parse the type-specifiers. */
8178 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8180 /* If that didn't work, stop. */
8181 if (type_specifiers.type == error_mark_node)
8182 return error_mark_node;
8183 /* Parse the conversion-declarator. */
8184 declarator = cp_parser_conversion_declarator_opt (parser);
8186 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8187 /*initialized=*/0, &attributes);
8189 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8190 return type_specified;
8193 /* Parse an (optional) conversion-declarator.
8195 conversion-declarator:
8196 ptr-operator conversion-declarator [opt]
8200 static cp_declarator *
8201 cp_parser_conversion_declarator_opt (cp_parser* parser)
8203 enum tree_code code;
8205 cp_cv_quals cv_quals;
8207 /* We don't know if there's a ptr-operator next, or not. */
8208 cp_parser_parse_tentatively (parser);
8209 /* Try the ptr-operator. */
8210 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8211 /* If it worked, look for more conversion-declarators. */
8212 if (cp_parser_parse_definitely (parser))
8214 cp_declarator *declarator;
8216 /* Parse another optional declarator. */
8217 declarator = cp_parser_conversion_declarator_opt (parser);
8219 /* Create the representation of the declarator. */
8221 declarator = make_ptrmem_declarator (cv_quals, class_type,
8223 else if (code == INDIRECT_REF)
8224 declarator = make_pointer_declarator (cv_quals, declarator);
8226 declarator = make_reference_declarator (cv_quals, declarator);
8234 /* Parse an (optional) ctor-initializer.
8237 : mem-initializer-list
8239 Returns TRUE iff the ctor-initializer was actually present. */
8242 cp_parser_ctor_initializer_opt (cp_parser* parser)
8244 /* If the next token is not a `:', then there is no
8245 ctor-initializer. */
8246 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8248 /* Do default initialization of any bases and members. */
8249 if (DECL_CONSTRUCTOR_P (current_function_decl))
8250 finish_mem_initializers (NULL_TREE);
8255 /* Consume the `:' token. */
8256 cp_lexer_consume_token (parser->lexer);
8257 /* And the mem-initializer-list. */
8258 cp_parser_mem_initializer_list (parser);
8263 /* Parse a mem-initializer-list.
8265 mem-initializer-list:
8266 mem-initializer ... [opt]
8267 mem-initializer ... [opt] , mem-initializer-list */
8270 cp_parser_mem_initializer_list (cp_parser* parser)
8272 tree mem_initializer_list = NULL_TREE;
8274 /* Let the semantic analysis code know that we are starting the
8275 mem-initializer-list. */
8276 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8277 error ("only constructors take base initializers");
8279 /* Loop through the list. */
8282 tree mem_initializer;
8284 /* Parse the mem-initializer. */
8285 mem_initializer = cp_parser_mem_initializer (parser);
8286 /* If the next token is a `...', we're expanding member initializers. */
8287 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8289 /* Consume the `...'. */
8290 cp_lexer_consume_token (parser->lexer);
8292 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8293 can be expanded but members cannot. */
8294 if (mem_initializer != error_mark_node
8295 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8297 error ("cannot expand initializer for member %<%D%>",
8298 TREE_PURPOSE (mem_initializer));
8299 mem_initializer = error_mark_node;
8302 /* Construct the pack expansion type. */
8303 if (mem_initializer != error_mark_node)
8304 mem_initializer = make_pack_expansion (mem_initializer);
8306 /* Add it to the list, unless it was erroneous. */
8307 if (mem_initializer != error_mark_node)
8309 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8310 mem_initializer_list = mem_initializer;
8312 /* If the next token is not a `,', we're done. */
8313 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8315 /* Consume the `,' token. */
8316 cp_lexer_consume_token (parser->lexer);
8319 /* Perform semantic analysis. */
8320 if (DECL_CONSTRUCTOR_P (current_function_decl))
8321 finish_mem_initializers (mem_initializer_list);
8324 /* Parse a mem-initializer.
8327 mem-initializer-id ( expression-list [opt] )
8332 ( expression-list [opt] )
8334 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8335 class) or FIELD_DECL (for a non-static data member) to initialize;
8336 the TREE_VALUE is the expression-list. An empty initialization
8337 list is represented by void_list_node. */
8340 cp_parser_mem_initializer (cp_parser* parser)
8342 tree mem_initializer_id;
8343 tree expression_list;
8346 /* Find out what is being initialized. */
8347 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8349 pedwarn ("anachronistic old-style base class initializer");
8350 mem_initializer_id = NULL_TREE;
8353 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8354 member = expand_member_init (mem_initializer_id);
8355 if (member && !DECL_P (member))
8356 in_base_initializer = 1;
8359 = cp_parser_parenthesized_expression_list (parser, false,
8361 /*allow_expansion_p=*/true,
8362 /*non_constant_p=*/NULL);
8363 if (expression_list == error_mark_node)
8364 return error_mark_node;
8365 if (!expression_list)
8366 expression_list = void_type_node;
8368 in_base_initializer = 0;
8370 return member ? build_tree_list (member, expression_list) : error_mark_node;
8373 /* Parse a mem-initializer-id.
8376 :: [opt] nested-name-specifier [opt] class-name
8379 Returns a TYPE indicating the class to be initializer for the first
8380 production. Returns an IDENTIFIER_NODE indicating the data member
8381 to be initialized for the second production. */
8384 cp_parser_mem_initializer_id (cp_parser* parser)
8386 bool global_scope_p;
8387 bool nested_name_specifier_p;
8388 bool template_p = false;
8391 /* `typename' is not allowed in this context ([temp.res]). */
8392 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8394 error ("keyword %<typename%> not allowed in this context (a qualified "
8395 "member initializer is implicitly a type)");
8396 cp_lexer_consume_token (parser->lexer);
8398 /* Look for the optional `::' operator. */
8400 = (cp_parser_global_scope_opt (parser,
8401 /*current_scope_valid_p=*/false)
8403 /* Look for the optional nested-name-specifier. The simplest way to
8408 The keyword `typename' is not permitted in a base-specifier or
8409 mem-initializer; in these contexts a qualified name that
8410 depends on a template-parameter is implicitly assumed to be a
8413 is to assume that we have seen the `typename' keyword at this
8415 nested_name_specifier_p
8416 = (cp_parser_nested_name_specifier_opt (parser,
8417 /*typename_keyword_p=*/true,
8418 /*check_dependency_p=*/true,
8420 /*is_declaration=*/true)
8422 if (nested_name_specifier_p)
8423 template_p = cp_parser_optional_template_keyword (parser);
8424 /* If there is a `::' operator or a nested-name-specifier, then we
8425 are definitely looking for a class-name. */
8426 if (global_scope_p || nested_name_specifier_p)
8427 return cp_parser_class_name (parser,
8428 /*typename_keyword_p=*/true,
8429 /*template_keyword_p=*/template_p,
8431 /*check_dependency_p=*/true,
8432 /*class_head_p=*/false,
8433 /*is_declaration=*/true);
8434 /* Otherwise, we could also be looking for an ordinary identifier. */
8435 cp_parser_parse_tentatively (parser);
8436 /* Try a class-name. */
8437 id = cp_parser_class_name (parser,
8438 /*typename_keyword_p=*/true,
8439 /*template_keyword_p=*/false,
8441 /*check_dependency_p=*/true,
8442 /*class_head_p=*/false,
8443 /*is_declaration=*/true);
8444 /* If we found one, we're done. */
8445 if (cp_parser_parse_definitely (parser))
8447 /* Otherwise, look for an ordinary identifier. */
8448 return cp_parser_identifier (parser);
8451 /* Overloading [gram.over] */
8453 /* Parse an operator-function-id.
8455 operator-function-id:
8458 Returns an IDENTIFIER_NODE for the operator which is a
8459 human-readable spelling of the identifier, e.g., `operator +'. */
8462 cp_parser_operator_function_id (cp_parser* parser)
8464 /* Look for the `operator' keyword. */
8465 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8466 return error_mark_node;
8467 /* And then the name of the operator itself. */
8468 return cp_parser_operator (parser);
8471 /* Parse an operator.
8474 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8475 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8476 || ++ -- , ->* -> () []
8483 Returns an IDENTIFIER_NODE for the operator which is a
8484 human-readable spelling of the identifier, e.g., `operator +'. */
8487 cp_parser_operator (cp_parser* parser)
8489 tree id = NULL_TREE;
8492 /* Peek at the next token. */
8493 token = cp_lexer_peek_token (parser->lexer);
8494 /* Figure out which operator we have. */
8495 switch (token->type)
8501 /* The keyword should be either `new' or `delete'. */
8502 if (token->keyword == RID_NEW)
8504 else if (token->keyword == RID_DELETE)
8509 /* Consume the `new' or `delete' token. */
8510 cp_lexer_consume_token (parser->lexer);
8512 /* Peek at the next token. */
8513 token = cp_lexer_peek_token (parser->lexer);
8514 /* If it's a `[' token then this is the array variant of the
8516 if (token->type == CPP_OPEN_SQUARE)
8518 /* Consume the `[' token. */
8519 cp_lexer_consume_token (parser->lexer);
8520 /* Look for the `]' token. */
8521 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8522 id = ansi_opname (op == NEW_EXPR
8523 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8525 /* Otherwise, we have the non-array variant. */
8527 id = ansi_opname (op);
8533 id = ansi_opname (PLUS_EXPR);
8537 id = ansi_opname (MINUS_EXPR);
8541 id = ansi_opname (MULT_EXPR);
8545 id = ansi_opname (TRUNC_DIV_EXPR);
8549 id = ansi_opname (TRUNC_MOD_EXPR);
8553 id = ansi_opname (BIT_XOR_EXPR);
8557 id = ansi_opname (BIT_AND_EXPR);
8561 id = ansi_opname (BIT_IOR_EXPR);
8565 id = ansi_opname (BIT_NOT_EXPR);
8569 id = ansi_opname (TRUTH_NOT_EXPR);
8573 id = ansi_assopname (NOP_EXPR);
8577 id = ansi_opname (LT_EXPR);
8581 id = ansi_opname (GT_EXPR);
8585 id = ansi_assopname (PLUS_EXPR);
8589 id = ansi_assopname (MINUS_EXPR);
8593 id = ansi_assopname (MULT_EXPR);
8597 id = ansi_assopname (TRUNC_DIV_EXPR);
8601 id = ansi_assopname (TRUNC_MOD_EXPR);
8605 id = ansi_assopname (BIT_XOR_EXPR);
8609 id = ansi_assopname (BIT_AND_EXPR);
8613 id = ansi_assopname (BIT_IOR_EXPR);
8617 id = ansi_opname (LSHIFT_EXPR);
8621 id = ansi_opname (RSHIFT_EXPR);
8625 id = ansi_assopname (LSHIFT_EXPR);
8629 id = ansi_assopname (RSHIFT_EXPR);
8633 id = ansi_opname (EQ_EXPR);
8637 id = ansi_opname (NE_EXPR);
8641 id = ansi_opname (LE_EXPR);
8644 case CPP_GREATER_EQ:
8645 id = ansi_opname (GE_EXPR);
8649 id = ansi_opname (TRUTH_ANDIF_EXPR);
8653 id = ansi_opname (TRUTH_ORIF_EXPR);
8657 id = ansi_opname (POSTINCREMENT_EXPR);
8660 case CPP_MINUS_MINUS:
8661 id = ansi_opname (PREDECREMENT_EXPR);
8665 id = ansi_opname (COMPOUND_EXPR);
8668 case CPP_DEREF_STAR:
8669 id = ansi_opname (MEMBER_REF);
8673 id = ansi_opname (COMPONENT_REF);
8676 case CPP_OPEN_PAREN:
8677 /* Consume the `('. */
8678 cp_lexer_consume_token (parser->lexer);
8679 /* Look for the matching `)'. */
8680 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8681 return ansi_opname (CALL_EXPR);
8683 case CPP_OPEN_SQUARE:
8684 /* Consume the `['. */
8685 cp_lexer_consume_token (parser->lexer);
8686 /* Look for the matching `]'. */
8687 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8688 return ansi_opname (ARRAY_REF);
8691 /* Anything else is an error. */
8695 /* If we have selected an identifier, we need to consume the
8698 cp_lexer_consume_token (parser->lexer);
8699 /* Otherwise, no valid operator name was present. */
8702 cp_parser_error (parser, "expected operator");
8703 id = error_mark_node;
8709 /* Parse a template-declaration.
8711 template-declaration:
8712 export [opt] template < template-parameter-list > declaration
8714 If MEMBER_P is TRUE, this template-declaration occurs within a
8717 The grammar rule given by the standard isn't correct. What
8720 template-declaration:
8721 export [opt] template-parameter-list-seq
8722 decl-specifier-seq [opt] init-declarator [opt] ;
8723 export [opt] template-parameter-list-seq
8726 template-parameter-list-seq:
8727 template-parameter-list-seq [opt]
8728 template < template-parameter-list > */
8731 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8733 /* Check for `export'. */
8734 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8736 /* Consume the `export' token. */
8737 cp_lexer_consume_token (parser->lexer);
8738 /* Warn that we do not support `export'. */
8739 warning (0, "keyword %<export%> not implemented, and will be ignored");
8742 cp_parser_template_declaration_after_export (parser, member_p);
8745 /* Parse a template-parameter-list.
8747 template-parameter-list:
8749 template-parameter-list , template-parameter
8751 Returns a TREE_LIST. Each node represents a template parameter.
8752 The nodes are connected via their TREE_CHAINs. */
8755 cp_parser_template_parameter_list (cp_parser* parser)
8757 tree parameter_list = NULL_TREE;
8759 begin_template_parm_list ();
8765 bool is_parameter_pack;
8767 /* Parse the template-parameter. */
8768 parameter = cp_parser_template_parameter (parser,
8770 &is_parameter_pack);
8771 /* Add it to the list. */
8772 if (parameter != error_mark_node)
8773 parameter_list = process_template_parm (parameter_list,
8779 tree err_parm = build_tree_list (parameter, parameter);
8780 TREE_VALUE (err_parm) = error_mark_node;
8781 parameter_list = chainon (parameter_list, err_parm);
8784 /* Peek at the next token. */
8785 token = cp_lexer_peek_token (parser->lexer);
8786 /* If it's not a `,', we're done. */
8787 if (token->type != CPP_COMMA)
8789 /* Otherwise, consume the `,' token. */
8790 cp_lexer_consume_token (parser->lexer);
8793 return end_template_parm_list (parameter_list);
8796 /* Parse a template-parameter.
8800 parameter-declaration
8802 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8803 the parameter. The TREE_PURPOSE is the default value, if any.
8804 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8805 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8806 set to true iff this parameter is a parameter pack. */
8809 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8810 bool *is_parameter_pack)
8813 cp_parameter_declarator *parameter_declarator;
8816 /* Assume it is a type parameter or a template parameter. */
8817 *is_non_type = false;
8818 /* Assume it not a parameter pack. */
8819 *is_parameter_pack = false;
8820 /* Peek at the next token. */
8821 token = cp_lexer_peek_token (parser->lexer);
8822 /* If it is `class' or `template', we have a type-parameter. */
8823 if (token->keyword == RID_TEMPLATE)
8824 return cp_parser_type_parameter (parser, is_parameter_pack);
8825 /* If it is `class' or `typename' we do not know yet whether it is a
8826 type parameter or a non-type parameter. Consider:
8828 template <typename T, typename T::X X> ...
8832 template <class C, class D*> ...
8834 Here, the first parameter is a type parameter, and the second is
8835 a non-type parameter. We can tell by looking at the token after
8836 the identifier -- if it is a `,', `=', or `>' then we have a type
8838 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8840 /* Peek at the token after `class' or `typename'. */
8841 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8842 /* If it's an ellipsis, we have a template type parameter
8844 if (token->type == CPP_ELLIPSIS)
8845 return cp_parser_type_parameter (parser, is_parameter_pack);
8846 /* If it's an identifier, skip it. */
8847 if (token->type == CPP_NAME)
8848 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8849 /* Now, see if the token looks like the end of a template
8851 if (token->type == CPP_COMMA
8852 || token->type == CPP_EQ
8853 || token->type == CPP_GREATER)
8854 return cp_parser_type_parameter (parser, is_parameter_pack);
8857 /* Otherwise, it is a non-type parameter.
8861 When parsing a default template-argument for a non-type
8862 template-parameter, the first non-nested `>' is taken as the end
8863 of the template parameter-list rather than a greater-than
8865 *is_non_type = true;
8866 parameter_declarator
8867 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8868 /*parenthesized_p=*/NULL);
8870 /* If the parameter declaration is marked as a parameter pack, set
8871 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8872 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8874 if (parameter_declarator
8875 && parameter_declarator->declarator
8876 && parameter_declarator->declarator->parameter_pack_p)
8878 *is_parameter_pack = true;
8879 parameter_declarator->declarator->parameter_pack_p = false;
8882 /* If the next token is an ellipsis, and we don't already have it
8883 marked as a parameter pack, then we have a parameter pack (that
8884 has no declarator); */
8885 if (!*is_parameter_pack
8886 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8889 /* Consume the `...'. */
8890 cp_lexer_consume_token (parser->lexer);
8891 maybe_warn_variadic_templates ();
8893 *is_parameter_pack = true;
8896 parm = grokdeclarator (parameter_declarator->declarator,
8897 ¶meter_declarator->decl_specifiers,
8898 PARM, /*initialized=*/0,
8900 if (parm == error_mark_node)
8901 return error_mark_node;
8903 return build_tree_list (parameter_declarator->default_argument, parm);
8906 /* Parse a type-parameter.
8909 class identifier [opt]
8910 class identifier [opt] = type-id
8911 typename identifier [opt]
8912 typename identifier [opt] = type-id
8913 template < template-parameter-list > class identifier [opt]
8914 template < template-parameter-list > class identifier [opt]
8917 GNU Extension (variadic templates):
8920 class ... identifier [opt]
8921 typename ... identifier [opt]
8923 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8924 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8925 the declaration of the parameter.
8927 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8930 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8935 /* Look for a keyword to tell us what kind of parameter this is. */
8936 token = cp_parser_require (parser, CPP_KEYWORD,
8937 "`class', `typename', or `template'");
8939 return error_mark_node;
8941 switch (token->keyword)
8947 tree default_argument;
8949 /* If the next token is an ellipsis, we have a template
8951 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8953 /* Consume the `...' token. */
8954 cp_lexer_consume_token (parser->lexer);
8955 maybe_warn_variadic_templates ();
8957 *is_parameter_pack = true;
8960 /* If the next token is an identifier, then it names the
8962 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8963 identifier = cp_parser_identifier (parser);
8965 identifier = NULL_TREE;
8967 /* Create the parameter. */
8968 parameter = finish_template_type_parm (class_type_node, identifier);
8970 /* If the next token is an `=', we have a default argument. */
8971 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8973 /* Consume the `=' token. */
8974 cp_lexer_consume_token (parser->lexer);
8975 /* Parse the default-argument. */
8976 push_deferring_access_checks (dk_no_deferred);
8977 default_argument = cp_parser_type_id (parser);
8979 /* Template parameter packs cannot have default
8981 if (*is_parameter_pack)
8984 error ("template parameter pack %qD cannot have a default argument",
8987 error ("template parameter packs cannot have default arguments");
8988 default_argument = NULL_TREE;
8990 pop_deferring_access_checks ();
8993 default_argument = NULL_TREE;
8995 /* Create the combined representation of the parameter and the
8996 default argument. */
8997 parameter = build_tree_list (default_argument, parameter);
9003 tree parameter_list;
9005 tree default_argument;
9007 /* Look for the `<'. */
9008 cp_parser_require (parser, CPP_LESS, "`<'");
9009 /* Parse the template-parameter-list. */
9010 parameter_list = cp_parser_template_parameter_list (parser);
9011 /* Look for the `>'. */
9012 cp_parser_require (parser, CPP_GREATER, "`>'");
9013 /* Look for the `class' keyword. */
9014 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9015 /* If the next token is an ellipsis, we have a template
9017 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9019 /* Consume the `...' token. */
9020 cp_lexer_consume_token (parser->lexer);
9021 maybe_warn_variadic_templates ();
9023 *is_parameter_pack = true;
9025 /* If the next token is an `=', then there is a
9026 default-argument. If the next token is a `>', we are at
9027 the end of the parameter-list. If the next token is a `,',
9028 then we are at the end of this parameter. */
9029 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9030 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9031 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9033 identifier = cp_parser_identifier (parser);
9034 /* Treat invalid names as if the parameter were nameless. */
9035 if (identifier == error_mark_node)
9036 identifier = NULL_TREE;
9039 identifier = NULL_TREE;
9041 /* Create the template parameter. */
9042 parameter = finish_template_template_parm (class_type_node,
9045 /* If the next token is an `=', then there is a
9046 default-argument. */
9047 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9051 /* Consume the `='. */
9052 cp_lexer_consume_token (parser->lexer);
9053 /* Parse the id-expression. */
9054 push_deferring_access_checks (dk_no_deferred);
9056 = cp_parser_id_expression (parser,
9057 /*template_keyword_p=*/false,
9058 /*check_dependency_p=*/true,
9059 /*template_p=*/&is_template,
9060 /*declarator_p=*/false,
9061 /*optional_p=*/false);
9062 if (TREE_CODE (default_argument) == TYPE_DECL)
9063 /* If the id-expression was a template-id that refers to
9064 a template-class, we already have the declaration here,
9065 so no further lookup is needed. */
9068 /* Look up the name. */
9070 = cp_parser_lookup_name (parser, default_argument,
9072 /*is_template=*/is_template,
9073 /*is_namespace=*/false,
9074 /*check_dependency=*/true,
9075 /*ambiguous_decls=*/NULL);
9076 /* See if the default argument is valid. */
9078 = check_template_template_default_arg (default_argument);
9080 /* Template parameter packs cannot have default
9082 if (*is_parameter_pack)
9085 error ("template parameter pack %qD cannot have a default argument",
9088 error ("template parameter packs cannot have default arguments");
9089 default_argument = NULL_TREE;
9091 pop_deferring_access_checks ();
9094 default_argument = NULL_TREE;
9096 /* Create the combined representation of the parameter and the
9097 default argument. */
9098 parameter = build_tree_list (default_argument, parameter);
9110 /* Parse a template-id.
9113 template-name < template-argument-list [opt] >
9115 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9116 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9117 returned. Otherwise, if the template-name names a function, or set
9118 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9119 names a class, returns a TYPE_DECL for the specialization.
9121 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9122 uninstantiated templates. */
9125 cp_parser_template_id (cp_parser *parser,
9126 bool template_keyword_p,
9127 bool check_dependency_p,
9128 bool is_declaration)
9134 cp_token_position start_of_id = 0;
9135 deferred_access_check *chk;
9136 VEC (deferred_access_check,gc) *access_check;
9137 cp_token *next_token, *next_token_2;
9140 /* If the next token corresponds to a template-id, there is no need
9142 next_token = cp_lexer_peek_token (parser->lexer);
9143 if (next_token->type == CPP_TEMPLATE_ID)
9145 struct tree_check *check_value;
9147 /* Get the stored value. */
9148 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9149 /* Perform any access checks that were deferred. */
9150 access_check = check_value->checks;
9154 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9157 perform_or_defer_access_check (chk->binfo,
9162 /* Return the stored value. */
9163 return check_value->value;
9166 /* Avoid performing name lookup if there is no possibility of
9167 finding a template-id. */
9168 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9169 || (next_token->type == CPP_NAME
9170 && !cp_parser_nth_token_starts_template_argument_list_p
9173 cp_parser_error (parser, "expected template-id");
9174 return error_mark_node;
9177 /* Remember where the template-id starts. */
9178 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9179 start_of_id = cp_lexer_token_position (parser->lexer, false);
9181 push_deferring_access_checks (dk_deferred);
9183 /* Parse the template-name. */
9184 is_identifier = false;
9185 template = cp_parser_template_name (parser, template_keyword_p,
9189 if (template == error_mark_node || is_identifier)
9191 pop_deferring_access_checks ();
9195 /* If we find the sequence `[:' after a template-name, it's probably
9196 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9197 parse correctly the argument list. */
9198 next_token = cp_lexer_peek_token (parser->lexer);
9199 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9200 if (next_token->type == CPP_OPEN_SQUARE
9201 && next_token->flags & DIGRAPH
9202 && next_token_2->type == CPP_COLON
9203 && !(next_token_2->flags & PREV_WHITE))
9205 cp_parser_parse_tentatively (parser);
9206 /* Change `:' into `::'. */
9207 next_token_2->type = CPP_SCOPE;
9208 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9210 cp_lexer_consume_token (parser->lexer);
9211 /* Parse the arguments. */
9212 arguments = cp_parser_enclosed_template_argument_list (parser);
9213 if (!cp_parser_parse_definitely (parser))
9215 /* If we couldn't parse an argument list, then we revert our changes
9216 and return simply an error. Maybe this is not a template-id
9218 next_token_2->type = CPP_COLON;
9219 cp_parser_error (parser, "expected %<<%>");
9220 pop_deferring_access_checks ();
9221 return error_mark_node;
9223 /* Otherwise, emit an error about the invalid digraph, but continue
9224 parsing because we got our argument list. */
9225 pedwarn ("%<<::%> cannot begin a template-argument list");
9226 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9227 "between %<<%> and %<::%>");
9228 if (!flag_permissive)
9233 inform ("(if you use -fpermissive G++ will accept your code)");
9240 /* Look for the `<' that starts the template-argument-list. */
9241 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9243 pop_deferring_access_checks ();
9244 return error_mark_node;
9246 /* Parse the arguments. */
9247 arguments = cp_parser_enclosed_template_argument_list (parser);
9250 /* Build a representation of the specialization. */
9251 if (TREE_CODE (template) == IDENTIFIER_NODE)
9252 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9253 else if (DECL_CLASS_TEMPLATE_P (template)
9254 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9256 bool entering_scope;
9257 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9258 template (rather than some instantiation thereof) only if
9259 is not nested within some other construct. For example, in
9260 "template <typename T> void f(T) { A<T>::", A<T> is just an
9261 instantiation of A. */
9262 entering_scope = (template_parm_scope_p ()
9263 && cp_lexer_next_token_is (parser->lexer,
9266 = finish_template_type (template, arguments, entering_scope);
9270 /* If it's not a class-template or a template-template, it should be
9271 a function-template. */
9272 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9273 || TREE_CODE (template) == OVERLOAD
9274 || BASELINK_P (template)));
9276 template_id = lookup_template_function (template, arguments);
9279 /* If parsing tentatively, replace the sequence of tokens that makes
9280 up the template-id with a CPP_TEMPLATE_ID token. That way,
9281 should we re-parse the token stream, we will not have to repeat
9282 the effort required to do the parse, nor will we issue duplicate
9283 error messages about problems during instantiation of the
9287 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9289 /* Reset the contents of the START_OF_ID token. */
9290 token->type = CPP_TEMPLATE_ID;
9291 /* Retrieve any deferred checks. Do not pop this access checks yet
9292 so the memory will not be reclaimed during token replacing below. */
9293 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9294 token->u.tree_check_value->value = template_id;
9295 token->u.tree_check_value->checks = get_deferred_access_checks ();
9296 token->keyword = RID_MAX;
9298 /* Purge all subsequent tokens. */
9299 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9301 /* ??? Can we actually assume that, if template_id ==
9302 error_mark_node, we will have issued a diagnostic to the
9303 user, as opposed to simply marking the tentative parse as
9305 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9306 error ("parse error in template argument list");
9309 pop_deferring_access_checks ();
9313 /* Parse a template-name.
9318 The standard should actually say:
9322 operator-function-id
9324 A defect report has been filed about this issue.
9326 A conversion-function-id cannot be a template name because they cannot
9327 be part of a template-id. In fact, looking at this code:
9331 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9332 It is impossible to call a templated conversion-function-id with an
9333 explicit argument list, since the only allowed template parameter is
9334 the type to which it is converting.
9336 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9337 `template' keyword, in a construction like:
9341 In that case `f' is taken to be a template-name, even though there
9342 is no way of knowing for sure.
9344 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9345 name refers to a set of overloaded functions, at least one of which
9346 is a template, or an IDENTIFIER_NODE with the name of the template,
9347 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9348 names are looked up inside uninstantiated templates. */
9351 cp_parser_template_name (cp_parser* parser,
9352 bool template_keyword_p,
9353 bool check_dependency_p,
9354 bool is_declaration,
9355 bool *is_identifier)
9361 /* If the next token is `operator', then we have either an
9362 operator-function-id or a conversion-function-id. */
9363 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9365 /* We don't know whether we're looking at an
9366 operator-function-id or a conversion-function-id. */
9367 cp_parser_parse_tentatively (parser);
9368 /* Try an operator-function-id. */
9369 identifier = cp_parser_operator_function_id (parser);
9370 /* If that didn't work, try a conversion-function-id. */
9371 if (!cp_parser_parse_definitely (parser))
9373 cp_parser_error (parser, "expected template-name");
9374 return error_mark_node;
9377 /* Look for the identifier. */
9379 identifier = cp_parser_identifier (parser);
9381 /* If we didn't find an identifier, we don't have a template-id. */
9382 if (identifier == error_mark_node)
9383 return error_mark_node;
9385 /* If the name immediately followed the `template' keyword, then it
9386 is a template-name. However, if the next token is not `<', then
9387 we do not treat it as a template-name, since it is not being used
9388 as part of a template-id. This enables us to handle constructs
9391 template <typename T> struct S { S(); };
9392 template <typename T> S<T>::S();
9394 correctly. We would treat `S' as a template -- if it were `S<T>'
9395 -- but we do not if there is no `<'. */
9397 if (processing_template_decl
9398 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9400 /* In a declaration, in a dependent context, we pretend that the
9401 "template" keyword was present in order to improve error
9402 recovery. For example, given:
9404 template <typename T> void f(T::X<int>);
9406 we want to treat "X<int>" as a template-id. */
9408 && !template_keyword_p
9409 && parser->scope && TYPE_P (parser->scope)
9410 && check_dependency_p
9411 && dependent_type_p (parser->scope)
9412 /* Do not do this for dtors (or ctors), since they never
9413 need the template keyword before their name. */
9414 && !constructor_name_p (identifier, parser->scope))
9416 cp_token_position start = 0;
9418 /* Explain what went wrong. */
9419 error ("non-template %qD used as template", identifier);
9420 inform ("use %<%T::template %D%> to indicate that it is a template",
9421 parser->scope, identifier);
9422 /* If parsing tentatively, find the location of the "<" token. */
9423 if (cp_parser_simulate_error (parser))
9424 start = cp_lexer_token_position (parser->lexer, true);
9425 /* Parse the template arguments so that we can issue error
9426 messages about them. */
9427 cp_lexer_consume_token (parser->lexer);
9428 cp_parser_enclosed_template_argument_list (parser);
9429 /* Skip tokens until we find a good place from which to
9430 continue parsing. */
9431 cp_parser_skip_to_closing_parenthesis (parser,
9432 /*recovering=*/true,
9434 /*consume_paren=*/false);
9435 /* If parsing tentatively, permanently remove the
9436 template argument list. That will prevent duplicate
9437 error messages from being issued about the missing
9438 "template" keyword. */
9440 cp_lexer_purge_tokens_after (parser->lexer, start);
9442 *is_identifier = true;
9446 /* If the "template" keyword is present, then there is generally
9447 no point in doing name-lookup, so we just return IDENTIFIER.
9448 But, if the qualifying scope is non-dependent then we can
9449 (and must) do name-lookup normally. */
9450 if (template_keyword_p
9452 || (TYPE_P (parser->scope)
9453 && dependent_type_p (parser->scope))))
9457 /* Look up the name. */
9458 decl = cp_parser_lookup_name (parser, identifier,
9460 /*is_template=*/false,
9461 /*is_namespace=*/false,
9463 /*ambiguous_decls=*/NULL);
9464 decl = maybe_get_template_decl_from_type_decl (decl);
9466 /* If DECL is a template, then the name was a template-name. */
9467 if (TREE_CODE (decl) == TEMPLATE_DECL)
9471 tree fn = NULL_TREE;
9473 /* The standard does not explicitly indicate whether a name that
9474 names a set of overloaded declarations, some of which are
9475 templates, is a template-name. However, such a name should
9476 be a template-name; otherwise, there is no way to form a
9477 template-id for the overloaded templates. */
9478 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9479 if (TREE_CODE (fns) == OVERLOAD)
9480 for (fn = fns; fn; fn = OVL_NEXT (fn))
9481 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9486 /* The name does not name a template. */
9487 cp_parser_error (parser, "expected template-name");
9488 return error_mark_node;
9492 /* If DECL is dependent, and refers to a function, then just return
9493 its name; we will look it up again during template instantiation. */
9494 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9496 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9497 if (TYPE_P (scope) && dependent_type_p (scope))
9504 /* Parse a template-argument-list.
9506 template-argument-list:
9507 template-argument ... [opt]
9508 template-argument-list , template-argument ... [opt]
9510 Returns a TREE_VEC containing the arguments. */
9513 cp_parser_template_argument_list (cp_parser* parser)
9515 tree fixed_args[10];
9516 unsigned n_args = 0;
9517 unsigned alloced = 10;
9518 tree *arg_ary = fixed_args;
9520 bool saved_in_template_argument_list_p;
9522 bool saved_non_ice_p;
9524 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9525 parser->in_template_argument_list_p = true;
9526 /* Even if the template-id appears in an integral
9527 constant-expression, the contents of the argument list do
9529 saved_ice_p = parser->integral_constant_expression_p;
9530 parser->integral_constant_expression_p = false;
9531 saved_non_ice_p = parser->non_integral_constant_expression_p;
9532 parser->non_integral_constant_expression_p = false;
9533 /* Parse the arguments. */
9539 /* Consume the comma. */
9540 cp_lexer_consume_token (parser->lexer);
9542 /* Parse the template-argument. */
9543 argument = cp_parser_template_argument (parser);
9545 /* If the next token is an ellipsis, we're expanding a template
9547 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9549 /* Consume the `...' token. */
9550 cp_lexer_consume_token (parser->lexer);
9552 /* Make the argument into a TYPE_PACK_EXPANSION or
9553 EXPR_PACK_EXPANSION. */
9554 argument = make_pack_expansion (argument);
9557 if (n_args == alloced)
9561 if (arg_ary == fixed_args)
9563 arg_ary = XNEWVEC (tree, alloced);
9564 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9567 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9569 arg_ary[n_args++] = argument;
9571 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9573 vec = make_tree_vec (n_args);
9576 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9578 if (arg_ary != fixed_args)
9580 parser->non_integral_constant_expression_p = saved_non_ice_p;
9581 parser->integral_constant_expression_p = saved_ice_p;
9582 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9586 /* Parse a template-argument.
9589 assignment-expression
9593 The representation is that of an assignment-expression, type-id, or
9594 id-expression -- except that the qualified id-expression is
9595 evaluated, so that the value returned is either a DECL or an
9598 Although the standard says "assignment-expression", it forbids
9599 throw-expressions or assignments in the template argument.
9600 Therefore, we use "conditional-expression" instead. */
9603 cp_parser_template_argument (cp_parser* parser)
9608 bool maybe_type_id = false;
9612 /* There's really no way to know what we're looking at, so we just
9613 try each alternative in order.
9617 In a template-argument, an ambiguity between a type-id and an
9618 expression is resolved to a type-id, regardless of the form of
9619 the corresponding template-parameter.
9621 Therefore, we try a type-id first. */
9622 cp_parser_parse_tentatively (parser);
9623 argument = cp_parser_type_id (parser);
9624 /* If there was no error parsing the type-id but the next token is a '>>',
9625 we probably found a typo for '> >'. But there are type-id which are
9626 also valid expressions. For instance:
9628 struct X { int operator >> (int); };
9629 template <int V> struct Foo {};
9632 Here 'X()' is a valid type-id of a function type, but the user just
9633 wanted to write the expression "X() >> 5". Thus, we remember that we
9634 found a valid type-id, but we still try to parse the argument as an
9635 expression to see what happens. */
9636 if (!cp_parser_error_occurred (parser)
9637 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9639 maybe_type_id = true;
9640 cp_parser_abort_tentative_parse (parser);
9644 /* If the next token isn't a `,' or a `>', then this argument wasn't
9645 really finished. This means that the argument is not a valid
9647 if (!cp_parser_next_token_ends_template_argument_p (parser))
9648 cp_parser_error (parser, "expected template-argument");
9649 /* If that worked, we're done. */
9650 if (cp_parser_parse_definitely (parser))
9653 /* We're still not sure what the argument will be. */
9654 cp_parser_parse_tentatively (parser);
9655 /* Try a template. */
9656 argument = cp_parser_id_expression (parser,
9657 /*template_keyword_p=*/false,
9658 /*check_dependency_p=*/true,
9660 /*declarator_p=*/false,
9661 /*optional_p=*/false);
9662 /* If the next token isn't a `,' or a `>', then this argument wasn't
9664 if (!cp_parser_next_token_ends_template_argument_p (parser))
9665 cp_parser_error (parser, "expected template-argument");
9666 if (!cp_parser_error_occurred (parser))
9668 /* Figure out what is being referred to. If the id-expression
9669 was for a class template specialization, then we will have a
9670 TYPE_DECL at this point. There is no need to do name lookup
9671 at this point in that case. */
9672 if (TREE_CODE (argument) != TYPE_DECL)
9673 argument = cp_parser_lookup_name (parser, argument,
9675 /*is_template=*/template_p,
9676 /*is_namespace=*/false,
9677 /*check_dependency=*/true,
9678 /*ambiguous_decls=*/NULL);
9679 if (TREE_CODE (argument) != TEMPLATE_DECL
9680 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9681 cp_parser_error (parser, "expected template-name");
9683 if (cp_parser_parse_definitely (parser))
9685 /* It must be a non-type argument. There permitted cases are given
9686 in [temp.arg.nontype]:
9688 -- an integral constant-expression of integral or enumeration
9691 -- the name of a non-type template-parameter; or
9693 -- the name of an object or function with external linkage...
9695 -- the address of an object or function with external linkage...
9697 -- a pointer to member... */
9698 /* Look for a non-type template parameter. */
9699 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9701 cp_parser_parse_tentatively (parser);
9702 argument = cp_parser_primary_expression (parser,
9705 /*template_arg_p=*/true,
9707 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9708 || !cp_parser_next_token_ends_template_argument_p (parser))
9709 cp_parser_simulate_error (parser);
9710 if (cp_parser_parse_definitely (parser))
9714 /* If the next token is "&", the argument must be the address of an
9715 object or function with external linkage. */
9716 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9718 cp_lexer_consume_token (parser->lexer);
9719 /* See if we might have an id-expression. */
9720 token = cp_lexer_peek_token (parser->lexer);
9721 if (token->type == CPP_NAME
9722 || token->keyword == RID_OPERATOR
9723 || token->type == CPP_SCOPE
9724 || token->type == CPP_TEMPLATE_ID
9725 || token->type == CPP_NESTED_NAME_SPECIFIER)
9727 cp_parser_parse_tentatively (parser);
9728 argument = cp_parser_primary_expression (parser,
9731 /*template_arg_p=*/true,
9733 if (cp_parser_error_occurred (parser)
9734 || !cp_parser_next_token_ends_template_argument_p (parser))
9735 cp_parser_abort_tentative_parse (parser);
9738 if (TREE_CODE (argument) == INDIRECT_REF)
9740 gcc_assert (REFERENCE_REF_P (argument));
9741 argument = TREE_OPERAND (argument, 0);
9744 if (TREE_CODE (argument) == VAR_DECL)
9746 /* A variable without external linkage might still be a
9747 valid constant-expression, so no error is issued here
9748 if the external-linkage check fails. */
9749 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9750 cp_parser_simulate_error (parser);
9752 else if (is_overloaded_fn (argument))
9753 /* All overloaded functions are allowed; if the external
9754 linkage test does not pass, an error will be issued
9758 && (TREE_CODE (argument) == OFFSET_REF
9759 || TREE_CODE (argument) == SCOPE_REF))
9760 /* A pointer-to-member. */
9762 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9765 cp_parser_simulate_error (parser);
9767 if (cp_parser_parse_definitely (parser))
9770 argument = build_x_unary_op (ADDR_EXPR, argument);
9775 /* If the argument started with "&", there are no other valid
9776 alternatives at this point. */
9779 cp_parser_error (parser, "invalid non-type template argument");
9780 return error_mark_node;
9783 /* If the argument wasn't successfully parsed as a type-id followed
9784 by '>>', the argument can only be a constant expression now.
9785 Otherwise, we try parsing the constant-expression tentatively,
9786 because the argument could really be a type-id. */
9788 cp_parser_parse_tentatively (parser);
9789 argument = cp_parser_constant_expression (parser,
9790 /*allow_non_constant_p=*/false,
9791 /*non_constant_p=*/NULL);
9792 argument = fold_non_dependent_expr (argument);
9795 if (!cp_parser_next_token_ends_template_argument_p (parser))
9796 cp_parser_error (parser, "expected template-argument");
9797 if (cp_parser_parse_definitely (parser))
9799 /* We did our best to parse the argument as a non type-id, but that
9800 was the only alternative that matched (albeit with a '>' after
9801 it). We can assume it's just a typo from the user, and a
9802 diagnostic will then be issued. */
9803 return cp_parser_type_id (parser);
9806 /* Parse an explicit-instantiation.
9808 explicit-instantiation:
9809 template declaration
9811 Although the standard says `declaration', what it really means is:
9813 explicit-instantiation:
9814 template decl-specifier-seq [opt] declarator [opt] ;
9816 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9817 supposed to be allowed. A defect report has been filed about this
9822 explicit-instantiation:
9823 storage-class-specifier template
9824 decl-specifier-seq [opt] declarator [opt] ;
9825 function-specifier template
9826 decl-specifier-seq [opt] declarator [opt] ; */
9829 cp_parser_explicit_instantiation (cp_parser* parser)
9831 int declares_class_or_enum;
9832 cp_decl_specifier_seq decl_specifiers;
9833 tree extension_specifier = NULL_TREE;
9835 /* Look for an (optional) storage-class-specifier or
9836 function-specifier. */
9837 if (cp_parser_allow_gnu_extensions_p (parser))
9840 = cp_parser_storage_class_specifier_opt (parser);
9841 if (!extension_specifier)
9843 = cp_parser_function_specifier_opt (parser,
9844 /*decl_specs=*/NULL);
9847 /* Look for the `template' keyword. */
9848 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9849 /* Let the front end know that we are processing an explicit
9851 begin_explicit_instantiation ();
9852 /* [temp.explicit] says that we are supposed to ignore access
9853 control while processing explicit instantiation directives. */
9854 push_deferring_access_checks (dk_no_check);
9855 /* Parse a decl-specifier-seq. */
9856 cp_parser_decl_specifier_seq (parser,
9857 CP_PARSER_FLAGS_OPTIONAL,
9859 &declares_class_or_enum);
9860 /* If there was exactly one decl-specifier, and it declared a class,
9861 and there's no declarator, then we have an explicit type
9863 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9867 type = check_tag_decl (&decl_specifiers);
9868 /* Turn access control back on for names used during
9869 template instantiation. */
9870 pop_deferring_access_checks ();
9872 do_type_instantiation (type, extension_specifier,
9873 /*complain=*/tf_error);
9877 cp_declarator *declarator;
9880 /* Parse the declarator. */
9882 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9883 /*ctor_dtor_or_conv_p=*/NULL,
9884 /*parenthesized_p=*/NULL,
9885 /*member_p=*/false);
9886 if (declares_class_or_enum & 2)
9887 cp_parser_check_for_definition_in_return_type (declarator,
9888 decl_specifiers.type);
9889 if (declarator != cp_error_declarator)
9891 decl = grokdeclarator (declarator, &decl_specifiers,
9892 NORMAL, 0, &decl_specifiers.attributes);
9893 /* Turn access control back on for names used during
9894 template instantiation. */
9895 pop_deferring_access_checks ();
9896 /* Do the explicit instantiation. */
9897 do_decl_instantiation (decl, extension_specifier);
9901 pop_deferring_access_checks ();
9902 /* Skip the body of the explicit instantiation. */
9903 cp_parser_skip_to_end_of_statement (parser);
9906 /* We're done with the instantiation. */
9907 end_explicit_instantiation ();
9909 cp_parser_consume_semicolon_at_end_of_statement (parser);
9912 /* Parse an explicit-specialization.
9914 explicit-specialization:
9915 template < > declaration
9917 Although the standard says `declaration', what it really means is:
9919 explicit-specialization:
9920 template <> decl-specifier [opt] init-declarator [opt] ;
9921 template <> function-definition
9922 template <> explicit-specialization
9923 template <> template-declaration */
9926 cp_parser_explicit_specialization (cp_parser* parser)
9929 /* Look for the `template' keyword. */
9930 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9931 /* Look for the `<'. */
9932 cp_parser_require (parser, CPP_LESS, "`<'");
9933 /* Look for the `>'. */
9934 cp_parser_require (parser, CPP_GREATER, "`>'");
9935 /* We have processed another parameter list. */
9936 ++parser->num_template_parameter_lists;
9939 A template ... explicit specialization ... shall not have C
9941 if (current_lang_name == lang_name_c)
9943 error ("template specialization with C linkage");
9944 /* Give it C++ linkage to avoid confusing other parts of the
9946 push_lang_context (lang_name_cplusplus);
9947 need_lang_pop = true;
9950 need_lang_pop = false;
9951 /* Let the front end know that we are beginning a specialization. */
9952 if (!begin_specialization ())
9954 end_specialization ();
9955 cp_parser_skip_to_end_of_block_or_statement (parser);
9959 /* If the next keyword is `template', we need to figure out whether
9960 or not we're looking a template-declaration. */
9961 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9963 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9964 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9965 cp_parser_template_declaration_after_export (parser,
9966 /*member_p=*/false);
9968 cp_parser_explicit_specialization (parser);
9971 /* Parse the dependent declaration. */
9972 cp_parser_single_declaration (parser,
9976 /* We're done with the specialization. */
9977 end_specialization ();
9978 /* For the erroneous case of a template with C linkage, we pushed an
9979 implicit C++ linkage scope; exit that scope now. */
9981 pop_lang_context ();
9982 /* We're done with this parameter list. */
9983 --parser->num_template_parameter_lists;
9986 /* Parse a type-specifier.
9989 simple-type-specifier
9992 elaborated-type-specifier
10000 Returns a representation of the type-specifier. For a
10001 class-specifier, enum-specifier, or elaborated-type-specifier, a
10002 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10004 The parser flags FLAGS is used to control type-specifier parsing.
10006 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10007 in a decl-specifier-seq.
10009 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10010 class-specifier, enum-specifier, or elaborated-type-specifier, then
10011 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10012 if a type is declared; 2 if it is defined. Otherwise, it is set to
10015 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10016 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10017 is set to FALSE. */
10020 cp_parser_type_specifier (cp_parser* parser,
10021 cp_parser_flags flags,
10022 cp_decl_specifier_seq *decl_specs,
10023 bool is_declaration,
10024 int* declares_class_or_enum,
10025 bool* is_cv_qualifier)
10027 tree type_spec = NULL_TREE;
10030 cp_decl_spec ds = ds_last;
10032 /* Assume this type-specifier does not declare a new type. */
10033 if (declares_class_or_enum)
10034 *declares_class_or_enum = 0;
10035 /* And that it does not specify a cv-qualifier. */
10036 if (is_cv_qualifier)
10037 *is_cv_qualifier = false;
10038 /* Peek at the next token. */
10039 token = cp_lexer_peek_token (parser->lexer);
10041 /* If we're looking at a keyword, we can use that to guide the
10042 production we choose. */
10043 keyword = token->keyword;
10047 /* Look for the enum-specifier. */
10048 type_spec = cp_parser_enum_specifier (parser);
10049 /* If that worked, we're done. */
10052 if (declares_class_or_enum)
10053 *declares_class_or_enum = 2;
10055 cp_parser_set_decl_spec_type (decl_specs,
10057 /*user_defined_p=*/true);
10061 goto elaborated_type_specifier;
10063 /* Any of these indicate either a class-specifier, or an
10064 elaborated-type-specifier. */
10068 /* Parse tentatively so that we can back up if we don't find a
10069 class-specifier. */
10070 cp_parser_parse_tentatively (parser);
10071 /* Look for the class-specifier. */
10072 type_spec = cp_parser_class_specifier (parser);
10073 /* If that worked, we're done. */
10074 if (cp_parser_parse_definitely (parser))
10076 if (declares_class_or_enum)
10077 *declares_class_or_enum = 2;
10079 cp_parser_set_decl_spec_type (decl_specs,
10081 /*user_defined_p=*/true);
10085 /* Fall through. */
10086 elaborated_type_specifier:
10087 /* We're declaring (not defining) a class or enum. */
10088 if (declares_class_or_enum)
10089 *declares_class_or_enum = 1;
10091 /* Fall through. */
10093 /* Look for an elaborated-type-specifier. */
10095 = (cp_parser_elaborated_type_specifier
10097 decl_specs && decl_specs->specs[(int) ds_friend],
10100 cp_parser_set_decl_spec_type (decl_specs,
10102 /*user_defined_p=*/true);
10107 if (is_cv_qualifier)
10108 *is_cv_qualifier = true;
10113 if (is_cv_qualifier)
10114 *is_cv_qualifier = true;
10119 if (is_cv_qualifier)
10120 *is_cv_qualifier = true;
10124 /* The `__complex__' keyword is a GNU extension. */
10132 /* Handle simple keywords. */
10137 ++decl_specs->specs[(int)ds];
10138 decl_specs->any_specifiers_p = true;
10140 return cp_lexer_consume_token (parser->lexer)->u.value;
10143 /* If we do not already have a type-specifier, assume we are looking
10144 at a simple-type-specifier. */
10145 type_spec = cp_parser_simple_type_specifier (parser,
10149 /* If we didn't find a type-specifier, and a type-specifier was not
10150 optional in this context, issue an error message. */
10151 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10153 cp_parser_error (parser, "expected type specifier");
10154 return error_mark_node;
10160 /* Parse a simple-type-specifier.
10162 simple-type-specifier:
10163 :: [opt] nested-name-specifier [opt] type-name
10164 :: [opt] nested-name-specifier template template-id
10179 simple-type-specifier:
10180 __typeof__ unary-expression
10181 __typeof__ ( type-id )
10183 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10184 appropriately updated. */
10187 cp_parser_simple_type_specifier (cp_parser* parser,
10188 cp_decl_specifier_seq *decl_specs,
10189 cp_parser_flags flags)
10191 tree type = NULL_TREE;
10194 /* Peek at the next token. */
10195 token = cp_lexer_peek_token (parser->lexer);
10197 /* If we're looking at a keyword, things are easy. */
10198 switch (token->keyword)
10202 decl_specs->explicit_char_p = true;
10203 type = char_type_node;
10206 type = wchar_type_node;
10209 type = boolean_type_node;
10213 ++decl_specs->specs[(int) ds_short];
10214 type = short_integer_type_node;
10218 decl_specs->explicit_int_p = true;
10219 type = integer_type_node;
10223 ++decl_specs->specs[(int) ds_long];
10224 type = long_integer_type_node;
10228 ++decl_specs->specs[(int) ds_signed];
10229 type = integer_type_node;
10233 ++decl_specs->specs[(int) ds_unsigned];
10234 type = unsigned_type_node;
10237 type = float_type_node;
10240 type = double_type_node;
10243 type = void_type_node;
10247 /* Consume the `typeof' token. */
10248 cp_lexer_consume_token (parser->lexer);
10249 /* Parse the operand to `typeof'. */
10250 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10251 /* If it is not already a TYPE, take its type. */
10252 if (!TYPE_P (type))
10253 type = finish_typeof (type);
10256 cp_parser_set_decl_spec_type (decl_specs, type,
10257 /*user_defined_p=*/true);
10265 /* If the type-specifier was for a built-in type, we're done. */
10270 /* Record the type. */
10272 && (token->keyword != RID_SIGNED
10273 && token->keyword != RID_UNSIGNED
10274 && token->keyword != RID_SHORT
10275 && token->keyword != RID_LONG))
10276 cp_parser_set_decl_spec_type (decl_specs,
10278 /*user_defined=*/false);
10280 decl_specs->any_specifiers_p = true;
10282 /* Consume the token. */
10283 id = cp_lexer_consume_token (parser->lexer)->u.value;
10285 /* There is no valid C++ program where a non-template type is
10286 followed by a "<". That usually indicates that the user thought
10287 that the type was a template. */
10288 cp_parser_check_for_invalid_template_id (parser, type);
10290 return TYPE_NAME (type);
10293 /* The type-specifier must be a user-defined type. */
10294 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10299 /* Don't gobble tokens or issue error messages if this is an
10300 optional type-specifier. */
10301 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10302 cp_parser_parse_tentatively (parser);
10304 /* Look for the optional `::' operator. */
10306 = (cp_parser_global_scope_opt (parser,
10307 /*current_scope_valid_p=*/false)
10309 /* Look for the nested-name specifier. */
10311 = (cp_parser_nested_name_specifier_opt (parser,
10312 /*typename_keyword_p=*/false,
10313 /*check_dependency_p=*/true,
10315 /*is_declaration=*/false)
10317 /* If we have seen a nested-name-specifier, and the next token
10318 is `template', then we are using the template-id production. */
10320 && cp_parser_optional_template_keyword (parser))
10322 /* Look for the template-id. */
10323 type = cp_parser_template_id (parser,
10324 /*template_keyword_p=*/true,
10325 /*check_dependency_p=*/true,
10326 /*is_declaration=*/false);
10327 /* If the template-id did not name a type, we are out of
10329 if (TREE_CODE (type) != TYPE_DECL)
10331 cp_parser_error (parser, "expected template-id for type");
10335 /* Otherwise, look for a type-name. */
10337 type = cp_parser_type_name (parser);
10338 /* Keep track of all name-lookups performed in class scopes. */
10342 && TREE_CODE (type) == TYPE_DECL
10343 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10344 maybe_note_name_used_in_class (DECL_NAME (type), type);
10345 /* If it didn't work out, we don't have a TYPE. */
10346 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10347 && !cp_parser_parse_definitely (parser))
10349 if (type && decl_specs)
10350 cp_parser_set_decl_spec_type (decl_specs, type,
10351 /*user_defined=*/true);
10354 /* If we didn't get a type-name, issue an error message. */
10355 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10357 cp_parser_error (parser, "expected type-name");
10358 return error_mark_node;
10361 /* There is no valid C++ program where a non-template type is
10362 followed by a "<". That usually indicates that the user thought
10363 that the type was a template. */
10364 if (type && type != error_mark_node)
10366 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10367 If it is, then the '<'...'>' enclose protocol names rather than
10368 template arguments, and so everything is fine. */
10369 if (c_dialect_objc ()
10370 && (objc_is_id (type) || objc_is_class_name (type)))
10372 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10373 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10375 /* Clobber the "unqualified" type previously entered into
10376 DECL_SPECS with the new, improved protocol-qualified version. */
10378 decl_specs->type = qual_type;
10383 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10389 /* Parse a type-name.
10402 Returns a TYPE_DECL for the type. */
10405 cp_parser_type_name (cp_parser* parser)
10410 /* We can't know yet whether it is a class-name or not. */
10411 cp_parser_parse_tentatively (parser);
10412 /* Try a class-name. */
10413 type_decl = cp_parser_class_name (parser,
10414 /*typename_keyword_p=*/false,
10415 /*template_keyword_p=*/false,
10417 /*check_dependency_p=*/true,
10418 /*class_head_p=*/false,
10419 /*is_declaration=*/false);
10420 /* If it's not a class-name, keep looking. */
10421 if (!cp_parser_parse_definitely (parser))
10423 /* It must be a typedef-name or an enum-name. */
10424 identifier = cp_parser_identifier (parser);
10425 if (identifier == error_mark_node)
10426 return error_mark_node;
10428 /* Look up the type-name. */
10429 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10431 if (TREE_CODE (type_decl) != TYPE_DECL
10432 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10434 /* See if this is an Objective-C type. */
10435 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10436 tree type = objc_get_protocol_qualified_type (identifier, protos);
10438 type_decl = TYPE_NAME (type);
10441 /* Issue an error if we did not find a type-name. */
10442 if (TREE_CODE (type_decl) != TYPE_DECL)
10444 if (!cp_parser_simulate_error (parser))
10445 cp_parser_name_lookup_error (parser, identifier, type_decl,
10447 type_decl = error_mark_node;
10449 /* Remember that the name was used in the definition of the
10450 current class so that we can check later to see if the
10451 meaning would have been different after the class was
10452 entirely defined. */
10453 else if (type_decl != error_mark_node
10455 maybe_note_name_used_in_class (identifier, type_decl);
10462 /* Parse an elaborated-type-specifier. Note that the grammar given
10463 here incorporates the resolution to DR68.
10465 elaborated-type-specifier:
10466 class-key :: [opt] nested-name-specifier [opt] identifier
10467 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10468 enum :: [opt] nested-name-specifier [opt] identifier
10469 typename :: [opt] nested-name-specifier identifier
10470 typename :: [opt] nested-name-specifier template [opt]
10475 elaborated-type-specifier:
10476 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10477 class-key attributes :: [opt] nested-name-specifier [opt]
10478 template [opt] template-id
10479 enum attributes :: [opt] nested-name-specifier [opt] identifier
10481 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10482 declared `friend'. If IS_DECLARATION is TRUE, then this
10483 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10484 something is being declared.
10486 Returns the TYPE specified. */
10489 cp_parser_elaborated_type_specifier (cp_parser* parser,
10491 bool is_declaration)
10493 enum tag_types tag_type;
10495 tree type = NULL_TREE;
10496 tree attributes = NULL_TREE;
10498 /* See if we're looking at the `enum' keyword. */
10499 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10501 /* Consume the `enum' token. */
10502 cp_lexer_consume_token (parser->lexer);
10503 /* Remember that it's an enumeration type. */
10504 tag_type = enum_type;
10505 /* Parse the attributes. */
10506 attributes = cp_parser_attributes_opt (parser);
10508 /* Or, it might be `typename'. */
10509 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10512 /* Consume the `typename' token. */
10513 cp_lexer_consume_token (parser->lexer);
10514 /* Remember that it's a `typename' type. */
10515 tag_type = typename_type;
10516 /* The `typename' keyword is only allowed in templates. */
10517 if (!processing_template_decl)
10518 pedwarn ("using %<typename%> outside of template");
10520 /* Otherwise it must be a class-key. */
10523 tag_type = cp_parser_class_key (parser);
10524 if (tag_type == none_type)
10525 return error_mark_node;
10526 /* Parse the attributes. */
10527 attributes = cp_parser_attributes_opt (parser);
10530 /* Look for the `::' operator. */
10531 cp_parser_global_scope_opt (parser,
10532 /*current_scope_valid_p=*/false);
10533 /* Look for the nested-name-specifier. */
10534 if (tag_type == typename_type)
10536 if (!cp_parser_nested_name_specifier (parser,
10537 /*typename_keyword_p=*/true,
10538 /*check_dependency_p=*/true,
10541 return error_mark_node;
10544 /* Even though `typename' is not present, the proposed resolution
10545 to Core Issue 180 says that in `class A<T>::B', `B' should be
10546 considered a type-name, even if `A<T>' is dependent. */
10547 cp_parser_nested_name_specifier_opt (parser,
10548 /*typename_keyword_p=*/true,
10549 /*check_dependency_p=*/true,
10552 /* For everything but enumeration types, consider a template-id.
10553 For an enumeration type, consider only a plain identifier. */
10554 if (tag_type != enum_type)
10556 bool template_p = false;
10559 /* Allow the `template' keyword. */
10560 template_p = cp_parser_optional_template_keyword (parser);
10561 /* If we didn't see `template', we don't know if there's a
10562 template-id or not. */
10564 cp_parser_parse_tentatively (parser);
10565 /* Parse the template-id. */
10566 decl = cp_parser_template_id (parser, template_p,
10567 /*check_dependency_p=*/true,
10569 /* If we didn't find a template-id, look for an ordinary
10571 if (!template_p && !cp_parser_parse_definitely (parser))
10573 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10574 in effect, then we must assume that, upon instantiation, the
10575 template will correspond to a class. */
10576 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10577 && tag_type == typename_type)
10578 type = make_typename_type (parser->scope, decl,
10580 /*complain=*/tf_error);
10582 type = TREE_TYPE (decl);
10587 identifier = cp_parser_identifier (parser);
10589 if (identifier == error_mark_node)
10591 parser->scope = NULL_TREE;
10592 return error_mark_node;
10595 /* For a `typename', we needn't call xref_tag. */
10596 if (tag_type == typename_type
10597 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10598 return cp_parser_make_typename_type (parser, parser->scope,
10600 /* Look up a qualified name in the usual way. */
10605 decl = cp_parser_lookup_name (parser, identifier,
10607 /*is_template=*/false,
10608 /*is_namespace=*/false,
10609 /*check_dependency=*/true,
10610 /*ambiguous_decls=*/NULL);
10612 /* If we are parsing friend declaration, DECL may be a
10613 TEMPLATE_DECL tree node here. However, we need to check
10614 whether this TEMPLATE_DECL results in valid code. Consider
10615 the following example:
10618 template <class T> class C {};
10621 template <class T> friend class N::C; // #1, valid code
10623 template <class T> class Y {
10624 friend class N::C; // #2, invalid code
10627 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10628 name lookup of `N::C'. We see that friend declaration must
10629 be template for the code to be valid. Note that
10630 processing_template_decl does not work here since it is
10631 always 1 for the above two cases. */
10633 decl = (cp_parser_maybe_treat_template_as_class
10634 (decl, /*tag_name_p=*/is_friend
10635 && parser->num_template_parameter_lists));
10637 if (TREE_CODE (decl) != TYPE_DECL)
10639 cp_parser_diagnose_invalid_type_name (parser,
10642 return error_mark_node;
10645 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10647 bool allow_template = (parser->num_template_parameter_lists
10648 || DECL_SELF_REFERENCE_P (decl));
10649 type = check_elaborated_type_specifier (tag_type, decl,
10652 if (type == error_mark_node)
10653 return error_mark_node;
10656 type = TREE_TYPE (decl);
10660 /* An elaborated-type-specifier sometimes introduces a new type and
10661 sometimes names an existing type. Normally, the rule is that it
10662 introduces a new type only if there is not an existing type of
10663 the same name already in scope. For example, given:
10666 void f() { struct S s; }
10668 the `struct S' in the body of `f' is the same `struct S' as in
10669 the global scope; the existing definition is used. However, if
10670 there were no global declaration, this would introduce a new
10671 local class named `S'.
10673 An exception to this rule applies to the following code:
10675 namespace N { struct S; }
10677 Here, the elaborated-type-specifier names a new type
10678 unconditionally; even if there is already an `S' in the
10679 containing scope this declaration names a new type.
10680 This exception only applies if the elaborated-type-specifier
10681 forms the complete declaration:
10685 A declaration consisting solely of `class-key identifier ;' is
10686 either a redeclaration of the name in the current scope or a
10687 forward declaration of the identifier as a class name. It
10688 introduces the name into the current scope.
10690 We are in this situation precisely when the next token is a `;'.
10692 An exception to the exception is that a `friend' declaration does
10693 *not* name a new type; i.e., given:
10695 struct S { friend struct T; };
10697 `T' is not a new type in the scope of `S'.
10699 Also, `new struct S' or `sizeof (struct S)' never results in the
10700 definition of a new type; a new type can only be declared in a
10701 declaration context. */
10707 /* Friends have special name lookup rules. */
10708 ts = ts_within_enclosing_non_class;
10709 else if (is_declaration
10710 && cp_lexer_next_token_is (parser->lexer,
10712 /* This is a `class-key identifier ;' */
10718 (parser->num_template_parameter_lists
10719 && (cp_parser_next_token_starts_class_definition_p (parser)
10720 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10721 /* An unqualified name was used to reference this type, so
10722 there were no qualifying templates. */
10723 if (!cp_parser_check_template_parameters (parser,
10724 /*num_templates=*/0))
10725 return error_mark_node;
10726 type = xref_tag (tag_type, identifier, ts, template_p);
10730 if (type == error_mark_node)
10731 return error_mark_node;
10733 /* Allow attributes on forward declarations of classes. */
10736 if (TREE_CODE (type) == TYPENAME_TYPE)
10737 warning (OPT_Wattributes,
10738 "attributes ignored on uninstantiated type");
10739 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10740 && ! processing_explicit_instantiation)
10741 warning (OPT_Wattributes,
10742 "attributes ignored on template instantiation");
10743 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10744 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10746 warning (OPT_Wattributes,
10747 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10750 if (tag_type != enum_type)
10751 cp_parser_check_class_key (tag_type, type);
10753 /* A "<" cannot follow an elaborated type specifier. If that
10754 happens, the user was probably trying to form a template-id. */
10755 cp_parser_check_for_invalid_template_id (parser, type);
10760 /* Parse an enum-specifier.
10763 enum identifier [opt] { enumerator-list [opt] }
10766 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10769 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10770 if the token stream isn't an enum-specifier after all. */
10773 cp_parser_enum_specifier (cp_parser* parser)
10779 /* Parse tentatively so that we can back up if we don't find a
10781 cp_parser_parse_tentatively (parser);
10783 /* Caller guarantees that the current token is 'enum', an identifier
10784 possibly follows, and the token after that is an opening brace.
10785 If we don't have an identifier, fabricate an anonymous name for
10786 the enumeration being defined. */
10787 cp_lexer_consume_token (parser->lexer);
10789 attributes = cp_parser_attributes_opt (parser);
10791 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10792 identifier = cp_parser_identifier (parser);
10794 identifier = make_anon_name ();
10796 /* Look for the `{' but don't consume it yet. */
10797 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10798 cp_parser_simulate_error (parser);
10800 if (!cp_parser_parse_definitely (parser))
10803 /* Issue an error message if type-definitions are forbidden here. */
10804 if (!cp_parser_check_type_definition (parser))
10805 type = error_mark_node;
10807 /* Create the new type. We do this before consuming the opening
10808 brace so the enum will be recorded as being on the line of its
10809 tag (or the 'enum' keyword, if there is no tag). */
10810 type = start_enum (identifier);
10812 /* Consume the opening brace. */
10813 cp_lexer_consume_token (parser->lexer);
10815 if (type == error_mark_node)
10817 cp_parser_skip_to_end_of_block_or_statement (parser);
10818 return error_mark_node;
10821 /* If the next token is not '}', then there are some enumerators. */
10822 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10823 cp_parser_enumerator_list (parser, type);
10825 /* Consume the final '}'. */
10826 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10828 /* Look for trailing attributes to apply to this enumeration, and
10829 apply them if appropriate. */
10830 if (cp_parser_allow_gnu_extensions_p (parser))
10832 tree trailing_attr = cp_parser_attributes_opt (parser);
10833 cplus_decl_attributes (&type,
10835 (int) ATTR_FLAG_TYPE_IN_PLACE);
10838 /* Finish up the enumeration. */
10839 finish_enum (type);
10844 /* Parse an enumerator-list. The enumerators all have the indicated
10848 enumerator-definition
10849 enumerator-list , enumerator-definition */
10852 cp_parser_enumerator_list (cp_parser* parser, tree type)
10856 /* Parse an enumerator-definition. */
10857 cp_parser_enumerator_definition (parser, type);
10859 /* If the next token is not a ',', we've reached the end of
10861 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10863 /* Otherwise, consume the `,' and keep going. */
10864 cp_lexer_consume_token (parser->lexer);
10865 /* If the next token is a `}', there is a trailing comma. */
10866 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10868 if (pedantic && !in_system_header)
10869 pedwarn ("comma at end of enumerator list");
10875 /* Parse an enumerator-definition. The enumerator has the indicated
10878 enumerator-definition:
10880 enumerator = constant-expression
10886 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10891 /* Look for the identifier. */
10892 identifier = cp_parser_identifier (parser);
10893 if (identifier == error_mark_node)
10896 /* If the next token is an '=', then there is an explicit value. */
10897 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10899 /* Consume the `=' token. */
10900 cp_lexer_consume_token (parser->lexer);
10901 /* Parse the value. */
10902 value = cp_parser_constant_expression (parser,
10903 /*allow_non_constant_p=*/false,
10909 /* Create the enumerator. */
10910 build_enumerator (identifier, value, type);
10913 /* Parse a namespace-name.
10916 original-namespace-name
10919 Returns the NAMESPACE_DECL for the namespace. */
10922 cp_parser_namespace_name (cp_parser* parser)
10925 tree namespace_decl;
10927 /* Get the name of the namespace. */
10928 identifier = cp_parser_identifier (parser);
10929 if (identifier == error_mark_node)
10930 return error_mark_node;
10932 /* Look up the identifier in the currently active scope. Look only
10933 for namespaces, due to:
10935 [basic.lookup.udir]
10937 When looking up a namespace-name in a using-directive or alias
10938 definition, only namespace names are considered.
10942 [basic.lookup.qual]
10944 During the lookup of a name preceding the :: scope resolution
10945 operator, object, function, and enumerator names are ignored.
10947 (Note that cp_parser_class_or_namespace_name only calls this
10948 function if the token after the name is the scope resolution
10950 namespace_decl = cp_parser_lookup_name (parser, identifier,
10952 /*is_template=*/false,
10953 /*is_namespace=*/true,
10954 /*check_dependency=*/true,
10955 /*ambiguous_decls=*/NULL);
10956 /* If it's not a namespace, issue an error. */
10957 if (namespace_decl == error_mark_node
10958 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10960 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10961 error ("%qD is not a namespace-name", identifier);
10962 cp_parser_error (parser, "expected namespace-name");
10963 namespace_decl = error_mark_node;
10966 return namespace_decl;
10969 /* Parse a namespace-definition.
10971 namespace-definition:
10972 named-namespace-definition
10973 unnamed-namespace-definition
10975 named-namespace-definition:
10976 original-namespace-definition
10977 extension-namespace-definition
10979 original-namespace-definition:
10980 namespace identifier { namespace-body }
10982 extension-namespace-definition:
10983 namespace original-namespace-name { namespace-body }
10985 unnamed-namespace-definition:
10986 namespace { namespace-body } */
10989 cp_parser_namespace_definition (cp_parser* parser)
10991 tree identifier, attribs;
10993 /* Look for the `namespace' keyword. */
10994 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10996 /* Get the name of the namespace. We do not attempt to distinguish
10997 between an original-namespace-definition and an
10998 extension-namespace-definition at this point. The semantic
10999 analysis routines are responsible for that. */
11000 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11001 identifier = cp_parser_identifier (parser);
11003 identifier = NULL_TREE;
11005 /* Parse any specified attributes. */
11006 attribs = cp_parser_attributes_opt (parser);
11008 /* Look for the `{' to start the namespace. */
11009 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11010 /* Start the namespace. */
11011 push_namespace_with_attribs (identifier, attribs);
11012 /* Parse the body of the namespace. */
11013 cp_parser_namespace_body (parser);
11014 /* Finish the namespace. */
11016 /* Look for the final `}'. */
11017 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11020 /* Parse a namespace-body.
11023 declaration-seq [opt] */
11026 cp_parser_namespace_body (cp_parser* parser)
11028 cp_parser_declaration_seq_opt (parser);
11031 /* Parse a namespace-alias-definition.
11033 namespace-alias-definition:
11034 namespace identifier = qualified-namespace-specifier ; */
11037 cp_parser_namespace_alias_definition (cp_parser* parser)
11040 tree namespace_specifier;
11042 /* Look for the `namespace' keyword. */
11043 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11044 /* Look for the identifier. */
11045 identifier = cp_parser_identifier (parser);
11046 if (identifier == error_mark_node)
11048 /* Look for the `=' token. */
11049 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11050 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11052 error ("%<namespace%> definition is not allowed here");
11053 /* Skip the definition. */
11054 cp_lexer_consume_token (parser->lexer);
11055 cp_parser_skip_to_closing_brace (parser);
11056 cp_lexer_consume_token (parser->lexer);
11059 cp_parser_require (parser, CPP_EQ, "`='");
11060 /* Look for the qualified-namespace-specifier. */
11061 namespace_specifier
11062 = cp_parser_qualified_namespace_specifier (parser);
11063 /* Look for the `;' token. */
11064 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11066 /* Register the alias in the symbol table. */
11067 do_namespace_alias (identifier, namespace_specifier);
11070 /* Parse a qualified-namespace-specifier.
11072 qualified-namespace-specifier:
11073 :: [opt] nested-name-specifier [opt] namespace-name
11075 Returns a NAMESPACE_DECL corresponding to the specified
11079 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11081 /* Look for the optional `::'. */
11082 cp_parser_global_scope_opt (parser,
11083 /*current_scope_valid_p=*/false);
11085 /* Look for the optional nested-name-specifier. */
11086 cp_parser_nested_name_specifier_opt (parser,
11087 /*typename_keyword_p=*/false,
11088 /*check_dependency_p=*/true,
11090 /*is_declaration=*/true);
11092 return cp_parser_namespace_name (parser);
11095 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11096 access declaration.
11099 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11100 using :: unqualified-id ;
11102 access-declaration:
11108 cp_parser_using_declaration (cp_parser* parser,
11109 bool access_declaration_p)
11112 bool typename_p = false;
11113 bool global_scope_p;
11118 if (access_declaration_p)
11119 cp_parser_parse_tentatively (parser);
11122 /* Look for the `using' keyword. */
11123 cp_parser_require_keyword (parser, RID_USING, "`using'");
11125 /* Peek at the next token. */
11126 token = cp_lexer_peek_token (parser->lexer);
11127 /* See if it's `typename'. */
11128 if (token->keyword == RID_TYPENAME)
11130 /* Remember that we've seen it. */
11132 /* Consume the `typename' token. */
11133 cp_lexer_consume_token (parser->lexer);
11137 /* Look for the optional global scope qualification. */
11139 = (cp_parser_global_scope_opt (parser,
11140 /*current_scope_valid_p=*/false)
11143 /* If we saw `typename', or didn't see `::', then there must be a
11144 nested-name-specifier present. */
11145 if (typename_p || !global_scope_p)
11146 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11147 /*check_dependency_p=*/true,
11149 /*is_declaration=*/true);
11150 /* Otherwise, we could be in either of the two productions. In that
11151 case, treat the nested-name-specifier as optional. */
11153 qscope = cp_parser_nested_name_specifier_opt (parser,
11154 /*typename_keyword_p=*/false,
11155 /*check_dependency_p=*/true,
11157 /*is_declaration=*/true);
11159 qscope = global_namespace;
11161 if (access_declaration_p && cp_parser_error_occurred (parser))
11162 /* Something has already gone wrong; there's no need to parse
11163 further. Since an error has occurred, the return value of
11164 cp_parser_parse_definitely will be false, as required. */
11165 return cp_parser_parse_definitely (parser);
11167 /* Parse the unqualified-id. */
11168 identifier = cp_parser_unqualified_id (parser,
11169 /*template_keyword_p=*/false,
11170 /*check_dependency_p=*/true,
11171 /*declarator_p=*/true,
11172 /*optional_p=*/false);
11174 if (access_declaration_p)
11176 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11177 cp_parser_simulate_error (parser);
11178 if (!cp_parser_parse_definitely (parser))
11182 /* The function we call to handle a using-declaration is different
11183 depending on what scope we are in. */
11184 if (qscope == error_mark_node || identifier == error_mark_node)
11186 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11187 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11188 /* [namespace.udecl]
11190 A using declaration shall not name a template-id. */
11191 error ("a template-id may not appear in a using-declaration");
11194 if (at_class_scope_p ())
11196 /* Create the USING_DECL. */
11197 decl = do_class_using_decl (parser->scope, identifier);
11198 /* Add it to the list of members in this class. */
11199 finish_member_declaration (decl);
11203 decl = cp_parser_lookup_name_simple (parser, identifier);
11204 if (decl == error_mark_node)
11205 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11206 else if (!at_namespace_scope_p ())
11207 do_local_using_decl (decl, qscope, identifier);
11209 do_toplevel_using_decl (decl, qscope, identifier);
11213 /* Look for the final `;'. */
11214 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11219 /* Parse a using-directive.
11222 using namespace :: [opt] nested-name-specifier [opt]
11223 namespace-name ; */
11226 cp_parser_using_directive (cp_parser* parser)
11228 tree namespace_decl;
11231 /* Look for the `using' keyword. */
11232 cp_parser_require_keyword (parser, RID_USING, "`using'");
11233 /* And the `namespace' keyword. */
11234 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11235 /* Look for the optional `::' operator. */
11236 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11237 /* And the optional nested-name-specifier. */
11238 cp_parser_nested_name_specifier_opt (parser,
11239 /*typename_keyword_p=*/false,
11240 /*check_dependency_p=*/true,
11242 /*is_declaration=*/true);
11243 /* Get the namespace being used. */
11244 namespace_decl = cp_parser_namespace_name (parser);
11245 /* And any specified attributes. */
11246 attribs = cp_parser_attributes_opt (parser);
11247 /* Update the symbol table. */
11248 parse_using_directive (namespace_decl, attribs);
11249 /* Look for the final `;'. */
11250 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11253 /* Parse an asm-definition.
11256 asm ( string-literal ) ;
11261 asm volatile [opt] ( string-literal ) ;
11262 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11263 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11264 : asm-operand-list [opt] ) ;
11265 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11266 : asm-operand-list [opt]
11267 : asm-operand-list [opt] ) ; */
11270 cp_parser_asm_definition (cp_parser* parser)
11273 tree outputs = NULL_TREE;
11274 tree inputs = NULL_TREE;
11275 tree clobbers = NULL_TREE;
11277 bool volatile_p = false;
11278 bool extended_p = false;
11280 /* Look for the `asm' keyword. */
11281 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11282 /* See if the next token is `volatile'. */
11283 if (cp_parser_allow_gnu_extensions_p (parser)
11284 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11286 /* Remember that we saw the `volatile' keyword. */
11288 /* Consume the token. */
11289 cp_lexer_consume_token (parser->lexer);
11291 /* Look for the opening `('. */
11292 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11294 /* Look for the string. */
11295 string = cp_parser_string_literal (parser, false, false);
11296 if (string == error_mark_node)
11298 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11299 /*consume_paren=*/true);
11303 /* If we're allowing GNU extensions, check for the extended assembly
11304 syntax. Unfortunately, the `:' tokens need not be separated by
11305 a space in C, and so, for compatibility, we tolerate that here
11306 too. Doing that means that we have to treat the `::' operator as
11308 if (cp_parser_allow_gnu_extensions_p (parser)
11309 && parser->in_function_body
11310 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11311 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11313 bool inputs_p = false;
11314 bool clobbers_p = false;
11316 /* The extended syntax was used. */
11319 /* Look for outputs. */
11320 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11322 /* Consume the `:'. */
11323 cp_lexer_consume_token (parser->lexer);
11324 /* Parse the output-operands. */
11325 if (cp_lexer_next_token_is_not (parser->lexer,
11327 && cp_lexer_next_token_is_not (parser->lexer,
11329 && cp_lexer_next_token_is_not (parser->lexer,
11331 outputs = cp_parser_asm_operand_list (parser);
11333 /* If the next token is `::', there are no outputs, and the
11334 next token is the beginning of the inputs. */
11335 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11336 /* The inputs are coming next. */
11339 /* Look for inputs. */
11341 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11343 /* Consume the `:' or `::'. */
11344 cp_lexer_consume_token (parser->lexer);
11345 /* Parse the output-operands. */
11346 if (cp_lexer_next_token_is_not (parser->lexer,
11348 && cp_lexer_next_token_is_not (parser->lexer,
11350 inputs = cp_parser_asm_operand_list (parser);
11352 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11353 /* The clobbers are coming next. */
11356 /* Look for clobbers. */
11358 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11360 /* Consume the `:' or `::'. */
11361 cp_lexer_consume_token (parser->lexer);
11362 /* Parse the clobbers. */
11363 if (cp_lexer_next_token_is_not (parser->lexer,
11365 clobbers = cp_parser_asm_clobber_list (parser);
11368 /* Look for the closing `)'. */
11369 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11370 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11371 /*consume_paren=*/true);
11372 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11374 /* Create the ASM_EXPR. */
11375 if (parser->in_function_body)
11377 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11379 /* If the extended syntax was not used, mark the ASM_EXPR. */
11382 tree temp = asm_stmt;
11383 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11384 temp = TREE_OPERAND (temp, 0);
11386 ASM_INPUT_P (temp) = 1;
11390 cgraph_add_asm_node (string);
11393 /* Declarators [gram.dcl.decl] */
11395 /* Parse an init-declarator.
11398 declarator initializer [opt]
11403 declarator asm-specification [opt] attributes [opt] initializer [opt]
11405 function-definition:
11406 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11408 decl-specifier-seq [opt] declarator function-try-block
11412 function-definition:
11413 __extension__ function-definition
11415 The DECL_SPECIFIERS apply to this declarator. Returns a
11416 representation of the entity declared. If MEMBER_P is TRUE, then
11417 this declarator appears in a class scope. The new DECL created by
11418 this declarator is returned.
11420 The CHECKS are access checks that should be performed once we know
11421 what entity is being declared (and, therefore, what classes have
11424 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11425 for a function-definition here as well. If the declarator is a
11426 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11427 be TRUE upon return. By that point, the function-definition will
11428 have been completely parsed.
11430 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11434 cp_parser_init_declarator (cp_parser* parser,
11435 cp_decl_specifier_seq *decl_specifiers,
11436 VEC (deferred_access_check,gc)* checks,
11437 bool function_definition_allowed_p,
11439 int declares_class_or_enum,
11440 bool* function_definition_p)
11443 cp_declarator *declarator;
11444 tree prefix_attributes;
11446 tree asm_specification;
11448 tree decl = NULL_TREE;
11450 bool is_initialized;
11451 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11452 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11454 enum cpp_ttype initialization_kind;
11455 bool is_parenthesized_init = false;
11456 bool is_non_constant_init;
11457 int ctor_dtor_or_conv_p;
11459 tree pushed_scope = NULL;
11461 /* Gather the attributes that were provided with the
11462 decl-specifiers. */
11463 prefix_attributes = decl_specifiers->attributes;
11465 /* Assume that this is not the declarator for a function
11467 if (function_definition_p)
11468 *function_definition_p = false;
11470 /* Defer access checks while parsing the declarator; we cannot know
11471 what names are accessible until we know what is being
11473 resume_deferring_access_checks ();
11475 /* Parse the declarator. */
11477 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11478 &ctor_dtor_or_conv_p,
11479 /*parenthesized_p=*/NULL,
11480 /*member_p=*/false);
11481 /* Gather up the deferred checks. */
11482 stop_deferring_access_checks ();
11484 /* If the DECLARATOR was erroneous, there's no need to go
11486 if (declarator == cp_error_declarator)
11487 return error_mark_node;
11489 /* Check that the number of template-parameter-lists is OK. */
11490 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11491 return error_mark_node;
11493 if (declares_class_or_enum & 2)
11494 cp_parser_check_for_definition_in_return_type (declarator,
11495 decl_specifiers->type);
11497 /* Figure out what scope the entity declared by the DECLARATOR is
11498 located in. `grokdeclarator' sometimes changes the scope, so
11499 we compute it now. */
11500 scope = get_scope_of_declarator (declarator);
11502 /* If we're allowing GNU extensions, look for an asm-specification
11504 if (cp_parser_allow_gnu_extensions_p (parser))
11506 /* Look for an asm-specification. */
11507 asm_specification = cp_parser_asm_specification_opt (parser);
11508 /* And attributes. */
11509 attributes = cp_parser_attributes_opt (parser);
11513 asm_specification = NULL_TREE;
11514 attributes = NULL_TREE;
11517 /* Peek at the next token. */
11518 token = cp_lexer_peek_token (parser->lexer);
11519 /* Check to see if the token indicates the start of a
11520 function-definition. */
11521 if (cp_parser_token_starts_function_definition_p (token))
11523 if (!function_definition_allowed_p)
11525 /* If a function-definition should not appear here, issue an
11527 cp_parser_error (parser,
11528 "a function-definition is not allowed here");
11529 return error_mark_node;
11533 /* Neither attributes nor an asm-specification are allowed
11534 on a function-definition. */
11535 if (asm_specification)
11536 error ("an asm-specification is not allowed on a function-definition");
11538 error ("attributes are not allowed on a function-definition");
11539 /* This is a function-definition. */
11540 *function_definition_p = true;
11542 /* Parse the function definition. */
11544 decl = cp_parser_save_member_function_body (parser,
11547 prefix_attributes);
11550 = (cp_parser_function_definition_from_specifiers_and_declarator
11551 (parser, decl_specifiers, prefix_attributes, declarator));
11559 Only in function declarations for constructors, destructors, and
11560 type conversions can the decl-specifier-seq be omitted.
11562 We explicitly postpone this check past the point where we handle
11563 function-definitions because we tolerate function-definitions
11564 that are missing their return types in some modes. */
11565 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11567 cp_parser_error (parser,
11568 "expected constructor, destructor, or type conversion");
11569 return error_mark_node;
11572 /* An `=' or an `(' indicates an initializer. */
11573 if (token->type == CPP_EQ
11574 || token->type == CPP_OPEN_PAREN)
11576 is_initialized = true;
11577 initialization_kind = token->type;
11581 /* If the init-declarator isn't initialized and isn't followed by a
11582 `,' or `;', it's not a valid init-declarator. */
11583 if (token->type != CPP_COMMA
11584 && token->type != CPP_SEMICOLON)
11586 cp_parser_error (parser, "expected initializer");
11587 return error_mark_node;
11589 is_initialized = false;
11590 initialization_kind = CPP_EOF;
11593 /* Because start_decl has side-effects, we should only call it if we
11594 know we're going ahead. By this point, we know that we cannot
11595 possibly be looking at any other construct. */
11596 cp_parser_commit_to_tentative_parse (parser);
11598 /* If the decl specifiers were bad, issue an error now that we're
11599 sure this was intended to be a declarator. Then continue
11600 declaring the variable(s), as int, to try to cut down on further
11602 if (decl_specifiers->any_specifiers_p
11603 && decl_specifiers->type == error_mark_node)
11605 cp_parser_error (parser, "invalid type in declaration");
11606 decl_specifiers->type = integer_type_node;
11609 /* Check to see whether or not this declaration is a friend. */
11610 friend_p = cp_parser_friend_p (decl_specifiers);
11612 /* Enter the newly declared entry in the symbol table. If we're
11613 processing a declaration in a class-specifier, we wait until
11614 after processing the initializer. */
11617 if (parser->in_unbraced_linkage_specification_p)
11618 decl_specifiers->storage_class = sc_extern;
11619 decl = start_decl (declarator, decl_specifiers,
11620 is_initialized, attributes, prefix_attributes,
11624 /* Enter the SCOPE. That way unqualified names appearing in the
11625 initializer will be looked up in SCOPE. */
11626 pushed_scope = push_scope (scope);
11628 /* Perform deferred access control checks, now that we know in which
11629 SCOPE the declared entity resides. */
11630 if (!member_p && decl)
11632 tree saved_current_function_decl = NULL_TREE;
11634 /* If the entity being declared is a function, pretend that we
11635 are in its scope. If it is a `friend', it may have access to
11636 things that would not otherwise be accessible. */
11637 if (TREE_CODE (decl) == FUNCTION_DECL)
11639 saved_current_function_decl = current_function_decl;
11640 current_function_decl = decl;
11643 /* Perform access checks for template parameters. */
11644 cp_parser_perform_template_parameter_access_checks (checks);
11646 /* Perform the access control checks for the declarator and the
11647 the decl-specifiers. */
11648 perform_deferred_access_checks ();
11650 /* Restore the saved value. */
11651 if (TREE_CODE (decl) == FUNCTION_DECL)
11652 current_function_decl = saved_current_function_decl;
11655 /* Parse the initializer. */
11656 initializer = NULL_TREE;
11657 is_parenthesized_init = false;
11658 is_non_constant_init = true;
11659 if (is_initialized)
11661 if (function_declarator_p (declarator))
11663 if (initialization_kind == CPP_EQ)
11664 initializer = cp_parser_pure_specifier (parser);
11667 /* If the declaration was erroneous, we don't really
11668 know what the user intended, so just silently
11669 consume the initializer. */
11670 if (decl != error_mark_node)
11671 error ("initializer provided for function");
11672 cp_parser_skip_to_closing_parenthesis (parser,
11673 /*recovering=*/true,
11674 /*or_comma=*/false,
11675 /*consume_paren=*/true);
11679 initializer = cp_parser_initializer (parser,
11680 &is_parenthesized_init,
11681 &is_non_constant_init);
11684 /* The old parser allows attributes to appear after a parenthesized
11685 initializer. Mark Mitchell proposed removing this functionality
11686 on the GCC mailing lists on 2002-08-13. This parser accepts the
11687 attributes -- but ignores them. */
11688 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11689 if (cp_parser_attributes_opt (parser))
11690 warning (OPT_Wattributes,
11691 "attributes after parenthesized initializer ignored");
11693 /* For an in-class declaration, use `grokfield' to create the
11699 pop_scope (pushed_scope);
11700 pushed_scope = false;
11702 decl = grokfield (declarator, decl_specifiers,
11703 initializer, !is_non_constant_init,
11704 /*asmspec=*/NULL_TREE,
11705 prefix_attributes);
11706 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11707 cp_parser_save_default_args (parser, decl);
11710 /* Finish processing the declaration. But, skip friend
11712 if (!friend_p && decl && decl != error_mark_node)
11714 cp_finish_decl (decl,
11715 initializer, !is_non_constant_init,
11717 /* If the initializer is in parentheses, then this is
11718 a direct-initialization, which means that an
11719 `explicit' constructor is OK. Otherwise, an
11720 `explicit' constructor cannot be used. */
11721 ((is_parenthesized_init || !is_initialized)
11722 ? 0 : LOOKUP_ONLYCONVERTING));
11724 else if (flag_cpp0x && friend_p && decl && TREE_CODE (decl) == FUNCTION_DECL)
11725 /* Core issue #226 (C++0x only): A default template-argument
11726 shall not be specified in a friend class template
11728 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
11729 /*is_partial=*/0, /*is_friend_decl=*/1);
11731 if (!friend_p && pushed_scope)
11732 pop_scope (pushed_scope);
11737 /* Parse a declarator.
11741 ptr-operator declarator
11743 abstract-declarator:
11744 ptr-operator abstract-declarator [opt]
11745 direct-abstract-declarator
11750 attributes [opt] direct-declarator
11751 attributes [opt] ptr-operator declarator
11753 abstract-declarator:
11754 attributes [opt] ptr-operator abstract-declarator [opt]
11755 attributes [opt] direct-abstract-declarator
11757 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11758 detect constructor, destructor or conversion operators. It is set
11759 to -1 if the declarator is a name, and +1 if it is a
11760 function. Otherwise it is set to zero. Usually you just want to
11761 test for >0, but internally the negative value is used.
11763 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11764 a decl-specifier-seq unless it declares a constructor, destructor,
11765 or conversion. It might seem that we could check this condition in
11766 semantic analysis, rather than parsing, but that makes it difficult
11767 to handle something like `f()'. We want to notice that there are
11768 no decl-specifiers, and therefore realize that this is an
11769 expression, not a declaration.)
11771 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11772 the declarator is a direct-declarator of the form "(...)".
11774 MEMBER_P is true iff this declarator is a member-declarator. */
11776 static cp_declarator *
11777 cp_parser_declarator (cp_parser* parser,
11778 cp_parser_declarator_kind dcl_kind,
11779 int* ctor_dtor_or_conv_p,
11780 bool* parenthesized_p,
11784 cp_declarator *declarator;
11785 enum tree_code code;
11786 cp_cv_quals cv_quals;
11788 tree attributes = NULL_TREE;
11790 /* Assume this is not a constructor, destructor, or type-conversion
11792 if (ctor_dtor_or_conv_p)
11793 *ctor_dtor_or_conv_p = 0;
11795 if (cp_parser_allow_gnu_extensions_p (parser))
11796 attributes = cp_parser_attributes_opt (parser);
11798 /* Peek at the next token. */
11799 token = cp_lexer_peek_token (parser->lexer);
11801 /* Check for the ptr-operator production. */
11802 cp_parser_parse_tentatively (parser);
11803 /* Parse the ptr-operator. */
11804 code = cp_parser_ptr_operator (parser,
11807 /* If that worked, then we have a ptr-operator. */
11808 if (cp_parser_parse_definitely (parser))
11810 /* If a ptr-operator was found, then this declarator was not
11812 if (parenthesized_p)
11813 *parenthesized_p = true;
11814 /* The dependent declarator is optional if we are parsing an
11815 abstract-declarator. */
11816 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11817 cp_parser_parse_tentatively (parser);
11819 /* Parse the dependent declarator. */
11820 declarator = cp_parser_declarator (parser, dcl_kind,
11821 /*ctor_dtor_or_conv_p=*/NULL,
11822 /*parenthesized_p=*/NULL,
11823 /*member_p=*/false);
11825 /* If we are parsing an abstract-declarator, we must handle the
11826 case where the dependent declarator is absent. */
11827 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11828 && !cp_parser_parse_definitely (parser))
11831 /* Build the representation of the ptr-operator. */
11833 declarator = make_ptrmem_declarator (cv_quals,
11836 else if (code == INDIRECT_REF)
11837 declarator = make_pointer_declarator (cv_quals, declarator);
11839 declarator = make_reference_declarator (cv_quals, declarator);
11841 /* Everything else is a direct-declarator. */
11844 if (parenthesized_p)
11845 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11847 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11848 ctor_dtor_or_conv_p,
11852 if (attributes && declarator && declarator != cp_error_declarator)
11853 declarator->attributes = attributes;
11858 /* Parse a direct-declarator or direct-abstract-declarator.
11862 direct-declarator ( parameter-declaration-clause )
11863 cv-qualifier-seq [opt]
11864 exception-specification [opt]
11865 direct-declarator [ constant-expression [opt] ]
11868 direct-abstract-declarator:
11869 direct-abstract-declarator [opt]
11870 ( parameter-declaration-clause )
11871 cv-qualifier-seq [opt]
11872 exception-specification [opt]
11873 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11874 ( abstract-declarator )
11876 Returns a representation of the declarator. DCL_KIND is
11877 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11878 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11879 we are parsing a direct-declarator. It is
11880 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11881 of ambiguity we prefer an abstract declarator, as per
11882 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11883 cp_parser_declarator. */
11885 static cp_declarator *
11886 cp_parser_direct_declarator (cp_parser* parser,
11887 cp_parser_declarator_kind dcl_kind,
11888 int* ctor_dtor_or_conv_p,
11892 cp_declarator *declarator = NULL;
11893 tree scope = NULL_TREE;
11894 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11895 bool saved_in_declarator_p = parser->in_declarator_p;
11897 tree pushed_scope = NULL_TREE;
11901 /* Peek at the next token. */
11902 token = cp_lexer_peek_token (parser->lexer);
11903 if (token->type == CPP_OPEN_PAREN)
11905 /* This is either a parameter-declaration-clause, or a
11906 parenthesized declarator. When we know we are parsing a
11907 named declarator, it must be a parenthesized declarator
11908 if FIRST is true. For instance, `(int)' is a
11909 parameter-declaration-clause, with an omitted
11910 direct-abstract-declarator. But `((*))', is a
11911 parenthesized abstract declarator. Finally, when T is a
11912 template parameter `(T)' is a
11913 parameter-declaration-clause, and not a parenthesized
11916 We first try and parse a parameter-declaration-clause,
11917 and then try a nested declarator (if FIRST is true).
11919 It is not an error for it not to be a
11920 parameter-declaration-clause, even when FIRST is
11926 The first is the declaration of a function while the
11927 second is a the definition of a variable, including its
11930 Having seen only the parenthesis, we cannot know which of
11931 these two alternatives should be selected. Even more
11932 complex are examples like:
11937 The former is a function-declaration; the latter is a
11938 variable initialization.
11940 Thus again, we try a parameter-declaration-clause, and if
11941 that fails, we back out and return. */
11943 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11945 cp_parameter_declarator *params;
11946 unsigned saved_num_template_parameter_lists;
11948 /* In a member-declarator, the only valid interpretation
11949 of a parenthesis is the start of a
11950 parameter-declaration-clause. (It is invalid to
11951 initialize a static data member with a parenthesized
11952 initializer; only the "=" form of initialization is
11955 cp_parser_parse_tentatively (parser);
11957 /* Consume the `('. */
11958 cp_lexer_consume_token (parser->lexer);
11961 /* If this is going to be an abstract declarator, we're
11962 in a declarator and we can't have default args. */
11963 parser->default_arg_ok_p = false;
11964 parser->in_declarator_p = true;
11967 /* Inside the function parameter list, surrounding
11968 template-parameter-lists do not apply. */
11969 saved_num_template_parameter_lists
11970 = parser->num_template_parameter_lists;
11971 parser->num_template_parameter_lists = 0;
11973 /* Parse the parameter-declaration-clause. */
11974 params = cp_parser_parameter_declaration_clause (parser);
11976 parser->num_template_parameter_lists
11977 = saved_num_template_parameter_lists;
11979 /* If all went well, parse the cv-qualifier-seq and the
11980 exception-specification. */
11981 if (member_p || cp_parser_parse_definitely (parser))
11983 cp_cv_quals cv_quals;
11984 tree exception_specification;
11986 if (ctor_dtor_or_conv_p)
11987 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11989 /* Consume the `)'. */
11990 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11992 /* Parse the cv-qualifier-seq. */
11993 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11994 /* And the exception-specification. */
11995 exception_specification
11996 = cp_parser_exception_specification_opt (parser);
11998 /* Create the function-declarator. */
11999 declarator = make_call_declarator (declarator,
12002 exception_specification);
12003 /* Any subsequent parameter lists are to do with
12004 return type, so are not those of the declared
12006 parser->default_arg_ok_p = false;
12008 /* Repeat the main loop. */
12013 /* If this is the first, we can try a parenthesized
12017 bool saved_in_type_id_in_expr_p;
12019 parser->default_arg_ok_p = saved_default_arg_ok_p;
12020 parser->in_declarator_p = saved_in_declarator_p;
12022 /* Consume the `('. */
12023 cp_lexer_consume_token (parser->lexer);
12024 /* Parse the nested declarator. */
12025 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12026 parser->in_type_id_in_expr_p = true;
12028 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12029 /*parenthesized_p=*/NULL,
12031 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12033 /* Expect a `)'. */
12034 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12035 declarator = cp_error_declarator;
12036 if (declarator == cp_error_declarator)
12039 goto handle_declarator;
12041 /* Otherwise, we must be done. */
12045 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12046 && token->type == CPP_OPEN_SQUARE)
12048 /* Parse an array-declarator. */
12051 if (ctor_dtor_or_conv_p)
12052 *ctor_dtor_or_conv_p = 0;
12055 parser->default_arg_ok_p = false;
12056 parser->in_declarator_p = true;
12057 /* Consume the `['. */
12058 cp_lexer_consume_token (parser->lexer);
12059 /* Peek at the next token. */
12060 token = cp_lexer_peek_token (parser->lexer);
12061 /* If the next token is `]', then there is no
12062 constant-expression. */
12063 if (token->type != CPP_CLOSE_SQUARE)
12065 bool non_constant_p;
12068 = cp_parser_constant_expression (parser,
12069 /*allow_non_constant=*/true,
12071 if (!non_constant_p)
12072 bounds = fold_non_dependent_expr (bounds);
12073 /* Normally, the array bound must be an integral constant
12074 expression. However, as an extension, we allow VLAs
12075 in function scopes. */
12076 else if (!parser->in_function_body)
12078 error ("array bound is not an integer constant");
12079 bounds = error_mark_node;
12083 bounds = NULL_TREE;
12084 /* Look for the closing `]'. */
12085 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12087 declarator = cp_error_declarator;
12091 declarator = make_array_declarator (declarator, bounds);
12093 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12095 tree qualifying_scope;
12096 tree unqualified_name;
12097 special_function_kind sfk;
12099 bool pack_expansion_p = false;
12101 /* Parse a declarator-id */
12102 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12105 cp_parser_parse_tentatively (parser);
12107 /* If we see an ellipsis, we should be looking at a
12109 if (token->type == CPP_ELLIPSIS)
12111 /* Consume the `...' */
12112 cp_lexer_consume_token (parser->lexer);
12114 pack_expansion_p = true;
12119 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12120 qualifying_scope = parser->scope;
12125 if (!unqualified_name && pack_expansion_p)
12127 /* Check whether an error occurred. */
12128 okay = !cp_parser_error_occurred (parser);
12130 /* We already consumed the ellipsis to mark a
12131 parameter pack, but we have no way to report it,
12132 so abort the tentative parse. We will be exiting
12133 immediately anyway. */
12134 cp_parser_abort_tentative_parse (parser);
12137 okay = cp_parser_parse_definitely (parser);
12140 unqualified_name = error_mark_node;
12141 else if (unqualified_name
12142 && (qualifying_scope
12143 || (TREE_CODE (unqualified_name)
12144 != IDENTIFIER_NODE)))
12146 cp_parser_error (parser, "expected unqualified-id");
12147 unqualified_name = error_mark_node;
12151 if (!unqualified_name)
12153 if (unqualified_name == error_mark_node)
12155 declarator = cp_error_declarator;
12156 pack_expansion_p = false;
12157 declarator->parameter_pack_p = false;
12161 if (qualifying_scope && at_namespace_scope_p ()
12162 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12164 /* In the declaration of a member of a template class
12165 outside of the class itself, the SCOPE will sometimes
12166 be a TYPENAME_TYPE. For example, given:
12168 template <typename T>
12169 int S<T>::R::i = 3;
12171 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12172 this context, we must resolve S<T>::R to an ordinary
12173 type, rather than a typename type.
12175 The reason we normally avoid resolving TYPENAME_TYPEs
12176 is that a specialization of `S' might render
12177 `S<T>::R' not a type. However, if `S' is
12178 specialized, then this `i' will not be used, so there
12179 is no harm in resolving the types here. */
12182 /* Resolve the TYPENAME_TYPE. */
12183 type = resolve_typename_type (qualifying_scope,
12184 /*only_current_p=*/false);
12185 /* If that failed, the declarator is invalid. */
12186 if (type == error_mark_node)
12187 error ("%<%T::%E%> is not a type",
12188 TYPE_CONTEXT (qualifying_scope),
12189 TYPE_IDENTIFIER (qualifying_scope));
12190 qualifying_scope = type;
12195 if (unqualified_name)
12199 if (qualifying_scope
12200 && CLASS_TYPE_P (qualifying_scope))
12201 class_type = qualifying_scope;
12203 class_type = current_class_type;
12205 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12207 tree name_type = TREE_TYPE (unqualified_name);
12208 if (class_type && same_type_p (name_type, class_type))
12210 if (qualifying_scope
12211 && CLASSTYPE_USE_TEMPLATE (name_type))
12213 error ("invalid use of constructor as a template");
12214 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12215 "name the constructor in a qualified name",
12217 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12218 class_type, name_type);
12219 declarator = cp_error_declarator;
12223 unqualified_name = constructor_name (class_type);
12227 /* We do not attempt to print the declarator
12228 here because we do not have enough
12229 information about its original syntactic
12231 cp_parser_error (parser, "invalid declarator");
12232 declarator = cp_error_declarator;
12239 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12240 sfk = sfk_destructor;
12241 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12242 sfk = sfk_conversion;
12243 else if (/* There's no way to declare a constructor
12244 for an anonymous type, even if the type
12245 got a name for linkage purposes. */
12246 !TYPE_WAS_ANONYMOUS (class_type)
12247 && constructor_name_p (unqualified_name,
12250 unqualified_name = constructor_name (class_type);
12251 sfk = sfk_constructor;
12254 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12255 *ctor_dtor_or_conv_p = -1;
12258 declarator = make_id_declarator (qualifying_scope,
12261 declarator->id_loc = token->location;
12262 declarator->parameter_pack_p = pack_expansion_p;
12264 if (pack_expansion_p)
12265 maybe_warn_variadic_templates ();
12267 handle_declarator:;
12268 scope = get_scope_of_declarator (declarator);
12270 /* Any names that appear after the declarator-id for a
12271 member are looked up in the containing scope. */
12272 pushed_scope = push_scope (scope);
12273 parser->in_declarator_p = true;
12274 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12275 || (declarator && declarator->kind == cdk_id))
12276 /* Default args are only allowed on function
12278 parser->default_arg_ok_p = saved_default_arg_ok_p;
12280 parser->default_arg_ok_p = false;
12289 /* For an abstract declarator, we might wind up with nothing at this
12290 point. That's an error; the declarator is not optional. */
12292 cp_parser_error (parser, "expected declarator");
12294 /* If we entered a scope, we must exit it now. */
12296 pop_scope (pushed_scope);
12298 parser->default_arg_ok_p = saved_default_arg_ok_p;
12299 parser->in_declarator_p = saved_in_declarator_p;
12304 /* Parse a ptr-operator.
12307 * cv-qualifier-seq [opt]
12309 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12314 & cv-qualifier-seq [opt]
12316 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12317 Returns ADDR_EXPR if a reference was used. In the case of a
12318 pointer-to-member, *TYPE is filled in with the TYPE containing the
12319 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12320 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12321 ERROR_MARK if an error occurred. */
12323 static enum tree_code
12324 cp_parser_ptr_operator (cp_parser* parser,
12326 cp_cv_quals *cv_quals)
12328 enum tree_code code = ERROR_MARK;
12331 /* Assume that it's not a pointer-to-member. */
12333 /* And that there are no cv-qualifiers. */
12334 *cv_quals = TYPE_UNQUALIFIED;
12336 /* Peek at the next token. */
12337 token = cp_lexer_peek_token (parser->lexer);
12338 /* If it's a `*' or `&' we have a pointer or reference. */
12339 if (token->type == CPP_MULT || token->type == CPP_AND)
12341 /* Remember which ptr-operator we were processing. */
12342 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12344 /* Consume the `*' or `&'. */
12345 cp_lexer_consume_token (parser->lexer);
12347 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12348 `&', if we are allowing GNU extensions. (The only qualifier
12349 that can legally appear after `&' is `restrict', but that is
12350 enforced during semantic analysis. */
12351 if (code == INDIRECT_REF
12352 || cp_parser_allow_gnu_extensions_p (parser))
12353 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12357 /* Try the pointer-to-member case. */
12358 cp_parser_parse_tentatively (parser);
12359 /* Look for the optional `::' operator. */
12360 cp_parser_global_scope_opt (parser,
12361 /*current_scope_valid_p=*/false);
12362 /* Look for the nested-name specifier. */
12363 cp_parser_nested_name_specifier (parser,
12364 /*typename_keyword_p=*/false,
12365 /*check_dependency_p=*/true,
12367 /*is_declaration=*/false);
12368 /* If we found it, and the next token is a `*', then we are
12369 indeed looking at a pointer-to-member operator. */
12370 if (!cp_parser_error_occurred (parser)
12371 && cp_parser_require (parser, CPP_MULT, "`*'"))
12373 /* Indicate that the `*' operator was used. */
12374 code = INDIRECT_REF;
12376 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12377 error ("%qD is a namespace", parser->scope);
12380 /* The type of which the member is a member is given by the
12382 *type = parser->scope;
12383 /* The next name will not be qualified. */
12384 parser->scope = NULL_TREE;
12385 parser->qualifying_scope = NULL_TREE;
12386 parser->object_scope = NULL_TREE;
12387 /* Look for the optional cv-qualifier-seq. */
12388 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12391 /* If that didn't work we don't have a ptr-operator. */
12392 if (!cp_parser_parse_definitely (parser))
12393 cp_parser_error (parser, "expected ptr-operator");
12399 /* Parse an (optional) cv-qualifier-seq.
12402 cv-qualifier cv-qualifier-seq [opt]
12413 Returns a bitmask representing the cv-qualifiers. */
12416 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12418 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12423 cp_cv_quals cv_qualifier;
12425 /* Peek at the next token. */
12426 token = cp_lexer_peek_token (parser->lexer);
12427 /* See if it's a cv-qualifier. */
12428 switch (token->keyword)
12431 cv_qualifier = TYPE_QUAL_CONST;
12435 cv_qualifier = TYPE_QUAL_VOLATILE;
12439 cv_qualifier = TYPE_QUAL_RESTRICT;
12443 cv_qualifier = TYPE_UNQUALIFIED;
12450 if (cv_quals & cv_qualifier)
12452 error ("duplicate cv-qualifier");
12453 cp_lexer_purge_token (parser->lexer);
12457 cp_lexer_consume_token (parser->lexer);
12458 cv_quals |= cv_qualifier;
12465 /* Parse a declarator-id.
12469 :: [opt] nested-name-specifier [opt] type-name
12471 In the `id-expression' case, the value returned is as for
12472 cp_parser_id_expression if the id-expression was an unqualified-id.
12473 If the id-expression was a qualified-id, then a SCOPE_REF is
12474 returned. The first operand is the scope (either a NAMESPACE_DECL
12475 or TREE_TYPE), but the second is still just a representation of an
12479 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12482 /* The expression must be an id-expression. Assume that qualified
12483 names are the names of types so that:
12486 int S<T>::R::i = 3;
12488 will work; we must treat `S<T>::R' as the name of a type.
12489 Similarly, assume that qualified names are templates, where
12493 int S<T>::R<T>::i = 3;
12496 id = cp_parser_id_expression (parser,
12497 /*template_keyword_p=*/false,
12498 /*check_dependency_p=*/false,
12499 /*template_p=*/NULL,
12500 /*declarator_p=*/true,
12502 if (id && BASELINK_P (id))
12503 id = BASELINK_FUNCTIONS (id);
12507 /* Parse a type-id.
12510 type-specifier-seq abstract-declarator [opt]
12512 Returns the TYPE specified. */
12515 cp_parser_type_id (cp_parser* parser)
12517 cp_decl_specifier_seq type_specifier_seq;
12518 cp_declarator *abstract_declarator;
12520 /* Parse the type-specifier-seq. */
12521 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12522 &type_specifier_seq);
12523 if (type_specifier_seq.type == error_mark_node)
12524 return error_mark_node;
12526 /* There might or might not be an abstract declarator. */
12527 cp_parser_parse_tentatively (parser);
12528 /* Look for the declarator. */
12529 abstract_declarator
12530 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12531 /*parenthesized_p=*/NULL,
12532 /*member_p=*/false);
12533 /* Check to see if there really was a declarator. */
12534 if (!cp_parser_parse_definitely (parser))
12535 abstract_declarator = NULL;
12537 return groktypename (&type_specifier_seq, abstract_declarator);
12540 /* Parse a type-specifier-seq.
12542 type-specifier-seq:
12543 type-specifier type-specifier-seq [opt]
12547 type-specifier-seq:
12548 attributes type-specifier-seq [opt]
12550 If IS_CONDITION is true, we are at the start of a "condition",
12551 e.g., we've just seen "if (".
12553 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12556 cp_parser_type_specifier_seq (cp_parser* parser,
12558 cp_decl_specifier_seq *type_specifier_seq)
12560 bool seen_type_specifier = false;
12561 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12563 /* Clear the TYPE_SPECIFIER_SEQ. */
12564 clear_decl_specs (type_specifier_seq);
12566 /* Parse the type-specifiers and attributes. */
12569 tree type_specifier;
12570 bool is_cv_qualifier;
12572 /* Check for attributes first. */
12573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12575 type_specifier_seq->attributes =
12576 chainon (type_specifier_seq->attributes,
12577 cp_parser_attributes_opt (parser));
12581 /* Look for the type-specifier. */
12582 type_specifier = cp_parser_type_specifier (parser,
12584 type_specifier_seq,
12585 /*is_declaration=*/false,
12588 if (!type_specifier)
12590 /* If the first type-specifier could not be found, this is not a
12591 type-specifier-seq at all. */
12592 if (!seen_type_specifier)
12594 cp_parser_error (parser, "expected type-specifier");
12595 type_specifier_seq->type = error_mark_node;
12598 /* If subsequent type-specifiers could not be found, the
12599 type-specifier-seq is complete. */
12603 seen_type_specifier = true;
12604 /* The standard says that a condition can be:
12606 type-specifier-seq declarator = assignment-expression
12613 we should treat the "S" as a declarator, not as a
12614 type-specifier. The standard doesn't say that explicitly for
12615 type-specifier-seq, but it does say that for
12616 decl-specifier-seq in an ordinary declaration. Perhaps it
12617 would be clearer just to allow a decl-specifier-seq here, and
12618 then add a semantic restriction that if any decl-specifiers
12619 that are not type-specifiers appear, the program is invalid. */
12620 if (is_condition && !is_cv_qualifier)
12621 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12624 cp_parser_check_decl_spec (type_specifier_seq);
12627 /* Parse a parameter-declaration-clause.
12629 parameter-declaration-clause:
12630 parameter-declaration-list [opt] ... [opt]
12631 parameter-declaration-list , ...
12633 Returns a representation for the parameter declarations. A return
12634 value of NULL indicates a parameter-declaration-clause consisting
12635 only of an ellipsis. */
12637 static cp_parameter_declarator *
12638 cp_parser_parameter_declaration_clause (cp_parser* parser)
12640 cp_parameter_declarator *parameters;
12645 /* Peek at the next token. */
12646 token = cp_lexer_peek_token (parser->lexer);
12647 /* Check for trivial parameter-declaration-clauses. */
12648 if (token->type == CPP_ELLIPSIS)
12650 /* Consume the `...' token. */
12651 cp_lexer_consume_token (parser->lexer);
12654 else if (token->type == CPP_CLOSE_PAREN)
12655 /* There are no parameters. */
12657 #ifndef NO_IMPLICIT_EXTERN_C
12658 if (in_system_header && current_class_type == NULL
12659 && current_lang_name == lang_name_c)
12663 return no_parameters;
12665 /* Check for `(void)', too, which is a special case. */
12666 else if (token->keyword == RID_VOID
12667 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12668 == CPP_CLOSE_PAREN))
12670 /* Consume the `void' token. */
12671 cp_lexer_consume_token (parser->lexer);
12672 /* There are no parameters. */
12673 return no_parameters;
12676 /* Parse the parameter-declaration-list. */
12677 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12678 /* If a parse error occurred while parsing the
12679 parameter-declaration-list, then the entire
12680 parameter-declaration-clause is erroneous. */
12684 /* Peek at the next token. */
12685 token = cp_lexer_peek_token (parser->lexer);
12686 /* If it's a `,', the clause should terminate with an ellipsis. */
12687 if (token->type == CPP_COMMA)
12689 /* Consume the `,'. */
12690 cp_lexer_consume_token (parser->lexer);
12691 /* Expect an ellipsis. */
12693 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12695 /* It might also be `...' if the optional trailing `,' was
12697 else if (token->type == CPP_ELLIPSIS)
12699 /* Consume the `...' token. */
12700 cp_lexer_consume_token (parser->lexer);
12701 /* And remember that we saw it. */
12705 ellipsis_p = false;
12707 /* Finish the parameter list. */
12708 if (parameters && ellipsis_p)
12709 parameters->ellipsis_p = true;
12714 /* Parse a parameter-declaration-list.
12716 parameter-declaration-list:
12717 parameter-declaration
12718 parameter-declaration-list , parameter-declaration
12720 Returns a representation of the parameter-declaration-list, as for
12721 cp_parser_parameter_declaration_clause. However, the
12722 `void_list_node' is never appended to the list. Upon return,
12723 *IS_ERROR will be true iff an error occurred. */
12725 static cp_parameter_declarator *
12726 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12728 cp_parameter_declarator *parameters = NULL;
12729 cp_parameter_declarator **tail = ¶meters;
12730 bool saved_in_unbraced_linkage_specification_p;
12732 /* Assume all will go well. */
12734 /* The special considerations that apply to a function within an
12735 unbraced linkage specifications do not apply to the parameters
12736 to the function. */
12737 saved_in_unbraced_linkage_specification_p
12738 = parser->in_unbraced_linkage_specification_p;
12739 parser->in_unbraced_linkage_specification_p = false;
12741 /* Look for more parameters. */
12744 cp_parameter_declarator *parameter;
12745 bool parenthesized_p;
12746 /* Parse the parameter. */
12748 = cp_parser_parameter_declaration (parser,
12749 /*template_parm_p=*/false,
12752 /* If a parse error occurred parsing the parameter declaration,
12753 then the entire parameter-declaration-list is erroneous. */
12760 /* Add the new parameter to the list. */
12762 tail = ¶meter->next;
12764 /* Peek at the next token. */
12765 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12766 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12767 /* These are for Objective-C++ */
12768 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12769 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12770 /* The parameter-declaration-list is complete. */
12772 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12776 /* Peek at the next token. */
12777 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12778 /* If it's an ellipsis, then the list is complete. */
12779 if (token->type == CPP_ELLIPSIS)
12781 /* Otherwise, there must be more parameters. Consume the
12783 cp_lexer_consume_token (parser->lexer);
12784 /* When parsing something like:
12786 int i(float f, double d)
12788 we can tell after seeing the declaration for "f" that we
12789 are not looking at an initialization of a variable "i",
12790 but rather at the declaration of a function "i".
12792 Due to the fact that the parsing of template arguments
12793 (as specified to a template-id) requires backtracking we
12794 cannot use this technique when inside a template argument
12796 if (!parser->in_template_argument_list_p
12797 && !parser->in_type_id_in_expr_p
12798 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12799 /* However, a parameter-declaration of the form
12800 "foat(f)" (which is a valid declaration of a
12801 parameter "f") can also be interpreted as an
12802 expression (the conversion of "f" to "float"). */
12803 && !parenthesized_p)
12804 cp_parser_commit_to_tentative_parse (parser);
12808 cp_parser_error (parser, "expected %<,%> or %<...%>");
12809 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12810 cp_parser_skip_to_closing_parenthesis (parser,
12811 /*recovering=*/true,
12812 /*or_comma=*/false,
12813 /*consume_paren=*/false);
12818 parser->in_unbraced_linkage_specification_p
12819 = saved_in_unbraced_linkage_specification_p;
12824 /* Parse a parameter declaration.
12826 parameter-declaration:
12827 decl-specifier-seq ... [opt] declarator
12828 decl-specifier-seq declarator = assignment-expression
12829 decl-specifier-seq ... [opt] abstract-declarator [opt]
12830 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12832 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12833 declares a template parameter. (In that case, a non-nested `>'
12834 token encountered during the parsing of the assignment-expression
12835 is not interpreted as a greater-than operator.)
12837 Returns a representation of the parameter, or NULL if an error
12838 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12839 true iff the declarator is of the form "(p)". */
12841 static cp_parameter_declarator *
12842 cp_parser_parameter_declaration (cp_parser *parser,
12843 bool template_parm_p,
12844 bool *parenthesized_p)
12846 int declares_class_or_enum;
12847 bool greater_than_is_operator_p;
12848 cp_decl_specifier_seq decl_specifiers;
12849 cp_declarator *declarator;
12850 tree default_argument;
12852 const char *saved_message;
12854 /* In a template parameter, `>' is not an operator.
12858 When parsing a default template-argument for a non-type
12859 template-parameter, the first non-nested `>' is taken as the end
12860 of the template parameter-list rather than a greater-than
12862 greater_than_is_operator_p = !template_parm_p;
12864 /* Type definitions may not appear in parameter types. */
12865 saved_message = parser->type_definition_forbidden_message;
12866 parser->type_definition_forbidden_message
12867 = "types may not be defined in parameter types";
12869 /* Parse the declaration-specifiers. */
12870 cp_parser_decl_specifier_seq (parser,
12871 CP_PARSER_FLAGS_NONE,
12873 &declares_class_or_enum);
12874 /* If an error occurred, there's no reason to attempt to parse the
12875 rest of the declaration. */
12876 if (cp_parser_error_occurred (parser))
12878 parser->type_definition_forbidden_message = saved_message;
12882 /* Peek at the next token. */
12883 token = cp_lexer_peek_token (parser->lexer);
12885 /* If the next token is a `)', `,', `=', `>', or `...', then there
12886 is no declarator. However, when variadic templates are enabled,
12887 there may be a declarator following `...'. */
12888 if (token->type == CPP_CLOSE_PAREN
12889 || token->type == CPP_COMMA
12890 || token->type == CPP_EQ
12891 || token->type == CPP_GREATER)
12894 if (parenthesized_p)
12895 *parenthesized_p = false;
12897 /* Otherwise, there should be a declarator. */
12900 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12901 parser->default_arg_ok_p = false;
12903 /* After seeing a decl-specifier-seq, if the next token is not a
12904 "(", there is no possibility that the code is a valid
12905 expression. Therefore, if parsing tentatively, we commit at
12907 if (!parser->in_template_argument_list_p
12908 /* In an expression context, having seen:
12912 we cannot be sure whether we are looking at a
12913 function-type (taking a "char" as a parameter) or a cast
12914 of some object of type "char" to "int". */
12915 && !parser->in_type_id_in_expr_p
12916 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12917 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12918 cp_parser_commit_to_tentative_parse (parser);
12919 /* Parse the declarator. */
12920 declarator = cp_parser_declarator (parser,
12921 CP_PARSER_DECLARATOR_EITHER,
12922 /*ctor_dtor_or_conv_p=*/NULL,
12924 /*member_p=*/false);
12925 parser->default_arg_ok_p = saved_default_arg_ok_p;
12926 /* After the declarator, allow more attributes. */
12927 decl_specifiers.attributes
12928 = chainon (decl_specifiers.attributes,
12929 cp_parser_attributes_opt (parser));
12932 /* If the next token is an ellipsis, and the type of the declarator
12933 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12934 we actually have a parameter pack expansion expression. Otherwise,
12935 leave the ellipsis for a C-style variadic function. */
12936 token = cp_lexer_peek_token (parser->lexer);
12937 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12939 tree type = decl_specifiers.type;
12942 type = TREE_TYPE (type);
12944 if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12945 && (!declarator || !declarator->parameter_pack_p)
12946 && uses_parameter_packs (type))
12948 /* Consume the `...'. */
12949 cp_lexer_consume_token (parser->lexer);
12950 maybe_warn_variadic_templates ();
12952 /* Build a pack expansion type */
12954 declarator->parameter_pack_p = true;
12956 decl_specifiers.type = make_pack_expansion (type);
12960 /* The restriction on defining new types applies only to the type
12961 of the parameter, not to the default argument. */
12962 parser->type_definition_forbidden_message = saved_message;
12964 /* If the next token is `=', then process a default argument. */
12965 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12967 bool saved_greater_than_is_operator_p;
12968 /* Consume the `='. */
12969 cp_lexer_consume_token (parser->lexer);
12971 /* If we are defining a class, then the tokens that make up the
12972 default argument must be saved and processed later. */
12973 if (!template_parm_p && at_class_scope_p ()
12974 && TYPE_BEING_DEFINED (current_class_type))
12976 unsigned depth = 0;
12977 cp_token *first_token;
12980 /* Add tokens until we have processed the entire default
12981 argument. We add the range [first_token, token). */
12982 first_token = cp_lexer_peek_token (parser->lexer);
12987 /* Peek at the next token. */
12988 token = cp_lexer_peek_token (parser->lexer);
12989 /* What we do depends on what token we have. */
12990 switch (token->type)
12992 /* In valid code, a default argument must be
12993 immediately followed by a `,' `)', or `...'. */
12995 case CPP_CLOSE_PAREN:
12997 /* If we run into a non-nested `;', `}', or `]',
12998 then the code is invalid -- but the default
12999 argument is certainly over. */
13000 case CPP_SEMICOLON:
13001 case CPP_CLOSE_BRACE:
13002 case CPP_CLOSE_SQUARE:
13005 /* Update DEPTH, if necessary. */
13006 else if (token->type == CPP_CLOSE_PAREN
13007 || token->type == CPP_CLOSE_BRACE
13008 || token->type == CPP_CLOSE_SQUARE)
13012 case CPP_OPEN_PAREN:
13013 case CPP_OPEN_SQUARE:
13014 case CPP_OPEN_BRACE:
13019 /* If we see a non-nested `>', and `>' is not an
13020 operator, then it marks the end of the default
13022 if (!depth && !greater_than_is_operator_p)
13026 /* If we run out of tokens, issue an error message. */
13028 case CPP_PRAGMA_EOL:
13029 error ("file ends in default argument");
13035 /* In these cases, we should look for template-ids.
13036 For example, if the default argument is
13037 `X<int, double>()', we need to do name lookup to
13038 figure out whether or not `X' is a template; if
13039 so, the `,' does not end the default argument.
13041 That is not yet done. */
13048 /* If we've reached the end, stop. */
13052 /* Add the token to the token block. */
13053 token = cp_lexer_consume_token (parser->lexer);
13056 /* Create a DEFAULT_ARG to represented the unparsed default
13058 default_argument = make_node (DEFAULT_ARG);
13059 DEFARG_TOKENS (default_argument)
13060 = cp_token_cache_new (first_token, token);
13061 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13063 /* Outside of a class definition, we can just parse the
13064 assignment-expression. */
13067 bool saved_local_variables_forbidden_p;
13069 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13071 saved_greater_than_is_operator_p
13072 = parser->greater_than_is_operator_p;
13073 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13074 /* Local variable names (and the `this' keyword) may not
13075 appear in a default argument. */
13076 saved_local_variables_forbidden_p
13077 = parser->local_variables_forbidden_p;
13078 parser->local_variables_forbidden_p = true;
13079 /* The default argument expression may cause implicitly
13080 defined member functions to be synthesized, which will
13081 result in garbage collection. We must treat this
13082 situation as if we were within the body of function so as
13083 to avoid collecting live data on the stack. */
13085 /* Parse the assignment-expression. */
13086 if (template_parm_p)
13087 push_deferring_access_checks (dk_no_deferred);
13089 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13090 if (template_parm_p)
13091 pop_deferring_access_checks ();
13092 /* Restore saved state. */
13094 parser->greater_than_is_operator_p
13095 = saved_greater_than_is_operator_p;
13096 parser->local_variables_forbidden_p
13097 = saved_local_variables_forbidden_p;
13099 if (!parser->default_arg_ok_p)
13101 if (!flag_pedantic_errors)
13102 warning (0, "deprecated use of default argument for parameter of non-function");
13105 error ("default arguments are only permitted for function parameters");
13106 default_argument = NULL_TREE;
13111 default_argument = NULL_TREE;
13113 return make_parameter_declarator (&decl_specifiers,
13118 /* Parse a function-body.
13121 compound_statement */
13124 cp_parser_function_body (cp_parser *parser)
13126 cp_parser_compound_statement (parser, NULL, false);
13129 /* Parse a ctor-initializer-opt followed by a function-body. Return
13130 true if a ctor-initializer was present. */
13133 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13136 bool ctor_initializer_p;
13138 /* Begin the function body. */
13139 body = begin_function_body ();
13140 /* Parse the optional ctor-initializer. */
13141 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13142 /* Parse the function-body. */
13143 cp_parser_function_body (parser);
13144 /* Finish the function body. */
13145 finish_function_body (body);
13147 return ctor_initializer_p;
13150 /* Parse an initializer.
13153 = initializer-clause
13154 ( expression-list )
13156 Returns an expression representing the initializer. If no
13157 initializer is present, NULL_TREE is returned.
13159 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13160 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13161 set to FALSE if there is no initializer present. If there is an
13162 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13163 is set to true; otherwise it is set to false. */
13166 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13167 bool* non_constant_p)
13172 /* Peek at the next token. */
13173 token = cp_lexer_peek_token (parser->lexer);
13175 /* Let our caller know whether or not this initializer was
13177 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13178 /* Assume that the initializer is constant. */
13179 *non_constant_p = false;
13181 if (token->type == CPP_EQ)
13183 /* Consume the `='. */
13184 cp_lexer_consume_token (parser->lexer);
13185 /* Parse the initializer-clause. */
13186 init = cp_parser_initializer_clause (parser, non_constant_p);
13188 else if (token->type == CPP_OPEN_PAREN)
13189 init = cp_parser_parenthesized_expression_list (parser, false,
13191 /*allow_expansion_p=*/true,
13195 /* Anything else is an error. */
13196 cp_parser_error (parser, "expected initializer");
13197 init = error_mark_node;
13203 /* Parse an initializer-clause.
13205 initializer-clause:
13206 assignment-expression
13207 { initializer-list , [opt] }
13210 Returns an expression representing the initializer.
13212 If the `assignment-expression' production is used the value
13213 returned is simply a representation for the expression.
13215 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13216 the elements of the initializer-list (or NULL, if the last
13217 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13218 NULL_TREE. There is no way to detect whether or not the optional
13219 trailing `,' was provided. NON_CONSTANT_P is as for
13220 cp_parser_initializer. */
13223 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13227 /* Assume the expression is constant. */
13228 *non_constant_p = false;
13230 /* If it is not a `{', then we are looking at an
13231 assignment-expression. */
13232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13235 = cp_parser_constant_expression (parser,
13236 /*allow_non_constant_p=*/true,
13238 if (!*non_constant_p)
13239 initializer = fold_non_dependent_expr (initializer);
13243 /* Consume the `{' token. */
13244 cp_lexer_consume_token (parser->lexer);
13245 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13246 initializer = make_node (CONSTRUCTOR);
13247 /* If it's not a `}', then there is a non-trivial initializer. */
13248 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13250 /* Parse the initializer list. */
13251 CONSTRUCTOR_ELTS (initializer)
13252 = cp_parser_initializer_list (parser, non_constant_p);
13253 /* A trailing `,' token is allowed. */
13254 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13255 cp_lexer_consume_token (parser->lexer);
13257 /* Now, there should be a trailing `}'. */
13258 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13261 return initializer;
13264 /* Parse an initializer-list.
13267 initializer-clause ... [opt]
13268 initializer-list , initializer-clause ... [opt]
13273 identifier : initializer-clause
13274 initializer-list, identifier : initializer-clause
13276 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13277 for the initializer. If the INDEX of the elt is non-NULL, it is the
13278 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13279 as for cp_parser_initializer. */
13281 static VEC(constructor_elt,gc) *
13282 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13284 VEC(constructor_elt,gc) *v = NULL;
13286 /* Assume all of the expressions are constant. */
13287 *non_constant_p = false;
13289 /* Parse the rest of the list. */
13295 bool clause_non_constant_p;
13297 /* If the next token is an identifier and the following one is a
13298 colon, we are looking at the GNU designated-initializer
13300 if (cp_parser_allow_gnu_extensions_p (parser)
13301 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13302 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13304 /* Warn the user that they are using an extension. */
13306 pedwarn ("ISO C++ does not allow designated initializers");
13307 /* Consume the identifier. */
13308 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13309 /* Consume the `:'. */
13310 cp_lexer_consume_token (parser->lexer);
13313 identifier = NULL_TREE;
13315 /* Parse the initializer. */
13316 initializer = cp_parser_initializer_clause (parser,
13317 &clause_non_constant_p);
13318 /* If any clause is non-constant, so is the entire initializer. */
13319 if (clause_non_constant_p)
13320 *non_constant_p = true;
13322 /* If we have an ellipsis, this is an initializer pack
13324 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13326 /* Consume the `...'. */
13327 cp_lexer_consume_token (parser->lexer);
13329 /* Turn the initializer into an initializer expansion. */
13330 initializer = make_pack_expansion (initializer);
13333 /* Add it to the vector. */
13334 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13336 /* If the next token is not a comma, we have reached the end of
13338 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13341 /* Peek at the next token. */
13342 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13343 /* If the next token is a `}', then we're still done. An
13344 initializer-clause can have a trailing `,' after the
13345 initializer-list and before the closing `}'. */
13346 if (token->type == CPP_CLOSE_BRACE)
13349 /* Consume the `,' token. */
13350 cp_lexer_consume_token (parser->lexer);
13356 /* Classes [gram.class] */
13358 /* Parse a class-name.
13364 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13365 to indicate that names looked up in dependent types should be
13366 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13367 keyword has been used to indicate that the name that appears next
13368 is a template. TAG_TYPE indicates the explicit tag given before
13369 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13370 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13371 is the class being defined in a class-head.
13373 Returns the TYPE_DECL representing the class. */
13376 cp_parser_class_name (cp_parser *parser,
13377 bool typename_keyword_p,
13378 bool template_keyword_p,
13379 enum tag_types tag_type,
13380 bool check_dependency_p,
13382 bool is_declaration)
13389 /* All class-names start with an identifier. */
13390 token = cp_lexer_peek_token (parser->lexer);
13391 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13393 cp_parser_error (parser, "expected class-name");
13394 return error_mark_node;
13397 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13398 to a template-id, so we save it here. */
13399 scope = parser->scope;
13400 if (scope == error_mark_node)
13401 return error_mark_node;
13403 /* Any name names a type if we're following the `typename' keyword
13404 in a qualified name where the enclosing scope is type-dependent. */
13405 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13406 && dependent_type_p (scope));
13407 /* Handle the common case (an identifier, but not a template-id)
13409 if (token->type == CPP_NAME
13410 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13412 cp_token *identifier_token;
13416 /* Look for the identifier. */
13417 identifier_token = cp_lexer_peek_token (parser->lexer);
13418 ambiguous_p = identifier_token->ambiguous_p;
13419 identifier = cp_parser_identifier (parser);
13420 /* If the next token isn't an identifier, we are certainly not
13421 looking at a class-name. */
13422 if (identifier == error_mark_node)
13423 decl = error_mark_node;
13424 /* If we know this is a type-name, there's no need to look it
13426 else if (typename_p)
13430 tree ambiguous_decls;
13431 /* If we already know that this lookup is ambiguous, then
13432 we've already issued an error message; there's no reason
13436 cp_parser_simulate_error (parser);
13437 return error_mark_node;
13439 /* If the next token is a `::', then the name must be a type
13442 [basic.lookup.qual]
13444 During the lookup for a name preceding the :: scope
13445 resolution operator, object, function, and enumerator
13446 names are ignored. */
13447 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13448 tag_type = typename_type;
13449 /* Look up the name. */
13450 decl = cp_parser_lookup_name (parser, identifier,
13452 /*is_template=*/false,
13453 /*is_namespace=*/false,
13454 check_dependency_p,
13456 if (ambiguous_decls)
13458 error ("reference to %qD is ambiguous", identifier);
13459 print_candidates (ambiguous_decls);
13460 if (cp_parser_parsing_tentatively (parser))
13462 identifier_token->ambiguous_p = true;
13463 cp_parser_simulate_error (parser);
13465 return error_mark_node;
13471 /* Try a template-id. */
13472 decl = cp_parser_template_id (parser, template_keyword_p,
13473 check_dependency_p,
13475 if (decl == error_mark_node)
13476 return error_mark_node;
13479 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13481 /* If this is a typename, create a TYPENAME_TYPE. */
13482 if (typename_p && decl != error_mark_node)
13484 decl = make_typename_type (scope, decl, typename_type,
13485 /*complain=*/tf_error);
13486 if (decl != error_mark_node)
13487 decl = TYPE_NAME (decl);
13490 /* Check to see that it is really the name of a class. */
13491 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13492 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13493 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13494 /* Situations like this:
13496 template <typename T> struct A {
13497 typename T::template X<int>::I i;
13500 are problematic. Is `T::template X<int>' a class-name? The
13501 standard does not seem to be definitive, but there is no other
13502 valid interpretation of the following `::'. Therefore, those
13503 names are considered class-names. */
13505 decl = make_typename_type (scope, decl, tag_type, tf_error);
13506 if (decl != error_mark_node)
13507 decl = TYPE_NAME (decl);
13509 else if (TREE_CODE (decl) != TYPE_DECL
13510 || TREE_TYPE (decl) == error_mark_node
13511 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13512 decl = error_mark_node;
13514 if (decl == error_mark_node)
13515 cp_parser_error (parser, "expected class-name");
13520 /* Parse a class-specifier.
13523 class-head { member-specification [opt] }
13525 Returns the TREE_TYPE representing the class. */
13528 cp_parser_class_specifier (cp_parser* parser)
13532 tree attributes = NULL_TREE;
13533 int has_trailing_semicolon;
13534 bool nested_name_specifier_p;
13535 unsigned saved_num_template_parameter_lists;
13536 bool saved_in_function_body;
13537 tree old_scope = NULL_TREE;
13538 tree scope = NULL_TREE;
13541 push_deferring_access_checks (dk_no_deferred);
13543 /* Parse the class-head. */
13544 type = cp_parser_class_head (parser,
13545 &nested_name_specifier_p,
13548 /* If the class-head was a semantic disaster, skip the entire body
13552 cp_parser_skip_to_end_of_block_or_statement (parser);
13553 pop_deferring_access_checks ();
13554 return error_mark_node;
13557 /* Look for the `{'. */
13558 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13560 pop_deferring_access_checks ();
13561 return error_mark_node;
13564 /* Process the base classes. If they're invalid, skip the
13565 entire class body. */
13566 if (!xref_basetypes (type, bases))
13568 cp_parser_skip_to_closing_brace (parser);
13570 /* Consuming the closing brace yields better error messages
13572 cp_lexer_consume_token (parser->lexer);
13573 pop_deferring_access_checks ();
13574 return error_mark_node;
13577 /* Issue an error message if type-definitions are forbidden here. */
13578 cp_parser_check_type_definition (parser);
13579 /* Remember that we are defining one more class. */
13580 ++parser->num_classes_being_defined;
13581 /* Inside the class, surrounding template-parameter-lists do not
13583 saved_num_template_parameter_lists
13584 = parser->num_template_parameter_lists;
13585 parser->num_template_parameter_lists = 0;
13586 /* We are not in a function body. */
13587 saved_in_function_body = parser->in_function_body;
13588 parser->in_function_body = false;
13590 /* Start the class. */
13591 if (nested_name_specifier_p)
13593 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13594 old_scope = push_inner_scope (scope);
13596 type = begin_class_definition (type, attributes);
13598 if (type == error_mark_node)
13599 /* If the type is erroneous, skip the entire body of the class. */
13600 cp_parser_skip_to_closing_brace (parser);
13602 /* Parse the member-specification. */
13603 cp_parser_member_specification_opt (parser);
13605 /* Look for the trailing `}'. */
13606 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13607 /* We get better error messages by noticing a common problem: a
13608 missing trailing `;'. */
13609 token = cp_lexer_peek_token (parser->lexer);
13610 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13611 /* Look for trailing attributes to apply to this class. */
13612 if (cp_parser_allow_gnu_extensions_p (parser))
13613 attributes = cp_parser_attributes_opt (parser);
13614 if (type != error_mark_node)
13615 type = finish_struct (type, attributes);
13616 if (nested_name_specifier_p)
13617 pop_inner_scope (old_scope, scope);
13618 /* If this class is not itself within the scope of another class,
13619 then we need to parse the bodies of all of the queued function
13620 definitions. Note that the queued functions defined in a class
13621 are not always processed immediately following the
13622 class-specifier for that class. Consider:
13625 struct B { void f() { sizeof (A); } };
13628 If `f' were processed before the processing of `A' were
13629 completed, there would be no way to compute the size of `A'.
13630 Note that the nesting we are interested in here is lexical --
13631 not the semantic nesting given by TYPE_CONTEXT. In particular,
13634 struct A { struct B; };
13635 struct A::B { void f() { } };
13637 there is no need to delay the parsing of `A::B::f'. */
13638 if (--parser->num_classes_being_defined == 0)
13642 tree class_type = NULL_TREE;
13643 tree pushed_scope = NULL_TREE;
13645 /* In a first pass, parse default arguments to the functions.
13646 Then, in a second pass, parse the bodies of the functions.
13647 This two-phased approach handles cases like:
13655 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13656 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13657 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13658 TREE_PURPOSE (parser->unparsed_functions_queues)
13659 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13661 fn = TREE_VALUE (queue_entry);
13662 /* If there are default arguments that have not yet been processed,
13663 take care of them now. */
13664 if (class_type != TREE_PURPOSE (queue_entry))
13667 pop_scope (pushed_scope);
13668 class_type = TREE_PURPOSE (queue_entry);
13669 pushed_scope = push_scope (class_type);
13671 /* Make sure that any template parameters are in scope. */
13672 maybe_begin_member_template_processing (fn);
13673 /* Parse the default argument expressions. */
13674 cp_parser_late_parsing_default_args (parser, fn);
13675 /* Remove any template parameters from the symbol table. */
13676 maybe_end_member_template_processing ();
13679 pop_scope (pushed_scope);
13680 /* Now parse the body of the functions. */
13681 for (TREE_VALUE (parser->unparsed_functions_queues)
13682 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13683 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13684 TREE_VALUE (parser->unparsed_functions_queues)
13685 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13687 /* Figure out which function we need to process. */
13688 fn = TREE_VALUE (queue_entry);
13689 /* Parse the function. */
13690 cp_parser_late_parsing_for_member (parser, fn);
13694 /* Put back any saved access checks. */
13695 pop_deferring_access_checks ();
13697 /* Restore saved state. */
13698 parser->in_function_body = saved_in_function_body;
13699 parser->num_template_parameter_lists
13700 = saved_num_template_parameter_lists;
13705 /* Parse a class-head.
13708 class-key identifier [opt] base-clause [opt]
13709 class-key nested-name-specifier identifier base-clause [opt]
13710 class-key nested-name-specifier [opt] template-id
13714 class-key attributes identifier [opt] base-clause [opt]
13715 class-key attributes nested-name-specifier identifier base-clause [opt]
13716 class-key attributes nested-name-specifier [opt] template-id
13719 Upon return BASES is initialized to the list of base classes (or
13720 NULL, if there are none) in the same form returned by
13721 cp_parser_base_clause.
13723 Returns the TYPE of the indicated class. Sets
13724 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13725 involving a nested-name-specifier was used, and FALSE otherwise.
13727 Returns error_mark_node if this is not a class-head.
13729 Returns NULL_TREE if the class-head is syntactically valid, but
13730 semantically invalid in a way that means we should skip the entire
13731 body of the class. */
13734 cp_parser_class_head (cp_parser* parser,
13735 bool* nested_name_specifier_p,
13736 tree *attributes_p,
13739 tree nested_name_specifier;
13740 enum tag_types class_key;
13741 tree id = NULL_TREE;
13742 tree type = NULL_TREE;
13744 bool template_id_p = false;
13745 bool qualified_p = false;
13746 bool invalid_nested_name_p = false;
13747 bool invalid_explicit_specialization_p = false;
13748 tree pushed_scope = NULL_TREE;
13749 unsigned num_templates;
13751 /* Assume no nested-name-specifier will be present. */
13752 *nested_name_specifier_p = false;
13753 /* Assume no template parameter lists will be used in defining the
13757 *bases = NULL_TREE;
13759 /* Look for the class-key. */
13760 class_key = cp_parser_class_key (parser);
13761 if (class_key == none_type)
13762 return error_mark_node;
13764 /* Parse the attributes. */
13765 attributes = cp_parser_attributes_opt (parser);
13767 /* If the next token is `::', that is invalid -- but sometimes
13768 people do try to write:
13772 Handle this gracefully by accepting the extra qualifier, and then
13773 issuing an error about it later if this really is a
13774 class-head. If it turns out just to be an elaborated type
13775 specifier, remain silent. */
13776 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13777 qualified_p = true;
13779 push_deferring_access_checks (dk_no_check);
13781 /* Determine the name of the class. Begin by looking for an
13782 optional nested-name-specifier. */
13783 nested_name_specifier
13784 = cp_parser_nested_name_specifier_opt (parser,
13785 /*typename_keyword_p=*/false,
13786 /*check_dependency_p=*/false,
13788 /*is_declaration=*/false);
13789 /* If there was a nested-name-specifier, then there *must* be an
13791 if (nested_name_specifier)
13793 /* Although the grammar says `identifier', it really means
13794 `class-name' or `template-name'. You are only allowed to
13795 define a class that has already been declared with this
13798 The proposed resolution for Core Issue 180 says that wherever
13799 you see `class T::X' you should treat `X' as a type-name.
13801 It is OK to define an inaccessible class; for example:
13803 class A { class B; };
13806 We do not know if we will see a class-name, or a
13807 template-name. We look for a class-name first, in case the
13808 class-name is a template-id; if we looked for the
13809 template-name first we would stop after the template-name. */
13810 cp_parser_parse_tentatively (parser);
13811 type = cp_parser_class_name (parser,
13812 /*typename_keyword_p=*/false,
13813 /*template_keyword_p=*/false,
13815 /*check_dependency_p=*/false,
13816 /*class_head_p=*/true,
13817 /*is_declaration=*/false);
13818 /* If that didn't work, ignore the nested-name-specifier. */
13819 if (!cp_parser_parse_definitely (parser))
13821 invalid_nested_name_p = true;
13822 id = cp_parser_identifier (parser);
13823 if (id == error_mark_node)
13826 /* If we could not find a corresponding TYPE, treat this
13827 declaration like an unqualified declaration. */
13828 if (type == error_mark_node)
13829 nested_name_specifier = NULL_TREE;
13830 /* Otherwise, count the number of templates used in TYPE and its
13831 containing scopes. */
13836 for (scope = TREE_TYPE (type);
13837 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13838 scope = (TYPE_P (scope)
13839 ? TYPE_CONTEXT (scope)
13840 : DECL_CONTEXT (scope)))
13842 && CLASS_TYPE_P (scope)
13843 && CLASSTYPE_TEMPLATE_INFO (scope)
13844 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13845 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13849 /* Otherwise, the identifier is optional. */
13852 /* We don't know whether what comes next is a template-id,
13853 an identifier, or nothing at all. */
13854 cp_parser_parse_tentatively (parser);
13855 /* Check for a template-id. */
13856 id = cp_parser_template_id (parser,
13857 /*template_keyword_p=*/false,
13858 /*check_dependency_p=*/true,
13859 /*is_declaration=*/true);
13860 /* If that didn't work, it could still be an identifier. */
13861 if (!cp_parser_parse_definitely (parser))
13863 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13864 id = cp_parser_identifier (parser);
13870 template_id_p = true;
13875 pop_deferring_access_checks ();
13878 cp_parser_check_for_invalid_template_id (parser, id);
13880 /* If it's not a `:' or a `{' then we can't really be looking at a
13881 class-head, since a class-head only appears as part of a
13882 class-specifier. We have to detect this situation before calling
13883 xref_tag, since that has irreversible side-effects. */
13884 if (!cp_parser_next_token_starts_class_definition_p (parser))
13886 cp_parser_error (parser, "expected %<{%> or %<:%>");
13887 return error_mark_node;
13890 /* At this point, we're going ahead with the class-specifier, even
13891 if some other problem occurs. */
13892 cp_parser_commit_to_tentative_parse (parser);
13893 /* Issue the error about the overly-qualified name now. */
13895 cp_parser_error (parser,
13896 "global qualification of class name is invalid");
13897 else if (invalid_nested_name_p)
13898 cp_parser_error (parser,
13899 "qualified name does not name a class");
13900 else if (nested_name_specifier)
13904 /* Reject typedef-names in class heads. */
13905 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13907 error ("invalid class name in declaration of %qD", type);
13912 /* Figure out in what scope the declaration is being placed. */
13913 scope = current_scope ();
13914 /* If that scope does not contain the scope in which the
13915 class was originally declared, the program is invalid. */
13916 if (scope && !is_ancestor (scope, nested_name_specifier))
13918 error ("declaration of %qD in %qD which does not enclose %qD",
13919 type, scope, nested_name_specifier);
13925 A declarator-id shall not be qualified exception of the
13926 definition of a ... nested class outside of its class
13927 ... [or] a the definition or explicit instantiation of a
13928 class member of a namespace outside of its namespace. */
13929 if (scope == nested_name_specifier)
13931 pedwarn ("extra qualification ignored");
13932 nested_name_specifier = NULL_TREE;
13936 /* An explicit-specialization must be preceded by "template <>". If
13937 it is not, try to recover gracefully. */
13938 if (at_namespace_scope_p ()
13939 && parser->num_template_parameter_lists == 0
13942 error ("an explicit specialization must be preceded by %<template <>%>");
13943 invalid_explicit_specialization_p = true;
13944 /* Take the same action that would have been taken by
13945 cp_parser_explicit_specialization. */
13946 ++parser->num_template_parameter_lists;
13947 begin_specialization ();
13949 /* There must be no "return" statements between this point and the
13950 end of this function; set "type "to the correct return value and
13951 use "goto done;" to return. */
13952 /* Make sure that the right number of template parameters were
13954 if (!cp_parser_check_template_parameters (parser, num_templates))
13956 /* If something went wrong, there is no point in even trying to
13957 process the class-definition. */
13962 /* Look up the type. */
13965 type = TREE_TYPE (id);
13966 type = maybe_process_partial_specialization (type);
13967 if (nested_name_specifier)
13968 pushed_scope = push_scope (nested_name_specifier);
13970 else if (nested_name_specifier)
13976 template <typename T> struct S { struct T };
13977 template <typename T> struct S<T>::T { };
13979 we will get a TYPENAME_TYPE when processing the definition of
13980 `S::T'. We need to resolve it to the actual type before we
13981 try to define it. */
13982 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13984 class_type = resolve_typename_type (TREE_TYPE (type),
13985 /*only_current_p=*/false);
13986 if (class_type != error_mark_node)
13987 type = TYPE_NAME (class_type);
13990 cp_parser_error (parser, "could not resolve typename type");
13991 type = error_mark_node;
13995 maybe_process_partial_specialization (TREE_TYPE (type));
13996 class_type = current_class_type;
13997 /* Enter the scope indicated by the nested-name-specifier. */
13998 pushed_scope = push_scope (nested_name_specifier);
13999 /* Get the canonical version of this type. */
14000 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14001 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14002 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14004 type = push_template_decl (type);
14005 if (type == error_mark_node)
14012 type = TREE_TYPE (type);
14013 *nested_name_specifier_p = true;
14015 else /* The name is not a nested name. */
14017 /* If the class was unnamed, create a dummy name. */
14019 id = make_anon_name ();
14020 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14021 parser->num_template_parameter_lists);
14024 /* Indicate whether this class was declared as a `class' or as a
14026 if (TREE_CODE (type) == RECORD_TYPE)
14027 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14028 cp_parser_check_class_key (class_key, type);
14030 /* If this type was already complete, and we see another definition,
14031 that's an error. */
14032 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14034 error ("redefinition of %q#T", type);
14035 error ("previous definition of %q+#T", type);
14039 else if (type == error_mark_node)
14042 /* We will have entered the scope containing the class; the names of
14043 base classes should be looked up in that context. For example:
14045 struct A { struct B {}; struct C; };
14046 struct A::C : B {};
14050 /* Get the list of base-classes, if there is one. */
14051 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14052 *bases = cp_parser_base_clause (parser);
14055 /* Leave the scope given by the nested-name-specifier. We will
14056 enter the class scope itself while processing the members. */
14058 pop_scope (pushed_scope);
14060 if (invalid_explicit_specialization_p)
14062 end_specialization ();
14063 --parser->num_template_parameter_lists;
14065 *attributes_p = attributes;
14069 /* Parse a class-key.
14076 Returns the kind of class-key specified, or none_type to indicate
14079 static enum tag_types
14080 cp_parser_class_key (cp_parser* parser)
14083 enum tag_types tag_type;
14085 /* Look for the class-key. */
14086 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14090 /* Check to see if the TOKEN is a class-key. */
14091 tag_type = cp_parser_token_is_class_key (token);
14093 cp_parser_error (parser, "expected class-key");
14097 /* Parse an (optional) member-specification.
14099 member-specification:
14100 member-declaration member-specification [opt]
14101 access-specifier : member-specification [opt] */
14104 cp_parser_member_specification_opt (cp_parser* parser)
14111 /* Peek at the next token. */
14112 token = cp_lexer_peek_token (parser->lexer);
14113 /* If it's a `}', or EOF then we've seen all the members. */
14114 if (token->type == CPP_CLOSE_BRACE
14115 || token->type == CPP_EOF
14116 || token->type == CPP_PRAGMA_EOL)
14119 /* See if this token is a keyword. */
14120 keyword = token->keyword;
14124 case RID_PROTECTED:
14126 /* Consume the access-specifier. */
14127 cp_lexer_consume_token (parser->lexer);
14128 /* Remember which access-specifier is active. */
14129 current_access_specifier = token->u.value;
14130 /* Look for the `:'. */
14131 cp_parser_require (parser, CPP_COLON, "`:'");
14135 /* Accept #pragmas at class scope. */
14136 if (token->type == CPP_PRAGMA)
14138 cp_parser_pragma (parser, pragma_external);
14142 /* Otherwise, the next construction must be a
14143 member-declaration. */
14144 cp_parser_member_declaration (parser);
14149 /* Parse a member-declaration.
14151 member-declaration:
14152 decl-specifier-seq [opt] member-declarator-list [opt] ;
14153 function-definition ; [opt]
14154 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14156 template-declaration
14158 member-declarator-list:
14160 member-declarator-list , member-declarator
14163 declarator pure-specifier [opt]
14164 declarator constant-initializer [opt]
14165 identifier [opt] : constant-expression
14169 member-declaration:
14170 __extension__ member-declaration
14173 declarator attributes [opt] pure-specifier [opt]
14174 declarator attributes [opt] constant-initializer [opt]
14175 identifier [opt] attributes [opt] : constant-expression
14179 member-declaration:
14180 static_assert-declaration */
14183 cp_parser_member_declaration (cp_parser* parser)
14185 cp_decl_specifier_seq decl_specifiers;
14186 tree prefix_attributes;
14188 int declares_class_or_enum;
14191 int saved_pedantic;
14193 /* Check for the `__extension__' keyword. */
14194 if (cp_parser_extension_opt (parser, &saved_pedantic))
14197 cp_parser_member_declaration (parser);
14198 /* Restore the old value of the PEDANTIC flag. */
14199 pedantic = saved_pedantic;
14204 /* Check for a template-declaration. */
14205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14207 /* An explicit specialization here is an error condition, and we
14208 expect the specialization handler to detect and report this. */
14209 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14210 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14211 cp_parser_explicit_specialization (parser);
14213 cp_parser_template_declaration (parser, /*member_p=*/true);
14218 /* Check for a using-declaration. */
14219 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14221 /* Parse the using-declaration. */
14222 cp_parser_using_declaration (parser,
14223 /*access_declaration_p=*/false);
14227 /* Check for @defs. */
14228 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14231 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14232 ivar = ivar_chains;
14236 ivar = TREE_CHAIN (member);
14237 TREE_CHAIN (member) = NULL_TREE;
14238 finish_member_declaration (member);
14243 /* If the next token is `static_assert' we have a static assertion. */
14244 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14246 cp_parser_static_assert (parser, /*member_p=*/true);
14250 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14253 /* Parse the decl-specifier-seq. */
14254 cp_parser_decl_specifier_seq (parser,
14255 CP_PARSER_FLAGS_OPTIONAL,
14257 &declares_class_or_enum);
14258 prefix_attributes = decl_specifiers.attributes;
14259 decl_specifiers.attributes = NULL_TREE;
14260 /* Check for an invalid type-name. */
14261 if (!decl_specifiers.type
14262 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14264 /* If there is no declarator, then the decl-specifier-seq should
14266 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14268 /* If there was no decl-specifier-seq, and the next token is a
14269 `;', then we have something like:
14275 Each member-declaration shall declare at least one member
14276 name of the class. */
14277 if (!decl_specifiers.any_specifiers_p)
14279 cp_token *token = cp_lexer_peek_token (parser->lexer);
14280 if (pedantic && !token->in_system_header)
14281 pedwarn ("%Hextra %<;%>", &token->location);
14287 /* See if this declaration is a friend. */
14288 friend_p = cp_parser_friend_p (&decl_specifiers);
14289 /* If there were decl-specifiers, check to see if there was
14290 a class-declaration. */
14291 type = check_tag_decl (&decl_specifiers);
14292 /* Nested classes have already been added to the class, but
14293 a `friend' needs to be explicitly registered. */
14296 /* If the `friend' keyword was present, the friend must
14297 be introduced with a class-key. */
14298 if (!declares_class_or_enum)
14299 error ("a class-key must be used when declaring a friend");
14302 template <typename T> struct A {
14303 friend struct A<T>::B;
14306 A<T>::B will be represented by a TYPENAME_TYPE, and
14307 therefore not recognized by check_tag_decl. */
14309 && decl_specifiers.type
14310 && TYPE_P (decl_specifiers.type))
14311 type = decl_specifiers.type;
14312 if (!type || !TYPE_P (type))
14313 error ("friend declaration does not name a class or "
14316 make_friend_class (current_class_type, type,
14317 /*complain=*/true);
14319 /* If there is no TYPE, an error message will already have
14321 else if (!type || type == error_mark_node)
14323 /* An anonymous aggregate has to be handled specially; such
14324 a declaration really declares a data member (with a
14325 particular type), as opposed to a nested class. */
14326 else if (ANON_AGGR_TYPE_P (type))
14328 /* Remove constructors and such from TYPE, now that we
14329 know it is an anonymous aggregate. */
14330 fixup_anonymous_aggr (type);
14331 /* And make the corresponding data member. */
14332 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14333 /* Add it to the class. */
14334 finish_member_declaration (decl);
14337 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14342 /* See if these declarations will be friends. */
14343 friend_p = cp_parser_friend_p (&decl_specifiers);
14345 /* Keep going until we hit the `;' at the end of the
14347 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14349 tree attributes = NULL_TREE;
14350 tree first_attribute;
14352 /* Peek at the next token. */
14353 token = cp_lexer_peek_token (parser->lexer);
14355 /* Check for a bitfield declaration. */
14356 if (token->type == CPP_COLON
14357 || (token->type == CPP_NAME
14358 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14364 /* Get the name of the bitfield. Note that we cannot just
14365 check TOKEN here because it may have been invalidated by
14366 the call to cp_lexer_peek_nth_token above. */
14367 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14368 identifier = cp_parser_identifier (parser);
14370 identifier = NULL_TREE;
14372 /* Consume the `:' token. */
14373 cp_lexer_consume_token (parser->lexer);
14374 /* Get the width of the bitfield. */
14376 = cp_parser_constant_expression (parser,
14377 /*allow_non_constant=*/false,
14380 /* Look for attributes that apply to the bitfield. */
14381 attributes = cp_parser_attributes_opt (parser);
14382 /* Remember which attributes are prefix attributes and
14384 first_attribute = attributes;
14385 /* Combine the attributes. */
14386 attributes = chainon (prefix_attributes, attributes);
14388 /* Create the bitfield declaration. */
14389 decl = grokbitfield (identifier
14390 ? make_id_declarator (NULL_TREE,
14396 /* Apply the attributes. */
14397 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14401 cp_declarator *declarator;
14403 tree asm_specification;
14404 int ctor_dtor_or_conv_p;
14406 /* Parse the declarator. */
14408 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14409 &ctor_dtor_or_conv_p,
14410 /*parenthesized_p=*/NULL,
14411 /*member_p=*/true);
14413 /* If something went wrong parsing the declarator, make sure
14414 that we at least consume some tokens. */
14415 if (declarator == cp_error_declarator)
14417 /* Skip to the end of the statement. */
14418 cp_parser_skip_to_end_of_statement (parser);
14419 /* If the next token is not a semicolon, that is
14420 probably because we just skipped over the body of
14421 a function. So, we consume a semicolon if
14422 present, but do not issue an error message if it
14424 if (cp_lexer_next_token_is (parser->lexer,
14426 cp_lexer_consume_token (parser->lexer);
14430 if (declares_class_or_enum & 2)
14431 cp_parser_check_for_definition_in_return_type
14432 (declarator, decl_specifiers.type);
14434 /* Look for an asm-specification. */
14435 asm_specification = cp_parser_asm_specification_opt (parser);
14436 /* Look for attributes that apply to the declaration. */
14437 attributes = cp_parser_attributes_opt (parser);
14438 /* Remember which attributes are prefix attributes and
14440 first_attribute = attributes;
14441 /* Combine the attributes. */
14442 attributes = chainon (prefix_attributes, attributes);
14444 /* If it's an `=', then we have a constant-initializer or a
14445 pure-specifier. It is not correct to parse the
14446 initializer before registering the member declaration
14447 since the member declaration should be in scope while
14448 its initializer is processed. However, the rest of the
14449 front end does not yet provide an interface that allows
14450 us to handle this correctly. */
14451 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14455 A pure-specifier shall be used only in the declaration of
14456 a virtual function.
14458 A member-declarator can contain a constant-initializer
14459 only if it declares a static member of integral or
14462 Therefore, if the DECLARATOR is for a function, we look
14463 for a pure-specifier; otherwise, we look for a
14464 constant-initializer. When we call `grokfield', it will
14465 perform more stringent semantics checks. */
14466 if (function_declarator_p (declarator))
14467 initializer = cp_parser_pure_specifier (parser);
14469 /* Parse the initializer. */
14470 initializer = cp_parser_constant_initializer (parser);
14472 /* Otherwise, there is no initializer. */
14474 initializer = NULL_TREE;
14476 /* See if we are probably looking at a function
14477 definition. We are certainly not looking at a
14478 member-declarator. Calling `grokfield' has
14479 side-effects, so we must not do it unless we are sure
14480 that we are looking at a member-declarator. */
14481 if (cp_parser_token_starts_function_definition_p
14482 (cp_lexer_peek_token (parser->lexer)))
14484 /* The grammar does not allow a pure-specifier to be
14485 used when a member function is defined. (It is
14486 possible that this fact is an oversight in the
14487 standard, since a pure function may be defined
14488 outside of the class-specifier. */
14490 error ("pure-specifier on function-definition");
14491 decl = cp_parser_save_member_function_body (parser,
14495 /* If the member was not a friend, declare it here. */
14497 finish_member_declaration (decl);
14498 /* Peek at the next token. */
14499 token = cp_lexer_peek_token (parser->lexer);
14500 /* If the next token is a semicolon, consume it. */
14501 if (token->type == CPP_SEMICOLON)
14503 if (pedantic && !in_system_header)
14504 pedwarn ("extra %<;%>");
14505 cp_lexer_consume_token (parser->lexer);
14510 /* Create the declaration. */
14511 decl = grokfield (declarator, &decl_specifiers,
14512 initializer, /*init_const_expr_p=*/true,
14517 /* Reset PREFIX_ATTRIBUTES. */
14518 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14519 attributes = TREE_CHAIN (attributes);
14521 TREE_CHAIN (attributes) = NULL_TREE;
14523 /* If there is any qualification still in effect, clear it
14524 now; we will be starting fresh with the next declarator. */
14525 parser->scope = NULL_TREE;
14526 parser->qualifying_scope = NULL_TREE;
14527 parser->object_scope = NULL_TREE;
14528 /* If it's a `,', then there are more declarators. */
14529 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14530 cp_lexer_consume_token (parser->lexer);
14531 /* If the next token isn't a `;', then we have a parse error. */
14532 else if (cp_lexer_next_token_is_not (parser->lexer,
14535 cp_parser_error (parser, "expected %<;%>");
14536 /* Skip tokens until we find a `;'. */
14537 cp_parser_skip_to_end_of_statement (parser);
14544 /* Add DECL to the list of members. */
14546 finish_member_declaration (decl);
14548 if (TREE_CODE (decl) == FUNCTION_DECL)
14549 cp_parser_save_default_args (parser, decl);
14554 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14557 /* Parse a pure-specifier.
14562 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14563 Otherwise, ERROR_MARK_NODE is returned. */
14566 cp_parser_pure_specifier (cp_parser* parser)
14570 /* Look for the `=' token. */
14571 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14572 return error_mark_node;
14573 /* Look for the `0' token. */
14574 token = cp_lexer_consume_token (parser->lexer);
14575 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14576 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14578 cp_parser_error (parser,
14579 "invalid pure specifier (only `= 0' is allowed)");
14580 cp_parser_skip_to_end_of_statement (parser);
14581 return error_mark_node;
14583 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14585 error ("templates may not be %<virtual%>");
14586 return error_mark_node;
14589 return integer_zero_node;
14592 /* Parse a constant-initializer.
14594 constant-initializer:
14595 = constant-expression
14597 Returns a representation of the constant-expression. */
14600 cp_parser_constant_initializer (cp_parser* parser)
14602 /* Look for the `=' token. */
14603 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14604 return error_mark_node;
14606 /* It is invalid to write:
14608 struct S { static const int i = { 7 }; };
14611 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14613 cp_parser_error (parser,
14614 "a brace-enclosed initializer is not allowed here");
14615 /* Consume the opening brace. */
14616 cp_lexer_consume_token (parser->lexer);
14617 /* Skip the initializer. */
14618 cp_parser_skip_to_closing_brace (parser);
14619 /* Look for the trailing `}'. */
14620 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14622 return error_mark_node;
14625 return cp_parser_constant_expression (parser,
14626 /*allow_non_constant=*/false,
14630 /* Derived classes [gram.class.derived] */
14632 /* Parse a base-clause.
14635 : base-specifier-list
14637 base-specifier-list:
14638 base-specifier ... [opt]
14639 base-specifier-list , base-specifier ... [opt]
14641 Returns a TREE_LIST representing the base-classes, in the order in
14642 which they were declared. The representation of each node is as
14643 described by cp_parser_base_specifier.
14645 In the case that no bases are specified, this function will return
14646 NULL_TREE, not ERROR_MARK_NODE. */
14649 cp_parser_base_clause (cp_parser* parser)
14651 tree bases = NULL_TREE;
14653 /* Look for the `:' that begins the list. */
14654 cp_parser_require (parser, CPP_COLON, "`:'");
14656 /* Scan the base-specifier-list. */
14661 bool pack_expansion_p = false;
14663 /* Look for the base-specifier. */
14664 base = cp_parser_base_specifier (parser);
14665 /* Look for the (optional) ellipsis. */
14666 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14668 /* Consume the `...'. */
14669 cp_lexer_consume_token (parser->lexer);
14671 pack_expansion_p = true;
14674 /* Add BASE to the front of the list. */
14675 if (base != error_mark_node)
14677 if (pack_expansion_p)
14678 /* Make this a pack expansion type. */
14679 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14681 check_for_bare_parameter_packs (TREE_VALUE (base));
14683 TREE_CHAIN (base) = bases;
14686 /* Peek at the next token. */
14687 token = cp_lexer_peek_token (parser->lexer);
14688 /* If it's not a comma, then the list is complete. */
14689 if (token->type != CPP_COMMA)
14691 /* Consume the `,'. */
14692 cp_lexer_consume_token (parser->lexer);
14695 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14696 base class had a qualified name. However, the next name that
14697 appears is certainly not qualified. */
14698 parser->scope = NULL_TREE;
14699 parser->qualifying_scope = NULL_TREE;
14700 parser->object_scope = NULL_TREE;
14702 return nreverse (bases);
14705 /* Parse a base-specifier.
14708 :: [opt] nested-name-specifier [opt] class-name
14709 virtual access-specifier [opt] :: [opt] nested-name-specifier
14711 access-specifier virtual [opt] :: [opt] nested-name-specifier
14714 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14715 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14716 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14717 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14720 cp_parser_base_specifier (cp_parser* parser)
14724 bool virtual_p = false;
14725 bool duplicate_virtual_error_issued_p = false;
14726 bool duplicate_access_error_issued_p = false;
14727 bool class_scope_p, template_p;
14728 tree access = access_default_node;
14731 /* Process the optional `virtual' and `access-specifier'. */
14734 /* Peek at the next token. */
14735 token = cp_lexer_peek_token (parser->lexer);
14736 /* Process `virtual'. */
14737 switch (token->keyword)
14740 /* If `virtual' appears more than once, issue an error. */
14741 if (virtual_p && !duplicate_virtual_error_issued_p)
14743 cp_parser_error (parser,
14744 "%<virtual%> specified more than once in base-specified");
14745 duplicate_virtual_error_issued_p = true;
14750 /* Consume the `virtual' token. */
14751 cp_lexer_consume_token (parser->lexer);
14756 case RID_PROTECTED:
14758 /* If more than one access specifier appears, issue an
14760 if (access != access_default_node
14761 && !duplicate_access_error_issued_p)
14763 cp_parser_error (parser,
14764 "more than one access specifier in base-specified");
14765 duplicate_access_error_issued_p = true;
14768 access = ridpointers[(int) token->keyword];
14770 /* Consume the access-specifier. */
14771 cp_lexer_consume_token (parser->lexer);
14780 /* It is not uncommon to see programs mechanically, erroneously, use
14781 the 'typename' keyword to denote (dependent) qualified types
14782 as base classes. */
14783 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14785 if (!processing_template_decl)
14786 error ("keyword %<typename%> not allowed outside of templates");
14788 error ("keyword %<typename%> not allowed in this context "
14789 "(the base class is implicitly a type)");
14790 cp_lexer_consume_token (parser->lexer);
14793 /* Look for the optional `::' operator. */
14794 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14795 /* Look for the nested-name-specifier. The simplest way to
14800 The keyword `typename' is not permitted in a base-specifier or
14801 mem-initializer; in these contexts a qualified name that
14802 depends on a template-parameter is implicitly assumed to be a
14805 is to pretend that we have seen the `typename' keyword at this
14807 cp_parser_nested_name_specifier_opt (parser,
14808 /*typename_keyword_p=*/true,
14809 /*check_dependency_p=*/true,
14811 /*is_declaration=*/true);
14812 /* If the base class is given by a qualified name, assume that names
14813 we see are type names or templates, as appropriate. */
14814 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14815 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14817 /* Finally, look for the class-name. */
14818 type = cp_parser_class_name (parser,
14822 /*check_dependency_p=*/true,
14823 /*class_head_p=*/false,
14824 /*is_declaration=*/true);
14826 if (type == error_mark_node)
14827 return error_mark_node;
14829 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14832 /* Exception handling [gram.exception] */
14834 /* Parse an (optional) exception-specification.
14836 exception-specification:
14837 throw ( type-id-list [opt] )
14839 Returns a TREE_LIST representing the exception-specification. The
14840 TREE_VALUE of each node is a type. */
14843 cp_parser_exception_specification_opt (cp_parser* parser)
14848 /* Peek at the next token. */
14849 token = cp_lexer_peek_token (parser->lexer);
14850 /* If it's not `throw', then there's no exception-specification. */
14851 if (!cp_parser_is_keyword (token, RID_THROW))
14854 /* Consume the `throw'. */
14855 cp_lexer_consume_token (parser->lexer);
14857 /* Look for the `('. */
14858 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14860 /* Peek at the next token. */
14861 token = cp_lexer_peek_token (parser->lexer);
14862 /* If it's not a `)', then there is a type-id-list. */
14863 if (token->type != CPP_CLOSE_PAREN)
14865 const char *saved_message;
14867 /* Types may not be defined in an exception-specification. */
14868 saved_message = parser->type_definition_forbidden_message;
14869 parser->type_definition_forbidden_message
14870 = "types may not be defined in an exception-specification";
14871 /* Parse the type-id-list. */
14872 type_id_list = cp_parser_type_id_list (parser);
14873 /* Restore the saved message. */
14874 parser->type_definition_forbidden_message = saved_message;
14877 type_id_list = empty_except_spec;
14879 /* Look for the `)'. */
14880 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14882 return type_id_list;
14885 /* Parse an (optional) type-id-list.
14889 type-id-list , type-id ... [opt]
14891 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14892 in the order that the types were presented. */
14895 cp_parser_type_id_list (cp_parser* parser)
14897 tree types = NULL_TREE;
14904 /* Get the next type-id. */
14905 type = cp_parser_type_id (parser);
14906 /* Parse the optional ellipsis. */
14907 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14909 /* Consume the `...'. */
14910 cp_lexer_consume_token (parser->lexer);
14912 /* Turn the type into a pack expansion expression. */
14913 type = make_pack_expansion (type);
14915 /* Add it to the list. */
14916 types = add_exception_specifier (types, type, /*complain=*/1);
14917 /* Peek at the next token. */
14918 token = cp_lexer_peek_token (parser->lexer);
14919 /* If it is not a `,', we are done. */
14920 if (token->type != CPP_COMMA)
14922 /* Consume the `,'. */
14923 cp_lexer_consume_token (parser->lexer);
14926 return nreverse (types);
14929 /* Parse a try-block.
14932 try compound-statement handler-seq */
14935 cp_parser_try_block (cp_parser* parser)
14939 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14940 try_block = begin_try_block ();
14941 cp_parser_compound_statement (parser, NULL, true);
14942 finish_try_block (try_block);
14943 cp_parser_handler_seq (parser);
14944 finish_handler_sequence (try_block);
14949 /* Parse a function-try-block.
14951 function-try-block:
14952 try ctor-initializer [opt] function-body handler-seq */
14955 cp_parser_function_try_block (cp_parser* parser)
14957 tree compound_stmt;
14959 bool ctor_initializer_p;
14961 /* Look for the `try' keyword. */
14962 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14964 /* Let the rest of the front end know where we are. */
14965 try_block = begin_function_try_block (&compound_stmt);
14966 /* Parse the function-body. */
14968 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14969 /* We're done with the `try' part. */
14970 finish_function_try_block (try_block);
14971 /* Parse the handlers. */
14972 cp_parser_handler_seq (parser);
14973 /* We're done with the handlers. */
14974 finish_function_handler_sequence (try_block, compound_stmt);
14976 return ctor_initializer_p;
14979 /* Parse a handler-seq.
14982 handler handler-seq [opt] */
14985 cp_parser_handler_seq (cp_parser* parser)
14991 /* Parse the handler. */
14992 cp_parser_handler (parser);
14993 /* Peek at the next token. */
14994 token = cp_lexer_peek_token (parser->lexer);
14995 /* If it's not `catch' then there are no more handlers. */
14996 if (!cp_parser_is_keyword (token, RID_CATCH))
15001 /* Parse a handler.
15004 catch ( exception-declaration ) compound-statement */
15007 cp_parser_handler (cp_parser* parser)
15012 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15013 handler = begin_handler ();
15014 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15015 declaration = cp_parser_exception_declaration (parser);
15016 finish_handler_parms (declaration, handler);
15017 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15018 cp_parser_compound_statement (parser, NULL, false);
15019 finish_handler (handler);
15022 /* Parse an exception-declaration.
15024 exception-declaration:
15025 type-specifier-seq declarator
15026 type-specifier-seq abstract-declarator
15030 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15031 ellipsis variant is used. */
15034 cp_parser_exception_declaration (cp_parser* parser)
15036 cp_decl_specifier_seq type_specifiers;
15037 cp_declarator *declarator;
15038 const char *saved_message;
15040 /* If it's an ellipsis, it's easy to handle. */
15041 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15043 /* Consume the `...' token. */
15044 cp_lexer_consume_token (parser->lexer);
15048 /* Types may not be defined in exception-declarations. */
15049 saved_message = parser->type_definition_forbidden_message;
15050 parser->type_definition_forbidden_message
15051 = "types may not be defined in exception-declarations";
15053 /* Parse the type-specifier-seq. */
15054 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15056 /* If it's a `)', then there is no declarator. */
15057 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15060 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15061 /*ctor_dtor_or_conv_p=*/NULL,
15062 /*parenthesized_p=*/NULL,
15063 /*member_p=*/false);
15065 /* Restore the saved message. */
15066 parser->type_definition_forbidden_message = saved_message;
15068 if (!type_specifiers.any_specifiers_p)
15069 return error_mark_node;
15071 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15074 /* Parse a throw-expression.
15077 throw assignment-expression [opt]
15079 Returns a THROW_EXPR representing the throw-expression. */
15082 cp_parser_throw_expression (cp_parser* parser)
15087 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15088 token = cp_lexer_peek_token (parser->lexer);
15089 /* Figure out whether or not there is an assignment-expression
15090 following the "throw" keyword. */
15091 if (token->type == CPP_COMMA
15092 || token->type == CPP_SEMICOLON
15093 || token->type == CPP_CLOSE_PAREN
15094 || token->type == CPP_CLOSE_SQUARE
15095 || token->type == CPP_CLOSE_BRACE
15096 || token->type == CPP_COLON)
15097 expression = NULL_TREE;
15099 expression = cp_parser_assignment_expression (parser,
15102 return build_throw (expression);
15105 /* GNU Extensions */
15107 /* Parse an (optional) asm-specification.
15110 asm ( string-literal )
15112 If the asm-specification is present, returns a STRING_CST
15113 corresponding to the string-literal. Otherwise, returns
15117 cp_parser_asm_specification_opt (cp_parser* parser)
15120 tree asm_specification;
15122 /* Peek at the next token. */
15123 token = cp_lexer_peek_token (parser->lexer);
15124 /* If the next token isn't the `asm' keyword, then there's no
15125 asm-specification. */
15126 if (!cp_parser_is_keyword (token, RID_ASM))
15129 /* Consume the `asm' token. */
15130 cp_lexer_consume_token (parser->lexer);
15131 /* Look for the `('. */
15132 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15134 /* Look for the string-literal. */
15135 asm_specification = cp_parser_string_literal (parser, false, false);
15137 /* Look for the `)'. */
15138 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15140 return asm_specification;
15143 /* Parse an asm-operand-list.
15147 asm-operand-list , asm-operand
15150 string-literal ( expression )
15151 [ string-literal ] string-literal ( expression )
15153 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15154 each node is the expression. The TREE_PURPOSE is itself a
15155 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15156 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15157 is a STRING_CST for the string literal before the parenthesis. */
15160 cp_parser_asm_operand_list (cp_parser* parser)
15162 tree asm_operands = NULL_TREE;
15166 tree string_literal;
15170 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15172 /* Consume the `[' token. */
15173 cp_lexer_consume_token (parser->lexer);
15174 /* Read the operand name. */
15175 name = cp_parser_identifier (parser);
15176 if (name != error_mark_node)
15177 name = build_string (IDENTIFIER_LENGTH (name),
15178 IDENTIFIER_POINTER (name));
15179 /* Look for the closing `]'. */
15180 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15184 /* Look for the string-literal. */
15185 string_literal = cp_parser_string_literal (parser, false, false);
15187 /* Look for the `('. */
15188 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15189 /* Parse the expression. */
15190 expression = cp_parser_expression (parser, /*cast_p=*/false);
15191 /* Look for the `)'. */
15192 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15194 /* Add this operand to the list. */
15195 asm_operands = tree_cons (build_tree_list (name, string_literal),
15198 /* If the next token is not a `,', there are no more
15200 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15202 /* Consume the `,'. */
15203 cp_lexer_consume_token (parser->lexer);
15206 return nreverse (asm_operands);
15209 /* Parse an asm-clobber-list.
15213 asm-clobber-list , string-literal
15215 Returns a TREE_LIST, indicating the clobbers in the order that they
15216 appeared. The TREE_VALUE of each node is a STRING_CST. */
15219 cp_parser_asm_clobber_list (cp_parser* parser)
15221 tree clobbers = NULL_TREE;
15225 tree string_literal;
15227 /* Look for the string literal. */
15228 string_literal = cp_parser_string_literal (parser, false, false);
15229 /* Add it to the list. */
15230 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15231 /* If the next token is not a `,', then the list is
15233 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15235 /* Consume the `,' token. */
15236 cp_lexer_consume_token (parser->lexer);
15242 /* Parse an (optional) series of attributes.
15245 attributes attribute
15248 __attribute__ (( attribute-list [opt] ))
15250 The return value is as for cp_parser_attribute_list. */
15253 cp_parser_attributes_opt (cp_parser* parser)
15255 tree attributes = NULL_TREE;
15260 tree attribute_list;
15262 /* Peek at the next token. */
15263 token = cp_lexer_peek_token (parser->lexer);
15264 /* If it's not `__attribute__', then we're done. */
15265 if (token->keyword != RID_ATTRIBUTE)
15268 /* Consume the `__attribute__' keyword. */
15269 cp_lexer_consume_token (parser->lexer);
15270 /* Look for the two `(' tokens. */
15271 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15272 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15274 /* Peek at the next token. */
15275 token = cp_lexer_peek_token (parser->lexer);
15276 if (token->type != CPP_CLOSE_PAREN)
15277 /* Parse the attribute-list. */
15278 attribute_list = cp_parser_attribute_list (parser);
15280 /* If the next token is a `)', then there is no attribute
15282 attribute_list = NULL;
15284 /* Look for the two `)' tokens. */
15285 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15286 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15288 /* Add these new attributes to the list. */
15289 attributes = chainon (attributes, attribute_list);
15295 /* Parse an attribute-list.
15299 attribute-list , attribute
15303 identifier ( identifier )
15304 identifier ( identifier , expression-list )
15305 identifier ( expression-list )
15307 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15308 to an attribute. The TREE_PURPOSE of each node is the identifier
15309 indicating which attribute is in use. The TREE_VALUE represents
15310 the arguments, if any. */
15313 cp_parser_attribute_list (cp_parser* parser)
15315 tree attribute_list = NULL_TREE;
15316 bool save_translate_strings_p = parser->translate_strings_p;
15318 parser->translate_strings_p = false;
15325 /* Look for the identifier. We also allow keywords here; for
15326 example `__attribute__ ((const))' is legal. */
15327 token = cp_lexer_peek_token (parser->lexer);
15328 if (token->type == CPP_NAME
15329 || token->type == CPP_KEYWORD)
15331 tree arguments = NULL_TREE;
15333 /* Consume the token. */
15334 token = cp_lexer_consume_token (parser->lexer);
15336 /* Save away the identifier that indicates which attribute
15338 identifier = token->u.value;
15339 attribute = build_tree_list (identifier, NULL_TREE);
15341 /* Peek at the next token. */
15342 token = cp_lexer_peek_token (parser->lexer);
15343 /* If it's an `(', then parse the attribute arguments. */
15344 if (token->type == CPP_OPEN_PAREN)
15346 arguments = cp_parser_parenthesized_expression_list
15347 (parser, true, /*cast_p=*/false,
15348 /*allow_expansion_p=*/false,
15349 /*non_constant_p=*/NULL);
15350 /* Save the arguments away. */
15351 TREE_VALUE (attribute) = arguments;
15354 if (arguments != error_mark_node)
15356 /* Add this attribute to the list. */
15357 TREE_CHAIN (attribute) = attribute_list;
15358 attribute_list = attribute;
15361 token = cp_lexer_peek_token (parser->lexer);
15363 /* Now, look for more attributes. If the next token isn't a
15364 `,', we're done. */
15365 if (token->type != CPP_COMMA)
15368 /* Consume the comma and keep going. */
15369 cp_lexer_consume_token (parser->lexer);
15371 parser->translate_strings_p = save_translate_strings_p;
15373 /* We built up the list in reverse order. */
15374 return nreverse (attribute_list);
15377 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15378 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15379 current value of the PEDANTIC flag, regardless of whether or not
15380 the `__extension__' keyword is present. The caller is responsible
15381 for restoring the value of the PEDANTIC flag. */
15384 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15386 /* Save the old value of the PEDANTIC flag. */
15387 *saved_pedantic = pedantic;
15389 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15391 /* Consume the `__extension__' token. */
15392 cp_lexer_consume_token (parser->lexer);
15393 /* We're not being pedantic while the `__extension__' keyword is
15403 /* Parse a label declaration.
15406 __label__ label-declarator-seq ;
15408 label-declarator-seq:
15409 identifier , label-declarator-seq
15413 cp_parser_label_declaration (cp_parser* parser)
15415 /* Look for the `__label__' keyword. */
15416 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15422 /* Look for an identifier. */
15423 identifier = cp_parser_identifier (parser);
15424 /* If we failed, stop. */
15425 if (identifier == error_mark_node)
15427 /* Declare it as a label. */
15428 finish_label_decl (identifier);
15429 /* If the next token is a `;', stop. */
15430 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15432 /* Look for the `,' separating the label declarations. */
15433 cp_parser_require (parser, CPP_COMMA, "`,'");
15436 /* Look for the final `;'. */
15437 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15440 /* Support Functions */
15442 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15443 NAME should have one of the representations used for an
15444 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15445 is returned. If PARSER->SCOPE is a dependent type, then a
15446 SCOPE_REF is returned.
15448 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15449 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15450 was formed. Abstractly, such entities should not be passed to this
15451 function, because they do not need to be looked up, but it is
15452 simpler to check for this special case here, rather than at the
15455 In cases not explicitly covered above, this function returns a
15456 DECL, OVERLOAD, or baselink representing the result of the lookup.
15457 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15460 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15461 (e.g., "struct") that was used. In that case bindings that do not
15462 refer to types are ignored.
15464 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15467 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15470 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15473 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15474 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15475 NULL_TREE otherwise. */
15478 cp_parser_lookup_name (cp_parser *parser, tree name,
15479 enum tag_types tag_type,
15482 bool check_dependency,
15483 tree *ambiguous_decls)
15487 tree object_type = parser->context->object_type;
15489 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15490 flags |= LOOKUP_COMPLAIN;
15492 /* Assume that the lookup will be unambiguous. */
15493 if (ambiguous_decls)
15494 *ambiguous_decls = NULL_TREE;
15496 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15497 no longer valid. Note that if we are parsing tentatively, and
15498 the parse fails, OBJECT_TYPE will be automatically restored. */
15499 parser->context->object_type = NULL_TREE;
15501 if (name == error_mark_node)
15502 return error_mark_node;
15504 /* A template-id has already been resolved; there is no lookup to
15506 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15508 if (BASELINK_P (name))
15510 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15511 == TEMPLATE_ID_EXPR);
15515 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15516 it should already have been checked to make sure that the name
15517 used matches the type being destroyed. */
15518 if (TREE_CODE (name) == BIT_NOT_EXPR)
15522 /* Figure out to which type this destructor applies. */
15524 type = parser->scope;
15525 else if (object_type)
15526 type = object_type;
15528 type = current_class_type;
15529 /* If that's not a class type, there is no destructor. */
15530 if (!type || !CLASS_TYPE_P (type))
15531 return error_mark_node;
15532 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15533 lazily_declare_fn (sfk_destructor, type);
15534 if (!CLASSTYPE_DESTRUCTORS (type))
15535 return error_mark_node;
15536 /* If it was a class type, return the destructor. */
15537 return CLASSTYPE_DESTRUCTORS (type);
15540 /* By this point, the NAME should be an ordinary identifier. If
15541 the id-expression was a qualified name, the qualifying scope is
15542 stored in PARSER->SCOPE at this point. */
15543 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15545 /* Perform the lookup. */
15550 if (parser->scope == error_mark_node)
15551 return error_mark_node;
15553 /* If the SCOPE is dependent, the lookup must be deferred until
15554 the template is instantiated -- unless we are explicitly
15555 looking up names in uninstantiated templates. Even then, we
15556 cannot look up the name if the scope is not a class type; it
15557 might, for example, be a template type parameter. */
15558 dependent_p = (TYPE_P (parser->scope)
15559 && !(parser->in_declarator_p
15560 && currently_open_class (parser->scope))
15561 && dependent_type_p (parser->scope));
15562 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15569 /* The resolution to Core Issue 180 says that `struct
15570 A::B' should be considered a type-name, even if `A'
15572 type = make_typename_type (parser->scope, name, tag_type,
15573 /*complain=*/tf_error);
15574 decl = TYPE_NAME (type);
15576 else if (is_template
15577 && (cp_parser_next_token_ends_template_argument_p (parser)
15578 || cp_lexer_next_token_is (parser->lexer,
15580 decl = make_unbound_class_template (parser->scope,
15582 /*complain=*/tf_error);
15584 decl = build_qualified_name (/*type=*/NULL_TREE,
15585 parser->scope, name,
15590 tree pushed_scope = NULL_TREE;
15592 /* If PARSER->SCOPE is a dependent type, then it must be a
15593 class type, and we must not be checking dependencies;
15594 otherwise, we would have processed this lookup above. So
15595 that PARSER->SCOPE is not considered a dependent base by
15596 lookup_member, we must enter the scope here. */
15598 pushed_scope = push_scope (parser->scope);
15599 /* If the PARSER->SCOPE is a template specialization, it
15600 may be instantiated during name lookup. In that case,
15601 errors may be issued. Even if we rollback the current
15602 tentative parse, those errors are valid. */
15603 decl = lookup_qualified_name (parser->scope, name,
15604 tag_type != none_type,
15605 /*complain=*/true);
15607 pop_scope (pushed_scope);
15609 parser->qualifying_scope = parser->scope;
15610 parser->object_scope = NULL_TREE;
15612 else if (object_type)
15614 tree object_decl = NULL_TREE;
15615 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15616 OBJECT_TYPE is not a class. */
15617 if (CLASS_TYPE_P (object_type))
15618 /* If the OBJECT_TYPE is a template specialization, it may
15619 be instantiated during name lookup. In that case, errors
15620 may be issued. Even if we rollback the current tentative
15621 parse, those errors are valid. */
15622 object_decl = lookup_member (object_type,
15625 tag_type != none_type);
15626 /* Look it up in the enclosing context, too. */
15627 decl = lookup_name_real (name, tag_type != none_type,
15629 /*block_p=*/true, is_namespace, flags);
15630 parser->object_scope = object_type;
15631 parser->qualifying_scope = NULL_TREE;
15633 decl = object_decl;
15637 decl = lookup_name_real (name, tag_type != none_type,
15639 /*block_p=*/true, is_namespace, flags);
15640 parser->qualifying_scope = NULL_TREE;
15641 parser->object_scope = NULL_TREE;
15644 /* If the lookup failed, let our caller know. */
15645 if (!decl || decl == error_mark_node)
15646 return error_mark_node;
15648 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15649 if (TREE_CODE (decl) == TREE_LIST)
15651 if (ambiguous_decls)
15652 *ambiguous_decls = decl;
15653 /* The error message we have to print is too complicated for
15654 cp_parser_error, so we incorporate its actions directly. */
15655 if (!cp_parser_simulate_error (parser))
15657 error ("reference to %qD is ambiguous", name);
15658 print_candidates (decl);
15660 return error_mark_node;
15663 gcc_assert (DECL_P (decl)
15664 || TREE_CODE (decl) == OVERLOAD
15665 || TREE_CODE (decl) == SCOPE_REF
15666 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15667 || BASELINK_P (decl));
15669 /* If we have resolved the name of a member declaration, check to
15670 see if the declaration is accessible. When the name resolves to
15671 set of overloaded functions, accessibility is checked when
15672 overload resolution is done.
15674 During an explicit instantiation, access is not checked at all,
15675 as per [temp.explicit]. */
15677 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15682 /* Like cp_parser_lookup_name, but for use in the typical case where
15683 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15684 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15687 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15689 return cp_parser_lookup_name (parser, name,
15691 /*is_template=*/false,
15692 /*is_namespace=*/false,
15693 /*check_dependency=*/true,
15694 /*ambiguous_decls=*/NULL);
15697 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15698 the current context, return the TYPE_DECL. If TAG_NAME_P is
15699 true, the DECL indicates the class being defined in a class-head,
15700 or declared in an elaborated-type-specifier.
15702 Otherwise, return DECL. */
15705 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15707 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15708 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15711 template <typename T> struct B;
15714 template <typename T> struct A::B {};
15716 Similarly, in an elaborated-type-specifier:
15718 namespace N { struct X{}; }
15721 template <typename T> friend struct N::X;
15724 However, if the DECL refers to a class type, and we are in
15725 the scope of the class, then the name lookup automatically
15726 finds the TYPE_DECL created by build_self_reference rather
15727 than a TEMPLATE_DECL. For example, in:
15729 template <class T> struct S {
15733 there is no need to handle such case. */
15735 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15736 return DECL_TEMPLATE_RESULT (decl);
15741 /* If too many, or too few, template-parameter lists apply to the
15742 declarator, issue an error message. Returns TRUE if all went well,
15743 and FALSE otherwise. */
15746 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15747 cp_declarator *declarator)
15749 unsigned num_templates;
15751 /* We haven't seen any classes that involve template parameters yet. */
15754 switch (declarator->kind)
15757 if (declarator->u.id.qualifying_scope)
15762 scope = declarator->u.id.qualifying_scope;
15763 member = declarator->u.id.unqualified_name;
15765 while (scope && CLASS_TYPE_P (scope))
15767 /* You're supposed to have one `template <...>'
15768 for every template class, but you don't need one
15769 for a full specialization. For example:
15771 template <class T> struct S{};
15772 template <> struct S<int> { void f(); };
15773 void S<int>::f () {}
15775 is correct; there shouldn't be a `template <>' for
15776 the definition of `S<int>::f'. */
15777 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15778 /* If SCOPE does not have template information of any
15779 kind, then it is not a template, nor is it nested
15780 within a template. */
15782 if (explicit_class_specialization_p (scope))
15784 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15787 scope = TYPE_CONTEXT (scope);
15790 else if (TREE_CODE (declarator->u.id.unqualified_name)
15791 == TEMPLATE_ID_EXPR)
15792 /* If the DECLARATOR has the form `X<y>' then it uses one
15793 additional level of template parameters. */
15796 return cp_parser_check_template_parameters (parser,
15802 case cdk_reference:
15804 return (cp_parser_check_declarator_template_parameters
15805 (parser, declarator->declarator));
15811 gcc_unreachable ();
15816 /* NUM_TEMPLATES were used in the current declaration. If that is
15817 invalid, return FALSE and issue an error messages. Otherwise,
15821 cp_parser_check_template_parameters (cp_parser* parser,
15822 unsigned num_templates)
15824 /* If there are more template classes than parameter lists, we have
15827 template <class T> void S<T>::R<T>::f (); */
15828 if (parser->num_template_parameter_lists < num_templates)
15830 error ("too few template-parameter-lists");
15833 /* If there are the same number of template classes and parameter
15834 lists, that's OK. */
15835 if (parser->num_template_parameter_lists == num_templates)
15837 /* If there are more, but only one more, then we are referring to a
15838 member template. That's OK too. */
15839 if (parser->num_template_parameter_lists == num_templates + 1)
15841 /* Otherwise, there are too many template parameter lists. We have
15844 template <class T> template <class U> void S::f(); */
15845 error ("too many template-parameter-lists");
15849 /* Parse an optional `::' token indicating that the following name is
15850 from the global namespace. If so, PARSER->SCOPE is set to the
15851 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15852 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15853 Returns the new value of PARSER->SCOPE, if the `::' token is
15854 present, and NULL_TREE otherwise. */
15857 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15861 /* Peek at the next token. */
15862 token = cp_lexer_peek_token (parser->lexer);
15863 /* If we're looking at a `::' token then we're starting from the
15864 global namespace, not our current location. */
15865 if (token->type == CPP_SCOPE)
15867 /* Consume the `::' token. */
15868 cp_lexer_consume_token (parser->lexer);
15869 /* Set the SCOPE so that we know where to start the lookup. */
15870 parser->scope = global_namespace;
15871 parser->qualifying_scope = global_namespace;
15872 parser->object_scope = NULL_TREE;
15874 return parser->scope;
15876 else if (!current_scope_valid_p)
15878 parser->scope = NULL_TREE;
15879 parser->qualifying_scope = NULL_TREE;
15880 parser->object_scope = NULL_TREE;
15886 /* Returns TRUE if the upcoming token sequence is the start of a
15887 constructor declarator. If FRIEND_P is true, the declarator is
15888 preceded by the `friend' specifier. */
15891 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15893 bool constructor_p;
15894 tree type_decl = NULL_TREE;
15895 bool nested_name_p;
15896 cp_token *next_token;
15898 /* The common case is that this is not a constructor declarator, so
15899 try to avoid doing lots of work if at all possible. It's not
15900 valid declare a constructor at function scope. */
15901 if (parser->in_function_body)
15903 /* And only certain tokens can begin a constructor declarator. */
15904 next_token = cp_lexer_peek_token (parser->lexer);
15905 if (next_token->type != CPP_NAME
15906 && next_token->type != CPP_SCOPE
15907 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15908 && next_token->type != CPP_TEMPLATE_ID)
15911 /* Parse tentatively; we are going to roll back all of the tokens
15913 cp_parser_parse_tentatively (parser);
15914 /* Assume that we are looking at a constructor declarator. */
15915 constructor_p = true;
15917 /* Look for the optional `::' operator. */
15918 cp_parser_global_scope_opt (parser,
15919 /*current_scope_valid_p=*/false);
15920 /* Look for the nested-name-specifier. */
15922 = (cp_parser_nested_name_specifier_opt (parser,
15923 /*typename_keyword_p=*/false,
15924 /*check_dependency_p=*/false,
15926 /*is_declaration=*/false)
15928 /* Outside of a class-specifier, there must be a
15929 nested-name-specifier. */
15930 if (!nested_name_p &&
15931 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15933 constructor_p = false;
15934 /* If we still think that this might be a constructor-declarator,
15935 look for a class-name. */
15940 template <typename T> struct S { S(); };
15941 template <typename T> S<T>::S ();
15943 we must recognize that the nested `S' names a class.
15946 template <typename T> S<T>::S<T> ();
15948 we must recognize that the nested `S' names a template. */
15949 type_decl = cp_parser_class_name (parser,
15950 /*typename_keyword_p=*/false,
15951 /*template_keyword_p=*/false,
15953 /*check_dependency_p=*/false,
15954 /*class_head_p=*/false,
15955 /*is_declaration=*/false);
15956 /* If there was no class-name, then this is not a constructor. */
15957 constructor_p = !cp_parser_error_occurred (parser);
15960 /* If we're still considering a constructor, we have to see a `(',
15961 to begin the parameter-declaration-clause, followed by either a
15962 `)', an `...', or a decl-specifier. We need to check for a
15963 type-specifier to avoid being fooled into thinking that:
15967 is a constructor. (It is actually a function named `f' that
15968 takes one parameter (of type `int') and returns a value of type
15971 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15973 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15974 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15975 /* A parameter declaration begins with a decl-specifier,
15976 which is either the "attribute" keyword, a storage class
15977 specifier, or (usually) a type-specifier. */
15978 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15981 tree pushed_scope = NULL_TREE;
15982 unsigned saved_num_template_parameter_lists;
15984 /* Names appearing in the type-specifier should be looked up
15985 in the scope of the class. */
15986 if (current_class_type)
15990 type = TREE_TYPE (type_decl);
15991 if (TREE_CODE (type) == TYPENAME_TYPE)
15993 type = resolve_typename_type (type,
15994 /*only_current_p=*/false);
15995 if (type == error_mark_node)
15997 cp_parser_abort_tentative_parse (parser);
16001 pushed_scope = push_scope (type);
16004 /* Inside the constructor parameter list, surrounding
16005 template-parameter-lists do not apply. */
16006 saved_num_template_parameter_lists
16007 = parser->num_template_parameter_lists;
16008 parser->num_template_parameter_lists = 0;
16010 /* Look for the type-specifier. */
16011 cp_parser_type_specifier (parser,
16012 CP_PARSER_FLAGS_NONE,
16013 /*decl_specs=*/NULL,
16014 /*is_declarator=*/true,
16015 /*declares_class_or_enum=*/NULL,
16016 /*is_cv_qualifier=*/NULL);
16018 parser->num_template_parameter_lists
16019 = saved_num_template_parameter_lists;
16021 /* Leave the scope of the class. */
16023 pop_scope (pushed_scope);
16025 constructor_p = !cp_parser_error_occurred (parser);
16029 constructor_p = false;
16030 /* We did not really want to consume any tokens. */
16031 cp_parser_abort_tentative_parse (parser);
16033 return constructor_p;
16036 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16037 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16038 they must be performed once we are in the scope of the function.
16040 Returns the function defined. */
16043 cp_parser_function_definition_from_specifiers_and_declarator
16044 (cp_parser* parser,
16045 cp_decl_specifier_seq *decl_specifiers,
16047 const cp_declarator *declarator)
16052 /* Begin the function-definition. */
16053 success_p = start_function (decl_specifiers, declarator, attributes);
16055 /* The things we're about to see are not directly qualified by any
16056 template headers we've seen thus far. */
16057 reset_specialization ();
16059 /* If there were names looked up in the decl-specifier-seq that we
16060 did not check, check them now. We must wait until we are in the
16061 scope of the function to perform the checks, since the function
16062 might be a friend. */
16063 perform_deferred_access_checks ();
16067 /* Skip the entire function. */
16068 cp_parser_skip_to_end_of_block_or_statement (parser);
16069 fn = error_mark_node;
16071 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16073 /* Seen already, skip it. An error message has already been output. */
16074 cp_parser_skip_to_end_of_block_or_statement (parser);
16075 fn = current_function_decl;
16076 current_function_decl = NULL_TREE;
16077 /* If this is a function from a class, pop the nested class. */
16078 if (current_class_name)
16079 pop_nested_class ();
16082 fn = cp_parser_function_definition_after_declarator (parser,
16083 /*inline_p=*/false);
16088 /* Parse the part of a function-definition that follows the
16089 declarator. INLINE_P is TRUE iff this function is an inline
16090 function defined with a class-specifier.
16092 Returns the function defined. */
16095 cp_parser_function_definition_after_declarator (cp_parser* parser,
16099 bool ctor_initializer_p = false;
16100 bool saved_in_unbraced_linkage_specification_p;
16101 bool saved_in_function_body;
16102 unsigned saved_num_template_parameter_lists;
16104 saved_in_function_body = parser->in_function_body;
16105 parser->in_function_body = true;
16106 /* If the next token is `return', then the code may be trying to
16107 make use of the "named return value" extension that G++ used to
16109 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16111 /* Consume the `return' keyword. */
16112 cp_lexer_consume_token (parser->lexer);
16113 /* Look for the identifier that indicates what value is to be
16115 cp_parser_identifier (parser);
16116 /* Issue an error message. */
16117 error ("named return values are no longer supported");
16118 /* Skip tokens until we reach the start of the function body. */
16121 cp_token *token = cp_lexer_peek_token (parser->lexer);
16122 if (token->type == CPP_OPEN_BRACE
16123 || token->type == CPP_EOF
16124 || token->type == CPP_PRAGMA_EOL)
16126 cp_lexer_consume_token (parser->lexer);
16129 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16130 anything declared inside `f'. */
16131 saved_in_unbraced_linkage_specification_p
16132 = parser->in_unbraced_linkage_specification_p;
16133 parser->in_unbraced_linkage_specification_p = false;
16134 /* Inside the function, surrounding template-parameter-lists do not
16136 saved_num_template_parameter_lists
16137 = parser->num_template_parameter_lists;
16138 parser->num_template_parameter_lists = 0;
16139 /* If the next token is `try', then we are looking at a
16140 function-try-block. */
16141 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16142 ctor_initializer_p = cp_parser_function_try_block (parser);
16143 /* A function-try-block includes the function-body, so we only do
16144 this next part if we're not processing a function-try-block. */
16147 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16149 /* Finish the function. */
16150 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16151 (inline_p ? 2 : 0));
16152 /* Generate code for it, if necessary. */
16153 expand_or_defer_fn (fn);
16154 /* Restore the saved values. */
16155 parser->in_unbraced_linkage_specification_p
16156 = saved_in_unbraced_linkage_specification_p;
16157 parser->num_template_parameter_lists
16158 = saved_num_template_parameter_lists;
16159 parser->in_function_body = saved_in_function_body;
16164 /* Parse a template-declaration, assuming that the `export' (and
16165 `extern') keywords, if present, has already been scanned. MEMBER_P
16166 is as for cp_parser_template_declaration. */
16169 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16171 tree decl = NULL_TREE;
16172 VEC (deferred_access_check,gc) *checks;
16173 tree parameter_list;
16174 bool friend_p = false;
16175 bool need_lang_pop;
16177 /* Look for the `template' keyword. */
16178 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16182 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16184 if (at_class_scope_p () && current_function_decl)
16186 /* 14.5.2.2 [temp.mem]
16188 A local class shall not have member templates. */
16189 error ("invalid declaration of member template in local class");
16190 cp_parser_skip_to_end_of_block_or_statement (parser);
16195 A template ... shall not have C linkage. */
16196 if (current_lang_name == lang_name_c)
16198 error ("template with C linkage");
16199 /* Give it C++ linkage to avoid confusing other parts of the
16201 push_lang_context (lang_name_cplusplus);
16202 need_lang_pop = true;
16205 need_lang_pop = false;
16207 /* We cannot perform access checks on the template parameter
16208 declarations until we know what is being declared, just as we
16209 cannot check the decl-specifier list. */
16210 push_deferring_access_checks (dk_deferred);
16212 /* If the next token is `>', then we have an invalid
16213 specialization. Rather than complain about an invalid template
16214 parameter, issue an error message here. */
16215 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16217 cp_parser_error (parser, "invalid explicit specialization");
16218 begin_specialization ();
16219 parameter_list = NULL_TREE;
16222 /* Parse the template parameters. */
16223 parameter_list = cp_parser_template_parameter_list (parser);
16225 /* Get the deferred access checks from the parameter list. These
16226 will be checked once we know what is being declared, as for a
16227 member template the checks must be performed in the scope of the
16228 class containing the member. */
16229 checks = get_deferred_access_checks ();
16231 /* Look for the `>'. */
16232 cp_parser_skip_to_end_of_template_parameter_list (parser);
16233 /* We just processed one more parameter list. */
16234 ++parser->num_template_parameter_lists;
16235 /* If the next token is `template', there are more template
16237 if (cp_lexer_next_token_is_keyword (parser->lexer,
16239 cp_parser_template_declaration_after_export (parser, member_p);
16242 /* There are no access checks when parsing a template, as we do not
16243 know if a specialization will be a friend. */
16244 push_deferring_access_checks (dk_no_check);
16245 decl = cp_parser_single_declaration (parser,
16249 pop_deferring_access_checks ();
16251 /* If this is a member template declaration, let the front
16253 if (member_p && !friend_p && decl)
16255 if (TREE_CODE (decl) == TYPE_DECL)
16256 cp_parser_check_access_in_redeclaration (decl);
16258 decl = finish_member_template_decl (decl);
16260 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16261 make_friend_class (current_class_type, TREE_TYPE (decl),
16262 /*complain=*/true);
16264 /* We are done with the current parameter list. */
16265 --parser->num_template_parameter_lists;
16267 pop_deferring_access_checks ();
16270 finish_template_decl (parameter_list);
16272 /* Register member declarations. */
16273 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16274 finish_member_declaration (decl);
16275 /* For the erroneous case of a template with C linkage, we pushed an
16276 implicit C++ linkage scope; exit that scope now. */
16278 pop_lang_context ();
16279 /* If DECL is a function template, we must return to parse it later.
16280 (Even though there is no definition, there might be default
16281 arguments that need handling.) */
16282 if (member_p && decl
16283 && (TREE_CODE (decl) == FUNCTION_DECL
16284 || DECL_FUNCTION_TEMPLATE_P (decl)))
16285 TREE_VALUE (parser->unparsed_functions_queues)
16286 = tree_cons (NULL_TREE, decl,
16287 TREE_VALUE (parser->unparsed_functions_queues));
16290 /* Perform the deferred access checks from a template-parameter-list.
16291 CHECKS is a TREE_LIST of access checks, as returned by
16292 get_deferred_access_checks. */
16295 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16297 ++processing_template_parmlist;
16298 perform_access_checks (checks);
16299 --processing_template_parmlist;
16302 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16303 `function-definition' sequence. MEMBER_P is true, this declaration
16304 appears in a class scope.
16306 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16307 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16310 cp_parser_single_declaration (cp_parser* parser,
16311 VEC (deferred_access_check,gc)* checks,
16315 int declares_class_or_enum;
16316 tree decl = NULL_TREE;
16317 cp_decl_specifier_seq decl_specifiers;
16318 bool function_definition_p = false;
16320 /* This function is only used when processing a template
16322 gcc_assert (innermost_scope_kind () == sk_template_parms
16323 || innermost_scope_kind () == sk_template_spec);
16325 /* Defer access checks until we know what is being declared. */
16326 push_deferring_access_checks (dk_deferred);
16328 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16330 cp_parser_decl_specifier_seq (parser,
16331 CP_PARSER_FLAGS_OPTIONAL,
16333 &declares_class_or_enum);
16335 *friend_p = cp_parser_friend_p (&decl_specifiers);
16337 /* There are no template typedefs. */
16338 if (decl_specifiers.specs[(int) ds_typedef])
16340 error ("template declaration of %qs", "typedef");
16341 decl = error_mark_node;
16344 /* Gather up the access checks that occurred the
16345 decl-specifier-seq. */
16346 stop_deferring_access_checks ();
16348 /* Check for the declaration of a template class. */
16349 if (declares_class_or_enum)
16351 if (cp_parser_declares_only_class_p (parser))
16353 decl = shadow_tag (&decl_specifiers);
16358 friend template <typename T> struct A<T>::B;
16361 A<T>::B will be represented by a TYPENAME_TYPE, and
16362 therefore not recognized by shadow_tag. */
16363 if (friend_p && *friend_p
16365 && decl_specifiers.type
16366 && TYPE_P (decl_specifiers.type))
16367 decl = decl_specifiers.type;
16369 if (decl && decl != error_mark_node)
16370 decl = TYPE_NAME (decl);
16372 decl = error_mark_node;
16374 /* Perform access checks for template parameters. */
16375 cp_parser_perform_template_parameter_access_checks (checks);
16378 /* If it's not a template class, try for a template function. If
16379 the next token is a `;', then this declaration does not declare
16380 anything. But, if there were errors in the decl-specifiers, then
16381 the error might well have come from an attempted class-specifier.
16382 In that case, there's no need to warn about a missing declarator. */
16384 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16385 || decl_specifiers.type != error_mark_node))
16386 decl = cp_parser_init_declarator (parser,
16389 /*function_definition_allowed_p=*/true,
16391 declares_class_or_enum,
16392 &function_definition_p);
16394 pop_deferring_access_checks ();
16396 /* Clear any current qualification; whatever comes next is the start
16397 of something new. */
16398 parser->scope = NULL_TREE;
16399 parser->qualifying_scope = NULL_TREE;
16400 parser->object_scope = NULL_TREE;
16401 /* Look for a trailing `;' after the declaration. */
16402 if (!function_definition_p
16403 && (decl == error_mark_node
16404 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16405 cp_parser_skip_to_end_of_block_or_statement (parser);
16410 /* Parse a cast-expression that is not the operand of a unary "&". */
16413 cp_parser_simple_cast_expression (cp_parser *parser)
16415 return cp_parser_cast_expression (parser, /*address_p=*/false,
16419 /* Parse a functional cast to TYPE. Returns an expression
16420 representing the cast. */
16423 cp_parser_functional_cast (cp_parser* parser, tree type)
16425 tree expression_list;
16429 = cp_parser_parenthesized_expression_list (parser, false,
16431 /*allow_expansion_p=*/true,
16432 /*non_constant_p=*/NULL);
16434 cast = build_functional_cast (type, expression_list);
16435 /* [expr.const]/1: In an integral constant expression "only type
16436 conversions to integral or enumeration type can be used". */
16437 if (TREE_CODE (type) == TYPE_DECL)
16438 type = TREE_TYPE (type);
16439 if (cast != error_mark_node
16440 && !cast_valid_in_integral_constant_expression_p (type)
16441 && (cp_parser_non_integral_constant_expression
16442 (parser, "a call to a constructor")))
16443 return error_mark_node;
16447 /* Save the tokens that make up the body of a member function defined
16448 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16449 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16450 specifiers applied to the declaration. Returns the FUNCTION_DECL
16451 for the member function. */
16454 cp_parser_save_member_function_body (cp_parser* parser,
16455 cp_decl_specifier_seq *decl_specifiers,
16456 cp_declarator *declarator,
16463 /* Create the function-declaration. */
16464 fn = start_method (decl_specifiers, declarator, attributes);
16465 /* If something went badly wrong, bail out now. */
16466 if (fn == error_mark_node)
16468 /* If there's a function-body, skip it. */
16469 if (cp_parser_token_starts_function_definition_p
16470 (cp_lexer_peek_token (parser->lexer)))
16471 cp_parser_skip_to_end_of_block_or_statement (parser);
16472 return error_mark_node;
16475 /* Remember it, if there default args to post process. */
16476 cp_parser_save_default_args (parser, fn);
16478 /* Save away the tokens that make up the body of the
16480 first = parser->lexer->next_token;
16481 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16482 /* Handle function try blocks. */
16483 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16484 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16485 last = parser->lexer->next_token;
16487 /* Save away the inline definition; we will process it when the
16488 class is complete. */
16489 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16490 DECL_PENDING_INLINE_P (fn) = 1;
16492 /* We need to know that this was defined in the class, so that
16493 friend templates are handled correctly. */
16494 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16496 /* We're done with the inline definition. */
16497 finish_method (fn);
16499 /* Add FN to the queue of functions to be parsed later. */
16500 TREE_VALUE (parser->unparsed_functions_queues)
16501 = tree_cons (NULL_TREE, fn,
16502 TREE_VALUE (parser->unparsed_functions_queues));
16507 /* Parse a template-argument-list, as well as the trailing ">" (but
16508 not the opening ">"). See cp_parser_template_argument_list for the
16512 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16516 tree saved_qualifying_scope;
16517 tree saved_object_scope;
16518 bool saved_greater_than_is_operator_p;
16519 bool saved_skip_evaluation;
16523 When parsing a template-id, the first non-nested `>' is taken as
16524 the end of the template-argument-list rather than a greater-than
16526 saved_greater_than_is_operator_p
16527 = parser->greater_than_is_operator_p;
16528 parser->greater_than_is_operator_p = false;
16529 /* Parsing the argument list may modify SCOPE, so we save it
16531 saved_scope = parser->scope;
16532 saved_qualifying_scope = parser->qualifying_scope;
16533 saved_object_scope = parser->object_scope;
16534 /* We need to evaluate the template arguments, even though this
16535 template-id may be nested within a "sizeof". */
16536 saved_skip_evaluation = skip_evaluation;
16537 skip_evaluation = false;
16538 /* Parse the template-argument-list itself. */
16539 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16540 arguments = NULL_TREE;
16542 arguments = cp_parser_template_argument_list (parser);
16543 /* Look for the `>' that ends the template-argument-list. If we find
16544 a '>>' instead, it's probably just a typo. */
16545 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16547 if (!saved_greater_than_is_operator_p)
16549 /* If we're in a nested template argument list, the '>>' has
16550 to be a typo for '> >'. We emit the error message, but we
16551 continue parsing and we push a '>' as next token, so that
16552 the argument list will be parsed correctly. Note that the
16553 global source location is still on the token before the
16554 '>>', so we need to say explicitly where we want it. */
16555 cp_token *token = cp_lexer_peek_token (parser->lexer);
16556 error ("%H%<>>%> should be %<> >%> "
16557 "within a nested template argument list",
16560 /* ??? Proper recovery should terminate two levels of
16561 template argument list here. */
16562 token->type = CPP_GREATER;
16566 /* If this is not a nested template argument list, the '>>'
16567 is a typo for '>'. Emit an error message and continue.
16568 Same deal about the token location, but here we can get it
16569 right by consuming the '>>' before issuing the diagnostic. */
16570 cp_lexer_consume_token (parser->lexer);
16571 error ("spurious %<>>%>, use %<>%> to terminate "
16572 "a template argument list");
16576 cp_parser_skip_to_end_of_template_parameter_list (parser);
16577 /* The `>' token might be a greater-than operator again now. */
16578 parser->greater_than_is_operator_p
16579 = saved_greater_than_is_operator_p;
16580 /* Restore the SAVED_SCOPE. */
16581 parser->scope = saved_scope;
16582 parser->qualifying_scope = saved_qualifying_scope;
16583 parser->object_scope = saved_object_scope;
16584 skip_evaluation = saved_skip_evaluation;
16589 /* MEMBER_FUNCTION is a member function, or a friend. If default
16590 arguments, or the body of the function have not yet been parsed,
16594 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16596 /* If this member is a template, get the underlying
16598 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16599 member_function = DECL_TEMPLATE_RESULT (member_function);
16601 /* There should not be any class definitions in progress at this
16602 point; the bodies of members are only parsed outside of all class
16604 gcc_assert (parser->num_classes_being_defined == 0);
16605 /* While we're parsing the member functions we might encounter more
16606 classes. We want to handle them right away, but we don't want
16607 them getting mixed up with functions that are currently in the
16609 parser->unparsed_functions_queues
16610 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16612 /* Make sure that any template parameters are in scope. */
16613 maybe_begin_member_template_processing (member_function);
16615 /* If the body of the function has not yet been parsed, parse it
16617 if (DECL_PENDING_INLINE_P (member_function))
16619 tree function_scope;
16620 cp_token_cache *tokens;
16622 /* The function is no longer pending; we are processing it. */
16623 tokens = DECL_PENDING_INLINE_INFO (member_function);
16624 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16625 DECL_PENDING_INLINE_P (member_function) = 0;
16627 /* If this is a local class, enter the scope of the containing
16629 function_scope = current_function_decl;
16630 if (function_scope)
16631 push_function_context_to (function_scope);
16634 /* Push the body of the function onto the lexer stack. */
16635 cp_parser_push_lexer_for_tokens (parser, tokens);
16637 /* Let the front end know that we going to be defining this
16639 start_preparsed_function (member_function, NULL_TREE,
16640 SF_PRE_PARSED | SF_INCLASS_INLINE);
16642 /* Don't do access checking if it is a templated function. */
16643 if (processing_template_decl)
16644 push_deferring_access_checks (dk_no_check);
16646 /* Now, parse the body of the function. */
16647 cp_parser_function_definition_after_declarator (parser,
16648 /*inline_p=*/true);
16650 if (processing_template_decl)
16651 pop_deferring_access_checks ();
16653 /* Leave the scope of the containing function. */
16654 if (function_scope)
16655 pop_function_context_from (function_scope);
16656 cp_parser_pop_lexer (parser);
16659 /* Remove any template parameters from the symbol table. */
16660 maybe_end_member_template_processing ();
16662 /* Restore the queue. */
16663 parser->unparsed_functions_queues
16664 = TREE_CHAIN (parser->unparsed_functions_queues);
16667 /* If DECL contains any default args, remember it on the unparsed
16668 functions queue. */
16671 cp_parser_save_default_args (cp_parser* parser, tree decl)
16675 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16677 probe = TREE_CHAIN (probe))
16678 if (TREE_PURPOSE (probe))
16680 TREE_PURPOSE (parser->unparsed_functions_queues)
16681 = tree_cons (current_class_type, decl,
16682 TREE_PURPOSE (parser->unparsed_functions_queues));
16687 /* FN is a FUNCTION_DECL which may contains a parameter with an
16688 unparsed DEFAULT_ARG. Parse the default args now. This function
16689 assumes that the current scope is the scope in which the default
16690 argument should be processed. */
16693 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16695 bool saved_local_variables_forbidden_p;
16698 /* While we're parsing the default args, we might (due to the
16699 statement expression extension) encounter more classes. We want
16700 to handle them right away, but we don't want them getting mixed
16701 up with default args that are currently in the queue. */
16702 parser->unparsed_functions_queues
16703 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16705 /* Local variable names (and the `this' keyword) may not appear
16706 in a default argument. */
16707 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16708 parser->local_variables_forbidden_p = true;
16710 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16712 parm = TREE_CHAIN (parm))
16714 cp_token_cache *tokens;
16715 tree default_arg = TREE_PURPOSE (parm);
16717 VEC(tree,gc) *insts;
16724 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16725 /* This can happen for a friend declaration for a function
16726 already declared with default arguments. */
16729 /* Push the saved tokens for the default argument onto the parser's
16731 tokens = DEFARG_TOKENS (default_arg);
16732 cp_parser_push_lexer_for_tokens (parser, tokens);
16734 /* Parse the assignment-expression. */
16735 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16737 if (!processing_template_decl)
16738 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16740 TREE_PURPOSE (parm) = parsed_arg;
16742 /* Update any instantiations we've already created. */
16743 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16744 VEC_iterate (tree, insts, ix, copy); ix++)
16745 TREE_PURPOSE (copy) = parsed_arg;
16747 /* If the token stream has not been completely used up, then
16748 there was extra junk after the end of the default
16750 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16751 cp_parser_error (parser, "expected %<,%>");
16753 /* Revert to the main lexer. */
16754 cp_parser_pop_lexer (parser);
16757 /* Make sure no default arg is missing. */
16758 check_default_args (fn);
16760 /* Restore the state of local_variables_forbidden_p. */
16761 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16763 /* Restore the queue. */
16764 parser->unparsed_functions_queues
16765 = TREE_CHAIN (parser->unparsed_functions_queues);
16768 /* Parse the operand of `sizeof' (or a similar operator). Returns
16769 either a TYPE or an expression, depending on the form of the
16770 input. The KEYWORD indicates which kind of expression we have
16774 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16776 static const char *format;
16777 tree expr = NULL_TREE;
16778 const char *saved_message;
16779 bool saved_integral_constant_expression_p;
16780 bool saved_non_integral_constant_expression_p;
16781 bool pack_expansion_p = false;
16783 /* Initialize FORMAT the first time we get here. */
16785 format = "types may not be defined in '%s' expressions";
16787 /* Types cannot be defined in a `sizeof' expression. Save away the
16789 saved_message = parser->type_definition_forbidden_message;
16790 /* And create the new one. */
16791 parser->type_definition_forbidden_message
16792 = XNEWVEC (const char, strlen (format)
16793 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16795 sprintf ((char *) parser->type_definition_forbidden_message,
16796 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16798 /* The restrictions on constant-expressions do not apply inside
16799 sizeof expressions. */
16800 saved_integral_constant_expression_p
16801 = parser->integral_constant_expression_p;
16802 saved_non_integral_constant_expression_p
16803 = parser->non_integral_constant_expression_p;
16804 parser->integral_constant_expression_p = false;
16806 /* If it's a `...', then we are computing the length of a parameter
16808 if (keyword == RID_SIZEOF
16809 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16811 /* Consume the `...'. */
16812 cp_lexer_consume_token (parser->lexer);
16813 maybe_warn_variadic_templates ();
16815 /* Note that this is an expansion. */
16816 pack_expansion_p = true;
16819 /* Do not actually evaluate the expression. */
16821 /* If it's a `(', then we might be looking at the type-id
16823 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16826 bool saved_in_type_id_in_expr_p;
16828 /* We can't be sure yet whether we're looking at a type-id or an
16830 cp_parser_parse_tentatively (parser);
16831 /* Consume the `('. */
16832 cp_lexer_consume_token (parser->lexer);
16833 /* Parse the type-id. */
16834 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16835 parser->in_type_id_in_expr_p = true;
16836 type = cp_parser_type_id (parser);
16837 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16838 /* Now, look for the trailing `)'. */
16839 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16840 /* If all went well, then we're done. */
16841 if (cp_parser_parse_definitely (parser))
16843 cp_decl_specifier_seq decl_specs;
16845 /* Build a trivial decl-specifier-seq. */
16846 clear_decl_specs (&decl_specs);
16847 decl_specs.type = type;
16849 /* Call grokdeclarator to figure out what type this is. */
16850 expr = grokdeclarator (NULL,
16854 /*attrlist=*/NULL);
16858 /* If the type-id production did not work out, then we must be
16859 looking at the unary-expression production. */
16861 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16864 if (pack_expansion_p)
16865 /* Build a pack expansion. */
16866 expr = make_pack_expansion (expr);
16868 /* Go back to evaluating expressions. */
16871 /* Free the message we created. */
16872 free ((char *) parser->type_definition_forbidden_message);
16873 /* And restore the old one. */
16874 parser->type_definition_forbidden_message = saved_message;
16875 parser->integral_constant_expression_p
16876 = saved_integral_constant_expression_p;
16877 parser->non_integral_constant_expression_p
16878 = saved_non_integral_constant_expression_p;
16883 /* If the current declaration has no declarator, return true. */
16886 cp_parser_declares_only_class_p (cp_parser *parser)
16888 /* If the next token is a `;' or a `,' then there is no
16890 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16891 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16894 /* Update the DECL_SPECS to reflect the storage class indicated by
16898 cp_parser_set_storage_class (cp_parser *parser,
16899 cp_decl_specifier_seq *decl_specs,
16902 cp_storage_class storage_class;
16904 if (parser->in_unbraced_linkage_specification_p)
16906 error ("invalid use of %qD in linkage specification",
16907 ridpointers[keyword]);
16910 else if (decl_specs->storage_class != sc_none)
16912 decl_specs->conflicting_specifiers_p = true;
16916 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16917 && decl_specs->specs[(int) ds_thread])
16919 error ("%<__thread%> before %qD", ridpointers[keyword]);
16920 decl_specs->specs[(int) ds_thread] = 0;
16926 storage_class = sc_auto;
16929 storage_class = sc_register;
16932 storage_class = sc_static;
16935 storage_class = sc_extern;
16938 storage_class = sc_mutable;
16941 gcc_unreachable ();
16943 decl_specs->storage_class = storage_class;
16945 /* A storage class specifier cannot be applied alongside a typedef
16946 specifier. If there is a typedef specifier present then set
16947 conflicting_specifiers_p which will trigger an error later
16948 on in grokdeclarator. */
16949 if (decl_specs->specs[(int)ds_typedef])
16950 decl_specs->conflicting_specifiers_p = true;
16953 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16954 is true, the type is a user-defined type; otherwise it is a
16955 built-in type specified by a keyword. */
16958 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16960 bool user_defined_p)
16962 decl_specs->any_specifiers_p = true;
16964 /* If the user tries to redeclare bool or wchar_t (with, for
16965 example, in "typedef int wchar_t;") we remember that this is what
16966 happened. In system headers, we ignore these declarations so
16967 that G++ can work with system headers that are not C++-safe. */
16968 if (decl_specs->specs[(int) ds_typedef]
16970 && (type_spec == boolean_type_node
16971 || type_spec == wchar_type_node)
16972 && (decl_specs->type
16973 || decl_specs->specs[(int) ds_long]
16974 || decl_specs->specs[(int) ds_short]
16975 || decl_specs->specs[(int) ds_unsigned]
16976 || decl_specs->specs[(int) ds_signed]))
16978 decl_specs->redefined_builtin_type = type_spec;
16979 if (!decl_specs->type)
16981 decl_specs->type = type_spec;
16982 decl_specs->user_defined_type_p = false;
16985 else if (decl_specs->type)
16986 decl_specs->multiple_types_p = true;
16989 decl_specs->type = type_spec;
16990 decl_specs->user_defined_type_p = user_defined_p;
16991 decl_specs->redefined_builtin_type = NULL_TREE;
16995 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16996 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16999 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17001 return decl_specifiers->specs[(int) ds_friend] != 0;
17004 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17005 issue an error message indicating that TOKEN_DESC was expected.
17007 Returns the token consumed, if the token had the appropriate type.
17008 Otherwise, returns NULL. */
17011 cp_parser_require (cp_parser* parser,
17012 enum cpp_ttype type,
17013 const char* token_desc)
17015 if (cp_lexer_next_token_is (parser->lexer, type))
17016 return cp_lexer_consume_token (parser->lexer);
17019 /* Output the MESSAGE -- unless we're parsing tentatively. */
17020 if (!cp_parser_simulate_error (parser))
17022 char *message = concat ("expected ", token_desc, NULL);
17023 cp_parser_error (parser, message);
17030 /* An error message is produced if the next token is not '>'.
17031 All further tokens are skipped until the desired token is
17032 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17035 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17037 /* Current level of '< ... >'. */
17038 unsigned level = 0;
17039 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17040 unsigned nesting_depth = 0;
17042 /* Are we ready, yet? If not, issue error message. */
17043 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17046 /* Skip tokens until the desired token is found. */
17049 /* Peek at the next token. */
17050 switch (cp_lexer_peek_token (parser->lexer)->type)
17053 if (!nesting_depth)
17058 if (!nesting_depth && level-- == 0)
17060 /* We've reached the token we want, consume it and stop. */
17061 cp_lexer_consume_token (parser->lexer);
17066 case CPP_OPEN_PAREN:
17067 case CPP_OPEN_SQUARE:
17071 case CPP_CLOSE_PAREN:
17072 case CPP_CLOSE_SQUARE:
17073 if (nesting_depth-- == 0)
17078 case CPP_PRAGMA_EOL:
17079 case CPP_SEMICOLON:
17080 case CPP_OPEN_BRACE:
17081 case CPP_CLOSE_BRACE:
17082 /* The '>' was probably forgotten, don't look further. */
17089 /* Consume this token. */
17090 cp_lexer_consume_token (parser->lexer);
17094 /* If the next token is the indicated keyword, consume it. Otherwise,
17095 issue an error message indicating that TOKEN_DESC was expected.
17097 Returns the token consumed, if the token had the appropriate type.
17098 Otherwise, returns NULL. */
17101 cp_parser_require_keyword (cp_parser* parser,
17103 const char* token_desc)
17105 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17107 if (token && token->keyword != keyword)
17109 dyn_string_t error_msg;
17111 /* Format the error message. */
17112 error_msg = dyn_string_new (0);
17113 dyn_string_append_cstr (error_msg, "expected ");
17114 dyn_string_append_cstr (error_msg, token_desc);
17115 cp_parser_error (parser, error_msg->s);
17116 dyn_string_delete (error_msg);
17123 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17124 function-definition. */
17127 cp_parser_token_starts_function_definition_p (cp_token* token)
17129 return (/* An ordinary function-body begins with an `{'. */
17130 token->type == CPP_OPEN_BRACE
17131 /* A ctor-initializer begins with a `:'. */
17132 || token->type == CPP_COLON
17133 /* A function-try-block begins with `try'. */
17134 || token->keyword == RID_TRY
17135 /* The named return value extension begins with `return'. */
17136 || token->keyword == RID_RETURN);
17139 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17143 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17147 token = cp_lexer_peek_token (parser->lexer);
17148 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17151 /* Returns TRUE iff the next token is the "," or ">" ending a
17152 template-argument. */
17155 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17159 token = cp_lexer_peek_token (parser->lexer);
17160 return (token->type == CPP_COMMA
17161 || token->type == CPP_GREATER
17162 || token->type == CPP_ELLIPSIS);
17165 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17166 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17169 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17174 token = cp_lexer_peek_nth_token (parser->lexer, n);
17175 if (token->type == CPP_LESS)
17177 /* Check for the sequence `<::' in the original code. It would be lexed as
17178 `[:', where `[' is a digraph, and there is no whitespace before
17180 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17183 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17184 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17190 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17191 or none_type otherwise. */
17193 static enum tag_types
17194 cp_parser_token_is_class_key (cp_token* token)
17196 switch (token->keyword)
17201 return record_type;
17210 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17213 cp_parser_check_class_key (enum tag_types class_key, tree type)
17215 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17216 pedwarn ("%qs tag used in naming %q#T",
17217 class_key == union_type ? "union"
17218 : class_key == record_type ? "struct" : "class",
17222 /* Issue an error message if DECL is redeclared with different
17223 access than its original declaration [class.access.spec/3].
17224 This applies to nested classes and nested class templates.
17228 cp_parser_check_access_in_redeclaration (tree decl)
17230 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17233 if ((TREE_PRIVATE (decl)
17234 != (current_access_specifier == access_private_node))
17235 || (TREE_PROTECTED (decl)
17236 != (current_access_specifier == access_protected_node)))
17237 error ("%qD redeclared with different access", decl);
17240 /* Look for the `template' keyword, as a syntactic disambiguator.
17241 Return TRUE iff it is present, in which case it will be
17245 cp_parser_optional_template_keyword (cp_parser *parser)
17247 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17249 /* The `template' keyword can only be used within templates;
17250 outside templates the parser can always figure out what is a
17251 template and what is not. */
17252 if (!processing_template_decl)
17254 error ("%<template%> (as a disambiguator) is only allowed "
17255 "within templates");
17256 /* If this part of the token stream is rescanned, the same
17257 error message would be generated. So, we purge the token
17258 from the stream. */
17259 cp_lexer_purge_token (parser->lexer);
17264 /* Consume the `template' keyword. */
17265 cp_lexer_consume_token (parser->lexer);
17273 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17274 set PARSER->SCOPE, and perform other related actions. */
17277 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17280 struct tree_check *check_value;
17281 deferred_access_check *chk;
17282 VEC (deferred_access_check,gc) *checks;
17284 /* Get the stored value. */
17285 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17286 /* Perform any access checks that were deferred. */
17287 checks = check_value->checks;
17291 VEC_iterate (deferred_access_check, checks, i, chk) ;
17294 perform_or_defer_access_check (chk->binfo,
17299 /* Set the scope from the stored value. */
17300 parser->scope = check_value->value;
17301 parser->qualifying_scope = check_value->qualifying_scope;
17302 parser->object_scope = NULL_TREE;
17305 /* Consume tokens up through a non-nested END token. */
17308 cp_parser_cache_group (cp_parser *parser,
17309 enum cpp_ttype end,
17316 /* Abort a parenthesized expression if we encounter a brace. */
17317 if ((end == CPP_CLOSE_PAREN || depth == 0)
17318 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17320 /* If we've reached the end of the file, stop. */
17321 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17322 || (end != CPP_PRAGMA_EOL
17323 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17325 /* Consume the next token. */
17326 token = cp_lexer_consume_token (parser->lexer);
17327 /* See if it starts a new group. */
17328 if (token->type == CPP_OPEN_BRACE)
17330 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17334 else if (token->type == CPP_OPEN_PAREN)
17335 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17336 else if (token->type == CPP_PRAGMA)
17337 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17338 else if (token->type == end)
17343 /* Begin parsing tentatively. We always save tokens while parsing
17344 tentatively so that if the tentative parsing fails we can restore the
17348 cp_parser_parse_tentatively (cp_parser* parser)
17350 /* Enter a new parsing context. */
17351 parser->context = cp_parser_context_new (parser->context);
17352 /* Begin saving tokens. */
17353 cp_lexer_save_tokens (parser->lexer);
17354 /* In order to avoid repetitive access control error messages,
17355 access checks are queued up until we are no longer parsing
17357 push_deferring_access_checks (dk_deferred);
17360 /* Commit to the currently active tentative parse. */
17363 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17365 cp_parser_context *context;
17368 /* Mark all of the levels as committed. */
17369 lexer = parser->lexer;
17370 for (context = parser->context; context->next; context = context->next)
17372 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17374 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17375 while (!cp_lexer_saving_tokens (lexer))
17376 lexer = lexer->next;
17377 cp_lexer_commit_tokens (lexer);
17381 /* Abort the currently active tentative parse. All consumed tokens
17382 will be rolled back, and no diagnostics will be issued. */
17385 cp_parser_abort_tentative_parse (cp_parser* parser)
17387 cp_parser_simulate_error (parser);
17388 /* Now, pretend that we want to see if the construct was
17389 successfully parsed. */
17390 cp_parser_parse_definitely (parser);
17393 /* Stop parsing tentatively. If a parse error has occurred, restore the
17394 token stream. Otherwise, commit to the tokens we have consumed.
17395 Returns true if no error occurred; false otherwise. */
17398 cp_parser_parse_definitely (cp_parser* parser)
17400 bool error_occurred;
17401 cp_parser_context *context;
17403 /* Remember whether or not an error occurred, since we are about to
17404 destroy that information. */
17405 error_occurred = cp_parser_error_occurred (parser);
17406 /* Remove the topmost context from the stack. */
17407 context = parser->context;
17408 parser->context = context->next;
17409 /* If no parse errors occurred, commit to the tentative parse. */
17410 if (!error_occurred)
17412 /* Commit to the tokens read tentatively, unless that was
17414 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17415 cp_lexer_commit_tokens (parser->lexer);
17417 pop_to_parent_deferring_access_checks ();
17419 /* Otherwise, if errors occurred, roll back our state so that things
17420 are just as they were before we began the tentative parse. */
17423 cp_lexer_rollback_tokens (parser->lexer);
17424 pop_deferring_access_checks ();
17426 /* Add the context to the front of the free list. */
17427 context->next = cp_parser_context_free_list;
17428 cp_parser_context_free_list = context;
17430 return !error_occurred;
17433 /* Returns true if we are parsing tentatively and are not committed to
17434 this tentative parse. */
17437 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17439 return (cp_parser_parsing_tentatively (parser)
17440 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17443 /* Returns nonzero iff an error has occurred during the most recent
17444 tentative parse. */
17447 cp_parser_error_occurred (cp_parser* parser)
17449 return (cp_parser_parsing_tentatively (parser)
17450 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17453 /* Returns nonzero if GNU extensions are allowed. */
17456 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17458 return parser->allow_gnu_extensions_p;
17461 /* Objective-C++ Productions */
17464 /* Parse an Objective-C expression, which feeds into a primary-expression
17468 objc-message-expression
17469 objc-string-literal
17470 objc-encode-expression
17471 objc-protocol-expression
17472 objc-selector-expression
17474 Returns a tree representation of the expression. */
17477 cp_parser_objc_expression (cp_parser* parser)
17479 /* Try to figure out what kind of declaration is present. */
17480 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17484 case CPP_OPEN_SQUARE:
17485 return cp_parser_objc_message_expression (parser);
17487 case CPP_OBJC_STRING:
17488 kwd = cp_lexer_consume_token (parser->lexer);
17489 return objc_build_string_object (kwd->u.value);
17492 switch (kwd->keyword)
17494 case RID_AT_ENCODE:
17495 return cp_parser_objc_encode_expression (parser);
17497 case RID_AT_PROTOCOL:
17498 return cp_parser_objc_protocol_expression (parser);
17500 case RID_AT_SELECTOR:
17501 return cp_parser_objc_selector_expression (parser);
17507 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17508 cp_parser_skip_to_end_of_block_or_statement (parser);
17511 return error_mark_node;
17514 /* Parse an Objective-C message expression.
17516 objc-message-expression:
17517 [ objc-message-receiver objc-message-args ]
17519 Returns a representation of an Objective-C message. */
17522 cp_parser_objc_message_expression (cp_parser* parser)
17524 tree receiver, messageargs;
17526 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17527 receiver = cp_parser_objc_message_receiver (parser);
17528 messageargs = cp_parser_objc_message_args (parser);
17529 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17531 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17534 /* Parse an objc-message-receiver.
17536 objc-message-receiver:
17538 simple-type-specifier
17540 Returns a representation of the type or expression. */
17543 cp_parser_objc_message_receiver (cp_parser* parser)
17547 /* An Objective-C message receiver may be either (1) a type
17548 or (2) an expression. */
17549 cp_parser_parse_tentatively (parser);
17550 rcv = cp_parser_expression (parser, false);
17552 if (cp_parser_parse_definitely (parser))
17555 rcv = cp_parser_simple_type_specifier (parser,
17556 /*decl_specs=*/NULL,
17557 CP_PARSER_FLAGS_NONE);
17559 return objc_get_class_reference (rcv);
17562 /* Parse the arguments and selectors comprising an Objective-C message.
17567 objc-selector-args , objc-comma-args
17569 objc-selector-args:
17570 objc-selector [opt] : assignment-expression
17571 objc-selector-args objc-selector [opt] : assignment-expression
17574 assignment-expression
17575 objc-comma-args , assignment-expression
17577 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17578 selector arguments and TREE_VALUE containing a list of comma
17582 cp_parser_objc_message_args (cp_parser* parser)
17584 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17585 bool maybe_unary_selector_p = true;
17586 cp_token *token = cp_lexer_peek_token (parser->lexer);
17588 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17590 tree selector = NULL_TREE, arg;
17592 if (token->type != CPP_COLON)
17593 selector = cp_parser_objc_selector (parser);
17595 /* Detect if we have a unary selector. */
17596 if (maybe_unary_selector_p
17597 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17598 return build_tree_list (selector, NULL_TREE);
17600 maybe_unary_selector_p = false;
17601 cp_parser_require (parser, CPP_COLON, "`:'");
17602 arg = cp_parser_assignment_expression (parser, false);
17605 = chainon (sel_args,
17606 build_tree_list (selector, arg));
17608 token = cp_lexer_peek_token (parser->lexer);
17611 /* Handle non-selector arguments, if any. */
17612 while (token->type == CPP_COMMA)
17616 cp_lexer_consume_token (parser->lexer);
17617 arg = cp_parser_assignment_expression (parser, false);
17620 = chainon (addl_args,
17621 build_tree_list (NULL_TREE, arg));
17623 token = cp_lexer_peek_token (parser->lexer);
17626 return build_tree_list (sel_args, addl_args);
17629 /* Parse an Objective-C encode expression.
17631 objc-encode-expression:
17632 @encode objc-typename
17634 Returns an encoded representation of the type argument. */
17637 cp_parser_objc_encode_expression (cp_parser* parser)
17641 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17642 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17643 type = complete_type (cp_parser_type_id (parser));
17644 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17648 error ("%<@encode%> must specify a type as an argument");
17649 return error_mark_node;
17652 return objc_build_encode_expr (type);
17655 /* Parse an Objective-C @defs expression. */
17658 cp_parser_objc_defs_expression (cp_parser *parser)
17662 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17663 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17664 name = cp_parser_identifier (parser);
17665 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17667 return objc_get_class_ivars (name);
17670 /* Parse an Objective-C protocol expression.
17672 objc-protocol-expression:
17673 @protocol ( identifier )
17675 Returns a representation of the protocol expression. */
17678 cp_parser_objc_protocol_expression (cp_parser* parser)
17682 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17683 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17684 proto = cp_parser_identifier (parser);
17685 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17687 return objc_build_protocol_expr (proto);
17690 /* Parse an Objective-C selector expression.
17692 objc-selector-expression:
17693 @selector ( objc-method-signature )
17695 objc-method-signature:
17701 objc-selector-seq objc-selector :
17703 Returns a representation of the method selector. */
17706 cp_parser_objc_selector_expression (cp_parser* parser)
17708 tree sel_seq = NULL_TREE;
17709 bool maybe_unary_selector_p = true;
17712 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17713 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17714 token = cp_lexer_peek_token (parser->lexer);
17716 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17717 || token->type == CPP_SCOPE)
17719 tree selector = NULL_TREE;
17721 if (token->type != CPP_COLON
17722 || token->type == CPP_SCOPE)
17723 selector = cp_parser_objc_selector (parser);
17725 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17726 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17728 /* Detect if we have a unary selector. */
17729 if (maybe_unary_selector_p)
17731 sel_seq = selector;
17732 goto finish_selector;
17736 cp_parser_error (parser, "expected %<:%>");
17739 maybe_unary_selector_p = false;
17740 token = cp_lexer_consume_token (parser->lexer);
17742 if (token->type == CPP_SCOPE)
17745 = chainon (sel_seq,
17746 build_tree_list (selector, NULL_TREE));
17748 = chainon (sel_seq,
17749 build_tree_list (NULL_TREE, NULL_TREE));
17753 = chainon (sel_seq,
17754 build_tree_list (selector, NULL_TREE));
17756 token = cp_lexer_peek_token (parser->lexer);
17760 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17762 return objc_build_selector_expr (sel_seq);
17765 /* Parse a list of identifiers.
17767 objc-identifier-list:
17769 objc-identifier-list , identifier
17771 Returns a TREE_LIST of identifier nodes. */
17774 cp_parser_objc_identifier_list (cp_parser* parser)
17776 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17777 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17779 while (sep->type == CPP_COMMA)
17781 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17782 list = chainon (list,
17783 build_tree_list (NULL_TREE,
17784 cp_parser_identifier (parser)));
17785 sep = cp_lexer_peek_token (parser->lexer);
17791 /* Parse an Objective-C alias declaration.
17793 objc-alias-declaration:
17794 @compatibility_alias identifier identifier ;
17796 This function registers the alias mapping with the Objective-C front end.
17797 It returns nothing. */
17800 cp_parser_objc_alias_declaration (cp_parser* parser)
17804 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17805 alias = cp_parser_identifier (parser);
17806 orig = cp_parser_identifier (parser);
17807 objc_declare_alias (alias, orig);
17808 cp_parser_consume_semicolon_at_end_of_statement (parser);
17811 /* Parse an Objective-C class forward-declaration.
17813 objc-class-declaration:
17814 @class objc-identifier-list ;
17816 The function registers the forward declarations with the Objective-C
17817 front end. It returns nothing. */
17820 cp_parser_objc_class_declaration (cp_parser* parser)
17822 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17823 objc_declare_class (cp_parser_objc_identifier_list (parser));
17824 cp_parser_consume_semicolon_at_end_of_statement (parser);
17827 /* Parse a list of Objective-C protocol references.
17829 objc-protocol-refs-opt:
17830 objc-protocol-refs [opt]
17832 objc-protocol-refs:
17833 < objc-identifier-list >
17835 Returns a TREE_LIST of identifiers, if any. */
17838 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17840 tree protorefs = NULL_TREE;
17842 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17844 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17845 protorefs = cp_parser_objc_identifier_list (parser);
17846 cp_parser_require (parser, CPP_GREATER, "`>'");
17852 /* Parse a Objective-C visibility specification. */
17855 cp_parser_objc_visibility_spec (cp_parser* parser)
17857 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17859 switch (vis->keyword)
17861 case RID_AT_PRIVATE:
17862 objc_set_visibility (2);
17864 case RID_AT_PROTECTED:
17865 objc_set_visibility (0);
17867 case RID_AT_PUBLIC:
17868 objc_set_visibility (1);
17874 /* Eat '@private'/'@protected'/'@public'. */
17875 cp_lexer_consume_token (parser->lexer);
17878 /* Parse an Objective-C method type. */
17881 cp_parser_objc_method_type (cp_parser* parser)
17883 objc_set_method_type
17884 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17889 /* Parse an Objective-C protocol qualifier. */
17892 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17894 tree quals = NULL_TREE, node;
17895 cp_token *token = cp_lexer_peek_token (parser->lexer);
17897 node = token->u.value;
17899 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17900 && (node == ridpointers [(int) RID_IN]
17901 || node == ridpointers [(int) RID_OUT]
17902 || node == ridpointers [(int) RID_INOUT]
17903 || node == ridpointers [(int) RID_BYCOPY]
17904 || node == ridpointers [(int) RID_BYREF]
17905 || node == ridpointers [(int) RID_ONEWAY]))
17907 quals = tree_cons (NULL_TREE, node, quals);
17908 cp_lexer_consume_token (parser->lexer);
17909 token = cp_lexer_peek_token (parser->lexer);
17910 node = token->u.value;
17916 /* Parse an Objective-C typename. */
17919 cp_parser_objc_typename (cp_parser* parser)
17921 tree typename = NULL_TREE;
17923 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17925 tree proto_quals, cp_type = NULL_TREE;
17927 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17928 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17930 /* An ObjC type name may consist of just protocol qualifiers, in which
17931 case the type shall default to 'id'. */
17932 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17933 cp_type = cp_parser_type_id (parser);
17935 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17936 typename = build_tree_list (proto_quals, cp_type);
17942 /* Check to see if TYPE refers to an Objective-C selector name. */
17945 cp_parser_objc_selector_p (enum cpp_ttype type)
17947 return (type == CPP_NAME || type == CPP_KEYWORD
17948 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17949 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17950 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17951 || type == CPP_XOR || type == CPP_XOR_EQ);
17954 /* Parse an Objective-C selector. */
17957 cp_parser_objc_selector (cp_parser* parser)
17959 cp_token *token = cp_lexer_consume_token (parser->lexer);
17961 if (!cp_parser_objc_selector_p (token->type))
17963 error ("invalid Objective-C++ selector name");
17964 return error_mark_node;
17967 /* C++ operator names are allowed to appear in ObjC selectors. */
17968 switch (token->type)
17970 case CPP_AND_AND: return get_identifier ("and");
17971 case CPP_AND_EQ: return get_identifier ("and_eq");
17972 case CPP_AND: return get_identifier ("bitand");
17973 case CPP_OR: return get_identifier ("bitor");
17974 case CPP_COMPL: return get_identifier ("compl");
17975 case CPP_NOT: return get_identifier ("not");
17976 case CPP_NOT_EQ: return get_identifier ("not_eq");
17977 case CPP_OR_OR: return get_identifier ("or");
17978 case CPP_OR_EQ: return get_identifier ("or_eq");
17979 case CPP_XOR: return get_identifier ("xor");
17980 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17981 default: return token->u.value;
17985 /* Parse an Objective-C params list. */
17988 cp_parser_objc_method_keyword_params (cp_parser* parser)
17990 tree params = NULL_TREE;
17991 bool maybe_unary_selector_p = true;
17992 cp_token *token = cp_lexer_peek_token (parser->lexer);
17994 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17996 tree selector = NULL_TREE, typename, identifier;
17998 if (token->type != CPP_COLON)
17999 selector = cp_parser_objc_selector (parser);
18001 /* Detect if we have a unary selector. */
18002 if (maybe_unary_selector_p
18003 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18006 maybe_unary_selector_p = false;
18007 cp_parser_require (parser, CPP_COLON, "`:'");
18008 typename = cp_parser_objc_typename (parser);
18009 identifier = cp_parser_identifier (parser);
18013 objc_build_keyword_decl (selector,
18017 token = cp_lexer_peek_token (parser->lexer);
18023 /* Parse the non-keyword Objective-C params. */
18026 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18028 tree params = make_node (TREE_LIST);
18029 cp_token *token = cp_lexer_peek_token (parser->lexer);
18030 *ellipsisp = false; /* Initially, assume no ellipsis. */
18032 while (token->type == CPP_COMMA)
18034 cp_parameter_declarator *parmdecl;
18037 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18038 token = cp_lexer_peek_token (parser->lexer);
18040 if (token->type == CPP_ELLIPSIS)
18042 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18047 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18048 parm = grokdeclarator (parmdecl->declarator,
18049 &parmdecl->decl_specifiers,
18050 PARM, /*initialized=*/0,
18051 /*attrlist=*/NULL);
18053 chainon (params, build_tree_list (NULL_TREE, parm));
18054 token = cp_lexer_peek_token (parser->lexer);
18060 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18063 cp_parser_objc_interstitial_code (cp_parser* parser)
18065 cp_token *token = cp_lexer_peek_token (parser->lexer);
18067 /* If the next token is `extern' and the following token is a string
18068 literal, then we have a linkage specification. */
18069 if (token->keyword == RID_EXTERN
18070 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18071 cp_parser_linkage_specification (parser);
18072 /* Handle #pragma, if any. */
18073 else if (token->type == CPP_PRAGMA)
18074 cp_parser_pragma (parser, pragma_external);
18075 /* Allow stray semicolons. */
18076 else if (token->type == CPP_SEMICOLON)
18077 cp_lexer_consume_token (parser->lexer);
18078 /* Finally, try to parse a block-declaration, or a function-definition. */
18080 cp_parser_block_declaration (parser, /*statement_p=*/false);
18083 /* Parse a method signature. */
18086 cp_parser_objc_method_signature (cp_parser* parser)
18088 tree rettype, kwdparms, optparms;
18089 bool ellipsis = false;
18091 cp_parser_objc_method_type (parser);
18092 rettype = cp_parser_objc_typename (parser);
18093 kwdparms = cp_parser_objc_method_keyword_params (parser);
18094 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18096 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18099 /* Pars an Objective-C method prototype list. */
18102 cp_parser_objc_method_prototype_list (cp_parser* parser)
18104 cp_token *token = cp_lexer_peek_token (parser->lexer);
18106 while (token->keyword != RID_AT_END)
18108 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18110 objc_add_method_declaration
18111 (cp_parser_objc_method_signature (parser));
18112 cp_parser_consume_semicolon_at_end_of_statement (parser);
18115 /* Allow for interspersed non-ObjC++ code. */
18116 cp_parser_objc_interstitial_code (parser);
18118 token = cp_lexer_peek_token (parser->lexer);
18121 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18122 objc_finish_interface ();
18125 /* Parse an Objective-C method definition list. */
18128 cp_parser_objc_method_definition_list (cp_parser* parser)
18130 cp_token *token = cp_lexer_peek_token (parser->lexer);
18132 while (token->keyword != RID_AT_END)
18136 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18138 push_deferring_access_checks (dk_deferred);
18139 objc_start_method_definition
18140 (cp_parser_objc_method_signature (parser));
18142 /* For historical reasons, we accept an optional semicolon. */
18143 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18144 cp_lexer_consume_token (parser->lexer);
18146 perform_deferred_access_checks ();
18147 stop_deferring_access_checks ();
18148 meth = cp_parser_function_definition_after_declarator (parser,
18150 pop_deferring_access_checks ();
18151 objc_finish_method_definition (meth);
18154 /* Allow for interspersed non-ObjC++ code. */
18155 cp_parser_objc_interstitial_code (parser);
18157 token = cp_lexer_peek_token (parser->lexer);
18160 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18161 objc_finish_implementation ();
18164 /* Parse Objective-C ivars. */
18167 cp_parser_objc_class_ivars (cp_parser* parser)
18169 cp_token *token = cp_lexer_peek_token (parser->lexer);
18171 if (token->type != CPP_OPEN_BRACE)
18172 return; /* No ivars specified. */
18174 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18175 token = cp_lexer_peek_token (parser->lexer);
18177 while (token->type != CPP_CLOSE_BRACE)
18179 cp_decl_specifier_seq declspecs;
18180 int decl_class_or_enum_p;
18181 tree prefix_attributes;
18183 cp_parser_objc_visibility_spec (parser);
18185 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18188 cp_parser_decl_specifier_seq (parser,
18189 CP_PARSER_FLAGS_OPTIONAL,
18191 &decl_class_or_enum_p);
18192 prefix_attributes = declspecs.attributes;
18193 declspecs.attributes = NULL_TREE;
18195 /* Keep going until we hit the `;' at the end of the
18197 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18199 tree width = NULL_TREE, attributes, first_attribute, decl;
18200 cp_declarator *declarator = NULL;
18201 int ctor_dtor_or_conv_p;
18203 /* Check for a (possibly unnamed) bitfield declaration. */
18204 token = cp_lexer_peek_token (parser->lexer);
18205 if (token->type == CPP_COLON)
18208 if (token->type == CPP_NAME
18209 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18212 /* Get the name of the bitfield. */
18213 declarator = make_id_declarator (NULL_TREE,
18214 cp_parser_identifier (parser),
18218 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18219 /* Get the width of the bitfield. */
18221 = cp_parser_constant_expression (parser,
18222 /*allow_non_constant=*/false,
18227 /* Parse the declarator. */
18229 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18230 &ctor_dtor_or_conv_p,
18231 /*parenthesized_p=*/NULL,
18232 /*member_p=*/false);
18235 /* Look for attributes that apply to the ivar. */
18236 attributes = cp_parser_attributes_opt (parser);
18237 /* Remember which attributes are prefix attributes and
18239 first_attribute = attributes;
18240 /* Combine the attributes. */
18241 attributes = chainon (prefix_attributes, attributes);
18245 /* Create the bitfield declaration. */
18246 decl = grokbitfield (declarator, &declspecs, width);
18247 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18250 decl = grokfield (declarator, &declspecs,
18251 NULL_TREE, /*init_const_expr_p=*/false,
18252 NULL_TREE, attributes);
18254 /* Add the instance variable. */
18255 objc_add_instance_variable (decl);
18257 /* Reset PREFIX_ATTRIBUTES. */
18258 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18259 attributes = TREE_CHAIN (attributes);
18261 TREE_CHAIN (attributes) = NULL_TREE;
18263 token = cp_lexer_peek_token (parser->lexer);
18265 if (token->type == CPP_COMMA)
18267 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18273 cp_parser_consume_semicolon_at_end_of_statement (parser);
18274 token = cp_lexer_peek_token (parser->lexer);
18277 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18278 /* For historical reasons, we accept an optional semicolon. */
18279 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18280 cp_lexer_consume_token (parser->lexer);
18283 /* Parse an Objective-C protocol declaration. */
18286 cp_parser_objc_protocol_declaration (cp_parser* parser)
18288 tree proto, protorefs;
18291 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18294 error ("identifier expected after %<@protocol%>");
18298 /* See if we have a forward declaration or a definition. */
18299 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18301 /* Try a forward declaration first. */
18302 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18304 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18306 cp_parser_consume_semicolon_at_end_of_statement (parser);
18309 /* Ok, we got a full-fledged definition (or at least should). */
18312 proto = cp_parser_identifier (parser);
18313 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18314 objc_start_protocol (proto, protorefs);
18315 cp_parser_objc_method_prototype_list (parser);
18319 /* Parse an Objective-C superclass or category. */
18322 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18325 cp_token *next = cp_lexer_peek_token (parser->lexer);
18327 *super = *categ = NULL_TREE;
18328 if (next->type == CPP_COLON)
18330 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18331 *super = cp_parser_identifier (parser);
18333 else if (next->type == CPP_OPEN_PAREN)
18335 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18336 *categ = cp_parser_identifier (parser);
18337 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18341 /* Parse an Objective-C class interface. */
18344 cp_parser_objc_class_interface (cp_parser* parser)
18346 tree name, super, categ, protos;
18348 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18349 name = cp_parser_identifier (parser);
18350 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18351 protos = cp_parser_objc_protocol_refs_opt (parser);
18353 /* We have either a class or a category on our hands. */
18355 objc_start_category_interface (name, categ, protos);
18358 objc_start_class_interface (name, super, protos);
18359 /* Handle instance variable declarations, if any. */
18360 cp_parser_objc_class_ivars (parser);
18361 objc_continue_interface ();
18364 cp_parser_objc_method_prototype_list (parser);
18367 /* Parse an Objective-C class implementation. */
18370 cp_parser_objc_class_implementation (cp_parser* parser)
18372 tree name, super, categ;
18374 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18375 name = cp_parser_identifier (parser);
18376 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18378 /* We have either a class or a category on our hands. */
18380 objc_start_category_implementation (name, categ);
18383 objc_start_class_implementation (name, super);
18384 /* Handle instance variable declarations, if any. */
18385 cp_parser_objc_class_ivars (parser);
18386 objc_continue_implementation ();
18389 cp_parser_objc_method_definition_list (parser);
18392 /* Consume the @end token and finish off the implementation. */
18395 cp_parser_objc_end_implementation (cp_parser* parser)
18397 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18398 objc_finish_implementation ();
18401 /* Parse an Objective-C declaration. */
18404 cp_parser_objc_declaration (cp_parser* parser)
18406 /* Try to figure out what kind of declaration is present. */
18407 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18409 switch (kwd->keyword)
18412 cp_parser_objc_alias_declaration (parser);
18415 cp_parser_objc_class_declaration (parser);
18417 case RID_AT_PROTOCOL:
18418 cp_parser_objc_protocol_declaration (parser);
18420 case RID_AT_INTERFACE:
18421 cp_parser_objc_class_interface (parser);
18423 case RID_AT_IMPLEMENTATION:
18424 cp_parser_objc_class_implementation (parser);
18427 cp_parser_objc_end_implementation (parser);
18430 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18431 cp_parser_skip_to_end_of_block_or_statement (parser);
18435 /* Parse an Objective-C try-catch-finally statement.
18437 objc-try-catch-finally-stmt:
18438 @try compound-statement objc-catch-clause-seq [opt]
18439 objc-finally-clause [opt]
18441 objc-catch-clause-seq:
18442 objc-catch-clause objc-catch-clause-seq [opt]
18445 @catch ( exception-declaration ) compound-statement
18447 objc-finally-clause
18448 @finally compound-statement
18450 Returns NULL_TREE. */
18453 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18454 location_t location;
18457 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18458 location = cp_lexer_peek_token (parser->lexer)->location;
18459 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18460 node, lest it get absorbed into the surrounding block. */
18461 stmt = push_stmt_list ();
18462 cp_parser_compound_statement (parser, NULL, false);
18463 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18465 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18467 cp_parameter_declarator *parmdecl;
18470 cp_lexer_consume_token (parser->lexer);
18471 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18472 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18473 parm = grokdeclarator (parmdecl->declarator,
18474 &parmdecl->decl_specifiers,
18475 PARM, /*initialized=*/0,
18476 /*attrlist=*/NULL);
18477 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18478 objc_begin_catch_clause (parm);
18479 cp_parser_compound_statement (parser, NULL, false);
18480 objc_finish_catch_clause ();
18483 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18485 cp_lexer_consume_token (parser->lexer);
18486 location = cp_lexer_peek_token (parser->lexer)->location;
18487 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18488 node, lest it get absorbed into the surrounding block. */
18489 stmt = push_stmt_list ();
18490 cp_parser_compound_statement (parser, NULL, false);
18491 objc_build_finally_clause (location, pop_stmt_list (stmt));
18494 return objc_finish_try_stmt ();
18497 /* Parse an Objective-C synchronized statement.
18499 objc-synchronized-stmt:
18500 @synchronized ( expression ) compound-statement
18502 Returns NULL_TREE. */
18505 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18506 location_t location;
18509 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18511 location = cp_lexer_peek_token (parser->lexer)->location;
18512 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18513 lock = cp_parser_expression (parser, false);
18514 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18516 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18517 node, lest it get absorbed into the surrounding block. */
18518 stmt = push_stmt_list ();
18519 cp_parser_compound_statement (parser, NULL, false);
18521 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18524 /* Parse an Objective-C throw statement.
18527 @throw assignment-expression [opt] ;
18529 Returns a constructed '@throw' statement. */
18532 cp_parser_objc_throw_statement (cp_parser *parser) {
18533 tree expr = NULL_TREE;
18535 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18537 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18538 expr = cp_parser_assignment_expression (parser, false);
18540 cp_parser_consume_semicolon_at_end_of_statement (parser);
18542 return objc_build_throw_stmt (expr);
18545 /* Parse an Objective-C statement. */
18548 cp_parser_objc_statement (cp_parser * parser) {
18549 /* Try to figure out what kind of declaration is present. */
18550 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18552 switch (kwd->keyword)
18555 return cp_parser_objc_try_catch_finally_statement (parser);
18556 case RID_AT_SYNCHRONIZED:
18557 return cp_parser_objc_synchronized_statement (parser);
18559 return cp_parser_objc_throw_statement (parser);
18561 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18562 cp_parser_skip_to_end_of_block_or_statement (parser);
18565 return error_mark_node;
18568 /* OpenMP 2.5 parsing routines. */
18570 /* Returns name of the next clause.
18571 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18572 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18573 returned and the token is consumed. */
18575 static pragma_omp_clause
18576 cp_parser_omp_clause_name (cp_parser *parser)
18578 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18580 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18581 result = PRAGMA_OMP_CLAUSE_IF;
18582 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18583 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18584 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18585 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18586 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18588 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18589 const char *p = IDENTIFIER_POINTER (id);
18594 if (!strcmp ("copyin", p))
18595 result = PRAGMA_OMP_CLAUSE_COPYIN;
18596 else if (!strcmp ("copyprivate", p))
18597 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18600 if (!strcmp ("firstprivate", p))
18601 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18604 if (!strcmp ("lastprivate", p))
18605 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18608 if (!strcmp ("nowait", p))
18609 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18610 else if (!strcmp ("num_threads", p))
18611 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18614 if (!strcmp ("ordered", p))
18615 result = PRAGMA_OMP_CLAUSE_ORDERED;
18618 if (!strcmp ("reduction", p))
18619 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18622 if (!strcmp ("schedule", p))
18623 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18624 else if (!strcmp ("shared", p))
18625 result = PRAGMA_OMP_CLAUSE_SHARED;
18630 if (result != PRAGMA_OMP_CLAUSE_NONE)
18631 cp_lexer_consume_token (parser->lexer);
18636 /* Validate that a clause of the given type does not already exist. */
18639 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18643 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18644 if (OMP_CLAUSE_CODE (c) == code)
18646 error ("too many %qs clauses", name);
18654 variable-list , identifier
18656 In addition, we match a closing parenthesis. An opening parenthesis
18657 will have been consumed by the caller.
18659 If KIND is nonzero, create the appropriate node and install the decl
18660 in OMP_CLAUSE_DECL and add the node to the head of the list.
18662 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18663 return the list created. */
18666 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18673 name = cp_parser_id_expression (parser, /*template_p=*/false,
18674 /*check_dependency_p=*/true,
18675 /*template_p=*/NULL,
18676 /*declarator_p=*/false,
18677 /*optional_p=*/false);
18678 if (name == error_mark_node)
18681 decl = cp_parser_lookup_name_simple (parser, name);
18682 if (decl == error_mark_node)
18683 cp_parser_name_lookup_error (parser, name, decl, NULL);
18684 else if (kind != 0)
18686 tree u = build_omp_clause (kind);
18687 OMP_CLAUSE_DECL (u) = decl;
18688 OMP_CLAUSE_CHAIN (u) = list;
18692 list = tree_cons (decl, NULL_TREE, list);
18695 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18697 cp_lexer_consume_token (parser->lexer);
18700 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18704 /* Try to resync to an unnested comma. Copied from
18705 cp_parser_parenthesized_expression_list. */
18707 ending = cp_parser_skip_to_closing_parenthesis (parser,
18708 /*recovering=*/true,
18710 /*consume_paren=*/true);
18718 /* Similarly, but expect leading and trailing parenthesis. This is a very
18719 common case for omp clauses. */
18722 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18724 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18725 return cp_parser_omp_var_list_no_open (parser, kind, list);
18730 default ( shared | none ) */
18733 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18735 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18738 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18740 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18742 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18743 const char *p = IDENTIFIER_POINTER (id);
18748 if (strcmp ("none", p) != 0)
18750 kind = OMP_CLAUSE_DEFAULT_NONE;
18754 if (strcmp ("shared", p) != 0)
18756 kind = OMP_CLAUSE_DEFAULT_SHARED;
18763 cp_lexer_consume_token (parser->lexer);
18768 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18771 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18772 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18773 /*or_comma=*/false,
18774 /*consume_paren=*/true);
18776 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18779 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18780 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18781 OMP_CLAUSE_CHAIN (c) = list;
18782 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18788 if ( expression ) */
18791 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18795 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18798 t = cp_parser_condition (parser);
18800 if (t == error_mark_node
18801 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18802 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18803 /*or_comma=*/false,
18804 /*consume_paren=*/true);
18806 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18808 c = build_omp_clause (OMP_CLAUSE_IF);
18809 OMP_CLAUSE_IF_EXPR (c) = t;
18810 OMP_CLAUSE_CHAIN (c) = list;
18819 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18823 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18825 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18826 OMP_CLAUSE_CHAIN (c) = list;
18831 num_threads ( expression ) */
18834 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18838 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18841 t = cp_parser_expression (parser, false);
18843 if (t == error_mark_node
18844 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18845 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18846 /*or_comma=*/false,
18847 /*consume_paren=*/true);
18849 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18851 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18852 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18853 OMP_CLAUSE_CHAIN (c) = list;
18862 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18866 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18868 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18869 OMP_CLAUSE_CHAIN (c) = list;
18874 reduction ( reduction-operator : variable-list )
18876 reduction-operator:
18877 One of: + * - & ^ | && || */
18880 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18882 enum tree_code code;
18885 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18888 switch (cp_lexer_peek_token (parser->lexer)->type)
18900 code = BIT_AND_EXPR;
18903 code = BIT_XOR_EXPR;
18906 code = BIT_IOR_EXPR;
18909 code = TRUTH_ANDIF_EXPR;
18912 code = TRUTH_ORIF_EXPR;
18915 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18917 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18918 /*or_comma=*/false,
18919 /*consume_paren=*/true);
18922 cp_lexer_consume_token (parser->lexer);
18924 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18927 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18928 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18929 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18935 schedule ( schedule-kind )
18936 schedule ( schedule-kind , expression )
18939 static | dynamic | guided | runtime */
18942 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18946 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18949 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18951 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18953 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18954 const char *p = IDENTIFIER_POINTER (id);
18959 if (strcmp ("dynamic", p) != 0)
18961 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18965 if (strcmp ("guided", p) != 0)
18967 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18971 if (strcmp ("runtime", p) != 0)
18973 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18980 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18981 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18984 cp_lexer_consume_token (parser->lexer);
18986 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18988 cp_lexer_consume_token (parser->lexer);
18990 t = cp_parser_assignment_expression (parser, false);
18992 if (t == error_mark_node)
18994 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18995 error ("schedule %<runtime%> does not take "
18996 "a %<chunk_size%> parameter");
18998 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19000 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19003 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19006 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19007 OMP_CLAUSE_CHAIN (c) = list;
19011 cp_parser_error (parser, "invalid schedule kind");
19013 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19014 /*or_comma=*/false,
19015 /*consume_paren=*/true);
19019 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19020 is a bitmask in MASK. Return the list of clauses found; the result
19021 of clause default goes in *pdefault. */
19024 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19025 const char *where, cp_token *pragma_tok)
19027 tree clauses = NULL;
19029 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19031 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19032 const char *c_name;
19033 tree prev = clauses;
19037 case PRAGMA_OMP_CLAUSE_COPYIN:
19038 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19041 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19042 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19044 c_name = "copyprivate";
19046 case PRAGMA_OMP_CLAUSE_DEFAULT:
19047 clauses = cp_parser_omp_clause_default (parser, clauses);
19048 c_name = "default";
19050 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19051 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19053 c_name = "firstprivate";
19055 case PRAGMA_OMP_CLAUSE_IF:
19056 clauses = cp_parser_omp_clause_if (parser, clauses);
19059 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19060 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19062 c_name = "lastprivate";
19064 case PRAGMA_OMP_CLAUSE_NOWAIT:
19065 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19068 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19069 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19070 c_name = "num_threads";
19072 case PRAGMA_OMP_CLAUSE_ORDERED:
19073 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19074 c_name = "ordered";
19076 case PRAGMA_OMP_CLAUSE_PRIVATE:
19077 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19079 c_name = "private";
19081 case PRAGMA_OMP_CLAUSE_REDUCTION:
19082 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19083 c_name = "reduction";
19085 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19086 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19087 c_name = "schedule";
19089 case PRAGMA_OMP_CLAUSE_SHARED:
19090 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19095 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19099 if (((mask >> c_kind) & 1) == 0)
19101 /* Remove the invalid clause(s) from the list to avoid
19102 confusing the rest of the compiler. */
19104 error ("%qs is not valid for %qs", c_name, where);
19108 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19109 return finish_omp_clauses (clauses);
19116 In practice, we're also interested in adding the statement to an
19117 outer node. So it is convenient if we work around the fact that
19118 cp_parser_statement calls add_stmt. */
19121 cp_parser_begin_omp_structured_block (cp_parser *parser)
19123 unsigned save = parser->in_statement;
19125 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19126 This preserves the "not within loop or switch" style error messages
19127 for nonsense cases like
19133 if (parser->in_statement)
19134 parser->in_statement = IN_OMP_BLOCK;
19140 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19142 parser->in_statement = save;
19146 cp_parser_omp_structured_block (cp_parser *parser)
19148 tree stmt = begin_omp_structured_block ();
19149 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19151 cp_parser_statement (parser, NULL_TREE, false, NULL);
19153 cp_parser_end_omp_structured_block (parser, save);
19154 return finish_omp_structured_block (stmt);
19158 # pragma omp atomic new-line
19162 x binop= expr | x++ | ++x | x-- | --x
19164 +, *, -, /, &, ^, |, <<, >>
19166 where x is an lvalue expression with scalar type. */
19169 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19172 enum tree_code code;
19174 cp_parser_require_pragma_eol (parser, pragma_tok);
19176 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19178 switch (TREE_CODE (lhs))
19183 case PREINCREMENT_EXPR:
19184 case POSTINCREMENT_EXPR:
19185 lhs = TREE_OPERAND (lhs, 0);
19187 rhs = integer_one_node;
19190 case PREDECREMENT_EXPR:
19191 case POSTDECREMENT_EXPR:
19192 lhs = TREE_OPERAND (lhs, 0);
19194 rhs = integer_one_node;
19198 switch (cp_lexer_peek_token (parser->lexer)->type)
19204 code = TRUNC_DIV_EXPR;
19212 case CPP_LSHIFT_EQ:
19213 code = LSHIFT_EXPR;
19215 case CPP_RSHIFT_EQ:
19216 code = RSHIFT_EXPR;
19219 code = BIT_AND_EXPR;
19222 code = BIT_IOR_EXPR;
19225 code = BIT_XOR_EXPR;
19228 cp_parser_error (parser,
19229 "invalid operator for %<#pragma omp atomic%>");
19232 cp_lexer_consume_token (parser->lexer);
19234 rhs = cp_parser_expression (parser, false);
19235 if (rhs == error_mark_node)
19239 finish_omp_atomic (code, lhs, rhs);
19240 cp_parser_consume_semicolon_at_end_of_statement (parser);
19244 cp_parser_skip_to_end_of_block_or_statement (parser);
19249 # pragma omp barrier new-line */
19252 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19254 cp_parser_require_pragma_eol (parser, pragma_tok);
19255 finish_omp_barrier ();
19259 # pragma omp critical [(name)] new-line
19260 structured-block */
19263 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19265 tree stmt, name = NULL;
19267 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19269 cp_lexer_consume_token (parser->lexer);
19271 name = cp_parser_identifier (parser);
19273 if (name == error_mark_node
19274 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19275 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19276 /*or_comma=*/false,
19277 /*consume_paren=*/true);
19278 if (name == error_mark_node)
19281 cp_parser_require_pragma_eol (parser, pragma_tok);
19283 stmt = cp_parser_omp_structured_block (parser);
19284 return c_finish_omp_critical (stmt, name);
19288 # pragma omp flush flush-vars[opt] new-line
19291 ( variable-list ) */
19294 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19296 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19297 (void) cp_parser_omp_var_list (parser, 0, NULL);
19298 cp_parser_require_pragma_eol (parser, pragma_tok);
19300 finish_omp_flush ();
19303 /* Parse the restricted form of the for statment allowed by OpenMP. */
19306 cp_parser_omp_for_loop (cp_parser *parser)
19308 tree init, cond, incr, body, decl, pre_body;
19311 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19313 cp_parser_error (parser, "for statement expected");
19316 loc = cp_lexer_consume_token (parser->lexer)->location;
19317 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19320 init = decl = NULL;
19321 pre_body = push_stmt_list ();
19322 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19324 cp_decl_specifier_seq type_specifiers;
19326 /* First, try to parse as an initialized declaration. See
19327 cp_parser_condition, from whence the bulk of this is copied. */
19329 cp_parser_parse_tentatively (parser);
19330 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19332 if (!cp_parser_error_occurred (parser))
19334 tree asm_specification, attributes;
19335 cp_declarator *declarator;
19337 declarator = cp_parser_declarator (parser,
19338 CP_PARSER_DECLARATOR_NAMED,
19339 /*ctor_dtor_or_conv_p=*/NULL,
19340 /*parenthesized_p=*/NULL,
19341 /*member_p=*/false);
19342 attributes = cp_parser_attributes_opt (parser);
19343 asm_specification = cp_parser_asm_specification_opt (parser);
19345 cp_parser_require (parser, CPP_EQ, "`='");
19346 if (cp_parser_parse_definitely (parser))
19350 decl = start_decl (declarator, &type_specifiers,
19351 /*initialized_p=*/false, attributes,
19352 /*prefix_attributes=*/NULL_TREE,
19355 init = cp_parser_assignment_expression (parser, false);
19357 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19358 asm_specification, LOOKUP_ONLYCONVERTING);
19361 pop_scope (pushed_scope);
19365 cp_parser_abort_tentative_parse (parser);
19367 /* If parsing as an initialized declaration failed, try again as
19368 a simple expression. */
19370 init = cp_parser_expression (parser, false);
19372 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19373 pre_body = pop_stmt_list (pre_body);
19376 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19377 cond = cp_parser_condition (parser);
19378 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19381 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19382 incr = cp_parser_expression (parser, false);
19384 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19385 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19386 /*or_comma=*/false,
19387 /*consume_paren=*/true);
19389 /* Note that we saved the original contents of this flag when we entered
19390 the structured block, and so we don't need to re-save it here. */
19391 parser->in_statement = IN_OMP_FOR;
19393 /* Note that the grammar doesn't call for a structured block here,
19394 though the loop as a whole is a structured block. */
19395 body = push_stmt_list ();
19396 cp_parser_statement (parser, NULL_TREE, false, NULL);
19397 body = pop_stmt_list (body);
19399 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19403 #pragma omp for for-clause[optseq] new-line
19406 #define OMP_FOR_CLAUSE_MASK \
19407 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19408 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19409 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19410 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19411 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19412 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19413 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19416 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19418 tree clauses, sb, ret;
19421 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19422 "#pragma omp for", pragma_tok);
19424 sb = begin_omp_structured_block ();
19425 save = cp_parser_begin_omp_structured_block (parser);
19427 ret = cp_parser_omp_for_loop (parser);
19429 OMP_FOR_CLAUSES (ret) = clauses;
19431 cp_parser_end_omp_structured_block (parser, save);
19432 add_stmt (finish_omp_structured_block (sb));
19438 # pragma omp master new-line
19439 structured-block */
19442 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19444 cp_parser_require_pragma_eol (parser, pragma_tok);
19445 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19449 # pragma omp ordered new-line
19450 structured-block */
19453 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19455 cp_parser_require_pragma_eol (parser, pragma_tok);
19456 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19462 { section-sequence }
19465 section-directive[opt] structured-block
19466 section-sequence section-directive structured-block */
19469 cp_parser_omp_sections_scope (cp_parser *parser)
19471 tree stmt, substmt;
19472 bool error_suppress = false;
19475 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19478 stmt = push_stmt_list ();
19480 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19484 substmt = begin_omp_structured_block ();
19485 save = cp_parser_begin_omp_structured_block (parser);
19489 cp_parser_statement (parser, NULL_TREE, false, NULL);
19491 tok = cp_lexer_peek_token (parser->lexer);
19492 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19494 if (tok->type == CPP_CLOSE_BRACE)
19496 if (tok->type == CPP_EOF)
19500 cp_parser_end_omp_structured_block (parser, save);
19501 substmt = finish_omp_structured_block (substmt);
19502 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19503 add_stmt (substmt);
19508 tok = cp_lexer_peek_token (parser->lexer);
19509 if (tok->type == CPP_CLOSE_BRACE)
19511 if (tok->type == CPP_EOF)
19514 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19516 cp_lexer_consume_token (parser->lexer);
19517 cp_parser_require_pragma_eol (parser, tok);
19518 error_suppress = false;
19520 else if (!error_suppress)
19522 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19523 error_suppress = true;
19526 substmt = cp_parser_omp_structured_block (parser);
19527 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19528 add_stmt (substmt);
19530 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19532 substmt = pop_stmt_list (stmt);
19534 stmt = make_node (OMP_SECTIONS);
19535 TREE_TYPE (stmt) = void_type_node;
19536 OMP_SECTIONS_BODY (stmt) = substmt;
19543 # pragma omp sections sections-clause[optseq] newline
19546 #define OMP_SECTIONS_CLAUSE_MASK \
19547 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19548 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19549 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19550 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19551 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19554 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19558 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19559 "#pragma omp sections", pragma_tok);
19561 ret = cp_parser_omp_sections_scope (parser);
19563 OMP_SECTIONS_CLAUSES (ret) = clauses;
19569 # pragma parallel parallel-clause new-line
19570 # pragma parallel for parallel-for-clause new-line
19571 # pragma parallel sections parallel-sections-clause new-line */
19573 #define OMP_PARALLEL_CLAUSE_MASK \
19574 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19575 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19576 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19577 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19578 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19579 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19580 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19581 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19584 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19586 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19587 const char *p_name = "#pragma omp parallel";
19588 tree stmt, clauses, par_clause, ws_clause, block;
19589 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19592 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19594 cp_lexer_consume_token (parser->lexer);
19595 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19596 p_name = "#pragma omp parallel for";
19597 mask |= OMP_FOR_CLAUSE_MASK;
19598 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19600 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19602 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19603 const char *p = IDENTIFIER_POINTER (id);
19604 if (strcmp (p, "sections") == 0)
19606 cp_lexer_consume_token (parser->lexer);
19607 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19608 p_name = "#pragma omp parallel sections";
19609 mask |= OMP_SECTIONS_CLAUSE_MASK;
19610 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19614 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19615 block = begin_omp_parallel ();
19616 save = cp_parser_begin_omp_structured_block (parser);
19620 case PRAGMA_OMP_PARALLEL:
19621 cp_parser_already_scoped_statement (parser);
19622 par_clause = clauses;
19625 case PRAGMA_OMP_PARALLEL_FOR:
19626 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19627 stmt = cp_parser_omp_for_loop (parser);
19629 OMP_FOR_CLAUSES (stmt) = ws_clause;
19632 case PRAGMA_OMP_PARALLEL_SECTIONS:
19633 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19634 stmt = cp_parser_omp_sections_scope (parser);
19636 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19640 gcc_unreachable ();
19643 cp_parser_end_omp_structured_block (parser, save);
19644 stmt = finish_omp_parallel (par_clause, block);
19645 if (p_kind != PRAGMA_OMP_PARALLEL)
19646 OMP_PARALLEL_COMBINED (stmt) = 1;
19651 # pragma omp single single-clause[optseq] new-line
19652 structured-block */
19654 #define OMP_SINGLE_CLAUSE_MASK \
19655 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19656 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19657 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19658 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19661 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19663 tree stmt = make_node (OMP_SINGLE);
19664 TREE_TYPE (stmt) = void_type_node;
19666 OMP_SINGLE_CLAUSES (stmt)
19667 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19668 "#pragma omp single", pragma_tok);
19669 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19671 return add_stmt (stmt);
19675 # pragma omp threadprivate (variable-list) */
19678 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19682 vars = cp_parser_omp_var_list (parser, 0, NULL);
19683 cp_parser_require_pragma_eol (parser, pragma_tok);
19685 finish_omp_threadprivate (vars);
19688 /* Main entry point to OpenMP statement pragmas. */
19691 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19695 switch (pragma_tok->pragma_kind)
19697 case PRAGMA_OMP_ATOMIC:
19698 cp_parser_omp_atomic (parser, pragma_tok);
19700 case PRAGMA_OMP_CRITICAL:
19701 stmt = cp_parser_omp_critical (parser, pragma_tok);
19703 case PRAGMA_OMP_FOR:
19704 stmt = cp_parser_omp_for (parser, pragma_tok);
19706 case PRAGMA_OMP_MASTER:
19707 stmt = cp_parser_omp_master (parser, pragma_tok);
19709 case PRAGMA_OMP_ORDERED:
19710 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19712 case PRAGMA_OMP_PARALLEL:
19713 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19715 case PRAGMA_OMP_SECTIONS:
19716 stmt = cp_parser_omp_sections (parser, pragma_tok);
19718 case PRAGMA_OMP_SINGLE:
19719 stmt = cp_parser_omp_single (parser, pragma_tok);
19722 gcc_unreachable ();
19726 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19731 static GTY (()) cp_parser *the_parser;
19734 /* Special handling for the first token or line in the file. The first
19735 thing in the file might be #pragma GCC pch_preprocess, which loads a
19736 PCH file, which is a GC collection point. So we need to handle this
19737 first pragma without benefit of an existing lexer structure.
19739 Always returns one token to the caller in *FIRST_TOKEN. This is
19740 either the true first token of the file, or the first token after
19741 the initial pragma. */
19744 cp_parser_initial_pragma (cp_token *first_token)
19748 cp_lexer_get_preprocessor_token (NULL, first_token);
19749 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19752 cp_lexer_get_preprocessor_token (NULL, first_token);
19753 if (first_token->type == CPP_STRING)
19755 name = first_token->u.value;
19757 cp_lexer_get_preprocessor_token (NULL, first_token);
19758 if (first_token->type != CPP_PRAGMA_EOL)
19759 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19762 error ("expected string literal");
19764 /* Skip to the end of the pragma. */
19765 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19766 cp_lexer_get_preprocessor_token (NULL, first_token);
19768 /* Now actually load the PCH file. */
19770 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19772 /* Read one more token to return to our caller. We have to do this
19773 after reading the PCH file in, since its pointers have to be
19775 cp_lexer_get_preprocessor_token (NULL, first_token);
19778 /* Normal parsing of a pragma token. Here we can (and must) use the
19782 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19784 cp_token *pragma_tok;
19787 pragma_tok = cp_lexer_consume_token (parser->lexer);
19788 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19789 parser->lexer->in_pragma = true;
19791 id = pragma_tok->pragma_kind;
19794 case PRAGMA_GCC_PCH_PREPROCESS:
19795 error ("%<#pragma GCC pch_preprocess%> must be first");
19798 case PRAGMA_OMP_BARRIER:
19801 case pragma_compound:
19802 cp_parser_omp_barrier (parser, pragma_tok);
19805 error ("%<#pragma omp barrier%> may only be "
19806 "used in compound statements");
19813 case PRAGMA_OMP_FLUSH:
19816 case pragma_compound:
19817 cp_parser_omp_flush (parser, pragma_tok);
19820 error ("%<#pragma omp flush%> may only be "
19821 "used in compound statements");
19828 case PRAGMA_OMP_THREADPRIVATE:
19829 cp_parser_omp_threadprivate (parser, pragma_tok);
19832 case PRAGMA_OMP_ATOMIC:
19833 case PRAGMA_OMP_CRITICAL:
19834 case PRAGMA_OMP_FOR:
19835 case PRAGMA_OMP_MASTER:
19836 case PRAGMA_OMP_ORDERED:
19837 case PRAGMA_OMP_PARALLEL:
19838 case PRAGMA_OMP_SECTIONS:
19839 case PRAGMA_OMP_SINGLE:
19840 if (context == pragma_external)
19842 cp_parser_omp_construct (parser, pragma_tok);
19845 case PRAGMA_OMP_SECTION:
19846 error ("%<#pragma omp section%> may only be used in "
19847 "%<#pragma omp sections%> construct");
19851 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19852 c_invoke_pragma_handler (id);
19856 cp_parser_error (parser, "expected declaration specifiers");
19860 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19864 /* The interface the pragma parsers have to the lexer. */
19867 pragma_lex (tree *value)
19870 enum cpp_ttype ret;
19872 tok = cp_lexer_peek_token (the_parser->lexer);
19875 *value = tok->u.value;
19877 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19879 else if (ret == CPP_STRING)
19880 *value = cp_parser_string_literal (the_parser, false, false);
19883 cp_lexer_consume_token (the_parser->lexer);
19884 if (ret == CPP_KEYWORD)
19892 /* External interface. */
19894 /* Parse one entire translation unit. */
19897 c_parse_file (void)
19899 bool error_occurred;
19900 static bool already_called = false;
19902 if (already_called)
19904 sorry ("inter-module optimizations not implemented for C++");
19907 already_called = true;
19909 the_parser = cp_parser_new ();
19910 push_deferring_access_checks (flag_access_control
19911 ? dk_no_deferred : dk_no_check);
19912 error_occurred = cp_parser_translation_unit (the_parser);
19916 #include "gt-cp-parser.h"