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 /* Forward declarations of nested types, such as
10921 are invalid unless all components preceding the final '::'
10922 are complete. If all enclosing types are complete, these
10923 declarations become merely pointless.
10925 Invalid forward declarations of nested types are errors
10926 caught elsewhere in parsing. Those that are pointless arrive
10929 if (cp_parser_declares_only_class_p (parser)
10930 && !is_friend && !processing_explicit_instantiation)
10931 warning (0, "declaration %qD does not declare anything", decl);
10933 type = TREE_TYPE (decl);
10937 /* An elaborated-type-specifier sometimes introduces a new type and
10938 sometimes names an existing type. Normally, the rule is that it
10939 introduces a new type only if there is not an existing type of
10940 the same name already in scope. For example, given:
10943 void f() { struct S s; }
10945 the `struct S' in the body of `f' is the same `struct S' as in
10946 the global scope; the existing definition is used. However, if
10947 there were no global declaration, this would introduce a new
10948 local class named `S'.
10950 An exception to this rule applies to the following code:
10952 namespace N { struct S; }
10954 Here, the elaborated-type-specifier names a new type
10955 unconditionally; even if there is already an `S' in the
10956 containing scope this declaration names a new type.
10957 This exception only applies if the elaborated-type-specifier
10958 forms the complete declaration:
10962 A declaration consisting solely of `class-key identifier ;' is
10963 either a redeclaration of the name in the current scope or a
10964 forward declaration of the identifier as a class name. It
10965 introduces the name into the current scope.
10967 We are in this situation precisely when the next token is a `;'.
10969 An exception to the exception is that a `friend' declaration does
10970 *not* name a new type; i.e., given:
10972 struct S { friend struct T; };
10974 `T' is not a new type in the scope of `S'.
10976 Also, `new struct S' or `sizeof (struct S)' never results in the
10977 definition of a new type; a new type can only be declared in a
10978 declaration context. */
10984 /* Friends have special name lookup rules. */
10985 ts = ts_within_enclosing_non_class;
10986 else if (is_declaration
10987 && cp_lexer_next_token_is (parser->lexer,
10989 /* This is a `class-key identifier ;' */
10995 (parser->num_template_parameter_lists
10996 && (cp_parser_next_token_starts_class_definition_p (parser)
10997 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
10998 /* An unqualified name was used to reference this type, so
10999 there were no qualifying templates. */
11000 if (!cp_parser_check_template_parameters (parser,
11001 /*num_templates=*/0))
11002 return error_mark_node;
11003 type = xref_tag (tag_type, identifier, ts, template_p);
11007 if (type == error_mark_node)
11008 return error_mark_node;
11010 /* Allow attributes on forward declarations of classes. */
11013 if (TREE_CODE (type) == TYPENAME_TYPE)
11014 warning (OPT_Wattributes,
11015 "attributes ignored on uninstantiated type");
11016 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11017 && ! processing_explicit_instantiation)
11018 warning (OPT_Wattributes,
11019 "attributes ignored on template instantiation");
11020 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11021 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11023 warning (OPT_Wattributes,
11024 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11027 if (tag_type != enum_type)
11028 cp_parser_check_class_key (tag_type, type);
11030 /* A "<" cannot follow an elaborated type specifier. If that
11031 happens, the user was probably trying to form a template-id. */
11032 cp_parser_check_for_invalid_template_id (parser, type);
11037 /* Parse an enum-specifier.
11040 enum identifier [opt] { enumerator-list [opt] }
11043 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11046 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11047 if the token stream isn't an enum-specifier after all. */
11050 cp_parser_enum_specifier (cp_parser* parser)
11056 /* Parse tentatively so that we can back up if we don't find a
11058 cp_parser_parse_tentatively (parser);
11060 /* Caller guarantees that the current token is 'enum', an identifier
11061 possibly follows, and the token after that is an opening brace.
11062 If we don't have an identifier, fabricate an anonymous name for
11063 the enumeration being defined. */
11064 cp_lexer_consume_token (parser->lexer);
11066 attributes = cp_parser_attributes_opt (parser);
11068 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11069 identifier = cp_parser_identifier (parser);
11071 identifier = make_anon_name ();
11073 /* Look for the `{' but don't consume it yet. */
11074 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11075 cp_parser_simulate_error (parser);
11077 if (!cp_parser_parse_definitely (parser))
11080 /* Issue an error message if type-definitions are forbidden here. */
11081 if (!cp_parser_check_type_definition (parser))
11082 type = error_mark_node;
11084 /* Create the new type. We do this before consuming the opening
11085 brace so the enum will be recorded as being on the line of its
11086 tag (or the 'enum' keyword, if there is no tag). */
11087 type = start_enum (identifier);
11089 /* Consume the opening brace. */
11090 cp_lexer_consume_token (parser->lexer);
11092 if (type == error_mark_node)
11094 cp_parser_skip_to_end_of_block_or_statement (parser);
11095 return error_mark_node;
11098 /* If the next token is not '}', then there are some enumerators. */
11099 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11100 cp_parser_enumerator_list (parser, type);
11102 /* Consume the final '}'. */
11103 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11105 /* Look for trailing attributes to apply to this enumeration, and
11106 apply them if appropriate. */
11107 if (cp_parser_allow_gnu_extensions_p (parser))
11109 tree trailing_attr = cp_parser_attributes_opt (parser);
11110 cplus_decl_attributes (&type,
11112 (int) ATTR_FLAG_TYPE_IN_PLACE);
11115 /* Finish up the enumeration. */
11116 finish_enum (type);
11121 /* Parse an enumerator-list. The enumerators all have the indicated
11125 enumerator-definition
11126 enumerator-list , enumerator-definition */
11129 cp_parser_enumerator_list (cp_parser* parser, tree type)
11133 /* Parse an enumerator-definition. */
11134 cp_parser_enumerator_definition (parser, type);
11136 /* If the next token is not a ',', we've reached the end of
11138 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11140 /* Otherwise, consume the `,' and keep going. */
11141 cp_lexer_consume_token (parser->lexer);
11142 /* If the next token is a `}', there is a trailing comma. */
11143 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11145 if (pedantic && !in_system_header)
11146 pedwarn ("comma at end of enumerator list");
11152 /* Parse an enumerator-definition. The enumerator has the indicated
11155 enumerator-definition:
11157 enumerator = constant-expression
11163 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11168 /* Look for the identifier. */
11169 identifier = cp_parser_identifier (parser);
11170 if (identifier == error_mark_node)
11173 /* If the next token is an '=', then there is an explicit value. */
11174 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11176 /* Consume the `=' token. */
11177 cp_lexer_consume_token (parser->lexer);
11178 /* Parse the value. */
11179 value = cp_parser_constant_expression (parser,
11180 /*allow_non_constant_p=*/false,
11186 /* Create the enumerator. */
11187 build_enumerator (identifier, value, type);
11190 /* Parse a namespace-name.
11193 original-namespace-name
11196 Returns the NAMESPACE_DECL for the namespace. */
11199 cp_parser_namespace_name (cp_parser* parser)
11202 tree namespace_decl;
11204 /* Get the name of the namespace. */
11205 identifier = cp_parser_identifier (parser);
11206 if (identifier == error_mark_node)
11207 return error_mark_node;
11209 /* Look up the identifier in the currently active scope. Look only
11210 for namespaces, due to:
11212 [basic.lookup.udir]
11214 When looking up a namespace-name in a using-directive or alias
11215 definition, only namespace names are considered.
11219 [basic.lookup.qual]
11221 During the lookup of a name preceding the :: scope resolution
11222 operator, object, function, and enumerator names are ignored.
11224 (Note that cp_parser_class_or_namespace_name only calls this
11225 function if the token after the name is the scope resolution
11227 namespace_decl = cp_parser_lookup_name (parser, identifier,
11229 /*is_template=*/false,
11230 /*is_namespace=*/true,
11231 /*check_dependency=*/true,
11232 /*ambiguous_decls=*/NULL);
11233 /* If it's not a namespace, issue an error. */
11234 if (namespace_decl == error_mark_node
11235 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11237 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11238 error ("%qD is not a namespace-name", identifier);
11239 cp_parser_error (parser, "expected namespace-name");
11240 namespace_decl = error_mark_node;
11243 return namespace_decl;
11246 /* Parse a namespace-definition.
11248 namespace-definition:
11249 named-namespace-definition
11250 unnamed-namespace-definition
11252 named-namespace-definition:
11253 original-namespace-definition
11254 extension-namespace-definition
11256 original-namespace-definition:
11257 namespace identifier { namespace-body }
11259 extension-namespace-definition:
11260 namespace original-namespace-name { namespace-body }
11262 unnamed-namespace-definition:
11263 namespace { namespace-body } */
11266 cp_parser_namespace_definition (cp_parser* parser)
11268 tree identifier, attribs;
11270 /* Look for the `namespace' keyword. */
11271 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11273 /* Get the name of the namespace. We do not attempt to distinguish
11274 between an original-namespace-definition and an
11275 extension-namespace-definition at this point. The semantic
11276 analysis routines are responsible for that. */
11277 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11278 identifier = cp_parser_identifier (parser);
11280 identifier = NULL_TREE;
11282 /* Parse any specified attributes. */
11283 attribs = cp_parser_attributes_opt (parser);
11285 /* Look for the `{' to start the namespace. */
11286 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11287 /* Start the namespace. */
11288 push_namespace_with_attribs (identifier, attribs);
11289 /* Parse the body of the namespace. */
11290 cp_parser_namespace_body (parser);
11291 /* Finish the namespace. */
11293 /* Look for the final `}'. */
11294 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11297 /* Parse a namespace-body.
11300 declaration-seq [opt] */
11303 cp_parser_namespace_body (cp_parser* parser)
11305 cp_parser_declaration_seq_opt (parser);
11308 /* Parse a namespace-alias-definition.
11310 namespace-alias-definition:
11311 namespace identifier = qualified-namespace-specifier ; */
11314 cp_parser_namespace_alias_definition (cp_parser* parser)
11317 tree namespace_specifier;
11319 /* Look for the `namespace' keyword. */
11320 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11321 /* Look for the identifier. */
11322 identifier = cp_parser_identifier (parser);
11323 if (identifier == error_mark_node)
11325 /* Look for the `=' token. */
11326 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11327 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11329 error ("%<namespace%> definition is not allowed here");
11330 /* Skip the definition. */
11331 cp_lexer_consume_token (parser->lexer);
11332 if (cp_parser_skip_to_closing_brace (parser))
11333 cp_lexer_consume_token (parser->lexer);
11336 cp_parser_require (parser, CPP_EQ, "`='");
11337 /* Look for the qualified-namespace-specifier. */
11338 namespace_specifier
11339 = cp_parser_qualified_namespace_specifier (parser);
11340 /* Look for the `;' token. */
11341 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11343 /* Register the alias in the symbol table. */
11344 do_namespace_alias (identifier, namespace_specifier);
11347 /* Parse a qualified-namespace-specifier.
11349 qualified-namespace-specifier:
11350 :: [opt] nested-name-specifier [opt] namespace-name
11352 Returns a NAMESPACE_DECL corresponding to the specified
11356 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11358 /* Look for the optional `::'. */
11359 cp_parser_global_scope_opt (parser,
11360 /*current_scope_valid_p=*/false);
11362 /* Look for the optional nested-name-specifier. */
11363 cp_parser_nested_name_specifier_opt (parser,
11364 /*typename_keyword_p=*/false,
11365 /*check_dependency_p=*/true,
11367 /*is_declaration=*/true);
11369 return cp_parser_namespace_name (parser);
11372 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11373 access declaration.
11376 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11377 using :: unqualified-id ;
11379 access-declaration:
11385 cp_parser_using_declaration (cp_parser* parser,
11386 bool access_declaration_p)
11389 bool typename_p = false;
11390 bool global_scope_p;
11395 if (access_declaration_p)
11396 cp_parser_parse_tentatively (parser);
11399 /* Look for the `using' keyword. */
11400 cp_parser_require_keyword (parser, RID_USING, "`using'");
11402 /* Peek at the next token. */
11403 token = cp_lexer_peek_token (parser->lexer);
11404 /* See if it's `typename'. */
11405 if (token->keyword == RID_TYPENAME)
11407 /* Remember that we've seen it. */
11409 /* Consume the `typename' token. */
11410 cp_lexer_consume_token (parser->lexer);
11414 /* Look for the optional global scope qualification. */
11416 = (cp_parser_global_scope_opt (parser,
11417 /*current_scope_valid_p=*/false)
11420 /* If we saw `typename', or didn't see `::', then there must be a
11421 nested-name-specifier present. */
11422 if (typename_p || !global_scope_p)
11423 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11424 /*check_dependency_p=*/true,
11426 /*is_declaration=*/true);
11427 /* Otherwise, we could be in either of the two productions. In that
11428 case, treat the nested-name-specifier as optional. */
11430 qscope = cp_parser_nested_name_specifier_opt (parser,
11431 /*typename_keyword_p=*/false,
11432 /*check_dependency_p=*/true,
11434 /*is_declaration=*/true);
11436 qscope = global_namespace;
11438 if (access_declaration_p && cp_parser_error_occurred (parser))
11439 /* Something has already gone wrong; there's no need to parse
11440 further. Since an error has occurred, the return value of
11441 cp_parser_parse_definitely will be false, as required. */
11442 return cp_parser_parse_definitely (parser);
11444 /* Parse the unqualified-id. */
11445 identifier = cp_parser_unqualified_id (parser,
11446 /*template_keyword_p=*/false,
11447 /*check_dependency_p=*/true,
11448 /*declarator_p=*/true,
11449 /*optional_p=*/false);
11451 if (access_declaration_p)
11453 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11454 cp_parser_simulate_error (parser);
11455 if (!cp_parser_parse_definitely (parser))
11459 /* The function we call to handle a using-declaration is different
11460 depending on what scope we are in. */
11461 if (qscope == error_mark_node || identifier == error_mark_node)
11463 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11464 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11465 /* [namespace.udecl]
11467 A using declaration shall not name a template-id. */
11468 error ("a template-id may not appear in a using-declaration");
11471 if (at_class_scope_p ())
11473 /* Create the USING_DECL. */
11474 decl = do_class_using_decl (parser->scope, identifier);
11475 /* Add it to the list of members in this class. */
11476 finish_member_declaration (decl);
11480 decl = cp_parser_lookup_name_simple (parser, identifier);
11481 if (decl == error_mark_node)
11482 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11483 else if (!at_namespace_scope_p ())
11484 do_local_using_decl (decl, qscope, identifier);
11486 do_toplevel_using_decl (decl, qscope, identifier);
11490 /* Look for the final `;'. */
11491 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11496 /* Parse a using-directive.
11499 using namespace :: [opt] nested-name-specifier [opt]
11500 namespace-name ; */
11503 cp_parser_using_directive (cp_parser* parser)
11505 tree namespace_decl;
11508 /* Look for the `using' keyword. */
11509 cp_parser_require_keyword (parser, RID_USING, "`using'");
11510 /* And the `namespace' keyword. */
11511 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11512 /* Look for the optional `::' operator. */
11513 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11514 /* And the optional nested-name-specifier. */
11515 cp_parser_nested_name_specifier_opt (parser,
11516 /*typename_keyword_p=*/false,
11517 /*check_dependency_p=*/true,
11519 /*is_declaration=*/true);
11520 /* Get the namespace being used. */
11521 namespace_decl = cp_parser_namespace_name (parser);
11522 /* And any specified attributes. */
11523 attribs = cp_parser_attributes_opt (parser);
11524 /* Update the symbol table. */
11525 parse_using_directive (namespace_decl, attribs);
11526 /* Look for the final `;'. */
11527 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11530 /* Parse an asm-definition.
11533 asm ( string-literal ) ;
11538 asm volatile [opt] ( string-literal ) ;
11539 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11540 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11541 : asm-operand-list [opt] ) ;
11542 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11543 : asm-operand-list [opt]
11544 : asm-operand-list [opt] ) ; */
11547 cp_parser_asm_definition (cp_parser* parser)
11550 tree outputs = NULL_TREE;
11551 tree inputs = NULL_TREE;
11552 tree clobbers = NULL_TREE;
11554 bool volatile_p = false;
11555 bool extended_p = false;
11557 /* Look for the `asm' keyword. */
11558 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11559 /* See if the next token is `volatile'. */
11560 if (cp_parser_allow_gnu_extensions_p (parser)
11561 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11563 /* Remember that we saw the `volatile' keyword. */
11565 /* Consume the token. */
11566 cp_lexer_consume_token (parser->lexer);
11568 /* Look for the opening `('. */
11569 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11571 /* Look for the string. */
11572 string = cp_parser_string_literal (parser, false, false);
11573 if (string == error_mark_node)
11575 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11576 /*consume_paren=*/true);
11580 /* If we're allowing GNU extensions, check for the extended assembly
11581 syntax. Unfortunately, the `:' tokens need not be separated by
11582 a space in C, and so, for compatibility, we tolerate that here
11583 too. Doing that means that we have to treat the `::' operator as
11585 if (cp_parser_allow_gnu_extensions_p (parser)
11586 && parser->in_function_body
11587 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11588 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11590 bool inputs_p = false;
11591 bool clobbers_p = false;
11593 /* The extended syntax was used. */
11596 /* Look for outputs. */
11597 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11599 /* Consume the `:'. */
11600 cp_lexer_consume_token (parser->lexer);
11601 /* Parse the output-operands. */
11602 if (cp_lexer_next_token_is_not (parser->lexer,
11604 && cp_lexer_next_token_is_not (parser->lexer,
11606 && cp_lexer_next_token_is_not (parser->lexer,
11608 outputs = cp_parser_asm_operand_list (parser);
11610 /* If the next token is `::', there are no outputs, and the
11611 next token is the beginning of the inputs. */
11612 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11613 /* The inputs are coming next. */
11616 /* Look for inputs. */
11618 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11620 /* Consume the `:' or `::'. */
11621 cp_lexer_consume_token (parser->lexer);
11622 /* Parse the output-operands. */
11623 if (cp_lexer_next_token_is_not (parser->lexer,
11625 && cp_lexer_next_token_is_not (parser->lexer,
11627 inputs = cp_parser_asm_operand_list (parser);
11629 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11630 /* The clobbers are coming next. */
11633 /* Look for clobbers. */
11635 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11637 /* Consume the `:' or `::'. */
11638 cp_lexer_consume_token (parser->lexer);
11639 /* Parse the clobbers. */
11640 if (cp_lexer_next_token_is_not (parser->lexer,
11642 clobbers = cp_parser_asm_clobber_list (parser);
11645 /* Look for the closing `)'. */
11646 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11647 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11648 /*consume_paren=*/true);
11649 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11651 /* Create the ASM_EXPR. */
11652 if (parser->in_function_body)
11654 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11656 /* If the extended syntax was not used, mark the ASM_EXPR. */
11659 tree temp = asm_stmt;
11660 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11661 temp = TREE_OPERAND (temp, 0);
11663 ASM_INPUT_P (temp) = 1;
11667 cgraph_add_asm_node (string);
11670 /* Declarators [gram.dcl.decl] */
11672 /* Parse an init-declarator.
11675 declarator initializer [opt]
11680 declarator asm-specification [opt] attributes [opt] initializer [opt]
11682 function-definition:
11683 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11685 decl-specifier-seq [opt] declarator function-try-block
11689 function-definition:
11690 __extension__ function-definition
11692 The DECL_SPECIFIERS apply to this declarator. Returns a
11693 representation of the entity declared. If MEMBER_P is TRUE, then
11694 this declarator appears in a class scope. The new DECL created by
11695 this declarator is returned.
11697 The CHECKS are access checks that should be performed once we know
11698 what entity is being declared (and, therefore, what classes have
11701 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11702 for a function-definition here as well. If the declarator is a
11703 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11704 be TRUE upon return. By that point, the function-definition will
11705 have been completely parsed.
11707 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11711 cp_parser_init_declarator (cp_parser* parser,
11712 cp_decl_specifier_seq *decl_specifiers,
11713 VEC (deferred_access_check,gc)* checks,
11714 bool function_definition_allowed_p,
11716 int declares_class_or_enum,
11717 bool* function_definition_p)
11720 cp_declarator *declarator;
11721 tree prefix_attributes;
11723 tree asm_specification;
11725 tree decl = NULL_TREE;
11727 bool is_initialized;
11728 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11729 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11731 enum cpp_ttype initialization_kind;
11732 bool is_parenthesized_init = false;
11733 bool is_non_constant_init;
11734 int ctor_dtor_or_conv_p;
11736 tree pushed_scope = NULL;
11738 /* Gather the attributes that were provided with the
11739 decl-specifiers. */
11740 prefix_attributes = decl_specifiers->attributes;
11742 /* Assume that this is not the declarator for a function
11744 if (function_definition_p)
11745 *function_definition_p = false;
11747 /* Defer access checks while parsing the declarator; we cannot know
11748 what names are accessible until we know what is being
11750 resume_deferring_access_checks ();
11752 /* Parse the declarator. */
11754 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11755 &ctor_dtor_or_conv_p,
11756 /*parenthesized_p=*/NULL,
11757 /*member_p=*/false);
11758 /* Gather up the deferred checks. */
11759 stop_deferring_access_checks ();
11761 /* If the DECLARATOR was erroneous, there's no need to go
11763 if (declarator == cp_error_declarator)
11764 return error_mark_node;
11766 /* Check that the number of template-parameter-lists is OK. */
11767 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
11768 return error_mark_node;
11770 if (declares_class_or_enum & 2)
11771 cp_parser_check_for_definition_in_return_type (declarator,
11772 decl_specifiers->type);
11774 /* Figure out what scope the entity declared by the DECLARATOR is
11775 located in. `grokdeclarator' sometimes changes the scope, so
11776 we compute it now. */
11777 scope = get_scope_of_declarator (declarator);
11779 /* If we're allowing GNU extensions, look for an asm-specification
11781 if (cp_parser_allow_gnu_extensions_p (parser))
11783 /* Look for an asm-specification. */
11784 asm_specification = cp_parser_asm_specification_opt (parser);
11785 /* And attributes. */
11786 attributes = cp_parser_attributes_opt (parser);
11790 asm_specification = NULL_TREE;
11791 attributes = NULL_TREE;
11794 /* Peek at the next token. */
11795 token = cp_lexer_peek_token (parser->lexer);
11796 /* Check to see if the token indicates the start of a
11797 function-definition. */
11798 if (cp_parser_token_starts_function_definition_p (token))
11800 if (!function_definition_allowed_p)
11802 /* If a function-definition should not appear here, issue an
11804 cp_parser_error (parser,
11805 "a function-definition is not allowed here");
11806 return error_mark_node;
11810 /* Neither attributes nor an asm-specification are allowed
11811 on a function-definition. */
11812 if (asm_specification)
11813 error ("an asm-specification is not allowed on a function-definition");
11815 error ("attributes are not allowed on a function-definition");
11816 /* This is a function-definition. */
11817 *function_definition_p = true;
11819 /* Parse the function definition. */
11821 decl = cp_parser_save_member_function_body (parser,
11824 prefix_attributes);
11827 = (cp_parser_function_definition_from_specifiers_and_declarator
11828 (parser, decl_specifiers, prefix_attributes, declarator));
11836 Only in function declarations for constructors, destructors, and
11837 type conversions can the decl-specifier-seq be omitted.
11839 We explicitly postpone this check past the point where we handle
11840 function-definitions because we tolerate function-definitions
11841 that are missing their return types in some modes. */
11842 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
11844 cp_parser_error (parser,
11845 "expected constructor, destructor, or type conversion");
11846 return error_mark_node;
11849 /* An `=' or an `(' indicates an initializer. */
11850 if (token->type == CPP_EQ
11851 || token->type == CPP_OPEN_PAREN)
11853 is_initialized = true;
11854 initialization_kind = token->type;
11858 /* If the init-declarator isn't initialized and isn't followed by a
11859 `,' or `;', it's not a valid init-declarator. */
11860 if (token->type != CPP_COMMA
11861 && token->type != CPP_SEMICOLON)
11863 cp_parser_error (parser, "expected initializer");
11864 return error_mark_node;
11866 is_initialized = false;
11867 initialization_kind = CPP_EOF;
11870 /* Because start_decl has side-effects, we should only call it if we
11871 know we're going ahead. By this point, we know that we cannot
11872 possibly be looking at any other construct. */
11873 cp_parser_commit_to_tentative_parse (parser);
11875 /* If the decl specifiers were bad, issue an error now that we're
11876 sure this was intended to be a declarator. Then continue
11877 declaring the variable(s), as int, to try to cut down on further
11879 if (decl_specifiers->any_specifiers_p
11880 && decl_specifiers->type == error_mark_node)
11882 cp_parser_error (parser, "invalid type in declaration");
11883 decl_specifiers->type = integer_type_node;
11886 /* Check to see whether or not this declaration is a friend. */
11887 friend_p = cp_parser_friend_p (decl_specifiers);
11889 /* Enter the newly declared entry in the symbol table. If we're
11890 processing a declaration in a class-specifier, we wait until
11891 after processing the initializer. */
11894 if (parser->in_unbraced_linkage_specification_p)
11895 decl_specifiers->storage_class = sc_extern;
11896 decl = start_decl (declarator, decl_specifiers,
11897 is_initialized, attributes, prefix_attributes,
11901 /* Enter the SCOPE. That way unqualified names appearing in the
11902 initializer will be looked up in SCOPE. */
11903 pushed_scope = push_scope (scope);
11905 /* Perform deferred access control checks, now that we know in which
11906 SCOPE the declared entity resides. */
11907 if (!member_p && decl)
11909 tree saved_current_function_decl = NULL_TREE;
11911 /* If the entity being declared is a function, pretend that we
11912 are in its scope. If it is a `friend', it may have access to
11913 things that would not otherwise be accessible. */
11914 if (TREE_CODE (decl) == FUNCTION_DECL)
11916 saved_current_function_decl = current_function_decl;
11917 current_function_decl = decl;
11920 /* Perform access checks for template parameters. */
11921 cp_parser_perform_template_parameter_access_checks (checks);
11923 /* Perform the access control checks for the declarator and the
11924 the decl-specifiers. */
11925 perform_deferred_access_checks ();
11927 /* Restore the saved value. */
11928 if (TREE_CODE (decl) == FUNCTION_DECL)
11929 current_function_decl = saved_current_function_decl;
11932 /* Parse the initializer. */
11933 initializer = NULL_TREE;
11934 is_parenthesized_init = false;
11935 is_non_constant_init = true;
11936 if (is_initialized)
11938 if (function_declarator_p (declarator))
11940 if (initialization_kind == CPP_EQ)
11941 initializer = cp_parser_pure_specifier (parser);
11944 /* If the declaration was erroneous, we don't really
11945 know what the user intended, so just silently
11946 consume the initializer. */
11947 if (decl != error_mark_node)
11948 error ("initializer provided for function");
11949 cp_parser_skip_to_closing_parenthesis (parser,
11950 /*recovering=*/true,
11951 /*or_comma=*/false,
11952 /*consume_paren=*/true);
11956 initializer = cp_parser_initializer (parser,
11957 &is_parenthesized_init,
11958 &is_non_constant_init);
11961 /* The old parser allows attributes to appear after a parenthesized
11962 initializer. Mark Mitchell proposed removing this functionality
11963 on the GCC mailing lists on 2002-08-13. This parser accepts the
11964 attributes -- but ignores them. */
11965 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
11966 if (cp_parser_attributes_opt (parser))
11967 warning (OPT_Wattributes,
11968 "attributes after parenthesized initializer ignored");
11970 /* For an in-class declaration, use `grokfield' to create the
11976 pop_scope (pushed_scope);
11977 pushed_scope = false;
11979 decl = grokfield (declarator, decl_specifiers,
11980 initializer, !is_non_constant_init,
11981 /*asmspec=*/NULL_TREE,
11982 prefix_attributes);
11983 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
11984 cp_parser_save_default_args (parser, decl);
11987 /* Finish processing the declaration. But, skip friend
11989 if (!friend_p && decl && decl != error_mark_node)
11991 cp_finish_decl (decl,
11992 initializer, !is_non_constant_init,
11994 /* If the initializer is in parentheses, then this is
11995 a direct-initialization, which means that an
11996 `explicit' constructor is OK. Otherwise, an
11997 `explicit' constructor cannot be used. */
11998 ((is_parenthesized_init || !is_initialized)
11999 ? 0 : LOOKUP_ONLYCONVERTING));
12001 else if ((cxx_dialect != cxx98) && friend_p
12002 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12003 /* Core issue #226 (C++0x only): A default template-argument
12004 shall not be specified in a friend class template
12006 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12007 /*is_partial=*/0, /*is_friend_decl=*/1);
12009 if (!friend_p && pushed_scope)
12010 pop_scope (pushed_scope);
12015 /* Parse a declarator.
12019 ptr-operator declarator
12021 abstract-declarator:
12022 ptr-operator abstract-declarator [opt]
12023 direct-abstract-declarator
12028 attributes [opt] direct-declarator
12029 attributes [opt] ptr-operator declarator
12031 abstract-declarator:
12032 attributes [opt] ptr-operator abstract-declarator [opt]
12033 attributes [opt] direct-abstract-declarator
12035 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12036 detect constructor, destructor or conversion operators. It is set
12037 to -1 if the declarator is a name, and +1 if it is a
12038 function. Otherwise it is set to zero. Usually you just want to
12039 test for >0, but internally the negative value is used.
12041 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12042 a decl-specifier-seq unless it declares a constructor, destructor,
12043 or conversion. It might seem that we could check this condition in
12044 semantic analysis, rather than parsing, but that makes it difficult
12045 to handle something like `f()'. We want to notice that there are
12046 no decl-specifiers, and therefore realize that this is an
12047 expression, not a declaration.)
12049 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12050 the declarator is a direct-declarator of the form "(...)".
12052 MEMBER_P is true iff this declarator is a member-declarator. */
12054 static cp_declarator *
12055 cp_parser_declarator (cp_parser* parser,
12056 cp_parser_declarator_kind dcl_kind,
12057 int* ctor_dtor_or_conv_p,
12058 bool* parenthesized_p,
12062 cp_declarator *declarator;
12063 enum tree_code code;
12064 cp_cv_quals cv_quals;
12066 tree attributes = NULL_TREE;
12068 /* Assume this is not a constructor, destructor, or type-conversion
12070 if (ctor_dtor_or_conv_p)
12071 *ctor_dtor_or_conv_p = 0;
12073 if (cp_parser_allow_gnu_extensions_p (parser))
12074 attributes = cp_parser_attributes_opt (parser);
12076 /* Peek at the next token. */
12077 token = cp_lexer_peek_token (parser->lexer);
12079 /* Check for the ptr-operator production. */
12080 cp_parser_parse_tentatively (parser);
12081 /* Parse the ptr-operator. */
12082 code = cp_parser_ptr_operator (parser,
12085 /* If that worked, then we have a ptr-operator. */
12086 if (cp_parser_parse_definitely (parser))
12088 /* If a ptr-operator was found, then this declarator was not
12090 if (parenthesized_p)
12091 *parenthesized_p = true;
12092 /* The dependent declarator is optional if we are parsing an
12093 abstract-declarator. */
12094 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12095 cp_parser_parse_tentatively (parser);
12097 /* Parse the dependent declarator. */
12098 declarator = cp_parser_declarator (parser, dcl_kind,
12099 /*ctor_dtor_or_conv_p=*/NULL,
12100 /*parenthesized_p=*/NULL,
12101 /*member_p=*/false);
12103 /* If we are parsing an abstract-declarator, we must handle the
12104 case where the dependent declarator is absent. */
12105 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12106 && !cp_parser_parse_definitely (parser))
12109 declarator = cp_parser_make_indirect_declarator
12110 (code, class_type, cv_quals, declarator);
12112 /* Everything else is a direct-declarator. */
12115 if (parenthesized_p)
12116 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12118 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12119 ctor_dtor_or_conv_p,
12123 if (attributes && declarator && declarator != cp_error_declarator)
12124 declarator->attributes = attributes;
12129 /* Parse a direct-declarator or direct-abstract-declarator.
12133 direct-declarator ( parameter-declaration-clause )
12134 cv-qualifier-seq [opt]
12135 exception-specification [opt]
12136 direct-declarator [ constant-expression [opt] ]
12139 direct-abstract-declarator:
12140 direct-abstract-declarator [opt]
12141 ( parameter-declaration-clause )
12142 cv-qualifier-seq [opt]
12143 exception-specification [opt]
12144 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12145 ( abstract-declarator )
12147 Returns a representation of the declarator. DCL_KIND is
12148 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12149 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12150 we are parsing a direct-declarator. It is
12151 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12152 of ambiguity we prefer an abstract declarator, as per
12153 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12154 cp_parser_declarator. */
12156 static cp_declarator *
12157 cp_parser_direct_declarator (cp_parser* parser,
12158 cp_parser_declarator_kind dcl_kind,
12159 int* ctor_dtor_or_conv_p,
12163 cp_declarator *declarator = NULL;
12164 tree scope = NULL_TREE;
12165 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12166 bool saved_in_declarator_p = parser->in_declarator_p;
12168 tree pushed_scope = NULL_TREE;
12172 /* Peek at the next token. */
12173 token = cp_lexer_peek_token (parser->lexer);
12174 if (token->type == CPP_OPEN_PAREN)
12176 /* This is either a parameter-declaration-clause, or a
12177 parenthesized declarator. When we know we are parsing a
12178 named declarator, it must be a parenthesized declarator
12179 if FIRST is true. For instance, `(int)' is a
12180 parameter-declaration-clause, with an omitted
12181 direct-abstract-declarator. But `((*))', is a
12182 parenthesized abstract declarator. Finally, when T is a
12183 template parameter `(T)' is a
12184 parameter-declaration-clause, and not a parenthesized
12187 We first try and parse a parameter-declaration-clause,
12188 and then try a nested declarator (if FIRST is true).
12190 It is not an error for it not to be a
12191 parameter-declaration-clause, even when FIRST is
12197 The first is the declaration of a function while the
12198 second is a the definition of a variable, including its
12201 Having seen only the parenthesis, we cannot know which of
12202 these two alternatives should be selected. Even more
12203 complex are examples like:
12208 The former is a function-declaration; the latter is a
12209 variable initialization.
12211 Thus again, we try a parameter-declaration-clause, and if
12212 that fails, we back out and return. */
12214 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12216 cp_parameter_declarator *params;
12217 unsigned saved_num_template_parameter_lists;
12219 /* In a member-declarator, the only valid interpretation
12220 of a parenthesis is the start of a
12221 parameter-declaration-clause. (It is invalid to
12222 initialize a static data member with a parenthesized
12223 initializer; only the "=" form of initialization is
12226 cp_parser_parse_tentatively (parser);
12228 /* Consume the `('. */
12229 cp_lexer_consume_token (parser->lexer);
12232 /* If this is going to be an abstract declarator, we're
12233 in a declarator and we can't have default args. */
12234 parser->default_arg_ok_p = false;
12235 parser->in_declarator_p = true;
12238 /* Inside the function parameter list, surrounding
12239 template-parameter-lists do not apply. */
12240 saved_num_template_parameter_lists
12241 = parser->num_template_parameter_lists;
12242 parser->num_template_parameter_lists = 0;
12244 /* Parse the parameter-declaration-clause. */
12245 params = cp_parser_parameter_declaration_clause (parser);
12247 parser->num_template_parameter_lists
12248 = saved_num_template_parameter_lists;
12250 /* If all went well, parse the cv-qualifier-seq and the
12251 exception-specification. */
12252 if (member_p || cp_parser_parse_definitely (parser))
12254 cp_cv_quals cv_quals;
12255 tree exception_specification;
12257 if (ctor_dtor_or_conv_p)
12258 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12260 /* Consume the `)'. */
12261 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12263 /* Parse the cv-qualifier-seq. */
12264 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12265 /* And the exception-specification. */
12266 exception_specification
12267 = cp_parser_exception_specification_opt (parser);
12269 /* Create the function-declarator. */
12270 declarator = make_call_declarator (declarator,
12273 exception_specification);
12274 /* Any subsequent parameter lists are to do with
12275 return type, so are not those of the declared
12277 parser->default_arg_ok_p = false;
12279 /* Repeat the main loop. */
12284 /* If this is the first, we can try a parenthesized
12288 bool saved_in_type_id_in_expr_p;
12290 parser->default_arg_ok_p = saved_default_arg_ok_p;
12291 parser->in_declarator_p = saved_in_declarator_p;
12293 /* Consume the `('. */
12294 cp_lexer_consume_token (parser->lexer);
12295 /* Parse the nested declarator. */
12296 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12297 parser->in_type_id_in_expr_p = true;
12299 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12300 /*parenthesized_p=*/NULL,
12302 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12304 /* Expect a `)'. */
12305 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12306 declarator = cp_error_declarator;
12307 if (declarator == cp_error_declarator)
12310 goto handle_declarator;
12312 /* Otherwise, we must be done. */
12316 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12317 && token->type == CPP_OPEN_SQUARE)
12319 /* Parse an array-declarator. */
12322 if (ctor_dtor_or_conv_p)
12323 *ctor_dtor_or_conv_p = 0;
12326 parser->default_arg_ok_p = false;
12327 parser->in_declarator_p = true;
12328 /* Consume the `['. */
12329 cp_lexer_consume_token (parser->lexer);
12330 /* Peek at the next token. */
12331 token = cp_lexer_peek_token (parser->lexer);
12332 /* If the next token is `]', then there is no
12333 constant-expression. */
12334 if (token->type != CPP_CLOSE_SQUARE)
12336 bool non_constant_p;
12339 = cp_parser_constant_expression (parser,
12340 /*allow_non_constant=*/true,
12342 if (!non_constant_p)
12343 bounds = fold_non_dependent_expr (bounds);
12344 /* Normally, the array bound must be an integral constant
12345 expression. However, as an extension, we allow VLAs
12346 in function scopes. */
12347 else if (!parser->in_function_body)
12349 error ("array bound is not an integer constant");
12350 bounds = error_mark_node;
12354 bounds = NULL_TREE;
12355 /* Look for the closing `]'. */
12356 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12358 declarator = cp_error_declarator;
12362 declarator = make_array_declarator (declarator, bounds);
12364 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12366 tree qualifying_scope;
12367 tree unqualified_name;
12368 special_function_kind sfk;
12370 bool pack_expansion_p = false;
12372 /* Parse a declarator-id */
12373 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12376 cp_parser_parse_tentatively (parser);
12378 /* If we see an ellipsis, we should be looking at a
12380 if (token->type == CPP_ELLIPSIS)
12382 /* Consume the `...' */
12383 cp_lexer_consume_token (parser->lexer);
12385 pack_expansion_p = true;
12390 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12391 qualifying_scope = parser->scope;
12396 if (!unqualified_name && pack_expansion_p)
12398 /* Check whether an error occurred. */
12399 okay = !cp_parser_error_occurred (parser);
12401 /* We already consumed the ellipsis to mark a
12402 parameter pack, but we have no way to report it,
12403 so abort the tentative parse. We will be exiting
12404 immediately anyway. */
12405 cp_parser_abort_tentative_parse (parser);
12408 okay = cp_parser_parse_definitely (parser);
12411 unqualified_name = error_mark_node;
12412 else if (unqualified_name
12413 && (qualifying_scope
12414 || (TREE_CODE (unqualified_name)
12415 != IDENTIFIER_NODE)))
12417 cp_parser_error (parser, "expected unqualified-id");
12418 unqualified_name = error_mark_node;
12422 if (!unqualified_name)
12424 if (unqualified_name == error_mark_node)
12426 declarator = cp_error_declarator;
12427 pack_expansion_p = false;
12428 declarator->parameter_pack_p = false;
12432 if (qualifying_scope && at_namespace_scope_p ()
12433 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12435 /* In the declaration of a member of a template class
12436 outside of the class itself, the SCOPE will sometimes
12437 be a TYPENAME_TYPE. For example, given:
12439 template <typename T>
12440 int S<T>::R::i = 3;
12442 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12443 this context, we must resolve S<T>::R to an ordinary
12444 type, rather than a typename type.
12446 The reason we normally avoid resolving TYPENAME_TYPEs
12447 is that a specialization of `S' might render
12448 `S<T>::R' not a type. However, if `S' is
12449 specialized, then this `i' will not be used, so there
12450 is no harm in resolving the types here. */
12453 /* Resolve the TYPENAME_TYPE. */
12454 type = resolve_typename_type (qualifying_scope,
12455 /*only_current_p=*/false);
12456 /* If that failed, the declarator is invalid. */
12457 if (type == error_mark_node)
12458 error ("%<%T::%E%> is not a type",
12459 TYPE_CONTEXT (qualifying_scope),
12460 TYPE_IDENTIFIER (qualifying_scope));
12461 qualifying_scope = type;
12466 if (unqualified_name)
12470 if (qualifying_scope
12471 && CLASS_TYPE_P (qualifying_scope))
12472 class_type = qualifying_scope;
12474 class_type = current_class_type;
12476 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12478 tree name_type = TREE_TYPE (unqualified_name);
12479 if (class_type && same_type_p (name_type, class_type))
12481 if (qualifying_scope
12482 && CLASSTYPE_USE_TEMPLATE (name_type))
12484 error ("invalid use of constructor as a template");
12485 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12486 "name the constructor in a qualified name",
12488 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12489 class_type, name_type);
12490 declarator = cp_error_declarator;
12494 unqualified_name = constructor_name (class_type);
12498 /* We do not attempt to print the declarator
12499 here because we do not have enough
12500 information about its original syntactic
12502 cp_parser_error (parser, "invalid declarator");
12503 declarator = cp_error_declarator;
12510 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12511 sfk = sfk_destructor;
12512 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12513 sfk = sfk_conversion;
12514 else if (/* There's no way to declare a constructor
12515 for an anonymous type, even if the type
12516 got a name for linkage purposes. */
12517 !TYPE_WAS_ANONYMOUS (class_type)
12518 && constructor_name_p (unqualified_name,
12521 unqualified_name = constructor_name (class_type);
12522 sfk = sfk_constructor;
12525 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12526 *ctor_dtor_or_conv_p = -1;
12529 declarator = make_id_declarator (qualifying_scope,
12532 declarator->id_loc = token->location;
12533 declarator->parameter_pack_p = pack_expansion_p;
12535 if (pack_expansion_p)
12536 maybe_warn_variadic_templates ();
12538 handle_declarator:;
12539 scope = get_scope_of_declarator (declarator);
12541 /* Any names that appear after the declarator-id for a
12542 member are looked up in the containing scope. */
12543 pushed_scope = push_scope (scope);
12544 parser->in_declarator_p = true;
12545 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12546 || (declarator && declarator->kind == cdk_id))
12547 /* Default args are only allowed on function
12549 parser->default_arg_ok_p = saved_default_arg_ok_p;
12551 parser->default_arg_ok_p = false;
12560 /* For an abstract declarator, we might wind up with nothing at this
12561 point. That's an error; the declarator is not optional. */
12563 cp_parser_error (parser, "expected declarator");
12565 /* If we entered a scope, we must exit it now. */
12567 pop_scope (pushed_scope);
12569 parser->default_arg_ok_p = saved_default_arg_ok_p;
12570 parser->in_declarator_p = saved_in_declarator_p;
12575 /* Parse a ptr-operator.
12578 * cv-qualifier-seq [opt]
12580 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12585 & cv-qualifier-seq [opt]
12587 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12588 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12589 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12590 filled in with the TYPE containing the member. *CV_QUALS is
12591 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12592 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12593 Note that the tree codes returned by this function have nothing
12594 to do with the types of trees that will be eventually be created
12595 to represent the pointer or reference type being parsed. They are
12596 just constants with suggestive names. */
12597 static enum tree_code
12598 cp_parser_ptr_operator (cp_parser* parser,
12600 cp_cv_quals *cv_quals)
12602 enum tree_code code = ERROR_MARK;
12605 /* Assume that it's not a pointer-to-member. */
12607 /* And that there are no cv-qualifiers. */
12608 *cv_quals = TYPE_UNQUALIFIED;
12610 /* Peek at the next token. */
12611 token = cp_lexer_peek_token (parser->lexer);
12613 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12614 if (token->type == CPP_MULT)
12615 code = INDIRECT_REF;
12616 else if (token->type == CPP_AND)
12618 else if ((cxx_dialect != cxx98) &&
12619 token->type == CPP_AND_AND) /* C++0x only */
12620 code = NON_LVALUE_EXPR;
12622 if (code != ERROR_MARK)
12624 /* Consume the `*', `&' or `&&'. */
12625 cp_lexer_consume_token (parser->lexer);
12627 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12628 `&', if we are allowing GNU extensions. (The only qualifier
12629 that can legally appear after `&' is `restrict', but that is
12630 enforced during semantic analysis. */
12631 if (code == INDIRECT_REF
12632 || cp_parser_allow_gnu_extensions_p (parser))
12633 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12637 /* Try the pointer-to-member case. */
12638 cp_parser_parse_tentatively (parser);
12639 /* Look for the optional `::' operator. */
12640 cp_parser_global_scope_opt (parser,
12641 /*current_scope_valid_p=*/false);
12642 /* Look for the nested-name specifier. */
12643 cp_parser_nested_name_specifier (parser,
12644 /*typename_keyword_p=*/false,
12645 /*check_dependency_p=*/true,
12647 /*is_declaration=*/false);
12648 /* If we found it, and the next token is a `*', then we are
12649 indeed looking at a pointer-to-member operator. */
12650 if (!cp_parser_error_occurred (parser)
12651 && cp_parser_require (parser, CPP_MULT, "`*'"))
12653 /* Indicate that the `*' operator was used. */
12654 code = INDIRECT_REF;
12656 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12657 error ("%qD is a namespace", parser->scope);
12660 /* The type of which the member is a member is given by the
12662 *type = parser->scope;
12663 /* The next name will not be qualified. */
12664 parser->scope = NULL_TREE;
12665 parser->qualifying_scope = NULL_TREE;
12666 parser->object_scope = NULL_TREE;
12667 /* Look for the optional cv-qualifier-seq. */
12668 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12671 /* If that didn't work we don't have a ptr-operator. */
12672 if (!cp_parser_parse_definitely (parser))
12673 cp_parser_error (parser, "expected ptr-operator");
12679 /* Parse an (optional) cv-qualifier-seq.
12682 cv-qualifier cv-qualifier-seq [opt]
12693 Returns a bitmask representing the cv-qualifiers. */
12696 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12698 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12703 cp_cv_quals cv_qualifier;
12705 /* Peek at the next token. */
12706 token = cp_lexer_peek_token (parser->lexer);
12707 /* See if it's a cv-qualifier. */
12708 switch (token->keyword)
12711 cv_qualifier = TYPE_QUAL_CONST;
12715 cv_qualifier = TYPE_QUAL_VOLATILE;
12719 cv_qualifier = TYPE_QUAL_RESTRICT;
12723 cv_qualifier = TYPE_UNQUALIFIED;
12730 if (cv_quals & cv_qualifier)
12732 error ("duplicate cv-qualifier");
12733 cp_lexer_purge_token (parser->lexer);
12737 cp_lexer_consume_token (parser->lexer);
12738 cv_quals |= cv_qualifier;
12745 /* Parse a declarator-id.
12749 :: [opt] nested-name-specifier [opt] type-name
12751 In the `id-expression' case, the value returned is as for
12752 cp_parser_id_expression if the id-expression was an unqualified-id.
12753 If the id-expression was a qualified-id, then a SCOPE_REF is
12754 returned. The first operand is the scope (either a NAMESPACE_DECL
12755 or TREE_TYPE), but the second is still just a representation of an
12759 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
12762 /* The expression must be an id-expression. Assume that qualified
12763 names are the names of types so that:
12766 int S<T>::R::i = 3;
12768 will work; we must treat `S<T>::R' as the name of a type.
12769 Similarly, assume that qualified names are templates, where
12773 int S<T>::R<T>::i = 3;
12776 id = cp_parser_id_expression (parser,
12777 /*template_keyword_p=*/false,
12778 /*check_dependency_p=*/false,
12779 /*template_p=*/NULL,
12780 /*declarator_p=*/true,
12782 if (id && BASELINK_P (id))
12783 id = BASELINK_FUNCTIONS (id);
12787 /* Parse a type-id.
12790 type-specifier-seq abstract-declarator [opt]
12792 Returns the TYPE specified. */
12795 cp_parser_type_id (cp_parser* parser)
12797 cp_decl_specifier_seq type_specifier_seq;
12798 cp_declarator *abstract_declarator;
12800 /* Parse the type-specifier-seq. */
12801 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12802 &type_specifier_seq);
12803 if (type_specifier_seq.type == error_mark_node)
12804 return error_mark_node;
12806 /* There might or might not be an abstract declarator. */
12807 cp_parser_parse_tentatively (parser);
12808 /* Look for the declarator. */
12809 abstract_declarator
12810 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
12811 /*parenthesized_p=*/NULL,
12812 /*member_p=*/false);
12813 /* Check to see if there really was a declarator. */
12814 if (!cp_parser_parse_definitely (parser))
12815 abstract_declarator = NULL;
12817 return groktypename (&type_specifier_seq, abstract_declarator);
12820 /* Parse a type-specifier-seq.
12822 type-specifier-seq:
12823 type-specifier type-specifier-seq [opt]
12827 type-specifier-seq:
12828 attributes type-specifier-seq [opt]
12830 If IS_CONDITION is true, we are at the start of a "condition",
12831 e.g., we've just seen "if (".
12833 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
12836 cp_parser_type_specifier_seq (cp_parser* parser,
12838 cp_decl_specifier_seq *type_specifier_seq)
12840 bool seen_type_specifier = false;
12841 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
12843 /* Clear the TYPE_SPECIFIER_SEQ. */
12844 clear_decl_specs (type_specifier_seq);
12846 /* Parse the type-specifiers and attributes. */
12849 tree type_specifier;
12850 bool is_cv_qualifier;
12852 /* Check for attributes first. */
12853 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
12855 type_specifier_seq->attributes =
12856 chainon (type_specifier_seq->attributes,
12857 cp_parser_attributes_opt (parser));
12861 /* Look for the type-specifier. */
12862 type_specifier = cp_parser_type_specifier (parser,
12864 type_specifier_seq,
12865 /*is_declaration=*/false,
12868 if (!type_specifier)
12870 /* If the first type-specifier could not be found, this is not a
12871 type-specifier-seq at all. */
12872 if (!seen_type_specifier)
12874 cp_parser_error (parser, "expected type-specifier");
12875 type_specifier_seq->type = error_mark_node;
12878 /* If subsequent type-specifiers could not be found, the
12879 type-specifier-seq is complete. */
12883 seen_type_specifier = true;
12884 /* The standard says that a condition can be:
12886 type-specifier-seq declarator = assignment-expression
12893 we should treat the "S" as a declarator, not as a
12894 type-specifier. The standard doesn't say that explicitly for
12895 type-specifier-seq, but it does say that for
12896 decl-specifier-seq in an ordinary declaration. Perhaps it
12897 would be clearer just to allow a decl-specifier-seq here, and
12898 then add a semantic restriction that if any decl-specifiers
12899 that are not type-specifiers appear, the program is invalid. */
12900 if (is_condition && !is_cv_qualifier)
12901 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
12904 cp_parser_check_decl_spec (type_specifier_seq);
12907 /* Parse a parameter-declaration-clause.
12909 parameter-declaration-clause:
12910 parameter-declaration-list [opt] ... [opt]
12911 parameter-declaration-list , ...
12913 Returns a representation for the parameter declarations. A return
12914 value of NULL indicates a parameter-declaration-clause consisting
12915 only of an ellipsis. */
12917 static cp_parameter_declarator *
12918 cp_parser_parameter_declaration_clause (cp_parser* parser)
12920 cp_parameter_declarator *parameters;
12925 /* Peek at the next token. */
12926 token = cp_lexer_peek_token (parser->lexer);
12927 /* Check for trivial parameter-declaration-clauses. */
12928 if (token->type == CPP_ELLIPSIS)
12930 /* Consume the `...' token. */
12931 cp_lexer_consume_token (parser->lexer);
12934 else if (token->type == CPP_CLOSE_PAREN)
12935 /* There are no parameters. */
12937 #ifndef NO_IMPLICIT_EXTERN_C
12938 if (in_system_header && current_class_type == NULL
12939 && current_lang_name == lang_name_c)
12943 return no_parameters;
12945 /* Check for `(void)', too, which is a special case. */
12946 else if (token->keyword == RID_VOID
12947 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
12948 == CPP_CLOSE_PAREN))
12950 /* Consume the `void' token. */
12951 cp_lexer_consume_token (parser->lexer);
12952 /* There are no parameters. */
12953 return no_parameters;
12956 /* Parse the parameter-declaration-list. */
12957 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
12958 /* If a parse error occurred while parsing the
12959 parameter-declaration-list, then the entire
12960 parameter-declaration-clause is erroneous. */
12964 /* Peek at the next token. */
12965 token = cp_lexer_peek_token (parser->lexer);
12966 /* If it's a `,', the clause should terminate with an ellipsis. */
12967 if (token->type == CPP_COMMA)
12969 /* Consume the `,'. */
12970 cp_lexer_consume_token (parser->lexer);
12971 /* Expect an ellipsis. */
12973 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
12975 /* It might also be `...' if the optional trailing `,' was
12977 else if (token->type == CPP_ELLIPSIS)
12979 /* Consume the `...' token. */
12980 cp_lexer_consume_token (parser->lexer);
12981 /* And remember that we saw it. */
12985 ellipsis_p = false;
12987 /* Finish the parameter list. */
12988 if (parameters && ellipsis_p)
12989 parameters->ellipsis_p = true;
12994 /* Parse a parameter-declaration-list.
12996 parameter-declaration-list:
12997 parameter-declaration
12998 parameter-declaration-list , parameter-declaration
13000 Returns a representation of the parameter-declaration-list, as for
13001 cp_parser_parameter_declaration_clause. However, the
13002 `void_list_node' is never appended to the list. Upon return,
13003 *IS_ERROR will be true iff an error occurred. */
13005 static cp_parameter_declarator *
13006 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13008 cp_parameter_declarator *parameters = NULL;
13009 cp_parameter_declarator **tail = ¶meters;
13010 bool saved_in_unbraced_linkage_specification_p;
13012 /* Assume all will go well. */
13014 /* The special considerations that apply to a function within an
13015 unbraced linkage specifications do not apply to the parameters
13016 to the function. */
13017 saved_in_unbraced_linkage_specification_p
13018 = parser->in_unbraced_linkage_specification_p;
13019 parser->in_unbraced_linkage_specification_p = false;
13021 /* Look for more parameters. */
13024 cp_parameter_declarator *parameter;
13025 bool parenthesized_p;
13026 /* Parse the parameter. */
13028 = cp_parser_parameter_declaration (parser,
13029 /*template_parm_p=*/false,
13032 /* If a parse error occurred parsing the parameter declaration,
13033 then the entire parameter-declaration-list is erroneous. */
13040 /* Add the new parameter to the list. */
13042 tail = ¶meter->next;
13044 /* Peek at the next token. */
13045 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13046 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13047 /* These are for Objective-C++ */
13048 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13049 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13050 /* The parameter-declaration-list is complete. */
13052 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13056 /* Peek at the next token. */
13057 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13058 /* If it's an ellipsis, then the list is complete. */
13059 if (token->type == CPP_ELLIPSIS)
13061 /* Otherwise, there must be more parameters. Consume the
13063 cp_lexer_consume_token (parser->lexer);
13064 /* When parsing something like:
13066 int i(float f, double d)
13068 we can tell after seeing the declaration for "f" that we
13069 are not looking at an initialization of a variable "i",
13070 but rather at the declaration of a function "i".
13072 Due to the fact that the parsing of template arguments
13073 (as specified to a template-id) requires backtracking we
13074 cannot use this technique when inside a template argument
13076 if (!parser->in_template_argument_list_p
13077 && !parser->in_type_id_in_expr_p
13078 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13079 /* However, a parameter-declaration of the form
13080 "foat(f)" (which is a valid declaration of a
13081 parameter "f") can also be interpreted as an
13082 expression (the conversion of "f" to "float"). */
13083 && !parenthesized_p)
13084 cp_parser_commit_to_tentative_parse (parser);
13088 cp_parser_error (parser, "expected %<,%> or %<...%>");
13089 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13090 cp_parser_skip_to_closing_parenthesis (parser,
13091 /*recovering=*/true,
13092 /*or_comma=*/false,
13093 /*consume_paren=*/false);
13098 parser->in_unbraced_linkage_specification_p
13099 = saved_in_unbraced_linkage_specification_p;
13104 /* Parse a parameter declaration.
13106 parameter-declaration:
13107 decl-specifier-seq ... [opt] declarator
13108 decl-specifier-seq declarator = assignment-expression
13109 decl-specifier-seq ... [opt] abstract-declarator [opt]
13110 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13112 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13113 declares a template parameter. (In that case, a non-nested `>'
13114 token encountered during the parsing of the assignment-expression
13115 is not interpreted as a greater-than operator.)
13117 Returns a representation of the parameter, or NULL if an error
13118 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13119 true iff the declarator is of the form "(p)". */
13121 static cp_parameter_declarator *
13122 cp_parser_parameter_declaration (cp_parser *parser,
13123 bool template_parm_p,
13124 bool *parenthesized_p)
13126 int declares_class_or_enum;
13127 bool greater_than_is_operator_p;
13128 cp_decl_specifier_seq decl_specifiers;
13129 cp_declarator *declarator;
13130 tree default_argument;
13132 const char *saved_message;
13134 /* In a template parameter, `>' is not an operator.
13138 When parsing a default template-argument for a non-type
13139 template-parameter, the first non-nested `>' is taken as the end
13140 of the template parameter-list rather than a greater-than
13142 greater_than_is_operator_p = !template_parm_p;
13144 /* Type definitions may not appear in parameter types. */
13145 saved_message = parser->type_definition_forbidden_message;
13146 parser->type_definition_forbidden_message
13147 = "types may not be defined in parameter types";
13149 /* Parse the declaration-specifiers. */
13150 cp_parser_decl_specifier_seq (parser,
13151 CP_PARSER_FLAGS_NONE,
13153 &declares_class_or_enum);
13154 /* If an error occurred, there's no reason to attempt to parse the
13155 rest of the declaration. */
13156 if (cp_parser_error_occurred (parser))
13158 parser->type_definition_forbidden_message = saved_message;
13162 /* Peek at the next token. */
13163 token = cp_lexer_peek_token (parser->lexer);
13165 /* If the next token is a `)', `,', `=', `>', or `...', then there
13166 is no declarator. However, when variadic templates are enabled,
13167 there may be a declarator following `...'. */
13168 if (token->type == CPP_CLOSE_PAREN
13169 || token->type == CPP_COMMA
13170 || token->type == CPP_EQ
13171 || token->type == CPP_GREATER)
13174 if (parenthesized_p)
13175 *parenthesized_p = false;
13177 /* Otherwise, there should be a declarator. */
13180 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13181 parser->default_arg_ok_p = false;
13183 /* After seeing a decl-specifier-seq, if the next token is not a
13184 "(", there is no possibility that the code is a valid
13185 expression. Therefore, if parsing tentatively, we commit at
13187 if (!parser->in_template_argument_list_p
13188 /* In an expression context, having seen:
13192 we cannot be sure whether we are looking at a
13193 function-type (taking a "char" as a parameter) or a cast
13194 of some object of type "char" to "int". */
13195 && !parser->in_type_id_in_expr_p
13196 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13197 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13198 cp_parser_commit_to_tentative_parse (parser);
13199 /* Parse the declarator. */
13200 declarator = cp_parser_declarator (parser,
13201 CP_PARSER_DECLARATOR_EITHER,
13202 /*ctor_dtor_or_conv_p=*/NULL,
13204 /*member_p=*/false);
13205 parser->default_arg_ok_p = saved_default_arg_ok_p;
13206 /* After the declarator, allow more attributes. */
13207 decl_specifiers.attributes
13208 = chainon (decl_specifiers.attributes,
13209 cp_parser_attributes_opt (parser));
13212 /* If the next token is an ellipsis, and we have not seen a
13213 declarator name, and the type of the declarator contains parameter
13214 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13215 a parameter pack expansion expression. Otherwise, leave the
13216 ellipsis for a C-style variadic function. */
13217 token = cp_lexer_peek_token (parser->lexer);
13218 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13220 tree type = decl_specifiers.type;
13222 if (type && DECL_P (type))
13223 type = TREE_TYPE (type);
13226 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13227 && declarator_can_be_parameter_pack (declarator)
13228 && (!declarator || !declarator->parameter_pack_p)
13229 && uses_parameter_packs (type))
13231 /* Consume the `...'. */
13232 cp_lexer_consume_token (parser->lexer);
13233 maybe_warn_variadic_templates ();
13235 /* Build a pack expansion type */
13237 declarator->parameter_pack_p = true;
13239 decl_specifiers.type = make_pack_expansion (type);
13243 /* The restriction on defining new types applies only to the type
13244 of the parameter, not to the default argument. */
13245 parser->type_definition_forbidden_message = saved_message;
13247 /* If the next token is `=', then process a default argument. */
13248 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13250 bool saved_greater_than_is_operator_p;
13251 /* Consume the `='. */
13252 cp_lexer_consume_token (parser->lexer);
13254 /* If we are defining a class, then the tokens that make up the
13255 default argument must be saved and processed later. */
13256 if (!template_parm_p && at_class_scope_p ()
13257 && TYPE_BEING_DEFINED (current_class_type))
13259 unsigned depth = 0;
13260 cp_token *first_token;
13263 /* Add tokens until we have processed the entire default
13264 argument. We add the range [first_token, token). */
13265 first_token = cp_lexer_peek_token (parser->lexer);
13270 /* Peek at the next token. */
13271 token = cp_lexer_peek_token (parser->lexer);
13272 /* What we do depends on what token we have. */
13273 switch (token->type)
13275 /* In valid code, a default argument must be
13276 immediately followed by a `,' `)', or `...'. */
13278 case CPP_CLOSE_PAREN:
13280 /* If we run into a non-nested `;', `}', or `]',
13281 then the code is invalid -- but the default
13282 argument is certainly over. */
13283 case CPP_SEMICOLON:
13284 case CPP_CLOSE_BRACE:
13285 case CPP_CLOSE_SQUARE:
13288 /* Update DEPTH, if necessary. */
13289 else if (token->type == CPP_CLOSE_PAREN
13290 || token->type == CPP_CLOSE_BRACE
13291 || token->type == CPP_CLOSE_SQUARE)
13295 case CPP_OPEN_PAREN:
13296 case CPP_OPEN_SQUARE:
13297 case CPP_OPEN_BRACE:
13302 if (cxx_dialect == cxx98)
13304 /* Fall through for C++0x, which treats the `>>'
13305 operator like two `>' tokens in certain
13309 /* If we see a non-nested `>', and `>' is not an
13310 operator, then it marks the end of the default
13312 if (!depth && !greater_than_is_operator_p)
13316 /* If we run out of tokens, issue an error message. */
13318 case CPP_PRAGMA_EOL:
13319 error ("file ends in default argument");
13325 /* In these cases, we should look for template-ids.
13326 For example, if the default argument is
13327 `X<int, double>()', we need to do name lookup to
13328 figure out whether or not `X' is a template; if
13329 so, the `,' does not end the default argument.
13331 That is not yet done. */
13338 /* If we've reached the end, stop. */
13342 /* Add the token to the token block. */
13343 token = cp_lexer_consume_token (parser->lexer);
13346 /* Create a DEFAULT_ARG to represented the unparsed default
13348 default_argument = make_node (DEFAULT_ARG);
13349 DEFARG_TOKENS (default_argument)
13350 = cp_token_cache_new (first_token, token);
13351 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13353 /* Outside of a class definition, we can just parse the
13354 assignment-expression. */
13357 bool saved_local_variables_forbidden_p;
13359 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13361 saved_greater_than_is_operator_p
13362 = parser->greater_than_is_operator_p;
13363 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13364 /* Local variable names (and the `this' keyword) may not
13365 appear in a default argument. */
13366 saved_local_variables_forbidden_p
13367 = parser->local_variables_forbidden_p;
13368 parser->local_variables_forbidden_p = true;
13369 /* The default argument expression may cause implicitly
13370 defined member functions to be synthesized, which will
13371 result in garbage collection. We must treat this
13372 situation as if we were within the body of function so as
13373 to avoid collecting live data on the stack. */
13375 /* Parse the assignment-expression. */
13376 if (template_parm_p)
13377 push_deferring_access_checks (dk_no_deferred);
13379 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13380 if (template_parm_p)
13381 pop_deferring_access_checks ();
13382 /* Restore saved state. */
13384 parser->greater_than_is_operator_p
13385 = saved_greater_than_is_operator_p;
13386 parser->local_variables_forbidden_p
13387 = saved_local_variables_forbidden_p;
13389 if (!parser->default_arg_ok_p)
13391 if (!flag_pedantic_errors)
13392 warning (0, "deprecated use of default argument for parameter of non-function");
13395 error ("default arguments are only permitted for function parameters");
13396 default_argument = NULL_TREE;
13401 default_argument = NULL_TREE;
13403 return make_parameter_declarator (&decl_specifiers,
13408 /* Parse a function-body.
13411 compound_statement */
13414 cp_parser_function_body (cp_parser *parser)
13416 cp_parser_compound_statement (parser, NULL, false);
13419 /* Parse a ctor-initializer-opt followed by a function-body. Return
13420 true if a ctor-initializer was present. */
13423 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13426 bool ctor_initializer_p;
13428 /* Begin the function body. */
13429 body = begin_function_body ();
13430 /* Parse the optional ctor-initializer. */
13431 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13432 /* Parse the function-body. */
13433 cp_parser_function_body (parser);
13434 /* Finish the function body. */
13435 finish_function_body (body);
13437 return ctor_initializer_p;
13440 /* Parse an initializer.
13443 = initializer-clause
13444 ( expression-list )
13446 Returns an expression representing the initializer. If no
13447 initializer is present, NULL_TREE is returned.
13449 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13450 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13451 set to FALSE if there is no initializer present. If there is an
13452 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13453 is set to true; otherwise it is set to false. */
13456 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13457 bool* non_constant_p)
13462 /* Peek at the next token. */
13463 token = cp_lexer_peek_token (parser->lexer);
13465 /* Let our caller know whether or not this initializer was
13467 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13468 /* Assume that the initializer is constant. */
13469 *non_constant_p = false;
13471 if (token->type == CPP_EQ)
13473 /* Consume the `='. */
13474 cp_lexer_consume_token (parser->lexer);
13475 /* Parse the initializer-clause. */
13476 init = cp_parser_initializer_clause (parser, non_constant_p);
13478 else if (token->type == CPP_OPEN_PAREN)
13479 init = cp_parser_parenthesized_expression_list (parser, false,
13481 /*allow_expansion_p=*/true,
13485 /* Anything else is an error. */
13486 cp_parser_error (parser, "expected initializer");
13487 init = error_mark_node;
13493 /* Parse an initializer-clause.
13495 initializer-clause:
13496 assignment-expression
13497 { initializer-list , [opt] }
13500 Returns an expression representing the initializer.
13502 If the `assignment-expression' production is used the value
13503 returned is simply a representation for the expression.
13505 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13506 the elements of the initializer-list (or NULL, if the last
13507 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13508 NULL_TREE. There is no way to detect whether or not the optional
13509 trailing `,' was provided. NON_CONSTANT_P is as for
13510 cp_parser_initializer. */
13513 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13517 /* Assume the expression is constant. */
13518 *non_constant_p = false;
13520 /* If it is not a `{', then we are looking at an
13521 assignment-expression. */
13522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13525 = cp_parser_constant_expression (parser,
13526 /*allow_non_constant_p=*/true,
13528 if (!*non_constant_p)
13529 initializer = fold_non_dependent_expr (initializer);
13533 /* Consume the `{' token. */
13534 cp_lexer_consume_token (parser->lexer);
13535 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13536 initializer = make_node (CONSTRUCTOR);
13537 /* If it's not a `}', then there is a non-trivial initializer. */
13538 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13540 /* Parse the initializer list. */
13541 CONSTRUCTOR_ELTS (initializer)
13542 = cp_parser_initializer_list (parser, non_constant_p);
13543 /* A trailing `,' token is allowed. */
13544 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13545 cp_lexer_consume_token (parser->lexer);
13547 /* Now, there should be a trailing `}'. */
13548 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13551 return initializer;
13554 /* Parse an initializer-list.
13557 initializer-clause ... [opt]
13558 initializer-list , initializer-clause ... [opt]
13563 identifier : initializer-clause
13564 initializer-list, identifier : initializer-clause
13566 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13567 for the initializer. If the INDEX of the elt is non-NULL, it is the
13568 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13569 as for cp_parser_initializer. */
13571 static VEC(constructor_elt,gc) *
13572 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13574 VEC(constructor_elt,gc) *v = NULL;
13576 /* Assume all of the expressions are constant. */
13577 *non_constant_p = false;
13579 /* Parse the rest of the list. */
13585 bool clause_non_constant_p;
13587 /* If the next token is an identifier and the following one is a
13588 colon, we are looking at the GNU designated-initializer
13590 if (cp_parser_allow_gnu_extensions_p (parser)
13591 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13592 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13594 /* Warn the user that they are using an extension. */
13596 pedwarn ("ISO C++ does not allow designated initializers");
13597 /* Consume the identifier. */
13598 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13599 /* Consume the `:'. */
13600 cp_lexer_consume_token (parser->lexer);
13603 identifier = NULL_TREE;
13605 /* Parse the initializer. */
13606 initializer = cp_parser_initializer_clause (parser,
13607 &clause_non_constant_p);
13608 /* If any clause is non-constant, so is the entire initializer. */
13609 if (clause_non_constant_p)
13610 *non_constant_p = true;
13612 /* If we have an ellipsis, this is an initializer pack
13614 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13616 /* Consume the `...'. */
13617 cp_lexer_consume_token (parser->lexer);
13619 /* Turn the initializer into an initializer expansion. */
13620 initializer = make_pack_expansion (initializer);
13623 /* Add it to the vector. */
13624 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13626 /* If the next token is not a comma, we have reached the end of
13628 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13631 /* Peek at the next token. */
13632 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13633 /* If the next token is a `}', then we're still done. An
13634 initializer-clause can have a trailing `,' after the
13635 initializer-list and before the closing `}'. */
13636 if (token->type == CPP_CLOSE_BRACE)
13639 /* Consume the `,' token. */
13640 cp_lexer_consume_token (parser->lexer);
13646 /* Classes [gram.class] */
13648 /* Parse a class-name.
13654 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13655 to indicate that names looked up in dependent types should be
13656 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13657 keyword has been used to indicate that the name that appears next
13658 is a template. TAG_TYPE indicates the explicit tag given before
13659 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13660 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13661 is the class being defined in a class-head.
13663 Returns the TYPE_DECL representing the class. */
13666 cp_parser_class_name (cp_parser *parser,
13667 bool typename_keyword_p,
13668 bool template_keyword_p,
13669 enum tag_types tag_type,
13670 bool check_dependency_p,
13672 bool is_declaration)
13679 /* All class-names start with an identifier. */
13680 token = cp_lexer_peek_token (parser->lexer);
13681 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13683 cp_parser_error (parser, "expected class-name");
13684 return error_mark_node;
13687 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13688 to a template-id, so we save it here. */
13689 scope = parser->scope;
13690 if (scope == error_mark_node)
13691 return error_mark_node;
13693 /* Any name names a type if we're following the `typename' keyword
13694 in a qualified name where the enclosing scope is type-dependent. */
13695 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13696 && dependent_type_p (scope));
13697 /* Handle the common case (an identifier, but not a template-id)
13699 if (token->type == CPP_NAME
13700 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13702 cp_token *identifier_token;
13706 /* Look for the identifier. */
13707 identifier_token = cp_lexer_peek_token (parser->lexer);
13708 ambiguous_p = identifier_token->ambiguous_p;
13709 identifier = cp_parser_identifier (parser);
13710 /* If the next token isn't an identifier, we are certainly not
13711 looking at a class-name. */
13712 if (identifier == error_mark_node)
13713 decl = error_mark_node;
13714 /* If we know this is a type-name, there's no need to look it
13716 else if (typename_p)
13720 tree ambiguous_decls;
13721 /* If we already know that this lookup is ambiguous, then
13722 we've already issued an error message; there's no reason
13726 cp_parser_simulate_error (parser);
13727 return error_mark_node;
13729 /* If the next token is a `::', then the name must be a type
13732 [basic.lookup.qual]
13734 During the lookup for a name preceding the :: scope
13735 resolution operator, object, function, and enumerator
13736 names are ignored. */
13737 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13738 tag_type = typename_type;
13739 /* Look up the name. */
13740 decl = cp_parser_lookup_name (parser, identifier,
13742 /*is_template=*/false,
13743 /*is_namespace=*/false,
13744 check_dependency_p,
13746 if (ambiguous_decls)
13748 error ("reference to %qD is ambiguous", identifier);
13749 print_candidates (ambiguous_decls);
13750 if (cp_parser_parsing_tentatively (parser))
13752 identifier_token->ambiguous_p = true;
13753 cp_parser_simulate_error (parser);
13755 return error_mark_node;
13761 /* Try a template-id. */
13762 decl = cp_parser_template_id (parser, template_keyword_p,
13763 check_dependency_p,
13765 if (decl == error_mark_node)
13766 return error_mark_node;
13769 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
13771 /* If this is a typename, create a TYPENAME_TYPE. */
13772 if (typename_p && decl != error_mark_node)
13774 decl = make_typename_type (scope, decl, typename_type,
13775 /*complain=*/tf_error);
13776 if (decl != error_mark_node)
13777 decl = TYPE_NAME (decl);
13780 /* Check to see that it is really the name of a class. */
13781 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13782 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
13783 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13784 /* Situations like this:
13786 template <typename T> struct A {
13787 typename T::template X<int>::I i;
13790 are problematic. Is `T::template X<int>' a class-name? The
13791 standard does not seem to be definitive, but there is no other
13792 valid interpretation of the following `::'. Therefore, those
13793 names are considered class-names. */
13795 decl = make_typename_type (scope, decl, tag_type, tf_error);
13796 if (decl != error_mark_node)
13797 decl = TYPE_NAME (decl);
13799 else if (TREE_CODE (decl) != TYPE_DECL
13800 || TREE_TYPE (decl) == error_mark_node
13801 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
13802 decl = error_mark_node;
13804 if (decl == error_mark_node)
13805 cp_parser_error (parser, "expected class-name");
13810 /* Parse a class-specifier.
13813 class-head { member-specification [opt] }
13815 Returns the TREE_TYPE representing the class. */
13818 cp_parser_class_specifier (cp_parser* parser)
13822 tree attributes = NULL_TREE;
13823 int has_trailing_semicolon;
13824 bool nested_name_specifier_p;
13825 unsigned saved_num_template_parameter_lists;
13826 bool saved_in_function_body;
13827 tree old_scope = NULL_TREE;
13828 tree scope = NULL_TREE;
13831 push_deferring_access_checks (dk_no_deferred);
13833 /* Parse the class-head. */
13834 type = cp_parser_class_head (parser,
13835 &nested_name_specifier_p,
13838 /* If the class-head was a semantic disaster, skip the entire body
13842 cp_parser_skip_to_end_of_block_or_statement (parser);
13843 pop_deferring_access_checks ();
13844 return error_mark_node;
13847 /* Look for the `{'. */
13848 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
13850 pop_deferring_access_checks ();
13851 return error_mark_node;
13854 /* Process the base classes. If they're invalid, skip the
13855 entire class body. */
13856 if (!xref_basetypes (type, bases))
13858 /* Consuming the closing brace yields better error messages
13860 if (cp_parser_skip_to_closing_brace (parser))
13861 cp_lexer_consume_token (parser->lexer);
13862 pop_deferring_access_checks ();
13863 return error_mark_node;
13866 /* Issue an error message if type-definitions are forbidden here. */
13867 cp_parser_check_type_definition (parser);
13868 /* Remember that we are defining one more class. */
13869 ++parser->num_classes_being_defined;
13870 /* Inside the class, surrounding template-parameter-lists do not
13872 saved_num_template_parameter_lists
13873 = parser->num_template_parameter_lists;
13874 parser->num_template_parameter_lists = 0;
13875 /* We are not in a function body. */
13876 saved_in_function_body = parser->in_function_body;
13877 parser->in_function_body = false;
13879 /* Start the class. */
13880 if (nested_name_specifier_p)
13882 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
13883 old_scope = push_inner_scope (scope);
13885 type = begin_class_definition (type, attributes);
13887 if (type == error_mark_node)
13888 /* If the type is erroneous, skip the entire body of the class. */
13889 cp_parser_skip_to_closing_brace (parser);
13891 /* Parse the member-specification. */
13892 cp_parser_member_specification_opt (parser);
13894 /* Look for the trailing `}'. */
13895 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13896 /* We get better error messages by noticing a common problem: a
13897 missing trailing `;'. */
13898 token = cp_lexer_peek_token (parser->lexer);
13899 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
13900 /* Look for trailing attributes to apply to this class. */
13901 if (cp_parser_allow_gnu_extensions_p (parser))
13902 attributes = cp_parser_attributes_opt (parser);
13903 if (type != error_mark_node)
13904 type = finish_struct (type, attributes);
13905 if (nested_name_specifier_p)
13906 pop_inner_scope (old_scope, scope);
13907 /* If this class is not itself within the scope of another class,
13908 then we need to parse the bodies of all of the queued function
13909 definitions. Note that the queued functions defined in a class
13910 are not always processed immediately following the
13911 class-specifier for that class. Consider:
13914 struct B { void f() { sizeof (A); } };
13917 If `f' were processed before the processing of `A' were
13918 completed, there would be no way to compute the size of `A'.
13919 Note that the nesting we are interested in here is lexical --
13920 not the semantic nesting given by TYPE_CONTEXT. In particular,
13923 struct A { struct B; };
13924 struct A::B { void f() { } };
13926 there is no need to delay the parsing of `A::B::f'. */
13927 if (--parser->num_classes_being_defined == 0)
13931 tree class_type = NULL_TREE;
13932 tree pushed_scope = NULL_TREE;
13934 /* In a first pass, parse default arguments to the functions.
13935 Then, in a second pass, parse the bodies of the functions.
13936 This two-phased approach handles cases like:
13944 for (TREE_PURPOSE (parser->unparsed_functions_queues)
13945 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
13946 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
13947 TREE_PURPOSE (parser->unparsed_functions_queues)
13948 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
13950 fn = TREE_VALUE (queue_entry);
13951 /* If there are default arguments that have not yet been processed,
13952 take care of them now. */
13953 if (class_type != TREE_PURPOSE (queue_entry))
13956 pop_scope (pushed_scope);
13957 class_type = TREE_PURPOSE (queue_entry);
13958 pushed_scope = push_scope (class_type);
13960 /* Make sure that any template parameters are in scope. */
13961 maybe_begin_member_template_processing (fn);
13962 /* Parse the default argument expressions. */
13963 cp_parser_late_parsing_default_args (parser, fn);
13964 /* Remove any template parameters from the symbol table. */
13965 maybe_end_member_template_processing ();
13968 pop_scope (pushed_scope);
13969 /* Now parse the body of the functions. */
13970 for (TREE_VALUE (parser->unparsed_functions_queues)
13971 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
13972 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
13973 TREE_VALUE (parser->unparsed_functions_queues)
13974 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
13976 /* Figure out which function we need to process. */
13977 fn = TREE_VALUE (queue_entry);
13978 /* Parse the function. */
13979 cp_parser_late_parsing_for_member (parser, fn);
13983 /* Put back any saved access checks. */
13984 pop_deferring_access_checks ();
13986 /* Restore saved state. */
13987 parser->in_function_body = saved_in_function_body;
13988 parser->num_template_parameter_lists
13989 = saved_num_template_parameter_lists;
13994 /* Parse a class-head.
13997 class-key identifier [opt] base-clause [opt]
13998 class-key nested-name-specifier identifier base-clause [opt]
13999 class-key nested-name-specifier [opt] template-id
14003 class-key attributes identifier [opt] base-clause [opt]
14004 class-key attributes nested-name-specifier identifier base-clause [opt]
14005 class-key attributes nested-name-specifier [opt] template-id
14008 Upon return BASES is initialized to the list of base classes (or
14009 NULL, if there are none) in the same form returned by
14010 cp_parser_base_clause.
14012 Returns the TYPE of the indicated class. Sets
14013 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14014 involving a nested-name-specifier was used, and FALSE otherwise.
14016 Returns error_mark_node if this is not a class-head.
14018 Returns NULL_TREE if the class-head is syntactically valid, but
14019 semantically invalid in a way that means we should skip the entire
14020 body of the class. */
14023 cp_parser_class_head (cp_parser* parser,
14024 bool* nested_name_specifier_p,
14025 tree *attributes_p,
14028 tree nested_name_specifier;
14029 enum tag_types class_key;
14030 tree id = NULL_TREE;
14031 tree type = NULL_TREE;
14033 bool template_id_p = false;
14034 bool qualified_p = false;
14035 bool invalid_nested_name_p = false;
14036 bool invalid_explicit_specialization_p = false;
14037 tree pushed_scope = NULL_TREE;
14038 unsigned num_templates;
14040 /* Assume no nested-name-specifier will be present. */
14041 *nested_name_specifier_p = false;
14042 /* Assume no template parameter lists will be used in defining the
14046 *bases = NULL_TREE;
14048 /* Look for the class-key. */
14049 class_key = cp_parser_class_key (parser);
14050 if (class_key == none_type)
14051 return error_mark_node;
14053 /* Parse the attributes. */
14054 attributes = cp_parser_attributes_opt (parser);
14056 /* If the next token is `::', that is invalid -- but sometimes
14057 people do try to write:
14061 Handle this gracefully by accepting the extra qualifier, and then
14062 issuing an error about it later if this really is a
14063 class-head. If it turns out just to be an elaborated type
14064 specifier, remain silent. */
14065 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14066 qualified_p = true;
14068 push_deferring_access_checks (dk_no_check);
14070 /* Determine the name of the class. Begin by looking for an
14071 optional nested-name-specifier. */
14072 nested_name_specifier
14073 = cp_parser_nested_name_specifier_opt (parser,
14074 /*typename_keyword_p=*/false,
14075 /*check_dependency_p=*/false,
14077 /*is_declaration=*/false);
14078 /* If there was a nested-name-specifier, then there *must* be an
14080 if (nested_name_specifier)
14082 /* Although the grammar says `identifier', it really means
14083 `class-name' or `template-name'. You are only allowed to
14084 define a class that has already been declared with this
14087 The proposed resolution for Core Issue 180 says that wherever
14088 you see `class T::X' you should treat `X' as a type-name.
14090 It is OK to define an inaccessible class; for example:
14092 class A { class B; };
14095 We do not know if we will see a class-name, or a
14096 template-name. We look for a class-name first, in case the
14097 class-name is a template-id; if we looked for the
14098 template-name first we would stop after the template-name. */
14099 cp_parser_parse_tentatively (parser);
14100 type = cp_parser_class_name (parser,
14101 /*typename_keyword_p=*/false,
14102 /*template_keyword_p=*/false,
14104 /*check_dependency_p=*/false,
14105 /*class_head_p=*/true,
14106 /*is_declaration=*/false);
14107 /* If that didn't work, ignore the nested-name-specifier. */
14108 if (!cp_parser_parse_definitely (parser))
14110 invalid_nested_name_p = true;
14111 id = cp_parser_identifier (parser);
14112 if (id == error_mark_node)
14115 /* If we could not find a corresponding TYPE, treat this
14116 declaration like an unqualified declaration. */
14117 if (type == error_mark_node)
14118 nested_name_specifier = NULL_TREE;
14119 /* Otherwise, count the number of templates used in TYPE and its
14120 containing scopes. */
14125 for (scope = TREE_TYPE (type);
14126 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14127 scope = (TYPE_P (scope)
14128 ? TYPE_CONTEXT (scope)
14129 : DECL_CONTEXT (scope)))
14131 && CLASS_TYPE_P (scope)
14132 && CLASSTYPE_TEMPLATE_INFO (scope)
14133 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14134 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14138 /* Otherwise, the identifier is optional. */
14141 /* We don't know whether what comes next is a template-id,
14142 an identifier, or nothing at all. */
14143 cp_parser_parse_tentatively (parser);
14144 /* Check for a template-id. */
14145 id = cp_parser_template_id (parser,
14146 /*template_keyword_p=*/false,
14147 /*check_dependency_p=*/true,
14148 /*is_declaration=*/true);
14149 /* If that didn't work, it could still be an identifier. */
14150 if (!cp_parser_parse_definitely (parser))
14152 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14153 id = cp_parser_identifier (parser);
14159 template_id_p = true;
14164 pop_deferring_access_checks ();
14167 cp_parser_check_for_invalid_template_id (parser, id);
14169 /* If it's not a `:' or a `{' then we can't really be looking at a
14170 class-head, since a class-head only appears as part of a
14171 class-specifier. We have to detect this situation before calling
14172 xref_tag, since that has irreversible side-effects. */
14173 if (!cp_parser_next_token_starts_class_definition_p (parser))
14175 cp_parser_error (parser, "expected %<{%> or %<:%>");
14176 return error_mark_node;
14179 /* At this point, we're going ahead with the class-specifier, even
14180 if some other problem occurs. */
14181 cp_parser_commit_to_tentative_parse (parser);
14182 /* Issue the error about the overly-qualified name now. */
14184 cp_parser_error (parser,
14185 "global qualification of class name is invalid");
14186 else if (invalid_nested_name_p)
14187 cp_parser_error (parser,
14188 "qualified name does not name a class");
14189 else if (nested_name_specifier)
14193 /* Reject typedef-names in class heads. */
14194 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14196 error ("invalid class name in declaration of %qD", type);
14201 /* Figure out in what scope the declaration is being placed. */
14202 scope = current_scope ();
14203 /* If that scope does not contain the scope in which the
14204 class was originally declared, the program is invalid. */
14205 if (scope && !is_ancestor (scope, nested_name_specifier))
14207 error ("declaration of %qD in %qD which does not enclose %qD",
14208 type, scope, nested_name_specifier);
14214 A declarator-id shall not be qualified exception of the
14215 definition of a ... nested class outside of its class
14216 ... [or] a the definition or explicit instantiation of a
14217 class member of a namespace outside of its namespace. */
14218 if (scope == nested_name_specifier)
14220 pedwarn ("extra qualification ignored");
14221 nested_name_specifier = NULL_TREE;
14225 /* An explicit-specialization must be preceded by "template <>". If
14226 it is not, try to recover gracefully. */
14227 if (at_namespace_scope_p ()
14228 && parser->num_template_parameter_lists == 0
14231 error ("an explicit specialization must be preceded by %<template <>%>");
14232 invalid_explicit_specialization_p = true;
14233 /* Take the same action that would have been taken by
14234 cp_parser_explicit_specialization. */
14235 ++parser->num_template_parameter_lists;
14236 begin_specialization ();
14238 /* There must be no "return" statements between this point and the
14239 end of this function; set "type "to the correct return value and
14240 use "goto done;" to return. */
14241 /* Make sure that the right number of template parameters were
14243 if (!cp_parser_check_template_parameters (parser, num_templates))
14245 /* If something went wrong, there is no point in even trying to
14246 process the class-definition. */
14251 /* Look up the type. */
14254 type = TREE_TYPE (id);
14255 type = maybe_process_partial_specialization (type);
14256 if (nested_name_specifier)
14257 pushed_scope = push_scope (nested_name_specifier);
14259 else if (nested_name_specifier)
14265 template <typename T> struct S { struct T };
14266 template <typename T> struct S<T>::T { };
14268 we will get a TYPENAME_TYPE when processing the definition of
14269 `S::T'. We need to resolve it to the actual type before we
14270 try to define it. */
14271 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14273 class_type = resolve_typename_type (TREE_TYPE (type),
14274 /*only_current_p=*/false);
14275 if (class_type != error_mark_node)
14276 type = TYPE_NAME (class_type);
14279 cp_parser_error (parser, "could not resolve typename type");
14280 type = error_mark_node;
14284 maybe_process_partial_specialization (TREE_TYPE (type));
14285 class_type = current_class_type;
14286 /* Enter the scope indicated by the nested-name-specifier. */
14287 pushed_scope = push_scope (nested_name_specifier);
14288 /* Get the canonical version of this type. */
14289 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14290 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14291 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14293 type = push_template_decl (type);
14294 if (type == error_mark_node)
14301 type = TREE_TYPE (type);
14302 *nested_name_specifier_p = true;
14304 else /* The name is not a nested name. */
14306 /* If the class was unnamed, create a dummy name. */
14308 id = make_anon_name ();
14309 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14310 parser->num_template_parameter_lists);
14313 /* Indicate whether this class was declared as a `class' or as a
14315 if (TREE_CODE (type) == RECORD_TYPE)
14316 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14317 cp_parser_check_class_key (class_key, type);
14319 /* If this type was already complete, and we see another definition,
14320 that's an error. */
14321 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14323 error ("redefinition of %q#T", type);
14324 error ("previous definition of %q+#T", type);
14328 else if (type == error_mark_node)
14331 /* We will have entered the scope containing the class; the names of
14332 base classes should be looked up in that context. For example:
14334 struct A { struct B {}; struct C; };
14335 struct A::C : B {};
14339 /* Get the list of base-classes, if there is one. */
14340 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14341 *bases = cp_parser_base_clause (parser);
14344 /* Leave the scope given by the nested-name-specifier. We will
14345 enter the class scope itself while processing the members. */
14347 pop_scope (pushed_scope);
14349 if (invalid_explicit_specialization_p)
14351 end_specialization ();
14352 --parser->num_template_parameter_lists;
14354 *attributes_p = attributes;
14358 /* Parse a class-key.
14365 Returns the kind of class-key specified, or none_type to indicate
14368 static enum tag_types
14369 cp_parser_class_key (cp_parser* parser)
14372 enum tag_types tag_type;
14374 /* Look for the class-key. */
14375 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14379 /* Check to see if the TOKEN is a class-key. */
14380 tag_type = cp_parser_token_is_class_key (token);
14382 cp_parser_error (parser, "expected class-key");
14386 /* Parse an (optional) member-specification.
14388 member-specification:
14389 member-declaration member-specification [opt]
14390 access-specifier : member-specification [opt] */
14393 cp_parser_member_specification_opt (cp_parser* parser)
14400 /* Peek at the next token. */
14401 token = cp_lexer_peek_token (parser->lexer);
14402 /* If it's a `}', or EOF then we've seen all the members. */
14403 if (token->type == CPP_CLOSE_BRACE
14404 || token->type == CPP_EOF
14405 || token->type == CPP_PRAGMA_EOL)
14408 /* See if this token is a keyword. */
14409 keyword = token->keyword;
14413 case RID_PROTECTED:
14415 /* Consume the access-specifier. */
14416 cp_lexer_consume_token (parser->lexer);
14417 /* Remember which access-specifier is active. */
14418 current_access_specifier = token->u.value;
14419 /* Look for the `:'. */
14420 cp_parser_require (parser, CPP_COLON, "`:'");
14424 /* Accept #pragmas at class scope. */
14425 if (token->type == CPP_PRAGMA)
14427 cp_parser_pragma (parser, pragma_external);
14431 /* Otherwise, the next construction must be a
14432 member-declaration. */
14433 cp_parser_member_declaration (parser);
14438 /* Parse a member-declaration.
14440 member-declaration:
14441 decl-specifier-seq [opt] member-declarator-list [opt] ;
14442 function-definition ; [opt]
14443 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14445 template-declaration
14447 member-declarator-list:
14449 member-declarator-list , member-declarator
14452 declarator pure-specifier [opt]
14453 declarator constant-initializer [opt]
14454 identifier [opt] : constant-expression
14458 member-declaration:
14459 __extension__ member-declaration
14462 declarator attributes [opt] pure-specifier [opt]
14463 declarator attributes [opt] constant-initializer [opt]
14464 identifier [opt] attributes [opt] : constant-expression
14468 member-declaration:
14469 static_assert-declaration */
14472 cp_parser_member_declaration (cp_parser* parser)
14474 cp_decl_specifier_seq decl_specifiers;
14475 tree prefix_attributes;
14477 int declares_class_or_enum;
14480 int saved_pedantic;
14482 /* Check for the `__extension__' keyword. */
14483 if (cp_parser_extension_opt (parser, &saved_pedantic))
14486 cp_parser_member_declaration (parser);
14487 /* Restore the old value of the PEDANTIC flag. */
14488 pedantic = saved_pedantic;
14493 /* Check for a template-declaration. */
14494 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14496 /* An explicit specialization here is an error condition, and we
14497 expect the specialization handler to detect and report this. */
14498 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14499 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14500 cp_parser_explicit_specialization (parser);
14502 cp_parser_template_declaration (parser, /*member_p=*/true);
14507 /* Check for a using-declaration. */
14508 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14510 /* Parse the using-declaration. */
14511 cp_parser_using_declaration (parser,
14512 /*access_declaration_p=*/false);
14516 /* Check for @defs. */
14517 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14520 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14521 ivar = ivar_chains;
14525 ivar = TREE_CHAIN (member);
14526 TREE_CHAIN (member) = NULL_TREE;
14527 finish_member_declaration (member);
14532 /* If the next token is `static_assert' we have a static assertion. */
14533 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14535 cp_parser_static_assert (parser, /*member_p=*/true);
14539 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14542 /* Parse the decl-specifier-seq. */
14543 cp_parser_decl_specifier_seq (parser,
14544 CP_PARSER_FLAGS_OPTIONAL,
14546 &declares_class_or_enum);
14547 prefix_attributes = decl_specifiers.attributes;
14548 decl_specifiers.attributes = NULL_TREE;
14549 /* Check for an invalid type-name. */
14550 if (!decl_specifiers.type
14551 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14553 /* If there is no declarator, then the decl-specifier-seq should
14555 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14557 /* If there was no decl-specifier-seq, and the next token is a
14558 `;', then we have something like:
14564 Each member-declaration shall declare at least one member
14565 name of the class. */
14566 if (!decl_specifiers.any_specifiers_p)
14568 cp_token *token = cp_lexer_peek_token (parser->lexer);
14569 if (pedantic && !token->in_system_header)
14570 pedwarn ("%Hextra %<;%>", &token->location);
14576 /* See if this declaration is a friend. */
14577 friend_p = cp_parser_friend_p (&decl_specifiers);
14578 /* If there were decl-specifiers, check to see if there was
14579 a class-declaration. */
14580 type = check_tag_decl (&decl_specifiers);
14581 /* Nested classes have already been added to the class, but
14582 a `friend' needs to be explicitly registered. */
14585 /* If the `friend' keyword was present, the friend must
14586 be introduced with a class-key. */
14587 if (!declares_class_or_enum)
14588 error ("a class-key must be used when declaring a friend");
14591 template <typename T> struct A {
14592 friend struct A<T>::B;
14595 A<T>::B will be represented by a TYPENAME_TYPE, and
14596 therefore not recognized by check_tag_decl. */
14598 && decl_specifiers.type
14599 && TYPE_P (decl_specifiers.type))
14600 type = decl_specifiers.type;
14601 if (!type || !TYPE_P (type))
14602 error ("friend declaration does not name a class or "
14605 make_friend_class (current_class_type, type,
14606 /*complain=*/true);
14608 /* If there is no TYPE, an error message will already have
14610 else if (!type || type == error_mark_node)
14612 /* An anonymous aggregate has to be handled specially; such
14613 a declaration really declares a data member (with a
14614 particular type), as opposed to a nested class. */
14615 else if (ANON_AGGR_TYPE_P (type))
14617 /* Remove constructors and such from TYPE, now that we
14618 know it is an anonymous aggregate. */
14619 fixup_anonymous_aggr (type);
14620 /* And make the corresponding data member. */
14621 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14622 /* Add it to the class. */
14623 finish_member_declaration (decl);
14626 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14631 /* See if these declarations will be friends. */
14632 friend_p = cp_parser_friend_p (&decl_specifiers);
14634 /* Keep going until we hit the `;' at the end of the
14636 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14638 tree attributes = NULL_TREE;
14639 tree first_attribute;
14641 /* Peek at the next token. */
14642 token = cp_lexer_peek_token (parser->lexer);
14644 /* Check for a bitfield declaration. */
14645 if (token->type == CPP_COLON
14646 || (token->type == CPP_NAME
14647 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14653 /* Get the name of the bitfield. Note that we cannot just
14654 check TOKEN here because it may have been invalidated by
14655 the call to cp_lexer_peek_nth_token above. */
14656 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14657 identifier = cp_parser_identifier (parser);
14659 identifier = NULL_TREE;
14661 /* Consume the `:' token. */
14662 cp_lexer_consume_token (parser->lexer);
14663 /* Get the width of the bitfield. */
14665 = cp_parser_constant_expression (parser,
14666 /*allow_non_constant=*/false,
14669 /* Look for attributes that apply to the bitfield. */
14670 attributes = cp_parser_attributes_opt (parser);
14671 /* Remember which attributes are prefix attributes and
14673 first_attribute = attributes;
14674 /* Combine the attributes. */
14675 attributes = chainon (prefix_attributes, attributes);
14677 /* Create the bitfield declaration. */
14678 decl = grokbitfield (identifier
14679 ? make_id_declarator (NULL_TREE,
14685 /* Apply the attributes. */
14686 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14690 cp_declarator *declarator;
14692 tree asm_specification;
14693 int ctor_dtor_or_conv_p;
14695 /* Parse the declarator. */
14697 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14698 &ctor_dtor_or_conv_p,
14699 /*parenthesized_p=*/NULL,
14700 /*member_p=*/true);
14702 /* If something went wrong parsing the declarator, make sure
14703 that we at least consume some tokens. */
14704 if (declarator == cp_error_declarator)
14706 /* Skip to the end of the statement. */
14707 cp_parser_skip_to_end_of_statement (parser);
14708 /* If the next token is not a semicolon, that is
14709 probably because we just skipped over the body of
14710 a function. So, we consume a semicolon if
14711 present, but do not issue an error message if it
14713 if (cp_lexer_next_token_is (parser->lexer,
14715 cp_lexer_consume_token (parser->lexer);
14719 if (declares_class_or_enum & 2)
14720 cp_parser_check_for_definition_in_return_type
14721 (declarator, decl_specifiers.type);
14723 /* Look for an asm-specification. */
14724 asm_specification = cp_parser_asm_specification_opt (parser);
14725 /* Look for attributes that apply to the declaration. */
14726 attributes = cp_parser_attributes_opt (parser);
14727 /* Remember which attributes are prefix attributes and
14729 first_attribute = attributes;
14730 /* Combine the attributes. */
14731 attributes = chainon (prefix_attributes, attributes);
14733 /* If it's an `=', then we have a constant-initializer or a
14734 pure-specifier. It is not correct to parse the
14735 initializer before registering the member declaration
14736 since the member declaration should be in scope while
14737 its initializer is processed. However, the rest of the
14738 front end does not yet provide an interface that allows
14739 us to handle this correctly. */
14740 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14744 A pure-specifier shall be used only in the declaration of
14745 a virtual function.
14747 A member-declarator can contain a constant-initializer
14748 only if it declares a static member of integral or
14751 Therefore, if the DECLARATOR is for a function, we look
14752 for a pure-specifier; otherwise, we look for a
14753 constant-initializer. When we call `grokfield', it will
14754 perform more stringent semantics checks. */
14755 if (function_declarator_p (declarator))
14756 initializer = cp_parser_pure_specifier (parser);
14758 /* Parse the initializer. */
14759 initializer = cp_parser_constant_initializer (parser);
14761 /* Otherwise, there is no initializer. */
14763 initializer = NULL_TREE;
14765 /* See if we are probably looking at a function
14766 definition. We are certainly not looking at a
14767 member-declarator. Calling `grokfield' has
14768 side-effects, so we must not do it unless we are sure
14769 that we are looking at a member-declarator. */
14770 if (cp_parser_token_starts_function_definition_p
14771 (cp_lexer_peek_token (parser->lexer)))
14773 /* The grammar does not allow a pure-specifier to be
14774 used when a member function is defined. (It is
14775 possible that this fact is an oversight in the
14776 standard, since a pure function may be defined
14777 outside of the class-specifier. */
14779 error ("pure-specifier on function-definition");
14780 decl = cp_parser_save_member_function_body (parser,
14784 /* If the member was not a friend, declare it here. */
14786 finish_member_declaration (decl);
14787 /* Peek at the next token. */
14788 token = cp_lexer_peek_token (parser->lexer);
14789 /* If the next token is a semicolon, consume it. */
14790 if (token->type == CPP_SEMICOLON)
14792 if (pedantic && !in_system_header)
14793 pedwarn ("extra %<;%>");
14794 cp_lexer_consume_token (parser->lexer);
14799 /* Create the declaration. */
14800 decl = grokfield (declarator, &decl_specifiers,
14801 initializer, /*init_const_expr_p=*/true,
14806 /* Reset PREFIX_ATTRIBUTES. */
14807 while (attributes && TREE_CHAIN (attributes) != first_attribute)
14808 attributes = TREE_CHAIN (attributes);
14810 TREE_CHAIN (attributes) = NULL_TREE;
14812 /* If there is any qualification still in effect, clear it
14813 now; we will be starting fresh with the next declarator. */
14814 parser->scope = NULL_TREE;
14815 parser->qualifying_scope = NULL_TREE;
14816 parser->object_scope = NULL_TREE;
14817 /* If it's a `,', then there are more declarators. */
14818 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14819 cp_lexer_consume_token (parser->lexer);
14820 /* If the next token isn't a `;', then we have a parse error. */
14821 else if (cp_lexer_next_token_is_not (parser->lexer,
14824 cp_parser_error (parser, "expected %<;%>");
14825 /* Skip tokens until we find a `;'. */
14826 cp_parser_skip_to_end_of_statement (parser);
14833 /* Add DECL to the list of members. */
14835 finish_member_declaration (decl);
14837 if (TREE_CODE (decl) == FUNCTION_DECL)
14838 cp_parser_save_default_args (parser, decl);
14843 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14846 /* Parse a pure-specifier.
14851 Returns INTEGER_ZERO_NODE if a pure specifier is found.
14852 Otherwise, ERROR_MARK_NODE is returned. */
14855 cp_parser_pure_specifier (cp_parser* parser)
14859 /* Look for the `=' token. */
14860 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14861 return error_mark_node;
14862 /* Look for the `0' token. */
14863 token = cp_lexer_consume_token (parser->lexer);
14864 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
14865 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
14867 cp_parser_error (parser,
14868 "invalid pure specifier (only `= 0' is allowed)");
14869 cp_parser_skip_to_end_of_statement (parser);
14870 return error_mark_node;
14872 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
14874 error ("templates may not be %<virtual%>");
14875 return error_mark_node;
14878 return integer_zero_node;
14881 /* Parse a constant-initializer.
14883 constant-initializer:
14884 = constant-expression
14886 Returns a representation of the constant-expression. */
14889 cp_parser_constant_initializer (cp_parser* parser)
14891 /* Look for the `=' token. */
14892 if (!cp_parser_require (parser, CPP_EQ, "`='"))
14893 return error_mark_node;
14895 /* It is invalid to write:
14897 struct S { static const int i = { 7 }; };
14900 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14902 cp_parser_error (parser,
14903 "a brace-enclosed initializer is not allowed here");
14904 /* Consume the opening brace. */
14905 cp_lexer_consume_token (parser->lexer);
14906 /* Skip the initializer. */
14907 cp_parser_skip_to_closing_brace (parser);
14908 /* Look for the trailing `}'. */
14909 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14911 return error_mark_node;
14914 return cp_parser_constant_expression (parser,
14915 /*allow_non_constant=*/false,
14919 /* Derived classes [gram.class.derived] */
14921 /* Parse a base-clause.
14924 : base-specifier-list
14926 base-specifier-list:
14927 base-specifier ... [opt]
14928 base-specifier-list , base-specifier ... [opt]
14930 Returns a TREE_LIST representing the base-classes, in the order in
14931 which they were declared. The representation of each node is as
14932 described by cp_parser_base_specifier.
14934 In the case that no bases are specified, this function will return
14935 NULL_TREE, not ERROR_MARK_NODE. */
14938 cp_parser_base_clause (cp_parser* parser)
14940 tree bases = NULL_TREE;
14942 /* Look for the `:' that begins the list. */
14943 cp_parser_require (parser, CPP_COLON, "`:'");
14945 /* Scan the base-specifier-list. */
14950 bool pack_expansion_p = false;
14952 /* Look for the base-specifier. */
14953 base = cp_parser_base_specifier (parser);
14954 /* Look for the (optional) ellipsis. */
14955 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
14957 /* Consume the `...'. */
14958 cp_lexer_consume_token (parser->lexer);
14960 pack_expansion_p = true;
14963 /* Add BASE to the front of the list. */
14964 if (base != error_mark_node)
14966 if (pack_expansion_p)
14967 /* Make this a pack expansion type. */
14968 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
14970 check_for_bare_parameter_packs (TREE_VALUE (base));
14972 TREE_CHAIN (base) = bases;
14975 /* Peek at the next token. */
14976 token = cp_lexer_peek_token (parser->lexer);
14977 /* If it's not a comma, then the list is complete. */
14978 if (token->type != CPP_COMMA)
14980 /* Consume the `,'. */
14981 cp_lexer_consume_token (parser->lexer);
14984 /* PARSER->SCOPE may still be non-NULL at this point, if the last
14985 base class had a qualified name. However, the next name that
14986 appears is certainly not qualified. */
14987 parser->scope = NULL_TREE;
14988 parser->qualifying_scope = NULL_TREE;
14989 parser->object_scope = NULL_TREE;
14991 return nreverse (bases);
14994 /* Parse a base-specifier.
14997 :: [opt] nested-name-specifier [opt] class-name
14998 virtual access-specifier [opt] :: [opt] nested-name-specifier
15000 access-specifier virtual [opt] :: [opt] nested-name-specifier
15003 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15004 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15005 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15006 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15009 cp_parser_base_specifier (cp_parser* parser)
15013 bool virtual_p = false;
15014 bool duplicate_virtual_error_issued_p = false;
15015 bool duplicate_access_error_issued_p = false;
15016 bool class_scope_p, template_p;
15017 tree access = access_default_node;
15020 /* Process the optional `virtual' and `access-specifier'. */
15023 /* Peek at the next token. */
15024 token = cp_lexer_peek_token (parser->lexer);
15025 /* Process `virtual'. */
15026 switch (token->keyword)
15029 /* If `virtual' appears more than once, issue an error. */
15030 if (virtual_p && !duplicate_virtual_error_issued_p)
15032 cp_parser_error (parser,
15033 "%<virtual%> specified more than once in base-specified");
15034 duplicate_virtual_error_issued_p = true;
15039 /* Consume the `virtual' token. */
15040 cp_lexer_consume_token (parser->lexer);
15045 case RID_PROTECTED:
15047 /* If more than one access specifier appears, issue an
15049 if (access != access_default_node
15050 && !duplicate_access_error_issued_p)
15052 cp_parser_error (parser,
15053 "more than one access specifier in base-specified");
15054 duplicate_access_error_issued_p = true;
15057 access = ridpointers[(int) token->keyword];
15059 /* Consume the access-specifier. */
15060 cp_lexer_consume_token (parser->lexer);
15069 /* It is not uncommon to see programs mechanically, erroneously, use
15070 the 'typename' keyword to denote (dependent) qualified types
15071 as base classes. */
15072 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15074 if (!processing_template_decl)
15075 error ("keyword %<typename%> not allowed outside of templates");
15077 error ("keyword %<typename%> not allowed in this context "
15078 "(the base class is implicitly a type)");
15079 cp_lexer_consume_token (parser->lexer);
15082 /* Look for the optional `::' operator. */
15083 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15084 /* Look for the nested-name-specifier. The simplest way to
15089 The keyword `typename' is not permitted in a base-specifier or
15090 mem-initializer; in these contexts a qualified name that
15091 depends on a template-parameter is implicitly assumed to be a
15094 is to pretend that we have seen the `typename' keyword at this
15096 cp_parser_nested_name_specifier_opt (parser,
15097 /*typename_keyword_p=*/true,
15098 /*check_dependency_p=*/true,
15100 /*is_declaration=*/true);
15101 /* If the base class is given by a qualified name, assume that names
15102 we see are type names or templates, as appropriate. */
15103 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15104 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15106 /* Finally, look for the class-name. */
15107 type = cp_parser_class_name (parser,
15111 /*check_dependency_p=*/true,
15112 /*class_head_p=*/false,
15113 /*is_declaration=*/true);
15115 if (type == error_mark_node)
15116 return error_mark_node;
15118 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15121 /* Exception handling [gram.exception] */
15123 /* Parse an (optional) exception-specification.
15125 exception-specification:
15126 throw ( type-id-list [opt] )
15128 Returns a TREE_LIST representing the exception-specification. The
15129 TREE_VALUE of each node is a type. */
15132 cp_parser_exception_specification_opt (cp_parser* parser)
15137 /* Peek at the next token. */
15138 token = cp_lexer_peek_token (parser->lexer);
15139 /* If it's not `throw', then there's no exception-specification. */
15140 if (!cp_parser_is_keyword (token, RID_THROW))
15143 /* Consume the `throw'. */
15144 cp_lexer_consume_token (parser->lexer);
15146 /* Look for the `('. */
15147 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15149 /* Peek at the next token. */
15150 token = cp_lexer_peek_token (parser->lexer);
15151 /* If it's not a `)', then there is a type-id-list. */
15152 if (token->type != CPP_CLOSE_PAREN)
15154 const char *saved_message;
15156 /* Types may not be defined in an exception-specification. */
15157 saved_message = parser->type_definition_forbidden_message;
15158 parser->type_definition_forbidden_message
15159 = "types may not be defined in an exception-specification";
15160 /* Parse the type-id-list. */
15161 type_id_list = cp_parser_type_id_list (parser);
15162 /* Restore the saved message. */
15163 parser->type_definition_forbidden_message = saved_message;
15166 type_id_list = empty_except_spec;
15168 /* Look for the `)'. */
15169 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15171 return type_id_list;
15174 /* Parse an (optional) type-id-list.
15178 type-id-list , type-id ... [opt]
15180 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15181 in the order that the types were presented. */
15184 cp_parser_type_id_list (cp_parser* parser)
15186 tree types = NULL_TREE;
15193 /* Get the next type-id. */
15194 type = cp_parser_type_id (parser);
15195 /* Parse the optional ellipsis. */
15196 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15198 /* Consume the `...'. */
15199 cp_lexer_consume_token (parser->lexer);
15201 /* Turn the type into a pack expansion expression. */
15202 type = make_pack_expansion (type);
15204 /* Add it to the list. */
15205 types = add_exception_specifier (types, type, /*complain=*/1);
15206 /* Peek at the next token. */
15207 token = cp_lexer_peek_token (parser->lexer);
15208 /* If it is not a `,', we are done. */
15209 if (token->type != CPP_COMMA)
15211 /* Consume the `,'. */
15212 cp_lexer_consume_token (parser->lexer);
15215 return nreverse (types);
15218 /* Parse a try-block.
15221 try compound-statement handler-seq */
15224 cp_parser_try_block (cp_parser* parser)
15228 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15229 try_block = begin_try_block ();
15230 cp_parser_compound_statement (parser, NULL, true);
15231 finish_try_block (try_block);
15232 cp_parser_handler_seq (parser);
15233 finish_handler_sequence (try_block);
15238 /* Parse a function-try-block.
15240 function-try-block:
15241 try ctor-initializer [opt] function-body handler-seq */
15244 cp_parser_function_try_block (cp_parser* parser)
15246 tree compound_stmt;
15248 bool ctor_initializer_p;
15250 /* Look for the `try' keyword. */
15251 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15253 /* Let the rest of the front end know where we are. */
15254 try_block = begin_function_try_block (&compound_stmt);
15255 /* Parse the function-body. */
15257 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15258 /* We're done with the `try' part. */
15259 finish_function_try_block (try_block);
15260 /* Parse the handlers. */
15261 cp_parser_handler_seq (parser);
15262 /* We're done with the handlers. */
15263 finish_function_handler_sequence (try_block, compound_stmt);
15265 return ctor_initializer_p;
15268 /* Parse a handler-seq.
15271 handler handler-seq [opt] */
15274 cp_parser_handler_seq (cp_parser* parser)
15280 /* Parse the handler. */
15281 cp_parser_handler (parser);
15282 /* Peek at the next token. */
15283 token = cp_lexer_peek_token (parser->lexer);
15284 /* If it's not `catch' then there are no more handlers. */
15285 if (!cp_parser_is_keyword (token, RID_CATCH))
15290 /* Parse a handler.
15293 catch ( exception-declaration ) compound-statement */
15296 cp_parser_handler (cp_parser* parser)
15301 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15302 handler = begin_handler ();
15303 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15304 declaration = cp_parser_exception_declaration (parser);
15305 finish_handler_parms (declaration, handler);
15306 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15307 cp_parser_compound_statement (parser, NULL, false);
15308 finish_handler (handler);
15311 /* Parse an exception-declaration.
15313 exception-declaration:
15314 type-specifier-seq declarator
15315 type-specifier-seq abstract-declarator
15319 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15320 ellipsis variant is used. */
15323 cp_parser_exception_declaration (cp_parser* parser)
15325 cp_decl_specifier_seq type_specifiers;
15326 cp_declarator *declarator;
15327 const char *saved_message;
15329 /* If it's an ellipsis, it's easy to handle. */
15330 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15332 /* Consume the `...' token. */
15333 cp_lexer_consume_token (parser->lexer);
15337 /* Types may not be defined in exception-declarations. */
15338 saved_message = parser->type_definition_forbidden_message;
15339 parser->type_definition_forbidden_message
15340 = "types may not be defined in exception-declarations";
15342 /* Parse the type-specifier-seq. */
15343 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15345 /* If it's a `)', then there is no declarator. */
15346 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15349 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15350 /*ctor_dtor_or_conv_p=*/NULL,
15351 /*parenthesized_p=*/NULL,
15352 /*member_p=*/false);
15354 /* Restore the saved message. */
15355 parser->type_definition_forbidden_message = saved_message;
15357 if (!type_specifiers.any_specifiers_p)
15358 return error_mark_node;
15360 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15363 /* Parse a throw-expression.
15366 throw assignment-expression [opt]
15368 Returns a THROW_EXPR representing the throw-expression. */
15371 cp_parser_throw_expression (cp_parser* parser)
15376 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15377 token = cp_lexer_peek_token (parser->lexer);
15378 /* Figure out whether or not there is an assignment-expression
15379 following the "throw" keyword. */
15380 if (token->type == CPP_COMMA
15381 || token->type == CPP_SEMICOLON
15382 || token->type == CPP_CLOSE_PAREN
15383 || token->type == CPP_CLOSE_SQUARE
15384 || token->type == CPP_CLOSE_BRACE
15385 || token->type == CPP_COLON)
15386 expression = NULL_TREE;
15388 expression = cp_parser_assignment_expression (parser,
15391 return build_throw (expression);
15394 /* GNU Extensions */
15396 /* Parse an (optional) asm-specification.
15399 asm ( string-literal )
15401 If the asm-specification is present, returns a STRING_CST
15402 corresponding to the string-literal. Otherwise, returns
15406 cp_parser_asm_specification_opt (cp_parser* parser)
15409 tree asm_specification;
15411 /* Peek at the next token. */
15412 token = cp_lexer_peek_token (parser->lexer);
15413 /* If the next token isn't the `asm' keyword, then there's no
15414 asm-specification. */
15415 if (!cp_parser_is_keyword (token, RID_ASM))
15418 /* Consume the `asm' token. */
15419 cp_lexer_consume_token (parser->lexer);
15420 /* Look for the `('. */
15421 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15423 /* Look for the string-literal. */
15424 asm_specification = cp_parser_string_literal (parser, false, false);
15426 /* Look for the `)'. */
15427 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15429 return asm_specification;
15432 /* Parse an asm-operand-list.
15436 asm-operand-list , asm-operand
15439 string-literal ( expression )
15440 [ string-literal ] string-literal ( expression )
15442 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15443 each node is the expression. The TREE_PURPOSE is itself a
15444 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15445 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15446 is a STRING_CST for the string literal before the parenthesis. */
15449 cp_parser_asm_operand_list (cp_parser* parser)
15451 tree asm_operands = NULL_TREE;
15455 tree string_literal;
15459 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15461 /* Consume the `[' token. */
15462 cp_lexer_consume_token (parser->lexer);
15463 /* Read the operand name. */
15464 name = cp_parser_identifier (parser);
15465 if (name != error_mark_node)
15466 name = build_string (IDENTIFIER_LENGTH (name),
15467 IDENTIFIER_POINTER (name));
15468 /* Look for the closing `]'. */
15469 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15473 /* Look for the string-literal. */
15474 string_literal = cp_parser_string_literal (parser, false, false);
15476 /* Look for the `('. */
15477 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15478 /* Parse the expression. */
15479 expression = cp_parser_expression (parser, /*cast_p=*/false);
15480 /* Look for the `)'. */
15481 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15483 /* Add this operand to the list. */
15484 asm_operands = tree_cons (build_tree_list (name, string_literal),
15487 /* If the next token is not a `,', there are no more
15489 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15491 /* Consume the `,'. */
15492 cp_lexer_consume_token (parser->lexer);
15495 return nreverse (asm_operands);
15498 /* Parse an asm-clobber-list.
15502 asm-clobber-list , string-literal
15504 Returns a TREE_LIST, indicating the clobbers in the order that they
15505 appeared. The TREE_VALUE of each node is a STRING_CST. */
15508 cp_parser_asm_clobber_list (cp_parser* parser)
15510 tree clobbers = NULL_TREE;
15514 tree string_literal;
15516 /* Look for the string literal. */
15517 string_literal = cp_parser_string_literal (parser, false, false);
15518 /* Add it to the list. */
15519 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15520 /* If the next token is not a `,', then the list is
15522 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15524 /* Consume the `,' token. */
15525 cp_lexer_consume_token (parser->lexer);
15531 /* Parse an (optional) series of attributes.
15534 attributes attribute
15537 __attribute__ (( attribute-list [opt] ))
15539 The return value is as for cp_parser_attribute_list. */
15542 cp_parser_attributes_opt (cp_parser* parser)
15544 tree attributes = NULL_TREE;
15549 tree attribute_list;
15551 /* Peek at the next token. */
15552 token = cp_lexer_peek_token (parser->lexer);
15553 /* If it's not `__attribute__', then we're done. */
15554 if (token->keyword != RID_ATTRIBUTE)
15557 /* Consume the `__attribute__' keyword. */
15558 cp_lexer_consume_token (parser->lexer);
15559 /* Look for the two `(' tokens. */
15560 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15561 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15563 /* Peek at the next token. */
15564 token = cp_lexer_peek_token (parser->lexer);
15565 if (token->type != CPP_CLOSE_PAREN)
15566 /* Parse the attribute-list. */
15567 attribute_list = cp_parser_attribute_list (parser);
15569 /* If the next token is a `)', then there is no attribute
15571 attribute_list = NULL;
15573 /* Look for the two `)' tokens. */
15574 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15575 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15577 /* Add these new attributes to the list. */
15578 attributes = chainon (attributes, attribute_list);
15584 /* Parse an attribute-list.
15588 attribute-list , attribute
15592 identifier ( identifier )
15593 identifier ( identifier , expression-list )
15594 identifier ( expression-list )
15596 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15597 to an attribute. The TREE_PURPOSE of each node is the identifier
15598 indicating which attribute is in use. The TREE_VALUE represents
15599 the arguments, if any. */
15602 cp_parser_attribute_list (cp_parser* parser)
15604 tree attribute_list = NULL_TREE;
15605 bool save_translate_strings_p = parser->translate_strings_p;
15607 parser->translate_strings_p = false;
15614 /* Look for the identifier. We also allow keywords here; for
15615 example `__attribute__ ((const))' is legal. */
15616 token = cp_lexer_peek_token (parser->lexer);
15617 if (token->type == CPP_NAME
15618 || token->type == CPP_KEYWORD)
15620 tree arguments = NULL_TREE;
15622 /* Consume the token. */
15623 token = cp_lexer_consume_token (parser->lexer);
15625 /* Save away the identifier that indicates which attribute
15627 identifier = token->u.value;
15628 attribute = build_tree_list (identifier, NULL_TREE);
15630 /* Peek at the next token. */
15631 token = cp_lexer_peek_token (parser->lexer);
15632 /* If it's an `(', then parse the attribute arguments. */
15633 if (token->type == CPP_OPEN_PAREN)
15635 arguments = cp_parser_parenthesized_expression_list
15636 (parser, true, /*cast_p=*/false,
15637 /*allow_expansion_p=*/false,
15638 /*non_constant_p=*/NULL);
15639 /* Save the arguments away. */
15640 TREE_VALUE (attribute) = arguments;
15643 if (arguments != error_mark_node)
15645 /* Add this attribute to the list. */
15646 TREE_CHAIN (attribute) = attribute_list;
15647 attribute_list = attribute;
15650 token = cp_lexer_peek_token (parser->lexer);
15652 /* Now, look for more attributes. If the next token isn't a
15653 `,', we're done. */
15654 if (token->type != CPP_COMMA)
15657 /* Consume the comma and keep going. */
15658 cp_lexer_consume_token (parser->lexer);
15660 parser->translate_strings_p = save_translate_strings_p;
15662 /* We built up the list in reverse order. */
15663 return nreverse (attribute_list);
15666 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15667 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15668 current value of the PEDANTIC flag, regardless of whether or not
15669 the `__extension__' keyword is present. The caller is responsible
15670 for restoring the value of the PEDANTIC flag. */
15673 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15675 /* Save the old value of the PEDANTIC flag. */
15676 *saved_pedantic = pedantic;
15678 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15680 /* Consume the `__extension__' token. */
15681 cp_lexer_consume_token (parser->lexer);
15682 /* We're not being pedantic while the `__extension__' keyword is
15692 /* Parse a label declaration.
15695 __label__ label-declarator-seq ;
15697 label-declarator-seq:
15698 identifier , label-declarator-seq
15702 cp_parser_label_declaration (cp_parser* parser)
15704 /* Look for the `__label__' keyword. */
15705 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15711 /* Look for an identifier. */
15712 identifier = cp_parser_identifier (parser);
15713 /* If we failed, stop. */
15714 if (identifier == error_mark_node)
15716 /* Declare it as a label. */
15717 finish_label_decl (identifier);
15718 /* If the next token is a `;', stop. */
15719 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15721 /* Look for the `,' separating the label declarations. */
15722 cp_parser_require (parser, CPP_COMMA, "`,'");
15725 /* Look for the final `;'. */
15726 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15729 /* Support Functions */
15731 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15732 NAME should have one of the representations used for an
15733 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15734 is returned. If PARSER->SCOPE is a dependent type, then a
15735 SCOPE_REF is returned.
15737 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15738 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15739 was formed. Abstractly, such entities should not be passed to this
15740 function, because they do not need to be looked up, but it is
15741 simpler to check for this special case here, rather than at the
15744 In cases not explicitly covered above, this function returns a
15745 DECL, OVERLOAD, or baselink representing the result of the lookup.
15746 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
15749 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
15750 (e.g., "struct") that was used. In that case bindings that do not
15751 refer to types are ignored.
15753 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
15756 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
15759 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
15762 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
15763 TREE_LIST of candidates if name-lookup results in an ambiguity, and
15764 NULL_TREE otherwise. */
15767 cp_parser_lookup_name (cp_parser *parser, tree name,
15768 enum tag_types tag_type,
15771 bool check_dependency,
15772 tree *ambiguous_decls)
15776 tree object_type = parser->context->object_type;
15778 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15779 flags |= LOOKUP_COMPLAIN;
15781 /* Assume that the lookup will be unambiguous. */
15782 if (ambiguous_decls)
15783 *ambiguous_decls = NULL_TREE;
15785 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
15786 no longer valid. Note that if we are parsing tentatively, and
15787 the parse fails, OBJECT_TYPE will be automatically restored. */
15788 parser->context->object_type = NULL_TREE;
15790 if (name == error_mark_node)
15791 return error_mark_node;
15793 /* A template-id has already been resolved; there is no lookup to
15795 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
15797 if (BASELINK_P (name))
15799 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
15800 == TEMPLATE_ID_EXPR);
15804 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
15805 it should already have been checked to make sure that the name
15806 used matches the type being destroyed. */
15807 if (TREE_CODE (name) == BIT_NOT_EXPR)
15811 /* Figure out to which type this destructor applies. */
15813 type = parser->scope;
15814 else if (object_type)
15815 type = object_type;
15817 type = current_class_type;
15818 /* If that's not a class type, there is no destructor. */
15819 if (!type || !CLASS_TYPE_P (type))
15820 return error_mark_node;
15821 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
15822 lazily_declare_fn (sfk_destructor, type);
15823 if (!CLASSTYPE_DESTRUCTORS (type))
15824 return error_mark_node;
15825 /* If it was a class type, return the destructor. */
15826 return CLASSTYPE_DESTRUCTORS (type);
15829 /* By this point, the NAME should be an ordinary identifier. If
15830 the id-expression was a qualified name, the qualifying scope is
15831 stored in PARSER->SCOPE at this point. */
15832 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
15834 /* Perform the lookup. */
15839 if (parser->scope == error_mark_node)
15840 return error_mark_node;
15842 /* If the SCOPE is dependent, the lookup must be deferred until
15843 the template is instantiated -- unless we are explicitly
15844 looking up names in uninstantiated templates. Even then, we
15845 cannot look up the name if the scope is not a class type; it
15846 might, for example, be a template type parameter. */
15847 dependent_p = (TYPE_P (parser->scope)
15848 && !(parser->in_declarator_p
15849 && currently_open_class (parser->scope))
15850 && dependent_type_p (parser->scope));
15851 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
15858 /* The resolution to Core Issue 180 says that `struct
15859 A::B' should be considered a type-name, even if `A'
15861 type = make_typename_type (parser->scope, name, tag_type,
15862 /*complain=*/tf_error);
15863 decl = TYPE_NAME (type);
15865 else if (is_template
15866 && (cp_parser_next_token_ends_template_argument_p (parser)
15867 || cp_lexer_next_token_is (parser->lexer,
15869 decl = make_unbound_class_template (parser->scope,
15871 /*complain=*/tf_error);
15873 decl = build_qualified_name (/*type=*/NULL_TREE,
15874 parser->scope, name,
15879 tree pushed_scope = NULL_TREE;
15881 /* If PARSER->SCOPE is a dependent type, then it must be a
15882 class type, and we must not be checking dependencies;
15883 otherwise, we would have processed this lookup above. So
15884 that PARSER->SCOPE is not considered a dependent base by
15885 lookup_member, we must enter the scope here. */
15887 pushed_scope = push_scope (parser->scope);
15888 /* If the PARSER->SCOPE is a template specialization, it
15889 may be instantiated during name lookup. In that case,
15890 errors may be issued. Even if we rollback the current
15891 tentative parse, those errors are valid. */
15892 decl = lookup_qualified_name (parser->scope, name,
15893 tag_type != none_type,
15894 /*complain=*/true);
15896 pop_scope (pushed_scope);
15898 parser->qualifying_scope = parser->scope;
15899 parser->object_scope = NULL_TREE;
15901 else if (object_type)
15903 tree object_decl = NULL_TREE;
15904 /* Look up the name in the scope of the OBJECT_TYPE, unless the
15905 OBJECT_TYPE is not a class. */
15906 if (CLASS_TYPE_P (object_type))
15907 /* If the OBJECT_TYPE is a template specialization, it may
15908 be instantiated during name lookup. In that case, errors
15909 may be issued. Even if we rollback the current tentative
15910 parse, those errors are valid. */
15911 object_decl = lookup_member (object_type,
15914 tag_type != none_type);
15915 /* Look it up in the enclosing context, too. */
15916 decl = lookup_name_real (name, tag_type != none_type,
15918 /*block_p=*/true, is_namespace, flags);
15919 parser->object_scope = object_type;
15920 parser->qualifying_scope = NULL_TREE;
15922 decl = object_decl;
15926 decl = lookup_name_real (name, tag_type != none_type,
15928 /*block_p=*/true, is_namespace, flags);
15929 parser->qualifying_scope = NULL_TREE;
15930 parser->object_scope = NULL_TREE;
15933 /* If the lookup failed, let our caller know. */
15934 if (!decl || decl == error_mark_node)
15935 return error_mark_node;
15937 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
15938 if (TREE_CODE (decl) == TREE_LIST)
15940 if (ambiguous_decls)
15941 *ambiguous_decls = decl;
15942 /* The error message we have to print is too complicated for
15943 cp_parser_error, so we incorporate its actions directly. */
15944 if (!cp_parser_simulate_error (parser))
15946 error ("reference to %qD is ambiguous", name);
15947 print_candidates (decl);
15949 return error_mark_node;
15952 gcc_assert (DECL_P (decl)
15953 || TREE_CODE (decl) == OVERLOAD
15954 || TREE_CODE (decl) == SCOPE_REF
15955 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
15956 || BASELINK_P (decl));
15958 /* If we have resolved the name of a member declaration, check to
15959 see if the declaration is accessible. When the name resolves to
15960 set of overloaded functions, accessibility is checked when
15961 overload resolution is done.
15963 During an explicit instantiation, access is not checked at all,
15964 as per [temp.explicit]. */
15966 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
15971 /* Like cp_parser_lookup_name, but for use in the typical case where
15972 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
15973 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
15976 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
15978 return cp_parser_lookup_name (parser, name,
15980 /*is_template=*/false,
15981 /*is_namespace=*/false,
15982 /*check_dependency=*/true,
15983 /*ambiguous_decls=*/NULL);
15986 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
15987 the current context, return the TYPE_DECL. If TAG_NAME_P is
15988 true, the DECL indicates the class being defined in a class-head,
15989 or declared in an elaborated-type-specifier.
15991 Otherwise, return DECL. */
15994 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
15996 /* If the TEMPLATE_DECL is being declared as part of a class-head,
15997 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16000 template <typename T> struct B;
16003 template <typename T> struct A::B {};
16005 Similarly, in an elaborated-type-specifier:
16007 namespace N { struct X{}; }
16010 template <typename T> friend struct N::X;
16013 However, if the DECL refers to a class type, and we are in
16014 the scope of the class, then the name lookup automatically
16015 finds the TYPE_DECL created by build_self_reference rather
16016 than a TEMPLATE_DECL. For example, in:
16018 template <class T> struct S {
16022 there is no need to handle such case. */
16024 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16025 return DECL_TEMPLATE_RESULT (decl);
16030 /* If too many, or too few, template-parameter lists apply to the
16031 declarator, issue an error message. Returns TRUE if all went well,
16032 and FALSE otherwise. */
16035 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16036 cp_declarator *declarator)
16038 unsigned num_templates;
16040 /* We haven't seen any classes that involve template parameters yet. */
16043 switch (declarator->kind)
16046 if (declarator->u.id.qualifying_scope)
16051 scope = declarator->u.id.qualifying_scope;
16052 member = declarator->u.id.unqualified_name;
16054 while (scope && CLASS_TYPE_P (scope))
16056 /* You're supposed to have one `template <...>'
16057 for every template class, but you don't need one
16058 for a full specialization. For example:
16060 template <class T> struct S{};
16061 template <> struct S<int> { void f(); };
16062 void S<int>::f () {}
16064 is correct; there shouldn't be a `template <>' for
16065 the definition of `S<int>::f'. */
16066 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16067 /* If SCOPE does not have template information of any
16068 kind, then it is not a template, nor is it nested
16069 within a template. */
16071 if (explicit_class_specialization_p (scope))
16073 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16076 scope = TYPE_CONTEXT (scope);
16079 else if (TREE_CODE (declarator->u.id.unqualified_name)
16080 == TEMPLATE_ID_EXPR)
16081 /* If the DECLARATOR has the form `X<y>' then it uses one
16082 additional level of template parameters. */
16085 return cp_parser_check_template_parameters (parser,
16091 case cdk_reference:
16093 return (cp_parser_check_declarator_template_parameters
16094 (parser, declarator->declarator));
16100 gcc_unreachable ();
16105 /* NUM_TEMPLATES were used in the current declaration. If that is
16106 invalid, return FALSE and issue an error messages. Otherwise,
16110 cp_parser_check_template_parameters (cp_parser* parser,
16111 unsigned num_templates)
16113 /* If there are more template classes than parameter lists, we have
16116 template <class T> void S<T>::R<T>::f (); */
16117 if (parser->num_template_parameter_lists < num_templates)
16119 error ("too few template-parameter-lists");
16122 /* If there are the same number of template classes and parameter
16123 lists, that's OK. */
16124 if (parser->num_template_parameter_lists == num_templates)
16126 /* If there are more, but only one more, then we are referring to a
16127 member template. That's OK too. */
16128 if (parser->num_template_parameter_lists == num_templates + 1)
16130 /* Otherwise, there are too many template parameter lists. We have
16133 template <class T> template <class U> void S::f(); */
16134 error ("too many template-parameter-lists");
16138 /* Parse an optional `::' token indicating that the following name is
16139 from the global namespace. If so, PARSER->SCOPE is set to the
16140 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16141 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16142 Returns the new value of PARSER->SCOPE, if the `::' token is
16143 present, and NULL_TREE otherwise. */
16146 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16150 /* Peek at the next token. */
16151 token = cp_lexer_peek_token (parser->lexer);
16152 /* If we're looking at a `::' token then we're starting from the
16153 global namespace, not our current location. */
16154 if (token->type == CPP_SCOPE)
16156 /* Consume the `::' token. */
16157 cp_lexer_consume_token (parser->lexer);
16158 /* Set the SCOPE so that we know where to start the lookup. */
16159 parser->scope = global_namespace;
16160 parser->qualifying_scope = global_namespace;
16161 parser->object_scope = NULL_TREE;
16163 return parser->scope;
16165 else if (!current_scope_valid_p)
16167 parser->scope = NULL_TREE;
16168 parser->qualifying_scope = NULL_TREE;
16169 parser->object_scope = NULL_TREE;
16175 /* Returns TRUE if the upcoming token sequence is the start of a
16176 constructor declarator. If FRIEND_P is true, the declarator is
16177 preceded by the `friend' specifier. */
16180 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16182 bool constructor_p;
16183 tree type_decl = NULL_TREE;
16184 bool nested_name_p;
16185 cp_token *next_token;
16187 /* The common case is that this is not a constructor declarator, so
16188 try to avoid doing lots of work if at all possible. It's not
16189 valid declare a constructor at function scope. */
16190 if (parser->in_function_body)
16192 /* And only certain tokens can begin a constructor declarator. */
16193 next_token = cp_lexer_peek_token (parser->lexer);
16194 if (next_token->type != CPP_NAME
16195 && next_token->type != CPP_SCOPE
16196 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16197 && next_token->type != CPP_TEMPLATE_ID)
16200 /* Parse tentatively; we are going to roll back all of the tokens
16202 cp_parser_parse_tentatively (parser);
16203 /* Assume that we are looking at a constructor declarator. */
16204 constructor_p = true;
16206 /* Look for the optional `::' operator. */
16207 cp_parser_global_scope_opt (parser,
16208 /*current_scope_valid_p=*/false);
16209 /* Look for the nested-name-specifier. */
16211 = (cp_parser_nested_name_specifier_opt (parser,
16212 /*typename_keyword_p=*/false,
16213 /*check_dependency_p=*/false,
16215 /*is_declaration=*/false)
16217 /* Outside of a class-specifier, there must be a
16218 nested-name-specifier. */
16219 if (!nested_name_p &&
16220 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16222 constructor_p = false;
16223 /* If we still think that this might be a constructor-declarator,
16224 look for a class-name. */
16229 template <typename T> struct S { S(); };
16230 template <typename T> S<T>::S ();
16232 we must recognize that the nested `S' names a class.
16235 template <typename T> S<T>::S<T> ();
16237 we must recognize that the nested `S' names a template. */
16238 type_decl = cp_parser_class_name (parser,
16239 /*typename_keyword_p=*/false,
16240 /*template_keyword_p=*/false,
16242 /*check_dependency_p=*/false,
16243 /*class_head_p=*/false,
16244 /*is_declaration=*/false);
16245 /* If there was no class-name, then this is not a constructor. */
16246 constructor_p = !cp_parser_error_occurred (parser);
16249 /* If we're still considering a constructor, we have to see a `(',
16250 to begin the parameter-declaration-clause, followed by either a
16251 `)', an `...', or a decl-specifier. We need to check for a
16252 type-specifier to avoid being fooled into thinking that:
16256 is a constructor. (It is actually a function named `f' that
16257 takes one parameter (of type `int') and returns a value of type
16260 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16262 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16263 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16264 /* A parameter declaration begins with a decl-specifier,
16265 which is either the "attribute" keyword, a storage class
16266 specifier, or (usually) a type-specifier. */
16267 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16270 tree pushed_scope = NULL_TREE;
16271 unsigned saved_num_template_parameter_lists;
16273 /* Names appearing in the type-specifier should be looked up
16274 in the scope of the class. */
16275 if (current_class_type)
16279 type = TREE_TYPE (type_decl);
16280 if (TREE_CODE (type) == TYPENAME_TYPE)
16282 type = resolve_typename_type (type,
16283 /*only_current_p=*/false);
16284 if (type == error_mark_node)
16286 cp_parser_abort_tentative_parse (parser);
16290 pushed_scope = push_scope (type);
16293 /* Inside the constructor parameter list, surrounding
16294 template-parameter-lists do not apply. */
16295 saved_num_template_parameter_lists
16296 = parser->num_template_parameter_lists;
16297 parser->num_template_parameter_lists = 0;
16299 /* Look for the type-specifier. */
16300 cp_parser_type_specifier (parser,
16301 CP_PARSER_FLAGS_NONE,
16302 /*decl_specs=*/NULL,
16303 /*is_declarator=*/true,
16304 /*declares_class_or_enum=*/NULL,
16305 /*is_cv_qualifier=*/NULL);
16307 parser->num_template_parameter_lists
16308 = saved_num_template_parameter_lists;
16310 /* Leave the scope of the class. */
16312 pop_scope (pushed_scope);
16314 constructor_p = !cp_parser_error_occurred (parser);
16318 constructor_p = false;
16319 /* We did not really want to consume any tokens. */
16320 cp_parser_abort_tentative_parse (parser);
16322 return constructor_p;
16325 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16326 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16327 they must be performed once we are in the scope of the function.
16329 Returns the function defined. */
16332 cp_parser_function_definition_from_specifiers_and_declarator
16333 (cp_parser* parser,
16334 cp_decl_specifier_seq *decl_specifiers,
16336 const cp_declarator *declarator)
16341 /* Begin the function-definition. */
16342 success_p = start_function (decl_specifiers, declarator, attributes);
16344 /* The things we're about to see are not directly qualified by any
16345 template headers we've seen thus far. */
16346 reset_specialization ();
16348 /* If there were names looked up in the decl-specifier-seq that we
16349 did not check, check them now. We must wait until we are in the
16350 scope of the function to perform the checks, since the function
16351 might be a friend. */
16352 perform_deferred_access_checks ();
16356 /* Skip the entire function. */
16357 cp_parser_skip_to_end_of_block_or_statement (parser);
16358 fn = error_mark_node;
16360 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16362 /* Seen already, skip it. An error message has already been output. */
16363 cp_parser_skip_to_end_of_block_or_statement (parser);
16364 fn = current_function_decl;
16365 current_function_decl = NULL_TREE;
16366 /* If this is a function from a class, pop the nested class. */
16367 if (current_class_name)
16368 pop_nested_class ();
16371 fn = cp_parser_function_definition_after_declarator (parser,
16372 /*inline_p=*/false);
16377 /* Parse the part of a function-definition that follows the
16378 declarator. INLINE_P is TRUE iff this function is an inline
16379 function defined with a class-specifier.
16381 Returns the function defined. */
16384 cp_parser_function_definition_after_declarator (cp_parser* parser,
16388 bool ctor_initializer_p = false;
16389 bool saved_in_unbraced_linkage_specification_p;
16390 bool saved_in_function_body;
16391 unsigned saved_num_template_parameter_lists;
16393 saved_in_function_body = parser->in_function_body;
16394 parser->in_function_body = true;
16395 /* If the next token is `return', then the code may be trying to
16396 make use of the "named return value" extension that G++ used to
16398 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16400 /* Consume the `return' keyword. */
16401 cp_lexer_consume_token (parser->lexer);
16402 /* Look for the identifier that indicates what value is to be
16404 cp_parser_identifier (parser);
16405 /* Issue an error message. */
16406 error ("named return values are no longer supported");
16407 /* Skip tokens until we reach the start of the function body. */
16410 cp_token *token = cp_lexer_peek_token (parser->lexer);
16411 if (token->type == CPP_OPEN_BRACE
16412 || token->type == CPP_EOF
16413 || token->type == CPP_PRAGMA_EOL)
16415 cp_lexer_consume_token (parser->lexer);
16418 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16419 anything declared inside `f'. */
16420 saved_in_unbraced_linkage_specification_p
16421 = parser->in_unbraced_linkage_specification_p;
16422 parser->in_unbraced_linkage_specification_p = false;
16423 /* Inside the function, surrounding template-parameter-lists do not
16425 saved_num_template_parameter_lists
16426 = parser->num_template_parameter_lists;
16427 parser->num_template_parameter_lists = 0;
16428 /* If the next token is `try', then we are looking at a
16429 function-try-block. */
16430 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16431 ctor_initializer_p = cp_parser_function_try_block (parser);
16432 /* A function-try-block includes the function-body, so we only do
16433 this next part if we're not processing a function-try-block. */
16436 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16438 /* Finish the function. */
16439 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16440 (inline_p ? 2 : 0));
16441 /* Generate code for it, if necessary. */
16442 expand_or_defer_fn (fn);
16443 /* Restore the saved values. */
16444 parser->in_unbraced_linkage_specification_p
16445 = saved_in_unbraced_linkage_specification_p;
16446 parser->num_template_parameter_lists
16447 = saved_num_template_parameter_lists;
16448 parser->in_function_body = saved_in_function_body;
16453 /* Parse a template-declaration, assuming that the `export' (and
16454 `extern') keywords, if present, has already been scanned. MEMBER_P
16455 is as for cp_parser_template_declaration. */
16458 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16460 tree decl = NULL_TREE;
16461 VEC (deferred_access_check,gc) *checks;
16462 tree parameter_list;
16463 bool friend_p = false;
16464 bool need_lang_pop;
16466 /* Look for the `template' keyword. */
16467 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16471 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16473 if (at_class_scope_p () && current_function_decl)
16475 /* 14.5.2.2 [temp.mem]
16477 A local class shall not have member templates. */
16478 error ("invalid declaration of member template in local class");
16479 cp_parser_skip_to_end_of_block_or_statement (parser);
16484 A template ... shall not have C linkage. */
16485 if (current_lang_name == lang_name_c)
16487 error ("template with C linkage");
16488 /* Give it C++ linkage to avoid confusing other parts of the
16490 push_lang_context (lang_name_cplusplus);
16491 need_lang_pop = true;
16494 need_lang_pop = false;
16496 /* We cannot perform access checks on the template parameter
16497 declarations until we know what is being declared, just as we
16498 cannot check the decl-specifier list. */
16499 push_deferring_access_checks (dk_deferred);
16501 /* If the next token is `>', then we have an invalid
16502 specialization. Rather than complain about an invalid template
16503 parameter, issue an error message here. */
16504 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16506 cp_parser_error (parser, "invalid explicit specialization");
16507 begin_specialization ();
16508 parameter_list = NULL_TREE;
16511 /* Parse the template parameters. */
16512 parameter_list = cp_parser_template_parameter_list (parser);
16514 /* Get the deferred access checks from the parameter list. These
16515 will be checked once we know what is being declared, as for a
16516 member template the checks must be performed in the scope of the
16517 class containing the member. */
16518 checks = get_deferred_access_checks ();
16520 /* Look for the `>'. */
16521 cp_parser_skip_to_end_of_template_parameter_list (parser);
16522 /* We just processed one more parameter list. */
16523 ++parser->num_template_parameter_lists;
16524 /* If the next token is `template', there are more template
16526 if (cp_lexer_next_token_is_keyword (parser->lexer,
16528 cp_parser_template_declaration_after_export (parser, member_p);
16531 /* There are no access checks when parsing a template, as we do not
16532 know if a specialization will be a friend. */
16533 push_deferring_access_checks (dk_no_check);
16534 decl = cp_parser_single_declaration (parser,
16537 /*explicit_specialization_p=*/false,
16539 pop_deferring_access_checks ();
16541 /* If this is a member template declaration, let the front
16543 if (member_p && !friend_p && decl)
16545 if (TREE_CODE (decl) == TYPE_DECL)
16546 cp_parser_check_access_in_redeclaration (decl);
16548 decl = finish_member_template_decl (decl);
16550 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16551 make_friend_class (current_class_type, TREE_TYPE (decl),
16552 /*complain=*/true);
16554 /* We are done with the current parameter list. */
16555 --parser->num_template_parameter_lists;
16557 pop_deferring_access_checks ();
16560 finish_template_decl (parameter_list);
16562 /* Register member declarations. */
16563 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16564 finish_member_declaration (decl);
16565 /* For the erroneous case of a template with C linkage, we pushed an
16566 implicit C++ linkage scope; exit that scope now. */
16568 pop_lang_context ();
16569 /* If DECL is a function template, we must return to parse it later.
16570 (Even though there is no definition, there might be default
16571 arguments that need handling.) */
16572 if (member_p && decl
16573 && (TREE_CODE (decl) == FUNCTION_DECL
16574 || DECL_FUNCTION_TEMPLATE_P (decl)))
16575 TREE_VALUE (parser->unparsed_functions_queues)
16576 = tree_cons (NULL_TREE, decl,
16577 TREE_VALUE (parser->unparsed_functions_queues));
16580 /* Perform the deferred access checks from a template-parameter-list.
16581 CHECKS is a TREE_LIST of access checks, as returned by
16582 get_deferred_access_checks. */
16585 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16587 ++processing_template_parmlist;
16588 perform_access_checks (checks);
16589 --processing_template_parmlist;
16592 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16593 `function-definition' sequence. MEMBER_P is true, this declaration
16594 appears in a class scope.
16596 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16597 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16600 cp_parser_single_declaration (cp_parser* parser,
16601 VEC (deferred_access_check,gc)* checks,
16603 bool explicit_specialization_p,
16606 int declares_class_or_enum;
16607 tree decl = NULL_TREE;
16608 cp_decl_specifier_seq decl_specifiers;
16609 bool function_definition_p = false;
16611 /* This function is only used when processing a template
16613 gcc_assert (innermost_scope_kind () == sk_template_parms
16614 || innermost_scope_kind () == sk_template_spec);
16616 /* Defer access checks until we know what is being declared. */
16617 push_deferring_access_checks (dk_deferred);
16619 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16621 cp_parser_decl_specifier_seq (parser,
16622 CP_PARSER_FLAGS_OPTIONAL,
16624 &declares_class_or_enum);
16626 *friend_p = cp_parser_friend_p (&decl_specifiers);
16628 /* There are no template typedefs. */
16629 if (decl_specifiers.specs[(int) ds_typedef])
16631 error ("template declaration of %qs", "typedef");
16632 decl = error_mark_node;
16635 /* Gather up the access checks that occurred the
16636 decl-specifier-seq. */
16637 stop_deferring_access_checks ();
16639 /* Check for the declaration of a template class. */
16640 if (declares_class_or_enum)
16642 if (cp_parser_declares_only_class_p (parser))
16644 decl = shadow_tag (&decl_specifiers);
16649 friend template <typename T> struct A<T>::B;
16652 A<T>::B will be represented by a TYPENAME_TYPE, and
16653 therefore not recognized by shadow_tag. */
16654 if (friend_p && *friend_p
16656 && decl_specifiers.type
16657 && TYPE_P (decl_specifiers.type))
16658 decl = decl_specifiers.type;
16660 if (decl && decl != error_mark_node)
16661 decl = TYPE_NAME (decl);
16663 decl = error_mark_node;
16665 /* Perform access checks for template parameters. */
16666 cp_parser_perform_template_parameter_access_checks (checks);
16669 /* If it's not a template class, try for a template function. If
16670 the next token is a `;', then this declaration does not declare
16671 anything. But, if there were errors in the decl-specifiers, then
16672 the error might well have come from an attempted class-specifier.
16673 In that case, there's no need to warn about a missing declarator. */
16675 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16676 || decl_specifiers.type != error_mark_node))
16678 decl = cp_parser_init_declarator (parser,
16681 /*function_definition_allowed_p=*/true,
16683 declares_class_or_enum,
16684 &function_definition_p);
16686 /* 7.1.1-1 [dcl.stc]
16688 A storage-class-specifier shall not be specified in an explicit
16689 specialization... */
16691 && explicit_specialization_p
16692 && decl_specifiers.storage_class != sc_none)
16694 error ("explicit template specialization cannot have a storage class");
16695 decl = error_mark_node;
16699 pop_deferring_access_checks ();
16701 /* Clear any current qualification; whatever comes next is the start
16702 of something new. */
16703 parser->scope = NULL_TREE;
16704 parser->qualifying_scope = NULL_TREE;
16705 parser->object_scope = NULL_TREE;
16706 /* Look for a trailing `;' after the declaration. */
16707 if (!function_definition_p
16708 && (decl == error_mark_node
16709 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16710 cp_parser_skip_to_end_of_block_or_statement (parser);
16715 /* Parse a cast-expression that is not the operand of a unary "&". */
16718 cp_parser_simple_cast_expression (cp_parser *parser)
16720 return cp_parser_cast_expression (parser, /*address_p=*/false,
16724 /* Parse a functional cast to TYPE. Returns an expression
16725 representing the cast. */
16728 cp_parser_functional_cast (cp_parser* parser, tree type)
16730 tree expression_list;
16734 = cp_parser_parenthesized_expression_list (parser, false,
16736 /*allow_expansion_p=*/true,
16737 /*non_constant_p=*/NULL);
16739 cast = build_functional_cast (type, expression_list);
16740 /* [expr.const]/1: In an integral constant expression "only type
16741 conversions to integral or enumeration type can be used". */
16742 if (TREE_CODE (type) == TYPE_DECL)
16743 type = TREE_TYPE (type);
16744 if (cast != error_mark_node
16745 && !cast_valid_in_integral_constant_expression_p (type)
16746 && (cp_parser_non_integral_constant_expression
16747 (parser, "a call to a constructor")))
16748 return error_mark_node;
16752 /* Save the tokens that make up the body of a member function defined
16753 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
16754 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
16755 specifiers applied to the declaration. Returns the FUNCTION_DECL
16756 for the member function. */
16759 cp_parser_save_member_function_body (cp_parser* parser,
16760 cp_decl_specifier_seq *decl_specifiers,
16761 cp_declarator *declarator,
16768 /* Create the function-declaration. */
16769 fn = start_method (decl_specifiers, declarator, attributes);
16770 /* If something went badly wrong, bail out now. */
16771 if (fn == error_mark_node)
16773 /* If there's a function-body, skip it. */
16774 if (cp_parser_token_starts_function_definition_p
16775 (cp_lexer_peek_token (parser->lexer)))
16776 cp_parser_skip_to_end_of_block_or_statement (parser);
16777 return error_mark_node;
16780 /* Remember it, if there default args to post process. */
16781 cp_parser_save_default_args (parser, fn);
16783 /* Save away the tokens that make up the body of the
16785 first = parser->lexer->next_token;
16786 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16787 /* Handle function try blocks. */
16788 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
16789 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
16790 last = parser->lexer->next_token;
16792 /* Save away the inline definition; we will process it when the
16793 class is complete. */
16794 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
16795 DECL_PENDING_INLINE_P (fn) = 1;
16797 /* We need to know that this was defined in the class, so that
16798 friend templates are handled correctly. */
16799 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
16801 /* We're done with the inline definition. */
16802 finish_method (fn);
16804 /* Add FN to the queue of functions to be parsed later. */
16805 TREE_VALUE (parser->unparsed_functions_queues)
16806 = tree_cons (NULL_TREE, fn,
16807 TREE_VALUE (parser->unparsed_functions_queues));
16812 /* Parse a template-argument-list, as well as the trailing ">" (but
16813 not the opening ">"). See cp_parser_template_argument_list for the
16817 cp_parser_enclosed_template_argument_list (cp_parser* parser)
16821 tree saved_qualifying_scope;
16822 tree saved_object_scope;
16823 bool saved_greater_than_is_operator_p;
16824 bool saved_skip_evaluation;
16828 When parsing a template-id, the first non-nested `>' is taken as
16829 the end of the template-argument-list rather than a greater-than
16831 saved_greater_than_is_operator_p
16832 = parser->greater_than_is_operator_p;
16833 parser->greater_than_is_operator_p = false;
16834 /* Parsing the argument list may modify SCOPE, so we save it
16836 saved_scope = parser->scope;
16837 saved_qualifying_scope = parser->qualifying_scope;
16838 saved_object_scope = parser->object_scope;
16839 /* We need to evaluate the template arguments, even though this
16840 template-id may be nested within a "sizeof". */
16841 saved_skip_evaluation = skip_evaluation;
16842 skip_evaluation = false;
16843 /* Parse the template-argument-list itself. */
16844 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
16845 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16846 arguments = NULL_TREE;
16848 arguments = cp_parser_template_argument_list (parser);
16849 /* Look for the `>' that ends the template-argument-list. If we find
16850 a '>>' instead, it's probably just a typo. */
16851 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
16853 if (cxx_dialect != cxx98)
16855 /* In C++0x, a `>>' in a template argument list or cast
16856 expression is considered to be two separate `>'
16857 tokens. So, change the current token to a `>', but don't
16858 consume it: it will be consumed later when the outer
16859 template argument list (or cast expression) is parsed.
16860 Note that this replacement of `>' for `>>' is necessary
16861 even if we are parsing tentatively: in the tentative
16862 case, after calling
16863 cp_parser_enclosed_template_argument_list we will always
16864 throw away all of the template arguments and the first
16865 closing `>', either because the template argument list
16866 was erroneous or because we are replacing those tokens
16867 with a CPP_TEMPLATE_ID token. The second `>' (which will
16868 not have been thrown away) is needed either to close an
16869 outer template argument list or to complete a new-style
16871 cp_token *token = cp_lexer_peek_token (parser->lexer);
16872 token->type = CPP_GREATER;
16874 else if (!saved_greater_than_is_operator_p)
16876 /* If we're in a nested template argument list, the '>>' has
16877 to be a typo for '> >'. We emit the error message, but we
16878 continue parsing and we push a '>' as next token, so that
16879 the argument list will be parsed correctly. Note that the
16880 global source location is still on the token before the
16881 '>>', so we need to say explicitly where we want it. */
16882 cp_token *token = cp_lexer_peek_token (parser->lexer);
16883 error ("%H%<>>%> should be %<> >%> "
16884 "within a nested template argument list",
16887 token->type = CPP_GREATER;
16891 /* If this is not a nested template argument list, the '>>'
16892 is a typo for '>'. Emit an error message and continue.
16893 Same deal about the token location, but here we can get it
16894 right by consuming the '>>' before issuing the diagnostic. */
16895 cp_lexer_consume_token (parser->lexer);
16896 error ("spurious %<>>%>, use %<>%> to terminate "
16897 "a template argument list");
16901 cp_parser_skip_to_end_of_template_parameter_list (parser);
16902 /* The `>' token might be a greater-than operator again now. */
16903 parser->greater_than_is_operator_p
16904 = saved_greater_than_is_operator_p;
16905 /* Restore the SAVED_SCOPE. */
16906 parser->scope = saved_scope;
16907 parser->qualifying_scope = saved_qualifying_scope;
16908 parser->object_scope = saved_object_scope;
16909 skip_evaluation = saved_skip_evaluation;
16914 /* MEMBER_FUNCTION is a member function, or a friend. If default
16915 arguments, or the body of the function have not yet been parsed,
16919 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
16921 /* If this member is a template, get the underlying
16923 if (DECL_FUNCTION_TEMPLATE_P (member_function))
16924 member_function = DECL_TEMPLATE_RESULT (member_function);
16926 /* There should not be any class definitions in progress at this
16927 point; the bodies of members are only parsed outside of all class
16929 gcc_assert (parser->num_classes_being_defined == 0);
16930 /* While we're parsing the member functions we might encounter more
16931 classes. We want to handle them right away, but we don't want
16932 them getting mixed up with functions that are currently in the
16934 parser->unparsed_functions_queues
16935 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
16937 /* Make sure that any template parameters are in scope. */
16938 maybe_begin_member_template_processing (member_function);
16940 /* If the body of the function has not yet been parsed, parse it
16942 if (DECL_PENDING_INLINE_P (member_function))
16944 tree function_scope;
16945 cp_token_cache *tokens;
16947 /* The function is no longer pending; we are processing it. */
16948 tokens = DECL_PENDING_INLINE_INFO (member_function);
16949 DECL_PENDING_INLINE_INFO (member_function) = NULL;
16950 DECL_PENDING_INLINE_P (member_function) = 0;
16952 /* If this is a local class, enter the scope of the containing
16954 function_scope = current_function_decl;
16955 if (function_scope)
16956 push_function_context_to (function_scope);
16959 /* Push the body of the function onto the lexer stack. */
16960 cp_parser_push_lexer_for_tokens (parser, tokens);
16962 /* Let the front end know that we going to be defining this
16964 start_preparsed_function (member_function, NULL_TREE,
16965 SF_PRE_PARSED | SF_INCLASS_INLINE);
16967 /* Don't do access checking if it is a templated function. */
16968 if (processing_template_decl)
16969 push_deferring_access_checks (dk_no_check);
16971 /* Now, parse the body of the function. */
16972 cp_parser_function_definition_after_declarator (parser,
16973 /*inline_p=*/true);
16975 if (processing_template_decl)
16976 pop_deferring_access_checks ();
16978 /* Leave the scope of the containing function. */
16979 if (function_scope)
16980 pop_function_context_from (function_scope);
16981 cp_parser_pop_lexer (parser);
16984 /* Remove any template parameters from the symbol table. */
16985 maybe_end_member_template_processing ();
16987 /* Restore the queue. */
16988 parser->unparsed_functions_queues
16989 = TREE_CHAIN (parser->unparsed_functions_queues);
16992 /* If DECL contains any default args, remember it on the unparsed
16993 functions queue. */
16996 cp_parser_save_default_args (cp_parser* parser, tree decl)
17000 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17002 probe = TREE_CHAIN (probe))
17003 if (TREE_PURPOSE (probe))
17005 TREE_PURPOSE (parser->unparsed_functions_queues)
17006 = tree_cons (current_class_type, decl,
17007 TREE_PURPOSE (parser->unparsed_functions_queues));
17012 /* FN is a FUNCTION_DECL which may contains a parameter with an
17013 unparsed DEFAULT_ARG. Parse the default args now. This function
17014 assumes that the current scope is the scope in which the default
17015 argument should be processed. */
17018 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17020 bool saved_local_variables_forbidden_p;
17023 /* While we're parsing the default args, we might (due to the
17024 statement expression extension) encounter more classes. We want
17025 to handle them right away, but we don't want them getting mixed
17026 up with default args that are currently in the queue. */
17027 parser->unparsed_functions_queues
17028 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17030 /* Local variable names (and the `this' keyword) may not appear
17031 in a default argument. */
17032 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17033 parser->local_variables_forbidden_p = true;
17035 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17037 parm = TREE_CHAIN (parm))
17039 cp_token_cache *tokens;
17040 tree default_arg = TREE_PURPOSE (parm);
17042 VEC(tree,gc) *insts;
17049 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17050 /* This can happen for a friend declaration for a function
17051 already declared with default arguments. */
17054 /* Push the saved tokens for the default argument onto the parser's
17056 tokens = DEFARG_TOKENS (default_arg);
17057 cp_parser_push_lexer_for_tokens (parser, tokens);
17059 /* Parse the assignment-expression. */
17060 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17062 if (!processing_template_decl)
17063 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17065 TREE_PURPOSE (parm) = parsed_arg;
17067 /* Update any instantiations we've already created. */
17068 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17069 VEC_iterate (tree, insts, ix, copy); ix++)
17070 TREE_PURPOSE (copy) = parsed_arg;
17072 /* If the token stream has not been completely used up, then
17073 there was extra junk after the end of the default
17075 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17076 cp_parser_error (parser, "expected %<,%>");
17078 /* Revert to the main lexer. */
17079 cp_parser_pop_lexer (parser);
17082 /* Make sure no default arg is missing. */
17083 check_default_args (fn);
17085 /* Restore the state of local_variables_forbidden_p. */
17086 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17088 /* Restore the queue. */
17089 parser->unparsed_functions_queues
17090 = TREE_CHAIN (parser->unparsed_functions_queues);
17093 /* Parse the operand of `sizeof' (or a similar operator). Returns
17094 either a TYPE or an expression, depending on the form of the
17095 input. The KEYWORD indicates which kind of expression we have
17099 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17101 static const char *format;
17102 tree expr = NULL_TREE;
17103 const char *saved_message;
17104 bool saved_integral_constant_expression_p;
17105 bool saved_non_integral_constant_expression_p;
17106 bool pack_expansion_p = false;
17108 /* Initialize FORMAT the first time we get here. */
17110 format = "types may not be defined in '%s' expressions";
17112 /* Types cannot be defined in a `sizeof' expression. Save away the
17114 saved_message = parser->type_definition_forbidden_message;
17115 /* And create the new one. */
17116 parser->type_definition_forbidden_message
17117 = XNEWVEC (const char, strlen (format)
17118 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17120 sprintf ((char *) parser->type_definition_forbidden_message,
17121 format, IDENTIFIER_POINTER (ridpointers[keyword]));
17123 /* The restrictions on constant-expressions do not apply inside
17124 sizeof expressions. */
17125 saved_integral_constant_expression_p
17126 = parser->integral_constant_expression_p;
17127 saved_non_integral_constant_expression_p
17128 = parser->non_integral_constant_expression_p;
17129 parser->integral_constant_expression_p = false;
17131 /* If it's a `...', then we are computing the length of a parameter
17133 if (keyword == RID_SIZEOF
17134 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17136 /* Consume the `...'. */
17137 cp_lexer_consume_token (parser->lexer);
17138 maybe_warn_variadic_templates ();
17140 /* Note that this is an expansion. */
17141 pack_expansion_p = true;
17144 /* Do not actually evaluate the expression. */
17146 /* If it's a `(', then we might be looking at the type-id
17148 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17151 bool saved_in_type_id_in_expr_p;
17153 /* We can't be sure yet whether we're looking at a type-id or an
17155 cp_parser_parse_tentatively (parser);
17156 /* Consume the `('. */
17157 cp_lexer_consume_token (parser->lexer);
17158 /* Parse the type-id. */
17159 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17160 parser->in_type_id_in_expr_p = true;
17161 type = cp_parser_type_id (parser);
17162 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17163 /* Now, look for the trailing `)'. */
17164 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17165 /* If all went well, then we're done. */
17166 if (cp_parser_parse_definitely (parser))
17168 cp_decl_specifier_seq decl_specs;
17170 /* Build a trivial decl-specifier-seq. */
17171 clear_decl_specs (&decl_specs);
17172 decl_specs.type = type;
17174 /* Call grokdeclarator to figure out what type this is. */
17175 expr = grokdeclarator (NULL,
17179 /*attrlist=*/NULL);
17183 /* If the type-id production did not work out, then we must be
17184 looking at the unary-expression production. */
17186 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17189 if (pack_expansion_p)
17190 /* Build a pack expansion. */
17191 expr = make_pack_expansion (expr);
17193 /* Go back to evaluating expressions. */
17196 /* Free the message we created. */
17197 free ((char *) parser->type_definition_forbidden_message);
17198 /* And restore the old one. */
17199 parser->type_definition_forbidden_message = saved_message;
17200 parser->integral_constant_expression_p
17201 = saved_integral_constant_expression_p;
17202 parser->non_integral_constant_expression_p
17203 = saved_non_integral_constant_expression_p;
17208 /* If the current declaration has no declarator, return true. */
17211 cp_parser_declares_only_class_p (cp_parser *parser)
17213 /* If the next token is a `;' or a `,' then there is no
17215 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17216 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17219 /* Update the DECL_SPECS to reflect the storage class indicated by
17223 cp_parser_set_storage_class (cp_parser *parser,
17224 cp_decl_specifier_seq *decl_specs,
17227 cp_storage_class storage_class;
17229 if (parser->in_unbraced_linkage_specification_p)
17231 error ("invalid use of %qD in linkage specification",
17232 ridpointers[keyword]);
17235 else if (decl_specs->storage_class != sc_none)
17237 decl_specs->conflicting_specifiers_p = true;
17241 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17242 && decl_specs->specs[(int) ds_thread])
17244 error ("%<__thread%> before %qD", ridpointers[keyword]);
17245 decl_specs->specs[(int) ds_thread] = 0;
17251 storage_class = sc_auto;
17254 storage_class = sc_register;
17257 storage_class = sc_static;
17260 storage_class = sc_extern;
17263 storage_class = sc_mutable;
17266 gcc_unreachable ();
17268 decl_specs->storage_class = storage_class;
17270 /* A storage class specifier cannot be applied alongside a typedef
17271 specifier. If there is a typedef specifier present then set
17272 conflicting_specifiers_p which will trigger an error later
17273 on in grokdeclarator. */
17274 if (decl_specs->specs[(int)ds_typedef])
17275 decl_specs->conflicting_specifiers_p = true;
17278 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17279 is true, the type is a user-defined type; otherwise it is a
17280 built-in type specified by a keyword. */
17283 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17285 bool user_defined_p)
17287 decl_specs->any_specifiers_p = true;
17289 /* If the user tries to redeclare bool or wchar_t (with, for
17290 example, in "typedef int wchar_t;") we remember that this is what
17291 happened. In system headers, we ignore these declarations so
17292 that G++ can work with system headers that are not C++-safe. */
17293 if (decl_specs->specs[(int) ds_typedef]
17295 && (type_spec == boolean_type_node
17296 || type_spec == wchar_type_node)
17297 && (decl_specs->type
17298 || decl_specs->specs[(int) ds_long]
17299 || decl_specs->specs[(int) ds_short]
17300 || decl_specs->specs[(int) ds_unsigned]
17301 || decl_specs->specs[(int) ds_signed]))
17303 decl_specs->redefined_builtin_type = type_spec;
17304 if (!decl_specs->type)
17306 decl_specs->type = type_spec;
17307 decl_specs->user_defined_type_p = false;
17310 else if (decl_specs->type)
17311 decl_specs->multiple_types_p = true;
17314 decl_specs->type = type_spec;
17315 decl_specs->user_defined_type_p = user_defined_p;
17316 decl_specs->redefined_builtin_type = NULL_TREE;
17320 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17321 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17324 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17326 return decl_specifiers->specs[(int) ds_friend] != 0;
17329 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17330 issue an error message indicating that TOKEN_DESC was expected.
17332 Returns the token consumed, if the token had the appropriate type.
17333 Otherwise, returns NULL. */
17336 cp_parser_require (cp_parser* parser,
17337 enum cpp_ttype type,
17338 const char* token_desc)
17340 if (cp_lexer_next_token_is (parser->lexer, type))
17341 return cp_lexer_consume_token (parser->lexer);
17344 /* Output the MESSAGE -- unless we're parsing tentatively. */
17345 if (!cp_parser_simulate_error (parser))
17347 char *message = concat ("expected ", token_desc, NULL);
17348 cp_parser_error (parser, message);
17355 /* An error message is produced if the next token is not '>'.
17356 All further tokens are skipped until the desired token is
17357 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17360 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17362 /* Current level of '< ... >'. */
17363 unsigned level = 0;
17364 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17365 unsigned nesting_depth = 0;
17367 /* Are we ready, yet? If not, issue error message. */
17368 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17371 /* Skip tokens until the desired token is found. */
17374 /* Peek at the next token. */
17375 switch (cp_lexer_peek_token (parser->lexer)->type)
17378 if (!nesting_depth)
17383 if (cxx_dialect == cxx98)
17384 /* C++0x views the `>>' operator as two `>' tokens, but
17387 else if (!nesting_depth && level-- == 0)
17389 /* We've hit a `>>' where the first `>' closes the
17390 template argument list, and the second `>' is
17391 spurious. Just consume the `>>' and stop; we've
17392 already produced at least one error. */
17393 cp_lexer_consume_token (parser->lexer);
17396 /* Fall through for C++0x, so we handle the second `>' in
17400 if (!nesting_depth && level-- == 0)
17402 /* We've reached the token we want, consume it and stop. */
17403 cp_lexer_consume_token (parser->lexer);
17408 case CPP_OPEN_PAREN:
17409 case CPP_OPEN_SQUARE:
17413 case CPP_CLOSE_PAREN:
17414 case CPP_CLOSE_SQUARE:
17415 if (nesting_depth-- == 0)
17420 case CPP_PRAGMA_EOL:
17421 case CPP_SEMICOLON:
17422 case CPP_OPEN_BRACE:
17423 case CPP_CLOSE_BRACE:
17424 /* The '>' was probably forgotten, don't look further. */
17431 /* Consume this token. */
17432 cp_lexer_consume_token (parser->lexer);
17436 /* If the next token is the indicated keyword, consume it. Otherwise,
17437 issue an error message indicating that TOKEN_DESC was expected.
17439 Returns the token consumed, if the token had the appropriate type.
17440 Otherwise, returns NULL. */
17443 cp_parser_require_keyword (cp_parser* parser,
17445 const char* token_desc)
17447 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17449 if (token && token->keyword != keyword)
17451 dyn_string_t error_msg;
17453 /* Format the error message. */
17454 error_msg = dyn_string_new (0);
17455 dyn_string_append_cstr (error_msg, "expected ");
17456 dyn_string_append_cstr (error_msg, token_desc);
17457 cp_parser_error (parser, error_msg->s);
17458 dyn_string_delete (error_msg);
17465 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17466 function-definition. */
17469 cp_parser_token_starts_function_definition_p (cp_token* token)
17471 return (/* An ordinary function-body begins with an `{'. */
17472 token->type == CPP_OPEN_BRACE
17473 /* A ctor-initializer begins with a `:'. */
17474 || token->type == CPP_COLON
17475 /* A function-try-block begins with `try'. */
17476 || token->keyword == RID_TRY
17477 /* The named return value extension begins with `return'. */
17478 || token->keyword == RID_RETURN);
17481 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17485 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17489 token = cp_lexer_peek_token (parser->lexer);
17490 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17493 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17494 C++0x) ending a template-argument. */
17497 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17501 token = cp_lexer_peek_token (parser->lexer);
17502 return (token->type == CPP_COMMA
17503 || token->type == CPP_GREATER
17504 || token->type == CPP_ELLIPSIS
17505 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17508 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17509 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17512 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17517 token = cp_lexer_peek_nth_token (parser->lexer, n);
17518 if (token->type == CPP_LESS)
17520 /* Check for the sequence `<::' in the original code. It would be lexed as
17521 `[:', where `[' is a digraph, and there is no whitespace before
17523 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17526 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17527 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17533 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17534 or none_type otherwise. */
17536 static enum tag_types
17537 cp_parser_token_is_class_key (cp_token* token)
17539 switch (token->keyword)
17544 return record_type;
17553 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17556 cp_parser_check_class_key (enum tag_types class_key, tree type)
17558 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17559 pedwarn ("%qs tag used in naming %q#T",
17560 class_key == union_type ? "union"
17561 : class_key == record_type ? "struct" : "class",
17565 /* Issue an error message if DECL is redeclared with different
17566 access than its original declaration [class.access.spec/3].
17567 This applies to nested classes and nested class templates.
17571 cp_parser_check_access_in_redeclaration (tree decl)
17573 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17576 if ((TREE_PRIVATE (decl)
17577 != (current_access_specifier == access_private_node))
17578 || (TREE_PROTECTED (decl)
17579 != (current_access_specifier == access_protected_node)))
17580 error ("%qD redeclared with different access", decl);
17583 /* Look for the `template' keyword, as a syntactic disambiguator.
17584 Return TRUE iff it is present, in which case it will be
17588 cp_parser_optional_template_keyword (cp_parser *parser)
17590 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17592 /* The `template' keyword can only be used within templates;
17593 outside templates the parser can always figure out what is a
17594 template and what is not. */
17595 if (!processing_template_decl)
17597 error ("%<template%> (as a disambiguator) is only allowed "
17598 "within templates");
17599 /* If this part of the token stream is rescanned, the same
17600 error message would be generated. So, we purge the token
17601 from the stream. */
17602 cp_lexer_purge_token (parser->lexer);
17607 /* Consume the `template' keyword. */
17608 cp_lexer_consume_token (parser->lexer);
17616 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17617 set PARSER->SCOPE, and perform other related actions. */
17620 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17623 struct tree_check *check_value;
17624 deferred_access_check *chk;
17625 VEC (deferred_access_check,gc) *checks;
17627 /* Get the stored value. */
17628 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17629 /* Perform any access checks that were deferred. */
17630 checks = check_value->checks;
17634 VEC_iterate (deferred_access_check, checks, i, chk) ;
17637 perform_or_defer_access_check (chk->binfo,
17642 /* Set the scope from the stored value. */
17643 parser->scope = check_value->value;
17644 parser->qualifying_scope = check_value->qualifying_scope;
17645 parser->object_scope = NULL_TREE;
17648 /* Consume tokens up through a non-nested END token. */
17651 cp_parser_cache_group (cp_parser *parser,
17652 enum cpp_ttype end,
17659 /* Abort a parenthesized expression if we encounter a brace. */
17660 if ((end == CPP_CLOSE_PAREN || depth == 0)
17661 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17663 /* If we've reached the end of the file, stop. */
17664 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17665 || (end != CPP_PRAGMA_EOL
17666 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17668 /* Consume the next token. */
17669 token = cp_lexer_consume_token (parser->lexer);
17670 /* See if it starts a new group. */
17671 if (token->type == CPP_OPEN_BRACE)
17673 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17677 else if (token->type == CPP_OPEN_PAREN)
17678 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17679 else if (token->type == CPP_PRAGMA)
17680 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17681 else if (token->type == end)
17686 /* Begin parsing tentatively. We always save tokens while parsing
17687 tentatively so that if the tentative parsing fails we can restore the
17691 cp_parser_parse_tentatively (cp_parser* parser)
17693 /* Enter a new parsing context. */
17694 parser->context = cp_parser_context_new (parser->context);
17695 /* Begin saving tokens. */
17696 cp_lexer_save_tokens (parser->lexer);
17697 /* In order to avoid repetitive access control error messages,
17698 access checks are queued up until we are no longer parsing
17700 push_deferring_access_checks (dk_deferred);
17703 /* Commit to the currently active tentative parse. */
17706 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17708 cp_parser_context *context;
17711 /* Mark all of the levels as committed. */
17712 lexer = parser->lexer;
17713 for (context = parser->context; context->next; context = context->next)
17715 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17717 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17718 while (!cp_lexer_saving_tokens (lexer))
17719 lexer = lexer->next;
17720 cp_lexer_commit_tokens (lexer);
17724 /* Abort the currently active tentative parse. All consumed tokens
17725 will be rolled back, and no diagnostics will be issued. */
17728 cp_parser_abort_tentative_parse (cp_parser* parser)
17730 cp_parser_simulate_error (parser);
17731 /* Now, pretend that we want to see if the construct was
17732 successfully parsed. */
17733 cp_parser_parse_definitely (parser);
17736 /* Stop parsing tentatively. If a parse error has occurred, restore the
17737 token stream. Otherwise, commit to the tokens we have consumed.
17738 Returns true if no error occurred; false otherwise. */
17741 cp_parser_parse_definitely (cp_parser* parser)
17743 bool error_occurred;
17744 cp_parser_context *context;
17746 /* Remember whether or not an error occurred, since we are about to
17747 destroy that information. */
17748 error_occurred = cp_parser_error_occurred (parser);
17749 /* Remove the topmost context from the stack. */
17750 context = parser->context;
17751 parser->context = context->next;
17752 /* If no parse errors occurred, commit to the tentative parse. */
17753 if (!error_occurred)
17755 /* Commit to the tokens read tentatively, unless that was
17757 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
17758 cp_lexer_commit_tokens (parser->lexer);
17760 pop_to_parent_deferring_access_checks ();
17762 /* Otherwise, if errors occurred, roll back our state so that things
17763 are just as they were before we began the tentative parse. */
17766 cp_lexer_rollback_tokens (parser->lexer);
17767 pop_deferring_access_checks ();
17769 /* Add the context to the front of the free list. */
17770 context->next = cp_parser_context_free_list;
17771 cp_parser_context_free_list = context;
17773 return !error_occurred;
17776 /* Returns true if we are parsing tentatively and are not committed to
17777 this tentative parse. */
17780 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
17782 return (cp_parser_parsing_tentatively (parser)
17783 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
17786 /* Returns nonzero iff an error has occurred during the most recent
17787 tentative parse. */
17790 cp_parser_error_occurred (cp_parser* parser)
17792 return (cp_parser_parsing_tentatively (parser)
17793 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
17796 /* Returns nonzero if GNU extensions are allowed. */
17799 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
17801 return parser->allow_gnu_extensions_p;
17804 /* Objective-C++ Productions */
17807 /* Parse an Objective-C expression, which feeds into a primary-expression
17811 objc-message-expression
17812 objc-string-literal
17813 objc-encode-expression
17814 objc-protocol-expression
17815 objc-selector-expression
17817 Returns a tree representation of the expression. */
17820 cp_parser_objc_expression (cp_parser* parser)
17822 /* Try to figure out what kind of declaration is present. */
17823 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17827 case CPP_OPEN_SQUARE:
17828 return cp_parser_objc_message_expression (parser);
17830 case CPP_OBJC_STRING:
17831 kwd = cp_lexer_consume_token (parser->lexer);
17832 return objc_build_string_object (kwd->u.value);
17835 switch (kwd->keyword)
17837 case RID_AT_ENCODE:
17838 return cp_parser_objc_encode_expression (parser);
17840 case RID_AT_PROTOCOL:
17841 return cp_parser_objc_protocol_expression (parser);
17843 case RID_AT_SELECTOR:
17844 return cp_parser_objc_selector_expression (parser);
17850 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
17851 cp_parser_skip_to_end_of_block_or_statement (parser);
17854 return error_mark_node;
17857 /* Parse an Objective-C message expression.
17859 objc-message-expression:
17860 [ objc-message-receiver objc-message-args ]
17862 Returns a representation of an Objective-C message. */
17865 cp_parser_objc_message_expression (cp_parser* parser)
17867 tree receiver, messageargs;
17869 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
17870 receiver = cp_parser_objc_message_receiver (parser);
17871 messageargs = cp_parser_objc_message_args (parser);
17872 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
17874 return objc_build_message_expr (build_tree_list (receiver, messageargs));
17877 /* Parse an objc-message-receiver.
17879 objc-message-receiver:
17881 simple-type-specifier
17883 Returns a representation of the type or expression. */
17886 cp_parser_objc_message_receiver (cp_parser* parser)
17890 /* An Objective-C message receiver may be either (1) a type
17891 or (2) an expression. */
17892 cp_parser_parse_tentatively (parser);
17893 rcv = cp_parser_expression (parser, false);
17895 if (cp_parser_parse_definitely (parser))
17898 rcv = cp_parser_simple_type_specifier (parser,
17899 /*decl_specs=*/NULL,
17900 CP_PARSER_FLAGS_NONE);
17902 return objc_get_class_reference (rcv);
17905 /* Parse the arguments and selectors comprising an Objective-C message.
17910 objc-selector-args , objc-comma-args
17912 objc-selector-args:
17913 objc-selector [opt] : assignment-expression
17914 objc-selector-args objc-selector [opt] : assignment-expression
17917 assignment-expression
17918 objc-comma-args , assignment-expression
17920 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
17921 selector arguments and TREE_VALUE containing a list of comma
17925 cp_parser_objc_message_args (cp_parser* parser)
17927 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
17928 bool maybe_unary_selector_p = true;
17929 cp_token *token = cp_lexer_peek_token (parser->lexer);
17931 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
17933 tree selector = NULL_TREE, arg;
17935 if (token->type != CPP_COLON)
17936 selector = cp_parser_objc_selector (parser);
17938 /* Detect if we have a unary selector. */
17939 if (maybe_unary_selector_p
17940 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
17941 return build_tree_list (selector, NULL_TREE);
17943 maybe_unary_selector_p = false;
17944 cp_parser_require (parser, CPP_COLON, "`:'");
17945 arg = cp_parser_assignment_expression (parser, false);
17948 = chainon (sel_args,
17949 build_tree_list (selector, arg));
17951 token = cp_lexer_peek_token (parser->lexer);
17954 /* Handle non-selector arguments, if any. */
17955 while (token->type == CPP_COMMA)
17959 cp_lexer_consume_token (parser->lexer);
17960 arg = cp_parser_assignment_expression (parser, false);
17963 = chainon (addl_args,
17964 build_tree_list (NULL_TREE, arg));
17966 token = cp_lexer_peek_token (parser->lexer);
17969 return build_tree_list (sel_args, addl_args);
17972 /* Parse an Objective-C encode expression.
17974 objc-encode-expression:
17975 @encode objc-typename
17977 Returns an encoded representation of the type argument. */
17980 cp_parser_objc_encode_expression (cp_parser* parser)
17984 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
17985 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17986 type = complete_type (cp_parser_type_id (parser));
17987 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17991 error ("%<@encode%> must specify a type as an argument");
17992 return error_mark_node;
17995 return objc_build_encode_expr (type);
17998 /* Parse an Objective-C @defs expression. */
18001 cp_parser_objc_defs_expression (cp_parser *parser)
18005 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18006 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18007 name = cp_parser_identifier (parser);
18008 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18010 return objc_get_class_ivars (name);
18013 /* Parse an Objective-C protocol expression.
18015 objc-protocol-expression:
18016 @protocol ( identifier )
18018 Returns a representation of the protocol expression. */
18021 cp_parser_objc_protocol_expression (cp_parser* parser)
18025 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18026 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18027 proto = cp_parser_identifier (parser);
18028 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18030 return objc_build_protocol_expr (proto);
18033 /* Parse an Objective-C selector expression.
18035 objc-selector-expression:
18036 @selector ( objc-method-signature )
18038 objc-method-signature:
18044 objc-selector-seq objc-selector :
18046 Returns a representation of the method selector. */
18049 cp_parser_objc_selector_expression (cp_parser* parser)
18051 tree sel_seq = NULL_TREE;
18052 bool maybe_unary_selector_p = true;
18055 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18056 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18057 token = cp_lexer_peek_token (parser->lexer);
18059 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18060 || token->type == CPP_SCOPE)
18062 tree selector = NULL_TREE;
18064 if (token->type != CPP_COLON
18065 || token->type == CPP_SCOPE)
18066 selector = cp_parser_objc_selector (parser);
18068 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18069 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18071 /* Detect if we have a unary selector. */
18072 if (maybe_unary_selector_p)
18074 sel_seq = selector;
18075 goto finish_selector;
18079 cp_parser_error (parser, "expected %<:%>");
18082 maybe_unary_selector_p = false;
18083 token = cp_lexer_consume_token (parser->lexer);
18085 if (token->type == CPP_SCOPE)
18088 = chainon (sel_seq,
18089 build_tree_list (selector, NULL_TREE));
18091 = chainon (sel_seq,
18092 build_tree_list (NULL_TREE, NULL_TREE));
18096 = chainon (sel_seq,
18097 build_tree_list (selector, NULL_TREE));
18099 token = cp_lexer_peek_token (parser->lexer);
18103 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18105 return objc_build_selector_expr (sel_seq);
18108 /* Parse a list of identifiers.
18110 objc-identifier-list:
18112 objc-identifier-list , identifier
18114 Returns a TREE_LIST of identifier nodes. */
18117 cp_parser_objc_identifier_list (cp_parser* parser)
18119 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18120 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18122 while (sep->type == CPP_COMMA)
18124 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18125 list = chainon (list,
18126 build_tree_list (NULL_TREE,
18127 cp_parser_identifier (parser)));
18128 sep = cp_lexer_peek_token (parser->lexer);
18134 /* Parse an Objective-C alias declaration.
18136 objc-alias-declaration:
18137 @compatibility_alias identifier identifier ;
18139 This function registers the alias mapping with the Objective-C front end.
18140 It returns nothing. */
18143 cp_parser_objc_alias_declaration (cp_parser* parser)
18147 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18148 alias = cp_parser_identifier (parser);
18149 orig = cp_parser_identifier (parser);
18150 objc_declare_alias (alias, orig);
18151 cp_parser_consume_semicolon_at_end_of_statement (parser);
18154 /* Parse an Objective-C class forward-declaration.
18156 objc-class-declaration:
18157 @class objc-identifier-list ;
18159 The function registers the forward declarations with the Objective-C
18160 front end. It returns nothing. */
18163 cp_parser_objc_class_declaration (cp_parser* parser)
18165 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18166 objc_declare_class (cp_parser_objc_identifier_list (parser));
18167 cp_parser_consume_semicolon_at_end_of_statement (parser);
18170 /* Parse a list of Objective-C protocol references.
18172 objc-protocol-refs-opt:
18173 objc-protocol-refs [opt]
18175 objc-protocol-refs:
18176 < objc-identifier-list >
18178 Returns a TREE_LIST of identifiers, if any. */
18181 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18183 tree protorefs = NULL_TREE;
18185 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18187 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18188 protorefs = cp_parser_objc_identifier_list (parser);
18189 cp_parser_require (parser, CPP_GREATER, "`>'");
18195 /* Parse a Objective-C visibility specification. */
18198 cp_parser_objc_visibility_spec (cp_parser* parser)
18200 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18202 switch (vis->keyword)
18204 case RID_AT_PRIVATE:
18205 objc_set_visibility (2);
18207 case RID_AT_PROTECTED:
18208 objc_set_visibility (0);
18210 case RID_AT_PUBLIC:
18211 objc_set_visibility (1);
18217 /* Eat '@private'/'@protected'/'@public'. */
18218 cp_lexer_consume_token (parser->lexer);
18221 /* Parse an Objective-C method type. */
18224 cp_parser_objc_method_type (cp_parser* parser)
18226 objc_set_method_type
18227 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18232 /* Parse an Objective-C protocol qualifier. */
18235 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18237 tree quals = NULL_TREE, node;
18238 cp_token *token = cp_lexer_peek_token (parser->lexer);
18240 node = token->u.value;
18242 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18243 && (node == ridpointers [(int) RID_IN]
18244 || node == ridpointers [(int) RID_OUT]
18245 || node == ridpointers [(int) RID_INOUT]
18246 || node == ridpointers [(int) RID_BYCOPY]
18247 || node == ridpointers [(int) RID_BYREF]
18248 || node == ridpointers [(int) RID_ONEWAY]))
18250 quals = tree_cons (NULL_TREE, node, quals);
18251 cp_lexer_consume_token (parser->lexer);
18252 token = cp_lexer_peek_token (parser->lexer);
18253 node = token->u.value;
18259 /* Parse an Objective-C typename. */
18262 cp_parser_objc_typename (cp_parser* parser)
18264 tree typename = NULL_TREE;
18266 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18268 tree proto_quals, cp_type = NULL_TREE;
18270 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18271 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18273 /* An ObjC type name may consist of just protocol qualifiers, in which
18274 case the type shall default to 'id'. */
18275 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18276 cp_type = cp_parser_type_id (parser);
18278 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18279 typename = build_tree_list (proto_quals, cp_type);
18285 /* Check to see if TYPE refers to an Objective-C selector name. */
18288 cp_parser_objc_selector_p (enum cpp_ttype type)
18290 return (type == CPP_NAME || type == CPP_KEYWORD
18291 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18292 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18293 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18294 || type == CPP_XOR || type == CPP_XOR_EQ);
18297 /* Parse an Objective-C selector. */
18300 cp_parser_objc_selector (cp_parser* parser)
18302 cp_token *token = cp_lexer_consume_token (parser->lexer);
18304 if (!cp_parser_objc_selector_p (token->type))
18306 error ("invalid Objective-C++ selector name");
18307 return error_mark_node;
18310 /* C++ operator names are allowed to appear in ObjC selectors. */
18311 switch (token->type)
18313 case CPP_AND_AND: return get_identifier ("and");
18314 case CPP_AND_EQ: return get_identifier ("and_eq");
18315 case CPP_AND: return get_identifier ("bitand");
18316 case CPP_OR: return get_identifier ("bitor");
18317 case CPP_COMPL: return get_identifier ("compl");
18318 case CPP_NOT: return get_identifier ("not");
18319 case CPP_NOT_EQ: return get_identifier ("not_eq");
18320 case CPP_OR_OR: return get_identifier ("or");
18321 case CPP_OR_EQ: return get_identifier ("or_eq");
18322 case CPP_XOR: return get_identifier ("xor");
18323 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18324 default: return token->u.value;
18328 /* Parse an Objective-C params list. */
18331 cp_parser_objc_method_keyword_params (cp_parser* parser)
18333 tree params = NULL_TREE;
18334 bool maybe_unary_selector_p = true;
18335 cp_token *token = cp_lexer_peek_token (parser->lexer);
18337 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18339 tree selector = NULL_TREE, typename, identifier;
18341 if (token->type != CPP_COLON)
18342 selector = cp_parser_objc_selector (parser);
18344 /* Detect if we have a unary selector. */
18345 if (maybe_unary_selector_p
18346 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18349 maybe_unary_selector_p = false;
18350 cp_parser_require (parser, CPP_COLON, "`:'");
18351 typename = cp_parser_objc_typename (parser);
18352 identifier = cp_parser_identifier (parser);
18356 objc_build_keyword_decl (selector,
18360 token = cp_lexer_peek_token (parser->lexer);
18366 /* Parse the non-keyword Objective-C params. */
18369 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18371 tree params = make_node (TREE_LIST);
18372 cp_token *token = cp_lexer_peek_token (parser->lexer);
18373 *ellipsisp = false; /* Initially, assume no ellipsis. */
18375 while (token->type == CPP_COMMA)
18377 cp_parameter_declarator *parmdecl;
18380 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18381 token = cp_lexer_peek_token (parser->lexer);
18383 if (token->type == CPP_ELLIPSIS)
18385 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18390 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18391 parm = grokdeclarator (parmdecl->declarator,
18392 &parmdecl->decl_specifiers,
18393 PARM, /*initialized=*/0,
18394 /*attrlist=*/NULL);
18396 chainon (params, build_tree_list (NULL_TREE, parm));
18397 token = cp_lexer_peek_token (parser->lexer);
18403 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18406 cp_parser_objc_interstitial_code (cp_parser* parser)
18408 cp_token *token = cp_lexer_peek_token (parser->lexer);
18410 /* If the next token is `extern' and the following token is a string
18411 literal, then we have a linkage specification. */
18412 if (token->keyword == RID_EXTERN
18413 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18414 cp_parser_linkage_specification (parser);
18415 /* Handle #pragma, if any. */
18416 else if (token->type == CPP_PRAGMA)
18417 cp_parser_pragma (parser, pragma_external);
18418 /* Allow stray semicolons. */
18419 else if (token->type == CPP_SEMICOLON)
18420 cp_lexer_consume_token (parser->lexer);
18421 /* Finally, try to parse a block-declaration, or a function-definition. */
18423 cp_parser_block_declaration (parser, /*statement_p=*/false);
18426 /* Parse a method signature. */
18429 cp_parser_objc_method_signature (cp_parser* parser)
18431 tree rettype, kwdparms, optparms;
18432 bool ellipsis = false;
18434 cp_parser_objc_method_type (parser);
18435 rettype = cp_parser_objc_typename (parser);
18436 kwdparms = cp_parser_objc_method_keyword_params (parser);
18437 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18439 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18442 /* Pars an Objective-C method prototype list. */
18445 cp_parser_objc_method_prototype_list (cp_parser* parser)
18447 cp_token *token = cp_lexer_peek_token (parser->lexer);
18449 while (token->keyword != RID_AT_END)
18451 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18453 objc_add_method_declaration
18454 (cp_parser_objc_method_signature (parser));
18455 cp_parser_consume_semicolon_at_end_of_statement (parser);
18458 /* Allow for interspersed non-ObjC++ code. */
18459 cp_parser_objc_interstitial_code (parser);
18461 token = cp_lexer_peek_token (parser->lexer);
18464 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18465 objc_finish_interface ();
18468 /* Parse an Objective-C method definition list. */
18471 cp_parser_objc_method_definition_list (cp_parser* parser)
18473 cp_token *token = cp_lexer_peek_token (parser->lexer);
18475 while (token->keyword != RID_AT_END)
18479 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18481 push_deferring_access_checks (dk_deferred);
18482 objc_start_method_definition
18483 (cp_parser_objc_method_signature (parser));
18485 /* For historical reasons, we accept an optional semicolon. */
18486 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18487 cp_lexer_consume_token (parser->lexer);
18489 perform_deferred_access_checks ();
18490 stop_deferring_access_checks ();
18491 meth = cp_parser_function_definition_after_declarator (parser,
18493 pop_deferring_access_checks ();
18494 objc_finish_method_definition (meth);
18497 /* Allow for interspersed non-ObjC++ code. */
18498 cp_parser_objc_interstitial_code (parser);
18500 token = cp_lexer_peek_token (parser->lexer);
18503 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18504 objc_finish_implementation ();
18507 /* Parse Objective-C ivars. */
18510 cp_parser_objc_class_ivars (cp_parser* parser)
18512 cp_token *token = cp_lexer_peek_token (parser->lexer);
18514 if (token->type != CPP_OPEN_BRACE)
18515 return; /* No ivars specified. */
18517 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18518 token = cp_lexer_peek_token (parser->lexer);
18520 while (token->type != CPP_CLOSE_BRACE)
18522 cp_decl_specifier_seq declspecs;
18523 int decl_class_or_enum_p;
18524 tree prefix_attributes;
18526 cp_parser_objc_visibility_spec (parser);
18528 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18531 cp_parser_decl_specifier_seq (parser,
18532 CP_PARSER_FLAGS_OPTIONAL,
18534 &decl_class_or_enum_p);
18535 prefix_attributes = declspecs.attributes;
18536 declspecs.attributes = NULL_TREE;
18538 /* Keep going until we hit the `;' at the end of the
18540 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18542 tree width = NULL_TREE, attributes, first_attribute, decl;
18543 cp_declarator *declarator = NULL;
18544 int ctor_dtor_or_conv_p;
18546 /* Check for a (possibly unnamed) bitfield declaration. */
18547 token = cp_lexer_peek_token (parser->lexer);
18548 if (token->type == CPP_COLON)
18551 if (token->type == CPP_NAME
18552 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18555 /* Get the name of the bitfield. */
18556 declarator = make_id_declarator (NULL_TREE,
18557 cp_parser_identifier (parser),
18561 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18562 /* Get the width of the bitfield. */
18564 = cp_parser_constant_expression (parser,
18565 /*allow_non_constant=*/false,
18570 /* Parse the declarator. */
18572 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18573 &ctor_dtor_or_conv_p,
18574 /*parenthesized_p=*/NULL,
18575 /*member_p=*/false);
18578 /* Look for attributes that apply to the ivar. */
18579 attributes = cp_parser_attributes_opt (parser);
18580 /* Remember which attributes are prefix attributes and
18582 first_attribute = attributes;
18583 /* Combine the attributes. */
18584 attributes = chainon (prefix_attributes, attributes);
18588 /* Create the bitfield declaration. */
18589 decl = grokbitfield (declarator, &declspecs, width);
18590 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18593 decl = grokfield (declarator, &declspecs,
18594 NULL_TREE, /*init_const_expr_p=*/false,
18595 NULL_TREE, attributes);
18597 /* Add the instance variable. */
18598 objc_add_instance_variable (decl);
18600 /* Reset PREFIX_ATTRIBUTES. */
18601 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18602 attributes = TREE_CHAIN (attributes);
18604 TREE_CHAIN (attributes) = NULL_TREE;
18606 token = cp_lexer_peek_token (parser->lexer);
18608 if (token->type == CPP_COMMA)
18610 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18616 cp_parser_consume_semicolon_at_end_of_statement (parser);
18617 token = cp_lexer_peek_token (parser->lexer);
18620 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18621 /* For historical reasons, we accept an optional semicolon. */
18622 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18623 cp_lexer_consume_token (parser->lexer);
18626 /* Parse an Objective-C protocol declaration. */
18629 cp_parser_objc_protocol_declaration (cp_parser* parser)
18631 tree proto, protorefs;
18634 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18635 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18637 error ("identifier expected after %<@protocol%>");
18641 /* See if we have a forward declaration or a definition. */
18642 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18644 /* Try a forward declaration first. */
18645 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18647 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18649 cp_parser_consume_semicolon_at_end_of_statement (parser);
18652 /* Ok, we got a full-fledged definition (or at least should). */
18655 proto = cp_parser_identifier (parser);
18656 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18657 objc_start_protocol (proto, protorefs);
18658 cp_parser_objc_method_prototype_list (parser);
18662 /* Parse an Objective-C superclass or category. */
18665 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18668 cp_token *next = cp_lexer_peek_token (parser->lexer);
18670 *super = *categ = NULL_TREE;
18671 if (next->type == CPP_COLON)
18673 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18674 *super = cp_parser_identifier (parser);
18676 else if (next->type == CPP_OPEN_PAREN)
18678 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18679 *categ = cp_parser_identifier (parser);
18680 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18684 /* Parse an Objective-C class interface. */
18687 cp_parser_objc_class_interface (cp_parser* parser)
18689 tree name, super, categ, protos;
18691 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18692 name = cp_parser_identifier (parser);
18693 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18694 protos = cp_parser_objc_protocol_refs_opt (parser);
18696 /* We have either a class or a category on our hands. */
18698 objc_start_category_interface (name, categ, protos);
18701 objc_start_class_interface (name, super, protos);
18702 /* Handle instance variable declarations, if any. */
18703 cp_parser_objc_class_ivars (parser);
18704 objc_continue_interface ();
18707 cp_parser_objc_method_prototype_list (parser);
18710 /* Parse an Objective-C class implementation. */
18713 cp_parser_objc_class_implementation (cp_parser* parser)
18715 tree name, super, categ;
18717 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18718 name = cp_parser_identifier (parser);
18719 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18721 /* We have either a class or a category on our hands. */
18723 objc_start_category_implementation (name, categ);
18726 objc_start_class_implementation (name, super);
18727 /* Handle instance variable declarations, if any. */
18728 cp_parser_objc_class_ivars (parser);
18729 objc_continue_implementation ();
18732 cp_parser_objc_method_definition_list (parser);
18735 /* Consume the @end token and finish off the implementation. */
18738 cp_parser_objc_end_implementation (cp_parser* parser)
18740 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18741 objc_finish_implementation ();
18744 /* Parse an Objective-C declaration. */
18747 cp_parser_objc_declaration (cp_parser* parser)
18749 /* Try to figure out what kind of declaration is present. */
18750 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18752 switch (kwd->keyword)
18755 cp_parser_objc_alias_declaration (parser);
18758 cp_parser_objc_class_declaration (parser);
18760 case RID_AT_PROTOCOL:
18761 cp_parser_objc_protocol_declaration (parser);
18763 case RID_AT_INTERFACE:
18764 cp_parser_objc_class_interface (parser);
18766 case RID_AT_IMPLEMENTATION:
18767 cp_parser_objc_class_implementation (parser);
18770 cp_parser_objc_end_implementation (parser);
18773 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18774 cp_parser_skip_to_end_of_block_or_statement (parser);
18778 /* Parse an Objective-C try-catch-finally statement.
18780 objc-try-catch-finally-stmt:
18781 @try compound-statement objc-catch-clause-seq [opt]
18782 objc-finally-clause [opt]
18784 objc-catch-clause-seq:
18785 objc-catch-clause objc-catch-clause-seq [opt]
18788 @catch ( exception-declaration ) compound-statement
18790 objc-finally-clause
18791 @finally compound-statement
18793 Returns NULL_TREE. */
18796 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
18797 location_t location;
18800 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
18801 location = cp_lexer_peek_token (parser->lexer)->location;
18802 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
18803 node, lest it get absorbed into the surrounding block. */
18804 stmt = push_stmt_list ();
18805 cp_parser_compound_statement (parser, NULL, false);
18806 objc_begin_try_stmt (location, pop_stmt_list (stmt));
18808 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
18810 cp_parameter_declarator *parmdecl;
18813 cp_lexer_consume_token (parser->lexer);
18814 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18815 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18816 parm = grokdeclarator (parmdecl->declarator,
18817 &parmdecl->decl_specifiers,
18818 PARM, /*initialized=*/0,
18819 /*attrlist=*/NULL);
18820 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18821 objc_begin_catch_clause (parm);
18822 cp_parser_compound_statement (parser, NULL, false);
18823 objc_finish_catch_clause ();
18826 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
18828 cp_lexer_consume_token (parser->lexer);
18829 location = cp_lexer_peek_token (parser->lexer)->location;
18830 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
18831 node, lest it get absorbed into the surrounding block. */
18832 stmt = push_stmt_list ();
18833 cp_parser_compound_statement (parser, NULL, false);
18834 objc_build_finally_clause (location, pop_stmt_list (stmt));
18837 return objc_finish_try_stmt ();
18840 /* Parse an Objective-C synchronized statement.
18842 objc-synchronized-stmt:
18843 @synchronized ( expression ) compound-statement
18845 Returns NULL_TREE. */
18848 cp_parser_objc_synchronized_statement (cp_parser *parser) {
18849 location_t location;
18852 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
18854 location = cp_lexer_peek_token (parser->lexer)->location;
18855 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18856 lock = cp_parser_expression (parser, false);
18857 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18859 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
18860 node, lest it get absorbed into the surrounding block. */
18861 stmt = push_stmt_list ();
18862 cp_parser_compound_statement (parser, NULL, false);
18864 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
18867 /* Parse an Objective-C throw statement.
18870 @throw assignment-expression [opt] ;
18872 Returns a constructed '@throw' statement. */
18875 cp_parser_objc_throw_statement (cp_parser *parser) {
18876 tree expr = NULL_TREE;
18878 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
18880 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18881 expr = cp_parser_assignment_expression (parser, false);
18883 cp_parser_consume_semicolon_at_end_of_statement (parser);
18885 return objc_build_throw_stmt (expr);
18888 /* Parse an Objective-C statement. */
18891 cp_parser_objc_statement (cp_parser * parser) {
18892 /* Try to figure out what kind of declaration is present. */
18893 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18895 switch (kwd->keyword)
18898 return cp_parser_objc_try_catch_finally_statement (parser);
18899 case RID_AT_SYNCHRONIZED:
18900 return cp_parser_objc_synchronized_statement (parser);
18902 return cp_parser_objc_throw_statement (parser);
18904 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18905 cp_parser_skip_to_end_of_block_or_statement (parser);
18908 return error_mark_node;
18911 /* OpenMP 2.5 parsing routines. */
18913 /* Returns name of the next clause.
18914 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
18915 the token is not consumed. Otherwise appropriate pragma_omp_clause is
18916 returned and the token is consumed. */
18918 static pragma_omp_clause
18919 cp_parser_omp_clause_name (cp_parser *parser)
18921 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
18923 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
18924 result = PRAGMA_OMP_CLAUSE_IF;
18925 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
18926 result = PRAGMA_OMP_CLAUSE_DEFAULT;
18927 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
18928 result = PRAGMA_OMP_CLAUSE_PRIVATE;
18929 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18931 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
18932 const char *p = IDENTIFIER_POINTER (id);
18937 if (!strcmp ("copyin", p))
18938 result = PRAGMA_OMP_CLAUSE_COPYIN;
18939 else if (!strcmp ("copyprivate", p))
18940 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
18943 if (!strcmp ("firstprivate", p))
18944 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
18947 if (!strcmp ("lastprivate", p))
18948 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
18951 if (!strcmp ("nowait", p))
18952 result = PRAGMA_OMP_CLAUSE_NOWAIT;
18953 else if (!strcmp ("num_threads", p))
18954 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
18957 if (!strcmp ("ordered", p))
18958 result = PRAGMA_OMP_CLAUSE_ORDERED;
18961 if (!strcmp ("reduction", p))
18962 result = PRAGMA_OMP_CLAUSE_REDUCTION;
18965 if (!strcmp ("schedule", p))
18966 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
18967 else if (!strcmp ("shared", p))
18968 result = PRAGMA_OMP_CLAUSE_SHARED;
18973 if (result != PRAGMA_OMP_CLAUSE_NONE)
18974 cp_lexer_consume_token (parser->lexer);
18979 /* Validate that a clause of the given type does not already exist. */
18982 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
18986 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
18987 if (OMP_CLAUSE_CODE (c) == code)
18989 error ("too many %qs clauses", name);
18997 variable-list , identifier
18999 In addition, we match a closing parenthesis. An opening parenthesis
19000 will have been consumed by the caller.
19002 If KIND is nonzero, create the appropriate node and install the decl
19003 in OMP_CLAUSE_DECL and add the node to the head of the list.
19005 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19006 return the list created. */
19009 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19016 name = cp_parser_id_expression (parser, /*template_p=*/false,
19017 /*check_dependency_p=*/true,
19018 /*template_p=*/NULL,
19019 /*declarator_p=*/false,
19020 /*optional_p=*/false);
19021 if (name == error_mark_node)
19024 decl = cp_parser_lookup_name_simple (parser, name);
19025 if (decl == error_mark_node)
19026 cp_parser_name_lookup_error (parser, name, decl, NULL);
19027 else if (kind != 0)
19029 tree u = build_omp_clause (kind);
19030 OMP_CLAUSE_DECL (u) = decl;
19031 OMP_CLAUSE_CHAIN (u) = list;
19035 list = tree_cons (decl, NULL_TREE, list);
19038 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19040 cp_lexer_consume_token (parser->lexer);
19043 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19047 /* Try to resync to an unnested comma. Copied from
19048 cp_parser_parenthesized_expression_list. */
19050 ending = cp_parser_skip_to_closing_parenthesis (parser,
19051 /*recovering=*/true,
19053 /*consume_paren=*/true);
19061 /* Similarly, but expect leading and trailing parenthesis. This is a very
19062 common case for omp clauses. */
19065 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19067 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19068 return cp_parser_omp_var_list_no_open (parser, kind, list);
19073 default ( shared | none ) */
19076 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19078 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19081 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19083 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19085 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19086 const char *p = IDENTIFIER_POINTER (id);
19091 if (strcmp ("none", p) != 0)
19093 kind = OMP_CLAUSE_DEFAULT_NONE;
19097 if (strcmp ("shared", p) != 0)
19099 kind = OMP_CLAUSE_DEFAULT_SHARED;
19106 cp_lexer_consume_token (parser->lexer);
19111 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19114 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19115 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19116 /*or_comma=*/false,
19117 /*consume_paren=*/true);
19119 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19122 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19123 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19124 OMP_CLAUSE_CHAIN (c) = list;
19125 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19131 if ( expression ) */
19134 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19138 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19141 t = cp_parser_condition (parser);
19143 if (t == error_mark_node
19144 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19145 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19146 /*or_comma=*/false,
19147 /*consume_paren=*/true);
19149 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19151 c = build_omp_clause (OMP_CLAUSE_IF);
19152 OMP_CLAUSE_IF_EXPR (c) = t;
19153 OMP_CLAUSE_CHAIN (c) = list;
19162 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19166 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19168 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19169 OMP_CLAUSE_CHAIN (c) = list;
19174 num_threads ( expression ) */
19177 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19181 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19184 t = cp_parser_expression (parser, false);
19186 if (t == error_mark_node
19187 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19188 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19189 /*or_comma=*/false,
19190 /*consume_paren=*/true);
19192 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19194 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19195 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19196 OMP_CLAUSE_CHAIN (c) = list;
19205 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19209 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19211 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19212 OMP_CLAUSE_CHAIN (c) = list;
19217 reduction ( reduction-operator : variable-list )
19219 reduction-operator:
19220 One of: + * - & ^ | && || */
19223 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19225 enum tree_code code;
19228 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19231 switch (cp_lexer_peek_token (parser->lexer)->type)
19243 code = BIT_AND_EXPR;
19246 code = BIT_XOR_EXPR;
19249 code = BIT_IOR_EXPR;
19252 code = TRUTH_ANDIF_EXPR;
19255 code = TRUTH_ORIF_EXPR;
19258 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19260 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19261 /*or_comma=*/false,
19262 /*consume_paren=*/true);
19265 cp_lexer_consume_token (parser->lexer);
19267 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19270 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19271 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19272 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19278 schedule ( schedule-kind )
19279 schedule ( schedule-kind , expression )
19282 static | dynamic | guided | runtime */
19285 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19289 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19292 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19294 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19296 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19297 const char *p = IDENTIFIER_POINTER (id);
19302 if (strcmp ("dynamic", p) != 0)
19304 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19308 if (strcmp ("guided", p) != 0)
19310 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19314 if (strcmp ("runtime", p) != 0)
19316 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19323 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19324 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19327 cp_lexer_consume_token (parser->lexer);
19329 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19331 cp_lexer_consume_token (parser->lexer);
19333 t = cp_parser_assignment_expression (parser, false);
19335 if (t == error_mark_node)
19337 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19338 error ("schedule %<runtime%> does not take "
19339 "a %<chunk_size%> parameter");
19341 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19343 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19346 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19349 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19350 OMP_CLAUSE_CHAIN (c) = list;
19354 cp_parser_error (parser, "invalid schedule kind");
19356 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19357 /*or_comma=*/false,
19358 /*consume_paren=*/true);
19362 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19363 is a bitmask in MASK. Return the list of clauses found; the result
19364 of clause default goes in *pdefault. */
19367 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19368 const char *where, cp_token *pragma_tok)
19370 tree clauses = NULL;
19372 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19374 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19375 const char *c_name;
19376 tree prev = clauses;
19380 case PRAGMA_OMP_CLAUSE_COPYIN:
19381 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19384 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19385 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19387 c_name = "copyprivate";
19389 case PRAGMA_OMP_CLAUSE_DEFAULT:
19390 clauses = cp_parser_omp_clause_default (parser, clauses);
19391 c_name = "default";
19393 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19394 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19396 c_name = "firstprivate";
19398 case PRAGMA_OMP_CLAUSE_IF:
19399 clauses = cp_parser_omp_clause_if (parser, clauses);
19402 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19403 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19405 c_name = "lastprivate";
19407 case PRAGMA_OMP_CLAUSE_NOWAIT:
19408 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19411 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19412 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19413 c_name = "num_threads";
19415 case PRAGMA_OMP_CLAUSE_ORDERED:
19416 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19417 c_name = "ordered";
19419 case PRAGMA_OMP_CLAUSE_PRIVATE:
19420 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19422 c_name = "private";
19424 case PRAGMA_OMP_CLAUSE_REDUCTION:
19425 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19426 c_name = "reduction";
19428 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19429 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19430 c_name = "schedule";
19432 case PRAGMA_OMP_CLAUSE_SHARED:
19433 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19438 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19442 if (((mask >> c_kind) & 1) == 0)
19444 /* Remove the invalid clause(s) from the list to avoid
19445 confusing the rest of the compiler. */
19447 error ("%qs is not valid for %qs", c_name, where);
19451 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19452 return finish_omp_clauses (clauses);
19459 In practice, we're also interested in adding the statement to an
19460 outer node. So it is convenient if we work around the fact that
19461 cp_parser_statement calls add_stmt. */
19464 cp_parser_begin_omp_structured_block (cp_parser *parser)
19466 unsigned save = parser->in_statement;
19468 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19469 This preserves the "not within loop or switch" style error messages
19470 for nonsense cases like
19476 if (parser->in_statement)
19477 parser->in_statement = IN_OMP_BLOCK;
19483 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19485 parser->in_statement = save;
19489 cp_parser_omp_structured_block (cp_parser *parser)
19491 tree stmt = begin_omp_structured_block ();
19492 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19494 cp_parser_statement (parser, NULL_TREE, false, NULL);
19496 cp_parser_end_omp_structured_block (parser, save);
19497 return finish_omp_structured_block (stmt);
19501 # pragma omp atomic new-line
19505 x binop= expr | x++ | ++x | x-- | --x
19507 +, *, -, /, &, ^, |, <<, >>
19509 where x is an lvalue expression with scalar type. */
19512 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19515 enum tree_code code;
19517 cp_parser_require_pragma_eol (parser, pragma_tok);
19519 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19521 switch (TREE_CODE (lhs))
19526 case PREINCREMENT_EXPR:
19527 case POSTINCREMENT_EXPR:
19528 lhs = TREE_OPERAND (lhs, 0);
19530 rhs = integer_one_node;
19533 case PREDECREMENT_EXPR:
19534 case POSTDECREMENT_EXPR:
19535 lhs = TREE_OPERAND (lhs, 0);
19537 rhs = integer_one_node;
19541 switch (cp_lexer_peek_token (parser->lexer)->type)
19547 code = TRUNC_DIV_EXPR;
19555 case CPP_LSHIFT_EQ:
19556 code = LSHIFT_EXPR;
19558 case CPP_RSHIFT_EQ:
19559 code = RSHIFT_EXPR;
19562 code = BIT_AND_EXPR;
19565 code = BIT_IOR_EXPR;
19568 code = BIT_XOR_EXPR;
19571 cp_parser_error (parser,
19572 "invalid operator for %<#pragma omp atomic%>");
19575 cp_lexer_consume_token (parser->lexer);
19577 rhs = cp_parser_expression (parser, false);
19578 if (rhs == error_mark_node)
19582 finish_omp_atomic (code, lhs, rhs);
19583 cp_parser_consume_semicolon_at_end_of_statement (parser);
19587 cp_parser_skip_to_end_of_block_or_statement (parser);
19592 # pragma omp barrier new-line */
19595 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19597 cp_parser_require_pragma_eol (parser, pragma_tok);
19598 finish_omp_barrier ();
19602 # pragma omp critical [(name)] new-line
19603 structured-block */
19606 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19608 tree stmt, name = NULL;
19610 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19612 cp_lexer_consume_token (parser->lexer);
19614 name = cp_parser_identifier (parser);
19616 if (name == error_mark_node
19617 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19618 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19619 /*or_comma=*/false,
19620 /*consume_paren=*/true);
19621 if (name == error_mark_node)
19624 cp_parser_require_pragma_eol (parser, pragma_tok);
19626 stmt = cp_parser_omp_structured_block (parser);
19627 return c_finish_omp_critical (stmt, name);
19631 # pragma omp flush flush-vars[opt] new-line
19634 ( variable-list ) */
19637 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19639 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19640 (void) cp_parser_omp_var_list (parser, 0, NULL);
19641 cp_parser_require_pragma_eol (parser, pragma_tok);
19643 finish_omp_flush ();
19646 /* Parse the restricted form of the for statment allowed by OpenMP. */
19649 cp_parser_omp_for_loop (cp_parser *parser)
19651 tree init, cond, incr, body, decl, pre_body;
19654 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19656 cp_parser_error (parser, "for statement expected");
19659 loc = cp_lexer_consume_token (parser->lexer)->location;
19660 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19663 init = decl = NULL;
19664 pre_body = push_stmt_list ();
19665 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19667 cp_decl_specifier_seq type_specifiers;
19669 /* First, try to parse as an initialized declaration. See
19670 cp_parser_condition, from whence the bulk of this is copied. */
19672 cp_parser_parse_tentatively (parser);
19673 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19675 if (!cp_parser_error_occurred (parser))
19677 tree asm_specification, attributes;
19678 cp_declarator *declarator;
19680 declarator = cp_parser_declarator (parser,
19681 CP_PARSER_DECLARATOR_NAMED,
19682 /*ctor_dtor_or_conv_p=*/NULL,
19683 /*parenthesized_p=*/NULL,
19684 /*member_p=*/false);
19685 attributes = cp_parser_attributes_opt (parser);
19686 asm_specification = cp_parser_asm_specification_opt (parser);
19688 cp_parser_require (parser, CPP_EQ, "`='");
19689 if (cp_parser_parse_definitely (parser))
19693 decl = start_decl (declarator, &type_specifiers,
19694 /*initialized_p=*/false, attributes,
19695 /*prefix_attributes=*/NULL_TREE,
19698 init = cp_parser_assignment_expression (parser, false);
19700 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19701 asm_specification, LOOKUP_ONLYCONVERTING);
19704 pop_scope (pushed_scope);
19708 cp_parser_abort_tentative_parse (parser);
19710 /* If parsing as an initialized declaration failed, try again as
19711 a simple expression. */
19713 init = cp_parser_expression (parser, false);
19715 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19716 pre_body = pop_stmt_list (pre_body);
19719 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19720 cond = cp_parser_condition (parser);
19721 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19724 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19725 incr = cp_parser_expression (parser, false);
19727 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19728 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19729 /*or_comma=*/false,
19730 /*consume_paren=*/true);
19732 /* Note that we saved the original contents of this flag when we entered
19733 the structured block, and so we don't need to re-save it here. */
19734 parser->in_statement = IN_OMP_FOR;
19736 /* Note that the grammar doesn't call for a structured block here,
19737 though the loop as a whole is a structured block. */
19738 body = push_stmt_list ();
19739 cp_parser_statement (parser, NULL_TREE, false, NULL);
19740 body = pop_stmt_list (body);
19742 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19746 #pragma omp for for-clause[optseq] new-line
19749 #define OMP_FOR_CLAUSE_MASK \
19750 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19751 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19752 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19753 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19754 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
19755 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
19756 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19759 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
19761 tree clauses, sb, ret;
19764 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
19765 "#pragma omp for", pragma_tok);
19767 sb = begin_omp_structured_block ();
19768 save = cp_parser_begin_omp_structured_block (parser);
19770 ret = cp_parser_omp_for_loop (parser);
19772 OMP_FOR_CLAUSES (ret) = clauses;
19774 cp_parser_end_omp_structured_block (parser, save);
19775 add_stmt (finish_omp_structured_block (sb));
19781 # pragma omp master new-line
19782 structured-block */
19785 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
19787 cp_parser_require_pragma_eol (parser, pragma_tok);
19788 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
19792 # pragma omp ordered new-line
19793 structured-block */
19796 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
19798 cp_parser_require_pragma_eol (parser, pragma_tok);
19799 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
19805 { section-sequence }
19808 section-directive[opt] structured-block
19809 section-sequence section-directive structured-block */
19812 cp_parser_omp_sections_scope (cp_parser *parser)
19814 tree stmt, substmt;
19815 bool error_suppress = false;
19818 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
19821 stmt = push_stmt_list ();
19823 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
19827 substmt = begin_omp_structured_block ();
19828 save = cp_parser_begin_omp_structured_block (parser);
19832 cp_parser_statement (parser, NULL_TREE, false, NULL);
19834 tok = cp_lexer_peek_token (parser->lexer);
19835 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19837 if (tok->type == CPP_CLOSE_BRACE)
19839 if (tok->type == CPP_EOF)
19843 cp_parser_end_omp_structured_block (parser, save);
19844 substmt = finish_omp_structured_block (substmt);
19845 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19846 add_stmt (substmt);
19851 tok = cp_lexer_peek_token (parser->lexer);
19852 if (tok->type == CPP_CLOSE_BRACE)
19854 if (tok->type == CPP_EOF)
19857 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
19859 cp_lexer_consume_token (parser->lexer);
19860 cp_parser_require_pragma_eol (parser, tok);
19861 error_suppress = false;
19863 else if (!error_suppress)
19865 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
19866 error_suppress = true;
19869 substmt = cp_parser_omp_structured_block (parser);
19870 substmt = build1 (OMP_SECTION, void_type_node, substmt);
19871 add_stmt (substmt);
19873 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
19875 substmt = pop_stmt_list (stmt);
19877 stmt = make_node (OMP_SECTIONS);
19878 TREE_TYPE (stmt) = void_type_node;
19879 OMP_SECTIONS_BODY (stmt) = substmt;
19886 # pragma omp sections sections-clause[optseq] newline
19889 #define OMP_SECTIONS_CLAUSE_MASK \
19890 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19891 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19892 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
19893 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19894 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
19897 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
19901 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
19902 "#pragma omp sections", pragma_tok);
19904 ret = cp_parser_omp_sections_scope (parser);
19906 OMP_SECTIONS_CLAUSES (ret) = clauses;
19912 # pragma parallel parallel-clause new-line
19913 # pragma parallel for parallel-for-clause new-line
19914 # pragma parallel sections parallel-sections-clause new-line */
19916 #define OMP_PARALLEL_CLAUSE_MASK \
19917 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
19918 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19919 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
19920 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
19921 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
19922 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
19923 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
19924 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
19927 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
19929 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
19930 const char *p_name = "#pragma omp parallel";
19931 tree stmt, clauses, par_clause, ws_clause, block;
19932 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
19935 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19937 cp_lexer_consume_token (parser->lexer);
19938 p_kind = PRAGMA_OMP_PARALLEL_FOR;
19939 p_name = "#pragma omp parallel for";
19940 mask |= OMP_FOR_CLAUSE_MASK;
19941 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19943 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19945 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19946 const char *p = IDENTIFIER_POINTER (id);
19947 if (strcmp (p, "sections") == 0)
19949 cp_lexer_consume_token (parser->lexer);
19950 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
19951 p_name = "#pragma omp parallel sections";
19952 mask |= OMP_SECTIONS_CLAUSE_MASK;
19953 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
19957 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
19958 block = begin_omp_parallel ();
19959 save = cp_parser_begin_omp_structured_block (parser);
19963 case PRAGMA_OMP_PARALLEL:
19964 cp_parser_already_scoped_statement (parser);
19965 par_clause = clauses;
19968 case PRAGMA_OMP_PARALLEL_FOR:
19969 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19970 stmt = cp_parser_omp_for_loop (parser);
19972 OMP_FOR_CLAUSES (stmt) = ws_clause;
19975 case PRAGMA_OMP_PARALLEL_SECTIONS:
19976 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
19977 stmt = cp_parser_omp_sections_scope (parser);
19979 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
19983 gcc_unreachable ();
19986 cp_parser_end_omp_structured_block (parser, save);
19987 stmt = finish_omp_parallel (par_clause, block);
19988 if (p_kind != PRAGMA_OMP_PARALLEL)
19989 OMP_PARALLEL_COMBINED (stmt) = 1;
19994 # pragma omp single single-clause[optseq] new-line
19995 structured-block */
19997 #define OMP_SINGLE_CLAUSE_MASK \
19998 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
19999 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20000 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20001 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20004 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20006 tree stmt = make_node (OMP_SINGLE);
20007 TREE_TYPE (stmt) = void_type_node;
20009 OMP_SINGLE_CLAUSES (stmt)
20010 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20011 "#pragma omp single", pragma_tok);
20012 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20014 return add_stmt (stmt);
20018 # pragma omp threadprivate (variable-list) */
20021 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20025 vars = cp_parser_omp_var_list (parser, 0, NULL);
20026 cp_parser_require_pragma_eol (parser, pragma_tok);
20028 finish_omp_threadprivate (vars);
20031 /* Main entry point to OpenMP statement pragmas. */
20034 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20038 switch (pragma_tok->pragma_kind)
20040 case PRAGMA_OMP_ATOMIC:
20041 cp_parser_omp_atomic (parser, pragma_tok);
20043 case PRAGMA_OMP_CRITICAL:
20044 stmt = cp_parser_omp_critical (parser, pragma_tok);
20046 case PRAGMA_OMP_FOR:
20047 stmt = cp_parser_omp_for (parser, pragma_tok);
20049 case PRAGMA_OMP_MASTER:
20050 stmt = cp_parser_omp_master (parser, pragma_tok);
20052 case PRAGMA_OMP_ORDERED:
20053 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20055 case PRAGMA_OMP_PARALLEL:
20056 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20058 case PRAGMA_OMP_SECTIONS:
20059 stmt = cp_parser_omp_sections (parser, pragma_tok);
20061 case PRAGMA_OMP_SINGLE:
20062 stmt = cp_parser_omp_single (parser, pragma_tok);
20065 gcc_unreachable ();
20069 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20074 static GTY (()) cp_parser *the_parser;
20077 /* Special handling for the first token or line in the file. The first
20078 thing in the file might be #pragma GCC pch_preprocess, which loads a
20079 PCH file, which is a GC collection point. So we need to handle this
20080 first pragma without benefit of an existing lexer structure.
20082 Always returns one token to the caller in *FIRST_TOKEN. This is
20083 either the true first token of the file, or the first token after
20084 the initial pragma. */
20087 cp_parser_initial_pragma (cp_token *first_token)
20091 cp_lexer_get_preprocessor_token (NULL, first_token);
20092 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20095 cp_lexer_get_preprocessor_token (NULL, first_token);
20096 if (first_token->type == CPP_STRING)
20098 name = first_token->u.value;
20100 cp_lexer_get_preprocessor_token (NULL, first_token);
20101 if (first_token->type != CPP_PRAGMA_EOL)
20102 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20105 error ("expected string literal");
20107 /* Skip to the end of the pragma. */
20108 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20109 cp_lexer_get_preprocessor_token (NULL, first_token);
20111 /* Now actually load the PCH file. */
20113 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20115 /* Read one more token to return to our caller. We have to do this
20116 after reading the PCH file in, since its pointers have to be
20118 cp_lexer_get_preprocessor_token (NULL, first_token);
20121 /* Normal parsing of a pragma token. Here we can (and must) use the
20125 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20127 cp_token *pragma_tok;
20130 pragma_tok = cp_lexer_consume_token (parser->lexer);
20131 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20132 parser->lexer->in_pragma = true;
20134 id = pragma_tok->pragma_kind;
20137 case PRAGMA_GCC_PCH_PREPROCESS:
20138 error ("%<#pragma GCC pch_preprocess%> must be first");
20141 case PRAGMA_OMP_BARRIER:
20144 case pragma_compound:
20145 cp_parser_omp_barrier (parser, pragma_tok);
20148 error ("%<#pragma omp barrier%> may only be "
20149 "used in compound statements");
20156 case PRAGMA_OMP_FLUSH:
20159 case pragma_compound:
20160 cp_parser_omp_flush (parser, pragma_tok);
20163 error ("%<#pragma omp flush%> may only be "
20164 "used in compound statements");
20171 case PRAGMA_OMP_THREADPRIVATE:
20172 cp_parser_omp_threadprivate (parser, pragma_tok);
20175 case PRAGMA_OMP_ATOMIC:
20176 case PRAGMA_OMP_CRITICAL:
20177 case PRAGMA_OMP_FOR:
20178 case PRAGMA_OMP_MASTER:
20179 case PRAGMA_OMP_ORDERED:
20180 case PRAGMA_OMP_PARALLEL:
20181 case PRAGMA_OMP_SECTIONS:
20182 case PRAGMA_OMP_SINGLE:
20183 if (context == pragma_external)
20185 cp_parser_omp_construct (parser, pragma_tok);
20188 case PRAGMA_OMP_SECTION:
20189 error ("%<#pragma omp section%> may only be used in "
20190 "%<#pragma omp sections%> construct");
20194 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20195 c_invoke_pragma_handler (id);
20199 cp_parser_error (parser, "expected declaration specifiers");
20203 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20207 /* The interface the pragma parsers have to the lexer. */
20210 pragma_lex (tree *value)
20213 enum cpp_ttype ret;
20215 tok = cp_lexer_peek_token (the_parser->lexer);
20218 *value = tok->u.value;
20220 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20222 else if (ret == CPP_STRING)
20223 *value = cp_parser_string_literal (the_parser, false, false);
20226 cp_lexer_consume_token (the_parser->lexer);
20227 if (ret == CPP_KEYWORD)
20235 /* External interface. */
20237 /* Parse one entire translation unit. */
20240 c_parse_file (void)
20242 bool error_occurred;
20243 static bool already_called = false;
20245 if (already_called)
20247 sorry ("inter-module optimizations not implemented for C++");
20250 already_called = true;
20252 the_parser = cp_parser_new ();
20253 push_deferring_access_checks (flag_access_control
20254 ? dk_no_deferred : dk_no_check);
20255 error_occurred = cp_parser_translation_unit (the_parser);
20259 #include "gt-cp-parser.h"