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 ("%<%D::%D%> has not been declared",
2058 parser->scope, name);
2059 else if (parser->scope == global_namespace)
2060 error ("%<::%D%> has not been declared", name);
2061 else if (parser->object_scope
2062 && !CLASS_TYPE_P (parser->object_scope))
2063 error ("request for member %qD in non-class type %qT",
2064 name, parser->object_scope);
2065 else if (parser->object_scope)
2066 error ("%<%T::%D%> has not been declared",
2067 parser->object_scope, name);
2069 error ("%qD has not been declared", name);
2071 else if (parser->scope && parser->scope != global_namespace)
2072 error ("%<%D::%D%> %s", parser->scope, name, desired);
2073 else if (parser->scope == global_namespace)
2074 error ("%<::%D%> %s", name, desired);
2076 error ("%qD %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)))
2348 cp_parser_abort_tentative_parse (parser);
2351 if (!cp_parser_parse_definitely (parser) || TREE_CODE (id) == TYPE_DECL)
2354 /* Emit a diagnostic for the invalid type. */
2355 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2356 /* Skip to the end of the declaration; there's no point in
2357 trying to process it. */
2358 cp_parser_skip_to_end_of_block_or_statement (parser);
2362 /* Consume tokens up to, and including, the next non-nested closing `)'.
2363 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2364 are doing error recovery. Returns -1 if OR_COMMA is true and we
2365 found an unnested comma. */
2368 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2373 unsigned paren_depth = 0;
2374 unsigned brace_depth = 0;
2376 if (recovering && !or_comma
2377 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2382 cp_token * token = cp_lexer_peek_token (parser->lexer);
2384 switch (token->type)
2387 case CPP_PRAGMA_EOL:
2388 /* If we've run out of tokens, then there is no closing `)'. */
2392 /* This matches the processing in skip_to_end_of_statement. */
2397 case CPP_OPEN_BRACE:
2400 case CPP_CLOSE_BRACE:
2406 if (recovering && or_comma && !brace_depth && !paren_depth)
2410 case CPP_OPEN_PAREN:
2415 case CPP_CLOSE_PAREN:
2416 if (!brace_depth && !paren_depth--)
2419 cp_lexer_consume_token (parser->lexer);
2428 /* Consume the token. */
2429 cp_lexer_consume_token (parser->lexer);
2433 /* Consume tokens until we reach the end of the current statement.
2434 Normally, that will be just before consuming a `;'. However, if a
2435 non-nested `}' comes first, then we stop before consuming that. */
2438 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2440 unsigned nesting_depth = 0;
2444 cp_token *token = cp_lexer_peek_token (parser->lexer);
2446 switch (token->type)
2449 case CPP_PRAGMA_EOL:
2450 /* If we've run out of tokens, stop. */
2454 /* If the next token is a `;', we have reached the end of the
2460 case CPP_CLOSE_BRACE:
2461 /* If this is a non-nested '}', stop before consuming it.
2462 That way, when confronted with something like:
2466 we stop before consuming the closing '}', even though we
2467 have not yet reached a `;'. */
2468 if (nesting_depth == 0)
2471 /* If it is the closing '}' for a block that we have
2472 scanned, stop -- but only after consuming the token.
2478 we will stop after the body of the erroneously declared
2479 function, but before consuming the following `typedef'
2481 if (--nesting_depth == 0)
2483 cp_lexer_consume_token (parser->lexer);
2487 case CPP_OPEN_BRACE:
2495 /* Consume the token. */
2496 cp_lexer_consume_token (parser->lexer);
2500 /* This function is called at the end of a statement or declaration.
2501 If the next token is a semicolon, it is consumed; otherwise, error
2502 recovery is attempted. */
2505 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2507 /* Look for the trailing `;'. */
2508 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2510 /* If there is additional (erroneous) input, skip to the end of
2512 cp_parser_skip_to_end_of_statement (parser);
2513 /* If the next token is now a `;', consume it. */
2514 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2515 cp_lexer_consume_token (parser->lexer);
2519 /* Skip tokens until we have consumed an entire block, or until we
2520 have consumed a non-nested `;'. */
2523 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2525 int nesting_depth = 0;
2527 while (nesting_depth >= 0)
2529 cp_token *token = cp_lexer_peek_token (parser->lexer);
2531 switch (token->type)
2534 case CPP_PRAGMA_EOL:
2535 /* If we've run out of tokens, stop. */
2539 /* Stop if this is an unnested ';'. */
2544 case CPP_CLOSE_BRACE:
2545 /* Stop if this is an unnested '}', or closes the outermost
2552 case CPP_OPEN_BRACE:
2561 /* Consume the token. */
2562 cp_lexer_consume_token (parser->lexer);
2566 /* Skip tokens until a non-nested closing curly brace is the next
2570 cp_parser_skip_to_closing_brace (cp_parser *parser)
2572 unsigned nesting_depth = 0;
2576 cp_token *token = cp_lexer_peek_token (parser->lexer);
2578 switch (token->type)
2581 case CPP_PRAGMA_EOL:
2582 /* If we've run out of tokens, stop. */
2585 case CPP_CLOSE_BRACE:
2586 /* If the next token is a non-nested `}', then we have reached
2587 the end of the current block. */
2588 if (nesting_depth-- == 0)
2592 case CPP_OPEN_BRACE:
2593 /* If it the next token is a `{', then we are entering a new
2594 block. Consume the entire block. */
2602 /* Consume the token. */
2603 cp_lexer_consume_token (parser->lexer);
2607 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2608 parameter is the PRAGMA token, allowing us to purge the entire pragma
2612 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2616 parser->lexer->in_pragma = false;
2619 token = cp_lexer_consume_token (parser->lexer);
2620 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2622 /* Ensure that the pragma is not parsed again. */
2623 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2626 /* Require pragma end of line, resyncing with it as necessary. The
2627 arguments are as for cp_parser_skip_to_pragma_eol. */
2630 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2632 parser->lexer->in_pragma = false;
2633 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2634 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2637 /* This is a simple wrapper around make_typename_type. When the id is
2638 an unresolved identifier node, we can provide a superior diagnostic
2639 using cp_parser_diagnose_invalid_type_name. */
2642 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2645 if (TREE_CODE (id) == IDENTIFIER_NODE)
2647 result = make_typename_type (scope, id, typename_type,
2648 /*complain=*/tf_none);
2649 if (result == error_mark_node)
2650 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2653 return make_typename_type (scope, id, typename_type, tf_error);
2657 /* Create a new C++ parser. */
2660 cp_parser_new (void)
2666 /* cp_lexer_new_main is called before calling ggc_alloc because
2667 cp_lexer_new_main might load a PCH file. */
2668 lexer = cp_lexer_new_main ();
2670 /* Initialize the binops_by_token so that we can get the tree
2671 directly from the token. */
2672 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2673 binops_by_token[binops[i].token_type] = binops[i];
2675 parser = GGC_CNEW (cp_parser);
2676 parser->lexer = lexer;
2677 parser->context = cp_parser_context_new (NULL);
2679 /* For now, we always accept GNU extensions. */
2680 parser->allow_gnu_extensions_p = 1;
2682 /* The `>' token is a greater-than operator, not the end of a
2684 parser->greater_than_is_operator_p = true;
2686 parser->default_arg_ok_p = true;
2688 /* We are not parsing a constant-expression. */
2689 parser->integral_constant_expression_p = false;
2690 parser->allow_non_integral_constant_expression_p = false;
2691 parser->non_integral_constant_expression_p = false;
2693 /* Local variable names are not forbidden. */
2694 parser->local_variables_forbidden_p = false;
2696 /* We are not processing an `extern "C"' declaration. */
2697 parser->in_unbraced_linkage_specification_p = false;
2699 /* We are not processing a declarator. */
2700 parser->in_declarator_p = false;
2702 /* We are not processing a template-argument-list. */
2703 parser->in_template_argument_list_p = false;
2705 /* We are not in an iteration statement. */
2706 parser->in_statement = 0;
2708 /* We are not in a switch statement. */
2709 parser->in_switch_statement_p = false;
2711 /* We are not parsing a type-id inside an expression. */
2712 parser->in_type_id_in_expr_p = false;
2714 /* Declarations aren't implicitly extern "C". */
2715 parser->implicit_extern_c = false;
2717 /* String literals should be translated to the execution character set. */
2718 parser->translate_strings_p = true;
2720 /* We are not parsing a function body. */
2721 parser->in_function_body = false;
2723 /* The unparsed function queue is empty. */
2724 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2726 /* There are no classes being defined. */
2727 parser->num_classes_being_defined = 0;
2729 /* No template parameters apply. */
2730 parser->num_template_parameter_lists = 0;
2735 /* Create a cp_lexer structure which will emit the tokens in CACHE
2736 and push it onto the parser's lexer stack. This is used for delayed
2737 parsing of in-class method bodies and default arguments, and should
2738 not be confused with tentative parsing. */
2740 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2742 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2743 lexer->next = parser->lexer;
2744 parser->lexer = lexer;
2746 /* Move the current source position to that of the first token in the
2748 cp_lexer_set_source_position_from_token (lexer->next_token);
2751 /* Pop the top lexer off the parser stack. This is never used for the
2752 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2754 cp_parser_pop_lexer (cp_parser *parser)
2756 cp_lexer *lexer = parser->lexer;
2757 parser->lexer = lexer->next;
2758 cp_lexer_destroy (lexer);
2760 /* Put the current source position back where it was before this
2761 lexer was pushed. */
2762 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2765 /* Lexical conventions [gram.lex] */
2767 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2771 cp_parser_identifier (cp_parser* parser)
2775 /* Look for the identifier. */
2776 token = cp_parser_require (parser, CPP_NAME, "identifier");
2777 /* Return the value. */
2778 return token ? token->u.value : error_mark_node;
2781 /* Parse a sequence of adjacent string constants. Returns a
2782 TREE_STRING representing the combined, nul-terminated string
2783 constant. If TRANSLATE is true, translate the string to the
2784 execution character set. If WIDE_OK is true, a wide string is
2787 C++98 [lex.string] says that if a narrow string literal token is
2788 adjacent to a wide string literal token, the behavior is undefined.
2789 However, C99 6.4.5p4 says that this results in a wide string literal.
2790 We follow C99 here, for consistency with the C front end.
2792 This code is largely lifted from lex_string() in c-lex.c.
2794 FUTURE: ObjC++ will need to handle @-strings here. */
2796 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2801 struct obstack str_ob;
2802 cpp_string str, istr, *strs;
2805 tok = cp_lexer_peek_token (parser->lexer);
2806 if (!cp_parser_is_string_literal (tok))
2808 cp_parser_error (parser, "expected string-literal");
2809 return error_mark_node;
2812 /* Try to avoid the overhead of creating and destroying an obstack
2813 for the common case of just one string. */
2814 if (!cp_parser_is_string_literal
2815 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2817 cp_lexer_consume_token (parser->lexer);
2819 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2820 str.len = TREE_STRING_LENGTH (tok->u.value);
2822 if (tok->type == CPP_WSTRING)
2829 gcc_obstack_init (&str_ob);
2834 cp_lexer_consume_token (parser->lexer);
2836 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2837 str.len = TREE_STRING_LENGTH (tok->u.value);
2838 if (tok->type == CPP_WSTRING)
2841 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2843 tok = cp_lexer_peek_token (parser->lexer);
2845 while (cp_parser_is_string_literal (tok));
2847 strs = (cpp_string *) obstack_finish (&str_ob);
2850 if (wide && !wide_ok)
2852 cp_parser_error (parser, "a wide string is invalid in this context");
2856 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2857 (parse_in, strs, count, &istr, wide))
2859 value = build_string (istr.len, (char *)istr.text);
2860 free ((void *)istr.text);
2862 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2863 value = fix_string_type (value);
2866 /* cpp_interpret_string has issued an error. */
2867 value = error_mark_node;
2870 obstack_free (&str_ob, 0);
2876 /* Basic concepts [gram.basic] */
2878 /* Parse a translation-unit.
2881 declaration-seq [opt]
2883 Returns TRUE if all went well. */
2886 cp_parser_translation_unit (cp_parser* parser)
2888 /* The address of the first non-permanent object on the declarator
2890 static void *declarator_obstack_base;
2894 /* Create the declarator obstack, if necessary. */
2895 if (!cp_error_declarator)
2897 gcc_obstack_init (&declarator_obstack);
2898 /* Create the error declarator. */
2899 cp_error_declarator = make_declarator (cdk_error);
2900 /* Create the empty parameter list. */
2901 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2902 /* Remember where the base of the declarator obstack lies. */
2903 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2906 cp_parser_declaration_seq_opt (parser);
2908 /* If there are no tokens left then all went well. */
2909 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2911 /* Get rid of the token array; we don't need it any more. */
2912 cp_lexer_destroy (parser->lexer);
2913 parser->lexer = NULL;
2915 /* This file might have been a context that's implicitly extern
2916 "C". If so, pop the lang context. (Only relevant for PCH.) */
2917 if (parser->implicit_extern_c)
2919 pop_lang_context ();
2920 parser->implicit_extern_c = false;
2924 finish_translation_unit ();
2930 cp_parser_error (parser, "expected declaration");
2934 /* Make sure the declarator obstack was fully cleaned up. */
2935 gcc_assert (obstack_next_free (&declarator_obstack)
2936 == declarator_obstack_base);
2938 /* All went well. */
2942 /* Expressions [gram.expr] */
2944 /* Parse a primary-expression.
2955 ( compound-statement )
2956 __builtin_va_arg ( assignment-expression , type-id )
2957 __builtin_offsetof ( type-id , offsetof-expression )
2959 Objective-C++ Extension:
2967 ADDRESS_P is true iff this expression was immediately preceded by
2968 "&" and therefore might denote a pointer-to-member. CAST_P is true
2969 iff this expression is the target of a cast. TEMPLATE_ARG_P is
2970 true iff this expression is a template argument.
2972 Returns a representation of the expression. Upon return, *IDK
2973 indicates what kind of id-expression (if any) was present. */
2976 cp_parser_primary_expression (cp_parser *parser,
2979 bool template_arg_p,
2984 /* Assume the primary expression is not an id-expression. */
2985 *idk = CP_ID_KIND_NONE;
2987 /* Peek at the next token. */
2988 token = cp_lexer_peek_token (parser->lexer);
2989 switch (token->type)
3000 token = cp_lexer_consume_token (parser->lexer);
3001 /* Floating-point literals are only allowed in an integral
3002 constant expression if they are cast to an integral or
3003 enumeration type. */
3004 if (TREE_CODE (token->u.value) == REAL_CST
3005 && parser->integral_constant_expression_p
3008 /* CAST_P will be set even in invalid code like "int(2.7 +
3009 ...)". Therefore, we have to check that the next token
3010 is sure to end the cast. */
3013 cp_token *next_token;
3015 next_token = cp_lexer_peek_token (parser->lexer);
3016 if (/* The comma at the end of an
3017 enumerator-definition. */
3018 next_token->type != CPP_COMMA
3019 /* The curly brace at the end of an enum-specifier. */
3020 && next_token->type != CPP_CLOSE_BRACE
3021 /* The end of a statement. */
3022 && next_token->type != CPP_SEMICOLON
3023 /* The end of the cast-expression. */
3024 && next_token->type != CPP_CLOSE_PAREN
3025 /* The end of an array bound. */
3026 && next_token->type != CPP_CLOSE_SQUARE
3027 /* The closing ">" in a template-argument-list. */
3028 && (next_token->type != CPP_GREATER
3029 || parser->greater_than_is_operator_p))
3033 /* If we are within a cast, then the constraint that the
3034 cast is to an integral or enumeration type will be
3035 checked at that point. If we are not within a cast, then
3036 this code is invalid. */
3038 cp_parser_non_integral_constant_expression
3039 (parser, "floating-point literal");
3041 return token->u.value;
3045 /* ??? Should wide strings be allowed when parser->translate_strings_p
3046 is false (i.e. in attributes)? If not, we can kill the third
3047 argument to cp_parser_string_literal. */
3048 return cp_parser_string_literal (parser,
3049 parser->translate_strings_p,
3052 case CPP_OPEN_PAREN:
3055 bool saved_greater_than_is_operator_p;
3057 /* Consume the `('. */
3058 cp_lexer_consume_token (parser->lexer);
3059 /* Within a parenthesized expression, a `>' token is always
3060 the greater-than operator. */
3061 saved_greater_than_is_operator_p
3062 = parser->greater_than_is_operator_p;
3063 parser->greater_than_is_operator_p = true;
3064 /* If we see `( { ' then we are looking at the beginning of
3065 a GNU statement-expression. */
3066 if (cp_parser_allow_gnu_extensions_p (parser)
3067 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3069 /* Statement-expressions are not allowed by the standard. */
3071 pedwarn ("ISO C++ forbids braced-groups within expressions");
3073 /* And they're not allowed outside of a function-body; you
3074 cannot, for example, write:
3076 int i = ({ int j = 3; j + 1; });
3078 at class or namespace scope. */
3079 if (!parser->in_function_body)
3081 error ("statement-expressions are allowed only inside functions");
3082 cp_parser_skip_to_end_of_block_or_statement (parser);
3083 expr = error_mark_node;
3087 /* Start the statement-expression. */
3088 expr = begin_stmt_expr ();
3089 /* Parse the compound-statement. */
3090 cp_parser_compound_statement (parser, expr, false);
3092 expr = finish_stmt_expr (expr, false);
3097 /* Parse the parenthesized expression. */
3098 expr = cp_parser_expression (parser, cast_p);
3099 /* Let the front end know that this expression was
3100 enclosed in parentheses. This matters in case, for
3101 example, the expression is of the form `A::B', since
3102 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3104 finish_parenthesized_expr (expr);
3106 /* The `>' token might be the end of a template-id or
3107 template-parameter-list now. */
3108 parser->greater_than_is_operator_p
3109 = saved_greater_than_is_operator_p;
3110 /* Consume the `)'. */
3111 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3112 cp_parser_skip_to_end_of_statement (parser);
3118 switch (token->keyword)
3120 /* These two are the boolean literals. */
3122 cp_lexer_consume_token (parser->lexer);
3123 return boolean_true_node;
3125 cp_lexer_consume_token (parser->lexer);
3126 return boolean_false_node;
3128 /* The `__null' literal. */
3130 cp_lexer_consume_token (parser->lexer);
3133 /* Recognize the `this' keyword. */
3135 cp_lexer_consume_token (parser->lexer);
3136 if (parser->local_variables_forbidden_p)
3138 error ("%<this%> may not be used in this context");
3139 return error_mark_node;
3141 /* Pointers cannot appear in constant-expressions. */
3142 if (cp_parser_non_integral_constant_expression (parser,
3144 return error_mark_node;
3145 return finish_this_expr ();
3147 /* The `operator' keyword can be the beginning of an
3152 case RID_FUNCTION_NAME:
3153 case RID_PRETTY_FUNCTION_NAME:
3154 case RID_C99_FUNCTION_NAME:
3155 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3156 __func__ are the names of variables -- but they are
3157 treated specially. Therefore, they are handled here,
3158 rather than relying on the generic id-expression logic
3159 below. Grammatically, these names are id-expressions.
3161 Consume the token. */
3162 token = cp_lexer_consume_token (parser->lexer);
3163 /* Look up the name. */
3164 return finish_fname (token->u.value);
3171 /* The `__builtin_va_arg' construct is used to handle
3172 `va_arg'. Consume the `__builtin_va_arg' token. */
3173 cp_lexer_consume_token (parser->lexer);
3174 /* Look for the opening `('. */
3175 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3176 /* Now, parse the assignment-expression. */
3177 expression = cp_parser_assignment_expression (parser,
3179 /* Look for the `,'. */
3180 cp_parser_require (parser, CPP_COMMA, "`,'");
3181 /* Parse the type-id. */
3182 type = cp_parser_type_id (parser);
3183 /* Look for the closing `)'. */
3184 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3185 /* Using `va_arg' in a constant-expression is not
3187 if (cp_parser_non_integral_constant_expression (parser,
3189 return error_mark_node;
3190 return build_x_va_arg (expression, type);
3194 return cp_parser_builtin_offsetof (parser);
3196 /* Objective-C++ expressions. */
3198 case RID_AT_PROTOCOL:
3199 case RID_AT_SELECTOR:
3200 return cp_parser_objc_expression (parser);
3203 cp_parser_error (parser, "expected primary-expression");
3204 return error_mark_node;
3207 /* An id-expression can start with either an identifier, a
3208 `::' as the beginning of a qualified-id, or the "operator"
3212 case CPP_TEMPLATE_ID:
3213 case CPP_NESTED_NAME_SPECIFIER:
3217 const char *error_msg;
3222 /* Parse the id-expression. */
3224 = cp_parser_id_expression (parser,
3225 /*template_keyword_p=*/false,
3226 /*check_dependency_p=*/true,
3228 /*declarator_p=*/false,
3229 /*optional_p=*/false);
3230 if (id_expression == error_mark_node)
3231 return error_mark_node;
3232 token = cp_lexer_peek_token (parser->lexer);
3233 done = (token->type != CPP_OPEN_SQUARE
3234 && token->type != CPP_OPEN_PAREN
3235 && token->type != CPP_DOT
3236 && token->type != CPP_DEREF
3237 && token->type != CPP_PLUS_PLUS
3238 && token->type != CPP_MINUS_MINUS);
3239 /* If we have a template-id, then no further lookup is
3240 required. If the template-id was for a template-class, we
3241 will sometimes have a TYPE_DECL at this point. */
3242 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3243 || TREE_CODE (id_expression) == TYPE_DECL)
3244 decl = id_expression;
3245 /* Look up the name. */
3248 tree ambiguous_decls;
3250 decl = cp_parser_lookup_name (parser, id_expression,
3253 /*is_namespace=*/false,
3254 /*check_dependency=*/true,
3256 /* If the lookup was ambiguous, an error will already have
3258 if (ambiguous_decls)
3259 return error_mark_node;
3261 /* In Objective-C++, an instance variable (ivar) may be preferred
3262 to whatever cp_parser_lookup_name() found. */
3263 decl = objc_lookup_ivar (decl, id_expression);
3265 /* If name lookup gives us a SCOPE_REF, then the
3266 qualifying scope was dependent. */
3267 if (TREE_CODE (decl) == SCOPE_REF)
3269 /* Check to see if DECL is a local variable in a context
3270 where that is forbidden. */
3271 if (parser->local_variables_forbidden_p
3272 && local_variable_p (decl))
3274 /* It might be that we only found DECL because we are
3275 trying to be generous with pre-ISO scoping rules.
3276 For example, consider:
3280 for (int i = 0; i < 10; ++i) {}
3281 extern void f(int j = i);
3284 Here, name look up will originally find the out
3285 of scope `i'. We need to issue a warning message,
3286 but then use the global `i'. */
3287 decl = check_for_out_of_scope_variable (decl);
3288 if (local_variable_p (decl))
3290 error ("local variable %qD may not appear in this context",
3292 return error_mark_node;
3297 decl = (finish_id_expression
3298 (id_expression, decl, parser->scope,
3300 parser->integral_constant_expression_p,
3301 parser->allow_non_integral_constant_expression_p,
3302 &parser->non_integral_constant_expression_p,
3303 template_p, done, address_p,
3307 cp_parser_error (parser, error_msg);
3311 /* Anything else is an error. */
3313 /* ...unless we have an Objective-C++ message or string literal,
3315 if (c_dialect_objc ()
3316 && (token->type == CPP_OPEN_SQUARE
3317 || token->type == CPP_OBJC_STRING))
3318 return cp_parser_objc_expression (parser);
3320 cp_parser_error (parser, "expected primary-expression");
3321 return error_mark_node;
3325 /* Parse an id-expression.
3332 :: [opt] nested-name-specifier template [opt] unqualified-id
3334 :: operator-function-id
3337 Return a representation of the unqualified portion of the
3338 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3339 a `::' or nested-name-specifier.
3341 Often, if the id-expression was a qualified-id, the caller will
3342 want to make a SCOPE_REF to represent the qualified-id. This
3343 function does not do this in order to avoid wastefully creating
3344 SCOPE_REFs when they are not required.
3346 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3349 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3350 uninstantiated templates.
3352 If *TEMPLATE_P is non-NULL, it is set to true iff the
3353 `template' keyword is used to explicitly indicate that the entity
3354 named is a template.
3356 If DECLARATOR_P is true, the id-expression is appearing as part of
3357 a declarator, rather than as part of an expression. */
3360 cp_parser_id_expression (cp_parser *parser,
3361 bool template_keyword_p,
3362 bool check_dependency_p,
3367 bool global_scope_p;
3368 bool nested_name_specifier_p;
3370 /* Assume the `template' keyword was not used. */
3372 *template_p = template_keyword_p;
3374 /* Look for the optional `::' operator. */
3376 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3378 /* Look for the optional nested-name-specifier. */
3379 nested_name_specifier_p
3380 = (cp_parser_nested_name_specifier_opt (parser,
3381 /*typename_keyword_p=*/false,
3386 /* If there is a nested-name-specifier, then we are looking at
3387 the first qualified-id production. */
3388 if (nested_name_specifier_p)
3391 tree saved_object_scope;
3392 tree saved_qualifying_scope;
3393 tree unqualified_id;
3396 /* See if the next token is the `template' keyword. */
3398 template_p = &is_template;
3399 *template_p = cp_parser_optional_template_keyword (parser);
3400 /* Name lookup we do during the processing of the
3401 unqualified-id might obliterate SCOPE. */
3402 saved_scope = parser->scope;
3403 saved_object_scope = parser->object_scope;
3404 saved_qualifying_scope = parser->qualifying_scope;
3405 /* Process the final unqualified-id. */
3406 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3409 /*optional_p=*/false);
3410 /* Restore the SAVED_SCOPE for our caller. */
3411 parser->scope = saved_scope;
3412 parser->object_scope = saved_object_scope;
3413 parser->qualifying_scope = saved_qualifying_scope;
3415 return unqualified_id;
3417 /* Otherwise, if we are in global scope, then we are looking at one
3418 of the other qualified-id productions. */
3419 else if (global_scope_p)
3424 /* Peek at the next token. */
3425 token = cp_lexer_peek_token (parser->lexer);
3427 /* If it's an identifier, and the next token is not a "<", then
3428 we can avoid the template-id case. This is an optimization
3429 for this common case. */
3430 if (token->type == CPP_NAME
3431 && !cp_parser_nth_token_starts_template_argument_list_p
3433 return cp_parser_identifier (parser);
3435 cp_parser_parse_tentatively (parser);
3436 /* Try a template-id. */
3437 id = cp_parser_template_id (parser,
3438 /*template_keyword_p=*/false,
3439 /*check_dependency_p=*/true,
3441 /* If that worked, we're done. */
3442 if (cp_parser_parse_definitely (parser))
3445 /* Peek at the next token. (Changes in the token buffer may
3446 have invalidated the pointer obtained above.) */
3447 token = cp_lexer_peek_token (parser->lexer);
3449 switch (token->type)
3452 return cp_parser_identifier (parser);
3455 if (token->keyword == RID_OPERATOR)
3456 return cp_parser_operator_function_id (parser);
3460 cp_parser_error (parser, "expected id-expression");
3461 return error_mark_node;
3465 return cp_parser_unqualified_id (parser, template_keyword_p,
3466 /*check_dependency_p=*/true,
3471 /* Parse an unqualified-id.
3475 operator-function-id
3476 conversion-function-id
3480 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3481 keyword, in a construct like `A::template ...'.
3483 Returns a representation of unqualified-id. For the `identifier'
3484 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3485 production a BIT_NOT_EXPR is returned; the operand of the
3486 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3487 other productions, see the documentation accompanying the
3488 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3489 names are looked up in uninstantiated templates. If DECLARATOR_P
3490 is true, the unqualified-id is appearing as part of a declarator,
3491 rather than as part of an expression. */
3494 cp_parser_unqualified_id (cp_parser* parser,
3495 bool template_keyword_p,
3496 bool check_dependency_p,
3502 /* Peek at the next token. */
3503 token = cp_lexer_peek_token (parser->lexer);
3505 switch (token->type)
3511 /* We don't know yet whether or not this will be a
3513 cp_parser_parse_tentatively (parser);
3514 /* Try a template-id. */
3515 id = cp_parser_template_id (parser, template_keyword_p,
3518 /* If it worked, we're done. */
3519 if (cp_parser_parse_definitely (parser))
3521 /* Otherwise, it's an ordinary identifier. */
3522 return cp_parser_identifier (parser);
3525 case CPP_TEMPLATE_ID:
3526 return cp_parser_template_id (parser, template_keyword_p,
3533 tree qualifying_scope;
3538 /* Consume the `~' token. */
3539 cp_lexer_consume_token (parser->lexer);
3540 /* Parse the class-name. The standard, as written, seems to
3543 template <typename T> struct S { ~S (); };
3544 template <typename T> S<T>::~S() {}
3546 is invalid, since `~' must be followed by a class-name, but
3547 `S<T>' is dependent, and so not known to be a class.
3548 That's not right; we need to look in uninstantiated
3549 templates. A further complication arises from:
3551 template <typename T> void f(T t) {
3555 Here, it is not possible to look up `T' in the scope of `T'
3556 itself. We must look in both the current scope, and the
3557 scope of the containing complete expression.
3559 Yet another issue is:
3568 The standard does not seem to say that the `S' in `~S'
3569 should refer to the type `S' and not the data member
3572 /* DR 244 says that we look up the name after the "~" in the
3573 same scope as we looked up the qualifying name. That idea
3574 isn't fully worked out; it's more complicated than that. */
3575 scope = parser->scope;
3576 object_scope = parser->object_scope;
3577 qualifying_scope = parser->qualifying_scope;
3579 /* Check for invalid scopes. */
3580 if (scope == error_mark_node)
3582 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3583 cp_lexer_consume_token (parser->lexer);
3584 return error_mark_node;
3586 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3588 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3589 error ("scope %qT before %<~%> is not a class-name", scope);
3590 cp_parser_simulate_error (parser);
3591 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3592 cp_lexer_consume_token (parser->lexer);
3593 return error_mark_node;
3595 gcc_assert (!scope || TYPE_P (scope));
3597 /* If the name is of the form "X::~X" it's OK. */
3598 token = cp_lexer_peek_token (parser->lexer);
3600 && token->type == CPP_NAME
3601 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3603 && constructor_name_p (token->u.value, scope))
3605 cp_lexer_consume_token (parser->lexer);
3606 return build_nt (BIT_NOT_EXPR, scope);
3609 /* If there was an explicit qualification (S::~T), first look
3610 in the scope given by the qualification (i.e., S). */
3612 type_decl = NULL_TREE;
3615 cp_parser_parse_tentatively (parser);
3616 type_decl = cp_parser_class_name (parser,
3617 /*typename_keyword_p=*/false,
3618 /*template_keyword_p=*/false,
3620 /*check_dependency=*/false,
3621 /*class_head_p=*/false,
3623 if (cp_parser_parse_definitely (parser))
3626 /* In "N::S::~S", look in "N" as well. */
3627 if (!done && scope && qualifying_scope)
3629 cp_parser_parse_tentatively (parser);
3630 parser->scope = qualifying_scope;
3631 parser->object_scope = NULL_TREE;
3632 parser->qualifying_scope = NULL_TREE;
3634 = cp_parser_class_name (parser,
3635 /*typename_keyword_p=*/false,
3636 /*template_keyword_p=*/false,
3638 /*check_dependency=*/false,
3639 /*class_head_p=*/false,
3641 if (cp_parser_parse_definitely (parser))
3644 /* In "p->S::~T", look in the scope given by "*p" as well. */
3645 else if (!done && object_scope)
3647 cp_parser_parse_tentatively (parser);
3648 parser->scope = object_scope;
3649 parser->object_scope = NULL_TREE;
3650 parser->qualifying_scope = NULL_TREE;
3652 = cp_parser_class_name (parser,
3653 /*typename_keyword_p=*/false,
3654 /*template_keyword_p=*/false,
3656 /*check_dependency=*/false,
3657 /*class_head_p=*/false,
3659 if (cp_parser_parse_definitely (parser))
3662 /* Look in the surrounding context. */
3665 parser->scope = NULL_TREE;
3666 parser->object_scope = NULL_TREE;
3667 parser->qualifying_scope = NULL_TREE;
3669 = cp_parser_class_name (parser,
3670 /*typename_keyword_p=*/false,
3671 /*template_keyword_p=*/false,
3673 /*check_dependency=*/false,
3674 /*class_head_p=*/false,
3677 /* If an error occurred, assume that the name of the
3678 destructor is the same as the name of the qualifying
3679 class. That allows us to keep parsing after running
3680 into ill-formed destructor names. */
3681 if (type_decl == error_mark_node && scope)
3682 return build_nt (BIT_NOT_EXPR, scope);
3683 else if (type_decl == error_mark_node)
3684 return error_mark_node;
3686 /* Check that destructor name and scope match. */
3687 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3689 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3690 error ("declaration of %<~%T%> as member of %qT",
3692 cp_parser_simulate_error (parser);
3693 return error_mark_node;
3698 A typedef-name that names a class shall not be used as the
3699 identifier in the declarator for a destructor declaration. */
3701 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3702 && !DECL_SELF_REFERENCE_P (type_decl)
3703 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3704 error ("typedef-name %qD used as destructor declarator",
3707 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3711 if (token->keyword == RID_OPERATOR)
3715 /* This could be a template-id, so we try that first. */
3716 cp_parser_parse_tentatively (parser);
3717 /* Try a template-id. */
3718 id = cp_parser_template_id (parser, template_keyword_p,
3719 /*check_dependency_p=*/true,
3721 /* If that worked, we're done. */
3722 if (cp_parser_parse_definitely (parser))
3724 /* We still don't know whether we're looking at an
3725 operator-function-id or a conversion-function-id. */
3726 cp_parser_parse_tentatively (parser);
3727 /* Try an operator-function-id. */
3728 id = cp_parser_operator_function_id (parser);
3729 /* If that didn't work, try a conversion-function-id. */
3730 if (!cp_parser_parse_definitely (parser))
3731 id = cp_parser_conversion_function_id (parser);
3740 cp_parser_error (parser, "expected unqualified-id");
3741 return error_mark_node;
3745 /* Parse an (optional) nested-name-specifier.
3747 nested-name-specifier:
3748 class-or-namespace-name :: nested-name-specifier [opt]
3749 class-or-namespace-name :: template nested-name-specifier [opt]
3751 PARSER->SCOPE should be set appropriately before this function is
3752 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3753 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3756 Sets PARSER->SCOPE to the class (TYPE) or namespace
3757 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3758 it unchanged if there is no nested-name-specifier. Returns the new
3759 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3761 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3762 part of a declaration and/or decl-specifier. */
3765 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3766 bool typename_keyword_p,
3767 bool check_dependency_p,
3769 bool is_declaration)
3771 bool success = false;
3772 cp_token_position start = 0;
3775 /* Remember where the nested-name-specifier starts. */
3776 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3778 start = cp_lexer_token_position (parser->lexer, false);
3779 push_deferring_access_checks (dk_deferred);
3786 tree saved_qualifying_scope;
3787 bool template_keyword_p;
3789 /* Spot cases that cannot be the beginning of a
3790 nested-name-specifier. */
3791 token = cp_lexer_peek_token (parser->lexer);
3793 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3794 the already parsed nested-name-specifier. */
3795 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3797 /* Grab the nested-name-specifier and continue the loop. */
3798 cp_parser_pre_parsed_nested_name_specifier (parser);
3799 /* If we originally encountered this nested-name-specifier
3800 with IS_DECLARATION set to false, we will not have
3801 resolved TYPENAME_TYPEs, so we must do so here. */
3803 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3805 new_scope = resolve_typename_type (parser->scope,
3806 /*only_current_p=*/false);
3807 if (new_scope != error_mark_node)
3808 parser->scope = new_scope;
3814 /* Spot cases that cannot be the beginning of a
3815 nested-name-specifier. On the second and subsequent times
3816 through the loop, we look for the `template' keyword. */
3817 if (success && token->keyword == RID_TEMPLATE)
3819 /* A template-id can start a nested-name-specifier. */
3820 else if (token->type == CPP_TEMPLATE_ID)
3824 /* If the next token is not an identifier, then it is
3825 definitely not a class-or-namespace-name. */
3826 if (token->type != CPP_NAME)
3828 /* If the following token is neither a `<' (to begin a
3829 template-id), nor a `::', then we are not looking at a
3830 nested-name-specifier. */
3831 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3832 if (token->type != CPP_SCOPE
3833 && !cp_parser_nth_token_starts_template_argument_list_p
3838 /* The nested-name-specifier is optional, so we parse
3840 cp_parser_parse_tentatively (parser);
3842 /* Look for the optional `template' keyword, if this isn't the
3843 first time through the loop. */
3845 template_keyword_p = cp_parser_optional_template_keyword (parser);
3847 template_keyword_p = false;
3849 /* Save the old scope since the name lookup we are about to do
3850 might destroy it. */
3851 old_scope = parser->scope;
3852 saved_qualifying_scope = parser->qualifying_scope;
3853 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3854 look up names in "X<T>::I" in order to determine that "Y" is
3855 a template. So, if we have a typename at this point, we make
3856 an effort to look through it. */
3858 && !typename_keyword_p
3860 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3861 parser->scope = resolve_typename_type (parser->scope,
3862 /*only_current_p=*/false);
3863 /* Parse the qualifying entity. */
3865 = cp_parser_class_or_namespace_name (parser,
3871 /* Look for the `::' token. */
3872 cp_parser_require (parser, CPP_SCOPE, "`::'");
3874 /* If we found what we wanted, we keep going; otherwise, we're
3876 if (!cp_parser_parse_definitely (parser))
3878 bool error_p = false;
3880 /* Restore the OLD_SCOPE since it was valid before the
3881 failed attempt at finding the last
3882 class-or-namespace-name. */
3883 parser->scope = old_scope;
3884 parser->qualifying_scope = saved_qualifying_scope;
3885 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3887 /* If the next token is an identifier, and the one after
3888 that is a `::', then any valid interpretation would have
3889 found a class-or-namespace-name. */
3890 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3891 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3893 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3896 token = cp_lexer_consume_token (parser->lexer);
3899 if (!token->ambiguous_p)
3902 tree ambiguous_decls;
3904 decl = cp_parser_lookup_name (parser, token->u.value,
3906 /*is_template=*/false,
3907 /*is_namespace=*/false,
3908 /*check_dependency=*/true,
3910 if (TREE_CODE (decl) == TEMPLATE_DECL)
3911 error ("%qD used without template parameters", decl);
3912 else if (ambiguous_decls)
3914 error ("reference to %qD is ambiguous",
3916 print_candidates (ambiguous_decls);
3917 decl = error_mark_node;
3920 cp_parser_name_lookup_error
3921 (parser, token->u.value, decl,
3922 "is not a class or namespace");
3924 parser->scope = error_mark_node;
3926 /* Treat this as a successful nested-name-specifier
3931 If the name found is not a class-name (clause
3932 _class_) or namespace-name (_namespace.def_), the
3933 program is ill-formed. */
3936 cp_lexer_consume_token (parser->lexer);
3940 /* We've found one valid nested-name-specifier. */
3942 /* Name lookup always gives us a DECL. */
3943 if (TREE_CODE (new_scope) == TYPE_DECL)
3944 new_scope = TREE_TYPE (new_scope);
3945 /* Uses of "template" must be followed by actual templates. */
3946 if (template_keyword_p
3947 && !(CLASS_TYPE_P (new_scope)
3948 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
3949 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
3950 || CLASSTYPE_IS_TEMPLATE (new_scope)))
3951 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
3952 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
3953 == TEMPLATE_ID_EXPR)))
3954 pedwarn (TYPE_P (new_scope)
3955 ? "%qT is not a template"
3956 : "%qD is not a template",
3958 /* If it is a class scope, try to complete it; we are about to
3959 be looking up names inside the class. */
3960 if (TYPE_P (new_scope)
3961 /* Since checking types for dependency can be expensive,
3962 avoid doing it if the type is already complete. */
3963 && !COMPLETE_TYPE_P (new_scope)
3964 /* Do not try to complete dependent types. */
3965 && !dependent_type_p (new_scope))
3966 new_scope = complete_type (new_scope);
3967 /* Make sure we look in the right scope the next time through
3969 parser->scope = new_scope;
3972 /* If parsing tentatively, replace the sequence of tokens that makes
3973 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3974 token. That way, should we re-parse the token stream, we will
3975 not have to repeat the effort required to do the parse, nor will
3976 we issue duplicate error messages. */
3977 if (success && start)
3981 token = cp_lexer_token_at (parser->lexer, start);
3982 /* Reset the contents of the START token. */
3983 token->type = CPP_NESTED_NAME_SPECIFIER;
3984 /* Retrieve any deferred checks. Do not pop this access checks yet
3985 so the memory will not be reclaimed during token replacing below. */
3986 token->u.tree_check_value = GGC_CNEW (struct tree_check);
3987 token->u.tree_check_value->value = parser->scope;
3988 token->u.tree_check_value->checks = get_deferred_access_checks ();
3989 token->u.tree_check_value->qualifying_scope =
3990 parser->qualifying_scope;
3991 token->keyword = RID_MAX;
3993 /* Purge all subsequent tokens. */
3994 cp_lexer_purge_tokens_after (parser->lexer, start);
3998 pop_to_parent_deferring_access_checks ();
4000 return success ? parser->scope : NULL_TREE;
4003 /* Parse a nested-name-specifier. See
4004 cp_parser_nested_name_specifier_opt for details. This function
4005 behaves identically, except that it will an issue an error if no
4006 nested-name-specifier is present. */
4009 cp_parser_nested_name_specifier (cp_parser *parser,
4010 bool typename_keyword_p,
4011 bool check_dependency_p,
4013 bool is_declaration)
4017 /* Look for the nested-name-specifier. */
4018 scope = cp_parser_nested_name_specifier_opt (parser,
4023 /* If it was not present, issue an error message. */
4026 cp_parser_error (parser, "expected nested-name-specifier");
4027 parser->scope = NULL_TREE;
4033 /* Parse a class-or-namespace-name.
4035 class-or-namespace-name:
4039 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4040 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4041 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4042 TYPE_P is TRUE iff the next name should be taken as a class-name,
4043 even the same name is declared to be another entity in the same
4046 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4047 specified by the class-or-namespace-name. If neither is found the
4048 ERROR_MARK_NODE is returned. */
4051 cp_parser_class_or_namespace_name (cp_parser *parser,
4052 bool typename_keyword_p,
4053 bool template_keyword_p,
4054 bool check_dependency_p,
4056 bool is_declaration)
4059 tree saved_qualifying_scope;
4060 tree saved_object_scope;
4064 /* Before we try to parse the class-name, we must save away the
4065 current PARSER->SCOPE since cp_parser_class_name will destroy
4067 saved_scope = parser->scope;
4068 saved_qualifying_scope = parser->qualifying_scope;
4069 saved_object_scope = parser->object_scope;
4070 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4071 there is no need to look for a namespace-name. */
4072 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4074 cp_parser_parse_tentatively (parser);
4075 scope = cp_parser_class_name (parser,
4078 type_p ? class_type : none_type,
4080 /*class_head_p=*/false,
4082 /* If that didn't work, try for a namespace-name. */
4083 if (!only_class_p && !cp_parser_parse_definitely (parser))
4085 /* Restore the saved scope. */
4086 parser->scope = saved_scope;
4087 parser->qualifying_scope = saved_qualifying_scope;
4088 parser->object_scope = saved_object_scope;
4089 /* If we are not looking at an identifier followed by the scope
4090 resolution operator, then this is not part of a
4091 nested-name-specifier. (Note that this function is only used
4092 to parse the components of a nested-name-specifier.) */
4093 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4094 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4095 return error_mark_node;
4096 scope = cp_parser_namespace_name (parser);
4102 /* Parse a postfix-expression.
4106 postfix-expression [ expression ]
4107 postfix-expression ( expression-list [opt] )
4108 simple-type-specifier ( expression-list [opt] )
4109 typename :: [opt] nested-name-specifier identifier
4110 ( expression-list [opt] )
4111 typename :: [opt] nested-name-specifier template [opt] template-id
4112 ( expression-list [opt] )
4113 postfix-expression . template [opt] id-expression
4114 postfix-expression -> template [opt] id-expression
4115 postfix-expression . pseudo-destructor-name
4116 postfix-expression -> pseudo-destructor-name
4117 postfix-expression ++
4118 postfix-expression --
4119 dynamic_cast < type-id > ( expression )
4120 static_cast < type-id > ( expression )
4121 reinterpret_cast < type-id > ( expression )
4122 const_cast < type-id > ( expression )
4123 typeid ( expression )
4129 ( type-id ) { initializer-list , [opt] }
4131 This extension is a GNU version of the C99 compound-literal
4132 construct. (The C99 grammar uses `type-name' instead of `type-id',
4133 but they are essentially the same concept.)
4135 If ADDRESS_P is true, the postfix expression is the operand of the
4136 `&' operator. CAST_P is true if this expression is the target of a
4139 Returns a representation of the expression. */
4142 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4146 cp_id_kind idk = CP_ID_KIND_NONE;
4147 tree postfix_expression = NULL_TREE;
4149 /* Peek at the next token. */
4150 token = cp_lexer_peek_token (parser->lexer);
4151 /* Some of the productions are determined by keywords. */
4152 keyword = token->keyword;
4162 const char *saved_message;
4164 /* All of these can be handled in the same way from the point
4165 of view of parsing. Begin by consuming the token
4166 identifying the cast. */
4167 cp_lexer_consume_token (parser->lexer);
4169 /* New types cannot be defined in the cast. */
4170 saved_message = parser->type_definition_forbidden_message;
4171 parser->type_definition_forbidden_message
4172 = "types may not be defined in casts";
4174 /* Look for the opening `<'. */
4175 cp_parser_require (parser, CPP_LESS, "`<'");
4176 /* Parse the type to which we are casting. */
4177 type = cp_parser_type_id (parser);
4178 /* Look for the closing `>'. */
4179 cp_parser_require (parser, CPP_GREATER, "`>'");
4180 /* Restore the old message. */
4181 parser->type_definition_forbidden_message = saved_message;
4183 /* And the expression which is being cast. */
4184 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4185 expression = cp_parser_expression (parser, /*cast_p=*/true);
4186 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4188 /* Only type conversions to integral or enumeration types
4189 can be used in constant-expressions. */
4190 if (!cast_valid_in_integral_constant_expression_p (type)
4191 && (cp_parser_non_integral_constant_expression
4193 "a cast to a type other than an integral or "
4194 "enumeration type")))
4195 return error_mark_node;
4201 = build_dynamic_cast (type, expression);
4205 = build_static_cast (type, expression);
4209 = build_reinterpret_cast (type, expression);
4213 = build_const_cast (type, expression);
4224 const char *saved_message;
4225 bool saved_in_type_id_in_expr_p;
4227 /* Consume the `typeid' token. */
4228 cp_lexer_consume_token (parser->lexer);
4229 /* Look for the `(' token. */
4230 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4231 /* Types cannot be defined in a `typeid' expression. */
4232 saved_message = parser->type_definition_forbidden_message;
4233 parser->type_definition_forbidden_message
4234 = "types may not be defined in a `typeid\' expression";
4235 /* We can't be sure yet whether we're looking at a type-id or an
4237 cp_parser_parse_tentatively (parser);
4238 /* Try a type-id first. */
4239 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4240 parser->in_type_id_in_expr_p = true;
4241 type = cp_parser_type_id (parser);
4242 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4243 /* Look for the `)' token. Otherwise, we can't be sure that
4244 we're not looking at an expression: consider `typeid (int
4245 (3))', for example. */
4246 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4247 /* If all went well, simply lookup the type-id. */
4248 if (cp_parser_parse_definitely (parser))
4249 postfix_expression = get_typeid (type);
4250 /* Otherwise, fall back to the expression variant. */
4255 /* Look for an expression. */
4256 expression = cp_parser_expression (parser, /*cast_p=*/false);
4257 /* Compute its typeid. */
4258 postfix_expression = build_typeid (expression);
4259 /* Look for the `)' token. */
4260 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4262 /* Restore the saved message. */
4263 parser->type_definition_forbidden_message = saved_message;
4264 /* `typeid' may not appear in an integral constant expression. */
4265 if (cp_parser_non_integral_constant_expression(parser,
4266 "`typeid' operator"))
4267 return error_mark_node;
4274 /* The syntax permitted here is the same permitted for an
4275 elaborated-type-specifier. */
4276 type = cp_parser_elaborated_type_specifier (parser,
4277 /*is_friend=*/false,
4278 /*is_declaration=*/false);
4279 postfix_expression = cp_parser_functional_cast (parser, type);
4287 /* If the next thing is a simple-type-specifier, we may be
4288 looking at a functional cast. We could also be looking at
4289 an id-expression. So, we try the functional cast, and if
4290 that doesn't work we fall back to the primary-expression. */
4291 cp_parser_parse_tentatively (parser);
4292 /* Look for the simple-type-specifier. */
4293 type = cp_parser_simple_type_specifier (parser,
4294 /*decl_specs=*/NULL,
4295 CP_PARSER_FLAGS_NONE);
4296 /* Parse the cast itself. */
4297 if (!cp_parser_error_occurred (parser))
4299 = cp_parser_functional_cast (parser, type);
4300 /* If that worked, we're done. */
4301 if (cp_parser_parse_definitely (parser))
4304 /* If the functional-cast didn't work out, try a
4305 compound-literal. */
4306 if (cp_parser_allow_gnu_extensions_p (parser)
4307 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4309 VEC(constructor_elt,gc) *initializer_list = NULL;
4310 bool saved_in_type_id_in_expr_p;
4312 cp_parser_parse_tentatively (parser);
4313 /* Consume the `('. */
4314 cp_lexer_consume_token (parser->lexer);
4315 /* Parse the type. */
4316 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4317 parser->in_type_id_in_expr_p = true;
4318 type = cp_parser_type_id (parser);
4319 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4320 /* Look for the `)'. */
4321 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4322 /* Look for the `{'. */
4323 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4324 /* If things aren't going well, there's no need to
4326 if (!cp_parser_error_occurred (parser))
4328 bool non_constant_p;
4329 /* Parse the initializer-list. */
4331 = cp_parser_initializer_list (parser, &non_constant_p);
4332 /* Allow a trailing `,'. */
4333 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4334 cp_lexer_consume_token (parser->lexer);
4335 /* Look for the final `}'. */
4336 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4338 /* If that worked, we're definitely looking at a
4339 compound-literal expression. */
4340 if (cp_parser_parse_definitely (parser))
4342 /* Warn the user that a compound literal is not
4343 allowed in standard C++. */
4345 pedwarn ("ISO C++ forbids compound-literals");
4346 /* For simplicitly, we disallow compound literals in
4347 constant-expressions for simpliicitly. We could
4348 allow compound literals of integer type, whose
4349 initializer was a constant, in constant
4350 expressions. Permitting that usage, as a further
4351 extension, would not change the meaning of any
4352 currently accepted programs. (Of course, as
4353 compound literals are not part of ISO C++, the
4354 standard has nothing to say.) */
4355 if (cp_parser_non_integral_constant_expression
4356 (parser, "non-constant compound literals"))
4358 postfix_expression = error_mark_node;
4361 /* Form the representation of the compound-literal. */
4363 = finish_compound_literal (type, initializer_list);
4368 /* It must be a primary-expression. */
4370 = cp_parser_primary_expression (parser, address_p, cast_p,
4371 /*template_arg_p=*/false,
4377 /* Keep looping until the postfix-expression is complete. */
4380 if (idk == CP_ID_KIND_UNQUALIFIED
4381 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4382 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4383 /* It is not a Koenig lookup function call. */
4385 = unqualified_name_lookup_error (postfix_expression);
4387 /* Peek at the next token. */
4388 token = cp_lexer_peek_token (parser->lexer);
4390 switch (token->type)
4392 case CPP_OPEN_SQUARE:
4394 = cp_parser_postfix_open_square_expression (parser,
4397 idk = CP_ID_KIND_NONE;
4400 case CPP_OPEN_PAREN:
4401 /* postfix-expression ( expression-list [opt] ) */
4404 bool is_builtin_constant_p;
4405 bool saved_integral_constant_expression_p = false;
4406 bool saved_non_integral_constant_expression_p = false;
4409 is_builtin_constant_p
4410 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4411 if (is_builtin_constant_p)
4413 /* The whole point of __builtin_constant_p is to allow
4414 non-constant expressions to appear as arguments. */
4415 saved_integral_constant_expression_p
4416 = parser->integral_constant_expression_p;
4417 saved_non_integral_constant_expression_p
4418 = parser->non_integral_constant_expression_p;
4419 parser->integral_constant_expression_p = false;
4421 args = (cp_parser_parenthesized_expression_list
4422 (parser, /*is_attribute_list=*/false,
4423 /*cast_p=*/false, /*allow_expansion_p=*/true,
4424 /*non_constant_p=*/NULL));
4425 if (is_builtin_constant_p)
4427 parser->integral_constant_expression_p
4428 = saved_integral_constant_expression_p;
4429 parser->non_integral_constant_expression_p
4430 = saved_non_integral_constant_expression_p;
4433 if (args == error_mark_node)
4435 postfix_expression = error_mark_node;
4439 /* Function calls are not permitted in
4440 constant-expressions. */
4441 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4442 && cp_parser_non_integral_constant_expression (parser,
4445 postfix_expression = error_mark_node;
4450 if (idk == CP_ID_KIND_UNQUALIFIED)
4452 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4458 = perform_koenig_lookup (postfix_expression, args);
4462 = unqualified_fn_lookup_error (postfix_expression);
4464 /* We do not perform argument-dependent lookup if
4465 normal lookup finds a non-function, in accordance
4466 with the expected resolution of DR 218. */
4467 else if (args && is_overloaded_fn (postfix_expression))
4469 tree fn = get_first_fn (postfix_expression);
4471 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4472 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4474 /* Only do argument dependent lookup if regular
4475 lookup does not find a set of member functions.
4476 [basic.lookup.koenig]/2a */
4477 if (!DECL_FUNCTION_MEMBER_P (fn))
4481 = perform_koenig_lookup (postfix_expression, args);
4486 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4488 tree instance = TREE_OPERAND (postfix_expression, 0);
4489 tree fn = TREE_OPERAND (postfix_expression, 1);
4491 if (processing_template_decl
4492 && (type_dependent_expression_p (instance)
4493 || (!BASELINK_P (fn)
4494 && TREE_CODE (fn) != FIELD_DECL)
4495 || type_dependent_expression_p (fn)
4496 || any_type_dependent_arguments_p (args)))
4499 = build_nt_call_list (postfix_expression, args);
4503 if (BASELINK_P (fn))
4505 = (build_new_method_call
4506 (instance, fn, args, NULL_TREE,
4507 (idk == CP_ID_KIND_QUALIFIED
4508 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4512 = finish_call_expr (postfix_expression, args,
4513 /*disallow_virtual=*/false,
4514 /*koenig_p=*/false);
4516 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4517 || TREE_CODE (postfix_expression) == MEMBER_REF
4518 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4519 postfix_expression = (build_offset_ref_call_from_tree
4520 (postfix_expression, args));
4521 else if (idk == CP_ID_KIND_QUALIFIED)
4522 /* A call to a static class member, or a namespace-scope
4525 = finish_call_expr (postfix_expression, args,
4526 /*disallow_virtual=*/true,
4529 /* All other function calls. */
4531 = finish_call_expr (postfix_expression, args,
4532 /*disallow_virtual=*/false,
4535 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4536 idk = CP_ID_KIND_NONE;
4542 /* postfix-expression . template [opt] id-expression
4543 postfix-expression . pseudo-destructor-name
4544 postfix-expression -> template [opt] id-expression
4545 postfix-expression -> pseudo-destructor-name */
4547 /* Consume the `.' or `->' operator. */
4548 cp_lexer_consume_token (parser->lexer);
4551 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4557 /* postfix-expression ++ */
4558 /* Consume the `++' token. */
4559 cp_lexer_consume_token (parser->lexer);
4560 /* Generate a representation for the complete expression. */
4562 = finish_increment_expr (postfix_expression,
4563 POSTINCREMENT_EXPR);
4564 /* Increments may not appear in constant-expressions. */
4565 if (cp_parser_non_integral_constant_expression (parser,
4567 postfix_expression = error_mark_node;
4568 idk = CP_ID_KIND_NONE;
4571 case CPP_MINUS_MINUS:
4572 /* postfix-expression -- */
4573 /* Consume the `--' token. */
4574 cp_lexer_consume_token (parser->lexer);
4575 /* Generate a representation for the complete expression. */
4577 = finish_increment_expr (postfix_expression,
4578 POSTDECREMENT_EXPR);
4579 /* Decrements may not appear in constant-expressions. */
4580 if (cp_parser_non_integral_constant_expression (parser,
4582 postfix_expression = error_mark_node;
4583 idk = CP_ID_KIND_NONE;
4587 return postfix_expression;
4591 /* We should never get here. */
4593 return error_mark_node;
4596 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4597 by cp_parser_builtin_offsetof. We're looking for
4599 postfix-expression [ expression ]
4601 FOR_OFFSETOF is set if we're being called in that context, which
4602 changes how we deal with integer constant expressions. */
4605 cp_parser_postfix_open_square_expression (cp_parser *parser,
4606 tree postfix_expression,
4611 /* Consume the `[' token. */
4612 cp_lexer_consume_token (parser->lexer);
4614 /* Parse the index expression. */
4615 /* ??? For offsetof, there is a question of what to allow here. If
4616 offsetof is not being used in an integral constant expression context,
4617 then we *could* get the right answer by computing the value at runtime.
4618 If we are in an integral constant expression context, then we might
4619 could accept any constant expression; hard to say without analysis.
4620 Rather than open the barn door too wide right away, allow only integer
4621 constant expressions here. */
4623 index = cp_parser_constant_expression (parser, false, NULL);
4625 index = cp_parser_expression (parser, /*cast_p=*/false);
4627 /* Look for the closing `]'. */
4628 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4630 /* Build the ARRAY_REF. */
4631 postfix_expression = grok_array_decl (postfix_expression, index);
4633 /* When not doing offsetof, array references are not permitted in
4634 constant-expressions. */
4636 && (cp_parser_non_integral_constant_expression
4637 (parser, "an array reference")))
4638 postfix_expression = error_mark_node;
4640 return postfix_expression;
4643 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4644 by cp_parser_builtin_offsetof. We're looking for
4646 postfix-expression . template [opt] id-expression
4647 postfix-expression . pseudo-destructor-name
4648 postfix-expression -> template [opt] id-expression
4649 postfix-expression -> pseudo-destructor-name
4651 FOR_OFFSETOF is set if we're being called in that context. That sorta
4652 limits what of the above we'll actually accept, but nevermind.
4653 TOKEN_TYPE is the "." or "->" token, which will already have been
4654 removed from the stream. */
4657 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4658 enum cpp_ttype token_type,
4659 tree postfix_expression,
4660 bool for_offsetof, cp_id_kind *idk)
4664 bool pseudo_destructor_p;
4665 tree scope = NULL_TREE;
4667 /* If this is a `->' operator, dereference the pointer. */
4668 if (token_type == CPP_DEREF)
4669 postfix_expression = build_x_arrow (postfix_expression);
4670 /* Check to see whether or not the expression is type-dependent. */
4671 dependent_p = type_dependent_expression_p (postfix_expression);
4672 /* The identifier following the `->' or `.' is not qualified. */
4673 parser->scope = NULL_TREE;
4674 parser->qualifying_scope = NULL_TREE;
4675 parser->object_scope = NULL_TREE;
4676 *idk = CP_ID_KIND_NONE;
4677 /* Enter the scope corresponding to the type of the object
4678 given by the POSTFIX_EXPRESSION. */
4679 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4681 scope = TREE_TYPE (postfix_expression);
4682 /* According to the standard, no expression should ever have
4683 reference type. Unfortunately, we do not currently match
4684 the standard in this respect in that our internal representation
4685 of an expression may have reference type even when the standard
4686 says it does not. Therefore, we have to manually obtain the
4687 underlying type here. */
4688 scope = non_reference (scope);
4689 /* The type of the POSTFIX_EXPRESSION must be complete. */
4690 if (scope == unknown_type_node)
4692 error ("%qE does not have class type", postfix_expression);
4696 scope = complete_type_or_else (scope, NULL_TREE);
4697 /* Let the name lookup machinery know that we are processing a
4698 class member access expression. */
4699 parser->context->object_type = scope;
4700 /* If something went wrong, we want to be able to discern that case,
4701 as opposed to the case where there was no SCOPE due to the type
4702 of expression being dependent. */
4704 scope = error_mark_node;
4705 /* If the SCOPE was erroneous, make the various semantic analysis
4706 functions exit quickly -- and without issuing additional error
4708 if (scope == error_mark_node)
4709 postfix_expression = error_mark_node;
4712 /* Assume this expression is not a pseudo-destructor access. */
4713 pseudo_destructor_p = false;
4715 /* If the SCOPE is a scalar type, then, if this is a valid program,
4716 we must be looking at a pseudo-destructor-name. */
4717 if (scope && SCALAR_TYPE_P (scope))
4722 cp_parser_parse_tentatively (parser);
4723 /* Parse the pseudo-destructor-name. */
4725 cp_parser_pseudo_destructor_name (parser, &s, &type);
4726 if (cp_parser_parse_definitely (parser))
4728 pseudo_destructor_p = true;
4730 = finish_pseudo_destructor_expr (postfix_expression,
4731 s, TREE_TYPE (type));
4735 if (!pseudo_destructor_p)
4737 /* If the SCOPE is not a scalar type, we are looking at an
4738 ordinary class member access expression, rather than a
4739 pseudo-destructor-name. */
4741 /* Parse the id-expression. */
4742 name = (cp_parser_id_expression
4744 cp_parser_optional_template_keyword (parser),
4745 /*check_dependency_p=*/true,
4747 /*declarator_p=*/false,
4748 /*optional_p=*/false));
4749 /* In general, build a SCOPE_REF if the member name is qualified.
4750 However, if the name was not dependent and has already been
4751 resolved; there is no need to build the SCOPE_REF. For example;
4753 struct X { void f(); };
4754 template <typename T> void f(T* t) { t->X::f(); }
4756 Even though "t" is dependent, "X::f" is not and has been resolved
4757 to a BASELINK; there is no need to include scope information. */
4759 /* But we do need to remember that there was an explicit scope for
4760 virtual function calls. */
4762 *idk = CP_ID_KIND_QUALIFIED;
4764 /* If the name is a template-id that names a type, we will get a
4765 TYPE_DECL here. That is invalid code. */
4766 if (TREE_CODE (name) == TYPE_DECL)
4768 error ("invalid use of %qD", name);
4769 postfix_expression = error_mark_node;
4773 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4775 name = build_qualified_name (/*type=*/NULL_TREE,
4779 parser->scope = NULL_TREE;
4780 parser->qualifying_scope = NULL_TREE;
4781 parser->object_scope = NULL_TREE;
4783 if (scope && name && BASELINK_P (name))
4784 adjust_result_of_qualified_name_lookup
4785 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4787 = finish_class_member_access_expr (postfix_expression, name,
4792 /* We no longer need to look up names in the scope of the object on
4793 the left-hand side of the `.' or `->' operator. */
4794 parser->context->object_type = NULL_TREE;
4796 /* Outside of offsetof, these operators may not appear in
4797 constant-expressions. */
4799 && (cp_parser_non_integral_constant_expression
4800 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4801 postfix_expression = error_mark_node;
4803 return postfix_expression;
4806 /* Parse a parenthesized expression-list.
4809 assignment-expression
4810 expression-list, assignment-expression
4815 identifier, expression-list
4817 CAST_P is true if this expression is the target of a cast.
4819 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4822 Returns a TREE_LIST. The TREE_VALUE of each node is a
4823 representation of an assignment-expression. Note that a TREE_LIST
4824 is returned even if there is only a single expression in the list.
4825 error_mark_node is returned if the ( and or ) are
4826 missing. NULL_TREE is returned on no expressions. The parentheses
4827 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4828 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4829 indicates whether or not all of the expressions in the list were
4833 cp_parser_parenthesized_expression_list (cp_parser* parser,
4834 bool is_attribute_list,
4836 bool allow_expansion_p,
4837 bool *non_constant_p)
4839 tree expression_list = NULL_TREE;
4840 bool fold_expr_p = is_attribute_list;
4841 tree identifier = NULL_TREE;
4843 /* Assume all the expressions will be constant. */
4845 *non_constant_p = false;
4847 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4848 return error_mark_node;
4850 /* Consume expressions until there are no more. */
4851 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4856 /* At the beginning of attribute lists, check to see if the
4857 next token is an identifier. */
4858 if (is_attribute_list
4859 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4863 /* Consume the identifier. */
4864 token = cp_lexer_consume_token (parser->lexer);
4865 /* Save the identifier. */
4866 identifier = token->u.value;
4870 /* Parse the next assignment-expression. */
4873 bool expr_non_constant_p;
4874 expr = (cp_parser_constant_expression
4875 (parser, /*allow_non_constant_p=*/true,
4876 &expr_non_constant_p));
4877 if (expr_non_constant_p)
4878 *non_constant_p = true;
4881 expr = cp_parser_assignment_expression (parser, cast_p);
4884 expr = fold_non_dependent_expr (expr);
4886 /* If we have an ellipsis, then this is an expression
4888 if (allow_expansion_p
4889 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4891 /* Consume the `...'. */
4892 cp_lexer_consume_token (parser->lexer);
4894 /* Build the argument pack. */
4895 expr = make_pack_expansion (expr);
4898 /* Add it to the list. We add error_mark_node
4899 expressions to the list, so that we can still tell if
4900 the correct form for a parenthesized expression-list
4901 is found. That gives better errors. */
4902 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4904 if (expr == error_mark_node)
4908 /* After the first item, attribute lists look the same as
4909 expression lists. */
4910 is_attribute_list = false;
4913 /* If the next token isn't a `,', then we are done. */
4914 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4917 /* Otherwise, consume the `,' and keep going. */
4918 cp_lexer_consume_token (parser->lexer);
4921 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4926 /* We try and resync to an unnested comma, as that will give the
4927 user better diagnostics. */
4928 ending = cp_parser_skip_to_closing_parenthesis (parser,
4929 /*recovering=*/true,
4931 /*consume_paren=*/true);
4935 return error_mark_node;
4938 /* We built up the list in reverse order so we must reverse it now. */
4939 expression_list = nreverse (expression_list);
4941 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4943 return expression_list;
4946 /* Parse a pseudo-destructor-name.
4948 pseudo-destructor-name:
4949 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4950 :: [opt] nested-name-specifier template template-id :: ~ type-name
4951 :: [opt] nested-name-specifier [opt] ~ type-name
4953 If either of the first two productions is used, sets *SCOPE to the
4954 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4955 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4956 or ERROR_MARK_NODE if the parse fails. */
4959 cp_parser_pseudo_destructor_name (cp_parser* parser,
4963 bool nested_name_specifier_p;
4965 /* Assume that things will not work out. */
4966 *type = error_mark_node;
4968 /* Look for the optional `::' operator. */
4969 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4970 /* Look for the optional nested-name-specifier. */
4971 nested_name_specifier_p
4972 = (cp_parser_nested_name_specifier_opt (parser,
4973 /*typename_keyword_p=*/false,
4974 /*check_dependency_p=*/true,
4976 /*is_declaration=*/true)
4978 /* Now, if we saw a nested-name-specifier, we might be doing the
4979 second production. */
4980 if (nested_name_specifier_p
4981 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4983 /* Consume the `template' keyword. */
4984 cp_lexer_consume_token (parser->lexer);
4985 /* Parse the template-id. */
4986 cp_parser_template_id (parser,
4987 /*template_keyword_p=*/true,
4988 /*check_dependency_p=*/false,
4989 /*is_declaration=*/true);
4990 /* Look for the `::' token. */
4991 cp_parser_require (parser, CPP_SCOPE, "`::'");
4993 /* If the next token is not a `~', then there might be some
4994 additional qualification. */
4995 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4997 /* Look for the type-name. */
4998 *scope = TREE_TYPE (cp_parser_type_name (parser));
5000 if (*scope == error_mark_node)
5003 /* If we don't have ::~, then something has gone wrong. Since
5004 the only caller of this function is looking for something
5005 after `.' or `->' after a scalar type, most likely the
5006 program is trying to get a member of a non-aggregate
5008 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5009 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5011 cp_parser_error (parser, "request for member of non-aggregate type");
5015 /* Look for the `::' token. */
5016 cp_parser_require (parser, CPP_SCOPE, "`::'");
5021 /* Look for the `~'. */
5022 cp_parser_require (parser, CPP_COMPL, "`~'");
5023 /* Look for the type-name again. We are not responsible for
5024 checking that it matches the first type-name. */
5025 *type = cp_parser_type_name (parser);
5028 /* Parse a unary-expression.
5034 unary-operator cast-expression
5035 sizeof unary-expression
5043 __extension__ cast-expression
5044 __alignof__ unary-expression
5045 __alignof__ ( type-id )
5046 __real__ cast-expression
5047 __imag__ cast-expression
5050 ADDRESS_P is true iff the unary-expression is appearing as the
5051 operand of the `&' operator. CAST_P is true if this expression is
5052 the target of a cast.
5054 Returns a representation of the expression. */
5057 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5060 enum tree_code unary_operator;
5062 /* Peek at the next token. */
5063 token = cp_lexer_peek_token (parser->lexer);
5064 /* Some keywords give away the kind of expression. */
5065 if (token->type == CPP_KEYWORD)
5067 enum rid keyword = token->keyword;
5077 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5078 /* Consume the token. */
5079 cp_lexer_consume_token (parser->lexer);
5080 /* Parse the operand. */
5081 operand = cp_parser_sizeof_operand (parser, keyword);
5083 if (TYPE_P (operand))
5084 return cxx_sizeof_or_alignof_type (operand, op, true);
5086 return cxx_sizeof_or_alignof_expr (operand, op);
5090 return cp_parser_new_expression (parser);
5093 return cp_parser_delete_expression (parser);
5097 /* The saved value of the PEDANTIC flag. */
5101 /* Save away the PEDANTIC flag. */
5102 cp_parser_extension_opt (parser, &saved_pedantic);
5103 /* Parse the cast-expression. */
5104 expr = cp_parser_simple_cast_expression (parser);
5105 /* Restore the PEDANTIC flag. */
5106 pedantic = saved_pedantic;
5116 /* Consume the `__real__' or `__imag__' token. */
5117 cp_lexer_consume_token (parser->lexer);
5118 /* Parse the cast-expression. */
5119 expression = cp_parser_simple_cast_expression (parser);
5120 /* Create the complete representation. */
5121 return build_x_unary_op ((keyword == RID_REALPART
5122 ? REALPART_EXPR : IMAGPART_EXPR),
5132 /* Look for the `:: new' and `:: delete', which also signal the
5133 beginning of a new-expression, or delete-expression,
5134 respectively. If the next token is `::', then it might be one of
5136 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5140 /* See if the token after the `::' is one of the keywords in
5141 which we're interested. */
5142 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5143 /* If it's `new', we have a new-expression. */
5144 if (keyword == RID_NEW)
5145 return cp_parser_new_expression (parser);
5146 /* Similarly, for `delete'. */
5147 else if (keyword == RID_DELETE)
5148 return cp_parser_delete_expression (parser);
5151 /* Look for a unary operator. */
5152 unary_operator = cp_parser_unary_operator (token);
5153 /* The `++' and `--' operators can be handled similarly, even though
5154 they are not technically unary-operators in the grammar. */
5155 if (unary_operator == ERROR_MARK)
5157 if (token->type == CPP_PLUS_PLUS)
5158 unary_operator = PREINCREMENT_EXPR;
5159 else if (token->type == CPP_MINUS_MINUS)
5160 unary_operator = PREDECREMENT_EXPR;
5161 /* Handle the GNU address-of-label extension. */
5162 else if (cp_parser_allow_gnu_extensions_p (parser)
5163 && token->type == CPP_AND_AND)
5167 /* Consume the '&&' token. */
5168 cp_lexer_consume_token (parser->lexer);
5169 /* Look for the identifier. */
5170 identifier = cp_parser_identifier (parser);
5171 /* Create an expression representing the address. */
5172 return finish_label_address_expr (identifier);
5175 if (unary_operator != ERROR_MARK)
5177 tree cast_expression;
5178 tree expression = error_mark_node;
5179 const char *non_constant_p = NULL;
5181 /* Consume the operator token. */
5182 token = cp_lexer_consume_token (parser->lexer);
5183 /* Parse the cast-expression. */
5185 = cp_parser_cast_expression (parser,
5186 unary_operator == ADDR_EXPR,
5188 /* Now, build an appropriate representation. */
5189 switch (unary_operator)
5192 non_constant_p = "`*'";
5193 expression = build_x_indirect_ref (cast_expression, "unary *");
5197 non_constant_p = "`&'";
5200 expression = build_x_unary_op (unary_operator, cast_expression);
5203 case PREINCREMENT_EXPR:
5204 case PREDECREMENT_EXPR:
5205 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5208 case UNARY_PLUS_EXPR:
5210 case TRUTH_NOT_EXPR:
5211 expression = finish_unary_op_expr (unary_operator, cast_expression);
5219 && cp_parser_non_integral_constant_expression (parser,
5221 expression = error_mark_node;
5226 return cp_parser_postfix_expression (parser, address_p, cast_p);
5229 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5230 unary-operator, the corresponding tree code is returned. */
5232 static enum tree_code
5233 cp_parser_unary_operator (cp_token* token)
5235 switch (token->type)
5238 return INDIRECT_REF;
5244 return UNARY_PLUS_EXPR;
5250 return TRUTH_NOT_EXPR;
5253 return BIT_NOT_EXPR;
5260 /* Parse a new-expression.
5263 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5264 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5266 Returns a representation of the expression. */
5269 cp_parser_new_expression (cp_parser* parser)
5271 bool global_scope_p;
5277 /* Look for the optional `::' operator. */
5279 = (cp_parser_global_scope_opt (parser,
5280 /*current_scope_valid_p=*/false)
5282 /* Look for the `new' operator. */
5283 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5284 /* There's no easy way to tell a new-placement from the
5285 `( type-id )' construct. */
5286 cp_parser_parse_tentatively (parser);
5287 /* Look for a new-placement. */
5288 placement = cp_parser_new_placement (parser);
5289 /* If that didn't work out, there's no new-placement. */
5290 if (!cp_parser_parse_definitely (parser))
5291 placement = NULL_TREE;
5293 /* If the next token is a `(', then we have a parenthesized
5295 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5297 /* Consume the `('. */
5298 cp_lexer_consume_token (parser->lexer);
5299 /* Parse the type-id. */
5300 type = cp_parser_type_id (parser);
5301 /* Look for the closing `)'. */
5302 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5303 /* There should not be a direct-new-declarator in this production,
5304 but GCC used to allowed this, so we check and emit a sensible error
5305 message for this case. */
5306 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5308 error ("array bound forbidden after parenthesized type-id");
5309 inform ("try removing the parentheses around the type-id");
5310 cp_parser_direct_new_declarator (parser);
5314 /* Otherwise, there must be a new-type-id. */
5316 type = cp_parser_new_type_id (parser, &nelts);
5318 /* If the next token is a `(', then we have a new-initializer. */
5319 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5320 initializer = cp_parser_new_initializer (parser);
5322 initializer = NULL_TREE;
5324 /* A new-expression may not appear in an integral constant
5326 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5327 return error_mark_node;
5329 /* Create a representation of the new-expression. */
5330 return build_new (placement, type, nelts, initializer, global_scope_p);
5333 /* Parse a new-placement.
5338 Returns the same representation as for an expression-list. */
5341 cp_parser_new_placement (cp_parser* parser)
5343 tree expression_list;
5345 /* Parse the expression-list. */
5346 expression_list = (cp_parser_parenthesized_expression_list
5347 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5348 /*non_constant_p=*/NULL));
5350 return expression_list;
5353 /* Parse a new-type-id.
5356 type-specifier-seq new-declarator [opt]
5358 Returns the TYPE allocated. If the new-type-id indicates an array
5359 type, *NELTS is set to the number of elements in the last array
5360 bound; the TYPE will not include the last array bound. */
5363 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5365 cp_decl_specifier_seq type_specifier_seq;
5366 cp_declarator *new_declarator;
5367 cp_declarator *declarator;
5368 cp_declarator *outer_declarator;
5369 const char *saved_message;
5372 /* The type-specifier sequence must not contain type definitions.
5373 (It cannot contain declarations of new types either, but if they
5374 are not definitions we will catch that because they are not
5376 saved_message = parser->type_definition_forbidden_message;
5377 parser->type_definition_forbidden_message
5378 = "types may not be defined in a new-type-id";
5379 /* Parse the type-specifier-seq. */
5380 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5381 &type_specifier_seq);
5382 /* Restore the old message. */
5383 parser->type_definition_forbidden_message = saved_message;
5384 /* Parse the new-declarator. */
5385 new_declarator = cp_parser_new_declarator_opt (parser);
5387 /* Determine the number of elements in the last array dimension, if
5390 /* Skip down to the last array dimension. */
5391 declarator = new_declarator;
5392 outer_declarator = NULL;
5393 while (declarator && (declarator->kind == cdk_pointer
5394 || declarator->kind == cdk_ptrmem))
5396 outer_declarator = declarator;
5397 declarator = declarator->declarator;
5400 && declarator->kind == cdk_array
5401 && declarator->declarator
5402 && declarator->declarator->kind == cdk_array)
5404 outer_declarator = declarator;
5405 declarator = declarator->declarator;
5408 if (declarator && declarator->kind == cdk_array)
5410 *nelts = declarator->u.array.bounds;
5411 if (*nelts == error_mark_node)
5412 *nelts = integer_one_node;
5414 if (outer_declarator)
5415 outer_declarator->declarator = declarator->declarator;
5417 new_declarator = NULL;
5420 type = groktypename (&type_specifier_seq, new_declarator);
5421 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5423 *nelts = array_type_nelts_top (type);
5424 type = TREE_TYPE (type);
5429 /* Parse an (optional) new-declarator.
5432 ptr-operator new-declarator [opt]
5433 direct-new-declarator
5435 Returns the declarator. */
5437 static cp_declarator *
5438 cp_parser_new_declarator_opt (cp_parser* parser)
5440 enum tree_code code;
5442 cp_cv_quals cv_quals;
5444 /* We don't know if there's a ptr-operator next, or not. */
5445 cp_parser_parse_tentatively (parser);
5446 /* Look for a ptr-operator. */
5447 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5448 /* If that worked, look for more new-declarators. */
5449 if (cp_parser_parse_definitely (parser))
5451 cp_declarator *declarator;
5453 /* Parse another optional declarator. */
5454 declarator = cp_parser_new_declarator_opt (parser);
5456 /* Create the representation of the declarator. */
5458 declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5459 else if (code == INDIRECT_REF)
5460 declarator = make_pointer_declarator (cv_quals, declarator);
5462 declarator = make_reference_declarator (cv_quals, declarator);
5467 /* If the next token is a `[', there is a direct-new-declarator. */
5468 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5469 return cp_parser_direct_new_declarator (parser);
5474 /* Parse a direct-new-declarator.
5476 direct-new-declarator:
5478 direct-new-declarator [constant-expression]
5482 static cp_declarator *
5483 cp_parser_direct_new_declarator (cp_parser* parser)
5485 cp_declarator *declarator = NULL;
5491 /* Look for the opening `['. */
5492 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5493 /* The first expression is not required to be constant. */
5496 expression = cp_parser_expression (parser, /*cast_p=*/false);
5497 /* The standard requires that the expression have integral
5498 type. DR 74 adds enumeration types. We believe that the
5499 real intent is that these expressions be handled like the
5500 expression in a `switch' condition, which also allows
5501 classes with a single conversion to integral or
5502 enumeration type. */
5503 if (!processing_template_decl)
5506 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5511 error ("expression in new-declarator must have integral "
5512 "or enumeration type");
5513 expression = error_mark_node;
5517 /* But all the other expressions must be. */
5520 = cp_parser_constant_expression (parser,
5521 /*allow_non_constant=*/false,
5523 /* Look for the closing `]'. */
5524 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5526 /* Add this bound to the declarator. */
5527 declarator = make_array_declarator (declarator, expression);
5529 /* If the next token is not a `[', then there are no more
5531 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5538 /* Parse a new-initializer.
5541 ( expression-list [opt] )
5543 Returns a representation of the expression-list. If there is no
5544 expression-list, VOID_ZERO_NODE is returned. */
5547 cp_parser_new_initializer (cp_parser* parser)
5549 tree expression_list;
5551 expression_list = (cp_parser_parenthesized_expression_list
5552 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5553 /*non_constant_p=*/NULL));
5554 if (!expression_list)
5555 expression_list = void_zero_node;
5557 return expression_list;
5560 /* Parse a delete-expression.
5563 :: [opt] delete cast-expression
5564 :: [opt] delete [ ] cast-expression
5566 Returns a representation of the expression. */
5569 cp_parser_delete_expression (cp_parser* parser)
5571 bool global_scope_p;
5575 /* Look for the optional `::' operator. */
5577 = (cp_parser_global_scope_opt (parser,
5578 /*current_scope_valid_p=*/false)
5580 /* Look for the `delete' keyword. */
5581 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5582 /* See if the array syntax is in use. */
5583 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5585 /* Consume the `[' token. */
5586 cp_lexer_consume_token (parser->lexer);
5587 /* Look for the `]' token. */
5588 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5589 /* Remember that this is the `[]' construct. */
5595 /* Parse the cast-expression. */
5596 expression = cp_parser_simple_cast_expression (parser);
5598 /* A delete-expression may not appear in an integral constant
5600 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5601 return error_mark_node;
5603 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5606 /* Parse a cast-expression.
5610 ( type-id ) cast-expression
5612 ADDRESS_P is true iff the unary-expression is appearing as the
5613 operand of the `&' operator. CAST_P is true if this expression is
5614 the target of a cast.
5616 Returns a representation of the expression. */
5619 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5621 /* If it's a `(', then we might be looking at a cast. */
5622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5624 tree type = NULL_TREE;
5625 tree expr = NULL_TREE;
5626 bool compound_literal_p;
5627 const char *saved_message;
5629 /* There's no way to know yet whether or not this is a cast.
5630 For example, `(int (3))' is a unary-expression, while `(int)
5631 3' is a cast. So, we resort to parsing tentatively. */
5632 cp_parser_parse_tentatively (parser);
5633 /* Types may not be defined in a cast. */
5634 saved_message = parser->type_definition_forbidden_message;
5635 parser->type_definition_forbidden_message
5636 = "types may not be defined in casts";
5637 /* Consume the `('. */
5638 cp_lexer_consume_token (parser->lexer);
5639 /* A very tricky bit is that `(struct S) { 3 }' is a
5640 compound-literal (which we permit in C++ as an extension).
5641 But, that construct is not a cast-expression -- it is a
5642 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5643 is legal; if the compound-literal were a cast-expression,
5644 you'd need an extra set of parentheses.) But, if we parse
5645 the type-id, and it happens to be a class-specifier, then we
5646 will commit to the parse at that point, because we cannot
5647 undo the action that is done when creating a new class. So,
5648 then we cannot back up and do a postfix-expression.
5650 Therefore, we scan ahead to the closing `)', and check to see
5651 if the token after the `)' is a `{'. If so, we are not
5652 looking at a cast-expression.
5654 Save tokens so that we can put them back. */
5655 cp_lexer_save_tokens (parser->lexer);
5656 /* Skip tokens until the next token is a closing parenthesis.
5657 If we find the closing `)', and the next token is a `{', then
5658 we are looking at a compound-literal. */
5660 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5661 /*consume_paren=*/true)
5662 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5663 /* Roll back the tokens we skipped. */
5664 cp_lexer_rollback_tokens (parser->lexer);
5665 /* If we were looking at a compound-literal, simulate an error
5666 so that the call to cp_parser_parse_definitely below will
5668 if (compound_literal_p)
5669 cp_parser_simulate_error (parser);
5672 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5673 parser->in_type_id_in_expr_p = true;
5674 /* Look for the type-id. */
5675 type = cp_parser_type_id (parser);
5676 /* Look for the closing `)'. */
5677 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5678 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5681 /* Restore the saved message. */
5682 parser->type_definition_forbidden_message = saved_message;
5684 /* If ok so far, parse the dependent expression. We cannot be
5685 sure it is a cast. Consider `(T ())'. It is a parenthesized
5686 ctor of T, but looks like a cast to function returning T
5687 without a dependent expression. */
5688 if (!cp_parser_error_occurred (parser))
5689 expr = cp_parser_cast_expression (parser,
5690 /*address_p=*/false,
5693 if (cp_parser_parse_definitely (parser))
5695 /* Warn about old-style casts, if so requested. */
5696 if (warn_old_style_cast
5697 && !in_system_header
5698 && !VOID_TYPE_P (type)
5699 && current_lang_name != lang_name_c)
5700 warning (OPT_Wold_style_cast, "use of old-style cast");
5702 /* Only type conversions to integral or enumeration types
5703 can be used in constant-expressions. */
5704 if (!cast_valid_in_integral_constant_expression_p (type)
5705 && (cp_parser_non_integral_constant_expression
5707 "a cast to a type other than an integral or "
5708 "enumeration type")))
5709 return error_mark_node;
5711 /* Perform the cast. */
5712 expr = build_c_cast (type, expr);
5717 /* If we get here, then it's not a cast, so it must be a
5718 unary-expression. */
5719 return cp_parser_unary_expression (parser, address_p, cast_p);
5722 /* Parse a binary expression of the general form:
5726 pm-expression .* cast-expression
5727 pm-expression ->* cast-expression
5729 multiplicative-expression:
5731 multiplicative-expression * pm-expression
5732 multiplicative-expression / pm-expression
5733 multiplicative-expression % pm-expression
5735 additive-expression:
5736 multiplicative-expression
5737 additive-expression + multiplicative-expression
5738 additive-expression - multiplicative-expression
5742 shift-expression << additive-expression
5743 shift-expression >> additive-expression
5745 relational-expression:
5747 relational-expression < shift-expression
5748 relational-expression > shift-expression
5749 relational-expression <= shift-expression
5750 relational-expression >= shift-expression
5754 relational-expression:
5755 relational-expression <? shift-expression
5756 relational-expression >? shift-expression
5758 equality-expression:
5759 relational-expression
5760 equality-expression == relational-expression
5761 equality-expression != relational-expression
5765 and-expression & equality-expression
5767 exclusive-or-expression:
5769 exclusive-or-expression ^ and-expression
5771 inclusive-or-expression:
5772 exclusive-or-expression
5773 inclusive-or-expression | exclusive-or-expression
5775 logical-and-expression:
5776 inclusive-or-expression
5777 logical-and-expression && inclusive-or-expression
5779 logical-or-expression:
5780 logical-and-expression
5781 logical-or-expression || logical-and-expression
5783 All these are implemented with a single function like:
5786 simple-cast-expression
5787 binary-expression <token> binary-expression
5789 CAST_P is true if this expression is the target of a cast.
5791 The binops_by_token map is used to get the tree codes for each <token> type.
5792 binary-expressions are associated according to a precedence table. */
5794 #define TOKEN_PRECEDENCE(token) \
5795 ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5796 ? PREC_NOT_OPERATOR \
5797 : binops_by_token[token->type].prec)
5800 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5802 cp_parser_expression_stack stack;
5803 cp_parser_expression_stack_entry *sp = &stack[0];
5806 enum tree_code tree_type, lhs_type, rhs_type;
5807 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5810 /* Parse the first expression. */
5811 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5812 lhs_type = ERROR_MARK;
5816 /* Get an operator token. */
5817 token = cp_lexer_peek_token (parser->lexer);
5819 new_prec = TOKEN_PRECEDENCE (token);
5821 /* Popping an entry off the stack means we completed a subexpression:
5822 - either we found a token which is not an operator (`>' where it is not
5823 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5824 will happen repeatedly;
5825 - or, we found an operator which has lower priority. This is the case
5826 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5828 if (new_prec <= prec)
5837 tree_type = binops_by_token[token->type].tree_type;
5839 /* We used the operator token. */
5840 cp_lexer_consume_token (parser->lexer);
5842 /* Extract another operand. It may be the RHS of this expression
5843 or the LHS of a new, higher priority expression. */
5844 rhs = cp_parser_simple_cast_expression (parser);
5845 rhs_type = ERROR_MARK;
5847 /* Get another operator token. Look up its precedence to avoid
5848 building a useless (immediately popped) stack entry for common
5849 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5850 token = cp_lexer_peek_token (parser->lexer);
5851 lookahead_prec = TOKEN_PRECEDENCE (token);
5852 if (lookahead_prec > new_prec)
5854 /* ... and prepare to parse the RHS of the new, higher priority
5855 expression. Since precedence levels on the stack are
5856 monotonically increasing, we do not have to care about
5859 sp->tree_type = tree_type;
5861 sp->lhs_type = lhs_type;
5864 lhs_type = rhs_type;
5866 new_prec = lookahead_prec;
5870 /* If the stack is not empty, we have parsed into LHS the right side
5871 (`4' in the example above) of an expression we had suspended.
5872 We can use the information on the stack to recover the LHS (`3')
5873 from the stack together with the tree code (`MULT_EXPR'), and
5874 the precedence of the higher level subexpression
5875 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5876 which will be used to actually build the additive expression. */
5879 tree_type = sp->tree_type;
5881 rhs_type = lhs_type;
5883 lhs_type = sp->lhs_type;
5886 overloaded_p = false;
5887 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5889 lhs_type = tree_type;
5891 /* If the binary operator required the use of an overloaded operator,
5892 then this expression cannot be an integral constant-expression.
5893 An overloaded operator can be used even if both operands are
5894 otherwise permissible in an integral constant-expression if at
5895 least one of the operands is of enumeration type. */
5898 && (cp_parser_non_integral_constant_expression
5899 (parser, "calls to overloaded operators")))
5900 return error_mark_node;
5907 /* Parse the `? expression : assignment-expression' part of a
5908 conditional-expression. The LOGICAL_OR_EXPR is the
5909 logical-or-expression that started the conditional-expression.
5910 Returns a representation of the entire conditional-expression.
5912 This routine is used by cp_parser_assignment_expression.
5914 ? expression : assignment-expression
5918 ? : assignment-expression */
5921 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5924 tree assignment_expr;
5926 /* Consume the `?' token. */
5927 cp_lexer_consume_token (parser->lexer);
5928 if (cp_parser_allow_gnu_extensions_p (parser)
5929 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5930 /* Implicit true clause. */
5933 /* Parse the expression. */
5934 expr = cp_parser_expression (parser, /*cast_p=*/false);
5936 /* The next token should be a `:'. */
5937 cp_parser_require (parser, CPP_COLON, "`:'");
5938 /* Parse the assignment-expression. */
5939 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5941 /* Build the conditional-expression. */
5942 return build_x_conditional_expr (logical_or_expr,
5947 /* Parse an assignment-expression.
5949 assignment-expression:
5950 conditional-expression
5951 logical-or-expression assignment-operator assignment_expression
5954 CAST_P is true if this expression is the target of a cast.
5956 Returns a representation for the expression. */
5959 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5963 /* If the next token is the `throw' keyword, then we're looking at
5964 a throw-expression. */
5965 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5966 expr = cp_parser_throw_expression (parser);
5967 /* Otherwise, it must be that we are looking at a
5968 logical-or-expression. */
5971 /* Parse the binary expressions (logical-or-expression). */
5972 expr = cp_parser_binary_expression (parser, cast_p);
5973 /* If the next token is a `?' then we're actually looking at a
5974 conditional-expression. */
5975 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5976 return cp_parser_question_colon_clause (parser, expr);
5979 enum tree_code assignment_operator;
5981 /* If it's an assignment-operator, we're using the second
5984 = cp_parser_assignment_operator_opt (parser);
5985 if (assignment_operator != ERROR_MARK)
5989 /* Parse the right-hand side of the assignment. */
5990 rhs = cp_parser_assignment_expression (parser, cast_p);
5991 /* An assignment may not appear in a
5992 constant-expression. */
5993 if (cp_parser_non_integral_constant_expression (parser,
5995 return error_mark_node;
5996 /* Build the assignment expression. */
5997 expr = build_x_modify_expr (expr,
5998 assignment_operator,
6007 /* Parse an (optional) assignment-operator.
6009 assignment-operator: one of
6010 = *= /= %= += -= >>= <<= &= ^= |=
6014 assignment-operator: one of
6017 If the next token is an assignment operator, the corresponding tree
6018 code is returned, and the token is consumed. For example, for
6019 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6020 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6021 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6022 operator, ERROR_MARK is returned. */
6024 static enum tree_code
6025 cp_parser_assignment_operator_opt (cp_parser* parser)
6030 /* Peek at the next toen. */
6031 token = cp_lexer_peek_token (parser->lexer);
6033 switch (token->type)
6044 op = TRUNC_DIV_EXPR;
6048 op = TRUNC_MOD_EXPR;
6080 /* Nothing else is an assignment operator. */
6084 /* If it was an assignment operator, consume it. */
6085 if (op != ERROR_MARK)
6086 cp_lexer_consume_token (parser->lexer);
6091 /* Parse an expression.
6094 assignment-expression
6095 expression , assignment-expression
6097 CAST_P is true if this expression is the target of a cast.
6099 Returns a representation of the expression. */
6102 cp_parser_expression (cp_parser* parser, bool cast_p)
6104 tree expression = NULL_TREE;
6108 tree assignment_expression;
6110 /* Parse the next assignment-expression. */
6111 assignment_expression
6112 = cp_parser_assignment_expression (parser, cast_p);
6113 /* If this is the first assignment-expression, we can just
6116 expression = assignment_expression;
6118 expression = build_x_compound_expr (expression,
6119 assignment_expression);
6120 /* If the next token is not a comma, then we are done with the
6122 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6124 /* Consume the `,'. */
6125 cp_lexer_consume_token (parser->lexer);
6126 /* A comma operator cannot appear in a constant-expression. */
6127 if (cp_parser_non_integral_constant_expression (parser,
6128 "a comma operator"))
6129 expression = error_mark_node;
6135 /* Parse a constant-expression.
6137 constant-expression:
6138 conditional-expression
6140 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6141 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6142 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6143 is false, NON_CONSTANT_P should be NULL. */
6146 cp_parser_constant_expression (cp_parser* parser,
6147 bool allow_non_constant_p,
6148 bool *non_constant_p)
6150 bool saved_integral_constant_expression_p;
6151 bool saved_allow_non_integral_constant_expression_p;
6152 bool saved_non_integral_constant_expression_p;
6155 /* It might seem that we could simply parse the
6156 conditional-expression, and then check to see if it were
6157 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6158 one that the compiler can figure out is constant, possibly after
6159 doing some simplifications or optimizations. The standard has a
6160 precise definition of constant-expression, and we must honor
6161 that, even though it is somewhat more restrictive.
6167 is not a legal declaration, because `(2, 3)' is not a
6168 constant-expression. The `,' operator is forbidden in a
6169 constant-expression. However, GCC's constant-folding machinery
6170 will fold this operation to an INTEGER_CST for `3'. */
6172 /* Save the old settings. */
6173 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6174 saved_allow_non_integral_constant_expression_p
6175 = parser->allow_non_integral_constant_expression_p;
6176 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6177 /* We are now parsing a constant-expression. */
6178 parser->integral_constant_expression_p = true;
6179 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6180 parser->non_integral_constant_expression_p = false;
6181 /* Although the grammar says "conditional-expression", we parse an
6182 "assignment-expression", which also permits "throw-expression"
6183 and the use of assignment operators. In the case that
6184 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6185 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6186 actually essential that we look for an assignment-expression.
6187 For example, cp_parser_initializer_clauses uses this function to
6188 determine whether a particular assignment-expression is in fact
6190 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6191 /* Restore the old settings. */
6192 parser->integral_constant_expression_p
6193 = saved_integral_constant_expression_p;
6194 parser->allow_non_integral_constant_expression_p
6195 = saved_allow_non_integral_constant_expression_p;
6196 if (allow_non_constant_p)
6197 *non_constant_p = parser->non_integral_constant_expression_p;
6198 else if (parser->non_integral_constant_expression_p)
6199 expression = error_mark_node;
6200 parser->non_integral_constant_expression_p
6201 = saved_non_integral_constant_expression_p;
6206 /* Parse __builtin_offsetof.
6208 offsetof-expression:
6209 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6211 offsetof-member-designator:
6213 | offsetof-member-designator "." id-expression
6214 | offsetof-member-designator "[" expression "]" */
6217 cp_parser_builtin_offsetof (cp_parser *parser)
6219 int save_ice_p, save_non_ice_p;
6223 /* We're about to accept non-integral-constant things, but will
6224 definitely yield an integral constant expression. Save and
6225 restore these values around our local parsing. */
6226 save_ice_p = parser->integral_constant_expression_p;
6227 save_non_ice_p = parser->non_integral_constant_expression_p;
6229 /* Consume the "__builtin_offsetof" token. */
6230 cp_lexer_consume_token (parser->lexer);
6231 /* Consume the opening `('. */
6232 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6233 /* Parse the type-id. */
6234 type = cp_parser_type_id (parser);
6235 /* Look for the `,'. */
6236 cp_parser_require (parser, CPP_COMMA, "`,'");
6238 /* Build the (type *)null that begins the traditional offsetof macro. */
6239 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6241 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6242 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6246 cp_token *token = cp_lexer_peek_token (parser->lexer);
6247 switch (token->type)
6249 case CPP_OPEN_SQUARE:
6250 /* offsetof-member-designator "[" expression "]" */
6251 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6255 /* offsetof-member-designator "." identifier */
6256 cp_lexer_consume_token (parser->lexer);
6257 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6261 case CPP_CLOSE_PAREN:
6262 /* Consume the ")" token. */
6263 cp_lexer_consume_token (parser->lexer);
6267 /* Error. We know the following require will fail, but
6268 that gives the proper error message. */
6269 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6270 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6271 expr = error_mark_node;
6277 /* If we're processing a template, we can't finish the semantics yet.
6278 Otherwise we can fold the entire expression now. */
6279 if (processing_template_decl)
6280 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6282 expr = finish_offsetof (expr);
6285 parser->integral_constant_expression_p = save_ice_p;
6286 parser->non_integral_constant_expression_p = save_non_ice_p;
6291 /* Statements [gram.stmt.stmt] */
6293 /* Parse a statement.
6297 expression-statement
6302 declaration-statement
6305 IN_COMPOUND is true when the statement is nested inside a
6306 cp_parser_compound_statement; this matters for certain pragmas.
6308 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6309 is a (possibly labeled) if statement which is not enclosed in braces
6310 and has an else clause. This is used to implement -Wparentheses. */
6313 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6314 bool in_compound, bool *if_p)
6318 location_t statement_location;
6323 /* There is no statement yet. */
6324 statement = NULL_TREE;
6325 /* Peek at the next token. */
6326 token = cp_lexer_peek_token (parser->lexer);
6327 /* Remember the location of the first token in the statement. */
6328 statement_location = token->location;
6329 /* If this is a keyword, then that will often determine what kind of
6330 statement we have. */
6331 if (token->type == CPP_KEYWORD)
6333 enum rid keyword = token->keyword;
6339 /* Looks like a labeled-statement with a case label.
6340 Parse the label, and then use tail recursion to parse
6342 cp_parser_label_for_labeled_statement (parser);
6347 statement = cp_parser_selection_statement (parser, if_p);
6353 statement = cp_parser_iteration_statement (parser);
6360 statement = cp_parser_jump_statement (parser);
6363 /* Objective-C++ exception-handling constructs. */
6366 case RID_AT_FINALLY:
6367 case RID_AT_SYNCHRONIZED:
6369 statement = cp_parser_objc_statement (parser);
6373 statement = cp_parser_try_block (parser);
6377 /* It might be a keyword like `int' that can start a
6378 declaration-statement. */
6382 else if (token->type == CPP_NAME)
6384 /* If the next token is a `:', then we are looking at a
6385 labeled-statement. */
6386 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6387 if (token->type == CPP_COLON)
6389 /* Looks like a labeled-statement with an ordinary label.
6390 Parse the label, and then use tail recursion to parse
6392 cp_parser_label_for_labeled_statement (parser);
6396 /* Anything that starts with a `{' must be a compound-statement. */
6397 else if (token->type == CPP_OPEN_BRACE)
6398 statement = cp_parser_compound_statement (parser, NULL, false);
6399 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6400 a statement all its own. */
6401 else if (token->type == CPP_PRAGMA)
6403 /* Only certain OpenMP pragmas are attached to statements, and thus
6404 are considered statements themselves. All others are not. In
6405 the context of a compound, accept the pragma as a "statement" and
6406 return so that we can check for a close brace. Otherwise we
6407 require a real statement and must go back and read one. */
6409 cp_parser_pragma (parser, pragma_compound);
6410 else if (!cp_parser_pragma (parser, pragma_stmt))
6414 else if (token->type == CPP_EOF)
6416 cp_parser_error (parser, "expected statement");
6420 /* Everything else must be a declaration-statement or an
6421 expression-statement. Try for the declaration-statement
6422 first, unless we are looking at a `;', in which case we know that
6423 we have an expression-statement. */
6426 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6428 cp_parser_parse_tentatively (parser);
6429 /* Try to parse the declaration-statement. */
6430 cp_parser_declaration_statement (parser);
6431 /* If that worked, we're done. */
6432 if (cp_parser_parse_definitely (parser))
6435 /* Look for an expression-statement instead. */
6436 statement = cp_parser_expression_statement (parser, in_statement_expr);
6439 /* Set the line number for the statement. */
6440 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6441 SET_EXPR_LOCATION (statement, statement_location);
6444 /* Parse the label for a labeled-statement, i.e.
6447 case constant-expression :
6451 case constant-expression ... constant-expression : statement
6453 When a label is parsed without errors, the label is added to the
6454 parse tree by the finish_* functions, so this function doesn't
6455 have to return the label. */
6458 cp_parser_label_for_labeled_statement (cp_parser* parser)
6462 /* The next token should be an identifier. */
6463 token = cp_lexer_peek_token (parser->lexer);
6464 if (token->type != CPP_NAME
6465 && token->type != CPP_KEYWORD)
6467 cp_parser_error (parser, "expected labeled-statement");
6471 switch (token->keyword)
6478 /* Consume the `case' token. */
6479 cp_lexer_consume_token (parser->lexer);
6480 /* Parse the constant-expression. */
6481 expr = cp_parser_constant_expression (parser,
6482 /*allow_non_constant_p=*/false,
6485 ellipsis = cp_lexer_peek_token (parser->lexer);
6486 if (ellipsis->type == CPP_ELLIPSIS)
6488 /* Consume the `...' token. */
6489 cp_lexer_consume_token (parser->lexer);
6491 cp_parser_constant_expression (parser,
6492 /*allow_non_constant_p=*/false,
6494 /* We don't need to emit warnings here, as the common code
6495 will do this for us. */
6498 expr_hi = NULL_TREE;
6500 if (parser->in_switch_statement_p)
6501 finish_case_label (expr, expr_hi);
6503 error ("case label %qE not within a switch statement", expr);
6508 /* Consume the `default' token. */
6509 cp_lexer_consume_token (parser->lexer);
6511 if (parser->in_switch_statement_p)
6512 finish_case_label (NULL_TREE, NULL_TREE);
6514 error ("case label not within a switch statement");
6518 /* Anything else must be an ordinary label. */
6519 finish_label_stmt (cp_parser_identifier (parser));
6523 /* Require the `:' token. */
6524 cp_parser_require (parser, CPP_COLON, "`:'");
6527 /* Parse an expression-statement.
6529 expression-statement:
6532 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6533 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6534 indicates whether this expression-statement is part of an
6535 expression statement. */
6538 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6540 tree statement = NULL_TREE;
6542 /* If the next token is a ';', then there is no expression
6544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6545 statement = cp_parser_expression (parser, /*cast_p=*/false);
6547 /* Consume the final `;'. */
6548 cp_parser_consume_semicolon_at_end_of_statement (parser);
6550 if (in_statement_expr
6551 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6552 /* This is the final expression statement of a statement
6554 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6556 statement = finish_expr_stmt (statement);
6563 /* Parse a compound-statement.
6566 { statement-seq [opt] }
6568 Returns a tree representing the statement. */
6571 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6576 /* Consume the `{'. */
6577 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6578 return error_mark_node;
6579 /* Begin the compound-statement. */
6580 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6581 /* Parse an (optional) statement-seq. */
6582 cp_parser_statement_seq_opt (parser, in_statement_expr);
6583 /* Finish the compound-statement. */
6584 finish_compound_stmt (compound_stmt);
6585 /* Consume the `}'. */
6586 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6588 return compound_stmt;
6591 /* Parse an (optional) statement-seq.
6595 statement-seq [opt] statement */
6598 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6600 /* Scan statements until there aren't any more. */
6603 cp_token *token = cp_lexer_peek_token (parser->lexer);
6605 /* If we're looking at a `}', then we've run out of statements. */
6606 if (token->type == CPP_CLOSE_BRACE
6607 || token->type == CPP_EOF
6608 || token->type == CPP_PRAGMA_EOL)
6611 /* If we are in a compound statement and find 'else' then
6612 something went wrong. */
6613 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6615 if (parser->in_statement & IN_IF_STMT)
6619 token = cp_lexer_consume_token (parser->lexer);
6620 error ("%<else%> without a previous %<if%>");
6624 /* Parse the statement. */
6625 cp_parser_statement (parser, in_statement_expr, true, NULL);
6629 /* Parse a selection-statement.
6631 selection-statement:
6632 if ( condition ) statement
6633 if ( condition ) statement else statement
6634 switch ( condition ) statement
6636 Returns the new IF_STMT or SWITCH_STMT.
6638 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6639 is a (possibly labeled) if statement which is not enclosed in
6640 braces and has an else clause. This is used to implement
6644 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6652 /* Peek at the next token. */
6653 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6655 /* See what kind of keyword it is. */
6656 keyword = token->keyword;
6665 /* Look for the `('. */
6666 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6668 cp_parser_skip_to_end_of_statement (parser);
6669 return error_mark_node;
6672 /* Begin the selection-statement. */
6673 if (keyword == RID_IF)
6674 statement = begin_if_stmt ();
6676 statement = begin_switch_stmt ();
6678 /* Parse the condition. */
6679 condition = cp_parser_condition (parser);
6680 /* Look for the `)'. */
6681 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6682 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6683 /*consume_paren=*/true);
6685 if (keyword == RID_IF)
6688 unsigned char in_statement;
6690 /* Add the condition. */
6691 finish_if_stmt_cond (condition, statement);
6693 /* Parse the then-clause. */
6694 in_statement = parser->in_statement;
6695 parser->in_statement |= IN_IF_STMT;
6696 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6697 parser->in_statement = in_statement;
6699 finish_then_clause (statement);
6701 /* If the next token is `else', parse the else-clause. */
6702 if (cp_lexer_next_token_is_keyword (parser->lexer,
6705 /* Consume the `else' keyword. */
6706 cp_lexer_consume_token (parser->lexer);
6707 begin_else_clause (statement);
6708 /* Parse the else-clause. */
6709 cp_parser_implicitly_scoped_statement (parser, NULL);
6710 finish_else_clause (statement);
6712 /* If we are currently parsing a then-clause, then
6713 IF_P will not be NULL. We set it to true to
6714 indicate that this if statement has an else clause.
6715 This may trigger the Wparentheses warning below
6716 when we get back up to the parent if statement. */
6722 /* This if statement does not have an else clause. If
6723 NESTED_IF is true, then the then-clause is an if
6724 statement which does have an else clause. We warn
6725 about the potential ambiguity. */
6727 warning (OPT_Wparentheses,
6728 ("%Hsuggest explicit braces "
6729 "to avoid ambiguous %<else%>"),
6730 EXPR_LOCUS (statement));
6733 /* Now we're all done with the if-statement. */
6734 finish_if_stmt (statement);
6738 bool in_switch_statement_p;
6739 unsigned char in_statement;
6741 /* Add the condition. */
6742 finish_switch_cond (condition, statement);
6744 /* Parse the body of the switch-statement. */
6745 in_switch_statement_p = parser->in_switch_statement_p;
6746 in_statement = parser->in_statement;
6747 parser->in_switch_statement_p = true;
6748 parser->in_statement |= IN_SWITCH_STMT;
6749 cp_parser_implicitly_scoped_statement (parser, NULL);
6750 parser->in_switch_statement_p = in_switch_statement_p;
6751 parser->in_statement = in_statement;
6753 /* Now we're all done with the switch-statement. */
6754 finish_switch_stmt (statement);
6762 cp_parser_error (parser, "expected selection-statement");
6763 return error_mark_node;
6767 /* Parse a condition.
6771 type-specifier-seq declarator = assignment-expression
6776 type-specifier-seq declarator asm-specification [opt]
6777 attributes [opt] = assignment-expression
6779 Returns the expression that should be tested. */
6782 cp_parser_condition (cp_parser* parser)
6784 cp_decl_specifier_seq type_specifiers;
6785 const char *saved_message;
6787 /* Try the declaration first. */
6788 cp_parser_parse_tentatively (parser);
6789 /* New types are not allowed in the type-specifier-seq for a
6791 saved_message = parser->type_definition_forbidden_message;
6792 parser->type_definition_forbidden_message
6793 = "types may not be defined in conditions";
6794 /* Parse the type-specifier-seq. */
6795 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6797 /* Restore the saved message. */
6798 parser->type_definition_forbidden_message = saved_message;
6799 /* If all is well, we might be looking at a declaration. */
6800 if (!cp_parser_error_occurred (parser))
6803 tree asm_specification;
6805 cp_declarator *declarator;
6806 tree initializer = NULL_TREE;
6808 /* Parse the declarator. */
6809 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6810 /*ctor_dtor_or_conv_p=*/NULL,
6811 /*parenthesized_p=*/NULL,
6812 /*member_p=*/false);
6813 /* Parse the attributes. */
6814 attributes = cp_parser_attributes_opt (parser);
6815 /* Parse the asm-specification. */
6816 asm_specification = cp_parser_asm_specification_opt (parser);
6817 /* If the next token is not an `=', then we might still be
6818 looking at an expression. For example:
6822 looks like a decl-specifier-seq and a declarator -- but then
6823 there is no `=', so this is an expression. */
6824 cp_parser_require (parser, CPP_EQ, "`='");
6825 /* If we did see an `=', then we are looking at a declaration
6827 if (cp_parser_parse_definitely (parser))
6830 bool non_constant_p;
6832 /* Create the declaration. */
6833 decl = start_decl (declarator, &type_specifiers,
6834 /*initialized_p=*/true,
6835 attributes, /*prefix_attributes=*/NULL_TREE,
6837 /* Parse the assignment-expression. */
6839 = cp_parser_constant_expression (parser,
6840 /*allow_non_constant_p=*/true,
6842 if (!non_constant_p)
6843 initializer = fold_non_dependent_expr (initializer);
6845 /* Process the initializer. */
6846 cp_finish_decl (decl,
6847 initializer, !non_constant_p,
6849 LOOKUP_ONLYCONVERTING);
6852 pop_scope (pushed_scope);
6854 return convert_from_reference (decl);
6857 /* If we didn't even get past the declarator successfully, we are
6858 definitely not looking at a declaration. */
6860 cp_parser_abort_tentative_parse (parser);
6862 /* Otherwise, we are looking at an expression. */
6863 return cp_parser_expression (parser, /*cast_p=*/false);
6866 /* Parse an iteration-statement.
6868 iteration-statement:
6869 while ( condition ) statement
6870 do statement while ( expression ) ;
6871 for ( for-init-statement condition [opt] ; expression [opt] )
6874 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
6877 cp_parser_iteration_statement (cp_parser* parser)
6882 unsigned char in_statement;
6884 /* Peek at the next token. */
6885 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6887 return error_mark_node;
6889 /* Remember whether or not we are already within an iteration
6891 in_statement = parser->in_statement;
6893 /* See what kind of keyword it is. */
6894 keyword = token->keyword;
6901 /* Begin the while-statement. */
6902 statement = begin_while_stmt ();
6903 /* Look for the `('. */
6904 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6905 /* Parse the condition. */
6906 condition = cp_parser_condition (parser);
6907 finish_while_stmt_cond (condition, statement);
6908 /* Look for the `)'. */
6909 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6910 /* Parse the dependent statement. */
6911 parser->in_statement = IN_ITERATION_STMT;
6912 cp_parser_already_scoped_statement (parser);
6913 parser->in_statement = in_statement;
6914 /* We're done with the while-statement. */
6915 finish_while_stmt (statement);
6923 /* Begin the do-statement. */
6924 statement = begin_do_stmt ();
6925 /* Parse the body of the do-statement. */
6926 parser->in_statement = IN_ITERATION_STMT;
6927 cp_parser_implicitly_scoped_statement (parser, NULL);
6928 parser->in_statement = in_statement;
6929 finish_do_body (statement);
6930 /* Look for the `while' keyword. */
6931 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6932 /* Look for the `('. */
6933 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6934 /* Parse the expression. */
6935 expression = cp_parser_expression (parser, /*cast_p=*/false);
6936 /* We're done with the do-statement. */
6937 finish_do_stmt (expression, statement);
6938 /* Look for the `)'. */
6939 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6940 /* Look for the `;'. */
6941 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6947 tree condition = NULL_TREE;
6948 tree expression = NULL_TREE;
6950 /* Begin the for-statement. */
6951 statement = begin_for_stmt ();
6952 /* Look for the `('. */
6953 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6954 /* Parse the initialization. */
6955 cp_parser_for_init_statement (parser);
6956 finish_for_init_stmt (statement);
6958 /* If there's a condition, process it. */
6959 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6960 condition = cp_parser_condition (parser);
6961 finish_for_cond (condition, statement);
6962 /* Look for the `;'. */
6963 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6965 /* If there's an expression, process it. */
6966 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6967 expression = cp_parser_expression (parser, /*cast_p=*/false);
6968 finish_for_expr (expression, statement);
6969 /* Look for the `)'. */
6970 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6972 /* Parse the body of the for-statement. */
6973 parser->in_statement = IN_ITERATION_STMT;
6974 cp_parser_already_scoped_statement (parser);
6975 parser->in_statement = in_statement;
6977 /* We're done with the for-statement. */
6978 finish_for_stmt (statement);
6983 cp_parser_error (parser, "expected iteration-statement");
6984 statement = error_mark_node;
6991 /* Parse a for-init-statement.
6994 expression-statement
6995 simple-declaration */
6998 cp_parser_for_init_statement (cp_parser* parser)
7000 /* If the next token is a `;', then we have an empty
7001 expression-statement. Grammatically, this is also a
7002 simple-declaration, but an invalid one, because it does not
7003 declare anything. Therefore, if we did not handle this case
7004 specially, we would issue an error message about an invalid
7006 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7008 /* We're going to speculatively look for a declaration, falling back
7009 to an expression, if necessary. */
7010 cp_parser_parse_tentatively (parser);
7011 /* Parse the declaration. */
7012 cp_parser_simple_declaration (parser,
7013 /*function_definition_allowed_p=*/false);
7014 /* If the tentative parse failed, then we shall need to look for an
7015 expression-statement. */
7016 if (cp_parser_parse_definitely (parser))
7020 cp_parser_expression_statement (parser, false);
7023 /* Parse a jump-statement.
7028 return expression [opt] ;
7036 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7039 cp_parser_jump_statement (cp_parser* parser)
7041 tree statement = error_mark_node;
7044 unsigned char in_statement;
7046 /* Peek at the next token. */
7047 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7049 return error_mark_node;
7051 /* See what kind of keyword it is. */
7052 keyword = token->keyword;
7056 in_statement = parser->in_statement & ~IN_IF_STMT;
7057 switch (in_statement)
7060 error ("break statement not within loop or switch");
7063 gcc_assert ((in_statement & IN_SWITCH_STMT)
7064 || in_statement == IN_ITERATION_STMT);
7065 statement = finish_break_stmt ();
7068 error ("invalid exit from OpenMP structured block");
7071 error ("break statement used with OpenMP for loop");
7074 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7078 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7081 error ("continue statement not within a loop");
7083 case IN_ITERATION_STMT:
7085 statement = finish_continue_stmt ();
7088 error ("invalid exit from OpenMP structured block");
7093 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7100 /* If the next token is a `;', then there is no
7102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7103 expr = cp_parser_expression (parser, /*cast_p=*/false);
7106 /* Build the return-statement. */
7107 statement = finish_return_stmt (expr);
7108 /* Look for the final `;'. */
7109 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7114 /* Create the goto-statement. */
7115 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7117 /* Issue a warning about this use of a GNU extension. */
7119 pedwarn ("ISO C++ forbids computed gotos");
7120 /* Consume the '*' token. */
7121 cp_lexer_consume_token (parser->lexer);
7122 /* Parse the dependent expression. */
7123 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7126 finish_goto_stmt (cp_parser_identifier (parser));
7127 /* Look for the final `;'. */
7128 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7132 cp_parser_error (parser, "expected jump-statement");
7139 /* Parse a declaration-statement.
7141 declaration-statement:
7142 block-declaration */
7145 cp_parser_declaration_statement (cp_parser* parser)
7149 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7150 p = obstack_alloc (&declarator_obstack, 0);
7152 /* Parse the block-declaration. */
7153 cp_parser_block_declaration (parser, /*statement_p=*/true);
7155 /* Free any declarators allocated. */
7156 obstack_free (&declarator_obstack, p);
7158 /* Finish off the statement. */
7162 /* Some dependent statements (like `if (cond) statement'), are
7163 implicitly in their own scope. In other words, if the statement is
7164 a single statement (as opposed to a compound-statement), it is
7165 none-the-less treated as if it were enclosed in braces. Any
7166 declarations appearing in the dependent statement are out of scope
7167 after control passes that point. This function parses a statement,
7168 but ensures that is in its own scope, even if it is not a
7171 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7172 is a (possibly labeled) if statement which is not enclosed in
7173 braces and has an else clause. This is used to implement
7176 Returns the new statement. */
7179 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7186 /* Mark if () ; with a special NOP_EXPR. */
7187 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7189 cp_lexer_consume_token (parser->lexer);
7190 statement = add_stmt (build_empty_stmt ());
7192 /* if a compound is opened, we simply parse the statement directly. */
7193 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7194 statement = cp_parser_compound_statement (parser, NULL, false);
7195 /* If the token is not a `{', then we must take special action. */
7198 /* Create a compound-statement. */
7199 statement = begin_compound_stmt (0);
7200 /* Parse the dependent-statement. */
7201 cp_parser_statement (parser, NULL_TREE, false, if_p);
7202 /* Finish the dummy compound-statement. */
7203 finish_compound_stmt (statement);
7206 /* Return the statement. */
7210 /* For some dependent statements (like `while (cond) statement'), we
7211 have already created a scope. Therefore, even if the dependent
7212 statement is a compound-statement, we do not want to create another
7216 cp_parser_already_scoped_statement (cp_parser* parser)
7218 /* If the token is a `{', then we must take special action. */
7219 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7220 cp_parser_statement (parser, NULL_TREE, false, NULL);
7223 /* Avoid calling cp_parser_compound_statement, so that we
7224 don't create a new scope. Do everything else by hand. */
7225 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7226 cp_parser_statement_seq_opt (parser, NULL_TREE);
7227 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7231 /* Declarations [gram.dcl.dcl] */
7233 /* Parse an optional declaration-sequence.
7237 declaration-seq declaration */
7240 cp_parser_declaration_seq_opt (cp_parser* parser)
7246 token = cp_lexer_peek_token (parser->lexer);
7248 if (token->type == CPP_CLOSE_BRACE
7249 || token->type == CPP_EOF
7250 || token->type == CPP_PRAGMA_EOL)
7253 if (token->type == CPP_SEMICOLON)
7255 /* A declaration consisting of a single semicolon is
7256 invalid. Allow it unless we're being pedantic. */
7257 cp_lexer_consume_token (parser->lexer);
7258 if (pedantic && !in_system_header)
7259 pedwarn ("extra %<;%>");
7263 /* If we're entering or exiting a region that's implicitly
7264 extern "C", modify the lang context appropriately. */
7265 if (!parser->implicit_extern_c && token->implicit_extern_c)
7267 push_lang_context (lang_name_c);
7268 parser->implicit_extern_c = true;
7270 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7272 pop_lang_context ();
7273 parser->implicit_extern_c = false;
7276 if (token->type == CPP_PRAGMA)
7278 /* A top-level declaration can consist solely of a #pragma.
7279 A nested declaration cannot, so this is done here and not
7280 in cp_parser_declaration. (A #pragma at block scope is
7281 handled in cp_parser_statement.) */
7282 cp_parser_pragma (parser, pragma_external);
7286 /* Parse the declaration itself. */
7287 cp_parser_declaration (parser);
7291 /* Parse a declaration.
7296 template-declaration
7297 explicit-instantiation
7298 explicit-specialization
7299 linkage-specification
7300 namespace-definition
7305 __extension__ declaration */
7308 cp_parser_declaration (cp_parser* parser)
7315 /* Check for the `__extension__' keyword. */
7316 if (cp_parser_extension_opt (parser, &saved_pedantic))
7318 /* Parse the qualified declaration. */
7319 cp_parser_declaration (parser);
7320 /* Restore the PEDANTIC flag. */
7321 pedantic = saved_pedantic;
7326 /* Try to figure out what kind of declaration is present. */
7327 token1 = *cp_lexer_peek_token (parser->lexer);
7329 if (token1.type != CPP_EOF)
7330 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7333 token2.type = CPP_EOF;
7334 token2.keyword = RID_MAX;
7337 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7338 p = obstack_alloc (&declarator_obstack, 0);
7340 /* If the next token is `extern' and the following token is a string
7341 literal, then we have a linkage specification. */
7342 if (token1.keyword == RID_EXTERN
7343 && cp_parser_is_string_literal (&token2))
7344 cp_parser_linkage_specification (parser);
7345 /* If the next token is `template', then we have either a template
7346 declaration, an explicit instantiation, or an explicit
7348 else if (token1.keyword == RID_TEMPLATE)
7350 /* `template <>' indicates a template specialization. */
7351 if (token2.type == CPP_LESS
7352 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7353 cp_parser_explicit_specialization (parser);
7354 /* `template <' indicates a template declaration. */
7355 else if (token2.type == CPP_LESS)
7356 cp_parser_template_declaration (parser, /*member_p=*/false);
7357 /* Anything else must be an explicit instantiation. */
7359 cp_parser_explicit_instantiation (parser);
7361 /* If the next token is `export', then we have a template
7363 else if (token1.keyword == RID_EXPORT)
7364 cp_parser_template_declaration (parser, /*member_p=*/false);
7365 /* If the next token is `extern', 'static' or 'inline' and the one
7366 after that is `template', we have a GNU extended explicit
7367 instantiation directive. */
7368 else if (cp_parser_allow_gnu_extensions_p (parser)
7369 && (token1.keyword == RID_EXTERN
7370 || token1.keyword == RID_STATIC
7371 || token1.keyword == RID_INLINE)
7372 && token2.keyword == RID_TEMPLATE)
7373 cp_parser_explicit_instantiation (parser);
7374 /* If the next token is `namespace', check for a named or unnamed
7375 namespace definition. */
7376 else if (token1.keyword == RID_NAMESPACE
7377 && (/* A named namespace definition. */
7378 (token2.type == CPP_NAME
7379 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7381 /* An unnamed namespace definition. */
7382 || token2.type == CPP_OPEN_BRACE
7383 || token2.keyword == RID_ATTRIBUTE))
7384 cp_parser_namespace_definition (parser);
7385 /* Objective-C++ declaration/definition. */
7386 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7387 cp_parser_objc_declaration (parser);
7388 /* We must have either a block declaration or a function
7391 /* Try to parse a block-declaration, or a function-definition. */
7392 cp_parser_block_declaration (parser, /*statement_p=*/false);
7394 /* Free any declarators allocated. */
7395 obstack_free (&declarator_obstack, p);
7398 /* Parse a block-declaration.
7403 namespace-alias-definition
7410 __extension__ block-declaration
7416 static_assert-declaration
7418 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7419 part of a declaration-statement. */
7422 cp_parser_block_declaration (cp_parser *parser,
7428 /* Check for the `__extension__' keyword. */
7429 if (cp_parser_extension_opt (parser, &saved_pedantic))
7431 /* Parse the qualified declaration. */
7432 cp_parser_block_declaration (parser, statement_p);
7433 /* Restore the PEDANTIC flag. */
7434 pedantic = saved_pedantic;
7439 /* Peek at the next token to figure out which kind of declaration is
7441 token1 = cp_lexer_peek_token (parser->lexer);
7443 /* If the next keyword is `asm', we have an asm-definition. */
7444 if (token1->keyword == RID_ASM)
7447 cp_parser_commit_to_tentative_parse (parser);
7448 cp_parser_asm_definition (parser);
7450 /* If the next keyword is `namespace', we have a
7451 namespace-alias-definition. */
7452 else if (token1->keyword == RID_NAMESPACE)
7453 cp_parser_namespace_alias_definition (parser);
7454 /* If the next keyword is `using', we have either a
7455 using-declaration or a using-directive. */
7456 else if (token1->keyword == RID_USING)
7461 cp_parser_commit_to_tentative_parse (parser);
7462 /* If the token after `using' is `namespace', then we have a
7464 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7465 if (token2->keyword == RID_NAMESPACE)
7466 cp_parser_using_directive (parser);
7467 /* Otherwise, it's a using-declaration. */
7469 cp_parser_using_declaration (parser,
7470 /*access_declaration_p=*/false);
7472 /* If the next keyword is `__label__' we have a label declaration. */
7473 else if (token1->keyword == RID_LABEL)
7476 cp_parser_commit_to_tentative_parse (parser);
7477 cp_parser_label_declaration (parser);
7479 /* If the next token is `static_assert' we have a static assertion. */
7480 else if (token1->keyword == RID_STATIC_ASSERT)
7481 cp_parser_static_assert (parser, /*member_p=*/false);
7482 /* Anything else must be a simple-declaration. */
7484 cp_parser_simple_declaration (parser, !statement_p);
7487 /* Parse a simple-declaration.
7490 decl-specifier-seq [opt] init-declarator-list [opt] ;
7492 init-declarator-list:
7494 init-declarator-list , init-declarator
7496 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7497 function-definition as a simple-declaration. */
7500 cp_parser_simple_declaration (cp_parser* parser,
7501 bool function_definition_allowed_p)
7503 cp_decl_specifier_seq decl_specifiers;
7504 int declares_class_or_enum;
7505 bool saw_declarator;
7507 /* Defer access checks until we know what is being declared; the
7508 checks for names appearing in the decl-specifier-seq should be
7509 done as if we were in the scope of the thing being declared. */
7510 push_deferring_access_checks (dk_deferred);
7512 /* Parse the decl-specifier-seq. We have to keep track of whether
7513 or not the decl-specifier-seq declares a named class or
7514 enumeration type, since that is the only case in which the
7515 init-declarator-list is allowed to be empty.
7519 In a simple-declaration, the optional init-declarator-list can be
7520 omitted only when declaring a class or enumeration, that is when
7521 the decl-specifier-seq contains either a class-specifier, an
7522 elaborated-type-specifier, or an enum-specifier. */
7523 cp_parser_decl_specifier_seq (parser,
7524 CP_PARSER_FLAGS_OPTIONAL,
7526 &declares_class_or_enum);
7527 /* We no longer need to defer access checks. */
7528 stop_deferring_access_checks ();
7530 /* In a block scope, a valid declaration must always have a
7531 decl-specifier-seq. By not trying to parse declarators, we can
7532 resolve the declaration/expression ambiguity more quickly. */
7533 if (!function_definition_allowed_p
7534 && !decl_specifiers.any_specifiers_p)
7536 cp_parser_error (parser, "expected declaration");
7540 /* If the next two tokens are both identifiers, the code is
7541 erroneous. The usual cause of this situation is code like:
7545 where "T" should name a type -- but does not. */
7546 if (!decl_specifiers.type
7547 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7549 /* If parsing tentatively, we should commit; we really are
7550 looking at a declaration. */
7551 cp_parser_commit_to_tentative_parse (parser);
7556 /* If we have seen at least one decl-specifier, and the next token
7557 is not a parenthesis, then we must be looking at a declaration.
7558 (After "int (" we might be looking at a functional cast.) */
7559 if (decl_specifiers.any_specifiers_p
7560 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7561 cp_parser_commit_to_tentative_parse (parser);
7563 /* Keep going until we hit the `;' at the end of the simple
7565 saw_declarator = false;
7566 while (cp_lexer_next_token_is_not (parser->lexer,
7570 bool function_definition_p;
7575 /* If we are processing next declarator, coma is expected */
7576 token = cp_lexer_peek_token (parser->lexer);
7577 gcc_assert (token->type == CPP_COMMA);
7578 cp_lexer_consume_token (parser->lexer);
7581 saw_declarator = true;
7583 /* Parse the init-declarator. */
7584 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7586 function_definition_allowed_p,
7588 declares_class_or_enum,
7589 &function_definition_p);
7590 /* If an error occurred while parsing tentatively, exit quickly.
7591 (That usually happens when in the body of a function; each
7592 statement is treated as a declaration-statement until proven
7594 if (cp_parser_error_occurred (parser))
7596 /* Handle function definitions specially. */
7597 if (function_definition_p)
7599 /* If the next token is a `,', then we are probably
7600 processing something like:
7604 which is erroneous. */
7605 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7606 error ("mixing declarations and function-definitions is forbidden");
7607 /* Otherwise, we're done with the list of declarators. */
7610 pop_deferring_access_checks ();
7614 /* The next token should be either a `,' or a `;'. */
7615 token = cp_lexer_peek_token (parser->lexer);
7616 /* If it's a `,', there are more declarators to come. */
7617 if (token->type == CPP_COMMA)
7618 /* will be consumed next time around */;
7619 /* If it's a `;', we are done. */
7620 else if (token->type == CPP_SEMICOLON)
7622 /* Anything else is an error. */
7625 /* If we have already issued an error message we don't need
7626 to issue another one. */
7627 if (decl != error_mark_node
7628 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7629 cp_parser_error (parser, "expected %<,%> or %<;%>");
7630 /* Skip tokens until we reach the end of the statement. */
7631 cp_parser_skip_to_end_of_statement (parser);
7632 /* If the next token is now a `;', consume it. */
7633 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7634 cp_lexer_consume_token (parser->lexer);
7637 /* After the first time around, a function-definition is not
7638 allowed -- even if it was OK at first. For example:
7643 function_definition_allowed_p = false;
7646 /* Issue an error message if no declarators are present, and the
7647 decl-specifier-seq does not itself declare a class or
7649 if (!saw_declarator)
7651 if (cp_parser_declares_only_class_p (parser))
7652 shadow_tag (&decl_specifiers);
7653 /* Perform any deferred access checks. */
7654 perform_deferred_access_checks ();
7657 /* Consume the `;'. */
7658 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7661 pop_deferring_access_checks ();
7664 /* Parse a decl-specifier-seq.
7667 decl-specifier-seq [opt] decl-specifier
7670 storage-class-specifier
7681 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7683 The parser flags FLAGS is used to control type-specifier parsing.
7685 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7688 1: one of the decl-specifiers is an elaborated-type-specifier
7689 (i.e., a type declaration)
7690 2: one of the decl-specifiers is an enum-specifier or a
7691 class-specifier (i.e., a type definition)
7696 cp_parser_decl_specifier_seq (cp_parser* parser,
7697 cp_parser_flags flags,
7698 cp_decl_specifier_seq *decl_specs,
7699 int* declares_class_or_enum)
7701 bool constructor_possible_p = !parser->in_declarator_p;
7703 /* Clear DECL_SPECS. */
7704 clear_decl_specs (decl_specs);
7706 /* Assume no class or enumeration type is declared. */
7707 *declares_class_or_enum = 0;
7709 /* Keep reading specifiers until there are no more to read. */
7713 bool found_decl_spec;
7716 /* Peek at the next token. */
7717 token = cp_lexer_peek_token (parser->lexer);
7718 /* Handle attributes. */
7719 if (token->keyword == RID_ATTRIBUTE)
7721 /* Parse the attributes. */
7722 decl_specs->attributes
7723 = chainon (decl_specs->attributes,
7724 cp_parser_attributes_opt (parser));
7727 /* Assume we will find a decl-specifier keyword. */
7728 found_decl_spec = true;
7729 /* If the next token is an appropriate keyword, we can simply
7730 add it to the list. */
7731 switch (token->keyword)
7736 if (!at_class_scope_p ())
7738 error ("%<friend%> used outside of class");
7739 cp_lexer_purge_token (parser->lexer);
7743 ++decl_specs->specs[(int) ds_friend];
7744 /* Consume the token. */
7745 cp_lexer_consume_token (parser->lexer);
7749 /* function-specifier:
7756 cp_parser_function_specifier_opt (parser, decl_specs);
7762 ++decl_specs->specs[(int) ds_typedef];
7763 /* Consume the token. */
7764 cp_lexer_consume_token (parser->lexer);
7765 /* A constructor declarator cannot appear in a typedef. */
7766 constructor_possible_p = false;
7767 /* The "typedef" keyword can only occur in a declaration; we
7768 may as well commit at this point. */
7769 cp_parser_commit_to_tentative_parse (parser);
7771 if (decl_specs->storage_class != sc_none)
7772 decl_specs->conflicting_specifiers_p = true;
7775 /* storage-class-specifier:
7789 /* Consume the token. */
7790 cp_lexer_consume_token (parser->lexer);
7791 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
7794 /* Consume the token. */
7795 cp_lexer_consume_token (parser->lexer);
7796 ++decl_specs->specs[(int) ds_thread];
7800 /* We did not yet find a decl-specifier yet. */
7801 found_decl_spec = false;
7805 /* Constructors are a special case. The `S' in `S()' is not a
7806 decl-specifier; it is the beginning of the declarator. */
7809 && constructor_possible_p
7810 && (cp_parser_constructor_declarator_p
7811 (parser, decl_specs->specs[(int) ds_friend] != 0)));
7813 /* If we don't have a DECL_SPEC yet, then we must be looking at
7814 a type-specifier. */
7815 if (!found_decl_spec && !constructor_p)
7817 int decl_spec_declares_class_or_enum;
7818 bool is_cv_qualifier;
7822 = cp_parser_type_specifier (parser, flags,
7824 /*is_declaration=*/true,
7825 &decl_spec_declares_class_or_enum,
7828 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7830 /* If this type-specifier referenced a user-defined type
7831 (a typedef, class-name, etc.), then we can't allow any
7832 more such type-specifiers henceforth.
7836 The longest sequence of decl-specifiers that could
7837 possibly be a type name is taken as the
7838 decl-specifier-seq of a declaration. The sequence shall
7839 be self-consistent as described below.
7843 As a general rule, at most one type-specifier is allowed
7844 in the complete decl-specifier-seq of a declaration. The
7845 only exceptions are the following:
7847 -- const or volatile can be combined with any other
7850 -- signed or unsigned can be combined with char, long,
7858 void g (const int Pc);
7860 Here, Pc is *not* part of the decl-specifier seq; it's
7861 the declarator. Therefore, once we see a type-specifier
7862 (other than a cv-qualifier), we forbid any additional
7863 user-defined types. We *do* still allow things like `int
7864 int' to be considered a decl-specifier-seq, and issue the
7865 error message later. */
7866 if (type_spec && !is_cv_qualifier)
7867 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7868 /* A constructor declarator cannot follow a type-specifier. */
7871 constructor_possible_p = false;
7872 found_decl_spec = true;
7876 /* If we still do not have a DECL_SPEC, then there are no more
7878 if (!found_decl_spec)
7881 decl_specs->any_specifiers_p = true;
7882 /* After we see one decl-specifier, further decl-specifiers are
7884 flags |= CP_PARSER_FLAGS_OPTIONAL;
7887 cp_parser_check_decl_spec (decl_specs);
7889 /* Don't allow a friend specifier with a class definition. */
7890 if (decl_specs->specs[(int) ds_friend] != 0
7891 && (*declares_class_or_enum & 2))
7892 error ("class definition may not be declared a friend");
7895 /* Parse an (optional) storage-class-specifier.
7897 storage-class-specifier:
7906 storage-class-specifier:
7909 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
7912 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7914 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7922 /* Consume the token. */
7923 return cp_lexer_consume_token (parser->lexer)->u.value;
7930 /* Parse an (optional) function-specifier.
7937 Returns an IDENTIFIER_NODE corresponding to the keyword used.
7938 Updates DECL_SPECS, if it is non-NULL. */
7941 cp_parser_function_specifier_opt (cp_parser* parser,
7942 cp_decl_specifier_seq *decl_specs)
7944 switch (cp_lexer_peek_token (parser->lexer)->keyword)
7948 ++decl_specs->specs[(int) ds_inline];
7952 /* 14.5.2.3 [temp.mem]
7954 A member function template shall not be virtual. */
7955 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
7956 error ("templates may not be %<virtual%>");
7957 else if (decl_specs)
7958 ++decl_specs->specs[(int) ds_virtual];
7963 ++decl_specs->specs[(int) ds_explicit];
7970 /* Consume the token. */
7971 return cp_lexer_consume_token (parser->lexer)->u.value;
7974 /* Parse a linkage-specification.
7976 linkage-specification:
7977 extern string-literal { declaration-seq [opt] }
7978 extern string-literal declaration */
7981 cp_parser_linkage_specification (cp_parser* parser)
7985 /* Look for the `extern' keyword. */
7986 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7988 /* Look for the string-literal. */
7989 linkage = cp_parser_string_literal (parser, false, false);
7991 /* Transform the literal into an identifier. If the literal is a
7992 wide-character string, or contains embedded NULs, then we can't
7993 handle it as the user wants. */
7994 if (strlen (TREE_STRING_POINTER (linkage))
7995 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7997 cp_parser_error (parser, "invalid linkage-specification");
7998 /* Assume C++ linkage. */
7999 linkage = lang_name_cplusplus;
8002 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8004 /* We're now using the new linkage. */
8005 push_lang_context (linkage);
8007 /* If the next token is a `{', then we're using the first
8009 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8011 /* Consume the `{' token. */
8012 cp_lexer_consume_token (parser->lexer);
8013 /* Parse the declarations. */
8014 cp_parser_declaration_seq_opt (parser);
8015 /* Look for the closing `}'. */
8016 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8018 /* Otherwise, there's just one declaration. */
8021 bool saved_in_unbraced_linkage_specification_p;
8023 saved_in_unbraced_linkage_specification_p
8024 = parser->in_unbraced_linkage_specification_p;
8025 parser->in_unbraced_linkage_specification_p = true;
8026 cp_parser_declaration (parser);
8027 parser->in_unbraced_linkage_specification_p
8028 = saved_in_unbraced_linkage_specification_p;
8031 /* We're done with the linkage-specification. */
8032 pop_lang_context ();
8035 /* Parse a static_assert-declaration.
8037 static_assert-declaration:
8038 static_assert ( constant-expression , string-literal ) ;
8040 If MEMBER_P, this static_assert is a class member. */
8043 cp_parser_static_assert(cp_parser *parser, bool member_p)
8048 location_t saved_loc;
8050 /* Peek at the `static_assert' token so we can keep track of exactly
8051 where the static assertion started. */
8052 token = cp_lexer_peek_token (parser->lexer);
8053 saved_loc = token->location;
8055 /* Look for the `static_assert' keyword. */
8056 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8060 /* We know we are in a static assertion; commit to any tentative
8062 if (cp_parser_parsing_tentatively (parser))
8063 cp_parser_commit_to_tentative_parse (parser);
8065 /* Parse the `(' starting the static assertion condition. */
8066 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8068 /* Parse the constant-expression. */
8070 cp_parser_constant_expression (parser,
8071 /*allow_non_constant_p=*/false,
8072 /*non_constant_p=*/NULL);
8074 /* Parse the separating `,'. */
8075 cp_parser_require (parser, CPP_COMMA, "`,'");
8077 /* Parse the string-literal message. */
8078 message = cp_parser_string_literal (parser,
8079 /*translate=*/false,
8082 /* A `)' completes the static assertion. */
8083 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8084 cp_parser_skip_to_closing_parenthesis (parser,
8085 /*recovering=*/true,
8087 /*consume_paren=*/true);
8089 /* A semicolon terminates the declaration. */
8090 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8092 /* Complete the static assertion, which may mean either processing
8093 the static assert now or saving it for template instantiation. */
8094 finish_static_assert (condition, message, saved_loc, member_p);
8097 /* Special member functions [gram.special] */
8099 /* Parse a conversion-function-id.
8101 conversion-function-id:
8102 operator conversion-type-id
8104 Returns an IDENTIFIER_NODE representing the operator. */
8107 cp_parser_conversion_function_id (cp_parser* parser)
8111 tree saved_qualifying_scope;
8112 tree saved_object_scope;
8113 tree pushed_scope = NULL_TREE;
8115 /* Look for the `operator' token. */
8116 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8117 return error_mark_node;
8118 /* When we parse the conversion-type-id, the current scope will be
8119 reset. However, we need that information in able to look up the
8120 conversion function later, so we save it here. */
8121 saved_scope = parser->scope;
8122 saved_qualifying_scope = parser->qualifying_scope;
8123 saved_object_scope = parser->object_scope;
8124 /* We must enter the scope of the class so that the names of
8125 entities declared within the class are available in the
8126 conversion-type-id. For example, consider:
8133 S::operator I() { ... }
8135 In order to see that `I' is a type-name in the definition, we
8136 must be in the scope of `S'. */
8138 pushed_scope = push_scope (saved_scope);
8139 /* Parse the conversion-type-id. */
8140 type = cp_parser_conversion_type_id (parser);
8141 /* Leave the scope of the class, if any. */
8143 pop_scope (pushed_scope);
8144 /* Restore the saved scope. */
8145 parser->scope = saved_scope;
8146 parser->qualifying_scope = saved_qualifying_scope;
8147 parser->object_scope = saved_object_scope;
8148 /* If the TYPE is invalid, indicate failure. */
8149 if (type == error_mark_node)
8150 return error_mark_node;
8151 return mangle_conv_op_name_for_type (type);
8154 /* Parse a conversion-type-id:
8157 type-specifier-seq conversion-declarator [opt]
8159 Returns the TYPE specified. */
8162 cp_parser_conversion_type_id (cp_parser* parser)
8165 cp_decl_specifier_seq type_specifiers;
8166 cp_declarator *declarator;
8167 tree type_specified;
8169 /* Parse the attributes. */
8170 attributes = cp_parser_attributes_opt (parser);
8171 /* Parse the type-specifiers. */
8172 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8174 /* If that didn't work, stop. */
8175 if (type_specifiers.type == error_mark_node)
8176 return error_mark_node;
8177 /* Parse the conversion-declarator. */
8178 declarator = cp_parser_conversion_declarator_opt (parser);
8180 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8181 /*initialized=*/0, &attributes);
8183 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8184 return type_specified;
8187 /* Parse an (optional) conversion-declarator.
8189 conversion-declarator:
8190 ptr-operator conversion-declarator [opt]
8194 static cp_declarator *
8195 cp_parser_conversion_declarator_opt (cp_parser* parser)
8197 enum tree_code code;
8199 cp_cv_quals cv_quals;
8201 /* We don't know if there's a ptr-operator next, or not. */
8202 cp_parser_parse_tentatively (parser);
8203 /* Try the ptr-operator. */
8204 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8205 /* If it worked, look for more conversion-declarators. */
8206 if (cp_parser_parse_definitely (parser))
8208 cp_declarator *declarator;
8210 /* Parse another optional declarator. */
8211 declarator = cp_parser_conversion_declarator_opt (parser);
8213 /* Create the representation of the declarator. */
8215 declarator = make_ptrmem_declarator (cv_quals, class_type,
8217 else if (code == INDIRECT_REF)
8218 declarator = make_pointer_declarator (cv_quals, declarator);
8220 declarator = make_reference_declarator (cv_quals, declarator);
8228 /* Parse an (optional) ctor-initializer.
8231 : mem-initializer-list
8233 Returns TRUE iff the ctor-initializer was actually present. */
8236 cp_parser_ctor_initializer_opt (cp_parser* parser)
8238 /* If the next token is not a `:', then there is no
8239 ctor-initializer. */
8240 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8242 /* Do default initialization of any bases and members. */
8243 if (DECL_CONSTRUCTOR_P (current_function_decl))
8244 finish_mem_initializers (NULL_TREE);
8249 /* Consume the `:' token. */
8250 cp_lexer_consume_token (parser->lexer);
8251 /* And the mem-initializer-list. */
8252 cp_parser_mem_initializer_list (parser);
8257 /* Parse a mem-initializer-list.
8259 mem-initializer-list:
8260 mem-initializer ... [opt]
8261 mem-initializer ... [opt] , mem-initializer-list */
8264 cp_parser_mem_initializer_list (cp_parser* parser)
8266 tree mem_initializer_list = NULL_TREE;
8268 /* Let the semantic analysis code know that we are starting the
8269 mem-initializer-list. */
8270 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8271 error ("only constructors take base initializers");
8273 /* Loop through the list. */
8276 tree mem_initializer;
8278 /* Parse the mem-initializer. */
8279 mem_initializer = cp_parser_mem_initializer (parser);
8280 /* If the next token is a `...', we're expanding member initializers. */
8281 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8283 /* Consume the `...'. */
8284 cp_lexer_consume_token (parser->lexer);
8286 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8287 can be expanded but members cannot. */
8288 if (mem_initializer != error_mark_node
8289 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8291 error ("cannot expand initializer for member %<%D%>",
8292 TREE_PURPOSE (mem_initializer));
8293 mem_initializer = error_mark_node;
8296 /* Construct the pack expansion type. */
8297 if (mem_initializer != error_mark_node)
8298 mem_initializer = make_pack_expansion (mem_initializer);
8300 /* Add it to the list, unless it was erroneous. */
8301 if (mem_initializer != error_mark_node)
8303 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8304 mem_initializer_list = mem_initializer;
8306 /* If the next token is not a `,', we're done. */
8307 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8309 /* Consume the `,' token. */
8310 cp_lexer_consume_token (parser->lexer);
8313 /* Perform semantic analysis. */
8314 if (DECL_CONSTRUCTOR_P (current_function_decl))
8315 finish_mem_initializers (mem_initializer_list);
8318 /* Parse a mem-initializer.
8321 mem-initializer-id ( expression-list [opt] )
8326 ( expression-list [opt] )
8328 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8329 class) or FIELD_DECL (for a non-static data member) to initialize;
8330 the TREE_VALUE is the expression-list. An empty initialization
8331 list is represented by void_list_node. */
8334 cp_parser_mem_initializer (cp_parser* parser)
8336 tree mem_initializer_id;
8337 tree expression_list;
8340 /* Find out what is being initialized. */
8341 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8343 pedwarn ("anachronistic old-style base class initializer");
8344 mem_initializer_id = NULL_TREE;
8347 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8348 member = expand_member_init (mem_initializer_id);
8349 if (member && !DECL_P (member))
8350 in_base_initializer = 1;
8353 = cp_parser_parenthesized_expression_list (parser, false,
8355 /*allow_expansion_p=*/true,
8356 /*non_constant_p=*/NULL);
8357 if (expression_list == error_mark_node)
8358 return error_mark_node;
8359 if (!expression_list)
8360 expression_list = void_type_node;
8362 in_base_initializer = 0;
8364 return member ? build_tree_list (member, expression_list) : error_mark_node;
8367 /* Parse a mem-initializer-id.
8370 :: [opt] nested-name-specifier [opt] class-name
8373 Returns a TYPE indicating the class to be initializer for the first
8374 production. Returns an IDENTIFIER_NODE indicating the data member
8375 to be initialized for the second production. */
8378 cp_parser_mem_initializer_id (cp_parser* parser)
8380 bool global_scope_p;
8381 bool nested_name_specifier_p;
8382 bool template_p = false;
8385 /* `typename' is not allowed in this context ([temp.res]). */
8386 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8388 error ("keyword %<typename%> not allowed in this context (a qualified "
8389 "member initializer is implicitly a type)");
8390 cp_lexer_consume_token (parser->lexer);
8392 /* Look for the optional `::' operator. */
8394 = (cp_parser_global_scope_opt (parser,
8395 /*current_scope_valid_p=*/false)
8397 /* Look for the optional nested-name-specifier. The simplest way to
8402 The keyword `typename' is not permitted in a base-specifier or
8403 mem-initializer; in these contexts a qualified name that
8404 depends on a template-parameter is implicitly assumed to be a
8407 is to assume that we have seen the `typename' keyword at this
8409 nested_name_specifier_p
8410 = (cp_parser_nested_name_specifier_opt (parser,
8411 /*typename_keyword_p=*/true,
8412 /*check_dependency_p=*/true,
8414 /*is_declaration=*/true)
8416 if (nested_name_specifier_p)
8417 template_p = cp_parser_optional_template_keyword (parser);
8418 /* If there is a `::' operator or a nested-name-specifier, then we
8419 are definitely looking for a class-name. */
8420 if (global_scope_p || nested_name_specifier_p)
8421 return cp_parser_class_name (parser,
8422 /*typename_keyword_p=*/true,
8423 /*template_keyword_p=*/template_p,
8425 /*check_dependency_p=*/true,
8426 /*class_head_p=*/false,
8427 /*is_declaration=*/true);
8428 /* Otherwise, we could also be looking for an ordinary identifier. */
8429 cp_parser_parse_tentatively (parser);
8430 /* Try a class-name. */
8431 id = cp_parser_class_name (parser,
8432 /*typename_keyword_p=*/true,
8433 /*template_keyword_p=*/false,
8435 /*check_dependency_p=*/true,
8436 /*class_head_p=*/false,
8437 /*is_declaration=*/true);
8438 /* If we found one, we're done. */
8439 if (cp_parser_parse_definitely (parser))
8441 /* Otherwise, look for an ordinary identifier. */
8442 return cp_parser_identifier (parser);
8445 /* Overloading [gram.over] */
8447 /* Parse an operator-function-id.
8449 operator-function-id:
8452 Returns an IDENTIFIER_NODE for the operator which is a
8453 human-readable spelling of the identifier, e.g., `operator +'. */
8456 cp_parser_operator_function_id (cp_parser* parser)
8458 /* Look for the `operator' keyword. */
8459 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8460 return error_mark_node;
8461 /* And then the name of the operator itself. */
8462 return cp_parser_operator (parser);
8465 /* Parse an operator.
8468 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8469 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8470 || ++ -- , ->* -> () []
8477 Returns an IDENTIFIER_NODE for the operator which is a
8478 human-readable spelling of the identifier, e.g., `operator +'. */
8481 cp_parser_operator (cp_parser* parser)
8483 tree id = NULL_TREE;
8486 /* Peek at the next token. */
8487 token = cp_lexer_peek_token (parser->lexer);
8488 /* Figure out which operator we have. */
8489 switch (token->type)
8495 /* The keyword should be either `new' or `delete'. */
8496 if (token->keyword == RID_NEW)
8498 else if (token->keyword == RID_DELETE)
8503 /* Consume the `new' or `delete' token. */
8504 cp_lexer_consume_token (parser->lexer);
8506 /* Peek at the next token. */
8507 token = cp_lexer_peek_token (parser->lexer);
8508 /* If it's a `[' token then this is the array variant of the
8510 if (token->type == CPP_OPEN_SQUARE)
8512 /* Consume the `[' token. */
8513 cp_lexer_consume_token (parser->lexer);
8514 /* Look for the `]' token. */
8515 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8516 id = ansi_opname (op == NEW_EXPR
8517 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8519 /* Otherwise, we have the non-array variant. */
8521 id = ansi_opname (op);
8527 id = ansi_opname (PLUS_EXPR);
8531 id = ansi_opname (MINUS_EXPR);
8535 id = ansi_opname (MULT_EXPR);
8539 id = ansi_opname (TRUNC_DIV_EXPR);
8543 id = ansi_opname (TRUNC_MOD_EXPR);
8547 id = ansi_opname (BIT_XOR_EXPR);
8551 id = ansi_opname (BIT_AND_EXPR);
8555 id = ansi_opname (BIT_IOR_EXPR);
8559 id = ansi_opname (BIT_NOT_EXPR);
8563 id = ansi_opname (TRUTH_NOT_EXPR);
8567 id = ansi_assopname (NOP_EXPR);
8571 id = ansi_opname (LT_EXPR);
8575 id = ansi_opname (GT_EXPR);
8579 id = ansi_assopname (PLUS_EXPR);
8583 id = ansi_assopname (MINUS_EXPR);
8587 id = ansi_assopname (MULT_EXPR);
8591 id = ansi_assopname (TRUNC_DIV_EXPR);
8595 id = ansi_assopname (TRUNC_MOD_EXPR);
8599 id = ansi_assopname (BIT_XOR_EXPR);
8603 id = ansi_assopname (BIT_AND_EXPR);
8607 id = ansi_assopname (BIT_IOR_EXPR);
8611 id = ansi_opname (LSHIFT_EXPR);
8615 id = ansi_opname (RSHIFT_EXPR);
8619 id = ansi_assopname (LSHIFT_EXPR);
8623 id = ansi_assopname (RSHIFT_EXPR);
8627 id = ansi_opname (EQ_EXPR);
8631 id = ansi_opname (NE_EXPR);
8635 id = ansi_opname (LE_EXPR);
8638 case CPP_GREATER_EQ:
8639 id = ansi_opname (GE_EXPR);
8643 id = ansi_opname (TRUTH_ANDIF_EXPR);
8647 id = ansi_opname (TRUTH_ORIF_EXPR);
8651 id = ansi_opname (POSTINCREMENT_EXPR);
8654 case CPP_MINUS_MINUS:
8655 id = ansi_opname (PREDECREMENT_EXPR);
8659 id = ansi_opname (COMPOUND_EXPR);
8662 case CPP_DEREF_STAR:
8663 id = ansi_opname (MEMBER_REF);
8667 id = ansi_opname (COMPONENT_REF);
8670 case CPP_OPEN_PAREN:
8671 /* Consume the `('. */
8672 cp_lexer_consume_token (parser->lexer);
8673 /* Look for the matching `)'. */
8674 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8675 return ansi_opname (CALL_EXPR);
8677 case CPP_OPEN_SQUARE:
8678 /* Consume the `['. */
8679 cp_lexer_consume_token (parser->lexer);
8680 /* Look for the matching `]'. */
8681 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8682 return ansi_opname (ARRAY_REF);
8685 /* Anything else is an error. */
8689 /* If we have selected an identifier, we need to consume the
8692 cp_lexer_consume_token (parser->lexer);
8693 /* Otherwise, no valid operator name was present. */
8696 cp_parser_error (parser, "expected operator");
8697 id = error_mark_node;
8703 /* Parse a template-declaration.
8705 template-declaration:
8706 export [opt] template < template-parameter-list > declaration
8708 If MEMBER_P is TRUE, this template-declaration occurs within a
8711 The grammar rule given by the standard isn't correct. What
8714 template-declaration:
8715 export [opt] template-parameter-list-seq
8716 decl-specifier-seq [opt] init-declarator [opt] ;
8717 export [opt] template-parameter-list-seq
8720 template-parameter-list-seq:
8721 template-parameter-list-seq [opt]
8722 template < template-parameter-list > */
8725 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8727 /* Check for `export'. */
8728 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8730 /* Consume the `export' token. */
8731 cp_lexer_consume_token (parser->lexer);
8732 /* Warn that we do not support `export'. */
8733 warning (0, "keyword %<export%> not implemented, and will be ignored");
8736 cp_parser_template_declaration_after_export (parser, member_p);
8739 /* Parse a template-parameter-list.
8741 template-parameter-list:
8743 template-parameter-list , template-parameter
8745 Returns a TREE_LIST. Each node represents a template parameter.
8746 The nodes are connected via their TREE_CHAINs. */
8749 cp_parser_template_parameter_list (cp_parser* parser)
8751 tree parameter_list = NULL_TREE;
8753 begin_template_parm_list ();
8759 bool is_parameter_pack;
8761 /* Parse the template-parameter. */
8762 parameter = cp_parser_template_parameter (parser,
8764 &is_parameter_pack);
8765 /* Add it to the list. */
8766 if (parameter != error_mark_node)
8767 parameter_list = process_template_parm (parameter_list,
8773 tree err_parm = build_tree_list (parameter, parameter);
8774 TREE_VALUE (err_parm) = error_mark_node;
8775 parameter_list = chainon (parameter_list, err_parm);
8778 /* Peek at the next token. */
8779 token = cp_lexer_peek_token (parser->lexer);
8780 /* If it's not a `,', we're done. */
8781 if (token->type != CPP_COMMA)
8783 /* Otherwise, consume the `,' token. */
8784 cp_lexer_consume_token (parser->lexer);
8787 return end_template_parm_list (parameter_list);
8790 /* Parse a template-parameter.
8794 parameter-declaration
8796 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
8797 the parameter. The TREE_PURPOSE is the default value, if any.
8798 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
8799 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
8800 set to true iff this parameter is a parameter pack. */
8803 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
8804 bool *is_parameter_pack)
8807 cp_parameter_declarator *parameter_declarator;
8810 /* Assume it is a type parameter or a template parameter. */
8811 *is_non_type = false;
8812 /* Assume it not a parameter pack. */
8813 *is_parameter_pack = false;
8814 /* Peek at the next token. */
8815 token = cp_lexer_peek_token (parser->lexer);
8816 /* If it is `class' or `template', we have a type-parameter. */
8817 if (token->keyword == RID_TEMPLATE)
8818 return cp_parser_type_parameter (parser, is_parameter_pack);
8819 /* If it is `class' or `typename' we do not know yet whether it is a
8820 type parameter or a non-type parameter. Consider:
8822 template <typename T, typename T::X X> ...
8826 template <class C, class D*> ...
8828 Here, the first parameter is a type parameter, and the second is
8829 a non-type parameter. We can tell by looking at the token after
8830 the identifier -- if it is a `,', `=', or `>' then we have a type
8832 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8834 /* Peek at the token after `class' or `typename'. */
8835 token = cp_lexer_peek_nth_token (parser->lexer, 2);
8836 /* If it's an ellipsis, we have a template type parameter
8838 if (token->type == CPP_ELLIPSIS)
8839 return cp_parser_type_parameter (parser, is_parameter_pack);
8840 /* If it's an identifier, skip it. */
8841 if (token->type == CPP_NAME)
8842 token = cp_lexer_peek_nth_token (parser->lexer, 3);
8843 /* Now, see if the token looks like the end of a template
8845 if (token->type == CPP_COMMA
8846 || token->type == CPP_EQ
8847 || token->type == CPP_GREATER)
8848 return cp_parser_type_parameter (parser, is_parameter_pack);
8851 /* Otherwise, it is a non-type parameter.
8855 When parsing a default template-argument for a non-type
8856 template-parameter, the first non-nested `>' is taken as the end
8857 of the template parameter-list rather than a greater-than
8859 *is_non_type = true;
8860 parameter_declarator
8861 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8862 /*parenthesized_p=*/NULL);
8864 /* If the parameter declaration is marked as a parameter pack, set
8865 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
8866 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
8868 if (parameter_declarator
8869 && parameter_declarator->declarator
8870 && parameter_declarator->declarator->parameter_pack_p)
8872 *is_parameter_pack = true;
8873 parameter_declarator->declarator->parameter_pack_p = false;
8876 /* If the next token is an ellipsis, and we don't already have it
8877 marked as a parameter pack, then we have a parameter pack (that
8878 has no declarator); */
8879 if (!*is_parameter_pack
8880 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8883 /* Consume the `...'. */
8884 cp_lexer_consume_token (parser->lexer);
8885 maybe_warn_variadic_templates ();
8887 *is_parameter_pack = true;
8890 parm = grokdeclarator (parameter_declarator->declarator,
8891 ¶meter_declarator->decl_specifiers,
8892 PARM, /*initialized=*/0,
8894 if (parm == error_mark_node)
8895 return error_mark_node;
8897 return build_tree_list (parameter_declarator->default_argument, parm);
8900 /* Parse a type-parameter.
8903 class identifier [opt]
8904 class identifier [opt] = type-id
8905 typename identifier [opt]
8906 typename identifier [opt] = type-id
8907 template < template-parameter-list > class identifier [opt]
8908 template < template-parameter-list > class identifier [opt]
8911 GNU Extension (variadic templates):
8914 class ... identifier [opt]
8915 typename ... identifier [opt]
8917 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
8918 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
8919 the declaration of the parameter.
8921 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
8924 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
8929 /* Look for a keyword to tell us what kind of parameter this is. */
8930 token = cp_parser_require (parser, CPP_KEYWORD,
8931 "`class', `typename', or `template'");
8933 return error_mark_node;
8935 switch (token->keyword)
8941 tree default_argument;
8943 /* If the next token is an ellipsis, we have a template
8945 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8947 /* Consume the `...' token. */
8948 cp_lexer_consume_token (parser->lexer);
8949 maybe_warn_variadic_templates ();
8951 *is_parameter_pack = true;
8954 /* If the next token is an identifier, then it names the
8956 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8957 identifier = cp_parser_identifier (parser);
8959 identifier = NULL_TREE;
8961 /* Create the parameter. */
8962 parameter = finish_template_type_parm (class_type_node, identifier);
8964 /* If the next token is an `=', we have a default argument. */
8965 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8967 /* Consume the `=' token. */
8968 cp_lexer_consume_token (parser->lexer);
8969 /* Parse the default-argument. */
8970 push_deferring_access_checks (dk_no_deferred);
8971 default_argument = cp_parser_type_id (parser);
8973 /* Template parameter packs cannot have default
8975 if (*is_parameter_pack)
8978 error ("template parameter pack %qD cannot have a default argument",
8981 error ("template parameter packs cannot have default arguments");
8982 default_argument = NULL_TREE;
8984 pop_deferring_access_checks ();
8987 default_argument = NULL_TREE;
8989 /* Create the combined representation of the parameter and the
8990 default argument. */
8991 parameter = build_tree_list (default_argument, parameter);
8997 tree parameter_list;
8999 tree default_argument;
9001 /* Look for the `<'. */
9002 cp_parser_require (parser, CPP_LESS, "`<'");
9003 /* Parse the template-parameter-list. */
9004 parameter_list = cp_parser_template_parameter_list (parser);
9005 /* Look for the `>'. */
9006 cp_parser_require (parser, CPP_GREATER, "`>'");
9007 /* Look for the `class' keyword. */
9008 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9009 /* If the next token is an ellipsis, we have a template
9011 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9013 /* Consume the `...' token. */
9014 cp_lexer_consume_token (parser->lexer);
9015 maybe_warn_variadic_templates ();
9017 *is_parameter_pack = true;
9019 /* If the next token is an `=', then there is a
9020 default-argument. If the next token is a `>', we are at
9021 the end of the parameter-list. If the next token is a `,',
9022 then we are at the end of this parameter. */
9023 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9024 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9025 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9027 identifier = cp_parser_identifier (parser);
9028 /* Treat invalid names as if the parameter were nameless. */
9029 if (identifier == error_mark_node)
9030 identifier = NULL_TREE;
9033 identifier = NULL_TREE;
9035 /* Create the template parameter. */
9036 parameter = finish_template_template_parm (class_type_node,
9039 /* If the next token is an `=', then there is a
9040 default-argument. */
9041 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9045 /* Consume the `='. */
9046 cp_lexer_consume_token (parser->lexer);
9047 /* Parse the id-expression. */
9048 push_deferring_access_checks (dk_no_deferred);
9050 = cp_parser_id_expression (parser,
9051 /*template_keyword_p=*/false,
9052 /*check_dependency_p=*/true,
9053 /*template_p=*/&is_template,
9054 /*declarator_p=*/false,
9055 /*optional_p=*/false);
9056 if (TREE_CODE (default_argument) == TYPE_DECL)
9057 /* If the id-expression was a template-id that refers to
9058 a template-class, we already have the declaration here,
9059 so no further lookup is needed. */
9062 /* Look up the name. */
9064 = cp_parser_lookup_name (parser, default_argument,
9066 /*is_template=*/is_template,
9067 /*is_namespace=*/false,
9068 /*check_dependency=*/true,
9069 /*ambiguous_decls=*/NULL);
9070 /* See if the default argument is valid. */
9072 = check_template_template_default_arg (default_argument);
9074 /* Template parameter packs cannot have default
9076 if (*is_parameter_pack)
9079 error ("template parameter pack %qD cannot have a default argument",
9082 error ("template parameter packs cannot have default arguments");
9083 default_argument = NULL_TREE;
9085 pop_deferring_access_checks ();
9088 default_argument = NULL_TREE;
9090 /* Create the combined representation of the parameter and the
9091 default argument. */
9092 parameter = build_tree_list (default_argument, parameter);
9104 /* Parse a template-id.
9107 template-name < template-argument-list [opt] >
9109 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9110 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9111 returned. Otherwise, if the template-name names a function, or set
9112 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9113 names a class, returns a TYPE_DECL for the specialization.
9115 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9116 uninstantiated templates. */
9119 cp_parser_template_id (cp_parser *parser,
9120 bool template_keyword_p,
9121 bool check_dependency_p,
9122 bool is_declaration)
9128 cp_token_position start_of_id = 0;
9129 deferred_access_check *chk;
9130 VEC (deferred_access_check,gc) *access_check;
9131 cp_token *next_token, *next_token_2;
9134 /* If the next token corresponds to a template-id, there is no need
9136 next_token = cp_lexer_peek_token (parser->lexer);
9137 if (next_token->type == CPP_TEMPLATE_ID)
9139 struct tree_check *check_value;
9141 /* Get the stored value. */
9142 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9143 /* Perform any access checks that were deferred. */
9144 access_check = check_value->checks;
9148 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9151 perform_or_defer_access_check (chk->binfo,
9156 /* Return the stored value. */
9157 return check_value->value;
9160 /* Avoid performing name lookup if there is no possibility of
9161 finding a template-id. */
9162 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9163 || (next_token->type == CPP_NAME
9164 && !cp_parser_nth_token_starts_template_argument_list_p
9167 cp_parser_error (parser, "expected template-id");
9168 return error_mark_node;
9171 /* Remember where the template-id starts. */
9172 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9173 start_of_id = cp_lexer_token_position (parser->lexer, false);
9175 push_deferring_access_checks (dk_deferred);
9177 /* Parse the template-name. */
9178 is_identifier = false;
9179 template = cp_parser_template_name (parser, template_keyword_p,
9183 if (template == error_mark_node || is_identifier)
9185 pop_deferring_access_checks ();
9189 /* If we find the sequence `[:' after a template-name, it's probably
9190 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9191 parse correctly the argument list. */
9192 next_token = cp_lexer_peek_token (parser->lexer);
9193 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9194 if (next_token->type == CPP_OPEN_SQUARE
9195 && next_token->flags & DIGRAPH
9196 && next_token_2->type == CPP_COLON
9197 && !(next_token_2->flags & PREV_WHITE))
9199 cp_parser_parse_tentatively (parser);
9200 /* Change `:' into `::'. */
9201 next_token_2->type = CPP_SCOPE;
9202 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9204 cp_lexer_consume_token (parser->lexer);
9205 /* Parse the arguments. */
9206 arguments = cp_parser_enclosed_template_argument_list (parser);
9207 if (!cp_parser_parse_definitely (parser))
9209 /* If we couldn't parse an argument list, then we revert our changes
9210 and return simply an error. Maybe this is not a template-id
9212 next_token_2->type = CPP_COLON;
9213 cp_parser_error (parser, "expected %<<%>");
9214 pop_deferring_access_checks ();
9215 return error_mark_node;
9217 /* Otherwise, emit an error about the invalid digraph, but continue
9218 parsing because we got our argument list. */
9219 pedwarn ("%<<::%> cannot begin a template-argument list");
9220 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9221 "between %<<%> and %<::%>");
9222 if (!flag_permissive)
9227 inform ("(if you use -fpermissive G++ will accept your code)");
9234 /* Look for the `<' that starts the template-argument-list. */
9235 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9237 pop_deferring_access_checks ();
9238 return error_mark_node;
9240 /* Parse the arguments. */
9241 arguments = cp_parser_enclosed_template_argument_list (parser);
9244 /* Build a representation of the specialization. */
9245 if (TREE_CODE (template) == IDENTIFIER_NODE)
9246 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9247 else if (DECL_CLASS_TEMPLATE_P (template)
9248 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9250 bool entering_scope;
9251 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9252 template (rather than some instantiation thereof) only if
9253 is not nested within some other construct. For example, in
9254 "template <typename T> void f(T) { A<T>::", A<T> is just an
9255 instantiation of A. */
9256 entering_scope = (template_parm_scope_p ()
9257 && cp_lexer_next_token_is (parser->lexer,
9260 = finish_template_type (template, arguments, entering_scope);
9264 /* If it's not a class-template or a template-template, it should be
9265 a function-template. */
9266 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9267 || TREE_CODE (template) == OVERLOAD
9268 || BASELINK_P (template)));
9270 template_id = lookup_template_function (template, arguments);
9273 /* If parsing tentatively, replace the sequence of tokens that makes
9274 up the template-id with a CPP_TEMPLATE_ID token. That way,
9275 should we re-parse the token stream, we will not have to repeat
9276 the effort required to do the parse, nor will we issue duplicate
9277 error messages about problems during instantiation of the
9281 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9283 /* Reset the contents of the START_OF_ID token. */
9284 token->type = CPP_TEMPLATE_ID;
9285 /* Retrieve any deferred checks. Do not pop this access checks yet
9286 so the memory will not be reclaimed during token replacing below. */
9287 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9288 token->u.tree_check_value->value = template_id;
9289 token->u.tree_check_value->checks = get_deferred_access_checks ();
9290 token->keyword = RID_MAX;
9292 /* Purge all subsequent tokens. */
9293 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9295 /* ??? Can we actually assume that, if template_id ==
9296 error_mark_node, we will have issued a diagnostic to the
9297 user, as opposed to simply marking the tentative parse as
9299 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9300 error ("parse error in template argument list");
9303 pop_deferring_access_checks ();
9307 /* Parse a template-name.
9312 The standard should actually say:
9316 operator-function-id
9318 A defect report has been filed about this issue.
9320 A conversion-function-id cannot be a template name because they cannot
9321 be part of a template-id. In fact, looking at this code:
9325 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9326 It is impossible to call a templated conversion-function-id with an
9327 explicit argument list, since the only allowed template parameter is
9328 the type to which it is converting.
9330 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9331 `template' keyword, in a construction like:
9335 In that case `f' is taken to be a template-name, even though there
9336 is no way of knowing for sure.
9338 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9339 name refers to a set of overloaded functions, at least one of which
9340 is a template, or an IDENTIFIER_NODE with the name of the template,
9341 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9342 names are looked up inside uninstantiated templates. */
9345 cp_parser_template_name (cp_parser* parser,
9346 bool template_keyword_p,
9347 bool check_dependency_p,
9348 bool is_declaration,
9349 bool *is_identifier)
9355 /* If the next token is `operator', then we have either an
9356 operator-function-id or a conversion-function-id. */
9357 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9359 /* We don't know whether we're looking at an
9360 operator-function-id or a conversion-function-id. */
9361 cp_parser_parse_tentatively (parser);
9362 /* Try an operator-function-id. */
9363 identifier = cp_parser_operator_function_id (parser);
9364 /* If that didn't work, try a conversion-function-id. */
9365 if (!cp_parser_parse_definitely (parser))
9367 cp_parser_error (parser, "expected template-name");
9368 return error_mark_node;
9371 /* Look for the identifier. */
9373 identifier = cp_parser_identifier (parser);
9375 /* If we didn't find an identifier, we don't have a template-id. */
9376 if (identifier == error_mark_node)
9377 return error_mark_node;
9379 /* If the name immediately followed the `template' keyword, then it
9380 is a template-name. However, if the next token is not `<', then
9381 we do not treat it as a template-name, since it is not being used
9382 as part of a template-id. This enables us to handle constructs
9385 template <typename T> struct S { S(); };
9386 template <typename T> S<T>::S();
9388 correctly. We would treat `S' as a template -- if it were `S<T>'
9389 -- but we do not if there is no `<'. */
9391 if (processing_template_decl
9392 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9394 /* In a declaration, in a dependent context, we pretend that the
9395 "template" keyword was present in order to improve error
9396 recovery. For example, given:
9398 template <typename T> void f(T::X<int>);
9400 we want to treat "X<int>" as a template-id. */
9402 && !template_keyword_p
9403 && parser->scope && TYPE_P (parser->scope)
9404 && check_dependency_p
9405 && dependent_type_p (parser->scope)
9406 /* Do not do this for dtors (or ctors), since they never
9407 need the template keyword before their name. */
9408 && !constructor_name_p (identifier, parser->scope))
9410 cp_token_position start = 0;
9412 /* Explain what went wrong. */
9413 error ("non-template %qD used as template", identifier);
9414 inform ("use %<%T::template %D%> to indicate that it is a template",
9415 parser->scope, identifier);
9416 /* If parsing tentatively, find the location of the "<" token. */
9417 if (cp_parser_simulate_error (parser))
9418 start = cp_lexer_token_position (parser->lexer, true);
9419 /* Parse the template arguments so that we can issue error
9420 messages about them. */
9421 cp_lexer_consume_token (parser->lexer);
9422 cp_parser_enclosed_template_argument_list (parser);
9423 /* Skip tokens until we find a good place from which to
9424 continue parsing. */
9425 cp_parser_skip_to_closing_parenthesis (parser,
9426 /*recovering=*/true,
9428 /*consume_paren=*/false);
9429 /* If parsing tentatively, permanently remove the
9430 template argument list. That will prevent duplicate
9431 error messages from being issued about the missing
9432 "template" keyword. */
9434 cp_lexer_purge_tokens_after (parser->lexer, start);
9436 *is_identifier = true;
9440 /* If the "template" keyword is present, then there is generally
9441 no point in doing name-lookup, so we just return IDENTIFIER.
9442 But, if the qualifying scope is non-dependent then we can
9443 (and must) do name-lookup normally. */
9444 if (template_keyword_p
9446 || (TYPE_P (parser->scope)
9447 && dependent_type_p (parser->scope))))
9451 /* Look up the name. */
9452 decl = cp_parser_lookup_name (parser, identifier,
9454 /*is_template=*/false,
9455 /*is_namespace=*/false,
9457 /*ambiguous_decls=*/NULL);
9458 decl = maybe_get_template_decl_from_type_decl (decl);
9460 /* If DECL is a template, then the name was a template-name. */
9461 if (TREE_CODE (decl) == TEMPLATE_DECL)
9465 tree fn = NULL_TREE;
9467 /* The standard does not explicitly indicate whether a name that
9468 names a set of overloaded declarations, some of which are
9469 templates, is a template-name. However, such a name should
9470 be a template-name; otherwise, there is no way to form a
9471 template-id for the overloaded templates. */
9472 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9473 if (TREE_CODE (fns) == OVERLOAD)
9474 for (fn = fns; fn; fn = OVL_NEXT (fn))
9475 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9480 /* The name does not name a template. */
9481 cp_parser_error (parser, "expected template-name");
9482 return error_mark_node;
9486 /* If DECL is dependent, and refers to a function, then just return
9487 its name; we will look it up again during template instantiation. */
9488 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9490 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9491 if (TYPE_P (scope) && dependent_type_p (scope))
9498 /* Parse a template-argument-list.
9500 template-argument-list:
9501 template-argument ... [opt]
9502 template-argument-list , template-argument ... [opt]
9504 Returns a TREE_VEC containing the arguments. */
9507 cp_parser_template_argument_list (cp_parser* parser)
9509 tree fixed_args[10];
9510 unsigned n_args = 0;
9511 unsigned alloced = 10;
9512 tree *arg_ary = fixed_args;
9514 bool saved_in_template_argument_list_p;
9516 bool saved_non_ice_p;
9518 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9519 parser->in_template_argument_list_p = true;
9520 /* Even if the template-id appears in an integral
9521 constant-expression, the contents of the argument list do
9523 saved_ice_p = parser->integral_constant_expression_p;
9524 parser->integral_constant_expression_p = false;
9525 saved_non_ice_p = parser->non_integral_constant_expression_p;
9526 parser->non_integral_constant_expression_p = false;
9527 /* Parse the arguments. */
9533 /* Consume the comma. */
9534 cp_lexer_consume_token (parser->lexer);
9536 /* Parse the template-argument. */
9537 argument = cp_parser_template_argument (parser);
9539 /* If the next token is an ellipsis, we're expanding a template
9541 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9543 /* Consume the `...' token. */
9544 cp_lexer_consume_token (parser->lexer);
9546 /* Make the argument into a TYPE_PACK_EXPANSION or
9547 EXPR_PACK_EXPANSION. */
9548 argument = make_pack_expansion (argument);
9551 if (n_args == alloced)
9555 if (arg_ary == fixed_args)
9557 arg_ary = XNEWVEC (tree, alloced);
9558 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9561 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9563 arg_ary[n_args++] = argument;
9565 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9567 vec = make_tree_vec (n_args);
9570 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9572 if (arg_ary != fixed_args)
9574 parser->non_integral_constant_expression_p = saved_non_ice_p;
9575 parser->integral_constant_expression_p = saved_ice_p;
9576 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9580 /* Parse a template-argument.
9583 assignment-expression
9587 The representation is that of an assignment-expression, type-id, or
9588 id-expression -- except that the qualified id-expression is
9589 evaluated, so that the value returned is either a DECL or an
9592 Although the standard says "assignment-expression", it forbids
9593 throw-expressions or assignments in the template argument.
9594 Therefore, we use "conditional-expression" instead. */
9597 cp_parser_template_argument (cp_parser* parser)
9602 bool maybe_type_id = false;
9606 /* There's really no way to know what we're looking at, so we just
9607 try each alternative in order.
9611 In a template-argument, an ambiguity between a type-id and an
9612 expression is resolved to a type-id, regardless of the form of
9613 the corresponding template-parameter.
9615 Therefore, we try a type-id first. */
9616 cp_parser_parse_tentatively (parser);
9617 argument = cp_parser_type_id (parser);
9618 /* If there was no error parsing the type-id but the next token is a '>>',
9619 we probably found a typo for '> >'. But there are type-id which are
9620 also valid expressions. For instance:
9622 struct X { int operator >> (int); };
9623 template <int V> struct Foo {};
9626 Here 'X()' is a valid type-id of a function type, but the user just
9627 wanted to write the expression "X() >> 5". Thus, we remember that we
9628 found a valid type-id, but we still try to parse the argument as an
9629 expression to see what happens. */
9630 if (!cp_parser_error_occurred (parser)
9631 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9633 maybe_type_id = true;
9634 cp_parser_abort_tentative_parse (parser);
9638 /* If the next token isn't a `,' or a `>', then this argument wasn't
9639 really finished. This means that the argument is not a valid
9641 if (!cp_parser_next_token_ends_template_argument_p (parser))
9642 cp_parser_error (parser, "expected template-argument");
9643 /* If that worked, we're done. */
9644 if (cp_parser_parse_definitely (parser))
9647 /* We're still not sure what the argument will be. */
9648 cp_parser_parse_tentatively (parser);
9649 /* Try a template. */
9650 argument = cp_parser_id_expression (parser,
9651 /*template_keyword_p=*/false,
9652 /*check_dependency_p=*/true,
9654 /*declarator_p=*/false,
9655 /*optional_p=*/false);
9656 /* If the next token isn't a `,' or a `>', then this argument wasn't
9658 if (!cp_parser_next_token_ends_template_argument_p (parser))
9659 cp_parser_error (parser, "expected template-argument");
9660 if (!cp_parser_error_occurred (parser))
9662 /* Figure out what is being referred to. If the id-expression
9663 was for a class template specialization, then we will have a
9664 TYPE_DECL at this point. There is no need to do name lookup
9665 at this point in that case. */
9666 if (TREE_CODE (argument) != TYPE_DECL)
9667 argument = cp_parser_lookup_name (parser, argument,
9669 /*is_template=*/template_p,
9670 /*is_namespace=*/false,
9671 /*check_dependency=*/true,
9672 /*ambiguous_decls=*/NULL);
9673 if (TREE_CODE (argument) != TEMPLATE_DECL
9674 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9675 cp_parser_error (parser, "expected template-name");
9677 if (cp_parser_parse_definitely (parser))
9679 /* It must be a non-type argument. There permitted cases are given
9680 in [temp.arg.nontype]:
9682 -- an integral constant-expression of integral or enumeration
9685 -- the name of a non-type template-parameter; or
9687 -- the name of an object or function with external linkage...
9689 -- the address of an object or function with external linkage...
9691 -- a pointer to member... */
9692 /* Look for a non-type template parameter. */
9693 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9695 cp_parser_parse_tentatively (parser);
9696 argument = cp_parser_primary_expression (parser,
9699 /*template_arg_p=*/true,
9701 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9702 || !cp_parser_next_token_ends_template_argument_p (parser))
9703 cp_parser_simulate_error (parser);
9704 if (cp_parser_parse_definitely (parser))
9708 /* If the next token is "&", the argument must be the address of an
9709 object or function with external linkage. */
9710 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9712 cp_lexer_consume_token (parser->lexer);
9713 /* See if we might have an id-expression. */
9714 token = cp_lexer_peek_token (parser->lexer);
9715 if (token->type == CPP_NAME
9716 || token->keyword == RID_OPERATOR
9717 || token->type == CPP_SCOPE
9718 || token->type == CPP_TEMPLATE_ID
9719 || token->type == CPP_NESTED_NAME_SPECIFIER)
9721 cp_parser_parse_tentatively (parser);
9722 argument = cp_parser_primary_expression (parser,
9725 /*template_arg_p=*/true,
9727 if (cp_parser_error_occurred (parser)
9728 || !cp_parser_next_token_ends_template_argument_p (parser))
9729 cp_parser_abort_tentative_parse (parser);
9732 if (TREE_CODE (argument) == INDIRECT_REF)
9734 gcc_assert (REFERENCE_REF_P (argument));
9735 argument = TREE_OPERAND (argument, 0);
9738 if (TREE_CODE (argument) == VAR_DECL)
9740 /* A variable without external linkage might still be a
9741 valid constant-expression, so no error is issued here
9742 if the external-linkage check fails. */
9743 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
9744 cp_parser_simulate_error (parser);
9746 else if (is_overloaded_fn (argument))
9747 /* All overloaded functions are allowed; if the external
9748 linkage test does not pass, an error will be issued
9752 && (TREE_CODE (argument) == OFFSET_REF
9753 || TREE_CODE (argument) == SCOPE_REF))
9754 /* A pointer-to-member. */
9756 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9759 cp_parser_simulate_error (parser);
9761 if (cp_parser_parse_definitely (parser))
9764 argument = build_x_unary_op (ADDR_EXPR, argument);
9769 /* If the argument started with "&", there are no other valid
9770 alternatives at this point. */
9773 cp_parser_error (parser, "invalid non-type template argument");
9774 return error_mark_node;
9777 /* If the argument wasn't successfully parsed as a type-id followed
9778 by '>>', the argument can only be a constant expression now.
9779 Otherwise, we try parsing the constant-expression tentatively,
9780 because the argument could really be a type-id. */
9782 cp_parser_parse_tentatively (parser);
9783 argument = cp_parser_constant_expression (parser,
9784 /*allow_non_constant_p=*/false,
9785 /*non_constant_p=*/NULL);
9786 argument = fold_non_dependent_expr (argument);
9789 if (!cp_parser_next_token_ends_template_argument_p (parser))
9790 cp_parser_error (parser, "expected template-argument");
9791 if (cp_parser_parse_definitely (parser))
9793 /* We did our best to parse the argument as a non type-id, but that
9794 was the only alternative that matched (albeit with a '>' after
9795 it). We can assume it's just a typo from the user, and a
9796 diagnostic will then be issued. */
9797 return cp_parser_type_id (parser);
9800 /* Parse an explicit-instantiation.
9802 explicit-instantiation:
9803 template declaration
9805 Although the standard says `declaration', what it really means is:
9807 explicit-instantiation:
9808 template decl-specifier-seq [opt] declarator [opt] ;
9810 Things like `template int S<int>::i = 5, int S<double>::j;' are not
9811 supposed to be allowed. A defect report has been filed about this
9816 explicit-instantiation:
9817 storage-class-specifier template
9818 decl-specifier-seq [opt] declarator [opt] ;
9819 function-specifier template
9820 decl-specifier-seq [opt] declarator [opt] ; */
9823 cp_parser_explicit_instantiation (cp_parser* parser)
9825 int declares_class_or_enum;
9826 cp_decl_specifier_seq decl_specifiers;
9827 tree extension_specifier = NULL_TREE;
9829 /* Look for an (optional) storage-class-specifier or
9830 function-specifier. */
9831 if (cp_parser_allow_gnu_extensions_p (parser))
9834 = cp_parser_storage_class_specifier_opt (parser);
9835 if (!extension_specifier)
9837 = cp_parser_function_specifier_opt (parser,
9838 /*decl_specs=*/NULL);
9841 /* Look for the `template' keyword. */
9842 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9843 /* Let the front end know that we are processing an explicit
9845 begin_explicit_instantiation ();
9846 /* [temp.explicit] says that we are supposed to ignore access
9847 control while processing explicit instantiation directives. */
9848 push_deferring_access_checks (dk_no_check);
9849 /* Parse a decl-specifier-seq. */
9850 cp_parser_decl_specifier_seq (parser,
9851 CP_PARSER_FLAGS_OPTIONAL,
9853 &declares_class_or_enum);
9854 /* If there was exactly one decl-specifier, and it declared a class,
9855 and there's no declarator, then we have an explicit type
9857 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9861 type = check_tag_decl (&decl_specifiers);
9862 /* Turn access control back on for names used during
9863 template instantiation. */
9864 pop_deferring_access_checks ();
9866 do_type_instantiation (type, extension_specifier,
9867 /*complain=*/tf_error);
9871 cp_declarator *declarator;
9874 /* Parse the declarator. */
9876 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9877 /*ctor_dtor_or_conv_p=*/NULL,
9878 /*parenthesized_p=*/NULL,
9879 /*member_p=*/false);
9880 if (declares_class_or_enum & 2)
9881 cp_parser_check_for_definition_in_return_type (declarator,
9882 decl_specifiers.type);
9883 if (declarator != cp_error_declarator)
9885 decl = grokdeclarator (declarator, &decl_specifiers,
9886 NORMAL, 0, &decl_specifiers.attributes);
9887 /* Turn access control back on for names used during
9888 template instantiation. */
9889 pop_deferring_access_checks ();
9890 /* Do the explicit instantiation. */
9891 do_decl_instantiation (decl, extension_specifier);
9895 pop_deferring_access_checks ();
9896 /* Skip the body of the explicit instantiation. */
9897 cp_parser_skip_to_end_of_statement (parser);
9900 /* We're done with the instantiation. */
9901 end_explicit_instantiation ();
9903 cp_parser_consume_semicolon_at_end_of_statement (parser);
9906 /* Parse an explicit-specialization.
9908 explicit-specialization:
9909 template < > declaration
9911 Although the standard says `declaration', what it really means is:
9913 explicit-specialization:
9914 template <> decl-specifier [opt] init-declarator [opt] ;
9915 template <> function-definition
9916 template <> explicit-specialization
9917 template <> template-declaration */
9920 cp_parser_explicit_specialization (cp_parser* parser)
9923 /* Look for the `template' keyword. */
9924 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9925 /* Look for the `<'. */
9926 cp_parser_require (parser, CPP_LESS, "`<'");
9927 /* Look for the `>'. */
9928 cp_parser_require (parser, CPP_GREATER, "`>'");
9929 /* We have processed another parameter list. */
9930 ++parser->num_template_parameter_lists;
9933 A template ... explicit specialization ... shall not have C
9935 if (current_lang_name == lang_name_c)
9937 error ("template specialization with C linkage");
9938 /* Give it C++ linkage to avoid confusing other parts of the
9940 push_lang_context (lang_name_cplusplus);
9941 need_lang_pop = true;
9944 need_lang_pop = false;
9945 /* Let the front end know that we are beginning a specialization. */
9946 if (!begin_specialization ())
9948 end_specialization ();
9949 cp_parser_skip_to_end_of_block_or_statement (parser);
9953 /* If the next keyword is `template', we need to figure out whether
9954 or not we're looking a template-declaration. */
9955 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9957 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9958 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9959 cp_parser_template_declaration_after_export (parser,
9960 /*member_p=*/false);
9962 cp_parser_explicit_specialization (parser);
9965 /* Parse the dependent declaration. */
9966 cp_parser_single_declaration (parser,
9970 /* We're done with the specialization. */
9971 end_specialization ();
9972 /* For the erroneous case of a template with C linkage, we pushed an
9973 implicit C++ linkage scope; exit that scope now. */
9975 pop_lang_context ();
9976 /* We're done with this parameter list. */
9977 --parser->num_template_parameter_lists;
9980 /* Parse a type-specifier.
9983 simple-type-specifier
9986 elaborated-type-specifier
9994 Returns a representation of the type-specifier. For a
9995 class-specifier, enum-specifier, or elaborated-type-specifier, a
9996 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9998 The parser flags FLAGS is used to control type-specifier parsing.
10000 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10001 in a decl-specifier-seq.
10003 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10004 class-specifier, enum-specifier, or elaborated-type-specifier, then
10005 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10006 if a type is declared; 2 if it is defined. Otherwise, it is set to
10009 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10010 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10011 is set to FALSE. */
10014 cp_parser_type_specifier (cp_parser* parser,
10015 cp_parser_flags flags,
10016 cp_decl_specifier_seq *decl_specs,
10017 bool is_declaration,
10018 int* declares_class_or_enum,
10019 bool* is_cv_qualifier)
10021 tree type_spec = NULL_TREE;
10024 cp_decl_spec ds = ds_last;
10026 /* Assume this type-specifier does not declare a new type. */
10027 if (declares_class_or_enum)
10028 *declares_class_or_enum = 0;
10029 /* And that it does not specify a cv-qualifier. */
10030 if (is_cv_qualifier)
10031 *is_cv_qualifier = false;
10032 /* Peek at the next token. */
10033 token = cp_lexer_peek_token (parser->lexer);
10035 /* If we're looking at a keyword, we can use that to guide the
10036 production we choose. */
10037 keyword = token->keyword;
10041 /* Look for the enum-specifier. */
10042 type_spec = cp_parser_enum_specifier (parser);
10043 /* If that worked, we're done. */
10046 if (declares_class_or_enum)
10047 *declares_class_or_enum = 2;
10049 cp_parser_set_decl_spec_type (decl_specs,
10051 /*user_defined_p=*/true);
10055 goto elaborated_type_specifier;
10057 /* Any of these indicate either a class-specifier, or an
10058 elaborated-type-specifier. */
10062 /* Parse tentatively so that we can back up if we don't find a
10063 class-specifier. */
10064 cp_parser_parse_tentatively (parser);
10065 /* Look for the class-specifier. */
10066 type_spec = cp_parser_class_specifier (parser);
10067 /* If that worked, we're done. */
10068 if (cp_parser_parse_definitely (parser))
10070 if (declares_class_or_enum)
10071 *declares_class_or_enum = 2;
10073 cp_parser_set_decl_spec_type (decl_specs,
10075 /*user_defined_p=*/true);
10079 /* Fall through. */
10080 elaborated_type_specifier:
10081 /* We're declaring (not defining) a class or enum. */
10082 if (declares_class_or_enum)
10083 *declares_class_or_enum = 1;
10085 /* Fall through. */
10087 /* Look for an elaborated-type-specifier. */
10089 = (cp_parser_elaborated_type_specifier
10091 decl_specs && decl_specs->specs[(int) ds_friend],
10094 cp_parser_set_decl_spec_type (decl_specs,
10096 /*user_defined_p=*/true);
10101 if (is_cv_qualifier)
10102 *is_cv_qualifier = true;
10107 if (is_cv_qualifier)
10108 *is_cv_qualifier = true;
10113 if (is_cv_qualifier)
10114 *is_cv_qualifier = true;
10118 /* The `__complex__' keyword is a GNU extension. */
10126 /* Handle simple keywords. */
10131 ++decl_specs->specs[(int)ds];
10132 decl_specs->any_specifiers_p = true;
10134 return cp_lexer_consume_token (parser->lexer)->u.value;
10137 /* If we do not already have a type-specifier, assume we are looking
10138 at a simple-type-specifier. */
10139 type_spec = cp_parser_simple_type_specifier (parser,
10143 /* If we didn't find a type-specifier, and a type-specifier was not
10144 optional in this context, issue an error message. */
10145 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10147 cp_parser_error (parser, "expected type specifier");
10148 return error_mark_node;
10154 /* Parse a simple-type-specifier.
10156 simple-type-specifier:
10157 :: [opt] nested-name-specifier [opt] type-name
10158 :: [opt] nested-name-specifier template template-id
10173 simple-type-specifier:
10174 __typeof__ unary-expression
10175 __typeof__ ( type-id )
10177 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10178 appropriately updated. */
10181 cp_parser_simple_type_specifier (cp_parser* parser,
10182 cp_decl_specifier_seq *decl_specs,
10183 cp_parser_flags flags)
10185 tree type = NULL_TREE;
10188 /* Peek at the next token. */
10189 token = cp_lexer_peek_token (parser->lexer);
10191 /* If we're looking at a keyword, things are easy. */
10192 switch (token->keyword)
10196 decl_specs->explicit_char_p = true;
10197 type = char_type_node;
10200 type = wchar_type_node;
10203 type = boolean_type_node;
10207 ++decl_specs->specs[(int) ds_short];
10208 type = short_integer_type_node;
10212 decl_specs->explicit_int_p = true;
10213 type = integer_type_node;
10217 ++decl_specs->specs[(int) ds_long];
10218 type = long_integer_type_node;
10222 ++decl_specs->specs[(int) ds_signed];
10223 type = integer_type_node;
10227 ++decl_specs->specs[(int) ds_unsigned];
10228 type = unsigned_type_node;
10231 type = float_type_node;
10234 type = double_type_node;
10237 type = void_type_node;
10241 /* Consume the `typeof' token. */
10242 cp_lexer_consume_token (parser->lexer);
10243 /* Parse the operand to `typeof'. */
10244 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10245 /* If it is not already a TYPE, take its type. */
10246 if (!TYPE_P (type))
10247 type = finish_typeof (type);
10250 cp_parser_set_decl_spec_type (decl_specs, type,
10251 /*user_defined_p=*/true);
10259 /* If the type-specifier was for a built-in type, we're done. */
10264 /* Record the type. */
10266 && (token->keyword != RID_SIGNED
10267 && token->keyword != RID_UNSIGNED
10268 && token->keyword != RID_SHORT
10269 && token->keyword != RID_LONG))
10270 cp_parser_set_decl_spec_type (decl_specs,
10272 /*user_defined=*/false);
10274 decl_specs->any_specifiers_p = true;
10276 /* Consume the token. */
10277 id = cp_lexer_consume_token (parser->lexer)->u.value;
10279 /* There is no valid C++ program where a non-template type is
10280 followed by a "<". That usually indicates that the user thought
10281 that the type was a template. */
10282 cp_parser_check_for_invalid_template_id (parser, type);
10284 return TYPE_NAME (type);
10287 /* The type-specifier must be a user-defined type. */
10288 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10293 /* Don't gobble tokens or issue error messages if this is an
10294 optional type-specifier. */
10295 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10296 cp_parser_parse_tentatively (parser);
10298 /* Look for the optional `::' operator. */
10300 = (cp_parser_global_scope_opt (parser,
10301 /*current_scope_valid_p=*/false)
10303 /* Look for the nested-name specifier. */
10305 = (cp_parser_nested_name_specifier_opt (parser,
10306 /*typename_keyword_p=*/false,
10307 /*check_dependency_p=*/true,
10309 /*is_declaration=*/false)
10311 /* If we have seen a nested-name-specifier, and the next token
10312 is `template', then we are using the template-id production. */
10314 && cp_parser_optional_template_keyword (parser))
10316 /* Look for the template-id. */
10317 type = cp_parser_template_id (parser,
10318 /*template_keyword_p=*/true,
10319 /*check_dependency_p=*/true,
10320 /*is_declaration=*/false);
10321 /* If the template-id did not name a type, we are out of
10323 if (TREE_CODE (type) != TYPE_DECL)
10325 cp_parser_error (parser, "expected template-id for type");
10329 /* Otherwise, look for a type-name. */
10331 type = cp_parser_type_name (parser);
10332 /* Keep track of all name-lookups performed in class scopes. */
10336 && TREE_CODE (type) == TYPE_DECL
10337 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10338 maybe_note_name_used_in_class (DECL_NAME (type), type);
10339 /* If it didn't work out, we don't have a TYPE. */
10340 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10341 && !cp_parser_parse_definitely (parser))
10343 if (type && decl_specs)
10344 cp_parser_set_decl_spec_type (decl_specs, type,
10345 /*user_defined=*/true);
10348 /* If we didn't get a type-name, issue an error message. */
10349 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10351 cp_parser_error (parser, "expected type-name");
10352 return error_mark_node;
10355 /* There is no valid C++ program where a non-template type is
10356 followed by a "<". That usually indicates that the user thought
10357 that the type was a template. */
10358 if (type && type != error_mark_node)
10360 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10361 If it is, then the '<'...'>' enclose protocol names rather than
10362 template arguments, and so everything is fine. */
10363 if (c_dialect_objc ()
10364 && (objc_is_id (type) || objc_is_class_name (type)))
10366 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10367 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10369 /* Clobber the "unqualified" type previously entered into
10370 DECL_SPECS with the new, improved protocol-qualified version. */
10372 decl_specs->type = qual_type;
10377 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10383 /* Parse a type-name.
10396 Returns a TYPE_DECL for the type. */
10399 cp_parser_type_name (cp_parser* parser)
10404 /* We can't know yet whether it is a class-name or not. */
10405 cp_parser_parse_tentatively (parser);
10406 /* Try a class-name. */
10407 type_decl = cp_parser_class_name (parser,
10408 /*typename_keyword_p=*/false,
10409 /*template_keyword_p=*/false,
10411 /*check_dependency_p=*/true,
10412 /*class_head_p=*/false,
10413 /*is_declaration=*/false);
10414 /* If it's not a class-name, keep looking. */
10415 if (!cp_parser_parse_definitely (parser))
10417 /* It must be a typedef-name or an enum-name. */
10418 identifier = cp_parser_identifier (parser);
10419 if (identifier == error_mark_node)
10420 return error_mark_node;
10422 /* Look up the type-name. */
10423 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10425 if (TREE_CODE (type_decl) != TYPE_DECL
10426 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10428 /* See if this is an Objective-C type. */
10429 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10430 tree type = objc_get_protocol_qualified_type (identifier, protos);
10432 type_decl = TYPE_NAME (type);
10435 /* Issue an error if we did not find a type-name. */
10436 if (TREE_CODE (type_decl) != TYPE_DECL)
10438 if (!cp_parser_simulate_error (parser))
10439 cp_parser_name_lookup_error (parser, identifier, type_decl,
10441 type_decl = error_mark_node;
10443 /* Remember that the name was used in the definition of the
10444 current class so that we can check later to see if the
10445 meaning would have been different after the class was
10446 entirely defined. */
10447 else if (type_decl != error_mark_node
10449 maybe_note_name_used_in_class (identifier, type_decl);
10456 /* Parse an elaborated-type-specifier. Note that the grammar given
10457 here incorporates the resolution to DR68.
10459 elaborated-type-specifier:
10460 class-key :: [opt] nested-name-specifier [opt] identifier
10461 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10462 enum :: [opt] nested-name-specifier [opt] identifier
10463 typename :: [opt] nested-name-specifier identifier
10464 typename :: [opt] nested-name-specifier template [opt]
10469 elaborated-type-specifier:
10470 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10471 class-key attributes :: [opt] nested-name-specifier [opt]
10472 template [opt] template-id
10473 enum attributes :: [opt] nested-name-specifier [opt] identifier
10475 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10476 declared `friend'. If IS_DECLARATION is TRUE, then this
10477 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10478 something is being declared.
10480 Returns the TYPE specified. */
10483 cp_parser_elaborated_type_specifier (cp_parser* parser,
10485 bool is_declaration)
10487 enum tag_types tag_type;
10489 tree type = NULL_TREE;
10490 tree attributes = NULL_TREE;
10492 /* See if we're looking at the `enum' keyword. */
10493 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10495 /* Consume the `enum' token. */
10496 cp_lexer_consume_token (parser->lexer);
10497 /* Remember that it's an enumeration type. */
10498 tag_type = enum_type;
10499 /* Parse the attributes. */
10500 attributes = cp_parser_attributes_opt (parser);
10502 /* Or, it might be `typename'. */
10503 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10506 /* Consume the `typename' token. */
10507 cp_lexer_consume_token (parser->lexer);
10508 /* Remember that it's a `typename' type. */
10509 tag_type = typename_type;
10510 /* The `typename' keyword is only allowed in templates. */
10511 if (!processing_template_decl)
10512 pedwarn ("using %<typename%> outside of template");
10514 /* Otherwise it must be a class-key. */
10517 tag_type = cp_parser_class_key (parser);
10518 if (tag_type == none_type)
10519 return error_mark_node;
10520 /* Parse the attributes. */
10521 attributes = cp_parser_attributes_opt (parser);
10524 /* Look for the `::' operator. */
10525 cp_parser_global_scope_opt (parser,
10526 /*current_scope_valid_p=*/false);
10527 /* Look for the nested-name-specifier. */
10528 if (tag_type == typename_type)
10530 if (!cp_parser_nested_name_specifier (parser,
10531 /*typename_keyword_p=*/true,
10532 /*check_dependency_p=*/true,
10535 return error_mark_node;
10538 /* Even though `typename' is not present, the proposed resolution
10539 to Core Issue 180 says that in `class A<T>::B', `B' should be
10540 considered a type-name, even if `A<T>' is dependent. */
10541 cp_parser_nested_name_specifier_opt (parser,
10542 /*typename_keyword_p=*/true,
10543 /*check_dependency_p=*/true,
10546 /* For everything but enumeration types, consider a template-id.
10547 For an enumeration type, consider only a plain identifier. */
10548 if (tag_type != enum_type)
10550 bool template_p = false;
10553 /* Allow the `template' keyword. */
10554 template_p = cp_parser_optional_template_keyword (parser);
10555 /* If we didn't see `template', we don't know if there's a
10556 template-id or not. */
10558 cp_parser_parse_tentatively (parser);
10559 /* Parse the template-id. */
10560 decl = cp_parser_template_id (parser, template_p,
10561 /*check_dependency_p=*/true,
10563 /* If we didn't find a template-id, look for an ordinary
10565 if (!template_p && !cp_parser_parse_definitely (parser))
10567 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10568 in effect, then we must assume that, upon instantiation, the
10569 template will correspond to a class. */
10570 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10571 && tag_type == typename_type)
10572 type = make_typename_type (parser->scope, decl,
10574 /*complain=*/tf_error);
10576 type = TREE_TYPE (decl);
10581 identifier = cp_parser_identifier (parser);
10583 if (identifier == error_mark_node)
10585 parser->scope = NULL_TREE;
10586 return error_mark_node;
10589 /* For a `typename', we needn't call xref_tag. */
10590 if (tag_type == typename_type
10591 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10592 return cp_parser_make_typename_type (parser, parser->scope,
10594 /* Look up a qualified name in the usual way. */
10599 decl = cp_parser_lookup_name (parser, identifier,
10601 /*is_template=*/false,
10602 /*is_namespace=*/false,
10603 /*check_dependency=*/true,
10604 /*ambiguous_decls=*/NULL);
10606 /* If we are parsing friend declaration, DECL may be a
10607 TEMPLATE_DECL tree node here. However, we need to check
10608 whether this TEMPLATE_DECL results in valid code. Consider
10609 the following example:
10612 template <class T> class C {};
10615 template <class T> friend class N::C; // #1, valid code
10617 template <class T> class Y {
10618 friend class N::C; // #2, invalid code
10621 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10622 name lookup of `N::C'. We see that friend declaration must
10623 be template for the code to be valid. Note that
10624 processing_template_decl does not work here since it is
10625 always 1 for the above two cases. */
10627 decl = (cp_parser_maybe_treat_template_as_class
10628 (decl, /*tag_name_p=*/is_friend
10629 && parser->num_template_parameter_lists));
10631 if (TREE_CODE (decl) != TYPE_DECL)
10633 cp_parser_diagnose_invalid_type_name (parser,
10636 return error_mark_node;
10639 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10641 bool allow_template = (parser->num_template_parameter_lists
10642 || DECL_SELF_REFERENCE_P (decl));
10643 type = check_elaborated_type_specifier (tag_type, decl,
10646 if (type == error_mark_node)
10647 return error_mark_node;
10650 type = TREE_TYPE (decl);
10654 /* An elaborated-type-specifier sometimes introduces a new type and
10655 sometimes names an existing type. Normally, the rule is that it
10656 introduces a new type only if there is not an existing type of
10657 the same name already in scope. For example, given:
10660 void f() { struct S s; }
10662 the `struct S' in the body of `f' is the same `struct S' as in
10663 the global scope; the existing definition is used. However, if
10664 there were no global declaration, this would introduce a new
10665 local class named `S'.
10667 An exception to this rule applies to the following code:
10669 namespace N { struct S; }
10671 Here, the elaborated-type-specifier names a new type
10672 unconditionally; even if there is already an `S' in the
10673 containing scope this declaration names a new type.
10674 This exception only applies if the elaborated-type-specifier
10675 forms the complete declaration:
10679 A declaration consisting solely of `class-key identifier ;' is
10680 either a redeclaration of the name in the current scope or a
10681 forward declaration of the identifier as a class name. It
10682 introduces the name into the current scope.
10684 We are in this situation precisely when the next token is a `;'.
10686 An exception to the exception is that a `friend' declaration does
10687 *not* name a new type; i.e., given:
10689 struct S { friend struct T; };
10691 `T' is not a new type in the scope of `S'.
10693 Also, `new struct S' or `sizeof (struct S)' never results in the
10694 definition of a new type; a new type can only be declared in a
10695 declaration context. */
10701 /* Friends have special name lookup rules. */
10702 ts = ts_within_enclosing_non_class;
10703 else if (is_declaration
10704 && cp_lexer_next_token_is (parser->lexer,
10706 /* This is a `class-key identifier ;' */
10712 (parser->num_template_parameter_lists
10713 && (cp_parser_next_token_starts_class_definition_p (parser)
10714 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10715 /* An unqualified name was used to reference this type, so
10716 there were no qualifying templates. */
10717 if (!cp_parser_check_template_parameters (parser,
10718 /*num_templates=*/0))
10719 return error_mark_node;
10720 type = xref_tag (tag_type, identifier, ts, template_p);
10724 if (type == error_mark_node)
10725 return error_mark_node;
10727 /* Allow attributes on forward declarations of classes. */
10730 if (TREE_CODE (type) == TYPENAME_TYPE)
10731 warning (OPT_Wattributes,
10732 "attributes ignored on uninstantiated type");
10733 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
10734 && ! processing_explicit_instantiation)
10735 warning (OPT_Wattributes,
10736 "attributes ignored on template instantiation");
10737 else if (is_declaration && cp_parser_declares_only_class_p (parser))
10738 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
10740 warning (OPT_Wattributes,
10741 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
10744 if (tag_type != enum_type)
10745 cp_parser_check_class_key (tag_type, type);
10747 /* A "<" cannot follow an elaborated type specifier. If that
10748 happens, the user was probably trying to form a template-id. */
10749 cp_parser_check_for_invalid_template_id (parser, type);
10754 /* Parse an enum-specifier.
10757 enum identifier [opt] { enumerator-list [opt] }
10760 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
10763 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
10764 if the token stream isn't an enum-specifier after all. */
10767 cp_parser_enum_specifier (cp_parser* parser)
10773 /* Parse tentatively so that we can back up if we don't find a
10775 cp_parser_parse_tentatively (parser);
10777 /* Caller guarantees that the current token is 'enum', an identifier
10778 possibly follows, and the token after that is an opening brace.
10779 If we don't have an identifier, fabricate an anonymous name for
10780 the enumeration being defined. */
10781 cp_lexer_consume_token (parser->lexer);
10783 attributes = cp_parser_attributes_opt (parser);
10785 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10786 identifier = cp_parser_identifier (parser);
10788 identifier = make_anon_name ();
10790 /* Look for the `{' but don't consume it yet. */
10791 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10792 cp_parser_simulate_error (parser);
10794 if (!cp_parser_parse_definitely (parser))
10797 /* Issue an error message if type-definitions are forbidden here. */
10798 if (!cp_parser_check_type_definition (parser))
10799 type = error_mark_node;
10801 /* Create the new type. We do this before consuming the opening
10802 brace so the enum will be recorded as being on the line of its
10803 tag (or the 'enum' keyword, if there is no tag). */
10804 type = start_enum (identifier);
10806 /* Consume the opening brace. */
10807 cp_lexer_consume_token (parser->lexer);
10809 if (type == error_mark_node)
10811 cp_parser_skip_to_end_of_block_or_statement (parser);
10812 return error_mark_node;
10815 /* If the next token is not '}', then there are some enumerators. */
10816 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10817 cp_parser_enumerator_list (parser, type);
10819 /* Consume the final '}'. */
10820 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10822 /* Look for trailing attributes to apply to this enumeration, and
10823 apply them if appropriate. */
10824 if (cp_parser_allow_gnu_extensions_p (parser))
10826 tree trailing_attr = cp_parser_attributes_opt (parser);
10827 cplus_decl_attributes (&type,
10829 (int) ATTR_FLAG_TYPE_IN_PLACE);
10832 /* Finish up the enumeration. */
10833 finish_enum (type);
10838 /* Parse an enumerator-list. The enumerators all have the indicated
10842 enumerator-definition
10843 enumerator-list , enumerator-definition */
10846 cp_parser_enumerator_list (cp_parser* parser, tree type)
10850 /* Parse an enumerator-definition. */
10851 cp_parser_enumerator_definition (parser, type);
10853 /* If the next token is not a ',', we've reached the end of
10855 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10857 /* Otherwise, consume the `,' and keep going. */
10858 cp_lexer_consume_token (parser->lexer);
10859 /* If the next token is a `}', there is a trailing comma. */
10860 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10862 if (pedantic && !in_system_header)
10863 pedwarn ("comma at end of enumerator list");
10869 /* Parse an enumerator-definition. The enumerator has the indicated
10872 enumerator-definition:
10874 enumerator = constant-expression
10880 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10885 /* Look for the identifier. */
10886 identifier = cp_parser_identifier (parser);
10887 if (identifier == error_mark_node)
10890 /* If the next token is an '=', then there is an explicit value. */
10891 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10893 /* Consume the `=' token. */
10894 cp_lexer_consume_token (parser->lexer);
10895 /* Parse the value. */
10896 value = cp_parser_constant_expression (parser,
10897 /*allow_non_constant_p=*/false,
10903 /* Create the enumerator. */
10904 build_enumerator (identifier, value, type);
10907 /* Parse a namespace-name.
10910 original-namespace-name
10913 Returns the NAMESPACE_DECL for the namespace. */
10916 cp_parser_namespace_name (cp_parser* parser)
10919 tree namespace_decl;
10921 /* Get the name of the namespace. */
10922 identifier = cp_parser_identifier (parser);
10923 if (identifier == error_mark_node)
10924 return error_mark_node;
10926 /* Look up the identifier in the currently active scope. Look only
10927 for namespaces, due to:
10929 [basic.lookup.udir]
10931 When looking up a namespace-name in a using-directive or alias
10932 definition, only namespace names are considered.
10936 [basic.lookup.qual]
10938 During the lookup of a name preceding the :: scope resolution
10939 operator, object, function, and enumerator names are ignored.
10941 (Note that cp_parser_class_or_namespace_name only calls this
10942 function if the token after the name is the scope resolution
10944 namespace_decl = cp_parser_lookup_name (parser, identifier,
10946 /*is_template=*/false,
10947 /*is_namespace=*/true,
10948 /*check_dependency=*/true,
10949 /*ambiguous_decls=*/NULL);
10950 /* If it's not a namespace, issue an error. */
10951 if (namespace_decl == error_mark_node
10952 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10954 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
10955 error ("%qD is not a namespace-name", identifier);
10956 cp_parser_error (parser, "expected namespace-name");
10957 namespace_decl = error_mark_node;
10960 return namespace_decl;
10963 /* Parse a namespace-definition.
10965 namespace-definition:
10966 named-namespace-definition
10967 unnamed-namespace-definition
10969 named-namespace-definition:
10970 original-namespace-definition
10971 extension-namespace-definition
10973 original-namespace-definition:
10974 namespace identifier { namespace-body }
10976 extension-namespace-definition:
10977 namespace original-namespace-name { namespace-body }
10979 unnamed-namespace-definition:
10980 namespace { namespace-body } */
10983 cp_parser_namespace_definition (cp_parser* parser)
10985 tree identifier, attribs;
10987 /* Look for the `namespace' keyword. */
10988 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10990 /* Get the name of the namespace. We do not attempt to distinguish
10991 between an original-namespace-definition and an
10992 extension-namespace-definition at this point. The semantic
10993 analysis routines are responsible for that. */
10994 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10995 identifier = cp_parser_identifier (parser);
10997 identifier = NULL_TREE;
10999 /* Parse any specified attributes. */
11000 attribs = cp_parser_attributes_opt (parser);
11002 /* Look for the `{' to start the namespace. */
11003 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11004 /* Start the namespace. */
11005 push_namespace_with_attribs (identifier, attribs);
11006 /* Parse the body of the namespace. */
11007 cp_parser_namespace_body (parser);
11008 /* Finish the namespace. */
11010 /* Look for the final `}'. */
11011 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11014 /* Parse a namespace-body.
11017 declaration-seq [opt] */
11020 cp_parser_namespace_body (cp_parser* parser)
11022 cp_parser_declaration_seq_opt (parser);
11025 /* Parse a namespace-alias-definition.
11027 namespace-alias-definition:
11028 namespace identifier = qualified-namespace-specifier ; */
11031 cp_parser_namespace_alias_definition (cp_parser* parser)
11034 tree namespace_specifier;
11036 /* Look for the `namespace' keyword. */
11037 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11038 /* Look for the identifier. */
11039 identifier = cp_parser_identifier (parser);
11040 if (identifier == error_mark_node)
11042 /* Look for the `=' token. */
11043 cp_parser_require (parser, CPP_EQ, "`='");
11044 /* Look for the qualified-namespace-specifier. */
11045 namespace_specifier
11046 = cp_parser_qualified_namespace_specifier (parser);
11047 /* Look for the `;' token. */
11048 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11050 /* Register the alias in the symbol table. */
11051 do_namespace_alias (identifier, namespace_specifier);
11054 /* Parse a qualified-namespace-specifier.
11056 qualified-namespace-specifier:
11057 :: [opt] nested-name-specifier [opt] namespace-name
11059 Returns a NAMESPACE_DECL corresponding to the specified
11063 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11065 /* Look for the optional `::'. */
11066 cp_parser_global_scope_opt (parser,
11067 /*current_scope_valid_p=*/false);
11069 /* Look for the optional nested-name-specifier. */
11070 cp_parser_nested_name_specifier_opt (parser,
11071 /*typename_keyword_p=*/false,
11072 /*check_dependency_p=*/true,
11074 /*is_declaration=*/true);
11076 return cp_parser_namespace_name (parser);
11079 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11080 access declaration.
11083 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11084 using :: unqualified-id ;
11086 access-declaration:
11092 cp_parser_using_declaration (cp_parser* parser,
11093 bool access_declaration_p)
11096 bool typename_p = false;
11097 bool global_scope_p;
11102 if (access_declaration_p)
11103 cp_parser_parse_tentatively (parser);
11106 /* Look for the `using' keyword. */
11107 cp_parser_require_keyword (parser, RID_USING, "`using'");
11109 /* Peek at the next token. */
11110 token = cp_lexer_peek_token (parser->lexer);
11111 /* See if it's `typename'. */
11112 if (token->keyword == RID_TYPENAME)
11114 /* Remember that we've seen it. */
11116 /* Consume the `typename' token. */
11117 cp_lexer_consume_token (parser->lexer);
11121 /* Look for the optional global scope qualification. */
11123 = (cp_parser_global_scope_opt (parser,
11124 /*current_scope_valid_p=*/false)
11127 /* If we saw `typename', or didn't see `::', then there must be a
11128 nested-name-specifier present. */
11129 if (typename_p || !global_scope_p)
11130 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11131 /*check_dependency_p=*/true,
11133 /*is_declaration=*/true);
11134 /* Otherwise, we could be in either of the two productions. In that
11135 case, treat the nested-name-specifier as optional. */
11137 qscope = cp_parser_nested_name_specifier_opt (parser,
11138 /*typename_keyword_p=*/false,
11139 /*check_dependency_p=*/true,
11141 /*is_declaration=*/true);
11143 qscope = global_namespace;
11145 if (access_declaration_p && cp_parser_error_occurred (parser))
11146 /* Something has already gone wrong; there's no need to parse
11147 further. Since an error has occurred, the return value of
11148 cp_parser_parse_definitely will be false, as required. */
11149 return cp_parser_parse_definitely (parser);
11151 /* Parse the unqualified-id. */
11152 identifier = cp_parser_unqualified_id (parser,
11153 /*template_keyword_p=*/false,
11154 /*check_dependency_p=*/true,
11155 /*declarator_p=*/true,
11156 /*optional_p=*/false);
11158 if (access_declaration_p)
11160 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11161 cp_parser_simulate_error (parser);
11162 if (!cp_parser_parse_definitely (parser))
11166 /* The function we call to handle a using-declaration is different
11167 depending on what scope we are in. */
11168 if (qscope == error_mark_node || identifier == error_mark_node)
11170 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11171 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11172 /* [namespace.udecl]
11174 A using declaration shall not name a template-id. */
11175 error ("a template-id may not appear in a using-declaration");
11178 if (at_class_scope_p ())
11180 /* Create the USING_DECL. */
11181 decl = do_class_using_decl (parser->scope, identifier);
11182 /* Add it to the list of members in this class. */
11183 finish_member_declaration (decl);
11187 decl = cp_parser_lookup_name_simple (parser, identifier);
11188 if (decl == error_mark_node)
11189 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11190 else if (!at_namespace_scope_p ())
11191 do_local_using_decl (decl, qscope, identifier);
11193 do_toplevel_using_decl (decl, qscope, identifier);
11197 /* Look for the final `;'. */
11198 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11203 /* Parse a using-directive.
11206 using namespace :: [opt] nested-name-specifier [opt]
11207 namespace-name ; */
11210 cp_parser_using_directive (cp_parser* parser)
11212 tree namespace_decl;
11215 /* Look for the `using' keyword. */
11216 cp_parser_require_keyword (parser, RID_USING, "`using'");
11217 /* And the `namespace' keyword. */
11218 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11219 /* Look for the optional `::' operator. */
11220 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11221 /* And the optional nested-name-specifier. */
11222 cp_parser_nested_name_specifier_opt (parser,
11223 /*typename_keyword_p=*/false,
11224 /*check_dependency_p=*/true,
11226 /*is_declaration=*/true);
11227 /* Get the namespace being used. */
11228 namespace_decl = cp_parser_namespace_name (parser);
11229 /* And any specified attributes. */
11230 attribs = cp_parser_attributes_opt (parser);
11231 /* Update the symbol table. */
11232 parse_using_directive (namespace_decl, attribs);
11233 /* Look for the final `;'. */
11234 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11237 /* Parse an asm-definition.
11240 asm ( string-literal ) ;
11245 asm volatile [opt] ( string-literal ) ;
11246 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11247 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11248 : asm-operand-list [opt] ) ;
11249 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11250 : asm-operand-list [opt]
11251 : asm-operand-list [opt] ) ; */
11254 cp_parser_asm_definition (cp_parser* parser)
11257 tree outputs = NULL_TREE;
11258 tree inputs = NULL_TREE;
11259 tree clobbers = NULL_TREE;
11261 bool volatile_p = false;
11262 bool extended_p = false;
11264 /* Look for the `asm' keyword. */
11265 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11266 /* See if the next token is `volatile'. */
11267 if (cp_parser_allow_gnu_extensions_p (parser)
11268 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11270 /* Remember that we saw the `volatile' keyword. */
11272 /* Consume the token. */
11273 cp_lexer_consume_token (parser->lexer);
11275 /* Look for the opening `('. */
11276 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11278 /* Look for the string. */
11279 string = cp_parser_string_literal (parser, false, false);
11280 if (string == error_mark_node)
11282 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11283 /*consume_paren=*/true);
11287 /* If we're allowing GNU extensions, check for the extended assembly
11288 syntax. Unfortunately, the `:' tokens need not be separated by
11289 a space in C, and so, for compatibility, we tolerate that here
11290 too. Doing that means that we have to treat the `::' operator as
11292 if (cp_parser_allow_gnu_extensions_p (parser)
11293 && parser->in_function_body
11294 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11295 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11297 bool inputs_p = false;
11298 bool clobbers_p = false;
11300 /* The extended syntax was used. */
11303 /* Look for outputs. */
11304 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11306 /* Consume the `:'. */
11307 cp_lexer_consume_token (parser->lexer);
11308 /* Parse the output-operands. */
11309 if (cp_lexer_next_token_is_not (parser->lexer,
11311 && cp_lexer_next_token_is_not (parser->lexer,
11313 && cp_lexer_next_token_is_not (parser->lexer,
11315 outputs = cp_parser_asm_operand_list (parser);
11317 /* If the next token is `::', there are no outputs, and the
11318 next token is the beginning of the inputs. */
11319 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11320 /* The inputs are coming next. */
11323 /* Look for inputs. */
11325 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11327 /* Consume the `:' or `::'. */
11328 cp_lexer_consume_token (parser->lexer);
11329 /* Parse the output-operands. */
11330 if (cp_lexer_next_token_is_not (parser->lexer,
11332 && cp_lexer_next_token_is_not (parser->lexer,
11334 inputs = cp_parser_asm_operand_list (parser);
11336 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11337 /* The clobbers are coming next. */
11340 /* Look for clobbers. */
11342 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11344 /* Consume the `:' or `::'. */
11345 cp_lexer_consume_token (parser->lexer);
11346 /* Parse the clobbers. */
11347 if (cp_lexer_next_token_is_not (parser->lexer,
11349 clobbers = cp_parser_asm_clobber_list (parser);
11352 /* Look for the closing `)'. */
11353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11354 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11355 /*consume_paren=*/true);
11356 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11358 /* Create the ASM_EXPR. */
11359 if (parser->in_function_body)
11361 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11363 /* If the extended syntax was not used, mark the ASM_EXPR. */
11366 tree temp = asm_stmt;
11367 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11368 temp = TREE_OPERAND (temp, 0);
11370 ASM_INPUT_P (temp) = 1;
11374 cgraph_add_asm_node (string);
11377 /* Declarators [gram.dcl.decl] */
11379 /* Parse an init-declarator.
11382 declarator initializer [opt]
11387 declarator asm-specification [opt] attributes [opt] initializer [opt]
11389 function-definition:
11390 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11392 decl-specifier-seq [opt] declarator function-try-block
11396 function-definition:
11397 __extension__ function-definition
11399 The DECL_SPECIFIERS apply to this declarator. Returns a
11400 representation of the entity declared. If MEMBER_P is TRUE, then
11401 this declarator appears in a class scope. The new DECL created by
11402 this declarator is returned.
11404 The CHECKS are access checks that should be performed once we know
11405 what entity is being declared (and, therefore, what classes have
11408 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11409 for a function-definition here as well. If the declarator is a
11410 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11411 be TRUE upon return. By that point, the function-definition will
11412 have been completely parsed.
11414 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11418 cp_parser_init_declarator (cp_parser* parser,
11419 cp_decl_specifier_seq *decl_specifiers,
11420 VEC (deferred_access_check,gc)* checks,
11421 bool function_definition_allowed_p,
11423 int declares_class_or_enum,
11424 bool* function_definition_p)
11427 cp_declarator *declarator;
11428 tree prefix_attributes;
11430 tree asm_specification;
11432 tree decl = NULL_TREE;
11434 bool is_initialized;
11435 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11436 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11438 enum cpp_ttype initialization_kind;
11439 bool is_parenthesized_init = false;
11440 bool is_non_constant_init;
11441 int ctor_dtor_or_conv_p;
11443 tree pushed_scope = NULL;
11445 /* Gather the attributes that were provided with the
11446 decl-specifiers. */
11447 prefix_attributes = decl_specifiers->attributes;
11449 /* Assume that this is not the declarator for a function
11451 if (function_definition_p)
11452 *function_definition_p = false;
11454 /* Defer access checks while parsing the declarator; we cannot know
11455 what names are accessible until we know what is being
11457 resume_deferring_access_checks ();
11459 /* Parse the declarator. */
11461 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11462 &ctor_dtor_or_conv_p,
11463 /*parenthesized_p=*/NULL,
11464 /*member_p=*/false);
11465 /* Gather up the deferred checks. */
11466 stop_deferring_access_checks ();
11468 /* If the DECLARATOR was erroneous, there's no need to go
11470 if (declarator == cp_error_declarator)
11471 return error_mark_node;
11473 /* Check that the number of template-parameter-lists is OK. */
11474 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11475 return error_mark_node;
11477 if (declares_class_or_enum & 2)
11478 cp_parser_check_for_definition_in_return_type (declarator,
11479 decl_specifiers->type);
11481 /* Figure out what scope the entity declared by the DECLARATOR is
11482 located in. `grokdeclarator' sometimes changes the scope, so
11483 we compute it now. */
11484 scope = get_scope_of_declarator (declarator);
11486 /* If we're allowing GNU extensions, look for an asm-specification
11488 if (cp_parser_allow_gnu_extensions_p (parser))
11490 /* Look for an asm-specification. */
11491 asm_specification = cp_parser_asm_specification_opt (parser);
11492 /* And attributes. */
11493 attributes = cp_parser_attributes_opt (parser);
11497 asm_specification = NULL_TREE;
11498 attributes = NULL_TREE;
11501 /* Peek at the next token. */
11502 token = cp_lexer_peek_token (parser->lexer);
11503 /* Check to see if the token indicates the start of a
11504 function-definition. */
11505 if (cp_parser_token_starts_function_definition_p (token))
11507 if (!function_definition_allowed_p)
11509 /* If a function-definition should not appear here, issue an
11511 cp_parser_error (parser,
11512 "a function-definition is not allowed here");
11513 return error_mark_node;
11517 /* Neither attributes nor an asm-specification are allowed
11518 on a function-definition. */
11519 if (asm_specification)
11520 error ("an asm-specification is not allowed on a function-definition");
11522 error ("attributes are not allowed on a function-definition");
11523 /* This is a function-definition. */
11524 *function_definition_p = true;
11526 /* Parse the function definition. */
11528 decl = cp_parser_save_member_function_body (parser,
11531 prefix_attributes);
11534 = (cp_parser_function_definition_from_specifiers_and_declarator
11535 (parser, decl_specifiers, prefix_attributes, declarator));
11543 Only in function declarations for constructors, destructors, and
11544 type conversions can the decl-specifier-seq be omitted.
11546 We explicitly postpone this check past the point where we handle
11547 function-definitions because we tolerate function-definitions
11548 that are missing their return types in some modes. */
11549 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11551 cp_parser_error (parser,
11552 "expected constructor, destructor, or type conversion");
11553 return error_mark_node;
11556 /* An `=' or an `(' indicates an initializer. */
11557 if (token->type == CPP_EQ
11558 || token->type == CPP_OPEN_PAREN)
11560 is_initialized = true;
11561 initialization_kind = token->type;
11565 /* If the init-declarator isn't initialized and isn't followed by a
11566 `,' or `;', it's not a valid init-declarator. */
11567 if (token->type != CPP_COMMA
11568 && token->type != CPP_SEMICOLON)
11570 cp_parser_error (parser, "expected initializer");
11571 return error_mark_node;
11573 is_initialized = false;
11574 initialization_kind = CPP_EOF;
11577 /* Because start_decl has side-effects, we should only call it if we
11578 know we're going ahead. By this point, we know that we cannot
11579 possibly be looking at any other construct. */
11580 cp_parser_commit_to_tentative_parse (parser);
11582 /* If the decl specifiers were bad, issue an error now that we're
11583 sure this was intended to be a declarator. Then continue
11584 declaring the variable(s), as int, to try to cut down on further
11586 if (decl_specifiers->any_specifiers_p
11587 && decl_specifiers->type == error_mark_node)
11589 cp_parser_error (parser, "invalid type in declaration");
11590 decl_specifiers->type = integer_type_node;
11593 /* Check to see whether or not this declaration is a friend. */
11594 friend_p = cp_parser_friend_p (decl_specifiers);
11596 /* Enter the newly declared entry in the symbol table. If we're
11597 processing a declaration in a class-specifier, we wait until
11598 after processing the initializer. */
11601 if (parser->in_unbraced_linkage_specification_p)
11602 decl_specifiers->storage_class = sc_extern;
11603 decl = start_decl (declarator, decl_specifiers,
11604 is_initialized, attributes, prefix_attributes,
11608 /* Enter the SCOPE. That way unqualified names appearing in the
11609 initializer will be looked up in SCOPE. */
11610 pushed_scope = push_scope (scope);
11612 /* Perform deferred access control checks, now that we know in which
11613 SCOPE the declared entity resides. */
11614 if (!member_p && decl)
11616 tree saved_current_function_decl = NULL_TREE;
11618 /* If the entity being declared is a function, pretend that we
11619 are in its scope. If it is a `friend', it may have access to
11620 things that would not otherwise be accessible. */
11621 if (TREE_CODE (decl) == FUNCTION_DECL)
11623 saved_current_function_decl = current_function_decl;
11624 current_function_decl = decl;
11627 /* Perform access checks for template parameters. */
11628 cp_parser_perform_template_parameter_access_checks (checks);
11630 /* Perform the access control checks for the declarator and the
11631 the decl-specifiers. */
11632 perform_deferred_access_checks ();
11634 /* Restore the saved value. */
11635 if (TREE_CODE (decl) == FUNCTION_DECL)
11636 current_function_decl = saved_current_function_decl;
11639 /* Parse the initializer. */
11640 initializer = NULL_TREE;
11641 is_parenthesized_init = false;
11642 is_non_constant_init = true;
11643 if (is_initialized)
11645 if (function_declarator_p (declarator))
11647 if (initialization_kind == CPP_EQ)
11648 initializer = cp_parser_pure_specifier (parser);
11651 /* If the declaration was erroneous, we don't really
11652 know what the user intended, so just silently
11653 consume the initializer. */
11654 if (decl != error_mark_node)
11655 error ("initializer provided for function");
11656 cp_parser_skip_to_closing_parenthesis (parser,
11657 /*recovering=*/true,
11658 /*or_comma=*/false,
11659 /*consume_paren=*/true);
11663 initializer = cp_parser_initializer (parser,
11664 &is_parenthesized_init,
11665 &is_non_constant_init);
11668 /* The old parser allows attributes to appear after a parenthesized
11669 initializer. Mark Mitchell proposed removing this functionality
11670 on the GCC mailing lists on 2002-08-13. This parser accepts the
11671 attributes -- but ignores them. */
11672 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11673 if (cp_parser_attributes_opt (parser))
11674 warning (OPT_Wattributes,
11675 "attributes after parenthesized initializer ignored");
11677 /* For an in-class declaration, use `grokfield' to create the
11683 pop_scope (pushed_scope);
11684 pushed_scope = false;
11686 decl = grokfield (declarator, decl_specifiers,
11687 initializer, !is_non_constant_init,
11688 /*asmspec=*/NULL_TREE,
11689 prefix_attributes);
11690 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11691 cp_parser_save_default_args (parser, decl);
11694 /* Finish processing the declaration. But, skip friend
11696 if (!friend_p && decl && decl != error_mark_node)
11698 cp_finish_decl (decl,
11699 initializer, !is_non_constant_init,
11701 /* If the initializer is in parentheses, then this is
11702 a direct-initialization, which means that an
11703 `explicit' constructor is OK. Otherwise, an
11704 `explicit' constructor cannot be used. */
11705 ((is_parenthesized_init || !is_initialized)
11706 ? 0 : LOOKUP_ONLYCONVERTING));
11708 if (!friend_p && pushed_scope)
11709 pop_scope (pushed_scope);
11714 /* Parse a declarator.
11718 ptr-operator declarator
11720 abstract-declarator:
11721 ptr-operator abstract-declarator [opt]
11722 direct-abstract-declarator
11727 attributes [opt] direct-declarator
11728 attributes [opt] ptr-operator declarator
11730 abstract-declarator:
11731 attributes [opt] ptr-operator abstract-declarator [opt]
11732 attributes [opt] direct-abstract-declarator
11734 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
11735 detect constructor, destructor or conversion operators. It is set
11736 to -1 if the declarator is a name, and +1 if it is a
11737 function. Otherwise it is set to zero. Usually you just want to
11738 test for >0, but internally the negative value is used.
11740 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
11741 a decl-specifier-seq unless it declares a constructor, destructor,
11742 or conversion. It might seem that we could check this condition in
11743 semantic analysis, rather than parsing, but that makes it difficult
11744 to handle something like `f()'. We want to notice that there are
11745 no decl-specifiers, and therefore realize that this is an
11746 expression, not a declaration.)
11748 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11749 the declarator is a direct-declarator of the form "(...)".
11751 MEMBER_P is true iff this declarator is a member-declarator. */
11753 static cp_declarator *
11754 cp_parser_declarator (cp_parser* parser,
11755 cp_parser_declarator_kind dcl_kind,
11756 int* ctor_dtor_or_conv_p,
11757 bool* parenthesized_p,
11761 cp_declarator *declarator;
11762 enum tree_code code;
11763 cp_cv_quals cv_quals;
11765 tree attributes = NULL_TREE;
11767 /* Assume this is not a constructor, destructor, or type-conversion
11769 if (ctor_dtor_or_conv_p)
11770 *ctor_dtor_or_conv_p = 0;
11772 if (cp_parser_allow_gnu_extensions_p (parser))
11773 attributes = cp_parser_attributes_opt (parser);
11775 /* Peek at the next token. */
11776 token = cp_lexer_peek_token (parser->lexer);
11778 /* Check for the ptr-operator production. */
11779 cp_parser_parse_tentatively (parser);
11780 /* Parse the ptr-operator. */
11781 code = cp_parser_ptr_operator (parser,
11784 /* If that worked, then we have a ptr-operator. */
11785 if (cp_parser_parse_definitely (parser))
11787 /* If a ptr-operator was found, then this declarator was not
11789 if (parenthesized_p)
11790 *parenthesized_p = true;
11791 /* The dependent declarator is optional if we are parsing an
11792 abstract-declarator. */
11793 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11794 cp_parser_parse_tentatively (parser);
11796 /* Parse the dependent declarator. */
11797 declarator = cp_parser_declarator (parser, dcl_kind,
11798 /*ctor_dtor_or_conv_p=*/NULL,
11799 /*parenthesized_p=*/NULL,
11800 /*member_p=*/false);
11802 /* If we are parsing an abstract-declarator, we must handle the
11803 case where the dependent declarator is absent. */
11804 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11805 && !cp_parser_parse_definitely (parser))
11808 /* Build the representation of the ptr-operator. */
11810 declarator = make_ptrmem_declarator (cv_quals,
11813 else if (code == INDIRECT_REF)
11814 declarator = make_pointer_declarator (cv_quals, declarator);
11816 declarator = make_reference_declarator (cv_quals, declarator);
11818 /* Everything else is a direct-declarator. */
11821 if (parenthesized_p)
11822 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11824 declarator = cp_parser_direct_declarator (parser, dcl_kind,
11825 ctor_dtor_or_conv_p,
11829 if (attributes && declarator && declarator != cp_error_declarator)
11830 declarator->attributes = attributes;
11835 /* Parse a direct-declarator or direct-abstract-declarator.
11839 direct-declarator ( parameter-declaration-clause )
11840 cv-qualifier-seq [opt]
11841 exception-specification [opt]
11842 direct-declarator [ constant-expression [opt] ]
11845 direct-abstract-declarator:
11846 direct-abstract-declarator [opt]
11847 ( parameter-declaration-clause )
11848 cv-qualifier-seq [opt]
11849 exception-specification [opt]
11850 direct-abstract-declarator [opt] [ constant-expression [opt] ]
11851 ( abstract-declarator )
11853 Returns a representation of the declarator. DCL_KIND is
11854 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11855 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
11856 we are parsing a direct-declarator. It is
11857 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11858 of ambiguity we prefer an abstract declarator, as per
11859 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11860 cp_parser_declarator. */
11862 static cp_declarator *
11863 cp_parser_direct_declarator (cp_parser* parser,
11864 cp_parser_declarator_kind dcl_kind,
11865 int* ctor_dtor_or_conv_p,
11869 cp_declarator *declarator = NULL;
11870 tree scope = NULL_TREE;
11871 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11872 bool saved_in_declarator_p = parser->in_declarator_p;
11874 tree pushed_scope = NULL_TREE;
11878 /* Peek at the next token. */
11879 token = cp_lexer_peek_token (parser->lexer);
11880 if (token->type == CPP_OPEN_PAREN)
11882 /* This is either a parameter-declaration-clause, or a
11883 parenthesized declarator. When we know we are parsing a
11884 named declarator, it must be a parenthesized declarator
11885 if FIRST is true. For instance, `(int)' is a
11886 parameter-declaration-clause, with an omitted
11887 direct-abstract-declarator. But `((*))', is a
11888 parenthesized abstract declarator. Finally, when T is a
11889 template parameter `(T)' is a
11890 parameter-declaration-clause, and not a parenthesized
11893 We first try and parse a parameter-declaration-clause,
11894 and then try a nested declarator (if FIRST is true).
11896 It is not an error for it not to be a
11897 parameter-declaration-clause, even when FIRST is
11903 The first is the declaration of a function while the
11904 second is a the definition of a variable, including its
11907 Having seen only the parenthesis, we cannot know which of
11908 these two alternatives should be selected. Even more
11909 complex are examples like:
11914 The former is a function-declaration; the latter is a
11915 variable initialization.
11917 Thus again, we try a parameter-declaration-clause, and if
11918 that fails, we back out and return. */
11920 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11922 cp_parameter_declarator *params;
11923 unsigned saved_num_template_parameter_lists;
11925 /* In a member-declarator, the only valid interpretation
11926 of a parenthesis is the start of a
11927 parameter-declaration-clause. (It is invalid to
11928 initialize a static data member with a parenthesized
11929 initializer; only the "=" form of initialization is
11932 cp_parser_parse_tentatively (parser);
11934 /* Consume the `('. */
11935 cp_lexer_consume_token (parser->lexer);
11938 /* If this is going to be an abstract declarator, we're
11939 in a declarator and we can't have default args. */
11940 parser->default_arg_ok_p = false;
11941 parser->in_declarator_p = true;
11944 /* Inside the function parameter list, surrounding
11945 template-parameter-lists do not apply. */
11946 saved_num_template_parameter_lists
11947 = parser->num_template_parameter_lists;
11948 parser->num_template_parameter_lists = 0;
11950 /* Parse the parameter-declaration-clause. */
11951 params = cp_parser_parameter_declaration_clause (parser);
11953 parser->num_template_parameter_lists
11954 = saved_num_template_parameter_lists;
11956 /* If all went well, parse the cv-qualifier-seq and the
11957 exception-specification. */
11958 if (member_p || cp_parser_parse_definitely (parser))
11960 cp_cv_quals cv_quals;
11961 tree exception_specification;
11963 if (ctor_dtor_or_conv_p)
11964 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11966 /* Consume the `)'. */
11967 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11969 /* Parse the cv-qualifier-seq. */
11970 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11971 /* And the exception-specification. */
11972 exception_specification
11973 = cp_parser_exception_specification_opt (parser);
11975 /* Create the function-declarator. */
11976 declarator = make_call_declarator (declarator,
11979 exception_specification);
11980 /* Any subsequent parameter lists are to do with
11981 return type, so are not those of the declared
11983 parser->default_arg_ok_p = false;
11985 /* Repeat the main loop. */
11990 /* If this is the first, we can try a parenthesized
11994 bool saved_in_type_id_in_expr_p;
11996 parser->default_arg_ok_p = saved_default_arg_ok_p;
11997 parser->in_declarator_p = saved_in_declarator_p;
11999 /* Consume the `('. */
12000 cp_lexer_consume_token (parser->lexer);
12001 /* Parse the nested declarator. */
12002 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12003 parser->in_type_id_in_expr_p = true;
12005 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12006 /*parenthesized_p=*/NULL,
12008 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12010 /* Expect a `)'. */
12011 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12012 declarator = cp_error_declarator;
12013 if (declarator == cp_error_declarator)
12016 goto handle_declarator;
12018 /* Otherwise, we must be done. */
12022 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12023 && token->type == CPP_OPEN_SQUARE)
12025 /* Parse an array-declarator. */
12028 if (ctor_dtor_or_conv_p)
12029 *ctor_dtor_or_conv_p = 0;
12032 parser->default_arg_ok_p = false;
12033 parser->in_declarator_p = true;
12034 /* Consume the `['. */
12035 cp_lexer_consume_token (parser->lexer);
12036 /* Peek at the next token. */
12037 token = cp_lexer_peek_token (parser->lexer);
12038 /* If the next token is `]', then there is no
12039 constant-expression. */
12040 if (token->type != CPP_CLOSE_SQUARE)
12042 bool non_constant_p;
12045 = cp_parser_constant_expression (parser,
12046 /*allow_non_constant=*/true,
12048 if (!non_constant_p)
12049 bounds = fold_non_dependent_expr (bounds);
12050 /* Normally, the array bound must be an integral constant
12051 expression. However, as an extension, we allow VLAs
12052 in function scopes. */
12053 else if (!parser->in_function_body)
12055 error ("array bound is not an integer constant");
12056 bounds = error_mark_node;
12060 bounds = NULL_TREE;
12061 /* Look for the closing `]'. */
12062 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12064 declarator = cp_error_declarator;
12068 declarator = make_array_declarator (declarator, bounds);
12070 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12072 tree qualifying_scope;
12073 tree unqualified_name;
12074 special_function_kind sfk;
12076 bool pack_expansion_p = false;
12078 /* Parse a declarator-id */
12079 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12082 cp_parser_parse_tentatively (parser);
12084 /* If we see an ellipsis, we should be looking at a
12086 if (token->type == CPP_ELLIPSIS)
12088 /* Consume the `...' */
12089 cp_lexer_consume_token (parser->lexer);
12091 pack_expansion_p = true;
12096 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12097 qualifying_scope = parser->scope;
12102 if (!unqualified_name && pack_expansion_p)
12104 /* Check whether an error occurred. */
12105 okay = !cp_parser_error_occurred (parser);
12107 /* We already consumed the ellipsis to mark a
12108 parameter pack, but we have no way to report it,
12109 so abort the tentative parse. We will be exiting
12110 immediately anyway. */
12111 cp_parser_abort_tentative_parse (parser);
12114 okay = cp_parser_parse_definitely (parser);
12117 unqualified_name = error_mark_node;
12118 else if (unqualified_name
12119 && (qualifying_scope
12120 || (TREE_CODE (unqualified_name)
12121 != IDENTIFIER_NODE)))
12123 cp_parser_error (parser, "expected unqualified-id");
12124 unqualified_name = error_mark_node;
12128 if (!unqualified_name)
12130 if (unqualified_name == error_mark_node)
12132 declarator = cp_error_declarator;
12133 pack_expansion_p = false;
12134 declarator->parameter_pack_p = false;
12138 if (qualifying_scope && at_namespace_scope_p ()
12139 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12141 /* In the declaration of a member of a template class
12142 outside of the class itself, the SCOPE will sometimes
12143 be a TYPENAME_TYPE. For example, given:
12145 template <typename T>
12146 int S<T>::R::i = 3;
12148 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12149 this context, we must resolve S<T>::R to an ordinary
12150 type, rather than a typename type.
12152 The reason we normally avoid resolving TYPENAME_TYPEs
12153 is that a specialization of `S' might render
12154 `S<T>::R' not a type. However, if `S' is
12155 specialized, then this `i' will not be used, so there
12156 is no harm in resolving the types here. */
12159 /* Resolve the TYPENAME_TYPE. */
12160 type = resolve_typename_type (qualifying_scope,
12161 /*only_current_p=*/false);
12162 /* If that failed, the declarator is invalid. */
12163 if (type == error_mark_node)
12164 error ("%<%T::%D%> is not a type",
12165 TYPE_CONTEXT (qualifying_scope),
12166 TYPE_IDENTIFIER (qualifying_scope));
12167 qualifying_scope = type;
12172 if (unqualified_name)
12176 if (qualifying_scope
12177 && CLASS_TYPE_P (qualifying_scope))
12178 class_type = qualifying_scope;
12180 class_type = current_class_type;
12182 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12184 tree name_type = TREE_TYPE (unqualified_name);
12185 if (class_type && same_type_p (name_type, class_type))
12187 if (qualifying_scope
12188 && CLASSTYPE_USE_TEMPLATE (name_type))
12190 error ("invalid use of constructor as a template");
12191 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12192 "name the constructor in a qualified name",
12194 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12195 class_type, name_type);
12196 declarator = cp_error_declarator;
12200 unqualified_name = constructor_name (class_type);
12204 /* We do not attempt to print the declarator
12205 here because we do not have enough
12206 information about its original syntactic
12208 cp_parser_error (parser, "invalid declarator");
12209 declarator = cp_error_declarator;
12216 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12217 sfk = sfk_destructor;
12218 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12219 sfk = sfk_conversion;
12220 else if (/* There's no way to declare a constructor
12221 for an anonymous type, even if the type
12222 got a name for linkage purposes. */
12223 !TYPE_WAS_ANONYMOUS (class_type)
12224 && constructor_name_p (unqualified_name,
12227 unqualified_name = constructor_name (class_type);
12228 sfk = sfk_constructor;
12231 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12232 *ctor_dtor_or_conv_p = -1;
12235 declarator = make_id_declarator (qualifying_scope,
12238 declarator->id_loc = token->location;
12239 declarator->parameter_pack_p = pack_expansion_p;
12241 if (pack_expansion_p)
12242 maybe_warn_variadic_templates ();
12244 handle_declarator:;
12245 scope = get_scope_of_declarator (declarator);
12247 /* Any names that appear after the declarator-id for a
12248 member are looked up in the containing scope. */
12249 pushed_scope = push_scope (scope);
12250 parser->in_declarator_p = true;
12251 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12252 || (declarator && declarator->kind == cdk_id))
12253 /* Default args are only allowed on function
12255 parser->default_arg_ok_p = saved_default_arg_ok_p;
12257 parser->default_arg_ok_p = false;
12266 /* For an abstract declarator, we might wind up with nothing at this
12267 point. That's an error; the declarator is not optional. */
12269 cp_parser_error (parser, "expected declarator");
12271 /* If we entered a scope, we must exit it now. */
12273 pop_scope (pushed_scope);
12275 parser->default_arg_ok_p = saved_default_arg_ok_p;
12276 parser->in_declarator_p = saved_in_declarator_p;
12281 /* Parse a ptr-operator.
12284 * cv-qualifier-seq [opt]
12286 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12291 & cv-qualifier-seq [opt]
12293 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12294 Returns ADDR_EXPR if a reference was used. In the case of a
12295 pointer-to-member, *TYPE is filled in with the TYPE containing the
12296 member. *CV_QUALS is filled in with the cv-qualifier-seq, or
12297 TYPE_UNQUALIFIED, if there are no cv-qualifiers. Returns
12298 ERROR_MARK if an error occurred. */
12300 static enum tree_code
12301 cp_parser_ptr_operator (cp_parser* parser,
12303 cp_cv_quals *cv_quals)
12305 enum tree_code code = ERROR_MARK;
12308 /* Assume that it's not a pointer-to-member. */
12310 /* And that there are no cv-qualifiers. */
12311 *cv_quals = TYPE_UNQUALIFIED;
12313 /* Peek at the next token. */
12314 token = cp_lexer_peek_token (parser->lexer);
12315 /* If it's a `*' or `&' we have a pointer or reference. */
12316 if (token->type == CPP_MULT || token->type == CPP_AND)
12318 /* Remember which ptr-operator we were processing. */
12319 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
12321 /* Consume the `*' or `&'. */
12322 cp_lexer_consume_token (parser->lexer);
12324 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12325 `&', if we are allowing GNU extensions. (The only qualifier
12326 that can legally appear after `&' is `restrict', but that is
12327 enforced during semantic analysis. */
12328 if (code == INDIRECT_REF
12329 || cp_parser_allow_gnu_extensions_p (parser))
12330 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12334 /* Try the pointer-to-member case. */
12335 cp_parser_parse_tentatively (parser);
12336 /* Look for the optional `::' operator. */
12337 cp_parser_global_scope_opt (parser,
12338 /*current_scope_valid_p=*/false);
12339 /* Look for the nested-name specifier. */
12340 cp_parser_nested_name_specifier (parser,
12341 /*typename_keyword_p=*/false,
12342 /*check_dependency_p=*/true,
12344 /*is_declaration=*/false);
12345 /* If we found it, and the next token is a `*', then we are
12346 indeed looking at a pointer-to-member operator. */
12347 if (!cp_parser_error_occurred (parser)
12348 && cp_parser_require (parser, CPP_MULT, "`*'"))
12350 /* Indicate that the `*' operator was used. */
12351 code = INDIRECT_REF;
12353 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12354 error ("%qD is a namespace", parser->scope);
12357 /* The type of which the member is a member is given by the
12359 *type = parser->scope;
12360 /* The next name will not be qualified. */
12361 parser->scope = NULL_TREE;
12362 parser->qualifying_scope = NULL_TREE;
12363 parser->object_scope = NULL_TREE;
12364 /* Look for the optional cv-qualifier-seq. */
12365 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12368 /* If that didn't work we don't have a ptr-operator. */
12369 if (!cp_parser_parse_definitely (parser))
12370 cp_parser_error (parser, "expected ptr-operator");
12376 /* Parse an (optional) cv-qualifier-seq.
12379 cv-qualifier cv-qualifier-seq [opt]
12390 Returns a bitmask representing the cv-qualifiers. */
12393 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12395 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12400 cp_cv_quals cv_qualifier;
12402 /* Peek at the next token. */
12403 token = cp_lexer_peek_token (parser->lexer);
12404 /* See if it's a cv-qualifier. */
12405 switch (token->keyword)
12408 cv_qualifier = TYPE_QUAL_CONST;
12412 cv_qualifier = TYPE_QUAL_VOLATILE;
12416 cv_qualifier = TYPE_QUAL_RESTRICT;
12420 cv_qualifier = TYPE_UNQUALIFIED;
12427 if (cv_quals & cv_qualifier)
12429 error ("duplicate cv-qualifier");
12430 cp_lexer_purge_token (parser->lexer);
12434 cp_lexer_consume_token (parser->lexer);
12435 cv_quals |= cv_qualifier;
12442 /* Parse a declarator-id.
12446 :: [opt] nested-name-specifier [opt] type-name
12448 In the `id-expression' case, the value returned is as for
12449 cp_parser_id_expression if the id-expression was an unqualified-id.
12450 If the id-expression was a qualified-id, then a SCOPE_REF is
12451 returned. The first operand is the scope (either a NAMESPACE_DECL
12452 or TREE_TYPE), but the second is still just a representation of an
12456 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12459 /* The expression must be an id-expression. Assume that qualified
12460 names are the names of types so that:
12463 int S<T>::R::i = 3;
12465 will work; we must treat `S<T>::R' as the name of a type.
12466 Similarly, assume that qualified names are templates, where
12470 int S<T>::R<T>::i = 3;
12473 id = cp_parser_id_expression (parser,
12474 /*template_keyword_p=*/false,
12475 /*check_dependency_p=*/false,
12476 /*template_p=*/NULL,
12477 /*declarator_p=*/true,
12479 if (id && BASELINK_P (id))
12480 id = BASELINK_FUNCTIONS (id);
12484 /* Parse a type-id.
12487 type-specifier-seq abstract-declarator [opt]
12489 Returns the TYPE specified. */
12492 cp_parser_type_id (cp_parser* parser)
12494 cp_decl_specifier_seq type_specifier_seq;
12495 cp_declarator *abstract_declarator;
12497 /* Parse the type-specifier-seq. */
12498 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12499 &type_specifier_seq);
12500 if (type_specifier_seq.type == error_mark_node)
12501 return error_mark_node;
12503 /* There might or might not be an abstract declarator. */
12504 cp_parser_parse_tentatively (parser);
12505 /* Look for the declarator. */
12506 abstract_declarator
12507 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12508 /*parenthesized_p=*/NULL,
12509 /*member_p=*/false);
12510 /* Check to see if there really was a declarator. */
12511 if (!cp_parser_parse_definitely (parser))
12512 abstract_declarator = NULL;
12514 return groktypename (&type_specifier_seq, abstract_declarator);
12517 /* Parse a type-specifier-seq.
12519 type-specifier-seq:
12520 type-specifier type-specifier-seq [opt]
12524 type-specifier-seq:
12525 attributes type-specifier-seq [opt]
12527 If IS_CONDITION is true, we are at the start of a "condition",
12528 e.g., we've just seen "if (".
12530 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12533 cp_parser_type_specifier_seq (cp_parser* parser,
12535 cp_decl_specifier_seq *type_specifier_seq)
12537 bool seen_type_specifier = false;
12538 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12540 /* Clear the TYPE_SPECIFIER_SEQ. */
12541 clear_decl_specs (type_specifier_seq);
12543 /* Parse the type-specifiers and attributes. */
12546 tree type_specifier;
12547 bool is_cv_qualifier;
12549 /* Check for attributes first. */
12550 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12552 type_specifier_seq->attributes =
12553 chainon (type_specifier_seq->attributes,
12554 cp_parser_attributes_opt (parser));
12558 /* Look for the type-specifier. */
12559 type_specifier = cp_parser_type_specifier (parser,
12561 type_specifier_seq,
12562 /*is_declaration=*/false,
12565 if (!type_specifier)
12567 /* If the first type-specifier could not be found, this is not a
12568 type-specifier-seq at all. */
12569 if (!seen_type_specifier)
12571 cp_parser_error (parser, "expected type-specifier");
12572 type_specifier_seq->type = error_mark_node;
12575 /* If subsequent type-specifiers could not be found, the
12576 type-specifier-seq is complete. */
12580 seen_type_specifier = true;
12581 /* The standard says that a condition can be:
12583 type-specifier-seq declarator = assignment-expression
12590 we should treat the "S" as a declarator, not as a
12591 type-specifier. The standard doesn't say that explicitly for
12592 type-specifier-seq, but it does say that for
12593 decl-specifier-seq in an ordinary declaration. Perhaps it
12594 would be clearer just to allow a decl-specifier-seq here, and
12595 then add a semantic restriction that if any decl-specifiers
12596 that are not type-specifiers appear, the program is invalid. */
12597 if (is_condition && !is_cv_qualifier)
12598 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12601 cp_parser_check_decl_spec (type_specifier_seq);
12604 /* Parse a parameter-declaration-clause.
12606 parameter-declaration-clause:
12607 parameter-declaration-list [opt] ... [opt]
12608 parameter-declaration-list , ...
12610 Returns a representation for the parameter declarations. A return
12611 value of NULL indicates a parameter-declaration-clause consisting
12612 only of an ellipsis. */
12614 static cp_parameter_declarator *
12615 cp_parser_parameter_declaration_clause (cp_parser* parser)
12617 cp_parameter_declarator *parameters;
12622 /* Peek at the next token. */
12623 token = cp_lexer_peek_token (parser->lexer);
12624 /* Check for trivial parameter-declaration-clauses. */
12625 if (token->type == CPP_ELLIPSIS)
12627 /* Consume the `...' token. */
12628 cp_lexer_consume_token (parser->lexer);
12631 else if (token->type == CPP_CLOSE_PAREN)
12632 /* There are no parameters. */
12634 #ifndef NO_IMPLICIT_EXTERN_C
12635 if (in_system_header && current_class_type == NULL
12636 && current_lang_name == lang_name_c)
12640 return no_parameters;
12642 /* Check for `(void)', too, which is a special case. */
12643 else if (token->keyword == RID_VOID
12644 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12645 == CPP_CLOSE_PAREN))
12647 /* Consume the `void' token. */
12648 cp_lexer_consume_token (parser->lexer);
12649 /* There are no parameters. */
12650 return no_parameters;
12653 /* Parse the parameter-declaration-list. */
12654 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12655 /* If a parse error occurred while parsing the
12656 parameter-declaration-list, then the entire
12657 parameter-declaration-clause is erroneous. */
12661 /* Peek at the next token. */
12662 token = cp_lexer_peek_token (parser->lexer);
12663 /* If it's a `,', the clause should terminate with an ellipsis. */
12664 if (token->type == CPP_COMMA)
12666 /* Consume the `,'. */
12667 cp_lexer_consume_token (parser->lexer);
12668 /* Expect an ellipsis. */
12670 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12672 /* It might also be `...' if the optional trailing `,' was
12674 else if (token->type == CPP_ELLIPSIS)
12676 /* Consume the `...' token. */
12677 cp_lexer_consume_token (parser->lexer);
12678 /* And remember that we saw it. */
12682 ellipsis_p = false;
12684 /* Finish the parameter list. */
12685 if (parameters && ellipsis_p)
12686 parameters->ellipsis_p = true;
12691 /* Parse a parameter-declaration-list.
12693 parameter-declaration-list:
12694 parameter-declaration
12695 parameter-declaration-list , parameter-declaration
12697 Returns a representation of the parameter-declaration-list, as for
12698 cp_parser_parameter_declaration_clause. However, the
12699 `void_list_node' is never appended to the list. Upon return,
12700 *IS_ERROR will be true iff an error occurred. */
12702 static cp_parameter_declarator *
12703 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12705 cp_parameter_declarator *parameters = NULL;
12706 cp_parameter_declarator **tail = ¶meters;
12707 bool saved_in_unbraced_linkage_specification_p;
12709 /* Assume all will go well. */
12711 /* The special considerations that apply to a function within an
12712 unbraced linkage specifications do not apply to the parameters
12713 to the function. */
12714 saved_in_unbraced_linkage_specification_p
12715 = parser->in_unbraced_linkage_specification_p;
12716 parser->in_unbraced_linkage_specification_p = false;
12718 /* Look for more parameters. */
12721 cp_parameter_declarator *parameter;
12722 bool parenthesized_p;
12723 /* Parse the parameter. */
12725 = cp_parser_parameter_declaration (parser,
12726 /*template_parm_p=*/false,
12729 /* If a parse error occurred parsing the parameter declaration,
12730 then the entire parameter-declaration-list is erroneous. */
12737 /* Add the new parameter to the list. */
12739 tail = ¶meter->next;
12741 /* Peek at the next token. */
12742 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
12743 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12744 /* These are for Objective-C++ */
12745 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12746 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12747 /* The parameter-declaration-list is complete. */
12749 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12753 /* Peek at the next token. */
12754 token = cp_lexer_peek_nth_token (parser->lexer, 2);
12755 /* If it's an ellipsis, then the list is complete. */
12756 if (token->type == CPP_ELLIPSIS)
12758 /* Otherwise, there must be more parameters. Consume the
12760 cp_lexer_consume_token (parser->lexer);
12761 /* When parsing something like:
12763 int i(float f, double d)
12765 we can tell after seeing the declaration for "f" that we
12766 are not looking at an initialization of a variable "i",
12767 but rather at the declaration of a function "i".
12769 Due to the fact that the parsing of template arguments
12770 (as specified to a template-id) requires backtracking we
12771 cannot use this technique when inside a template argument
12773 if (!parser->in_template_argument_list_p
12774 && !parser->in_type_id_in_expr_p
12775 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12776 /* However, a parameter-declaration of the form
12777 "foat(f)" (which is a valid declaration of a
12778 parameter "f") can also be interpreted as an
12779 expression (the conversion of "f" to "float"). */
12780 && !parenthesized_p)
12781 cp_parser_commit_to_tentative_parse (parser);
12785 cp_parser_error (parser, "expected %<,%> or %<...%>");
12786 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12787 cp_parser_skip_to_closing_parenthesis (parser,
12788 /*recovering=*/true,
12789 /*or_comma=*/false,
12790 /*consume_paren=*/false);
12795 parser->in_unbraced_linkage_specification_p
12796 = saved_in_unbraced_linkage_specification_p;
12801 /* Parse a parameter declaration.
12803 parameter-declaration:
12804 decl-specifier-seq ... [opt] declarator
12805 decl-specifier-seq declarator = assignment-expression
12806 decl-specifier-seq ... [opt] abstract-declarator [opt]
12807 decl-specifier-seq abstract-declarator [opt] = assignment-expression
12809 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
12810 declares a template parameter. (In that case, a non-nested `>'
12811 token encountered during the parsing of the assignment-expression
12812 is not interpreted as a greater-than operator.)
12814 Returns a representation of the parameter, or NULL if an error
12815 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
12816 true iff the declarator is of the form "(p)". */
12818 static cp_parameter_declarator *
12819 cp_parser_parameter_declaration (cp_parser *parser,
12820 bool template_parm_p,
12821 bool *parenthesized_p)
12823 int declares_class_or_enum;
12824 bool greater_than_is_operator_p;
12825 cp_decl_specifier_seq decl_specifiers;
12826 cp_declarator *declarator;
12827 tree default_argument;
12829 const char *saved_message;
12831 /* In a template parameter, `>' is not an operator.
12835 When parsing a default template-argument for a non-type
12836 template-parameter, the first non-nested `>' is taken as the end
12837 of the template parameter-list rather than a greater-than
12839 greater_than_is_operator_p = !template_parm_p;
12841 /* Type definitions may not appear in parameter types. */
12842 saved_message = parser->type_definition_forbidden_message;
12843 parser->type_definition_forbidden_message
12844 = "types may not be defined in parameter types";
12846 /* Parse the declaration-specifiers. */
12847 cp_parser_decl_specifier_seq (parser,
12848 CP_PARSER_FLAGS_NONE,
12850 &declares_class_or_enum);
12851 /* If an error occurred, there's no reason to attempt to parse the
12852 rest of the declaration. */
12853 if (cp_parser_error_occurred (parser))
12855 parser->type_definition_forbidden_message = saved_message;
12859 /* Peek at the next token. */
12860 token = cp_lexer_peek_token (parser->lexer);
12862 /* If the next token is a `)', `,', `=', `>', or `...', then there
12863 is no declarator. However, when variadic templates are enabled,
12864 there may be a declarator following `...'. */
12865 if (token->type == CPP_CLOSE_PAREN
12866 || token->type == CPP_COMMA
12867 || token->type == CPP_EQ
12868 || token->type == CPP_GREATER)
12871 if (parenthesized_p)
12872 *parenthesized_p = false;
12874 /* Otherwise, there should be a declarator. */
12877 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12878 parser->default_arg_ok_p = false;
12880 /* After seeing a decl-specifier-seq, if the next token is not a
12881 "(", there is no possibility that the code is a valid
12882 expression. Therefore, if parsing tentatively, we commit at
12884 if (!parser->in_template_argument_list_p
12885 /* In an expression context, having seen:
12889 we cannot be sure whether we are looking at a
12890 function-type (taking a "char" as a parameter) or a cast
12891 of some object of type "char" to "int". */
12892 && !parser->in_type_id_in_expr_p
12893 && cp_parser_uncommitted_to_tentative_parse_p (parser)
12894 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12895 cp_parser_commit_to_tentative_parse (parser);
12896 /* Parse the declarator. */
12897 declarator = cp_parser_declarator (parser,
12898 CP_PARSER_DECLARATOR_EITHER,
12899 /*ctor_dtor_or_conv_p=*/NULL,
12901 /*member_p=*/false);
12902 parser->default_arg_ok_p = saved_default_arg_ok_p;
12903 /* After the declarator, allow more attributes. */
12904 decl_specifiers.attributes
12905 = chainon (decl_specifiers.attributes,
12906 cp_parser_attributes_opt (parser));
12909 /* If the next token is an ellipsis, and the type of the declarator
12910 contains parameter packs but it is not a TYPE_PACK_EXPANSION, then
12911 we actually have a parameter pack expansion expression. Otherwise,
12912 leave the ellipsis for a C-style variadic function. */
12913 token = cp_lexer_peek_token (parser->lexer);
12914 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12916 tree type = decl_specifiers.type;
12919 type = TREE_TYPE (type);
12921 if (TREE_CODE (type) != TYPE_PACK_EXPANSION
12922 && (!declarator || !declarator->parameter_pack_p)
12923 && uses_parameter_packs (type))
12925 /* Consume the `...'. */
12926 cp_lexer_consume_token (parser->lexer);
12927 maybe_warn_variadic_templates ();
12929 /* Build a pack expansion type */
12931 declarator->parameter_pack_p = true;
12933 decl_specifiers.type = make_pack_expansion (type);
12937 /* The restriction on defining new types applies only to the type
12938 of the parameter, not to the default argument. */
12939 parser->type_definition_forbidden_message = saved_message;
12941 /* If the next token is `=', then process a default argument. */
12942 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12944 bool saved_greater_than_is_operator_p;
12945 /* Consume the `='. */
12946 cp_lexer_consume_token (parser->lexer);
12948 /* If we are defining a class, then the tokens that make up the
12949 default argument must be saved and processed later. */
12950 if (!template_parm_p && at_class_scope_p ()
12951 && TYPE_BEING_DEFINED (current_class_type))
12953 unsigned depth = 0;
12954 cp_token *first_token;
12957 /* Add tokens until we have processed the entire default
12958 argument. We add the range [first_token, token). */
12959 first_token = cp_lexer_peek_token (parser->lexer);
12964 /* Peek at the next token. */
12965 token = cp_lexer_peek_token (parser->lexer);
12966 /* What we do depends on what token we have. */
12967 switch (token->type)
12969 /* In valid code, a default argument must be
12970 immediately followed by a `,' `)', or `...'. */
12972 case CPP_CLOSE_PAREN:
12974 /* If we run into a non-nested `;', `}', or `]',
12975 then the code is invalid -- but the default
12976 argument is certainly over. */
12977 case CPP_SEMICOLON:
12978 case CPP_CLOSE_BRACE:
12979 case CPP_CLOSE_SQUARE:
12982 /* Update DEPTH, if necessary. */
12983 else if (token->type == CPP_CLOSE_PAREN
12984 || token->type == CPP_CLOSE_BRACE
12985 || token->type == CPP_CLOSE_SQUARE)
12989 case CPP_OPEN_PAREN:
12990 case CPP_OPEN_SQUARE:
12991 case CPP_OPEN_BRACE:
12996 /* If we see a non-nested `>', and `>' is not an
12997 operator, then it marks the end of the default
12999 if (!depth && !greater_than_is_operator_p)
13003 /* If we run out of tokens, issue an error message. */
13005 case CPP_PRAGMA_EOL:
13006 error ("file ends in default argument");
13012 /* In these cases, we should look for template-ids.
13013 For example, if the default argument is
13014 `X<int, double>()', we need to do name lookup to
13015 figure out whether or not `X' is a template; if
13016 so, the `,' does not end the default argument.
13018 That is not yet done. */
13025 /* If we've reached the end, stop. */
13029 /* Add the token to the token block. */
13030 token = cp_lexer_consume_token (parser->lexer);
13033 /* Create a DEFAULT_ARG to represented the unparsed default
13035 default_argument = make_node (DEFAULT_ARG);
13036 DEFARG_TOKENS (default_argument)
13037 = cp_token_cache_new (first_token, token);
13038 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13040 /* Outside of a class definition, we can just parse the
13041 assignment-expression. */
13044 bool saved_local_variables_forbidden_p;
13046 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13048 saved_greater_than_is_operator_p
13049 = parser->greater_than_is_operator_p;
13050 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13051 /* Local variable names (and the `this' keyword) may not
13052 appear in a default argument. */
13053 saved_local_variables_forbidden_p
13054 = parser->local_variables_forbidden_p;
13055 parser->local_variables_forbidden_p = true;
13056 /* The default argument expression may cause implicitly
13057 defined member functions to be synthesized, which will
13058 result in garbage collection. We must treat this
13059 situation as if we were within the body of function so as
13060 to avoid collecting live data on the stack. */
13062 /* Parse the assignment-expression. */
13063 if (template_parm_p)
13064 push_deferring_access_checks (dk_no_deferred);
13066 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13067 if (template_parm_p)
13068 pop_deferring_access_checks ();
13069 /* Restore saved state. */
13071 parser->greater_than_is_operator_p
13072 = saved_greater_than_is_operator_p;
13073 parser->local_variables_forbidden_p
13074 = saved_local_variables_forbidden_p;
13076 if (!parser->default_arg_ok_p)
13078 if (!flag_pedantic_errors)
13079 warning (0, "deprecated use of default argument for parameter of non-function");
13082 error ("default arguments are only permitted for function parameters");
13083 default_argument = NULL_TREE;
13088 default_argument = NULL_TREE;
13090 return make_parameter_declarator (&decl_specifiers,
13095 /* Parse a function-body.
13098 compound_statement */
13101 cp_parser_function_body (cp_parser *parser)
13103 cp_parser_compound_statement (parser, NULL, false);
13106 /* Parse a ctor-initializer-opt followed by a function-body. Return
13107 true if a ctor-initializer was present. */
13110 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13113 bool ctor_initializer_p;
13115 /* Begin the function body. */
13116 body = begin_function_body ();
13117 /* Parse the optional ctor-initializer. */
13118 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13119 /* Parse the function-body. */
13120 cp_parser_function_body (parser);
13121 /* Finish the function body. */
13122 finish_function_body (body);
13124 return ctor_initializer_p;
13127 /* Parse an initializer.
13130 = initializer-clause
13131 ( expression-list )
13133 Returns an expression representing the initializer. If no
13134 initializer is present, NULL_TREE is returned.
13136 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13137 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13138 set to FALSE if there is no initializer present. If there is an
13139 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13140 is set to true; otherwise it is set to false. */
13143 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13144 bool* non_constant_p)
13149 /* Peek at the next token. */
13150 token = cp_lexer_peek_token (parser->lexer);
13152 /* Let our caller know whether or not this initializer was
13154 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13155 /* Assume that the initializer is constant. */
13156 *non_constant_p = false;
13158 if (token->type == CPP_EQ)
13160 /* Consume the `='. */
13161 cp_lexer_consume_token (parser->lexer);
13162 /* Parse the initializer-clause. */
13163 init = cp_parser_initializer_clause (parser, non_constant_p);
13165 else if (token->type == CPP_OPEN_PAREN)
13166 init = cp_parser_parenthesized_expression_list (parser, false,
13168 /*allow_expansion_p=*/true,
13172 /* Anything else is an error. */
13173 cp_parser_error (parser, "expected initializer");
13174 init = error_mark_node;
13180 /* Parse an initializer-clause.
13182 initializer-clause:
13183 assignment-expression
13184 { initializer-list , [opt] }
13187 Returns an expression representing the initializer.
13189 If the `assignment-expression' production is used the value
13190 returned is simply a representation for the expression.
13192 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13193 the elements of the initializer-list (or NULL, if the last
13194 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13195 NULL_TREE. There is no way to detect whether or not the optional
13196 trailing `,' was provided. NON_CONSTANT_P is as for
13197 cp_parser_initializer. */
13200 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13204 /* Assume the expression is constant. */
13205 *non_constant_p = false;
13207 /* If it is not a `{', then we are looking at an
13208 assignment-expression. */
13209 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13212 = cp_parser_constant_expression (parser,
13213 /*allow_non_constant_p=*/true,
13215 if (!*non_constant_p)
13216 initializer = fold_non_dependent_expr (initializer);
13220 /* Consume the `{' token. */
13221 cp_lexer_consume_token (parser->lexer);
13222 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13223 initializer = make_node (CONSTRUCTOR);
13224 /* If it's not a `}', then there is a non-trivial initializer. */
13225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13227 /* Parse the initializer list. */
13228 CONSTRUCTOR_ELTS (initializer)
13229 = cp_parser_initializer_list (parser, non_constant_p);
13230 /* A trailing `,' token is allowed. */
13231 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13232 cp_lexer_consume_token (parser->lexer);
13234 /* Now, there should be a trailing `}'. */
13235 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13238 return initializer;
13241 /* Parse an initializer-list.
13244 initializer-clause ... [opt]
13245 initializer-list , initializer-clause ... [opt]
13250 identifier : initializer-clause
13251 initializer-list, identifier : initializer-clause
13253 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13254 for the initializer. If the INDEX of the elt is non-NULL, it is the
13255 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13256 as for cp_parser_initializer. */
13258 static VEC(constructor_elt,gc) *
13259 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13261 VEC(constructor_elt,gc) *v = NULL;
13263 /* Assume all of the expressions are constant. */
13264 *non_constant_p = false;
13266 /* Parse the rest of the list. */
13272 bool clause_non_constant_p;
13274 /* If the next token is an identifier and the following one is a
13275 colon, we are looking at the GNU designated-initializer
13277 if (cp_parser_allow_gnu_extensions_p (parser)
13278 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13279 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13281 /* Warn the user that they are using an extension. */
13283 pedwarn ("ISO C++ does not allow designated initializers");
13284 /* Consume the identifier. */
13285 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13286 /* Consume the `:'. */
13287 cp_lexer_consume_token (parser->lexer);
13290 identifier = NULL_TREE;
13292 /* Parse the initializer. */
13293 initializer = cp_parser_initializer_clause (parser,
13294 &clause_non_constant_p);
13295 /* If any clause is non-constant, so is the entire initializer. */
13296 if (clause_non_constant_p)
13297 *non_constant_p = true;
13299 /* If we have an ellipsis, this is an initializer pack
13301 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13303 /* Consume the `...'. */
13304 cp_lexer_consume_token (parser->lexer);
13306 /* Turn the initializer into an initializer expansion. */
13307 initializer = make_pack_expansion (initializer);
13310 /* Add it to the vector. */
13311 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13313 /* If the next token is not a comma, we have reached the end of
13315 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13318 /* Peek at the next token. */
13319 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13320 /* If the next token is a `}', then we're still done. An
13321 initializer-clause can have a trailing `,' after the
13322 initializer-list and before the closing `}'. */
13323 if (token->type == CPP_CLOSE_BRACE)
13326 /* Consume the `,' token. */
13327 cp_lexer_consume_token (parser->lexer);
13333 /* Classes [gram.class] */
13335 /* Parse a class-name.
13341 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13342 to indicate that names looked up in dependent types should be
13343 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13344 keyword has been used to indicate that the name that appears next
13345 is a template. TAG_TYPE indicates the explicit tag given before
13346 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13347 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13348 is the class being defined in a class-head.
13350 Returns the TYPE_DECL representing the class. */
13353 cp_parser_class_name (cp_parser *parser,
13354 bool typename_keyword_p,
13355 bool template_keyword_p,
13356 enum tag_types tag_type,
13357 bool check_dependency_p,
13359 bool is_declaration)
13366 /* All class-names start with an identifier. */
13367 token = cp_lexer_peek_token (parser->lexer);
13368 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13370 cp_parser_error (parser, "expected class-name");
13371 return error_mark_node;
13374 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13375 to a template-id, so we save it here. */
13376 scope = parser->scope;
13377 if (scope == error_mark_node)
13378 return error_mark_node;
13380 /* Any name names a type if we're following the `typename' keyword
13381 in a qualified name where the enclosing scope is type-dependent. */
13382 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13383 && dependent_type_p (scope));
13384 /* Handle the common case (an identifier, but not a template-id)
13386 if (token->type == CPP_NAME
13387 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13389 cp_token *identifier_token;
13393 /* Look for the identifier. */
13394 identifier_token = cp_lexer_peek_token (parser->lexer);
13395 ambiguous_p = identifier_token->ambiguous_p;
13396 identifier = cp_parser_identifier (parser);
13397 /* If the next token isn't an identifier, we are certainly not
13398 looking at a class-name. */
13399 if (identifier == error_mark_node)
13400 decl = error_mark_node;
13401 /* If we know this is a type-name, there's no need to look it
13403 else if (typename_p)
13407 tree ambiguous_decls;
13408 /* If we already know that this lookup is ambiguous, then
13409 we've already issued an error message; there's no reason
13413 cp_parser_simulate_error (parser);
13414 return error_mark_node;
13416 /* If the next token is a `::', then the name must be a type
13419 [basic.lookup.qual]
13421 During the lookup for a name preceding the :: scope
13422 resolution operator, object, function, and enumerator
13423 names are ignored. */
13424 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13425 tag_type = typename_type;
13426 /* Look up the name. */
13427 decl = cp_parser_lookup_name (parser, identifier,
13429 /*is_template=*/false,
13430 /*is_namespace=*/false,
13431 check_dependency_p,
13433 if (ambiguous_decls)
13435 error ("reference to %qD is ambiguous", identifier);
13436 print_candidates (ambiguous_decls);
13437 if (cp_parser_parsing_tentatively (parser))
13439 identifier_token->ambiguous_p = true;
13440 cp_parser_simulate_error (parser);
13442 return error_mark_node;
13448 /* Try a template-id. */
13449 decl = cp_parser_template_id (parser, template_keyword_p,
13450 check_dependency_p,
13452 if (decl == error_mark_node)
13453 return error_mark_node;
13456 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13458 /* If this is a typename, create a TYPENAME_TYPE. */
13459 if (typename_p && decl != error_mark_node)
13461 decl = make_typename_type (scope, decl, typename_type,
13462 /*complain=*/tf_error);
13463 if (decl != error_mark_node)
13464 decl = TYPE_NAME (decl);
13467 /* Check to see that it is really the name of a class. */
13468 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13469 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13470 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13471 /* Situations like this:
13473 template <typename T> struct A {
13474 typename T::template X<int>::I i;
13477 are problematic. Is `T::template X<int>' a class-name? The
13478 standard does not seem to be definitive, but there is no other
13479 valid interpretation of the following `::'. Therefore, those
13480 names are considered class-names. */
13482 decl = make_typename_type (scope, decl, tag_type, tf_error);
13483 if (decl != error_mark_node)
13484 decl = TYPE_NAME (decl);
13486 else if (TREE_CODE (decl) != TYPE_DECL
13487 || TREE_TYPE (decl) == error_mark_node
13488 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13489 decl = error_mark_node;
13491 if (decl == error_mark_node)
13492 cp_parser_error (parser, "expected class-name");
13497 /* Parse a class-specifier.
13500 class-head { member-specification [opt] }
13502 Returns the TREE_TYPE representing the class. */
13505 cp_parser_class_specifier (cp_parser* parser)
13509 tree attributes = NULL_TREE;
13510 int has_trailing_semicolon;
13511 bool nested_name_specifier_p;
13512 unsigned saved_num_template_parameter_lists;
13513 bool saved_in_function_body;
13514 tree old_scope = NULL_TREE;
13515 tree scope = NULL_TREE;
13518 push_deferring_access_checks (dk_no_deferred);
13520 /* Parse the class-head. */
13521 type = cp_parser_class_head (parser,
13522 &nested_name_specifier_p,
13525 /* If the class-head was a semantic disaster, skip the entire body
13529 cp_parser_skip_to_end_of_block_or_statement (parser);
13530 pop_deferring_access_checks ();
13531 return error_mark_node;
13534 /* Look for the `{'. */
13535 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13537 pop_deferring_access_checks ();
13538 return error_mark_node;
13541 /* Process the base classes. If they're invalid, skip the
13542 entire class body. */
13543 if (!xref_basetypes (type, bases))
13545 cp_parser_skip_to_closing_brace (parser);
13547 /* Consuming the closing brace yields better error messages
13549 cp_lexer_consume_token (parser->lexer);
13550 pop_deferring_access_checks ();
13551 return error_mark_node;
13554 /* Issue an error message if type-definitions are forbidden here. */
13555 cp_parser_check_type_definition (parser);
13556 /* Remember that we are defining one more class. */
13557 ++parser->num_classes_being_defined;
13558 /* Inside the class, surrounding template-parameter-lists do not
13560 saved_num_template_parameter_lists
13561 = parser->num_template_parameter_lists;
13562 parser->num_template_parameter_lists = 0;
13563 /* We are not in a function body. */
13564 saved_in_function_body = parser->in_function_body;
13565 parser->in_function_body = false;
13567 /* Start the class. */
13568 if (nested_name_specifier_p)
13570 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13571 old_scope = push_inner_scope (scope);
13573 type = begin_class_definition (type, attributes);
13575 if (type == error_mark_node)
13576 /* If the type is erroneous, skip the entire body of the class. */
13577 cp_parser_skip_to_closing_brace (parser);
13579 /* Parse the member-specification. */
13580 cp_parser_member_specification_opt (parser);
13582 /* Look for the trailing `}'. */
13583 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13584 /* We get better error messages by noticing a common problem: a
13585 missing trailing `;'. */
13586 token = cp_lexer_peek_token (parser->lexer);
13587 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13588 /* Look for trailing attributes to apply to this class. */
13589 if (cp_parser_allow_gnu_extensions_p (parser))
13590 attributes = cp_parser_attributes_opt (parser);
13591 if (type != error_mark_node)
13592 type = finish_struct (type, attributes);
13593 if (nested_name_specifier_p)
13594 pop_inner_scope (old_scope, scope);
13595 /* If this class is not itself within the scope of another class,
13596 then we need to parse the bodies of all of the queued function
13597 definitions. Note that the queued functions defined in a class
13598 are not always processed immediately following the
13599 class-specifier for that class. Consider:
13602 struct B { void f() { sizeof (A); } };
13605 If `f' were processed before the processing of `A' were
13606 completed, there would be no way to compute the size of `A'.
13607 Note that the nesting we are interested in here is lexical --
13608 not the semantic nesting given by TYPE_CONTEXT. In particular,
13611 struct A { struct B; };
13612 struct A::B { void f() { } };
13614 there is no need to delay the parsing of `A::B::f'. */
13615 if (--parser->num_classes_being_defined == 0)
13619 tree class_type = NULL_TREE;
13620 tree pushed_scope = NULL_TREE;
13622 /* In a first pass, parse default arguments to the functions.
13623 Then, in a second pass, parse the bodies of the functions.
13624 This two-phased approach handles cases like:
13632 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13633 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13634 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13635 TREE_PURPOSE (parser->unparsed_functions_queues)
13636 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13638 fn = TREE_VALUE (queue_entry);
13639 /* If there are default arguments that have not yet been processed,
13640 take care of them now. */
13641 if (class_type != TREE_PURPOSE (queue_entry))
13644 pop_scope (pushed_scope);
13645 class_type = TREE_PURPOSE (queue_entry);
13646 pushed_scope = push_scope (class_type);
13648 /* Make sure that any template parameters are in scope. */
13649 maybe_begin_member_template_processing (fn);
13650 /* Parse the default argument expressions. */
13651 cp_parser_late_parsing_default_args (parser, fn);
13652 /* Remove any template parameters from the symbol table. */
13653 maybe_end_member_template_processing ();
13656 pop_scope (pushed_scope);
13657 /* Now parse the body of the functions. */
13658 for (TREE_VALUE (parser->unparsed_functions_queues)
13659 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13660 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13661 TREE_VALUE (parser->unparsed_functions_queues)
13662 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13664 /* Figure out which function we need to process. */
13665 fn = TREE_VALUE (queue_entry);
13666 /* Parse the function. */
13667 cp_parser_late_parsing_for_member (parser, fn);
13671 /* Put back any saved access checks. */
13672 pop_deferring_access_checks ();
13674 /* Restore saved state. */
13675 parser->in_function_body = saved_in_function_body;
13676 parser->num_template_parameter_lists
13677 = saved_num_template_parameter_lists;
13682 /* Parse a class-head.
13685 class-key identifier [opt] base-clause [opt]
13686 class-key nested-name-specifier identifier base-clause [opt]
13687 class-key nested-name-specifier [opt] template-id
13691 class-key attributes identifier [opt] base-clause [opt]
13692 class-key attributes nested-name-specifier identifier base-clause [opt]
13693 class-key attributes nested-name-specifier [opt] template-id
13696 Upon return BASES is initialized to the list of base classes (or
13697 NULL, if there are none) in the same form returned by
13698 cp_parser_base_clause.
13700 Returns the TYPE of the indicated class. Sets
13701 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13702 involving a nested-name-specifier was used, and FALSE otherwise.
13704 Returns error_mark_node if this is not a class-head.
13706 Returns NULL_TREE if the class-head is syntactically valid, but
13707 semantically invalid in a way that means we should skip the entire
13708 body of the class. */
13711 cp_parser_class_head (cp_parser* parser,
13712 bool* nested_name_specifier_p,
13713 tree *attributes_p,
13716 tree nested_name_specifier;
13717 enum tag_types class_key;
13718 tree id = NULL_TREE;
13719 tree type = NULL_TREE;
13721 bool template_id_p = false;
13722 bool qualified_p = false;
13723 bool invalid_nested_name_p = false;
13724 bool invalid_explicit_specialization_p = false;
13725 tree pushed_scope = NULL_TREE;
13726 unsigned num_templates;
13728 /* Assume no nested-name-specifier will be present. */
13729 *nested_name_specifier_p = false;
13730 /* Assume no template parameter lists will be used in defining the
13734 *bases = NULL_TREE;
13736 /* Look for the class-key. */
13737 class_key = cp_parser_class_key (parser);
13738 if (class_key == none_type)
13739 return error_mark_node;
13741 /* Parse the attributes. */
13742 attributes = cp_parser_attributes_opt (parser);
13744 /* If the next token is `::', that is invalid -- but sometimes
13745 people do try to write:
13749 Handle this gracefully by accepting the extra qualifier, and then
13750 issuing an error about it later if this really is a
13751 class-head. If it turns out just to be an elaborated type
13752 specifier, remain silent. */
13753 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
13754 qualified_p = true;
13756 push_deferring_access_checks (dk_no_check);
13758 /* Determine the name of the class. Begin by looking for an
13759 optional nested-name-specifier. */
13760 nested_name_specifier
13761 = cp_parser_nested_name_specifier_opt (parser,
13762 /*typename_keyword_p=*/false,
13763 /*check_dependency_p=*/false,
13765 /*is_declaration=*/false);
13766 /* If there was a nested-name-specifier, then there *must* be an
13768 if (nested_name_specifier)
13770 /* Although the grammar says `identifier', it really means
13771 `class-name' or `template-name'. You are only allowed to
13772 define a class that has already been declared with this
13775 The proposed resolution for Core Issue 180 says that wherever
13776 you see `class T::X' you should treat `X' as a type-name.
13778 It is OK to define an inaccessible class; for example:
13780 class A { class B; };
13783 We do not know if we will see a class-name, or a
13784 template-name. We look for a class-name first, in case the
13785 class-name is a template-id; if we looked for the
13786 template-name first we would stop after the template-name. */
13787 cp_parser_parse_tentatively (parser);
13788 type = cp_parser_class_name (parser,
13789 /*typename_keyword_p=*/false,
13790 /*template_keyword_p=*/false,
13792 /*check_dependency_p=*/false,
13793 /*class_head_p=*/true,
13794 /*is_declaration=*/false);
13795 /* If that didn't work, ignore the nested-name-specifier. */
13796 if (!cp_parser_parse_definitely (parser))
13798 invalid_nested_name_p = true;
13799 id = cp_parser_identifier (parser);
13800 if (id == error_mark_node)
13803 /* If we could not find a corresponding TYPE, treat this
13804 declaration like an unqualified declaration. */
13805 if (type == error_mark_node)
13806 nested_name_specifier = NULL_TREE;
13807 /* Otherwise, count the number of templates used in TYPE and its
13808 containing scopes. */
13813 for (scope = TREE_TYPE (type);
13814 scope && TREE_CODE (scope) != NAMESPACE_DECL;
13815 scope = (TYPE_P (scope)
13816 ? TYPE_CONTEXT (scope)
13817 : DECL_CONTEXT (scope)))
13819 && CLASS_TYPE_P (scope)
13820 && CLASSTYPE_TEMPLATE_INFO (scope)
13821 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
13822 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
13826 /* Otherwise, the identifier is optional. */
13829 /* We don't know whether what comes next is a template-id,
13830 an identifier, or nothing at all. */
13831 cp_parser_parse_tentatively (parser);
13832 /* Check for a template-id. */
13833 id = cp_parser_template_id (parser,
13834 /*template_keyword_p=*/false,
13835 /*check_dependency_p=*/true,
13836 /*is_declaration=*/true);
13837 /* If that didn't work, it could still be an identifier. */
13838 if (!cp_parser_parse_definitely (parser))
13840 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13841 id = cp_parser_identifier (parser);
13847 template_id_p = true;
13852 pop_deferring_access_checks ();
13855 cp_parser_check_for_invalid_template_id (parser, id);
13857 /* If it's not a `:' or a `{' then we can't really be looking at a
13858 class-head, since a class-head only appears as part of a
13859 class-specifier. We have to detect this situation before calling
13860 xref_tag, since that has irreversible side-effects. */
13861 if (!cp_parser_next_token_starts_class_definition_p (parser))
13863 cp_parser_error (parser, "expected %<{%> or %<:%>");
13864 return error_mark_node;
13867 /* At this point, we're going ahead with the class-specifier, even
13868 if some other problem occurs. */
13869 cp_parser_commit_to_tentative_parse (parser);
13870 /* Issue the error about the overly-qualified name now. */
13872 cp_parser_error (parser,
13873 "global qualification of class name is invalid");
13874 else if (invalid_nested_name_p)
13875 cp_parser_error (parser,
13876 "qualified name does not name a class");
13877 else if (nested_name_specifier)
13881 /* Reject typedef-names in class heads. */
13882 if (!DECL_IMPLICIT_TYPEDEF_P (type))
13884 error ("invalid class name in declaration of %qD", type);
13889 /* Figure out in what scope the declaration is being placed. */
13890 scope = current_scope ();
13891 /* If that scope does not contain the scope in which the
13892 class was originally declared, the program is invalid. */
13893 if (scope && !is_ancestor (scope, nested_name_specifier))
13895 error ("declaration of %qD in %qD which does not enclose %qD",
13896 type, scope, nested_name_specifier);
13902 A declarator-id shall not be qualified exception of the
13903 definition of a ... nested class outside of its class
13904 ... [or] a the definition or explicit instantiation of a
13905 class member of a namespace outside of its namespace. */
13906 if (scope == nested_name_specifier)
13908 pedwarn ("extra qualification ignored");
13909 nested_name_specifier = NULL_TREE;
13913 /* An explicit-specialization must be preceded by "template <>". If
13914 it is not, try to recover gracefully. */
13915 if (at_namespace_scope_p ()
13916 && parser->num_template_parameter_lists == 0
13919 error ("an explicit specialization must be preceded by %<template <>%>");
13920 invalid_explicit_specialization_p = true;
13921 /* Take the same action that would have been taken by
13922 cp_parser_explicit_specialization. */
13923 ++parser->num_template_parameter_lists;
13924 begin_specialization ();
13926 /* There must be no "return" statements between this point and the
13927 end of this function; set "type "to the correct return value and
13928 use "goto done;" to return. */
13929 /* Make sure that the right number of template parameters were
13931 if (!cp_parser_check_template_parameters (parser, num_templates))
13933 /* If something went wrong, there is no point in even trying to
13934 process the class-definition. */
13939 /* Look up the type. */
13942 type = TREE_TYPE (id);
13943 type = maybe_process_partial_specialization (type);
13944 if (nested_name_specifier)
13945 pushed_scope = push_scope (nested_name_specifier);
13947 else if (nested_name_specifier)
13953 template <typename T> struct S { struct T };
13954 template <typename T> struct S<T>::T { };
13956 we will get a TYPENAME_TYPE when processing the definition of
13957 `S::T'. We need to resolve it to the actual type before we
13958 try to define it. */
13959 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
13961 class_type = resolve_typename_type (TREE_TYPE (type),
13962 /*only_current_p=*/false);
13963 if (class_type != error_mark_node)
13964 type = TYPE_NAME (class_type);
13967 cp_parser_error (parser, "could not resolve typename type");
13968 type = error_mark_node;
13972 maybe_process_partial_specialization (TREE_TYPE (type));
13973 class_type = current_class_type;
13974 /* Enter the scope indicated by the nested-name-specifier. */
13975 pushed_scope = push_scope (nested_name_specifier);
13976 /* Get the canonical version of this type. */
13977 type = TYPE_MAIN_DECL (TREE_TYPE (type));
13978 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
13979 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
13981 type = push_template_decl (type);
13982 if (type == error_mark_node)
13989 type = TREE_TYPE (type);
13990 *nested_name_specifier_p = true;
13992 else /* The name is not a nested name. */
13994 /* If the class was unnamed, create a dummy name. */
13996 id = make_anon_name ();
13997 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13998 parser->num_template_parameter_lists);
14001 /* Indicate whether this class was declared as a `class' or as a
14003 if (TREE_CODE (type) == RECORD_TYPE)
14004 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14005 cp_parser_check_class_key (class_key, type);
14007 /* If this type was already complete, and we see another definition,
14008 that's an error. */
14009 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14011 error ("redefinition of %q#T", type);
14012 error ("previous definition of %q+#T", type);
14016 else if (type == error_mark_node)
14019 /* We will have entered the scope containing the class; the names of
14020 base classes should be looked up in that context. For example:
14022 struct A { struct B {}; struct C; };
14023 struct A::C : B {};
14027 /* Get the list of base-classes, if there is one. */
14028 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14029 *bases = cp_parser_base_clause (parser);
14032 /* Leave the scope given by the nested-name-specifier. We will
14033 enter the class scope itself while processing the members. */
14035 pop_scope (pushed_scope);
14037 if (invalid_explicit_specialization_p)
14039 end_specialization ();
14040 --parser->num_template_parameter_lists;
14042 *attributes_p = attributes;
14046 /* Parse a class-key.
14053 Returns the kind of class-key specified, or none_type to indicate
14056 static enum tag_types
14057 cp_parser_class_key (cp_parser* parser)
14060 enum tag_types tag_type;
14062 /* Look for the class-key. */
14063 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14067 /* Check to see if the TOKEN is a class-key. */
14068 tag_type = cp_parser_token_is_class_key (token);
14070 cp_parser_error (parser, "expected class-key");
14074 /* Parse an (optional) member-specification.
14076 member-specification:
14077 member-declaration member-specification [opt]
14078 access-specifier : member-specification [opt] */
14081 cp_parser_member_specification_opt (cp_parser* parser)
14088 /* Peek at the next token. */
14089 token = cp_lexer_peek_token (parser->lexer);
14090 /* If it's a `}', or EOF then we've seen all the members. */
14091 if (token->type == CPP_CLOSE_BRACE
14092 || token->type == CPP_EOF
14093 || token->type == CPP_PRAGMA_EOL)
14096 /* See if this token is a keyword. */
14097 keyword = token->keyword;
14101 case RID_PROTECTED:
14103 /* Consume the access-specifier. */
14104 cp_lexer_consume_token (parser->lexer);
14105 /* Remember which access-specifier is active. */
14106 current_access_specifier = token->u.value;
14107 /* Look for the `:'. */
14108 cp_parser_require (parser, CPP_COLON, "`:'");
14112 /* Accept #pragmas at class scope. */
14113 if (token->type == CPP_PRAGMA)
14115 cp_parser_pragma (parser, pragma_external);
14119 /* Otherwise, the next construction must be a
14120 member-declaration. */
14121 cp_parser_member_declaration (parser);
14126 /* Parse a member-declaration.
14128 member-declaration:
14129 decl-specifier-seq [opt] member-declarator-list [opt] ;
14130 function-definition ; [opt]
14131 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14133 template-declaration
14135 member-declarator-list:
14137 member-declarator-list , member-declarator
14140 declarator pure-specifier [opt]
14141 declarator constant-initializer [opt]
14142 identifier [opt] : constant-expression
14146 member-declaration:
14147 __extension__ member-declaration
14150 declarator attributes [opt] pure-specifier [opt]
14151 declarator attributes [opt] constant-initializer [opt]
14152 identifier [opt] attributes [opt] : constant-expression
14156 member-declaration:
14157 static_assert-declaration */
14160 cp_parser_member_declaration (cp_parser* parser)
14162 cp_decl_specifier_seq decl_specifiers;
14163 tree prefix_attributes;
14165 int declares_class_or_enum;
14168 int saved_pedantic;
14170 /* Check for the `__extension__' keyword. */
14171 if (cp_parser_extension_opt (parser, &saved_pedantic))
14174 cp_parser_member_declaration (parser);
14175 /* Restore the old value of the PEDANTIC flag. */
14176 pedantic = saved_pedantic;
14181 /* Check for a template-declaration. */
14182 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14184 /* An explicit specialization here is an error condition, and we
14185 expect the specialization handler to detect and report this. */
14186 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14187 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14188 cp_parser_explicit_specialization (parser);
14190 cp_parser_template_declaration (parser, /*member_p=*/true);
14195 /* Check for a using-declaration. */
14196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14198 /* Parse the using-declaration. */
14199 cp_parser_using_declaration (parser,
14200 /*access_declaration_p=*/false);
14204 /* Check for @defs. */
14205 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14208 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14209 ivar = ivar_chains;
14213 ivar = TREE_CHAIN (member);
14214 TREE_CHAIN (member) = NULL_TREE;
14215 finish_member_declaration (member);
14220 /* If the next token is `static_assert' we have a static assertion. */
14221 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14223 cp_parser_static_assert (parser, /*member_p=*/true);
14227 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14230 /* Parse the decl-specifier-seq. */
14231 cp_parser_decl_specifier_seq (parser,
14232 CP_PARSER_FLAGS_OPTIONAL,
14234 &declares_class_or_enum);
14235 prefix_attributes = decl_specifiers.attributes;
14236 decl_specifiers.attributes = NULL_TREE;
14237 /* Check for an invalid type-name. */
14238 if (!decl_specifiers.type
14239 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14241 /* If there is no declarator, then the decl-specifier-seq should
14243 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14245 /* If there was no decl-specifier-seq, and the next token is a
14246 `;', then we have something like:
14252 Each member-declaration shall declare at least one member
14253 name of the class. */
14254 if (!decl_specifiers.any_specifiers_p)
14256 cp_token *token = cp_lexer_peek_token (parser->lexer);
14257 if (pedantic && !token->in_system_header)
14258 pedwarn ("%Hextra %<;%>", &token->location);
14264 /* See if this declaration is a friend. */
14265 friend_p = cp_parser_friend_p (&decl_specifiers);
14266 /* If there were decl-specifiers, check to see if there was
14267 a class-declaration. */
14268 type = check_tag_decl (&decl_specifiers);
14269 /* Nested classes have already been added to the class, but
14270 a `friend' needs to be explicitly registered. */
14273 /* If the `friend' keyword was present, the friend must
14274 be introduced with a class-key. */
14275 if (!declares_class_or_enum)
14276 error ("a class-key must be used when declaring a friend");
14279 template <typename T> struct A {
14280 friend struct A<T>::B;
14283 A<T>::B will be represented by a TYPENAME_TYPE, and
14284 therefore not recognized by check_tag_decl. */
14286 && decl_specifiers.type
14287 && TYPE_P (decl_specifiers.type))
14288 type = decl_specifiers.type;
14289 if (!type || !TYPE_P (type))
14290 error ("friend declaration does not name a class or "
14293 make_friend_class (current_class_type, type,
14294 /*complain=*/true);
14296 /* If there is no TYPE, an error message will already have
14298 else if (!type || type == error_mark_node)
14300 /* An anonymous aggregate has to be handled specially; such
14301 a declaration really declares a data member (with a
14302 particular type), as opposed to a nested class. */
14303 else if (ANON_AGGR_TYPE_P (type))
14305 /* Remove constructors and such from TYPE, now that we
14306 know it is an anonymous aggregate. */
14307 fixup_anonymous_aggr (type);
14308 /* And make the corresponding data member. */
14309 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14310 /* Add it to the class. */
14311 finish_member_declaration (decl);
14314 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14319 /* See if these declarations will be friends. */
14320 friend_p = cp_parser_friend_p (&decl_specifiers);
14322 /* Keep going until we hit the `;' at the end of the
14324 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14326 tree attributes = NULL_TREE;
14327 tree first_attribute;
14329 /* Peek at the next token. */
14330 token = cp_lexer_peek_token (parser->lexer);
14332 /* Check for a bitfield declaration. */
14333 if (token->type == CPP_COLON
14334 || (token->type == CPP_NAME
14335 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14341 /* Get the name of the bitfield. Note that we cannot just
14342 check TOKEN here because it may have been invalidated by
14343 the call to cp_lexer_peek_nth_token above. */
14344 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14345 identifier = cp_parser_identifier (parser);
14347 identifier = NULL_TREE;
14349 /* Consume the `:' token. */
14350 cp_lexer_consume_token (parser->lexer);
14351 /* Get the width of the bitfield. */
14353 = cp_parser_constant_expression (parser,
14354 /*allow_non_constant=*/false,
14357 /* Look for attributes that apply to the bitfield. */
14358 attributes = cp_parser_attributes_opt (parser);
14359 /* Remember which attributes are prefix attributes and
14361 first_attribute = attributes;
14362 /* Combine the attributes. */
14363 attributes = chainon (prefix_attributes, attributes);
14365 /* Create the bitfield declaration. */
14366 decl = grokbitfield (identifier
14367 ? make_id_declarator (NULL_TREE,
14373 /* Apply the attributes. */
14374 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14378 cp_declarator *declarator;
14380 tree asm_specification;
14381 int ctor_dtor_or_conv_p;
14383 /* Parse the declarator. */
14385 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14386 &ctor_dtor_or_conv_p,
14387 /*parenthesized_p=*/NULL,
14388 /*member_p=*/true);
14390 /* If something went wrong parsing the declarator, make sure
14391 that we at least consume some tokens. */
14392 if (declarator == cp_error_declarator)
14394 /* Skip to the end of the statement. */
14395 cp_parser_skip_to_end_of_statement (parser);
14396 /* If the next token is not a semicolon, that is
14397 probably because we just skipped over the body of
14398 a function. So, we consume a semicolon if
14399 present, but do not issue an error message if it
14401 if (cp_lexer_next_token_is (parser->lexer,
14403 cp_lexer_consume_token (parser->lexer);
14407 if (declares_class_or_enum & 2)
14408 cp_parser_check_for_definition_in_return_type
14409 (declarator, decl_specifiers.type);
14411 /* Look for an asm-specification. */
14412 asm_specification = cp_parser_asm_specification_opt (parser);
14413 /* Look for attributes that apply to the declaration. */
14414 attributes = cp_parser_attributes_opt (parser);
14415 /* Remember which attributes are prefix attributes and
14417 first_attribute = attributes;
14418 /* Combine the attributes. */
14419 attributes = chainon (prefix_attributes, attributes);
14421 /* If it's an `=', then we have a constant-initializer or a
14422 pure-specifier. It is not correct to parse the
14423 initializer before registering the member declaration
14424 since the member declaration should be in scope while
14425 its initializer is processed. However, the rest of the
14426 front end does not yet provide an interface that allows
14427 us to handle this correctly. */
14428 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14432 A pure-specifier shall be used only in the declaration of
14433 a virtual function.
14435 A member-declarator can contain a constant-initializer
14436 only if it declares a static member of integral or
14439 Therefore, if the DECLARATOR is for a function, we look
14440 for a pure-specifier; otherwise, we look for a
14441 constant-initializer. When we call `grokfield', it will
14442 perform more stringent semantics checks. */
14443 if (function_declarator_p (declarator))
14444 initializer = cp_parser_pure_specifier (parser);
14446 /* Parse the initializer. */
14447 initializer = cp_parser_constant_initializer (parser);
14449 /* Otherwise, there is no initializer. */
14451 initializer = NULL_TREE;
14453 /* See if we are probably looking at a function
14454 definition. We are certainly not looking at a
14455 member-declarator. Calling `grokfield' has
14456 side-effects, so we must not do it unless we are sure
14457 that we are looking at a member-declarator. */
14458 if (cp_parser_token_starts_function_definition_p
14459 (cp_lexer_peek_token (parser->lexer)))
14461 /* The grammar does not allow a pure-specifier to be
14462 used when a member function is defined. (It is
14463 possible that this fact is an oversight in the
14464 standard, since a pure function may be defined
14465 outside of the class-specifier. */
14467 error ("pure-specifier on function-definition");
14468 decl = cp_parser_save_member_function_body (parser,
14472 /* If the member was not a friend, declare it here. */
14474 finish_member_declaration (decl);
14475 /* Peek at the next token. */
14476 token = cp_lexer_peek_token (parser->lexer);
14477 /* If the next token is a semicolon, consume it. */
14478 if (token->type == CPP_SEMICOLON)
14479 cp_lexer_consume_token (parser->lexer);
14483 /* Create the declaration. */
14484 decl = grokfield (declarator, &decl_specifiers,
14485 initializer, /*init_const_expr_p=*/true,
14490 /* Reset PREFIX_ATTRIBUTES. */
14491 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14492 attributes = TREE_CHAIN (attributes);
14494 TREE_CHAIN (attributes) = NULL_TREE;
14496 /* If there is any qualification still in effect, clear it
14497 now; we will be starting fresh with the next declarator. */
14498 parser->scope = NULL_TREE;
14499 parser->qualifying_scope = NULL_TREE;
14500 parser->object_scope = NULL_TREE;
14501 /* If it's a `,', then there are more declarators. */
14502 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14503 cp_lexer_consume_token (parser->lexer);
14504 /* If the next token isn't a `;', then we have a parse error. */
14505 else if (cp_lexer_next_token_is_not (parser->lexer,
14508 cp_parser_error (parser, "expected %<;%>");
14509 /* Skip tokens until we find a `;'. */
14510 cp_parser_skip_to_end_of_statement (parser);
14517 /* Add DECL to the list of members. */
14519 finish_member_declaration (decl);
14521 if (TREE_CODE (decl) == FUNCTION_DECL)
14522 cp_parser_save_default_args (parser, decl);
14527 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14530 /* Parse a pure-specifier.
14535 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14536 Otherwise, ERROR_MARK_NODE is returned. */
14539 cp_parser_pure_specifier (cp_parser* parser)
14543 /* Look for the `=' token. */
14544 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14545 return error_mark_node;
14546 /* Look for the `0' token. */
14547 token = cp_lexer_consume_token (parser->lexer);
14548 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14549 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14551 cp_parser_error (parser,
14552 "invalid pure specifier (only `= 0' is allowed)");
14553 cp_parser_skip_to_end_of_statement (parser);
14554 return error_mark_node;
14556 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14558 error ("templates may not be %<virtual%>");
14559 return error_mark_node;
14562 return integer_zero_node;
14565 /* Parse a constant-initializer.
14567 constant-initializer:
14568 = constant-expression
14570 Returns a representation of the constant-expression. */
14573 cp_parser_constant_initializer (cp_parser* parser)
14575 /* Look for the `=' token. */
14576 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14577 return error_mark_node;
14579 /* It is invalid to write:
14581 struct S { static const int i = { 7 }; };
14584 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14586 cp_parser_error (parser,
14587 "a brace-enclosed initializer is not allowed here");
14588 /* Consume the opening brace. */
14589 cp_lexer_consume_token (parser->lexer);
14590 /* Skip the initializer. */
14591 cp_parser_skip_to_closing_brace (parser);
14592 /* Look for the trailing `}'. */
14593 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14595 return error_mark_node;
14598 return cp_parser_constant_expression (parser,
14599 /*allow_non_constant=*/false,
14603 /* Derived classes [gram.class.derived] */
14605 /* Parse a base-clause.
14608 : base-specifier-list
14610 base-specifier-list:
14611 base-specifier ... [opt]
14612 base-specifier-list , base-specifier ... [opt]
14614 Returns a TREE_LIST representing the base-classes, in the order in
14615 which they were declared. The representation of each node is as
14616 described by cp_parser_base_specifier.
14618 In the case that no bases are specified, this function will return
14619 NULL_TREE, not ERROR_MARK_NODE. */
14622 cp_parser_base_clause (cp_parser* parser)
14624 tree bases = NULL_TREE;
14626 /* Look for the `:' that begins the list. */
14627 cp_parser_require (parser, CPP_COLON, "`:'");
14629 /* Scan the base-specifier-list. */
14634 bool pack_expansion_p = false;
14636 /* Look for the base-specifier. */
14637 base = cp_parser_base_specifier (parser);
14638 /* Look for the (optional) ellipsis. */
14639 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14641 /* Consume the `...'. */
14642 cp_lexer_consume_token (parser->lexer);
14644 pack_expansion_p = true;
14647 /* Add BASE to the front of the list. */
14648 if (base != error_mark_node)
14650 if (pack_expansion_p)
14651 /* Make this a pack expansion type. */
14652 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14654 check_for_bare_parameter_packs (TREE_VALUE (base));
14656 TREE_CHAIN (base) = bases;
14659 /* Peek at the next token. */
14660 token = cp_lexer_peek_token (parser->lexer);
14661 /* If it's not a comma, then the list is complete. */
14662 if (token->type != CPP_COMMA)
14664 /* Consume the `,'. */
14665 cp_lexer_consume_token (parser->lexer);
14668 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14669 base class had a qualified name. However, the next name that
14670 appears is certainly not qualified. */
14671 parser->scope = NULL_TREE;
14672 parser->qualifying_scope = NULL_TREE;
14673 parser->object_scope = NULL_TREE;
14675 return nreverse (bases);
14678 /* Parse a base-specifier.
14681 :: [opt] nested-name-specifier [opt] class-name
14682 virtual access-specifier [opt] :: [opt] nested-name-specifier
14684 access-specifier virtual [opt] :: [opt] nested-name-specifier
14687 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14688 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14689 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14690 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14693 cp_parser_base_specifier (cp_parser* parser)
14697 bool virtual_p = false;
14698 bool duplicate_virtual_error_issued_p = false;
14699 bool duplicate_access_error_issued_p = false;
14700 bool class_scope_p, template_p;
14701 tree access = access_default_node;
14704 /* Process the optional `virtual' and `access-specifier'. */
14707 /* Peek at the next token. */
14708 token = cp_lexer_peek_token (parser->lexer);
14709 /* Process `virtual'. */
14710 switch (token->keyword)
14713 /* If `virtual' appears more than once, issue an error. */
14714 if (virtual_p && !duplicate_virtual_error_issued_p)
14716 cp_parser_error (parser,
14717 "%<virtual%> specified more than once in base-specified");
14718 duplicate_virtual_error_issued_p = true;
14723 /* Consume the `virtual' token. */
14724 cp_lexer_consume_token (parser->lexer);
14729 case RID_PROTECTED:
14731 /* If more than one access specifier appears, issue an
14733 if (access != access_default_node
14734 && !duplicate_access_error_issued_p)
14736 cp_parser_error (parser,
14737 "more than one access specifier in base-specified");
14738 duplicate_access_error_issued_p = true;
14741 access = ridpointers[(int) token->keyword];
14743 /* Consume the access-specifier. */
14744 cp_lexer_consume_token (parser->lexer);
14753 /* It is not uncommon to see programs mechanically, erroneously, use
14754 the 'typename' keyword to denote (dependent) qualified types
14755 as base classes. */
14756 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
14758 if (!processing_template_decl)
14759 error ("keyword %<typename%> not allowed outside of templates");
14761 error ("keyword %<typename%> not allowed in this context "
14762 "(the base class is implicitly a type)");
14763 cp_lexer_consume_token (parser->lexer);
14766 /* Look for the optional `::' operator. */
14767 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14768 /* Look for the nested-name-specifier. The simplest way to
14773 The keyword `typename' is not permitted in a base-specifier or
14774 mem-initializer; in these contexts a qualified name that
14775 depends on a template-parameter is implicitly assumed to be a
14778 is to pretend that we have seen the `typename' keyword at this
14780 cp_parser_nested_name_specifier_opt (parser,
14781 /*typename_keyword_p=*/true,
14782 /*check_dependency_p=*/true,
14784 /*is_declaration=*/true);
14785 /* If the base class is given by a qualified name, assume that names
14786 we see are type names or templates, as appropriate. */
14787 class_scope_p = (parser->scope && TYPE_P (parser->scope));
14788 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
14790 /* Finally, look for the class-name. */
14791 type = cp_parser_class_name (parser,
14795 /*check_dependency_p=*/true,
14796 /*class_head_p=*/false,
14797 /*is_declaration=*/true);
14799 if (type == error_mark_node)
14800 return error_mark_node;
14802 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
14805 /* Exception handling [gram.exception] */
14807 /* Parse an (optional) exception-specification.
14809 exception-specification:
14810 throw ( type-id-list [opt] )
14812 Returns a TREE_LIST representing the exception-specification. The
14813 TREE_VALUE of each node is a type. */
14816 cp_parser_exception_specification_opt (cp_parser* parser)
14821 /* Peek at the next token. */
14822 token = cp_lexer_peek_token (parser->lexer);
14823 /* If it's not `throw', then there's no exception-specification. */
14824 if (!cp_parser_is_keyword (token, RID_THROW))
14827 /* Consume the `throw'. */
14828 cp_lexer_consume_token (parser->lexer);
14830 /* Look for the `('. */
14831 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14833 /* Peek at the next token. */
14834 token = cp_lexer_peek_token (parser->lexer);
14835 /* If it's not a `)', then there is a type-id-list. */
14836 if (token->type != CPP_CLOSE_PAREN)
14838 const char *saved_message;
14840 /* Types may not be defined in an exception-specification. */
14841 saved_message = parser->type_definition_forbidden_message;
14842 parser->type_definition_forbidden_message
14843 = "types may not be defined in an exception-specification";
14844 /* Parse the type-id-list. */
14845 type_id_list = cp_parser_type_id_list (parser);
14846 /* Restore the saved message. */
14847 parser->type_definition_forbidden_message = saved_message;
14850 type_id_list = empty_except_spec;
14852 /* Look for the `)'. */
14853 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14855 return type_id_list;
14858 /* Parse an (optional) type-id-list.
14862 type-id-list , type-id ... [opt]
14864 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
14865 in the order that the types were presented. */
14868 cp_parser_type_id_list (cp_parser* parser)
14870 tree types = NULL_TREE;
14877 /* Get the next type-id. */
14878 type = cp_parser_type_id (parser);
14879 /* Parse the optional ellipsis. */
14880 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14882 /* Consume the `...'. */
14883 cp_lexer_consume_token (parser->lexer);
14885 /* Turn the type into a pack expansion expression. */
14886 type = make_pack_expansion (type);
14888 /* Add it to the list. */
14889 types = add_exception_specifier (types, type, /*complain=*/1);
14890 /* Peek at the next token. */
14891 token = cp_lexer_peek_token (parser->lexer);
14892 /* If it is not a `,', we are done. */
14893 if (token->type != CPP_COMMA)
14895 /* Consume the `,'. */
14896 cp_lexer_consume_token (parser->lexer);
14899 return nreverse (types);
14902 /* Parse a try-block.
14905 try compound-statement handler-seq */
14908 cp_parser_try_block (cp_parser* parser)
14912 cp_parser_require_keyword (parser, RID_TRY, "`try'");
14913 try_block = begin_try_block ();
14914 cp_parser_compound_statement (parser, NULL, true);
14915 finish_try_block (try_block);
14916 cp_parser_handler_seq (parser);
14917 finish_handler_sequence (try_block);
14922 /* Parse a function-try-block.
14924 function-try-block:
14925 try ctor-initializer [opt] function-body handler-seq */
14928 cp_parser_function_try_block (cp_parser* parser)
14930 tree compound_stmt;
14932 bool ctor_initializer_p;
14934 /* Look for the `try' keyword. */
14935 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
14937 /* Let the rest of the front end know where we are. */
14938 try_block = begin_function_try_block (&compound_stmt);
14939 /* Parse the function-body. */
14941 = cp_parser_ctor_initializer_opt_and_function_body (parser);
14942 /* We're done with the `try' part. */
14943 finish_function_try_block (try_block);
14944 /* Parse the handlers. */
14945 cp_parser_handler_seq (parser);
14946 /* We're done with the handlers. */
14947 finish_function_handler_sequence (try_block, compound_stmt);
14949 return ctor_initializer_p;
14952 /* Parse a handler-seq.
14955 handler handler-seq [opt] */
14958 cp_parser_handler_seq (cp_parser* parser)
14964 /* Parse the handler. */
14965 cp_parser_handler (parser);
14966 /* Peek at the next token. */
14967 token = cp_lexer_peek_token (parser->lexer);
14968 /* If it's not `catch' then there are no more handlers. */
14969 if (!cp_parser_is_keyword (token, RID_CATCH))
14974 /* Parse a handler.
14977 catch ( exception-declaration ) compound-statement */
14980 cp_parser_handler (cp_parser* parser)
14985 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
14986 handler = begin_handler ();
14987 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14988 declaration = cp_parser_exception_declaration (parser);
14989 finish_handler_parms (declaration, handler);
14990 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14991 cp_parser_compound_statement (parser, NULL, false);
14992 finish_handler (handler);
14995 /* Parse an exception-declaration.
14997 exception-declaration:
14998 type-specifier-seq declarator
14999 type-specifier-seq abstract-declarator
15003 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15004 ellipsis variant is used. */
15007 cp_parser_exception_declaration (cp_parser* parser)
15009 cp_decl_specifier_seq type_specifiers;
15010 cp_declarator *declarator;
15011 const char *saved_message;
15013 /* If it's an ellipsis, it's easy to handle. */
15014 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15016 /* Consume the `...' token. */
15017 cp_lexer_consume_token (parser->lexer);
15021 /* Types may not be defined in exception-declarations. */
15022 saved_message = parser->type_definition_forbidden_message;
15023 parser->type_definition_forbidden_message
15024 = "types may not be defined in exception-declarations";
15026 /* Parse the type-specifier-seq. */
15027 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15029 /* If it's a `)', then there is no declarator. */
15030 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15033 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15034 /*ctor_dtor_or_conv_p=*/NULL,
15035 /*parenthesized_p=*/NULL,
15036 /*member_p=*/false);
15038 /* Restore the saved message. */
15039 parser->type_definition_forbidden_message = saved_message;
15041 if (!type_specifiers.any_specifiers_p)
15042 return error_mark_node;
15044 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15047 /* Parse a throw-expression.
15050 throw assignment-expression [opt]
15052 Returns a THROW_EXPR representing the throw-expression. */
15055 cp_parser_throw_expression (cp_parser* parser)
15060 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15061 token = cp_lexer_peek_token (parser->lexer);
15062 /* Figure out whether or not there is an assignment-expression
15063 following the "throw" keyword. */
15064 if (token->type == CPP_COMMA
15065 || token->type == CPP_SEMICOLON
15066 || token->type == CPP_CLOSE_PAREN
15067 || token->type == CPP_CLOSE_SQUARE
15068 || token->type == CPP_CLOSE_BRACE
15069 || token->type == CPP_COLON)
15070 expression = NULL_TREE;
15072 expression = cp_parser_assignment_expression (parser,
15075 return build_throw (expression);
15078 /* GNU Extensions */
15080 /* Parse an (optional) asm-specification.
15083 asm ( string-literal )
15085 If the asm-specification is present, returns a STRING_CST
15086 corresponding to the string-literal. Otherwise, returns
15090 cp_parser_asm_specification_opt (cp_parser* parser)
15093 tree asm_specification;
15095 /* Peek at the next token. */
15096 token = cp_lexer_peek_token (parser->lexer);
15097 /* If the next token isn't the `asm' keyword, then there's no
15098 asm-specification. */
15099 if (!cp_parser_is_keyword (token, RID_ASM))
15102 /* Consume the `asm' token. */
15103 cp_lexer_consume_token (parser->lexer);
15104 /* Look for the `('. */
15105 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15107 /* Look for the string-literal. */
15108 asm_specification = cp_parser_string_literal (parser, false, false);
15110 /* Look for the `)'. */
15111 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15113 return asm_specification;
15116 /* Parse an asm-operand-list.
15120 asm-operand-list , asm-operand
15123 string-literal ( expression )
15124 [ string-literal ] string-literal ( expression )
15126 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15127 each node is the expression. The TREE_PURPOSE is itself a
15128 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15129 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15130 is a STRING_CST for the string literal before the parenthesis. */
15133 cp_parser_asm_operand_list (cp_parser* parser)
15135 tree asm_operands = NULL_TREE;
15139 tree string_literal;
15143 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15145 /* Consume the `[' token. */
15146 cp_lexer_consume_token (parser->lexer);
15147 /* Read the operand name. */
15148 name = cp_parser_identifier (parser);
15149 if (name != error_mark_node)
15150 name = build_string (IDENTIFIER_LENGTH (name),
15151 IDENTIFIER_POINTER (name));
15152 /* Look for the closing `]'. */
15153 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15157 /* Look for the string-literal. */
15158 string_literal = cp_parser_string_literal (parser, false, false);
15160 /* Look for the `('. */
15161 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15162 /* Parse the expression. */
15163 expression = cp_parser_expression (parser, /*cast_p=*/false);
15164 /* Look for the `)'. */
15165 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15167 /* Add this operand to the list. */
15168 asm_operands = tree_cons (build_tree_list (name, string_literal),
15171 /* If the next token is not a `,', there are no more
15173 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15175 /* Consume the `,'. */
15176 cp_lexer_consume_token (parser->lexer);
15179 return nreverse (asm_operands);
15182 /* Parse an asm-clobber-list.
15186 asm-clobber-list , string-literal
15188 Returns a TREE_LIST, indicating the clobbers in the order that they
15189 appeared. The TREE_VALUE of each node is a STRING_CST. */
15192 cp_parser_asm_clobber_list (cp_parser* parser)
15194 tree clobbers = NULL_TREE;
15198 tree string_literal;
15200 /* Look for the string literal. */
15201 string_literal = cp_parser_string_literal (parser, false, false);
15202 /* Add it to the list. */
15203 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15204 /* If the next token is not a `,', then the list is
15206 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15208 /* Consume the `,' token. */
15209 cp_lexer_consume_token (parser->lexer);
15215 /* Parse an (optional) series of attributes.
15218 attributes attribute
15221 __attribute__ (( attribute-list [opt] ))
15223 The return value is as for cp_parser_attribute_list. */
15226 cp_parser_attributes_opt (cp_parser* parser)
15228 tree attributes = NULL_TREE;
15233 tree attribute_list;
15235 /* Peek at the next token. */
15236 token = cp_lexer_peek_token (parser->lexer);
15237 /* If it's not `__attribute__', then we're done. */
15238 if (token->keyword != RID_ATTRIBUTE)
15241 /* Consume the `__attribute__' keyword. */
15242 cp_lexer_consume_token (parser->lexer);
15243 /* Look for the two `(' tokens. */
15244 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15245 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15247 /* Peek at the next token. */
15248 token = cp_lexer_peek_token (parser->lexer);
15249 if (token->type != CPP_CLOSE_PAREN)
15250 /* Parse the attribute-list. */
15251 attribute_list = cp_parser_attribute_list (parser);
15253 /* If the next token is a `)', then there is no attribute
15255 attribute_list = NULL;
15257 /* Look for the two `)' tokens. */
15258 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15259 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15261 /* Add these new attributes to the list. */
15262 attributes = chainon (attributes, attribute_list);
15268 /* Parse an attribute-list.
15272 attribute-list , attribute
15276 identifier ( identifier )
15277 identifier ( identifier , expression-list )
15278 identifier ( expression-list )
15280 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15281 to an attribute. The TREE_PURPOSE of each node is the identifier
15282 indicating which attribute is in use. The TREE_VALUE represents
15283 the arguments, if any. */
15286 cp_parser_attribute_list (cp_parser* parser)
15288 tree attribute_list = NULL_TREE;
15289 bool save_translate_strings_p = parser->translate_strings_p;
15291 parser->translate_strings_p = false;
15298 /* Look for the identifier. We also allow keywords here; for
15299 example `__attribute__ ((const))' is legal. */
15300 token = cp_lexer_peek_token (parser->lexer);
15301 if (token->type == CPP_NAME
15302 || token->type == CPP_KEYWORD)
15304 tree arguments = NULL_TREE;
15306 /* Consume the token. */
15307 token = cp_lexer_consume_token (parser->lexer);
15309 /* Save away the identifier that indicates which attribute
15311 identifier = token->u.value;
15312 attribute = build_tree_list (identifier, NULL_TREE);
15314 /* Peek at the next token. */
15315 token = cp_lexer_peek_token (parser->lexer);
15316 /* If it's an `(', then parse the attribute arguments. */
15317 if (token->type == CPP_OPEN_PAREN)
15319 arguments = cp_parser_parenthesized_expression_list
15320 (parser, true, /*cast_p=*/false,
15321 /*allow_expansion_p=*/false,
15322 /*non_constant_p=*/NULL);
15323 /* Save the arguments away. */
15324 TREE_VALUE (attribute) = arguments;
15327 if (arguments != error_mark_node)
15329 /* Add this attribute to the list. */
15330 TREE_CHAIN (attribute) = attribute_list;
15331 attribute_list = attribute;
15334 token = cp_lexer_peek_token (parser->lexer);
15336 /* Now, look for more attributes. If the next token isn't a
15337 `,', we're done. */
15338 if (token->type != CPP_COMMA)
15341 /* Consume the comma and keep going. */
15342 cp_lexer_consume_token (parser->lexer);
15344 parser->translate_strings_p = save_translate_strings_p;
15346 /* We built up the list in reverse order. */
15347 return nreverse (attribute_list);
15350 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15351 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15352 current value of the PEDANTIC flag, regardless of whether or not
15353 the `__extension__' keyword is present. The caller is responsible
15354 for restoring the value of the PEDANTIC flag. */
15357 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15359 /* Save the old value of the PEDANTIC flag. */
15360 *saved_pedantic = pedantic;
15362 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15364 /* Consume the `__extension__' token. */
15365 cp_lexer_consume_token (parser->lexer);
15366 /* We're not being pedantic while the `__extension__' keyword is
15376 /* Parse a label declaration.
15379 __label__ label-declarator-seq ;
15381 label-declarator-seq:
15382 identifier , label-declarator-seq
15386 cp_parser_label_declaration (cp_parser* parser)
15388 /* Look for the `__label__' keyword. */
15389 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15395 /* Look for an identifier. */
15396 identifier = cp_parser_identifier (parser);
15397 /* If we failed, stop. */
15398 if (identifier == error_mark_node)
15400 /* Declare it as a label. */
15401 finish_label_decl (identifier);
15402 /* If the next token is a `;', stop. */
15403 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15405 /* Look for the `,' separating the label declarations. */
15406 cp_parser_require (parser, CPP_COMMA, "`,'");
15409 /* Look for the final `;'. */
15410 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15413 /* Support Functions */
15415 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15416 NAME should have one of the representations used for an
15417 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15418 is returned. If PARSER->SCOPE is a dependent type, then a
15419 SCOPE_REF is returned.
15421 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15422 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15423 was formed. Abstractly, such entities should not be passed to this
15424 function, because they do not need to be looked up, but it is
15425 simpler to check for this special case here, rather than at the
15428 In cases not explicitly covered above, this function returns a
15429 DECL, OVERLOAD, or baselink representing the result of the lookup.
15430 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15433 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15434 (e.g., "struct") that was used. In that case bindings that do not
15435 refer to types are ignored.
15437 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15440 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15443 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15446 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15447 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15448 NULL_TREE otherwise. */
15451 cp_parser_lookup_name (cp_parser *parser, tree name,
15452 enum tag_types tag_type,
15455 bool check_dependency,
15456 tree *ambiguous_decls)
15460 tree object_type = parser->context->object_type;
15462 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15463 flags |= LOOKUP_COMPLAIN;
15465 /* Assume that the lookup will be unambiguous. */
15466 if (ambiguous_decls)
15467 *ambiguous_decls = NULL_TREE;
15469 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15470 no longer valid. Note that if we are parsing tentatively, and
15471 the parse fails, OBJECT_TYPE will be automatically restored. */
15472 parser->context->object_type = NULL_TREE;
15474 if (name == error_mark_node)
15475 return error_mark_node;
15477 /* A template-id has already been resolved; there is no lookup to
15479 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15481 if (BASELINK_P (name))
15483 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15484 == TEMPLATE_ID_EXPR);
15488 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15489 it should already have been checked to make sure that the name
15490 used matches the type being destroyed. */
15491 if (TREE_CODE (name) == BIT_NOT_EXPR)
15495 /* Figure out to which type this destructor applies. */
15497 type = parser->scope;
15498 else if (object_type)
15499 type = object_type;
15501 type = current_class_type;
15502 /* If that's not a class type, there is no destructor. */
15503 if (!type || !CLASS_TYPE_P (type))
15504 return error_mark_node;
15505 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15506 lazily_declare_fn (sfk_destructor, type);
15507 if (!CLASSTYPE_DESTRUCTORS (type))
15508 return error_mark_node;
15509 /* If it was a class type, return the destructor. */
15510 return CLASSTYPE_DESTRUCTORS (type);
15513 /* By this point, the NAME should be an ordinary identifier. If
15514 the id-expression was a qualified name, the qualifying scope is
15515 stored in PARSER->SCOPE at this point. */
15516 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15518 /* Perform the lookup. */
15523 if (parser->scope == error_mark_node)
15524 return error_mark_node;
15526 /* If the SCOPE is dependent, the lookup must be deferred until
15527 the template is instantiated -- unless we are explicitly
15528 looking up names in uninstantiated templates. Even then, we
15529 cannot look up the name if the scope is not a class type; it
15530 might, for example, be a template type parameter. */
15531 dependent_p = (TYPE_P (parser->scope)
15532 && !(parser->in_declarator_p
15533 && currently_open_class (parser->scope))
15534 && dependent_type_p (parser->scope));
15535 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15542 /* The resolution to Core Issue 180 says that `struct
15543 A::B' should be considered a type-name, even if `A'
15545 type = make_typename_type (parser->scope, name, tag_type,
15546 /*complain=*/tf_error);
15547 decl = TYPE_NAME (type);
15549 else if (is_template
15550 && (cp_parser_next_token_ends_template_argument_p (parser)
15551 || cp_lexer_next_token_is (parser->lexer,
15553 decl = make_unbound_class_template (parser->scope,
15555 /*complain=*/tf_error);
15557 decl = build_qualified_name (/*type=*/NULL_TREE,
15558 parser->scope, name,
15563 tree pushed_scope = NULL_TREE;
15565 /* If PARSER->SCOPE is a dependent type, then it must be a
15566 class type, and we must not be checking dependencies;
15567 otherwise, we would have processed this lookup above. So
15568 that PARSER->SCOPE is not considered a dependent base by
15569 lookup_member, we must enter the scope here. */
15571 pushed_scope = push_scope (parser->scope);
15572 /* If the PARSER->SCOPE is a template specialization, it
15573 may be instantiated during name lookup. In that case,
15574 errors may be issued. Even if we rollback the current
15575 tentative parse, those errors are valid. */
15576 decl = lookup_qualified_name (parser->scope, name,
15577 tag_type != none_type,
15578 /*complain=*/true);
15580 pop_scope (pushed_scope);
15582 parser->qualifying_scope = parser->scope;
15583 parser->object_scope = NULL_TREE;
15585 else if (object_type)
15587 tree object_decl = NULL_TREE;
15588 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15589 OBJECT_TYPE is not a class. */
15590 if (CLASS_TYPE_P (object_type))
15591 /* If the OBJECT_TYPE is a template specialization, it may
15592 be instantiated during name lookup. In that case, errors
15593 may be issued. Even if we rollback the current tentative
15594 parse, those errors are valid. */
15595 object_decl = lookup_member (object_type,
15598 tag_type != none_type);
15599 /* Look it up in the enclosing context, too. */
15600 decl = lookup_name_real (name, tag_type != none_type,
15602 /*block_p=*/true, is_namespace, flags);
15603 parser->object_scope = object_type;
15604 parser->qualifying_scope = NULL_TREE;
15606 decl = object_decl;
15610 decl = lookup_name_real (name, tag_type != none_type,
15612 /*block_p=*/true, is_namespace, flags);
15613 parser->qualifying_scope = NULL_TREE;
15614 parser->object_scope = NULL_TREE;
15617 /* If the lookup failed, let our caller know. */
15618 if (!decl || decl == error_mark_node)
15619 return error_mark_node;
15621 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15622 if (TREE_CODE (decl) == TREE_LIST)
15624 if (ambiguous_decls)
15625 *ambiguous_decls = decl;
15626 /* The error message we have to print is too complicated for
15627 cp_parser_error, so we incorporate its actions directly. */
15628 if (!cp_parser_simulate_error (parser))
15630 error ("reference to %qD is ambiguous", name);
15631 print_candidates (decl);
15633 return error_mark_node;
15636 gcc_assert (DECL_P (decl)
15637 || TREE_CODE (decl) == OVERLOAD
15638 || TREE_CODE (decl) == SCOPE_REF
15639 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15640 || BASELINK_P (decl));
15642 /* If we have resolved the name of a member declaration, check to
15643 see if the declaration is accessible. When the name resolves to
15644 set of overloaded functions, accessibility is checked when
15645 overload resolution is done.
15647 During an explicit instantiation, access is not checked at all,
15648 as per [temp.explicit]. */
15650 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15655 /* Like cp_parser_lookup_name, but for use in the typical case where
15656 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15657 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15660 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15662 return cp_parser_lookup_name (parser, name,
15664 /*is_template=*/false,
15665 /*is_namespace=*/false,
15666 /*check_dependency=*/true,
15667 /*ambiguous_decls=*/NULL);
15670 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15671 the current context, return the TYPE_DECL. If TAG_NAME_P is
15672 true, the DECL indicates the class being defined in a class-head,
15673 or declared in an elaborated-type-specifier.
15675 Otherwise, return DECL. */
15678 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15680 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15681 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15684 template <typename T> struct B;
15687 template <typename T> struct A::B {};
15689 Similarly, in an elaborated-type-specifier:
15691 namespace N { struct X{}; }
15694 template <typename T> friend struct N::X;
15697 However, if the DECL refers to a class type, and we are in
15698 the scope of the class, then the name lookup automatically
15699 finds the TYPE_DECL created by build_self_reference rather
15700 than a TEMPLATE_DECL. For example, in:
15702 template <class T> struct S {
15706 there is no need to handle such case. */
15708 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
15709 return DECL_TEMPLATE_RESULT (decl);
15714 /* If too many, or too few, template-parameter lists apply to the
15715 declarator, issue an error message. Returns TRUE if all went well,
15716 and FALSE otherwise. */
15719 cp_parser_check_declarator_template_parameters (cp_parser* parser,
15720 cp_declarator *declarator)
15722 unsigned num_templates;
15724 /* We haven't seen any classes that involve template parameters yet. */
15727 switch (declarator->kind)
15730 if (declarator->u.id.qualifying_scope)
15735 scope = declarator->u.id.qualifying_scope;
15736 member = declarator->u.id.unqualified_name;
15738 while (scope && CLASS_TYPE_P (scope))
15740 /* You're supposed to have one `template <...>'
15741 for every template class, but you don't need one
15742 for a full specialization. For example:
15744 template <class T> struct S{};
15745 template <> struct S<int> { void f(); };
15746 void S<int>::f () {}
15748 is correct; there shouldn't be a `template <>' for
15749 the definition of `S<int>::f'. */
15750 if (!CLASSTYPE_TEMPLATE_INFO (scope))
15751 /* If SCOPE does not have template information of any
15752 kind, then it is not a template, nor is it nested
15753 within a template. */
15755 if (explicit_class_specialization_p (scope))
15757 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
15760 scope = TYPE_CONTEXT (scope);
15763 else if (TREE_CODE (declarator->u.id.unqualified_name)
15764 == TEMPLATE_ID_EXPR)
15765 /* If the DECLARATOR has the form `X<y>' then it uses one
15766 additional level of template parameters. */
15769 return cp_parser_check_template_parameters (parser,
15775 case cdk_reference:
15777 return (cp_parser_check_declarator_template_parameters
15778 (parser, declarator->declarator));
15784 gcc_unreachable ();
15789 /* NUM_TEMPLATES were used in the current declaration. If that is
15790 invalid, return FALSE and issue an error messages. Otherwise,
15794 cp_parser_check_template_parameters (cp_parser* parser,
15795 unsigned num_templates)
15797 /* If there are more template classes than parameter lists, we have
15800 template <class T> void S<T>::R<T>::f (); */
15801 if (parser->num_template_parameter_lists < num_templates)
15803 error ("too few template-parameter-lists");
15806 /* If there are the same number of template classes and parameter
15807 lists, that's OK. */
15808 if (parser->num_template_parameter_lists == num_templates)
15810 /* If there are more, but only one more, then we are referring to a
15811 member template. That's OK too. */
15812 if (parser->num_template_parameter_lists == num_templates + 1)
15814 /* Otherwise, there are too many template parameter lists. We have
15817 template <class T> template <class U> void S::f(); */
15818 error ("too many template-parameter-lists");
15822 /* Parse an optional `::' token indicating that the following name is
15823 from the global namespace. If so, PARSER->SCOPE is set to the
15824 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
15825 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
15826 Returns the new value of PARSER->SCOPE, if the `::' token is
15827 present, and NULL_TREE otherwise. */
15830 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
15834 /* Peek at the next token. */
15835 token = cp_lexer_peek_token (parser->lexer);
15836 /* If we're looking at a `::' token then we're starting from the
15837 global namespace, not our current location. */
15838 if (token->type == CPP_SCOPE)
15840 /* Consume the `::' token. */
15841 cp_lexer_consume_token (parser->lexer);
15842 /* Set the SCOPE so that we know where to start the lookup. */
15843 parser->scope = global_namespace;
15844 parser->qualifying_scope = global_namespace;
15845 parser->object_scope = NULL_TREE;
15847 return parser->scope;
15849 else if (!current_scope_valid_p)
15851 parser->scope = NULL_TREE;
15852 parser->qualifying_scope = NULL_TREE;
15853 parser->object_scope = NULL_TREE;
15859 /* Returns TRUE if the upcoming token sequence is the start of a
15860 constructor declarator. If FRIEND_P is true, the declarator is
15861 preceded by the `friend' specifier. */
15864 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
15866 bool constructor_p;
15867 tree type_decl = NULL_TREE;
15868 bool nested_name_p;
15869 cp_token *next_token;
15871 /* The common case is that this is not a constructor declarator, so
15872 try to avoid doing lots of work if at all possible. It's not
15873 valid declare a constructor at function scope. */
15874 if (parser->in_function_body)
15876 /* And only certain tokens can begin a constructor declarator. */
15877 next_token = cp_lexer_peek_token (parser->lexer);
15878 if (next_token->type != CPP_NAME
15879 && next_token->type != CPP_SCOPE
15880 && next_token->type != CPP_NESTED_NAME_SPECIFIER
15881 && next_token->type != CPP_TEMPLATE_ID)
15884 /* Parse tentatively; we are going to roll back all of the tokens
15886 cp_parser_parse_tentatively (parser);
15887 /* Assume that we are looking at a constructor declarator. */
15888 constructor_p = true;
15890 /* Look for the optional `::' operator. */
15891 cp_parser_global_scope_opt (parser,
15892 /*current_scope_valid_p=*/false);
15893 /* Look for the nested-name-specifier. */
15895 = (cp_parser_nested_name_specifier_opt (parser,
15896 /*typename_keyword_p=*/false,
15897 /*check_dependency_p=*/false,
15899 /*is_declaration=*/false)
15901 /* Outside of a class-specifier, there must be a
15902 nested-name-specifier. */
15903 if (!nested_name_p &&
15904 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
15906 constructor_p = false;
15907 /* If we still think that this might be a constructor-declarator,
15908 look for a class-name. */
15913 template <typename T> struct S { S(); };
15914 template <typename T> S<T>::S ();
15916 we must recognize that the nested `S' names a class.
15919 template <typename T> S<T>::S<T> ();
15921 we must recognize that the nested `S' names a template. */
15922 type_decl = cp_parser_class_name (parser,
15923 /*typename_keyword_p=*/false,
15924 /*template_keyword_p=*/false,
15926 /*check_dependency_p=*/false,
15927 /*class_head_p=*/false,
15928 /*is_declaration=*/false);
15929 /* If there was no class-name, then this is not a constructor. */
15930 constructor_p = !cp_parser_error_occurred (parser);
15933 /* If we're still considering a constructor, we have to see a `(',
15934 to begin the parameter-declaration-clause, followed by either a
15935 `)', an `...', or a decl-specifier. We need to check for a
15936 type-specifier to avoid being fooled into thinking that:
15940 is a constructor. (It is actually a function named `f' that
15941 takes one parameter (of type `int') and returns a value of type
15944 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
15946 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
15947 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
15948 /* A parameter declaration begins with a decl-specifier,
15949 which is either the "attribute" keyword, a storage class
15950 specifier, or (usually) a type-specifier. */
15951 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
15954 tree pushed_scope = NULL_TREE;
15955 unsigned saved_num_template_parameter_lists;
15957 /* Names appearing in the type-specifier should be looked up
15958 in the scope of the class. */
15959 if (current_class_type)
15963 type = TREE_TYPE (type_decl);
15964 if (TREE_CODE (type) == TYPENAME_TYPE)
15966 type = resolve_typename_type (type,
15967 /*only_current_p=*/false);
15968 if (type == error_mark_node)
15970 cp_parser_abort_tentative_parse (parser);
15974 pushed_scope = push_scope (type);
15977 /* Inside the constructor parameter list, surrounding
15978 template-parameter-lists do not apply. */
15979 saved_num_template_parameter_lists
15980 = parser->num_template_parameter_lists;
15981 parser->num_template_parameter_lists = 0;
15983 /* Look for the type-specifier. */
15984 cp_parser_type_specifier (parser,
15985 CP_PARSER_FLAGS_NONE,
15986 /*decl_specs=*/NULL,
15987 /*is_declarator=*/true,
15988 /*declares_class_or_enum=*/NULL,
15989 /*is_cv_qualifier=*/NULL);
15991 parser->num_template_parameter_lists
15992 = saved_num_template_parameter_lists;
15994 /* Leave the scope of the class. */
15996 pop_scope (pushed_scope);
15998 constructor_p = !cp_parser_error_occurred (parser);
16002 constructor_p = false;
16003 /* We did not really want to consume any tokens. */
16004 cp_parser_abort_tentative_parse (parser);
16006 return constructor_p;
16009 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16010 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16011 they must be performed once we are in the scope of the function.
16013 Returns the function defined. */
16016 cp_parser_function_definition_from_specifiers_and_declarator
16017 (cp_parser* parser,
16018 cp_decl_specifier_seq *decl_specifiers,
16020 const cp_declarator *declarator)
16025 /* Begin the function-definition. */
16026 success_p = start_function (decl_specifiers, declarator, attributes);
16028 /* The things we're about to see are not directly qualified by any
16029 template headers we've seen thus far. */
16030 reset_specialization ();
16032 /* If there were names looked up in the decl-specifier-seq that we
16033 did not check, check them now. We must wait until we are in the
16034 scope of the function to perform the checks, since the function
16035 might be a friend. */
16036 perform_deferred_access_checks ();
16040 /* Skip the entire function. */
16041 cp_parser_skip_to_end_of_block_or_statement (parser);
16042 fn = error_mark_node;
16044 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16046 /* Seen already, skip it. An error message has already been output. */
16047 cp_parser_skip_to_end_of_block_or_statement (parser);
16048 fn = current_function_decl;
16049 current_function_decl = NULL_TREE;
16050 /* If this is a function from a class, pop the nested class. */
16051 if (current_class_name)
16052 pop_nested_class ();
16055 fn = cp_parser_function_definition_after_declarator (parser,
16056 /*inline_p=*/false);
16061 /* Parse the part of a function-definition that follows the
16062 declarator. INLINE_P is TRUE iff this function is an inline
16063 function defined with a class-specifier.
16065 Returns the function defined. */
16068 cp_parser_function_definition_after_declarator (cp_parser* parser,
16072 bool ctor_initializer_p = false;
16073 bool saved_in_unbraced_linkage_specification_p;
16074 bool saved_in_function_body;
16075 unsigned saved_num_template_parameter_lists;
16077 saved_in_function_body = parser->in_function_body;
16078 parser->in_function_body = true;
16079 /* If the next token is `return', then the code may be trying to
16080 make use of the "named return value" extension that G++ used to
16082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16084 /* Consume the `return' keyword. */
16085 cp_lexer_consume_token (parser->lexer);
16086 /* Look for the identifier that indicates what value is to be
16088 cp_parser_identifier (parser);
16089 /* Issue an error message. */
16090 error ("named return values are no longer supported");
16091 /* Skip tokens until we reach the start of the function body. */
16094 cp_token *token = cp_lexer_peek_token (parser->lexer);
16095 if (token->type == CPP_OPEN_BRACE
16096 || token->type == CPP_EOF
16097 || token->type == CPP_PRAGMA_EOL)
16099 cp_lexer_consume_token (parser->lexer);
16102 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16103 anything declared inside `f'. */
16104 saved_in_unbraced_linkage_specification_p
16105 = parser->in_unbraced_linkage_specification_p;
16106 parser->in_unbraced_linkage_specification_p = false;
16107 /* Inside the function, surrounding template-parameter-lists do not
16109 saved_num_template_parameter_lists
16110 = parser->num_template_parameter_lists;
16111 parser->num_template_parameter_lists = 0;
16112 /* If the next token is `try', then we are looking at a
16113 function-try-block. */
16114 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16115 ctor_initializer_p = cp_parser_function_try_block (parser);
16116 /* A function-try-block includes the function-body, so we only do
16117 this next part if we're not processing a function-try-block. */
16120 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16122 /* Finish the function. */
16123 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16124 (inline_p ? 2 : 0));
16125 /* Generate code for it, if necessary. */
16126 expand_or_defer_fn (fn);
16127 /* Restore the saved values. */
16128 parser->in_unbraced_linkage_specification_p
16129 = saved_in_unbraced_linkage_specification_p;
16130 parser->num_template_parameter_lists
16131 = saved_num_template_parameter_lists;
16132 parser->in_function_body = saved_in_function_body;
16137 /* Parse a template-declaration, assuming that the `export' (and
16138 `extern') keywords, if present, has already been scanned. MEMBER_P
16139 is as for cp_parser_template_declaration. */
16142 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16144 tree decl = NULL_TREE;
16145 VEC (deferred_access_check,gc) *checks;
16146 tree parameter_list;
16147 bool friend_p = false;
16148 bool need_lang_pop;
16150 /* Look for the `template' keyword. */
16151 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16155 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16157 if (at_class_scope_p () && current_function_decl)
16159 /* 14.5.2.2 [temp.mem]
16161 A local class shall not have member templates. */
16162 error ("invalid declaration of member template in local class");
16163 cp_parser_skip_to_end_of_block_or_statement (parser);
16168 A template ... shall not have C linkage. */
16169 if (current_lang_name == lang_name_c)
16171 error ("template with C linkage");
16172 /* Give it C++ linkage to avoid confusing other parts of the
16174 push_lang_context (lang_name_cplusplus);
16175 need_lang_pop = true;
16178 need_lang_pop = false;
16180 /* We cannot perform access checks on the template parameter
16181 declarations until we know what is being declared, just as we
16182 cannot check the decl-specifier list. */
16183 push_deferring_access_checks (dk_deferred);
16185 /* If the next token is `>', then we have an invalid
16186 specialization. Rather than complain about an invalid template
16187 parameter, issue an error message here. */
16188 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16190 cp_parser_error (parser, "invalid explicit specialization");
16191 begin_specialization ();
16192 parameter_list = NULL_TREE;
16195 /* Parse the template parameters. */
16196 parameter_list = cp_parser_template_parameter_list (parser);
16198 /* Get the deferred access checks from the parameter list. These
16199 will be checked once we know what is being declared, as for a
16200 member template the checks must be performed in the scope of the
16201 class containing the member. */
16202 checks = get_deferred_access_checks ();
16204 /* Look for the `>'. */
16205 cp_parser_skip_to_end_of_template_parameter_list (parser);
16206 /* We just processed one more parameter list. */
16207 ++parser->num_template_parameter_lists;
16208 /* If the next token is `template', there are more template
16210 if (cp_lexer_next_token_is_keyword (parser->lexer,
16212 cp_parser_template_declaration_after_export (parser, member_p);
16215 /* There are no access checks when parsing a template, as we do not
16216 know if a specialization will be a friend. */
16217 push_deferring_access_checks (dk_no_check);
16218 decl = cp_parser_single_declaration (parser,
16222 pop_deferring_access_checks ();
16224 /* If this is a member template declaration, let the front
16226 if (member_p && !friend_p && decl)
16228 if (TREE_CODE (decl) == TYPE_DECL)
16229 cp_parser_check_access_in_redeclaration (decl);
16231 decl = finish_member_template_decl (decl);
16233 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16234 make_friend_class (current_class_type, TREE_TYPE (decl),
16235 /*complain=*/true);
16237 /* We are done with the current parameter list. */
16238 --parser->num_template_parameter_lists;
16240 pop_deferring_access_checks ();
16243 finish_template_decl (parameter_list);
16245 /* Register member declarations. */
16246 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16247 finish_member_declaration (decl);
16248 /* For the erroneous case of a template with C linkage, we pushed an
16249 implicit C++ linkage scope; exit that scope now. */
16251 pop_lang_context ();
16252 /* If DECL is a function template, we must return to parse it later.
16253 (Even though there is no definition, there might be default
16254 arguments that need handling.) */
16255 if (member_p && decl
16256 && (TREE_CODE (decl) == FUNCTION_DECL
16257 || DECL_FUNCTION_TEMPLATE_P (decl)))
16258 TREE_VALUE (parser->unparsed_functions_queues)
16259 = tree_cons (NULL_TREE, decl,
16260 TREE_VALUE (parser->unparsed_functions_queues));
16263 /* Perform the deferred access checks from a template-parameter-list.
16264 CHECKS is a TREE_LIST of access checks, as returned by
16265 get_deferred_access_checks. */
16268 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16270 ++processing_template_parmlist;
16271 perform_access_checks (checks);
16272 --processing_template_parmlist;
16275 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16276 `function-definition' sequence. MEMBER_P is true, this declaration
16277 appears in a class scope.
16279 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16280 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16283 cp_parser_single_declaration (cp_parser* parser,
16284 VEC (deferred_access_check,gc)* checks,
16288 int declares_class_or_enum;
16289 tree decl = NULL_TREE;
16290 cp_decl_specifier_seq decl_specifiers;
16291 bool function_definition_p = false;
16293 /* This function is only used when processing a template
16295 gcc_assert (innermost_scope_kind () == sk_template_parms
16296 || innermost_scope_kind () == sk_template_spec);
16298 /* Defer access checks until we know what is being declared. */
16299 push_deferring_access_checks (dk_deferred);
16301 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16303 cp_parser_decl_specifier_seq (parser,
16304 CP_PARSER_FLAGS_OPTIONAL,
16306 &declares_class_or_enum);
16308 *friend_p = cp_parser_friend_p (&decl_specifiers);
16310 /* There are no template typedefs. */
16311 if (decl_specifiers.specs[(int) ds_typedef])
16313 error ("template declaration of %qs", "typedef");
16314 decl = error_mark_node;
16317 /* Gather up the access checks that occurred the
16318 decl-specifier-seq. */
16319 stop_deferring_access_checks ();
16321 /* Check for the declaration of a template class. */
16322 if (declares_class_or_enum)
16324 if (cp_parser_declares_only_class_p (parser))
16326 decl = shadow_tag (&decl_specifiers);
16331 friend template <typename T> struct A<T>::B;
16334 A<T>::B will be represented by a TYPENAME_TYPE, and
16335 therefore not recognized by shadow_tag. */
16336 if (friend_p && *friend_p
16338 && decl_specifiers.type
16339 && TYPE_P (decl_specifiers.type))
16340 decl = decl_specifiers.type;
16342 if (decl && decl != error_mark_node)
16343 decl = TYPE_NAME (decl);
16345 decl = error_mark_node;
16347 /* Perform access checks for template parameters. */
16348 cp_parser_perform_template_parameter_access_checks (checks);
16351 /* If it's not a template class, try for a template function. If
16352 the next token is a `;', then this declaration does not declare
16353 anything. But, if there were errors in the decl-specifiers, then
16354 the error might well have come from an attempted class-specifier.
16355 In that case, there's no need to warn about a missing declarator. */
16357 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16358 || decl_specifiers.type != error_mark_node))
16359 decl = cp_parser_init_declarator (parser,
16362 /*function_definition_allowed_p=*/true,
16364 declares_class_or_enum,
16365 &function_definition_p);
16367 pop_deferring_access_checks ();
16369 /* Clear any current qualification; whatever comes next is the start
16370 of something new. */
16371 parser->scope = NULL_TREE;
16372 parser->qualifying_scope = NULL_TREE;
16373 parser->object_scope = NULL_TREE;
16374 /* Look for a trailing `;' after the declaration. */
16375 if (!function_definition_p
16376 && (decl == error_mark_node
16377 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16378 cp_parser_skip_to_end_of_block_or_statement (parser);
16383 /* Parse a cast-expression that is not the operand of a unary "&". */
16386 cp_parser_simple_cast_expression (cp_parser *parser)
16388 return cp_parser_cast_expression (parser, /*address_p=*/false,
16392 /* Parse a functional cast to TYPE. Returns an expression
16393 representing the cast. */
16396 cp_parser_functional_cast (cp_parser* parser, tree type)
16398 tree expression_list;
16402 = cp_parser_parenthesized_expression_list (parser, false,
16404 /*allow_expansion_p=*/true,
16405 /*non_constant_p=*/NULL);
16407 cast = build_functional_cast (type, expression_list);
16408 /* [expr.const]/1: In an integral constant expression "only type
16409 conversions to integral or enumeration type can be used". */
16410 if (TREE_CODE (type) == TYPE_DECL)
16411 type = TREE_TYPE (type);
16412 if (cast != error_mark_node
16413 && !cast_valid_in_integral_constant_expression_p (type)
16414 && (cp_parser_non_integral_constant_expression
16415 (parser, "a call to a constructor")))
16416 return error_mark_node;
16420 /* Save the tokens that make up the body of a member function defined
16421 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16422 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16423 specifiers applied to the declaration. Returns the FUNCTION_DECL
16424 for the member function. */
16427 cp_parser_save_member_function_body (cp_parser* parser,
16428 cp_decl_specifier_seq *decl_specifiers,
16429 cp_declarator *declarator,
16436 /* Create the function-declaration. */
16437 fn = start_method (decl_specifiers, declarator, attributes);
16438 /* If something went badly wrong, bail out now. */
16439 if (fn == error_mark_node)
16441 /* If there's a function-body, skip it. */
16442 if (cp_parser_token_starts_function_definition_p
16443 (cp_lexer_peek_token (parser->lexer)))
16444 cp_parser_skip_to_end_of_block_or_statement (parser);
16445 return error_mark_node;
16448 /* Remember it, if there default args to post process. */
16449 cp_parser_save_default_args (parser, fn);
16451 /* Save away the tokens that make up the body of the
16453 first = parser->lexer->next_token;
16454 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16455 /* Handle function try blocks. */
16456 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16457 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16458 last = parser->lexer->next_token;
16460 /* Save away the inline definition; we will process it when the
16461 class is complete. */
16462 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16463 DECL_PENDING_INLINE_P (fn) = 1;
16465 /* We need to know that this was defined in the class, so that
16466 friend templates are handled correctly. */
16467 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16469 /* We're done with the inline definition. */
16470 finish_method (fn);
16472 /* Add FN to the queue of functions to be parsed later. */
16473 TREE_VALUE (parser->unparsed_functions_queues)
16474 = tree_cons (NULL_TREE, fn,
16475 TREE_VALUE (parser->unparsed_functions_queues));
16480 /* Parse a template-argument-list, as well as the trailing ">" (but
16481 not the opening ">"). See cp_parser_template_argument_list for the
16485 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16489 tree saved_qualifying_scope;
16490 tree saved_object_scope;
16491 bool saved_greater_than_is_operator_p;
16492 bool saved_skip_evaluation;
16496 When parsing a template-id, the first non-nested `>' is taken as
16497 the end of the template-argument-list rather than a greater-than
16499 saved_greater_than_is_operator_p
16500 = parser->greater_than_is_operator_p;
16501 parser->greater_than_is_operator_p = false;
16502 /* Parsing the argument list may modify SCOPE, so we save it
16504 saved_scope = parser->scope;
16505 saved_qualifying_scope = parser->qualifying_scope;
16506 saved_object_scope = parser->object_scope;
16507 /* We need to evaluate the template arguments, even though this
16508 template-id may be nested within a "sizeof". */
16509 saved_skip_evaluation = skip_evaluation;
16510 skip_evaluation = false;
16511 /* Parse the template-argument-list itself. */
16512 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16513 arguments = NULL_TREE;
16515 arguments = cp_parser_template_argument_list (parser);
16516 /* Look for the `>' that ends the template-argument-list. If we find
16517 a '>>' instead, it's probably just a typo. */
16518 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16520 if (!saved_greater_than_is_operator_p)
16522 /* If we're in a nested template argument list, the '>>' has
16523 to be a typo for '> >'. We emit the error message, but we
16524 continue parsing and we push a '>' as next token, so that
16525 the argument list will be parsed correctly. Note that the
16526 global source location is still on the token before the
16527 '>>', so we need to say explicitly where we want it. */
16528 cp_token *token = cp_lexer_peek_token (parser->lexer);
16529 error ("%H%<>>%> should be %<> >%> "
16530 "within a nested template argument list",
16533 /* ??? Proper recovery should terminate two levels of
16534 template argument list here. */
16535 token->type = CPP_GREATER;
16539 /* If this is not a nested template argument list, the '>>'
16540 is a typo for '>'. Emit an error message and continue.
16541 Same deal about the token location, but here we can get it
16542 right by consuming the '>>' before issuing the diagnostic. */
16543 cp_lexer_consume_token (parser->lexer);
16544 error ("spurious %<>>%>, use %<>%> to terminate "
16545 "a template argument list");
16549 cp_parser_skip_to_end_of_template_parameter_list (parser);
16550 /* The `>' token might be a greater-than operator again now. */
16551 parser->greater_than_is_operator_p
16552 = saved_greater_than_is_operator_p;
16553 /* Restore the SAVED_SCOPE. */
16554 parser->scope = saved_scope;
16555 parser->qualifying_scope = saved_qualifying_scope;
16556 parser->object_scope = saved_object_scope;
16557 skip_evaluation = saved_skip_evaluation;
16562 /* MEMBER_FUNCTION is a member function, or a friend. If default
16563 arguments, or the body of the function have not yet been parsed,
16567 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16569 /* If this member is a template, get the underlying
16571 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16572 member_function = DECL_TEMPLATE_RESULT (member_function);
16574 /* There should not be any class definitions in progress at this
16575 point; the bodies of members are only parsed outside of all class
16577 gcc_assert (parser->num_classes_being_defined == 0);
16578 /* While we're parsing the member functions we might encounter more
16579 classes. We want to handle them right away, but we don't want
16580 them getting mixed up with functions that are currently in the
16582 parser->unparsed_functions_queues
16583 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16585 /* Make sure that any template parameters are in scope. */
16586 maybe_begin_member_template_processing (member_function);
16588 /* If the body of the function has not yet been parsed, parse it
16590 if (DECL_PENDING_INLINE_P (member_function))
16592 tree function_scope;
16593 cp_token_cache *tokens;
16595 /* The function is no longer pending; we are processing it. */
16596 tokens = DECL_PENDING_INLINE_INFO (member_function);
16597 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16598 DECL_PENDING_INLINE_P (member_function) = 0;
16600 /* If this is a local class, enter the scope of the containing
16602 function_scope = current_function_decl;
16603 if (function_scope)
16604 push_function_context_to (function_scope);
16607 /* Push the body of the function onto the lexer stack. */
16608 cp_parser_push_lexer_for_tokens (parser, tokens);
16610 /* Let the front end know that we going to be defining this
16612 start_preparsed_function (member_function, NULL_TREE,
16613 SF_PRE_PARSED | SF_INCLASS_INLINE);
16615 /* Don't do access checking if it is a templated function. */
16616 if (processing_template_decl)
16617 push_deferring_access_checks (dk_no_check);
16619 /* Now, parse the body of the function. */
16620 cp_parser_function_definition_after_declarator (parser,
16621 /*inline_p=*/true);
16623 if (processing_template_decl)
16624 pop_deferring_access_checks ();
16626 /* Leave the scope of the containing function. */
16627 if (function_scope)
16628 pop_function_context_from (function_scope);
16629 cp_parser_pop_lexer (parser);
16632 /* Remove any template parameters from the symbol table. */
16633 maybe_end_member_template_processing ();
16635 /* Restore the queue. */
16636 parser->unparsed_functions_queues
16637 = TREE_CHAIN (parser->unparsed_functions_queues);
16640 /* If DECL contains any default args, remember it on the unparsed
16641 functions queue. */
16644 cp_parser_save_default_args (cp_parser* parser, tree decl)
16648 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16650 probe = TREE_CHAIN (probe))
16651 if (TREE_PURPOSE (probe))
16653 TREE_PURPOSE (parser->unparsed_functions_queues)
16654 = tree_cons (current_class_type, decl,
16655 TREE_PURPOSE (parser->unparsed_functions_queues));
16660 /* FN is a FUNCTION_DECL which may contains a parameter with an
16661 unparsed DEFAULT_ARG. Parse the default args now. This function
16662 assumes that the current scope is the scope in which the default
16663 argument should be processed. */
16666 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
16668 bool saved_local_variables_forbidden_p;
16671 /* While we're parsing the default args, we might (due to the
16672 statement expression extension) encounter more classes. We want
16673 to handle them right away, but we don't want them getting mixed
16674 up with default args that are currently in the queue. */
16675 parser->unparsed_functions_queues
16676 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16678 /* Local variable names (and the `this' keyword) may not appear
16679 in a default argument. */
16680 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16681 parser->local_variables_forbidden_p = true;
16683 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
16685 parm = TREE_CHAIN (parm))
16687 cp_token_cache *tokens;
16688 tree default_arg = TREE_PURPOSE (parm);
16690 VEC(tree,gc) *insts;
16697 if (TREE_CODE (default_arg) != DEFAULT_ARG)
16698 /* This can happen for a friend declaration for a function
16699 already declared with default arguments. */
16702 /* Push the saved tokens for the default argument onto the parser's
16704 tokens = DEFARG_TOKENS (default_arg);
16705 cp_parser_push_lexer_for_tokens (parser, tokens);
16707 /* Parse the assignment-expression. */
16708 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
16710 if (!processing_template_decl)
16711 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
16713 TREE_PURPOSE (parm) = parsed_arg;
16715 /* Update any instantiations we've already created. */
16716 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
16717 VEC_iterate (tree, insts, ix, copy); ix++)
16718 TREE_PURPOSE (copy) = parsed_arg;
16720 /* If the token stream has not been completely used up, then
16721 there was extra junk after the end of the default
16723 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16724 cp_parser_error (parser, "expected %<,%>");
16726 /* Revert to the main lexer. */
16727 cp_parser_pop_lexer (parser);
16730 /* Make sure no default arg is missing. */
16731 check_default_args (fn);
16733 /* Restore the state of local_variables_forbidden_p. */
16734 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16736 /* Restore the queue. */
16737 parser->unparsed_functions_queues
16738 = TREE_CHAIN (parser->unparsed_functions_queues);
16741 /* Parse the operand of `sizeof' (or a similar operator). Returns
16742 either a TYPE or an expression, depending on the form of the
16743 input. The KEYWORD indicates which kind of expression we have
16747 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
16749 static const char *format;
16750 tree expr = NULL_TREE;
16751 const char *saved_message;
16752 bool saved_integral_constant_expression_p;
16753 bool saved_non_integral_constant_expression_p;
16754 bool pack_expansion_p = false;
16756 /* Initialize FORMAT the first time we get here. */
16758 format = "types may not be defined in '%s' expressions";
16760 /* Types cannot be defined in a `sizeof' expression. Save away the
16762 saved_message = parser->type_definition_forbidden_message;
16763 /* And create the new one. */
16764 parser->type_definition_forbidden_message
16765 = XNEWVEC (const char, strlen (format)
16766 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
16768 sprintf ((char *) parser->type_definition_forbidden_message,
16769 format, IDENTIFIER_POINTER (ridpointers[keyword]));
16771 /* The restrictions on constant-expressions do not apply inside
16772 sizeof expressions. */
16773 saved_integral_constant_expression_p
16774 = parser->integral_constant_expression_p;
16775 saved_non_integral_constant_expression_p
16776 = parser->non_integral_constant_expression_p;
16777 parser->integral_constant_expression_p = false;
16779 /* If it's a `...', then we are computing the length of a parameter
16781 if (keyword == RID_SIZEOF
16782 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16784 /* Consume the `...'. */
16785 cp_lexer_consume_token (parser->lexer);
16786 maybe_warn_variadic_templates ();
16788 /* Note that this is an expansion. */
16789 pack_expansion_p = true;
16792 /* Do not actually evaluate the expression. */
16794 /* If it's a `(', then we might be looking at the type-id
16796 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16799 bool saved_in_type_id_in_expr_p;
16801 /* We can't be sure yet whether we're looking at a type-id or an
16803 cp_parser_parse_tentatively (parser);
16804 /* Consume the `('. */
16805 cp_lexer_consume_token (parser->lexer);
16806 /* Parse the type-id. */
16807 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16808 parser->in_type_id_in_expr_p = true;
16809 type = cp_parser_type_id (parser);
16810 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16811 /* Now, look for the trailing `)'. */
16812 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
16813 /* If all went well, then we're done. */
16814 if (cp_parser_parse_definitely (parser))
16816 cp_decl_specifier_seq decl_specs;
16818 /* Build a trivial decl-specifier-seq. */
16819 clear_decl_specs (&decl_specs);
16820 decl_specs.type = type;
16822 /* Call grokdeclarator to figure out what type this is. */
16823 expr = grokdeclarator (NULL,
16827 /*attrlist=*/NULL);
16831 /* If the type-id production did not work out, then we must be
16832 looking at the unary-expression production. */
16834 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
16837 if (pack_expansion_p)
16838 /* Build a pack expansion. */
16839 expr = make_pack_expansion (expr);
16841 /* Go back to evaluating expressions. */
16844 /* Free the message we created. */
16845 free ((char *) parser->type_definition_forbidden_message);
16846 /* And restore the old one. */
16847 parser->type_definition_forbidden_message = saved_message;
16848 parser->integral_constant_expression_p
16849 = saved_integral_constant_expression_p;
16850 parser->non_integral_constant_expression_p
16851 = saved_non_integral_constant_expression_p;
16856 /* If the current declaration has no declarator, return true. */
16859 cp_parser_declares_only_class_p (cp_parser *parser)
16861 /* If the next token is a `;' or a `,' then there is no
16863 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16864 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
16867 /* Update the DECL_SPECS to reflect the storage class indicated by
16871 cp_parser_set_storage_class (cp_parser *parser,
16872 cp_decl_specifier_seq *decl_specs,
16875 cp_storage_class storage_class;
16877 if (parser->in_unbraced_linkage_specification_p)
16879 error ("invalid use of %qD in linkage specification",
16880 ridpointers[keyword]);
16883 else if (decl_specs->storage_class != sc_none)
16885 decl_specs->conflicting_specifiers_p = true;
16889 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
16890 && decl_specs->specs[(int) ds_thread])
16892 error ("%<__thread%> before %qD", ridpointers[keyword]);
16893 decl_specs->specs[(int) ds_thread] = 0;
16899 storage_class = sc_auto;
16902 storage_class = sc_register;
16905 storage_class = sc_static;
16908 storage_class = sc_extern;
16911 storage_class = sc_mutable;
16914 gcc_unreachable ();
16916 decl_specs->storage_class = storage_class;
16918 /* A storage class specifier cannot be applied alongside a typedef
16919 specifier. If there is a typedef specifier present then set
16920 conflicting_specifiers_p which will trigger an error later
16921 on in grokdeclarator. */
16922 if (decl_specs->specs[(int)ds_typedef])
16923 decl_specs->conflicting_specifiers_p = true;
16926 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
16927 is true, the type is a user-defined type; otherwise it is a
16928 built-in type specified by a keyword. */
16931 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
16933 bool user_defined_p)
16935 decl_specs->any_specifiers_p = true;
16937 /* If the user tries to redeclare bool or wchar_t (with, for
16938 example, in "typedef int wchar_t;") we remember that this is what
16939 happened. In system headers, we ignore these declarations so
16940 that G++ can work with system headers that are not C++-safe. */
16941 if (decl_specs->specs[(int) ds_typedef]
16943 && (type_spec == boolean_type_node
16944 || type_spec == wchar_type_node)
16945 && (decl_specs->type
16946 || decl_specs->specs[(int) ds_long]
16947 || decl_specs->specs[(int) ds_short]
16948 || decl_specs->specs[(int) ds_unsigned]
16949 || decl_specs->specs[(int) ds_signed]))
16951 decl_specs->redefined_builtin_type = type_spec;
16952 if (!decl_specs->type)
16954 decl_specs->type = type_spec;
16955 decl_specs->user_defined_type_p = false;
16958 else if (decl_specs->type)
16959 decl_specs->multiple_types_p = true;
16962 decl_specs->type = type_spec;
16963 decl_specs->user_defined_type_p = user_defined_p;
16964 decl_specs->redefined_builtin_type = NULL_TREE;
16968 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
16969 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
16972 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
16974 return decl_specifiers->specs[(int) ds_friend] != 0;
16977 /* If the next token is of the indicated TYPE, consume it. Otherwise,
16978 issue an error message indicating that TOKEN_DESC was expected.
16980 Returns the token consumed, if the token had the appropriate type.
16981 Otherwise, returns NULL. */
16984 cp_parser_require (cp_parser* parser,
16985 enum cpp_ttype type,
16986 const char* token_desc)
16988 if (cp_lexer_next_token_is (parser->lexer, type))
16989 return cp_lexer_consume_token (parser->lexer);
16992 /* Output the MESSAGE -- unless we're parsing tentatively. */
16993 if (!cp_parser_simulate_error (parser))
16995 char *message = concat ("expected ", token_desc, NULL);
16996 cp_parser_error (parser, message);
17003 /* An error message is produced if the next token is not '>'.
17004 All further tokens are skipped until the desired token is
17005 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17008 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17010 /* Current level of '< ... >'. */
17011 unsigned level = 0;
17012 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17013 unsigned nesting_depth = 0;
17015 /* Are we ready, yet? If not, issue error message. */
17016 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17019 /* Skip tokens until the desired token is found. */
17022 /* Peek at the next token. */
17023 switch (cp_lexer_peek_token (parser->lexer)->type)
17026 if (!nesting_depth)
17031 if (!nesting_depth && level-- == 0)
17033 /* We've reached the token we want, consume it and stop. */
17034 cp_lexer_consume_token (parser->lexer);
17039 case CPP_OPEN_PAREN:
17040 case CPP_OPEN_SQUARE:
17044 case CPP_CLOSE_PAREN:
17045 case CPP_CLOSE_SQUARE:
17046 if (nesting_depth-- == 0)
17051 case CPP_PRAGMA_EOL:
17052 case CPP_SEMICOLON:
17053 case CPP_OPEN_BRACE:
17054 case CPP_CLOSE_BRACE:
17055 /* The '>' was probably forgotten, don't look further. */
17062 /* Consume this token. */
17063 cp_lexer_consume_token (parser->lexer);
17067 /* If the next token is the indicated keyword, consume it. Otherwise,
17068 issue an error message indicating that TOKEN_DESC was expected.
17070 Returns the token consumed, if the token had the appropriate type.
17071 Otherwise, returns NULL. */
17074 cp_parser_require_keyword (cp_parser* parser,
17076 const char* token_desc)
17078 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17080 if (token && token->keyword != keyword)
17082 dyn_string_t error_msg;
17084 /* Format the error message. */
17085 error_msg = dyn_string_new (0);
17086 dyn_string_append_cstr (error_msg, "expected ");
17087 dyn_string_append_cstr (error_msg, token_desc);
17088 cp_parser_error (parser, error_msg->s);
17089 dyn_string_delete (error_msg);
17096 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17097 function-definition. */
17100 cp_parser_token_starts_function_definition_p (cp_token* token)
17102 return (/* An ordinary function-body begins with an `{'. */
17103 token->type == CPP_OPEN_BRACE
17104 /* A ctor-initializer begins with a `:'. */
17105 || token->type == CPP_COLON
17106 /* A function-try-block begins with `try'. */
17107 || token->keyword == RID_TRY
17108 /* The named return value extension begins with `return'. */
17109 || token->keyword == RID_RETURN);
17112 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17116 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17120 token = cp_lexer_peek_token (parser->lexer);
17121 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17124 /* Returns TRUE iff the next token is the "," or ">" ending a
17125 template-argument. */
17128 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17132 token = cp_lexer_peek_token (parser->lexer);
17133 return (token->type == CPP_COMMA
17134 || token->type == CPP_GREATER
17135 || token->type == CPP_ELLIPSIS);
17138 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17139 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17142 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17147 token = cp_lexer_peek_nth_token (parser->lexer, n);
17148 if (token->type == CPP_LESS)
17150 /* Check for the sequence `<::' in the original code. It would be lexed as
17151 `[:', where `[' is a digraph, and there is no whitespace before
17153 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17156 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17157 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17163 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17164 or none_type otherwise. */
17166 static enum tag_types
17167 cp_parser_token_is_class_key (cp_token* token)
17169 switch (token->keyword)
17174 return record_type;
17183 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17186 cp_parser_check_class_key (enum tag_types class_key, tree type)
17188 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17189 pedwarn ("%qs tag used in naming %q#T",
17190 class_key == union_type ? "union"
17191 : class_key == record_type ? "struct" : "class",
17195 /* Issue an error message if DECL is redeclared with different
17196 access than its original declaration [class.access.spec/3].
17197 This applies to nested classes and nested class templates.
17201 cp_parser_check_access_in_redeclaration (tree decl)
17203 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17206 if ((TREE_PRIVATE (decl)
17207 != (current_access_specifier == access_private_node))
17208 || (TREE_PROTECTED (decl)
17209 != (current_access_specifier == access_protected_node)))
17210 error ("%qD redeclared with different access", decl);
17213 /* Look for the `template' keyword, as a syntactic disambiguator.
17214 Return TRUE iff it is present, in which case it will be
17218 cp_parser_optional_template_keyword (cp_parser *parser)
17220 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17222 /* The `template' keyword can only be used within templates;
17223 outside templates the parser can always figure out what is a
17224 template and what is not. */
17225 if (!processing_template_decl)
17227 error ("%<template%> (as a disambiguator) is only allowed "
17228 "within templates");
17229 /* If this part of the token stream is rescanned, the same
17230 error message would be generated. So, we purge the token
17231 from the stream. */
17232 cp_lexer_purge_token (parser->lexer);
17237 /* Consume the `template' keyword. */
17238 cp_lexer_consume_token (parser->lexer);
17246 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17247 set PARSER->SCOPE, and perform other related actions. */
17250 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17253 struct tree_check *check_value;
17254 deferred_access_check *chk;
17255 VEC (deferred_access_check,gc) *checks;
17257 /* Get the stored value. */
17258 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17259 /* Perform any access checks that were deferred. */
17260 checks = check_value->checks;
17264 VEC_iterate (deferred_access_check, checks, i, chk) ;
17267 perform_or_defer_access_check (chk->binfo,
17272 /* Set the scope from the stored value. */
17273 parser->scope = check_value->value;
17274 parser->qualifying_scope = check_value->qualifying_scope;
17275 parser->object_scope = NULL_TREE;
17278 /* Consume tokens up through a non-nested END token. */
17281 cp_parser_cache_group (cp_parser *parser,
17282 enum cpp_ttype end,
17289 /* Abort a parenthesized expression if we encounter a brace. */
17290 if ((end == CPP_CLOSE_PAREN || depth == 0)
17291 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17293 /* If we've reached the end of the file, stop. */
17294 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17295 || (end != CPP_PRAGMA_EOL
17296 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17298 /* Consume the next token. */
17299 token = cp_lexer_consume_token (parser->lexer);
17300 /* See if it starts a new group. */
17301 if (token->type == CPP_OPEN_BRACE)
17303 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17307 else if (token->type == CPP_OPEN_PAREN)
17308 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17309 else if (token->type == CPP_PRAGMA)
17310 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17311 else if (token->type == end)
17316 /* Begin parsing tentatively. We always save tokens while parsing
17317 tentatively so that if the tentative parsing fails we can restore the
17321 cp_parser_parse_tentatively (cp_parser* parser)
17323 /* Enter a new parsing context. */
17324 parser->context = cp_parser_context_new (parser->context);
17325 /* Begin saving tokens. */
17326 cp_lexer_save_tokens (parser->lexer);
17327 /* In order to avoid repetitive access control error messages,
17328 access checks are queued up until we are no longer parsing
17330 push_deferring_access_checks (dk_deferred);
17333 /* Commit to the currently active tentative parse. */
17336 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17338 cp_parser_context *context;
17341 /* Mark all of the levels as committed. */
17342 lexer = parser->lexer;
17343 for (context = parser->context; context->next; context = context->next)
17345 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17347 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17348 while (!cp_lexer_saving_tokens (lexer))
17349 lexer = lexer->next;
17350 cp_lexer_commit_tokens (lexer);
17354 /* Abort the currently active tentative parse. All consumed tokens
17355 will be rolled back, and no diagnostics will be issued. */
17358 cp_parser_abort_tentative_parse (cp_parser* parser)
17360 cp_parser_simulate_error (parser);
17361 /* Now, pretend that we want to see if the construct was
17362 successfully parsed. */
17363 cp_parser_parse_definitely (parser);
17366 /* Stop parsing tentatively. If a parse error has occurred, restore the
17367 token stream. Otherwise, commit to the tokens we have consumed.
17368 Returns true if no error occurred; false otherwise. */
17371 cp_parser_parse_definitely (cp_parser* parser)
17373 bool error_occurred;
17374 cp_parser_context *context;
17376 /* Remember whether or not an error occurred, since we are about to
17377 destroy that information. */
17378 error_occurred = cp_parser_error_occurred (parser);
17379 /* Remove the topmost context from the stack. */
17380 context = parser->context;
17381 parser->context = context->next;
17382 /* If no parse errors occurred, commit to the tentative parse. */
17383 if (!error_occurred)
17385 /* Commit to the tokens read tentatively, unless that was
17387 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17388 cp_lexer_commit_tokens (parser->lexer);
17390 pop_to_parent_deferring_access_checks ();
17392 /* Otherwise, if errors occurred, roll back our state so that things
17393 are just as they were before we began the tentative parse. */
17396 cp_lexer_rollback_tokens (parser->lexer);
17397 pop_deferring_access_checks ();
17399 /* Add the context to the front of the free list. */
17400 context->next = cp_parser_context_free_list;
17401 cp_parser_context_free_list = context;
17403 return !error_occurred;
17406 /* Returns true if we are parsing tentatively and are not committed to
17407 this tentative parse. */
17410 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17412 return (cp_parser_parsing_tentatively (parser)
17413 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17416 /* Returns nonzero iff an error has occurred during the most recent
17417 tentative parse. */
17420 cp_parser_error_occurred (cp_parser* parser)
17422 return (cp_parser_parsing_tentatively (parser)
17423 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17426 /* Returns nonzero if GNU extensions are allowed. */
17429 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17431 return parser->allow_gnu_extensions_p;
17434 /* Objective-C++ Productions */
17437 /* Parse an Objective-C expression, which feeds into a primary-expression
17441 objc-message-expression
17442 objc-string-literal
17443 objc-encode-expression
17444 objc-protocol-expression
17445 objc-selector-expression
17447 Returns a tree representation of the expression. */
17450 cp_parser_objc_expression (cp_parser* parser)
17452 /* Try to figure out what kind of declaration is present. */
17453 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17457 case CPP_OPEN_SQUARE:
17458 return cp_parser_objc_message_expression (parser);
17460 case CPP_OBJC_STRING:
17461 kwd = cp_lexer_consume_token (parser->lexer);
17462 return objc_build_string_object (kwd->u.value);
17465 switch (kwd->keyword)
17467 case RID_AT_ENCODE:
17468 return cp_parser_objc_encode_expression (parser);
17470 case RID_AT_PROTOCOL:
17471 return cp_parser_objc_protocol_expression (parser);
17473 case RID_AT_SELECTOR:
17474 return cp_parser_objc_selector_expression (parser);
17480 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17481 cp_parser_skip_to_end_of_block_or_statement (parser);
17484 return error_mark_node;
17487 /* Parse an Objective-C message expression.
17489 objc-message-expression:
17490 [ objc-message-receiver objc-message-args ]
17492 Returns a representation of an Objective-C message. */
17495 cp_parser_objc_message_expression (cp_parser* parser)
17497 tree receiver, messageargs;
17499 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17500 receiver = cp_parser_objc_message_receiver (parser);
17501 messageargs = cp_parser_objc_message_args (parser);
17502 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17504 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17507 /* Parse an objc-message-receiver.
17509 objc-message-receiver:
17511 simple-type-specifier
17513 Returns a representation of the type or expression. */
17516 cp_parser_objc_message_receiver (cp_parser* parser)
17520 /* An Objective-C message receiver may be either (1) a type
17521 or (2) an expression. */
17522 cp_parser_parse_tentatively (parser);
17523 rcv = cp_parser_expression (parser, false);
17525 if (cp_parser_parse_definitely (parser))
17528 rcv = cp_parser_simple_type_specifier (parser,
17529 /*decl_specs=*/NULL,
17530 CP_PARSER_FLAGS_NONE);
17532 return objc_get_class_reference (rcv);
17535 /* Parse the arguments and selectors comprising an Objective-C message.
17540 objc-selector-args , objc-comma-args
17542 objc-selector-args:
17543 objc-selector [opt] : assignment-expression
17544 objc-selector-args objc-selector [opt] : assignment-expression
17547 assignment-expression
17548 objc-comma-args , assignment-expression
17550 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17551 selector arguments and TREE_VALUE containing a list of comma
17555 cp_parser_objc_message_args (cp_parser* parser)
17557 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17558 bool maybe_unary_selector_p = true;
17559 cp_token *token = cp_lexer_peek_token (parser->lexer);
17561 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17563 tree selector = NULL_TREE, arg;
17565 if (token->type != CPP_COLON)
17566 selector = cp_parser_objc_selector (parser);
17568 /* Detect if we have a unary selector. */
17569 if (maybe_unary_selector_p
17570 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17571 return build_tree_list (selector, NULL_TREE);
17573 maybe_unary_selector_p = false;
17574 cp_parser_require (parser, CPP_COLON, "`:'");
17575 arg = cp_parser_assignment_expression (parser, false);
17578 = chainon (sel_args,
17579 build_tree_list (selector, arg));
17581 token = cp_lexer_peek_token (parser->lexer);
17584 /* Handle non-selector arguments, if any. */
17585 while (token->type == CPP_COMMA)
17589 cp_lexer_consume_token (parser->lexer);
17590 arg = cp_parser_assignment_expression (parser, false);
17593 = chainon (addl_args,
17594 build_tree_list (NULL_TREE, arg));
17596 token = cp_lexer_peek_token (parser->lexer);
17599 return build_tree_list (sel_args, addl_args);
17602 /* Parse an Objective-C encode expression.
17604 objc-encode-expression:
17605 @encode objc-typename
17607 Returns an encoded representation of the type argument. */
17610 cp_parser_objc_encode_expression (cp_parser* parser)
17614 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17615 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17616 type = complete_type (cp_parser_type_id (parser));
17617 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17621 error ("%<@encode%> must specify a type as an argument");
17622 return error_mark_node;
17625 return objc_build_encode_expr (type);
17628 /* Parse an Objective-C @defs expression. */
17631 cp_parser_objc_defs_expression (cp_parser *parser)
17635 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17636 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17637 name = cp_parser_identifier (parser);
17638 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17640 return objc_get_class_ivars (name);
17643 /* Parse an Objective-C protocol expression.
17645 objc-protocol-expression:
17646 @protocol ( identifier )
17648 Returns a representation of the protocol expression. */
17651 cp_parser_objc_protocol_expression (cp_parser* parser)
17655 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
17656 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17657 proto = cp_parser_identifier (parser);
17658 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17660 return objc_build_protocol_expr (proto);
17663 /* Parse an Objective-C selector expression.
17665 objc-selector-expression:
17666 @selector ( objc-method-signature )
17668 objc-method-signature:
17674 objc-selector-seq objc-selector :
17676 Returns a representation of the method selector. */
17679 cp_parser_objc_selector_expression (cp_parser* parser)
17681 tree sel_seq = NULL_TREE;
17682 bool maybe_unary_selector_p = true;
17685 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
17686 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17687 token = cp_lexer_peek_token (parser->lexer);
17689 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
17690 || token->type == CPP_SCOPE)
17692 tree selector = NULL_TREE;
17694 if (token->type != CPP_COLON
17695 || token->type == CPP_SCOPE)
17696 selector = cp_parser_objc_selector (parser);
17698 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
17699 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
17701 /* Detect if we have a unary selector. */
17702 if (maybe_unary_selector_p)
17704 sel_seq = selector;
17705 goto finish_selector;
17709 cp_parser_error (parser, "expected %<:%>");
17712 maybe_unary_selector_p = false;
17713 token = cp_lexer_consume_token (parser->lexer);
17715 if (token->type == CPP_SCOPE)
17718 = chainon (sel_seq,
17719 build_tree_list (selector, NULL_TREE));
17721 = chainon (sel_seq,
17722 build_tree_list (NULL_TREE, NULL_TREE));
17726 = chainon (sel_seq,
17727 build_tree_list (selector, NULL_TREE));
17729 token = cp_lexer_peek_token (parser->lexer);
17733 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17735 return objc_build_selector_expr (sel_seq);
17738 /* Parse a list of identifiers.
17740 objc-identifier-list:
17742 objc-identifier-list , identifier
17744 Returns a TREE_LIST of identifier nodes. */
17747 cp_parser_objc_identifier_list (cp_parser* parser)
17749 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
17750 cp_token *sep = cp_lexer_peek_token (parser->lexer);
17752 while (sep->type == CPP_COMMA)
17754 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
17755 list = chainon (list,
17756 build_tree_list (NULL_TREE,
17757 cp_parser_identifier (parser)));
17758 sep = cp_lexer_peek_token (parser->lexer);
17764 /* Parse an Objective-C alias declaration.
17766 objc-alias-declaration:
17767 @compatibility_alias identifier identifier ;
17769 This function registers the alias mapping with the Objective-C front end.
17770 It returns nothing. */
17773 cp_parser_objc_alias_declaration (cp_parser* parser)
17777 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
17778 alias = cp_parser_identifier (parser);
17779 orig = cp_parser_identifier (parser);
17780 objc_declare_alias (alias, orig);
17781 cp_parser_consume_semicolon_at_end_of_statement (parser);
17784 /* Parse an Objective-C class forward-declaration.
17786 objc-class-declaration:
17787 @class objc-identifier-list ;
17789 The function registers the forward declarations with the Objective-C
17790 front end. It returns nothing. */
17793 cp_parser_objc_class_declaration (cp_parser* parser)
17795 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
17796 objc_declare_class (cp_parser_objc_identifier_list (parser));
17797 cp_parser_consume_semicolon_at_end_of_statement (parser);
17800 /* Parse a list of Objective-C protocol references.
17802 objc-protocol-refs-opt:
17803 objc-protocol-refs [opt]
17805 objc-protocol-refs:
17806 < objc-identifier-list >
17808 Returns a TREE_LIST of identifiers, if any. */
17811 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
17813 tree protorefs = NULL_TREE;
17815 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
17817 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
17818 protorefs = cp_parser_objc_identifier_list (parser);
17819 cp_parser_require (parser, CPP_GREATER, "`>'");
17825 /* Parse a Objective-C visibility specification. */
17828 cp_parser_objc_visibility_spec (cp_parser* parser)
17830 cp_token *vis = cp_lexer_peek_token (parser->lexer);
17832 switch (vis->keyword)
17834 case RID_AT_PRIVATE:
17835 objc_set_visibility (2);
17837 case RID_AT_PROTECTED:
17838 objc_set_visibility (0);
17840 case RID_AT_PUBLIC:
17841 objc_set_visibility (1);
17847 /* Eat '@private'/'@protected'/'@public'. */
17848 cp_lexer_consume_token (parser->lexer);
17851 /* Parse an Objective-C method type. */
17854 cp_parser_objc_method_type (cp_parser* parser)
17856 objc_set_method_type
17857 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
17862 /* Parse an Objective-C protocol qualifier. */
17865 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
17867 tree quals = NULL_TREE, node;
17868 cp_token *token = cp_lexer_peek_token (parser->lexer);
17870 node = token->u.value;
17872 while (node && TREE_CODE (node) == IDENTIFIER_NODE
17873 && (node == ridpointers [(int) RID_IN]
17874 || node == ridpointers [(int) RID_OUT]
17875 || node == ridpointers [(int) RID_INOUT]
17876 || node == ridpointers [(int) RID_BYCOPY]
17877 || node == ridpointers [(int) RID_BYREF]
17878 || node == ridpointers [(int) RID_ONEWAY]))
17880 quals = tree_cons (NULL_TREE, node, quals);
17881 cp_lexer_consume_token (parser->lexer);
17882 token = cp_lexer_peek_token (parser->lexer);
17883 node = token->u.value;
17889 /* Parse an Objective-C typename. */
17892 cp_parser_objc_typename (cp_parser* parser)
17894 tree typename = NULL_TREE;
17896 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17898 tree proto_quals, cp_type = NULL_TREE;
17900 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
17901 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
17903 /* An ObjC type name may consist of just protocol qualifiers, in which
17904 case the type shall default to 'id'. */
17905 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
17906 cp_type = cp_parser_type_id (parser);
17908 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17909 typename = build_tree_list (proto_quals, cp_type);
17915 /* Check to see if TYPE refers to an Objective-C selector name. */
17918 cp_parser_objc_selector_p (enum cpp_ttype type)
17920 return (type == CPP_NAME || type == CPP_KEYWORD
17921 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
17922 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
17923 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
17924 || type == CPP_XOR || type == CPP_XOR_EQ);
17927 /* Parse an Objective-C selector. */
17930 cp_parser_objc_selector (cp_parser* parser)
17932 cp_token *token = cp_lexer_consume_token (parser->lexer);
17934 if (!cp_parser_objc_selector_p (token->type))
17936 error ("invalid Objective-C++ selector name");
17937 return error_mark_node;
17940 /* C++ operator names are allowed to appear in ObjC selectors. */
17941 switch (token->type)
17943 case CPP_AND_AND: return get_identifier ("and");
17944 case CPP_AND_EQ: return get_identifier ("and_eq");
17945 case CPP_AND: return get_identifier ("bitand");
17946 case CPP_OR: return get_identifier ("bitor");
17947 case CPP_COMPL: return get_identifier ("compl");
17948 case CPP_NOT: return get_identifier ("not");
17949 case CPP_NOT_EQ: return get_identifier ("not_eq");
17950 case CPP_OR_OR: return get_identifier ("or");
17951 case CPP_OR_EQ: return get_identifier ("or_eq");
17952 case CPP_XOR: return get_identifier ("xor");
17953 case CPP_XOR_EQ: return get_identifier ("xor_eq");
17954 default: return token->u.value;
17958 /* Parse an Objective-C params list. */
17961 cp_parser_objc_method_keyword_params (cp_parser* parser)
17963 tree params = NULL_TREE;
17964 bool maybe_unary_selector_p = true;
17965 cp_token *token = cp_lexer_peek_token (parser->lexer);
17967 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17969 tree selector = NULL_TREE, typename, identifier;
17971 if (token->type != CPP_COLON)
17972 selector = cp_parser_objc_selector (parser);
17974 /* Detect if we have a unary selector. */
17975 if (maybe_unary_selector_p
17976 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17979 maybe_unary_selector_p = false;
17980 cp_parser_require (parser, CPP_COLON, "`:'");
17981 typename = cp_parser_objc_typename (parser);
17982 identifier = cp_parser_identifier (parser);
17986 objc_build_keyword_decl (selector,
17990 token = cp_lexer_peek_token (parser->lexer);
17996 /* Parse the non-keyword Objective-C params. */
17999 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18001 tree params = make_node (TREE_LIST);
18002 cp_token *token = cp_lexer_peek_token (parser->lexer);
18003 *ellipsisp = false; /* Initially, assume no ellipsis. */
18005 while (token->type == CPP_COMMA)
18007 cp_parameter_declarator *parmdecl;
18010 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18011 token = cp_lexer_peek_token (parser->lexer);
18013 if (token->type == CPP_ELLIPSIS)
18015 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18020 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18021 parm = grokdeclarator (parmdecl->declarator,
18022 &parmdecl->decl_specifiers,
18023 PARM, /*initialized=*/0,
18024 /*attrlist=*/NULL);
18026 chainon (params, build_tree_list (NULL_TREE, parm));
18027 token = cp_lexer_peek_token (parser->lexer);
18033 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18036 cp_parser_objc_interstitial_code (cp_parser* parser)
18038 cp_token *token = cp_lexer_peek_token (parser->lexer);
18040 /* If the next token is `extern' and the following token is a string
18041 literal, then we have a linkage specification. */
18042 if (token->keyword == RID_EXTERN
18043 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18044 cp_parser_linkage_specification (parser);
18045 /* Handle #pragma, if any. */
18046 else if (token->type == CPP_PRAGMA)
18047 cp_parser_pragma (parser, pragma_external);
18048 /* Allow stray semicolons. */
18049 else if (token->type == CPP_SEMICOLON)
18050 cp_lexer_consume_token (parser->lexer);
18051 /* Finally, try to parse a block-declaration, or a function-definition. */
18053 cp_parser_block_declaration (parser, /*statement_p=*/false);
18056 /* Parse a method signature. */
18059 cp_parser_objc_method_signature (cp_parser* parser)
18061 tree rettype, kwdparms, optparms;
18062 bool ellipsis = false;
18064 cp_parser_objc_method_type (parser);
18065 rettype = cp_parser_objc_typename (parser);
18066 kwdparms = cp_parser_objc_method_keyword_params (parser);
18067 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18069 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18072 /* Pars an Objective-C method prototype list. */
18075 cp_parser_objc_method_prototype_list (cp_parser* parser)
18077 cp_token *token = cp_lexer_peek_token (parser->lexer);
18079 while (token->keyword != RID_AT_END)
18081 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18083 objc_add_method_declaration
18084 (cp_parser_objc_method_signature (parser));
18085 cp_parser_consume_semicolon_at_end_of_statement (parser);
18088 /* Allow for interspersed non-ObjC++ code. */
18089 cp_parser_objc_interstitial_code (parser);
18091 token = cp_lexer_peek_token (parser->lexer);
18094 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18095 objc_finish_interface ();
18098 /* Parse an Objective-C method definition list. */
18101 cp_parser_objc_method_definition_list (cp_parser* parser)
18103 cp_token *token = cp_lexer_peek_token (parser->lexer);
18105 while (token->keyword != RID_AT_END)
18109 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18111 push_deferring_access_checks (dk_deferred);
18112 objc_start_method_definition
18113 (cp_parser_objc_method_signature (parser));
18115 /* For historical reasons, we accept an optional semicolon. */
18116 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18117 cp_lexer_consume_token (parser->lexer);
18119 perform_deferred_access_checks ();
18120 stop_deferring_access_checks ();
18121 meth = cp_parser_function_definition_after_declarator (parser,
18123 pop_deferring_access_checks ();
18124 objc_finish_method_definition (meth);
18127 /* Allow for interspersed non-ObjC++ code. */
18128 cp_parser_objc_interstitial_code (parser);
18130 token = cp_lexer_peek_token (parser->lexer);
18133 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18134 objc_finish_implementation ();
18137 /* Parse Objective-C ivars. */
18140 cp_parser_objc_class_ivars (cp_parser* parser)
18142 cp_token *token = cp_lexer_peek_token (parser->lexer);
18144 if (token->type != CPP_OPEN_BRACE)
18145 return; /* No ivars specified. */
18147 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18148 token = cp_lexer_peek_token (parser->lexer);
18150 while (token->type != CPP_CLOSE_BRACE)
18152 cp_decl_specifier_seq declspecs;
18153 int decl_class_or_enum_p;
18154 tree prefix_attributes;
18156 cp_parser_objc_visibility_spec (parser);
18158 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18161 cp_parser_decl_specifier_seq (parser,
18162 CP_PARSER_FLAGS_OPTIONAL,
18164 &decl_class_or_enum_p);
18165 prefix_attributes = declspecs.attributes;
18166 declspecs.attributes = NULL_TREE;
18168 /* Keep going until we hit the `;' at the end of the
18170 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18172 tree width = NULL_TREE, attributes, first_attribute, decl;
18173 cp_declarator *declarator = NULL;
18174 int ctor_dtor_or_conv_p;
18176 /* Check for a (possibly unnamed) bitfield declaration. */
18177 token = cp_lexer_peek_token (parser->lexer);
18178 if (token->type == CPP_COLON)
18181 if (token->type == CPP_NAME
18182 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18185 /* Get the name of the bitfield. */
18186 declarator = make_id_declarator (NULL_TREE,
18187 cp_parser_identifier (parser),
18191 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18192 /* Get the width of the bitfield. */
18194 = cp_parser_constant_expression (parser,
18195 /*allow_non_constant=*/false,
18200 /* Parse the declarator. */
18202 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18203 &ctor_dtor_or_conv_p,
18204 /*parenthesized_p=*/NULL,
18205 /*member_p=*/false);
18208 /* Look for attributes that apply to the ivar. */
18209 attributes = cp_parser_attributes_opt (parser);
18210 /* Remember which attributes are prefix attributes and
18212 first_attribute = attributes;
18213 /* Combine the attributes. */
18214 attributes = chainon (prefix_attributes, attributes);
18218 /* Create the bitfield declaration. */
18219 decl = grokbitfield (declarator, &declspecs, width);
18220 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18223 decl = grokfield (declarator, &declspecs,
18224 NULL_TREE, /*init_const_expr_p=*/false,
18225 NULL_TREE, attributes);
18227 /* Add the instance variable. */
18228 objc_add_instance_variable (decl);
18230 /* Reset PREFIX_ATTRIBUTES. */
18231 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18232 attributes = TREE_CHAIN (attributes);
18234 TREE_CHAIN (attributes) = NULL_TREE;
18236 token = cp_lexer_peek_token (parser->lexer);
18238 if (token->type == CPP_COMMA)
18240 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18246 cp_parser_consume_semicolon_at_end_of_statement (parser);
18247 token = cp_lexer_peek_token (parser->lexer);
18250 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18251 /* For historical reasons, we accept an optional semicolon. */
18252 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18253 cp_lexer_consume_token (parser->lexer);
18256 /* Parse an Objective-C protocol declaration. */
18259 cp_parser_objc_protocol_declaration (cp_parser* parser)
18261 tree proto, protorefs;
18264 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18265 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18267 error ("identifier expected after %<@protocol%>");
18271 /* See if we have a forward declaration or a definition. */
18272 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18274 /* Try a forward declaration first. */
18275 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18277 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18279 cp_parser_consume_semicolon_at_end_of_statement (parser);
18282 /* Ok, we got a full-fledged definition (or at least should). */
18285 proto = cp_parser_identifier (parser);
18286 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18287 objc_start_protocol (proto, protorefs);
18288 cp_parser_objc_method_prototype_list (parser);
18292 /* Parse an Objective-C superclass or category. */
18295 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18298 cp_token *next = cp_lexer_peek_token (parser->lexer);
18300 *super = *categ = NULL_TREE;
18301 if (next->type == CPP_COLON)
18303 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18304 *super = cp_parser_identifier (parser);
18306 else if (next->type == CPP_OPEN_PAREN)
18308 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18309 *categ = cp_parser_identifier (parser);
18310 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18314 /* Parse an Objective-C class interface. */
18317 cp_parser_objc_class_interface (cp_parser* parser)
18319 tree name, super, categ, protos;
18321 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18322 name = cp_parser_identifier (parser);
18323 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18324 protos = cp_parser_objc_protocol_refs_opt (parser);
18326 /* We have either a class or a category on our hands. */
18328 objc_start_category_interface (name, categ, protos);
18331 objc_start_class_interface (name, super, protos);
18332 /* Handle instance variable declarations, if any. */
18333 cp_parser_objc_class_ivars (parser);
18334 objc_continue_interface ();
18337 cp_parser_objc_method_prototype_list (parser);
18340 /* Parse an Objective-C class implementation. */
18343 cp_parser_objc_class_implementation (cp_parser* parser)
18345 tree name, super, categ;
18347 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18348 name = cp_parser_identifier (parser);
18349 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18351 /* We have either a class or a category on our hands. */
18353 objc_start_category_implementation (name, categ);
18356 objc_start_class_implementation (name, super);
18357 /* Handle instance variable declarations, if any. */
18358 cp_parser_objc_class_ivars (parser);
18359 objc_continue_implementation ();
18362 cp_parser_objc_method_definition_list (parser);
18365 /* Consume the @end token and finish off the implementation. */
18368 cp_parser_objc_end_implementation (cp_parser* parser)
18370 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18371 objc_finish_implementation ();
18374 /* Parse an Objective-C declaration. */
18377 cp_parser_objc_declaration (cp_parser* parser)
18379 /* Try to figure out what kind of declaration is present. */
18380 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18382 switch (kwd->keyword)
18385 cp_parser_objc_alias_declaration (parser);
18388 cp_parser_objc_class_declaration (parser);
18390 case RID_AT_PROTOCOL:
18391 cp_parser_objc_protocol_declaration (parser);
18393 case RID_AT_INTERFACE:
18394 cp_parser_objc_class_interface (parser);
18396 case RID_AT_IMPLEMENTATION:
18397 cp_parser_objc_class_implementation (parser);
18400 cp_parser_objc_end_implementation (parser);
18403 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18404 cp_parser_skip_to_end_of_block_or_statement (parser);
18408 /* Parse an Objective-C try-catch-finally statement.
18410 objc-try-catch-finally-stmt:
18411 @try compound-statement objc-catch-clause-seq [opt]
18412 objc-finally-clause [opt]
18414 objc-catch-clause-seq:
18415 objc-catch-clause objc-catch-clause-seq [opt]
18418 @catch ( exception-declaration ) compound-statement
18420 objc-finally-clause
18421 @finally compound-statement
18423 Returns NULL_TREE. */
18426 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18427 location_t location;
18430 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18431 location = cp_lexer_peek_token (parser->lexer)->location;
18432 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18433 node, lest it get absorbed into the surrounding block. */
18434 stmt = push_stmt_list ();
18435 cp_parser_compound_statement (parser, NULL, false);
18436 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18438 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18440 cp_parameter_declarator *parmdecl;
18443 cp_lexer_consume_token (parser->lexer);
18444 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18445 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18446 parm = grokdeclarator (parmdecl->declarator,
18447 &parmdecl->decl_specifiers,
18448 PARM, /*initialized=*/0,
18449 /*attrlist=*/NULL);
18450 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18451 objc_begin_catch_clause (parm);
18452 cp_parser_compound_statement (parser, NULL, false);
18453 objc_finish_catch_clause ();
18456 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18458 cp_lexer_consume_token (parser->lexer);
18459 location = cp_lexer_peek_token (parser->lexer)->location;
18460 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18461 node, lest it get absorbed into the surrounding block. */
18462 stmt = push_stmt_list ();
18463 cp_parser_compound_statement (parser, NULL, false);
18464 objc_build_finally_clause (location, pop_stmt_list (stmt));
18467 return objc_finish_try_stmt ();
18470 /* Parse an Objective-C synchronized statement.
18472 objc-synchronized-stmt:
18473 @synchronized ( expression ) compound-statement
18475 Returns NULL_TREE. */
18478 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18479 location_t location;
18482 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18484 location = cp_lexer_peek_token (parser->lexer)->location;
18485 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18486 lock = cp_parser_expression (parser, false);
18487 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18489 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18490 node, lest it get absorbed into the surrounding block. */
18491 stmt = push_stmt_list ();
18492 cp_parser_compound_statement (parser, NULL, false);
18494 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18497 /* Parse an Objective-C throw statement.
18500 @throw assignment-expression [opt] ;
18502 Returns a constructed '@throw' statement. */
18505 cp_parser_objc_throw_statement (cp_parser *parser) {
18506 tree expr = NULL_TREE;
18508 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18510 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18511 expr = cp_parser_assignment_expression (parser, false);
18513 cp_parser_consume_semicolon_at_end_of_statement (parser);
18515 return objc_build_throw_stmt (expr);
18518 /* Parse an Objective-C statement. */
18521 cp_parser_objc_statement (cp_parser * parser) {
18522 /* Try to figure out what kind of declaration is present. */
18523 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18525 switch (kwd->keyword)
18528 return cp_parser_objc_try_catch_finally_statement (parser);
18529 case RID_AT_SYNCHRONIZED:
18530 return cp_parser_objc_synchronized_statement (parser);
18532 return cp_parser_objc_throw_statement (parser);
18534 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18535 cp_parser_skip_to_end_of_block_or_statement (parser);
18538 return error_mark_node;
18541 /* OpenMP 2.5 parsing routines. */
18543 /* Returns name of the next clause.
18544 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18545 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18546 returned and the token is consumed. */
18548 static pragma_omp_clause
18549 cp_parser_omp_clause_name (cp_parser *parser)
18551 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18553 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18554 result = PRAGMA_OMP_CLAUSE_IF;
18555 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18556 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18557 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18558 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18559 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18561 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18562 const char *p = IDENTIFIER_POINTER (id);
18567 if (!strcmp ("copyin", p))
18568 result = PRAGMA_OMP_CLAUSE_COPYIN;
18569 else if (!strcmp ("copyprivate", p))
18570 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18573 if (!strcmp ("firstprivate", p))
18574 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18577 if (!strcmp ("lastprivate", p))
18578 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18581 if (!strcmp ("nowait", p))
18582 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18583 else if (!strcmp ("num_threads", p))
18584 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18587 if (!strcmp ("ordered", p))
18588 result = PRAGMA_OMP_CLAUSE_ORDERED;
18591 if (!strcmp ("reduction", p))
18592 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18595 if (!strcmp ("schedule", p))
18596 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18597 else if (!strcmp ("shared", p))
18598 result = PRAGMA_OMP_CLAUSE_SHARED;
18603 if (result != PRAGMA_OMP_CLAUSE_NONE)
18604 cp_lexer_consume_token (parser->lexer);
18609 /* Validate that a clause of the given type does not already exist. */
18612 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18616 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18617 if (OMP_CLAUSE_CODE (c) == code)
18619 error ("too many %qs clauses", name);
18627 variable-list , identifier
18629 In addition, we match a closing parenthesis. An opening parenthesis
18630 will have been consumed by the caller.
18632 If KIND is nonzero, create the appropriate node and install the decl
18633 in OMP_CLAUSE_DECL and add the node to the head of the list.
18635 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18636 return the list created. */
18639 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18646 name = cp_parser_id_expression (parser, /*template_p=*/false,
18647 /*check_dependency_p=*/true,
18648 /*template_p=*/NULL,
18649 /*declarator_p=*/false,
18650 /*optional_p=*/false);
18651 if (name == error_mark_node)
18654 decl = cp_parser_lookup_name_simple (parser, name);
18655 if (decl == error_mark_node)
18656 cp_parser_name_lookup_error (parser, name, decl, NULL);
18657 else if (kind != 0)
18659 tree u = build_omp_clause (kind);
18660 OMP_CLAUSE_DECL (u) = decl;
18661 OMP_CLAUSE_CHAIN (u) = list;
18665 list = tree_cons (decl, NULL_TREE, list);
18668 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18670 cp_lexer_consume_token (parser->lexer);
18673 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18677 /* Try to resync to an unnested comma. Copied from
18678 cp_parser_parenthesized_expression_list. */
18680 ending = cp_parser_skip_to_closing_parenthesis (parser,
18681 /*recovering=*/true,
18683 /*consume_paren=*/true);
18691 /* Similarly, but expect leading and trailing parenthesis. This is a very
18692 common case for omp clauses. */
18695 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
18697 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18698 return cp_parser_omp_var_list_no_open (parser, kind, list);
18703 default ( shared | none ) */
18706 cp_parser_omp_clause_default (cp_parser *parser, tree list)
18708 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
18711 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18715 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18716 const char *p = IDENTIFIER_POINTER (id);
18721 if (strcmp ("none", p) != 0)
18723 kind = OMP_CLAUSE_DEFAULT_NONE;
18727 if (strcmp ("shared", p) != 0)
18729 kind = OMP_CLAUSE_DEFAULT_SHARED;
18736 cp_lexer_consume_token (parser->lexer);
18741 cp_parser_error (parser, "expected %<none%> or %<shared%>");
18744 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18745 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18746 /*or_comma=*/false,
18747 /*consume_paren=*/true);
18749 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
18752 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
18753 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
18754 OMP_CLAUSE_CHAIN (c) = list;
18755 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
18761 if ( expression ) */
18764 cp_parser_omp_clause_if (cp_parser *parser, tree list)
18768 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18771 t = cp_parser_condition (parser);
18773 if (t == error_mark_node
18774 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18775 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18776 /*or_comma=*/false,
18777 /*consume_paren=*/true);
18779 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
18781 c = build_omp_clause (OMP_CLAUSE_IF);
18782 OMP_CLAUSE_IF_EXPR (c) = t;
18783 OMP_CLAUSE_CHAIN (c) = list;
18792 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18796 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
18798 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
18799 OMP_CLAUSE_CHAIN (c) = list;
18804 num_threads ( expression ) */
18807 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
18811 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18814 t = cp_parser_expression (parser, false);
18816 if (t == error_mark_node
18817 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18818 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18819 /*or_comma=*/false,
18820 /*consume_paren=*/true);
18822 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
18824 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
18825 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
18826 OMP_CLAUSE_CHAIN (c) = list;
18835 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
18839 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
18841 c = build_omp_clause (OMP_CLAUSE_ORDERED);
18842 OMP_CLAUSE_CHAIN (c) = list;
18847 reduction ( reduction-operator : variable-list )
18849 reduction-operator:
18850 One of: + * - & ^ | && || */
18853 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
18855 enum tree_code code;
18858 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
18861 switch (cp_lexer_peek_token (parser->lexer)->type)
18873 code = BIT_AND_EXPR;
18876 code = BIT_XOR_EXPR;
18879 code = BIT_IOR_EXPR;
18882 code = TRUTH_ANDIF_EXPR;
18885 code = TRUTH_ORIF_EXPR;
18888 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
18890 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18891 /*or_comma=*/false,
18892 /*consume_paren=*/true);
18895 cp_lexer_consume_token (parser->lexer);
18897 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
18900 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
18901 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
18902 OMP_CLAUSE_REDUCTION_CODE (c) = code;
18908 schedule ( schedule-kind )
18909 schedule ( schedule-kind , expression )
18912 static | dynamic | guided | runtime */
18915 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
18919 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
18922 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
18924 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18926 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18927 const char *p = IDENTIFIER_POINTER (id);
18932 if (strcmp ("dynamic", p) != 0)
18934 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
18938 if (strcmp ("guided", p) != 0)
18940 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
18944 if (strcmp ("runtime", p) != 0)
18946 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
18953 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
18954 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
18957 cp_lexer_consume_token (parser->lexer);
18959 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18961 cp_lexer_consume_token (parser->lexer);
18963 t = cp_parser_assignment_expression (parser, false);
18965 if (t == error_mark_node)
18967 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
18968 error ("schedule %<runtime%> does not take "
18969 "a %<chunk_size%> parameter");
18971 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
18973 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
18976 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
18979 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
18980 OMP_CLAUSE_CHAIN (c) = list;
18984 cp_parser_error (parser, "invalid schedule kind");
18986 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
18987 /*or_comma=*/false,
18988 /*consume_paren=*/true);
18992 /* Parse all OpenMP clauses. The set clauses allowed by the directive
18993 is a bitmask in MASK. Return the list of clauses found; the result
18994 of clause default goes in *pdefault. */
18997 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
18998 const char *where, cp_token *pragma_tok)
19000 tree clauses = NULL;
19002 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19004 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19005 const char *c_name;
19006 tree prev = clauses;
19010 case PRAGMA_OMP_CLAUSE_COPYIN:
19011 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19014 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19015 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19017 c_name = "copyprivate";
19019 case PRAGMA_OMP_CLAUSE_DEFAULT:
19020 clauses = cp_parser_omp_clause_default (parser, clauses);
19021 c_name = "default";
19023 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19024 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19026 c_name = "firstprivate";
19028 case PRAGMA_OMP_CLAUSE_IF:
19029 clauses = cp_parser_omp_clause_if (parser, clauses);
19032 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19033 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19035 c_name = "lastprivate";
19037 case PRAGMA_OMP_CLAUSE_NOWAIT:
19038 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19041 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19042 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19043 c_name = "num_threads";
19045 case PRAGMA_OMP_CLAUSE_ORDERED:
19046 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19047 c_name = "ordered";
19049 case PRAGMA_OMP_CLAUSE_PRIVATE:
19050 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19052 c_name = "private";
19054 case PRAGMA_OMP_CLAUSE_REDUCTION:
19055 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19056 c_name = "reduction";
19058 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19059 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19060 c_name = "schedule";
19062 case PRAGMA_OMP_CLAUSE_SHARED:
19063 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19068 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19072 if (((mask >> c_kind) & 1) == 0)
19074 /* Remove the invalid clause(s) from the list to avoid
19075 confusing the rest of the compiler. */
19077 error ("%qs is not valid for %qs", c_name, where);
19081 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19082 return finish_omp_clauses (clauses);
19089 In practice, we're also interested in adding the statement to an
19090 outer node. So it is convenient if we work around the fact that
19091 cp_parser_statement calls add_stmt. */
19094 cp_parser_begin_omp_structured_block (cp_parser *parser)
19096 unsigned save = parser->in_statement;
19098 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19099 This preserves the "not within loop or switch" style error messages
19100 for nonsense cases like
19106 if (parser->in_statement)
19107 parser->in_statement = IN_OMP_BLOCK;
19113 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19115 parser->in_statement = save;
19119 cp_parser_omp_structured_block (cp_parser *parser)
19121 tree stmt = begin_omp_structured_block ();
19122 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19124 cp_parser_statement (parser, NULL_TREE, false, NULL);
19126 cp_parser_end_omp_structured_block (parser, save);
19127 return finish_omp_structured_block (stmt);
19131 # pragma omp atomic new-line
19135 x binop= expr | x++ | ++x | x-- | --x
19137 +, *, -, /, &, ^, |, <<, >>
19139 where x is an lvalue expression with scalar type. */
19142 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19145 enum tree_code code;
19147 cp_parser_require_pragma_eol (parser, pragma_tok);
19149 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19151 switch (TREE_CODE (lhs))
19156 case PREINCREMENT_EXPR:
19157 case POSTINCREMENT_EXPR:
19158 lhs = TREE_OPERAND (lhs, 0);
19160 rhs = integer_one_node;
19163 case PREDECREMENT_EXPR:
19164 case POSTDECREMENT_EXPR:
19165 lhs = TREE_OPERAND (lhs, 0);
19167 rhs = integer_one_node;
19171 switch (cp_lexer_peek_token (parser->lexer)->type)
19177 code = TRUNC_DIV_EXPR;
19185 case CPP_LSHIFT_EQ:
19186 code = LSHIFT_EXPR;
19188 case CPP_RSHIFT_EQ:
19189 code = RSHIFT_EXPR;
19192 code = BIT_AND_EXPR;
19195 code = BIT_IOR_EXPR;
19198 code = BIT_XOR_EXPR;
19201 cp_parser_error (parser,
19202 "invalid operator for %<#pragma omp atomic%>");
19205 cp_lexer_consume_token (parser->lexer);
19207 rhs = cp_parser_expression (parser, false);
19208 if (rhs == error_mark_node)
19212 finish_omp_atomic (code, lhs, rhs);
19213 cp_parser_consume_semicolon_at_end_of_statement (parser);
19217 cp_parser_skip_to_end_of_block_or_statement (parser);
19222 # pragma omp barrier new-line */
19225 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19227 cp_parser_require_pragma_eol (parser, pragma_tok);
19228 finish_omp_barrier ();
19232 # pragma omp critical [(name)] new-line
19233 structured-block */
19236 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19238 tree stmt, name = NULL;
19240 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19242 cp_lexer_consume_token (parser->lexer);
19244 name = cp_parser_identifier (parser);
19246 if (name == error_mark_node
19247 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19248 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19249 /*or_comma=*/false,
19250 /*consume_paren=*/true);
19251 if (name == error_mark_node)
19254 cp_parser_require_pragma_eol (parser, pragma_tok);
19256 stmt = cp_parser_omp_structured_block (parser);
19257 return c_finish_omp_critical (stmt, name);
19261 # pragma omp flush flush-vars[opt] new-line
19264 ( variable-list ) */
19267 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19269 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19270 (void) cp_parser_omp_var_list (parser, 0, NULL);
19271 cp_parser_require_pragma_eol (parser, pragma_tok);
19273 finish_omp_flush ();
19276 /* Parse the restricted form of the for statment allowed by OpenMP. */
19279 cp_parser_omp_for_loop (cp_parser *parser)
19281 tree init, cond, incr, body, decl, pre_body;
19284 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19286 cp_parser_error (parser, "for statement expected");
19289 loc = cp_lexer_consume_token (parser->lexer)->location;
19290 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19293 init = decl = NULL;
19294 pre_body = push_stmt_list ();
19295 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19297 cp_decl_specifier_seq type_specifiers;
19299 /* First, try to parse as an initialized declaration. See
19300 cp_parser_condition, from whence the bulk of this is copied. */
19302 cp_parser_parse_tentatively (parser);
19303 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19305 if (!cp_parser_error_occurred (parser))
19307 tree asm_specification, attributes;
19308 cp_declarator *declarator;
19310 declarator = cp_parser_declarator (parser,
19311 CP_PARSER_DECLARATOR_NAMED,
19312 /*ctor_dtor_or_conv_p=*/NULL,
19313 /*parenthesized_p=*/NULL,
19314 /*member_p=*/false);
19315 attributes = cp_parser_attributes_opt (parser);
19316 asm_specification = cp_parser_asm_specification_opt (parser);
19318 cp_parser_require (parser, CPP_EQ, "`='");
19319 if (cp_parser_parse_definitely (parser))
19323 decl = start_decl (declarator, &type_specifiers,
19324 /*initialized_p=*/false, attributes,
19325 /*prefix_attributes=*/NULL_TREE,
19328 init = cp_parser_assignment_expression (parser, false);
19330 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19331 asm_specification, LOOKUP_ONLYCONVERTING);
19334 pop_scope (pushed_scope);
19338 cp_parser_abort_tentative_parse (parser);
19340 /* If parsing as an initialized declaration failed, try again as
19341 a simple expression. */
19343 init = cp_parser_expression (parser, false);
19345 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19346 pre_body = pop_stmt_list (pre_body);
19349 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19350 cond = cp_parser_condition (parser);
19351 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19354 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19355 incr = cp_parser_expression (parser, false);
19357 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19358 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19359 /*or_comma=*/false,
19360 /*consume_paren=*/true);
19362 /* Note that we saved the original contents of this flag when we entered
19363 the structured block, and so we don't need to re-save it here. */
19364 parser->in_statement = IN_OMP_FOR;
19366 /* Note that the grammar doesn't call for a structured block here,
19367 though the loop as a whole is a structured block. */
19368 body = push_stmt_list ();
19369 cp_parser_statement (parser, NULL_TREE, false, NULL);
19370 body = pop_stmt_list (body);
19372 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19376 #pragma omp for for-clause[optseq] new-line
19379 #define OMP_FOR_CLAUSE_MASK \
19380 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19381 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19382 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19383 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19384 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19385 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19386 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19389 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19391 tree clauses, sb, ret;
19394 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19395 "#pragma omp for", pragma_tok);
19397 sb = begin_omp_structured_block ();
19398 save = cp_parser_begin_omp_structured_block (parser);
19400 ret = cp_parser_omp_for_loop (parser);
19402 OMP_FOR_CLAUSES (ret) = clauses;
19404 cp_parser_end_omp_structured_block (parser, save);
19405 add_stmt (finish_omp_structured_block (sb));
19411 # pragma omp master new-line
19412 structured-block */
19415 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19417 cp_parser_require_pragma_eol (parser, pragma_tok);
19418 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19422 # pragma omp ordered new-line
19423 structured-block */
19426 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19428 cp_parser_require_pragma_eol (parser, pragma_tok);
19429 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19435 { section-sequence }
19438 section-directive[opt] structured-block
19439 section-sequence section-directive structured-block */
19442 cp_parser_omp_sections_scope (cp_parser *parser)
19444 tree stmt, substmt;
19445 bool error_suppress = false;
19448 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19451 stmt = push_stmt_list ();
19453 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19457 substmt = begin_omp_structured_block ();
19458 save = cp_parser_begin_omp_structured_block (parser);
19462 cp_parser_statement (parser, NULL_TREE, false, NULL);
19464 tok = cp_lexer_peek_token (parser->lexer);
19465 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19467 if (tok->type == CPP_CLOSE_BRACE)
19469 if (tok->type == CPP_EOF)
19473 cp_parser_end_omp_structured_block (parser, save);
19474 substmt = finish_omp_structured_block (substmt);
19475 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19476 add_stmt (substmt);
19481 tok = cp_lexer_peek_token (parser->lexer);
19482 if (tok->type == CPP_CLOSE_BRACE)
19484 if (tok->type == CPP_EOF)
19487 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19489 cp_lexer_consume_token (parser->lexer);
19490 cp_parser_require_pragma_eol (parser, tok);
19491 error_suppress = false;
19493 else if (!error_suppress)
19495 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19496 error_suppress = true;
19499 substmt = cp_parser_omp_structured_block (parser);
19500 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19501 add_stmt (substmt);
19503 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19505 substmt = pop_stmt_list (stmt);
19507 stmt = make_node (OMP_SECTIONS);
19508 TREE_TYPE (stmt) = void_type_node;
19509 OMP_SECTIONS_BODY (stmt) = substmt;
19516 # pragma omp sections sections-clause[optseq] newline
19519 #define OMP_SECTIONS_CLAUSE_MASK \
19520 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19521 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19522 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19523 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19524 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19527 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19531 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19532 "#pragma omp sections", pragma_tok);
19534 ret = cp_parser_omp_sections_scope (parser);
19536 OMP_SECTIONS_CLAUSES (ret) = clauses;
19542 # pragma parallel parallel-clause new-line
19543 # pragma parallel for parallel-for-clause new-line
19544 # pragma parallel sections parallel-sections-clause new-line */
19546 #define OMP_PARALLEL_CLAUSE_MASK \
19547 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19548 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19549 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19550 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19551 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19552 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19553 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19554 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19557 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19559 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19560 const char *p_name = "#pragma omp parallel";
19561 tree stmt, clauses, par_clause, ws_clause, block;
19562 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19565 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19567 cp_lexer_consume_token (parser->lexer);
19568 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19569 p_name = "#pragma omp parallel for";
19570 mask |= OMP_FOR_CLAUSE_MASK;
19571 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19573 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19575 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19576 const char *p = IDENTIFIER_POINTER (id);
19577 if (strcmp (p, "sections") == 0)
19579 cp_lexer_consume_token (parser->lexer);
19580 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19581 p_name = "#pragma omp parallel sections";
19582 mask |= OMP_SECTIONS_CLAUSE_MASK;
19583 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19587 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19588 block = begin_omp_parallel ();
19589 save = cp_parser_begin_omp_structured_block (parser);
19593 case PRAGMA_OMP_PARALLEL:
19594 cp_parser_already_scoped_statement (parser);
19595 par_clause = clauses;
19598 case PRAGMA_OMP_PARALLEL_FOR:
19599 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19600 stmt = cp_parser_omp_for_loop (parser);
19602 OMP_FOR_CLAUSES (stmt) = ws_clause;
19605 case PRAGMA_OMP_PARALLEL_SECTIONS:
19606 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19607 stmt = cp_parser_omp_sections_scope (parser);
19609 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19613 gcc_unreachable ();
19616 cp_parser_end_omp_structured_block (parser, save);
19617 stmt = finish_omp_parallel (par_clause, block);
19618 if (p_kind != PRAGMA_OMP_PARALLEL)
19619 OMP_PARALLEL_COMBINED (stmt) = 1;
19624 # pragma omp single single-clause[optseq] new-line
19625 structured-block */
19627 #define OMP_SINGLE_CLAUSE_MASK \
19628 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19629 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19630 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19631 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19634 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19636 tree stmt = make_node (OMP_SINGLE);
19637 TREE_TYPE (stmt) = void_type_node;
19639 OMP_SINGLE_CLAUSES (stmt)
19640 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19641 "#pragma omp single", pragma_tok);
19642 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19644 return add_stmt (stmt);
19648 # pragma omp threadprivate (variable-list) */
19651 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
19655 vars = cp_parser_omp_var_list (parser, 0, NULL);
19656 cp_parser_require_pragma_eol (parser, pragma_tok);
19658 finish_omp_threadprivate (vars);
19661 /* Main entry point to OpenMP statement pragmas. */
19664 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
19668 switch (pragma_tok->pragma_kind)
19670 case PRAGMA_OMP_ATOMIC:
19671 cp_parser_omp_atomic (parser, pragma_tok);
19673 case PRAGMA_OMP_CRITICAL:
19674 stmt = cp_parser_omp_critical (parser, pragma_tok);
19676 case PRAGMA_OMP_FOR:
19677 stmt = cp_parser_omp_for (parser, pragma_tok);
19679 case PRAGMA_OMP_MASTER:
19680 stmt = cp_parser_omp_master (parser, pragma_tok);
19682 case PRAGMA_OMP_ORDERED:
19683 stmt = cp_parser_omp_ordered (parser, pragma_tok);
19685 case PRAGMA_OMP_PARALLEL:
19686 stmt = cp_parser_omp_parallel (parser, pragma_tok);
19688 case PRAGMA_OMP_SECTIONS:
19689 stmt = cp_parser_omp_sections (parser, pragma_tok);
19691 case PRAGMA_OMP_SINGLE:
19692 stmt = cp_parser_omp_single (parser, pragma_tok);
19695 gcc_unreachable ();
19699 SET_EXPR_LOCATION (stmt, pragma_tok->location);
19704 static GTY (()) cp_parser *the_parser;
19707 /* Special handling for the first token or line in the file. The first
19708 thing in the file might be #pragma GCC pch_preprocess, which loads a
19709 PCH file, which is a GC collection point. So we need to handle this
19710 first pragma without benefit of an existing lexer structure.
19712 Always returns one token to the caller in *FIRST_TOKEN. This is
19713 either the true first token of the file, or the first token after
19714 the initial pragma. */
19717 cp_parser_initial_pragma (cp_token *first_token)
19721 cp_lexer_get_preprocessor_token (NULL, first_token);
19722 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
19725 cp_lexer_get_preprocessor_token (NULL, first_token);
19726 if (first_token->type == CPP_STRING)
19728 name = first_token->u.value;
19730 cp_lexer_get_preprocessor_token (NULL, first_token);
19731 if (first_token->type != CPP_PRAGMA_EOL)
19732 error ("junk at end of %<#pragma GCC pch_preprocess%>");
19735 error ("expected string literal");
19737 /* Skip to the end of the pragma. */
19738 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
19739 cp_lexer_get_preprocessor_token (NULL, first_token);
19741 /* Now actually load the PCH file. */
19743 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
19745 /* Read one more token to return to our caller. We have to do this
19746 after reading the PCH file in, since its pointers have to be
19748 cp_lexer_get_preprocessor_token (NULL, first_token);
19751 /* Normal parsing of a pragma token. Here we can (and must) use the
19755 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
19757 cp_token *pragma_tok;
19760 pragma_tok = cp_lexer_consume_token (parser->lexer);
19761 gcc_assert (pragma_tok->type == CPP_PRAGMA);
19762 parser->lexer->in_pragma = true;
19764 id = pragma_tok->pragma_kind;
19767 case PRAGMA_GCC_PCH_PREPROCESS:
19768 error ("%<#pragma GCC pch_preprocess%> must be first");
19771 case PRAGMA_OMP_BARRIER:
19774 case pragma_compound:
19775 cp_parser_omp_barrier (parser, pragma_tok);
19778 error ("%<#pragma omp barrier%> may only be "
19779 "used in compound statements");
19786 case PRAGMA_OMP_FLUSH:
19789 case pragma_compound:
19790 cp_parser_omp_flush (parser, pragma_tok);
19793 error ("%<#pragma omp flush%> may only be "
19794 "used in compound statements");
19801 case PRAGMA_OMP_THREADPRIVATE:
19802 cp_parser_omp_threadprivate (parser, pragma_tok);
19805 case PRAGMA_OMP_ATOMIC:
19806 case PRAGMA_OMP_CRITICAL:
19807 case PRAGMA_OMP_FOR:
19808 case PRAGMA_OMP_MASTER:
19809 case PRAGMA_OMP_ORDERED:
19810 case PRAGMA_OMP_PARALLEL:
19811 case PRAGMA_OMP_SECTIONS:
19812 case PRAGMA_OMP_SINGLE:
19813 if (context == pragma_external)
19815 cp_parser_omp_construct (parser, pragma_tok);
19818 case PRAGMA_OMP_SECTION:
19819 error ("%<#pragma omp section%> may only be used in "
19820 "%<#pragma omp sections%> construct");
19824 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
19825 c_invoke_pragma_handler (id);
19829 cp_parser_error (parser, "expected declaration specifiers");
19833 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19837 /* The interface the pragma parsers have to the lexer. */
19840 pragma_lex (tree *value)
19843 enum cpp_ttype ret;
19845 tok = cp_lexer_peek_token (the_parser->lexer);
19848 *value = tok->u.value;
19850 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
19852 else if (ret == CPP_STRING)
19853 *value = cp_parser_string_literal (the_parser, false, false);
19856 cp_lexer_consume_token (the_parser->lexer);
19857 if (ret == CPP_KEYWORD)
19865 /* External interface. */
19867 /* Parse one entire translation unit. */
19870 c_parse_file (void)
19872 bool error_occurred;
19873 static bool already_called = false;
19875 if (already_called)
19877 sorry ("inter-module optimizations not implemented for C++");
19880 already_called = true;
19882 the_parser = cp_parser_new ();
19883 push_deferring_access_checks (flag_access_control
19884 ? dk_no_deferred : dk_no_check);
19885 error_occurred = cp_parser_translation_unit (the_parser);
19889 #include "gt-cp-parser.h"