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 == INDIRECT_REF)
2706 if (class_type == NULL_TREE)
2707 return make_pointer_declarator (cv_qualifiers, target);
2709 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2710 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2711 return make_reference_declarator (cv_qualifiers, target, false);
2712 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2713 return make_reference_declarator (cv_qualifiers, target, true);
2717 /* Create a new C++ parser. */
2720 cp_parser_new (void)
2726 /* cp_lexer_new_main is called before calling ggc_alloc because
2727 cp_lexer_new_main might load a PCH file. */
2728 lexer = cp_lexer_new_main ();
2730 /* Initialize the binops_by_token so that we can get the tree
2731 directly from the token. */
2732 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2733 binops_by_token[binops[i].token_type] = binops[i];
2735 parser = GGC_CNEW (cp_parser);
2736 parser->lexer = lexer;
2737 parser->context = cp_parser_context_new (NULL);
2739 /* For now, we always accept GNU extensions. */
2740 parser->allow_gnu_extensions_p = 1;
2742 /* The `>' token is a greater-than operator, not the end of a
2744 parser->greater_than_is_operator_p = true;
2746 parser->default_arg_ok_p = true;
2748 /* We are not parsing a constant-expression. */
2749 parser->integral_constant_expression_p = false;
2750 parser->allow_non_integral_constant_expression_p = false;
2751 parser->non_integral_constant_expression_p = false;
2753 /* Local variable names are not forbidden. */
2754 parser->local_variables_forbidden_p = false;
2756 /* We are not processing an `extern "C"' declaration. */
2757 parser->in_unbraced_linkage_specification_p = false;
2759 /* We are not processing a declarator. */
2760 parser->in_declarator_p = false;
2762 /* We are not processing a template-argument-list. */
2763 parser->in_template_argument_list_p = false;
2765 /* We are not in an iteration statement. */
2766 parser->in_statement = 0;
2768 /* We are not in a switch statement. */
2769 parser->in_switch_statement_p = false;
2771 /* We are not parsing a type-id inside an expression. */
2772 parser->in_type_id_in_expr_p = false;
2774 /* Declarations aren't implicitly extern "C". */
2775 parser->implicit_extern_c = false;
2777 /* String literals should be translated to the execution character set. */
2778 parser->translate_strings_p = true;
2780 /* We are not parsing a function body. */
2781 parser->in_function_body = false;
2783 /* The unparsed function queue is empty. */
2784 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2786 /* There are no classes being defined. */
2787 parser->num_classes_being_defined = 0;
2789 /* No template parameters apply. */
2790 parser->num_template_parameter_lists = 0;
2795 /* Create a cp_lexer structure which will emit the tokens in CACHE
2796 and push it onto the parser's lexer stack. This is used for delayed
2797 parsing of in-class method bodies and default arguments, and should
2798 not be confused with tentative parsing. */
2800 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2802 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2803 lexer->next = parser->lexer;
2804 parser->lexer = lexer;
2806 /* Move the current source position to that of the first token in the
2808 cp_lexer_set_source_position_from_token (lexer->next_token);
2811 /* Pop the top lexer off the parser stack. This is never used for the
2812 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2814 cp_parser_pop_lexer (cp_parser *parser)
2816 cp_lexer *lexer = parser->lexer;
2817 parser->lexer = lexer->next;
2818 cp_lexer_destroy (lexer);
2820 /* Put the current source position back where it was before this
2821 lexer was pushed. */
2822 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2825 /* Lexical conventions [gram.lex] */
2827 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2831 cp_parser_identifier (cp_parser* parser)
2835 /* Look for the identifier. */
2836 token = cp_parser_require (parser, CPP_NAME, "identifier");
2837 /* Return the value. */
2838 return token ? token->u.value : error_mark_node;
2841 /* Parse a sequence of adjacent string constants. Returns a
2842 TREE_STRING representing the combined, nul-terminated string
2843 constant. If TRANSLATE is true, translate the string to the
2844 execution character set. If WIDE_OK is true, a wide string is
2847 C++98 [lex.string] says that if a narrow string literal token is
2848 adjacent to a wide string literal token, the behavior is undefined.
2849 However, C99 6.4.5p4 says that this results in a wide string literal.
2850 We follow C99 here, for consistency with the C front end.
2852 This code is largely lifted from lex_string() in c-lex.c.
2854 FUTURE: ObjC++ will need to handle @-strings here. */
2856 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2861 struct obstack str_ob;
2862 cpp_string str, istr, *strs;
2865 tok = cp_lexer_peek_token (parser->lexer);
2866 if (!cp_parser_is_string_literal (tok))
2868 cp_parser_error (parser, "expected string-literal");
2869 return error_mark_node;
2872 /* Try to avoid the overhead of creating and destroying an obstack
2873 for the common case of just one string. */
2874 if (!cp_parser_is_string_literal
2875 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2877 cp_lexer_consume_token (parser->lexer);
2879 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2880 str.len = TREE_STRING_LENGTH (tok->u.value);
2882 if (tok->type == CPP_WSTRING)
2889 gcc_obstack_init (&str_ob);
2894 cp_lexer_consume_token (parser->lexer);
2896 str.text = (unsigned char *)TREE_STRING_POINTER (tok->u.value);
2897 str.len = TREE_STRING_LENGTH (tok->u.value);
2898 if (tok->type == CPP_WSTRING)
2901 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2903 tok = cp_lexer_peek_token (parser->lexer);
2905 while (cp_parser_is_string_literal (tok));
2907 strs = (cpp_string *) obstack_finish (&str_ob);
2910 if (wide && !wide_ok)
2912 cp_parser_error (parser, "a wide string is invalid in this context");
2916 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2917 (parse_in, strs, count, &istr, wide))
2919 value = build_string (istr.len, (char *)istr.text);
2920 free ((void *)istr.text);
2922 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2923 value = fix_string_type (value);
2926 /* cpp_interpret_string has issued an error. */
2927 value = error_mark_node;
2930 obstack_free (&str_ob, 0);
2936 /* Basic concepts [gram.basic] */
2938 /* Parse a translation-unit.
2941 declaration-seq [opt]
2943 Returns TRUE if all went well. */
2946 cp_parser_translation_unit (cp_parser* parser)
2948 /* The address of the first non-permanent object on the declarator
2950 static void *declarator_obstack_base;
2954 /* Create the declarator obstack, if necessary. */
2955 if (!cp_error_declarator)
2957 gcc_obstack_init (&declarator_obstack);
2958 /* Create the error declarator. */
2959 cp_error_declarator = make_declarator (cdk_error);
2960 /* Create the empty parameter list. */
2961 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2962 /* Remember where the base of the declarator obstack lies. */
2963 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2966 cp_parser_declaration_seq_opt (parser);
2968 /* If there are no tokens left then all went well. */
2969 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2971 /* Get rid of the token array; we don't need it any more. */
2972 cp_lexer_destroy (parser->lexer);
2973 parser->lexer = NULL;
2975 /* This file might have been a context that's implicitly extern
2976 "C". If so, pop the lang context. (Only relevant for PCH.) */
2977 if (parser->implicit_extern_c)
2979 pop_lang_context ();
2980 parser->implicit_extern_c = false;
2984 finish_translation_unit ();
2990 cp_parser_error (parser, "expected declaration");
2994 /* Make sure the declarator obstack was fully cleaned up. */
2995 gcc_assert (obstack_next_free (&declarator_obstack)
2996 == declarator_obstack_base);
2998 /* All went well. */
3002 /* Expressions [gram.expr] */
3004 /* Parse a primary-expression.
3015 ( compound-statement )
3016 __builtin_va_arg ( assignment-expression , type-id )
3017 __builtin_offsetof ( type-id , offsetof-expression )
3020 __has_nothrow_assign ( type-id )
3021 __has_nothrow_constructor ( type-id )
3022 __has_nothrow_copy ( type-id )
3023 __has_trivial_assign ( type-id )
3024 __has_trivial_constructor ( type-id )
3025 __has_trivial_copy ( type-id )
3026 __has_trivial_destructor ( type-id )
3027 __has_virtual_destructor ( type-id )
3028 __is_abstract ( type-id )
3029 __is_base_of ( type-id , type-id )
3030 __is_class ( type-id )
3031 __is_convertible_to ( type-id , type-id )
3032 __is_empty ( type-id )
3033 __is_enum ( type-id )
3034 __is_pod ( type-id )
3035 __is_polymorphic ( type-id )
3036 __is_union ( type-id )
3038 Objective-C++ Extension:
3046 ADDRESS_P is true iff this expression was immediately preceded by
3047 "&" and therefore might denote a pointer-to-member. CAST_P is true
3048 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3049 true iff this expression is a template argument.
3051 Returns a representation of the expression. Upon return, *IDK
3052 indicates what kind of id-expression (if any) was present. */
3055 cp_parser_primary_expression (cp_parser *parser,
3058 bool template_arg_p,
3063 /* Assume the primary expression is not an id-expression. */
3064 *idk = CP_ID_KIND_NONE;
3066 /* Peek at the next token. */
3067 token = cp_lexer_peek_token (parser->lexer);
3068 switch (token->type)
3079 token = cp_lexer_consume_token (parser->lexer);
3080 /* Floating-point literals are only allowed in an integral
3081 constant expression if they are cast to an integral or
3082 enumeration type. */
3083 if (TREE_CODE (token->u.value) == REAL_CST
3084 && parser->integral_constant_expression_p
3087 /* CAST_P will be set even in invalid code like "int(2.7 +
3088 ...)". Therefore, we have to check that the next token
3089 is sure to end the cast. */
3092 cp_token *next_token;
3094 next_token = cp_lexer_peek_token (parser->lexer);
3095 if (/* The comma at the end of an
3096 enumerator-definition. */
3097 next_token->type != CPP_COMMA
3098 /* The curly brace at the end of an enum-specifier. */
3099 && next_token->type != CPP_CLOSE_BRACE
3100 /* The end of a statement. */
3101 && next_token->type != CPP_SEMICOLON
3102 /* The end of the cast-expression. */
3103 && next_token->type != CPP_CLOSE_PAREN
3104 /* The end of an array bound. */
3105 && next_token->type != CPP_CLOSE_SQUARE
3106 /* The closing ">" in a template-argument-list. */
3107 && (next_token->type != CPP_GREATER
3108 || parser->greater_than_is_operator_p)
3109 /* C++0x only: A ">>" treated like two ">" tokens,
3110 in a template-argument-list. */
3111 && (next_token->type != CPP_RSHIFT
3112 || (cxx_dialect == cxx98)
3113 || parser->greater_than_is_operator_p))
3117 /* If we are within a cast, then the constraint that the
3118 cast is to an integral or enumeration type will be
3119 checked at that point. If we are not within a cast, then
3120 this code is invalid. */
3122 cp_parser_non_integral_constant_expression
3123 (parser, "floating-point literal");
3125 return token->u.value;
3129 /* ??? Should wide strings be allowed when parser->translate_strings_p
3130 is false (i.e. in attributes)? If not, we can kill the third
3131 argument to cp_parser_string_literal. */
3132 return cp_parser_string_literal (parser,
3133 parser->translate_strings_p,
3136 case CPP_OPEN_PAREN:
3139 bool saved_greater_than_is_operator_p;
3141 /* Consume the `('. */
3142 cp_lexer_consume_token (parser->lexer);
3143 /* Within a parenthesized expression, a `>' token is always
3144 the greater-than operator. */
3145 saved_greater_than_is_operator_p
3146 = parser->greater_than_is_operator_p;
3147 parser->greater_than_is_operator_p = true;
3148 /* If we see `( { ' then we are looking at the beginning of
3149 a GNU statement-expression. */
3150 if (cp_parser_allow_gnu_extensions_p (parser)
3151 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3153 /* Statement-expressions are not allowed by the standard. */
3155 pedwarn ("ISO C++ forbids braced-groups within expressions");
3157 /* And they're not allowed outside of a function-body; you
3158 cannot, for example, write:
3160 int i = ({ int j = 3; j + 1; });
3162 at class or namespace scope. */
3163 if (!parser->in_function_body)
3165 error ("statement-expressions are allowed only inside functions");
3166 cp_parser_skip_to_end_of_block_or_statement (parser);
3167 expr = error_mark_node;
3171 /* Start the statement-expression. */
3172 expr = begin_stmt_expr ();
3173 /* Parse the compound-statement. */
3174 cp_parser_compound_statement (parser, expr, false);
3176 expr = finish_stmt_expr (expr, false);
3181 /* Parse the parenthesized expression. */
3182 expr = cp_parser_expression (parser, cast_p);
3183 /* Let the front end know that this expression was
3184 enclosed in parentheses. This matters in case, for
3185 example, the expression is of the form `A::B', since
3186 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3188 finish_parenthesized_expr (expr);
3190 /* The `>' token might be the end of a template-id or
3191 template-parameter-list now. */
3192 parser->greater_than_is_operator_p
3193 = saved_greater_than_is_operator_p;
3194 /* Consume the `)'. */
3195 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3196 cp_parser_skip_to_end_of_statement (parser);
3202 switch (token->keyword)
3204 /* These two are the boolean literals. */
3206 cp_lexer_consume_token (parser->lexer);
3207 return boolean_true_node;
3209 cp_lexer_consume_token (parser->lexer);
3210 return boolean_false_node;
3212 /* The `__null' literal. */
3214 cp_lexer_consume_token (parser->lexer);
3217 /* Recognize the `this' keyword. */
3219 cp_lexer_consume_token (parser->lexer);
3220 if (parser->local_variables_forbidden_p)
3222 error ("%<this%> may not be used in this context");
3223 return error_mark_node;
3225 /* Pointers cannot appear in constant-expressions. */
3226 if (cp_parser_non_integral_constant_expression (parser,
3228 return error_mark_node;
3229 return finish_this_expr ();
3231 /* The `operator' keyword can be the beginning of an
3236 case RID_FUNCTION_NAME:
3237 case RID_PRETTY_FUNCTION_NAME:
3238 case RID_C99_FUNCTION_NAME:
3239 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3240 __func__ are the names of variables -- but they are
3241 treated specially. Therefore, they are handled here,
3242 rather than relying on the generic id-expression logic
3243 below. Grammatically, these names are id-expressions.
3245 Consume the token. */
3246 token = cp_lexer_consume_token (parser->lexer);
3247 /* Look up the name. */
3248 return finish_fname (token->u.value);
3255 /* The `__builtin_va_arg' construct is used to handle
3256 `va_arg'. Consume the `__builtin_va_arg' token. */
3257 cp_lexer_consume_token (parser->lexer);
3258 /* Look for the opening `('. */
3259 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3260 /* Now, parse the assignment-expression. */
3261 expression = cp_parser_assignment_expression (parser,
3263 /* Look for the `,'. */
3264 cp_parser_require (parser, CPP_COMMA, "`,'");
3265 /* Parse the type-id. */
3266 type = cp_parser_type_id (parser);
3267 /* Look for the closing `)'. */
3268 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3269 /* Using `va_arg' in a constant-expression is not
3271 if (cp_parser_non_integral_constant_expression (parser,
3273 return error_mark_node;
3274 return build_x_va_arg (expression, type);
3278 return cp_parser_builtin_offsetof (parser);
3280 case RID_HAS_NOTHROW_ASSIGN:
3281 case RID_HAS_NOTHROW_CONSTRUCTOR:
3282 case RID_HAS_NOTHROW_COPY:
3283 case RID_HAS_TRIVIAL_ASSIGN:
3284 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3285 case RID_HAS_TRIVIAL_COPY:
3286 case RID_HAS_TRIVIAL_DESTRUCTOR:
3287 case RID_HAS_VIRTUAL_DESTRUCTOR:
3288 case RID_IS_ABSTRACT:
3289 case RID_IS_BASE_OF:
3291 case RID_IS_CONVERTIBLE_TO:
3295 case RID_IS_POLYMORPHIC:
3297 return cp_parser_trait_expr (parser, token->keyword);
3299 /* Objective-C++ expressions. */
3301 case RID_AT_PROTOCOL:
3302 case RID_AT_SELECTOR:
3303 return cp_parser_objc_expression (parser);
3306 cp_parser_error (parser, "expected primary-expression");
3307 return error_mark_node;
3310 /* An id-expression can start with either an identifier, a
3311 `::' as the beginning of a qualified-id, or the "operator"
3315 case CPP_TEMPLATE_ID:
3316 case CPP_NESTED_NAME_SPECIFIER:
3320 const char *error_msg;
3325 /* Parse the id-expression. */
3327 = cp_parser_id_expression (parser,
3328 /*template_keyword_p=*/false,
3329 /*check_dependency_p=*/true,
3331 /*declarator_p=*/false,
3332 /*optional_p=*/false);
3333 if (id_expression == error_mark_node)
3334 return error_mark_node;
3335 token = cp_lexer_peek_token (parser->lexer);
3336 done = (token->type != CPP_OPEN_SQUARE
3337 && token->type != CPP_OPEN_PAREN
3338 && token->type != CPP_DOT
3339 && token->type != CPP_DEREF
3340 && token->type != CPP_PLUS_PLUS
3341 && token->type != CPP_MINUS_MINUS);
3342 /* If we have a template-id, then no further lookup is
3343 required. If the template-id was for a template-class, we
3344 will sometimes have a TYPE_DECL at this point. */
3345 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3346 || TREE_CODE (id_expression) == TYPE_DECL)
3347 decl = id_expression;
3348 /* Look up the name. */
3351 tree ambiguous_decls;
3353 decl = cp_parser_lookup_name (parser, id_expression,
3356 /*is_namespace=*/false,
3357 /*check_dependency=*/true,
3359 /* If the lookup was ambiguous, an error will already have
3361 if (ambiguous_decls)
3362 return error_mark_node;
3364 /* In Objective-C++, an instance variable (ivar) may be preferred
3365 to whatever cp_parser_lookup_name() found. */
3366 decl = objc_lookup_ivar (decl, id_expression);
3368 /* If name lookup gives us a SCOPE_REF, then the
3369 qualifying scope was dependent. */
3370 if (TREE_CODE (decl) == SCOPE_REF)
3372 /* Check to see if DECL is a local variable in a context
3373 where that is forbidden. */
3374 if (parser->local_variables_forbidden_p
3375 && local_variable_p (decl))
3377 /* It might be that we only found DECL because we are
3378 trying to be generous with pre-ISO scoping rules.
3379 For example, consider:
3383 for (int i = 0; i < 10; ++i) {}
3384 extern void f(int j = i);
3387 Here, name look up will originally find the out
3388 of scope `i'. We need to issue a warning message,
3389 but then use the global `i'. */
3390 decl = check_for_out_of_scope_variable (decl);
3391 if (local_variable_p (decl))
3393 error ("local variable %qD may not appear in this context",
3395 return error_mark_node;
3400 decl = (finish_id_expression
3401 (id_expression, decl, parser->scope,
3403 parser->integral_constant_expression_p,
3404 parser->allow_non_integral_constant_expression_p,
3405 &parser->non_integral_constant_expression_p,
3406 template_p, done, address_p,
3410 cp_parser_error (parser, error_msg);
3414 /* Anything else is an error. */
3416 /* ...unless we have an Objective-C++ message or string literal,
3418 if (c_dialect_objc ()
3419 && (token->type == CPP_OPEN_SQUARE
3420 || token->type == CPP_OBJC_STRING))
3421 return cp_parser_objc_expression (parser);
3423 cp_parser_error (parser, "expected primary-expression");
3424 return error_mark_node;
3428 /* Parse an id-expression.
3435 :: [opt] nested-name-specifier template [opt] unqualified-id
3437 :: operator-function-id
3440 Return a representation of the unqualified portion of the
3441 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3442 a `::' or nested-name-specifier.
3444 Often, if the id-expression was a qualified-id, the caller will
3445 want to make a SCOPE_REF to represent the qualified-id. This
3446 function does not do this in order to avoid wastefully creating
3447 SCOPE_REFs when they are not required.
3449 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3452 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3453 uninstantiated templates.
3455 If *TEMPLATE_P is non-NULL, it is set to true iff the
3456 `template' keyword is used to explicitly indicate that the entity
3457 named is a template.
3459 If DECLARATOR_P is true, the id-expression is appearing as part of
3460 a declarator, rather than as part of an expression. */
3463 cp_parser_id_expression (cp_parser *parser,
3464 bool template_keyword_p,
3465 bool check_dependency_p,
3470 bool global_scope_p;
3471 bool nested_name_specifier_p;
3473 /* Assume the `template' keyword was not used. */
3475 *template_p = template_keyword_p;
3477 /* Look for the optional `::' operator. */
3479 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3481 /* Look for the optional nested-name-specifier. */
3482 nested_name_specifier_p
3483 = (cp_parser_nested_name_specifier_opt (parser,
3484 /*typename_keyword_p=*/false,
3489 /* If there is a nested-name-specifier, then we are looking at
3490 the first qualified-id production. */
3491 if (nested_name_specifier_p)
3494 tree saved_object_scope;
3495 tree saved_qualifying_scope;
3496 tree unqualified_id;
3499 /* See if the next token is the `template' keyword. */
3501 template_p = &is_template;
3502 *template_p = cp_parser_optional_template_keyword (parser);
3503 /* Name lookup we do during the processing of the
3504 unqualified-id might obliterate SCOPE. */
3505 saved_scope = parser->scope;
3506 saved_object_scope = parser->object_scope;
3507 saved_qualifying_scope = parser->qualifying_scope;
3508 /* Process the final unqualified-id. */
3509 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3512 /*optional_p=*/false);
3513 /* Restore the SAVED_SCOPE for our caller. */
3514 parser->scope = saved_scope;
3515 parser->object_scope = saved_object_scope;
3516 parser->qualifying_scope = saved_qualifying_scope;
3518 return unqualified_id;
3520 /* Otherwise, if we are in global scope, then we are looking at one
3521 of the other qualified-id productions. */
3522 else if (global_scope_p)
3527 /* Peek at the next token. */
3528 token = cp_lexer_peek_token (parser->lexer);
3530 /* If it's an identifier, and the next token is not a "<", then
3531 we can avoid the template-id case. This is an optimization
3532 for this common case. */
3533 if (token->type == CPP_NAME
3534 && !cp_parser_nth_token_starts_template_argument_list_p
3536 return cp_parser_identifier (parser);
3538 cp_parser_parse_tentatively (parser);
3539 /* Try a template-id. */
3540 id = cp_parser_template_id (parser,
3541 /*template_keyword_p=*/false,
3542 /*check_dependency_p=*/true,
3544 /* If that worked, we're done. */
3545 if (cp_parser_parse_definitely (parser))
3548 /* Peek at the next token. (Changes in the token buffer may
3549 have invalidated the pointer obtained above.) */
3550 token = cp_lexer_peek_token (parser->lexer);
3552 switch (token->type)
3555 return cp_parser_identifier (parser);
3558 if (token->keyword == RID_OPERATOR)
3559 return cp_parser_operator_function_id (parser);
3563 cp_parser_error (parser, "expected id-expression");
3564 return error_mark_node;
3568 return cp_parser_unqualified_id (parser, template_keyword_p,
3569 /*check_dependency_p=*/true,
3574 /* Parse an unqualified-id.
3578 operator-function-id
3579 conversion-function-id
3583 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3584 keyword, in a construct like `A::template ...'.
3586 Returns a representation of unqualified-id. For the `identifier'
3587 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3588 production a BIT_NOT_EXPR is returned; the operand of the
3589 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3590 other productions, see the documentation accompanying the
3591 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3592 names are looked up in uninstantiated templates. If DECLARATOR_P
3593 is true, the unqualified-id is appearing as part of a declarator,
3594 rather than as part of an expression. */
3597 cp_parser_unqualified_id (cp_parser* parser,
3598 bool template_keyword_p,
3599 bool check_dependency_p,
3605 /* Peek at the next token. */
3606 token = cp_lexer_peek_token (parser->lexer);
3608 switch (token->type)
3614 /* We don't know yet whether or not this will be a
3616 cp_parser_parse_tentatively (parser);
3617 /* Try a template-id. */
3618 id = cp_parser_template_id (parser, template_keyword_p,
3621 /* If it worked, we're done. */
3622 if (cp_parser_parse_definitely (parser))
3624 /* Otherwise, it's an ordinary identifier. */
3625 return cp_parser_identifier (parser);
3628 case CPP_TEMPLATE_ID:
3629 return cp_parser_template_id (parser, template_keyword_p,
3636 tree qualifying_scope;
3641 /* Consume the `~' token. */
3642 cp_lexer_consume_token (parser->lexer);
3643 /* Parse the class-name. The standard, as written, seems to
3646 template <typename T> struct S { ~S (); };
3647 template <typename T> S<T>::~S() {}
3649 is invalid, since `~' must be followed by a class-name, but
3650 `S<T>' is dependent, and so not known to be a class.
3651 That's not right; we need to look in uninstantiated
3652 templates. A further complication arises from:
3654 template <typename T> void f(T t) {
3658 Here, it is not possible to look up `T' in the scope of `T'
3659 itself. We must look in both the current scope, and the
3660 scope of the containing complete expression.
3662 Yet another issue is:
3671 The standard does not seem to say that the `S' in `~S'
3672 should refer to the type `S' and not the data member
3675 /* DR 244 says that we look up the name after the "~" in the
3676 same scope as we looked up the qualifying name. That idea
3677 isn't fully worked out; it's more complicated than that. */
3678 scope = parser->scope;
3679 object_scope = parser->object_scope;
3680 qualifying_scope = parser->qualifying_scope;
3682 /* Check for invalid scopes. */
3683 if (scope == error_mark_node)
3685 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3686 cp_lexer_consume_token (parser->lexer);
3687 return error_mark_node;
3689 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3691 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3692 error ("scope %qT before %<~%> is not a class-name", scope);
3693 cp_parser_simulate_error (parser);
3694 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3695 cp_lexer_consume_token (parser->lexer);
3696 return error_mark_node;
3698 gcc_assert (!scope || TYPE_P (scope));
3700 /* If the name is of the form "X::~X" it's OK. */
3701 token = cp_lexer_peek_token (parser->lexer);
3703 && token->type == CPP_NAME
3704 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3706 && constructor_name_p (token->u.value, scope))
3708 cp_lexer_consume_token (parser->lexer);
3709 return build_nt (BIT_NOT_EXPR, scope);
3712 /* If there was an explicit qualification (S::~T), first look
3713 in the scope given by the qualification (i.e., S). */
3715 type_decl = NULL_TREE;
3718 cp_parser_parse_tentatively (parser);
3719 type_decl = cp_parser_class_name (parser,
3720 /*typename_keyword_p=*/false,
3721 /*template_keyword_p=*/false,
3723 /*check_dependency=*/false,
3724 /*class_head_p=*/false,
3726 if (cp_parser_parse_definitely (parser))
3729 /* In "N::S::~S", look in "N" as well. */
3730 if (!done && scope && qualifying_scope)
3732 cp_parser_parse_tentatively (parser);
3733 parser->scope = qualifying_scope;
3734 parser->object_scope = NULL_TREE;
3735 parser->qualifying_scope = NULL_TREE;
3737 = cp_parser_class_name (parser,
3738 /*typename_keyword_p=*/false,
3739 /*template_keyword_p=*/false,
3741 /*check_dependency=*/false,
3742 /*class_head_p=*/false,
3744 if (cp_parser_parse_definitely (parser))
3747 /* In "p->S::~T", look in the scope given by "*p" as well. */
3748 else if (!done && object_scope)
3750 cp_parser_parse_tentatively (parser);
3751 parser->scope = object_scope;
3752 parser->object_scope = NULL_TREE;
3753 parser->qualifying_scope = NULL_TREE;
3755 = cp_parser_class_name (parser,
3756 /*typename_keyword_p=*/false,
3757 /*template_keyword_p=*/false,
3759 /*check_dependency=*/false,
3760 /*class_head_p=*/false,
3762 if (cp_parser_parse_definitely (parser))
3765 /* Look in the surrounding context. */
3768 parser->scope = NULL_TREE;
3769 parser->object_scope = NULL_TREE;
3770 parser->qualifying_scope = NULL_TREE;
3772 = cp_parser_class_name (parser,
3773 /*typename_keyword_p=*/false,
3774 /*template_keyword_p=*/false,
3776 /*check_dependency=*/false,
3777 /*class_head_p=*/false,
3780 /* If an error occurred, assume that the name of the
3781 destructor is the same as the name of the qualifying
3782 class. That allows us to keep parsing after running
3783 into ill-formed destructor names. */
3784 if (type_decl == error_mark_node && scope)
3785 return build_nt (BIT_NOT_EXPR, scope);
3786 else if (type_decl == error_mark_node)
3787 return error_mark_node;
3789 /* Check that destructor name and scope match. */
3790 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3792 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3793 error ("declaration of %<~%T%> as member of %qT",
3795 cp_parser_simulate_error (parser);
3796 return error_mark_node;
3801 A typedef-name that names a class shall not be used as the
3802 identifier in the declarator for a destructor declaration. */
3804 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3805 && !DECL_SELF_REFERENCE_P (type_decl)
3806 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3807 error ("typedef-name %qD used as destructor declarator",
3810 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3814 if (token->keyword == RID_OPERATOR)
3818 /* This could be a template-id, so we try that first. */
3819 cp_parser_parse_tentatively (parser);
3820 /* Try a template-id. */
3821 id = cp_parser_template_id (parser, template_keyword_p,
3822 /*check_dependency_p=*/true,
3824 /* If that worked, we're done. */
3825 if (cp_parser_parse_definitely (parser))
3827 /* We still don't know whether we're looking at an
3828 operator-function-id or a conversion-function-id. */
3829 cp_parser_parse_tentatively (parser);
3830 /* Try an operator-function-id. */
3831 id = cp_parser_operator_function_id (parser);
3832 /* If that didn't work, try a conversion-function-id. */
3833 if (!cp_parser_parse_definitely (parser))
3834 id = cp_parser_conversion_function_id (parser);
3843 cp_parser_error (parser, "expected unqualified-id");
3844 return error_mark_node;
3848 /* Parse an (optional) nested-name-specifier.
3850 nested-name-specifier:
3851 class-or-namespace-name :: nested-name-specifier [opt]
3852 class-or-namespace-name :: template nested-name-specifier [opt]
3854 PARSER->SCOPE should be set appropriately before this function is
3855 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3856 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3859 Sets PARSER->SCOPE to the class (TYPE) or namespace
3860 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3861 it unchanged if there is no nested-name-specifier. Returns the new
3862 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3864 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3865 part of a declaration and/or decl-specifier. */
3868 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3869 bool typename_keyword_p,
3870 bool check_dependency_p,
3872 bool is_declaration)
3874 bool success = false;
3875 cp_token_position start = 0;
3878 /* Remember where the nested-name-specifier starts. */
3879 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3881 start = cp_lexer_token_position (parser->lexer, false);
3882 push_deferring_access_checks (dk_deferred);
3889 tree saved_qualifying_scope;
3890 bool template_keyword_p;
3892 /* Spot cases that cannot be the beginning of a
3893 nested-name-specifier. */
3894 token = cp_lexer_peek_token (parser->lexer);
3896 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3897 the already parsed nested-name-specifier. */
3898 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3900 /* Grab the nested-name-specifier and continue the loop. */
3901 cp_parser_pre_parsed_nested_name_specifier (parser);
3902 /* If we originally encountered this nested-name-specifier
3903 with IS_DECLARATION set to false, we will not have
3904 resolved TYPENAME_TYPEs, so we must do so here. */
3906 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3908 new_scope = resolve_typename_type (parser->scope,
3909 /*only_current_p=*/false);
3910 if (new_scope != error_mark_node)
3911 parser->scope = new_scope;
3917 /* Spot cases that cannot be the beginning of a
3918 nested-name-specifier. On the second and subsequent times
3919 through the loop, we look for the `template' keyword. */
3920 if (success && token->keyword == RID_TEMPLATE)
3922 /* A template-id can start a nested-name-specifier. */
3923 else if (token->type == CPP_TEMPLATE_ID)
3927 /* If the next token is not an identifier, then it is
3928 definitely not a class-or-namespace-name. */
3929 if (token->type != CPP_NAME)
3931 /* If the following token is neither a `<' (to begin a
3932 template-id), nor a `::', then we are not looking at a
3933 nested-name-specifier. */
3934 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3935 if (token->type != CPP_SCOPE
3936 && !cp_parser_nth_token_starts_template_argument_list_p
3941 /* The nested-name-specifier is optional, so we parse
3943 cp_parser_parse_tentatively (parser);
3945 /* Look for the optional `template' keyword, if this isn't the
3946 first time through the loop. */
3948 template_keyword_p = cp_parser_optional_template_keyword (parser);
3950 template_keyword_p = false;
3952 /* Save the old scope since the name lookup we are about to do
3953 might destroy it. */
3954 old_scope = parser->scope;
3955 saved_qualifying_scope = parser->qualifying_scope;
3956 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3957 look up names in "X<T>::I" in order to determine that "Y" is
3958 a template. So, if we have a typename at this point, we make
3959 an effort to look through it. */
3961 && !typename_keyword_p
3963 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3964 parser->scope = resolve_typename_type (parser->scope,
3965 /*only_current_p=*/false);
3966 /* Parse the qualifying entity. */
3968 = cp_parser_class_or_namespace_name (parser,
3974 /* Look for the `::' token. */
3975 cp_parser_require (parser, CPP_SCOPE, "`::'");
3977 /* If we found what we wanted, we keep going; otherwise, we're
3979 if (!cp_parser_parse_definitely (parser))
3981 bool error_p = false;
3983 /* Restore the OLD_SCOPE since it was valid before the
3984 failed attempt at finding the last
3985 class-or-namespace-name. */
3986 parser->scope = old_scope;
3987 parser->qualifying_scope = saved_qualifying_scope;
3988 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3990 /* If the next token is an identifier, and the one after
3991 that is a `::', then any valid interpretation would have
3992 found a class-or-namespace-name. */
3993 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3994 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3996 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3999 token = cp_lexer_consume_token (parser->lexer);
4002 if (!token->ambiguous_p)
4005 tree ambiguous_decls;
4007 decl = cp_parser_lookup_name (parser, token->u.value,
4009 /*is_template=*/false,
4010 /*is_namespace=*/false,
4011 /*check_dependency=*/true,
4013 if (TREE_CODE (decl) == TEMPLATE_DECL)
4014 error ("%qD used without template parameters", decl);
4015 else if (ambiguous_decls)
4017 error ("reference to %qD is ambiguous",
4019 print_candidates (ambiguous_decls);
4020 decl = error_mark_node;
4023 cp_parser_name_lookup_error
4024 (parser, token->u.value, decl,
4025 "is not a class or namespace");
4027 parser->scope = error_mark_node;
4029 /* Treat this as a successful nested-name-specifier
4034 If the name found is not a class-name (clause
4035 _class_) or namespace-name (_namespace.def_), the
4036 program is ill-formed. */
4039 cp_lexer_consume_token (parser->lexer);
4043 /* We've found one valid nested-name-specifier. */
4045 /* Name lookup always gives us a DECL. */
4046 if (TREE_CODE (new_scope) == TYPE_DECL)
4047 new_scope = TREE_TYPE (new_scope);
4048 /* Uses of "template" must be followed by actual templates. */
4049 if (template_keyword_p
4050 && !(CLASS_TYPE_P (new_scope)
4051 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4052 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4053 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4054 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4055 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4056 == TEMPLATE_ID_EXPR)))
4057 pedwarn (TYPE_P (new_scope)
4058 ? "%qT is not a template"
4059 : "%qD is not a template",
4061 /* If it is a class scope, try to complete it; we are about to
4062 be looking up names inside the class. */
4063 if (TYPE_P (new_scope)
4064 /* Since checking types for dependency can be expensive,
4065 avoid doing it if the type is already complete. */
4066 && !COMPLETE_TYPE_P (new_scope)
4067 /* Do not try to complete dependent types. */
4068 && !dependent_type_p (new_scope))
4069 new_scope = complete_type (new_scope);
4070 /* Make sure we look in the right scope the next time through
4072 parser->scope = new_scope;
4075 /* If parsing tentatively, replace the sequence of tokens that makes
4076 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4077 token. That way, should we re-parse the token stream, we will
4078 not have to repeat the effort required to do the parse, nor will
4079 we issue duplicate error messages. */
4080 if (success && start)
4084 token = cp_lexer_token_at (parser->lexer, start);
4085 /* Reset the contents of the START token. */
4086 token->type = CPP_NESTED_NAME_SPECIFIER;
4087 /* Retrieve any deferred checks. Do not pop this access checks yet
4088 so the memory will not be reclaimed during token replacing below. */
4089 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4090 token->u.tree_check_value->value = parser->scope;
4091 token->u.tree_check_value->checks = get_deferred_access_checks ();
4092 token->u.tree_check_value->qualifying_scope =
4093 parser->qualifying_scope;
4094 token->keyword = RID_MAX;
4096 /* Purge all subsequent tokens. */
4097 cp_lexer_purge_tokens_after (parser->lexer, start);
4101 pop_to_parent_deferring_access_checks ();
4103 return success ? parser->scope : NULL_TREE;
4106 /* Parse a nested-name-specifier. See
4107 cp_parser_nested_name_specifier_opt for details. This function
4108 behaves identically, except that it will an issue an error if no
4109 nested-name-specifier is present. */
4112 cp_parser_nested_name_specifier (cp_parser *parser,
4113 bool typename_keyword_p,
4114 bool check_dependency_p,
4116 bool is_declaration)
4120 /* Look for the nested-name-specifier. */
4121 scope = cp_parser_nested_name_specifier_opt (parser,
4126 /* If it was not present, issue an error message. */
4129 cp_parser_error (parser, "expected nested-name-specifier");
4130 parser->scope = NULL_TREE;
4136 /* Parse a class-or-namespace-name.
4138 class-or-namespace-name:
4142 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4143 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4144 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4145 TYPE_P is TRUE iff the next name should be taken as a class-name,
4146 even the same name is declared to be another entity in the same
4149 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4150 specified by the class-or-namespace-name. If neither is found the
4151 ERROR_MARK_NODE is returned. */
4154 cp_parser_class_or_namespace_name (cp_parser *parser,
4155 bool typename_keyword_p,
4156 bool template_keyword_p,
4157 bool check_dependency_p,
4159 bool is_declaration)
4162 tree saved_qualifying_scope;
4163 tree saved_object_scope;
4167 /* Before we try to parse the class-name, we must save away the
4168 current PARSER->SCOPE since cp_parser_class_name will destroy
4170 saved_scope = parser->scope;
4171 saved_qualifying_scope = parser->qualifying_scope;
4172 saved_object_scope = parser->object_scope;
4173 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4174 there is no need to look for a namespace-name. */
4175 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4177 cp_parser_parse_tentatively (parser);
4178 scope = cp_parser_class_name (parser,
4181 type_p ? class_type : none_type,
4183 /*class_head_p=*/false,
4185 /* If that didn't work, try for a namespace-name. */
4186 if (!only_class_p && !cp_parser_parse_definitely (parser))
4188 /* Restore the saved scope. */
4189 parser->scope = saved_scope;
4190 parser->qualifying_scope = saved_qualifying_scope;
4191 parser->object_scope = saved_object_scope;
4192 /* If we are not looking at an identifier followed by the scope
4193 resolution operator, then this is not part of a
4194 nested-name-specifier. (Note that this function is only used
4195 to parse the components of a nested-name-specifier.) */
4196 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4197 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4198 return error_mark_node;
4199 scope = cp_parser_namespace_name (parser);
4205 /* Parse a postfix-expression.
4209 postfix-expression [ expression ]
4210 postfix-expression ( expression-list [opt] )
4211 simple-type-specifier ( expression-list [opt] )
4212 typename :: [opt] nested-name-specifier identifier
4213 ( expression-list [opt] )
4214 typename :: [opt] nested-name-specifier template [opt] template-id
4215 ( expression-list [opt] )
4216 postfix-expression . template [opt] id-expression
4217 postfix-expression -> template [opt] id-expression
4218 postfix-expression . pseudo-destructor-name
4219 postfix-expression -> pseudo-destructor-name
4220 postfix-expression ++
4221 postfix-expression --
4222 dynamic_cast < type-id > ( expression )
4223 static_cast < type-id > ( expression )
4224 reinterpret_cast < type-id > ( expression )
4225 const_cast < type-id > ( expression )
4226 typeid ( expression )
4232 ( type-id ) { initializer-list , [opt] }
4234 This extension is a GNU version of the C99 compound-literal
4235 construct. (The C99 grammar uses `type-name' instead of `type-id',
4236 but they are essentially the same concept.)
4238 If ADDRESS_P is true, the postfix expression is the operand of the
4239 `&' operator. CAST_P is true if this expression is the target of a
4242 Returns a representation of the expression. */
4245 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
4249 cp_id_kind idk = CP_ID_KIND_NONE;
4250 tree postfix_expression = NULL_TREE;
4252 /* Peek at the next token. */
4253 token = cp_lexer_peek_token (parser->lexer);
4254 /* Some of the productions are determined by keywords. */
4255 keyword = token->keyword;
4265 const char *saved_message;
4267 /* All of these can be handled in the same way from the point
4268 of view of parsing. Begin by consuming the token
4269 identifying the cast. */
4270 cp_lexer_consume_token (parser->lexer);
4272 /* New types cannot be defined in the cast. */
4273 saved_message = parser->type_definition_forbidden_message;
4274 parser->type_definition_forbidden_message
4275 = "types may not be defined in casts";
4277 /* Look for the opening `<'. */
4278 cp_parser_require (parser, CPP_LESS, "`<'");
4279 /* Parse the type to which we are casting. */
4280 type = cp_parser_type_id (parser);
4281 /* Look for the closing `>'. */
4282 cp_parser_require (parser, CPP_GREATER, "`>'");
4283 /* Restore the old message. */
4284 parser->type_definition_forbidden_message = saved_message;
4286 /* And the expression which is being cast. */
4287 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4288 expression = cp_parser_expression (parser, /*cast_p=*/true);
4289 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4291 /* Only type conversions to integral or enumeration types
4292 can be used in constant-expressions. */
4293 if (!cast_valid_in_integral_constant_expression_p (type)
4294 && (cp_parser_non_integral_constant_expression
4296 "a cast to a type other than an integral or "
4297 "enumeration type")))
4298 return error_mark_node;
4304 = build_dynamic_cast (type, expression);
4308 = build_static_cast (type, expression);
4312 = build_reinterpret_cast (type, expression);
4316 = build_const_cast (type, expression);
4327 const char *saved_message;
4328 bool saved_in_type_id_in_expr_p;
4330 /* Consume the `typeid' token. */
4331 cp_lexer_consume_token (parser->lexer);
4332 /* Look for the `(' token. */
4333 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4334 /* Types cannot be defined in a `typeid' expression. */
4335 saved_message = parser->type_definition_forbidden_message;
4336 parser->type_definition_forbidden_message
4337 = "types may not be defined in a `typeid\' expression";
4338 /* We can't be sure yet whether we're looking at a type-id or an
4340 cp_parser_parse_tentatively (parser);
4341 /* Try a type-id first. */
4342 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4343 parser->in_type_id_in_expr_p = true;
4344 type = cp_parser_type_id (parser);
4345 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4346 /* Look for the `)' token. Otherwise, we can't be sure that
4347 we're not looking at an expression: consider `typeid (int
4348 (3))', for example. */
4349 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4350 /* If all went well, simply lookup the type-id. */
4351 if (cp_parser_parse_definitely (parser))
4352 postfix_expression = get_typeid (type);
4353 /* Otherwise, fall back to the expression variant. */
4358 /* Look for an expression. */
4359 expression = cp_parser_expression (parser, /*cast_p=*/false);
4360 /* Compute its typeid. */
4361 postfix_expression = build_typeid (expression);
4362 /* Look for the `)' token. */
4363 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4365 /* Restore the saved message. */
4366 parser->type_definition_forbidden_message = saved_message;
4367 /* `typeid' may not appear in an integral constant expression. */
4368 if (cp_parser_non_integral_constant_expression(parser,
4369 "`typeid' operator"))
4370 return error_mark_node;
4377 /* The syntax permitted here is the same permitted for an
4378 elaborated-type-specifier. */
4379 type = cp_parser_elaborated_type_specifier (parser,
4380 /*is_friend=*/false,
4381 /*is_declaration=*/false);
4382 postfix_expression = cp_parser_functional_cast (parser, type);
4390 /* If the next thing is a simple-type-specifier, we may be
4391 looking at a functional cast. We could also be looking at
4392 an id-expression. So, we try the functional cast, and if
4393 that doesn't work we fall back to the primary-expression. */
4394 cp_parser_parse_tentatively (parser);
4395 /* Look for the simple-type-specifier. */
4396 type = cp_parser_simple_type_specifier (parser,
4397 /*decl_specs=*/NULL,
4398 CP_PARSER_FLAGS_NONE);
4399 /* Parse the cast itself. */
4400 if (!cp_parser_error_occurred (parser))
4402 = cp_parser_functional_cast (parser, type);
4403 /* If that worked, we're done. */
4404 if (cp_parser_parse_definitely (parser))
4407 /* If the functional-cast didn't work out, try a
4408 compound-literal. */
4409 if (cp_parser_allow_gnu_extensions_p (parser)
4410 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4412 VEC(constructor_elt,gc) *initializer_list = NULL;
4413 bool saved_in_type_id_in_expr_p;
4415 cp_parser_parse_tentatively (parser);
4416 /* Consume the `('. */
4417 cp_lexer_consume_token (parser->lexer);
4418 /* Parse the type. */
4419 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4420 parser->in_type_id_in_expr_p = true;
4421 type = cp_parser_type_id (parser);
4422 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4423 /* Look for the `)'. */
4424 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4425 /* Look for the `{'. */
4426 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4427 /* If things aren't going well, there's no need to
4429 if (!cp_parser_error_occurred (parser))
4431 bool non_constant_p;
4432 /* Parse the initializer-list. */
4434 = cp_parser_initializer_list (parser, &non_constant_p);
4435 /* Allow a trailing `,'. */
4436 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4437 cp_lexer_consume_token (parser->lexer);
4438 /* Look for the final `}'. */
4439 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4441 /* If that worked, we're definitely looking at a
4442 compound-literal expression. */
4443 if (cp_parser_parse_definitely (parser))
4445 /* Warn the user that a compound literal is not
4446 allowed in standard C++. */
4448 pedwarn ("ISO C++ forbids compound-literals");
4449 /* For simplicity, we disallow compound literals in
4450 constant-expressions. We could
4451 allow compound literals of integer type, whose
4452 initializer was a constant, in constant
4453 expressions. Permitting that usage, as a further
4454 extension, would not change the meaning of any
4455 currently accepted programs. (Of course, as
4456 compound literals are not part of ISO C++, the
4457 standard has nothing to say.) */
4458 if (cp_parser_non_integral_constant_expression
4459 (parser, "non-constant compound literals"))
4461 postfix_expression = error_mark_node;
4464 /* Form the representation of the compound-literal. */
4466 = finish_compound_literal (type, initializer_list);
4471 /* It must be a primary-expression. */
4473 = cp_parser_primary_expression (parser, address_p, cast_p,
4474 /*template_arg_p=*/false,
4480 /* Keep looping until the postfix-expression is complete. */
4483 if (idk == CP_ID_KIND_UNQUALIFIED
4484 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4485 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4486 /* It is not a Koenig lookup function call. */
4488 = unqualified_name_lookup_error (postfix_expression);
4490 /* Peek at the next token. */
4491 token = cp_lexer_peek_token (parser->lexer);
4493 switch (token->type)
4495 case CPP_OPEN_SQUARE:
4497 = cp_parser_postfix_open_square_expression (parser,
4500 idk = CP_ID_KIND_NONE;
4503 case CPP_OPEN_PAREN:
4504 /* postfix-expression ( expression-list [opt] ) */
4507 bool is_builtin_constant_p;
4508 bool saved_integral_constant_expression_p = false;
4509 bool saved_non_integral_constant_expression_p = false;
4512 is_builtin_constant_p
4513 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4514 if (is_builtin_constant_p)
4516 /* The whole point of __builtin_constant_p is to allow
4517 non-constant expressions to appear as arguments. */
4518 saved_integral_constant_expression_p
4519 = parser->integral_constant_expression_p;
4520 saved_non_integral_constant_expression_p
4521 = parser->non_integral_constant_expression_p;
4522 parser->integral_constant_expression_p = false;
4524 args = (cp_parser_parenthesized_expression_list
4525 (parser, /*is_attribute_list=*/false,
4526 /*cast_p=*/false, /*allow_expansion_p=*/true,
4527 /*non_constant_p=*/NULL));
4528 if (is_builtin_constant_p)
4530 parser->integral_constant_expression_p
4531 = saved_integral_constant_expression_p;
4532 parser->non_integral_constant_expression_p
4533 = saved_non_integral_constant_expression_p;
4536 if (args == error_mark_node)
4538 postfix_expression = error_mark_node;
4542 /* Function calls are not permitted in
4543 constant-expressions. */
4544 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4545 && cp_parser_non_integral_constant_expression (parser,
4548 postfix_expression = error_mark_node;
4553 if (idk == CP_ID_KIND_UNQUALIFIED)
4555 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4561 = perform_koenig_lookup (postfix_expression, args);
4565 = unqualified_fn_lookup_error (postfix_expression);
4567 /* We do not perform argument-dependent lookup if
4568 normal lookup finds a non-function, in accordance
4569 with the expected resolution of DR 218. */
4570 else if (args && is_overloaded_fn (postfix_expression))
4572 tree fn = get_first_fn (postfix_expression);
4574 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4575 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4577 /* Only do argument dependent lookup if regular
4578 lookup does not find a set of member functions.
4579 [basic.lookup.koenig]/2a */
4580 if (!DECL_FUNCTION_MEMBER_P (fn))
4584 = perform_koenig_lookup (postfix_expression, args);
4589 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4591 tree instance = TREE_OPERAND (postfix_expression, 0);
4592 tree fn = TREE_OPERAND (postfix_expression, 1);
4594 if (processing_template_decl
4595 && (type_dependent_expression_p (instance)
4596 || (!BASELINK_P (fn)
4597 && TREE_CODE (fn) != FIELD_DECL)
4598 || type_dependent_expression_p (fn)
4599 || any_type_dependent_arguments_p (args)))
4602 = build_nt_call_list (postfix_expression, args);
4606 if (BASELINK_P (fn))
4608 = (build_new_method_call
4609 (instance, fn, args, NULL_TREE,
4610 (idk == CP_ID_KIND_QUALIFIED
4611 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4615 = finish_call_expr (postfix_expression, args,
4616 /*disallow_virtual=*/false,
4617 /*koenig_p=*/false);
4619 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4620 || TREE_CODE (postfix_expression) == MEMBER_REF
4621 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4622 postfix_expression = (build_offset_ref_call_from_tree
4623 (postfix_expression, args));
4624 else if (idk == CP_ID_KIND_QUALIFIED)
4625 /* A call to a static class member, or a namespace-scope
4628 = finish_call_expr (postfix_expression, args,
4629 /*disallow_virtual=*/true,
4632 /* All other function calls. */
4634 = finish_call_expr (postfix_expression, args,
4635 /*disallow_virtual=*/false,
4638 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4639 idk = CP_ID_KIND_NONE;
4645 /* postfix-expression . template [opt] id-expression
4646 postfix-expression . pseudo-destructor-name
4647 postfix-expression -> template [opt] id-expression
4648 postfix-expression -> pseudo-destructor-name */
4650 /* Consume the `.' or `->' operator. */
4651 cp_lexer_consume_token (parser->lexer);
4654 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4660 /* postfix-expression ++ */
4661 /* Consume the `++' token. */
4662 cp_lexer_consume_token (parser->lexer);
4663 /* Generate a representation for the complete expression. */
4665 = finish_increment_expr (postfix_expression,
4666 POSTINCREMENT_EXPR);
4667 /* Increments may not appear in constant-expressions. */
4668 if (cp_parser_non_integral_constant_expression (parser,
4670 postfix_expression = error_mark_node;
4671 idk = CP_ID_KIND_NONE;
4674 case CPP_MINUS_MINUS:
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 POSTDECREMENT_EXPR);
4682 /* Decrements 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;
4690 return postfix_expression;
4694 /* We should never get here. */
4696 return error_mark_node;
4699 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4700 by cp_parser_builtin_offsetof. We're looking for
4702 postfix-expression [ expression ]
4704 FOR_OFFSETOF is set if we're being called in that context, which
4705 changes how we deal with integer constant expressions. */
4708 cp_parser_postfix_open_square_expression (cp_parser *parser,
4709 tree postfix_expression,
4714 /* Consume the `[' token. */
4715 cp_lexer_consume_token (parser->lexer);
4717 /* Parse the index expression. */
4718 /* ??? For offsetof, there is a question of what to allow here. If
4719 offsetof is not being used in an integral constant expression context,
4720 then we *could* get the right answer by computing the value at runtime.
4721 If we are in an integral constant expression context, then we might
4722 could accept any constant expression; hard to say without analysis.
4723 Rather than open the barn door too wide right away, allow only integer
4724 constant expressions here. */
4726 index = cp_parser_constant_expression (parser, false, NULL);
4728 index = cp_parser_expression (parser, /*cast_p=*/false);
4730 /* Look for the closing `]'. */
4731 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4733 /* Build the ARRAY_REF. */
4734 postfix_expression = grok_array_decl (postfix_expression, index);
4736 /* When not doing offsetof, array references are not permitted in
4737 constant-expressions. */
4739 && (cp_parser_non_integral_constant_expression
4740 (parser, "an array reference")))
4741 postfix_expression = error_mark_node;
4743 return postfix_expression;
4746 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4747 by cp_parser_builtin_offsetof. We're looking for
4749 postfix-expression . template [opt] id-expression
4750 postfix-expression . pseudo-destructor-name
4751 postfix-expression -> template [opt] id-expression
4752 postfix-expression -> pseudo-destructor-name
4754 FOR_OFFSETOF is set if we're being called in that context. That sorta
4755 limits what of the above we'll actually accept, but nevermind.
4756 TOKEN_TYPE is the "." or "->" token, which will already have been
4757 removed from the stream. */
4760 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4761 enum cpp_ttype token_type,
4762 tree postfix_expression,
4763 bool for_offsetof, cp_id_kind *idk)
4767 bool pseudo_destructor_p;
4768 tree scope = NULL_TREE;
4770 /* If this is a `->' operator, dereference the pointer. */
4771 if (token_type == CPP_DEREF)
4772 postfix_expression = build_x_arrow (postfix_expression);
4773 /* Check to see whether or not the expression is type-dependent. */
4774 dependent_p = type_dependent_expression_p (postfix_expression);
4775 /* The identifier following the `->' or `.' is not qualified. */
4776 parser->scope = NULL_TREE;
4777 parser->qualifying_scope = NULL_TREE;
4778 parser->object_scope = NULL_TREE;
4779 *idk = CP_ID_KIND_NONE;
4780 /* Enter the scope corresponding to the type of the object
4781 given by the POSTFIX_EXPRESSION. */
4782 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4784 scope = TREE_TYPE (postfix_expression);
4785 /* According to the standard, no expression should ever have
4786 reference type. Unfortunately, we do not currently match
4787 the standard in this respect in that our internal representation
4788 of an expression may have reference type even when the standard
4789 says it does not. Therefore, we have to manually obtain the
4790 underlying type here. */
4791 scope = non_reference (scope);
4792 /* The type of the POSTFIX_EXPRESSION must be complete. */
4793 if (scope == unknown_type_node)
4795 error ("%qE does not have class type", postfix_expression);
4799 scope = complete_type_or_else (scope, NULL_TREE);
4800 /* Let the name lookup machinery know that we are processing a
4801 class member access expression. */
4802 parser->context->object_type = scope;
4803 /* If something went wrong, we want to be able to discern that case,
4804 as opposed to the case where there was no SCOPE due to the type
4805 of expression being dependent. */
4807 scope = error_mark_node;
4808 /* If the SCOPE was erroneous, make the various semantic analysis
4809 functions exit quickly -- and without issuing additional error
4811 if (scope == error_mark_node)
4812 postfix_expression = error_mark_node;
4815 /* Assume this expression is not a pseudo-destructor access. */
4816 pseudo_destructor_p = false;
4818 /* If the SCOPE is a scalar type, then, if this is a valid program,
4819 we must be looking at a pseudo-destructor-name. */
4820 if (scope && SCALAR_TYPE_P (scope))
4825 cp_parser_parse_tentatively (parser);
4826 /* Parse the pseudo-destructor-name. */
4828 cp_parser_pseudo_destructor_name (parser, &s, &type);
4829 if (cp_parser_parse_definitely (parser))
4831 pseudo_destructor_p = true;
4833 = finish_pseudo_destructor_expr (postfix_expression,
4834 s, TREE_TYPE (type));
4838 if (!pseudo_destructor_p)
4840 /* If the SCOPE is not a scalar type, we are looking at an
4841 ordinary class member access expression, rather than a
4842 pseudo-destructor-name. */
4844 /* Parse the id-expression. */
4845 name = (cp_parser_id_expression
4847 cp_parser_optional_template_keyword (parser),
4848 /*check_dependency_p=*/true,
4850 /*declarator_p=*/false,
4851 /*optional_p=*/false));
4852 /* In general, build a SCOPE_REF if the member name is qualified.
4853 However, if the name was not dependent and has already been
4854 resolved; there is no need to build the SCOPE_REF. For example;
4856 struct X { void f(); };
4857 template <typename T> void f(T* t) { t->X::f(); }
4859 Even though "t" is dependent, "X::f" is not and has been resolved
4860 to a BASELINK; there is no need to include scope information. */
4862 /* But we do need to remember that there was an explicit scope for
4863 virtual function calls. */
4865 *idk = CP_ID_KIND_QUALIFIED;
4867 /* If the name is a template-id that names a type, we will get a
4868 TYPE_DECL here. That is invalid code. */
4869 if (TREE_CODE (name) == TYPE_DECL)
4871 error ("invalid use of %qD", name);
4872 postfix_expression = error_mark_node;
4876 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4878 name = build_qualified_name (/*type=*/NULL_TREE,
4882 parser->scope = NULL_TREE;
4883 parser->qualifying_scope = NULL_TREE;
4884 parser->object_scope = NULL_TREE;
4886 if (scope && name && BASELINK_P (name))
4887 adjust_result_of_qualified_name_lookup
4888 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4890 = finish_class_member_access_expr (postfix_expression, name,
4895 /* We no longer need to look up names in the scope of the object on
4896 the left-hand side of the `.' or `->' operator. */
4897 parser->context->object_type = NULL_TREE;
4899 /* Outside of offsetof, these operators may not appear in
4900 constant-expressions. */
4902 && (cp_parser_non_integral_constant_expression
4903 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4904 postfix_expression = error_mark_node;
4906 return postfix_expression;
4909 /* Parse a parenthesized expression-list.
4912 assignment-expression
4913 expression-list, assignment-expression
4918 identifier, expression-list
4920 CAST_P is true if this expression is the target of a cast.
4922 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4925 Returns a TREE_LIST. The TREE_VALUE of each node is a
4926 representation of an assignment-expression. Note that a TREE_LIST
4927 is returned even if there is only a single expression in the list.
4928 error_mark_node is returned if the ( and or ) are
4929 missing. NULL_TREE is returned on no expressions. The parentheses
4930 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4931 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4932 indicates whether or not all of the expressions in the list were
4936 cp_parser_parenthesized_expression_list (cp_parser* parser,
4937 bool is_attribute_list,
4939 bool allow_expansion_p,
4940 bool *non_constant_p)
4942 tree expression_list = NULL_TREE;
4943 bool fold_expr_p = is_attribute_list;
4944 tree identifier = NULL_TREE;
4946 /* Assume all the expressions will be constant. */
4948 *non_constant_p = false;
4950 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4951 return error_mark_node;
4953 /* Consume expressions until there are no more. */
4954 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4959 /* At the beginning of attribute lists, check to see if the
4960 next token is an identifier. */
4961 if (is_attribute_list
4962 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4966 /* Consume the identifier. */
4967 token = cp_lexer_consume_token (parser->lexer);
4968 /* Save the identifier. */
4969 identifier = token->u.value;
4973 /* Parse the next assignment-expression. */
4976 bool expr_non_constant_p;
4977 expr = (cp_parser_constant_expression
4978 (parser, /*allow_non_constant_p=*/true,
4979 &expr_non_constant_p));
4980 if (expr_non_constant_p)
4981 *non_constant_p = true;
4984 expr = cp_parser_assignment_expression (parser, cast_p);
4987 expr = fold_non_dependent_expr (expr);
4989 /* If we have an ellipsis, then this is an expression
4991 if (allow_expansion_p
4992 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
4994 /* Consume the `...'. */
4995 cp_lexer_consume_token (parser->lexer);
4997 /* Build the argument pack. */
4998 expr = make_pack_expansion (expr);
5001 /* Add it to the list. We add error_mark_node
5002 expressions to the list, so that we can still tell if
5003 the correct form for a parenthesized expression-list
5004 is found. That gives better errors. */
5005 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5007 if (expr == error_mark_node)
5011 /* After the first item, attribute lists look the same as
5012 expression lists. */
5013 is_attribute_list = false;
5016 /* If the next token isn't a `,', then we are done. */
5017 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5020 /* Otherwise, consume the `,' and keep going. */
5021 cp_lexer_consume_token (parser->lexer);
5024 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5029 /* We try and resync to an unnested comma, as that will give the
5030 user better diagnostics. */
5031 ending = cp_parser_skip_to_closing_parenthesis (parser,
5032 /*recovering=*/true,
5034 /*consume_paren=*/true);
5038 return error_mark_node;
5041 /* We built up the list in reverse order so we must reverse it now. */
5042 expression_list = nreverse (expression_list);
5044 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5046 return expression_list;
5049 /* Parse a pseudo-destructor-name.
5051 pseudo-destructor-name:
5052 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5053 :: [opt] nested-name-specifier template template-id :: ~ type-name
5054 :: [opt] nested-name-specifier [opt] ~ type-name
5056 If either of the first two productions is used, sets *SCOPE to the
5057 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5058 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5059 or ERROR_MARK_NODE if the parse fails. */
5062 cp_parser_pseudo_destructor_name (cp_parser* parser,
5066 bool nested_name_specifier_p;
5068 /* Assume that things will not work out. */
5069 *type = error_mark_node;
5071 /* Look for the optional `::' operator. */
5072 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5073 /* Look for the optional nested-name-specifier. */
5074 nested_name_specifier_p
5075 = (cp_parser_nested_name_specifier_opt (parser,
5076 /*typename_keyword_p=*/false,
5077 /*check_dependency_p=*/true,
5079 /*is_declaration=*/true)
5081 /* Now, if we saw a nested-name-specifier, we might be doing the
5082 second production. */
5083 if (nested_name_specifier_p
5084 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5086 /* Consume the `template' keyword. */
5087 cp_lexer_consume_token (parser->lexer);
5088 /* Parse the template-id. */
5089 cp_parser_template_id (parser,
5090 /*template_keyword_p=*/true,
5091 /*check_dependency_p=*/false,
5092 /*is_declaration=*/true);
5093 /* Look for the `::' token. */
5094 cp_parser_require (parser, CPP_SCOPE, "`::'");
5096 /* If the next token is not a `~', then there might be some
5097 additional qualification. */
5098 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5100 /* Look for the type-name. */
5101 *scope = TREE_TYPE (cp_parser_type_name (parser));
5103 if (*scope == error_mark_node)
5106 /* If we don't have ::~, then something has gone wrong. Since
5107 the only caller of this function is looking for something
5108 after `.' or `->' after a scalar type, most likely the
5109 program is trying to get a member of a non-aggregate
5111 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5112 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5114 cp_parser_error (parser, "request for member of non-aggregate type");
5118 /* Look for the `::' token. */
5119 cp_parser_require (parser, CPP_SCOPE, "`::'");
5124 /* Look for the `~'. */
5125 cp_parser_require (parser, CPP_COMPL, "`~'");
5126 /* Look for the type-name again. We are not responsible for
5127 checking that it matches the first type-name. */
5128 *type = cp_parser_type_name (parser);
5131 /* Parse a unary-expression.
5137 unary-operator cast-expression
5138 sizeof unary-expression
5146 __extension__ cast-expression
5147 __alignof__ unary-expression
5148 __alignof__ ( type-id )
5149 __real__ cast-expression
5150 __imag__ cast-expression
5153 ADDRESS_P is true iff the unary-expression is appearing as the
5154 operand of the `&' operator. CAST_P is true if this expression is
5155 the target of a cast.
5157 Returns a representation of the expression. */
5160 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5163 enum tree_code unary_operator;
5165 /* Peek at the next token. */
5166 token = cp_lexer_peek_token (parser->lexer);
5167 /* Some keywords give away the kind of expression. */
5168 if (token->type == CPP_KEYWORD)
5170 enum rid keyword = token->keyword;
5180 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5181 /* Consume the token. */
5182 cp_lexer_consume_token (parser->lexer);
5183 /* Parse the operand. */
5184 operand = cp_parser_sizeof_operand (parser, keyword);
5186 if (TYPE_P (operand))
5187 return cxx_sizeof_or_alignof_type (operand, op, true);
5189 return cxx_sizeof_or_alignof_expr (operand, op);
5193 return cp_parser_new_expression (parser);
5196 return cp_parser_delete_expression (parser);
5200 /* The saved value of the PEDANTIC flag. */
5204 /* Save away the PEDANTIC flag. */
5205 cp_parser_extension_opt (parser, &saved_pedantic);
5206 /* Parse the cast-expression. */
5207 expr = cp_parser_simple_cast_expression (parser);
5208 /* Restore the PEDANTIC flag. */
5209 pedantic = saved_pedantic;
5219 /* Consume the `__real__' or `__imag__' token. */
5220 cp_lexer_consume_token (parser->lexer);
5221 /* Parse the cast-expression. */
5222 expression = cp_parser_simple_cast_expression (parser);
5223 /* Create the complete representation. */
5224 return build_x_unary_op ((keyword == RID_REALPART
5225 ? REALPART_EXPR : IMAGPART_EXPR),
5235 /* Look for the `:: new' and `:: delete', which also signal the
5236 beginning of a new-expression, or delete-expression,
5237 respectively. If the next token is `::', then it might be one of
5239 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5243 /* See if the token after the `::' is one of the keywords in
5244 which we're interested. */
5245 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5246 /* If it's `new', we have a new-expression. */
5247 if (keyword == RID_NEW)
5248 return cp_parser_new_expression (parser);
5249 /* Similarly, for `delete'. */
5250 else if (keyword == RID_DELETE)
5251 return cp_parser_delete_expression (parser);
5254 /* Look for a unary operator. */
5255 unary_operator = cp_parser_unary_operator (token);
5256 /* The `++' and `--' operators can be handled similarly, even though
5257 they are not technically unary-operators in the grammar. */
5258 if (unary_operator == ERROR_MARK)
5260 if (token->type == CPP_PLUS_PLUS)
5261 unary_operator = PREINCREMENT_EXPR;
5262 else if (token->type == CPP_MINUS_MINUS)
5263 unary_operator = PREDECREMENT_EXPR;
5264 /* Handle the GNU address-of-label extension. */
5265 else if (cp_parser_allow_gnu_extensions_p (parser)
5266 && token->type == CPP_AND_AND)
5270 /* Consume the '&&' token. */
5271 cp_lexer_consume_token (parser->lexer);
5272 /* Look for the identifier. */
5273 identifier = cp_parser_identifier (parser);
5274 /* Create an expression representing the address. */
5275 return finish_label_address_expr (identifier);
5278 if (unary_operator != ERROR_MARK)
5280 tree cast_expression;
5281 tree expression = error_mark_node;
5282 const char *non_constant_p = NULL;
5284 /* Consume the operator token. */
5285 token = cp_lexer_consume_token (parser->lexer);
5286 /* Parse the cast-expression. */
5288 = cp_parser_cast_expression (parser,
5289 unary_operator == ADDR_EXPR,
5291 /* Now, build an appropriate representation. */
5292 switch (unary_operator)
5295 non_constant_p = "`*'";
5296 expression = build_x_indirect_ref (cast_expression, "unary *");
5300 non_constant_p = "`&'";
5303 expression = build_x_unary_op (unary_operator, cast_expression);
5306 case PREINCREMENT_EXPR:
5307 case PREDECREMENT_EXPR:
5308 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5311 case UNARY_PLUS_EXPR:
5313 case TRUTH_NOT_EXPR:
5314 expression = finish_unary_op_expr (unary_operator, cast_expression);
5322 && cp_parser_non_integral_constant_expression (parser,
5324 expression = error_mark_node;
5329 return cp_parser_postfix_expression (parser, address_p, cast_p);
5332 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5333 unary-operator, the corresponding tree code is returned. */
5335 static enum tree_code
5336 cp_parser_unary_operator (cp_token* token)
5338 switch (token->type)
5341 return INDIRECT_REF;
5347 return UNARY_PLUS_EXPR;
5353 return TRUTH_NOT_EXPR;
5356 return BIT_NOT_EXPR;
5363 /* Parse a new-expression.
5366 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5367 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5369 Returns a representation of the expression. */
5372 cp_parser_new_expression (cp_parser* parser)
5374 bool global_scope_p;
5380 /* Look for the optional `::' operator. */
5382 = (cp_parser_global_scope_opt (parser,
5383 /*current_scope_valid_p=*/false)
5385 /* Look for the `new' operator. */
5386 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5387 /* There's no easy way to tell a new-placement from the
5388 `( type-id )' construct. */
5389 cp_parser_parse_tentatively (parser);
5390 /* Look for a new-placement. */
5391 placement = cp_parser_new_placement (parser);
5392 /* If that didn't work out, there's no new-placement. */
5393 if (!cp_parser_parse_definitely (parser))
5394 placement = NULL_TREE;
5396 /* If the next token is a `(', then we have a parenthesized
5398 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5400 /* Consume the `('. */
5401 cp_lexer_consume_token (parser->lexer);
5402 /* Parse the type-id. */
5403 type = cp_parser_type_id (parser);
5404 /* Look for the closing `)'. */
5405 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5406 /* There should not be a direct-new-declarator in this production,
5407 but GCC used to allowed this, so we check and emit a sensible error
5408 message for this case. */
5409 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5411 error ("array bound forbidden after parenthesized type-id");
5412 inform ("try removing the parentheses around the type-id");
5413 cp_parser_direct_new_declarator (parser);
5417 /* Otherwise, there must be a new-type-id. */
5419 type = cp_parser_new_type_id (parser, &nelts);
5421 /* If the next token is a `(', then we have a new-initializer. */
5422 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5423 initializer = cp_parser_new_initializer (parser);
5425 initializer = NULL_TREE;
5427 /* A new-expression may not appear in an integral constant
5429 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5430 return error_mark_node;
5432 /* Create a representation of the new-expression. */
5433 return build_new (placement, type, nelts, initializer, global_scope_p);
5436 /* Parse a new-placement.
5441 Returns the same representation as for an expression-list. */
5444 cp_parser_new_placement (cp_parser* parser)
5446 tree expression_list;
5448 /* Parse the expression-list. */
5449 expression_list = (cp_parser_parenthesized_expression_list
5450 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5451 /*non_constant_p=*/NULL));
5453 return expression_list;
5456 /* Parse a new-type-id.
5459 type-specifier-seq new-declarator [opt]
5461 Returns the TYPE allocated. If the new-type-id indicates an array
5462 type, *NELTS is set to the number of elements in the last array
5463 bound; the TYPE will not include the last array bound. */
5466 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5468 cp_decl_specifier_seq type_specifier_seq;
5469 cp_declarator *new_declarator;
5470 cp_declarator *declarator;
5471 cp_declarator *outer_declarator;
5472 const char *saved_message;
5475 /* The type-specifier sequence must not contain type definitions.
5476 (It cannot contain declarations of new types either, but if they
5477 are not definitions we will catch that because they are not
5479 saved_message = parser->type_definition_forbidden_message;
5480 parser->type_definition_forbidden_message
5481 = "types may not be defined in a new-type-id";
5482 /* Parse the type-specifier-seq. */
5483 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5484 &type_specifier_seq);
5485 /* Restore the old message. */
5486 parser->type_definition_forbidden_message = saved_message;
5487 /* Parse the new-declarator. */
5488 new_declarator = cp_parser_new_declarator_opt (parser);
5490 /* Determine the number of elements in the last array dimension, if
5493 /* Skip down to the last array dimension. */
5494 declarator = new_declarator;
5495 outer_declarator = NULL;
5496 while (declarator && (declarator->kind == cdk_pointer
5497 || declarator->kind == cdk_ptrmem))
5499 outer_declarator = declarator;
5500 declarator = declarator->declarator;
5503 && declarator->kind == cdk_array
5504 && declarator->declarator
5505 && declarator->declarator->kind == cdk_array)
5507 outer_declarator = declarator;
5508 declarator = declarator->declarator;
5511 if (declarator && declarator->kind == cdk_array)
5513 *nelts = declarator->u.array.bounds;
5514 if (*nelts == error_mark_node)
5515 *nelts = integer_one_node;
5517 if (outer_declarator)
5518 outer_declarator->declarator = declarator->declarator;
5520 new_declarator = NULL;
5523 type = groktypename (&type_specifier_seq, new_declarator);
5524 if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5526 *nelts = array_type_nelts_top (type);
5527 type = TREE_TYPE (type);
5532 /* Parse an (optional) new-declarator.
5535 ptr-operator new-declarator [opt]
5536 direct-new-declarator
5538 Returns the declarator. */
5540 static cp_declarator *
5541 cp_parser_new_declarator_opt (cp_parser* parser)
5543 enum tree_code code;
5545 cp_cv_quals cv_quals;
5547 /* We don't know if there's a ptr-operator next, or not. */
5548 cp_parser_parse_tentatively (parser);
5549 /* Look for a ptr-operator. */
5550 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5551 /* If that worked, look for more new-declarators. */
5552 if (cp_parser_parse_definitely (parser))
5554 cp_declarator *declarator;
5556 /* Parse another optional declarator. */
5557 declarator = cp_parser_new_declarator_opt (parser);
5559 return cp_parser_make_indirect_declarator
5560 (code, type, cv_quals, declarator);
5563 /* If the next token is a `[', there is a direct-new-declarator. */
5564 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5565 return cp_parser_direct_new_declarator (parser);
5570 /* Parse a direct-new-declarator.
5572 direct-new-declarator:
5574 direct-new-declarator [constant-expression]
5578 static cp_declarator *
5579 cp_parser_direct_new_declarator (cp_parser* parser)
5581 cp_declarator *declarator = NULL;
5587 /* Look for the opening `['. */
5588 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5589 /* The first expression is not required to be constant. */
5592 expression = cp_parser_expression (parser, /*cast_p=*/false);
5593 /* The standard requires that the expression have integral
5594 type. DR 74 adds enumeration types. We believe that the
5595 real intent is that these expressions be handled like the
5596 expression in a `switch' condition, which also allows
5597 classes with a single conversion to integral or
5598 enumeration type. */
5599 if (!processing_template_decl)
5602 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5607 error ("expression in new-declarator must have integral "
5608 "or enumeration type");
5609 expression = error_mark_node;
5613 /* But all the other expressions must be. */
5616 = cp_parser_constant_expression (parser,
5617 /*allow_non_constant=*/false,
5619 /* Look for the closing `]'. */
5620 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5622 /* Add this bound to the declarator. */
5623 declarator = make_array_declarator (declarator, expression);
5625 /* If the next token is not a `[', then there are no more
5627 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5634 /* Parse a new-initializer.
5637 ( expression-list [opt] )
5639 Returns a representation of the expression-list. If there is no
5640 expression-list, VOID_ZERO_NODE is returned. */
5643 cp_parser_new_initializer (cp_parser* parser)
5645 tree expression_list;
5647 expression_list = (cp_parser_parenthesized_expression_list
5648 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5649 /*non_constant_p=*/NULL));
5650 if (!expression_list)
5651 expression_list = void_zero_node;
5653 return expression_list;
5656 /* Parse a delete-expression.
5659 :: [opt] delete cast-expression
5660 :: [opt] delete [ ] cast-expression
5662 Returns a representation of the expression. */
5665 cp_parser_delete_expression (cp_parser* parser)
5667 bool global_scope_p;
5671 /* Look for the optional `::' operator. */
5673 = (cp_parser_global_scope_opt (parser,
5674 /*current_scope_valid_p=*/false)
5676 /* Look for the `delete' keyword. */
5677 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5678 /* See if the array syntax is in use. */
5679 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5681 /* Consume the `[' token. */
5682 cp_lexer_consume_token (parser->lexer);
5683 /* Look for the `]' token. */
5684 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5685 /* Remember that this is the `[]' construct. */
5691 /* Parse the cast-expression. */
5692 expression = cp_parser_simple_cast_expression (parser);
5694 /* A delete-expression may not appear in an integral constant
5696 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5697 return error_mark_node;
5699 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5702 /* Parse a cast-expression.
5706 ( type-id ) cast-expression
5708 ADDRESS_P is true iff the unary-expression is appearing as the
5709 operand of the `&' operator. CAST_P is true if this expression is
5710 the target of a cast.
5712 Returns a representation of the expression. */
5715 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5717 /* If it's a `(', then we might be looking at a cast. */
5718 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5720 tree type = NULL_TREE;
5721 tree expr = NULL_TREE;
5722 bool compound_literal_p;
5723 const char *saved_message;
5725 /* There's no way to know yet whether or not this is a cast.
5726 For example, `(int (3))' is a unary-expression, while `(int)
5727 3' is a cast. So, we resort to parsing tentatively. */
5728 cp_parser_parse_tentatively (parser);
5729 /* Types may not be defined in a cast. */
5730 saved_message = parser->type_definition_forbidden_message;
5731 parser->type_definition_forbidden_message
5732 = "types may not be defined in casts";
5733 /* Consume the `('. */
5734 cp_lexer_consume_token (parser->lexer);
5735 /* A very tricky bit is that `(struct S) { 3 }' is a
5736 compound-literal (which we permit in C++ as an extension).
5737 But, that construct is not a cast-expression -- it is a
5738 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5739 is legal; if the compound-literal were a cast-expression,
5740 you'd need an extra set of parentheses.) But, if we parse
5741 the type-id, and it happens to be a class-specifier, then we
5742 will commit to the parse at that point, because we cannot
5743 undo the action that is done when creating a new class. So,
5744 then we cannot back up and do a postfix-expression.
5746 Therefore, we scan ahead to the closing `)', and check to see
5747 if the token after the `)' is a `{'. If so, we are not
5748 looking at a cast-expression.
5750 Save tokens so that we can put them back. */
5751 cp_lexer_save_tokens (parser->lexer);
5752 /* Skip tokens until the next token is a closing parenthesis.
5753 If we find the closing `)', and the next token is a `{', then
5754 we are looking at a compound-literal. */
5756 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5757 /*consume_paren=*/true)
5758 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5759 /* Roll back the tokens we skipped. */
5760 cp_lexer_rollback_tokens (parser->lexer);
5761 /* If we were looking at a compound-literal, simulate an error
5762 so that the call to cp_parser_parse_definitely below will
5764 if (compound_literal_p)
5765 cp_parser_simulate_error (parser);
5768 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5769 parser->in_type_id_in_expr_p = true;
5770 /* Look for the type-id. */
5771 type = cp_parser_type_id (parser);
5772 /* Look for the closing `)'. */
5773 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5774 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5777 /* Restore the saved message. */
5778 parser->type_definition_forbidden_message = saved_message;
5780 /* If ok so far, parse the dependent expression. We cannot be
5781 sure it is a cast. Consider `(T ())'. It is a parenthesized
5782 ctor of T, but looks like a cast to function returning T
5783 without a dependent expression. */
5784 if (!cp_parser_error_occurred (parser))
5785 expr = cp_parser_cast_expression (parser,
5786 /*address_p=*/false,
5789 if (cp_parser_parse_definitely (parser))
5791 /* Warn about old-style casts, if so requested. */
5792 if (warn_old_style_cast
5793 && !in_system_header
5794 && !VOID_TYPE_P (type)
5795 && current_lang_name != lang_name_c)
5796 warning (OPT_Wold_style_cast, "use of old-style cast");
5798 /* Only type conversions to integral or enumeration types
5799 can be used in constant-expressions. */
5800 if (!cast_valid_in_integral_constant_expression_p (type)
5801 && (cp_parser_non_integral_constant_expression
5803 "a cast to a type other than an integral or "
5804 "enumeration type")))
5805 return error_mark_node;
5807 /* Perform the cast. */
5808 expr = build_c_cast (type, expr);
5813 /* If we get here, then it's not a cast, so it must be a
5814 unary-expression. */
5815 return cp_parser_unary_expression (parser, address_p, cast_p);
5818 /* Parse a binary expression of the general form:
5822 pm-expression .* cast-expression
5823 pm-expression ->* cast-expression
5825 multiplicative-expression:
5827 multiplicative-expression * pm-expression
5828 multiplicative-expression / pm-expression
5829 multiplicative-expression % pm-expression
5831 additive-expression:
5832 multiplicative-expression
5833 additive-expression + multiplicative-expression
5834 additive-expression - multiplicative-expression
5838 shift-expression << additive-expression
5839 shift-expression >> additive-expression
5841 relational-expression:
5843 relational-expression < shift-expression
5844 relational-expression > shift-expression
5845 relational-expression <= shift-expression
5846 relational-expression >= shift-expression
5850 relational-expression:
5851 relational-expression <? shift-expression
5852 relational-expression >? shift-expression
5854 equality-expression:
5855 relational-expression
5856 equality-expression == relational-expression
5857 equality-expression != relational-expression
5861 and-expression & equality-expression
5863 exclusive-or-expression:
5865 exclusive-or-expression ^ and-expression
5867 inclusive-or-expression:
5868 exclusive-or-expression
5869 inclusive-or-expression | exclusive-or-expression
5871 logical-and-expression:
5872 inclusive-or-expression
5873 logical-and-expression && inclusive-or-expression
5875 logical-or-expression:
5876 logical-and-expression
5877 logical-or-expression || logical-and-expression
5879 All these are implemented with a single function like:
5882 simple-cast-expression
5883 binary-expression <token> binary-expression
5885 CAST_P is true if this expression is the target of a cast.
5887 The binops_by_token map is used to get the tree codes for each <token> type.
5888 binary-expressions are associated according to a precedence table. */
5890 #define TOKEN_PRECEDENCE(token) \
5891 (((token->type == CPP_GREATER \
5892 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5893 && !parser->greater_than_is_operator_p) \
5894 ? PREC_NOT_OPERATOR \
5895 : binops_by_token[token->type].prec)
5898 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5900 cp_parser_expression_stack stack;
5901 cp_parser_expression_stack_entry *sp = &stack[0];
5904 enum tree_code tree_type, lhs_type, rhs_type;
5905 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5908 /* Parse the first expression. */
5909 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5910 lhs_type = ERROR_MARK;
5914 /* Get an operator token. */
5915 token = cp_lexer_peek_token (parser->lexer);
5917 if (warn_cxx0x_compat
5918 && token->type == CPP_RSHIFT
5919 && !parser->greater_than_is_operator_p)
5921 warning (OPT_Wc__0x_compat,
5922 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5924 warning (OPT_Wc__0x_compat,
5925 "suggest parentheses around %<>>%> expression");
5928 new_prec = TOKEN_PRECEDENCE (token);
5930 /* Popping an entry off the stack means we completed a subexpression:
5931 - either we found a token which is not an operator (`>' where it is not
5932 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5933 will happen repeatedly;
5934 - or, we found an operator which has lower priority. This is the case
5935 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5937 if (new_prec <= prec)
5946 tree_type = binops_by_token[token->type].tree_type;
5948 /* We used the operator token. */
5949 cp_lexer_consume_token (parser->lexer);
5951 /* Extract another operand. It may be the RHS of this expression
5952 or the LHS of a new, higher priority expression. */
5953 rhs = cp_parser_simple_cast_expression (parser);
5954 rhs_type = ERROR_MARK;
5956 /* Get another operator token. Look up its precedence to avoid
5957 building a useless (immediately popped) stack entry for common
5958 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5959 token = cp_lexer_peek_token (parser->lexer);
5960 lookahead_prec = TOKEN_PRECEDENCE (token);
5961 if (lookahead_prec > new_prec)
5963 /* ... and prepare to parse the RHS of the new, higher priority
5964 expression. Since precedence levels on the stack are
5965 monotonically increasing, we do not have to care about
5968 sp->tree_type = tree_type;
5970 sp->lhs_type = lhs_type;
5973 lhs_type = rhs_type;
5975 new_prec = lookahead_prec;
5979 /* If the stack is not empty, we have parsed into LHS the right side
5980 (`4' in the example above) of an expression we had suspended.
5981 We can use the information on the stack to recover the LHS (`3')
5982 from the stack together with the tree code (`MULT_EXPR'), and
5983 the precedence of the higher level subexpression
5984 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
5985 which will be used to actually build the additive expression. */
5988 tree_type = sp->tree_type;
5990 rhs_type = lhs_type;
5992 lhs_type = sp->lhs_type;
5995 overloaded_p = false;
5996 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
5998 lhs_type = tree_type;
6000 /* If the binary operator required the use of an overloaded operator,
6001 then this expression cannot be an integral constant-expression.
6002 An overloaded operator can be used even if both operands are
6003 otherwise permissible in an integral constant-expression if at
6004 least one of the operands is of enumeration type. */
6007 && (cp_parser_non_integral_constant_expression
6008 (parser, "calls to overloaded operators")))
6009 return error_mark_node;
6016 /* Parse the `? expression : assignment-expression' part of a
6017 conditional-expression. The LOGICAL_OR_EXPR is the
6018 logical-or-expression that started the conditional-expression.
6019 Returns a representation of the entire conditional-expression.
6021 This routine is used by cp_parser_assignment_expression.
6023 ? expression : assignment-expression
6027 ? : assignment-expression */
6030 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6033 tree assignment_expr;
6035 /* Consume the `?' token. */
6036 cp_lexer_consume_token (parser->lexer);
6037 if (cp_parser_allow_gnu_extensions_p (parser)
6038 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6039 /* Implicit true clause. */
6042 /* Parse the expression. */
6043 expr = cp_parser_expression (parser, /*cast_p=*/false);
6045 /* The next token should be a `:'. */
6046 cp_parser_require (parser, CPP_COLON, "`:'");
6047 /* Parse the assignment-expression. */
6048 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6050 /* Build the conditional-expression. */
6051 return build_x_conditional_expr (logical_or_expr,
6056 /* Parse an assignment-expression.
6058 assignment-expression:
6059 conditional-expression
6060 logical-or-expression assignment-operator assignment_expression
6063 CAST_P is true if this expression is the target of a cast.
6065 Returns a representation for the expression. */
6068 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6072 /* If the next token is the `throw' keyword, then we're looking at
6073 a throw-expression. */
6074 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6075 expr = cp_parser_throw_expression (parser);
6076 /* Otherwise, it must be that we are looking at a
6077 logical-or-expression. */
6080 /* Parse the binary expressions (logical-or-expression). */
6081 expr = cp_parser_binary_expression (parser, cast_p);
6082 /* If the next token is a `?' then we're actually looking at a
6083 conditional-expression. */
6084 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6085 return cp_parser_question_colon_clause (parser, expr);
6088 enum tree_code assignment_operator;
6090 /* If it's an assignment-operator, we're using the second
6093 = cp_parser_assignment_operator_opt (parser);
6094 if (assignment_operator != ERROR_MARK)
6098 /* Parse the right-hand side of the assignment. */
6099 rhs = cp_parser_assignment_expression (parser, cast_p);
6100 /* An assignment may not appear in a
6101 constant-expression. */
6102 if (cp_parser_non_integral_constant_expression (parser,
6104 return error_mark_node;
6105 /* Build the assignment expression. */
6106 expr = build_x_modify_expr (expr,
6107 assignment_operator,
6116 /* Parse an (optional) assignment-operator.
6118 assignment-operator: one of
6119 = *= /= %= += -= >>= <<= &= ^= |=
6123 assignment-operator: one of
6126 If the next token is an assignment operator, the corresponding tree
6127 code is returned, and the token is consumed. For example, for
6128 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6129 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6130 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6131 operator, ERROR_MARK is returned. */
6133 static enum tree_code
6134 cp_parser_assignment_operator_opt (cp_parser* parser)
6139 /* Peek at the next toen. */
6140 token = cp_lexer_peek_token (parser->lexer);
6142 switch (token->type)
6153 op = TRUNC_DIV_EXPR;
6157 op = TRUNC_MOD_EXPR;
6189 /* Nothing else is an assignment operator. */
6193 /* If it was an assignment operator, consume it. */
6194 if (op != ERROR_MARK)
6195 cp_lexer_consume_token (parser->lexer);
6200 /* Parse an expression.
6203 assignment-expression
6204 expression , assignment-expression
6206 CAST_P is true if this expression is the target of a cast.
6208 Returns a representation of the expression. */
6211 cp_parser_expression (cp_parser* parser, bool cast_p)
6213 tree expression = NULL_TREE;
6217 tree assignment_expression;
6219 /* Parse the next assignment-expression. */
6220 assignment_expression
6221 = cp_parser_assignment_expression (parser, cast_p);
6222 /* If this is the first assignment-expression, we can just
6225 expression = assignment_expression;
6227 expression = build_x_compound_expr (expression,
6228 assignment_expression);
6229 /* If the next token is not a comma, then we are done with the
6231 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6233 /* Consume the `,'. */
6234 cp_lexer_consume_token (parser->lexer);
6235 /* A comma operator cannot appear in a constant-expression. */
6236 if (cp_parser_non_integral_constant_expression (parser,
6237 "a comma operator"))
6238 expression = error_mark_node;
6244 /* Parse a constant-expression.
6246 constant-expression:
6247 conditional-expression
6249 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6250 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6251 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6252 is false, NON_CONSTANT_P should be NULL. */
6255 cp_parser_constant_expression (cp_parser* parser,
6256 bool allow_non_constant_p,
6257 bool *non_constant_p)
6259 bool saved_integral_constant_expression_p;
6260 bool saved_allow_non_integral_constant_expression_p;
6261 bool saved_non_integral_constant_expression_p;
6264 /* It might seem that we could simply parse the
6265 conditional-expression, and then check to see if it were
6266 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6267 one that the compiler can figure out is constant, possibly after
6268 doing some simplifications or optimizations. The standard has a
6269 precise definition of constant-expression, and we must honor
6270 that, even though it is somewhat more restrictive.
6276 is not a legal declaration, because `(2, 3)' is not a
6277 constant-expression. The `,' operator is forbidden in a
6278 constant-expression. However, GCC's constant-folding machinery
6279 will fold this operation to an INTEGER_CST for `3'. */
6281 /* Save the old settings. */
6282 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6283 saved_allow_non_integral_constant_expression_p
6284 = parser->allow_non_integral_constant_expression_p;
6285 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6286 /* We are now parsing a constant-expression. */
6287 parser->integral_constant_expression_p = true;
6288 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6289 parser->non_integral_constant_expression_p = false;
6290 /* Although the grammar says "conditional-expression", we parse an
6291 "assignment-expression", which also permits "throw-expression"
6292 and the use of assignment operators. In the case that
6293 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6294 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6295 actually essential that we look for an assignment-expression.
6296 For example, cp_parser_initializer_clauses uses this function to
6297 determine whether a particular assignment-expression is in fact
6299 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6300 /* Restore the old settings. */
6301 parser->integral_constant_expression_p
6302 = saved_integral_constant_expression_p;
6303 parser->allow_non_integral_constant_expression_p
6304 = saved_allow_non_integral_constant_expression_p;
6305 if (allow_non_constant_p)
6306 *non_constant_p = parser->non_integral_constant_expression_p;
6307 else if (parser->non_integral_constant_expression_p)
6308 expression = error_mark_node;
6309 parser->non_integral_constant_expression_p
6310 = saved_non_integral_constant_expression_p;
6315 /* Parse __builtin_offsetof.
6317 offsetof-expression:
6318 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6320 offsetof-member-designator:
6322 | offsetof-member-designator "." id-expression
6323 | offsetof-member-designator "[" expression "]" */
6326 cp_parser_builtin_offsetof (cp_parser *parser)
6328 int save_ice_p, save_non_ice_p;
6332 /* We're about to accept non-integral-constant things, but will
6333 definitely yield an integral constant expression. Save and
6334 restore these values around our local parsing. */
6335 save_ice_p = parser->integral_constant_expression_p;
6336 save_non_ice_p = parser->non_integral_constant_expression_p;
6338 /* Consume the "__builtin_offsetof" token. */
6339 cp_lexer_consume_token (parser->lexer);
6340 /* Consume the opening `('. */
6341 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6342 /* Parse the type-id. */
6343 type = cp_parser_type_id (parser);
6344 /* Look for the `,'. */
6345 cp_parser_require (parser, CPP_COMMA, "`,'");
6347 /* Build the (type *)null that begins the traditional offsetof macro. */
6348 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6350 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6351 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6355 cp_token *token = cp_lexer_peek_token (parser->lexer);
6356 switch (token->type)
6358 case CPP_OPEN_SQUARE:
6359 /* offsetof-member-designator "[" expression "]" */
6360 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6364 /* offsetof-member-designator "." identifier */
6365 cp_lexer_consume_token (parser->lexer);
6366 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6370 case CPP_CLOSE_PAREN:
6371 /* Consume the ")" token. */
6372 cp_lexer_consume_token (parser->lexer);
6376 /* Error. We know the following require will fail, but
6377 that gives the proper error message. */
6378 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6379 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6380 expr = error_mark_node;
6386 /* If we're processing a template, we can't finish the semantics yet.
6387 Otherwise we can fold the entire expression now. */
6388 if (processing_template_decl)
6389 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6391 expr = finish_offsetof (expr);
6394 parser->integral_constant_expression_p = save_ice_p;
6395 parser->non_integral_constant_expression_p = save_non_ice_p;
6400 /* Parse a trait expression. */
6403 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6406 tree type1, type2 = NULL_TREE;
6407 bool binary = false;
6408 cp_decl_specifier_seq decl_specs;
6412 case RID_HAS_NOTHROW_ASSIGN:
6413 kind = CPTK_HAS_NOTHROW_ASSIGN;
6415 case RID_HAS_NOTHROW_CONSTRUCTOR:
6416 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6418 case RID_HAS_NOTHROW_COPY:
6419 kind = CPTK_HAS_NOTHROW_COPY;
6421 case RID_HAS_TRIVIAL_ASSIGN:
6422 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6424 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6425 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6427 case RID_HAS_TRIVIAL_COPY:
6428 kind = CPTK_HAS_TRIVIAL_COPY;
6430 case RID_HAS_TRIVIAL_DESTRUCTOR:
6431 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6433 case RID_HAS_VIRTUAL_DESTRUCTOR:
6434 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6436 case RID_IS_ABSTRACT:
6437 kind = CPTK_IS_ABSTRACT;
6439 case RID_IS_BASE_OF:
6440 kind = CPTK_IS_BASE_OF;
6444 kind = CPTK_IS_CLASS;
6446 case RID_IS_CONVERTIBLE_TO:
6447 kind = CPTK_IS_CONVERTIBLE_TO;
6451 kind = CPTK_IS_EMPTY;
6454 kind = CPTK_IS_ENUM;
6459 case RID_IS_POLYMORPHIC:
6460 kind = CPTK_IS_POLYMORPHIC;
6463 kind = CPTK_IS_UNION;
6469 /* Consume the token. */
6470 cp_lexer_consume_token (parser->lexer);
6472 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6474 type1 = cp_parser_type_id (parser);
6476 /* Build a trivial decl-specifier-seq. */
6477 clear_decl_specs (&decl_specs);
6478 decl_specs.type = type1;
6480 /* Call grokdeclarator to figure out what type this is. */
6481 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6482 /*initialized=*/0, /*attrlist=*/NULL);
6486 cp_parser_require (parser, CPP_COMMA, "`,'");
6488 type2 = cp_parser_type_id (parser);
6490 /* Build a trivial decl-specifier-seq. */
6491 clear_decl_specs (&decl_specs);
6492 decl_specs.type = type2;
6494 /* Call grokdeclarator to figure out what type this is. */
6495 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6496 /*initialized=*/0, /*attrlist=*/NULL);
6499 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6501 /* Complete the trait expr, which may mean either processing the
6502 static assert now or saving it for template instantiation. */
6503 return finish_trait_expr (kind, type1, type2);
6506 /* Statements [gram.stmt.stmt] */
6508 /* Parse a statement.
6512 expression-statement
6517 declaration-statement
6520 IN_COMPOUND is true when the statement is nested inside a
6521 cp_parser_compound_statement; this matters for certain pragmas.
6523 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6524 is a (possibly labeled) if statement which is not enclosed in braces
6525 and has an else clause. This is used to implement -Wparentheses. */
6528 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6529 bool in_compound, bool *if_p)
6533 location_t statement_location;
6538 /* There is no statement yet. */
6539 statement = NULL_TREE;
6540 /* Peek at the next token. */
6541 token = cp_lexer_peek_token (parser->lexer);
6542 /* Remember the location of the first token in the statement. */
6543 statement_location = token->location;
6544 /* If this is a keyword, then that will often determine what kind of
6545 statement we have. */
6546 if (token->type == CPP_KEYWORD)
6548 enum rid keyword = token->keyword;
6554 /* Looks like a labeled-statement with a case label.
6555 Parse the label, and then use tail recursion to parse
6557 cp_parser_label_for_labeled_statement (parser);
6562 statement = cp_parser_selection_statement (parser, if_p);
6568 statement = cp_parser_iteration_statement (parser);
6575 statement = cp_parser_jump_statement (parser);
6578 /* Objective-C++ exception-handling constructs. */
6581 case RID_AT_FINALLY:
6582 case RID_AT_SYNCHRONIZED:
6584 statement = cp_parser_objc_statement (parser);
6588 statement = cp_parser_try_block (parser);
6592 /* This must be a namespace alias definition. */
6593 cp_parser_declaration_statement (parser);
6597 /* It might be a keyword like `int' that can start a
6598 declaration-statement. */
6602 else if (token->type == CPP_NAME)
6604 /* If the next token is a `:', then we are looking at a
6605 labeled-statement. */
6606 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6607 if (token->type == CPP_COLON)
6609 /* Looks like a labeled-statement with an ordinary label.
6610 Parse the label, and then use tail recursion to parse
6612 cp_parser_label_for_labeled_statement (parser);
6616 /* Anything that starts with a `{' must be a compound-statement. */
6617 else if (token->type == CPP_OPEN_BRACE)
6618 statement = cp_parser_compound_statement (parser, NULL, false);
6619 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6620 a statement all its own. */
6621 else if (token->type == CPP_PRAGMA)
6623 /* Only certain OpenMP pragmas are attached to statements, and thus
6624 are considered statements themselves. All others are not. In
6625 the context of a compound, accept the pragma as a "statement" and
6626 return so that we can check for a close brace. Otherwise we
6627 require a real statement and must go back and read one. */
6629 cp_parser_pragma (parser, pragma_compound);
6630 else if (!cp_parser_pragma (parser, pragma_stmt))
6634 else if (token->type == CPP_EOF)
6636 cp_parser_error (parser, "expected statement");
6640 /* Everything else must be a declaration-statement or an
6641 expression-statement. Try for the declaration-statement
6642 first, unless we are looking at a `;', in which case we know that
6643 we have an expression-statement. */
6646 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6648 cp_parser_parse_tentatively (parser);
6649 /* Try to parse the declaration-statement. */
6650 cp_parser_declaration_statement (parser);
6651 /* If that worked, we're done. */
6652 if (cp_parser_parse_definitely (parser))
6655 /* Look for an expression-statement instead. */
6656 statement = cp_parser_expression_statement (parser, in_statement_expr);
6659 /* Set the line number for the statement. */
6660 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6661 SET_EXPR_LOCATION (statement, statement_location);
6664 /* Parse the label for a labeled-statement, i.e.
6667 case constant-expression :
6671 case constant-expression ... constant-expression : statement
6673 When a label is parsed without errors, the label is added to the
6674 parse tree by the finish_* functions, so this function doesn't
6675 have to return the label. */
6678 cp_parser_label_for_labeled_statement (cp_parser* parser)
6682 /* The next token should be an identifier. */
6683 token = cp_lexer_peek_token (parser->lexer);
6684 if (token->type != CPP_NAME
6685 && token->type != CPP_KEYWORD)
6687 cp_parser_error (parser, "expected labeled-statement");
6691 switch (token->keyword)
6698 /* Consume the `case' token. */
6699 cp_lexer_consume_token (parser->lexer);
6700 /* Parse the constant-expression. */
6701 expr = cp_parser_constant_expression (parser,
6702 /*allow_non_constant_p=*/false,
6705 ellipsis = cp_lexer_peek_token (parser->lexer);
6706 if (ellipsis->type == CPP_ELLIPSIS)
6708 /* Consume the `...' token. */
6709 cp_lexer_consume_token (parser->lexer);
6711 cp_parser_constant_expression (parser,
6712 /*allow_non_constant_p=*/false,
6714 /* We don't need to emit warnings here, as the common code
6715 will do this for us. */
6718 expr_hi = NULL_TREE;
6720 if (parser->in_switch_statement_p)
6721 finish_case_label (expr, expr_hi);
6723 error ("case label %qE not within a switch statement", expr);
6728 /* Consume the `default' token. */
6729 cp_lexer_consume_token (parser->lexer);
6731 if (parser->in_switch_statement_p)
6732 finish_case_label (NULL_TREE, NULL_TREE);
6734 error ("case label not within a switch statement");
6738 /* Anything else must be an ordinary label. */
6739 finish_label_stmt (cp_parser_identifier (parser));
6743 /* Require the `:' token. */
6744 cp_parser_require (parser, CPP_COLON, "`:'");
6747 /* Parse an expression-statement.
6749 expression-statement:
6752 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6753 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6754 indicates whether this expression-statement is part of an
6755 expression statement. */
6758 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6760 tree statement = NULL_TREE;
6762 /* If the next token is a ';', then there is no expression
6764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6765 statement = cp_parser_expression (parser, /*cast_p=*/false);
6767 /* Consume the final `;'. */
6768 cp_parser_consume_semicolon_at_end_of_statement (parser);
6770 if (in_statement_expr
6771 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6772 /* This is the final expression statement of a statement
6774 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6776 statement = finish_expr_stmt (statement);
6783 /* Parse a compound-statement.
6786 { statement-seq [opt] }
6788 Returns a tree representing the statement. */
6791 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6796 /* Consume the `{'. */
6797 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6798 return error_mark_node;
6799 /* Begin the compound-statement. */
6800 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6801 /* Parse an (optional) statement-seq. */
6802 cp_parser_statement_seq_opt (parser, in_statement_expr);
6803 /* Finish the compound-statement. */
6804 finish_compound_stmt (compound_stmt);
6805 /* Consume the `}'. */
6806 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6808 return compound_stmt;
6811 /* Parse an (optional) statement-seq.
6815 statement-seq [opt] statement */
6818 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6820 /* Scan statements until there aren't any more. */
6823 cp_token *token = cp_lexer_peek_token (parser->lexer);
6825 /* If we're looking at a `}', then we've run out of statements. */
6826 if (token->type == CPP_CLOSE_BRACE
6827 || token->type == CPP_EOF
6828 || token->type == CPP_PRAGMA_EOL)
6831 /* If we are in a compound statement and find 'else' then
6832 something went wrong. */
6833 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6835 if (parser->in_statement & IN_IF_STMT)
6839 token = cp_lexer_consume_token (parser->lexer);
6840 error ("%<else%> without a previous %<if%>");
6844 /* Parse the statement. */
6845 cp_parser_statement (parser, in_statement_expr, true, NULL);
6849 /* Parse a selection-statement.
6851 selection-statement:
6852 if ( condition ) statement
6853 if ( condition ) statement else statement
6854 switch ( condition ) statement
6856 Returns the new IF_STMT or SWITCH_STMT.
6858 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6859 is a (possibly labeled) if statement which is not enclosed in
6860 braces and has an else clause. This is used to implement
6864 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6872 /* Peek at the next token. */
6873 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6875 /* See what kind of keyword it is. */
6876 keyword = token->keyword;
6885 /* Look for the `('. */
6886 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6888 cp_parser_skip_to_end_of_statement (parser);
6889 return error_mark_node;
6892 /* Begin the selection-statement. */
6893 if (keyword == RID_IF)
6894 statement = begin_if_stmt ();
6896 statement = begin_switch_stmt ();
6898 /* Parse the condition. */
6899 condition = cp_parser_condition (parser);
6900 /* Look for the `)'. */
6901 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6902 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6903 /*consume_paren=*/true);
6905 if (keyword == RID_IF)
6908 unsigned char in_statement;
6910 /* Add the condition. */
6911 finish_if_stmt_cond (condition, statement);
6913 /* Parse the then-clause. */
6914 in_statement = parser->in_statement;
6915 parser->in_statement |= IN_IF_STMT;
6916 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6917 parser->in_statement = in_statement;
6919 finish_then_clause (statement);
6921 /* If the next token is `else', parse the else-clause. */
6922 if (cp_lexer_next_token_is_keyword (parser->lexer,
6925 /* Consume the `else' keyword. */
6926 cp_lexer_consume_token (parser->lexer);
6927 begin_else_clause (statement);
6928 /* Parse the else-clause. */
6929 cp_parser_implicitly_scoped_statement (parser, NULL);
6930 finish_else_clause (statement);
6932 /* If we are currently parsing a then-clause, then
6933 IF_P will not be NULL. We set it to true to
6934 indicate that this if statement has an else clause.
6935 This may trigger the Wparentheses warning below
6936 when we get back up to the parent if statement. */
6942 /* This if statement does not have an else clause. If
6943 NESTED_IF is true, then the then-clause is an if
6944 statement which does have an else clause. We warn
6945 about the potential ambiguity. */
6947 warning (OPT_Wparentheses,
6948 ("%Hsuggest explicit braces "
6949 "to avoid ambiguous %<else%>"),
6950 EXPR_LOCUS (statement));
6953 /* Now we're all done with the if-statement. */
6954 finish_if_stmt (statement);
6958 bool in_switch_statement_p;
6959 unsigned char in_statement;
6961 /* Add the condition. */
6962 finish_switch_cond (condition, statement);
6964 /* Parse the body of the switch-statement. */
6965 in_switch_statement_p = parser->in_switch_statement_p;
6966 in_statement = parser->in_statement;
6967 parser->in_switch_statement_p = true;
6968 parser->in_statement |= IN_SWITCH_STMT;
6969 cp_parser_implicitly_scoped_statement (parser, NULL);
6970 parser->in_switch_statement_p = in_switch_statement_p;
6971 parser->in_statement = in_statement;
6973 /* Now we're all done with the switch-statement. */
6974 finish_switch_stmt (statement);
6982 cp_parser_error (parser, "expected selection-statement");
6983 return error_mark_node;
6987 /* Parse a condition.
6991 type-specifier-seq declarator = assignment-expression
6996 type-specifier-seq declarator asm-specification [opt]
6997 attributes [opt] = assignment-expression
6999 Returns the expression that should be tested. */
7002 cp_parser_condition (cp_parser* parser)
7004 cp_decl_specifier_seq type_specifiers;
7005 const char *saved_message;
7007 /* Try the declaration first. */
7008 cp_parser_parse_tentatively (parser);
7009 /* New types are not allowed in the type-specifier-seq for a
7011 saved_message = parser->type_definition_forbidden_message;
7012 parser->type_definition_forbidden_message
7013 = "types may not be defined in conditions";
7014 /* Parse the type-specifier-seq. */
7015 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7017 /* Restore the saved message. */
7018 parser->type_definition_forbidden_message = saved_message;
7019 /* If all is well, we might be looking at a declaration. */
7020 if (!cp_parser_error_occurred (parser))
7023 tree asm_specification;
7025 cp_declarator *declarator;
7026 tree initializer = NULL_TREE;
7028 /* Parse the declarator. */
7029 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7030 /*ctor_dtor_or_conv_p=*/NULL,
7031 /*parenthesized_p=*/NULL,
7032 /*member_p=*/false);
7033 /* Parse the attributes. */
7034 attributes = cp_parser_attributes_opt (parser);
7035 /* Parse the asm-specification. */
7036 asm_specification = cp_parser_asm_specification_opt (parser);
7037 /* If the next token is not an `=', then we might still be
7038 looking at an expression. For example:
7042 looks like a decl-specifier-seq and a declarator -- but then
7043 there is no `=', so this is an expression. */
7044 cp_parser_require (parser, CPP_EQ, "`='");
7045 /* If we did see an `=', then we are looking at a declaration
7047 if (cp_parser_parse_definitely (parser))
7050 bool non_constant_p;
7052 /* Create the declaration. */
7053 decl = start_decl (declarator, &type_specifiers,
7054 /*initialized_p=*/true,
7055 attributes, /*prefix_attributes=*/NULL_TREE,
7057 /* Parse the assignment-expression. */
7059 = cp_parser_constant_expression (parser,
7060 /*allow_non_constant_p=*/true,
7062 if (!non_constant_p)
7063 initializer = fold_non_dependent_expr (initializer);
7065 /* Process the initializer. */
7066 cp_finish_decl (decl,
7067 initializer, !non_constant_p,
7069 LOOKUP_ONLYCONVERTING);
7072 pop_scope (pushed_scope);
7074 return convert_from_reference (decl);
7077 /* If we didn't even get past the declarator successfully, we are
7078 definitely not looking at a declaration. */
7080 cp_parser_abort_tentative_parse (parser);
7082 /* Otherwise, we are looking at an expression. */
7083 return cp_parser_expression (parser, /*cast_p=*/false);
7086 /* We check for a ) immediately followed by ; with no whitespacing
7087 between. This is used to issue a warning for:
7095 as the semicolon is probably extraneous.
7097 On parse errors, the next token might not be a ), so do nothing in
7101 check_empty_body (cp_parser* parser, const char* type)
7104 cp_token *close_paren;
7105 expanded_location close_loc;
7106 expanded_location semi_loc;
7108 close_paren = cp_lexer_peek_token (parser->lexer);
7109 if (close_paren->type != CPP_CLOSE_PAREN)
7112 close_loc = expand_location (close_paren->location);
7113 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7115 if (token->type != CPP_SEMICOLON
7116 || (token->flags & PREV_WHITE))
7119 semi_loc = expand_location (token->location);
7120 if (close_loc.line == semi_loc.line
7121 #ifdef USE_MAPPED_LOCATION
7122 && close_loc.column+1 == semi_loc.column
7125 warning (OPT_Wempty_body,
7126 "suggest a space before %<;%> or explicit braces around empty "
7127 "body in %<%s%> statement",
7131 /* Parse an iteration-statement.
7133 iteration-statement:
7134 while ( condition ) statement
7135 do statement while ( expression ) ;
7136 for ( for-init-statement condition [opt] ; expression [opt] )
7139 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7142 cp_parser_iteration_statement (cp_parser* parser)
7147 unsigned char in_statement;
7149 /* Peek at the next token. */
7150 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7152 return error_mark_node;
7154 /* Remember whether or not we are already within an iteration
7156 in_statement = parser->in_statement;
7158 /* See what kind of keyword it is. */
7159 keyword = token->keyword;
7166 /* Begin the while-statement. */
7167 statement = begin_while_stmt ();
7168 /* Look for the `('. */
7169 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7170 /* Parse the condition. */
7171 condition = cp_parser_condition (parser);
7172 finish_while_stmt_cond (condition, statement);
7173 check_empty_body (parser, "while");
7174 /* Look for the `)'. */
7175 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7176 /* Parse the dependent statement. */
7177 parser->in_statement = IN_ITERATION_STMT;
7178 cp_parser_already_scoped_statement (parser);
7179 parser->in_statement = in_statement;
7180 /* We're done with the while-statement. */
7181 finish_while_stmt (statement);
7189 /* Begin the do-statement. */
7190 statement = begin_do_stmt ();
7191 /* Parse the body of the do-statement. */
7192 parser->in_statement = IN_ITERATION_STMT;
7193 cp_parser_implicitly_scoped_statement (parser, NULL);
7194 parser->in_statement = in_statement;
7195 finish_do_body (statement);
7196 /* Look for the `while' keyword. */
7197 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7198 /* Look for the `('. */
7199 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7200 /* Parse the expression. */
7201 expression = cp_parser_expression (parser, /*cast_p=*/false);
7202 /* We're done with the do-statement. */
7203 finish_do_stmt (expression, statement);
7204 /* Look for the `)'. */
7205 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7206 /* Look for the `;'. */
7207 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7213 tree condition = NULL_TREE;
7214 tree expression = NULL_TREE;
7216 /* Begin the for-statement. */
7217 statement = begin_for_stmt ();
7218 /* Look for the `('. */
7219 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7220 /* Parse the initialization. */
7221 cp_parser_for_init_statement (parser);
7222 finish_for_init_stmt (statement);
7224 /* If there's a condition, process it. */
7225 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7226 condition = cp_parser_condition (parser);
7227 finish_for_cond (condition, statement);
7228 /* Look for the `;'. */
7229 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7231 /* If there's an expression, process it. */
7232 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7233 expression = cp_parser_expression (parser, /*cast_p=*/false);
7234 finish_for_expr (expression, statement);
7235 check_empty_body (parser, "for");
7236 /* Look for the `)'. */
7237 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7239 /* Parse the body of the for-statement. */
7240 parser->in_statement = IN_ITERATION_STMT;
7241 cp_parser_already_scoped_statement (parser);
7242 parser->in_statement = in_statement;
7244 /* We're done with the for-statement. */
7245 finish_for_stmt (statement);
7250 cp_parser_error (parser, "expected iteration-statement");
7251 statement = error_mark_node;
7258 /* Parse a for-init-statement.
7261 expression-statement
7262 simple-declaration */
7265 cp_parser_for_init_statement (cp_parser* parser)
7267 /* If the next token is a `;', then we have an empty
7268 expression-statement. Grammatically, this is also a
7269 simple-declaration, but an invalid one, because it does not
7270 declare anything. Therefore, if we did not handle this case
7271 specially, we would issue an error message about an invalid
7273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7275 /* We're going to speculatively look for a declaration, falling back
7276 to an expression, if necessary. */
7277 cp_parser_parse_tentatively (parser);
7278 /* Parse the declaration. */
7279 cp_parser_simple_declaration (parser,
7280 /*function_definition_allowed_p=*/false);
7281 /* If the tentative parse failed, then we shall need to look for an
7282 expression-statement. */
7283 if (cp_parser_parse_definitely (parser))
7287 cp_parser_expression_statement (parser, false);
7290 /* Parse a jump-statement.
7295 return expression [opt] ;
7303 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7306 cp_parser_jump_statement (cp_parser* parser)
7308 tree statement = error_mark_node;
7311 unsigned char in_statement;
7313 /* Peek at the next token. */
7314 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7316 return error_mark_node;
7318 /* See what kind of keyword it is. */
7319 keyword = token->keyword;
7323 in_statement = parser->in_statement & ~IN_IF_STMT;
7324 switch (in_statement)
7327 error ("break statement not within loop or switch");
7330 gcc_assert ((in_statement & IN_SWITCH_STMT)
7331 || in_statement == IN_ITERATION_STMT);
7332 statement = finish_break_stmt ();
7335 error ("invalid exit from OpenMP structured block");
7338 error ("break statement used with OpenMP for loop");
7341 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7345 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7348 error ("continue statement not within a loop");
7350 case IN_ITERATION_STMT:
7352 statement = finish_continue_stmt ();
7355 error ("invalid exit from OpenMP structured block");
7360 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7367 /* If the next token is a `;', then there is no
7369 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7370 expr = cp_parser_expression (parser, /*cast_p=*/false);
7373 /* Build the return-statement. */
7374 statement = finish_return_stmt (expr);
7375 /* Look for the final `;'. */
7376 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7381 /* Create the goto-statement. */
7382 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7384 /* Issue a warning about this use of a GNU extension. */
7386 pedwarn ("ISO C++ forbids computed gotos");
7387 /* Consume the '*' token. */
7388 cp_lexer_consume_token (parser->lexer);
7389 /* Parse the dependent expression. */
7390 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7393 finish_goto_stmt (cp_parser_identifier (parser));
7394 /* Look for the final `;'. */
7395 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7399 cp_parser_error (parser, "expected jump-statement");
7406 /* Parse a declaration-statement.
7408 declaration-statement:
7409 block-declaration */
7412 cp_parser_declaration_statement (cp_parser* parser)
7416 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7417 p = obstack_alloc (&declarator_obstack, 0);
7419 /* Parse the block-declaration. */
7420 cp_parser_block_declaration (parser, /*statement_p=*/true);
7422 /* Free any declarators allocated. */
7423 obstack_free (&declarator_obstack, p);
7425 /* Finish off the statement. */
7429 /* Some dependent statements (like `if (cond) statement'), are
7430 implicitly in their own scope. In other words, if the statement is
7431 a single statement (as opposed to a compound-statement), it is
7432 none-the-less treated as if it were enclosed in braces. Any
7433 declarations appearing in the dependent statement are out of scope
7434 after control passes that point. This function parses a statement,
7435 but ensures that is in its own scope, even if it is not a
7438 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7439 is a (possibly labeled) if statement which is not enclosed in
7440 braces and has an else clause. This is used to implement
7443 Returns the new statement. */
7446 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7453 /* Mark if () ; with a special NOP_EXPR. */
7454 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7456 cp_lexer_consume_token (parser->lexer);
7457 statement = add_stmt (build_empty_stmt ());
7459 /* if a compound is opened, we simply parse the statement directly. */
7460 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7461 statement = cp_parser_compound_statement (parser, NULL, false);
7462 /* If the token is not a `{', then we must take special action. */
7465 /* Create a compound-statement. */
7466 statement = begin_compound_stmt (0);
7467 /* Parse the dependent-statement. */
7468 cp_parser_statement (parser, NULL_TREE, false, if_p);
7469 /* Finish the dummy compound-statement. */
7470 finish_compound_stmt (statement);
7473 /* Return the statement. */
7477 /* For some dependent statements (like `while (cond) statement'), we
7478 have already created a scope. Therefore, even if the dependent
7479 statement is a compound-statement, we do not want to create another
7483 cp_parser_already_scoped_statement (cp_parser* parser)
7485 /* If the token is a `{', then we must take special action. */
7486 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7487 cp_parser_statement (parser, NULL_TREE, false, NULL);
7490 /* Avoid calling cp_parser_compound_statement, so that we
7491 don't create a new scope. Do everything else by hand. */
7492 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7493 cp_parser_statement_seq_opt (parser, NULL_TREE);
7494 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7498 /* Declarations [gram.dcl.dcl] */
7500 /* Parse an optional declaration-sequence.
7504 declaration-seq declaration */
7507 cp_parser_declaration_seq_opt (cp_parser* parser)
7513 token = cp_lexer_peek_token (parser->lexer);
7515 if (token->type == CPP_CLOSE_BRACE
7516 || token->type == CPP_EOF
7517 || token->type == CPP_PRAGMA_EOL)
7520 if (token->type == CPP_SEMICOLON)
7522 /* A declaration consisting of a single semicolon is
7523 invalid. Allow it unless we're being pedantic. */
7524 cp_lexer_consume_token (parser->lexer);
7525 if (pedantic && !in_system_header)
7526 pedwarn ("extra %<;%>");
7530 /* If we're entering or exiting a region that's implicitly
7531 extern "C", modify the lang context appropriately. */
7532 if (!parser->implicit_extern_c && token->implicit_extern_c)
7534 push_lang_context (lang_name_c);
7535 parser->implicit_extern_c = true;
7537 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7539 pop_lang_context ();
7540 parser->implicit_extern_c = false;
7543 if (token->type == CPP_PRAGMA)
7545 /* A top-level declaration can consist solely of a #pragma.
7546 A nested declaration cannot, so this is done here and not
7547 in cp_parser_declaration. (A #pragma at block scope is
7548 handled in cp_parser_statement.) */
7549 cp_parser_pragma (parser, pragma_external);
7553 /* Parse the declaration itself. */
7554 cp_parser_declaration (parser);
7558 /* Parse a declaration.
7563 template-declaration
7564 explicit-instantiation
7565 explicit-specialization
7566 linkage-specification
7567 namespace-definition
7572 __extension__ declaration */
7575 cp_parser_declaration (cp_parser* parser)
7582 /* Check for the `__extension__' keyword. */
7583 if (cp_parser_extension_opt (parser, &saved_pedantic))
7585 /* Parse the qualified declaration. */
7586 cp_parser_declaration (parser);
7587 /* Restore the PEDANTIC flag. */
7588 pedantic = saved_pedantic;
7593 /* Try to figure out what kind of declaration is present. */
7594 token1 = *cp_lexer_peek_token (parser->lexer);
7596 if (token1.type != CPP_EOF)
7597 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7600 token2.type = CPP_EOF;
7601 token2.keyword = RID_MAX;
7604 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7605 p = obstack_alloc (&declarator_obstack, 0);
7607 /* If the next token is `extern' and the following token is a string
7608 literal, then we have a linkage specification. */
7609 if (token1.keyword == RID_EXTERN
7610 && cp_parser_is_string_literal (&token2))
7611 cp_parser_linkage_specification (parser);
7612 /* If the next token is `template', then we have either a template
7613 declaration, an explicit instantiation, or an explicit
7615 else if (token1.keyword == RID_TEMPLATE)
7617 /* `template <>' indicates a template specialization. */
7618 if (token2.type == CPP_LESS
7619 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7620 cp_parser_explicit_specialization (parser);
7621 /* `template <' indicates a template declaration. */
7622 else if (token2.type == CPP_LESS)
7623 cp_parser_template_declaration (parser, /*member_p=*/false);
7624 /* Anything else must be an explicit instantiation. */
7626 cp_parser_explicit_instantiation (parser);
7628 /* If the next token is `export', then we have a template
7630 else if (token1.keyword == RID_EXPORT)
7631 cp_parser_template_declaration (parser, /*member_p=*/false);
7632 /* If the next token is `extern', 'static' or 'inline' and the one
7633 after that is `template', we have a GNU extended explicit
7634 instantiation directive. */
7635 else if (cp_parser_allow_gnu_extensions_p (parser)
7636 && (token1.keyword == RID_EXTERN
7637 || token1.keyword == RID_STATIC
7638 || token1.keyword == RID_INLINE)
7639 && token2.keyword == RID_TEMPLATE)
7640 cp_parser_explicit_instantiation (parser);
7641 /* If the next token is `namespace', check for a named or unnamed
7642 namespace definition. */
7643 else if (token1.keyword == RID_NAMESPACE
7644 && (/* A named namespace definition. */
7645 (token2.type == CPP_NAME
7646 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7648 /* An unnamed namespace definition. */
7649 || token2.type == CPP_OPEN_BRACE
7650 || token2.keyword == RID_ATTRIBUTE))
7651 cp_parser_namespace_definition (parser);
7652 /* Objective-C++ declaration/definition. */
7653 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7654 cp_parser_objc_declaration (parser);
7655 /* We must have either a block declaration or a function
7658 /* Try to parse a block-declaration, or a function-definition. */
7659 cp_parser_block_declaration (parser, /*statement_p=*/false);
7661 /* Free any declarators allocated. */
7662 obstack_free (&declarator_obstack, p);
7665 /* Parse a block-declaration.
7670 namespace-alias-definition
7677 __extension__ block-declaration
7683 static_assert-declaration
7685 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7686 part of a declaration-statement. */
7689 cp_parser_block_declaration (cp_parser *parser,
7695 /* Check for the `__extension__' keyword. */
7696 if (cp_parser_extension_opt (parser, &saved_pedantic))
7698 /* Parse the qualified declaration. */
7699 cp_parser_block_declaration (parser, statement_p);
7700 /* Restore the PEDANTIC flag. */
7701 pedantic = saved_pedantic;
7706 /* Peek at the next token to figure out which kind of declaration is
7708 token1 = cp_lexer_peek_token (parser->lexer);
7710 /* If the next keyword is `asm', we have an asm-definition. */
7711 if (token1->keyword == RID_ASM)
7714 cp_parser_commit_to_tentative_parse (parser);
7715 cp_parser_asm_definition (parser);
7717 /* If the next keyword is `namespace', we have a
7718 namespace-alias-definition. */
7719 else if (token1->keyword == RID_NAMESPACE)
7720 cp_parser_namespace_alias_definition (parser);
7721 /* If the next keyword is `using', we have either a
7722 using-declaration or a using-directive. */
7723 else if (token1->keyword == RID_USING)
7728 cp_parser_commit_to_tentative_parse (parser);
7729 /* If the token after `using' is `namespace', then we have a
7731 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7732 if (token2->keyword == RID_NAMESPACE)
7733 cp_parser_using_directive (parser);
7734 /* Otherwise, it's a using-declaration. */
7736 cp_parser_using_declaration (parser,
7737 /*access_declaration_p=*/false);
7739 /* If the next keyword is `__label__' we have a label declaration. */
7740 else if (token1->keyword == RID_LABEL)
7743 cp_parser_commit_to_tentative_parse (parser);
7744 cp_parser_label_declaration (parser);
7746 /* If the next token is `static_assert' we have a static assertion. */
7747 else if (token1->keyword == RID_STATIC_ASSERT)
7748 cp_parser_static_assert (parser, /*member_p=*/false);
7749 /* Anything else must be a simple-declaration. */
7751 cp_parser_simple_declaration (parser, !statement_p);
7754 /* Parse a simple-declaration.
7757 decl-specifier-seq [opt] init-declarator-list [opt] ;
7759 init-declarator-list:
7761 init-declarator-list , init-declarator
7763 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7764 function-definition as a simple-declaration. */
7767 cp_parser_simple_declaration (cp_parser* parser,
7768 bool function_definition_allowed_p)
7770 cp_decl_specifier_seq decl_specifiers;
7771 int declares_class_or_enum;
7772 bool saw_declarator;
7774 /* Defer access checks until we know what is being declared; the
7775 checks for names appearing in the decl-specifier-seq should be
7776 done as if we were in the scope of the thing being declared. */
7777 push_deferring_access_checks (dk_deferred);
7779 /* Parse the decl-specifier-seq. We have to keep track of whether
7780 or not the decl-specifier-seq declares a named class or
7781 enumeration type, since that is the only case in which the
7782 init-declarator-list is allowed to be empty.
7786 In a simple-declaration, the optional init-declarator-list can be
7787 omitted only when declaring a class or enumeration, that is when
7788 the decl-specifier-seq contains either a class-specifier, an
7789 elaborated-type-specifier, or an enum-specifier. */
7790 cp_parser_decl_specifier_seq (parser,
7791 CP_PARSER_FLAGS_OPTIONAL,
7793 &declares_class_or_enum);
7794 /* We no longer need to defer access checks. */
7795 stop_deferring_access_checks ();
7797 /* In a block scope, a valid declaration must always have a
7798 decl-specifier-seq. By not trying to parse declarators, we can
7799 resolve the declaration/expression ambiguity more quickly. */
7800 if (!function_definition_allowed_p
7801 && !decl_specifiers.any_specifiers_p)
7803 cp_parser_error (parser, "expected declaration");
7807 /* If the next two tokens are both identifiers, the code is
7808 erroneous. The usual cause of this situation is code like:
7812 where "T" should name a type -- but does not. */
7813 if (!decl_specifiers.type
7814 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7816 /* If parsing tentatively, we should commit; we really are
7817 looking at a declaration. */
7818 cp_parser_commit_to_tentative_parse (parser);
7823 /* If we have seen at least one decl-specifier, and the next token
7824 is not a parenthesis, then we must be looking at a declaration.
7825 (After "int (" we might be looking at a functional cast.) */
7826 if (decl_specifiers.any_specifiers_p
7827 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7828 cp_parser_commit_to_tentative_parse (parser);
7830 /* Keep going until we hit the `;' at the end of the simple
7832 saw_declarator = false;
7833 while (cp_lexer_next_token_is_not (parser->lexer,
7837 bool function_definition_p;
7842 /* If we are processing next declarator, coma is expected */
7843 token = cp_lexer_peek_token (parser->lexer);
7844 gcc_assert (token->type == CPP_COMMA);
7845 cp_lexer_consume_token (parser->lexer);
7848 saw_declarator = true;
7850 /* Parse the init-declarator. */
7851 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7853 function_definition_allowed_p,
7855 declares_class_or_enum,
7856 &function_definition_p);
7857 /* If an error occurred while parsing tentatively, exit quickly.
7858 (That usually happens when in the body of a function; each
7859 statement is treated as a declaration-statement until proven
7861 if (cp_parser_error_occurred (parser))
7863 /* Handle function definitions specially. */
7864 if (function_definition_p)
7866 /* If the next token is a `,', then we are probably
7867 processing something like:
7871 which is erroneous. */
7872 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7873 error ("mixing declarations and function-definitions is forbidden");
7874 /* Otherwise, we're done with the list of declarators. */
7877 pop_deferring_access_checks ();
7881 /* The next token should be either a `,' or a `;'. */
7882 token = cp_lexer_peek_token (parser->lexer);
7883 /* If it's a `,', there are more declarators to come. */
7884 if (token->type == CPP_COMMA)
7885 /* will be consumed next time around */;
7886 /* If it's a `;', we are done. */
7887 else if (token->type == CPP_SEMICOLON)
7889 /* Anything else is an error. */
7892 /* If we have already issued an error message we don't need
7893 to issue another one. */
7894 if (decl != error_mark_node
7895 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7896 cp_parser_error (parser, "expected %<,%> or %<;%>");
7897 /* Skip tokens until we reach the end of the statement. */
7898 cp_parser_skip_to_end_of_statement (parser);
7899 /* If the next token is now a `;', consume it. */
7900 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7901 cp_lexer_consume_token (parser->lexer);
7904 /* After the first time around, a function-definition is not
7905 allowed -- even if it was OK at first. For example:
7910 function_definition_allowed_p = false;
7913 /* Issue an error message if no declarators are present, and the
7914 decl-specifier-seq does not itself declare a class or
7916 if (!saw_declarator)
7918 if (cp_parser_declares_only_class_p (parser))
7919 shadow_tag (&decl_specifiers);
7920 /* Perform any deferred access checks. */
7921 perform_deferred_access_checks ();
7924 /* Consume the `;'. */
7925 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7928 pop_deferring_access_checks ();
7931 /* Parse a decl-specifier-seq.
7934 decl-specifier-seq [opt] decl-specifier
7937 storage-class-specifier
7948 Set *DECL_SPECS to a representation of the decl-specifier-seq.
7950 The parser flags FLAGS is used to control type-specifier parsing.
7952 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7955 1: one of the decl-specifiers is an elaborated-type-specifier
7956 (i.e., a type declaration)
7957 2: one of the decl-specifiers is an enum-specifier or a
7958 class-specifier (i.e., a type definition)
7963 cp_parser_decl_specifier_seq (cp_parser* parser,
7964 cp_parser_flags flags,
7965 cp_decl_specifier_seq *decl_specs,
7966 int* declares_class_or_enum)
7968 bool constructor_possible_p = !parser->in_declarator_p;
7970 /* Clear DECL_SPECS. */
7971 clear_decl_specs (decl_specs);
7973 /* Assume no class or enumeration type is declared. */
7974 *declares_class_or_enum = 0;
7976 /* Keep reading specifiers until there are no more to read. */
7980 bool found_decl_spec;
7983 /* Peek at the next token. */
7984 token = cp_lexer_peek_token (parser->lexer);
7985 /* Handle attributes. */
7986 if (token->keyword == RID_ATTRIBUTE)
7988 /* Parse the attributes. */
7989 decl_specs->attributes
7990 = chainon (decl_specs->attributes,
7991 cp_parser_attributes_opt (parser));
7994 /* Assume we will find a decl-specifier keyword. */
7995 found_decl_spec = true;
7996 /* If the next token is an appropriate keyword, we can simply
7997 add it to the list. */
7998 switch (token->keyword)
8003 if (!at_class_scope_p ())
8005 error ("%<friend%> used outside of class");
8006 cp_lexer_purge_token (parser->lexer);
8010 ++decl_specs->specs[(int) ds_friend];
8011 /* Consume the token. */
8012 cp_lexer_consume_token (parser->lexer);
8016 /* function-specifier:
8023 cp_parser_function_specifier_opt (parser, decl_specs);
8029 ++decl_specs->specs[(int) ds_typedef];
8030 /* Consume the token. */
8031 cp_lexer_consume_token (parser->lexer);
8032 /* A constructor declarator cannot appear in a typedef. */
8033 constructor_possible_p = false;
8034 /* The "typedef" keyword can only occur in a declaration; we
8035 may as well commit at this point. */
8036 cp_parser_commit_to_tentative_parse (parser);
8038 if (decl_specs->storage_class != sc_none)
8039 decl_specs->conflicting_specifiers_p = true;
8042 /* storage-class-specifier:
8056 /* Consume the token. */
8057 cp_lexer_consume_token (parser->lexer);
8058 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8061 /* Consume the token. */
8062 cp_lexer_consume_token (parser->lexer);
8063 ++decl_specs->specs[(int) ds_thread];
8067 /* We did not yet find a decl-specifier yet. */
8068 found_decl_spec = false;
8072 /* Constructors are a special case. The `S' in `S()' is not a
8073 decl-specifier; it is the beginning of the declarator. */
8076 && constructor_possible_p
8077 && (cp_parser_constructor_declarator_p
8078 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8080 /* If we don't have a DECL_SPEC yet, then we must be looking at
8081 a type-specifier. */
8082 if (!found_decl_spec && !constructor_p)
8084 int decl_spec_declares_class_or_enum;
8085 bool is_cv_qualifier;
8089 = cp_parser_type_specifier (parser, flags,
8091 /*is_declaration=*/true,
8092 &decl_spec_declares_class_or_enum,
8095 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8097 /* If this type-specifier referenced a user-defined type
8098 (a typedef, class-name, etc.), then we can't allow any
8099 more such type-specifiers henceforth.
8103 The longest sequence of decl-specifiers that could
8104 possibly be a type name is taken as the
8105 decl-specifier-seq of a declaration. The sequence shall
8106 be self-consistent as described below.
8110 As a general rule, at most one type-specifier is allowed
8111 in the complete decl-specifier-seq of a declaration. The
8112 only exceptions are the following:
8114 -- const or volatile can be combined with any other
8117 -- signed or unsigned can be combined with char, long,
8125 void g (const int Pc);
8127 Here, Pc is *not* part of the decl-specifier seq; it's
8128 the declarator. Therefore, once we see a type-specifier
8129 (other than a cv-qualifier), we forbid any additional
8130 user-defined types. We *do* still allow things like `int
8131 int' to be considered a decl-specifier-seq, and issue the
8132 error message later. */
8133 if (type_spec && !is_cv_qualifier)
8134 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8135 /* A constructor declarator cannot follow a type-specifier. */
8138 constructor_possible_p = false;
8139 found_decl_spec = true;
8143 /* If we still do not have a DECL_SPEC, then there are no more
8145 if (!found_decl_spec)
8148 decl_specs->any_specifiers_p = true;
8149 /* After we see one decl-specifier, further decl-specifiers are
8151 flags |= CP_PARSER_FLAGS_OPTIONAL;
8154 cp_parser_check_decl_spec (decl_specs);
8156 /* Don't allow a friend specifier with a class definition. */
8157 if (decl_specs->specs[(int) ds_friend] != 0
8158 && (*declares_class_or_enum & 2))
8159 error ("class definition may not be declared a friend");
8162 /* Parse an (optional) storage-class-specifier.
8164 storage-class-specifier:
8173 storage-class-specifier:
8176 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8179 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8181 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8189 /* Consume the token. */
8190 return cp_lexer_consume_token (parser->lexer)->u.value;
8197 /* Parse an (optional) function-specifier.
8204 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8205 Updates DECL_SPECS, if it is non-NULL. */
8208 cp_parser_function_specifier_opt (cp_parser* parser,
8209 cp_decl_specifier_seq *decl_specs)
8211 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8215 ++decl_specs->specs[(int) ds_inline];
8219 /* 14.5.2.3 [temp.mem]
8221 A member function template shall not be virtual. */
8222 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8223 error ("templates may not be %<virtual%>");
8224 else if (decl_specs)
8225 ++decl_specs->specs[(int) ds_virtual];
8230 ++decl_specs->specs[(int) ds_explicit];
8237 /* Consume the token. */
8238 return cp_lexer_consume_token (parser->lexer)->u.value;
8241 /* Parse a linkage-specification.
8243 linkage-specification:
8244 extern string-literal { declaration-seq [opt] }
8245 extern string-literal declaration */
8248 cp_parser_linkage_specification (cp_parser* parser)
8252 /* Look for the `extern' keyword. */
8253 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8255 /* Look for the string-literal. */
8256 linkage = cp_parser_string_literal (parser, false, false);
8258 /* Transform the literal into an identifier. If the literal is a
8259 wide-character string, or contains embedded NULs, then we can't
8260 handle it as the user wants. */
8261 if (strlen (TREE_STRING_POINTER (linkage))
8262 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8264 cp_parser_error (parser, "invalid linkage-specification");
8265 /* Assume C++ linkage. */
8266 linkage = lang_name_cplusplus;
8269 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8271 /* We're now using the new linkage. */
8272 push_lang_context (linkage);
8274 /* If the next token is a `{', then we're using the first
8276 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8278 /* Consume the `{' token. */
8279 cp_lexer_consume_token (parser->lexer);
8280 /* Parse the declarations. */
8281 cp_parser_declaration_seq_opt (parser);
8282 /* Look for the closing `}'. */
8283 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8285 /* Otherwise, there's just one declaration. */
8288 bool saved_in_unbraced_linkage_specification_p;
8290 saved_in_unbraced_linkage_specification_p
8291 = parser->in_unbraced_linkage_specification_p;
8292 parser->in_unbraced_linkage_specification_p = true;
8293 cp_parser_declaration (parser);
8294 parser->in_unbraced_linkage_specification_p
8295 = saved_in_unbraced_linkage_specification_p;
8298 /* We're done with the linkage-specification. */
8299 pop_lang_context ();
8302 /* Parse a static_assert-declaration.
8304 static_assert-declaration:
8305 static_assert ( constant-expression , string-literal ) ;
8307 If MEMBER_P, this static_assert is a class member. */
8310 cp_parser_static_assert(cp_parser *parser, bool member_p)
8315 location_t saved_loc;
8317 /* Peek at the `static_assert' token so we can keep track of exactly
8318 where the static assertion started. */
8319 token = cp_lexer_peek_token (parser->lexer);
8320 saved_loc = token->location;
8322 /* Look for the `static_assert' keyword. */
8323 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8327 /* We know we are in a static assertion; commit to any tentative
8329 if (cp_parser_parsing_tentatively (parser))
8330 cp_parser_commit_to_tentative_parse (parser);
8332 /* Parse the `(' starting the static assertion condition. */
8333 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8335 /* Parse the constant-expression. */
8337 cp_parser_constant_expression (parser,
8338 /*allow_non_constant_p=*/false,
8339 /*non_constant_p=*/NULL);
8341 /* Parse the separating `,'. */
8342 cp_parser_require (parser, CPP_COMMA, "`,'");
8344 /* Parse the string-literal message. */
8345 message = cp_parser_string_literal (parser,
8346 /*translate=*/false,
8349 /* A `)' completes the static assertion. */
8350 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8351 cp_parser_skip_to_closing_parenthesis (parser,
8352 /*recovering=*/true,
8354 /*consume_paren=*/true);
8356 /* A semicolon terminates the declaration. */
8357 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8359 /* Complete the static assertion, which may mean either processing
8360 the static assert now or saving it for template instantiation. */
8361 finish_static_assert (condition, message, saved_loc, member_p);
8364 /* Special member functions [gram.special] */
8366 /* Parse a conversion-function-id.
8368 conversion-function-id:
8369 operator conversion-type-id
8371 Returns an IDENTIFIER_NODE representing the operator. */
8374 cp_parser_conversion_function_id (cp_parser* parser)
8378 tree saved_qualifying_scope;
8379 tree saved_object_scope;
8380 tree pushed_scope = NULL_TREE;
8382 /* Look for the `operator' token. */
8383 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8384 return error_mark_node;
8385 /* When we parse the conversion-type-id, the current scope will be
8386 reset. However, we need that information in able to look up the
8387 conversion function later, so we save it here. */
8388 saved_scope = parser->scope;
8389 saved_qualifying_scope = parser->qualifying_scope;
8390 saved_object_scope = parser->object_scope;
8391 /* We must enter the scope of the class so that the names of
8392 entities declared within the class are available in the
8393 conversion-type-id. For example, consider:
8400 S::operator I() { ... }
8402 In order to see that `I' is a type-name in the definition, we
8403 must be in the scope of `S'. */
8405 pushed_scope = push_scope (saved_scope);
8406 /* Parse the conversion-type-id. */
8407 type = cp_parser_conversion_type_id (parser);
8408 /* Leave the scope of the class, if any. */
8410 pop_scope (pushed_scope);
8411 /* Restore the saved scope. */
8412 parser->scope = saved_scope;
8413 parser->qualifying_scope = saved_qualifying_scope;
8414 parser->object_scope = saved_object_scope;
8415 /* If the TYPE is invalid, indicate failure. */
8416 if (type == error_mark_node)
8417 return error_mark_node;
8418 return mangle_conv_op_name_for_type (type);
8421 /* Parse a conversion-type-id:
8424 type-specifier-seq conversion-declarator [opt]
8426 Returns the TYPE specified. */
8429 cp_parser_conversion_type_id (cp_parser* parser)
8432 cp_decl_specifier_seq type_specifiers;
8433 cp_declarator *declarator;
8434 tree type_specified;
8436 /* Parse the attributes. */
8437 attributes = cp_parser_attributes_opt (parser);
8438 /* Parse the type-specifiers. */
8439 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8441 /* If that didn't work, stop. */
8442 if (type_specifiers.type == error_mark_node)
8443 return error_mark_node;
8444 /* Parse the conversion-declarator. */
8445 declarator = cp_parser_conversion_declarator_opt (parser);
8447 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8448 /*initialized=*/0, &attributes);
8450 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8451 return type_specified;
8454 /* Parse an (optional) conversion-declarator.
8456 conversion-declarator:
8457 ptr-operator conversion-declarator [opt]
8461 static cp_declarator *
8462 cp_parser_conversion_declarator_opt (cp_parser* parser)
8464 enum tree_code code;
8466 cp_cv_quals cv_quals;
8468 /* We don't know if there's a ptr-operator next, or not. */
8469 cp_parser_parse_tentatively (parser);
8470 /* Try the ptr-operator. */
8471 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8472 /* If it worked, look for more conversion-declarators. */
8473 if (cp_parser_parse_definitely (parser))
8475 cp_declarator *declarator;
8477 /* Parse another optional declarator. */
8478 declarator = cp_parser_conversion_declarator_opt (parser);
8480 return cp_parser_make_indirect_declarator
8481 (code, class_type, cv_quals, declarator);
8487 /* Parse an (optional) ctor-initializer.
8490 : mem-initializer-list
8492 Returns TRUE iff the ctor-initializer was actually present. */
8495 cp_parser_ctor_initializer_opt (cp_parser* parser)
8497 /* If the next token is not a `:', then there is no
8498 ctor-initializer. */
8499 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8501 /* Do default initialization of any bases and members. */
8502 if (DECL_CONSTRUCTOR_P (current_function_decl))
8503 finish_mem_initializers (NULL_TREE);
8508 /* Consume the `:' token. */
8509 cp_lexer_consume_token (parser->lexer);
8510 /* And the mem-initializer-list. */
8511 cp_parser_mem_initializer_list (parser);
8516 /* Parse a mem-initializer-list.
8518 mem-initializer-list:
8519 mem-initializer ... [opt]
8520 mem-initializer ... [opt] , mem-initializer-list */
8523 cp_parser_mem_initializer_list (cp_parser* parser)
8525 tree mem_initializer_list = NULL_TREE;
8527 /* Let the semantic analysis code know that we are starting the
8528 mem-initializer-list. */
8529 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8530 error ("only constructors take base initializers");
8532 /* Loop through the list. */
8535 tree mem_initializer;
8537 /* Parse the mem-initializer. */
8538 mem_initializer = cp_parser_mem_initializer (parser);
8539 /* If the next token is a `...', we're expanding member initializers. */
8540 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8542 /* Consume the `...'. */
8543 cp_lexer_consume_token (parser->lexer);
8545 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8546 can be expanded but members cannot. */
8547 if (mem_initializer != error_mark_node
8548 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8550 error ("cannot expand initializer for member %<%D%>",
8551 TREE_PURPOSE (mem_initializer));
8552 mem_initializer = error_mark_node;
8555 /* Construct the pack expansion type. */
8556 if (mem_initializer != error_mark_node)
8557 mem_initializer = make_pack_expansion (mem_initializer);
8559 /* Add it to the list, unless it was erroneous. */
8560 if (mem_initializer != error_mark_node)
8562 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8563 mem_initializer_list = mem_initializer;
8565 /* If the next token is not a `,', we're done. */
8566 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8568 /* Consume the `,' token. */
8569 cp_lexer_consume_token (parser->lexer);
8572 /* Perform semantic analysis. */
8573 if (DECL_CONSTRUCTOR_P (current_function_decl))
8574 finish_mem_initializers (mem_initializer_list);
8577 /* Parse a mem-initializer.
8580 mem-initializer-id ( expression-list [opt] )
8585 ( expression-list [opt] )
8587 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8588 class) or FIELD_DECL (for a non-static data member) to initialize;
8589 the TREE_VALUE is the expression-list. An empty initialization
8590 list is represented by void_list_node. */
8593 cp_parser_mem_initializer (cp_parser* parser)
8595 tree mem_initializer_id;
8596 tree expression_list;
8599 /* Find out what is being initialized. */
8600 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8602 pedwarn ("anachronistic old-style base class initializer");
8603 mem_initializer_id = NULL_TREE;
8606 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8607 member = expand_member_init (mem_initializer_id);
8608 if (member && !DECL_P (member))
8609 in_base_initializer = 1;
8612 = cp_parser_parenthesized_expression_list (parser, false,
8614 /*allow_expansion_p=*/true,
8615 /*non_constant_p=*/NULL);
8616 if (expression_list == error_mark_node)
8617 return error_mark_node;
8618 if (!expression_list)
8619 expression_list = void_type_node;
8621 in_base_initializer = 0;
8623 return member ? build_tree_list (member, expression_list) : error_mark_node;
8626 /* Parse a mem-initializer-id.
8629 :: [opt] nested-name-specifier [opt] class-name
8632 Returns a TYPE indicating the class to be initializer for the first
8633 production. Returns an IDENTIFIER_NODE indicating the data member
8634 to be initialized for the second production. */
8637 cp_parser_mem_initializer_id (cp_parser* parser)
8639 bool global_scope_p;
8640 bool nested_name_specifier_p;
8641 bool template_p = false;
8644 /* `typename' is not allowed in this context ([temp.res]). */
8645 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8647 error ("keyword %<typename%> not allowed in this context (a qualified "
8648 "member initializer is implicitly a type)");
8649 cp_lexer_consume_token (parser->lexer);
8651 /* Look for the optional `::' operator. */
8653 = (cp_parser_global_scope_opt (parser,
8654 /*current_scope_valid_p=*/false)
8656 /* Look for the optional nested-name-specifier. The simplest way to
8661 The keyword `typename' is not permitted in a base-specifier or
8662 mem-initializer; in these contexts a qualified name that
8663 depends on a template-parameter is implicitly assumed to be a
8666 is to assume that we have seen the `typename' keyword at this
8668 nested_name_specifier_p
8669 = (cp_parser_nested_name_specifier_opt (parser,
8670 /*typename_keyword_p=*/true,
8671 /*check_dependency_p=*/true,
8673 /*is_declaration=*/true)
8675 if (nested_name_specifier_p)
8676 template_p = cp_parser_optional_template_keyword (parser);
8677 /* If there is a `::' operator or a nested-name-specifier, then we
8678 are definitely looking for a class-name. */
8679 if (global_scope_p || nested_name_specifier_p)
8680 return cp_parser_class_name (parser,
8681 /*typename_keyword_p=*/true,
8682 /*template_keyword_p=*/template_p,
8684 /*check_dependency_p=*/true,
8685 /*class_head_p=*/false,
8686 /*is_declaration=*/true);
8687 /* Otherwise, we could also be looking for an ordinary identifier. */
8688 cp_parser_parse_tentatively (parser);
8689 /* Try a class-name. */
8690 id = cp_parser_class_name (parser,
8691 /*typename_keyword_p=*/true,
8692 /*template_keyword_p=*/false,
8694 /*check_dependency_p=*/true,
8695 /*class_head_p=*/false,
8696 /*is_declaration=*/true);
8697 /* If we found one, we're done. */
8698 if (cp_parser_parse_definitely (parser))
8700 /* Otherwise, look for an ordinary identifier. */
8701 return cp_parser_identifier (parser);
8704 /* Overloading [gram.over] */
8706 /* Parse an operator-function-id.
8708 operator-function-id:
8711 Returns an IDENTIFIER_NODE for the operator which is a
8712 human-readable spelling of the identifier, e.g., `operator +'. */
8715 cp_parser_operator_function_id (cp_parser* parser)
8717 /* Look for the `operator' keyword. */
8718 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8719 return error_mark_node;
8720 /* And then the name of the operator itself. */
8721 return cp_parser_operator (parser);
8724 /* Parse an operator.
8727 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8728 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8729 || ++ -- , ->* -> () []
8736 Returns an IDENTIFIER_NODE for the operator which is a
8737 human-readable spelling of the identifier, e.g., `operator +'. */
8740 cp_parser_operator (cp_parser* parser)
8742 tree id = NULL_TREE;
8745 /* Peek at the next token. */
8746 token = cp_lexer_peek_token (parser->lexer);
8747 /* Figure out which operator we have. */
8748 switch (token->type)
8754 /* The keyword should be either `new' or `delete'. */
8755 if (token->keyword == RID_NEW)
8757 else if (token->keyword == RID_DELETE)
8762 /* Consume the `new' or `delete' token. */
8763 cp_lexer_consume_token (parser->lexer);
8765 /* Peek at the next token. */
8766 token = cp_lexer_peek_token (parser->lexer);
8767 /* If it's a `[' token then this is the array variant of the
8769 if (token->type == CPP_OPEN_SQUARE)
8771 /* Consume the `[' token. */
8772 cp_lexer_consume_token (parser->lexer);
8773 /* Look for the `]' token. */
8774 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8775 id = ansi_opname (op == NEW_EXPR
8776 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8778 /* Otherwise, we have the non-array variant. */
8780 id = ansi_opname (op);
8786 id = ansi_opname (PLUS_EXPR);
8790 id = ansi_opname (MINUS_EXPR);
8794 id = ansi_opname (MULT_EXPR);
8798 id = ansi_opname (TRUNC_DIV_EXPR);
8802 id = ansi_opname (TRUNC_MOD_EXPR);
8806 id = ansi_opname (BIT_XOR_EXPR);
8810 id = ansi_opname (BIT_AND_EXPR);
8814 id = ansi_opname (BIT_IOR_EXPR);
8818 id = ansi_opname (BIT_NOT_EXPR);
8822 id = ansi_opname (TRUTH_NOT_EXPR);
8826 id = ansi_assopname (NOP_EXPR);
8830 id = ansi_opname (LT_EXPR);
8834 id = ansi_opname (GT_EXPR);
8838 id = ansi_assopname (PLUS_EXPR);
8842 id = ansi_assopname (MINUS_EXPR);
8846 id = ansi_assopname (MULT_EXPR);
8850 id = ansi_assopname (TRUNC_DIV_EXPR);
8854 id = ansi_assopname (TRUNC_MOD_EXPR);
8858 id = ansi_assopname (BIT_XOR_EXPR);
8862 id = ansi_assopname (BIT_AND_EXPR);
8866 id = ansi_assopname (BIT_IOR_EXPR);
8870 id = ansi_opname (LSHIFT_EXPR);
8874 id = ansi_opname (RSHIFT_EXPR);
8878 id = ansi_assopname (LSHIFT_EXPR);
8882 id = ansi_assopname (RSHIFT_EXPR);
8886 id = ansi_opname (EQ_EXPR);
8890 id = ansi_opname (NE_EXPR);
8894 id = ansi_opname (LE_EXPR);
8897 case CPP_GREATER_EQ:
8898 id = ansi_opname (GE_EXPR);
8902 id = ansi_opname (TRUTH_ANDIF_EXPR);
8906 id = ansi_opname (TRUTH_ORIF_EXPR);
8910 id = ansi_opname (POSTINCREMENT_EXPR);
8913 case CPP_MINUS_MINUS:
8914 id = ansi_opname (PREDECREMENT_EXPR);
8918 id = ansi_opname (COMPOUND_EXPR);
8921 case CPP_DEREF_STAR:
8922 id = ansi_opname (MEMBER_REF);
8926 id = ansi_opname (COMPONENT_REF);
8929 case CPP_OPEN_PAREN:
8930 /* Consume the `('. */
8931 cp_lexer_consume_token (parser->lexer);
8932 /* Look for the matching `)'. */
8933 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8934 return ansi_opname (CALL_EXPR);
8936 case CPP_OPEN_SQUARE:
8937 /* Consume the `['. */
8938 cp_lexer_consume_token (parser->lexer);
8939 /* Look for the matching `]'. */
8940 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8941 return ansi_opname (ARRAY_REF);
8944 /* Anything else is an error. */
8948 /* If we have selected an identifier, we need to consume the
8951 cp_lexer_consume_token (parser->lexer);
8952 /* Otherwise, no valid operator name was present. */
8955 cp_parser_error (parser, "expected operator");
8956 id = error_mark_node;
8962 /* Parse a template-declaration.
8964 template-declaration:
8965 export [opt] template < template-parameter-list > declaration
8967 If MEMBER_P is TRUE, this template-declaration occurs within a
8970 The grammar rule given by the standard isn't correct. What
8973 template-declaration:
8974 export [opt] template-parameter-list-seq
8975 decl-specifier-seq [opt] init-declarator [opt] ;
8976 export [opt] template-parameter-list-seq
8979 template-parameter-list-seq:
8980 template-parameter-list-seq [opt]
8981 template < template-parameter-list > */
8984 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8986 /* Check for `export'. */
8987 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8989 /* Consume the `export' token. */
8990 cp_lexer_consume_token (parser->lexer);
8991 /* Warn that we do not support `export'. */
8992 warning (0, "keyword %<export%> not implemented, and will be ignored");
8995 cp_parser_template_declaration_after_export (parser, member_p);
8998 /* Parse a template-parameter-list.
9000 template-parameter-list:
9002 template-parameter-list , template-parameter
9004 Returns a TREE_LIST. Each node represents a template parameter.
9005 The nodes are connected via their TREE_CHAINs. */
9008 cp_parser_template_parameter_list (cp_parser* parser)
9010 tree parameter_list = NULL_TREE;
9012 begin_template_parm_list ();
9018 bool is_parameter_pack;
9020 /* Parse the template-parameter. */
9021 parameter = cp_parser_template_parameter (parser,
9023 &is_parameter_pack);
9024 /* Add it to the list. */
9025 if (parameter != error_mark_node)
9026 parameter_list = process_template_parm (parameter_list,
9032 tree err_parm = build_tree_list (parameter, parameter);
9033 TREE_VALUE (err_parm) = error_mark_node;
9034 parameter_list = chainon (parameter_list, err_parm);
9037 /* Peek at the next token. */
9038 token = cp_lexer_peek_token (parser->lexer);
9039 /* If it's not a `,', we're done. */
9040 if (token->type != CPP_COMMA)
9042 /* Otherwise, consume the `,' token. */
9043 cp_lexer_consume_token (parser->lexer);
9046 return end_template_parm_list (parameter_list);
9049 /* Parse a template-parameter.
9053 parameter-declaration
9055 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9056 the parameter. The TREE_PURPOSE is the default value, if any.
9057 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9058 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9059 set to true iff this parameter is a parameter pack. */
9062 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9063 bool *is_parameter_pack)
9066 cp_parameter_declarator *parameter_declarator;
9069 /* Assume it is a type parameter or a template parameter. */
9070 *is_non_type = false;
9071 /* Assume it not a parameter pack. */
9072 *is_parameter_pack = false;
9073 /* Peek at the next token. */
9074 token = cp_lexer_peek_token (parser->lexer);
9075 /* If it is `class' or `template', we have a type-parameter. */
9076 if (token->keyword == RID_TEMPLATE)
9077 return cp_parser_type_parameter (parser, is_parameter_pack);
9078 /* If it is `class' or `typename' we do not know yet whether it is a
9079 type parameter or a non-type parameter. Consider:
9081 template <typename T, typename T::X X> ...
9085 template <class C, class D*> ...
9087 Here, the first parameter is a type parameter, and the second is
9088 a non-type parameter. We can tell by looking at the token after
9089 the identifier -- if it is a `,', `=', or `>' then we have a type
9091 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9093 /* Peek at the token after `class' or `typename'. */
9094 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9095 /* If it's an ellipsis, we have a template type parameter
9097 if (token->type == CPP_ELLIPSIS)
9098 return cp_parser_type_parameter (parser, is_parameter_pack);
9099 /* If it's an identifier, skip it. */
9100 if (token->type == CPP_NAME)
9101 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9102 /* Now, see if the token looks like the end of a template
9104 if (token->type == CPP_COMMA
9105 || token->type == CPP_EQ
9106 || token->type == CPP_GREATER)
9107 return cp_parser_type_parameter (parser, is_parameter_pack);
9110 /* Otherwise, it is a non-type parameter.
9114 When parsing a default template-argument for a non-type
9115 template-parameter, the first non-nested `>' is taken as the end
9116 of the template parameter-list rather than a greater-than
9118 *is_non_type = true;
9119 parameter_declarator
9120 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9121 /*parenthesized_p=*/NULL);
9123 /* If the parameter declaration is marked as a parameter pack, set
9124 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9125 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9127 if (parameter_declarator
9128 && parameter_declarator->declarator
9129 && parameter_declarator->declarator->parameter_pack_p)
9131 *is_parameter_pack = true;
9132 parameter_declarator->declarator->parameter_pack_p = false;
9135 /* If the next token is an ellipsis, and we don't already have it
9136 marked as a parameter pack, then we have a parameter pack (that
9137 has no declarator); */
9138 if (!*is_parameter_pack
9139 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9140 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9142 /* Consume the `...'. */
9143 cp_lexer_consume_token (parser->lexer);
9144 maybe_warn_variadic_templates ();
9146 *is_parameter_pack = true;
9149 parm = grokdeclarator (parameter_declarator->declarator,
9150 ¶meter_declarator->decl_specifiers,
9151 PARM, /*initialized=*/0,
9153 if (parm == error_mark_node)
9154 return error_mark_node;
9156 return build_tree_list (parameter_declarator->default_argument, parm);
9159 /* Parse a type-parameter.
9162 class identifier [opt]
9163 class identifier [opt] = type-id
9164 typename identifier [opt]
9165 typename identifier [opt] = type-id
9166 template < template-parameter-list > class identifier [opt]
9167 template < template-parameter-list > class identifier [opt]
9170 GNU Extension (variadic templates):
9173 class ... identifier [opt]
9174 typename ... identifier [opt]
9176 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9177 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9178 the declaration of the parameter.
9180 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9183 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9188 /* Look for a keyword to tell us what kind of parameter this is. */
9189 token = cp_parser_require (parser, CPP_KEYWORD,
9190 "`class', `typename', or `template'");
9192 return error_mark_node;
9194 switch (token->keyword)
9200 tree default_argument;
9202 /* If the next token is an ellipsis, we have a template
9204 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9206 /* Consume the `...' token. */
9207 cp_lexer_consume_token (parser->lexer);
9208 maybe_warn_variadic_templates ();
9210 *is_parameter_pack = true;
9213 /* If the next token is an identifier, then it names the
9215 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9216 identifier = cp_parser_identifier (parser);
9218 identifier = NULL_TREE;
9220 /* Create the parameter. */
9221 parameter = finish_template_type_parm (class_type_node, identifier);
9223 /* If the next token is an `=', we have a default argument. */
9224 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9226 /* Consume the `=' token. */
9227 cp_lexer_consume_token (parser->lexer);
9228 /* Parse the default-argument. */
9229 push_deferring_access_checks (dk_no_deferred);
9230 default_argument = cp_parser_type_id (parser);
9232 /* Template parameter packs cannot have default
9234 if (*is_parameter_pack)
9237 error ("template parameter pack %qD cannot have a default argument",
9240 error ("template parameter packs cannot have default arguments");
9241 default_argument = NULL_TREE;
9243 pop_deferring_access_checks ();
9246 default_argument = NULL_TREE;
9248 /* Create the combined representation of the parameter and the
9249 default argument. */
9250 parameter = build_tree_list (default_argument, parameter);
9256 tree parameter_list;
9258 tree default_argument;
9260 /* Look for the `<'. */
9261 cp_parser_require (parser, CPP_LESS, "`<'");
9262 /* Parse the template-parameter-list. */
9263 parameter_list = cp_parser_template_parameter_list (parser);
9264 /* Look for the `>'. */
9265 cp_parser_require (parser, CPP_GREATER, "`>'");
9266 /* Look for the `class' keyword. */
9267 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9268 /* If the next token is an ellipsis, we have a template
9270 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9272 /* Consume the `...' token. */
9273 cp_lexer_consume_token (parser->lexer);
9274 maybe_warn_variadic_templates ();
9276 *is_parameter_pack = true;
9278 /* If the next token is an `=', then there is a
9279 default-argument. If the next token is a `>', we are at
9280 the end of the parameter-list. If the next token is a `,',
9281 then we are at the end of this parameter. */
9282 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9283 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9284 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9286 identifier = cp_parser_identifier (parser);
9287 /* Treat invalid names as if the parameter were nameless. */
9288 if (identifier == error_mark_node)
9289 identifier = NULL_TREE;
9292 identifier = NULL_TREE;
9294 /* Create the template parameter. */
9295 parameter = finish_template_template_parm (class_type_node,
9298 /* If the next token is an `=', then there is a
9299 default-argument. */
9300 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9304 /* Consume the `='. */
9305 cp_lexer_consume_token (parser->lexer);
9306 /* Parse the id-expression. */
9307 push_deferring_access_checks (dk_no_deferred);
9309 = cp_parser_id_expression (parser,
9310 /*template_keyword_p=*/false,
9311 /*check_dependency_p=*/true,
9312 /*template_p=*/&is_template,
9313 /*declarator_p=*/false,
9314 /*optional_p=*/false);
9315 if (TREE_CODE (default_argument) == TYPE_DECL)
9316 /* If the id-expression was a template-id that refers to
9317 a template-class, we already have the declaration here,
9318 so no further lookup is needed. */
9321 /* Look up the name. */
9323 = cp_parser_lookup_name (parser, default_argument,
9325 /*is_template=*/is_template,
9326 /*is_namespace=*/false,
9327 /*check_dependency=*/true,
9328 /*ambiguous_decls=*/NULL);
9329 /* See if the default argument is valid. */
9331 = check_template_template_default_arg (default_argument);
9333 /* Template parameter packs cannot have default
9335 if (*is_parameter_pack)
9338 error ("template parameter pack %qD cannot have a default argument",
9341 error ("template parameter packs cannot have default arguments");
9342 default_argument = NULL_TREE;
9344 pop_deferring_access_checks ();
9347 default_argument = NULL_TREE;
9349 /* Create the combined representation of the parameter and the
9350 default argument. */
9351 parameter = build_tree_list (default_argument, parameter);
9363 /* Parse a template-id.
9366 template-name < template-argument-list [opt] >
9368 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9369 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9370 returned. Otherwise, if the template-name names a function, or set
9371 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9372 names a class, returns a TYPE_DECL for the specialization.
9374 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9375 uninstantiated templates. */
9378 cp_parser_template_id (cp_parser *parser,
9379 bool template_keyword_p,
9380 bool check_dependency_p,
9381 bool is_declaration)
9387 cp_token_position start_of_id = 0;
9388 deferred_access_check *chk;
9389 VEC (deferred_access_check,gc) *access_check;
9390 cp_token *next_token, *next_token_2;
9393 /* If the next token corresponds to a template-id, there is no need
9395 next_token = cp_lexer_peek_token (parser->lexer);
9396 if (next_token->type == CPP_TEMPLATE_ID)
9398 struct tree_check *check_value;
9400 /* Get the stored value. */
9401 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9402 /* Perform any access checks that were deferred. */
9403 access_check = check_value->checks;
9407 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9410 perform_or_defer_access_check (chk->binfo,
9415 /* Return the stored value. */
9416 return check_value->value;
9419 /* Avoid performing name lookup if there is no possibility of
9420 finding a template-id. */
9421 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9422 || (next_token->type == CPP_NAME
9423 && !cp_parser_nth_token_starts_template_argument_list_p
9426 cp_parser_error (parser, "expected template-id");
9427 return error_mark_node;
9430 /* Remember where the template-id starts. */
9431 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9432 start_of_id = cp_lexer_token_position (parser->lexer, false);
9434 push_deferring_access_checks (dk_deferred);
9436 /* Parse the template-name. */
9437 is_identifier = false;
9438 template = cp_parser_template_name (parser, template_keyword_p,
9442 if (template == error_mark_node || is_identifier)
9444 pop_deferring_access_checks ();
9448 /* If we find the sequence `[:' after a template-name, it's probably
9449 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9450 parse correctly the argument list. */
9451 next_token = cp_lexer_peek_token (parser->lexer);
9452 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9453 if (next_token->type == CPP_OPEN_SQUARE
9454 && next_token->flags & DIGRAPH
9455 && next_token_2->type == CPP_COLON
9456 && !(next_token_2->flags & PREV_WHITE))
9458 cp_parser_parse_tentatively (parser);
9459 /* Change `:' into `::'. */
9460 next_token_2->type = CPP_SCOPE;
9461 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9463 cp_lexer_consume_token (parser->lexer);
9464 /* Parse the arguments. */
9465 arguments = cp_parser_enclosed_template_argument_list (parser);
9466 if (!cp_parser_parse_definitely (parser))
9468 /* If we couldn't parse an argument list, then we revert our changes
9469 and return simply an error. Maybe this is not a template-id
9471 next_token_2->type = CPP_COLON;
9472 cp_parser_error (parser, "expected %<<%>");
9473 pop_deferring_access_checks ();
9474 return error_mark_node;
9476 /* Otherwise, emit an error about the invalid digraph, but continue
9477 parsing because we got our argument list. */
9478 pedwarn ("%<<::%> cannot begin a template-argument list");
9479 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9480 "between %<<%> and %<::%>");
9481 if (!flag_permissive)
9486 inform ("(if you use -fpermissive G++ will accept your code)");
9493 /* Look for the `<' that starts the template-argument-list. */
9494 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9496 pop_deferring_access_checks ();
9497 return error_mark_node;
9499 /* Parse the arguments. */
9500 arguments = cp_parser_enclosed_template_argument_list (parser);
9503 /* Build a representation of the specialization. */
9504 if (TREE_CODE (template) == IDENTIFIER_NODE)
9505 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9506 else if (DECL_CLASS_TEMPLATE_P (template)
9507 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9509 bool entering_scope;
9510 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9511 template (rather than some instantiation thereof) only if
9512 is not nested within some other construct. For example, in
9513 "template <typename T> void f(T) { A<T>::", A<T> is just an
9514 instantiation of A. */
9515 entering_scope = (template_parm_scope_p ()
9516 && cp_lexer_next_token_is (parser->lexer,
9519 = finish_template_type (template, arguments, entering_scope);
9523 /* If it's not a class-template or a template-template, it should be
9524 a function-template. */
9525 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9526 || TREE_CODE (template) == OVERLOAD
9527 || BASELINK_P (template)));
9529 template_id = lookup_template_function (template, arguments);
9532 /* If parsing tentatively, replace the sequence of tokens that makes
9533 up the template-id with a CPP_TEMPLATE_ID token. That way,
9534 should we re-parse the token stream, we will not have to repeat
9535 the effort required to do the parse, nor will we issue duplicate
9536 error messages about problems during instantiation of the
9540 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9542 /* Reset the contents of the START_OF_ID token. */
9543 token->type = CPP_TEMPLATE_ID;
9544 /* Retrieve any deferred checks. Do not pop this access checks yet
9545 so the memory will not be reclaimed during token replacing below. */
9546 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9547 token->u.tree_check_value->value = template_id;
9548 token->u.tree_check_value->checks = get_deferred_access_checks ();
9549 token->keyword = RID_MAX;
9551 /* Purge all subsequent tokens. */
9552 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9554 /* ??? Can we actually assume that, if template_id ==
9555 error_mark_node, we will have issued a diagnostic to the
9556 user, as opposed to simply marking the tentative parse as
9558 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9559 error ("parse error in template argument list");
9562 pop_deferring_access_checks ();
9566 /* Parse a template-name.
9571 The standard should actually say:
9575 operator-function-id
9577 A defect report has been filed about this issue.
9579 A conversion-function-id cannot be a template name because they cannot
9580 be part of a template-id. In fact, looking at this code:
9584 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9585 It is impossible to call a templated conversion-function-id with an
9586 explicit argument list, since the only allowed template parameter is
9587 the type to which it is converting.
9589 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9590 `template' keyword, in a construction like:
9594 In that case `f' is taken to be a template-name, even though there
9595 is no way of knowing for sure.
9597 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9598 name refers to a set of overloaded functions, at least one of which
9599 is a template, or an IDENTIFIER_NODE with the name of the template,
9600 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9601 names are looked up inside uninstantiated templates. */
9604 cp_parser_template_name (cp_parser* parser,
9605 bool template_keyword_p,
9606 bool check_dependency_p,
9607 bool is_declaration,
9608 bool *is_identifier)
9614 /* If the next token is `operator', then we have either an
9615 operator-function-id or a conversion-function-id. */
9616 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9618 /* We don't know whether we're looking at an
9619 operator-function-id or a conversion-function-id. */
9620 cp_parser_parse_tentatively (parser);
9621 /* Try an operator-function-id. */
9622 identifier = cp_parser_operator_function_id (parser);
9623 /* If that didn't work, try a conversion-function-id. */
9624 if (!cp_parser_parse_definitely (parser))
9626 cp_parser_error (parser, "expected template-name");
9627 return error_mark_node;
9630 /* Look for the identifier. */
9632 identifier = cp_parser_identifier (parser);
9634 /* If we didn't find an identifier, we don't have a template-id. */
9635 if (identifier == error_mark_node)
9636 return error_mark_node;
9638 /* If the name immediately followed the `template' keyword, then it
9639 is a template-name. However, if the next token is not `<', then
9640 we do not treat it as a template-name, since it is not being used
9641 as part of a template-id. This enables us to handle constructs
9644 template <typename T> struct S { S(); };
9645 template <typename T> S<T>::S();
9647 correctly. We would treat `S' as a template -- if it were `S<T>'
9648 -- but we do not if there is no `<'. */
9650 if (processing_template_decl
9651 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9653 /* In a declaration, in a dependent context, we pretend that the
9654 "template" keyword was present in order to improve error
9655 recovery. For example, given:
9657 template <typename T> void f(T::X<int>);
9659 we want to treat "X<int>" as a template-id. */
9661 && !template_keyword_p
9662 && parser->scope && TYPE_P (parser->scope)
9663 && check_dependency_p
9664 && dependent_type_p (parser->scope)
9665 /* Do not do this for dtors (or ctors), since they never
9666 need the template keyword before their name. */
9667 && !constructor_name_p (identifier, parser->scope))
9669 cp_token_position start = 0;
9671 /* Explain what went wrong. */
9672 error ("non-template %qD used as template", identifier);
9673 inform ("use %<%T::template %D%> to indicate that it is a template",
9674 parser->scope, identifier);
9675 /* If parsing tentatively, find the location of the "<" token. */
9676 if (cp_parser_simulate_error (parser))
9677 start = cp_lexer_token_position (parser->lexer, true);
9678 /* Parse the template arguments so that we can issue error
9679 messages about them. */
9680 cp_lexer_consume_token (parser->lexer);
9681 cp_parser_enclosed_template_argument_list (parser);
9682 /* Skip tokens until we find a good place from which to
9683 continue parsing. */
9684 cp_parser_skip_to_closing_parenthesis (parser,
9685 /*recovering=*/true,
9687 /*consume_paren=*/false);
9688 /* If parsing tentatively, permanently remove the
9689 template argument list. That will prevent duplicate
9690 error messages from being issued about the missing
9691 "template" keyword. */
9693 cp_lexer_purge_tokens_after (parser->lexer, start);
9695 *is_identifier = true;
9699 /* If the "template" keyword is present, then there is generally
9700 no point in doing name-lookup, so we just return IDENTIFIER.
9701 But, if the qualifying scope is non-dependent then we can
9702 (and must) do name-lookup normally. */
9703 if (template_keyword_p
9705 || (TYPE_P (parser->scope)
9706 && dependent_type_p (parser->scope))))
9710 /* Look up the name. */
9711 decl = cp_parser_lookup_name (parser, identifier,
9713 /*is_template=*/false,
9714 /*is_namespace=*/false,
9716 /*ambiguous_decls=*/NULL);
9717 decl = maybe_get_template_decl_from_type_decl (decl);
9719 /* If DECL is a template, then the name was a template-name. */
9720 if (TREE_CODE (decl) == TEMPLATE_DECL)
9724 tree fn = NULL_TREE;
9726 /* The standard does not explicitly indicate whether a name that
9727 names a set of overloaded declarations, some of which are
9728 templates, is a template-name. However, such a name should
9729 be a template-name; otherwise, there is no way to form a
9730 template-id for the overloaded templates. */
9731 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9732 if (TREE_CODE (fns) == OVERLOAD)
9733 for (fn = fns; fn; fn = OVL_NEXT (fn))
9734 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9739 /* The name does not name a template. */
9740 cp_parser_error (parser, "expected template-name");
9741 return error_mark_node;
9745 /* If DECL is dependent, and refers to a function, then just return
9746 its name; we will look it up again during template instantiation. */
9747 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9749 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9750 if (TYPE_P (scope) && dependent_type_p (scope))
9757 /* Parse a template-argument-list.
9759 template-argument-list:
9760 template-argument ... [opt]
9761 template-argument-list , template-argument ... [opt]
9763 Returns a TREE_VEC containing the arguments. */
9766 cp_parser_template_argument_list (cp_parser* parser)
9768 tree fixed_args[10];
9769 unsigned n_args = 0;
9770 unsigned alloced = 10;
9771 tree *arg_ary = fixed_args;
9773 bool saved_in_template_argument_list_p;
9775 bool saved_non_ice_p;
9777 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9778 parser->in_template_argument_list_p = true;
9779 /* Even if the template-id appears in an integral
9780 constant-expression, the contents of the argument list do
9782 saved_ice_p = parser->integral_constant_expression_p;
9783 parser->integral_constant_expression_p = false;
9784 saved_non_ice_p = parser->non_integral_constant_expression_p;
9785 parser->non_integral_constant_expression_p = false;
9786 /* Parse the arguments. */
9792 /* Consume the comma. */
9793 cp_lexer_consume_token (parser->lexer);
9795 /* Parse the template-argument. */
9796 argument = cp_parser_template_argument (parser);
9798 /* If the next token is an ellipsis, we're expanding a template
9800 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9802 /* Consume the `...' token. */
9803 cp_lexer_consume_token (parser->lexer);
9805 /* Make the argument into a TYPE_PACK_EXPANSION or
9806 EXPR_PACK_EXPANSION. */
9807 argument = make_pack_expansion (argument);
9810 if (n_args == alloced)
9814 if (arg_ary == fixed_args)
9816 arg_ary = XNEWVEC (tree, alloced);
9817 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
9820 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
9822 arg_ary[n_args++] = argument;
9824 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
9826 vec = make_tree_vec (n_args);
9829 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
9831 if (arg_ary != fixed_args)
9833 parser->non_integral_constant_expression_p = saved_non_ice_p;
9834 parser->integral_constant_expression_p = saved_ice_p;
9835 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
9839 /* Parse a template-argument.
9842 assignment-expression
9846 The representation is that of an assignment-expression, type-id, or
9847 id-expression -- except that the qualified id-expression is
9848 evaluated, so that the value returned is either a DECL or an
9851 Although the standard says "assignment-expression", it forbids
9852 throw-expressions or assignments in the template argument.
9853 Therefore, we use "conditional-expression" instead. */
9856 cp_parser_template_argument (cp_parser* parser)
9861 bool maybe_type_id = false;
9865 /* There's really no way to know what we're looking at, so we just
9866 try each alternative in order.
9870 In a template-argument, an ambiguity between a type-id and an
9871 expression is resolved to a type-id, regardless of the form of
9872 the corresponding template-parameter.
9874 Therefore, we try a type-id first. */
9875 cp_parser_parse_tentatively (parser);
9876 argument = cp_parser_type_id (parser);
9877 /* If there was no error parsing the type-id but the next token is a '>>',
9878 we probably found a typo for '> >'. But there are type-id which are
9879 also valid expressions. For instance:
9881 struct X { int operator >> (int); };
9882 template <int V> struct Foo {};
9885 Here 'X()' is a valid type-id of a function type, but the user just
9886 wanted to write the expression "X() >> 5". Thus, we remember that we
9887 found a valid type-id, but we still try to parse the argument as an
9888 expression to see what happens. */
9889 if (!cp_parser_error_occurred (parser)
9890 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
9892 maybe_type_id = true;
9893 cp_parser_abort_tentative_parse (parser);
9897 /* If the next token isn't a `,' or a `>', then this argument wasn't
9898 really finished. This means that the argument is not a valid
9900 if (!cp_parser_next_token_ends_template_argument_p (parser))
9901 cp_parser_error (parser, "expected template-argument");
9902 /* If that worked, we're done. */
9903 if (cp_parser_parse_definitely (parser))
9906 /* We're still not sure what the argument will be. */
9907 cp_parser_parse_tentatively (parser);
9908 /* Try a template. */
9909 argument = cp_parser_id_expression (parser,
9910 /*template_keyword_p=*/false,
9911 /*check_dependency_p=*/true,
9913 /*declarator_p=*/false,
9914 /*optional_p=*/false);
9915 /* If the next token isn't a `,' or a `>', then this argument wasn't
9917 if (!cp_parser_next_token_ends_template_argument_p (parser))
9918 cp_parser_error (parser, "expected template-argument");
9919 if (!cp_parser_error_occurred (parser))
9921 /* Figure out what is being referred to. If the id-expression
9922 was for a class template specialization, then we will have a
9923 TYPE_DECL at this point. There is no need to do name lookup
9924 at this point in that case. */
9925 if (TREE_CODE (argument) != TYPE_DECL)
9926 argument = cp_parser_lookup_name (parser, argument,
9928 /*is_template=*/template_p,
9929 /*is_namespace=*/false,
9930 /*check_dependency=*/true,
9931 /*ambiguous_decls=*/NULL);
9932 if (TREE_CODE (argument) != TEMPLATE_DECL
9933 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9934 cp_parser_error (parser, "expected template-name");
9936 if (cp_parser_parse_definitely (parser))
9938 /* It must be a non-type argument. There permitted cases are given
9939 in [temp.arg.nontype]:
9941 -- an integral constant-expression of integral or enumeration
9944 -- the name of a non-type template-parameter; or
9946 -- the name of an object or function with external linkage...
9948 -- the address of an object or function with external linkage...
9950 -- a pointer to member... */
9951 /* Look for a non-type template parameter. */
9952 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9954 cp_parser_parse_tentatively (parser);
9955 argument = cp_parser_primary_expression (parser,
9958 /*template_arg_p=*/true,
9960 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9961 || !cp_parser_next_token_ends_template_argument_p (parser))
9962 cp_parser_simulate_error (parser);
9963 if (cp_parser_parse_definitely (parser))
9967 /* If the next token is "&", the argument must be the address of an
9968 object or function with external linkage. */
9969 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9971 cp_lexer_consume_token (parser->lexer);
9972 /* See if we might have an id-expression. */
9973 token = cp_lexer_peek_token (parser->lexer);
9974 if (token->type == CPP_NAME
9975 || token->keyword == RID_OPERATOR
9976 || token->type == CPP_SCOPE
9977 || token->type == CPP_TEMPLATE_ID
9978 || token->type == CPP_NESTED_NAME_SPECIFIER)
9980 cp_parser_parse_tentatively (parser);
9981 argument = cp_parser_primary_expression (parser,
9984 /*template_arg_p=*/true,
9986 if (cp_parser_error_occurred (parser)
9987 || !cp_parser_next_token_ends_template_argument_p (parser))
9988 cp_parser_abort_tentative_parse (parser);
9991 if (TREE_CODE (argument) == INDIRECT_REF)
9993 gcc_assert (REFERENCE_REF_P (argument));
9994 argument = TREE_OPERAND (argument, 0);
9997 if (TREE_CODE (argument) == VAR_DECL)
9999 /* A variable without external linkage might still be a
10000 valid constant-expression, so no error is issued here
10001 if the external-linkage check fails. */
10002 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10003 cp_parser_simulate_error (parser);
10005 else if (is_overloaded_fn (argument))
10006 /* All overloaded functions are allowed; if the external
10007 linkage test does not pass, an error will be issued
10011 && (TREE_CODE (argument) == OFFSET_REF
10012 || TREE_CODE (argument) == SCOPE_REF))
10013 /* A pointer-to-member. */
10015 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10018 cp_parser_simulate_error (parser);
10020 if (cp_parser_parse_definitely (parser))
10023 argument = build_x_unary_op (ADDR_EXPR, argument);
10028 /* If the argument started with "&", there are no other valid
10029 alternatives at this point. */
10032 cp_parser_error (parser, "invalid non-type template argument");
10033 return error_mark_node;
10036 /* If the argument wasn't successfully parsed as a type-id followed
10037 by '>>', the argument can only be a constant expression now.
10038 Otherwise, we try parsing the constant-expression tentatively,
10039 because the argument could really be a type-id. */
10041 cp_parser_parse_tentatively (parser);
10042 argument = cp_parser_constant_expression (parser,
10043 /*allow_non_constant_p=*/false,
10044 /*non_constant_p=*/NULL);
10045 argument = fold_non_dependent_expr (argument);
10046 if (!maybe_type_id)
10048 if (!cp_parser_next_token_ends_template_argument_p (parser))
10049 cp_parser_error (parser, "expected template-argument");
10050 if (cp_parser_parse_definitely (parser))
10052 /* We did our best to parse the argument as a non type-id, but that
10053 was the only alternative that matched (albeit with a '>' after
10054 it). We can assume it's just a typo from the user, and a
10055 diagnostic will then be issued. */
10056 return cp_parser_type_id (parser);
10059 /* Parse an explicit-instantiation.
10061 explicit-instantiation:
10062 template declaration
10064 Although the standard says `declaration', what it really means is:
10066 explicit-instantiation:
10067 template decl-specifier-seq [opt] declarator [opt] ;
10069 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10070 supposed to be allowed. A defect report has been filed about this
10075 explicit-instantiation:
10076 storage-class-specifier template
10077 decl-specifier-seq [opt] declarator [opt] ;
10078 function-specifier template
10079 decl-specifier-seq [opt] declarator [opt] ; */
10082 cp_parser_explicit_instantiation (cp_parser* parser)
10084 int declares_class_or_enum;
10085 cp_decl_specifier_seq decl_specifiers;
10086 tree extension_specifier = NULL_TREE;
10088 /* Look for an (optional) storage-class-specifier or
10089 function-specifier. */
10090 if (cp_parser_allow_gnu_extensions_p (parser))
10092 extension_specifier
10093 = cp_parser_storage_class_specifier_opt (parser);
10094 if (!extension_specifier)
10095 extension_specifier
10096 = cp_parser_function_specifier_opt (parser,
10097 /*decl_specs=*/NULL);
10100 /* Look for the `template' keyword. */
10101 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10102 /* Let the front end know that we are processing an explicit
10104 begin_explicit_instantiation ();
10105 /* [temp.explicit] says that we are supposed to ignore access
10106 control while processing explicit instantiation directives. */
10107 push_deferring_access_checks (dk_no_check);
10108 /* Parse a decl-specifier-seq. */
10109 cp_parser_decl_specifier_seq (parser,
10110 CP_PARSER_FLAGS_OPTIONAL,
10112 &declares_class_or_enum);
10113 /* If there was exactly one decl-specifier, and it declared a class,
10114 and there's no declarator, then we have an explicit type
10116 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10120 type = check_tag_decl (&decl_specifiers);
10121 /* Turn access control back on for names used during
10122 template instantiation. */
10123 pop_deferring_access_checks ();
10125 do_type_instantiation (type, extension_specifier,
10126 /*complain=*/tf_error);
10130 cp_declarator *declarator;
10133 /* Parse the declarator. */
10135 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10136 /*ctor_dtor_or_conv_p=*/NULL,
10137 /*parenthesized_p=*/NULL,
10138 /*member_p=*/false);
10139 if (declares_class_or_enum & 2)
10140 cp_parser_check_for_definition_in_return_type (declarator,
10141 decl_specifiers.type);
10142 if (declarator != cp_error_declarator)
10144 decl = grokdeclarator (declarator, &decl_specifiers,
10145 NORMAL, 0, &decl_specifiers.attributes);
10146 /* Turn access control back on for names used during
10147 template instantiation. */
10148 pop_deferring_access_checks ();
10149 /* Do the explicit instantiation. */
10150 do_decl_instantiation (decl, extension_specifier);
10154 pop_deferring_access_checks ();
10155 /* Skip the body of the explicit instantiation. */
10156 cp_parser_skip_to_end_of_statement (parser);
10159 /* We're done with the instantiation. */
10160 end_explicit_instantiation ();
10162 cp_parser_consume_semicolon_at_end_of_statement (parser);
10165 /* Parse an explicit-specialization.
10167 explicit-specialization:
10168 template < > declaration
10170 Although the standard says `declaration', what it really means is:
10172 explicit-specialization:
10173 template <> decl-specifier [opt] init-declarator [opt] ;
10174 template <> function-definition
10175 template <> explicit-specialization
10176 template <> template-declaration */
10179 cp_parser_explicit_specialization (cp_parser* parser)
10181 bool need_lang_pop;
10182 /* Look for the `template' keyword. */
10183 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10184 /* Look for the `<'. */
10185 cp_parser_require (parser, CPP_LESS, "`<'");
10186 /* Look for the `>'. */
10187 cp_parser_require (parser, CPP_GREATER, "`>'");
10188 /* We have processed another parameter list. */
10189 ++parser->num_template_parameter_lists;
10192 A template ... explicit specialization ... shall not have C
10194 if (current_lang_name == lang_name_c)
10196 error ("template specialization with C linkage");
10197 /* Give it C++ linkage to avoid confusing other parts of the
10199 push_lang_context (lang_name_cplusplus);
10200 need_lang_pop = true;
10203 need_lang_pop = false;
10204 /* Let the front end know that we are beginning a specialization. */
10205 if (!begin_specialization ())
10207 end_specialization ();
10208 cp_parser_skip_to_end_of_block_or_statement (parser);
10212 /* If the next keyword is `template', we need to figure out whether
10213 or not we're looking a template-declaration. */
10214 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10216 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10217 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10218 cp_parser_template_declaration_after_export (parser,
10219 /*member_p=*/false);
10221 cp_parser_explicit_specialization (parser);
10224 /* Parse the dependent declaration. */
10225 cp_parser_single_declaration (parser,
10227 /*member_p=*/false,
10228 /*explicit_specialization_p=*/true,
10229 /*friend_p=*/NULL);
10230 /* We're done with the specialization. */
10231 end_specialization ();
10232 /* For the erroneous case of a template with C linkage, we pushed an
10233 implicit C++ linkage scope; exit that scope now. */
10235 pop_lang_context ();
10236 /* We're done with this parameter list. */
10237 --parser->num_template_parameter_lists;
10240 /* Parse a type-specifier.
10243 simple-type-specifier
10246 elaborated-type-specifier
10254 Returns a representation of the type-specifier. For a
10255 class-specifier, enum-specifier, or elaborated-type-specifier, a
10256 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10258 The parser flags FLAGS is used to control type-specifier parsing.
10260 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10261 in a decl-specifier-seq.
10263 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10264 class-specifier, enum-specifier, or elaborated-type-specifier, then
10265 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10266 if a type is declared; 2 if it is defined. Otherwise, it is set to
10269 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10270 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10271 is set to FALSE. */
10274 cp_parser_type_specifier (cp_parser* parser,
10275 cp_parser_flags flags,
10276 cp_decl_specifier_seq *decl_specs,
10277 bool is_declaration,
10278 int* declares_class_or_enum,
10279 bool* is_cv_qualifier)
10281 tree type_spec = NULL_TREE;
10284 cp_decl_spec ds = ds_last;
10286 /* Assume this type-specifier does not declare a new type. */
10287 if (declares_class_or_enum)
10288 *declares_class_or_enum = 0;
10289 /* And that it does not specify a cv-qualifier. */
10290 if (is_cv_qualifier)
10291 *is_cv_qualifier = false;
10292 /* Peek at the next token. */
10293 token = cp_lexer_peek_token (parser->lexer);
10295 /* If we're looking at a keyword, we can use that to guide the
10296 production we choose. */
10297 keyword = token->keyword;
10301 /* Look for the enum-specifier. */
10302 type_spec = cp_parser_enum_specifier (parser);
10303 /* If that worked, we're done. */
10306 if (declares_class_or_enum)
10307 *declares_class_or_enum = 2;
10309 cp_parser_set_decl_spec_type (decl_specs,
10311 /*user_defined_p=*/true);
10315 goto elaborated_type_specifier;
10317 /* Any of these indicate either a class-specifier, or an
10318 elaborated-type-specifier. */
10322 /* Parse tentatively so that we can back up if we don't find a
10323 class-specifier. */
10324 cp_parser_parse_tentatively (parser);
10325 /* Look for the class-specifier. */
10326 type_spec = cp_parser_class_specifier (parser);
10327 /* If that worked, we're done. */
10328 if (cp_parser_parse_definitely (parser))
10330 if (declares_class_or_enum)
10331 *declares_class_or_enum = 2;
10333 cp_parser_set_decl_spec_type (decl_specs,
10335 /*user_defined_p=*/true);
10339 /* Fall through. */
10340 elaborated_type_specifier:
10341 /* We're declaring (not defining) a class or enum. */
10342 if (declares_class_or_enum)
10343 *declares_class_or_enum = 1;
10345 /* Fall through. */
10347 /* Look for an elaborated-type-specifier. */
10349 = (cp_parser_elaborated_type_specifier
10351 decl_specs && decl_specs->specs[(int) ds_friend],
10354 cp_parser_set_decl_spec_type (decl_specs,
10356 /*user_defined_p=*/true);
10361 if (is_cv_qualifier)
10362 *is_cv_qualifier = true;
10367 if (is_cv_qualifier)
10368 *is_cv_qualifier = true;
10373 if (is_cv_qualifier)
10374 *is_cv_qualifier = true;
10378 /* The `__complex__' keyword is a GNU extension. */
10386 /* Handle simple keywords. */
10391 ++decl_specs->specs[(int)ds];
10392 decl_specs->any_specifiers_p = true;
10394 return cp_lexer_consume_token (parser->lexer)->u.value;
10397 /* If we do not already have a type-specifier, assume we are looking
10398 at a simple-type-specifier. */
10399 type_spec = cp_parser_simple_type_specifier (parser,
10403 /* If we didn't find a type-specifier, and a type-specifier was not
10404 optional in this context, issue an error message. */
10405 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10407 cp_parser_error (parser, "expected type specifier");
10408 return error_mark_node;
10414 /* Parse a simple-type-specifier.
10416 simple-type-specifier:
10417 :: [opt] nested-name-specifier [opt] type-name
10418 :: [opt] nested-name-specifier template template-id
10433 simple-type-specifier:
10434 __typeof__ unary-expression
10435 __typeof__ ( type-id )
10437 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10438 appropriately updated. */
10441 cp_parser_simple_type_specifier (cp_parser* parser,
10442 cp_decl_specifier_seq *decl_specs,
10443 cp_parser_flags flags)
10445 tree type = NULL_TREE;
10448 /* Peek at the next token. */
10449 token = cp_lexer_peek_token (parser->lexer);
10451 /* If we're looking at a keyword, things are easy. */
10452 switch (token->keyword)
10456 decl_specs->explicit_char_p = true;
10457 type = char_type_node;
10460 type = wchar_type_node;
10463 type = boolean_type_node;
10467 ++decl_specs->specs[(int) ds_short];
10468 type = short_integer_type_node;
10472 decl_specs->explicit_int_p = true;
10473 type = integer_type_node;
10477 ++decl_specs->specs[(int) ds_long];
10478 type = long_integer_type_node;
10482 ++decl_specs->specs[(int) ds_signed];
10483 type = integer_type_node;
10487 ++decl_specs->specs[(int) ds_unsigned];
10488 type = unsigned_type_node;
10491 type = float_type_node;
10494 type = double_type_node;
10497 type = void_type_node;
10501 /* Consume the `typeof' token. */
10502 cp_lexer_consume_token (parser->lexer);
10503 /* Parse the operand to `typeof'. */
10504 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10505 /* If it is not already a TYPE, take its type. */
10506 if (!TYPE_P (type))
10507 type = finish_typeof (type);
10510 cp_parser_set_decl_spec_type (decl_specs, type,
10511 /*user_defined_p=*/true);
10519 /* If the type-specifier was for a built-in type, we're done. */
10524 /* Record the type. */
10526 && (token->keyword != RID_SIGNED
10527 && token->keyword != RID_UNSIGNED
10528 && token->keyword != RID_SHORT
10529 && token->keyword != RID_LONG))
10530 cp_parser_set_decl_spec_type (decl_specs,
10532 /*user_defined=*/false);
10534 decl_specs->any_specifiers_p = true;
10536 /* Consume the token. */
10537 id = cp_lexer_consume_token (parser->lexer)->u.value;
10539 /* There is no valid C++ program where a non-template type is
10540 followed by a "<". That usually indicates that the user thought
10541 that the type was a template. */
10542 cp_parser_check_for_invalid_template_id (parser, type);
10544 return TYPE_NAME (type);
10547 /* The type-specifier must be a user-defined type. */
10548 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10553 /* Don't gobble tokens or issue error messages if this is an
10554 optional type-specifier. */
10555 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10556 cp_parser_parse_tentatively (parser);
10558 /* Look for the optional `::' operator. */
10560 = (cp_parser_global_scope_opt (parser,
10561 /*current_scope_valid_p=*/false)
10563 /* Look for the nested-name specifier. */
10565 = (cp_parser_nested_name_specifier_opt (parser,
10566 /*typename_keyword_p=*/false,
10567 /*check_dependency_p=*/true,
10569 /*is_declaration=*/false)
10571 /* If we have seen a nested-name-specifier, and the next token
10572 is `template', then we are using the template-id production. */
10574 && cp_parser_optional_template_keyword (parser))
10576 /* Look for the template-id. */
10577 type = cp_parser_template_id (parser,
10578 /*template_keyword_p=*/true,
10579 /*check_dependency_p=*/true,
10580 /*is_declaration=*/false);
10581 /* If the template-id did not name a type, we are out of
10583 if (TREE_CODE (type) != TYPE_DECL)
10585 cp_parser_error (parser, "expected template-id for type");
10589 /* Otherwise, look for a type-name. */
10591 type = cp_parser_type_name (parser);
10592 /* Keep track of all name-lookups performed in class scopes. */
10596 && TREE_CODE (type) == TYPE_DECL
10597 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10598 maybe_note_name_used_in_class (DECL_NAME (type), type);
10599 /* If it didn't work out, we don't have a TYPE. */
10600 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10601 && !cp_parser_parse_definitely (parser))
10603 if (type && decl_specs)
10604 cp_parser_set_decl_spec_type (decl_specs, type,
10605 /*user_defined=*/true);
10608 /* If we didn't get a type-name, issue an error message. */
10609 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10611 cp_parser_error (parser, "expected type-name");
10612 return error_mark_node;
10615 /* There is no valid C++ program where a non-template type is
10616 followed by a "<". That usually indicates that the user thought
10617 that the type was a template. */
10618 if (type && type != error_mark_node)
10620 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10621 If it is, then the '<'...'>' enclose protocol names rather than
10622 template arguments, and so everything is fine. */
10623 if (c_dialect_objc ()
10624 && (objc_is_id (type) || objc_is_class_name (type)))
10626 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10627 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10629 /* Clobber the "unqualified" type previously entered into
10630 DECL_SPECS with the new, improved protocol-qualified version. */
10632 decl_specs->type = qual_type;
10637 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10643 /* Parse a type-name.
10656 Returns a TYPE_DECL for the type. */
10659 cp_parser_type_name (cp_parser* parser)
10664 /* We can't know yet whether it is a class-name or not. */
10665 cp_parser_parse_tentatively (parser);
10666 /* Try a class-name. */
10667 type_decl = cp_parser_class_name (parser,
10668 /*typename_keyword_p=*/false,
10669 /*template_keyword_p=*/false,
10671 /*check_dependency_p=*/true,
10672 /*class_head_p=*/false,
10673 /*is_declaration=*/false);
10674 /* If it's not a class-name, keep looking. */
10675 if (!cp_parser_parse_definitely (parser))
10677 /* It must be a typedef-name or an enum-name. */
10678 identifier = cp_parser_identifier (parser);
10679 if (identifier == error_mark_node)
10680 return error_mark_node;
10682 /* Look up the type-name. */
10683 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10685 if (TREE_CODE (type_decl) != TYPE_DECL
10686 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10688 /* See if this is an Objective-C type. */
10689 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10690 tree type = objc_get_protocol_qualified_type (identifier, protos);
10692 type_decl = TYPE_NAME (type);
10695 /* Issue an error if we did not find a type-name. */
10696 if (TREE_CODE (type_decl) != TYPE_DECL)
10698 if (!cp_parser_simulate_error (parser))
10699 cp_parser_name_lookup_error (parser, identifier, type_decl,
10701 type_decl = error_mark_node;
10703 /* Remember that the name was used in the definition of the
10704 current class so that we can check later to see if the
10705 meaning would have been different after the class was
10706 entirely defined. */
10707 else if (type_decl != error_mark_node
10709 maybe_note_name_used_in_class (identifier, type_decl);
10716 /* Parse an elaborated-type-specifier. Note that the grammar given
10717 here incorporates the resolution to DR68.
10719 elaborated-type-specifier:
10720 class-key :: [opt] nested-name-specifier [opt] identifier
10721 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10722 enum :: [opt] nested-name-specifier [opt] identifier
10723 typename :: [opt] nested-name-specifier identifier
10724 typename :: [opt] nested-name-specifier template [opt]
10729 elaborated-type-specifier:
10730 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10731 class-key attributes :: [opt] nested-name-specifier [opt]
10732 template [opt] template-id
10733 enum attributes :: [opt] nested-name-specifier [opt] identifier
10735 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10736 declared `friend'. If IS_DECLARATION is TRUE, then this
10737 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10738 something is being declared.
10740 Returns the TYPE specified. */
10743 cp_parser_elaborated_type_specifier (cp_parser* parser,
10745 bool is_declaration)
10747 enum tag_types tag_type;
10749 tree type = NULL_TREE;
10750 tree attributes = NULL_TREE;
10752 /* See if we're looking at the `enum' keyword. */
10753 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10755 /* Consume the `enum' token. */
10756 cp_lexer_consume_token (parser->lexer);
10757 /* Remember that it's an enumeration type. */
10758 tag_type = enum_type;
10759 /* Parse the attributes. */
10760 attributes = cp_parser_attributes_opt (parser);
10762 /* Or, it might be `typename'. */
10763 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10766 /* Consume the `typename' token. */
10767 cp_lexer_consume_token (parser->lexer);
10768 /* Remember that it's a `typename' type. */
10769 tag_type = typename_type;
10770 /* The `typename' keyword is only allowed in templates. */
10771 if (!processing_template_decl)
10772 pedwarn ("using %<typename%> outside of template");
10774 /* Otherwise it must be a class-key. */
10777 tag_type = cp_parser_class_key (parser);
10778 if (tag_type == none_type)
10779 return error_mark_node;
10780 /* Parse the attributes. */
10781 attributes = cp_parser_attributes_opt (parser);
10784 /* Look for the `::' operator. */
10785 cp_parser_global_scope_opt (parser,
10786 /*current_scope_valid_p=*/false);
10787 /* Look for the nested-name-specifier. */
10788 if (tag_type == typename_type)
10790 if (!cp_parser_nested_name_specifier (parser,
10791 /*typename_keyword_p=*/true,
10792 /*check_dependency_p=*/true,
10795 return error_mark_node;
10798 /* Even though `typename' is not present, the proposed resolution
10799 to Core Issue 180 says that in `class A<T>::B', `B' should be
10800 considered a type-name, even if `A<T>' is dependent. */
10801 cp_parser_nested_name_specifier_opt (parser,
10802 /*typename_keyword_p=*/true,
10803 /*check_dependency_p=*/true,
10806 /* For everything but enumeration types, consider a template-id.
10807 For an enumeration type, consider only a plain identifier. */
10808 if (tag_type != enum_type)
10810 bool template_p = false;
10813 /* Allow the `template' keyword. */
10814 template_p = cp_parser_optional_template_keyword (parser);
10815 /* If we didn't see `template', we don't know if there's a
10816 template-id or not. */
10818 cp_parser_parse_tentatively (parser);
10819 /* Parse the template-id. */
10820 decl = cp_parser_template_id (parser, template_p,
10821 /*check_dependency_p=*/true,
10823 /* If we didn't find a template-id, look for an ordinary
10825 if (!template_p && !cp_parser_parse_definitely (parser))
10827 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
10828 in effect, then we must assume that, upon instantiation, the
10829 template will correspond to a class. */
10830 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
10831 && tag_type == typename_type)
10832 type = make_typename_type (parser->scope, decl,
10834 /*complain=*/tf_error);
10836 type = TREE_TYPE (decl);
10841 identifier = cp_parser_identifier (parser);
10843 if (identifier == error_mark_node)
10845 parser->scope = NULL_TREE;
10846 return error_mark_node;
10849 /* For a `typename', we needn't call xref_tag. */
10850 if (tag_type == typename_type
10851 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
10852 return cp_parser_make_typename_type (parser, parser->scope,
10854 /* Look up a qualified name in the usual way. */
10858 tree ambiguous_decls;
10860 decl = cp_parser_lookup_name (parser, identifier,
10862 /*is_template=*/false,
10863 /*is_namespace=*/false,
10864 /*check_dependency=*/true,
10867 /* If the lookup was ambiguous, an error will already have been
10869 if (ambiguous_decls)
10870 return error_mark_node;
10872 /* If we are parsing friend declaration, DECL may be a
10873 TEMPLATE_DECL tree node here. However, we need to check
10874 whether this TEMPLATE_DECL results in valid code. Consider
10875 the following example:
10878 template <class T> class C {};
10881 template <class T> friend class N::C; // #1, valid code
10883 template <class T> class Y {
10884 friend class N::C; // #2, invalid code
10887 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
10888 name lookup of `N::C'. We see that friend declaration must
10889 be template for the code to be valid. Note that
10890 processing_template_decl does not work here since it is
10891 always 1 for the above two cases. */
10893 decl = (cp_parser_maybe_treat_template_as_class
10894 (decl, /*tag_name_p=*/is_friend
10895 && parser->num_template_parameter_lists));
10897 if (TREE_CODE (decl) != TYPE_DECL)
10899 cp_parser_diagnose_invalid_type_name (parser,
10902 return error_mark_node;
10905 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
10907 bool allow_template = (parser->num_template_parameter_lists
10908 || DECL_SELF_REFERENCE_P (decl));
10909 type = check_elaborated_type_specifier (tag_type, decl,
10912 if (type == error_mark_node)
10913 return error_mark_node;
10916 type = TREE_TYPE (decl);
10920 /* An elaborated-type-specifier sometimes introduces a new type and
10921 sometimes names an existing type. Normally, the rule is that it
10922 introduces a new type only if there is not an existing type of
10923 the same name already in scope. For example, given:
10926 void f() { struct S s; }
10928 the `struct S' in the body of `f' is the same `struct S' as in
10929 the global scope; the existing definition is used. However, if
10930 there were no global declaration, this would introduce a new
10931 local class named `S'.
10933 An exception to this rule applies to the following code:
10935 namespace N { struct S; }
10937 Here, the elaborated-type-specifier names a new type
10938 unconditionally; even if there is already an `S' in the
10939 containing scope this declaration names a new type.
10940 This exception only applies if the elaborated-type-specifier
10941 forms the complete declaration:
10945 A declaration consisting solely of `class-key identifier ;' is
10946 either a redeclaration of the name in the current scope or a
10947 forward declaration of the identifier as a class name. It
10948 introduces the name into the current scope.
10950 We are in this situation precisely when the next token is a `;'.
10952 An exception to the exception is that a `friend' declaration does
10953 *not* name a new type; i.e., given:
10955 struct S { friend struct T; };
10957 `T' is not a new type in the scope of `S'.
10959 Also, `new struct S' or `sizeof (struct S)' never results in the
10960 definition of a new type; a new type can only be declared in a
10961 declaration context. */
10967 /* Friends have special name lookup rules. */
10968 ts = ts_within_enclosing_non_class;
10969 else if (is_declaration
10970 && cp_lexer_next_token_is (parser->lexer,
10972 /* This is a `class-key identifier ;' */
10978 (parser->num_template_parameter_lists
10979 && (cp_parser_next_token_starts_class_definition_p (parser)
10980 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10981 /* An unqualified name was used to reference this type, so
10982 there were no qualifying templates. */
10983 if (!cp_parser_check_template_parameters (parser,
10984 /*num_templates=*/0))
10985 return error_mark_node;
10986 type = xref_tag (tag_type, identifier, ts, template_p);
10990 if (type == error_mark_node)
10991 return error_mark_node;
10993 /* Allow attributes on forward declarations of classes. */
10996 if (TREE_CODE (type) == TYPENAME_TYPE)
10997 warning (OPT_Wattributes,
10998 "attributes ignored on uninstantiated type");
10999 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11000 && ! processing_explicit_instantiation)
11001 warning (OPT_Wattributes,
11002 "attributes ignored on template instantiation");
11003 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11004 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11006 warning (OPT_Wattributes,
11007 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11010 if (tag_type != enum_type)
11011 cp_parser_check_class_key (tag_type, type);
11013 /* A "<" cannot follow an elaborated type specifier. If that
11014 happens, the user was probably trying to form a template-id. */
11015 cp_parser_check_for_invalid_template_id (parser, type);
11020 /* Parse an enum-specifier.
11023 enum identifier [opt] { enumerator-list [opt] }
11026 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11029 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11030 if the token stream isn't an enum-specifier after all. */
11033 cp_parser_enum_specifier (cp_parser* parser)
11039 /* Parse tentatively so that we can back up if we don't find a
11041 cp_parser_parse_tentatively (parser);
11043 /* Caller guarantees that the current token is 'enum', an identifier
11044 possibly follows, and the token after that is an opening brace.
11045 If we don't have an identifier, fabricate an anonymous name for
11046 the enumeration being defined. */
11047 cp_lexer_consume_token (parser->lexer);
11049 attributes = cp_parser_attributes_opt (parser);
11051 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11052 identifier = cp_parser_identifier (parser);
11054 identifier = make_anon_name ();
11056 /* Look for the `{' but don't consume it yet. */
11057 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11058 cp_parser_simulate_error (parser);
11060 if (!cp_parser_parse_definitely (parser))
11063 /* Issue an error message if type-definitions are forbidden here. */
11064 if (!cp_parser_check_type_definition (parser))
11065 type = error_mark_node;
11067 /* Create the new type. We do this before consuming the opening
11068 brace so the enum will be recorded as being on the line of its
11069 tag (or the 'enum' keyword, if there is no tag). */
11070 type = start_enum (identifier);
11072 /* Consume the opening brace. */
11073 cp_lexer_consume_token (parser->lexer);
11075 if (type == error_mark_node)
11077 cp_parser_skip_to_end_of_block_or_statement (parser);
11078 return error_mark_node;
11081 /* If the next token is not '}', then there are some enumerators. */
11082 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11083 cp_parser_enumerator_list (parser, type);
11085 /* Consume the final '}'. */
11086 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11088 /* Look for trailing attributes to apply to this enumeration, and
11089 apply them if appropriate. */
11090 if (cp_parser_allow_gnu_extensions_p (parser))
11092 tree trailing_attr = cp_parser_attributes_opt (parser);
11093 cplus_decl_attributes (&type,
11095 (int) ATTR_FLAG_TYPE_IN_PLACE);
11098 /* Finish up the enumeration. */
11099 finish_enum (type);
11104 /* Parse an enumerator-list. The enumerators all have the indicated
11108 enumerator-definition
11109 enumerator-list , enumerator-definition */
11112 cp_parser_enumerator_list (cp_parser* parser, tree type)
11116 /* Parse an enumerator-definition. */
11117 cp_parser_enumerator_definition (parser, type);
11119 /* If the next token is not a ',', we've reached the end of
11121 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11123 /* Otherwise, consume the `,' and keep going. */
11124 cp_lexer_consume_token (parser->lexer);
11125 /* If the next token is a `}', there is a trailing comma. */
11126 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11128 if (pedantic && !in_system_header)
11129 pedwarn ("comma at end of enumerator list");
11135 /* Parse an enumerator-definition. The enumerator has the indicated
11138 enumerator-definition:
11140 enumerator = constant-expression
11146 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11151 /* Look for the identifier. */
11152 identifier = cp_parser_identifier (parser);
11153 if (identifier == error_mark_node)
11156 /* If the next token is an '=', then there is an explicit value. */
11157 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11159 /* Consume the `=' token. */
11160 cp_lexer_consume_token (parser->lexer);
11161 /* Parse the value. */
11162 value = cp_parser_constant_expression (parser,
11163 /*allow_non_constant_p=*/false,
11169 /* Create the enumerator. */
11170 build_enumerator (identifier, value, type);
11173 /* Parse a namespace-name.
11176 original-namespace-name
11179 Returns the NAMESPACE_DECL for the namespace. */
11182 cp_parser_namespace_name (cp_parser* parser)
11185 tree namespace_decl;
11187 /* Get the name of the namespace. */
11188 identifier = cp_parser_identifier (parser);
11189 if (identifier == error_mark_node)
11190 return error_mark_node;
11192 /* Look up the identifier in the currently active scope. Look only
11193 for namespaces, due to:
11195 [basic.lookup.udir]
11197 When looking up a namespace-name in a using-directive or alias
11198 definition, only namespace names are considered.
11202 [basic.lookup.qual]
11204 During the lookup of a name preceding the :: scope resolution
11205 operator, object, function, and enumerator names are ignored.
11207 (Note that cp_parser_class_or_namespace_name only calls this
11208 function if the token after the name is the scope resolution
11210 namespace_decl = cp_parser_lookup_name (parser, identifier,
11212 /*is_template=*/false,
11213 /*is_namespace=*/true,
11214 /*check_dependency=*/true,
11215 /*ambiguous_decls=*/NULL);
11216 /* If it's not a namespace, issue an error. */
11217 if (namespace_decl == error_mark_node
11218 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11220 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11221 error ("%qD is not a namespace-name", identifier);
11222 cp_parser_error (parser, "expected namespace-name");
11223 namespace_decl = error_mark_node;
11226 return namespace_decl;
11229 /* Parse a namespace-definition.
11231 namespace-definition:
11232 named-namespace-definition
11233 unnamed-namespace-definition
11235 named-namespace-definition:
11236 original-namespace-definition
11237 extension-namespace-definition
11239 original-namespace-definition:
11240 namespace identifier { namespace-body }
11242 extension-namespace-definition:
11243 namespace original-namespace-name { namespace-body }
11245 unnamed-namespace-definition:
11246 namespace { namespace-body } */
11249 cp_parser_namespace_definition (cp_parser* parser)
11251 tree identifier, attribs;
11253 /* Look for the `namespace' keyword. */
11254 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11256 /* Get the name of the namespace. We do not attempt to distinguish
11257 between an original-namespace-definition and an
11258 extension-namespace-definition at this point. The semantic
11259 analysis routines are responsible for that. */
11260 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11261 identifier = cp_parser_identifier (parser);
11263 identifier = NULL_TREE;
11265 /* Parse any specified attributes. */
11266 attribs = cp_parser_attributes_opt (parser);
11268 /* Look for the `{' to start the namespace. */
11269 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11270 /* Start the namespace. */
11271 push_namespace_with_attribs (identifier, attribs);
11272 /* Parse the body of the namespace. */
11273 cp_parser_namespace_body (parser);
11274 /* Finish the namespace. */
11276 /* Look for the final `}'. */
11277 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11280 /* Parse a namespace-body.
11283 declaration-seq [opt] */
11286 cp_parser_namespace_body (cp_parser* parser)
11288 cp_parser_declaration_seq_opt (parser);
11291 /* Parse a namespace-alias-definition.
11293 namespace-alias-definition:
11294 namespace identifier = qualified-namespace-specifier ; */
11297 cp_parser_namespace_alias_definition (cp_parser* parser)
11300 tree namespace_specifier;
11302 /* Look for the `namespace' keyword. */
11303 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11304 /* Look for the identifier. */
11305 identifier = cp_parser_identifier (parser);
11306 if (identifier == error_mark_node)
11308 /* Look for the `=' token. */
11309 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11310 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11312 error ("%<namespace%> definition is not allowed here");
11313 /* Skip the definition. */
11314 cp_lexer_consume_token (parser->lexer);
11315 if (cp_parser_skip_to_closing_brace (parser))
11316 cp_lexer_consume_token (parser->lexer);
11319 cp_parser_require (parser, CPP_EQ, "`='");
11320 /* Look for the qualified-namespace-specifier. */
11321 namespace_specifier
11322 = cp_parser_qualified_namespace_specifier (parser);
11323 /* Look for the `;' token. */
11324 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11326 /* Register the alias in the symbol table. */
11327 do_namespace_alias (identifier, namespace_specifier);
11330 /* Parse a qualified-namespace-specifier.
11332 qualified-namespace-specifier:
11333 :: [opt] nested-name-specifier [opt] namespace-name
11335 Returns a NAMESPACE_DECL corresponding to the specified
11339 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11341 /* Look for the optional `::'. */
11342 cp_parser_global_scope_opt (parser,
11343 /*current_scope_valid_p=*/false);
11345 /* Look for the optional nested-name-specifier. */
11346 cp_parser_nested_name_specifier_opt (parser,
11347 /*typename_keyword_p=*/false,
11348 /*check_dependency_p=*/true,
11350 /*is_declaration=*/true);
11352 return cp_parser_namespace_name (parser);
11355 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11356 access declaration.
11359 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11360 using :: unqualified-id ;
11362 access-declaration:
11368 cp_parser_using_declaration (cp_parser* parser,
11369 bool access_declaration_p)
11372 bool typename_p = false;
11373 bool global_scope_p;
11378 if (access_declaration_p)
11379 cp_parser_parse_tentatively (parser);
11382 /* Look for the `using' keyword. */
11383 cp_parser_require_keyword (parser, RID_USING, "`using'");
11385 /* Peek at the next token. */
11386 token = cp_lexer_peek_token (parser->lexer);
11387 /* See if it's `typename'. */
11388 if (token->keyword == RID_TYPENAME)
11390 /* Remember that we've seen it. */
11392 /* Consume the `typename' token. */
11393 cp_lexer_consume_token (parser->lexer);
11397 /* Look for the optional global scope qualification. */
11399 = (cp_parser_global_scope_opt (parser,
11400 /*current_scope_valid_p=*/false)
11403 /* If we saw `typename', or didn't see `::', then there must be a
11404 nested-name-specifier present. */
11405 if (typename_p || !global_scope_p)
11406 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11407 /*check_dependency_p=*/true,
11409 /*is_declaration=*/true);
11410 /* Otherwise, we could be in either of the two productions. In that
11411 case, treat the nested-name-specifier as optional. */
11413 qscope = cp_parser_nested_name_specifier_opt (parser,
11414 /*typename_keyword_p=*/false,
11415 /*check_dependency_p=*/true,
11417 /*is_declaration=*/true);
11419 qscope = global_namespace;
11421 if (access_declaration_p && cp_parser_error_occurred (parser))
11422 /* Something has already gone wrong; there's no need to parse
11423 further. Since an error has occurred, the return value of
11424 cp_parser_parse_definitely will be false, as required. */
11425 return cp_parser_parse_definitely (parser);
11427 /* Parse the unqualified-id. */
11428 identifier = cp_parser_unqualified_id (parser,
11429 /*template_keyword_p=*/false,
11430 /*check_dependency_p=*/true,
11431 /*declarator_p=*/true,
11432 /*optional_p=*/false);
11434 if (access_declaration_p)
11436 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11437 cp_parser_simulate_error (parser);
11438 if (!cp_parser_parse_definitely (parser))
11442 /* The function we call to handle a using-declaration is different
11443 depending on what scope we are in. */
11444 if (qscope == error_mark_node || identifier == error_mark_node)
11446 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11447 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11448 /* [namespace.udecl]
11450 A using declaration shall not name a template-id. */
11451 error ("a template-id may not appear in a using-declaration");
11454 if (at_class_scope_p ())
11456 /* Create the USING_DECL. */
11457 decl = do_class_using_decl (parser->scope, identifier);
11458 /* Add it to the list of members in this class. */
11459 finish_member_declaration (decl);
11463 decl = cp_parser_lookup_name_simple (parser, identifier);
11464 if (decl == error_mark_node)
11465 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11466 else if (!at_namespace_scope_p ())
11467 do_local_using_decl (decl, qscope, identifier);
11469 do_toplevel_using_decl (decl, qscope, identifier);
11473 /* Look for the final `;'. */
11474 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11479 /* Parse a using-directive.
11482 using namespace :: [opt] nested-name-specifier [opt]
11483 namespace-name ; */
11486 cp_parser_using_directive (cp_parser* parser)
11488 tree namespace_decl;
11491 /* Look for the `using' keyword. */
11492 cp_parser_require_keyword (parser, RID_USING, "`using'");
11493 /* And the `namespace' keyword. */
11494 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11495 /* Look for the optional `::' operator. */
11496 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11497 /* And the optional nested-name-specifier. */
11498 cp_parser_nested_name_specifier_opt (parser,
11499 /*typename_keyword_p=*/false,
11500 /*check_dependency_p=*/true,
11502 /*is_declaration=*/true);
11503 /* Get the namespace being used. */
11504 namespace_decl = cp_parser_namespace_name (parser);
11505 /* And any specified attributes. */
11506 attribs = cp_parser_attributes_opt (parser);
11507 /* Update the symbol table. */
11508 parse_using_directive (namespace_decl, attribs);
11509 /* Look for the final `;'. */
11510 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11513 /* Parse an asm-definition.
11516 asm ( string-literal ) ;
11521 asm volatile [opt] ( string-literal ) ;
11522 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11523 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11524 : asm-operand-list [opt] ) ;
11525 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11526 : asm-operand-list [opt]
11527 : asm-operand-list [opt] ) ; */
11530 cp_parser_asm_definition (cp_parser* parser)
11533 tree outputs = NULL_TREE;
11534 tree inputs = NULL_TREE;
11535 tree clobbers = NULL_TREE;
11537 bool volatile_p = false;
11538 bool extended_p = false;
11540 /* Look for the `asm' keyword. */
11541 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11542 /* See if the next token is `volatile'. */
11543 if (cp_parser_allow_gnu_extensions_p (parser)
11544 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11546 /* Remember that we saw the `volatile' keyword. */
11548 /* Consume the token. */
11549 cp_lexer_consume_token (parser->lexer);
11551 /* Look for the opening `('. */
11552 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11554 /* Look for the string. */
11555 string = cp_parser_string_literal (parser, false, false);
11556 if (string == error_mark_node)
11558 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11559 /*consume_paren=*/true);
11563 /* If we're allowing GNU extensions, check for the extended assembly
11564 syntax. Unfortunately, the `:' tokens need not be separated by
11565 a space in C, and so, for compatibility, we tolerate that here
11566 too. Doing that means that we have to treat the `::' operator as
11568 if (cp_parser_allow_gnu_extensions_p (parser)
11569 && parser->in_function_body
11570 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11571 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11573 bool inputs_p = false;
11574 bool clobbers_p = false;
11576 /* The extended syntax was used. */
11579 /* Look for outputs. */
11580 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11582 /* Consume the `:'. */
11583 cp_lexer_consume_token (parser->lexer);
11584 /* Parse the output-operands. */
11585 if (cp_lexer_next_token_is_not (parser->lexer,
11587 && cp_lexer_next_token_is_not (parser->lexer,
11589 && cp_lexer_next_token_is_not (parser->lexer,
11591 outputs = cp_parser_asm_operand_list (parser);
11593 /* If the next token is `::', there are no outputs, and the
11594 next token is the beginning of the inputs. */
11595 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11596 /* The inputs are coming next. */
11599 /* Look for inputs. */
11601 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11603 /* Consume the `:' or `::'. */
11604 cp_lexer_consume_token (parser->lexer);
11605 /* Parse the output-operands. */
11606 if (cp_lexer_next_token_is_not (parser->lexer,
11608 && cp_lexer_next_token_is_not (parser->lexer,
11610 inputs = cp_parser_asm_operand_list (parser);
11612 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11613 /* The clobbers are coming next. */
11616 /* Look for clobbers. */
11618 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11620 /* Consume the `:' or `::'. */
11621 cp_lexer_consume_token (parser->lexer);
11622 /* Parse the clobbers. */
11623 if (cp_lexer_next_token_is_not (parser->lexer,
11625 clobbers = cp_parser_asm_clobber_list (parser);
11628 /* Look for the closing `)'. */
11629 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11630 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11631 /*consume_paren=*/true);
11632 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11634 /* Create the ASM_EXPR. */
11635 if (parser->in_function_body)
11637 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11639 /* If the extended syntax was not used, mark the ASM_EXPR. */
11642 tree temp = asm_stmt;
11643 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11644 temp = TREE_OPERAND (temp, 0);
11646 ASM_INPUT_P (temp) = 1;
11650 cgraph_add_asm_node (string);
11653 /* Declarators [gram.dcl.decl] */
11655 /* Parse an init-declarator.
11658 declarator initializer [opt]
11663 declarator asm-specification [opt] attributes [opt] initializer [opt]
11665 function-definition:
11666 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11668 decl-specifier-seq [opt] declarator function-try-block
11672 function-definition:
11673 __extension__ function-definition
11675 The DECL_SPECIFIERS apply to this declarator. Returns a
11676 representation of the entity declared. If MEMBER_P is TRUE, then
11677 this declarator appears in a class scope. The new DECL created by
11678 this declarator is returned.
11680 The CHECKS are access checks that should be performed once we know
11681 what entity is being declared (and, therefore, what classes have
11684 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11685 for a function-definition here as well. If the declarator is a
11686 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11687 be TRUE upon return. By that point, the function-definition will
11688 have been completely parsed.
11690 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11694 cp_parser_init_declarator (cp_parser* parser,
11695 cp_decl_specifier_seq *decl_specifiers,
11696 VEC (deferred_access_check,gc)* checks,
11697 bool function_definition_allowed_p,
11699 int declares_class_or_enum,
11700 bool* function_definition_p)
11703 cp_declarator *declarator;
11704 tree prefix_attributes;
11706 tree asm_specification;
11708 tree decl = NULL_TREE;
11710 bool is_initialized;
11711 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11712 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11714 enum cpp_ttype initialization_kind;
11715 bool is_parenthesized_init = false;
11716 bool is_non_constant_init;
11717 int ctor_dtor_or_conv_p;
11719 tree pushed_scope = NULL;
11721 /* Gather the attributes that were provided with the
11722 decl-specifiers. */
11723 prefix_attributes = decl_specifiers->attributes;
11725 /* Assume that this is not the declarator for a function
11727 if (function_definition_p)
11728 *function_definition_p = false;
11730 /* Defer access checks while parsing the declarator; we cannot know
11731 what names are accessible until we know what is being
11733 resume_deferring_access_checks ();
11735 /* Parse the declarator. */
11737 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11738 &ctor_dtor_or_conv_p,
11739 /*parenthesized_p=*/NULL,
11740 /*member_p=*/false);
11741 /* Gather up the deferred checks. */
11742 stop_deferring_access_checks ();
11744 /* If the DECLARATOR was erroneous, there's no need to go
11746 if (declarator == cp_error_declarator)
11747 return error_mark_node;
11749 /* Check that the number of template-parameter-lists is OK. */
11750 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11751 return error_mark_node;
11753 if (declares_class_or_enum & 2)
11754 cp_parser_check_for_definition_in_return_type (declarator,
11755 decl_specifiers->type);
11757 /* Figure out what scope the entity declared by the DECLARATOR is
11758 located in. `grokdeclarator' sometimes changes the scope, so
11759 we compute it now. */
11760 scope = get_scope_of_declarator (declarator);
11762 /* If we're allowing GNU extensions, look for an asm-specification
11764 if (cp_parser_allow_gnu_extensions_p (parser))
11766 /* Look for an asm-specification. */
11767 asm_specification = cp_parser_asm_specification_opt (parser);
11768 /* And attributes. */
11769 attributes = cp_parser_attributes_opt (parser);
11773 asm_specification = NULL_TREE;
11774 attributes = NULL_TREE;
11777 /* Peek at the next token. */
11778 token = cp_lexer_peek_token (parser->lexer);
11779 /* Check to see if the token indicates the start of a
11780 function-definition. */
11781 if (cp_parser_token_starts_function_definition_p (token))
11783 if (!function_definition_allowed_p)
11785 /* If a function-definition should not appear here, issue an
11787 cp_parser_error (parser,
11788 "a function-definition is not allowed here");
11789 return error_mark_node;
11793 /* Neither attributes nor an asm-specification are allowed
11794 on a function-definition. */
11795 if (asm_specification)
11796 error ("an asm-specification is not allowed on a function-definition");
11798 error ("attributes are not allowed on a function-definition");
11799 /* This is a function-definition. */
11800 *function_definition_p = true;
11802 /* Parse the function definition. */
11804 decl = cp_parser_save_member_function_body (parser,
11807 prefix_attributes);
11810 = (cp_parser_function_definition_from_specifiers_and_declarator
11811 (parser, decl_specifiers, prefix_attributes, declarator));
11819 Only in function declarations for constructors, destructors, and
11820 type conversions can the decl-specifier-seq be omitted.
11822 We explicitly postpone this check past the point where we handle
11823 function-definitions because we tolerate function-definitions
11824 that are missing their return types in some modes. */
11825 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11827 cp_parser_error (parser,
11828 "expected constructor, destructor, or type conversion");
11829 return error_mark_node;
11832 /* An `=' or an `(' indicates an initializer. */
11833 if (token->type == CPP_EQ
11834 || token->type == CPP_OPEN_PAREN)
11836 is_initialized = true;
11837 initialization_kind = token->type;
11841 /* If the init-declarator isn't initialized and isn't followed by a
11842 `,' or `;', it's not a valid init-declarator. */
11843 if (token->type != CPP_COMMA
11844 && token->type != CPP_SEMICOLON)
11846 cp_parser_error (parser, "expected initializer");
11847 return error_mark_node;
11849 is_initialized = false;
11850 initialization_kind = CPP_EOF;
11853 /* Because start_decl has side-effects, we should only call it if we
11854 know we're going ahead. By this point, we know that we cannot
11855 possibly be looking at any other construct. */
11856 cp_parser_commit_to_tentative_parse (parser);
11858 /* If the decl specifiers were bad, issue an error now that we're
11859 sure this was intended to be a declarator. Then continue
11860 declaring the variable(s), as int, to try to cut down on further
11862 if (decl_specifiers->any_specifiers_p
11863 && decl_specifiers->type == error_mark_node)
11865 cp_parser_error (parser, "invalid type in declaration");
11866 decl_specifiers->type = integer_type_node;
11869 /* Check to see whether or not this declaration is a friend. */
11870 friend_p = cp_parser_friend_p (decl_specifiers);
11872 /* Enter the newly declared entry in the symbol table. If we're
11873 processing a declaration in a class-specifier, we wait until
11874 after processing the initializer. */
11877 if (parser->in_unbraced_linkage_specification_p)
11878 decl_specifiers->storage_class = sc_extern;
11879 decl = start_decl (declarator, decl_specifiers,
11880 is_initialized, attributes, prefix_attributes,
11884 /* Enter the SCOPE. That way unqualified names appearing in the
11885 initializer will be looked up in SCOPE. */
11886 pushed_scope = push_scope (scope);
11888 /* Perform deferred access control checks, now that we know in which
11889 SCOPE the declared entity resides. */
11890 if (!member_p && decl)
11892 tree saved_current_function_decl = NULL_TREE;
11894 /* If the entity being declared is a function, pretend that we
11895 are in its scope. If it is a `friend', it may have access to
11896 things that would not otherwise be accessible. */
11897 if (TREE_CODE (decl) == FUNCTION_DECL)
11899 saved_current_function_decl = current_function_decl;
11900 current_function_decl = decl;
11903 /* Perform access checks for template parameters. */
11904 cp_parser_perform_template_parameter_access_checks (checks);
11906 /* Perform the access control checks for the declarator and the
11907 the decl-specifiers. */
11908 perform_deferred_access_checks ();
11910 /* Restore the saved value. */
11911 if (TREE_CODE (decl) == FUNCTION_DECL)
11912 current_function_decl = saved_current_function_decl;
11915 /* Parse the initializer. */
11916 initializer = NULL_TREE;
11917 is_parenthesized_init = false;
11918 is_non_constant_init = true;
11919 if (is_initialized)
11921 if (function_declarator_p (declarator))
11923 if (initialization_kind == CPP_EQ)
11924 initializer = cp_parser_pure_specifier (parser);
11927 /* If the declaration was erroneous, we don't really
11928 know what the user intended, so just silently
11929 consume the initializer. */
11930 if (decl != error_mark_node)
11931 error ("initializer provided for function");
11932 cp_parser_skip_to_closing_parenthesis (parser,
11933 /*recovering=*/true,
11934 /*or_comma=*/false,
11935 /*consume_paren=*/true);
11939 initializer = cp_parser_initializer (parser,
11940 &is_parenthesized_init,
11941 &is_non_constant_init);
11944 /* The old parser allows attributes to appear after a parenthesized
11945 initializer. Mark Mitchell proposed removing this functionality
11946 on the GCC mailing lists on 2002-08-13. This parser accepts the
11947 attributes -- but ignores them. */
11948 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11949 if (cp_parser_attributes_opt (parser))
11950 warning (OPT_Wattributes,
11951 "attributes after parenthesized initializer ignored");
11953 /* For an in-class declaration, use `grokfield' to create the
11959 pop_scope (pushed_scope);
11960 pushed_scope = false;
11962 decl = grokfield (declarator, decl_specifiers,
11963 initializer, !is_non_constant_init,
11964 /*asmspec=*/NULL_TREE,
11965 prefix_attributes);
11966 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11967 cp_parser_save_default_args (parser, decl);
11970 /* Finish processing the declaration. But, skip friend
11972 if (!friend_p && decl && decl != error_mark_node)
11974 cp_finish_decl (decl,
11975 initializer, !is_non_constant_init,
11977 /* If the initializer is in parentheses, then this is
11978 a direct-initialization, which means that an
11979 `explicit' constructor is OK. Otherwise, an
11980 `explicit' constructor cannot be used. */
11981 ((is_parenthesized_init || !is_initialized)
11982 ? 0 : LOOKUP_ONLYCONVERTING));
11984 else if ((cxx_dialect != cxx98) && friend_p
11985 && decl && TREE_CODE (decl) == FUNCTION_DECL)
11986 /* Core issue #226 (C++0x only): A default template-argument
11987 shall not be specified in a friend class template
11989 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
11990 /*is_partial=*/0, /*is_friend_decl=*/1);
11992 if (!friend_p && pushed_scope)
11993 pop_scope (pushed_scope);
11998 /* Parse a declarator.
12002 ptr-operator declarator
12004 abstract-declarator:
12005 ptr-operator abstract-declarator [opt]
12006 direct-abstract-declarator
12011 attributes [opt] direct-declarator
12012 attributes [opt] ptr-operator declarator
12014 abstract-declarator:
12015 attributes [opt] ptr-operator abstract-declarator [opt]
12016 attributes [opt] direct-abstract-declarator
12018 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12019 detect constructor, destructor or conversion operators. It is set
12020 to -1 if the declarator is a name, and +1 if it is a
12021 function. Otherwise it is set to zero. Usually you just want to
12022 test for >0, but internally the negative value is used.
12024 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12025 a decl-specifier-seq unless it declares a constructor, destructor,
12026 or conversion. It might seem that we could check this condition in
12027 semantic analysis, rather than parsing, but that makes it difficult
12028 to handle something like `f()'. We want to notice that there are
12029 no decl-specifiers, and therefore realize that this is an
12030 expression, not a declaration.)
12032 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12033 the declarator is a direct-declarator of the form "(...)".
12035 MEMBER_P is true iff this declarator is a member-declarator. */
12037 static cp_declarator *
12038 cp_parser_declarator (cp_parser* parser,
12039 cp_parser_declarator_kind dcl_kind,
12040 int* ctor_dtor_or_conv_p,
12041 bool* parenthesized_p,
12045 cp_declarator *declarator;
12046 enum tree_code code;
12047 cp_cv_quals cv_quals;
12049 tree attributes = NULL_TREE;
12051 /* Assume this is not a constructor, destructor, or type-conversion
12053 if (ctor_dtor_or_conv_p)
12054 *ctor_dtor_or_conv_p = 0;
12056 if (cp_parser_allow_gnu_extensions_p (parser))
12057 attributes = cp_parser_attributes_opt (parser);
12059 /* Peek at the next token. */
12060 token = cp_lexer_peek_token (parser->lexer);
12062 /* Check for the ptr-operator production. */
12063 cp_parser_parse_tentatively (parser);
12064 /* Parse the ptr-operator. */
12065 code = cp_parser_ptr_operator (parser,
12068 /* If that worked, then we have a ptr-operator. */
12069 if (cp_parser_parse_definitely (parser))
12071 /* If a ptr-operator was found, then this declarator was not
12073 if (parenthesized_p)
12074 *parenthesized_p = true;
12075 /* The dependent declarator is optional if we are parsing an
12076 abstract-declarator. */
12077 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12078 cp_parser_parse_tentatively (parser);
12080 /* Parse the dependent declarator. */
12081 declarator = cp_parser_declarator (parser, dcl_kind,
12082 /*ctor_dtor_or_conv_p=*/NULL,
12083 /*parenthesized_p=*/NULL,
12084 /*member_p=*/false);
12086 /* If we are parsing an abstract-declarator, we must handle the
12087 case where the dependent declarator is absent. */
12088 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12089 && !cp_parser_parse_definitely (parser))
12092 declarator = cp_parser_make_indirect_declarator
12093 (code, class_type, cv_quals, declarator);
12095 /* Everything else is a direct-declarator. */
12098 if (parenthesized_p)
12099 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12101 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12102 ctor_dtor_or_conv_p,
12106 if (attributes && declarator && declarator != cp_error_declarator)
12107 declarator->attributes = attributes;
12112 /* Parse a direct-declarator or direct-abstract-declarator.
12116 direct-declarator ( parameter-declaration-clause )
12117 cv-qualifier-seq [opt]
12118 exception-specification [opt]
12119 direct-declarator [ constant-expression [opt] ]
12122 direct-abstract-declarator:
12123 direct-abstract-declarator [opt]
12124 ( parameter-declaration-clause )
12125 cv-qualifier-seq [opt]
12126 exception-specification [opt]
12127 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12128 ( abstract-declarator )
12130 Returns a representation of the declarator. DCL_KIND is
12131 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12132 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12133 we are parsing a direct-declarator. It is
12134 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12135 of ambiguity we prefer an abstract declarator, as per
12136 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12137 cp_parser_declarator. */
12139 static cp_declarator *
12140 cp_parser_direct_declarator (cp_parser* parser,
12141 cp_parser_declarator_kind dcl_kind,
12142 int* ctor_dtor_or_conv_p,
12146 cp_declarator *declarator = NULL;
12147 tree scope = NULL_TREE;
12148 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12149 bool saved_in_declarator_p = parser->in_declarator_p;
12151 tree pushed_scope = NULL_TREE;
12155 /* Peek at the next token. */
12156 token = cp_lexer_peek_token (parser->lexer);
12157 if (token->type == CPP_OPEN_PAREN)
12159 /* This is either a parameter-declaration-clause, or a
12160 parenthesized declarator. When we know we are parsing a
12161 named declarator, it must be a parenthesized declarator
12162 if FIRST is true. For instance, `(int)' is a
12163 parameter-declaration-clause, with an omitted
12164 direct-abstract-declarator. But `((*))', is a
12165 parenthesized abstract declarator. Finally, when T is a
12166 template parameter `(T)' is a
12167 parameter-declaration-clause, and not a parenthesized
12170 We first try and parse a parameter-declaration-clause,
12171 and then try a nested declarator (if FIRST is true).
12173 It is not an error for it not to be a
12174 parameter-declaration-clause, even when FIRST is
12180 The first is the declaration of a function while the
12181 second is a the definition of a variable, including its
12184 Having seen only the parenthesis, we cannot know which of
12185 these two alternatives should be selected. Even more
12186 complex are examples like:
12191 The former is a function-declaration; the latter is a
12192 variable initialization.
12194 Thus again, we try a parameter-declaration-clause, and if
12195 that fails, we back out and return. */
12197 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12199 cp_parameter_declarator *params;
12200 unsigned saved_num_template_parameter_lists;
12202 /* In a member-declarator, the only valid interpretation
12203 of a parenthesis is the start of a
12204 parameter-declaration-clause. (It is invalid to
12205 initialize a static data member with a parenthesized
12206 initializer; only the "=" form of initialization is
12209 cp_parser_parse_tentatively (parser);
12211 /* Consume the `('. */
12212 cp_lexer_consume_token (parser->lexer);
12215 /* If this is going to be an abstract declarator, we're
12216 in a declarator and we can't have default args. */
12217 parser->default_arg_ok_p = false;
12218 parser->in_declarator_p = true;
12221 /* Inside the function parameter list, surrounding
12222 template-parameter-lists do not apply. */
12223 saved_num_template_parameter_lists
12224 = parser->num_template_parameter_lists;
12225 parser->num_template_parameter_lists = 0;
12227 /* Parse the parameter-declaration-clause. */
12228 params = cp_parser_parameter_declaration_clause (parser);
12230 parser->num_template_parameter_lists
12231 = saved_num_template_parameter_lists;
12233 /* If all went well, parse the cv-qualifier-seq and the
12234 exception-specification. */
12235 if (member_p || cp_parser_parse_definitely (parser))
12237 cp_cv_quals cv_quals;
12238 tree exception_specification;
12240 if (ctor_dtor_or_conv_p)
12241 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12243 /* Consume the `)'. */
12244 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12246 /* Parse the cv-qualifier-seq. */
12247 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12248 /* And the exception-specification. */
12249 exception_specification
12250 = cp_parser_exception_specification_opt (parser);
12252 /* Create the function-declarator. */
12253 declarator = make_call_declarator (declarator,
12256 exception_specification);
12257 /* Any subsequent parameter lists are to do with
12258 return type, so are not those of the declared
12260 parser->default_arg_ok_p = false;
12262 /* Repeat the main loop. */
12267 /* If this is the first, we can try a parenthesized
12271 bool saved_in_type_id_in_expr_p;
12273 parser->default_arg_ok_p = saved_default_arg_ok_p;
12274 parser->in_declarator_p = saved_in_declarator_p;
12276 /* Consume the `('. */
12277 cp_lexer_consume_token (parser->lexer);
12278 /* Parse the nested declarator. */
12279 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12280 parser->in_type_id_in_expr_p = true;
12282 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12283 /*parenthesized_p=*/NULL,
12285 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12287 /* Expect a `)'. */
12288 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12289 declarator = cp_error_declarator;
12290 if (declarator == cp_error_declarator)
12293 goto handle_declarator;
12295 /* Otherwise, we must be done. */
12299 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12300 && token->type == CPP_OPEN_SQUARE)
12302 /* Parse an array-declarator. */
12305 if (ctor_dtor_or_conv_p)
12306 *ctor_dtor_or_conv_p = 0;
12309 parser->default_arg_ok_p = false;
12310 parser->in_declarator_p = true;
12311 /* Consume the `['. */
12312 cp_lexer_consume_token (parser->lexer);
12313 /* Peek at the next token. */
12314 token = cp_lexer_peek_token (parser->lexer);
12315 /* If the next token is `]', then there is no
12316 constant-expression. */
12317 if (token->type != CPP_CLOSE_SQUARE)
12319 bool non_constant_p;
12322 = cp_parser_constant_expression (parser,
12323 /*allow_non_constant=*/true,
12325 if (!non_constant_p)
12326 bounds = fold_non_dependent_expr (bounds);
12327 /* Normally, the array bound must be an integral constant
12328 expression. However, as an extension, we allow VLAs
12329 in function scopes. */
12330 else if (!parser->in_function_body)
12332 error ("array bound is not an integer constant");
12333 bounds = error_mark_node;
12337 bounds = NULL_TREE;
12338 /* Look for the closing `]'. */
12339 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12341 declarator = cp_error_declarator;
12345 declarator = make_array_declarator (declarator, bounds);
12347 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12349 tree qualifying_scope;
12350 tree unqualified_name;
12351 special_function_kind sfk;
12353 bool pack_expansion_p = false;
12355 /* Parse a declarator-id */
12356 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12359 cp_parser_parse_tentatively (parser);
12361 /* If we see an ellipsis, we should be looking at a
12363 if (token->type == CPP_ELLIPSIS)
12365 /* Consume the `...' */
12366 cp_lexer_consume_token (parser->lexer);
12368 pack_expansion_p = true;
12373 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12374 qualifying_scope = parser->scope;
12379 if (!unqualified_name && pack_expansion_p)
12381 /* Check whether an error occurred. */
12382 okay = !cp_parser_error_occurred (parser);
12384 /* We already consumed the ellipsis to mark a
12385 parameter pack, but we have no way to report it,
12386 so abort the tentative parse. We will be exiting
12387 immediately anyway. */
12388 cp_parser_abort_tentative_parse (parser);
12391 okay = cp_parser_parse_definitely (parser);
12394 unqualified_name = error_mark_node;
12395 else if (unqualified_name
12396 && (qualifying_scope
12397 || (TREE_CODE (unqualified_name)
12398 != IDENTIFIER_NODE)))
12400 cp_parser_error (parser, "expected unqualified-id");
12401 unqualified_name = error_mark_node;
12405 if (!unqualified_name)
12407 if (unqualified_name == error_mark_node)
12409 declarator = cp_error_declarator;
12410 pack_expansion_p = false;
12411 declarator->parameter_pack_p = false;
12415 if (qualifying_scope && at_namespace_scope_p ()
12416 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12418 /* In the declaration of a member of a template class
12419 outside of the class itself, the SCOPE will sometimes
12420 be a TYPENAME_TYPE. For example, given:
12422 template <typename T>
12423 int S<T>::R::i = 3;
12425 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12426 this context, we must resolve S<T>::R to an ordinary
12427 type, rather than a typename type.
12429 The reason we normally avoid resolving TYPENAME_TYPEs
12430 is that a specialization of `S' might render
12431 `S<T>::R' not a type. However, if `S' is
12432 specialized, then this `i' will not be used, so there
12433 is no harm in resolving the types here. */
12436 /* Resolve the TYPENAME_TYPE. */
12437 type = resolve_typename_type (qualifying_scope,
12438 /*only_current_p=*/false);
12439 /* If that failed, the declarator is invalid. */
12440 if (type == error_mark_node)
12441 error ("%<%T::%E%> is not a type",
12442 TYPE_CONTEXT (qualifying_scope),
12443 TYPE_IDENTIFIER (qualifying_scope));
12444 qualifying_scope = type;
12449 if (unqualified_name)
12453 if (qualifying_scope
12454 && CLASS_TYPE_P (qualifying_scope))
12455 class_type = qualifying_scope;
12457 class_type = current_class_type;
12459 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12461 tree name_type = TREE_TYPE (unqualified_name);
12462 if (class_type && same_type_p (name_type, class_type))
12464 if (qualifying_scope
12465 && CLASSTYPE_USE_TEMPLATE (name_type))
12467 error ("invalid use of constructor as a template");
12468 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12469 "name the constructor in a qualified name",
12471 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12472 class_type, name_type);
12473 declarator = cp_error_declarator;
12477 unqualified_name = constructor_name (class_type);
12481 /* We do not attempt to print the declarator
12482 here because we do not have enough
12483 information about its original syntactic
12485 cp_parser_error (parser, "invalid declarator");
12486 declarator = cp_error_declarator;
12493 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12494 sfk = sfk_destructor;
12495 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12496 sfk = sfk_conversion;
12497 else if (/* There's no way to declare a constructor
12498 for an anonymous type, even if the type
12499 got a name for linkage purposes. */
12500 !TYPE_WAS_ANONYMOUS (class_type)
12501 && constructor_name_p (unqualified_name,
12504 unqualified_name = constructor_name (class_type);
12505 sfk = sfk_constructor;
12508 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12509 *ctor_dtor_or_conv_p = -1;
12512 declarator = make_id_declarator (qualifying_scope,
12515 declarator->id_loc = token->location;
12516 declarator->parameter_pack_p = pack_expansion_p;
12518 if (pack_expansion_p)
12519 maybe_warn_variadic_templates ();
12521 handle_declarator:;
12522 scope = get_scope_of_declarator (declarator);
12524 /* Any names that appear after the declarator-id for a
12525 member are looked up in the containing scope. */
12526 pushed_scope = push_scope (scope);
12527 parser->in_declarator_p = true;
12528 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12529 || (declarator && declarator->kind == cdk_id))
12530 /* Default args are only allowed on function
12532 parser->default_arg_ok_p = saved_default_arg_ok_p;
12534 parser->default_arg_ok_p = false;
12543 /* For an abstract declarator, we might wind up with nothing at this
12544 point. That's an error; the declarator is not optional. */
12546 cp_parser_error (parser, "expected declarator");
12548 /* If we entered a scope, we must exit it now. */
12550 pop_scope (pushed_scope);
12552 parser->default_arg_ok_p = saved_default_arg_ok_p;
12553 parser->in_declarator_p = saved_in_declarator_p;
12558 /* Parse a ptr-operator.
12561 * cv-qualifier-seq [opt]
12563 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12568 & cv-qualifier-seq [opt]
12570 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12571 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12572 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12573 filled in with the TYPE containing the member. *CV_QUALS is
12574 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12575 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12576 Note that the tree codes returned by this function have nothing
12577 to do with the types of trees that will be eventually be created
12578 to represent the pointer or reference type being parsed. They are
12579 just constants with suggestive names. */
12580 static enum tree_code
12581 cp_parser_ptr_operator (cp_parser* parser,
12583 cp_cv_quals *cv_quals)
12585 enum tree_code code = ERROR_MARK;
12588 /* Assume that it's not a pointer-to-member. */
12590 /* And that there are no cv-qualifiers. */
12591 *cv_quals = TYPE_UNQUALIFIED;
12593 /* Peek at the next token. */
12594 token = cp_lexer_peek_token (parser->lexer);
12596 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12597 if (token->type == CPP_MULT)
12598 code = INDIRECT_REF;
12599 else if (token->type == CPP_AND)
12601 else if ((cxx_dialect != cxx98) &&
12602 token->type == CPP_AND_AND) /* C++0x only */
12603 code = NON_LVALUE_EXPR;
12605 if (code != ERROR_MARK)
12607 /* Consume the `*', `&' or `&&'. */
12608 cp_lexer_consume_token (parser->lexer);
12610 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12611 `&', if we are allowing GNU extensions. (The only qualifier
12612 that can legally appear after `&' is `restrict', but that is
12613 enforced during semantic analysis. */
12614 if (code == INDIRECT_REF
12615 || cp_parser_allow_gnu_extensions_p (parser))
12616 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12620 /* Try the pointer-to-member case. */
12621 cp_parser_parse_tentatively (parser);
12622 /* Look for the optional `::' operator. */
12623 cp_parser_global_scope_opt (parser,
12624 /*current_scope_valid_p=*/false);
12625 /* Look for the nested-name specifier. */
12626 cp_parser_nested_name_specifier (parser,
12627 /*typename_keyword_p=*/false,
12628 /*check_dependency_p=*/true,
12630 /*is_declaration=*/false);
12631 /* If we found it, and the next token is a `*', then we are
12632 indeed looking at a pointer-to-member operator. */
12633 if (!cp_parser_error_occurred (parser)
12634 && cp_parser_require (parser, CPP_MULT, "`*'"))
12636 /* Indicate that the `*' operator was used. */
12637 code = INDIRECT_REF;
12639 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12640 error ("%qD is a namespace", parser->scope);
12643 /* The type of which the member is a member is given by the
12645 *type = parser->scope;
12646 /* The next name will not be qualified. */
12647 parser->scope = NULL_TREE;
12648 parser->qualifying_scope = NULL_TREE;
12649 parser->object_scope = NULL_TREE;
12650 /* Look for the optional cv-qualifier-seq. */
12651 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12654 /* If that didn't work we don't have a ptr-operator. */
12655 if (!cp_parser_parse_definitely (parser))
12656 cp_parser_error (parser, "expected ptr-operator");
12662 /* Parse an (optional) cv-qualifier-seq.
12665 cv-qualifier cv-qualifier-seq [opt]
12676 Returns a bitmask representing the cv-qualifiers. */
12679 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12681 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12686 cp_cv_quals cv_qualifier;
12688 /* Peek at the next token. */
12689 token = cp_lexer_peek_token (parser->lexer);
12690 /* See if it's a cv-qualifier. */
12691 switch (token->keyword)
12694 cv_qualifier = TYPE_QUAL_CONST;
12698 cv_qualifier = TYPE_QUAL_VOLATILE;
12702 cv_qualifier = TYPE_QUAL_RESTRICT;
12706 cv_qualifier = TYPE_UNQUALIFIED;
12713 if (cv_quals & cv_qualifier)
12715 error ("duplicate cv-qualifier");
12716 cp_lexer_purge_token (parser->lexer);
12720 cp_lexer_consume_token (parser->lexer);
12721 cv_quals |= cv_qualifier;
12728 /* Parse a declarator-id.
12732 :: [opt] nested-name-specifier [opt] type-name
12734 In the `id-expression' case, the value returned is as for
12735 cp_parser_id_expression if the id-expression was an unqualified-id.
12736 If the id-expression was a qualified-id, then a SCOPE_REF is
12737 returned. The first operand is the scope (either a NAMESPACE_DECL
12738 or TREE_TYPE), but the second is still just a representation of an
12742 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12745 /* The expression must be an id-expression. Assume that qualified
12746 names are the names of types so that:
12749 int S<T>::R::i = 3;
12751 will work; we must treat `S<T>::R' as the name of a type.
12752 Similarly, assume that qualified names are templates, where
12756 int S<T>::R<T>::i = 3;
12759 id = cp_parser_id_expression (parser,
12760 /*template_keyword_p=*/false,
12761 /*check_dependency_p=*/false,
12762 /*template_p=*/NULL,
12763 /*declarator_p=*/true,
12765 if (id && BASELINK_P (id))
12766 id = BASELINK_FUNCTIONS (id);
12770 /* Parse a type-id.
12773 type-specifier-seq abstract-declarator [opt]
12775 Returns the TYPE specified. */
12778 cp_parser_type_id (cp_parser* parser)
12780 cp_decl_specifier_seq type_specifier_seq;
12781 cp_declarator *abstract_declarator;
12783 /* Parse the type-specifier-seq. */
12784 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12785 &type_specifier_seq);
12786 if (type_specifier_seq.type == error_mark_node)
12787 return error_mark_node;
12789 /* There might or might not be an abstract declarator. */
12790 cp_parser_parse_tentatively (parser);
12791 /* Look for the declarator. */
12792 abstract_declarator
12793 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12794 /*parenthesized_p=*/NULL,
12795 /*member_p=*/false);
12796 /* Check to see if there really was a declarator. */
12797 if (!cp_parser_parse_definitely (parser))
12798 abstract_declarator = NULL;
12800 return groktypename (&type_specifier_seq, abstract_declarator);
12803 /* Parse a type-specifier-seq.
12805 type-specifier-seq:
12806 type-specifier type-specifier-seq [opt]
12810 type-specifier-seq:
12811 attributes type-specifier-seq [opt]
12813 If IS_CONDITION is true, we are at the start of a "condition",
12814 e.g., we've just seen "if (".
12816 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12819 cp_parser_type_specifier_seq (cp_parser* parser,
12821 cp_decl_specifier_seq *type_specifier_seq)
12823 bool seen_type_specifier = false;
12824 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12826 /* Clear the TYPE_SPECIFIER_SEQ. */
12827 clear_decl_specs (type_specifier_seq);
12829 /* Parse the type-specifiers and attributes. */
12832 tree type_specifier;
12833 bool is_cv_qualifier;
12835 /* Check for attributes first. */
12836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12838 type_specifier_seq->attributes =
12839 chainon (type_specifier_seq->attributes,
12840 cp_parser_attributes_opt (parser));
12844 /* Look for the type-specifier. */
12845 type_specifier = cp_parser_type_specifier (parser,
12847 type_specifier_seq,
12848 /*is_declaration=*/false,
12851 if (!type_specifier)
12853 /* If the first type-specifier could not be found, this is not a
12854 type-specifier-seq at all. */
12855 if (!seen_type_specifier)
12857 cp_parser_error (parser, "expected type-specifier");
12858 type_specifier_seq->type = error_mark_node;
12861 /* If subsequent type-specifiers could not be found, the
12862 type-specifier-seq is complete. */
12866 seen_type_specifier = true;
12867 /* The standard says that a condition can be:
12869 type-specifier-seq declarator = assignment-expression
12876 we should treat the "S" as a declarator, not as a
12877 type-specifier. The standard doesn't say that explicitly for
12878 type-specifier-seq, but it does say that for
12879 decl-specifier-seq in an ordinary declaration. Perhaps it
12880 would be clearer just to allow a decl-specifier-seq here, and
12881 then add a semantic restriction that if any decl-specifiers
12882 that are not type-specifiers appear, the program is invalid. */
12883 if (is_condition && !is_cv_qualifier)
12884 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12887 cp_parser_check_decl_spec (type_specifier_seq);
12890 /* Parse a parameter-declaration-clause.
12892 parameter-declaration-clause:
12893 parameter-declaration-list [opt] ... [opt]
12894 parameter-declaration-list , ...
12896 Returns a representation for the parameter declarations. A return
12897 value of NULL indicates a parameter-declaration-clause consisting
12898 only of an ellipsis. */
12900 static cp_parameter_declarator *
12901 cp_parser_parameter_declaration_clause (cp_parser* parser)
12903 cp_parameter_declarator *parameters;
12908 /* Peek at the next token. */
12909 token = cp_lexer_peek_token (parser->lexer);
12910 /* Check for trivial parameter-declaration-clauses. */
12911 if (token->type == CPP_ELLIPSIS)
12913 /* Consume the `...' token. */
12914 cp_lexer_consume_token (parser->lexer);
12917 else if (token->type == CPP_CLOSE_PAREN)
12918 /* There are no parameters. */
12920 #ifndef NO_IMPLICIT_EXTERN_C
12921 if (in_system_header && current_class_type == NULL
12922 && current_lang_name == lang_name_c)
12926 return no_parameters;
12928 /* Check for `(void)', too, which is a special case. */
12929 else if (token->keyword == RID_VOID
12930 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12931 == CPP_CLOSE_PAREN))
12933 /* Consume the `void' token. */
12934 cp_lexer_consume_token (parser->lexer);
12935 /* There are no parameters. */
12936 return no_parameters;
12939 /* Parse the parameter-declaration-list. */
12940 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12941 /* If a parse error occurred while parsing the
12942 parameter-declaration-list, then the entire
12943 parameter-declaration-clause is erroneous. */
12947 /* Peek at the next token. */
12948 token = cp_lexer_peek_token (parser->lexer);
12949 /* If it's a `,', the clause should terminate with an ellipsis. */
12950 if (token->type == CPP_COMMA)
12952 /* Consume the `,'. */
12953 cp_lexer_consume_token (parser->lexer);
12954 /* Expect an ellipsis. */
12956 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12958 /* It might also be `...' if the optional trailing `,' was
12960 else if (token->type == CPP_ELLIPSIS)
12962 /* Consume the `...' token. */
12963 cp_lexer_consume_token (parser->lexer);
12964 /* And remember that we saw it. */
12968 ellipsis_p = false;
12970 /* Finish the parameter list. */
12971 if (parameters && ellipsis_p)
12972 parameters->ellipsis_p = true;
12977 /* Parse a parameter-declaration-list.
12979 parameter-declaration-list:
12980 parameter-declaration
12981 parameter-declaration-list , parameter-declaration
12983 Returns a representation of the parameter-declaration-list, as for
12984 cp_parser_parameter_declaration_clause. However, the
12985 `void_list_node' is never appended to the list. Upon return,
12986 *IS_ERROR will be true iff an error occurred. */
12988 static cp_parameter_declarator *
12989 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
12991 cp_parameter_declarator *parameters = NULL;
12992 cp_parameter_declarator **tail = ¶meters;
12993 bool saved_in_unbraced_linkage_specification_p;
12995 /* Assume all will go well. */
12997 /* The special considerations that apply to a function within an
12998 unbraced linkage specifications do not apply to the parameters
12999 to the function. */
13000 saved_in_unbraced_linkage_specification_p
13001 = parser->in_unbraced_linkage_specification_p;
13002 parser->in_unbraced_linkage_specification_p = false;
13004 /* Look for more parameters. */
13007 cp_parameter_declarator *parameter;
13008 bool parenthesized_p;
13009 /* Parse the parameter. */
13011 = cp_parser_parameter_declaration (parser,
13012 /*template_parm_p=*/false,
13015 /* If a parse error occurred parsing the parameter declaration,
13016 then the entire parameter-declaration-list is erroneous. */
13023 /* Add the new parameter to the list. */
13025 tail = ¶meter->next;
13027 /* Peek at the next token. */
13028 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13029 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13030 /* These are for Objective-C++ */
13031 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13032 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13033 /* The parameter-declaration-list is complete. */
13035 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13039 /* Peek at the next token. */
13040 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13041 /* If it's an ellipsis, then the list is complete. */
13042 if (token->type == CPP_ELLIPSIS)
13044 /* Otherwise, there must be more parameters. Consume the
13046 cp_lexer_consume_token (parser->lexer);
13047 /* When parsing something like:
13049 int i(float f, double d)
13051 we can tell after seeing the declaration for "f" that we
13052 are not looking at an initialization of a variable "i",
13053 but rather at the declaration of a function "i".
13055 Due to the fact that the parsing of template arguments
13056 (as specified to a template-id) requires backtracking we
13057 cannot use this technique when inside a template argument
13059 if (!parser->in_template_argument_list_p
13060 && !parser->in_type_id_in_expr_p
13061 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13062 /* However, a parameter-declaration of the form
13063 "foat(f)" (which is a valid declaration of a
13064 parameter "f") can also be interpreted as an
13065 expression (the conversion of "f" to "float"). */
13066 && !parenthesized_p)
13067 cp_parser_commit_to_tentative_parse (parser);
13071 cp_parser_error (parser, "expected %<,%> or %<...%>");
13072 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13073 cp_parser_skip_to_closing_parenthesis (parser,
13074 /*recovering=*/true,
13075 /*or_comma=*/false,
13076 /*consume_paren=*/false);
13081 parser->in_unbraced_linkage_specification_p
13082 = saved_in_unbraced_linkage_specification_p;
13087 /* Parse a parameter declaration.
13089 parameter-declaration:
13090 decl-specifier-seq ... [opt] declarator
13091 decl-specifier-seq declarator = assignment-expression
13092 decl-specifier-seq ... [opt] abstract-declarator [opt]
13093 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13095 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13096 declares a template parameter. (In that case, a non-nested `>'
13097 token encountered during the parsing of the assignment-expression
13098 is not interpreted as a greater-than operator.)
13100 Returns a representation of the parameter, or NULL if an error
13101 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13102 true iff the declarator is of the form "(p)". */
13104 static cp_parameter_declarator *
13105 cp_parser_parameter_declaration (cp_parser *parser,
13106 bool template_parm_p,
13107 bool *parenthesized_p)
13109 int declares_class_or_enum;
13110 bool greater_than_is_operator_p;
13111 cp_decl_specifier_seq decl_specifiers;
13112 cp_declarator *declarator;
13113 tree default_argument;
13115 const char *saved_message;
13117 /* In a template parameter, `>' is not an operator.
13121 When parsing a default template-argument for a non-type
13122 template-parameter, the first non-nested `>' is taken as the end
13123 of the template parameter-list rather than a greater-than
13125 greater_than_is_operator_p = !template_parm_p;
13127 /* Type definitions may not appear in parameter types. */
13128 saved_message = parser->type_definition_forbidden_message;
13129 parser->type_definition_forbidden_message
13130 = "types may not be defined in parameter types";
13132 /* Parse the declaration-specifiers. */
13133 cp_parser_decl_specifier_seq (parser,
13134 CP_PARSER_FLAGS_NONE,
13136 &declares_class_or_enum);
13137 /* If an error occurred, there's no reason to attempt to parse the
13138 rest of the declaration. */
13139 if (cp_parser_error_occurred (parser))
13141 parser->type_definition_forbidden_message = saved_message;
13145 /* Peek at the next token. */
13146 token = cp_lexer_peek_token (parser->lexer);
13148 /* If the next token is a `)', `,', `=', `>', or `...', then there
13149 is no declarator. However, when variadic templates are enabled,
13150 there may be a declarator following `...'. */
13151 if (token->type == CPP_CLOSE_PAREN
13152 || token->type == CPP_COMMA
13153 || token->type == CPP_EQ
13154 || token->type == CPP_GREATER)
13157 if (parenthesized_p)
13158 *parenthesized_p = false;
13160 /* Otherwise, there should be a declarator. */
13163 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13164 parser->default_arg_ok_p = false;
13166 /* After seeing a decl-specifier-seq, if the next token is not a
13167 "(", there is no possibility that the code is a valid
13168 expression. Therefore, if parsing tentatively, we commit at
13170 if (!parser->in_template_argument_list_p
13171 /* In an expression context, having seen:
13175 we cannot be sure whether we are looking at a
13176 function-type (taking a "char" as a parameter) or a cast
13177 of some object of type "char" to "int". */
13178 && !parser->in_type_id_in_expr_p
13179 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13180 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13181 cp_parser_commit_to_tentative_parse (parser);
13182 /* Parse the declarator. */
13183 declarator = cp_parser_declarator (parser,
13184 CP_PARSER_DECLARATOR_EITHER,
13185 /*ctor_dtor_or_conv_p=*/NULL,
13187 /*member_p=*/false);
13188 parser->default_arg_ok_p = saved_default_arg_ok_p;
13189 /* After the declarator, allow more attributes. */
13190 decl_specifiers.attributes
13191 = chainon (decl_specifiers.attributes,
13192 cp_parser_attributes_opt (parser));
13195 /* If the next token is an ellipsis, and we have not seen a
13196 declarator name, and the type of the declarator contains parameter
13197 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13198 a parameter pack expansion expression. Otherwise, leave the
13199 ellipsis for a C-style variadic function. */
13200 token = cp_lexer_peek_token (parser->lexer);
13201 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13203 tree type = decl_specifiers.type;
13205 if (type && DECL_P (type))
13206 type = TREE_TYPE (type);
13209 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13210 && declarator_can_be_parameter_pack (declarator)
13211 && (!declarator || !declarator->parameter_pack_p)
13212 && uses_parameter_packs (type))
13214 /* Consume the `...'. */
13215 cp_lexer_consume_token (parser->lexer);
13216 maybe_warn_variadic_templates ();
13218 /* Build a pack expansion type */
13220 declarator->parameter_pack_p = true;
13222 decl_specifiers.type = make_pack_expansion (type);
13226 /* The restriction on defining new types applies only to the type
13227 of the parameter, not to the default argument. */
13228 parser->type_definition_forbidden_message = saved_message;
13230 /* If the next token is `=', then process a default argument. */
13231 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13233 bool saved_greater_than_is_operator_p;
13234 /* Consume the `='. */
13235 cp_lexer_consume_token (parser->lexer);
13237 /* If we are defining a class, then the tokens that make up the
13238 default argument must be saved and processed later. */
13239 if (!template_parm_p && at_class_scope_p ()
13240 && TYPE_BEING_DEFINED (current_class_type))
13242 unsigned depth = 0;
13243 cp_token *first_token;
13246 /* Add tokens until we have processed the entire default
13247 argument. We add the range [first_token, token). */
13248 first_token = cp_lexer_peek_token (parser->lexer);
13253 /* Peek at the next token. */
13254 token = cp_lexer_peek_token (parser->lexer);
13255 /* What we do depends on what token we have. */
13256 switch (token->type)
13258 /* In valid code, a default argument must be
13259 immediately followed by a `,' `)', or `...'. */
13261 case CPP_CLOSE_PAREN:
13263 /* If we run into a non-nested `;', `}', or `]',
13264 then the code is invalid -- but the default
13265 argument is certainly over. */
13266 case CPP_SEMICOLON:
13267 case CPP_CLOSE_BRACE:
13268 case CPP_CLOSE_SQUARE:
13271 /* Update DEPTH, if necessary. */
13272 else if (token->type == CPP_CLOSE_PAREN
13273 || token->type == CPP_CLOSE_BRACE
13274 || token->type == CPP_CLOSE_SQUARE)
13278 case CPP_OPEN_PAREN:
13279 case CPP_OPEN_SQUARE:
13280 case CPP_OPEN_BRACE:
13285 if (cxx_dialect == cxx98)
13287 /* Fall through for C++0x, which treats the `>>'
13288 operator like two `>' tokens in certain
13292 /* If we see a non-nested `>', and `>' is not an
13293 operator, then it marks the end of the default
13295 if (!depth && !greater_than_is_operator_p)
13299 /* If we run out of tokens, issue an error message. */
13301 case CPP_PRAGMA_EOL:
13302 error ("file ends in default argument");
13308 /* In these cases, we should look for template-ids.
13309 For example, if the default argument is
13310 `X<int, double>()', we need to do name lookup to
13311 figure out whether or not `X' is a template; if
13312 so, the `,' does not end the default argument.
13314 That is not yet done. */
13321 /* If we've reached the end, stop. */
13325 /* Add the token to the token block. */
13326 token = cp_lexer_consume_token (parser->lexer);
13329 /* Create a DEFAULT_ARG to represented the unparsed default
13331 default_argument = make_node (DEFAULT_ARG);
13332 DEFARG_TOKENS (default_argument)
13333 = cp_token_cache_new (first_token, token);
13334 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13336 /* Outside of a class definition, we can just parse the
13337 assignment-expression. */
13340 bool saved_local_variables_forbidden_p;
13342 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13344 saved_greater_than_is_operator_p
13345 = parser->greater_than_is_operator_p;
13346 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13347 /* Local variable names (and the `this' keyword) may not
13348 appear in a default argument. */
13349 saved_local_variables_forbidden_p
13350 = parser->local_variables_forbidden_p;
13351 parser->local_variables_forbidden_p = true;
13352 /* The default argument expression may cause implicitly
13353 defined member functions to be synthesized, which will
13354 result in garbage collection. We must treat this
13355 situation as if we were within the body of function so as
13356 to avoid collecting live data on the stack. */
13358 /* Parse the assignment-expression. */
13359 if (template_parm_p)
13360 push_deferring_access_checks (dk_no_deferred);
13362 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13363 if (template_parm_p)
13364 pop_deferring_access_checks ();
13365 /* Restore saved state. */
13367 parser->greater_than_is_operator_p
13368 = saved_greater_than_is_operator_p;
13369 parser->local_variables_forbidden_p
13370 = saved_local_variables_forbidden_p;
13372 if (!parser->default_arg_ok_p)
13374 if (!flag_pedantic_errors)
13375 warning (0, "deprecated use of default argument for parameter of non-function");
13378 error ("default arguments are only permitted for function parameters");
13379 default_argument = NULL_TREE;
13384 default_argument = NULL_TREE;
13386 return make_parameter_declarator (&decl_specifiers,
13391 /* Parse a function-body.
13394 compound_statement */
13397 cp_parser_function_body (cp_parser *parser)
13399 cp_parser_compound_statement (parser, NULL, false);
13402 /* Parse a ctor-initializer-opt followed by a function-body. Return
13403 true if a ctor-initializer was present. */
13406 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13409 bool ctor_initializer_p;
13411 /* Begin the function body. */
13412 body = begin_function_body ();
13413 /* Parse the optional ctor-initializer. */
13414 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13415 /* Parse the function-body. */
13416 cp_parser_function_body (parser);
13417 /* Finish the function body. */
13418 finish_function_body (body);
13420 return ctor_initializer_p;
13423 /* Parse an initializer.
13426 = initializer-clause
13427 ( expression-list )
13429 Returns an expression representing the initializer. If no
13430 initializer is present, NULL_TREE is returned.
13432 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13433 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13434 set to FALSE if there is no initializer present. If there is an
13435 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13436 is set to true; otherwise it is set to false. */
13439 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13440 bool* non_constant_p)
13445 /* Peek at the next token. */
13446 token = cp_lexer_peek_token (parser->lexer);
13448 /* Let our caller know whether or not this initializer was
13450 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13451 /* Assume that the initializer is constant. */
13452 *non_constant_p = false;
13454 if (token->type == CPP_EQ)
13456 /* Consume the `='. */
13457 cp_lexer_consume_token (parser->lexer);
13458 /* Parse the initializer-clause. */
13459 init = cp_parser_initializer_clause (parser, non_constant_p);
13461 else if (token->type == CPP_OPEN_PAREN)
13462 init = cp_parser_parenthesized_expression_list (parser, false,
13464 /*allow_expansion_p=*/true,
13468 /* Anything else is an error. */
13469 cp_parser_error (parser, "expected initializer");
13470 init = error_mark_node;
13476 /* Parse an initializer-clause.
13478 initializer-clause:
13479 assignment-expression
13480 { initializer-list , [opt] }
13483 Returns an expression representing the initializer.
13485 If the `assignment-expression' production is used the value
13486 returned is simply a representation for the expression.
13488 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13489 the elements of the initializer-list (or NULL, if the last
13490 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13491 NULL_TREE. There is no way to detect whether or not the optional
13492 trailing `,' was provided. NON_CONSTANT_P is as for
13493 cp_parser_initializer. */
13496 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13500 /* Assume the expression is constant. */
13501 *non_constant_p = false;
13503 /* If it is not a `{', then we are looking at an
13504 assignment-expression. */
13505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13508 = cp_parser_constant_expression (parser,
13509 /*allow_non_constant_p=*/true,
13511 if (!*non_constant_p)
13512 initializer = fold_non_dependent_expr (initializer);
13516 /* Consume the `{' token. */
13517 cp_lexer_consume_token (parser->lexer);
13518 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13519 initializer = make_node (CONSTRUCTOR);
13520 /* If it's not a `}', then there is a non-trivial initializer. */
13521 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13523 /* Parse the initializer list. */
13524 CONSTRUCTOR_ELTS (initializer)
13525 = cp_parser_initializer_list (parser, non_constant_p);
13526 /* A trailing `,' token is allowed. */
13527 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13528 cp_lexer_consume_token (parser->lexer);
13530 /* Now, there should be a trailing `}'. */
13531 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13534 return initializer;
13537 /* Parse an initializer-list.
13540 initializer-clause ... [opt]
13541 initializer-list , initializer-clause ... [opt]
13546 identifier : initializer-clause
13547 initializer-list, identifier : initializer-clause
13549 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13550 for the initializer. If the INDEX of the elt is non-NULL, it is the
13551 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13552 as for cp_parser_initializer. */
13554 static VEC(constructor_elt,gc) *
13555 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13557 VEC(constructor_elt,gc) *v = NULL;
13559 /* Assume all of the expressions are constant. */
13560 *non_constant_p = false;
13562 /* Parse the rest of the list. */
13568 bool clause_non_constant_p;
13570 /* If the next token is an identifier and the following one is a
13571 colon, we are looking at the GNU designated-initializer
13573 if (cp_parser_allow_gnu_extensions_p (parser)
13574 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13575 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13577 /* Warn the user that they are using an extension. */
13579 pedwarn ("ISO C++ does not allow designated initializers");
13580 /* Consume the identifier. */
13581 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13582 /* Consume the `:'. */
13583 cp_lexer_consume_token (parser->lexer);
13586 identifier = NULL_TREE;
13588 /* Parse the initializer. */
13589 initializer = cp_parser_initializer_clause (parser,
13590 &clause_non_constant_p);
13591 /* If any clause is non-constant, so is the entire initializer. */
13592 if (clause_non_constant_p)
13593 *non_constant_p = true;
13595 /* If we have an ellipsis, this is an initializer pack
13597 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13599 /* Consume the `...'. */
13600 cp_lexer_consume_token (parser->lexer);
13602 /* Turn the initializer into an initializer expansion. */
13603 initializer = make_pack_expansion (initializer);
13606 /* Add it to the vector. */
13607 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13609 /* If the next token is not a comma, we have reached the end of
13611 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13614 /* Peek at the next token. */
13615 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13616 /* If the next token is a `}', then we're still done. An
13617 initializer-clause can have a trailing `,' after the
13618 initializer-list and before the closing `}'. */
13619 if (token->type == CPP_CLOSE_BRACE)
13622 /* Consume the `,' token. */
13623 cp_lexer_consume_token (parser->lexer);
13629 /* Classes [gram.class] */
13631 /* Parse a class-name.
13637 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13638 to indicate that names looked up in dependent types should be
13639 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13640 keyword has been used to indicate that the name that appears next
13641 is a template. TAG_TYPE indicates the explicit tag given before
13642 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13643 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13644 is the class being defined in a class-head.
13646 Returns the TYPE_DECL representing the class. */
13649 cp_parser_class_name (cp_parser *parser,
13650 bool typename_keyword_p,
13651 bool template_keyword_p,
13652 enum tag_types tag_type,
13653 bool check_dependency_p,
13655 bool is_declaration)
13662 /* All class-names start with an identifier. */
13663 token = cp_lexer_peek_token (parser->lexer);
13664 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13666 cp_parser_error (parser, "expected class-name");
13667 return error_mark_node;
13670 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13671 to a template-id, so we save it here. */
13672 scope = parser->scope;
13673 if (scope == error_mark_node)
13674 return error_mark_node;
13676 /* Any name names a type if we're following the `typename' keyword
13677 in a qualified name where the enclosing scope is type-dependent. */
13678 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13679 && dependent_type_p (scope));
13680 /* Handle the common case (an identifier, but not a template-id)
13682 if (token->type == CPP_NAME
13683 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13685 cp_token *identifier_token;
13689 /* Look for the identifier. */
13690 identifier_token = cp_lexer_peek_token (parser->lexer);
13691 ambiguous_p = identifier_token->ambiguous_p;
13692 identifier = cp_parser_identifier (parser);
13693 /* If the next token isn't an identifier, we are certainly not
13694 looking at a class-name. */
13695 if (identifier == error_mark_node)
13696 decl = error_mark_node;
13697 /* If we know this is a type-name, there's no need to look it
13699 else if (typename_p)
13703 tree ambiguous_decls;
13704 /* If we already know that this lookup is ambiguous, then
13705 we've already issued an error message; there's no reason
13709 cp_parser_simulate_error (parser);
13710 return error_mark_node;
13712 /* If the next token is a `::', then the name must be a type
13715 [basic.lookup.qual]
13717 During the lookup for a name preceding the :: scope
13718 resolution operator, object, function, and enumerator
13719 names are ignored. */
13720 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13721 tag_type = typename_type;
13722 /* Look up the name. */
13723 decl = cp_parser_lookup_name (parser, identifier,
13725 /*is_template=*/false,
13726 /*is_namespace=*/false,
13727 check_dependency_p,
13729 if (ambiguous_decls)
13731 error ("reference to %qD is ambiguous", identifier);
13732 print_candidates (ambiguous_decls);
13733 if (cp_parser_parsing_tentatively (parser))
13735 identifier_token->ambiguous_p = true;
13736 cp_parser_simulate_error (parser);
13738 return error_mark_node;
13744 /* Try a template-id. */
13745 decl = cp_parser_template_id (parser, template_keyword_p,
13746 check_dependency_p,
13748 if (decl == error_mark_node)
13749 return error_mark_node;
13752 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13754 /* If this is a typename, create a TYPENAME_TYPE. */
13755 if (typename_p && decl != error_mark_node)
13757 decl = make_typename_type (scope, decl, typename_type,
13758 /*complain=*/tf_error);
13759 if (decl != error_mark_node)
13760 decl = TYPE_NAME (decl);
13763 /* Check to see that it is really the name of a class. */
13764 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13765 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13766 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13767 /* Situations like this:
13769 template <typename T> struct A {
13770 typename T::template X<int>::I i;
13773 are problematic. Is `T::template X<int>' a class-name? The
13774 standard does not seem to be definitive, but there is no other
13775 valid interpretation of the following `::'. Therefore, those
13776 names are considered class-names. */
13778 decl = make_typename_type (scope, decl, tag_type, tf_error);
13779 if (decl != error_mark_node)
13780 decl = TYPE_NAME (decl);
13782 else if (TREE_CODE (decl) != TYPE_DECL
13783 || TREE_TYPE (decl) == error_mark_node
13784 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13785 decl = error_mark_node;
13787 if (decl == error_mark_node)
13788 cp_parser_error (parser, "expected class-name");
13793 /* Parse a class-specifier.
13796 class-head { member-specification [opt] }
13798 Returns the TREE_TYPE representing the class. */
13801 cp_parser_class_specifier (cp_parser* parser)
13805 tree attributes = NULL_TREE;
13806 int has_trailing_semicolon;
13807 bool nested_name_specifier_p;
13808 unsigned saved_num_template_parameter_lists;
13809 bool saved_in_function_body;
13810 tree old_scope = NULL_TREE;
13811 tree scope = NULL_TREE;
13814 push_deferring_access_checks (dk_no_deferred);
13816 /* Parse the class-head. */
13817 type = cp_parser_class_head (parser,
13818 &nested_name_specifier_p,
13821 /* If the class-head was a semantic disaster, skip the entire body
13825 cp_parser_skip_to_end_of_block_or_statement (parser);
13826 pop_deferring_access_checks ();
13827 return error_mark_node;
13830 /* Look for the `{'. */
13831 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13833 pop_deferring_access_checks ();
13834 return error_mark_node;
13837 /* Process the base classes. If they're invalid, skip the
13838 entire class body. */
13839 if (!xref_basetypes (type, bases))
13841 /* Consuming the closing brace yields better error messages
13843 if (cp_parser_skip_to_closing_brace (parser))
13844 cp_lexer_consume_token (parser->lexer);
13845 pop_deferring_access_checks ();
13846 return error_mark_node;
13849 /* Issue an error message if type-definitions are forbidden here. */
13850 cp_parser_check_type_definition (parser);
13851 /* Remember that we are defining one more class. */
13852 ++parser->num_classes_being_defined;
13853 /* Inside the class, surrounding template-parameter-lists do not
13855 saved_num_template_parameter_lists
13856 = parser->num_template_parameter_lists;
13857 parser->num_template_parameter_lists = 0;
13858 /* We are not in a function body. */
13859 saved_in_function_body = parser->in_function_body;
13860 parser->in_function_body = false;
13862 /* Start the class. */
13863 if (nested_name_specifier_p)
13865 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13866 old_scope = push_inner_scope (scope);
13868 type = begin_class_definition (type, attributes);
13870 if (type == error_mark_node)
13871 /* If the type is erroneous, skip the entire body of the class. */
13872 cp_parser_skip_to_closing_brace (parser);
13874 /* Parse the member-specification. */
13875 cp_parser_member_specification_opt (parser);
13877 /* Look for the trailing `}'. */
13878 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13879 /* We get better error messages by noticing a common problem: a
13880 missing trailing `;'. */
13881 token = cp_lexer_peek_token (parser->lexer);
13882 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13883 /* Look for trailing attributes to apply to this class. */
13884 if (cp_parser_allow_gnu_extensions_p (parser))
13885 attributes = cp_parser_attributes_opt (parser);
13886 if (type != error_mark_node)
13887 type = finish_struct (type, attributes);
13888 if (nested_name_specifier_p)
13889 pop_inner_scope (old_scope, scope);
13890 /* If this class is not itself within the scope of another class,
13891 then we need to parse the bodies of all of the queued function
13892 definitions. Note that the queued functions defined in a class
13893 are not always processed immediately following the
13894 class-specifier for that class. Consider:
13897 struct B { void f() { sizeof (A); } };
13900 If `f' were processed before the processing of `A' were
13901 completed, there would be no way to compute the size of `A'.
13902 Note that the nesting we are interested in here is lexical --
13903 not the semantic nesting given by TYPE_CONTEXT. In particular,
13906 struct A { struct B; };
13907 struct A::B { void f() { } };
13909 there is no need to delay the parsing of `A::B::f'. */
13910 if (--parser->num_classes_being_defined == 0)
13914 tree class_type = NULL_TREE;
13915 tree pushed_scope = NULL_TREE;
13917 /* In a first pass, parse default arguments to the functions.
13918 Then, in a second pass, parse the bodies of the functions.
13919 This two-phased approach handles cases like:
13927 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13928 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13929 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13930 TREE_PURPOSE (parser->unparsed_functions_queues)
13931 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13933 fn = TREE_VALUE (queue_entry);
13934 /* If there are default arguments that have not yet been processed,
13935 take care of them now. */
13936 if (class_type != TREE_PURPOSE (queue_entry))
13939 pop_scope (pushed_scope);
13940 class_type = TREE_PURPOSE (queue_entry);
13941 pushed_scope = push_scope (class_type);
13943 /* Make sure that any template parameters are in scope. */
13944 maybe_begin_member_template_processing (fn);
13945 /* Parse the default argument expressions. */
13946 cp_parser_late_parsing_default_args (parser, fn);
13947 /* Remove any template parameters from the symbol table. */
13948 maybe_end_member_template_processing ();
13951 pop_scope (pushed_scope);
13952 /* Now parse the body of the functions. */
13953 for (TREE_VALUE (parser->unparsed_functions_queues)
13954 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13955 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13956 TREE_VALUE (parser->unparsed_functions_queues)
13957 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13959 /* Figure out which function we need to process. */
13960 fn = TREE_VALUE (queue_entry);
13961 /* Parse the function. */
13962 cp_parser_late_parsing_for_member (parser, fn);
13966 /* Put back any saved access checks. */
13967 pop_deferring_access_checks ();
13969 /* Restore saved state. */
13970 parser->in_function_body = saved_in_function_body;
13971 parser->num_template_parameter_lists
13972 = saved_num_template_parameter_lists;
13977 /* Parse a class-head.
13980 class-key identifier [opt] base-clause [opt]
13981 class-key nested-name-specifier identifier base-clause [opt]
13982 class-key nested-name-specifier [opt] template-id
13986 class-key attributes identifier [opt] base-clause [opt]
13987 class-key attributes nested-name-specifier identifier base-clause [opt]
13988 class-key attributes nested-name-specifier [opt] template-id
13991 Upon return BASES is initialized to the list of base classes (or
13992 NULL, if there are none) in the same form returned by
13993 cp_parser_base_clause.
13995 Returns the TYPE of the indicated class. Sets
13996 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
13997 involving a nested-name-specifier was used, and FALSE otherwise.
13999 Returns error_mark_node if this is not a class-head.
14001 Returns NULL_TREE if the class-head is syntactically valid, but
14002 semantically invalid in a way that means we should skip the entire
14003 body of the class. */
14006 cp_parser_class_head (cp_parser* parser,
14007 bool* nested_name_specifier_p,
14008 tree *attributes_p,
14011 tree nested_name_specifier;
14012 enum tag_types class_key;
14013 tree id = NULL_TREE;
14014 tree type = NULL_TREE;
14016 bool template_id_p = false;
14017 bool qualified_p = false;
14018 bool invalid_nested_name_p = false;
14019 bool invalid_explicit_specialization_p = false;
14020 tree pushed_scope = NULL_TREE;
14021 unsigned num_templates;
14023 /* Assume no nested-name-specifier will be present. */
14024 *nested_name_specifier_p = false;
14025 /* Assume no template parameter lists will be used in defining the
14029 *bases = NULL_TREE;
14031 /* Look for the class-key. */
14032 class_key = cp_parser_class_key (parser);
14033 if (class_key == none_type)
14034 return error_mark_node;
14036 /* Parse the attributes. */
14037 attributes = cp_parser_attributes_opt (parser);
14039 /* If the next token is `::', that is invalid -- but sometimes
14040 people do try to write:
14044 Handle this gracefully by accepting the extra qualifier, and then
14045 issuing an error about it later if this really is a
14046 class-head. If it turns out just to be an elaborated type
14047 specifier, remain silent. */
14048 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14049 qualified_p = true;
14051 push_deferring_access_checks (dk_no_check);
14053 /* Determine the name of the class. Begin by looking for an
14054 optional nested-name-specifier. */
14055 nested_name_specifier
14056 = cp_parser_nested_name_specifier_opt (parser,
14057 /*typename_keyword_p=*/false,
14058 /*check_dependency_p=*/false,
14060 /*is_declaration=*/false);
14061 /* If there was a nested-name-specifier, then there *must* be an
14063 if (nested_name_specifier)
14065 /* Although the grammar says `identifier', it really means
14066 `class-name' or `template-name'. You are only allowed to
14067 define a class that has already been declared with this
14070 The proposed resolution for Core Issue 180 says that wherever
14071 you see `class T::X' you should treat `X' as a type-name.
14073 It is OK to define an inaccessible class; for example:
14075 class A { class B; };
14078 We do not know if we will see a class-name, or a
14079 template-name. We look for a class-name first, in case the
14080 class-name is a template-id; if we looked for the
14081 template-name first we would stop after the template-name. */
14082 cp_parser_parse_tentatively (parser);
14083 type = cp_parser_class_name (parser,
14084 /*typename_keyword_p=*/false,
14085 /*template_keyword_p=*/false,
14087 /*check_dependency_p=*/false,
14088 /*class_head_p=*/true,
14089 /*is_declaration=*/false);
14090 /* If that didn't work, ignore the nested-name-specifier. */
14091 if (!cp_parser_parse_definitely (parser))
14093 invalid_nested_name_p = true;
14094 id = cp_parser_identifier (parser);
14095 if (id == error_mark_node)
14098 /* If we could not find a corresponding TYPE, treat this
14099 declaration like an unqualified declaration. */
14100 if (type == error_mark_node)
14101 nested_name_specifier = NULL_TREE;
14102 /* Otherwise, count the number of templates used in TYPE and its
14103 containing scopes. */
14108 for (scope = TREE_TYPE (type);
14109 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14110 scope = (TYPE_P (scope)
14111 ? TYPE_CONTEXT (scope)
14112 : DECL_CONTEXT (scope)))
14114 && CLASS_TYPE_P (scope)
14115 && CLASSTYPE_TEMPLATE_INFO (scope)
14116 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14117 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14121 /* Otherwise, the identifier is optional. */
14124 /* We don't know whether what comes next is a template-id,
14125 an identifier, or nothing at all. */
14126 cp_parser_parse_tentatively (parser);
14127 /* Check for a template-id. */
14128 id = cp_parser_template_id (parser,
14129 /*template_keyword_p=*/false,
14130 /*check_dependency_p=*/true,
14131 /*is_declaration=*/true);
14132 /* If that didn't work, it could still be an identifier. */
14133 if (!cp_parser_parse_definitely (parser))
14135 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14136 id = cp_parser_identifier (parser);
14142 template_id_p = true;
14147 pop_deferring_access_checks ();
14150 cp_parser_check_for_invalid_template_id (parser, id);
14152 /* If it's not a `:' or a `{' then we can't really be looking at a
14153 class-head, since a class-head only appears as part of a
14154 class-specifier. We have to detect this situation before calling
14155 xref_tag, since that has irreversible side-effects. */
14156 if (!cp_parser_next_token_starts_class_definition_p (parser))
14158 cp_parser_error (parser, "expected %<{%> or %<:%>");
14159 return error_mark_node;
14162 /* At this point, we're going ahead with the class-specifier, even
14163 if some other problem occurs. */
14164 cp_parser_commit_to_tentative_parse (parser);
14165 /* Issue the error about the overly-qualified name now. */
14167 cp_parser_error (parser,
14168 "global qualification of class name is invalid");
14169 else if (invalid_nested_name_p)
14170 cp_parser_error (parser,
14171 "qualified name does not name a class");
14172 else if (nested_name_specifier)
14176 /* Reject typedef-names in class heads. */
14177 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14179 error ("invalid class name in declaration of %qD", type);
14184 /* Figure out in what scope the declaration is being placed. */
14185 scope = current_scope ();
14186 /* If that scope does not contain the scope in which the
14187 class was originally declared, the program is invalid. */
14188 if (scope && !is_ancestor (scope, nested_name_specifier))
14190 error ("declaration of %qD in %qD which does not enclose %qD",
14191 type, scope, nested_name_specifier);
14197 A declarator-id shall not be qualified exception of the
14198 definition of a ... nested class outside of its class
14199 ... [or] a the definition or explicit instantiation of a
14200 class member of a namespace outside of its namespace. */
14201 if (scope == nested_name_specifier)
14203 pedwarn ("extra qualification ignored");
14204 nested_name_specifier = NULL_TREE;
14208 /* An explicit-specialization must be preceded by "template <>". If
14209 it is not, try to recover gracefully. */
14210 if (at_namespace_scope_p ()
14211 && parser->num_template_parameter_lists == 0
14214 error ("an explicit specialization must be preceded by %<template <>%>");
14215 invalid_explicit_specialization_p = true;
14216 /* Take the same action that would have been taken by
14217 cp_parser_explicit_specialization. */
14218 ++parser->num_template_parameter_lists;
14219 begin_specialization ();
14221 /* There must be no "return" statements between this point and the
14222 end of this function; set "type "to the correct return value and
14223 use "goto done;" to return. */
14224 /* Make sure that the right number of template parameters were
14226 if (!cp_parser_check_template_parameters (parser, num_templates))
14228 /* If something went wrong, there is no point in even trying to
14229 process the class-definition. */
14234 /* Look up the type. */
14237 type = TREE_TYPE (id);
14238 type = maybe_process_partial_specialization (type);
14239 if (nested_name_specifier)
14240 pushed_scope = push_scope (nested_name_specifier);
14242 else if (nested_name_specifier)
14248 template <typename T> struct S { struct T };
14249 template <typename T> struct S<T>::T { };
14251 we will get a TYPENAME_TYPE when processing the definition of
14252 `S::T'. We need to resolve it to the actual type before we
14253 try to define it. */
14254 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14256 class_type = resolve_typename_type (TREE_TYPE (type),
14257 /*only_current_p=*/false);
14258 if (class_type != error_mark_node)
14259 type = TYPE_NAME (class_type);
14262 cp_parser_error (parser, "could not resolve typename type");
14263 type = error_mark_node;
14267 maybe_process_partial_specialization (TREE_TYPE (type));
14268 class_type = current_class_type;
14269 /* Enter the scope indicated by the nested-name-specifier. */
14270 pushed_scope = push_scope (nested_name_specifier);
14271 /* Get the canonical version of this type. */
14272 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14273 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14274 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14276 type = push_template_decl (type);
14277 if (type == error_mark_node)
14284 type = TREE_TYPE (type);
14285 *nested_name_specifier_p = true;
14287 else /* The name is not a nested name. */
14289 /* If the class was unnamed, create a dummy name. */
14291 id = make_anon_name ();
14292 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14293 parser->num_template_parameter_lists);
14296 /* Indicate whether this class was declared as a `class' or as a
14298 if (TREE_CODE (type) == RECORD_TYPE)
14299 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14300 cp_parser_check_class_key (class_key, type);
14302 /* If this type was already complete, and we see another definition,
14303 that's an error. */
14304 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14306 error ("redefinition of %q#T", type);
14307 error ("previous definition of %q+#T", type);
14311 else if (type == error_mark_node)
14314 /* We will have entered the scope containing the class; the names of
14315 base classes should be looked up in that context. For example:
14317 struct A { struct B {}; struct C; };
14318 struct A::C : B {};
14322 /* Get the list of base-classes, if there is one. */
14323 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14324 *bases = cp_parser_base_clause (parser);
14327 /* Leave the scope given by the nested-name-specifier. We will
14328 enter the class scope itself while processing the members. */
14330 pop_scope (pushed_scope);
14332 if (invalid_explicit_specialization_p)
14334 end_specialization ();
14335 --parser->num_template_parameter_lists;
14337 *attributes_p = attributes;
14341 /* Parse a class-key.
14348 Returns the kind of class-key specified, or none_type to indicate
14351 static enum tag_types
14352 cp_parser_class_key (cp_parser* parser)
14355 enum tag_types tag_type;
14357 /* Look for the class-key. */
14358 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14362 /* Check to see if the TOKEN is a class-key. */
14363 tag_type = cp_parser_token_is_class_key (token);
14365 cp_parser_error (parser, "expected class-key");
14369 /* Parse an (optional) member-specification.
14371 member-specification:
14372 member-declaration member-specification [opt]
14373 access-specifier : member-specification [opt] */
14376 cp_parser_member_specification_opt (cp_parser* parser)
14383 /* Peek at the next token. */
14384 token = cp_lexer_peek_token (parser->lexer);
14385 /* If it's a `}', or EOF then we've seen all the members. */
14386 if (token->type == CPP_CLOSE_BRACE
14387 || token->type == CPP_EOF
14388 || token->type == CPP_PRAGMA_EOL)
14391 /* See if this token is a keyword. */
14392 keyword = token->keyword;
14396 case RID_PROTECTED:
14398 /* Consume the access-specifier. */
14399 cp_lexer_consume_token (parser->lexer);
14400 /* Remember which access-specifier is active. */
14401 current_access_specifier = token->u.value;
14402 /* Look for the `:'. */
14403 cp_parser_require (parser, CPP_COLON, "`:'");
14407 /* Accept #pragmas at class scope. */
14408 if (token->type == CPP_PRAGMA)
14410 cp_parser_pragma (parser, pragma_external);
14414 /* Otherwise, the next construction must be a
14415 member-declaration. */
14416 cp_parser_member_declaration (parser);
14421 /* Parse a member-declaration.
14423 member-declaration:
14424 decl-specifier-seq [opt] member-declarator-list [opt] ;
14425 function-definition ; [opt]
14426 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14428 template-declaration
14430 member-declarator-list:
14432 member-declarator-list , member-declarator
14435 declarator pure-specifier [opt]
14436 declarator constant-initializer [opt]
14437 identifier [opt] : constant-expression
14441 member-declaration:
14442 __extension__ member-declaration
14445 declarator attributes [opt] pure-specifier [opt]
14446 declarator attributes [opt] constant-initializer [opt]
14447 identifier [opt] attributes [opt] : constant-expression
14451 member-declaration:
14452 static_assert-declaration */
14455 cp_parser_member_declaration (cp_parser* parser)
14457 cp_decl_specifier_seq decl_specifiers;
14458 tree prefix_attributes;
14460 int declares_class_or_enum;
14463 int saved_pedantic;
14465 /* Check for the `__extension__' keyword. */
14466 if (cp_parser_extension_opt (parser, &saved_pedantic))
14469 cp_parser_member_declaration (parser);
14470 /* Restore the old value of the PEDANTIC flag. */
14471 pedantic = saved_pedantic;
14476 /* Check for a template-declaration. */
14477 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14479 /* An explicit specialization here is an error condition, and we
14480 expect the specialization handler to detect and report this. */
14481 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14482 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14483 cp_parser_explicit_specialization (parser);
14485 cp_parser_template_declaration (parser, /*member_p=*/true);
14490 /* Check for a using-declaration. */
14491 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14493 /* Parse the using-declaration. */
14494 cp_parser_using_declaration (parser,
14495 /*access_declaration_p=*/false);
14499 /* Check for @defs. */
14500 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14503 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14504 ivar = ivar_chains;
14508 ivar = TREE_CHAIN (member);
14509 TREE_CHAIN (member) = NULL_TREE;
14510 finish_member_declaration (member);
14515 /* If the next token is `static_assert' we have a static assertion. */
14516 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14518 cp_parser_static_assert (parser, /*member_p=*/true);
14522 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14525 /* Parse the decl-specifier-seq. */
14526 cp_parser_decl_specifier_seq (parser,
14527 CP_PARSER_FLAGS_OPTIONAL,
14529 &declares_class_or_enum);
14530 prefix_attributes = decl_specifiers.attributes;
14531 decl_specifiers.attributes = NULL_TREE;
14532 /* Check for an invalid type-name. */
14533 if (!decl_specifiers.type
14534 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14536 /* If there is no declarator, then the decl-specifier-seq should
14538 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14540 /* If there was no decl-specifier-seq, and the next token is a
14541 `;', then we have something like:
14547 Each member-declaration shall declare at least one member
14548 name of the class. */
14549 if (!decl_specifiers.any_specifiers_p)
14551 cp_token *token = cp_lexer_peek_token (parser->lexer);
14552 if (pedantic && !token->in_system_header)
14553 pedwarn ("%Hextra %<;%>", &token->location);
14559 /* See if this declaration is a friend. */
14560 friend_p = cp_parser_friend_p (&decl_specifiers);
14561 /* If there were decl-specifiers, check to see if there was
14562 a class-declaration. */
14563 type = check_tag_decl (&decl_specifiers);
14564 /* Nested classes have already been added to the class, but
14565 a `friend' needs to be explicitly registered. */
14568 /* If the `friend' keyword was present, the friend must
14569 be introduced with a class-key. */
14570 if (!declares_class_or_enum)
14571 error ("a class-key must be used when declaring a friend");
14574 template <typename T> struct A {
14575 friend struct A<T>::B;
14578 A<T>::B will be represented by a TYPENAME_TYPE, and
14579 therefore not recognized by check_tag_decl. */
14581 && decl_specifiers.type
14582 && TYPE_P (decl_specifiers.type))
14583 type = decl_specifiers.type;
14584 if (!type || !TYPE_P (type))
14585 error ("friend declaration does not name a class or "
14588 make_friend_class (current_class_type, type,
14589 /*complain=*/true);
14591 /* If there is no TYPE, an error message will already have
14593 else if (!type || type == error_mark_node)
14595 /* An anonymous aggregate has to be handled specially; such
14596 a declaration really declares a data member (with a
14597 particular type), as opposed to a nested class. */
14598 else if (ANON_AGGR_TYPE_P (type))
14600 /* Remove constructors and such from TYPE, now that we
14601 know it is an anonymous aggregate. */
14602 fixup_anonymous_aggr (type);
14603 /* And make the corresponding data member. */
14604 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14605 /* Add it to the class. */
14606 finish_member_declaration (decl);
14609 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14614 /* See if these declarations will be friends. */
14615 friend_p = cp_parser_friend_p (&decl_specifiers);
14617 /* Keep going until we hit the `;' at the end of the
14619 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14621 tree attributes = NULL_TREE;
14622 tree first_attribute;
14624 /* Peek at the next token. */
14625 token = cp_lexer_peek_token (parser->lexer);
14627 /* Check for a bitfield declaration. */
14628 if (token->type == CPP_COLON
14629 || (token->type == CPP_NAME
14630 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14636 /* Get the name of the bitfield. Note that we cannot just
14637 check TOKEN here because it may have been invalidated by
14638 the call to cp_lexer_peek_nth_token above. */
14639 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14640 identifier = cp_parser_identifier (parser);
14642 identifier = NULL_TREE;
14644 /* Consume the `:' token. */
14645 cp_lexer_consume_token (parser->lexer);
14646 /* Get the width of the bitfield. */
14648 = cp_parser_constant_expression (parser,
14649 /*allow_non_constant=*/false,
14652 /* Look for attributes that apply to the bitfield. */
14653 attributes = cp_parser_attributes_opt (parser);
14654 /* Remember which attributes are prefix attributes and
14656 first_attribute = attributes;
14657 /* Combine the attributes. */
14658 attributes = chainon (prefix_attributes, attributes);
14660 /* Create the bitfield declaration. */
14661 decl = grokbitfield (identifier
14662 ? make_id_declarator (NULL_TREE,
14668 /* Apply the attributes. */
14669 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14673 cp_declarator *declarator;
14675 tree asm_specification;
14676 int ctor_dtor_or_conv_p;
14678 /* Parse the declarator. */
14680 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14681 &ctor_dtor_or_conv_p,
14682 /*parenthesized_p=*/NULL,
14683 /*member_p=*/true);
14685 /* If something went wrong parsing the declarator, make sure
14686 that we at least consume some tokens. */
14687 if (declarator == cp_error_declarator)
14689 /* Skip to the end of the statement. */
14690 cp_parser_skip_to_end_of_statement (parser);
14691 /* If the next token is not a semicolon, that is
14692 probably because we just skipped over the body of
14693 a function. So, we consume a semicolon if
14694 present, but do not issue an error message if it
14696 if (cp_lexer_next_token_is (parser->lexer,
14698 cp_lexer_consume_token (parser->lexer);
14702 if (declares_class_or_enum & 2)
14703 cp_parser_check_for_definition_in_return_type
14704 (declarator, decl_specifiers.type);
14706 /* Look for an asm-specification. */
14707 asm_specification = cp_parser_asm_specification_opt (parser);
14708 /* Look for attributes that apply to the declaration. */
14709 attributes = cp_parser_attributes_opt (parser);
14710 /* Remember which attributes are prefix attributes and
14712 first_attribute = attributes;
14713 /* Combine the attributes. */
14714 attributes = chainon (prefix_attributes, attributes);
14716 /* If it's an `=', then we have a constant-initializer or a
14717 pure-specifier. It is not correct to parse the
14718 initializer before registering the member declaration
14719 since the member declaration should be in scope while
14720 its initializer is processed. However, the rest of the
14721 front end does not yet provide an interface that allows
14722 us to handle this correctly. */
14723 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14727 A pure-specifier shall be used only in the declaration of
14728 a virtual function.
14730 A member-declarator can contain a constant-initializer
14731 only if it declares a static member of integral or
14734 Therefore, if the DECLARATOR is for a function, we look
14735 for a pure-specifier; otherwise, we look for a
14736 constant-initializer. When we call `grokfield', it will
14737 perform more stringent semantics checks. */
14738 if (function_declarator_p (declarator))
14739 initializer = cp_parser_pure_specifier (parser);
14741 /* Parse the initializer. */
14742 initializer = cp_parser_constant_initializer (parser);
14744 /* Otherwise, there is no initializer. */
14746 initializer = NULL_TREE;
14748 /* See if we are probably looking at a function
14749 definition. We are certainly not looking at a
14750 member-declarator. Calling `grokfield' has
14751 side-effects, so we must not do it unless we are sure
14752 that we are looking at a member-declarator. */
14753 if (cp_parser_token_starts_function_definition_p
14754 (cp_lexer_peek_token (parser->lexer)))
14756 /* The grammar does not allow a pure-specifier to be
14757 used when a member function is defined. (It is
14758 possible that this fact is an oversight in the
14759 standard, since a pure function may be defined
14760 outside of the class-specifier. */
14762 error ("pure-specifier on function-definition");
14763 decl = cp_parser_save_member_function_body (parser,
14767 /* If the member was not a friend, declare it here. */
14769 finish_member_declaration (decl);
14770 /* Peek at the next token. */
14771 token = cp_lexer_peek_token (parser->lexer);
14772 /* If the next token is a semicolon, consume it. */
14773 if (token->type == CPP_SEMICOLON)
14775 if (pedantic && !in_system_header)
14776 pedwarn ("extra %<;%>");
14777 cp_lexer_consume_token (parser->lexer);
14782 /* Create the declaration. */
14783 decl = grokfield (declarator, &decl_specifiers,
14784 initializer, /*init_const_expr_p=*/true,
14789 /* Reset PREFIX_ATTRIBUTES. */
14790 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14791 attributes = TREE_CHAIN (attributes);
14793 TREE_CHAIN (attributes) = NULL_TREE;
14795 /* If there is any qualification still in effect, clear it
14796 now; we will be starting fresh with the next declarator. */
14797 parser->scope = NULL_TREE;
14798 parser->qualifying_scope = NULL_TREE;
14799 parser->object_scope = NULL_TREE;
14800 /* If it's a `,', then there are more declarators. */
14801 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14802 cp_lexer_consume_token (parser->lexer);
14803 /* If the next token isn't a `;', then we have a parse error. */
14804 else if (cp_lexer_next_token_is_not (parser->lexer,
14807 cp_parser_error (parser, "expected %<;%>");
14808 /* Skip tokens until we find a `;'. */
14809 cp_parser_skip_to_end_of_statement (parser);
14816 /* Add DECL to the list of members. */
14818 finish_member_declaration (decl);
14820 if (TREE_CODE (decl) == FUNCTION_DECL)
14821 cp_parser_save_default_args (parser, decl);
14826 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14829 /* Parse a pure-specifier.
14834 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14835 Otherwise, ERROR_MARK_NODE is returned. */
14838 cp_parser_pure_specifier (cp_parser* parser)
14842 /* Look for the `=' token. */
14843 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14844 return error_mark_node;
14845 /* Look for the `0' token. */
14846 token = cp_lexer_consume_token (parser->lexer);
14847 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14848 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14850 cp_parser_error (parser,
14851 "invalid pure specifier (only `= 0' is allowed)");
14852 cp_parser_skip_to_end_of_statement (parser);
14853 return error_mark_node;
14855 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14857 error ("templates may not be %<virtual%>");
14858 return error_mark_node;
14861 return integer_zero_node;
14864 /* Parse a constant-initializer.
14866 constant-initializer:
14867 = constant-expression
14869 Returns a representation of the constant-expression. */
14872 cp_parser_constant_initializer (cp_parser* parser)
14874 /* Look for the `=' token. */
14875 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14876 return error_mark_node;
14878 /* It is invalid to write:
14880 struct S { static const int i = { 7 }; };
14883 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14885 cp_parser_error (parser,
14886 "a brace-enclosed initializer is not allowed here");
14887 /* Consume the opening brace. */
14888 cp_lexer_consume_token (parser->lexer);
14889 /* Skip the initializer. */
14890 cp_parser_skip_to_closing_brace (parser);
14891 /* Look for the trailing `}'. */
14892 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14894 return error_mark_node;
14897 return cp_parser_constant_expression (parser,
14898 /*allow_non_constant=*/false,
14902 /* Derived classes [gram.class.derived] */
14904 /* Parse a base-clause.
14907 : base-specifier-list
14909 base-specifier-list:
14910 base-specifier ... [opt]
14911 base-specifier-list , base-specifier ... [opt]
14913 Returns a TREE_LIST representing the base-classes, in the order in
14914 which they were declared. The representation of each node is as
14915 described by cp_parser_base_specifier.
14917 In the case that no bases are specified, this function will return
14918 NULL_TREE, not ERROR_MARK_NODE. */
14921 cp_parser_base_clause (cp_parser* parser)
14923 tree bases = NULL_TREE;
14925 /* Look for the `:' that begins the list. */
14926 cp_parser_require (parser, CPP_COLON, "`:'");
14928 /* Scan the base-specifier-list. */
14933 bool pack_expansion_p = false;
14935 /* Look for the base-specifier. */
14936 base = cp_parser_base_specifier (parser);
14937 /* Look for the (optional) ellipsis. */
14938 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14940 /* Consume the `...'. */
14941 cp_lexer_consume_token (parser->lexer);
14943 pack_expansion_p = true;
14946 /* Add BASE to the front of the list. */
14947 if (base != error_mark_node)
14949 if (pack_expansion_p)
14950 /* Make this a pack expansion type. */
14951 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14953 check_for_bare_parameter_packs (TREE_VALUE (base));
14955 TREE_CHAIN (base) = bases;
14958 /* Peek at the next token. */
14959 token = cp_lexer_peek_token (parser->lexer);
14960 /* If it's not a comma, then the list is complete. */
14961 if (token->type != CPP_COMMA)
14963 /* Consume the `,'. */
14964 cp_lexer_consume_token (parser->lexer);
14967 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14968 base class had a qualified name. However, the next name that
14969 appears is certainly not qualified. */
14970 parser->scope = NULL_TREE;
14971 parser->qualifying_scope = NULL_TREE;
14972 parser->object_scope = NULL_TREE;
14974 return nreverse (bases);
14977 /* Parse a base-specifier.
14980 :: [opt] nested-name-specifier [opt] class-name
14981 virtual access-specifier [opt] :: [opt] nested-name-specifier
14983 access-specifier virtual [opt] :: [opt] nested-name-specifier
14986 Returns a TREE_LIST. The TREE_PURPOSE will be one of
14987 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
14988 indicate the specifiers provided. The TREE_VALUE will be a TYPE
14989 (or the ERROR_MARK_NODE) indicating the type that was specified. */
14992 cp_parser_base_specifier (cp_parser* parser)
14996 bool virtual_p = false;
14997 bool duplicate_virtual_error_issued_p = false;
14998 bool duplicate_access_error_issued_p = false;
14999 bool class_scope_p, template_p;
15000 tree access = access_default_node;
15003 /* Process the optional `virtual' and `access-specifier'. */
15006 /* Peek at the next token. */
15007 token = cp_lexer_peek_token (parser->lexer);
15008 /* Process `virtual'. */
15009 switch (token->keyword)
15012 /* If `virtual' appears more than once, issue an error. */
15013 if (virtual_p && !duplicate_virtual_error_issued_p)
15015 cp_parser_error (parser,
15016 "%<virtual%> specified more than once in base-specified");
15017 duplicate_virtual_error_issued_p = true;
15022 /* Consume the `virtual' token. */
15023 cp_lexer_consume_token (parser->lexer);
15028 case RID_PROTECTED:
15030 /* If more than one access specifier appears, issue an
15032 if (access != access_default_node
15033 && !duplicate_access_error_issued_p)
15035 cp_parser_error (parser,
15036 "more than one access specifier in base-specified");
15037 duplicate_access_error_issued_p = true;
15040 access = ridpointers[(int) token->keyword];
15042 /* Consume the access-specifier. */
15043 cp_lexer_consume_token (parser->lexer);
15052 /* It is not uncommon to see programs mechanically, erroneously, use
15053 the 'typename' keyword to denote (dependent) qualified types
15054 as base classes. */
15055 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15057 if (!processing_template_decl)
15058 error ("keyword %<typename%> not allowed outside of templates");
15060 error ("keyword %<typename%> not allowed in this context "
15061 "(the base class is implicitly a type)");
15062 cp_lexer_consume_token (parser->lexer);
15065 /* Look for the optional `::' operator. */
15066 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15067 /* Look for the nested-name-specifier. The simplest way to
15072 The keyword `typename' is not permitted in a base-specifier or
15073 mem-initializer; in these contexts a qualified name that
15074 depends on a template-parameter is implicitly assumed to be a
15077 is to pretend that we have seen the `typename' keyword at this
15079 cp_parser_nested_name_specifier_opt (parser,
15080 /*typename_keyword_p=*/true,
15081 /*check_dependency_p=*/true,
15083 /*is_declaration=*/true);
15084 /* If the base class is given by a qualified name, assume that names
15085 we see are type names or templates, as appropriate. */
15086 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15087 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15089 /* Finally, look for the class-name. */
15090 type = cp_parser_class_name (parser,
15094 /*check_dependency_p=*/true,
15095 /*class_head_p=*/false,
15096 /*is_declaration=*/true);
15098 if (type == error_mark_node)
15099 return error_mark_node;
15101 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15104 /* Exception handling [gram.exception] */
15106 /* Parse an (optional) exception-specification.
15108 exception-specification:
15109 throw ( type-id-list [opt] )
15111 Returns a TREE_LIST representing the exception-specification. The
15112 TREE_VALUE of each node is a type. */
15115 cp_parser_exception_specification_opt (cp_parser* parser)
15120 /* Peek at the next token. */
15121 token = cp_lexer_peek_token (parser->lexer);
15122 /* If it's not `throw', then there's no exception-specification. */
15123 if (!cp_parser_is_keyword (token, RID_THROW))
15126 /* Consume the `throw'. */
15127 cp_lexer_consume_token (parser->lexer);
15129 /* Look for the `('. */
15130 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15132 /* Peek at the next token. */
15133 token = cp_lexer_peek_token (parser->lexer);
15134 /* If it's not a `)', then there is a type-id-list. */
15135 if (token->type != CPP_CLOSE_PAREN)
15137 const char *saved_message;
15139 /* Types may not be defined in an exception-specification. */
15140 saved_message = parser->type_definition_forbidden_message;
15141 parser->type_definition_forbidden_message
15142 = "types may not be defined in an exception-specification";
15143 /* Parse the type-id-list. */
15144 type_id_list = cp_parser_type_id_list (parser);
15145 /* Restore the saved message. */
15146 parser->type_definition_forbidden_message = saved_message;
15149 type_id_list = empty_except_spec;
15151 /* Look for the `)'. */
15152 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15154 return type_id_list;
15157 /* Parse an (optional) type-id-list.
15161 type-id-list , type-id ... [opt]
15163 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15164 in the order that the types were presented. */
15167 cp_parser_type_id_list (cp_parser* parser)
15169 tree types = NULL_TREE;
15176 /* Get the next type-id. */
15177 type = cp_parser_type_id (parser);
15178 /* Parse the optional ellipsis. */
15179 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15181 /* Consume the `...'. */
15182 cp_lexer_consume_token (parser->lexer);
15184 /* Turn the type into a pack expansion expression. */
15185 type = make_pack_expansion (type);
15187 /* Add it to the list. */
15188 types = add_exception_specifier (types, type, /*complain=*/1);
15189 /* Peek at the next token. */
15190 token = cp_lexer_peek_token (parser->lexer);
15191 /* If it is not a `,', we are done. */
15192 if (token->type != CPP_COMMA)
15194 /* Consume the `,'. */
15195 cp_lexer_consume_token (parser->lexer);
15198 return nreverse (types);
15201 /* Parse a try-block.
15204 try compound-statement handler-seq */
15207 cp_parser_try_block (cp_parser* parser)
15211 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15212 try_block = begin_try_block ();
15213 cp_parser_compound_statement (parser, NULL, true);
15214 finish_try_block (try_block);
15215 cp_parser_handler_seq (parser);
15216 finish_handler_sequence (try_block);
15221 /* Parse a function-try-block.
15223 function-try-block:
15224 try ctor-initializer [opt] function-body handler-seq */
15227 cp_parser_function_try_block (cp_parser* parser)
15229 tree compound_stmt;
15231 bool ctor_initializer_p;
15233 /* Look for the `try' keyword. */
15234 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15236 /* Let the rest of the front end know where we are. */
15237 try_block = begin_function_try_block (&compound_stmt);
15238 /* Parse the function-body. */
15240 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15241 /* We're done with the `try' part. */
15242 finish_function_try_block (try_block);
15243 /* Parse the handlers. */
15244 cp_parser_handler_seq (parser);
15245 /* We're done with the handlers. */
15246 finish_function_handler_sequence (try_block, compound_stmt);
15248 return ctor_initializer_p;
15251 /* Parse a handler-seq.
15254 handler handler-seq [opt] */
15257 cp_parser_handler_seq (cp_parser* parser)
15263 /* Parse the handler. */
15264 cp_parser_handler (parser);
15265 /* Peek at the next token. */
15266 token = cp_lexer_peek_token (parser->lexer);
15267 /* If it's not `catch' then there are no more handlers. */
15268 if (!cp_parser_is_keyword (token, RID_CATCH))
15273 /* Parse a handler.
15276 catch ( exception-declaration ) compound-statement */
15279 cp_parser_handler (cp_parser* parser)
15284 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15285 handler = begin_handler ();
15286 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15287 declaration = cp_parser_exception_declaration (parser);
15288 finish_handler_parms (declaration, handler);
15289 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15290 cp_parser_compound_statement (parser, NULL, false);
15291 finish_handler (handler);
15294 /* Parse an exception-declaration.
15296 exception-declaration:
15297 type-specifier-seq declarator
15298 type-specifier-seq abstract-declarator
15302 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15303 ellipsis variant is used. */
15306 cp_parser_exception_declaration (cp_parser* parser)
15308 cp_decl_specifier_seq type_specifiers;
15309 cp_declarator *declarator;
15310 const char *saved_message;
15312 /* If it's an ellipsis, it's easy to handle. */
15313 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15315 /* Consume the `...' token. */
15316 cp_lexer_consume_token (parser->lexer);
15320 /* Types may not be defined in exception-declarations. */
15321 saved_message = parser->type_definition_forbidden_message;
15322 parser->type_definition_forbidden_message
15323 = "types may not be defined in exception-declarations";
15325 /* Parse the type-specifier-seq. */
15326 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15328 /* If it's a `)', then there is no declarator. */
15329 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15332 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15333 /*ctor_dtor_or_conv_p=*/NULL,
15334 /*parenthesized_p=*/NULL,
15335 /*member_p=*/false);
15337 /* Restore the saved message. */
15338 parser->type_definition_forbidden_message = saved_message;
15340 if (!type_specifiers.any_specifiers_p)
15341 return error_mark_node;
15343 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15346 /* Parse a throw-expression.
15349 throw assignment-expression [opt]
15351 Returns a THROW_EXPR representing the throw-expression. */
15354 cp_parser_throw_expression (cp_parser* parser)
15359 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15360 token = cp_lexer_peek_token (parser->lexer);
15361 /* Figure out whether or not there is an assignment-expression
15362 following the "throw" keyword. */
15363 if (token->type == CPP_COMMA
15364 || token->type == CPP_SEMICOLON
15365 || token->type == CPP_CLOSE_PAREN
15366 || token->type == CPP_CLOSE_SQUARE
15367 || token->type == CPP_CLOSE_BRACE
15368 || token->type == CPP_COLON)
15369 expression = NULL_TREE;
15371 expression = cp_parser_assignment_expression (parser,
15374 return build_throw (expression);
15377 /* GNU Extensions */
15379 /* Parse an (optional) asm-specification.
15382 asm ( string-literal )
15384 If the asm-specification is present, returns a STRING_CST
15385 corresponding to the string-literal. Otherwise, returns
15389 cp_parser_asm_specification_opt (cp_parser* parser)
15392 tree asm_specification;
15394 /* Peek at the next token. */
15395 token = cp_lexer_peek_token (parser->lexer);
15396 /* If the next token isn't the `asm' keyword, then there's no
15397 asm-specification. */
15398 if (!cp_parser_is_keyword (token, RID_ASM))
15401 /* Consume the `asm' token. */
15402 cp_lexer_consume_token (parser->lexer);
15403 /* Look for the `('. */
15404 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15406 /* Look for the string-literal. */
15407 asm_specification = cp_parser_string_literal (parser, false, false);
15409 /* Look for the `)'. */
15410 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15412 return asm_specification;
15415 /* Parse an asm-operand-list.
15419 asm-operand-list , asm-operand
15422 string-literal ( expression )
15423 [ string-literal ] string-literal ( expression )
15425 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15426 each node is the expression. The TREE_PURPOSE is itself a
15427 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15428 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15429 is a STRING_CST for the string literal before the parenthesis. */
15432 cp_parser_asm_operand_list (cp_parser* parser)
15434 tree asm_operands = NULL_TREE;
15438 tree string_literal;
15442 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15444 /* Consume the `[' token. */
15445 cp_lexer_consume_token (parser->lexer);
15446 /* Read the operand name. */
15447 name = cp_parser_identifier (parser);
15448 if (name != error_mark_node)
15449 name = build_string (IDENTIFIER_LENGTH (name),
15450 IDENTIFIER_POINTER (name));
15451 /* Look for the closing `]'. */
15452 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15456 /* Look for the string-literal. */
15457 string_literal = cp_parser_string_literal (parser, false, false);
15459 /* Look for the `('. */
15460 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15461 /* Parse the expression. */
15462 expression = cp_parser_expression (parser, /*cast_p=*/false);
15463 /* Look for the `)'. */
15464 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15466 /* Add this operand to the list. */
15467 asm_operands = tree_cons (build_tree_list (name, string_literal),
15470 /* If the next token is not a `,', there are no more
15472 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15474 /* Consume the `,'. */
15475 cp_lexer_consume_token (parser->lexer);
15478 return nreverse (asm_operands);
15481 /* Parse an asm-clobber-list.
15485 asm-clobber-list , string-literal
15487 Returns a TREE_LIST, indicating the clobbers in the order that they
15488 appeared. The TREE_VALUE of each node is a STRING_CST. */
15491 cp_parser_asm_clobber_list (cp_parser* parser)
15493 tree clobbers = NULL_TREE;
15497 tree string_literal;
15499 /* Look for the string literal. */
15500 string_literal = cp_parser_string_literal (parser, false, false);
15501 /* Add it to the list. */
15502 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15503 /* If the next token is not a `,', then the list is
15505 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15507 /* Consume the `,' token. */
15508 cp_lexer_consume_token (parser->lexer);
15514 /* Parse an (optional) series of attributes.
15517 attributes attribute
15520 __attribute__ (( attribute-list [opt] ))
15522 The return value is as for cp_parser_attribute_list. */
15525 cp_parser_attributes_opt (cp_parser* parser)
15527 tree attributes = NULL_TREE;
15532 tree attribute_list;
15534 /* Peek at the next token. */
15535 token = cp_lexer_peek_token (parser->lexer);
15536 /* If it's not `__attribute__', then we're done. */
15537 if (token->keyword != RID_ATTRIBUTE)
15540 /* Consume the `__attribute__' keyword. */
15541 cp_lexer_consume_token (parser->lexer);
15542 /* Look for the two `(' tokens. */
15543 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15544 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15546 /* Peek at the next token. */
15547 token = cp_lexer_peek_token (parser->lexer);
15548 if (token->type != CPP_CLOSE_PAREN)
15549 /* Parse the attribute-list. */
15550 attribute_list = cp_parser_attribute_list (parser);
15552 /* If the next token is a `)', then there is no attribute
15554 attribute_list = NULL;
15556 /* Look for the two `)' tokens. */
15557 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15558 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15560 /* Add these new attributes to the list. */
15561 attributes = chainon (attributes, attribute_list);
15567 /* Parse an attribute-list.
15571 attribute-list , attribute
15575 identifier ( identifier )
15576 identifier ( identifier , expression-list )
15577 identifier ( expression-list )
15579 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15580 to an attribute. The TREE_PURPOSE of each node is the identifier
15581 indicating which attribute is in use. The TREE_VALUE represents
15582 the arguments, if any. */
15585 cp_parser_attribute_list (cp_parser* parser)
15587 tree attribute_list = NULL_TREE;
15588 bool save_translate_strings_p = parser->translate_strings_p;
15590 parser->translate_strings_p = false;
15597 /* Look for the identifier. We also allow keywords here; for
15598 example `__attribute__ ((const))' is legal. */
15599 token = cp_lexer_peek_token (parser->lexer);
15600 if (token->type == CPP_NAME
15601 || token->type == CPP_KEYWORD)
15603 tree arguments = NULL_TREE;
15605 /* Consume the token. */
15606 token = cp_lexer_consume_token (parser->lexer);
15608 /* Save away the identifier that indicates which attribute
15610 identifier = token->u.value;
15611 attribute = build_tree_list (identifier, NULL_TREE);
15613 /* Peek at the next token. */
15614 token = cp_lexer_peek_token (parser->lexer);
15615 /* If it's an `(', then parse the attribute arguments. */
15616 if (token->type == CPP_OPEN_PAREN)
15618 arguments = cp_parser_parenthesized_expression_list
15619 (parser, true, /*cast_p=*/false,
15620 /*allow_expansion_p=*/false,
15621 /*non_constant_p=*/NULL);
15622 /* Save the arguments away. */
15623 TREE_VALUE (attribute) = arguments;
15626 if (arguments != error_mark_node)
15628 /* Add this attribute to the list. */
15629 TREE_CHAIN (attribute) = attribute_list;
15630 attribute_list = attribute;
15633 token = cp_lexer_peek_token (parser->lexer);
15635 /* Now, look for more attributes. If the next token isn't a
15636 `,', we're done. */
15637 if (token->type != CPP_COMMA)
15640 /* Consume the comma and keep going. */
15641 cp_lexer_consume_token (parser->lexer);
15643 parser->translate_strings_p = save_translate_strings_p;
15645 /* We built up the list in reverse order. */
15646 return nreverse (attribute_list);
15649 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15650 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15651 current value of the PEDANTIC flag, regardless of whether or not
15652 the `__extension__' keyword is present. The caller is responsible
15653 for restoring the value of the PEDANTIC flag. */
15656 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15658 /* Save the old value of the PEDANTIC flag. */
15659 *saved_pedantic = pedantic;
15661 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15663 /* Consume the `__extension__' token. */
15664 cp_lexer_consume_token (parser->lexer);
15665 /* We're not being pedantic while the `__extension__' keyword is
15675 /* Parse a label declaration.
15678 __label__ label-declarator-seq ;
15680 label-declarator-seq:
15681 identifier , label-declarator-seq
15685 cp_parser_label_declaration (cp_parser* parser)
15687 /* Look for the `__label__' keyword. */
15688 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15694 /* Look for an identifier. */
15695 identifier = cp_parser_identifier (parser);
15696 /* If we failed, stop. */
15697 if (identifier == error_mark_node)
15699 /* Declare it as a label. */
15700 finish_label_decl (identifier);
15701 /* If the next token is a `;', stop. */
15702 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15704 /* Look for the `,' separating the label declarations. */
15705 cp_parser_require (parser, CPP_COMMA, "`,'");
15708 /* Look for the final `;'. */
15709 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15712 /* Support Functions */
15714 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15715 NAME should have one of the representations used for an
15716 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15717 is returned. If PARSER->SCOPE is a dependent type, then a
15718 SCOPE_REF is returned.
15720 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15721 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15722 was formed. Abstractly, such entities should not be passed to this
15723 function, because they do not need to be looked up, but it is
15724 simpler to check for this special case here, rather than at the
15727 In cases not explicitly covered above, this function returns a
15728 DECL, OVERLOAD, or baselink representing the result of the lookup.
15729 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15732 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15733 (e.g., "struct") that was used. In that case bindings that do not
15734 refer to types are ignored.
15736 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15739 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15742 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15745 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15746 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15747 NULL_TREE otherwise. */
15750 cp_parser_lookup_name (cp_parser *parser, tree name,
15751 enum tag_types tag_type,
15754 bool check_dependency,
15755 tree *ambiguous_decls)
15759 tree object_type = parser->context->object_type;
15761 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15762 flags |= LOOKUP_COMPLAIN;
15764 /* Assume that the lookup will be unambiguous. */
15765 if (ambiguous_decls)
15766 *ambiguous_decls = NULL_TREE;
15768 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15769 no longer valid. Note that if we are parsing tentatively, and
15770 the parse fails, OBJECT_TYPE will be automatically restored. */
15771 parser->context->object_type = NULL_TREE;
15773 if (name == error_mark_node)
15774 return error_mark_node;
15776 /* A template-id has already been resolved; there is no lookup to
15778 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15780 if (BASELINK_P (name))
15782 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15783 == TEMPLATE_ID_EXPR);
15787 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15788 it should already have been checked to make sure that the name
15789 used matches the type being destroyed. */
15790 if (TREE_CODE (name) == BIT_NOT_EXPR)
15794 /* Figure out to which type this destructor applies. */
15796 type = parser->scope;
15797 else if (object_type)
15798 type = object_type;
15800 type = current_class_type;
15801 /* If that's not a class type, there is no destructor. */
15802 if (!type || !CLASS_TYPE_P (type))
15803 return error_mark_node;
15804 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15805 lazily_declare_fn (sfk_destructor, type);
15806 if (!CLASSTYPE_DESTRUCTORS (type))
15807 return error_mark_node;
15808 /* If it was a class type, return the destructor. */
15809 return CLASSTYPE_DESTRUCTORS (type);
15812 /* By this point, the NAME should be an ordinary identifier. If
15813 the id-expression was a qualified name, the qualifying scope is
15814 stored in PARSER->SCOPE at this point. */
15815 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15817 /* Perform the lookup. */
15822 if (parser->scope == error_mark_node)
15823 return error_mark_node;
15825 /* If the SCOPE is dependent, the lookup must be deferred until
15826 the template is instantiated -- unless we are explicitly
15827 looking up names in uninstantiated templates. Even then, we
15828 cannot look up the name if the scope is not a class type; it
15829 might, for example, be a template type parameter. */
15830 dependent_p = (TYPE_P (parser->scope)
15831 && !(parser->in_declarator_p
15832 && currently_open_class (parser->scope))
15833 && dependent_type_p (parser->scope));
15834 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15841 /* The resolution to Core Issue 180 says that `struct
15842 A::B' should be considered a type-name, even if `A'
15844 type = make_typename_type (parser->scope, name, tag_type,
15845 /*complain=*/tf_error);
15846 decl = TYPE_NAME (type);
15848 else if (is_template
15849 && (cp_parser_next_token_ends_template_argument_p (parser)
15850 || cp_lexer_next_token_is (parser->lexer,
15852 decl = make_unbound_class_template (parser->scope,
15854 /*complain=*/tf_error);
15856 decl = build_qualified_name (/*type=*/NULL_TREE,
15857 parser->scope, name,
15862 tree pushed_scope = NULL_TREE;
15864 /* If PARSER->SCOPE is a dependent type, then it must be a
15865 class type, and we must not be checking dependencies;
15866 otherwise, we would have processed this lookup above. So
15867 that PARSER->SCOPE is not considered a dependent base by
15868 lookup_member, we must enter the scope here. */
15870 pushed_scope = push_scope (parser->scope);
15871 /* If the PARSER->SCOPE is a template specialization, it
15872 may be instantiated during name lookup. In that case,
15873 errors may be issued. Even if we rollback the current
15874 tentative parse, those errors are valid. */
15875 decl = lookup_qualified_name (parser->scope, name,
15876 tag_type != none_type,
15877 /*complain=*/true);
15879 pop_scope (pushed_scope);
15881 parser->qualifying_scope = parser->scope;
15882 parser->object_scope = NULL_TREE;
15884 else if (object_type)
15886 tree object_decl = NULL_TREE;
15887 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15888 OBJECT_TYPE is not a class. */
15889 if (CLASS_TYPE_P (object_type))
15890 /* If the OBJECT_TYPE is a template specialization, it may
15891 be instantiated during name lookup. In that case, errors
15892 may be issued. Even if we rollback the current tentative
15893 parse, those errors are valid. */
15894 object_decl = lookup_member (object_type,
15897 tag_type != none_type);
15898 /* Look it up in the enclosing context, too. */
15899 decl = lookup_name_real (name, tag_type != none_type,
15901 /*block_p=*/true, is_namespace, flags);
15902 parser->object_scope = object_type;
15903 parser->qualifying_scope = NULL_TREE;
15905 decl = object_decl;
15909 decl = lookup_name_real (name, tag_type != none_type,
15911 /*block_p=*/true, is_namespace, flags);
15912 parser->qualifying_scope = NULL_TREE;
15913 parser->object_scope = NULL_TREE;
15916 /* If the lookup failed, let our caller know. */
15917 if (!decl || decl == error_mark_node)
15918 return error_mark_node;
15920 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15921 if (TREE_CODE (decl) == TREE_LIST)
15923 if (ambiguous_decls)
15924 *ambiguous_decls = decl;
15925 /* The error message we have to print is too complicated for
15926 cp_parser_error, so we incorporate its actions directly. */
15927 if (!cp_parser_simulate_error (parser))
15929 error ("reference to %qD is ambiguous", name);
15930 print_candidates (decl);
15932 return error_mark_node;
15935 gcc_assert (DECL_P (decl)
15936 || TREE_CODE (decl) == OVERLOAD
15937 || TREE_CODE (decl) == SCOPE_REF
15938 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15939 || BASELINK_P (decl));
15941 /* If we have resolved the name of a member declaration, check to
15942 see if the declaration is accessible. When the name resolves to
15943 set of overloaded functions, accessibility is checked when
15944 overload resolution is done.
15946 During an explicit instantiation, access is not checked at all,
15947 as per [temp.explicit]. */
15949 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15954 /* Like cp_parser_lookup_name, but for use in the typical case where
15955 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15956 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15959 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15961 return cp_parser_lookup_name (parser, name,
15963 /*is_template=*/false,
15964 /*is_namespace=*/false,
15965 /*check_dependency=*/true,
15966 /*ambiguous_decls=*/NULL);
15969 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15970 the current context, return the TYPE_DECL. If TAG_NAME_P is
15971 true, the DECL indicates the class being defined in a class-head,
15972 or declared in an elaborated-type-specifier.
15974 Otherwise, return DECL. */
15977 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15979 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15980 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
15983 template <typename T> struct B;
15986 template <typename T> struct A::B {};
15988 Similarly, in an elaborated-type-specifier:
15990 namespace N { struct X{}; }
15993 template <typename T> friend struct N::X;
15996 However, if the DECL refers to a class type, and we are in
15997 the scope of the class, then the name lookup automatically
15998 finds the TYPE_DECL created by build_self_reference rather
15999 than a TEMPLATE_DECL. For example, in:
16001 template <class T> struct S {
16005 there is no need to handle such case. */
16007 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16008 return DECL_TEMPLATE_RESULT (decl);
16013 /* If too many, or too few, template-parameter lists apply to the
16014 declarator, issue an error message. Returns TRUE if all went well,
16015 and FALSE otherwise. */
16018 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16019 cp_declarator *declarator)
16021 unsigned num_templates;
16023 /* We haven't seen any classes that involve template parameters yet. */
16026 switch (declarator->kind)
16029 if (declarator->u.id.qualifying_scope)
16034 scope = declarator->u.id.qualifying_scope;
16035 member = declarator->u.id.unqualified_name;
16037 while (scope && CLASS_TYPE_P (scope))
16039 /* You're supposed to have one `template <...>'
16040 for every template class, but you don't need one
16041 for a full specialization. For example:
16043 template <class T> struct S{};
16044 template <> struct S<int> { void f(); };
16045 void S<int>::f () {}
16047 is correct; there shouldn't be a `template <>' for
16048 the definition of `S<int>::f'. */
16049 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16050 /* If SCOPE does not have template information of any
16051 kind, then it is not a template, nor is it nested
16052 within a template. */
16054 if (explicit_class_specialization_p (scope))
16056 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16059 scope = TYPE_CONTEXT (scope);
16062 else if (TREE_CODE (declarator->u.id.unqualified_name)
16063 == TEMPLATE_ID_EXPR)
16064 /* If the DECLARATOR has the form `X<y>' then it uses one
16065 additional level of template parameters. */
16068 return cp_parser_check_template_parameters (parser,
16074 case cdk_reference:
16076 return (cp_parser_check_declarator_template_parameters
16077 (parser, declarator->declarator));
16083 gcc_unreachable ();
16088 /* NUM_TEMPLATES were used in the current declaration. If that is
16089 invalid, return FALSE and issue an error messages. Otherwise,
16093 cp_parser_check_template_parameters (cp_parser* parser,
16094 unsigned num_templates)
16096 /* If there are more template classes than parameter lists, we have
16099 template <class T> void S<T>::R<T>::f (); */
16100 if (parser->num_template_parameter_lists < num_templates)
16102 error ("too few template-parameter-lists");
16105 /* If there are the same number of template classes and parameter
16106 lists, that's OK. */
16107 if (parser->num_template_parameter_lists == num_templates)
16109 /* If there are more, but only one more, then we are referring to a
16110 member template. That's OK too. */
16111 if (parser->num_template_parameter_lists == num_templates + 1)
16113 /* Otherwise, there are too many template parameter lists. We have
16116 template <class T> template <class U> void S::f(); */
16117 error ("too many template-parameter-lists");
16121 /* Parse an optional `::' token indicating that the following name is
16122 from the global namespace. If so, PARSER->SCOPE is set to the
16123 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16124 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16125 Returns the new value of PARSER->SCOPE, if the `::' token is
16126 present, and NULL_TREE otherwise. */
16129 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16133 /* Peek at the next token. */
16134 token = cp_lexer_peek_token (parser->lexer);
16135 /* If we're looking at a `::' token then we're starting from the
16136 global namespace, not our current location. */
16137 if (token->type == CPP_SCOPE)
16139 /* Consume the `::' token. */
16140 cp_lexer_consume_token (parser->lexer);
16141 /* Set the SCOPE so that we know where to start the lookup. */
16142 parser->scope = global_namespace;
16143 parser->qualifying_scope = global_namespace;
16144 parser->object_scope = NULL_TREE;
16146 return parser->scope;
16148 else if (!current_scope_valid_p)
16150 parser->scope = NULL_TREE;
16151 parser->qualifying_scope = NULL_TREE;
16152 parser->object_scope = NULL_TREE;
16158 /* Returns TRUE if the upcoming token sequence is the start of a
16159 constructor declarator. If FRIEND_P is true, the declarator is
16160 preceded by the `friend' specifier. */
16163 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16165 bool constructor_p;
16166 tree type_decl = NULL_TREE;
16167 bool nested_name_p;
16168 cp_token *next_token;
16170 /* The common case is that this is not a constructor declarator, so
16171 try to avoid doing lots of work if at all possible. It's not
16172 valid declare a constructor at function scope. */
16173 if (parser->in_function_body)
16175 /* And only certain tokens can begin a constructor declarator. */
16176 next_token = cp_lexer_peek_token (parser->lexer);
16177 if (next_token->type != CPP_NAME
16178 && next_token->type != CPP_SCOPE
16179 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16180 && next_token->type != CPP_TEMPLATE_ID)
16183 /* Parse tentatively; we are going to roll back all of the tokens
16185 cp_parser_parse_tentatively (parser);
16186 /* Assume that we are looking at a constructor declarator. */
16187 constructor_p = true;
16189 /* Look for the optional `::' operator. */
16190 cp_parser_global_scope_opt (parser,
16191 /*current_scope_valid_p=*/false);
16192 /* Look for the nested-name-specifier. */
16194 = (cp_parser_nested_name_specifier_opt (parser,
16195 /*typename_keyword_p=*/false,
16196 /*check_dependency_p=*/false,
16198 /*is_declaration=*/false)
16200 /* Outside of a class-specifier, there must be a
16201 nested-name-specifier. */
16202 if (!nested_name_p &&
16203 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16205 constructor_p = false;
16206 /* If we still think that this might be a constructor-declarator,
16207 look for a class-name. */
16212 template <typename T> struct S { S(); };
16213 template <typename T> S<T>::S ();
16215 we must recognize that the nested `S' names a class.
16218 template <typename T> S<T>::S<T> ();
16220 we must recognize that the nested `S' names a template. */
16221 type_decl = cp_parser_class_name (parser,
16222 /*typename_keyword_p=*/false,
16223 /*template_keyword_p=*/false,
16225 /*check_dependency_p=*/false,
16226 /*class_head_p=*/false,
16227 /*is_declaration=*/false);
16228 /* If there was no class-name, then this is not a constructor. */
16229 constructor_p = !cp_parser_error_occurred (parser);
16232 /* If we're still considering a constructor, we have to see a `(',
16233 to begin the parameter-declaration-clause, followed by either a
16234 `)', an `...', or a decl-specifier. We need to check for a
16235 type-specifier to avoid being fooled into thinking that:
16239 is a constructor. (It is actually a function named `f' that
16240 takes one parameter (of type `int') and returns a value of type
16243 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16245 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16246 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16247 /* A parameter declaration begins with a decl-specifier,
16248 which is either the "attribute" keyword, a storage class
16249 specifier, or (usually) a type-specifier. */
16250 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16253 tree pushed_scope = NULL_TREE;
16254 unsigned saved_num_template_parameter_lists;
16256 /* Names appearing in the type-specifier should be looked up
16257 in the scope of the class. */
16258 if (current_class_type)
16262 type = TREE_TYPE (type_decl);
16263 if (TREE_CODE (type) == TYPENAME_TYPE)
16265 type = resolve_typename_type (type,
16266 /*only_current_p=*/false);
16267 if (type == error_mark_node)
16269 cp_parser_abort_tentative_parse (parser);
16273 pushed_scope = push_scope (type);
16276 /* Inside the constructor parameter list, surrounding
16277 template-parameter-lists do not apply. */
16278 saved_num_template_parameter_lists
16279 = parser->num_template_parameter_lists;
16280 parser->num_template_parameter_lists = 0;
16282 /* Look for the type-specifier. */
16283 cp_parser_type_specifier (parser,
16284 CP_PARSER_FLAGS_NONE,
16285 /*decl_specs=*/NULL,
16286 /*is_declarator=*/true,
16287 /*declares_class_or_enum=*/NULL,
16288 /*is_cv_qualifier=*/NULL);
16290 parser->num_template_parameter_lists
16291 = saved_num_template_parameter_lists;
16293 /* Leave the scope of the class. */
16295 pop_scope (pushed_scope);
16297 constructor_p = !cp_parser_error_occurred (parser);
16301 constructor_p = false;
16302 /* We did not really want to consume any tokens. */
16303 cp_parser_abort_tentative_parse (parser);
16305 return constructor_p;
16308 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16309 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16310 they must be performed once we are in the scope of the function.
16312 Returns the function defined. */
16315 cp_parser_function_definition_from_specifiers_and_declarator
16316 (cp_parser* parser,
16317 cp_decl_specifier_seq *decl_specifiers,
16319 const cp_declarator *declarator)
16324 /* Begin the function-definition. */
16325 success_p = start_function (decl_specifiers, declarator, attributes);
16327 /* The things we're about to see are not directly qualified by any
16328 template headers we've seen thus far. */
16329 reset_specialization ();
16331 /* If there were names looked up in the decl-specifier-seq that we
16332 did not check, check them now. We must wait until we are in the
16333 scope of the function to perform the checks, since the function
16334 might be a friend. */
16335 perform_deferred_access_checks ();
16339 /* Skip the entire function. */
16340 cp_parser_skip_to_end_of_block_or_statement (parser);
16341 fn = error_mark_node;
16343 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16345 /* Seen already, skip it. An error message has already been output. */
16346 cp_parser_skip_to_end_of_block_or_statement (parser);
16347 fn = current_function_decl;
16348 current_function_decl = NULL_TREE;
16349 /* If this is a function from a class, pop the nested class. */
16350 if (current_class_name)
16351 pop_nested_class ();
16354 fn = cp_parser_function_definition_after_declarator (parser,
16355 /*inline_p=*/false);
16360 /* Parse the part of a function-definition that follows the
16361 declarator. INLINE_P is TRUE iff this function is an inline
16362 function defined with a class-specifier.
16364 Returns the function defined. */
16367 cp_parser_function_definition_after_declarator (cp_parser* parser,
16371 bool ctor_initializer_p = false;
16372 bool saved_in_unbraced_linkage_specification_p;
16373 bool saved_in_function_body;
16374 unsigned saved_num_template_parameter_lists;
16376 saved_in_function_body = parser->in_function_body;
16377 parser->in_function_body = true;
16378 /* If the next token is `return', then the code may be trying to
16379 make use of the "named return value" extension that G++ used to
16381 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16383 /* Consume the `return' keyword. */
16384 cp_lexer_consume_token (parser->lexer);
16385 /* Look for the identifier that indicates what value is to be
16387 cp_parser_identifier (parser);
16388 /* Issue an error message. */
16389 error ("named return values are no longer supported");
16390 /* Skip tokens until we reach the start of the function body. */
16393 cp_token *token = cp_lexer_peek_token (parser->lexer);
16394 if (token->type == CPP_OPEN_BRACE
16395 || token->type == CPP_EOF
16396 || token->type == CPP_PRAGMA_EOL)
16398 cp_lexer_consume_token (parser->lexer);
16401 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16402 anything declared inside `f'. */
16403 saved_in_unbraced_linkage_specification_p
16404 = parser->in_unbraced_linkage_specification_p;
16405 parser->in_unbraced_linkage_specification_p = false;
16406 /* Inside the function, surrounding template-parameter-lists do not
16408 saved_num_template_parameter_lists
16409 = parser->num_template_parameter_lists;
16410 parser->num_template_parameter_lists = 0;
16411 /* If the next token is `try', then we are looking at a
16412 function-try-block. */
16413 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16414 ctor_initializer_p = cp_parser_function_try_block (parser);
16415 /* A function-try-block includes the function-body, so we only do
16416 this next part if we're not processing a function-try-block. */
16419 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16421 /* Finish the function. */
16422 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16423 (inline_p ? 2 : 0));
16424 /* Generate code for it, if necessary. */
16425 expand_or_defer_fn (fn);
16426 /* Restore the saved values. */
16427 parser->in_unbraced_linkage_specification_p
16428 = saved_in_unbraced_linkage_specification_p;
16429 parser->num_template_parameter_lists
16430 = saved_num_template_parameter_lists;
16431 parser->in_function_body = saved_in_function_body;
16436 /* Parse a template-declaration, assuming that the `export' (and
16437 `extern') keywords, if present, has already been scanned. MEMBER_P
16438 is as for cp_parser_template_declaration. */
16441 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16443 tree decl = NULL_TREE;
16444 VEC (deferred_access_check,gc) *checks;
16445 tree parameter_list;
16446 bool friend_p = false;
16447 bool need_lang_pop;
16449 /* Look for the `template' keyword. */
16450 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16454 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16456 if (at_class_scope_p () && current_function_decl)
16458 /* 14.5.2.2 [temp.mem]
16460 A local class shall not have member templates. */
16461 error ("invalid declaration of member template in local class");
16462 cp_parser_skip_to_end_of_block_or_statement (parser);
16467 A template ... shall not have C linkage. */
16468 if (current_lang_name == lang_name_c)
16470 error ("template with C linkage");
16471 /* Give it C++ linkage to avoid confusing other parts of the
16473 push_lang_context (lang_name_cplusplus);
16474 need_lang_pop = true;
16477 need_lang_pop = false;
16479 /* We cannot perform access checks on the template parameter
16480 declarations until we know what is being declared, just as we
16481 cannot check the decl-specifier list. */
16482 push_deferring_access_checks (dk_deferred);
16484 /* If the next token is `>', then we have an invalid
16485 specialization. Rather than complain about an invalid template
16486 parameter, issue an error message here. */
16487 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16489 cp_parser_error (parser, "invalid explicit specialization");
16490 begin_specialization ();
16491 parameter_list = NULL_TREE;
16494 /* Parse the template parameters. */
16495 parameter_list = cp_parser_template_parameter_list (parser);
16497 /* Get the deferred access checks from the parameter list. These
16498 will be checked once we know what is being declared, as for a
16499 member template the checks must be performed in the scope of the
16500 class containing the member. */
16501 checks = get_deferred_access_checks ();
16503 /* Look for the `>'. */
16504 cp_parser_skip_to_end_of_template_parameter_list (parser);
16505 /* We just processed one more parameter list. */
16506 ++parser->num_template_parameter_lists;
16507 /* If the next token is `template', there are more template
16509 if (cp_lexer_next_token_is_keyword (parser->lexer,
16511 cp_parser_template_declaration_after_export (parser, member_p);
16514 /* There are no access checks when parsing a template, as we do not
16515 know if a specialization will be a friend. */
16516 push_deferring_access_checks (dk_no_check);
16517 decl = cp_parser_single_declaration (parser,
16520 /*explicit_specialization_p=*/false,
16522 pop_deferring_access_checks ();
16524 /* If this is a member template declaration, let the front
16526 if (member_p && !friend_p && decl)
16528 if (TREE_CODE (decl) == TYPE_DECL)
16529 cp_parser_check_access_in_redeclaration (decl);
16531 decl = finish_member_template_decl (decl);
16533 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16534 make_friend_class (current_class_type, TREE_TYPE (decl),
16535 /*complain=*/true);
16537 /* We are done with the current parameter list. */
16538 --parser->num_template_parameter_lists;
16540 pop_deferring_access_checks ();
16543 finish_template_decl (parameter_list);
16545 /* Register member declarations. */
16546 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16547 finish_member_declaration (decl);
16548 /* For the erroneous case of a template with C linkage, we pushed an
16549 implicit C++ linkage scope; exit that scope now. */
16551 pop_lang_context ();
16552 /* If DECL is a function template, we must return to parse it later.
16553 (Even though there is no definition, there might be default
16554 arguments that need handling.) */
16555 if (member_p && decl
16556 && (TREE_CODE (decl) == FUNCTION_DECL
16557 || DECL_FUNCTION_TEMPLATE_P (decl)))
16558 TREE_VALUE (parser->unparsed_functions_queues)
16559 = tree_cons (NULL_TREE, decl,
16560 TREE_VALUE (parser->unparsed_functions_queues));
16563 /* Perform the deferred access checks from a template-parameter-list.
16564 CHECKS is a TREE_LIST of access checks, as returned by
16565 get_deferred_access_checks. */
16568 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16570 ++processing_template_parmlist;
16571 perform_access_checks (checks);
16572 --processing_template_parmlist;
16575 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16576 `function-definition' sequence. MEMBER_P is true, this declaration
16577 appears in a class scope.
16579 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16580 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16583 cp_parser_single_declaration (cp_parser* parser,
16584 VEC (deferred_access_check,gc)* checks,
16586 bool explicit_specialization_p,
16589 int declares_class_or_enum;
16590 tree decl = NULL_TREE;
16591 cp_decl_specifier_seq decl_specifiers;
16592 bool function_definition_p = false;
16594 /* This function is only used when processing a template
16596 gcc_assert (innermost_scope_kind () == sk_template_parms
16597 || innermost_scope_kind () == sk_template_spec);
16599 /* Defer access checks until we know what is being declared. */
16600 push_deferring_access_checks (dk_deferred);
16602 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16604 cp_parser_decl_specifier_seq (parser,
16605 CP_PARSER_FLAGS_OPTIONAL,
16607 &declares_class_or_enum);
16609 *friend_p = cp_parser_friend_p (&decl_specifiers);
16611 /* There are no template typedefs. */
16612 if (decl_specifiers.specs[(int) ds_typedef])
16614 error ("template declaration of %qs", "typedef");
16615 decl = error_mark_node;
16618 /* Gather up the access checks that occurred the
16619 decl-specifier-seq. */
16620 stop_deferring_access_checks ();
16622 /* Check for the declaration of a template class. */
16623 if (declares_class_or_enum)
16625 if (cp_parser_declares_only_class_p (parser))
16627 decl = shadow_tag (&decl_specifiers);
16632 friend template <typename T> struct A<T>::B;
16635 A<T>::B will be represented by a TYPENAME_TYPE, and
16636 therefore not recognized by shadow_tag. */
16637 if (friend_p && *friend_p
16639 && decl_specifiers.type
16640 && TYPE_P (decl_specifiers.type))
16641 decl = decl_specifiers.type;
16643 if (decl && decl != error_mark_node)
16644 decl = TYPE_NAME (decl);
16646 decl = error_mark_node;
16648 /* Perform access checks for template parameters. */
16649 cp_parser_perform_template_parameter_access_checks (checks);
16652 /* If it's not a template class, try for a template function. If
16653 the next token is a `;', then this declaration does not declare
16654 anything. But, if there were errors in the decl-specifiers, then
16655 the error might well have come from an attempted class-specifier.
16656 In that case, there's no need to warn about a missing declarator. */
16658 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16659 || decl_specifiers.type != error_mark_node))
16661 decl = cp_parser_init_declarator (parser,
16664 /*function_definition_allowed_p=*/true,
16666 declares_class_or_enum,
16667 &function_definition_p);
16669 /* 7.1.1-1 [dcl.stc]
16671 A storage-class-specifier shall not be specified in an explicit
16672 specialization... */
16674 && explicit_specialization_p
16675 && decl_specifiers.storage_class != sc_none)
16677 error ("explicit template specialization cannot have a storage class");
16678 decl = error_mark_node;
16682 pop_deferring_access_checks ();
16684 /* Clear any current qualification; whatever comes next is the start
16685 of something new. */
16686 parser->scope = NULL_TREE;
16687 parser->qualifying_scope = NULL_TREE;
16688 parser->object_scope = NULL_TREE;
16689 /* Look for a trailing `;' after the declaration. */
16690 if (!function_definition_p
16691 && (decl == error_mark_node
16692 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16693 cp_parser_skip_to_end_of_block_or_statement (parser);
16698 /* Parse a cast-expression that is not the operand of a unary "&". */
16701 cp_parser_simple_cast_expression (cp_parser *parser)
16703 return cp_parser_cast_expression (parser, /*address_p=*/false,
16707 /* Parse a functional cast to TYPE. Returns an expression
16708 representing the cast. */
16711 cp_parser_functional_cast (cp_parser* parser, tree type)
16713 tree expression_list;
16717 = cp_parser_parenthesized_expression_list (parser, false,
16719 /*allow_expansion_p=*/true,
16720 /*non_constant_p=*/NULL);
16722 cast = build_functional_cast (type, expression_list);
16723 /* [expr.const]/1: In an integral constant expression "only type
16724 conversions to integral or enumeration type can be used". */
16725 if (TREE_CODE (type) == TYPE_DECL)
16726 type = TREE_TYPE (type);
16727 if (cast != error_mark_node
16728 && !cast_valid_in_integral_constant_expression_p (type)
16729 && (cp_parser_non_integral_constant_expression
16730 (parser, "a call to a constructor")))
16731 return error_mark_node;
16735 /* Save the tokens that make up the body of a member function defined
16736 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16737 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16738 specifiers applied to the declaration. Returns the FUNCTION_DECL
16739 for the member function. */
16742 cp_parser_save_member_function_body (cp_parser* parser,
16743 cp_decl_specifier_seq *decl_specifiers,
16744 cp_declarator *declarator,
16751 /* Create the function-declaration. */
16752 fn = start_method (decl_specifiers, declarator, attributes);
16753 /* If something went badly wrong, bail out now. */
16754 if (fn == error_mark_node)
16756 /* If there's a function-body, skip it. */
16757 if (cp_parser_token_starts_function_definition_p
16758 (cp_lexer_peek_token (parser->lexer)))
16759 cp_parser_skip_to_end_of_block_or_statement (parser);
16760 return error_mark_node;
16763 /* Remember it, if there default args to post process. */
16764 cp_parser_save_default_args (parser, fn);
16766 /* Save away the tokens that make up the body of the
16768 first = parser->lexer->next_token;
16769 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16770 /* Handle function try blocks. */
16771 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16772 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16773 last = parser->lexer->next_token;
16775 /* Save away the inline definition; we will process it when the
16776 class is complete. */
16777 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16778 DECL_PENDING_INLINE_P (fn) = 1;
16780 /* We need to know that this was defined in the class, so that
16781 friend templates are handled correctly. */
16782 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16784 /* We're done with the inline definition. */
16785 finish_method (fn);
16787 /* Add FN to the queue of functions to be parsed later. */
16788 TREE_VALUE (parser->unparsed_functions_queues)
16789 = tree_cons (NULL_TREE, fn,
16790 TREE_VALUE (parser->unparsed_functions_queues));
16795 /* Parse a template-argument-list, as well as the trailing ">" (but
16796 not the opening ">"). See cp_parser_template_argument_list for the
16800 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16804 tree saved_qualifying_scope;
16805 tree saved_object_scope;
16806 bool saved_greater_than_is_operator_p;
16807 bool saved_skip_evaluation;
16811 When parsing a template-id, the first non-nested `>' is taken as
16812 the end of the template-argument-list rather than a greater-than
16814 saved_greater_than_is_operator_p
16815 = parser->greater_than_is_operator_p;
16816 parser->greater_than_is_operator_p = false;
16817 /* Parsing the argument list may modify SCOPE, so we save it
16819 saved_scope = parser->scope;
16820 saved_qualifying_scope = parser->qualifying_scope;
16821 saved_object_scope = parser->object_scope;
16822 /* We need to evaluate the template arguments, even though this
16823 template-id may be nested within a "sizeof". */
16824 saved_skip_evaluation = skip_evaluation;
16825 skip_evaluation = false;
16826 /* Parse the template-argument-list itself. */
16827 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16828 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16829 arguments = NULL_TREE;
16831 arguments = cp_parser_template_argument_list (parser);
16832 /* Look for the `>' that ends the template-argument-list. If we find
16833 a '>>' instead, it's probably just a typo. */
16834 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16836 if (cxx_dialect != cxx98)
16838 /* In C++0x, a `>>' in a template argument list or cast
16839 expression is considered to be two separate `>'
16840 tokens. So, change the current token to a `>', but don't
16841 consume it: it will be consumed later when the outer
16842 template argument list (or cast expression) is parsed.
16843 Note that this replacement of `>' for `>>' is necessary
16844 even if we are parsing tentatively: in the tentative
16845 case, after calling
16846 cp_parser_enclosed_template_argument_list we will always
16847 throw away all of the template arguments and the first
16848 closing `>', either because the template argument list
16849 was erroneous or because we are replacing those tokens
16850 with a CPP_TEMPLATE_ID token. The second `>' (which will
16851 not have been thrown away) is needed either to close an
16852 outer template argument list or to complete a new-style
16854 cp_token *token = cp_lexer_peek_token (parser->lexer);
16855 token->type = CPP_GREATER;
16857 else if (!saved_greater_than_is_operator_p)
16859 /* If we're in a nested template argument list, the '>>' has
16860 to be a typo for '> >'. We emit the error message, but we
16861 continue parsing and we push a '>' as next token, so that
16862 the argument list will be parsed correctly. Note that the
16863 global source location is still on the token before the
16864 '>>', so we need to say explicitly where we want it. */
16865 cp_token *token = cp_lexer_peek_token (parser->lexer);
16866 error ("%H%<>>%> should be %<> >%> "
16867 "within a nested template argument list",
16870 token->type = CPP_GREATER;
16874 /* If this is not a nested template argument list, the '>>'
16875 is a typo for '>'. Emit an error message and continue.
16876 Same deal about the token location, but here we can get it
16877 right by consuming the '>>' before issuing the diagnostic. */
16878 cp_lexer_consume_token (parser->lexer);
16879 error ("spurious %<>>%>, use %<>%> to terminate "
16880 "a template argument list");
16884 cp_parser_skip_to_end_of_template_parameter_list (parser);
16885 /* The `>' token might be a greater-than operator again now. */
16886 parser->greater_than_is_operator_p
16887 = saved_greater_than_is_operator_p;
16888 /* Restore the SAVED_SCOPE. */
16889 parser->scope = saved_scope;
16890 parser->qualifying_scope = saved_qualifying_scope;
16891 parser->object_scope = saved_object_scope;
16892 skip_evaluation = saved_skip_evaluation;
16897 /* MEMBER_FUNCTION is a member function, or a friend. If default
16898 arguments, or the body of the function have not yet been parsed,
16902 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16904 /* If this member is a template, get the underlying
16906 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16907 member_function = DECL_TEMPLATE_RESULT (member_function);
16909 /* There should not be any class definitions in progress at this
16910 point; the bodies of members are only parsed outside of all class
16912 gcc_assert (parser->num_classes_being_defined == 0);
16913 /* While we're parsing the member functions we might encounter more
16914 classes. We want to handle them right away, but we don't want
16915 them getting mixed up with functions that are currently in the
16917 parser->unparsed_functions_queues
16918 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16920 /* Make sure that any template parameters are in scope. */
16921 maybe_begin_member_template_processing (member_function);
16923 /* If the body of the function has not yet been parsed, parse it
16925 if (DECL_PENDING_INLINE_P (member_function))
16927 tree function_scope;
16928 cp_token_cache *tokens;
16930 /* The function is no longer pending; we are processing it. */
16931 tokens = DECL_PENDING_INLINE_INFO (member_function);
16932 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16933 DECL_PENDING_INLINE_P (member_function) = 0;
16935 /* If this is a local class, enter the scope of the containing
16937 function_scope = current_function_decl;
16938 if (function_scope)
16939 push_function_context_to (function_scope);
16942 /* Push the body of the function onto the lexer stack. */
16943 cp_parser_push_lexer_for_tokens (parser, tokens);
16945 /* Let the front end know that we going to be defining this
16947 start_preparsed_function (member_function, NULL_TREE,
16948 SF_PRE_PARSED | SF_INCLASS_INLINE);
16950 /* Don't do access checking if it is a templated function. */
16951 if (processing_template_decl)
16952 push_deferring_access_checks (dk_no_check);
16954 /* Now, parse the body of the function. */
16955 cp_parser_function_definition_after_declarator (parser,
16956 /*inline_p=*/true);
16958 if (processing_template_decl)
16959 pop_deferring_access_checks ();
16961 /* Leave the scope of the containing function. */
16962 if (function_scope)
16963 pop_function_context_from (function_scope);
16964 cp_parser_pop_lexer (parser);
16967 /* Remove any template parameters from the symbol table. */
16968 maybe_end_member_template_processing ();
16970 /* Restore the queue. */
16971 parser->unparsed_functions_queues
16972 = TREE_CHAIN (parser->unparsed_functions_queues);
16975 /* If DECL contains any default args, remember it on the unparsed
16976 functions queue. */
16979 cp_parser_save_default_args (cp_parser* parser, tree decl)
16983 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
16985 probe = TREE_CHAIN (probe))
16986 if (TREE_PURPOSE (probe))
16988 TREE_PURPOSE (parser->unparsed_functions_queues)
16989 = tree_cons (current_class_type, decl,
16990 TREE_PURPOSE (parser->unparsed_functions_queues));
16995 /* FN is a FUNCTION_DECL which may contains a parameter with an
16996 unparsed DEFAULT_ARG. Parse the default args now. This function
16997 assumes that the current scope is the scope in which the default
16998 argument should be processed. */
17001 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17003 bool saved_local_variables_forbidden_p;
17006 /* While we're parsing the default args, we might (due to the
17007 statement expression extension) encounter more classes. We want
17008 to handle them right away, but we don't want them getting mixed
17009 up with default args that are currently in the queue. */
17010 parser->unparsed_functions_queues
17011 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17013 /* Local variable names (and the `this' keyword) may not appear
17014 in a default argument. */
17015 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17016 parser->local_variables_forbidden_p = true;
17018 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17020 parm = TREE_CHAIN (parm))
17022 cp_token_cache *tokens;
17023 tree default_arg = TREE_PURPOSE (parm);
17025 VEC(tree,gc) *insts;
17032 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17033 /* This can happen for a friend declaration for a function
17034 already declared with default arguments. */
17037 /* Push the saved tokens for the default argument onto the parser's
17039 tokens = DEFARG_TOKENS (default_arg);
17040 cp_parser_push_lexer_for_tokens (parser, tokens);
17042 /* Parse the assignment-expression. */
17043 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17045 if (!processing_template_decl)
17046 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17048 TREE_PURPOSE (parm) = parsed_arg;
17050 /* Update any instantiations we've already created. */
17051 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17052 VEC_iterate (tree, insts, ix, copy); ix++)
17053 TREE_PURPOSE (copy) = parsed_arg;
17055 /* If the token stream has not been completely used up, then
17056 there was extra junk after the end of the default
17058 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17059 cp_parser_error (parser, "expected %<,%>");
17061 /* Revert to the main lexer. */
17062 cp_parser_pop_lexer (parser);
17065 /* Make sure no default arg is missing. */
17066 check_default_args (fn);
17068 /* Restore the state of local_variables_forbidden_p. */
17069 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17071 /* Restore the queue. */
17072 parser->unparsed_functions_queues
17073 = TREE_CHAIN (parser->unparsed_functions_queues);
17076 /* Parse the operand of `sizeof' (or a similar operator). Returns
17077 either a TYPE or an expression, depending on the form of the
17078 input. The KEYWORD indicates which kind of expression we have
17082 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17084 static const char *format;
17085 tree expr = NULL_TREE;
17086 const char *saved_message;
17087 bool saved_integral_constant_expression_p;
17088 bool saved_non_integral_constant_expression_p;
17089 bool pack_expansion_p = false;
17091 /* Initialize FORMAT the first time we get here. */
17093 format = "types may not be defined in '%s' expressions";
17095 /* Types cannot be defined in a `sizeof' expression. Save away the
17097 saved_message = parser->type_definition_forbidden_message;
17098 /* And create the new one. */
17099 parser->type_definition_forbidden_message
17100 = XNEWVEC (const char, strlen (format)
17101 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17103 sprintf ((char *) parser->type_definition_forbidden_message,
17104 format, IDENTIFIER_POINTER (ridpointers[keyword]));
17106 /* The restrictions on constant-expressions do not apply inside
17107 sizeof expressions. */
17108 saved_integral_constant_expression_p
17109 = parser->integral_constant_expression_p;
17110 saved_non_integral_constant_expression_p
17111 = parser->non_integral_constant_expression_p;
17112 parser->integral_constant_expression_p = false;
17114 /* If it's a `...', then we are computing the length of a parameter
17116 if (keyword == RID_SIZEOF
17117 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17119 /* Consume the `...'. */
17120 cp_lexer_consume_token (parser->lexer);
17121 maybe_warn_variadic_templates ();
17123 /* Note that this is an expansion. */
17124 pack_expansion_p = true;
17127 /* Do not actually evaluate the expression. */
17129 /* If it's a `(', then we might be looking at the type-id
17131 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17134 bool saved_in_type_id_in_expr_p;
17136 /* We can't be sure yet whether we're looking at a type-id or an
17138 cp_parser_parse_tentatively (parser);
17139 /* Consume the `('. */
17140 cp_lexer_consume_token (parser->lexer);
17141 /* Parse the type-id. */
17142 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17143 parser->in_type_id_in_expr_p = true;
17144 type = cp_parser_type_id (parser);
17145 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17146 /* Now, look for the trailing `)'. */
17147 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17148 /* If all went well, then we're done. */
17149 if (cp_parser_parse_definitely (parser))
17151 cp_decl_specifier_seq decl_specs;
17153 /* Build a trivial decl-specifier-seq. */
17154 clear_decl_specs (&decl_specs);
17155 decl_specs.type = type;
17157 /* Call grokdeclarator to figure out what type this is. */
17158 expr = grokdeclarator (NULL,
17162 /*attrlist=*/NULL);
17166 /* If the type-id production did not work out, then we must be
17167 looking at the unary-expression production. */
17169 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17172 if (pack_expansion_p)
17173 /* Build a pack expansion. */
17174 expr = make_pack_expansion (expr);
17176 /* Go back to evaluating expressions. */
17179 /* Free the message we created. */
17180 free ((char *) parser->type_definition_forbidden_message);
17181 /* And restore the old one. */
17182 parser->type_definition_forbidden_message = saved_message;
17183 parser->integral_constant_expression_p
17184 = saved_integral_constant_expression_p;
17185 parser->non_integral_constant_expression_p
17186 = saved_non_integral_constant_expression_p;
17191 /* If the current declaration has no declarator, return true. */
17194 cp_parser_declares_only_class_p (cp_parser *parser)
17196 /* If the next token is a `;' or a `,' then there is no
17198 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17199 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17202 /* Update the DECL_SPECS to reflect the storage class indicated by
17206 cp_parser_set_storage_class (cp_parser *parser,
17207 cp_decl_specifier_seq *decl_specs,
17210 cp_storage_class storage_class;
17212 if (parser->in_unbraced_linkage_specification_p)
17214 error ("invalid use of %qD in linkage specification",
17215 ridpointers[keyword]);
17218 else if (decl_specs->storage_class != sc_none)
17220 decl_specs->conflicting_specifiers_p = true;
17224 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17225 && decl_specs->specs[(int) ds_thread])
17227 error ("%<__thread%> before %qD", ridpointers[keyword]);
17228 decl_specs->specs[(int) ds_thread] = 0;
17234 storage_class = sc_auto;
17237 storage_class = sc_register;
17240 storage_class = sc_static;
17243 storage_class = sc_extern;
17246 storage_class = sc_mutable;
17249 gcc_unreachable ();
17251 decl_specs->storage_class = storage_class;
17253 /* A storage class specifier cannot be applied alongside a typedef
17254 specifier. If there is a typedef specifier present then set
17255 conflicting_specifiers_p which will trigger an error later
17256 on in grokdeclarator. */
17257 if (decl_specs->specs[(int)ds_typedef])
17258 decl_specs->conflicting_specifiers_p = true;
17261 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17262 is true, the type is a user-defined type; otherwise it is a
17263 built-in type specified by a keyword. */
17266 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17268 bool user_defined_p)
17270 decl_specs->any_specifiers_p = true;
17272 /* If the user tries to redeclare bool or wchar_t (with, for
17273 example, in "typedef int wchar_t;") we remember that this is what
17274 happened. In system headers, we ignore these declarations so
17275 that G++ can work with system headers that are not C++-safe. */
17276 if (decl_specs->specs[(int) ds_typedef]
17278 && (type_spec == boolean_type_node
17279 || type_spec == wchar_type_node)
17280 && (decl_specs->type
17281 || decl_specs->specs[(int) ds_long]
17282 || decl_specs->specs[(int) ds_short]
17283 || decl_specs->specs[(int) ds_unsigned]
17284 || decl_specs->specs[(int) ds_signed]))
17286 decl_specs->redefined_builtin_type = type_spec;
17287 if (!decl_specs->type)
17289 decl_specs->type = type_spec;
17290 decl_specs->user_defined_type_p = false;
17293 else if (decl_specs->type)
17294 decl_specs->multiple_types_p = true;
17297 decl_specs->type = type_spec;
17298 decl_specs->user_defined_type_p = user_defined_p;
17299 decl_specs->redefined_builtin_type = NULL_TREE;
17303 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17304 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17307 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17309 return decl_specifiers->specs[(int) ds_friend] != 0;
17312 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17313 issue an error message indicating that TOKEN_DESC was expected.
17315 Returns the token consumed, if the token had the appropriate type.
17316 Otherwise, returns NULL. */
17319 cp_parser_require (cp_parser* parser,
17320 enum cpp_ttype type,
17321 const char* token_desc)
17323 if (cp_lexer_next_token_is (parser->lexer, type))
17324 return cp_lexer_consume_token (parser->lexer);
17327 /* Output the MESSAGE -- unless we're parsing tentatively. */
17328 if (!cp_parser_simulate_error (parser))
17330 char *message = concat ("expected ", token_desc, NULL);
17331 cp_parser_error (parser, message);
17338 /* An error message is produced if the next token is not '>'.
17339 All further tokens are skipped until the desired token is
17340 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17343 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17345 /* Current level of '< ... >'. */
17346 unsigned level = 0;
17347 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17348 unsigned nesting_depth = 0;
17350 /* Are we ready, yet? If not, issue error message. */
17351 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17354 /* Skip tokens until the desired token is found. */
17357 /* Peek at the next token. */
17358 switch (cp_lexer_peek_token (parser->lexer)->type)
17361 if (!nesting_depth)
17366 if (cxx_dialect == cxx98)
17367 /* C++0x views the `>>' operator as two `>' tokens, but
17370 else if (!nesting_depth && level-- == 0)
17372 /* We've hit a `>>' where the first `>' closes the
17373 template argument list, and the second `>' is
17374 spurious. Just consume the `>>' and stop; we've
17375 already produced at least one error. */
17376 cp_lexer_consume_token (parser->lexer);
17379 /* Fall through for C++0x, so we handle the second `>' in
17383 if (!nesting_depth && level-- == 0)
17385 /* We've reached the token we want, consume it and stop. */
17386 cp_lexer_consume_token (parser->lexer);
17391 case CPP_OPEN_PAREN:
17392 case CPP_OPEN_SQUARE:
17396 case CPP_CLOSE_PAREN:
17397 case CPP_CLOSE_SQUARE:
17398 if (nesting_depth-- == 0)
17403 case CPP_PRAGMA_EOL:
17404 case CPP_SEMICOLON:
17405 case CPP_OPEN_BRACE:
17406 case CPP_CLOSE_BRACE:
17407 /* The '>' was probably forgotten, don't look further. */
17414 /* Consume this token. */
17415 cp_lexer_consume_token (parser->lexer);
17419 /* If the next token is the indicated keyword, consume it. Otherwise,
17420 issue an error message indicating that TOKEN_DESC was expected.
17422 Returns the token consumed, if the token had the appropriate type.
17423 Otherwise, returns NULL. */
17426 cp_parser_require_keyword (cp_parser* parser,
17428 const char* token_desc)
17430 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17432 if (token && token->keyword != keyword)
17434 dyn_string_t error_msg;
17436 /* Format the error message. */
17437 error_msg = dyn_string_new (0);
17438 dyn_string_append_cstr (error_msg, "expected ");
17439 dyn_string_append_cstr (error_msg, token_desc);
17440 cp_parser_error (parser, error_msg->s);
17441 dyn_string_delete (error_msg);
17448 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17449 function-definition. */
17452 cp_parser_token_starts_function_definition_p (cp_token* token)
17454 return (/* An ordinary function-body begins with an `{'. */
17455 token->type == CPP_OPEN_BRACE
17456 /* A ctor-initializer begins with a `:'. */
17457 || token->type == CPP_COLON
17458 /* A function-try-block begins with `try'. */
17459 || token->keyword == RID_TRY
17460 /* The named return value extension begins with `return'. */
17461 || token->keyword == RID_RETURN);
17464 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17468 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17472 token = cp_lexer_peek_token (parser->lexer);
17473 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17476 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17477 C++0x) ending a template-argument. */
17480 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17484 token = cp_lexer_peek_token (parser->lexer);
17485 return (token->type == CPP_COMMA
17486 || token->type == CPP_GREATER
17487 || token->type == CPP_ELLIPSIS
17488 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17491 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17492 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17495 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17500 token = cp_lexer_peek_nth_token (parser->lexer, n);
17501 if (token->type == CPP_LESS)
17503 /* Check for the sequence `<::' in the original code. It would be lexed as
17504 `[:', where `[' is a digraph, and there is no whitespace before
17506 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17509 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17510 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17516 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17517 or none_type otherwise. */
17519 static enum tag_types
17520 cp_parser_token_is_class_key (cp_token* token)
17522 switch (token->keyword)
17527 return record_type;
17536 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17539 cp_parser_check_class_key (enum tag_types class_key, tree type)
17541 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17542 pedwarn ("%qs tag used in naming %q#T",
17543 class_key == union_type ? "union"
17544 : class_key == record_type ? "struct" : "class",
17548 /* Issue an error message if DECL is redeclared with different
17549 access than its original declaration [class.access.spec/3].
17550 This applies to nested classes and nested class templates.
17554 cp_parser_check_access_in_redeclaration (tree decl)
17556 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17559 if ((TREE_PRIVATE (decl)
17560 != (current_access_specifier == access_private_node))
17561 || (TREE_PROTECTED (decl)
17562 != (current_access_specifier == access_protected_node)))
17563 error ("%qD redeclared with different access", decl);
17566 /* Look for the `template' keyword, as a syntactic disambiguator.
17567 Return TRUE iff it is present, in which case it will be
17571 cp_parser_optional_template_keyword (cp_parser *parser)
17573 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17575 /* The `template' keyword can only be used within templates;
17576 outside templates the parser can always figure out what is a
17577 template and what is not. */
17578 if (!processing_template_decl)
17580 error ("%<template%> (as a disambiguator) is only allowed "
17581 "within templates");
17582 /* If this part of the token stream is rescanned, the same
17583 error message would be generated. So, we purge the token
17584 from the stream. */
17585 cp_lexer_purge_token (parser->lexer);
17590 /* Consume the `template' keyword. */
17591 cp_lexer_consume_token (parser->lexer);
17599 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17600 set PARSER->SCOPE, and perform other related actions. */
17603 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17606 struct tree_check *check_value;
17607 deferred_access_check *chk;
17608 VEC (deferred_access_check,gc) *checks;
17610 /* Get the stored value. */
17611 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17612 /* Perform any access checks that were deferred. */
17613 checks = check_value->checks;
17617 VEC_iterate (deferred_access_check, checks, i, chk) ;
17620 perform_or_defer_access_check (chk->binfo,
17625 /* Set the scope from the stored value. */
17626 parser->scope = check_value->value;
17627 parser->qualifying_scope = check_value->qualifying_scope;
17628 parser->object_scope = NULL_TREE;
17631 /* Consume tokens up through a non-nested END token. */
17634 cp_parser_cache_group (cp_parser *parser,
17635 enum cpp_ttype end,
17642 /* Abort a parenthesized expression if we encounter a brace. */
17643 if ((end == CPP_CLOSE_PAREN || depth == 0)
17644 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17646 /* If we've reached the end of the file, stop. */
17647 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17648 || (end != CPP_PRAGMA_EOL
17649 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17651 /* Consume the next token. */
17652 token = cp_lexer_consume_token (parser->lexer);
17653 /* See if it starts a new group. */
17654 if (token->type == CPP_OPEN_BRACE)
17656 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17660 else if (token->type == CPP_OPEN_PAREN)
17661 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17662 else if (token->type == CPP_PRAGMA)
17663 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17664 else if (token->type == end)
17669 /* Begin parsing tentatively. We always save tokens while parsing
17670 tentatively so that if the tentative parsing fails we can restore the
17674 cp_parser_parse_tentatively (cp_parser* parser)
17676 /* Enter a new parsing context. */
17677 parser->context = cp_parser_context_new (parser->context);
17678 /* Begin saving tokens. */
17679 cp_lexer_save_tokens (parser->lexer);
17680 /* In order to avoid repetitive access control error messages,
17681 access checks are queued up until we are no longer parsing
17683 push_deferring_access_checks (dk_deferred);
17686 /* Commit to the currently active tentative parse. */
17689 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17691 cp_parser_context *context;
17694 /* Mark all of the levels as committed. */
17695 lexer = parser->lexer;
17696 for (context = parser->context; context->next; context = context->next)
17698 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17700 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17701 while (!cp_lexer_saving_tokens (lexer))
17702 lexer = lexer->next;
17703 cp_lexer_commit_tokens (lexer);
17707 /* Abort the currently active tentative parse. All consumed tokens
17708 will be rolled back, and no diagnostics will be issued. */
17711 cp_parser_abort_tentative_parse (cp_parser* parser)
17713 cp_parser_simulate_error (parser);
17714 /* Now, pretend that we want to see if the construct was
17715 successfully parsed. */
17716 cp_parser_parse_definitely (parser);
17719 /* Stop parsing tentatively. If a parse error has occurred, restore the
17720 token stream. Otherwise, commit to the tokens we have consumed.
17721 Returns true if no error occurred; false otherwise. */
17724 cp_parser_parse_definitely (cp_parser* parser)
17726 bool error_occurred;
17727 cp_parser_context *context;
17729 /* Remember whether or not an error occurred, since we are about to
17730 destroy that information. */
17731 error_occurred = cp_parser_error_occurred (parser);
17732 /* Remove the topmost context from the stack. */
17733 context = parser->context;
17734 parser->context = context->next;
17735 /* If no parse errors occurred, commit to the tentative parse. */
17736 if (!error_occurred)
17738 /* Commit to the tokens read tentatively, unless that was
17740 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17741 cp_lexer_commit_tokens (parser->lexer);
17743 pop_to_parent_deferring_access_checks ();
17745 /* Otherwise, if errors occurred, roll back our state so that things
17746 are just as they were before we began the tentative parse. */
17749 cp_lexer_rollback_tokens (parser->lexer);
17750 pop_deferring_access_checks ();
17752 /* Add the context to the front of the free list. */
17753 context->next = cp_parser_context_free_list;
17754 cp_parser_context_free_list = context;
17756 return !error_occurred;
17759 /* Returns true if we are parsing tentatively and are not committed to
17760 this tentative parse. */
17763 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17765 return (cp_parser_parsing_tentatively (parser)
17766 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17769 /* Returns nonzero iff an error has occurred during the most recent
17770 tentative parse. */
17773 cp_parser_error_occurred (cp_parser* parser)
17775 return (cp_parser_parsing_tentatively (parser)
17776 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17779 /* Returns nonzero if GNU extensions are allowed. */
17782 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17784 return parser->allow_gnu_extensions_p;
17787 /* Objective-C++ Productions */
17790 /* Parse an Objective-C expression, which feeds into a primary-expression
17794 objc-message-expression
17795 objc-string-literal
17796 objc-encode-expression
17797 objc-protocol-expression
17798 objc-selector-expression
17800 Returns a tree representation of the expression. */
17803 cp_parser_objc_expression (cp_parser* parser)
17805 /* Try to figure out what kind of declaration is present. */
17806 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17810 case CPP_OPEN_SQUARE:
17811 return cp_parser_objc_message_expression (parser);
17813 case CPP_OBJC_STRING:
17814 kwd = cp_lexer_consume_token (parser->lexer);
17815 return objc_build_string_object (kwd->u.value);
17818 switch (kwd->keyword)
17820 case RID_AT_ENCODE:
17821 return cp_parser_objc_encode_expression (parser);
17823 case RID_AT_PROTOCOL:
17824 return cp_parser_objc_protocol_expression (parser);
17826 case RID_AT_SELECTOR:
17827 return cp_parser_objc_selector_expression (parser);
17833 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17834 cp_parser_skip_to_end_of_block_or_statement (parser);
17837 return error_mark_node;
17840 /* Parse an Objective-C message expression.
17842 objc-message-expression:
17843 [ objc-message-receiver objc-message-args ]
17845 Returns a representation of an Objective-C message. */
17848 cp_parser_objc_message_expression (cp_parser* parser)
17850 tree receiver, messageargs;
17852 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17853 receiver = cp_parser_objc_message_receiver (parser);
17854 messageargs = cp_parser_objc_message_args (parser);
17855 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17857 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17860 /* Parse an objc-message-receiver.
17862 objc-message-receiver:
17864 simple-type-specifier
17866 Returns a representation of the type or expression. */
17869 cp_parser_objc_message_receiver (cp_parser* parser)
17873 /* An Objective-C message receiver may be either (1) a type
17874 or (2) an expression. */
17875 cp_parser_parse_tentatively (parser);
17876 rcv = cp_parser_expression (parser, false);
17878 if (cp_parser_parse_definitely (parser))
17881 rcv = cp_parser_simple_type_specifier (parser,
17882 /*decl_specs=*/NULL,
17883 CP_PARSER_FLAGS_NONE);
17885 return objc_get_class_reference (rcv);
17888 /* Parse the arguments and selectors comprising an Objective-C message.
17893 objc-selector-args , objc-comma-args
17895 objc-selector-args:
17896 objc-selector [opt] : assignment-expression
17897 objc-selector-args objc-selector [opt] : assignment-expression
17900 assignment-expression
17901 objc-comma-args , assignment-expression
17903 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17904 selector arguments and TREE_VALUE containing a list of comma
17908 cp_parser_objc_message_args (cp_parser* parser)
17910 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17911 bool maybe_unary_selector_p = true;
17912 cp_token *token = cp_lexer_peek_token (parser->lexer);
17914 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17916 tree selector = NULL_TREE, arg;
17918 if (token->type != CPP_COLON)
17919 selector = cp_parser_objc_selector (parser);
17921 /* Detect if we have a unary selector. */
17922 if (maybe_unary_selector_p
17923 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17924 return build_tree_list (selector, NULL_TREE);
17926 maybe_unary_selector_p = false;
17927 cp_parser_require (parser, CPP_COLON, "`:'");
17928 arg = cp_parser_assignment_expression (parser, false);
17931 = chainon (sel_args,
17932 build_tree_list (selector, arg));
17934 token = cp_lexer_peek_token (parser->lexer);
17937 /* Handle non-selector arguments, if any. */
17938 while (token->type == CPP_COMMA)
17942 cp_lexer_consume_token (parser->lexer);
17943 arg = cp_parser_assignment_expression (parser, false);
17946 = chainon (addl_args,
17947 build_tree_list (NULL_TREE, arg));
17949 token = cp_lexer_peek_token (parser->lexer);
17952 return build_tree_list (sel_args, addl_args);
17955 /* Parse an Objective-C encode expression.
17957 objc-encode-expression:
17958 @encode objc-typename
17960 Returns an encoded representation of the type argument. */
17963 cp_parser_objc_encode_expression (cp_parser* parser)
17967 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17968 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17969 type = complete_type (cp_parser_type_id (parser));
17970 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17974 error ("%<@encode%> must specify a type as an argument");
17975 return error_mark_node;
17978 return objc_build_encode_expr (type);
17981 /* Parse an Objective-C @defs expression. */
17984 cp_parser_objc_defs_expression (cp_parser *parser)
17988 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
17989 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17990 name = cp_parser_identifier (parser);
17991 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17993 return objc_get_class_ivars (name);
17996 /* Parse an Objective-C protocol expression.
17998 objc-protocol-expression:
17999 @protocol ( identifier )
18001 Returns a representation of the protocol expression. */
18004 cp_parser_objc_protocol_expression (cp_parser* parser)
18008 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18009 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18010 proto = cp_parser_identifier (parser);
18011 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18013 return objc_build_protocol_expr (proto);
18016 /* Parse an Objective-C selector expression.
18018 objc-selector-expression:
18019 @selector ( objc-method-signature )
18021 objc-method-signature:
18027 objc-selector-seq objc-selector :
18029 Returns a representation of the method selector. */
18032 cp_parser_objc_selector_expression (cp_parser* parser)
18034 tree sel_seq = NULL_TREE;
18035 bool maybe_unary_selector_p = true;
18038 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18039 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18040 token = cp_lexer_peek_token (parser->lexer);
18042 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18043 || token->type == CPP_SCOPE)
18045 tree selector = NULL_TREE;
18047 if (token->type != CPP_COLON
18048 || token->type == CPP_SCOPE)
18049 selector = cp_parser_objc_selector (parser);
18051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18052 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18054 /* Detect if we have a unary selector. */
18055 if (maybe_unary_selector_p)
18057 sel_seq = selector;
18058 goto finish_selector;
18062 cp_parser_error (parser, "expected %<:%>");
18065 maybe_unary_selector_p = false;
18066 token = cp_lexer_consume_token (parser->lexer);
18068 if (token->type == CPP_SCOPE)
18071 = chainon (sel_seq,
18072 build_tree_list (selector, NULL_TREE));
18074 = chainon (sel_seq,
18075 build_tree_list (NULL_TREE, NULL_TREE));
18079 = chainon (sel_seq,
18080 build_tree_list (selector, NULL_TREE));
18082 token = cp_lexer_peek_token (parser->lexer);
18086 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18088 return objc_build_selector_expr (sel_seq);
18091 /* Parse a list of identifiers.
18093 objc-identifier-list:
18095 objc-identifier-list , identifier
18097 Returns a TREE_LIST of identifier nodes. */
18100 cp_parser_objc_identifier_list (cp_parser* parser)
18102 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18103 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18105 while (sep->type == CPP_COMMA)
18107 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18108 list = chainon (list,
18109 build_tree_list (NULL_TREE,
18110 cp_parser_identifier (parser)));
18111 sep = cp_lexer_peek_token (parser->lexer);
18117 /* Parse an Objective-C alias declaration.
18119 objc-alias-declaration:
18120 @compatibility_alias identifier identifier ;
18122 This function registers the alias mapping with the Objective-C front end.
18123 It returns nothing. */
18126 cp_parser_objc_alias_declaration (cp_parser* parser)
18130 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18131 alias = cp_parser_identifier (parser);
18132 orig = cp_parser_identifier (parser);
18133 objc_declare_alias (alias, orig);
18134 cp_parser_consume_semicolon_at_end_of_statement (parser);
18137 /* Parse an Objective-C class forward-declaration.
18139 objc-class-declaration:
18140 @class objc-identifier-list ;
18142 The function registers the forward declarations with the Objective-C
18143 front end. It returns nothing. */
18146 cp_parser_objc_class_declaration (cp_parser* parser)
18148 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18149 objc_declare_class (cp_parser_objc_identifier_list (parser));
18150 cp_parser_consume_semicolon_at_end_of_statement (parser);
18153 /* Parse a list of Objective-C protocol references.
18155 objc-protocol-refs-opt:
18156 objc-protocol-refs [opt]
18158 objc-protocol-refs:
18159 < objc-identifier-list >
18161 Returns a TREE_LIST of identifiers, if any. */
18164 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18166 tree protorefs = NULL_TREE;
18168 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18170 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18171 protorefs = cp_parser_objc_identifier_list (parser);
18172 cp_parser_require (parser, CPP_GREATER, "`>'");
18178 /* Parse a Objective-C visibility specification. */
18181 cp_parser_objc_visibility_spec (cp_parser* parser)
18183 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18185 switch (vis->keyword)
18187 case RID_AT_PRIVATE:
18188 objc_set_visibility (2);
18190 case RID_AT_PROTECTED:
18191 objc_set_visibility (0);
18193 case RID_AT_PUBLIC:
18194 objc_set_visibility (1);
18200 /* Eat '@private'/'@protected'/'@public'. */
18201 cp_lexer_consume_token (parser->lexer);
18204 /* Parse an Objective-C method type. */
18207 cp_parser_objc_method_type (cp_parser* parser)
18209 objc_set_method_type
18210 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18215 /* Parse an Objective-C protocol qualifier. */
18218 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18220 tree quals = NULL_TREE, node;
18221 cp_token *token = cp_lexer_peek_token (parser->lexer);
18223 node = token->u.value;
18225 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18226 && (node == ridpointers [(int) RID_IN]
18227 || node == ridpointers [(int) RID_OUT]
18228 || node == ridpointers [(int) RID_INOUT]
18229 || node == ridpointers [(int) RID_BYCOPY]
18230 || node == ridpointers [(int) RID_BYREF]
18231 || node == ridpointers [(int) RID_ONEWAY]))
18233 quals = tree_cons (NULL_TREE, node, quals);
18234 cp_lexer_consume_token (parser->lexer);
18235 token = cp_lexer_peek_token (parser->lexer);
18236 node = token->u.value;
18242 /* Parse an Objective-C typename. */
18245 cp_parser_objc_typename (cp_parser* parser)
18247 tree typename = NULL_TREE;
18249 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18251 tree proto_quals, cp_type = NULL_TREE;
18253 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18254 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18256 /* An ObjC type name may consist of just protocol qualifiers, in which
18257 case the type shall default to 'id'. */
18258 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18259 cp_type = cp_parser_type_id (parser);
18261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18262 typename = build_tree_list (proto_quals, cp_type);
18268 /* Check to see if TYPE refers to an Objective-C selector name. */
18271 cp_parser_objc_selector_p (enum cpp_ttype type)
18273 return (type == CPP_NAME || type == CPP_KEYWORD
18274 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18275 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18276 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18277 || type == CPP_XOR || type == CPP_XOR_EQ);
18280 /* Parse an Objective-C selector. */
18283 cp_parser_objc_selector (cp_parser* parser)
18285 cp_token *token = cp_lexer_consume_token (parser->lexer);
18287 if (!cp_parser_objc_selector_p (token->type))
18289 error ("invalid Objective-C++ selector name");
18290 return error_mark_node;
18293 /* C++ operator names are allowed to appear in ObjC selectors. */
18294 switch (token->type)
18296 case CPP_AND_AND: return get_identifier ("and");
18297 case CPP_AND_EQ: return get_identifier ("and_eq");
18298 case CPP_AND: return get_identifier ("bitand");
18299 case CPP_OR: return get_identifier ("bitor");
18300 case CPP_COMPL: return get_identifier ("compl");
18301 case CPP_NOT: return get_identifier ("not");
18302 case CPP_NOT_EQ: return get_identifier ("not_eq");
18303 case CPP_OR_OR: return get_identifier ("or");
18304 case CPP_OR_EQ: return get_identifier ("or_eq");
18305 case CPP_XOR: return get_identifier ("xor");
18306 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18307 default: return token->u.value;
18311 /* Parse an Objective-C params list. */
18314 cp_parser_objc_method_keyword_params (cp_parser* parser)
18316 tree params = NULL_TREE;
18317 bool maybe_unary_selector_p = true;
18318 cp_token *token = cp_lexer_peek_token (parser->lexer);
18320 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18322 tree selector = NULL_TREE, typename, identifier;
18324 if (token->type != CPP_COLON)
18325 selector = cp_parser_objc_selector (parser);
18327 /* Detect if we have a unary selector. */
18328 if (maybe_unary_selector_p
18329 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18332 maybe_unary_selector_p = false;
18333 cp_parser_require (parser, CPP_COLON, "`:'");
18334 typename = cp_parser_objc_typename (parser);
18335 identifier = cp_parser_identifier (parser);
18339 objc_build_keyword_decl (selector,
18343 token = cp_lexer_peek_token (parser->lexer);
18349 /* Parse the non-keyword Objective-C params. */
18352 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18354 tree params = make_node (TREE_LIST);
18355 cp_token *token = cp_lexer_peek_token (parser->lexer);
18356 *ellipsisp = false; /* Initially, assume no ellipsis. */
18358 while (token->type == CPP_COMMA)
18360 cp_parameter_declarator *parmdecl;
18363 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18364 token = cp_lexer_peek_token (parser->lexer);
18366 if (token->type == CPP_ELLIPSIS)
18368 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18373 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18374 parm = grokdeclarator (parmdecl->declarator,
18375 &parmdecl->decl_specifiers,
18376 PARM, /*initialized=*/0,
18377 /*attrlist=*/NULL);
18379 chainon (params, build_tree_list (NULL_TREE, parm));
18380 token = cp_lexer_peek_token (parser->lexer);
18386 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18389 cp_parser_objc_interstitial_code (cp_parser* parser)
18391 cp_token *token = cp_lexer_peek_token (parser->lexer);
18393 /* If the next token is `extern' and the following token is a string
18394 literal, then we have a linkage specification. */
18395 if (token->keyword == RID_EXTERN
18396 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18397 cp_parser_linkage_specification (parser);
18398 /* Handle #pragma, if any. */
18399 else if (token->type == CPP_PRAGMA)
18400 cp_parser_pragma (parser, pragma_external);
18401 /* Allow stray semicolons. */
18402 else if (token->type == CPP_SEMICOLON)
18403 cp_lexer_consume_token (parser->lexer);
18404 /* Finally, try to parse a block-declaration, or a function-definition. */
18406 cp_parser_block_declaration (parser, /*statement_p=*/false);
18409 /* Parse a method signature. */
18412 cp_parser_objc_method_signature (cp_parser* parser)
18414 tree rettype, kwdparms, optparms;
18415 bool ellipsis = false;
18417 cp_parser_objc_method_type (parser);
18418 rettype = cp_parser_objc_typename (parser);
18419 kwdparms = cp_parser_objc_method_keyword_params (parser);
18420 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18422 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18425 /* Pars an Objective-C method prototype list. */
18428 cp_parser_objc_method_prototype_list (cp_parser* parser)
18430 cp_token *token = cp_lexer_peek_token (parser->lexer);
18432 while (token->keyword != RID_AT_END)
18434 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18436 objc_add_method_declaration
18437 (cp_parser_objc_method_signature (parser));
18438 cp_parser_consume_semicolon_at_end_of_statement (parser);
18441 /* Allow for interspersed non-ObjC++ code. */
18442 cp_parser_objc_interstitial_code (parser);
18444 token = cp_lexer_peek_token (parser->lexer);
18447 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18448 objc_finish_interface ();
18451 /* Parse an Objective-C method definition list. */
18454 cp_parser_objc_method_definition_list (cp_parser* parser)
18456 cp_token *token = cp_lexer_peek_token (parser->lexer);
18458 while (token->keyword != RID_AT_END)
18462 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18464 push_deferring_access_checks (dk_deferred);
18465 objc_start_method_definition
18466 (cp_parser_objc_method_signature (parser));
18468 /* For historical reasons, we accept an optional semicolon. */
18469 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18470 cp_lexer_consume_token (parser->lexer);
18472 perform_deferred_access_checks ();
18473 stop_deferring_access_checks ();
18474 meth = cp_parser_function_definition_after_declarator (parser,
18476 pop_deferring_access_checks ();
18477 objc_finish_method_definition (meth);
18480 /* Allow for interspersed non-ObjC++ code. */
18481 cp_parser_objc_interstitial_code (parser);
18483 token = cp_lexer_peek_token (parser->lexer);
18486 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18487 objc_finish_implementation ();
18490 /* Parse Objective-C ivars. */
18493 cp_parser_objc_class_ivars (cp_parser* parser)
18495 cp_token *token = cp_lexer_peek_token (parser->lexer);
18497 if (token->type != CPP_OPEN_BRACE)
18498 return; /* No ivars specified. */
18500 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18501 token = cp_lexer_peek_token (parser->lexer);
18503 while (token->type != CPP_CLOSE_BRACE)
18505 cp_decl_specifier_seq declspecs;
18506 int decl_class_or_enum_p;
18507 tree prefix_attributes;
18509 cp_parser_objc_visibility_spec (parser);
18511 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18514 cp_parser_decl_specifier_seq (parser,
18515 CP_PARSER_FLAGS_OPTIONAL,
18517 &decl_class_or_enum_p);
18518 prefix_attributes = declspecs.attributes;
18519 declspecs.attributes = NULL_TREE;
18521 /* Keep going until we hit the `;' at the end of the
18523 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18525 tree width = NULL_TREE, attributes, first_attribute, decl;
18526 cp_declarator *declarator = NULL;
18527 int ctor_dtor_or_conv_p;
18529 /* Check for a (possibly unnamed) bitfield declaration. */
18530 token = cp_lexer_peek_token (parser->lexer);
18531 if (token->type == CPP_COLON)
18534 if (token->type == CPP_NAME
18535 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18538 /* Get the name of the bitfield. */
18539 declarator = make_id_declarator (NULL_TREE,
18540 cp_parser_identifier (parser),
18544 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18545 /* Get the width of the bitfield. */
18547 = cp_parser_constant_expression (parser,
18548 /*allow_non_constant=*/false,
18553 /* Parse the declarator. */
18555 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18556 &ctor_dtor_or_conv_p,
18557 /*parenthesized_p=*/NULL,
18558 /*member_p=*/false);
18561 /* Look for attributes that apply to the ivar. */
18562 attributes = cp_parser_attributes_opt (parser);
18563 /* Remember which attributes are prefix attributes and
18565 first_attribute = attributes;
18566 /* Combine the attributes. */
18567 attributes = chainon (prefix_attributes, attributes);
18571 /* Create the bitfield declaration. */
18572 decl = grokbitfield (declarator, &declspecs, width);
18573 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18576 decl = grokfield (declarator, &declspecs,
18577 NULL_TREE, /*init_const_expr_p=*/false,
18578 NULL_TREE, attributes);
18580 /* Add the instance variable. */
18581 objc_add_instance_variable (decl);
18583 /* Reset PREFIX_ATTRIBUTES. */
18584 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18585 attributes = TREE_CHAIN (attributes);
18587 TREE_CHAIN (attributes) = NULL_TREE;
18589 token = cp_lexer_peek_token (parser->lexer);
18591 if (token->type == CPP_COMMA)
18593 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18599 cp_parser_consume_semicolon_at_end_of_statement (parser);
18600 token = cp_lexer_peek_token (parser->lexer);
18603 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18604 /* For historical reasons, we accept an optional semicolon. */
18605 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18606 cp_lexer_consume_token (parser->lexer);
18609 /* Parse an Objective-C protocol declaration. */
18612 cp_parser_objc_protocol_declaration (cp_parser* parser)
18614 tree proto, protorefs;
18617 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18618 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18620 error ("identifier expected after %<@protocol%>");
18624 /* See if we have a forward declaration or a definition. */
18625 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18627 /* Try a forward declaration first. */
18628 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18630 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18632 cp_parser_consume_semicolon_at_end_of_statement (parser);
18635 /* Ok, we got a full-fledged definition (or at least should). */
18638 proto = cp_parser_identifier (parser);
18639 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18640 objc_start_protocol (proto, protorefs);
18641 cp_parser_objc_method_prototype_list (parser);
18645 /* Parse an Objective-C superclass or category. */
18648 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18651 cp_token *next = cp_lexer_peek_token (parser->lexer);
18653 *super = *categ = NULL_TREE;
18654 if (next->type == CPP_COLON)
18656 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18657 *super = cp_parser_identifier (parser);
18659 else if (next->type == CPP_OPEN_PAREN)
18661 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18662 *categ = cp_parser_identifier (parser);
18663 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18667 /* Parse an Objective-C class interface. */
18670 cp_parser_objc_class_interface (cp_parser* parser)
18672 tree name, super, categ, protos;
18674 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18675 name = cp_parser_identifier (parser);
18676 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18677 protos = cp_parser_objc_protocol_refs_opt (parser);
18679 /* We have either a class or a category on our hands. */
18681 objc_start_category_interface (name, categ, protos);
18684 objc_start_class_interface (name, super, protos);
18685 /* Handle instance variable declarations, if any. */
18686 cp_parser_objc_class_ivars (parser);
18687 objc_continue_interface ();
18690 cp_parser_objc_method_prototype_list (parser);
18693 /* Parse an Objective-C class implementation. */
18696 cp_parser_objc_class_implementation (cp_parser* parser)
18698 tree name, super, categ;
18700 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18701 name = cp_parser_identifier (parser);
18702 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18704 /* We have either a class or a category on our hands. */
18706 objc_start_category_implementation (name, categ);
18709 objc_start_class_implementation (name, super);
18710 /* Handle instance variable declarations, if any. */
18711 cp_parser_objc_class_ivars (parser);
18712 objc_continue_implementation ();
18715 cp_parser_objc_method_definition_list (parser);
18718 /* Consume the @end token and finish off the implementation. */
18721 cp_parser_objc_end_implementation (cp_parser* parser)
18723 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18724 objc_finish_implementation ();
18727 /* Parse an Objective-C declaration. */
18730 cp_parser_objc_declaration (cp_parser* parser)
18732 /* Try to figure out what kind of declaration is present. */
18733 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18735 switch (kwd->keyword)
18738 cp_parser_objc_alias_declaration (parser);
18741 cp_parser_objc_class_declaration (parser);
18743 case RID_AT_PROTOCOL:
18744 cp_parser_objc_protocol_declaration (parser);
18746 case RID_AT_INTERFACE:
18747 cp_parser_objc_class_interface (parser);
18749 case RID_AT_IMPLEMENTATION:
18750 cp_parser_objc_class_implementation (parser);
18753 cp_parser_objc_end_implementation (parser);
18756 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18757 cp_parser_skip_to_end_of_block_or_statement (parser);
18761 /* Parse an Objective-C try-catch-finally statement.
18763 objc-try-catch-finally-stmt:
18764 @try compound-statement objc-catch-clause-seq [opt]
18765 objc-finally-clause [opt]
18767 objc-catch-clause-seq:
18768 objc-catch-clause objc-catch-clause-seq [opt]
18771 @catch ( exception-declaration ) compound-statement
18773 objc-finally-clause
18774 @finally compound-statement
18776 Returns NULL_TREE. */
18779 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18780 location_t location;
18783 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18784 location = cp_lexer_peek_token (parser->lexer)->location;
18785 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18786 node, lest it get absorbed into the surrounding block. */
18787 stmt = push_stmt_list ();
18788 cp_parser_compound_statement (parser, NULL, false);
18789 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18791 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18793 cp_parameter_declarator *parmdecl;
18796 cp_lexer_consume_token (parser->lexer);
18797 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18798 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18799 parm = grokdeclarator (parmdecl->declarator,
18800 &parmdecl->decl_specifiers,
18801 PARM, /*initialized=*/0,
18802 /*attrlist=*/NULL);
18803 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18804 objc_begin_catch_clause (parm);
18805 cp_parser_compound_statement (parser, NULL, false);
18806 objc_finish_catch_clause ();
18809 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18811 cp_lexer_consume_token (parser->lexer);
18812 location = cp_lexer_peek_token (parser->lexer)->location;
18813 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18814 node, lest it get absorbed into the surrounding block. */
18815 stmt = push_stmt_list ();
18816 cp_parser_compound_statement (parser, NULL, false);
18817 objc_build_finally_clause (location, pop_stmt_list (stmt));
18820 return objc_finish_try_stmt ();
18823 /* Parse an Objective-C synchronized statement.
18825 objc-synchronized-stmt:
18826 @synchronized ( expression ) compound-statement
18828 Returns NULL_TREE. */
18831 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18832 location_t location;
18835 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18837 location = cp_lexer_peek_token (parser->lexer)->location;
18838 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18839 lock = cp_parser_expression (parser, false);
18840 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18842 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18843 node, lest it get absorbed into the surrounding block. */
18844 stmt = push_stmt_list ();
18845 cp_parser_compound_statement (parser, NULL, false);
18847 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18850 /* Parse an Objective-C throw statement.
18853 @throw assignment-expression [opt] ;
18855 Returns a constructed '@throw' statement. */
18858 cp_parser_objc_throw_statement (cp_parser *parser) {
18859 tree expr = NULL_TREE;
18861 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18863 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18864 expr = cp_parser_assignment_expression (parser, false);
18866 cp_parser_consume_semicolon_at_end_of_statement (parser);
18868 return objc_build_throw_stmt (expr);
18871 /* Parse an Objective-C statement. */
18874 cp_parser_objc_statement (cp_parser * parser) {
18875 /* Try to figure out what kind of declaration is present. */
18876 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18878 switch (kwd->keyword)
18881 return cp_parser_objc_try_catch_finally_statement (parser);
18882 case RID_AT_SYNCHRONIZED:
18883 return cp_parser_objc_synchronized_statement (parser);
18885 return cp_parser_objc_throw_statement (parser);
18887 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18888 cp_parser_skip_to_end_of_block_or_statement (parser);
18891 return error_mark_node;
18894 /* OpenMP 2.5 parsing routines. */
18896 /* Returns name of the next clause.
18897 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18898 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18899 returned and the token is consumed. */
18901 static pragma_omp_clause
18902 cp_parser_omp_clause_name (cp_parser *parser)
18904 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18906 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18907 result = PRAGMA_OMP_CLAUSE_IF;
18908 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18909 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18910 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18911 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18912 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18914 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18915 const char *p = IDENTIFIER_POINTER (id);
18920 if (!strcmp ("copyin", p))
18921 result = PRAGMA_OMP_CLAUSE_COPYIN;
18922 else if (!strcmp ("copyprivate", p))
18923 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18926 if (!strcmp ("firstprivate", p))
18927 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18930 if (!strcmp ("lastprivate", p))
18931 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18934 if (!strcmp ("nowait", p))
18935 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18936 else if (!strcmp ("num_threads", p))
18937 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18940 if (!strcmp ("ordered", p))
18941 result = PRAGMA_OMP_CLAUSE_ORDERED;
18944 if (!strcmp ("reduction", p))
18945 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18948 if (!strcmp ("schedule", p))
18949 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18950 else if (!strcmp ("shared", p))
18951 result = PRAGMA_OMP_CLAUSE_SHARED;
18956 if (result != PRAGMA_OMP_CLAUSE_NONE)
18957 cp_lexer_consume_token (parser->lexer);
18962 /* Validate that a clause of the given type does not already exist. */
18965 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18969 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18970 if (OMP_CLAUSE_CODE (c) == code)
18972 error ("too many %qs clauses", name);
18980 variable-list , identifier
18982 In addition, we match a closing parenthesis. An opening parenthesis
18983 will have been consumed by the caller.
18985 If KIND is nonzero, create the appropriate node and install the decl
18986 in OMP_CLAUSE_DECL and add the node to the head of the list.
18988 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
18989 return the list created. */
18992 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
18999 name = cp_parser_id_expression (parser, /*template_p=*/false,
19000 /*check_dependency_p=*/true,
19001 /*template_p=*/NULL,
19002 /*declarator_p=*/false,
19003 /*optional_p=*/false);
19004 if (name == error_mark_node)
19007 decl = cp_parser_lookup_name_simple (parser, name);
19008 if (decl == error_mark_node)
19009 cp_parser_name_lookup_error (parser, name, decl, NULL);
19010 else if (kind != 0)
19012 tree u = build_omp_clause (kind);
19013 OMP_CLAUSE_DECL (u) = decl;
19014 OMP_CLAUSE_CHAIN (u) = list;
19018 list = tree_cons (decl, NULL_TREE, list);
19021 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19023 cp_lexer_consume_token (parser->lexer);
19026 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19030 /* Try to resync to an unnested comma. Copied from
19031 cp_parser_parenthesized_expression_list. */
19033 ending = cp_parser_skip_to_closing_parenthesis (parser,
19034 /*recovering=*/true,
19036 /*consume_paren=*/true);
19044 /* Similarly, but expect leading and trailing parenthesis. This is a very
19045 common case for omp clauses. */
19048 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19050 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19051 return cp_parser_omp_var_list_no_open (parser, kind, list);
19056 default ( shared | none ) */
19059 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19061 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19064 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19066 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19068 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19069 const char *p = IDENTIFIER_POINTER (id);
19074 if (strcmp ("none", p) != 0)
19076 kind = OMP_CLAUSE_DEFAULT_NONE;
19080 if (strcmp ("shared", p) != 0)
19082 kind = OMP_CLAUSE_DEFAULT_SHARED;
19089 cp_lexer_consume_token (parser->lexer);
19094 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19097 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19098 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19099 /*or_comma=*/false,
19100 /*consume_paren=*/true);
19102 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19105 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19106 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19107 OMP_CLAUSE_CHAIN (c) = list;
19108 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19114 if ( expression ) */
19117 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19121 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19124 t = cp_parser_condition (parser);
19126 if (t == error_mark_node
19127 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19128 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19129 /*or_comma=*/false,
19130 /*consume_paren=*/true);
19132 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19134 c = build_omp_clause (OMP_CLAUSE_IF);
19135 OMP_CLAUSE_IF_EXPR (c) = t;
19136 OMP_CLAUSE_CHAIN (c) = list;
19145 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19149 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19151 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19152 OMP_CLAUSE_CHAIN (c) = list;
19157 num_threads ( expression ) */
19160 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19164 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19167 t = cp_parser_expression (parser, false);
19169 if (t == error_mark_node
19170 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19171 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19172 /*or_comma=*/false,
19173 /*consume_paren=*/true);
19175 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19177 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19178 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19179 OMP_CLAUSE_CHAIN (c) = list;
19188 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19192 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19194 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19195 OMP_CLAUSE_CHAIN (c) = list;
19200 reduction ( reduction-operator : variable-list )
19202 reduction-operator:
19203 One of: + * - & ^ | && || */
19206 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19208 enum tree_code code;
19211 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19214 switch (cp_lexer_peek_token (parser->lexer)->type)
19226 code = BIT_AND_EXPR;
19229 code = BIT_XOR_EXPR;
19232 code = BIT_IOR_EXPR;
19235 code = TRUTH_ANDIF_EXPR;
19238 code = TRUTH_ORIF_EXPR;
19241 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19243 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19244 /*or_comma=*/false,
19245 /*consume_paren=*/true);
19248 cp_lexer_consume_token (parser->lexer);
19250 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19253 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19254 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19255 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19261 schedule ( schedule-kind )
19262 schedule ( schedule-kind , expression )
19265 static | dynamic | guided | runtime */
19268 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19272 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19275 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19279 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19280 const char *p = IDENTIFIER_POINTER (id);
19285 if (strcmp ("dynamic", p) != 0)
19287 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19291 if (strcmp ("guided", p) != 0)
19293 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19297 if (strcmp ("runtime", p) != 0)
19299 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19306 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19307 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19310 cp_lexer_consume_token (parser->lexer);
19312 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19314 cp_lexer_consume_token (parser->lexer);
19316 t = cp_parser_assignment_expression (parser, false);
19318 if (t == error_mark_node)
19320 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19321 error ("schedule %<runtime%> does not take "
19322 "a %<chunk_size%> parameter");
19324 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19326 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19329 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19332 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19333 OMP_CLAUSE_CHAIN (c) = list;
19337 cp_parser_error (parser, "invalid schedule kind");
19339 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19340 /*or_comma=*/false,
19341 /*consume_paren=*/true);
19345 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19346 is a bitmask in MASK. Return the list of clauses found; the result
19347 of clause default goes in *pdefault. */
19350 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19351 const char *where, cp_token *pragma_tok)
19353 tree clauses = NULL;
19355 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19357 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19358 const char *c_name;
19359 tree prev = clauses;
19363 case PRAGMA_OMP_CLAUSE_COPYIN:
19364 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19367 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19368 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19370 c_name = "copyprivate";
19372 case PRAGMA_OMP_CLAUSE_DEFAULT:
19373 clauses = cp_parser_omp_clause_default (parser, clauses);
19374 c_name = "default";
19376 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19377 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19379 c_name = "firstprivate";
19381 case PRAGMA_OMP_CLAUSE_IF:
19382 clauses = cp_parser_omp_clause_if (parser, clauses);
19385 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19386 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19388 c_name = "lastprivate";
19390 case PRAGMA_OMP_CLAUSE_NOWAIT:
19391 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19394 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19395 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19396 c_name = "num_threads";
19398 case PRAGMA_OMP_CLAUSE_ORDERED:
19399 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19400 c_name = "ordered";
19402 case PRAGMA_OMP_CLAUSE_PRIVATE:
19403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19405 c_name = "private";
19407 case PRAGMA_OMP_CLAUSE_REDUCTION:
19408 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19409 c_name = "reduction";
19411 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19412 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19413 c_name = "schedule";
19415 case PRAGMA_OMP_CLAUSE_SHARED:
19416 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19421 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19425 if (((mask >> c_kind) & 1) == 0)
19427 /* Remove the invalid clause(s) from the list to avoid
19428 confusing the rest of the compiler. */
19430 error ("%qs is not valid for %qs", c_name, where);
19434 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19435 return finish_omp_clauses (clauses);
19442 In practice, we're also interested in adding the statement to an
19443 outer node. So it is convenient if we work around the fact that
19444 cp_parser_statement calls add_stmt. */
19447 cp_parser_begin_omp_structured_block (cp_parser *parser)
19449 unsigned save = parser->in_statement;
19451 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19452 This preserves the "not within loop or switch" style error messages
19453 for nonsense cases like
19459 if (parser->in_statement)
19460 parser->in_statement = IN_OMP_BLOCK;
19466 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19468 parser->in_statement = save;
19472 cp_parser_omp_structured_block (cp_parser *parser)
19474 tree stmt = begin_omp_structured_block ();
19475 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19477 cp_parser_statement (parser, NULL_TREE, false, NULL);
19479 cp_parser_end_omp_structured_block (parser, save);
19480 return finish_omp_structured_block (stmt);
19484 # pragma omp atomic new-line
19488 x binop= expr | x++ | ++x | x-- | --x
19490 +, *, -, /, &, ^, |, <<, >>
19492 where x is an lvalue expression with scalar type. */
19495 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19498 enum tree_code code;
19500 cp_parser_require_pragma_eol (parser, pragma_tok);
19502 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19504 switch (TREE_CODE (lhs))
19509 case PREINCREMENT_EXPR:
19510 case POSTINCREMENT_EXPR:
19511 lhs = TREE_OPERAND (lhs, 0);
19513 rhs = integer_one_node;
19516 case PREDECREMENT_EXPR:
19517 case POSTDECREMENT_EXPR:
19518 lhs = TREE_OPERAND (lhs, 0);
19520 rhs = integer_one_node;
19524 switch (cp_lexer_peek_token (parser->lexer)->type)
19530 code = TRUNC_DIV_EXPR;
19538 case CPP_LSHIFT_EQ:
19539 code = LSHIFT_EXPR;
19541 case CPP_RSHIFT_EQ:
19542 code = RSHIFT_EXPR;
19545 code = BIT_AND_EXPR;
19548 code = BIT_IOR_EXPR;
19551 code = BIT_XOR_EXPR;
19554 cp_parser_error (parser,
19555 "invalid operator for %<#pragma omp atomic%>");
19558 cp_lexer_consume_token (parser->lexer);
19560 rhs = cp_parser_expression (parser, false);
19561 if (rhs == error_mark_node)
19565 finish_omp_atomic (code, lhs, rhs);
19566 cp_parser_consume_semicolon_at_end_of_statement (parser);
19570 cp_parser_skip_to_end_of_block_or_statement (parser);
19575 # pragma omp barrier new-line */
19578 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19580 cp_parser_require_pragma_eol (parser, pragma_tok);
19581 finish_omp_barrier ();
19585 # pragma omp critical [(name)] new-line
19586 structured-block */
19589 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19591 tree stmt, name = NULL;
19593 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19595 cp_lexer_consume_token (parser->lexer);
19597 name = cp_parser_identifier (parser);
19599 if (name == error_mark_node
19600 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19601 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19602 /*or_comma=*/false,
19603 /*consume_paren=*/true);
19604 if (name == error_mark_node)
19607 cp_parser_require_pragma_eol (parser, pragma_tok);
19609 stmt = cp_parser_omp_structured_block (parser);
19610 return c_finish_omp_critical (stmt, name);
19614 # pragma omp flush flush-vars[opt] new-line
19617 ( variable-list ) */
19620 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19622 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19623 (void) cp_parser_omp_var_list (parser, 0, NULL);
19624 cp_parser_require_pragma_eol (parser, pragma_tok);
19626 finish_omp_flush ();
19629 /* Parse the restricted form of the for statment allowed by OpenMP. */
19632 cp_parser_omp_for_loop (cp_parser *parser)
19634 tree init, cond, incr, body, decl, pre_body;
19637 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19639 cp_parser_error (parser, "for statement expected");
19642 loc = cp_lexer_consume_token (parser->lexer)->location;
19643 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19646 init = decl = NULL;
19647 pre_body = push_stmt_list ();
19648 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19650 cp_decl_specifier_seq type_specifiers;
19652 /* First, try to parse as an initialized declaration. See
19653 cp_parser_condition, from whence the bulk of this is copied. */
19655 cp_parser_parse_tentatively (parser);
19656 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19658 if (!cp_parser_error_occurred (parser))
19660 tree asm_specification, attributes;
19661 cp_declarator *declarator;
19663 declarator = cp_parser_declarator (parser,
19664 CP_PARSER_DECLARATOR_NAMED,
19665 /*ctor_dtor_or_conv_p=*/NULL,
19666 /*parenthesized_p=*/NULL,
19667 /*member_p=*/false);
19668 attributes = cp_parser_attributes_opt (parser);
19669 asm_specification = cp_parser_asm_specification_opt (parser);
19671 cp_parser_require (parser, CPP_EQ, "`='");
19672 if (cp_parser_parse_definitely (parser))
19676 decl = start_decl (declarator, &type_specifiers,
19677 /*initialized_p=*/false, attributes,
19678 /*prefix_attributes=*/NULL_TREE,
19681 init = cp_parser_assignment_expression (parser, false);
19683 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19684 asm_specification, LOOKUP_ONLYCONVERTING);
19687 pop_scope (pushed_scope);
19691 cp_parser_abort_tentative_parse (parser);
19693 /* If parsing as an initialized declaration failed, try again as
19694 a simple expression. */
19696 init = cp_parser_expression (parser, false);
19698 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19699 pre_body = pop_stmt_list (pre_body);
19702 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19703 cond = cp_parser_condition (parser);
19704 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19707 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19708 incr = cp_parser_expression (parser, false);
19710 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19711 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19712 /*or_comma=*/false,
19713 /*consume_paren=*/true);
19715 /* Note that we saved the original contents of this flag when we entered
19716 the structured block, and so we don't need to re-save it here. */
19717 parser->in_statement = IN_OMP_FOR;
19719 /* Note that the grammar doesn't call for a structured block here,
19720 though the loop as a whole is a structured block. */
19721 body = push_stmt_list ();
19722 cp_parser_statement (parser, NULL_TREE, false, NULL);
19723 body = pop_stmt_list (body);
19725 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19729 #pragma omp for for-clause[optseq] new-line
19732 #define OMP_FOR_CLAUSE_MASK \
19733 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19734 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19735 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19736 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19737 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19738 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19739 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19742 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19744 tree clauses, sb, ret;
19747 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19748 "#pragma omp for", pragma_tok);
19750 sb = begin_omp_structured_block ();
19751 save = cp_parser_begin_omp_structured_block (parser);
19753 ret = cp_parser_omp_for_loop (parser);
19755 OMP_FOR_CLAUSES (ret) = clauses;
19757 cp_parser_end_omp_structured_block (parser, save);
19758 add_stmt (finish_omp_structured_block (sb));
19764 # pragma omp master new-line
19765 structured-block */
19768 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19770 cp_parser_require_pragma_eol (parser, pragma_tok);
19771 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19775 # pragma omp ordered new-line
19776 structured-block */
19779 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19781 cp_parser_require_pragma_eol (parser, pragma_tok);
19782 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19788 { section-sequence }
19791 section-directive[opt] structured-block
19792 section-sequence section-directive structured-block */
19795 cp_parser_omp_sections_scope (cp_parser *parser)
19797 tree stmt, substmt;
19798 bool error_suppress = false;
19801 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19804 stmt = push_stmt_list ();
19806 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19810 substmt = begin_omp_structured_block ();
19811 save = cp_parser_begin_omp_structured_block (parser);
19815 cp_parser_statement (parser, NULL_TREE, false, NULL);
19817 tok = cp_lexer_peek_token (parser->lexer);
19818 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19820 if (tok->type == CPP_CLOSE_BRACE)
19822 if (tok->type == CPP_EOF)
19826 cp_parser_end_omp_structured_block (parser, save);
19827 substmt = finish_omp_structured_block (substmt);
19828 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19829 add_stmt (substmt);
19834 tok = cp_lexer_peek_token (parser->lexer);
19835 if (tok->type == CPP_CLOSE_BRACE)
19837 if (tok->type == CPP_EOF)
19840 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19842 cp_lexer_consume_token (parser->lexer);
19843 cp_parser_require_pragma_eol (parser, tok);
19844 error_suppress = false;
19846 else if (!error_suppress)
19848 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19849 error_suppress = true;
19852 substmt = cp_parser_omp_structured_block (parser);
19853 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19854 add_stmt (substmt);
19856 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19858 substmt = pop_stmt_list (stmt);
19860 stmt = make_node (OMP_SECTIONS);
19861 TREE_TYPE (stmt) = void_type_node;
19862 OMP_SECTIONS_BODY (stmt) = substmt;
19869 # pragma omp sections sections-clause[optseq] newline
19872 #define OMP_SECTIONS_CLAUSE_MASK \
19873 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19874 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19875 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19876 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19877 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19880 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19884 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19885 "#pragma omp sections", pragma_tok);
19887 ret = cp_parser_omp_sections_scope (parser);
19889 OMP_SECTIONS_CLAUSES (ret) = clauses;
19895 # pragma parallel parallel-clause new-line
19896 # pragma parallel for parallel-for-clause new-line
19897 # pragma parallel sections parallel-sections-clause new-line */
19899 #define OMP_PARALLEL_CLAUSE_MASK \
19900 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19901 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19902 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19903 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19904 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19905 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19906 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19907 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19910 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19912 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19913 const char *p_name = "#pragma omp parallel";
19914 tree stmt, clauses, par_clause, ws_clause, block;
19915 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19918 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19920 cp_lexer_consume_token (parser->lexer);
19921 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19922 p_name = "#pragma omp parallel for";
19923 mask |= OMP_FOR_CLAUSE_MASK;
19924 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19926 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19928 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19929 const char *p = IDENTIFIER_POINTER (id);
19930 if (strcmp (p, "sections") == 0)
19932 cp_lexer_consume_token (parser->lexer);
19933 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19934 p_name = "#pragma omp parallel sections";
19935 mask |= OMP_SECTIONS_CLAUSE_MASK;
19936 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19940 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19941 block = begin_omp_parallel ();
19942 save = cp_parser_begin_omp_structured_block (parser);
19946 case PRAGMA_OMP_PARALLEL:
19947 cp_parser_already_scoped_statement (parser);
19948 par_clause = clauses;
19951 case PRAGMA_OMP_PARALLEL_FOR:
19952 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19953 stmt = cp_parser_omp_for_loop (parser);
19955 OMP_FOR_CLAUSES (stmt) = ws_clause;
19958 case PRAGMA_OMP_PARALLEL_SECTIONS:
19959 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19960 stmt = cp_parser_omp_sections_scope (parser);
19962 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19966 gcc_unreachable ();
19969 cp_parser_end_omp_structured_block (parser, save);
19970 stmt = finish_omp_parallel (par_clause, block);
19971 if (p_kind != PRAGMA_OMP_PARALLEL)
19972 OMP_PARALLEL_COMBINED (stmt) = 1;
19977 # pragma omp single single-clause[optseq] new-line
19978 structured-block */
19980 #define OMP_SINGLE_CLAUSE_MASK \
19981 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19982 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19983 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
19984 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19987 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
19989 tree stmt = make_node (OMP_SINGLE);
19990 TREE_TYPE (stmt) = void_type_node;
19992 OMP_SINGLE_CLAUSES (stmt)
19993 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
19994 "#pragma omp single", pragma_tok);
19995 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
19997 return add_stmt (stmt);
20001 # pragma omp threadprivate (variable-list) */
20004 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20008 vars = cp_parser_omp_var_list (parser, 0, NULL);
20009 cp_parser_require_pragma_eol (parser, pragma_tok);
20011 finish_omp_threadprivate (vars);
20014 /* Main entry point to OpenMP statement pragmas. */
20017 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20021 switch (pragma_tok->pragma_kind)
20023 case PRAGMA_OMP_ATOMIC:
20024 cp_parser_omp_atomic (parser, pragma_tok);
20026 case PRAGMA_OMP_CRITICAL:
20027 stmt = cp_parser_omp_critical (parser, pragma_tok);
20029 case PRAGMA_OMP_FOR:
20030 stmt = cp_parser_omp_for (parser, pragma_tok);
20032 case PRAGMA_OMP_MASTER:
20033 stmt = cp_parser_omp_master (parser, pragma_tok);
20035 case PRAGMA_OMP_ORDERED:
20036 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20038 case PRAGMA_OMP_PARALLEL:
20039 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20041 case PRAGMA_OMP_SECTIONS:
20042 stmt = cp_parser_omp_sections (parser, pragma_tok);
20044 case PRAGMA_OMP_SINGLE:
20045 stmt = cp_parser_omp_single (parser, pragma_tok);
20048 gcc_unreachable ();
20052 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20057 static GTY (()) cp_parser *the_parser;
20060 /* Special handling for the first token or line in the file. The first
20061 thing in the file might be #pragma GCC pch_preprocess, which loads a
20062 PCH file, which is a GC collection point. So we need to handle this
20063 first pragma without benefit of an existing lexer structure.
20065 Always returns one token to the caller in *FIRST_TOKEN. This is
20066 either the true first token of the file, or the first token after
20067 the initial pragma. */
20070 cp_parser_initial_pragma (cp_token *first_token)
20074 cp_lexer_get_preprocessor_token (NULL, first_token);
20075 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20078 cp_lexer_get_preprocessor_token (NULL, first_token);
20079 if (first_token->type == CPP_STRING)
20081 name = first_token->u.value;
20083 cp_lexer_get_preprocessor_token (NULL, first_token);
20084 if (first_token->type != CPP_PRAGMA_EOL)
20085 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20088 error ("expected string literal");
20090 /* Skip to the end of the pragma. */
20091 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20092 cp_lexer_get_preprocessor_token (NULL, first_token);
20094 /* Now actually load the PCH file. */
20096 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20098 /* Read one more token to return to our caller. We have to do this
20099 after reading the PCH file in, since its pointers have to be
20101 cp_lexer_get_preprocessor_token (NULL, first_token);
20104 /* Normal parsing of a pragma token. Here we can (and must) use the
20108 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20110 cp_token *pragma_tok;
20113 pragma_tok = cp_lexer_consume_token (parser->lexer);
20114 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20115 parser->lexer->in_pragma = true;
20117 id = pragma_tok->pragma_kind;
20120 case PRAGMA_GCC_PCH_PREPROCESS:
20121 error ("%<#pragma GCC pch_preprocess%> must be first");
20124 case PRAGMA_OMP_BARRIER:
20127 case pragma_compound:
20128 cp_parser_omp_barrier (parser, pragma_tok);
20131 error ("%<#pragma omp barrier%> may only be "
20132 "used in compound statements");
20139 case PRAGMA_OMP_FLUSH:
20142 case pragma_compound:
20143 cp_parser_omp_flush (parser, pragma_tok);
20146 error ("%<#pragma omp flush%> may only be "
20147 "used in compound statements");
20154 case PRAGMA_OMP_THREADPRIVATE:
20155 cp_parser_omp_threadprivate (parser, pragma_tok);
20158 case PRAGMA_OMP_ATOMIC:
20159 case PRAGMA_OMP_CRITICAL:
20160 case PRAGMA_OMP_FOR:
20161 case PRAGMA_OMP_MASTER:
20162 case PRAGMA_OMP_ORDERED:
20163 case PRAGMA_OMP_PARALLEL:
20164 case PRAGMA_OMP_SECTIONS:
20165 case PRAGMA_OMP_SINGLE:
20166 if (context == pragma_external)
20168 cp_parser_omp_construct (parser, pragma_tok);
20171 case PRAGMA_OMP_SECTION:
20172 error ("%<#pragma omp section%> may only be used in "
20173 "%<#pragma omp sections%> construct");
20177 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20178 c_invoke_pragma_handler (id);
20182 cp_parser_error (parser, "expected declaration specifiers");
20186 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20190 /* The interface the pragma parsers have to the lexer. */
20193 pragma_lex (tree *value)
20196 enum cpp_ttype ret;
20198 tok = cp_lexer_peek_token (the_parser->lexer);
20201 *value = tok->u.value;
20203 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20205 else if (ret == CPP_STRING)
20206 *value = cp_parser_string_literal (the_parser, false, false);
20209 cp_lexer_consume_token (the_parser->lexer);
20210 if (ret == CPP_KEYWORD)
20218 /* External interface. */
20220 /* Parse one entire translation unit. */
20223 c_parse_file (void)
20225 bool error_occurred;
20226 static bool already_called = false;
20228 if (already_called)
20230 sorry ("inter-module optimizations not implemented for C++");
20233 already_called = true;
20235 the_parser = cp_parser_new ();
20236 push_deferring_access_checks (flag_access_control
20237 ? dk_no_deferred : dk_no_check);
20238 error_occurred = cp_parser_translation_unit (the_parser);
20242 #include "gt-cp-parser.h"