2 Copyright (C) 2000, 2001, 2002, 2003, 2004,
3 2005, 2007 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
26 #include "dyn-string.h"
34 #include "diagnostic.h"
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45 and c-lex.c) and the C++ parser. */
47 /* A token's value and its associated deferred access checks and
50 struct tree_check GTY(())
52 /* The value associated with the token. */
54 /* The checks that have been associated with value. */
55 VEC (deferred_access_check, gc)* checks;
56 /* The token's qualifying scope (used when it is a
57 CPP_NESTED_NAME_SPECIFIER). */
58 tree qualifying_scope;
63 typedef struct cp_token GTY (())
65 /* The kind of token. */
66 ENUM_BITFIELD (cpp_ttype) type : 8;
67 /* If this token is a keyword, this value indicates which keyword.
68 Otherwise, this value is RID_MAX. */
69 ENUM_BITFIELD (rid) keyword : 8;
72 /* Identifier for the pragma. */
73 ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
74 /* True if this token is from a system header. */
75 BOOL_BITFIELD in_system_header : 1;
76 /* True if this token is from a context where it is implicitly extern "C" */
77 BOOL_BITFIELD implicit_extern_c : 1;
78 /* True for a CPP_NAME token that is not a keyword (i.e., for which
79 KEYWORD is RID_MAX) iff this name was looked up and found to be
80 ambiguous. An error has already been reported. */
81 BOOL_BITFIELD ambiguous_p : 1;
82 /* The input file stack index at which this token was found. */
83 unsigned input_file_stack_index : INPUT_FILE_STACK_BITS;
84 /* The value associated with this token, if any. */
85 union cp_token_value {
86 /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID. */
87 struct tree_check* GTY((tag ("1"))) tree_check_value;
88 /* Use for all other tokens. */
89 tree GTY((tag ("0"))) value;
90 } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
91 /* The location at which this token was found. */
95 /* We use a stack of token pointer for saving token sets. */
96 typedef struct cp_token *cp_token_position;
97 DEF_VEC_P (cp_token_position);
98 DEF_VEC_ALLOC_P (cp_token_position,heap);
100 static cp_token eof_token =
102 CPP_EOF, RID_MAX, 0, PRAGMA_NONE, 0, 0, false, 0, { NULL },
103 #if USE_MAPPED_LOCATION
110 /* The cp_lexer structure represents the C++ lexer. It is responsible
111 for managing the token stream from the preprocessor and supplying
112 it to the parser. Tokens are never added to the cp_lexer after
115 typedef struct cp_lexer GTY (())
117 /* The memory allocated for the buffer. NULL if this lexer does not
118 own the token buffer. */
119 cp_token * GTY ((length ("%h.buffer_length"))) buffer;
120 /* If the lexer owns the buffer, this is the number of tokens in the
122 size_t buffer_length;
124 /* A pointer just past the last available token. The tokens
125 in this lexer are [buffer, last_token). */
126 cp_token_position GTY ((skip)) last_token;
128 /* The next available token. If NEXT_TOKEN is &eof_token, then there are
129 no more available tokens. */
130 cp_token_position GTY ((skip)) next_token;
132 /* A stack indicating positions at which cp_lexer_save_tokens was
133 called. The top entry is the most recent position at which we
134 began saving tokens. If the stack is non-empty, we are saving
136 VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
138 /* The next lexer in a linked list of lexers. */
139 struct cp_lexer *next;
141 /* True if we should output debugging information. */
144 /* True if we're in the context of parsing a pragma, and should not
145 increment past the end-of-line marker. */
149 /* cp_token_cache is a range of tokens. There is no need to represent
150 allocate heap memory for it, since tokens are never removed from the
151 lexer's array. There is also no need for the GC to walk through
152 a cp_token_cache, since everything in here is referenced through
155 typedef struct cp_token_cache GTY(())
157 /* The beginning of the token range. */
158 cp_token * GTY((skip)) first;
160 /* Points immediately after the last token in the range. */
161 cp_token * GTY ((skip)) last;
166 static cp_lexer *cp_lexer_new_main
168 static cp_lexer *cp_lexer_new_from_tokens
169 (cp_token_cache *tokens);
170 static void cp_lexer_destroy
172 static int cp_lexer_saving_tokens
174 static cp_token_position cp_lexer_token_position
176 static cp_token *cp_lexer_token_at
177 (cp_lexer *, cp_token_position);
178 static void cp_lexer_get_preprocessor_token
179 (cp_lexer *, cp_token *);
180 static inline cp_token *cp_lexer_peek_token
182 static cp_token *cp_lexer_peek_nth_token
183 (cp_lexer *, size_t);
184 static inline bool cp_lexer_next_token_is
185 (cp_lexer *, enum cpp_ttype);
186 static bool cp_lexer_next_token_is_not
187 (cp_lexer *, enum cpp_ttype);
188 static bool cp_lexer_next_token_is_keyword
189 (cp_lexer *, enum rid);
190 static cp_token *cp_lexer_consume_token
192 static void cp_lexer_purge_token
194 static void cp_lexer_purge_tokens_after
195 (cp_lexer *, cp_token_position);
196 static void cp_lexer_save_tokens
198 static void cp_lexer_commit_tokens
200 static void cp_lexer_rollback_tokens
202 #ifdef ENABLE_CHECKING
203 static void cp_lexer_print_token
204 (FILE *, cp_token *);
205 static inline bool cp_lexer_debugging_p
207 static void cp_lexer_start_debugging
208 (cp_lexer *) ATTRIBUTE_UNUSED;
209 static void cp_lexer_stop_debugging
210 (cp_lexer *) ATTRIBUTE_UNUSED;
212 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
213 about passing NULL to functions that require non-NULL arguments
214 (fputs, fprintf). It will never be used, so all we need is a value
215 of the right type that's guaranteed not to be NULL. */
216 #define cp_lexer_debug_stream stdout
217 #define cp_lexer_print_token(str, tok) (void) 0
218 #define cp_lexer_debugging_p(lexer) 0
219 #endif /* ENABLE_CHECKING */
221 static cp_token_cache *cp_token_cache_new
222 (cp_token *, cp_token *);
224 static void cp_parser_initial_pragma
227 /* Manifest constants. */
228 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
229 #define CP_SAVED_TOKEN_STACK 5
231 /* A token type for keywords, as opposed to ordinary identifiers. */
232 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
234 /* A token type for template-ids. If a template-id is processed while
235 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
236 the value of the CPP_TEMPLATE_ID is whatever was returned by
237 cp_parser_template_id. */
238 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
240 /* A token type for nested-name-specifiers. If a
241 nested-name-specifier is processed while parsing tentatively, it is
242 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
243 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
244 cp_parser_nested_name_specifier_opt. */
245 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
247 /* A token type for tokens that are not tokens at all; these are used
248 to represent slots in the array where there used to be a token
249 that has now been deleted. */
250 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
252 /* The number of token types, including C++-specific ones. */
253 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
257 #ifdef ENABLE_CHECKING
258 /* The stream to which debugging output should be written. */
259 static FILE *cp_lexer_debug_stream;
260 #endif /* ENABLE_CHECKING */
262 /* Create a new main C++ lexer, the lexer that gets tokens from the
266 cp_lexer_new_main (void)
268 cp_token first_token;
275 /* It's possible that parsing the first pragma will load a PCH file,
276 which is a GC collection point. So we have to do that before
277 allocating any memory. */
278 cp_parser_initial_pragma (&first_token);
280 c_common_no_more_pch ();
282 /* Allocate the memory. */
283 lexer = GGC_CNEW (cp_lexer);
285 #ifdef ENABLE_CHECKING
286 /* Initially we are not debugging. */
287 lexer->debugging_p = false;
288 #endif /* ENABLE_CHECKING */
289 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
290 CP_SAVED_TOKEN_STACK);
292 /* Create the buffer. */
293 alloc = CP_LEXER_BUFFER_SIZE;
294 buffer = GGC_NEWVEC (cp_token, alloc);
296 /* Put the first token in the buffer. */
301 /* Get the remaining tokens from the preprocessor. */
302 while (pos->type != CPP_EOF)
309 buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
310 pos = buffer + space;
312 cp_lexer_get_preprocessor_token (lexer, pos);
314 lexer->buffer = buffer;
315 lexer->buffer_length = alloc - space;
316 lexer->last_token = pos;
317 lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
319 /* Subsequent preprocessor diagnostics should use compiler
320 diagnostic functions to get the compiler source location. */
321 cpp_get_options (parse_in)->client_diagnostic = true;
322 cpp_get_callbacks (parse_in)->error = cp_cpp_error;
324 gcc_assert (lexer->next_token->type != CPP_PURGED);
328 /* Create a new lexer whose token stream is primed with the tokens in
329 CACHE. When these tokens are exhausted, no new tokens will be read. */
332 cp_lexer_new_from_tokens (cp_token_cache *cache)
334 cp_token *first = cache->first;
335 cp_token *last = cache->last;
336 cp_lexer *lexer = GGC_CNEW (cp_lexer);
338 /* We do not own the buffer. */
339 lexer->buffer = NULL;
340 lexer->buffer_length = 0;
341 lexer->next_token = first == last ? &eof_token : first;
342 lexer->last_token = last;
344 lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
345 CP_SAVED_TOKEN_STACK);
347 #ifdef ENABLE_CHECKING
348 /* Initially we are not debugging. */
349 lexer->debugging_p = false;
352 gcc_assert (lexer->next_token->type != CPP_PURGED);
356 /* Frees all resources associated with LEXER. */
359 cp_lexer_destroy (cp_lexer *lexer)
362 ggc_free (lexer->buffer);
363 VEC_free (cp_token_position, heap, lexer->saved_tokens);
367 /* Returns nonzero if debugging information should be output. */
369 #ifdef ENABLE_CHECKING
372 cp_lexer_debugging_p (cp_lexer *lexer)
374 return lexer->debugging_p;
377 #endif /* ENABLE_CHECKING */
379 static inline cp_token_position
380 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
382 gcc_assert (!previous_p || lexer->next_token != &eof_token);
384 return lexer->next_token - previous_p;
387 static inline cp_token *
388 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
393 /* nonzero if we are presently saving tokens. */
396 cp_lexer_saving_tokens (const cp_lexer* lexer)
398 return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
401 /* Store the next token from the preprocessor in *TOKEN. Return true
402 if we reach EOF. If LEXER is NULL, assume we are handling an
403 initial #pragma pch_preprocess, and thus want the lexer to return
404 processed strings. */
407 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
409 static int is_extern_c = 0;
411 /* Get a new token from the preprocessor. */
413 = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
414 lexer == NULL ? 0 : C_LEX_RAW_STRINGS);
415 token->input_file_stack_index = input_file_stack_tick;
416 token->keyword = RID_MAX;
417 token->pragma_kind = PRAGMA_NONE;
418 token->in_system_header = in_system_header;
420 /* On some systems, some header files are surrounded by an
421 implicit extern "C" block. Set a flag in the token if it
422 comes from such a header. */
423 is_extern_c += pending_lang_change;
424 pending_lang_change = 0;
425 token->implicit_extern_c = is_extern_c > 0;
427 /* Check to see if this token is a keyword. */
428 if (token->type == CPP_NAME)
430 if (C_IS_RESERVED_WORD (token->u.value))
432 /* Mark this token as a keyword. */
433 token->type = CPP_KEYWORD;
434 /* Record which keyword. */
435 token->keyword = C_RID_CODE (token->u.value);
436 /* Update the value. Some keywords are mapped to particular
437 entities, rather than simply having the value of the
438 corresponding IDENTIFIER_NODE. For example, `__const' is
439 mapped to `const'. */
440 token->u.value = ridpointers[token->keyword];
444 if (warn_cxx0x_compat
445 && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
446 && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
448 /* Warn about the C++0x keyword (but still treat it as
450 warning (OPT_Wc__0x_compat,
451 "identifier %<%s%> will become a keyword in C++0x",
452 IDENTIFIER_POINTER (token->u.value));
454 /* Clear out the C_RID_CODE so we don't warn about this
455 particular identifier-turned-keyword again. */
456 C_RID_CODE (token->u.value) = RID_MAX;
459 token->ambiguous_p = false;
460 token->keyword = RID_MAX;
463 /* Handle Objective-C++ keywords. */
464 else if (token->type == CPP_AT_NAME)
466 token->type = CPP_KEYWORD;
467 switch (C_RID_CODE (token->u.value))
469 /* Map 'class' to '@class', 'private' to '@private', etc. */
470 case RID_CLASS: token->keyword = RID_AT_CLASS; break;
471 case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
472 case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
473 case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
474 case RID_THROW: token->keyword = RID_AT_THROW; break;
475 case RID_TRY: token->keyword = RID_AT_TRY; break;
476 case RID_CATCH: token->keyword = RID_AT_CATCH; break;
477 default: token->keyword = C_RID_CODE (token->u.value);
480 else if (token->type == CPP_PRAGMA)
482 /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST. */
483 token->pragma_kind = TREE_INT_CST_LOW (token->u.value);
484 token->u.value = NULL_TREE;
488 /* Update the globals input_location and in_system_header and the
489 input file stack from TOKEN. */
491 cp_lexer_set_source_position_from_token (cp_token *token)
493 if (token->type != CPP_EOF)
495 input_location = token->location;
496 in_system_header = token->in_system_header;
497 restore_input_file_stack (token->input_file_stack_index);
501 /* Return a pointer to the next token in the token stream, but do not
504 static inline cp_token *
505 cp_lexer_peek_token (cp_lexer *lexer)
507 if (cp_lexer_debugging_p (lexer))
509 fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
510 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
511 putc ('\n', cp_lexer_debug_stream);
513 return lexer->next_token;
516 /* Return true if the next token has the indicated TYPE. */
519 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
521 return cp_lexer_peek_token (lexer)->type == type;
524 /* Return true if the next token does not have the indicated TYPE. */
527 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
529 return !cp_lexer_next_token_is (lexer, type);
532 /* Return true if the next token is the indicated KEYWORD. */
535 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
537 return cp_lexer_peek_token (lexer)->keyword == keyword;
540 /* Return true if the next token is a keyword for a decl-specifier. */
543 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
547 token = cp_lexer_peek_token (lexer);
548 switch (token->keyword)
550 /* Storage classes. */
557 /* Elaborated type specifiers. */
563 /* Simple type specifiers. */
575 /* GNU extensions. */
578 /* C++0x 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)
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 = &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)
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, 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);
1710 static tree cp_parser_decltype
1713 /* Declarators [gram.dcl.decl] */
1715 static tree cp_parser_init_declarator
1716 (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1717 static cp_declarator *cp_parser_declarator
1718 (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1719 static cp_declarator *cp_parser_direct_declarator
1720 (cp_parser *, cp_parser_declarator_kind, int *, bool);
1721 static enum tree_code cp_parser_ptr_operator
1722 (cp_parser *, tree *, cp_cv_quals *);
1723 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1725 static tree cp_parser_declarator_id
1726 (cp_parser *, bool);
1727 static tree cp_parser_type_id
1729 static void cp_parser_type_specifier_seq
1730 (cp_parser *, bool, cp_decl_specifier_seq *);
1731 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1733 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1734 (cp_parser *, bool *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration
1736 (cp_parser *, bool, bool *);
1737 static void cp_parser_function_body
1739 static tree cp_parser_initializer
1740 (cp_parser *, bool *, bool *);
1741 static tree cp_parser_initializer_clause
1742 (cp_parser *, bool *);
1743 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1744 (cp_parser *, bool *);
1746 static bool cp_parser_ctor_initializer_opt_and_function_body
1749 /* Classes [gram.class] */
1751 static tree cp_parser_class_name
1752 (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1753 static tree cp_parser_class_specifier
1755 static tree cp_parser_class_head
1756 (cp_parser *, bool *, tree *, tree *);
1757 static enum tag_types cp_parser_class_key
1759 static void cp_parser_member_specification_opt
1761 static void cp_parser_member_declaration
1763 static tree cp_parser_pure_specifier
1765 static tree cp_parser_constant_initializer
1768 /* Derived classes [gram.class.derived] */
1770 static tree cp_parser_base_clause
1772 static tree cp_parser_base_specifier
1775 /* Special member functions [gram.special] */
1777 static tree cp_parser_conversion_function_id
1779 static tree cp_parser_conversion_type_id
1781 static cp_declarator *cp_parser_conversion_declarator_opt
1783 static bool cp_parser_ctor_initializer_opt
1785 static void cp_parser_mem_initializer_list
1787 static tree cp_parser_mem_initializer
1789 static tree cp_parser_mem_initializer_id
1792 /* Overloading [gram.over] */
1794 static tree cp_parser_operator_function_id
1796 static tree cp_parser_operator
1799 /* Templates [gram.temp] */
1801 static void cp_parser_template_declaration
1802 (cp_parser *, bool);
1803 static tree cp_parser_template_parameter_list
1805 static tree cp_parser_template_parameter
1806 (cp_parser *, bool *, bool *);
1807 static tree cp_parser_type_parameter
1808 (cp_parser *, bool *);
1809 static tree cp_parser_template_id
1810 (cp_parser *, bool, bool, bool);
1811 static tree cp_parser_template_name
1812 (cp_parser *, bool, bool, bool, bool *);
1813 static tree cp_parser_template_argument_list
1815 static tree cp_parser_template_argument
1817 static void cp_parser_explicit_instantiation
1819 static void cp_parser_explicit_specialization
1822 /* Exception handling [gram.exception] */
1824 static tree cp_parser_try_block
1826 static bool cp_parser_function_try_block
1828 static void cp_parser_handler_seq
1830 static void cp_parser_handler
1832 static tree cp_parser_exception_declaration
1834 static tree cp_parser_throw_expression
1836 static tree cp_parser_exception_specification_opt
1838 static tree cp_parser_type_id_list
1841 /* GNU Extensions */
1843 static tree cp_parser_asm_specification_opt
1845 static tree cp_parser_asm_operand_list
1847 static tree cp_parser_asm_clobber_list
1849 static tree cp_parser_attributes_opt
1851 static tree cp_parser_attribute_list
1853 static bool cp_parser_extension_opt
1854 (cp_parser *, int *);
1855 static void cp_parser_label_declaration
1858 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1859 static bool cp_parser_pragma
1860 (cp_parser *, enum pragma_context);
1862 /* Objective-C++ Productions */
1864 static tree cp_parser_objc_message_receiver
1866 static tree cp_parser_objc_message_args
1868 static tree cp_parser_objc_message_expression
1870 static tree cp_parser_objc_encode_expression
1872 static tree cp_parser_objc_defs_expression
1874 static tree cp_parser_objc_protocol_expression
1876 static tree cp_parser_objc_selector_expression
1878 static tree cp_parser_objc_expression
1880 static bool cp_parser_objc_selector_p
1882 static tree cp_parser_objc_selector
1884 static tree cp_parser_objc_protocol_refs_opt
1886 static void cp_parser_objc_declaration
1888 static tree cp_parser_objc_statement
1891 /* Utility Routines */
1893 static tree cp_parser_lookup_name
1894 (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *);
1895 static tree cp_parser_lookup_name_simple
1896 (cp_parser *, tree);
1897 static tree cp_parser_maybe_treat_template_as_class
1899 static bool cp_parser_check_declarator_template_parameters
1900 (cp_parser *, cp_declarator *);
1901 static bool cp_parser_check_template_parameters
1902 (cp_parser *, unsigned);
1903 static tree cp_parser_simple_cast_expression
1905 static tree cp_parser_global_scope_opt
1906 (cp_parser *, bool);
1907 static bool cp_parser_constructor_declarator_p
1908 (cp_parser *, bool);
1909 static tree cp_parser_function_definition_from_specifiers_and_declarator
1910 (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1911 static tree cp_parser_function_definition_after_declarator
1912 (cp_parser *, bool);
1913 static void cp_parser_template_declaration_after_export
1914 (cp_parser *, bool);
1915 static void cp_parser_perform_template_parameter_access_checks
1916 (VEC (deferred_access_check,gc)*);
1917 static tree cp_parser_single_declaration
1918 (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1919 static tree cp_parser_functional_cast
1920 (cp_parser *, tree);
1921 static tree cp_parser_save_member_function_body
1922 (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1923 static tree cp_parser_enclosed_template_argument_list
1925 static void cp_parser_save_default_args
1926 (cp_parser *, tree);
1927 static void cp_parser_late_parsing_for_member
1928 (cp_parser *, tree);
1929 static void cp_parser_late_parsing_default_args
1930 (cp_parser *, tree);
1931 static tree cp_parser_sizeof_operand
1932 (cp_parser *, enum rid);
1933 static tree cp_parser_trait_expr
1934 (cp_parser *, enum rid);
1935 static bool cp_parser_declares_only_class_p
1937 static void cp_parser_set_storage_class
1938 (cp_parser *, cp_decl_specifier_seq *, enum rid);
1939 static void cp_parser_set_decl_spec_type
1940 (cp_decl_specifier_seq *, tree, bool);
1941 static bool cp_parser_friend_p
1942 (const cp_decl_specifier_seq *);
1943 static cp_token *cp_parser_require
1944 (cp_parser *, enum cpp_ttype, const char *);
1945 static cp_token *cp_parser_require_keyword
1946 (cp_parser *, enum rid, const char *);
1947 static bool cp_parser_token_starts_function_definition_p
1949 static bool cp_parser_next_token_starts_class_definition_p
1951 static bool cp_parser_next_token_ends_template_argument_p
1953 static bool cp_parser_nth_token_starts_template_argument_list_p
1954 (cp_parser *, size_t);
1955 static enum tag_types cp_parser_token_is_class_key
1957 static void cp_parser_check_class_key
1958 (enum tag_types, tree type);
1959 static void cp_parser_check_access_in_redeclaration
1961 static bool cp_parser_optional_template_keyword
1963 static void cp_parser_pre_parsed_nested_name_specifier
1965 static void cp_parser_cache_group
1966 (cp_parser *, enum cpp_ttype, unsigned);
1967 static void cp_parser_parse_tentatively
1969 static void cp_parser_commit_to_tentative_parse
1971 static void cp_parser_abort_tentative_parse
1973 static bool cp_parser_parse_definitely
1975 static inline bool cp_parser_parsing_tentatively
1977 static bool cp_parser_uncommitted_to_tentative_parse_p
1979 static void cp_parser_error
1980 (cp_parser *, const char *);
1981 static void cp_parser_name_lookup_error
1982 (cp_parser *, tree, tree, const char *);
1983 static bool cp_parser_simulate_error
1985 static bool cp_parser_check_type_definition
1987 static void cp_parser_check_for_definition_in_return_type
1988 (cp_declarator *, tree);
1989 static void cp_parser_check_for_invalid_template_id
1990 (cp_parser *, tree);
1991 static bool cp_parser_non_integral_constant_expression
1992 (cp_parser *, const char *);
1993 static void cp_parser_diagnose_invalid_type_name
1994 (cp_parser *, tree, tree);
1995 static bool cp_parser_parse_and_diagnose_invalid_type_name
1997 static int cp_parser_skip_to_closing_parenthesis
1998 (cp_parser *, bool, bool, bool);
1999 static void cp_parser_skip_to_end_of_statement
2001 static void cp_parser_consume_semicolon_at_end_of_statement
2003 static void cp_parser_skip_to_end_of_block_or_statement
2005 static bool cp_parser_skip_to_closing_brace
2007 static void cp_parser_skip_to_end_of_template_parameter_list
2009 static void cp_parser_skip_to_pragma_eol
2010 (cp_parser*, cp_token *);
2011 static bool cp_parser_error_occurred
2013 static bool cp_parser_allow_gnu_extensions_p
2015 static bool cp_parser_is_string_literal
2017 static bool cp_parser_is_keyword
2018 (cp_token *, enum rid);
2019 static tree cp_parser_make_typename_type
2020 (cp_parser *, tree, tree);
2021 static cp_declarator * cp_parser_make_indirect_declarator
2022 (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2024 /* Returns nonzero if we are parsing tentatively. */
2027 cp_parser_parsing_tentatively (cp_parser* parser)
2029 return parser->context->next != NULL;
2032 /* Returns nonzero if TOKEN is a string literal. */
2035 cp_parser_is_string_literal (cp_token* token)
2037 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
2040 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
2043 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2045 return token->keyword == keyword;
2048 /* If not parsing tentatively, issue a diagnostic of the form
2049 FILE:LINE: MESSAGE before TOKEN
2050 where TOKEN is the next token in the input stream. MESSAGE
2051 (specified by the caller) is usually of the form "expected
2055 cp_parser_error (cp_parser* parser, const char* message)
2057 if (!cp_parser_simulate_error (parser))
2059 cp_token *token = cp_lexer_peek_token (parser->lexer);
2060 /* This diagnostic makes more sense if it is tagged to the line
2061 of the token we just peeked at. */
2062 cp_lexer_set_source_position_from_token (token);
2064 if (token->type == CPP_PRAGMA)
2066 error ("%<#pragma%> is not allowed here");
2067 cp_parser_skip_to_pragma_eol (parser, token);
2071 c_parse_error (message,
2072 /* Because c_parser_error does not understand
2073 CPP_KEYWORD, keywords are treated like
2075 (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2080 /* Issue an error about name-lookup failing. NAME is the
2081 IDENTIFIER_NODE DECL is the result of
2082 the lookup (as returned from cp_parser_lookup_name). DESIRED is
2083 the thing that we hoped to find. */
2086 cp_parser_name_lookup_error (cp_parser* parser,
2089 const char* desired)
2091 /* If name lookup completely failed, tell the user that NAME was not
2093 if (decl == error_mark_node)
2095 if (parser->scope && parser->scope != global_namespace)
2096 error ("%<%E::%E%> has not been declared",
2097 parser->scope, name);
2098 else if (parser->scope == global_namespace)
2099 error ("%<::%E%> has not been declared", name);
2100 else if (parser->object_scope
2101 && !CLASS_TYPE_P (parser->object_scope))
2102 error ("request for member %qE in non-class type %qT",
2103 name, parser->object_scope);
2104 else if (parser->object_scope)
2105 error ("%<%T::%E%> has not been declared",
2106 parser->object_scope, name);
2108 error ("%qE has not been declared", name);
2110 else if (parser->scope && parser->scope != global_namespace)
2111 error ("%<%E::%E%> %s", parser->scope, name, desired);
2112 else if (parser->scope == global_namespace)
2113 error ("%<::%E%> %s", name, desired);
2115 error ("%qE %s", name, desired);
2118 /* If we are parsing tentatively, remember that an error has occurred
2119 during this tentative parse. Returns true if the error was
2120 simulated; false if a message should be issued by the caller. */
2123 cp_parser_simulate_error (cp_parser* parser)
2125 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2127 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2133 /* Check for repeated decl-specifiers. */
2136 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs)
2140 for (ds = ds_first; ds != ds_last; ++ds)
2142 unsigned count = decl_specs->specs[(int)ds];
2145 /* The "long" specifier is a special case because of "long long". */
2149 error ("%<long long long%> is too long for GCC");
2150 else if (pedantic && !in_system_header && warn_long_long)
2151 pedwarn ("ISO C++ does not support %<long long%>");
2155 static const char *const decl_spec_names[] = {
2171 error ("duplicate %qs", decl_spec_names[(int)ds]);
2176 /* This function is called when a type is defined. If type
2177 definitions are forbidden at this point, an error message is
2181 cp_parser_check_type_definition (cp_parser* parser)
2183 /* If types are forbidden here, issue a message. */
2184 if (parser->type_definition_forbidden_message)
2186 /* Use `%s' to print the string in case there are any escape
2187 characters in the message. */
2188 error ("%s", parser->type_definition_forbidden_message);
2194 /* This function is called when the DECLARATOR is processed. The TYPE
2195 was a type defined in the decl-specifiers. If it is invalid to
2196 define a type in the decl-specifiers for DECLARATOR, an error is
2200 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2203 /* [dcl.fct] forbids type definitions in return types.
2204 Unfortunately, it's not easy to know whether or not we are
2205 processing a return type until after the fact. */
2207 && (declarator->kind == cdk_pointer
2208 || declarator->kind == cdk_reference
2209 || declarator->kind == cdk_ptrmem))
2210 declarator = declarator->declarator;
2212 && declarator->kind == cdk_function)
2214 error ("new types may not be defined in a return type");
2215 inform ("(perhaps a semicolon is missing after the definition of %qT)",
2220 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2221 "<" in any valid C++ program. If the next token is indeed "<",
2222 issue a message warning the user about what appears to be an
2223 invalid attempt to form a template-id. */
2226 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2229 cp_token_position start = 0;
2231 if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2234 error ("%qT is not a template", type);
2235 else if (TREE_CODE (type) == IDENTIFIER_NODE)
2236 error ("%qE is not a template", type);
2238 error ("invalid template-id");
2239 /* Remember the location of the invalid "<". */
2240 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2241 start = cp_lexer_token_position (parser->lexer, true);
2242 /* Consume the "<". */
2243 cp_lexer_consume_token (parser->lexer);
2244 /* Parse the template arguments. */
2245 cp_parser_enclosed_template_argument_list (parser);
2246 /* Permanently remove the invalid template arguments so that
2247 this error message is not issued again. */
2249 cp_lexer_purge_tokens_after (parser->lexer, start);
2253 /* If parsing an integral constant-expression, issue an error message
2254 about the fact that THING appeared and return true. Otherwise,
2255 return false. In either case, set
2256 PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P. */
2259 cp_parser_non_integral_constant_expression (cp_parser *parser,
2262 parser->non_integral_constant_expression_p = true;
2263 if (parser->integral_constant_expression_p)
2265 if (!parser->allow_non_integral_constant_expression_p)
2267 error ("%s cannot appear in a constant-expression", thing);
2274 /* Emit a diagnostic for an invalid type name. SCOPE is the
2275 qualifying scope (or NULL, if none) for ID. This function commits
2276 to the current active tentative parse, if any. (Otherwise, the
2277 problematic construct might be encountered again later, resulting
2278 in duplicate error messages.) */
2281 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2283 tree decl, old_scope;
2284 /* Try to lookup the identifier. */
2285 old_scope = parser->scope;
2286 parser->scope = scope;
2287 decl = cp_parser_lookup_name_simple (parser, id);
2288 parser->scope = old_scope;
2289 /* If the lookup found a template-name, it means that the user forgot
2290 to specify an argument list. Emit a useful error message. */
2291 if (TREE_CODE (decl) == TEMPLATE_DECL)
2292 error ("invalid use of template-name %qE without an argument list", decl);
2293 else if (TREE_CODE (id) == BIT_NOT_EXPR)
2294 error ("invalid use of destructor %qD as a type", id);
2295 else if (TREE_CODE (decl) == TYPE_DECL)
2296 /* Something like 'unsigned A a;' */
2297 error ("invalid combination of multiple type-specifiers");
2298 else if (!parser->scope)
2300 /* Issue an error message. */
2301 error ("%qE does not name a type", id);
2302 /* If we're in a template class, it's possible that the user was
2303 referring to a type from a base class. For example:
2305 template <typename T> struct A { typedef T X; };
2306 template <typename T> struct B : public A<T> { X x; };
2308 The user should have said "typename A<T>::X". */
2309 if (processing_template_decl && current_class_type
2310 && TYPE_BINFO (current_class_type))
2314 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2318 tree base_type = BINFO_TYPE (b);
2319 if (CLASS_TYPE_P (base_type)
2320 && dependent_type_p (base_type))
2323 /* Go from a particular instantiation of the
2324 template (which will have an empty TYPE_FIELDs),
2325 to the main version. */
2326 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2327 for (field = TYPE_FIELDS (base_type);
2329 field = TREE_CHAIN (field))
2330 if (TREE_CODE (field) == TYPE_DECL
2331 && DECL_NAME (field) == id)
2333 inform ("(perhaps %<typename %T::%E%> was intended)",
2334 BINFO_TYPE (b), id);
2343 /* Here we diagnose qualified-ids where the scope is actually correct,
2344 but the identifier does not resolve to a valid type name. */
2345 else if (parser->scope != error_mark_node)
2347 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2348 error ("%qE in namespace %qE does not name a type",
2350 else if (TYPE_P (parser->scope))
2351 error ("%qE in class %qT does not name a type", id, parser->scope);
2355 cp_parser_commit_to_tentative_parse (parser);
2358 /* Check for a common situation where a type-name should be present,
2359 but is not, and issue a sensible error message. Returns true if an
2360 invalid type-name was detected.
2362 The situation handled by this function are variable declarations of the
2363 form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2364 Usually, `ID' should name a type, but if we got here it means that it
2365 does not. We try to emit the best possible error message depending on
2366 how exactly the id-expression looks like. */
2369 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2373 cp_parser_parse_tentatively (parser);
2374 id = cp_parser_id_expression (parser,
2375 /*template_keyword_p=*/false,
2376 /*check_dependency_p=*/true,
2377 /*template_p=*/NULL,
2378 /*declarator_p=*/true,
2379 /*optional_p=*/false);
2380 /* After the id-expression, there should be a plain identifier,
2381 otherwise this is not a simple variable declaration. Also, if
2382 the scope is dependent, we cannot do much. */
2383 if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2384 || (parser->scope && TYPE_P (parser->scope)
2385 && dependent_type_p (parser->scope))
2386 || TREE_CODE (id) == TYPE_DECL)
2388 cp_parser_abort_tentative_parse (parser);
2391 if (!cp_parser_parse_definitely (parser))
2394 /* Emit a diagnostic for the invalid type. */
2395 cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2396 /* Skip to the end of the declaration; there's no point in
2397 trying to process it. */
2398 cp_parser_skip_to_end_of_block_or_statement (parser);
2402 /* Consume tokens up to, and including, the next non-nested closing `)'.
2403 Returns 1 iff we found a closing `)'. RECOVERING is true, if we
2404 are doing error recovery. Returns -1 if OR_COMMA is true and we
2405 found an unnested comma. */
2408 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2413 unsigned paren_depth = 0;
2414 unsigned brace_depth = 0;
2416 if (recovering && !or_comma
2417 && cp_parser_uncommitted_to_tentative_parse_p (parser))
2422 cp_token * token = cp_lexer_peek_token (parser->lexer);
2424 switch (token->type)
2427 case CPP_PRAGMA_EOL:
2428 /* If we've run out of tokens, then there is no closing `)'. */
2432 /* This matches the processing in skip_to_end_of_statement. */
2437 case CPP_OPEN_BRACE:
2440 case CPP_CLOSE_BRACE:
2446 if (recovering && or_comma && !brace_depth && !paren_depth)
2450 case CPP_OPEN_PAREN:
2455 case CPP_CLOSE_PAREN:
2456 if (!brace_depth && !paren_depth--)
2459 cp_lexer_consume_token (parser->lexer);
2468 /* Consume the token. */
2469 cp_lexer_consume_token (parser->lexer);
2473 /* Consume tokens until we reach the end of the current statement.
2474 Normally, that will be just before consuming a `;'. However, if a
2475 non-nested `}' comes first, then we stop before consuming that. */
2478 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2480 unsigned nesting_depth = 0;
2484 cp_token *token = cp_lexer_peek_token (parser->lexer);
2486 switch (token->type)
2489 case CPP_PRAGMA_EOL:
2490 /* If we've run out of tokens, stop. */
2494 /* If the next token is a `;', we have reached the end of the
2500 case CPP_CLOSE_BRACE:
2501 /* If this is a non-nested '}', stop before consuming it.
2502 That way, when confronted with something like:
2506 we stop before consuming the closing '}', even though we
2507 have not yet reached a `;'. */
2508 if (nesting_depth == 0)
2511 /* If it is the closing '}' for a block that we have
2512 scanned, stop -- but only after consuming the token.
2518 we will stop after the body of the erroneously declared
2519 function, but before consuming the following `typedef'
2521 if (--nesting_depth == 0)
2523 cp_lexer_consume_token (parser->lexer);
2527 case CPP_OPEN_BRACE:
2535 /* Consume the token. */
2536 cp_lexer_consume_token (parser->lexer);
2540 /* This function is called at the end of a statement or declaration.
2541 If the next token is a semicolon, it is consumed; otherwise, error
2542 recovery is attempted. */
2545 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2547 /* Look for the trailing `;'. */
2548 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2550 /* If there is additional (erroneous) input, skip to the end of
2552 cp_parser_skip_to_end_of_statement (parser);
2553 /* If the next token is now a `;', consume it. */
2554 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2555 cp_lexer_consume_token (parser->lexer);
2559 /* Skip tokens until we have consumed an entire block, or until we
2560 have consumed a non-nested `;'. */
2563 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2565 int nesting_depth = 0;
2567 while (nesting_depth >= 0)
2569 cp_token *token = cp_lexer_peek_token (parser->lexer);
2571 switch (token->type)
2574 case CPP_PRAGMA_EOL:
2575 /* If we've run out of tokens, stop. */
2579 /* Stop if this is an unnested ';'. */
2584 case CPP_CLOSE_BRACE:
2585 /* Stop if this is an unnested '}', or closes the outermost
2592 case CPP_OPEN_BRACE:
2601 /* Consume the token. */
2602 cp_lexer_consume_token (parser->lexer);
2606 /* Skip tokens until a non-nested closing curly brace is the next
2607 token, or there are no more tokens. Return true in the first case,
2611 cp_parser_skip_to_closing_brace (cp_parser *parser)
2613 unsigned nesting_depth = 0;
2617 cp_token *token = cp_lexer_peek_token (parser->lexer);
2619 switch (token->type)
2622 case CPP_PRAGMA_EOL:
2623 /* If we've run out of tokens, stop. */
2626 case CPP_CLOSE_BRACE:
2627 /* If the next token is a non-nested `}', then we have reached
2628 the end of the current block. */
2629 if (nesting_depth-- == 0)
2633 case CPP_OPEN_BRACE:
2634 /* If it the next token is a `{', then we are entering a new
2635 block. Consume the entire block. */
2643 /* Consume the token. */
2644 cp_lexer_consume_token (parser->lexer);
2648 /* Consume tokens until we reach the end of the pragma. The PRAGMA_TOK
2649 parameter is the PRAGMA token, allowing us to purge the entire pragma
2653 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2657 parser->lexer->in_pragma = false;
2660 token = cp_lexer_consume_token (parser->lexer);
2661 while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2663 /* Ensure that the pragma is not parsed again. */
2664 cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2667 /* Require pragma end of line, resyncing with it as necessary. The
2668 arguments are as for cp_parser_skip_to_pragma_eol. */
2671 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2673 parser->lexer->in_pragma = false;
2674 if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2675 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2678 /* This is a simple wrapper around make_typename_type. When the id is
2679 an unresolved identifier node, we can provide a superior diagnostic
2680 using cp_parser_diagnose_invalid_type_name. */
2683 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2686 if (TREE_CODE (id) == IDENTIFIER_NODE)
2688 result = make_typename_type (scope, id, typename_type,
2689 /*complain=*/tf_none);
2690 if (result == error_mark_node)
2691 cp_parser_diagnose_invalid_type_name (parser, scope, id);
2694 return make_typename_type (scope, id, typename_type, tf_error);
2697 /* This is a wrapper around the
2698 make_{pointer,ptrmem,reference}_declarator functions that decides
2699 which one to call based on the CODE and CLASS_TYPE arguments. The
2700 CODE argument should be one of the values returned by
2701 cp_parser_ptr_operator. */
2702 static cp_declarator *
2703 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2704 cp_cv_quals cv_qualifiers,
2705 cp_declarator *target)
2707 if (code == ERROR_MARK)
2708 return cp_error_declarator;
2710 if (code == INDIRECT_REF)
2711 if (class_type == NULL_TREE)
2712 return make_pointer_declarator (cv_qualifiers, target);
2714 return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2715 else if (code == ADDR_EXPR && class_type == NULL_TREE)
2716 return make_reference_declarator (cv_qualifiers, target, false);
2717 else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2718 return make_reference_declarator (cv_qualifiers, target, true);
2722 /* Create a new C++ parser. */
2725 cp_parser_new (void)
2731 /* cp_lexer_new_main is called before calling ggc_alloc because
2732 cp_lexer_new_main might load a PCH file. */
2733 lexer = cp_lexer_new_main ();
2735 /* Initialize the binops_by_token so that we can get the tree
2736 directly from the token. */
2737 for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2738 binops_by_token[binops[i].token_type] = binops[i];
2740 parser = GGC_CNEW (cp_parser);
2741 parser->lexer = lexer;
2742 parser->context = cp_parser_context_new (NULL);
2744 /* For now, we always accept GNU extensions. */
2745 parser->allow_gnu_extensions_p = 1;
2747 /* The `>' token is a greater-than operator, not the end of a
2749 parser->greater_than_is_operator_p = true;
2751 parser->default_arg_ok_p = true;
2753 /* We are not parsing a constant-expression. */
2754 parser->integral_constant_expression_p = false;
2755 parser->allow_non_integral_constant_expression_p = false;
2756 parser->non_integral_constant_expression_p = false;
2758 /* Local variable names are not forbidden. */
2759 parser->local_variables_forbidden_p = false;
2761 /* We are not processing an `extern "C"' declaration. */
2762 parser->in_unbraced_linkage_specification_p = false;
2764 /* We are not processing a declarator. */
2765 parser->in_declarator_p = false;
2767 /* We are not processing a template-argument-list. */
2768 parser->in_template_argument_list_p = false;
2770 /* We are not in an iteration statement. */
2771 parser->in_statement = 0;
2773 /* We are not in a switch statement. */
2774 parser->in_switch_statement_p = false;
2776 /* We are not parsing a type-id inside an expression. */
2777 parser->in_type_id_in_expr_p = false;
2779 /* Declarations aren't implicitly extern "C". */
2780 parser->implicit_extern_c = false;
2782 /* String literals should be translated to the execution character set. */
2783 parser->translate_strings_p = true;
2785 /* We are not parsing a function body. */
2786 parser->in_function_body = false;
2788 /* The unparsed function queue is empty. */
2789 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2791 /* There are no classes being defined. */
2792 parser->num_classes_being_defined = 0;
2794 /* No template parameters apply. */
2795 parser->num_template_parameter_lists = 0;
2800 /* Create a cp_lexer structure which will emit the tokens in CACHE
2801 and push it onto the parser's lexer stack. This is used for delayed
2802 parsing of in-class method bodies and default arguments, and should
2803 not be confused with tentative parsing. */
2805 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2807 cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2808 lexer->next = parser->lexer;
2809 parser->lexer = lexer;
2811 /* Move the current source position to that of the first token in the
2813 cp_lexer_set_source_position_from_token (lexer->next_token);
2816 /* Pop the top lexer off the parser stack. This is never used for the
2817 "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens. */
2819 cp_parser_pop_lexer (cp_parser *parser)
2821 cp_lexer *lexer = parser->lexer;
2822 parser->lexer = lexer->next;
2823 cp_lexer_destroy (lexer);
2825 /* Put the current source position back where it was before this
2826 lexer was pushed. */
2827 cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2830 /* Lexical conventions [gram.lex] */
2832 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2836 cp_parser_identifier (cp_parser* parser)
2840 /* Look for the identifier. */
2841 token = cp_parser_require (parser, CPP_NAME, "identifier");
2842 /* Return the value. */
2843 return token ? token->u.value : error_mark_node;
2846 /* Parse a sequence of adjacent string constants. Returns a
2847 TREE_STRING representing the combined, nul-terminated string
2848 constant. If TRANSLATE is true, translate the string to the
2849 execution character set. If WIDE_OK is true, a wide string is
2852 C++98 [lex.string] says that if a narrow string literal token is
2853 adjacent to a wide string literal token, the behavior is undefined.
2854 However, C99 6.4.5p4 says that this results in a wide string literal.
2855 We follow C99 here, for consistency with the C front end.
2857 This code is largely lifted from lex_string() in c-lex.c.
2859 FUTURE: ObjC++ will need to handle @-strings here. */
2861 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2866 struct obstack str_ob;
2867 cpp_string str, istr, *strs;
2870 tok = cp_lexer_peek_token (parser->lexer);
2871 if (!cp_parser_is_string_literal (tok))
2873 cp_parser_error (parser, "expected string-literal");
2874 return error_mark_node;
2877 /* Try to avoid the overhead of creating and destroying an obstack
2878 for the common case of just one string. */
2879 if (!cp_parser_is_string_literal
2880 (cp_lexer_peek_nth_token (parser->lexer, 2)))
2882 cp_lexer_consume_token (parser->lexer);
2884 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2885 str.len = TREE_STRING_LENGTH (tok->u.value);
2887 if (tok->type == CPP_WSTRING)
2894 gcc_obstack_init (&str_ob);
2899 cp_lexer_consume_token (parser->lexer);
2901 str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2902 str.len = TREE_STRING_LENGTH (tok->u.value);
2903 if (tok->type == CPP_WSTRING)
2906 obstack_grow (&str_ob, &str, sizeof (cpp_string));
2908 tok = cp_lexer_peek_token (parser->lexer);
2910 while (cp_parser_is_string_literal (tok));
2912 strs = (cpp_string *) obstack_finish (&str_ob);
2915 if (wide && !wide_ok)
2917 cp_parser_error (parser, "a wide string is invalid in this context");
2921 if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2922 (parse_in, strs, count, &istr, wide))
2924 value = build_string (istr.len, (const char *)istr.text);
2925 free (CONST_CAST (unsigned char *, istr.text));
2927 TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2928 value = fix_string_type (value);
2931 /* cpp_interpret_string has issued an error. */
2932 value = error_mark_node;
2935 obstack_free (&str_ob, 0);
2941 /* Basic concepts [gram.basic] */
2943 /* Parse a translation-unit.
2946 declaration-seq [opt]
2948 Returns TRUE if all went well. */
2951 cp_parser_translation_unit (cp_parser* parser)
2953 /* The address of the first non-permanent object on the declarator
2955 static void *declarator_obstack_base;
2959 /* Create the declarator obstack, if necessary. */
2960 if (!cp_error_declarator)
2962 gcc_obstack_init (&declarator_obstack);
2963 /* Create the error declarator. */
2964 cp_error_declarator = make_declarator (cdk_error);
2965 /* Create the empty parameter list. */
2966 no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2967 /* Remember where the base of the declarator obstack lies. */
2968 declarator_obstack_base = obstack_next_free (&declarator_obstack);
2971 cp_parser_declaration_seq_opt (parser);
2973 /* If there are no tokens left then all went well. */
2974 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2976 /* Get rid of the token array; we don't need it any more. */
2977 cp_lexer_destroy (parser->lexer);
2978 parser->lexer = NULL;
2980 /* This file might have been a context that's implicitly extern
2981 "C". If so, pop the lang context. (Only relevant for PCH.) */
2982 if (parser->implicit_extern_c)
2984 pop_lang_context ();
2985 parser->implicit_extern_c = false;
2989 finish_translation_unit ();
2995 cp_parser_error (parser, "expected declaration");
2999 /* Make sure the declarator obstack was fully cleaned up. */
3000 gcc_assert (obstack_next_free (&declarator_obstack)
3001 == declarator_obstack_base);
3003 /* All went well. */
3007 /* Expressions [gram.expr] */
3009 /* Parse a primary-expression.
3020 ( compound-statement )
3021 __builtin_va_arg ( assignment-expression , type-id )
3022 __builtin_offsetof ( type-id , offsetof-expression )
3025 __has_nothrow_assign ( type-id )
3026 __has_nothrow_constructor ( type-id )
3027 __has_nothrow_copy ( type-id )
3028 __has_trivial_assign ( type-id )
3029 __has_trivial_constructor ( type-id )
3030 __has_trivial_copy ( type-id )
3031 __has_trivial_destructor ( type-id )
3032 __has_virtual_destructor ( type-id )
3033 __is_abstract ( type-id )
3034 __is_base_of ( type-id , type-id )
3035 __is_class ( type-id )
3036 __is_convertible_to ( type-id , type-id )
3037 __is_empty ( type-id )
3038 __is_enum ( type-id )
3039 __is_pod ( type-id )
3040 __is_polymorphic ( type-id )
3041 __is_union ( type-id )
3043 Objective-C++ Extension:
3051 ADDRESS_P is true iff this expression was immediately preceded by
3052 "&" and therefore might denote a pointer-to-member. CAST_P is true
3053 iff this expression is the target of a cast. TEMPLATE_ARG_P is
3054 true iff this expression is a template argument.
3056 Returns a representation of the expression. Upon return, *IDK
3057 indicates what kind of id-expression (if any) was present. */
3060 cp_parser_primary_expression (cp_parser *parser,
3063 bool template_arg_p,
3068 /* Assume the primary expression is not an id-expression. */
3069 *idk = CP_ID_KIND_NONE;
3071 /* Peek at the next token. */
3072 token = cp_lexer_peek_token (parser->lexer);
3073 switch (token->type)
3084 token = cp_lexer_consume_token (parser->lexer);
3085 /* Floating-point literals are only allowed in an integral
3086 constant expression if they are cast to an integral or
3087 enumeration type. */
3088 if (TREE_CODE (token->u.value) == REAL_CST
3089 && parser->integral_constant_expression_p
3092 /* CAST_P will be set even in invalid code like "int(2.7 +
3093 ...)". Therefore, we have to check that the next token
3094 is sure to end the cast. */
3097 cp_token *next_token;
3099 next_token = cp_lexer_peek_token (parser->lexer);
3100 if (/* The comma at the end of an
3101 enumerator-definition. */
3102 next_token->type != CPP_COMMA
3103 /* The curly brace at the end of an enum-specifier. */
3104 && next_token->type != CPP_CLOSE_BRACE
3105 /* The end of a statement. */
3106 && next_token->type != CPP_SEMICOLON
3107 /* The end of the cast-expression. */
3108 && next_token->type != CPP_CLOSE_PAREN
3109 /* The end of an array bound. */
3110 && next_token->type != CPP_CLOSE_SQUARE
3111 /* The closing ">" in a template-argument-list. */
3112 && (next_token->type != CPP_GREATER
3113 || parser->greater_than_is_operator_p)
3114 /* C++0x only: A ">>" treated like two ">" tokens,
3115 in a template-argument-list. */
3116 && (next_token->type != CPP_RSHIFT
3117 || (cxx_dialect == cxx98)
3118 || parser->greater_than_is_operator_p))
3122 /* If we are within a cast, then the constraint that the
3123 cast is to an integral or enumeration type will be
3124 checked at that point. If we are not within a cast, then
3125 this code is invalid. */
3127 cp_parser_non_integral_constant_expression
3128 (parser, "floating-point literal");
3130 return token->u.value;
3134 /* ??? Should wide strings be allowed when parser->translate_strings_p
3135 is false (i.e. in attributes)? If not, we can kill the third
3136 argument to cp_parser_string_literal. */
3137 return cp_parser_string_literal (parser,
3138 parser->translate_strings_p,
3141 case CPP_OPEN_PAREN:
3144 bool saved_greater_than_is_operator_p;
3146 /* Consume the `('. */
3147 cp_lexer_consume_token (parser->lexer);
3148 /* Within a parenthesized expression, a `>' token is always
3149 the greater-than operator. */
3150 saved_greater_than_is_operator_p
3151 = parser->greater_than_is_operator_p;
3152 parser->greater_than_is_operator_p = true;
3153 /* If we see `( { ' then we are looking at the beginning of
3154 a GNU statement-expression. */
3155 if (cp_parser_allow_gnu_extensions_p (parser)
3156 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3158 /* Statement-expressions are not allowed by the standard. */
3160 pedwarn ("ISO C++ forbids braced-groups within expressions");
3162 /* And they're not allowed outside of a function-body; you
3163 cannot, for example, write:
3165 int i = ({ int j = 3; j + 1; });
3167 at class or namespace scope. */
3168 if (!parser->in_function_body
3169 || parser->in_template_argument_list_p)
3171 error ("statement-expressions are not allowed outside "
3172 "functions nor in template-argument lists");
3173 cp_parser_skip_to_end_of_block_or_statement (parser);
3174 expr = error_mark_node;
3178 /* Start the statement-expression. */
3179 expr = begin_stmt_expr ();
3180 /* Parse the compound-statement. */
3181 cp_parser_compound_statement (parser, expr, false);
3183 expr = finish_stmt_expr (expr, false);
3188 /* Parse the parenthesized expression. */
3189 expr = cp_parser_expression (parser, cast_p);
3190 /* Let the front end know that this expression was
3191 enclosed in parentheses. This matters in case, for
3192 example, the expression is of the form `A::B', since
3193 `&A::B' might be a pointer-to-member, but `&(A::B)' is
3195 finish_parenthesized_expr (expr);
3197 /* The `>' token might be the end of a template-id or
3198 template-parameter-list now. */
3199 parser->greater_than_is_operator_p
3200 = saved_greater_than_is_operator_p;
3201 /* Consume the `)'. */
3202 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
3203 cp_parser_skip_to_end_of_statement (parser);
3209 switch (token->keyword)
3211 /* These two are the boolean literals. */
3213 cp_lexer_consume_token (parser->lexer);
3214 return boolean_true_node;
3216 cp_lexer_consume_token (parser->lexer);
3217 return boolean_false_node;
3219 /* The `__null' literal. */
3221 cp_lexer_consume_token (parser->lexer);
3224 /* Recognize the `this' keyword. */
3226 cp_lexer_consume_token (parser->lexer);
3227 if (parser->local_variables_forbidden_p)
3229 error ("%<this%> may not be used in this context");
3230 return error_mark_node;
3232 /* Pointers cannot appear in constant-expressions. */
3233 if (cp_parser_non_integral_constant_expression (parser,
3235 return error_mark_node;
3236 return finish_this_expr ();
3238 /* The `operator' keyword can be the beginning of an
3243 case RID_FUNCTION_NAME:
3244 case RID_PRETTY_FUNCTION_NAME:
3245 case RID_C99_FUNCTION_NAME:
3246 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3247 __func__ are the names of variables -- but they are
3248 treated specially. Therefore, they are handled here,
3249 rather than relying on the generic id-expression logic
3250 below. Grammatically, these names are id-expressions.
3252 Consume the token. */
3253 token = cp_lexer_consume_token (parser->lexer);
3254 /* Look up the name. */
3255 return finish_fname (token->u.value);
3262 /* The `__builtin_va_arg' construct is used to handle
3263 `va_arg'. Consume the `__builtin_va_arg' token. */
3264 cp_lexer_consume_token (parser->lexer);
3265 /* Look for the opening `('. */
3266 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3267 /* Now, parse the assignment-expression. */
3268 expression = cp_parser_assignment_expression (parser,
3270 /* Look for the `,'. */
3271 cp_parser_require (parser, CPP_COMMA, "`,'");
3272 /* Parse the type-id. */
3273 type = cp_parser_type_id (parser);
3274 /* Look for the closing `)'. */
3275 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3276 /* Using `va_arg' in a constant-expression is not
3278 if (cp_parser_non_integral_constant_expression (parser,
3280 return error_mark_node;
3281 return build_x_va_arg (expression, type);
3285 return cp_parser_builtin_offsetof (parser);
3287 case RID_HAS_NOTHROW_ASSIGN:
3288 case RID_HAS_NOTHROW_CONSTRUCTOR:
3289 case RID_HAS_NOTHROW_COPY:
3290 case RID_HAS_TRIVIAL_ASSIGN:
3291 case RID_HAS_TRIVIAL_CONSTRUCTOR:
3292 case RID_HAS_TRIVIAL_COPY:
3293 case RID_HAS_TRIVIAL_DESTRUCTOR:
3294 case RID_HAS_VIRTUAL_DESTRUCTOR:
3295 case RID_IS_ABSTRACT:
3296 case RID_IS_BASE_OF:
3298 case RID_IS_CONVERTIBLE_TO:
3302 case RID_IS_POLYMORPHIC:
3304 return cp_parser_trait_expr (parser, token->keyword);
3306 /* Objective-C++ expressions. */
3308 case RID_AT_PROTOCOL:
3309 case RID_AT_SELECTOR:
3310 return cp_parser_objc_expression (parser);
3313 cp_parser_error (parser, "expected primary-expression");
3314 return error_mark_node;
3317 /* An id-expression can start with either an identifier, a
3318 `::' as the beginning of a qualified-id, or the "operator"
3322 case CPP_TEMPLATE_ID:
3323 case CPP_NESTED_NAME_SPECIFIER:
3327 const char *error_msg;
3332 /* Parse the id-expression. */
3334 = cp_parser_id_expression (parser,
3335 /*template_keyword_p=*/false,
3336 /*check_dependency_p=*/true,
3338 /*declarator_p=*/false,
3339 /*optional_p=*/false);
3340 if (id_expression == error_mark_node)
3341 return error_mark_node;
3342 token = cp_lexer_peek_token (parser->lexer);
3343 done = (token->type != CPP_OPEN_SQUARE
3344 && token->type != CPP_OPEN_PAREN
3345 && token->type != CPP_DOT
3346 && token->type != CPP_DEREF
3347 && token->type != CPP_PLUS_PLUS
3348 && token->type != CPP_MINUS_MINUS);
3349 /* If we have a template-id, then no further lookup is
3350 required. If the template-id was for a template-class, we
3351 will sometimes have a TYPE_DECL at this point. */
3352 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3353 || TREE_CODE (id_expression) == TYPE_DECL)
3354 decl = id_expression;
3355 /* Look up the name. */
3358 tree ambiguous_decls;
3360 decl = cp_parser_lookup_name (parser, id_expression,
3363 /*is_namespace=*/false,
3364 /*check_dependency=*/true,
3366 /* If the lookup was ambiguous, an error will already have
3368 if (ambiguous_decls)
3369 return error_mark_node;
3371 /* In Objective-C++, an instance variable (ivar) may be preferred
3372 to whatever cp_parser_lookup_name() found. */
3373 decl = objc_lookup_ivar (decl, id_expression);
3375 /* If name lookup gives us a SCOPE_REF, then the
3376 qualifying scope was dependent. */
3377 if (TREE_CODE (decl) == SCOPE_REF)
3379 /* At this point, we do not know if DECL is a valid
3380 integral constant expression. We assume that it is
3381 in fact such an expression, so that code like:
3383 template <int N> struct A {
3387 is accepted. At template-instantiation time, we
3388 will check that B<N>::i is actually a constant. */
3391 /* Check to see if DECL is a local variable in a context
3392 where that is forbidden. */
3393 if (parser->local_variables_forbidden_p
3394 && local_variable_p (decl))
3396 /* It might be that we only found DECL because we are
3397 trying to be generous with pre-ISO scoping rules.
3398 For example, consider:
3402 for (int i = 0; i < 10; ++i) {}
3403 extern void f(int j = i);
3406 Here, name look up will originally find the out
3407 of scope `i'. We need to issue a warning message,
3408 but then use the global `i'. */
3409 decl = check_for_out_of_scope_variable (decl);
3410 if (local_variable_p (decl))
3412 error ("local variable %qD may not appear in this context",
3414 return error_mark_node;
3419 decl = (finish_id_expression
3420 (id_expression, decl, parser->scope,
3422 parser->integral_constant_expression_p,
3423 parser->allow_non_integral_constant_expression_p,
3424 &parser->non_integral_constant_expression_p,
3425 template_p, done, address_p,
3429 cp_parser_error (parser, error_msg);
3433 /* Anything else is an error. */
3435 /* ...unless we have an Objective-C++ message or string literal,
3437 if (c_dialect_objc ()
3438 && (token->type == CPP_OPEN_SQUARE
3439 || token->type == CPP_OBJC_STRING))
3440 return cp_parser_objc_expression (parser);
3442 cp_parser_error (parser, "expected primary-expression");
3443 return error_mark_node;
3447 /* Parse an id-expression.
3454 :: [opt] nested-name-specifier template [opt] unqualified-id
3456 :: operator-function-id
3459 Return a representation of the unqualified portion of the
3460 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
3461 a `::' or nested-name-specifier.
3463 Often, if the id-expression was a qualified-id, the caller will
3464 want to make a SCOPE_REF to represent the qualified-id. This
3465 function does not do this in order to avoid wastefully creating
3466 SCOPE_REFs when they are not required.
3468 If TEMPLATE_KEYWORD_P is true, then we have just seen the
3471 If CHECK_DEPENDENCY_P is false, then names are looked up inside
3472 uninstantiated templates.
3474 If *TEMPLATE_P is non-NULL, it is set to true iff the
3475 `template' keyword is used to explicitly indicate that the entity
3476 named is a template.
3478 If DECLARATOR_P is true, the id-expression is appearing as part of
3479 a declarator, rather than as part of an expression. */
3482 cp_parser_id_expression (cp_parser *parser,
3483 bool template_keyword_p,
3484 bool check_dependency_p,
3489 bool global_scope_p;
3490 bool nested_name_specifier_p;
3492 /* Assume the `template' keyword was not used. */
3494 *template_p = template_keyword_p;
3496 /* Look for the optional `::' operator. */
3498 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3500 /* Look for the optional nested-name-specifier. */
3501 nested_name_specifier_p
3502 = (cp_parser_nested_name_specifier_opt (parser,
3503 /*typename_keyword_p=*/false,
3508 /* If there is a nested-name-specifier, then we are looking at
3509 the first qualified-id production. */
3510 if (nested_name_specifier_p)
3513 tree saved_object_scope;
3514 tree saved_qualifying_scope;
3515 tree unqualified_id;
3518 /* See if the next token is the `template' keyword. */
3520 template_p = &is_template;
3521 *template_p = cp_parser_optional_template_keyword (parser);
3522 /* Name lookup we do during the processing of the
3523 unqualified-id might obliterate SCOPE. */
3524 saved_scope = parser->scope;
3525 saved_object_scope = parser->object_scope;
3526 saved_qualifying_scope = parser->qualifying_scope;
3527 /* Process the final unqualified-id. */
3528 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3531 /*optional_p=*/false);
3532 /* Restore the SAVED_SCOPE for our caller. */
3533 parser->scope = saved_scope;
3534 parser->object_scope = saved_object_scope;
3535 parser->qualifying_scope = saved_qualifying_scope;
3537 return unqualified_id;
3539 /* Otherwise, if we are in global scope, then we are looking at one
3540 of the other qualified-id productions. */
3541 else if (global_scope_p)
3546 /* Peek at the next token. */
3547 token = cp_lexer_peek_token (parser->lexer);
3549 /* If it's an identifier, and the next token is not a "<", then
3550 we can avoid the template-id case. This is an optimization
3551 for this common case. */
3552 if (token->type == CPP_NAME
3553 && !cp_parser_nth_token_starts_template_argument_list_p
3555 return cp_parser_identifier (parser);
3557 cp_parser_parse_tentatively (parser);
3558 /* Try a template-id. */
3559 id = cp_parser_template_id (parser,
3560 /*template_keyword_p=*/false,
3561 /*check_dependency_p=*/true,
3563 /* If that worked, we're done. */
3564 if (cp_parser_parse_definitely (parser))
3567 /* Peek at the next token. (Changes in the token buffer may
3568 have invalidated the pointer obtained above.) */
3569 token = cp_lexer_peek_token (parser->lexer);
3571 switch (token->type)
3574 return cp_parser_identifier (parser);
3577 if (token->keyword == RID_OPERATOR)
3578 return cp_parser_operator_function_id (parser);
3582 cp_parser_error (parser, "expected id-expression");
3583 return error_mark_node;
3587 return cp_parser_unqualified_id (parser, template_keyword_p,
3588 /*check_dependency_p=*/true,
3593 /* Parse an unqualified-id.
3597 operator-function-id
3598 conversion-function-id
3602 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3603 keyword, in a construct like `A::template ...'.
3605 Returns a representation of unqualified-id. For the `identifier'
3606 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
3607 production a BIT_NOT_EXPR is returned; the operand of the
3608 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
3609 other productions, see the documentation accompanying the
3610 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
3611 names are looked up in uninstantiated templates. If DECLARATOR_P
3612 is true, the unqualified-id is appearing as part of a declarator,
3613 rather than as part of an expression. */
3616 cp_parser_unqualified_id (cp_parser* parser,
3617 bool template_keyword_p,
3618 bool check_dependency_p,
3624 /* Peek at the next token. */
3625 token = cp_lexer_peek_token (parser->lexer);
3627 switch (token->type)
3633 /* We don't know yet whether or not this will be a
3635 cp_parser_parse_tentatively (parser);
3636 /* Try a template-id. */
3637 id = cp_parser_template_id (parser, template_keyword_p,
3640 /* If it worked, we're done. */
3641 if (cp_parser_parse_definitely (parser))
3643 /* Otherwise, it's an ordinary identifier. */
3644 return cp_parser_identifier (parser);
3647 case CPP_TEMPLATE_ID:
3648 return cp_parser_template_id (parser, template_keyword_p,
3655 tree qualifying_scope;
3660 /* Consume the `~' token. */
3661 cp_lexer_consume_token (parser->lexer);
3662 /* Parse the class-name. The standard, as written, seems to
3665 template <typename T> struct S { ~S (); };
3666 template <typename T> S<T>::~S() {}
3668 is invalid, since `~' must be followed by a class-name, but
3669 `S<T>' is dependent, and so not known to be a class.
3670 That's not right; we need to look in uninstantiated
3671 templates. A further complication arises from:
3673 template <typename T> void f(T t) {
3677 Here, it is not possible to look up `T' in the scope of `T'
3678 itself. We must look in both the current scope, and the
3679 scope of the containing complete expression.
3681 Yet another issue is:
3690 The standard does not seem to say that the `S' in `~S'
3691 should refer to the type `S' and not the data member
3694 /* DR 244 says that we look up the name after the "~" in the
3695 same scope as we looked up the qualifying name. That idea
3696 isn't fully worked out; it's more complicated than that. */
3697 scope = parser->scope;
3698 object_scope = parser->object_scope;
3699 qualifying_scope = parser->qualifying_scope;
3701 /* Check for invalid scopes. */
3702 if (scope == error_mark_node)
3704 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3705 cp_lexer_consume_token (parser->lexer);
3706 return error_mark_node;
3708 if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3710 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3711 error ("scope %qT before %<~%> is not a class-name", scope);
3712 cp_parser_simulate_error (parser);
3713 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3714 cp_lexer_consume_token (parser->lexer);
3715 return error_mark_node;
3717 gcc_assert (!scope || TYPE_P (scope));
3719 /* If the name is of the form "X::~X" it's OK. */
3720 token = cp_lexer_peek_token (parser->lexer);
3722 && token->type == CPP_NAME
3723 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3725 && constructor_name_p (token->u.value, scope))
3727 cp_lexer_consume_token (parser->lexer);
3728 return build_nt (BIT_NOT_EXPR, scope);
3731 /* If there was an explicit qualification (S::~T), first look
3732 in the scope given by the qualification (i.e., S). */
3734 type_decl = NULL_TREE;
3737 cp_parser_parse_tentatively (parser);
3738 type_decl = cp_parser_class_name (parser,
3739 /*typename_keyword_p=*/false,
3740 /*template_keyword_p=*/false,
3742 /*check_dependency=*/false,
3743 /*class_head_p=*/false,
3745 if (cp_parser_parse_definitely (parser))
3748 /* In "N::S::~S", look in "N" as well. */
3749 if (!done && scope && qualifying_scope)
3751 cp_parser_parse_tentatively (parser);
3752 parser->scope = qualifying_scope;
3753 parser->object_scope = NULL_TREE;
3754 parser->qualifying_scope = NULL_TREE;
3756 = cp_parser_class_name (parser,
3757 /*typename_keyword_p=*/false,
3758 /*template_keyword_p=*/false,
3760 /*check_dependency=*/false,
3761 /*class_head_p=*/false,
3763 if (cp_parser_parse_definitely (parser))
3766 /* In "p->S::~T", look in the scope given by "*p" as well. */
3767 else if (!done && object_scope)
3769 cp_parser_parse_tentatively (parser);
3770 parser->scope = object_scope;
3771 parser->object_scope = NULL_TREE;
3772 parser->qualifying_scope = NULL_TREE;
3774 = cp_parser_class_name (parser,
3775 /*typename_keyword_p=*/false,
3776 /*template_keyword_p=*/false,
3778 /*check_dependency=*/false,
3779 /*class_head_p=*/false,
3781 if (cp_parser_parse_definitely (parser))
3784 /* Look in the surrounding context. */
3787 parser->scope = NULL_TREE;
3788 parser->object_scope = NULL_TREE;
3789 parser->qualifying_scope = NULL_TREE;
3791 = cp_parser_class_name (parser,
3792 /*typename_keyword_p=*/false,
3793 /*template_keyword_p=*/false,
3795 /*check_dependency=*/false,
3796 /*class_head_p=*/false,
3799 /* If an error occurred, assume that the name of the
3800 destructor is the same as the name of the qualifying
3801 class. That allows us to keep parsing after running
3802 into ill-formed destructor names. */
3803 if (type_decl == error_mark_node && scope)
3804 return build_nt (BIT_NOT_EXPR, scope);
3805 else if (type_decl == error_mark_node)
3806 return error_mark_node;
3808 /* Check that destructor name and scope match. */
3809 if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3811 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3812 error ("declaration of %<~%T%> as member of %qT",
3814 cp_parser_simulate_error (parser);
3815 return error_mark_node;
3820 A typedef-name that names a class shall not be used as the
3821 identifier in the declarator for a destructor declaration. */
3823 && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3824 && !DECL_SELF_REFERENCE_P (type_decl)
3825 && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3826 error ("typedef-name %qD used as destructor declarator",
3829 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3833 if (token->keyword == RID_OPERATOR)
3837 /* This could be a template-id, so we try that first. */
3838 cp_parser_parse_tentatively (parser);
3839 /* Try a template-id. */
3840 id = cp_parser_template_id (parser, template_keyword_p,
3841 /*check_dependency_p=*/true,
3843 /* If that worked, we're done. */
3844 if (cp_parser_parse_definitely (parser))
3846 /* We still don't know whether we're looking at an
3847 operator-function-id or a conversion-function-id. */
3848 cp_parser_parse_tentatively (parser);
3849 /* Try an operator-function-id. */
3850 id = cp_parser_operator_function_id (parser);
3851 /* If that didn't work, try a conversion-function-id. */
3852 if (!cp_parser_parse_definitely (parser))
3853 id = cp_parser_conversion_function_id (parser);
3862 cp_parser_error (parser, "expected unqualified-id");
3863 return error_mark_node;
3867 /* Parse an (optional) nested-name-specifier.
3869 nested-name-specifier:
3870 class-or-namespace-name :: nested-name-specifier [opt]
3871 class-or-namespace-name :: template nested-name-specifier [opt]
3873 PARSER->SCOPE should be set appropriately before this function is
3874 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3875 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3878 Sets PARSER->SCOPE to the class (TYPE) or namespace
3879 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3880 it unchanged if there is no nested-name-specifier. Returns the new
3881 scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3883 If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3884 part of a declaration and/or decl-specifier. */
3887 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3888 bool typename_keyword_p,
3889 bool check_dependency_p,
3891 bool is_declaration)
3893 bool success = false;
3894 cp_token_position start = 0;
3897 /* Remember where the nested-name-specifier starts. */
3898 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3900 start = cp_lexer_token_position (parser->lexer, false);
3901 push_deferring_access_checks (dk_deferred);
3908 tree saved_qualifying_scope;
3909 bool template_keyword_p;
3911 /* Spot cases that cannot be the beginning of a
3912 nested-name-specifier. */
3913 token = cp_lexer_peek_token (parser->lexer);
3915 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3916 the already parsed nested-name-specifier. */
3917 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3919 /* Grab the nested-name-specifier and continue the loop. */
3920 cp_parser_pre_parsed_nested_name_specifier (parser);
3921 /* If we originally encountered this nested-name-specifier
3922 with IS_DECLARATION set to false, we will not have
3923 resolved TYPENAME_TYPEs, so we must do so here. */
3925 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3927 new_scope = resolve_typename_type (parser->scope,
3928 /*only_current_p=*/false);
3929 if (TREE_CODE (new_scope) != TYPENAME_TYPE)
3930 parser->scope = new_scope;
3936 /* Spot cases that cannot be the beginning of a
3937 nested-name-specifier. On the second and subsequent times
3938 through the loop, we look for the `template' keyword. */
3939 if (success && token->keyword == RID_TEMPLATE)
3941 /* A template-id can start a nested-name-specifier. */
3942 else if (token->type == CPP_TEMPLATE_ID)
3946 /* If the next token is not an identifier, then it is
3947 definitely not a class-or-namespace-name. */
3948 if (token->type != CPP_NAME)
3950 /* If the following token is neither a `<' (to begin a
3951 template-id), nor a `::', then we are not looking at a
3952 nested-name-specifier. */
3953 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3954 if (token->type != CPP_SCOPE
3955 && !cp_parser_nth_token_starts_template_argument_list_p
3960 /* The nested-name-specifier is optional, so we parse
3962 cp_parser_parse_tentatively (parser);
3964 /* Look for the optional `template' keyword, if this isn't the
3965 first time through the loop. */
3967 template_keyword_p = cp_parser_optional_template_keyword (parser);
3969 template_keyword_p = false;
3971 /* Save the old scope since the name lookup we are about to do
3972 might destroy it. */
3973 old_scope = parser->scope;
3974 saved_qualifying_scope = parser->qualifying_scope;
3975 /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3976 look up names in "X<T>::I" in order to determine that "Y" is
3977 a template. So, if we have a typename at this point, we make
3978 an effort to look through it. */
3980 && !typename_keyword_p
3982 && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3983 parser->scope = resolve_typename_type (parser->scope,
3984 /*only_current_p=*/false);
3985 /* Parse the qualifying entity. */
3987 = cp_parser_class_or_namespace_name (parser,
3993 /* Look for the `::' token. */
3994 cp_parser_require (parser, CPP_SCOPE, "`::'");
3996 /* If we found what we wanted, we keep going; otherwise, we're
3998 if (!cp_parser_parse_definitely (parser))
4000 bool error_p = false;
4002 /* Restore the OLD_SCOPE since it was valid before the
4003 failed attempt at finding the last
4004 class-or-namespace-name. */
4005 parser->scope = old_scope;
4006 parser->qualifying_scope = saved_qualifying_scope;
4007 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4009 /* If the next token is an identifier, and the one after
4010 that is a `::', then any valid interpretation would have
4011 found a class-or-namespace-name. */
4012 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4013 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4015 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4018 token = cp_lexer_consume_token (parser->lexer);
4021 if (!token->ambiguous_p)
4024 tree ambiguous_decls;
4026 decl = cp_parser_lookup_name (parser, token->u.value,
4028 /*is_template=*/false,
4029 /*is_namespace=*/false,
4030 /*check_dependency=*/true,
4032 if (TREE_CODE (decl) == TEMPLATE_DECL)
4033 error ("%qD used without template parameters", decl);
4034 else if (ambiguous_decls)
4036 error ("reference to %qD is ambiguous",
4038 print_candidates (ambiguous_decls);
4039 decl = error_mark_node;
4042 cp_parser_name_lookup_error
4043 (parser, token->u.value, decl,
4044 "is not a class or namespace");
4046 parser->scope = error_mark_node;
4048 /* Treat this as a successful nested-name-specifier
4053 If the name found is not a class-name (clause
4054 _class_) or namespace-name (_namespace.def_), the
4055 program is ill-formed. */
4058 cp_lexer_consume_token (parser->lexer);
4062 /* We've found one valid nested-name-specifier. */
4064 /* Name lookup always gives us a DECL. */
4065 if (TREE_CODE (new_scope) == TYPE_DECL)
4066 new_scope = TREE_TYPE (new_scope);
4067 /* Uses of "template" must be followed by actual templates. */
4068 if (template_keyword_p
4069 && !(CLASS_TYPE_P (new_scope)
4070 && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4071 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4072 || CLASSTYPE_IS_TEMPLATE (new_scope)))
4073 && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4074 && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4075 == TEMPLATE_ID_EXPR)))
4076 pedwarn (TYPE_P (new_scope)
4077 ? "%qT is not a template"
4078 : "%qD is not a template",
4080 /* If it is a class scope, try to complete it; we are about to
4081 be looking up names inside the class. */
4082 if (TYPE_P (new_scope)
4083 /* Since checking types for dependency can be expensive,
4084 avoid doing it if the type is already complete. */
4085 && !COMPLETE_TYPE_P (new_scope)
4086 /* Do not try to complete dependent types. */
4087 && !dependent_type_p (new_scope))
4088 new_scope = complete_type (new_scope);
4089 /* Make sure we look in the right scope the next time through
4091 parser->scope = new_scope;
4094 /* If parsing tentatively, replace the sequence of tokens that makes
4095 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4096 token. That way, should we re-parse the token stream, we will
4097 not have to repeat the effort required to do the parse, nor will
4098 we issue duplicate error messages. */
4099 if (success && start)
4103 token = cp_lexer_token_at (parser->lexer, start);
4104 /* Reset the contents of the START token. */
4105 token->type = CPP_NESTED_NAME_SPECIFIER;
4106 /* Retrieve any deferred checks. Do not pop this access checks yet
4107 so the memory will not be reclaimed during token replacing below. */
4108 token->u.tree_check_value = GGC_CNEW (struct tree_check);
4109 token->u.tree_check_value->value = parser->scope;
4110 token->u.tree_check_value->checks = get_deferred_access_checks ();
4111 token->u.tree_check_value->qualifying_scope =
4112 parser->qualifying_scope;
4113 token->keyword = RID_MAX;
4115 /* Purge all subsequent tokens. */
4116 cp_lexer_purge_tokens_after (parser->lexer, start);
4120 pop_to_parent_deferring_access_checks ();
4122 return success ? parser->scope : NULL_TREE;
4125 /* Parse a nested-name-specifier. See
4126 cp_parser_nested_name_specifier_opt for details. This function
4127 behaves identically, except that it will an issue an error if no
4128 nested-name-specifier is present. */
4131 cp_parser_nested_name_specifier (cp_parser *parser,
4132 bool typename_keyword_p,
4133 bool check_dependency_p,
4135 bool is_declaration)
4139 /* Look for the nested-name-specifier. */
4140 scope = cp_parser_nested_name_specifier_opt (parser,
4145 /* If it was not present, issue an error message. */
4148 cp_parser_error (parser, "expected nested-name-specifier");
4149 parser->scope = NULL_TREE;
4155 /* Parse a class-or-namespace-name.
4157 class-or-namespace-name:
4161 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4162 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4163 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4164 TYPE_P is TRUE iff the next name should be taken as a class-name,
4165 even the same name is declared to be another entity in the same
4168 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4169 specified by the class-or-namespace-name. If neither is found the
4170 ERROR_MARK_NODE is returned. */
4173 cp_parser_class_or_namespace_name (cp_parser *parser,
4174 bool typename_keyword_p,
4175 bool template_keyword_p,
4176 bool check_dependency_p,
4178 bool is_declaration)
4181 tree saved_qualifying_scope;
4182 tree saved_object_scope;
4186 /* Before we try to parse the class-name, we must save away the
4187 current PARSER->SCOPE since cp_parser_class_name will destroy
4189 saved_scope = parser->scope;
4190 saved_qualifying_scope = parser->qualifying_scope;
4191 saved_object_scope = parser->object_scope;
4192 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
4193 there is no need to look for a namespace-name. */
4194 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
4196 cp_parser_parse_tentatively (parser);
4197 scope = cp_parser_class_name (parser,
4200 type_p ? class_type : none_type,
4202 /*class_head_p=*/false,
4204 /* If that didn't work, try for a namespace-name. */
4205 if (!only_class_p && !cp_parser_parse_definitely (parser))
4207 /* Restore the saved scope. */
4208 parser->scope = saved_scope;
4209 parser->qualifying_scope = saved_qualifying_scope;
4210 parser->object_scope = saved_object_scope;
4211 /* If we are not looking at an identifier followed by the scope
4212 resolution operator, then this is not part of a
4213 nested-name-specifier. (Note that this function is only used
4214 to parse the components of a nested-name-specifier.) */
4215 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4216 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4217 return error_mark_node;
4218 scope = cp_parser_namespace_name (parser);
4224 /* Parse a postfix-expression.
4228 postfix-expression [ expression ]
4229 postfix-expression ( expression-list [opt] )
4230 simple-type-specifier ( expression-list [opt] )
4231 typename :: [opt] nested-name-specifier identifier
4232 ( expression-list [opt] )
4233 typename :: [opt] nested-name-specifier template [opt] template-id
4234 ( expression-list [opt] )
4235 postfix-expression . template [opt] id-expression
4236 postfix-expression -> template [opt] id-expression
4237 postfix-expression . pseudo-destructor-name
4238 postfix-expression -> pseudo-destructor-name
4239 postfix-expression ++
4240 postfix-expression --
4241 dynamic_cast < type-id > ( expression )
4242 static_cast < type-id > ( expression )
4243 reinterpret_cast < type-id > ( expression )
4244 const_cast < type-id > ( expression )
4245 typeid ( expression )
4251 ( type-id ) { initializer-list , [opt] }
4253 This extension is a GNU version of the C99 compound-literal
4254 construct. (The C99 grammar uses `type-name' instead of `type-id',
4255 but they are essentially the same concept.)
4257 If ADDRESS_P is true, the postfix expression is the operand of the
4258 `&' operator. CAST_P is true if this expression is the target of a
4261 If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4262 class member access expressions [expr.ref].
4264 Returns a representation of the expression. */
4267 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4268 bool member_access_only_p)
4272 cp_id_kind idk = CP_ID_KIND_NONE;
4273 tree postfix_expression = NULL_TREE;
4274 bool is_member_access = false;
4276 /* Peek at the next token. */
4277 token = cp_lexer_peek_token (parser->lexer);
4278 /* Some of the productions are determined by keywords. */
4279 keyword = token->keyword;
4289 const char *saved_message;
4291 /* All of these can be handled in the same way from the point
4292 of view of parsing. Begin by consuming the token
4293 identifying the cast. */
4294 cp_lexer_consume_token (parser->lexer);
4296 /* New types cannot be defined in the cast. */
4297 saved_message = parser->type_definition_forbidden_message;
4298 parser->type_definition_forbidden_message
4299 = "types may not be defined in casts";
4301 /* Look for the opening `<'. */
4302 cp_parser_require (parser, CPP_LESS, "`<'");
4303 /* Parse the type to which we are casting. */
4304 type = cp_parser_type_id (parser);
4305 /* Look for the closing `>'. */
4306 cp_parser_require (parser, CPP_GREATER, "`>'");
4307 /* Restore the old message. */
4308 parser->type_definition_forbidden_message = saved_message;
4310 /* And the expression which is being cast. */
4311 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4312 expression = cp_parser_expression (parser, /*cast_p=*/true);
4313 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4315 /* Only type conversions to integral or enumeration types
4316 can be used in constant-expressions. */
4317 if (!cast_valid_in_integral_constant_expression_p (type)
4318 && (cp_parser_non_integral_constant_expression
4320 "a cast to a type other than an integral or "
4321 "enumeration type")))
4322 return error_mark_node;
4328 = build_dynamic_cast (type, expression);
4332 = build_static_cast (type, expression);
4336 = build_reinterpret_cast (type, expression);
4340 = build_const_cast (type, expression);
4351 const char *saved_message;
4352 bool saved_in_type_id_in_expr_p;
4354 /* Consume the `typeid' token. */
4355 cp_lexer_consume_token (parser->lexer);
4356 /* Look for the `(' token. */
4357 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4358 /* Types cannot be defined in a `typeid' expression. */
4359 saved_message = parser->type_definition_forbidden_message;
4360 parser->type_definition_forbidden_message
4361 = "types may not be defined in a `typeid\' expression";
4362 /* We can't be sure yet whether we're looking at a type-id or an
4364 cp_parser_parse_tentatively (parser);
4365 /* Try a type-id first. */
4366 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4367 parser->in_type_id_in_expr_p = true;
4368 type = cp_parser_type_id (parser);
4369 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4370 /* Look for the `)' token. Otherwise, we can't be sure that
4371 we're not looking at an expression: consider `typeid (int
4372 (3))', for example. */
4373 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4374 /* If all went well, simply lookup the type-id. */
4375 if (cp_parser_parse_definitely (parser))
4376 postfix_expression = get_typeid (type);
4377 /* Otherwise, fall back to the expression variant. */
4382 /* Look for an expression. */
4383 expression = cp_parser_expression (parser, /*cast_p=*/false);
4384 /* Compute its typeid. */
4385 postfix_expression = build_typeid (expression);
4386 /* Look for the `)' token. */
4387 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4389 /* Restore the saved message. */
4390 parser->type_definition_forbidden_message = saved_message;
4391 /* `typeid' may not appear in an integral constant expression. */
4392 if (cp_parser_non_integral_constant_expression(parser,
4393 "`typeid' operator"))
4394 return error_mark_node;
4401 /* The syntax permitted here is the same permitted for an
4402 elaborated-type-specifier. */
4403 type = cp_parser_elaborated_type_specifier (parser,
4404 /*is_friend=*/false,
4405 /*is_declaration=*/false);
4406 postfix_expression = cp_parser_functional_cast (parser, type);
4414 /* If the next thing is a simple-type-specifier, we may be
4415 looking at a functional cast. We could also be looking at
4416 an id-expression. So, we try the functional cast, and if
4417 that doesn't work we fall back to the primary-expression. */
4418 cp_parser_parse_tentatively (parser);
4419 /* Look for the simple-type-specifier. */
4420 type = cp_parser_simple_type_specifier (parser,
4421 /*decl_specs=*/NULL,
4422 CP_PARSER_FLAGS_NONE);
4423 /* Parse the cast itself. */
4424 if (!cp_parser_error_occurred (parser))
4426 = cp_parser_functional_cast (parser, type);
4427 /* If that worked, we're done. */
4428 if (cp_parser_parse_definitely (parser))
4431 /* If the functional-cast didn't work out, try a
4432 compound-literal. */
4433 if (cp_parser_allow_gnu_extensions_p (parser)
4434 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4436 VEC(constructor_elt,gc) *initializer_list = NULL;
4437 bool saved_in_type_id_in_expr_p;
4439 cp_parser_parse_tentatively (parser);
4440 /* Consume the `('. */
4441 cp_lexer_consume_token (parser->lexer);
4442 /* Parse the type. */
4443 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4444 parser->in_type_id_in_expr_p = true;
4445 type = cp_parser_type_id (parser);
4446 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4447 /* Look for the `)'. */
4448 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4449 /* Look for the `{'. */
4450 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4451 /* If things aren't going well, there's no need to
4453 if (!cp_parser_error_occurred (parser))
4455 bool non_constant_p;
4456 /* Parse the initializer-list. */
4458 = cp_parser_initializer_list (parser, &non_constant_p);
4459 /* Allow a trailing `,'. */
4460 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4461 cp_lexer_consume_token (parser->lexer);
4462 /* Look for the final `}'. */
4463 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4465 /* If that worked, we're definitely looking at a
4466 compound-literal expression. */
4467 if (cp_parser_parse_definitely (parser))
4469 /* Warn the user that a compound literal is not
4470 allowed in standard C++. */
4472 pedwarn ("ISO C++ forbids compound-literals");
4473 /* For simplicity, we disallow compound literals in
4474 constant-expressions. We could
4475 allow compound literals of integer type, whose
4476 initializer was a constant, in constant
4477 expressions. Permitting that usage, as a further
4478 extension, would not change the meaning of any
4479 currently accepted programs. (Of course, as
4480 compound literals are not part of ISO C++, the
4481 standard has nothing to say.) */
4482 if (cp_parser_non_integral_constant_expression
4483 (parser, "non-constant compound literals"))
4485 postfix_expression = error_mark_node;
4488 /* Form the representation of the compound-literal. */
4490 = finish_compound_literal (type, initializer_list);
4495 /* It must be a primary-expression. */
4497 = cp_parser_primary_expression (parser, address_p, cast_p,
4498 /*template_arg_p=*/false,
4504 /* Keep looping until the postfix-expression is complete. */
4507 if (idk == CP_ID_KIND_UNQUALIFIED
4508 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4509 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4510 /* It is not a Koenig lookup function call. */
4512 = unqualified_name_lookup_error (postfix_expression);
4514 /* Peek at the next token. */
4515 token = cp_lexer_peek_token (parser->lexer);
4517 switch (token->type)
4519 case CPP_OPEN_SQUARE:
4521 = cp_parser_postfix_open_square_expression (parser,
4524 idk = CP_ID_KIND_NONE;
4525 is_member_access = false;
4528 case CPP_OPEN_PAREN:
4529 /* postfix-expression ( expression-list [opt] ) */
4532 bool is_builtin_constant_p;
4533 bool saved_integral_constant_expression_p = false;
4534 bool saved_non_integral_constant_expression_p = false;
4537 is_member_access = false;
4539 is_builtin_constant_p
4540 = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4541 if (is_builtin_constant_p)
4543 /* The whole point of __builtin_constant_p is to allow
4544 non-constant expressions to appear as arguments. */
4545 saved_integral_constant_expression_p
4546 = parser->integral_constant_expression_p;
4547 saved_non_integral_constant_expression_p
4548 = parser->non_integral_constant_expression_p;
4549 parser->integral_constant_expression_p = false;
4551 args = (cp_parser_parenthesized_expression_list
4552 (parser, /*is_attribute_list=*/false,
4553 /*cast_p=*/false, /*allow_expansion_p=*/true,
4554 /*non_constant_p=*/NULL));
4555 if (is_builtin_constant_p)
4557 parser->integral_constant_expression_p
4558 = saved_integral_constant_expression_p;
4559 parser->non_integral_constant_expression_p
4560 = saved_non_integral_constant_expression_p;
4563 if (args == error_mark_node)
4565 postfix_expression = error_mark_node;
4569 /* Function calls are not permitted in
4570 constant-expressions. */
4571 if (! builtin_valid_in_constant_expr_p (postfix_expression)
4572 && cp_parser_non_integral_constant_expression (parser,
4575 postfix_expression = error_mark_node;
4580 if (idk == CP_ID_KIND_UNQUALIFIED)
4582 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4588 = perform_koenig_lookup (postfix_expression, args);
4592 = unqualified_fn_lookup_error (postfix_expression);
4594 /* We do not perform argument-dependent lookup if
4595 normal lookup finds a non-function, in accordance
4596 with the expected resolution of DR 218. */
4597 else if (args && is_overloaded_fn (postfix_expression))
4599 tree fn = get_first_fn (postfix_expression);
4601 if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4602 fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4604 /* Only do argument dependent lookup if regular
4605 lookup does not find a set of member functions.
4606 [basic.lookup.koenig]/2a */
4607 if (!DECL_FUNCTION_MEMBER_P (fn))
4611 = perform_koenig_lookup (postfix_expression, args);
4616 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4618 tree instance = TREE_OPERAND (postfix_expression, 0);
4619 tree fn = TREE_OPERAND (postfix_expression, 1);
4621 if (processing_template_decl
4622 && (type_dependent_expression_p (instance)
4623 || (!BASELINK_P (fn)
4624 && TREE_CODE (fn) != FIELD_DECL)
4625 || type_dependent_expression_p (fn)
4626 || any_type_dependent_arguments_p (args)))
4629 = build_nt_call_list (postfix_expression, args);
4633 if (BASELINK_P (fn))
4635 = (build_new_method_call
4636 (instance, fn, args, NULL_TREE,
4637 (idk == CP_ID_KIND_QUALIFIED
4638 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4642 = finish_call_expr (postfix_expression, args,
4643 /*disallow_virtual=*/false,
4644 /*koenig_p=*/false);
4646 else if (TREE_CODE (postfix_expression) == OFFSET_REF
4647 || TREE_CODE (postfix_expression) == MEMBER_REF
4648 || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4649 postfix_expression = (build_offset_ref_call_from_tree
4650 (postfix_expression, args));
4651 else if (idk == CP_ID_KIND_QUALIFIED)
4652 /* A call to a static class member, or a namespace-scope
4655 = finish_call_expr (postfix_expression, args,
4656 /*disallow_virtual=*/true,
4659 /* All other function calls. */
4661 = finish_call_expr (postfix_expression, args,
4662 /*disallow_virtual=*/false,
4665 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
4666 idk = CP_ID_KIND_NONE;
4672 /* postfix-expression . template [opt] id-expression
4673 postfix-expression . pseudo-destructor-name
4674 postfix-expression -> template [opt] id-expression
4675 postfix-expression -> pseudo-destructor-name */
4677 /* Consume the `.' or `->' operator. */
4678 cp_lexer_consume_token (parser->lexer);
4681 = cp_parser_postfix_dot_deref_expression (parser, token->type,
4685 is_member_access = true;
4689 /* postfix-expression ++ */
4690 /* Consume the `++' token. */
4691 cp_lexer_consume_token (parser->lexer);
4692 /* Generate a representation for the complete expression. */
4694 = finish_increment_expr (postfix_expression,
4695 POSTINCREMENT_EXPR);
4696 /* Increments may not appear in constant-expressions. */
4697 if (cp_parser_non_integral_constant_expression (parser,
4699 postfix_expression = error_mark_node;
4700 idk = CP_ID_KIND_NONE;
4701 is_member_access = false;
4704 case CPP_MINUS_MINUS:
4705 /* postfix-expression -- */
4706 /* Consume the `--' token. */
4707 cp_lexer_consume_token (parser->lexer);
4708 /* Generate a representation for the complete expression. */
4710 = finish_increment_expr (postfix_expression,
4711 POSTDECREMENT_EXPR);
4712 /* Decrements may not appear in constant-expressions. */
4713 if (cp_parser_non_integral_constant_expression (parser,
4715 postfix_expression = error_mark_node;
4716 idk = CP_ID_KIND_NONE;
4717 is_member_access = false;
4721 if (member_access_only_p)
4722 return is_member_access? postfix_expression : error_mark_node;
4724 return postfix_expression;
4728 /* We should never get here. */
4730 return error_mark_node;
4733 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4734 by cp_parser_builtin_offsetof. We're looking for
4736 postfix-expression [ expression ]
4738 FOR_OFFSETOF is set if we're being called in that context, which
4739 changes how we deal with integer constant expressions. */
4742 cp_parser_postfix_open_square_expression (cp_parser *parser,
4743 tree postfix_expression,
4748 /* Consume the `[' token. */
4749 cp_lexer_consume_token (parser->lexer);
4751 /* Parse the index expression. */
4752 /* ??? For offsetof, there is a question of what to allow here. If
4753 offsetof is not being used in an integral constant expression context,
4754 then we *could* get the right answer by computing the value at runtime.
4755 If we are in an integral constant expression context, then we might
4756 could accept any constant expression; hard to say without analysis.
4757 Rather than open the barn door too wide right away, allow only integer
4758 constant expressions here. */
4760 index = cp_parser_constant_expression (parser, false, NULL);
4762 index = cp_parser_expression (parser, /*cast_p=*/false);
4764 /* Look for the closing `]'. */
4765 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4767 /* Build the ARRAY_REF. */
4768 postfix_expression = grok_array_decl (postfix_expression, index);
4770 /* When not doing offsetof, array references are not permitted in
4771 constant-expressions. */
4773 && (cp_parser_non_integral_constant_expression
4774 (parser, "an array reference")))
4775 postfix_expression = error_mark_node;
4777 return postfix_expression;
4780 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4781 by cp_parser_builtin_offsetof. We're looking for
4783 postfix-expression . template [opt] id-expression
4784 postfix-expression . pseudo-destructor-name
4785 postfix-expression -> template [opt] id-expression
4786 postfix-expression -> pseudo-destructor-name
4788 FOR_OFFSETOF is set if we're being called in that context. That sorta
4789 limits what of the above we'll actually accept, but nevermind.
4790 TOKEN_TYPE is the "." or "->" token, which will already have been
4791 removed from the stream. */
4794 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4795 enum cpp_ttype token_type,
4796 tree postfix_expression,
4797 bool for_offsetof, cp_id_kind *idk)
4801 bool pseudo_destructor_p;
4802 tree scope = NULL_TREE;
4804 /* If this is a `->' operator, dereference the pointer. */
4805 if (token_type == CPP_DEREF)
4806 postfix_expression = build_x_arrow (postfix_expression);
4807 /* Check to see whether or not the expression is type-dependent. */
4808 dependent_p = type_dependent_expression_p (postfix_expression);
4809 /* The identifier following the `->' or `.' is not qualified. */
4810 parser->scope = NULL_TREE;
4811 parser->qualifying_scope = NULL_TREE;
4812 parser->object_scope = NULL_TREE;
4813 *idk = CP_ID_KIND_NONE;
4814 /* Enter the scope corresponding to the type of the object
4815 given by the POSTFIX_EXPRESSION. */
4816 if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4818 scope = TREE_TYPE (postfix_expression);
4819 /* According to the standard, no expression should ever have
4820 reference type. Unfortunately, we do not currently match
4821 the standard in this respect in that our internal representation
4822 of an expression may have reference type even when the standard
4823 says it does not. Therefore, we have to manually obtain the
4824 underlying type here. */
4825 scope = non_reference (scope);
4826 /* The type of the POSTFIX_EXPRESSION must be complete. */
4827 if (scope == unknown_type_node)
4829 error ("%qE does not have class type", postfix_expression);
4833 scope = complete_type_or_else (scope, NULL_TREE);
4834 /* Let the name lookup machinery know that we are processing a
4835 class member access expression. */
4836 parser->context->object_type = scope;
4837 /* If something went wrong, we want to be able to discern that case,
4838 as opposed to the case where there was no SCOPE due to the type
4839 of expression being dependent. */
4841 scope = error_mark_node;
4842 /* If the SCOPE was erroneous, make the various semantic analysis
4843 functions exit quickly -- and without issuing additional error
4845 if (scope == error_mark_node)
4846 postfix_expression = error_mark_node;
4849 /* Assume this expression is not a pseudo-destructor access. */
4850 pseudo_destructor_p = false;
4852 /* If the SCOPE is a scalar type, then, if this is a valid program,
4853 we must be looking at a pseudo-destructor-name. */
4854 if (scope && SCALAR_TYPE_P (scope))
4859 cp_parser_parse_tentatively (parser);
4860 /* Parse the pseudo-destructor-name. */
4862 cp_parser_pseudo_destructor_name (parser, &s, &type);
4863 if (cp_parser_parse_definitely (parser))
4865 pseudo_destructor_p = true;
4867 = finish_pseudo_destructor_expr (postfix_expression,
4868 s, TREE_TYPE (type));
4872 if (!pseudo_destructor_p)
4874 /* If the SCOPE is not a scalar type, we are looking at an
4875 ordinary class member access expression, rather than a
4876 pseudo-destructor-name. */
4878 /* Parse the id-expression. */
4879 name = (cp_parser_id_expression
4881 cp_parser_optional_template_keyword (parser),
4882 /*check_dependency_p=*/true,
4884 /*declarator_p=*/false,
4885 /*optional_p=*/false));
4886 /* In general, build a SCOPE_REF if the member name is qualified.
4887 However, if the name was not dependent and has already been
4888 resolved; there is no need to build the SCOPE_REF. For example;
4890 struct X { void f(); };
4891 template <typename T> void f(T* t) { t->X::f(); }
4893 Even though "t" is dependent, "X::f" is not and has been resolved
4894 to a BASELINK; there is no need to include scope information. */
4896 /* But we do need to remember that there was an explicit scope for
4897 virtual function calls. */
4899 *idk = CP_ID_KIND_QUALIFIED;
4901 /* If the name is a template-id that names a type, we will get a
4902 TYPE_DECL here. That is invalid code. */
4903 if (TREE_CODE (name) == TYPE_DECL)
4905 error ("invalid use of %qD", name);
4906 postfix_expression = error_mark_node;
4910 if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4912 name = build_qualified_name (/*type=*/NULL_TREE,
4916 parser->scope = NULL_TREE;
4917 parser->qualifying_scope = NULL_TREE;
4918 parser->object_scope = NULL_TREE;
4920 if (scope && name && BASELINK_P (name))
4921 adjust_result_of_qualified_name_lookup
4922 (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
4924 = finish_class_member_access_expr (postfix_expression, name,
4929 /* We no longer need to look up names in the scope of the object on
4930 the left-hand side of the `.' or `->' operator. */
4931 parser->context->object_type = NULL_TREE;
4933 /* Outside of offsetof, these operators may not appear in
4934 constant-expressions. */
4936 && (cp_parser_non_integral_constant_expression
4937 (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4938 postfix_expression = error_mark_node;
4940 return postfix_expression;
4943 /* Parse a parenthesized expression-list.
4946 assignment-expression
4947 expression-list, assignment-expression
4952 identifier, expression-list
4954 CAST_P is true if this expression is the target of a cast.
4956 ALLOW_EXPANSION_P is true if this expression allows expansion of an
4959 Returns a TREE_LIST. The TREE_VALUE of each node is a
4960 representation of an assignment-expression. Note that a TREE_LIST
4961 is returned even if there is only a single expression in the list.
4962 error_mark_node is returned if the ( and or ) are
4963 missing. NULL_TREE is returned on no expressions. The parentheses
4964 are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4965 list being parsed. If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4966 indicates whether or not all of the expressions in the list were
4970 cp_parser_parenthesized_expression_list (cp_parser* parser,
4971 bool is_attribute_list,
4973 bool allow_expansion_p,
4974 bool *non_constant_p)
4976 tree expression_list = NULL_TREE;
4977 bool fold_expr_p = is_attribute_list;
4978 tree identifier = NULL_TREE;
4980 /* Assume all the expressions will be constant. */
4982 *non_constant_p = false;
4984 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4985 return error_mark_node;
4987 /* Consume expressions until there are no more. */
4988 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4993 /* At the beginning of attribute lists, check to see if the
4994 next token is an identifier. */
4995 if (is_attribute_list
4996 && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5000 /* Consume the identifier. */
5001 token = cp_lexer_consume_token (parser->lexer);
5002 /* Save the identifier. */
5003 identifier = token->u.value;
5007 /* Parse the next assignment-expression. */
5010 bool expr_non_constant_p;
5011 expr = (cp_parser_constant_expression
5012 (parser, /*allow_non_constant_p=*/true,
5013 &expr_non_constant_p));
5014 if (expr_non_constant_p)
5015 *non_constant_p = true;
5018 expr = cp_parser_assignment_expression (parser, cast_p);
5021 expr = fold_non_dependent_expr (expr);
5023 /* If we have an ellipsis, then this is an expression
5025 if (allow_expansion_p
5026 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5028 /* Consume the `...'. */
5029 cp_lexer_consume_token (parser->lexer);
5031 /* Build the argument pack. */
5032 expr = make_pack_expansion (expr);
5035 /* Add it to the list. We add error_mark_node
5036 expressions to the list, so that we can still tell if
5037 the correct form for a parenthesized expression-list
5038 is found. That gives better errors. */
5039 expression_list = tree_cons (NULL_TREE, expr, expression_list);
5041 if (expr == error_mark_node)
5045 /* After the first item, attribute lists look the same as
5046 expression lists. */
5047 is_attribute_list = false;
5050 /* If the next token isn't a `,', then we are done. */
5051 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5054 /* Otherwise, consume the `,' and keep going. */
5055 cp_lexer_consume_token (parser->lexer);
5058 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5063 /* We try and resync to an unnested comma, as that will give the
5064 user better diagnostics. */
5065 ending = cp_parser_skip_to_closing_parenthesis (parser,
5066 /*recovering=*/true,
5068 /*consume_paren=*/true);
5072 return error_mark_node;
5075 /* We built up the list in reverse order so we must reverse it now. */
5076 expression_list = nreverse (expression_list);
5078 expression_list = tree_cons (NULL_TREE, identifier, expression_list);
5080 return expression_list;
5083 /* Parse a pseudo-destructor-name.
5085 pseudo-destructor-name:
5086 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5087 :: [opt] nested-name-specifier template template-id :: ~ type-name
5088 :: [opt] nested-name-specifier [opt] ~ type-name
5090 If either of the first two productions is used, sets *SCOPE to the
5091 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
5092 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
5093 or ERROR_MARK_NODE if the parse fails. */
5096 cp_parser_pseudo_destructor_name (cp_parser* parser,
5100 bool nested_name_specifier_p;
5102 /* Assume that things will not work out. */
5103 *type = error_mark_node;
5105 /* Look for the optional `::' operator. */
5106 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5107 /* Look for the optional nested-name-specifier. */
5108 nested_name_specifier_p
5109 = (cp_parser_nested_name_specifier_opt (parser,
5110 /*typename_keyword_p=*/false,
5111 /*check_dependency_p=*/true,
5113 /*is_declaration=*/true)
5115 /* Now, if we saw a nested-name-specifier, we might be doing the
5116 second production. */
5117 if (nested_name_specifier_p
5118 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5120 /* Consume the `template' keyword. */
5121 cp_lexer_consume_token (parser->lexer);
5122 /* Parse the template-id. */
5123 cp_parser_template_id (parser,
5124 /*template_keyword_p=*/true,
5125 /*check_dependency_p=*/false,
5126 /*is_declaration=*/true);
5127 /* Look for the `::' token. */
5128 cp_parser_require (parser, CPP_SCOPE, "`::'");
5130 /* If the next token is not a `~', then there might be some
5131 additional qualification. */
5132 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5134 /* Look for the type-name. */
5135 *scope = TREE_TYPE (cp_parser_type_name (parser));
5137 if (*scope == error_mark_node)
5140 /* If we don't have ::~, then something has gone wrong. Since
5141 the only caller of this function is looking for something
5142 after `.' or `->' after a scalar type, most likely the
5143 program is trying to get a member of a non-aggregate
5145 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
5146 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
5148 cp_parser_error (parser, "request for member of non-aggregate type");
5152 /* Look for the `::' token. */
5153 cp_parser_require (parser, CPP_SCOPE, "`::'");
5158 /* Look for the `~'. */
5159 cp_parser_require (parser, CPP_COMPL, "`~'");
5160 /* Look for the type-name again. We are not responsible for
5161 checking that it matches the first type-name. */
5162 *type = cp_parser_type_name (parser);
5165 /* Parse a unary-expression.
5171 unary-operator cast-expression
5172 sizeof unary-expression
5180 __extension__ cast-expression
5181 __alignof__ unary-expression
5182 __alignof__ ( type-id )
5183 __real__ cast-expression
5184 __imag__ cast-expression
5187 ADDRESS_P is true iff the unary-expression is appearing as the
5188 operand of the `&' operator. CAST_P is true if this expression is
5189 the target of a cast.
5191 Returns a representation of the expression. */
5194 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
5197 enum tree_code unary_operator;
5199 /* Peek at the next token. */
5200 token = cp_lexer_peek_token (parser->lexer);
5201 /* Some keywords give away the kind of expression. */
5202 if (token->type == CPP_KEYWORD)
5204 enum rid keyword = token->keyword;
5214 op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5215 /* Consume the token. */
5216 cp_lexer_consume_token (parser->lexer);
5217 /* Parse the operand. */
5218 operand = cp_parser_sizeof_operand (parser, keyword);
5220 if (TYPE_P (operand))
5221 return cxx_sizeof_or_alignof_type (operand, op, true);
5223 return cxx_sizeof_or_alignof_expr (operand, op);
5227 return cp_parser_new_expression (parser);
5230 return cp_parser_delete_expression (parser);
5234 /* The saved value of the PEDANTIC flag. */
5238 /* Save away the PEDANTIC flag. */
5239 cp_parser_extension_opt (parser, &saved_pedantic);
5240 /* Parse the cast-expression. */
5241 expr = cp_parser_simple_cast_expression (parser);
5242 /* Restore the PEDANTIC flag. */
5243 pedantic = saved_pedantic;
5253 /* Consume the `__real__' or `__imag__' token. */
5254 cp_lexer_consume_token (parser->lexer);
5255 /* Parse the cast-expression. */
5256 expression = cp_parser_simple_cast_expression (parser);
5257 /* Create the complete representation. */
5258 return build_x_unary_op ((keyword == RID_REALPART
5259 ? REALPART_EXPR : IMAGPART_EXPR),
5269 /* Look for the `:: new' and `:: delete', which also signal the
5270 beginning of a new-expression, or delete-expression,
5271 respectively. If the next token is `::', then it might be one of
5273 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5277 /* See if the token after the `::' is one of the keywords in
5278 which we're interested. */
5279 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5280 /* If it's `new', we have a new-expression. */
5281 if (keyword == RID_NEW)
5282 return cp_parser_new_expression (parser);
5283 /* Similarly, for `delete'. */
5284 else if (keyword == RID_DELETE)
5285 return cp_parser_delete_expression (parser);
5288 /* Look for a unary operator. */
5289 unary_operator = cp_parser_unary_operator (token);
5290 /* The `++' and `--' operators can be handled similarly, even though
5291 they are not technically unary-operators in the grammar. */
5292 if (unary_operator == ERROR_MARK)
5294 if (token->type == CPP_PLUS_PLUS)
5295 unary_operator = PREINCREMENT_EXPR;
5296 else if (token->type == CPP_MINUS_MINUS)
5297 unary_operator = PREDECREMENT_EXPR;
5298 /* Handle the GNU address-of-label extension. */
5299 else if (cp_parser_allow_gnu_extensions_p (parser)
5300 && token->type == CPP_AND_AND)
5304 /* Consume the '&&' token. */
5305 cp_lexer_consume_token (parser->lexer);
5306 /* Look for the identifier. */
5307 identifier = cp_parser_identifier (parser);
5308 /* Create an expression representing the address. */
5309 return finish_label_address_expr (identifier);
5312 if (unary_operator != ERROR_MARK)
5314 tree cast_expression;
5315 tree expression = error_mark_node;
5316 const char *non_constant_p = NULL;
5318 /* Consume the operator token. */
5319 token = cp_lexer_consume_token (parser->lexer);
5320 /* Parse the cast-expression. */
5322 = cp_parser_cast_expression (parser,
5323 unary_operator == ADDR_EXPR,
5325 /* Now, build an appropriate representation. */
5326 switch (unary_operator)
5329 non_constant_p = "`*'";
5330 expression = build_x_indirect_ref (cast_expression, "unary *");
5334 non_constant_p = "`&'";
5337 expression = build_x_unary_op (unary_operator, cast_expression);
5340 case PREINCREMENT_EXPR:
5341 case PREDECREMENT_EXPR:
5342 non_constant_p = (unary_operator == PREINCREMENT_EXPR
5345 case UNARY_PLUS_EXPR:
5347 case TRUTH_NOT_EXPR:
5348 expression = finish_unary_op_expr (unary_operator, cast_expression);
5356 && cp_parser_non_integral_constant_expression (parser,
5358 expression = error_mark_node;
5363 return cp_parser_postfix_expression (parser, address_p, cast_p,
5364 /*member_access_only_p=*/false);
5367 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
5368 unary-operator, the corresponding tree code is returned. */
5370 static enum tree_code
5371 cp_parser_unary_operator (cp_token* token)
5373 switch (token->type)
5376 return INDIRECT_REF;
5382 return UNARY_PLUS_EXPR;
5388 return TRUTH_NOT_EXPR;
5391 return BIT_NOT_EXPR;
5398 /* Parse a new-expression.
5401 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5402 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5404 Returns a representation of the expression. */
5407 cp_parser_new_expression (cp_parser* parser)
5409 bool global_scope_p;
5415 /* Look for the optional `::' operator. */
5417 = (cp_parser_global_scope_opt (parser,
5418 /*current_scope_valid_p=*/false)
5420 /* Look for the `new' operator. */
5421 cp_parser_require_keyword (parser, RID_NEW, "`new'");
5422 /* There's no easy way to tell a new-placement from the
5423 `( type-id )' construct. */
5424 cp_parser_parse_tentatively (parser);
5425 /* Look for a new-placement. */
5426 placement = cp_parser_new_placement (parser);
5427 /* If that didn't work out, there's no new-placement. */
5428 if (!cp_parser_parse_definitely (parser))
5429 placement = NULL_TREE;
5431 /* If the next token is a `(', then we have a parenthesized
5433 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5435 /* Consume the `('. */
5436 cp_lexer_consume_token (parser->lexer);
5437 /* Parse the type-id. */
5438 type = cp_parser_type_id (parser);
5439 /* Look for the closing `)'. */
5440 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5441 /* There should not be a direct-new-declarator in this production,
5442 but GCC used to allowed this, so we check and emit a sensible error
5443 message for this case. */
5444 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5446 error ("array bound forbidden after parenthesized type-id");
5447 inform ("try removing the parentheses around the type-id");
5448 cp_parser_direct_new_declarator (parser);
5452 /* Otherwise, there must be a new-type-id. */
5454 type = cp_parser_new_type_id (parser, &nelts);
5456 /* If the next token is a `(', then we have a new-initializer. */
5457 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5458 initializer = cp_parser_new_initializer (parser);
5460 initializer = NULL_TREE;
5462 /* A new-expression may not appear in an integral constant
5464 if (cp_parser_non_integral_constant_expression (parser, "`new'"))
5465 return error_mark_node;
5467 /* Create a representation of the new-expression. */
5468 return build_new (placement, type, nelts, initializer, global_scope_p);
5471 /* Parse a new-placement.
5476 Returns the same representation as for an expression-list. */
5479 cp_parser_new_placement (cp_parser* parser)
5481 tree expression_list;
5483 /* Parse the expression-list. */
5484 expression_list = (cp_parser_parenthesized_expression_list
5485 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5486 /*non_constant_p=*/NULL));
5488 return expression_list;
5491 /* Parse a new-type-id.
5494 type-specifier-seq new-declarator [opt]
5496 Returns the TYPE allocated. If the new-type-id indicates an array
5497 type, *NELTS is set to the number of elements in the last array
5498 bound; the TYPE will not include the last array bound. */
5501 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5503 cp_decl_specifier_seq type_specifier_seq;
5504 cp_declarator *new_declarator;
5505 cp_declarator *declarator;
5506 cp_declarator *outer_declarator;
5507 const char *saved_message;
5510 /* The type-specifier sequence must not contain type definitions.
5511 (It cannot contain declarations of new types either, but if they
5512 are not definitions we will catch that because they are not
5514 saved_message = parser->type_definition_forbidden_message;
5515 parser->type_definition_forbidden_message
5516 = "types may not be defined in a new-type-id";
5517 /* Parse the type-specifier-seq. */
5518 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5519 &type_specifier_seq);
5520 /* Restore the old message. */
5521 parser->type_definition_forbidden_message = saved_message;
5522 /* Parse the new-declarator. */
5523 new_declarator = cp_parser_new_declarator_opt (parser);
5525 /* Determine the number of elements in the last array dimension, if
5528 /* Skip down to the last array dimension. */
5529 declarator = new_declarator;
5530 outer_declarator = NULL;
5531 while (declarator && (declarator->kind == cdk_pointer
5532 || declarator->kind == cdk_ptrmem))
5534 outer_declarator = declarator;
5535 declarator = declarator->declarator;
5538 && declarator->kind == cdk_array
5539 && declarator->declarator
5540 && declarator->declarator->kind == cdk_array)
5542 outer_declarator = declarator;
5543 declarator = declarator->declarator;
5546 if (declarator && declarator->kind == cdk_array)
5548 *nelts = declarator->u.array.bounds;
5549 if (*nelts == error_mark_node)
5550 *nelts = integer_one_node;
5552 if (outer_declarator)
5553 outer_declarator->declarator = declarator->declarator;
5555 new_declarator = NULL;
5558 type = groktypename (&type_specifier_seq, new_declarator);
5562 /* Parse an (optional) new-declarator.
5565 ptr-operator new-declarator [opt]
5566 direct-new-declarator
5568 Returns the declarator. */
5570 static cp_declarator *
5571 cp_parser_new_declarator_opt (cp_parser* parser)
5573 enum tree_code code;
5575 cp_cv_quals cv_quals;
5577 /* We don't know if there's a ptr-operator next, or not. */
5578 cp_parser_parse_tentatively (parser);
5579 /* Look for a ptr-operator. */
5580 code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5581 /* If that worked, look for more new-declarators. */
5582 if (cp_parser_parse_definitely (parser))
5584 cp_declarator *declarator;
5586 /* Parse another optional declarator. */
5587 declarator = cp_parser_new_declarator_opt (parser);
5589 return cp_parser_make_indirect_declarator
5590 (code, type, cv_quals, declarator);
5593 /* If the next token is a `[', there is a direct-new-declarator. */
5594 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5595 return cp_parser_direct_new_declarator (parser);
5600 /* Parse a direct-new-declarator.
5602 direct-new-declarator:
5604 direct-new-declarator [constant-expression]
5608 static cp_declarator *
5609 cp_parser_direct_new_declarator (cp_parser* parser)
5611 cp_declarator *declarator = NULL;
5617 /* Look for the opening `['. */
5618 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5619 /* The first expression is not required to be constant. */
5622 expression = cp_parser_expression (parser, /*cast_p=*/false);
5623 /* The standard requires that the expression have integral
5624 type. DR 74 adds enumeration types. We believe that the
5625 real intent is that these expressions be handled like the
5626 expression in a `switch' condition, which also allows
5627 classes with a single conversion to integral or
5628 enumeration type. */
5629 if (!processing_template_decl)
5632 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5637 error ("expression in new-declarator must have integral "
5638 "or enumeration type");
5639 expression = error_mark_node;
5643 /* But all the other expressions must be. */
5646 = cp_parser_constant_expression (parser,
5647 /*allow_non_constant=*/false,
5649 /* Look for the closing `]'. */
5650 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5652 /* Add this bound to the declarator. */
5653 declarator = make_array_declarator (declarator, expression);
5655 /* If the next token is not a `[', then there are no more
5657 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5664 /* Parse a new-initializer.
5667 ( expression-list [opt] )
5669 Returns a representation of the expression-list. If there is no
5670 expression-list, VOID_ZERO_NODE is returned. */
5673 cp_parser_new_initializer (cp_parser* parser)
5675 tree expression_list;
5677 expression_list = (cp_parser_parenthesized_expression_list
5678 (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5679 /*non_constant_p=*/NULL));
5680 if (!expression_list)
5681 expression_list = void_zero_node;
5683 return expression_list;
5686 /* Parse a delete-expression.
5689 :: [opt] delete cast-expression
5690 :: [opt] delete [ ] cast-expression
5692 Returns a representation of the expression. */
5695 cp_parser_delete_expression (cp_parser* parser)
5697 bool global_scope_p;
5701 /* Look for the optional `::' operator. */
5703 = (cp_parser_global_scope_opt (parser,
5704 /*current_scope_valid_p=*/false)
5706 /* Look for the `delete' keyword. */
5707 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5708 /* See if the array syntax is in use. */
5709 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5711 /* Consume the `[' token. */
5712 cp_lexer_consume_token (parser->lexer);
5713 /* Look for the `]' token. */
5714 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5715 /* Remember that this is the `[]' construct. */
5721 /* Parse the cast-expression. */
5722 expression = cp_parser_simple_cast_expression (parser);
5724 /* A delete-expression may not appear in an integral constant
5726 if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5727 return error_mark_node;
5729 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5732 /* Parse a cast-expression.
5736 ( type-id ) cast-expression
5738 ADDRESS_P is true iff the unary-expression is appearing as the
5739 operand of the `&' operator. CAST_P is true if this expression is
5740 the target of a cast.
5742 Returns a representation of the expression. */
5745 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5747 /* If it's a `(', then we might be looking at a cast. */
5748 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5750 tree type = NULL_TREE;
5751 tree expr = NULL_TREE;
5752 bool compound_literal_p;
5753 const char *saved_message;
5755 /* There's no way to know yet whether or not this is a cast.
5756 For example, `(int (3))' is a unary-expression, while `(int)
5757 3' is a cast. So, we resort to parsing tentatively. */
5758 cp_parser_parse_tentatively (parser);
5759 /* Types may not be defined in a cast. */
5760 saved_message = parser->type_definition_forbidden_message;
5761 parser->type_definition_forbidden_message
5762 = "types may not be defined in casts";
5763 /* Consume the `('. */
5764 cp_lexer_consume_token (parser->lexer);
5765 /* A very tricky bit is that `(struct S) { 3 }' is a
5766 compound-literal (which we permit in C++ as an extension).
5767 But, that construct is not a cast-expression -- it is a
5768 postfix-expression. (The reason is that `(struct S) { 3 }.i'
5769 is legal; if the compound-literal were a cast-expression,
5770 you'd need an extra set of parentheses.) But, if we parse
5771 the type-id, and it happens to be a class-specifier, then we
5772 will commit to the parse at that point, because we cannot
5773 undo the action that is done when creating a new class. So,
5774 then we cannot back up and do a postfix-expression.
5776 Therefore, we scan ahead to the closing `)', and check to see
5777 if the token after the `)' is a `{'. If so, we are not
5778 looking at a cast-expression.
5780 Save tokens so that we can put them back. */
5781 cp_lexer_save_tokens (parser->lexer);
5782 /* Skip tokens until the next token is a closing parenthesis.
5783 If we find the closing `)', and the next token is a `{', then
5784 we are looking at a compound-literal. */
5786 = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5787 /*consume_paren=*/true)
5788 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5789 /* Roll back the tokens we skipped. */
5790 cp_lexer_rollback_tokens (parser->lexer);
5791 /* If we were looking at a compound-literal, simulate an error
5792 so that the call to cp_parser_parse_definitely below will
5794 if (compound_literal_p)
5795 cp_parser_simulate_error (parser);
5798 bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5799 parser->in_type_id_in_expr_p = true;
5800 /* Look for the type-id. */
5801 type = cp_parser_type_id (parser);
5802 /* Look for the closing `)'. */
5803 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5804 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5807 /* Restore the saved message. */
5808 parser->type_definition_forbidden_message = saved_message;
5810 /* If ok so far, parse the dependent expression. We cannot be
5811 sure it is a cast. Consider `(T ())'. It is a parenthesized
5812 ctor of T, but looks like a cast to function returning T
5813 without a dependent expression. */
5814 if (!cp_parser_error_occurred (parser))
5815 expr = cp_parser_cast_expression (parser,
5816 /*address_p=*/false,
5819 if (cp_parser_parse_definitely (parser))
5821 /* Warn about old-style casts, if so requested. */
5822 if (warn_old_style_cast
5823 && !in_system_header
5824 && !VOID_TYPE_P (type)
5825 && current_lang_name != lang_name_c)
5826 warning (OPT_Wold_style_cast, "use of old-style cast");
5828 /* Only type conversions to integral or enumeration types
5829 can be used in constant-expressions. */
5830 if (!cast_valid_in_integral_constant_expression_p (type)
5831 && (cp_parser_non_integral_constant_expression
5833 "a cast to a type other than an integral or "
5834 "enumeration type")))
5835 return error_mark_node;
5837 /* Perform the cast. */
5838 expr = build_c_cast (type, expr);
5843 /* If we get here, then it's not a cast, so it must be a
5844 unary-expression. */
5845 return cp_parser_unary_expression (parser, address_p, cast_p);
5848 /* Parse a binary expression of the general form:
5852 pm-expression .* cast-expression
5853 pm-expression ->* cast-expression
5855 multiplicative-expression:
5857 multiplicative-expression * pm-expression
5858 multiplicative-expression / pm-expression
5859 multiplicative-expression % pm-expression
5861 additive-expression:
5862 multiplicative-expression
5863 additive-expression + multiplicative-expression
5864 additive-expression - multiplicative-expression
5868 shift-expression << additive-expression
5869 shift-expression >> additive-expression
5871 relational-expression:
5873 relational-expression < shift-expression
5874 relational-expression > shift-expression
5875 relational-expression <= shift-expression
5876 relational-expression >= shift-expression
5880 relational-expression:
5881 relational-expression <? shift-expression
5882 relational-expression >? shift-expression
5884 equality-expression:
5885 relational-expression
5886 equality-expression == relational-expression
5887 equality-expression != relational-expression
5891 and-expression & equality-expression
5893 exclusive-or-expression:
5895 exclusive-or-expression ^ and-expression
5897 inclusive-or-expression:
5898 exclusive-or-expression
5899 inclusive-or-expression | exclusive-or-expression
5901 logical-and-expression:
5902 inclusive-or-expression
5903 logical-and-expression && inclusive-or-expression
5905 logical-or-expression:
5906 logical-and-expression
5907 logical-or-expression || logical-and-expression
5909 All these are implemented with a single function like:
5912 simple-cast-expression
5913 binary-expression <token> binary-expression
5915 CAST_P is true if this expression is the target of a cast.
5917 The binops_by_token map is used to get the tree codes for each <token> type.
5918 binary-expressions are associated according to a precedence table. */
5920 #define TOKEN_PRECEDENCE(token) \
5921 (((token->type == CPP_GREATER \
5922 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
5923 && !parser->greater_than_is_operator_p) \
5924 ? PREC_NOT_OPERATOR \
5925 : binops_by_token[token->type].prec)
5928 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5930 cp_parser_expression_stack stack;
5931 cp_parser_expression_stack_entry *sp = &stack[0];
5934 enum tree_code tree_type, lhs_type, rhs_type;
5935 enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5938 /* Parse the first expression. */
5939 lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5940 lhs_type = ERROR_MARK;
5944 /* Get an operator token. */
5945 token = cp_lexer_peek_token (parser->lexer);
5947 if (warn_cxx0x_compat
5948 && token->type == CPP_RSHIFT
5949 && !parser->greater_than_is_operator_p)
5951 warning (OPT_Wc__0x_compat,
5952 "%H%<>>%> operator will be treated as two right angle brackets in C++0x",
5954 warning (OPT_Wc__0x_compat,
5955 "suggest parentheses around %<>>%> expression");
5958 new_prec = TOKEN_PRECEDENCE (token);
5960 /* Popping an entry off the stack means we completed a subexpression:
5961 - either we found a token which is not an operator (`>' where it is not
5962 an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5963 will happen repeatedly;
5964 - or, we found an operator which has lower priority. This is the case
5965 where the recursive descent *ascends*, as in `3 * 4 + 5' after
5967 if (new_prec <= prec)
5976 tree_type = binops_by_token[token->type].tree_type;
5978 /* We used the operator token. */
5979 cp_lexer_consume_token (parser->lexer);
5981 /* Extract another operand. It may be the RHS of this expression
5982 or the LHS of a new, higher priority expression. */
5983 rhs = cp_parser_simple_cast_expression (parser);
5984 rhs_type = ERROR_MARK;
5986 /* Get another operator token. Look up its precedence to avoid
5987 building a useless (immediately popped) stack entry for common
5988 cases such as 3 + 4 + 5 or 3 * 4 + 5. */
5989 token = cp_lexer_peek_token (parser->lexer);
5990 lookahead_prec = TOKEN_PRECEDENCE (token);
5991 if (lookahead_prec > new_prec)
5993 /* ... and prepare to parse the RHS of the new, higher priority
5994 expression. Since precedence levels on the stack are
5995 monotonically increasing, we do not have to care about
5998 sp->tree_type = tree_type;
6000 sp->lhs_type = lhs_type;
6003 lhs_type = rhs_type;
6005 new_prec = lookahead_prec;
6009 /* If the stack is not empty, we have parsed into LHS the right side
6010 (`4' in the example above) of an expression we had suspended.
6011 We can use the information on the stack to recover the LHS (`3')
6012 from the stack together with the tree code (`MULT_EXPR'), and
6013 the precedence of the higher level subexpression
6014 (`PREC_ADDITIVE_EXPRESSION'). TOKEN is the CPP_PLUS token,
6015 which will be used to actually build the additive expression. */
6018 tree_type = sp->tree_type;
6020 rhs_type = lhs_type;
6022 lhs_type = sp->lhs_type;
6025 overloaded_p = false;
6026 lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6028 lhs_type = tree_type;
6030 /* If the binary operator required the use of an overloaded operator,
6031 then this expression cannot be an integral constant-expression.
6032 An overloaded operator can be used even if both operands are
6033 otherwise permissible in an integral constant-expression if at
6034 least one of the operands is of enumeration type. */
6037 && (cp_parser_non_integral_constant_expression
6038 (parser, "calls to overloaded operators")))
6039 return error_mark_node;
6046 /* Parse the `? expression : assignment-expression' part of a
6047 conditional-expression. The LOGICAL_OR_EXPR is the
6048 logical-or-expression that started the conditional-expression.
6049 Returns a representation of the entire conditional-expression.
6051 This routine is used by cp_parser_assignment_expression.
6053 ? expression : assignment-expression
6057 ? : assignment-expression */
6060 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6063 tree assignment_expr;
6065 /* Consume the `?' token. */
6066 cp_lexer_consume_token (parser->lexer);
6067 if (cp_parser_allow_gnu_extensions_p (parser)
6068 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6069 /* Implicit true clause. */
6072 /* Parse the expression. */
6073 expr = cp_parser_expression (parser, /*cast_p=*/false);
6075 /* The next token should be a `:'. */
6076 cp_parser_require (parser, CPP_COLON, "`:'");
6077 /* Parse the assignment-expression. */
6078 assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6080 /* Build the conditional-expression. */
6081 return build_x_conditional_expr (logical_or_expr,
6086 /* Parse an assignment-expression.
6088 assignment-expression:
6089 conditional-expression
6090 logical-or-expression assignment-operator assignment_expression
6093 CAST_P is true if this expression is the target of a cast.
6095 Returns a representation for the expression. */
6098 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
6102 /* If the next token is the `throw' keyword, then we're looking at
6103 a throw-expression. */
6104 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6105 expr = cp_parser_throw_expression (parser);
6106 /* Otherwise, it must be that we are looking at a
6107 logical-or-expression. */
6110 /* Parse the binary expressions (logical-or-expression). */
6111 expr = cp_parser_binary_expression (parser, cast_p);
6112 /* If the next token is a `?' then we're actually looking at a
6113 conditional-expression. */
6114 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6115 return cp_parser_question_colon_clause (parser, expr);
6118 enum tree_code assignment_operator;
6120 /* If it's an assignment-operator, we're using the second
6123 = cp_parser_assignment_operator_opt (parser);
6124 if (assignment_operator != ERROR_MARK)
6128 /* Parse the right-hand side of the assignment. */
6129 rhs = cp_parser_assignment_expression (parser, cast_p);
6130 /* An assignment may not appear in a
6131 constant-expression. */
6132 if (cp_parser_non_integral_constant_expression (parser,
6134 return error_mark_node;
6135 /* Build the assignment expression. */
6136 expr = build_x_modify_expr (expr,
6137 assignment_operator,
6146 /* Parse an (optional) assignment-operator.
6148 assignment-operator: one of
6149 = *= /= %= += -= >>= <<= &= ^= |=
6153 assignment-operator: one of
6156 If the next token is an assignment operator, the corresponding tree
6157 code is returned, and the token is consumed. For example, for
6158 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
6159 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
6160 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
6161 operator, ERROR_MARK is returned. */
6163 static enum tree_code
6164 cp_parser_assignment_operator_opt (cp_parser* parser)
6169 /* Peek at the next toen. */
6170 token = cp_lexer_peek_token (parser->lexer);
6172 switch (token->type)
6183 op = TRUNC_DIV_EXPR;
6187 op = TRUNC_MOD_EXPR;
6219 /* Nothing else is an assignment operator. */
6223 /* If it was an assignment operator, consume it. */
6224 if (op != ERROR_MARK)
6225 cp_lexer_consume_token (parser->lexer);
6230 /* Parse an expression.
6233 assignment-expression
6234 expression , assignment-expression
6236 CAST_P is true if this expression is the target of a cast.
6238 Returns a representation of the expression. */
6241 cp_parser_expression (cp_parser* parser, bool cast_p)
6243 tree expression = NULL_TREE;
6247 tree assignment_expression;
6249 /* Parse the next assignment-expression. */
6250 assignment_expression
6251 = cp_parser_assignment_expression (parser, cast_p);
6252 /* If this is the first assignment-expression, we can just
6255 expression = assignment_expression;
6257 expression = build_x_compound_expr (expression,
6258 assignment_expression);
6259 /* If the next token is not a comma, then we are done with the
6261 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6263 /* Consume the `,'. */
6264 cp_lexer_consume_token (parser->lexer);
6265 /* A comma operator cannot appear in a constant-expression. */
6266 if (cp_parser_non_integral_constant_expression (parser,
6267 "a comma operator"))
6268 expression = error_mark_node;
6274 /* Parse a constant-expression.
6276 constant-expression:
6277 conditional-expression
6279 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6280 accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
6281 constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
6282 is false, NON_CONSTANT_P should be NULL. */
6285 cp_parser_constant_expression (cp_parser* parser,
6286 bool allow_non_constant_p,
6287 bool *non_constant_p)
6289 bool saved_integral_constant_expression_p;
6290 bool saved_allow_non_integral_constant_expression_p;
6291 bool saved_non_integral_constant_expression_p;
6294 /* It might seem that we could simply parse the
6295 conditional-expression, and then check to see if it were
6296 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
6297 one that the compiler can figure out is constant, possibly after
6298 doing some simplifications or optimizations. The standard has a
6299 precise definition of constant-expression, and we must honor
6300 that, even though it is somewhat more restrictive.
6306 is not a legal declaration, because `(2, 3)' is not a
6307 constant-expression. The `,' operator is forbidden in a
6308 constant-expression. However, GCC's constant-folding machinery
6309 will fold this operation to an INTEGER_CST for `3'. */
6311 /* Save the old settings. */
6312 saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6313 saved_allow_non_integral_constant_expression_p
6314 = parser->allow_non_integral_constant_expression_p;
6315 saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6316 /* We are now parsing a constant-expression. */
6317 parser->integral_constant_expression_p = true;
6318 parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6319 parser->non_integral_constant_expression_p = false;
6320 /* Although the grammar says "conditional-expression", we parse an
6321 "assignment-expression", which also permits "throw-expression"
6322 and the use of assignment operators. In the case that
6323 ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6324 otherwise. In the case that ALLOW_NON_CONSTANT_P is true, it is
6325 actually essential that we look for an assignment-expression.
6326 For example, cp_parser_initializer_clauses uses this function to
6327 determine whether a particular assignment-expression is in fact
6329 expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
6330 /* Restore the old settings. */
6331 parser->integral_constant_expression_p
6332 = saved_integral_constant_expression_p;
6333 parser->allow_non_integral_constant_expression_p
6334 = saved_allow_non_integral_constant_expression_p;
6335 if (allow_non_constant_p)
6336 *non_constant_p = parser->non_integral_constant_expression_p;
6337 else if (parser->non_integral_constant_expression_p)
6338 expression = error_mark_node;
6339 parser->non_integral_constant_expression_p
6340 = saved_non_integral_constant_expression_p;
6345 /* Parse __builtin_offsetof.
6347 offsetof-expression:
6348 "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6350 offsetof-member-designator:
6352 | offsetof-member-designator "." id-expression
6353 | offsetof-member-designator "[" expression "]" */
6356 cp_parser_builtin_offsetof (cp_parser *parser)
6358 int save_ice_p, save_non_ice_p;
6362 /* We're about to accept non-integral-constant things, but will
6363 definitely yield an integral constant expression. Save and
6364 restore these values around our local parsing. */
6365 save_ice_p = parser->integral_constant_expression_p;
6366 save_non_ice_p = parser->non_integral_constant_expression_p;
6368 /* Consume the "__builtin_offsetof" token. */
6369 cp_lexer_consume_token (parser->lexer);
6370 /* Consume the opening `('. */
6371 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6372 /* Parse the type-id. */
6373 type = cp_parser_type_id (parser);
6374 /* Look for the `,'. */
6375 cp_parser_require (parser, CPP_COMMA, "`,'");
6377 /* Build the (type *)null that begins the traditional offsetof macro. */
6378 expr = build_static_cast (build_pointer_type (type), null_pointer_node);
6380 /* Parse the offsetof-member-designator. We begin as if we saw "expr->". */
6381 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6385 cp_token *token = cp_lexer_peek_token (parser->lexer);
6386 switch (token->type)
6388 case CPP_OPEN_SQUARE:
6389 /* offsetof-member-designator "[" expression "]" */
6390 expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6394 /* offsetof-member-designator "." identifier */
6395 cp_lexer_consume_token (parser->lexer);
6396 expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
6400 case CPP_CLOSE_PAREN:
6401 /* Consume the ")" token. */
6402 cp_lexer_consume_token (parser->lexer);
6406 /* Error. We know the following require will fail, but
6407 that gives the proper error message. */
6408 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6409 cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6410 expr = error_mark_node;
6416 /* If we're processing a template, we can't finish the semantics yet.
6417 Otherwise we can fold the entire expression now. */
6418 if (processing_template_decl)
6419 expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6421 expr = finish_offsetof (expr);
6424 parser->integral_constant_expression_p = save_ice_p;
6425 parser->non_integral_constant_expression_p = save_non_ice_p;
6430 /* Parse a trait expression. */
6433 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6436 tree type1, type2 = NULL_TREE;
6437 bool binary = false;
6438 cp_decl_specifier_seq decl_specs;
6442 case RID_HAS_NOTHROW_ASSIGN:
6443 kind = CPTK_HAS_NOTHROW_ASSIGN;
6445 case RID_HAS_NOTHROW_CONSTRUCTOR:
6446 kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6448 case RID_HAS_NOTHROW_COPY:
6449 kind = CPTK_HAS_NOTHROW_COPY;
6451 case RID_HAS_TRIVIAL_ASSIGN:
6452 kind = CPTK_HAS_TRIVIAL_ASSIGN;
6454 case RID_HAS_TRIVIAL_CONSTRUCTOR:
6455 kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6457 case RID_HAS_TRIVIAL_COPY:
6458 kind = CPTK_HAS_TRIVIAL_COPY;
6460 case RID_HAS_TRIVIAL_DESTRUCTOR:
6461 kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6463 case RID_HAS_VIRTUAL_DESTRUCTOR:
6464 kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6466 case RID_IS_ABSTRACT:
6467 kind = CPTK_IS_ABSTRACT;
6469 case RID_IS_BASE_OF:
6470 kind = CPTK_IS_BASE_OF;
6474 kind = CPTK_IS_CLASS;
6476 case RID_IS_CONVERTIBLE_TO:
6477 kind = CPTK_IS_CONVERTIBLE_TO;
6481 kind = CPTK_IS_EMPTY;
6484 kind = CPTK_IS_ENUM;
6489 case RID_IS_POLYMORPHIC:
6490 kind = CPTK_IS_POLYMORPHIC;
6493 kind = CPTK_IS_UNION;
6499 /* Consume the token. */
6500 cp_lexer_consume_token (parser->lexer);
6502 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6504 type1 = cp_parser_type_id (parser);
6506 if (type1 == error_mark_node)
6507 return error_mark_node;
6509 /* Build a trivial decl-specifier-seq. */
6510 clear_decl_specs (&decl_specs);
6511 decl_specs.type = type1;
6513 /* Call grokdeclarator to figure out what type this is. */
6514 type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6515 /*initialized=*/0, /*attrlist=*/NULL);
6519 cp_parser_require (parser, CPP_COMMA, "`,'");
6521 type2 = cp_parser_type_id (parser);
6523 if (type2 == error_mark_node)
6524 return error_mark_node;
6526 /* Build a trivial decl-specifier-seq. */
6527 clear_decl_specs (&decl_specs);
6528 decl_specs.type = type2;
6530 /* Call grokdeclarator to figure out what type this is. */
6531 type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6532 /*initialized=*/0, /*attrlist=*/NULL);
6535 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6537 /* Complete the trait expression, which may mean either processing
6538 the trait expr now or saving it for template instantiation. */
6539 return finish_trait_expr (kind, type1, type2);
6542 /* Statements [gram.stmt.stmt] */
6544 /* Parse a statement.
6548 expression-statement
6553 declaration-statement
6556 IN_COMPOUND is true when the statement is nested inside a
6557 cp_parser_compound_statement; this matters for certain pragmas.
6559 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6560 is a (possibly labeled) if statement which is not enclosed in braces
6561 and has an else clause. This is used to implement -Wparentheses. */
6564 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
6565 bool in_compound, bool *if_p)
6569 location_t statement_location;
6574 /* There is no statement yet. */
6575 statement = NULL_TREE;
6576 /* Peek at the next token. */
6577 token = cp_lexer_peek_token (parser->lexer);
6578 /* Remember the location of the first token in the statement. */
6579 statement_location = token->location;
6580 /* If this is a keyword, then that will often determine what kind of
6581 statement we have. */
6582 if (token->type == CPP_KEYWORD)
6584 enum rid keyword = token->keyword;
6590 /* Looks like a labeled-statement with a case label.
6591 Parse the label, and then use tail recursion to parse
6593 cp_parser_label_for_labeled_statement (parser);
6598 statement = cp_parser_selection_statement (parser, if_p);
6604 statement = cp_parser_iteration_statement (parser);
6611 statement = cp_parser_jump_statement (parser);
6614 /* Objective-C++ exception-handling constructs. */
6617 case RID_AT_FINALLY:
6618 case RID_AT_SYNCHRONIZED:
6620 statement = cp_parser_objc_statement (parser);
6624 statement = cp_parser_try_block (parser);
6628 /* This must be a namespace alias definition. */
6629 cp_parser_declaration_statement (parser);
6633 /* It might be a keyword like `int' that can start a
6634 declaration-statement. */
6638 else if (token->type == CPP_NAME)
6640 /* If the next token is a `:', then we are looking at a
6641 labeled-statement. */
6642 token = cp_lexer_peek_nth_token (parser->lexer, 2);
6643 if (token->type == CPP_COLON)
6645 /* Looks like a labeled-statement with an ordinary label.
6646 Parse the label, and then use tail recursion to parse
6648 cp_parser_label_for_labeled_statement (parser);
6652 /* Anything that starts with a `{' must be a compound-statement. */
6653 else if (token->type == CPP_OPEN_BRACE)
6654 statement = cp_parser_compound_statement (parser, NULL, false);
6655 /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6656 a statement all its own. */
6657 else if (token->type == CPP_PRAGMA)
6659 /* Only certain OpenMP pragmas are attached to statements, and thus
6660 are considered statements themselves. All others are not. In
6661 the context of a compound, accept the pragma as a "statement" and
6662 return so that we can check for a close brace. Otherwise we
6663 require a real statement and must go back and read one. */
6665 cp_parser_pragma (parser, pragma_compound);
6666 else if (!cp_parser_pragma (parser, pragma_stmt))
6670 else if (token->type == CPP_EOF)
6672 cp_parser_error (parser, "expected statement");
6676 /* Everything else must be a declaration-statement or an
6677 expression-statement. Try for the declaration-statement
6678 first, unless we are looking at a `;', in which case we know that
6679 we have an expression-statement. */
6682 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6684 cp_parser_parse_tentatively (parser);
6685 /* Try to parse the declaration-statement. */
6686 cp_parser_declaration_statement (parser);
6687 /* If that worked, we're done. */
6688 if (cp_parser_parse_definitely (parser))
6691 /* Look for an expression-statement instead. */
6692 statement = cp_parser_expression_statement (parser, in_statement_expr);
6695 /* Set the line number for the statement. */
6696 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6697 SET_EXPR_LOCATION (statement, statement_location);
6700 /* Parse the label for a labeled-statement, i.e.
6703 case constant-expression :
6707 case constant-expression ... constant-expression : statement
6709 When a label is parsed without errors, the label is added to the
6710 parse tree by the finish_* functions, so this function doesn't
6711 have to return the label. */
6714 cp_parser_label_for_labeled_statement (cp_parser* parser)
6718 /* The next token should be an identifier. */
6719 token = cp_lexer_peek_token (parser->lexer);
6720 if (token->type != CPP_NAME
6721 && token->type != CPP_KEYWORD)
6723 cp_parser_error (parser, "expected labeled-statement");
6727 switch (token->keyword)
6734 /* Consume the `case' token. */
6735 cp_lexer_consume_token (parser->lexer);
6736 /* Parse the constant-expression. */
6737 expr = cp_parser_constant_expression (parser,
6738 /*allow_non_constant_p=*/false,
6741 ellipsis = cp_lexer_peek_token (parser->lexer);
6742 if (ellipsis->type == CPP_ELLIPSIS)
6744 /* Consume the `...' token. */
6745 cp_lexer_consume_token (parser->lexer);
6747 cp_parser_constant_expression (parser,
6748 /*allow_non_constant_p=*/false,
6750 /* We don't need to emit warnings here, as the common code
6751 will do this for us. */
6754 expr_hi = NULL_TREE;
6756 if (parser->in_switch_statement_p)
6757 finish_case_label (expr, expr_hi);
6759 error ("case label %qE not within a switch statement", expr);
6764 /* Consume the `default' token. */
6765 cp_lexer_consume_token (parser->lexer);
6767 if (parser->in_switch_statement_p)
6768 finish_case_label (NULL_TREE, NULL_TREE);
6770 error ("case label not within a switch statement");
6774 /* Anything else must be an ordinary label. */
6775 finish_label_stmt (cp_parser_identifier (parser));
6779 /* Require the `:' token. */
6780 cp_parser_require (parser, CPP_COLON, "`:'");
6783 /* Parse an expression-statement.
6785 expression-statement:
6788 Returns the new EXPR_STMT -- or NULL_TREE if the expression
6789 statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6790 indicates whether this expression-statement is part of an
6791 expression statement. */
6794 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6796 tree statement = NULL_TREE;
6798 /* If the next token is a ';', then there is no expression
6800 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6801 statement = cp_parser_expression (parser, /*cast_p=*/false);
6803 /* Consume the final `;'. */
6804 cp_parser_consume_semicolon_at_end_of_statement (parser);
6806 if (in_statement_expr
6807 && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6808 /* This is the final expression statement of a statement
6810 statement = finish_stmt_expr_expr (statement, in_statement_expr);
6812 statement = finish_expr_stmt (statement);
6819 /* Parse a compound-statement.
6822 { statement-seq [opt] }
6827 { label-declaration-seq [opt] statement-seq [opt] }
6829 label-declaration-seq:
6831 label-declaration-seq label-declaration
6833 Returns a tree representing the statement. */
6836 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6841 /* Consume the `{'. */
6842 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6843 return error_mark_node;
6844 /* Begin the compound-statement. */
6845 compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6846 /* If the next keyword is `__label__' we have a label declaration. */
6847 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
6848 cp_parser_label_declaration (parser);
6849 /* Parse an (optional) statement-seq. */
6850 cp_parser_statement_seq_opt (parser, in_statement_expr);
6851 /* Finish the compound-statement. */
6852 finish_compound_stmt (compound_stmt);
6853 /* Consume the `}'. */
6854 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6856 return compound_stmt;
6859 /* Parse an (optional) statement-seq.
6863 statement-seq [opt] statement */
6866 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6868 /* Scan statements until there aren't any more. */
6871 cp_token *token = cp_lexer_peek_token (parser->lexer);
6873 /* If we're looking at a `}', then we've run out of statements. */
6874 if (token->type == CPP_CLOSE_BRACE
6875 || token->type == CPP_EOF
6876 || token->type == CPP_PRAGMA_EOL)
6879 /* If we are in a compound statement and find 'else' then
6880 something went wrong. */
6881 else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
6883 if (parser->in_statement & IN_IF_STMT)
6887 token = cp_lexer_consume_token (parser->lexer);
6888 error ("%<else%> without a previous %<if%>");
6892 /* Parse the statement. */
6893 cp_parser_statement (parser, in_statement_expr, true, NULL);
6897 /* Parse a selection-statement.
6899 selection-statement:
6900 if ( condition ) statement
6901 if ( condition ) statement else statement
6902 switch ( condition ) statement
6904 Returns the new IF_STMT or SWITCH_STMT.
6906 If IF_P is not NULL, *IF_P is set to indicate whether the statement
6907 is a (possibly labeled) if statement which is not enclosed in
6908 braces and has an else clause. This is used to implement
6912 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
6920 /* Peek at the next token. */
6921 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6923 /* See what kind of keyword it is. */
6924 keyword = token->keyword;
6933 /* Look for the `('. */
6934 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6936 cp_parser_skip_to_end_of_statement (parser);
6937 return error_mark_node;
6940 /* Begin the selection-statement. */
6941 if (keyword == RID_IF)
6942 statement = begin_if_stmt ();
6944 statement = begin_switch_stmt ();
6946 /* Parse the condition. */
6947 condition = cp_parser_condition (parser);
6948 /* Look for the `)'. */
6949 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6950 cp_parser_skip_to_closing_parenthesis (parser, true, false,
6951 /*consume_paren=*/true);
6953 if (keyword == RID_IF)
6956 unsigned char in_statement;
6958 /* Add the condition. */
6959 finish_if_stmt_cond (condition, statement);
6961 /* Parse the then-clause. */
6962 in_statement = parser->in_statement;
6963 parser->in_statement |= IN_IF_STMT;
6964 cp_parser_implicitly_scoped_statement (parser, &nested_if);
6965 parser->in_statement = in_statement;
6967 finish_then_clause (statement);
6969 /* If the next token is `else', parse the else-clause. */
6970 if (cp_lexer_next_token_is_keyword (parser->lexer,
6973 /* Consume the `else' keyword. */
6974 cp_lexer_consume_token (parser->lexer);
6975 begin_else_clause (statement);
6976 /* Parse the else-clause. */
6977 cp_parser_implicitly_scoped_statement (parser, NULL);
6978 finish_else_clause (statement);
6980 /* If we are currently parsing a then-clause, then
6981 IF_P will not be NULL. We set it to true to
6982 indicate that this if statement has an else clause.
6983 This may trigger the Wparentheses warning below
6984 when we get back up to the parent if statement. */
6990 /* This if statement does not have an else clause. If
6991 NESTED_IF is true, then the then-clause is an if
6992 statement which does have an else clause. We warn
6993 about the potential ambiguity. */
6995 warning (OPT_Wparentheses,
6996 ("%Hsuggest explicit braces "
6997 "to avoid ambiguous %<else%>"),
6998 EXPR_LOCUS (statement));
7001 /* Now we're all done with the if-statement. */
7002 finish_if_stmt (statement);
7006 bool in_switch_statement_p;
7007 unsigned char in_statement;
7009 /* Add the condition. */
7010 finish_switch_cond (condition, statement);
7012 /* Parse the body of the switch-statement. */
7013 in_switch_statement_p = parser->in_switch_statement_p;
7014 in_statement = parser->in_statement;
7015 parser->in_switch_statement_p = true;
7016 parser->in_statement |= IN_SWITCH_STMT;
7017 cp_parser_implicitly_scoped_statement (parser, NULL);
7018 parser->in_switch_statement_p = in_switch_statement_p;
7019 parser->in_statement = in_statement;
7021 /* Now we're all done with the switch-statement. */
7022 finish_switch_stmt (statement);
7030 cp_parser_error (parser, "expected selection-statement");
7031 return error_mark_node;
7035 /* Parse a condition.
7039 type-specifier-seq declarator = assignment-expression
7044 type-specifier-seq declarator asm-specification [opt]
7045 attributes [opt] = assignment-expression
7047 Returns the expression that should be tested. */
7050 cp_parser_condition (cp_parser* parser)
7052 cp_decl_specifier_seq type_specifiers;
7053 const char *saved_message;
7055 /* Try the declaration first. */
7056 cp_parser_parse_tentatively (parser);
7057 /* New types are not allowed in the type-specifier-seq for a
7059 saved_message = parser->type_definition_forbidden_message;
7060 parser->type_definition_forbidden_message
7061 = "types may not be defined in conditions";
7062 /* Parse the type-specifier-seq. */
7063 cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
7065 /* Restore the saved message. */
7066 parser->type_definition_forbidden_message = saved_message;
7067 /* If all is well, we might be looking at a declaration. */
7068 if (!cp_parser_error_occurred (parser))
7071 tree asm_specification;
7073 cp_declarator *declarator;
7074 tree initializer = NULL_TREE;
7076 /* Parse the declarator. */
7077 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
7078 /*ctor_dtor_or_conv_p=*/NULL,
7079 /*parenthesized_p=*/NULL,
7080 /*member_p=*/false);
7081 /* Parse the attributes. */
7082 attributes = cp_parser_attributes_opt (parser);
7083 /* Parse the asm-specification. */
7084 asm_specification = cp_parser_asm_specification_opt (parser);
7085 /* If the next token is not an `=', then we might still be
7086 looking at an expression. For example:
7090 looks like a decl-specifier-seq and a declarator -- but then
7091 there is no `=', so this is an expression. */
7092 cp_parser_require (parser, CPP_EQ, "`='");
7093 /* If we did see an `=', then we are looking at a declaration
7095 if (cp_parser_parse_definitely (parser))
7098 bool non_constant_p;
7100 /* Create the declaration. */
7101 decl = start_decl (declarator, &type_specifiers,
7102 /*initialized_p=*/true,
7103 attributes, /*prefix_attributes=*/NULL_TREE,
7105 /* Parse the assignment-expression. */
7107 = cp_parser_constant_expression (parser,
7108 /*allow_non_constant_p=*/true,
7110 if (!non_constant_p)
7111 initializer = fold_non_dependent_expr (initializer);
7113 /* Process the initializer. */
7114 cp_finish_decl (decl,
7115 initializer, !non_constant_p,
7117 LOOKUP_ONLYCONVERTING);
7120 pop_scope (pushed_scope);
7122 return convert_from_reference (decl);
7125 /* If we didn't even get past the declarator successfully, we are
7126 definitely not looking at a declaration. */
7128 cp_parser_abort_tentative_parse (parser);
7130 /* Otherwise, we are looking at an expression. */
7131 return cp_parser_expression (parser, /*cast_p=*/false);
7134 /* We check for a ) immediately followed by ; with no whitespacing
7135 between. This is used to issue a warning for:
7143 as the semicolon is probably extraneous.
7145 On parse errors, the next token might not be a ), so do nothing in
7149 check_empty_body (cp_parser* parser, const char* type)
7152 cp_token *close_paren;
7153 expanded_location close_loc;
7154 expanded_location semi_loc;
7156 close_paren = cp_lexer_peek_token (parser->lexer);
7157 if (close_paren->type != CPP_CLOSE_PAREN)
7160 close_loc = expand_location (close_paren->location);
7161 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7163 if (token->type != CPP_SEMICOLON
7164 || (token->flags & PREV_WHITE))
7167 semi_loc = expand_location (token->location);
7168 if (close_loc.line == semi_loc.line
7169 #ifdef USE_MAPPED_LOCATION
7170 && close_loc.column+1 == semi_loc.column
7173 warning (OPT_Wempty_body,
7174 "suggest a space before %<;%> or explicit braces around empty "
7175 "body in %<%s%> statement",
7179 /* Parse an iteration-statement.
7181 iteration-statement:
7182 while ( condition ) statement
7183 do statement while ( expression ) ;
7184 for ( for-init-statement condition [opt] ; expression [opt] )
7187 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
7190 cp_parser_iteration_statement (cp_parser* parser)
7195 unsigned char in_statement;
7197 /* Peek at the next token. */
7198 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
7200 return error_mark_node;
7202 /* Remember whether or not we are already within an iteration
7204 in_statement = parser->in_statement;
7206 /* See what kind of keyword it is. */
7207 keyword = token->keyword;
7214 /* Begin the while-statement. */
7215 statement = begin_while_stmt ();
7216 /* Look for the `('. */
7217 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7218 /* Parse the condition. */
7219 condition = cp_parser_condition (parser);
7220 finish_while_stmt_cond (condition, statement);
7221 check_empty_body (parser, "while");
7222 /* Look for the `)'. */
7223 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7224 /* Parse the dependent statement. */
7225 parser->in_statement = IN_ITERATION_STMT;
7226 cp_parser_already_scoped_statement (parser);
7227 parser->in_statement = in_statement;
7228 /* We're done with the while-statement. */
7229 finish_while_stmt (statement);
7237 /* Begin the do-statement. */
7238 statement = begin_do_stmt ();
7239 /* Parse the body of the do-statement. */
7240 parser->in_statement = IN_ITERATION_STMT;
7241 cp_parser_implicitly_scoped_statement (parser, NULL);
7242 parser->in_statement = in_statement;
7243 finish_do_body (statement);
7244 /* Look for the `while' keyword. */
7245 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
7246 /* Look for the `('. */
7247 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7248 /* Parse the expression. */
7249 expression = cp_parser_expression (parser, /*cast_p=*/false);
7250 /* We're done with the do-statement. */
7251 finish_do_stmt (expression, statement);
7252 /* Look for the `)'. */
7253 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7254 /* Look for the `;'. */
7255 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7261 tree condition = NULL_TREE;
7262 tree expression = NULL_TREE;
7264 /* Begin the for-statement. */
7265 statement = begin_for_stmt ();
7266 /* Look for the `('. */
7267 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7268 /* Parse the initialization. */
7269 cp_parser_for_init_statement (parser);
7270 finish_for_init_stmt (statement);
7272 /* If there's a condition, process it. */
7273 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7274 condition = cp_parser_condition (parser);
7275 finish_for_cond (condition, statement);
7276 /* Look for the `;'. */
7277 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7279 /* If there's an expression, process it. */
7280 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
7281 expression = cp_parser_expression (parser, /*cast_p=*/false);
7282 finish_for_expr (expression, statement);
7283 check_empty_body (parser, "for");
7284 /* Look for the `)'. */
7285 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7287 /* Parse the body of the for-statement. */
7288 parser->in_statement = IN_ITERATION_STMT;
7289 cp_parser_already_scoped_statement (parser);
7290 parser->in_statement = in_statement;
7292 /* We're done with the for-statement. */
7293 finish_for_stmt (statement);
7298 cp_parser_error (parser, "expected iteration-statement");
7299 statement = error_mark_node;
7306 /* Parse a for-init-statement.
7309 expression-statement
7310 simple-declaration */
7313 cp_parser_for_init_statement (cp_parser* parser)
7315 /* If the next token is a `;', then we have an empty
7316 expression-statement. Grammatically, this is also a
7317 simple-declaration, but an invalid one, because it does not
7318 declare anything. Therefore, if we did not handle this case
7319 specially, we would issue an error message about an invalid
7321 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7323 /* We're going to speculatively look for a declaration, falling back
7324 to an expression, if necessary. */
7325 cp_parser_parse_tentatively (parser);
7326 /* Parse the declaration. */
7327 cp_parser_simple_declaration (parser,
7328 /*function_definition_allowed_p=*/false);
7329 /* If the tentative parse failed, then we shall need to look for an
7330 expression-statement. */
7331 if (cp_parser_parse_definitely (parser))
7335 cp_parser_expression_statement (parser, false);
7338 /* Parse a jump-statement.
7343 return expression [opt] ;
7351 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR. */
7354 cp_parser_jump_statement (cp_parser* parser)
7356 tree statement = error_mark_node;
7359 unsigned char in_statement;
7361 /* Peek at the next token. */
7362 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
7364 return error_mark_node;
7366 /* See what kind of keyword it is. */
7367 keyword = token->keyword;
7371 in_statement = parser->in_statement & ~IN_IF_STMT;
7372 switch (in_statement)
7375 error ("break statement not within loop or switch");
7378 gcc_assert ((in_statement & IN_SWITCH_STMT)
7379 || in_statement == IN_ITERATION_STMT);
7380 statement = finish_break_stmt ();
7383 error ("invalid exit from OpenMP structured block");
7386 error ("break statement used with OpenMP for loop");
7389 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7393 switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
7396 error ("continue statement not within a loop");
7398 case IN_ITERATION_STMT:
7400 statement = finish_continue_stmt ();
7403 error ("invalid exit from OpenMP structured block");
7408 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7415 /* If the next token is a `;', then there is no
7417 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7418 expr = cp_parser_expression (parser, /*cast_p=*/false);
7421 /* Build the return-statement. */
7422 statement = finish_return_stmt (expr);
7423 /* Look for the final `;'. */
7424 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7429 /* Create the goto-statement. */
7430 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
7432 /* Issue a warning about this use of a GNU extension. */
7434 pedwarn ("ISO C++ forbids computed gotos");
7435 /* Consume the '*' token. */
7436 cp_lexer_consume_token (parser->lexer);
7437 /* Parse the dependent expression. */
7438 finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
7441 finish_goto_stmt (cp_parser_identifier (parser));
7442 /* Look for the final `;'. */
7443 cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7447 cp_parser_error (parser, "expected jump-statement");
7454 /* Parse a declaration-statement.
7456 declaration-statement:
7457 block-declaration */
7460 cp_parser_declaration_statement (cp_parser* parser)
7464 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7465 p = obstack_alloc (&declarator_obstack, 0);
7467 /* Parse the block-declaration. */
7468 cp_parser_block_declaration (parser, /*statement_p=*/true);
7470 /* Free any declarators allocated. */
7471 obstack_free (&declarator_obstack, p);
7473 /* Finish off the statement. */
7477 /* Some dependent statements (like `if (cond) statement'), are
7478 implicitly in their own scope. In other words, if the statement is
7479 a single statement (as opposed to a compound-statement), it is
7480 none-the-less treated as if it were enclosed in braces. Any
7481 declarations appearing in the dependent statement are out of scope
7482 after control passes that point. This function parses a statement,
7483 but ensures that is in its own scope, even if it is not a
7486 If IF_P is not NULL, *IF_P is set to indicate whether the statement
7487 is a (possibly labeled) if statement which is not enclosed in
7488 braces and has an else clause. This is used to implement
7491 Returns the new statement. */
7494 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
7501 /* Mark if () ; with a special NOP_EXPR. */
7502 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7504 cp_lexer_consume_token (parser->lexer);
7505 statement = add_stmt (build_empty_stmt ());
7507 /* if a compound is opened, we simply parse the statement directly. */
7508 else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7509 statement = cp_parser_compound_statement (parser, NULL, false);
7510 /* If the token is not a `{', then we must take special action. */
7513 /* Create a compound-statement. */
7514 statement = begin_compound_stmt (0);
7515 /* Parse the dependent-statement. */
7516 cp_parser_statement (parser, NULL_TREE, false, if_p);
7517 /* Finish the dummy compound-statement. */
7518 finish_compound_stmt (statement);
7521 /* Return the statement. */
7525 /* For some dependent statements (like `while (cond) statement'), we
7526 have already created a scope. Therefore, even if the dependent
7527 statement is a compound-statement, we do not want to create another
7531 cp_parser_already_scoped_statement (cp_parser* parser)
7533 /* If the token is a `{', then we must take special action. */
7534 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
7535 cp_parser_statement (parser, NULL_TREE, false, NULL);
7538 /* Avoid calling cp_parser_compound_statement, so that we
7539 don't create a new scope. Do everything else by hand. */
7540 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
7541 cp_parser_statement_seq_opt (parser, NULL_TREE);
7542 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7546 /* Declarations [gram.dcl.dcl] */
7548 /* Parse an optional declaration-sequence.
7552 declaration-seq declaration */
7555 cp_parser_declaration_seq_opt (cp_parser* parser)
7561 token = cp_lexer_peek_token (parser->lexer);
7563 if (token->type == CPP_CLOSE_BRACE
7564 || token->type == CPP_EOF
7565 || token->type == CPP_PRAGMA_EOL)
7568 if (token->type == CPP_SEMICOLON)
7570 /* A declaration consisting of a single semicolon is
7571 invalid. Allow it unless we're being pedantic. */
7572 cp_lexer_consume_token (parser->lexer);
7573 if (pedantic && !in_system_header)
7574 pedwarn ("extra %<;%>");
7578 /* If we're entering or exiting a region that's implicitly
7579 extern "C", modify the lang context appropriately. */
7580 if (!parser->implicit_extern_c && token->implicit_extern_c)
7582 push_lang_context (lang_name_c);
7583 parser->implicit_extern_c = true;
7585 else if (parser->implicit_extern_c && !token->implicit_extern_c)
7587 pop_lang_context ();
7588 parser->implicit_extern_c = false;
7591 if (token->type == CPP_PRAGMA)
7593 /* A top-level declaration can consist solely of a #pragma.
7594 A nested declaration cannot, so this is done here and not
7595 in cp_parser_declaration. (A #pragma at block scope is
7596 handled in cp_parser_statement.) */
7597 cp_parser_pragma (parser, pragma_external);
7601 /* Parse the declaration itself. */
7602 cp_parser_declaration (parser);
7606 /* Parse a declaration.
7611 template-declaration
7612 explicit-instantiation
7613 explicit-specialization
7614 linkage-specification
7615 namespace-definition
7620 __extension__ declaration */
7623 cp_parser_declaration (cp_parser* parser)
7630 /* Check for the `__extension__' keyword. */
7631 if (cp_parser_extension_opt (parser, &saved_pedantic))
7633 /* Parse the qualified declaration. */
7634 cp_parser_declaration (parser);
7635 /* Restore the PEDANTIC flag. */
7636 pedantic = saved_pedantic;
7641 /* Try to figure out what kind of declaration is present. */
7642 token1 = *cp_lexer_peek_token (parser->lexer);
7644 if (token1.type != CPP_EOF)
7645 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
7648 token2.type = CPP_EOF;
7649 token2.keyword = RID_MAX;
7652 /* Get the high-water mark for the DECLARATOR_OBSTACK. */
7653 p = obstack_alloc (&declarator_obstack, 0);
7655 /* If the next token is `extern' and the following token is a string
7656 literal, then we have a linkage specification. */
7657 if (token1.keyword == RID_EXTERN
7658 && cp_parser_is_string_literal (&token2))
7659 cp_parser_linkage_specification (parser);
7660 /* If the next token is `template', then we have either a template
7661 declaration, an explicit instantiation, or an explicit
7663 else if (token1.keyword == RID_TEMPLATE)
7665 /* `template <>' indicates a template specialization. */
7666 if (token2.type == CPP_LESS
7667 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
7668 cp_parser_explicit_specialization (parser);
7669 /* `template <' indicates a template declaration. */
7670 else if (token2.type == CPP_LESS)
7671 cp_parser_template_declaration (parser, /*member_p=*/false);
7672 /* Anything else must be an explicit instantiation. */
7674 cp_parser_explicit_instantiation (parser);
7676 /* If the next token is `export', then we have a template
7678 else if (token1.keyword == RID_EXPORT)
7679 cp_parser_template_declaration (parser, /*member_p=*/false);
7680 /* If the next token is `extern', 'static' or 'inline' and the one
7681 after that is `template', we have a GNU extended explicit
7682 instantiation directive. */
7683 else if (cp_parser_allow_gnu_extensions_p (parser)
7684 && (token1.keyword == RID_EXTERN
7685 || token1.keyword == RID_STATIC
7686 || token1.keyword == RID_INLINE)
7687 && token2.keyword == RID_TEMPLATE)
7688 cp_parser_explicit_instantiation (parser);
7689 /* If the next token is `namespace', check for a named or unnamed
7690 namespace definition. */
7691 else if (token1.keyword == RID_NAMESPACE
7692 && (/* A named namespace definition. */
7693 (token2.type == CPP_NAME
7694 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
7696 /* An unnamed namespace definition. */
7697 || token2.type == CPP_OPEN_BRACE
7698 || token2.keyword == RID_ATTRIBUTE))
7699 cp_parser_namespace_definition (parser);
7700 /* Objective-C++ declaration/definition. */
7701 else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
7702 cp_parser_objc_declaration (parser);
7703 /* We must have either a block declaration or a function
7706 /* Try to parse a block-declaration, or a function-definition. */
7707 cp_parser_block_declaration (parser, /*statement_p=*/false);
7709 /* Free any declarators allocated. */
7710 obstack_free (&declarator_obstack, p);
7713 /* Parse a block-declaration.
7718 namespace-alias-definition
7725 __extension__ block-declaration
7730 static_assert-declaration
7732 If STATEMENT_P is TRUE, then this block-declaration is occurring as
7733 part of a declaration-statement. */
7736 cp_parser_block_declaration (cp_parser *parser,
7742 /* Check for the `__extension__' keyword. */
7743 if (cp_parser_extension_opt (parser, &saved_pedantic))
7745 /* Parse the qualified declaration. */
7746 cp_parser_block_declaration (parser, statement_p);
7747 /* Restore the PEDANTIC flag. */
7748 pedantic = saved_pedantic;
7753 /* Peek at the next token to figure out which kind of declaration is
7755 token1 = cp_lexer_peek_token (parser->lexer);
7757 /* If the next keyword is `asm', we have an asm-definition. */
7758 if (token1->keyword == RID_ASM)
7761 cp_parser_commit_to_tentative_parse (parser);
7762 cp_parser_asm_definition (parser);
7764 /* If the next keyword is `namespace', we have a
7765 namespace-alias-definition. */
7766 else if (token1->keyword == RID_NAMESPACE)
7767 cp_parser_namespace_alias_definition (parser);
7768 /* If the next keyword is `using', we have either a
7769 using-declaration or a using-directive. */
7770 else if (token1->keyword == RID_USING)
7775 cp_parser_commit_to_tentative_parse (parser);
7776 /* If the token after `using' is `namespace', then we have a
7778 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7779 if (token2->keyword == RID_NAMESPACE)
7780 cp_parser_using_directive (parser);
7781 /* Otherwise, it's a using-declaration. */
7783 cp_parser_using_declaration (parser,
7784 /*access_declaration_p=*/false);
7786 /* If the next keyword is `__label__' we have a misplaced label
7788 else if (token1->keyword == RID_LABEL)
7790 cp_lexer_consume_token (parser->lexer);
7791 error ("%<__label__%> not at the beginning of a block");
7792 cp_parser_skip_to_end_of_statement (parser);
7793 /* If the next token is now a `;', consume it. */
7794 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7795 cp_lexer_consume_token (parser->lexer);
7797 /* If the next token is `static_assert' we have a static assertion. */
7798 else if (token1->keyword == RID_STATIC_ASSERT)
7799 cp_parser_static_assert (parser, /*member_p=*/false);
7800 /* Anything else must be a simple-declaration. */
7802 cp_parser_simple_declaration (parser, !statement_p);
7805 /* Parse a simple-declaration.
7808 decl-specifier-seq [opt] init-declarator-list [opt] ;
7810 init-declarator-list:
7812 init-declarator-list , init-declarator
7814 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7815 function-definition as a simple-declaration. */
7818 cp_parser_simple_declaration (cp_parser* parser,
7819 bool function_definition_allowed_p)
7821 cp_decl_specifier_seq decl_specifiers;
7822 int declares_class_or_enum;
7823 bool saw_declarator;
7825 /* Defer access checks until we know what is being declared; the
7826 checks for names appearing in the decl-specifier-seq should be
7827 done as if we were in the scope of the thing being declared. */
7828 push_deferring_access_checks (dk_deferred);
7830 /* Parse the decl-specifier-seq. We have to keep track of whether
7831 or not the decl-specifier-seq declares a named class or
7832 enumeration type, since that is the only case in which the
7833 init-declarator-list is allowed to be empty.
7837 In a simple-declaration, the optional init-declarator-list can be
7838 omitted only when declaring a class or enumeration, that is when
7839 the decl-specifier-seq contains either a class-specifier, an
7840 elaborated-type-specifier, or an enum-specifier. */
7841 cp_parser_decl_specifier_seq (parser,
7842 CP_PARSER_FLAGS_OPTIONAL,
7844 &declares_class_or_enum);
7845 /* We no longer need to defer access checks. */
7846 stop_deferring_access_checks ();
7848 /* In a block scope, a valid declaration must always have a
7849 decl-specifier-seq. By not trying to parse declarators, we can
7850 resolve the declaration/expression ambiguity more quickly. */
7851 if (!function_definition_allowed_p
7852 && !decl_specifiers.any_specifiers_p)
7854 cp_parser_error (parser, "expected declaration");
7858 /* If the next two tokens are both identifiers, the code is
7859 erroneous. The usual cause of this situation is code like:
7863 where "T" should name a type -- but does not. */
7864 if (!decl_specifiers.type
7865 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7867 /* If parsing tentatively, we should commit; we really are
7868 looking at a declaration. */
7869 cp_parser_commit_to_tentative_parse (parser);
7874 /* If we have seen at least one decl-specifier, and the next token
7875 is not a parenthesis, then we must be looking at a declaration.
7876 (After "int (" we might be looking at a functional cast.) */
7877 if (decl_specifiers.any_specifiers_p
7878 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7879 cp_parser_commit_to_tentative_parse (parser);
7881 /* Keep going until we hit the `;' at the end of the simple
7883 saw_declarator = false;
7884 while (cp_lexer_next_token_is_not (parser->lexer,
7888 bool function_definition_p;
7893 /* If we are processing next declarator, coma is expected */
7894 token = cp_lexer_peek_token (parser->lexer);
7895 gcc_assert (token->type == CPP_COMMA);
7896 cp_lexer_consume_token (parser->lexer);
7899 saw_declarator = true;
7901 /* Parse the init-declarator. */
7902 decl = cp_parser_init_declarator (parser, &decl_specifiers,
7904 function_definition_allowed_p,
7906 declares_class_or_enum,
7907 &function_definition_p);
7908 /* If an error occurred while parsing tentatively, exit quickly.
7909 (That usually happens when in the body of a function; each
7910 statement is treated as a declaration-statement until proven
7912 if (cp_parser_error_occurred (parser))
7914 /* Handle function definitions specially. */
7915 if (function_definition_p)
7917 /* If the next token is a `,', then we are probably
7918 processing something like:
7922 which is erroneous. */
7923 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7924 error ("mixing declarations and function-definitions is forbidden");
7925 /* Otherwise, we're done with the list of declarators. */
7928 pop_deferring_access_checks ();
7932 /* The next token should be either a `,' or a `;'. */
7933 token = cp_lexer_peek_token (parser->lexer);
7934 /* If it's a `,', there are more declarators to come. */
7935 if (token->type == CPP_COMMA)
7936 /* will be consumed next time around */;
7937 /* If it's a `;', we are done. */
7938 else if (token->type == CPP_SEMICOLON)
7940 /* Anything else is an error. */
7943 /* If we have already issued an error message we don't need
7944 to issue another one. */
7945 if (decl != error_mark_node
7946 || cp_parser_uncommitted_to_tentative_parse_p (parser))
7947 cp_parser_error (parser, "expected %<,%> or %<;%>");
7948 /* Skip tokens until we reach the end of the statement. */
7949 cp_parser_skip_to_end_of_statement (parser);
7950 /* If the next token is now a `;', consume it. */
7951 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7952 cp_lexer_consume_token (parser->lexer);
7955 /* After the first time around, a function-definition is not
7956 allowed -- even if it was OK at first. For example:
7961 function_definition_allowed_p = false;
7964 /* Issue an error message if no declarators are present, and the
7965 decl-specifier-seq does not itself declare a class or
7967 if (!saw_declarator)
7969 if (cp_parser_declares_only_class_p (parser))
7970 shadow_tag (&decl_specifiers);
7971 /* Perform any deferred access checks. */
7972 perform_deferred_access_checks ();
7975 /* Consume the `;'. */
7976 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7979 pop_deferring_access_checks ();
7982 /* Parse a decl-specifier-seq.
7985 decl-specifier-seq [opt] decl-specifier
7988 storage-class-specifier
7999 Set *DECL_SPECS to a representation of the decl-specifier-seq.
8001 The parser flags FLAGS is used to control type-specifier parsing.
8003 *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8006 1: one of the decl-specifiers is an elaborated-type-specifier
8007 (i.e., a type declaration)
8008 2: one of the decl-specifiers is an enum-specifier or a
8009 class-specifier (i.e., a type definition)
8014 cp_parser_decl_specifier_seq (cp_parser* parser,
8015 cp_parser_flags flags,
8016 cp_decl_specifier_seq *decl_specs,
8017 int* declares_class_or_enum)
8019 bool constructor_possible_p = !parser->in_declarator_p;
8021 /* Clear DECL_SPECS. */
8022 clear_decl_specs (decl_specs);
8024 /* Assume no class or enumeration type is declared. */
8025 *declares_class_or_enum = 0;
8027 /* Keep reading specifiers until there are no more to read. */
8031 bool found_decl_spec;
8034 /* Peek at the next token. */
8035 token = cp_lexer_peek_token (parser->lexer);
8036 /* Handle attributes. */
8037 if (token->keyword == RID_ATTRIBUTE)
8039 /* Parse the attributes. */
8040 decl_specs->attributes
8041 = chainon (decl_specs->attributes,
8042 cp_parser_attributes_opt (parser));
8045 /* Assume we will find a decl-specifier keyword. */
8046 found_decl_spec = true;
8047 /* If the next token is an appropriate keyword, we can simply
8048 add it to the list. */
8049 switch (token->keyword)
8054 if (!at_class_scope_p ())
8056 error ("%<friend%> used outside of class");
8057 cp_lexer_purge_token (parser->lexer);
8061 ++decl_specs->specs[(int) ds_friend];
8062 /* Consume the token. */
8063 cp_lexer_consume_token (parser->lexer);
8067 /* function-specifier:
8074 cp_parser_function_specifier_opt (parser, decl_specs);
8080 ++decl_specs->specs[(int) ds_typedef];
8081 /* Consume the token. */
8082 cp_lexer_consume_token (parser->lexer);
8083 /* A constructor declarator cannot appear in a typedef. */
8084 constructor_possible_p = false;
8085 /* The "typedef" keyword can only occur in a declaration; we
8086 may as well commit at this point. */
8087 cp_parser_commit_to_tentative_parse (parser);
8089 if (decl_specs->storage_class != sc_none)
8090 decl_specs->conflicting_specifiers_p = true;
8093 /* storage-class-specifier:
8107 /* Consume the token. */
8108 cp_lexer_consume_token (parser->lexer);
8109 cp_parser_set_storage_class (parser, decl_specs, token->keyword);
8112 /* Consume the token. */
8113 cp_lexer_consume_token (parser->lexer);
8114 ++decl_specs->specs[(int) ds_thread];
8118 /* We did not yet find a decl-specifier yet. */
8119 found_decl_spec = false;
8123 /* Constructors are a special case. The `S' in `S()' is not a
8124 decl-specifier; it is the beginning of the declarator. */
8127 && constructor_possible_p
8128 && (cp_parser_constructor_declarator_p
8129 (parser, decl_specs->specs[(int) ds_friend] != 0)));
8131 /* If we don't have a DECL_SPEC yet, then we must be looking at
8132 a type-specifier. */
8133 if (!found_decl_spec && !constructor_p)
8135 int decl_spec_declares_class_or_enum;
8136 bool is_cv_qualifier;
8140 = cp_parser_type_specifier (parser, flags,
8142 /*is_declaration=*/true,
8143 &decl_spec_declares_class_or_enum,
8146 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
8148 /* If this type-specifier referenced a user-defined type
8149 (a typedef, class-name, etc.), then we can't allow any
8150 more such type-specifiers henceforth.
8154 The longest sequence of decl-specifiers that could
8155 possibly be a type name is taken as the
8156 decl-specifier-seq of a declaration. The sequence shall
8157 be self-consistent as described below.
8161 As a general rule, at most one type-specifier is allowed
8162 in the complete decl-specifier-seq of a declaration. The
8163 only exceptions are the following:
8165 -- const or volatile can be combined with any other
8168 -- signed or unsigned can be combined with char, long,
8176 void g (const int Pc);
8178 Here, Pc is *not* part of the decl-specifier seq; it's
8179 the declarator. Therefore, once we see a type-specifier
8180 (other than a cv-qualifier), we forbid any additional
8181 user-defined types. We *do* still allow things like `int
8182 int' to be considered a decl-specifier-seq, and issue the
8183 error message later. */
8184 if (type_spec && !is_cv_qualifier)
8185 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
8186 /* A constructor declarator cannot follow a type-specifier. */
8189 constructor_possible_p = false;
8190 found_decl_spec = true;
8194 /* If we still do not have a DECL_SPEC, then there are no more
8196 if (!found_decl_spec)
8199 decl_specs->any_specifiers_p = true;
8200 /* After we see one decl-specifier, further decl-specifiers are
8202 flags |= CP_PARSER_FLAGS_OPTIONAL;
8205 cp_parser_check_decl_spec (decl_specs);
8207 /* Don't allow a friend specifier with a class definition. */
8208 if (decl_specs->specs[(int) ds_friend] != 0
8209 && (*declares_class_or_enum & 2))
8210 error ("class definition may not be declared a friend");
8213 /* Parse an (optional) storage-class-specifier.
8215 storage-class-specifier:
8224 storage-class-specifier:
8227 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
8230 cp_parser_storage_class_specifier_opt (cp_parser* parser)
8232 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8240 /* Consume the token. */
8241 return cp_lexer_consume_token (parser->lexer)->u.value;
8248 /* Parse an (optional) function-specifier.
8255 Returns an IDENTIFIER_NODE corresponding to the keyword used.
8256 Updates DECL_SPECS, if it is non-NULL. */
8259 cp_parser_function_specifier_opt (cp_parser* parser,
8260 cp_decl_specifier_seq *decl_specs)
8262 switch (cp_lexer_peek_token (parser->lexer)->keyword)
8266 ++decl_specs->specs[(int) ds_inline];
8270 /* 14.5.2.3 [temp.mem]
8272 A member function template shall not be virtual. */
8273 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
8274 error ("templates may not be %<virtual%>");
8275 else if (decl_specs)
8276 ++decl_specs->specs[(int) ds_virtual];
8281 ++decl_specs->specs[(int) ds_explicit];
8288 /* Consume the token. */
8289 return cp_lexer_consume_token (parser->lexer)->u.value;
8292 /* Parse a linkage-specification.
8294 linkage-specification:
8295 extern string-literal { declaration-seq [opt] }
8296 extern string-literal declaration */
8299 cp_parser_linkage_specification (cp_parser* parser)
8303 /* Look for the `extern' keyword. */
8304 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
8306 /* Look for the string-literal. */
8307 linkage = cp_parser_string_literal (parser, false, false);
8309 /* Transform the literal into an identifier. If the literal is a
8310 wide-character string, or contains embedded NULs, then we can't
8311 handle it as the user wants. */
8312 if (strlen (TREE_STRING_POINTER (linkage))
8313 != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
8315 cp_parser_error (parser, "invalid linkage-specification");
8316 /* Assume C++ linkage. */
8317 linkage = lang_name_cplusplus;
8320 linkage = get_identifier (TREE_STRING_POINTER (linkage));
8322 /* We're now using the new linkage. */
8323 push_lang_context (linkage);
8325 /* If the next token is a `{', then we're using the first
8327 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8329 /* Consume the `{' token. */
8330 cp_lexer_consume_token (parser->lexer);
8331 /* Parse the declarations. */
8332 cp_parser_declaration_seq_opt (parser);
8333 /* Look for the closing `}'. */
8334 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8336 /* Otherwise, there's just one declaration. */
8339 bool saved_in_unbraced_linkage_specification_p;
8341 saved_in_unbraced_linkage_specification_p
8342 = parser->in_unbraced_linkage_specification_p;
8343 parser->in_unbraced_linkage_specification_p = true;
8344 cp_parser_declaration (parser);
8345 parser->in_unbraced_linkage_specification_p
8346 = saved_in_unbraced_linkage_specification_p;
8349 /* We're done with the linkage-specification. */
8350 pop_lang_context ();
8353 /* Parse a static_assert-declaration.
8355 static_assert-declaration:
8356 static_assert ( constant-expression , string-literal ) ;
8358 If MEMBER_P, this static_assert is a class member. */
8361 cp_parser_static_assert(cp_parser *parser, bool member_p)
8366 location_t saved_loc;
8368 /* Peek at the `static_assert' token so we can keep track of exactly
8369 where the static assertion started. */
8370 token = cp_lexer_peek_token (parser->lexer);
8371 saved_loc = token->location;
8373 /* Look for the `static_assert' keyword. */
8374 if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT,
8378 /* We know we are in a static assertion; commit to any tentative
8380 if (cp_parser_parsing_tentatively (parser))
8381 cp_parser_commit_to_tentative_parse (parser);
8383 /* Parse the `(' starting the static assertion condition. */
8384 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8386 /* Parse the constant-expression. */
8388 cp_parser_constant_expression (parser,
8389 /*allow_non_constant_p=*/false,
8390 /*non_constant_p=*/NULL);
8392 /* Parse the separating `,'. */
8393 cp_parser_require (parser, CPP_COMMA, "`,'");
8395 /* Parse the string-literal message. */
8396 message = cp_parser_string_literal (parser,
8397 /*translate=*/false,
8400 /* A `)' completes the static assertion. */
8401 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8402 cp_parser_skip_to_closing_parenthesis (parser,
8403 /*recovering=*/true,
8405 /*consume_paren=*/true);
8407 /* A semicolon terminates the declaration. */
8408 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
8410 /* Complete the static assertion, which may mean either processing
8411 the static assert now or saving it for template instantiation. */
8412 finish_static_assert (condition, message, saved_loc, member_p);
8415 /* Parse a `decltype' type. Returns the type.
8417 simple-type-specifier:
8418 decltype ( expression ) */
8421 cp_parser_decltype (cp_parser *parser)
8424 bool id_expression_or_member_access_p = false;
8425 const char *saved_message;
8426 bool saved_integral_constant_expression_p;
8427 bool saved_non_integral_constant_expression_p;
8429 /* Look for the `decltype' token. */
8430 if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "`decltype'"))
8431 return error_mark_node;
8433 /* Types cannot be defined in a `decltype' expression. Save away the
8435 saved_message = parser->type_definition_forbidden_message;
8437 /* And create the new one. */
8438 parser->type_definition_forbidden_message
8439 = "types may not be defined in `decltype' expressions";
8441 /* The restrictions on constant-expressions do not apply inside
8442 decltype expressions. */
8443 saved_integral_constant_expression_p
8444 = parser->integral_constant_expression_p;
8445 saved_non_integral_constant_expression_p
8446 = parser->non_integral_constant_expression_p;
8447 parser->integral_constant_expression_p = false;
8449 /* Do not actually evaluate the expression. */
8452 /* Parse the opening `('. */
8453 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
8455 /* First, try parsing an id-expression. */
8456 cp_parser_parse_tentatively (parser);
8457 expr = cp_parser_id_expression (parser,
8458 /*template_keyword_p=*/false,
8459 /*check_dependency_p=*/true,
8460 /*template_p=*/NULL,
8461 /*declarator_p=*/false,
8462 /*optional_p=*/false);
8464 if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
8466 bool non_integral_constant_expression_p = false;
8467 tree id_expression = expr;
8469 const char *error_msg;
8471 /* Lookup the name we got back from the id-expression. */
8472 expr = cp_parser_lookup_name (parser, expr,
8474 /*is_template=*/false,
8475 /*is_namespace=*/false,
8476 /*check_dependency=*/true,
8477 /*ambiguous_decls=*/NULL);
8480 && expr != error_mark_node
8481 && TREE_CODE (expr) != TEMPLATE_ID_EXPR
8482 && TREE_CODE (expr) != TYPE_DECL
8483 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8485 /* Complete lookup of the id-expression. */
8486 expr = (finish_id_expression
8487 (id_expression, expr, parser->scope, &idk,
8488 /*integral_constant_expression_p=*/false,
8489 /*allow_non_integral_constant_expression_p=*/true,
8490 &non_integral_constant_expression_p,
8491 /*template_p=*/false,
8493 /*address_p=*/false,
8494 /*template_arg_p=*/false,
8497 if (expr == error_mark_node)
8498 /* We found an id-expression, but it was something that we
8499 should not have found. This is an error, not something
8500 we can recover from, so note that we found an
8501 id-expression and we'll recover as gracefully as
8503 id_expression_or_member_access_p = true;
8507 && expr != error_mark_node
8508 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8509 /* We have an id-expression. */
8510 id_expression_or_member_access_p = true;
8513 if (!id_expression_or_member_access_p)
8515 /* Abort the id-expression parse. */
8516 cp_parser_abort_tentative_parse (parser);
8518 /* Parsing tentatively, again. */
8519 cp_parser_parse_tentatively (parser);
8521 /* Parse a class member access. */
8522 expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
8524 /*member_access_only_p=*/true);
8527 && expr != error_mark_node
8528 && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
8529 /* We have an id-expression. */
8530 id_expression_or_member_access_p = true;
8533 if (id_expression_or_member_access_p)
8534 /* We have parsed the complete id-expression or member access. */
8535 cp_parser_parse_definitely (parser);
8538 /* Abort our attempt to parse an id-expression or member access
8540 cp_parser_abort_tentative_parse (parser);
8542 /* Parse a full expression. */
8543 expr = cp_parser_expression (parser, /*cast_p=*/false);
8546 /* Go back to evaluating expressions. */
8549 /* Restore the old message and the integral constant expression
8551 parser->type_definition_forbidden_message = saved_message;
8552 parser->integral_constant_expression_p
8553 = saved_integral_constant_expression_p;
8554 parser->non_integral_constant_expression_p
8555 = saved_non_integral_constant_expression_p;
8557 if (expr == error_mark_node)
8559 /* Skip everything up to the closing `)'. */
8560 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8561 /*consume_paren=*/true);
8562 return error_mark_node;
8565 /* Parse to the closing `)'. */
8566 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
8567 cp_parser_skip_to_closing_parenthesis (parser, true, false,
8568 /*consume_paren=*/true);
8570 return finish_decltype_type (expr, id_expression_or_member_access_p);
8573 /* Special member functions [gram.special] */
8575 /* Parse a conversion-function-id.
8577 conversion-function-id:
8578 operator conversion-type-id
8580 Returns an IDENTIFIER_NODE representing the operator. */
8583 cp_parser_conversion_function_id (cp_parser* parser)
8587 tree saved_qualifying_scope;
8588 tree saved_object_scope;
8589 tree pushed_scope = NULL_TREE;
8591 /* Look for the `operator' token. */
8592 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8593 return error_mark_node;
8594 /* When we parse the conversion-type-id, the current scope will be
8595 reset. However, we need that information in able to look up the
8596 conversion function later, so we save it here. */
8597 saved_scope = parser->scope;
8598 saved_qualifying_scope = parser->qualifying_scope;
8599 saved_object_scope = parser->object_scope;
8600 /* We must enter the scope of the class so that the names of
8601 entities declared within the class are available in the
8602 conversion-type-id. For example, consider:
8609 S::operator I() { ... }
8611 In order to see that `I' is a type-name in the definition, we
8612 must be in the scope of `S'. */
8614 pushed_scope = push_scope (saved_scope);
8615 /* Parse the conversion-type-id. */
8616 type = cp_parser_conversion_type_id (parser);
8617 /* Leave the scope of the class, if any. */
8619 pop_scope (pushed_scope);
8620 /* Restore the saved scope. */
8621 parser->scope = saved_scope;
8622 parser->qualifying_scope = saved_qualifying_scope;
8623 parser->object_scope = saved_object_scope;
8624 /* If the TYPE is invalid, indicate failure. */
8625 if (type == error_mark_node)
8626 return error_mark_node;
8627 return mangle_conv_op_name_for_type (type);
8630 /* Parse a conversion-type-id:
8633 type-specifier-seq conversion-declarator [opt]
8635 Returns the TYPE specified. */
8638 cp_parser_conversion_type_id (cp_parser* parser)
8641 cp_decl_specifier_seq type_specifiers;
8642 cp_declarator *declarator;
8643 tree type_specified;
8645 /* Parse the attributes. */
8646 attributes = cp_parser_attributes_opt (parser);
8647 /* Parse the type-specifiers. */
8648 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
8650 /* If that didn't work, stop. */
8651 if (type_specifiers.type == error_mark_node)
8652 return error_mark_node;
8653 /* Parse the conversion-declarator. */
8654 declarator = cp_parser_conversion_declarator_opt (parser);
8656 type_specified = grokdeclarator (declarator, &type_specifiers, TYPENAME,
8657 /*initialized=*/0, &attributes);
8659 cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
8660 return type_specified;
8663 /* Parse an (optional) conversion-declarator.
8665 conversion-declarator:
8666 ptr-operator conversion-declarator [opt]
8670 static cp_declarator *
8671 cp_parser_conversion_declarator_opt (cp_parser* parser)
8673 enum tree_code code;
8675 cp_cv_quals cv_quals;
8677 /* We don't know if there's a ptr-operator next, or not. */
8678 cp_parser_parse_tentatively (parser);
8679 /* Try the ptr-operator. */
8680 code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
8681 /* If it worked, look for more conversion-declarators. */
8682 if (cp_parser_parse_definitely (parser))
8684 cp_declarator *declarator;
8686 /* Parse another optional declarator. */
8687 declarator = cp_parser_conversion_declarator_opt (parser);
8689 return cp_parser_make_indirect_declarator
8690 (code, class_type, cv_quals, declarator);
8696 /* Parse an (optional) ctor-initializer.
8699 : mem-initializer-list
8701 Returns TRUE iff the ctor-initializer was actually present. */
8704 cp_parser_ctor_initializer_opt (cp_parser* parser)
8706 /* If the next token is not a `:', then there is no
8707 ctor-initializer. */
8708 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
8710 /* Do default initialization of any bases and members. */
8711 if (DECL_CONSTRUCTOR_P (current_function_decl))
8712 finish_mem_initializers (NULL_TREE);
8717 /* Consume the `:' token. */
8718 cp_lexer_consume_token (parser->lexer);
8719 /* And the mem-initializer-list. */
8720 cp_parser_mem_initializer_list (parser);
8725 /* Parse a mem-initializer-list.
8727 mem-initializer-list:
8728 mem-initializer ... [opt]
8729 mem-initializer ... [opt] , mem-initializer-list */
8732 cp_parser_mem_initializer_list (cp_parser* parser)
8734 tree mem_initializer_list = NULL_TREE;
8736 /* Let the semantic analysis code know that we are starting the
8737 mem-initializer-list. */
8738 if (!DECL_CONSTRUCTOR_P (current_function_decl))
8739 error ("only constructors take base initializers");
8741 /* Loop through the list. */
8744 tree mem_initializer;
8746 /* Parse the mem-initializer. */
8747 mem_initializer = cp_parser_mem_initializer (parser);
8748 /* If the next token is a `...', we're expanding member initializers. */
8749 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
8751 /* Consume the `...'. */
8752 cp_lexer_consume_token (parser->lexer);
8754 /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
8755 can be expanded but members cannot. */
8756 if (mem_initializer != error_mark_node
8757 && !TYPE_P (TREE_PURPOSE (mem_initializer)))
8759 error ("cannot expand initializer for member %<%D%>",
8760 TREE_PURPOSE (mem_initializer));
8761 mem_initializer = error_mark_node;
8764 /* Construct the pack expansion type. */
8765 if (mem_initializer != error_mark_node)
8766 mem_initializer = make_pack_expansion (mem_initializer);
8768 /* Add it to the list, unless it was erroneous. */
8769 if (mem_initializer != error_mark_node)
8771 TREE_CHAIN (mem_initializer) = mem_initializer_list;
8772 mem_initializer_list = mem_initializer;
8774 /* If the next token is not a `,', we're done. */
8775 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8777 /* Consume the `,' token. */
8778 cp_lexer_consume_token (parser->lexer);
8781 /* Perform semantic analysis. */
8782 if (DECL_CONSTRUCTOR_P (current_function_decl))
8783 finish_mem_initializers (mem_initializer_list);
8786 /* Parse a mem-initializer.
8789 mem-initializer-id ( expression-list [opt] )
8794 ( expression-list [opt] )
8796 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
8797 class) or FIELD_DECL (for a non-static data member) to initialize;
8798 the TREE_VALUE is the expression-list. An empty initialization
8799 list is represented by void_list_node. */
8802 cp_parser_mem_initializer (cp_parser* parser)
8804 tree mem_initializer_id;
8805 tree expression_list;
8808 /* Find out what is being initialized. */
8809 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8811 pedwarn ("anachronistic old-style base class initializer");
8812 mem_initializer_id = NULL_TREE;
8815 mem_initializer_id = cp_parser_mem_initializer_id (parser);
8816 member = expand_member_init (mem_initializer_id);
8817 if (member && !DECL_P (member))
8818 in_base_initializer = 1;
8821 = cp_parser_parenthesized_expression_list (parser, false,
8823 /*allow_expansion_p=*/true,
8824 /*non_constant_p=*/NULL);
8825 if (expression_list == error_mark_node)
8826 return error_mark_node;
8827 if (!expression_list)
8828 expression_list = void_type_node;
8830 in_base_initializer = 0;
8832 return member ? build_tree_list (member, expression_list) : error_mark_node;
8835 /* Parse a mem-initializer-id.
8838 :: [opt] nested-name-specifier [opt] class-name
8841 Returns a TYPE indicating the class to be initializer for the first
8842 production. Returns an IDENTIFIER_NODE indicating the data member
8843 to be initialized for the second production. */
8846 cp_parser_mem_initializer_id (cp_parser* parser)
8848 bool global_scope_p;
8849 bool nested_name_specifier_p;
8850 bool template_p = false;
8853 /* `typename' is not allowed in this context ([temp.res]). */
8854 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
8856 error ("keyword %<typename%> not allowed in this context (a qualified "
8857 "member initializer is implicitly a type)");
8858 cp_lexer_consume_token (parser->lexer);
8860 /* Look for the optional `::' operator. */
8862 = (cp_parser_global_scope_opt (parser,
8863 /*current_scope_valid_p=*/false)
8865 /* Look for the optional nested-name-specifier. The simplest way to
8870 The keyword `typename' is not permitted in a base-specifier or
8871 mem-initializer; in these contexts a qualified name that
8872 depends on a template-parameter is implicitly assumed to be a
8875 is to assume that we have seen the `typename' keyword at this
8877 nested_name_specifier_p
8878 = (cp_parser_nested_name_specifier_opt (parser,
8879 /*typename_keyword_p=*/true,
8880 /*check_dependency_p=*/true,
8882 /*is_declaration=*/true)
8884 if (nested_name_specifier_p)
8885 template_p = cp_parser_optional_template_keyword (parser);
8886 /* If there is a `::' operator or a nested-name-specifier, then we
8887 are definitely looking for a class-name. */
8888 if (global_scope_p || nested_name_specifier_p)
8889 return cp_parser_class_name (parser,
8890 /*typename_keyword_p=*/true,
8891 /*template_keyword_p=*/template_p,
8893 /*check_dependency_p=*/true,
8894 /*class_head_p=*/false,
8895 /*is_declaration=*/true);
8896 /* Otherwise, we could also be looking for an ordinary identifier. */
8897 cp_parser_parse_tentatively (parser);
8898 /* Try a class-name. */
8899 id = cp_parser_class_name (parser,
8900 /*typename_keyword_p=*/true,
8901 /*template_keyword_p=*/false,
8903 /*check_dependency_p=*/true,
8904 /*class_head_p=*/false,
8905 /*is_declaration=*/true);
8906 /* If we found one, we're done. */
8907 if (cp_parser_parse_definitely (parser))
8909 /* Otherwise, look for an ordinary identifier. */
8910 return cp_parser_identifier (parser);
8913 /* Overloading [gram.over] */
8915 /* Parse an operator-function-id.
8917 operator-function-id:
8920 Returns an IDENTIFIER_NODE for the operator which is a
8921 human-readable spelling of the identifier, e.g., `operator +'. */
8924 cp_parser_operator_function_id (cp_parser* parser)
8926 /* Look for the `operator' keyword. */
8927 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
8928 return error_mark_node;
8929 /* And then the name of the operator itself. */
8930 return cp_parser_operator (parser);
8933 /* Parse an operator.
8936 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
8937 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
8938 || ++ -- , ->* -> () []
8945 Returns an IDENTIFIER_NODE for the operator which is a
8946 human-readable spelling of the identifier, e.g., `operator +'. */
8949 cp_parser_operator (cp_parser* parser)
8951 tree id = NULL_TREE;
8954 /* Peek at the next token. */
8955 token = cp_lexer_peek_token (parser->lexer);
8956 /* Figure out which operator we have. */
8957 switch (token->type)
8963 /* The keyword should be either `new' or `delete'. */
8964 if (token->keyword == RID_NEW)
8966 else if (token->keyword == RID_DELETE)
8971 /* Consume the `new' or `delete' token. */
8972 cp_lexer_consume_token (parser->lexer);
8974 /* Peek at the next token. */
8975 token = cp_lexer_peek_token (parser->lexer);
8976 /* If it's a `[' token then this is the array variant of the
8978 if (token->type == CPP_OPEN_SQUARE)
8980 /* Consume the `[' token. */
8981 cp_lexer_consume_token (parser->lexer);
8982 /* Look for the `]' token. */
8983 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8984 id = ansi_opname (op == NEW_EXPR
8985 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
8987 /* Otherwise, we have the non-array variant. */
8989 id = ansi_opname (op);
8995 id = ansi_opname (PLUS_EXPR);
8999 id = ansi_opname (MINUS_EXPR);
9003 id = ansi_opname (MULT_EXPR);
9007 id = ansi_opname (TRUNC_DIV_EXPR);
9011 id = ansi_opname (TRUNC_MOD_EXPR);
9015 id = ansi_opname (BIT_XOR_EXPR);
9019 id = ansi_opname (BIT_AND_EXPR);
9023 id = ansi_opname (BIT_IOR_EXPR);
9027 id = ansi_opname (BIT_NOT_EXPR);
9031 id = ansi_opname (TRUTH_NOT_EXPR);
9035 id = ansi_assopname (NOP_EXPR);
9039 id = ansi_opname (LT_EXPR);
9043 id = ansi_opname (GT_EXPR);
9047 id = ansi_assopname (PLUS_EXPR);
9051 id = ansi_assopname (MINUS_EXPR);
9055 id = ansi_assopname (MULT_EXPR);
9059 id = ansi_assopname (TRUNC_DIV_EXPR);
9063 id = ansi_assopname (TRUNC_MOD_EXPR);
9067 id = ansi_assopname (BIT_XOR_EXPR);
9071 id = ansi_assopname (BIT_AND_EXPR);
9075 id = ansi_assopname (BIT_IOR_EXPR);
9079 id = ansi_opname (LSHIFT_EXPR);
9083 id = ansi_opname (RSHIFT_EXPR);
9087 id = ansi_assopname (LSHIFT_EXPR);
9091 id = ansi_assopname (RSHIFT_EXPR);
9095 id = ansi_opname (EQ_EXPR);
9099 id = ansi_opname (NE_EXPR);
9103 id = ansi_opname (LE_EXPR);
9106 case CPP_GREATER_EQ:
9107 id = ansi_opname (GE_EXPR);
9111 id = ansi_opname (TRUTH_ANDIF_EXPR);
9115 id = ansi_opname (TRUTH_ORIF_EXPR);
9119 id = ansi_opname (POSTINCREMENT_EXPR);
9122 case CPP_MINUS_MINUS:
9123 id = ansi_opname (PREDECREMENT_EXPR);
9127 id = ansi_opname (COMPOUND_EXPR);
9130 case CPP_DEREF_STAR:
9131 id = ansi_opname (MEMBER_REF);
9135 id = ansi_opname (COMPONENT_REF);
9138 case CPP_OPEN_PAREN:
9139 /* Consume the `('. */
9140 cp_lexer_consume_token (parser->lexer);
9141 /* Look for the matching `)'. */
9142 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9143 return ansi_opname (CALL_EXPR);
9145 case CPP_OPEN_SQUARE:
9146 /* Consume the `['. */
9147 cp_lexer_consume_token (parser->lexer);
9148 /* Look for the matching `]'. */
9149 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
9150 return ansi_opname (ARRAY_REF);
9153 /* Anything else is an error. */
9157 /* If we have selected an identifier, we need to consume the
9160 cp_lexer_consume_token (parser->lexer);
9161 /* Otherwise, no valid operator name was present. */
9164 cp_parser_error (parser, "expected operator");
9165 id = error_mark_node;
9171 /* Parse a template-declaration.
9173 template-declaration:
9174 export [opt] template < template-parameter-list > declaration
9176 If MEMBER_P is TRUE, this template-declaration occurs within a
9179 The grammar rule given by the standard isn't correct. What
9182 template-declaration:
9183 export [opt] template-parameter-list-seq
9184 decl-specifier-seq [opt] init-declarator [opt] ;
9185 export [opt] template-parameter-list-seq
9188 template-parameter-list-seq:
9189 template-parameter-list-seq [opt]
9190 template < template-parameter-list > */
9193 cp_parser_template_declaration (cp_parser* parser, bool member_p)
9195 /* Check for `export'. */
9196 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
9198 /* Consume the `export' token. */
9199 cp_lexer_consume_token (parser->lexer);
9200 /* Warn that we do not support `export'. */
9201 warning (0, "keyword %<export%> not implemented, and will be ignored");
9204 cp_parser_template_declaration_after_export (parser, member_p);
9207 /* Parse a template-parameter-list.
9209 template-parameter-list:
9211 template-parameter-list , template-parameter
9213 Returns a TREE_LIST. Each node represents a template parameter.
9214 The nodes are connected via their TREE_CHAINs. */
9217 cp_parser_template_parameter_list (cp_parser* parser)
9219 tree parameter_list = NULL_TREE;
9221 begin_template_parm_list ();
9227 bool is_parameter_pack;
9229 /* Parse the template-parameter. */
9230 parameter = cp_parser_template_parameter (parser,
9232 &is_parameter_pack);
9233 /* Add it to the list. */
9234 if (parameter != error_mark_node)
9235 parameter_list = process_template_parm (parameter_list,
9241 tree err_parm = build_tree_list (parameter, parameter);
9242 TREE_VALUE (err_parm) = error_mark_node;
9243 parameter_list = chainon (parameter_list, err_parm);
9246 /* Peek at the next token. */
9247 token = cp_lexer_peek_token (parser->lexer);
9248 /* If it's not a `,', we're done. */
9249 if (token->type != CPP_COMMA)
9251 /* Otherwise, consume the `,' token. */
9252 cp_lexer_consume_token (parser->lexer);
9255 return end_template_parm_list (parameter_list);
9258 /* Parse a template-parameter.
9262 parameter-declaration
9264 If all goes well, returns a TREE_LIST. The TREE_VALUE represents
9265 the parameter. The TREE_PURPOSE is the default value, if any.
9266 Returns ERROR_MARK_NODE on failure. *IS_NON_TYPE is set to true
9267 iff this parameter is a non-type parameter. *IS_PARAMETER_PACK is
9268 set to true iff this parameter is a parameter pack. */
9271 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
9272 bool *is_parameter_pack)
9275 cp_parameter_declarator *parameter_declarator;
9278 /* Assume it is a type parameter or a template parameter. */
9279 *is_non_type = false;
9280 /* Assume it not a parameter pack. */
9281 *is_parameter_pack = false;
9282 /* Peek at the next token. */
9283 token = cp_lexer_peek_token (parser->lexer);
9284 /* If it is `class' or `template', we have a type-parameter. */
9285 if (token->keyword == RID_TEMPLATE)
9286 return cp_parser_type_parameter (parser, is_parameter_pack);
9287 /* If it is `class' or `typename' we do not know yet whether it is a
9288 type parameter or a non-type parameter. Consider:
9290 template <typename T, typename T::X X> ...
9294 template <class C, class D*> ...
9296 Here, the first parameter is a type parameter, and the second is
9297 a non-type parameter. We can tell by looking at the token after
9298 the identifier -- if it is a `,', `=', or `>' then we have a type
9300 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
9302 /* Peek at the token after `class' or `typename'. */
9303 token = cp_lexer_peek_nth_token (parser->lexer, 2);
9304 /* If it's an ellipsis, we have a template type parameter
9306 if (token->type == CPP_ELLIPSIS)
9307 return cp_parser_type_parameter (parser, is_parameter_pack);
9308 /* If it's an identifier, skip it. */
9309 if (token->type == CPP_NAME)
9310 token = cp_lexer_peek_nth_token (parser->lexer, 3);
9311 /* Now, see if the token looks like the end of a template
9313 if (token->type == CPP_COMMA
9314 || token->type == CPP_EQ
9315 || token->type == CPP_GREATER)
9316 return cp_parser_type_parameter (parser, is_parameter_pack);
9319 /* Otherwise, it is a non-type parameter.
9323 When parsing a default template-argument for a non-type
9324 template-parameter, the first non-nested `>' is taken as the end
9325 of the template parameter-list rather than a greater-than
9327 *is_non_type = true;
9328 parameter_declarator
9329 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
9330 /*parenthesized_p=*/NULL);
9332 /* If the parameter declaration is marked as a parameter pack, set
9333 *IS_PARAMETER_PACK to notify the caller. Also, unmark the
9334 declarator's PACK_EXPANSION_P, otherwise we'll get errors from
9336 if (parameter_declarator
9337 && parameter_declarator->declarator
9338 && parameter_declarator->declarator->parameter_pack_p)
9340 *is_parameter_pack = true;
9341 parameter_declarator->declarator->parameter_pack_p = false;
9344 /* If the next token is an ellipsis, and we don't already have it
9345 marked as a parameter pack, then we have a parameter pack (that
9346 has no declarator); */
9347 if (!*is_parameter_pack
9348 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
9349 && declarator_can_be_parameter_pack (parameter_declarator->declarator))
9351 /* Consume the `...'. */
9352 cp_lexer_consume_token (parser->lexer);
9353 maybe_warn_variadic_templates ();
9355 *is_parameter_pack = true;
9358 parm = grokdeclarator (parameter_declarator->declarator,
9359 ¶meter_declarator->decl_specifiers,
9360 PARM, /*initialized=*/0,
9362 if (parm == error_mark_node)
9363 return error_mark_node;
9365 return build_tree_list (parameter_declarator->default_argument, parm);
9368 /* Parse a type-parameter.
9371 class identifier [opt]
9372 class identifier [opt] = type-id
9373 typename identifier [opt]
9374 typename identifier [opt] = type-id
9375 template < template-parameter-list > class identifier [opt]
9376 template < template-parameter-list > class identifier [opt]
9379 GNU Extension (variadic templates):
9382 class ... identifier [opt]
9383 typename ... identifier [opt]
9385 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
9386 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
9387 the declaration of the parameter.
9389 Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
9392 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
9397 /* Look for a keyword to tell us what kind of parameter this is. */
9398 token = cp_parser_require (parser, CPP_KEYWORD,
9399 "`class', `typename', or `template'");
9401 return error_mark_node;
9403 switch (token->keyword)
9409 tree default_argument;
9411 /* If the next token is an ellipsis, we have a template
9413 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9415 /* Consume the `...' token. */
9416 cp_lexer_consume_token (parser->lexer);
9417 maybe_warn_variadic_templates ();
9419 *is_parameter_pack = true;
9422 /* If the next token is an identifier, then it names the
9424 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9425 identifier = cp_parser_identifier (parser);
9427 identifier = NULL_TREE;
9429 /* Create the parameter. */
9430 parameter = finish_template_type_parm (class_type_node, identifier);
9432 /* If the next token is an `=', we have a default argument. */
9433 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9435 /* Consume the `=' token. */
9436 cp_lexer_consume_token (parser->lexer);
9437 /* Parse the default-argument. */
9438 push_deferring_access_checks (dk_no_deferred);
9439 default_argument = cp_parser_type_id (parser);
9441 /* Template parameter packs cannot have default
9443 if (*is_parameter_pack)
9446 error ("template parameter pack %qD cannot have a default argument",
9449 error ("template parameter packs cannot have default arguments");
9450 default_argument = NULL_TREE;
9452 pop_deferring_access_checks ();
9455 default_argument = NULL_TREE;
9457 /* Create the combined representation of the parameter and the
9458 default argument. */
9459 parameter = build_tree_list (default_argument, parameter);
9465 tree parameter_list;
9467 tree default_argument;
9469 /* Look for the `<'. */
9470 cp_parser_require (parser, CPP_LESS, "`<'");
9471 /* Parse the template-parameter-list. */
9472 parameter_list = cp_parser_template_parameter_list (parser);
9473 /* Look for the `>'. */
9474 cp_parser_require (parser, CPP_GREATER, "`>'");
9475 /* Look for the `class' keyword. */
9476 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
9477 /* If the next token is an ellipsis, we have a template
9479 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9481 /* Consume the `...' token. */
9482 cp_lexer_consume_token (parser->lexer);
9483 maybe_warn_variadic_templates ();
9485 *is_parameter_pack = true;
9487 /* If the next token is an `=', then there is a
9488 default-argument. If the next token is a `>', we are at
9489 the end of the parameter-list. If the next token is a `,',
9490 then we are at the end of this parameter. */
9491 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9492 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
9493 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9495 identifier = cp_parser_identifier (parser);
9496 /* Treat invalid names as if the parameter were nameless. */
9497 if (identifier == error_mark_node)
9498 identifier = NULL_TREE;
9501 identifier = NULL_TREE;
9503 /* Create the template parameter. */
9504 parameter = finish_template_template_parm (class_type_node,
9507 /* If the next token is an `=', then there is a
9508 default-argument. */
9509 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9513 /* Consume the `='. */
9514 cp_lexer_consume_token (parser->lexer);
9515 /* Parse the id-expression. */
9516 push_deferring_access_checks (dk_no_deferred);
9518 = cp_parser_id_expression (parser,
9519 /*template_keyword_p=*/false,
9520 /*check_dependency_p=*/true,
9521 /*template_p=*/&is_template,
9522 /*declarator_p=*/false,
9523 /*optional_p=*/false);
9524 if (TREE_CODE (default_argument) == TYPE_DECL)
9525 /* If the id-expression was a template-id that refers to
9526 a template-class, we already have the declaration here,
9527 so no further lookup is needed. */
9530 /* Look up the name. */
9532 = cp_parser_lookup_name (parser, default_argument,
9534 /*is_template=*/is_template,
9535 /*is_namespace=*/false,
9536 /*check_dependency=*/true,
9537 /*ambiguous_decls=*/NULL);
9538 /* See if the default argument is valid. */
9540 = check_template_template_default_arg (default_argument);
9542 /* Template parameter packs cannot have default
9544 if (*is_parameter_pack)
9547 error ("template parameter pack %qD cannot have a default argument",
9550 error ("template parameter packs cannot have default arguments");
9551 default_argument = NULL_TREE;
9553 pop_deferring_access_checks ();
9556 default_argument = NULL_TREE;
9558 /* Create the combined representation of the parameter and the
9559 default argument. */
9560 parameter = build_tree_list (default_argument, parameter);
9572 /* Parse a template-id.
9575 template-name < template-argument-list [opt] >
9577 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
9578 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
9579 returned. Otherwise, if the template-name names a function, or set
9580 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
9581 names a class, returns a TYPE_DECL for the specialization.
9583 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
9584 uninstantiated templates. */
9587 cp_parser_template_id (cp_parser *parser,
9588 bool template_keyword_p,
9589 bool check_dependency_p,
9590 bool is_declaration)
9596 cp_token_position start_of_id = 0;
9597 deferred_access_check *chk;
9598 VEC (deferred_access_check,gc) *access_check;
9599 cp_token *next_token, *next_token_2;
9602 /* If the next token corresponds to a template-id, there is no need
9604 next_token = cp_lexer_peek_token (parser->lexer);
9605 if (next_token->type == CPP_TEMPLATE_ID)
9607 struct tree_check *check_value;
9609 /* Get the stored value. */
9610 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
9611 /* Perform any access checks that were deferred. */
9612 access_check = check_value->checks;
9616 VEC_iterate (deferred_access_check, access_check, i, chk) ;
9619 perform_or_defer_access_check (chk->binfo,
9624 /* Return the stored value. */
9625 return check_value->value;
9628 /* Avoid performing name lookup if there is no possibility of
9629 finding a template-id. */
9630 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
9631 || (next_token->type == CPP_NAME
9632 && !cp_parser_nth_token_starts_template_argument_list_p
9635 cp_parser_error (parser, "expected template-id");
9636 return error_mark_node;
9639 /* Remember where the template-id starts. */
9640 if (cp_parser_uncommitted_to_tentative_parse_p (parser))
9641 start_of_id = cp_lexer_token_position (parser->lexer, false);
9643 push_deferring_access_checks (dk_deferred);
9645 /* Parse the template-name. */
9646 is_identifier = false;
9647 template = cp_parser_template_name (parser, template_keyword_p,
9651 if (template == error_mark_node || is_identifier)
9653 pop_deferring_access_checks ();
9657 /* If we find the sequence `[:' after a template-name, it's probably
9658 a digraph-typo for `< ::'. Substitute the tokens and check if we can
9659 parse correctly the argument list. */
9660 next_token = cp_lexer_peek_token (parser->lexer);
9661 next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9662 if (next_token->type == CPP_OPEN_SQUARE
9663 && next_token->flags & DIGRAPH
9664 && next_token_2->type == CPP_COLON
9665 && !(next_token_2->flags & PREV_WHITE))
9667 cp_parser_parse_tentatively (parser);
9668 /* Change `:' into `::'. */
9669 next_token_2->type = CPP_SCOPE;
9670 /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
9672 cp_lexer_consume_token (parser->lexer);
9673 /* Parse the arguments. */
9674 arguments = cp_parser_enclosed_template_argument_list (parser);
9675 if (!cp_parser_parse_definitely (parser))
9677 /* If we couldn't parse an argument list, then we revert our changes
9678 and return simply an error. Maybe this is not a template-id
9680 next_token_2->type = CPP_COLON;
9681 cp_parser_error (parser, "expected %<<%>");
9682 pop_deferring_access_checks ();
9683 return error_mark_node;
9685 /* Otherwise, emit an error about the invalid digraph, but continue
9686 parsing because we got our argument list. */
9687 pedwarn ("%<<::%> cannot begin a template-argument list");
9688 inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
9689 "between %<<%> and %<::%>");
9690 if (!flag_permissive)
9695 inform ("(if you use -fpermissive G++ will accept your code)");
9702 /* Look for the `<' that starts the template-argument-list. */
9703 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
9705 pop_deferring_access_checks ();
9706 return error_mark_node;
9708 /* Parse the arguments. */
9709 arguments = cp_parser_enclosed_template_argument_list (parser);
9712 /* Build a representation of the specialization. */
9713 if (TREE_CODE (template) == IDENTIFIER_NODE)
9714 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
9715 else if (DECL_CLASS_TEMPLATE_P (template)
9716 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
9718 bool entering_scope;
9719 /* In "template <typename T> ... A<T>::", A<T> is the abstract A
9720 template (rather than some instantiation thereof) only if
9721 is not nested within some other construct. For example, in
9722 "template <typename T> void f(T) { A<T>::", A<T> is just an
9723 instantiation of A. */
9724 entering_scope = (template_parm_scope_p ()
9725 && cp_lexer_next_token_is (parser->lexer,
9728 = finish_template_type (template, arguments, entering_scope);
9732 /* If it's not a class-template or a template-template, it should be
9733 a function-template. */
9734 gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
9735 || TREE_CODE (template) == OVERLOAD
9736 || BASELINK_P (template)));
9738 template_id = lookup_template_function (template, arguments);
9741 /* If parsing tentatively, replace the sequence of tokens that makes
9742 up the template-id with a CPP_TEMPLATE_ID token. That way,
9743 should we re-parse the token stream, we will not have to repeat
9744 the effort required to do the parse, nor will we issue duplicate
9745 error messages about problems during instantiation of the
9749 cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
9751 /* Reset the contents of the START_OF_ID token. */
9752 token->type = CPP_TEMPLATE_ID;
9753 /* Retrieve any deferred checks. Do not pop this access checks yet
9754 so the memory will not be reclaimed during token replacing below. */
9755 token->u.tree_check_value = GGC_CNEW (struct tree_check);
9756 token->u.tree_check_value->value = template_id;
9757 token->u.tree_check_value->checks = get_deferred_access_checks ();
9758 token->keyword = RID_MAX;
9760 /* Purge all subsequent tokens. */
9761 cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
9763 /* ??? Can we actually assume that, if template_id ==
9764 error_mark_node, we will have issued a diagnostic to the
9765 user, as opposed to simply marking the tentative parse as
9767 if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
9768 error ("parse error in template argument list");
9771 pop_deferring_access_checks ();
9775 /* Parse a template-name.
9780 The standard should actually say:
9784 operator-function-id
9786 A defect report has been filed about this issue.
9788 A conversion-function-id cannot be a template name because they cannot
9789 be part of a template-id. In fact, looking at this code:
9793 the conversion-function-id is "operator K<int>", and K<int> is a type-id.
9794 It is impossible to call a templated conversion-function-id with an
9795 explicit argument list, since the only allowed template parameter is
9796 the type to which it is converting.
9798 If TEMPLATE_KEYWORD_P is true, then we have just seen the
9799 `template' keyword, in a construction like:
9803 In that case `f' is taken to be a template-name, even though there
9804 is no way of knowing for sure.
9806 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
9807 name refers to a set of overloaded functions, at least one of which
9808 is a template, or an IDENTIFIER_NODE with the name of the template,
9809 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
9810 names are looked up inside uninstantiated templates. */
9813 cp_parser_template_name (cp_parser* parser,
9814 bool template_keyword_p,
9815 bool check_dependency_p,
9816 bool is_declaration,
9817 bool *is_identifier)
9823 /* If the next token is `operator', then we have either an
9824 operator-function-id or a conversion-function-id. */
9825 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
9827 /* We don't know whether we're looking at an
9828 operator-function-id or a conversion-function-id. */
9829 cp_parser_parse_tentatively (parser);
9830 /* Try an operator-function-id. */
9831 identifier = cp_parser_operator_function_id (parser);
9832 /* If that didn't work, try a conversion-function-id. */
9833 if (!cp_parser_parse_definitely (parser))
9835 cp_parser_error (parser, "expected template-name");
9836 return error_mark_node;
9839 /* Look for the identifier. */
9841 identifier = cp_parser_identifier (parser);
9843 /* If we didn't find an identifier, we don't have a template-id. */
9844 if (identifier == error_mark_node)
9845 return error_mark_node;
9847 /* If the name immediately followed the `template' keyword, then it
9848 is a template-name. However, if the next token is not `<', then
9849 we do not treat it as a template-name, since it is not being used
9850 as part of a template-id. This enables us to handle constructs
9853 template <typename T> struct S { S(); };
9854 template <typename T> S<T>::S();
9856 correctly. We would treat `S' as a template -- if it were `S<T>'
9857 -- but we do not if there is no `<'. */
9859 if (processing_template_decl
9860 && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
9862 /* In a declaration, in a dependent context, we pretend that the
9863 "template" keyword was present in order to improve error
9864 recovery. For example, given:
9866 template <typename T> void f(T::X<int>);
9868 we want to treat "X<int>" as a template-id. */
9870 && !template_keyword_p
9871 && parser->scope && TYPE_P (parser->scope)
9872 && check_dependency_p
9873 && dependent_type_p (parser->scope)
9874 /* Do not do this for dtors (or ctors), since they never
9875 need the template keyword before their name. */
9876 && !constructor_name_p (identifier, parser->scope))
9878 cp_token_position start = 0;
9880 /* Explain what went wrong. */
9881 error ("non-template %qD used as template", identifier);
9882 inform ("use %<%T::template %D%> to indicate that it is a template",
9883 parser->scope, identifier);
9884 /* If parsing tentatively, find the location of the "<" token. */
9885 if (cp_parser_simulate_error (parser))
9886 start = cp_lexer_token_position (parser->lexer, true);
9887 /* Parse the template arguments so that we can issue error
9888 messages about them. */
9889 cp_lexer_consume_token (parser->lexer);
9890 cp_parser_enclosed_template_argument_list (parser);
9891 /* Skip tokens until we find a good place from which to
9892 continue parsing. */
9893 cp_parser_skip_to_closing_parenthesis (parser,
9894 /*recovering=*/true,
9896 /*consume_paren=*/false);
9897 /* If parsing tentatively, permanently remove the
9898 template argument list. That will prevent duplicate
9899 error messages from being issued about the missing
9900 "template" keyword. */
9902 cp_lexer_purge_tokens_after (parser->lexer, start);
9904 *is_identifier = true;
9908 /* If the "template" keyword is present, then there is generally
9909 no point in doing name-lookup, so we just return IDENTIFIER.
9910 But, if the qualifying scope is non-dependent then we can
9911 (and must) do name-lookup normally. */
9912 if (template_keyword_p
9914 || (TYPE_P (parser->scope)
9915 && dependent_type_p (parser->scope))))
9919 /* Look up the name. */
9920 decl = cp_parser_lookup_name (parser, identifier,
9922 /*is_template=*/false,
9923 /*is_namespace=*/false,
9925 /*ambiguous_decls=*/NULL);
9926 decl = maybe_get_template_decl_from_type_decl (decl);
9928 /* If DECL is a template, then the name was a template-name. */
9929 if (TREE_CODE (decl) == TEMPLATE_DECL)
9933 tree fn = NULL_TREE;
9935 /* The standard does not explicitly indicate whether a name that
9936 names a set of overloaded declarations, some of which are
9937 templates, is a template-name. However, such a name should
9938 be a template-name; otherwise, there is no way to form a
9939 template-id for the overloaded templates. */
9940 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
9941 if (TREE_CODE (fns) == OVERLOAD)
9942 for (fn = fns; fn; fn = OVL_NEXT (fn))
9943 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
9948 /* The name does not name a template. */
9949 cp_parser_error (parser, "expected template-name");
9950 return error_mark_node;
9954 /* If DECL is dependent, and refers to a function, then just return
9955 its name; we will look it up again during template instantiation. */
9956 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
9958 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
9959 if (TYPE_P (scope) && dependent_type_p (scope))
9966 /* Parse a template-argument-list.
9968 template-argument-list:
9969 template-argument ... [opt]
9970 template-argument-list , template-argument ... [opt]
9972 Returns a TREE_VEC containing the arguments. */
9975 cp_parser_template_argument_list (cp_parser* parser)
9977 tree fixed_args[10];
9978 unsigned n_args = 0;
9979 unsigned alloced = 10;
9980 tree *arg_ary = fixed_args;
9982 bool saved_in_template_argument_list_p;
9984 bool saved_non_ice_p;
9986 saved_in_template_argument_list_p = parser->in_template_argument_list_p;
9987 parser->in_template_argument_list_p = true;
9988 /* Even if the template-id appears in an integral
9989 constant-expression, the contents of the argument list do
9991 saved_ice_p = parser->integral_constant_expression_p;
9992 parser->integral_constant_expression_p = false;
9993 saved_non_ice_p = parser->non_integral_constant_expression_p;
9994 parser->non_integral_constant_expression_p = false;
9995 /* Parse the arguments. */
10001 /* Consume the comma. */
10002 cp_lexer_consume_token (parser->lexer);
10004 /* Parse the template-argument. */
10005 argument = cp_parser_template_argument (parser);
10007 /* If the next token is an ellipsis, we're expanding a template
10009 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10011 /* Consume the `...' token. */
10012 cp_lexer_consume_token (parser->lexer);
10014 /* Make the argument into a TYPE_PACK_EXPANSION or
10015 EXPR_PACK_EXPANSION. */
10016 argument = make_pack_expansion (argument);
10019 if (n_args == alloced)
10023 if (arg_ary == fixed_args)
10025 arg_ary = XNEWVEC (tree, alloced);
10026 memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
10029 arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
10031 arg_ary[n_args++] = argument;
10033 while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
10035 vec = make_tree_vec (n_args);
10038 TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
10040 if (arg_ary != fixed_args)
10042 parser->non_integral_constant_expression_p = saved_non_ice_p;
10043 parser->integral_constant_expression_p = saved_ice_p;
10044 parser->in_template_argument_list_p = saved_in_template_argument_list_p;
10048 /* Parse a template-argument.
10051 assignment-expression
10055 The representation is that of an assignment-expression, type-id, or
10056 id-expression -- except that the qualified id-expression is
10057 evaluated, so that the value returned is either a DECL or an
10060 Although the standard says "assignment-expression", it forbids
10061 throw-expressions or assignments in the template argument.
10062 Therefore, we use "conditional-expression" instead. */
10065 cp_parser_template_argument (cp_parser* parser)
10070 bool maybe_type_id = false;
10074 /* There's really no way to know what we're looking at, so we just
10075 try each alternative in order.
10079 In a template-argument, an ambiguity between a type-id and an
10080 expression is resolved to a type-id, regardless of the form of
10081 the corresponding template-parameter.
10083 Therefore, we try a type-id first. */
10084 cp_parser_parse_tentatively (parser);
10085 argument = cp_parser_type_id (parser);
10086 /* If there was no error parsing the type-id but the next token is a '>>',
10087 we probably found a typo for '> >'. But there are type-id which are
10088 also valid expressions. For instance:
10090 struct X { int operator >> (int); };
10091 template <int V> struct Foo {};
10094 Here 'X()' is a valid type-id of a function type, but the user just
10095 wanted to write the expression "X() >> 5". Thus, we remember that we
10096 found a valid type-id, but we still try to parse the argument as an
10097 expression to see what happens. */
10098 if (!cp_parser_error_occurred (parser)
10099 && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
10101 maybe_type_id = true;
10102 cp_parser_abort_tentative_parse (parser);
10106 /* If the next token isn't a `,' or a `>', then this argument wasn't
10107 really finished. This means that the argument is not a valid
10109 if (!cp_parser_next_token_ends_template_argument_p (parser))
10110 cp_parser_error (parser, "expected template-argument");
10111 /* If that worked, we're done. */
10112 if (cp_parser_parse_definitely (parser))
10115 /* We're still not sure what the argument will be. */
10116 cp_parser_parse_tentatively (parser);
10117 /* Try a template. */
10118 argument = cp_parser_id_expression (parser,
10119 /*template_keyword_p=*/false,
10120 /*check_dependency_p=*/true,
10122 /*declarator_p=*/false,
10123 /*optional_p=*/false);
10124 /* If the next token isn't a `,' or a `>', then this argument wasn't
10125 really finished. */
10126 if (!cp_parser_next_token_ends_template_argument_p (parser))
10127 cp_parser_error (parser, "expected template-argument");
10128 if (!cp_parser_error_occurred (parser))
10130 /* Figure out what is being referred to. If the id-expression
10131 was for a class template specialization, then we will have a
10132 TYPE_DECL at this point. There is no need to do name lookup
10133 at this point in that case. */
10134 if (TREE_CODE (argument) != TYPE_DECL)
10135 argument = cp_parser_lookup_name (parser, argument,
10137 /*is_template=*/template_p,
10138 /*is_namespace=*/false,
10139 /*check_dependency=*/true,
10140 /*ambiguous_decls=*/NULL);
10141 if (TREE_CODE (argument) != TEMPLATE_DECL
10142 && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
10143 cp_parser_error (parser, "expected template-name");
10145 if (cp_parser_parse_definitely (parser))
10147 /* It must be a non-type argument. There permitted cases are given
10148 in [temp.arg.nontype]:
10150 -- an integral constant-expression of integral or enumeration
10153 -- the name of a non-type template-parameter; or
10155 -- the name of an object or function with external linkage...
10157 -- the address of an object or function with external linkage...
10159 -- a pointer to member... */
10160 /* Look for a non-type template parameter. */
10161 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10163 cp_parser_parse_tentatively (parser);
10164 argument = cp_parser_primary_expression (parser,
10165 /*adress_p=*/false,
10167 /*template_arg_p=*/true,
10169 if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
10170 || !cp_parser_next_token_ends_template_argument_p (parser))
10171 cp_parser_simulate_error (parser);
10172 if (cp_parser_parse_definitely (parser))
10176 /* If the next token is "&", the argument must be the address of an
10177 object or function with external linkage. */
10178 address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
10180 cp_lexer_consume_token (parser->lexer);
10181 /* See if we might have an id-expression. */
10182 token = cp_lexer_peek_token (parser->lexer);
10183 if (token->type == CPP_NAME
10184 || token->keyword == RID_OPERATOR
10185 || token->type == CPP_SCOPE
10186 || token->type == CPP_TEMPLATE_ID
10187 || token->type == CPP_NESTED_NAME_SPECIFIER)
10189 cp_parser_parse_tentatively (parser);
10190 argument = cp_parser_primary_expression (parser,
10193 /*template_arg_p=*/true,
10195 if (cp_parser_error_occurred (parser)
10196 || !cp_parser_next_token_ends_template_argument_p (parser))
10197 cp_parser_abort_tentative_parse (parser);
10200 if (TREE_CODE (argument) == INDIRECT_REF)
10202 gcc_assert (REFERENCE_REF_P (argument));
10203 argument = TREE_OPERAND (argument, 0);
10206 if (TREE_CODE (argument) == VAR_DECL)
10208 /* A variable without external linkage might still be a
10209 valid constant-expression, so no error is issued here
10210 if the external-linkage check fails. */
10211 if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
10212 cp_parser_simulate_error (parser);
10214 else if (is_overloaded_fn (argument))
10215 /* All overloaded functions are allowed; if the external
10216 linkage test does not pass, an error will be issued
10220 && (TREE_CODE (argument) == OFFSET_REF
10221 || TREE_CODE (argument) == SCOPE_REF))
10222 /* A pointer-to-member. */
10224 else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
10227 cp_parser_simulate_error (parser);
10229 if (cp_parser_parse_definitely (parser))
10232 argument = build_x_unary_op (ADDR_EXPR, argument);
10237 /* If the argument started with "&", there are no other valid
10238 alternatives at this point. */
10241 cp_parser_error (parser, "invalid non-type template argument");
10242 return error_mark_node;
10245 /* If the argument wasn't successfully parsed as a type-id followed
10246 by '>>', the argument can only be a constant expression now.
10247 Otherwise, we try parsing the constant-expression tentatively,
10248 because the argument could really be a type-id. */
10250 cp_parser_parse_tentatively (parser);
10251 argument = cp_parser_constant_expression (parser,
10252 /*allow_non_constant_p=*/false,
10253 /*non_constant_p=*/NULL);
10254 argument = fold_non_dependent_expr (argument);
10255 if (!maybe_type_id)
10257 if (!cp_parser_next_token_ends_template_argument_p (parser))
10258 cp_parser_error (parser, "expected template-argument");
10259 if (cp_parser_parse_definitely (parser))
10261 /* We did our best to parse the argument as a non type-id, but that
10262 was the only alternative that matched (albeit with a '>' after
10263 it). We can assume it's just a typo from the user, and a
10264 diagnostic will then be issued. */
10265 return cp_parser_type_id (parser);
10268 /* Parse an explicit-instantiation.
10270 explicit-instantiation:
10271 template declaration
10273 Although the standard says `declaration', what it really means is:
10275 explicit-instantiation:
10276 template decl-specifier-seq [opt] declarator [opt] ;
10278 Things like `template int S<int>::i = 5, int S<double>::j;' are not
10279 supposed to be allowed. A defect report has been filed about this
10284 explicit-instantiation:
10285 storage-class-specifier template
10286 decl-specifier-seq [opt] declarator [opt] ;
10287 function-specifier template
10288 decl-specifier-seq [opt] declarator [opt] ; */
10291 cp_parser_explicit_instantiation (cp_parser* parser)
10293 int declares_class_or_enum;
10294 cp_decl_specifier_seq decl_specifiers;
10295 tree extension_specifier = NULL_TREE;
10297 /* Look for an (optional) storage-class-specifier or
10298 function-specifier. */
10299 if (cp_parser_allow_gnu_extensions_p (parser))
10301 extension_specifier
10302 = cp_parser_storage_class_specifier_opt (parser);
10303 if (!extension_specifier)
10304 extension_specifier
10305 = cp_parser_function_specifier_opt (parser,
10306 /*decl_specs=*/NULL);
10309 /* Look for the `template' keyword. */
10310 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10311 /* Let the front end know that we are processing an explicit
10313 begin_explicit_instantiation ();
10314 /* [temp.explicit] says that we are supposed to ignore access
10315 control while processing explicit instantiation directives. */
10316 push_deferring_access_checks (dk_no_check);
10317 /* Parse a decl-specifier-seq. */
10318 cp_parser_decl_specifier_seq (parser,
10319 CP_PARSER_FLAGS_OPTIONAL,
10321 &declares_class_or_enum);
10322 /* If there was exactly one decl-specifier, and it declared a class,
10323 and there's no declarator, then we have an explicit type
10325 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
10329 type = check_tag_decl (&decl_specifiers);
10330 /* Turn access control back on for names used during
10331 template instantiation. */
10332 pop_deferring_access_checks ();
10334 do_type_instantiation (type, extension_specifier,
10335 /*complain=*/tf_error);
10339 cp_declarator *declarator;
10342 /* Parse the declarator. */
10344 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10345 /*ctor_dtor_or_conv_p=*/NULL,
10346 /*parenthesized_p=*/NULL,
10347 /*member_p=*/false);
10348 if (declares_class_or_enum & 2)
10349 cp_parser_check_for_definition_in_return_type (declarator,
10350 decl_specifiers.type);
10351 if (declarator != cp_error_declarator)
10353 decl = grokdeclarator (declarator, &decl_specifiers,
10354 NORMAL, 0, &decl_specifiers.attributes);
10355 /* Turn access control back on for names used during
10356 template instantiation. */
10357 pop_deferring_access_checks ();
10358 /* Do the explicit instantiation. */
10359 do_decl_instantiation (decl, extension_specifier);
10363 pop_deferring_access_checks ();
10364 /* Skip the body of the explicit instantiation. */
10365 cp_parser_skip_to_end_of_statement (parser);
10368 /* We're done with the instantiation. */
10369 end_explicit_instantiation ();
10371 cp_parser_consume_semicolon_at_end_of_statement (parser);
10374 /* Parse an explicit-specialization.
10376 explicit-specialization:
10377 template < > declaration
10379 Although the standard says `declaration', what it really means is:
10381 explicit-specialization:
10382 template <> decl-specifier [opt] init-declarator [opt] ;
10383 template <> function-definition
10384 template <> explicit-specialization
10385 template <> template-declaration */
10388 cp_parser_explicit_specialization (cp_parser* parser)
10390 bool need_lang_pop;
10391 /* Look for the `template' keyword. */
10392 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
10393 /* Look for the `<'. */
10394 cp_parser_require (parser, CPP_LESS, "`<'");
10395 /* Look for the `>'. */
10396 cp_parser_require (parser, CPP_GREATER, "`>'");
10397 /* We have processed another parameter list. */
10398 ++parser->num_template_parameter_lists;
10401 A template ... explicit specialization ... shall not have C
10403 if (current_lang_name == lang_name_c)
10405 error ("template specialization with C linkage");
10406 /* Give it C++ linkage to avoid confusing other parts of the
10408 push_lang_context (lang_name_cplusplus);
10409 need_lang_pop = true;
10412 need_lang_pop = false;
10413 /* Let the front end know that we are beginning a specialization. */
10414 if (!begin_specialization ())
10416 end_specialization ();
10417 cp_parser_skip_to_end_of_block_or_statement (parser);
10421 /* If the next keyword is `template', we need to figure out whether
10422 or not we're looking a template-declaration. */
10423 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
10425 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
10426 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
10427 cp_parser_template_declaration_after_export (parser,
10428 /*member_p=*/false);
10430 cp_parser_explicit_specialization (parser);
10433 /* Parse the dependent declaration. */
10434 cp_parser_single_declaration (parser,
10436 /*member_p=*/false,
10437 /*explicit_specialization_p=*/true,
10438 /*friend_p=*/NULL);
10439 /* We're done with the specialization. */
10440 end_specialization ();
10441 /* For the erroneous case of a template with C linkage, we pushed an
10442 implicit C++ linkage scope; exit that scope now. */
10444 pop_lang_context ();
10445 /* We're done with this parameter list. */
10446 --parser->num_template_parameter_lists;
10449 /* Parse a type-specifier.
10452 simple-type-specifier
10455 elaborated-type-specifier
10463 Returns a representation of the type-specifier. For a
10464 class-specifier, enum-specifier, or elaborated-type-specifier, a
10465 TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
10467 The parser flags FLAGS is used to control type-specifier parsing.
10469 If IS_DECLARATION is TRUE, then this type-specifier is appearing
10470 in a decl-specifier-seq.
10472 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
10473 class-specifier, enum-specifier, or elaborated-type-specifier, then
10474 *DECLARES_CLASS_OR_ENUM is set to a nonzero value. The value is 1
10475 if a type is declared; 2 if it is defined. Otherwise, it is set to
10478 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
10479 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
10480 is set to FALSE. */
10483 cp_parser_type_specifier (cp_parser* parser,
10484 cp_parser_flags flags,
10485 cp_decl_specifier_seq *decl_specs,
10486 bool is_declaration,
10487 int* declares_class_or_enum,
10488 bool* is_cv_qualifier)
10490 tree type_spec = NULL_TREE;
10493 cp_decl_spec ds = ds_last;
10495 /* Assume this type-specifier does not declare a new type. */
10496 if (declares_class_or_enum)
10497 *declares_class_or_enum = 0;
10498 /* And that it does not specify a cv-qualifier. */
10499 if (is_cv_qualifier)
10500 *is_cv_qualifier = false;
10501 /* Peek at the next token. */
10502 token = cp_lexer_peek_token (parser->lexer);
10504 /* If we're looking at a keyword, we can use that to guide the
10505 production we choose. */
10506 keyword = token->keyword;
10510 /* Look for the enum-specifier. */
10511 type_spec = cp_parser_enum_specifier (parser);
10512 /* If that worked, we're done. */
10515 if (declares_class_or_enum)
10516 *declares_class_or_enum = 2;
10518 cp_parser_set_decl_spec_type (decl_specs,
10520 /*user_defined_p=*/true);
10524 goto elaborated_type_specifier;
10526 /* Any of these indicate either a class-specifier, or an
10527 elaborated-type-specifier. */
10531 /* Parse tentatively so that we can back up if we don't find a
10532 class-specifier. */
10533 cp_parser_parse_tentatively (parser);
10534 /* Look for the class-specifier. */
10535 type_spec = cp_parser_class_specifier (parser);
10536 /* If that worked, we're done. */
10537 if (cp_parser_parse_definitely (parser))
10539 if (declares_class_or_enum)
10540 *declares_class_or_enum = 2;
10542 cp_parser_set_decl_spec_type (decl_specs,
10544 /*user_defined_p=*/true);
10548 /* Fall through. */
10549 elaborated_type_specifier:
10550 /* We're declaring (not defining) a class or enum. */
10551 if (declares_class_or_enum)
10552 *declares_class_or_enum = 1;
10554 /* Fall through. */
10556 /* Look for an elaborated-type-specifier. */
10558 = (cp_parser_elaborated_type_specifier
10560 decl_specs && decl_specs->specs[(int) ds_friend],
10563 cp_parser_set_decl_spec_type (decl_specs,
10565 /*user_defined_p=*/true);
10570 if (is_cv_qualifier)
10571 *is_cv_qualifier = true;
10576 if (is_cv_qualifier)
10577 *is_cv_qualifier = true;
10582 if (is_cv_qualifier)
10583 *is_cv_qualifier = true;
10587 /* The `__complex__' keyword is a GNU extension. */
10595 /* Handle simple keywords. */
10600 ++decl_specs->specs[(int)ds];
10601 decl_specs->any_specifiers_p = true;
10603 return cp_lexer_consume_token (parser->lexer)->u.value;
10606 /* If we do not already have a type-specifier, assume we are looking
10607 at a simple-type-specifier. */
10608 type_spec = cp_parser_simple_type_specifier (parser,
10612 /* If we didn't find a type-specifier, and a type-specifier was not
10613 optional in this context, issue an error message. */
10614 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10616 cp_parser_error (parser, "expected type specifier");
10617 return error_mark_node;
10623 /* Parse a simple-type-specifier.
10625 simple-type-specifier:
10626 :: [opt] nested-name-specifier [opt] type-name
10627 :: [opt] nested-name-specifier template template-id
10642 simple-type-specifier:
10643 decltype ( expression )
10647 simple-type-specifier:
10648 __typeof__ unary-expression
10649 __typeof__ ( type-id )
10651 Returns the indicated TYPE_DECL. If DECL_SPECS is not NULL, it is
10652 appropriately updated. */
10655 cp_parser_simple_type_specifier (cp_parser* parser,
10656 cp_decl_specifier_seq *decl_specs,
10657 cp_parser_flags flags)
10659 tree type = NULL_TREE;
10662 /* Peek at the next token. */
10663 token = cp_lexer_peek_token (parser->lexer);
10665 /* If we're looking at a keyword, things are easy. */
10666 switch (token->keyword)
10670 decl_specs->explicit_char_p = true;
10671 type = char_type_node;
10674 type = wchar_type_node;
10677 type = boolean_type_node;
10681 ++decl_specs->specs[(int) ds_short];
10682 type = short_integer_type_node;
10686 decl_specs->explicit_int_p = true;
10687 type = integer_type_node;
10691 ++decl_specs->specs[(int) ds_long];
10692 type = long_integer_type_node;
10696 ++decl_specs->specs[(int) ds_signed];
10697 type = integer_type_node;
10701 ++decl_specs->specs[(int) ds_unsigned];
10702 type = unsigned_type_node;
10705 type = float_type_node;
10708 type = double_type_node;
10711 type = void_type_node;
10715 /* Parse the `decltype' type. */
10716 type = cp_parser_decltype (parser);
10719 cp_parser_set_decl_spec_type (decl_specs, type,
10720 /*user_defined_p=*/true);
10725 /* Consume the `typeof' token. */
10726 cp_lexer_consume_token (parser->lexer);
10727 /* Parse the operand to `typeof'. */
10728 type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
10729 /* If it is not already a TYPE, take its type. */
10730 if (!TYPE_P (type))
10731 type = finish_typeof (type);
10734 cp_parser_set_decl_spec_type (decl_specs, type,
10735 /*user_defined_p=*/true);
10743 /* If the type-specifier was for a built-in type, we're done. */
10748 /* Record the type. */
10750 && (token->keyword != RID_SIGNED
10751 && token->keyword != RID_UNSIGNED
10752 && token->keyword != RID_SHORT
10753 && token->keyword != RID_LONG))
10754 cp_parser_set_decl_spec_type (decl_specs,
10756 /*user_defined=*/false);
10758 decl_specs->any_specifiers_p = true;
10760 /* Consume the token. */
10761 id = cp_lexer_consume_token (parser->lexer)->u.value;
10763 /* There is no valid C++ program where a non-template type is
10764 followed by a "<". That usually indicates that the user thought
10765 that the type was a template. */
10766 cp_parser_check_for_invalid_template_id (parser, type);
10768 return TYPE_NAME (type);
10771 /* The type-specifier must be a user-defined type. */
10772 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
10777 /* Don't gobble tokens or issue error messages if this is an
10778 optional type-specifier. */
10779 if (flags & CP_PARSER_FLAGS_OPTIONAL)
10780 cp_parser_parse_tentatively (parser);
10782 /* Look for the optional `::' operator. */
10784 = (cp_parser_global_scope_opt (parser,
10785 /*current_scope_valid_p=*/false)
10787 /* Look for the nested-name specifier. */
10789 = (cp_parser_nested_name_specifier_opt (parser,
10790 /*typename_keyword_p=*/false,
10791 /*check_dependency_p=*/true,
10793 /*is_declaration=*/false)
10795 /* If we have seen a nested-name-specifier, and the next token
10796 is `template', then we are using the template-id production. */
10798 && cp_parser_optional_template_keyword (parser))
10800 /* Look for the template-id. */
10801 type = cp_parser_template_id (parser,
10802 /*template_keyword_p=*/true,
10803 /*check_dependency_p=*/true,
10804 /*is_declaration=*/false);
10805 /* If the template-id did not name a type, we are out of
10807 if (TREE_CODE (type) != TYPE_DECL)
10809 cp_parser_error (parser, "expected template-id for type");
10813 /* Otherwise, look for a type-name. */
10815 type = cp_parser_type_name (parser);
10816 /* Keep track of all name-lookups performed in class scopes. */
10820 && TREE_CODE (type) == TYPE_DECL
10821 && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
10822 maybe_note_name_used_in_class (DECL_NAME (type), type);
10823 /* If it didn't work out, we don't have a TYPE. */
10824 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
10825 && !cp_parser_parse_definitely (parser))
10827 if (type && decl_specs)
10828 cp_parser_set_decl_spec_type (decl_specs, type,
10829 /*user_defined=*/true);
10832 /* If we didn't get a type-name, issue an error message. */
10833 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
10835 cp_parser_error (parser, "expected type-name");
10836 return error_mark_node;
10839 /* There is no valid C++ program where a non-template type is
10840 followed by a "<". That usually indicates that the user thought
10841 that the type was a template. */
10842 if (type && type != error_mark_node)
10844 /* As a last-ditch effort, see if TYPE is an Objective-C type.
10845 If it is, then the '<'...'>' enclose protocol names rather than
10846 template arguments, and so everything is fine. */
10847 if (c_dialect_objc ()
10848 && (objc_is_id (type) || objc_is_class_name (type)))
10850 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10851 tree qual_type = objc_get_protocol_qualified_type (type, protos);
10853 /* Clobber the "unqualified" type previously entered into
10854 DECL_SPECS with the new, improved protocol-qualified version. */
10856 decl_specs->type = qual_type;
10861 cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
10867 /* Parse a type-name.
10880 Returns a TYPE_DECL for the type. */
10883 cp_parser_type_name (cp_parser* parser)
10888 /* We can't know yet whether it is a class-name or not. */
10889 cp_parser_parse_tentatively (parser);
10890 /* Try a class-name. */
10891 type_decl = cp_parser_class_name (parser,
10892 /*typename_keyword_p=*/false,
10893 /*template_keyword_p=*/false,
10895 /*check_dependency_p=*/true,
10896 /*class_head_p=*/false,
10897 /*is_declaration=*/false);
10898 /* If it's not a class-name, keep looking. */
10899 if (!cp_parser_parse_definitely (parser))
10901 /* It must be a typedef-name or an enum-name. */
10902 identifier = cp_parser_identifier (parser);
10903 if (identifier == error_mark_node)
10904 return error_mark_node;
10906 /* Look up the type-name. */
10907 type_decl = cp_parser_lookup_name_simple (parser, identifier);
10909 if (TREE_CODE (type_decl) != TYPE_DECL
10910 && (objc_is_id (identifier) || objc_is_class_name (identifier)))
10912 /* See if this is an Objective-C type. */
10913 tree protos = cp_parser_objc_protocol_refs_opt (parser);
10914 tree type = objc_get_protocol_qualified_type (identifier, protos);
10916 type_decl = TYPE_NAME (type);
10919 /* Issue an error if we did not find a type-name. */
10920 if (TREE_CODE (type_decl) != TYPE_DECL)
10922 if (!cp_parser_simulate_error (parser))
10923 cp_parser_name_lookup_error (parser, identifier, type_decl,
10925 type_decl = error_mark_node;
10927 /* Remember that the name was used in the definition of the
10928 current class so that we can check later to see if the
10929 meaning would have been different after the class was
10930 entirely defined. */
10931 else if (type_decl != error_mark_node
10933 maybe_note_name_used_in_class (identifier, type_decl);
10940 /* Parse an elaborated-type-specifier. Note that the grammar given
10941 here incorporates the resolution to DR68.
10943 elaborated-type-specifier:
10944 class-key :: [opt] nested-name-specifier [opt] identifier
10945 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
10946 enum :: [opt] nested-name-specifier [opt] identifier
10947 typename :: [opt] nested-name-specifier identifier
10948 typename :: [opt] nested-name-specifier template [opt]
10953 elaborated-type-specifier:
10954 class-key attributes :: [opt] nested-name-specifier [opt] identifier
10955 class-key attributes :: [opt] nested-name-specifier [opt]
10956 template [opt] template-id
10957 enum attributes :: [opt] nested-name-specifier [opt] identifier
10959 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
10960 declared `friend'. If IS_DECLARATION is TRUE, then this
10961 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
10962 something is being declared.
10964 Returns the TYPE specified. */
10967 cp_parser_elaborated_type_specifier (cp_parser* parser,
10969 bool is_declaration)
10971 enum tag_types tag_type;
10973 tree type = NULL_TREE;
10974 tree attributes = NULL_TREE;
10976 /* See if we're looking at the `enum' keyword. */
10977 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
10979 /* Consume the `enum' token. */
10980 cp_lexer_consume_token (parser->lexer);
10981 /* Remember that it's an enumeration type. */
10982 tag_type = enum_type;
10983 /* Parse the attributes. */
10984 attributes = cp_parser_attributes_opt (parser);
10986 /* Or, it might be `typename'. */
10987 else if (cp_lexer_next_token_is_keyword (parser->lexer,
10990 /* Consume the `typename' token. */
10991 cp_lexer_consume_token (parser->lexer);
10992 /* Remember that it's a `typename' type. */
10993 tag_type = typename_type;
10994 /* The `typename' keyword is only allowed in templates. */
10995 if (!processing_template_decl)
10996 pedwarn ("using %<typename%> outside of template");
10998 /* Otherwise it must be a class-key. */
11001 tag_type = cp_parser_class_key (parser);
11002 if (tag_type == none_type)
11003 return error_mark_node;
11004 /* Parse the attributes. */
11005 attributes = cp_parser_attributes_opt (parser);
11008 /* Look for the `::' operator. */
11009 cp_parser_global_scope_opt (parser,
11010 /*current_scope_valid_p=*/false);
11011 /* Look for the nested-name-specifier. */
11012 if (tag_type == typename_type)
11014 if (!cp_parser_nested_name_specifier (parser,
11015 /*typename_keyword_p=*/true,
11016 /*check_dependency_p=*/true,
11019 return error_mark_node;
11022 /* Even though `typename' is not present, the proposed resolution
11023 to Core Issue 180 says that in `class A<T>::B', `B' should be
11024 considered a type-name, even if `A<T>' is dependent. */
11025 cp_parser_nested_name_specifier_opt (parser,
11026 /*typename_keyword_p=*/true,
11027 /*check_dependency_p=*/true,
11030 /* For everything but enumeration types, consider a template-id.
11031 For an enumeration type, consider only a plain identifier. */
11032 if (tag_type != enum_type)
11034 bool template_p = false;
11037 /* Allow the `template' keyword. */
11038 template_p = cp_parser_optional_template_keyword (parser);
11039 /* If we didn't see `template', we don't know if there's a
11040 template-id or not. */
11042 cp_parser_parse_tentatively (parser);
11043 /* Parse the template-id. */
11044 decl = cp_parser_template_id (parser, template_p,
11045 /*check_dependency_p=*/true,
11047 /* If we didn't find a template-id, look for an ordinary
11049 if (!template_p && !cp_parser_parse_definitely (parser))
11051 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
11052 in effect, then we must assume that, upon instantiation, the
11053 template will correspond to a class. */
11054 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11055 && tag_type == typename_type)
11056 type = make_typename_type (parser->scope, decl,
11058 /*complain=*/tf_error);
11060 type = TREE_TYPE (decl);
11065 identifier = cp_parser_identifier (parser);
11067 if (identifier == error_mark_node)
11069 parser->scope = NULL_TREE;
11070 return error_mark_node;
11073 /* For a `typename', we needn't call xref_tag. */
11074 if (tag_type == typename_type
11075 && TREE_CODE (parser->scope) != NAMESPACE_DECL)
11076 return cp_parser_make_typename_type (parser, parser->scope,
11078 /* Look up a qualified name in the usual way. */
11082 tree ambiguous_decls;
11084 decl = cp_parser_lookup_name (parser, identifier,
11086 /*is_template=*/false,
11087 /*is_namespace=*/false,
11088 /*check_dependency=*/true,
11091 /* If the lookup was ambiguous, an error will already have been
11093 if (ambiguous_decls)
11094 return error_mark_node;
11096 /* If we are parsing friend declaration, DECL may be a
11097 TEMPLATE_DECL tree node here. However, we need to check
11098 whether this TEMPLATE_DECL results in valid code. Consider
11099 the following example:
11102 template <class T> class C {};
11105 template <class T> friend class N::C; // #1, valid code
11107 template <class T> class Y {
11108 friend class N::C; // #2, invalid code
11111 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
11112 name lookup of `N::C'. We see that friend declaration must
11113 be template for the code to be valid. Note that
11114 processing_template_decl does not work here since it is
11115 always 1 for the above two cases. */
11117 decl = (cp_parser_maybe_treat_template_as_class
11118 (decl, /*tag_name_p=*/is_friend
11119 && parser->num_template_parameter_lists));
11121 if (TREE_CODE (decl) != TYPE_DECL)
11123 cp_parser_diagnose_invalid_type_name (parser,
11126 return error_mark_node;
11129 if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
11131 bool allow_template = (parser->num_template_parameter_lists
11132 || DECL_SELF_REFERENCE_P (decl));
11133 type = check_elaborated_type_specifier (tag_type, decl,
11136 if (type == error_mark_node)
11137 return error_mark_node;
11140 /* Forward declarations of nested types, such as
11145 are invalid unless all components preceding the final '::'
11146 are complete. If all enclosing types are complete, these
11147 declarations become merely pointless.
11149 Invalid forward declarations of nested types are errors
11150 caught elsewhere in parsing. Those that are pointless arrive
11153 if (cp_parser_declares_only_class_p (parser)
11154 && !is_friend && !processing_explicit_instantiation)
11155 warning (0, "declaration %qD does not declare anything", decl);
11157 type = TREE_TYPE (decl);
11161 /* An elaborated-type-specifier sometimes introduces a new type and
11162 sometimes names an existing type. Normally, the rule is that it
11163 introduces a new type only if there is not an existing type of
11164 the same name already in scope. For example, given:
11167 void f() { struct S s; }
11169 the `struct S' in the body of `f' is the same `struct S' as in
11170 the global scope; the existing definition is used. However, if
11171 there were no global declaration, this would introduce a new
11172 local class named `S'.
11174 An exception to this rule applies to the following code:
11176 namespace N { struct S; }
11178 Here, the elaborated-type-specifier names a new type
11179 unconditionally; even if there is already an `S' in the
11180 containing scope this declaration names a new type.
11181 This exception only applies if the elaborated-type-specifier
11182 forms the complete declaration:
11186 A declaration consisting solely of `class-key identifier ;' is
11187 either a redeclaration of the name in the current scope or a
11188 forward declaration of the identifier as a class name. It
11189 introduces the name into the current scope.
11191 We are in this situation precisely when the next token is a `;'.
11193 An exception to the exception is that a `friend' declaration does
11194 *not* name a new type; i.e., given:
11196 struct S { friend struct T; };
11198 `T' is not a new type in the scope of `S'.
11200 Also, `new struct S' or `sizeof (struct S)' never results in the
11201 definition of a new type; a new type can only be declared in a
11202 declaration context. */
11208 /* Friends have special name lookup rules. */
11209 ts = ts_within_enclosing_non_class;
11210 else if (is_declaration
11211 && cp_lexer_next_token_is (parser->lexer,
11213 /* This is a `class-key identifier ;' */
11219 (parser->num_template_parameter_lists
11220 && (cp_parser_next_token_starts_class_definition_p (parser)
11221 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
11222 /* An unqualified name was used to reference this type, so
11223 there were no qualifying templates. */
11224 if (!cp_parser_check_template_parameters (parser,
11225 /*num_templates=*/0))
11226 return error_mark_node;
11227 type = xref_tag (tag_type, identifier, ts, template_p);
11231 if (type == error_mark_node)
11232 return error_mark_node;
11234 /* Allow attributes on forward declarations of classes. */
11237 if (TREE_CODE (type) == TYPENAME_TYPE)
11238 warning (OPT_Wattributes,
11239 "attributes ignored on uninstantiated type");
11240 else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
11241 && ! processing_explicit_instantiation)
11242 warning (OPT_Wattributes,
11243 "attributes ignored on template instantiation");
11244 else if (is_declaration && cp_parser_declares_only_class_p (parser))
11245 cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
11247 warning (OPT_Wattributes,
11248 "attributes ignored on elaborated-type-specifier that is not a forward declaration");
11251 if (tag_type != enum_type)
11252 cp_parser_check_class_key (tag_type, type);
11254 /* A "<" cannot follow an elaborated type specifier. If that
11255 happens, the user was probably trying to form a template-id. */
11256 cp_parser_check_for_invalid_template_id (parser, type);
11261 /* Parse an enum-specifier.
11264 enum identifier [opt] { enumerator-list [opt] }
11267 enum attributes[opt] identifier [opt] { enumerator-list [opt] }
11270 Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
11271 if the token stream isn't an enum-specifier after all. */
11274 cp_parser_enum_specifier (cp_parser* parser)
11280 /* Parse tentatively so that we can back up if we don't find a
11282 cp_parser_parse_tentatively (parser);
11284 /* Caller guarantees that the current token is 'enum', an identifier
11285 possibly follows, and the token after that is an opening brace.
11286 If we don't have an identifier, fabricate an anonymous name for
11287 the enumeration being defined. */
11288 cp_lexer_consume_token (parser->lexer);
11290 attributes = cp_parser_attributes_opt (parser);
11292 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11293 identifier = cp_parser_identifier (parser);
11295 identifier = make_anon_name ();
11297 /* Look for the `{' but don't consume it yet. */
11298 if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11299 cp_parser_simulate_error (parser);
11301 if (!cp_parser_parse_definitely (parser))
11304 /* Issue an error message if type-definitions are forbidden here. */
11305 if (!cp_parser_check_type_definition (parser))
11306 type = error_mark_node;
11308 /* Create the new type. We do this before consuming the opening
11309 brace so the enum will be recorded as being on the line of its
11310 tag (or the 'enum' keyword, if there is no tag). */
11311 type = start_enum (identifier);
11313 /* Consume the opening brace. */
11314 cp_lexer_consume_token (parser->lexer);
11316 if (type == error_mark_node)
11318 cp_parser_skip_to_end_of_block_or_statement (parser);
11319 return error_mark_node;
11322 /* If the next token is not '}', then there are some enumerators. */
11323 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11324 cp_parser_enumerator_list (parser, type);
11326 /* Consume the final '}'. */
11327 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11329 /* Look for trailing attributes to apply to this enumeration, and
11330 apply them if appropriate. */
11331 if (cp_parser_allow_gnu_extensions_p (parser))
11333 tree trailing_attr = cp_parser_attributes_opt (parser);
11334 cplus_decl_attributes (&type,
11336 (int) ATTR_FLAG_TYPE_IN_PLACE);
11339 /* Finish up the enumeration. */
11340 finish_enum (type);
11345 /* Parse an enumerator-list. The enumerators all have the indicated
11349 enumerator-definition
11350 enumerator-list , enumerator-definition */
11353 cp_parser_enumerator_list (cp_parser* parser, tree type)
11357 /* Parse an enumerator-definition. */
11358 cp_parser_enumerator_definition (parser, type);
11360 /* If the next token is not a ',', we've reached the end of
11362 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11364 /* Otherwise, consume the `,' and keep going. */
11365 cp_lexer_consume_token (parser->lexer);
11366 /* If the next token is a `}', there is a trailing comma. */
11367 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
11369 if (pedantic && !in_system_header)
11370 pedwarn ("comma at end of enumerator list");
11376 /* Parse an enumerator-definition. The enumerator has the indicated
11379 enumerator-definition:
11381 enumerator = constant-expression
11387 cp_parser_enumerator_definition (cp_parser* parser, tree type)
11392 /* Look for the identifier. */
11393 identifier = cp_parser_identifier (parser);
11394 if (identifier == error_mark_node)
11397 /* If the next token is an '=', then there is an explicit value. */
11398 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11400 /* Consume the `=' token. */
11401 cp_lexer_consume_token (parser->lexer);
11402 /* Parse the value. */
11403 value = cp_parser_constant_expression (parser,
11404 /*allow_non_constant_p=*/false,
11410 /* Create the enumerator. */
11411 build_enumerator (identifier, value, type);
11414 /* Parse a namespace-name.
11417 original-namespace-name
11420 Returns the NAMESPACE_DECL for the namespace. */
11423 cp_parser_namespace_name (cp_parser* parser)
11426 tree namespace_decl;
11428 /* Get the name of the namespace. */
11429 identifier = cp_parser_identifier (parser);
11430 if (identifier == error_mark_node)
11431 return error_mark_node;
11433 /* Look up the identifier in the currently active scope. Look only
11434 for namespaces, due to:
11436 [basic.lookup.udir]
11438 When looking up a namespace-name in a using-directive or alias
11439 definition, only namespace names are considered.
11443 [basic.lookup.qual]
11445 During the lookup of a name preceding the :: scope resolution
11446 operator, object, function, and enumerator names are ignored.
11448 (Note that cp_parser_class_or_namespace_name only calls this
11449 function if the token after the name is the scope resolution
11451 namespace_decl = cp_parser_lookup_name (parser, identifier,
11453 /*is_template=*/false,
11454 /*is_namespace=*/true,
11455 /*check_dependency=*/true,
11456 /*ambiguous_decls=*/NULL);
11457 /* If it's not a namespace, issue an error. */
11458 if (namespace_decl == error_mark_node
11459 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
11461 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11462 error ("%qD is not a namespace-name", identifier);
11463 cp_parser_error (parser, "expected namespace-name");
11464 namespace_decl = error_mark_node;
11467 return namespace_decl;
11470 /* Parse a namespace-definition.
11472 namespace-definition:
11473 named-namespace-definition
11474 unnamed-namespace-definition
11476 named-namespace-definition:
11477 original-namespace-definition
11478 extension-namespace-definition
11480 original-namespace-definition:
11481 namespace identifier { namespace-body }
11483 extension-namespace-definition:
11484 namespace original-namespace-name { namespace-body }
11486 unnamed-namespace-definition:
11487 namespace { namespace-body } */
11490 cp_parser_namespace_definition (cp_parser* parser)
11492 tree identifier, attribs;
11493 bool has_visibility;
11495 /* Look for the `namespace' keyword. */
11496 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11498 /* Get the name of the namespace. We do not attempt to distinguish
11499 between an original-namespace-definition and an
11500 extension-namespace-definition at this point. The semantic
11501 analysis routines are responsible for that. */
11502 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11503 identifier = cp_parser_identifier (parser);
11505 identifier = NULL_TREE;
11507 /* Parse any specified attributes. */
11508 attribs = cp_parser_attributes_opt (parser);
11510 /* Look for the `{' to start the namespace. */
11511 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
11512 /* Start the namespace. */
11513 push_namespace (identifier);
11515 has_visibility = handle_namespace_attrs (current_namespace, attribs);
11517 /* Parse the body of the namespace. */
11518 cp_parser_namespace_body (parser);
11520 #ifdef HANDLE_PRAGMA_VISIBILITY
11521 if (has_visibility)
11525 /* Finish the namespace. */
11527 /* Look for the final `}'. */
11528 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11531 /* Parse a namespace-body.
11534 declaration-seq [opt] */
11537 cp_parser_namespace_body (cp_parser* parser)
11539 cp_parser_declaration_seq_opt (parser);
11542 /* Parse a namespace-alias-definition.
11544 namespace-alias-definition:
11545 namespace identifier = qualified-namespace-specifier ; */
11548 cp_parser_namespace_alias_definition (cp_parser* parser)
11551 tree namespace_specifier;
11553 /* Look for the `namespace' keyword. */
11554 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11555 /* Look for the identifier. */
11556 identifier = cp_parser_identifier (parser);
11557 if (identifier == error_mark_node)
11559 /* Look for the `=' token. */
11560 if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
11561 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11563 error ("%<namespace%> definition is not allowed here");
11564 /* Skip the definition. */
11565 cp_lexer_consume_token (parser->lexer);
11566 if (cp_parser_skip_to_closing_brace (parser))
11567 cp_lexer_consume_token (parser->lexer);
11570 cp_parser_require (parser, CPP_EQ, "`='");
11571 /* Look for the qualified-namespace-specifier. */
11572 namespace_specifier
11573 = cp_parser_qualified_namespace_specifier (parser);
11574 /* Look for the `;' token. */
11575 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11577 /* Register the alias in the symbol table. */
11578 do_namespace_alias (identifier, namespace_specifier);
11581 /* Parse a qualified-namespace-specifier.
11583 qualified-namespace-specifier:
11584 :: [opt] nested-name-specifier [opt] namespace-name
11586 Returns a NAMESPACE_DECL corresponding to the specified
11590 cp_parser_qualified_namespace_specifier (cp_parser* parser)
11592 /* Look for the optional `::'. */
11593 cp_parser_global_scope_opt (parser,
11594 /*current_scope_valid_p=*/false);
11596 /* Look for the optional nested-name-specifier. */
11597 cp_parser_nested_name_specifier_opt (parser,
11598 /*typename_keyword_p=*/false,
11599 /*check_dependency_p=*/true,
11601 /*is_declaration=*/true);
11603 return cp_parser_namespace_name (parser);
11606 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
11607 access declaration.
11610 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
11611 using :: unqualified-id ;
11613 access-declaration:
11619 cp_parser_using_declaration (cp_parser* parser,
11620 bool access_declaration_p)
11623 bool typename_p = false;
11624 bool global_scope_p;
11629 if (access_declaration_p)
11630 cp_parser_parse_tentatively (parser);
11633 /* Look for the `using' keyword. */
11634 cp_parser_require_keyword (parser, RID_USING, "`using'");
11636 /* Peek at the next token. */
11637 token = cp_lexer_peek_token (parser->lexer);
11638 /* See if it's `typename'. */
11639 if (token->keyword == RID_TYPENAME)
11641 /* Remember that we've seen it. */
11643 /* Consume the `typename' token. */
11644 cp_lexer_consume_token (parser->lexer);
11648 /* Look for the optional global scope qualification. */
11650 = (cp_parser_global_scope_opt (parser,
11651 /*current_scope_valid_p=*/false)
11654 /* If we saw `typename', or didn't see `::', then there must be a
11655 nested-name-specifier present. */
11656 if (typename_p || !global_scope_p)
11657 qscope = cp_parser_nested_name_specifier (parser, typename_p,
11658 /*check_dependency_p=*/true,
11660 /*is_declaration=*/true);
11661 /* Otherwise, we could be in either of the two productions. In that
11662 case, treat the nested-name-specifier as optional. */
11664 qscope = cp_parser_nested_name_specifier_opt (parser,
11665 /*typename_keyword_p=*/false,
11666 /*check_dependency_p=*/true,
11668 /*is_declaration=*/true);
11670 qscope = global_namespace;
11672 if (access_declaration_p && cp_parser_error_occurred (parser))
11673 /* Something has already gone wrong; there's no need to parse
11674 further. Since an error has occurred, the return value of
11675 cp_parser_parse_definitely will be false, as required. */
11676 return cp_parser_parse_definitely (parser);
11678 /* Parse the unqualified-id. */
11679 identifier = cp_parser_unqualified_id (parser,
11680 /*template_keyword_p=*/false,
11681 /*check_dependency_p=*/true,
11682 /*declarator_p=*/true,
11683 /*optional_p=*/false);
11685 if (access_declaration_p)
11687 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11688 cp_parser_simulate_error (parser);
11689 if (!cp_parser_parse_definitely (parser))
11693 /* The function we call to handle a using-declaration is different
11694 depending on what scope we are in. */
11695 if (qscope == error_mark_node || identifier == error_mark_node)
11697 else if (TREE_CODE (identifier) != IDENTIFIER_NODE
11698 && TREE_CODE (identifier) != BIT_NOT_EXPR)
11699 /* [namespace.udecl]
11701 A using declaration shall not name a template-id. */
11702 error ("a template-id may not appear in a using-declaration");
11705 if (at_class_scope_p ())
11707 /* Create the USING_DECL. */
11708 decl = do_class_using_decl (parser->scope, identifier);
11709 /* Add it to the list of members in this class. */
11710 finish_member_declaration (decl);
11714 decl = cp_parser_lookup_name_simple (parser, identifier);
11715 if (decl == error_mark_node)
11716 cp_parser_name_lookup_error (parser, identifier, decl, NULL);
11717 else if (!at_namespace_scope_p ())
11718 do_local_using_decl (decl, qscope, identifier);
11720 do_toplevel_using_decl (decl, qscope, identifier);
11724 /* Look for the final `;'. */
11725 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11730 /* Parse a using-directive.
11733 using namespace :: [opt] nested-name-specifier [opt]
11734 namespace-name ; */
11737 cp_parser_using_directive (cp_parser* parser)
11739 tree namespace_decl;
11742 /* Look for the `using' keyword. */
11743 cp_parser_require_keyword (parser, RID_USING, "`using'");
11744 /* And the `namespace' keyword. */
11745 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
11746 /* Look for the optional `::' operator. */
11747 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
11748 /* And the optional nested-name-specifier. */
11749 cp_parser_nested_name_specifier_opt (parser,
11750 /*typename_keyword_p=*/false,
11751 /*check_dependency_p=*/true,
11753 /*is_declaration=*/true);
11754 /* Get the namespace being used. */
11755 namespace_decl = cp_parser_namespace_name (parser);
11756 /* And any specified attributes. */
11757 attribs = cp_parser_attributes_opt (parser);
11758 /* Update the symbol table. */
11759 parse_using_directive (namespace_decl, attribs);
11760 /* Look for the final `;'. */
11761 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11764 /* Parse an asm-definition.
11767 asm ( string-literal ) ;
11772 asm volatile [opt] ( string-literal ) ;
11773 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
11774 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11775 : asm-operand-list [opt] ) ;
11776 asm volatile [opt] ( string-literal : asm-operand-list [opt]
11777 : asm-operand-list [opt]
11778 : asm-operand-list [opt] ) ; */
11781 cp_parser_asm_definition (cp_parser* parser)
11784 tree outputs = NULL_TREE;
11785 tree inputs = NULL_TREE;
11786 tree clobbers = NULL_TREE;
11788 bool volatile_p = false;
11789 bool extended_p = false;
11790 bool invalid_inputs_p = false;
11791 bool invalid_outputs_p = false;
11793 /* Look for the `asm' keyword. */
11794 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
11795 /* See if the next token is `volatile'. */
11796 if (cp_parser_allow_gnu_extensions_p (parser)
11797 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
11799 /* Remember that we saw the `volatile' keyword. */
11801 /* Consume the token. */
11802 cp_lexer_consume_token (parser->lexer);
11804 /* Look for the opening `('. */
11805 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
11807 /* Look for the string. */
11808 string = cp_parser_string_literal (parser, false, false);
11809 if (string == error_mark_node)
11811 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11812 /*consume_paren=*/true);
11816 /* If we're allowing GNU extensions, check for the extended assembly
11817 syntax. Unfortunately, the `:' tokens need not be separated by
11818 a space in C, and so, for compatibility, we tolerate that here
11819 too. Doing that means that we have to treat the `::' operator as
11821 if (cp_parser_allow_gnu_extensions_p (parser)
11822 && parser->in_function_body
11823 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
11824 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
11826 bool inputs_p = false;
11827 bool clobbers_p = false;
11829 /* The extended syntax was used. */
11832 /* Look for outputs. */
11833 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11835 /* Consume the `:'. */
11836 cp_lexer_consume_token (parser->lexer);
11837 /* Parse the output-operands. */
11838 if (cp_lexer_next_token_is_not (parser->lexer,
11840 && cp_lexer_next_token_is_not (parser->lexer,
11842 && cp_lexer_next_token_is_not (parser->lexer,
11844 outputs = cp_parser_asm_operand_list (parser);
11846 if (outputs == error_mark_node)
11847 invalid_outputs_p = true;
11849 /* If the next token is `::', there are no outputs, and the
11850 next token is the beginning of the inputs. */
11851 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11852 /* The inputs are coming next. */
11855 /* Look for inputs. */
11857 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11859 /* Consume the `:' or `::'. */
11860 cp_lexer_consume_token (parser->lexer);
11861 /* Parse the output-operands. */
11862 if (cp_lexer_next_token_is_not (parser->lexer,
11864 && cp_lexer_next_token_is_not (parser->lexer,
11866 inputs = cp_parser_asm_operand_list (parser);
11868 if (inputs == error_mark_node)
11869 invalid_inputs_p = true;
11871 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11872 /* The clobbers are coming next. */
11875 /* Look for clobbers. */
11877 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
11879 /* Consume the `:' or `::'. */
11880 cp_lexer_consume_token (parser->lexer);
11881 /* Parse the clobbers. */
11882 if (cp_lexer_next_token_is_not (parser->lexer,
11884 clobbers = cp_parser_asm_clobber_list (parser);
11887 /* Look for the closing `)'. */
11888 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11889 cp_parser_skip_to_closing_parenthesis (parser, true, false,
11890 /*consume_paren=*/true);
11891 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
11893 if (!invalid_inputs_p && !invalid_outputs_p)
11895 /* Create the ASM_EXPR. */
11896 if (parser->in_function_body)
11898 asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
11900 /* If the extended syntax was not used, mark the ASM_EXPR. */
11903 tree temp = asm_stmt;
11904 if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
11905 temp = TREE_OPERAND (temp, 0);
11907 ASM_INPUT_P (temp) = 1;
11911 cgraph_add_asm_node (string);
11915 /* Declarators [gram.dcl.decl] */
11917 /* Parse an init-declarator.
11920 declarator initializer [opt]
11925 declarator asm-specification [opt] attributes [opt] initializer [opt]
11927 function-definition:
11928 decl-specifier-seq [opt] declarator ctor-initializer [opt]
11930 decl-specifier-seq [opt] declarator function-try-block
11934 function-definition:
11935 __extension__ function-definition
11937 The DECL_SPECIFIERS apply to this declarator. Returns a
11938 representation of the entity declared. If MEMBER_P is TRUE, then
11939 this declarator appears in a class scope. The new DECL created by
11940 this declarator is returned.
11942 The CHECKS are access checks that should be performed once we know
11943 what entity is being declared (and, therefore, what classes have
11946 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
11947 for a function-definition here as well. If the declarator is a
11948 declarator for a function-definition, *FUNCTION_DEFINITION_P will
11949 be TRUE upon return. By that point, the function-definition will
11950 have been completely parsed.
11952 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
11956 cp_parser_init_declarator (cp_parser* parser,
11957 cp_decl_specifier_seq *decl_specifiers,
11958 VEC (deferred_access_check,gc)* checks,
11959 bool function_definition_allowed_p,
11961 int declares_class_or_enum,
11962 bool* function_definition_p)
11965 cp_declarator *declarator;
11966 tree prefix_attributes;
11968 tree asm_specification;
11970 tree decl = NULL_TREE;
11972 bool is_initialized;
11973 /* Only valid if IS_INITIALIZED is true. In that case, CPP_EQ if
11974 initialized with "= ..", CPP_OPEN_PAREN if initialized with
11976 enum cpp_ttype initialization_kind;
11977 bool is_parenthesized_init = false;
11978 bool is_non_constant_init;
11979 int ctor_dtor_or_conv_p;
11981 tree pushed_scope = NULL;
11983 /* Gather the attributes that were provided with the
11984 decl-specifiers. */
11985 prefix_attributes = decl_specifiers->attributes;
11987 /* Assume that this is not the declarator for a function
11989 if (function_definition_p)
11990 *function_definition_p = false;
11992 /* Defer access checks while parsing the declarator; we cannot know
11993 what names are accessible until we know what is being
11995 resume_deferring_access_checks ();
11997 /* Parse the declarator. */
11999 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12000 &ctor_dtor_or_conv_p,
12001 /*parenthesized_p=*/NULL,
12002 /*member_p=*/false);
12003 /* Gather up the deferred checks. */
12004 stop_deferring_access_checks ();
12006 /* If the DECLARATOR was erroneous, there's no need to go
12008 if (declarator == cp_error_declarator)
12009 return error_mark_node;
12011 /* Check that the number of template-parameter-lists is OK. */
12012 if (!cp_parser_check_declarator_template_parameters (parser, declarator))
12013 return error_mark_node;
12015 if (declares_class_or_enum & 2)
12016 cp_parser_check_for_definition_in_return_type (declarator,
12017 decl_specifiers->type);
12019 /* Figure out what scope the entity declared by the DECLARATOR is
12020 located in. `grokdeclarator' sometimes changes the scope, so
12021 we compute it now. */
12022 scope = get_scope_of_declarator (declarator);
12024 /* If we're allowing GNU extensions, look for an asm-specification
12026 if (cp_parser_allow_gnu_extensions_p (parser))
12028 /* Look for an asm-specification. */
12029 asm_specification = cp_parser_asm_specification_opt (parser);
12030 /* And attributes. */
12031 attributes = cp_parser_attributes_opt (parser);
12035 asm_specification = NULL_TREE;
12036 attributes = NULL_TREE;
12039 /* Peek at the next token. */
12040 token = cp_lexer_peek_token (parser->lexer);
12041 /* Check to see if the token indicates the start of a
12042 function-definition. */
12043 if (cp_parser_token_starts_function_definition_p (token))
12045 if (!function_definition_allowed_p)
12047 /* If a function-definition should not appear here, issue an
12049 cp_parser_error (parser,
12050 "a function-definition is not allowed here");
12051 return error_mark_node;
12055 /* Neither attributes nor an asm-specification are allowed
12056 on a function-definition. */
12057 if (asm_specification)
12058 error ("an asm-specification is not allowed on a function-definition");
12060 error ("attributes are not allowed on a function-definition");
12061 /* This is a function-definition. */
12062 *function_definition_p = true;
12064 /* Parse the function definition. */
12066 decl = cp_parser_save_member_function_body (parser,
12069 prefix_attributes);
12072 = (cp_parser_function_definition_from_specifiers_and_declarator
12073 (parser, decl_specifiers, prefix_attributes, declarator));
12081 Only in function declarations for constructors, destructors, and
12082 type conversions can the decl-specifier-seq be omitted.
12084 We explicitly postpone this check past the point where we handle
12085 function-definitions because we tolerate function-definitions
12086 that are missing their return types in some modes. */
12087 if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
12089 cp_parser_error (parser,
12090 "expected constructor, destructor, or type conversion");
12091 return error_mark_node;
12094 /* An `=' or an `(' indicates an initializer. */
12095 if (token->type == CPP_EQ
12096 || token->type == CPP_OPEN_PAREN)
12098 is_initialized = true;
12099 initialization_kind = token->type;
12103 /* If the init-declarator isn't initialized and isn't followed by a
12104 `,' or `;', it's not a valid init-declarator. */
12105 if (token->type != CPP_COMMA
12106 && token->type != CPP_SEMICOLON)
12108 cp_parser_error (parser, "expected initializer");
12109 return error_mark_node;
12111 is_initialized = false;
12112 initialization_kind = CPP_EOF;
12115 /* Because start_decl has side-effects, we should only call it if we
12116 know we're going ahead. By this point, we know that we cannot
12117 possibly be looking at any other construct. */
12118 cp_parser_commit_to_tentative_parse (parser);
12120 /* If the decl specifiers were bad, issue an error now that we're
12121 sure this was intended to be a declarator. Then continue
12122 declaring the variable(s), as int, to try to cut down on further
12124 if (decl_specifiers->any_specifiers_p
12125 && decl_specifiers->type == error_mark_node)
12127 cp_parser_error (parser, "invalid type in declaration");
12128 decl_specifiers->type = integer_type_node;
12131 /* Check to see whether or not this declaration is a friend. */
12132 friend_p = cp_parser_friend_p (decl_specifiers);
12134 /* Enter the newly declared entry in the symbol table. If we're
12135 processing a declaration in a class-specifier, we wait until
12136 after processing the initializer. */
12139 if (parser->in_unbraced_linkage_specification_p)
12140 decl_specifiers->storage_class = sc_extern;
12141 decl = start_decl (declarator, decl_specifiers,
12142 is_initialized, attributes, prefix_attributes,
12146 /* Enter the SCOPE. That way unqualified names appearing in the
12147 initializer will be looked up in SCOPE. */
12148 pushed_scope = push_scope (scope);
12150 /* Perform deferred access control checks, now that we know in which
12151 SCOPE the declared entity resides. */
12152 if (!member_p && decl)
12154 tree saved_current_function_decl = NULL_TREE;
12156 /* If the entity being declared is a function, pretend that we
12157 are in its scope. If it is a `friend', it may have access to
12158 things that would not otherwise be accessible. */
12159 if (TREE_CODE (decl) == FUNCTION_DECL)
12161 saved_current_function_decl = current_function_decl;
12162 current_function_decl = decl;
12165 /* Perform access checks for template parameters. */
12166 cp_parser_perform_template_parameter_access_checks (checks);
12168 /* Perform the access control checks for the declarator and the
12169 the decl-specifiers. */
12170 perform_deferred_access_checks ();
12172 /* Restore the saved value. */
12173 if (TREE_CODE (decl) == FUNCTION_DECL)
12174 current_function_decl = saved_current_function_decl;
12177 /* Parse the initializer. */
12178 initializer = NULL_TREE;
12179 is_parenthesized_init = false;
12180 is_non_constant_init = true;
12181 if (is_initialized)
12183 if (function_declarator_p (declarator))
12185 if (initialization_kind == CPP_EQ)
12186 initializer = cp_parser_pure_specifier (parser);
12189 /* If the declaration was erroneous, we don't really
12190 know what the user intended, so just silently
12191 consume the initializer. */
12192 if (decl != error_mark_node)
12193 error ("initializer provided for function");
12194 cp_parser_skip_to_closing_parenthesis (parser,
12195 /*recovering=*/true,
12196 /*or_comma=*/false,
12197 /*consume_paren=*/true);
12201 initializer = cp_parser_initializer (parser,
12202 &is_parenthesized_init,
12203 &is_non_constant_init);
12206 /* The old parser allows attributes to appear after a parenthesized
12207 initializer. Mark Mitchell proposed removing this functionality
12208 on the GCC mailing lists on 2002-08-13. This parser accepts the
12209 attributes -- but ignores them. */
12210 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
12211 if (cp_parser_attributes_opt (parser))
12212 warning (OPT_Wattributes,
12213 "attributes after parenthesized initializer ignored");
12215 /* For an in-class declaration, use `grokfield' to create the
12221 pop_scope (pushed_scope);
12222 pushed_scope = false;
12224 decl = grokfield (declarator, decl_specifiers,
12225 initializer, !is_non_constant_init,
12226 /*asmspec=*/NULL_TREE,
12227 prefix_attributes);
12228 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
12229 cp_parser_save_default_args (parser, decl);
12232 /* Finish processing the declaration. But, skip friend
12234 if (!friend_p && decl && decl != error_mark_node)
12236 cp_finish_decl (decl,
12237 initializer, !is_non_constant_init,
12239 /* If the initializer is in parentheses, then this is
12240 a direct-initialization, which means that an
12241 `explicit' constructor is OK. Otherwise, an
12242 `explicit' constructor cannot be used. */
12243 ((is_parenthesized_init || !is_initialized)
12244 ? 0 : LOOKUP_ONLYCONVERTING));
12246 else if ((cxx_dialect != cxx98) && friend_p
12247 && decl && TREE_CODE (decl) == FUNCTION_DECL)
12248 /* Core issue #226 (C++0x only): A default template-argument
12249 shall not be specified in a friend class template
12251 check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1,
12252 /*is_partial=*/0, /*is_friend_decl=*/1);
12254 if (!friend_p && pushed_scope)
12255 pop_scope (pushed_scope);
12260 /* Parse a declarator.
12264 ptr-operator declarator
12266 abstract-declarator:
12267 ptr-operator abstract-declarator [opt]
12268 direct-abstract-declarator
12273 attributes [opt] direct-declarator
12274 attributes [opt] ptr-operator declarator
12276 abstract-declarator:
12277 attributes [opt] ptr-operator abstract-declarator [opt]
12278 attributes [opt] direct-abstract-declarator
12280 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
12281 detect constructor, destructor or conversion operators. It is set
12282 to -1 if the declarator is a name, and +1 if it is a
12283 function. Otherwise it is set to zero. Usually you just want to
12284 test for >0, but internally the negative value is used.
12286 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
12287 a decl-specifier-seq unless it declares a constructor, destructor,
12288 or conversion. It might seem that we could check this condition in
12289 semantic analysis, rather than parsing, but that makes it difficult
12290 to handle something like `f()'. We want to notice that there are
12291 no decl-specifiers, and therefore realize that this is an
12292 expression, not a declaration.)
12294 If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
12295 the declarator is a direct-declarator of the form "(...)".
12297 MEMBER_P is true iff this declarator is a member-declarator. */
12299 static cp_declarator *
12300 cp_parser_declarator (cp_parser* parser,
12301 cp_parser_declarator_kind dcl_kind,
12302 int* ctor_dtor_or_conv_p,
12303 bool* parenthesized_p,
12307 cp_declarator *declarator;
12308 enum tree_code code;
12309 cp_cv_quals cv_quals;
12311 tree attributes = NULL_TREE;
12313 /* Assume this is not a constructor, destructor, or type-conversion
12315 if (ctor_dtor_or_conv_p)
12316 *ctor_dtor_or_conv_p = 0;
12318 if (cp_parser_allow_gnu_extensions_p (parser))
12319 attributes = cp_parser_attributes_opt (parser);
12321 /* Peek at the next token. */
12322 token = cp_lexer_peek_token (parser->lexer);
12324 /* Check for the ptr-operator production. */
12325 cp_parser_parse_tentatively (parser);
12326 /* Parse the ptr-operator. */
12327 code = cp_parser_ptr_operator (parser,
12330 /* If that worked, then we have a ptr-operator. */
12331 if (cp_parser_parse_definitely (parser))
12333 /* If a ptr-operator was found, then this declarator was not
12335 if (parenthesized_p)
12336 *parenthesized_p = true;
12337 /* The dependent declarator is optional if we are parsing an
12338 abstract-declarator. */
12339 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12340 cp_parser_parse_tentatively (parser);
12342 /* Parse the dependent declarator. */
12343 declarator = cp_parser_declarator (parser, dcl_kind,
12344 /*ctor_dtor_or_conv_p=*/NULL,
12345 /*parenthesized_p=*/NULL,
12346 /*member_p=*/false);
12348 /* If we are parsing an abstract-declarator, we must handle the
12349 case where the dependent declarator is absent. */
12350 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
12351 && !cp_parser_parse_definitely (parser))
12354 declarator = cp_parser_make_indirect_declarator
12355 (code, class_type, cv_quals, declarator);
12357 /* Everything else is a direct-declarator. */
12360 if (parenthesized_p)
12361 *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
12363 declarator = cp_parser_direct_declarator (parser, dcl_kind,
12364 ctor_dtor_or_conv_p,
12368 if (attributes && declarator && declarator != cp_error_declarator)
12369 declarator->attributes = attributes;
12374 /* Parse a direct-declarator or direct-abstract-declarator.
12378 direct-declarator ( parameter-declaration-clause )
12379 cv-qualifier-seq [opt]
12380 exception-specification [opt]
12381 direct-declarator [ constant-expression [opt] ]
12384 direct-abstract-declarator:
12385 direct-abstract-declarator [opt]
12386 ( parameter-declaration-clause )
12387 cv-qualifier-seq [opt]
12388 exception-specification [opt]
12389 direct-abstract-declarator [opt] [ constant-expression [opt] ]
12390 ( abstract-declarator )
12392 Returns a representation of the declarator. DCL_KIND is
12393 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
12394 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
12395 we are parsing a direct-declarator. It is
12396 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
12397 of ambiguity we prefer an abstract declarator, as per
12398 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
12399 cp_parser_declarator. */
12401 static cp_declarator *
12402 cp_parser_direct_declarator (cp_parser* parser,
12403 cp_parser_declarator_kind dcl_kind,
12404 int* ctor_dtor_or_conv_p,
12408 cp_declarator *declarator = NULL;
12409 tree scope = NULL_TREE;
12410 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
12411 bool saved_in_declarator_p = parser->in_declarator_p;
12413 tree pushed_scope = NULL_TREE;
12417 /* Peek at the next token. */
12418 token = cp_lexer_peek_token (parser->lexer);
12419 if (token->type == CPP_OPEN_PAREN)
12421 /* This is either a parameter-declaration-clause, or a
12422 parenthesized declarator. When we know we are parsing a
12423 named declarator, it must be a parenthesized declarator
12424 if FIRST is true. For instance, `(int)' is a
12425 parameter-declaration-clause, with an omitted
12426 direct-abstract-declarator. But `((*))', is a
12427 parenthesized abstract declarator. Finally, when T is a
12428 template parameter `(T)' is a
12429 parameter-declaration-clause, and not a parenthesized
12432 We first try and parse a parameter-declaration-clause,
12433 and then try a nested declarator (if FIRST is true).
12435 It is not an error for it not to be a
12436 parameter-declaration-clause, even when FIRST is
12442 The first is the declaration of a function while the
12443 second is a the definition of a variable, including its
12446 Having seen only the parenthesis, we cannot know which of
12447 these two alternatives should be selected. Even more
12448 complex are examples like:
12453 The former is a function-declaration; the latter is a
12454 variable initialization.
12456 Thus again, we try a parameter-declaration-clause, and if
12457 that fails, we back out and return. */
12459 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12461 cp_parameter_declarator *params;
12462 unsigned saved_num_template_parameter_lists;
12464 /* In a member-declarator, the only valid interpretation
12465 of a parenthesis is the start of a
12466 parameter-declaration-clause. (It is invalid to
12467 initialize a static data member with a parenthesized
12468 initializer; only the "=" form of initialization is
12471 cp_parser_parse_tentatively (parser);
12473 /* Consume the `('. */
12474 cp_lexer_consume_token (parser->lexer);
12477 /* If this is going to be an abstract declarator, we're
12478 in a declarator and we can't have default args. */
12479 parser->default_arg_ok_p = false;
12480 parser->in_declarator_p = true;
12483 /* Inside the function parameter list, surrounding
12484 template-parameter-lists do not apply. */
12485 saved_num_template_parameter_lists
12486 = parser->num_template_parameter_lists;
12487 parser->num_template_parameter_lists = 0;
12489 /* Parse the parameter-declaration-clause. */
12490 params = cp_parser_parameter_declaration_clause (parser);
12492 parser->num_template_parameter_lists
12493 = saved_num_template_parameter_lists;
12495 /* If all went well, parse the cv-qualifier-seq and the
12496 exception-specification. */
12497 if (member_p || cp_parser_parse_definitely (parser))
12499 cp_cv_quals cv_quals;
12500 tree exception_specification;
12502 if (ctor_dtor_or_conv_p)
12503 *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
12505 /* Consume the `)'. */
12506 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12508 /* Parse the cv-qualifier-seq. */
12509 cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12510 /* And the exception-specification. */
12511 exception_specification
12512 = cp_parser_exception_specification_opt (parser);
12514 /* Create the function-declarator. */
12515 declarator = make_call_declarator (declarator,
12518 exception_specification);
12519 /* Any subsequent parameter lists are to do with
12520 return type, so are not those of the declared
12522 parser->default_arg_ok_p = false;
12524 /* Repeat the main loop. */
12529 /* If this is the first, we can try a parenthesized
12533 bool saved_in_type_id_in_expr_p;
12535 parser->default_arg_ok_p = saved_default_arg_ok_p;
12536 parser->in_declarator_p = saved_in_declarator_p;
12538 /* Consume the `('. */
12539 cp_lexer_consume_token (parser->lexer);
12540 /* Parse the nested declarator. */
12541 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
12542 parser->in_type_id_in_expr_p = true;
12544 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
12545 /*parenthesized_p=*/NULL,
12547 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
12549 /* Expect a `)'. */
12550 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
12551 declarator = cp_error_declarator;
12552 if (declarator == cp_error_declarator)
12555 goto handle_declarator;
12557 /* Otherwise, we must be done. */
12561 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
12562 && token->type == CPP_OPEN_SQUARE)
12564 /* Parse an array-declarator. */
12567 if (ctor_dtor_or_conv_p)
12568 *ctor_dtor_or_conv_p = 0;
12571 parser->default_arg_ok_p = false;
12572 parser->in_declarator_p = true;
12573 /* Consume the `['. */
12574 cp_lexer_consume_token (parser->lexer);
12575 /* Peek at the next token. */
12576 token = cp_lexer_peek_token (parser->lexer);
12577 /* If the next token is `]', then there is no
12578 constant-expression. */
12579 if (token->type != CPP_CLOSE_SQUARE)
12581 bool non_constant_p;
12584 = cp_parser_constant_expression (parser,
12585 /*allow_non_constant=*/true,
12587 if (!non_constant_p)
12588 bounds = fold_non_dependent_expr (bounds);
12589 /* Normally, the array bound must be an integral constant
12590 expression. However, as an extension, we allow VLAs
12591 in function scopes. */
12592 else if (!parser->in_function_body)
12594 error ("array bound is not an integer constant");
12595 bounds = error_mark_node;
12599 bounds = NULL_TREE;
12600 /* Look for the closing `]'. */
12601 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
12603 declarator = cp_error_declarator;
12607 declarator = make_array_declarator (declarator, bounds);
12609 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
12611 tree qualifying_scope;
12612 tree unqualified_name;
12613 special_function_kind sfk;
12615 bool pack_expansion_p = false;
12617 /* Parse a declarator-id */
12618 abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
12621 cp_parser_parse_tentatively (parser);
12623 /* If we see an ellipsis, we should be looking at a
12625 if (token->type == CPP_ELLIPSIS)
12627 /* Consume the `...' */
12628 cp_lexer_consume_token (parser->lexer);
12630 pack_expansion_p = true;
12635 = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
12636 qualifying_scope = parser->scope;
12641 if (!unqualified_name && pack_expansion_p)
12643 /* Check whether an error occurred. */
12644 okay = !cp_parser_error_occurred (parser);
12646 /* We already consumed the ellipsis to mark a
12647 parameter pack, but we have no way to report it,
12648 so abort the tentative parse. We will be exiting
12649 immediately anyway. */
12650 cp_parser_abort_tentative_parse (parser);
12653 okay = cp_parser_parse_definitely (parser);
12656 unqualified_name = error_mark_node;
12657 else if (unqualified_name
12658 && (qualifying_scope
12659 || (TREE_CODE (unqualified_name)
12660 != IDENTIFIER_NODE)))
12662 cp_parser_error (parser, "expected unqualified-id");
12663 unqualified_name = error_mark_node;
12667 if (!unqualified_name)
12669 if (unqualified_name == error_mark_node)
12671 declarator = cp_error_declarator;
12672 pack_expansion_p = false;
12673 declarator->parameter_pack_p = false;
12677 if (qualifying_scope && at_namespace_scope_p ()
12678 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
12680 /* In the declaration of a member of a template class
12681 outside of the class itself, the SCOPE will sometimes
12682 be a TYPENAME_TYPE. For example, given:
12684 template <typename T>
12685 int S<T>::R::i = 3;
12687 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
12688 this context, we must resolve S<T>::R to an ordinary
12689 type, rather than a typename type.
12691 The reason we normally avoid resolving TYPENAME_TYPEs
12692 is that a specialization of `S' might render
12693 `S<T>::R' not a type. However, if `S' is
12694 specialized, then this `i' will not be used, so there
12695 is no harm in resolving the types here. */
12698 /* Resolve the TYPENAME_TYPE. */
12699 type = resolve_typename_type (qualifying_scope,
12700 /*only_current_p=*/false);
12701 /* If that failed, the declarator is invalid. */
12702 if (TREE_CODE (type) == TYPENAME_TYPE)
12703 error ("%<%T::%E%> is not a type",
12704 TYPE_CONTEXT (qualifying_scope),
12705 TYPE_IDENTIFIER (qualifying_scope));
12706 qualifying_scope = type;
12711 if (unqualified_name)
12715 if (qualifying_scope
12716 && CLASS_TYPE_P (qualifying_scope))
12717 class_type = qualifying_scope;
12719 class_type = current_class_type;
12721 if (TREE_CODE (unqualified_name) == TYPE_DECL)
12723 tree name_type = TREE_TYPE (unqualified_name);
12724 if (class_type && same_type_p (name_type, class_type))
12726 if (qualifying_scope
12727 && CLASSTYPE_USE_TEMPLATE (name_type))
12729 error ("invalid use of constructor as a template");
12730 inform ("use %<%T::%D%> instead of %<%T::%D%> to "
12731 "name the constructor in a qualified name",
12733 DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
12734 class_type, name_type);
12735 declarator = cp_error_declarator;
12739 unqualified_name = constructor_name (class_type);
12743 /* We do not attempt to print the declarator
12744 here because we do not have enough
12745 information about its original syntactic
12747 cp_parser_error (parser, "invalid declarator");
12748 declarator = cp_error_declarator;
12755 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
12756 sfk = sfk_destructor;
12757 else if (IDENTIFIER_TYPENAME_P (unqualified_name))
12758 sfk = sfk_conversion;
12759 else if (/* There's no way to declare a constructor
12760 for an anonymous type, even if the type
12761 got a name for linkage purposes. */
12762 !TYPE_WAS_ANONYMOUS (class_type)
12763 && constructor_name_p (unqualified_name,
12766 unqualified_name = constructor_name (class_type);
12767 sfk = sfk_constructor;
12770 if (ctor_dtor_or_conv_p && sfk != sfk_none)
12771 *ctor_dtor_or_conv_p = -1;
12774 declarator = make_id_declarator (qualifying_scope,
12777 declarator->id_loc = token->location;
12778 declarator->parameter_pack_p = pack_expansion_p;
12780 if (pack_expansion_p)
12781 maybe_warn_variadic_templates ();
12783 handle_declarator:;
12784 scope = get_scope_of_declarator (declarator);
12786 /* Any names that appear after the declarator-id for a
12787 member are looked up in the containing scope. */
12788 pushed_scope = push_scope (scope);
12789 parser->in_declarator_p = true;
12790 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
12791 || (declarator && declarator->kind == cdk_id))
12792 /* Default args are only allowed on function
12794 parser->default_arg_ok_p = saved_default_arg_ok_p;
12796 parser->default_arg_ok_p = false;
12805 /* For an abstract declarator, we might wind up with nothing at this
12806 point. That's an error; the declarator is not optional. */
12808 cp_parser_error (parser, "expected declarator");
12810 /* If we entered a scope, we must exit it now. */
12812 pop_scope (pushed_scope);
12814 parser->default_arg_ok_p = saved_default_arg_ok_p;
12815 parser->in_declarator_p = saved_in_declarator_p;
12820 /* Parse a ptr-operator.
12823 * cv-qualifier-seq [opt]
12825 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
12830 & cv-qualifier-seq [opt]
12832 Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
12833 Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
12834 an rvalue reference. In the case of a pointer-to-member, *TYPE is
12835 filled in with the TYPE containing the member. *CV_QUALS is
12836 filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
12837 are no cv-qualifiers. Returns ERROR_MARK if an error occurred.
12838 Note that the tree codes returned by this function have nothing
12839 to do with the types of trees that will be eventually be created
12840 to represent the pointer or reference type being parsed. They are
12841 just constants with suggestive names. */
12842 static enum tree_code
12843 cp_parser_ptr_operator (cp_parser* parser,
12845 cp_cv_quals *cv_quals)
12847 enum tree_code code = ERROR_MARK;
12850 /* Assume that it's not a pointer-to-member. */
12852 /* And that there are no cv-qualifiers. */
12853 *cv_quals = TYPE_UNQUALIFIED;
12855 /* Peek at the next token. */
12856 token = cp_lexer_peek_token (parser->lexer);
12858 /* If it's a `*', `&' or `&&' we have a pointer or reference. */
12859 if (token->type == CPP_MULT)
12860 code = INDIRECT_REF;
12861 else if (token->type == CPP_AND)
12863 else if ((cxx_dialect != cxx98) &&
12864 token->type == CPP_AND_AND) /* C++0x only */
12865 code = NON_LVALUE_EXPR;
12867 if (code != ERROR_MARK)
12869 /* Consume the `*', `&' or `&&'. */
12870 cp_lexer_consume_token (parser->lexer);
12872 /* A `*' can be followed by a cv-qualifier-seq, and so can a
12873 `&', if we are allowing GNU extensions. (The only qualifier
12874 that can legally appear after `&' is `restrict', but that is
12875 enforced during semantic analysis. */
12876 if (code == INDIRECT_REF
12877 || cp_parser_allow_gnu_extensions_p (parser))
12878 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12882 /* Try the pointer-to-member case. */
12883 cp_parser_parse_tentatively (parser);
12884 /* Look for the optional `::' operator. */
12885 cp_parser_global_scope_opt (parser,
12886 /*current_scope_valid_p=*/false);
12887 /* Look for the nested-name specifier. */
12888 cp_parser_nested_name_specifier (parser,
12889 /*typename_keyword_p=*/false,
12890 /*check_dependency_p=*/true,
12892 /*is_declaration=*/false);
12893 /* If we found it, and the next token is a `*', then we are
12894 indeed looking at a pointer-to-member operator. */
12895 if (!cp_parser_error_occurred (parser)
12896 && cp_parser_require (parser, CPP_MULT, "`*'"))
12898 /* Indicate that the `*' operator was used. */
12899 code = INDIRECT_REF;
12901 if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
12902 error ("%qD is a namespace", parser->scope);
12905 /* The type of which the member is a member is given by the
12907 *type = parser->scope;
12908 /* The next name will not be qualified. */
12909 parser->scope = NULL_TREE;
12910 parser->qualifying_scope = NULL_TREE;
12911 parser->object_scope = NULL_TREE;
12912 /* Look for the optional cv-qualifier-seq. */
12913 *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
12916 /* If that didn't work we don't have a ptr-operator. */
12917 if (!cp_parser_parse_definitely (parser))
12918 cp_parser_error (parser, "expected ptr-operator");
12924 /* Parse an (optional) cv-qualifier-seq.
12927 cv-qualifier cv-qualifier-seq [opt]
12938 Returns a bitmask representing the cv-qualifiers. */
12941 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
12943 cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
12948 cp_cv_quals cv_qualifier;
12950 /* Peek at the next token. */
12951 token = cp_lexer_peek_token (parser->lexer);
12952 /* See if it's a cv-qualifier. */
12953 switch (token->keyword)
12956 cv_qualifier = TYPE_QUAL_CONST;
12960 cv_qualifier = TYPE_QUAL_VOLATILE;
12964 cv_qualifier = TYPE_QUAL_RESTRICT;
12968 cv_qualifier = TYPE_UNQUALIFIED;
12975 if (cv_quals & cv_qualifier)
12977 error ("duplicate cv-qualifier");
12978 cp_lexer_purge_token (parser->lexer);
12982 cp_lexer_consume_token (parser->lexer);
12983 cv_quals |= cv_qualifier;
12990 /* Parse a declarator-id.
12994 :: [opt] nested-name-specifier [opt] type-name
12996 In the `id-expression' case, the value returned is as for
12997 cp_parser_id_expression if the id-expression was an unqualified-id.
12998 If the id-expression was a qualified-id, then a SCOPE_REF is
12999 returned. The first operand is the scope (either a NAMESPACE_DECL
13000 or TREE_TYPE), but the second is still just a representation of an
13004 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
13007 /* The expression must be an id-expression. Assume that qualified
13008 names are the names of types so that:
13011 int S<T>::R::i = 3;
13013 will work; we must treat `S<T>::R' as the name of a type.
13014 Similarly, assume that qualified names are templates, where
13018 int S<T>::R<T>::i = 3;
13021 id = cp_parser_id_expression (parser,
13022 /*template_keyword_p=*/false,
13023 /*check_dependency_p=*/false,
13024 /*template_p=*/NULL,
13025 /*declarator_p=*/true,
13027 if (id && BASELINK_P (id))
13028 id = BASELINK_FUNCTIONS (id);
13032 /* Parse a type-id.
13035 type-specifier-seq abstract-declarator [opt]
13037 Returns the TYPE specified. */
13040 cp_parser_type_id (cp_parser* parser)
13042 cp_decl_specifier_seq type_specifier_seq;
13043 cp_declarator *abstract_declarator;
13045 /* Parse the type-specifier-seq. */
13046 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13047 &type_specifier_seq);
13048 if (type_specifier_seq.type == error_mark_node)
13049 return error_mark_node;
13051 /* There might or might not be an abstract declarator. */
13052 cp_parser_parse_tentatively (parser);
13053 /* Look for the declarator. */
13054 abstract_declarator
13055 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
13056 /*parenthesized_p=*/NULL,
13057 /*member_p=*/false);
13058 /* Check to see if there really was a declarator. */
13059 if (!cp_parser_parse_definitely (parser))
13060 abstract_declarator = NULL;
13062 return groktypename (&type_specifier_seq, abstract_declarator);
13065 /* Parse a type-specifier-seq.
13067 type-specifier-seq:
13068 type-specifier type-specifier-seq [opt]
13072 type-specifier-seq:
13073 attributes type-specifier-seq [opt]
13075 If IS_CONDITION is true, we are at the start of a "condition",
13076 e.g., we've just seen "if (".
13078 Sets *TYPE_SPECIFIER_SEQ to represent the sequence. */
13081 cp_parser_type_specifier_seq (cp_parser* parser,
13083 cp_decl_specifier_seq *type_specifier_seq)
13085 bool seen_type_specifier = false;
13086 cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
13088 /* Clear the TYPE_SPECIFIER_SEQ. */
13089 clear_decl_specs (type_specifier_seq);
13091 /* Parse the type-specifiers and attributes. */
13094 tree type_specifier;
13095 bool is_cv_qualifier;
13097 /* Check for attributes first. */
13098 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
13100 type_specifier_seq->attributes =
13101 chainon (type_specifier_seq->attributes,
13102 cp_parser_attributes_opt (parser));
13106 /* Look for the type-specifier. */
13107 type_specifier = cp_parser_type_specifier (parser,
13109 type_specifier_seq,
13110 /*is_declaration=*/false,
13113 if (!type_specifier)
13115 /* If the first type-specifier could not be found, this is not a
13116 type-specifier-seq at all. */
13117 if (!seen_type_specifier)
13119 cp_parser_error (parser, "expected type-specifier");
13120 type_specifier_seq->type = error_mark_node;
13123 /* If subsequent type-specifiers could not be found, the
13124 type-specifier-seq is complete. */
13128 seen_type_specifier = true;
13129 /* The standard says that a condition can be:
13131 type-specifier-seq declarator = assignment-expression
13138 we should treat the "S" as a declarator, not as a
13139 type-specifier. The standard doesn't say that explicitly for
13140 type-specifier-seq, but it does say that for
13141 decl-specifier-seq in an ordinary declaration. Perhaps it
13142 would be clearer just to allow a decl-specifier-seq here, and
13143 then add a semantic restriction that if any decl-specifiers
13144 that are not type-specifiers appear, the program is invalid. */
13145 if (is_condition && !is_cv_qualifier)
13146 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
13149 cp_parser_check_decl_spec (type_specifier_seq);
13152 /* Parse a parameter-declaration-clause.
13154 parameter-declaration-clause:
13155 parameter-declaration-list [opt] ... [opt]
13156 parameter-declaration-list , ...
13158 Returns a representation for the parameter declarations. A return
13159 value of NULL indicates a parameter-declaration-clause consisting
13160 only of an ellipsis. */
13162 static cp_parameter_declarator *
13163 cp_parser_parameter_declaration_clause (cp_parser* parser)
13165 cp_parameter_declarator *parameters;
13170 /* Peek at the next token. */
13171 token = cp_lexer_peek_token (parser->lexer);
13172 /* Check for trivial parameter-declaration-clauses. */
13173 if (token->type == CPP_ELLIPSIS)
13175 /* Consume the `...' token. */
13176 cp_lexer_consume_token (parser->lexer);
13179 else if (token->type == CPP_CLOSE_PAREN)
13180 /* There are no parameters. */
13182 #ifndef NO_IMPLICIT_EXTERN_C
13183 if (in_system_header && current_class_type == NULL
13184 && current_lang_name == lang_name_c)
13188 return no_parameters;
13190 /* Check for `(void)', too, which is a special case. */
13191 else if (token->keyword == RID_VOID
13192 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
13193 == CPP_CLOSE_PAREN))
13195 /* Consume the `void' token. */
13196 cp_lexer_consume_token (parser->lexer);
13197 /* There are no parameters. */
13198 return no_parameters;
13201 /* Parse the parameter-declaration-list. */
13202 parameters = cp_parser_parameter_declaration_list (parser, &is_error);
13203 /* If a parse error occurred while parsing the
13204 parameter-declaration-list, then the entire
13205 parameter-declaration-clause is erroneous. */
13209 /* Peek at the next token. */
13210 token = cp_lexer_peek_token (parser->lexer);
13211 /* If it's a `,', the clause should terminate with an ellipsis. */
13212 if (token->type == CPP_COMMA)
13214 /* Consume the `,'. */
13215 cp_lexer_consume_token (parser->lexer);
13216 /* Expect an ellipsis. */
13218 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
13220 /* It might also be `...' if the optional trailing `,' was
13222 else if (token->type == CPP_ELLIPSIS)
13224 /* Consume the `...' token. */
13225 cp_lexer_consume_token (parser->lexer);
13226 /* And remember that we saw it. */
13230 ellipsis_p = false;
13232 /* Finish the parameter list. */
13233 if (parameters && ellipsis_p)
13234 parameters->ellipsis_p = true;
13239 /* Parse a parameter-declaration-list.
13241 parameter-declaration-list:
13242 parameter-declaration
13243 parameter-declaration-list , parameter-declaration
13245 Returns a representation of the parameter-declaration-list, as for
13246 cp_parser_parameter_declaration_clause. However, the
13247 `void_list_node' is never appended to the list. Upon return,
13248 *IS_ERROR will be true iff an error occurred. */
13250 static cp_parameter_declarator *
13251 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
13253 cp_parameter_declarator *parameters = NULL;
13254 cp_parameter_declarator **tail = ¶meters;
13255 bool saved_in_unbraced_linkage_specification_p;
13257 /* Assume all will go well. */
13259 /* The special considerations that apply to a function within an
13260 unbraced linkage specifications do not apply to the parameters
13261 to the function. */
13262 saved_in_unbraced_linkage_specification_p
13263 = parser->in_unbraced_linkage_specification_p;
13264 parser->in_unbraced_linkage_specification_p = false;
13266 /* Look for more parameters. */
13269 cp_parameter_declarator *parameter;
13270 bool parenthesized_p;
13271 /* Parse the parameter. */
13273 = cp_parser_parameter_declaration (parser,
13274 /*template_parm_p=*/false,
13277 /* If a parse error occurred parsing the parameter declaration,
13278 then the entire parameter-declaration-list is erroneous. */
13285 /* Add the new parameter to the list. */
13287 tail = ¶meter->next;
13289 /* Peek at the next token. */
13290 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
13291 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
13292 /* These are for Objective-C++ */
13293 || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13294 || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13295 /* The parameter-declaration-list is complete. */
13297 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13301 /* Peek at the next token. */
13302 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13303 /* If it's an ellipsis, then the list is complete. */
13304 if (token->type == CPP_ELLIPSIS)
13306 /* Otherwise, there must be more parameters. Consume the
13308 cp_lexer_consume_token (parser->lexer);
13309 /* When parsing something like:
13311 int i(float f, double d)
13313 we can tell after seeing the declaration for "f" that we
13314 are not looking at an initialization of a variable "i",
13315 but rather at the declaration of a function "i".
13317 Due to the fact that the parsing of template arguments
13318 (as specified to a template-id) requires backtracking we
13319 cannot use this technique when inside a template argument
13321 if (!parser->in_template_argument_list_p
13322 && !parser->in_type_id_in_expr_p
13323 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13324 /* However, a parameter-declaration of the form
13325 "foat(f)" (which is a valid declaration of a
13326 parameter "f") can also be interpreted as an
13327 expression (the conversion of "f" to "float"). */
13328 && !parenthesized_p)
13329 cp_parser_commit_to_tentative_parse (parser);
13333 cp_parser_error (parser, "expected %<,%> or %<...%>");
13334 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13335 cp_parser_skip_to_closing_parenthesis (parser,
13336 /*recovering=*/true,
13337 /*or_comma=*/false,
13338 /*consume_paren=*/false);
13343 parser->in_unbraced_linkage_specification_p
13344 = saved_in_unbraced_linkage_specification_p;
13349 /* Parse a parameter declaration.
13351 parameter-declaration:
13352 decl-specifier-seq ... [opt] declarator
13353 decl-specifier-seq declarator = assignment-expression
13354 decl-specifier-seq ... [opt] abstract-declarator [opt]
13355 decl-specifier-seq abstract-declarator [opt] = assignment-expression
13357 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
13358 declares a template parameter. (In that case, a non-nested `>'
13359 token encountered during the parsing of the assignment-expression
13360 is not interpreted as a greater-than operator.)
13362 Returns a representation of the parameter, or NULL if an error
13363 occurs. If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
13364 true iff the declarator is of the form "(p)". */
13366 static cp_parameter_declarator *
13367 cp_parser_parameter_declaration (cp_parser *parser,
13368 bool template_parm_p,
13369 bool *parenthesized_p)
13371 int declares_class_or_enum;
13372 bool greater_than_is_operator_p;
13373 cp_decl_specifier_seq decl_specifiers;
13374 cp_declarator *declarator;
13375 tree default_argument;
13377 const char *saved_message;
13379 /* In a template parameter, `>' is not an operator.
13383 When parsing a default template-argument for a non-type
13384 template-parameter, the first non-nested `>' is taken as the end
13385 of the template parameter-list rather than a greater-than
13387 greater_than_is_operator_p = !template_parm_p;
13389 /* Type definitions may not appear in parameter types. */
13390 saved_message = parser->type_definition_forbidden_message;
13391 parser->type_definition_forbidden_message
13392 = "types may not be defined in parameter types";
13394 /* Parse the declaration-specifiers. */
13395 cp_parser_decl_specifier_seq (parser,
13396 CP_PARSER_FLAGS_NONE,
13398 &declares_class_or_enum);
13399 /* If an error occurred, there's no reason to attempt to parse the
13400 rest of the declaration. */
13401 if (cp_parser_error_occurred (parser))
13403 parser->type_definition_forbidden_message = saved_message;
13407 /* Peek at the next token. */
13408 token = cp_lexer_peek_token (parser->lexer);
13410 /* If the next token is a `)', `,', `=', `>', or `...', then there
13411 is no declarator. However, when variadic templates are enabled,
13412 there may be a declarator following `...'. */
13413 if (token->type == CPP_CLOSE_PAREN
13414 || token->type == CPP_COMMA
13415 || token->type == CPP_EQ
13416 || token->type == CPP_GREATER)
13419 if (parenthesized_p)
13420 *parenthesized_p = false;
13422 /* Otherwise, there should be a declarator. */
13425 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13426 parser->default_arg_ok_p = false;
13428 /* After seeing a decl-specifier-seq, if the next token is not a
13429 "(", there is no possibility that the code is a valid
13430 expression. Therefore, if parsing tentatively, we commit at
13432 if (!parser->in_template_argument_list_p
13433 /* In an expression context, having seen:
13437 we cannot be sure whether we are looking at a
13438 function-type (taking a "char" as a parameter) or a cast
13439 of some object of type "char" to "int". */
13440 && !parser->in_type_id_in_expr_p
13441 && cp_parser_uncommitted_to_tentative_parse_p (parser)
13442 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
13443 cp_parser_commit_to_tentative_parse (parser);
13444 /* Parse the declarator. */
13445 declarator = cp_parser_declarator (parser,
13446 CP_PARSER_DECLARATOR_EITHER,
13447 /*ctor_dtor_or_conv_p=*/NULL,
13449 /*member_p=*/false);
13450 parser->default_arg_ok_p = saved_default_arg_ok_p;
13451 /* After the declarator, allow more attributes. */
13452 decl_specifiers.attributes
13453 = chainon (decl_specifiers.attributes,
13454 cp_parser_attributes_opt (parser));
13457 /* If the next token is an ellipsis, and we have not seen a
13458 declarator name, and the type of the declarator contains parameter
13459 packs but it is not a TYPE_PACK_EXPANSION, then we actually have
13460 a parameter pack expansion expression. Otherwise, leave the
13461 ellipsis for a C-style variadic function. */
13462 token = cp_lexer_peek_token (parser->lexer);
13463 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13465 tree type = decl_specifiers.type;
13467 if (type && DECL_P (type))
13468 type = TREE_TYPE (type);
13471 && TREE_CODE (type) != TYPE_PACK_EXPANSION
13472 && declarator_can_be_parameter_pack (declarator)
13473 && (!declarator || !declarator->parameter_pack_p)
13474 && uses_parameter_packs (type))
13476 /* Consume the `...'. */
13477 cp_lexer_consume_token (parser->lexer);
13478 maybe_warn_variadic_templates ();
13480 /* Build a pack expansion type */
13482 declarator->parameter_pack_p = true;
13484 decl_specifiers.type = make_pack_expansion (type);
13488 /* The restriction on defining new types applies only to the type
13489 of the parameter, not to the default argument. */
13490 parser->type_definition_forbidden_message = saved_message;
13492 /* If the next token is `=', then process a default argument. */
13493 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13495 bool saved_greater_than_is_operator_p;
13496 /* Consume the `='. */
13497 cp_lexer_consume_token (parser->lexer);
13499 /* If we are defining a class, then the tokens that make up the
13500 default argument must be saved and processed later. */
13501 if (!template_parm_p && at_class_scope_p ()
13502 && TYPE_BEING_DEFINED (current_class_type))
13504 unsigned depth = 0;
13505 cp_token *first_token;
13508 /* Add tokens until we have processed the entire default
13509 argument. We add the range [first_token, token). */
13510 first_token = cp_lexer_peek_token (parser->lexer);
13515 /* Peek at the next token. */
13516 token = cp_lexer_peek_token (parser->lexer);
13517 /* What we do depends on what token we have. */
13518 switch (token->type)
13520 /* In valid code, a default argument must be
13521 immediately followed by a `,' `)', or `...'. */
13523 case CPP_CLOSE_PAREN:
13525 /* If we run into a non-nested `;', `}', or `]',
13526 then the code is invalid -- but the default
13527 argument is certainly over. */
13528 case CPP_SEMICOLON:
13529 case CPP_CLOSE_BRACE:
13530 case CPP_CLOSE_SQUARE:
13533 /* Update DEPTH, if necessary. */
13534 else if (token->type == CPP_CLOSE_PAREN
13535 || token->type == CPP_CLOSE_BRACE
13536 || token->type == CPP_CLOSE_SQUARE)
13540 case CPP_OPEN_PAREN:
13541 case CPP_OPEN_SQUARE:
13542 case CPP_OPEN_BRACE:
13547 if (cxx_dialect == cxx98)
13549 /* Fall through for C++0x, which treats the `>>'
13550 operator like two `>' tokens in certain
13554 /* If we see a non-nested `>', and `>' is not an
13555 operator, then it marks the end of the default
13557 if (!depth && !greater_than_is_operator_p)
13561 /* If we run out of tokens, issue an error message. */
13563 case CPP_PRAGMA_EOL:
13564 error ("file ends in default argument");
13570 /* In these cases, we should look for template-ids.
13571 For example, if the default argument is
13572 `X<int, double>()', we need to do name lookup to
13573 figure out whether or not `X' is a template; if
13574 so, the `,' does not end the default argument.
13576 That is not yet done. */
13583 /* If we've reached the end, stop. */
13587 /* Add the token to the token block. */
13588 token = cp_lexer_consume_token (parser->lexer);
13591 /* Create a DEFAULT_ARG to represent the unparsed default
13593 default_argument = make_node (DEFAULT_ARG);
13594 DEFARG_TOKENS (default_argument)
13595 = cp_token_cache_new (first_token, token);
13596 DEFARG_INSTANTIATIONS (default_argument) = NULL;
13598 /* Outside of a class definition, we can just parse the
13599 assignment-expression. */
13602 bool saved_local_variables_forbidden_p;
13604 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
13606 saved_greater_than_is_operator_p
13607 = parser->greater_than_is_operator_p;
13608 parser->greater_than_is_operator_p = greater_than_is_operator_p;
13609 /* Local variable names (and the `this' keyword) may not
13610 appear in a default argument. */
13611 saved_local_variables_forbidden_p
13612 = parser->local_variables_forbidden_p;
13613 parser->local_variables_forbidden_p = true;
13614 /* The default argument expression may cause implicitly
13615 defined member functions to be synthesized, which will
13616 result in garbage collection. We must treat this
13617 situation as if we were within the body of function so as
13618 to avoid collecting live data on the stack. */
13620 /* Parse the assignment-expression. */
13621 if (template_parm_p)
13622 push_deferring_access_checks (dk_no_deferred);
13624 = cp_parser_assignment_expression (parser, /*cast_p=*/false);
13625 if (template_parm_p)
13626 pop_deferring_access_checks ();
13627 /* Restore saved state. */
13629 parser->greater_than_is_operator_p
13630 = saved_greater_than_is_operator_p;
13631 parser->local_variables_forbidden_p
13632 = saved_local_variables_forbidden_p;
13634 if (!parser->default_arg_ok_p)
13636 if (!flag_pedantic_errors)
13637 warning (0, "deprecated use of default argument for parameter of non-function");
13640 error ("default arguments are only permitted for function parameters");
13641 default_argument = NULL_TREE;
13646 default_argument = NULL_TREE;
13648 return make_parameter_declarator (&decl_specifiers,
13653 /* Parse a function-body.
13656 compound_statement */
13659 cp_parser_function_body (cp_parser *parser)
13661 cp_parser_compound_statement (parser, NULL, false);
13664 /* Parse a ctor-initializer-opt followed by a function-body. Return
13665 true if a ctor-initializer was present. */
13668 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
13671 bool ctor_initializer_p;
13673 /* Begin the function body. */
13674 body = begin_function_body ();
13675 /* Parse the optional ctor-initializer. */
13676 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
13677 /* Parse the function-body. */
13678 cp_parser_function_body (parser);
13679 /* Finish the function body. */
13680 finish_function_body (body);
13682 return ctor_initializer_p;
13685 /* Parse an initializer.
13688 = initializer-clause
13689 ( expression-list )
13691 Returns an expression representing the initializer. If no
13692 initializer is present, NULL_TREE is returned.
13694 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
13695 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
13696 set to FALSE if there is no initializer present. If there is an
13697 initializer, and it is not a constant-expression, *NON_CONSTANT_P
13698 is set to true; otherwise it is set to false. */
13701 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
13702 bool* non_constant_p)
13707 /* Peek at the next token. */
13708 token = cp_lexer_peek_token (parser->lexer);
13710 /* Let our caller know whether or not this initializer was
13712 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
13713 /* Assume that the initializer is constant. */
13714 *non_constant_p = false;
13716 if (token->type == CPP_EQ)
13718 /* Consume the `='. */
13719 cp_lexer_consume_token (parser->lexer);
13720 /* Parse the initializer-clause. */
13721 init = cp_parser_initializer_clause (parser, non_constant_p);
13723 else if (token->type == CPP_OPEN_PAREN)
13724 init = cp_parser_parenthesized_expression_list (parser, false,
13726 /*allow_expansion_p=*/true,
13730 /* Anything else is an error. */
13731 cp_parser_error (parser, "expected initializer");
13732 init = error_mark_node;
13738 /* Parse an initializer-clause.
13740 initializer-clause:
13741 assignment-expression
13742 { initializer-list , [opt] }
13745 Returns an expression representing the initializer.
13747 If the `assignment-expression' production is used the value
13748 returned is simply a representation for the expression.
13750 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
13751 the elements of the initializer-list (or NULL, if the last
13752 production is used). The TREE_TYPE for the CONSTRUCTOR will be
13753 NULL_TREE. There is no way to detect whether or not the optional
13754 trailing `,' was provided. NON_CONSTANT_P is as for
13755 cp_parser_initializer. */
13758 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
13762 /* Assume the expression is constant. */
13763 *non_constant_p = false;
13765 /* If it is not a `{', then we are looking at an
13766 assignment-expression. */
13767 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13770 = cp_parser_constant_expression (parser,
13771 /*allow_non_constant_p=*/true,
13773 if (!*non_constant_p)
13774 initializer = fold_non_dependent_expr (initializer);
13778 /* Consume the `{' token. */
13779 cp_lexer_consume_token (parser->lexer);
13780 /* Create a CONSTRUCTOR to represent the braced-initializer. */
13781 initializer = make_node (CONSTRUCTOR);
13782 /* If it's not a `}', then there is a non-trivial initializer. */
13783 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13785 /* Parse the initializer list. */
13786 CONSTRUCTOR_ELTS (initializer)
13787 = cp_parser_initializer_list (parser, non_constant_p);
13788 /* A trailing `,' token is allowed. */
13789 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13790 cp_lexer_consume_token (parser->lexer);
13792 /* Now, there should be a trailing `}'. */
13793 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13796 return initializer;
13799 /* Parse an initializer-list.
13802 initializer-clause ... [opt]
13803 initializer-list , initializer-clause ... [opt]
13808 identifier : initializer-clause
13809 initializer-list, identifier : initializer-clause
13811 Returns a VEC of constructor_elt. The VALUE of each elt is an expression
13812 for the initializer. If the INDEX of the elt is non-NULL, it is the
13813 IDENTIFIER_NODE naming the field to initialize. NON_CONSTANT_P is
13814 as for cp_parser_initializer. */
13816 static VEC(constructor_elt,gc) *
13817 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
13819 VEC(constructor_elt,gc) *v = NULL;
13821 /* Assume all of the expressions are constant. */
13822 *non_constant_p = false;
13824 /* Parse the rest of the list. */
13830 bool clause_non_constant_p;
13832 /* If the next token is an identifier and the following one is a
13833 colon, we are looking at the GNU designated-initializer
13835 if (cp_parser_allow_gnu_extensions_p (parser)
13836 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
13837 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
13839 /* Warn the user that they are using an extension. */
13841 pedwarn ("ISO C++ does not allow designated initializers");
13842 /* Consume the identifier. */
13843 identifier = cp_lexer_consume_token (parser->lexer)->u.value;
13844 /* Consume the `:'. */
13845 cp_lexer_consume_token (parser->lexer);
13848 identifier = NULL_TREE;
13850 /* Parse the initializer. */
13851 initializer = cp_parser_initializer_clause (parser,
13852 &clause_non_constant_p);
13853 /* If any clause is non-constant, so is the entire initializer. */
13854 if (clause_non_constant_p)
13855 *non_constant_p = true;
13857 /* If we have an ellipsis, this is an initializer pack
13859 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13861 /* Consume the `...'. */
13862 cp_lexer_consume_token (parser->lexer);
13864 /* Turn the initializer into an initializer expansion. */
13865 initializer = make_pack_expansion (initializer);
13868 /* Add it to the vector. */
13869 CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
13871 /* If the next token is not a comma, we have reached the end of
13873 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13876 /* Peek at the next token. */
13877 token = cp_lexer_peek_nth_token (parser->lexer, 2);
13878 /* If the next token is a `}', then we're still done. An
13879 initializer-clause can have a trailing `,' after the
13880 initializer-list and before the closing `}'. */
13881 if (token->type == CPP_CLOSE_BRACE)
13884 /* Consume the `,' token. */
13885 cp_lexer_consume_token (parser->lexer);
13891 /* Classes [gram.class] */
13893 /* Parse a class-name.
13899 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
13900 to indicate that names looked up in dependent types should be
13901 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
13902 keyword has been used to indicate that the name that appears next
13903 is a template. TAG_TYPE indicates the explicit tag given before
13904 the type name, if any. If CHECK_DEPENDENCY_P is FALSE, names are
13905 looked up in dependent scopes. If CLASS_HEAD_P is TRUE, this class
13906 is the class being defined in a class-head.
13908 Returns the TYPE_DECL representing the class. */
13911 cp_parser_class_name (cp_parser *parser,
13912 bool typename_keyword_p,
13913 bool template_keyword_p,
13914 enum tag_types tag_type,
13915 bool check_dependency_p,
13917 bool is_declaration)
13924 /* All class-names start with an identifier. */
13925 token = cp_lexer_peek_token (parser->lexer);
13926 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
13928 cp_parser_error (parser, "expected class-name");
13929 return error_mark_node;
13932 /* PARSER->SCOPE can be cleared when parsing the template-arguments
13933 to a template-id, so we save it here. */
13934 scope = parser->scope;
13935 if (scope == error_mark_node)
13936 return error_mark_node;
13938 /* Any name names a type if we're following the `typename' keyword
13939 in a qualified name where the enclosing scope is type-dependent. */
13940 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
13941 && dependent_type_p (scope));
13942 /* Handle the common case (an identifier, but not a template-id)
13944 if (token->type == CPP_NAME
13945 && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
13947 cp_token *identifier_token;
13951 /* Look for the identifier. */
13952 identifier_token = cp_lexer_peek_token (parser->lexer);
13953 ambiguous_p = identifier_token->ambiguous_p;
13954 identifier = cp_parser_identifier (parser);
13955 /* If the next token isn't an identifier, we are certainly not
13956 looking at a class-name. */
13957 if (identifier == error_mark_node)
13958 decl = error_mark_node;
13959 /* If we know this is a type-name, there's no need to look it
13961 else if (typename_p)
13965 tree ambiguous_decls;
13966 /* If we already know that this lookup is ambiguous, then
13967 we've already issued an error message; there's no reason
13971 cp_parser_simulate_error (parser);
13972 return error_mark_node;
13974 /* If the next token is a `::', then the name must be a type
13977 [basic.lookup.qual]
13979 During the lookup for a name preceding the :: scope
13980 resolution operator, object, function, and enumerator
13981 names are ignored. */
13982 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13983 tag_type = typename_type;
13984 /* Look up the name. */
13985 decl = cp_parser_lookup_name (parser, identifier,
13987 /*is_template=*/false,
13988 /*is_namespace=*/false,
13989 check_dependency_p,
13991 if (ambiguous_decls)
13993 error ("reference to %qD is ambiguous", identifier);
13994 print_candidates (ambiguous_decls);
13995 if (cp_parser_parsing_tentatively (parser))
13997 identifier_token->ambiguous_p = true;
13998 cp_parser_simulate_error (parser);
14000 return error_mark_node;
14006 /* Try a template-id. */
14007 decl = cp_parser_template_id (parser, template_keyword_p,
14008 check_dependency_p,
14010 if (decl == error_mark_node)
14011 return error_mark_node;
14014 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
14016 /* If this is a typename, create a TYPENAME_TYPE. */
14017 if (typename_p && decl != error_mark_node)
14019 decl = make_typename_type (scope, decl, typename_type,
14020 /*complain=*/tf_error);
14021 if (decl != error_mark_node)
14022 decl = TYPE_NAME (decl);
14025 /* Check to see that it is really the name of a class. */
14026 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14027 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
14028 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14029 /* Situations like this:
14031 template <typename T> struct A {
14032 typename T::template X<int>::I i;
14035 are problematic. Is `T::template X<int>' a class-name? The
14036 standard does not seem to be definitive, but there is no other
14037 valid interpretation of the following `::'. Therefore, those
14038 names are considered class-names. */
14040 decl = make_typename_type (scope, decl, tag_type, tf_error);
14041 if (decl != error_mark_node)
14042 decl = TYPE_NAME (decl);
14044 else if (TREE_CODE (decl) != TYPE_DECL
14045 || TREE_TYPE (decl) == error_mark_node
14046 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
14047 decl = error_mark_node;
14049 if (decl == error_mark_node)
14050 cp_parser_error (parser, "expected class-name");
14055 /* Parse a class-specifier.
14058 class-head { member-specification [opt] }
14060 Returns the TREE_TYPE representing the class. */
14063 cp_parser_class_specifier (cp_parser* parser)
14067 tree attributes = NULL_TREE;
14068 int has_trailing_semicolon;
14069 bool nested_name_specifier_p;
14070 unsigned saved_num_template_parameter_lists;
14071 bool saved_in_function_body;
14072 tree old_scope = NULL_TREE;
14073 tree scope = NULL_TREE;
14076 push_deferring_access_checks (dk_no_deferred);
14078 /* Parse the class-head. */
14079 type = cp_parser_class_head (parser,
14080 &nested_name_specifier_p,
14083 /* If the class-head was a semantic disaster, skip the entire body
14087 cp_parser_skip_to_end_of_block_or_statement (parser);
14088 pop_deferring_access_checks ();
14089 return error_mark_node;
14092 /* Look for the `{'. */
14093 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
14095 pop_deferring_access_checks ();
14096 return error_mark_node;
14099 /* Process the base classes. If they're invalid, skip the
14100 entire class body. */
14101 if (!xref_basetypes (type, bases))
14103 /* Consuming the closing brace yields better error messages
14105 if (cp_parser_skip_to_closing_brace (parser))
14106 cp_lexer_consume_token (parser->lexer);
14107 pop_deferring_access_checks ();
14108 return error_mark_node;
14111 /* Issue an error message if type-definitions are forbidden here. */
14112 cp_parser_check_type_definition (parser);
14113 /* Remember that we are defining one more class. */
14114 ++parser->num_classes_being_defined;
14115 /* Inside the class, surrounding template-parameter-lists do not
14117 saved_num_template_parameter_lists
14118 = parser->num_template_parameter_lists;
14119 parser->num_template_parameter_lists = 0;
14120 /* We are not in a function body. */
14121 saved_in_function_body = parser->in_function_body;
14122 parser->in_function_body = false;
14124 /* Start the class. */
14125 if (nested_name_specifier_p)
14127 scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
14128 old_scope = push_inner_scope (scope);
14130 type = begin_class_definition (type, attributes);
14132 if (type == error_mark_node)
14133 /* If the type is erroneous, skip the entire body of the class. */
14134 cp_parser_skip_to_closing_brace (parser);
14136 /* Parse the member-specification. */
14137 cp_parser_member_specification_opt (parser);
14139 /* Look for the trailing `}'. */
14140 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
14141 /* We get better error messages by noticing a common problem: a
14142 missing trailing `;'. */
14143 token = cp_lexer_peek_token (parser->lexer);
14144 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
14145 /* Look for trailing attributes to apply to this class. */
14146 if (cp_parser_allow_gnu_extensions_p (parser))
14147 attributes = cp_parser_attributes_opt (parser);
14148 if (type != error_mark_node)
14149 type = finish_struct (type, attributes);
14150 if (nested_name_specifier_p)
14151 pop_inner_scope (old_scope, scope);
14152 /* If this class is not itself within the scope of another class,
14153 then we need to parse the bodies of all of the queued function
14154 definitions. Note that the queued functions defined in a class
14155 are not always processed immediately following the
14156 class-specifier for that class. Consider:
14159 struct B { void f() { sizeof (A); } };
14162 If `f' were processed before the processing of `A' were
14163 completed, there would be no way to compute the size of `A'.
14164 Note that the nesting we are interested in here is lexical --
14165 not the semantic nesting given by TYPE_CONTEXT. In particular,
14168 struct A { struct B; };
14169 struct A::B { void f() { } };
14171 there is no need to delay the parsing of `A::B::f'. */
14172 if (--parser->num_classes_being_defined == 0)
14176 tree class_type = NULL_TREE;
14177 tree pushed_scope = NULL_TREE;
14179 /* In a first pass, parse default arguments to the functions.
14180 Then, in a second pass, parse the bodies of the functions.
14181 This two-phased approach handles cases like:
14189 for (TREE_PURPOSE (parser->unparsed_functions_queues)
14190 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
14191 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
14192 TREE_PURPOSE (parser->unparsed_functions_queues)
14193 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
14195 fn = TREE_VALUE (queue_entry);
14196 /* If there are default arguments that have not yet been processed,
14197 take care of them now. */
14198 if (class_type != TREE_PURPOSE (queue_entry))
14201 pop_scope (pushed_scope);
14202 class_type = TREE_PURPOSE (queue_entry);
14203 pushed_scope = push_scope (class_type);
14205 /* Make sure that any template parameters are in scope. */
14206 maybe_begin_member_template_processing (fn);
14207 /* Parse the default argument expressions. */
14208 cp_parser_late_parsing_default_args (parser, fn);
14209 /* Remove any template parameters from the symbol table. */
14210 maybe_end_member_template_processing ();
14213 pop_scope (pushed_scope);
14214 /* Now parse the body of the functions. */
14215 for (TREE_VALUE (parser->unparsed_functions_queues)
14216 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
14217 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
14218 TREE_VALUE (parser->unparsed_functions_queues)
14219 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
14221 /* Figure out which function we need to process. */
14222 fn = TREE_VALUE (queue_entry);
14223 /* Parse the function. */
14224 cp_parser_late_parsing_for_member (parser, fn);
14228 /* Put back any saved access checks. */
14229 pop_deferring_access_checks ();
14231 /* Restore saved state. */
14232 parser->in_function_body = saved_in_function_body;
14233 parser->num_template_parameter_lists
14234 = saved_num_template_parameter_lists;
14239 /* Parse a class-head.
14242 class-key identifier [opt] base-clause [opt]
14243 class-key nested-name-specifier identifier base-clause [opt]
14244 class-key nested-name-specifier [opt] template-id
14248 class-key attributes identifier [opt] base-clause [opt]
14249 class-key attributes nested-name-specifier identifier base-clause [opt]
14250 class-key attributes nested-name-specifier [opt] template-id
14253 Upon return BASES is initialized to the list of base classes (or
14254 NULL, if there are none) in the same form returned by
14255 cp_parser_base_clause.
14257 Returns the TYPE of the indicated class. Sets
14258 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
14259 involving a nested-name-specifier was used, and FALSE otherwise.
14261 Returns error_mark_node if this is not a class-head.
14263 Returns NULL_TREE if the class-head is syntactically valid, but
14264 semantically invalid in a way that means we should skip the entire
14265 body of the class. */
14268 cp_parser_class_head (cp_parser* parser,
14269 bool* nested_name_specifier_p,
14270 tree *attributes_p,
14273 tree nested_name_specifier;
14274 enum tag_types class_key;
14275 tree id = NULL_TREE;
14276 tree type = NULL_TREE;
14278 bool template_id_p = false;
14279 bool qualified_p = false;
14280 bool invalid_nested_name_p = false;
14281 bool invalid_explicit_specialization_p = false;
14282 tree pushed_scope = NULL_TREE;
14283 unsigned num_templates;
14285 /* Assume no nested-name-specifier will be present. */
14286 *nested_name_specifier_p = false;
14287 /* Assume no template parameter lists will be used in defining the
14291 *bases = NULL_TREE;
14293 /* Look for the class-key. */
14294 class_key = cp_parser_class_key (parser);
14295 if (class_key == none_type)
14296 return error_mark_node;
14298 /* Parse the attributes. */
14299 attributes = cp_parser_attributes_opt (parser);
14301 /* If the next token is `::', that is invalid -- but sometimes
14302 people do try to write:
14306 Handle this gracefully by accepting the extra qualifier, and then
14307 issuing an error about it later if this really is a
14308 class-head. If it turns out just to be an elaborated type
14309 specifier, remain silent. */
14310 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
14311 qualified_p = true;
14313 push_deferring_access_checks (dk_no_check);
14315 /* Determine the name of the class. Begin by looking for an
14316 optional nested-name-specifier. */
14317 nested_name_specifier
14318 = cp_parser_nested_name_specifier_opt (parser,
14319 /*typename_keyword_p=*/false,
14320 /*check_dependency_p=*/false,
14322 /*is_declaration=*/false);
14323 /* If there was a nested-name-specifier, then there *must* be an
14325 if (nested_name_specifier)
14327 /* Although the grammar says `identifier', it really means
14328 `class-name' or `template-name'. You are only allowed to
14329 define a class that has already been declared with this
14332 The proposed resolution for Core Issue 180 says that wherever
14333 you see `class T::X' you should treat `X' as a type-name.
14335 It is OK to define an inaccessible class; for example:
14337 class A { class B; };
14340 We do not know if we will see a class-name, or a
14341 template-name. We look for a class-name first, in case the
14342 class-name is a template-id; if we looked for the
14343 template-name first we would stop after the template-name. */
14344 cp_parser_parse_tentatively (parser);
14345 type = cp_parser_class_name (parser,
14346 /*typename_keyword_p=*/false,
14347 /*template_keyword_p=*/false,
14349 /*check_dependency_p=*/false,
14350 /*class_head_p=*/true,
14351 /*is_declaration=*/false);
14352 /* If that didn't work, ignore the nested-name-specifier. */
14353 if (!cp_parser_parse_definitely (parser))
14355 invalid_nested_name_p = true;
14356 id = cp_parser_identifier (parser);
14357 if (id == error_mark_node)
14360 /* If we could not find a corresponding TYPE, treat this
14361 declaration like an unqualified declaration. */
14362 if (type == error_mark_node)
14363 nested_name_specifier = NULL_TREE;
14364 /* Otherwise, count the number of templates used in TYPE and its
14365 containing scopes. */
14370 for (scope = TREE_TYPE (type);
14371 scope && TREE_CODE (scope) != NAMESPACE_DECL;
14372 scope = (TYPE_P (scope)
14373 ? TYPE_CONTEXT (scope)
14374 : DECL_CONTEXT (scope)))
14376 && CLASS_TYPE_P (scope)
14377 && CLASSTYPE_TEMPLATE_INFO (scope)
14378 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
14379 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
14383 /* Otherwise, the identifier is optional. */
14386 /* We don't know whether what comes next is a template-id,
14387 an identifier, or nothing at all. */
14388 cp_parser_parse_tentatively (parser);
14389 /* Check for a template-id. */
14390 id = cp_parser_template_id (parser,
14391 /*template_keyword_p=*/false,
14392 /*check_dependency_p=*/true,
14393 /*is_declaration=*/true);
14394 /* If that didn't work, it could still be an identifier. */
14395 if (!cp_parser_parse_definitely (parser))
14397 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14398 id = cp_parser_identifier (parser);
14404 template_id_p = true;
14409 pop_deferring_access_checks ();
14412 cp_parser_check_for_invalid_template_id (parser, id);
14414 /* If it's not a `:' or a `{' then we can't really be looking at a
14415 class-head, since a class-head only appears as part of a
14416 class-specifier. We have to detect this situation before calling
14417 xref_tag, since that has irreversible side-effects. */
14418 if (!cp_parser_next_token_starts_class_definition_p (parser))
14420 cp_parser_error (parser, "expected %<{%> or %<:%>");
14421 return error_mark_node;
14424 /* At this point, we're going ahead with the class-specifier, even
14425 if some other problem occurs. */
14426 cp_parser_commit_to_tentative_parse (parser);
14427 /* Issue the error about the overly-qualified name now. */
14429 cp_parser_error (parser,
14430 "global qualification of class name is invalid");
14431 else if (invalid_nested_name_p)
14432 cp_parser_error (parser,
14433 "qualified name does not name a class");
14434 else if (nested_name_specifier)
14438 /* Reject typedef-names in class heads. */
14439 if (!DECL_IMPLICIT_TYPEDEF_P (type))
14441 error ("invalid class name in declaration of %qD", type);
14446 /* Figure out in what scope the declaration is being placed. */
14447 scope = current_scope ();
14448 /* If that scope does not contain the scope in which the
14449 class was originally declared, the program is invalid. */
14450 if (scope && !is_ancestor (scope, nested_name_specifier))
14452 if (at_namespace_scope_p ())
14453 error ("declaration of %qD in namespace %qD which does not "
14454 "enclose %qD", type, scope, nested_name_specifier);
14456 error ("declaration of %qD in %qD which does not enclose %qD",
14457 type, scope, nested_name_specifier);
14463 A declarator-id shall not be qualified exception of the
14464 definition of a ... nested class outside of its class
14465 ... [or] a the definition or explicit instantiation of a
14466 class member of a namespace outside of its namespace. */
14467 if (scope == nested_name_specifier)
14469 pedwarn ("extra qualification ignored");
14470 nested_name_specifier = NULL_TREE;
14474 /* An explicit-specialization must be preceded by "template <>". If
14475 it is not, try to recover gracefully. */
14476 if (at_namespace_scope_p ()
14477 && parser->num_template_parameter_lists == 0
14480 error ("an explicit specialization must be preceded by %<template <>%>");
14481 invalid_explicit_specialization_p = true;
14482 /* Take the same action that would have been taken by
14483 cp_parser_explicit_specialization. */
14484 ++parser->num_template_parameter_lists;
14485 begin_specialization ();
14487 /* There must be no "return" statements between this point and the
14488 end of this function; set "type "to the correct return value and
14489 use "goto done;" to return. */
14490 /* Make sure that the right number of template parameters were
14492 if (!cp_parser_check_template_parameters (parser, num_templates))
14494 /* If something went wrong, there is no point in even trying to
14495 process the class-definition. */
14500 /* Look up the type. */
14503 type = TREE_TYPE (id);
14504 type = maybe_process_partial_specialization (type);
14505 if (nested_name_specifier)
14506 pushed_scope = push_scope (nested_name_specifier);
14508 else if (nested_name_specifier)
14514 template <typename T> struct S { struct T };
14515 template <typename T> struct S<T>::T { };
14517 we will get a TYPENAME_TYPE when processing the definition of
14518 `S::T'. We need to resolve it to the actual type before we
14519 try to define it. */
14520 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
14522 class_type = resolve_typename_type (TREE_TYPE (type),
14523 /*only_current_p=*/false);
14524 if (TREE_CODE (class_type) != TYPENAME_TYPE)
14525 type = TYPE_NAME (class_type);
14528 cp_parser_error (parser, "could not resolve typename type");
14529 type = error_mark_node;
14533 maybe_process_partial_specialization (TREE_TYPE (type));
14534 class_type = current_class_type;
14535 /* Enter the scope indicated by the nested-name-specifier. */
14536 pushed_scope = push_scope (nested_name_specifier);
14537 /* Get the canonical version of this type. */
14538 type = TYPE_MAIN_DECL (TREE_TYPE (type));
14539 if (PROCESSING_REAL_TEMPLATE_DECL_P ()
14540 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
14542 type = push_template_decl (type);
14543 if (type == error_mark_node)
14550 type = TREE_TYPE (type);
14551 *nested_name_specifier_p = true;
14553 else /* The name is not a nested name. */
14555 /* If the class was unnamed, create a dummy name. */
14557 id = make_anon_name ();
14558 type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
14559 parser->num_template_parameter_lists);
14562 /* Indicate whether this class was declared as a `class' or as a
14564 if (TREE_CODE (type) == RECORD_TYPE)
14565 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
14566 cp_parser_check_class_key (class_key, type);
14568 /* If this type was already complete, and we see another definition,
14569 that's an error. */
14570 if (type != error_mark_node && COMPLETE_TYPE_P (type))
14572 error ("redefinition of %q#T", type);
14573 error ("previous definition of %q+#T", type);
14577 else if (type == error_mark_node)
14580 /* We will have entered the scope containing the class; the names of
14581 base classes should be looked up in that context. For example:
14583 struct A { struct B {}; struct C; };
14584 struct A::C : B {};
14588 /* Get the list of base-classes, if there is one. */
14589 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14590 *bases = cp_parser_base_clause (parser);
14593 /* Leave the scope given by the nested-name-specifier. We will
14594 enter the class scope itself while processing the members. */
14596 pop_scope (pushed_scope);
14598 if (invalid_explicit_specialization_p)
14600 end_specialization ();
14601 --parser->num_template_parameter_lists;
14603 *attributes_p = attributes;
14607 /* Parse a class-key.
14614 Returns the kind of class-key specified, or none_type to indicate
14617 static enum tag_types
14618 cp_parser_class_key (cp_parser* parser)
14621 enum tag_types tag_type;
14623 /* Look for the class-key. */
14624 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
14628 /* Check to see if the TOKEN is a class-key. */
14629 tag_type = cp_parser_token_is_class_key (token);
14631 cp_parser_error (parser, "expected class-key");
14635 /* Parse an (optional) member-specification.
14637 member-specification:
14638 member-declaration member-specification [opt]
14639 access-specifier : member-specification [opt] */
14642 cp_parser_member_specification_opt (cp_parser* parser)
14649 /* Peek at the next token. */
14650 token = cp_lexer_peek_token (parser->lexer);
14651 /* If it's a `}', or EOF then we've seen all the members. */
14652 if (token->type == CPP_CLOSE_BRACE
14653 || token->type == CPP_EOF
14654 || token->type == CPP_PRAGMA_EOL)
14657 /* See if this token is a keyword. */
14658 keyword = token->keyword;
14662 case RID_PROTECTED:
14664 /* Consume the access-specifier. */
14665 cp_lexer_consume_token (parser->lexer);
14666 /* Remember which access-specifier is active. */
14667 current_access_specifier = token->u.value;
14668 /* Look for the `:'. */
14669 cp_parser_require (parser, CPP_COLON, "`:'");
14673 /* Accept #pragmas at class scope. */
14674 if (token->type == CPP_PRAGMA)
14676 cp_parser_pragma (parser, pragma_external);
14680 /* Otherwise, the next construction must be a
14681 member-declaration. */
14682 cp_parser_member_declaration (parser);
14687 /* Parse a member-declaration.
14689 member-declaration:
14690 decl-specifier-seq [opt] member-declarator-list [opt] ;
14691 function-definition ; [opt]
14692 :: [opt] nested-name-specifier template [opt] unqualified-id ;
14694 template-declaration
14696 member-declarator-list:
14698 member-declarator-list , member-declarator
14701 declarator pure-specifier [opt]
14702 declarator constant-initializer [opt]
14703 identifier [opt] : constant-expression
14707 member-declaration:
14708 __extension__ member-declaration
14711 declarator attributes [opt] pure-specifier [opt]
14712 declarator attributes [opt] constant-initializer [opt]
14713 identifier [opt] attributes [opt] : constant-expression
14717 member-declaration:
14718 static_assert-declaration */
14721 cp_parser_member_declaration (cp_parser* parser)
14723 cp_decl_specifier_seq decl_specifiers;
14724 tree prefix_attributes;
14726 int declares_class_or_enum;
14729 int saved_pedantic;
14731 /* Check for the `__extension__' keyword. */
14732 if (cp_parser_extension_opt (parser, &saved_pedantic))
14735 cp_parser_member_declaration (parser);
14736 /* Restore the old value of the PEDANTIC flag. */
14737 pedantic = saved_pedantic;
14742 /* Check for a template-declaration. */
14743 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14745 /* An explicit specialization here is an error condition, and we
14746 expect the specialization handler to detect and report this. */
14747 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
14748 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
14749 cp_parser_explicit_specialization (parser);
14751 cp_parser_template_declaration (parser, /*member_p=*/true);
14756 /* Check for a using-declaration. */
14757 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
14759 /* Parse the using-declaration. */
14760 cp_parser_using_declaration (parser,
14761 /*access_declaration_p=*/false);
14765 /* Check for @defs. */
14766 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
14769 tree ivar_chains = cp_parser_objc_defs_expression (parser);
14770 ivar = ivar_chains;
14774 ivar = TREE_CHAIN (member);
14775 TREE_CHAIN (member) = NULL_TREE;
14776 finish_member_declaration (member);
14781 /* If the next token is `static_assert' we have a static assertion. */
14782 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
14784 cp_parser_static_assert (parser, /*member_p=*/true);
14788 if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
14791 /* Parse the decl-specifier-seq. */
14792 cp_parser_decl_specifier_seq (parser,
14793 CP_PARSER_FLAGS_OPTIONAL,
14795 &declares_class_or_enum);
14796 prefix_attributes = decl_specifiers.attributes;
14797 decl_specifiers.attributes = NULL_TREE;
14798 /* Check for an invalid type-name. */
14799 if (!decl_specifiers.type
14800 && cp_parser_parse_and_diagnose_invalid_type_name (parser))
14802 /* If there is no declarator, then the decl-specifier-seq should
14804 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14806 /* If there was no decl-specifier-seq, and the next token is a
14807 `;', then we have something like:
14813 Each member-declaration shall declare at least one member
14814 name of the class. */
14815 if (!decl_specifiers.any_specifiers_p)
14817 cp_token *token = cp_lexer_peek_token (parser->lexer);
14818 if (pedantic && !token->in_system_header)
14819 pedwarn ("%Hextra %<;%>", &token->location);
14825 /* See if this declaration is a friend. */
14826 friend_p = cp_parser_friend_p (&decl_specifiers);
14827 /* If there were decl-specifiers, check to see if there was
14828 a class-declaration. */
14829 type = check_tag_decl (&decl_specifiers);
14830 /* Nested classes have already been added to the class, but
14831 a `friend' needs to be explicitly registered. */
14834 /* If the `friend' keyword was present, the friend must
14835 be introduced with a class-key. */
14836 if (!declares_class_or_enum)
14837 error ("a class-key must be used when declaring a friend");
14840 template <typename T> struct A {
14841 friend struct A<T>::B;
14844 A<T>::B will be represented by a TYPENAME_TYPE, and
14845 therefore not recognized by check_tag_decl. */
14847 && decl_specifiers.type
14848 && TYPE_P (decl_specifiers.type))
14849 type = decl_specifiers.type;
14850 if (!type || !TYPE_P (type))
14851 error ("friend declaration does not name a class or "
14854 make_friend_class (current_class_type, type,
14855 /*complain=*/true);
14857 /* If there is no TYPE, an error message will already have
14859 else if (!type || type == error_mark_node)
14861 /* An anonymous aggregate has to be handled specially; such
14862 a declaration really declares a data member (with a
14863 particular type), as opposed to a nested class. */
14864 else if (ANON_AGGR_TYPE_P (type))
14866 /* Remove constructors and such from TYPE, now that we
14867 know it is an anonymous aggregate. */
14868 fixup_anonymous_aggr (type);
14869 /* And make the corresponding data member. */
14870 decl = build_decl (FIELD_DECL, NULL_TREE, type);
14871 /* Add it to the class. */
14872 finish_member_declaration (decl);
14875 cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
14880 /* See if these declarations will be friends. */
14881 friend_p = cp_parser_friend_p (&decl_specifiers);
14883 /* Keep going until we hit the `;' at the end of the
14885 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14887 tree attributes = NULL_TREE;
14888 tree first_attribute;
14890 /* Peek at the next token. */
14891 token = cp_lexer_peek_token (parser->lexer);
14893 /* Check for a bitfield declaration. */
14894 if (token->type == CPP_COLON
14895 || (token->type == CPP_NAME
14896 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
14902 /* Get the name of the bitfield. Note that we cannot just
14903 check TOKEN here because it may have been invalidated by
14904 the call to cp_lexer_peek_nth_token above. */
14905 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
14906 identifier = cp_parser_identifier (parser);
14908 identifier = NULL_TREE;
14910 /* Consume the `:' token. */
14911 cp_lexer_consume_token (parser->lexer);
14912 /* Get the width of the bitfield. */
14914 = cp_parser_constant_expression (parser,
14915 /*allow_non_constant=*/false,
14918 /* Look for attributes that apply to the bitfield. */
14919 attributes = cp_parser_attributes_opt (parser);
14920 /* Remember which attributes are prefix attributes and
14922 first_attribute = attributes;
14923 /* Combine the attributes. */
14924 attributes = chainon (prefix_attributes, attributes);
14926 /* Create the bitfield declaration. */
14927 decl = grokbitfield (identifier
14928 ? make_id_declarator (NULL_TREE,
14934 /* Apply the attributes. */
14935 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
14939 cp_declarator *declarator;
14941 tree asm_specification;
14942 int ctor_dtor_or_conv_p;
14944 /* Parse the declarator. */
14946 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14947 &ctor_dtor_or_conv_p,
14948 /*parenthesized_p=*/NULL,
14949 /*member_p=*/true);
14951 /* If something went wrong parsing the declarator, make sure
14952 that we at least consume some tokens. */
14953 if (declarator == cp_error_declarator)
14955 /* Skip to the end of the statement. */
14956 cp_parser_skip_to_end_of_statement (parser);
14957 /* If the next token is not a semicolon, that is
14958 probably because we just skipped over the body of
14959 a function. So, we consume a semicolon if
14960 present, but do not issue an error message if it
14962 if (cp_lexer_next_token_is (parser->lexer,
14964 cp_lexer_consume_token (parser->lexer);
14968 if (declares_class_or_enum & 2)
14969 cp_parser_check_for_definition_in_return_type
14970 (declarator, decl_specifiers.type);
14972 /* Look for an asm-specification. */
14973 asm_specification = cp_parser_asm_specification_opt (parser);
14974 /* Look for attributes that apply to the declaration. */
14975 attributes = cp_parser_attributes_opt (parser);
14976 /* Remember which attributes are prefix attributes and
14978 first_attribute = attributes;
14979 /* Combine the attributes. */
14980 attributes = chainon (prefix_attributes, attributes);
14982 /* If it's an `=', then we have a constant-initializer or a
14983 pure-specifier. It is not correct to parse the
14984 initializer before registering the member declaration
14985 since the member declaration should be in scope while
14986 its initializer is processed. However, the rest of the
14987 front end does not yet provide an interface that allows
14988 us to handle this correctly. */
14989 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14993 A pure-specifier shall be used only in the declaration of
14994 a virtual function.
14996 A member-declarator can contain a constant-initializer
14997 only if it declares a static member of integral or
15000 Therefore, if the DECLARATOR is for a function, we look
15001 for a pure-specifier; otherwise, we look for a
15002 constant-initializer. When we call `grokfield', it will
15003 perform more stringent semantics checks. */
15004 if (function_declarator_p (declarator))
15005 initializer = cp_parser_pure_specifier (parser);
15007 /* Parse the initializer. */
15008 initializer = cp_parser_constant_initializer (parser);
15010 /* Otherwise, there is no initializer. */
15012 initializer = NULL_TREE;
15014 /* See if we are probably looking at a function
15015 definition. We are certainly not looking at a
15016 member-declarator. Calling `grokfield' has
15017 side-effects, so we must not do it unless we are sure
15018 that we are looking at a member-declarator. */
15019 if (cp_parser_token_starts_function_definition_p
15020 (cp_lexer_peek_token (parser->lexer)))
15022 /* The grammar does not allow a pure-specifier to be
15023 used when a member function is defined. (It is
15024 possible that this fact is an oversight in the
15025 standard, since a pure function may be defined
15026 outside of the class-specifier. */
15028 error ("pure-specifier on function-definition");
15029 decl = cp_parser_save_member_function_body (parser,
15033 /* If the member was not a friend, declare it here. */
15035 finish_member_declaration (decl);
15036 /* Peek at the next token. */
15037 token = cp_lexer_peek_token (parser->lexer);
15038 /* If the next token is a semicolon, consume it. */
15039 if (token->type == CPP_SEMICOLON)
15040 cp_lexer_consume_token (parser->lexer);
15044 /* Create the declaration. */
15045 decl = grokfield (declarator, &decl_specifiers,
15046 initializer, /*init_const_expr_p=*/true,
15051 /* Reset PREFIX_ATTRIBUTES. */
15052 while (attributes && TREE_CHAIN (attributes) != first_attribute)
15053 attributes = TREE_CHAIN (attributes);
15055 TREE_CHAIN (attributes) = NULL_TREE;
15057 /* If there is any qualification still in effect, clear it
15058 now; we will be starting fresh with the next declarator. */
15059 parser->scope = NULL_TREE;
15060 parser->qualifying_scope = NULL_TREE;
15061 parser->object_scope = NULL_TREE;
15062 /* If it's a `,', then there are more declarators. */
15063 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15064 cp_lexer_consume_token (parser->lexer);
15065 /* If the next token isn't a `;', then we have a parse error. */
15066 else if (cp_lexer_next_token_is_not (parser->lexer,
15069 cp_parser_error (parser, "expected %<;%>");
15070 /* Skip tokens until we find a `;'. */
15071 cp_parser_skip_to_end_of_statement (parser);
15078 /* Add DECL to the list of members. */
15080 finish_member_declaration (decl);
15082 if (TREE_CODE (decl) == FUNCTION_DECL)
15083 cp_parser_save_default_args (parser, decl);
15088 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15091 /* Parse a pure-specifier.
15096 Returns INTEGER_ZERO_NODE if a pure specifier is found.
15097 Otherwise, ERROR_MARK_NODE is returned. */
15100 cp_parser_pure_specifier (cp_parser* parser)
15104 /* Look for the `=' token. */
15105 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15106 return error_mark_node;
15107 /* Look for the `0' token. */
15108 token = cp_lexer_consume_token (parser->lexer);
15109 /* c_lex_with_flags marks a single digit '0' with PURE_ZERO. */
15110 if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
15112 cp_parser_error (parser,
15113 "invalid pure specifier (only `= 0' is allowed)");
15114 cp_parser_skip_to_end_of_statement (parser);
15115 return error_mark_node;
15117 if (PROCESSING_REAL_TEMPLATE_DECL_P ())
15119 error ("templates may not be %<virtual%>");
15120 return error_mark_node;
15123 return integer_zero_node;
15126 /* Parse a constant-initializer.
15128 constant-initializer:
15129 = constant-expression
15131 Returns a representation of the constant-expression. */
15134 cp_parser_constant_initializer (cp_parser* parser)
15136 /* Look for the `=' token. */
15137 if (!cp_parser_require (parser, CPP_EQ, "`='"))
15138 return error_mark_node;
15140 /* It is invalid to write:
15142 struct S { static const int i = { 7 }; };
15145 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15147 cp_parser_error (parser,
15148 "a brace-enclosed initializer is not allowed here");
15149 /* Consume the opening brace. */
15150 cp_lexer_consume_token (parser->lexer);
15151 /* Skip the initializer. */
15152 cp_parser_skip_to_closing_brace (parser);
15153 /* Look for the trailing `}'. */
15154 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
15156 return error_mark_node;
15159 return cp_parser_constant_expression (parser,
15160 /*allow_non_constant=*/false,
15164 /* Derived classes [gram.class.derived] */
15166 /* Parse a base-clause.
15169 : base-specifier-list
15171 base-specifier-list:
15172 base-specifier ... [opt]
15173 base-specifier-list , base-specifier ... [opt]
15175 Returns a TREE_LIST representing the base-classes, in the order in
15176 which they were declared. The representation of each node is as
15177 described by cp_parser_base_specifier.
15179 In the case that no bases are specified, this function will return
15180 NULL_TREE, not ERROR_MARK_NODE. */
15183 cp_parser_base_clause (cp_parser* parser)
15185 tree bases = NULL_TREE;
15187 /* Look for the `:' that begins the list. */
15188 cp_parser_require (parser, CPP_COLON, "`:'");
15190 /* Scan the base-specifier-list. */
15195 bool pack_expansion_p = false;
15197 /* Look for the base-specifier. */
15198 base = cp_parser_base_specifier (parser);
15199 /* Look for the (optional) ellipsis. */
15200 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15202 /* Consume the `...'. */
15203 cp_lexer_consume_token (parser->lexer);
15205 pack_expansion_p = true;
15208 /* Add BASE to the front of the list. */
15209 if (base != error_mark_node)
15211 if (pack_expansion_p)
15212 /* Make this a pack expansion type. */
15213 TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
15215 check_for_bare_parameter_packs (TREE_VALUE (base));
15217 TREE_CHAIN (base) = bases;
15220 /* Peek at the next token. */
15221 token = cp_lexer_peek_token (parser->lexer);
15222 /* If it's not a comma, then the list is complete. */
15223 if (token->type != CPP_COMMA)
15225 /* Consume the `,'. */
15226 cp_lexer_consume_token (parser->lexer);
15229 /* PARSER->SCOPE may still be non-NULL at this point, if the last
15230 base class had a qualified name. However, the next name that
15231 appears is certainly not qualified. */
15232 parser->scope = NULL_TREE;
15233 parser->qualifying_scope = NULL_TREE;
15234 parser->object_scope = NULL_TREE;
15236 return nreverse (bases);
15239 /* Parse a base-specifier.
15242 :: [opt] nested-name-specifier [opt] class-name
15243 virtual access-specifier [opt] :: [opt] nested-name-specifier
15245 access-specifier virtual [opt] :: [opt] nested-name-specifier
15248 Returns a TREE_LIST. The TREE_PURPOSE will be one of
15249 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
15250 indicate the specifiers provided. The TREE_VALUE will be a TYPE
15251 (or the ERROR_MARK_NODE) indicating the type that was specified. */
15254 cp_parser_base_specifier (cp_parser* parser)
15258 bool virtual_p = false;
15259 bool duplicate_virtual_error_issued_p = false;
15260 bool duplicate_access_error_issued_p = false;
15261 bool class_scope_p, template_p;
15262 tree access = access_default_node;
15265 /* Process the optional `virtual' and `access-specifier'. */
15268 /* Peek at the next token. */
15269 token = cp_lexer_peek_token (parser->lexer);
15270 /* Process `virtual'. */
15271 switch (token->keyword)
15274 /* If `virtual' appears more than once, issue an error. */
15275 if (virtual_p && !duplicate_virtual_error_issued_p)
15277 cp_parser_error (parser,
15278 "%<virtual%> specified more than once in base-specified");
15279 duplicate_virtual_error_issued_p = true;
15284 /* Consume the `virtual' token. */
15285 cp_lexer_consume_token (parser->lexer);
15290 case RID_PROTECTED:
15292 /* If more than one access specifier appears, issue an
15294 if (access != access_default_node
15295 && !duplicate_access_error_issued_p)
15297 cp_parser_error (parser,
15298 "more than one access specifier in base-specified");
15299 duplicate_access_error_issued_p = true;
15302 access = ridpointers[(int) token->keyword];
15304 /* Consume the access-specifier. */
15305 cp_lexer_consume_token (parser->lexer);
15314 /* It is not uncommon to see programs mechanically, erroneously, use
15315 the 'typename' keyword to denote (dependent) qualified types
15316 as base classes. */
15317 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
15319 if (!processing_template_decl)
15320 error ("keyword %<typename%> not allowed outside of templates");
15322 error ("keyword %<typename%> not allowed in this context "
15323 "(the base class is implicitly a type)");
15324 cp_lexer_consume_token (parser->lexer);
15327 /* Look for the optional `::' operator. */
15328 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15329 /* Look for the nested-name-specifier. The simplest way to
15334 The keyword `typename' is not permitted in a base-specifier or
15335 mem-initializer; in these contexts a qualified name that
15336 depends on a template-parameter is implicitly assumed to be a
15339 is to pretend that we have seen the `typename' keyword at this
15341 cp_parser_nested_name_specifier_opt (parser,
15342 /*typename_keyword_p=*/true,
15343 /*check_dependency_p=*/true,
15345 /*is_declaration=*/true);
15346 /* If the base class is given by a qualified name, assume that names
15347 we see are type names or templates, as appropriate. */
15348 class_scope_p = (parser->scope && TYPE_P (parser->scope));
15349 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
15351 /* Finally, look for the class-name. */
15352 type = cp_parser_class_name (parser,
15356 /*check_dependency_p=*/true,
15357 /*class_head_p=*/false,
15358 /*is_declaration=*/true);
15360 if (type == error_mark_node)
15361 return error_mark_node;
15363 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
15366 /* Exception handling [gram.exception] */
15368 /* Parse an (optional) exception-specification.
15370 exception-specification:
15371 throw ( type-id-list [opt] )
15373 Returns a TREE_LIST representing the exception-specification. The
15374 TREE_VALUE of each node is a type. */
15377 cp_parser_exception_specification_opt (cp_parser* parser)
15382 /* Peek at the next token. */
15383 token = cp_lexer_peek_token (parser->lexer);
15384 /* If it's not `throw', then there's no exception-specification. */
15385 if (!cp_parser_is_keyword (token, RID_THROW))
15388 /* Consume the `throw'. */
15389 cp_lexer_consume_token (parser->lexer);
15391 /* Look for the `('. */
15392 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15394 /* Peek at the next token. */
15395 token = cp_lexer_peek_token (parser->lexer);
15396 /* If it's not a `)', then there is a type-id-list. */
15397 if (token->type != CPP_CLOSE_PAREN)
15399 const char *saved_message;
15401 /* Types may not be defined in an exception-specification. */
15402 saved_message = parser->type_definition_forbidden_message;
15403 parser->type_definition_forbidden_message
15404 = "types may not be defined in an exception-specification";
15405 /* Parse the type-id-list. */
15406 type_id_list = cp_parser_type_id_list (parser);
15407 /* Restore the saved message. */
15408 parser->type_definition_forbidden_message = saved_message;
15411 type_id_list = empty_except_spec;
15413 /* Look for the `)'. */
15414 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15416 return type_id_list;
15419 /* Parse an (optional) type-id-list.
15423 type-id-list , type-id ... [opt]
15425 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
15426 in the order that the types were presented. */
15429 cp_parser_type_id_list (cp_parser* parser)
15431 tree types = NULL_TREE;
15438 /* Get the next type-id. */
15439 type = cp_parser_type_id (parser);
15440 /* Parse the optional ellipsis. */
15441 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15443 /* Consume the `...'. */
15444 cp_lexer_consume_token (parser->lexer);
15446 /* Turn the type into a pack expansion expression. */
15447 type = make_pack_expansion (type);
15449 /* Add it to the list. */
15450 types = add_exception_specifier (types, type, /*complain=*/1);
15451 /* Peek at the next token. */
15452 token = cp_lexer_peek_token (parser->lexer);
15453 /* If it is not a `,', we are done. */
15454 if (token->type != CPP_COMMA)
15456 /* Consume the `,'. */
15457 cp_lexer_consume_token (parser->lexer);
15460 return nreverse (types);
15463 /* Parse a try-block.
15466 try compound-statement handler-seq */
15469 cp_parser_try_block (cp_parser* parser)
15473 cp_parser_require_keyword (parser, RID_TRY, "`try'");
15474 try_block = begin_try_block ();
15475 cp_parser_compound_statement (parser, NULL, true);
15476 finish_try_block (try_block);
15477 cp_parser_handler_seq (parser);
15478 finish_handler_sequence (try_block);
15483 /* Parse a function-try-block.
15485 function-try-block:
15486 try ctor-initializer [opt] function-body handler-seq */
15489 cp_parser_function_try_block (cp_parser* parser)
15491 tree compound_stmt;
15493 bool ctor_initializer_p;
15495 /* Look for the `try' keyword. */
15496 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
15498 /* Let the rest of the front end know where we are. */
15499 try_block = begin_function_try_block (&compound_stmt);
15500 /* Parse the function-body. */
15502 = cp_parser_ctor_initializer_opt_and_function_body (parser);
15503 /* We're done with the `try' part. */
15504 finish_function_try_block (try_block);
15505 /* Parse the handlers. */
15506 cp_parser_handler_seq (parser);
15507 /* We're done with the handlers. */
15508 finish_function_handler_sequence (try_block, compound_stmt);
15510 return ctor_initializer_p;
15513 /* Parse a handler-seq.
15516 handler handler-seq [opt] */
15519 cp_parser_handler_seq (cp_parser* parser)
15525 /* Parse the handler. */
15526 cp_parser_handler (parser);
15527 /* Peek at the next token. */
15528 token = cp_lexer_peek_token (parser->lexer);
15529 /* If it's not `catch' then there are no more handlers. */
15530 if (!cp_parser_is_keyword (token, RID_CATCH))
15535 /* Parse a handler.
15538 catch ( exception-declaration ) compound-statement */
15541 cp_parser_handler (cp_parser* parser)
15546 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
15547 handler = begin_handler ();
15548 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15549 declaration = cp_parser_exception_declaration (parser);
15550 finish_handler_parms (declaration, handler);
15551 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15552 cp_parser_compound_statement (parser, NULL, false);
15553 finish_handler (handler);
15556 /* Parse an exception-declaration.
15558 exception-declaration:
15559 type-specifier-seq declarator
15560 type-specifier-seq abstract-declarator
15564 Returns a VAR_DECL for the declaration, or NULL_TREE if the
15565 ellipsis variant is used. */
15568 cp_parser_exception_declaration (cp_parser* parser)
15570 cp_decl_specifier_seq type_specifiers;
15571 cp_declarator *declarator;
15572 const char *saved_message;
15574 /* If it's an ellipsis, it's easy to handle. */
15575 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15577 /* Consume the `...' token. */
15578 cp_lexer_consume_token (parser->lexer);
15582 /* Types may not be defined in exception-declarations. */
15583 saved_message = parser->type_definition_forbidden_message;
15584 parser->type_definition_forbidden_message
15585 = "types may not be defined in exception-declarations";
15587 /* Parse the type-specifier-seq. */
15588 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
15590 /* If it's a `)', then there is no declarator. */
15591 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
15594 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
15595 /*ctor_dtor_or_conv_p=*/NULL,
15596 /*parenthesized_p=*/NULL,
15597 /*member_p=*/false);
15599 /* Restore the saved message. */
15600 parser->type_definition_forbidden_message = saved_message;
15602 if (!type_specifiers.any_specifiers_p)
15603 return error_mark_node;
15605 return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
15608 /* Parse a throw-expression.
15611 throw assignment-expression [opt]
15613 Returns a THROW_EXPR representing the throw-expression. */
15616 cp_parser_throw_expression (cp_parser* parser)
15621 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
15622 token = cp_lexer_peek_token (parser->lexer);
15623 /* Figure out whether or not there is an assignment-expression
15624 following the "throw" keyword. */
15625 if (token->type == CPP_COMMA
15626 || token->type == CPP_SEMICOLON
15627 || token->type == CPP_CLOSE_PAREN
15628 || token->type == CPP_CLOSE_SQUARE
15629 || token->type == CPP_CLOSE_BRACE
15630 || token->type == CPP_COLON)
15631 expression = NULL_TREE;
15633 expression = cp_parser_assignment_expression (parser,
15636 return build_throw (expression);
15639 /* GNU Extensions */
15641 /* Parse an (optional) asm-specification.
15644 asm ( string-literal )
15646 If the asm-specification is present, returns a STRING_CST
15647 corresponding to the string-literal. Otherwise, returns
15651 cp_parser_asm_specification_opt (cp_parser* parser)
15654 tree asm_specification;
15656 /* Peek at the next token. */
15657 token = cp_lexer_peek_token (parser->lexer);
15658 /* If the next token isn't the `asm' keyword, then there's no
15659 asm-specification. */
15660 if (!cp_parser_is_keyword (token, RID_ASM))
15663 /* Consume the `asm' token. */
15664 cp_lexer_consume_token (parser->lexer);
15665 /* Look for the `('. */
15666 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15668 /* Look for the string-literal. */
15669 asm_specification = cp_parser_string_literal (parser, false, false);
15671 /* Look for the `)'. */
15672 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
15674 return asm_specification;
15677 /* Parse an asm-operand-list.
15681 asm-operand-list , asm-operand
15684 string-literal ( expression )
15685 [ string-literal ] string-literal ( expression )
15687 Returns a TREE_LIST representing the operands. The TREE_VALUE of
15688 each node is the expression. The TREE_PURPOSE is itself a
15689 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
15690 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
15691 is a STRING_CST for the string literal before the parenthesis. Returns
15692 ERROR_MARK_NODE if any of the operands are invalid. */
15695 cp_parser_asm_operand_list (cp_parser* parser)
15697 tree asm_operands = NULL_TREE;
15698 bool invalid_operands = false;
15702 tree string_literal;
15706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
15708 /* Consume the `[' token. */
15709 cp_lexer_consume_token (parser->lexer);
15710 /* Read the operand name. */
15711 name = cp_parser_identifier (parser);
15712 if (name != error_mark_node)
15713 name = build_string (IDENTIFIER_LENGTH (name),
15714 IDENTIFIER_POINTER (name));
15715 /* Look for the closing `]'. */
15716 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
15720 /* Look for the string-literal. */
15721 string_literal = cp_parser_string_literal (parser, false, false);
15723 /* Look for the `('. */
15724 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15725 /* Parse the expression. */
15726 expression = cp_parser_expression (parser, /*cast_p=*/false);
15727 /* Look for the `)'. */
15728 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15730 if (name == error_mark_node
15731 || string_literal == error_mark_node
15732 || expression == error_mark_node)
15733 invalid_operands = true;
15735 /* Add this operand to the list. */
15736 asm_operands = tree_cons (build_tree_list (name, string_literal),
15739 /* If the next token is not a `,', there are no more
15741 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15743 /* Consume the `,'. */
15744 cp_lexer_consume_token (parser->lexer);
15747 return invalid_operands ? error_mark_node : nreverse (asm_operands);
15750 /* Parse an asm-clobber-list.
15754 asm-clobber-list , string-literal
15756 Returns a TREE_LIST, indicating the clobbers in the order that they
15757 appeared. The TREE_VALUE of each node is a STRING_CST. */
15760 cp_parser_asm_clobber_list (cp_parser* parser)
15762 tree clobbers = NULL_TREE;
15766 tree string_literal;
15768 /* Look for the string literal. */
15769 string_literal = cp_parser_string_literal (parser, false, false);
15770 /* Add it to the list. */
15771 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
15772 /* If the next token is not a `,', then the list is
15774 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15776 /* Consume the `,' token. */
15777 cp_lexer_consume_token (parser->lexer);
15783 /* Parse an (optional) series of attributes.
15786 attributes attribute
15789 __attribute__ (( attribute-list [opt] ))
15791 The return value is as for cp_parser_attribute_list. */
15794 cp_parser_attributes_opt (cp_parser* parser)
15796 tree attributes = NULL_TREE;
15801 tree attribute_list;
15803 /* Peek at the next token. */
15804 token = cp_lexer_peek_token (parser->lexer);
15805 /* If it's not `__attribute__', then we're done. */
15806 if (token->keyword != RID_ATTRIBUTE)
15809 /* Consume the `__attribute__' keyword. */
15810 cp_lexer_consume_token (parser->lexer);
15811 /* Look for the two `(' tokens. */
15812 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15813 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
15815 /* Peek at the next token. */
15816 token = cp_lexer_peek_token (parser->lexer);
15817 if (token->type != CPP_CLOSE_PAREN)
15818 /* Parse the attribute-list. */
15819 attribute_list = cp_parser_attribute_list (parser);
15821 /* If the next token is a `)', then there is no attribute
15823 attribute_list = NULL;
15825 /* Look for the two `)' tokens. */
15826 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15827 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15829 /* Add these new attributes to the list. */
15830 attributes = chainon (attributes, attribute_list);
15836 /* Parse an attribute-list.
15840 attribute-list , attribute
15844 identifier ( identifier )
15845 identifier ( identifier , expression-list )
15846 identifier ( expression-list )
15848 Returns a TREE_LIST, or NULL_TREE on error. Each node corresponds
15849 to an attribute. The TREE_PURPOSE of each node is the identifier
15850 indicating which attribute is in use. The TREE_VALUE represents
15851 the arguments, if any. */
15854 cp_parser_attribute_list (cp_parser* parser)
15856 tree attribute_list = NULL_TREE;
15857 bool save_translate_strings_p = parser->translate_strings_p;
15859 parser->translate_strings_p = false;
15866 /* Look for the identifier. We also allow keywords here; for
15867 example `__attribute__ ((const))' is legal. */
15868 token = cp_lexer_peek_token (parser->lexer);
15869 if (token->type == CPP_NAME
15870 || token->type == CPP_KEYWORD)
15872 tree arguments = NULL_TREE;
15874 /* Consume the token. */
15875 token = cp_lexer_consume_token (parser->lexer);
15877 /* Save away the identifier that indicates which attribute
15879 identifier = token->u.value;
15880 attribute = build_tree_list (identifier, NULL_TREE);
15882 /* Peek at the next token. */
15883 token = cp_lexer_peek_token (parser->lexer);
15884 /* If it's an `(', then parse the attribute arguments. */
15885 if (token->type == CPP_OPEN_PAREN)
15887 arguments = cp_parser_parenthesized_expression_list
15888 (parser, true, /*cast_p=*/false,
15889 /*allow_expansion_p=*/false,
15890 /*non_constant_p=*/NULL);
15891 /* Save the arguments away. */
15892 TREE_VALUE (attribute) = arguments;
15895 if (arguments != error_mark_node)
15897 /* Add this attribute to the list. */
15898 TREE_CHAIN (attribute) = attribute_list;
15899 attribute_list = attribute;
15902 token = cp_lexer_peek_token (parser->lexer);
15904 /* Now, look for more attributes. If the next token isn't a
15905 `,', we're done. */
15906 if (token->type != CPP_COMMA)
15909 /* Consume the comma and keep going. */
15910 cp_lexer_consume_token (parser->lexer);
15912 parser->translate_strings_p = save_translate_strings_p;
15914 /* We built up the list in reverse order. */
15915 return nreverse (attribute_list);
15918 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
15919 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
15920 current value of the PEDANTIC flag, regardless of whether or not
15921 the `__extension__' keyword is present. The caller is responsible
15922 for restoring the value of the PEDANTIC flag. */
15925 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
15927 /* Save the old value of the PEDANTIC flag. */
15928 *saved_pedantic = pedantic;
15930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
15932 /* Consume the `__extension__' token. */
15933 cp_lexer_consume_token (parser->lexer);
15934 /* We're not being pedantic while the `__extension__' keyword is
15944 /* Parse a label declaration.
15947 __label__ label-declarator-seq ;
15949 label-declarator-seq:
15950 identifier , label-declarator-seq
15954 cp_parser_label_declaration (cp_parser* parser)
15956 /* Look for the `__label__' keyword. */
15957 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
15963 /* Look for an identifier. */
15964 identifier = cp_parser_identifier (parser);
15965 /* If we failed, stop. */
15966 if (identifier == error_mark_node)
15968 /* Declare it as a label. */
15969 finish_label_decl (identifier);
15970 /* If the next token is a `;', stop. */
15971 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15973 /* Look for the `,' separating the label declarations. */
15974 cp_parser_require (parser, CPP_COMMA, "`,'");
15977 /* Look for the final `;'. */
15978 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
15981 /* Support Functions */
15983 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
15984 NAME should have one of the representations used for an
15985 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
15986 is returned. If PARSER->SCOPE is a dependent type, then a
15987 SCOPE_REF is returned.
15989 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
15990 returned; the name was already resolved when the TEMPLATE_ID_EXPR
15991 was formed. Abstractly, such entities should not be passed to this
15992 function, because they do not need to be looked up, but it is
15993 simpler to check for this special case here, rather than at the
15996 In cases not explicitly covered above, this function returns a
15997 DECL, OVERLOAD, or baselink representing the result of the lookup.
15998 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
16001 If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
16002 (e.g., "struct") that was used. In that case bindings that do not
16003 refer to types are ignored.
16005 If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
16008 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
16011 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
16014 If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
16015 TREE_LIST of candidates if name-lookup results in an ambiguity, and
16016 NULL_TREE otherwise. */
16019 cp_parser_lookup_name (cp_parser *parser, tree name,
16020 enum tag_types tag_type,
16023 bool check_dependency,
16024 tree *ambiguous_decls)
16028 tree object_type = parser->context->object_type;
16030 if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16031 flags |= LOOKUP_COMPLAIN;
16033 /* Assume that the lookup will be unambiguous. */
16034 if (ambiguous_decls)
16035 *ambiguous_decls = NULL_TREE;
16037 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
16038 no longer valid. Note that if we are parsing tentatively, and
16039 the parse fails, OBJECT_TYPE will be automatically restored. */
16040 parser->context->object_type = NULL_TREE;
16042 if (name == error_mark_node)
16043 return error_mark_node;
16045 /* A template-id has already been resolved; there is no lookup to
16047 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
16049 if (BASELINK_P (name))
16051 gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
16052 == TEMPLATE_ID_EXPR);
16056 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
16057 it should already have been checked to make sure that the name
16058 used matches the type being destroyed. */
16059 if (TREE_CODE (name) == BIT_NOT_EXPR)
16063 /* Figure out to which type this destructor applies. */
16065 type = parser->scope;
16066 else if (object_type)
16067 type = object_type;
16069 type = current_class_type;
16070 /* If that's not a class type, there is no destructor. */
16071 if (!type || !CLASS_TYPE_P (type))
16072 return error_mark_node;
16073 if (CLASSTYPE_LAZY_DESTRUCTOR (type))
16074 lazily_declare_fn (sfk_destructor, type);
16075 if (!CLASSTYPE_DESTRUCTORS (type))
16076 return error_mark_node;
16077 /* If it was a class type, return the destructor. */
16078 return CLASSTYPE_DESTRUCTORS (type);
16081 /* By this point, the NAME should be an ordinary identifier. If
16082 the id-expression was a qualified name, the qualifying scope is
16083 stored in PARSER->SCOPE at this point. */
16084 gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
16086 /* Perform the lookup. */
16091 if (parser->scope == error_mark_node)
16092 return error_mark_node;
16094 /* If the SCOPE is dependent, the lookup must be deferred until
16095 the template is instantiated -- unless we are explicitly
16096 looking up names in uninstantiated templates. Even then, we
16097 cannot look up the name if the scope is not a class type; it
16098 might, for example, be a template type parameter. */
16099 dependent_p = (TYPE_P (parser->scope)
16100 && !(parser->in_declarator_p
16101 && currently_open_class (parser->scope))
16102 && dependent_type_p (parser->scope));
16103 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
16110 /* The resolution to Core Issue 180 says that `struct
16111 A::B' should be considered a type-name, even if `A'
16113 type = make_typename_type (parser->scope, name, tag_type,
16114 /*complain=*/tf_error);
16115 decl = TYPE_NAME (type);
16117 else if (is_template
16118 && (cp_parser_next_token_ends_template_argument_p (parser)
16119 || cp_lexer_next_token_is (parser->lexer,
16121 decl = make_unbound_class_template (parser->scope,
16123 /*complain=*/tf_error);
16125 decl = build_qualified_name (/*type=*/NULL_TREE,
16126 parser->scope, name,
16131 tree pushed_scope = NULL_TREE;
16133 /* If PARSER->SCOPE is a dependent type, then it must be a
16134 class type, and we must not be checking dependencies;
16135 otherwise, we would have processed this lookup above. So
16136 that PARSER->SCOPE is not considered a dependent base by
16137 lookup_member, we must enter the scope here. */
16139 pushed_scope = push_scope (parser->scope);
16140 /* If the PARSER->SCOPE is a template specialization, it
16141 may be instantiated during name lookup. In that case,
16142 errors may be issued. Even if we rollback the current
16143 tentative parse, those errors are valid. */
16144 decl = lookup_qualified_name (parser->scope, name,
16145 tag_type != none_type,
16146 /*complain=*/true);
16148 pop_scope (pushed_scope);
16150 parser->qualifying_scope = parser->scope;
16151 parser->object_scope = NULL_TREE;
16153 else if (object_type)
16155 tree object_decl = NULL_TREE;
16156 /* Look up the name in the scope of the OBJECT_TYPE, unless the
16157 OBJECT_TYPE is not a class. */
16158 if (CLASS_TYPE_P (object_type))
16159 /* If the OBJECT_TYPE is a template specialization, it may
16160 be instantiated during name lookup. In that case, errors
16161 may be issued. Even if we rollback the current tentative
16162 parse, those errors are valid. */
16163 object_decl = lookup_member (object_type,
16166 tag_type != none_type);
16167 /* Look it up in the enclosing context, too. */
16168 decl = lookup_name_real (name, tag_type != none_type,
16170 /*block_p=*/true, is_namespace, flags);
16171 parser->object_scope = object_type;
16172 parser->qualifying_scope = NULL_TREE;
16174 decl = object_decl;
16178 decl = lookup_name_real (name, tag_type != none_type,
16180 /*block_p=*/true, is_namespace, flags);
16181 parser->qualifying_scope = NULL_TREE;
16182 parser->object_scope = NULL_TREE;
16185 /* If the lookup failed, let our caller know. */
16186 if (!decl || decl == error_mark_node)
16187 return error_mark_node;
16189 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
16190 if (TREE_CODE (decl) == TREE_LIST)
16192 if (ambiguous_decls)
16193 *ambiguous_decls = decl;
16194 /* The error message we have to print is too complicated for
16195 cp_parser_error, so we incorporate its actions directly. */
16196 if (!cp_parser_simulate_error (parser))
16198 error ("reference to %qD is ambiguous", name);
16199 print_candidates (decl);
16201 return error_mark_node;
16204 gcc_assert (DECL_P (decl)
16205 || TREE_CODE (decl) == OVERLOAD
16206 || TREE_CODE (decl) == SCOPE_REF
16207 || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
16208 || BASELINK_P (decl));
16210 /* If we have resolved the name of a member declaration, check to
16211 see if the declaration is accessible. When the name resolves to
16212 set of overloaded functions, accessibility is checked when
16213 overload resolution is done.
16215 During an explicit instantiation, access is not checked at all,
16216 as per [temp.explicit]. */
16218 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
16223 /* Like cp_parser_lookup_name, but for use in the typical case where
16224 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
16225 IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE. */
16228 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
16230 return cp_parser_lookup_name (parser, name,
16232 /*is_template=*/false,
16233 /*is_namespace=*/false,
16234 /*check_dependency=*/true,
16235 /*ambiguous_decls=*/NULL);
16238 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
16239 the current context, return the TYPE_DECL. If TAG_NAME_P is
16240 true, the DECL indicates the class being defined in a class-head,
16241 or declared in an elaborated-type-specifier.
16243 Otherwise, return DECL. */
16246 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
16248 /* If the TEMPLATE_DECL is being declared as part of a class-head,
16249 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
16252 template <typename T> struct B;
16255 template <typename T> struct A::B {};
16257 Similarly, in an elaborated-type-specifier:
16259 namespace N { struct X{}; }
16262 template <typename T> friend struct N::X;
16265 However, if the DECL refers to a class type, and we are in
16266 the scope of the class, then the name lookup automatically
16267 finds the TYPE_DECL created by build_self_reference rather
16268 than a TEMPLATE_DECL. For example, in:
16270 template <class T> struct S {
16274 there is no need to handle such case. */
16276 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
16277 return DECL_TEMPLATE_RESULT (decl);
16282 /* If too many, or too few, template-parameter lists apply to the
16283 declarator, issue an error message. Returns TRUE if all went well,
16284 and FALSE otherwise. */
16287 cp_parser_check_declarator_template_parameters (cp_parser* parser,
16288 cp_declarator *declarator)
16290 unsigned num_templates;
16292 /* We haven't seen any classes that involve template parameters yet. */
16295 switch (declarator->kind)
16298 if (declarator->u.id.qualifying_scope)
16303 scope = declarator->u.id.qualifying_scope;
16304 member = declarator->u.id.unqualified_name;
16306 while (scope && CLASS_TYPE_P (scope))
16308 /* You're supposed to have one `template <...>'
16309 for every template class, but you don't need one
16310 for a full specialization. For example:
16312 template <class T> struct S{};
16313 template <> struct S<int> { void f(); };
16314 void S<int>::f () {}
16316 is correct; there shouldn't be a `template <>' for
16317 the definition of `S<int>::f'. */
16318 if (!CLASSTYPE_TEMPLATE_INFO (scope))
16319 /* If SCOPE does not have template information of any
16320 kind, then it is not a template, nor is it nested
16321 within a template. */
16323 if (explicit_class_specialization_p (scope))
16325 if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
16328 scope = TYPE_CONTEXT (scope);
16331 else if (TREE_CODE (declarator->u.id.unqualified_name)
16332 == TEMPLATE_ID_EXPR)
16333 /* If the DECLARATOR has the form `X<y>' then it uses one
16334 additional level of template parameters. */
16337 return cp_parser_check_template_parameters (parser,
16343 case cdk_reference:
16345 return (cp_parser_check_declarator_template_parameters
16346 (parser, declarator->declarator));
16352 gcc_unreachable ();
16357 /* NUM_TEMPLATES were used in the current declaration. If that is
16358 invalid, return FALSE and issue an error messages. Otherwise,
16362 cp_parser_check_template_parameters (cp_parser* parser,
16363 unsigned num_templates)
16365 /* If there are more template classes than parameter lists, we have
16368 template <class T> void S<T>::R<T>::f (); */
16369 if (parser->num_template_parameter_lists < num_templates)
16371 error ("too few template-parameter-lists");
16374 /* If there are the same number of template classes and parameter
16375 lists, that's OK. */
16376 if (parser->num_template_parameter_lists == num_templates)
16378 /* If there are more, but only one more, then we are referring to a
16379 member template. That's OK too. */
16380 if (parser->num_template_parameter_lists == num_templates + 1)
16382 /* Otherwise, there are too many template parameter lists. We have
16385 template <class T> template <class U> void S::f(); */
16386 error ("too many template-parameter-lists");
16390 /* Parse an optional `::' token indicating that the following name is
16391 from the global namespace. If so, PARSER->SCOPE is set to the
16392 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
16393 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
16394 Returns the new value of PARSER->SCOPE, if the `::' token is
16395 present, and NULL_TREE otherwise. */
16398 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
16402 /* Peek at the next token. */
16403 token = cp_lexer_peek_token (parser->lexer);
16404 /* If we're looking at a `::' token then we're starting from the
16405 global namespace, not our current location. */
16406 if (token->type == CPP_SCOPE)
16408 /* Consume the `::' token. */
16409 cp_lexer_consume_token (parser->lexer);
16410 /* Set the SCOPE so that we know where to start the lookup. */
16411 parser->scope = global_namespace;
16412 parser->qualifying_scope = global_namespace;
16413 parser->object_scope = NULL_TREE;
16415 return parser->scope;
16417 else if (!current_scope_valid_p)
16419 parser->scope = NULL_TREE;
16420 parser->qualifying_scope = NULL_TREE;
16421 parser->object_scope = NULL_TREE;
16427 /* Returns TRUE if the upcoming token sequence is the start of a
16428 constructor declarator. If FRIEND_P is true, the declarator is
16429 preceded by the `friend' specifier. */
16432 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
16434 bool constructor_p;
16435 tree type_decl = NULL_TREE;
16436 bool nested_name_p;
16437 cp_token *next_token;
16439 /* The common case is that this is not a constructor declarator, so
16440 try to avoid doing lots of work if at all possible. It's not
16441 valid declare a constructor at function scope. */
16442 if (parser->in_function_body)
16444 /* And only certain tokens can begin a constructor declarator. */
16445 next_token = cp_lexer_peek_token (parser->lexer);
16446 if (next_token->type != CPP_NAME
16447 && next_token->type != CPP_SCOPE
16448 && next_token->type != CPP_NESTED_NAME_SPECIFIER
16449 && next_token->type != CPP_TEMPLATE_ID)
16452 /* Parse tentatively; we are going to roll back all of the tokens
16454 cp_parser_parse_tentatively (parser);
16455 /* Assume that we are looking at a constructor declarator. */
16456 constructor_p = true;
16458 /* Look for the optional `::' operator. */
16459 cp_parser_global_scope_opt (parser,
16460 /*current_scope_valid_p=*/false);
16461 /* Look for the nested-name-specifier. */
16463 = (cp_parser_nested_name_specifier_opt (parser,
16464 /*typename_keyword_p=*/false,
16465 /*check_dependency_p=*/false,
16467 /*is_declaration=*/false)
16469 /* Outside of a class-specifier, there must be a
16470 nested-name-specifier. */
16471 if (!nested_name_p &&
16472 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
16474 constructor_p = false;
16475 /* If we still think that this might be a constructor-declarator,
16476 look for a class-name. */
16481 template <typename T> struct S { S(); };
16482 template <typename T> S<T>::S ();
16484 we must recognize that the nested `S' names a class.
16487 template <typename T> S<T>::S<T> ();
16489 we must recognize that the nested `S' names a template. */
16490 type_decl = cp_parser_class_name (parser,
16491 /*typename_keyword_p=*/false,
16492 /*template_keyword_p=*/false,
16494 /*check_dependency_p=*/false,
16495 /*class_head_p=*/false,
16496 /*is_declaration=*/false);
16497 /* If there was no class-name, then this is not a constructor. */
16498 constructor_p = !cp_parser_error_occurred (parser);
16501 /* If we're still considering a constructor, we have to see a `(',
16502 to begin the parameter-declaration-clause, followed by either a
16503 `)', an `...', or a decl-specifier. We need to check for a
16504 type-specifier to avoid being fooled into thinking that:
16508 is a constructor. (It is actually a function named `f' that
16509 takes one parameter (of type `int') and returns a value of type
16512 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
16514 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
16515 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
16516 /* A parameter declaration begins with a decl-specifier,
16517 which is either the "attribute" keyword, a storage class
16518 specifier, or (usually) a type-specifier. */
16519 && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
16522 tree pushed_scope = NULL_TREE;
16523 unsigned saved_num_template_parameter_lists;
16525 /* Names appearing in the type-specifier should be looked up
16526 in the scope of the class. */
16527 if (current_class_type)
16531 type = TREE_TYPE (type_decl);
16532 if (TREE_CODE (type) == TYPENAME_TYPE)
16534 type = resolve_typename_type (type,
16535 /*only_current_p=*/false);
16536 if (TREE_CODE (type) == TYPENAME_TYPE)
16538 cp_parser_abort_tentative_parse (parser);
16542 pushed_scope = push_scope (type);
16545 /* Inside the constructor parameter list, surrounding
16546 template-parameter-lists do not apply. */
16547 saved_num_template_parameter_lists
16548 = parser->num_template_parameter_lists;
16549 parser->num_template_parameter_lists = 0;
16551 /* Look for the type-specifier. */
16552 cp_parser_type_specifier (parser,
16553 CP_PARSER_FLAGS_NONE,
16554 /*decl_specs=*/NULL,
16555 /*is_declarator=*/true,
16556 /*declares_class_or_enum=*/NULL,
16557 /*is_cv_qualifier=*/NULL);
16559 parser->num_template_parameter_lists
16560 = saved_num_template_parameter_lists;
16562 /* Leave the scope of the class. */
16564 pop_scope (pushed_scope);
16566 constructor_p = !cp_parser_error_occurred (parser);
16570 constructor_p = false;
16571 /* We did not really want to consume any tokens. */
16572 cp_parser_abort_tentative_parse (parser);
16574 return constructor_p;
16577 /* Parse the definition of the function given by the DECL_SPECIFIERS,
16578 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
16579 they must be performed once we are in the scope of the function.
16581 Returns the function defined. */
16584 cp_parser_function_definition_from_specifiers_and_declarator
16585 (cp_parser* parser,
16586 cp_decl_specifier_seq *decl_specifiers,
16588 const cp_declarator *declarator)
16593 /* Begin the function-definition. */
16594 success_p = start_function (decl_specifiers, declarator, attributes);
16596 /* The things we're about to see are not directly qualified by any
16597 template headers we've seen thus far. */
16598 reset_specialization ();
16600 /* If there were names looked up in the decl-specifier-seq that we
16601 did not check, check them now. We must wait until we are in the
16602 scope of the function to perform the checks, since the function
16603 might be a friend. */
16604 perform_deferred_access_checks ();
16608 /* Skip the entire function. */
16609 cp_parser_skip_to_end_of_block_or_statement (parser);
16610 fn = error_mark_node;
16612 else if (DECL_INITIAL (current_function_decl) != error_mark_node)
16614 /* Seen already, skip it. An error message has already been output. */
16615 cp_parser_skip_to_end_of_block_or_statement (parser);
16616 fn = current_function_decl;
16617 current_function_decl = NULL_TREE;
16618 /* If this is a function from a class, pop the nested class. */
16619 if (current_class_name)
16620 pop_nested_class ();
16623 fn = cp_parser_function_definition_after_declarator (parser,
16624 /*inline_p=*/false);
16629 /* Parse the part of a function-definition that follows the
16630 declarator. INLINE_P is TRUE iff this function is an inline
16631 function defined with a class-specifier.
16633 Returns the function defined. */
16636 cp_parser_function_definition_after_declarator (cp_parser* parser,
16640 bool ctor_initializer_p = false;
16641 bool saved_in_unbraced_linkage_specification_p;
16642 bool saved_in_function_body;
16643 unsigned saved_num_template_parameter_lists;
16645 saved_in_function_body = parser->in_function_body;
16646 parser->in_function_body = true;
16647 /* If the next token is `return', then the code may be trying to
16648 make use of the "named return value" extension that G++ used to
16650 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
16652 /* Consume the `return' keyword. */
16653 cp_lexer_consume_token (parser->lexer);
16654 /* Look for the identifier that indicates what value is to be
16656 cp_parser_identifier (parser);
16657 /* Issue an error message. */
16658 error ("named return values are no longer supported");
16659 /* Skip tokens until we reach the start of the function body. */
16662 cp_token *token = cp_lexer_peek_token (parser->lexer);
16663 if (token->type == CPP_OPEN_BRACE
16664 || token->type == CPP_EOF
16665 || token->type == CPP_PRAGMA_EOL)
16667 cp_lexer_consume_token (parser->lexer);
16670 /* The `extern' in `extern "C" void f () { ... }' does not apply to
16671 anything declared inside `f'. */
16672 saved_in_unbraced_linkage_specification_p
16673 = parser->in_unbraced_linkage_specification_p;
16674 parser->in_unbraced_linkage_specification_p = false;
16675 /* Inside the function, surrounding template-parameter-lists do not
16677 saved_num_template_parameter_lists
16678 = parser->num_template_parameter_lists;
16679 parser->num_template_parameter_lists = 0;
16680 /* If the next token is `try', then we are looking at a
16681 function-try-block. */
16682 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
16683 ctor_initializer_p = cp_parser_function_try_block (parser);
16684 /* A function-try-block includes the function-body, so we only do
16685 this next part if we're not processing a function-try-block. */
16688 = cp_parser_ctor_initializer_opt_and_function_body (parser);
16690 /* Finish the function. */
16691 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
16692 (inline_p ? 2 : 0));
16693 /* Generate code for it, if necessary. */
16694 expand_or_defer_fn (fn);
16695 /* Restore the saved values. */
16696 parser->in_unbraced_linkage_specification_p
16697 = saved_in_unbraced_linkage_specification_p;
16698 parser->num_template_parameter_lists
16699 = saved_num_template_parameter_lists;
16700 parser->in_function_body = saved_in_function_body;
16705 /* Parse a template-declaration, assuming that the `export' (and
16706 `extern') keywords, if present, has already been scanned. MEMBER_P
16707 is as for cp_parser_template_declaration. */
16710 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
16712 tree decl = NULL_TREE;
16713 VEC (deferred_access_check,gc) *checks;
16714 tree parameter_list;
16715 bool friend_p = false;
16716 bool need_lang_pop;
16718 /* Look for the `template' keyword. */
16719 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
16723 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
16725 if (at_class_scope_p () && current_function_decl)
16727 /* 14.5.2.2 [temp.mem]
16729 A local class shall not have member templates. */
16730 error ("invalid declaration of member template in local class");
16731 cp_parser_skip_to_end_of_block_or_statement (parser);
16736 A template ... shall not have C linkage. */
16737 if (current_lang_name == lang_name_c)
16739 error ("template with C linkage");
16740 /* Give it C++ linkage to avoid confusing other parts of the
16742 push_lang_context (lang_name_cplusplus);
16743 need_lang_pop = true;
16746 need_lang_pop = false;
16748 /* We cannot perform access checks on the template parameter
16749 declarations until we know what is being declared, just as we
16750 cannot check the decl-specifier list. */
16751 push_deferring_access_checks (dk_deferred);
16753 /* If the next token is `>', then we have an invalid
16754 specialization. Rather than complain about an invalid template
16755 parameter, issue an error message here. */
16756 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
16758 cp_parser_error (parser, "invalid explicit specialization");
16759 begin_specialization ();
16760 parameter_list = NULL_TREE;
16763 /* Parse the template parameters. */
16764 parameter_list = cp_parser_template_parameter_list (parser);
16766 /* Get the deferred access checks from the parameter list. These
16767 will be checked once we know what is being declared, as for a
16768 member template the checks must be performed in the scope of the
16769 class containing the member. */
16770 checks = get_deferred_access_checks ();
16772 /* Look for the `>'. */
16773 cp_parser_skip_to_end_of_template_parameter_list (parser);
16774 /* We just processed one more parameter list. */
16775 ++parser->num_template_parameter_lists;
16776 /* If the next token is `template', there are more template
16778 if (cp_lexer_next_token_is_keyword (parser->lexer,
16780 cp_parser_template_declaration_after_export (parser, member_p);
16783 /* There are no access checks when parsing a template, as we do not
16784 know if a specialization will be a friend. */
16785 push_deferring_access_checks (dk_no_check);
16786 decl = cp_parser_single_declaration (parser,
16789 /*explicit_specialization_p=*/false,
16791 pop_deferring_access_checks ();
16793 /* If this is a member template declaration, let the front
16795 if (member_p && !friend_p && decl)
16797 if (TREE_CODE (decl) == TYPE_DECL)
16798 cp_parser_check_access_in_redeclaration (decl);
16800 decl = finish_member_template_decl (decl);
16802 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
16803 make_friend_class (current_class_type, TREE_TYPE (decl),
16804 /*complain=*/true);
16806 /* We are done with the current parameter list. */
16807 --parser->num_template_parameter_lists;
16809 pop_deferring_access_checks ();
16812 finish_template_decl (parameter_list);
16814 /* Register member declarations. */
16815 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
16816 finish_member_declaration (decl);
16817 /* For the erroneous case of a template with C linkage, we pushed an
16818 implicit C++ linkage scope; exit that scope now. */
16820 pop_lang_context ();
16821 /* If DECL is a function template, we must return to parse it later.
16822 (Even though there is no definition, there might be default
16823 arguments that need handling.) */
16824 if (member_p && decl
16825 && (TREE_CODE (decl) == FUNCTION_DECL
16826 || DECL_FUNCTION_TEMPLATE_P (decl)))
16827 TREE_VALUE (parser->unparsed_functions_queues)
16828 = tree_cons (NULL_TREE, decl,
16829 TREE_VALUE (parser->unparsed_functions_queues));
16832 /* Perform the deferred access checks from a template-parameter-list.
16833 CHECKS is a TREE_LIST of access checks, as returned by
16834 get_deferred_access_checks. */
16837 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
16839 ++processing_template_parmlist;
16840 perform_access_checks (checks);
16841 --processing_template_parmlist;
16844 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
16845 `function-definition' sequence. MEMBER_P is true, this declaration
16846 appears in a class scope.
16848 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
16849 *FRIEND_P is set to TRUE iff the declaration is a friend. */
16852 cp_parser_single_declaration (cp_parser* parser,
16853 VEC (deferred_access_check,gc)* checks,
16855 bool explicit_specialization_p,
16858 int declares_class_or_enum;
16859 tree decl = NULL_TREE;
16860 cp_decl_specifier_seq decl_specifiers;
16861 bool function_definition_p = false;
16863 /* This function is only used when processing a template
16865 gcc_assert (innermost_scope_kind () == sk_template_parms
16866 || innermost_scope_kind () == sk_template_spec);
16868 /* Defer access checks until we know what is being declared. */
16869 push_deferring_access_checks (dk_deferred);
16871 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
16873 cp_parser_decl_specifier_seq (parser,
16874 CP_PARSER_FLAGS_OPTIONAL,
16876 &declares_class_or_enum);
16878 *friend_p = cp_parser_friend_p (&decl_specifiers);
16880 /* There are no template typedefs. */
16881 if (decl_specifiers.specs[(int) ds_typedef])
16883 error ("template declaration of %qs", "typedef");
16884 decl = error_mark_node;
16887 /* Gather up the access checks that occurred the
16888 decl-specifier-seq. */
16889 stop_deferring_access_checks ();
16891 /* Check for the declaration of a template class. */
16892 if (declares_class_or_enum)
16894 if (cp_parser_declares_only_class_p (parser))
16896 decl = shadow_tag (&decl_specifiers);
16901 friend template <typename T> struct A<T>::B;
16904 A<T>::B will be represented by a TYPENAME_TYPE, and
16905 therefore not recognized by shadow_tag. */
16906 if (friend_p && *friend_p
16908 && decl_specifiers.type
16909 && TYPE_P (decl_specifiers.type))
16910 decl = decl_specifiers.type;
16912 if (decl && decl != error_mark_node)
16913 decl = TYPE_NAME (decl);
16915 decl = error_mark_node;
16917 /* Perform access checks for template parameters. */
16918 cp_parser_perform_template_parameter_access_checks (checks);
16921 /* If it's not a template class, try for a template function. If
16922 the next token is a `;', then this declaration does not declare
16923 anything. But, if there were errors in the decl-specifiers, then
16924 the error might well have come from an attempted class-specifier.
16925 In that case, there's no need to warn about a missing declarator. */
16927 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
16928 || decl_specifiers.type != error_mark_node))
16930 decl = cp_parser_init_declarator (parser,
16933 /*function_definition_allowed_p=*/true,
16935 declares_class_or_enum,
16936 &function_definition_p);
16938 /* 7.1.1-1 [dcl.stc]
16940 A storage-class-specifier shall not be specified in an explicit
16941 specialization... */
16943 && explicit_specialization_p
16944 && decl_specifiers.storage_class != sc_none)
16946 error ("explicit template specialization cannot have a storage class");
16947 decl = error_mark_node;
16951 pop_deferring_access_checks ();
16953 /* Clear any current qualification; whatever comes next is the start
16954 of something new. */
16955 parser->scope = NULL_TREE;
16956 parser->qualifying_scope = NULL_TREE;
16957 parser->object_scope = NULL_TREE;
16958 /* Look for a trailing `;' after the declaration. */
16959 if (!function_definition_p
16960 && (decl == error_mark_node
16961 || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
16962 cp_parser_skip_to_end_of_block_or_statement (parser);
16967 /* Parse a cast-expression that is not the operand of a unary "&". */
16970 cp_parser_simple_cast_expression (cp_parser *parser)
16972 return cp_parser_cast_expression (parser, /*address_p=*/false,
16976 /* Parse a functional cast to TYPE. Returns an expression
16977 representing the cast. */
16980 cp_parser_functional_cast (cp_parser* parser, tree type)
16982 tree expression_list;
16986 = cp_parser_parenthesized_expression_list (parser, false,
16988 /*allow_expansion_p=*/true,
16989 /*non_constant_p=*/NULL);
16991 cast = build_functional_cast (type, expression_list);
16992 /* [expr.const]/1: In an integral constant expression "only type
16993 conversions to integral or enumeration type can be used". */
16994 if (TREE_CODE (type) == TYPE_DECL)
16995 type = TREE_TYPE (type);
16996 if (cast != error_mark_node
16997 && !cast_valid_in_integral_constant_expression_p (type)
16998 && (cp_parser_non_integral_constant_expression
16999 (parser, "a call to a constructor")))
17000 return error_mark_node;
17004 /* Save the tokens that make up the body of a member function defined
17005 in a class-specifier. The DECL_SPECIFIERS and DECLARATOR have
17006 already been parsed. The ATTRIBUTES are any GNU "__attribute__"
17007 specifiers applied to the declaration. Returns the FUNCTION_DECL
17008 for the member function. */
17011 cp_parser_save_member_function_body (cp_parser* parser,
17012 cp_decl_specifier_seq *decl_specifiers,
17013 cp_declarator *declarator,
17020 /* Create the function-declaration. */
17021 fn = start_method (decl_specifiers, declarator, attributes);
17022 /* If something went badly wrong, bail out now. */
17023 if (fn == error_mark_node)
17025 /* If there's a function-body, skip it. */
17026 if (cp_parser_token_starts_function_definition_p
17027 (cp_lexer_peek_token (parser->lexer)))
17028 cp_parser_skip_to_end_of_block_or_statement (parser);
17029 return error_mark_node;
17032 /* Remember it, if there default args to post process. */
17033 cp_parser_save_default_args (parser, fn);
17035 /* Save away the tokens that make up the body of the
17037 first = parser->lexer->next_token;
17038 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17039 /* Handle function try blocks. */
17040 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
17041 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
17042 last = parser->lexer->next_token;
17044 /* Save away the inline definition; we will process it when the
17045 class is complete. */
17046 DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
17047 DECL_PENDING_INLINE_P (fn) = 1;
17049 /* We need to know that this was defined in the class, so that
17050 friend templates are handled correctly. */
17051 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
17053 /* We're done with the inline definition. */
17054 finish_method (fn);
17056 /* Add FN to the queue of functions to be parsed later. */
17057 TREE_VALUE (parser->unparsed_functions_queues)
17058 = tree_cons (NULL_TREE, fn,
17059 TREE_VALUE (parser->unparsed_functions_queues));
17064 /* Parse a template-argument-list, as well as the trailing ">" (but
17065 not the opening ">"). See cp_parser_template_argument_list for the
17069 cp_parser_enclosed_template_argument_list (cp_parser* parser)
17073 tree saved_qualifying_scope;
17074 tree saved_object_scope;
17075 bool saved_greater_than_is_operator_p;
17076 bool saved_skip_evaluation;
17080 When parsing a template-id, the first non-nested `>' is taken as
17081 the end of the template-argument-list rather than a greater-than
17083 saved_greater_than_is_operator_p
17084 = parser->greater_than_is_operator_p;
17085 parser->greater_than_is_operator_p = false;
17086 /* Parsing the argument list may modify SCOPE, so we save it
17088 saved_scope = parser->scope;
17089 saved_qualifying_scope = parser->qualifying_scope;
17090 saved_object_scope = parser->object_scope;
17091 /* We need to evaluate the template arguments, even though this
17092 template-id may be nested within a "sizeof". */
17093 saved_skip_evaluation = skip_evaluation;
17094 skip_evaluation = false;
17095 /* Parse the template-argument-list itself. */
17096 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
17097 || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17098 arguments = NULL_TREE;
17100 arguments = cp_parser_template_argument_list (parser);
17101 /* Look for the `>' that ends the template-argument-list. If we find
17102 a '>>' instead, it's probably just a typo. */
17103 if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
17105 if (cxx_dialect != cxx98)
17107 /* In C++0x, a `>>' in a template argument list or cast
17108 expression is considered to be two separate `>'
17109 tokens. So, change the current token to a `>', but don't
17110 consume it: it will be consumed later when the outer
17111 template argument list (or cast expression) is parsed.
17112 Note that this replacement of `>' for `>>' is necessary
17113 even if we are parsing tentatively: in the tentative
17114 case, after calling
17115 cp_parser_enclosed_template_argument_list we will always
17116 throw away all of the template arguments and the first
17117 closing `>', either because the template argument list
17118 was erroneous or because we are replacing those tokens
17119 with a CPP_TEMPLATE_ID token. The second `>' (which will
17120 not have been thrown away) is needed either to close an
17121 outer template argument list or to complete a new-style
17123 cp_token *token = cp_lexer_peek_token (parser->lexer);
17124 token->type = CPP_GREATER;
17126 else if (!saved_greater_than_is_operator_p)
17128 /* If we're in a nested template argument list, the '>>' has
17129 to be a typo for '> >'. We emit the error message, but we
17130 continue parsing and we push a '>' as next token, so that
17131 the argument list will be parsed correctly. Note that the
17132 global source location is still on the token before the
17133 '>>', so we need to say explicitly where we want it. */
17134 cp_token *token = cp_lexer_peek_token (parser->lexer);
17135 error ("%H%<>>%> should be %<> >%> "
17136 "within a nested template argument list",
17139 token->type = CPP_GREATER;
17143 /* If this is not a nested template argument list, the '>>'
17144 is a typo for '>'. Emit an error message and continue.
17145 Same deal about the token location, but here we can get it
17146 right by consuming the '>>' before issuing the diagnostic. */
17147 cp_lexer_consume_token (parser->lexer);
17148 error ("spurious %<>>%>, use %<>%> to terminate "
17149 "a template argument list");
17153 cp_parser_skip_to_end_of_template_parameter_list (parser);
17154 /* The `>' token might be a greater-than operator again now. */
17155 parser->greater_than_is_operator_p
17156 = saved_greater_than_is_operator_p;
17157 /* Restore the SAVED_SCOPE. */
17158 parser->scope = saved_scope;
17159 parser->qualifying_scope = saved_qualifying_scope;
17160 parser->object_scope = saved_object_scope;
17161 skip_evaluation = saved_skip_evaluation;
17166 /* MEMBER_FUNCTION is a member function, or a friend. If default
17167 arguments, or the body of the function have not yet been parsed,
17171 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
17173 /* If this member is a template, get the underlying
17175 if (DECL_FUNCTION_TEMPLATE_P (member_function))
17176 member_function = DECL_TEMPLATE_RESULT (member_function);
17178 /* There should not be any class definitions in progress at this
17179 point; the bodies of members are only parsed outside of all class
17181 gcc_assert (parser->num_classes_being_defined == 0);
17182 /* While we're parsing the member functions we might encounter more
17183 classes. We want to handle them right away, but we don't want
17184 them getting mixed up with functions that are currently in the
17186 parser->unparsed_functions_queues
17187 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17189 /* Make sure that any template parameters are in scope. */
17190 maybe_begin_member_template_processing (member_function);
17192 /* If the body of the function has not yet been parsed, parse it
17194 if (DECL_PENDING_INLINE_P (member_function))
17196 tree function_scope;
17197 cp_token_cache *tokens;
17199 /* The function is no longer pending; we are processing it. */
17200 tokens = DECL_PENDING_INLINE_INFO (member_function);
17201 DECL_PENDING_INLINE_INFO (member_function) = NULL;
17202 DECL_PENDING_INLINE_P (member_function) = 0;
17204 /* If this is a local class, enter the scope of the containing
17206 function_scope = current_function_decl;
17207 if (function_scope)
17208 push_function_context_to (function_scope);
17211 /* Push the body of the function onto the lexer stack. */
17212 cp_parser_push_lexer_for_tokens (parser, tokens);
17214 /* Let the front end know that we going to be defining this
17216 start_preparsed_function (member_function, NULL_TREE,
17217 SF_PRE_PARSED | SF_INCLASS_INLINE);
17219 /* Don't do access checking if it is a templated function. */
17220 if (processing_template_decl)
17221 push_deferring_access_checks (dk_no_check);
17223 /* Now, parse the body of the function. */
17224 cp_parser_function_definition_after_declarator (parser,
17225 /*inline_p=*/true);
17227 if (processing_template_decl)
17228 pop_deferring_access_checks ();
17230 /* Leave the scope of the containing function. */
17231 if (function_scope)
17232 pop_function_context_from (function_scope);
17233 cp_parser_pop_lexer (parser);
17236 /* Remove any template parameters from the symbol table. */
17237 maybe_end_member_template_processing ();
17239 /* Restore the queue. */
17240 parser->unparsed_functions_queues
17241 = TREE_CHAIN (parser->unparsed_functions_queues);
17244 /* If DECL contains any default args, remember it on the unparsed
17245 functions queue. */
17248 cp_parser_save_default_args (cp_parser* parser, tree decl)
17252 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
17254 probe = TREE_CHAIN (probe))
17255 if (TREE_PURPOSE (probe))
17257 TREE_PURPOSE (parser->unparsed_functions_queues)
17258 = tree_cons (current_class_type, decl,
17259 TREE_PURPOSE (parser->unparsed_functions_queues));
17264 /* FN is a FUNCTION_DECL which may contains a parameter with an
17265 unparsed DEFAULT_ARG. Parse the default args now. This function
17266 assumes that the current scope is the scope in which the default
17267 argument should be processed. */
17270 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
17272 bool saved_local_variables_forbidden_p;
17275 /* While we're parsing the default args, we might (due to the
17276 statement expression extension) encounter more classes. We want
17277 to handle them right away, but we don't want them getting mixed
17278 up with default args that are currently in the queue. */
17279 parser->unparsed_functions_queues
17280 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
17282 /* Local variable names (and the `this' keyword) may not appear
17283 in a default argument. */
17284 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17285 parser->local_variables_forbidden_p = true;
17287 for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
17289 parm = TREE_CHAIN (parm))
17291 cp_token_cache *tokens;
17292 tree default_arg = TREE_PURPOSE (parm);
17294 VEC(tree,gc) *insts;
17301 if (TREE_CODE (default_arg) != DEFAULT_ARG)
17302 /* This can happen for a friend declaration for a function
17303 already declared with default arguments. */
17306 /* Push the saved tokens for the default argument onto the parser's
17308 tokens = DEFARG_TOKENS (default_arg);
17309 cp_parser_push_lexer_for_tokens (parser, tokens);
17311 /* Parse the assignment-expression. */
17312 parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false);
17314 if (!processing_template_decl)
17315 parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
17317 TREE_PURPOSE (parm) = parsed_arg;
17319 /* Update any instantiations we've already created. */
17320 for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
17321 VEC_iterate (tree, insts, ix, copy); ix++)
17322 TREE_PURPOSE (copy) = parsed_arg;
17324 /* If the token stream has not been completely used up, then
17325 there was extra junk after the end of the default
17327 if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
17328 cp_parser_error (parser, "expected %<,%>");
17330 /* Revert to the main lexer. */
17331 cp_parser_pop_lexer (parser);
17334 /* Make sure no default arg is missing. */
17335 check_default_args (fn);
17337 /* Restore the state of local_variables_forbidden_p. */
17338 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17340 /* Restore the queue. */
17341 parser->unparsed_functions_queues
17342 = TREE_CHAIN (parser->unparsed_functions_queues);
17345 /* Parse the operand of `sizeof' (or a similar operator). Returns
17346 either a TYPE or an expression, depending on the form of the
17347 input. The KEYWORD indicates which kind of expression we have
17351 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
17353 static const char *format;
17354 tree expr = NULL_TREE;
17355 const char *saved_message;
17357 bool saved_integral_constant_expression_p;
17358 bool saved_non_integral_constant_expression_p;
17359 bool pack_expansion_p = false;
17361 /* Initialize FORMAT the first time we get here. */
17363 format = "types may not be defined in '%s' expressions";
17365 /* Types cannot be defined in a `sizeof' expression. Save away the
17367 saved_message = parser->type_definition_forbidden_message;
17368 /* And create the new one. */
17369 parser->type_definition_forbidden_message = tmp
17370 = XNEWVEC (char, strlen (format)
17371 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
17373 sprintf (tmp, format, IDENTIFIER_POINTER (ridpointers[keyword]));
17375 /* The restrictions on constant-expressions do not apply inside
17376 sizeof expressions. */
17377 saved_integral_constant_expression_p
17378 = parser->integral_constant_expression_p;
17379 saved_non_integral_constant_expression_p
17380 = parser->non_integral_constant_expression_p;
17381 parser->integral_constant_expression_p = false;
17383 /* If it's a `...', then we are computing the length of a parameter
17385 if (keyword == RID_SIZEOF
17386 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17388 /* Consume the `...'. */
17389 cp_lexer_consume_token (parser->lexer);
17390 maybe_warn_variadic_templates ();
17392 /* Note that this is an expansion. */
17393 pack_expansion_p = true;
17396 /* Do not actually evaluate the expression. */
17398 /* If it's a `(', then we might be looking at the type-id
17400 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
17403 bool saved_in_type_id_in_expr_p;
17405 /* We can't be sure yet whether we're looking at a type-id or an
17407 cp_parser_parse_tentatively (parser);
17408 /* Consume the `('. */
17409 cp_lexer_consume_token (parser->lexer);
17410 /* Parse the type-id. */
17411 saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
17412 parser->in_type_id_in_expr_p = true;
17413 type = cp_parser_type_id (parser);
17414 parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
17415 /* Now, look for the trailing `)'. */
17416 cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17417 /* If all went well, then we're done. */
17418 if (cp_parser_parse_definitely (parser))
17420 cp_decl_specifier_seq decl_specs;
17422 /* Build a trivial decl-specifier-seq. */
17423 clear_decl_specs (&decl_specs);
17424 decl_specs.type = type;
17426 /* Call grokdeclarator to figure out what type this is. */
17427 expr = grokdeclarator (NULL,
17431 /*attrlist=*/NULL);
17435 /* If the type-id production did not work out, then we must be
17436 looking at the unary-expression production. */
17438 expr = cp_parser_unary_expression (parser, /*address_p=*/false,
17441 if (pack_expansion_p)
17442 /* Build a pack expansion. */
17443 expr = make_pack_expansion (expr);
17445 /* Go back to evaluating expressions. */
17448 /* Free the message we created. */
17450 /* And restore the old one. */
17451 parser->type_definition_forbidden_message = saved_message;
17452 parser->integral_constant_expression_p
17453 = saved_integral_constant_expression_p;
17454 parser->non_integral_constant_expression_p
17455 = saved_non_integral_constant_expression_p;
17460 /* If the current declaration has no declarator, return true. */
17463 cp_parser_declares_only_class_p (cp_parser *parser)
17465 /* If the next token is a `;' or a `,' then there is no
17467 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17468 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
17471 /* Update the DECL_SPECS to reflect the storage class indicated by
17475 cp_parser_set_storage_class (cp_parser *parser,
17476 cp_decl_specifier_seq *decl_specs,
17479 cp_storage_class storage_class;
17481 if (parser->in_unbraced_linkage_specification_p)
17483 error ("invalid use of %qD in linkage specification",
17484 ridpointers[keyword]);
17487 else if (decl_specs->storage_class != sc_none)
17489 decl_specs->conflicting_specifiers_p = true;
17493 if ((keyword == RID_EXTERN || keyword == RID_STATIC)
17494 && decl_specs->specs[(int) ds_thread])
17496 error ("%<__thread%> before %qD", ridpointers[keyword]);
17497 decl_specs->specs[(int) ds_thread] = 0;
17503 storage_class = sc_auto;
17506 storage_class = sc_register;
17509 storage_class = sc_static;
17512 storage_class = sc_extern;
17515 storage_class = sc_mutable;
17518 gcc_unreachable ();
17520 decl_specs->storage_class = storage_class;
17522 /* A storage class specifier cannot be applied alongside a typedef
17523 specifier. If there is a typedef specifier present then set
17524 conflicting_specifiers_p which will trigger an error later
17525 on in grokdeclarator. */
17526 if (decl_specs->specs[(int)ds_typedef])
17527 decl_specs->conflicting_specifiers_p = true;
17530 /* Update the DECL_SPECS to reflect the TYPE_SPEC. If USER_DEFINED_P
17531 is true, the type is a user-defined type; otherwise it is a
17532 built-in type specified by a keyword. */
17535 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
17537 bool user_defined_p)
17539 decl_specs->any_specifiers_p = true;
17541 /* If the user tries to redeclare bool or wchar_t (with, for
17542 example, in "typedef int wchar_t;") we remember that this is what
17543 happened. In system headers, we ignore these declarations so
17544 that G++ can work with system headers that are not C++-safe. */
17545 if (decl_specs->specs[(int) ds_typedef]
17547 && (type_spec == boolean_type_node
17548 || type_spec == wchar_type_node)
17549 && (decl_specs->type
17550 || decl_specs->specs[(int) ds_long]
17551 || decl_specs->specs[(int) ds_short]
17552 || decl_specs->specs[(int) ds_unsigned]
17553 || decl_specs->specs[(int) ds_signed]))
17555 decl_specs->redefined_builtin_type = type_spec;
17556 if (!decl_specs->type)
17558 decl_specs->type = type_spec;
17559 decl_specs->user_defined_type_p = false;
17562 else if (decl_specs->type)
17563 decl_specs->multiple_types_p = true;
17566 decl_specs->type = type_spec;
17567 decl_specs->user_defined_type_p = user_defined_p;
17568 decl_specs->redefined_builtin_type = NULL_TREE;
17572 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
17573 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
17576 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
17578 return decl_specifiers->specs[(int) ds_friend] != 0;
17581 /* If the next token is of the indicated TYPE, consume it. Otherwise,
17582 issue an error message indicating that TOKEN_DESC was expected.
17584 Returns the token consumed, if the token had the appropriate type.
17585 Otherwise, returns NULL. */
17588 cp_parser_require (cp_parser* parser,
17589 enum cpp_ttype type,
17590 const char* token_desc)
17592 if (cp_lexer_next_token_is (parser->lexer, type))
17593 return cp_lexer_consume_token (parser->lexer);
17596 /* Output the MESSAGE -- unless we're parsing tentatively. */
17597 if (!cp_parser_simulate_error (parser))
17599 char *message = concat ("expected ", token_desc, NULL);
17600 cp_parser_error (parser, message);
17607 /* An error message is produced if the next token is not '>'.
17608 All further tokens are skipped until the desired token is
17609 found or '{', '}', ';' or an unbalanced ')' or ']'. */
17612 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
17614 /* Current level of '< ... >'. */
17615 unsigned level = 0;
17616 /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'. */
17617 unsigned nesting_depth = 0;
17619 /* Are we ready, yet? If not, issue error message. */
17620 if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
17623 /* Skip tokens until the desired token is found. */
17626 /* Peek at the next token. */
17627 switch (cp_lexer_peek_token (parser->lexer)->type)
17630 if (!nesting_depth)
17635 if (cxx_dialect == cxx98)
17636 /* C++0x views the `>>' operator as two `>' tokens, but
17639 else if (!nesting_depth && level-- == 0)
17641 /* We've hit a `>>' where the first `>' closes the
17642 template argument list, and the second `>' is
17643 spurious. Just consume the `>>' and stop; we've
17644 already produced at least one error. */
17645 cp_lexer_consume_token (parser->lexer);
17648 /* Fall through for C++0x, so we handle the second `>' in
17652 if (!nesting_depth && level-- == 0)
17654 /* We've reached the token we want, consume it and stop. */
17655 cp_lexer_consume_token (parser->lexer);
17660 case CPP_OPEN_PAREN:
17661 case CPP_OPEN_SQUARE:
17665 case CPP_CLOSE_PAREN:
17666 case CPP_CLOSE_SQUARE:
17667 if (nesting_depth-- == 0)
17672 case CPP_PRAGMA_EOL:
17673 case CPP_SEMICOLON:
17674 case CPP_OPEN_BRACE:
17675 case CPP_CLOSE_BRACE:
17676 /* The '>' was probably forgotten, don't look further. */
17683 /* Consume this token. */
17684 cp_lexer_consume_token (parser->lexer);
17688 /* If the next token is the indicated keyword, consume it. Otherwise,
17689 issue an error message indicating that TOKEN_DESC was expected.
17691 Returns the token consumed, if the token had the appropriate type.
17692 Otherwise, returns NULL. */
17695 cp_parser_require_keyword (cp_parser* parser,
17697 const char* token_desc)
17699 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
17701 if (token && token->keyword != keyword)
17703 dyn_string_t error_msg;
17705 /* Format the error message. */
17706 error_msg = dyn_string_new (0);
17707 dyn_string_append_cstr (error_msg, "expected ");
17708 dyn_string_append_cstr (error_msg, token_desc);
17709 cp_parser_error (parser, error_msg->s);
17710 dyn_string_delete (error_msg);
17717 /* Returns TRUE iff TOKEN is a token that can begin the body of a
17718 function-definition. */
17721 cp_parser_token_starts_function_definition_p (cp_token* token)
17723 return (/* An ordinary function-body begins with an `{'. */
17724 token->type == CPP_OPEN_BRACE
17725 /* A ctor-initializer begins with a `:'. */
17726 || token->type == CPP_COLON
17727 /* A function-try-block begins with `try'. */
17728 || token->keyword == RID_TRY
17729 /* The named return value extension begins with `return'. */
17730 || token->keyword == RID_RETURN);
17733 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
17737 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
17741 token = cp_lexer_peek_token (parser->lexer);
17742 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
17745 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
17746 C++0x) ending a template-argument. */
17749 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
17753 token = cp_lexer_peek_token (parser->lexer);
17754 return (token->type == CPP_COMMA
17755 || token->type == CPP_GREATER
17756 || token->type == CPP_ELLIPSIS
17757 || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
17760 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
17761 (n+1)-th is a ":" (which is a possible digraph typo for "< ::"). */
17764 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
17769 token = cp_lexer_peek_nth_token (parser->lexer, n);
17770 if (token->type == CPP_LESS)
17772 /* Check for the sequence `<::' in the original code. It would be lexed as
17773 `[:', where `[' is a digraph, and there is no whitespace before
17775 if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
17778 token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
17779 if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
17785 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
17786 or none_type otherwise. */
17788 static enum tag_types
17789 cp_parser_token_is_class_key (cp_token* token)
17791 switch (token->keyword)
17796 return record_type;
17805 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
17808 cp_parser_check_class_key (enum tag_types class_key, tree type)
17810 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
17811 pedwarn ("%qs tag used in naming %q#T",
17812 class_key == union_type ? "union"
17813 : class_key == record_type ? "struct" : "class",
17817 /* Issue an error message if DECL is redeclared with different
17818 access than its original declaration [class.access.spec/3].
17819 This applies to nested classes and nested class templates.
17823 cp_parser_check_access_in_redeclaration (tree decl)
17825 if (!CLASS_TYPE_P (TREE_TYPE (decl)))
17828 if ((TREE_PRIVATE (decl)
17829 != (current_access_specifier == access_private_node))
17830 || (TREE_PROTECTED (decl)
17831 != (current_access_specifier == access_protected_node)))
17832 error ("%qD redeclared with different access", decl);
17835 /* Look for the `template' keyword, as a syntactic disambiguator.
17836 Return TRUE iff it is present, in which case it will be
17840 cp_parser_optional_template_keyword (cp_parser *parser)
17842 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17844 /* The `template' keyword can only be used within templates;
17845 outside templates the parser can always figure out what is a
17846 template and what is not. */
17847 if (!processing_template_decl)
17849 error ("%<template%> (as a disambiguator) is only allowed "
17850 "within templates");
17851 /* If this part of the token stream is rescanned, the same
17852 error message would be generated. So, we purge the token
17853 from the stream. */
17854 cp_lexer_purge_token (parser->lexer);
17859 /* Consume the `template' keyword. */
17860 cp_lexer_consume_token (parser->lexer);
17868 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
17869 set PARSER->SCOPE, and perform other related actions. */
17872 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
17875 struct tree_check *check_value;
17876 deferred_access_check *chk;
17877 VEC (deferred_access_check,gc) *checks;
17879 /* Get the stored value. */
17880 check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
17881 /* Perform any access checks that were deferred. */
17882 checks = check_value->checks;
17886 VEC_iterate (deferred_access_check, checks, i, chk) ;
17889 perform_or_defer_access_check (chk->binfo,
17894 /* Set the scope from the stored value. */
17895 parser->scope = check_value->value;
17896 parser->qualifying_scope = check_value->qualifying_scope;
17897 parser->object_scope = NULL_TREE;
17900 /* Consume tokens up through a non-nested END token. */
17903 cp_parser_cache_group (cp_parser *parser,
17904 enum cpp_ttype end,
17911 /* Abort a parenthesized expression if we encounter a brace. */
17912 if ((end == CPP_CLOSE_PAREN || depth == 0)
17913 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17915 /* If we've reached the end of the file, stop. */
17916 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF)
17917 || (end != CPP_PRAGMA_EOL
17918 && cp_lexer_next_token_is (parser->lexer, CPP_PRAGMA_EOL)))
17920 /* Consume the next token. */
17921 token = cp_lexer_consume_token (parser->lexer);
17922 /* See if it starts a new group. */
17923 if (token->type == CPP_OPEN_BRACE)
17925 cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
17929 else if (token->type == CPP_OPEN_PAREN)
17930 cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
17931 else if (token->type == CPP_PRAGMA)
17932 cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
17933 else if (token->type == end)
17938 /* Begin parsing tentatively. We always save tokens while parsing
17939 tentatively so that if the tentative parsing fails we can restore the
17943 cp_parser_parse_tentatively (cp_parser* parser)
17945 /* Enter a new parsing context. */
17946 parser->context = cp_parser_context_new (parser->context);
17947 /* Begin saving tokens. */
17948 cp_lexer_save_tokens (parser->lexer);
17949 /* In order to avoid repetitive access control error messages,
17950 access checks are queued up until we are no longer parsing
17952 push_deferring_access_checks (dk_deferred);
17955 /* Commit to the currently active tentative parse. */
17958 cp_parser_commit_to_tentative_parse (cp_parser* parser)
17960 cp_parser_context *context;
17963 /* Mark all of the levels as committed. */
17964 lexer = parser->lexer;
17965 for (context = parser->context; context->next; context = context->next)
17967 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
17969 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
17970 while (!cp_lexer_saving_tokens (lexer))
17971 lexer = lexer->next;
17972 cp_lexer_commit_tokens (lexer);
17976 /* Abort the currently active tentative parse. All consumed tokens
17977 will be rolled back, and no diagnostics will be issued. */
17980 cp_parser_abort_tentative_parse (cp_parser* parser)
17982 cp_parser_simulate_error (parser);
17983 /* Now, pretend that we want to see if the construct was
17984 successfully parsed. */
17985 cp_parser_parse_definitely (parser);
17988 /* Stop parsing tentatively. If a parse error has occurred, restore the
17989 token stream. Otherwise, commit to the tokens we have consumed.
17990 Returns true if no error occurred; false otherwise. */
17993 cp_parser_parse_definitely (cp_parser* parser)
17995 bool error_occurred;
17996 cp_parser_context *context;
17998 /* Remember whether or not an error occurred, since we are about to
17999 destroy that information. */
18000 error_occurred = cp_parser_error_occurred (parser);
18001 /* Remove the topmost context from the stack. */
18002 context = parser->context;
18003 parser->context = context->next;
18004 /* If no parse errors occurred, commit to the tentative parse. */
18005 if (!error_occurred)
18007 /* Commit to the tokens read tentatively, unless that was
18009 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
18010 cp_lexer_commit_tokens (parser->lexer);
18012 pop_to_parent_deferring_access_checks ();
18014 /* Otherwise, if errors occurred, roll back our state so that things
18015 are just as they were before we began the tentative parse. */
18018 cp_lexer_rollback_tokens (parser->lexer);
18019 pop_deferring_access_checks ();
18021 /* Add the context to the front of the free list. */
18022 context->next = cp_parser_context_free_list;
18023 cp_parser_context_free_list = context;
18025 return !error_occurred;
18028 /* Returns true if we are parsing tentatively and are not committed to
18029 this tentative parse. */
18032 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
18034 return (cp_parser_parsing_tentatively (parser)
18035 && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
18038 /* Returns nonzero iff an error has occurred during the most recent
18039 tentative parse. */
18042 cp_parser_error_occurred (cp_parser* parser)
18044 return (cp_parser_parsing_tentatively (parser)
18045 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
18048 /* Returns nonzero if GNU extensions are allowed. */
18051 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
18053 return parser->allow_gnu_extensions_p;
18056 /* Objective-C++ Productions */
18059 /* Parse an Objective-C expression, which feeds into a primary-expression
18063 objc-message-expression
18064 objc-string-literal
18065 objc-encode-expression
18066 objc-protocol-expression
18067 objc-selector-expression
18069 Returns a tree representation of the expression. */
18072 cp_parser_objc_expression (cp_parser* parser)
18074 /* Try to figure out what kind of declaration is present. */
18075 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
18079 case CPP_OPEN_SQUARE:
18080 return cp_parser_objc_message_expression (parser);
18082 case CPP_OBJC_STRING:
18083 kwd = cp_lexer_consume_token (parser->lexer);
18084 return objc_build_string_object (kwd->u.value);
18087 switch (kwd->keyword)
18089 case RID_AT_ENCODE:
18090 return cp_parser_objc_encode_expression (parser);
18092 case RID_AT_PROTOCOL:
18093 return cp_parser_objc_protocol_expression (parser);
18095 case RID_AT_SELECTOR:
18096 return cp_parser_objc_selector_expression (parser);
18102 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
18103 cp_parser_skip_to_end_of_block_or_statement (parser);
18106 return error_mark_node;
18109 /* Parse an Objective-C message expression.
18111 objc-message-expression:
18112 [ objc-message-receiver objc-message-args ]
18114 Returns a representation of an Objective-C message. */
18117 cp_parser_objc_message_expression (cp_parser* parser)
18119 tree receiver, messageargs;
18121 cp_lexer_consume_token (parser->lexer); /* Eat '['. */
18122 receiver = cp_parser_objc_message_receiver (parser);
18123 messageargs = cp_parser_objc_message_args (parser);
18124 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
18126 return objc_build_message_expr (build_tree_list (receiver, messageargs));
18129 /* Parse an objc-message-receiver.
18131 objc-message-receiver:
18133 simple-type-specifier
18135 Returns a representation of the type or expression. */
18138 cp_parser_objc_message_receiver (cp_parser* parser)
18142 /* An Objective-C message receiver may be either (1) a type
18143 or (2) an expression. */
18144 cp_parser_parse_tentatively (parser);
18145 rcv = cp_parser_expression (parser, false);
18147 if (cp_parser_parse_definitely (parser))
18150 rcv = cp_parser_simple_type_specifier (parser,
18151 /*decl_specs=*/NULL,
18152 CP_PARSER_FLAGS_NONE);
18154 return objc_get_class_reference (rcv);
18157 /* Parse the arguments and selectors comprising an Objective-C message.
18162 objc-selector-args , objc-comma-args
18164 objc-selector-args:
18165 objc-selector [opt] : assignment-expression
18166 objc-selector-args objc-selector [opt] : assignment-expression
18169 assignment-expression
18170 objc-comma-args , assignment-expression
18172 Returns a TREE_LIST, with TREE_PURPOSE containing a list of
18173 selector arguments and TREE_VALUE containing a list of comma
18177 cp_parser_objc_message_args (cp_parser* parser)
18179 tree sel_args = NULL_TREE, addl_args = NULL_TREE;
18180 bool maybe_unary_selector_p = true;
18181 cp_token *token = cp_lexer_peek_token (parser->lexer);
18183 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18185 tree selector = NULL_TREE, arg;
18187 if (token->type != CPP_COLON)
18188 selector = cp_parser_objc_selector (parser);
18190 /* Detect if we have a unary selector. */
18191 if (maybe_unary_selector_p
18192 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18193 return build_tree_list (selector, NULL_TREE);
18195 maybe_unary_selector_p = false;
18196 cp_parser_require (parser, CPP_COLON, "`:'");
18197 arg = cp_parser_assignment_expression (parser, false);
18200 = chainon (sel_args,
18201 build_tree_list (selector, arg));
18203 token = cp_lexer_peek_token (parser->lexer);
18206 /* Handle non-selector arguments, if any. */
18207 while (token->type == CPP_COMMA)
18211 cp_lexer_consume_token (parser->lexer);
18212 arg = cp_parser_assignment_expression (parser, false);
18215 = chainon (addl_args,
18216 build_tree_list (NULL_TREE, arg));
18218 token = cp_lexer_peek_token (parser->lexer);
18221 return build_tree_list (sel_args, addl_args);
18224 /* Parse an Objective-C encode expression.
18226 objc-encode-expression:
18227 @encode objc-typename
18229 Returns an encoded representation of the type argument. */
18232 cp_parser_objc_encode_expression (cp_parser* parser)
18236 cp_lexer_consume_token (parser->lexer); /* Eat '@encode'. */
18237 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18238 type = complete_type (cp_parser_type_id (parser));
18239 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18243 error ("%<@encode%> must specify a type as an argument");
18244 return error_mark_node;
18247 return objc_build_encode_expr (type);
18250 /* Parse an Objective-C @defs expression. */
18253 cp_parser_objc_defs_expression (cp_parser *parser)
18257 cp_lexer_consume_token (parser->lexer); /* Eat '@defs'. */
18258 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18259 name = cp_parser_identifier (parser);
18260 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18262 return objc_get_class_ivars (name);
18265 /* Parse an Objective-C protocol expression.
18267 objc-protocol-expression:
18268 @protocol ( identifier )
18270 Returns a representation of the protocol expression. */
18273 cp_parser_objc_protocol_expression (cp_parser* parser)
18277 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18278 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18279 proto = cp_parser_identifier (parser);
18280 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18282 return objc_build_protocol_expr (proto);
18285 /* Parse an Objective-C selector expression.
18287 objc-selector-expression:
18288 @selector ( objc-method-signature )
18290 objc-method-signature:
18296 objc-selector-seq objc-selector :
18298 Returns a representation of the method selector. */
18301 cp_parser_objc_selector_expression (cp_parser* parser)
18303 tree sel_seq = NULL_TREE;
18304 bool maybe_unary_selector_p = true;
18307 cp_lexer_consume_token (parser->lexer); /* Eat '@selector'. */
18308 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
18309 token = cp_lexer_peek_token (parser->lexer);
18311 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
18312 || token->type == CPP_SCOPE)
18314 tree selector = NULL_TREE;
18316 if (token->type != CPP_COLON
18317 || token->type == CPP_SCOPE)
18318 selector = cp_parser_objc_selector (parser);
18320 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
18321 && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
18323 /* Detect if we have a unary selector. */
18324 if (maybe_unary_selector_p)
18326 sel_seq = selector;
18327 goto finish_selector;
18331 cp_parser_error (parser, "expected %<:%>");
18334 maybe_unary_selector_p = false;
18335 token = cp_lexer_consume_token (parser->lexer);
18337 if (token->type == CPP_SCOPE)
18340 = chainon (sel_seq,
18341 build_tree_list (selector, NULL_TREE));
18343 = chainon (sel_seq,
18344 build_tree_list (NULL_TREE, NULL_TREE));
18348 = chainon (sel_seq,
18349 build_tree_list (selector, NULL_TREE));
18351 token = cp_lexer_peek_token (parser->lexer);
18355 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18357 return objc_build_selector_expr (sel_seq);
18360 /* Parse a list of identifiers.
18362 objc-identifier-list:
18364 objc-identifier-list , identifier
18366 Returns a TREE_LIST of identifier nodes. */
18369 cp_parser_objc_identifier_list (cp_parser* parser)
18371 tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
18372 cp_token *sep = cp_lexer_peek_token (parser->lexer);
18374 while (sep->type == CPP_COMMA)
18376 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18377 list = chainon (list,
18378 build_tree_list (NULL_TREE,
18379 cp_parser_identifier (parser)));
18380 sep = cp_lexer_peek_token (parser->lexer);
18386 /* Parse an Objective-C alias declaration.
18388 objc-alias-declaration:
18389 @compatibility_alias identifier identifier ;
18391 This function registers the alias mapping with the Objective-C front end.
18392 It returns nothing. */
18395 cp_parser_objc_alias_declaration (cp_parser* parser)
18399 cp_lexer_consume_token (parser->lexer); /* Eat '@compatibility_alias'. */
18400 alias = cp_parser_identifier (parser);
18401 orig = cp_parser_identifier (parser);
18402 objc_declare_alias (alias, orig);
18403 cp_parser_consume_semicolon_at_end_of_statement (parser);
18406 /* Parse an Objective-C class forward-declaration.
18408 objc-class-declaration:
18409 @class objc-identifier-list ;
18411 The function registers the forward declarations with the Objective-C
18412 front end. It returns nothing. */
18415 cp_parser_objc_class_declaration (cp_parser* parser)
18417 cp_lexer_consume_token (parser->lexer); /* Eat '@class'. */
18418 objc_declare_class (cp_parser_objc_identifier_list (parser));
18419 cp_parser_consume_semicolon_at_end_of_statement (parser);
18422 /* Parse a list of Objective-C protocol references.
18424 objc-protocol-refs-opt:
18425 objc-protocol-refs [opt]
18427 objc-protocol-refs:
18428 < objc-identifier-list >
18430 Returns a TREE_LIST of identifiers, if any. */
18433 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
18435 tree protorefs = NULL_TREE;
18437 if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
18439 cp_lexer_consume_token (parser->lexer); /* Eat '<'. */
18440 protorefs = cp_parser_objc_identifier_list (parser);
18441 cp_parser_require (parser, CPP_GREATER, "`>'");
18447 /* Parse a Objective-C visibility specification. */
18450 cp_parser_objc_visibility_spec (cp_parser* parser)
18452 cp_token *vis = cp_lexer_peek_token (parser->lexer);
18454 switch (vis->keyword)
18456 case RID_AT_PRIVATE:
18457 objc_set_visibility (2);
18459 case RID_AT_PROTECTED:
18460 objc_set_visibility (0);
18462 case RID_AT_PUBLIC:
18463 objc_set_visibility (1);
18469 /* Eat '@private'/'@protected'/'@public'. */
18470 cp_lexer_consume_token (parser->lexer);
18473 /* Parse an Objective-C method type. */
18476 cp_parser_objc_method_type (cp_parser* parser)
18478 objc_set_method_type
18479 (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
18484 /* Parse an Objective-C protocol qualifier. */
18487 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
18489 tree quals = NULL_TREE, node;
18490 cp_token *token = cp_lexer_peek_token (parser->lexer);
18492 node = token->u.value;
18494 while (node && TREE_CODE (node) == IDENTIFIER_NODE
18495 && (node == ridpointers [(int) RID_IN]
18496 || node == ridpointers [(int) RID_OUT]
18497 || node == ridpointers [(int) RID_INOUT]
18498 || node == ridpointers [(int) RID_BYCOPY]
18499 || node == ridpointers [(int) RID_BYREF]
18500 || node == ridpointers [(int) RID_ONEWAY]))
18502 quals = tree_cons (NULL_TREE, node, quals);
18503 cp_lexer_consume_token (parser->lexer);
18504 token = cp_lexer_peek_token (parser->lexer);
18505 node = token->u.value;
18511 /* Parse an Objective-C typename. */
18514 cp_parser_objc_typename (cp_parser* parser)
18516 tree typename = NULL_TREE;
18518 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
18520 tree proto_quals, cp_type = NULL_TREE;
18522 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18523 proto_quals = cp_parser_objc_protocol_qualifiers (parser);
18525 /* An ObjC type name may consist of just protocol qualifiers, in which
18526 case the type shall default to 'id'. */
18527 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
18528 cp_type = cp_parser_type_id (parser);
18530 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18531 typename = build_tree_list (proto_quals, cp_type);
18537 /* Check to see if TYPE refers to an Objective-C selector name. */
18540 cp_parser_objc_selector_p (enum cpp_ttype type)
18542 return (type == CPP_NAME || type == CPP_KEYWORD
18543 || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
18544 || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
18545 || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
18546 || type == CPP_XOR || type == CPP_XOR_EQ);
18549 /* Parse an Objective-C selector. */
18552 cp_parser_objc_selector (cp_parser* parser)
18554 cp_token *token = cp_lexer_consume_token (parser->lexer);
18556 if (!cp_parser_objc_selector_p (token->type))
18558 error ("invalid Objective-C++ selector name");
18559 return error_mark_node;
18562 /* C++ operator names are allowed to appear in ObjC selectors. */
18563 switch (token->type)
18565 case CPP_AND_AND: return get_identifier ("and");
18566 case CPP_AND_EQ: return get_identifier ("and_eq");
18567 case CPP_AND: return get_identifier ("bitand");
18568 case CPP_OR: return get_identifier ("bitor");
18569 case CPP_COMPL: return get_identifier ("compl");
18570 case CPP_NOT: return get_identifier ("not");
18571 case CPP_NOT_EQ: return get_identifier ("not_eq");
18572 case CPP_OR_OR: return get_identifier ("or");
18573 case CPP_OR_EQ: return get_identifier ("or_eq");
18574 case CPP_XOR: return get_identifier ("xor");
18575 case CPP_XOR_EQ: return get_identifier ("xor_eq");
18576 default: return token->u.value;
18580 /* Parse an Objective-C params list. */
18583 cp_parser_objc_method_keyword_params (cp_parser* parser)
18585 tree params = NULL_TREE;
18586 bool maybe_unary_selector_p = true;
18587 cp_token *token = cp_lexer_peek_token (parser->lexer);
18589 while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
18591 tree selector = NULL_TREE, typename, identifier;
18593 if (token->type != CPP_COLON)
18594 selector = cp_parser_objc_selector (parser);
18596 /* Detect if we have a unary selector. */
18597 if (maybe_unary_selector_p
18598 && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
18601 maybe_unary_selector_p = false;
18602 cp_parser_require (parser, CPP_COLON, "`:'");
18603 typename = cp_parser_objc_typename (parser);
18604 identifier = cp_parser_identifier (parser);
18608 objc_build_keyword_decl (selector,
18612 token = cp_lexer_peek_token (parser->lexer);
18618 /* Parse the non-keyword Objective-C params. */
18621 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
18623 tree params = make_node (TREE_LIST);
18624 cp_token *token = cp_lexer_peek_token (parser->lexer);
18625 *ellipsisp = false; /* Initially, assume no ellipsis. */
18627 while (token->type == CPP_COMMA)
18629 cp_parameter_declarator *parmdecl;
18632 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18633 token = cp_lexer_peek_token (parser->lexer);
18635 if (token->type == CPP_ELLIPSIS)
18637 cp_lexer_consume_token (parser->lexer); /* Eat '...'. */
18642 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
18643 parm = grokdeclarator (parmdecl->declarator,
18644 &parmdecl->decl_specifiers,
18645 PARM, /*initialized=*/0,
18646 /*attrlist=*/NULL);
18648 chainon (params, build_tree_list (NULL_TREE, parm));
18649 token = cp_lexer_peek_token (parser->lexer);
18655 /* Parse a linkage specification, a pragma, an extra semicolon or a block. */
18658 cp_parser_objc_interstitial_code (cp_parser* parser)
18660 cp_token *token = cp_lexer_peek_token (parser->lexer);
18662 /* If the next token is `extern' and the following token is a string
18663 literal, then we have a linkage specification. */
18664 if (token->keyword == RID_EXTERN
18665 && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
18666 cp_parser_linkage_specification (parser);
18667 /* Handle #pragma, if any. */
18668 else if (token->type == CPP_PRAGMA)
18669 cp_parser_pragma (parser, pragma_external);
18670 /* Allow stray semicolons. */
18671 else if (token->type == CPP_SEMICOLON)
18672 cp_lexer_consume_token (parser->lexer);
18673 /* Finally, try to parse a block-declaration, or a function-definition. */
18675 cp_parser_block_declaration (parser, /*statement_p=*/false);
18678 /* Parse a method signature. */
18681 cp_parser_objc_method_signature (cp_parser* parser)
18683 tree rettype, kwdparms, optparms;
18684 bool ellipsis = false;
18686 cp_parser_objc_method_type (parser);
18687 rettype = cp_parser_objc_typename (parser);
18688 kwdparms = cp_parser_objc_method_keyword_params (parser);
18689 optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
18691 return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
18694 /* Pars an Objective-C method prototype list. */
18697 cp_parser_objc_method_prototype_list (cp_parser* parser)
18699 cp_token *token = cp_lexer_peek_token (parser->lexer);
18701 while (token->keyword != RID_AT_END)
18703 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18705 objc_add_method_declaration
18706 (cp_parser_objc_method_signature (parser));
18707 cp_parser_consume_semicolon_at_end_of_statement (parser);
18710 /* Allow for interspersed non-ObjC++ code. */
18711 cp_parser_objc_interstitial_code (parser);
18713 token = cp_lexer_peek_token (parser->lexer);
18716 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18717 objc_finish_interface ();
18720 /* Parse an Objective-C method definition list. */
18723 cp_parser_objc_method_definition_list (cp_parser* parser)
18725 cp_token *token = cp_lexer_peek_token (parser->lexer);
18727 while (token->keyword != RID_AT_END)
18731 if (token->type == CPP_PLUS || token->type == CPP_MINUS)
18733 push_deferring_access_checks (dk_deferred);
18734 objc_start_method_definition
18735 (cp_parser_objc_method_signature (parser));
18737 /* For historical reasons, we accept an optional semicolon. */
18738 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18739 cp_lexer_consume_token (parser->lexer);
18741 perform_deferred_access_checks ();
18742 stop_deferring_access_checks ();
18743 meth = cp_parser_function_definition_after_declarator (parser,
18745 pop_deferring_access_checks ();
18746 objc_finish_method_definition (meth);
18749 /* Allow for interspersed non-ObjC++ code. */
18750 cp_parser_objc_interstitial_code (parser);
18752 token = cp_lexer_peek_token (parser->lexer);
18755 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18756 objc_finish_implementation ();
18759 /* Parse Objective-C ivars. */
18762 cp_parser_objc_class_ivars (cp_parser* parser)
18764 cp_token *token = cp_lexer_peek_token (parser->lexer);
18766 if (token->type != CPP_OPEN_BRACE)
18767 return; /* No ivars specified. */
18769 cp_lexer_consume_token (parser->lexer); /* Eat '{'. */
18770 token = cp_lexer_peek_token (parser->lexer);
18772 while (token->type != CPP_CLOSE_BRACE)
18774 cp_decl_specifier_seq declspecs;
18775 int decl_class_or_enum_p;
18776 tree prefix_attributes;
18778 cp_parser_objc_visibility_spec (parser);
18780 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
18783 cp_parser_decl_specifier_seq (parser,
18784 CP_PARSER_FLAGS_OPTIONAL,
18786 &decl_class_or_enum_p);
18787 prefix_attributes = declspecs.attributes;
18788 declspecs.attributes = NULL_TREE;
18790 /* Keep going until we hit the `;' at the end of the
18792 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18794 tree width = NULL_TREE, attributes, first_attribute, decl;
18795 cp_declarator *declarator = NULL;
18796 int ctor_dtor_or_conv_p;
18798 /* Check for a (possibly unnamed) bitfield declaration. */
18799 token = cp_lexer_peek_token (parser->lexer);
18800 if (token->type == CPP_COLON)
18803 if (token->type == CPP_NAME
18804 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
18807 /* Get the name of the bitfield. */
18808 declarator = make_id_declarator (NULL_TREE,
18809 cp_parser_identifier (parser),
18813 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18814 /* Get the width of the bitfield. */
18816 = cp_parser_constant_expression (parser,
18817 /*allow_non_constant=*/false,
18822 /* Parse the declarator. */
18824 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18825 &ctor_dtor_or_conv_p,
18826 /*parenthesized_p=*/NULL,
18827 /*member_p=*/false);
18830 /* Look for attributes that apply to the ivar. */
18831 attributes = cp_parser_attributes_opt (parser);
18832 /* Remember which attributes are prefix attributes and
18834 first_attribute = attributes;
18835 /* Combine the attributes. */
18836 attributes = chainon (prefix_attributes, attributes);
18840 /* Create the bitfield declaration. */
18841 decl = grokbitfield (declarator, &declspecs, width);
18842 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
18845 decl = grokfield (declarator, &declspecs,
18846 NULL_TREE, /*init_const_expr_p=*/false,
18847 NULL_TREE, attributes);
18849 /* Add the instance variable. */
18850 objc_add_instance_variable (decl);
18852 /* Reset PREFIX_ATTRIBUTES. */
18853 while (attributes && TREE_CHAIN (attributes) != first_attribute)
18854 attributes = TREE_CHAIN (attributes);
18856 TREE_CHAIN (attributes) = NULL_TREE;
18858 token = cp_lexer_peek_token (parser->lexer);
18860 if (token->type == CPP_COMMA)
18862 cp_lexer_consume_token (parser->lexer); /* Eat ','. */
18868 cp_parser_consume_semicolon_at_end_of_statement (parser);
18869 token = cp_lexer_peek_token (parser->lexer);
18872 cp_lexer_consume_token (parser->lexer); /* Eat '}'. */
18873 /* For historical reasons, we accept an optional semicolon. */
18874 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18875 cp_lexer_consume_token (parser->lexer);
18878 /* Parse an Objective-C protocol declaration. */
18881 cp_parser_objc_protocol_declaration (cp_parser* parser)
18883 tree proto, protorefs;
18886 cp_lexer_consume_token (parser->lexer); /* Eat '@protocol'. */
18887 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
18889 error ("identifier expected after %<@protocol%>");
18893 /* See if we have a forward declaration or a definition. */
18894 tok = cp_lexer_peek_nth_token (parser->lexer, 2);
18896 /* Try a forward declaration first. */
18897 if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
18899 objc_declare_protocols (cp_parser_objc_identifier_list (parser));
18901 cp_parser_consume_semicolon_at_end_of_statement (parser);
18904 /* Ok, we got a full-fledged definition (or at least should). */
18907 proto = cp_parser_identifier (parser);
18908 protorefs = cp_parser_objc_protocol_refs_opt (parser);
18909 objc_start_protocol (proto, protorefs);
18910 cp_parser_objc_method_prototype_list (parser);
18914 /* Parse an Objective-C superclass or category. */
18917 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
18920 cp_token *next = cp_lexer_peek_token (parser->lexer);
18922 *super = *categ = NULL_TREE;
18923 if (next->type == CPP_COLON)
18925 cp_lexer_consume_token (parser->lexer); /* Eat ':'. */
18926 *super = cp_parser_identifier (parser);
18928 else if (next->type == CPP_OPEN_PAREN)
18930 cp_lexer_consume_token (parser->lexer); /* Eat '('. */
18931 *categ = cp_parser_identifier (parser);
18932 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
18936 /* Parse an Objective-C class interface. */
18939 cp_parser_objc_class_interface (cp_parser* parser)
18941 tree name, super, categ, protos;
18943 cp_lexer_consume_token (parser->lexer); /* Eat '@interface'. */
18944 name = cp_parser_identifier (parser);
18945 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18946 protos = cp_parser_objc_protocol_refs_opt (parser);
18948 /* We have either a class or a category on our hands. */
18950 objc_start_category_interface (name, categ, protos);
18953 objc_start_class_interface (name, super, protos);
18954 /* Handle instance variable declarations, if any. */
18955 cp_parser_objc_class_ivars (parser);
18956 objc_continue_interface ();
18959 cp_parser_objc_method_prototype_list (parser);
18962 /* Parse an Objective-C class implementation. */
18965 cp_parser_objc_class_implementation (cp_parser* parser)
18967 tree name, super, categ;
18969 cp_lexer_consume_token (parser->lexer); /* Eat '@implementation'. */
18970 name = cp_parser_identifier (parser);
18971 cp_parser_objc_superclass_or_category (parser, &super, &categ);
18973 /* We have either a class or a category on our hands. */
18975 objc_start_category_implementation (name, categ);
18978 objc_start_class_implementation (name, super);
18979 /* Handle instance variable declarations, if any. */
18980 cp_parser_objc_class_ivars (parser);
18981 objc_continue_implementation ();
18984 cp_parser_objc_method_definition_list (parser);
18987 /* Consume the @end token and finish off the implementation. */
18990 cp_parser_objc_end_implementation (cp_parser* parser)
18992 cp_lexer_consume_token (parser->lexer); /* Eat '@end'. */
18993 objc_finish_implementation ();
18996 /* Parse an Objective-C declaration. */
18999 cp_parser_objc_declaration (cp_parser* parser)
19001 /* Try to figure out what kind of declaration is present. */
19002 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19004 switch (kwd->keyword)
19007 cp_parser_objc_alias_declaration (parser);
19010 cp_parser_objc_class_declaration (parser);
19012 case RID_AT_PROTOCOL:
19013 cp_parser_objc_protocol_declaration (parser);
19015 case RID_AT_INTERFACE:
19016 cp_parser_objc_class_interface (parser);
19018 case RID_AT_IMPLEMENTATION:
19019 cp_parser_objc_class_implementation (parser);
19022 cp_parser_objc_end_implementation (parser);
19025 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19026 cp_parser_skip_to_end_of_block_or_statement (parser);
19030 /* Parse an Objective-C try-catch-finally statement.
19032 objc-try-catch-finally-stmt:
19033 @try compound-statement objc-catch-clause-seq [opt]
19034 objc-finally-clause [opt]
19036 objc-catch-clause-seq:
19037 objc-catch-clause objc-catch-clause-seq [opt]
19040 @catch ( exception-declaration ) compound-statement
19042 objc-finally-clause
19043 @finally compound-statement
19045 Returns NULL_TREE. */
19048 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
19049 location_t location;
19052 cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
19053 location = cp_lexer_peek_token (parser->lexer)->location;
19054 /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
19055 node, lest it get absorbed into the surrounding block. */
19056 stmt = push_stmt_list ();
19057 cp_parser_compound_statement (parser, NULL, false);
19058 objc_begin_try_stmt (location, pop_stmt_list (stmt));
19060 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
19062 cp_parameter_declarator *parmdecl;
19065 cp_lexer_consume_token (parser->lexer);
19066 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19067 parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
19068 parm = grokdeclarator (parmdecl->declarator,
19069 &parmdecl->decl_specifiers,
19070 PARM, /*initialized=*/0,
19071 /*attrlist=*/NULL);
19072 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19073 objc_begin_catch_clause (parm);
19074 cp_parser_compound_statement (parser, NULL, false);
19075 objc_finish_catch_clause ();
19078 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
19080 cp_lexer_consume_token (parser->lexer);
19081 location = cp_lexer_peek_token (parser->lexer)->location;
19082 /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
19083 node, lest it get absorbed into the surrounding block. */
19084 stmt = push_stmt_list ();
19085 cp_parser_compound_statement (parser, NULL, false);
19086 objc_build_finally_clause (location, pop_stmt_list (stmt));
19089 return objc_finish_try_stmt ();
19092 /* Parse an Objective-C synchronized statement.
19094 objc-synchronized-stmt:
19095 @synchronized ( expression ) compound-statement
19097 Returns NULL_TREE. */
19100 cp_parser_objc_synchronized_statement (cp_parser *parser) {
19101 location_t location;
19104 cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
19106 location = cp_lexer_peek_token (parser->lexer)->location;
19107 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
19108 lock = cp_parser_expression (parser, false);
19109 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
19111 /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
19112 node, lest it get absorbed into the surrounding block. */
19113 stmt = push_stmt_list ();
19114 cp_parser_compound_statement (parser, NULL, false);
19116 return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
19119 /* Parse an Objective-C throw statement.
19122 @throw assignment-expression [opt] ;
19124 Returns a constructed '@throw' statement. */
19127 cp_parser_objc_throw_statement (cp_parser *parser) {
19128 tree expr = NULL_TREE;
19130 cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
19132 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19133 expr = cp_parser_assignment_expression (parser, false);
19135 cp_parser_consume_semicolon_at_end_of_statement (parser);
19137 return objc_build_throw_stmt (expr);
19140 /* Parse an Objective-C statement. */
19143 cp_parser_objc_statement (cp_parser * parser) {
19144 /* Try to figure out what kind of declaration is present. */
19145 cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19147 switch (kwd->keyword)
19150 return cp_parser_objc_try_catch_finally_statement (parser);
19151 case RID_AT_SYNCHRONIZED:
19152 return cp_parser_objc_synchronized_statement (parser);
19154 return cp_parser_objc_throw_statement (parser);
19156 error ("misplaced %<@%D%> Objective-C++ construct", kwd->u.value);
19157 cp_parser_skip_to_end_of_block_or_statement (parser);
19160 return error_mark_node;
19163 /* OpenMP 2.5 parsing routines. */
19165 /* Returns name of the next clause.
19166 If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
19167 the token is not consumed. Otherwise appropriate pragma_omp_clause is
19168 returned and the token is consumed. */
19170 static pragma_omp_clause
19171 cp_parser_omp_clause_name (cp_parser *parser)
19173 pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
19175 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
19176 result = PRAGMA_OMP_CLAUSE_IF;
19177 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
19178 result = PRAGMA_OMP_CLAUSE_DEFAULT;
19179 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
19180 result = PRAGMA_OMP_CLAUSE_PRIVATE;
19181 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19183 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19184 const char *p = IDENTIFIER_POINTER (id);
19189 if (!strcmp ("copyin", p))
19190 result = PRAGMA_OMP_CLAUSE_COPYIN;
19191 else if (!strcmp ("copyprivate", p))
19192 result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
19195 if (!strcmp ("firstprivate", p))
19196 result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
19199 if (!strcmp ("lastprivate", p))
19200 result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
19203 if (!strcmp ("nowait", p))
19204 result = PRAGMA_OMP_CLAUSE_NOWAIT;
19205 else if (!strcmp ("num_threads", p))
19206 result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
19209 if (!strcmp ("ordered", p))
19210 result = PRAGMA_OMP_CLAUSE_ORDERED;
19213 if (!strcmp ("reduction", p))
19214 result = PRAGMA_OMP_CLAUSE_REDUCTION;
19217 if (!strcmp ("schedule", p))
19218 result = PRAGMA_OMP_CLAUSE_SCHEDULE;
19219 else if (!strcmp ("shared", p))
19220 result = PRAGMA_OMP_CLAUSE_SHARED;
19225 if (result != PRAGMA_OMP_CLAUSE_NONE)
19226 cp_lexer_consume_token (parser->lexer);
19231 /* Validate that a clause of the given type does not already exist. */
19234 check_no_duplicate_clause (tree clauses, enum tree_code code, const char *name)
19238 for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
19239 if (OMP_CLAUSE_CODE (c) == code)
19241 error ("too many %qs clauses", name);
19249 variable-list , identifier
19251 In addition, we match a closing parenthesis. An opening parenthesis
19252 will have been consumed by the caller.
19254 If KIND is nonzero, create the appropriate node and install the decl
19255 in OMP_CLAUSE_DECL and add the node to the head of the list.
19257 If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
19258 return the list created. */
19261 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
19268 name = cp_parser_id_expression (parser, /*template_p=*/false,
19269 /*check_dependency_p=*/true,
19270 /*template_p=*/NULL,
19271 /*declarator_p=*/false,
19272 /*optional_p=*/false);
19273 if (name == error_mark_node)
19276 decl = cp_parser_lookup_name_simple (parser, name);
19277 if (decl == error_mark_node)
19278 cp_parser_name_lookup_error (parser, name, decl, NULL);
19279 else if (kind != 0)
19281 tree u = build_omp_clause (kind);
19282 OMP_CLAUSE_DECL (u) = decl;
19283 OMP_CLAUSE_CHAIN (u) = list;
19287 list = tree_cons (decl, NULL_TREE, list);
19290 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19292 cp_lexer_consume_token (parser->lexer);
19295 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19299 /* Try to resync to an unnested comma. Copied from
19300 cp_parser_parenthesized_expression_list. */
19302 ending = cp_parser_skip_to_closing_parenthesis (parser,
19303 /*recovering=*/true,
19305 /*consume_paren=*/true);
19313 /* Similarly, but expect leading and trailing parenthesis. This is a very
19314 common case for omp clauses. */
19317 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
19319 if (cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19320 return cp_parser_omp_var_list_no_open (parser, kind, list);
19325 default ( shared | none ) */
19328 cp_parser_omp_clause_default (cp_parser *parser, tree list)
19330 enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
19333 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19335 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19337 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19338 const char *p = IDENTIFIER_POINTER (id);
19343 if (strcmp ("none", p) != 0)
19345 kind = OMP_CLAUSE_DEFAULT_NONE;
19349 if (strcmp ("shared", p) != 0)
19351 kind = OMP_CLAUSE_DEFAULT_SHARED;
19358 cp_lexer_consume_token (parser->lexer);
19363 cp_parser_error (parser, "expected %<none%> or %<shared%>");
19366 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19367 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19368 /*or_comma=*/false,
19369 /*consume_paren=*/true);
19371 if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
19374 check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default");
19375 c = build_omp_clause (OMP_CLAUSE_DEFAULT);
19376 OMP_CLAUSE_CHAIN (c) = list;
19377 OMP_CLAUSE_DEFAULT_KIND (c) = kind;
19383 if ( expression ) */
19386 cp_parser_omp_clause_if (cp_parser *parser, tree list)
19390 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19393 t = cp_parser_condition (parser);
19395 if (t == error_mark_node
19396 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19397 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19398 /*or_comma=*/false,
19399 /*consume_paren=*/true);
19401 check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if");
19403 c = build_omp_clause (OMP_CLAUSE_IF);
19404 OMP_CLAUSE_IF_EXPR (c) = t;
19405 OMP_CLAUSE_CHAIN (c) = list;
19414 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19418 check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait");
19420 c = build_omp_clause (OMP_CLAUSE_NOWAIT);
19421 OMP_CLAUSE_CHAIN (c) = list;
19426 num_threads ( expression ) */
19429 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list)
19433 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19436 t = cp_parser_expression (parser, false);
19438 if (t == error_mark_node
19439 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19440 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19441 /*or_comma=*/false,
19442 /*consume_paren=*/true);
19444 check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS, "num_threads");
19446 c = build_omp_clause (OMP_CLAUSE_NUM_THREADS);
19447 OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
19448 OMP_CLAUSE_CHAIN (c) = list;
19457 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED, tree list)
19461 check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED, "ordered");
19463 c = build_omp_clause (OMP_CLAUSE_ORDERED);
19464 OMP_CLAUSE_CHAIN (c) = list;
19469 reduction ( reduction-operator : variable-list )
19471 reduction-operator:
19472 One of: + * - & ^ | && || */
19475 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
19477 enum tree_code code;
19480 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19483 switch (cp_lexer_peek_token (parser->lexer)->type)
19495 code = BIT_AND_EXPR;
19498 code = BIT_XOR_EXPR;
19501 code = BIT_IOR_EXPR;
19504 code = TRUTH_ANDIF_EXPR;
19507 code = TRUTH_ORIF_EXPR;
19510 cp_parser_error (parser, "`+', `*', `-', `&', `^', `|', `&&', or `||'");
19512 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19513 /*or_comma=*/false,
19514 /*consume_paren=*/true);
19517 cp_lexer_consume_token (parser->lexer);
19519 if (!cp_parser_require (parser, CPP_COLON, "`:'"))
19522 nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
19523 for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
19524 OMP_CLAUSE_REDUCTION_CODE (c) = code;
19530 schedule ( schedule-kind )
19531 schedule ( schedule-kind , expression )
19534 static | dynamic | guided | runtime */
19537 cp_parser_omp_clause_schedule (cp_parser *parser, tree list)
19541 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "expected %<(%>"))
19544 c = build_omp_clause (OMP_CLAUSE_SCHEDULE);
19546 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
19548 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
19549 const char *p = IDENTIFIER_POINTER (id);
19554 if (strcmp ("dynamic", p) != 0)
19556 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
19560 if (strcmp ("guided", p) != 0)
19562 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
19566 if (strcmp ("runtime", p) != 0)
19568 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
19575 else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
19576 OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
19579 cp_lexer_consume_token (parser->lexer);
19581 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19583 cp_lexer_consume_token (parser->lexer);
19585 t = cp_parser_assignment_expression (parser, false);
19587 if (t == error_mark_node)
19589 else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
19590 error ("schedule %<runtime%> does not take "
19591 "a %<chunk_size%> parameter");
19593 OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
19595 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19598 else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`,' or `)'"))
19601 check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule");
19602 OMP_CLAUSE_CHAIN (c) = list;
19606 cp_parser_error (parser, "invalid schedule kind");
19608 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19609 /*or_comma=*/false,
19610 /*consume_paren=*/true);
19614 /* Parse all OpenMP clauses. The set clauses allowed by the directive
19615 is a bitmask in MASK. Return the list of clauses found; the result
19616 of clause default goes in *pdefault. */
19619 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
19620 const char *where, cp_token *pragma_tok)
19622 tree clauses = NULL;
19624 while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
19626 pragma_omp_clause c_kind = cp_parser_omp_clause_name (parser);
19627 const char *c_name;
19628 tree prev = clauses;
19632 case PRAGMA_OMP_CLAUSE_COPYIN:
19633 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
19636 case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
19637 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
19639 c_name = "copyprivate";
19641 case PRAGMA_OMP_CLAUSE_DEFAULT:
19642 clauses = cp_parser_omp_clause_default (parser, clauses);
19643 c_name = "default";
19645 case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
19646 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
19648 c_name = "firstprivate";
19650 case PRAGMA_OMP_CLAUSE_IF:
19651 clauses = cp_parser_omp_clause_if (parser, clauses);
19654 case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
19655 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
19657 c_name = "lastprivate";
19659 case PRAGMA_OMP_CLAUSE_NOWAIT:
19660 clauses = cp_parser_omp_clause_nowait (parser, clauses);
19663 case PRAGMA_OMP_CLAUSE_NUM_THREADS:
19664 clauses = cp_parser_omp_clause_num_threads (parser, clauses);
19665 c_name = "num_threads";
19667 case PRAGMA_OMP_CLAUSE_ORDERED:
19668 clauses = cp_parser_omp_clause_ordered (parser, clauses);
19669 c_name = "ordered";
19671 case PRAGMA_OMP_CLAUSE_PRIVATE:
19672 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
19674 c_name = "private";
19676 case PRAGMA_OMP_CLAUSE_REDUCTION:
19677 clauses = cp_parser_omp_clause_reduction (parser, clauses);
19678 c_name = "reduction";
19680 case PRAGMA_OMP_CLAUSE_SCHEDULE:
19681 clauses = cp_parser_omp_clause_schedule (parser, clauses);
19682 c_name = "schedule";
19684 case PRAGMA_OMP_CLAUSE_SHARED:
19685 clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
19690 cp_parser_error (parser, "expected %<#pragma omp%> clause");
19694 if (((mask >> c_kind) & 1) == 0)
19696 /* Remove the invalid clause(s) from the list to avoid
19697 confusing the rest of the compiler. */
19699 error ("%qs is not valid for %qs", c_name, where);
19703 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
19704 return finish_omp_clauses (clauses);
19711 In practice, we're also interested in adding the statement to an
19712 outer node. So it is convenient if we work around the fact that
19713 cp_parser_statement calls add_stmt. */
19716 cp_parser_begin_omp_structured_block (cp_parser *parser)
19718 unsigned save = parser->in_statement;
19720 /* Only move the values to IN_OMP_BLOCK if they weren't false.
19721 This preserves the "not within loop or switch" style error messages
19722 for nonsense cases like
19728 if (parser->in_statement)
19729 parser->in_statement = IN_OMP_BLOCK;
19735 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
19737 parser->in_statement = save;
19741 cp_parser_omp_structured_block (cp_parser *parser)
19743 tree stmt = begin_omp_structured_block ();
19744 unsigned int save = cp_parser_begin_omp_structured_block (parser);
19746 cp_parser_statement (parser, NULL_TREE, false, NULL);
19748 cp_parser_end_omp_structured_block (parser, save);
19749 return finish_omp_structured_block (stmt);
19753 # pragma omp atomic new-line
19757 x binop= expr | x++ | ++x | x-- | --x
19759 +, *, -, /, &, ^, |, <<, >>
19761 where x is an lvalue expression with scalar type. */
19764 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
19767 enum tree_code code;
19769 cp_parser_require_pragma_eol (parser, pragma_tok);
19771 lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
19773 switch (TREE_CODE (lhs))
19778 case PREINCREMENT_EXPR:
19779 case POSTINCREMENT_EXPR:
19780 lhs = TREE_OPERAND (lhs, 0);
19782 rhs = integer_one_node;
19785 case PREDECREMENT_EXPR:
19786 case POSTDECREMENT_EXPR:
19787 lhs = TREE_OPERAND (lhs, 0);
19789 rhs = integer_one_node;
19793 switch (cp_lexer_peek_token (parser->lexer)->type)
19799 code = TRUNC_DIV_EXPR;
19807 case CPP_LSHIFT_EQ:
19808 code = LSHIFT_EXPR;
19810 case CPP_RSHIFT_EQ:
19811 code = RSHIFT_EXPR;
19814 code = BIT_AND_EXPR;
19817 code = BIT_IOR_EXPR;
19820 code = BIT_XOR_EXPR;
19823 cp_parser_error (parser,
19824 "invalid operator for %<#pragma omp atomic%>");
19827 cp_lexer_consume_token (parser->lexer);
19829 rhs = cp_parser_expression (parser, false);
19830 if (rhs == error_mark_node)
19834 finish_omp_atomic (code, lhs, rhs);
19835 cp_parser_consume_semicolon_at_end_of_statement (parser);
19839 cp_parser_skip_to_end_of_block_or_statement (parser);
19844 # pragma omp barrier new-line */
19847 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
19849 cp_parser_require_pragma_eol (parser, pragma_tok);
19850 finish_omp_barrier ();
19854 # pragma omp critical [(name)] new-line
19855 structured-block */
19858 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
19860 tree stmt, name = NULL;
19862 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19864 cp_lexer_consume_token (parser->lexer);
19866 name = cp_parser_identifier (parser);
19868 if (name == error_mark_node
19869 || !cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19870 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19871 /*or_comma=*/false,
19872 /*consume_paren=*/true);
19873 if (name == error_mark_node)
19876 cp_parser_require_pragma_eol (parser, pragma_tok);
19878 stmt = cp_parser_omp_structured_block (parser);
19879 return c_finish_omp_critical (stmt, name);
19883 # pragma omp flush flush-vars[opt] new-line
19886 ( variable-list ) */
19889 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
19891 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19892 (void) cp_parser_omp_var_list (parser, 0, NULL);
19893 cp_parser_require_pragma_eol (parser, pragma_tok);
19895 finish_omp_flush ();
19898 /* Parse the restricted form of the for statment allowed by OpenMP. */
19901 cp_parser_omp_for_loop (cp_parser *parser)
19903 tree init, cond, incr, body, decl, pre_body;
19906 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
19908 cp_parser_error (parser, "for statement expected");
19911 loc = cp_lexer_consume_token (parser->lexer)->location;
19912 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
19915 init = decl = NULL;
19916 pre_body = push_stmt_list ();
19917 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19919 cp_decl_specifier_seq type_specifiers;
19921 /* First, try to parse as an initialized declaration. See
19922 cp_parser_condition, from whence the bulk of this is copied. */
19924 cp_parser_parse_tentatively (parser);
19925 cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
19927 if (!cp_parser_error_occurred (parser))
19929 tree asm_specification, attributes;
19930 cp_declarator *declarator;
19932 declarator = cp_parser_declarator (parser,
19933 CP_PARSER_DECLARATOR_NAMED,
19934 /*ctor_dtor_or_conv_p=*/NULL,
19935 /*parenthesized_p=*/NULL,
19936 /*member_p=*/false);
19937 attributes = cp_parser_attributes_opt (parser);
19938 asm_specification = cp_parser_asm_specification_opt (parser);
19940 cp_parser_require (parser, CPP_EQ, "`='");
19941 if (cp_parser_parse_definitely (parser))
19945 decl = start_decl (declarator, &type_specifiers,
19946 /*initialized_p=*/false, attributes,
19947 /*prefix_attributes=*/NULL_TREE,
19950 init = cp_parser_assignment_expression (parser, false);
19952 cp_finish_decl (decl, NULL_TREE, /*init_const_expr_p=*/false,
19953 asm_specification, LOOKUP_ONLYCONVERTING);
19956 pop_scope (pushed_scope);
19960 cp_parser_abort_tentative_parse (parser);
19962 /* If parsing as an initialized declaration failed, try again as
19963 a simple expression. */
19965 init = cp_parser_expression (parser, false);
19967 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19968 pre_body = pop_stmt_list (pre_body);
19971 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19972 cond = cp_parser_condition (parser);
19973 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
19976 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
19977 incr = cp_parser_expression (parser, false);
19979 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
19980 cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
19981 /*or_comma=*/false,
19982 /*consume_paren=*/true);
19984 /* Note that we saved the original contents of this flag when we entered
19985 the structured block, and so we don't need to re-save it here. */
19986 parser->in_statement = IN_OMP_FOR;
19988 /* Note that the grammar doesn't call for a structured block here,
19989 though the loop as a whole is a structured block. */
19990 body = push_stmt_list ();
19991 cp_parser_statement (parser, NULL_TREE, false, NULL);
19992 body = pop_stmt_list (body);
19994 return finish_omp_for (loc, decl, init, cond, incr, body, pre_body);
19998 #pragma omp for for-clause[optseq] new-line
20001 #define OMP_FOR_CLAUSE_MASK \
20002 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20003 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20004 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20005 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20006 | (1u << PRAGMA_OMP_CLAUSE_ORDERED) \
20007 | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE) \
20008 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20011 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
20013 tree clauses, sb, ret;
20016 clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
20017 "#pragma omp for", pragma_tok);
20019 sb = begin_omp_structured_block ();
20020 save = cp_parser_begin_omp_structured_block (parser);
20022 ret = cp_parser_omp_for_loop (parser);
20024 OMP_FOR_CLAUSES (ret) = clauses;
20026 cp_parser_end_omp_structured_block (parser, save);
20027 add_stmt (finish_omp_structured_block (sb));
20033 # pragma omp master new-line
20034 structured-block */
20037 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
20039 cp_parser_require_pragma_eol (parser, pragma_tok);
20040 return c_finish_omp_master (cp_parser_omp_structured_block (parser));
20044 # pragma omp ordered new-line
20045 structured-block */
20048 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
20050 cp_parser_require_pragma_eol (parser, pragma_tok);
20051 return c_finish_omp_ordered (cp_parser_omp_structured_block (parser));
20057 { section-sequence }
20060 section-directive[opt] structured-block
20061 section-sequence section-directive structured-block */
20064 cp_parser_omp_sections_scope (cp_parser *parser)
20066 tree stmt, substmt;
20067 bool error_suppress = false;
20070 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
20073 stmt = push_stmt_list ();
20075 if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
20079 substmt = begin_omp_structured_block ();
20080 save = cp_parser_begin_omp_structured_block (parser);
20084 cp_parser_statement (parser, NULL_TREE, false, NULL);
20086 tok = cp_lexer_peek_token (parser->lexer);
20087 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20089 if (tok->type == CPP_CLOSE_BRACE)
20091 if (tok->type == CPP_EOF)
20095 cp_parser_end_omp_structured_block (parser, save);
20096 substmt = finish_omp_structured_block (substmt);
20097 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20098 add_stmt (substmt);
20103 tok = cp_lexer_peek_token (parser->lexer);
20104 if (tok->type == CPP_CLOSE_BRACE)
20106 if (tok->type == CPP_EOF)
20109 if (tok->pragma_kind == PRAGMA_OMP_SECTION)
20111 cp_lexer_consume_token (parser->lexer);
20112 cp_parser_require_pragma_eol (parser, tok);
20113 error_suppress = false;
20115 else if (!error_suppress)
20117 cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
20118 error_suppress = true;
20121 substmt = cp_parser_omp_structured_block (parser);
20122 substmt = build1 (OMP_SECTION, void_type_node, substmt);
20123 add_stmt (substmt);
20125 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
20127 substmt = pop_stmt_list (stmt);
20129 stmt = make_node (OMP_SECTIONS);
20130 TREE_TYPE (stmt) = void_type_node;
20131 OMP_SECTIONS_BODY (stmt) = substmt;
20138 # pragma omp sections sections-clause[optseq] newline
20141 #define OMP_SECTIONS_CLAUSE_MASK \
20142 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20143 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20144 | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE) \
20145 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20146 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20149 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
20153 clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
20154 "#pragma omp sections", pragma_tok);
20156 ret = cp_parser_omp_sections_scope (parser);
20158 OMP_SECTIONS_CLAUSES (ret) = clauses;
20164 # pragma parallel parallel-clause new-line
20165 # pragma parallel for parallel-for-clause new-line
20166 # pragma parallel sections parallel-sections-clause new-line */
20168 #define OMP_PARALLEL_CLAUSE_MASK \
20169 ( (1u << PRAGMA_OMP_CLAUSE_IF) \
20170 | (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20171 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20172 | (1u << PRAGMA_OMP_CLAUSE_DEFAULT) \
20173 | (1u << PRAGMA_OMP_CLAUSE_SHARED) \
20174 | (1u << PRAGMA_OMP_CLAUSE_COPYIN) \
20175 | (1u << PRAGMA_OMP_CLAUSE_REDUCTION) \
20176 | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
20179 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
20181 enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
20182 const char *p_name = "#pragma omp parallel";
20183 tree stmt, clauses, par_clause, ws_clause, block;
20184 unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
20187 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
20189 cp_lexer_consume_token (parser->lexer);
20190 p_kind = PRAGMA_OMP_PARALLEL_FOR;
20191 p_name = "#pragma omp parallel for";
20192 mask |= OMP_FOR_CLAUSE_MASK;
20193 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20195 else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
20197 tree id = cp_lexer_peek_token (parser->lexer)->u.value;
20198 const char *p = IDENTIFIER_POINTER (id);
20199 if (strcmp (p, "sections") == 0)
20201 cp_lexer_consume_token (parser->lexer);
20202 p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
20203 p_name = "#pragma omp parallel sections";
20204 mask |= OMP_SECTIONS_CLAUSE_MASK;
20205 mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
20209 clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
20210 block = begin_omp_parallel ();
20211 save = cp_parser_begin_omp_structured_block (parser);
20215 case PRAGMA_OMP_PARALLEL:
20216 cp_parser_already_scoped_statement (parser);
20217 par_clause = clauses;
20220 case PRAGMA_OMP_PARALLEL_FOR:
20221 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20222 stmt = cp_parser_omp_for_loop (parser);
20224 OMP_FOR_CLAUSES (stmt) = ws_clause;
20227 case PRAGMA_OMP_PARALLEL_SECTIONS:
20228 c_split_parallel_clauses (clauses, &par_clause, &ws_clause);
20229 stmt = cp_parser_omp_sections_scope (parser);
20231 OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
20235 gcc_unreachable ();
20238 cp_parser_end_omp_structured_block (parser, save);
20239 stmt = finish_omp_parallel (par_clause, block);
20240 if (p_kind != PRAGMA_OMP_PARALLEL)
20241 OMP_PARALLEL_COMBINED (stmt) = 1;
20246 # pragma omp single single-clause[optseq] new-line
20247 structured-block */
20249 #define OMP_SINGLE_CLAUSE_MASK \
20250 ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE) \
20251 | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE) \
20252 | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE) \
20253 | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
20256 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
20258 tree stmt = make_node (OMP_SINGLE);
20259 TREE_TYPE (stmt) = void_type_node;
20261 OMP_SINGLE_CLAUSES (stmt)
20262 = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
20263 "#pragma omp single", pragma_tok);
20264 OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
20266 return add_stmt (stmt);
20270 # pragma omp threadprivate (variable-list) */
20273 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
20277 vars = cp_parser_omp_var_list (parser, 0, NULL);
20278 cp_parser_require_pragma_eol (parser, pragma_tok);
20280 finish_omp_threadprivate (vars);
20283 /* Main entry point to OpenMP statement pragmas. */
20286 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
20290 switch (pragma_tok->pragma_kind)
20292 case PRAGMA_OMP_ATOMIC:
20293 cp_parser_omp_atomic (parser, pragma_tok);
20295 case PRAGMA_OMP_CRITICAL:
20296 stmt = cp_parser_omp_critical (parser, pragma_tok);
20298 case PRAGMA_OMP_FOR:
20299 stmt = cp_parser_omp_for (parser, pragma_tok);
20301 case PRAGMA_OMP_MASTER:
20302 stmt = cp_parser_omp_master (parser, pragma_tok);
20304 case PRAGMA_OMP_ORDERED:
20305 stmt = cp_parser_omp_ordered (parser, pragma_tok);
20307 case PRAGMA_OMP_PARALLEL:
20308 stmt = cp_parser_omp_parallel (parser, pragma_tok);
20310 case PRAGMA_OMP_SECTIONS:
20311 stmt = cp_parser_omp_sections (parser, pragma_tok);
20313 case PRAGMA_OMP_SINGLE:
20314 stmt = cp_parser_omp_single (parser, pragma_tok);
20317 gcc_unreachable ();
20321 SET_EXPR_LOCATION (stmt, pragma_tok->location);
20326 static GTY (()) cp_parser *the_parser;
20329 /* Special handling for the first token or line in the file. The first
20330 thing in the file might be #pragma GCC pch_preprocess, which loads a
20331 PCH file, which is a GC collection point. So we need to handle this
20332 first pragma without benefit of an existing lexer structure.
20334 Always returns one token to the caller in *FIRST_TOKEN. This is
20335 either the true first token of the file, or the first token after
20336 the initial pragma. */
20339 cp_parser_initial_pragma (cp_token *first_token)
20343 cp_lexer_get_preprocessor_token (NULL, first_token);
20344 if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
20347 cp_lexer_get_preprocessor_token (NULL, first_token);
20348 if (first_token->type == CPP_STRING)
20350 name = first_token->u.value;
20352 cp_lexer_get_preprocessor_token (NULL, first_token);
20353 if (first_token->type != CPP_PRAGMA_EOL)
20354 error ("junk at end of %<#pragma GCC pch_preprocess%>");
20357 error ("expected string literal");
20359 /* Skip to the end of the pragma. */
20360 while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
20361 cp_lexer_get_preprocessor_token (NULL, first_token);
20363 /* Now actually load the PCH file. */
20365 c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
20367 /* Read one more token to return to our caller. We have to do this
20368 after reading the PCH file in, since its pointers have to be
20370 cp_lexer_get_preprocessor_token (NULL, first_token);
20373 /* Normal parsing of a pragma token. Here we can (and must) use the
20377 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
20379 cp_token *pragma_tok;
20382 pragma_tok = cp_lexer_consume_token (parser->lexer);
20383 gcc_assert (pragma_tok->type == CPP_PRAGMA);
20384 parser->lexer->in_pragma = true;
20386 id = pragma_tok->pragma_kind;
20389 case PRAGMA_GCC_PCH_PREPROCESS:
20390 error ("%<#pragma GCC pch_preprocess%> must be first");
20393 case PRAGMA_OMP_BARRIER:
20396 case pragma_compound:
20397 cp_parser_omp_barrier (parser, pragma_tok);
20400 error ("%<#pragma omp barrier%> may only be "
20401 "used in compound statements");
20408 case PRAGMA_OMP_FLUSH:
20411 case pragma_compound:
20412 cp_parser_omp_flush (parser, pragma_tok);
20415 error ("%<#pragma omp flush%> may only be "
20416 "used in compound statements");
20423 case PRAGMA_OMP_THREADPRIVATE:
20424 cp_parser_omp_threadprivate (parser, pragma_tok);
20427 case PRAGMA_OMP_ATOMIC:
20428 case PRAGMA_OMP_CRITICAL:
20429 case PRAGMA_OMP_FOR:
20430 case PRAGMA_OMP_MASTER:
20431 case PRAGMA_OMP_ORDERED:
20432 case PRAGMA_OMP_PARALLEL:
20433 case PRAGMA_OMP_SECTIONS:
20434 case PRAGMA_OMP_SINGLE:
20435 if (context == pragma_external)
20437 cp_parser_omp_construct (parser, pragma_tok);
20440 case PRAGMA_OMP_SECTION:
20441 error ("%<#pragma omp section%> may only be used in "
20442 "%<#pragma omp sections%> construct");
20446 gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
20447 c_invoke_pragma_handler (id);
20451 cp_parser_error (parser, "expected declaration specifiers");
20455 cp_parser_skip_to_pragma_eol (parser, pragma_tok);
20459 /* The interface the pragma parsers have to the lexer. */
20462 pragma_lex (tree *value)
20465 enum cpp_ttype ret;
20467 tok = cp_lexer_peek_token (the_parser->lexer);
20470 *value = tok->u.value;
20472 if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
20474 else if (ret == CPP_STRING)
20475 *value = cp_parser_string_literal (the_parser, false, false);
20478 cp_lexer_consume_token (the_parser->lexer);
20479 if (ret == CPP_KEYWORD)
20487 /* External interface. */
20489 /* Parse one entire translation unit. */
20492 c_parse_file (void)
20494 bool error_occurred;
20495 static bool already_called = false;
20497 if (already_called)
20499 sorry ("inter-module optimizations not implemented for C++");
20502 already_called = true;
20504 the_parser = cp_parser_new ();
20505 push_deferring_access_checks (flag_access_control
20506 ? dk_no_deferred : dk_no_check);
20507 error_occurred = cp_parser_translation_unit (the_parser);
20511 #include "gt-cp-parser.h"