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 *, bool);
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,
966 cp_declarator *declarator;
968 declarator = make_declarator (cdk_reference);
969 declarator->declarator = target;
970 declarator->u.reference.qualifiers = cv_qualifiers;
971 declarator->u.reference.rvalue_ref = rvalue_ref;
974 declarator->parameter_pack_p = target->parameter_pack_p;
975 target->parameter_pack_p = false;
978 declarator->parameter_pack_p = false;
983 /* Like make_pointer_declarator -- but for a pointer to a non-static
984 member of CLASS_TYPE. */
987 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
988 cp_declarator *pointee)
990 cp_declarator *declarator;
992 declarator = make_declarator (cdk_ptrmem);
993 declarator->declarator = pointee;
994 declarator->u.pointer.qualifiers = cv_qualifiers;
995 declarator->u.pointer.class_type = class_type;
999 declarator->parameter_pack_p = pointee->parameter_pack_p;
1000 pointee->parameter_pack_p = false;
1003 declarator->parameter_pack_p = false;
1008 /* Make a declarator for the function given by TARGET, with the
1009 indicated PARMS. The CV_QUALIFIERS aply to the function, as in
1010 "const"-qualified member function. The EXCEPTION_SPECIFICATION
1011 indicates what exceptions can be thrown. */
1014 make_call_declarator (cp_declarator *target,
1015 cp_parameter_declarator *parms,
1016 cp_cv_quals cv_qualifiers,
1017 tree exception_specification)
1019 cp_declarator *declarator;
1021 declarator = make_declarator (cdk_function);
1022 declarator->declarator = target;
1023 declarator->u.function.parameters = parms;
1024 declarator->u.function.qualifiers = cv_qualifiers;
1025 declarator->u.function.exception_specification = exception_specification;
1028 declarator->parameter_pack_p = target->parameter_pack_p;
1029 target->parameter_pack_p = false;
1032 declarator->parameter_pack_p = false;
1037 /* Make a declarator for an array of BOUNDS elements, each of which is
1038 defined by ELEMENT. */
1041 make_array_declarator (cp_declarator *element, tree bounds)
1043 cp_declarator *declarator;
1045 declarator = make_declarator (cdk_array);
1046 declarator->declarator = element;
1047 declarator->u.array.bounds = bounds;
1050 declarator->parameter_pack_p = element->parameter_pack_p;
1051 element->parameter_pack_p = false;
1054 declarator->parameter_pack_p = false;
1059 /* Determine whether the declarator we've seen so far can be a
1060 parameter pack, when followed by an ellipsis. */
1062 declarator_can_be_parameter_pack (cp_declarator *declarator)
1064 /* Search for a declarator name, or any other declarator that goes
1065 after the point where the ellipsis could appear in a parameter
1066 pack. If we find any of these, then this declarator can not be
1067 made into a parameter pack. */
1069 while (declarator && !found)
1071 switch ((int)declarator->kind)
1081 declarator = declarator->declarator;
1089 cp_parameter_declarator *no_parameters;
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092 DECLARATOR and DEFAULT_ARGUMENT. */
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096 cp_declarator *declarator,
1097 tree default_argument)
1099 cp_parameter_declarator *parameter;
1101 parameter = ((cp_parameter_declarator *)
1102 alloc_declarator (sizeof (cp_parameter_declarator)));
1103 parameter->next = NULL;
1104 if (decl_specifiers)
1105 parameter->decl_specifiers = *decl_specifiers;
1107 clear_decl_specs (¶meter->decl_specifiers);
1108 parameter->declarator = declarator;
1109 parameter->default_argument = default_argument;
1110 parameter->ellipsis_p = false;
1115 /* Returns true iff DECLARATOR is a declaration for a function. */
1118 function_declarator_p (const cp_declarator *declarator)
1122 if (declarator->kind == cdk_function
1123 && declarator->declarator->kind == cdk_id)
1125 if (declarator->kind == cdk_id
1126 || declarator->kind == cdk_error)
1128 declarator = declarator->declarator;
1138 A cp_parser parses the token stream as specified by the C++
1139 grammar. Its job is purely parsing, not semantic analysis. For
1140 example, the parser breaks the token stream into declarators,
1141 expressions, statements, and other similar syntactic constructs.
1142 It does not check that the types of the expressions on either side
1143 of an assignment-statement are compatible, or that a function is
1144 not declared with a parameter of type `void'.
1146 The parser invokes routines elsewhere in the compiler to perform
1147 semantic analysis and to build up the abstract syntax tree for the
1150 The parser (and the template instantiation code, which is, in a
1151 way, a close relative of parsing) are the only parts of the
1152 compiler that should be calling push_scope and pop_scope, or
1153 related functions. The parser (and template instantiation code)
1154 keeps track of what scope is presently active; everything else
1155 should simply honor that. (The code that generates static
1156 initializers may also need to set the scope, in order to check
1157 access control correctly when emitting the initializers.)
1162 The parser is of the standard recursive-descent variety. Upcoming
1163 tokens in the token stream are examined in order to determine which
1164 production to use when parsing a non-terminal. Some C++ constructs
1165 require arbitrary look ahead to disambiguate. For example, it is
1166 impossible, in the general case, to tell whether a statement is an
1167 expression or declaration without scanning the entire statement.
1168 Therefore, the parser is capable of "parsing tentatively." When the
1169 parser is not sure what construct comes next, it enters this mode.
1170 Then, while we attempt to parse the construct, the parser queues up
1171 error messages, rather than issuing them immediately, and saves the
1172 tokens it consumes. If the construct is parsed successfully, the
1173 parser "commits", i.e., it issues any queued error messages and
1174 the tokens that were being preserved are permanently discarded.
1175 If, however, the construct is not parsed successfully, the parser
1176 rolls back its state completely so that it can resume parsing using
1177 a different alternative.
1182 The performance of the parser could probably be improved substantially.
1183 We could often eliminate the need to parse tentatively by looking ahead
1184 a little bit. In some places, this approach might not entirely eliminate
1185 the need to parse tentatively, but it might still speed up the average
1188 /* Flags that are passed to some parsing functions. These values can
1189 be bitwise-ored together. */
1191 typedef enum cp_parser_flags
1194 CP_PARSER_FLAGS_NONE = 0x0,
1195 /* The construct is optional. If it is not present, then no error
1196 should be issued. */
1197 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198 /* When parsing a type-specifier, do not allow user-defined types. */
1199 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1202 /* The different kinds of declarators we want to parse. */
1204 typedef enum cp_parser_declarator_kind
1206 /* We want an abstract declarator. */
1207 CP_PARSER_DECLARATOR_ABSTRACT,
1208 /* We want a named declarator. */
1209 CP_PARSER_DECLARATOR_NAMED,
1210 /* We don't mind, but the name must be an unqualified-id. */
1211 CP_PARSER_DECLARATOR_EITHER
1212 } cp_parser_declarator_kind;
1214 /* The precedence values used to parse binary expressions. The minimum value
1215 of PREC must be 1, because zero is reserved to quickly discriminate
1216 binary operators from other tokens. */
1221 PREC_LOGICAL_OR_EXPRESSION,
1222 PREC_LOGICAL_AND_EXPRESSION,
1223 PREC_INCLUSIVE_OR_EXPRESSION,
1224 PREC_EXCLUSIVE_OR_EXPRESSION,
1225 PREC_AND_EXPRESSION,
1226 PREC_EQUALITY_EXPRESSION,
1227 PREC_RELATIONAL_EXPRESSION,
1228 PREC_SHIFT_EXPRESSION,
1229 PREC_ADDITIVE_EXPRESSION,
1230 PREC_MULTIPLICATIVE_EXPRESSION,
1232 NUM_PREC_VALUES = PREC_PM_EXPRESSION
1235 /* A mapping from a token type to a corresponding tree node type, with a
1236 precedence value. */
1238 typedef struct cp_parser_binary_operations_map_node
1240 /* The token type. */
1241 enum cpp_ttype token_type;
1242 /* The corresponding tree code. */
1243 enum tree_code tree_type;
1244 /* The precedence of this operator. */
1245 enum cp_parser_prec prec;
1246 } cp_parser_binary_operations_map_node;
1248 /* The status of a tentative parse. */
1250 typedef enum cp_parser_status_kind
1252 /* No errors have occurred. */
1253 CP_PARSER_STATUS_KIND_NO_ERROR,
1254 /* An error has occurred. */
1255 CP_PARSER_STATUS_KIND_ERROR,
1256 /* We are committed to this tentative parse, whether or not an error
1258 CP_PARSER_STATUS_KIND_COMMITTED
1259 } cp_parser_status_kind;
1261 typedef struct cp_parser_expression_stack_entry
1263 /* Left hand side of the binary operation we are currently
1266 /* Original tree code for left hand side, if it was a binary
1267 expression itself (used for -Wparentheses). */
1268 enum tree_code lhs_type;
1269 /* Tree code for the binary operation we are parsing. */
1270 enum tree_code tree_type;
1271 /* Precedence of the binary operation we are parsing. */
1273 } cp_parser_expression_stack_entry;
1275 /* The stack for storing partial expressions. We only need NUM_PREC_VALUES
1276 entries because precedence levels on the stack are monotonically
1278 typedef struct cp_parser_expression_stack_entry
1279 cp_parser_expression_stack[NUM_PREC_VALUES];
1281 /* Context that is saved and restored when parsing tentatively. */
1282 typedef struct cp_parser_context GTY (())
1284 /* If this is a tentative parsing context, the status of the
1286 enum cp_parser_status_kind status;
1287 /* If non-NULL, we have just seen a `x->' or `x.' expression. Names
1288 that are looked up in this context must be looked up both in the
1289 scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1290 the context of the containing expression. */
1293 /* The next parsing context in the stack. */
1294 struct cp_parser_context *next;
1295 } cp_parser_context;
1299 /* Constructors and destructors. */
1301 static cp_parser_context *cp_parser_context_new
1302 (cp_parser_context *);
1304 /* Class variables. */
1306 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1308 /* The operator-precedence table used by cp_parser_binary_expression.
1309 Transformed into an associative array (binops_by_token) by
1312 static const cp_parser_binary_operations_map_node binops[] = {
1313 { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1314 { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1316 { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1317 { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1318 { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1320 { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1321 { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1323 { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1324 { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1326 { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1327 { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1328 { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1329 { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1331 { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1332 { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1334 { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1336 { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1338 { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1340 { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1342 { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1345 /* The same as binops, but initialized by cp_parser_new so that
1346 binops_by_token[N].token_type == N. Used in cp_parser_binary_expression
1348 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1350 /* Constructors and destructors. */
1352 /* Construct a new context. The context below this one on the stack
1353 is given by NEXT. */
1355 static cp_parser_context *
1356 cp_parser_context_new (cp_parser_context* next)
1358 cp_parser_context *context;
1360 /* Allocate the storage. */
1361 if (cp_parser_context_free_list != NULL)
1363 /* Pull the first entry from the free list. */
1364 context = cp_parser_context_free_list;
1365 cp_parser_context_free_list = context->next;
1366 memset (context, 0, sizeof (*context));
1369 context = GGC_CNEW (cp_parser_context);
1371 /* No errors have occurred yet in this context. */
1372 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1373 /* If this is not the bottomost context, copy information that we
1374 need from the previous context. */
1377 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1378 expression, then we are parsing one in this context, too. */
1379 context->object_type = next->object_type;
1380 /* Thread the stack. */
1381 context->next = next;
1387 /* The cp_parser structure represents the C++ parser. */
1389 typedef struct cp_parser GTY(())
1391 /* The lexer from which we are obtaining tokens. */
1394 /* The scope in which names should be looked up. If NULL_TREE, then
1395 we look up names in the scope that is currently open in the
1396 source program. If non-NULL, this is either a TYPE or
1397 NAMESPACE_DECL for the scope in which we should look. It can
1398 also be ERROR_MARK, when we've parsed a bogus scope.
1400 This value is not cleared automatically after a name is looked
1401 up, so we must be careful to clear it before starting a new look
1402 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1403 will look up `Z' in the scope of `X', rather than the current
1404 scope.) Unfortunately, it is difficult to tell when name lookup
1405 is complete, because we sometimes peek at a token, look it up,
1406 and then decide not to consume it. */
1409 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1410 last lookup took place. OBJECT_SCOPE is used if an expression
1411 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1412 respectively. QUALIFYING_SCOPE is used for an expression of the
1413 form "X::Y"; it refers to X. */
1415 tree qualifying_scope;
1417 /* A stack of parsing contexts. All but the bottom entry on the
1418 stack will be tentative contexts.
1420 We parse tentatively in order to determine which construct is in
1421 use in some situations. For example, in order to determine
1422 whether a statement is an expression-statement or a
1423 declaration-statement we parse it tentatively as a
1424 declaration-statement. If that fails, we then reparse the same
1425 token stream as an expression-statement. */
1426 cp_parser_context *context;
1428 /* True if we are parsing GNU C++. If this flag is not set, then
1429 GNU extensions are not recognized. */
1430 bool allow_gnu_extensions_p;
1432 /* TRUE if the `>' token should be interpreted as the greater-than
1433 operator. FALSE if it is the end of a template-id or
1434 template-parameter-list. In C++0x mode, this flag also applies to
1435 `>>' tokens, which are viewed as two consecutive `>' tokens when
1436 this flag is FALSE. */
1437 bool greater_than_is_operator_p;
1439 /* TRUE if default arguments are allowed within a parameter list
1440 that starts at this point. FALSE if only a gnu extension makes
1441 them permissible. */
1442 bool default_arg_ok_p;
1444 /* TRUE if we are parsing an integral constant-expression. See
1445 [expr.const] for a precise definition. */
1446 bool integral_constant_expression_p;
1448 /* TRUE if we are parsing an integral constant-expression -- but a
1449 non-constant expression should be permitted as well. This flag
1450 is used when parsing an array bound so that GNU variable-length
1451 arrays are tolerated. */
1452 bool allow_non_integral_constant_expression_p;
1454 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1455 been seen that makes the expression non-constant. */
1456 bool non_integral_constant_expression_p;
1458 /* TRUE if local variable names and `this' are forbidden in the
1460 bool local_variables_forbidden_p;
1462 /* TRUE if the declaration we are parsing is part of a
1463 linkage-specification of the form `extern string-literal
1465 bool in_unbraced_linkage_specification_p;
1467 /* TRUE if we are presently parsing a declarator, after the
1468 direct-declarator. */
1469 bool in_declarator_p;
1471 /* TRUE if we are presently parsing a template-argument-list. */
1472 bool in_template_argument_list_p;
1474 /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1475 to IN_OMP_BLOCK if parsing OpenMP structured block and
1476 IN_OMP_FOR if parsing OpenMP loop. If parsing a switch statement,
1477 this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1478 iteration-statement, OpenMP block or loop within that switch. */
1479 #define IN_SWITCH_STMT 1
1480 #define IN_ITERATION_STMT 2
1481 #define IN_OMP_BLOCK 4
1482 #define IN_OMP_FOR 8
1483 #define IN_IF_STMT 16
1484 unsigned char in_statement;
1486 /* TRUE if we are presently parsing the body of a switch statement.
1487 Note that this doesn't quite overlap with in_statement above.
1488 The difference relates to giving the right sets of error messages:
1489 "case not in switch" vs "break statement used with OpenMP...". */
1490 bool in_switch_statement_p;
1492 /* TRUE if we are parsing a type-id in an expression context. In
1493 such a situation, both "type (expr)" and "type (type)" are valid
1495 bool in_type_id_in_expr_p;
1497 /* TRUE if we are currently in a header file where declarations are
1498 implicitly extern "C". */
1499 bool implicit_extern_c;
1501 /* TRUE if strings in expressions should be translated to the execution
1503 bool translate_strings_p;
1505 /* TRUE if we are presently parsing the body of a function, but not
1507 bool in_function_body;
1509 /* If non-NULL, then we are parsing a construct where new type
1510 definitions are not permitted. The string stored here will be
1511 issued as an error message if a type is defined. */
1512 const char *type_definition_forbidden_message;
1514 /* A list of lists. The outer list is a stack, used for member
1515 functions of local classes. At each level there are two sub-list,
1516 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1517 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1518 TREE_VALUE's. The functions are chained in reverse declaration
1521 The TREE_PURPOSE sublist contains those functions with default
1522 arguments that need post processing, and the TREE_VALUE sublist
1523 contains those functions with definitions that need post
1526 These lists can only be processed once the outermost class being
1527 defined is complete. */
1528 tree unparsed_functions_queues;
1530 /* The number of classes whose definitions are currently in
1532 unsigned num_classes_being_defined;
1534 /* The number of template parameter lists that apply directly to the
1535 current declaration. */
1536 unsigned num_template_parameter_lists;
1541 /* Constructors and destructors. */
1543 static cp_parser *cp_parser_new
1546 /* Routines to parse various constructs.
1548 Those that return `tree' will return the error_mark_node (rather
1549 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1550 Sometimes, they will return an ordinary node if error-recovery was
1551 attempted, even though a parse error occurred. So, to check
1552 whether or not a parse error occurred, you should always use
1553 cp_parser_error_occurred. If the construct is optional (indicated
1554 either by an `_opt' in the name of the function that does the
1555 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1556 the construct is not present. */
1558 /* Lexical conventions [gram.lex] */
1560 static tree cp_parser_identifier
1562 static tree cp_parser_string_literal
1563 (cp_parser *, bool, bool);
1565 /* Basic concepts [gram.basic] */
1567 static bool cp_parser_translation_unit
1570 /* Expressions [gram.expr] */
1572 static tree cp_parser_primary_expression
1573 (cp_parser *, bool, bool, bool, cp_id_kind *);
1574 static tree cp_parser_id_expression
1575 (cp_parser *, bool, bool, bool *, bool, bool);
1576 static tree cp_parser_unqualified_id
1577 (cp_parser *, bool, bool, bool, bool);
1578 static tree cp_parser_nested_name_specifier_opt
1579 (cp_parser *, bool, bool, bool, bool);
1580 static tree cp_parser_nested_name_specifier
1581 (cp_parser *, bool, bool, bool, bool);
1582 static tree cp_parser_class_or_namespace_name
1583 (cp_parser *, bool, bool, bool, bool, bool);
1584 static tree cp_parser_postfix_expression
1585 (cp_parser *, bool, bool);
1586 static tree cp_parser_postfix_open_square_expression
1587 (cp_parser *, tree, bool);
1588 static tree cp_parser_postfix_dot_deref_expression
1589 (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1590 static tree cp_parser_parenthesized_expression_list
1591 (cp_parser *, bool, bool, bool, bool *);
1592 static void cp_parser_pseudo_destructor_name
1593 (cp_parser *, tree *, tree *);
1594 static tree cp_parser_unary_expression
1595 (cp_parser *, bool, bool);
1596 static enum tree_code cp_parser_unary_operator
1598 static tree cp_parser_new_expression
1600 static tree cp_parser_new_placement
1602 static tree cp_parser_new_type_id
1603 (cp_parser *, tree *);
1604 static cp_declarator *cp_parser_new_declarator_opt
1606 static cp_declarator *cp_parser_direct_new_declarator
1608 static tree cp_parser_new_initializer
1610 static tree cp_parser_delete_expression
1612 static tree cp_parser_cast_expression
1613 (cp_parser *, bool, bool);
1614 static tree cp_parser_binary_expression
1615 (cp_parser *, bool);
1616 static tree cp_parser_question_colon_clause
1617 (cp_parser *, tree);
1618 static tree cp_parser_assignment_expression
1619 (cp_parser *, bool);
1620 static enum tree_code cp_parser_assignment_operator_opt
1622 static tree cp_parser_expression
1623 (cp_parser *, bool);
1624 static tree cp_parser_constant_expression
1625 (cp_parser *, bool, bool *);
1626 static tree cp_parser_builtin_offsetof
1629 /* Statements [gram.stmt.stmt] */
1631 static void cp_parser_statement
1632 (cp_parser *, tree, bool, bool *);
1633 static void cp_parser_label_for_labeled_statement
1635 static tree cp_parser_expression_statement
1636 (cp_parser *, tree);
1637 static tree cp_parser_compound_statement
1638 (cp_parser *, tree, bool);
1639 static void cp_parser_statement_seq_opt
1640 (cp_parser *, tree);
1641 static tree cp_parser_selection_statement
1642 (cp_parser *, bool *);
1643 static tree cp_parser_condition
1645 static tree cp_parser_iteration_statement
1647 static void cp_parser_for_init_statement
1649 static tree cp_parser_jump_statement
1651 static void cp_parser_declaration_statement
1654 static tree cp_parser_implicitly_scoped_statement
1655 (cp_parser *, bool *);
1656 static void cp_parser_already_scoped_statement
1659 /* Declarations [gram.dcl.dcl] */
1661 static void cp_parser_declaration_seq_opt
1663 static void cp_parser_declaration
1665 static void cp_parser_block_declaration
1666 (cp_parser *, bool);
1667 static void cp_parser_simple_declaration
1668 (cp_parser *, bool);
1669 static void cp_parser_decl_specifier_seq
1670 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1671 static tree cp_parser_storage_class_specifier_opt
1673 static tree cp_parser_function_specifier_opt
1674 (cp_parser *, cp_decl_specifier_seq *);
1675 static tree cp_parser_type_specifier
1676 (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1678 static tree cp_parser_simple_type_specifier
1679 (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1680 static tree cp_parser_type_name
1682 static tree cp_parser_elaborated_type_specifier
1683 (cp_parser *, bool, bool);
1684 static tree cp_parser_enum_specifier
1686 static void cp_parser_enumerator_list
1687 (cp_parser *, tree);
1688 static void cp_parser_enumerator_definition
1689 (cp_parser *, tree);
1690 static tree cp_parser_namespace_name
1692 static void cp_parser_namespace_definition
1694 static void cp_parser_namespace_body
1696 static tree cp_parser_qualified_namespace_specifier
1698 static void cp_parser_namespace_alias_definition
1700 static bool cp_parser_using_declaration
1701 (cp_parser *, bool);
1702 static void cp_parser_using_directive
1704 static void cp_parser_asm_definition
1706 static void cp_parser_linkage_specification
1708 static void cp_parser_static_assert
1709 (cp_parser *, bool);
1711 /* Declarators [gram.dcl.decl] */
1713 static tree cp_parser_init_declarator
1714 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1715 static cp_declarator *cp_parser_declarator
1716 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1717 static cp_declarator *cp_parser_direct_declarator
1718 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1719 static enum tree_code cp_parser_ptr_operator
1720 (cp_parser *, tree *, cp_cv_quals *);
1721 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1723 static tree cp_parser_declarator_id
1724 (cp_parser *, bool);
1725 static tree cp_parser_type_id
1727 static void cp_parser_type_specifier_seq
1728 (cp_parser *, bool, cp_decl_specifier_seq *);
1729 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1731 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1732 (cp_parser *, bool *);
1733 static cp_parameter_declarator *cp_parser_parameter_declaration
1734 (cp_parser *, bool, bool *);
1735 static void cp_parser_function_body
1737 static tree cp_parser_initializer
1738 (cp_parser *, bool *, bool *);
1739 static tree cp_parser_initializer_clause
1740 (cp_parser *, bool *);
1741 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1742 (cp_parser *, bool *);
1744 static bool cp_parser_ctor_initializer_opt_and_function_body
1747 /* Classes [gram.class] */
1749 static tree cp_parser_class_name
1750 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1751 static tree cp_parser_class_specifier
1753 static tree cp_parser_class_head
1754 (cp_parser *, bool *, tree *, tree *);
1755 static enum tag_types cp_parser_class_key
1757 static void cp_parser_member_specification_opt
1759 static void cp_parser_member_declaration
1761 static tree cp_parser_pure_specifier
1763 static tree cp_parser_constant_initializer
1766 /* Derived classes [gram.class.derived] */
1768 static tree cp_parser_base_clause
1770 static tree cp_parser_base_specifier
1773 /* Special member functions [gram.special] */
1775 static tree cp_parser_conversion_function_id
1777 static tree cp_parser_conversion_type_id
1779 static cp_declarator *cp_parser_conversion_declarator_opt
1781 static bool cp_parser_ctor_initializer_opt
1783 static void cp_parser_mem_initializer_list
1785 static tree cp_parser_mem_initializer
1787 static tree cp_parser_mem_initializer_id
1790 /* Overloading [gram.over] */
1792 static tree cp_parser_operator_function_id
1794 static tree cp_parser_operator
1797 /* Templates [gram.temp] */
1799 static void cp_parser_template_declaration
1800 (cp_parser *, bool);
1801 static tree cp_parser_template_parameter_list
1803 static tree cp_parser_template_parameter
1804 (cp_parser *, bool *, bool *);
1805 static tree cp_parser_type_parameter
1806 (cp_parser *, bool *);
1807 static tree cp_parser_template_id
1808 (cp_parser *, bool, bool, bool);
1809 static tree cp_parser_template_name
1810 (cp_parser *, bool, bool, bool, bool *);
1811 static tree cp_parser_template_argument_list
1813 static tree cp_parser_template_argument
1815 static void cp_parser_explicit_instantiation
1817 static void cp_parser_explicit_specialization
1820 /* Exception handling [gram.exception] */
1822 static tree cp_parser_try_block
1824 static bool cp_parser_function_try_block
1826 static void cp_parser_handler_seq
1828 static void cp_parser_handler
1830 static tree cp_parser_exception_declaration
1832 static tree cp_parser_throw_expression
1834 static tree cp_parser_exception_specification_opt
1836 static tree cp_parser_type_id_list
1839 /* GNU Extensions */
1841 static tree cp_parser_asm_specification_opt
1843 static tree cp_parser_asm_operand_list
1845 static tree cp_parser_asm_clobber_list
1847 static tree cp_parser_attributes_opt
1849 static tree cp_parser_attribute_list
1851 static bool cp_parser_extension_opt
1852 (cp_parser *, int *);
1853 static void cp_parser_label_declaration
1856 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1857 static bool cp_parser_pragma
1858 (cp_parser *, enum pragma_context);
1860 /* Objective-C++ Productions */
1862 static tree cp_parser_objc_message_receiver
1864 static tree cp_parser_objc_message_args
1866 static tree cp_parser_objc_message_expression
1868 static tree cp_parser_objc_encode_expression
1870 static tree cp_parser_objc_defs_expression
1872 static tree cp_parser_objc_protocol_expression
1874 static tree cp_parser_objc_selector_expression
1876 static tree cp_parser_objc_expression
1878 static bool cp_parser_objc_selector_p
1880 static tree cp_parser_objc_selector
1882 static tree cp_parser_objc_protocol_refs_opt
1884 static void cp_parser_objc_declaration
1886 static tree cp_parser_objc_statement
1889 /* Utility Routines */
1891 static tree cp_parser_lookup_name
1892 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1893 static tree cp_parser_lookup_name_simple
1894 (cp_parser *, tree);
1895 static tree cp_parser_maybe_treat_template_as_class
1897 static bool cp_parser_check_declarator_template_parameters
1898 (cp_parser *, cp_declarator *);
1899 static bool cp_parser_check_template_parameters
1900 (cp_parser *, unsigned);
1901 static tree cp_parser_simple_cast_expression
1903 static tree cp_parser_global_scope_opt
1904 (cp_parser *, bool);
1905 static bool cp_parser_constructor_declarator_p
1906 (cp_parser *, bool);
1907 static tree cp_parser_function_definition_from_specifiers_and_declarator
1908 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1909 static tree cp_parser_function_definition_after_declarator
1910 (cp_parser *, bool);
1911 static void cp_parser_template_declaration_after_export
1912 (cp_parser *, bool);
1913 static void cp_parser_perform_template_parameter_access_checks
1914 (VEC (deferred_access_check,gc)*);
1915 static tree cp_parser_single_declaration
1916 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1917 static tree cp_parser_functional_cast
1918 (cp_parser *, tree);
1919 static tree cp_parser_save_member_function_body
1920 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1921 static tree cp_parser_enclosed_template_argument_list
1923 static void cp_parser_save_default_args
1924 (cp_parser *, tree);
1925 static void cp_parser_late_parsing_for_member
1926 (cp_parser *, tree);
1927 static void cp_parser_late_parsing_default_args
1928 (cp_parser *, tree);
1929 static tree cp_parser_sizeof_operand
1930 (cp_parser *, enum rid);
1931 static tree cp_parser_trait_expr
1932 (cp_parser *, enum rid);
1933 static bool cp_parser_declares_only_class_p
1935 static void cp_parser_set_storage_class
1936 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1937 static void cp_parser_set_decl_spec_type
1938 (cp_decl_specifier_seq *, tree, bool);
1939 static bool cp_parser_friend_p
1940 (const cp_decl_specifier_seq *);
1941 static cp_token *cp_parser_require
1942 (cp_parser *, enum cpp_ttype, const char *);
1943 static cp_token *cp_parser_require_keyword
1944 (cp_parser *, enum rid, const char *);
1945 static bool cp_parser_token_starts_function_definition_p
1947 static bool cp_parser_next_token_starts_class_definition_p
1949 static bool cp_parser_next_token_ends_template_argument_p
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952 (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1955 static void cp_parser_check_class_key
1956 (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1959 static bool cp_parser_optional_template_keyword
1961 static void cp_parser_pre_parsed_nested_name_specifier
1963 static void cp_parser_cache_group
1964 (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1967 static void cp_parser_commit_to_tentative_parse
1969 static void cp_parser_abort_tentative_parse
1971 static bool cp_parser_parse_definitely
1973 static inline bool cp_parser_parsing_tentatively
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1977 static void cp_parser_error
1978 (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980 (cp_parser *, tree, tree, const char *);
1981 static bool cp_parser_simulate_error
1983 static bool cp_parser_check_type_definition
1985 static void cp_parser_check_for_definition_in_return_type
1986 (cp_declarator *, tree);
1987 static void cp_parser_check_for_invalid_template_id
1988 (cp_parser *, tree);
1989 static bool cp_parser_non_integral_constant_expression
1990 (cp_parser *, const char *);
1991 static void cp_parser_diagnose_invalid_type_name
1992 (cp_parser *, tree, tree);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1995 static int cp_parser_skip_to_closing_parenthesis
1996 (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2001 static void cp_parser_skip_to_end_of_block_or_statement
2003 static bool cp_parser_skip_to_closing_brace
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2007 static void cp_parser_skip_to_pragma_eol
2008 (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2011 static bool cp_parser_allow_gnu_extensions_p
2013 static bool cp_parser_is_string_literal
2015 static bool cp_parser_is_keyword
2016 (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018 (cp_parser *, tree, tree);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2022 /* Returns nonzero if we are parsing tentatively. */
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2027 return parser->context->next != NULL;
2030 /* Returns nonzero if TOKEN is a string literal. */
2033 cp_parser_is_string_literal (cp_token* token)
2035 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2038 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2041 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2043 return token->keyword == keyword;
2046 /* If not parsing tentatively, issue a diagnostic of the form
2047 FILE:LINE: MESSAGE before TOKEN
2048 where TOKEN is the next token in the input stream. MESSAGE
2049 (specified by the caller) is usually of the form "expected
2053 cp_parser_error (cp_parser* parser, const char* message)
2055 if (!cp_parser_simulate_error (parser))
2057 cp_token *token = cp_lexer_peek_token (parser->lexer);
2058 /* This diagnostic makes more sense if it is tagged to the line
2059 of the token we just peeked at. */
2060 cp_lexer_set_source_position_from_token (token);
2062 if (token->type == CPP_PRAGMA)
2064 error ("%<#pragma%> is not allowed here");
2065 cp_parser_skip_to_pragma_eol (parser, token);
2069 c_parse_error (message,
2070 /* Because c_parser_error does not understand
2071 CPP_KEYWORD, keywords are treated like
2073 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2078 /* Issue an error about name-lookup failing. NAME is the
2079 IDENTIFIER_NODE DECL is the result of
2080 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2081 the thing that we hoped to find. */
2084 cp_parser_name_lookup_error (cp_parser* parser,
2087 const char* desired)
2089 /* If name lookup completely failed, tell the user that NAME was not
2091 if (decl == error_mark_node)
2093 if (parser->scope && parser->scope != global_namespace)
2094 error ("%<%E::%E%> has not been declared",
2095 parser->scope, name);
2096 else if (parser->scope == global_namespace)
2097 error ("%<::%E%> has not been declared", name);
2098 else if (parser->object_scope
2099 && !CLASS_TYPE_P (parser->object_scope))
2100 error ("request for member %qE in non-class type %qT",
2101 name, parser->object_scope);
2102 else if (parser->object_scope)
2103 error ("%<%T::%E%> has not been declared",
2104 parser->object_scope, name);
2106 error ("%qE has not been declared", name);
2108 else if (parser->scope && parser->scope != global_namespace)
2109 error ("%<%E::%E%> %s", parser->scope, name, desired);
2110 else if (parser->scope == global_namespace)
2111 error ("%<::%E%> %s", name, desired);
2113 error ("%qE %s", name, desired);
2116 /* If we are parsing tentatively, remember that an error has occurred
2117 during this tentative parse. Returns true if the error was
2118 simulated; false if a message should be issued by the caller. */
2121 cp_parser_simulate_error (cp_parser* parser)
2123 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2125 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2131 /* Check for repeated decl-specifiers. */
2134 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2138 for (ds = ds_first; ds != ds_last; ++ds)
2140 unsigned count = decl_specs->specs[(int)ds];
2143 /* The "long" specifier is a special case because of "long long". */
2147 error ("%<long long long%> is too long for GCC");
2148 else if (pedantic && !in_system_header && warn_long_long)
2149 pedwarn ("ISO C++ does not support %<long long%>");
2153 static const char *const decl_spec_names[] = {
2169 error ("duplicate %qs", decl_spec_names[(int)ds]);
2174 /* This function is called when a type is defined. If type
2175 definitions are forbidden at this point, an error message is
2179 cp_parser_check_type_definition (cp_parser* parser)
2181 /* If types are forbidden here, issue a message. */
2182 if (parser->type_definition_forbidden_message)
2184 /* Use `%s' to print the string in case there are any escape
2185 characters in the message. */
2186 error ("%s", parser->type_definition_forbidden_message);
2192 /* This function is called when the DECLARATOR is processed. The TYPE
2193 was a type defined in the decl-specifiers. If it is invalid to
2194 define a type in the decl-specifiers for DECLARATOR, an error is
2198 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2201 /* [dcl.fct] forbids type definitions in return types.
2202 Unfortunately, it's not easy to know whether or not we are
2203 processing a return type until after the fact. */
2205 && (declarator->kind == cdk_pointer
2206 || declarator->kind == cdk_reference
2207 || declarator->kind == cdk_ptrmem))
2208 declarator = declarator->declarator;
2210 && declarator->kind == cdk_function)
2212 error ("new types may not be defined in a return type");
2213 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2218 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2219 "<" in any valid C++ program. If the next token is indeed "<",
2220 issue a message warning the user about what appears to be an
2221 invalid attempt to form a template-id. */
2224 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2227 cp_token_position start = 0;
2229 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2232 error ("%qT is not a template", type);
2233 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2234 error ("%qE is not a template", type);
2236 error ("invalid template-id");
2237 /* Remember the location of the invalid "<". */
2238 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2239 start = cp_lexer_token_position (parser->lexer, true);
2240 /* Consume the "<". */
2241 cp_lexer_consume_token (parser->lexer);
2242 /* Parse the template arguments. */
2243 cp_parser_enclosed_template_argument_list (parser);
2244 /* Permanently remove the invalid template arguments so that
2245 this error message is not issued again. */
2247 cp_lexer_purge_tokens_after (parser->lexer, start);
2251 /* If parsing an integral constant-expression, issue an error message
2252 about the fact that THING appeared and return true. Otherwise,
2253 return false. In either case, set
2254 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2257 cp_parser_non_integral_constant_expression (cp_parser *parser,
2260 parser->non_integral_constant_expression_p = true;
2261 if (parser->integral_constant_expression_p)
2263 if (!parser->allow_non_integral_constant_expression_p)
2265 error ("%s cannot appear in a constant-expression", thing);
2272 /* Emit a diagnostic for an invalid type name. SCOPE is the
2273 qualifying scope (or NULL, if none) for ID. This function commits
2274 to the current active tentative parse, if any. (Otherwise, the
2275 problematic construct might be encountered again later, resulting
2276 in duplicate error messages.) */
2279 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2281 tree decl, old_scope;
2282 /* Try to lookup the identifier. */
2283 old_scope = parser->scope;
2284 parser->scope = scope;
2285 decl = cp_parser_lookup_name_simple (parser, id);
2286 parser->scope = old_scope;
2287 /* If the lookup found a template-name, it means that the user forgot
2288 to specify an argument list. Emit a useful error message. */
2289 if (TREE_CODE (decl) == TEMPLATE_DECL)
2290 error ("invalid use of template-name %qE without an argument list", decl);
2291 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2292 error ("invalid use of destructor %qD as a type", id);
2293 else if (TREE_CODE (decl) == TYPE_DECL)
2294 /* Something like 'unsigned A a;' */
2295 error ("invalid combination of multiple type-specifiers");
2296 else if (!parser->scope)
2298 /* Issue an error message. */
2299 error ("%qE does not name a type", id);
2300 /* If we're in a template class, it's possible that the user was
2301 referring to a type from a base class. For example:
2303 template <typename T> struct A { typedef T X; };
2304 template <typename T> struct B : public A<T> { X x; };
2306 The user should have said "typename A<T>::X". */
2307 if (processing_template_decl && current_class_type
2308 && TYPE_BINFO (current_class_type))
2312 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2316 tree base_type = BINFO_TYPE (b);
2317 if (CLASS_TYPE_P (base_type)
2318 && dependent_type_p (base_type))
2321 /* Go from a particular instantiation of the
2322 template (which will have an empty TYPE_FIELDs),
2323 to the main version. */
2324 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2325 for (field = TYPE_FIELDS (base_type);
2327 field = TREE_CHAIN (field))
2328 if (TREE_CODE (field) == TYPE_DECL
2329 && DECL_NAME (field) == id)
2331 inform ("(perhaps %<typename %T::%E%> was intended)",
2332 BINFO_TYPE (b), id);
2341 /* Here we diagnose qualified-ids where the scope is actually correct,
2342 but the identifier does not resolve to a valid type name. */
2343 else if (parser->scope != error_mark_node)
2345 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2346 error ("%qE in namespace %qE does not name a type",
2348 else if (TYPE_P (parser->scope))
2349 error ("%qE in class %qT does not name a type", id, parser->scope);
2353 cp_parser_commit_to_tentative_parse (parser);
2356 /* Check for a common situation where a type-name should be present,
2357 but is not, and issue a sensible error message. Returns true if an
2358 invalid type-name was detected.
2360 The situation handled by this function are variable declarations of the
2361 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2362 Usually, `ID' should name a type, but if we got here it means that it
2363 does not. We try to emit the best possible error message depending on
2364 how exactly the id-expression looks like. */
2367 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2371 cp_parser_parse_tentatively (parser);
2372 id = cp_parser_id_expression (parser,
2373 /*template_keyword_p=*/false,
2374 /*check_dependency_p=*/true,
2375 /*template_p=*/NULL,
2376 /*declarator_p=*/true,
2377 /*optional_p=*/false);
2378 /* After the id-expression, there should be a plain identifier,
2379 otherwise this is not a simple variable declaration. Also, if
2380 the scope is dependent, we cannot do much. */
2381 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2382 || (parser->scope && TYPE_P (parser->scope)
2383 && dependent_type_p (parser->scope))
2384 || TREE_CODE (id) == TYPE_DECL)
2386 cp_parser_abort_tentative_parse (parser);
2389 if (!cp_parser_parse_definitely (parser))
2392 /* Emit a diagnostic for the invalid type. */
2393 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2394 /* Skip to the end of the declaration; there's no point in
2395 trying to process it. */
2396 cp_parser_skip_to_end_of_block_or_statement (parser);
2400 /* Consume tokens up to, and including, the next non-nested closing `)'.
2401 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2402 are doing error recovery. Returns -1 if OR_COMMA is true and we
2403 found an unnested comma. */
2406 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2411 unsigned paren_depth = 0;
2412 unsigned brace_depth = 0;
2414 if (recovering && !or_comma
2415 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2420 cp_token * token = cp_lexer_peek_token (parser->lexer);
2422 switch (token->type)
2425 case CPP_PRAGMA_EOL:
2426 /* If we've run out of tokens, then there is no closing `)'. */
2430 /* This matches the processing in skip_to_end_of_statement. */
2435 case CPP_OPEN_BRACE:
2438 case CPP_CLOSE_BRACE:
2444 if (recovering && or_comma && !brace_depth && !paren_depth)
2448 case CPP_OPEN_PAREN:
2453 case CPP_CLOSE_PAREN:
2454 if (!brace_depth && !paren_depth--)
2457 cp_lexer_consume_token (parser->lexer);
2466 /* Consume the token. */
2467 cp_lexer_consume_token (parser->lexer);
2471 /* Consume tokens until we reach the end of the current statement.
2472 Normally, that will be just before consuming a `;'. However, if a
2473 non-nested `}' comes first, then we stop before consuming that. */
2476 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2478 unsigned nesting_depth = 0;
2482 cp_token *token = cp_lexer_peek_token (parser->lexer);
2484 switch (token->type)
2487 case CPP_PRAGMA_EOL:
2488 /* If we've run out of tokens, stop. */
2492 /* If the next token is a `;', we have reached the end of the
2498 case CPP_CLOSE_BRACE:
2499 /* If this is a non-nested '}', stop before consuming it.
2500 That way, when confronted with something like:
2504 we stop before consuming the closing '}', even though we
2505 have not yet reached a `;'. */
2506 if (nesting_depth == 0)
2509 /* If it is the closing '}' for a block that we have
2510 scanned, stop -- but only after consuming the token.
2516 we will stop after the body of the erroneously declared
2517 function, but before consuming the following `typedef'
2519 if (--nesting_depth == 0)
2521 cp_lexer_consume_token (parser->lexer);
2525 case CPP_OPEN_BRACE:
2533 /* Consume the token. */
2534 cp_lexer_consume_token (parser->lexer);
2538 /* This function is called at the end of a statement or declaration.
2539 If the next token is a semicolon, it is consumed; otherwise, error
2540 recovery is attempted. */
2543 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2545 /* Look for the trailing `;'. */
2546 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2548 /* If there is additional (erroneous) input, skip to the end of
2550 cp_parser_skip_to_end_of_statement (parser);
2551 /* If the next token is now a `;', consume it. */
2552 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2553 cp_lexer_consume_token (parser->lexer);
2557 /* Skip tokens until we have consumed an entire block, or until we
2558 have consumed a non-nested `;'. */
2561 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2563 int nesting_depth = 0;
2565 while (nesting_depth >= 0)
2567 cp_token *token = cp_lexer_peek_token (parser->lexer);
2569 switch (token->type)
2572 case CPP_PRAGMA_EOL:
2573 /* If we've run out of tokens, stop. */
2577 /* Stop if this is an unnested ';'. */
2582 case CPP_CLOSE_BRACE:
2583 /* Stop if this is an unnested '}', or closes the outermost
2590 case CPP_OPEN_BRACE:
2599 /* Consume the token. */
2600 cp_lexer_consume_token (parser->lexer);
2604 /* Skip tokens until a non-nested closing curly brace is the next
2605 token, or there are no more tokens. Return true in the first case,
2609 cp_parser_skip_to_closing_brace (cp_parser *parser)
2611 unsigned nesting_depth = 0;
2615 cp_token *token = cp_lexer_peek_token (parser->lexer);
2617 switch (token->type)
2620 case CPP_PRAGMA_EOL:
2621 /* If we've run out of tokens, stop. */
2624 case CPP_CLOSE_BRACE:
2625 /* If the next token is a non-nested `}', then we have reached
2626 the end of the current block. */
2627 if (nesting_depth-- == 0)
2631 case CPP_OPEN_BRACE:
2632 /* If it the next token is a `{', then we are entering a new
2633 block. Consume the entire block. */
2641 /* Consume the token. */
2642 cp_lexer_consume_token (parser->lexer);
2646 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2647 parameter is the PRAGMA token, allowing us to purge the entire pragma
2651 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2655 parser->lexer->in_pragma = false;
2658 token = cp_lexer_consume_token (parser->lexer);
2659 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2661 /* Ensure that the pragma is not parsed again. */
2662 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2665 /* Require pragma end of line, resyncing with it as necessary. The
2666 arguments are as for cp_parser_skip_to_pragma_eol. */
2669 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2671 parser->lexer->in_pragma = false;
2672 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2673 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2676 /* This is a simple wrapper around make_typename_type. When the id is
2677 an unresolved identifier node, we can provide a superior diagnostic
2678 using cp_parser_diagnose_invalid_type_name. */
2681 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2684 if (TREE_CODE (id) == IDENTIFIER_NODE)
2686 result = make_typename_type (scope, id, typename_type,
2687 /*complain=*/tf_none);
2688 if (result == error_mark_node)
2689 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2692 return make_typename_type (scope, id, typename_type, tf_error);
2695 /* This is a wrapper around the
2696 make_{pointer,ptrmem,reference}_declarator functions that decides
2697 which one to call based on the CODE and CLASS_TYPE arguments. The
2698 CODE argument should be one of the values returned by
2699 cp_parser_ptr_operator. */
2700 static cp_declarator *
2701 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2702 cp_cv_quals cv_qualifiers,
2703 cp_declarator *target)
2705 if (code == ERROR_MARK)
2706 return cp_error_declarator;
2708 if (code == INDIRECT_REF)
2709 if (class_type == NULL_TREE)
2710 return make_pointer_declarator (cv_qualifiers, target);
2712 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2713 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2714 return make_reference_declarator (cv_qualifiers, target, false);
2715 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, true);
2720 /* Create a new C++ parser. */
2723 cp_parser_new (void)
2729 /* cp_lexer_new_main is called before calling ggc_alloc because
2730 cp_lexer_new_main might load a PCH file. */
2731 lexer = cp_lexer_new_main ();
2733 /* Initialize the binops_by_token so that we can get the tree
2734 directly from the token. */
2735 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2736 binops_by_token[binops[i].token_type] = binops[i];
2738 parser = GGC_CNEW (cp_parser);
2739 parser->lexer = lexer;
2740 parser->context = cp_parser_context_new (NULL);
2742 /* For now, we always accept GNU extensions. */
2743 parser->allow_gnu_extensions_p = 1;
2745 /* The `>' token is a greater-than operator, not the end of a
2747 parser->greater_than_is_operator_p = true;
2749 parser->default_arg_ok_p = true;
2751 /* We are not parsing a constant-expression. */
2752 parser->integral_constant_expression_p = false;
2753 parser->allow_non_integral_constant_expression_p = false;
2754 parser->non_integral_constant_expression_p = false;
2756 /* Local variable names are not forbidden. */
2757 parser->local_variables_forbidden_p = false;
2759 /* We are not processing an `extern "C"' declaration. */
2760 parser->in_unbraced_linkage_specification_p = false;
2762 /* We are not processing a declarator. */
2763 parser->in_declarator_p = false;
2765 /* We are not processing a template-argument-list. */
2766 parser->in_template_argument_list_p = false;
2768 /* We are not in an iteration statement. */
2769 parser->in_statement = 0;
2771 /* We are not in a switch statement. */
2772 parser->in_switch_statement_p = false;
2774 /* We are not parsing a type-id inside an expression. */
2775 parser->in_type_id_in_expr_p = false;
2777 /* Declarations aren't implicitly extern "C". */
2778 parser->implicit_extern_c = false;
2780 /* String literals should be translated to the execution character set. */
2781 parser->translate_strings_p = true;
2783 /* We are not parsing a function body. */
2784 parser->in_function_body = false;
2786 /* The unparsed function queue is empty. */
2787 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2789 /* There are no classes being defined. */
2790 parser->num_classes_being_defined = 0;
2792 /* No template parameters apply. */
2793 parser->num_template_parameter_lists = 0;
2798 /* Create a cp_lexer structure which will emit the tokens in CACHE
2799 and push it onto the parser's lexer stack. This is used for delayed
2800 parsing of in-class method bodies and default arguments, and should
2801 not be confused with tentative parsing. */
2803 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2805 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2806 lexer->next = parser->lexer;
2807 parser->lexer = lexer;
2809 /* Move the current source position to that of the first token in the
2811 cp_lexer_set_source_position_from_token (lexer->next_token);
2814 /* Pop the top lexer off the parser stack. This is never used for the
2815 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2817 cp_parser_pop_lexer (cp_parser *parser)
2819 cp_lexer *lexer = parser->lexer;
2820 parser->lexer = lexer->next;
2821 cp_lexer_destroy (lexer);
2823 /* Put the current source position back where it was before this
2824 lexer was pushed. */
2825 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2828 /* Lexical conventions [gram.lex] */
2830 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2834 cp_parser_identifier (cp_parser* parser)
2838 /* Look for the identifier. */
2839 token = cp_parser_require (parser, CPP_NAME, "identifier");
2840 /* Return the value. */
2841 return token ? token->u.value : error_mark_node;
2844 /* Parse a sequence of adjacent string constants. Returns a
2845 TREE_STRING representing the combined, nul-terminated string
2846 constant. If TRANSLATE is true, translate the string to the
2847 execution character set. If WIDE_OK is true, a wide string is
2850 C++98 [lex.string] says that if a narrow string literal token is
2851 adjacent to a wide string literal token, the behavior is undefined.
2852 However, C99 6.4.5p4 says that this results in a wide string literal.
2853 We follow C99 here, for consistency with the C front end.
2855 This code is largely lifted from lex_string() in c-lex.c.
2857 FUTURE: ObjC++ will need to handle @-strings here. */
2859 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2864 struct obstack str_ob;
2865 cpp_string str, istr, *strs;
2868 tok = cp_lexer_peek_token (parser->lexer);
2869 if (!cp_parser_is_string_literal (tok))
2871 cp_parser_error (parser, "expected string-literal");
2872 return error_mark_node;
2875 /* Try to avoid the overhead of creating and destroying an obstack
2876 for the common case of just one string. */
2877 if (!cp_parser_is_string_literal
2878 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2880 cp_lexer_consume_token (parser->lexer);
2882 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2883 str.len = TREE_STRING_LENGTH (tok->u.value);
2885 if (tok->type == CPP_WSTRING)
2892 gcc_obstack_init (&str_ob);
2897 cp_lexer_consume_token (parser->lexer);
2899 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2900 str.len = TREE_STRING_LENGTH (tok->u.value);
2901 if (tok->type == CPP_WSTRING)
2904 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2906 tok = cp_lexer_peek_token (parser->lexer);
2908 while (cp_parser_is_string_literal (tok));
2910 strs = (cpp_string *) obstack_finish (&str_ob);
2913 if (wide && !wide_ok)
2915 cp_parser_error (parser, "a wide string is invalid in this context");
2919 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2920 (parse_in, strs, count, &istr, wide))
2922 value = build_string (istr.len, (char *)istr.text);
2923 free ((void *)istr.text);
2925 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2926 value = fix_string_type (value);
2929 /* cpp_interpret_string has issued an error. */
2930 value = error_mark_node;
2933 obstack_free (&str_ob, 0);
2939 /* Basic concepts [gram.basic] */
2941 /* Parse a translation-unit.
2944 declaration-seq [opt]
2946 Returns TRUE if all went well. */
2949 cp_parser_translation_unit (cp_parser* parser)
2951 /* The address of the first non-permanent object on the declarator
2953 static void *declarator_obstack_base;
2957 /* Create the declarator obstack, if necessary. */
2958 if (!cp_error_declarator)
2960 gcc_obstack_init (&declarator_obstack);
2961 /* Create the error declarator. */
2962 cp_error_declarator = make_declarator (cdk_error);
2963 /* Create the empty parameter list. */
2964 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2965 /* Remember where the base of the declarator obstack lies. */
2966 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2969 cp_parser_declaration_seq_opt (parser);
2971 /* If there are no tokens left then all went well. */
2972 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2974 /* Get rid of the token array; we don't need it any more. */
2975 cp_lexer_destroy (parser->lexer);
2976 parser->lexer = NULL;
2978 /* This file might have been a context that's implicitly extern
2979 "C". If so, pop the lang context. (Only relevant for PCH.) */
2980 if (parser->implicit_extern_c)
2982 pop_lang_context ();
2983 parser->implicit_extern_c = false;
2987 finish_translation_unit ();
2993 cp_parser_error (parser, "expected declaration");
2997 /* Make sure the declarator obstack was fully cleaned up. */
2998 gcc_assert (obstack_next_free (&declarator_obstack)
2999 == declarator_obstack_base);
3001 /* All went well. */
3005 /* Expressions [gram.expr] */
3007 /* Parse a primary-expression.
3018 ( compound-statement )
3019 __builtin_va_arg ( assignment-expression , type-id )
3020 __builtin_offsetof ( type-id , offsetof-expression )
3023 __has_nothrow_assign ( type-id )
3024 __has_nothrow_constructor ( type-id )
3025 __has_nothrow_copy ( type-id )
3026 __has_trivial_assign ( type-id )
3027 __has_trivial_constructor ( type-id )
3028 __has_trivial_copy ( type-id )
3029 __has_trivial_destructor ( type-id )
3030 __has_virtual_destructor ( type-id )
3031 __is_abstract ( type-id )
3032 __is_base_of ( type-id , type-id )
3033 __is_class ( type-id )
3034 __is_convertible_to ( type-id , type-id )
3035 __is_empty ( type-id )
3036 __is_enum ( type-id )
3037 __is_pod ( type-id )
3038 __is_polymorphic ( type-id )
3039 __is_union ( type-id )
3041 Objective-C++ Extension:
3049 ADDRESS_P is true iff this expression was immediately preceded by
3050 "&" and therefore might denote a pointer-to-member. CAST_P is true
3051 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3052 true iff this expression is a template argument.
3054 Returns a representation of the expression. Upon return, *IDK
3055 indicates what kind of id-expression (if any) was present. */
3058 cp_parser_primary_expression (cp_parser *parser,
3061 bool template_arg_p,
3066 /* Assume the primary expression is not an id-expression. */
3067 *idk = CP_ID_KIND_NONE;
3069 /* Peek at the next token. */
3070 token = cp_lexer_peek_token (parser->lexer);
3071 switch (token->type)
3082 token = cp_lexer_consume_token (parser->lexer);
3083 /* Floating-point literals are only allowed in an integral
3084 constant expression if they are cast to an integral or
3085 enumeration type. */
3086 if (TREE_CODE (token->u.value) == REAL_CST
3087 && parser->integral_constant_expression_p
3090 /* CAST_P will be set even in invalid code like "int(2.7 +
3091 ...)". Therefore, we have to check that the next token
3092 is sure to end the cast. */
3095 cp_token *next_token;
3097 next_token = cp_lexer_peek_token (parser->lexer);
3098 if (/* The comma at the end of an
3099 enumerator-definition. */
3100 next_token->type != CPP_COMMA
3101 /* The curly brace at the end of an enum-specifier. */
3102 && next_token->type != CPP_CLOSE_BRACE
3103 /* The end of a statement. */
3104 && next_token->type != CPP_SEMICOLON
3105 /* The end of the cast-expression. */
3106 && next_token->type != CPP_CLOSE_PAREN
3107 /* The end of an array bound. */
3108 && next_token->type != CPP_CLOSE_SQUARE
3109 /* The closing ">" in a template-argument-list. */
3110 && (next_token->type != CPP_GREATER
3111 || parser->greater_than_is_operator_p)
3112 /* C++0x only: A ">>" treated like two ">" tokens,
3113 in a template-argument-list. */
3114 && (next_token->type != CPP_RSHIFT
3115 || (cxx_dialect == cxx98)
3116 || parser->greater_than_is_operator_p))
3120 /* If we are within a cast, then the constraint that the
3121 cast is to an integral or enumeration type will be
3122 checked at that point. If we are not within a cast, then
3123 this code is invalid. */
3125 cp_parser_non_integral_constant_expression
3126 (parser, "floating-point literal");
3128 return token->u.value;
3132 /* ??? Should wide strings be allowed when parser->translate_strings_p
3133 is false (i.e. in attributes)? If not, we can kill the third
3134 argument to cp_parser_string_literal. */
3135 return cp_parser_string_literal (parser,
3136 parser->translate_strings_p,
3139 case CPP_OPEN_PAREN:
3142 bool saved_greater_than_is_operator_p;
3144 /* Consume the `('. */
3145 cp_lexer_consume_token (parser->lexer);
3146 /* Within a parenthesized expression, a `>' token is always
3147 the greater-than operator. */
3148 saved_greater_than_is_operator_p
3149 = parser->greater_than_is_operator_p;
3150 parser->greater_than_is_operator_p = true;
3151 /* If we see `( { ' then we are looking at the beginning of
3152 a GNU statement-expression. */
3153 if (cp_parser_allow_gnu_extensions_p (parser)
3154 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3156 /* Statement-expressions are not allowed by the standard. */
3158 pedwarn ("ISO C++ forbids braced-groups within expressions");
3160 /* And they're not allowed outside of a function-body; you
3161 cannot, for example, write:
3163 int i = ({ int j = 3; j + 1; });
3165 at class or namespace scope. */
3166 if (!parser->in_function_body)
3168 error ("statement-expressions are allowed only inside functions");
3169 cp_parser_skip_to_end_of_block_or_statement (parser);
3170 expr = error_mark_node;
3174 /* Start the statement-expression. */
3175 expr = begin_stmt_expr ();
3176 /* Parse the compound-statement. */
3177 cp_parser_compound_statement (parser, expr, false);
3179 expr = finish_stmt_expr (expr, false);
3184 /* Parse the parenthesized expression. */
3185 expr = cp_parser_expression (parser, cast_p);
3186 /* Let the front end know that this expression was
3187 enclosed in parentheses. This matters in case, for
3188 example, the expression is of the form `A::B', since
3189 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3191 finish_parenthesized_expr (expr);
3193 /* The `>' token might be the end of a template-id or
3194 template-parameter-list now. */
3195 parser->greater_than_is_operator_p
3196 = saved_greater_than_is_operator_p;
3197 /* Consume the `)'. */
3198 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3199 cp_parser_skip_to_end_of_statement (parser);
3205 switch (token->keyword)
3207 /* These two are the boolean literals. */
3209 cp_lexer_consume_token (parser->lexer);
3210 return boolean_true_node;
3212 cp_lexer_consume_token (parser->lexer);
3213 return boolean_false_node;
3215 /* The `__null' literal. */
3217 cp_lexer_consume_token (parser->lexer);
3220 /* Recognize the `this' keyword. */
3222 cp_lexer_consume_token (parser->lexer);
3223 if (parser->local_variables_forbidden_p)
3225 error ("%<this%> may not be used in this context");
3226 return error_mark_node;
3228 /* Pointers cannot appear in constant-expressions. */
3229 if (cp_parser_non_integral_constant_expression (parser,
3231 return error_mark_node;
3232 return finish_this_expr ();
3234 /* The `operator' keyword can be the beginning of an
3239 case RID_FUNCTION_NAME:
3240 case RID_PRETTY_FUNCTION_NAME:
3241 case RID_C99_FUNCTION_NAME:
3242 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3243 __func__ are the names of variables -- but they are
3244 treated specially. Therefore, they are handled here,
3245 rather than relying on the generic id-expression logic
3246 below. Grammatically, these names are id-expressions.
3248 Consume the token. */
3249 token = cp_lexer_consume_token (parser->lexer);
3250 /* Look up the name. */
3251 return finish_fname (token->u.value);
3258 /* The `__builtin_va_arg' construct is used to handle
3259 `va_arg'. Consume the `__builtin_va_arg' token. */
3260 cp_lexer_consume_token (parser->lexer);
3261 /* Look for the opening `('. */
3262 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3263 /* Now, parse the assignment-expression. */
3264 expression = cp_parser_assignment_expression (parser,
3266 /* Look for the `,'. */
3267 cp_parser_require (parser, CPP_COMMA, "`,'");
3268 /* Parse the type-id. */
3269 type = cp_parser_type_id (parser);
3270 /* Look for the closing `)'. */
3271 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3272 /* Using `va_arg' in a constant-expression is not
3274 if (cp_parser_non_integral_constant_expression (parser,
3276 return error_mark_node;
3277 return build_x_va_arg (expression, type);
3281 return cp_parser_builtin_offsetof (parser);
3283 case RID_HAS_NOTHROW_ASSIGN:
3284 case RID_HAS_NOTHROW_CONSTRUCTOR:
3285 case RID_HAS_NOTHROW_COPY:
3286 case RID_HAS_TRIVIAL_ASSIGN:
3287 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3288 case RID_HAS_TRIVIAL_COPY:
3289 case RID_HAS_TRIVIAL_DESTRUCTOR:
3290 case RID_HAS_VIRTUAL_DESTRUCTOR:
3291 case RID_IS_ABSTRACT:
3292 case RID_IS_BASE_OF:
3294 case RID_IS_CONVERTIBLE_TO:
3298 case RID_IS_POLYMORPHIC:
3300 return cp_parser_trait_expr (parser, token->keyword);
3302 /* Objective-C++ expressions. */
3304 case RID_AT_PROTOCOL:
3305 case RID_AT_SELECTOR:
3306 return cp_parser_objc_expression (parser);
3309 cp_parser_error (parser, "expected primary-expression");
3310 return error_mark_node;
3313 /* An id-expression can start with either an identifier, a
3314 `::' as the beginning of a qualified-id, or the "operator"
3318 case CPP_TEMPLATE_ID:
3319 case CPP_NESTED_NAME_SPECIFIER:
3323 const char *error_msg;
3328 /* Parse the id-expression. */
3330 = cp_parser_id_expression (parser,
3331 /*template_keyword_p=*/false,
3332 /*check_dependency_p=*/true,
3334 /*declarator_p=*/false,
3335 /*optional_p=*/false);
3336 if (id_expression == error_mark_node)
3337 return error_mark_node;
3338 token = cp_lexer_peek_token (parser->lexer);
3339 done = (token->type != CPP_OPEN_SQUARE
3340 && token->type != CPP_OPEN_PAREN
3341 && token->type != CPP_DOT
3342 && token->type != CPP_DEREF
3343 && token->type != CPP_PLUS_PLUS
3344 && token->type != CPP_MINUS_MINUS);
3345 /* If we have a template-id, then no further lookup is
3346 required. If the template-id was for a template-class, we
3347 will sometimes have a TYPE_DECL at this point. */
3348 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3349 || TREE_CODE (id_expression) == TYPE_DECL)
3350 decl = id_expression;
3351 /* Look up the name. */
3354 tree ambiguous_decls;
3356 decl = cp_parser_lookup_name (parser, id_expression,
3359 /*is_namespace=*/false,
3360 /*check_dependency=*/true,
3362 /* If the lookup was ambiguous, an error will already have
3364 if (ambiguous_decls)
3365 return error_mark_node;
3367 /* In Objective-C++, an instance variable (ivar) may be preferred
3368 to whatever cp_parser_lookup_name() found. */
3369 decl = objc_lookup_ivar (decl, id_expression);
3371 /* If name lookup gives us a SCOPE_REF, then the
3372 qualifying scope was dependent. */
3373 if (TREE_CODE (decl) == SCOPE_REF)
3375 /* At this point, we do not know if DECL is a valid
3376 integral constant expression. We assume that it is
3377 in fact such an expression, so that code like:
3379 template <int N> struct A {
3383 is accepted. At template-instantiation time, we
3384 will check that B<N>::i is actually a constant. */
3387 /* Check to see if DECL is a local variable in a context
3388 where that is forbidden. */
3389 if (parser->local_variables_forbidden_p
3390 && local_variable_p (decl))
3392 /* It might be that we only found DECL because we are
3393 trying to be generous with pre-ISO scoping rules.
3394 For example, consider:
3398 for (int i = 0; i < 10; ++i) {}
3399 extern void f(int j = i);
3402 Here, name look up will originally find the out
3403 of scope `i'. We need to issue a warning message,
3404 but then use the global `i'. */
3405 decl = check_for_out_of_scope_variable (decl);
3406 if (local_variable_p (decl))
3408 error ("local variable %qD may not appear in this context",
3410 return error_mark_node;
3415 decl = (finish_id_expression
3416 (id_expression, decl, parser->scope,
3418 parser->integral_constant_expression_p,
3419 parser->allow_non_integral_constant_expression_p,
3420 &parser->non_integral_constant_expression_p,
3421 template_p, done, address_p,
3425 cp_parser_error (parser, error_msg);
3429 /* Anything else is an error. */
3431 /* ...unless we have an Objective-C++ message or string literal,
3433 if (c_dialect_objc ()
3434 && (token->type == CPP_OPEN_SQUARE
3435 || token->type == CPP_OBJC_STRING))
3436 return cp_parser_objc_expression (parser);
3438 cp_parser_error (parser, "expected primary-expression");
3439 return error_mark_node;
3443 /* Parse an id-expression.
3450 :: [opt] nested-name-specifier template [opt] unqualified-id
3452 :: operator-function-id
3455 Return a representation of the unqualified portion of the
3456 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3457 a `::' or nested-name-specifier.
3459 Often, if the id-expression was a qualified-id, the caller will
3460 want to make a SCOPE_REF to represent the qualified-id. This
3461 function does not do this in order to avoid wastefully creating
3462 SCOPE_REFs when they are not required.
3464 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3467 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3468 uninstantiated templates.
3470 If *TEMPLATE_P is non-NULL, it is set to true iff the
3471 `template' keyword is used to explicitly indicate that the entity
3472 named is a template.
3474 If DECLARATOR_P is true, the id-expression is appearing as part of
3475 a declarator, rather than as part of an expression. */
3478 cp_parser_id_expression (cp_parser *parser,
3479 bool template_keyword_p,
3480 bool check_dependency_p,
3485 bool global_scope_p;
3486 bool nested_name_specifier_p;
3488 /* Assume the `template' keyword was not used. */
3490 *template_p = template_keyword_p;
3492 /* Look for the optional `::' operator. */
3494 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3496 /* Look for the optional nested-name-specifier. */
3497 nested_name_specifier_p
3498 = (cp_parser_nested_name_specifier_opt (parser,
3499 /*typename_keyword_p=*/false,
3504 /* If there is a nested-name-specifier, then we are looking at
3505 the first qualified-id production. */
3506 if (nested_name_specifier_p)
3509 tree saved_object_scope;
3510 tree saved_qualifying_scope;
3511 tree unqualified_id;
3514 /* See if the next token is the `template' keyword. */
3516 template_p = &is_template;
3517 *template_p = cp_parser_optional_template_keyword (parser);
3518 /* Name lookup we do during the processing of the
3519 unqualified-id might obliterate SCOPE. */
3520 saved_scope = parser->scope;
3521 saved_object_scope = parser->object_scope;
3522 saved_qualifying_scope = parser->qualifying_scope;
3523 /* Process the final unqualified-id. */
3524 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3527 /*optional_p=*/false);
3528 /* Restore the SAVED_SCOPE for our caller. */
3529 parser->scope = saved_scope;
3530 parser->object_scope = saved_object_scope;
3531 parser->qualifying_scope = saved_qualifying_scope;
3533 return unqualified_id;
3535 /* Otherwise, if we are in global scope, then we are looking at one
3536 of the other qualified-id productions. */
3537 else if (global_scope_p)
3542 /* Peek at the next token. */
3543 token = cp_lexer_peek_token (parser->lexer);
3545 /* If it's an identifier, and the next token is not a "<", then
3546 we can avoid the template-id case. This is an optimization
3547 for this common case. */
3548 if (token->type == CPP_NAME
3549 && !cp_parser_nth_token_starts_template_argument_list_p
3551 return cp_parser_identifier (parser);
3553 cp_parser_parse_tentatively (parser);
3554 /* Try a template-id. */
3555 id = cp_parser_template_id (parser,
3556 /*template_keyword_p=*/false,
3557 /*check_dependency_p=*/true,
3559 /* If that worked, we're done. */
3560 if (cp_parser_parse_definitely (parser))
3563 /* Peek at the next token. (Changes in the token buffer may
3564 have invalidated the pointer obtained above.) */
3565 token = cp_lexer_peek_token (parser->lexer);
3567 switch (token->type)
3570 return cp_parser_identifier (parser);
3573 if (token->keyword == RID_OPERATOR)
3574 return cp_parser_operator_function_id (parser);
3578 cp_parser_error (parser, "expected id-expression");
3579 return error_mark_node;
3583 return cp_parser_unqualified_id (parser, template_keyword_p,
3584 /*check_dependency_p=*/true,
3589 /* Parse an unqualified-id.
3593 operator-function-id
3594 conversion-function-id
3598 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3599 keyword, in a construct like `A::template ...'.
3601 Returns a representation of unqualified-id. For the `identifier'
3602 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3603 production a BIT_NOT_EXPR is returned; the operand of the
3604 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3605 other productions, see the documentation accompanying the
3606 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3607 names are looked up in uninstantiated templates. If DECLARATOR_P
3608 is true, the unqualified-id is appearing as part of a declarator,
3609 rather than as part of an expression. */
3612 cp_parser_unqualified_id (cp_parser* parser,
3613 bool template_keyword_p,
3614 bool check_dependency_p,
3620 /* Peek at the next token. */
3621 token = cp_lexer_peek_token (parser->lexer);
3623 switch (token->type)
3629 /* We don't know yet whether or not this will be a
3631 cp_parser_parse_tentatively (parser);
3632 /* Try a template-id. */
3633 id = cp_parser_template_id (parser, template_keyword_p,
3636 /* If it worked, we're done. */
3637 if (cp_parser_parse_definitely (parser))
3639 /* Otherwise, it's an ordinary identifier. */
3640 return cp_parser_identifier (parser);
3643 case CPP_TEMPLATE_ID:
3644 return cp_parser_template_id (parser, template_keyword_p,
3651 tree qualifying_scope;
3656 /* Consume the `~' token. */
3657 cp_lexer_consume_token (parser->lexer);
3658 /* Parse the class-name. The standard, as written, seems to
3661 template <typename T> struct S { ~S (); };
3662 template <typename T> S<T>::~S() {}
3664 is invalid, since `~' must be followed by a class-name, but
3665 `S<T>' is dependent, and so not known to be a class.
3666 That's not right; we need to look in uninstantiated
3667 templates. A further complication arises from:
3669 template <typename T> void f(T t) {
3673 Here, it is not possible to look up `T' in the scope of `T'
3674 itself. We must look in both the current scope, and the
3675 scope of the containing complete expression.
3677 Yet another issue is:
3686 The standard does not seem to say that the `S' in `~S'
3687 should refer to the type `S' and not the data member
3690 /* DR 244 says that we look up the name after the "~" in the
3691 same scope as we looked up the qualifying name. That idea
3692 isn't fully worked out; it's more complicated than that. */
3693 scope = parser->scope;
3694 object_scope = parser->object_scope;
3695 qualifying_scope = parser->qualifying_scope;
3697 /* Check for invalid scopes. */
3698 if (scope == error_mark_node)
3700 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3701 cp_lexer_consume_token (parser->lexer);
3702 return error_mark_node;
3704 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3706 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3707 error ("scope %qT before %<~%> is not a class-name", scope);
3708 cp_parser_simulate_error (parser);
3709 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3710 cp_lexer_consume_token (parser->lexer);
3711 return error_mark_node;
3713 gcc_assert (!scope || TYPE_P (scope));
3715 /* If the name is of the form "X::~X" it's OK. */
3716 token = cp_lexer_peek_token (parser->lexer);
3718 && token->type == CPP_NAME
3719 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3721 && constructor_name_p (token->u.value, scope))
3723 cp_lexer_consume_token (parser->lexer);
3724 return build_nt (BIT_NOT_EXPR, scope);
3727 /* If there was an explicit qualification (S::~T), first look
3728 in the scope given by the qualification (i.e., S). */
3730 type_decl = NULL_TREE;
3733 cp_parser_parse_tentatively (parser);
3734 type_decl = cp_parser_class_name (parser,
3735 /*typename_keyword_p=*/false,
3736 /*template_keyword_p=*/false,
3738 /*check_dependency=*/false,
3739 /*class_head_p=*/false,
3741 if (cp_parser_parse_definitely (parser))
3744 /* In "N::S::~S", look in "N" as well. */
3745 if (!done && scope && qualifying_scope)
3747 cp_parser_parse_tentatively (parser);
3748 parser->scope = qualifying_scope;
3749 parser->object_scope = NULL_TREE;
3750 parser->qualifying_scope = NULL_TREE;
3752 = cp_parser_class_name (parser,
3753 /*typename_keyword_p=*/false,
3754 /*template_keyword_p=*/false,
3756 /*check_dependency=*/false,
3757 /*class_head_p=*/false,
3759 if (cp_parser_parse_definitely (parser))
3762 /* In "p->S::~T", look in the scope given by "*p" as well. */
3763 else if (!done && object_scope)
3765 cp_parser_parse_tentatively (parser);
3766 parser->scope = object_scope;
3767 parser->object_scope = NULL_TREE;
3768 parser->qualifying_scope = NULL_TREE;
3770 = cp_parser_class_name (parser,
3771 /*typename_keyword_p=*/false,
3772 /*template_keyword_p=*/false,
3774 /*check_dependency=*/false,
3775 /*class_head_p=*/false,
3777 if (cp_parser_parse_definitely (parser))
3780 /* Look in the surrounding context. */
3783 parser->scope = NULL_TREE;
3784 parser->object_scope = NULL_TREE;
3785 parser->qualifying_scope = NULL_TREE;
3787 = cp_parser_class_name (parser,
3788 /*typename_keyword_p=*/false,
3789 /*template_keyword_p=*/false,
3791 /*check_dependency=*/false,
3792 /*class_head_p=*/false,
3795 /* If an error occurred, assume that the name of the
3796 destructor is the same as the name of the qualifying
3797 class. That allows us to keep parsing after running
3798 into ill-formed destructor names. */
3799 if (type_decl == error_mark_node && scope)
3800 return build_nt (BIT_NOT_EXPR, scope);
3801 else if (type_decl == error_mark_node)
3802 return error_mark_node;
3804 /* Check that destructor name and scope match. */
3805 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3807 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3808 error ("declaration of %<~%T%> as member of %qT",
3810 cp_parser_simulate_error (parser);
3811 return error_mark_node;
3816 A typedef-name that names a class shall not be used as the
3817 identifier in the declarator for a destructor declaration. */
3819 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3820 && !DECL_SELF_REFERENCE_P (type_decl)
3821 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3822 error ("typedef-name %qD used as destructor declarator",
3825 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3829 if (token->keyword == RID_OPERATOR)
3833 /* This could be a template-id, so we try that first. */
3834 cp_parser_parse_tentatively (parser);
3835 /* Try a template-id. */
3836 id = cp_parser_template_id (parser, template_keyword_p,
3837 /*check_dependency_p=*/true,
3839 /* If that worked, we're done. */
3840 if (cp_parser_parse_definitely (parser))
3842 /* We still don't know whether we're looking at an
3843 operator-function-id or a conversion-function-id. */
3844 cp_parser_parse_tentatively (parser);
3845 /* Try an operator-function-id. */
3846 id = cp_parser_operator_function_id (parser);
3847 /* If that didn't work, try a conversion-function-id. */
3848 if (!cp_parser_parse_definitely (parser))
3849 id = cp_parser_conversion_function_id (parser);
3858 cp_parser_error (parser, "expected unqualified-id");
3859 return error_mark_node;
3863 /* Parse an (optional) nested-name-specifier.
3865 nested-name-specifier:
3866 class-or-namespace-name :: nested-name-specifier [opt]
3867 class-or-namespace-name :: template nested-name-specifier [opt]
3869 PARSER->SCOPE should be set appropriately before this function is
3870 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3871 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3874 Sets PARSER->SCOPE to the class (TYPE) or namespace
3875 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3876 it unchanged if there is no nested-name-specifier. Returns the new
3877 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3879 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3880 part of a declaration and/or decl-specifier. */
3883 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3884 bool typename_keyword_p,
3885 bool check_dependency_p,
3887 bool is_declaration)
3889 bool success = false;
3890 cp_token_position start = 0;
3893 /* Remember where the nested-name-specifier starts. */
3894 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3896 start = cp_lexer_token_position (parser->lexer, false);
3897 push_deferring_access_checks (dk_deferred);
3904 tree saved_qualifying_scope;
3905 bool template_keyword_p;
3907 /* Spot cases that cannot be the beginning of a
3908 nested-name-specifier. */
3909 token = cp_lexer_peek_token (parser->lexer);
3911 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3912 the already parsed nested-name-specifier. */
3913 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3915 /* Grab the nested-name-specifier and continue the loop. */
3916 cp_parser_pre_parsed_nested_name_specifier (parser);
3917 /* If we originally encountered this nested-name-specifier
3918 with IS_DECLARATION set to false, we will not have
3919 resolved TYPENAME_TYPEs, so we must do so here. */
3921 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3923 new_scope = resolve_typename_type (parser->scope,
3924 /*only_current_p=*/false);
3925 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3926 parser->scope = new_scope;
3932 /* Spot cases that cannot be the beginning of a
3933 nested-name-specifier. On the second and subsequent times
3934 through the loop, we look for the `template' keyword. */
3935 if (success && token->keyword == RID_TEMPLATE)
3937 /* A template-id can start a nested-name-specifier. */
3938 else if (token->type == CPP_TEMPLATE_ID)
3942 /* If the next token is not an identifier, then it is
3943 definitely not a class-or-namespace-name. */
3944 if (token->type != CPP_NAME)
3946 /* If the following token is neither a `<' (to begin a
3947 template-id), nor a `::', then we are not looking at a
3948 nested-name-specifier. */
3949 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3950 if (token->type != CPP_SCOPE
3951 && !cp_parser_nth_token_starts_template_argument_list_p
3956 /* The nested-name-specifier is optional, so we parse
3958 cp_parser_parse_tentatively (parser);
3960 /* Look for the optional `template' keyword, if this isn't the
3961 first time through the loop. */
3963 template_keyword_p = cp_parser_optional_template_keyword (parser);
3965 template_keyword_p = false;
3967 /* Save the old scope since the name lookup we are about to do
3968 might destroy it. */
3969 old_scope = parser->scope;
3970 saved_qualifying_scope = parser->qualifying_scope;
3971 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3972 look up names in "X<T>::I" in order to determine that "Y" is
3973 a template. So, if we have a typename at this point, we make
3974 an effort to look through it. */
3976 && !typename_keyword_p
3978 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3979 parser->scope = resolve_typename_type (parser->scope,
3980 /*only_current_p=*/false);
3981 /* Parse the qualifying entity. */
3983 = cp_parser_class_or_namespace_name (parser,
3989 /* Look for the `::' token. */
3990 cp_parser_require (parser, CPP_SCOPE, "`::'");
3992 /* If we found what we wanted, we keep going; otherwise, we're
3994 if (!cp_parser_parse_definitely (parser))
3996 bool error_p = false;
3998 /* Restore the OLD_SCOPE since it was valid before the
3999 failed attempt at finding the last
4000 class-or-namespace-name. */
4001 parser->scope = old_scope;
4002 parser->qualifying_scope = saved_qualifying_scope;
4003 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4005 /* If the next token is an identifier, and the one after
4006 that is a `::', then any valid interpretation would have
4007 found a class-or-namespace-name. */
4008 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4009 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4011 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4014 token = cp_lexer_consume_token (parser->lexer);
4017 if (!token->ambiguous_p)
4020 tree ambiguous_decls;
4022 decl = cp_parser_lookup_name (parser, token->u.value,
4024 /*is_template=*/false,
4025 /*is_namespace=*/false,
4026 /*check_dependency=*/true,
4028 if (TREE_CODE (decl) == TEMPLATE_DECL)
4029 error ("%qD used without template parameters", decl);
4030 else if (ambiguous_decls)
4032 error ("reference to %qD is ambiguous",
4034 print_candidates (ambiguous_decls);
4035 decl = error_mark_node;
4038 cp_parser_name_lookup_error
4039 (parser, token->u.value, decl,
4040 "is not a class or namespace");
4042 parser->scope = error_mark_node;
4044 /* Treat this as a successful nested-name-specifier
4049 If the name found is not a class-name (clause
4050 _class_) or namespace-name (_namespace.def_), the
4051 program is ill-formed. */
4054 cp_lexer_consume_token (parser->lexer);
4058 /* We've found one valid nested-name-specifier. */
4060 /* Name lookup always gives us a DECL. */
4061 if (TREE_CODE (new_scope) == TYPE_DECL)
4062 new_scope = TREE_TYPE (new_scope);
4063 /* Uses of "template" must be followed by actual templates. */
4064 if (template_keyword_p
4065 && !(CLASS_TYPE_P (new_scope)
4066 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4067 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4068 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4069 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4070 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4071 == TEMPLATE_ID_EXPR)))
4072 pedwarn (TYPE_P (new_scope)
4073 ? "%qT is not a template"
4074 : "%qD is not a template",
4076 /* If it is a class scope, try to complete it; we are about to
4077 be looking up names inside the class. */
4078 if (TYPE_P (new_scope)
4079 /* Since checking types for dependency can be expensive,
4080 avoid doing it if the type is already complete. */
4081 && !COMPLETE_TYPE_P (new_scope)
4082 /* Do not try to complete dependent types. */
4083 && !dependent_type_p (new_scope))
4084 new_scope = complete_type (new_scope);
4085 /* Make sure we look in the right scope the next time through
4087 parser->scope = new_scope;
4090 /* If parsing tentatively, replace the sequence of tokens that makes
4091 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4092 token. That way, should we re-parse the token stream, we will
4093 not have to repeat the effort required to do the parse, nor will
4094 we issue duplicate error messages. */
4095 if (success && start)
4099 token = cp_lexer_token_at (parser->lexer, start);
4100 /* Reset the contents of the START token. */
4101 token->type = CPP_NESTED_NAME_SPECIFIER;
4102 /* Retrieve any deferred checks. Do not pop this access checks yet
4103 so the memory will not be reclaimed during token replacing below. */
4104 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4105 token->u.tree_check_value->value = parser->scope;
4106 token->u.tree_check_value->checks = get_deferred_access_checks ();
4107 token->u.tree_check_value->qualifying_scope =
4108 parser->qualifying_scope;
4109 token->keyword = RID_MAX;
4111 /* Purge all subsequent tokens. */
4112 cp_lexer_purge_tokens_after (parser->lexer, start);
4116 pop_to_parent_deferring_access_checks ();
4118 return success ? parser->scope : NULL_TREE;
4121 /* Parse a nested-name-specifier. See
4122 cp_parser_nested_name_specifier_opt for details. This function
4123 behaves identically, except that it will an issue an error if no
4124 nested-name-specifier is present. */
4127 cp_parser_nested_name_specifier (cp_parser *parser,
4128 bool typename_keyword_p,
4129 bool check_dependency_p,
4131 bool is_declaration)
4135 /* Look for the nested-name-specifier. */
4136 scope = cp_parser_nested_name_specifier_opt (parser,
4141 /* If it was not present, issue an error message. */
4144 cp_parser_error (parser, "expected nested-name-specifier");
4145 parser->scope = NULL_TREE;
4151 /* Parse a class-or-namespace-name.
4153 class-or-namespace-name:
4157 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4158 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4159 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4160 TYPE_P is TRUE iff the next name should be taken as a class-name,
4161 even the same name is declared to be another entity in the same
4164 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4165 specified by the class-or-namespace-name. If neither is found the
4166 ERROR_MARK_NODE is returned. */
4169 cp_parser_class_or_namespace_name (cp_parser *parser,
4170 bool typename_keyword_p,
4171 bool template_keyword_p,
4172 bool check_dependency_p,
4174 bool is_declaration)
4177 tree saved_qualifying_scope;
4178 tree saved_object_scope;
4182 /* Before we try to parse the class-name, we must save away the
4183 current PARSER->SCOPE since cp_parser_class_name will destroy
4185 saved_scope = parser->scope;
4186 saved_qualifying_scope = parser->qualifying_scope;
4187 saved_object_scope = parser->object_scope;
4188 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4189 there is no need to look for a namespace-name. */
4190 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4192 cp_parser_parse_tentatively (parser);
4193 scope = cp_parser_class_name (parser,
4196 type_p ? class_type : none_type,
4198 /*class_head_p=*/false,
4200 /* If that didn't work, try for a namespace-name. */
4201 if (!only_class_p && !cp_parser_parse_definitely (parser))
4203 /* Restore the saved scope. */
4204 parser->scope = saved_scope;
4205 parser->qualifying_scope = saved_qualifying_scope;
4206 parser->object_scope = saved_object_scope;
4207 /* If we are not looking at an identifier followed by the scope
4208 resolution operator, then this is not part of a
4209 nested-name-specifier. (Note that this function is only used
4210 to parse the components of a nested-name-specifier.) */
4211 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4212 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4213 return error_mark_node;
4214 scope = cp_parser_namespace_name (parser);
4220 /* Parse a postfix-expression.
4224 postfix-expression [ expression ]
4225 postfix-expression ( expression-list [opt] )
4226 simple-type-specifier ( expression-list [opt] )
4227 typename :: [opt] nested-name-specifier identifier
4228 ( expression-list [opt] )
4229 typename :: [opt] nested-name-specifier template [opt] template-id
4230 ( expression-list [opt] )
4231 postfix-expression . template [opt] id-expression
4232 postfix-expression -> template [opt] id-expression
4233 postfix-expression . pseudo-destructor-name
4234 postfix-expression -> pseudo-destructor-name
4235 postfix-expression ++
4236 postfix-expression --
4237 dynamic_cast < type-id > ( expression )
4238 static_cast < type-id > ( expression )
4239 reinterpret_cast < type-id > ( expression )
4240 const_cast < type-id > ( expression )
4241 typeid ( expression )
4247 ( type-id ) { initializer-list , [opt] }
4249 This extension is a GNU version of the C99 compound-literal
4250 construct. (The C99 grammar uses `type-name' instead of `type-id',
4251 but they are essentially the same concept.)
4253 If ADDRESS_P is true, the postfix expression is the operand of the
4254 `&' operator. CAST_P is true if this expression is the target of a
4257 Returns a representation of the expression. */
4260 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4264 cp_id_kind idk = CP_ID_KIND_NONE;
4265 tree postfix_expression = NULL_TREE;
4267 /* Peek at the next token. */
4268 token = cp_lexer_peek_token (parser->lexer);
4269 /* Some of the productions are determined by keywords. */
4270 keyword = token->keyword;
4280 const char *saved_message;
4282 /* All of these can be handled in the same way from the point
4283 of view of parsing. Begin by consuming the token
4284 identifying the cast. */
4285 cp_lexer_consume_token (parser->lexer);
4287 /* New types cannot be defined in the cast. */
4288 saved_message = parser->type_definition_forbidden_message;
4289 parser->type_definition_forbidden_message
4290 = "types may not be defined in casts";
4292 /* Look for the opening `<'. */
4293 cp_parser_require (parser, CPP_LESS, "`<'");
4294 /* Parse the type to which we are casting. */
4295 type = cp_parser_type_id (parser);
4296 /* Look for the closing `>'. */
4297 cp_parser_require (parser, CPP_GREATER, "`>'");
4298 /* Restore the old message. */
4299 parser->type_definition_forbidden_message = saved_message;
4301 /* And the expression which is being cast. */
4302 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4303 expression = cp_parser_expression (parser, /*cast_p=*/true);
4304 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4306 /* Only type conversions to integral or enumeration types
4307 can be used in constant-expressions. */
4308 if (!cast_valid_in_integral_constant_expression_p (type)
4309 && (cp_parser_non_integral_constant_expression
4311 "a cast to a type other than an integral or "
4312 "enumeration type")))
4313 return error_mark_node;
4319 = build_dynamic_cast (type, expression);
4323 = build_static_cast (type, expression);
4327 = build_reinterpret_cast (type, expression);
4331 = build_const_cast (type, expression);
4342 const char *saved_message;
4343 bool saved_in_type_id_in_expr_p;
4345 /* Consume the `typeid' token. */
4346 cp_lexer_consume_token (parser->lexer);
4347 /* Look for the `(' token. */
4348 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4349 /* Types cannot be defined in a `typeid' expression. */
4350 saved_message = parser->type_definition_forbidden_message;
4351 parser->type_definition_forbidden_message
4352 = "types may not be defined in a `typeid\' expression";
4353 /* We can't be sure yet whether we're looking at a type-id or an
4355 cp_parser_parse_tentatively (parser);
4356 /* Try a type-id first. */
4357 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4358 parser->in_type_id_in_expr_p = true;
4359 type = cp_parser_type_id (parser);
4360 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4361 /* Look for the `)' token. Otherwise, we can't be sure that
4362 we're not looking at an expression: consider `typeid (int
4363 (3))', for example. */
4364 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4365 /* If all went well, simply lookup the type-id. */
4366 if (cp_parser_parse_definitely (parser))
4367 postfix_expression = get_typeid (type);
4368 /* Otherwise, fall back to the expression variant. */
4373 /* Look for an expression. */
4374 expression = cp_parser_expression (parser, /*cast_p=*/false);
4375 /* Compute its typeid. */
4376 postfix_expression = build_typeid (expression);
4377 /* Look for the `)' token. */
4378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4380 /* Restore the saved message. */
4381 parser->type_definition_forbidden_message = saved_message;
4382 /* `typeid' may not appear in an integral constant expression. */
4383 if (cp_parser_non_integral_constant_expression(parser,
4384 "`typeid' operator"))
4385 return error_mark_node;
4392 /* The syntax permitted here is the same permitted for an
4393 elaborated-type-specifier. */
4394 type = cp_parser_elaborated_type_specifier (parser,
4395 /*is_friend=*/false,
4396 /*is_declaration=*/false);
4397 postfix_expression = cp_parser_functional_cast (parser, type);
4405 /* If the next thing is a simple-type-specifier, we may be
4406 looking at a functional cast. We could also be looking at
4407 an id-expression. So, we try the functional cast, and if
4408 that doesn't work we fall back to the primary-expression. */
4409 cp_parser_parse_tentatively (parser);
4410 /* Look for the simple-type-specifier. */
4411 type = cp_parser_simple_type_specifier (parser,
4412 /*decl_specs=*/NULL,
4413 CP_PARSER_FLAGS_NONE);
4414 /* Parse the cast itself. */
4415 if (!cp_parser_error_occurred (parser))
4417 = cp_parser_functional_cast (parser, type);
4418 /* If that worked, we're done. */
4419 if (cp_parser_parse_definitely (parser))
4422 /* If the functional-cast didn't work out, try a
4423 compound-literal. */
4424 if (cp_parser_allow_gnu_extensions_p (parser)
4425 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4427 VEC(constructor_elt,gc) *initializer_list = NULL;
4428 bool saved_in_type_id_in_expr_p;
4430 cp_parser_parse_tentatively (parser);
4431 /* Consume the `('. */
4432 cp_lexer_consume_token (parser->lexer);
4433 /* Parse the type. */
4434 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4435 parser->in_type_id_in_expr_p = true;
4436 type = cp_parser_type_id (parser);
4437 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4438 /* Look for the `)'. */
4439 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4440 /* Look for the `{'. */
4441 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4442 /* If things aren't going well, there's no need to
4444 if (!cp_parser_error_occurred (parser))
4446 bool non_constant_p;
4447 /* Parse the initializer-list. */
4449 = cp_parser_initializer_list (parser, &non_constant_p);
4450 /* Allow a trailing `,'. */
4451 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4452 cp_lexer_consume_token (parser->lexer);
4453 /* Look for the final `}'. */
4454 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4456 /* If that worked, we're definitely looking at a
4457 compound-literal expression. */
4458 if (cp_parser_parse_definitely (parser))
4460 /* Warn the user that a compound literal is not
4461 allowed in standard C++. */
4463 pedwarn ("ISO C++ forbids compound-literals");
4464 /* For simplicity, we disallow compound literals in
4465 constant-expressions. We could
4466 allow compound literals of integer type, whose
4467 initializer was a constant, in constant
4468 expressions. Permitting that usage, as a further
4469 extension, would not change the meaning of any
4470 currently accepted programs. (Of course, as
4471 compound literals are not part of ISO C++, the
4472 standard has nothing to say.) */
4473 if (cp_parser_non_integral_constant_expression
4474 (parser, "non-constant compound literals"))
4476 postfix_expression = error_mark_node;
4479 /* Form the representation of the compound-literal. */
4481 = finish_compound_literal (type, initializer_list);
4486 /* It must be a primary-expression. */
4488 = cp_parser_primary_expression (parser, address_p, cast_p,
4489 /*template_arg_p=*/false,
4495 /* Keep looping until the postfix-expression is complete. */
4498 if (idk == CP_ID_KIND_UNQUALIFIED
4499 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4500 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4501 /* It is not a Koenig lookup function call. */
4503 = unqualified_name_lookup_error (postfix_expression);
4505 /* Peek at the next token. */
4506 token = cp_lexer_peek_token (parser->lexer);
4508 switch (token->type)
4510 case CPP_OPEN_SQUARE:
4512 = cp_parser_postfix_open_square_expression (parser,
4515 idk = CP_ID_KIND_NONE;
4518 case CPP_OPEN_PAREN:
4519 /* postfix-expression ( expression-list [opt] ) */
4522 bool is_builtin_constant_p;
4523 bool saved_integral_constant_expression_p = false;
4524 bool saved_non_integral_constant_expression_p = false;
4527 is_builtin_constant_p
4528 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4529 if (is_builtin_constant_p)
4531 /* The whole point of __builtin_constant_p is to allow
4532 non-constant expressions to appear as arguments. */
4533 saved_integral_constant_expression_p
4534 = parser->integral_constant_expression_p;
4535 saved_non_integral_constant_expression_p
4536 = parser->non_integral_constant_expression_p;
4537 parser->integral_constant_expression_p = false;
4539 args = (cp_parser_parenthesized_expression_list
4540 (parser, /*is_attribute_list=*/false,
4541 /*cast_p=*/false, /*allow_expansion_p=*/true,
4542 /*non_constant_p=*/NULL));
4543 if (is_builtin_constant_p)
4545 parser->integral_constant_expression_p
4546 = saved_integral_constant_expression_p;
4547 parser->non_integral_constant_expression_p
4548 = saved_non_integral_constant_expression_p;
4551 if (args == error_mark_node)
4553 postfix_expression = error_mark_node;
4557 /* Function calls are not permitted in
4558 constant-expressions. */
4559 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4560 && cp_parser_non_integral_constant_expression (parser,
4563 postfix_expression = error_mark_node;
4568 if (idk == CP_ID_KIND_UNQUALIFIED)
4570 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4576 = perform_koenig_lookup (postfix_expression, args);
4580 = unqualified_fn_lookup_error (postfix_expression);
4582 /* We do not perform argument-dependent lookup if
4583 normal lookup finds a non-function, in accordance
4584 with the expected resolution of DR 218. */
4585 else if (args && is_overloaded_fn (postfix_expression))
4587 tree fn = get_first_fn (postfix_expression);
4589 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4590 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4592 /* Only do argument dependent lookup if regular
4593 lookup does not find a set of member functions.
4594 [basic.lookup.koenig]/2a */
4595 if (!DECL_FUNCTION_MEMBER_P (fn))
4599 = perform_koenig_lookup (postfix_expression, args);
4604 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4606 tree instance = TREE_OPERAND (postfix_expression, 0);
4607 tree fn = TREE_OPERAND (postfix_expression, 1);
4609 if (processing_template_decl
4610 && (type_dependent_expression_p (instance)
4611 || (!BASELINK_P (fn)
4612 && TREE_CODE (fn) != FIELD_DECL)
4613 || type_dependent_expression_p (fn)
4614 || any_type_dependent_arguments_p (args)))
4617 = build_nt_call_list (postfix_expression, args);
4621 if (BASELINK_P (fn))
4623 = (build_new_method_call
4624 (instance, fn, args, NULL_TREE,
4625 (idk == CP_ID_KIND_QUALIFIED
4626 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4630 = finish_call_expr (postfix_expression, args,
4631 /*disallow_virtual=*/false,
4632 /*koenig_p=*/false);
4634 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4635 || TREE_CODE (postfix_expression) == MEMBER_REF
4636 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4637 postfix_expression = (build_offset_ref_call_from_tree
4638 (postfix_expression, args));
4639 else if (idk == CP_ID_KIND_QUALIFIED)
4640 /* A call to a static class member, or a namespace-scope
4643 = finish_call_expr (postfix_expression, args,
4644 /*disallow_virtual=*/true,
4647 /* All other function calls. */
4649 = finish_call_expr (postfix_expression, args,
4650 /*disallow_virtual=*/false,
4653 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4654 idk = CP_ID_KIND_NONE;
4660 /* postfix-expression . template [opt] id-expression
4661 postfix-expression . pseudo-destructor-name
4662 postfix-expression -> template [opt] id-expression
4663 postfix-expression -> pseudo-destructor-name */
4665 /* Consume the `.' or `->' operator. */
4666 cp_lexer_consume_token (parser->lexer);
4669 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4675 /* postfix-expression ++ */
4676 /* Consume the `++' token. */
4677 cp_lexer_consume_token (parser->lexer);
4678 /* Generate a representation for the complete expression. */
4680 = finish_increment_expr (postfix_expression,
4681 POSTINCREMENT_EXPR);
4682 /* Increments may not appear in constant-expressions. */
4683 if (cp_parser_non_integral_constant_expression (parser,
4685 postfix_expression = error_mark_node;
4686 idk = CP_ID_KIND_NONE;
4689 case CPP_MINUS_MINUS:
4690 /* postfix-expression -- */
4691 /* Consume the `--' token. */
4692 cp_lexer_consume_token (parser->lexer);
4693 /* Generate a representation for the complete expression. */
4695 = finish_increment_expr (postfix_expression,
4696 POSTDECREMENT_EXPR);
4697 /* Decrements may not appear in constant-expressions. */
4698 if (cp_parser_non_integral_constant_expression (parser,
4700 postfix_expression = error_mark_node;
4701 idk = CP_ID_KIND_NONE;
4705 return postfix_expression;
4709 /* We should never get here. */
4711 return error_mark_node;
4714 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4715 by cp_parser_builtin_offsetof. We're looking for
4717 postfix-expression [ expression ]
4719 FOR_OFFSETOF is set if we're being called in that context, which
4720 changes how we deal with integer constant expressions. */
4723 cp_parser_postfix_open_square_expression (cp_parser *parser,
4724 tree postfix_expression,
4729 /* Consume the `[' token. */
4730 cp_lexer_consume_token (parser->lexer);
4732 /* Parse the index expression. */
4733 /* ??? For offsetof, there is a question of what to allow here. If
4734 offsetof is not being used in an integral constant expression context,
4735 then we *could* get the right answer by computing the value at runtime.
4736 If we are in an integral constant expression context, then we might
4737 could accept any constant expression; hard to say without analysis.
4738 Rather than open the barn door too wide right away, allow only integer
4739 constant expressions here. */
4741 index = cp_parser_constant_expression (parser, false, NULL);
4743 index = cp_parser_expression (parser, /*cast_p=*/false);
4745 /* Look for the closing `]'. */
4746 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4748 /* Build the ARRAY_REF. */
4749 postfix_expression = grok_array_decl (postfix_expression, index);
4751 /* When not doing offsetof, array references are not permitted in
4752 constant-expressions. */
4754 && (cp_parser_non_integral_constant_expression
4755 (parser, "an array reference")))
4756 postfix_expression = error_mark_node;
4758 return postfix_expression;
4761 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4762 by cp_parser_builtin_offsetof. We're looking for
4764 postfix-expression . template [opt] id-expression
4765 postfix-expression . pseudo-destructor-name
4766 postfix-expression -> template [opt] id-expression
4767 postfix-expression -> pseudo-destructor-name
4769 FOR_OFFSETOF is set if we're being called in that context. That sorta
4770 limits what of the above we'll actually accept, but nevermind.
4771 TOKEN_TYPE is the "." or "->" token, which will already have been
4772 removed from the stream. */
4775 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4776 enum cpp_ttype token_type,
4777 tree postfix_expression,
4778 bool for_offsetof, cp_id_kind *idk)
4782 bool pseudo_destructor_p;
4783 tree scope = NULL_TREE;
4785 /* If this is a `->' operator, dereference the pointer. */
4786 if (token_type == CPP_DEREF)
4787 postfix_expression = build_x_arrow (postfix_expression);
4788 /* Check to see whether or not the expression is type-dependent. */
4789 dependent_p = type_dependent_expression_p (postfix_expression);
4790 /* The identifier following the `->' or `.' is not qualified. */
4791 parser->scope = NULL_TREE;
4792 parser->qualifying_scope = NULL_TREE;
4793 parser->object_scope = NULL_TREE;
4794 *idk = CP_ID_KIND_NONE;
4795 /* Enter the scope corresponding to the type of the object
4796 given by the POSTFIX_EXPRESSION. */
4797 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4799 scope = TREE_TYPE (postfix_expression);
4800 /* According to the standard, no expression should ever have
4801 reference type. Unfortunately, we do not currently match
4802 the standard in this respect in that our internal representation
4803 of an expression may have reference type even when the standard
4804 says it does not. Therefore, we have to manually obtain the
4805 underlying type here. */
4806 scope = non_reference (scope);
4807 /* The type of the POSTFIX_EXPRESSION must be complete. */
4808 if (scope == unknown_type_node)
4810 error ("%qE does not have class type", postfix_expression);
4814 scope = complete_type_or_else (scope, NULL_TREE);
4815 /* Let the name lookup machinery know that we are processing a
4816 class member access expression. */
4817 parser->context->object_type = scope;
4818 /* If something went wrong, we want to be able to discern that case,
4819 as opposed to the case where there was no SCOPE due to the type
4820 of expression being dependent. */
4822 scope = error_mark_node;
4823 /* If the SCOPE was erroneous, make the various semantic analysis
4824 functions exit quickly -- and without issuing additional error
4826 if (scope == error_mark_node)
4827 postfix_expression = error_mark_node;
4830 /* Assume this expression is not a pseudo-destructor access. */
4831 pseudo_destructor_p = false;
4833 /* If the SCOPE is a scalar type, then, if this is a valid program,
4834 we must be looking at a pseudo-destructor-name. */
4835 if (scope && SCALAR_TYPE_P (scope))
4840 cp_parser_parse_tentatively (parser);
4841 /* Parse the pseudo-destructor-name. */
4843 cp_parser_pseudo_destructor_name (parser, &s, &type);
4844 if (cp_parser_parse_definitely (parser))
4846 pseudo_destructor_p = true;
4848 = finish_pseudo_destructor_expr (postfix_expression,
4849 s, TREE_TYPE (type));
4853 if (!pseudo_destructor_p)
4855 /* If the SCOPE is not a scalar type, we are looking at an
4856 ordinary class member access expression, rather than a
4857 pseudo-destructor-name. */
4859 /* Parse the id-expression. */
4860 name = (cp_parser_id_expression
4862 cp_parser_optional_template_keyword (parser),
4863 /*check_dependency_p=*/true,
4865 /*declarator_p=*/false,
4866 /*optional_p=*/false));
4867 /* In general, build a SCOPE_REF if the member name is qualified.
4868 However, if the name was not dependent and has already been
4869 resolved; there is no need to build the SCOPE_REF. For example;
4871 struct X { void f(); };
4872 template <typename T> void f(T* t) { t->X::f(); }
4874 Even though "t" is dependent, "X::f" is not and has been resolved
4875 to a BASELINK; there is no need to include scope information. */
4877 /* But we do need to remember that there was an explicit scope for
4878 virtual function calls. */
4880 *idk = CP_ID_KIND_QUALIFIED;
4882 /* If the name is a template-id that names a type, we will get a
4883 TYPE_DECL here. That is invalid code. */
4884 if (TREE_CODE (name) == TYPE_DECL)
4886 error ("invalid use of %qD", name);
4887 postfix_expression = error_mark_node;
4891 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4893 name = build_qualified_name (/*type=*/NULL_TREE,
4897 parser->scope = NULL_TREE;
4898 parser->qualifying_scope = NULL_TREE;
4899 parser->object_scope = NULL_TREE;
4901 if (scope && name && BASELINK_P (name))
4902 adjust_result_of_qualified_name_lookup
4903 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4905 = finish_class_member_access_expr (postfix_expression, name,
4910 /* We no longer need to look up names in the scope of the object on
4911 the left-hand side of the `.' or `->' operator. */
4912 parser->context->object_type = NULL_TREE;
4914 /* Outside of offsetof, these operators may not appear in
4915 constant-expressions. */
4917 && (cp_parser_non_integral_constant_expression
4918 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4919 postfix_expression = error_mark_node;
4921 return postfix_expression;
4924 /* Parse a parenthesized expression-list.
4927 assignment-expression
4928 expression-list, assignment-expression
4933 identifier, expression-list
4935 CAST_P is true if this expression is the target of a cast.
4937 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4940 Returns a TREE_LIST. The TREE_VALUE of each node is a
4941 representation of an assignment-expression. Note that a TREE_LIST
4942 is returned even if there is only a single expression in the list.
4943 error_mark_node is returned if the ( and or ) are
4944 missing. NULL_TREE is returned on no expressions. The parentheses
4945 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4946 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4947 indicates whether or not all of the expressions in the list were
4951 cp_parser_parenthesized_expression_list (cp_parser* parser,
4952 bool is_attribute_list,
4954 bool allow_expansion_p,
4955 bool *non_constant_p)
4957 tree expression_list = NULL_TREE;
4958 bool fold_expr_p = is_attribute_list;
4959 tree identifier = NULL_TREE;
4961 /* Assume all the expressions will be constant. */
4963 *non_constant_p = false;
4965 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4966 return error_mark_node;
4968 /* Consume expressions until there are no more. */
4969 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4974 /* At the beginning of attribute lists, check to see if the
4975 next token is an identifier. */
4976 if (is_attribute_list
4977 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4981 /* Consume the identifier. */
4982 token = cp_lexer_consume_token (parser->lexer);
4983 /* Save the identifier. */
4984 identifier = token->u.value;
4988 /* Parse the next assignment-expression. */
4991 bool expr_non_constant_p;
4992 expr = (cp_parser_constant_expression
4993 (parser, /*allow_non_constant_p=*/true,
4994 &expr_non_constant_p));
4995 if (expr_non_constant_p)
4996 *non_constant_p = true;
4999 expr = cp_parser_assignment_expression (parser, cast_p);
5002 expr = fold_non_dependent_expr (expr);
5004 /* If we have an ellipsis, then this is an expression
5006 if (allow_expansion_p
5007 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5009 /* Consume the `...'. */
5010 cp_lexer_consume_token (parser->lexer);
5012 /* Build the argument pack. */
5013 expr = make_pack_expansion (expr);
5016 /* Add it to the list. We add error_mark_node
5017 expressions to the list, so that we can still tell if
5018 the correct form for a parenthesized expression-list
5019 is found. That gives better errors. */
5020 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5022 if (expr == error_mark_node)
5026 /* After the first item, attribute lists look the same as
5027 expression lists. */
5028 is_attribute_list = false;
5031 /* If the next token isn't a `,', then we are done. */
5032 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5035 /* Otherwise, consume the `,' and keep going. */
5036 cp_lexer_consume_token (parser->lexer);
5039 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5044 /* We try and resync to an unnested comma, as that will give the
5045 user better diagnostics. */
5046 ending = cp_parser_skip_to_closing_parenthesis (parser,
5047 /*recovering=*/true,
5049 /*consume_paren=*/true);
5053 return error_mark_node;
5056 /* We built up the list in reverse order so we must reverse it now. */
5057 expression_list = nreverse (expression_list);
5059 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5061 return expression_list;
5064 /* Parse a pseudo-destructor-name.
5066 pseudo-destructor-name:
5067 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5068 :: [opt] nested-name-specifier template template-id :: ~ type-name
5069 :: [opt] nested-name-specifier [opt] ~ type-name
5071 If either of the first two productions is used, sets *SCOPE to the
5072 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5073 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5074 or ERROR_MARK_NODE if the parse fails. */
5077 cp_parser_pseudo_destructor_name (cp_parser* parser,
5081 bool nested_name_specifier_p;
5083 /* Assume that things will not work out. */
5084 *type = error_mark_node;
5086 /* Look for the optional `::' operator. */
5087 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5088 /* Look for the optional nested-name-specifier. */
5089 nested_name_specifier_p
5090 = (cp_parser_nested_name_specifier_opt (parser,
5091 /*typename_keyword_p=*/false,
5092 /*check_dependency_p=*/true,
5094 /*is_declaration=*/true)
5096 /* Now, if we saw a nested-name-specifier, we might be doing the
5097 second production. */
5098 if (nested_name_specifier_p
5099 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5101 /* Consume the `template' keyword. */
5102 cp_lexer_consume_token (parser->lexer);
5103 /* Parse the template-id. */
5104 cp_parser_template_id (parser,
5105 /*template_keyword_p=*/true,
5106 /*check_dependency_p=*/false,
5107 /*is_declaration=*/true);
5108 /* Look for the `::' token. */
5109 cp_parser_require (parser, CPP_SCOPE, "`::'");
5111 /* If the next token is not a `~', then there might be some
5112 additional qualification. */
5113 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5115 /* Look for the type-name. */
5116 *scope = TREE_TYPE (cp_parser_type_name (parser));
5118 if (*scope == error_mark_node)
5121 /* If we don't have ::~, then something has gone wrong. Since
5122 the only caller of this function is looking for something
5123 after `.' or `->' after a scalar type, most likely the
5124 program is trying to get a member of a non-aggregate
5126 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5127 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5129 cp_parser_error (parser, "request for member of non-aggregate type");
5133 /* Look for the `::' token. */
5134 cp_parser_require (parser, CPP_SCOPE, "`::'");
5139 /* Look for the `~'. */
5140 cp_parser_require (parser, CPP_COMPL, "`~'");
5141 /* Look for the type-name again. We are not responsible for
5142 checking that it matches the first type-name. */
5143 *type = cp_parser_type_name (parser);
5146 /* Parse a unary-expression.
5152 unary-operator cast-expression
5153 sizeof unary-expression
5161 __extension__ cast-expression
5162 __alignof__ unary-expression
5163 __alignof__ ( type-id )
5164 __real__ cast-expression
5165 __imag__ cast-expression
5168 ADDRESS_P is true iff the unary-expression is appearing as the
5169 operand of the `&' operator. CAST_P is true if this expression is
5170 the target of a cast.
5172 Returns a representation of the expression. */
5175 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5178 enum tree_code unary_operator;
5180 /* Peek at the next token. */
5181 token = cp_lexer_peek_token (parser->lexer);
5182 /* Some keywords give away the kind of expression. */
5183 if (token->type == CPP_KEYWORD)
5185 enum rid keyword = token->keyword;
5195 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5196 /* Consume the token. */
5197 cp_lexer_consume_token (parser->lexer);
5198 /* Parse the operand. */
5199 operand = cp_parser_sizeof_operand (parser, keyword);
5201 if (TYPE_P (operand))
5202 return cxx_sizeof_or_alignof_type (operand, op, true);
5204 return cxx_sizeof_or_alignof_expr (operand, op);
5208 return cp_parser_new_expression (parser);
5211 return cp_parser_delete_expression (parser);
5215 /* The saved value of the PEDANTIC flag. */
5219 /* Save away the PEDANTIC flag. */
5220 cp_parser_extension_opt (parser, &saved_pedantic);
5221 /* Parse the cast-expression. */
5222 expr = cp_parser_simple_cast_expression (parser);
5223 /* Restore the PEDANTIC flag. */
5224 pedantic = saved_pedantic;
5234 /* Consume the `__real__' or `__imag__' token. */
5235 cp_lexer_consume_token (parser->lexer);
5236 /* Parse the cast-expression. */
5237 expression = cp_parser_simple_cast_expression (parser);
5238 /* Create the complete representation. */
5239 return build_x_unary_op ((keyword == RID_REALPART
5240 ? REALPART_EXPR : IMAGPART_EXPR),
5250 /* Look for the `:: new' and `:: delete', which also signal the
5251 beginning of a new-expression, or delete-expression,
5252 respectively. If the next token is `::', then it might be one of
5254 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5258 /* See if the token after the `::' is one of the keywords in
5259 which we're interested. */
5260 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5261 /* If it's `new', we have a new-expression. */
5262 if (keyword == RID_NEW)
5263 return cp_parser_new_expression (parser);
5264 /* Similarly, for `delete'. */
5265 else if (keyword == RID_DELETE)
5266 return cp_parser_delete_expression (parser);
5269 /* Look for a unary operator. */
5270 unary_operator = cp_parser_unary_operator (token);
5271 /* The `++' and `--' operators can be handled similarly, even though
5272 they are not technically unary-operators in the grammar. */
5273 if (unary_operator == ERROR_MARK)
5275 if (token->type == CPP_PLUS_PLUS)
5276 unary_operator = PREINCREMENT_EXPR;
5277 else if (token->type == CPP_MINUS_MINUS)
5278 unary_operator = PREDECREMENT_EXPR;
5279 /* Handle the GNU address-of-label extension. */
5280 else if (cp_parser_allow_gnu_extensions_p (parser)
5281 && token->type == CPP_AND_AND)
5285 /* Consume the '&&' token. */
5286 cp_lexer_consume_token (parser->lexer);
5287 /* Look for the identifier. */
5288 identifier = cp_parser_identifier (parser);
5289 /* Create an expression representing the address. */
5290 return finish_label_address_expr (identifier);
5293 if (unary_operator != ERROR_MARK)
5295 tree cast_expression;
5296 tree expression = error_mark_node;
5297 const char *non_constant_p = NULL;
5299 /* Consume the operator token. */
5300 token = cp_lexer_consume_token (parser->lexer);
5301 /* Parse the cast-expression. */
5303 = cp_parser_cast_expression (parser,
5304 unary_operator == ADDR_EXPR,
5306 /* Now, build an appropriate representation. */
5307 switch (unary_operator)
5310 non_constant_p = "`*'";
5311 expression = build_x_indirect_ref (cast_expression, "unary *");
5315 non_constant_p = "`&'";
5318 expression = build_x_unary_op (unary_operator, cast_expression);
5321 case PREINCREMENT_EXPR:
5322 case PREDECREMENT_EXPR:
5323 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5326 case UNARY_PLUS_EXPR:
5328 case TRUTH_NOT_EXPR:
5329 expression = finish_unary_op_expr (unary_operator, cast_expression);
5337 && cp_parser_non_integral_constant_expression (parser,
5339 expression = error_mark_node;
5344 return cp_parser_postfix_expression (parser, address_p, cast_p);
5347 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5348 unary-operator, the corresponding tree code is returned. */
5350 static enum tree_code
5351 cp_parser_unary_operator (cp_token* token)
5353 switch (token->type)
5356 return INDIRECT_REF;
5362 return UNARY_PLUS_EXPR;
5368 return TRUTH_NOT_EXPR;
5371 return BIT_NOT_EXPR;
5378 /* Parse a new-expression.
5381 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5382 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5384 Returns a representation of the expression. */
5387 cp_parser_new_expression (cp_parser* parser)
5389 bool global_scope_p;
5395 /* Look for the optional `::' operator. */
5397 = (cp_parser_global_scope_opt (parser,
5398 /*current_scope_valid_p=*/false)
5400 /* Look for the `new' operator. */
5401 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5402 /* There's no easy way to tell a new-placement from the
5403 `( type-id )' construct. */
5404 cp_parser_parse_tentatively (parser);
5405 /* Look for a new-placement. */
5406 placement = cp_parser_new_placement (parser);
5407 /* If that didn't work out, there's no new-placement. */
5408 if (!cp_parser_parse_definitely (parser))
5409 placement = NULL_TREE;
5411 /* If the next token is a `(', then we have a parenthesized
5413 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5415 /* Consume the `('. */
5416 cp_lexer_consume_token (parser->lexer);
5417 /* Parse the type-id. */
5418 type = cp_parser_type_id (parser);
5419 /* Look for the closing `)'. */
5420 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5421 /* There should not be a direct-new-declarator in this production,
5422 but GCC used to allowed this, so we check and emit a sensible error
5423 message for this case. */
5424 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5426 error ("array bound forbidden after parenthesized type-id");
5427 inform ("try removing the parentheses around the type-id");
5428 cp_parser_direct_new_declarator (parser);
5432 /* Otherwise, there must be a new-type-id. */
5434 type = cp_parser_new_type_id (parser, &nelts);
5436 /* If the next token is a `(', then we have a new-initializer. */
5437 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5438 initializer = cp_parser_new_initializer (parser);
5440 initializer = NULL_TREE;
5442 /* A new-expression may not appear in an integral constant
5444 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5445 return error_mark_node;
5447 /* Create a representation of the new-expression. */
5448 return build_new (placement, type, nelts, initializer, global_scope_p);
5451 /* Parse a new-placement.
5456 Returns the same representation as for an expression-list. */
5459 cp_parser_new_placement (cp_parser* parser)
5461 tree expression_list;
5463 /* Parse the expression-list. */
5464 expression_list = (cp_parser_parenthesized_expression_list
5465 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5466 /*non_constant_p=*/NULL));
5468 return expression_list;
5471 /* Parse a new-type-id.
5474 type-specifier-seq new-declarator [opt]
5476 Returns the TYPE allocated. If the new-type-id indicates an array
5477 type, *NELTS is set to the number of elements in the last array
5478 bound; the TYPE will not include the last array bound. */
5481 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5483 cp_decl_specifier_seq type_specifier_seq;
5484 cp_declarator *new_declarator;
5485 cp_declarator *declarator;
5486 cp_declarator *outer_declarator;
5487 const char *saved_message;
5490 /* The type-specifier sequence must not contain type definitions.
5491 (It cannot contain declarations of new types either, but if they
5492 are not definitions we will catch that because they are not
5494 saved_message = parser->type_definition_forbidden_message;
5495 parser->type_definition_forbidden_message
5496 = "types may not be defined in a new-type-id";
5497 /* Parse the type-specifier-seq. */
5498 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5499 &type_specifier_seq);
5500 /* Restore the old message. */
5501 parser->type_definition_forbidden_message = saved_message;
5502 /* Parse the new-declarator. */
5503 new_declarator = cp_parser_new_declarator_opt (parser);
5505 /* Determine the number of elements in the last array dimension, if
5508 /* Skip down to the last array dimension. */
5509 declarator = new_declarator;
5510 outer_declarator = NULL;
5511 while (declarator && (declarator->kind == cdk_pointer
5512 || declarator->kind == cdk_ptrmem))
5514 outer_declarator = declarator;
5515 declarator = declarator->declarator;
5518 && declarator->kind == cdk_array
5519 && declarator->declarator
5520 && declarator->declarator->kind == cdk_array)
5522 outer_declarator = declarator;
5523 declarator = declarator->declarator;
5526 if (declarator && declarator->kind == cdk_array)
5528 *nelts = declarator->u.array.bounds;
5529 if (*nelts == error_mark_node)
5530 *nelts = integer_one_node;
5532 if (outer_declarator)
5533 outer_declarator->declarator = declarator->declarator;
5535 new_declarator = NULL;
5538 type = groktypename (&type_specifier_seq, new_declarator);
5542 /* Parse an (optional) new-declarator.
5545 ptr-operator new-declarator [opt]
5546 direct-new-declarator
5548 Returns the declarator. */
5550 static cp_declarator *
5551 cp_parser_new_declarator_opt (cp_parser* parser)
5553 enum tree_code code;
5555 cp_cv_quals cv_quals;
5557 /* We don't know if there's a ptr-operator next, or not. */
5558 cp_parser_parse_tentatively (parser);
5559 /* Look for a ptr-operator. */
5560 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5561 /* If that worked, look for more new-declarators. */
5562 if (cp_parser_parse_definitely (parser))
5564 cp_declarator *declarator;
5566 /* Parse another optional declarator. */
5567 declarator = cp_parser_new_declarator_opt (parser);
5569 return cp_parser_make_indirect_declarator
5570 (code, type, cv_quals, declarator);
5573 /* If the next token is a `[', there is a direct-new-declarator. */
5574 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5575 return cp_parser_direct_new_declarator (parser);
5580 /* Parse a direct-new-declarator.
5582 direct-new-declarator:
5584 direct-new-declarator [constant-expression]
5588 static cp_declarator *
5589 cp_parser_direct_new_declarator (cp_parser* parser)
5591 cp_declarator *declarator = NULL;
5597 /* Look for the opening `['. */
5598 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5599 /* The first expression is not required to be constant. */
5602 expression = cp_parser_expression (parser, /*cast_p=*/false);
5603 /* The standard requires that the expression have integral
5604 type. DR 74 adds enumeration types. We believe that the
5605 real intent is that these expressions be handled like the
5606 expression in a `switch' condition, which also allows
5607 classes with a single conversion to integral or
5608 enumeration type. */
5609 if (!processing_template_decl)
5612 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5617 error ("expression in new-declarator must have integral "
5618 "or enumeration type");
5619 expression = error_mark_node;
5623 /* But all the other expressions must be. */
5626 = cp_parser_constant_expression (parser,
5627 /*allow_non_constant=*/false,
5629 /* Look for the closing `]'. */
5630 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5632 /* Add this bound to the declarator. */
5633 declarator = make_array_declarator (declarator, expression);
5635 /* If the next token is not a `[', then there are no more
5637 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5644 /* Parse a new-initializer.
5647 ( expression-list [opt] )
5649 Returns a representation of the expression-list. If there is no
5650 expression-list, VOID_ZERO_NODE is returned. */
5653 cp_parser_new_initializer (cp_parser* parser)
5655 tree expression_list;
5657 expression_list = (cp_parser_parenthesized_expression_list
5658 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5659 /*non_constant_p=*/NULL));
5660 if (!expression_list)
5661 expression_list = void_zero_node;
5663 return expression_list;
5666 /* Parse a delete-expression.
5669 :: [opt] delete cast-expression
5670 :: [opt] delete [ ] cast-expression
5672 Returns a representation of the expression. */
5675 cp_parser_delete_expression (cp_parser* parser)
5677 bool global_scope_p;
5681 /* Look for the optional `::' operator. */
5683 = (cp_parser_global_scope_opt (parser,
5684 /*current_scope_valid_p=*/false)
5686 /* Look for the `delete' keyword. */
5687 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5688 /* See if the array syntax is in use. */
5689 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5691 /* Consume the `[' token. */
5692 cp_lexer_consume_token (parser->lexer);
5693 /* Look for the `]' token. */
5694 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5695 /* Remember that this is the `[]' construct. */
5701 /* Parse the cast-expression. */
5702 expression = cp_parser_simple_cast_expression (parser);
5704 /* A delete-expression may not appear in an integral constant
5706 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5707 return error_mark_node;
5709 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5712 /* Parse a cast-expression.
5716 ( type-id ) cast-expression
5718 ADDRESS_P is true iff the unary-expression is appearing as the
5719 operand of the `&' operator. CAST_P is true if this expression is
5720 the target of a cast.
5722 Returns a representation of the expression. */
5725 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5727 /* If it's a `(', then we might be looking at a cast. */
5728 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5730 tree type = NULL_TREE;
5731 tree expr = NULL_TREE;
5732 bool compound_literal_p;
5733 const char *saved_message;
5735 /* There's no way to know yet whether or not this is a cast.
5736 For example, `(int (3))' is a unary-expression, while `(int)
5737 3' is a cast. So, we resort to parsing tentatively. */
5738 cp_parser_parse_tentatively (parser);
5739 /* Types may not be defined in a cast. */
5740 saved_message = parser->type_definition_forbidden_message;
5741 parser->type_definition_forbidden_message
5742 = "types may not be defined in casts";
5743 /* Consume the `('. */
5744 cp_lexer_consume_token (parser->lexer);
5745 /* A very tricky bit is that `(struct S) { 3 }' is a
5746 compound-literal (which we permit in C++ as an extension).
5747 But, that construct is not a cast-expression -- it is a
5748 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5749 is legal; if the compound-literal were a cast-expression,
5750 you'd need an extra set of parentheses.) But, if we parse
5751 the type-id, and it happens to be a class-specifier, then we
5752 will commit to the parse at that point, because we cannot
5753 undo the action that is done when creating a new class. So,
5754 then we cannot back up and do a postfix-expression.
5756 Therefore, we scan ahead to the closing `)', and check to see
5757 if the token after the `)' is a `{'. If so, we are not
5758 looking at a cast-expression.
5760 Save tokens so that we can put them back. */
5761 cp_lexer_save_tokens (parser->lexer);
5762 /* Skip tokens until the next token is a closing parenthesis.
5763 If we find the closing `)', and the next token is a `{', then
5764 we are looking at a compound-literal. */
5766 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5767 /*consume_paren=*/true)
5768 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5769 /* Roll back the tokens we skipped. */
5770 cp_lexer_rollback_tokens (parser->lexer);
5771 /* If we were looking at a compound-literal, simulate an error
5772 so that the call to cp_parser_parse_definitely below will
5774 if (compound_literal_p)
5775 cp_parser_simulate_error (parser);
5778 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5779 parser->in_type_id_in_expr_p = true;
5780 /* Look for the type-id. */
5781 type = cp_parser_type_id (parser);
5782 /* Look for the closing `)'. */
5783 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5784 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5787 /* Restore the saved message. */
5788 parser->type_definition_forbidden_message = saved_message;
5790 /* If ok so far, parse the dependent expression. We cannot be
5791 sure it is a cast. Consider `(T ())'. It is a parenthesized
5792 ctor of T, but looks like a cast to function returning T
5793 without a dependent expression. */
5794 if (!cp_parser_error_occurred (parser))
5795 expr = cp_parser_cast_expression (parser,
5796 /*address_p=*/false,
5799 if (cp_parser_parse_definitely (parser))
5801 /* Warn about old-style casts, if so requested. */
5802 if (warn_old_style_cast
5803 && !in_system_header
5804 && !VOID_TYPE_P (type)
5805 && current_lang_name != lang_name_c)
5806 warning (OPT_Wold_style_cast, "use of old-style cast");
5808 /* Only type conversions to integral or enumeration types
5809 can be used in constant-expressions. */
5810 if (!cast_valid_in_integral_constant_expression_p (type)
5811 && (cp_parser_non_integral_constant_expression
5813 "a cast to a type other than an integral or "
5814 "enumeration type")))
5815 return error_mark_node;
5817 /* Perform the cast. */
5818 expr = build_c_cast (type, expr);
5823 /* If we get here, then it's not a cast, so it must be a
5824 unary-expression. */
5825 return cp_parser_unary_expression (parser, address_p, cast_p);
5828 /* Parse a binary expression of the general form:
5832 pm-expression .* cast-expression
5833 pm-expression ->* cast-expression
5835 multiplicative-expression:
5837 multiplicative-expression * pm-expression
5838 multiplicative-expression / pm-expression
5839 multiplicative-expression % pm-expression
5841 additive-expression:
5842 multiplicative-expression
5843 additive-expression + multiplicative-expression
5844 additive-expression - multiplicative-expression
5848 shift-expression << additive-expression
5849 shift-expression >> additive-expression
5851 relational-expression:
5853 relational-expression < shift-expression
5854 relational-expression > shift-expression
5855 relational-expression <= shift-expression
5856 relational-expression >= shift-expression
5860 relational-expression:
5861 relational-expression <? shift-expression
5862 relational-expression >? shift-expression
5864 equality-expression:
5865 relational-expression
5866 equality-expression == relational-expression
5867 equality-expression != relational-expression
5871 and-expression & equality-expression
5873 exclusive-or-expression:
5875 exclusive-or-expression ^ and-expression
5877 inclusive-or-expression:
5878 exclusive-or-expression
5879 inclusive-or-expression | exclusive-or-expression
5881 logical-and-expression:
5882 inclusive-or-expression
5883 logical-and-expression && inclusive-or-expression
5885 logical-or-expression:
5886 logical-and-expression
5887 logical-or-expression || logical-and-expression
5889 All these are implemented with a single function like:
5892 simple-cast-expression
5893 binary-expression <token> binary-expression
5895 CAST_P is true if this expression is the target of a cast.
5897 The binops_by_token map is used to get the tree codes for each <token> type.
5898 binary-expressions are associated according to a precedence table. */
5900 #define TOKEN_PRECEDENCE(token) \
5901 (((token->type == CPP_GREATER \
5902 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5903 && !parser->greater_than_is_operator_p) \
5904 ? PREC_NOT_OPERATOR \
5905 : binops_by_token[token->type].prec)
5908 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5910 cp_parser_expression_stack stack;
5911 cp_parser_expression_stack_entry *sp = &stack[0];
5914 enum tree_code tree_type, lhs_type, rhs_type;
5915 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5918 /* Parse the first expression. */
5919 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5920 lhs_type = ERROR_MARK;
5924 /* Get an operator token. */
5925 token = cp_lexer_peek_token (parser->lexer);
5927 if (warn_cxx0x_compat
5928 && token->type == CPP_RSHIFT
5929 && !parser->greater_than_is_operator_p)
5931 warning (OPT_Wc__0x_compat,
5932 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5934 warning (OPT_Wc__0x_compat,
5935 "suggest parentheses around %<>>%> expression");
5938 new_prec = TOKEN_PRECEDENCE (token);
5940 /* Popping an entry off the stack means we completed a subexpression:
5941 - either we found a token which is not an operator (`>' where it is not
5942 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5943 will happen repeatedly;
5944 - or, we found an operator which has lower priority. This is the case
5945 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5947 if (new_prec <= prec)
5956 tree_type = binops_by_token[token->type].tree_type;
5958 /* We used the operator token. */
5959 cp_lexer_consume_token (parser->lexer);
5961 /* Extract another operand. It may be the RHS of this expression
5962 or the LHS of a new, higher priority expression. */
5963 rhs = cp_parser_simple_cast_expression (parser);
5964 rhs_type = ERROR_MARK;
5966 /* Get another operator token. Look up its precedence to avoid
5967 building a useless (immediately popped) stack entry for common
5968 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5969 token = cp_lexer_peek_token (parser->lexer);
5970 lookahead_prec = TOKEN_PRECEDENCE (token);
5971 if (lookahead_prec > new_prec)
5973 /* ... and prepare to parse the RHS of the new, higher priority
5974 expression. Since precedence levels on the stack are
5975 monotonically increasing, we do not have to care about
5978 sp->tree_type = tree_type;
5980 sp->lhs_type = lhs_type;
5983 lhs_type = rhs_type;
5985 new_prec = lookahead_prec;
5989 /* If the stack is not empty, we have parsed into LHS the right side
5990 (`4' in the example above) of an expression we had suspended.
5991 We can use the information on the stack to recover the LHS (`3')
5992 from the stack together with the tree code (`MULT_EXPR'), and
5993 the precedence of the higher level subexpression
5994 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5995 which will be used to actually build the additive expression. */
5998 tree_type = sp->tree_type;
6000 rhs_type = lhs_type;
6002 lhs_type = sp->lhs_type;
6005 overloaded_p = false;
6006 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6008 lhs_type = tree_type;
6010 /* If the binary operator required the use of an overloaded operator,
6011 then this expression cannot be an integral constant-expression.
6012 An overloaded operator can be used even if both operands are
6013 otherwise permissible in an integral constant-expression if at
6014 least one of the operands is of enumeration type. */
6017 && (cp_parser_non_integral_constant_expression
6018 (parser, "calls to overloaded operators")))
6019 return error_mark_node;
6026 /* Parse the `? expression : assignment-expression' part of a
6027 conditional-expression. The LOGICAL_OR_EXPR is the
6028 logical-or-expression that started the conditional-expression.
6029 Returns a representation of the entire conditional-expression.
6031 This routine is used by cp_parser_assignment_expression.
6033 ? expression : assignment-expression
6037 ? : assignment-expression */
6040 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6043 tree assignment_expr;
6045 /* Consume the `?' token. */
6046 cp_lexer_consume_token (parser->lexer);
6047 if (cp_parser_allow_gnu_extensions_p (parser)
6048 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6049 /* Implicit true clause. */
6052 /* Parse the expression. */
6053 expr = cp_parser_expression (parser, /*cast_p=*/false);
6055 /* The next token should be a `:'. */
6056 cp_parser_require (parser, CPP_COLON, "`:'");
6057 /* Parse the assignment-expression. */
6058 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6060 /* Build the conditional-expression. */
6061 return build_x_conditional_expr (logical_or_expr,
6066 /* Parse an assignment-expression.
6068 assignment-expression:
6069 conditional-expression
6070 logical-or-expression assignment-operator assignment_expression
6073 CAST_P is true if this expression is the target of a cast.
6075 Returns a representation for the expression. */
6078 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6082 /* If the next token is the `throw' keyword, then we're looking at
6083 a throw-expression. */
6084 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6085 expr = cp_parser_throw_expression (parser);
6086 /* Otherwise, it must be that we are looking at a
6087 logical-or-expression. */
6090 /* Parse the binary expressions (logical-or-expression). */
6091 expr = cp_parser_binary_expression (parser, cast_p);
6092 /* If the next token is a `?' then we're actually looking at a
6093 conditional-expression. */
6094 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6095 return cp_parser_question_colon_clause (parser, expr);
6098 enum tree_code assignment_operator;
6100 /* If it's an assignment-operator, we're using the second
6103 = cp_parser_assignment_operator_opt (parser);
6104 if (assignment_operator != ERROR_MARK)
6108 /* Parse the right-hand side of the assignment. */
6109 rhs = cp_parser_assignment_expression (parser, cast_p);
6110 /* An assignment may not appear in a
6111 constant-expression. */
6112 if (cp_parser_non_integral_constant_expression (parser,
6114 return error_mark_node;
6115 /* Build the assignment expression. */
6116 expr = build_x_modify_expr (expr,
6117 assignment_operator,
6126 /* Parse an (optional) assignment-operator.
6128 assignment-operator: one of
6129 = *= /= %= += -= >>= <<= &= ^= |=
6133 assignment-operator: one of
6136 If the next token is an assignment operator, the corresponding tree
6137 code is returned, and the token is consumed. For example, for
6138 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6139 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6140 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6141 operator, ERROR_MARK is returned. */
6143 static enum tree_code
6144 cp_parser_assignment_operator_opt (cp_parser* parser)
6149 /* Peek at the next toen. */
6150 token = cp_lexer_peek_token (parser->lexer);
6152 switch (token->type)
6163 op = TRUNC_DIV_EXPR;
6167 op = TRUNC_MOD_EXPR;
6199 /* Nothing else is an assignment operator. */
6203 /* If it was an assignment operator, consume it. */
6204 if (op != ERROR_MARK)
6205 cp_lexer_consume_token (parser->lexer);
6210 /* Parse an expression.
6213 assignment-expression
6214 expression , assignment-expression
6216 CAST_P is true if this expression is the target of a cast.
6218 Returns a representation of the expression. */
6221 cp_parser_expression (cp_parser* parser, bool cast_p)
6223 tree expression = NULL_TREE;
6227 tree assignment_expression;
6229 /* Parse the next assignment-expression. */
6230 assignment_expression
6231 = cp_parser_assignment_expression (parser, cast_p);
6232 /* If this is the first assignment-expression, we can just
6235 expression = assignment_expression;
6237 expression = build_x_compound_expr (expression,
6238 assignment_expression);
6239 /* If the next token is not a comma, then we are done with the
6241 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6243 /* Consume the `,'. */
6244 cp_lexer_consume_token (parser->lexer);
6245 /* A comma operator cannot appear in a constant-expression. */
6246 if (cp_parser_non_integral_constant_expression (parser,
6247 "a comma operator"))
6248 expression = error_mark_node;
6254 /* Parse a constant-expression.
6256 constant-expression:
6257 conditional-expression
6259 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6260 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6261 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6262 is false, NON_CONSTANT_P should be NULL. */
6265 cp_parser_constant_expression (cp_parser* parser,
6266 bool allow_non_constant_p,
6267 bool *non_constant_p)
6269 bool saved_integral_constant_expression_p;
6270 bool saved_allow_non_integral_constant_expression_p;
6271 bool saved_non_integral_constant_expression_p;
6274 /* It might seem that we could simply parse the
6275 conditional-expression, and then check to see if it were
6276 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6277 one that the compiler can figure out is constant, possibly after
6278 doing some simplifications or optimizations. The standard has a
6279 precise definition of constant-expression, and we must honor
6280 that, even though it is somewhat more restrictive.
6286 is not a legal declaration, because `(2, 3)' is not a
6287 constant-expression. The `,' operator is forbidden in a
6288 constant-expression. However, GCC's constant-folding machinery
6289 will fold this operation to an INTEGER_CST for `3'. */
6291 /* Save the old settings. */
6292 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6293 saved_allow_non_integral_constant_expression_p
6294 = parser->allow_non_integral_constant_expression_p;
6295 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6296 /* We are now parsing a constant-expression. */
6297 parser->integral_constant_expression_p = true;
6298 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6299 parser->non_integral_constant_expression_p = false;
6300 /* Although the grammar says "conditional-expression", we parse an
6301 "assignment-expression", which also permits "throw-expression"
6302 and the use of assignment operators. In the case that
6303 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6304 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6305 actually essential that we look for an assignment-expression.
6306 For example, cp_parser_initializer_clauses uses this function to
6307 determine whether a particular assignment-expression is in fact
6309 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6310 /* Restore the old settings. */
6311 parser->integral_constant_expression_p
6312 = saved_integral_constant_expression_p;
6313 parser->allow_non_integral_constant_expression_p
6314 = saved_allow_non_integral_constant_expression_p;
6315 if (allow_non_constant_p)
6316 *non_constant_p = parser->non_integral_constant_expression_p;
6317 else if (parser->non_integral_constant_expression_p)
6318 expression = error_mark_node;
6319 parser->non_integral_constant_expression_p
6320 = saved_non_integral_constant_expression_p;
6325 /* Parse __builtin_offsetof.
6327 offsetof-expression:
6328 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6330 offsetof-member-designator:
6332 | offsetof-member-designator "." id-expression
6333 | offsetof-member-designator "[" expression "]" */
6336 cp_parser_builtin_offsetof (cp_parser *parser)
6338 int save_ice_p, save_non_ice_p;
6342 /* We're about to accept non-integral-constant things, but will
6343 definitely yield an integral constant expression. Save and
6344 restore these values around our local parsing. */
6345 save_ice_p = parser->integral_constant_expression_p;
6346 save_non_ice_p = parser->non_integral_constant_expression_p;
6348 /* Consume the "__builtin_offsetof" token. */
6349 cp_lexer_consume_token (parser->lexer);
6350 /* Consume the opening `('. */
6351 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6352 /* Parse the type-id. */
6353 type = cp_parser_type_id (parser);
6354 /* Look for the `,'. */
6355 cp_parser_require (parser, CPP_COMMA, "`,'");
6357 /* Build the (type *)null that begins the traditional offsetof macro. */
6358 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6360 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6361 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6365 cp_token *token = cp_lexer_peek_token (parser->lexer);
6366 switch (token->type)
6368 case CPP_OPEN_SQUARE:
6369 /* offsetof-member-designator "[" expression "]" */
6370 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6374 /* offsetof-member-designator "." identifier */
6375 cp_lexer_consume_token (parser->lexer);
6376 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6380 case CPP_CLOSE_PAREN:
6381 /* Consume the ")" token. */
6382 cp_lexer_consume_token (parser->lexer);
6386 /* Error. We know the following require will fail, but
6387 that gives the proper error message. */
6388 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6389 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6390 expr = error_mark_node;
6396 /* If we're processing a template, we can't finish the semantics yet.
6397 Otherwise we can fold the entire expression now. */
6398 if (processing_template_decl)
6399 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6401 expr = finish_offsetof (expr);
6404 parser->integral_constant_expression_p = save_ice_p;
6405 parser->non_integral_constant_expression_p = save_non_ice_p;
6410 /* Parse a trait expression. */
6413 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6416 tree type1, type2 = NULL_TREE;
6417 bool binary = false;
6418 cp_decl_specifier_seq decl_specs;
6422 case RID_HAS_NOTHROW_ASSIGN:
6423 kind = CPTK_HAS_NOTHROW_ASSIGN;
6425 case RID_HAS_NOTHROW_CONSTRUCTOR:
6426 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6428 case RID_HAS_NOTHROW_COPY:
6429 kind = CPTK_HAS_NOTHROW_COPY;
6431 case RID_HAS_TRIVIAL_ASSIGN:
6432 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6434 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6435 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6437 case RID_HAS_TRIVIAL_COPY:
6438 kind = CPTK_HAS_TRIVIAL_COPY;
6440 case RID_HAS_TRIVIAL_DESTRUCTOR:
6441 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6443 case RID_HAS_VIRTUAL_DESTRUCTOR:
6444 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6446 case RID_IS_ABSTRACT:
6447 kind = CPTK_IS_ABSTRACT;
6449 case RID_IS_BASE_OF:
6450 kind = CPTK_IS_BASE_OF;
6454 kind = CPTK_IS_CLASS;
6456 case RID_IS_CONVERTIBLE_TO:
6457 kind = CPTK_IS_CONVERTIBLE_TO;
6461 kind = CPTK_IS_EMPTY;
6464 kind = CPTK_IS_ENUM;
6469 case RID_IS_POLYMORPHIC:
6470 kind = CPTK_IS_POLYMORPHIC;
6473 kind = CPTK_IS_UNION;
6479 /* Consume the token. */
6480 cp_lexer_consume_token (parser->lexer);
6482 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6484 type1 = cp_parser_type_id (parser);
6486 /* Build a trivial decl-specifier-seq. */
6487 clear_decl_specs (&decl_specs);
6488 decl_specs.type = type1;
6490 /* Call grokdeclarator to figure out what type this is. */
6491 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6492 /*initialized=*/0, /*attrlist=*/NULL);
6496 cp_parser_require (parser, CPP_COMMA, "`,'");
6498 type2 = cp_parser_type_id (parser);
6500 /* Build a trivial decl-specifier-seq. */
6501 clear_decl_specs (&decl_specs);
6502 decl_specs.type = type2;
6504 /* Call grokdeclarator to figure out what type this is. */
6505 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6506 /*initialized=*/0, /*attrlist=*/NULL);
6509 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6511 /* Complete the trait expr, which may mean either processing the
6512 static assert now or saving it for template instantiation. */
6513 return finish_trait_expr (kind, type1, type2);
6516 /* Statements [gram.stmt.stmt] */
6518 /* Parse a statement.
6522 expression-statement
6527 declaration-statement
6530 IN_COMPOUND is true when the statement is nested inside a
6531 cp_parser_compound_statement; this matters for certain pragmas.
6533 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6534 is a (possibly labeled) if statement which is not enclosed in braces
6535 and has an else clause. This is used to implement -Wparentheses. */
6538 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6539 bool in_compound, bool *if_p)
6543 location_t statement_location;
6548 /* There is no statement yet. */
6549 statement = NULL_TREE;
6550 /* Peek at the next token. */
6551 token = cp_lexer_peek_token (parser->lexer);
6552 /* Remember the location of the first token in the statement. */
6553 statement_location = token->location;
6554 /* If this is a keyword, then that will often determine what kind of
6555 statement we have. */
6556 if (token->type == CPP_KEYWORD)
6558 enum rid keyword = token->keyword;
6564 /* Looks like a labeled-statement with a case label.
6565 Parse the label, and then use tail recursion to parse
6567 cp_parser_label_for_labeled_statement (parser);
6572 statement = cp_parser_selection_statement (parser, if_p);
6578 statement = cp_parser_iteration_statement (parser);
6585 statement = cp_parser_jump_statement (parser);
6588 /* Objective-C++ exception-handling constructs. */
6591 case RID_AT_FINALLY:
6592 case RID_AT_SYNCHRONIZED:
6594 statement = cp_parser_objc_statement (parser);
6598 statement = cp_parser_try_block (parser);
6602 /* This must be a namespace alias definition. */
6603 cp_parser_declaration_statement (parser);
6607 /* It might be a keyword like `int' that can start a
6608 declaration-statement. */
6612 else if (token->type == CPP_NAME)
6614 /* If the next token is a `:', then we are looking at a
6615 labeled-statement. */
6616 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6617 if (token->type == CPP_COLON)
6619 /* Looks like a labeled-statement with an ordinary label.
6620 Parse the label, and then use tail recursion to parse
6622 cp_parser_label_for_labeled_statement (parser);
6626 /* Anything that starts with a `{' must be a compound-statement. */
6627 else if (token->type == CPP_OPEN_BRACE)
6628 statement = cp_parser_compound_statement (parser, NULL, false);
6629 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6630 a statement all its own. */
6631 else if (token->type == CPP_PRAGMA)
6633 /* Only certain OpenMP pragmas are attached to statements, and thus
6634 are considered statements themselves. All others are not. In
6635 the context of a compound, accept the pragma as a "statement" and
6636 return so that we can check for a close brace. Otherwise we
6637 require a real statement and must go back and read one. */
6639 cp_parser_pragma (parser, pragma_compound);
6640 else if (!cp_parser_pragma (parser, pragma_stmt))
6644 else if (token->type == CPP_EOF)
6646 cp_parser_error (parser, "expected statement");
6650 /* Everything else must be a declaration-statement or an
6651 expression-statement. Try for the declaration-statement
6652 first, unless we are looking at a `;', in which case we know that
6653 we have an expression-statement. */
6656 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6658 cp_parser_parse_tentatively (parser);
6659 /* Try to parse the declaration-statement. */
6660 cp_parser_declaration_statement (parser);
6661 /* If that worked, we're done. */
6662 if (cp_parser_parse_definitely (parser))
6665 /* Look for an expression-statement instead. */
6666 statement = cp_parser_expression_statement (parser, in_statement_expr);
6669 /* Set the line number for the statement. */
6670 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6671 SET_EXPR_LOCATION (statement, statement_location);
6674 /* Parse the label for a labeled-statement, i.e.
6677 case constant-expression :
6681 case constant-expression ... constant-expression : statement
6683 When a label is parsed without errors, the label is added to the
6684 parse tree by the finish_* functions, so this function doesn't
6685 have to return the label. */
6688 cp_parser_label_for_labeled_statement (cp_parser* parser)
6692 /* The next token should be an identifier. */
6693 token = cp_lexer_peek_token (parser->lexer);
6694 if (token->type != CPP_NAME
6695 && token->type != CPP_KEYWORD)
6697 cp_parser_error (parser, "expected labeled-statement");
6701 switch (token->keyword)
6708 /* Consume the `case' token. */
6709 cp_lexer_consume_token (parser->lexer);
6710 /* Parse the constant-expression. */
6711 expr = cp_parser_constant_expression (parser,
6712 /*allow_non_constant_p=*/false,
6715 ellipsis = cp_lexer_peek_token (parser->lexer);
6716 if (ellipsis->type == CPP_ELLIPSIS)
6718 /* Consume the `...' token. */
6719 cp_lexer_consume_token (parser->lexer);
6721 cp_parser_constant_expression (parser,
6722 /*allow_non_constant_p=*/false,
6724 /* We don't need to emit warnings here, as the common code
6725 will do this for us. */
6728 expr_hi = NULL_TREE;
6730 if (parser->in_switch_statement_p)
6731 finish_case_label (expr, expr_hi);
6733 error ("case label %qE not within a switch statement", expr);
6738 /* Consume the `default' token. */
6739 cp_lexer_consume_token (parser->lexer);
6741 if (parser->in_switch_statement_p)
6742 finish_case_label (NULL_TREE, NULL_TREE);
6744 error ("case label not within a switch statement");
6748 /* Anything else must be an ordinary label. */
6749 finish_label_stmt (cp_parser_identifier (parser));
6753 /* Require the `:' token. */
6754 cp_parser_require (parser, CPP_COLON, "`:'");
6757 /* Parse an expression-statement.
6759 expression-statement:
6762 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6763 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6764 indicates whether this expression-statement is part of an
6765 expression statement. */
6768 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6770 tree statement = NULL_TREE;
6772 /* If the next token is a ';', then there is no expression
6774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6775 statement = cp_parser_expression (parser, /*cast_p=*/false);
6777 /* Consume the final `;'. */
6778 cp_parser_consume_semicolon_at_end_of_statement (parser);
6780 if (in_statement_expr
6781 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6782 /* This is the final expression statement of a statement
6784 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6786 statement = finish_expr_stmt (statement);
6793 /* Parse a compound-statement.
6796 { statement-seq [opt] }
6798 Returns a tree representing the statement. */
6801 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6806 /* Consume the `{'. */
6807 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6808 return error_mark_node;
6809 /* Begin the compound-statement. */
6810 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6811 /* Parse an (optional) statement-seq. */
6812 cp_parser_statement_seq_opt (parser, in_statement_expr);
6813 /* Finish the compound-statement. */
6814 finish_compound_stmt (compound_stmt);
6815 /* Consume the `}'. */
6816 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6818 return compound_stmt;
6821 /* Parse an (optional) statement-seq.
6825 statement-seq [opt] statement */
6828 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6830 /* Scan statements until there aren't any more. */
6833 cp_token *token = cp_lexer_peek_token (parser->lexer);
6835 /* If we're looking at a `}', then we've run out of statements. */
6836 if (token->type == CPP_CLOSE_BRACE
6837 || token->type == CPP_EOF
6838 || token->type == CPP_PRAGMA_EOL)
6841 /* If we are in a compound statement and find 'else' then
6842 something went wrong. */
6843 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6845 if (parser->in_statement & IN_IF_STMT)
6849 token = cp_lexer_consume_token (parser->lexer);
6850 error ("%<else%> without a previous %<if%>");
6854 /* Parse the statement. */
6855 cp_parser_statement (parser, in_statement_expr, true, NULL);
6859 /* Parse a selection-statement.
6861 selection-statement:
6862 if ( condition ) statement
6863 if ( condition ) statement else statement
6864 switch ( condition ) statement
6866 Returns the new IF_STMT or SWITCH_STMT.
6868 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6869 is a (possibly labeled) if statement which is not enclosed in
6870 braces and has an else clause. This is used to implement
6874 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6882 /* Peek at the next token. */
6883 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6885 /* See what kind of keyword it is. */
6886 keyword = token->keyword;
6895 /* Look for the `('. */
6896 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6898 cp_parser_skip_to_end_of_statement (parser);
6899 return error_mark_node;
6902 /* Begin the selection-statement. */
6903 if (keyword == RID_IF)
6904 statement = begin_if_stmt ();
6906 statement = begin_switch_stmt ();
6908 /* Parse the condition. */
6909 condition = cp_parser_condition (parser);
6910 /* Look for the `)'. */
6911 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6912 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6913 /*consume_paren=*/true);
6915 if (keyword == RID_IF)
6918 unsigned char in_statement;
6920 /* Add the condition. */
6921 finish_if_stmt_cond (condition, statement);
6923 /* Parse the then-clause. */
6924 in_statement = parser->in_statement;
6925 parser->in_statement |= IN_IF_STMT;
6926 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6927 parser->in_statement = in_statement;
6929 finish_then_clause (statement);
6931 /* If the next token is `else', parse the else-clause. */
6932 if (cp_lexer_next_token_is_keyword (parser->lexer,
6935 /* Consume the `else' keyword. */
6936 cp_lexer_consume_token (parser->lexer);
6937 begin_else_clause (statement);
6938 /* Parse the else-clause. */
6939 cp_parser_implicitly_scoped_statement (parser, NULL);
6940 finish_else_clause (statement);
6942 /* If we are currently parsing a then-clause, then
6943 IF_P will not be NULL. We set it to true to
6944 indicate that this if statement has an else clause.
6945 This may trigger the Wparentheses warning below
6946 when we get back up to the parent if statement. */
6952 /* This if statement does not have an else clause. If
6953 NESTED_IF is true, then the then-clause is an if
6954 statement which does have an else clause. We warn
6955 about the potential ambiguity. */
6957 warning (OPT_Wparentheses,
6958 ("%Hsuggest explicit braces "
6959 "to avoid ambiguous %<else%>"),
6960 EXPR_LOCUS (statement));
6963 /* Now we're all done with the if-statement. */
6964 finish_if_stmt (statement);
6968 bool in_switch_statement_p;
6969 unsigned char in_statement;
6971 /* Add the condition. */
6972 finish_switch_cond (condition, statement);
6974 /* Parse the body of the switch-statement. */
6975 in_switch_statement_p = parser->in_switch_statement_p;
6976 in_statement = parser->in_statement;
6977 parser->in_switch_statement_p = true;
6978 parser->in_statement |= IN_SWITCH_STMT;
6979 cp_parser_implicitly_scoped_statement (parser, NULL);
6980 parser->in_switch_statement_p = in_switch_statement_p;
6981 parser->in_statement = in_statement;
6983 /* Now we're all done with the switch-statement. */
6984 finish_switch_stmt (statement);
6992 cp_parser_error (parser, "expected selection-statement");
6993 return error_mark_node;
6997 /* Parse a condition.
7001 type-specifier-seq declarator = assignment-expression
7006 type-specifier-seq declarator asm-specification [opt]
7007 attributes [opt] = assignment-expression
7009 Returns the expression that should be tested. */
7012 cp_parser_condition (cp_parser* parser)
7014 cp_decl_specifier_seq type_specifiers;
7015 const char *saved_message;
7017 /* Try the declaration first. */
7018 cp_parser_parse_tentatively (parser);
7019 /* New types are not allowed in the type-specifier-seq for a
7021 saved_message = parser->type_definition_forbidden_message;
7022 parser->type_definition_forbidden_message
7023 = "types may not be defined in conditions";
7024 /* Parse the type-specifier-seq. */
7025 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7027 /* Restore the saved message. */
7028 parser->type_definition_forbidden_message = saved_message;
7029 /* If all is well, we might be looking at a declaration. */
7030 if (!cp_parser_error_occurred (parser))
7033 tree asm_specification;
7035 cp_declarator *declarator;
7036 tree initializer = NULL_TREE;
7038 /* Parse the declarator. */
7039 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7040 /*ctor_dtor_or_conv_p=*/NULL,
7041 /*parenthesized_p=*/NULL,
7042 /*member_p=*/false);
7043 /* Parse the attributes. */
7044 attributes = cp_parser_attributes_opt (parser);
7045 /* Parse the asm-specification. */
7046 asm_specification = cp_parser_asm_specification_opt (parser);
7047 /* If the next token is not an `=', then we might still be
7048 looking at an expression. For example:
7052 looks like a decl-specifier-seq and a declarator -- but then
7053 there is no `=', so this is an expression. */
7054 cp_parser_require (parser, CPP_EQ, "`='");
7055 /* If we did see an `=', then we are looking at a declaration
7057 if (cp_parser_parse_definitely (parser))
7060 bool non_constant_p;
7062 /* Create the declaration. */
7063 decl = start_decl (declarator, &type_specifiers,
7064 /*initialized_p=*/true,
7065 attributes, /*prefix_attributes=*/NULL_TREE,
7067 /* Parse the assignment-expression. */
7069 = cp_parser_constant_expression (parser,
7070 /*allow_non_constant_p=*/true,
7072 if (!non_constant_p)
7073 initializer = fold_non_dependent_expr (initializer);
7075 /* Process the initializer. */
7076 cp_finish_decl (decl,
7077 initializer, !non_constant_p,
7079 LOOKUP_ONLYCONVERTING);
7082 pop_scope (pushed_scope);
7084 return convert_from_reference (decl);
7087 /* If we didn't even get past the declarator successfully, we are
7088 definitely not looking at a declaration. */
7090 cp_parser_abort_tentative_parse (parser);
7092 /* Otherwise, we are looking at an expression. */
7093 return cp_parser_expression (parser, /*cast_p=*/false);
7096 /* We check for a ) immediately followed by ; with no whitespacing
7097 between. This is used to issue a warning for:
7105 as the semicolon is probably extraneous.
7107 On parse errors, the next token might not be a ), so do nothing in
7111 check_empty_body (cp_parser* parser, const char* type)
7114 cp_token *close_paren;
7115 expanded_location close_loc;
7116 expanded_location semi_loc;
7118 close_paren = cp_lexer_peek_token (parser->lexer);
7119 if (close_paren->type != CPP_CLOSE_PAREN)
7122 close_loc = expand_location (close_paren->location);
7123 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7125 if (token->type != CPP_SEMICOLON
7126 || (token->flags & PREV_WHITE))
7129 semi_loc = expand_location (token->location);
7130 if (close_loc.line == semi_loc.line
7131 #ifdef USE_MAPPED_LOCATION
7132 && close_loc.column+1 == semi_loc.column
7135 warning (OPT_Wempty_body,
7136 "suggest a space before %<;%> or explicit braces around empty "
7137 "body in %<%s%> statement",
7141 /* Parse an iteration-statement.
7143 iteration-statement:
7144 while ( condition ) statement
7145 do statement while ( expression ) ;
7146 for ( for-init-statement condition [opt] ; expression [opt] )
7149 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7152 cp_parser_iteration_statement (cp_parser* parser)
7157 unsigned char in_statement;
7159 /* Peek at the next token. */
7160 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7162 return error_mark_node;
7164 /* Remember whether or not we are already within an iteration
7166 in_statement = parser->in_statement;
7168 /* See what kind of keyword it is. */
7169 keyword = token->keyword;
7176 /* Begin the while-statement. */
7177 statement = begin_while_stmt ();
7178 /* Look for the `('. */
7179 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7180 /* Parse the condition. */
7181 condition = cp_parser_condition (parser);
7182 finish_while_stmt_cond (condition, statement);
7183 check_empty_body (parser, "while");
7184 /* Look for the `)'. */
7185 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7186 /* Parse the dependent statement. */
7187 parser->in_statement = IN_ITERATION_STMT;
7188 cp_parser_already_scoped_statement (parser);
7189 parser->in_statement = in_statement;
7190 /* We're done with the while-statement. */
7191 finish_while_stmt (statement);
7199 /* Begin the do-statement. */
7200 statement = begin_do_stmt ();
7201 /* Parse the body of the do-statement. */
7202 parser->in_statement = IN_ITERATION_STMT;
7203 cp_parser_implicitly_scoped_statement (parser, NULL);
7204 parser->in_statement = in_statement;
7205 finish_do_body (statement);
7206 /* Look for the `while' keyword. */
7207 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7208 /* Look for the `('. */
7209 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7210 /* Parse the expression. */
7211 expression = cp_parser_expression (parser, /*cast_p=*/false);
7212 /* We're done with the do-statement. */
7213 finish_do_stmt (expression, statement);
7214 /* Look for the `)'. */
7215 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7216 /* Look for the `;'. */
7217 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7223 tree condition = NULL_TREE;
7224 tree expression = NULL_TREE;
7226 /* Begin the for-statement. */
7227 statement = begin_for_stmt ();
7228 /* Look for the `('. */
7229 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7230 /* Parse the initialization. */
7231 cp_parser_for_init_statement (parser);
7232 finish_for_init_stmt (statement);
7234 /* If there's a condition, process it. */
7235 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7236 condition = cp_parser_condition (parser);
7237 finish_for_cond (condition, statement);
7238 /* Look for the `;'. */
7239 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7241 /* If there's an expression, process it. */
7242 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7243 expression = cp_parser_expression (parser, /*cast_p=*/false);
7244 finish_for_expr (expression, statement);
7245 check_empty_body (parser, "for");
7246 /* Look for the `)'. */
7247 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7249 /* Parse the body of the for-statement. */
7250 parser->in_statement = IN_ITERATION_STMT;
7251 cp_parser_already_scoped_statement (parser);
7252 parser->in_statement = in_statement;
7254 /* We're done with the for-statement. */
7255 finish_for_stmt (statement);
7260 cp_parser_error (parser, "expected iteration-statement");
7261 statement = error_mark_node;
7268 /* Parse a for-init-statement.
7271 expression-statement
7272 simple-declaration */
7275 cp_parser_for_init_statement (cp_parser* parser)
7277 /* If the next token is a `;', then we have an empty
7278 expression-statement. Grammatically, this is also a
7279 simple-declaration, but an invalid one, because it does not
7280 declare anything. Therefore, if we did not handle this case
7281 specially, we would issue an error message about an invalid
7283 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7285 /* We're going to speculatively look for a declaration, falling back
7286 to an expression, if necessary. */
7287 cp_parser_parse_tentatively (parser);
7288 /* Parse the declaration. */
7289 cp_parser_simple_declaration (parser,
7290 /*function_definition_allowed_p=*/false);
7291 /* If the tentative parse failed, then we shall need to look for an
7292 expression-statement. */
7293 if (cp_parser_parse_definitely (parser))
7297 cp_parser_expression_statement (parser, false);
7300 /* Parse a jump-statement.
7305 return expression [opt] ;
7313 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7316 cp_parser_jump_statement (cp_parser* parser)
7318 tree statement = error_mark_node;
7321 unsigned char in_statement;
7323 /* Peek at the next token. */
7324 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7326 return error_mark_node;
7328 /* See what kind of keyword it is. */
7329 keyword = token->keyword;
7333 in_statement = parser->in_statement & ~IN_IF_STMT;
7334 switch (in_statement)
7337 error ("break statement not within loop or switch");
7340 gcc_assert ((in_statement & IN_SWITCH_STMT)
7341 || in_statement == IN_ITERATION_STMT);
7342 statement = finish_break_stmt ();
7345 error ("invalid exit from OpenMP structured block");
7348 error ("break statement used with OpenMP for loop");
7351 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7355 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7358 error ("continue statement not within a loop");
7360 case IN_ITERATION_STMT:
7362 statement = finish_continue_stmt ();
7365 error ("invalid exit from OpenMP structured block");
7370 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7377 /* If the next token is a `;', then there is no
7379 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7380 expr = cp_parser_expression (parser, /*cast_p=*/false);
7383 /* Build the return-statement. */
7384 statement = finish_return_stmt (expr);
7385 /* Look for the final `;'. */
7386 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7391 /* Create the goto-statement. */
7392 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7394 /* Issue a warning about this use of a GNU extension. */
7396 pedwarn ("ISO C++ forbids computed gotos");
7397 /* Consume the '*' token. */
7398 cp_lexer_consume_token (parser->lexer);
7399 /* Parse the dependent expression. */
7400 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7403 finish_goto_stmt (cp_parser_identifier (parser));
7404 /* Look for the final `;'. */
7405 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7409 cp_parser_error (parser, "expected jump-statement");
7416 /* Parse a declaration-statement.
7418 declaration-statement:
7419 block-declaration */
7422 cp_parser_declaration_statement (cp_parser* parser)
7426 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7427 p = obstack_alloc (&declarator_obstack, 0);
7429 /* Parse the block-declaration. */
7430 cp_parser_block_declaration (parser, /*statement_p=*/true);
7432 /* Free any declarators allocated. */
7433 obstack_free (&declarator_obstack, p);
7435 /* Finish off the statement. */
7439 /* Some dependent statements (like `if (cond) statement'), are
7440 implicitly in their own scope. In other words, if the statement is
7441 a single statement (as opposed to a compound-statement), it is
7442 none-the-less treated as if it were enclosed in braces. Any
7443 declarations appearing in the dependent statement are out of scope
7444 after control passes that point. This function parses a statement,
7445 but ensures that is in its own scope, even if it is not a
7448 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7449 is a (possibly labeled) if statement which is not enclosed in
7450 braces and has an else clause. This is used to implement
7453 Returns the new statement. */
7456 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7463 /* Mark if () ; with a special NOP_EXPR. */
7464 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7466 cp_lexer_consume_token (parser->lexer);
7467 statement = add_stmt (build_empty_stmt ());
7469 /* if a compound is opened, we simply parse the statement directly. */
7470 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7471 statement = cp_parser_compound_statement (parser, NULL, false);
7472 /* If the token is not a `{', then we must take special action. */
7475 /* Create a compound-statement. */
7476 statement = begin_compound_stmt (0);
7477 /* Parse the dependent-statement. */
7478 cp_parser_statement (parser, NULL_TREE, false, if_p);
7479 /* Finish the dummy compound-statement. */
7480 finish_compound_stmt (statement);
7483 /* Return the statement. */
7487 /* For some dependent statements (like `while (cond) statement'), we
7488 have already created a scope. Therefore, even if the dependent
7489 statement is a compound-statement, we do not want to create another
7493 cp_parser_already_scoped_statement (cp_parser* parser)
7495 /* If the token is a `{', then we must take special action. */
7496 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7497 cp_parser_statement (parser, NULL_TREE, false, NULL);
7500 /* Avoid calling cp_parser_compound_statement, so that we
7501 don't create a new scope. Do everything else by hand. */
7502 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7503 cp_parser_statement_seq_opt (parser, NULL_TREE);
7504 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7508 /* Declarations [gram.dcl.dcl] */
7510 /* Parse an optional declaration-sequence.
7514 declaration-seq declaration */
7517 cp_parser_declaration_seq_opt (cp_parser* parser)
7523 token = cp_lexer_peek_token (parser->lexer);
7525 if (token->type == CPP_CLOSE_BRACE
7526 || token->type == CPP_EOF
7527 || token->type == CPP_PRAGMA_EOL)
7530 if (token->type == CPP_SEMICOLON)
7532 /* A declaration consisting of a single semicolon is
7533 invalid. Allow it unless we're being pedantic. */
7534 cp_lexer_consume_token (parser->lexer);
7535 if (pedantic && !in_system_header)
7536 pedwarn ("extra %<;%>");
7540 /* If we're entering or exiting a region that's implicitly
7541 extern "C", modify the lang context appropriately. */
7542 if (!parser->implicit_extern_c && token->implicit_extern_c)
7544 push_lang_context (lang_name_c);
7545 parser->implicit_extern_c = true;
7547 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7549 pop_lang_context ();
7550 parser->implicit_extern_c = false;
7553 if (token->type == CPP_PRAGMA)
7555 /* A top-level declaration can consist solely of a #pragma.
7556 A nested declaration cannot, so this is done here and not
7557 in cp_parser_declaration. (A #pragma at block scope is
7558 handled in cp_parser_statement.) */
7559 cp_parser_pragma (parser, pragma_external);
7563 /* Parse the declaration itself. */
7564 cp_parser_declaration (parser);
7568 /* Parse a declaration.
7573 template-declaration
7574 explicit-instantiation
7575 explicit-specialization
7576 linkage-specification
7577 namespace-definition
7582 __extension__ declaration */
7585 cp_parser_declaration (cp_parser* parser)
7592 /* Check for the `__extension__' keyword. */
7593 if (cp_parser_extension_opt (parser, &saved_pedantic))
7595 /* Parse the qualified declaration. */
7596 cp_parser_declaration (parser);
7597 /* Restore the PEDANTIC flag. */
7598 pedantic = saved_pedantic;
7603 /* Try to figure out what kind of declaration is present. */
7604 token1 = *cp_lexer_peek_token (parser->lexer);
7606 if (token1.type != CPP_EOF)
7607 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7610 token2.type = CPP_EOF;
7611 token2.keyword = RID_MAX;
7614 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7615 p = obstack_alloc (&declarator_obstack, 0);
7617 /* If the next token is `extern' and the following token is a string
7618 literal, then we have a linkage specification. */
7619 if (token1.keyword == RID_EXTERN
7620 && cp_parser_is_string_literal (&token2))
7621 cp_parser_linkage_specification (parser);
7622 /* If the next token is `template', then we have either a template
7623 declaration, an explicit instantiation, or an explicit
7625 else if (token1.keyword == RID_TEMPLATE)
7627 /* `template <>' indicates a template specialization. */
7628 if (token2.type == CPP_LESS
7629 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7630 cp_parser_explicit_specialization (parser);
7631 /* `template <' indicates a template declaration. */
7632 else if (token2.type == CPP_LESS)
7633 cp_parser_template_declaration (parser, /*member_p=*/false);
7634 /* Anything else must be an explicit instantiation. */
7636 cp_parser_explicit_instantiation (parser);
7638 /* If the next token is `export', then we have a template
7640 else if (token1.keyword == RID_EXPORT)
7641 cp_parser_template_declaration (parser, /*member_p=*/false);
7642 /* If the next token is `extern', 'static' or 'inline' and the one
7643 after that is `template', we have a GNU extended explicit
7644 instantiation directive. */
7645 else if (cp_parser_allow_gnu_extensions_p (parser)
7646 && (token1.keyword == RID_EXTERN
7647 || token1.keyword == RID_STATIC
7648 || token1.keyword == RID_INLINE)
7649 && token2.keyword == RID_TEMPLATE)
7650 cp_parser_explicit_instantiation (parser);
7651 /* If the next token is `namespace', check for a named or unnamed
7652 namespace definition. */
7653 else if (token1.keyword == RID_NAMESPACE
7654 && (/* A named namespace definition. */
7655 (token2.type == CPP_NAME
7656 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7658 /* An unnamed namespace definition. */
7659 || token2.type == CPP_OPEN_BRACE
7660 || token2.keyword == RID_ATTRIBUTE))
7661 cp_parser_namespace_definition (parser);
7662 /* Objective-C++ declaration/definition. */
7663 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7664 cp_parser_objc_declaration (parser);
7665 /* We must have either a block declaration or a function
7668 /* Try to parse a block-declaration, or a function-definition. */
7669 cp_parser_block_declaration (parser, /*statement_p=*/false);
7671 /* Free any declarators allocated. */
7672 obstack_free (&declarator_obstack, p);
7675 /* Parse a block-declaration.
7680 namespace-alias-definition
7687 __extension__ block-declaration
7693 static_assert-declaration
7695 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7696 part of a declaration-statement. */
7699 cp_parser_block_declaration (cp_parser *parser,
7705 /* Check for the `__extension__' keyword. */
7706 if (cp_parser_extension_opt (parser, &saved_pedantic))
7708 /* Parse the qualified declaration. */
7709 cp_parser_block_declaration (parser, statement_p);
7710 /* Restore the PEDANTIC flag. */
7711 pedantic = saved_pedantic;
7716 /* Peek at the next token to figure out which kind of declaration is
7718 token1 = cp_lexer_peek_token (parser->lexer);
7720 /* If the next keyword is `asm', we have an asm-definition. */
7721 if (token1->keyword == RID_ASM)
7724 cp_parser_commit_to_tentative_parse (parser);
7725 cp_parser_asm_definition (parser);
7727 /* If the next keyword is `namespace', we have a
7728 namespace-alias-definition. */
7729 else if (token1->keyword == RID_NAMESPACE)
7730 cp_parser_namespace_alias_definition (parser);
7731 /* If the next keyword is `using', we have either a
7732 using-declaration or a using-directive. */
7733 else if (token1->keyword == RID_USING)
7738 cp_parser_commit_to_tentative_parse (parser);
7739 /* If the token after `using' is `namespace', then we have a
7741 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7742 if (token2->keyword == RID_NAMESPACE)
7743 cp_parser_using_directive (parser);
7744 /* Otherwise, it's a using-declaration. */
7746 cp_parser_using_declaration (parser,
7747 /*access_declaration_p=*/false);
7749 /* If the next keyword is `__label__' we have a label declaration. */
7750 else if (token1->keyword == RID_LABEL)
7753 cp_parser_commit_to_tentative_parse (parser);
7754 cp_parser_label_declaration (parser);
7756 /* If the next token is `static_assert' we have a static assertion. */
7757 else if (token1->keyword == RID_STATIC_ASSERT)
7758 cp_parser_static_assert (parser, /*member_p=*/false);
7759 /* Anything else must be a simple-declaration. */
7761 cp_parser_simple_declaration (parser, !statement_p);
7764 /* Parse a simple-declaration.
7767 decl-specifier-seq [opt] init-declarator-list [opt] ;
7769 init-declarator-list:
7771 init-declarator-list , init-declarator
7773 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7774 function-definition as a simple-declaration. */
7777 cp_parser_simple_declaration (cp_parser* parser,
7778 bool function_definition_allowed_p)
7780 cp_decl_specifier_seq decl_specifiers;
7781 int declares_class_or_enum;
7782 bool saw_declarator;
7784 /* Defer access checks until we know what is being declared; the
7785 checks for names appearing in the decl-specifier-seq should be
7786 done as if we were in the scope of the thing being declared. */
7787 push_deferring_access_checks (dk_deferred);
7789 /* Parse the decl-specifier-seq. We have to keep track of whether
7790 or not the decl-specifier-seq declares a named class or
7791 enumeration type, since that is the only case in which the
7792 init-declarator-list is allowed to be empty.
7796 In a simple-declaration, the optional init-declarator-list can be
7797 omitted only when declaring a class or enumeration, that is when
7798 the decl-specifier-seq contains either a class-specifier, an
7799 elaborated-type-specifier, or an enum-specifier. */
7800 cp_parser_decl_specifier_seq (parser,
7801 CP_PARSER_FLAGS_OPTIONAL,
7803 &declares_class_or_enum);
7804 /* We no longer need to defer access checks. */
7805 stop_deferring_access_checks ();
7807 /* In a block scope, a valid declaration must always have a
7808 decl-specifier-seq. By not trying to parse declarators, we can
7809 resolve the declaration/expression ambiguity more quickly. */
7810 if (!function_definition_allowed_p
7811 && !decl_specifiers.any_specifiers_p)
7813 cp_parser_error (parser, "expected declaration");
7817 /* If the next two tokens are both identifiers, the code is
7818 erroneous. The usual cause of this situation is code like:
7822 where "T" should name a type -- but does not. */
7823 if (!decl_specifiers.type
7824 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7826 /* If parsing tentatively, we should commit; we really are
7827 looking at a declaration. */
7828 cp_parser_commit_to_tentative_parse (parser);
7833 /* If we have seen at least one decl-specifier, and the next token
7834 is not a parenthesis, then we must be looking at a declaration.
7835 (After "int (" we might be looking at a functional cast.) */
7836 if (decl_specifiers.any_specifiers_p
7837 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7838 cp_parser_commit_to_tentative_parse (parser);
7840 /* Keep going until we hit the `;' at the end of the simple
7842 saw_declarator = false;
7843 while (cp_lexer_next_token_is_not (parser->lexer,
7847 bool function_definition_p;
7852 /* If we are processing next declarator, coma is expected */
7853 token = cp_lexer_peek_token (parser->lexer);
7854 gcc_assert (token->type == CPP_COMMA);
7855 cp_lexer_consume_token (parser->lexer);
7858 saw_declarator = true;
7860 /* Parse the init-declarator. */
7861 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7863 function_definition_allowed_p,
7865 declares_class_or_enum,
7866 &function_definition_p);
7867 /* If an error occurred while parsing tentatively, exit quickly.
7868 (That usually happens when in the body of a function; each
7869 statement is treated as a declaration-statement until proven
7871 if (cp_parser_error_occurred (parser))
7873 /* Handle function definitions specially. */
7874 if (function_definition_p)
7876 /* If the next token is a `,', then we are probably
7877 processing something like:
7881 which is erroneous. */
7882 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7883 error ("mixing declarations and function-definitions is forbidden");
7884 /* Otherwise, we're done with the list of declarators. */
7887 pop_deferring_access_checks ();
7891 /* The next token should be either a `,' or a `;'. */
7892 token = cp_lexer_peek_token (parser->lexer);
7893 /* If it's a `,', there are more declarators to come. */
7894 if (token->type == CPP_COMMA)
7895 /* will be consumed next time around */;
7896 /* If it's a `;', we are done. */
7897 else if (token->type == CPP_SEMICOLON)
7899 /* Anything else is an error. */
7902 /* If we have already issued an error message we don't need
7903 to issue another one. */
7904 if (decl != error_mark_node
7905 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7906 cp_parser_error (parser, "expected %<,%> or %<;%>");
7907 /* Skip tokens until we reach the end of the statement. */
7908 cp_parser_skip_to_end_of_statement (parser);
7909 /* If the next token is now a `;', consume it. */
7910 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7911 cp_lexer_consume_token (parser->lexer);
7914 /* After the first time around, a function-definition is not
7915 allowed -- even if it was OK at first. For example:
7920 function_definition_allowed_p = false;
7923 /* Issue an error message if no declarators are present, and the
7924 decl-specifier-seq does not itself declare a class or
7926 if (!saw_declarator)
7928 if (cp_parser_declares_only_class_p (parser))
7929 shadow_tag (&decl_specifiers);
7930 /* Perform any deferred access checks. */
7931 perform_deferred_access_checks ();
7934 /* Consume the `;'. */
7935 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7938 pop_deferring_access_checks ();
7941 /* Parse a decl-specifier-seq.
7944 decl-specifier-seq [opt] decl-specifier
7947 storage-class-specifier
7958 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7960 The parser flags FLAGS is used to control type-specifier parsing.
7962 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7965 1: one of the decl-specifiers is an elaborated-type-specifier
7966 (i.e., a type declaration)
7967 2: one of the decl-specifiers is an enum-specifier or a
7968 class-specifier (i.e., a type definition)
7973 cp_parser_decl_specifier_seq (cp_parser* parser,
7974 cp_parser_flags flags,
7975 cp_decl_specifier_seq *decl_specs,
7976 int* declares_class_or_enum)
7978 bool constructor_possible_p = !parser->in_declarator_p;
7980 /* Clear DECL_SPECS. */
7981 clear_decl_specs (decl_specs);
7983 /* Assume no class or enumeration type is declared. */
7984 *declares_class_or_enum = 0;
7986 /* Keep reading specifiers until there are no more to read. */
7990 bool found_decl_spec;
7993 /* Peek at the next token. */
7994 token = cp_lexer_peek_token (parser->lexer);
7995 /* Handle attributes. */
7996 if (token->keyword == RID_ATTRIBUTE)
7998 /* Parse the attributes. */
7999 decl_specs->attributes
8000 = chainon (decl_specs->attributes,
8001 cp_parser_attributes_opt (parser));
8004 /* Assume we will find a decl-specifier keyword. */
8005 found_decl_spec = true;
8006 /* If the next token is an appropriate keyword, we can simply
8007 add it to the list. */
8008 switch (token->keyword)
8013 if (!at_class_scope_p ())
8015 error ("%<friend%> used outside of class");
8016 cp_lexer_purge_token (parser->lexer);
8020 ++decl_specs->specs[(int) ds_friend];
8021 /* Consume the token. */
8022 cp_lexer_consume_token (parser->lexer);
8026 /* function-specifier:
8033 cp_parser_function_specifier_opt (parser, decl_specs);
8039 ++decl_specs->specs[(int) ds_typedef];
8040 /* Consume the token. */
8041 cp_lexer_consume_token (parser->lexer);
8042 /* A constructor declarator cannot appear in a typedef. */
8043 constructor_possible_p = false;
8044 /* The "typedef" keyword can only occur in a declaration; we
8045 may as well commit at this point. */
8046 cp_parser_commit_to_tentative_parse (parser);
8048 if (decl_specs->storage_class != sc_none)
8049 decl_specs->conflicting_specifiers_p = true;
8052 /* storage-class-specifier:
8066 /* Consume the token. */
8067 cp_lexer_consume_token (parser->lexer);
8068 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8071 /* Consume the token. */
8072 cp_lexer_consume_token (parser->lexer);
8073 ++decl_specs->specs[(int) ds_thread];
8077 /* We did not yet find a decl-specifier yet. */
8078 found_decl_spec = false;
8082 /* Constructors are a special case. The `S' in `S()' is not a
8083 decl-specifier; it is the beginning of the declarator. */
8086 && constructor_possible_p
8087 && (cp_parser_constructor_declarator_p
8088 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8090 /* If we don't have a DECL_SPEC yet, then we must be looking at
8091 a type-specifier. */
8092 if (!found_decl_spec && !constructor_p)
8094 int decl_spec_declares_class_or_enum;
8095 bool is_cv_qualifier;
8099 = cp_parser_type_specifier (parser, flags,
8101 /*is_declaration=*/true,
8102 &decl_spec_declares_class_or_enum,
8105 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8107 /* If this type-specifier referenced a user-defined type
8108 (a typedef, class-name, etc.), then we can't allow any
8109 more such type-specifiers henceforth.
8113 The longest sequence of decl-specifiers that could
8114 possibly be a type name is taken as the
8115 decl-specifier-seq of a declaration. The sequence shall
8116 be self-consistent as described below.
8120 As a general rule, at most one type-specifier is allowed
8121 in the complete decl-specifier-seq of a declaration. The
8122 only exceptions are the following:
8124 -- const or volatile can be combined with any other
8127 -- signed or unsigned can be combined with char, long,
8135 void g (const int Pc);
8137 Here, Pc is *not* part of the decl-specifier seq; it's
8138 the declarator. Therefore, once we see a type-specifier
8139 (other than a cv-qualifier), we forbid any additional
8140 user-defined types. We *do* still allow things like `int
8141 int' to be considered a decl-specifier-seq, and issue the
8142 error message later. */
8143 if (type_spec && !is_cv_qualifier)
8144 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8145 /* A constructor declarator cannot follow a type-specifier. */
8148 constructor_possible_p = false;
8149 found_decl_spec = true;
8153 /* If we still do not have a DECL_SPEC, then there are no more
8155 if (!found_decl_spec)
8158 decl_specs->any_specifiers_p = true;
8159 /* After we see one decl-specifier, further decl-specifiers are
8161 flags |= CP_PARSER_FLAGS_OPTIONAL;
8164 cp_parser_check_decl_spec (decl_specs);
8166 /* Don't allow a friend specifier with a class definition. */
8167 if (decl_specs->specs[(int) ds_friend] != 0
8168 && (*declares_class_or_enum & 2))
8169 error ("class definition may not be declared a friend");
8172 /* Parse an (optional) storage-class-specifier.
8174 storage-class-specifier:
8183 storage-class-specifier:
8186 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8189 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8191 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8199 /* Consume the token. */
8200 return cp_lexer_consume_token (parser->lexer)->u.value;
8207 /* Parse an (optional) function-specifier.
8214 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8215 Updates DECL_SPECS, if it is non-NULL. */
8218 cp_parser_function_specifier_opt (cp_parser* parser,
8219 cp_decl_specifier_seq *decl_specs)
8221 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8225 ++decl_specs->specs[(int) ds_inline];
8229 /* 14.5.2.3 [temp.mem]
8231 A member function template shall not be virtual. */
8232 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8233 error ("templates may not be %<virtual%>");
8234 else if (decl_specs)
8235 ++decl_specs->specs[(int) ds_virtual];
8240 ++decl_specs->specs[(int) ds_explicit];
8247 /* Consume the token. */
8248 return cp_lexer_consume_token (parser->lexer)->u.value;
8251 /* Parse a linkage-specification.
8253 linkage-specification:
8254 extern string-literal { declaration-seq [opt] }
8255 extern string-literal declaration */
8258 cp_parser_linkage_specification (cp_parser* parser)
8262 /* Look for the `extern' keyword. */
8263 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8265 /* Look for the string-literal. */
8266 linkage = cp_parser_string_literal (parser, false, false);
8268 /* Transform the literal into an identifier. If the literal is a
8269 wide-character string, or contains embedded NULs, then we can't
8270 handle it as the user wants. */
8271 if (strlen (TREE_STRING_POINTER (linkage))
8272 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8274 cp_parser_error (parser, "invalid linkage-specification");
8275 /* Assume C++ linkage. */
8276 linkage = lang_name_cplusplus;
8279 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8281 /* We're now using the new linkage. */
8282 push_lang_context (linkage);
8284 /* If the next token is a `{', then we're using the first
8286 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8288 /* Consume the `{' token. */
8289 cp_lexer_consume_token (parser->lexer);
8290 /* Parse the declarations. */
8291 cp_parser_declaration_seq_opt (parser);
8292 /* Look for the closing `}'. */
8293 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8295 /* Otherwise, there's just one declaration. */
8298 bool saved_in_unbraced_linkage_specification_p;
8300 saved_in_unbraced_linkage_specification_p
8301 = parser->in_unbraced_linkage_specification_p;
8302 parser->in_unbraced_linkage_specification_p = true;
8303 cp_parser_declaration (parser);
8304 parser->in_unbraced_linkage_specification_p
8305 = saved_in_unbraced_linkage_specification_p;
8308 /* We're done with the linkage-specification. */
8309 pop_lang_context ();
8312 /* Parse a static_assert-declaration.
8314 static_assert-declaration:
8315 static_assert ( constant-expression , string-literal ) ;
8317 If MEMBER_P, this static_assert is a class member. */
8320 cp_parser_static_assert(cp_parser *parser, bool member_p)
8325 location_t saved_loc;
8327 /* Peek at the `static_assert' token so we can keep track of exactly
8328 where the static assertion started. */
8329 token = cp_lexer_peek_token (parser->lexer);
8330 saved_loc = token->location;
8332 /* Look for the `static_assert' keyword. */
8333 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8337 /* We know we are in a static assertion; commit to any tentative
8339 if (cp_parser_parsing_tentatively (parser))
8340 cp_parser_commit_to_tentative_parse (parser);
8342 /* Parse the `(' starting the static assertion condition. */
8343 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8345 /* Parse the constant-expression. */
8347 cp_parser_constant_expression (parser,
8348 /*allow_non_constant_p=*/false,
8349 /*non_constant_p=*/NULL);
8351 /* Parse the separating `,'. */
8352 cp_parser_require (parser, CPP_COMMA, "`,'");
8354 /* Parse the string-literal message. */
8355 message = cp_parser_string_literal (parser,
8356 /*translate=*/false,
8359 /* A `)' completes the static assertion. */
8360 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8361 cp_parser_skip_to_closing_parenthesis (parser,
8362 /*recovering=*/true,
8364 /*consume_paren=*/true);
8366 /* A semicolon terminates the declaration. */
8367 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8369 /* Complete the static assertion, which may mean either processing
8370 the static assert now or saving it for template instantiation. */
8371 finish_static_assert (condition, message, saved_loc, member_p);
8374 /* Special member functions [gram.special] */
8376 /* Parse a conversion-function-id.
8378 conversion-function-id:
8379 operator conversion-type-id
8381 Returns an IDENTIFIER_NODE representing the operator. */
8384 cp_parser_conversion_function_id (cp_parser* parser)
8388 tree saved_qualifying_scope;
8389 tree saved_object_scope;
8390 tree pushed_scope = NULL_TREE;
8392 /* Look for the `operator' token. */
8393 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8394 return error_mark_node;
8395 /* When we parse the conversion-type-id, the current scope will be
8396 reset. However, we need that information in able to look up the
8397 conversion function later, so we save it here. */
8398 saved_scope = parser->scope;
8399 saved_qualifying_scope = parser->qualifying_scope;
8400 saved_object_scope = parser->object_scope;
8401 /* We must enter the scope of the class so that the names of
8402 entities declared within the class are available in the
8403 conversion-type-id. For example, consider:
8410 S::operator I() { ... }
8412 In order to see that `I' is a type-name in the definition, we
8413 must be in the scope of `S'. */
8415 pushed_scope = push_scope (saved_scope);
8416 /* Parse the conversion-type-id. */
8417 type = cp_parser_conversion_type_id (parser);
8418 /* Leave the scope of the class, if any. */
8420 pop_scope (pushed_scope);
8421 /* Restore the saved scope. */
8422 parser->scope = saved_scope;
8423 parser->qualifying_scope = saved_qualifying_scope;
8424 parser->object_scope = saved_object_scope;
8425 /* If the TYPE is invalid, indicate failure. */
8426 if (type == error_mark_node)
8427 return error_mark_node;
8428 return mangle_conv_op_name_for_type (type);
8431 /* Parse a conversion-type-id:
8434 type-specifier-seq conversion-declarator [opt]
8436 Returns the TYPE specified. */
8439 cp_parser_conversion_type_id (cp_parser* parser)
8442 cp_decl_specifier_seq type_specifiers;
8443 cp_declarator *declarator;
8444 tree type_specified;
8446 /* Parse the attributes. */
8447 attributes = cp_parser_attributes_opt (parser);
8448 /* Parse the type-specifiers. */
8449 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8451 /* If that didn't work, stop. */
8452 if (type_specifiers.type == error_mark_node)
8453 return error_mark_node;
8454 /* Parse the conversion-declarator. */
8455 declarator = cp_parser_conversion_declarator_opt (parser);
8457 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8458 /*initialized=*/0, &attributes);
8460 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8461 return type_specified;
8464 /* Parse an (optional) conversion-declarator.
8466 conversion-declarator:
8467 ptr-operator conversion-declarator [opt]
8471 static cp_declarator *
8472 cp_parser_conversion_declarator_opt (cp_parser* parser)
8474 enum tree_code code;
8476 cp_cv_quals cv_quals;
8478 /* We don't know if there's a ptr-operator next, or not. */
8479 cp_parser_parse_tentatively (parser);
8480 /* Try the ptr-operator. */
8481 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8482 /* If it worked, look for more conversion-declarators. */
8483 if (cp_parser_parse_definitely (parser))
8485 cp_declarator *declarator;
8487 /* Parse another optional declarator. */
8488 declarator = cp_parser_conversion_declarator_opt (parser);
8490 return cp_parser_make_indirect_declarator
8491 (code, class_type, cv_quals, declarator);
8497 /* Parse an (optional) ctor-initializer.
8500 : mem-initializer-list
8502 Returns TRUE iff the ctor-initializer was actually present. */
8505 cp_parser_ctor_initializer_opt (cp_parser* parser)
8507 /* If the next token is not a `:', then there is no
8508 ctor-initializer. */
8509 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8511 /* Do default initialization of any bases and members. */
8512 if (DECL_CONSTRUCTOR_P (current_function_decl))
8513 finish_mem_initializers (NULL_TREE);
8518 /* Consume the `:' token. */
8519 cp_lexer_consume_token (parser->lexer);
8520 /* And the mem-initializer-list. */
8521 cp_parser_mem_initializer_list (parser);
8526 /* Parse a mem-initializer-list.
8528 mem-initializer-list:
8529 mem-initializer ... [opt]
8530 mem-initializer ... [opt] , mem-initializer-list */
8533 cp_parser_mem_initializer_list (cp_parser* parser)
8535 tree mem_initializer_list = NULL_TREE;
8537 /* Let the semantic analysis code know that we are starting the
8538 mem-initializer-list. */
8539 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8540 error ("only constructors take base initializers");
8542 /* Loop through the list. */
8545 tree mem_initializer;
8547 /* Parse the mem-initializer. */
8548 mem_initializer = cp_parser_mem_initializer (parser);
8549 /* If the next token is a `...', we're expanding member initializers. */
8550 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8552 /* Consume the `...'. */
8553 cp_lexer_consume_token (parser->lexer);
8555 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8556 can be expanded but members cannot. */
8557 if (mem_initializer != error_mark_node
8558 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8560 error ("cannot expand initializer for member %<%D%>",
8561 TREE_PURPOSE (mem_initializer));
8562 mem_initializer = error_mark_node;
8565 /* Construct the pack expansion type. */
8566 if (mem_initializer != error_mark_node)
8567 mem_initializer = make_pack_expansion (mem_initializer);
8569 /* Add it to the list, unless it was erroneous. */
8570 if (mem_initializer != error_mark_node)
8572 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8573 mem_initializer_list = mem_initializer;
8575 /* If the next token is not a `,', we're done. */
8576 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8578 /* Consume the `,' token. */
8579 cp_lexer_consume_token (parser->lexer);
8582 /* Perform semantic analysis. */
8583 if (DECL_CONSTRUCTOR_P (current_function_decl))
8584 finish_mem_initializers (mem_initializer_list);
8587 /* Parse a mem-initializer.
8590 mem-initializer-id ( expression-list [opt] )
8595 ( expression-list [opt] )
8597 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8598 class) or FIELD_DECL (for a non-static data member) to initialize;
8599 the TREE_VALUE is the expression-list. An empty initialization
8600 list is represented by void_list_node. */
8603 cp_parser_mem_initializer (cp_parser* parser)
8605 tree mem_initializer_id;
8606 tree expression_list;
8609 /* Find out what is being initialized. */
8610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8612 pedwarn ("anachronistic old-style base class initializer");
8613 mem_initializer_id = NULL_TREE;
8616 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8617 member = expand_member_init (mem_initializer_id);
8618 if (member && !DECL_P (member))
8619 in_base_initializer = 1;
8622 = cp_parser_parenthesized_expression_list (parser, false,
8624 /*allow_expansion_p=*/true,
8625 /*non_constant_p=*/NULL);
8626 if (expression_list == error_mark_node)
8627 return error_mark_node;
8628 if (!expression_list)
8629 expression_list = void_type_node;
8631 in_base_initializer = 0;
8633 return member ? build_tree_list (member, expression_list) : error_mark_node;
8636 /* Parse a mem-initializer-id.
8639 :: [opt] nested-name-specifier [opt] class-name
8642 Returns a TYPE indicating the class to be initializer for the first
8643 production. Returns an IDENTIFIER_NODE indicating the data member
8644 to be initialized for the second production. */
8647 cp_parser_mem_initializer_id (cp_parser* parser)
8649 bool global_scope_p;
8650 bool nested_name_specifier_p;
8651 bool template_p = false;
8654 /* `typename' is not allowed in this context ([temp.res]). */
8655 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8657 error ("keyword %<typename%> not allowed in this context (a qualified "
8658 "member initializer is implicitly a type)");
8659 cp_lexer_consume_token (parser->lexer);
8661 /* Look for the optional `::' operator. */
8663 = (cp_parser_global_scope_opt (parser,
8664 /*current_scope_valid_p=*/false)
8666 /* Look for the optional nested-name-specifier. The simplest way to
8671 The keyword `typename' is not permitted in a base-specifier or
8672 mem-initializer; in these contexts a qualified name that
8673 depends on a template-parameter is implicitly assumed to be a
8676 is to assume that we have seen the `typename' keyword at this
8678 nested_name_specifier_p
8679 = (cp_parser_nested_name_specifier_opt (parser,
8680 /*typename_keyword_p=*/true,
8681 /*check_dependency_p=*/true,
8683 /*is_declaration=*/true)
8685 if (nested_name_specifier_p)
8686 template_p = cp_parser_optional_template_keyword (parser);
8687 /* If there is a `::' operator or a nested-name-specifier, then we
8688 are definitely looking for a class-name. */
8689 if (global_scope_p || nested_name_specifier_p)
8690 return cp_parser_class_name (parser,
8691 /*typename_keyword_p=*/true,
8692 /*template_keyword_p=*/template_p,
8694 /*check_dependency_p=*/true,
8695 /*class_head_p=*/false,
8696 /*is_declaration=*/true);
8697 /* Otherwise, we could also be looking for an ordinary identifier. */
8698 cp_parser_parse_tentatively (parser);
8699 /* Try a class-name. */
8700 id = cp_parser_class_name (parser,
8701 /*typename_keyword_p=*/true,
8702 /*template_keyword_p=*/false,
8704 /*check_dependency_p=*/true,
8705 /*class_head_p=*/false,
8706 /*is_declaration=*/true);
8707 /* If we found one, we're done. */
8708 if (cp_parser_parse_definitely (parser))
8710 /* Otherwise, look for an ordinary identifier. */
8711 return cp_parser_identifier (parser);
8714 /* Overloading [gram.over] */
8716 /* Parse an operator-function-id.
8718 operator-function-id:
8721 Returns an IDENTIFIER_NODE for the operator which is a
8722 human-readable spelling of the identifier, e.g., `operator +'. */
8725 cp_parser_operator_function_id (cp_parser* parser)
8727 /* Look for the `operator' keyword. */
8728 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8729 return error_mark_node;
8730 /* And then the name of the operator itself. */
8731 return cp_parser_operator (parser);
8734 /* Parse an operator.
8737 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8738 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8739 || ++ -- , ->* -> () []
8746 Returns an IDENTIFIER_NODE for the operator which is a
8747 human-readable spelling of the identifier, e.g., `operator +'. */
8750 cp_parser_operator (cp_parser* parser)
8752 tree id = NULL_TREE;
8755 /* Peek at the next token. */
8756 token = cp_lexer_peek_token (parser->lexer);
8757 /* Figure out which operator we have. */
8758 switch (token->type)
8764 /* The keyword should be either `new' or `delete'. */
8765 if (token->keyword == RID_NEW)
8767 else if (token->keyword == RID_DELETE)
8772 /* Consume the `new' or `delete' token. */
8773 cp_lexer_consume_token (parser->lexer);
8775 /* Peek at the next token. */
8776 token = cp_lexer_peek_token (parser->lexer);
8777 /* If it's a `[' token then this is the array variant of the
8779 if (token->type == CPP_OPEN_SQUARE)
8781 /* Consume the `[' token. */
8782 cp_lexer_consume_token (parser->lexer);
8783 /* Look for the `]' token. */
8784 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8785 id = ansi_opname (op == NEW_EXPR
8786 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8788 /* Otherwise, we have the non-array variant. */
8790 id = ansi_opname (op);
8796 id = ansi_opname (PLUS_EXPR);
8800 id = ansi_opname (MINUS_EXPR);
8804 id = ansi_opname (MULT_EXPR);
8808 id = ansi_opname (TRUNC_DIV_EXPR);
8812 id = ansi_opname (TRUNC_MOD_EXPR);
8816 id = ansi_opname (BIT_XOR_EXPR);
8820 id = ansi_opname (BIT_AND_EXPR);
8824 id = ansi_opname (BIT_IOR_EXPR);
8828 id = ansi_opname (BIT_NOT_EXPR);
8832 id = ansi_opname (TRUTH_NOT_EXPR);
8836 id = ansi_assopname (NOP_EXPR);
8840 id = ansi_opname (LT_EXPR);
8844 id = ansi_opname (GT_EXPR);
8848 id = ansi_assopname (PLUS_EXPR);
8852 id = ansi_assopname (MINUS_EXPR);
8856 id = ansi_assopname (MULT_EXPR);
8860 id = ansi_assopname (TRUNC_DIV_EXPR);
8864 id = ansi_assopname (TRUNC_MOD_EXPR);
8868 id = ansi_assopname (BIT_XOR_EXPR);
8872 id = ansi_assopname (BIT_AND_EXPR);
8876 id = ansi_assopname (BIT_IOR_EXPR);
8880 id = ansi_opname (LSHIFT_EXPR);
8884 id = ansi_opname (RSHIFT_EXPR);
8888 id = ansi_assopname (LSHIFT_EXPR);
8892 id = ansi_assopname (RSHIFT_EXPR);
8896 id = ansi_opname (EQ_EXPR);
8900 id = ansi_opname (NE_EXPR);
8904 id = ansi_opname (LE_EXPR);
8907 case CPP_GREATER_EQ:
8908 id = ansi_opname (GE_EXPR);
8912 id = ansi_opname (TRUTH_ANDIF_EXPR);
8916 id = ansi_opname (TRUTH_ORIF_EXPR);
8920 id = ansi_opname (POSTINCREMENT_EXPR);
8923 case CPP_MINUS_MINUS:
8924 id = ansi_opname (PREDECREMENT_EXPR);
8928 id = ansi_opname (COMPOUND_EXPR);
8931 case CPP_DEREF_STAR:
8932 id = ansi_opname (MEMBER_REF);
8936 id = ansi_opname (COMPONENT_REF);
8939 case CPP_OPEN_PAREN:
8940 /* Consume the `('. */
8941 cp_lexer_consume_token (parser->lexer);
8942 /* Look for the matching `)'. */
8943 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8944 return ansi_opname (CALL_EXPR);
8946 case CPP_OPEN_SQUARE:
8947 /* Consume the `['. */
8948 cp_lexer_consume_token (parser->lexer);
8949 /* Look for the matching `]'. */
8950 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8951 return ansi_opname (ARRAY_REF);
8954 /* Anything else is an error. */
8958 /* If we have selected an identifier, we need to consume the
8961 cp_lexer_consume_token (parser->lexer);
8962 /* Otherwise, no valid operator name was present. */
8965 cp_parser_error (parser, "expected operator");
8966 id = error_mark_node;
8972 /* Parse a template-declaration.
8974 template-declaration:
8975 export [opt] template < template-parameter-list > declaration
8977 If MEMBER_P is TRUE, this template-declaration occurs within a
8980 The grammar rule given by the standard isn't correct. What
8983 template-declaration:
8984 export [opt] template-parameter-list-seq
8985 decl-specifier-seq [opt] init-declarator [opt] ;
8986 export [opt] template-parameter-list-seq
8989 template-parameter-list-seq:
8990 template-parameter-list-seq [opt]
8991 template < template-parameter-list > */
8994 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8996 /* Check for `export'. */
8997 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8999 /* Consume the `export' token. */
9000 cp_lexer_consume_token (parser->lexer);
9001 /* Warn that we do not support `export'. */
9002 warning (0, "keyword %<export%> not implemented, and will be ignored");
9005 cp_parser_template_declaration_after_export (parser, member_p);
9008 /* Parse a template-parameter-list.
9010 template-parameter-list:
9012 template-parameter-list , template-parameter
9014 Returns a TREE_LIST. Each node represents a template parameter.
9015 The nodes are connected via their TREE_CHAINs. */
9018 cp_parser_template_parameter_list (cp_parser* parser)
9020 tree parameter_list = NULL_TREE;
9022 begin_template_parm_list ();
9028 bool is_parameter_pack;
9030 /* Parse the template-parameter. */
9031 parameter = cp_parser_template_parameter (parser,
9033 &is_parameter_pack);
9034 /* Add it to the list. */
9035 if (parameter != error_mark_node)
9036 parameter_list = process_template_parm (parameter_list,
9042 tree err_parm = build_tree_list (parameter, parameter);
9043 TREE_VALUE (err_parm) = error_mark_node;
9044 parameter_list = chainon (parameter_list, err_parm);
9047 /* Peek at the next token. */
9048 token = cp_lexer_peek_token (parser->lexer);
9049 /* If it's not a `,', we're done. */
9050 if (token->type != CPP_COMMA)
9052 /* Otherwise, consume the `,' token. */
9053 cp_lexer_consume_token (parser->lexer);
9056 return end_template_parm_list (parameter_list);
9059 /* Parse a template-parameter.
9063 parameter-declaration
9065 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9066 the parameter. The TREE_PURPOSE is the default value, if any.
9067 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9068 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9069 set to true iff this parameter is a parameter pack. */
9072 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9073 bool *is_parameter_pack)
9076 cp_parameter_declarator *parameter_declarator;
9079 /* Assume it is a type parameter or a template parameter. */
9080 *is_non_type = false;
9081 /* Assume it not a parameter pack. */
9082 *is_parameter_pack = false;
9083 /* Peek at the next token. */
9084 token = cp_lexer_peek_token (parser->lexer);
9085 /* If it is `class' or `template', we have a type-parameter. */
9086 if (token->keyword == RID_TEMPLATE)
9087 return cp_parser_type_parameter (parser, is_parameter_pack);
9088 /* If it is `class' or `typename' we do not know yet whether it is a
9089 type parameter or a non-type parameter. Consider:
9091 template <typename T, typename T::X X> ...
9095 template <class C, class D*> ...
9097 Here, the first parameter is a type parameter, and the second is
9098 a non-type parameter. We can tell by looking at the token after
9099 the identifier -- if it is a `,', `=', or `>' then we have a type
9101 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9103 /* Peek at the token after `class' or `typename'. */
9104 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9105 /* If it's an ellipsis, we have a template type parameter
9107 if (token->type == CPP_ELLIPSIS)
9108 return cp_parser_type_parameter (parser, is_parameter_pack);
9109 /* If it's an identifier, skip it. */
9110 if (token->type == CPP_NAME)
9111 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9112 /* Now, see if the token looks like the end of a template
9114 if (token->type == CPP_COMMA
9115 || token->type == CPP_EQ
9116 || token->type == CPP_GREATER)
9117 return cp_parser_type_parameter (parser, is_parameter_pack);
9120 /* Otherwise, it is a non-type parameter.
9124 When parsing a default template-argument for a non-type
9125 template-parameter, the first non-nested `>' is taken as the end
9126 of the template parameter-list rather than a greater-than
9128 *is_non_type = true;
9129 parameter_declarator
9130 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9131 /*parenthesized_p=*/NULL);
9133 /* If the parameter declaration is marked as a parameter pack, set
9134 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9135 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9137 if (parameter_declarator
9138 && parameter_declarator->declarator
9139 && parameter_declarator->declarator->parameter_pack_p)
9141 *is_parameter_pack = true;
9142 parameter_declarator->declarator->parameter_pack_p = false;
9145 /* If the next token is an ellipsis, and we don't already have it
9146 marked as a parameter pack, then we have a parameter pack (that
9147 has no declarator); */
9148 if (!*is_parameter_pack
9149 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9150 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9152 /* Consume the `...'. */
9153 cp_lexer_consume_token (parser->lexer);
9154 maybe_warn_variadic_templates ();
9156 *is_parameter_pack = true;
9159 parm = grokdeclarator (parameter_declarator->declarator,
9160 ¶meter_declarator->decl_specifiers,
9161 PARM, /*initialized=*/0,
9163 if (parm == error_mark_node)
9164 return error_mark_node;
9166 return build_tree_list (parameter_declarator->default_argument, parm);
9169 /* Parse a type-parameter.
9172 class identifier [opt]
9173 class identifier [opt] = type-id
9174 typename identifier [opt]
9175 typename identifier [opt] = type-id
9176 template < template-parameter-list > class identifier [opt]
9177 template < template-parameter-list > class identifier [opt]
9180 GNU Extension (variadic templates):
9183 class ... identifier [opt]
9184 typename ... identifier [opt]
9186 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9187 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9188 the declaration of the parameter.
9190 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9193 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9198 /* Look for a keyword to tell us what kind of parameter this is. */
9199 token = cp_parser_require (parser, CPP_KEYWORD,
9200 "`class', `typename', or `template'");
9202 return error_mark_node;
9204 switch (token->keyword)
9210 tree default_argument;
9212 /* If the next token is an ellipsis, we have a template
9214 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9216 /* Consume the `...' token. */
9217 cp_lexer_consume_token (parser->lexer);
9218 maybe_warn_variadic_templates ();
9220 *is_parameter_pack = true;
9223 /* If the next token is an identifier, then it names the
9225 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9226 identifier = cp_parser_identifier (parser);
9228 identifier = NULL_TREE;
9230 /* Create the parameter. */
9231 parameter = finish_template_type_parm (class_type_node, identifier);
9233 /* If the next token is an `=', we have a default argument. */
9234 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9236 /* Consume the `=' token. */
9237 cp_lexer_consume_token (parser->lexer);
9238 /* Parse the default-argument. */
9239 push_deferring_access_checks (dk_no_deferred);
9240 default_argument = cp_parser_type_id (parser);
9242 /* Template parameter packs cannot have default
9244 if (*is_parameter_pack)
9247 error ("template parameter pack %qD cannot have a default argument",
9250 error ("template parameter packs cannot have default arguments");
9251 default_argument = NULL_TREE;
9253 pop_deferring_access_checks ();
9256 default_argument = NULL_TREE;
9258 /* Create the combined representation of the parameter and the
9259 default argument. */
9260 parameter = build_tree_list (default_argument, parameter);
9266 tree parameter_list;
9268 tree default_argument;
9270 /* Look for the `<'. */
9271 cp_parser_require (parser, CPP_LESS, "`<'");
9272 /* Parse the template-parameter-list. */
9273 parameter_list = cp_parser_template_parameter_list (parser);
9274 /* Look for the `>'. */
9275 cp_parser_require (parser, CPP_GREATER, "`>'");
9276 /* Look for the `class' keyword. */
9277 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9278 /* If the next token is an ellipsis, we have a template
9280 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9282 /* Consume the `...' token. */
9283 cp_lexer_consume_token (parser->lexer);
9284 maybe_warn_variadic_templates ();
9286 *is_parameter_pack = true;
9288 /* If the next token is an `=', then there is a
9289 default-argument. If the next token is a `>', we are at
9290 the end of the parameter-list. If the next token is a `,',
9291 then we are at the end of this parameter. */
9292 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9293 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9294 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9296 identifier = cp_parser_identifier (parser);
9297 /* Treat invalid names as if the parameter were nameless. */
9298 if (identifier == error_mark_node)
9299 identifier = NULL_TREE;
9302 identifier = NULL_TREE;
9304 /* Create the template parameter. */
9305 parameter = finish_template_template_parm (class_type_node,
9308 /* If the next token is an `=', then there is a
9309 default-argument. */
9310 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9314 /* Consume the `='. */
9315 cp_lexer_consume_token (parser->lexer);
9316 /* Parse the id-expression. */
9317 push_deferring_access_checks (dk_no_deferred);
9319 = cp_parser_id_expression (parser,
9320 /*template_keyword_p=*/false,
9321 /*check_dependency_p=*/true,
9322 /*template_p=*/&is_template,
9323 /*declarator_p=*/false,
9324 /*optional_p=*/false);
9325 if (TREE_CODE (default_argument) == TYPE_DECL)
9326 /* If the id-expression was a template-id that refers to
9327 a template-class, we already have the declaration here,
9328 so no further lookup is needed. */
9331 /* Look up the name. */
9333 = cp_parser_lookup_name (parser, default_argument,
9335 /*is_template=*/is_template,
9336 /*is_namespace=*/false,
9337 /*check_dependency=*/true,
9338 /*ambiguous_decls=*/NULL);
9339 /* See if the default argument is valid. */
9341 = check_template_template_default_arg (default_argument);
9343 /* Template parameter packs cannot have default
9345 if (*is_parameter_pack)
9348 error ("template parameter pack %qD cannot have a default argument",
9351 error ("template parameter packs cannot have default arguments");
9352 default_argument = NULL_TREE;
9354 pop_deferring_access_checks ();
9357 default_argument = NULL_TREE;
9359 /* Create the combined representation of the parameter and the
9360 default argument. */
9361 parameter = build_tree_list (default_argument, parameter);
9373 /* Parse a template-id.
9376 template-name < template-argument-list [opt] >
9378 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9379 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9380 returned. Otherwise, if the template-name names a function, or set
9381 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9382 names a class, returns a TYPE_DECL for the specialization.
9384 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9385 uninstantiated templates. */
9388 cp_parser_template_id (cp_parser *parser,
9389 bool template_keyword_p,
9390 bool check_dependency_p,
9391 bool is_declaration)
9397 cp_token_position start_of_id = 0;
9398 deferred_access_check *chk;
9399 VEC (deferred_access_check,gc) *access_check;
9400 cp_token *next_token, *next_token_2;
9403 /* If the next token corresponds to a template-id, there is no need
9405 next_token = cp_lexer_peek_token (parser->lexer);
9406 if (next_token->type == CPP_TEMPLATE_ID)
9408 struct tree_check *check_value;
9410 /* Get the stored value. */
9411 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9412 /* Perform any access checks that were deferred. */
9413 access_check = check_value->checks;
9417 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9420 perform_or_defer_access_check (chk->binfo,
9425 /* Return the stored value. */
9426 return check_value->value;
9429 /* Avoid performing name lookup if there is no possibility of
9430 finding a template-id. */
9431 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9432 || (next_token->type == CPP_NAME
9433 && !cp_parser_nth_token_starts_template_argument_list_p
9436 cp_parser_error (parser, "expected template-id");
9437 return error_mark_node;
9440 /* Remember where the template-id starts. */
9441 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9442 start_of_id = cp_lexer_token_position (parser->lexer, false);
9444 push_deferring_access_checks (dk_deferred);
9446 /* Parse the template-name. */
9447 is_identifier = false;
9448 template = cp_parser_template_name (parser, template_keyword_p,
9452 if (template == error_mark_node || is_identifier)
9454 pop_deferring_access_checks ();
9458 /* If we find the sequence `[:' after a template-name, it's probably
9459 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9460 parse correctly the argument list. */
9461 next_token = cp_lexer_peek_token (parser->lexer);
9462 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9463 if (next_token->type == CPP_OPEN_SQUARE
9464 && next_token->flags & DIGRAPH
9465 && next_token_2->type == CPP_COLON
9466 && !(next_token_2->flags & PREV_WHITE))
9468 cp_parser_parse_tentatively (parser);
9469 /* Change `:' into `::'. */
9470 next_token_2->type = CPP_SCOPE;
9471 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9473 cp_lexer_consume_token (parser->lexer);
9474 /* Parse the arguments. */
9475 arguments = cp_parser_enclosed_template_argument_list (parser);
9476 if (!cp_parser_parse_definitely (parser))
9478 /* If we couldn't parse an argument list, then we revert our changes
9479 and return simply an error. Maybe this is not a template-id
9481 next_token_2->type = CPP_COLON;
9482 cp_parser_error (parser, "expected %<<%>");
9483 pop_deferring_access_checks ();
9484 return error_mark_node;
9486 /* Otherwise, emit an error about the invalid digraph, but continue
9487 parsing because we got our argument list. */
9488 pedwarn ("%<<::%> cannot begin a template-argument list");
9489 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9490 "between %<<%> and %<::%>");
9491 if (!flag_permissive)
9496 inform ("(if you use -fpermissive G++ will accept your code)");
9503 /* Look for the `<' that starts the template-argument-list. */
9504 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9506 pop_deferring_access_checks ();
9507 return error_mark_node;
9509 /* Parse the arguments. */
9510 arguments = cp_parser_enclosed_template_argument_list (parser);
9513 /* Build a representation of the specialization. */
9514 if (TREE_CODE (template) == IDENTIFIER_NODE)
9515 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9516 else if (DECL_CLASS_TEMPLATE_P (template)
9517 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9519 bool entering_scope;
9520 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9521 template (rather than some instantiation thereof) only if
9522 is not nested within some other construct. For example, in
9523 "template <typename T> void f(T) { A<T>::", A<T> is just an
9524 instantiation of A. */
9525 entering_scope = (template_parm_scope_p ()
9526 && cp_lexer_next_token_is (parser->lexer,
9529 = finish_template_type (template, arguments, entering_scope);
9533 /* If it's not a class-template or a template-template, it should be
9534 a function-template. */
9535 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9536 || TREE_CODE (template) == OVERLOAD
9537 || BASELINK_P (template)));
9539 template_id = lookup_template_function (template, arguments);
9542 /* If parsing tentatively, replace the sequence of tokens that makes
9543 up the template-id with a CPP_TEMPLATE_ID token. That way,
9544 should we re-parse the token stream, we will not have to repeat
9545 the effort required to do the parse, nor will we issue duplicate
9546 error messages about problems during instantiation of the
9550 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9552 /* Reset the contents of the START_OF_ID token. */
9553 token->type = CPP_TEMPLATE_ID;
9554 /* Retrieve any deferred checks. Do not pop this access checks yet
9555 so the memory will not be reclaimed during token replacing below. */
9556 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9557 token->u.tree_check_value->value = template_id;
9558 token->u.tree_check_value->checks = get_deferred_access_checks ();
9559 token->keyword = RID_MAX;
9561 /* Purge all subsequent tokens. */
9562 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9564 /* ??? Can we actually assume that, if template_id ==
9565 error_mark_node, we will have issued a diagnostic to the
9566 user, as opposed to simply marking the tentative parse as
9568 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9569 error ("parse error in template argument list");
9572 pop_deferring_access_checks ();
9576 /* Parse a template-name.
9581 The standard should actually say:
9585 operator-function-id
9587 A defect report has been filed about this issue.
9589 A conversion-function-id cannot be a template name because they cannot
9590 be part of a template-id. In fact, looking at this code:
9594 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9595 It is impossible to call a templated conversion-function-id with an
9596 explicit argument list, since the only allowed template parameter is
9597 the type to which it is converting.
9599 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9600 `template' keyword, in a construction like:
9604 In that case `f' is taken to be a template-name, even though there
9605 is no way of knowing for sure.
9607 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9608 name refers to a set of overloaded functions, at least one of which
9609 is a template, or an IDENTIFIER_NODE with the name of the template,
9610 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9611 names are looked up inside uninstantiated templates. */
9614 cp_parser_template_name (cp_parser* parser,
9615 bool template_keyword_p,
9616 bool check_dependency_p,
9617 bool is_declaration,
9618 bool *is_identifier)
9624 /* If the next token is `operator', then we have either an
9625 operator-function-id or a conversion-function-id. */
9626 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9628 /* We don't know whether we're looking at an
9629 operator-function-id or a conversion-function-id. */
9630 cp_parser_parse_tentatively (parser);
9631 /* Try an operator-function-id. */
9632 identifier = cp_parser_operator_function_id (parser);
9633 /* If that didn't work, try a conversion-function-id. */
9634 if (!cp_parser_parse_definitely (parser))
9636 cp_parser_error (parser, "expected template-name");
9637 return error_mark_node;
9640 /* Look for the identifier. */
9642 identifier = cp_parser_identifier (parser);
9644 /* If we didn't find an identifier, we don't have a template-id. */
9645 if (identifier == error_mark_node)
9646 return error_mark_node;
9648 /* If the name immediately followed the `template' keyword, then it
9649 is a template-name. However, if the next token is not `<', then
9650 we do not treat it as a template-name, since it is not being used
9651 as part of a template-id. This enables us to handle constructs
9654 template <typename T> struct S { S(); };
9655 template <typename T> S<T>::S();
9657 correctly. We would treat `S' as a template -- if it were `S<T>'
9658 -- but we do not if there is no `<'. */
9660 if (processing_template_decl
9661 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9663 /* In a declaration, in a dependent context, we pretend that the
9664 "template" keyword was present in order to improve error
9665 recovery. For example, given:
9667 template <typename T> void f(T::X<int>);
9669 we want to treat "X<int>" as a template-id. */
9671 && !template_keyword_p
9672 && parser->scope && TYPE_P (parser->scope)
9673 && check_dependency_p
9674 && dependent_type_p (parser->scope)
9675 /* Do not do this for dtors (or ctors), since they never
9676 need the template keyword before their name. */
9677 && !constructor_name_p (identifier, parser->scope))
9679 cp_token_position start = 0;
9681 /* Explain what went wrong. */
9682 error ("non-template %qD used as template", identifier);
9683 inform ("use %<%T::template %D%> to indicate that it is a template",
9684 parser->scope, identifier);
9685 /* If parsing tentatively, find the location of the "<" token. */
9686 if (cp_parser_simulate_error (parser))
9687 start = cp_lexer_token_position (parser->lexer, true);
9688 /* Parse the template arguments so that we can issue error
9689 messages about them. */
9690 cp_lexer_consume_token (parser->lexer);
9691 cp_parser_enclosed_template_argument_list (parser);
9692 /* Skip tokens until we find a good place from which to
9693 continue parsing. */
9694 cp_parser_skip_to_closing_parenthesis (parser,
9695 /*recovering=*/true,
9697 /*consume_paren=*/false);
9698 /* If parsing tentatively, permanently remove the
9699 template argument list. That will prevent duplicate
9700 error messages from being issued about the missing
9701 "template" keyword. */
9703 cp_lexer_purge_tokens_after (parser->lexer, start);
9705 *is_identifier = true;
9709 /* If the "template" keyword is present, then there is generally
9710 no point in doing name-lookup, so we just return IDENTIFIER.
9711 But, if the qualifying scope is non-dependent then we can
9712 (and must) do name-lookup normally. */
9713 if (template_keyword_p
9715 || (TYPE_P (parser->scope)
9716 && dependent_type_p (parser->scope))))
9720 /* Look up the name. */
9721 decl = cp_parser_lookup_name (parser, identifier,
9723 /*is_template=*/false,
9724 /*is_namespace=*/false,
9726 /*ambiguous_decls=*/NULL);
9727 decl = maybe_get_template_decl_from_type_decl (decl);
9729 /* If DECL is a template, then the name was a template-name. */
9730 if (TREE_CODE (decl) == TEMPLATE_DECL)
9734 tree fn = NULL_TREE;
9736 /* The standard does not explicitly indicate whether a name that
9737 names a set of overloaded declarations, some of which are
9738 templates, is a template-name. However, such a name should
9739 be a template-name; otherwise, there is no way to form a
9740 template-id for the overloaded templates. */
9741 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9742 if (TREE_CODE (fns) == OVERLOAD)
9743 for (fn = fns; fn; fn = OVL_NEXT (fn))
9744 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9749 /* The name does not name a template. */
9750 cp_parser_error (parser, "expected template-name");
9751 return error_mark_node;
9755 /* If DECL is dependent, and refers to a function, then just return
9756 its name; we will look it up again during template instantiation. */
9757 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9759 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9760 if (TYPE_P (scope) && dependent_type_p (scope))
9767 /* Parse a template-argument-list.
9769 template-argument-list:
9770 template-argument ... [opt]
9771 template-argument-list , template-argument ... [opt]
9773 Returns a TREE_VEC containing the arguments. */
9776 cp_parser_template_argument_list (cp_parser* parser)
9778 tree fixed_args[10];
9779 unsigned n_args = 0;
9780 unsigned alloced = 10;
9781 tree *arg_ary = fixed_args;
9783 bool saved_in_template_argument_list_p;
9785 bool saved_non_ice_p;
9787 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9788 parser->in_template_argument_list_p = true;
9789 /* Even if the template-id appears in an integral
9790 constant-expression, the contents of the argument list do
9792 saved_ice_p = parser->integral_constant_expression_p;
9793 parser->integral_constant_expression_p = false;
9794 saved_non_ice_p = parser->non_integral_constant_expression_p;
9795 parser->non_integral_constant_expression_p = false;
9796 /* Parse the arguments. */
9802 /* Consume the comma. */
9803 cp_lexer_consume_token (parser->lexer);
9805 /* Parse the template-argument. */
9806 argument = cp_parser_template_argument (parser);
9808 /* If the next token is an ellipsis, we're expanding a template
9810 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9812 /* Consume the `...' token. */
9813 cp_lexer_consume_token (parser->lexer);
9815 /* Make the argument into a TYPE_PACK_EXPANSION or
9816 EXPR_PACK_EXPANSION. */
9817 argument = make_pack_expansion (argument);
9820 if (n_args == alloced)
9824 if (arg_ary == fixed_args)
9826 arg_ary = XNEWVEC (tree, alloced);
9827 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9830 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9832 arg_ary[n_args++] = argument;
9834 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9836 vec = make_tree_vec (n_args);
9839 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9841 if (arg_ary != fixed_args)
9843 parser->non_integral_constant_expression_p = saved_non_ice_p;
9844 parser->integral_constant_expression_p = saved_ice_p;
9845 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9849 /* Parse a template-argument.
9852 assignment-expression
9856 The representation is that of an assignment-expression, type-id, or
9857 id-expression -- except that the qualified id-expression is
9858 evaluated, so that the value returned is either a DECL or an
9861 Although the standard says "assignment-expression", it forbids
9862 throw-expressions or assignments in the template argument.
9863 Therefore, we use "conditional-expression" instead. */
9866 cp_parser_template_argument (cp_parser* parser)
9871 bool maybe_type_id = false;
9875 /* There's really no way to know what we're looking at, so we just
9876 try each alternative in order.
9880 In a template-argument, an ambiguity between a type-id and an
9881 expression is resolved to a type-id, regardless of the form of
9882 the corresponding template-parameter.
9884 Therefore, we try a type-id first. */
9885 cp_parser_parse_tentatively (parser);
9886 argument = cp_parser_type_id (parser);
9887 /* If there was no error parsing the type-id but the next token is a '>>',
9888 we probably found a typo for '> >'. But there are type-id which are
9889 also valid expressions. For instance:
9891 struct X { int operator >> (int); };
9892 template <int V> struct Foo {};
9895 Here 'X()' is a valid type-id of a function type, but the user just
9896 wanted to write the expression "X() >> 5". Thus, we remember that we
9897 found a valid type-id, but we still try to parse the argument as an
9898 expression to see what happens. */
9899 if (!cp_parser_error_occurred (parser)
9900 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9902 maybe_type_id = true;
9903 cp_parser_abort_tentative_parse (parser);
9907 /* If the next token isn't a `,' or a `>', then this argument wasn't
9908 really finished. This means that the argument is not a valid
9910 if (!cp_parser_next_token_ends_template_argument_p (parser))
9911 cp_parser_error (parser, "expected template-argument");
9912 /* If that worked, we're done. */
9913 if (cp_parser_parse_definitely (parser))
9916 /* We're still not sure what the argument will be. */
9917 cp_parser_parse_tentatively (parser);
9918 /* Try a template. */
9919 argument = cp_parser_id_expression (parser,
9920 /*template_keyword_p=*/false,
9921 /*check_dependency_p=*/true,
9923 /*declarator_p=*/false,
9924 /*optional_p=*/false);
9925 /* If the next token isn't a `,' or a `>', then this argument wasn't
9927 if (!cp_parser_next_token_ends_template_argument_p (parser))
9928 cp_parser_error (parser, "expected template-argument");
9929 if (!cp_parser_error_occurred (parser))
9931 /* Figure out what is being referred to. If the id-expression
9932 was for a class template specialization, then we will have a
9933 TYPE_DECL at this point. There is no need to do name lookup
9934 at this point in that case. */
9935 if (TREE_CODE (argument) != TYPE_DECL)
9936 argument = cp_parser_lookup_name (parser, argument,
9938 /*is_template=*/template_p,
9939 /*is_namespace=*/false,
9940 /*check_dependency=*/true,
9941 /*ambiguous_decls=*/NULL);
9942 if (TREE_CODE (argument) != TEMPLATE_DECL
9943 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9944 cp_parser_error (parser, "expected template-name");
9946 if (cp_parser_parse_definitely (parser))
9948 /* It must be a non-type argument. There permitted cases are given
9949 in [temp.arg.nontype]:
9951 -- an integral constant-expression of integral or enumeration
9954 -- the name of a non-type template-parameter; or
9956 -- the name of an object or function with external linkage...
9958 -- the address of an object or function with external linkage...
9960 -- a pointer to member... */
9961 /* Look for a non-type template parameter. */
9962 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9964 cp_parser_parse_tentatively (parser);
9965 argument = cp_parser_primary_expression (parser,
9968 /*template_arg_p=*/true,
9970 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9971 || !cp_parser_next_token_ends_template_argument_p (parser))
9972 cp_parser_simulate_error (parser);
9973 if (cp_parser_parse_definitely (parser))
9977 /* If the next token is "&", the argument must be the address of an
9978 object or function with external linkage. */
9979 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9981 cp_lexer_consume_token (parser->lexer);
9982 /* See if we might have an id-expression. */
9983 token = cp_lexer_peek_token (parser->lexer);
9984 if (token->type == CPP_NAME
9985 || token->keyword == RID_OPERATOR
9986 || token->type == CPP_SCOPE
9987 || token->type == CPP_TEMPLATE_ID
9988 || token->type == CPP_NESTED_NAME_SPECIFIER)
9990 cp_parser_parse_tentatively (parser);
9991 argument = cp_parser_primary_expression (parser,
9994 /*template_arg_p=*/true,
9996 if (cp_parser_error_occurred (parser)
9997 || !cp_parser_next_token_ends_template_argument_p (parser))
9998 cp_parser_abort_tentative_parse (parser);
10001 if (TREE_CODE (argument) == INDIRECT_REF)
10003 gcc_assert (REFERENCE_REF_P (argument));
10004 argument = TREE_OPERAND (argument, 0);
10007 if (TREE_CODE (argument) == VAR_DECL)
10009 /* A variable without external linkage might still be a
10010 valid constant-expression, so no error is issued here
10011 if the external-linkage check fails. */
10012 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10013 cp_parser_simulate_error (parser);
10015 else if (is_overloaded_fn (argument))
10016 /* All overloaded functions are allowed; if the external
10017 linkage test does not pass, an error will be issued
10021 && (TREE_CODE (argument) == OFFSET_REF
10022 || TREE_CODE (argument) == SCOPE_REF))
10023 /* A pointer-to-member. */
10025 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10028 cp_parser_simulate_error (parser);
10030 if (cp_parser_parse_definitely (parser))
10033 argument = build_x_unary_op (ADDR_EXPR, argument);
10038 /* If the argument started with "&", there are no other valid
10039 alternatives at this point. */
10042 cp_parser_error (parser, "invalid non-type template argument");
10043 return error_mark_node;
10046 /* If the argument wasn't successfully parsed as a type-id followed
10047 by '>>', the argument can only be a constant expression now.
10048 Otherwise, we try parsing the constant-expression tentatively,
10049 because the argument could really be a type-id. */
10051 cp_parser_parse_tentatively (parser);
10052 argument = cp_parser_constant_expression (parser,
10053 /*allow_non_constant_p=*/false,
10054 /*non_constant_p=*/NULL);
10055 argument = fold_non_dependent_expr (argument);
10056 if (!maybe_type_id)
10058 if (!cp_parser_next_token_ends_template_argument_p (parser))
10059 cp_parser_error (parser, "expected template-argument");
10060 if (cp_parser_parse_definitely (parser))
10062 /* We did our best to parse the argument as a non type-id, but that
10063 was the only alternative that matched (albeit with a '>' after
10064 it). We can assume it's just a typo from the user, and a
10065 diagnostic will then be issued. */
10066 return cp_parser_type_id (parser);
10069 /* Parse an explicit-instantiation.
10071 explicit-instantiation:
10072 template declaration
10074 Although the standard says `declaration', what it really means is:
10076 explicit-instantiation:
10077 template decl-specifier-seq [opt] declarator [opt] ;
10079 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10080 supposed to be allowed. A defect report has been filed about this
10085 explicit-instantiation:
10086 storage-class-specifier template
10087 decl-specifier-seq [opt] declarator [opt] ;
10088 function-specifier template
10089 decl-specifier-seq [opt] declarator [opt] ; */
10092 cp_parser_explicit_instantiation (cp_parser* parser)
10094 int declares_class_or_enum;
10095 cp_decl_specifier_seq decl_specifiers;
10096 tree extension_specifier = NULL_TREE;
10098 /* Look for an (optional) storage-class-specifier or
10099 function-specifier. */
10100 if (cp_parser_allow_gnu_extensions_p (parser))
10102 extension_specifier
10103 = cp_parser_storage_class_specifier_opt (parser);
10104 if (!extension_specifier)
10105 extension_specifier
10106 = cp_parser_function_specifier_opt (parser,
10107 /*decl_specs=*/NULL);
10110 /* Look for the `template' keyword. */
10111 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10112 /* Let the front end know that we are processing an explicit
10114 begin_explicit_instantiation ();
10115 /* [temp.explicit] says that we are supposed to ignore access
10116 control while processing explicit instantiation directives. */
10117 push_deferring_access_checks (dk_no_check);
10118 /* Parse a decl-specifier-seq. */
10119 cp_parser_decl_specifier_seq (parser,
10120 CP_PARSER_FLAGS_OPTIONAL,
10122 &declares_class_or_enum);
10123 /* If there was exactly one decl-specifier, and it declared a class,
10124 and there's no declarator, then we have an explicit type
10126 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10130 type = check_tag_decl (&decl_specifiers);
10131 /* Turn access control back on for names used during
10132 template instantiation. */
10133 pop_deferring_access_checks ();
10135 do_type_instantiation (type, extension_specifier,
10136 /*complain=*/tf_error);
10140 cp_declarator *declarator;
10143 /* Parse the declarator. */
10145 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10146 /*ctor_dtor_or_conv_p=*/NULL,
10147 /*parenthesized_p=*/NULL,
10148 /*member_p=*/false);
10149 if (declares_class_or_enum & 2)
10150 cp_parser_check_for_definition_in_return_type (declarator,
10151 decl_specifiers.type);
10152 if (declarator != cp_error_declarator)
10154 decl = grokdeclarator (declarator, &decl_specifiers,
10155 NORMAL, 0, &decl_specifiers.attributes);
10156 /* Turn access control back on for names used during
10157 template instantiation. */
10158 pop_deferring_access_checks ();
10159 /* Do the explicit instantiation. */
10160 do_decl_instantiation (decl, extension_specifier);
10164 pop_deferring_access_checks ();
10165 /* Skip the body of the explicit instantiation. */
10166 cp_parser_skip_to_end_of_statement (parser);
10169 /* We're done with the instantiation. */
10170 end_explicit_instantiation ();
10172 cp_parser_consume_semicolon_at_end_of_statement (parser);
10175 /* Parse an explicit-specialization.
10177 explicit-specialization:
10178 template < > declaration
10180 Although the standard says `declaration', what it really means is:
10182 explicit-specialization:
10183 template <> decl-specifier [opt] init-declarator [opt] ;
10184 template <> function-definition
10185 template <> explicit-specialization
10186 template <> template-declaration */
10189 cp_parser_explicit_specialization (cp_parser* parser)
10191 bool need_lang_pop;
10192 /* Look for the `template' keyword. */
10193 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10194 /* Look for the `<'. */
10195 cp_parser_require (parser, CPP_LESS, "`<'");
10196 /* Look for the `>'. */
10197 cp_parser_require (parser, CPP_GREATER, "`>'");
10198 /* We have processed another parameter list. */
10199 ++parser->num_template_parameter_lists;
10202 A template ... explicit specialization ... shall not have C
10204 if (current_lang_name == lang_name_c)
10206 error ("template specialization with C linkage");
10207 /* Give it C++ linkage to avoid confusing other parts of the
10209 push_lang_context (lang_name_cplusplus);
10210 need_lang_pop = true;
10213 need_lang_pop = false;
10214 /* Let the front end know that we are beginning a specialization. */
10215 if (!begin_specialization ())
10217 end_specialization ();
10218 cp_parser_skip_to_end_of_block_or_statement (parser);
10222 /* If the next keyword is `template', we need to figure out whether
10223 or not we're looking a template-declaration. */
10224 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10226 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10227 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10228 cp_parser_template_declaration_after_export (parser,
10229 /*member_p=*/false);
10231 cp_parser_explicit_specialization (parser);
10234 /* Parse the dependent declaration. */
10235 cp_parser_single_declaration (parser,
10237 /*member_p=*/false,
10238 /*explicit_specialization_p=*/true,
10239 /*friend_p=*/NULL);
10240 /* We're done with the specialization. */
10241 end_specialization ();
10242 /* For the erroneous case of a template with C linkage, we pushed an
10243 implicit C++ linkage scope; exit that scope now. */
10245 pop_lang_context ();
10246 /* We're done with this parameter list. */
10247 --parser->num_template_parameter_lists;
10250 /* Parse a type-specifier.
10253 simple-type-specifier
10256 elaborated-type-specifier
10264 Returns a representation of the type-specifier. For a
10265 class-specifier, enum-specifier, or elaborated-type-specifier, a
10266 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10268 The parser flags FLAGS is used to control type-specifier parsing.
10270 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10271 in a decl-specifier-seq.
10273 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10274 class-specifier, enum-specifier, or elaborated-type-specifier, then
10275 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10276 if a type is declared; 2 if it is defined. Otherwise, it is set to
10279 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10280 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10281 is set to FALSE. */
10284 cp_parser_type_specifier (cp_parser* parser,
10285 cp_parser_flags flags,
10286 cp_decl_specifier_seq *decl_specs,
10287 bool is_declaration,
10288 int* declares_class_or_enum,
10289 bool* is_cv_qualifier)
10291 tree type_spec = NULL_TREE;
10294 cp_decl_spec ds = ds_last;
10296 /* Assume this type-specifier does not declare a new type. */
10297 if (declares_class_or_enum)
10298 *declares_class_or_enum = 0;
10299 /* And that it does not specify a cv-qualifier. */
10300 if (is_cv_qualifier)
10301 *is_cv_qualifier = false;
10302 /* Peek at the next token. */
10303 token = cp_lexer_peek_token (parser->lexer);
10305 /* If we're looking at a keyword, we can use that to guide the
10306 production we choose. */
10307 keyword = token->keyword;
10311 /* Look for the enum-specifier. */
10312 type_spec = cp_parser_enum_specifier (parser);
10313 /* If that worked, we're done. */
10316 if (declares_class_or_enum)
10317 *declares_class_or_enum = 2;
10319 cp_parser_set_decl_spec_type (decl_specs,
10321 /*user_defined_p=*/true);
10325 goto elaborated_type_specifier;
10327 /* Any of these indicate either a class-specifier, or an
10328 elaborated-type-specifier. */
10332 /* Parse tentatively so that we can back up if we don't find a
10333 class-specifier. */
10334 cp_parser_parse_tentatively (parser);
10335 /* Look for the class-specifier. */
10336 type_spec = cp_parser_class_specifier (parser);
10337 /* If that worked, we're done. */
10338 if (cp_parser_parse_definitely (parser))
10340 if (declares_class_or_enum)
10341 *declares_class_or_enum = 2;
10343 cp_parser_set_decl_spec_type (decl_specs,
10345 /*user_defined_p=*/true);
10349 /* Fall through. */
10350 elaborated_type_specifier:
10351 /* We're declaring (not defining) a class or enum. */
10352 if (declares_class_or_enum)
10353 *declares_class_or_enum = 1;
10355 /* Fall through. */
10357 /* Look for an elaborated-type-specifier. */
10359 = (cp_parser_elaborated_type_specifier
10361 decl_specs && decl_specs->specs[(int) ds_friend],
10364 cp_parser_set_decl_spec_type (decl_specs,
10366 /*user_defined_p=*/true);
10371 if (is_cv_qualifier)
10372 *is_cv_qualifier = true;
10377 if (is_cv_qualifier)
10378 *is_cv_qualifier = true;
10383 if (is_cv_qualifier)
10384 *is_cv_qualifier = true;
10388 /* The `__complex__' keyword is a GNU extension. */
10396 /* Handle simple keywords. */
10401 ++decl_specs->specs[(int)ds];
10402 decl_specs->any_specifiers_p = true;
10404 return cp_lexer_consume_token (parser->lexer)->u.value;
10407 /* If we do not already have a type-specifier, assume we are looking
10408 at a simple-type-specifier. */
10409 type_spec = cp_parser_simple_type_specifier (parser,
10413 /* If we didn't find a type-specifier, and a type-specifier was not
10414 optional in this context, issue an error message. */
10415 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10417 cp_parser_error (parser, "expected type specifier");
10418 return error_mark_node;
10424 /* Parse a simple-type-specifier.
10426 simple-type-specifier:
10427 :: [opt] nested-name-specifier [opt] type-name
10428 :: [opt] nested-name-specifier template template-id
10443 simple-type-specifier:
10444 __typeof__ unary-expression
10445 __typeof__ ( type-id )
10447 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10448 appropriately updated. */
10451 cp_parser_simple_type_specifier (cp_parser* parser,
10452 cp_decl_specifier_seq *decl_specs,
10453 cp_parser_flags flags)
10455 tree type = NULL_TREE;
10458 /* Peek at the next token. */
10459 token = cp_lexer_peek_token (parser->lexer);
10461 /* If we're looking at a keyword, things are easy. */
10462 switch (token->keyword)
10466 decl_specs->explicit_char_p = true;
10467 type = char_type_node;
10470 type = wchar_type_node;
10473 type = boolean_type_node;
10477 ++decl_specs->specs[(int) ds_short];
10478 type = short_integer_type_node;
10482 decl_specs->explicit_int_p = true;
10483 type = integer_type_node;
10487 ++decl_specs->specs[(int) ds_long];
10488 type = long_integer_type_node;
10492 ++decl_specs->specs[(int) ds_signed];
10493 type = integer_type_node;
10497 ++decl_specs->specs[(int) ds_unsigned];
10498 type = unsigned_type_node;
10501 type = float_type_node;
10504 type = double_type_node;
10507 type = void_type_node;
10511 /* Consume the `typeof' token. */
10512 cp_lexer_consume_token (parser->lexer);
10513 /* Parse the operand to `typeof'. */
10514 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10515 /* If it is not already a TYPE, take its type. */
10516 if (!TYPE_P (type))
10517 type = finish_typeof (type);
10520 cp_parser_set_decl_spec_type (decl_specs, type,
10521 /*user_defined_p=*/true);
10529 /* If the type-specifier was for a built-in type, we're done. */
10534 /* Record the type. */
10536 && (token->keyword != RID_SIGNED
10537 && token->keyword != RID_UNSIGNED
10538 && token->keyword != RID_SHORT
10539 && token->keyword != RID_LONG))
10540 cp_parser_set_decl_spec_type (decl_specs,
10542 /*user_defined=*/false);
10544 decl_specs->any_specifiers_p = true;
10546 /* Consume the token. */
10547 id = cp_lexer_consume_token (parser->lexer)->u.value;
10549 /* There is no valid C++ program where a non-template type is
10550 followed by a "<". That usually indicates that the user thought
10551 that the type was a template. */
10552 cp_parser_check_for_invalid_template_id (parser, type);
10554 return TYPE_NAME (type);
10557 /* The type-specifier must be a user-defined type. */
10558 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10563 /* Don't gobble tokens or issue error messages if this is an
10564 optional type-specifier. */
10565 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10566 cp_parser_parse_tentatively (parser);
10568 /* Look for the optional `::' operator. */
10570 = (cp_parser_global_scope_opt (parser,
10571 /*current_scope_valid_p=*/false)
10573 /* Look for the nested-name specifier. */
10575 = (cp_parser_nested_name_specifier_opt (parser,
10576 /*typename_keyword_p=*/false,
10577 /*check_dependency_p=*/true,
10579 /*is_declaration=*/false)
10581 /* If we have seen a nested-name-specifier, and the next token
10582 is `template', then we are using the template-id production. */
10584 && cp_parser_optional_template_keyword (parser))
10586 /* Look for the template-id. */
10587 type = cp_parser_template_id (parser,
10588 /*template_keyword_p=*/true,
10589 /*check_dependency_p=*/true,
10590 /*is_declaration=*/false);
10591 /* If the template-id did not name a type, we are out of
10593 if (TREE_CODE (type) != TYPE_DECL)
10595 cp_parser_error (parser, "expected template-id for type");
10599 /* Otherwise, look for a type-name. */
10601 type = cp_parser_type_name (parser);
10602 /* Keep track of all name-lookups performed in class scopes. */
10606 && TREE_CODE (type) == TYPE_DECL
10607 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10608 maybe_note_name_used_in_class (DECL_NAME (type), type);
10609 /* If it didn't work out, we don't have a TYPE. */
10610 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10611 && !cp_parser_parse_definitely (parser))
10613 if (type && decl_specs)
10614 cp_parser_set_decl_spec_type (decl_specs, type,
10615 /*user_defined=*/true);
10618 /* If we didn't get a type-name, issue an error message. */
10619 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10621 cp_parser_error (parser, "expected type-name");
10622 return error_mark_node;
10625 /* There is no valid C++ program where a non-template type is
10626 followed by a "<". That usually indicates that the user thought
10627 that the type was a template. */
10628 if (type && type != error_mark_node)
10630 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10631 If it is, then the '<'...'>' enclose protocol names rather than
10632 template arguments, and so everything is fine. */
10633 if (c_dialect_objc ()
10634 && (objc_is_id (type) || objc_is_class_name (type)))
10636 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10637 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10639 /* Clobber the "unqualified" type previously entered into
10640 DECL_SPECS with the new, improved protocol-qualified version. */
10642 decl_specs->type = qual_type;
10647 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10653 /* Parse a type-name.
10666 Returns a TYPE_DECL for the type. */
10669 cp_parser_type_name (cp_parser* parser)
10674 /* We can't know yet whether it is a class-name or not. */
10675 cp_parser_parse_tentatively (parser);
10676 /* Try a class-name. */
10677 type_decl = cp_parser_class_name (parser,
10678 /*typename_keyword_p=*/false,
10679 /*template_keyword_p=*/false,
10681 /*check_dependency_p=*/true,
10682 /*class_head_p=*/false,
10683 /*is_declaration=*/false);
10684 /* If it's not a class-name, keep looking. */
10685 if (!cp_parser_parse_definitely (parser))
10687 /* It must be a typedef-name or an enum-name. */
10688 identifier = cp_parser_identifier (parser);
10689 if (identifier == error_mark_node)
10690 return error_mark_node;
10692 /* Look up the type-name. */
10693 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10695 if (TREE_CODE (type_decl) != TYPE_DECL
10696 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10698 /* See if this is an Objective-C type. */
10699 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10700 tree type = objc_get_protocol_qualified_type (identifier, protos);
10702 type_decl = TYPE_NAME (type);
10705 /* Issue an error if we did not find a type-name. */
10706 if (TREE_CODE (type_decl) != TYPE_DECL)
10708 if (!cp_parser_simulate_error (parser))
10709 cp_parser_name_lookup_error (parser, identifier, type_decl,
10711 type_decl = error_mark_node;
10713 /* Remember that the name was used in the definition of the
10714 current class so that we can check later to see if the
10715 meaning would have been different after the class was
10716 entirely defined. */
10717 else if (type_decl != error_mark_node
10719 maybe_note_name_used_in_class (identifier, type_decl);
10726 /* Parse an elaborated-type-specifier. Note that the grammar given
10727 here incorporates the resolution to DR68.
10729 elaborated-type-specifier:
10730 class-key :: [opt] nested-name-specifier [opt] identifier
10731 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10732 enum :: [opt] nested-name-specifier [opt] identifier
10733 typename :: [opt] nested-name-specifier identifier
10734 typename :: [opt] nested-name-specifier template [opt]
10739 elaborated-type-specifier:
10740 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10741 class-key attributes :: [opt] nested-name-specifier [opt]
10742 template [opt] template-id
10743 enum attributes :: [opt] nested-name-specifier [opt] identifier
10745 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10746 declared `friend'. If IS_DECLARATION is TRUE, then this
10747 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10748 something is being declared.
10750 Returns the TYPE specified. */
10753 cp_parser_elaborated_type_specifier (cp_parser* parser,
10755 bool is_declaration)
10757 enum tag_types tag_type;
10759 tree type = NULL_TREE;
10760 tree attributes = NULL_TREE;
10762 /* See if we're looking at the `enum' keyword. */
10763 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10765 /* Consume the `enum' token. */
10766 cp_lexer_consume_token (parser->lexer);
10767 /* Remember that it's an enumeration type. */
10768 tag_type = enum_type;
10769 /* Parse the attributes. */
10770 attributes = cp_parser_attributes_opt (parser);
10772 /* Or, it might be `typename'. */
10773 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10776 /* Consume the `typename' token. */
10777 cp_lexer_consume_token (parser->lexer);
10778 /* Remember that it's a `typename' type. */
10779 tag_type = typename_type;
10780 /* The `typename' keyword is only allowed in templates. */
10781 if (!processing_template_decl)
10782 pedwarn ("using %<typename%> outside of template");
10784 /* Otherwise it must be a class-key. */
10787 tag_type = cp_parser_class_key (parser);
10788 if (tag_type == none_type)
10789 return error_mark_node;
10790 /* Parse the attributes. */
10791 attributes = cp_parser_attributes_opt (parser);
10794 /* Look for the `::' operator. */
10795 cp_parser_global_scope_opt (parser,
10796 /*current_scope_valid_p=*/false);
10797 /* Look for the nested-name-specifier. */
10798 if (tag_type == typename_type)
10800 if (!cp_parser_nested_name_specifier (parser,
10801 /*typename_keyword_p=*/true,
10802 /*check_dependency_p=*/true,
10805 return error_mark_node;
10808 /* Even though `typename' is not present, the proposed resolution
10809 to Core Issue 180 says that in `class A<T>::B', `B' should be
10810 considered a type-name, even if `A<T>' is dependent. */
10811 cp_parser_nested_name_specifier_opt (parser,
10812 /*typename_keyword_p=*/true,
10813 /*check_dependency_p=*/true,
10816 /* For everything but enumeration types, consider a template-id.
10817 For an enumeration type, consider only a plain identifier. */
10818 if (tag_type != enum_type)
10820 bool template_p = false;
10823 /* Allow the `template' keyword. */
10824 template_p = cp_parser_optional_template_keyword (parser);
10825 /* If we didn't see `template', we don't know if there's a
10826 template-id or not. */
10828 cp_parser_parse_tentatively (parser);
10829 /* Parse the template-id. */
10830 decl = cp_parser_template_id (parser, template_p,
10831 /*check_dependency_p=*/true,
10833 /* If we didn't find a template-id, look for an ordinary
10835 if (!template_p && !cp_parser_parse_definitely (parser))
10837 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10838 in effect, then we must assume that, upon instantiation, the
10839 template will correspond to a class. */
10840 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10841 && tag_type == typename_type)
10842 type = make_typename_type (parser->scope, decl,
10844 /*complain=*/tf_error);
10846 type = TREE_TYPE (decl);
10851 identifier = cp_parser_identifier (parser);
10853 if (identifier == error_mark_node)
10855 parser->scope = NULL_TREE;
10856 return error_mark_node;
10859 /* For a `typename', we needn't call xref_tag. */
10860 if (tag_type == typename_type
10861 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10862 return cp_parser_make_typename_type (parser, parser->scope,
10864 /* Look up a qualified name in the usual way. */
10868 tree ambiguous_decls;
10870 decl = cp_parser_lookup_name (parser, identifier,
10872 /*is_template=*/false,
10873 /*is_namespace=*/false,
10874 /*check_dependency=*/true,
10877 /* If the lookup was ambiguous, an error will already have been
10879 if (ambiguous_decls)
10880 return error_mark_node;
10882 /* If we are parsing friend declaration, DECL may be a
10883 TEMPLATE_DECL tree node here. However, we need to check
10884 whether this TEMPLATE_DECL results in valid code. Consider
10885 the following example:
10888 template <class T> class C {};
10891 template <class T> friend class N::C; // #1, valid code
10893 template <class T> class Y {
10894 friend class N::C; // #2, invalid code
10897 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10898 name lookup of `N::C'. We see that friend declaration must
10899 be template for the code to be valid. Note that
10900 processing_template_decl does not work here since it is
10901 always 1 for the above two cases. */
10903 decl = (cp_parser_maybe_treat_template_as_class
10904 (decl, /*tag_name_p=*/is_friend
10905 && parser->num_template_parameter_lists));
10907 if (TREE_CODE (decl) != TYPE_DECL)
10909 cp_parser_diagnose_invalid_type_name (parser,
10912 return error_mark_node;
10915 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10917 bool allow_template = (parser->num_template_parameter_lists
10918 || DECL_SELF_REFERENCE_P (decl));
10919 type = check_elaborated_type_specifier (tag_type, decl,
10922 if (type == error_mark_node)
10923 return error_mark_node;
10926 /* Forward declarations of nested types, such as
10931 are invalid unless all components preceding the final '::'
10932 are complete. If all enclosing types are complete, these
10933 declarations become merely pointless.
10935 Invalid forward declarations of nested types are errors
10936 caught elsewhere in parsing. Those that are pointless arrive
10939 if (cp_parser_declares_only_class_p (parser)
10940 && !is_friend && !processing_explicit_instantiation)
10941 warning (0, "declaration %qD does not declare anything", decl);
10943 type = TREE_TYPE (decl);
10947 /* An elaborated-type-specifier sometimes introduces a new type and
10948 sometimes names an existing type. Normally, the rule is that it
10949 introduces a new type only if there is not an existing type of
10950 the same name already in scope. For example, given:
10953 void f() { struct S s; }
10955 the `struct S' in the body of `f' is the same `struct S' as in
10956 the global scope; the existing definition is used. However, if
10957 there were no global declaration, this would introduce a new
10958 local class named `S'.
10960 An exception to this rule applies to the following code:
10962 namespace N { struct S; }
10964 Here, the elaborated-type-specifier names a new type
10965 unconditionally; even if there is already an `S' in the
10966 containing scope this declaration names a new type.
10967 This exception only applies if the elaborated-type-specifier
10968 forms the complete declaration:
10972 A declaration consisting solely of `class-key identifier ;' is
10973 either a redeclaration of the name in the current scope or a
10974 forward declaration of the identifier as a class name. It
10975 introduces the name into the current scope.
10977 We are in this situation precisely when the next token is a `;'.
10979 An exception to the exception is that a `friend' declaration does
10980 *not* name a new type; i.e., given:
10982 struct S { friend struct T; };
10984 `T' is not a new type in the scope of `S'.
10986 Also, `new struct S' or `sizeof (struct S)' never results in the
10987 definition of a new type; a new type can only be declared in a
10988 declaration context. */
10994 /* Friends have special name lookup rules. */
10995 ts = ts_within_enclosing_non_class;
10996 else if (is_declaration
10997 && cp_lexer_next_token_is (parser->lexer,
10999 /* This is a `class-key identifier ;' */
11005 (parser->num_template_parameter_lists
11006 && (cp_parser_next_token_starts_class_definition_p (parser)
11007 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11008 /* An unqualified name was used to reference this type, so
11009 there were no qualifying templates. */
11010 if (!cp_parser_check_template_parameters (parser,
11011 /*num_templates=*/0))
11012 return error_mark_node;
11013 type = xref_tag (tag_type, identifier, ts, template_p);
11017 if (type == error_mark_node)
11018 return error_mark_node;
11020 /* Allow attributes on forward declarations of classes. */
11023 if (TREE_CODE (type) == TYPENAME_TYPE)
11024 warning (OPT_Wattributes,
11025 "attributes ignored on uninstantiated type");
11026 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11027 && ! processing_explicit_instantiation)
11028 warning (OPT_Wattributes,
11029 "attributes ignored on template instantiation");
11030 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11031 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11033 warning (OPT_Wattributes,
11034 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11037 if (tag_type != enum_type)
11038 cp_parser_check_class_key (tag_type, type);
11040 /* A "<" cannot follow an elaborated type specifier. If that
11041 happens, the user was probably trying to form a template-id. */
11042 cp_parser_check_for_invalid_template_id (parser, type);
11047 /* Parse an enum-specifier.
11050 enum identifier [opt] { enumerator-list [opt] }
11053 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11056 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11057 if the token stream isn't an enum-specifier after all. */
11060 cp_parser_enum_specifier (cp_parser* parser)
11066 /* Parse tentatively so that we can back up if we don't find a
11068 cp_parser_parse_tentatively (parser);
11070 /* Caller guarantees that the current token is 'enum', an identifier
11071 possibly follows, and the token after that is an opening brace.
11072 If we don't have an identifier, fabricate an anonymous name for
11073 the enumeration being defined. */
11074 cp_lexer_consume_token (parser->lexer);
11076 attributes = cp_parser_attributes_opt (parser);
11078 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11079 identifier = cp_parser_identifier (parser);
11081 identifier = make_anon_name ();
11083 /* Look for the `{' but don't consume it yet. */
11084 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11085 cp_parser_simulate_error (parser);
11087 if (!cp_parser_parse_definitely (parser))
11090 /* Issue an error message if type-definitions are forbidden here. */
11091 if (!cp_parser_check_type_definition (parser))
11092 type = error_mark_node;
11094 /* Create the new type. We do this before consuming the opening
11095 brace so the enum will be recorded as being on the line of its
11096 tag (or the 'enum' keyword, if there is no tag). */
11097 type = start_enum (identifier);
11099 /* Consume the opening brace. */
11100 cp_lexer_consume_token (parser->lexer);
11102 if (type == error_mark_node)
11104 cp_parser_skip_to_end_of_block_or_statement (parser);
11105 return error_mark_node;
11108 /* If the next token is not '}', then there are some enumerators. */
11109 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11110 cp_parser_enumerator_list (parser, type);
11112 /* Consume the final '}'. */
11113 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11115 /* Look for trailing attributes to apply to this enumeration, and
11116 apply them if appropriate. */
11117 if (cp_parser_allow_gnu_extensions_p (parser))
11119 tree trailing_attr = cp_parser_attributes_opt (parser);
11120 cplus_decl_attributes (&type,
11122 (int) ATTR_FLAG_TYPE_IN_PLACE);
11125 /* Finish up the enumeration. */
11126 finish_enum (type);
11131 /* Parse an enumerator-list. The enumerators all have the indicated
11135 enumerator-definition
11136 enumerator-list , enumerator-definition */
11139 cp_parser_enumerator_list (cp_parser* parser, tree type)
11143 /* Parse an enumerator-definition. */
11144 cp_parser_enumerator_definition (parser, type);
11146 /* If the next token is not a ',', we've reached the end of
11148 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11150 /* Otherwise, consume the `,' and keep going. */
11151 cp_lexer_consume_token (parser->lexer);
11152 /* If the next token is a `}', there is a trailing comma. */
11153 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11155 if (pedantic && !in_system_header)
11156 pedwarn ("comma at end of enumerator list");
11162 /* Parse an enumerator-definition. The enumerator has the indicated
11165 enumerator-definition:
11167 enumerator = constant-expression
11173 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11178 /* Look for the identifier. */
11179 identifier = cp_parser_identifier (parser);
11180 if (identifier == error_mark_node)
11183 /* If the next token is an '=', then there is an explicit value. */
11184 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11186 /* Consume the `=' token. */
11187 cp_lexer_consume_token (parser->lexer);
11188 /* Parse the value. */
11189 value = cp_parser_constant_expression (parser,
11190 /*allow_non_constant_p=*/false,
11196 /* Create the enumerator. */
11197 build_enumerator (identifier, value, type);
11200 /* Parse a namespace-name.
11203 original-namespace-name
11206 Returns the NAMESPACE_DECL for the namespace. */
11209 cp_parser_namespace_name (cp_parser* parser)
11212 tree namespace_decl;
11214 /* Get the name of the namespace. */
11215 identifier = cp_parser_identifier (parser);
11216 if (identifier == error_mark_node)
11217 return error_mark_node;
11219 /* Look up the identifier in the currently active scope. Look only
11220 for namespaces, due to:
11222 [basic.lookup.udir]
11224 When looking up a namespace-name in a using-directive or alias
11225 definition, only namespace names are considered.
11229 [basic.lookup.qual]
11231 During the lookup of a name preceding the :: scope resolution
11232 operator, object, function, and enumerator names are ignored.
11234 (Note that cp_parser_class_or_namespace_name only calls this
11235 function if the token after the name is the scope resolution
11237 namespace_decl = cp_parser_lookup_name (parser, identifier,
11239 /*is_template=*/false,
11240 /*is_namespace=*/true,
11241 /*check_dependency=*/true,
11242 /*ambiguous_decls=*/NULL);
11243 /* If it's not a namespace, issue an error. */
11244 if (namespace_decl == error_mark_node
11245 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11247 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11248 error ("%qD is not a namespace-name", identifier);
11249 cp_parser_error (parser, "expected namespace-name");
11250 namespace_decl = error_mark_node;
11253 return namespace_decl;
11256 /* Parse a namespace-definition.
11258 namespace-definition:
11259 named-namespace-definition
11260 unnamed-namespace-definition
11262 named-namespace-definition:
11263 original-namespace-definition
11264 extension-namespace-definition
11266 original-namespace-definition:
11267 namespace identifier { namespace-body }
11269 extension-namespace-definition:
11270 namespace original-namespace-name { namespace-body }
11272 unnamed-namespace-definition:
11273 namespace { namespace-body } */
11276 cp_parser_namespace_definition (cp_parser* parser)
11278 tree identifier, attribs;
11280 /* Look for the `namespace' keyword. */
11281 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11283 /* Get the name of the namespace. We do not attempt to distinguish
11284 between an original-namespace-definition and an
11285 extension-namespace-definition at this point. The semantic
11286 analysis routines are responsible for that. */
11287 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11288 identifier = cp_parser_identifier (parser);
11290 identifier = NULL_TREE;
11292 /* Parse any specified attributes. */
11293 attribs = cp_parser_attributes_opt (parser);
11295 /* Look for the `{' to start the namespace. */
11296 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11297 /* Start the namespace. */
11298 push_namespace_with_attribs (identifier, attribs);
11299 /* Parse the body of the namespace. */
11300 cp_parser_namespace_body (parser);
11301 /* Finish the namespace. */
11303 /* Look for the final `}'. */
11304 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11307 /* Parse a namespace-body.
11310 declaration-seq [opt] */
11313 cp_parser_namespace_body (cp_parser* parser)
11315 cp_parser_declaration_seq_opt (parser);
11318 /* Parse a namespace-alias-definition.
11320 namespace-alias-definition:
11321 namespace identifier = qualified-namespace-specifier ; */
11324 cp_parser_namespace_alias_definition (cp_parser* parser)
11327 tree namespace_specifier;
11329 /* Look for the `namespace' keyword. */
11330 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11331 /* Look for the identifier. */
11332 identifier = cp_parser_identifier (parser);
11333 if (identifier == error_mark_node)
11335 /* Look for the `=' token. */
11336 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11337 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11339 error ("%<namespace%> definition is not allowed here");
11340 /* Skip the definition. */
11341 cp_lexer_consume_token (parser->lexer);
11342 if (cp_parser_skip_to_closing_brace (parser))
11343 cp_lexer_consume_token (parser->lexer);
11346 cp_parser_require (parser, CPP_EQ, "`='");
11347 /* Look for the qualified-namespace-specifier. */
11348 namespace_specifier
11349 = cp_parser_qualified_namespace_specifier (parser);
11350 /* Look for the `;' token. */
11351 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11353 /* Register the alias in the symbol table. */
11354 do_namespace_alias (identifier, namespace_specifier);
11357 /* Parse a qualified-namespace-specifier.
11359 qualified-namespace-specifier:
11360 :: [opt] nested-name-specifier [opt] namespace-name
11362 Returns a NAMESPACE_DECL corresponding to the specified
11366 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11368 /* Look for the optional `::'. */
11369 cp_parser_global_scope_opt (parser,
11370 /*current_scope_valid_p=*/false);
11372 /* Look for the optional nested-name-specifier. */
11373 cp_parser_nested_name_specifier_opt (parser,
11374 /*typename_keyword_p=*/false,
11375 /*check_dependency_p=*/true,
11377 /*is_declaration=*/true);
11379 return cp_parser_namespace_name (parser);
11382 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11383 access declaration.
11386 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11387 using :: unqualified-id ;
11389 access-declaration:
11395 cp_parser_using_declaration (cp_parser* parser,
11396 bool access_declaration_p)
11399 bool typename_p = false;
11400 bool global_scope_p;
11405 if (access_declaration_p)
11406 cp_parser_parse_tentatively (parser);
11409 /* Look for the `using' keyword. */
11410 cp_parser_require_keyword (parser, RID_USING, "`using'");
11412 /* Peek at the next token. */
11413 token = cp_lexer_peek_token (parser->lexer);
11414 /* See if it's `typename'. */
11415 if (token->keyword == RID_TYPENAME)
11417 /* Remember that we've seen it. */
11419 /* Consume the `typename' token. */
11420 cp_lexer_consume_token (parser->lexer);
11424 /* Look for the optional global scope qualification. */
11426 = (cp_parser_global_scope_opt (parser,
11427 /*current_scope_valid_p=*/false)
11430 /* If we saw `typename', or didn't see `::', then there must be a
11431 nested-name-specifier present. */
11432 if (typename_p || !global_scope_p)
11433 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11434 /*check_dependency_p=*/true,
11436 /*is_declaration=*/true);
11437 /* Otherwise, we could be in either of the two productions. In that
11438 case, treat the nested-name-specifier as optional. */
11440 qscope = cp_parser_nested_name_specifier_opt (parser,
11441 /*typename_keyword_p=*/false,
11442 /*check_dependency_p=*/true,
11444 /*is_declaration=*/true);
11446 qscope = global_namespace;
11448 if (access_declaration_p && cp_parser_error_occurred (parser))
11449 /* Something has already gone wrong; there's no need to parse
11450 further. Since an error has occurred, the return value of
11451 cp_parser_parse_definitely will be false, as required. */
11452 return cp_parser_parse_definitely (parser);
11454 /* Parse the unqualified-id. */
11455 identifier = cp_parser_unqualified_id (parser,
11456 /*template_keyword_p=*/false,
11457 /*check_dependency_p=*/true,
11458 /*declarator_p=*/true,
11459 /*optional_p=*/false);
11461 if (access_declaration_p)
11463 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11464 cp_parser_simulate_error (parser);
11465 if (!cp_parser_parse_definitely (parser))
11469 /* The function we call to handle a using-declaration is different
11470 depending on what scope we are in. */
11471 if (qscope == error_mark_node || identifier == error_mark_node)
11473 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11474 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11475 /* [namespace.udecl]
11477 A using declaration shall not name a template-id. */
11478 error ("a template-id may not appear in a using-declaration");
11481 if (at_class_scope_p ())
11483 /* Create the USING_DECL. */
11484 decl = do_class_using_decl (parser->scope, identifier);
11485 /* Add it to the list of members in this class. */
11486 finish_member_declaration (decl);
11490 decl = cp_parser_lookup_name_simple (parser, identifier);
11491 if (decl == error_mark_node)
11492 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11493 else if (!at_namespace_scope_p ())
11494 do_local_using_decl (decl, qscope, identifier);
11496 do_toplevel_using_decl (decl, qscope, identifier);
11500 /* Look for the final `;'. */
11501 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11506 /* Parse a using-directive.
11509 using namespace :: [opt] nested-name-specifier [opt]
11510 namespace-name ; */
11513 cp_parser_using_directive (cp_parser* parser)
11515 tree namespace_decl;
11518 /* Look for the `using' keyword. */
11519 cp_parser_require_keyword (parser, RID_USING, "`using'");
11520 /* And the `namespace' keyword. */
11521 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11522 /* Look for the optional `::' operator. */
11523 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11524 /* And the optional nested-name-specifier. */
11525 cp_parser_nested_name_specifier_opt (parser,
11526 /*typename_keyword_p=*/false,
11527 /*check_dependency_p=*/true,
11529 /*is_declaration=*/true);
11530 /* Get the namespace being used. */
11531 namespace_decl = cp_parser_namespace_name (parser);
11532 /* And any specified attributes. */
11533 attribs = cp_parser_attributes_opt (parser);
11534 /* Update the symbol table. */
11535 parse_using_directive (namespace_decl, attribs);
11536 /* Look for the final `;'. */
11537 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11540 /* Parse an asm-definition.
11543 asm ( string-literal ) ;
11548 asm volatile [opt] ( string-literal ) ;
11549 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11550 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11551 : asm-operand-list [opt] ) ;
11552 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11553 : asm-operand-list [opt]
11554 : asm-operand-list [opt] ) ; */
11557 cp_parser_asm_definition (cp_parser* parser)
11560 tree outputs = NULL_TREE;
11561 tree inputs = NULL_TREE;
11562 tree clobbers = NULL_TREE;
11564 bool volatile_p = false;
11565 bool extended_p = false;
11567 /* Look for the `asm' keyword. */
11568 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11569 /* See if the next token is `volatile'. */
11570 if (cp_parser_allow_gnu_extensions_p (parser)
11571 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11573 /* Remember that we saw the `volatile' keyword. */
11575 /* Consume the token. */
11576 cp_lexer_consume_token (parser->lexer);
11578 /* Look for the opening `('. */
11579 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11581 /* Look for the string. */
11582 string = cp_parser_string_literal (parser, false, false);
11583 if (string == error_mark_node)
11585 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11586 /*consume_paren=*/true);
11590 /* If we're allowing GNU extensions, check for the extended assembly
11591 syntax. Unfortunately, the `:' tokens need not be separated by
11592 a space in C, and so, for compatibility, we tolerate that here
11593 too. Doing that means that we have to treat the `::' operator as
11595 if (cp_parser_allow_gnu_extensions_p (parser)
11596 && parser->in_function_body
11597 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11598 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11600 bool inputs_p = false;
11601 bool clobbers_p = false;
11603 /* The extended syntax was used. */
11606 /* Look for outputs. */
11607 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11609 /* Consume the `:'. */
11610 cp_lexer_consume_token (parser->lexer);
11611 /* Parse the output-operands. */
11612 if (cp_lexer_next_token_is_not (parser->lexer,
11614 && cp_lexer_next_token_is_not (parser->lexer,
11616 && cp_lexer_next_token_is_not (parser->lexer,
11618 outputs = cp_parser_asm_operand_list (parser);
11620 /* If the next token is `::', there are no outputs, and the
11621 next token is the beginning of the inputs. */
11622 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11623 /* The inputs are coming next. */
11626 /* Look for inputs. */
11628 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11630 /* Consume the `:' or `::'. */
11631 cp_lexer_consume_token (parser->lexer);
11632 /* Parse the output-operands. */
11633 if (cp_lexer_next_token_is_not (parser->lexer,
11635 && cp_lexer_next_token_is_not (parser->lexer,
11637 inputs = cp_parser_asm_operand_list (parser);
11639 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11640 /* The clobbers are coming next. */
11643 /* Look for clobbers. */
11645 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11647 /* Consume the `:' or `::'. */
11648 cp_lexer_consume_token (parser->lexer);
11649 /* Parse the clobbers. */
11650 if (cp_lexer_next_token_is_not (parser->lexer,
11652 clobbers = cp_parser_asm_clobber_list (parser);
11655 /* Look for the closing `)'. */
11656 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11657 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11658 /*consume_paren=*/true);
11659 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11661 /* Create the ASM_EXPR. */
11662 if (parser->in_function_body)
11664 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11666 /* If the extended syntax was not used, mark the ASM_EXPR. */
11669 tree temp = asm_stmt;
11670 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11671 temp = TREE_OPERAND (temp, 0);
11673 ASM_INPUT_P (temp) = 1;
11677 cgraph_add_asm_node (string);
11680 /* Declarators [gram.dcl.decl] */
11682 /* Parse an init-declarator.
11685 declarator initializer [opt]
11690 declarator asm-specification [opt] attributes [opt] initializer [opt]
11692 function-definition:
11693 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11695 decl-specifier-seq [opt] declarator function-try-block
11699 function-definition:
11700 __extension__ function-definition
11702 The DECL_SPECIFIERS apply to this declarator. Returns a
11703 representation of the entity declared. If MEMBER_P is TRUE, then
11704 this declarator appears in a class scope. The new DECL created by
11705 this declarator is returned.
11707 The CHECKS are access checks that should be performed once we know
11708 what entity is being declared (and, therefore, what classes have
11711 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11712 for a function-definition here as well. If the declarator is a
11713 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11714 be TRUE upon return. By that point, the function-definition will
11715 have been completely parsed.
11717 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11721 cp_parser_init_declarator (cp_parser* parser,
11722 cp_decl_specifier_seq *decl_specifiers,
11723 VEC (deferred_access_check,gc)* checks,
11724 bool function_definition_allowed_p,
11726 int declares_class_or_enum,
11727 bool* function_definition_p)
11730 cp_declarator *declarator;
11731 tree prefix_attributes;
11733 tree asm_specification;
11735 tree decl = NULL_TREE;
11737 bool is_initialized;
11738 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11739 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11741 enum cpp_ttype initialization_kind;
11742 bool is_parenthesized_init = false;
11743 bool is_non_constant_init;
11744 int ctor_dtor_or_conv_p;
11746 tree pushed_scope = NULL;
11748 /* Gather the attributes that were provided with the
11749 decl-specifiers. */
11750 prefix_attributes = decl_specifiers->attributes;
11752 /* Assume that this is not the declarator for a function
11754 if (function_definition_p)
11755 *function_definition_p = false;
11757 /* Defer access checks while parsing the declarator; we cannot know
11758 what names are accessible until we know what is being
11760 resume_deferring_access_checks ();
11762 /* Parse the declarator. */
11764 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11765 &ctor_dtor_or_conv_p,
11766 /*parenthesized_p=*/NULL,
11767 /*member_p=*/false);
11768 /* Gather up the deferred checks. */
11769 stop_deferring_access_checks ();
11771 /* If the DECLARATOR was erroneous, there's no need to go
11773 if (declarator == cp_error_declarator)
11774 return error_mark_node;
11776 /* Check that the number of template-parameter-lists is OK. */
11777 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11778 return error_mark_node;
11780 if (declares_class_or_enum & 2)
11781 cp_parser_check_for_definition_in_return_type (declarator,
11782 decl_specifiers->type);
11784 /* Figure out what scope the entity declared by the DECLARATOR is
11785 located in. `grokdeclarator' sometimes changes the scope, so
11786 we compute it now. */
11787 scope = get_scope_of_declarator (declarator);
11789 /* If we're allowing GNU extensions, look for an asm-specification
11791 if (cp_parser_allow_gnu_extensions_p (parser))
11793 /* Look for an asm-specification. */
11794 asm_specification = cp_parser_asm_specification_opt (parser);
11795 /* And attributes. */
11796 attributes = cp_parser_attributes_opt (parser);
11800 asm_specification = NULL_TREE;
11801 attributes = NULL_TREE;
11804 /* Peek at the next token. */
11805 token = cp_lexer_peek_token (parser->lexer);
11806 /* Check to see if the token indicates the start of a
11807 function-definition. */
11808 if (cp_parser_token_starts_function_definition_p (token))
11810 if (!function_definition_allowed_p)
11812 /* If a function-definition should not appear here, issue an
11814 cp_parser_error (parser,
11815 "a function-definition is not allowed here");
11816 return error_mark_node;
11820 /* Neither attributes nor an asm-specification are allowed
11821 on a function-definition. */
11822 if (asm_specification)
11823 error ("an asm-specification is not allowed on a function-definition");
11825 error ("attributes are not allowed on a function-definition");
11826 /* This is a function-definition. */
11827 *function_definition_p = true;
11829 /* Parse the function definition. */
11831 decl = cp_parser_save_member_function_body (parser,
11834 prefix_attributes);
11837 = (cp_parser_function_definition_from_specifiers_and_declarator
11838 (parser, decl_specifiers, prefix_attributes, declarator));
11846 Only in function declarations for constructors, destructors, and
11847 type conversions can the decl-specifier-seq be omitted.
11849 We explicitly postpone this check past the point where we handle
11850 function-definitions because we tolerate function-definitions
11851 that are missing their return types in some modes. */
11852 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11854 cp_parser_error (parser,
11855 "expected constructor, destructor, or type conversion");
11856 return error_mark_node;
11859 /* An `=' or an `(' indicates an initializer. */
11860 if (token->type == CPP_EQ
11861 || token->type == CPP_OPEN_PAREN)
11863 is_initialized = true;
11864 initialization_kind = token->type;
11868 /* If the init-declarator isn't initialized and isn't followed by a
11869 `,' or `;', it's not a valid init-declarator. */
11870 if (token->type != CPP_COMMA
11871 && token->type != CPP_SEMICOLON)
11873 cp_parser_error (parser, "expected initializer");
11874 return error_mark_node;
11876 is_initialized = false;
11877 initialization_kind = CPP_EOF;
11880 /* Because start_decl has side-effects, we should only call it if we
11881 know we're going ahead. By this point, we know that we cannot
11882 possibly be looking at any other construct. */
11883 cp_parser_commit_to_tentative_parse (parser);
11885 /* If the decl specifiers were bad, issue an error now that we're
11886 sure this was intended to be a declarator. Then continue
11887 declaring the variable(s), as int, to try to cut down on further
11889 if (decl_specifiers->any_specifiers_p
11890 && decl_specifiers->type == error_mark_node)
11892 cp_parser_error (parser, "invalid type in declaration");
11893 decl_specifiers->type = integer_type_node;
11896 /* Check to see whether or not this declaration is a friend. */
11897 friend_p = cp_parser_friend_p (decl_specifiers);
11899 /* Enter the newly declared entry in the symbol table. If we're
11900 processing a declaration in a class-specifier, we wait until
11901 after processing the initializer. */
11904 if (parser->in_unbraced_linkage_specification_p)
11905 decl_specifiers->storage_class = sc_extern;
11906 decl = start_decl (declarator, decl_specifiers,
11907 is_initialized, attributes, prefix_attributes,
11911 /* Enter the SCOPE. That way unqualified names appearing in the
11912 initializer will be looked up in SCOPE. */
11913 pushed_scope = push_scope (scope);
11915 /* Perform deferred access control checks, now that we know in which
11916 SCOPE the declared entity resides. */
11917 if (!member_p && decl)
11919 tree saved_current_function_decl = NULL_TREE;
11921 /* If the entity being declared is a function, pretend that we
11922 are in its scope. If it is a `friend', it may have access to
11923 things that would not otherwise be accessible. */
11924 if (TREE_CODE (decl) == FUNCTION_DECL)
11926 saved_current_function_decl = current_function_decl;
11927 current_function_decl = decl;
11930 /* Perform access checks for template parameters. */
11931 cp_parser_perform_template_parameter_access_checks (checks);
11933 /* Perform the access control checks for the declarator and the
11934 the decl-specifiers. */
11935 perform_deferred_access_checks ();
11937 /* Restore the saved value. */
11938 if (TREE_CODE (decl) == FUNCTION_DECL)
11939 current_function_decl = saved_current_function_decl;
11942 /* Parse the initializer. */
11943 initializer = NULL_TREE;
11944 is_parenthesized_init = false;
11945 is_non_constant_init = true;
11946 if (is_initialized)
11948 if (function_declarator_p (declarator))
11950 if (initialization_kind == CPP_EQ)
11951 initializer = cp_parser_pure_specifier (parser);
11954 /* If the declaration was erroneous, we don't really
11955 know what the user intended, so just silently
11956 consume the initializer. */
11957 if (decl != error_mark_node)
11958 error ("initializer provided for function");
11959 cp_parser_skip_to_closing_parenthesis (parser,
11960 /*recovering=*/true,
11961 /*or_comma=*/false,
11962 /*consume_paren=*/true);
11966 initializer = cp_parser_initializer (parser,
11967 &is_parenthesized_init,
11968 &is_non_constant_init);
11971 /* The old parser allows attributes to appear after a parenthesized
11972 initializer. Mark Mitchell proposed removing this functionality
11973 on the GCC mailing lists on 2002-08-13. This parser accepts the
11974 attributes -- but ignores them. */
11975 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11976 if (cp_parser_attributes_opt (parser))
11977 warning (OPT_Wattributes,
11978 "attributes after parenthesized initializer ignored");
11980 /* For an in-class declaration, use `grokfield' to create the
11986 pop_scope (pushed_scope);
11987 pushed_scope = false;
11989 decl = grokfield (declarator, decl_specifiers,
11990 initializer, !is_non_constant_init,
11991 /*asmspec=*/NULL_TREE,
11992 prefix_attributes);
11993 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11994 cp_parser_save_default_args (parser, decl);
11997 /* Finish processing the declaration. But, skip friend
11999 if (!friend_p && decl && decl != error_mark_node)
12001 cp_finish_decl (decl,
12002 initializer, !is_non_constant_init,
12004 /* If the initializer is in parentheses, then this is
12005 a direct-initialization, which means that an
12006 `explicit' constructor is OK. Otherwise, an
12007 `explicit' constructor cannot be used. */
12008 ((is_parenthesized_init || !is_initialized)
12009 ? 0 : LOOKUP_ONLYCONVERTING));
12011 else if ((cxx_dialect != cxx98) && friend_p
12012 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12013 /* Core issue #226 (C++0x only): A default template-argument
12014 shall not be specified in a friend class template
12016 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12017 /*is_partial=*/0, /*is_friend_decl=*/1);
12019 if (!friend_p && pushed_scope)
12020 pop_scope (pushed_scope);
12025 /* Parse a declarator.
12029 ptr-operator declarator
12031 abstract-declarator:
12032 ptr-operator abstract-declarator [opt]
12033 direct-abstract-declarator
12038 attributes [opt] direct-declarator
12039 attributes [opt] ptr-operator declarator
12041 abstract-declarator:
12042 attributes [opt] ptr-operator abstract-declarator [opt]
12043 attributes [opt] direct-abstract-declarator
12045 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12046 detect constructor, destructor or conversion operators. It is set
12047 to -1 if the declarator is a name, and +1 if it is a
12048 function. Otherwise it is set to zero. Usually you just want to
12049 test for >0, but internally the negative value is used.
12051 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12052 a decl-specifier-seq unless it declares a constructor, destructor,
12053 or conversion. It might seem that we could check this condition in
12054 semantic analysis, rather than parsing, but that makes it difficult
12055 to handle something like `f()'. We want to notice that there are
12056 no decl-specifiers, and therefore realize that this is an
12057 expression, not a declaration.)
12059 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12060 the declarator is a direct-declarator of the form "(...)".
12062 MEMBER_P is true iff this declarator is a member-declarator. */
12064 static cp_declarator *
12065 cp_parser_declarator (cp_parser* parser,
12066 cp_parser_declarator_kind dcl_kind,
12067 int* ctor_dtor_or_conv_p,
12068 bool* parenthesized_p,
12072 cp_declarator *declarator;
12073 enum tree_code code;
12074 cp_cv_quals cv_quals;
12076 tree attributes = NULL_TREE;
12078 /* Assume this is not a constructor, destructor, or type-conversion
12080 if (ctor_dtor_or_conv_p)
12081 *ctor_dtor_or_conv_p = 0;
12083 if (cp_parser_allow_gnu_extensions_p (parser))
12084 attributes = cp_parser_attributes_opt (parser);
12086 /* Peek at the next token. */
12087 token = cp_lexer_peek_token (parser->lexer);
12089 /* Check for the ptr-operator production. */
12090 cp_parser_parse_tentatively (parser);
12091 /* Parse the ptr-operator. */
12092 code = cp_parser_ptr_operator (parser,
12095 /* If that worked, then we have a ptr-operator. */
12096 if (cp_parser_parse_definitely (parser))
12098 /* If a ptr-operator was found, then this declarator was not
12100 if (parenthesized_p)
12101 *parenthesized_p = true;
12102 /* The dependent declarator is optional if we are parsing an
12103 abstract-declarator. */
12104 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12105 cp_parser_parse_tentatively (parser);
12107 /* Parse the dependent declarator. */
12108 declarator = cp_parser_declarator (parser, dcl_kind,
12109 /*ctor_dtor_or_conv_p=*/NULL,
12110 /*parenthesized_p=*/NULL,
12111 /*member_p=*/false);
12113 /* If we are parsing an abstract-declarator, we must handle the
12114 case where the dependent declarator is absent. */
12115 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12116 && !cp_parser_parse_definitely (parser))
12119 declarator = cp_parser_make_indirect_declarator
12120 (code, class_type, cv_quals, declarator);
12122 /* Everything else is a direct-declarator. */
12125 if (parenthesized_p)
12126 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12128 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12129 ctor_dtor_or_conv_p,
12133 if (attributes && declarator && declarator != cp_error_declarator)
12134 declarator->attributes = attributes;
12139 /* Parse a direct-declarator or direct-abstract-declarator.
12143 direct-declarator ( parameter-declaration-clause )
12144 cv-qualifier-seq [opt]
12145 exception-specification [opt]
12146 direct-declarator [ constant-expression [opt] ]
12149 direct-abstract-declarator:
12150 direct-abstract-declarator [opt]
12151 ( parameter-declaration-clause )
12152 cv-qualifier-seq [opt]
12153 exception-specification [opt]
12154 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12155 ( abstract-declarator )
12157 Returns a representation of the declarator. DCL_KIND is
12158 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12159 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12160 we are parsing a direct-declarator. It is
12161 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12162 of ambiguity we prefer an abstract declarator, as per
12163 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12164 cp_parser_declarator. */
12166 static cp_declarator *
12167 cp_parser_direct_declarator (cp_parser* parser,
12168 cp_parser_declarator_kind dcl_kind,
12169 int* ctor_dtor_or_conv_p,
12173 cp_declarator *declarator = NULL;
12174 tree scope = NULL_TREE;
12175 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12176 bool saved_in_declarator_p = parser->in_declarator_p;
12178 tree pushed_scope = NULL_TREE;
12182 /* Peek at the next token. */
12183 token = cp_lexer_peek_token (parser->lexer);
12184 if (token->type == CPP_OPEN_PAREN)
12186 /* This is either a parameter-declaration-clause, or a
12187 parenthesized declarator. When we know we are parsing a
12188 named declarator, it must be a parenthesized declarator
12189 if FIRST is true. For instance, `(int)' is a
12190 parameter-declaration-clause, with an omitted
12191 direct-abstract-declarator. But `((*))', is a
12192 parenthesized abstract declarator. Finally, when T is a
12193 template parameter `(T)' is a
12194 parameter-declaration-clause, and not a parenthesized
12197 We first try and parse a parameter-declaration-clause,
12198 and then try a nested declarator (if FIRST is true).
12200 It is not an error for it not to be a
12201 parameter-declaration-clause, even when FIRST is
12207 The first is the declaration of a function while the
12208 second is a the definition of a variable, including its
12211 Having seen only the parenthesis, we cannot know which of
12212 these two alternatives should be selected. Even more
12213 complex are examples like:
12218 The former is a function-declaration; the latter is a
12219 variable initialization.
12221 Thus again, we try a parameter-declaration-clause, and if
12222 that fails, we back out and return. */
12224 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12226 cp_parameter_declarator *params;
12227 unsigned saved_num_template_parameter_lists;
12229 /* In a member-declarator, the only valid interpretation
12230 of a parenthesis is the start of a
12231 parameter-declaration-clause. (It is invalid to
12232 initialize a static data member with a parenthesized
12233 initializer; only the "=" form of initialization is
12236 cp_parser_parse_tentatively (parser);
12238 /* Consume the `('. */
12239 cp_lexer_consume_token (parser->lexer);
12242 /* If this is going to be an abstract declarator, we're
12243 in a declarator and we can't have default args. */
12244 parser->default_arg_ok_p = false;
12245 parser->in_declarator_p = true;
12248 /* Inside the function parameter list, surrounding
12249 template-parameter-lists do not apply. */
12250 saved_num_template_parameter_lists
12251 = parser->num_template_parameter_lists;
12252 parser->num_template_parameter_lists = 0;
12254 /* Parse the parameter-declaration-clause. */
12255 params = cp_parser_parameter_declaration_clause (parser);
12257 parser->num_template_parameter_lists
12258 = saved_num_template_parameter_lists;
12260 /* If all went well, parse the cv-qualifier-seq and the
12261 exception-specification. */
12262 if (member_p || cp_parser_parse_definitely (parser))
12264 cp_cv_quals cv_quals;
12265 tree exception_specification;
12267 if (ctor_dtor_or_conv_p)
12268 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12270 /* Consume the `)'. */
12271 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12273 /* Parse the cv-qualifier-seq. */
12274 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12275 /* And the exception-specification. */
12276 exception_specification
12277 = cp_parser_exception_specification_opt (parser);
12279 /* Create the function-declarator. */
12280 declarator = make_call_declarator (declarator,
12283 exception_specification);
12284 /* Any subsequent parameter lists are to do with
12285 return type, so are not those of the declared
12287 parser->default_arg_ok_p = false;
12289 /* Repeat the main loop. */
12294 /* If this is the first, we can try a parenthesized
12298 bool saved_in_type_id_in_expr_p;
12300 parser->default_arg_ok_p = saved_default_arg_ok_p;
12301 parser->in_declarator_p = saved_in_declarator_p;
12303 /* Consume the `('. */
12304 cp_lexer_consume_token (parser->lexer);
12305 /* Parse the nested declarator. */
12306 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12307 parser->in_type_id_in_expr_p = true;
12309 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12310 /*parenthesized_p=*/NULL,
12312 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12314 /* Expect a `)'. */
12315 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12316 declarator = cp_error_declarator;
12317 if (declarator == cp_error_declarator)
12320 goto handle_declarator;
12322 /* Otherwise, we must be done. */
12326 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12327 && token->type == CPP_OPEN_SQUARE)
12329 /* Parse an array-declarator. */
12332 if (ctor_dtor_or_conv_p)
12333 *ctor_dtor_or_conv_p = 0;
12336 parser->default_arg_ok_p = false;
12337 parser->in_declarator_p = true;
12338 /* Consume the `['. */
12339 cp_lexer_consume_token (parser->lexer);
12340 /* Peek at the next token. */
12341 token = cp_lexer_peek_token (parser->lexer);
12342 /* If the next token is `]', then there is no
12343 constant-expression. */
12344 if (token->type != CPP_CLOSE_SQUARE)
12346 bool non_constant_p;
12349 = cp_parser_constant_expression (parser,
12350 /*allow_non_constant=*/true,
12352 if (!non_constant_p)
12353 bounds = fold_non_dependent_expr (bounds);
12354 /* Normally, the array bound must be an integral constant
12355 expression. However, as an extension, we allow VLAs
12356 in function scopes. */
12357 else if (!parser->in_function_body)
12359 error ("array bound is not an integer constant");
12360 bounds = error_mark_node;
12364 bounds = NULL_TREE;
12365 /* Look for the closing `]'. */
12366 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12368 declarator = cp_error_declarator;
12372 declarator = make_array_declarator (declarator, bounds);
12374 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12376 tree qualifying_scope;
12377 tree unqualified_name;
12378 special_function_kind sfk;
12380 bool pack_expansion_p = false;
12382 /* Parse a declarator-id */
12383 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12386 cp_parser_parse_tentatively (parser);
12388 /* If we see an ellipsis, we should be looking at a
12390 if (token->type == CPP_ELLIPSIS)
12392 /* Consume the `...' */
12393 cp_lexer_consume_token (parser->lexer);
12395 pack_expansion_p = true;
12400 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12401 qualifying_scope = parser->scope;
12406 if (!unqualified_name && pack_expansion_p)
12408 /* Check whether an error occurred. */
12409 okay = !cp_parser_error_occurred (parser);
12411 /* We already consumed the ellipsis to mark a
12412 parameter pack, but we have no way to report it,
12413 so abort the tentative parse. We will be exiting
12414 immediately anyway. */
12415 cp_parser_abort_tentative_parse (parser);
12418 okay = cp_parser_parse_definitely (parser);
12421 unqualified_name = error_mark_node;
12422 else if (unqualified_name
12423 && (qualifying_scope
12424 || (TREE_CODE (unqualified_name)
12425 != IDENTIFIER_NODE)))
12427 cp_parser_error (parser, "expected unqualified-id");
12428 unqualified_name = error_mark_node;
12432 if (!unqualified_name)
12434 if (unqualified_name == error_mark_node)
12436 declarator = cp_error_declarator;
12437 pack_expansion_p = false;
12438 declarator->parameter_pack_p = false;
12442 if (qualifying_scope && at_namespace_scope_p ()
12443 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12445 /* In the declaration of a member of a template class
12446 outside of the class itself, the SCOPE will sometimes
12447 be a TYPENAME_TYPE. For example, given:
12449 template <typename T>
12450 int S<T>::R::i = 3;
12452 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12453 this context, we must resolve S<T>::R to an ordinary
12454 type, rather than a typename type.
12456 The reason we normally avoid resolving TYPENAME_TYPEs
12457 is that a specialization of `S' might render
12458 `S<T>::R' not a type. However, if `S' is
12459 specialized, then this `i' will not be used, so there
12460 is no harm in resolving the types here. */
12463 /* Resolve the TYPENAME_TYPE. */
12464 type = resolve_typename_type (qualifying_scope,
12465 /*only_current_p=*/false);
12466 /* If that failed, the declarator is invalid. */
12467 if (TREE_CODE (type) == TYPENAME_TYPE)
12468 error ("%<%T::%E%> is not a type",
12469 TYPE_CONTEXT (qualifying_scope),
12470 TYPE_IDENTIFIER (qualifying_scope));
12471 qualifying_scope = type;
12476 if (unqualified_name)
12480 if (qualifying_scope
12481 && CLASS_TYPE_P (qualifying_scope))
12482 class_type = qualifying_scope;
12484 class_type = current_class_type;
12486 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12488 tree name_type = TREE_TYPE (unqualified_name);
12489 if (class_type && same_type_p (name_type, class_type))
12491 if (qualifying_scope
12492 && CLASSTYPE_USE_TEMPLATE (name_type))
12494 error ("invalid use of constructor as a template");
12495 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12496 "name the constructor in a qualified name",
12498 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12499 class_type, name_type);
12500 declarator = cp_error_declarator;
12504 unqualified_name = constructor_name (class_type);
12508 /* We do not attempt to print the declarator
12509 here because we do not have enough
12510 information about its original syntactic
12512 cp_parser_error (parser, "invalid declarator");
12513 declarator = cp_error_declarator;
12520 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12521 sfk = sfk_destructor;
12522 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12523 sfk = sfk_conversion;
12524 else if (/* There's no way to declare a constructor
12525 for an anonymous type, even if the type
12526 got a name for linkage purposes. */
12527 !TYPE_WAS_ANONYMOUS (class_type)
12528 && constructor_name_p (unqualified_name,
12531 unqualified_name = constructor_name (class_type);
12532 sfk = sfk_constructor;
12535 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12536 *ctor_dtor_or_conv_p = -1;
12539 declarator = make_id_declarator (qualifying_scope,
12542 declarator->id_loc = token->location;
12543 declarator->parameter_pack_p = pack_expansion_p;
12545 if (pack_expansion_p)
12546 maybe_warn_variadic_templates ();
12548 handle_declarator:;
12549 scope = get_scope_of_declarator (declarator);
12551 /* Any names that appear after the declarator-id for a
12552 member are looked up in the containing scope. */
12553 pushed_scope = push_scope (scope);
12554 parser->in_declarator_p = true;
12555 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12556 || (declarator && declarator->kind == cdk_id))
12557 /* Default args are only allowed on function
12559 parser->default_arg_ok_p = saved_default_arg_ok_p;
12561 parser->default_arg_ok_p = false;
12570 /* For an abstract declarator, we might wind up with nothing at this
12571 point. That's an error; the declarator is not optional. */
12573 cp_parser_error (parser, "expected declarator");
12575 /* If we entered a scope, we must exit it now. */
12577 pop_scope (pushed_scope);
12579 parser->default_arg_ok_p = saved_default_arg_ok_p;
12580 parser->in_declarator_p = saved_in_declarator_p;
12585 /* Parse a ptr-operator.
12588 * cv-qualifier-seq [opt]
12590 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12595 & cv-qualifier-seq [opt]
12597 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12598 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12599 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12600 filled in with the TYPE containing the member. *CV_QUALS is
12601 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12602 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12603 Note that the tree codes returned by this function have nothing
12604 to do with the types of trees that will be eventually be created
12605 to represent the pointer or reference type being parsed. They are
12606 just constants with suggestive names. */
12607 static enum tree_code
12608 cp_parser_ptr_operator (cp_parser* parser,
12610 cp_cv_quals *cv_quals)
12612 enum tree_code code = ERROR_MARK;
12615 /* Assume that it's not a pointer-to-member. */
12617 /* And that there are no cv-qualifiers. */
12618 *cv_quals = TYPE_UNQUALIFIED;
12620 /* Peek at the next token. */
12621 token = cp_lexer_peek_token (parser->lexer);
12623 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12624 if (token->type == CPP_MULT)
12625 code = INDIRECT_REF;
12626 else if (token->type == CPP_AND)
12628 else if ((cxx_dialect != cxx98) &&
12629 token->type == CPP_AND_AND) /* C++0x only */
12630 code = NON_LVALUE_EXPR;
12632 if (code != ERROR_MARK)
12634 /* Consume the `*', `&' or `&&'. */
12635 cp_lexer_consume_token (parser->lexer);
12637 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12638 `&', if we are allowing GNU extensions. (The only qualifier
12639 that can legally appear after `&' is `restrict', but that is
12640 enforced during semantic analysis. */
12641 if (code == INDIRECT_REF
12642 || cp_parser_allow_gnu_extensions_p (parser))
12643 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12647 /* Try the pointer-to-member case. */
12648 cp_parser_parse_tentatively (parser);
12649 /* Look for the optional `::' operator. */
12650 cp_parser_global_scope_opt (parser,
12651 /*current_scope_valid_p=*/false);
12652 /* Look for the nested-name specifier. */
12653 cp_parser_nested_name_specifier (parser,
12654 /*typename_keyword_p=*/false,
12655 /*check_dependency_p=*/true,
12657 /*is_declaration=*/false);
12658 /* If we found it, and the next token is a `*', then we are
12659 indeed looking at a pointer-to-member operator. */
12660 if (!cp_parser_error_occurred (parser)
12661 && cp_parser_require (parser, CPP_MULT, "`*'"))
12663 /* Indicate that the `*' operator was used. */
12664 code = INDIRECT_REF;
12666 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12667 error ("%qD is a namespace", parser->scope);
12670 /* The type of which the member is a member is given by the
12672 *type = parser->scope;
12673 /* The next name will not be qualified. */
12674 parser->scope = NULL_TREE;
12675 parser->qualifying_scope = NULL_TREE;
12676 parser->object_scope = NULL_TREE;
12677 /* Look for the optional cv-qualifier-seq. */
12678 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12681 /* If that didn't work we don't have a ptr-operator. */
12682 if (!cp_parser_parse_definitely (parser))
12683 cp_parser_error (parser, "expected ptr-operator");
12689 /* Parse an (optional) cv-qualifier-seq.
12692 cv-qualifier cv-qualifier-seq [opt]
12703 Returns a bitmask representing the cv-qualifiers. */
12706 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12708 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12713 cp_cv_quals cv_qualifier;
12715 /* Peek at the next token. */
12716 token = cp_lexer_peek_token (parser->lexer);
12717 /* See if it's a cv-qualifier. */
12718 switch (token->keyword)
12721 cv_qualifier = TYPE_QUAL_CONST;
12725 cv_qualifier = TYPE_QUAL_VOLATILE;
12729 cv_qualifier = TYPE_QUAL_RESTRICT;
12733 cv_qualifier = TYPE_UNQUALIFIED;
12740 if (cv_quals & cv_qualifier)
12742 error ("duplicate cv-qualifier");
12743 cp_lexer_purge_token (parser->lexer);
12747 cp_lexer_consume_token (parser->lexer);
12748 cv_quals |= cv_qualifier;
12755 /* Parse a declarator-id.
12759 :: [opt] nested-name-specifier [opt] type-name
12761 In the `id-expression' case, the value returned is as for
12762 cp_parser_id_expression if the id-expression was an unqualified-id.
12763 If the id-expression was a qualified-id, then a SCOPE_REF is
12764 returned. The first operand is the scope (either a NAMESPACE_DECL
12765 or TREE_TYPE), but the second is still just a representation of an
12769 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12772 /* The expression must be an id-expression. Assume that qualified
12773 names are the names of types so that:
12776 int S<T>::R::i = 3;
12778 will work; we must treat `S<T>::R' as the name of a type.
12779 Similarly, assume that qualified names are templates, where
12783 int S<T>::R<T>::i = 3;
12786 id = cp_parser_id_expression (parser,
12787 /*template_keyword_p=*/false,
12788 /*check_dependency_p=*/false,
12789 /*template_p=*/NULL,
12790 /*declarator_p=*/true,
12792 if (id && BASELINK_P (id))
12793 id = BASELINK_FUNCTIONS (id);
12797 /* Parse a type-id.
12800 type-specifier-seq abstract-declarator [opt]
12802 Returns the TYPE specified. */
12805 cp_parser_type_id (cp_parser* parser)
12807 cp_decl_specifier_seq type_specifier_seq;
12808 cp_declarator *abstract_declarator;
12810 /* Parse the type-specifier-seq. */
12811 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12812 &type_specifier_seq);
12813 if (type_specifier_seq.type == error_mark_node)
12814 return error_mark_node;
12816 /* There might or might not be an abstract declarator. */
12817 cp_parser_parse_tentatively (parser);
12818 /* Look for the declarator. */
12819 abstract_declarator
12820 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12821 /*parenthesized_p=*/NULL,
12822 /*member_p=*/false);
12823 /* Check to see if there really was a declarator. */
12824 if (!cp_parser_parse_definitely (parser))
12825 abstract_declarator = NULL;
12827 return groktypename (&type_specifier_seq, abstract_declarator);
12830 /* Parse a type-specifier-seq.
12832 type-specifier-seq:
12833 type-specifier type-specifier-seq [opt]
12837 type-specifier-seq:
12838 attributes type-specifier-seq [opt]
12840 If IS_CONDITION is true, we are at the start of a "condition",
12841 e.g., we've just seen "if (".
12843 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12846 cp_parser_type_specifier_seq (cp_parser* parser,
12848 cp_decl_specifier_seq *type_specifier_seq)
12850 bool seen_type_specifier = false;
12851 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12853 /* Clear the TYPE_SPECIFIER_SEQ. */
12854 clear_decl_specs (type_specifier_seq);
12856 /* Parse the type-specifiers and attributes. */
12859 tree type_specifier;
12860 bool is_cv_qualifier;
12862 /* Check for attributes first. */
12863 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12865 type_specifier_seq->attributes =
12866 chainon (type_specifier_seq->attributes,
12867 cp_parser_attributes_opt (parser));
12871 /* Look for the type-specifier. */
12872 type_specifier = cp_parser_type_specifier (parser,
12874 type_specifier_seq,
12875 /*is_declaration=*/false,
12878 if (!type_specifier)
12880 /* If the first type-specifier could not be found, this is not a
12881 type-specifier-seq at all. */
12882 if (!seen_type_specifier)
12884 cp_parser_error (parser, "expected type-specifier");
12885 type_specifier_seq->type = error_mark_node;
12888 /* If subsequent type-specifiers could not be found, the
12889 type-specifier-seq is complete. */
12893 seen_type_specifier = true;
12894 /* The standard says that a condition can be:
12896 type-specifier-seq declarator = assignment-expression
12903 we should treat the "S" as a declarator, not as a
12904 type-specifier. The standard doesn't say that explicitly for
12905 type-specifier-seq, but it does say that for
12906 decl-specifier-seq in an ordinary declaration. Perhaps it
12907 would be clearer just to allow a decl-specifier-seq here, and
12908 then add a semantic restriction that if any decl-specifiers
12909 that are not type-specifiers appear, the program is invalid. */
12910 if (is_condition && !is_cv_qualifier)
12911 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12914 cp_parser_check_decl_spec (type_specifier_seq);
12917 /* Parse a parameter-declaration-clause.
12919 parameter-declaration-clause:
12920 parameter-declaration-list [opt] ... [opt]
12921 parameter-declaration-list , ...
12923 Returns a representation for the parameter declarations. A return
12924 value of NULL indicates a parameter-declaration-clause consisting
12925 only of an ellipsis. */
12927 static cp_parameter_declarator *
12928 cp_parser_parameter_declaration_clause (cp_parser* parser)
12930 cp_parameter_declarator *parameters;
12935 /* Peek at the next token. */
12936 token = cp_lexer_peek_token (parser->lexer);
12937 /* Check for trivial parameter-declaration-clauses. */
12938 if (token->type == CPP_ELLIPSIS)
12940 /* Consume the `...' token. */
12941 cp_lexer_consume_token (parser->lexer);
12944 else if (token->type == CPP_CLOSE_PAREN)
12945 /* There are no parameters. */
12947 #ifndef NO_IMPLICIT_EXTERN_C
12948 if (in_system_header && current_class_type == NULL
12949 && current_lang_name == lang_name_c)
12953 return no_parameters;
12955 /* Check for `(void)', too, which is a special case. */
12956 else if (token->keyword == RID_VOID
12957 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12958 == CPP_CLOSE_PAREN))
12960 /* Consume the `void' token. */
12961 cp_lexer_consume_token (parser->lexer);
12962 /* There are no parameters. */
12963 return no_parameters;
12966 /* Parse the parameter-declaration-list. */
12967 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12968 /* If a parse error occurred while parsing the
12969 parameter-declaration-list, then the entire
12970 parameter-declaration-clause is erroneous. */
12974 /* Peek at the next token. */
12975 token = cp_lexer_peek_token (parser->lexer);
12976 /* If it's a `,', the clause should terminate with an ellipsis. */
12977 if (token->type == CPP_COMMA)
12979 /* Consume the `,'. */
12980 cp_lexer_consume_token (parser->lexer);
12981 /* Expect an ellipsis. */
12983 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12985 /* It might also be `...' if the optional trailing `,' was
12987 else if (token->type == CPP_ELLIPSIS)
12989 /* Consume the `...' token. */
12990 cp_lexer_consume_token (parser->lexer);
12991 /* And remember that we saw it. */
12995 ellipsis_p = false;
12997 /* Finish the parameter list. */
12998 if (parameters && ellipsis_p)
12999 parameters->ellipsis_p = true;
13004 /* Parse a parameter-declaration-list.
13006 parameter-declaration-list:
13007 parameter-declaration
13008 parameter-declaration-list , parameter-declaration
13010 Returns a representation of the parameter-declaration-list, as for
13011 cp_parser_parameter_declaration_clause. However, the
13012 `void_list_node' is never appended to the list. Upon return,
13013 *IS_ERROR will be true iff an error occurred. */
13015 static cp_parameter_declarator *
13016 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13018 cp_parameter_declarator *parameters = NULL;
13019 cp_parameter_declarator **tail = ¶meters;
13020 bool saved_in_unbraced_linkage_specification_p;
13022 /* Assume all will go well. */
13024 /* The special considerations that apply to a function within an
13025 unbraced linkage specifications do not apply to the parameters
13026 to the function. */
13027 saved_in_unbraced_linkage_specification_p
13028 = parser->in_unbraced_linkage_specification_p;
13029 parser->in_unbraced_linkage_specification_p = false;
13031 /* Look for more parameters. */
13034 cp_parameter_declarator *parameter;
13035 bool parenthesized_p;
13036 /* Parse the parameter. */
13038 = cp_parser_parameter_declaration (parser,
13039 /*template_parm_p=*/false,
13042 /* If a parse error occurred parsing the parameter declaration,
13043 then the entire parameter-declaration-list is erroneous. */
13050 /* Add the new parameter to the list. */
13052 tail = ¶meter->next;
13054 /* Peek at the next token. */
13055 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13056 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13057 /* These are for Objective-C++ */
13058 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13059 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13060 /* The parameter-declaration-list is complete. */
13062 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13066 /* Peek at the next token. */
13067 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13068 /* If it's an ellipsis, then the list is complete. */
13069 if (token->type == CPP_ELLIPSIS)
13071 /* Otherwise, there must be more parameters. Consume the
13073 cp_lexer_consume_token (parser->lexer);
13074 /* When parsing something like:
13076 int i(float f, double d)
13078 we can tell after seeing the declaration for "f" that we
13079 are not looking at an initialization of a variable "i",
13080 but rather at the declaration of a function "i".
13082 Due to the fact that the parsing of template arguments
13083 (as specified to a template-id) requires backtracking we
13084 cannot use this technique when inside a template argument
13086 if (!parser->in_template_argument_list_p
13087 && !parser->in_type_id_in_expr_p
13088 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13089 /* However, a parameter-declaration of the form
13090 "foat(f)" (which is a valid declaration of a
13091 parameter "f") can also be interpreted as an
13092 expression (the conversion of "f" to "float"). */
13093 && !parenthesized_p)
13094 cp_parser_commit_to_tentative_parse (parser);
13098 cp_parser_error (parser, "expected %<,%> or %<...%>");
13099 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13100 cp_parser_skip_to_closing_parenthesis (parser,
13101 /*recovering=*/true,
13102 /*or_comma=*/false,
13103 /*consume_paren=*/false);
13108 parser->in_unbraced_linkage_specification_p
13109 = saved_in_unbraced_linkage_specification_p;
13114 /* Parse a parameter declaration.
13116 parameter-declaration:
13117 decl-specifier-seq ... [opt] declarator
13118 decl-specifier-seq declarator = assignment-expression
13119 decl-specifier-seq ... [opt] abstract-declarator [opt]
13120 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13122 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13123 declares a template parameter. (In that case, a non-nested `>'
13124 token encountered during the parsing of the assignment-expression
13125 is not interpreted as a greater-than operator.)
13127 Returns a representation of the parameter, or NULL if an error
13128 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13129 true iff the declarator is of the form "(p)". */
13131 static cp_parameter_declarator *
13132 cp_parser_parameter_declaration (cp_parser *parser,
13133 bool template_parm_p,
13134 bool *parenthesized_p)
13136 int declares_class_or_enum;
13137 bool greater_than_is_operator_p;
13138 cp_decl_specifier_seq decl_specifiers;
13139 cp_declarator *declarator;
13140 tree default_argument;
13142 const char *saved_message;
13144 /* In a template parameter, `>' is not an operator.
13148 When parsing a default template-argument for a non-type
13149 template-parameter, the first non-nested `>' is taken as the end
13150 of the template parameter-list rather than a greater-than
13152 greater_than_is_operator_p = !template_parm_p;
13154 /* Type definitions may not appear in parameter types. */
13155 saved_message = parser->type_definition_forbidden_message;
13156 parser->type_definition_forbidden_message
13157 = "types may not be defined in parameter types";
13159 /* Parse the declaration-specifiers. */
13160 cp_parser_decl_specifier_seq (parser,
13161 CP_PARSER_FLAGS_NONE,
13163 &declares_class_or_enum);
13164 /* If an error occurred, there's no reason to attempt to parse the
13165 rest of the declaration. */
13166 if (cp_parser_error_occurred (parser))
13168 parser->type_definition_forbidden_message = saved_message;
13172 /* Peek at the next token. */
13173 token = cp_lexer_peek_token (parser->lexer);
13175 /* If the next token is a `)', `,', `=', `>', or `...', then there
13176 is no declarator. However, when variadic templates are enabled,
13177 there may be a declarator following `...'. */
13178 if (token->type == CPP_CLOSE_PAREN
13179 || token->type == CPP_COMMA
13180 || token->type == CPP_EQ
13181 || token->type == CPP_GREATER)
13184 if (parenthesized_p)
13185 *parenthesized_p = false;
13187 /* Otherwise, there should be a declarator. */
13190 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13191 parser->default_arg_ok_p = false;
13193 /* After seeing a decl-specifier-seq, if the next token is not a
13194 "(", there is no possibility that the code is a valid
13195 expression. Therefore, if parsing tentatively, we commit at
13197 if (!parser->in_template_argument_list_p
13198 /* In an expression context, having seen:
13202 we cannot be sure whether we are looking at a
13203 function-type (taking a "char" as a parameter) or a cast
13204 of some object of type "char" to "int". */
13205 && !parser->in_type_id_in_expr_p
13206 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13207 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13208 cp_parser_commit_to_tentative_parse (parser);
13209 /* Parse the declarator. */
13210 declarator = cp_parser_declarator (parser,
13211 CP_PARSER_DECLARATOR_EITHER,
13212 /*ctor_dtor_or_conv_p=*/NULL,
13214 /*member_p=*/false);
13215 parser->default_arg_ok_p = saved_default_arg_ok_p;
13216 /* After the declarator, allow more attributes. */
13217 decl_specifiers.attributes
13218 = chainon (decl_specifiers.attributes,
13219 cp_parser_attributes_opt (parser));
13222 /* If the next token is an ellipsis, and we have not seen a
13223 declarator name, and the type of the declarator contains parameter
13224 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13225 a parameter pack expansion expression. Otherwise, leave the
13226 ellipsis for a C-style variadic function. */
13227 token = cp_lexer_peek_token (parser->lexer);
13228 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13230 tree type = decl_specifiers.type;
13232 if (type && DECL_P (type))
13233 type = TREE_TYPE (type);
13236 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13237 && declarator_can_be_parameter_pack (declarator)
13238 && (!declarator || !declarator->parameter_pack_p)
13239 && uses_parameter_packs (type))
13241 /* Consume the `...'. */
13242 cp_lexer_consume_token (parser->lexer);
13243 maybe_warn_variadic_templates ();
13245 /* Build a pack expansion type */
13247 declarator->parameter_pack_p = true;
13249 decl_specifiers.type = make_pack_expansion (type);
13253 /* The restriction on defining new types applies only to the type
13254 of the parameter, not to the default argument. */
13255 parser->type_definition_forbidden_message = saved_message;
13257 /* If the next token is `=', then process a default argument. */
13258 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13260 bool saved_greater_than_is_operator_p;
13261 /* Consume the `='. */
13262 cp_lexer_consume_token (parser->lexer);
13264 /* If we are defining a class, then the tokens that make up the
13265 default argument must be saved and processed later. */
13266 if (!template_parm_p && at_class_scope_p ()
13267 && TYPE_BEING_DEFINED (current_class_type))
13269 unsigned depth = 0;
13270 cp_token *first_token;
13273 /* Add tokens until we have processed the entire default
13274 argument. We add the range [first_token, token). */
13275 first_token = cp_lexer_peek_token (parser->lexer);
13280 /* Peek at the next token. */
13281 token = cp_lexer_peek_token (parser->lexer);
13282 /* What we do depends on what token we have. */
13283 switch (token->type)
13285 /* In valid code, a default argument must be
13286 immediately followed by a `,' `)', or `...'. */
13288 case CPP_CLOSE_PAREN:
13290 /* If we run into a non-nested `;', `}', or `]',
13291 then the code is invalid -- but the default
13292 argument is certainly over. */
13293 case CPP_SEMICOLON:
13294 case CPP_CLOSE_BRACE:
13295 case CPP_CLOSE_SQUARE:
13298 /* Update DEPTH, if necessary. */
13299 else if (token->type == CPP_CLOSE_PAREN
13300 || token->type == CPP_CLOSE_BRACE
13301 || token->type == CPP_CLOSE_SQUARE)
13305 case CPP_OPEN_PAREN:
13306 case CPP_OPEN_SQUARE:
13307 case CPP_OPEN_BRACE:
13312 if (cxx_dialect == cxx98)
13314 /* Fall through for C++0x, which treats the `>>'
13315 operator like two `>' tokens in certain
13319 /* If we see a non-nested `>', and `>' is not an
13320 operator, then it marks the end of the default
13322 if (!depth && !greater_than_is_operator_p)
13326 /* If we run out of tokens, issue an error message. */
13328 case CPP_PRAGMA_EOL:
13329 error ("file ends in default argument");
13335 /* In these cases, we should look for template-ids.
13336 For example, if the default argument is
13337 `X<int, double>()', we need to do name lookup to
13338 figure out whether or not `X' is a template; if
13339 so, the `,' does not end the default argument.
13341 That is not yet done. */
13348 /* If we've reached the end, stop. */
13352 /* Add the token to the token block. */
13353 token = cp_lexer_consume_token (parser->lexer);
13356 /* Create a DEFAULT_ARG to represented the unparsed default
13358 default_argument = make_node (DEFAULT_ARG);
13359 DEFARG_TOKENS (default_argument)
13360 = cp_token_cache_new (first_token, token);
13361 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13363 /* Outside of a class definition, we can just parse the
13364 assignment-expression. */
13367 bool saved_local_variables_forbidden_p;
13369 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13371 saved_greater_than_is_operator_p
13372 = parser->greater_than_is_operator_p;
13373 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13374 /* Local variable names (and the `this' keyword) may not
13375 appear in a default argument. */
13376 saved_local_variables_forbidden_p
13377 = parser->local_variables_forbidden_p;
13378 parser->local_variables_forbidden_p = true;
13379 /* The default argument expression may cause implicitly
13380 defined member functions to be synthesized, which will
13381 result in garbage collection. We must treat this
13382 situation as if we were within the body of function so as
13383 to avoid collecting live data on the stack. */
13385 /* Parse the assignment-expression. */
13386 if (template_parm_p)
13387 push_deferring_access_checks (dk_no_deferred);
13389 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13390 if (template_parm_p)
13391 pop_deferring_access_checks ();
13392 /* Restore saved state. */
13394 parser->greater_than_is_operator_p
13395 = saved_greater_than_is_operator_p;
13396 parser->local_variables_forbidden_p
13397 = saved_local_variables_forbidden_p;
13399 if (!parser->default_arg_ok_p)
13401 if (!flag_pedantic_errors)
13402 warning (0, "deprecated use of default argument for parameter of non-function");
13405 error ("default arguments are only permitted for function parameters");
13406 default_argument = NULL_TREE;
13411 default_argument = NULL_TREE;
13413 return make_parameter_declarator (&decl_specifiers,
13418 /* Parse a function-body.
13421 compound_statement */
13424 cp_parser_function_body (cp_parser *parser)
13426 cp_parser_compound_statement (parser, NULL, false);
13429 /* Parse a ctor-initializer-opt followed by a function-body. Return
13430 true if a ctor-initializer was present. */
13433 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13436 bool ctor_initializer_p;
13438 /* Begin the function body. */
13439 body = begin_function_body ();
13440 /* Parse the optional ctor-initializer. */
13441 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13442 /* Parse the function-body. */
13443 cp_parser_function_body (parser);
13444 /* Finish the function body. */
13445 finish_function_body (body);
13447 return ctor_initializer_p;
13450 /* Parse an initializer.
13453 = initializer-clause
13454 ( expression-list )
13456 Returns an expression representing the initializer. If no
13457 initializer is present, NULL_TREE is returned.
13459 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13460 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13461 set to FALSE if there is no initializer present. If there is an
13462 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13463 is set to true; otherwise it is set to false. */
13466 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13467 bool* non_constant_p)
13472 /* Peek at the next token. */
13473 token = cp_lexer_peek_token (parser->lexer);
13475 /* Let our caller know whether or not this initializer was
13477 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13478 /* Assume that the initializer is constant. */
13479 *non_constant_p = false;
13481 if (token->type == CPP_EQ)
13483 /* Consume the `='. */
13484 cp_lexer_consume_token (parser->lexer);
13485 /* Parse the initializer-clause. */
13486 init = cp_parser_initializer_clause (parser, non_constant_p);
13488 else if (token->type == CPP_OPEN_PAREN)
13489 init = cp_parser_parenthesized_expression_list (parser, false,
13491 /*allow_expansion_p=*/true,
13495 /* Anything else is an error. */
13496 cp_parser_error (parser, "expected initializer");
13497 init = error_mark_node;
13503 /* Parse an initializer-clause.
13505 initializer-clause:
13506 assignment-expression
13507 { initializer-list , [opt] }
13510 Returns an expression representing the initializer.
13512 If the `assignment-expression' production is used the value
13513 returned is simply a representation for the expression.
13515 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13516 the elements of the initializer-list (or NULL, if the last
13517 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13518 NULL_TREE. There is no way to detect whether or not the optional
13519 trailing `,' was provided. NON_CONSTANT_P is as for
13520 cp_parser_initializer. */
13523 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13527 /* Assume the expression is constant. */
13528 *non_constant_p = false;
13530 /* If it is not a `{', then we are looking at an
13531 assignment-expression. */
13532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13535 = cp_parser_constant_expression (parser,
13536 /*allow_non_constant_p=*/true,
13538 if (!*non_constant_p)
13539 initializer = fold_non_dependent_expr (initializer);
13543 /* Consume the `{' token. */
13544 cp_lexer_consume_token (parser->lexer);
13545 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13546 initializer = make_node (CONSTRUCTOR);
13547 /* If it's not a `}', then there is a non-trivial initializer. */
13548 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13550 /* Parse the initializer list. */
13551 CONSTRUCTOR_ELTS (initializer)
13552 = cp_parser_initializer_list (parser, non_constant_p);
13553 /* A trailing `,' token is allowed. */
13554 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13555 cp_lexer_consume_token (parser->lexer);
13557 /* Now, there should be a trailing `}'. */
13558 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13561 return initializer;
13564 /* Parse an initializer-list.
13567 initializer-clause ... [opt]
13568 initializer-list , initializer-clause ... [opt]
13573 identifier : initializer-clause
13574 initializer-list, identifier : initializer-clause
13576 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13577 for the initializer. If the INDEX of the elt is non-NULL, it is the
13578 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13579 as for cp_parser_initializer. */
13581 static VEC(constructor_elt,gc) *
13582 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13584 VEC(constructor_elt,gc) *v = NULL;
13586 /* Assume all of the expressions are constant. */
13587 *non_constant_p = false;
13589 /* Parse the rest of the list. */
13595 bool clause_non_constant_p;
13597 /* If the next token is an identifier and the following one is a
13598 colon, we are looking at the GNU designated-initializer
13600 if (cp_parser_allow_gnu_extensions_p (parser)
13601 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13602 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13604 /* Warn the user that they are using an extension. */
13606 pedwarn ("ISO C++ does not allow designated initializers");
13607 /* Consume the identifier. */
13608 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13609 /* Consume the `:'. */
13610 cp_lexer_consume_token (parser->lexer);
13613 identifier = NULL_TREE;
13615 /* Parse the initializer. */
13616 initializer = cp_parser_initializer_clause (parser,
13617 &clause_non_constant_p);
13618 /* If any clause is non-constant, so is the entire initializer. */
13619 if (clause_non_constant_p)
13620 *non_constant_p = true;
13622 /* If we have an ellipsis, this is an initializer pack
13624 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13626 /* Consume the `...'. */
13627 cp_lexer_consume_token (parser->lexer);
13629 /* Turn the initializer into an initializer expansion. */
13630 initializer = make_pack_expansion (initializer);
13633 /* Add it to the vector. */
13634 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13636 /* If the next token is not a comma, we have reached the end of
13638 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13641 /* Peek at the next token. */
13642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13643 /* If the next token is a `}', then we're still done. An
13644 initializer-clause can have a trailing `,' after the
13645 initializer-list and before the closing `}'. */
13646 if (token->type == CPP_CLOSE_BRACE)
13649 /* Consume the `,' token. */
13650 cp_lexer_consume_token (parser->lexer);
13656 /* Classes [gram.class] */
13658 /* Parse a class-name.
13664 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13665 to indicate that names looked up in dependent types should be
13666 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13667 keyword has been used to indicate that the name that appears next
13668 is a template. TAG_TYPE indicates the explicit tag given before
13669 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13670 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13671 is the class being defined in a class-head.
13673 Returns the TYPE_DECL representing the class. */
13676 cp_parser_class_name (cp_parser *parser,
13677 bool typename_keyword_p,
13678 bool template_keyword_p,
13679 enum tag_types tag_type,
13680 bool check_dependency_p,
13682 bool is_declaration)
13689 /* All class-names start with an identifier. */
13690 token = cp_lexer_peek_token (parser->lexer);
13691 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13693 cp_parser_error (parser, "expected class-name");
13694 return error_mark_node;
13697 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13698 to a template-id, so we save it here. */
13699 scope = parser->scope;
13700 if (scope == error_mark_node)
13701 return error_mark_node;
13703 /* Any name names a type if we're following the `typename' keyword
13704 in a qualified name where the enclosing scope is type-dependent. */
13705 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13706 && dependent_type_p (scope));
13707 /* Handle the common case (an identifier, but not a template-id)
13709 if (token->type == CPP_NAME
13710 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13712 cp_token *identifier_token;
13716 /* Look for the identifier. */
13717 identifier_token = cp_lexer_peek_token (parser->lexer);
13718 ambiguous_p = identifier_token->ambiguous_p;
13719 identifier = cp_parser_identifier (parser);
13720 /* If the next token isn't an identifier, we are certainly not
13721 looking at a class-name. */
13722 if (identifier == error_mark_node)
13723 decl = error_mark_node;
13724 /* If we know this is a type-name, there's no need to look it
13726 else if (typename_p)
13730 tree ambiguous_decls;
13731 /* If we already know that this lookup is ambiguous, then
13732 we've already issued an error message; there's no reason
13736 cp_parser_simulate_error (parser);
13737 return error_mark_node;
13739 /* If the next token is a `::', then the name must be a type
13742 [basic.lookup.qual]
13744 During the lookup for a name preceding the :: scope
13745 resolution operator, object, function, and enumerator
13746 names are ignored. */
13747 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13748 tag_type = typename_type;
13749 /* Look up the name. */
13750 decl = cp_parser_lookup_name (parser, identifier,
13752 /*is_template=*/false,
13753 /*is_namespace=*/false,
13754 check_dependency_p,
13756 if (ambiguous_decls)
13758 error ("reference to %qD is ambiguous", identifier);
13759 print_candidates (ambiguous_decls);
13760 if (cp_parser_parsing_tentatively (parser))
13762 identifier_token->ambiguous_p = true;
13763 cp_parser_simulate_error (parser);
13765 return error_mark_node;
13771 /* Try a template-id. */
13772 decl = cp_parser_template_id (parser, template_keyword_p,
13773 check_dependency_p,
13775 if (decl == error_mark_node)
13776 return error_mark_node;
13779 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13781 /* If this is a typename, create a TYPENAME_TYPE. */
13782 if (typename_p && decl != error_mark_node)
13784 decl = make_typename_type (scope, decl, typename_type,
13785 /*complain=*/tf_error);
13786 if (decl != error_mark_node)
13787 decl = TYPE_NAME (decl);
13790 /* Check to see that it is really the name of a class. */
13791 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13792 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13793 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13794 /* Situations like this:
13796 template <typename T> struct A {
13797 typename T::template X<int>::I i;
13800 are problematic. Is `T::template X<int>' a class-name? The
13801 standard does not seem to be definitive, but there is no other
13802 valid interpretation of the following `::'. Therefore, those
13803 names are considered class-names. */
13805 decl = make_typename_type (scope, decl, tag_type, tf_error);
13806 if (decl != error_mark_node)
13807 decl = TYPE_NAME (decl);
13809 else if (TREE_CODE (decl) != TYPE_DECL
13810 || TREE_TYPE (decl) == error_mark_node
13811 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13812 decl = error_mark_node;
13814 if (decl == error_mark_node)
13815 cp_parser_error (parser, "expected class-name");
13820 /* Parse a class-specifier.
13823 class-head { member-specification [opt] }
13825 Returns the TREE_TYPE representing the class. */
13828 cp_parser_class_specifier (cp_parser* parser)
13832 tree attributes = NULL_TREE;
13833 int has_trailing_semicolon;
13834 bool nested_name_specifier_p;
13835 unsigned saved_num_template_parameter_lists;
13836 bool saved_in_function_body;
13837 tree old_scope = NULL_TREE;
13838 tree scope = NULL_TREE;
13841 push_deferring_access_checks (dk_no_deferred);
13843 /* Parse the class-head. */
13844 type = cp_parser_class_head (parser,
13845 &nested_name_specifier_p,
13848 /* If the class-head was a semantic disaster, skip the entire body
13852 cp_parser_skip_to_end_of_block_or_statement (parser);
13853 pop_deferring_access_checks ();
13854 return error_mark_node;
13857 /* Look for the `{'. */
13858 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13860 pop_deferring_access_checks ();
13861 return error_mark_node;
13864 /* Process the base classes. If they're invalid, skip the
13865 entire class body. */
13866 if (!xref_basetypes (type, bases))
13868 /* Consuming the closing brace yields better error messages
13870 if (cp_parser_skip_to_closing_brace (parser))
13871 cp_lexer_consume_token (parser->lexer);
13872 pop_deferring_access_checks ();
13873 return error_mark_node;
13876 /* Issue an error message if type-definitions are forbidden here. */
13877 cp_parser_check_type_definition (parser);
13878 /* Remember that we are defining one more class. */
13879 ++parser->num_classes_being_defined;
13880 /* Inside the class, surrounding template-parameter-lists do not
13882 saved_num_template_parameter_lists
13883 = parser->num_template_parameter_lists;
13884 parser->num_template_parameter_lists = 0;
13885 /* We are not in a function body. */
13886 saved_in_function_body = parser->in_function_body;
13887 parser->in_function_body = false;
13889 /* Start the class. */
13890 if (nested_name_specifier_p)
13892 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13893 old_scope = push_inner_scope (scope);
13895 type = begin_class_definition (type, attributes);
13897 if (type == error_mark_node)
13898 /* If the type is erroneous, skip the entire body of the class. */
13899 cp_parser_skip_to_closing_brace (parser);
13901 /* Parse the member-specification. */
13902 cp_parser_member_specification_opt (parser);
13904 /* Look for the trailing `}'. */
13905 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13906 /* We get better error messages by noticing a common problem: a
13907 missing trailing `;'. */
13908 token = cp_lexer_peek_token (parser->lexer);
13909 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13910 /* Look for trailing attributes to apply to this class. */
13911 if (cp_parser_allow_gnu_extensions_p (parser))
13912 attributes = cp_parser_attributes_opt (parser);
13913 if (type != error_mark_node)
13914 type = finish_struct (type, attributes);
13915 if (nested_name_specifier_p)
13916 pop_inner_scope (old_scope, scope);
13917 /* If this class is not itself within the scope of another class,
13918 then we need to parse the bodies of all of the queued function
13919 definitions. Note that the queued functions defined in a class
13920 are not always processed immediately following the
13921 class-specifier for that class. Consider:
13924 struct B { void f() { sizeof (A); } };
13927 If `f' were processed before the processing of `A' were
13928 completed, there would be no way to compute the size of `A'.
13929 Note that the nesting we are interested in here is lexical --
13930 not the semantic nesting given by TYPE_CONTEXT. In particular,
13933 struct A { struct B; };
13934 struct A::B { void f() { } };
13936 there is no need to delay the parsing of `A::B::f'. */
13937 if (--parser->num_classes_being_defined == 0)
13941 tree class_type = NULL_TREE;
13942 tree pushed_scope = NULL_TREE;
13944 /* In a first pass, parse default arguments to the functions.
13945 Then, in a second pass, parse the bodies of the functions.
13946 This two-phased approach handles cases like:
13954 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13955 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13956 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13957 TREE_PURPOSE (parser->unparsed_functions_queues)
13958 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13960 fn = TREE_VALUE (queue_entry);
13961 /* If there are default arguments that have not yet been processed,
13962 take care of them now. */
13963 if (class_type != TREE_PURPOSE (queue_entry))
13966 pop_scope (pushed_scope);
13967 class_type = TREE_PURPOSE (queue_entry);
13968 pushed_scope = push_scope (class_type);
13970 /* Make sure that any template parameters are in scope. */
13971 maybe_begin_member_template_processing (fn);
13972 /* Parse the default argument expressions. */
13973 cp_parser_late_parsing_default_args (parser, fn);
13974 /* Remove any template parameters from the symbol table. */
13975 maybe_end_member_template_processing ();
13978 pop_scope (pushed_scope);
13979 /* Now parse the body of the functions. */
13980 for (TREE_VALUE (parser->unparsed_functions_queues)
13981 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13982 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13983 TREE_VALUE (parser->unparsed_functions_queues)
13984 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13986 /* Figure out which function we need to process. */
13987 fn = TREE_VALUE (queue_entry);
13988 /* Parse the function. */
13989 cp_parser_late_parsing_for_member (parser, fn);
13993 /* Put back any saved access checks. */
13994 pop_deferring_access_checks ();
13996 /* Restore saved state. */
13997 parser->in_function_body = saved_in_function_body;
13998 parser->num_template_parameter_lists
13999 = saved_num_template_parameter_lists;
14004 /* Parse a class-head.
14007 class-key identifier [opt] base-clause [opt]
14008 class-key nested-name-specifier identifier base-clause [opt]
14009 class-key nested-name-specifier [opt] template-id
14013 class-key attributes identifier [opt] base-clause [opt]
14014 class-key attributes nested-name-specifier identifier base-clause [opt]
14015 class-key attributes nested-name-specifier [opt] template-id
14018 Upon return BASES is initialized to the list of base classes (or
14019 NULL, if there are none) in the same form returned by
14020 cp_parser_base_clause.
14022 Returns the TYPE of the indicated class. Sets
14023 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14024 involving a nested-name-specifier was used, and FALSE otherwise.
14026 Returns error_mark_node if this is not a class-head.
14028 Returns NULL_TREE if the class-head is syntactically valid, but
14029 semantically invalid in a way that means we should skip the entire
14030 body of the class. */
14033 cp_parser_class_head (cp_parser* parser,
14034 bool* nested_name_specifier_p,
14035 tree *attributes_p,
14038 tree nested_name_specifier;
14039 enum tag_types class_key;
14040 tree id = NULL_TREE;
14041 tree type = NULL_TREE;
14043 bool template_id_p = false;
14044 bool qualified_p = false;
14045 bool invalid_nested_name_p = false;
14046 bool invalid_explicit_specialization_p = false;
14047 tree pushed_scope = NULL_TREE;
14048 unsigned num_templates;
14050 /* Assume no nested-name-specifier will be present. */
14051 *nested_name_specifier_p = false;
14052 /* Assume no template parameter lists will be used in defining the
14056 *bases = NULL_TREE;
14058 /* Look for the class-key. */
14059 class_key = cp_parser_class_key (parser);
14060 if (class_key == none_type)
14061 return error_mark_node;
14063 /* Parse the attributes. */
14064 attributes = cp_parser_attributes_opt (parser);
14066 /* If the next token is `::', that is invalid -- but sometimes
14067 people do try to write:
14071 Handle this gracefully by accepting the extra qualifier, and then
14072 issuing an error about it later if this really is a
14073 class-head. If it turns out just to be an elaborated type
14074 specifier, remain silent. */
14075 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14076 qualified_p = true;
14078 push_deferring_access_checks (dk_no_check);
14080 /* Determine the name of the class. Begin by looking for an
14081 optional nested-name-specifier. */
14082 nested_name_specifier
14083 = cp_parser_nested_name_specifier_opt (parser,
14084 /*typename_keyword_p=*/false,
14085 /*check_dependency_p=*/false,
14087 /*is_declaration=*/false);
14088 /* If there was a nested-name-specifier, then there *must* be an
14090 if (nested_name_specifier)
14092 /* Although the grammar says `identifier', it really means
14093 `class-name' or `template-name'. You are only allowed to
14094 define a class that has already been declared with this
14097 The proposed resolution for Core Issue 180 says that wherever
14098 you see `class T::X' you should treat `X' as a type-name.
14100 It is OK to define an inaccessible class; for example:
14102 class A { class B; };
14105 We do not know if we will see a class-name, or a
14106 template-name. We look for a class-name first, in case the
14107 class-name is a template-id; if we looked for the
14108 template-name first we would stop after the template-name. */
14109 cp_parser_parse_tentatively (parser);
14110 type = cp_parser_class_name (parser,
14111 /*typename_keyword_p=*/false,
14112 /*template_keyword_p=*/false,
14114 /*check_dependency_p=*/false,
14115 /*class_head_p=*/true,
14116 /*is_declaration=*/false);
14117 /* If that didn't work, ignore the nested-name-specifier. */
14118 if (!cp_parser_parse_definitely (parser))
14120 invalid_nested_name_p = true;
14121 id = cp_parser_identifier (parser);
14122 if (id == error_mark_node)
14125 /* If we could not find a corresponding TYPE, treat this
14126 declaration like an unqualified declaration. */
14127 if (type == error_mark_node)
14128 nested_name_specifier = NULL_TREE;
14129 /* Otherwise, count the number of templates used in TYPE and its
14130 containing scopes. */
14135 for (scope = TREE_TYPE (type);
14136 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14137 scope = (TYPE_P (scope)
14138 ? TYPE_CONTEXT (scope)
14139 : DECL_CONTEXT (scope)))
14141 && CLASS_TYPE_P (scope)
14142 && CLASSTYPE_TEMPLATE_INFO (scope)
14143 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14144 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14148 /* Otherwise, the identifier is optional. */
14151 /* We don't know whether what comes next is a template-id,
14152 an identifier, or nothing at all. */
14153 cp_parser_parse_tentatively (parser);
14154 /* Check for a template-id. */
14155 id = cp_parser_template_id (parser,
14156 /*template_keyword_p=*/false,
14157 /*check_dependency_p=*/true,
14158 /*is_declaration=*/true);
14159 /* If that didn't work, it could still be an identifier. */
14160 if (!cp_parser_parse_definitely (parser))
14162 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14163 id = cp_parser_identifier (parser);
14169 template_id_p = true;
14174 pop_deferring_access_checks ();
14177 cp_parser_check_for_invalid_template_id (parser, id);
14179 /* If it's not a `:' or a `{' then we can't really be looking at a
14180 class-head, since a class-head only appears as part of a
14181 class-specifier. We have to detect this situation before calling
14182 xref_tag, since that has irreversible side-effects. */
14183 if (!cp_parser_next_token_starts_class_definition_p (parser))
14185 cp_parser_error (parser, "expected %<{%> or %<:%>");
14186 return error_mark_node;
14189 /* At this point, we're going ahead with the class-specifier, even
14190 if some other problem occurs. */
14191 cp_parser_commit_to_tentative_parse (parser);
14192 /* Issue the error about the overly-qualified name now. */
14194 cp_parser_error (parser,
14195 "global qualification of class name is invalid");
14196 else if (invalid_nested_name_p)
14197 cp_parser_error (parser,
14198 "qualified name does not name a class");
14199 else if (nested_name_specifier)
14203 /* Reject typedef-names in class heads. */
14204 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14206 error ("invalid class name in declaration of %qD", type);
14211 /* Figure out in what scope the declaration is being placed. */
14212 scope = current_scope ();
14213 /* If that scope does not contain the scope in which the
14214 class was originally declared, the program is invalid. */
14215 if (scope && !is_ancestor (scope, nested_name_specifier))
14217 error ("declaration of %qD in %qD which does not enclose %qD",
14218 type, scope, nested_name_specifier);
14224 A declarator-id shall not be qualified exception of the
14225 definition of a ... nested class outside of its class
14226 ... [or] a the definition or explicit instantiation of a
14227 class member of a namespace outside of its namespace. */
14228 if (scope == nested_name_specifier)
14230 pedwarn ("extra qualification ignored");
14231 nested_name_specifier = NULL_TREE;
14235 /* An explicit-specialization must be preceded by "template <>". If
14236 it is not, try to recover gracefully. */
14237 if (at_namespace_scope_p ()
14238 && parser->num_template_parameter_lists == 0
14241 error ("an explicit specialization must be preceded by %<template <>%>");
14242 invalid_explicit_specialization_p = true;
14243 /* Take the same action that would have been taken by
14244 cp_parser_explicit_specialization. */
14245 ++parser->num_template_parameter_lists;
14246 begin_specialization ();
14248 /* There must be no "return" statements between this point and the
14249 end of this function; set "type "to the correct return value and
14250 use "goto done;" to return. */
14251 /* Make sure that the right number of template parameters were
14253 if (!cp_parser_check_template_parameters (parser, num_templates))
14255 /* If something went wrong, there is no point in even trying to
14256 process the class-definition. */
14261 /* Look up the type. */
14264 type = TREE_TYPE (id);
14265 type = maybe_process_partial_specialization (type);
14266 if (nested_name_specifier)
14267 pushed_scope = push_scope (nested_name_specifier);
14269 else if (nested_name_specifier)
14275 template <typename T> struct S { struct T };
14276 template <typename T> struct S<T>::T { };
14278 we will get a TYPENAME_TYPE when processing the definition of
14279 `S::T'. We need to resolve it to the actual type before we
14280 try to define it. */
14281 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14283 class_type = resolve_typename_type (TREE_TYPE (type),
14284 /*only_current_p=*/false);
14285 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14286 type = TYPE_NAME (class_type);
14289 cp_parser_error (parser, "could not resolve typename type");
14290 type = error_mark_node;
14294 maybe_process_partial_specialization (TREE_TYPE (type));
14295 class_type = current_class_type;
14296 /* Enter the scope indicated by the nested-name-specifier. */
14297 pushed_scope = push_scope (nested_name_specifier);
14298 /* Get the canonical version of this type. */
14299 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14300 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14301 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14303 type = push_template_decl (type);
14304 if (type == error_mark_node)
14311 type = TREE_TYPE (type);
14312 *nested_name_specifier_p = true;
14314 else /* The name is not a nested name. */
14316 /* If the class was unnamed, create a dummy name. */
14318 id = make_anon_name ();
14319 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14320 parser->num_template_parameter_lists);
14323 /* Indicate whether this class was declared as a `class' or as a
14325 if (TREE_CODE (type) == RECORD_TYPE)
14326 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14327 cp_parser_check_class_key (class_key, type);
14329 /* If this type was already complete, and we see another definition,
14330 that's an error. */
14331 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14333 error ("redefinition of %q#T", type);
14334 error ("previous definition of %q+#T", type);
14338 else if (type == error_mark_node)
14341 /* We will have entered the scope containing the class; the names of
14342 base classes should be looked up in that context. For example:
14344 struct A { struct B {}; struct C; };
14345 struct A::C : B {};
14349 /* Get the list of base-classes, if there is one. */
14350 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14351 *bases = cp_parser_base_clause (parser);
14354 /* Leave the scope given by the nested-name-specifier. We will
14355 enter the class scope itself while processing the members. */
14357 pop_scope (pushed_scope);
14359 if (invalid_explicit_specialization_p)
14361 end_specialization ();
14362 --parser->num_template_parameter_lists;
14364 *attributes_p = attributes;
14368 /* Parse a class-key.
14375 Returns the kind of class-key specified, or none_type to indicate
14378 static enum tag_types
14379 cp_parser_class_key (cp_parser* parser)
14382 enum tag_types tag_type;
14384 /* Look for the class-key. */
14385 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14389 /* Check to see if the TOKEN is a class-key. */
14390 tag_type = cp_parser_token_is_class_key (token);
14392 cp_parser_error (parser, "expected class-key");
14396 /* Parse an (optional) member-specification.
14398 member-specification:
14399 member-declaration member-specification [opt]
14400 access-specifier : member-specification [opt] */
14403 cp_parser_member_specification_opt (cp_parser* parser)
14410 /* Peek at the next token. */
14411 token = cp_lexer_peek_token (parser->lexer);
14412 /* If it's a `}', or EOF then we've seen all the members. */
14413 if (token->type == CPP_CLOSE_BRACE
14414 || token->type == CPP_EOF
14415 || token->type == CPP_PRAGMA_EOL)
14418 /* See if this token is a keyword. */
14419 keyword = token->keyword;
14423 case RID_PROTECTED:
14425 /* Consume the access-specifier. */
14426 cp_lexer_consume_token (parser->lexer);
14427 /* Remember which access-specifier is active. */
14428 current_access_specifier = token->u.value;
14429 /* Look for the `:'. */
14430 cp_parser_require (parser, CPP_COLON, "`:'");
14434 /* Accept #pragmas at class scope. */
14435 if (token->type == CPP_PRAGMA)
14437 cp_parser_pragma (parser, pragma_external);
14441 /* Otherwise, the next construction must be a
14442 member-declaration. */
14443 cp_parser_member_declaration (parser);
14448 /* Parse a member-declaration.
14450 member-declaration:
14451 decl-specifier-seq [opt] member-declarator-list [opt] ;
14452 function-definition ; [opt]
14453 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14455 template-declaration
14457 member-declarator-list:
14459 member-declarator-list , member-declarator
14462 declarator pure-specifier [opt]
14463 declarator constant-initializer [opt]
14464 identifier [opt] : constant-expression
14468 member-declaration:
14469 __extension__ member-declaration
14472 declarator attributes [opt] pure-specifier [opt]
14473 declarator attributes [opt] constant-initializer [opt]
14474 identifier [opt] attributes [opt] : constant-expression
14478 member-declaration:
14479 static_assert-declaration */
14482 cp_parser_member_declaration (cp_parser* parser)
14484 cp_decl_specifier_seq decl_specifiers;
14485 tree prefix_attributes;
14487 int declares_class_or_enum;
14490 int saved_pedantic;
14492 /* Check for the `__extension__' keyword. */
14493 if (cp_parser_extension_opt (parser, &saved_pedantic))
14496 cp_parser_member_declaration (parser);
14497 /* Restore the old value of the PEDANTIC flag. */
14498 pedantic = saved_pedantic;
14503 /* Check for a template-declaration. */
14504 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14506 /* An explicit specialization here is an error condition, and we
14507 expect the specialization handler to detect and report this. */
14508 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14509 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14510 cp_parser_explicit_specialization (parser);
14512 cp_parser_template_declaration (parser, /*member_p=*/true);
14517 /* Check for a using-declaration. */
14518 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14520 /* Parse the using-declaration. */
14521 cp_parser_using_declaration (parser,
14522 /*access_declaration_p=*/false);
14526 /* Check for @defs. */
14527 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14530 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14531 ivar = ivar_chains;
14535 ivar = TREE_CHAIN (member);
14536 TREE_CHAIN (member) = NULL_TREE;
14537 finish_member_declaration (member);
14542 /* If the next token is `static_assert' we have a static assertion. */
14543 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14545 cp_parser_static_assert (parser, /*member_p=*/true);
14549 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14552 /* Parse the decl-specifier-seq. */
14553 cp_parser_decl_specifier_seq (parser,
14554 CP_PARSER_FLAGS_OPTIONAL,
14556 &declares_class_or_enum);
14557 prefix_attributes = decl_specifiers.attributes;
14558 decl_specifiers.attributes = NULL_TREE;
14559 /* Check for an invalid type-name. */
14560 if (!decl_specifiers.type
14561 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14563 /* If there is no declarator, then the decl-specifier-seq should
14565 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14567 /* If there was no decl-specifier-seq, and the next token is a
14568 `;', then we have something like:
14574 Each member-declaration shall declare at least one member
14575 name of the class. */
14576 if (!decl_specifiers.any_specifiers_p)
14578 cp_token *token = cp_lexer_peek_token (parser->lexer);
14579 if (pedantic && !token->in_system_header)
14580 pedwarn ("%Hextra %<;%>", &token->location);
14586 /* See if this declaration is a friend. */
14587 friend_p = cp_parser_friend_p (&decl_specifiers);
14588 /* If there were decl-specifiers, check to see if there was
14589 a class-declaration. */
14590 type = check_tag_decl (&decl_specifiers);
14591 /* Nested classes have already been added to the class, but
14592 a `friend' needs to be explicitly registered. */
14595 /* If the `friend' keyword was present, the friend must
14596 be introduced with a class-key. */
14597 if (!declares_class_or_enum)
14598 error ("a class-key must be used when declaring a friend");
14601 template <typename T> struct A {
14602 friend struct A<T>::B;
14605 A<T>::B will be represented by a TYPENAME_TYPE, and
14606 therefore not recognized by check_tag_decl. */
14608 && decl_specifiers.type
14609 && TYPE_P (decl_specifiers.type))
14610 type = decl_specifiers.type;
14611 if (!type || !TYPE_P (type))
14612 error ("friend declaration does not name a class or "
14615 make_friend_class (current_class_type, type,
14616 /*complain=*/true);
14618 /* If there is no TYPE, an error message will already have
14620 else if (!type || type == error_mark_node)
14622 /* An anonymous aggregate has to be handled specially; such
14623 a declaration really declares a data member (with a
14624 particular type), as opposed to a nested class. */
14625 else if (ANON_AGGR_TYPE_P (type))
14627 /* Remove constructors and such from TYPE, now that we
14628 know it is an anonymous aggregate. */
14629 fixup_anonymous_aggr (type);
14630 /* And make the corresponding data member. */
14631 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14632 /* Add it to the class. */
14633 finish_member_declaration (decl);
14636 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14641 /* See if these declarations will be friends. */
14642 friend_p = cp_parser_friend_p (&decl_specifiers);
14644 /* Keep going until we hit the `;' at the end of the
14646 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14648 tree attributes = NULL_TREE;
14649 tree first_attribute;
14651 /* Peek at the next token. */
14652 token = cp_lexer_peek_token (parser->lexer);
14654 /* Check for a bitfield declaration. */
14655 if (token->type == CPP_COLON
14656 || (token->type == CPP_NAME
14657 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14663 /* Get the name of the bitfield. Note that we cannot just
14664 check TOKEN here because it may have been invalidated by
14665 the call to cp_lexer_peek_nth_token above. */
14666 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14667 identifier = cp_parser_identifier (parser);
14669 identifier = NULL_TREE;
14671 /* Consume the `:' token. */
14672 cp_lexer_consume_token (parser->lexer);
14673 /* Get the width of the bitfield. */
14675 = cp_parser_constant_expression (parser,
14676 /*allow_non_constant=*/false,
14679 /* Look for attributes that apply to the bitfield. */
14680 attributes = cp_parser_attributes_opt (parser);
14681 /* Remember which attributes are prefix attributes and
14683 first_attribute = attributes;
14684 /* Combine the attributes. */
14685 attributes = chainon (prefix_attributes, attributes);
14687 /* Create the bitfield declaration. */
14688 decl = grokbitfield (identifier
14689 ? make_id_declarator (NULL_TREE,
14695 /* Apply the attributes. */
14696 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14700 cp_declarator *declarator;
14702 tree asm_specification;
14703 int ctor_dtor_or_conv_p;
14705 /* Parse the declarator. */
14707 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14708 &ctor_dtor_or_conv_p,
14709 /*parenthesized_p=*/NULL,
14710 /*member_p=*/true);
14712 /* If something went wrong parsing the declarator, make sure
14713 that we at least consume some tokens. */
14714 if (declarator == cp_error_declarator)
14716 /* Skip to the end of the statement. */
14717 cp_parser_skip_to_end_of_statement (parser);
14718 /* If the next token is not a semicolon, that is
14719 probably because we just skipped over the body of
14720 a function. So, we consume a semicolon if
14721 present, but do not issue an error message if it
14723 if (cp_lexer_next_token_is (parser->lexer,
14725 cp_lexer_consume_token (parser->lexer);
14729 if (declares_class_or_enum & 2)
14730 cp_parser_check_for_definition_in_return_type
14731 (declarator, decl_specifiers.type);
14733 /* Look for an asm-specification. */
14734 asm_specification = cp_parser_asm_specification_opt (parser);
14735 /* Look for attributes that apply to the declaration. */
14736 attributes = cp_parser_attributes_opt (parser);
14737 /* Remember which attributes are prefix attributes and
14739 first_attribute = attributes;
14740 /* Combine the attributes. */
14741 attributes = chainon (prefix_attributes, attributes);
14743 /* If it's an `=', then we have a constant-initializer or a
14744 pure-specifier. It is not correct to parse the
14745 initializer before registering the member declaration
14746 since the member declaration should be in scope while
14747 its initializer is processed. However, the rest of the
14748 front end does not yet provide an interface that allows
14749 us to handle this correctly. */
14750 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14754 A pure-specifier shall be used only in the declaration of
14755 a virtual function.
14757 A member-declarator can contain a constant-initializer
14758 only if it declares a static member of integral or
14761 Therefore, if the DECLARATOR is for a function, we look
14762 for a pure-specifier; otherwise, we look for a
14763 constant-initializer. When we call `grokfield', it will
14764 perform more stringent semantics checks. */
14765 if (function_declarator_p (declarator))
14766 initializer = cp_parser_pure_specifier (parser);
14768 /* Parse the initializer. */
14769 initializer = cp_parser_constant_initializer (parser);
14771 /* Otherwise, there is no initializer. */
14773 initializer = NULL_TREE;
14775 /* See if we are probably looking at a function
14776 definition. We are certainly not looking at a
14777 member-declarator. Calling `grokfield' has
14778 side-effects, so we must not do it unless we are sure
14779 that we are looking at a member-declarator. */
14780 if (cp_parser_token_starts_function_definition_p
14781 (cp_lexer_peek_token (parser->lexer)))
14783 /* The grammar does not allow a pure-specifier to be
14784 used when a member function is defined. (It is
14785 possible that this fact is an oversight in the
14786 standard, since a pure function may be defined
14787 outside of the class-specifier. */
14789 error ("pure-specifier on function-definition");
14790 decl = cp_parser_save_member_function_body (parser,
14794 /* If the member was not a friend, declare it here. */
14796 finish_member_declaration (decl);
14797 /* Peek at the next token. */
14798 token = cp_lexer_peek_token (parser->lexer);
14799 /* If the next token is a semicolon, consume it. */
14800 if (token->type == CPP_SEMICOLON)
14802 if (pedantic && !in_system_header)
14803 pedwarn ("extra %<;%>");
14804 cp_lexer_consume_token (parser->lexer);
14809 /* Create the declaration. */
14810 decl = grokfield (declarator, &decl_specifiers,
14811 initializer, /*init_const_expr_p=*/true,
14816 /* Reset PREFIX_ATTRIBUTES. */
14817 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14818 attributes = TREE_CHAIN (attributes);
14820 TREE_CHAIN (attributes) = NULL_TREE;
14822 /* If there is any qualification still in effect, clear it
14823 now; we will be starting fresh with the next declarator. */
14824 parser->scope = NULL_TREE;
14825 parser->qualifying_scope = NULL_TREE;
14826 parser->object_scope = NULL_TREE;
14827 /* If it's a `,', then there are more declarators. */
14828 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14829 cp_lexer_consume_token (parser->lexer);
14830 /* If the next token isn't a `;', then we have a parse error. */
14831 else if (cp_lexer_next_token_is_not (parser->lexer,
14834 cp_parser_error (parser, "expected %<;%>");
14835 /* Skip tokens until we find a `;'. */
14836 cp_parser_skip_to_end_of_statement (parser);
14843 /* Add DECL to the list of members. */
14845 finish_member_declaration (decl);
14847 if (TREE_CODE (decl) == FUNCTION_DECL)
14848 cp_parser_save_default_args (parser, decl);
14853 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14856 /* Parse a pure-specifier.
14861 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14862 Otherwise, ERROR_MARK_NODE is returned. */
14865 cp_parser_pure_specifier (cp_parser* parser)
14869 /* Look for the `=' token. */
14870 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14871 return error_mark_node;
14872 /* Look for the `0' token. */
14873 token = cp_lexer_consume_token (parser->lexer);
14874 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14875 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14877 cp_parser_error (parser,
14878 "invalid pure specifier (only `= 0' is allowed)");
14879 cp_parser_skip_to_end_of_statement (parser);
14880 return error_mark_node;
14882 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14884 error ("templates may not be %<virtual%>");
14885 return error_mark_node;
14888 return integer_zero_node;
14891 /* Parse a constant-initializer.
14893 constant-initializer:
14894 = constant-expression
14896 Returns a representation of the constant-expression. */
14899 cp_parser_constant_initializer (cp_parser* parser)
14901 /* Look for the `=' token. */
14902 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14903 return error_mark_node;
14905 /* It is invalid to write:
14907 struct S { static const int i = { 7 }; };
14910 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14912 cp_parser_error (parser,
14913 "a brace-enclosed initializer is not allowed here");
14914 /* Consume the opening brace. */
14915 cp_lexer_consume_token (parser->lexer);
14916 /* Skip the initializer. */
14917 cp_parser_skip_to_closing_brace (parser);
14918 /* Look for the trailing `}'. */
14919 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14921 return error_mark_node;
14924 return cp_parser_constant_expression (parser,
14925 /*allow_non_constant=*/false,
14929 /* Derived classes [gram.class.derived] */
14931 /* Parse a base-clause.
14934 : base-specifier-list
14936 base-specifier-list:
14937 base-specifier ... [opt]
14938 base-specifier-list , base-specifier ... [opt]
14940 Returns a TREE_LIST representing the base-classes, in the order in
14941 which they were declared. The representation of each node is as
14942 described by cp_parser_base_specifier.
14944 In the case that no bases are specified, this function will return
14945 NULL_TREE, not ERROR_MARK_NODE. */
14948 cp_parser_base_clause (cp_parser* parser)
14950 tree bases = NULL_TREE;
14952 /* Look for the `:' that begins the list. */
14953 cp_parser_require (parser, CPP_COLON, "`:'");
14955 /* Scan the base-specifier-list. */
14960 bool pack_expansion_p = false;
14962 /* Look for the base-specifier. */
14963 base = cp_parser_base_specifier (parser);
14964 /* Look for the (optional) ellipsis. */
14965 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14967 /* Consume the `...'. */
14968 cp_lexer_consume_token (parser->lexer);
14970 pack_expansion_p = true;
14973 /* Add BASE to the front of the list. */
14974 if (base != error_mark_node)
14976 if (pack_expansion_p)
14977 /* Make this a pack expansion type. */
14978 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14980 check_for_bare_parameter_packs (TREE_VALUE (base));
14982 TREE_CHAIN (base) = bases;
14985 /* Peek at the next token. */
14986 token = cp_lexer_peek_token (parser->lexer);
14987 /* If it's not a comma, then the list is complete. */
14988 if (token->type != CPP_COMMA)
14990 /* Consume the `,'. */
14991 cp_lexer_consume_token (parser->lexer);
14994 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14995 base class had a qualified name. However, the next name that
14996 appears is certainly not qualified. */
14997 parser->scope = NULL_TREE;
14998 parser->qualifying_scope = NULL_TREE;
14999 parser->object_scope = NULL_TREE;
15001 return nreverse (bases);
15004 /* Parse a base-specifier.
15007 :: [opt] nested-name-specifier [opt] class-name
15008 virtual access-specifier [opt] :: [opt] nested-name-specifier
15010 access-specifier virtual [opt] :: [opt] nested-name-specifier
15013 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15014 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15015 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15016 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15019 cp_parser_base_specifier (cp_parser* parser)
15023 bool virtual_p = false;
15024 bool duplicate_virtual_error_issued_p = false;
15025 bool duplicate_access_error_issued_p = false;
15026 bool class_scope_p, template_p;
15027 tree access = access_default_node;
15030 /* Process the optional `virtual' and `access-specifier'. */
15033 /* Peek at the next token. */
15034 token = cp_lexer_peek_token (parser->lexer);
15035 /* Process `virtual'. */
15036 switch (token->keyword)
15039 /* If `virtual' appears more than once, issue an error. */
15040 if (virtual_p && !duplicate_virtual_error_issued_p)
15042 cp_parser_error (parser,
15043 "%<virtual%> specified more than once in base-specified");
15044 duplicate_virtual_error_issued_p = true;
15049 /* Consume the `virtual' token. */
15050 cp_lexer_consume_token (parser->lexer);
15055 case RID_PROTECTED:
15057 /* If more than one access specifier appears, issue an
15059 if (access != access_default_node
15060 && !duplicate_access_error_issued_p)
15062 cp_parser_error (parser,
15063 "more than one access specifier in base-specified");
15064 duplicate_access_error_issued_p = true;
15067 access = ridpointers[(int) token->keyword];
15069 /* Consume the access-specifier. */
15070 cp_lexer_consume_token (parser->lexer);
15079 /* It is not uncommon to see programs mechanically, erroneously, use
15080 the 'typename' keyword to denote (dependent) qualified types
15081 as base classes. */
15082 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15084 if (!processing_template_decl)
15085 error ("keyword %<typename%> not allowed outside of templates");
15087 error ("keyword %<typename%> not allowed in this context "
15088 "(the base class is implicitly a type)");
15089 cp_lexer_consume_token (parser->lexer);
15092 /* Look for the optional `::' operator. */
15093 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15094 /* Look for the nested-name-specifier. The simplest way to
15099 The keyword `typename' is not permitted in a base-specifier or
15100 mem-initializer; in these contexts a qualified name that
15101 depends on a template-parameter is implicitly assumed to be a
15104 is to pretend that we have seen the `typename' keyword at this
15106 cp_parser_nested_name_specifier_opt (parser,
15107 /*typename_keyword_p=*/true,
15108 /*check_dependency_p=*/true,
15110 /*is_declaration=*/true);
15111 /* If the base class is given by a qualified name, assume that names
15112 we see are type names or templates, as appropriate. */
15113 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15114 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15116 /* Finally, look for the class-name. */
15117 type = cp_parser_class_name (parser,
15121 /*check_dependency_p=*/true,
15122 /*class_head_p=*/false,
15123 /*is_declaration=*/true);
15125 if (type == error_mark_node)
15126 return error_mark_node;
15128 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15131 /* Exception handling [gram.exception] */
15133 /* Parse an (optional) exception-specification.
15135 exception-specification:
15136 throw ( type-id-list [opt] )
15138 Returns a TREE_LIST representing the exception-specification. The
15139 TREE_VALUE of each node is a type. */
15142 cp_parser_exception_specification_opt (cp_parser* parser)
15147 /* Peek at the next token. */
15148 token = cp_lexer_peek_token (parser->lexer);
15149 /* If it's not `throw', then there's no exception-specification. */
15150 if (!cp_parser_is_keyword (token, RID_THROW))
15153 /* Consume the `throw'. */
15154 cp_lexer_consume_token (parser->lexer);
15156 /* Look for the `('. */
15157 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15159 /* Peek at the next token. */
15160 token = cp_lexer_peek_token (parser->lexer);
15161 /* If it's not a `)', then there is a type-id-list. */
15162 if (token->type != CPP_CLOSE_PAREN)
15164 const char *saved_message;
15166 /* Types may not be defined in an exception-specification. */
15167 saved_message = parser->type_definition_forbidden_message;
15168 parser->type_definition_forbidden_message
15169 = "types may not be defined in an exception-specification";
15170 /* Parse the type-id-list. */
15171 type_id_list = cp_parser_type_id_list (parser);
15172 /* Restore the saved message. */
15173 parser->type_definition_forbidden_message = saved_message;
15176 type_id_list = empty_except_spec;
15178 /* Look for the `)'. */
15179 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15181 return type_id_list;
15184 /* Parse an (optional) type-id-list.
15188 type-id-list , type-id ... [opt]
15190 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15191 in the order that the types were presented. */
15194 cp_parser_type_id_list (cp_parser* parser)
15196 tree types = NULL_TREE;
15203 /* Get the next type-id. */
15204 type = cp_parser_type_id (parser);
15205 /* Parse the optional ellipsis. */
15206 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15208 /* Consume the `...'. */
15209 cp_lexer_consume_token (parser->lexer);
15211 /* Turn the type into a pack expansion expression. */
15212 type = make_pack_expansion (type);
15214 /* Add it to the list. */
15215 types = add_exception_specifier (types, type, /*complain=*/1);
15216 /* Peek at the next token. */
15217 token = cp_lexer_peek_token (parser->lexer);
15218 /* If it is not a `,', we are done. */
15219 if (token->type != CPP_COMMA)
15221 /* Consume the `,'. */
15222 cp_lexer_consume_token (parser->lexer);
15225 return nreverse (types);
15228 /* Parse a try-block.
15231 try compound-statement handler-seq */
15234 cp_parser_try_block (cp_parser* parser)
15238 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15239 try_block = begin_try_block ();
15240 cp_parser_compound_statement (parser, NULL, true);
15241 finish_try_block (try_block);
15242 cp_parser_handler_seq (parser);
15243 finish_handler_sequence (try_block);
15248 /* Parse a function-try-block.
15250 function-try-block:
15251 try ctor-initializer [opt] function-body handler-seq */
15254 cp_parser_function_try_block (cp_parser* parser)
15256 tree compound_stmt;
15258 bool ctor_initializer_p;
15260 /* Look for the `try' keyword. */
15261 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15263 /* Let the rest of the front end know where we are. */
15264 try_block = begin_function_try_block (&compound_stmt);
15265 /* Parse the function-body. */
15267 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15268 /* We're done with the `try' part. */
15269 finish_function_try_block (try_block);
15270 /* Parse the handlers. */
15271 cp_parser_handler_seq (parser);
15272 /* We're done with the handlers. */
15273 finish_function_handler_sequence (try_block, compound_stmt);
15275 return ctor_initializer_p;
15278 /* Parse a handler-seq.
15281 handler handler-seq [opt] */
15284 cp_parser_handler_seq (cp_parser* parser)
15290 /* Parse the handler. */
15291 cp_parser_handler (parser);
15292 /* Peek at the next token. */
15293 token = cp_lexer_peek_token (parser->lexer);
15294 /* If it's not `catch' then there are no more handlers. */
15295 if (!cp_parser_is_keyword (token, RID_CATCH))
15300 /* Parse a handler.
15303 catch ( exception-declaration ) compound-statement */
15306 cp_parser_handler (cp_parser* parser)
15311 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15312 handler = begin_handler ();
15313 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15314 declaration = cp_parser_exception_declaration (parser);
15315 finish_handler_parms (declaration, handler);
15316 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15317 cp_parser_compound_statement (parser, NULL, false);
15318 finish_handler (handler);
15321 /* Parse an exception-declaration.
15323 exception-declaration:
15324 type-specifier-seq declarator
15325 type-specifier-seq abstract-declarator
15329 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15330 ellipsis variant is used. */
15333 cp_parser_exception_declaration (cp_parser* parser)
15335 cp_decl_specifier_seq type_specifiers;
15336 cp_declarator *declarator;
15337 const char *saved_message;
15339 /* If it's an ellipsis, it's easy to handle. */
15340 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15342 /* Consume the `...' token. */
15343 cp_lexer_consume_token (parser->lexer);
15347 /* Types may not be defined in exception-declarations. */
15348 saved_message = parser->type_definition_forbidden_message;
15349 parser->type_definition_forbidden_message
15350 = "types may not be defined in exception-declarations";
15352 /* Parse the type-specifier-seq. */
15353 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15355 /* If it's a `)', then there is no declarator. */
15356 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15359 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15360 /*ctor_dtor_or_conv_p=*/NULL,
15361 /*parenthesized_p=*/NULL,
15362 /*member_p=*/false);
15364 /* Restore the saved message. */
15365 parser->type_definition_forbidden_message = saved_message;
15367 if (!type_specifiers.any_specifiers_p)
15368 return error_mark_node;
15370 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15373 /* Parse a throw-expression.
15376 throw assignment-expression [opt]
15378 Returns a THROW_EXPR representing the throw-expression. */
15381 cp_parser_throw_expression (cp_parser* parser)
15386 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15387 token = cp_lexer_peek_token (parser->lexer);
15388 /* Figure out whether or not there is an assignment-expression
15389 following the "throw" keyword. */
15390 if (token->type == CPP_COMMA
15391 || token->type == CPP_SEMICOLON
15392 || token->type == CPP_CLOSE_PAREN
15393 || token->type == CPP_CLOSE_SQUARE
15394 || token->type == CPP_CLOSE_BRACE
15395 || token->type == CPP_COLON)
15396 expression = NULL_TREE;
15398 expression = cp_parser_assignment_expression (parser,
15401 return build_throw (expression);
15404 /* GNU Extensions */
15406 /* Parse an (optional) asm-specification.
15409 asm ( string-literal )
15411 If the asm-specification is present, returns a STRING_CST
15412 corresponding to the string-literal. Otherwise, returns
15416 cp_parser_asm_specification_opt (cp_parser* parser)
15419 tree asm_specification;
15421 /* Peek at the next token. */
15422 token = cp_lexer_peek_token (parser->lexer);
15423 /* If the next token isn't the `asm' keyword, then there's no
15424 asm-specification. */
15425 if (!cp_parser_is_keyword (token, RID_ASM))
15428 /* Consume the `asm' token. */
15429 cp_lexer_consume_token (parser->lexer);
15430 /* Look for the `('. */
15431 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15433 /* Look for the string-literal. */
15434 asm_specification = cp_parser_string_literal (parser, false, false);
15436 /* Look for the `)'. */
15437 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15439 return asm_specification;
15442 /* Parse an asm-operand-list.
15446 asm-operand-list , asm-operand
15449 string-literal ( expression )
15450 [ string-literal ] string-literal ( expression )
15452 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15453 each node is the expression. The TREE_PURPOSE is itself a
15454 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15455 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15456 is a STRING_CST for the string literal before the parenthesis. */
15459 cp_parser_asm_operand_list (cp_parser* parser)
15461 tree asm_operands = NULL_TREE;
15465 tree string_literal;
15469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15471 /* Consume the `[' token. */
15472 cp_lexer_consume_token (parser->lexer);
15473 /* Read the operand name. */
15474 name = cp_parser_identifier (parser);
15475 if (name != error_mark_node)
15476 name = build_string (IDENTIFIER_LENGTH (name),
15477 IDENTIFIER_POINTER (name));
15478 /* Look for the closing `]'. */
15479 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15483 /* Look for the string-literal. */
15484 string_literal = cp_parser_string_literal (parser, false, false);
15486 /* Look for the `('. */
15487 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15488 /* Parse the expression. */
15489 expression = cp_parser_expression (parser, /*cast_p=*/false);
15490 /* Look for the `)'. */
15491 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15493 /* Add this operand to the list. */
15494 asm_operands = tree_cons (build_tree_list (name, string_literal),
15497 /* If the next token is not a `,', there are no more
15499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15501 /* Consume the `,'. */
15502 cp_lexer_consume_token (parser->lexer);
15505 return nreverse (asm_operands);
15508 /* Parse an asm-clobber-list.
15512 asm-clobber-list , string-literal
15514 Returns a TREE_LIST, indicating the clobbers in the order that they
15515 appeared. The TREE_VALUE of each node is a STRING_CST. */
15518 cp_parser_asm_clobber_list (cp_parser* parser)
15520 tree clobbers = NULL_TREE;
15524 tree string_literal;
15526 /* Look for the string literal. */
15527 string_literal = cp_parser_string_literal (parser, false, false);
15528 /* Add it to the list. */
15529 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15530 /* If the next token is not a `,', then the list is
15532 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15534 /* Consume the `,' token. */
15535 cp_lexer_consume_token (parser->lexer);
15541 /* Parse an (optional) series of attributes.
15544 attributes attribute
15547 __attribute__ (( attribute-list [opt] ))
15549 The return value is as for cp_parser_attribute_list. */
15552 cp_parser_attributes_opt (cp_parser* parser)
15554 tree attributes = NULL_TREE;
15559 tree attribute_list;
15561 /* Peek at the next token. */
15562 token = cp_lexer_peek_token (parser->lexer);
15563 /* If it's not `__attribute__', then we're done. */
15564 if (token->keyword != RID_ATTRIBUTE)
15567 /* Consume the `__attribute__' keyword. */
15568 cp_lexer_consume_token (parser->lexer);
15569 /* Look for the two `(' tokens. */
15570 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15571 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15573 /* Peek at the next token. */
15574 token = cp_lexer_peek_token (parser->lexer);
15575 if (token->type != CPP_CLOSE_PAREN)
15576 /* Parse the attribute-list. */
15577 attribute_list = cp_parser_attribute_list (parser);
15579 /* If the next token is a `)', then there is no attribute
15581 attribute_list = NULL;
15583 /* Look for the two `)' tokens. */
15584 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15585 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15587 /* Add these new attributes to the list. */
15588 attributes = chainon (attributes, attribute_list);
15594 /* Parse an attribute-list.
15598 attribute-list , attribute
15602 identifier ( identifier )
15603 identifier ( identifier , expression-list )
15604 identifier ( expression-list )
15606 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15607 to an attribute. The TREE_PURPOSE of each node is the identifier
15608 indicating which attribute is in use. The TREE_VALUE represents
15609 the arguments, if any. */
15612 cp_parser_attribute_list (cp_parser* parser)
15614 tree attribute_list = NULL_TREE;
15615 bool save_translate_strings_p = parser->translate_strings_p;
15617 parser->translate_strings_p = false;
15624 /* Look for the identifier. We also allow keywords here; for
15625 example `__attribute__ ((const))' is legal. */
15626 token = cp_lexer_peek_token (parser->lexer);
15627 if (token->type == CPP_NAME
15628 || token->type == CPP_KEYWORD)
15630 tree arguments = NULL_TREE;
15632 /* Consume the token. */
15633 token = cp_lexer_consume_token (parser->lexer);
15635 /* Save away the identifier that indicates which attribute
15637 identifier = token->u.value;
15638 attribute = build_tree_list (identifier, NULL_TREE);
15640 /* Peek at the next token. */
15641 token = cp_lexer_peek_token (parser->lexer);
15642 /* If it's an `(', then parse the attribute arguments. */
15643 if (token->type == CPP_OPEN_PAREN)
15645 arguments = cp_parser_parenthesized_expression_list
15646 (parser, true, /*cast_p=*/false,
15647 /*allow_expansion_p=*/false,
15648 /*non_constant_p=*/NULL);
15649 /* Save the arguments away. */
15650 TREE_VALUE (attribute) = arguments;
15653 if (arguments != error_mark_node)
15655 /* Add this attribute to the list. */
15656 TREE_CHAIN (attribute) = attribute_list;
15657 attribute_list = attribute;
15660 token = cp_lexer_peek_token (parser->lexer);
15662 /* Now, look for more attributes. If the next token isn't a
15663 `,', we're done. */
15664 if (token->type != CPP_COMMA)
15667 /* Consume the comma and keep going. */
15668 cp_lexer_consume_token (parser->lexer);
15670 parser->translate_strings_p = save_translate_strings_p;
15672 /* We built up the list in reverse order. */
15673 return nreverse (attribute_list);
15676 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15677 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15678 current value of the PEDANTIC flag, regardless of whether or not
15679 the `__extension__' keyword is present. The caller is responsible
15680 for restoring the value of the PEDANTIC flag. */
15683 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15685 /* Save the old value of the PEDANTIC flag. */
15686 *saved_pedantic = pedantic;
15688 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15690 /* Consume the `__extension__' token. */
15691 cp_lexer_consume_token (parser->lexer);
15692 /* We're not being pedantic while the `__extension__' keyword is
15702 /* Parse a label declaration.
15705 __label__ label-declarator-seq ;
15707 label-declarator-seq:
15708 identifier , label-declarator-seq
15712 cp_parser_label_declaration (cp_parser* parser)
15714 /* Look for the `__label__' keyword. */
15715 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15721 /* Look for an identifier. */
15722 identifier = cp_parser_identifier (parser);
15723 /* If we failed, stop. */
15724 if (identifier == error_mark_node)
15726 /* Declare it as a label. */
15727 finish_label_decl (identifier);
15728 /* If the next token is a `;', stop. */
15729 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15731 /* Look for the `,' separating the label declarations. */
15732 cp_parser_require (parser, CPP_COMMA, "`,'");
15735 /* Look for the final `;'. */
15736 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15739 /* Support Functions */
15741 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15742 NAME should have one of the representations used for an
15743 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15744 is returned. If PARSER->SCOPE is a dependent type, then a
15745 SCOPE_REF is returned.
15747 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15748 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15749 was formed. Abstractly, such entities should not be passed to this
15750 function, because they do not need to be looked up, but it is
15751 simpler to check for this special case here, rather than at the
15754 In cases not explicitly covered above, this function returns a
15755 DECL, OVERLOAD, or baselink representing the result of the lookup.
15756 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15759 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15760 (e.g., "struct") that was used. In that case bindings that do not
15761 refer to types are ignored.
15763 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15766 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15769 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15772 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15773 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15774 NULL_TREE otherwise. */
15777 cp_parser_lookup_name (cp_parser *parser, tree name,
15778 enum tag_types tag_type,
15781 bool check_dependency,
15782 tree *ambiguous_decls)
15786 tree object_type = parser->context->object_type;
15788 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15789 flags |= LOOKUP_COMPLAIN;
15791 /* Assume that the lookup will be unambiguous. */
15792 if (ambiguous_decls)
15793 *ambiguous_decls = NULL_TREE;
15795 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15796 no longer valid. Note that if we are parsing tentatively, and
15797 the parse fails, OBJECT_TYPE will be automatically restored. */
15798 parser->context->object_type = NULL_TREE;
15800 if (name == error_mark_node)
15801 return error_mark_node;
15803 /* A template-id has already been resolved; there is no lookup to
15805 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15807 if (BASELINK_P (name))
15809 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15810 == TEMPLATE_ID_EXPR);
15814 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15815 it should already have been checked to make sure that the name
15816 used matches the type being destroyed. */
15817 if (TREE_CODE (name) == BIT_NOT_EXPR)
15821 /* Figure out to which type this destructor applies. */
15823 type = parser->scope;
15824 else if (object_type)
15825 type = object_type;
15827 type = current_class_type;
15828 /* If that's not a class type, there is no destructor. */
15829 if (!type || !CLASS_TYPE_P (type))
15830 return error_mark_node;
15831 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15832 lazily_declare_fn (sfk_destructor, type);
15833 if (!CLASSTYPE_DESTRUCTORS (type))
15834 return error_mark_node;
15835 /* If it was a class type, return the destructor. */
15836 return CLASSTYPE_DESTRUCTORS (type);
15839 /* By this point, the NAME should be an ordinary identifier. If
15840 the id-expression was a qualified name, the qualifying scope is
15841 stored in PARSER->SCOPE at this point. */
15842 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15844 /* Perform the lookup. */
15849 if (parser->scope == error_mark_node)
15850 return error_mark_node;
15852 /* If the SCOPE is dependent, the lookup must be deferred until
15853 the template is instantiated -- unless we are explicitly
15854 looking up names in uninstantiated templates. Even then, we
15855 cannot look up the name if the scope is not a class type; it
15856 might, for example, be a template type parameter. */
15857 dependent_p = (TYPE_P (parser->scope)
15858 && !(parser->in_declarator_p
15859 && currently_open_class (parser->scope))
15860 && dependent_type_p (parser->scope));
15861 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15868 /* The resolution to Core Issue 180 says that `struct
15869 A::B' should be considered a type-name, even if `A'
15871 type = make_typename_type (parser->scope, name, tag_type,
15872 /*complain=*/tf_error);
15873 decl = TYPE_NAME (type);
15875 else if (is_template
15876 && (cp_parser_next_token_ends_template_argument_p (parser)
15877 || cp_lexer_next_token_is (parser->lexer,
15879 decl = make_unbound_class_template (parser->scope,
15881 /*complain=*/tf_error);
15883 decl = build_qualified_name (/*type=*/NULL_TREE,
15884 parser->scope, name,
15889 tree pushed_scope = NULL_TREE;
15891 /* If PARSER->SCOPE is a dependent type, then it must be a
15892 class type, and we must not be checking dependencies;
15893 otherwise, we would have processed this lookup above. So
15894 that PARSER->SCOPE is not considered a dependent base by
15895 lookup_member, we must enter the scope here. */
15897 pushed_scope = push_scope (parser->scope);
15898 /* If the PARSER->SCOPE is a template specialization, it
15899 may be instantiated during name lookup. In that case,
15900 errors may be issued. Even if we rollback the current
15901 tentative parse, those errors are valid. */
15902 decl = lookup_qualified_name (parser->scope, name,
15903 tag_type != none_type,
15904 /*complain=*/true);
15906 pop_scope (pushed_scope);
15908 parser->qualifying_scope = parser->scope;
15909 parser->object_scope = NULL_TREE;
15911 else if (object_type)
15913 tree object_decl = NULL_TREE;
15914 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15915 OBJECT_TYPE is not a class. */
15916 if (CLASS_TYPE_P (object_type))
15917 /* If the OBJECT_TYPE is a template specialization, it may
15918 be instantiated during name lookup. In that case, errors
15919 may be issued. Even if we rollback the current tentative
15920 parse, those errors are valid. */
15921 object_decl = lookup_member (object_type,
15924 tag_type != none_type);
15925 /* Look it up in the enclosing context, too. */
15926 decl = lookup_name_real (name, tag_type != none_type,
15928 /*block_p=*/true, is_namespace, flags);
15929 parser->object_scope = object_type;
15930 parser->qualifying_scope = NULL_TREE;
15932 decl = object_decl;
15936 decl = lookup_name_real (name, tag_type != none_type,
15938 /*block_p=*/true, is_namespace, flags);
15939 parser->qualifying_scope = NULL_TREE;
15940 parser->object_scope = NULL_TREE;
15943 /* If the lookup failed, let our caller know. */
15944 if (!decl || decl == error_mark_node)
15945 return error_mark_node;
15947 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15948 if (TREE_CODE (decl) == TREE_LIST)
15950 if (ambiguous_decls)
15951 *ambiguous_decls = decl;
15952 /* The error message we have to print is too complicated for
15953 cp_parser_error, so we incorporate its actions directly. */
15954 if (!cp_parser_simulate_error (parser))
15956 error ("reference to %qD is ambiguous", name);
15957 print_candidates (decl);
15959 return error_mark_node;
15962 gcc_assert (DECL_P (decl)
15963 || TREE_CODE (decl) == OVERLOAD
15964 || TREE_CODE (decl) == SCOPE_REF
15965 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15966 || BASELINK_P (decl));
15968 /* If we have resolved the name of a member declaration, check to
15969 see if the declaration is accessible. When the name resolves to
15970 set of overloaded functions, accessibility is checked when
15971 overload resolution is done.
15973 During an explicit instantiation, access is not checked at all,
15974 as per [temp.explicit]. */
15976 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15981 /* Like cp_parser_lookup_name, but for use in the typical case where
15982 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15983 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15986 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15988 return cp_parser_lookup_name (parser, name,
15990 /*is_template=*/false,
15991 /*is_namespace=*/false,
15992 /*check_dependency=*/true,
15993 /*ambiguous_decls=*/NULL);
15996 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15997 the current context, return the TYPE_DECL. If TAG_NAME_P is
15998 true, the DECL indicates the class being defined in a class-head,
15999 or declared in an elaborated-type-specifier.
16001 Otherwise, return DECL. */
16004 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16006 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16007 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16010 template <typename T> struct B;
16013 template <typename T> struct A::B {};
16015 Similarly, in an elaborated-type-specifier:
16017 namespace N { struct X{}; }
16020 template <typename T> friend struct N::X;
16023 However, if the DECL refers to a class type, and we are in
16024 the scope of the class, then the name lookup automatically
16025 finds the TYPE_DECL created by build_self_reference rather
16026 than a TEMPLATE_DECL. For example, in:
16028 template <class T> struct S {
16032 there is no need to handle such case. */
16034 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16035 return DECL_TEMPLATE_RESULT (decl);
16040 /* If too many, or too few, template-parameter lists apply to the
16041 declarator, issue an error message. Returns TRUE if all went well,
16042 and FALSE otherwise. */
16045 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16046 cp_declarator *declarator)
16048 unsigned num_templates;
16050 /* We haven't seen any classes that involve template parameters yet. */
16053 switch (declarator->kind)
16056 if (declarator->u.id.qualifying_scope)
16061 scope = declarator->u.id.qualifying_scope;
16062 member = declarator->u.id.unqualified_name;
16064 while (scope && CLASS_TYPE_P (scope))
16066 /* You're supposed to have one `template <...>'
16067 for every template class, but you don't need one
16068 for a full specialization. For example:
16070 template <class T> struct S{};
16071 template <> struct S<int> { void f(); };
16072 void S<int>::f () {}
16074 is correct; there shouldn't be a `template <>' for
16075 the definition of `S<int>::f'. */
16076 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16077 /* If SCOPE does not have template information of any
16078 kind, then it is not a template, nor is it nested
16079 within a template. */
16081 if (explicit_class_specialization_p (scope))
16083 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16086 scope = TYPE_CONTEXT (scope);
16089 else if (TREE_CODE (declarator->u.id.unqualified_name)
16090 == TEMPLATE_ID_EXPR)
16091 /* If the DECLARATOR has the form `X<y>' then it uses one
16092 additional level of template parameters. */
16095 return cp_parser_check_template_parameters (parser,
16101 case cdk_reference:
16103 return (cp_parser_check_declarator_template_parameters
16104 (parser, declarator->declarator));
16110 gcc_unreachable ();
16115 /* NUM_TEMPLATES were used in the current declaration. If that is
16116 invalid, return FALSE and issue an error messages. Otherwise,
16120 cp_parser_check_template_parameters (cp_parser* parser,
16121 unsigned num_templates)
16123 /* If there are more template classes than parameter lists, we have
16126 template <class T> void S<T>::R<T>::f (); */
16127 if (parser->num_template_parameter_lists < num_templates)
16129 error ("too few template-parameter-lists");
16132 /* If there are the same number of template classes and parameter
16133 lists, that's OK. */
16134 if (parser->num_template_parameter_lists == num_templates)
16136 /* If there are more, but only one more, then we are referring to a
16137 member template. That's OK too. */
16138 if (parser->num_template_parameter_lists == num_templates + 1)
16140 /* Otherwise, there are too many template parameter lists. We have
16143 template <class T> template <class U> void S::f(); */
16144 error ("too many template-parameter-lists");
16148 /* Parse an optional `::' token indicating that the following name is
16149 from the global namespace. If so, PARSER->SCOPE is set to the
16150 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16151 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16152 Returns the new value of PARSER->SCOPE, if the `::' token is
16153 present, and NULL_TREE otherwise. */
16156 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16160 /* Peek at the next token. */
16161 token = cp_lexer_peek_token (parser->lexer);
16162 /* If we're looking at a `::' token then we're starting from the
16163 global namespace, not our current location. */
16164 if (token->type == CPP_SCOPE)
16166 /* Consume the `::' token. */
16167 cp_lexer_consume_token (parser->lexer);
16168 /* Set the SCOPE so that we know where to start the lookup. */
16169 parser->scope = global_namespace;
16170 parser->qualifying_scope = global_namespace;
16171 parser->object_scope = NULL_TREE;
16173 return parser->scope;
16175 else if (!current_scope_valid_p)
16177 parser->scope = NULL_TREE;
16178 parser->qualifying_scope = NULL_TREE;
16179 parser->object_scope = NULL_TREE;
16185 /* Returns TRUE if the upcoming token sequence is the start of a
16186 constructor declarator. If FRIEND_P is true, the declarator is
16187 preceded by the `friend' specifier. */
16190 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16192 bool constructor_p;
16193 tree type_decl = NULL_TREE;
16194 bool nested_name_p;
16195 cp_token *next_token;
16197 /* The common case is that this is not a constructor declarator, so
16198 try to avoid doing lots of work if at all possible. It's not
16199 valid declare a constructor at function scope. */
16200 if (parser->in_function_body)
16202 /* And only certain tokens can begin a constructor declarator. */
16203 next_token = cp_lexer_peek_token (parser->lexer);
16204 if (next_token->type != CPP_NAME
16205 && next_token->type != CPP_SCOPE
16206 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16207 && next_token->type != CPP_TEMPLATE_ID)
16210 /* Parse tentatively; we are going to roll back all of the tokens
16212 cp_parser_parse_tentatively (parser);
16213 /* Assume that we are looking at a constructor declarator. */
16214 constructor_p = true;
16216 /* Look for the optional `::' operator. */
16217 cp_parser_global_scope_opt (parser,
16218 /*current_scope_valid_p=*/false);
16219 /* Look for the nested-name-specifier. */
16221 = (cp_parser_nested_name_specifier_opt (parser,
16222 /*typename_keyword_p=*/false,
16223 /*check_dependency_p=*/false,
16225 /*is_declaration=*/false)
16227 /* Outside of a class-specifier, there must be a
16228 nested-name-specifier. */
16229 if (!nested_name_p &&
16230 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16232 constructor_p = false;
16233 /* If we still think that this might be a constructor-declarator,
16234 look for a class-name. */
16239 template <typename T> struct S { S(); };
16240 template <typename T> S<T>::S ();
16242 we must recognize that the nested `S' names a class.
16245 template <typename T> S<T>::S<T> ();
16247 we must recognize that the nested `S' names a template. */
16248 type_decl = cp_parser_class_name (parser,
16249 /*typename_keyword_p=*/false,
16250 /*template_keyword_p=*/false,
16252 /*check_dependency_p=*/false,
16253 /*class_head_p=*/false,
16254 /*is_declaration=*/false);
16255 /* If there was no class-name, then this is not a constructor. */
16256 constructor_p = !cp_parser_error_occurred (parser);
16259 /* If we're still considering a constructor, we have to see a `(',
16260 to begin the parameter-declaration-clause, followed by either a
16261 `)', an `...', or a decl-specifier. We need to check for a
16262 type-specifier to avoid being fooled into thinking that:
16266 is a constructor. (It is actually a function named `f' that
16267 takes one parameter (of type `int') and returns a value of type
16270 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16272 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16273 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16274 /* A parameter declaration begins with a decl-specifier,
16275 which is either the "attribute" keyword, a storage class
16276 specifier, or (usually) a type-specifier. */
16277 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16280 tree pushed_scope = NULL_TREE;
16281 unsigned saved_num_template_parameter_lists;
16283 /* Names appearing in the type-specifier should be looked up
16284 in the scope of the class. */
16285 if (current_class_type)
16289 type = TREE_TYPE (type_decl);
16290 if (TREE_CODE (type) == TYPENAME_TYPE)
16292 type = resolve_typename_type (type,
16293 /*only_current_p=*/false);
16294 if (TREE_CODE (type) == TYPENAME_TYPE)
16296 cp_parser_abort_tentative_parse (parser);
16300 pushed_scope = push_scope (type);
16303 /* Inside the constructor parameter list, surrounding
16304 template-parameter-lists do not apply. */
16305 saved_num_template_parameter_lists
16306 = parser->num_template_parameter_lists;
16307 parser->num_template_parameter_lists = 0;
16309 /* Look for the type-specifier. */
16310 cp_parser_type_specifier (parser,
16311 CP_PARSER_FLAGS_NONE,
16312 /*decl_specs=*/NULL,
16313 /*is_declarator=*/true,
16314 /*declares_class_or_enum=*/NULL,
16315 /*is_cv_qualifier=*/NULL);
16317 parser->num_template_parameter_lists
16318 = saved_num_template_parameter_lists;
16320 /* Leave the scope of the class. */
16322 pop_scope (pushed_scope);
16324 constructor_p = !cp_parser_error_occurred (parser);
16328 constructor_p = false;
16329 /* We did not really want to consume any tokens. */
16330 cp_parser_abort_tentative_parse (parser);
16332 return constructor_p;
16335 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16336 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16337 they must be performed once we are in the scope of the function.
16339 Returns the function defined. */
16342 cp_parser_function_definition_from_specifiers_and_declarator
16343 (cp_parser* parser,
16344 cp_decl_specifier_seq *decl_specifiers,
16346 const cp_declarator *declarator)
16351 /* Begin the function-definition. */
16352 success_p = start_function (decl_specifiers, declarator, attributes);
16354 /* The things we're about to see are not directly qualified by any
16355 template headers we've seen thus far. */
16356 reset_specialization ();
16358 /* If there were names looked up in the decl-specifier-seq that we
16359 did not check, check them now. We must wait until we are in the
16360 scope of the function to perform the checks, since the function
16361 might be a friend. */
16362 perform_deferred_access_checks ();
16366 /* Skip the entire function. */
16367 cp_parser_skip_to_end_of_block_or_statement (parser);
16368 fn = error_mark_node;
16370 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16372 /* Seen already, skip it. An error message has already been output. */
16373 cp_parser_skip_to_end_of_block_or_statement (parser);
16374 fn = current_function_decl;
16375 current_function_decl = NULL_TREE;
16376 /* If this is a function from a class, pop the nested class. */
16377 if (current_class_name)
16378 pop_nested_class ();
16381 fn = cp_parser_function_definition_after_declarator (parser,
16382 /*inline_p=*/false);
16387 /* Parse the part of a function-definition that follows the
16388 declarator. INLINE_P is TRUE iff this function is an inline
16389 function defined with a class-specifier.
16391 Returns the function defined. */
16394 cp_parser_function_definition_after_declarator (cp_parser* parser,
16398 bool ctor_initializer_p = false;
16399 bool saved_in_unbraced_linkage_specification_p;
16400 bool saved_in_function_body;
16401 unsigned saved_num_template_parameter_lists;
16403 saved_in_function_body = parser->in_function_body;
16404 parser->in_function_body = true;
16405 /* If the next token is `return', then the code may be trying to
16406 make use of the "named return value" extension that G++ used to
16408 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16410 /* Consume the `return' keyword. */
16411 cp_lexer_consume_token (parser->lexer);
16412 /* Look for the identifier that indicates what value is to be
16414 cp_parser_identifier (parser);
16415 /* Issue an error message. */
16416 error ("named return values are no longer supported");
16417 /* Skip tokens until we reach the start of the function body. */
16420 cp_token *token = cp_lexer_peek_token (parser->lexer);
16421 if (token->type == CPP_OPEN_BRACE
16422 || token->type == CPP_EOF
16423 || token->type == CPP_PRAGMA_EOL)
16425 cp_lexer_consume_token (parser->lexer);
16428 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16429 anything declared inside `f'. */
16430 saved_in_unbraced_linkage_specification_p
16431 = parser->in_unbraced_linkage_specification_p;
16432 parser->in_unbraced_linkage_specification_p = false;
16433 /* Inside the function, surrounding template-parameter-lists do not
16435 saved_num_template_parameter_lists
16436 = parser->num_template_parameter_lists;
16437 parser->num_template_parameter_lists = 0;
16438 /* If the next token is `try', then we are looking at a
16439 function-try-block. */
16440 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16441 ctor_initializer_p = cp_parser_function_try_block (parser);
16442 /* A function-try-block includes the function-body, so we only do
16443 this next part if we're not processing a function-try-block. */
16446 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16448 /* Finish the function. */
16449 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16450 (inline_p ? 2 : 0));
16451 /* Generate code for it, if necessary. */
16452 expand_or_defer_fn (fn);
16453 /* Restore the saved values. */
16454 parser->in_unbraced_linkage_specification_p
16455 = saved_in_unbraced_linkage_specification_p;
16456 parser->num_template_parameter_lists
16457 = saved_num_template_parameter_lists;
16458 parser->in_function_body = saved_in_function_body;
16463 /* Parse a template-declaration, assuming that the `export' (and
16464 `extern') keywords, if present, has already been scanned. MEMBER_P
16465 is as for cp_parser_template_declaration. */
16468 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16470 tree decl = NULL_TREE;
16471 VEC (deferred_access_check,gc) *checks;
16472 tree parameter_list;
16473 bool friend_p = false;
16474 bool need_lang_pop;
16476 /* Look for the `template' keyword. */
16477 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16481 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16483 if (at_class_scope_p () && current_function_decl)
16485 /* 14.5.2.2 [temp.mem]
16487 A local class shall not have member templates. */
16488 error ("invalid declaration of member template in local class");
16489 cp_parser_skip_to_end_of_block_or_statement (parser);
16494 A template ... shall not have C linkage. */
16495 if (current_lang_name == lang_name_c)
16497 error ("template with C linkage");
16498 /* Give it C++ linkage to avoid confusing other parts of the
16500 push_lang_context (lang_name_cplusplus);
16501 need_lang_pop = true;
16504 need_lang_pop = false;
16506 /* We cannot perform access checks on the template parameter
16507 declarations until we know what is being declared, just as we
16508 cannot check the decl-specifier list. */
16509 push_deferring_access_checks (dk_deferred);
16511 /* If the next token is `>', then we have an invalid
16512 specialization. Rather than complain about an invalid template
16513 parameter, issue an error message here. */
16514 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16516 cp_parser_error (parser, "invalid explicit specialization");
16517 begin_specialization ();
16518 parameter_list = NULL_TREE;
16521 /* Parse the template parameters. */
16522 parameter_list = cp_parser_template_parameter_list (parser);
16524 /* Get the deferred access checks from the parameter list. These
16525 will be checked once we know what is being declared, as for a
16526 member template the checks must be performed in the scope of the
16527 class containing the member. */
16528 checks = get_deferred_access_checks ();
16530 /* Look for the `>'. */
16531 cp_parser_skip_to_end_of_template_parameter_list (parser);
16532 /* We just processed one more parameter list. */
16533 ++parser->num_template_parameter_lists;
16534 /* If the next token is `template', there are more template
16536 if (cp_lexer_next_token_is_keyword (parser->lexer,
16538 cp_parser_template_declaration_after_export (parser, member_p);
16541 /* There are no access checks when parsing a template, as we do not
16542 know if a specialization will be a friend. */
16543 push_deferring_access_checks (dk_no_check);
16544 decl = cp_parser_single_declaration (parser,
16547 /*explicit_specialization_p=*/false,
16549 pop_deferring_access_checks ();
16551 /* If this is a member template declaration, let the front
16553 if (member_p && !friend_p && decl)
16555 if (TREE_CODE (decl) == TYPE_DECL)
16556 cp_parser_check_access_in_redeclaration (decl);
16558 decl = finish_member_template_decl (decl);
16560 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16561 make_friend_class (current_class_type, TREE_TYPE (decl),
16562 /*complain=*/true);
16564 /* We are done with the current parameter list. */
16565 --parser->num_template_parameter_lists;
16567 pop_deferring_access_checks ();
16570 finish_template_decl (parameter_list);
16572 /* Register member declarations. */
16573 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16574 finish_member_declaration (decl);
16575 /* For the erroneous case of a template with C linkage, we pushed an
16576 implicit C++ linkage scope; exit that scope now. */
16578 pop_lang_context ();
16579 /* If DECL is a function template, we must return to parse it later.
16580 (Even though there is no definition, there might be default
16581 arguments that need handling.) */
16582 if (member_p && decl
16583 && (TREE_CODE (decl) == FUNCTION_DECL
16584 || DECL_FUNCTION_TEMPLATE_P (decl)))
16585 TREE_VALUE (parser->unparsed_functions_queues)
16586 = tree_cons (NULL_TREE, decl,
16587 TREE_VALUE (parser->unparsed_functions_queues));
16590 /* Perform the deferred access checks from a template-parameter-list.
16591 CHECKS is a TREE_LIST of access checks, as returned by
16592 get_deferred_access_checks. */
16595 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16597 ++processing_template_parmlist;
16598 perform_access_checks (checks);
16599 --processing_template_parmlist;
16602 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16603 `function-definition' sequence. MEMBER_P is true, this declaration
16604 appears in a class scope.
16606 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16607 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16610 cp_parser_single_declaration (cp_parser* parser,
16611 VEC (deferred_access_check,gc)* checks,
16613 bool explicit_specialization_p,
16616 int declares_class_or_enum;
16617 tree decl = NULL_TREE;
16618 cp_decl_specifier_seq decl_specifiers;
16619 bool function_definition_p = false;
16621 /* This function is only used when processing a template
16623 gcc_assert (innermost_scope_kind () == sk_template_parms
16624 || innermost_scope_kind () == sk_template_spec);
16626 /* Defer access checks until we know what is being declared. */
16627 push_deferring_access_checks (dk_deferred);
16629 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16631 cp_parser_decl_specifier_seq (parser,
16632 CP_PARSER_FLAGS_OPTIONAL,
16634 &declares_class_or_enum);
16636 *friend_p = cp_parser_friend_p (&decl_specifiers);
16638 /* There are no template typedefs. */
16639 if (decl_specifiers.specs[(int) ds_typedef])
16641 error ("template declaration of %qs", "typedef");
16642 decl = error_mark_node;
16645 /* Gather up the access checks that occurred the
16646 decl-specifier-seq. */
16647 stop_deferring_access_checks ();
16649 /* Check for the declaration of a template class. */
16650 if (declares_class_or_enum)
16652 if (cp_parser_declares_only_class_p (parser))
16654 decl = shadow_tag (&decl_specifiers);
16659 friend template <typename T> struct A<T>::B;
16662 A<T>::B will be represented by a TYPENAME_TYPE, and
16663 therefore not recognized by shadow_tag. */
16664 if (friend_p && *friend_p
16666 && decl_specifiers.type
16667 && TYPE_P (decl_specifiers.type))
16668 decl = decl_specifiers.type;
16670 if (decl && decl != error_mark_node)
16671 decl = TYPE_NAME (decl);
16673 decl = error_mark_node;
16675 /* Perform access checks for template parameters. */
16676 cp_parser_perform_template_parameter_access_checks (checks);
16679 /* If it's not a template class, try for a template function. If
16680 the next token is a `;', then this declaration does not declare
16681 anything. But, if there were errors in the decl-specifiers, then
16682 the error might well have come from an attempted class-specifier.
16683 In that case, there's no need to warn about a missing declarator. */
16685 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16686 || decl_specifiers.type != error_mark_node))
16688 decl = cp_parser_init_declarator (parser,
16691 /*function_definition_allowed_p=*/true,
16693 declares_class_or_enum,
16694 &function_definition_p);
16696 /* 7.1.1-1 [dcl.stc]
16698 A storage-class-specifier shall not be specified in an explicit
16699 specialization... */
16701 && explicit_specialization_p
16702 && decl_specifiers.storage_class != sc_none)
16704 error ("explicit template specialization cannot have a storage class");
16705 decl = error_mark_node;
16709 pop_deferring_access_checks ();
16711 /* Clear any current qualification; whatever comes next is the start
16712 of something new. */
16713 parser->scope = NULL_TREE;
16714 parser->qualifying_scope = NULL_TREE;
16715 parser->object_scope = NULL_TREE;
16716 /* Look for a trailing `;' after the declaration. */
16717 if (!function_definition_p
16718 && (decl == error_mark_node
16719 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16720 cp_parser_skip_to_end_of_block_or_statement (parser);
16725 /* Parse a cast-expression that is not the operand of a unary "&". */
16728 cp_parser_simple_cast_expression (cp_parser *parser)
16730 return cp_parser_cast_expression (parser, /*address_p=*/false,
16734 /* Parse a functional cast to TYPE. Returns an expression
16735 representing the cast. */
16738 cp_parser_functional_cast (cp_parser* parser, tree type)
16740 tree expression_list;
16744 = cp_parser_parenthesized_expression_list (parser, false,
16746 /*allow_expansion_p=*/true,
16747 /*non_constant_p=*/NULL);
16749 cast = build_functional_cast (type, expression_list);
16750 /* [expr.const]/1: In an integral constant expression "only type
16751 conversions to integral or enumeration type can be used". */
16752 if (TREE_CODE (type) == TYPE_DECL)
16753 type = TREE_TYPE (type);
16754 if (cast != error_mark_node
16755 && !cast_valid_in_integral_constant_expression_p (type)
16756 && (cp_parser_non_integral_constant_expression
16757 (parser, "a call to a constructor")))
16758 return error_mark_node;
16762 /* Save the tokens that make up the body of a member function defined
16763 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16764 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16765 specifiers applied to the declaration. Returns the FUNCTION_DECL
16766 for the member function. */
16769 cp_parser_save_member_function_body (cp_parser* parser,
16770 cp_decl_specifier_seq *decl_specifiers,
16771 cp_declarator *declarator,
16778 /* Create the function-declaration. */
16779 fn = start_method (decl_specifiers, declarator, attributes);
16780 /* If something went badly wrong, bail out now. */
16781 if (fn == error_mark_node)
16783 /* If there's a function-body, skip it. */
16784 if (cp_parser_token_starts_function_definition_p
16785 (cp_lexer_peek_token (parser->lexer)))
16786 cp_parser_skip_to_end_of_block_or_statement (parser);
16787 return error_mark_node;
16790 /* Remember it, if there default args to post process. */
16791 cp_parser_save_default_args (parser, fn);
16793 /* Save away the tokens that make up the body of the
16795 first = parser->lexer->next_token;
16796 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16797 /* Handle function try blocks. */
16798 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16799 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16800 last = parser->lexer->next_token;
16802 /* Save away the inline definition; we will process it when the
16803 class is complete. */
16804 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16805 DECL_PENDING_INLINE_P (fn) = 1;
16807 /* We need to know that this was defined in the class, so that
16808 friend templates are handled correctly. */
16809 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16811 /* We're done with the inline definition. */
16812 finish_method (fn);
16814 /* Add FN to the queue of functions to be parsed later. */
16815 TREE_VALUE (parser->unparsed_functions_queues)
16816 = tree_cons (NULL_TREE, fn,
16817 TREE_VALUE (parser->unparsed_functions_queues));
16822 /* Parse a template-argument-list, as well as the trailing ">" (but
16823 not the opening ">"). See cp_parser_template_argument_list for the
16827 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16831 tree saved_qualifying_scope;
16832 tree saved_object_scope;
16833 bool saved_greater_than_is_operator_p;
16834 bool saved_skip_evaluation;
16838 When parsing a template-id, the first non-nested `>' is taken as
16839 the end of the template-argument-list rather than a greater-than
16841 saved_greater_than_is_operator_p
16842 = parser->greater_than_is_operator_p;
16843 parser->greater_than_is_operator_p = false;
16844 /* Parsing the argument list may modify SCOPE, so we save it
16846 saved_scope = parser->scope;
16847 saved_qualifying_scope = parser->qualifying_scope;
16848 saved_object_scope = parser->object_scope;
16849 /* We need to evaluate the template arguments, even though this
16850 template-id may be nested within a "sizeof". */
16851 saved_skip_evaluation = skip_evaluation;
16852 skip_evaluation = false;
16853 /* Parse the template-argument-list itself. */
16854 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16855 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16856 arguments = NULL_TREE;
16858 arguments = cp_parser_template_argument_list (parser);
16859 /* Look for the `>' that ends the template-argument-list. If we find
16860 a '>>' instead, it's probably just a typo. */
16861 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16863 if (cxx_dialect != cxx98)
16865 /* In C++0x, a `>>' in a template argument list or cast
16866 expression is considered to be two separate `>'
16867 tokens. So, change the current token to a `>', but don't
16868 consume it: it will be consumed later when the outer
16869 template argument list (or cast expression) is parsed.
16870 Note that this replacement of `>' for `>>' is necessary
16871 even if we are parsing tentatively: in the tentative
16872 case, after calling
16873 cp_parser_enclosed_template_argument_list we will always
16874 throw away all of the template arguments and the first
16875 closing `>', either because the template argument list
16876 was erroneous or because we are replacing those tokens
16877 with a CPP_TEMPLATE_ID token. The second `>' (which will
16878 not have been thrown away) is needed either to close an
16879 outer template argument list or to complete a new-style
16881 cp_token *token = cp_lexer_peek_token (parser->lexer);
16882 token->type = CPP_GREATER;
16884 else if (!saved_greater_than_is_operator_p)
16886 /* If we're in a nested template argument list, the '>>' has
16887 to be a typo for '> >'. We emit the error message, but we
16888 continue parsing and we push a '>' as next token, so that
16889 the argument list will be parsed correctly. Note that the
16890 global source location is still on the token before the
16891 '>>', so we need to say explicitly where we want it. */
16892 cp_token *token = cp_lexer_peek_token (parser->lexer);
16893 error ("%H%<>>%> should be %<> >%> "
16894 "within a nested template argument list",
16897 token->type = CPP_GREATER;
16901 /* If this is not a nested template argument list, the '>>'
16902 is a typo for '>'. Emit an error message and continue.
16903 Same deal about the token location, but here we can get it
16904 right by consuming the '>>' before issuing the diagnostic. */
16905 cp_lexer_consume_token (parser->lexer);
16906 error ("spurious %<>>%>, use %<>%> to terminate "
16907 "a template argument list");
16911 cp_parser_skip_to_end_of_template_parameter_list (parser);
16912 /* The `>' token might be a greater-than operator again now. */
16913 parser->greater_than_is_operator_p
16914 = saved_greater_than_is_operator_p;
16915 /* Restore the SAVED_SCOPE. */
16916 parser->scope = saved_scope;
16917 parser->qualifying_scope = saved_qualifying_scope;
16918 parser->object_scope = saved_object_scope;
16919 skip_evaluation = saved_skip_evaluation;
16924 /* MEMBER_FUNCTION is a member function, or a friend. If default
16925 arguments, or the body of the function have not yet been parsed,
16929 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16931 /* If this member is a template, get the underlying
16933 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16934 member_function = DECL_TEMPLATE_RESULT (member_function);
16936 /* There should not be any class definitions in progress at this
16937 point; the bodies of members are only parsed outside of all class
16939 gcc_assert (parser->num_classes_being_defined == 0);
16940 /* While we're parsing the member functions we might encounter more
16941 classes. We want to handle them right away, but we don't want
16942 them getting mixed up with functions that are currently in the
16944 parser->unparsed_functions_queues
16945 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16947 /* Make sure that any template parameters are in scope. */
16948 maybe_begin_member_template_processing (member_function);
16950 /* If the body of the function has not yet been parsed, parse it
16952 if (DECL_PENDING_INLINE_P (member_function))
16954 tree function_scope;
16955 cp_token_cache *tokens;
16957 /* The function is no longer pending; we are processing it. */
16958 tokens = DECL_PENDING_INLINE_INFO (member_function);
16959 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16960 DECL_PENDING_INLINE_P (member_function) = 0;
16962 /* If this is a local class, enter the scope of the containing
16964 function_scope = current_function_decl;
16965 if (function_scope)
16966 push_function_context_to (function_scope);
16969 /* Push the body of the function onto the lexer stack. */
16970 cp_parser_push_lexer_for_tokens (parser, tokens);
16972 /* Let the front end know that we going to be defining this
16974 start_preparsed_function (member_function, NULL_TREE,
16975 SF_PRE_PARSED | SF_INCLASS_INLINE);
16977 /* Don't do access checking if it is a templated function. */
16978 if (processing_template_decl)
16979 push_deferring_access_checks (dk_no_check);
16981 /* Now, parse the body of the function. */
16982 cp_parser_function_definition_after_declarator (parser,
16983 /*inline_p=*/true);
16985 if (processing_template_decl)
16986 pop_deferring_access_checks ();
16988 /* Leave the scope of the containing function. */
16989 if (function_scope)
16990 pop_function_context_from (function_scope);
16991 cp_parser_pop_lexer (parser);
16994 /* Remove any template parameters from the symbol table. */
16995 maybe_end_member_template_processing ();
16997 /* Restore the queue. */
16998 parser->unparsed_functions_queues
16999 = TREE_CHAIN (parser->unparsed_functions_queues);
17002 /* If DECL contains any default args, remember it on the unparsed
17003 functions queue. */
17006 cp_parser_save_default_args (cp_parser* parser, tree decl)
17010 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17012 probe = TREE_CHAIN (probe))
17013 if (TREE_PURPOSE (probe))
17015 TREE_PURPOSE (parser->unparsed_functions_queues)
17016 = tree_cons (current_class_type, decl,
17017 TREE_PURPOSE (parser->unparsed_functions_queues));
17022 /* FN is a FUNCTION_DECL which may contains a parameter with an
17023 unparsed DEFAULT_ARG. Parse the default args now. This function
17024 assumes that the current scope is the scope in which the default
17025 argument should be processed. */
17028 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17030 bool saved_local_variables_forbidden_p;
17033 /* While we're parsing the default args, we might (due to the
17034 statement expression extension) encounter more classes. We want
17035 to handle them right away, but we don't want them getting mixed
17036 up with default args that are currently in the queue. */
17037 parser->unparsed_functions_queues
17038 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17040 /* Local variable names (and the `this' keyword) may not appear
17041 in a default argument. */
17042 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17043 parser->local_variables_forbidden_p = true;
17045 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17047 parm = TREE_CHAIN (parm))
17049 cp_token_cache *tokens;
17050 tree default_arg = TREE_PURPOSE (parm);
17052 VEC(tree,gc) *insts;
17059 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17060 /* This can happen for a friend declaration for a function
17061 already declared with default arguments. */
17064 /* Push the saved tokens for the default argument onto the parser's
17066 tokens = DEFARG_TOKENS (default_arg);
17067 cp_parser_push_lexer_for_tokens (parser, tokens);
17069 /* Parse the assignment-expression. */
17070 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17072 if (!processing_template_decl)
17073 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17075 TREE_PURPOSE (parm) = parsed_arg;
17077 /* Update any instantiations we've already created. */
17078 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17079 VEC_iterate (tree, insts, ix, copy); ix++)
17080 TREE_PURPOSE (copy) = parsed_arg;
17082 /* If the token stream has not been completely used up, then
17083 there was extra junk after the end of the default
17085 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17086 cp_parser_error (parser, "expected %<,%>");
17088 /* Revert to the main lexer. */
17089 cp_parser_pop_lexer (parser);
17092 /* Make sure no default arg is missing. */
17093 check_default_args (fn);
17095 /* Restore the state of local_variables_forbidden_p. */
17096 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17098 /* Restore the queue. */
17099 parser->unparsed_functions_queues
17100 = TREE_CHAIN (parser->unparsed_functions_queues);
17103 /* Parse the operand of `sizeof' (or a similar operator). Returns
17104 either a TYPE or an expression, depending on the form of the
17105 input. The KEYWORD indicates which kind of expression we have
17109 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17111 static const char *format;
17112 tree expr = NULL_TREE;
17113 const char *saved_message;
17114 bool saved_integral_constant_expression_p;
17115 bool saved_non_integral_constant_expression_p;
17116 bool pack_expansion_p = false;
17118 /* Initialize FORMAT the first time we get here. */
17120 format = "types may not be defined in '%s' expressions";
17122 /* Types cannot be defined in a `sizeof' expression. Save away the
17124 saved_message = parser->type_definition_forbidden_message;
17125 /* And create the new one. */
17126 parser->type_definition_forbidden_message
17127 = XNEWVEC (const char, strlen (format)
17128 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17130 sprintf ((char *) parser->type_definition_forbidden_message,
17131 format, IDENTIFIER_POINTER (ridpointers[keyword]));
17133 /* The restrictions on constant-expressions do not apply inside
17134 sizeof expressions. */
17135 saved_integral_constant_expression_p
17136 = parser->integral_constant_expression_p;
17137 saved_non_integral_constant_expression_p
17138 = parser->non_integral_constant_expression_p;
17139 parser->integral_constant_expression_p = false;
17141 /* If it's a `...', then we are computing the length of a parameter
17143 if (keyword == RID_SIZEOF
17144 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17146 /* Consume the `...'. */
17147 cp_lexer_consume_token (parser->lexer);
17148 maybe_warn_variadic_templates ();
17150 /* Note that this is an expansion. */
17151 pack_expansion_p = true;
17154 /* Do not actually evaluate the expression. */
17156 /* If it's a `(', then we might be looking at the type-id
17158 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17161 bool saved_in_type_id_in_expr_p;
17163 /* We can't be sure yet whether we're looking at a type-id or an
17165 cp_parser_parse_tentatively (parser);
17166 /* Consume the `('. */
17167 cp_lexer_consume_token (parser->lexer);
17168 /* Parse the type-id. */
17169 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17170 parser->in_type_id_in_expr_p = true;
17171 type = cp_parser_type_id (parser);
17172 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17173 /* Now, look for the trailing `)'. */
17174 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17175 /* If all went well, then we're done. */
17176 if (cp_parser_parse_definitely (parser))
17178 cp_decl_specifier_seq decl_specs;
17180 /* Build a trivial decl-specifier-seq. */
17181 clear_decl_specs (&decl_specs);
17182 decl_specs.type = type;
17184 /* Call grokdeclarator to figure out what type this is. */
17185 expr = grokdeclarator (NULL,
17189 /*attrlist=*/NULL);
17193 /* If the type-id production did not work out, then we must be
17194 looking at the unary-expression production. */
17196 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17199 if (pack_expansion_p)
17200 /* Build a pack expansion. */
17201 expr = make_pack_expansion (expr);
17203 /* Go back to evaluating expressions. */
17206 /* Free the message we created. */
17207 free ((char *) parser->type_definition_forbidden_message);
17208 /* And restore the old one. */
17209 parser->type_definition_forbidden_message = saved_message;
17210 parser->integral_constant_expression_p
17211 = saved_integral_constant_expression_p;
17212 parser->non_integral_constant_expression_p
17213 = saved_non_integral_constant_expression_p;
17218 /* If the current declaration has no declarator, return true. */
17221 cp_parser_declares_only_class_p (cp_parser *parser)
17223 /* If the next token is a `;' or a `,' then there is no
17225 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17226 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17229 /* Update the DECL_SPECS to reflect the storage class indicated by
17233 cp_parser_set_storage_class (cp_parser *parser,
17234 cp_decl_specifier_seq *decl_specs,
17237 cp_storage_class storage_class;
17239 if (parser->in_unbraced_linkage_specification_p)
17241 error ("invalid use of %qD in linkage specification",
17242 ridpointers[keyword]);
17245 else if (decl_specs->storage_class != sc_none)
17247 decl_specs->conflicting_specifiers_p = true;
17251 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17252 && decl_specs->specs[(int) ds_thread])
17254 error ("%<__thread%> before %qD", ridpointers[keyword]);
17255 decl_specs->specs[(int) ds_thread] = 0;
17261 storage_class = sc_auto;
17264 storage_class = sc_register;
17267 storage_class = sc_static;
17270 storage_class = sc_extern;
17273 storage_class = sc_mutable;
17276 gcc_unreachable ();
17278 decl_specs->storage_class = storage_class;
17280 /* A storage class specifier cannot be applied alongside a typedef
17281 specifier. If there is a typedef specifier present then set
17282 conflicting_specifiers_p which will trigger an error later
17283 on in grokdeclarator. */
17284 if (decl_specs->specs[(int)ds_typedef])
17285 decl_specs->conflicting_specifiers_p = true;
17288 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17289 is true, the type is a user-defined type; otherwise it is a
17290 built-in type specified by a keyword. */
17293 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17295 bool user_defined_p)
17297 decl_specs->any_specifiers_p = true;
17299 /* If the user tries to redeclare bool or wchar_t (with, for
17300 example, in "typedef int wchar_t;") we remember that this is what
17301 happened. In system headers, we ignore these declarations so
17302 that G++ can work with system headers that are not C++-safe. */
17303 if (decl_specs->specs[(int) ds_typedef]
17305 && (type_spec == boolean_type_node
17306 || type_spec == wchar_type_node)
17307 && (decl_specs->type
17308 || decl_specs->specs[(int) ds_long]
17309 || decl_specs->specs[(int) ds_short]
17310 || decl_specs->specs[(int) ds_unsigned]
17311 || decl_specs->specs[(int) ds_signed]))
17313 decl_specs->redefined_builtin_type = type_spec;
17314 if (!decl_specs->type)
17316 decl_specs->type = type_spec;
17317 decl_specs->user_defined_type_p = false;
17320 else if (decl_specs->type)
17321 decl_specs->multiple_types_p = true;
17324 decl_specs->type = type_spec;
17325 decl_specs->user_defined_type_p = user_defined_p;
17326 decl_specs->redefined_builtin_type = NULL_TREE;
17330 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17331 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17334 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17336 return decl_specifiers->specs[(int) ds_friend] != 0;
17339 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17340 issue an error message indicating that TOKEN_DESC was expected.
17342 Returns the token consumed, if the token had the appropriate type.
17343 Otherwise, returns NULL. */
17346 cp_parser_require (cp_parser* parser,
17347 enum cpp_ttype type,
17348 const char* token_desc)
17350 if (cp_lexer_next_token_is (parser->lexer, type))
17351 return cp_lexer_consume_token (parser->lexer);
17354 /* Output the MESSAGE -- unless we're parsing tentatively. */
17355 if (!cp_parser_simulate_error (parser))
17357 char *message = concat ("expected ", token_desc, NULL);
17358 cp_parser_error (parser, message);
17365 /* An error message is produced if the next token is not '>'.
17366 All further tokens are skipped until the desired token is
17367 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17370 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17372 /* Current level of '< ... >'. */
17373 unsigned level = 0;
17374 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17375 unsigned nesting_depth = 0;
17377 /* Are we ready, yet? If not, issue error message. */
17378 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17381 /* Skip tokens until the desired token is found. */
17384 /* Peek at the next token. */
17385 switch (cp_lexer_peek_token (parser->lexer)->type)
17388 if (!nesting_depth)
17393 if (cxx_dialect == cxx98)
17394 /* C++0x views the `>>' operator as two `>' tokens, but
17397 else if (!nesting_depth && level-- == 0)
17399 /* We've hit a `>>' where the first `>' closes the
17400 template argument list, and the second `>' is
17401 spurious. Just consume the `>>' and stop; we've
17402 already produced at least one error. */
17403 cp_lexer_consume_token (parser->lexer);
17406 /* Fall through for C++0x, so we handle the second `>' in
17410 if (!nesting_depth && level-- == 0)
17412 /* We've reached the token we want, consume it and stop. */
17413 cp_lexer_consume_token (parser->lexer);
17418 case CPP_OPEN_PAREN:
17419 case CPP_OPEN_SQUARE:
17423 case CPP_CLOSE_PAREN:
17424 case CPP_CLOSE_SQUARE:
17425 if (nesting_depth-- == 0)
17430 case CPP_PRAGMA_EOL:
17431 case CPP_SEMICOLON:
17432 case CPP_OPEN_BRACE:
17433 case CPP_CLOSE_BRACE:
17434 /* The '>' was probably forgotten, don't look further. */
17441 /* Consume this token. */
17442 cp_lexer_consume_token (parser->lexer);
17446 /* If the next token is the indicated keyword, consume it. Otherwise,
17447 issue an error message indicating that TOKEN_DESC was expected.
17449 Returns the token consumed, if the token had the appropriate type.
17450 Otherwise, returns NULL. */
17453 cp_parser_require_keyword (cp_parser* parser,
17455 const char* token_desc)
17457 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17459 if (token && token->keyword != keyword)
17461 dyn_string_t error_msg;
17463 /* Format the error message. */
17464 error_msg = dyn_string_new (0);
17465 dyn_string_append_cstr (error_msg, "expected ");
17466 dyn_string_append_cstr (error_msg, token_desc);
17467 cp_parser_error (parser, error_msg->s);
17468 dyn_string_delete (error_msg);
17475 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17476 function-definition. */
17479 cp_parser_token_starts_function_definition_p (cp_token* token)
17481 return (/* An ordinary function-body begins with an `{'. */
17482 token->type == CPP_OPEN_BRACE
17483 /* A ctor-initializer begins with a `:'. */
17484 || token->type == CPP_COLON
17485 /* A function-try-block begins with `try'. */
17486 || token->keyword == RID_TRY
17487 /* The named return value extension begins with `return'. */
17488 || token->keyword == RID_RETURN);
17491 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17495 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17499 token = cp_lexer_peek_token (parser->lexer);
17500 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17503 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17504 C++0x) ending a template-argument. */
17507 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17511 token = cp_lexer_peek_token (parser->lexer);
17512 return (token->type == CPP_COMMA
17513 || token->type == CPP_GREATER
17514 || token->type == CPP_ELLIPSIS
17515 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17518 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17519 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17522 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17527 token = cp_lexer_peek_nth_token (parser->lexer, n);
17528 if (token->type == CPP_LESS)
17530 /* Check for the sequence `<::' in the original code. It would be lexed as
17531 `[:', where `[' is a digraph, and there is no whitespace before
17533 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17536 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17537 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17543 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17544 or none_type otherwise. */
17546 static enum tag_types
17547 cp_parser_token_is_class_key (cp_token* token)
17549 switch (token->keyword)
17554 return record_type;
17563 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17566 cp_parser_check_class_key (enum tag_types class_key, tree type)
17568 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17569 pedwarn ("%qs tag used in naming %q#T",
17570 class_key == union_type ? "union"
17571 : class_key == record_type ? "struct" : "class",
17575 /* Issue an error message if DECL is redeclared with different
17576 access than its original declaration [class.access.spec/3].
17577 This applies to nested classes and nested class templates.
17581 cp_parser_check_access_in_redeclaration (tree decl)
17583 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17586 if ((TREE_PRIVATE (decl)
17587 != (current_access_specifier == access_private_node))
17588 || (TREE_PROTECTED (decl)
17589 != (current_access_specifier == access_protected_node)))
17590 error ("%qD redeclared with different access", decl);
17593 /* Look for the `template' keyword, as a syntactic disambiguator.
17594 Return TRUE iff it is present, in which case it will be
17598 cp_parser_optional_template_keyword (cp_parser *parser)
17600 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17602 /* The `template' keyword can only be used within templates;
17603 outside templates the parser can always figure out what is a
17604 template and what is not. */
17605 if (!processing_template_decl)
17607 error ("%<template%> (as a disambiguator) is only allowed "
17608 "within templates");
17609 /* If this part of the token stream is rescanned, the same
17610 error message would be generated. So, we purge the token
17611 from the stream. */
17612 cp_lexer_purge_token (parser->lexer);
17617 /* Consume the `template' keyword. */
17618 cp_lexer_consume_token (parser->lexer);
17626 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17627 set PARSER->SCOPE, and perform other related actions. */
17630 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17633 struct tree_check *check_value;
17634 deferred_access_check *chk;
17635 VEC (deferred_access_check,gc) *checks;
17637 /* Get the stored value. */
17638 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17639 /* Perform any access checks that were deferred. */
17640 checks = check_value->checks;
17644 VEC_iterate (deferred_access_check, checks, i, chk) ;
17647 perform_or_defer_access_check (chk->binfo,
17652 /* Set the scope from the stored value. */
17653 parser->scope = check_value->value;
17654 parser->qualifying_scope = check_value->qualifying_scope;
17655 parser->object_scope = NULL_TREE;
17658 /* Consume tokens up through a non-nested END token. */
17661 cp_parser_cache_group (cp_parser *parser,
17662 enum cpp_ttype end,
17669 /* Abort a parenthesized expression if we encounter a brace. */
17670 if ((end == CPP_CLOSE_PAREN || depth == 0)
17671 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17673 /* If we've reached the end of the file, stop. */
17674 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17675 || (end != CPP_PRAGMA_EOL
17676 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17678 /* Consume the next token. */
17679 token = cp_lexer_consume_token (parser->lexer);
17680 /* See if it starts a new group. */
17681 if (token->type == CPP_OPEN_BRACE)
17683 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17687 else if (token->type == CPP_OPEN_PAREN)
17688 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17689 else if (token->type == CPP_PRAGMA)
17690 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17691 else if (token->type == end)
17696 /* Begin parsing tentatively. We always save tokens while parsing
17697 tentatively so that if the tentative parsing fails we can restore the
17701 cp_parser_parse_tentatively (cp_parser* parser)
17703 /* Enter a new parsing context. */
17704 parser->context = cp_parser_context_new (parser->context);
17705 /* Begin saving tokens. */
17706 cp_lexer_save_tokens (parser->lexer);
17707 /* In order to avoid repetitive access control error messages,
17708 access checks are queued up until we are no longer parsing
17710 push_deferring_access_checks (dk_deferred);
17713 /* Commit to the currently active tentative parse. */
17716 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17718 cp_parser_context *context;
17721 /* Mark all of the levels as committed. */
17722 lexer = parser->lexer;
17723 for (context = parser->context; context->next; context = context->next)
17725 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17727 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17728 while (!cp_lexer_saving_tokens (lexer))
17729 lexer = lexer->next;
17730 cp_lexer_commit_tokens (lexer);
17734 /* Abort the currently active tentative parse. All consumed tokens
17735 will be rolled back, and no diagnostics will be issued. */
17738 cp_parser_abort_tentative_parse (cp_parser* parser)
17740 cp_parser_simulate_error (parser);
17741 /* Now, pretend that we want to see if the construct was
17742 successfully parsed. */
17743 cp_parser_parse_definitely (parser);
17746 /* Stop parsing tentatively. If a parse error has occurred, restore the
17747 token stream. Otherwise, commit to the tokens we have consumed.
17748 Returns true if no error occurred; false otherwise. */
17751 cp_parser_parse_definitely (cp_parser* parser)
17753 bool error_occurred;
17754 cp_parser_context *context;
17756 /* Remember whether or not an error occurred, since we are about to
17757 destroy that information. */
17758 error_occurred = cp_parser_error_occurred (parser);
17759 /* Remove the topmost context from the stack. */
17760 context = parser->context;
17761 parser->context = context->next;
17762 /* If no parse errors occurred, commit to the tentative parse. */
17763 if (!error_occurred)
17765 /* Commit to the tokens read tentatively, unless that was
17767 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17768 cp_lexer_commit_tokens (parser->lexer);
17770 pop_to_parent_deferring_access_checks ();
17772 /* Otherwise, if errors occurred, roll back our state so that things
17773 are just as they were before we began the tentative parse. */
17776 cp_lexer_rollback_tokens (parser->lexer);
17777 pop_deferring_access_checks ();
17779 /* Add the context to the front of the free list. */
17780 context->next = cp_parser_context_free_list;
17781 cp_parser_context_free_list = context;
17783 return !error_occurred;
17786 /* Returns true if we are parsing tentatively and are not committed to
17787 this tentative parse. */
17790 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17792 return (cp_parser_parsing_tentatively (parser)
17793 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17796 /* Returns nonzero iff an error has occurred during the most recent
17797 tentative parse. */
17800 cp_parser_error_occurred (cp_parser* parser)
17802 return (cp_parser_parsing_tentatively (parser)
17803 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17806 /* Returns nonzero if GNU extensions are allowed. */
17809 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17811 return parser->allow_gnu_extensions_p;
17814 /* Objective-C++ Productions */
17817 /* Parse an Objective-C expression, which feeds into a primary-expression
17821 objc-message-expression
17822 objc-string-literal
17823 objc-encode-expression
17824 objc-protocol-expression
17825 objc-selector-expression
17827 Returns a tree representation of the expression. */
17830 cp_parser_objc_expression (cp_parser* parser)
17832 /* Try to figure out what kind of declaration is present. */
17833 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17837 case CPP_OPEN_SQUARE:
17838 return cp_parser_objc_message_expression (parser);
17840 case CPP_OBJC_STRING:
17841 kwd = cp_lexer_consume_token (parser->lexer);
17842 return objc_build_string_object (kwd->u.value);
17845 switch (kwd->keyword)
17847 case RID_AT_ENCODE:
17848 return cp_parser_objc_encode_expression (parser);
17850 case RID_AT_PROTOCOL:
17851 return cp_parser_objc_protocol_expression (parser);
17853 case RID_AT_SELECTOR:
17854 return cp_parser_objc_selector_expression (parser);
17860 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17861 cp_parser_skip_to_end_of_block_or_statement (parser);
17864 return error_mark_node;
17867 /* Parse an Objective-C message expression.
17869 objc-message-expression:
17870 [ objc-message-receiver objc-message-args ]
17872 Returns a representation of an Objective-C message. */
17875 cp_parser_objc_message_expression (cp_parser* parser)
17877 tree receiver, messageargs;
17879 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17880 receiver = cp_parser_objc_message_receiver (parser);
17881 messageargs = cp_parser_objc_message_args (parser);
17882 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17884 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17887 /* Parse an objc-message-receiver.
17889 objc-message-receiver:
17891 simple-type-specifier
17893 Returns a representation of the type or expression. */
17896 cp_parser_objc_message_receiver (cp_parser* parser)
17900 /* An Objective-C message receiver may be either (1) a type
17901 or (2) an expression. */
17902 cp_parser_parse_tentatively (parser);
17903 rcv = cp_parser_expression (parser, false);
17905 if (cp_parser_parse_definitely (parser))
17908 rcv = cp_parser_simple_type_specifier (parser,
17909 /*decl_specs=*/NULL,
17910 CP_PARSER_FLAGS_NONE);
17912 return objc_get_class_reference (rcv);
17915 /* Parse the arguments and selectors comprising an Objective-C message.
17920 objc-selector-args , objc-comma-args
17922 objc-selector-args:
17923 objc-selector [opt] : assignment-expression
17924 objc-selector-args objc-selector [opt] : assignment-expression
17927 assignment-expression
17928 objc-comma-args , assignment-expression
17930 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17931 selector arguments and TREE_VALUE containing a list of comma
17935 cp_parser_objc_message_args (cp_parser* parser)
17937 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17938 bool maybe_unary_selector_p = true;
17939 cp_token *token = cp_lexer_peek_token (parser->lexer);
17941 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17943 tree selector = NULL_TREE, arg;
17945 if (token->type != CPP_COLON)
17946 selector = cp_parser_objc_selector (parser);
17948 /* Detect if we have a unary selector. */
17949 if (maybe_unary_selector_p
17950 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17951 return build_tree_list (selector, NULL_TREE);
17953 maybe_unary_selector_p = false;
17954 cp_parser_require (parser, CPP_COLON, "`:'");
17955 arg = cp_parser_assignment_expression (parser, false);
17958 = chainon (sel_args,
17959 build_tree_list (selector, arg));
17961 token = cp_lexer_peek_token (parser->lexer);
17964 /* Handle non-selector arguments, if any. */
17965 while (token->type == CPP_COMMA)
17969 cp_lexer_consume_token (parser->lexer);
17970 arg = cp_parser_assignment_expression (parser, false);
17973 = chainon (addl_args,
17974 build_tree_list (NULL_TREE, arg));
17976 token = cp_lexer_peek_token (parser->lexer);
17979 return build_tree_list (sel_args, addl_args);
17982 /* Parse an Objective-C encode expression.
17984 objc-encode-expression:
17985 @encode objc-typename
17987 Returns an encoded representation of the type argument. */
17990 cp_parser_objc_encode_expression (cp_parser* parser)
17994 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17995 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17996 type = complete_type (cp_parser_type_id (parser));
17997 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18001 error ("%<@encode%> must specify a type as an argument");
18002 return error_mark_node;
18005 return objc_build_encode_expr (type);
18008 /* Parse an Objective-C @defs expression. */
18011 cp_parser_objc_defs_expression (cp_parser *parser)
18015 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18016 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18017 name = cp_parser_identifier (parser);
18018 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18020 return objc_get_class_ivars (name);
18023 /* Parse an Objective-C protocol expression.
18025 objc-protocol-expression:
18026 @protocol ( identifier )
18028 Returns a representation of the protocol expression. */
18031 cp_parser_objc_protocol_expression (cp_parser* parser)
18035 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18036 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18037 proto = cp_parser_identifier (parser);
18038 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18040 return objc_build_protocol_expr (proto);
18043 /* Parse an Objective-C selector expression.
18045 objc-selector-expression:
18046 @selector ( objc-method-signature )
18048 objc-method-signature:
18054 objc-selector-seq objc-selector :
18056 Returns a representation of the method selector. */
18059 cp_parser_objc_selector_expression (cp_parser* parser)
18061 tree sel_seq = NULL_TREE;
18062 bool maybe_unary_selector_p = true;
18065 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18066 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18067 token = cp_lexer_peek_token (parser->lexer);
18069 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18070 || token->type == CPP_SCOPE)
18072 tree selector = NULL_TREE;
18074 if (token->type != CPP_COLON
18075 || token->type == CPP_SCOPE)
18076 selector = cp_parser_objc_selector (parser);
18078 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18079 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18081 /* Detect if we have a unary selector. */
18082 if (maybe_unary_selector_p)
18084 sel_seq = selector;
18085 goto finish_selector;
18089 cp_parser_error (parser, "expected %<:%>");
18092 maybe_unary_selector_p = false;
18093 token = cp_lexer_consume_token (parser->lexer);
18095 if (token->type == CPP_SCOPE)
18098 = chainon (sel_seq,
18099 build_tree_list (selector, NULL_TREE));
18101 = chainon (sel_seq,
18102 build_tree_list (NULL_TREE, NULL_TREE));
18106 = chainon (sel_seq,
18107 build_tree_list (selector, NULL_TREE));
18109 token = cp_lexer_peek_token (parser->lexer);
18113 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18115 return objc_build_selector_expr (sel_seq);
18118 /* Parse a list of identifiers.
18120 objc-identifier-list:
18122 objc-identifier-list , identifier
18124 Returns a TREE_LIST of identifier nodes. */
18127 cp_parser_objc_identifier_list (cp_parser* parser)
18129 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18130 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18132 while (sep->type == CPP_COMMA)
18134 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18135 list = chainon (list,
18136 build_tree_list (NULL_TREE,
18137 cp_parser_identifier (parser)));
18138 sep = cp_lexer_peek_token (parser->lexer);
18144 /* Parse an Objective-C alias declaration.
18146 objc-alias-declaration:
18147 @compatibility_alias identifier identifier ;
18149 This function registers the alias mapping with the Objective-C front end.
18150 It returns nothing. */
18153 cp_parser_objc_alias_declaration (cp_parser* parser)
18157 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18158 alias = cp_parser_identifier (parser);
18159 orig = cp_parser_identifier (parser);
18160 objc_declare_alias (alias, orig);
18161 cp_parser_consume_semicolon_at_end_of_statement (parser);
18164 /* Parse an Objective-C class forward-declaration.
18166 objc-class-declaration:
18167 @class objc-identifier-list ;
18169 The function registers the forward declarations with the Objective-C
18170 front end. It returns nothing. */
18173 cp_parser_objc_class_declaration (cp_parser* parser)
18175 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18176 objc_declare_class (cp_parser_objc_identifier_list (parser));
18177 cp_parser_consume_semicolon_at_end_of_statement (parser);
18180 /* Parse a list of Objective-C protocol references.
18182 objc-protocol-refs-opt:
18183 objc-protocol-refs [opt]
18185 objc-protocol-refs:
18186 < objc-identifier-list >
18188 Returns a TREE_LIST of identifiers, if any. */
18191 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18193 tree protorefs = NULL_TREE;
18195 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18197 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18198 protorefs = cp_parser_objc_identifier_list (parser);
18199 cp_parser_require (parser, CPP_GREATER, "`>'");
18205 /* Parse a Objective-C visibility specification. */
18208 cp_parser_objc_visibility_spec (cp_parser* parser)
18210 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18212 switch (vis->keyword)
18214 case RID_AT_PRIVATE:
18215 objc_set_visibility (2);
18217 case RID_AT_PROTECTED:
18218 objc_set_visibility (0);
18220 case RID_AT_PUBLIC:
18221 objc_set_visibility (1);
18227 /* Eat '@private'/'@protected'/'@public'. */
18228 cp_lexer_consume_token (parser->lexer);
18231 /* Parse an Objective-C method type. */
18234 cp_parser_objc_method_type (cp_parser* parser)
18236 objc_set_method_type
18237 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18242 /* Parse an Objective-C protocol qualifier. */
18245 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18247 tree quals = NULL_TREE, node;
18248 cp_token *token = cp_lexer_peek_token (parser->lexer);
18250 node = token->u.value;
18252 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18253 && (node == ridpointers [(int) RID_IN]
18254 || node == ridpointers [(int) RID_OUT]
18255 || node == ridpointers [(int) RID_INOUT]
18256 || node == ridpointers [(int) RID_BYCOPY]
18257 || node == ridpointers [(int) RID_BYREF]
18258 || node == ridpointers [(int) RID_ONEWAY]))
18260 quals = tree_cons (NULL_TREE, node, quals);
18261 cp_lexer_consume_token (parser->lexer);
18262 token = cp_lexer_peek_token (parser->lexer);
18263 node = token->u.value;
18269 /* Parse an Objective-C typename. */
18272 cp_parser_objc_typename (cp_parser* parser)
18274 tree typename = NULL_TREE;
18276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18278 tree proto_quals, cp_type = NULL_TREE;
18280 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18281 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18283 /* An ObjC type name may consist of just protocol qualifiers, in which
18284 case the type shall default to 'id'. */
18285 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18286 cp_type = cp_parser_type_id (parser);
18288 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18289 typename = build_tree_list (proto_quals, cp_type);
18295 /* Check to see if TYPE refers to an Objective-C selector name. */
18298 cp_parser_objc_selector_p (enum cpp_ttype type)
18300 return (type == CPP_NAME || type == CPP_KEYWORD
18301 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18302 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18303 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18304 || type == CPP_XOR || type == CPP_XOR_EQ);
18307 /* Parse an Objective-C selector. */
18310 cp_parser_objc_selector (cp_parser* parser)
18312 cp_token *token = cp_lexer_consume_token (parser->lexer);
18314 if (!cp_parser_objc_selector_p (token->type))
18316 error ("invalid Objective-C++ selector name");
18317 return error_mark_node;
18320 /* C++ operator names are allowed to appear in ObjC selectors. */
18321 switch (token->type)
18323 case CPP_AND_AND: return get_identifier ("and");
18324 case CPP_AND_EQ: return get_identifier ("and_eq");
18325 case CPP_AND: return get_identifier ("bitand");
18326 case CPP_OR: return get_identifier ("bitor");
18327 case CPP_COMPL: return get_identifier ("compl");
18328 case CPP_NOT: return get_identifier ("not");
18329 case CPP_NOT_EQ: return get_identifier ("not_eq");
18330 case CPP_OR_OR: return get_identifier ("or");
18331 case CPP_OR_EQ: return get_identifier ("or_eq");
18332 case CPP_XOR: return get_identifier ("xor");
18333 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18334 default: return token->u.value;
18338 /* Parse an Objective-C params list. */
18341 cp_parser_objc_method_keyword_params (cp_parser* parser)
18343 tree params = NULL_TREE;
18344 bool maybe_unary_selector_p = true;
18345 cp_token *token = cp_lexer_peek_token (parser->lexer);
18347 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18349 tree selector = NULL_TREE, typename, identifier;
18351 if (token->type != CPP_COLON)
18352 selector = cp_parser_objc_selector (parser);
18354 /* Detect if we have a unary selector. */
18355 if (maybe_unary_selector_p
18356 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18359 maybe_unary_selector_p = false;
18360 cp_parser_require (parser, CPP_COLON, "`:'");
18361 typename = cp_parser_objc_typename (parser);
18362 identifier = cp_parser_identifier (parser);
18366 objc_build_keyword_decl (selector,
18370 token = cp_lexer_peek_token (parser->lexer);
18376 /* Parse the non-keyword Objective-C params. */
18379 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18381 tree params = make_node (TREE_LIST);
18382 cp_token *token = cp_lexer_peek_token (parser->lexer);
18383 *ellipsisp = false; /* Initially, assume no ellipsis. */
18385 while (token->type == CPP_COMMA)
18387 cp_parameter_declarator *parmdecl;
18390 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18391 token = cp_lexer_peek_token (parser->lexer);
18393 if (token->type == CPP_ELLIPSIS)
18395 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18400 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18401 parm = grokdeclarator (parmdecl->declarator,
18402 &parmdecl->decl_specifiers,
18403 PARM, /*initialized=*/0,
18404 /*attrlist=*/NULL);
18406 chainon (params, build_tree_list (NULL_TREE, parm));
18407 token = cp_lexer_peek_token (parser->lexer);
18413 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18416 cp_parser_objc_interstitial_code (cp_parser* parser)
18418 cp_token *token = cp_lexer_peek_token (parser->lexer);
18420 /* If the next token is `extern' and the following token is a string
18421 literal, then we have a linkage specification. */
18422 if (token->keyword == RID_EXTERN
18423 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18424 cp_parser_linkage_specification (parser);
18425 /* Handle #pragma, if any. */
18426 else if (token->type == CPP_PRAGMA)
18427 cp_parser_pragma (parser, pragma_external);
18428 /* Allow stray semicolons. */
18429 else if (token->type == CPP_SEMICOLON)
18430 cp_lexer_consume_token (parser->lexer);
18431 /* Finally, try to parse a block-declaration, or a function-definition. */
18433 cp_parser_block_declaration (parser, /*statement_p=*/false);
18436 /* Parse a method signature. */
18439 cp_parser_objc_method_signature (cp_parser* parser)
18441 tree rettype, kwdparms, optparms;
18442 bool ellipsis = false;
18444 cp_parser_objc_method_type (parser);
18445 rettype = cp_parser_objc_typename (parser);
18446 kwdparms = cp_parser_objc_method_keyword_params (parser);
18447 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18449 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18452 /* Pars an Objective-C method prototype list. */
18455 cp_parser_objc_method_prototype_list (cp_parser* parser)
18457 cp_token *token = cp_lexer_peek_token (parser->lexer);
18459 while (token->keyword != RID_AT_END)
18461 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18463 objc_add_method_declaration
18464 (cp_parser_objc_method_signature (parser));
18465 cp_parser_consume_semicolon_at_end_of_statement (parser);
18468 /* Allow for interspersed non-ObjC++ code. */
18469 cp_parser_objc_interstitial_code (parser);
18471 token = cp_lexer_peek_token (parser->lexer);
18474 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18475 objc_finish_interface ();
18478 /* Parse an Objective-C method definition list. */
18481 cp_parser_objc_method_definition_list (cp_parser* parser)
18483 cp_token *token = cp_lexer_peek_token (parser->lexer);
18485 while (token->keyword != RID_AT_END)
18489 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18491 push_deferring_access_checks (dk_deferred);
18492 objc_start_method_definition
18493 (cp_parser_objc_method_signature (parser));
18495 /* For historical reasons, we accept an optional semicolon. */
18496 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18497 cp_lexer_consume_token (parser->lexer);
18499 perform_deferred_access_checks ();
18500 stop_deferring_access_checks ();
18501 meth = cp_parser_function_definition_after_declarator (parser,
18503 pop_deferring_access_checks ();
18504 objc_finish_method_definition (meth);
18507 /* Allow for interspersed non-ObjC++ code. */
18508 cp_parser_objc_interstitial_code (parser);
18510 token = cp_lexer_peek_token (parser->lexer);
18513 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18514 objc_finish_implementation ();
18517 /* Parse Objective-C ivars. */
18520 cp_parser_objc_class_ivars (cp_parser* parser)
18522 cp_token *token = cp_lexer_peek_token (parser->lexer);
18524 if (token->type != CPP_OPEN_BRACE)
18525 return; /* No ivars specified. */
18527 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18528 token = cp_lexer_peek_token (parser->lexer);
18530 while (token->type != CPP_CLOSE_BRACE)
18532 cp_decl_specifier_seq declspecs;
18533 int decl_class_or_enum_p;
18534 tree prefix_attributes;
18536 cp_parser_objc_visibility_spec (parser);
18538 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18541 cp_parser_decl_specifier_seq (parser,
18542 CP_PARSER_FLAGS_OPTIONAL,
18544 &decl_class_or_enum_p);
18545 prefix_attributes = declspecs.attributes;
18546 declspecs.attributes = NULL_TREE;
18548 /* Keep going until we hit the `;' at the end of the
18550 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18552 tree width = NULL_TREE, attributes, first_attribute, decl;
18553 cp_declarator *declarator = NULL;
18554 int ctor_dtor_or_conv_p;
18556 /* Check for a (possibly unnamed) bitfield declaration. */
18557 token = cp_lexer_peek_token (parser->lexer);
18558 if (token->type == CPP_COLON)
18561 if (token->type == CPP_NAME
18562 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18565 /* Get the name of the bitfield. */
18566 declarator = make_id_declarator (NULL_TREE,
18567 cp_parser_identifier (parser),
18571 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18572 /* Get the width of the bitfield. */
18574 = cp_parser_constant_expression (parser,
18575 /*allow_non_constant=*/false,
18580 /* Parse the declarator. */
18582 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18583 &ctor_dtor_or_conv_p,
18584 /*parenthesized_p=*/NULL,
18585 /*member_p=*/false);
18588 /* Look for attributes that apply to the ivar. */
18589 attributes = cp_parser_attributes_opt (parser);
18590 /* Remember which attributes are prefix attributes and
18592 first_attribute = attributes;
18593 /* Combine the attributes. */
18594 attributes = chainon (prefix_attributes, attributes);
18598 /* Create the bitfield declaration. */
18599 decl = grokbitfield (declarator, &declspecs, width);
18600 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18603 decl = grokfield (declarator, &declspecs,
18604 NULL_TREE, /*init_const_expr_p=*/false,
18605 NULL_TREE, attributes);
18607 /* Add the instance variable. */
18608 objc_add_instance_variable (decl);
18610 /* Reset PREFIX_ATTRIBUTES. */
18611 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18612 attributes = TREE_CHAIN (attributes);
18614 TREE_CHAIN (attributes) = NULL_TREE;
18616 token = cp_lexer_peek_token (parser->lexer);
18618 if (token->type == CPP_COMMA)
18620 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18626 cp_parser_consume_semicolon_at_end_of_statement (parser);
18627 token = cp_lexer_peek_token (parser->lexer);
18630 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18631 /* For historical reasons, we accept an optional semicolon. */
18632 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18633 cp_lexer_consume_token (parser->lexer);
18636 /* Parse an Objective-C protocol declaration. */
18639 cp_parser_objc_protocol_declaration (cp_parser* parser)
18641 tree proto, protorefs;
18644 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18645 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18647 error ("identifier expected after %<@protocol%>");
18651 /* See if we have a forward declaration or a definition. */
18652 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18654 /* Try a forward declaration first. */
18655 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18657 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18659 cp_parser_consume_semicolon_at_end_of_statement (parser);
18662 /* Ok, we got a full-fledged definition (or at least should). */
18665 proto = cp_parser_identifier (parser);
18666 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18667 objc_start_protocol (proto, protorefs);
18668 cp_parser_objc_method_prototype_list (parser);
18672 /* Parse an Objective-C superclass or category. */
18675 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18678 cp_token *next = cp_lexer_peek_token (parser->lexer);
18680 *super = *categ = NULL_TREE;
18681 if (next->type == CPP_COLON)
18683 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18684 *super = cp_parser_identifier (parser);
18686 else if (next->type == CPP_OPEN_PAREN)
18688 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18689 *categ = cp_parser_identifier (parser);
18690 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18694 /* Parse an Objective-C class interface. */
18697 cp_parser_objc_class_interface (cp_parser* parser)
18699 tree name, super, categ, protos;
18701 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18702 name = cp_parser_identifier (parser);
18703 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18704 protos = cp_parser_objc_protocol_refs_opt (parser);
18706 /* We have either a class or a category on our hands. */
18708 objc_start_category_interface (name, categ, protos);
18711 objc_start_class_interface (name, super, protos);
18712 /* Handle instance variable declarations, if any. */
18713 cp_parser_objc_class_ivars (parser);
18714 objc_continue_interface ();
18717 cp_parser_objc_method_prototype_list (parser);
18720 /* Parse an Objective-C class implementation. */
18723 cp_parser_objc_class_implementation (cp_parser* parser)
18725 tree name, super, categ;
18727 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18728 name = cp_parser_identifier (parser);
18729 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18731 /* We have either a class or a category on our hands. */
18733 objc_start_category_implementation (name, categ);
18736 objc_start_class_implementation (name, super);
18737 /* Handle instance variable declarations, if any. */
18738 cp_parser_objc_class_ivars (parser);
18739 objc_continue_implementation ();
18742 cp_parser_objc_method_definition_list (parser);
18745 /* Consume the @end token and finish off the implementation. */
18748 cp_parser_objc_end_implementation (cp_parser* parser)
18750 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18751 objc_finish_implementation ();
18754 /* Parse an Objective-C declaration. */
18757 cp_parser_objc_declaration (cp_parser* parser)
18759 /* Try to figure out what kind of declaration is present. */
18760 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18762 switch (kwd->keyword)
18765 cp_parser_objc_alias_declaration (parser);
18768 cp_parser_objc_class_declaration (parser);
18770 case RID_AT_PROTOCOL:
18771 cp_parser_objc_protocol_declaration (parser);
18773 case RID_AT_INTERFACE:
18774 cp_parser_objc_class_interface (parser);
18776 case RID_AT_IMPLEMENTATION:
18777 cp_parser_objc_class_implementation (parser);
18780 cp_parser_objc_end_implementation (parser);
18783 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18784 cp_parser_skip_to_end_of_block_or_statement (parser);
18788 /* Parse an Objective-C try-catch-finally statement.
18790 objc-try-catch-finally-stmt:
18791 @try compound-statement objc-catch-clause-seq [opt]
18792 objc-finally-clause [opt]
18794 objc-catch-clause-seq:
18795 objc-catch-clause objc-catch-clause-seq [opt]
18798 @catch ( exception-declaration ) compound-statement
18800 objc-finally-clause
18801 @finally compound-statement
18803 Returns NULL_TREE. */
18806 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18807 location_t location;
18810 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18811 location = cp_lexer_peek_token (parser->lexer)->location;
18812 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18813 node, lest it get absorbed into the surrounding block. */
18814 stmt = push_stmt_list ();
18815 cp_parser_compound_statement (parser, NULL, false);
18816 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18818 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18820 cp_parameter_declarator *parmdecl;
18823 cp_lexer_consume_token (parser->lexer);
18824 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18825 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18826 parm = grokdeclarator (parmdecl->declarator,
18827 &parmdecl->decl_specifiers,
18828 PARM, /*initialized=*/0,
18829 /*attrlist=*/NULL);
18830 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18831 objc_begin_catch_clause (parm);
18832 cp_parser_compound_statement (parser, NULL, false);
18833 objc_finish_catch_clause ();
18836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18838 cp_lexer_consume_token (parser->lexer);
18839 location = cp_lexer_peek_token (parser->lexer)->location;
18840 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18841 node, lest it get absorbed into the surrounding block. */
18842 stmt = push_stmt_list ();
18843 cp_parser_compound_statement (parser, NULL, false);
18844 objc_build_finally_clause (location, pop_stmt_list (stmt));
18847 return objc_finish_try_stmt ();
18850 /* Parse an Objective-C synchronized statement.
18852 objc-synchronized-stmt:
18853 @synchronized ( expression ) compound-statement
18855 Returns NULL_TREE. */
18858 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18859 location_t location;
18862 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18864 location = cp_lexer_peek_token (parser->lexer)->location;
18865 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18866 lock = cp_parser_expression (parser, false);
18867 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18869 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18870 node, lest it get absorbed into the surrounding block. */
18871 stmt = push_stmt_list ();
18872 cp_parser_compound_statement (parser, NULL, false);
18874 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18877 /* Parse an Objective-C throw statement.
18880 @throw assignment-expression [opt] ;
18882 Returns a constructed '@throw' statement. */
18885 cp_parser_objc_throw_statement (cp_parser *parser) {
18886 tree expr = NULL_TREE;
18888 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18890 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18891 expr = cp_parser_assignment_expression (parser, false);
18893 cp_parser_consume_semicolon_at_end_of_statement (parser);
18895 return objc_build_throw_stmt (expr);
18898 /* Parse an Objective-C statement. */
18901 cp_parser_objc_statement (cp_parser * parser) {
18902 /* Try to figure out what kind of declaration is present. */
18903 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18905 switch (kwd->keyword)
18908 return cp_parser_objc_try_catch_finally_statement (parser);
18909 case RID_AT_SYNCHRONIZED:
18910 return cp_parser_objc_synchronized_statement (parser);
18912 return cp_parser_objc_throw_statement (parser);
18914 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18915 cp_parser_skip_to_end_of_block_or_statement (parser);
18918 return error_mark_node;
18921 /* OpenMP 2.5 parsing routines. */
18923 /* Returns name of the next clause.
18924 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18925 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18926 returned and the token is consumed. */
18928 static pragma_omp_clause
18929 cp_parser_omp_clause_name (cp_parser *parser)
18931 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18933 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18934 result = PRAGMA_OMP_CLAUSE_IF;
18935 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18936 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18937 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18938 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18939 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18941 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18942 const char *p = IDENTIFIER_POINTER (id);
18947 if (!strcmp ("copyin", p))
18948 result = PRAGMA_OMP_CLAUSE_COPYIN;
18949 else if (!strcmp ("copyprivate", p))
18950 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18953 if (!strcmp ("firstprivate", p))
18954 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18957 if (!strcmp ("lastprivate", p))
18958 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18961 if (!strcmp ("nowait", p))
18962 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18963 else if (!strcmp ("num_threads", p))
18964 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18967 if (!strcmp ("ordered", p))
18968 result = PRAGMA_OMP_CLAUSE_ORDERED;
18971 if (!strcmp ("reduction", p))
18972 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18975 if (!strcmp ("schedule", p))
18976 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18977 else if (!strcmp ("shared", p))
18978 result = PRAGMA_OMP_CLAUSE_SHARED;
18983 if (result != PRAGMA_OMP_CLAUSE_NONE)
18984 cp_lexer_consume_token (parser->lexer);
18989 /* Validate that a clause of the given type does not already exist. */
18992 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18996 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18997 if (OMP_CLAUSE_CODE (c) == code)
18999 error ("too many %qs clauses", name);
19007 variable-list , identifier
19009 In addition, we match a closing parenthesis. An opening parenthesis
19010 will have been consumed by the caller.
19012 If KIND is nonzero, create the appropriate node and install the decl
19013 in OMP_CLAUSE_DECL and add the node to the head of the list.
19015 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19016 return the list created. */
19019 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19026 name = cp_parser_id_expression (parser, /*template_p=*/false,
19027 /*check_dependency_p=*/true,
19028 /*template_p=*/NULL,
19029 /*declarator_p=*/false,
19030 /*optional_p=*/false);
19031 if (name == error_mark_node)
19034 decl = cp_parser_lookup_name_simple (parser, name);
19035 if (decl == error_mark_node)
19036 cp_parser_name_lookup_error (parser, name, decl, NULL);
19037 else if (kind != 0)
19039 tree u = build_omp_clause (kind);
19040 OMP_CLAUSE_DECL (u) = decl;
19041 OMP_CLAUSE_CHAIN (u) = list;
19045 list = tree_cons (decl, NULL_TREE, list);
19048 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19050 cp_lexer_consume_token (parser->lexer);
19053 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19057 /* Try to resync to an unnested comma. Copied from
19058 cp_parser_parenthesized_expression_list. */
19060 ending = cp_parser_skip_to_closing_parenthesis (parser,
19061 /*recovering=*/true,
19063 /*consume_paren=*/true);
19071 /* Similarly, but expect leading and trailing parenthesis. This is a very
19072 common case for omp clauses. */
19075 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19077 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19078 return cp_parser_omp_var_list_no_open (parser, kind, list);
19083 default ( shared | none ) */
19086 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19088 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19091 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19093 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19095 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19096 const char *p = IDENTIFIER_POINTER (id);
19101 if (strcmp ("none", p) != 0)
19103 kind = OMP_CLAUSE_DEFAULT_NONE;
19107 if (strcmp ("shared", p) != 0)
19109 kind = OMP_CLAUSE_DEFAULT_SHARED;
19116 cp_lexer_consume_token (parser->lexer);
19121 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19124 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19125 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19126 /*or_comma=*/false,
19127 /*consume_paren=*/true);
19129 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19132 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19133 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19134 OMP_CLAUSE_CHAIN (c) = list;
19135 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19141 if ( expression ) */
19144 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19148 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19151 t = cp_parser_condition (parser);
19153 if (t == error_mark_node
19154 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19155 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19156 /*or_comma=*/false,
19157 /*consume_paren=*/true);
19159 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19161 c = build_omp_clause (OMP_CLAUSE_IF);
19162 OMP_CLAUSE_IF_EXPR (c) = t;
19163 OMP_CLAUSE_CHAIN (c) = list;
19172 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19176 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19178 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19179 OMP_CLAUSE_CHAIN (c) = list;
19184 num_threads ( expression ) */
19187 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19191 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19194 t = cp_parser_expression (parser, false);
19196 if (t == error_mark_node
19197 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19198 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19199 /*or_comma=*/false,
19200 /*consume_paren=*/true);
19202 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19204 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19205 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19206 OMP_CLAUSE_CHAIN (c) = list;
19215 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19219 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19221 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19222 OMP_CLAUSE_CHAIN (c) = list;
19227 reduction ( reduction-operator : variable-list )
19229 reduction-operator:
19230 One of: + * - & ^ | && || */
19233 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19235 enum tree_code code;
19238 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19241 switch (cp_lexer_peek_token (parser->lexer)->type)
19253 code = BIT_AND_EXPR;
19256 code = BIT_XOR_EXPR;
19259 code = BIT_IOR_EXPR;
19262 code = TRUTH_ANDIF_EXPR;
19265 code = TRUTH_ORIF_EXPR;
19268 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19270 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19271 /*or_comma=*/false,
19272 /*consume_paren=*/true);
19275 cp_lexer_consume_token (parser->lexer);
19277 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19280 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19281 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19282 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19288 schedule ( schedule-kind )
19289 schedule ( schedule-kind , expression )
19292 static | dynamic | guided | runtime */
19295 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19299 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19302 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19304 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19306 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19307 const char *p = IDENTIFIER_POINTER (id);
19312 if (strcmp ("dynamic", p) != 0)
19314 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19318 if (strcmp ("guided", p) != 0)
19320 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19324 if (strcmp ("runtime", p) != 0)
19326 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19333 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19334 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19337 cp_lexer_consume_token (parser->lexer);
19339 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19341 cp_lexer_consume_token (parser->lexer);
19343 t = cp_parser_assignment_expression (parser, false);
19345 if (t == error_mark_node)
19347 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19348 error ("schedule %<runtime%> does not take "
19349 "a %<chunk_size%> parameter");
19351 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19353 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19356 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19359 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19360 OMP_CLAUSE_CHAIN (c) = list;
19364 cp_parser_error (parser, "invalid schedule kind");
19366 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19367 /*or_comma=*/false,
19368 /*consume_paren=*/true);
19372 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19373 is a bitmask in MASK. Return the list of clauses found; the result
19374 of clause default goes in *pdefault. */
19377 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19378 const char *where, cp_token *pragma_tok)
19380 tree clauses = NULL;
19382 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19384 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19385 const char *c_name;
19386 tree prev = clauses;
19390 case PRAGMA_OMP_CLAUSE_COPYIN:
19391 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19394 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19395 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19397 c_name = "copyprivate";
19399 case PRAGMA_OMP_CLAUSE_DEFAULT:
19400 clauses = cp_parser_omp_clause_default (parser, clauses);
19401 c_name = "default";
19403 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19404 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19406 c_name = "firstprivate";
19408 case PRAGMA_OMP_CLAUSE_IF:
19409 clauses = cp_parser_omp_clause_if (parser, clauses);
19412 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19413 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19415 c_name = "lastprivate";
19417 case PRAGMA_OMP_CLAUSE_NOWAIT:
19418 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19421 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19422 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19423 c_name = "num_threads";
19425 case PRAGMA_OMP_CLAUSE_ORDERED:
19426 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19427 c_name = "ordered";
19429 case PRAGMA_OMP_CLAUSE_PRIVATE:
19430 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19432 c_name = "private";
19434 case PRAGMA_OMP_CLAUSE_REDUCTION:
19435 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19436 c_name = "reduction";
19438 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19439 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19440 c_name = "schedule";
19442 case PRAGMA_OMP_CLAUSE_SHARED:
19443 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19448 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19452 if (((mask >> c_kind) & 1) == 0)
19454 /* Remove the invalid clause(s) from the list to avoid
19455 confusing the rest of the compiler. */
19457 error ("%qs is not valid for %qs", c_name, where);
19461 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19462 return finish_omp_clauses (clauses);
19469 In practice, we're also interested in adding the statement to an
19470 outer node. So it is convenient if we work around the fact that
19471 cp_parser_statement calls add_stmt. */
19474 cp_parser_begin_omp_structured_block (cp_parser *parser)
19476 unsigned save = parser->in_statement;
19478 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19479 This preserves the "not within loop or switch" style error messages
19480 for nonsense cases like
19486 if (parser->in_statement)
19487 parser->in_statement = IN_OMP_BLOCK;
19493 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19495 parser->in_statement = save;
19499 cp_parser_omp_structured_block (cp_parser *parser)
19501 tree stmt = begin_omp_structured_block ();
19502 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19504 cp_parser_statement (parser, NULL_TREE, false, NULL);
19506 cp_parser_end_omp_structured_block (parser, save);
19507 return finish_omp_structured_block (stmt);
19511 # pragma omp atomic new-line
19515 x binop= expr | x++ | ++x | x-- | --x
19517 +, *, -, /, &, ^, |, <<, >>
19519 where x is an lvalue expression with scalar type. */
19522 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19525 enum tree_code code;
19527 cp_parser_require_pragma_eol (parser, pragma_tok);
19529 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19531 switch (TREE_CODE (lhs))
19536 case PREINCREMENT_EXPR:
19537 case POSTINCREMENT_EXPR:
19538 lhs = TREE_OPERAND (lhs, 0);
19540 rhs = integer_one_node;
19543 case PREDECREMENT_EXPR:
19544 case POSTDECREMENT_EXPR:
19545 lhs = TREE_OPERAND (lhs, 0);
19547 rhs = integer_one_node;
19551 switch (cp_lexer_peek_token (parser->lexer)->type)
19557 code = TRUNC_DIV_EXPR;
19565 case CPP_LSHIFT_EQ:
19566 code = LSHIFT_EXPR;
19568 case CPP_RSHIFT_EQ:
19569 code = RSHIFT_EXPR;
19572 code = BIT_AND_EXPR;
19575 code = BIT_IOR_EXPR;
19578 code = BIT_XOR_EXPR;
19581 cp_parser_error (parser,
19582 "invalid operator for %<#pragma omp atomic%>");
19585 cp_lexer_consume_token (parser->lexer);
19587 rhs = cp_parser_expression (parser, false);
19588 if (rhs == error_mark_node)
19592 finish_omp_atomic (code, lhs, rhs);
19593 cp_parser_consume_semicolon_at_end_of_statement (parser);
19597 cp_parser_skip_to_end_of_block_or_statement (parser);
19602 # pragma omp barrier new-line */
19605 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19607 cp_parser_require_pragma_eol (parser, pragma_tok);
19608 finish_omp_barrier ();
19612 # pragma omp critical [(name)] new-line
19613 structured-block */
19616 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19618 tree stmt, name = NULL;
19620 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19622 cp_lexer_consume_token (parser->lexer);
19624 name = cp_parser_identifier (parser);
19626 if (name == error_mark_node
19627 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19628 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19629 /*or_comma=*/false,
19630 /*consume_paren=*/true);
19631 if (name == error_mark_node)
19634 cp_parser_require_pragma_eol (parser, pragma_tok);
19636 stmt = cp_parser_omp_structured_block (parser);
19637 return c_finish_omp_critical (stmt, name);
19641 # pragma omp flush flush-vars[opt] new-line
19644 ( variable-list ) */
19647 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19649 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19650 (void) cp_parser_omp_var_list (parser, 0, NULL);
19651 cp_parser_require_pragma_eol (parser, pragma_tok);
19653 finish_omp_flush ();
19656 /* Parse the restricted form of the for statment allowed by OpenMP. */
19659 cp_parser_omp_for_loop (cp_parser *parser)
19661 tree init, cond, incr, body, decl, pre_body;
19664 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19666 cp_parser_error (parser, "for statement expected");
19669 loc = cp_lexer_consume_token (parser->lexer)->location;
19670 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19673 init = decl = NULL;
19674 pre_body = push_stmt_list ();
19675 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19677 cp_decl_specifier_seq type_specifiers;
19679 /* First, try to parse as an initialized declaration. See
19680 cp_parser_condition, from whence the bulk of this is copied. */
19682 cp_parser_parse_tentatively (parser);
19683 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19685 if (!cp_parser_error_occurred (parser))
19687 tree asm_specification, attributes;
19688 cp_declarator *declarator;
19690 declarator = cp_parser_declarator (parser,
19691 CP_PARSER_DECLARATOR_NAMED,
19692 /*ctor_dtor_or_conv_p=*/NULL,
19693 /*parenthesized_p=*/NULL,
19694 /*member_p=*/false);
19695 attributes = cp_parser_attributes_opt (parser);
19696 asm_specification = cp_parser_asm_specification_opt (parser);
19698 cp_parser_require (parser, CPP_EQ, "`='");
19699 if (cp_parser_parse_definitely (parser))
19703 decl = start_decl (declarator, &type_specifiers,
19704 /*initialized_p=*/false, attributes,
19705 /*prefix_attributes=*/NULL_TREE,
19708 init = cp_parser_assignment_expression (parser, false);
19710 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19711 asm_specification, LOOKUP_ONLYCONVERTING);
19714 pop_scope (pushed_scope);
19718 cp_parser_abort_tentative_parse (parser);
19720 /* If parsing as an initialized declaration failed, try again as
19721 a simple expression. */
19723 init = cp_parser_expression (parser, false);
19725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19726 pre_body = pop_stmt_list (pre_body);
19729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19730 cond = cp_parser_condition (parser);
19731 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19734 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19735 incr = cp_parser_expression (parser, false);
19737 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19738 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19739 /*or_comma=*/false,
19740 /*consume_paren=*/true);
19742 /* Note that we saved the original contents of this flag when we entered
19743 the structured block, and so we don't need to re-save it here. */
19744 parser->in_statement = IN_OMP_FOR;
19746 /* Note that the grammar doesn't call for a structured block here,
19747 though the loop as a whole is a structured block. */
19748 body = push_stmt_list ();
19749 cp_parser_statement (parser, NULL_TREE, false, NULL);
19750 body = pop_stmt_list (body);
19752 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19756 #pragma omp for for-clause[optseq] new-line
19759 #define OMP_FOR_CLAUSE_MASK \
19760 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19761 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19762 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19763 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19764 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19765 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19766 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19769 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19771 tree clauses, sb, ret;
19774 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19775 "#pragma omp for", pragma_tok);
19777 sb = begin_omp_structured_block ();
19778 save = cp_parser_begin_omp_structured_block (parser);
19780 ret = cp_parser_omp_for_loop (parser);
19782 OMP_FOR_CLAUSES (ret) = clauses;
19784 cp_parser_end_omp_structured_block (parser, save);
19785 add_stmt (finish_omp_structured_block (sb));
19791 # pragma omp master new-line
19792 structured-block */
19795 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19797 cp_parser_require_pragma_eol (parser, pragma_tok);
19798 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19802 # pragma omp ordered new-line
19803 structured-block */
19806 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19808 cp_parser_require_pragma_eol (parser, pragma_tok);
19809 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19815 { section-sequence }
19818 section-directive[opt] structured-block
19819 section-sequence section-directive structured-block */
19822 cp_parser_omp_sections_scope (cp_parser *parser)
19824 tree stmt, substmt;
19825 bool error_suppress = false;
19828 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19831 stmt = push_stmt_list ();
19833 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19837 substmt = begin_omp_structured_block ();
19838 save = cp_parser_begin_omp_structured_block (parser);
19842 cp_parser_statement (parser, NULL_TREE, false, NULL);
19844 tok = cp_lexer_peek_token (parser->lexer);
19845 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19847 if (tok->type == CPP_CLOSE_BRACE)
19849 if (tok->type == CPP_EOF)
19853 cp_parser_end_omp_structured_block (parser, save);
19854 substmt = finish_omp_structured_block (substmt);
19855 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19856 add_stmt (substmt);
19861 tok = cp_lexer_peek_token (parser->lexer);
19862 if (tok->type == CPP_CLOSE_BRACE)
19864 if (tok->type == CPP_EOF)
19867 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19869 cp_lexer_consume_token (parser->lexer);
19870 cp_parser_require_pragma_eol (parser, tok);
19871 error_suppress = false;
19873 else if (!error_suppress)
19875 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19876 error_suppress = true;
19879 substmt = cp_parser_omp_structured_block (parser);
19880 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19881 add_stmt (substmt);
19883 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19885 substmt = pop_stmt_list (stmt);
19887 stmt = make_node (OMP_SECTIONS);
19888 TREE_TYPE (stmt) = void_type_node;
19889 OMP_SECTIONS_BODY (stmt) = substmt;
19896 # pragma omp sections sections-clause[optseq] newline
19899 #define OMP_SECTIONS_CLAUSE_MASK \
19900 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19901 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19902 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19903 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19904 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19907 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19911 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19912 "#pragma omp sections", pragma_tok);
19914 ret = cp_parser_omp_sections_scope (parser);
19916 OMP_SECTIONS_CLAUSES (ret) = clauses;
19922 # pragma parallel parallel-clause new-line
19923 # pragma parallel for parallel-for-clause new-line
19924 # pragma parallel sections parallel-sections-clause new-line */
19926 #define OMP_PARALLEL_CLAUSE_MASK \
19927 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19928 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19929 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19930 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19931 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19932 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19933 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19934 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19937 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19939 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19940 const char *p_name = "#pragma omp parallel";
19941 tree stmt, clauses, par_clause, ws_clause, block;
19942 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19945 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19947 cp_lexer_consume_token (parser->lexer);
19948 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19949 p_name = "#pragma omp parallel for";
19950 mask |= OMP_FOR_CLAUSE_MASK;
19951 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19953 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19955 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19956 const char *p = IDENTIFIER_POINTER (id);
19957 if (strcmp (p, "sections") == 0)
19959 cp_lexer_consume_token (parser->lexer);
19960 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19961 p_name = "#pragma omp parallel sections";
19962 mask |= OMP_SECTIONS_CLAUSE_MASK;
19963 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19967 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19968 block = begin_omp_parallel ();
19969 save = cp_parser_begin_omp_structured_block (parser);
19973 case PRAGMA_OMP_PARALLEL:
19974 cp_parser_already_scoped_statement (parser);
19975 par_clause = clauses;
19978 case PRAGMA_OMP_PARALLEL_FOR:
19979 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19980 stmt = cp_parser_omp_for_loop (parser);
19982 OMP_FOR_CLAUSES (stmt) = ws_clause;
19985 case PRAGMA_OMP_PARALLEL_SECTIONS:
19986 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19987 stmt = cp_parser_omp_sections_scope (parser);
19989 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19993 gcc_unreachable ();
19996 cp_parser_end_omp_structured_block (parser, save);
19997 stmt = finish_omp_parallel (par_clause, block);
19998 if (p_kind != PRAGMA_OMP_PARALLEL)
19999 OMP_PARALLEL_COMBINED (stmt) = 1;
20004 # pragma omp single single-clause[optseq] new-line
20005 structured-block */
20007 #define OMP_SINGLE_CLAUSE_MASK \
20008 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20009 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20010 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20011 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20014 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20016 tree stmt = make_node (OMP_SINGLE);
20017 TREE_TYPE (stmt) = void_type_node;
20019 OMP_SINGLE_CLAUSES (stmt)
20020 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20021 "#pragma omp single", pragma_tok);
20022 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20024 return add_stmt (stmt);
20028 # pragma omp threadprivate (variable-list) */
20031 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20035 vars = cp_parser_omp_var_list (parser, 0, NULL);
20036 cp_parser_require_pragma_eol (parser, pragma_tok);
20038 finish_omp_threadprivate (vars);
20041 /* Main entry point to OpenMP statement pragmas. */
20044 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20048 switch (pragma_tok->pragma_kind)
20050 case PRAGMA_OMP_ATOMIC:
20051 cp_parser_omp_atomic (parser, pragma_tok);
20053 case PRAGMA_OMP_CRITICAL:
20054 stmt = cp_parser_omp_critical (parser, pragma_tok);
20056 case PRAGMA_OMP_FOR:
20057 stmt = cp_parser_omp_for (parser, pragma_tok);
20059 case PRAGMA_OMP_MASTER:
20060 stmt = cp_parser_omp_master (parser, pragma_tok);
20062 case PRAGMA_OMP_ORDERED:
20063 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20065 case PRAGMA_OMP_PARALLEL:
20066 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20068 case PRAGMA_OMP_SECTIONS:
20069 stmt = cp_parser_omp_sections (parser, pragma_tok);
20071 case PRAGMA_OMP_SINGLE:
20072 stmt = cp_parser_omp_single (parser, pragma_tok);
20075 gcc_unreachable ();
20079 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20084 static GTY (()) cp_parser *the_parser;
20087 /* Special handling for the first token or line in the file. The first
20088 thing in the file might be #pragma GCC pch_preprocess, which loads a
20089 PCH file, which is a GC collection point. So we need to handle this
20090 first pragma without benefit of an existing lexer structure.
20092 Always returns one token to the caller in *FIRST_TOKEN. This is
20093 either the true first token of the file, or the first token after
20094 the initial pragma. */
20097 cp_parser_initial_pragma (cp_token *first_token)
20101 cp_lexer_get_preprocessor_token (NULL, first_token);
20102 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20105 cp_lexer_get_preprocessor_token (NULL, first_token);
20106 if (first_token->type == CPP_STRING)
20108 name = first_token->u.value;
20110 cp_lexer_get_preprocessor_token (NULL, first_token);
20111 if (first_token->type != CPP_PRAGMA_EOL)
20112 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20115 error ("expected string literal");
20117 /* Skip to the end of the pragma. */
20118 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20119 cp_lexer_get_preprocessor_token (NULL, first_token);
20121 /* Now actually load the PCH file. */
20123 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20125 /* Read one more token to return to our caller. We have to do this
20126 after reading the PCH file in, since its pointers have to be
20128 cp_lexer_get_preprocessor_token (NULL, first_token);
20131 /* Normal parsing of a pragma token. Here we can (and must) use the
20135 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20137 cp_token *pragma_tok;
20140 pragma_tok = cp_lexer_consume_token (parser->lexer);
20141 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20142 parser->lexer->in_pragma = true;
20144 id = pragma_tok->pragma_kind;
20147 case PRAGMA_GCC_PCH_PREPROCESS:
20148 error ("%<#pragma GCC pch_preprocess%> must be first");
20151 case PRAGMA_OMP_BARRIER:
20154 case pragma_compound:
20155 cp_parser_omp_barrier (parser, pragma_tok);
20158 error ("%<#pragma omp barrier%> may only be "
20159 "used in compound statements");
20166 case PRAGMA_OMP_FLUSH:
20169 case pragma_compound:
20170 cp_parser_omp_flush (parser, pragma_tok);
20173 error ("%<#pragma omp flush%> may only be "
20174 "used in compound statements");
20181 case PRAGMA_OMP_THREADPRIVATE:
20182 cp_parser_omp_threadprivate (parser, pragma_tok);
20185 case PRAGMA_OMP_ATOMIC:
20186 case PRAGMA_OMP_CRITICAL:
20187 case PRAGMA_OMP_FOR:
20188 case PRAGMA_OMP_MASTER:
20189 case PRAGMA_OMP_ORDERED:
20190 case PRAGMA_OMP_PARALLEL:
20191 case PRAGMA_OMP_SECTIONS:
20192 case PRAGMA_OMP_SINGLE:
20193 if (context == pragma_external)
20195 cp_parser_omp_construct (parser, pragma_tok);
20198 case PRAGMA_OMP_SECTION:
20199 error ("%<#pragma omp section%> may only be used in "
20200 "%<#pragma omp sections%> construct");
20204 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20205 c_invoke_pragma_handler (id);
20209 cp_parser_error (parser, "expected declaration specifiers");
20213 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20217 /* The interface the pragma parsers have to the lexer. */
20220 pragma_lex (tree *value)
20223 enum cpp_ttype ret;
20225 tok = cp_lexer_peek_token (the_parser->lexer);
20228 *value = tok->u.value;
20230 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20232 else if (ret == CPP_STRING)
20233 *value = cp_parser_string_literal (the_parser, false, false);
20236 cp_lexer_consume_token (the_parser->lexer);
20237 if (ret == CPP_KEYWORD)
20245 /* External interface. */
20247 /* Parse one entire translation unit. */
20250 c_parse_file (void)
20252 bool error_occurred;
20253 static bool already_called = false;
20255 if (already_called)
20257 sorry ("inter-module optimizations not implemented for C++");
20260 already_called = true;
20262 the_parser = cp_parser_new ();
20263 push_deferring_access_checks (flag_access_control
20264 ? dk_no_deferred : dk_no_check);
20265 error_occurred = cp_parser_translation_unit (the_parser);
20269 #include "gt-cp-parser.h"